JSONNodeDumper.cpp 57 KB

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