JSONNodeDumper.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. case EST_NoThrow:
  393. JOS.attribute("exceptionSpec", "nothrow");
  394. break;
  395. // FIXME: I cannot find a way to trigger these cases while dumping the AST. I
  396. // suspect you can only run into them when executing an AST dump from within
  397. // the debugger, which is not a use case we worry about for the JSON dumping
  398. // feature.
  399. case EST_DependentNoexcept:
  400. case EST_Unevaluated:
  401. case EST_Uninstantiated:
  402. case EST_Unparsed:
  403. case EST_None: break;
  404. }
  405. VisitFunctionType(T);
  406. }
  407. void JSONNodeDumper::VisitNamedDecl(const NamedDecl *ND) {
  408. if (ND && ND->getDeclName())
  409. JOS.attribute("name", ND->getNameAsString());
  410. }
  411. void JSONNodeDumper::VisitTypedefDecl(const TypedefDecl *TD) {
  412. VisitNamedDecl(TD);
  413. JOS.attribute("type", createQualType(TD->getUnderlyingType()));
  414. }
  415. void JSONNodeDumper::VisitTypeAliasDecl(const TypeAliasDecl *TAD) {
  416. VisitNamedDecl(TAD);
  417. JOS.attribute("type", createQualType(TAD->getUnderlyingType()));
  418. }
  419. void JSONNodeDumper::VisitNamespaceDecl(const NamespaceDecl *ND) {
  420. VisitNamedDecl(ND);
  421. attributeOnlyIfTrue("isInline", ND->isInline());
  422. if (!ND->isOriginalNamespace())
  423. JOS.attribute("originalNamespace",
  424. createBareDeclRef(ND->getOriginalNamespace()));
  425. }
  426. void JSONNodeDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *UDD) {
  427. JOS.attribute("nominatedNamespace",
  428. createBareDeclRef(UDD->getNominatedNamespace()));
  429. }
  430. void JSONNodeDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *NAD) {
  431. VisitNamedDecl(NAD);
  432. JOS.attribute("aliasedNamespace",
  433. createBareDeclRef(NAD->getAliasedNamespace()));
  434. }
  435. void JSONNodeDumper::VisitUsingDecl(const UsingDecl *UD) {
  436. std::string Name;
  437. if (const NestedNameSpecifier *NNS = UD->getQualifier()) {
  438. llvm::raw_string_ostream SOS(Name);
  439. NNS->print(SOS, UD->getASTContext().getPrintingPolicy());
  440. }
  441. Name += UD->getNameAsString();
  442. JOS.attribute("name", Name);
  443. }
  444. void JSONNodeDumper::VisitUsingShadowDecl(const UsingShadowDecl *USD) {
  445. JOS.attribute("target", createBareDeclRef(USD->getTargetDecl()));
  446. }
  447. void JSONNodeDumper::VisitVarDecl(const VarDecl *VD) {
  448. VisitNamedDecl(VD);
  449. JOS.attribute("type", createQualType(VD->getType()));
  450. StorageClass SC = VD->getStorageClass();
  451. if (SC != SC_None)
  452. JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC));
  453. switch (VD->getTLSKind()) {
  454. case VarDecl::TLS_Dynamic: JOS.attribute("tls", "dynamic"); break;
  455. case VarDecl::TLS_Static: JOS.attribute("tls", "static"); break;
  456. case VarDecl::TLS_None: break;
  457. }
  458. attributeOnlyIfTrue("nrvo", VD->isNRVOVariable());
  459. attributeOnlyIfTrue("inline", VD->isInline());
  460. attributeOnlyIfTrue("constexpr", VD->isConstexpr());
  461. attributeOnlyIfTrue("modulePrivate", VD->isModulePrivate());
  462. if (VD->hasInit()) {
  463. switch (VD->getInitStyle()) {
  464. case VarDecl::CInit: JOS.attribute("init", "c"); break;
  465. case VarDecl::CallInit: JOS.attribute("init", "call"); break;
  466. case VarDecl::ListInit: JOS.attribute("init", "list"); break;
  467. }
  468. }
  469. attributeOnlyIfTrue("isParameterPack", VD->isParameterPack());
  470. }
  471. void JSONNodeDumper::VisitFieldDecl(const FieldDecl *FD) {
  472. VisitNamedDecl(FD);
  473. JOS.attribute("type", createQualType(FD->getType()));
  474. attributeOnlyIfTrue("mutable", FD->isMutable());
  475. attributeOnlyIfTrue("modulePrivate", FD->isModulePrivate());
  476. attributeOnlyIfTrue("isBitfield", FD->isBitField());
  477. attributeOnlyIfTrue("hasInClassInitializer", FD->hasInClassInitializer());
  478. }
  479. void JSONNodeDumper::VisitFunctionDecl(const FunctionDecl *FD) {
  480. VisitNamedDecl(FD);
  481. JOS.attribute("type", createQualType(FD->getType()));
  482. StorageClass SC = FD->getStorageClass();
  483. if (SC != SC_None)
  484. JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC));
  485. attributeOnlyIfTrue("inline", FD->isInlineSpecified());
  486. attributeOnlyIfTrue("virtual", FD->isVirtualAsWritten());
  487. attributeOnlyIfTrue("pure", FD->isPure());
  488. attributeOnlyIfTrue("explicitlyDeleted", FD->isDeletedAsWritten());
  489. attributeOnlyIfTrue("constexpr", FD->isConstexpr());
  490. attributeOnlyIfTrue("variadic", FD->isVariadic());
  491. if (FD->isDefaulted())
  492. JOS.attribute("explicitlyDefaulted",
  493. FD->isDeleted() ? "deleted" : "default");
  494. }
  495. void JSONNodeDumper::VisitEnumDecl(const EnumDecl *ED) {
  496. VisitNamedDecl(ED);
  497. if (ED->isFixed())
  498. JOS.attribute("fixedUnderlyingType", createQualType(ED->getIntegerType()));
  499. if (ED->isScoped())
  500. JOS.attribute("scopedEnumTag",
  501. ED->isScopedUsingClassTag() ? "class" : "struct");
  502. }
  503. void JSONNodeDumper::VisitEnumConstantDecl(const EnumConstantDecl *ECD) {
  504. VisitNamedDecl(ECD);
  505. JOS.attribute("type", createQualType(ECD->getType()));
  506. }
  507. void JSONNodeDumper::VisitRecordDecl(const RecordDecl *RD) {
  508. VisitNamedDecl(RD);
  509. JOS.attribute("tagUsed", RD->getKindName());
  510. attributeOnlyIfTrue("completeDefinition", RD->isCompleteDefinition());
  511. }
  512. void JSONNodeDumper::VisitCXXRecordDecl(const CXXRecordDecl *RD) {
  513. VisitRecordDecl(RD);
  514. // All other information requires a complete definition.
  515. if (!RD->isCompleteDefinition())
  516. return;
  517. JOS.attribute("definitionData", createCXXRecordDefinitionData(RD));
  518. if (RD->getNumBases()) {
  519. JOS.attributeArray("bases", [this, RD] {
  520. for (const auto &Spec : RD->bases())
  521. JOS.value(createCXXBaseSpecifier(Spec));
  522. });
  523. }
  524. }
  525. void JSONNodeDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
  526. VisitNamedDecl(D);
  527. JOS.attribute("tagUsed", D->wasDeclaredWithTypename() ? "typename" : "class");
  528. JOS.attribute("depth", D->getDepth());
  529. JOS.attribute("index", D->getIndex());
  530. attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
  531. }
  532. void JSONNodeDumper::VisitNonTypeTemplateParmDecl(
  533. const NonTypeTemplateParmDecl *D) {
  534. VisitNamedDecl(D);
  535. JOS.attribute("type", createQualType(D->getType()));
  536. JOS.attribute("depth", D->getDepth());
  537. JOS.attribute("index", D->getIndex());
  538. attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
  539. }
  540. void JSONNodeDumper::VisitTemplateTemplateParmDecl(
  541. const TemplateTemplateParmDecl *D) {
  542. VisitNamedDecl(D);
  543. JOS.attribute("depth", D->getDepth());
  544. JOS.attribute("index", D->getIndex());
  545. attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
  546. }
  547. void JSONNodeDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *LSD) {
  548. StringRef Lang;
  549. switch (LSD->getLanguage()) {
  550. case LinkageSpecDecl::lang_c: Lang = "C"; break;
  551. case LinkageSpecDecl::lang_cxx: Lang = "C++"; break;
  552. }
  553. JOS.attribute("language", Lang);
  554. attributeOnlyIfTrue("hasBraces", LSD->hasBraces());
  555. }
  556. void JSONNodeDumper::VisitAccessSpecDecl(const AccessSpecDecl *ASD) {
  557. JOS.attribute("access", createAccessSpecifier(ASD->getAccess()));
  558. }
  559. void JSONNodeDumper::VisitFriendDecl(const FriendDecl *FD) {
  560. if (const TypeSourceInfo *T = FD->getFriendType())
  561. JOS.attribute("type", createQualType(T->getType()));
  562. }
  563. void JSONNodeDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
  564. VisitNamedDecl(D);
  565. JOS.attribute("type", createQualType(D->getType()));
  566. attributeOnlyIfTrue("synthesized", D->getSynthesize());
  567. switch (D->getAccessControl()) {
  568. case ObjCIvarDecl::None: JOS.attribute("access", "none"); break;
  569. case ObjCIvarDecl::Private: JOS.attribute("access", "private"); break;
  570. case ObjCIvarDecl::Protected: JOS.attribute("access", "protected"); break;
  571. case ObjCIvarDecl::Public: JOS.attribute("access", "public"); break;
  572. case ObjCIvarDecl::Package: JOS.attribute("access", "package"); break;
  573. }
  574. }
  575. void JSONNodeDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
  576. VisitNamedDecl(D);
  577. JOS.attribute("returnType", createQualType(D->getReturnType()));
  578. JOS.attribute("instance", D->isInstanceMethod());
  579. attributeOnlyIfTrue("variadic", D->isVariadic());
  580. }
  581. void JSONNodeDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) {
  582. VisitNamedDecl(D);
  583. JOS.attribute("type", createQualType(D->getUnderlyingType()));
  584. attributeOnlyIfTrue("bounded", D->hasExplicitBound());
  585. switch (D->getVariance()) {
  586. case ObjCTypeParamVariance::Invariant:
  587. break;
  588. case ObjCTypeParamVariance::Covariant:
  589. JOS.attribute("variance", "covariant");
  590. break;
  591. case ObjCTypeParamVariance::Contravariant:
  592. JOS.attribute("variance", "contravariant");
  593. break;
  594. }
  595. }
  596. void JSONNodeDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
  597. VisitNamedDecl(D);
  598. JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
  599. JOS.attribute("implementation", createBareDeclRef(D->getImplementation()));
  600. llvm::json::Array Protocols;
  601. for (const auto* P : D->protocols())
  602. Protocols.push_back(createBareDeclRef(P));
  603. if (!Protocols.empty())
  604. JOS.attribute("protocols", std::move(Protocols));
  605. }
  606. void JSONNodeDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
  607. VisitNamedDecl(D);
  608. JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
  609. JOS.attribute("categoryDecl", createBareDeclRef(D->getCategoryDecl()));
  610. }
  611. void JSONNodeDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
  612. VisitNamedDecl(D);
  613. llvm::json::Array Protocols;
  614. for (const auto *P : D->protocols())
  615. Protocols.push_back(createBareDeclRef(P));
  616. if (!Protocols.empty())
  617. JOS.attribute("protocols", std::move(Protocols));
  618. }
  619. void JSONNodeDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
  620. VisitNamedDecl(D);
  621. JOS.attribute("super", createBareDeclRef(D->getSuperClass()));
  622. JOS.attribute("implementation", createBareDeclRef(D->getImplementation()));
  623. llvm::json::Array Protocols;
  624. for (const auto* P : D->protocols())
  625. Protocols.push_back(createBareDeclRef(P));
  626. if (!Protocols.empty())
  627. JOS.attribute("protocols", std::move(Protocols));
  628. }
  629. void JSONNodeDumper::VisitObjCImplementationDecl(
  630. const ObjCImplementationDecl *D) {
  631. VisitNamedDecl(D);
  632. JOS.attribute("super", createBareDeclRef(D->getSuperClass()));
  633. JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
  634. }
  635. void JSONNodeDumper::VisitObjCCompatibleAliasDecl(
  636. const ObjCCompatibleAliasDecl *D) {
  637. VisitNamedDecl(D);
  638. JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
  639. }
  640. void JSONNodeDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
  641. VisitNamedDecl(D);
  642. JOS.attribute("type", createQualType(D->getType()));
  643. switch (D->getPropertyImplementation()) {
  644. case ObjCPropertyDecl::None: break;
  645. case ObjCPropertyDecl::Required: JOS.attribute("control", "required"); break;
  646. case ObjCPropertyDecl::Optional: JOS.attribute("control", "optional"); break;
  647. }
  648. ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
  649. if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
  650. if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
  651. JOS.attribute("getter", createBareDeclRef(D->getGetterMethodDecl()));
  652. if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
  653. JOS.attribute("setter", createBareDeclRef(D->getSetterMethodDecl()));
  654. attributeOnlyIfTrue("readonly", Attrs & ObjCPropertyDecl::OBJC_PR_readonly);
  655. attributeOnlyIfTrue("assign", Attrs & ObjCPropertyDecl::OBJC_PR_assign);
  656. attributeOnlyIfTrue("readwrite",
  657. Attrs & ObjCPropertyDecl::OBJC_PR_readwrite);
  658. attributeOnlyIfTrue("retain", Attrs & ObjCPropertyDecl::OBJC_PR_retain);
  659. attributeOnlyIfTrue("copy", Attrs & ObjCPropertyDecl::OBJC_PR_copy);
  660. attributeOnlyIfTrue("nonatomic",
  661. Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic);
  662. attributeOnlyIfTrue("atomic", Attrs & ObjCPropertyDecl::OBJC_PR_atomic);
  663. attributeOnlyIfTrue("weak", Attrs & ObjCPropertyDecl::OBJC_PR_weak);
  664. attributeOnlyIfTrue("strong", Attrs & ObjCPropertyDecl::OBJC_PR_strong);
  665. attributeOnlyIfTrue("unsafe_unretained",
  666. Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
  667. attributeOnlyIfTrue("class", Attrs & ObjCPropertyDecl::OBJC_PR_class);
  668. attributeOnlyIfTrue("nullability",
  669. Attrs & ObjCPropertyDecl::OBJC_PR_nullability);
  670. attributeOnlyIfTrue("null_resettable",
  671. Attrs & ObjCPropertyDecl::OBJC_PR_null_resettable);
  672. }
  673. }
  674. void JSONNodeDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
  675. VisitNamedDecl(D->getPropertyDecl());
  676. JOS.attribute("implKind", D->getPropertyImplementation() ==
  677. ObjCPropertyImplDecl::Synthesize
  678. ? "synthesize"
  679. : "dynamic");
  680. JOS.attribute("propertyDecl", createBareDeclRef(D->getPropertyDecl()));
  681. JOS.attribute("ivarDecl", createBareDeclRef(D->getPropertyIvarDecl()));
  682. }
  683. void JSONNodeDumper::VisitBlockDecl(const BlockDecl *D) {
  684. attributeOnlyIfTrue("variadic", D->isVariadic());
  685. attributeOnlyIfTrue("capturesThis", D->capturesCXXThis());
  686. }
  687. void JSONNodeDumper::VisitDeclRefExpr(const DeclRefExpr *DRE) {
  688. JOS.attribute("referencedDecl", createBareDeclRef(DRE->getDecl()));
  689. if (DRE->getDecl() != DRE->getFoundDecl())
  690. JOS.attribute("foundReferencedDecl",
  691. createBareDeclRef(DRE->getFoundDecl()));
  692. switch (DRE->isNonOdrUse()) {
  693. case NOUR_None: break;
  694. case NOUR_Unevaluated: JOS.attribute("nonOdrUseReason", "unevaluated"); break;
  695. case NOUR_Constant: JOS.attribute("nonOdrUseReason", "constant"); break;
  696. case NOUR_Discarded: JOS.attribute("nonOdrUseReason", "discarded"); break;
  697. }
  698. }
  699. void JSONNodeDumper::VisitPredefinedExpr(const PredefinedExpr *PE) {
  700. JOS.attribute("name", PredefinedExpr::getIdentKindName(PE->getIdentKind()));
  701. }
  702. void JSONNodeDumper::VisitUnaryOperator(const UnaryOperator *UO) {
  703. JOS.attribute("isPostfix", UO->isPostfix());
  704. JOS.attribute("opcode", UnaryOperator::getOpcodeStr(UO->getOpcode()));
  705. if (!UO->canOverflow())
  706. JOS.attribute("canOverflow", false);
  707. }
  708. void JSONNodeDumper::VisitBinaryOperator(const BinaryOperator *BO) {
  709. JOS.attribute("opcode", BinaryOperator::getOpcodeStr(BO->getOpcode()));
  710. }
  711. void JSONNodeDumper::VisitCompoundAssignOperator(
  712. const CompoundAssignOperator *CAO) {
  713. VisitBinaryOperator(CAO);
  714. JOS.attribute("computeLHSType", createQualType(CAO->getComputationLHSType()));
  715. JOS.attribute("computeResultType",
  716. createQualType(CAO->getComputationResultType()));
  717. }
  718. void JSONNodeDumper::VisitMemberExpr(const MemberExpr *ME) {
  719. // Note, we always write this Boolean field because the information it conveys
  720. // is critical to understanding the AST node.
  721. ValueDecl *VD = ME->getMemberDecl();
  722. JOS.attribute("name", VD && VD->getDeclName() ? VD->getNameAsString() : "");
  723. JOS.attribute("isArrow", ME->isArrow());
  724. JOS.attribute("referencedMemberDecl", createPointerRepresentation(VD));
  725. }
  726. void JSONNodeDumper::VisitCXXNewExpr(const CXXNewExpr *NE) {
  727. attributeOnlyIfTrue("isGlobal", NE->isGlobalNew());
  728. attributeOnlyIfTrue("isArray", NE->isArray());
  729. attributeOnlyIfTrue("isPlacement", NE->getNumPlacementArgs() != 0);
  730. switch (NE->getInitializationStyle()) {
  731. case CXXNewExpr::NoInit: break;
  732. case CXXNewExpr::CallInit: JOS.attribute("initStyle", "call"); break;
  733. case CXXNewExpr::ListInit: JOS.attribute("initStyle", "list"); break;
  734. }
  735. if (const FunctionDecl *FD = NE->getOperatorNew())
  736. JOS.attribute("operatorNewDecl", createBareDeclRef(FD));
  737. if (const FunctionDecl *FD = NE->getOperatorDelete())
  738. JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD));
  739. }
  740. void JSONNodeDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *DE) {
  741. attributeOnlyIfTrue("isGlobal", DE->isGlobalDelete());
  742. attributeOnlyIfTrue("isArray", DE->isArrayForm());
  743. attributeOnlyIfTrue("isArrayAsWritten", DE->isArrayFormAsWritten());
  744. if (const FunctionDecl *FD = DE->getOperatorDelete())
  745. JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD));
  746. }
  747. void JSONNodeDumper::VisitCXXThisExpr(const CXXThisExpr *TE) {
  748. attributeOnlyIfTrue("implicit", TE->isImplicit());
  749. }
  750. void JSONNodeDumper::VisitCastExpr(const CastExpr *CE) {
  751. JOS.attribute("castKind", CE->getCastKindName());
  752. llvm::json::Array Path = createCastPath(CE);
  753. if (!Path.empty())
  754. JOS.attribute("path", std::move(Path));
  755. // FIXME: This may not be useful information as it can be obtusely gleaned
  756. // from the inner[] array.
  757. if (const NamedDecl *ND = CE->getConversionFunction())
  758. JOS.attribute("conversionFunc", createBareDeclRef(ND));
  759. }
  760. void JSONNodeDumper::VisitImplicitCastExpr(const ImplicitCastExpr *ICE) {
  761. VisitCastExpr(ICE);
  762. attributeOnlyIfTrue("isPartOfExplicitCast", ICE->isPartOfExplicitCast());
  763. }
  764. void JSONNodeDumper::VisitCallExpr(const CallExpr *CE) {
  765. attributeOnlyIfTrue("adl", CE->usesADL());
  766. }
  767. void JSONNodeDumper::VisitUnaryExprOrTypeTraitExpr(
  768. const UnaryExprOrTypeTraitExpr *TTE) {
  769. switch (TTE->getKind()) {
  770. case UETT_SizeOf: JOS.attribute("name", "sizeof"); break;
  771. case UETT_AlignOf: JOS.attribute("name", "alignof"); break;
  772. case UETT_VecStep: JOS.attribute("name", "vec_step"); break;
  773. case UETT_PreferredAlignOf: JOS.attribute("name", "__alignof"); break;
  774. case UETT_OpenMPRequiredSimdAlign:
  775. JOS.attribute("name", "__builtin_omp_required_simd_align"); break;
  776. }
  777. if (TTE->isArgumentType())
  778. JOS.attribute("argType", createQualType(TTE->getArgumentType()));
  779. }
  780. void JSONNodeDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *SOPE) {
  781. VisitNamedDecl(SOPE->getPack());
  782. }
  783. void JSONNodeDumper::VisitUnresolvedLookupExpr(
  784. const UnresolvedLookupExpr *ULE) {
  785. JOS.attribute("usesADL", ULE->requiresADL());
  786. JOS.attribute("name", ULE->getName().getAsString());
  787. JOS.attributeArray("lookups", [this, ULE] {
  788. for (const NamedDecl *D : ULE->decls())
  789. JOS.value(createBareDeclRef(D));
  790. });
  791. }
  792. void JSONNodeDumper::VisitAddrLabelExpr(const AddrLabelExpr *ALE) {
  793. JOS.attribute("name", ALE->getLabel()->getName());
  794. JOS.attribute("labelDeclId", createPointerRepresentation(ALE->getLabel()));
  795. }
  796. void JSONNodeDumper::VisitCXXTypeidExpr(const CXXTypeidExpr *CTE) {
  797. if (CTE->isTypeOperand()) {
  798. QualType Adjusted = CTE->getTypeOperand(Ctx);
  799. QualType Unadjusted = CTE->getTypeOperandSourceInfo()->getType();
  800. JOS.attribute("typeArg", createQualType(Unadjusted));
  801. if (Adjusted != Unadjusted)
  802. JOS.attribute("adjustedTypeArg", createQualType(Adjusted));
  803. }
  804. }
  805. void JSONNodeDumper::VisitIntegerLiteral(const IntegerLiteral *IL) {
  806. JOS.attribute("value",
  807. IL->getValue().toString(
  808. /*Radix=*/10, IL->getType()->isSignedIntegerType()));
  809. }
  810. void JSONNodeDumper::VisitCharacterLiteral(const CharacterLiteral *CL) {
  811. // FIXME: This should probably print the character literal as a string,
  812. // rather than as a numerical value. It would be nice if the behavior matched
  813. // what we do to print a string literal; right now, it is impossible to tell
  814. // the difference between 'a' and L'a' in C from the JSON output.
  815. JOS.attribute("value", CL->getValue());
  816. }
  817. void JSONNodeDumper::VisitFixedPointLiteral(const FixedPointLiteral *FPL) {
  818. JOS.attribute("value", FPL->getValueAsString(/*Radix=*/10));
  819. }
  820. void JSONNodeDumper::VisitFloatingLiteral(const FloatingLiteral *FL) {
  821. JOS.attribute("value", FL->getValueAsApproximateDouble());
  822. }
  823. void JSONNodeDumper::VisitStringLiteral(const StringLiteral *SL) {
  824. std::string Buffer;
  825. llvm::raw_string_ostream SS(Buffer);
  826. SL->outputString(SS);
  827. JOS.attribute("value", SS.str());
  828. }
  829. void JSONNodeDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *BLE) {
  830. JOS.attribute("value", BLE->getValue());
  831. }
  832. void JSONNodeDumper::VisitIfStmt(const IfStmt *IS) {
  833. attributeOnlyIfTrue("hasInit", IS->hasInitStorage());
  834. attributeOnlyIfTrue("hasVar", IS->hasVarStorage());
  835. attributeOnlyIfTrue("hasElse", IS->hasElseStorage());
  836. attributeOnlyIfTrue("isConstexpr", IS->isConstexpr());
  837. }
  838. void JSONNodeDumper::VisitSwitchStmt(const SwitchStmt *SS) {
  839. attributeOnlyIfTrue("hasInit", SS->hasInitStorage());
  840. attributeOnlyIfTrue("hasVar", SS->hasVarStorage());
  841. }
  842. void JSONNodeDumper::VisitCaseStmt(const CaseStmt *CS) {
  843. attributeOnlyIfTrue("isGNURange", CS->caseStmtIsGNURange());
  844. }
  845. void JSONNodeDumper::VisitLabelStmt(const LabelStmt *LS) {
  846. JOS.attribute("name", LS->getName());
  847. JOS.attribute("declId", createPointerRepresentation(LS->getDecl()));
  848. }
  849. void JSONNodeDumper::VisitGotoStmt(const GotoStmt *GS) {
  850. JOS.attribute("targetLabelDeclId",
  851. createPointerRepresentation(GS->getLabel()));
  852. }
  853. void JSONNodeDumper::VisitWhileStmt(const WhileStmt *WS) {
  854. attributeOnlyIfTrue("hasVar", WS->hasVarStorage());
  855. }
  856. void JSONNodeDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt* OACS) {
  857. // FIXME: it would be nice for the ASTNodeTraverser would handle the catch
  858. // parameter the same way for C++ and ObjC rather. In this case, C++ gets a
  859. // null child node and ObjC gets no child node.
  860. attributeOnlyIfTrue("isCatchAll", OACS->getCatchParamDecl() == nullptr);
  861. }
  862. StringRef JSONNodeDumper::getCommentCommandName(unsigned CommandID) const {
  863. if (Traits)
  864. return Traits->getCommandInfo(CommandID)->Name;
  865. if (const comments::CommandInfo *Info =
  866. comments::CommandTraits::getBuiltinCommandInfo(CommandID))
  867. return Info->Name;
  868. return "<invalid>";
  869. }
  870. void JSONNodeDumper::visitTextComment(const comments::TextComment *C,
  871. const comments::FullComment *) {
  872. JOS.attribute("text", C->getText());
  873. }
  874. void JSONNodeDumper::visitInlineCommandComment(
  875. const comments::InlineCommandComment *C, const comments::FullComment *) {
  876. JOS.attribute("name", getCommentCommandName(C->getCommandID()));
  877. switch (C->getRenderKind()) {
  878. case comments::InlineCommandComment::RenderNormal:
  879. JOS.attribute("renderKind", "normal");
  880. break;
  881. case comments::InlineCommandComment::RenderBold:
  882. JOS.attribute("renderKind", "bold");
  883. break;
  884. case comments::InlineCommandComment::RenderEmphasized:
  885. JOS.attribute("renderKind", "emphasized");
  886. break;
  887. case comments::InlineCommandComment::RenderMonospaced:
  888. JOS.attribute("renderKind", "monospaced");
  889. break;
  890. }
  891. llvm::json::Array Args;
  892. for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I)
  893. Args.push_back(C->getArgText(I));
  894. if (!Args.empty())
  895. JOS.attribute("args", std::move(Args));
  896. }
  897. void JSONNodeDumper::visitHTMLStartTagComment(
  898. const comments::HTMLStartTagComment *C, const comments::FullComment *) {
  899. JOS.attribute("name", C->getTagName());
  900. attributeOnlyIfTrue("selfClosing", C->isSelfClosing());
  901. attributeOnlyIfTrue("malformed", C->isMalformed());
  902. llvm::json::Array Attrs;
  903. for (unsigned I = 0, E = C->getNumAttrs(); I < E; ++I)
  904. Attrs.push_back(
  905. {{"name", C->getAttr(I).Name}, {"value", C->getAttr(I).Value}});
  906. if (!Attrs.empty())
  907. JOS.attribute("attrs", std::move(Attrs));
  908. }
  909. void JSONNodeDumper::visitHTMLEndTagComment(
  910. const comments::HTMLEndTagComment *C, const comments::FullComment *) {
  911. JOS.attribute("name", C->getTagName());
  912. }
  913. void JSONNodeDumper::visitBlockCommandComment(
  914. const comments::BlockCommandComment *C, const comments::FullComment *) {
  915. JOS.attribute("name", getCommentCommandName(C->getCommandID()));
  916. llvm::json::Array Args;
  917. for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I)
  918. Args.push_back(C->getArgText(I));
  919. if (!Args.empty())
  920. JOS.attribute("args", std::move(Args));
  921. }
  922. void JSONNodeDumper::visitParamCommandComment(
  923. const comments::ParamCommandComment *C, const comments::FullComment *FC) {
  924. switch (C->getDirection()) {
  925. case comments::ParamCommandComment::In:
  926. JOS.attribute("direction", "in");
  927. break;
  928. case comments::ParamCommandComment::Out:
  929. JOS.attribute("direction", "out");
  930. break;
  931. case comments::ParamCommandComment::InOut:
  932. JOS.attribute("direction", "in,out");
  933. break;
  934. }
  935. attributeOnlyIfTrue("explicit", C->isDirectionExplicit());
  936. if (C->hasParamName())
  937. JOS.attribute("param", C->isParamIndexValid() ? C->getParamName(FC)
  938. : C->getParamNameAsWritten());
  939. if (C->isParamIndexValid() && !C->isVarArgParam())
  940. JOS.attribute("paramIdx", C->getParamIndex());
  941. }
  942. void JSONNodeDumper::visitTParamCommandComment(
  943. const comments::TParamCommandComment *C, const comments::FullComment *FC) {
  944. if (C->hasParamName())
  945. JOS.attribute("param", C->isPositionValid() ? C->getParamName(FC)
  946. : C->getParamNameAsWritten());
  947. if (C->isPositionValid()) {
  948. llvm::json::Array Positions;
  949. for (unsigned I = 0, E = C->getDepth(); I < E; ++I)
  950. Positions.push_back(C->getIndex(I));
  951. if (!Positions.empty())
  952. JOS.attribute("positions", std::move(Positions));
  953. }
  954. }
  955. void JSONNodeDumper::visitVerbatimBlockComment(
  956. const comments::VerbatimBlockComment *C, const comments::FullComment *) {
  957. JOS.attribute("name", getCommentCommandName(C->getCommandID()));
  958. JOS.attribute("closeName", C->getCloseName());
  959. }
  960. void JSONNodeDumper::visitVerbatimBlockLineComment(
  961. const comments::VerbatimBlockLineComment *C,
  962. const comments::FullComment *) {
  963. JOS.attribute("text", C->getText());
  964. }
  965. void JSONNodeDumper::visitVerbatimLineComment(
  966. const comments::VerbatimLineComment *C, const comments::FullComment *) {
  967. JOS.attribute("text", C->getText());
  968. }