1 //===--- TextNodeDumper.h - Printing of AST nodes -------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements AST dumping of components of individual AST nodes.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_TEXTNODEDUMPER_H
14 #define LLVM_CLANG_AST_TEXTNODEDUMPER_H
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTDumperUtils.h"
18 #include "clang/AST/AttrVisitor.h"
19 #include "clang/AST/CommentCommandTraits.h"
20 #include "clang/AST/CommentVisitor.h"
21 #include "clang/AST/DeclVisitor.h"
22 #include "clang/AST/ExprCXX.h"
23 #include "clang/AST/StmtVisitor.h"
24 #include "clang/AST/TemplateArgumentVisitor.h"
25 #include "clang/AST/TypeVisitor.h"
29 class TextTreeStructure {
31 const bool ShowColors;
33 /// Pending[i] is an action to dump an entity at level i.
34 llvm::SmallVector<std::function<void(bool IsLastChild)>, 32> Pending;
36 /// Indicates whether we're at the top level.
39 /// Indicates if we're handling the first child after entering a new depth.
40 bool FirstChild = true;
42 /// Prefix for currently-being-dumped entity.
46 /// Add a child of the current node. Calls DoAddChild without arguments
47 template <typename Fn> void AddChild(Fn DoAddChild) {
48 return AddChild("", DoAddChild);
51 /// Add a child of the current node with an optional label.
52 /// Calls DoAddChild without arguments.
53 template <typename Fn> void AddChild(StringRef Label, Fn DoAddChild) {
54 // If we're at the top level, there's nothing interesting to do; just
59 while (!Pending.empty()) {
69 // We need to capture an owning-string in the lambda because the lambda
70 // is invoked in a deferred manner.
71 std::string LabelStr = Label;
72 auto DumpWithIndent = [this, DoAddChild, LabelStr](bool IsLastChild) {
73 // Print out the appropriate tree structure and work out the prefix for
74 // children of this node. For instance:
78 // | `-C Prefix = "| "
84 // Note that the first level gets no prefix.
87 ColorScope Color(OS, ShowColors, IndentColor);
88 OS << Prefix << (IsLastChild ? '`' : '|') << '-';
89 if (!LabelStr.empty())
90 OS << LabelStr << ": ";
92 this->Prefix.push_back(IsLastChild ? ' ' : '|');
93 this->Prefix.push_back(' ');
97 unsigned Depth = Pending.size();
101 // If any children are left, they're the last at their nesting level.
102 // Dump those ones out now.
103 while (Depth < Pending.size()) {
104 Pending.back()(true);
105 this->Pending.pop_back();
108 // Restore the old prefix.
109 this->Prefix.resize(Prefix.size() - 2);
113 Pending.push_back(std::move(DumpWithIndent));
115 Pending.back()(false);
116 Pending.back() = std::move(DumpWithIndent);
121 TextTreeStructure(raw_ostream &OS, bool ShowColors)
122 : OS(OS), ShowColors(ShowColors) {}
126 : public TextTreeStructure,
127 public comments::ConstCommentVisitor<TextNodeDumper, void,
128 const comments::FullComment *>,
129 public ConstAttrVisitor<TextNodeDumper>,
130 public ConstTemplateArgumentVisitor<TextNodeDumper>,
131 public ConstStmtVisitor<TextNodeDumper>,
132 public TypeVisitor<TextNodeDumper>,
133 public ConstDeclVisitor<TextNodeDumper> {
135 const bool ShowColors;
137 /// Keep track of the last location we print out so that we can
138 /// print out deltas from then on out.
139 const char *LastLocFilename = "";
140 unsigned LastLocLine = ~0U;
142 const SourceManager *SM;
144 /// The policy to use for printing; can be defaulted.
145 PrintingPolicy PrintPolicy;
147 const comments::CommandTraits *Traits;
149 const ASTContext *Context;
151 const char *getCommandName(unsigned CommandID);
154 TextNodeDumper(raw_ostream &OS, bool ShowColors, const SourceManager *SM,
155 const PrintingPolicy &PrintPolicy,
156 const comments::CommandTraits *Traits);
158 void Visit(const comments::Comment *C, const comments::FullComment *FC);
160 void Visit(const Attr *A);
162 void Visit(const TemplateArgument &TA, SourceRange R,
163 const Decl *From = nullptr, StringRef Label = {});
165 void Visit(const Stmt *Node);
167 void Visit(const Type *T);
169 void Visit(QualType T);
171 void Visit(const Decl *D);
173 void Visit(const CXXCtorInitializer *Init);
175 void Visit(const OMPClause *C);
177 void Visit(const BlockDecl::Capture &C);
179 void Visit(const GenericSelectionExpr::ConstAssociation &A);
181 void dumpPointer(const void *Ptr);
182 void dumpLocation(SourceLocation Loc);
183 void dumpSourceRange(SourceRange R);
184 void dumpBareType(QualType T, bool Desugar = true);
185 void dumpType(QualType T);
186 void dumpBareDeclRef(const Decl *D);
187 void dumpName(const NamedDecl *ND);
188 void dumpAccessSpecifier(AccessSpecifier AS);
190 void dumpDeclRef(const Decl *D, StringRef Label = {});
192 void visitTextComment(const comments::TextComment *C,
193 const comments::FullComment *);
194 void visitInlineCommandComment(const comments::InlineCommandComment *C,
195 const comments::FullComment *);
196 void visitHTMLStartTagComment(const comments::HTMLStartTagComment *C,
197 const comments::FullComment *);
198 void visitHTMLEndTagComment(const comments::HTMLEndTagComment *C,
199 const comments::FullComment *);
200 void visitBlockCommandComment(const comments::BlockCommandComment *C,
201 const comments::FullComment *);
202 void visitParamCommandComment(const comments::ParamCommandComment *C,
203 const comments::FullComment *FC);
204 void visitTParamCommandComment(const comments::TParamCommandComment *C,
205 const comments::FullComment *FC);
206 void visitVerbatimBlockComment(const comments::VerbatimBlockComment *C,
207 const comments::FullComment *);
209 visitVerbatimBlockLineComment(const comments::VerbatimBlockLineComment *C,
210 const comments::FullComment *);
211 void visitVerbatimLineComment(const comments::VerbatimLineComment *C,
212 const comments::FullComment *);
214 // Implements Visit methods for Attrs.
215 #include "clang/AST/AttrTextNodeDump.inc"
217 void VisitNullTemplateArgument(const TemplateArgument &TA);
218 void VisitTypeTemplateArgument(const TemplateArgument &TA);
219 void VisitDeclarationTemplateArgument(const TemplateArgument &TA);
220 void VisitNullPtrTemplateArgument(const TemplateArgument &TA);
221 void VisitIntegralTemplateArgument(const TemplateArgument &TA);
222 void VisitTemplateTemplateArgument(const TemplateArgument &TA);
223 void VisitTemplateExpansionTemplateArgument(const TemplateArgument &TA);
224 void VisitExpressionTemplateArgument(const TemplateArgument &TA);
225 void VisitPackTemplateArgument(const TemplateArgument &TA);
227 void VisitIfStmt(const IfStmt *Node);
228 void VisitSwitchStmt(const SwitchStmt *Node);
229 void VisitWhileStmt(const WhileStmt *Node);
230 void VisitLabelStmt(const LabelStmt *Node);
231 void VisitGotoStmt(const GotoStmt *Node);
232 void VisitCaseStmt(const CaseStmt *Node);
233 void VisitConstantExpr(const ConstantExpr *Node);
234 void VisitCallExpr(const CallExpr *Node);
235 void VisitCastExpr(const CastExpr *Node);
236 void VisitImplicitCastExpr(const ImplicitCastExpr *Node);
237 void VisitDeclRefExpr(const DeclRefExpr *Node);
238 void VisitPredefinedExpr(const PredefinedExpr *Node);
239 void VisitCharacterLiteral(const CharacterLiteral *Node);
240 void VisitIntegerLiteral(const IntegerLiteral *Node);
241 void VisitFixedPointLiteral(const FixedPointLiteral *Node);
242 void VisitFloatingLiteral(const FloatingLiteral *Node);
243 void VisitStringLiteral(const StringLiteral *Str);
244 void VisitInitListExpr(const InitListExpr *ILE);
245 void VisitGenericSelectionExpr(const GenericSelectionExpr *E);
246 void VisitUnaryOperator(const UnaryOperator *Node);
247 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
248 void VisitMemberExpr(const MemberExpr *Node);
249 void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
250 void VisitBinaryOperator(const BinaryOperator *Node);
251 void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
252 void VisitAddrLabelExpr(const AddrLabelExpr *Node);
253 void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
254 void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
255 void VisitCXXThisExpr(const CXXThisExpr *Node);
256 void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
257 void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *Node);
258 void VisitCXXConstructExpr(const CXXConstructExpr *Node);
259 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
260 void VisitCXXNewExpr(const CXXNewExpr *Node);
261 void VisitCXXDeleteExpr(const CXXDeleteExpr *Node);
262 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
263 void VisitExprWithCleanups(const ExprWithCleanups *Node);
264 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
265 void VisitSizeOfPackExpr(const SizeOfPackExpr *Node);
267 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *Node);
268 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
269 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
270 void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
271 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
272 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
273 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
274 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
275 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
276 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
277 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
279 void VisitRValueReferenceType(const ReferenceType *T);
280 void VisitArrayType(const ArrayType *T);
281 void VisitConstantArrayType(const ConstantArrayType *T);
282 void VisitVariableArrayType(const VariableArrayType *T);
283 void VisitDependentSizedArrayType(const DependentSizedArrayType *T);
284 void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T);
285 void VisitVectorType(const VectorType *T);
286 void VisitFunctionType(const FunctionType *T);
287 void VisitFunctionProtoType(const FunctionProtoType *T);
288 void VisitUnresolvedUsingType(const UnresolvedUsingType *T);
289 void VisitTypedefType(const TypedefType *T);
290 void VisitUnaryTransformType(const UnaryTransformType *T);
291 void VisitTagType(const TagType *T);
292 void VisitTemplateTypeParmType(const TemplateTypeParmType *T);
293 void VisitAutoType(const AutoType *T);
294 void VisitTemplateSpecializationType(const TemplateSpecializationType *T);
295 void VisitInjectedClassNameType(const InjectedClassNameType *T);
296 void VisitObjCInterfaceType(const ObjCInterfaceType *T);
297 void VisitPackExpansionType(const PackExpansionType *T);
299 void VisitLabelDecl(const LabelDecl *D);
300 void VisitTypedefDecl(const TypedefDecl *D);
301 void VisitEnumDecl(const EnumDecl *D);
302 void VisitRecordDecl(const RecordDecl *D);
303 void VisitEnumConstantDecl(const EnumConstantDecl *D);
304 void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
305 void VisitFunctionDecl(const FunctionDecl *D);
306 void VisitFieldDecl(const FieldDecl *D);
307 void VisitVarDecl(const VarDecl *D);
308 void VisitBindingDecl(const BindingDecl *D);
309 void VisitCapturedDecl(const CapturedDecl *D);
310 void VisitImportDecl(const ImportDecl *D);
311 void VisitPragmaCommentDecl(const PragmaCommentDecl *D);
312 void VisitPragmaDetectMismatchDecl(const PragmaDetectMismatchDecl *D);
313 void VisitOMPExecutableDirective(const OMPExecutableDirective *D);
314 void VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D);
315 void VisitOMPRequiresDecl(const OMPRequiresDecl *D);
316 void VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D);
317 void VisitNamespaceDecl(const NamespaceDecl *D);
318 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
319 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
320 void VisitTypeAliasDecl(const TypeAliasDecl *D);
321 void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
322 void VisitCXXRecordDecl(const CXXRecordDecl *D);
323 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
324 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
325 void VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D);
326 void VisitVarTemplateDecl(const VarTemplateDecl *D);
327 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
328 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
329 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
330 void VisitUsingDecl(const UsingDecl *D);
331 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
332 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
333 void VisitUsingShadowDecl(const UsingShadowDecl *D);
334 void VisitConstructorUsingShadowDecl(const ConstructorUsingShadowDecl *D);
335 void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
336 void VisitAccessSpecDecl(const AccessSpecDecl *D);
337 void VisitFriendDecl(const FriendDecl *D);
338 void VisitObjCIvarDecl(const ObjCIvarDecl *D);
339 void VisitObjCMethodDecl(const ObjCMethodDecl *D);
340 void VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D);
341 void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
342 void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
343 void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
344 void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
345 void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
346 void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
347 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
348 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
349 void VisitBlockDecl(const BlockDecl *D);
350 void VisitConceptDecl(const ConceptDecl *D);
355 #endif // LLVM_CLANG_AST_TEXTNODEDUMPER_H