Skip to content

Commit db2c117

Browse files
committed
[AST] Inherit doc-brief comment from protocol, superclass, and requirement
rdar://problem/38422822
1 parent bb2e003 commit db2c117

File tree

17 files changed

+344
-207
lines changed

17 files changed

+344
-207
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: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,19 @@
1818

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

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

28+
DocComment(swift::markup::Document *Doc, swift::markup::CommentParts Parts)
29+
: Doc(Doc), Parts(Parts) {}
2930
public:
30-
DocComment(const Decl *D, swift::markup::Document *Doc,
31-
swift::markup::CommentParts Parts)
32-
: D(D), Doc(Doc), Parts(Parts) {}
31+
static DocComment *create(swift::markup::MarkupContext &MC, RawComment RC);
3332

34-
const Decl *getDecl() const { return D; }
33+
void addInheritanceNote(swift::markup::MarkupContext &MC, TypeDecl *base);
3534

3635
const swift::markup::Document *getDocument() const { return Doc; }
3736

@@ -87,18 +86,23 @@ class DocComment {
8786
};
8887

8988
/// Get a parsed documentation comment for the declaration, if there is one.
90-
Optional<DocComment *>getSingleDocComment(swift::markup::MarkupContext &Context,
91-
const Decl *D);
89+
DocComment *getSingleDocComment(swift::markup::MarkupContext &Context,
90+
const Decl *D);
91+
92+
const Decl *getDocCommentProvidingDecl(const Decl *D);
9293

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

9899
/// Extract comments parts from the given Markup node.
99100
swift::markup::CommentParts
100101
extractCommentParts(swift::markup::MarkupContext &MC,
101102
swift::markup::MarkupASTNode *Node);
103+
104+
/// Extract brief comment from \p RC, and print it to \p OS .
105+
void printBriefComment(RawComment RC, llvm::raw_ostream &OS);
102106
} // namespace swift
103107

104108
#endif // LLVM_SWIFT_AST_COMMENT_H

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)