JSONNodeDumper.cpp 60 KB

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