[clang-tools-extra] Refactor clang doc comment structure (PR #142273)
Paul Kirth via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 5 18:58:08 PDT 2025
================
@@ -208,37 +208,107 @@ static json::Value extractValue(const TypedefInfo &I) {
}
static json::Value extractValue(const CommentInfo &I) {
- assert((I.Kind == "BlockCommandComment" || I.Kind == "FullComment" ||
- I.Kind == "ParagraphComment" || I.Kind == "TextComment") &&
- "Unknown Comment type in CommentInfo.");
-
Object Obj = Object();
- json::Value Child = Object();
- // TextComment has no children, so return it.
- if (I.Kind == "TextComment") {
- Obj.insert({"TextComment", I.Text});
- return Obj;
- }
+ json::Value ChildVal = Object();
+ Object &Child = *ChildVal.getAsObject();
- // BlockCommandComment needs to generate a Command key.
- if (I.Kind == "BlockCommandComment")
- Child.getAsObject()->insert({"Command", I.Name});
-
- // Use the same handling for everything else.
- // Only valid for:
- // - BlockCommandComment
- // - FullComment
- // - ParagraphComment
json::Value ChildArr = Array();
auto &CARef = *ChildArr.getAsArray();
CARef.reserve(I.Children.size());
for (const auto &C : I.Children)
CARef.emplace_back(extractValue(*C));
- Child.getAsObject()->insert({"Children", ChildArr});
- Obj.insert({I.Kind, Child});
- return Obj;
+ switch (I.Kind) {
+ case CommentKind::CK_TextComment: {
+ Obj.insert({commentKindToString(I.Kind), I.Text});
+ return Obj;
+ }
+
+ case CommentKind::CK_BlockCommandComment: {
+ Child.insert({"Command", I.Name});
+ Child.insert({"Children", ChildArr});
+ Obj.insert({commentKindToString(I.Kind), ChildVal});
+ return Obj;
+ }
+
+ case CommentKind::CK_InlineCommandComment: {
+ json::Value ArgsArr = Array();
+ for (const auto &Arg : I.Args)
+ ArgsArr.getAsArray()->emplace_back(Arg);
+
+ Child.insert({"Command", I.Name});
+ Child.insert({"Args", ArgsArr});
+ Child.insert({"Children", ChildArr});
+ Obj.insert({commentKindToString(I.Kind), ChildVal});
+ return Obj;
+ }
+
+ case CommentKind::CK_ParamCommandComment:
+ case CommentKind::CK_TParamCommandComment: {
+ Child.insert({"ParamName", I.ParamName});
+ Child.insert({"Direction", I.Direction});
+ Child.insert({"Explicit", I.Explicit});
+ Child.insert({"Children", ChildArr});
+ Obj.insert({commentKindToString(I.Kind), ChildVal});
+ return Obj;
+ }
+
+ case CommentKind::CK_VerbatimBlockComment: {
+ Child.insert({"Text", I.Text});
+ if (!I.CloseName.empty())
+ Child.insert({"CloseName", I.CloseName});
+ Child.insert({"Children", ChildArr});
+ Obj.insert({commentKindToString(I.Kind), ChildVal});
+ return Obj;
+ }
+
+ case CommentKind::CK_VerbatimBlockLineComment:
+ case CommentKind::CK_VerbatimLineComment: {
+ Child.insert({"Text", I.Text});
+ Child.insert({"Children", ChildArr});
+ Obj.insert({commentKindToString(I.Kind), ChildVal});
+ return Obj;
+ }
+
+ case CommentKind::CK_HTMLStartTagComment: {
+ json::Value AttrKeysArray = json::Array();
+ for (const auto &Key : I.AttrKeys)
+ AttrKeysArray.getAsArray()->emplace_back(Key);
+
+ json::Value AttrValuesArray = json::Array();
+ for (const auto &Val : I.AttrValues)
+ AttrValuesArray.getAsArray()->emplace_back(Val);
----------------
ilovepi wrote:
for these, can we use something from `<algorithm>` or ADT to copy? If not, then we should at least reserve the size so we don't pay for realloc + copy operations on resize. `json::Array` is a thin wrapper over std::vector, so it has all the same limitations.
I think there's a couple of other loops in this patch that can make the same optimization.
https://github.com/llvm/llvm-project/pull/142273
More information about the cfe-commits
mailing list