Skip to content

Commit 170dcd8

Browse files
Merge pull request #36863 from apple/QuietMisdreavus/docs-inheritance
[SymbolGraph] Add information about "inherited docs" for synthesized symbols
2 parents 89039a8 + 640aa21 commit 170dcd8

File tree

14 files changed

+138
-6
lines changed

14 files changed

+138
-6
lines changed

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ class FrontendOptions {
394394
///
395395
/// \sa SymbolGraphASTWalker
396396
std::string SymbolGraphOutputDir;
397+
398+
/// Whether to emit doc comment information in symbol graphs for symbols
399+
/// which are inherited through classes or default implementations.
400+
bool SkipInheritedDocs = false;
397401

398402
private:
399403
static bool canActionEmitDependencies(ActionType);

include/swift/Option/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,4 +1214,10 @@ def minimum_access_level : Separate<["-"], "minimum-access-level">,
12141214
HelpText<"Include symbols with this access level or more">,
12151215
MetaVarName<"<level>">;
12161216

1217+
def skip_inherited_docs : Flag<["-"], "skip-inherited-docs">,
1218+
Flags<[SwiftSymbolGraphExtractOption, FrontendOption,
1219+
NoInteractiveOption, SupplementaryOutput, HelpHidden]>,
1220+
HelpText<"Skip emitting doc comments for members inherited through classes or "
1221+
"default implementations">;
1222+
12171223
include "FrontendOptions.td"

include/swift/Serialization/SerializationOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace swift {
3131
const char *DocOutputPath = nullptr;
3232
const char *SourceInfoOutputPath = nullptr;
3333
std::string SymbolGraphOutputDir;
34+
bool SkipSymbolGraphInheritedDocs = true;
3435

3536
StringRef GroupInfoPath;
3637
StringRef ImportedHeader;

include/swift/SymbolGraphGen/SymbolGraphOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ struct SymbolGraphOptions {
3939
/// Whether to print informational messages when rendering
4040
/// a symbol graph.
4141
bool PrintMessages;
42+
43+
/// Whether to skip docs for symbols with compound, "SYNTHESIZED" USRs.
44+
bool SkipInheritedDocs;
4245
};
4346

4447
} // end namespace symbolgraphgen

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ bool ArgsToFrontendOptionsConverter::convert(
248248
if (const Arg *A = Args.getLastArg(OPT_emit_symbol_graph_dir)) {
249249
Opts.SymbolGraphOutputDir = A->getValue();
250250
}
251+
252+
Opts.SkipInheritedDocs = Args.hasArg(OPT_skip_inherited_docs);
251253

252254
return false;
253255
}

lib/Frontend/Frontend.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
168168
llvm::sys::fs::make_absolute(OutputDir);
169169
serializationOpts.SymbolGraphOutputDir = OutputDir.str().str();
170170
}
171+
serializationOpts.SkipSymbolGraphInheritedDocs = opts.SkipInheritedDocs;
171172

172173
if (!getIRGenOptions().ForceLoadSymbolName.empty())
173174
serializationOpts.AutolinkForceLoad = true;

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5635,6 +5635,7 @@ void swift::serialize(ModuleOrSourceFile DC,
56355635
AccessLevel::Public,
56365636
/*EmitSynthesizedMembers*/true,
56375637
/*PrintMessages*/false,
5638+
/*EmitInheritedDocs*/options.SkipSymbolGraphInheritedDocs,
56385639
};
56395640
symbolgraphgen::emitSymbolGraphForModule(M, SGOpts);
56405641
}

lib/SymbolGraphGen/Edge.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,27 @@ void Edge::serialize(llvm::json::OStream &OS) const {
5757
});
5858
}
5959
}
60+
61+
const ValueDecl *InheritingDecl = nullptr;
62+
if (const auto *ID = Source.getDeclInheritingDocs()) {
63+
if (Target.getSymbolDecl() == ID || Source.getSynthesizedBaseTypeDecl())
64+
InheritingDecl = ID;
65+
}
66+
67+
// If our source symbol is a inheriting decl, write in information about
68+
// where it's inheriting docs from.
69+
if (InheritingDecl) {
70+
Symbol inheritedSym(Graph, InheritingDecl, nullptr);
71+
SmallString<256> USR, Display;
72+
llvm::raw_svector_ostream DisplayOS(Display);
73+
74+
inheritedSym.getUSR(USR);
75+
inheritedSym.printPath(DisplayOS);
76+
77+
OS.attributeObject("sourceOrigin", [&](){
78+
OS.attribute("identifier", USR.str());
79+
OS.attribute("displayName", Display.str());
80+
});
81+
}
6082
});
6183
}

lib/SymbolGraphGen/FormatVersion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515

1616
#define SWIFT_SYMBOLGRAPH_FORMAT_MAJOR 0
1717
#define SWIFT_SYMBOLGRAPH_FORMAT_MINOR 5
18-
#define SWIFT_SYMBOLGRAPH_FORMAT_PATCH 2
18+
#define SWIFT_SYMBOLGRAPH_FORMAT_PATCH 3
1919

2020
#endif // SWIFT_SYMBOLGRAPHGEN_FORMATVERSION_H

lib/SymbolGraphGen/Symbol.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,34 @@ void Symbol::serializeRange(size_t InitialIndentation,
172172
});
173173
}
174174

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

0 commit comments

Comments
 (0)