Skip to content

Commit 977f134

Browse files
non-synthesized symbols can inherit docs too
1 parent f4154d6 commit 977f134

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

lib/SymbolGraphGen/Edge.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,16 @@ void Edge::serialize(llvm::json::OStream &OS) const {
5858
}
5959
}
6060

61+
const ValueDecl *InheritingDecl = nullptr;
62+
if (const auto *ID = Source.getDeclInheritingDocs()) {
63+
if (Target.getSymbolDecl() == ID || Source.getSynthesizedBaseTypeDecl())
64+
InheritingDecl = ID;
65+
}
66+
6167
// If our source symbol is a synthesized decl, write in information about
6268
// where it's inheriting docs from.
63-
if (Source.getSynthesizedBaseTypeDecl()) {
64-
Symbol inheritedSym(Graph, Source.getSymbolDecl(), nullptr);
69+
if (InheritingDecl) {
70+
Symbol inheritedSym(Graph, InheritingDecl, nullptr);
6571
SmallString<256> USR, Display;
6672
llvm::raw_svector_ostream DisplayOS(Display);
6773

lib/SymbolGraphGen/Symbol.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,32 @@ void Symbol::serializeRange(size_t InitialIndentation,
171171
});
172172
}
173173

174-
void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
175-
if (Graph->Walker.Options.SkipInheritedDocs && SynthesizedBaseTypeDecl) {
176-
return;
177-
}
174+
const ValueDecl *Symbol::getDeclInheritingDocs() const {
175+
// get the decl that would provide docs for this symbol
178176
const auto *DocCommentProvidingDecl =
179-
dyn_cast_or_null<ValueDecl>(
180-
getDocCommentProvidingDecl(VD, /*AllowSerialized=*/true));
181-
if (!DocCommentProvidingDecl) {
182-
DocCommentProvidingDecl = VD;
177+
dyn_cast_or_null<ValueDecl>(
178+
getDocCommentProvidingDecl(VD, /*AllowSerialized=*/true));
179+
180+
// if the decl is the same as the one for this symbol, we're not
181+
// inheriting docs, so return null.
182+
if (DocCommentProvidingDecl == VD) {
183+
return nullptr;
184+
} else {
185+
// otherwise, return whatever `getDocCommentProvidingDecl` returned.
186+
// it will be null if there are no decls that provide docs for this
187+
// symbol.
188+
return DocCommentProvidingDecl;
189+
}
190+
}
191+
192+
void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
193+
const auto *DocCommentProvidingDecl = VD;
194+
if (!Graph->Walker.Options.SkipInheritedDocs) {
195+
DocCommentProvidingDecl = dyn_cast_or_null<ValueDecl>(
196+
getDocCommentProvidingDecl(VD, /*AllowSerialized=*/true));
197+
if (!DocCommentProvidingDecl) {
198+
DocCommentProvidingDecl = VD;
199+
}
183200
}
184201
auto RC = DocCommentProvidingDecl->getRawComment(/*SerializedOK=*/true);
185202
if (RC.isEmpty()) {

lib/SymbolGraphGen/Symbol.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ class Symbol {
111111
void printPath(llvm::raw_ostream &OS) const;
112112

113113
void getUSR(SmallVectorImpl<char> &USR) const;
114+
115+
/// If this symbol is inheriting docs from a parent class, protocol, or default
116+
/// implementation, returns that decl. Returns null if there are no docs or if
117+
/// the symbol has its own doc comments to render.
118+
const ValueDecl *getDeclInheritingDocs() const;
114119

115120
static bool supportsKind(DeclKind Kind);
116121
};

test/SymbolGraph/Relationships/Synthesized/InheritedDocs.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,30 @@
33

44
// RUN: %target-swift-symbolgraph-extract -module-name InheritedDocs -I %t -pretty-print -output-dir %t
55
// RUN: %FileCheck %s --input-file %t/InheritedDocs.symbols.json --check-prefixes CHECK,DOCS
6+
// RUN: %FileCheck %s --input-file %t/InheritedDocs.symbols.json --check-prefixes IMPL
67

78
// RUN: %target-swift-symbolgraph-extract -module-name InheritedDocs -I %t -pretty-print -output-dir %t -skip-inherited-docs
89
// RUN: %FileCheck %s --input-file %t/InheritedDocs.symbols.json --check-prefixes CHECK,SKIP
10+
// RUN: %FileCheck %s --input-file %t/InheritedDocs.symbols.json --check-prefixes IMPL
911

1012
// RUN: %empty-directory(%t)
1113
// RUN: %target-build-swift %s -module-name InheritedDocs -emit-module -emit-module-path %t/InheritedDocs.swiftmodule -emit-symbol-graph -emit-symbol-graph-dir %t/ -skip-inherited-docs
1214
// RUN: %FileCheck %s --input-file %t/InheritedDocs.symbols.json --check-prefixes SKIP
1315

1416
// DOCS-COUNT-3: Some Function
15-
// SKIP-COUNT-2: Some Function
17+
// SKIP-COUNT-1: Some Function
1618

1719
// CHECK: "source": "s:13InheritedDocs1PPAAE8someFuncyyF::SYNTHESIZED::s:13InheritedDocs1SV"
1820
// CHECK-NEXT: "target": "s:13InheritedDocs1SV"
19-
// CHECK-NEXT: "sourceOrigin": {
20-
// CHECK-NEXT: "identifier": "s:13InheritedDocs1PPAAE8someFuncyyF"
21+
// CHECK-NEXT: "sourceOrigin"
22+
// CHECK-NEXT: "identifier": "s:13InheritedDocs1PP8someFuncyyF"
2123
// CHECK-NEXT: "displayName": "P.someFunc()"
22-
// CHECK-NEXT: }
24+
25+
// IMPL: "source": "s:13InheritedDocs1PPAAE8someFuncyyF"
26+
// IMPL-NEXT: "target": "s:13InheritedDocs1PP8someFuncyyF"
27+
// IMPL-NEXT: "sourceOrigin"
28+
// IMPL-NEXT: "identifier": "s:13InheritedDocs1PP8someFuncyyF"
29+
// IMPL-NEXT: "displayName": "P.someFunc()"
2330

2431
/// Protocol P
2532
public protocol P {

0 commit comments

Comments
 (0)