Skip to content

Commit abed7b6

Browse files
committed
Merge pull request #2132 from bitjammer/closure-param-doc
Add nested parameter/returns/throws comment recognition
2 parents abc5541 + c20f3db commit abed7b6

File tree

19 files changed

+312
-205
lines changed

19 files changed

+312
-205
lines changed

bindings/xml/comment-xml-schema.rng

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,28 @@
650650
<!-- In general, template parameters with whitespace discussion
651651
should not be emitted, unless direction is explicitly specified.
652652
Schema might be more strict here. -->
653-
<element name="Discussion">
654-
<ref name="BlockContent" />
655-
</element>
653+
<choice>
654+
<element name="ClosureParameter">
655+
<optional>
656+
<ref name="Abstract" />
657+
</optional>
658+
<optional>
659+
<ref name="Parameters" />
660+
</optional>
661+
<optional>
662+
<ref name="ResultDiscussion" />
663+
</optional>
664+
<optional>
665+
<ref name="ThrowsDiscussion" />
666+
</optional>
667+
<optional>
668+
<ref name="Discussion" />
669+
</optional>
670+
</element>
671+
<element name="Discussion">
672+
<ref name="BlockContent" />
673+
</element>
674+
</choice>
656675
</element>
657676
</oneOrMore>
658677
</element>

include/swift/AST/Comment.h

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,40 @@ class DocComment;
2222
struct RawComment;
2323

2424
class DocComment {
25-
public:
26-
struct CommentParts {
27-
Optional<const llvm::markup::Paragraph *>Brief;
28-
ArrayRef<const llvm::markup::MarkupASTNode *> BodyNodes;
29-
ArrayRef<const llvm::markup::ParamField *> ParamFields;
30-
Optional<const llvm::markup::ReturnsField *> ReturnsField;
31-
Optional<const llvm::markup::ThrowsField *> ThrowsField;
32-
33-
bool isEmpty() const {
34-
return !Brief.hasValue() && !ReturnsField.hasValue() && !ThrowsField.hasValue() && BodyNodes.empty() && ParamFields.empty();
35-
}
36-
};
37-
38-
private:
3925
const Decl *D;
40-
const llvm::markup::Document *Doc = nullptr;
41-
const CommentParts Parts;
26+
const swift::markup::Document *Doc = nullptr;
27+
const swift::markup::CommentParts Parts;
4228

4329
public:
44-
DocComment(const Decl *D, llvm::markup::Document *Doc,
45-
CommentParts Parts)
30+
DocComment(const Decl *D, swift::markup::Document *Doc,
31+
swift::markup::CommentParts Parts)
4632
: D(D), Doc(Doc), Parts(Parts) {}
4733

4834
const Decl *getDecl() const { return D; }
4935

50-
const llvm::markup::Document *getDocument() const { return Doc; }
36+
const swift::markup::Document *getDocument() const { return Doc; }
5137

52-
CommentParts getParts() const {
38+
swift::markup::CommentParts getParts() const {
5339
return Parts;
5440
}
5541

56-
Optional<const llvm::markup::Paragraph *> getBrief() const {
42+
Optional<const swift::markup::Paragraph *> getBrief() const {
5743
return Parts.Brief;
5844
}
5945

60-
Optional<const llvm::markup::ReturnsField * >getReturnsField() const {
46+
Optional<const swift::markup::ReturnsField * >getReturnsField() const {
6147
return Parts.ReturnsField;
6248
}
6349

64-
Optional<const llvm::markup::ThrowsField*> getThrowsField() const {
50+
Optional<const swift::markup::ThrowsField*> getThrowsField() const {
6551
return Parts.ThrowsField;
6652
}
6753

68-
ArrayRef<const llvm::markup::ParamField *> getParamFields() const {
54+
ArrayRef<const swift::markup::ParamField *> getParamFields() const {
6955
return Parts.ParamFields;
7056
}
7157

72-
ArrayRef<const llvm::markup::MarkupASTNode *> getBodyNodes() const {
58+
ArrayRef<const swift::markup::MarkupASTNode *> getBodyNodes() const {
7359
return Parts.BodyNodes;
7460
}
7561

@@ -79,7 +65,7 @@ class DocComment {
7965

8066
// Only allow allocation using the allocator in MarkupContext or by
8167
// placement new.
82-
void *operator new(size_t Bytes, llvm::markup::MarkupContext &MC,
68+
void *operator new(size_t Bytes, swift::markup::MarkupContext &MC,
8369
unsigned Alignment = alignof(DocComment));
8470
void *operator new(size_t Bytes, void *Mem) {
8571
assert(Mem);
@@ -91,7 +77,7 @@ class DocComment {
9177
void operator delete(void *Data) = delete;
9278
};
9379

94-
Optional<DocComment *>getDocComment(llvm::markup::MarkupContext &Context,
80+
Optional<DocComment *>getDocComment(swift::markup::MarkupContext &Context,
9581
const Decl *D);
9682

9783
} // namespace swift

include/swift/Markup/AST.h

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,47 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
//
13-
#ifndef LLVM_MARKUP_AST_H
14-
#define LLVM_MARKUP_AST_H
13+
#ifndef SWIFT_MARKUP_AST_H
14+
#define SWIFT_MARKUP_AST_H
1515

1616
#include "swift/Markup/LineList.h"
1717
#include "llvm/ADT/Optional.h"
1818
#include "llvm/Support/ErrorHandling.h"
1919
#include "llvm/Support/TrailingObjects.h"
2020

21-
namespace llvm {
21+
namespace swift {
2222
namespace markup {
2323

2424
class MarkupContext;
25+
class MarkupASTNode;
26+
class Paragraph;
27+
class ParamField;
28+
class ReturnsField;
29+
class ThrowsField;
30+
31+
/// The basic structure of a doc comment attached to a Swift
32+
/// declaration.
33+
struct CommentParts {
34+
Optional<const Paragraph *> Brief;
35+
ArrayRef<const MarkupASTNode *> BodyNodes;
36+
ArrayRef<ParamField *> ParamFields;
37+
Optional<const ReturnsField *> ReturnsField;
38+
Optional<const ThrowsField *> ThrowsField;
39+
40+
bool isEmpty() const {
41+
return !Brief.hasValue() &&
42+
!ReturnsField.hasValue() &&
43+
!ThrowsField.hasValue() &&
44+
BodyNodes.empty() &&
45+
ParamFields.empty();
46+
}
47+
48+
bool hasFunctionDocumentation() const {
49+
return !ParamFields.empty() ||
50+
ReturnsField.hasValue() ||
51+
ThrowsField.hasValue();
52+
}
53+
};
2554

2655
#define MARKUP_AST_NODE(Id, Parent) class Id;
2756
#define ABSTRACT_MARKUP_AST_NODE(Id, Parent) class Id;
@@ -585,6 +614,10 @@ class ParamField final : public PrivateExtension,
585614

586615
StringRef Name;
587616

617+
// Parameter fields can contain a substructure describing a
618+
// function or closure parameter.
619+
llvm::Optional<CommentParts> Parts;
620+
588621
ParamField(StringRef Name, ArrayRef<MarkupASTNode *> Children);
589622

590623
public:
@@ -596,6 +629,21 @@ class ParamField final : public PrivateExtension,
596629
return Name;
597630
}
598631

632+
llvm::Optional<CommentParts> getParts() const {
633+
return Parts;
634+
}
635+
636+
void setParts(CommentParts P) {
637+
Parts = P;
638+
}
639+
640+
bool isClosureParameter() const {
641+
if (!Parts.hasValue())
642+
return false;
643+
644+
return Parts.getValue().hasFunctionDocumentation();
645+
}
646+
599647
ArrayRef<MarkupASTNode *> getChildren() {
600648
return {getTrailingObjects<MarkupASTNode *>(), NumChildren};
601649
}
@@ -682,6 +730,6 @@ void dump(const MarkupASTNode *Node, llvm::raw_ostream &OS, unsigned indent = 0)
682730
void printInlinesUnder(const MarkupASTNode *Node, llvm::raw_ostream &OS,
683731
bool PrintDecorators = false);
684732
} // namespace markup
685-
} // namespace llvm
733+
} // namespace swift
686734

687-
#endif // LLVM_MARKUP_AST_H
735+
#endif // SWIFT_MARKUP_AST_H

include/swift/Markup/LineList.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef LLVM_MARKUP_LINELIST_H
14-
#define LLVM_MARKUP_LINELIST_H
13+
#ifndef SWIFT_MARKUP_LINELIST_H
14+
#define SWIFT_MARKUP_LINELIST_H
1515

1616
#include "llvm/ADT/ArrayRef.h"
1717
#include "llvm/ADT/StringRef.h"
1818
#include "swift/Basic/SourceLoc.h"
1919

20-
namespace llvm {
20+
namespace swift {
2121
namespace markup {
2222

2323
class MarkupContext;
@@ -76,6 +76,6 @@ class LineListBuilder {
7676
};
7777

7878
} // namespace markup
79-
} // namespace llvm
79+
} // namespace swift
8080

81-
#endif // LLVM_MARKUP_LINELIST_H
81+
#endif // SWIFT_MARKUP_LINELIST_H

include/swift/Markup/Markup.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef LLVM_MARKUP_MARKUP_H
14-
#define LLVM_MARKUP_MARKUP_H
13+
#ifndef SWIFT_MARKUP_MARKUP_H
14+
#define SWIFT_MARKUP_MARKUP_H
1515

1616
#include "llvm/ADT/ArrayRef.h"
1717
#include "llvm/Support/Allocator.h"
@@ -20,12 +20,10 @@
2020
#include "swift/Markup/AST.h"
2121
#include "swift/Markup/LineList.h"
2222

23-
2423
namespace swift {
25-
struct RawComment;
26-
}
2724

28-
namespace llvm {
25+
struct RawComment;
26+
2927
namespace markup {
3028

3129
class LineList;
@@ -65,6 +63,6 @@ class MarkupContext final {
6563
Document *parseDocument(MarkupContext &MC, LineList &LL);
6664

6765
} // namespace markup
68-
} // namespace llvm
66+
} // namespace swift
6967

70-
#endif // LLVM_MARKUP_MARKUP_H
68+
#endif // SWIFT_MARKUP_MARKUP_H

include/swift/Markup/SourceLoc.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
//===----------------------------------------------------------------------===//
1212

1313

14-
#ifndef LLVM_REST_SOURCELOC_H
15-
#define LLVM_REST_SOURCELOC_H
14+
#ifndef SWIFT_MARKUP_SOURCELOC_H
15+
#define SWIFT_MARKUP_SOURCELOC_H
1616

1717
#include "llvm/ADT/StringRef.h"
1818
#include <algorithm>
1919
#include <cassert>
2020
#include <utility>
2121
#include <vector>
2222

23-
namespace llvm {
23+
namespace swift {
2424
namespace markup {
2525

2626
class SourceLoc {
@@ -147,7 +147,7 @@ SourceManager<ExternalSourceLocTy>::toExternalSourceLoc(SourceLoc Loc) const {
147147
}
148148

149149
} // namespace markup
150-
} // namespace llvm
150+
} // namespace swift
151151

152-
#endif // LLVM_REST_SOURCELOC_H
152+
#endif // SWIFT_MARKUP_SOURCELOC_H
153153

include/swift/Markup/XMLUtils.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef LLVM_MARKUP_XML_UTILS_H
14-
#define LLVM_MARKUP_XML_UTILS_H
13+
#ifndef SWIFT_MARKUP_XML_UTILS_H
14+
#define SWIFT_MARKUP_XML_UTILS_H
1515

1616
#include "llvm/ADT/StringRef.h"
1717
#include "llvm/Support/raw_ostream.h"
1818

19-
namespace llvm {
19+
namespace swift {
2020
namespace markup {
2121

2222
// FIXME: copied from Clang's
@@ -71,7 +71,7 @@ static inline void appendWithCDATAEscaping(raw_ostream &OS, StringRef S) {
7171
}
7272

7373
} // namespace markup
74-
} // namespace llvm
74+
} // namespace swift
7575

76-
#endif // LLVM_MARKUP_XML_UTILS_H
76+
#endif // SWIFT_MARKUP_XML_UTILS_H
7777

0 commit comments

Comments
 (0)