Skip to content

[clang-doc] add code comments to comment template #150648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions clang-tools-extra/clang-doc/JSONGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ static json::Value extractTextComments(Object *ParagraphComment) {
return *ParagraphComment->get("Children");
}

static json::Value extractVerbatimComments(json::Array VerbatimLines) {
json::Value TextArray = json::Array();
auto &TextArrayRef = *TextArray.getAsArray();
for (auto &Line : VerbatimLines)
TextArrayRef.push_back(*Line.getAsObject()
->get("VerbatimBlockLineComment")
->getAsObject()
->get("Text"));

return TextArray;
}

static Object serializeComment(const CommentInfo &I, Object &Description) {
// taken from PR #142273
Object Obj = Object();
Expand Down Expand Up @@ -157,13 +169,11 @@ static Object serializeComment(const CommentInfo &I, Object &Description) {
}

case CommentKind::CK_VerbatimBlockComment: {
Child.insert({"Text", I.Text});
if (!I.CloseName.empty())
Child.insert({"CloseName", I.CloseName});
Child.insert({"Children", ChildArr});
if (I.CloseName == "endcode")
insertComment(Description, ChildVal, "CodeComments");
else if (I.CloseName == "endverbatim")
if (I.CloseName == "endcode") {
// We don't support \code language specification
auto TextCommentsArray = extractVerbatimComments(CARef);
insertComment(Description, TextCommentsArray, "CodeComments");
} else if (I.CloseName == "endverbatim")
insertComment(Description, ChildVal, "VerbatimComments");
return Obj;
}
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/clang-doc/assets/clang-doc-mustache.css
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,7 @@ a, a:visited, a:hover, a:active {
text-decoration: none;
color: inherit;
}

.code-block {
white-space: pre-line;
}
14 changes: 14 additions & 0 deletions clang-tools-extra/clang-doc/assets/comment-template.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@
{{/.}}
{{/ReturnComments}}
{{/HasReturnComments}}
{{#HasCodeComments}}
<h3>Code</h3>
{{#CodeComments}}
<div>
<pre class="code-block">
<code>
{{#.}}
{{.}}
{{/.}}
</code>
</pre>
</div>
{{/CodeComments}}
{{/HasCodeComments}}
{{#BlockCommandComment}}
<div class="block-command-comment__command">
<div class="block-command-command">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class Circle : public Shape {
/**
* @brief Calculates the perimeter of the circle.
*
* @code
* Circle circle(5.0);
* double perimeter = circle.perimeter();
* @endcode
* @return double The perimeter of the circle.
*/
double perimeter() const override;
Expand Down
9 changes: 9 additions & 0 deletions clang-tools-extra/test/clang-doc/basic-project.mustache.test
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,15 @@ HTML-CIRCLE: <p></p>
HTML-CIRCLE: </div>
HTML-CIRCLE: <h3>Returns</h3>
HTML-CIRCLE: <p> double The perimeter of the circle.</p>
HTML-CIRCLE: <h3>Code</h3>
HTML-CIRCLE: <div>
HTML-CIRCLE: <pre class="code-block">
HTML-CIRCLE: <code>
HTML-CIRCLE: Circle circle(5.0);
HTML-CIRCLE: double perimeter = circle.perimeter();
HTML-CIRCLE: </code>
HTML-CIRCLE: </pre>
HTML-CIRCLE: </div>
HTML-CIRCLE: </div>
HTML-CIRCLE: </div>
HTML-CIRCLE: </div>
Expand Down