Skip to content

Commit 3dbfb45

Browse files
committed
[clang-doc] add code comments to comment template
Serializes Doxygen code comments in HTML templates. Modifies the basic-project to add a code example.
1 parent 69317a3 commit 3dbfb45

File tree

5 files changed

+48
-16
lines changed

5 files changed

+48
-16
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ static json::Value extractTextComments(Object *ParagraphComment) {
103103
return *ParagraphComment->get("Children");
104104
}
105105

106+
static json::Value extractVerbatimComments(json::Array VerbatimLines) {
107+
json::Value TextArray = json::Array();
108+
auto &TextArrayRef = *TextArray.getAsArray();
109+
for (auto &Line : VerbatimLines)
110+
TextArrayRef.push_back(*Line.getAsObject()
111+
->get("VerbatimBlockLineComment")
112+
->getAsObject()
113+
->get("Text"));
114+
115+
return TextArray;
116+
}
117+
106118
static Object serializeComment(const CommentInfo &I, Object &Description) {
107119
// taken from PR #142273
108120
Object Obj = Object();
@@ -157,13 +169,11 @@ static Object serializeComment(const CommentInfo &I, Object &Description) {
157169
}
158170

159171
case CommentKind::CK_VerbatimBlockComment: {
160-
Child.insert({"Text", I.Text});
161-
if (!I.CloseName.empty())
162-
Child.insert({"CloseName", I.CloseName});
163-
Child.insert({"Children", ChildArr});
164-
if (I.CloseName == "endcode")
165-
insertComment(Description, ChildVal, "CodeComments");
166-
else if (I.CloseName == "endverbatim")
172+
if (I.CloseName == "endcode") {
173+
// We don't support \code language specification
174+
auto TextCommentsArray = extractVerbatimComments(CARef);
175+
insertComment(Description, TextCommentsArray, "CodeComments");
176+
} else if (I.CloseName == "endverbatim")
167177
insertComment(Description, ChildVal, "VerbatimComments");
168178
return Obj;
169179
}

clang-tools-extra/clang-doc/assets/clang-doc-mustache.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,7 @@ a, a:visited, a:hover, a:active {
469469
text-decoration: none;
470470
color: inherit;
471471
}
472+
473+
.code-block {
474+
white-space: pre-line;
475+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@
4040
{{/.}}
4141
{{/ReturnComments}}
4242
{{/HasReturnComments}}
43+
{{#HasCodeComments}}
44+
<h3>Code</h3>
45+
{{#CodeComments}}
46+
<div>
47+
<pre class="code-block">
48+
<code>
49+
{{#.}}
50+
{{.}}
51+
{{/.}}
52+
</code>
53+
</pre>
54+
</div>
55+
{{/CodeComments}}
56+
{{/HasCodeComments}}
4357
{{#BlockCommandComment}}
4458
<div class="block-command-comment__command">
4559
<div class="block-command-command">

clang-tools-extra/test/clang-doc/Inputs/basic-project/include/Circle.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class Circle : public Shape {
2626
/**
2727
* @brief Calculates the perimeter of the circle.
2828
*
29+
* @code
30+
* Circle circle(5.0);
31+
* double perimeter = circle.perimeter();
32+
* @endcode
2933
* @return double The perimeter of the circle.
3034
*/
3135
double perimeter() const override;

clang-tools-extra/test/clang-doc/basic-project.mustache.test

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -729,15 +729,15 @@ HTML-CIRCLE: <p></p>
729729
HTML-CIRCLE: </div>
730730
HTML-CIRCLE: <h3>Returns</h3>
731731
HTML-CIRCLE: <p> double The perimeter of the circle.</p>
732-
HTML-CIRCLE-NOT: <h3>Code</h3>
733-
HTML-CIRCLE-NOT: <div>
734-
HTML-CIRCLE-NOT: <pre class="code-block">
735-
HTML-CIRCLE-NOT: <code>
736-
HTML-CIRCLE-NOT: Circle circle(5.0);
737-
HTML-CIRCLE-NOT: double perimeter = circle.perimeter();
738-
HTML-CIRCLE-NOT: </code>
739-
HTML-CIRCLE-NOT: </pre>
740-
HTML-CIRCLE-NOT: </div>
732+
HTML-CIRCLE: <h3>Code</h3>
733+
HTML-CIRCLE: <div>
734+
HTML-CIRCLE: <pre class="code-block">
735+
HTML-CIRCLE: <code>
736+
HTML-CIRCLE: Circle circle(5.0);
737+
HTML-CIRCLE: double perimeter = circle.perimeter();
738+
HTML-CIRCLE: </code>
739+
HTML-CIRCLE: </pre>
740+
HTML-CIRCLE: </div>
741741
HTML-CIRCLE: </div>
742742
HTML-CIRCLE: </div>
743743
HTML-CIRCLE: </div>

0 commit comments

Comments
 (0)