Skip to content

Add optional -emit-symbol-graph output when emitting modules #35110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ class alignas(1 << DeclAlignInBits) Decl {
}

/// \returns the unparsed comment attached to this declaration.
RawComment getRawComment(bool SerializedOK = false) const;
RawComment getRawComment(bool SerializedOK = true) const;

Optional<StringRef> getGroupName() const;

Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ ERROR(error_mode_cannot_emit_interface,none,
"this mode does not support emitting module interface files", ())
ERROR(error_mode_cannot_emit_module_summary,none,
"this mode does not support emitting module summary files", ())
ERROR(error_mode_cannot_emit_symbol_graph,none,
"this mode does not support emitting symbol graph files", ())
ERROR(cannot_emit_ir_skipping_function_bodies,none,
"the -experimental-skip-*-function-bodies* flags do not support "
"emitting IR", ())
Expand Down
13 changes: 13 additions & 0 deletions include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,19 @@ class FrontendOptions {

/// Whether we're configured to track system intermodule dependencies.
bool shouldTrackSystemDependencies() const;

/// Whether to emit symbol graphs for the output module.
bool EmitSymbolGraph = false;

/// The directory to which we should emit a symbol graph JSON files.
/// It is valid whenever there are any inputs.
///
/// These are JSON file that describes the public interface of a module for
/// curating documentation, separated into files for each module this module
/// extends.
///
/// \sa SymbolGraphASTWalker
std::string SymbolGraphOutputDir;

private:
static bool canActionEmitDependencies(ActionType);
Expand Down
11 changes: 11 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1159,4 +1159,15 @@ def disable_autolinking_runtime_compatibility_dynamic_replacements
HelpText<"Do not use autolinking for the dynamic replacement runtime "
"compatibility library">;

def emit_symbol_graph: Flag<["-"], "emit-symbol-graph">,
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild,
SupplementaryOutput, HelpHidden]>,
HelpText<"Emit a symbol graph">;

def emit_symbol_graph_dir : Separate<["-"], "emit-symbol-graph-dir">,
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild,
ArgumentIsPath, SupplementaryOutput, HelpHidden]>,
HelpText<"Emit a symbol graph to directory <dir>">,
MetaVarName<"<dir>">;

include "FrontendOptions.td"
1 change: 1 addition & 0 deletions include/swift/Serialization/SerializationOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace swift {
const char *OutputPath = nullptr;
const char *DocOutputPath = nullptr;
const char *SourceInfoOutputPath = nullptr;
std::string SymbolGraphOutputDir;

StringRef GroupInfoPath;
StringRef ImportedHeader;
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ SourceFile::getBasicLocsForDecl(const Decl *D) const {
BasicDeclLocs Result;
Result.SourceFilePath = SM.getDisplayNameForLoc(D->getLoc());

for (const auto &SRC : D->getRawComment().Comments) {
for (const auto &SRC : D->getRawComment(/*SerializedOK*/false).Comments) {
Result.DocRanges.push_back(std::make_pair(
LineColumn { SRC.StartLine, SRC.StartColumn },
SRC.Range.getByteLength())
Expand Down
8 changes: 8 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,11 @@ ToolChain::constructInvocation(const CompileJobAction &job,
options::
OPT_disable_autolinking_runtime_compatibility_dynamic_replacements);

if (context.OI.CompilerMode == OutputInfo::Mode::SingleCompile) {
context.Args.AddLastArg(Arguments, options::OPT_emit_symbol_graph);
context.Args.AddLastArg(Arguments, options::OPT_emit_symbol_graph_dir);
}

return II;
}

Expand Down Expand Up @@ -1039,6 +1044,9 @@ ToolChain::constructInvocation(const MergeModuleJobAction &job,
addOutputsOfType(Arguments, context.Output, context.Args, file_types::TY_TBD,
"-emit-tbd-path");

context.Args.AddLastArg(Arguments, options::OPT_emit_symbol_graph);
context.Args.AddLastArg(Arguments, options::OPT_emit_symbol_graph_dir);

context.Args.AddLastArg(Arguments, options::OPT_import_objc_header);

context.Args.AddLastArg(
Expand Down
11 changes: 11 additions & 0 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ bool ArgsToFrontendOptionsConverter::convert(
computeImplicitImportModuleNames(OPT_testable_import_module, /*isTestable=*/true);
computeLLVMArgs();

Opts.EmitSymbolGraph |= Args.hasArg(OPT_emit_symbol_graph);

if (const Arg *A = Args.getLastArg(OPT_emit_symbol_graph_dir)) {
Opts.SymbolGraphOutputDir = A->getValue();
}

return false;
}

Expand Down Expand Up @@ -581,6 +587,11 @@ bool ArgsToFrontendOptionsConverter::checkUnusedSupplementaryOutputPaths()
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_module_summary);
return true;
}
if (!FrontendOptions::canActionEmitModule(Opts.RequestedAction) &&
!Opts.SymbolGraphOutputDir.empty()) {
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_symbol_graph);
return true;
}
return false;
}

Expand Down
6 changes: 4 additions & 2 deletions lib/Frontend/ArgsToFrontendOutputsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,8 @@ createFromTypeToPathMap(const TypeToPathMap *map) {
paths.ModuleInterfaceOutputPath},
{file_types::TY_SwiftModuleSummaryFile, paths.ModuleSummaryOutputPath},
{file_types::TY_PrivateSwiftModuleInterfaceFile,
paths.PrivateModuleInterfaceOutputPath}};
paths.PrivateModuleInterfaceOutputPath},
};
for (const std::pair<file_types::ID, std::string &> &typeAndString :
typesAndStrings) {
auto const out = map->find(typeAndString.first);
Expand All @@ -559,7 +560,8 @@ SupplementaryOutputPathsComputer::readSupplementaryOutputFileMap() const {
options::OPT_emit_private_module_interface_path,
options::OPT_emit_module_source_info_path,
options::OPT_emit_tbd_path,
options::OPT_emit_ldadd_cfile_path)) {
options::OPT_emit_ldadd_cfile_path,
options::OPT_emit_symbol_graph_dir)) {
Diags.diagnose(SourceLoc(),
diag::error_cannot_have_supplementary_outputs,
A->getSpelling(), "-supplementary-output-file-map");
Expand Down
6 changes: 6 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,12 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
Opts.AttachCommentsToDecls = true;
}

// If we are emitting a symbol graph file, configure lexing and parsing to
// remember comments.
if (FrontendOpts.EmitSymbolGraph) {
Opts.AttachCommentsToDecls = true;
}

// If we're parsing SIL, access control doesn't make sense to enforce.
if (Args.hasArg(OPT_parse_sil) ||
FrontendOpts.InputsAndOutputs.shouldTreatAsSIL()) {
Expand Down
13 changes: 13 additions & 0 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
serializationOpts.ImportedHeader = opts.ImplicitObjCHeaderPath;
serializationOpts.ModuleLinkName = opts.ModuleLinkName;
serializationOpts.ExtraClangOptions = getClangImporterOptions().ExtraArgs;

if (opts.EmitSymbolGraph) {
if (!opts.SymbolGraphOutputDir.empty()) {
serializationOpts.SymbolGraphOutputDir = opts.SymbolGraphOutputDir;
} else {
serializationOpts.SymbolGraphOutputDir = serializationOpts.OutputPath;
}
SmallString<256> OutputDir(serializationOpts.SymbolGraphOutputDir);
llvm::sys::fs::make_absolute(OutputDir);
llvm::sys::path::remove_filename(OutputDir);
serializationOpts.SymbolGraphOutputDir = OutputDir.str().str();
}

if (!getIRGenOptions().ForceLoadSymbolName.empty())
serializationOpts.AutolinkForceLoad = true;

Expand Down
3 changes: 2 additions & 1 deletion lib/Serialization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ add_swift_host_library(swiftSerialization STATIC
)
target_link_libraries(swiftSerialization PRIVATE
swiftClangImporter
swiftSIL)
swiftSIL
swiftSymbolGraphGen)

18 changes: 18 additions & 0 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#include "swift/Serialization/SerializationOptions.h"
#include "swift/Strings.h"
#include "clang/AST/DeclTemplate.h"
#include "swift/SymbolGraphGen/SymbolGraphOptions.h"
#include "swift/SymbolGraphGen/SymbolGraphGen.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
Expand Down Expand Up @@ -5544,4 +5546,20 @@ void swift::serialize(ModuleOrSourceFile DC,
return false;
});
}

if (!options.SymbolGraphOutputDir.empty()) {
if (DC.is<ModuleDecl *>()) {
auto *M = DC.get<ModuleDecl*>();
FrontendStatsTracer tracer(getContext(DC).Stats,
"Serialization, symbolgraph");
symbolgraphgen::SymbolGraphOptions SGOpts {
options.SymbolGraphOutputDir,
M->getASTContext().LangOpts.Target,
/* PrettyPrint */false,
AccessLevel::Public,
/*EmitSynthesizedMembers*/true,
};
symbolgraphgen::emitSymbolGraphForModule(M, SGOpts);
}
}
}
6 changes: 2 additions & 4 deletions lib/SymbolGraphGen/SymbolGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,9 @@ void SymbolGraph::serialize(llvm::json::OStream &OS) {
case FileUnitKind::Synthesized:
llvm_unreachable("Unexpected module kind: Synthesized");
break;
case FileUnitKind::Source: {
auto Target = MainFile->getASTContext().LangOpts.Target;
symbolgraphgen::serialize(Target, OS);
case FileUnitKind::Source:
symbolgraphgen::serialize(M.getASTContext().LangOpts.Target, OS);
break;
}
case FileUnitKind::SerializedAST: {
auto SerializedAST = cast<SerializedASTFile>(MainFile);
auto Target = llvm::Triple(SerializedAST->getTargetTriple());
Expand Down
19 changes: 7 additions & 12 deletions lib/SymbolGraphGen/SymbolGraphGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include "llvm/Support/JSON.h"
#include "llvm/Support/Path.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/FileSystem.h"
#include "swift/SymbolGraphGen/SymbolGraphGen.h"

#include "SymbolGraphASTWalker.h"
Expand Down Expand Up @@ -49,18 +51,11 @@ int serializeSymbolGraph(SymbolGraph &SG,
SmallString<1024> OutputPath(Options.OutputDir);
llvm::sys::path::append(OutputPath, FileName);

std::error_code Error;
llvm::raw_fd_ostream OS(OutputPath, Error, llvm::sys::fs::FA_Write);
if (Error) {
llvm::errs() << "Couldn't open output file '" << OutputPath
<< " for writing: "
<< Error.message() << "\n";
return EXIT_FAILURE;
}

llvm::json::OStream J(OS, Options.PrettyPrint ? 2 : 0);
SG.serialize(J);
return EXIT_SUCCESS;
return withOutputFile(SG.M.getASTContext().Diags, OutputPath, [&](raw_ostream &OS) {
llvm::json::OStream J(OS, Options.PrettyPrint ? 2 : 0);
SG.serialize(J);
return false;
});
}

} // end anonymous namespace
Expand Down
12 changes: 6 additions & 6 deletions test/Serialization/comments-hidden.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ private class PrivateClass {
// TESTING: InternalClass Documentation
// TESTING: Internal Function Documentation

// SOURCE-LOC: comments-hidden.swift:37:15: Func/PublicClass.__UnderscoredPublic RawComment=none BriefComment=none DocCommentAsXML=none
// SOURCE-LOC: comments-hidden.swift:39:10: Constructor/PublicClass.init RawComment=none BriefComment=none DocCommentAsXML=none
// SOURCE-LOC: comments-hidden.swift:41:10: Subscript/PublicClass.subscript RawComment=none BriefComment=none DocCommentAsXML=none
// SOURCE-LOC: comments-hidden.swift:43:10: Constructor/PublicClass.init RawComment=none BriefComment=none DocCommentAsXML=none
// SOURCE-LOC: comments-hidden.swift:45:10: Subscript/PublicClass.subscript RawComment=none BriefComment=none DocCommentAsXML=none
// SOURCE-LOC: comments-hidden.swift:52:15: Func/-= RawComment=none BriefComment=none DocCommentAsXML=none
// SOURCE-LOC: comments-hidden.swift:37:15: Func/PublicClass.__UnderscoredPublic
// SOURCE-LOC: comments-hidden.swift:39:10: Constructor/PublicClass.init
// SOURCE-LOC: comments-hidden.swift:41:10: Subscript/PublicClass.subscript
// SOURCE-LOC: comments-hidden.swift:43:10: Constructor/PublicClass.init
// SOURCE-LOC: comments-hidden.swift:45:10: Subscript/PublicClass.subscript
// SOURCE-LOC: comments-hidden.swift:52:15: Func/-=
8 changes: 8 additions & 0 deletions test/SymbolGraph/EmitWhileBuilding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -module-name EmitWhileBuilding -emit-module -emit-module-path %t/ -emit-symbol-graph -emit-symbol-graph-dir %t/
// RUN: %FileCheck %s --input-file %t/EmitWhileBuilding.symbols.json

/// Does a foo.
public func foo() {}

// CHECK: "precise":"s:17EmitWhileBuilding3fooyyF"