1 #include "clang/AST/JSONNodeDumper.h"
2 #include "llvm/ADT/StringSwitch.h"
3 
4 using namespace clang;
5 
6 void JSONNodeDumper::addPreviousDeclaration(const Decl *D) {
7   switch (D->getKind()) {
8 #define DECL(DERIVED, BASE)                                                    \
9   case Decl::DERIVED:                                                          \
10     return writePreviousDeclImpl(cast<DERIVED##Decl>(D));
11 #define ABSTRACT_DECL(DECL)
12 #include "clang/AST/DeclNodes.inc"
13 #undef ABSTRACT_DECL
14 #undef DECL
15   }
16   llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
17 }
18 
19 void JSONNodeDumper::Visit(const Attr *A) {
20   const char *AttrName = nullptr;
21   switch (A->getKind()) {
22 #define ATTR(X)                                                                \
23   case attr::X:                                                                \
24     AttrName = #X"Attr";                                                       \
25     break;
26 #include "clang/Basic/AttrList.inc"
27 #undef ATTR
28   }
29   JOS.attribute("id", createPointerRepresentation(A));
30   JOS.attribute("kind", AttrName);
31   JOS.attribute("range", createSourceRange(A->getRange()));
32   attributeOnlyIfTrue("inherited", A->isInherited());
33   attributeOnlyIfTrue("implicit", A->isImplicit());
34 
35   // FIXME: it would be useful for us to output the spelling kind as well as
36   // the actual spelling. This would allow us to distinguish between the
37   // various attribute syntaxes, but we don't currently track that information
38   // within the AST.
39   //JOS.attribute("spelling", A->getSpelling());
40 
41   InnerAttrVisitor::Visit(A);
42 }
43 
44 void JSONNodeDumper::Visit(const Stmt *S) {
45   if (!S)
46     return;
47 
48   JOS.attribute("id", createPointerRepresentation(S));
49   JOS.attribute("kind", S->getStmtClassName());
50   JOS.attribute("range", createSourceRange(S->getSourceRange()));
51 
52   if (const auto *E = dyn_cast<Expr>(S)) {
53     JOS.attribute("type", createQualType(E->getType()));
54     const char *Category = nullptr;
55     switch (E->getValueKind()) {
56     case VK_LValue: Category = "lvalue"; break;
57     case VK_XValue: Category = "xvalue"; break;
58     case VK_RValue: Category = "rvalue"; break;
59     }
60     JOS.attribute("valueCategory", Category);
61   }
62   InnerStmtVisitor::Visit(S);
63 }
64 
65 void JSONNodeDumper::Visit(const Type *T) {
66   JOS.attribute("id", createPointerRepresentation(T));
67   JOS.attribute("kind", (llvm::Twine(T->getTypeClassName()) + "Type").str());
68   JOS.attribute("type", createQualType(QualType(T, 0), /*Desugar*/ false));
69   attributeOnlyIfTrue("isDependent", T->isDependentType());
70   attributeOnlyIfTrue("isInstantiationDependent",
71                       T->isInstantiationDependentType());
72   attributeOnlyIfTrue("isVariablyModified", T->isVariablyModifiedType());
73   attributeOnlyIfTrue("containsUnexpandedPack",
74                       T->containsUnexpandedParameterPack());
75   attributeOnlyIfTrue("isImported", T->isFromAST());
76   InnerTypeVisitor::Visit(T);
77 }
78 
79 void JSONNodeDumper::Visit(QualType T) {
80   JOS.attribute("id", createPointerRepresentation(T.getAsOpaquePtr()));
81   JOS.attribute("type", createQualType(T));
82   JOS.attribute("qualifiers", T.split().Quals.getAsString());
83 }
84 
85 void JSONNodeDumper::Visit(const Decl *D) {
86   JOS.attribute("id", createPointerRepresentation(D));
87 
88   if (!D)
89     return;
90 
91   JOS.attribute("kind", (llvm::Twine(D->getDeclKindName()) + "Decl").str());
92   JOS.attribute("loc", createSourceLocation(D->getLocation()));
93   JOS.attribute("range", createSourceRange(D->getSourceRange()));
94   attributeOnlyIfTrue("isImplicit", D->isImplicit());
95   attributeOnlyIfTrue("isInvalid", D->isInvalidDecl());
96 
97   if (D->isUsed())
98     JOS.attribute("isUsed", true);
99   else if (D->isThisDeclarationReferenced())
100     JOS.attribute("isReferenced", true);
101 
102   if (const auto *ND = dyn_cast<NamedDecl>(D))
103     attributeOnlyIfTrue("isHidden", ND->isHidden());
104 
105   if (D->getLexicalDeclContext() != D->getDeclContext())
106     JOS.attribute("parentDeclContext",
107                   createPointerRepresentation(D->getDeclContext()));
108 
109   addPreviousDeclaration(D);
110   InnerDeclVisitor::Visit(D);
111 }
112 
113 void JSONNodeDumper::Visit(const comments::Comment *C,
114                            const comments::FullComment *FC) {
115   if (!C)
116     return;
117 
118   JOS.attribute("id", createPointerRepresentation(C));
119   JOS.attribute("kind", C->getCommentKindName());
120   JOS.attribute("loc", createSourceLocation(C->getLocation()));
121   JOS.attribute("range", createSourceRange(C->getSourceRange()));
122 
123   InnerCommentVisitor::visit(C, FC);
124 }
125 
126 void JSONNodeDumper::Visit(const TemplateArgument &TA, SourceRange R,
127                            const Decl *From, StringRef Label) {
128   JOS.attribute("kind", "TemplateArgument");
129   if (R.isValid())
130     JOS.attribute("range", createSourceRange(R));
131 
132   if (From)
133     JOS.attribute(Label.empty() ? "fromDecl" : Label, createBareDeclRef(From));
134 
135   InnerTemplateArgVisitor::Visit(TA);
136 }
137 
138 void JSONNodeDumper::Visit(const CXXCtorInitializer *Init) {
139   JOS.attribute("kind", "CXXCtorInitializer");
140   if (Init->isAnyMemberInitializer())
141     JOS.attribute("anyInit", createBareDeclRef(Init->getAnyMember()));
142   else if (Init->isBaseInitializer())
143     JOS.attribute("baseInit",
144                   createQualType(QualType(Init->getBaseClass(), 0)));
145   else if (Init->isDelegatingInitializer())
146     JOS.attribute("delegatingInit",
147                   createQualType(Init->getTypeSourceInfo()->getType()));
148   else
149     llvm_unreachable("Unknown initializer type");
150 }
151 
152 void JSONNodeDumper::Visit(const OMPClause *C) {}
153 
154 void JSONNodeDumper::Visit(const BlockDecl::Capture &C) {
155   JOS.attribute("kind", "Capture");
156   attributeOnlyIfTrue("byref", C.isByRef());
157   attributeOnlyIfTrue("nested", C.isNested());
158   if (C.getVariable())
159     JOS.attribute("var", createBareDeclRef(C.getVariable()));
160 }
161 
162 void JSONNodeDumper::Visit(const GenericSelectionExpr::ConstAssociation &A) {
163   JOS.attribute("associationKind", A.getTypeSourceInfo() ? "case" : "default");
164   attributeOnlyIfTrue("selected", A.isSelected());
165 }
166 
167 llvm::json::Object
168 JSONNodeDumper::createBareSourceLocation(SourceLocation Loc) {
169   PresumedLoc Presumed = SM.getPresumedLoc(Loc);
170 
171   if (Presumed.isInvalid())
172     return llvm::json::Object{};
173 
174   return llvm::json::Object{{"file", Presumed.getFilename()},
175                             {"line", Presumed.getLine()},
176                             {"col", Presumed.getColumn()}};
177 }
178 
179 llvm::json::Object JSONNodeDumper::createSourceLocation(SourceLocation Loc) {
180   SourceLocation Spelling = SM.getSpellingLoc(Loc);
181   SourceLocation Expansion = SM.getExpansionLoc(Loc);
182 
183   llvm::json::Object SLoc = createBareSourceLocation(Spelling);
184   if (Expansion != Spelling) {
185     // If the expansion and the spelling are different, output subobjects
186     // describing both locations.
187     llvm::json::Object ELoc = createBareSourceLocation(Expansion);
188 
189     // If there is a macro expansion, add extra information if the interesting
190     // bit is the macro arg expansion.
191     if (SM.isMacroArgExpansion(Loc))
192       ELoc["isMacroArgExpansion"] = true;
193 
194     return llvm::json::Object{{"spellingLoc", std::move(SLoc)},
195                               {"expansionLoc", std::move(ELoc)}};
196   }
197 
198   return SLoc;
199 }
200 
201 llvm::json::Object JSONNodeDumper::createSourceRange(SourceRange R) {
202   return llvm::json::Object{{"begin", createSourceLocation(R.getBegin())},
203                             {"end", createSourceLocation(R.getEnd())}};
204 }
205 
206 std::string JSONNodeDumper::createPointerRepresentation(const void *Ptr) {
207   // Because JSON stores integer values as signed 64-bit integers, trying to
208   // represent them as such makes for very ugly pointer values in the resulting
209   // output. Instead, we convert the value to hex and treat it as a string.
210   return "0x" + llvm::utohexstr(reinterpret_cast<uint64_t>(Ptr), true);
211 }
212 
213 llvm::json::Object JSONNodeDumper::createQualType(QualType QT, bool Desugar) {
214   SplitQualType SQT = QT.split();
215   llvm::json::Object Ret{{"qualType", QualType::getAsString(SQT, PrintPolicy)}};
216 
217   if (Desugar && !QT.isNull()) {
218     SplitQualType DSQT = QT.getSplitDesugaredType();
219     if (DSQT != SQT)
220       Ret["desugaredQualType"] = QualType::getAsString(DSQT, PrintPolicy);
221   }
222   return Ret;
223 }
224 
225 llvm::json::Object JSONNodeDumper::createBareDeclRef(const Decl *D) {
226   llvm::json::Object Ret{{"id", createPointerRepresentation(D)}};
227   if (!D)
228     return Ret;
229 
230   Ret["kind"] = (llvm::Twine(D->getDeclKindName()) + "Decl").str();
231   if (const auto *ND = dyn_cast<NamedDecl>(D))
232     Ret["name"] = ND->getDeclName().getAsString();
233   if (const auto *VD = dyn_cast<ValueDecl>(D))
234     Ret["type"] = createQualType(VD->getType());
235   return Ret;
236 }
237 
238 llvm::json::Array JSONNodeDumper::createCastPath(const CastExpr *C) {
239   llvm::json::Array Ret;
240   if (C->path_empty())
241     return Ret;
242 
243   for (auto I = C->path_begin(), E = C->path_end(); I != E; ++I) {
244     const CXXBaseSpecifier *Base = *I;
245     const auto *RD =
246         cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
247 
248     llvm::json::Object Val{{"name", RD->getName()}};
249     if (Base->isVirtual())
250       Val["isVirtual"] = true;
251     Ret.push_back(std::move(Val));
252   }
253   return Ret;
254 }
255 
256 #define FIELD2(Name, Flag)  if (RD->Flag()) Ret[Name] = true
257 #define FIELD1(Flag)        FIELD2(#Flag, Flag)
258 
259 static llvm::json::Object
260 createDefaultConstructorDefinitionData(const CXXRecordDecl *RD) {
261   llvm::json::Object Ret;
262 
263   FIELD2("exists", hasDefaultConstructor);
264   FIELD2("trivial", hasTrivialDefaultConstructor);
265   FIELD2("nonTrivial", hasNonTrivialDefaultConstructor);
266   FIELD2("userProvided", hasUserProvidedDefaultConstructor);
267   FIELD2("isConstexpr", hasConstexprDefaultConstructor);
268   FIELD2("needsImplicit", needsImplicitDefaultConstructor);
269   FIELD2("defaultedIsConstexpr", defaultedDefaultConstructorIsConstexpr);
270 
271   return Ret;
272 }
273 
274 static llvm::json::Object
275 createCopyConstructorDefinitionData(const CXXRecordDecl *RD) {
276   llvm::json::Object Ret;
277 
278   FIELD2("simple", hasSimpleCopyConstructor);
279   FIELD2("trivial", hasTrivialCopyConstructor);
280   FIELD2("nonTrivial", hasNonTrivialCopyConstructor);
281   FIELD2("userDeclared", hasUserDeclaredCopyConstructor);
282   FIELD2("hasConstParam", hasCopyConstructorWithConstParam);
283   FIELD2("implicitHasConstParam", implicitCopyConstructorHasConstParam);
284   FIELD2("needsImplicit", needsImplicitCopyConstructor);
285   FIELD2("needsOverloadResolution", needsOverloadResolutionForCopyConstructor);
286   if (!RD->needsOverloadResolutionForCopyConstructor())
287     FIELD2("defaultedIsDeleted", defaultedCopyConstructorIsDeleted);
288 
289   return Ret;
290 }
291 
292 static llvm::json::Object
293 createMoveConstructorDefinitionData(const CXXRecordDecl *RD) {
294   llvm::json::Object Ret;
295 
296   FIELD2("exists", hasMoveConstructor);
297   FIELD2("simple", hasSimpleMoveConstructor);
298   FIELD2("trivial", hasTrivialMoveConstructor);
299   FIELD2("nonTrivial", hasNonTrivialMoveConstructor);
300   FIELD2("userDeclared", hasUserDeclaredMoveConstructor);
301   FIELD2("needsImplicit", needsImplicitMoveConstructor);
302   FIELD2("needsOverloadResolution", needsOverloadResolutionForMoveConstructor);
303   if (!RD->needsOverloadResolutionForMoveConstructor())
304     FIELD2("defaultedIsDeleted", defaultedMoveConstructorIsDeleted);
305 
306   return Ret;
307 }
308 
309 static llvm::json::Object
310 createCopyAssignmentDefinitionData(const CXXRecordDecl *RD) {
311   llvm::json::Object Ret;
312 
313   FIELD2("trivial", hasTrivialCopyAssignment);
314   FIELD2("nonTrivial", hasNonTrivialCopyAssignment);
315   FIELD2("hasConstParam", hasCopyAssignmentWithConstParam);
316   FIELD2("implicitHasConstParam", implicitCopyAssignmentHasConstParam);
317   FIELD2("userDeclared", hasUserDeclaredCopyAssignment);
318   FIELD2("needsImplicit", needsImplicitCopyAssignment);
319   FIELD2("needsOverloadResolution", needsOverloadResolutionForCopyAssignment);
320 
321   return Ret;
322 }
323 
324 static llvm::json::Object
325 createMoveAssignmentDefinitionData(const CXXRecordDecl *RD) {
326   llvm::json::Object Ret;
327 
328   FIELD2("exists", hasMoveAssignment);
329   FIELD2("simple", hasSimpleMoveAssignment);
330   FIELD2("trivial", hasTrivialMoveAssignment);
331   FIELD2("nonTrivial", hasNonTrivialMoveAssignment);
332   FIELD2("userDeclared", hasUserDeclaredMoveAssignment);
333   FIELD2("needsImplicit", needsImplicitMoveAssignment);
334   FIELD2("needsOverloadResolution", needsOverloadResolutionForMoveAssignment);
335 
336   return Ret;
337 }
338 
339 static llvm::json::Object
340 createDestructorDefinitionData(const CXXRecordDecl *RD) {
341   llvm::json::Object Ret;
342 
343   FIELD2("simple", hasSimpleDestructor);
344   FIELD2("irrelevant", hasIrrelevantDestructor);
345   FIELD2("trivial", hasTrivialDestructor);
346   FIELD2("nonTrivial", hasNonTrivialDestructor);
347   FIELD2("userDeclared", hasUserDeclaredDestructor);
348   FIELD2("needsImplicit", needsImplicitDestructor);
349   FIELD2("needsOverloadResolution", needsOverloadResolutionForDestructor);
350   if (!RD->needsOverloadResolutionForDestructor())
351     FIELD2("defaultedIsDeleted", defaultedDestructorIsDeleted);
352 
353   return Ret;
354 }
355 
356 llvm::json::Object
357 JSONNodeDumper::createCXXRecordDefinitionData(const CXXRecordDecl *RD) {
358   llvm::json::Object Ret;
359 
360   // This data is common to all C++ classes.
361   FIELD1(isGenericLambda);
362   FIELD1(isLambda);
363   FIELD1(isEmpty);
364   FIELD1(isAggregate);
365   FIELD1(isStandardLayout);
366   FIELD1(isTriviallyCopyable);
367   FIELD1(isPOD);
368   FIELD1(isTrivial);
369   FIELD1(isPolymorphic);
370   FIELD1(isAbstract);
371   FIELD1(isLiteral);
372   FIELD1(canPassInRegisters);
373   FIELD1(hasUserDeclaredConstructor);
374   FIELD1(hasConstexprNonCopyMoveConstructor);
375   FIELD1(hasMutableFields);
376   FIELD1(hasVariantMembers);
377   FIELD2("canConstDefaultInit", allowConstDefaultInit);
378 
379   Ret["defaultCtor"] = createDefaultConstructorDefinitionData(RD);
380   Ret["copyCtor"] = createCopyConstructorDefinitionData(RD);
381   Ret["moveCtor"] = createMoveConstructorDefinitionData(RD);
382   Ret["copyAssign"] = createCopyAssignmentDefinitionData(RD);
383   Ret["moveAssign"] = createMoveAssignmentDefinitionData(RD);
384   Ret["dtor"] = createDestructorDefinitionData(RD);
385 
386   return Ret;
387 }
388 
389 #undef FIELD1
390 #undef FIELD2
391 
392 std::string JSONNodeDumper::createAccessSpecifier(AccessSpecifier AS) {
393   switch (AS) {
394   case AS_none: return "none";
395   case AS_private: return "private";
396   case AS_protected: return "protected";
397   case AS_public: return "public";
398   }
399   llvm_unreachable("Unknown access specifier");
400 }
401 
402 llvm::json::Object
403 JSONNodeDumper::createCXXBaseSpecifier(const CXXBaseSpecifier &BS) {
404   llvm::json::Object Ret;
405 
406   Ret["type"] = createQualType(BS.getType());
407   Ret["access"] = createAccessSpecifier(BS.getAccessSpecifier());
408   Ret["writtenAccess"] =
409       createAccessSpecifier(BS.getAccessSpecifierAsWritten());
410   if (BS.isVirtual())
411     Ret["isVirtual"] = true;
412   if (BS.isPackExpansion())
413     Ret["isPackExpansion"] = true;
414 
415   return Ret;
416 }
417 
418 void JSONNodeDumper::VisitTypedefType(const TypedefType *TT) {
419   JOS.attribute("decl", createBareDeclRef(TT->getDecl()));
420 }
421 
422 void JSONNodeDumper::VisitFunctionType(const FunctionType *T) {
423   FunctionType::ExtInfo E = T->getExtInfo();
424   attributeOnlyIfTrue("noreturn", E.getNoReturn());
425   attributeOnlyIfTrue("producesResult", E.getProducesResult());
426   if (E.getHasRegParm())
427     JOS.attribute("regParm", E.getRegParm());
428   JOS.attribute("cc", FunctionType::getNameForCallConv(E.getCC()));
429 }
430 
431 void JSONNodeDumper::VisitFunctionProtoType(const FunctionProtoType *T) {
432   FunctionProtoType::ExtProtoInfo E = T->getExtProtoInfo();
433   attributeOnlyIfTrue("trailingReturn", E.HasTrailingReturn);
434   attributeOnlyIfTrue("const", T->isConst());
435   attributeOnlyIfTrue("volatile", T->isVolatile());
436   attributeOnlyIfTrue("restrict", T->isRestrict());
437   attributeOnlyIfTrue("variadic", E.Variadic);
438   switch (E.RefQualifier) {
439   case RQ_LValue: JOS.attribute("refQualifier", "&"); break;
440   case RQ_RValue: JOS.attribute("refQualifier", "&&"); break;
441   case RQ_None: break;
442   }
443   switch (E.ExceptionSpec.Type) {
444   case EST_DynamicNone:
445   case EST_Dynamic: {
446     JOS.attribute("exceptionSpec", "throw");
447     llvm::json::Array Types;
448     for (QualType QT : E.ExceptionSpec.Exceptions)
449       Types.push_back(createQualType(QT));
450     JOS.attribute("exceptionTypes", std::move(Types));
451   } break;
452   case EST_MSAny:
453     JOS.attribute("exceptionSpec", "throw");
454     JOS.attribute("throwsAny", true);
455     break;
456   case EST_BasicNoexcept:
457     JOS.attribute("exceptionSpec", "noexcept");
458     break;
459   case EST_NoexceptTrue:
460   case EST_NoexceptFalse:
461     JOS.attribute("exceptionSpec", "noexcept");
462     JOS.attribute("conditionEvaluatesTo",
463                 E.ExceptionSpec.Type == EST_NoexceptTrue);
464     //JOS.attributeWithCall("exceptionSpecExpr",
465     //                    [this, E]() { Visit(E.ExceptionSpec.NoexceptExpr); });
466     break;
467 
468   // FIXME: I cannot find a way to trigger these cases while dumping the AST. I
469   // suspect you can only run into them when executing an AST dump from within
470   // the debugger, which is not a use case we worry about for the JSON dumping
471   // feature.
472   case EST_DependentNoexcept:
473   case EST_Unevaluated:
474   case EST_Uninstantiated:
475   case EST_Unparsed:
476   case EST_None: break;
477   }
478   VisitFunctionType(T);
479 }
480 
481 void JSONNodeDumper::VisitNamedDecl(const NamedDecl *ND) {
482   if (ND && ND->getDeclName())
483     JOS.attribute("name", ND->getNameAsString());
484 }
485 
486 void JSONNodeDumper::VisitTypedefDecl(const TypedefDecl *TD) {
487   VisitNamedDecl(TD);
488   JOS.attribute("type", createQualType(TD->getUnderlyingType()));
489 }
490 
491 void JSONNodeDumper::VisitTypeAliasDecl(const TypeAliasDecl *TAD) {
492   VisitNamedDecl(TAD);
493   JOS.attribute("type", createQualType(TAD->getUnderlyingType()));
494 }
495 
496 void JSONNodeDumper::VisitNamespaceDecl(const NamespaceDecl *ND) {
497   VisitNamedDecl(ND);
498   attributeOnlyIfTrue("isInline", ND->isInline());
499   if (!ND->isOriginalNamespace())
500     JOS.attribute("originalNamespace",
501                   createBareDeclRef(ND->getOriginalNamespace()));
502 }
503 
504 void JSONNodeDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *UDD) {
505   JOS.attribute("nominatedNamespace",
506                 createBareDeclRef(UDD->getNominatedNamespace()));
507 }
508 
509 void JSONNodeDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *NAD) {
510   VisitNamedDecl(NAD);
511   JOS.attribute("aliasedNamespace",
512                 createBareDeclRef(NAD->getAliasedNamespace()));
513 }
514 
515 void JSONNodeDumper::VisitUsingDecl(const UsingDecl *UD) {
516   std::string Name;
517   if (const NestedNameSpecifier *NNS = UD->getQualifier()) {
518     llvm::raw_string_ostream SOS(Name);
519     NNS->print(SOS, UD->getASTContext().getPrintingPolicy());
520   }
521   Name += UD->getNameAsString();
522   JOS.attribute("name", Name);
523 }
524 
525 void JSONNodeDumper::VisitUsingShadowDecl(const UsingShadowDecl *USD) {
526   JOS.attribute("target", createBareDeclRef(USD->getTargetDecl()));
527 }
528 
529 void JSONNodeDumper::VisitVarDecl(const VarDecl *VD) {
530   VisitNamedDecl(VD);
531   JOS.attribute("type", createQualType(VD->getType()));
532 
533   StorageClass SC = VD->getStorageClass();
534   if (SC != SC_None)
535     JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC));
536   switch (VD->getTLSKind()) {
537   case VarDecl::TLS_Dynamic: JOS.attribute("tls", "dynamic"); break;
538   case VarDecl::TLS_Static: JOS.attribute("tls", "static"); break;
539   case VarDecl::TLS_None: break;
540   }
541   attributeOnlyIfTrue("nrvo", VD->isNRVOVariable());
542   attributeOnlyIfTrue("inline", VD->isInline());
543   attributeOnlyIfTrue("constexpr", VD->isConstexpr());
544   attributeOnlyIfTrue("modulePrivate", VD->isModulePrivate());
545   if (VD->hasInit()) {
546     switch (VD->getInitStyle()) {
547     case VarDecl::CInit: JOS.attribute("init", "c");  break;
548     case VarDecl::CallInit: JOS.attribute("init", "call"); break;
549     case VarDecl::ListInit: JOS.attribute("init", "list"); break;
550     }
551   }
552   attributeOnlyIfTrue("isParameterPack", VD->isParameterPack());
553 }
554 
555 void JSONNodeDumper::VisitFieldDecl(const FieldDecl *FD) {
556   VisitNamedDecl(FD);
557   JOS.attribute("type", createQualType(FD->getType()));
558   attributeOnlyIfTrue("mutable", FD->isMutable());
559   attributeOnlyIfTrue("modulePrivate", FD->isModulePrivate());
560   attributeOnlyIfTrue("isBitfield", FD->isBitField());
561   attributeOnlyIfTrue("hasInClassInitializer", FD->hasInClassInitializer());
562 }
563 
564 void JSONNodeDumper::VisitFunctionDecl(const FunctionDecl *FD) {
565   VisitNamedDecl(FD);
566   JOS.attribute("type", createQualType(FD->getType()));
567   StorageClass SC = FD->getStorageClass();
568   if (SC != SC_None)
569     JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC));
570   attributeOnlyIfTrue("inline", FD->isInlineSpecified());
571   attributeOnlyIfTrue("virtual", FD->isVirtualAsWritten());
572   attributeOnlyIfTrue("pure", FD->isPure());
573   attributeOnlyIfTrue("explicitlyDeleted", FD->isDeletedAsWritten());
574   attributeOnlyIfTrue("constexpr", FD->isConstexpr());
575   attributeOnlyIfTrue("variadic", FD->isVariadic());
576 
577   if (FD->isDefaulted())
578     JOS.attribute("explicitlyDefaulted",
579                   FD->isDeleted() ? "deleted" : "default");
580 }
581 
582 void JSONNodeDumper::VisitEnumDecl(const EnumDecl *ED) {
583   VisitNamedDecl(ED);
584   if (ED->isFixed())
585     JOS.attribute("fixedUnderlyingType", createQualType(ED->getIntegerType()));
586   if (ED->isScoped())
587     JOS.attribute("scopedEnumTag",
588                   ED->isScopedUsingClassTag() ? "class" : "struct");
589 }
590 void JSONNodeDumper::VisitEnumConstantDecl(const EnumConstantDecl *ECD) {
591   VisitNamedDecl(ECD);
592   JOS.attribute("type", createQualType(ECD->getType()));
593 }
594 
595 void JSONNodeDumper::VisitRecordDecl(const RecordDecl *RD) {
596   VisitNamedDecl(RD);
597   JOS.attribute("tagUsed", RD->getKindName());
598   attributeOnlyIfTrue("completeDefinition", RD->isCompleteDefinition());
599 }
600 void JSONNodeDumper::VisitCXXRecordDecl(const CXXRecordDecl *RD) {
601   VisitRecordDecl(RD);
602 
603   // All other information requires a complete definition.
604   if (!RD->isCompleteDefinition())
605     return;
606 
607   JOS.attribute("definitionData", createCXXRecordDefinitionData(RD));
608   if (RD->getNumBases()) {
609     JOS.attributeArray("bases", [this, RD] {
610       for (const auto &Spec : RD->bases())
611         JOS.value(createCXXBaseSpecifier(Spec));
612     });
613   }
614 }
615 
616 void JSONNodeDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
617   VisitNamedDecl(D);
618   JOS.attribute("tagUsed", D->wasDeclaredWithTypename() ? "typename" : "class");
619   JOS.attribute("depth", D->getDepth());
620   JOS.attribute("index", D->getIndex());
621   attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
622 }
623 
624 void JSONNodeDumper::VisitNonTypeTemplateParmDecl(
625     const NonTypeTemplateParmDecl *D) {
626   VisitNamedDecl(D);
627   JOS.attribute("type", createQualType(D->getType()));
628   JOS.attribute("depth", D->getDepth());
629   JOS.attribute("index", D->getIndex());
630   attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
631 }
632 
633 void JSONNodeDumper::VisitTemplateTemplateParmDecl(
634     const TemplateTemplateParmDecl *D) {
635   VisitNamedDecl(D);
636   JOS.attribute("depth", D->getDepth());
637   JOS.attribute("index", D->getIndex());
638   attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
639 }
640 
641 void JSONNodeDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *LSD) {
642   StringRef Lang;
643   switch (LSD->getLanguage()) {
644   case LinkageSpecDecl::lang_c: Lang = "C"; break;
645   case LinkageSpecDecl::lang_cxx: Lang = "C++"; break;
646   }
647   JOS.attribute("language", Lang);
648   attributeOnlyIfTrue("hasBraces", LSD->hasBraces());
649 }
650 
651 void JSONNodeDumper::VisitAccessSpecDecl(const AccessSpecDecl *ASD) {
652   JOS.attribute("access", createAccessSpecifier(ASD->getAccess()));
653 }
654 
655 void JSONNodeDumper::VisitFriendDecl(const FriendDecl *FD) {
656   if (const TypeSourceInfo *T = FD->getFriendType())
657     JOS.attribute("type", createQualType(T->getType()));
658 }
659 
660 void JSONNodeDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
661   VisitNamedDecl(D);
662   JOS.attribute("type", createQualType(D->getType()));
663   attributeOnlyIfTrue("synthesized", D->getSynthesize());
664   switch (D->getAccessControl()) {
665   case ObjCIvarDecl::None: JOS.attribute("access", "none"); break;
666   case ObjCIvarDecl::Private: JOS.attribute("access", "private"); break;
667   case ObjCIvarDecl::Protected: JOS.attribute("access", "protected"); break;
668   case ObjCIvarDecl::Public: JOS.attribute("access", "public"); break;
669   case ObjCIvarDecl::Package: JOS.attribute("access", "package"); break;
670   }
671 }
672 
673 void JSONNodeDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
674   VisitNamedDecl(D);
675   JOS.attribute("returnType", createQualType(D->getReturnType()));
676   JOS.attribute("instance", D->isInstanceMethod());
677   attributeOnlyIfTrue("variadic", D->isVariadic());
678 }
679 
680 void JSONNodeDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) {
681   VisitNamedDecl(D);
682   JOS.attribute("type", createQualType(D->getUnderlyingType()));
683   attributeOnlyIfTrue("bounded", D->hasExplicitBound());
684   switch (D->getVariance()) {
685   case ObjCTypeParamVariance::Invariant:
686     break;
687   case ObjCTypeParamVariance::Covariant:
688     JOS.attribute("variance", "covariant");
689     break;
690   case ObjCTypeParamVariance::Contravariant:
691     JOS.attribute("variance", "contravariant");
692     break;
693   }
694 }
695 
696 void JSONNodeDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
697   VisitNamedDecl(D);
698   JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
699   JOS.attribute("implementation", createBareDeclRef(D->getImplementation()));
700 
701   llvm::json::Array Protocols;
702   for (const auto* P : D->protocols())
703     Protocols.push_back(createBareDeclRef(P));
704   if (!Protocols.empty())
705     JOS.attribute("protocols", std::move(Protocols));
706 }
707 
708 void JSONNodeDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
709   VisitNamedDecl(D);
710   JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
711   JOS.attribute("categoryDecl", createBareDeclRef(D->getCategoryDecl()));
712 }
713 
714 void JSONNodeDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
715   VisitNamedDecl(D);
716 
717   llvm::json::Array Protocols;
718   for (const auto *P : D->protocols())
719     Protocols.push_back(createBareDeclRef(P));
720   if (!Protocols.empty())
721     JOS.attribute("protocols", std::move(Protocols));
722 }
723 
724 void JSONNodeDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
725   VisitNamedDecl(D);
726   JOS.attribute("super", createBareDeclRef(D->getSuperClass()));
727   JOS.attribute("implementation", createBareDeclRef(D->getImplementation()));
728 
729   llvm::json::Array Protocols;
730   for (const auto* P : D->protocols())
731     Protocols.push_back(createBareDeclRef(P));
732   if (!Protocols.empty())
733     JOS.attribute("protocols", std::move(Protocols));
734 }
735 
736 void JSONNodeDumper::VisitObjCImplementationDecl(
737     const ObjCImplementationDecl *D) {
738   VisitNamedDecl(D);
739   JOS.attribute("super", createBareDeclRef(D->getSuperClass()));
740   JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
741 }
742 
743 void JSONNodeDumper::VisitObjCCompatibleAliasDecl(
744     const ObjCCompatibleAliasDecl *D) {
745   VisitNamedDecl(D);
746   JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
747 }
748 
749 void JSONNodeDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
750   VisitNamedDecl(D);
751   JOS.attribute("type", createQualType(D->getType()));
752 
753   switch (D->getPropertyImplementation()) {
754   case ObjCPropertyDecl::None: break;
755   case ObjCPropertyDecl::Required: JOS.attribute("control", "required"); break;
756   case ObjCPropertyDecl::Optional: JOS.attribute("control", "optional"); break;
757   }
758 
759   ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
760   if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
761     if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
762       JOS.attribute("getter", createBareDeclRef(D->getGetterMethodDecl()));
763     if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
764       JOS.attribute("setter", createBareDeclRef(D->getSetterMethodDecl()));
765     attributeOnlyIfTrue("readonly", Attrs & ObjCPropertyDecl::OBJC_PR_readonly);
766     attributeOnlyIfTrue("assign", Attrs & ObjCPropertyDecl::OBJC_PR_assign);
767     attributeOnlyIfTrue("readwrite",
768                         Attrs & ObjCPropertyDecl::OBJC_PR_readwrite);
769     attributeOnlyIfTrue("retain", Attrs & ObjCPropertyDecl::OBJC_PR_retain);
770     attributeOnlyIfTrue("copy", Attrs & ObjCPropertyDecl::OBJC_PR_copy);
771     attributeOnlyIfTrue("nonatomic",
772                         Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic);
773     attributeOnlyIfTrue("atomic", Attrs & ObjCPropertyDecl::OBJC_PR_atomic);
774     attributeOnlyIfTrue("weak", Attrs & ObjCPropertyDecl::OBJC_PR_weak);
775     attributeOnlyIfTrue("strong", Attrs & ObjCPropertyDecl::OBJC_PR_strong);
776     attributeOnlyIfTrue("unsafe_unretained",
777                         Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
778     attributeOnlyIfTrue("class", Attrs & ObjCPropertyDecl::OBJC_PR_class);
779     attributeOnlyIfTrue("nullability",
780                         Attrs & ObjCPropertyDecl::OBJC_PR_nullability);
781     attributeOnlyIfTrue("null_resettable",
782                         Attrs & ObjCPropertyDecl::OBJC_PR_null_resettable);
783   }
784 }
785 
786 void JSONNodeDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
787   VisitNamedDecl(D->getPropertyDecl());
788   JOS.attribute("implKind", D->getPropertyImplementation() ==
789                                     ObjCPropertyImplDecl::Synthesize
790                                 ? "synthesize"
791                                 : "dynamic");
792   JOS.attribute("propertyDecl", createBareDeclRef(D->getPropertyDecl()));
793   JOS.attribute("ivarDecl", createBareDeclRef(D->getPropertyIvarDecl()));
794 }
795 
796 void JSONNodeDumper::VisitBlockDecl(const BlockDecl *D) {
797   attributeOnlyIfTrue("variadic", D->isVariadic());
798   attributeOnlyIfTrue("capturesThis", D->capturesCXXThis());
799 }
800 
801 void JSONNodeDumper::VisitDeclRefExpr(const DeclRefExpr *DRE) {
802   JOS.attribute("referencedDecl", createBareDeclRef(DRE->getDecl()));
803   if (DRE->getDecl() != DRE->getFoundDecl())
804     JOS.attribute("foundReferencedDecl",
805                   createBareDeclRef(DRE->getFoundDecl()));
806 }
807 
808 void JSONNodeDumper::VisitPredefinedExpr(const PredefinedExpr *PE) {
809   JOS.attribute("name", PredefinedExpr::getIdentKindName(PE->getIdentKind()));
810 }
811 
812 void JSONNodeDumper::VisitUnaryOperator(const UnaryOperator *UO) {
813   JOS.attribute("isPostfix", UO->isPostfix());
814   JOS.attribute("opcode", UnaryOperator::getOpcodeStr(UO->getOpcode()));
815   if (!UO->canOverflow())
816     JOS.attribute("canOverflow", false);
817 }
818 
819 void JSONNodeDumper::VisitBinaryOperator(const BinaryOperator *BO) {
820   JOS.attribute("opcode", BinaryOperator::getOpcodeStr(BO->getOpcode()));
821 }
822 
823 void JSONNodeDumper::VisitCompoundAssignOperator(
824     const CompoundAssignOperator *CAO) {
825   VisitBinaryOperator(CAO);
826   JOS.attribute("computeLHSType", createQualType(CAO->getComputationLHSType()));
827   JOS.attribute("computeResultType",
828                 createQualType(CAO->getComputationResultType()));
829 }
830 
831 void JSONNodeDumper::VisitMemberExpr(const MemberExpr *ME) {
832   // Note, we always write this Boolean field because the information it conveys
833   // is critical to understanding the AST node.
834   ValueDecl *VD = ME->getMemberDecl();
835   JOS.attribute("name", VD && VD->getDeclName() ? VD->getNameAsString() : "");
836   JOS.attribute("isArrow", ME->isArrow());
837   JOS.attribute("referencedMemberDecl", createPointerRepresentation(VD));
838 }
839 
840 void JSONNodeDumper::VisitCXXNewExpr(const CXXNewExpr *NE) {
841   attributeOnlyIfTrue("isGlobal", NE->isGlobalNew());
842   attributeOnlyIfTrue("isArray", NE->isArray());
843   attributeOnlyIfTrue("isPlacement", NE->getNumPlacementArgs() != 0);
844   switch (NE->getInitializationStyle()) {
845   case CXXNewExpr::NoInit: break;
846   case CXXNewExpr::CallInit: JOS.attribute("initStyle", "call"); break;
847   case CXXNewExpr::ListInit: JOS.attribute("initStyle", "list"); break;
848   }
849   if (const FunctionDecl *FD = NE->getOperatorNew())
850     JOS.attribute("operatorNewDecl", createBareDeclRef(FD));
851   if (const FunctionDecl *FD = NE->getOperatorDelete())
852     JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD));
853 }
854 void JSONNodeDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *DE) {
855   attributeOnlyIfTrue("isGlobal", DE->isGlobalDelete());
856   attributeOnlyIfTrue("isArray", DE->isArrayForm());
857   attributeOnlyIfTrue("isArrayAsWritten", DE->isArrayFormAsWritten());
858   if (const FunctionDecl *FD = DE->getOperatorDelete())
859     JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD));
860 }
861 
862 void JSONNodeDumper::VisitCXXThisExpr(const CXXThisExpr *TE) {
863   attributeOnlyIfTrue("implicit", TE->isImplicit());
864 }
865 
866 void JSONNodeDumper::VisitCastExpr(const CastExpr *CE) {
867   JOS.attribute("castKind", CE->getCastKindName());
868   llvm::json::Array Path = createCastPath(CE);
869   if (!Path.empty())
870     JOS.attribute("path", std::move(Path));
871   // FIXME: This may not be useful information as it can be obtusely gleaned
872   // from the inner[] array.
873   if (const NamedDecl *ND = CE->getConversionFunction())
874     JOS.attribute("conversionFunc", createBareDeclRef(ND));
875 }
876 
877 void JSONNodeDumper::VisitImplicitCastExpr(const ImplicitCastExpr *ICE) {
878   VisitCastExpr(ICE);
879   attributeOnlyIfTrue("isPartOfExplicitCast", ICE->isPartOfExplicitCast());
880 }
881 
882 void JSONNodeDumper::VisitCallExpr(const CallExpr *CE) {
883   attributeOnlyIfTrue("adl", CE->usesADL());
884 }
885 
886 void JSONNodeDumper::VisitUnaryExprOrTypeTraitExpr(
887     const UnaryExprOrTypeTraitExpr *TTE) {
888   switch (TTE->getKind()) {
889   case UETT_SizeOf: JOS.attribute("name", "sizeof"); break;
890   case UETT_AlignOf: JOS.attribute("name", "alignof"); break;
891   case UETT_VecStep:  JOS.attribute("name", "vec_step"); break;
892   case UETT_PreferredAlignOf:  JOS.attribute("name", "__alignof"); break;
893   case UETT_OpenMPRequiredSimdAlign:
894     JOS.attribute("name", "__builtin_omp_required_simd_align"); break;
895   }
896   if (TTE->isArgumentType())
897     JOS.attribute("argType", createQualType(TTE->getArgumentType()));
898 }
899 
900 void JSONNodeDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *SOPE) {
901   VisitNamedDecl(SOPE->getPack());
902 }
903 
904 void JSONNodeDumper::VisitUnresolvedLookupExpr(
905     const UnresolvedLookupExpr *ULE) {
906   JOS.attribute("usesADL", ULE->requiresADL());
907   JOS.attribute("name", ULE->getName().getAsString());
908 
909   JOS.attributeArray("lookups", [this, ULE] {
910     for (const NamedDecl *D : ULE->decls())
911       JOS.value(createBareDeclRef(D));
912   });
913 }
914 
915 void JSONNodeDumper::VisitAddrLabelExpr(const AddrLabelExpr *ALE) {
916   JOS.attribute("name", ALE->getLabel()->getName());
917   JOS.attribute("labelDeclId", createPointerRepresentation(ALE->getLabel()));
918 }
919 
920 void JSONNodeDumper::VisitCXXTypeidExpr(const CXXTypeidExpr *CTE) {
921   if (CTE->isTypeOperand()) {
922     QualType Adjusted = CTE->getTypeOperand(Ctx);
923     QualType Unadjusted = CTE->getTypeOperandSourceInfo()->getType();
924     JOS.attribute("typeArg", createQualType(Unadjusted));
925     if (Adjusted != Unadjusted)
926       JOS.attribute("adjustedTypeArg", createQualType(Adjusted));
927   }
928 }
929 
930 void JSONNodeDumper::VisitIntegerLiteral(const IntegerLiteral *IL) {
931   JOS.attribute("value",
932                 IL->getValue().toString(
933                     /*Radix=*/10, IL->getType()->isSignedIntegerType()));
934 }
935 void JSONNodeDumper::VisitCharacterLiteral(const CharacterLiteral *CL) {
936   // FIXME: This should probably print the character literal as a string,
937   // rather than as a numerical value. It would be nice if the behavior matched
938   // what we do to print a string literal; right now, it is impossible to tell
939   // the difference between 'a' and L'a' in C from the JSON output.
940   JOS.attribute("value", CL->getValue());
941 }
942 void JSONNodeDumper::VisitFixedPointLiteral(const FixedPointLiteral *FPL) {
943   JOS.attribute("value", FPL->getValueAsString(/*Radix=*/10));
944 }
945 void JSONNodeDumper::VisitFloatingLiteral(const FloatingLiteral *FL) {
946   JOS.attribute("value", FL->getValueAsApproximateDouble());
947 }
948 void JSONNodeDumper::VisitStringLiteral(const StringLiteral *SL) {
949   std::string Buffer;
950   llvm::raw_string_ostream SS(Buffer);
951   SL->outputString(SS);
952   JOS.attribute("value", SS.str());
953 }
954 void JSONNodeDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *BLE) {
955   JOS.attribute("value", BLE->getValue());
956 }
957 
958 void JSONNodeDumper::VisitIfStmt(const IfStmt *IS) {
959   attributeOnlyIfTrue("hasInit", IS->hasInitStorage());
960   attributeOnlyIfTrue("hasVar", IS->hasVarStorage());
961   attributeOnlyIfTrue("hasElse", IS->hasElseStorage());
962   attributeOnlyIfTrue("isConstexpr", IS->isConstexpr());
963 }
964 
965 void JSONNodeDumper::VisitSwitchStmt(const SwitchStmt *SS) {
966   attributeOnlyIfTrue("hasInit", SS->hasInitStorage());
967   attributeOnlyIfTrue("hasVar", SS->hasVarStorage());
968 }
969 void JSONNodeDumper::VisitCaseStmt(const CaseStmt *CS) {
970   attributeOnlyIfTrue("isGNURange", CS->caseStmtIsGNURange());
971 }
972 
973 void JSONNodeDumper::VisitLabelStmt(const LabelStmt *LS) {
974   JOS.attribute("name", LS->getName());
975   JOS.attribute("declId", createPointerRepresentation(LS->getDecl()));
976 }
977 void JSONNodeDumper::VisitGotoStmt(const GotoStmt *GS) {
978   JOS.attribute("targetLabelDeclId",
979                 createPointerRepresentation(GS->getLabel()));
980 }
981 
982 void JSONNodeDumper::VisitWhileStmt(const WhileStmt *WS) {
983   attributeOnlyIfTrue("hasVar", WS->hasVarStorage());
984 }
985 
986 void JSONNodeDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt* OACS) {
987   // FIXME: it would be nice for the ASTNodeTraverser would handle the catch
988   // parameter the same way for C++ and ObjC rather. In this case, C++ gets a
989   // null child node and ObjC gets no child node.
990   attributeOnlyIfTrue("isCatchAll", OACS->getCatchParamDecl() == nullptr);
991 }
992 
993 StringRef JSONNodeDumper::getCommentCommandName(unsigned CommandID) const {
994   if (Traits)
995     return Traits->getCommandInfo(CommandID)->Name;
996   if (const comments::CommandInfo *Info =
997           comments::CommandTraits::getBuiltinCommandInfo(CommandID))
998     return Info->Name;
999   return "<invalid>";
1000 }
1001 
1002 void JSONNodeDumper::visitTextComment(const comments::TextComment *C,
1003                                       const comments::FullComment *) {
1004   JOS.attribute("text", C->getText());
1005 }
1006 
1007 void JSONNodeDumper::visitInlineCommandComment(
1008     const comments::InlineCommandComment *C, const comments::FullComment *) {
1009   JOS.attribute("name", getCommentCommandName(C->getCommandID()));
1010 
1011   switch (C->getRenderKind()) {
1012   case comments::InlineCommandComment::RenderNormal:
1013     JOS.attribute("renderKind", "normal");
1014     break;
1015   case comments::InlineCommandComment::RenderBold:
1016     JOS.attribute("renderKind", "bold");
1017     break;
1018   case comments::InlineCommandComment::RenderEmphasized:
1019     JOS.attribute("renderKind", "emphasized");
1020     break;
1021   case comments::InlineCommandComment::RenderMonospaced:
1022     JOS.attribute("renderKind", "monospaced");
1023     break;
1024   }
1025 
1026   llvm::json::Array Args;
1027   for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I)
1028     Args.push_back(C->getArgText(I));
1029 
1030   if (!Args.empty())
1031     JOS.attribute("args", std::move(Args));
1032 }
1033 
1034 void JSONNodeDumper::visitHTMLStartTagComment(
1035     const comments::HTMLStartTagComment *C, const comments::FullComment *) {
1036   JOS.attribute("name", C->getTagName());
1037   attributeOnlyIfTrue("selfClosing", C->isSelfClosing());
1038   attributeOnlyIfTrue("malformed", C->isMalformed());
1039 
1040   llvm::json::Array Attrs;
1041   for (unsigned I = 0, E = C->getNumAttrs(); I < E; ++I)
1042     Attrs.push_back(
1043         {{"name", C->getAttr(I).Name}, {"value", C->getAttr(I).Value}});
1044 
1045   if (!Attrs.empty())
1046     JOS.attribute("attrs", std::move(Attrs));
1047 }
1048 
1049 void JSONNodeDumper::visitHTMLEndTagComment(
1050     const comments::HTMLEndTagComment *C, const comments::FullComment *) {
1051   JOS.attribute("name", C->getTagName());
1052 }
1053 
1054 void JSONNodeDumper::visitBlockCommandComment(
1055     const comments::BlockCommandComment *C, const comments::FullComment *) {
1056   JOS.attribute("name", getCommentCommandName(C->getCommandID()));
1057 
1058   llvm::json::Array Args;
1059   for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I)
1060     Args.push_back(C->getArgText(I));
1061 
1062   if (!Args.empty())
1063     JOS.attribute("args", std::move(Args));
1064 }
1065 
1066 void JSONNodeDumper::visitParamCommandComment(
1067     const comments::ParamCommandComment *C, const comments::FullComment *FC) {
1068   switch (C->getDirection()) {
1069   case comments::ParamCommandComment::In:
1070     JOS.attribute("direction", "in");
1071     break;
1072   case comments::ParamCommandComment::Out:
1073     JOS.attribute("direction", "out");
1074     break;
1075   case comments::ParamCommandComment::InOut:
1076     JOS.attribute("direction", "in,out");
1077     break;
1078   }
1079   attributeOnlyIfTrue("explicit", C->isDirectionExplicit());
1080 
1081   if (C->hasParamName())
1082     JOS.attribute("param", C->isParamIndexValid() ? C->getParamName(FC)
1083                                                   : C->getParamNameAsWritten());
1084 
1085   if (C->isParamIndexValid() && !C->isVarArgParam())
1086     JOS.attribute("paramIdx", C->getParamIndex());
1087 }
1088 
1089 void JSONNodeDumper::visitTParamCommandComment(
1090     const comments::TParamCommandComment *C, const comments::FullComment *FC) {
1091   if (C->hasParamName())
1092     JOS.attribute("param", C->isPositionValid() ? C->getParamName(FC)
1093                                                 : C->getParamNameAsWritten());
1094   if (C->isPositionValid()) {
1095     llvm::json::Array Positions;
1096     for (unsigned I = 0, E = C->getDepth(); I < E; ++I)
1097       Positions.push_back(C->getIndex(I));
1098 
1099     if (!Positions.empty())
1100       JOS.attribute("positions", std::move(Positions));
1101   }
1102 }
1103 
1104 void JSONNodeDumper::visitVerbatimBlockComment(
1105     const comments::VerbatimBlockComment *C, const comments::FullComment *) {
1106   JOS.attribute("name", getCommentCommandName(C->getCommandID()));
1107   JOS.attribute("closeName", C->getCloseName());
1108 }
1109 
1110 void JSONNodeDumper::visitVerbatimBlockLineComment(
1111     const comments::VerbatimBlockLineComment *C,
1112     const comments::FullComment *) {
1113   JOS.attribute("text", C->getText());
1114 }
1115 
1116 void JSONNodeDumper::visitVerbatimLineComment(
1117     const comments::VerbatimLineComment *C, const comments::FullComment *) {
1118   JOS.attribute("text", C->getText());
1119 }
1120