Skip to content

Commit 453fd22

Browse files
authored
Allow for emission of swift.extension symbols for extensions to external types in swiftSymbolGraphGen (#59047)
This includes: - bumping the SWIFT_SYMBOLGRAPH_FORMAT_MINOR version - introduction of the "swift.extension" symbol and "extensionTo" relationship - adding support for ExtensionDecl to the Symbol class - adding a "typeKind" field to the symbol's extension mixin which indicates what kind of symbol was extended - intoduction of the -emit-extension-block-symbols flag, which enables the behavior outlined below - adaptions to SymbolGraphASTWalker that ensure a swift.extension symbol is emitted for each extension to a type that does not exist in the local symbol graph - adaptions to SymbolGraph and SymbolGraphASTWalker that ensure member and conformance relationships are correctly associated with the swift.extension symbol instead of the original type declaration's (extended nominal's) symbol where applicable - adaptions to SymbolGraphASTWalker that ensure swift.extension symbols are connected to their respective extended nominal's symbol using an extensionTo relationship Testing: - adds SymbolGraph tests that test behavior only relevant in -emit-extension-block-symbols mode - adapts some SymbolGraph tests to additionally test similar behavior for extensions to external types in -emit-extension-block-symbols mode - adapts some SymbolGraph tests to (additionally or exclusively) test the behavior with -emit-extension-block-symbols mode enabled Bugfixes: - fixes a bug where some conformsTo relationships implicated by the conformances declared on an extension to an external type were not emitted (see test/SymbolGraph/Relationships/ConformsTo/Indirect.swift) Further changes: - documents the strategy for naming and associating children declared in extensions to typealiases (see test/SymbolGraph/Relationships/MemberOf/Typealias.swift, test/SymbolGraph/Symbols/Names.swift)
1 parent 4b361d5 commit 453fd22

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+912
-264
lines changed

include/swift/Option/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,11 @@ def pretty_print: Flag<["-"], "pretty-print">,
13571357
Flags<[SwiftAPIExtractOption, SwiftSymbolGraphExtractOption]>,
13581358
HelpText<"Pretty-print the output JSON">;
13591359

1360+
def emit_extension_block_symbols: Flag<["-"], "emit-extension-block-symbols">,
1361+
Flags<[SwiftSymbolGraphExtractOption, FrontendOption,
1362+
NoInteractiveOption, SupplementaryOutput, HelpHidden]>,
1363+
HelpText<"Emit 'swift.extension' symbols for extensions to external types instead of directly associating members and conformances with the extended nominal when generating symbol graphs">;
1364+
13601365
// swift-symbolgraph-extract-only options
13611366
def output_dir : Separate<["-"], "output-dir">,
13621367
Flags<[NoDriverOption, SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption,

include/swift/SymbolGraphGen/SymbolGraphOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ struct SymbolGraphOptions {
4848

4949
/// Whether to include documentation for clang nodes or not.
5050
bool IncludeClangDocs = false;
51+
52+
/// Whether to emit "swift.extension" symbols for extensions to external types
53+
/// along with "extensionTo" relationships instead of directly associating
54+
/// members and conformances with the extended nominal.
55+
bool EmitExtensionBlockSymbols = false;
5156
};
5257

5358
} // end namespace symbolgraphgen

lib/Driver/ToolChains.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ ToolChain::constructInvocation(const CompileJobAction &job,
617617
context.Args.AddLastArg(Arguments, options::OPT_emit_symbol_graph_dir);
618618
}
619619
context.Args.AddLastArg(Arguments, options::OPT_include_spi_symbols);
620+
context.Args.AddLastArg(Arguments, options::OPT_emit_extension_block_symbols);
620621
context.Args.AddLastArg(Arguments, options::OPT_symbol_graph_minimum_access_level);
621622

622623
return II;
@@ -1113,6 +1114,7 @@ ToolChain::constructInvocation(const MergeModuleJobAction &job,
11131114
context.Args.AddLastArg(Arguments, options::OPT_emit_symbol_graph);
11141115
context.Args.AddLastArg(Arguments, options::OPT_emit_symbol_graph_dir);
11151116
context.Args.AddLastArg(Arguments, options::OPT_include_spi_symbols);
1117+
context.Args.AddLastArg(Arguments, options::OPT_emit_extension_block_symbols);
11161118
context.Args.AddLastArg(Arguments, options::OPT_symbol_graph_minimum_access_level);
11171119

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

lib/DriverTool/swift_symbolgraph_extract_main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ int swift_symbolgraph_extract_main(ArrayRef<const char *> Args,
172172
ParsedArgs.hasArg(OPT_skip_inherited_docs),
173173
ParsedArgs.hasArg(OPT_include_spi_symbols),
174174
/*IncludeClangDocs=*/false,
175+
ParsedArgs.hasArg(OPT_emit_extension_block_symbols),
175176
};
176177

177178
if (auto *A = ParsedArgs.getLastArg(OPT_minimum_access_level)) {

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,8 @@ static void ParseSymbolGraphArgs(symbolgraphgen::SymbolGraphOptions &Opts,
13001300

13011301
Opts.SkipInheritedDocs = Args.hasArg(OPT_skip_inherited_docs);
13021302
Opts.IncludeSPISymbols = Args.hasArg(OPT_include_spi_symbols);
1303+
Opts.EmitExtensionBlockSymbols =
1304+
Args.hasArg(OPT_emit_extension_block_symbols);
13031305

13041306
if (auto *A = Args.getLastArg(OPT_symbol_graph_minimum_access_level)) {
13051307
Opts.MinimumAccessLevel =

lib/SymbolGraphGen/Edge.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,20 @@ struct RelationshipKind {
104104
static inline RelationshipKind OptionalRequirementOf() {
105105
return RelationshipKind { "optionalRequirementOf" };
106106
}
107-
107+
108+
/**
109+
A symbol A extends a symbol B with members or conformances.
110+
111+
This relationship describes the connection between extension blocks
112+
(swift.extension symbols) and the type they extend.
113+
114+
The implied inverse of this relationship is a symbol B that is extended
115+
by an extension block symbol A.
116+
*/
117+
static inline RelationshipKind ExtensionTo() {
118+
return RelationshipKind{"extensionTo"};
119+
}
120+
108121
bool operator==(const RelationshipKind &Other) const {
109122
return Name == Other.Name;
110123
}

lib/SymbolGraphGen/FormatVersion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#define SWIFT_SYMBOLGRAPHGEN_FORMATVERSION_H
1515

1616
#define SWIFT_SYMBOLGRAPH_FORMAT_MAJOR 0
17-
#define SWIFT_SYMBOLGRAPH_FORMAT_MINOR 5
18-
#define SWIFT_SYMBOLGRAPH_FORMAT_PATCH 3
17+
#define SWIFT_SYMBOLGRAPH_FORMAT_MINOR 6
18+
#define SWIFT_SYMBOLGRAPH_FORMAT_PATCH 0
1919

2020
#endif // SWIFT_SYMBOLGRAPHGEN_FORMATVERSION_H

lib/SymbolGraphGen/JSON.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// Adds Symbol Graph JSON serialization to other types.
1313
//===----------------------------------------------------------------------===//
1414

15+
#include "JSON.h"
16+
#include "Symbol.h"
1517
#include "swift/AST/ASTContext.h"
1618
#include "swift/AST/Decl.h"
1719
#include "swift/AST/FileUnit.h"
@@ -21,7 +23,6 @@
2123
#include "swift/AST/USRGeneration.h"
2224
#include "swift/ClangImporter/ClangModule.h"
2325
#include "swift/Serialization/SerializedModuleLoader.h"
24-
#include "JSON.h"
2526

2627
void swift::symbolgraphgen::serialize(const llvm::VersionTuple &VT,
2728
llvm::json::OStream &OS) {
@@ -69,6 +70,8 @@ void swift::symbolgraphgen::serialize(const ExtensionDecl *Extension,
6970
if (const auto *ExtendedModule = ExtendedNominal->getModuleContext()) {
7071
OS.attribute("extendedModule", ExtendedModule->getNameStr());
7172
}
73+
74+
OS.attribute("typeKind", Symbol::getKind(ExtendedNominal).first);
7275
}
7376

7477
SmallVector<Requirement, 4> FilteredRequirements;

0 commit comments

Comments
 (0)