Skip to content

[clang-doc] remove nesting of text comments inside paragraphs #150451

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 1 commit into from
Jul 25, 2025

Conversation

evelez7
Copy link
Member

@evelez7 evelez7 commented Jul 24, 2025

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.

@llvmbot
Copy link
Member

llvmbot commented Jul 24, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Erick Velez (evelez7)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/150451.diff

4 Files Affected:

  • (modified) clang-tools-extra/clang-doc/JSONGenerator.cpp (+15-12)
  • (modified) clang-tools-extra/clang-doc/assets/comment-template.mustache (+10-6)
  • (modified) clang-tools-extra/test/clang-doc/json/class.cpp (+13-19)
  • (modified) clang-tools-extra/test/clang-doc/json/concept.cpp (+3-4)
diff --git a/clang-tools-extra/clang-doc/JSONGenerator.cpp b/clang-tools-extra/clang-doc/JSONGenerator.cpp
index 75302b0dc3264..92a4117c4e534 100644
--- a/clang-tools-extra/clang-doc/JSONGenerator.cpp
+++ b/clang-tools-extra/clang-doc/JSONGenerator.cpp
@@ -97,6 +97,12 @@ static void insertComment(Object &Description, json::Value &Comment,
   }
 }
 
+static json::Value extractTextComments(Object *ParagraphComment) {
+  if (!ParagraphComment)
+    return json::Object();
+  return *ParagraphComment->get("Children");
+}
+
 static Object serializeComment(const CommentInfo &I, Object &Description) {
   // taken from PR #142273
   Object Obj = Object();
@@ -117,14 +123,9 @@ static Object serializeComment(const CommentInfo &I, Object &Description) {
   }
 
   case CommentKind::CK_BlockCommandComment: {
-    Child.insert({"Command", I.Name});
-    // TODO: The "Children" level of nesting isn't needed for comments that
-    // don't hold additional information at the top level. BriefComments can
-    // just be an array of ParagraphComments.
-    Child.insert({"Children", ChildArr});
-    Obj.insert({commentKindToString(I.Kind), ChildVal});
+    auto TextCommentsArray = extractTextComments(CARef.front().getAsObject());
     if (I.Name == "brief")
-      insertComment(Description, ChildVal, "BriefComments");
+      insertComment(Description, TextCommentsArray, "BriefComments");
     return Obj;
   }
 
@@ -201,8 +202,8 @@ static Object serializeComment(const CommentInfo &I, Object &Description) {
   case CommentKind::CK_FullComment:
   case CommentKind::CK_ParagraphComment: {
     Child.insert({"Children", ChildArr});
-    Obj.insert({commentKindToString(I.Kind), ChildVal});
-    return Obj;
+    Child["ParagraphComment"] = true;
+    return Child;
   }
 
   case CommentKind::CK_Unknown: {
@@ -239,9 +240,11 @@ serializeCommonAttributes(const Info &I, json::Object &Obj,
       json::Value Comment = serializeComment(*CommentInfo, Description);
       // if a ParagraphComment is returned, then it is a top-level comment that
       // needs to be inserted manually.
-      if (auto *ParagraphComment =
-              Comment.getAsObject()->get("ParagraphComment"))
-        insertComment(Description, *ParagraphComment, "ParagraphComments");
+      if (auto *ParagraphComment = Comment.getAsObject();
+          ParagraphComment->get("ParagraphComment")) {
+        auto TextCommentsArray = extractTextComments(ParagraphComment);
+        insertComment(Description, TextCommentsArray, "ParagraphComments");
+      }
     }
     Obj["Description"] = std::move(Description);
   }
diff --git a/clang-tools-extra/clang-doc/assets/comment-template.mustache b/clang-tools-extra/clang-doc/assets/comment-template.mustache
index b793bad55cf6c..f2edb1b2eb9ac 100644
--- a/clang-tools-extra/clang-doc/assets/comment-template.mustache
+++ b/clang-tools-extra/clang-doc/assets/comment-template.mustache
@@ -6,14 +6,18 @@
     This file defines templates for generating comments
 }}
 {{#BriefComments}}
-    {{#Children}}
-    {{>Comments}}
-    {{/Children}}
+    <div>
+    {{#.}}
+        <p>{{TextComment}}</p>
+    {{/.}}
+    </div>
 {{/BriefComments}}
 {{#ParagraphComments}}
-    {{#Children}}
-    {{>Comments}}
-    {{/Children}}
+    <div>
+    {{#.}}
+        <p>{{TextComment}}</p>
+    {{/.}}
+    </div>
 {{/ParagraphComments}}
 {{#ParagraphComment}}
     {{#Children}}
diff --git a/clang-tools-extra/test/clang-doc/json/class.cpp b/clang-tools-extra/test/clang-doc/json/class.cpp
index e8fafca28a956..79b8fed0a0188 100644
--- a/clang-tools-extra/test/clang-doc/json/class.cpp
+++ b/clang-tools-extra/test/clang-doc/json/class.cpp
@@ -35,28 +35,22 @@ struct MyClass {
 // CHECK:       {
 // CHECK-NEXT:    "Description": {
 // CHECK-NEXT:      "BriefComments": [
-// CHECK-NEXT:        {
-// CHECK-NEXT:          "Children": [
-// CHECK-NEXT:            {
-// CHECK-NEXT:              "ParagraphComment": {
-// CHECK-NEXT:                "Children": [
-// CHECK-NEXT:                  {
-// CHECK-NEXT:                    "TextComment": " This is a brief description."
-// CHECK:               "Command": "brief"
+// CHECK-NEXT:        [
+// CHECK-NEXT:          {
+// CHECK-NEXT:            "TextComment": " This is a brief description."
 // CHECK:           "HasBriefComments": true,
 // CHECK-NEXT:      "HasParagraphComments": true,
 // CHECK-NEXT:      "ParagraphComments": [
-// CHECK-NEXT:        {
-// CHECK-NEXT:          "Children": [
-// CHECK-NEXT:            {
-// CHECK-NEXT:              "TextComment": " This is a nice class."
-// CHECK-NEXT:            },
-// CHECK-NEXT:            {
-// CHECK-NEXT:              "TextComment": " It has some nice methods and fields."
-// CHECK-NEXT:            },
-// CHECK-NEXT:            {
-// CHECK-NEXT:              "TextComment": ""
-// CHECK-NEXT:            }
+// CHECK-NEXT:        [
+// CHECK-NEXT:          {
+// CHECK-NEXT:            "TextComment": " This is a nice class."
+// CHECK-NEXT:          },
+// CHECK-NEXT:          {
+// CHECK-NEXT:            "TextComment": " It has some nice methods and fields."
+// CHECK-NEXT:          },
+// CHECK-NEXT:          {
+// CHECK-NEXT:            "TextComment": ""
+// CHECK-NEXT:          }
 // CHECK:         "DocumentationFileName": "_ZTV7MyClass",
 // CHECK:         "Enums": [
 // CHECK-NEXT:      {
diff --git a/clang-tools-extra/test/clang-doc/json/concept.cpp b/clang-tools-extra/test/clang-doc/json/concept.cpp
index 2874caf28f8f5..4c810244ca41b 100644
--- a/clang-tools-extra/test/clang-doc/json/concept.cpp
+++ b/clang-tools-extra/test/clang-doc/json/concept.cpp
@@ -16,10 +16,9 @@ concept Incrementable = requires(T x) {
 // CHECK-NEXT:        "Description": {
 // CHECK-NEXT:        "HasParagraphComments": true,
 // CHECK-NEXT:        "ParagraphComments": [
-// CHECK-NEXT:          {
-// CHECK-NEXT:            "Children": [
-// CHECK-NEXT:              {
-// CHECK-NEXT:                "TextComment": " Requires that T suports post and pre-incrementing."
+// CHECK-NEXT:          [
+// CHECK-NEXT:            {
+// CHECK-NEXT:              "TextComment": " Requires that T suports post and pre-incrementing."
 // CHECK:             "End": true,
 // CHECK-NEXT:        "InfoType": "concept",
 // CHECK-NEXT:        "IsType": true,

@evelez7 evelez7 force-pushed the users/evelez7/clang-doc-class-comments branch from 1768c96 to fb80d5d Compare July 25, 2025 01:38
@evelez7 evelez7 force-pushed the users/evelez7/clang-doc-remove-paragraph-children branch from ff9bd90 to f032126 Compare July 25, 2025 01:38
Base automatically changed from users/evelez7/clang-doc-class-comments to main July 25, 2025 02:16
@evelez7 evelez7 force-pushed the users/evelez7/clang-doc-remove-paragraph-children branch 2 times, most recently from cab8494 to 1108620 Compare July 25, 2025 02:37
Copy link
Member Author

evelez7 commented Jul 25, 2025

Merge activity

  • Jul 25, 2:38 AM UTC: Graphite rebased this pull request as part of a merge.
  • Jul 25, 2:41 AM UTC: Graphite rebased this pull request as part of a merge.
  • Jul 25, 2:49 AM UTC: @evelez7 merged this pull request with Graphite.

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.
@evelez7 evelez7 force-pushed the users/evelez7/clang-doc-remove-paragraph-children branch from 1108620 to 919f294 Compare July 25, 2025 02:41
@evelez7 evelez7 merged commit 2ca8cf9 into main Jul 25, 2025
9 checks passed
@evelez7 evelez7 deleted the users/evelez7/clang-doc-remove-paragraph-children branch July 25, 2025 02:49
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
…50451)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants