Skip to content

Commit 9a8b91e

Browse files
authored
Merge pull request #31606 from bitjammer/acgarland/rdar-62953144-use-identifier-for-type-names
[SymbolGraph] Use `identifier` for type name fragments
2 parents 590f83c + f2c10d2 commit 9a8b91e

File tree

5 files changed

+52
-58
lines changed

5 files changed

+52
-58
lines changed

lib/SymbolGraphGen/DeclarationFragmentPrinter.cpp

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -148,38 +148,36 @@ void DeclarationFragmentPrinter::printText(StringRef Text) {
148148
Spelling.append(Text);
149149
}
150150

151-
void DeclarationFragmentPrinter::printAbridgedType(const GenericTypeDecl *TD) {
152-
// Subheadings for types are abridged, omitting generics and inheritance.
153-
openFragment(DeclarationFragmentPrinter::FragmentKind::Keyword);
154-
switch (TD->getKind()) {
155-
case DeclKind::Struct:
156-
printText(getTokenText(tok::kw_struct));
157-
break;
158-
case DeclKind::Enum:
159-
printText(getTokenText(tok::kw_enum));
160-
break;
161-
case DeclKind::Protocol:
162-
printText(getTokenText(tok::kw_protocol));
163-
break;
164-
case DeclKind::Class:
165-
printText(getTokenText(tok::kw_class));
166-
break;
167-
case DeclKind::TypeAlias:
168-
printText(getTokenText(tok::kw_typealias));
169-
break;
170-
case DeclKind::OpaqueType:
171-
llvm_unreachable("OpaqueType should not be in symbol graphs!");
172-
default:
173-
llvm_unreachable("GenericTypeDecl kind not handled in DeclarationFragmentPrinter!");
151+
void DeclarationFragmentPrinter::printAbridgedType(const GenericTypeDecl *TD,
152+
bool PrintKeyword) {
153+
if (PrintKeyword) {
154+
openFragment(DeclarationFragmentPrinter::FragmentKind::Keyword);
155+
switch (TD->getKind()) {
156+
case DeclKind::Struct:
157+
printText(getTokenText(tok::kw_struct));
158+
break;
159+
case DeclKind::Enum:
160+
printText(getTokenText(tok::kw_enum));
161+
break;
162+
case DeclKind::Protocol:
163+
printText(getTokenText(tok::kw_protocol));
164+
break;
165+
case DeclKind::Class:
166+
printText(getTokenText(tok::kw_class));
167+
break;
168+
case DeclKind::TypeAlias:
169+
printText(getTokenText(tok::kw_typealias));
170+
break;
171+
case DeclKind::OpaqueType:
172+
llvm_unreachable("OpaqueType should not be in symbol graphs!");
173+
default:
174+
llvm_unreachable("GenericTypeDecl kind not handled in DeclarationFragmentPrinter!");
175+
}
176+
177+
openFragment(DeclarationFragmentPrinter::FragmentKind::Text);
178+
printText(" ");
174179
}
175180

176-
openFragment(DeclarationFragmentPrinter::FragmentKind::Text);
177-
printText(" ");
178-
179-
openFragment(DeclarationFragmentPrinter::FragmentKind::TypeIdentifier);
181+
openFragment(DeclarationFragmentPrinter::FragmentKind::Identifier);
180182
printText(TD->getNameStr());
181-
182-
USR.clear();
183-
llvm::raw_svector_ostream USROS(USR);
184-
ide::printDeclUSR(TD, USROS);
185183
}

lib/SymbolGraphGen/DeclarationFragmentPrinter.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ class DeclarationFragmentPrinter : public ASTPrinter {
9999
///
100100
/// Subheadings for types don't include the complete declaration line
101101
/// including generics and inheritance.
102-
void printAbridgedType(const GenericTypeDecl *TD);
102+
///
103+
/// \param TD The type declaration to print.
104+
/// \param PrintKeyword Print the corresponding keyword introducer if `true`.
105+
void printAbridgedType(const GenericTypeDecl *TD, bool PrintKeyword);
103106

104107
void printDeclLoc(const Decl *D) override;
105108

lib/SymbolGraphGen/SymbolGraph.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -522,15 +522,14 @@ SymbolGraph::serializeNavigatorDeclarationFragments(StringRef Key,
522522
DeclarationFragmentPrinter Printer(OS, Key);
523523

524524
if (const auto *TD = dyn_cast<GenericTypeDecl>(S.getSymbolDecl())) {
525-
Printer.printTypeRef(TD->getInterfaceType(), TD, TD->getName(),
526-
PrintNameContext::Normal);
527-
return;
528-
}
529-
auto Options = getSubHeadingDeclarationFragmentsPrintOptions();
530-
if (S.getSynthesizedBaseType()) {
531-
Options.setBaseType(S.getSynthesizedBaseType());
525+
Printer.printAbridgedType(TD, /*PrintKeyword=*/false);
526+
} else {
527+
auto Options = getSubHeadingDeclarationFragmentsPrintOptions();
528+
if (S.getSynthesizedBaseType()) {
529+
Options.setBaseType(S.getSynthesizedBaseType());
530+
}
531+
S.getSymbolDecl()->print(Printer, Options);
532532
}
533-
S.getSymbolDecl()->print(Printer, Options);
534533
}
535534

536535
void
@@ -540,7 +539,7 @@ SymbolGraph::serializeSubheadingDeclarationFragments(StringRef Key,
540539
DeclarationFragmentPrinter Printer(OS, Key);
541540

542541
if (const auto *TD = dyn_cast<GenericTypeDecl>(S.getSymbolDecl())) {
543-
Printer.printAbridgedType(TD);
542+
Printer.printAbridgedType(TD, /*PrintKeyword=*/true);
544543
} else {
545544
auto Options = getSubHeadingDeclarationFragmentsPrintOptions();
546545
if (S.getSynthesizedBaseType()) {

test/SymbolGraph/Symbols/Mixins/DeclarationFragments/Navigator.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ public struct MyStruct<S> { public var x: S
1717
// MYSTRUCT-NEXT: "title": "MyStruct",
1818
// MYSTRUCT-NEXT: "navigator": [
1919
// MYSTRUCT-NEXT: {
20-
// MYSTRUCT-NEXT: "kind": "typeIdentifier"
20+
// MYSTRUCT-NEXT: "kind": "identifier"
2121
// MYSTRUCT-NEXT: "spelling": "MyStruct"
22-
// MYSTRUCT-NEXT: "preciseIdentifier": "s:9Navigator8MyStructV"
2322
// MYSTRUCT-NEXT: }
2423
// MYSTRUCT-NEXT: ]
2524

test/SymbolGraph/Symbols/Mixins/DeclarationFragments/SubheadingDeclarationFragmentsTypes.swift

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
// STRUCT-NEXT "spelling": " "
1919
// STRUCT-NEXT }
2020
// STRUCT-NEXT {
21-
// STRUCT-NEXT "kind": "typeIdentifier",
22-
// STRUCT-NEXT "spelling": "Struct",
23-
// STRUCT-NEXT "preciseIdentifier": "s:35SubheadingDeclarationFragmentsTypes6StructV"
21+
// STRUCT-NEXT "kind": "identifier",
22+
// STRUCT-NEXT "spelling": "Struct"
2423
// STRUCT-NEXT }
2524
public struct Struct<T> where T: Sequence {}
2625

@@ -35,9 +34,8 @@ public struct Struct<T> where T: Sequence {}
3534
// ENUM-NEXT: "spelling": " "
3635
// ENUM-NEXT: }
3736
// ENUM-NEXT: {
38-
// ENUM-NEXT: "kind": "typeIdentifier",
39-
// ENUM-NEXT: "spelling": "Enum",
40-
// ENUM-NEXT: "preciseIdentifier": "s:35SubheadingDeclarationFragmentsTypes4EnumO"
37+
// ENUM-NEXT: "kind": "identifier",
38+
// ENUM-NEXT: "spelling": "Enum"
4139
// ENUM-NEXT: }
4240
public enum Enum<T> where T: Sequence {}
4341

@@ -52,9 +50,8 @@ public enum Enum<T> where T: Sequence {}
5250
// PROTOCOL-NEXT: "spelling": " "
5351
// PROTOCOL-NEXT: }
5452
// PROTOCOL-NEXT: {
55-
// PROTOCOL-NEXT: "kind": "typeIdentifier",
56-
// PROTOCOL-NEXT: "spelling": "Protocol",
57-
// PROTOCOL-NEXT: "preciseIdentifier": "s:35SubheadingDeclarationFragmentsTypes8ProtocolP"
53+
// PROTOCOL-NEXT: "kind": "identifier",
54+
// PROTOCOL-NEXT: "spelling": "Protocol"
5855
// PROTOCOL-NEXT: }
5956
public protocol Protocol where T: Sequence {
6057
associatedtype T
@@ -71,9 +68,8 @@ public protocol Protocol where T: Sequence {
7168
// CLASS-NEXT "spelling": " "
7269
// CLASS-NEXT },
7370
// CLASS-NEXT {
74-
// CLASS-NEXT "kind": "typeIdentifier",
75-
// CLASS-NEXT "spelling": "Class",
76-
// CLASS-NEXT "preciseIdentifier": "s:35SubheadingDeclarationFragmentsTypes5ClassC"
71+
// CLASS-NEXT "kind": "identifier",
72+
// CLASS-NEXT "spelling": "Class"
7773
// CLASS-NEXT }
7874
public class Class<T> where T: Sequence {}
7975

@@ -88,9 +84,8 @@ public class Class<T> where T: Sequence {}
8884
// TYPEALIAS-NEXT: "spelling": " "
8985
// TYPEALIAS-NEXT: },
9086
// TYPEALIAS-NEXT: {
91-
// TYPEALIAS-NEXT: "kind": "typeIdentifier",
92-
// TYPEALIAS-NEXT: "spelling": "TypeAlias",
93-
// TYPEALIAS-NEXT: "preciseIdentifier": "s:35SubheadingDeclarationFragmentsTypes9TypeAliasa"
87+
// TYPEALIAS-NEXT: "kind": "identifier",
88+
// TYPEALIAS-NEXT: "spelling": "TypeAlias"
9489
// TYPEALIAS-NEXT: }
9590
public typealias TypeAlias<T> = Struct<T> where T: Collection
9691

0 commit comments

Comments
 (0)