TextNodeDumper.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- TextNodeDumper.h - Printing of AST nodes -------------------------===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file implements AST dumping of components of individual AST nodes.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_TEXTNODEDUMPER_H
  18. #define LLVM_CLANG_AST_TEXTNODEDUMPER_H
  19. #include "clang/AST/ASTContext.h"
  20. #include "clang/AST/ASTDumperUtils.h"
  21. #include "clang/AST/AttrVisitor.h"
  22. #include "clang/AST/CommentCommandTraits.h"
  23. #include "clang/AST/CommentVisitor.h"
  24. #include "clang/AST/DeclVisitor.h"
  25. #include "clang/AST/ExprConcepts.h"
  26. #include "clang/AST/ExprCXX.h"
  27. #include "clang/AST/StmtVisitor.h"
  28. #include "clang/AST/TemplateArgumentVisitor.h"
  29. #include "clang/AST/Type.h"
  30. #include "clang/AST/TypeVisitor.h"
  31. namespace clang {
  32. class APValue;
  33. class TextTreeStructure {
  34. raw_ostream &OS;
  35. const bool ShowColors;
  36. /// Pending[i] is an action to dump an entity at level i.
  37. llvm::SmallVector<std::function<void(bool IsLastChild)>, 32> Pending;
  38. /// Indicates whether we're at the top level.
  39. bool TopLevel = true;
  40. /// Indicates if we're handling the first child after entering a new depth.
  41. bool FirstChild = true;
  42. /// Prefix for currently-being-dumped entity.
  43. std::string Prefix;
  44. public:
  45. /// Add a child of the current node. Calls DoAddChild without arguments
  46. template <typename Fn> void AddChild(Fn DoAddChild) {
  47. return AddChild("", DoAddChild);
  48. }
  49. /// Add a child of the current node with an optional label.
  50. /// Calls DoAddChild without arguments.
  51. template <typename Fn> void AddChild(StringRef Label, Fn DoAddChild) {
  52. // If we're at the top level, there's nothing interesting to do; just
  53. // run the dumper.
  54. if (TopLevel) {
  55. TopLevel = false;
  56. DoAddChild();
  57. while (!Pending.empty()) {
  58. Pending.back()(true);
  59. Pending.pop_back();
  60. }
  61. Prefix.clear();
  62. OS << "\n";
  63. TopLevel = true;
  64. return;
  65. }
  66. auto DumpWithIndent = [this, DoAddChild,
  67. Label(Label.str())](bool IsLastChild) {
  68. // Print out the appropriate tree structure and work out the prefix for
  69. // children of this node. For instance:
  70. //
  71. // A Prefix = ""
  72. // |-B Prefix = "| "
  73. // | `-C Prefix = "| "
  74. // `-D Prefix = " "
  75. // |-E Prefix = " | "
  76. // `-F Prefix = " "
  77. // G Prefix = ""
  78. //
  79. // Note that the first level gets no prefix.
  80. {
  81. OS << '\n';
  82. ColorScope Color(OS, ShowColors, IndentColor);
  83. OS << Prefix << (IsLastChild ? '`' : '|') << '-';
  84. if (!Label.empty())
  85. OS << Label << ": ";
  86. this->Prefix.push_back(IsLastChild ? ' ' : '|');
  87. this->Prefix.push_back(' ');
  88. }
  89. FirstChild = true;
  90. unsigned Depth = Pending.size();
  91. DoAddChild();
  92. // If any children are left, they're the last at their nesting level.
  93. // Dump those ones out now.
  94. while (Depth < Pending.size()) {
  95. Pending.back()(true);
  96. this->Pending.pop_back();
  97. }
  98. // Restore the old prefix.
  99. this->Prefix.resize(Prefix.size() - 2);
  100. };
  101. if (FirstChild) {
  102. Pending.push_back(std::move(DumpWithIndent));
  103. } else {
  104. Pending.back()(false);
  105. Pending.back() = std::move(DumpWithIndent);
  106. }
  107. FirstChild = false;
  108. }
  109. TextTreeStructure(raw_ostream &OS, bool ShowColors)
  110. : OS(OS), ShowColors(ShowColors) {}
  111. };
  112. class TextNodeDumper
  113. : public TextTreeStructure,
  114. public comments::ConstCommentVisitor<TextNodeDumper, void,
  115. const comments::FullComment *>,
  116. public ConstAttrVisitor<TextNodeDumper>,
  117. public ConstTemplateArgumentVisitor<TextNodeDumper>,
  118. public ConstStmtVisitor<TextNodeDumper>,
  119. public TypeVisitor<TextNodeDumper>,
  120. public ConstDeclVisitor<TextNodeDumper> {
  121. raw_ostream &OS;
  122. const bool ShowColors;
  123. /// Keep track of the last location we print out so that we can
  124. /// print out deltas from then on out.
  125. const char *LastLocFilename = "";
  126. unsigned LastLocLine = ~0U;
  127. /// \p Context, \p SM, and \p Traits can be null. This is because we want
  128. /// to be able to call \p dump() in a debugger without having to pass the
  129. /// \p ASTContext to \p dump. Not all parts of the AST dump output will be
  130. /// available without the \p ASTContext.
  131. const ASTContext *Context = nullptr;
  132. const SourceManager *SM = nullptr;
  133. /// The policy to use for printing; can be defaulted.
  134. PrintingPolicy PrintPolicy = LangOptions();
  135. const comments::CommandTraits *Traits = nullptr;
  136. const char *getCommandName(unsigned CommandID);
  137. void printFPOptions(FPOptionsOverride FPO);
  138. void dumpAPValueChildren(const APValue &Value, QualType Ty,
  139. const APValue &(*IdxToChildFun)(const APValue &,
  140. unsigned),
  141. unsigned NumChildren, StringRef LabelSingular,
  142. StringRef LabelPlurial);
  143. public:
  144. TextNodeDumper(raw_ostream &OS, const ASTContext &Context, bool ShowColors);
  145. TextNodeDumper(raw_ostream &OS, bool ShowColors);
  146. void Visit(const comments::Comment *C, const comments::FullComment *FC);
  147. void Visit(const Attr *A);
  148. void Visit(const TemplateArgument &TA, SourceRange R,
  149. const Decl *From = nullptr, StringRef Label = {});
  150. void Visit(const Stmt *Node);
  151. void Visit(const Type *T);
  152. void Visit(QualType T);
  153. void Visit(const Decl *D);
  154. void Visit(const CXXCtorInitializer *Init);
  155. void Visit(const OMPClause *C);
  156. void Visit(const BlockDecl::Capture &C);
  157. void Visit(const GenericSelectionExpr::ConstAssociation &A);
  158. void Visit(const concepts::Requirement *R);
  159. void Visit(const APValue &Value, QualType Ty);
  160. void dumpPointer(const void *Ptr);
  161. void dumpLocation(SourceLocation Loc);
  162. void dumpSourceRange(SourceRange R);
  163. void dumpBareType(QualType T, bool Desugar = true);
  164. void dumpType(QualType T);
  165. void dumpBareDeclRef(const Decl *D);
  166. void dumpName(const NamedDecl *ND);
  167. void dumpAccessSpecifier(AccessSpecifier AS);
  168. void dumpCleanupObject(const ExprWithCleanups::CleanupObject &C);
  169. void dumpDeclRef(const Decl *D, StringRef Label = {});
  170. void visitTextComment(const comments::TextComment *C,
  171. const comments::FullComment *);
  172. void visitInlineCommandComment(const comments::InlineCommandComment *C,
  173. const comments::FullComment *);
  174. void visitHTMLStartTagComment(const comments::HTMLStartTagComment *C,
  175. const comments::FullComment *);
  176. void visitHTMLEndTagComment(const comments::HTMLEndTagComment *C,
  177. const comments::FullComment *);
  178. void visitBlockCommandComment(const comments::BlockCommandComment *C,
  179. const comments::FullComment *);
  180. void visitParamCommandComment(const comments::ParamCommandComment *C,
  181. const comments::FullComment *FC);
  182. void visitTParamCommandComment(const comments::TParamCommandComment *C,
  183. const comments::FullComment *FC);
  184. void visitVerbatimBlockComment(const comments::VerbatimBlockComment *C,
  185. const comments::FullComment *);
  186. void
  187. visitVerbatimBlockLineComment(const comments::VerbatimBlockLineComment *C,
  188. const comments::FullComment *);
  189. void visitVerbatimLineComment(const comments::VerbatimLineComment *C,
  190. const comments::FullComment *);
  191. // Implements Visit methods for Attrs.
  192. #include "clang/AST/AttrTextNodeDump.inc"
  193. void VisitNullTemplateArgument(const TemplateArgument &TA);
  194. void VisitTypeTemplateArgument(const TemplateArgument &TA);
  195. void VisitDeclarationTemplateArgument(const TemplateArgument &TA);
  196. void VisitNullPtrTemplateArgument(const TemplateArgument &TA);
  197. void VisitIntegralTemplateArgument(const TemplateArgument &TA);
  198. void VisitTemplateTemplateArgument(const TemplateArgument &TA);
  199. void VisitTemplateExpansionTemplateArgument(const TemplateArgument &TA);
  200. void VisitExpressionTemplateArgument(const TemplateArgument &TA);
  201. void VisitPackTemplateArgument(const TemplateArgument &TA);
  202. void VisitIfStmt(const IfStmt *Node);
  203. void VisitSwitchStmt(const SwitchStmt *Node);
  204. void VisitWhileStmt(const WhileStmt *Node);
  205. void VisitLabelStmt(const LabelStmt *Node);
  206. void VisitGotoStmt(const GotoStmt *Node);
  207. void VisitCaseStmt(const CaseStmt *Node);
  208. void VisitCompoundStmt(const CompoundStmt *Node);
  209. void VisitConstantExpr(const ConstantExpr *Node);
  210. void VisitCallExpr(const CallExpr *Node);
  211. void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Node);
  212. void VisitCastExpr(const CastExpr *Node);
  213. void VisitImplicitCastExpr(const ImplicitCastExpr *Node);
  214. void VisitDeclRefExpr(const DeclRefExpr *Node);
  215. void VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *Node);
  216. void VisitPredefinedExpr(const PredefinedExpr *Node);
  217. void VisitCharacterLiteral(const CharacterLiteral *Node);
  218. void VisitIntegerLiteral(const IntegerLiteral *Node);
  219. void VisitFixedPointLiteral(const FixedPointLiteral *Node);
  220. void VisitFloatingLiteral(const FloatingLiteral *Node);
  221. void VisitStringLiteral(const StringLiteral *Str);
  222. void VisitInitListExpr(const InitListExpr *ILE);
  223. void VisitGenericSelectionExpr(const GenericSelectionExpr *E);
  224. void VisitUnaryOperator(const UnaryOperator *Node);
  225. void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
  226. void VisitMemberExpr(const MemberExpr *Node);
  227. void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
  228. void VisitBinaryOperator(const BinaryOperator *Node);
  229. void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
  230. void VisitAddrLabelExpr(const AddrLabelExpr *Node);
  231. void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
  232. void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
  233. void VisitCXXThisExpr(const CXXThisExpr *Node);
  234. void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
  235. void VisitCXXStaticCastExpr(const CXXStaticCastExpr *Node);
  236. void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *Node);
  237. void VisitCXXConstructExpr(const CXXConstructExpr *Node);
  238. void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
  239. void VisitCXXNewExpr(const CXXNewExpr *Node);
  240. void VisitCXXDeleteExpr(const CXXDeleteExpr *Node);
  241. void VisitTypeTraitExpr(const TypeTraitExpr *Node);
  242. void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *Node);
  243. void VisitExpressionTraitExpr(const ExpressionTraitExpr *Node);
  244. void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
  245. void VisitExprWithCleanups(const ExprWithCleanups *Node);
  246. void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
  247. void VisitSizeOfPackExpr(const SizeOfPackExpr *Node);
  248. void
  249. VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *Node);
  250. void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
  251. void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
  252. void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
  253. void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
  254. void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
  255. void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
  256. void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
  257. void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
  258. void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
  259. void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
  260. void VisitOMPIteratorExpr(const OMPIteratorExpr *Node);
  261. void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *Node);
  262. void VisitRequiresExpr(const RequiresExpr *Node);
  263. void VisitRValueReferenceType(const ReferenceType *T);
  264. void VisitArrayType(const ArrayType *T);
  265. void VisitConstantArrayType(const ConstantArrayType *T);
  266. void VisitVariableArrayType(const VariableArrayType *T);
  267. void VisitDependentSizedArrayType(const DependentSizedArrayType *T);
  268. void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T);
  269. void VisitVectorType(const VectorType *T);
  270. void VisitFunctionType(const FunctionType *T);
  271. void VisitFunctionProtoType(const FunctionProtoType *T);
  272. void VisitUnresolvedUsingType(const UnresolvedUsingType *T);
  273. void VisitUsingType(const UsingType *T);
  274. void VisitTypedefType(const TypedefType *T);
  275. void VisitUnaryTransformType(const UnaryTransformType *T);
  276. void VisitTagType(const TagType *T);
  277. void VisitTemplateTypeParmType(const TemplateTypeParmType *T);
  278. void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
  279. void
  280. VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T);
  281. void VisitAutoType(const AutoType *T);
  282. void VisitDeducedTemplateSpecializationType(
  283. const DeducedTemplateSpecializationType *T);
  284. void VisitTemplateSpecializationType(const TemplateSpecializationType *T);
  285. void VisitInjectedClassNameType(const InjectedClassNameType *T);
  286. void VisitObjCInterfaceType(const ObjCInterfaceType *T);
  287. void VisitPackExpansionType(const PackExpansionType *T);
  288. void VisitLabelDecl(const LabelDecl *D);
  289. void VisitTypedefDecl(const TypedefDecl *D);
  290. void VisitEnumDecl(const EnumDecl *D);
  291. void VisitRecordDecl(const RecordDecl *D);
  292. void VisitEnumConstantDecl(const EnumConstantDecl *D);
  293. void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
  294. void VisitFunctionDecl(const FunctionDecl *D);
  295. void VisitFieldDecl(const FieldDecl *D);
  296. void VisitVarDecl(const VarDecl *D);
  297. void VisitBindingDecl(const BindingDecl *D);
  298. void VisitCapturedDecl(const CapturedDecl *D);
  299. void VisitImportDecl(const ImportDecl *D);
  300. void VisitPragmaCommentDecl(const PragmaCommentDecl *D);
  301. void VisitPragmaDetectMismatchDecl(const PragmaDetectMismatchDecl *D);
  302. void VisitOMPExecutableDirective(const OMPExecutableDirective *D);
  303. void VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D);
  304. void VisitOMPRequiresDecl(const OMPRequiresDecl *D);
  305. void VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D);
  306. void VisitNamespaceDecl(const NamespaceDecl *D);
  307. void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
  308. void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
  309. void VisitTypeAliasDecl(const TypeAliasDecl *D);
  310. void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
  311. void VisitCXXRecordDecl(const CXXRecordDecl *D);
  312. void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
  313. void VisitClassTemplateDecl(const ClassTemplateDecl *D);
  314. void VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D);
  315. void VisitVarTemplateDecl(const VarTemplateDecl *D);
  316. void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
  317. void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
  318. void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
  319. void VisitUsingDecl(const UsingDecl *D);
  320. void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
  321. void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
  322. void VisitUsingEnumDecl(const UsingEnumDecl *D);
  323. void VisitUsingShadowDecl(const UsingShadowDecl *D);
  324. void VisitConstructorUsingShadowDecl(const ConstructorUsingShadowDecl *D);
  325. void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
  326. void VisitAccessSpecDecl(const AccessSpecDecl *D);
  327. void VisitFriendDecl(const FriendDecl *D);
  328. void VisitObjCIvarDecl(const ObjCIvarDecl *D);
  329. void VisitObjCMethodDecl(const ObjCMethodDecl *D);
  330. void VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D);
  331. void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
  332. void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
  333. void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
  334. void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
  335. void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
  336. void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
  337. void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
  338. void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
  339. void VisitBlockDecl(const BlockDecl *D);
  340. void VisitConceptDecl(const ConceptDecl *D);
  341. void
  342. VisitLifetimeExtendedTemporaryDecl(const LifetimeExtendedTemporaryDecl *D);
  343. void VisitHLSLBufferDecl(const HLSLBufferDecl *D);
  344. };
  345. } // namespace clang
  346. #endif // LLVM_CLANG_AST_TEXTNODEDUMPER_H
  347. #ifdef __GNUC__
  348. #pragma GCC diagnostic pop
  349. #endif