Skip to content

Commit 2d59365

Browse files
authored
Merge pull request #29414 from bitjammer/acgarland/rdar-58853310-interface-language
SymbolGraph: Serialize interface language
2 parents b872052 + 42345da commit 2d59365

File tree

5 files changed

+37
-77
lines changed

5 files changed

+37
-77
lines changed

lib/SymbolGraphGen/Edge.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ void Edge::serialize(llvm::json::OStream &OS) const {
2525

2626
// In case a dependent module isn't available, serialize a fallback name.
2727
auto TargetModuleName = Target->getModuleContext()->getName().str();
28+
2829
if (TargetModuleName != Walker->M.getName().str()) {
29-
auto TargetSymbolIdentifier = Walker->getSymbolIdentifier(Target);
30-
auto TargetComponents = TargetSymbolIdentifier.SimpleComponents;
30+
SmallVector<SmallString<32>, 8> TargetPathComponents;
31+
Walker->getPathComponents(Target, TargetPathComponents);
32+
3133
SmallString<128> Scratch(TargetModuleName);
32-
for (auto it = TargetComponents.begin();
33-
it != TargetComponents.end(); ++it) {
34+
for (auto it = TargetPathComponents.begin();
35+
it != TargetPathComponents.end(); ++it) {
3436
Scratch.push_back('.');
3537
Scratch.append(*it);
3638
}

lib/SymbolGraphGen/Symbol.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,30 @@ void Symbol::serializeKind(llvm::json::OStream &OS) const {
7676

7777
void Symbol::serializeIdentifier(SymbolGraphASTWalker &Walker,
7878
llvm::json::OStream &OS) const {
79-
AttributeRAII A("identifier", OS);
80-
Walker.getSymbolIdentifier(VD).serialize(OS);
79+
OS.attributeObject("identifier", [&](){
80+
OS.attribute("precise", Walker.getUSR(VD));
81+
OS.attribute("interfaceLanguage", "swift");
82+
});
83+
}
84+
85+
void Symbol::serializePathComponents(SymbolGraphASTWalker &Walker,
86+
llvm::json::OStream &OS) const {
87+
OS.attributeArray("pathComponents", [&](){
88+
SmallVector<SmallString<32>, 8> PathComponents;
89+
Walker.getPathComponents(VD, PathComponents);
90+
for (auto Component : PathComponents) {
91+
OS.value(Component);
92+
}
93+
});
8194
}
8295

8396
void Symbol::serializeNames(SymbolGraphASTWalker &Walker,
8497
llvm::json::OStream &OS) const {
8598
OS.attributeObject("names", [&](){
86-
auto Identifier = Walker.getSymbolIdentifier(VD);
87-
OS.attribute("title", Identifier.SimpleComponents.back());
99+
SmallVector<SmallString<32>, 8> PathComponents;
100+
Walker.getPathComponents(VD, PathComponents);
101+
102+
OS.attribute("title", PathComponents.back());
88103
// "navigator": null
89104
Walker.serializeSubheadingDeclarationFragments("subheading", VD, OS);
90105
// "prose": null
@@ -370,6 +385,7 @@ void Symbol::serialize(SymbolGraphASTWalker &Walker,
370385
OS.object([&](){
371386
serializeKind(OS);
372387
serializeIdentifier(Walker, OS);
388+
serializePathComponents(Walker, OS);
373389
serializeNames(Walker, OS);
374390
serializeDocComment(Walker, OS);
375391

lib/SymbolGraphGen/Symbol.h

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,6 @@ namespace symbolgraphgen {
2424
struct AvailabilityDomain;
2525
struct SymbolGraphASTWalker;
2626

27-
/**
28-
An identifier for a symbol that provides a globally unique identifier suitable for
29-
internal lookups and a locally unique path for human use, such as a URL.
30-
*/
31-
struct SymbolIdentifier {
32-
/**
33-
A string that uniquely identifies a symbol within a module in the event of
34-
ambiguities. A precise identifier need not be human readable.
35-
*/
36-
StringRef PreciseIdentifier;
37-
38-
/**
39-
The components for a "fully qualified" identifier.
40-
*/
41-
ArrayRef<StringRef> SimpleComponents;
42-
43-
SymbolIdentifier(llvm::StringRef PreciseIdentifier,
44-
ArrayRef<StringRef> SimpleComponents)
45-
: PreciseIdentifier(PreciseIdentifier),
46-
SimpleComponents(SimpleComponents) {
47-
assert(!PreciseIdentifier.empty());
48-
}
49-
50-
void serialize(llvm::json::OStream &OS) const {
51-
OS.object([&](){
52-
OS.attribute("precise", PreciseIdentifier);
53-
OS.attributeArray("simpleComponents", [&](){
54-
for (auto Component : SimpleComponents) {
55-
OS.value(Component);
56-
}
57-
});
58-
});
59-
}
60-
61-
bool operator==(const SymbolIdentifier &Other) const {
62-
return PreciseIdentifier == Other.PreciseIdentifier &&
63-
SimpleComponents == Other.SimpleComponents;
64-
}
65-
};
66-
6727
/// A symbol from a module: a node in a graph.
6828
struct Symbol {
6929
const ValueDecl *VD;
@@ -76,6 +36,9 @@ struct Symbol {
7636
void serializeIdentifier(SymbolGraphASTWalker &Walker,
7737
llvm::json::OStream &OS) const;
7838

39+
void serializePathComponents(SymbolGraphASTWalker &Walker,
40+
llvm::json::OStream &OS) const;
41+
7942
void serializeNames(SymbolGraphASTWalker &Walker,
8043
llvm::json::OStream &OS) const;
8144

lib/SymbolGraphGen/SymbolGraphASTWalker.cpp

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -149,24 +149,15 @@ StringRef SymbolGraphASTWalker::getUSR(const ValueDecl *VD) {
149149
return USR;
150150
}
151151

152-
SymbolIdentifier
153-
SymbolGraphASTWalker::getSymbolIdentifier(const ValueDecl *VD) {
154-
// Look in the symbol identifier cache for this declartion.
155-
auto Found = SymbolIdentifierCache.find(VD);
156-
if (Found != SymbolIdentifierCache.end()) {
157-
return Found->getSecond();
158-
}
159-
160-
// Not found; need to build a symbol identifier and add it to the cache.
161-
auto PreciseIdentifier = getUSR(VD);
162-
llvm::SmallVector<llvm::StringRef, 4> SimpleIdentifierChain;
163-
152+
void
153+
SymbolGraphASTWalker::getPathComponents(const ValueDecl *VD,
154+
SmallVectorImpl<SmallString<32>> &Components) {
164155
// Collect the spellings of the fully qualified identifier components.
165156
auto Decl = VD;
166157
while (Decl && !isa<ModuleDecl>(Decl)) {
167158
SmallString<32> Scratch;
168159
Decl->getFullName().getString(Scratch);
169-
SimpleIdentifierChain.push_back(Ctx.allocateCopy(Scratch.str()));
160+
Components.push_back(Scratch);
170161
if (const auto *DC = Decl->getDeclContext()) {
171162
if (const auto *Proto = DC->getExtendedProtocolDecl()) {
172163
Decl = Proto;
@@ -179,17 +170,9 @@ SymbolGraphASTWalker::getSymbolIdentifier(const ValueDecl *VD) {
179170
Decl = nullptr;
180171
}
181172
}
182-
183-
// The list is leaf-to-root, but our list is root-to-leaf, so reverse it.
184-
std::reverse(SimpleIdentifierChain.begin(), SimpleIdentifierChain.end());
185173

186-
SymbolIdentifier Identifier {
187-
PreciseIdentifier,
188-
Ctx.allocateCopy(llvm::makeArrayRef(SimpleIdentifierChain))
189-
};
190-
191-
SymbolIdentifierCache.insert({VD, Identifier});
192-
return Identifier;
174+
// The list is leaf-to-root, but our list is root-to-leaf, so reverse it.
175+
std::reverse(Components.begin(), Components.end());
193176
}
194177

195178
PrintOptions SymbolGraphASTWalker::getDeclarationFragmentsPrintOptions() const {

lib/SymbolGraphGen/SymbolGraphASTWalker.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class ValueDecl;
2727

2828
namespace symbolgraphgen {
2929

30-
struct SymbolIdentifier;
3130
struct SymbolGraph;
3231
struct SymbolGraphOptions;
3332

@@ -49,9 +48,6 @@ struct SymbolGraphASTWalker : public SourceEntityWalker {
4948
/// A context for allocations.
5049
markup::MarkupContext Ctx;
5150

52-
/// A cache of identifiers for declarations that may be seen more than once.
53-
llvm::DenseMap<const Decl *, SymbolIdentifier> SymbolIdentifierCache;
54-
5551
/// A cache of USRs for declarations.
5652
llvm::DenseMap<const ValueDecl *, StringRef> USRCache;
5753

@@ -72,8 +68,8 @@ struct SymbolGraphASTWalker : public SourceEntityWalker {
7268
/// Get the USR of a declaration and add it to the local allocator.
7369
StringRef getUSR(const ValueDecl *VD);
7470

75-
/// Returns a `SymbolIdentifier` for a given declaration.
76-
SymbolIdentifier getSymbolIdentifier(const ValueDecl *VD);
71+
/// Returns an array of path components for a declaration.
72+
void getPathComponents(const ValueDecl *VD, SmallVectorImpl<SmallString<32>> &Components);
7773

7874
// MARK: - Declaration Fragments
7975

0 commit comments

Comments
 (0)