JSONNodeDumper.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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. void JSONNodeDumper::writeBareDeclRef(const Decl *D) {
  186. JOS.attribute("id", createPointerRepresentation(D));
  187. if (!D)
  188. return;
  189. JOS.attribute("kind", (llvm::Twine(D->getDeclKindName()) + "Decl").str());
  190. if (const auto *ND = dyn_cast<NamedDecl>(D))
  191. JOS.attribute("name", ND->getDeclName().getAsString());
  192. if (const auto *VD = dyn_cast<ValueDecl>(D))
  193. JOS.attribute("type", createQualType(VD->getType()));
  194. }
  195. llvm::json::Object JSONNodeDumper::createBareDeclRef(const Decl *D) {
  196. llvm::json::Object Ret{{"id", createPointerRepresentation(D)}};
  197. if (!D)
  198. return Ret;
  199. Ret["kind"] = (llvm::Twine(D->getDeclKindName()) + "Decl").str();
  200. if (const auto *ND = dyn_cast<NamedDecl>(D))
  201. Ret["name"] = ND->getDeclName().getAsString();
  202. if (const auto *VD = dyn_cast<ValueDecl>(D))
  203. Ret["type"] = createQualType(VD->getType());
  204. return Ret;
  205. }
  206. llvm::json::Array JSONNodeDumper::createCastPath(const CastExpr *C) {
  207. llvm::json::Array Ret;
  208. if (C->path_empty())
  209. return Ret;
  210. for (auto I = C->path_begin(), E = C->path_end(); I != E; ++I) {
  211. const CXXBaseSpecifier *Base = *I;
  212. const auto *RD =
  213. cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
  214. llvm::json::Object Val{{"name", RD->getName()}};
  215. if (Base->isVirtual())
  216. Val["isVirtual"] = true;
  217. Ret.push_back(std::move(Val));
  218. }
  219. return Ret;
  220. }
  221. #define FIELD2(Name, Flag) if (RD->Flag()) Ret[Name] = true
  222. #define FIELD1(Flag) FIELD2(#Flag, Flag)
  223. static llvm::json::Object
  224. createDefaultConstructorDefinitionData(const CXXRecordDecl *RD) {
  225. llvm::json::Object Ret;
  226. FIELD2("exists", hasDefaultConstructor);
  227. FIELD2("trivial", hasTrivialDefaultConstructor);
  228. FIELD2("nonTrivial", hasNonTrivialDefaultConstructor);
  229. FIELD2("userProvided", hasUserProvidedDefaultConstructor);
  230. FIELD2("isConstexpr", hasConstexprDefaultConstructor);
  231. FIELD2("needsImplicit", needsImplicitDefaultConstructor);
  232. FIELD2("defaultedIsConstexpr", defaultedDefaultConstructorIsConstexpr);
  233. return Ret;
  234. }
  235. static llvm::json::Object
  236. createCopyConstructorDefinitionData(const CXXRecordDecl *RD) {
  237. llvm::json::Object Ret;
  238. FIELD2("simple", hasSimpleCopyConstructor);
  239. FIELD2("trivial", hasTrivialCopyConstructor);
  240. FIELD2("nonTrivial", hasNonTrivialCopyConstructor);
  241. FIELD2("userDeclared", hasUserDeclaredCopyConstructor);
  242. FIELD2("hasConstParam", hasCopyConstructorWithConstParam);
  243. FIELD2("implicitHasConstParam", implicitCopyConstructorHasConstParam);
  244. FIELD2("needsImplicit", needsImplicitCopyConstructor);
  245. FIELD2("needsOverloadResolution", needsOverloadResolutionForCopyConstructor);
  246. if (!RD->needsOverloadResolutionForCopyConstructor())
  247. FIELD2("defaultedIsDeleted", defaultedCopyConstructorIsDeleted);
  248. return Ret;
  249. }
  250. static llvm::json::Object
  251. createMoveConstructorDefinitionData(const CXXRecordDecl *RD) {
  252. llvm::json::Object Ret;
  253. FIELD2("exists", hasMoveConstructor);
  254. FIELD2("simple", hasSimpleMoveConstructor);
  255. FIELD2("trivial", hasTrivialMoveConstructor);
  256. FIELD2("nonTrivial", hasNonTrivialMoveConstructor);
  257. FIELD2("userDeclared", hasUserDeclaredMoveConstructor);
  258. FIELD2("needsImplicit", needsImplicitMoveConstructor);
  259. FIELD2("needsOverloadResolution", needsOverloadResolutionForMoveConstructor);
  260. if (!RD->needsOverloadResolutionForMoveConstructor())
  261. FIELD2("defaultedIsDeleted", defaultedMoveConstructorIsDeleted);
  262. return Ret;
  263. }
  264. static llvm::json::Object
  265. createCopyAssignmentDefinitionData(const CXXRecordDecl *RD) {
  266. llvm::json::Object Ret;
  267. FIELD2("trivial", hasTrivialCopyAssignment);
  268. FIELD2("nonTrivial", hasNonTrivialCopyAssignment);
  269. FIELD2("hasConstParam", hasCopyAssignmentWithConstParam);
  270. FIELD2("implicitHasConstParam", implicitCopyAssignmentHasConstParam);
  271. FIELD2("userDeclared", hasUserDeclaredCopyAssignment);
  272. FIELD2("needsImplicit", needsImplicitCopyAssignment);
  273. FIELD2("needsOverloadResolution", needsOverloadResolutionForCopyAssignment);
  274. return Ret;
  275. }
  276. static llvm::json::Object
  277. createMoveAssignmentDefinitionData(const CXXRecordDecl *RD) {
  278. llvm::json::Object Ret;
  279. FIELD2("exists", hasMoveAssignment);
  280. FIELD2("simple", hasSimpleMoveAssignment);
  281. FIELD2("trivial", hasTrivialMoveAssignment);
  282. FIELD2("nonTrivial", hasNonTrivialMoveAssignment);
  283. FIELD2("userDeclared", hasUserDeclaredMoveAssignment);
  284. FIELD2("needsImplicit", needsImplicitMoveAssignment);
  285. FIELD2("needsOverloadResolution", needsOverloadResolutionForMoveAssignment);
  286. return Ret;
  287. }
  288. static llvm::json::Object
  289. createDestructorDefinitionData(const CXXRecordDecl *RD) {
  290. llvm::json::Object Ret;
  291. FIELD2("simple", hasSimpleDestructor);
  292. FIELD2("irrelevant", hasIrrelevantDestructor);
  293. FIELD2("trivial", hasTrivialDestructor);
  294. FIELD2("nonTrivial", hasNonTrivialDestructor);
  295. FIELD2("userDeclared", hasUserDeclaredDestructor);
  296. FIELD2("needsImplicit", needsImplicitDestructor);
  297. FIELD2("needsOverloadResolution", needsOverloadResolutionForDestructor);
  298. if (!RD->needsOverloadResolutionForDestructor())
  299. FIELD2("defaultedIsDeleted", defaultedDestructorIsDeleted);
  300. return Ret;
  301. }
  302. llvm::json::Object
  303. JSONNodeDumper::createCXXRecordDefinitionData(const CXXRecordDecl *RD) {
  304. llvm::json::Object Ret;
  305. // This data is common to all C++ classes.
  306. FIELD1(isGenericLambda);
  307. FIELD1(isLambda);
  308. FIELD1(isEmpty);
  309. FIELD1(isAggregate);
  310. FIELD1(isStandardLayout);
  311. FIELD1(isTriviallyCopyable);
  312. FIELD1(isPOD);
  313. FIELD1(isTrivial);
  314. FIELD1(isPolymorphic);
  315. FIELD1(isAbstract);
  316. FIELD1(isLiteral);
  317. FIELD1(canPassInRegisters);
  318. FIELD1(hasUserDeclaredConstructor);
  319. FIELD1(hasConstexprNonCopyMoveConstructor);
  320. FIELD1(hasMutableFields);
  321. FIELD1(hasVariantMembers);
  322. FIELD2("canConstDefaultInit", allowConstDefaultInit);
  323. Ret["defaultCtor"] = createDefaultConstructorDefinitionData(RD);
  324. Ret["copyCtor"] = createCopyConstructorDefinitionData(RD);
  325. Ret["moveCtor"] = createMoveConstructorDefinitionData(RD);
  326. Ret["copyAssign"] = createCopyAssignmentDefinitionData(RD);
  327. Ret["moveAssign"] = createMoveAssignmentDefinitionData(RD);
  328. Ret["dtor"] = createDestructorDefinitionData(RD);
  329. return Ret;
  330. }
  331. #undef FIELD1
  332. #undef FIELD2
  333. std::string JSONNodeDumper::createAccessSpecifier(AccessSpecifier AS) {
  334. switch (AS) {
  335. case AS_none: return "none";
  336. case AS_private: return "private";
  337. case AS_protected: return "protected";
  338. case AS_public: return "public";
  339. }
  340. llvm_unreachable("Unknown access specifier");
  341. }
  342. llvm::json::Object
  343. JSONNodeDumper::createCXXBaseSpecifier(const CXXBaseSpecifier &BS) {
  344. llvm::json::Object Ret;
  345. Ret["type"] = createQualType(BS.getType());
  346. Ret["access"] = createAccessSpecifier(BS.getAccessSpecifier());
  347. Ret["writtenAccess"] =
  348. createAccessSpecifier(BS.getAccessSpecifierAsWritten());
  349. if (BS.isVirtual())
  350. Ret["isVirtual"] = true;
  351. if (BS.isPackExpansion())
  352. Ret["isPackExpansion"] = true;
  353. return Ret;
  354. }
  355. void JSONNodeDumper::VisitTypedefType(const TypedefType *TT) {
  356. JOS.attribute("decl", createBareDeclRef(TT->getDecl()));
  357. }
  358. void JSONNodeDumper::VisitFunctionType(const FunctionType *T) {
  359. FunctionType::ExtInfo E = T->getExtInfo();
  360. attributeOnlyIfTrue("noreturn", E.getNoReturn());
  361. attributeOnlyIfTrue("producesResult", E.getProducesResult());
  362. if (E.getHasRegParm())
  363. JOS.attribute("regParm", E.getRegParm());
  364. JOS.attribute("cc", FunctionType::getNameForCallConv(E.getCC()));
  365. }
  366. void JSONNodeDumper::VisitFunctionProtoType(const FunctionProtoType *T) {
  367. FunctionProtoType::ExtProtoInfo E = T->getExtProtoInfo();
  368. attributeOnlyIfTrue("trailingReturn", E.HasTrailingReturn);
  369. attributeOnlyIfTrue("const", T->isConst());
  370. attributeOnlyIfTrue("volatile", T->isVolatile());
  371. attributeOnlyIfTrue("restrict", T->isRestrict());
  372. attributeOnlyIfTrue("variadic", E.Variadic);
  373. switch (E.RefQualifier) {
  374. case RQ_LValue: JOS.attribute("refQualifier", "&"); break;
  375. case RQ_RValue: JOS.attribute("refQualifier", "&&"); break;
  376. case RQ_None: break;
  377. }
  378. switch (E.ExceptionSpec.Type) {
  379. case EST_DynamicNone:
  380. case EST_Dynamic: {
  381. JOS.attribute("exceptionSpec", "throw");
  382. llvm::json::Array Types;
  383. for (QualType QT : E.ExceptionSpec.Exceptions)
  384. Types.push_back(createQualType(QT));
  385. JOS.attribute("exceptionTypes", std::move(Types));
  386. } break;
  387. case EST_MSAny:
  388. JOS.attribute("exceptionSpec", "throw");
  389. JOS.attribute("throwsAny", true);
  390. break;
  391. case EST_BasicNoexcept:
  392. JOS.attribute("exceptionSpec", "noexcept");
  393. break;
  394. case EST_NoexceptTrue:
  395. case EST_NoexceptFalse:
  396. JOS.attribute("exceptionSpec", "noexcept");
  397. JOS.attribute("conditionEvaluatesTo",
  398. E.ExceptionSpec.Type == EST_NoexceptTrue);
  399. //JOS.attributeWithCall("exceptionSpecExpr",
  400. // [this, E]() { Visit(E.ExceptionSpec.NoexceptExpr); });
  401. break;
  402. case EST_NoThrow:
  403. JOS.attribute("exceptionSpec", "nothrow");
  404. break;
  405. // FIXME: I cannot find a way to trigger these cases while dumping the AST. I
  406. // suspect you can only run into them when executing an AST dump from within
  407. // the debugger, which is not a use case we worry about for the JSON dumping
  408. // feature.
  409. case EST_DependentNoexcept:
  410. case EST_Unevaluated:
  411. case EST_Uninstantiated:
  412. case EST_Unparsed:
  413. case EST_None: break;
  414. }
  415. VisitFunctionType(T);
  416. }
  417. void JSONNodeDumper::VisitNamedDecl(const NamedDecl *ND) {
  418. if (ND && ND->getDeclName())
  419. JOS.attribute("name", ND->getNameAsString());
  420. }
  421. void JSONNodeDumper::VisitTypedefDecl(const TypedefDecl *TD) {
  422. VisitNamedDecl(TD);
  423. JOS.attribute("type", createQualType(TD->getUnderlyingType()));
  424. }
  425. void JSONNodeDumper::VisitTypeAliasDecl(const TypeAliasDecl *TAD) {
  426. VisitNamedDecl(TAD);
  427. JOS.attribute("type", createQualType(TAD->getUnderlyingType()));
  428. }
  429. void JSONNodeDumper::VisitNamespaceDecl(const NamespaceDecl *ND) {
  430. VisitNamedDecl(ND);
  431. attributeOnlyIfTrue("isInline", ND->isInline());
  432. if (!ND->isOriginalNamespace())
  433. JOS.attribute("originalNamespace",
  434. createBareDeclRef(ND->getOriginalNamespace()));
  435. }
  436. void JSONNodeDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *UDD) {
  437. JOS.attribute("nominatedNamespace",
  438. createBareDeclRef(UDD->getNominatedNamespace()));
  439. }
  440. void JSONNodeDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *NAD) {
  441. VisitNamedDecl(NAD);
  442. JOS.attribute("aliasedNamespace",
  443. createBareDeclRef(NAD->getAliasedNamespace()));
  444. }
  445. void JSONNodeDumper::VisitUsingDecl(const UsingDecl *UD) {
  446. std::string Name;
  447. if (const NestedNameSpecifier *NNS = UD->getQualifier()) {
  448. llvm::raw_string_ostream SOS(Name);
  449. NNS->print(SOS, UD->getASTContext().getPrintingPolicy());
  450. }
  451. Name += UD->getNameAsString();
  452. JOS.attribute("name", Name);
  453. }
  454. void JSONNodeDumper::VisitUsingShadowDecl(const UsingShadowDecl *USD) {
  455. JOS.attribute("target", createBareDeclRef(USD->getTargetDecl()));
  456. }
  457. void JSONNodeDumper::VisitVarDecl(const VarDecl *VD) {
  458. VisitNamedDecl(VD);
  459. JOS.attribute("type", createQualType(VD->getType()));
  460. StorageClass SC = VD->getStorageClass();
  461. if (SC != SC_None)
  462. JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC));
  463. switch (VD->getTLSKind()) {
  464. case VarDecl::TLS_Dynamic: JOS.attribute("tls", "dynamic"); break;
  465. case VarDecl::TLS_Static: JOS.attribute("tls", "static"); break;
  466. case VarDecl::TLS_None: break;
  467. }
  468. attributeOnlyIfTrue("nrvo", VD->isNRVOVariable());
  469. attributeOnlyIfTrue("inline", VD->isInline());
  470. attributeOnlyIfTrue("constexpr", VD->isConstexpr());
  471. attributeOnlyIfTrue("modulePrivate", VD->isModulePrivate());
  472. if (VD->hasInit()) {
  473. switch (VD->getInitStyle()) {
  474. case VarDecl::CInit: JOS.attribute("init", "c"); break;
  475. case VarDecl::CallInit: JOS.attribute("init", "call"); break;
  476. case VarDecl::ListInit: JOS.attribute("init", "list"); break;
  477. }
  478. }
  479. attributeOnlyIfTrue("isParameterPack", VD->isParameterPack());
  480. }
  481. void JSONNodeDumper::VisitFieldDecl(const FieldDecl *FD) {
  482. VisitNamedDecl(FD);
  483. JOS.attribute("type", createQualType(FD->getType()));
  484. attributeOnlyIfTrue("mutable", FD->isMutable());
  485. attributeOnlyIfTrue("modulePrivate", FD->isModulePrivate());
  486. attributeOnlyIfTrue("isBitfield", FD->isBitField());
  487. attributeOnlyIfTrue("hasInClassInitializer", FD->hasInClassInitializer());
  488. }
  489. void JSONNodeDumper::VisitFunctionDecl(const FunctionDecl *FD) {
  490. VisitNamedDecl(FD);
  491. JOS.attribute("type", createQualType(FD->getType()));
  492. StorageClass SC = FD->getStorageClass();
  493. if (SC != SC_None)
  494. JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC));
  495. attributeOnlyIfTrue("inline", FD->isInlineSpecified());
  496. attributeOnlyIfTrue("virtual", FD->isVirtualAsWritten());
  497. attributeOnlyIfTrue("pure", FD->isPure());
  498. attributeOnlyIfTrue("explicitlyDeleted", FD->isDeletedAsWritten());
  499. attributeOnlyIfTrue("constexpr", FD->isConstexpr());
  500. attributeOnlyIfTrue("variadic", FD->isVariadic());
  501. if (FD->isDefaulted())
  502. JOS.attribute("explicitlyDefaulted",
  503. FD->isDeleted() ? "deleted" : "default");
  504. }
  505. void JSONNodeDumper::VisitEnumDecl(const EnumDecl *ED) {
  506. VisitNamedDecl(ED);
  507. if (ED->isFixed())
  508. JOS.attribute("fixedUnderlyingType", createQualType(ED->getIntegerType()));
  509. if (ED->isScoped())
  510. JOS.attribute("scopedEnumTag",
  511. ED->isScopedUsingClassTag() ? "class" : "struct");
  512. }
  513. void JSONNodeDumper::VisitEnumConstantDecl(const EnumConstantDecl *ECD) {
  514. VisitNamedDecl(ECD);
  515. JOS.attribute("type", createQualType(ECD->getType()));
  516. }
  517. void JSONNodeDumper::VisitRecordDecl(const RecordDecl *RD) {
  518. VisitNamedDecl(RD);
  519. JOS.attribute("tagUsed", RD->getKindName());
  520. attributeOnlyIfTrue("completeDefinition", RD->isCompleteDefinition());
  521. }
  522. void JSONNodeDumper::VisitCXXRecordDecl(const CXXRecordDecl *RD) {
  523. VisitRecordDecl(RD);
  524. // All other information requires a complete definition.
  525. if (!RD->isCompleteDefinition())
  526. return;
  527. JOS.attribute("definitionData", createCXXRecordDefinitionData(RD));
  528. if (RD->getNumBases()) {
  529. JOS.attributeArray("bases", [this, RD] {
  530. for (const auto &Spec : RD->bases())
  531. JOS.value(createCXXBaseSpecifier(Spec));
  532. });
  533. }
  534. }
  535. void JSONNodeDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
  536. VisitNamedDecl(D);
  537. JOS.attribute("tagUsed", D->wasDeclaredWithTypename() ? "typename" : "class");
  538. JOS.attribute("depth", D->getDepth());
  539. JOS.attribute("index", D->getIndex());
  540. attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
  541. if (D->hasDefaultArgument())
  542. JOS.attributeObject("defaultArg", [=] {
  543. Visit(D->getDefaultArgument(), SourceRange(),
  544. D->getDefaultArgStorage().getInheritedFrom(),
  545. D->defaultArgumentWasInherited() ? "inherited from" : "previous");
  546. });
  547. }
  548. void JSONNodeDumper::VisitNonTypeTemplateParmDecl(
  549. const NonTypeTemplateParmDecl *D) {
  550. VisitNamedDecl(D);
  551. JOS.attribute("type", createQualType(D->getType()));
  552. JOS.attribute("depth", D->getDepth());
  553. JOS.attribute("index", D->getIndex());
  554. attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
  555. if (D->hasDefaultArgument())
  556. JOS.attributeObject("defaultArg", [=] {
  557. Visit(D->getDefaultArgument(), SourceRange(),
  558. D->getDefaultArgStorage().getInheritedFrom(),
  559. D->defaultArgumentWasInherited() ? "inherited from" : "previous");
  560. });
  561. }
  562. void JSONNodeDumper::VisitTemplateTemplateParmDecl(
  563. const TemplateTemplateParmDecl *D) {
  564. VisitNamedDecl(D);
  565. JOS.attribute("depth", D->getDepth());
  566. JOS.attribute("index", D->getIndex());
  567. attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
  568. if (D->hasDefaultArgument())
  569. JOS.attributeObject("defaultArg", [=] {
  570. Visit(D->getDefaultArgument().getArgument(),
  571. D->getDefaultArgStorage().getInheritedFrom()->getSourceRange(),
  572. D->getDefaultArgStorage().getInheritedFrom(),
  573. D->defaultArgumentWasInherited() ? "inherited from" : "previous");
  574. });
  575. }
  576. void JSONNodeDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *LSD) {
  577. StringRef Lang;
  578. switch (LSD->getLanguage()) {
  579. case LinkageSpecDecl::lang_c: Lang = "C"; break;
  580. case LinkageSpecDecl::lang_cxx: Lang = "C++"; break;
  581. }
  582. JOS.attribute("language", Lang);
  583. attributeOnlyIfTrue("hasBraces", LSD->hasBraces());
  584. }
  585. void JSONNodeDumper::VisitAccessSpecDecl(const AccessSpecDecl *ASD) {
  586. JOS.attribute("access", createAccessSpecifier(ASD->getAccess()));
  587. }
  588. void JSONNodeDumper::VisitFriendDecl(const FriendDecl *FD) {
  589. if (const TypeSourceInfo *T = FD->getFriendType())
  590. JOS.attribute("type", createQualType(T->getType()));
  591. }
  592. void JSONNodeDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
  593. VisitNamedDecl(D);
  594. JOS.attribute("type", createQualType(D->getType()));
  595. attributeOnlyIfTrue("synthesized", D->getSynthesize());
  596. switch (D->getAccessControl()) {
  597. case ObjCIvarDecl::None: JOS.attribute("access", "none"); break;
  598. case ObjCIvarDecl::Private: JOS.attribute("access", "private"); break;
  599. case ObjCIvarDecl::Protected: JOS.attribute("access", "protected"); break;
  600. case ObjCIvarDecl::Public: JOS.attribute("access", "public"); break;
  601. case ObjCIvarDecl::Package: JOS.attribute("access", "package"); break;
  602. }
  603. }
  604. void JSONNodeDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
  605. VisitNamedDecl(D);
  606. JOS.attribute("returnType", createQualType(D->getReturnType()));
  607. JOS.attribute("instance", D->isInstanceMethod());
  608. attributeOnlyIfTrue("variadic", D->isVariadic());
  609. }
  610. void JSONNodeDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) {
  611. VisitNamedDecl(D);
  612. JOS.attribute("type", createQualType(D->getUnderlyingType()));
  613. attributeOnlyIfTrue("bounded", D->hasExplicitBound());
  614. switch (D->getVariance()) {
  615. case ObjCTypeParamVariance::Invariant:
  616. break;
  617. case ObjCTypeParamVariance::Covariant:
  618. JOS.attribute("variance", "covariant");
  619. break;
  620. case ObjCTypeParamVariance::Contravariant:
  621. JOS.attribute("variance", "contravariant");
  622. break;
  623. }
  624. }
  625. void JSONNodeDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
  626. VisitNamedDecl(D);
  627. JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
  628. JOS.attribute("implementation", createBareDeclRef(D->getImplementation()));
  629. llvm::json::Array Protocols;
  630. for (const auto* P : D->protocols())
  631. Protocols.push_back(createBareDeclRef(P));
  632. if (!Protocols.empty())
  633. JOS.attribute("protocols", std::move(Protocols));
  634. }
  635. void JSONNodeDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
  636. VisitNamedDecl(D);
  637. JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
  638. JOS.attribute("categoryDecl", createBareDeclRef(D->getCategoryDecl()));
  639. }
  640. void JSONNodeDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
  641. VisitNamedDecl(D);
  642. llvm::json::Array Protocols;
  643. for (const auto *P : D->protocols())
  644. Protocols.push_back(createBareDeclRef(P));
  645. if (!Protocols.empty())
  646. JOS.attribute("protocols", std::move(Protocols));
  647. }
  648. void JSONNodeDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
  649. VisitNamedDecl(D);
  650. JOS.attribute("super", createBareDeclRef(D->getSuperClass()));
  651. JOS.attribute("implementation", createBareDeclRef(D->getImplementation()));
  652. llvm::json::Array Protocols;
  653. for (const auto* P : D->protocols())
  654. Protocols.push_back(createBareDeclRef(P));
  655. if (!Protocols.empty())
  656. JOS.attribute("protocols", std::move(Protocols));
  657. }
  658. void JSONNodeDumper::VisitObjCImplementationDecl(
  659. const ObjCImplementationDecl *D) {
  660. VisitNamedDecl(D);
  661. JOS.attribute("super", createBareDeclRef(D->getSuperClass()));
  662. JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
  663. }
  664. void JSONNodeDumper::VisitObjCCompatibleAliasDecl(
  665. const ObjCCompatibleAliasDecl *D) {
  666. VisitNamedDecl(D);
  667. JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
  668. }
  669. void JSONNodeDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
  670. VisitNamedDecl(D);
  671. JOS.attribute("type", createQualType(D->getType()));
  672. switch (D->getPropertyImplementation()) {
  673. case ObjCPropertyDecl::None: break;
  674. case ObjCPropertyDecl::Required: JOS.attribute("control", "required"); break;
  675. case ObjCPropertyDecl::Optional: JOS.attribute("control", "optional"); break;
  676. }
  677. ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
  678. if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
  679. if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
  680. JOS.attribute("getter", createBareDeclRef(D->getGetterMethodDecl()));
  681. if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
  682. JOS.attribute("setter", createBareDeclRef(D->getSetterMethodDecl()));
  683. attributeOnlyIfTrue("readonly", Attrs & ObjCPropertyDecl::OBJC_PR_readonly);
  684. attributeOnlyIfTrue("assign", Attrs & ObjCPropertyDecl::OBJC_PR_assign);
  685. attributeOnlyIfTrue("readwrite",
  686. Attrs & ObjCPropertyDecl::OBJC_PR_readwrite);
  687. attributeOnlyIfTrue("retain", Attrs & ObjCPropertyDecl::OBJC_PR_retain);
  688. attributeOnlyIfTrue("copy", Attrs & ObjCPropertyDecl::OBJC_PR_copy);
  689. attributeOnlyIfTrue("nonatomic",
  690. Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic);
  691. attributeOnlyIfTrue("atomic", Attrs & ObjCPropertyDecl::OBJC_PR_atomic);
  692. attributeOnlyIfTrue("weak", Attrs & ObjCPropertyDecl::OBJC_PR_weak);
  693. attributeOnlyIfTrue("strong", Attrs & ObjCPropertyDecl::OBJC_PR_strong);
  694. attributeOnlyIfTrue("unsafe_unretained",
  695. Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
  696. attributeOnlyIfTrue("class", Attrs & ObjCPropertyDecl::OBJC_PR_class);
  697. attributeOnlyIfTrue("nullability",
  698. Attrs & ObjCPropertyDecl::OBJC_PR_nullability);
  699. attributeOnlyIfTrue("null_resettable",
  700. Attrs & ObjCPropertyDecl::OBJC_PR_null_resettable);
  701. }
  702. }
  703. void JSONNodeDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
  704. VisitNamedDecl(D->getPropertyDecl());
  705. JOS.attribute("implKind", D->getPropertyImplementation() ==
  706. ObjCPropertyImplDecl::Synthesize
  707. ? "synthesize"
  708. : "dynamic");
  709. JOS.attribute("propertyDecl", createBareDeclRef(D->getPropertyDecl()));
  710. JOS.attribute("ivarDecl", createBareDeclRef(D->getPropertyIvarDecl()));
  711. }
  712. void JSONNodeDumper::VisitBlockDecl(const BlockDecl *D) {
  713. attributeOnlyIfTrue("variadic", D->isVariadic());
  714. attributeOnlyIfTrue("capturesThis", D->capturesCXXThis());
  715. }
  716. void JSONNodeDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *OEE) {
  717. JOS.attribute("encodedType", createQualType(OEE->getEncodedType()));
  718. }
  719. void JSONNodeDumper::VisitDeclRefExpr(const DeclRefExpr *DRE) {
  720. JOS.attribute("referencedDecl", createBareDeclRef(DRE->getDecl()));
  721. if (DRE->getDecl() != DRE->getFoundDecl())
  722. JOS.attribute("foundReferencedDecl",
  723. createBareDeclRef(DRE->getFoundDecl()));
  724. switch (DRE->isNonOdrUse()) {
  725. case NOUR_None: break;
  726. case NOUR_Unevaluated: JOS.attribute("nonOdrUseReason", "unevaluated"); break;
  727. case NOUR_Constant: JOS.attribute("nonOdrUseReason", "constant"); break;
  728. case NOUR_Discarded: JOS.attribute("nonOdrUseReason", "discarded"); break;
  729. }
  730. }
  731. void JSONNodeDumper::VisitPredefinedExpr(const PredefinedExpr *PE) {
  732. JOS.attribute("name", PredefinedExpr::getIdentKindName(PE->getIdentKind()));
  733. }
  734. void JSONNodeDumper::VisitUnaryOperator(const UnaryOperator *UO) {
  735. JOS.attribute("isPostfix", UO->isPostfix());
  736. JOS.attribute("opcode", UnaryOperator::getOpcodeStr(UO->getOpcode()));
  737. if (!UO->canOverflow())
  738. JOS.attribute("canOverflow", false);
  739. }
  740. void JSONNodeDumper::VisitBinaryOperator(const BinaryOperator *BO) {
  741. JOS.attribute("opcode", BinaryOperator::getOpcodeStr(BO->getOpcode()));
  742. }
  743. void JSONNodeDumper::VisitCompoundAssignOperator(
  744. const CompoundAssignOperator *CAO) {
  745. VisitBinaryOperator(CAO);
  746. JOS.attribute("computeLHSType", createQualType(CAO->getComputationLHSType()));
  747. JOS.attribute("computeResultType",
  748. createQualType(CAO->getComputationResultType()));
  749. }
  750. void JSONNodeDumper::VisitMemberExpr(const MemberExpr *ME) {
  751. // Note, we always write this Boolean field because the information it conveys
  752. // is critical to understanding the AST node.
  753. ValueDecl *VD = ME->getMemberDecl();
  754. JOS.attribute("name", VD && VD->getDeclName() ? VD->getNameAsString() : "");
  755. JOS.attribute("isArrow", ME->isArrow());
  756. JOS.attribute("referencedMemberDecl", createPointerRepresentation(VD));
  757. switch (ME->isNonOdrUse()) {
  758. case NOUR_None: break;
  759. case NOUR_Unevaluated: JOS.attribute("nonOdrUseReason", "unevaluated"); break;
  760. case NOUR_Constant: JOS.attribute("nonOdrUseReason", "constant"); break;
  761. case NOUR_Discarded: JOS.attribute("nonOdrUseReason", "discarded"); break;
  762. }
  763. }
  764. void JSONNodeDumper::VisitCXXNewExpr(const CXXNewExpr *NE) {
  765. attributeOnlyIfTrue("isGlobal", NE->isGlobalNew());
  766. attributeOnlyIfTrue("isArray", NE->isArray());
  767. attributeOnlyIfTrue("isPlacement", NE->getNumPlacementArgs() != 0);
  768. switch (NE->getInitializationStyle()) {
  769. case CXXNewExpr::NoInit: break;
  770. case CXXNewExpr::CallInit: JOS.attribute("initStyle", "call"); break;
  771. case CXXNewExpr::ListInit: JOS.attribute("initStyle", "list"); break;
  772. }
  773. if (const FunctionDecl *FD = NE->getOperatorNew())
  774. JOS.attribute("operatorNewDecl", createBareDeclRef(FD));
  775. if (const FunctionDecl *FD = NE->getOperatorDelete())
  776. JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD));
  777. }
  778. void JSONNodeDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *DE) {
  779. attributeOnlyIfTrue("isGlobal", DE->isGlobalDelete());
  780. attributeOnlyIfTrue("isArray", DE->isArrayForm());
  781. attributeOnlyIfTrue("isArrayAsWritten", DE->isArrayFormAsWritten());
  782. if (const FunctionDecl *FD = DE->getOperatorDelete())
  783. JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD));
  784. }
  785. void JSONNodeDumper::VisitCXXThisExpr(const CXXThisExpr *TE) {
  786. attributeOnlyIfTrue("implicit", TE->isImplicit());
  787. }
  788. void JSONNodeDumper::VisitCastExpr(const CastExpr *CE) {
  789. JOS.attribute("castKind", CE->getCastKindName());
  790. llvm::json::Array Path = createCastPath(CE);
  791. if (!Path.empty())
  792. JOS.attribute("path", std::move(Path));
  793. // FIXME: This may not be useful information as it can be obtusely gleaned
  794. // from the inner[] array.
  795. if (const NamedDecl *ND = CE->getConversionFunction())
  796. JOS.attribute("conversionFunc", createBareDeclRef(ND));
  797. }
  798. void JSONNodeDumper::VisitImplicitCastExpr(const ImplicitCastExpr *ICE) {
  799. VisitCastExpr(ICE);
  800. attributeOnlyIfTrue("isPartOfExplicitCast", ICE->isPartOfExplicitCast());
  801. }
  802. void JSONNodeDumper::VisitCallExpr(const CallExpr *CE) {
  803. attributeOnlyIfTrue("adl", CE->usesADL());
  804. }
  805. void JSONNodeDumper::VisitUnaryExprOrTypeTraitExpr(
  806. const UnaryExprOrTypeTraitExpr *TTE) {
  807. switch (TTE->getKind()) {
  808. case UETT_SizeOf: JOS.attribute("name", "sizeof"); break;
  809. case UETT_AlignOf: JOS.attribute("name", "alignof"); break;
  810. case UETT_VecStep: JOS.attribute("name", "vec_step"); break;
  811. case UETT_PreferredAlignOf: JOS.attribute("name", "__alignof"); break;
  812. case UETT_OpenMPRequiredSimdAlign:
  813. JOS.attribute("name", "__builtin_omp_required_simd_align"); break;
  814. }
  815. if (TTE->isArgumentType())
  816. JOS.attribute("argType", createQualType(TTE->getArgumentType()));
  817. }
  818. void JSONNodeDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *SOPE) {
  819. VisitNamedDecl(SOPE->getPack());
  820. }
  821. void JSONNodeDumper::VisitUnresolvedLookupExpr(
  822. const UnresolvedLookupExpr *ULE) {
  823. JOS.attribute("usesADL", ULE->requiresADL());
  824. JOS.attribute("name", ULE->getName().getAsString());
  825. JOS.attributeArray("lookups", [this, ULE] {
  826. for (const NamedDecl *D : ULE->decls())
  827. JOS.value(createBareDeclRef(D));
  828. });
  829. }
  830. void JSONNodeDumper::VisitAddrLabelExpr(const AddrLabelExpr *ALE) {
  831. JOS.attribute("name", ALE->getLabel()->getName());
  832. JOS.attribute("labelDeclId", createPointerRepresentation(ALE->getLabel()));
  833. }
  834. void JSONNodeDumper::VisitCXXTypeidExpr(const CXXTypeidExpr *CTE) {
  835. if (CTE->isTypeOperand()) {
  836. QualType Adjusted = CTE->getTypeOperand(Ctx);
  837. QualType Unadjusted = CTE->getTypeOperandSourceInfo()->getType();
  838. JOS.attribute("typeArg", createQualType(Unadjusted));
  839. if (Adjusted != Unadjusted)
  840. JOS.attribute("adjustedTypeArg", createQualType(Adjusted));
  841. }
  842. }
  843. void JSONNodeDumper::VisitConstantExpr(const ConstantExpr *CE) {
  844. if (CE->getResultAPValueKind() != APValue::None) {
  845. std::string Str;
  846. llvm::raw_string_ostream OS(Str);
  847. CE->getAPValueResult().printPretty(OS, Ctx, CE->getType());
  848. JOS.attribute("value", OS.str());
  849. }
  850. }
  851. void JSONNodeDumper::VisitInitListExpr(const InitListExpr *ILE) {
  852. if (const FieldDecl *FD = ILE->getInitializedFieldInUnion())
  853. JOS.attribute("field", createBareDeclRef(FD));
  854. }
  855. void JSONNodeDumper::VisitGenericSelectionExpr(
  856. const GenericSelectionExpr *GSE) {
  857. attributeOnlyIfTrue("resultDependent", GSE->isResultDependent());
  858. }
  859. void JSONNodeDumper::VisitCXXUnresolvedConstructExpr(
  860. const CXXUnresolvedConstructExpr *UCE) {
  861. if (UCE->getType() != UCE->getTypeAsWritten())
  862. JOS.attribute("typeAsWritten", createQualType(UCE->getTypeAsWritten()));
  863. attributeOnlyIfTrue("list", UCE->isListInitialization());
  864. }
  865. void JSONNodeDumper::VisitCXXConstructExpr(const CXXConstructExpr *CE) {
  866. CXXConstructorDecl *Ctor = CE->getConstructor();
  867. JOS.attribute("ctorType", createQualType(Ctor->getType()));
  868. attributeOnlyIfTrue("elidable", CE->isElidable());
  869. attributeOnlyIfTrue("list", CE->isListInitialization());
  870. attributeOnlyIfTrue("initializer_list", CE->isStdInitListInitialization());
  871. attributeOnlyIfTrue("zeroing", CE->requiresZeroInitialization());
  872. attributeOnlyIfTrue("hadMultipleCandidates", CE->hadMultipleCandidates());
  873. switch (CE->getConstructionKind()) {
  874. case CXXConstructExpr::CK_Complete:
  875. JOS.attribute("constructionKind", "complete");
  876. break;
  877. case CXXConstructExpr::CK_Delegating:
  878. JOS.attribute("constructionKind", "delegating");
  879. break;
  880. case CXXConstructExpr::CK_NonVirtualBase:
  881. JOS.attribute("constructionKind", "non-virtual base");
  882. break;
  883. case CXXConstructExpr::CK_VirtualBase:
  884. JOS.attribute("constructionKind", "virtual base");
  885. break;
  886. }
  887. }
  888. void JSONNodeDumper::VisitExprWithCleanups(const ExprWithCleanups *EWC) {
  889. attributeOnlyIfTrue("cleanupsHaveSideEffects",
  890. EWC->cleanupsHaveSideEffects());
  891. if (EWC->getNumObjects()) {
  892. JOS.attributeArray("cleanups", [this, EWC] {
  893. for (const ExprWithCleanups::CleanupObject &CO : EWC->getObjects())
  894. JOS.value(createBareDeclRef(CO));
  895. });
  896. }
  897. }
  898. void JSONNodeDumper::VisitCXXBindTemporaryExpr(
  899. const CXXBindTemporaryExpr *BTE) {
  900. const CXXTemporary *Temp = BTE->getTemporary();
  901. JOS.attribute("temp", createPointerRepresentation(Temp));
  902. if (const CXXDestructorDecl *Dtor = Temp->getDestructor())
  903. JOS.attribute("dtor", createBareDeclRef(Dtor));
  904. }
  905. void JSONNodeDumper::VisitMaterializeTemporaryExpr(
  906. const MaterializeTemporaryExpr *MTE) {
  907. if (const ValueDecl *VD = MTE->getExtendingDecl())
  908. JOS.attribute("extendingDecl", createBareDeclRef(VD));
  909. switch (MTE->getStorageDuration()) {
  910. case SD_Automatic:
  911. JOS.attribute("storageDuration", "automatic");
  912. break;
  913. case SD_Dynamic:
  914. JOS.attribute("storageDuration", "dynamic");
  915. break;
  916. case SD_FullExpression:
  917. JOS.attribute("storageDuration", "full expression");
  918. break;
  919. case SD_Static:
  920. JOS.attribute("storageDuration", "static");
  921. break;
  922. case SD_Thread:
  923. JOS.attribute("storageDuration", "thread");
  924. break;
  925. }
  926. attributeOnlyIfTrue("boundToLValueRef", MTE->isBoundToLvalueReference());
  927. }
  928. void JSONNodeDumper::VisitCXXDependentScopeMemberExpr(
  929. const CXXDependentScopeMemberExpr *DSME) {
  930. JOS.attribute("isArrow", DSME->isArrow());
  931. JOS.attribute("member", DSME->getMember().getAsString());
  932. attributeOnlyIfTrue("hasTemplateKeyword", DSME->hasTemplateKeyword());
  933. attributeOnlyIfTrue("hasExplicitTemplateArgs",
  934. DSME->hasExplicitTemplateArgs());
  935. if (DSME->getNumTemplateArgs()) {
  936. JOS.attributeArray("explicitTemplateArgs", [DSME, this] {
  937. for (const TemplateArgumentLoc &TAL : DSME->template_arguments())
  938. JOS.object(
  939. [&TAL, this] { Visit(TAL.getArgument(), TAL.getSourceRange()); });
  940. });
  941. }
  942. }
  943. void JSONNodeDumper::VisitIntegerLiteral(const IntegerLiteral *IL) {
  944. JOS.attribute("value",
  945. IL->getValue().toString(
  946. /*Radix=*/10, IL->getType()->isSignedIntegerType()));
  947. }
  948. void JSONNodeDumper::VisitCharacterLiteral(const CharacterLiteral *CL) {
  949. // FIXME: This should probably print the character literal as a string,
  950. // rather than as a numerical value. It would be nice if the behavior matched
  951. // what we do to print a string literal; right now, it is impossible to tell
  952. // the difference between 'a' and L'a' in C from the JSON output.
  953. JOS.attribute("value", CL->getValue());
  954. }
  955. void JSONNodeDumper::VisitFixedPointLiteral(const FixedPointLiteral *FPL) {
  956. JOS.attribute("value", FPL->getValueAsString(/*Radix=*/10));
  957. }
  958. void JSONNodeDumper::VisitFloatingLiteral(const FloatingLiteral *FL) {
  959. JOS.attribute("value", FL->getValueAsApproximateDouble());
  960. }
  961. void JSONNodeDumper::VisitStringLiteral(const StringLiteral *SL) {
  962. std::string Buffer;
  963. llvm::raw_string_ostream SS(Buffer);
  964. SL->outputString(SS);
  965. JOS.attribute("value", SS.str());
  966. }
  967. void JSONNodeDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *BLE) {
  968. JOS.attribute("value", BLE->getValue());
  969. }
  970. void JSONNodeDumper::VisitIfStmt(const IfStmt *IS) {
  971. attributeOnlyIfTrue("hasInit", IS->hasInitStorage());
  972. attributeOnlyIfTrue("hasVar", IS->hasVarStorage());
  973. attributeOnlyIfTrue("hasElse", IS->hasElseStorage());
  974. attributeOnlyIfTrue("isConstexpr", IS->isConstexpr());
  975. }
  976. void JSONNodeDumper::VisitSwitchStmt(const SwitchStmt *SS) {
  977. attributeOnlyIfTrue("hasInit", SS->hasInitStorage());
  978. attributeOnlyIfTrue("hasVar", SS->hasVarStorage());
  979. }
  980. void JSONNodeDumper::VisitCaseStmt(const CaseStmt *CS) {
  981. attributeOnlyIfTrue("isGNURange", CS->caseStmtIsGNURange());
  982. }
  983. void JSONNodeDumper::VisitLabelStmt(const LabelStmt *LS) {
  984. JOS.attribute("name", LS->getName());
  985. JOS.attribute("declId", createPointerRepresentation(LS->getDecl()));
  986. }
  987. void JSONNodeDumper::VisitGotoStmt(const GotoStmt *GS) {
  988. JOS.attribute("targetLabelDeclId",
  989. createPointerRepresentation(GS->getLabel()));
  990. }
  991. void JSONNodeDumper::VisitWhileStmt(const WhileStmt *WS) {
  992. attributeOnlyIfTrue("hasVar", WS->hasVarStorage());
  993. }
  994. void JSONNodeDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt* OACS) {
  995. // FIXME: it would be nice for the ASTNodeTraverser would handle the catch
  996. // parameter the same way for C++ and ObjC rather. In this case, C++ gets a
  997. // null child node and ObjC gets no child node.
  998. attributeOnlyIfTrue("isCatchAll", OACS->getCatchParamDecl() == nullptr);
  999. }
  1000. void JSONNodeDumper::VisitNullTemplateArgument(const TemplateArgument &TA) {
  1001. JOS.attribute("isNull", true);
  1002. }
  1003. void JSONNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) {
  1004. JOS.attribute("type", createQualType(TA.getAsType()));
  1005. }
  1006. void JSONNodeDumper::VisitDeclarationTemplateArgument(
  1007. const TemplateArgument &TA) {
  1008. JOS.attribute("decl", createBareDeclRef(TA.getAsDecl()));
  1009. }
  1010. void JSONNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) {
  1011. JOS.attribute("isNullptr", true);
  1012. }
  1013. void JSONNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) {
  1014. JOS.attribute("value", TA.getAsIntegral().getSExtValue());
  1015. }
  1016. void JSONNodeDumper::VisitTemplateTemplateArgument(const TemplateArgument &TA) {
  1017. // FIXME: cannot just call dump() on the argument, as that doesn't specify
  1018. // the output format.
  1019. }
  1020. void JSONNodeDumper::VisitTemplateExpansionTemplateArgument(
  1021. const TemplateArgument &TA) {
  1022. // FIXME: cannot just call dump() on the argument, as that doesn't specify
  1023. // the output format.
  1024. }
  1025. void JSONNodeDumper::VisitExpressionTemplateArgument(
  1026. const TemplateArgument &TA) {
  1027. JOS.attribute("isExpr", true);
  1028. }
  1029. void JSONNodeDumper::VisitPackTemplateArgument(const TemplateArgument &TA) {
  1030. JOS.attribute("isPack", true);
  1031. }
  1032. StringRef JSONNodeDumper::getCommentCommandName(unsigned CommandID) const {
  1033. if (Traits)
  1034. return Traits->getCommandInfo(CommandID)->Name;
  1035. if (const comments::CommandInfo *Info =
  1036. comments::CommandTraits::getBuiltinCommandInfo(CommandID))
  1037. return Info->Name;
  1038. return "<invalid>";
  1039. }
  1040. void JSONNodeDumper::visitTextComment(const comments::TextComment *C,
  1041. const comments::FullComment *) {
  1042. JOS.attribute("text", C->getText());
  1043. }
  1044. void JSONNodeDumper::visitInlineCommandComment(
  1045. const comments::InlineCommandComment *C, const comments::FullComment *) {
  1046. JOS.attribute("name", getCommentCommandName(C->getCommandID()));
  1047. switch (C->getRenderKind()) {
  1048. case comments::InlineCommandComment::RenderNormal:
  1049. JOS.attribute("renderKind", "normal");
  1050. break;
  1051. case comments::InlineCommandComment::RenderBold:
  1052. JOS.attribute("renderKind", "bold");
  1053. break;
  1054. case comments::InlineCommandComment::RenderEmphasized:
  1055. JOS.attribute("renderKind", "emphasized");
  1056. break;
  1057. case comments::InlineCommandComment::RenderMonospaced:
  1058. JOS.attribute("renderKind", "monospaced");
  1059. break;
  1060. }
  1061. llvm::json::Array Args;
  1062. for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I)
  1063. Args.push_back(C->getArgText(I));
  1064. if (!Args.empty())
  1065. JOS.attribute("args", std::move(Args));
  1066. }
  1067. void JSONNodeDumper::visitHTMLStartTagComment(
  1068. const comments::HTMLStartTagComment *C, const comments::FullComment *) {
  1069. JOS.attribute("name", C->getTagName());
  1070. attributeOnlyIfTrue("selfClosing", C->isSelfClosing());
  1071. attributeOnlyIfTrue("malformed", C->isMalformed());
  1072. llvm::json::Array Attrs;
  1073. for (unsigned I = 0, E = C->getNumAttrs(); I < E; ++I)
  1074. Attrs.push_back(
  1075. {{"name", C->getAttr(I).Name}, {"value", C->getAttr(I).Value}});
  1076. if (!Attrs.empty())
  1077. JOS.attribute("attrs", std::move(Attrs));
  1078. }
  1079. void JSONNodeDumper::visitHTMLEndTagComment(
  1080. const comments::HTMLEndTagComment *C, const comments::FullComment *) {
  1081. JOS.attribute("name", C->getTagName());
  1082. }
  1083. void JSONNodeDumper::visitBlockCommandComment(
  1084. const comments::BlockCommandComment *C, const comments::FullComment *) {
  1085. JOS.attribute("name", getCommentCommandName(C->getCommandID()));
  1086. llvm::json::Array Args;
  1087. for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I)
  1088. Args.push_back(C->getArgText(I));
  1089. if (!Args.empty())
  1090. JOS.attribute("args", std::move(Args));
  1091. }
  1092. void JSONNodeDumper::visitParamCommandComment(
  1093. const comments::ParamCommandComment *C, const comments::FullComment *FC) {
  1094. switch (C->getDirection()) {
  1095. case comments::ParamCommandComment::In:
  1096. JOS.attribute("direction", "in");
  1097. break;
  1098. case comments::ParamCommandComment::Out:
  1099. JOS.attribute("direction", "out");
  1100. break;
  1101. case comments::ParamCommandComment::InOut:
  1102. JOS.attribute("direction", "in,out");
  1103. break;
  1104. }
  1105. attributeOnlyIfTrue("explicit", C->isDirectionExplicit());
  1106. if (C->hasParamName())
  1107. JOS.attribute("param", C->isParamIndexValid() ? C->getParamName(FC)
  1108. : C->getParamNameAsWritten());
  1109. if (C->isParamIndexValid() && !C->isVarArgParam())
  1110. JOS.attribute("paramIdx", C->getParamIndex());
  1111. }
  1112. void JSONNodeDumper::visitTParamCommandComment(
  1113. const comments::TParamCommandComment *C, const comments::FullComment *FC) {
  1114. if (C->hasParamName())
  1115. JOS.attribute("param", C->isPositionValid() ? C->getParamName(FC)
  1116. : C->getParamNameAsWritten());
  1117. if (C->isPositionValid()) {
  1118. llvm::json::Array Positions;
  1119. for (unsigned I = 0, E = C->getDepth(); I < E; ++I)
  1120. Positions.push_back(C->getIndex(I));
  1121. if (!Positions.empty())
  1122. JOS.attribute("positions", std::move(Positions));
  1123. }
  1124. }
  1125. void JSONNodeDumper::visitVerbatimBlockComment(
  1126. const comments::VerbatimBlockComment *C, const comments::FullComment *) {
  1127. JOS.attribute("name", getCommentCommandName(C->getCommandID()));
  1128. JOS.attribute("closeName", C->getCloseName());
  1129. }
  1130. void JSONNodeDumper::visitVerbatimBlockLineComment(
  1131. const comments::VerbatimBlockLineComment *C,
  1132. const comments::FullComment *) {
  1133. JOS.attribute("text", C->getText());
  1134. }
  1135. void JSONNodeDumper::visitVerbatimLineComment(
  1136. const comments::VerbatimLineComment *C, const comments::FullComment *) {
  1137. JOS.attribute("text", C->getText());
  1138. }