Skip to content

Commit 69c4fc4

Browse files
Ashley Garlandbitjammer
authored andcommitted
Add optional -emit-symbol-graph output when emitting modules
rdar://71497047
1 parent 96397ec commit 69c4fc4

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
@@ -866,7 +866,7 @@ class alignas(1 << DeclAlignInBits) Decl {
866866
}
867867

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

871871
Optional<StringRef> getGroupName() const;
872872

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
@@ -373,6 +373,19 @@ class FrontendOptions {
373373

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

377390
private:
378391
static bool canActionEmitDependencies(ActionType);

include/swift/Option/Options.td

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

1162+
def emit_symbol_graph: Flag<["-"], "emit-symbol-graph">,
1163+
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild,
1164+
SupplementaryOutput, HelpHidden]>,
1165+
HelpText<"Emit a symbol graph">;
1166+
1167+
def emit_symbol_graph_dir : Separate<["-"], "emit-symbol-graph-dir">,
1168+
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild,
1169+
ArgumentIsPath, SupplementaryOutput, HelpHidden]>,
1170+
HelpText<"Emit a symbol graph to directory <dir>">,
1171+
MetaVarName<"<dir>">;
1172+
11621173
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
@@ -556,6 +556,11 @@ ToolChain::constructInvocation(const CompileJobAction &job,
556556
options::
557557
OPT_disable_autolinking_runtime_compatibility_dynamic_replacements);
558558

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

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

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

10441052
context.Args.AddLastArg(

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

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

237+
Opts.EmitSymbolGraph |= Args.hasArg(OPT_emit_symbol_graph);
238+
239+
if (const Arg *A = Args.getLastArg(OPT_emit_symbol_graph_dir)) {
240+
Opts.SymbolGraphOutputDir = A->getValue();
241+
}
242+
237243
return false;
238244
}
239245

@@ -581,6 +587,11 @@ bool ArgsToFrontendOptionsConverter::checkUnusedSupplementaryOutputPaths()
581587
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_module_summary);
582588
return true;
583589
}
590+
if (!FrontendOptions::canActionEmitModule(Opts.RequestedAction) &&
591+
!Opts.SymbolGraphOutputDir.empty()) {
592+
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_symbol_graph);
593+
return true;
594+
}
584595
return false;
585596
}
586597

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
@@ -682,6 +682,12 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
682682
Opts.AttachCommentsToDecls = true;
683683
}
684684

685+
// If we are emitting a symbol graph file, configure lexing and parsing to
686+
// remember comments.
687+
if (FrontendOpts.EmitSymbolGraph) {
688+
Opts.AttachCommentsToDecls = true;
689+
}
690+
685691
// If we're parsing SIL, access control doesn't make sense to enforce.
686692
if (Args.hasArg(OPT_parse_sil) ||
687693
FrontendOpts.InputsAndOutputs.shouldTreatAsSIL()) {

0 commit comments

Comments
 (0)