Skip to content

Commit 16bc322

Browse files
authored
Merge pull request #24813 from rintaro/comment-refactor
[AST] Inherit doc-brief comment from protocol, superclass, and requirement
2 parents 40a49ce + b1fae10 commit 16bc322

File tree

17 files changed

+353
-201
lines changed

17 files changed

+353
-201
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ class ASTPrinter {
286286

287287
/// To sanitize a malformed utf8 string to a well-formed one.
288288
static std::string sanitizeUtf8(StringRef Text);
289-
static ValueDecl* findConformancesWithDocComment(ValueDecl *VD);
290289

291290
private:
292291
virtual void anchor();

include/swift/AST/Comment.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,26 @@
1818

1919
namespace swift {
2020
class Decl;
21-
class DocComment;
21+
class TypeDecl;
2222
struct RawComment;
2323

2424
class DocComment {
2525
const Decl *D;
26-
const swift::markup::Document *Doc = nullptr;
27-
const swift::markup::CommentParts Parts;
26+
swift::markup::Document *Doc = nullptr;
27+
swift::markup::CommentParts Parts;
2828

29-
public:
3029
DocComment(const Decl *D, swift::markup::Document *Doc,
3130
swift::markup::CommentParts Parts)
3231
: D(D), Doc(Doc), Parts(Parts) {}
3332

33+
public:
34+
static DocComment *create(const Decl *D, swift::markup::MarkupContext &MC,
35+
RawComment RC);
36+
37+
void addInheritanceNote(swift::markup::MarkupContext &MC, TypeDecl *base);
38+
3439
const Decl *getDecl() const { return D; }
40+
void setDecl(const Decl *D) { this->D = D; }
3541

3642
const swift::markup::Document *getDocument() const { return Doc; }
3743

@@ -87,19 +93,23 @@ class DocComment {
8793
};
8894

8995
/// Get a parsed documentation comment for the declaration, if there is one.
90-
Optional<DocComment *>getSingleDocComment(swift::markup::MarkupContext &Context,
91-
const Decl *D);
96+
DocComment *getSingleDocComment(swift::markup::MarkupContext &Context,
97+
const Decl *D);
98+
99+
const Decl *getDocCommentProvidingDecl(const Decl *D);
92100

93101
/// Attempt to get a doc comment from the declaration, or other inherited
94102
/// sources, like from base classes or protocols.
95-
Optional<DocComment *> getCascadingDocComment(swift::markup::MarkupContext &MC,
96-
const Decl *D);
103+
DocComment *getCascadingDocComment(swift::markup::MarkupContext &MC,
104+
const Decl *D);
97105

98106
/// Extract comments parts from the given Markup node.
99107
swift::markup::CommentParts
100108
extractCommentParts(swift::markup::MarkupContext &MC,
101109
swift::markup::MarkupASTNode *Node);
110+
111+
/// Extract brief comment from \p RC, and print it to \p OS .
112+
void printBriefComment(RawComment RC, llvm::raw_ostream &OS);
102113
} // namespace swift
103114

104115
#endif // LLVM_SWIFT_AST_COMMENT_H
105-

include/swift/AST/PrintOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ struct PrintOptions {
349349

350350
/// Whether to print the doc-comment from the conformance if a member decl
351351
/// has no associated doc-comment by itself.
352-
bool ElevateDocCommentFromConformance = false;
352+
bool CascadeDocComment = false;
353353

354354
/// Whether to print the content of an extension decl inside the type decl where it
355355
/// extends from.
@@ -480,7 +480,7 @@ struct PrintOptions {
480480
result.SkipDeinit = true;
481481
result.ExcludeAttrList.push_back(DAK_DiscardableResult);
482482
result.EmptyLineBetweenMembers = true;
483-
result.ElevateDocCommentFromConformance = true;
483+
result.CascadeDocComment = true;
484484
result.ShouldQualifyNestedDeclarations =
485485
QualifyNestedDeclarations::Always;
486486
result.PrintDocumentationComments = true;

lib/AST/ASTPrinter.cpp

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/ASTMangler.h"
2020
#include "swift/AST/ASTVisitor.h"
2121
#include "swift/AST/Attr.h"
22+
#include "swift/AST/Comment.h"
2223
#include "swift/AST/Decl.h"
2324
#include "swift/AST/Expr.h"
2425
#include "swift/AST/GenericEnvironment.h"
@@ -250,24 +251,6 @@ std::string ASTPrinter::sanitizeUtf8(StringRef Text) {
250251
return Builder.str();
251252
}
252253

253-
ValueDecl* ASTPrinter::findConformancesWithDocComment(ValueDecl *VD) {
254-
assert(VD->getRawComment().isEmpty());
255-
std::queue<ValueDecl*> AllConformances;
256-
AllConformances.push(VD);
257-
while (!AllConformances.empty()) {
258-
auto *VD = AllConformances.front();
259-
AllConformances.pop();
260-
if (VD->getRawComment().isEmpty()) {
261-
for (auto *Req : VD->getSatisfiedProtocolRequirements()) {
262-
AllConformances.push(Req);
263-
}
264-
} else {
265-
return VD;
266-
}
267-
}
268-
return nullptr;
269-
}
270-
271254
void ASTPrinter::anchor() {}
272255

273256
void ASTPrinter::printIndent() {
@@ -569,20 +552,14 @@ class PrintAST : public ASTVisitor<PrintAST> {
569552
}
570553

571554
void printSwiftDocumentationComment(const Decl *D) {
555+
if (Options.CascadeDocComment)
556+
D = getDocCommentProvidingDecl(D);
557+
if (!D)
558+
return;
572559
auto RC = D->getRawComment();
573-
if (RC.isEmpty() && !Options.ElevateDocCommentFromConformance)
560+
if (RC.isEmpty())
574561
return;
575-
576-
if (RC.isEmpty()) {
577-
if (auto *VD = dyn_cast<ValueDecl>(D)) {
578-
if (auto *Req = ASTPrinter::findConformancesWithDocComment(
579-
const_cast<ValueDecl*>(VD))) {
580-
printRawComment(Req->getRawComment());
581-
}
582-
}
583-
} else {
584-
printRawComment(RC);
585-
}
562+
printRawComment(RC);
586563
}
587564

588565
void printDocumentationComment(const Decl *D) {

0 commit comments

Comments
 (0)