Skip to content

Commit ff9bd90

Browse files
committed
[clang-doc] remove nesting of text comments inside paragraphs
Text comments were unnecessarily nested inside Paragraph comments as a Children array. If they're at the top level, we can also avoid more nesting in templates.
1 parent 1768c96 commit ff9bd90

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

clang-tools-extra/clang-doc/JSONGenerator.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ static void insertComment(Object &Description, json::Value &Comment,
9797
}
9898
}
9999

100+
static json::Value extractTextComments(Object *ParagraphComment) {
101+
if (!ParagraphComment)
102+
return json::Object();
103+
return *ParagraphComment->get("Children");
104+
}
105+
100106
static Object serializeComment(const CommentInfo &I, Object &Description) {
101107
// taken from PR #142273
102108
Object Obj = Object();
@@ -117,14 +123,9 @@ static Object serializeComment(const CommentInfo &I, Object &Description) {
117123
}
118124

119125
case CommentKind::CK_BlockCommandComment: {
120-
Child.insert({"Command", I.Name});
121-
// TODO: The "Children" level of nesting isn't needed for comments that
122-
// don't hold additional information at the top level. BriefComments can
123-
// just be an array of ParagraphComments.
124-
Child.insert({"Children", ChildArr});
125-
Obj.insert({commentKindToString(I.Kind), ChildVal});
126+
auto TextCommentsArray = extractTextComments(CARef.front().getAsObject());
126127
if (I.Name == "brief")
127-
insertComment(Description, ChildVal, "BriefComments");
128+
insertComment(Description, TextCommentsArray, "BriefComments");
128129
return Obj;
129130
}
130131

@@ -201,8 +202,8 @@ static Object serializeComment(const CommentInfo &I, Object &Description) {
201202
case CommentKind::CK_FullComment:
202203
case CommentKind::CK_ParagraphComment: {
203204
Child.insert({"Children", ChildArr});
204-
Obj.insert({commentKindToString(I.Kind), ChildVal});
205-
return Obj;
205+
Child["ParagraphComment"] = true;
206+
return Child;
206207
}
207208

208209
case CommentKind::CK_Unknown: {
@@ -239,9 +240,11 @@ serializeCommonAttributes(const Info &I, json::Object &Obj,
239240
json::Value Comment = serializeComment(*CommentInfo, Description);
240241
// if a ParagraphComment is returned, then it is a top-level comment that
241242
// needs to be inserted manually.
242-
if (auto *ParagraphComment =
243-
Comment.getAsObject()->get("ParagraphComment"))
244-
insertComment(Description, *ParagraphComment, "ParagraphComments");
243+
if (auto *ParagraphComment = Comment.getAsObject();
244+
ParagraphComment->get("ParagraphComment")) {
245+
auto TextCommentsArray = extractTextComments(ParagraphComment);
246+
insertComment(Description, TextCommentsArray, "ParagraphComments");
247+
}
245248
}
246249
Obj["Description"] = std::move(Description);
247250
}

clang-tools-extra/clang-doc/assets/comment-template.mustache

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
This file defines templates for generating comments
77
}}
88
{{#BriefComments}}
9-
{{#Children}}
10-
{{>Comments}}
11-
{{/Children}}
9+
<div>
10+
{{#.}}
11+
<p>{{TextComment}}</p>
12+
{{/.}}
13+
</div>
1214
{{/BriefComments}}
1315
{{#ParagraphComments}}
14-
{{#Children}}
15-
{{>Comments}}
16-
{{/Children}}
16+
<div>
17+
{{#.}}
18+
<p>{{TextComment}}</p>
19+
{{/.}}
20+
</div>
1721
{{/ParagraphComments}}
1822
{{#ParagraphComment}}
1923
{{#Children}}

clang-tools-extra/test/clang-doc/json/class.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,22 @@ struct MyClass {
3535
// CHECK: {
3636
// CHECK-NEXT: "Description": {
3737
// CHECK-NEXT: "BriefComments": [
38-
// CHECK-NEXT: {
39-
// CHECK-NEXT: "Children": [
40-
// CHECK-NEXT: {
41-
// CHECK-NEXT: "ParagraphComment": {
42-
// CHECK-NEXT: "Children": [
43-
// CHECK-NEXT: {
44-
// CHECK-NEXT: "TextComment": " This is a brief description."
45-
// CHECK: "Command": "brief"
38+
// CHECK-NEXT: [
39+
// CHECK-NEXT: {
40+
// CHECK-NEXT: "TextComment": " This is a brief description."
4641
// CHECK: "HasBriefComments": true,
4742
// CHECK-NEXT: "HasParagraphComments": true,
4843
// CHECK-NEXT: "ParagraphComments": [
49-
// CHECK-NEXT: {
50-
// CHECK-NEXT: "Children": [
51-
// CHECK-NEXT: {
52-
// CHECK-NEXT: "TextComment": " This is a nice class."
53-
// CHECK-NEXT: },
54-
// CHECK-NEXT: {
55-
// CHECK-NEXT: "TextComment": " It has some nice methods and fields."
56-
// CHECK-NEXT: },
57-
// CHECK-NEXT: {
58-
// CHECK-NEXT: "TextComment": ""
59-
// CHECK-NEXT: }
44+
// CHECK-NEXT: [
45+
// CHECK-NEXT: {
46+
// CHECK-NEXT: "TextComment": " This is a nice class."
47+
// CHECK-NEXT: },
48+
// CHECK-NEXT: {
49+
// CHECK-NEXT: "TextComment": " It has some nice methods and fields."
50+
// CHECK-NEXT: },
51+
// CHECK-NEXT: {
52+
// CHECK-NEXT: "TextComment": ""
53+
// CHECK-NEXT: }
6054
// CHECK: "DocumentationFileName": "_ZTV7MyClass",
6155
// CHECK: "Enums": [
6256
// CHECK-NEXT: {

clang-tools-extra/test/clang-doc/json/concept.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ concept Incrementable = requires(T x) {
1616
// CHECK-NEXT: "Description": {
1717
// CHECK-NEXT: "HasParagraphComments": true,
1818
// CHECK-NEXT: "ParagraphComments": [
19-
// CHECK-NEXT: {
20-
// CHECK-NEXT: "Children": [
21-
// CHECK-NEXT: {
22-
// CHECK-NEXT: "TextComment": " Requires that T suports post and pre-incrementing."
19+
// CHECK-NEXT: [
20+
// CHECK-NEXT: {
21+
// CHECK-NEXT: "TextComment": " Requires that T suports post and pre-incrementing."
2322
// CHECK: "End": true,
2423
// CHECK-NEXT: "InfoType": "concept",
2524
// CHECK-NEXT: "IsType": true,

0 commit comments

Comments
 (0)