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 BlockDecl::Capture &C);
193
194 void Visit(const GenericSelectionExpr::ConstAssociation &A);
195
196 void Visit(const ConceptReference *);
197
198 void Visit(const concepts::Requirement *R);
199
200 void Visit(const APValue &Value, QualType Ty);
201
202 void dumpPointer(const void *Ptr);
203 void dumpLocation(SourceLocation Loc);
204 void dumpSourceRange(SourceRange R);
205 void dumpBareType(QualType T, bool Desugar = true);
206 void dumpType(QualType T);
207 void dumpBareDeclRef(const Decl *D);
208 void dumpName(const NamedDecl *ND);
209 void dumpAccessSpecifier(AccessSpecifier AS);
210 void dumpCleanupObject(const ExprWithCleanups::CleanupObject &C);
211 void dumpTemplateSpecializationKind(TemplateSpecializationKind TSK);
212 void dumpNestedNameSpecifier(const NestedNameSpecifier *NNS);
213 void dumpConceptReference(const ConceptReference *R);
214
215 void dumpDeclRef(const Decl *D, StringRef Label = {});
216
217 void visitTextComment(const comments::TextComment *C,
218 const comments::FullComment *);
219 void visitInlineCommandComment(const comments::InlineCommandComment *C,
220 const comments::FullComment *);
221 void visitHTMLStartTagComment(const comments::HTMLStartTagComment *C,
222 const comments::FullComment *);
223 void visitHTMLEndTagComment(const comments::HTMLEndTagComment *C,
224 const comments::FullComment *);
225 void visitBlockCommandComment(const comments::BlockCommandComment *C,
226 const comments::FullComment *);
227 void visitParamCommandComment(const comments::ParamCommandComment *C,
228 const comments::FullComment *FC);
229 void visitTParamCommandComment(const comments::TParamCommandComment *C,
230 const comments::FullComment *FC);
231 void visitVerbatimBlockComment(const comments::VerbatimBlockComment *C,
232 const comments::FullComment *);
233 void
234 visitVerbatimBlockLineComment(const comments::VerbatimBlockLineComment *C,
235 const comments::FullComment *);
236 void visitVerbatimLineComment(const comments::VerbatimLineComment *C,
237 const comments::FullComment *);
238
239// Implements Visit methods for Attrs.
240#include "clang/AST/AttrTextNodeDump.inc"
241
242 void VisitNullTemplateArgument(const TemplateArgument &TA);
243 void VisitTypeTemplateArgument(const TemplateArgument &TA);
244 void VisitDeclarationTemplateArgument(const TemplateArgument &TA);
245 void VisitNullPtrTemplateArgument(const TemplateArgument &TA);
246 void VisitIntegralTemplateArgument(const TemplateArgument &TA);
247 void VisitTemplateTemplateArgument(const TemplateArgument &TA);
248 void VisitTemplateExpansionTemplateArgument(const TemplateArgument &TA);
249 void VisitExpressionTemplateArgument(const TemplateArgument &TA);
250 void VisitPackTemplateArgument(const TemplateArgument &TA);
251
252 void VisitIfStmt(const IfStmt *Node);
253 void VisitSwitchStmt(const SwitchStmt *Node);
254 void VisitWhileStmt(const WhileStmt *Node);
255 void VisitLabelStmt(const LabelStmt *Node);
256 void VisitGotoStmt(const GotoStmt *Node);
257 void VisitCaseStmt(const CaseStmt *Node);
258 void VisitReturnStmt(const ReturnStmt *Node);
259 void VisitCoawaitExpr(const CoawaitExpr *Node);
260 void VisitCoreturnStmt(const CoreturnStmt *Node);
261 void VisitCompoundStmt(const CompoundStmt *Node);
262 void VisitConstantExpr(const ConstantExpr *Node);
263 void VisitCallExpr(const CallExpr *Node);
264 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Node);
265 void VisitCastExpr(const CastExpr *Node);
266 void VisitImplicitCastExpr(const ImplicitCastExpr *Node);
267 void VisitDeclRefExpr(const DeclRefExpr *Node);
268 void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *Node);
269 void VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *Node);
270 void VisitPredefinedExpr(const PredefinedExpr *Node);
271 void VisitCharacterLiteral(const CharacterLiteral *Node);
272 void VisitIntegerLiteral(const IntegerLiteral *Node);
273 void VisitFixedPointLiteral(const FixedPointLiteral *Node);
274 void VisitFloatingLiteral(const FloatingLiteral *Node);
275 void VisitStringLiteral(const StringLiteral *Str);
276 void VisitInitListExpr(const InitListExpr *ILE);
277 void VisitGenericSelectionExpr(const GenericSelectionExpr *E);
278 void VisitUnaryOperator(const UnaryOperator *Node);
279 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
280 void VisitMemberExpr(const MemberExpr *Node);
281 void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
282 void VisitBinaryOperator(const BinaryOperator *Node);
283 void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
284 void VisitAddrLabelExpr(const AddrLabelExpr *Node);
285 void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
286 void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
287 void VisitCXXThisExpr(const CXXThisExpr *Node);
288 void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
289 void VisitCXXStaticCastExpr(const CXXStaticCastExpr *Node);
290 void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *Node);
291 void VisitCXXConstructExpr(const CXXConstructExpr *Node);
292 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
293 void VisitCXXNewExpr(const CXXNewExpr *Node);
294 void VisitCXXDeleteExpr(const CXXDeleteExpr *Node);
295 void VisitTypeTraitExpr(const TypeTraitExpr *Node);
296 void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *Node);
297 void VisitExpressionTraitExpr(const ExpressionTraitExpr *Node);
298 void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *Node);
299 void VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *Node);
300 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
301 void VisitExprWithCleanups(const ExprWithCleanups *Node);
302 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
303 void VisitSizeOfPackExpr(const SizeOfPackExpr *Node);
304 void
305 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *Node);
306 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
307 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
308 void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
309 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
310 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
311 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
312 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
313 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
314 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
315 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
316 void VisitOMPIteratorExpr(const OMPIteratorExpr *Node);
317 void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *Node);
318 void VisitRequiresExpr(const RequiresExpr *Node);
319
320 void VisitRValueReferenceType(const ReferenceType *T);
321 void VisitArrayType(const ArrayType *T);
322 void VisitConstantArrayType(const ConstantArrayType *T);
323 void VisitVariableArrayType(const VariableArrayType *T);
324 void VisitDependentSizedArrayType(const DependentSizedArrayType *T);
325 void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T);
326 void VisitVectorType(const VectorType *T);
327 void VisitFunctionType(const FunctionType *T);
328 void VisitFunctionProtoType(const FunctionProtoType *T);
329 void VisitUnresolvedUsingType(const UnresolvedUsingType *T);
330 void VisitUsingType(const UsingType *T);
331 void VisitTypedefType(const TypedefType *T);
332 void VisitUnaryTransformType(const UnaryTransformType *T);
333 void VisitTagType(const TagType *T);
334 void VisitTemplateTypeParmType(const TemplateTypeParmType *T);
335 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
336 void
337 VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T);
338 void VisitAutoType(const AutoType *T);
339 void VisitDeducedTemplateSpecializationType(
340 const DeducedTemplateSpecializationType *T);
341 void VisitTemplateSpecializationType(const TemplateSpecializationType *T);
342 void VisitInjectedClassNameType(const InjectedClassNameType *T);
343 void VisitObjCInterfaceType(const ObjCInterfaceType *T);
344 void VisitPackExpansionType(const PackExpansionType *T);
345
346 void VisitTypeLoc(TypeLoc TL);
347
348 void VisitLabelDecl(const LabelDecl *D);
349 void VisitTypedefDecl(const TypedefDecl *D);
350 void VisitEnumDecl(const EnumDecl *D);
351 void VisitRecordDecl(const RecordDecl *D);
352 void VisitEnumConstantDecl(const EnumConstantDecl *D);
353 void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
354 void VisitFunctionDecl(const FunctionDecl *D);
355 void VisitFieldDecl(const FieldDecl *D);
356 void VisitVarDecl(const VarDecl *D);
357 void VisitBindingDecl(const BindingDecl *D);
358 void VisitCapturedDecl(const CapturedDecl *D);
359 void VisitImportDecl(const ImportDecl *D);
360 void VisitPragmaCommentDecl(const PragmaCommentDecl *D);
361 void VisitPragmaDetectMismatchDecl(const PragmaDetectMismatchDecl *D);
362 void VisitOMPExecutableDirective(const OMPExecutableDirective *D);
363 void VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D);
364 void VisitOMPRequiresDecl(const OMPRequiresDecl *D);
365 void VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D);
366 void VisitNamespaceDecl(const NamespaceDecl *D);
367 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
368 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
369 void VisitTypeAliasDecl(const TypeAliasDecl *D);
370 void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
371 void VisitCXXRecordDecl(const CXXRecordDecl *D);
372 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
373 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
374 void VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D);
375 void VisitVarTemplateDecl(const VarTemplateDecl *D);
376 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
377 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
378 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
379 void VisitUsingDecl(const UsingDecl *D);
380 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
381 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
382 void VisitUsingEnumDecl(const UsingEnumDecl *D);
383 void VisitUsingShadowDecl(const UsingShadowDecl *D);
384 void VisitConstructorUsingShadowDecl(const ConstructorUsingShadowDecl *D);
385 void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
386 void VisitAccessSpecDecl(const AccessSpecDecl *D);
387 void VisitFriendDecl(const FriendDecl *D);
388 void VisitObjCIvarDecl(const ObjCIvarDecl *D);
389 void VisitObjCMethodDecl(const ObjCMethodDecl *D);
390 void VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D);
391 void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
392 void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
393 void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
394 void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
395 void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
396 void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
397 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
398 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
399 void VisitBlockDecl(const BlockDecl *D);
400 void VisitConceptDecl(const ConceptDecl *D);
401 void
402 VisitLifetimeExtendedTemporaryDecl(const LifetimeExtendedTemporaryDecl *D);
403 void VisitHLSLBufferDecl(const HLSLBufferDecl *D);
404 void VisitOpenACCConstructStmt(const OpenACCConstructStmt *S);
405};
406
407} // namespace clang
408
409#endif // LLVM_CLANG_AST_TEXTNODEDUMPER_H
410

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