Skip to content

[SymbolGraph] Don't link type identifier fragments to private symbols #33374

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
Aug 11, 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
10 changes: 9 additions & 1 deletion lib/SymbolGraphGen/DeclarationFragmentPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,15 @@ void DeclarationFragmentPrinter::printTypeRef(Type T, const TypeDecl *RefTo,
openFragment(FragmentKind::TypeIdentifier);
printText(Name.str());
USR.clear();
if (Name.str() != "Self") {

auto ShouldLink = Name.str() != "Self";
if (const auto *TD = T->getAnyNominal()) {
if (SG->isImplicitlyPrivate(TD)) {
ShouldLink = false;
}
}

if (ShouldLink) {
llvm::raw_svector_ostream OS(USR);
ide::printDeclUSR(RefTo, OS);
}
Expand Down
9 changes: 7 additions & 2 deletions lib/SymbolGraphGen/DeclarationFragmentPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class DeclarationFragmentPrinter : public ASTPrinter {
Text,
};
private:
/// The symbol graph for which a declaration is being printed.
const SymbolGraph *SG;

/// The output stream to print fragment objects to.
llvm::json::OStream &OS;

Expand All @@ -81,9 +84,11 @@ class DeclarationFragmentPrinter : public ASTPrinter {
unsigned NumFragments;

public:
DeclarationFragmentPrinter(llvm::json::OStream &OS,
DeclarationFragmentPrinter(const SymbolGraph *SG,
llvm::json::OStream &OS,
Optional<StringRef> Key = None)
: OS(OS),
: SG(SG),
OS(OS),
Kind(FragmentKind::None),
NumFragments(0) {
if (Key) {
Expand Down
8 changes: 4 additions & 4 deletions lib/SymbolGraphGen/SymbolGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ void
SymbolGraph::serializeDeclarationFragments(StringRef Key,
const Symbol &S,
llvm::json::OStream &OS) {
DeclarationFragmentPrinter Printer(OS, Key);
DeclarationFragmentPrinter Printer(this, OS, Key);
auto Options = getDeclarationFragmentsPrintOptions();
if (S.getSynthesizedBaseType()) {
Options.setBaseType(S.getSynthesizedBaseType());
Expand All @@ -570,7 +570,7 @@ void
SymbolGraph::serializeNavigatorDeclarationFragments(StringRef Key,
const Symbol &S,
llvm::json::OStream &OS) {
DeclarationFragmentPrinter Printer(OS, Key);
DeclarationFragmentPrinter Printer(this, OS, Key);

if (const auto *TD = dyn_cast<GenericTypeDecl>(S.getSymbolDecl())) {
Printer.printAbridgedType(TD, /*PrintKeyword=*/false);
Expand All @@ -587,7 +587,7 @@ void
SymbolGraph::serializeSubheadingDeclarationFragments(StringRef Key,
const Symbol &S,
llvm::json::OStream &OS) {
DeclarationFragmentPrinter Printer(OS, Key);
DeclarationFragmentPrinter Printer(this, OS, Key);

if (const auto *TD = dyn_cast<GenericTypeDecl>(S.getSymbolDecl())) {
Printer.printAbridgedType(TD, /*PrintKeyword=*/true);
Expand All @@ -603,7 +603,7 @@ SymbolGraph::serializeSubheadingDeclarationFragments(StringRef Key,
void
SymbolGraph::serializeDeclarationFragments(StringRef Key, Type T,
llvm::json::OStream &OS) {
DeclarationFragmentPrinter Printer(OS, Key);
DeclarationFragmentPrinter Printer(this, OS, Key);
T->print(Printer, getDeclarationFragmentsPrintOptions());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -module-name UnderscoreNotLinked -emit-module -emit-module-path %t/
// RUN: %target-swift-symbolgraph-extract -module-name UnderscoreNotLinked -I %t -pretty-print -output-dir %t
// RUN: %FileCheck %s --input-file %t/UnderscoreNotLinked.symbols.json

public protocol _ShouldntBeLinked {}
public protocol ShouldBeLinked : _ShouldntBeLinked {}
public struct MyStruct : ShouldBeLinked {}

// CHECK: "spelling": "_ShouldntBeLinked"
// CHECK-NOT: "preciseIdentifier": "s:19UnderscoreNotLinked011_ShouldntBeC0P"