1 //===--- ASTDumper.cpp - Dumping implementation for ASTs ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AST dump methods, which dump out the
11 // AST in a form that exposes type details and other fields.
13 //===----------------------------------------------------------------------===//
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/DeclVisitor.h"
22 #include "clang/AST/StmtVisitor.h"
23 #include "clang/Basic/Module.h"
24 #include "clang/Basic/SourceManager.h"
25 #include "llvm/Support/raw_ostream.h"
26 using namespace clang;
27 using namespace clang::comments;
29 //===----------------------------------------------------------------------===//
31 //===----------------------------------------------------------------------===//
34 // Colors used for various parts of the AST dump
35 // Do not use bold yellow for any text. It is hard to read on white screens.
37 struct TerminalColor {
38 raw_ostream::Colors Color;
44 // Bold Green - DeclKindNameColor, UndeserializedColor
45 // Yellow - AddressColor, LocationColor
46 // Blue - CommentColor, NullColor, IndentColor
47 // Bold Blue - AttrColor
48 // Bold Magenta - StmtColor
49 // Cyan - ValueKindColor, ObjectKindColor
50 // Bold Cyan - ValueColor, DeclNameColor
52 // Decl kind names (VarDecl, FunctionDecl, etc)
53 static const TerminalColor DeclKindNameColor = { raw_ostream::GREEN, true };
54 // Attr names (CleanupAttr, GuardedByAttr, etc)
55 static const TerminalColor AttrColor = { raw_ostream::BLUE, true };
56 // Statement names (DeclStmt, ImplicitCastExpr, etc)
57 static const TerminalColor StmtColor = { raw_ostream::MAGENTA, true };
58 // Comment names (FullComment, ParagraphComment, TextComment, etc)
59 static const TerminalColor CommentColor = { raw_ostream::BLUE, false };
61 // Type names (int, float, etc, plus user defined types)
62 static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
65 static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
67 static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
70 static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
71 // bitfield/objcproperty/objcsubscript/vectorcomponent
72 static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
75 static const TerminalColor NullColor = { raw_ostream::BLUE, false };
77 // Undeserialized entities
78 static const TerminalColor UndeserializedColor = { raw_ostream::GREEN, true };
80 // CastKind from CastExpr's
81 static const TerminalColor CastColor = { raw_ostream::RED, false };
83 // Value of the statement
84 static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
86 static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
88 // Indents ( `, -. | )
89 static const TerminalColor IndentColor = { raw_ostream::BLUE, false };
92 : public ConstDeclVisitor<ASTDumper>, public ConstStmtVisitor<ASTDumper>,
93 public ConstCommentVisitor<ASTDumper> {
95 const CommandTraits *Traits;
96 const SourceManager *SM;
99 // Indicates whether more child are expected at the current tree depth
100 enum IndentType { IT_Child, IT_LastChild };
102 /// Indents[i] indicates if another child exists at level i.
103 /// Used by Indent() to print the tree structure.
104 llvm::SmallVector<IndentType, 32> Indents;
106 /// Indicates that more children will be needed at this indent level.
107 /// If true, prevents lastChild() from marking the node as the last child.
108 /// This is used when there are multiple collections of children to be
109 /// dumped as well as during conditional node dumping.
112 /// Keep track of the last location we print out so that we can
113 /// print out deltas from then on out.
114 const char *LastLocFilename;
115 unsigned LastLocLine;
117 /// The \c FullComment parent of the comment being dumped.
118 const FullComment *FC;
124 // Preserve the Dumper's MoreChildren value from the previous IndentScope
127 IndentScope(ASTDumper &Dumper) : Dumper(Dumper) {
128 MoreChildren = Dumper.hasMoreChildren();
129 Dumper.setMoreChildren(false);
133 Dumper.setMoreChildren(MoreChildren);
141 ColorScope(ASTDumper &Dumper, TerminalColor Color)
143 if (Dumper.ShowColors)
144 Dumper.OS.changeColor(Color.Color, Color.Bold);
147 if (Dumper.ShowColors)
148 Dumper.OS.resetColor();
158 ChildDumper(ASTDumper &Dumper) : Dumper(Dumper), Prev(nullptr) {}
166 // FIXME: This should take an arbitrary callable as the dumping action.
167 void dump(const Decl *D, bool Ref = false) {
170 Dumper.dumpDeclRef(Prev);
172 Dumper.dumpDecl(Prev);
177 void dumpRef(const Decl *D) { dump(D, true); }
179 // Give up ownership of the children of the node. By calling this,
180 // the caller takes back responsibility for calling lastChild().
181 void release() { dump(nullptr); }
185 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
186 const SourceManager *SM)
187 : OS(OS), Traits(Traits), SM(SM), IsFirstLine(true), MoreChildren(false),
188 LastLocFilename(""), LastLocLine(~0U), FC(nullptr),
189 ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
191 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
192 const SourceManager *SM, bool ShowColors)
193 : OS(OS), Traits(Traits), SM(SM), IsFirstLine(true), MoreChildren(false),
194 LastLocFilename(""), LastLocLine(~0U),
195 ShowColors(ShowColors) { }
201 void dumpDecl(const Decl *D);
202 void dumpStmt(const Stmt *S);
203 void dumpFullComment(const FullComment *C);
209 bool hasMoreChildren();
210 void setMoreChildren(bool Value);
213 void dumpPointer(const void *Ptr);
214 void dumpSourceRange(SourceRange R);
215 void dumpLocation(SourceLocation Loc);
216 void dumpBareType(QualType T);
217 void dumpType(QualType T);
218 void dumpBareDeclRef(const Decl *Node);
219 void dumpDeclRef(const Decl *Node, const char *Label = nullptr);
220 void dumpName(const NamedDecl *D);
221 bool hasNodes(const DeclContext *DC);
222 void dumpDeclContext(const DeclContext *DC);
223 void dumpLookups(const DeclContext *DC);
224 void dumpAttr(const Attr *A);
227 void dumpAccessSpecifier(AccessSpecifier AS);
228 void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
229 void dumpTemplateParameters(const TemplateParameterList *TPL);
230 void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
231 void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
232 void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
233 void dumpTemplateArgument(const TemplateArgument &A,
234 SourceRange R = SourceRange());
237 void VisitLabelDecl(const LabelDecl *D);
238 void VisitTypedefDecl(const TypedefDecl *D);
239 void VisitEnumDecl(const EnumDecl *D);
240 void VisitRecordDecl(const RecordDecl *D);
241 void VisitEnumConstantDecl(const EnumConstantDecl *D);
242 void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
243 void VisitFunctionDecl(const FunctionDecl *D);
244 void VisitFieldDecl(const FieldDecl *D);
245 void VisitVarDecl(const VarDecl *D);
246 void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D);
247 void VisitImportDecl(const ImportDecl *D);
250 void VisitNamespaceDecl(const NamespaceDecl *D);
251 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
252 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
253 void VisitTypeAliasDecl(const TypeAliasDecl *D);
254 void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
255 void VisitCXXRecordDecl(const CXXRecordDecl *D);
256 void VisitStaticAssertDecl(const StaticAssertDecl *D);
257 template<typename SpecializationDecl>
258 void VisitTemplateDeclSpecialization(ChildDumper &Children,
259 const SpecializationDecl *D,
260 bool DumpExplicitInst,
262 template<typename TemplateDecl>
263 void VisitTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst);
264 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
265 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
266 void VisitClassTemplateSpecializationDecl(
267 const ClassTemplateSpecializationDecl *D);
268 void VisitClassTemplatePartialSpecializationDecl(
269 const ClassTemplatePartialSpecializationDecl *D);
270 void VisitClassScopeFunctionSpecializationDecl(
271 const ClassScopeFunctionSpecializationDecl *D);
272 void VisitVarTemplateDecl(const VarTemplateDecl *D);
273 void VisitVarTemplateSpecializationDecl(
274 const VarTemplateSpecializationDecl *D);
275 void VisitVarTemplatePartialSpecializationDecl(
276 const VarTemplatePartialSpecializationDecl *D);
277 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
278 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
279 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
280 void VisitUsingDecl(const UsingDecl *D);
281 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
282 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
283 void VisitUsingShadowDecl(const UsingShadowDecl *D);
284 void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
285 void VisitAccessSpecDecl(const AccessSpecDecl *D);
286 void VisitFriendDecl(const FriendDecl *D);
289 void VisitObjCIvarDecl(const ObjCIvarDecl *D);
290 void VisitObjCMethodDecl(const ObjCMethodDecl *D);
291 void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
292 void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
293 void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
294 void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
295 void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
296 void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
297 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
298 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
299 void VisitBlockDecl(const BlockDecl *D);
302 void VisitStmt(const Stmt *Node);
303 void VisitDeclStmt(const DeclStmt *Node);
304 void VisitAttributedStmt(const AttributedStmt *Node);
305 void VisitLabelStmt(const LabelStmt *Node);
306 void VisitGotoStmt(const GotoStmt *Node);
307 void VisitCXXCatchStmt(const CXXCatchStmt *Node);
310 void VisitExpr(const Expr *Node);
311 void VisitCastExpr(const CastExpr *Node);
312 void VisitDeclRefExpr(const DeclRefExpr *Node);
313 void VisitPredefinedExpr(const PredefinedExpr *Node);
314 void VisitCharacterLiteral(const CharacterLiteral *Node);
315 void VisitIntegerLiteral(const IntegerLiteral *Node);
316 void VisitFloatingLiteral(const FloatingLiteral *Node);
317 void VisitStringLiteral(const StringLiteral *Str);
318 void VisitInitListExpr(const InitListExpr *ILE);
319 void VisitUnaryOperator(const UnaryOperator *Node);
320 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
321 void VisitMemberExpr(const MemberExpr *Node);
322 void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
323 void VisitBinaryOperator(const BinaryOperator *Node);
324 void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
325 void VisitAddrLabelExpr(const AddrLabelExpr *Node);
326 void VisitBlockExpr(const BlockExpr *Node);
327 void VisitOpaqueValueExpr(const OpaqueValueExpr *Node);
330 void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
331 void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
332 void VisitCXXThisExpr(const CXXThisExpr *Node);
333 void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
334 void VisitCXXConstructExpr(const CXXConstructExpr *Node);
335 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
336 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
337 void VisitExprWithCleanups(const ExprWithCleanups *Node);
338 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
339 void dumpCXXTemporary(const CXXTemporary *Temporary);
340 void VisitLambdaExpr(const LambdaExpr *Node) {
342 dumpDecl(Node->getLambdaClass());
346 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
347 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
348 void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
349 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
350 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
351 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
352 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
353 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
354 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
355 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
358 const char *getCommandName(unsigned CommandID);
359 void dumpComment(const Comment *C);
362 void visitTextComment(const TextComment *C);
363 void visitInlineCommandComment(const InlineCommandComment *C);
364 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
365 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
368 void visitBlockCommandComment(const BlockCommandComment *C);
369 void visitParamCommandComment(const ParamCommandComment *C);
370 void visitTParamCommandComment(const TParamCommandComment *C);
371 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
372 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
373 void visitVerbatimLineComment(const VerbatimLineComment *C);
377 //===----------------------------------------------------------------------===//
379 //===----------------------------------------------------------------------===//
381 // Print out the appropriate tree structure using the Indents vector.
382 // Example of tree and the Indents vector at each level.
385 // | `-C { IT_Child, IT_LastChild }
386 // `-D { IT_LastChild }
387 // |-E { IT_LastChild, IT_Child }
388 // `-F { IT_LastChild, IT_LastChild }
389 // Type non-last element, last element
390 // IT_Child "| " "|-"
391 // IT_LastChild " " "`-"
392 void ASTDumper::indent() {
398 ColorScope Color(*this, IndentColor);
399 for (SmallVectorImpl<IndentType>::const_iterator I = Indents.begin(),
416 llvm_unreachable("Invalid IndentType");
418 Indents.push_back(IT_Child);
421 void ASTDumper::unindent() {
425 // Call before each potential last child node is to be dumped. If MoreChildren
426 // is false, then this is the last child, otherwise treat as a regular node.
427 void ASTDumper::lastChild() {
428 if (!hasMoreChildren())
429 Indents.back() = IT_LastChild;
432 // MoreChildren should be set before calling another function that may print
433 // additional nodes to prevent conflicting final child nodes.
434 bool ASTDumper::hasMoreChildren() {
438 void ASTDumper::setMoreChildren(bool Value) {
439 MoreChildren = Value;
442 void ASTDumper::dumpPointer(const void *Ptr) {
443 ColorScope Color(*this, AddressColor);
447 void ASTDumper::dumpLocation(SourceLocation Loc) {
451 ColorScope Color(*this, LocationColor);
452 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
454 // The general format we print out is filename:line:col, but we drop pieces
455 // that haven't changed since the last loc printed.
456 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
458 if (PLoc.isInvalid()) {
459 OS << "<invalid sloc>";
463 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
464 OS << PLoc.getFilename() << ':' << PLoc.getLine()
465 << ':' << PLoc.getColumn();
466 LastLocFilename = PLoc.getFilename();
467 LastLocLine = PLoc.getLine();
468 } else if (PLoc.getLine() != LastLocLine) {
469 OS << "line" << ':' << PLoc.getLine()
470 << ':' << PLoc.getColumn();
471 LastLocLine = PLoc.getLine();
473 OS << "col" << ':' << PLoc.getColumn();
477 void ASTDumper::dumpSourceRange(SourceRange R) {
478 // Can't translate locations if a SourceManager isn't available.
483 dumpLocation(R.getBegin());
484 if (R.getBegin() != R.getEnd()) {
486 dumpLocation(R.getEnd());
490 // <t2.c:123:421[blah], t2.c:412:321>
494 void ASTDumper::dumpBareType(QualType T) {
495 ColorScope Color(*this, TypeColor);
497 SplitQualType T_split = T.split();
498 OS << "'" << QualType::getAsString(T_split) << "'";
501 // If the type is sugared, also dump a (shallow) desugared type.
502 SplitQualType D_split = T.getSplitDesugaredType();
503 if (T_split != D_split)
504 OS << ":'" << QualType::getAsString(D_split) << "'";
508 void ASTDumper::dumpType(QualType T) {
513 void ASTDumper::dumpBareDeclRef(const Decl *D) {
515 ColorScope Color(*this, DeclKindNameColor);
516 OS << D->getDeclKindName();
520 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
521 ColorScope Color(*this, DeclNameColor);
522 OS << " '" << ND->getDeclName() << '\'';
525 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
526 dumpType(VD->getType());
529 void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
533 IndentScope Indent(*this);
539 void ASTDumper::dumpName(const NamedDecl *ND) {
540 if (ND->getDeclName()) {
541 ColorScope Color(*this, DeclNameColor);
542 OS << ' ' << ND->getNameAsString();
546 bool ASTDumper::hasNodes(const DeclContext *DC) {
550 return DC->hasExternalLexicalStorage() ||
551 DC->noload_decls_begin() != DC->noload_decls_end();
554 void ASTDumper::dumpDeclContext(const DeclContext *DC) {
558 ChildDumper Children(*this);
559 for (auto *D : DC->noload_decls())
562 if (DC->hasExternalLexicalStorage()) {
566 IndentScope Indent(*this);
567 ColorScope Color(*this, UndeserializedColor);
568 OS << "<undeserialized declarations>";
572 void ASTDumper::dumpLookups(const DeclContext *DC) {
573 IndentScope Indent(*this);
575 OS << "StoredDeclsMap ";
576 dumpBareDeclRef(cast<Decl>(DC));
578 const DeclContext *Primary = DC->getPrimaryContext();
581 dumpPointer(cast<Decl>(Primary));
584 bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
586 DeclContext::all_lookups_iterator I = Primary->noload_lookups_begin(),
587 E = Primary->noload_lookups_end();
589 DeclarationName Name = I.getLookupName();
590 DeclContextLookupResult R = *I++;
591 if (I == E && !HasUndeserializedLookups)
594 IndentScope Indent(*this);
595 OS << "DeclarationName ";
597 ColorScope Color(*this, DeclNameColor);
598 OS << '\'' << Name << '\'';
601 for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
606 if ((*RI)->isHidden())
611 if (HasUndeserializedLookups) {
613 IndentScope Indent(*this);
614 ColorScope Color(*this, UndeserializedColor);
615 OS << "<undeserialized lookups>";
619 void ASTDumper::dumpAttr(const Attr *A) {
620 IndentScope Indent(*this);
622 ColorScope Color(*this, AttrColor);
624 switch (A->getKind()) {
625 #define ATTR(X) case attr::X: OS << #X; break;
626 #include "clang/Basic/AttrList.inc"
627 default: llvm_unreachable("unexpected attribute kind");
632 dumpSourceRange(A->getRange());
633 if (A->isInherited())
637 #include "clang/AST/AttrDump.inc"
640 static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
643 static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
644 const T *First = D->getFirstDecl();
646 OS << " first " << First;
650 static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
651 const T *Prev = D->getPreviousDecl();
653 OS << " prev " << Prev;
656 /// Dump the previous declaration in the redeclaration chain for a declaration,
658 static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
659 switch (D->getKind()) {
660 #define DECL(DERIVED, BASE) \
661 case Decl::DERIVED: \
662 return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
663 #define ABSTRACT_DECL(DECL)
664 #include "clang/AST/DeclNodes.inc"
666 llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
669 //===----------------------------------------------------------------------===//
671 //===----------------------------------------------------------------------===//
673 void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
689 void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
690 IndentScope Indent(*this);
691 OS << "CXXCtorInitializer";
692 if (Init->isAnyMemberInitializer()) {
694 dumpBareDeclRef(Init->getAnyMember());
696 dumpType(QualType(Init->getBaseClass(), 0));
698 dumpStmt(Init->getInit());
701 void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
705 for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
710 void ASTDumper::dumpTemplateArgumentListInfo(
711 const TemplateArgumentListInfo &TALI) {
712 for (unsigned i = 0, e = TALI.size(); i < e; ++i) {
715 dumpTemplateArgumentLoc(TALI[i]);
719 void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
720 dumpTemplateArgument(A.getArgument(), A.getSourceRange());
723 void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
724 for (unsigned i = 0, e = TAL.size(); i < e; ++i)
725 dumpTemplateArgument(TAL[i]);
728 void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
729 IndentScope Indent(*this);
730 OS << "TemplateArgument";
734 switch (A.getKind()) {
735 case TemplateArgument::Null:
738 case TemplateArgument::Type:
741 dumpType(A.getAsType());
743 case TemplateArgument::Declaration:
746 dumpDeclRef(A.getAsDecl());
748 case TemplateArgument::NullPtr:
751 case TemplateArgument::Integral:
752 OS << " integral " << A.getAsIntegral();
754 case TemplateArgument::Template:
756 A.getAsTemplate().dump(OS);
758 case TemplateArgument::TemplateExpansion:
759 OS << " template expansion";
760 A.getAsTemplateOrTemplatePattern().dump(OS);
762 case TemplateArgument::Expression:
765 dumpStmt(A.getAsExpr());
767 case TemplateArgument::Pack:
769 for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
773 dumpTemplateArgument(*I);
779 //===----------------------------------------------------------------------===//
780 // Decl dumping methods.
781 //===----------------------------------------------------------------------===//
783 void ASTDumper::dumpDecl(const Decl *D) {
784 IndentScope Indent(*this);
787 ColorScope Color(*this, NullColor);
793 ColorScope Color(*this, DeclKindNameColor);
794 OS << D->getDeclKindName() << "Decl";
797 if (D->getLexicalDeclContext() != D->getDeclContext())
798 OS << " parent " << cast<Decl>(D->getDeclContext());
799 dumpPreviousDecl(OS, D);
800 dumpSourceRange(D->getSourceRange());
802 dumpLocation(D->getLocation());
803 if (Module *M = D->getOwningModule())
804 OS << " in " << M->getFullModuleName();
805 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
812 else if (D->isReferenced())
814 if (D->isInvalidDecl())
817 bool HasAttrs = D->hasAttrs();
818 const FullComment *Comment =
819 D->getASTContext().getLocalCommentForDeclUncached(D);
820 // Decls within functions are visited by the body
821 bool HasDeclContext = !isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
822 hasNodes(dyn_cast<DeclContext>(D));
824 setMoreChildren(HasAttrs || Comment || HasDeclContext);
825 ConstDeclVisitor<ASTDumper>::Visit(D);
827 setMoreChildren(Comment || HasDeclContext);
828 for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end();
835 setMoreChildren(HasDeclContext);
837 dumpFullComment(Comment);
839 setMoreChildren(false);
841 dumpDeclContext(cast<DeclContext>(D));
844 void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
848 void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
850 dumpType(D->getUnderlyingType());
851 if (D->isModulePrivate())
852 OS << " __module_private__";
855 void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
857 if (D->isScopedUsingClassTag())
863 if (D->isModulePrivate())
864 OS << " __module_private__";
866 dumpType(D->getIntegerType());
869 void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
870 OS << ' ' << D->getKindName();
872 if (D->isModulePrivate())
873 OS << " __module_private__";
874 if (D->isCompleteDefinition())
878 void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
880 dumpType(D->getType());
881 if (const Expr *Init = D->getInitExpr()) {
887 void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
889 dumpType(D->getType());
891 ChildDumper Children(*this);
892 for (auto *Child : D->chain())
893 Children.dumpRef(Child);
896 void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
898 dumpType(D->getType());
900 StorageClass SC = D->getStorageClass();
902 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
903 if (D->isInlineSpecified())
905 if (D->isVirtualAsWritten())
907 if (D->isModulePrivate())
908 OS << " __module_private__";
912 else if (D->isDeletedAsWritten())
915 if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
916 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
917 switch (EPI.ExceptionSpecType) {
919 case EST_Unevaluated:
920 OS << " noexcept-unevaluated " << EPI.ExceptionSpecDecl;
922 case EST_Uninstantiated:
923 OS << " noexcept-uninstantiated " << EPI.ExceptionSpecTemplate;
928 bool OldMoreChildren = hasMoreChildren();
929 const FunctionTemplateSpecializationInfo *FTSI =
930 D->getTemplateSpecializationInfo();
931 bool HasTemplateSpecialization = FTSI;
933 bool HasNamedDecls = D->getDeclsInPrototypeScope().begin() !=
934 D->getDeclsInPrototypeScope().end();
936 bool HasFunctionDecls = D->param_begin() != D->param_end();
938 const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D);
939 bool HasCtorInitializers = C && C->init_begin() != C->init_end();
941 bool HasDeclarationBody = D->doesThisDeclarationHaveABody();
943 setMoreChildren(OldMoreChildren || HasNamedDecls || HasFunctionDecls ||
944 HasCtorInitializers || HasDeclarationBody);
945 if (HasTemplateSpecialization) {
947 dumpTemplateArgumentList(*FTSI->TemplateArguments);
950 setMoreChildren(OldMoreChildren || HasFunctionDecls ||
951 HasCtorInitializers || HasDeclarationBody);
952 for (ArrayRef<NamedDecl *>::iterator
953 I = D->getDeclsInPrototypeScope().begin(),
954 E = D->getDeclsInPrototypeScope().end(); I != E; ++I) {
960 setMoreChildren(OldMoreChildren || HasCtorInitializers || HasDeclarationBody);
961 for (FunctionDecl::param_const_iterator I = D->param_begin(),
969 setMoreChildren(OldMoreChildren || HasDeclarationBody);
970 if (HasCtorInitializers)
971 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
976 dumpCXXCtorInitializer(*I);
979 setMoreChildren(OldMoreChildren);
980 if (HasDeclarationBody) {
982 dumpStmt(D->getBody());
986 void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
988 dumpType(D->getType());
991 if (D->isModulePrivate())
992 OS << " __module_private__";
994 bool OldMoreChildren = hasMoreChildren();
995 bool IsBitField = D->isBitField();
996 Expr *Init = D->getInClassInitializer();
999 setMoreChildren(OldMoreChildren || HasInit);
1002 dumpStmt(D->getBitWidth());
1004 setMoreChildren(OldMoreChildren);
1011 void ASTDumper::VisitVarDecl(const VarDecl *D) {
1013 dumpType(D->getType());
1014 StorageClass SC = D->getStorageClass();
1016 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1017 switch (D->getTLSKind()) {
1018 case VarDecl::TLS_None: break;
1019 case VarDecl::TLS_Static: OS << " tls"; break;
1020 case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
1022 if (D->isModulePrivate())
1023 OS << " __module_private__";
1024 if (D->isNRVOVariable())
1027 switch (D->getInitStyle()) {
1028 case VarDecl::CInit: OS << " cinit"; break;
1029 case VarDecl::CallInit: OS << " callinit"; break;
1030 case VarDecl::ListInit: OS << " listinit"; break;
1033 dumpStmt(D->getInit());
1037 void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
1039 dumpStmt(D->getAsmString());
1042 void ASTDumper::VisitImportDecl(const ImportDecl *D) {
1043 OS << ' ' << D->getImportedModule()->getFullModuleName();
1046 //===----------------------------------------------------------------------===//
1048 //===----------------------------------------------------------------------===//
1050 void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
1054 if (!D->isOriginalNamespace())
1055 dumpDeclRef(D->getOriginalNamespace(), "original");
1058 void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
1060 dumpBareDeclRef(D->getNominatedNamespace());
1063 void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
1065 dumpDeclRef(D->getAliasedNamespace());
1068 void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
1070 dumpType(D->getUnderlyingType());
1073 void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
1075 dumpTemplateParameters(D->getTemplateParameters());
1076 dumpDecl(D->getTemplatedDecl());
1079 void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
1081 if (!D->isCompleteDefinition())
1084 for (const auto &I : D->bases()) {
1085 IndentScope Indent(*this);
1088 dumpAccessSpecifier(I.getAccessSpecifier());
1089 dumpType(I.getType());
1090 if (I.isPackExpansion())
1095 void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
1096 dumpStmt(D->getAssertExpr());
1098 dumpStmt(D->getMessage());
1101 template<typename SpecializationDecl>
1102 void ASTDumper::VisitTemplateDeclSpecialization(ChildDumper &Children,
1103 const SpecializationDecl *D,
1104 bool DumpExplicitInst,
1106 bool DumpedAny = false;
1107 for (auto *RedeclWithBadType : D->redecls()) {
1108 // FIXME: The redecls() range sometimes has elements of a less-specific
1109 // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
1110 // us TagDecls, and should give CXXRecordDecls).
1111 auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
1113 // Found the injected-class-name for a class template. This will be dumped
1114 // as part of its surrounding class so we don't need to dump it here.
1115 assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
1116 "expected an injected-class-name");
1120 switch (Redecl->getTemplateSpecializationKind()) {
1121 case TSK_ExplicitInstantiationDeclaration:
1122 case TSK_ExplicitInstantiationDefinition:
1123 if (!DumpExplicitInst)
1126 case TSK_Undeclared:
1127 case TSK_ImplicitInstantiation:
1128 Children.dump(Redecl, DumpRefOnly);
1131 case TSK_ExplicitSpecialization:
1136 // Ensure we dump at least one decl for each specialization.
1138 Children.dumpRef(D);
1141 template<typename TemplateDecl>
1142 void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
1143 bool DumpExplicitInst) {
1145 dumpTemplateParameters(D->getTemplateParameters());
1147 ChildDumper Children(*this);
1148 Children.dump(D->getTemplatedDecl());
1150 for (auto *Child : D->specializations())
1151 VisitTemplateDeclSpecialization(Children, Child, DumpExplicitInst,
1152 !D->isCanonicalDecl());
1155 void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
1156 // FIXME: We don't add a declaration of a function template specialization
1157 // to its context when it's explicitly instantiated, so dump explicit
1158 // instantiations when we dump the template itself.
1159 VisitTemplateDecl(D, true);
1162 void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
1163 VisitTemplateDecl(D, false);
1166 void ASTDumper::VisitClassTemplateSpecializationDecl(
1167 const ClassTemplateSpecializationDecl *D) {
1168 VisitCXXRecordDecl(D);
1169 dumpTemplateArgumentList(D->getTemplateArgs());
1172 void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
1173 const ClassTemplatePartialSpecializationDecl *D) {
1174 VisitClassTemplateSpecializationDecl(D);
1175 dumpTemplateParameters(D->getTemplateParameters());
1178 void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
1179 const ClassScopeFunctionSpecializationDecl *D) {
1180 dumpDeclRef(D->getSpecialization());
1181 if (D->hasExplicitTemplateArgs())
1182 dumpTemplateArgumentListInfo(D->templateArgs());
1185 void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
1186 VisitTemplateDecl(D, false);
1189 void ASTDumper::VisitVarTemplateSpecializationDecl(
1190 const VarTemplateSpecializationDecl *D) {
1191 dumpTemplateArgumentList(D->getTemplateArgs());
1195 void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
1196 const VarTemplatePartialSpecializationDecl *D) {
1197 dumpTemplateParameters(D->getTemplateParameters());
1198 VisitVarTemplateSpecializationDecl(D);
1201 void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
1202 if (D->wasDeclaredWithTypename())
1206 if (D->isParameterPack())
1209 if (D->hasDefaultArgument()) {
1211 dumpTemplateArgument(D->getDefaultArgument());
1215 void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
1216 dumpType(D->getType());
1217 if (D->isParameterPack())
1220 if (D->hasDefaultArgument()) {
1222 dumpTemplateArgument(D->getDefaultArgument());
1226 void ASTDumper::VisitTemplateTemplateParmDecl(
1227 const TemplateTemplateParmDecl *D) {
1228 if (D->isParameterPack())
1231 dumpTemplateParameters(D->getTemplateParameters());
1232 if (D->hasDefaultArgument()) {
1234 dumpTemplateArgumentLoc(D->getDefaultArgument());
1238 void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
1240 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1241 OS << D->getNameAsString();
1244 void ASTDumper::VisitUnresolvedUsingTypenameDecl(
1245 const UnresolvedUsingTypenameDecl *D) {
1247 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1248 OS << D->getNameAsString();
1251 void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
1253 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1254 OS << D->getNameAsString();
1255 dumpType(D->getType());
1258 void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
1260 dumpBareDeclRef(D->getTargetDecl());
1263 void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
1264 switch (D->getLanguage()) {
1265 case LinkageSpecDecl::lang_c: OS << " C"; break;
1266 case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1270 void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
1272 dumpAccessSpecifier(D->getAccess());
1275 void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
1277 if (TypeSourceInfo *T = D->getFriendType())
1278 dumpType(T->getType());
1280 dumpDecl(D->getFriendDecl());
1283 //===----------------------------------------------------------------------===//
1284 // Obj-C Declarations
1285 //===----------------------------------------------------------------------===//
1287 void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
1289 dumpType(D->getType());
1290 if (D->getSynthesize())
1291 OS << " synthesize";
1293 switch (D->getAccessControl()) {
1294 case ObjCIvarDecl::None:
1297 case ObjCIvarDecl::Private:
1300 case ObjCIvarDecl::Protected:
1303 case ObjCIvarDecl::Public:
1306 case ObjCIvarDecl::Package:
1312 void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
1313 if (D->isInstanceMethod())
1318 dumpType(D->getReturnType());
1320 bool OldMoreChildren = hasMoreChildren();
1321 bool IsVariadic = D->isVariadic();
1322 bool HasBody = D->hasBody();
1324 setMoreChildren(OldMoreChildren || IsVariadic || HasBody);
1325 if (D->isThisDeclarationADefinition()) {
1329 for (ObjCMethodDecl::param_const_iterator I = D->param_begin(),
1338 setMoreChildren(OldMoreChildren || HasBody);
1341 IndentScope Indent(*this);
1345 setMoreChildren(OldMoreChildren);
1348 dumpStmt(D->getBody());
1352 void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
1354 dumpDeclRef(D->getClassInterface());
1355 if (D->protocol_begin() == D->protocol_end())
1357 dumpDeclRef(D->getImplementation());
1358 for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
1359 E = D->protocol_end();
1367 void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
1369 dumpDeclRef(D->getClassInterface());
1371 dumpDeclRef(D->getCategoryDecl());
1374 void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
1377 ChildDumper Children(*this);
1378 for (auto *Child : D->protocols())
1379 Children.dumpRef(Child);
1382 void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
1384 dumpDeclRef(D->getSuperClass(), "super");
1386 ChildDumper Children(*this);
1387 Children.dumpRef(D->getImplementation());
1388 for (auto *Child : D->protocols())
1389 Children.dumpRef(Child);
1392 void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
1394 dumpDeclRef(D->getSuperClass(), "super");
1395 if (D->init_begin() == D->init_end())
1397 dumpDeclRef(D->getClassInterface());
1398 for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
1403 dumpCXXCtorInitializer(*I);
1407 void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
1410 dumpDeclRef(D->getClassInterface());
1413 void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
1415 dumpType(D->getType());
1417 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1419 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1422 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1423 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1424 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1426 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1428 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1430 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1432 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1434 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1436 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1438 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1440 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1442 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1443 OS << " unsafe_unretained";
1444 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter) {
1445 if (!(Attrs & ObjCPropertyDecl::OBJC_PR_setter))
1447 dumpDeclRef(D->getGetterMethodDecl(), "getter");
1449 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter) {
1451 dumpDeclRef(D->getSetterMethodDecl(), "setter");
1456 void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
1457 dumpName(D->getPropertyDecl());
1458 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1459 OS << " synthesize";
1462 dumpDeclRef(D->getPropertyDecl());
1464 dumpDeclRef(D->getPropertyIvarDecl());
1467 void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
1468 for (auto I : D->params())
1471 if (D->isVariadic()) {
1472 IndentScope Indent(*this);
1476 if (D->capturesCXXThis()) {
1477 IndentScope Indent(*this);
1478 OS << "capture this";
1480 for (const auto &I : D->captures()) {
1481 IndentScope Indent(*this);
1487 if (I.getVariable()) {
1489 dumpBareDeclRef(I.getVariable());
1491 if (I.hasCopyExpr())
1492 dumpStmt(I.getCopyExpr());
1495 dumpStmt(D->getBody());
1498 //===----------------------------------------------------------------------===//
1499 // Stmt dumping methods.
1500 //===----------------------------------------------------------------------===//
1502 void ASTDumper::dumpStmt(const Stmt *S) {
1503 IndentScope Indent(*this);
1506 ColorScope Color(*this, NullColor);
1511 if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
1516 setMoreChildren(!S->children().empty());
1517 ConstStmtVisitor<ASTDumper>::Visit(S);
1518 setMoreChildren(false);
1519 for (Stmt::const_child_range CI = S->children(); CI; ++CI) {
1520 Stmt::const_child_range Next = CI;
1528 void ASTDumper::VisitStmt(const Stmt *Node) {
1530 ColorScope Color(*this, StmtColor);
1531 OS << Node->getStmtClassName();
1534 dumpSourceRange(Node->getSourceRange());
1537 void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
1539 for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
1540 E = Node->decl_end();
1548 void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
1550 for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
1551 E = Node->getAttrs().end();
1559 void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
1561 OS << " '" << Node->getName() << "'";
1564 void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
1566 OS << " '" << Node->getLabel()->getName() << "'";
1567 dumpPointer(Node->getLabel());
1570 void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
1572 dumpDecl(Node->getExceptionDecl());
1575 //===----------------------------------------------------------------------===//
1576 // Expr dumping methods.
1577 //===----------------------------------------------------------------------===//
1579 void ASTDumper::VisitExpr(const Expr *Node) {
1581 dumpType(Node->getType());
1584 ColorScope Color(*this, ValueKindColor);
1585 switch (Node->getValueKind()) {
1598 ColorScope Color(*this, ObjectKindColor);
1599 switch (Node->getObjectKind()) {
1605 case OK_ObjCProperty:
1606 OS << " objcproperty";
1608 case OK_ObjCSubscript:
1609 OS << " objcsubscript";
1611 case OK_VectorComponent:
1612 OS << " vectorcomponent";
1618 static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
1619 if (Node->path_empty())
1624 for (CastExpr::path_const_iterator I = Node->path_begin(),
1625 E = Node->path_end();
1627 const CXXBaseSpecifier *Base = *I;
1631 const CXXRecordDecl *RD =
1632 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1634 if (Base->isVirtual())
1636 OS << RD->getName();
1643 void ASTDumper::VisitCastExpr(const CastExpr *Node) {
1647 ColorScope Color(*this, CastColor);
1648 OS << Node->getCastKindName();
1650 dumpBasePath(OS, Node);
1654 void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
1658 dumpBareDeclRef(Node->getDecl());
1659 if (Node->getDecl() != Node->getFoundDecl()) {
1661 dumpBareDeclRef(Node->getFoundDecl());
1666 void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
1669 if (!Node->requiresADL())
1671 OS << "ADL) = '" << Node->getName() << '\'';
1673 UnresolvedLookupExpr::decls_iterator
1674 I = Node->decls_begin(), E = Node->decls_end();
1681 void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
1685 ColorScope Color(*this, DeclKindNameColor);
1686 OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
1688 OS << "='" << *Node->getDecl() << "'";
1689 dumpPointer(Node->getDecl());
1690 if (Node->isFreeIvar())
1691 OS << " isFreeIvar";
1694 void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
1696 switch (Node->getIdentType()) {
1697 default: llvm_unreachable("unknown case");
1698 case PredefinedExpr::Func: OS << " __func__"; break;
1699 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
1700 case PredefinedExpr::FuncDName: OS << " __FUNCDNAME__"; break;
1701 case PredefinedExpr::LFunction: OS << " L__FUNCTION__"; break;
1702 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
1703 case PredefinedExpr::FuncSig: OS << " __FUNCSIG__"; break;
1707 void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
1709 ColorScope Color(*this, ValueColor);
1710 OS << " " << Node->getValue();
1713 void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
1716 bool isSigned = Node->getType()->isSignedIntegerType();
1717 ColorScope Color(*this, ValueColor);
1718 OS << " " << Node->getValue().toString(10, isSigned);
1721 void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
1723 ColorScope Color(*this, ValueColor);
1724 OS << " " << Node->getValueAsApproximateDouble();
1727 void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
1729 ColorScope Color(*this, ValueColor);
1731 Str->outputString(OS);
1734 void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
1736 if (auto *Filler = ILE->getArrayFiller()) {
1737 if (!ILE->getNumInits())
1739 IndentScope Indent(*this);
1740 OS << "array filler";
1744 if (auto *Field = ILE->getInitializedFieldInUnion()) {
1746 dumpBareDeclRef(Field);
1750 void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
1752 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
1753 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
1756 void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
1757 const UnaryExprOrTypeTraitExpr *Node) {
1759 switch(Node->getKind()) {
1770 if (Node->isArgumentType())
1771 dumpType(Node->getArgumentType());
1774 void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
1776 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
1777 dumpPointer(Node->getMemberDecl());
1780 void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
1782 OS << " " << Node->getAccessor().getNameStart();
1785 void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
1787 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
1790 void ASTDumper::VisitCompoundAssignOperator(
1791 const CompoundAssignOperator *Node) {
1793 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
1794 << "' ComputeLHSTy=";
1795 dumpBareType(Node->getComputationLHSType());
1796 OS << " ComputeResultTy=";
1797 dumpBareType(Node->getComputationResultType());
1800 void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
1802 dumpDecl(Node->getBlockDecl());
1805 void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
1808 if (Expr *Source = Node->getSourceExpr()) {
1816 void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
1818 OS << " " << Node->getLabel()->getName();
1819 dumpPointer(Node->getLabel());
1822 //===----------------------------------------------------------------------===//
1824 //===----------------------------------------------------------------------===//
1826 void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
1828 OS << " " << Node->getCastName()
1829 << "<" << Node->getTypeAsWritten().getAsString() << ">"
1830 << " <" << Node->getCastKindName();
1831 dumpBasePath(OS, Node);
1835 void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
1837 OS << " " << (Node->getValue() ? "true" : "false");
1840 void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
1845 void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
1847 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
1848 << " <" << Node->getCastKindName() << ">";
1851 void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
1853 CXXConstructorDecl *Ctor = Node->getConstructor();
1854 dumpType(Ctor->getType());
1855 if (Node->isElidable())
1857 if (Node->requiresZeroInitialization())
1861 void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
1864 dumpCXXTemporary(Node->getTemporary());
1868 ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
1870 if (const ValueDecl *VD = Node->getExtendingDecl()) {
1871 OS << " extended by ";
1872 dumpBareDeclRef(VD);
1876 void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
1878 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
1879 dumpDeclRef(Node->getObject(i), "cleanup");
1882 void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
1883 OS << "(CXXTemporary";
1884 dumpPointer(Temporary);
1888 //===----------------------------------------------------------------------===//
1889 // Obj-C Expressions
1890 //===----------------------------------------------------------------------===//
1892 void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
1895 Node->getSelector().print(OS);
1896 switch (Node->getReceiverKind()) {
1897 case ObjCMessageExpr::Instance:
1900 case ObjCMessageExpr::Class:
1902 dumpBareType(Node->getClassReceiver());
1905 case ObjCMessageExpr::SuperInstance:
1906 OS << " super (instance)";
1909 case ObjCMessageExpr::SuperClass:
1910 OS << " super (class)";
1915 void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
1918 Node->getBoxingMethod()->getSelector().print(OS);
1921 void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
1923 if (const VarDecl *CatchParam = Node->getCatchParamDecl())
1924 dumpDecl(CatchParam);
1929 void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
1931 dumpType(Node->getEncodedType());
1934 void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
1938 Node->getSelector().print(OS);
1941 void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
1944 OS << ' ' << *Node->getProtocol();
1947 void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
1949 if (Node->isImplicitProperty()) {
1950 OS << " Kind=MethodRef Getter=\"";
1951 if (Node->getImplicitPropertyGetter())
1952 Node->getImplicitPropertyGetter()->getSelector().print(OS);
1956 OS << "\" Setter=\"";
1957 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
1958 Setter->getSelector().print(OS);
1963 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
1966 if (Node->isSuperReceiver())
1969 OS << " Messaging=";
1970 if (Node->isMessagingGetter() && Node->isMessagingSetter())
1971 OS << "Getter&Setter";
1972 else if (Node->isMessagingGetter())
1974 else if (Node->isMessagingSetter())
1978 void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
1980 if (Node->isArraySubscriptRefExpr())
1981 OS << " Kind=ArraySubscript GetterForArray=\"";
1983 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
1984 if (Node->getAtIndexMethodDecl())
1985 Node->getAtIndexMethodDecl()->getSelector().print(OS);
1989 if (Node->isArraySubscriptRefExpr())
1990 OS << "\" SetterForArray=\"";
1992 OS << "\" SetterForDictionary=\"";
1993 if (Node->setAtIndexMethodDecl())
1994 Node->setAtIndexMethodDecl()->getSelector().print(OS);
1999 void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
2001 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
2004 //===----------------------------------------------------------------------===//
2006 //===----------------------------------------------------------------------===//
2008 const char *ASTDumper::getCommandName(unsigned CommandID) {
2010 return Traits->getCommandInfo(CommandID)->Name;
2011 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
2014 return "<not a builtin command>";
2017 void ASTDumper::dumpFullComment(const FullComment *C) {
2026 void ASTDumper::dumpComment(const Comment *C) {
2027 IndentScope Indent(*this);
2030 ColorScope Color(*this, NullColor);
2036 ColorScope Color(*this, CommentColor);
2037 OS << C->getCommentKindName();
2040 dumpSourceRange(C->getSourceRange());
2041 ConstCommentVisitor<ASTDumper>::visit(C);
2042 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
2050 void ASTDumper::visitTextComment(const TextComment *C) {
2051 OS << " Text=\"" << C->getText() << "\"";
2054 void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
2055 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2056 switch (C->getRenderKind()) {
2057 case InlineCommandComment::RenderNormal:
2058 OS << " RenderNormal";
2060 case InlineCommandComment::RenderBold:
2061 OS << " RenderBold";
2063 case InlineCommandComment::RenderMonospaced:
2064 OS << " RenderMonospaced";
2066 case InlineCommandComment::RenderEmphasized:
2067 OS << " RenderEmphasized";
2071 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2072 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2075 void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
2076 OS << " Name=\"" << C->getTagName() << "\"";
2077 if (C->getNumAttrs() != 0) {
2079 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
2080 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
2081 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
2084 if (C->isSelfClosing())
2085 OS << " SelfClosing";
2088 void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
2089 OS << " Name=\"" << C->getTagName() << "\"";
2092 void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
2093 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2094 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2095 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2098 void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
2099 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
2101 if (C->isDirectionExplicit())
2102 OS << " explicitly";
2104 OS << " implicitly";
2106 if (C->hasParamName()) {
2107 if (C->isParamIndexValid())
2108 OS << " Param=\"" << C->getParamName(FC) << "\"";
2110 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2113 if (C->isParamIndexValid() && !C->isVarArgParam())
2114 OS << " ParamIndex=" << C->getParamIndex();
2117 void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
2118 if (C->hasParamName()) {
2119 if (C->isPositionValid())
2120 OS << " Param=\"" << C->getParamName(FC) << "\"";
2122 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2125 if (C->isPositionValid()) {
2126 OS << " Position=<";
2127 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
2128 OS << C->getIndex(i);
2136 void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
2137 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
2138 " CloseName=\"" << C->getCloseName() << "\"";
2141 void ASTDumper::visitVerbatimBlockLineComment(
2142 const VerbatimBlockLineComment *C) {
2143 OS << " Text=\"" << C->getText() << "\"";
2146 void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
2147 OS << " Text=\"" << C->getText() << "\"";
2150 //===----------------------------------------------------------------------===//
2151 // Decl method implementations
2152 //===----------------------------------------------------------------------===//
2154 LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
2156 LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS) const {
2157 ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
2158 &getASTContext().getSourceManager());
2162 LLVM_DUMP_METHOD void Decl::dumpColor() const {
2163 ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
2164 &getASTContext().getSourceManager(), /*ShowColors*/true);
2168 LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
2169 dumpLookups(llvm::errs());
2172 LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS) const {
2173 const DeclContext *DC = this;
2174 while (!DC->isTranslationUnit())
2175 DC = DC->getParent();
2176 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
2177 ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
2178 P.dumpLookups(this);
2181 //===----------------------------------------------------------------------===//
2182 // Stmt method implementations
2183 //===----------------------------------------------------------------------===//
2185 LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
2186 dump(llvm::errs(), SM);
2189 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
2190 ASTDumper P(OS, nullptr, &SM);
2194 LLVM_DUMP_METHOD void Stmt::dump() const {
2195 ASTDumper P(llvm::errs(), nullptr, nullptr);
2199 LLVM_DUMP_METHOD void Stmt::dumpColor() const {
2200 ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
2204 //===----------------------------------------------------------------------===//
2205 // Comment method implementations
2206 //===----------------------------------------------------------------------===//
2208 LLVM_DUMP_METHOD void Comment::dump() const {
2209 dump(llvm::errs(), nullptr, nullptr);
2212 LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
2213 dump(llvm::errs(), &Context.getCommentCommandTraits(),
2214 &Context.getSourceManager());
2217 void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
2218 const SourceManager *SM) const {
2219 const FullComment *FC = dyn_cast<FullComment>(this);
2220 ASTDumper D(OS, Traits, SM);
2221 D.dumpFullComment(FC);
2224 LLVM_DUMP_METHOD void Comment::dumpColor() const {
2225 const FullComment *FC = dyn_cast<FullComment>(this);
2226 ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
2227 D.dumpFullComment(FC);