Skip to content

Commit b1fae10

Browse files
committed
[AST] Restore previous behavior for DocCommentAsXML
Associate requirement decl with DocComment.
1 parent 87c1c5c commit b1fae10

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

include/swift/AST/Comment.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,23 @@ class TypeDecl;
2222
struct RawComment;
2323

2424
class DocComment {
25+
const Decl *D;
2526
swift::markup::Document *Doc = nullptr;
2627
swift::markup::CommentParts Parts;
2728

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

3337
void addInheritanceNote(swift::markup::MarkupContext &MC, TypeDecl *base);
3438

39+
const Decl *getDecl() const { return D; }
40+
void setDecl(const Decl *D) { this->D = D; }
41+
3542
const swift::markup::Document *getDocument() const { return Doc; }
3643

3744
swift::markup::CommentParts getParts() const {
@@ -106,4 +113,3 @@ void printBriefComment(RawComment RC, llvm::raw_ostream &OS);
106113
} // namespace swift
107114

108115
#endif // LLVM_SWIFT_AST_COMMENT_H
109-

lib/AST/DocComment.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,13 @@ swift::extractCommentParts(swift::markup::MarkupContext &MC,
352352
return Parts;
353353
}
354354

355-
DocComment *DocComment::create(markup::MarkupContext &MC, RawComment RC) {
355+
DocComment *DocComment::create(const Decl *D, markup::MarkupContext &MC,
356+
RawComment RC) {
356357
assert(!RC.isEmpty());
357358
swift::markup::LineList LL = MC.getLineList(RC);
358359
auto *Doc = swift::markup::parseDocument(MC, LL);
359360
auto Parts = extractCommentParts(MC, Doc);
360-
return new (MC) DocComment(Doc, Parts);
361+
return new (MC) DocComment(D, Doc, Parts);
361362
}
362363

363364
void DocComment::addInheritanceNote(swift::markup::MarkupContext &MC,
@@ -385,7 +386,7 @@ DocComment *swift::getSingleDocComment(swift::markup::MarkupContext &MC,
385386
auto RC = D->getRawComment();
386387
if (RC.isEmpty())
387388
return nullptr;
388-
return DocComment::create(MC, RC);
389+
return DocComment::create(D, MC, RC);
389390
}
390391

391392
namespace {
@@ -480,10 +481,19 @@ swift::getCascadingDocComment(swift::markup::MarkupContext &MC, const Decl *D) {
480481
assert(doc && "getDocCommentProvidingDecl() returned decl with no comment");
481482

482483
// If the doc-comment is inherited from other decl, add a note about it.
483-
if (docD != D)
484-
if (auto baseD = docD->getDeclContext()->getSelfNominalTypeDecl())
484+
if (docD != D) {
485+
doc->setDecl(D);
486+
if (auto baseD = docD->getDeclContext()->getSelfNominalTypeDecl()) {
485487
doc->addInheritanceNote(MC, baseD);
486488

489+
// If the doc is inherited from protocol requirement, associate the
490+
// requirement with the doc-comment.
491+
// FIXME: This is to keep the old behavior.
492+
if (isa<ProtocolDecl>(baseD))
493+
doc->setDecl(docD);
494+
}
495+
}
496+
487497
return doc;
488498
}
489499

lib/IDE/CommentConversion.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ struct CommentToXMLConverter {
258258
OS << "</Tags>";
259259
}
260260

261-
void visitDocComment(const Decl *D, const DocComment *DC);
261+
void visitDocComment(const DocComment *DC);
262262
void visitCommentParts(const swift::markup::CommentParts &Parts);
263263
};
264264
} // unnamed namespace
@@ -297,7 +297,8 @@ void CommentToXMLConverter::visitCommentParts(const swift::markup::CommentParts
297297
}
298298
}
299299

300-
void CommentToXMLConverter::visitDocComment(const Decl *D, const DocComment *DC) {
300+
void CommentToXMLConverter::visitDocComment(const DocComment *DC) {
301+
const Decl *D = DC->getDecl();
301302

302303
StringRef RootEndTag;
303304
if (isa<AbstractFunctionDecl>(D)) {
@@ -463,7 +464,7 @@ bool ide::getDocumentationCommentAsXML(const Decl *D, raw_ostream &OS) {
463464
return false;
464465

465466
CommentToXMLConverter Converter(OS);
466-
Converter.visitDocComment(D, DC);
467+
Converter.visitDocComment(DC);
467468

468469
OS.flush();
469470
return true;

test/IDE/comment_inherited_protocol.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,23 @@ protocol ChildProtocol : ParentProtocol1, ParentProtocol2 {
6363
extension ChildProtocol {
6464
// Should come from ParentProtocol1.
6565
func onlyParent1() {}
66-
// CHECK: Func/onlyParent1 {{.*}} DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>onlyParent1()</Name><USR>s:14swift_ide_test13ChildProtocolPAAE11onlyParent1yyF</USR><Declaration>func onlyParent1()</Declaration><CommentParts><Abstract><Para>ParentProtocol1.onlyParent1()</Para></Abstract><Discussion><Note><Para>This documentation comment was inherited from <codeVoice>ParentProtocol1</codeVoice>.</Para></Note></Discussion></CommentParts></Function>]
66+
// CHECK: Func/onlyParent1 {{.*}} DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>onlyParent1()</Name><USR>s:14swift_ide_test15ParentProtocol1P11onlyParent1yyF</USR><Declaration>func onlyParent1()</Declaration><CommentParts><Abstract><Para>ParentProtocol1.onlyParent1()</Para></Abstract><Discussion><Note><Para>This documentation comment was inherited from <codeVoice>ParentProtocol1</codeVoice>.</Para></Note></Discussion></CommentParts></Function>]
6767

6868
// Should come from ParentProtocol1.
6969
var onlyParent1Var: Int { return 0 }
70-
// CHECK: Var/onlyParent1Var {{.*}} DocCommentAsXML=[<Other file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>onlyParent1Var</Name><USR>s:14swift_ide_test13ChildProtocolPAAE14onlyParent1VarSivp</USR><Declaration>var onlyParent1Var: Int { get }</Declaration><CommentParts><Abstract><Para>ParentProtocol.onlyParent1Var</Para></Abstract><Discussion><Note><Para>This documentation comment was inherited from <codeVoice>ParentProtocol1</codeVoice>.</Para></Note></Discussion></CommentParts></Other>]
70+
// CHECK: Var/onlyParent1Var {{.*}} DocCommentAsXML=[<Other file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>onlyParent1Var</Name><USR>s:14swift_ide_test15ParentProtocol1P14onlyParent1VarSivp</USR><Declaration>var onlyParent1Var: Int { get }</Declaration><CommentParts><Abstract><Para>ParentProtocol.onlyParent1Var</Para></Abstract><Discussion><Note><Para>This documentation comment was inherited from <codeVoice>ParentProtocol1</codeVoice>.</Para></Note></Discussion></CommentParts></Other>]
7171

7272
// Should come from ParentProtocol1.
7373
subscript(index: Int) -> Int { return 0 }
7474
// CHECK: Subscript/subscript {{.*}} DocCommentAsXML=[<Other file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>subscript(_:)</Name><USR>{{.*}}</USR><Declaration>subscript(index: Int) -&gt; Int { get }</Declaration><CommentParts><Abstract><Para>ParentProtocol.subscript(index:)</Para></Abstract><Discussion><Note><Para>This documentation comment was inherited from <codeVoice>ParentProtocol1</codeVoice>.</Para></Note></Discussion></CommentParts></Other>]
7575

7676
// Should come from ParentProtocol1.
7777
typealias AssocType = Int
78-
// CHECK: TypeAlias/AssocType {{.*}} DocCommentAsXML=[<Other file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>AssocType</Name><USR>s:14swift_ide_test13ChildProtocolPAAE9AssocTypea</USR><Declaration>typealias AssocType = Int</Declaration><CommentParts><Abstract><Para>ParentProtocol.AssocType</Para></Abstract><Discussion><Note><Para>This documentation comment was inherited from <codeVoice>ParentProtocol1</codeVoice>.</Para></Note></Discussion></CommentParts></Other>]
78+
// CHECK: TypeAlias/AssocType {{.*}} DocCommentAsXML=[<Other file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>AssocType</Name><USR>s:14swift_ide_test15ParentProtocol1P9AssocTypeQa</USR><Declaration>associatedtype AssocType</Declaration><CommentParts><Abstract><Para>ParentProtocol.AssocType</Para></Abstract><Discussion><Note><Para>This documentation comment was inherited from <codeVoice>ParentProtocol1</codeVoice>.</Para></Note></Discussion></CommentParts></Other>]
7979

8080
// Should come from ParentProtocol2.
8181
func onlyParent2() {}
82-
// CHECK: Func/onlyParent2 {{.*}} DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>onlyParent2()</Name><USR>s:14swift_ide_test13ChildProtocolPAAE11onlyParent2yyF</USR><Declaration>func onlyParent2()</Declaration><CommentParts><Abstract><Para>ParentProtocol2.onlyParent2()</Para></Abstract><Discussion><Note><Para>This documentation comment was inherited from <codeVoice>ParentProtocol2</codeVoice>.</Para></Note></Discussion></CommentParts></Function>]
82+
// CHECK: Func/onlyParent2 {{.*}} DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>onlyParent2()</Name><USR>s:14swift_ide_test15ParentProtocol2P11onlyParent2yyF</USR><Declaration>func onlyParent2()</Declaration><CommentParts><Abstract><Para>ParentProtocol2.onlyParent2()</Para></Abstract><Discussion><Note><Para>This documentation comment was inherited from <codeVoice>ParentProtocol2</codeVoice>.</Para></Note></Discussion></CommentParts></Function>]
8383

8484
// Should show nothing because the requirement is in both parents.
8585
func commonParentRequirement() {}

0 commit comments

Comments
 (0)