1 //===--- ASTDumper.cpp - Dumping implementation for ASTs ------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the AST dump methods, which dump out the
11 // AST in a form that exposes type details and other fields.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/CommentVisitor.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclLookups.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/AST/DeclOpenMP.h"
22 #include "clang/AST/DeclVisitor.h"
23 #include "clang/AST/LocInfoType.h"
24 #include "clang/AST/StmtVisitor.h"
25 #include "clang/AST/TypeVisitor.h"
26 #include "clang/Basic/Builtins.h"
27 #include "clang/Basic/Module.h"
28 #include "clang/Basic/SourceManager.h"
29 #include "llvm/Support/raw_ostream.h"
30 using namespace clang;
31 using namespace clang::comments;
32
33 //===----------------------------------------------------------------------===//
34 // ASTDumper Visitor
35 //===----------------------------------------------------------------------===//
36
37 namespace {
38 // Colors used for various parts of the AST dump
39 // Do not use bold yellow for any text. It is hard to read on white screens.
40
41 struct TerminalColor {
42 raw_ostream::Colors Color;
43 bool Bold;
44 };
45
46 // Red - CastColor
47 // Green - TypeColor
48 // Bold Green - DeclKindNameColor, UndeserializedColor
49 // Yellow - AddressColor, LocationColor
50 // Blue - CommentColor, NullColor, IndentColor
51 // Bold Blue - AttrColor
52 // Bold Magenta - StmtColor
53 // Cyan - ValueKindColor, ObjectKindColor
54 // Bold Cyan - ValueColor, DeclNameColor
55
56 // Decl kind names (VarDecl, FunctionDecl, etc)
57 static const TerminalColor DeclKindNameColor = { raw_ostream::GREEN, true };
58 // Attr names (CleanupAttr, GuardedByAttr, etc)
59 static const TerminalColor AttrColor = { raw_ostream::BLUE, true };
60 // Statement names (DeclStmt, ImplicitCastExpr, etc)
61 static const TerminalColor StmtColor = { raw_ostream::MAGENTA, true };
62 // Comment names (FullComment, ParagraphComment, TextComment, etc)
63 static const TerminalColor CommentColor = { raw_ostream::BLUE, false };
64
65 // Type names (int, float, etc, plus user defined types)
66 static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
67
68 // Pointer address
69 static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
70 // Source locations
71 static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
72
73 // lvalue/xvalue
74 static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
75 // bitfield/objcproperty/objcsubscript/vectorcomponent
76 static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
77
78 // Null statements
79 static const TerminalColor NullColor = { raw_ostream::BLUE, false };
80
81 // Undeserialized entities
82 static const TerminalColor UndeserializedColor = { raw_ostream::GREEN, true };
83
84 // CastKind from CastExpr's
85 static const TerminalColor CastColor = { raw_ostream::RED, false };
86
87 // Value of the statement
88 static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
89 // Decl names
90 static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
91
92 // Indents ( `, -. | )
93 static const TerminalColor IndentColor = { raw_ostream::BLUE, false };
94
95 class ASTDumper
96 : public ConstDeclVisitor<ASTDumper>, public ConstStmtVisitor<ASTDumper>,
97 public ConstCommentVisitor<ASTDumper>, public TypeVisitor<ASTDumper> {
98 raw_ostream &OS;
99 const CommandTraits *Traits;
100 const SourceManager *SM;
101
102 /// Pending[i] is an action to dump an entity at level i.
103 llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending;
104
105 /// Indicates whether we're at the top level.
106 bool TopLevel;
107
108 /// Indicates if we're handling the first child after entering a new depth.
109 bool FirstChild;
110
111 /// Prefix for currently-being-dumped entity.
112 std::string Prefix;
113
114 /// Keep track of the last location we print out so that we can
115 /// print out deltas from then on out.
116 const char *LastLocFilename;
117 unsigned LastLocLine;
118
119 /// The \c FullComment parent of the comment being dumped.
120 const FullComment *FC;
121
122 bool ShowColors;
123
124 /// Dump a child of the current node.
dumpChild(Fn doDumpChild)125 template<typename Fn> void dumpChild(Fn doDumpChild) {
126 // If we're at the top level, there's nothing interesting to do; just
127 // run the dumper.
128 if (TopLevel) {
129 TopLevel = false;
130 doDumpChild();
131 while (!Pending.empty()) {
132 Pending.back()(true);
133 Pending.pop_back();
134 }
135 Prefix.clear();
136 OS << "\n";
137 TopLevel = true;
138 return;
139 }
140
141 const FullComment *OrigFC = FC;
142 auto dumpWithIndent = [this, doDumpChild, OrigFC](bool isLastChild) {
143 // Print out the appropriate tree structure and work out the prefix for
144 // children of this node. For instance:
145 //
146 // A Prefix = ""
147 // |-B Prefix = "| "
148 // | `-C Prefix = "| "
149 // `-D Prefix = " "
150 // |-E Prefix = " | "
151 // `-F Prefix = " "
152 // G Prefix = ""
153 //
154 // Note that the first level gets no prefix.
155 {
156 OS << '\n';
157 ColorScope Color(*this, IndentColor);
158 OS << Prefix << (isLastChild ? '`' : '|') << '-';
159 this->Prefix.push_back(isLastChild ? ' ' : '|');
160 this->Prefix.push_back(' ');
161 }
162
163 FirstChild = true;
164 unsigned Depth = Pending.size();
165
166 FC = OrigFC;
167 doDumpChild();
168
169 // If any children are left, they're the last at their nesting level.
170 // Dump those ones out now.
171 while (Depth < Pending.size()) {
172 Pending.back()(true);
173 this->Pending.pop_back();
174 }
175
176 // Restore the old prefix.
177 this->Prefix.resize(Prefix.size() - 2);
178 };
179
180 if (FirstChild) {
181 Pending.push_back(std::move(dumpWithIndent));
182 } else {
183 Pending.back()(false);
184 Pending.back() = std::move(dumpWithIndent);
185 }
186 FirstChild = false;
187 }
188
189 class ColorScope {
190 ASTDumper &Dumper;
191 public:
ColorScope(ASTDumper & Dumper,TerminalColor Color)192 ColorScope(ASTDumper &Dumper, TerminalColor Color)
193 : Dumper(Dumper) {
194 if (Dumper.ShowColors)
195 Dumper.OS.changeColor(Color.Color, Color.Bold);
196 }
~ColorScope()197 ~ColorScope() {
198 if (Dumper.ShowColors)
199 Dumper.OS.resetColor();
200 }
201 };
202
203 public:
ASTDumper(raw_ostream & OS,const CommandTraits * Traits,const SourceManager * SM)204 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
205 const SourceManager *SM)
206 : OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
207 LastLocFilename(""), LastLocLine(~0U), FC(nullptr),
208 ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
209
ASTDumper(raw_ostream & OS,const CommandTraits * Traits,const SourceManager * SM,bool ShowColors)210 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
211 const SourceManager *SM, bool ShowColors)
212 : OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
213 LastLocFilename(""), LastLocLine(~0U),
214 ShowColors(ShowColors) { }
215
216 void dumpDecl(const Decl *D);
217 void dumpStmt(const Stmt *S);
218 void dumpFullComment(const FullComment *C);
219
220 // Utilities
221 void dumpPointer(const void *Ptr);
222 void dumpSourceRange(SourceRange R);
223 void dumpLocation(SourceLocation Loc);
224 void dumpBareType(QualType T, bool Desugar = true);
225 void dumpType(QualType T);
226 void dumpTypeAsChild(QualType T);
227 void dumpTypeAsChild(const Type *T);
228 void dumpBareDeclRef(const Decl *Node);
229 void dumpDeclRef(const Decl *Node, const char *Label = nullptr);
230 void dumpName(const NamedDecl *D);
231 bool hasNodes(const DeclContext *DC);
232 void dumpDeclContext(const DeclContext *DC);
233 void dumpLookups(const DeclContext *DC, bool DumpDecls);
234 void dumpAttr(const Attr *A);
235
236 // C++ Utilities
237 void dumpAccessSpecifier(AccessSpecifier AS);
238 void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
239 void dumpTemplateParameters(const TemplateParameterList *TPL);
240 void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
241 void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
242 void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
243 void dumpTemplateArgument(const TemplateArgument &A,
244 SourceRange R = SourceRange());
245
246 // Objective-C utilities.
247 void dumpObjCTypeParamList(const ObjCTypeParamList *typeParams);
248
249 // Types
VisitComplexType(const ComplexType * T)250 void VisitComplexType(const ComplexType *T) {
251 dumpTypeAsChild(T->getElementType());
252 }
VisitPointerType(const PointerType * T)253 void VisitPointerType(const PointerType *T) {
254 dumpTypeAsChild(T->getPointeeType());
255 }
VisitBlockPointerType(const BlockPointerType * T)256 void VisitBlockPointerType(const BlockPointerType *T) {
257 dumpTypeAsChild(T->getPointeeType());
258 }
VisitReferenceType(const ReferenceType * T)259 void VisitReferenceType(const ReferenceType *T) {
260 dumpTypeAsChild(T->getPointeeType());
261 }
VisitRValueReferenceType(const ReferenceType * T)262 void VisitRValueReferenceType(const ReferenceType *T) {
263 if (T->isSpelledAsLValue())
264 OS << " written as lvalue reference";
265 VisitReferenceType(T);
266 }
VisitMemberPointerType(const MemberPointerType * T)267 void VisitMemberPointerType(const MemberPointerType *T) {
268 dumpTypeAsChild(T->getClass());
269 dumpTypeAsChild(T->getPointeeType());
270 }
VisitArrayType(const ArrayType * T)271 void VisitArrayType(const ArrayType *T) {
272 switch (T->getSizeModifier()) {
273 case ArrayType::Normal: break;
274 case ArrayType::Static: OS << " static"; break;
275 case ArrayType::Star: OS << " *"; break;
276 }
277 OS << " " << T->getIndexTypeQualifiers().getAsString();
278 dumpTypeAsChild(T->getElementType());
279 }
VisitConstantArrayType(const ConstantArrayType * T)280 void VisitConstantArrayType(const ConstantArrayType *T) {
281 OS << " " << T->getSize();
282 VisitArrayType(T);
283 }
VisitVariableArrayType(const VariableArrayType * T)284 void VisitVariableArrayType(const VariableArrayType *T) {
285 OS << " ";
286 dumpSourceRange(T->getBracketsRange());
287 VisitArrayType(T);
288 dumpStmt(T->getSizeExpr());
289 }
VisitDependentSizedArrayType(const DependentSizedArrayType * T)290 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
291 VisitArrayType(T);
292 OS << " ";
293 dumpSourceRange(T->getBracketsRange());
294 dumpStmt(T->getSizeExpr());
295 }
VisitDependentSizedExtVectorType(const DependentSizedExtVectorType * T)296 void VisitDependentSizedExtVectorType(
297 const DependentSizedExtVectorType *T) {
298 OS << " ";
299 dumpLocation(T->getAttributeLoc());
300 dumpTypeAsChild(T->getElementType());
301 dumpStmt(T->getSizeExpr());
302 }
VisitVectorType(const VectorType * T)303 void VisitVectorType(const VectorType *T) {
304 switch (T->getVectorKind()) {
305 case VectorType::GenericVector: break;
306 case VectorType::AltiVecVector: OS << " altivec"; break;
307 case VectorType::AltiVecPixel: OS << " altivec pixel"; break;
308 case VectorType::AltiVecBool: OS << " altivec bool"; break;
309 case VectorType::NeonVector: OS << " neon"; break;
310 case VectorType::NeonPolyVector: OS << " neon poly"; break;
311 }
312 OS << " " << T->getNumElements();
313 dumpTypeAsChild(T->getElementType());
314 }
VisitFunctionType(const FunctionType * T)315 void VisitFunctionType(const FunctionType *T) {
316 auto EI = T->getExtInfo();
317 if (EI.getNoReturn()) OS << " noreturn";
318 if (EI.getProducesResult()) OS << " produces_result";
319 if (EI.getHasRegParm()) OS << " regparm " << EI.getRegParm();
320 OS << " " << FunctionType::getNameForCallConv(EI.getCC());
321 dumpTypeAsChild(T->getReturnType());
322 }
VisitFunctionProtoType(const FunctionProtoType * T)323 void VisitFunctionProtoType(const FunctionProtoType *T) {
324 auto EPI = T->getExtProtoInfo();
325 if (EPI.HasTrailingReturn) OS << " trailing_return";
326 if (T->isConst()) OS << " const";
327 if (T->isVolatile()) OS << " volatile";
328 if (T->isRestrict()) OS << " restrict";
329 switch (EPI.RefQualifier) {
330 case RQ_None: break;
331 case RQ_LValue: OS << " &"; break;
332 case RQ_RValue: OS << " &&"; break;
333 }
334 // FIXME: Exception specification.
335 // FIXME: Consumed parameters.
336 VisitFunctionType(T);
337 for (QualType PT : T->getParamTypes())
338 dumpTypeAsChild(PT);
339 if (EPI.Variadic)
340 dumpChild([=] { OS << "..."; });
341 }
VisitUnresolvedUsingType(const UnresolvedUsingType * T)342 void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
343 dumpDeclRef(T->getDecl());
344 }
VisitTypedefType(const TypedefType * T)345 void VisitTypedefType(const TypedefType *T) {
346 dumpDeclRef(T->getDecl());
347 }
VisitTypeOfExprType(const TypeOfExprType * T)348 void VisitTypeOfExprType(const TypeOfExprType *T) {
349 dumpStmt(T->getUnderlyingExpr());
350 }
VisitDecltypeType(const DecltypeType * T)351 void VisitDecltypeType(const DecltypeType *T) {
352 dumpStmt(T->getUnderlyingExpr());
353 }
VisitUnaryTransformType(const UnaryTransformType * T)354 void VisitUnaryTransformType(const UnaryTransformType *T) {
355 switch (T->getUTTKind()) {
356 case UnaryTransformType::EnumUnderlyingType:
357 OS << " underlying_type";
358 break;
359 }
360 dumpTypeAsChild(T->getBaseType());
361 }
VisitTagType(const TagType * T)362 void VisitTagType(const TagType *T) {
363 dumpDeclRef(T->getDecl());
364 }
VisitAttributedType(const AttributedType * T)365 void VisitAttributedType(const AttributedType *T) {
366 // FIXME: AttrKind
367 dumpTypeAsChild(T->getModifiedType());
368 }
VisitTemplateTypeParmType(const TemplateTypeParmType * T)369 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
370 OS << " depth " << T->getDepth() << " index " << T->getIndex();
371 if (T->isParameterPack()) OS << " pack";
372 dumpDeclRef(T->getDecl());
373 }
VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType * T)374 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
375 dumpTypeAsChild(T->getReplacedParameter());
376 }
VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType * T)377 void VisitSubstTemplateTypeParmPackType(
378 const SubstTemplateTypeParmPackType *T) {
379 dumpTypeAsChild(T->getReplacedParameter());
380 dumpTemplateArgument(T->getArgumentPack());
381 }
VisitAutoType(const AutoType * T)382 void VisitAutoType(const AutoType *T) {
383 if (T->isDecltypeAuto()) OS << " decltype(auto)";
384 if (!T->isDeduced())
385 OS << " undeduced";
386 }
VisitTemplateSpecializationType(const TemplateSpecializationType * T)387 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
388 if (T->isTypeAlias()) OS << " alias";
389 OS << " "; T->getTemplateName().dump(OS);
390 for (auto &Arg : *T)
391 dumpTemplateArgument(Arg);
392 if (T->isTypeAlias())
393 dumpTypeAsChild(T->getAliasedType());
394 }
VisitInjectedClassNameType(const InjectedClassNameType * T)395 void VisitInjectedClassNameType(const InjectedClassNameType *T) {
396 dumpDeclRef(T->getDecl());
397 }
VisitObjCInterfaceType(const ObjCInterfaceType * T)398 void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
399 dumpDeclRef(T->getDecl());
400 }
VisitObjCObjectPointerType(const ObjCObjectPointerType * T)401 void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
402 dumpTypeAsChild(T->getPointeeType());
403 }
VisitAtomicType(const AtomicType * T)404 void VisitAtomicType(const AtomicType *T) {
405 dumpTypeAsChild(T->getValueType());
406 }
VisitPipeType(const PipeType * T)407 void VisitPipeType(const PipeType *T) {
408 dumpTypeAsChild(T->getElementType());
409 }
VisitAdjustedType(const AdjustedType * T)410 void VisitAdjustedType(const AdjustedType *T) {
411 dumpTypeAsChild(T->getOriginalType());
412 }
VisitPackExpansionType(const PackExpansionType * T)413 void VisitPackExpansionType(const PackExpansionType *T) {
414 if (auto N = T->getNumExpansions()) OS << " expansions " << *N;
415 if (!T->isSugared())
416 dumpTypeAsChild(T->getPattern());
417 }
418 // FIXME: ElaboratedType, DependentNameType,
419 // DependentTemplateSpecializationType, ObjCObjectType
420
421 // Decls
422 void VisitLabelDecl(const LabelDecl *D);
423 void VisitTypedefDecl(const TypedefDecl *D);
424 void VisitEnumDecl(const EnumDecl *D);
425 void VisitRecordDecl(const RecordDecl *D);
426 void VisitEnumConstantDecl(const EnumConstantDecl *D);
427 void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
428 void VisitFunctionDecl(const FunctionDecl *D);
429 void VisitFieldDecl(const FieldDecl *D);
430 void VisitVarDecl(const VarDecl *D);
431 void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D);
432 void VisitImportDecl(const ImportDecl *D);
433 void VisitPragmaCommentDecl(const PragmaCommentDecl *D);
434 void VisitPragmaDetectMismatchDecl(const PragmaDetectMismatchDecl *D);
435 void VisitCapturedDecl(const CapturedDecl *D);
436
437 // OpenMP decls
438 void VisitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D);
439 void VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D);
440 void VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D);
441
442 // C++ Decls
443 void VisitNamespaceDecl(const NamespaceDecl *D);
444 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
445 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
446 void VisitTypeAliasDecl(const TypeAliasDecl *D);
447 void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
448 void VisitCXXRecordDecl(const CXXRecordDecl *D);
449 void VisitStaticAssertDecl(const StaticAssertDecl *D);
450 template<typename SpecializationDecl>
451 void VisitTemplateDeclSpecialization(const SpecializationDecl *D,
452 bool DumpExplicitInst,
453 bool DumpRefOnly);
454 template<typename TemplateDecl>
455 void VisitTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst);
456 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
457 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
458 void VisitClassTemplateSpecializationDecl(
459 const ClassTemplateSpecializationDecl *D);
460 void VisitClassTemplatePartialSpecializationDecl(
461 const ClassTemplatePartialSpecializationDecl *D);
462 void VisitClassScopeFunctionSpecializationDecl(
463 const ClassScopeFunctionSpecializationDecl *D);
464 void VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D);
465 void VisitVarTemplateDecl(const VarTemplateDecl *D);
466 void VisitVarTemplateSpecializationDecl(
467 const VarTemplateSpecializationDecl *D);
468 void VisitVarTemplatePartialSpecializationDecl(
469 const VarTemplatePartialSpecializationDecl *D);
470 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
471 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
472 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
473 void VisitUsingDecl(const UsingDecl *D);
474 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
475 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
476 void VisitUsingShadowDecl(const UsingShadowDecl *D);
477 void VisitConstructorUsingShadowDecl(const ConstructorUsingShadowDecl *D);
478 void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
479 void VisitAccessSpecDecl(const AccessSpecDecl *D);
480 void VisitFriendDecl(const FriendDecl *D);
481
482 // ObjC Decls
483 void VisitObjCIvarDecl(const ObjCIvarDecl *D);
484 void VisitObjCMethodDecl(const ObjCMethodDecl *D);
485 void VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D);
486 void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
487 void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
488 void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
489 void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
490 void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
491 void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
492 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
493 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
494 void VisitBlockDecl(const BlockDecl *D);
495
496 // Stmts.
497 void VisitStmt(const Stmt *Node);
498 void VisitDeclStmt(const DeclStmt *Node);
499 void VisitAttributedStmt(const AttributedStmt *Node);
500 void VisitLabelStmt(const LabelStmt *Node);
501 void VisitGotoStmt(const GotoStmt *Node);
502 void VisitCXXCatchStmt(const CXXCatchStmt *Node);
503 void VisitCapturedStmt(const CapturedStmt *Node);
504
505 // OpenMP
506 void VisitOMPExecutableDirective(const OMPExecutableDirective *Node);
507
508 // Exprs
509 void VisitExpr(const Expr *Node);
510 void VisitCastExpr(const CastExpr *Node);
511 void VisitDeclRefExpr(const DeclRefExpr *Node);
512 void VisitPredefinedExpr(const PredefinedExpr *Node);
513 void VisitCharacterLiteral(const CharacterLiteral *Node);
514 void VisitIntegerLiteral(const IntegerLiteral *Node);
515 void VisitFloatingLiteral(const FloatingLiteral *Node);
516 void VisitStringLiteral(const StringLiteral *Str);
517 void VisitInitListExpr(const InitListExpr *ILE);
518 void VisitUnaryOperator(const UnaryOperator *Node);
519 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
520 void VisitMemberExpr(const MemberExpr *Node);
521 void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
522 void VisitBinaryOperator(const BinaryOperator *Node);
523 void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
524 void VisitAddrLabelExpr(const AddrLabelExpr *Node);
525 void VisitBlockExpr(const BlockExpr *Node);
526 void VisitOpaqueValueExpr(const OpaqueValueExpr *Node);
527
528 // C++
529 void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
530 void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
531 void VisitCXXThisExpr(const CXXThisExpr *Node);
532 void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
533 void VisitCXXConstructExpr(const CXXConstructExpr *Node);
534 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
535 void VisitCXXNewExpr(const CXXNewExpr *Node);
536 void VisitCXXDeleteExpr(const CXXDeleteExpr *Node);
537 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
538 void VisitExprWithCleanups(const ExprWithCleanups *Node);
539 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
540 void dumpCXXTemporary(const CXXTemporary *Temporary);
VisitLambdaExpr(const LambdaExpr * Node)541 void VisitLambdaExpr(const LambdaExpr *Node) {
542 VisitExpr(Node);
543 dumpDecl(Node->getLambdaClass());
544 }
545 void VisitSizeOfPackExpr(const SizeOfPackExpr *Node);
546
547 // ObjC
548 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
549 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
550 void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
551 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
552 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
553 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
554 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
555 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
556 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
557 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
558
559 // Comments.
560 const char *getCommandName(unsigned CommandID);
561 void dumpComment(const Comment *C);
562
563 // Inline comments.
564 void visitTextComment(const TextComment *C);
565 void visitInlineCommandComment(const InlineCommandComment *C);
566 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
567 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
568
569 // Block comments.
570 void visitBlockCommandComment(const BlockCommandComment *C);
571 void visitParamCommandComment(const ParamCommandComment *C);
572 void visitTParamCommandComment(const TParamCommandComment *C);
573 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
574 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
575 void visitVerbatimLineComment(const VerbatimLineComment *C);
576 };
577 }
578
579 //===----------------------------------------------------------------------===//
580 // Utilities
581 //===----------------------------------------------------------------------===//
582
dumpPointer(const void * Ptr)583 void ASTDumper::dumpPointer(const void *Ptr) {
584 ColorScope Color(*this, AddressColor);
585 OS << ' ' << Ptr;
586 }
587
dumpLocation(SourceLocation Loc)588 void ASTDumper::dumpLocation(SourceLocation Loc) {
589 if (!SM)
590 return;
591
592 ColorScope Color(*this, LocationColor);
593 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
594
595 // The general format we print out is filename:line:col, but we drop pieces
596 // that haven't changed since the last loc printed.
597 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
598
599 if (PLoc.isInvalid()) {
600 OS << "<invalid sloc>";
601 return;
602 }
603
604 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
605 OS << PLoc.getFilename() << ':' << PLoc.getLine()
606 << ':' << PLoc.getColumn();
607 LastLocFilename = PLoc.getFilename();
608 LastLocLine = PLoc.getLine();
609 } else if (PLoc.getLine() != LastLocLine) {
610 OS << "line" << ':' << PLoc.getLine()
611 << ':' << PLoc.getColumn();
612 LastLocLine = PLoc.getLine();
613 } else {
614 OS << "col" << ':' << PLoc.getColumn();
615 }
616 }
617
dumpSourceRange(SourceRange R)618 void ASTDumper::dumpSourceRange(SourceRange R) {
619 // Can't translate locations if a SourceManager isn't available.
620 if (!SM)
621 return;
622
623 OS << " <";
624 dumpLocation(R.getBegin());
625 if (R.getBegin() != R.getEnd()) {
626 OS << ", ";
627 dumpLocation(R.getEnd());
628 }
629 OS << ">";
630
631 // <t2.c:123:421[blah], t2.c:412:321>
632
633 }
634
dumpBareType(QualType T,bool Desugar)635 void ASTDumper::dumpBareType(QualType T, bool Desugar) {
636 ColorScope Color(*this, TypeColor);
637
638 SplitQualType T_split = T.split();
639 OS << "'" << QualType::getAsString(T_split) << "'";
640
641 if (Desugar && !T.isNull()) {
642 // If the type is sugared, also dump a (shallow) desugared type.
643 SplitQualType D_split = T.getSplitDesugaredType();
644 if (T_split != D_split)
645 OS << ":'" << QualType::getAsString(D_split) << "'";
646 }
647 }
648
dumpType(QualType T)649 void ASTDumper::dumpType(QualType T) {
650 OS << ' ';
651 dumpBareType(T);
652 }
653
dumpTypeAsChild(QualType T)654 void ASTDumper::dumpTypeAsChild(QualType T) {
655 SplitQualType SQT = T.split();
656 if (!SQT.Quals.hasQualifiers())
657 return dumpTypeAsChild(SQT.Ty);
658
659 dumpChild([=] {
660 OS << "QualType";
661 dumpPointer(T.getAsOpaquePtr());
662 OS << " ";
663 dumpBareType(T, false);
664 OS << " " << T.split().Quals.getAsString();
665 dumpTypeAsChild(T.split().Ty);
666 });
667 }
668
dumpTypeAsChild(const Type * T)669 void ASTDumper::dumpTypeAsChild(const Type *T) {
670 dumpChild([=] {
671 if (!T) {
672 ColorScope Color(*this, NullColor);
673 OS << "<<<NULL>>>";
674 return;
675 }
676 if (const LocInfoType *LIT = llvm::dyn_cast<LocInfoType>(T)) {
677 {
678 ColorScope Color(*this, TypeColor);
679 OS << "LocInfo Type";
680 }
681 dumpPointer(T);
682 dumpTypeAsChild(LIT->getTypeSourceInfo()->getType());
683 return;
684 }
685
686 {
687 ColorScope Color(*this, TypeColor);
688 OS << T->getTypeClassName() << "Type";
689 }
690 dumpPointer(T);
691 OS << " ";
692 dumpBareType(QualType(T, 0), false);
693
694 QualType SingleStepDesugar =
695 T->getLocallyUnqualifiedSingleStepDesugaredType();
696 if (SingleStepDesugar != QualType(T, 0))
697 OS << " sugar";
698 if (T->isDependentType())
699 OS << " dependent";
700 else if (T->isInstantiationDependentType())
701 OS << " instantiation_dependent";
702 if (T->isVariablyModifiedType())
703 OS << " variably_modified";
704 if (T->containsUnexpandedParameterPack())
705 OS << " contains_unexpanded_pack";
706 if (T->isFromAST())
707 OS << " imported";
708
709 TypeVisitor<ASTDumper>::Visit(T);
710
711 if (SingleStepDesugar != QualType(T, 0))
712 dumpTypeAsChild(SingleStepDesugar);
713 });
714 }
715
dumpBareDeclRef(const Decl * D)716 void ASTDumper::dumpBareDeclRef(const Decl *D) {
717 if (!D) {
718 ColorScope Color(*this, NullColor);
719 OS << "<<<NULL>>>";
720 return;
721 }
722
723 {
724 ColorScope Color(*this, DeclKindNameColor);
725 OS << D->getDeclKindName();
726 }
727 dumpPointer(D);
728
729 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
730 ColorScope Color(*this, DeclNameColor);
731 OS << " '" << ND->getDeclName() << '\'';
732 }
733
734 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
735 dumpType(VD->getType());
736 }
737
dumpDeclRef(const Decl * D,const char * Label)738 void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
739 if (!D)
740 return;
741
742 dumpChild([=]{
743 if (Label)
744 OS << Label << ' ';
745 dumpBareDeclRef(D);
746 });
747 }
748
dumpName(const NamedDecl * ND)749 void ASTDumper::dumpName(const NamedDecl *ND) {
750 if (ND->getDeclName()) {
751 ColorScope Color(*this, DeclNameColor);
752 OS << ' ' << ND->getNameAsString();
753 }
754 }
755
hasNodes(const DeclContext * DC)756 bool ASTDumper::hasNodes(const DeclContext *DC) {
757 if (!DC)
758 return false;
759
760 return DC->hasExternalLexicalStorage() ||
761 DC->noload_decls_begin() != DC->noload_decls_end();
762 }
763
dumpDeclContext(const DeclContext * DC)764 void ASTDumper::dumpDeclContext(const DeclContext *DC) {
765 if (!DC)
766 return;
767
768 for (auto *D : DC->noload_decls())
769 dumpDecl(D);
770
771 if (DC->hasExternalLexicalStorage()) {
772 dumpChild([=]{
773 ColorScope Color(*this, UndeserializedColor);
774 OS << "<undeserialized declarations>";
775 });
776 }
777 }
778
dumpLookups(const DeclContext * DC,bool DumpDecls)779 void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
780 dumpChild([=] {
781 OS << "StoredDeclsMap ";
782 dumpBareDeclRef(cast<Decl>(DC));
783
784 const DeclContext *Primary = DC->getPrimaryContext();
785 if (Primary != DC) {
786 OS << " primary";
787 dumpPointer(cast<Decl>(Primary));
788 }
789
790 bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
791
792 DeclContext::all_lookups_iterator I = Primary->noload_lookups_begin(),
793 E = Primary->noload_lookups_end();
794 while (I != E) {
795 DeclarationName Name = I.getLookupName();
796 DeclContextLookupResult R = *I++;
797
798 dumpChild([=] {
799 OS << "DeclarationName ";
800 {
801 ColorScope Color(*this, DeclNameColor);
802 OS << '\'' << Name << '\'';
803 }
804
805 for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
806 RI != RE; ++RI) {
807 dumpChild([=] {
808 dumpBareDeclRef(*RI);
809
810 if ((*RI)->isHidden())
811 OS << " hidden";
812
813 // If requested, dump the redecl chain for this lookup.
814 if (DumpDecls) {
815 // Dump earliest decl first.
816 std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) {
817 if (Decl *Prev = D->getPreviousDecl())
818 DumpWithPrev(Prev);
819 dumpDecl(D);
820 };
821 DumpWithPrev(*RI);
822 }
823 });
824 }
825 });
826 }
827
828 if (HasUndeserializedLookups) {
829 dumpChild([=] {
830 ColorScope Color(*this, UndeserializedColor);
831 OS << "<undeserialized lookups>";
832 });
833 }
834 });
835 }
836
dumpAttr(const Attr * A)837 void ASTDumper::dumpAttr(const Attr *A) {
838 dumpChild([=] {
839 {
840 ColorScope Color(*this, AttrColor);
841
842 switch (A->getKind()) {
843 #define ATTR(X) case attr::X: OS << #X; break;
844 #include "clang/Basic/AttrList.inc"
845 }
846 OS << "Attr";
847 }
848 dumpPointer(A);
849 dumpSourceRange(A->getRange());
850 if (A->isInherited())
851 OS << " Inherited";
852 if (A->isImplicit())
853 OS << " Implicit";
854 #include "clang/AST/AttrDump.inc"
855 });
856 }
857
dumpPreviousDeclImpl(raw_ostream & OS,...)858 static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
859
860 template<typename T>
dumpPreviousDeclImpl(raw_ostream & OS,const Mergeable<T> * D)861 static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
862 const T *First = D->getFirstDecl();
863 if (First != D)
864 OS << " first " << First;
865 }
866
867 template<typename T>
dumpPreviousDeclImpl(raw_ostream & OS,const Redeclarable<T> * D)868 static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
869 const T *Prev = D->getPreviousDecl();
870 if (Prev)
871 OS << " prev " << Prev;
872 }
873
874 /// Dump the previous declaration in the redeclaration chain for a declaration,
875 /// if any.
dumpPreviousDecl(raw_ostream & OS,const Decl * D)876 static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
877 switch (D->getKind()) {
878 #define DECL(DERIVED, BASE) \
879 case Decl::DERIVED: \
880 return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
881 #define ABSTRACT_DECL(DECL)
882 #include "clang/AST/DeclNodes.inc"
883 }
884 llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
885 }
886
887 //===----------------------------------------------------------------------===//
888 // C++ Utilities
889 //===----------------------------------------------------------------------===//
890
dumpAccessSpecifier(AccessSpecifier AS)891 void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
892 switch (AS) {
893 case AS_none:
894 break;
895 case AS_public:
896 OS << "public";
897 break;
898 case AS_protected:
899 OS << "protected";
900 break;
901 case AS_private:
902 OS << "private";
903 break;
904 }
905 }
906
dumpCXXCtorInitializer(const CXXCtorInitializer * Init)907 void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
908 dumpChild([=] {
909 OS << "CXXCtorInitializer";
910 if (Init->isAnyMemberInitializer()) {
911 OS << ' ';
912 dumpBareDeclRef(Init->getAnyMember());
913 } else if (Init->isBaseInitializer()) {
914 dumpType(QualType(Init->getBaseClass(), 0));
915 } else if (Init->isDelegatingInitializer()) {
916 dumpType(Init->getTypeSourceInfo()->getType());
917 } else {
918 llvm_unreachable("Unknown initializer type");
919 }
920 dumpStmt(Init->getInit());
921 });
922 }
923
dumpTemplateParameters(const TemplateParameterList * TPL)924 void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
925 if (!TPL)
926 return;
927
928 for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
929 I != E; ++I)
930 dumpDecl(*I);
931 }
932
dumpTemplateArgumentListInfo(const TemplateArgumentListInfo & TALI)933 void ASTDumper::dumpTemplateArgumentListInfo(
934 const TemplateArgumentListInfo &TALI) {
935 for (unsigned i = 0, e = TALI.size(); i < e; ++i)
936 dumpTemplateArgumentLoc(TALI[i]);
937 }
938
dumpTemplateArgumentLoc(const TemplateArgumentLoc & A)939 void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
940 dumpTemplateArgument(A.getArgument(), A.getSourceRange());
941 }
942
dumpTemplateArgumentList(const TemplateArgumentList & TAL)943 void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
944 for (unsigned i = 0, e = TAL.size(); i < e; ++i)
945 dumpTemplateArgument(TAL[i]);
946 }
947
dumpTemplateArgument(const TemplateArgument & A,SourceRange R)948 void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
949 dumpChild([=] {
950 OS << "TemplateArgument";
951 if (R.isValid())
952 dumpSourceRange(R);
953
954 switch (A.getKind()) {
955 case TemplateArgument::Null:
956 OS << " null";
957 break;
958 case TemplateArgument::Type:
959 OS << " type";
960 dumpType(A.getAsType());
961 break;
962 case TemplateArgument::Declaration:
963 OS << " decl";
964 dumpDeclRef(A.getAsDecl());
965 break;
966 case TemplateArgument::NullPtr:
967 OS << " nullptr";
968 break;
969 case TemplateArgument::Integral:
970 OS << " integral " << A.getAsIntegral();
971 break;
972 case TemplateArgument::Template:
973 OS << " template ";
974 A.getAsTemplate().dump(OS);
975 break;
976 case TemplateArgument::TemplateExpansion:
977 OS << " template expansion";
978 A.getAsTemplateOrTemplatePattern().dump(OS);
979 break;
980 case TemplateArgument::Expression:
981 OS << " expr";
982 dumpStmt(A.getAsExpr());
983 break;
984 case TemplateArgument::Pack:
985 OS << " pack";
986 for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
987 I != E; ++I)
988 dumpTemplateArgument(*I);
989 break;
990 }
991 });
992 }
993
994 //===----------------------------------------------------------------------===//
995 // Objective-C Utilities
996 //===----------------------------------------------------------------------===//
dumpObjCTypeParamList(const ObjCTypeParamList * typeParams)997 void ASTDumper::dumpObjCTypeParamList(const ObjCTypeParamList *typeParams) {
998 if (!typeParams)
999 return;
1000
1001 for (auto typeParam : *typeParams) {
1002 dumpDecl(typeParam);
1003 }
1004 }
1005
1006 //===----------------------------------------------------------------------===//
1007 // Decl dumping methods.
1008 //===----------------------------------------------------------------------===//
1009
dumpDecl(const Decl * D)1010 void ASTDumper::dumpDecl(const Decl *D) {
1011 dumpChild([=] {
1012 if (!D) {
1013 ColorScope Color(*this, NullColor);
1014 OS << "<<<NULL>>>";
1015 return;
1016 }
1017
1018 {
1019 ColorScope Color(*this, DeclKindNameColor);
1020 OS << D->getDeclKindName() << "Decl";
1021 }
1022 dumpPointer(D);
1023 if (D->getLexicalDeclContext() != D->getDeclContext())
1024 OS << " parent " << cast<Decl>(D->getDeclContext());
1025 dumpPreviousDecl(OS, D);
1026 dumpSourceRange(D->getSourceRange());
1027 OS << ' ';
1028 dumpLocation(D->getLocation());
1029 if (Module *M = D->getImportedOwningModule())
1030 OS << " in " << M->getFullModuleName();
1031 else if (Module *M = D->getLocalOwningModule())
1032 OS << " in (local) " << M->getFullModuleName();
1033 if (auto *ND = dyn_cast<NamedDecl>(D))
1034 for (Module *M : D->getASTContext().getModulesWithMergedDefinition(
1035 const_cast<NamedDecl *>(ND)))
1036 dumpChild([=] { OS << "also in " << M->getFullModuleName(); });
1037 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
1038 if (ND->isHidden())
1039 OS << " hidden";
1040 if (D->isImplicit())
1041 OS << " implicit";
1042 if (D->isUsed())
1043 OS << " used";
1044 else if (D->isThisDeclarationReferenced())
1045 OS << " referenced";
1046 if (D->isInvalidDecl())
1047 OS << " invalid";
1048 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1049 if (FD->isConstexpr())
1050 OS << " constexpr";
1051
1052
1053 ConstDeclVisitor<ASTDumper>::Visit(D);
1054
1055 for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); I != E;
1056 ++I)
1057 dumpAttr(*I);
1058
1059 if (const FullComment *Comment =
1060 D->getASTContext().getLocalCommentForDeclUncached(D))
1061 dumpFullComment(Comment);
1062
1063 // Decls within functions are visited by the body.
1064 if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
1065 hasNodes(dyn_cast<DeclContext>(D)))
1066 dumpDeclContext(cast<DeclContext>(D));
1067 });
1068 }
1069
VisitLabelDecl(const LabelDecl * D)1070 void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
1071 dumpName(D);
1072 }
1073
VisitTypedefDecl(const TypedefDecl * D)1074 void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
1075 dumpName(D);
1076 dumpType(D->getUnderlyingType());
1077 if (D->isModulePrivate())
1078 OS << " __module_private__";
1079 dumpTypeAsChild(D->getUnderlyingType());
1080 }
1081
VisitEnumDecl(const EnumDecl * D)1082 void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
1083 if (D->isScoped()) {
1084 if (D->isScopedUsingClassTag())
1085 OS << " class";
1086 else
1087 OS << " struct";
1088 }
1089 dumpName(D);
1090 if (D->isModulePrivate())
1091 OS << " __module_private__";
1092 if (D->isFixed())
1093 dumpType(D->getIntegerType());
1094 }
1095
VisitRecordDecl(const RecordDecl * D)1096 void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
1097 OS << ' ' << D->getKindName();
1098 dumpName(D);
1099 if (D->isModulePrivate())
1100 OS << " __module_private__";
1101 if (D->isCompleteDefinition())
1102 OS << " definition";
1103 }
1104
VisitEnumConstantDecl(const EnumConstantDecl * D)1105 void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
1106 dumpName(D);
1107 dumpType(D->getType());
1108 if (const Expr *Init = D->getInitExpr())
1109 dumpStmt(Init);
1110 }
1111
VisitIndirectFieldDecl(const IndirectFieldDecl * D)1112 void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
1113 dumpName(D);
1114 dumpType(D->getType());
1115
1116 for (auto *Child : D->chain())
1117 dumpDeclRef(Child);
1118 }
1119
VisitFunctionDecl(const FunctionDecl * D)1120 void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
1121 dumpName(D);
1122 dumpType(D->getType());
1123
1124 StorageClass SC = D->getStorageClass();
1125 if (SC != SC_None)
1126 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1127 if (D->isInlineSpecified())
1128 OS << " inline";
1129 if (D->isVirtualAsWritten())
1130 OS << " virtual";
1131 if (D->isModulePrivate())
1132 OS << " __module_private__";
1133
1134 if (D->isPure())
1135 OS << " pure";
1136 else if (D->isDeletedAsWritten())
1137 OS << " delete";
1138
1139 if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
1140 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1141 switch (EPI.ExceptionSpec.Type) {
1142 default: break;
1143 case EST_Unevaluated:
1144 OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl;
1145 break;
1146 case EST_Uninstantiated:
1147 OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate;
1148 break;
1149 }
1150 }
1151
1152 if (const FunctionTemplateSpecializationInfo *FTSI =
1153 D->getTemplateSpecializationInfo())
1154 dumpTemplateArgumentList(*FTSI->TemplateArguments);
1155
1156 for (ArrayRef<NamedDecl *>::iterator
1157 I = D->getDeclsInPrototypeScope().begin(),
1158 E = D->getDeclsInPrototypeScope().end(); I != E; ++I)
1159 dumpDecl(*I);
1160
1161 if (!D->param_begin() && D->getNumParams())
1162 dumpChild([=] { OS << "<<NULL params x " << D->getNumParams() << ">>"; });
1163 else
1164 for (const ParmVarDecl *Parameter : D->parameters())
1165 dumpDecl(Parameter);
1166
1167 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D))
1168 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
1169 E = C->init_end();
1170 I != E; ++I)
1171 dumpCXXCtorInitializer(*I);
1172
1173 if (D->doesThisDeclarationHaveABody())
1174 dumpStmt(D->getBody());
1175 }
1176
VisitFieldDecl(const FieldDecl * D)1177 void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
1178 dumpName(D);
1179 dumpType(D->getType());
1180 if (D->isMutable())
1181 OS << " mutable";
1182 if (D->isModulePrivate())
1183 OS << " __module_private__";
1184
1185 if (D->isBitField())
1186 dumpStmt(D->getBitWidth());
1187 if (Expr *Init = D->getInClassInitializer())
1188 dumpStmt(Init);
1189 }
1190
VisitVarDecl(const VarDecl * D)1191 void ASTDumper::VisitVarDecl(const VarDecl *D) {
1192 dumpName(D);
1193 dumpType(D->getType());
1194 StorageClass SC = D->getStorageClass();
1195 if (SC != SC_None)
1196 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1197 switch (D->getTLSKind()) {
1198 case VarDecl::TLS_None: break;
1199 case VarDecl::TLS_Static: OS << " tls"; break;
1200 case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
1201 }
1202 if (D->isModulePrivate())
1203 OS << " __module_private__";
1204 if (D->isNRVOVariable())
1205 OS << " nrvo";
1206 if (D->isInline())
1207 OS << " inline";
1208 if (D->isConstexpr())
1209 OS << " constexpr";
1210 if (D->hasInit()) {
1211 switch (D->getInitStyle()) {
1212 case VarDecl::CInit: OS << " cinit"; break;
1213 case VarDecl::CallInit: OS << " callinit"; break;
1214 case VarDecl::ListInit: OS << " listinit"; break;
1215 }
1216 dumpStmt(D->getInit());
1217 }
1218 }
1219
VisitFileScopeAsmDecl(const FileScopeAsmDecl * D)1220 void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
1221 dumpStmt(D->getAsmString());
1222 }
1223
VisitImportDecl(const ImportDecl * D)1224 void ASTDumper::VisitImportDecl(const ImportDecl *D) {
1225 OS << ' ' << D->getImportedModule()->getFullModuleName();
1226 }
1227
VisitPragmaCommentDecl(const PragmaCommentDecl * D)1228 void ASTDumper::VisitPragmaCommentDecl(const PragmaCommentDecl *D) {
1229 OS << ' ';
1230 switch (D->getCommentKind()) {
1231 case PCK_Unknown: llvm_unreachable("unexpected pragma comment kind");
1232 case PCK_Compiler: OS << "compiler"; break;
1233 case PCK_ExeStr: OS << "exestr"; break;
1234 case PCK_Lib: OS << "lib"; break;
1235 case PCK_Linker: OS << "linker"; break;
1236 case PCK_User: OS << "user"; break;
1237 }
1238 StringRef Arg = D->getArg();
1239 if (!Arg.empty())
1240 OS << " \"" << Arg << "\"";
1241 }
1242
VisitPragmaDetectMismatchDecl(const PragmaDetectMismatchDecl * D)1243 void ASTDumper::VisitPragmaDetectMismatchDecl(
1244 const PragmaDetectMismatchDecl *D) {
1245 OS << " \"" << D->getName() << "\" \"" << D->getValue() << "\"";
1246 }
1247
VisitCapturedDecl(const CapturedDecl * D)1248 void ASTDumper::VisitCapturedDecl(const CapturedDecl *D) {
1249 dumpStmt(D->getBody());
1250 }
1251
1252 //===----------------------------------------------------------------------===//
1253 // OpenMP Declarations
1254 //===----------------------------------------------------------------------===//
1255
VisitOMPThreadPrivateDecl(const OMPThreadPrivateDecl * D)1256 void ASTDumper::VisitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) {
1257 for (auto *E : D->varlists())
1258 dumpStmt(E);
1259 }
1260
VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl * D)1261 void ASTDumper::VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D) {
1262 dumpName(D);
1263 dumpType(D->getType());
1264 OS << " combiner";
1265 dumpStmt(D->getCombiner());
1266 if (auto *Initializer = D->getInitializer()) {
1267 OS << " initializer";
1268 dumpStmt(Initializer);
1269 }
1270 }
1271
VisitOMPCapturedExprDecl(const OMPCapturedExprDecl * D)1272 void ASTDumper::VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D) {
1273 dumpName(D);
1274 dumpType(D->getType());
1275 dumpStmt(D->getInit());
1276 }
1277
1278 //===----------------------------------------------------------------------===//
1279 // C++ Declarations
1280 //===----------------------------------------------------------------------===//
1281
VisitNamespaceDecl(const NamespaceDecl * D)1282 void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
1283 dumpName(D);
1284 if (D->isInline())
1285 OS << " inline";
1286 if (!D->isOriginalNamespace())
1287 dumpDeclRef(D->getOriginalNamespace(), "original");
1288 }
1289
VisitUsingDirectiveDecl(const UsingDirectiveDecl * D)1290 void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
1291 OS << ' ';
1292 dumpBareDeclRef(D->getNominatedNamespace());
1293 }
1294
VisitNamespaceAliasDecl(const NamespaceAliasDecl * D)1295 void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
1296 dumpName(D);
1297 dumpDeclRef(D->getAliasedNamespace());
1298 }
1299
VisitTypeAliasDecl(const TypeAliasDecl * D)1300 void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
1301 dumpName(D);
1302 dumpType(D->getUnderlyingType());
1303 dumpTypeAsChild(D->getUnderlyingType());
1304 }
1305
VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl * D)1306 void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
1307 dumpName(D);
1308 dumpTemplateParameters(D->getTemplateParameters());
1309 dumpDecl(D->getTemplatedDecl());
1310 }
1311
VisitCXXRecordDecl(const CXXRecordDecl * D)1312 void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
1313 VisitRecordDecl(D);
1314 if (!D->isCompleteDefinition())
1315 return;
1316
1317 for (const auto &I : D->bases()) {
1318 dumpChild([=] {
1319 if (I.isVirtual())
1320 OS << "virtual ";
1321 dumpAccessSpecifier(I.getAccessSpecifier());
1322 dumpType(I.getType());
1323 if (I.isPackExpansion())
1324 OS << "...";
1325 });
1326 }
1327 }
1328
VisitStaticAssertDecl(const StaticAssertDecl * D)1329 void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
1330 dumpStmt(D->getAssertExpr());
1331 dumpStmt(D->getMessage());
1332 }
1333
1334 template<typename SpecializationDecl>
VisitTemplateDeclSpecialization(const SpecializationDecl * D,bool DumpExplicitInst,bool DumpRefOnly)1335 void ASTDumper::VisitTemplateDeclSpecialization(const SpecializationDecl *D,
1336 bool DumpExplicitInst,
1337 bool DumpRefOnly) {
1338 bool DumpedAny = false;
1339 for (auto *RedeclWithBadType : D->redecls()) {
1340 // FIXME: The redecls() range sometimes has elements of a less-specific
1341 // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
1342 // us TagDecls, and should give CXXRecordDecls).
1343 auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
1344 if (!Redecl) {
1345 // Found the injected-class-name for a class template. This will be dumped
1346 // as part of its surrounding class so we don't need to dump it here.
1347 assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
1348 "expected an injected-class-name");
1349 continue;
1350 }
1351
1352 switch (Redecl->getTemplateSpecializationKind()) {
1353 case TSK_ExplicitInstantiationDeclaration:
1354 case TSK_ExplicitInstantiationDefinition:
1355 if (!DumpExplicitInst)
1356 break;
1357 // Fall through.
1358 case TSK_Undeclared:
1359 case TSK_ImplicitInstantiation:
1360 if (DumpRefOnly)
1361 dumpDeclRef(Redecl);
1362 else
1363 dumpDecl(Redecl);
1364 DumpedAny = true;
1365 break;
1366 case TSK_ExplicitSpecialization:
1367 break;
1368 }
1369 }
1370
1371 // Ensure we dump at least one decl for each specialization.
1372 if (!DumpedAny)
1373 dumpDeclRef(D);
1374 }
1375
1376 template<typename TemplateDecl>
VisitTemplateDecl(const TemplateDecl * D,bool DumpExplicitInst)1377 void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
1378 bool DumpExplicitInst) {
1379 dumpName(D);
1380 dumpTemplateParameters(D->getTemplateParameters());
1381
1382 dumpDecl(D->getTemplatedDecl());
1383
1384 for (auto *Child : D->specializations())
1385 VisitTemplateDeclSpecialization(Child, DumpExplicitInst,
1386 !D->isCanonicalDecl());
1387 }
1388
VisitFunctionTemplateDecl(const FunctionTemplateDecl * D)1389 void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
1390 // FIXME: We don't add a declaration of a function template specialization
1391 // to its context when it's explicitly instantiated, so dump explicit
1392 // instantiations when we dump the template itself.
1393 VisitTemplateDecl(D, true);
1394 }
1395
VisitClassTemplateDecl(const ClassTemplateDecl * D)1396 void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
1397 VisitTemplateDecl(D, false);
1398 }
1399
VisitClassTemplateSpecializationDecl(const ClassTemplateSpecializationDecl * D)1400 void ASTDumper::VisitClassTemplateSpecializationDecl(
1401 const ClassTemplateSpecializationDecl *D) {
1402 VisitCXXRecordDecl(D);
1403 dumpTemplateArgumentList(D->getTemplateArgs());
1404 }
1405
VisitClassTemplatePartialSpecializationDecl(const ClassTemplatePartialSpecializationDecl * D)1406 void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
1407 const ClassTemplatePartialSpecializationDecl *D) {
1408 VisitClassTemplateSpecializationDecl(D);
1409 dumpTemplateParameters(D->getTemplateParameters());
1410 }
1411
VisitClassScopeFunctionSpecializationDecl(const ClassScopeFunctionSpecializationDecl * D)1412 void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
1413 const ClassScopeFunctionSpecializationDecl *D) {
1414 dumpDeclRef(D->getSpecialization());
1415 if (D->hasExplicitTemplateArgs())
1416 dumpTemplateArgumentListInfo(D->templateArgs());
1417 }
1418
VisitVarTemplateDecl(const VarTemplateDecl * D)1419 void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
1420 VisitTemplateDecl(D, false);
1421 }
1422
VisitBuiltinTemplateDecl(const BuiltinTemplateDecl * D)1423 void ASTDumper::VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D) {
1424 dumpName(D);
1425 dumpTemplateParameters(D->getTemplateParameters());
1426 }
1427
VisitVarTemplateSpecializationDecl(const VarTemplateSpecializationDecl * D)1428 void ASTDumper::VisitVarTemplateSpecializationDecl(
1429 const VarTemplateSpecializationDecl *D) {
1430 dumpTemplateArgumentList(D->getTemplateArgs());
1431 VisitVarDecl(D);
1432 }
1433
VisitVarTemplatePartialSpecializationDecl(const VarTemplatePartialSpecializationDecl * D)1434 void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
1435 const VarTemplatePartialSpecializationDecl *D) {
1436 dumpTemplateParameters(D->getTemplateParameters());
1437 VisitVarTemplateSpecializationDecl(D);
1438 }
1439
VisitTemplateTypeParmDecl(const TemplateTypeParmDecl * D)1440 void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
1441 if (D->wasDeclaredWithTypename())
1442 OS << " typename";
1443 else
1444 OS << " class";
1445 if (D->isParameterPack())
1446 OS << " ...";
1447 dumpName(D);
1448 if (D->hasDefaultArgument())
1449 dumpTemplateArgument(D->getDefaultArgument());
1450 }
1451
VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl * D)1452 void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
1453 dumpType(D->getType());
1454 if (D->isParameterPack())
1455 OS << " ...";
1456 dumpName(D);
1457 if (D->hasDefaultArgument())
1458 dumpTemplateArgument(D->getDefaultArgument());
1459 }
1460
VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl * D)1461 void ASTDumper::VisitTemplateTemplateParmDecl(
1462 const TemplateTemplateParmDecl *D) {
1463 if (D->isParameterPack())
1464 OS << " ...";
1465 dumpName(D);
1466 dumpTemplateParameters(D->getTemplateParameters());
1467 if (D->hasDefaultArgument())
1468 dumpTemplateArgumentLoc(D->getDefaultArgument());
1469 }
1470
VisitUsingDecl(const UsingDecl * D)1471 void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
1472 OS << ' ';
1473 if (D->getQualifier())
1474 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1475 OS << D->getNameAsString();
1476 }
1477
VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl * D)1478 void ASTDumper::VisitUnresolvedUsingTypenameDecl(
1479 const UnresolvedUsingTypenameDecl *D) {
1480 OS << ' ';
1481 if (D->getQualifier())
1482 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1483 OS << D->getNameAsString();
1484 }
1485
VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl * D)1486 void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
1487 OS << ' ';
1488 if (D->getQualifier())
1489 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1490 OS << D->getNameAsString();
1491 dumpType(D->getType());
1492 }
1493
VisitUsingShadowDecl(const UsingShadowDecl * D)1494 void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
1495 OS << ' ';
1496 dumpBareDeclRef(D->getTargetDecl());
1497 if (auto *TD = dyn_cast<TypeDecl>(D->getUnderlyingDecl()))
1498 dumpTypeAsChild(TD->getTypeForDecl());
1499 }
1500
VisitConstructorUsingShadowDecl(const ConstructorUsingShadowDecl * D)1501 void ASTDumper::VisitConstructorUsingShadowDecl(
1502 const ConstructorUsingShadowDecl *D) {
1503 if (D->constructsVirtualBase())
1504 OS << " virtual";
1505
1506 dumpChild([=] {
1507 OS << "target ";
1508 dumpBareDeclRef(D->getTargetDecl());
1509 });
1510
1511 dumpChild([=] {
1512 OS << "nominated ";
1513 dumpBareDeclRef(D->getNominatedBaseClass());
1514 OS << ' ';
1515 dumpBareDeclRef(D->getNominatedBaseClassShadowDecl());
1516 });
1517
1518 dumpChild([=] {
1519 OS << "constructed ";
1520 dumpBareDeclRef(D->getConstructedBaseClass());
1521 OS << ' ';
1522 dumpBareDeclRef(D->getConstructedBaseClassShadowDecl());
1523 });
1524 }
1525
VisitLinkageSpecDecl(const LinkageSpecDecl * D)1526 void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
1527 switch (D->getLanguage()) {
1528 case LinkageSpecDecl::lang_c: OS << " C"; break;
1529 case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1530 }
1531 }
1532
VisitAccessSpecDecl(const AccessSpecDecl * D)1533 void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
1534 OS << ' ';
1535 dumpAccessSpecifier(D->getAccess());
1536 }
1537
VisitFriendDecl(const FriendDecl * D)1538 void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
1539 if (TypeSourceInfo *T = D->getFriendType())
1540 dumpType(T->getType());
1541 else
1542 dumpDecl(D->getFriendDecl());
1543 }
1544
1545 //===----------------------------------------------------------------------===//
1546 // Obj-C Declarations
1547 //===----------------------------------------------------------------------===//
1548
VisitObjCIvarDecl(const ObjCIvarDecl * D)1549 void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
1550 dumpName(D);
1551 dumpType(D->getType());
1552 if (D->getSynthesize())
1553 OS << " synthesize";
1554
1555 switch (D->getAccessControl()) {
1556 case ObjCIvarDecl::None:
1557 OS << " none";
1558 break;
1559 case ObjCIvarDecl::Private:
1560 OS << " private";
1561 break;
1562 case ObjCIvarDecl::Protected:
1563 OS << " protected";
1564 break;
1565 case ObjCIvarDecl::Public:
1566 OS << " public";
1567 break;
1568 case ObjCIvarDecl::Package:
1569 OS << " package";
1570 break;
1571 }
1572 }
1573
VisitObjCMethodDecl(const ObjCMethodDecl * D)1574 void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
1575 if (D->isInstanceMethod())
1576 OS << " -";
1577 else
1578 OS << " +";
1579 dumpName(D);
1580 dumpType(D->getReturnType());
1581
1582 if (D->isThisDeclarationADefinition()) {
1583 dumpDeclContext(D);
1584 } else {
1585 for (const ParmVarDecl *Parameter : D->parameters())
1586 dumpDecl(Parameter);
1587 }
1588
1589 if (D->isVariadic())
1590 dumpChild([=] { OS << "..."; });
1591
1592 if (D->hasBody())
1593 dumpStmt(D->getBody());
1594 }
1595
VisitObjCTypeParamDecl(const ObjCTypeParamDecl * D)1596 void ASTDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) {
1597 dumpName(D);
1598 switch (D->getVariance()) {
1599 case ObjCTypeParamVariance::Invariant:
1600 break;
1601
1602 case ObjCTypeParamVariance::Covariant:
1603 OS << " covariant";
1604 break;
1605
1606 case ObjCTypeParamVariance::Contravariant:
1607 OS << " contravariant";
1608 break;
1609 }
1610
1611 if (D->hasExplicitBound())
1612 OS << " bounded";
1613 dumpType(D->getUnderlyingType());
1614 }
1615
VisitObjCCategoryDecl(const ObjCCategoryDecl * D)1616 void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
1617 dumpName(D);
1618 dumpDeclRef(D->getClassInterface());
1619 dumpObjCTypeParamList(D->getTypeParamList());
1620 dumpDeclRef(D->getImplementation());
1621 for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
1622 E = D->protocol_end();
1623 I != E; ++I)
1624 dumpDeclRef(*I);
1625 }
1626
VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl * D)1627 void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
1628 dumpName(D);
1629 dumpDeclRef(D->getClassInterface());
1630 dumpDeclRef(D->getCategoryDecl());
1631 }
1632
VisitObjCProtocolDecl(const ObjCProtocolDecl * D)1633 void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
1634 dumpName(D);
1635
1636 for (auto *Child : D->protocols())
1637 dumpDeclRef(Child);
1638 }
1639
VisitObjCInterfaceDecl(const ObjCInterfaceDecl * D)1640 void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
1641 dumpName(D);
1642 dumpObjCTypeParamList(D->getTypeParamListAsWritten());
1643 dumpDeclRef(D->getSuperClass(), "super");
1644
1645 dumpDeclRef(D->getImplementation());
1646 for (auto *Child : D->protocols())
1647 dumpDeclRef(Child);
1648 }
1649
VisitObjCImplementationDecl(const ObjCImplementationDecl * D)1650 void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
1651 dumpName(D);
1652 dumpDeclRef(D->getSuperClass(), "super");
1653 dumpDeclRef(D->getClassInterface());
1654 for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
1655 E = D->init_end();
1656 I != E; ++I)
1657 dumpCXXCtorInitializer(*I);
1658 }
1659
VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl * D)1660 void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
1661 dumpName(D);
1662 dumpDeclRef(D->getClassInterface());
1663 }
1664
VisitObjCPropertyDecl(const ObjCPropertyDecl * D)1665 void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
1666 dumpName(D);
1667 dumpType(D->getType());
1668
1669 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1670 OS << " required";
1671 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1672 OS << " optional";
1673
1674 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1675 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1676 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1677 OS << " readonly";
1678 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1679 OS << " assign";
1680 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1681 OS << " readwrite";
1682 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1683 OS << " retain";
1684 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1685 OS << " copy";
1686 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1687 OS << " nonatomic";
1688 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1689 OS << " atomic";
1690 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1691 OS << " weak";
1692 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1693 OS << " strong";
1694 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1695 OS << " unsafe_unretained";
1696 if (Attrs & ObjCPropertyDecl::OBJC_PR_class)
1697 OS << " class";
1698 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
1699 dumpDeclRef(D->getGetterMethodDecl(), "getter");
1700 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
1701 dumpDeclRef(D->getSetterMethodDecl(), "setter");
1702 }
1703 }
1704
VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl * D)1705 void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
1706 dumpName(D->getPropertyDecl());
1707 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1708 OS << " synthesize";
1709 else
1710 OS << " dynamic";
1711 dumpDeclRef(D->getPropertyDecl());
1712 dumpDeclRef(D->getPropertyIvarDecl());
1713 }
1714
VisitBlockDecl(const BlockDecl * D)1715 void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
1716 for (auto I : D->parameters())
1717 dumpDecl(I);
1718
1719 if (D->isVariadic())
1720 dumpChild([=]{ OS << "..."; });
1721
1722 if (D->capturesCXXThis())
1723 dumpChild([=]{ OS << "capture this"; });
1724
1725 for (const auto &I : D->captures()) {
1726 dumpChild([=] {
1727 OS << "capture";
1728 if (I.isByRef())
1729 OS << " byref";
1730 if (I.isNested())
1731 OS << " nested";
1732 if (I.getVariable()) {
1733 OS << ' ';
1734 dumpBareDeclRef(I.getVariable());
1735 }
1736 if (I.hasCopyExpr())
1737 dumpStmt(I.getCopyExpr());
1738 });
1739 }
1740 dumpStmt(D->getBody());
1741 }
1742
1743 //===----------------------------------------------------------------------===//
1744 // Stmt dumping methods.
1745 //===----------------------------------------------------------------------===//
1746
dumpStmt(const Stmt * S)1747 void ASTDumper::dumpStmt(const Stmt *S) {
1748 dumpChild([=] {
1749 if (!S) {
1750 ColorScope Color(*this, NullColor);
1751 OS << "<<<NULL>>>";
1752 return;
1753 }
1754
1755 if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
1756 VisitDeclStmt(DS);
1757 return;
1758 }
1759
1760 ConstStmtVisitor<ASTDumper>::Visit(S);
1761
1762 for (const Stmt *SubStmt : S->children())
1763 dumpStmt(SubStmt);
1764 });
1765 }
1766
VisitStmt(const Stmt * Node)1767 void ASTDumper::VisitStmt(const Stmt *Node) {
1768 {
1769 ColorScope Color(*this, StmtColor);
1770 OS << Node->getStmtClassName();
1771 }
1772 dumpPointer(Node);
1773 dumpSourceRange(Node->getSourceRange());
1774 }
1775
VisitDeclStmt(const DeclStmt * Node)1776 void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
1777 VisitStmt(Node);
1778 for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
1779 E = Node->decl_end();
1780 I != E; ++I)
1781 dumpDecl(*I);
1782 }
1783
VisitAttributedStmt(const AttributedStmt * Node)1784 void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
1785 VisitStmt(Node);
1786 for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
1787 E = Node->getAttrs().end();
1788 I != E; ++I)
1789 dumpAttr(*I);
1790 }
1791
VisitLabelStmt(const LabelStmt * Node)1792 void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
1793 VisitStmt(Node);
1794 OS << " '" << Node->getName() << "'";
1795 }
1796
VisitGotoStmt(const GotoStmt * Node)1797 void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
1798 VisitStmt(Node);
1799 OS << " '" << Node->getLabel()->getName() << "'";
1800 dumpPointer(Node->getLabel());
1801 }
1802
VisitCXXCatchStmt(const CXXCatchStmt * Node)1803 void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
1804 VisitStmt(Node);
1805 dumpDecl(Node->getExceptionDecl());
1806 }
1807
VisitCapturedStmt(const CapturedStmt * Node)1808 void ASTDumper::VisitCapturedStmt(const CapturedStmt *Node) {
1809 VisitStmt(Node);
1810 dumpDecl(Node->getCapturedDecl());
1811 }
1812
1813 //===----------------------------------------------------------------------===//
1814 // OpenMP dumping methods.
1815 //===----------------------------------------------------------------------===//
1816
VisitOMPExecutableDirective(const OMPExecutableDirective * Node)1817 void ASTDumper::VisitOMPExecutableDirective(
1818 const OMPExecutableDirective *Node) {
1819 VisitStmt(Node);
1820 for (auto *C : Node->clauses()) {
1821 dumpChild([=] {
1822 if (!C) {
1823 ColorScope Color(*this, NullColor);
1824 OS << "<<<NULL>>> OMPClause";
1825 return;
1826 }
1827 {
1828 ColorScope Color(*this, AttrColor);
1829 StringRef ClauseName(getOpenMPClauseName(C->getClauseKind()));
1830 OS << "OMP" << ClauseName.substr(/*Start=*/0, /*N=*/1).upper()
1831 << ClauseName.drop_front() << "Clause";
1832 }
1833 dumpPointer(C);
1834 dumpSourceRange(SourceRange(C->getLocStart(), C->getLocEnd()));
1835 if (C->isImplicit())
1836 OS << " <implicit>";
1837 for (auto *S : C->children())
1838 dumpStmt(S);
1839 });
1840 }
1841 }
1842
1843 //===----------------------------------------------------------------------===//
1844 // Expr dumping methods.
1845 //===----------------------------------------------------------------------===//
1846
VisitExpr(const Expr * Node)1847 void ASTDumper::VisitExpr(const Expr *Node) {
1848 VisitStmt(Node);
1849 dumpType(Node->getType());
1850
1851 {
1852 ColorScope Color(*this, ValueKindColor);
1853 switch (Node->getValueKind()) {
1854 case VK_RValue:
1855 break;
1856 case VK_LValue:
1857 OS << " lvalue";
1858 break;
1859 case VK_XValue:
1860 OS << " xvalue";
1861 break;
1862 }
1863 }
1864
1865 {
1866 ColorScope Color(*this, ObjectKindColor);
1867 switch (Node->getObjectKind()) {
1868 case OK_Ordinary:
1869 break;
1870 case OK_BitField:
1871 OS << " bitfield";
1872 break;
1873 case OK_ObjCProperty:
1874 OS << " objcproperty";
1875 break;
1876 case OK_ObjCSubscript:
1877 OS << " objcsubscript";
1878 break;
1879 case OK_VectorComponent:
1880 OS << " vectorcomponent";
1881 break;
1882 }
1883 }
1884 }
1885
dumpBasePath(raw_ostream & OS,const CastExpr * Node)1886 static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
1887 if (Node->path_empty())
1888 return;
1889
1890 OS << " (";
1891 bool First = true;
1892 for (CastExpr::path_const_iterator I = Node->path_begin(),
1893 E = Node->path_end();
1894 I != E; ++I) {
1895 const CXXBaseSpecifier *Base = *I;
1896 if (!First)
1897 OS << " -> ";
1898
1899 const CXXRecordDecl *RD =
1900 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1901
1902 if (Base->isVirtual())
1903 OS << "virtual ";
1904 OS << RD->getName();
1905 First = false;
1906 }
1907
1908 OS << ')';
1909 }
1910
VisitCastExpr(const CastExpr * Node)1911 void ASTDumper::VisitCastExpr(const CastExpr *Node) {
1912 VisitExpr(Node);
1913 OS << " <";
1914 {
1915 ColorScope Color(*this, CastColor);
1916 OS << Node->getCastKindName();
1917 }
1918 dumpBasePath(OS, Node);
1919 OS << ">";
1920 }
1921
VisitDeclRefExpr(const DeclRefExpr * Node)1922 void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
1923 VisitExpr(Node);
1924
1925 OS << " ";
1926 dumpBareDeclRef(Node->getDecl());
1927 if (Node->getDecl() != Node->getFoundDecl()) {
1928 OS << " (";
1929 dumpBareDeclRef(Node->getFoundDecl());
1930 OS << ")";
1931 }
1932 }
1933
VisitUnresolvedLookupExpr(const UnresolvedLookupExpr * Node)1934 void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
1935 VisitExpr(Node);
1936 OS << " (";
1937 if (!Node->requiresADL())
1938 OS << "no ";
1939 OS << "ADL) = '" << Node->getName() << '\'';
1940
1941 UnresolvedLookupExpr::decls_iterator
1942 I = Node->decls_begin(), E = Node->decls_end();
1943 if (I == E)
1944 OS << " empty";
1945 for (; I != E; ++I)
1946 dumpPointer(*I);
1947 }
1948
VisitObjCIvarRefExpr(const ObjCIvarRefExpr * Node)1949 void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
1950 VisitExpr(Node);
1951
1952 {
1953 ColorScope Color(*this, DeclKindNameColor);
1954 OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
1955 }
1956 OS << "='" << *Node->getDecl() << "'";
1957 dumpPointer(Node->getDecl());
1958 if (Node->isFreeIvar())
1959 OS << " isFreeIvar";
1960 }
1961
VisitPredefinedExpr(const PredefinedExpr * Node)1962 void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
1963 VisitExpr(Node);
1964 OS << " " << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1965 }
1966
VisitCharacterLiteral(const CharacterLiteral * Node)1967 void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
1968 VisitExpr(Node);
1969 ColorScope Color(*this, ValueColor);
1970 OS << " " << Node->getValue();
1971 }
1972
VisitIntegerLiteral(const IntegerLiteral * Node)1973 void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
1974 VisitExpr(Node);
1975
1976 bool isSigned = Node->getType()->isSignedIntegerType();
1977 ColorScope Color(*this, ValueColor);
1978 OS << " " << Node->getValue().toString(10, isSigned);
1979 }
1980
VisitFloatingLiteral(const FloatingLiteral * Node)1981 void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
1982 VisitExpr(Node);
1983 ColorScope Color(*this, ValueColor);
1984 OS << " " << Node->getValueAsApproximateDouble();
1985 }
1986
VisitStringLiteral(const StringLiteral * Str)1987 void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
1988 VisitExpr(Str);
1989 ColorScope Color(*this, ValueColor);
1990 OS << " ";
1991 Str->outputString(OS);
1992 }
1993
VisitInitListExpr(const InitListExpr * ILE)1994 void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
1995 VisitExpr(ILE);
1996 if (auto *Filler = ILE->getArrayFiller()) {
1997 dumpChild([=] {
1998 OS << "array filler";
1999 dumpStmt(Filler);
2000 });
2001 }
2002 if (auto *Field = ILE->getInitializedFieldInUnion()) {
2003 OS << " field ";
2004 dumpBareDeclRef(Field);
2005 }
2006 }
2007
VisitUnaryOperator(const UnaryOperator * Node)2008 void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
2009 VisitExpr(Node);
2010 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
2011 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
2012 }
2013
VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr * Node)2014 void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
2015 const UnaryExprOrTypeTraitExpr *Node) {
2016 VisitExpr(Node);
2017 switch(Node->getKind()) {
2018 case UETT_SizeOf:
2019 OS << " sizeof";
2020 break;
2021 case UETT_AlignOf:
2022 OS << " alignof";
2023 break;
2024 case UETT_VecStep:
2025 OS << " vec_step";
2026 break;
2027 case UETT_OpenMPRequiredSimdAlign:
2028 OS << " __builtin_omp_required_simd_align";
2029 break;
2030 }
2031 if (Node->isArgumentType())
2032 dumpType(Node->getArgumentType());
2033 }
2034
VisitMemberExpr(const MemberExpr * Node)2035 void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
2036 VisitExpr(Node);
2037 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
2038 dumpPointer(Node->getMemberDecl());
2039 }
2040
VisitExtVectorElementExpr(const ExtVectorElementExpr * Node)2041 void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
2042 VisitExpr(Node);
2043 OS << " " << Node->getAccessor().getNameStart();
2044 }
2045
VisitBinaryOperator(const BinaryOperator * Node)2046 void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
2047 VisitExpr(Node);
2048 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
2049 }
2050
VisitCompoundAssignOperator(const CompoundAssignOperator * Node)2051 void ASTDumper::VisitCompoundAssignOperator(
2052 const CompoundAssignOperator *Node) {
2053 VisitExpr(Node);
2054 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
2055 << "' ComputeLHSTy=";
2056 dumpBareType(Node->getComputationLHSType());
2057 OS << " ComputeResultTy=";
2058 dumpBareType(Node->getComputationResultType());
2059 }
2060
VisitBlockExpr(const BlockExpr * Node)2061 void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
2062 VisitExpr(Node);
2063 dumpDecl(Node->getBlockDecl());
2064 }
2065
VisitOpaqueValueExpr(const OpaqueValueExpr * Node)2066 void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
2067 VisitExpr(Node);
2068
2069 if (Expr *Source = Node->getSourceExpr())
2070 dumpStmt(Source);
2071 }
2072
2073 // GNU extensions.
2074
VisitAddrLabelExpr(const AddrLabelExpr * Node)2075 void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
2076 VisitExpr(Node);
2077 OS << " " << Node->getLabel()->getName();
2078 dumpPointer(Node->getLabel());
2079 }
2080
2081 //===----------------------------------------------------------------------===//
2082 // C++ Expressions
2083 //===----------------------------------------------------------------------===//
2084
VisitCXXNamedCastExpr(const CXXNamedCastExpr * Node)2085 void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
2086 VisitExpr(Node);
2087 OS << " " << Node->getCastName()
2088 << "<" << Node->getTypeAsWritten().getAsString() << ">"
2089 << " <" << Node->getCastKindName();
2090 dumpBasePath(OS, Node);
2091 OS << ">";
2092 }
2093
VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr * Node)2094 void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
2095 VisitExpr(Node);
2096 OS << " " << (Node->getValue() ? "true" : "false");
2097 }
2098
VisitCXXThisExpr(const CXXThisExpr * Node)2099 void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
2100 VisitExpr(Node);
2101 OS << " this";
2102 }
2103
VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr * Node)2104 void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
2105 VisitExpr(Node);
2106 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
2107 << " <" << Node->getCastKindName() << ">";
2108 }
2109
VisitCXXConstructExpr(const CXXConstructExpr * Node)2110 void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
2111 VisitExpr(Node);
2112 CXXConstructorDecl *Ctor = Node->getConstructor();
2113 dumpType(Ctor->getType());
2114 if (Node->isElidable())
2115 OS << " elidable";
2116 if (Node->requiresZeroInitialization())
2117 OS << " zeroing";
2118 }
2119
VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr * Node)2120 void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
2121 VisitExpr(Node);
2122 OS << " ";
2123 dumpCXXTemporary(Node->getTemporary());
2124 }
2125
VisitCXXNewExpr(const CXXNewExpr * Node)2126 void ASTDumper::VisitCXXNewExpr(const CXXNewExpr *Node) {
2127 VisitExpr(Node);
2128 if (Node->isGlobalNew())
2129 OS << " global";
2130 if (Node->isArray())
2131 OS << " array";
2132 if (Node->getOperatorNew()) {
2133 OS << ' ';
2134 dumpBareDeclRef(Node->getOperatorNew());
2135 }
2136 // We could dump the deallocation function used in case of error, but it's
2137 // usually not that interesting.
2138 }
2139
VisitCXXDeleteExpr(const CXXDeleteExpr * Node)2140 void ASTDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *Node) {
2141 VisitExpr(Node);
2142 if (Node->isGlobalDelete())
2143 OS << " global";
2144 if (Node->isArrayForm())
2145 OS << " array";
2146 if (Node->getOperatorDelete()) {
2147 OS << ' ';
2148 dumpBareDeclRef(Node->getOperatorDelete());
2149 }
2150 }
2151
2152 void
VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr * Node)2153 ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
2154 VisitExpr(Node);
2155 if (const ValueDecl *VD = Node->getExtendingDecl()) {
2156 OS << " extended by ";
2157 dumpBareDeclRef(VD);
2158 }
2159 }
2160
VisitExprWithCleanups(const ExprWithCleanups * Node)2161 void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
2162 VisitExpr(Node);
2163 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
2164 dumpDeclRef(Node->getObject(i), "cleanup");
2165 }
2166
dumpCXXTemporary(const CXXTemporary * Temporary)2167 void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
2168 OS << "(CXXTemporary";
2169 dumpPointer(Temporary);
2170 OS << ")";
2171 }
2172
VisitSizeOfPackExpr(const SizeOfPackExpr * Node)2173 void ASTDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *Node) {
2174 VisitExpr(Node);
2175 dumpPointer(Node->getPack());
2176 dumpName(Node->getPack());
2177 if (Node->isPartiallySubstituted())
2178 for (const auto &A : Node->getPartialArguments())
2179 dumpTemplateArgument(A);
2180 }
2181
2182
2183 //===----------------------------------------------------------------------===//
2184 // Obj-C Expressions
2185 //===----------------------------------------------------------------------===//
2186
VisitObjCMessageExpr(const ObjCMessageExpr * Node)2187 void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
2188 VisitExpr(Node);
2189 OS << " selector=";
2190 Node->getSelector().print(OS);
2191 switch (Node->getReceiverKind()) {
2192 case ObjCMessageExpr::Instance:
2193 break;
2194
2195 case ObjCMessageExpr::Class:
2196 OS << " class=";
2197 dumpBareType(Node->getClassReceiver());
2198 break;
2199
2200 case ObjCMessageExpr::SuperInstance:
2201 OS << " super (instance)";
2202 break;
2203
2204 case ObjCMessageExpr::SuperClass:
2205 OS << " super (class)";
2206 break;
2207 }
2208 }
2209
VisitObjCBoxedExpr(const ObjCBoxedExpr * Node)2210 void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
2211 VisitExpr(Node);
2212 if (auto *BoxingMethod = Node->getBoxingMethod()) {
2213 OS << " selector=";
2214 BoxingMethod->getSelector().print(OS);
2215 }
2216 }
2217
VisitObjCAtCatchStmt(const ObjCAtCatchStmt * Node)2218 void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
2219 VisitStmt(Node);
2220 if (const VarDecl *CatchParam = Node->getCatchParamDecl())
2221 dumpDecl(CatchParam);
2222 else
2223 OS << " catch all";
2224 }
2225
VisitObjCEncodeExpr(const ObjCEncodeExpr * Node)2226 void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
2227 VisitExpr(Node);
2228 dumpType(Node->getEncodedType());
2229 }
2230
VisitObjCSelectorExpr(const ObjCSelectorExpr * Node)2231 void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
2232 VisitExpr(Node);
2233
2234 OS << " ";
2235 Node->getSelector().print(OS);
2236 }
2237
VisitObjCProtocolExpr(const ObjCProtocolExpr * Node)2238 void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
2239 VisitExpr(Node);
2240
2241 OS << ' ' << *Node->getProtocol();
2242 }
2243
VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr * Node)2244 void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
2245 VisitExpr(Node);
2246 if (Node->isImplicitProperty()) {
2247 OS << " Kind=MethodRef Getter=\"";
2248 if (Node->getImplicitPropertyGetter())
2249 Node->getImplicitPropertyGetter()->getSelector().print(OS);
2250 else
2251 OS << "(null)";
2252
2253 OS << "\" Setter=\"";
2254 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
2255 Setter->getSelector().print(OS);
2256 else
2257 OS << "(null)";
2258 OS << "\"";
2259 } else {
2260 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
2261 }
2262
2263 if (Node->isSuperReceiver())
2264 OS << " super";
2265
2266 OS << " Messaging=";
2267 if (Node->isMessagingGetter() && Node->isMessagingSetter())
2268 OS << "Getter&Setter";
2269 else if (Node->isMessagingGetter())
2270 OS << "Getter";
2271 else if (Node->isMessagingSetter())
2272 OS << "Setter";
2273 }
2274
VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr * Node)2275 void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
2276 VisitExpr(Node);
2277 if (Node->isArraySubscriptRefExpr())
2278 OS << " Kind=ArraySubscript GetterForArray=\"";
2279 else
2280 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
2281 if (Node->getAtIndexMethodDecl())
2282 Node->getAtIndexMethodDecl()->getSelector().print(OS);
2283 else
2284 OS << "(null)";
2285
2286 if (Node->isArraySubscriptRefExpr())
2287 OS << "\" SetterForArray=\"";
2288 else
2289 OS << "\" SetterForDictionary=\"";
2290 if (Node->setAtIndexMethodDecl())
2291 Node->setAtIndexMethodDecl()->getSelector().print(OS);
2292 else
2293 OS << "(null)";
2294 }
2295
VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr * Node)2296 void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
2297 VisitExpr(Node);
2298 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
2299 }
2300
2301 //===----------------------------------------------------------------------===//
2302 // Comments
2303 //===----------------------------------------------------------------------===//
2304
getCommandName(unsigned CommandID)2305 const char *ASTDumper::getCommandName(unsigned CommandID) {
2306 if (Traits)
2307 return Traits->getCommandInfo(CommandID)->Name;
2308 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
2309 if (Info)
2310 return Info->Name;
2311 return "<not a builtin command>";
2312 }
2313
dumpFullComment(const FullComment * C)2314 void ASTDumper::dumpFullComment(const FullComment *C) {
2315 if (!C)
2316 return;
2317
2318 FC = C;
2319 dumpComment(C);
2320 FC = nullptr;
2321 }
2322
dumpComment(const Comment * C)2323 void ASTDumper::dumpComment(const Comment *C) {
2324 dumpChild([=] {
2325 if (!C) {
2326 ColorScope Color(*this, NullColor);
2327 OS << "<<<NULL>>>";
2328 return;
2329 }
2330
2331 {
2332 ColorScope Color(*this, CommentColor);
2333 OS << C->getCommentKindName();
2334 }
2335 dumpPointer(C);
2336 dumpSourceRange(C->getSourceRange());
2337 ConstCommentVisitor<ASTDumper>::visit(C);
2338 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
2339 I != E; ++I)
2340 dumpComment(*I);
2341 });
2342 }
2343
visitTextComment(const TextComment * C)2344 void ASTDumper::visitTextComment(const TextComment *C) {
2345 OS << " Text=\"" << C->getText() << "\"";
2346 }
2347
visitInlineCommandComment(const InlineCommandComment * C)2348 void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
2349 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2350 switch (C->getRenderKind()) {
2351 case InlineCommandComment::RenderNormal:
2352 OS << " RenderNormal";
2353 break;
2354 case InlineCommandComment::RenderBold:
2355 OS << " RenderBold";
2356 break;
2357 case InlineCommandComment::RenderMonospaced:
2358 OS << " RenderMonospaced";
2359 break;
2360 case InlineCommandComment::RenderEmphasized:
2361 OS << " RenderEmphasized";
2362 break;
2363 }
2364
2365 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2366 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2367 }
2368
visitHTMLStartTagComment(const HTMLStartTagComment * C)2369 void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
2370 OS << " Name=\"" << C->getTagName() << "\"";
2371 if (C->getNumAttrs() != 0) {
2372 OS << " Attrs: ";
2373 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
2374 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
2375 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
2376 }
2377 }
2378 if (C->isSelfClosing())
2379 OS << " SelfClosing";
2380 }
2381
visitHTMLEndTagComment(const HTMLEndTagComment * C)2382 void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
2383 OS << " Name=\"" << C->getTagName() << "\"";
2384 }
2385
visitBlockCommandComment(const BlockCommandComment * C)2386 void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
2387 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2388 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2389 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2390 }
2391
visitParamCommandComment(const ParamCommandComment * C)2392 void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
2393 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
2394
2395 if (C->isDirectionExplicit())
2396 OS << " explicitly";
2397 else
2398 OS << " implicitly";
2399
2400 if (C->hasParamName()) {
2401 if (C->isParamIndexValid())
2402 OS << " Param=\"" << C->getParamName(FC) << "\"";
2403 else
2404 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2405 }
2406
2407 if (C->isParamIndexValid() && !C->isVarArgParam())
2408 OS << " ParamIndex=" << C->getParamIndex();
2409 }
2410
visitTParamCommandComment(const TParamCommandComment * C)2411 void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
2412 if (C->hasParamName()) {
2413 if (C->isPositionValid())
2414 OS << " Param=\"" << C->getParamName(FC) << "\"";
2415 else
2416 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2417 }
2418
2419 if (C->isPositionValid()) {
2420 OS << " Position=<";
2421 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
2422 OS << C->getIndex(i);
2423 if (i != e - 1)
2424 OS << ", ";
2425 }
2426 OS << ">";
2427 }
2428 }
2429
visitVerbatimBlockComment(const VerbatimBlockComment * C)2430 void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
2431 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
2432 " CloseName=\"" << C->getCloseName() << "\"";
2433 }
2434
visitVerbatimBlockLineComment(const VerbatimBlockLineComment * C)2435 void ASTDumper::visitVerbatimBlockLineComment(
2436 const VerbatimBlockLineComment *C) {
2437 OS << " Text=\"" << C->getText() << "\"";
2438 }
2439
visitVerbatimLineComment(const VerbatimLineComment * C)2440 void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
2441 OS << " Text=\"" << C->getText() << "\"";
2442 }
2443
2444 //===----------------------------------------------------------------------===//
2445 // Type method implementations
2446 //===----------------------------------------------------------------------===//
2447
dump(const char * msg) const2448 void QualType::dump(const char *msg) const {
2449 if (msg)
2450 llvm::errs() << msg << ": ";
2451 dump();
2452 }
2453
dump() const2454 LLVM_DUMP_METHOD void QualType::dump() const {
2455 ASTDumper Dumper(llvm::errs(), nullptr, nullptr);
2456 Dumper.dumpTypeAsChild(*this);
2457 }
2458
dump() const2459 LLVM_DUMP_METHOD void Type::dump() const { QualType(this, 0).dump(); }
2460
2461 //===----------------------------------------------------------------------===//
2462 // Decl method implementations
2463 //===----------------------------------------------------------------------===//
2464
dump() const2465 LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
2466
dump(raw_ostream & OS) const2467 LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS) const {
2468 ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
2469 &getASTContext().getSourceManager());
2470 P.dumpDecl(this);
2471 }
2472
dumpColor() const2473 LLVM_DUMP_METHOD void Decl::dumpColor() const {
2474 ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
2475 &getASTContext().getSourceManager(), /*ShowColors*/true);
2476 P.dumpDecl(this);
2477 }
2478
dumpLookups() const2479 LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
2480 dumpLookups(llvm::errs());
2481 }
2482
dumpLookups(raw_ostream & OS,bool DumpDecls) const2483 LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
2484 bool DumpDecls) const {
2485 const DeclContext *DC = this;
2486 while (!DC->isTranslationUnit())
2487 DC = DC->getParent();
2488 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
2489 ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
2490 P.dumpLookups(this, DumpDecls);
2491 }
2492
2493 //===----------------------------------------------------------------------===//
2494 // Stmt method implementations
2495 //===----------------------------------------------------------------------===//
2496
dump(SourceManager & SM) const2497 LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
2498 dump(llvm::errs(), SM);
2499 }
2500
dump(raw_ostream & OS,SourceManager & SM) const2501 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
2502 ASTDumper P(OS, nullptr, &SM);
2503 P.dumpStmt(this);
2504 }
2505
dump(raw_ostream & OS) const2506 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS) const {
2507 ASTDumper P(OS, nullptr, nullptr);
2508 P.dumpStmt(this);
2509 }
2510
dump() const2511 LLVM_DUMP_METHOD void Stmt::dump() const {
2512 ASTDumper P(llvm::errs(), nullptr, nullptr);
2513 P.dumpStmt(this);
2514 }
2515
dumpColor() const2516 LLVM_DUMP_METHOD void Stmt::dumpColor() const {
2517 ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
2518 P.dumpStmt(this);
2519 }
2520
2521 //===----------------------------------------------------------------------===//
2522 // Comment method implementations
2523 //===----------------------------------------------------------------------===//
2524
dump() const2525 LLVM_DUMP_METHOD void Comment::dump() const {
2526 dump(llvm::errs(), nullptr, nullptr);
2527 }
2528
dump(const ASTContext & Context) const2529 LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
2530 dump(llvm::errs(), &Context.getCommentCommandTraits(),
2531 &Context.getSourceManager());
2532 }
2533
dump(raw_ostream & OS,const CommandTraits * Traits,const SourceManager * SM) const2534 void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
2535 const SourceManager *SM) const {
2536 const FullComment *FC = dyn_cast<FullComment>(this);
2537 ASTDumper D(OS, Traits, SM);
2538 D.dumpFullComment(FC);
2539 }
2540
dumpColor() const2541 LLVM_DUMP_METHOD void Comment::dumpColor() const {
2542 const FullComment *FC = dyn_cast<FullComment>(this);
2543 ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
2544 D.dumpFullComment(FC);
2545 }
2546