JSONNodeDumper.cpp 40 KB

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