ASTDumper.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //===--- ASTDumper.cpp - Dumping implementation for ASTs ------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the AST dump methods, which dump out the
  10. // AST in a form that exposes type details and other fields.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ASTDumper.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/DeclLookups.h"
  16. #include "clang/AST/JSONNodeDumper.h"
  17. #include "clang/Basic/Builtins.h"
  18. #include "clang/Basic/Module.h"
  19. #include "clang/Basic/SourceManager.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. using namespace clang;
  22. using namespace clang::comments;
  23. void ASTDumper::dumpInvalidDeclContext(const DeclContext *DC) {
  24. NodeDumper.AddChild([=] {
  25. if (!DC) {
  26. ColorScope Color(OS, ShowColors, NullColor);
  27. OS << "<<<NULL>>>";
  28. return;
  29. }
  30. // An invalid DeclContext is one for which a dyn_cast() from a DeclContext
  31. // pointer to a Decl pointer would fail an assertion or otherwise fall prey
  32. // to undefined behavior as a result of an invalid associated DeclKind.
  33. // Such invalidity is not supposed to happen of course, but, when it does,
  34. // the information provided below is intended to provide some hints about
  35. // what might have gone awry.
  36. {
  37. ColorScope Color(OS, ShowColors, DeclKindNameColor);
  38. OS << "DeclContext";
  39. }
  40. NodeDumper.dumpPointer(DC);
  41. OS << " <";
  42. {
  43. ColorScope Color(OS, ShowColors, DeclNameColor);
  44. OS << "unrecognized Decl kind " << (unsigned)DC->getDeclKind();
  45. }
  46. OS << ">";
  47. });
  48. }
  49. void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
  50. NodeDumper.AddChild([=] {
  51. OS << "StoredDeclsMap ";
  52. NodeDumper.dumpBareDeclRef(cast<Decl>(DC));
  53. const DeclContext *Primary = DC->getPrimaryContext();
  54. if (Primary != DC) {
  55. OS << " primary";
  56. NodeDumper.dumpPointer(cast<Decl>(Primary));
  57. }
  58. bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
  59. auto Range = getDeserialize()
  60. ? Primary->lookups()
  61. : Primary->noload_lookups(/*PreserveInternalState=*/true);
  62. for (auto I = Range.begin(), E = Range.end(); I != E; ++I) {
  63. DeclarationName Name = I.getLookupName();
  64. DeclContextLookupResult R = *I;
  65. NodeDumper.AddChild([=] {
  66. OS << "DeclarationName ";
  67. {
  68. ColorScope Color(OS, ShowColors, DeclNameColor);
  69. OS << '\'' << Name << '\'';
  70. }
  71. for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
  72. RI != RE; ++RI) {
  73. NodeDumper.AddChild([=] {
  74. NodeDumper.dumpBareDeclRef(*RI);
  75. if (!(*RI)->isUnconditionallyVisible())
  76. OS << " hidden";
  77. // If requested, dump the redecl chain for this lookup.
  78. if (DumpDecls) {
  79. // Dump earliest decl first.
  80. std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) {
  81. if (Decl *Prev = D->getPreviousDecl())
  82. DumpWithPrev(Prev);
  83. Visit(D);
  84. };
  85. DumpWithPrev(*RI);
  86. }
  87. });
  88. }
  89. });
  90. }
  91. if (HasUndeserializedLookups) {
  92. NodeDumper.AddChild([=] {
  93. ColorScope Color(OS, ShowColors, UndeserializedColor);
  94. OS << "<undeserialized lookups>";
  95. });
  96. }
  97. });
  98. }
  99. template <typename SpecializationDecl>
  100. void ASTDumper::dumpTemplateDeclSpecialization(const SpecializationDecl *D,
  101. bool DumpExplicitInst,
  102. bool DumpRefOnly) {
  103. bool DumpedAny = false;
  104. for (const auto *RedeclWithBadType : D->redecls()) {
  105. // FIXME: The redecls() range sometimes has elements of a less-specific
  106. // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
  107. // us TagDecls, and should give CXXRecordDecls).
  108. auto *Redecl = cast<SpecializationDecl>(RedeclWithBadType);
  109. switch (Redecl->getTemplateSpecializationKind()) {
  110. case TSK_ExplicitInstantiationDeclaration:
  111. case TSK_ExplicitInstantiationDefinition:
  112. if (!DumpExplicitInst)
  113. break;
  114. [[fallthrough]];
  115. case TSK_Undeclared:
  116. case TSK_ImplicitInstantiation:
  117. if (DumpRefOnly)
  118. NodeDumper.dumpDeclRef(Redecl);
  119. else
  120. Visit(Redecl);
  121. DumpedAny = true;
  122. break;
  123. case TSK_ExplicitSpecialization:
  124. break;
  125. }
  126. }
  127. // Ensure we dump at least one decl for each specialization.
  128. if (!DumpedAny)
  129. NodeDumper.dumpDeclRef(D);
  130. }
  131. template <typename TemplateDecl>
  132. void ASTDumper::dumpTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst) {
  133. dumpTemplateParameters(D->getTemplateParameters());
  134. Visit(D->getTemplatedDecl());
  135. if (GetTraversalKind() == TK_AsIs) {
  136. for (const auto *Child : D->specializations())
  137. dumpTemplateDeclSpecialization(Child, DumpExplicitInst,
  138. !D->isCanonicalDecl());
  139. }
  140. }
  141. void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
  142. // FIXME: We don't add a declaration of a function template specialization
  143. // to its context when it's explicitly instantiated, so dump explicit
  144. // instantiations when we dump the template itself.
  145. dumpTemplateDecl(D, true);
  146. }
  147. void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
  148. dumpTemplateDecl(D, false);
  149. }
  150. void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
  151. dumpTemplateDecl(D, false);
  152. }
  153. //===----------------------------------------------------------------------===//
  154. // Type method implementations
  155. //===----------------------------------------------------------------------===//
  156. void QualType::dump(const char *msg) const {
  157. if (msg)
  158. llvm::errs() << msg << ": ";
  159. dump();
  160. }
  161. LLVM_DUMP_METHOD void QualType::dump() const {
  162. ASTDumper Dumper(llvm::errs(), /*ShowColors=*/false);
  163. Dumper.Visit(*this);
  164. }
  165. LLVM_DUMP_METHOD void QualType::dump(llvm::raw_ostream &OS,
  166. const ASTContext &Context) const {
  167. ASTDumper Dumper(OS, Context, Context.getDiagnostics().getShowColors());
  168. Dumper.Visit(*this);
  169. }
  170. LLVM_DUMP_METHOD void Type::dump() const { QualType(this, 0).dump(); }
  171. LLVM_DUMP_METHOD void Type::dump(llvm::raw_ostream &OS,
  172. const ASTContext &Context) const {
  173. QualType(this, 0).dump(OS, Context);
  174. }
  175. //===----------------------------------------------------------------------===//
  176. // Decl method implementations
  177. //===----------------------------------------------------------------------===//
  178. LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
  179. LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS, bool Deserialize,
  180. ASTDumpOutputFormat Format) const {
  181. ASTContext &Ctx = getASTContext();
  182. const SourceManager &SM = Ctx.getSourceManager();
  183. if (ADOF_JSON == Format) {
  184. JSONDumper P(OS, SM, Ctx, Ctx.getPrintingPolicy(),
  185. &Ctx.getCommentCommandTraits());
  186. (void)Deserialize; // FIXME?
  187. P.Visit(this);
  188. } else {
  189. ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors());
  190. P.setDeserialize(Deserialize);
  191. P.Visit(this);
  192. }
  193. }
  194. LLVM_DUMP_METHOD void Decl::dumpColor() const {
  195. const ASTContext &Ctx = getASTContext();
  196. ASTDumper P(llvm::errs(), Ctx, /*ShowColors=*/true);
  197. P.Visit(this);
  198. }
  199. LLVM_DUMP_METHOD void DeclContext::dumpAsDecl() const {
  200. dumpAsDecl(nullptr);
  201. }
  202. LLVM_DUMP_METHOD void DeclContext::dumpAsDecl(const ASTContext *Ctx) const {
  203. // By design, DeclContext is required to be a base class of some class that
  204. // derives from Decl. Thus, it should always be possible to dyn_cast() from
  205. // a DeclContext pointer to a Decl pointer and Decl::castFromDeclContext()
  206. // asserts that to be the case. Since this function is intended for use in a
  207. // debugger, it performs an additional check in order to prevent a failed
  208. // cast and assertion. If that check fails, then the (invalid) DeclContext
  209. // is dumped with an indication of its invalidity.
  210. if (hasValidDeclKind()) {
  211. const auto *D = cast<Decl>(this);
  212. D->dump();
  213. } else {
  214. // If an ASTContext is not available, a less capable ASTDumper is
  215. // constructed for which color diagnostics are, regrettably, disabled.
  216. ASTDumper P = Ctx ? ASTDumper(llvm::errs(), *Ctx,
  217. Ctx->getDiagnostics().getShowColors())
  218. : ASTDumper(llvm::errs(), /*ShowColors*/ false);
  219. P.dumpInvalidDeclContext(this);
  220. }
  221. }
  222. LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
  223. dumpLookups(llvm::errs());
  224. }
  225. LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
  226. bool DumpDecls,
  227. bool Deserialize) const {
  228. const DeclContext *DC = this;
  229. while (!DC->isTranslationUnit())
  230. DC = DC->getParent();
  231. const ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
  232. ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors());
  233. P.setDeserialize(Deserialize);
  234. P.dumpLookups(this, DumpDecls);
  235. }
  236. //===----------------------------------------------------------------------===//
  237. // Stmt method implementations
  238. //===----------------------------------------------------------------------===//
  239. LLVM_DUMP_METHOD void Stmt::dump() const {
  240. ASTDumper P(llvm::errs(), /*ShowColors=*/false);
  241. P.Visit(this);
  242. }
  243. LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS,
  244. const ASTContext &Context) const {
  245. ASTDumper P(OS, Context, Context.getDiagnostics().getShowColors());
  246. P.Visit(this);
  247. }
  248. LLVM_DUMP_METHOD void Stmt::dumpColor() const {
  249. ASTDumper P(llvm::errs(), /*ShowColors=*/true);
  250. P.Visit(this);
  251. }
  252. //===----------------------------------------------------------------------===//
  253. // Comment method implementations
  254. //===----------------------------------------------------------------------===//
  255. LLVM_DUMP_METHOD void Comment::dump() const {
  256. const auto *FC = dyn_cast<FullComment>(this);
  257. if (!FC)
  258. return;
  259. ASTDumper Dumper(llvm::errs(), /*ShowColors=*/false);
  260. Dumper.Visit(FC, FC);
  261. }
  262. LLVM_DUMP_METHOD void Comment::dump(raw_ostream &OS,
  263. const ASTContext &Context) const {
  264. const auto *FC = dyn_cast<FullComment>(this);
  265. if (!FC)
  266. return;
  267. ASTDumper Dumper(OS, Context, Context.getDiagnostics().getShowColors());
  268. Dumper.Visit(FC, FC);
  269. }
  270. LLVM_DUMP_METHOD void Comment::dumpColor() const {
  271. const auto *FC = dyn_cast<FullComment>(this);
  272. if (!FC)
  273. return;
  274. ASTDumper Dumper(llvm::errs(), /*ShowColors=*/true);
  275. Dumper.Visit(FC, FC);
  276. }
  277. //===----------------------------------------------------------------------===//
  278. // APValue method implementations
  279. //===----------------------------------------------------------------------===//
  280. LLVM_DUMP_METHOD void APValue::dump() const {
  281. ASTDumper Dumper(llvm::errs(), /*ShowColors=*/false);
  282. Dumper.Visit(*this, /*Ty=*/QualType());
  283. }
  284. LLVM_DUMP_METHOD void APValue::dump(raw_ostream &OS,
  285. const ASTContext &Context) const {
  286. ASTDumper Dumper(llvm::errs(), Context,
  287. Context.getDiagnostics().getShowColors());
  288. Dumper.Visit(*this, /*Ty=*/Context.getPointerType(Context.CharTy));
  289. }