Skip to content

Commit a5b8046

Browse files
authored
Merge pull request #35110 from bitjammer/acgarland/emit-symbol-graph
Add optional -emit-symbol-graph output when emitting modules
2 parents efe32c4 + 69c4fc4 commit a5b8046

File tree

17 files changed

+114
-27
lines changed

17 files changed

+114
-27
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ class alignas(1 << DeclAlignInBits) Decl {
871871
}
872872

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

876876
Optional<StringRef> getGroupName() const;
877877

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ ERROR(error_mode_cannot_emit_interface,none,
123123
"this mode does not support emitting module interface files", ())
124124
ERROR(error_mode_cannot_emit_module_summary,none,
125125
"this mode does not support emitting module summary files", ())
126+
ERROR(error_mode_cannot_emit_symbol_graph,none,
127+
"this mode does not support emitting symbol graph files", ())
126128
ERROR(cannot_emit_ir_skipping_function_bodies,none,
127129
"the -experimental-skip-*-function-bodies* flags do not support "
128130
"emitting IR", ())

include/swift/Frontend/FrontendOptions.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,19 @@ class FrontendOptions {
376376

377377
/// Whether we're configured to track system intermodule dependencies.
378378
bool shouldTrackSystemDependencies() const;
379+
380+
/// Whether to emit symbol graphs for the output module.
381+
bool EmitSymbolGraph = false;
382+
383+
/// The directory to which we should emit a symbol graph JSON files.
384+
/// It is valid whenever there are any inputs.
385+
///
386+
/// These are JSON file that describes the public interface of a module for
387+
/// curating documentation, separated into files for each module this module
388+
/// extends.
389+
///
390+
/// \sa SymbolGraphASTWalker
391+
std::string SymbolGraphOutputDir;
379392

380393
private:
381394
static bool canActionEmitDependencies(ActionType);

include/swift/Option/Options.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,4 +1166,15 @@ def disable_autolinking_runtime_compatibility_dynamic_replacements
11661166
HelpText<"Do not use autolinking for the dynamic replacement runtime "
11671167
"compatibility library">;
11681168

1169+
def emit_symbol_graph: Flag<["-"], "emit-symbol-graph">,
1170+
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild,
1171+
SupplementaryOutput, HelpHidden]>,
1172+
HelpText<"Emit a symbol graph">;
1173+
1174+
def emit_symbol_graph_dir : Separate<["-"], "emit-symbol-graph-dir">,
1175+
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild,
1176+
ArgumentIsPath, SupplementaryOutput, HelpHidden]>,
1177+
HelpText<"Emit a symbol graph to directory <dir>">,
1178+
MetaVarName<"<dir>">;
1179+
11691180
include "FrontendOptions.td"

include/swift/Serialization/SerializationOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace swift {
3030
const char *OutputPath = nullptr;
3131
const char *DocOutputPath = nullptr;
3232
const char *SourceInfoOutputPath = nullptr;
33+
std::string SymbolGraphOutputDir;
3334

3435
StringRef GroupInfoPath;
3536
StringRef ImportedHeader;

lib/AST/Module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ SourceFile::getBasicLocsForDecl(const Decl *D) const {
878878
BasicDeclLocs Result;
879879
Result.SourceFilePath = SM.getDisplayNameForLoc(D->getLoc());
880880

881-
for (const auto &SRC : D->getRawComment().Comments) {
881+
for (const auto &SRC : D->getRawComment(/*SerializedOK*/false).Comments) {
882882
Result.DocRanges.push_back(std::make_pair(
883883
LineColumn { SRC.StartLine, SRC.StartColumn },
884884
SRC.Range.getByteLength())

lib/Driver/ToolChains.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,11 @@ ToolChain::constructInvocation(const CompileJobAction &job,
557557
options::
558558
OPT_disable_autolinking_runtime_compatibility_dynamic_replacements);
559559

560+
if (context.OI.CompilerMode == OutputInfo::Mode::SingleCompile) {
561+
context.Args.AddLastArg(Arguments, options::OPT_emit_symbol_graph);
562+
context.Args.AddLastArg(Arguments, options::OPT_emit_symbol_graph_dir);
563+
}
564+
560565
return II;
561566
}
562567

@@ -1040,6 +1045,9 @@ ToolChain::constructInvocation(const MergeModuleJobAction &job,
10401045
addOutputsOfType(Arguments, context.Output, context.Args, file_types::TY_TBD,
10411046
"-emit-tbd-path");
10421047

1048+
context.Args.AddLastArg(Arguments, options::OPT_emit_symbol_graph);
1049+
context.Args.AddLastArg(Arguments, options::OPT_emit_symbol_graph_dir);
1050+
10431051
context.Args.AddLastArg(Arguments, options::OPT_import_objc_header);
10441052

10451053
context.Args.AddLastArg(

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ bool ArgsToFrontendOptionsConverter::convert(
237237
computeImplicitImportModuleNames(OPT_testable_import_module, /*isTestable=*/true);
238238
computeLLVMArgs();
239239

240+
Opts.EmitSymbolGraph |= Args.hasArg(OPT_emit_symbol_graph);
241+
242+
if (const Arg *A = Args.getLastArg(OPT_emit_symbol_graph_dir)) {
243+
Opts.SymbolGraphOutputDir = A->getValue();
244+
}
245+
240246
return false;
241247
}
242248

@@ -584,6 +590,11 @@ bool ArgsToFrontendOptionsConverter::checkUnusedSupplementaryOutputPaths()
584590
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_module_summary);
585591
return true;
586592
}
593+
if (!FrontendOptions::canActionEmitModule(Opts.RequestedAction) &&
594+
!Opts.SymbolGraphOutputDir.empty()) {
595+
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_symbol_graph);
596+
return true;
597+
}
587598
return false;
588599
}
589600

lib/Frontend/ArgsToFrontendOutputsConverter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,8 @@ createFromTypeToPathMap(const TypeToPathMap *map) {
536536
paths.ModuleInterfaceOutputPath},
537537
{file_types::TY_SwiftModuleSummaryFile, paths.ModuleSummaryOutputPath},
538538
{file_types::TY_PrivateSwiftModuleInterfaceFile,
539-
paths.PrivateModuleInterfaceOutputPath}};
539+
paths.PrivateModuleInterfaceOutputPath},
540+
};
540541
for (const std::pair<file_types::ID, std::string &> &typeAndString :
541542
typesAndStrings) {
542543
auto const out = map->find(typeAndString.first);
@@ -559,7 +560,8 @@ SupplementaryOutputPathsComputer::readSupplementaryOutputFileMap() const {
559560
options::OPT_emit_private_module_interface_path,
560561
options::OPT_emit_module_source_info_path,
561562
options::OPT_emit_tbd_path,
562-
options::OPT_emit_ldadd_cfile_path)) {
563+
options::OPT_emit_ldadd_cfile_path,
564+
options::OPT_emit_symbol_graph_dir)) {
563565
Diags.diagnose(SourceLoc(),
564566
diag::error_cannot_have_supplementary_outputs,
565567
A->getSpelling(), "-supplementary-output-file-map");

lib/Frontend/CompilerInvocation.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,12 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
686686
Opts.AttachCommentsToDecls = true;
687687
}
688688

689+
// If we are emitting a symbol graph file, configure lexing and parsing to
690+
// remember comments.
691+
if (FrontendOpts.EmitSymbolGraph) {
692+
Opts.AttachCommentsToDecls = true;
693+
}
694+
689695
// If we're parsing SIL, access control doesn't make sense to enforce.
690696
if (Args.hasArg(OPT_parse_sil) ||
691697
FrontendOpts.InputsAndOutputs.shouldTreatAsSIL()) {

0 commit comments

Comments
 (0)