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

source code of clang/include/clang/AST/TextNodeDumper.h