JSONNodeDumper.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. #include "clang/AST/JSONNodeDumper.h"
  2. #include "clang/Lex/Lexer.h"
  3. #include "llvm/ADT/StringSwitch.h"
  4. using namespace clang;
  5. void JSONNodeDumper::addPreviousDeclaration(const Decl *D) {
  6. switch (D->getKind()) {
  7. #define DECL(DERIVED, BASE) \
  8. case Decl::DERIVED: \
  9. return writePreviousDeclImpl(cast<DERIVED##Decl>(D));
  10. #define ABSTRACT_DECL(DECL)
  11. #include "clang/AST/DeclNodes.inc"
  12. #undef ABSTRACT_DECL
  13. #undef DECL
  14. }
  15. llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
  16. }
  17. void JSONNodeDumper::Visit(const Attr *A) {
  18. const char *AttrName = nullptr;
  19. switch (A->getKind()) {
  20. #define ATTR(X) \
  21. case attr::X: \
  22. AttrName = #X"Attr"; \
  23. break;
  24. #include "clang/Basic/AttrList.inc"
  25. #undef ATTR
  26. }
  27. JOS.attribute("id", createPointerRepresentation(A));
  28. JOS.attribute("kind", AttrName);
  29. JOS.attributeObject("range", [A, this] { writeSourceRange(A->getRange()); });
  30. attributeOnlyIfTrue("inherited", A->isInherited());
  31. attributeOnlyIfTrue("implicit", A->isImplicit());
  32. // FIXME: it would be useful for us to output the spelling kind as well as
  33. // the actual spelling. This would allow us to distinguish between the
  34. // various attribute syntaxes, but we don't currently track that information
  35. // within the AST.
  36. //JOS.attribute("spelling", A->getSpelling());
  37. InnerAttrVisitor::Visit(A);
  38. }
  39. void JSONNodeDumper::Visit(const Stmt *S) {
  40. if (!S)
  41. return;
  42. JOS.attribute("id", createPointerRepresentation(S));
  43. JOS.attribute("kind", S->getStmtClassName());
  44. JOS.attributeObject("range",
  45. [S, this] { writeSourceRange(S->getSourceRange()); });
  46. if (const auto *E = dyn_cast<Expr>(S)) {
  47. JOS.attribute("type", createQualType(E->getType()));
  48. const char *Category = nullptr;
  49. switch (E->getValueKind()) {
  50. case VK_LValue: Category = "lvalue"; break;
  51. case VK_XValue: Category = "xvalue"; break;
  52. case VK_RValue: Category = "rvalue"; break;
  53. }
  54. JOS.attribute("valueCategory", Category);
  55. }
  56. InnerStmtVisitor::Visit(S);
  57. }
  58. void JSONNodeDumper::Visit(const Type *T) {
  59. JOS.attribute("id", createPointerRepresentation(T));
  60. if (!T)
  61. return;
  62. JOS.attribute("kind", (llvm::Twine(T->getTypeClassName()) + "Type").str());
  63. JOS.attribute("type", createQualType(QualType(T, 0), /*Desugar*/ false));
  64. attributeOnlyIfTrue("isDependent", T->isDependentType());
  65. attributeOnlyIfTrue("isInstantiationDependent",
  66. T->isInstantiationDependentType());
  67. attributeOnlyIfTrue("isVariablyModified", T->isVariablyModifiedType());
  68. attributeOnlyIfTrue("containsUnexpandedPack",
  69. T->containsUnexpandedParameterPack());
  70. attributeOnlyIfTrue("isImported", T->isFromAST());
  71. InnerTypeVisitor::Visit(T);
  72. }
  73. void JSONNodeDumper::Visit(QualType T) {
  74. JOS.attribute("id", createPointerRepresentation(T.getAsOpaquePtr()));
  75. JOS.attribute("kind", "QualType");
  76. JOS.attribute("type", createQualType(T));
  77. JOS.attribute("qualifiers", T.split().Quals.getAsString());
  78. }
  79. void JSONNodeDumper::Visit(const Decl *D) {
  80. JOS.attribute("id", createPointerRepresentation(D));
  81. if (!D)
  82. return;
  83. JOS.attribute("kind", (llvm::Twine(D->getDeclKindName()) + "Decl").str());
  84. JOS.attributeObject("loc",
  85. [D, this] { writeSourceLocation(D->getLocation()); });
  86. JOS.attributeObject("range",
  87. [D, this] { writeSourceRange(D->getSourceRange()); });
  88. attributeOnlyIfTrue("isImplicit", D->isImplicit());
  89. attributeOnlyIfTrue("isInvalid", D->isInvalidDecl());
  90. if (D->isUsed())
  91. JOS.attribute("isUsed", true);
  92. else if (D->isThisDeclarationReferenced())
  93. JOS.attribute("isReferenced", true);
  94. if (const auto *ND = dyn_cast<NamedDecl>(D))
  95. attributeOnlyIfTrue("isHidden", ND->isHidden());
  96. if (D->getLexicalDeclContext() != D->getDeclContext()) {
  97. // Because of multiple inheritance, a DeclContext pointer does not produce
  98. // the same pointer representation as a Decl pointer that references the
  99. // same AST Node.
  100. const auto *ParentDeclContextDecl = dyn_cast<Decl>(D->getDeclContext());
  101. JOS.attribute("parentDeclContextId",
  102. createPointerRepresentation(ParentDeclContextDecl));
  103. }
  104. addPreviousDeclaration(D);
  105. InnerDeclVisitor::Visit(D);
  106. }
  107. void JSONNodeDumper::Visit(const comments::Comment *C,
  108. const comments::FullComment *FC) {
  109. if (!C)
  110. return;
  111. JOS.attribute("id", createPointerRepresentation(C));
  112. JOS.attribute("kind", C->getCommentKindName());
  113. JOS.attributeObject("loc",
  114. [C, this] { writeSourceLocation(C->getLocation()); });
  115. JOS.attributeObject("range",
  116. [C, this] { writeSourceRange(C->getSourceRange()); });
  117. InnerCommentVisitor::visit(C, FC);
  118. }
  119. void JSONNodeDumper::Visit(const TemplateArgument &TA, SourceRange R,
  120. const Decl *From, StringRef Label) {
  121. JOS.attribute("kind", "TemplateArgument");
  122. if (R.isValid())
  123. JOS.attributeObject("range", [R, this] { writeSourceRange(R); });
  124. if (From)
  125. JOS.attribute(Label.empty() ? "fromDecl" : Label, createBareDeclRef(From));
  126. InnerTemplateArgVisitor::Visit(TA);
  127. }
  128. void JSONNodeDumper::Visit(const CXXCtorInitializer *Init) {
  129. JOS.attribute("kind", "CXXCtorInitializer");
  130. if (Init->isAnyMemberInitializer())
  131. JOS.attribute("anyInit", createBareDeclRef(Init->getAnyMember()));
  132. else if (Init->isBaseInitializer())
  133. JOS.attribute("baseInit",
  134. createQualType(QualType(Init->getBaseClass(), 0)));
  135. else if (Init->isDelegatingInitializer())
  136. JOS.attribute("delegatingInit",
  137. createQualType(Init->getTypeSourceInfo()->getType()));
  138. else
  139. llvm_unreachable("Unknown initializer type");
  140. }
  141. void JSONNodeDumper::Visit(const OMPClause *C) {}
  142. void JSONNodeDumper::Visit(const BlockDecl::Capture &C) {
  143. JOS.attribute("kind", "Capture");
  144. attributeOnlyIfTrue("byref", C.isByRef());
  145. attributeOnlyIfTrue("nested", C.isNested());
  146. if (C.getVariable())
  147. JOS.attribute("var", createBareDeclRef(C.getVariable()));
  148. }
  149. void JSONNodeDumper::Visit(const GenericSelectionExpr::ConstAssociation &A) {
  150. JOS.attribute("associationKind", A.getTypeSourceInfo() ? "case" : "default");
  151. attributeOnlyIfTrue("selected", A.isSelected());
  152. }
  153. void JSONNodeDumper::writeIncludeStack(PresumedLoc Loc, bool JustFirst) {
  154. if (Loc.isInvalid())
  155. return;
  156. JOS.attributeBegin("includedFrom");
  157. JOS.objectBegin();
  158. if (!JustFirst) {
  159. // Walk the stack recursively, then print out the presumed location.
  160. writeIncludeStack(SM.getPresumedLoc(Loc.getIncludeLoc()));
  161. }
  162. JOS.attribute("file", Loc.getFilename());
  163. JOS.objectEnd();
  164. JOS.attributeEnd();
  165. }
  166. void JSONNodeDumper::writeBareSourceLocation(SourceLocation Loc,
  167. bool IsSpelling) {
  168. PresumedLoc Presumed = SM.getPresumedLoc(Loc);
  169. unsigned ActualLine = IsSpelling ? SM.getSpellingLineNumber(Loc)
  170. : SM.getExpansionLineNumber(Loc);
  171. if (Presumed.isValid()) {
  172. JOS.attribute("offset", SM.getDecomposedLoc(Loc).second);
  173. if (LastLocFilename != Presumed.getFilename()) {
  174. JOS.attribute("file", Presumed.getFilename());
  175. JOS.attribute("line", ActualLine);
  176. } else if (LastLocLine != ActualLine)
  177. JOS.attribute("line", ActualLine);
  178. unsigned PresumedLine = Presumed.getLine();
  179. if (ActualLine != PresumedLine && LastLocPresumedLine != PresumedLine)
  180. JOS.attribute("presumedLine", PresumedLine);
  181. JOS.attribute("col", Presumed.getColumn());
  182. JOS.attribute("tokLen",
  183. Lexer::MeasureTokenLength(Loc, SM, Ctx.getLangOpts()));
  184. LastLocFilename = Presumed.getFilename();
  185. LastLocPresumedLine = PresumedLine;
  186. LastLocLine = ActualLine;
  187. // Orthogonal to the file, line, and column de-duplication is whether the
  188. // given location was a result of an include. If so, print where the
  189. // include location came from.
  190. writeIncludeStack(SM.getPresumedLoc(Presumed.getIncludeLoc()),
  191. /*JustFirst*/ true);
  192. }
  193. }
  194. void JSONNodeDumper::writeSourceLocation(SourceLocation Loc) {
  195. SourceLocation Spelling = SM.getSpellingLoc(Loc);
  196. SourceLocation Expansion = SM.getExpansionLoc(Loc);
  197. if (Expansion != Spelling) {
  198. // If the expansion and the spelling are different, output subobjects
  199. // describing both locations.
  200. JOS.attributeObject("spellingLoc", [Spelling, this] {
  201. writeBareSourceLocation(Spelling, /*IsSpelling*/ true);
  202. });
  203. JOS.attributeObject("expansionLoc", [Expansion, Loc, this] {
  204. writeBareSourceLocation(Expansion, /*IsSpelling*/ false);
  205. // If there is a macro expansion, add extra information if the interesting
  206. // bit is the macro arg expansion.
  207. if (SM.isMacroArgExpansion(Loc))
  208. JOS.attribute("isMacroArgExpansion", true);
  209. });
  210. } else
  211. writeBareSourceLocation(Spelling, /*IsSpelling*/ true);
  212. }
  213. void JSONNodeDumper::writeSourceRange(SourceRange R) {
  214. JOS.attributeObject("begin",
  215. [R, this] { writeSourceLocation(R.getBegin()); });
  216. JOS.attributeObject("end", [R, this] { writeSourceLocation(R.getEnd()); });
  217. }
  218. std::string JSONNodeDumper::createPointerRepresentation(const void *Ptr) {
  219. // Because JSON stores integer values as signed 64-bit integers, trying to
  220. // represent them as such makes for very ugly pointer values in the resulting
  221. // output. Instead, we convert the value to hex and treat it as a string.
  222. return "0x" + llvm::utohexstr(reinterpret_cast<uint64_t>(Ptr), true);
  223. }
  224. llvm::json::Object JSONNodeDumper::createQualType(QualType QT, bool Desugar) {
  225. SplitQualType SQT = QT.split();
  226. llvm::json::Object Ret{{"qualType", QualType::getAsString(SQT, PrintPolicy)}};
  227. if (Desugar && !QT.isNull()) {
  228. SplitQualType DSQT = QT.getSplitDesugaredType();
  229. if (DSQT != SQT)
  230. Ret["desugaredQualType"] = QualType::getAsString(DSQT, PrintPolicy);
  231. if (const auto *TT = QT->getAs<TypedefType>())
  232. Ret["typeAliasDeclId"] = createPointerRepresentation(TT->getDecl());
  233. }
  234. return Ret;
  235. }
  236. void JSONNodeDumper::writeBareDeclRef(const Decl *D) {
  237. JOS.attribute("id", createPointerRepresentation(D));
  238. if (!D)
  239. return;
  240. JOS.attribute("kind", (llvm::Twine(D->getDeclKindName()) + "Decl").str());
  241. if (const auto *ND = dyn_cast<NamedDecl>(D))
  242. JOS.attribute("name", ND->getDeclName().getAsString());
  243. if (const auto *VD = dyn_cast<ValueDecl>(D))
  244. JOS.attribute("type", createQualType(VD->getType()));
  245. }
  246. llvm::json::Object JSONNodeDumper::createBareDeclRef(const Decl *D) {
  247. llvm::json::Object Ret{{"id", createPointerRepresentation(D)}};
  248. if (!D)
  249. return Ret;
  250. Ret["kind"] = (llvm::Twine(D->getDeclKindName()) + "Decl").str();
  251. if (const auto *ND = dyn_cast<NamedDecl>(D))
  252. Ret["name"] = ND->getDeclName().getAsString();
  253. if (const auto *VD = dyn_cast<ValueDecl>(D))
  254. Ret["type"] = createQualType(VD->getType());
  255. return Ret;
  256. }
  257. llvm::json::Array JSONNodeDumper::createCastPath(const CastExpr *C) {
  258. llvm::json::Array Ret;
  259. if (C->path_empty())
  260. return Ret;
  261. for (auto I = C->path_begin(), E = C->path_end(); I != E; ++I) {
  262. const CXXBaseSpecifier *Base = *I;
  263. const auto *RD =
  264. cast<CXXRecordDecl>(Base->getType()->castAs<RecordType>()->getDecl());
  265. llvm::json::Object Val{{"name", RD->getName()}};
  266. if (Base->isVirtual())
  267. Val["isVirtual"] = true;
  268. Ret.push_back(std::move(Val));
  269. }
  270. return Ret;
  271. }
  272. #define FIELD2(Name, Flag) if (RD->Flag()) Ret[Name] = true
  273. #define FIELD1(Flag) FIELD2(#Flag, Flag)
  274. static llvm::json::Object
  275. createDefaultConstructorDefinitionData(const CXXRecordDecl *RD) {
  276. llvm::json::Object Ret;
  277. FIELD2("exists", hasDefaultConstructor);
  278. FIELD2("trivial", hasTrivialDefaultConstructor);
  279. FIELD2("nonTrivial", hasNonTrivialDefaultConstructor);
  280. FIELD2("userProvided", hasUserProvidedDefaultConstructor);
  281. FIELD2("isConstexpr", hasConstexprDefaultConstructor);
  282. FIELD2("needsImplicit", needsImplicitDefaultConstructor);
  283. FIELD2("defaultedIsConstexpr", defaultedDefaultConstructorIsConstexpr);
  284. return Ret;
  285. }
  286. static llvm::json::Object
  287. createCopyConstructorDefinitionData(const CXXRecordDecl *RD) {
  288. llvm::json::Object Ret;
  289. FIELD2("simple", hasSimpleCopyConstructor);
  290. FIELD2("trivial", hasTrivialCopyConstructor);
  291. FIELD2("nonTrivial", hasNonTrivialCopyConstructor);
  292. FIELD2("userDeclared", hasUserDeclaredCopyConstructor);
  293. FIELD2("hasConstParam", hasCopyConstructorWithConstParam);
  294. FIELD2("implicitHasConstParam", implicitCopyConstructorHasConstParam);
  295. FIELD2("needsImplicit", needsImplicitCopyConstructor);
  296. FIELD2("needsOverloadResolution", needsOverloadResolutionForCopyConstructor);
  297. if (!RD->needsOverloadResolutionForCopyConstructor())
  298. FIELD2("defaultedIsDeleted", defaultedCopyConstructorIsDeleted);
  299. return Ret;
  300. }
  301. static llvm::json::Object
  302. createMoveConstructorDefinitionData(const CXXRecordDecl *RD) {
  303. llvm::json::Object Ret;
  304. FIELD2("exists", hasMoveConstructor);
  305. FIELD2("simple", hasSimpleMoveConstructor);
  306. FIELD2("trivial", hasTrivialMoveConstructor);
  307. FIELD2("nonTrivial", hasNonTrivialMoveConstructor);
  308. FIELD2("userDeclared", hasUserDeclaredMoveConstructor);
  309. FIELD2("needsImplicit", needsImplicitMoveConstructor);
  310. FIELD2("needsOverloadResolution", needsOverloadResolutionForMoveConstructor);
  311. if (!RD->needsOverloadResolutionForMoveConstructor())
  312. FIELD2("defaultedIsDeleted", defaultedMoveConstructorIsDeleted);
  313. return Ret;
  314. }
  315. static llvm::json::Object
  316. createCopyAssignmentDefinitionData(const CXXRecordDecl *RD) {
  317. llvm::json::Object Ret;
  318. FIELD2("trivial", hasTrivialCopyAssignment);
  319. FIELD2("nonTrivial", hasNonTrivialCopyAssignment);
  320. FIELD2("hasConstParam", hasCopyAssignmentWithConstParam);
  321. FIELD2("implicitHasConstParam", implicitCopyAssignmentHasConstParam);
  322. FIELD2("userDeclared", hasUserDeclaredCopyAssignment);
  323. FIELD2("needsImplicit", needsImplicitCopyAssignment);
  324. FIELD2("needsOverloadResolution", needsOverloadResolutionForCopyAssignment);
  325. return Ret;
  326. }
  327. static llvm::json::Object
  328. createMoveAssignmentDefinitionData(const CXXRecordDecl *RD) {
  329. llvm::json::Object Ret;
  330. FIELD2("exists", hasMoveAssignment);
  331. FIELD2("simple", hasSimpleMoveAssignment);
  332. FIELD2("trivial", hasTrivialMoveAssignment);
  333. FIELD2("nonTrivial", hasNonTrivialMoveAssignment);
  334. FIELD2("userDeclared", hasUserDeclaredMoveAssignment);
  335. FIELD2("needsImplicit", needsImplicitMoveAssignment);
  336. FIELD2("needsOverloadResolution", needsOverloadResolutionForMoveAssignment);
  337. return Ret;
  338. }
  339. static llvm::json::Object
  340. createDestructorDefinitionData(const CXXRecordDecl *RD) {
  341. llvm::json::Object Ret;
  342. FIELD2("simple", hasSimpleDestructor);
  343. FIELD2("irrelevant", hasIrrelevantDestructor);
  344. FIELD2("trivial", hasTrivialDestructor);
  345. FIELD2("nonTrivial", hasNonTrivialDestructor);
  346. FIELD2("userDeclared", hasUserDeclaredDestructor);
  347. FIELD2("needsImplicit", needsImplicitDestructor);
  348. FIELD2("needsOverloadResolution", needsOverloadResolutionForDestructor);
  349. if (!RD->needsOverloadResolutionForDestructor())
  350. FIELD2("defaultedIsDeleted", defaultedDestructorIsDeleted);
  351. return Ret;
  352. }
  353. llvm::json::Object
  354. JSONNodeDumper::createCXXRecordDefinitionData(const CXXRecordDecl *RD) {
  355. llvm::json::Object Ret;
  356. // This data is common to all C++ classes.
  357. FIELD1(isGenericLambda);
  358. FIELD1(isLambda);
  359. FIELD1(isEmpty);
  360. FIELD1(isAggregate);
  361. FIELD1(isStandardLayout);
  362. FIELD1(isTriviallyCopyable);
  363. FIELD1(isPOD);
  364. FIELD1(isTrivial);
  365. FIELD1(isPolymorphic);
  366. FIELD1(isAbstract);
  367. FIELD1(isLiteral);
  368. FIELD1(canPassInRegisters);
  369. FIELD1(hasUserDeclaredConstructor);
  370. FIELD1(hasConstexprNonCopyMoveConstructor);
  371. FIELD1(hasMutableFields);
  372. FIELD1(hasVariantMembers);
  373. FIELD2("canConstDefaultInit", allowConstDefaultInit);
  374. Ret["defaultCtor"] = createDefaultConstructorDefinitionData(RD);
  375. Ret["copyCtor"] = createCopyConstructorDefinitionData(RD);
  376. Ret["moveCtor"] = createMoveConstructorDefinitionData(RD);
  377. Ret["copyAssign"] = createCopyAssignmentDefinitionData(RD);
  378. Ret["moveAssign"] = createMoveAssignmentDefinitionData(RD);
  379. Ret["dtor"] = createDestructorDefinitionData(RD);
  380. return Ret;
  381. }
  382. #undef FIELD1
  383. #undef FIELD2
  384. std::string JSONNodeDumper::createAccessSpecifier(AccessSpecifier AS) {
  385. switch (AS) {
  386. case AS_none: return "none";
  387. case AS_private: return "private";
  388. case AS_protected: return "protected";
  389. case AS_public: return "public";
  390. }
  391. llvm_unreachable("Unknown access specifier");
  392. }
  393. llvm::json::Object
  394. JSONNodeDumper::createCXXBaseSpecifier(const CXXBaseSpecifier &BS) {
  395. llvm::json::Object Ret;
  396. Ret["type"] = createQualType(BS.getType());
  397. Ret["access"] = createAccessSpecifier(BS.getAccessSpecifier());
  398. Ret["writtenAccess"] =
  399. createAccessSpecifier(BS.getAccessSpecifierAsWritten());
  400. if (BS.isVirtual())
  401. Ret["isVirtual"] = true;
  402. if (BS.isPackExpansion())
  403. Ret["isPackExpansion"] = true;
  404. return Ret;
  405. }
  406. void JSONNodeDumper::VisitTypedefType(const TypedefType *TT) {
  407. JOS.attribute("decl", createBareDeclRef(TT->getDecl()));
  408. }
  409. void JSONNodeDumper::VisitFunctionType(const FunctionType *T) {
  410. FunctionType::ExtInfo E = T->getExtInfo();
  411. attributeOnlyIfTrue("noreturn", E.getNoReturn());
  412. attributeOnlyIfTrue("producesResult", E.getProducesResult());
  413. if (E.getHasRegParm())
  414. JOS.attribute("regParm", E.getRegParm());
  415. JOS.attribute("cc", FunctionType::getNameForCallConv(E.getCC()));
  416. }
  417. void JSONNodeDumper::VisitFunctionProtoType(const FunctionProtoType *T) {
  418. FunctionProtoType::ExtProtoInfo E = T->getExtProtoInfo();
  419. attributeOnlyIfTrue("trailingReturn", E.HasTrailingReturn);
  420. attributeOnlyIfTrue("const", T->isConst());
  421. attributeOnlyIfTrue("volatile", T->isVolatile());
  422. attributeOnlyIfTrue("restrict", T->isRestrict());
  423. attributeOnlyIfTrue("variadic", E.Variadic);
  424. switch (E.RefQualifier) {
  425. case RQ_LValue: JOS.attribute("refQualifier", "&"); break;
  426. case RQ_RValue: JOS.attribute("refQualifier", "&&"); break;
  427. case RQ_None: break;
  428. }
  429. switch (E.ExceptionSpec.Type) {
  430. case EST_DynamicNone:
  431. case EST_Dynamic: {
  432. JOS.attribute("exceptionSpec", "throw");
  433. llvm::json::Array Types;
  434. for (QualType QT : E.ExceptionSpec.Exceptions)
  435. Types.push_back(createQualType(QT));
  436. JOS.attribute("exceptionTypes", std::move(Types));
  437. } break;
  438. case EST_MSAny:
  439. JOS.attribute("exceptionSpec", "throw");
  440. JOS.attribute("throwsAny", true);
  441. break;
  442. case EST_BasicNoexcept:
  443. JOS.attribute("exceptionSpec", "noexcept");
  444. break;
  445. case EST_NoexceptTrue:
  446. case EST_NoexceptFalse:
  447. JOS.attribute("exceptionSpec", "noexcept");
  448. JOS.attribute("conditionEvaluatesTo",
  449. E.ExceptionSpec.Type == EST_NoexceptTrue);
  450. //JOS.attributeWithCall("exceptionSpecExpr",
  451. // [this, E]() { Visit(E.ExceptionSpec.NoexceptExpr); });
  452. break;
  453. case EST_NoThrow:
  454. JOS.attribute("exceptionSpec", "nothrow");
  455. break;
  456. // FIXME: I cannot find a way to trigger these cases while dumping the AST. I
  457. // suspect you can only run into them when executing an AST dump from within
  458. // the debugger, which is not a use case we worry about for the JSON dumping
  459. // feature.
  460. case EST_DependentNoexcept:
  461. case EST_Unevaluated:
  462. case EST_Uninstantiated:
  463. case EST_Unparsed:
  464. case EST_None: break;
  465. }
  466. VisitFunctionType(T);
  467. }
  468. void JSONNodeDumper::VisitRValueReferenceType(const ReferenceType *RT) {
  469. attributeOnlyIfTrue("spelledAsLValue", RT->isSpelledAsLValue());
  470. }
  471. void JSONNodeDumper::VisitArrayType(const ArrayType *AT) {
  472. switch (AT->getSizeModifier()) {
  473. case ArrayType::Star:
  474. JOS.attribute("sizeModifier", "*");
  475. break;
  476. case ArrayType::Static:
  477. JOS.attribute("sizeModifier", "static");
  478. break;
  479. case ArrayType::Normal:
  480. break;
  481. }
  482. std::string Str = AT->getIndexTypeQualifiers().getAsString();
  483. if (!Str.empty())
  484. JOS.attribute("indexTypeQualifiers", Str);
  485. }
  486. void JSONNodeDumper::VisitConstantArrayType(const ConstantArrayType *CAT) {
  487. // FIXME: this should use ZExt instead of SExt, but JSON doesn't allow a
  488. // narrowing conversion to int64_t so it cannot be expressed.
  489. JOS.attribute("size", CAT->getSize().getSExtValue());
  490. VisitArrayType(CAT);
  491. }
  492. void JSONNodeDumper::VisitDependentSizedExtVectorType(
  493. const DependentSizedExtVectorType *VT) {
  494. JOS.attributeObject(
  495. "attrLoc", [VT, this] { writeSourceLocation(VT->getAttributeLoc()); });
  496. }
  497. void JSONNodeDumper::VisitVectorType(const VectorType *VT) {
  498. JOS.attribute("numElements", VT->getNumElements());
  499. switch (VT->getVectorKind()) {
  500. case VectorType::GenericVector:
  501. break;
  502. case VectorType::AltiVecVector:
  503. JOS.attribute("vectorKind", "altivec");
  504. break;
  505. case VectorType::AltiVecPixel:
  506. JOS.attribute("vectorKind", "altivec pixel");
  507. break;
  508. case VectorType::AltiVecBool:
  509. JOS.attribute("vectorKind", "altivec bool");
  510. break;
  511. case VectorType::NeonVector:
  512. JOS.attribute("vectorKind", "neon");
  513. break;
  514. case VectorType::NeonPolyVector:
  515. JOS.attribute("vectorKind", "neon poly");
  516. break;
  517. }
  518. }
  519. void JSONNodeDumper::VisitUnresolvedUsingType(const UnresolvedUsingType *UUT) {
  520. JOS.attribute("decl", createBareDeclRef(UUT->getDecl()));
  521. }
  522. void JSONNodeDumper::VisitUnaryTransformType(const UnaryTransformType *UTT) {
  523. switch (UTT->getUTTKind()) {
  524. case UnaryTransformType::EnumUnderlyingType:
  525. JOS.attribute("transformKind", "underlying_type");
  526. break;
  527. }
  528. }
  529. void JSONNodeDumper::VisitTagType(const TagType *TT) {
  530. JOS.attribute("decl", createBareDeclRef(TT->getDecl()));
  531. }
  532. void JSONNodeDumper::VisitTemplateTypeParmType(
  533. const TemplateTypeParmType *TTPT) {
  534. JOS.attribute("depth", TTPT->getDepth());
  535. JOS.attribute("index", TTPT->getIndex());
  536. attributeOnlyIfTrue("isPack", TTPT->isParameterPack());
  537. JOS.attribute("decl", createBareDeclRef(TTPT->getDecl()));
  538. }
  539. void JSONNodeDumper::VisitAutoType(const AutoType *AT) {
  540. JOS.attribute("undeduced", !AT->isDeduced());
  541. switch (AT->getKeyword()) {
  542. case AutoTypeKeyword::Auto:
  543. JOS.attribute("typeKeyword", "auto");
  544. break;
  545. case AutoTypeKeyword::DecltypeAuto:
  546. JOS.attribute("typeKeyword", "decltype(auto)");
  547. break;
  548. case AutoTypeKeyword::GNUAutoType:
  549. JOS.attribute("typeKeyword", "__auto_type");
  550. break;
  551. }
  552. }
  553. void JSONNodeDumper::VisitTemplateSpecializationType(
  554. const TemplateSpecializationType *TST) {
  555. attributeOnlyIfTrue("isAlias", TST->isTypeAlias());
  556. std::string Str;
  557. llvm::raw_string_ostream OS(Str);
  558. TST->getTemplateName().print(OS, PrintPolicy);
  559. JOS.attribute("templateName", OS.str());
  560. }
  561. void JSONNodeDumper::VisitInjectedClassNameType(
  562. const InjectedClassNameType *ICNT) {
  563. JOS.attribute("decl", createBareDeclRef(ICNT->getDecl()));
  564. }
  565. void JSONNodeDumper::VisitObjCInterfaceType(const ObjCInterfaceType *OIT) {
  566. JOS.attribute("decl", createBareDeclRef(OIT->getDecl()));
  567. }
  568. void JSONNodeDumper::VisitPackExpansionType(const PackExpansionType *PET) {
  569. if (llvm::Optional<unsigned> N = PET->getNumExpansions())
  570. JOS.attribute("numExpansions", *N);
  571. }
  572. void JSONNodeDumper::VisitElaboratedType(const ElaboratedType *ET) {
  573. if (const NestedNameSpecifier *NNS = ET->getQualifier()) {
  574. std::string Str;
  575. llvm::raw_string_ostream OS(Str);
  576. NNS->print(OS, PrintPolicy, /*ResolveTemplateArgs*/ true);
  577. JOS.attribute("qualifier", OS.str());
  578. }
  579. if (const TagDecl *TD = ET->getOwnedTagDecl())
  580. JOS.attribute("ownedTagDecl", createBareDeclRef(TD));
  581. }
  582. void JSONNodeDumper::VisitMacroQualifiedType(const MacroQualifiedType *MQT) {
  583. JOS.attribute("macroName", MQT->getMacroIdentifier()->getName());
  584. }
  585. void JSONNodeDumper::VisitMemberPointerType(const MemberPointerType *MPT) {
  586. attributeOnlyIfTrue("isData", MPT->isMemberDataPointer());
  587. attributeOnlyIfTrue("isFunction", MPT->isMemberFunctionPointer());
  588. }
  589. void JSONNodeDumper::VisitNamedDecl(const NamedDecl *ND) {
  590. if (ND && ND->getDeclName())
  591. JOS.attribute("name", ND->getNameAsString());
  592. }
  593. void JSONNodeDumper::VisitTypedefDecl(const TypedefDecl *TD) {
  594. VisitNamedDecl(TD);
  595. JOS.attribute("type", createQualType(TD->getUnderlyingType()));
  596. }
  597. void JSONNodeDumper::VisitTypeAliasDecl(const TypeAliasDecl *TAD) {
  598. VisitNamedDecl(TAD);
  599. JOS.attribute("type", createQualType(TAD->getUnderlyingType()));
  600. }
  601. void JSONNodeDumper::VisitNamespaceDecl(const NamespaceDecl *ND) {
  602. VisitNamedDecl(ND);
  603. attributeOnlyIfTrue("isInline", ND->isInline());
  604. if (!ND->isOriginalNamespace())
  605. JOS.attribute("originalNamespace",
  606. createBareDeclRef(ND->getOriginalNamespace()));
  607. }
  608. void JSONNodeDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *UDD) {
  609. JOS.attribute("nominatedNamespace",
  610. createBareDeclRef(UDD->getNominatedNamespace()));
  611. }
  612. void JSONNodeDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *NAD) {
  613. VisitNamedDecl(NAD);
  614. JOS.attribute("aliasedNamespace",
  615. createBareDeclRef(NAD->getAliasedNamespace()));
  616. }
  617. void JSONNodeDumper::VisitUsingDecl(const UsingDecl *UD) {
  618. std::string Name;
  619. if (const NestedNameSpecifier *NNS = UD->getQualifier()) {
  620. llvm::raw_string_ostream SOS(Name);
  621. NNS->print(SOS, UD->getASTContext().getPrintingPolicy());
  622. }
  623. Name += UD->getNameAsString();
  624. JOS.attribute("name", Name);
  625. }
  626. void JSONNodeDumper::VisitUsingShadowDecl(const UsingShadowDecl *USD) {
  627. JOS.attribute("target", createBareDeclRef(USD->getTargetDecl()));
  628. }
  629. void JSONNodeDumper::VisitVarDecl(const VarDecl *VD) {
  630. VisitNamedDecl(VD);
  631. JOS.attribute("type", createQualType(VD->getType()));
  632. StorageClass SC = VD->getStorageClass();
  633. if (SC != SC_None)
  634. JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC));
  635. switch (VD->getTLSKind()) {
  636. case VarDecl::TLS_Dynamic: JOS.attribute("tls", "dynamic"); break;
  637. case VarDecl::TLS_Static: JOS.attribute("tls", "static"); break;
  638. case VarDecl::TLS_None: break;
  639. }
  640. attributeOnlyIfTrue("nrvo", VD->isNRVOVariable());
  641. attributeOnlyIfTrue("inline", VD->isInline());
  642. attributeOnlyIfTrue("constexpr", VD->isConstexpr());
  643. attributeOnlyIfTrue("modulePrivate", VD->isModulePrivate());
  644. if (VD->hasInit()) {
  645. switch (VD->getInitStyle()) {
  646. case VarDecl::CInit: JOS.attribute("init", "c"); break;
  647. case VarDecl::CallInit: JOS.attribute("init", "call"); break;
  648. case VarDecl::ListInit: JOS.attribute("init", "list"); break;
  649. }
  650. }
  651. attributeOnlyIfTrue("isParameterPack", VD->isParameterPack());
  652. }
  653. void JSONNodeDumper::VisitFieldDecl(const FieldDecl *FD) {
  654. VisitNamedDecl(FD);
  655. JOS.attribute("type", createQualType(FD->getType()));
  656. attributeOnlyIfTrue("mutable", FD->isMutable());
  657. attributeOnlyIfTrue("modulePrivate", FD->isModulePrivate());
  658. attributeOnlyIfTrue("isBitfield", FD->isBitField());
  659. attributeOnlyIfTrue("hasInClassInitializer", FD->hasInClassInitializer());
  660. }
  661. void JSONNodeDumper::VisitFunctionDecl(const FunctionDecl *FD) {
  662. VisitNamedDecl(FD);
  663. JOS.attribute("type", createQualType(FD->getType()));
  664. StorageClass SC = FD->getStorageClass();
  665. if (SC != SC_None)
  666. JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC));
  667. attributeOnlyIfTrue("inline", FD->isInlineSpecified());
  668. attributeOnlyIfTrue("virtual", FD->isVirtualAsWritten());
  669. attributeOnlyIfTrue("pure", FD->isPure());
  670. attributeOnlyIfTrue("explicitlyDeleted", FD->isDeletedAsWritten());
  671. attributeOnlyIfTrue("constexpr", FD->isConstexpr());
  672. attributeOnlyIfTrue("variadic", FD->isVariadic());
  673. if (FD->isDefaulted())
  674. JOS.attribute("explicitlyDefaulted",
  675. FD->isDeleted() ? "deleted" : "default");
  676. }
  677. void JSONNodeDumper::VisitEnumDecl(const EnumDecl *ED) {
  678. VisitNamedDecl(ED);
  679. if (ED->isFixed())
  680. JOS.attribute("fixedUnderlyingType", createQualType(ED->getIntegerType()));
  681. if (ED->isScoped())
  682. JOS.attribute("scopedEnumTag",
  683. ED->isScopedUsingClassTag() ? "class" : "struct");
  684. }
  685. void JSONNodeDumper::VisitEnumConstantDecl(const EnumConstantDecl *ECD) {
  686. VisitNamedDecl(ECD);
  687. JOS.attribute("type", createQualType(ECD->getType()));
  688. }
  689. void JSONNodeDumper::VisitRecordDecl(const RecordDecl *RD) {
  690. VisitNamedDecl(RD);
  691. JOS.attribute("tagUsed", RD->getKindName());
  692. attributeOnlyIfTrue("completeDefinition", RD->isCompleteDefinition());
  693. }
  694. void JSONNodeDumper::VisitCXXRecordDecl(const CXXRecordDecl *RD) {
  695. VisitRecordDecl(RD);
  696. // All other information requires a complete definition.
  697. if (!RD->isCompleteDefinition())
  698. return;
  699. JOS.attribute("definitionData", createCXXRecordDefinitionData(RD));
  700. if (RD->getNumBases()) {
  701. JOS.attributeArray("bases", [this, RD] {
  702. for (const auto &Spec : RD->bases())
  703. JOS.value(createCXXBaseSpecifier(Spec));
  704. });
  705. }
  706. }
  707. void JSONNodeDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
  708. VisitNamedDecl(D);
  709. JOS.attribute("tagUsed", D->wasDeclaredWithTypename() ? "typename" : "class");
  710. JOS.attribute("depth", D->getDepth());
  711. JOS.attribute("index", D->getIndex());
  712. attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
  713. if (D->hasDefaultArgument())
  714. JOS.attributeObject("defaultArg", [=] {
  715. Visit(D->getDefaultArgument(), SourceRange(),
  716. D->getDefaultArgStorage().getInheritedFrom(),
  717. D->defaultArgumentWasInherited() ? "inherited from" : "previous");
  718. });
  719. }
  720. void JSONNodeDumper::VisitNonTypeTemplateParmDecl(
  721. const NonTypeTemplateParmDecl *D) {
  722. VisitNamedDecl(D);
  723. JOS.attribute("type", createQualType(D->getType()));
  724. JOS.attribute("depth", D->getDepth());
  725. JOS.attribute("index", D->getIndex());
  726. attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
  727. if (D->hasDefaultArgument())
  728. JOS.attributeObject("defaultArg", [=] {
  729. Visit(D->getDefaultArgument(), SourceRange(),
  730. D->getDefaultArgStorage().getInheritedFrom(),
  731. D->defaultArgumentWasInherited() ? "inherited from" : "previous");
  732. });
  733. }
  734. void JSONNodeDumper::VisitTemplateTemplateParmDecl(
  735. const TemplateTemplateParmDecl *D) {
  736. VisitNamedDecl(D);
  737. JOS.attribute("depth", D->getDepth());
  738. JOS.attribute("index", D->getIndex());
  739. attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
  740. if (D->hasDefaultArgument())
  741. JOS.attributeObject("defaultArg", [=] {
  742. Visit(D->getDefaultArgument().getArgument(),
  743. D->getDefaultArgStorage().getInheritedFrom()->getSourceRange(),
  744. D->getDefaultArgStorage().getInheritedFrom(),
  745. D->defaultArgumentWasInherited() ? "inherited from" : "previous");
  746. });
  747. }
  748. void JSONNodeDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *LSD) {
  749. StringRef Lang;
  750. switch (LSD->getLanguage()) {
  751. case LinkageSpecDecl::lang_c: Lang = "C"; break;
  752. case LinkageSpecDecl::lang_cxx: Lang = "C++"; break;
  753. case LinkageSpecDecl::lang_cxx_11:
  754. Lang = "C++11";
  755. break;
  756. case LinkageSpecDecl::lang_cxx_14:
  757. Lang = "C++14";
  758. break;
  759. }
  760. JOS.attribute("language", Lang);
  761. attributeOnlyIfTrue("hasBraces", LSD->hasBraces());
  762. }
  763. void JSONNodeDumper::VisitAccessSpecDecl(const AccessSpecDecl *ASD) {
  764. JOS.attribute("access", createAccessSpecifier(ASD->getAccess()));
  765. }
  766. void JSONNodeDumper::VisitFriendDecl(const FriendDecl *FD) {
  767. if (const TypeSourceInfo *T = FD->getFriendType())
  768. JOS.attribute("type", createQualType(T->getType()));
  769. }
  770. void JSONNodeDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
  771. VisitNamedDecl(D);
  772. JOS.attribute("type", createQualType(D->getType()));
  773. attributeOnlyIfTrue("synthesized", D->getSynthesize());
  774. switch (D->getAccessControl()) {
  775. case ObjCIvarDecl::None: JOS.attribute("access", "none"); break;
  776. case ObjCIvarDecl::Private: JOS.attribute("access", "private"); break;
  777. case ObjCIvarDecl::Protected: JOS.attribute("access", "protected"); break;
  778. case ObjCIvarDecl::Public: JOS.attribute("access", "public"); break;
  779. case ObjCIvarDecl::Package: JOS.attribute("access", "package"); break;
  780. }
  781. }
  782. void JSONNodeDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
  783. VisitNamedDecl(D);
  784. JOS.attribute("returnType", createQualType(D->getReturnType()));
  785. JOS.attribute("instance", D->isInstanceMethod());
  786. attributeOnlyIfTrue("variadic", D->isVariadic());
  787. }
  788. void JSONNodeDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) {
  789. VisitNamedDecl(D);
  790. JOS.attribute("type", createQualType(D->getUnderlyingType()));
  791. attributeOnlyIfTrue("bounded", D->hasExplicitBound());
  792. switch (D->getVariance()) {
  793. case ObjCTypeParamVariance::Invariant:
  794. break;
  795. case ObjCTypeParamVariance::Covariant:
  796. JOS.attribute("variance", "covariant");
  797. break;
  798. case ObjCTypeParamVariance::Contravariant:
  799. JOS.attribute("variance", "contravariant");
  800. break;
  801. }
  802. }
  803. void JSONNodeDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
  804. VisitNamedDecl(D);
  805. JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
  806. JOS.attribute("implementation", createBareDeclRef(D->getImplementation()));
  807. llvm::json::Array Protocols;
  808. for (const auto* P : D->protocols())
  809. Protocols.push_back(createBareDeclRef(P));
  810. if (!Protocols.empty())
  811. JOS.attribute("protocols", std::move(Protocols));
  812. }
  813. void JSONNodeDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
  814. VisitNamedDecl(D);
  815. JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
  816. JOS.attribute("categoryDecl", createBareDeclRef(D->getCategoryDecl()));
  817. }
  818. void JSONNodeDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
  819. VisitNamedDecl(D);
  820. llvm::json::Array Protocols;
  821. for (const auto *P : D->protocols())
  822. Protocols.push_back(createBareDeclRef(P));
  823. if (!Protocols.empty())
  824. JOS.attribute("protocols", std::move(Protocols));
  825. }
  826. void JSONNodeDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
  827. VisitNamedDecl(D);
  828. JOS.attribute("super", createBareDeclRef(D->getSuperClass()));
  829. JOS.attribute("implementation", createBareDeclRef(D->getImplementation()));
  830. llvm::json::Array Protocols;
  831. for (const auto* P : D->protocols())
  832. Protocols.push_back(createBareDeclRef(P));
  833. if (!Protocols.empty())
  834. JOS.attribute("protocols", std::move(Protocols));
  835. }
  836. void JSONNodeDumper::VisitObjCImplementationDecl(
  837. const ObjCImplementationDecl *D) {
  838. VisitNamedDecl(D);
  839. JOS.attribute("super", createBareDeclRef(D->getSuperClass()));
  840. JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
  841. }
  842. void JSONNodeDumper::VisitObjCCompatibleAliasDecl(
  843. const ObjCCompatibleAliasDecl *D) {
  844. VisitNamedDecl(D);
  845. JOS.attribute("interface", createBareDeclRef(D->getClassInterface()));
  846. }
  847. void JSONNodeDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
  848. VisitNamedDecl(D);
  849. JOS.attribute("type", createQualType(D->getType()));
  850. switch (D->getPropertyImplementation()) {
  851. case ObjCPropertyDecl::None: break;
  852. case ObjCPropertyDecl::Required: JOS.attribute("control", "required"); break;
  853. case ObjCPropertyDecl::Optional: JOS.attribute("control", "optional"); break;
  854. }
  855. ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
  856. if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
  857. if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
  858. JOS.attribute("getter", createBareDeclRef(D->getGetterMethodDecl()));
  859. if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
  860. JOS.attribute("setter", createBareDeclRef(D->getSetterMethodDecl()));
  861. attributeOnlyIfTrue("readonly", Attrs & ObjCPropertyDecl::OBJC_PR_readonly);
  862. attributeOnlyIfTrue("assign", Attrs & ObjCPropertyDecl::OBJC_PR_assign);
  863. attributeOnlyIfTrue("readwrite",
  864. Attrs & ObjCPropertyDecl::OBJC_PR_readwrite);
  865. attributeOnlyIfTrue("retain", Attrs & ObjCPropertyDecl::OBJC_PR_retain);
  866. attributeOnlyIfTrue("copy", Attrs & ObjCPropertyDecl::OBJC_PR_copy);
  867. attributeOnlyIfTrue("nonatomic",
  868. Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic);
  869. attributeOnlyIfTrue("atomic", Attrs & ObjCPropertyDecl::OBJC_PR_atomic);
  870. attributeOnlyIfTrue("weak", Attrs & ObjCPropertyDecl::OBJC_PR_weak);
  871. attributeOnlyIfTrue("strong", Attrs & ObjCPropertyDecl::OBJC_PR_strong);
  872. attributeOnlyIfTrue("unsafe_unretained",
  873. Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
  874. attributeOnlyIfTrue("class", Attrs & ObjCPropertyDecl::OBJC_PR_class);
  875. attributeOnlyIfTrue("nullability",
  876. Attrs & ObjCPropertyDecl::OBJC_PR_nullability);
  877. attributeOnlyIfTrue("null_resettable",
  878. Attrs & ObjCPropertyDecl::OBJC_PR_null_resettable);
  879. }
  880. }
  881. void JSONNodeDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
  882. VisitNamedDecl(D->getPropertyDecl());
  883. JOS.attribute("implKind", D->getPropertyImplementation() ==
  884. ObjCPropertyImplDecl::Synthesize
  885. ? "synthesize"
  886. : "dynamic");
  887. JOS.attribute("propertyDecl", createBareDeclRef(D->getPropertyDecl()));
  888. JOS.attribute("ivarDecl", createBareDeclRef(D->getPropertyIvarDecl()));
  889. }
  890. void JSONNodeDumper::VisitBlockDecl(const BlockDecl *D) {
  891. attributeOnlyIfTrue("variadic", D->isVariadic());
  892. attributeOnlyIfTrue("capturesThis", D->capturesCXXThis());
  893. }
  894. void JSONNodeDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *OEE) {
  895. JOS.attribute("encodedType", createQualType(OEE->getEncodedType()));
  896. }
  897. void JSONNodeDumper::VisitObjCMessageExpr(const ObjCMessageExpr *OME) {
  898. std::string Str;
  899. llvm::raw_string_ostream OS(Str);
  900. OME->getSelector().print(OS);
  901. JOS.attribute("selector", OS.str());
  902. switch (OME->getReceiverKind()) {
  903. case ObjCMessageExpr::Instance:
  904. JOS.attribute("receiverKind", "instance");
  905. break;
  906. case ObjCMessageExpr::Class:
  907. JOS.attribute("receiverKind", "class");
  908. JOS.attribute("classType", createQualType(OME->getClassReceiver()));
  909. break;
  910. case ObjCMessageExpr::SuperInstance:
  911. JOS.attribute("receiverKind", "super (instance)");
  912. JOS.attribute("superType", createQualType(OME->getSuperType()));
  913. break;
  914. case ObjCMessageExpr::SuperClass:
  915. JOS.attribute("receiverKind", "super (class)");
  916. JOS.attribute("superType", createQualType(OME->getSuperType()));
  917. break;
  918. }
  919. QualType CallReturnTy = OME->getCallReturnType(Ctx);
  920. if (OME->getType() != CallReturnTy)
  921. JOS.attribute("callReturnType", createQualType(CallReturnTy));
  922. }
  923. void JSONNodeDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *OBE) {
  924. if (const ObjCMethodDecl *MD = OBE->getBoxingMethod()) {
  925. std::string Str;
  926. llvm::raw_string_ostream OS(Str);
  927. MD->getSelector().print(OS);
  928. JOS.attribute("selector", OS.str());
  929. }
  930. }
  931. void JSONNodeDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *OSE) {
  932. std::string Str;
  933. llvm::raw_string_ostream OS(Str);
  934. OSE->getSelector().print(OS);
  935. JOS.attribute("selector", OS.str());
  936. }
  937. void JSONNodeDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *OPE) {
  938. JOS.attribute("protocol", createBareDeclRef(OPE->getProtocol()));
  939. }
  940. void JSONNodeDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *OPRE) {
  941. if (OPRE->isImplicitProperty()) {
  942. JOS.attribute("propertyKind", "implicit");
  943. if (const ObjCMethodDecl *MD = OPRE->getImplicitPropertyGetter())
  944. JOS.attribute("getter", createBareDeclRef(MD));
  945. if (const ObjCMethodDecl *MD = OPRE->getImplicitPropertySetter())
  946. JOS.attribute("setter", createBareDeclRef(MD));
  947. } else {
  948. JOS.attribute("propertyKind", "explicit");
  949. JOS.attribute("property", createBareDeclRef(OPRE->getExplicitProperty()));
  950. }
  951. attributeOnlyIfTrue("isSuperReceiver", OPRE->isSuperReceiver());
  952. attributeOnlyIfTrue("isMessagingGetter", OPRE->isMessagingGetter());
  953. attributeOnlyIfTrue("isMessagingSetter", OPRE->isMessagingSetter());
  954. }
  955. void JSONNodeDumper::VisitObjCSubscriptRefExpr(
  956. const ObjCSubscriptRefExpr *OSRE) {
  957. JOS.attribute("subscriptKind",
  958. OSRE->isArraySubscriptRefExpr() ? "array" : "dictionary");
  959. if (const ObjCMethodDecl *MD = OSRE->getAtIndexMethodDecl())
  960. JOS.attribute("getter", createBareDeclRef(MD));
  961. if (const ObjCMethodDecl *MD = OSRE->setAtIndexMethodDecl())
  962. JOS.attribute("setter", createBareDeclRef(MD));
  963. }
  964. void JSONNodeDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *OIRE) {
  965. JOS.attribute("decl", createBareDeclRef(OIRE->getDecl()));
  966. attributeOnlyIfTrue("isFreeIvar", OIRE->isFreeIvar());
  967. JOS.attribute("isArrow", OIRE->isArrow());
  968. }
  969. void JSONNodeDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *OBLE) {
  970. JOS.attribute("value", OBLE->getValue() ? "__objc_yes" : "__objc_no");
  971. }
  972. void JSONNodeDumper::VisitDeclRefExpr(const DeclRefExpr *DRE) {
  973. JOS.attribute("referencedDecl", createBareDeclRef(DRE->getDecl()));
  974. if (DRE->getDecl() != DRE->getFoundDecl())
  975. JOS.attribute("foundReferencedDecl",
  976. createBareDeclRef(DRE->getFoundDecl()));
  977. switch (DRE->isNonOdrUse()) {
  978. case NOUR_None: break;
  979. case NOUR_Unevaluated: JOS.attribute("nonOdrUseReason", "unevaluated"); break;
  980. case NOUR_Constant: JOS.attribute("nonOdrUseReason", "constant"); break;
  981. case NOUR_Discarded: JOS.attribute("nonOdrUseReason", "discarded"); break;
  982. }
  983. }
  984. void JSONNodeDumper::VisitPredefinedExpr(const PredefinedExpr *PE) {
  985. JOS.attribute("name", PredefinedExpr::getIdentKindName(PE->getIdentKind()));
  986. }
  987. void JSONNodeDumper::VisitUnaryOperator(const UnaryOperator *UO) {
  988. JOS.attribute("isPostfix", UO->isPostfix());
  989. JOS.attribute("opcode", UnaryOperator::getOpcodeStr(UO->getOpcode()));
  990. if (!UO->canOverflow())
  991. JOS.attribute("canOverflow", false);
  992. }
  993. void JSONNodeDumper::VisitBinaryOperator(const BinaryOperator *BO) {
  994. JOS.attribute("opcode", BinaryOperator::getOpcodeStr(BO->getOpcode()));
  995. }
  996. void JSONNodeDumper::VisitCompoundAssignOperator(
  997. const CompoundAssignOperator *CAO) {
  998. VisitBinaryOperator(CAO);
  999. JOS.attribute("computeLHSType", createQualType(CAO->getComputationLHSType()));
  1000. JOS.attribute("computeResultType",
  1001. createQualType(CAO->getComputationResultType()));
  1002. }
  1003. void JSONNodeDumper::VisitMemberExpr(const MemberExpr *ME) {
  1004. // Note, we always write this Boolean field because the information it conveys
  1005. // is critical to understanding the AST node.
  1006. ValueDecl *VD = ME->getMemberDecl();
  1007. JOS.attribute("name", VD && VD->getDeclName() ? VD->getNameAsString() : "");
  1008. JOS.attribute("isArrow", ME->isArrow());
  1009. JOS.attribute("referencedMemberDecl", createPointerRepresentation(VD));
  1010. switch (ME->isNonOdrUse()) {
  1011. case NOUR_None: break;
  1012. case NOUR_Unevaluated: JOS.attribute("nonOdrUseReason", "unevaluated"); break;
  1013. case NOUR_Constant: JOS.attribute("nonOdrUseReason", "constant"); break;
  1014. case NOUR_Discarded: JOS.attribute("nonOdrUseReason", "discarded"); break;
  1015. }
  1016. }
  1017. void JSONNodeDumper::VisitCXXNewExpr(const CXXNewExpr *NE) {
  1018. attributeOnlyIfTrue("isGlobal", NE->isGlobalNew());
  1019. attributeOnlyIfTrue("isArray", NE->isArray());
  1020. attributeOnlyIfTrue("isPlacement", NE->getNumPlacementArgs() != 0);
  1021. switch (NE->getInitializationStyle()) {
  1022. case CXXNewExpr::NoInit: break;
  1023. case CXXNewExpr::CallInit: JOS.attribute("initStyle", "call"); break;
  1024. case CXXNewExpr::ListInit: JOS.attribute("initStyle", "list"); break;
  1025. }
  1026. if (const FunctionDecl *FD = NE->getOperatorNew())
  1027. JOS.attribute("operatorNewDecl", createBareDeclRef(FD));
  1028. if (const FunctionDecl *FD = NE->getOperatorDelete())
  1029. JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD));
  1030. }
  1031. void JSONNodeDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *DE) {
  1032. attributeOnlyIfTrue("isGlobal", DE->isGlobalDelete());
  1033. attributeOnlyIfTrue("isArray", DE->isArrayForm());
  1034. attributeOnlyIfTrue("isArrayAsWritten", DE->isArrayFormAsWritten());
  1035. if (const FunctionDecl *FD = DE->getOperatorDelete())
  1036. JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD));
  1037. }
  1038. void JSONNodeDumper::VisitCXXThisExpr(const CXXThisExpr *TE) {
  1039. attributeOnlyIfTrue("implicit", TE->isImplicit());
  1040. }
  1041. void JSONNodeDumper::VisitCastExpr(const CastExpr *CE) {
  1042. JOS.attribute("castKind", CE->getCastKindName());
  1043. llvm::json::Array Path = createCastPath(CE);
  1044. if (!Path.empty())
  1045. JOS.attribute("path", std::move(Path));
  1046. // FIXME: This may not be useful information as it can be obtusely gleaned
  1047. // from the inner[] array.
  1048. if (const NamedDecl *ND = CE->getConversionFunction())
  1049. JOS.attribute("conversionFunc", createBareDeclRef(ND));
  1050. }
  1051. void JSONNodeDumper::VisitImplicitCastExpr(const ImplicitCastExpr *ICE) {
  1052. VisitCastExpr(ICE);
  1053. attributeOnlyIfTrue("isPartOfExplicitCast", ICE->isPartOfExplicitCast());
  1054. }
  1055. void JSONNodeDumper::VisitCallExpr(const CallExpr *CE) {
  1056. attributeOnlyIfTrue("adl", CE->usesADL());
  1057. }
  1058. void JSONNodeDumper::VisitUnaryExprOrTypeTraitExpr(
  1059. const UnaryExprOrTypeTraitExpr *TTE) {
  1060. switch (TTE->getKind()) {
  1061. case UETT_SizeOf: JOS.attribute("name", "sizeof"); break;
  1062. case UETT_AlignOf: JOS.attribute("name", "alignof"); break;
  1063. case UETT_VecStep: JOS.attribute("name", "vec_step"); break;
  1064. case UETT_PreferredAlignOf: JOS.attribute("name", "__alignof"); break;
  1065. case UETT_OpenMPRequiredSimdAlign:
  1066. JOS.attribute("name", "__builtin_omp_required_simd_align"); break;
  1067. }
  1068. if (TTE->isArgumentType())
  1069. JOS.attribute("argType", createQualType(TTE->getArgumentType()));
  1070. }
  1071. void JSONNodeDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *SOPE) {
  1072. VisitNamedDecl(SOPE->getPack());
  1073. }
  1074. void JSONNodeDumper::VisitUnresolvedLookupExpr(
  1075. const UnresolvedLookupExpr *ULE) {
  1076. JOS.attribute("usesADL", ULE->requiresADL());
  1077. JOS.attribute("name", ULE->getName().getAsString());
  1078. JOS.attributeArray("lookups", [this, ULE] {
  1079. for (const NamedDecl *D : ULE->decls())
  1080. JOS.value(createBareDeclRef(D));
  1081. });
  1082. }
  1083. void JSONNodeDumper::VisitAddrLabelExpr(const AddrLabelExpr *ALE) {
  1084. JOS.attribute("name", ALE->getLabel()->getName());
  1085. JOS.attribute("labelDeclId", createPointerRepresentation(ALE->getLabel()));
  1086. }
  1087. void JSONNodeDumper::VisitCXXTypeidExpr(const CXXTypeidExpr *CTE) {
  1088. if (CTE->isTypeOperand()) {
  1089. QualType Adjusted = CTE->getTypeOperand(Ctx);
  1090. QualType Unadjusted = CTE->getTypeOperandSourceInfo()->getType();
  1091. JOS.attribute("typeArg", createQualType(Unadjusted));
  1092. if (Adjusted != Unadjusted)
  1093. JOS.attribute("adjustedTypeArg", createQualType(Adjusted));
  1094. }
  1095. }
  1096. void JSONNodeDumper::VisitConstantExpr(const ConstantExpr *CE) {
  1097. if (CE->getResultAPValueKind() != APValue::None) {
  1098. std::string Str;
  1099. llvm::raw_string_ostream OS(Str);
  1100. CE->getAPValueResult().printPretty(OS, Ctx, CE->getType());
  1101. JOS.attribute("value", OS.str());
  1102. }
  1103. }
  1104. void JSONNodeDumper::VisitInitListExpr(const InitListExpr *ILE) {
  1105. if (const FieldDecl *FD = ILE->getInitializedFieldInUnion())
  1106. JOS.attribute("field", createBareDeclRef(FD));
  1107. }
  1108. void JSONNodeDumper::VisitGenericSelectionExpr(
  1109. const GenericSelectionExpr *GSE) {
  1110. attributeOnlyIfTrue("resultDependent", GSE->isResultDependent());
  1111. }
  1112. void JSONNodeDumper::VisitCXXUnresolvedConstructExpr(
  1113. const CXXUnresolvedConstructExpr *UCE) {
  1114. if (UCE->getType() != UCE->getTypeAsWritten())
  1115. JOS.attribute("typeAsWritten", createQualType(UCE->getTypeAsWritten()));
  1116. attributeOnlyIfTrue("list", UCE->isListInitialization());
  1117. }
  1118. void JSONNodeDumper::VisitCXXConstructExpr(const CXXConstructExpr *CE) {
  1119. CXXConstructorDecl *Ctor = CE->getConstructor();
  1120. JOS.attribute("ctorType", createQualType(Ctor->getType()));
  1121. attributeOnlyIfTrue("elidable", CE->isElidable());
  1122. attributeOnlyIfTrue("list", CE->isListInitialization());
  1123. attributeOnlyIfTrue("initializer_list", CE->isStdInitListInitialization());
  1124. attributeOnlyIfTrue("zeroing", CE->requiresZeroInitialization());
  1125. attributeOnlyIfTrue("hadMultipleCandidates", CE->hadMultipleCandidates());
  1126. switch (CE->getConstructionKind()) {
  1127. case CXXConstructExpr::CK_Complete:
  1128. JOS.attribute("constructionKind", "complete");
  1129. break;
  1130. case CXXConstructExpr::CK_Delegating:
  1131. JOS.attribute("constructionKind", "delegating");
  1132. break;
  1133. case CXXConstructExpr::CK_NonVirtualBase:
  1134. JOS.attribute("constructionKind", "non-virtual base");
  1135. break;
  1136. case CXXConstructExpr::CK_VirtualBase:
  1137. JOS.attribute("constructionKind", "virtual base");
  1138. break;
  1139. }
  1140. }
  1141. void JSONNodeDumper::VisitExprWithCleanups(const ExprWithCleanups *EWC) {
  1142. attributeOnlyIfTrue("cleanupsHaveSideEffects",
  1143. EWC->cleanupsHaveSideEffects());
  1144. if (EWC->getNumObjects()) {
  1145. JOS.attributeArray("cleanups", [this, EWC] {
  1146. for (const ExprWithCleanups::CleanupObject &CO : EWC->getObjects())
  1147. JOS.value(createBareDeclRef(CO));
  1148. });
  1149. }
  1150. }
  1151. void JSONNodeDumper::VisitCXXBindTemporaryExpr(
  1152. const CXXBindTemporaryExpr *BTE) {
  1153. const CXXTemporary *Temp = BTE->getTemporary();
  1154. JOS.attribute("temp", createPointerRepresentation(Temp));
  1155. if (const CXXDestructorDecl *Dtor = Temp->getDestructor())
  1156. JOS.attribute("dtor", createBareDeclRef(Dtor));
  1157. }
  1158. void JSONNodeDumper::VisitMaterializeTemporaryExpr(
  1159. const MaterializeTemporaryExpr *MTE) {
  1160. if (const ValueDecl *VD = MTE->getExtendingDecl())
  1161. JOS.attribute("extendingDecl", createBareDeclRef(VD));
  1162. switch (MTE->getStorageDuration()) {
  1163. case SD_Automatic:
  1164. JOS.attribute("storageDuration", "automatic");
  1165. break;
  1166. case SD_Dynamic:
  1167. JOS.attribute("storageDuration", "dynamic");
  1168. break;
  1169. case SD_FullExpression:
  1170. JOS.attribute("storageDuration", "full expression");
  1171. break;
  1172. case SD_Static:
  1173. JOS.attribute("storageDuration", "static");
  1174. break;
  1175. case SD_Thread:
  1176. JOS.attribute("storageDuration", "thread");
  1177. break;
  1178. }
  1179. attributeOnlyIfTrue("boundToLValueRef", MTE->isBoundToLvalueReference());
  1180. }
  1181. void JSONNodeDumper::VisitCXXDependentScopeMemberExpr(
  1182. const CXXDependentScopeMemberExpr *DSME) {
  1183. JOS.attribute("isArrow", DSME->isArrow());
  1184. JOS.attribute("member", DSME->getMember().getAsString());
  1185. attributeOnlyIfTrue("hasTemplateKeyword", DSME->hasTemplateKeyword());
  1186. attributeOnlyIfTrue("hasExplicitTemplateArgs",
  1187. DSME->hasExplicitTemplateArgs());
  1188. if (DSME->getNumTemplateArgs()) {
  1189. JOS.attributeArray("explicitTemplateArgs", [DSME, this] {
  1190. for (const TemplateArgumentLoc &TAL : DSME->template_arguments())
  1191. JOS.object(
  1192. [&TAL, this] { Visit(TAL.getArgument(), TAL.getSourceRange()); });
  1193. });
  1194. }
  1195. }
  1196. void JSONNodeDumper::VisitIntegerLiteral(const IntegerLiteral *IL) {
  1197. JOS.attribute("value",
  1198. IL->getValue().toString(
  1199. /*Radix=*/10, IL->getType()->isSignedIntegerType()));
  1200. }
  1201. void JSONNodeDumper::VisitCharacterLiteral(const CharacterLiteral *CL) {
  1202. // FIXME: This should probably print the character literal as a string,
  1203. // rather than as a numerical value. It would be nice if the behavior matched
  1204. // what we do to print a string literal; right now, it is impossible to tell
  1205. // the difference between 'a' and L'a' in C from the JSON output.
  1206. JOS.attribute("value", CL->getValue());
  1207. }
  1208. void JSONNodeDumper::VisitFixedPointLiteral(const FixedPointLiteral *FPL) {
  1209. JOS.attribute("value", FPL->getValueAsString(/*Radix=*/10));
  1210. }
  1211. void JSONNodeDumper::VisitFloatingLiteral(const FloatingLiteral *FL) {
  1212. llvm::SmallVector<char, 16> Buffer;
  1213. FL->getValue().toString(Buffer);
  1214. JOS.attribute("value", Buffer);
  1215. }
  1216. void JSONNodeDumper::VisitStringLiteral(const StringLiteral *SL) {
  1217. std::string Buffer;
  1218. llvm::raw_string_ostream SS(Buffer);
  1219. SL->outputString(SS);
  1220. JOS.attribute("value", SS.str());
  1221. }
  1222. void JSONNodeDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *BLE) {
  1223. JOS.attribute("value", BLE->getValue());
  1224. }
  1225. void JSONNodeDumper::VisitIfStmt(const IfStmt *IS) {
  1226. attributeOnlyIfTrue("hasInit", IS->hasInitStorage());
  1227. attributeOnlyIfTrue("hasVar", IS->hasVarStorage());
  1228. attributeOnlyIfTrue("hasElse", IS->hasElseStorage());
  1229. attributeOnlyIfTrue("isConstexpr", IS->isConstexpr());
  1230. }
  1231. void JSONNodeDumper::VisitSwitchStmt(const SwitchStmt *SS) {
  1232. attributeOnlyIfTrue("hasInit", SS->hasInitStorage());
  1233. attributeOnlyIfTrue("hasVar", SS->hasVarStorage());
  1234. }
  1235. void JSONNodeDumper::VisitCaseStmt(const CaseStmt *CS) {
  1236. attributeOnlyIfTrue("isGNURange", CS->caseStmtIsGNURange());
  1237. }
  1238. void JSONNodeDumper::VisitLabelStmt(const LabelStmt *LS) {
  1239. JOS.attribute("name", LS->getName());
  1240. JOS.attribute("declId", createPointerRepresentation(LS->getDecl()));
  1241. }
  1242. void JSONNodeDumper::VisitGotoStmt(const GotoStmt *GS) {
  1243. JOS.attribute("targetLabelDeclId",
  1244. createPointerRepresentation(GS->getLabel()));
  1245. }
  1246. void JSONNodeDumper::VisitWhileStmt(const WhileStmt *WS) {
  1247. attributeOnlyIfTrue("hasVar", WS->hasVarStorage());
  1248. }
  1249. void JSONNodeDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt* OACS) {
  1250. // FIXME: it would be nice for the ASTNodeTraverser would handle the catch
  1251. // parameter the same way for C++ and ObjC rather. In this case, C++ gets a
  1252. // null child node and ObjC gets no child node.
  1253. attributeOnlyIfTrue("isCatchAll", OACS->getCatchParamDecl() == nullptr);
  1254. }
  1255. void JSONNodeDumper::VisitNullTemplateArgument(const TemplateArgument &TA) {
  1256. JOS.attribute("isNull", true);
  1257. }
  1258. void JSONNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) {
  1259. JOS.attribute("type", createQualType(TA.getAsType()));
  1260. }
  1261. void JSONNodeDumper::VisitDeclarationTemplateArgument(
  1262. const TemplateArgument &TA) {
  1263. JOS.attribute("decl", createBareDeclRef(TA.getAsDecl()));
  1264. }
  1265. void JSONNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &TA) {
  1266. JOS.attribute("isNullptr", true);
  1267. }
  1268. void JSONNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) {
  1269. JOS.attribute("value", TA.getAsIntegral().getSExtValue());
  1270. }
  1271. void JSONNodeDumper::VisitTemplateTemplateArgument(const TemplateArgument &TA) {
  1272. // FIXME: cannot just call dump() on the argument, as that doesn't specify
  1273. // the output format.
  1274. }
  1275. void JSONNodeDumper::VisitTemplateExpansionTemplateArgument(
  1276. const TemplateArgument &TA) {
  1277. // FIXME: cannot just call dump() on the argument, as that doesn't specify
  1278. // the output format.
  1279. }
  1280. void JSONNodeDumper::VisitExpressionTemplateArgument(
  1281. const TemplateArgument &TA) {
  1282. JOS.attribute("isExpr", true);
  1283. }
  1284. void JSONNodeDumper::VisitPackTemplateArgument(const TemplateArgument &TA) {
  1285. JOS.attribute("isPack", true);
  1286. }
  1287. StringRef JSONNodeDumper::getCommentCommandName(unsigned CommandID) const {
  1288. if (Traits)
  1289. return Traits->getCommandInfo(CommandID)->Name;
  1290. if (const comments::CommandInfo *Info =
  1291. comments::CommandTraits::getBuiltinCommandInfo(CommandID))
  1292. return Info->Name;
  1293. return "<invalid>";
  1294. }
  1295. void JSONNodeDumper::visitTextComment(const comments::TextComment *C,
  1296. const comments::FullComment *) {
  1297. JOS.attribute("text", C->getText());
  1298. }
  1299. void JSONNodeDumper::visitInlineCommandComment(
  1300. const comments::InlineCommandComment *C, const comments::FullComment *) {
  1301. JOS.attribute("name", getCommentCommandName(C->getCommandID()));
  1302. switch (C->getRenderKind()) {
  1303. case comments::InlineCommandComment::RenderNormal:
  1304. JOS.attribute("renderKind", "normal");
  1305. break;
  1306. case comments::InlineCommandComment::RenderBold:
  1307. JOS.attribute("renderKind", "bold");
  1308. break;
  1309. case comments::InlineCommandComment::RenderEmphasized:
  1310. JOS.attribute("renderKind", "emphasized");
  1311. break;
  1312. case comments::InlineCommandComment::RenderMonospaced:
  1313. JOS.attribute("renderKind", "monospaced");
  1314. break;
  1315. }
  1316. llvm::json::Array Args;
  1317. for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I)
  1318. Args.push_back(C->getArgText(I));
  1319. if (!Args.empty())
  1320. JOS.attribute("args", std::move(Args));
  1321. }
  1322. void JSONNodeDumper::visitHTMLStartTagComment(
  1323. const comments::HTMLStartTagComment *C, const comments::FullComment *) {
  1324. JOS.attribute("name", C->getTagName());
  1325. attributeOnlyIfTrue("selfClosing", C->isSelfClosing());
  1326. attributeOnlyIfTrue("malformed", C->isMalformed());
  1327. llvm::json::Array Attrs;
  1328. for (unsigned I = 0, E = C->getNumAttrs(); I < E; ++I)
  1329. Attrs.push_back(
  1330. {{"name", C->getAttr(I).Name}, {"value", C->getAttr(I).Value}});
  1331. if (!Attrs.empty())
  1332. JOS.attribute("attrs", std::move(Attrs));
  1333. }
  1334. void JSONNodeDumper::visitHTMLEndTagComment(
  1335. const comments::HTMLEndTagComment *C, const comments::FullComment *) {
  1336. JOS.attribute("name", C->getTagName());
  1337. }
  1338. void JSONNodeDumper::visitBlockCommandComment(
  1339. const comments::BlockCommandComment *C, const comments::FullComment *) {
  1340. JOS.attribute("name", getCommentCommandName(C->getCommandID()));
  1341. llvm::json::Array Args;
  1342. for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I)
  1343. Args.push_back(C->getArgText(I));
  1344. if (!Args.empty())
  1345. JOS.attribute("args", std::move(Args));
  1346. }
  1347. void JSONNodeDumper::visitParamCommandComment(
  1348. const comments::ParamCommandComment *C, const comments::FullComment *FC) {
  1349. switch (C->getDirection()) {
  1350. case comments::ParamCommandComment::In:
  1351. JOS.attribute("direction", "in");
  1352. break;
  1353. case comments::ParamCommandComment::Out:
  1354. JOS.attribute("direction", "out");
  1355. break;
  1356. case comments::ParamCommandComment::InOut:
  1357. JOS.attribute("direction", "in,out");
  1358. break;
  1359. }
  1360. attributeOnlyIfTrue("explicit", C->isDirectionExplicit());
  1361. if (C->hasParamName())
  1362. JOS.attribute("param", C->isParamIndexValid() ? C->getParamName(FC)
  1363. : C->getParamNameAsWritten());
  1364. if (C->isParamIndexValid() && !C->isVarArgParam())
  1365. JOS.attribute("paramIdx", C->getParamIndex());
  1366. }
  1367. void JSONNodeDumper::visitTParamCommandComment(
  1368. const comments::TParamCommandComment *C, const comments::FullComment *FC) {
  1369. if (C->hasParamName())
  1370. JOS.attribute("param", C->isPositionValid() ? C->getParamName(FC)
  1371. : C->getParamNameAsWritten());
  1372. if (C->isPositionValid()) {
  1373. llvm::json::Array Positions;
  1374. for (unsigned I = 0, E = C->getDepth(); I < E; ++I)
  1375. Positions.push_back(C->getIndex(I));
  1376. if (!Positions.empty())
  1377. JOS.attribute("positions", std::move(Positions));
  1378. }
  1379. }
  1380. void JSONNodeDumper::visitVerbatimBlockComment(
  1381. const comments::VerbatimBlockComment *C, const comments::FullComment *) {
  1382. JOS.attribute("name", getCommentCommandName(C->getCommandID()));
  1383. JOS.attribute("closeName", C->getCloseName());
  1384. }
  1385. void JSONNodeDumper::visitVerbatimBlockLineComment(
  1386. const comments::VerbatimBlockLineComment *C,
  1387. const comments::FullComment *) {
  1388. JOS.attribute("text", C->getText());
  1389. }
  1390. void JSONNodeDumper::visitVerbatimLineComment(
  1391. const comments::VerbatimLineComment *C, const comments::FullComment *) {
  1392. JOS.attribute("text", C->getText());
  1393. }