Skip to content

Commit 528ace8

Browse files
authored
Merge pull request #33374 from bitjammer/acgarland/rdar-64178490-dont-link-private-symbols
[SymbolGraph] Don't link type identifier fragments to private symbols
2 parents c4541c5 + ecf8d55 commit 528ace8

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

lib/SymbolGraphGen/DeclarationFragmentPrinter.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,15 @@ void DeclarationFragmentPrinter::printTypeRef(Type T, const TypeDecl *RefTo,
138138
openFragment(FragmentKind::TypeIdentifier);
139139
printText(Name.str());
140140
USR.clear();
141-
if (Name.str() != "Self") {
141+
142+
auto ShouldLink = Name.str() != "Self";
143+
if (const auto *TD = T->getAnyNominal()) {
144+
if (SG->isImplicitlyPrivate(TD)) {
145+
ShouldLink = false;
146+
}
147+
}
148+
149+
if (ShouldLink) {
142150
llvm::raw_svector_ostream OS(USR);
143151
ide::printDeclUSR(RefTo, OS);
144152
}

lib/SymbolGraphGen/DeclarationFragmentPrinter.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class DeclarationFragmentPrinter : public ASTPrinter {
5959
Text,
6060
};
6161
private:
62+
/// The symbol graph for which a declaration is being printed.
63+
const SymbolGraph *SG;
64+
6265
/// The output stream to print fragment objects to.
6366
llvm::json::OStream &OS;
6467

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

8386
public:
84-
DeclarationFragmentPrinter(llvm::json::OStream &OS,
87+
DeclarationFragmentPrinter(const SymbolGraph *SG,
88+
llvm::json::OStream &OS,
8589
Optional<StringRef> Key = None)
86-
: OS(OS),
90+
: SG(SG),
91+
OS(OS),
8792
Kind(FragmentKind::None),
8893
NumFragments(0) {
8994
if (Key) {

lib/SymbolGraphGen/SymbolGraph.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ void
558558
SymbolGraph::serializeDeclarationFragments(StringRef Key,
559559
const Symbol &S,
560560
llvm::json::OStream &OS) {
561-
DeclarationFragmentPrinter Printer(OS, Key);
561+
DeclarationFragmentPrinter Printer(this, OS, Key);
562562
auto Options = getDeclarationFragmentsPrintOptions();
563563
if (S.getSynthesizedBaseType()) {
564564
Options.setBaseType(S.getSynthesizedBaseType());
@@ -570,7 +570,7 @@ void
570570
SymbolGraph::serializeNavigatorDeclarationFragments(StringRef Key,
571571
const Symbol &S,
572572
llvm::json::OStream &OS) {
573-
DeclarationFragmentPrinter Printer(OS, Key);
573+
DeclarationFragmentPrinter Printer(this, OS, Key);
574574

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

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -module-name UnderscoreNotLinked -emit-module -emit-module-path %t/
3+
// RUN: %target-swift-symbolgraph-extract -module-name UnderscoreNotLinked -I %t -pretty-print -output-dir %t
4+
// RUN: %FileCheck %s --input-file %t/UnderscoreNotLinked.symbols.json
5+
6+
public protocol _ShouldntBeLinked {}
7+
public protocol ShouldBeLinked : _ShouldntBeLinked {}
8+
public struct MyStruct : ShouldBeLinked {}
9+
10+
// CHECK: "spelling": "_ShouldntBeLinked"
11+
// CHECK-NOT: "preciseIdentifier": "s:19UnderscoreNotLinked011_ShouldntBeC0P"

0 commit comments

Comments
 (0)