Skip to content

Commit 231e422

Browse files
committed
Add support for different CommentKind types in HTMLMustacheGenerator
Change existing handling from if/else to switch-case for better clarity and extensibility.
1 parent db2573d commit 231e422

File tree

1 file changed

+92
-24
lines changed

1 file changed

+92
-24
lines changed

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

Lines changed: 92 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -198,39 +198,107 @@ static json::Value extractValue(const TypedefInfo &I) {
198198
}
199199

200200
static json::Value extractValue(const CommentInfo &I) {
201-
assert((I.Kind == CommentKind::CK_BlockCommandComment ||
202-
I.Kind == CommentKind::CK_FullComment ||
203-
I.Kind == CommentKind::CK_ParagraphComment ||
204-
I.Kind == CommentKind::CK_TextComment) &&
205-
"Unknown Comment type in CommentInfo.");
206-
207201
Object Obj = Object();
208202
json::Value Child = Object();
209203

210-
// TextComment has no children, so return it.
211-
if (I.Kind == CommentKind::CK_TextComment) {
212-
Obj.insert({"TextComment", I.Text});
204+
json::Value ChildArr = Array();
205+
auto &CARef = *ChildArr.getAsArray();
206+
CARef.reserve(I.Children.size());
207+
for (const auto &C : I.Children) {
208+
CARef.emplace_back(extractValue(*C));
209+
}
210+
211+
switch (I.Kind) {
212+
case CommentKind::CK_TextComment: {
213+
Obj.insert({commentKindToString(I.Kind), I.Text});
213214
return Obj;
214215
}
215216

216-
// BlockCommandComment needs to generate a Command key.
217-
if (I.Kind == CommentKind::CK_BlockCommandComment)
217+
case CommentKind::CK_BlockCommandComment: {
218218
Child.getAsObject()->insert({"Command", I.Name});
219+
Child.getAsObject()->insert({"Children", ChildArr});
220+
Obj.insert({commentKindToString(I.Kind), Child});
221+
return Obj;
222+
}
219223

220-
// Use the same handling for everything else.
221-
// Only valid for:
222-
// - BlockCommandComment
223-
// - FullComment
224-
// - ParagraphComment
225-
json::Value ChildArr = Array();
226-
auto &CARef = *ChildArr.getAsArray();
227-
CARef.reserve(I.Children.size());
228-
for (const auto &C : I.Children)
229-
CARef.emplace_back(extractValue(*C));
230-
Child.getAsObject()->insert({"Children", ChildArr});
231-
Obj.insert({commentKindToString(I.Kind), Child});
224+
case CommentKind::CK_InlineCommandComment: {
225+
json::Value ArgsArr = Array();
226+
for (const auto &Arg : I.Args) {
227+
ArgsArr.getAsArray()->emplace_back(Arg);
228+
}
229+
Child.getAsObject()->insert({"Command", I.Name});
230+
Child.getAsObject()->insert({"Args", ArgsArr});
231+
Child.getAsObject()->insert({"Children", ChildArr});
232+
Obj.insert({commentKindToString(I.Kind), Child});
233+
return Obj;
234+
}
232235

233-
return Obj;
236+
case CommentKind::CK_ParamCommandComment:
237+
case CommentKind::CK_TParamCommandComment: {
238+
Child.getAsObject()->insert({"ParamName", I.ParamName});
239+
Child.getAsObject()->insert({"Direction", I.Direction});
240+
Child.getAsObject()->insert({"Explicit", I.Explicit});
241+
Child.getAsObject()->insert({"Children", ChildArr});
242+
Obj.insert({commentKindToString(I.Kind), Child});
243+
return Obj;
244+
}
245+
246+
case CommentKind::CK_VerbatimBlockComment: {
247+
Child.getAsObject()->insert({"Text", I.Text});
248+
Child.getAsObject()->insert({"Children", ChildArr});
249+
if (!I.CloseName.empty())
250+
Child.getAsObject()->insert({"CloseName", I.CloseName});
251+
Obj.insert({commentKindToString(I.Kind), Child});
252+
return Obj;
253+
}
254+
255+
case CommentKind::CK_VerbatimBlockLineComment:
256+
case CommentKind::CK_VerbatimLineComment: {
257+
Child.getAsObject()->insert({"Text", I.Text});
258+
Child.getAsObject()->insert({"Children", ChildArr});
259+
Obj.insert({commentKindToString(I.Kind), Child});
260+
return Obj;
261+
}
262+
263+
case CommentKind::CK_HTMLStartTagComment: {
264+
json::Value AttrKeysArray = json::Array();
265+
for (const auto &Key : I.AttrKeys)
266+
AttrKeysArray.getAsArray()->emplace_back(Key);
267+
268+
json::Value AttrValuesArray = json::Array();
269+
for (const auto &Val : I.AttrValues)
270+
AttrValuesArray.getAsArray()->emplace_back(Val);
271+
272+
Child.getAsObject()->insert({"Name", I.Name});
273+
Child.getAsObject()->insert({"SelfClosing", I.SelfClosing});
274+
Child.getAsObject()->insert({"AttrKeys", AttrKeysArray});
275+
Child.getAsObject()->insert({"AttrValues", AttrValuesArray});
276+
Child.getAsObject()->insert({"Children", ChildArr});
277+
Obj.insert({commentKindToString(I.Kind), Child});
278+
return Obj;
279+
}
280+
281+
case CommentKind::CK_HTMLEndTagComment: {
282+
Child.getAsObject()->insert({"Name", I.Name});
283+
Child.getAsObject()->insert({"Children", ChildArr});
284+
Obj.insert({commentKindToString(I.Kind), Child});
285+
return Obj;
286+
}
287+
288+
case CommentKind::CK_FullComment:
289+
case CommentKind::CK_ParagraphComment: {
290+
Child.getAsObject()->insert({"Children", ChildArr});
291+
Obj.insert({commentKindToString(I.Kind), Child});
292+
return Obj;
293+
}
294+
295+
case CommentKind::CK_Unknown: {
296+
Obj.insert({commentKindToString(I.Kind), I.Text});
297+
return Obj;
298+
}
299+
300+
llvm_unreachable("Unknown comment kind encountered.");
301+
}
234302
}
235303

236304
static void maybeInsertLocation(std::optional<Location> Loc,

0 commit comments

Comments
 (0)