Skip to content

[SymbolGraph] Use identifier for type name fragments #31606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 29 additions & 31 deletions lib/SymbolGraphGen/DeclarationFragmentPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,38 +148,36 @@ void DeclarationFragmentPrinter::printText(StringRef Text) {
Spelling.append(Text);
}

void DeclarationFragmentPrinter::printAbridgedType(const GenericTypeDecl *TD) {
// Subheadings for types are abridged, omitting generics and inheritance.
openFragment(DeclarationFragmentPrinter::FragmentKind::Keyword);
switch (TD->getKind()) {
case DeclKind::Struct:
printText(getTokenText(tok::kw_struct));
break;
case DeclKind::Enum:
printText(getTokenText(tok::kw_enum));
break;
case DeclKind::Protocol:
printText(getTokenText(tok::kw_protocol));
break;
case DeclKind::Class:
printText(getTokenText(tok::kw_class));
break;
case DeclKind::TypeAlias:
printText(getTokenText(tok::kw_typealias));
break;
case DeclKind::OpaqueType:
llvm_unreachable("OpaqueType should not be in symbol graphs!");
default:
llvm_unreachable("GenericTypeDecl kind not handled in DeclarationFragmentPrinter!");
void DeclarationFragmentPrinter::printAbridgedType(const GenericTypeDecl *TD,
bool PrintKeyword) {
if (PrintKeyword) {
openFragment(DeclarationFragmentPrinter::FragmentKind::Keyword);
switch (TD->getKind()) {
case DeclKind::Struct:
printText(getTokenText(tok::kw_struct));
break;
case DeclKind::Enum:
printText(getTokenText(tok::kw_enum));
break;
case DeclKind::Protocol:
printText(getTokenText(tok::kw_protocol));
break;
case DeclKind::Class:
printText(getTokenText(tok::kw_class));
break;
case DeclKind::TypeAlias:
printText(getTokenText(tok::kw_typealias));
break;
case DeclKind::OpaqueType:
llvm_unreachable("OpaqueType should not be in symbol graphs!");
default:
llvm_unreachable("GenericTypeDecl kind not handled in DeclarationFragmentPrinter!");
}

openFragment(DeclarationFragmentPrinter::FragmentKind::Text);
printText(" ");
}

openFragment(DeclarationFragmentPrinter::FragmentKind::Text);
printText(" ");

openFragment(DeclarationFragmentPrinter::FragmentKind::TypeIdentifier);
openFragment(DeclarationFragmentPrinter::FragmentKind::Identifier);
printText(TD->getNameStr());

USR.clear();
llvm::raw_svector_ostream USROS(USR);
ide::printDeclUSR(TD, USROS);
}
5 changes: 4 additions & 1 deletion lib/SymbolGraphGen/DeclarationFragmentPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ class DeclarationFragmentPrinter : public ASTPrinter {
///
/// Subheadings for types don't include the complete declaration line
/// including generics and inheritance.
void printAbridgedType(const GenericTypeDecl *TD);
///
/// \param TD The type declaration to print.
/// \param PrintKeyword Print the corresponding keyword introducer if `true`.
void printAbridgedType(const GenericTypeDecl *TD, bool PrintKeyword);

void printDeclLoc(const Decl *D) override;

Expand Down
17 changes: 8 additions & 9 deletions lib/SymbolGraphGen/SymbolGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,14 @@ SymbolGraph::serializeNavigatorDeclarationFragments(StringRef Key,
DeclarationFragmentPrinter Printer(OS, Key);

if (const auto *TD = dyn_cast<GenericTypeDecl>(S.getSymbolDecl())) {
Printer.printTypeRef(TD->getInterfaceType(), TD, TD->getName(),
PrintNameContext::Normal);
return;
}
auto Options = getSubHeadingDeclarationFragmentsPrintOptions();
if (S.getSynthesizedBaseType()) {
Options.setBaseType(S.getSynthesizedBaseType());
Printer.printAbridgedType(TD, /*PrintKeyword=*/false);
} else {
auto Options = getSubHeadingDeclarationFragmentsPrintOptions();
if (S.getSynthesizedBaseType()) {
Options.setBaseType(S.getSynthesizedBaseType());
}
S.getSymbolDecl()->print(Printer, Options);
}
S.getSymbolDecl()->print(Printer, Options);
}

void
Expand All @@ -540,7 +539,7 @@ SymbolGraph::serializeSubheadingDeclarationFragments(StringRef Key,
DeclarationFragmentPrinter Printer(OS, Key);

if (const auto *TD = dyn_cast<GenericTypeDecl>(S.getSymbolDecl())) {
Printer.printAbridgedType(TD);
Printer.printAbridgedType(TD, /*PrintKeyword=*/true);
} else {
auto Options = getSubHeadingDeclarationFragmentsPrintOptions();
if (S.getSynthesizedBaseType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ public struct MyStruct<S> { public var x: S
// MYSTRUCT-NEXT: "title": "MyStruct",
// MYSTRUCT-NEXT: "navigator": [
// MYSTRUCT-NEXT: {
// MYSTRUCT-NEXT: "kind": "typeIdentifier"
// MYSTRUCT-NEXT: "kind": "identifier"
// MYSTRUCT-NEXT: "spelling": "MyStruct"
// MYSTRUCT-NEXT: "preciseIdentifier": "s:9Navigator8MyStructV"
// MYSTRUCT-NEXT: }
// MYSTRUCT-NEXT: ]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
// STRUCT-NEXT "spelling": " "
// STRUCT-NEXT }
// STRUCT-NEXT {
// STRUCT-NEXT "kind": "typeIdentifier",
// STRUCT-NEXT "spelling": "Struct",
// STRUCT-NEXT "preciseIdentifier": "s:35SubheadingDeclarationFragmentsTypes6StructV"
// STRUCT-NEXT "kind": "identifier",
// STRUCT-NEXT "spelling": "Struct"
// STRUCT-NEXT }
public struct Struct<T> where T: Sequence {}

Expand All @@ -35,9 +34,8 @@ public struct Struct<T> where T: Sequence {}
// ENUM-NEXT: "spelling": " "
// ENUM-NEXT: }
// ENUM-NEXT: {
// ENUM-NEXT: "kind": "typeIdentifier",
// ENUM-NEXT: "spelling": "Enum",
// ENUM-NEXT: "preciseIdentifier": "s:35SubheadingDeclarationFragmentsTypes4EnumO"
// ENUM-NEXT: "kind": "identifier",
// ENUM-NEXT: "spelling": "Enum"
// ENUM-NEXT: }
public enum Enum<T> where T: Sequence {}

Expand All @@ -52,9 +50,8 @@ public enum Enum<T> where T: Sequence {}
// PROTOCOL-NEXT: "spelling": " "
// PROTOCOL-NEXT: }
// PROTOCOL-NEXT: {
// PROTOCOL-NEXT: "kind": "typeIdentifier",
// PROTOCOL-NEXT: "spelling": "Protocol",
// PROTOCOL-NEXT: "preciseIdentifier": "s:35SubheadingDeclarationFragmentsTypes8ProtocolP"
// PROTOCOL-NEXT: "kind": "identifier",
// PROTOCOL-NEXT: "spelling": "Protocol"
// PROTOCOL-NEXT: }
public protocol Protocol where T: Sequence {
associatedtype T
Expand All @@ -71,9 +68,8 @@ public protocol Protocol where T: Sequence {
// CLASS-NEXT "spelling": " "
// CLASS-NEXT },
// CLASS-NEXT {
// CLASS-NEXT "kind": "typeIdentifier",
// CLASS-NEXT "spelling": "Class",
// CLASS-NEXT "preciseIdentifier": "s:35SubheadingDeclarationFragmentsTypes5ClassC"
// CLASS-NEXT "kind": "identifier",
// CLASS-NEXT "spelling": "Class"
// CLASS-NEXT }
public class Class<T> where T: Sequence {}

Expand All @@ -88,9 +84,8 @@ public class Class<T> where T: Sequence {}
// TYPEALIAS-NEXT: "spelling": " "
// TYPEALIAS-NEXT: },
// TYPEALIAS-NEXT: {
// TYPEALIAS-NEXT: "kind": "typeIdentifier",
// TYPEALIAS-NEXT: "spelling": "TypeAlias",
// TYPEALIAS-NEXT: "preciseIdentifier": "s:35SubheadingDeclarationFragmentsTypes9TypeAliasa"
// TYPEALIAS-NEXT: "kind": "identifier",
// TYPEALIAS-NEXT: "spelling": "TypeAlias"
// TYPEALIAS-NEXT: }
public typealias TypeAlias<T> = Struct<T> where T: Collection