Skip to content

Commit 97d8c99

Browse files
authored
Merge pull request swiftlang#30219 from bitjammer/acgarland/rdar-59128787-sg-include-synthesized
[SymbolGraph] Emit synthesized members
2 parents d6a9399 + f0887fa commit 97d8c99

27 files changed

+855
-475
lines changed

include/swift/AST/Comment.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,27 @@ class DocComment {
9393
};
9494

9595
/// Get a parsed documentation comment for the declaration, if there is one.
96+
///
97+
/// \param AllowSerialized Allow loading serialized doc comment data, including
98+
/// comment ranges.
9699
DocComment *getSingleDocComment(swift::markup::MarkupContext &Context,
97-
const Decl *D);
100+
const Decl *D, bool AllowSerialized = false);
98101

99-
const Decl *getDocCommentProvidingDecl(const Decl *D);
102+
/// Get the declaration that actually provides a doc comment for another.
103+
///
104+
/// \param AllowSerialized Allow loading serialized doc comment data, including
105+
/// comment ranges.
106+
const Decl *getDocCommentProvidingDecl(const Decl *D,
107+
bool AllowSerialized = false);
100108

101109
/// Attempt to get a doc comment from the declaration, or other inherited
102110
/// sources, like from base classes or protocols.
111+
///
112+
/// \param AllowSerialized Allow loading serialized doc comment data, including
113+
/// comment ranges.
103114
DocComment *getCascadingDocComment(swift::markup::MarkupContext &MC,
104-
const Decl *D);
115+
const Decl *D,
116+
bool AllowSerialized = false);
105117

106118
/// Extract comments parts from the given Markup node.
107119
swift::markup::CommentParts
Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- swift_indent_main.cpp - Swift code formatting tool ---------------===//
1+
//===--- SymbolGraphGen.h - Swift SymbolGraph Generator -------------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -10,32 +10,19 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "llvm/ADT/Triple.h"
14-
#include "swift/AST/AttrKind.h"
13+
#ifndef SWIFT_SYMBOLGRAPHGEN_SYMBOLGRAPHGEN_H
14+
#define SWIFT_SYMBOLGRAPHGEN_SYMBOLGRAPHGEN_H
1515

16-
namespace swift {
17-
18-
class ModuleDecl;
16+
#include "swift/AST/Module.h"
17+
#include "SymbolGraphOptions.h"
1918

19+
namespace swift {
2020
namespace symbolgraphgen {
2121

22-
struct SymbolGraphOptions {
23-
/// The directory to output the symbol graph JSON files.
24-
StringRef OutputDir;
25-
26-
/// The target of the module.
27-
llvm::Triple Target;
28-
29-
/// Pretty-print the JSON with newlines and indentation.
30-
bool PrettyPrint;
31-
32-
/// The minimum access level that symbols must have in order to be
33-
/// included in the graph.
34-
AccessLevel MinimumAccessLevel;
35-
};
36-
3722
/// Emit a Symbol Graph JSON file for a module.
3823
int emitSymbolGraphForModule(ModuleDecl *M, const SymbolGraphOptions &Options);
3924

4025
} // end namespace symbolgraphgen
4126
} // end namespace swift
27+
28+
#endif // SWIFT_SYMBOLGRAPHGEN_SYMBOLGRAPHGEN_H
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===--- SymbolGraphOptions.h - Swift SymbolGraph Options -----------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/ADT/Triple.h"
14+
#include "swift/AST/AttrKind.h"
15+
16+
#ifndef SWIFT_SYMBOLGRAPHGEN_SYMBOLGRAPHOPTIONS_H
17+
#define SWIFT_SYMBOLGRAPHGEN_SYMBOLGRAPHOPTIONS_H
18+
19+
namespace swift {
20+
namespace symbolgraphgen {
21+
22+
struct SymbolGraphOptions {
23+
/// The directory to output the symbol graph JSON files.
24+
StringRef OutputDir;
25+
26+
/// The target of the module.
27+
llvm::Triple Target;
28+
/// Pretty-print the JSON with newlines and indentation.
29+
bool PrettyPrint;
30+
31+
/// The minimum access level that symbols must have in order to be
32+
/// included in the graph.
33+
AccessLevel MinimumAccessLevel;
34+
35+
/// Emit members gotten through class inheritance or protocol default
36+
/// implementations with compound, "SYNTHESIZED" USRs.
37+
bool EmitSynthesizedMembers;
38+
};
39+
40+
} // end namespace symbolgraphgen
41+
} // end namespace swift
42+
43+
#endif // SWIFT_SYMBOLGRAPHGEN_SYMBOLGRAPHOPTIONS_H

lib/AST/DocComment.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -381,31 +381,33 @@ void DocComment::addInheritanceNote(swift::markup::MarkupContext &MC,
381381
}
382382

383383
DocComment *swift::getSingleDocComment(swift::markup::MarkupContext &MC,
384-
const Decl *D) {
384+
const Decl *D, bool AllowSerialized) {
385385
PrettyStackTraceDecl StackTrace("parsing comment for", D);
386386

387-
auto RC = D->getRawComment();
387+
auto RC = D->getRawComment(AllowSerialized);
388388
if (RC.isEmpty())
389389
return nullptr;
390390
return DocComment::create(D, MC, RC);
391391
}
392392

393393
namespace {
394-
const ValueDecl *findOverriddenDeclWithDocComment(const ValueDecl *VD) {
394+
const ValueDecl *findOverriddenDeclWithDocComment(const ValueDecl *VD,
395+
bool AllowSerialized = true) {
395396
// Only applies to class member.
396397
if (!VD->getDeclContext()->getSelfClassDecl())
397398
return nullptr;
398399

399400
while (auto *baseDecl = VD->getOverriddenDecl()) {
400-
if (!baseDecl->getRawComment().isEmpty())
401+
if (!baseDecl->getRawComment(AllowSerialized).isEmpty())
401402
return baseDecl;
402403
VD = baseDecl;
403404
}
404405

405406
return nullptr;
406407
}
407408

408-
const ValueDecl *findDefaultProvidedDeclWithDocComment(const ValueDecl *VD) {
409+
const ValueDecl *findDefaultProvidedDeclWithDocComment(const ValueDecl *VD,
410+
bool AllowSerialized = false) {
409411
auto protocol = VD->getDeclContext()->getExtendedProtocolDecl();
410412
// Only applies to protocol extension member.
411413
if (!protocol)
@@ -422,7 +424,7 @@ const ValueDecl *findDefaultProvidedDeclWithDocComment(const ValueDecl *VD) {
422424
for (auto *member : members) {
423425
if (!isa<ProtocolDecl>(member->getDeclContext()) ||
424426
!member->isProtocolRequirement() ||
425-
member->getRawComment().isEmpty())
427+
member->getRawComment(AllowSerialized).isEmpty())
426428
continue;
427429
if (requirement)
428430
// Found two or more decls with doc-comment.
@@ -433,7 +435,8 @@ const ValueDecl *findDefaultProvidedDeclWithDocComment(const ValueDecl *VD) {
433435
return requirement;
434436
}
435437

436-
const ValueDecl *findRequirementDeclWithDocComment(const ValueDecl *VD) {
438+
const ValueDecl *findRequirementDeclWithDocComment(const ValueDecl *VD,
439+
bool AllowSerialized = false) {
437440
std::queue<const ValueDecl *> requirements;
438441
while (true) {
439442
for (auto *req : VD->getSatisfiedProtocolRequirements()) {
@@ -450,36 +453,38 @@ const ValueDecl *findRequirementDeclWithDocComment(const ValueDecl *VD) {
450453
}
451454
} // namespace
452455

453-
const Decl *swift::getDocCommentProvidingDecl(const Decl *D) {
456+
const Decl *swift::getDocCommentProvidingDecl(const Decl *D,
457+
bool AllowSerialized) {
454458
if (!D->canHaveComment())
455459
return nullptr;
456460

457-
if (!D->getRawComment().isEmpty())
461+
if (!D->getRawComment(AllowSerialized).isEmpty())
458462
return D;
459463

460464
auto *VD = dyn_cast<ValueDecl>(D);
461465
if (!VD)
462466
return nullptr;
463467

464-
if (auto *overriden = findOverriddenDeclWithDocComment(VD))
468+
if (auto *overriden = findOverriddenDeclWithDocComment(VD, AllowSerialized))
465469
return overriden;
466470

467-
if (auto *requirement = findDefaultProvidedDeclWithDocComment(VD))
471+
if (auto *requirement = findDefaultProvidedDeclWithDocComment(VD, AllowSerialized))
468472
return requirement;
469473

470-
if (auto *requirement = findRequirementDeclWithDocComment(VD))
474+
if (auto *requirement = findRequirementDeclWithDocComment(VD, AllowSerialized))
471475
return requirement;
472476

473477
return nullptr;
474478
}
475479

476480
DocComment *
477-
swift::getCascadingDocComment(swift::markup::MarkupContext &MC, const Decl *D) {
478-
auto *docD = getDocCommentProvidingDecl(D);
481+
swift::getCascadingDocComment(swift::markup::MarkupContext &MC, const Decl *D,
482+
bool AllowSerialized) {
483+
auto *docD = getDocCommentProvidingDecl(D, AllowSerialized);
479484
if (!docD)
480485
return nullptr;
481486

482-
auto *doc = getSingleDocComment(MC, docD);
487+
auto *doc = getSingleDocComment(MC, docD, AllowSerialized);
483488
assert(doc && "getDocCommentProvidingDecl() returned decl with no comment");
484489

485490
// If the doc-comment is inherited from other decl, add a note about it.

lib/AST/USRGeneration.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/ASTMangler.h"
1818
#include "swift/AST/SwiftNameTranslation.h"
1919
#include "swift/AST/TypeCheckRequests.h"
20+
#include "swift/AST/USRGeneration.h"
2021
#include "llvm/ADT/SmallString.h"
2122
#include "llvm/ADT/StringRef.h"
2223
#include "llvm/Support/raw_ostream.h"

lib/SymbolGraphGen/DeclarationFragmentPrinter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "swift/AST/USRGeneration.h"
1314
#include "DeclarationFragmentPrinter.h"
1415
#include "SymbolGraphASTWalker.h"
1516

@@ -55,6 +56,8 @@ void DeclarationFragmentPrinter::closeFragment() {
5556
return;
5657
}
5758

59+
++NumFragments;
60+
5861
if (!Spelling.empty()) {
5962
OS.object([&](){
6063
OS.attribute("kind", getKindSpelling(Kind));
@@ -128,7 +131,8 @@ void DeclarationFragmentPrinter::printTypeRef(Type T, const TypeDecl *RefTo,
128131
PrintNameContext NameContext) {
129132
openFragment(FragmentKind::TypeIdentifier);
130133
printText(Name.str());
131-
USR = Graph.getUSR(RefTo);
134+
llvm::raw_svector_ostream OS(USR);
135+
ide::printDeclUSR(RefTo, OS);
132136
closeFragment();
133137
}
134138

lib/SymbolGraphGen/DeclarationFragmentPrinter.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ class DeclarationFragmentPrinter : public ASTPrinter {
5555
Text,
5656
};
5757

58-
SymbolGraph &Graph;
59-
6058
/// The output stream to print fragment objects to.
6159
llvm::json::OStream &OS;
6260

@@ -76,13 +74,14 @@ class DeclarationFragmentPrinter : public ASTPrinter {
7674
/// Close the current fragment if there is one, and commit it for display.
7775
void closeFragment();
7876

77+
unsigned NumFragments;
78+
7979
public:
80-
DeclarationFragmentPrinter(SymbolGraph &Graph,
81-
llvm::json::OStream &OS,
80+
DeclarationFragmentPrinter(llvm::json::OStream &OS,
8281
Optional<StringRef> Key = None)
83-
: Graph(Graph),
84-
OS(OS),
85-
Kind(FragmentKind::None) {
82+
: OS(OS),
83+
Kind(FragmentKind::None),
84+
NumFragments(0) {
8685
if (Key) {
8786
OS.attributeBegin(*Key);
8887
OS.arrayBegin();
@@ -114,6 +113,7 @@ class DeclarationFragmentPrinter : public ASTPrinter {
114113
closeFragment();
115114
OS.arrayEnd();
116115
OS.attributeEnd();
116+
assert(NumFragments);
117117
}
118118
};
119119

lib/SymbolGraphGen/Edge.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "swift/AST/Module.h"
1414
#include "Edge.h"
15+
#include "Symbol.h"
1516
#include "SymbolGraphASTWalker.h"
1617

1718
using namespace swift;
@@ -20,22 +21,26 @@ using namespace symbolgraphgen;
2021
void Edge::serialize(llvm::json::OStream &OS) const {
2122
OS.object([&](){
2223
OS.attribute("kind", Kind.Name);
23-
OS.attribute("source", Graph->getUSR(Source));
24-
OS.attribute("target", Graph->getUSR(Target));
24+
if (Kind == RelationshipKind::DefaultImplementationOf() && Source.getSynthesizedBaseTypeDecl()) {
25+
abort();
26+
}
27+
SmallString<256> SourceUSR, TargetUSR;
28+
29+
Source.getUSR(SourceUSR);
30+
OS.attribute("source", SourceUSR.str());
31+
32+
Target.getUSR(TargetUSR);
33+
OS.attribute("target", TargetUSR.str());
2534

2635
// In case a dependent module isn't available, serialize a fallback name.
27-
auto TargetModuleName = Target->getModuleContext()->getName().str();
36+
auto TargetModuleName = Target.getSymbolDecl()
37+
->getModuleContext()->getName().str();
2838

2939
if (TargetModuleName != Graph->M.getName().str()) {
30-
SmallVector<SmallString<32>, 8> TargetPathComponents;
31-
Graph->getPathComponents(Target, TargetPathComponents);
32-
3340
SmallString<128> Scratch(TargetModuleName);
34-
for (auto it = TargetPathComponents.begin();
35-
it != TargetPathComponents.end(); ++it) {
36-
Scratch.push_back('.');
37-
Scratch.append(*it);
38-
}
41+
llvm::raw_svector_ostream PathOS(Scratch);
42+
PathOS << '.';
43+
Target.printPath(PathOS);
3944
OS.attribute("targetFallback", Scratch.str());
4045
}
4146
});

0 commit comments

Comments
 (0)