llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

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

Author: Samrudh Nelli (SamrudhNelli)

<details>
<summary>Changes</summary>

Currently comments of enum variables are not displayed in both HTML and MD.
Add support to display the comments.
Also support displaying the value of an enum member in MD. 

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


3 Files Affected:

- (modified) clang-tools-extra/clang-doc/JSONGenerator.cpp (+16) 
- (modified) clang-tools-extra/clang-doc/MDGenerator.cpp (+28-2) 
- (modified) clang-tools-extra/clang-doc/assets/enum-template.mustache (+2) 


``````````diff
diff --git a/clang-tools-extra/clang-doc/JSONGenerator.cpp 
b/clang-tools-extra/clang-doc/JSONGenerator.cpp
index 5051e7e6e690d..65a9a6bb94e04 100644
--- a/clang-tools-extra/clang-doc/JSONGenerator.cpp
+++ b/clang-tools-extra/clang-doc/JSONGenerator.cpp
@@ -554,6 +554,22 @@ static void serializeInfo(const EnumValueInfo &I, Object 
&Obj) {
     Obj["ValueExpr"] = I.ValueExpr;
   else
     Obj["Value"] = I.Value;
+  if (!I.Description.empty()) {
+    json::Value CommentsArray = Array();
+    auto &CommentsArrayRef = *CommentsArray.getAsArray();
+    Object TempObj, ChildJson;
+    for (const auto &Child : I.Description) {
+      for (const auto &CI : Child.Children) {
+        ChildJson = serializeComment(*CI, TempObj);
+        if (!ChildJson.empty()) {
+          CommentsArrayRef.push_back(std::move(ChildJson));
+        }
+      }
+    }
+    if (!CommentsArrayRef.empty()) {
+      Obj["Description"] = CommentsArray;
+    }
+  }
 }
 
 static void serializeInfo(const EnumInfo &I, json::Object &Obj,
diff --git a/clang-tools-extra/clang-doc/MDGenerator.cpp 
b/clang-tools-extra/clang-doc/MDGenerator.cpp
index fcb75af80f9e9..7c01ae820b286 100644
--- a/clang-tools-extra/clang-doc/MDGenerator.cpp
+++ b/clang-tools-extra/clang-doc/MDGenerator.cpp
@@ -67,6 +67,23 @@ static void writeSourceFileRef(const ClangDocContext &CDCtx, 
const Location &L,
   OS << "\n\n";
 }
 
+static std::string genRawText(const std::vector<CommentInfo> &Comments) {
+  std::string Result;
+  std::queue<const CommentInfo *> Q;
+  for (const auto &CI : Comments)
+    Q.push(&CI);
+  const CommentInfo *Comment;
+  while (Q.size()) {
+    Comment = Q.front();
+    Q.pop();
+    if (!Comment->Text.empty())
+      Result += Comment->Text;
+    for (const auto &CI : Comment->Children)
+      Q.push(CI.get());
+  }
+  return Result;
+}
+
 static void maybeWriteSourceFileRef(llvm::raw_ostream &OS,
                                     const ClangDocContext &CDCtx,
                                     const std::optional<Location> &DefLoc) {
@@ -162,11 +179,20 @@ static void genMarkdown(const ClangDocContext &CDCtx, 
const EnumInfo &I,
     writeLine("| enum " + I.Name + " |", OS);
   writeLine("--", OS);
 
+  OS << "| Name | Value | Comments |\n\n";
   std::string Buffer;
   llvm::raw_string_ostream Members(Buffer);
   if (!I.Members.empty())
-    for (const auto &N : I.Members)
-      Members << "| " << N.Name << " |\n";
+    for (const auto &N : I.Members) {
+      Members << "| " << N.Name << " ";
+      if (!N.Value.empty())
+        Members << "| " << N.Value << " ";
+      std::string RawComment = genRawText(N.Description);
+      RawComment.erase(0, RawComment.find_first_not_of(" \t\r\n"));
+      RawComment.erase(RawComment.find_last_not_of(" \t\r\n") + 1);
+      Members << "| " << (RawComment.empty() ? "--" : RawComment) << " ";
+      Members << "|\n";
+    }
   writeLine(Members.str(), OS);
 
   maybeWriteSourceFileRef(OS, CDCtx, I.DefLoc);
diff --git a/clang-tools-extra/clang-doc/assets/enum-template.mustache 
b/clang-tools-extra/clang-doc/assets/enum-template.mustache
index 7434b7bfce347..783aaf9d2193f 100644
--- a/clang-tools-extra/clang-doc/assets/enum-template.mustache
+++ b/clang-tools-extra/clang-doc/assets/enum-template.mustache
@@ -15,6 +15,7 @@
             <tr>
                 <th>Name</th>
                 <th>Value</th>
+                <th>Comments</th>
             </tr>
             {{#Members}}
             <tr>
@@ -26,6 +27,7 @@
                 {{^Value}}
                 <td>{{ValueExpr}}</td>
                 {{/Value}}
+                
<td>{{#Description}}{{>Comments}}{{/Description}}{{^Description}} -- 
{{/Description}}</td>
             </tr>
             {{/Members}}
         </tbody>

``````````

</details>


https://github.com/llvm/llvm-project/pull/183085
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to