Skip to content

Commit 53f494c

Browse files
authored
Merge pull request swiftlang#29708 from bitjammer/acgarland/rdar-58941718-separate-sgf-extended-modules
Separate symbol graph files for extended modules
2 parents 565b0e1 + 58bbe1e commit 53f494c

40 files changed

+633
-430
lines changed

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ class SerializedASTFile final : public LoadedFile {
403403
SmallVectorImpl<GenericSignature> &genericSignatures)
404404
override;
405405

406+
StringRef getTargetTriple() const;
407+
406408
static bool classof(const FileUnit *file) {
407409
return file->getKind() == FileUnitKind::SerializedAST;
408410
}

include/swift/SymbolGraphGen/SymbolGraphGen.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class ModuleDecl;
2020
namespace symbolgraphgen {
2121

2222
struct SymbolGraphOptions {
23-
/// The path to output the symbol graph JSON.
24-
StringRef OutputPath;
23+
/// The directory to output the symbol graph JSON files.
24+
StringRef OutputDir;
2525

2626
/// The target of the module.
2727
llvm::Triple Target;

lib/Serialization/ModuleFile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,10 @@ class ModuleFile
823823
return ModuleInputBuffer->getBufferIdentifier();
824824
}
825825

826+
StringRef getTargetTriple() const {
827+
return TargetTriple;
828+
}
829+
826830
/// AST-verify imported decls.
827831
///
828832
/// Has no effect in NDEBUG builds.

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,10 @@ StringRef SerializedASTFile::getFilename() const {
11811181
return File.getModuleFilename();
11821182
}
11831183

1184+
StringRef SerializedASTFile::getTargetTriple() const {
1185+
return File.getTargetTriple();
1186+
}
1187+
11841188
const clang::Module *SerializedASTFile::getUnderlyingClangModule() const {
11851189
if (auto *UnderlyingModule = File.getUnderlyingModule())
11861190
return UnderlyingModule->findUnderlyingClangModule();

lib/SymbolGraphGen/DeclarationFragmentPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void DeclarationFragmentPrinter::printTypeRef(Type T, const TypeDecl *RefTo,
128128
PrintNameContext NameContext) {
129129
openFragment(FragmentKind::TypeIdentifier);
130130
printText(Name.str());
131-
USR = Walker.getUSR(RefTo);
131+
USR = Graph.getUSR(RefTo);
132132
closeFragment();
133133
}
134134

lib/SymbolGraphGen/DeclarationFragmentPrinter.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TypeDecl;
2525

2626
namespace symbolgraphgen {
2727

28-
struct SymbolGraphASTWalker;
28+
struct SymbolGraph;
2929

3030
/// Prints AST nodes as a stream of tagged fragments for syntax highlighting.
3131
///
@@ -55,7 +55,7 @@ class DeclarationFragmentPrinter : public ASTPrinter {
5555
Text,
5656
};
5757

58-
SymbolGraphASTWalker &Walker;
58+
SymbolGraph &Graph;
5959

6060
/// The output stream to print fragment objects to.
6161
llvm::json::OStream &OS;
@@ -77,10 +77,10 @@ class DeclarationFragmentPrinter : public ASTPrinter {
7777
void closeFragment();
7878

7979
public:
80-
DeclarationFragmentPrinter(SymbolGraphASTWalker &Walker,
80+
DeclarationFragmentPrinter(SymbolGraph &Graph,
8181
llvm::json::OStream &OS,
8282
Optional<StringRef> Key = None)
83-
: Walker(Walker),
83+
: Graph(Graph),
8484
OS(OS),
8585
Kind(FragmentKind::None) {
8686
if (Key) {

lib/SymbolGraphGen/Edge.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ using namespace symbolgraphgen;
2020
void Edge::serialize(llvm::json::OStream &OS) const {
2121
OS.object([&](){
2222
OS.attribute("kind", Kind.Name);
23-
OS.attribute("source", Walker->getUSR(Source));
24-
OS.attribute("target", Walker->getUSR(Target));
23+
OS.attribute("source", Graph->getUSR(Source));
24+
OS.attribute("target", Graph->getUSR(Target));
2525

2626
// In case a dependent module isn't available, serialize a fallback name.
2727
auto TargetModuleName = Target->getModuleContext()->getName().str();
2828

29-
if (TargetModuleName != Walker->M.getName().str()) {
29+
if (TargetModuleName != Graph->M.getName().str()) {
3030
SmallVector<SmallString<32>, 8> TargetPathComponents;
31-
Walker->getPathComponents(Target, TargetPathComponents);
31+
Graph->getPathComponents(Target, TargetPathComponents);
3232

3333
SmallString<128> Scratch(TargetModuleName);
3434
for (auto it = TargetPathComponents.begin();

lib/SymbolGraphGen/Edge.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
#include "swift/Basic/LLVM.h"
1919

2020
#include "JSON.h"
21-
#include "Symbol.h"
2221

2322
namespace swift {
2423
namespace symbolgraphgen {
24+
25+
struct SymbolGraph;
2526

2627
/// The kind of relationship, tagging an edge in the graph.
2728
struct RelationshipKind {
@@ -113,7 +114,7 @@ struct RelationshipKind {
113114

114115
/// A relationship between two symbols: an edge in a directed graph.
115116
struct Edge {
116-
SymbolGraphASTWalker *Walker;
117+
SymbolGraph *Graph;
117118

118119
/// The kind of relationship this edge represents.
119120
RelationshipKind Kind;

lib/SymbolGraphGen/FormatVersion.h

Lines changed: 1 addition & 1 deletion
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 2
17+
#define SWIFT_SYMBOLGRAPH_FORMAT_MINOR 3
1818
#define SWIFT_SYMBOLGRAPH_FORMAT_PATCH 0
1919

2020
#endif // SWIFT_SYMBOLGRAPHGEN_FORMATVERSION_H

lib/SymbolGraphGen/Symbol.cpp

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -74,34 +74,31 @@ void Symbol::serializeKind(llvm::json::OStream &OS) const {
7474
}
7575
}
7676

77-
void Symbol::serializeIdentifier(SymbolGraphASTWalker &Walker,
78-
llvm::json::OStream &OS) const {
77+
void Symbol::serializeIdentifier(llvm::json::OStream &OS) const {
7978
OS.attributeObject("identifier", [&](){
80-
OS.attribute("precise", Walker.getUSR(VD));
79+
OS.attribute("precise", Graph.getUSR(VD));
8180
OS.attribute("interfaceLanguage", "swift");
8281
});
8382
}
8483

85-
void Symbol::serializePathComponents(SymbolGraphASTWalker &Walker,
86-
llvm::json::OStream &OS) const {
84+
void Symbol::serializePathComponents(llvm::json::OStream &OS) const {
8785
OS.attributeArray("pathComponents", [&](){
8886
SmallVector<SmallString<32>, 8> PathComponents;
89-
Walker.getPathComponents(VD, PathComponents);
87+
Graph.getPathComponents(VD, PathComponents);
9088
for (auto Component : PathComponents) {
9189
OS.value(Component);
9290
}
9391
});
9492
}
9593

96-
void Symbol::serializeNames(SymbolGraphASTWalker &Walker,
97-
llvm::json::OStream &OS) const {
94+
void Symbol::serializeNames(llvm::json::OStream &OS) const {
9895
OS.attributeObject("names", [&](){
9996
SmallVector<SmallString<32>, 8> PathComponents;
100-
Walker.getPathComponents(VD, PathComponents);
97+
Graph.getPathComponents(VD, PathComponents);
10198

10299
OS.attribute("title", PathComponents.back());
103100
// "navigator": null
104-
Walker.serializeSubheadingDeclarationFragments("subheading", VD, OS);
101+
Graph.serializeSubheadingDeclarationFragments("subheading", VD, OS);
105102
// "prose": null
106103
});
107104
}
@@ -131,10 +128,9 @@ void Symbol::serializeRange(size_t InitialIndentation,
131128
});
132129
}
133130

134-
void Symbol::serializeDocComment(SymbolGraphASTWalker &Walker,
135-
llvm::json::OStream &OS) const {
131+
void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
136132
OS.attributeObject("docComment", [&](){
137-
auto LL = Walker.Ctx.getLineList(VD->getRawComment());
133+
auto LL = Graph.Ctx.getLineList(VD->getRawComment());
138134
size_t InitialIndentation = LL.getLines().empty()
139135
? 0
140136
: markup::measureIndentation(LL.getLines().front().Text);
@@ -146,7 +142,7 @@ void Symbol::serializeDocComment(SymbolGraphASTWalker &Walker,
146142
// text and start of its source range, if it has one.
147143
if (Line.Range.isValid()) {
148144
serializeRange(InitialIndentation,
149-
Line.Range, Walker.M.getASTContext().SourceMgr, OS);
145+
Line.Range, Graph.M.getASTContext().SourceMgr, OS);
150146
}
151147
auto TrimmedLine = Line.Text.drop_front(std::min(InitialIndentation,
152148
Line.FirstNonspaceOffset));
@@ -157,8 +153,7 @@ void Symbol::serializeDocComment(SymbolGraphASTWalker &Walker,
157153
}); // end docComment:
158154
}
159155

160-
void Symbol::serializeFunctionSignature(SymbolGraphASTWalker &Walker,
161-
llvm::json::OStream &OS) const {
156+
void Symbol::serializeFunctionSignature(llvm::json::OStream &OS) const {
162157
if (const auto *FD = dyn_cast_or_null<FuncDecl>(VD)) {
163158
OS.attributeObject("functionSignature", [&](){
164159

@@ -180,7 +175,7 @@ void Symbol::serializeFunctionSignature(SymbolGraphASTWalker &Walker,
180175
OS.attribute("internalName", InternalName);
181176
}
182177
}
183-
Walker.serializeDeclarationFragments("declarationFragments",
178+
Graph.serializeDeclarationFragments("declarationFragments",
184179
Param, OS);
185180
}); // end parameter object
186181
}
@@ -190,7 +185,7 @@ void Symbol::serializeFunctionSignature(SymbolGraphASTWalker &Walker,
190185

191186
// Returns
192187
if (const auto ReturnType = FD->getResultInterfaceType()) {
193-
Walker.serializeDeclarationFragments("returns", ReturnType, OS);
188+
Graph.serializeDeclarationFragments("returns", ReturnType, OS);
194189
}
195190
});
196191
}
@@ -257,12 +252,15 @@ void Symbol::serializeSwiftGenericMixin(llvm::json::OStream &OS) const {
257252
}
258253
}
259254

260-
void Symbol::serializeSwiftExtensionMixin(SymbolGraphASTWalker &Walker,
261-
llvm::json::OStream &OS) const {
255+
void Symbol::serializeSwiftExtensionMixin(llvm::json::OStream &OS) const {
262256
if (const auto *Extension
263257
= dyn_cast_or_null<ExtensionDecl>(VD->getInnermostDeclContext())) {
264258
OS.attributeObject("swiftExtension", [&](){
265-
OS.attribute("definedInModule", Walker.M.getNameStr());
259+
if (const auto *ExtendedNominal = Extension->getExtendedNominal()) {
260+
if (const auto *ExtendedModule = ExtendedNominal->getModuleContext()) {
261+
OS.attribute("extendedModule", ExtendedModule->getNameStr());
262+
}
263+
}
266264
auto Generics = Extension->getGenericSignature();
267265
if (Generics && !Generics->getRequirements().empty()) {
268266
OS.attributeArray("constraints", [&](){
@@ -275,9 +273,8 @@ void Symbol::serializeSwiftExtensionMixin(SymbolGraphASTWalker &Walker,
275273
}
276274
}
277275

278-
void Symbol::serializeDeclarationFragmentMixin(SymbolGraphASTWalker &Walker,
279-
llvm::json::OStream &OS) const {
280-
Walker.serializeDeclarationFragments("declarationFragments", VD, OS);
276+
void Symbol::serializeDeclarationFragmentMixin(llvm::json::OStream &OS) const {
277+
Graph.serializeDeclarationFragments("declarationFragments", VD, OS);
281278
}
282279

283280
void Symbol::serializeAccessLevelMixin(llvm::json::OStream &OS) const {
@@ -380,20 +377,19 @@ void Symbol::serializeAvailabilityMixin(llvm::json::OStream &OS) const {
380377
}); // end availability: []
381378
}
382379

383-
void Symbol::serialize(SymbolGraphASTWalker &Walker,
384-
llvm::json::OStream &OS) const {
380+
void Symbol::serialize(llvm::json::OStream &OS) const {
385381
OS.object([&](){
386382
serializeKind(OS);
387-
serializeIdentifier(Walker, OS);
388-
serializePathComponents(Walker, OS);
389-
serializeNames(Walker, OS);
390-
serializeDocComment(Walker, OS);
383+
serializeIdentifier(OS);
384+
serializePathComponents(OS);
385+
serializeNames(OS);
386+
serializeDocComment(OS);
391387

392388
// "Mixins"
393-
serializeFunctionSignature(Walker, OS);
389+
serializeFunctionSignature(OS);
394390
serializeSwiftGenericMixin(OS);
395-
serializeSwiftExtensionMixin(Walker, OS);
396-
serializeDeclarationFragmentMixin(Walker, OS);
391+
serializeSwiftExtensionMixin(OS);
392+
serializeDeclarationFragmentMixin(OS);
397393
serializeAccessLevelMixin(OS);
398394
serializeAvailabilityMixin(OS);
399395
});

0 commit comments

Comments
 (0)