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();
153 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
154 const SourceManager *SM)
155 : OS(OS), Traits(Traits), SM(SM), IsFirstLine(true), MoreChildren(false),
156 LastLocFilename(""), LastLocLine(~0U), FC(0),
157 ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
159 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
160 const SourceManager *SM, bool ShowColors)
161 : OS(OS), Traits(Traits), SM(SM), IsFirstLine(true), MoreChildren(false),
162 LastLocFilename(""), LastLocLine(~0U),
163 ShowColors(ShowColors) { }
169 void dumpDecl(const Decl *D);
170 void dumpStmt(const Stmt *S);
171 void dumpFullComment(const FullComment *C);
177 bool hasMoreChildren();
178 void setMoreChildren(bool Value);
181 void dumpPointer(const void *Ptr);
182 void dumpSourceRange(SourceRange R);
183 void dumpLocation(SourceLocation Loc);
184 void dumpBareType(QualType T);
185 void dumpType(QualType T);
186 void dumpBareDeclRef(const Decl *Node);
187 void dumpDeclRef(const Decl *Node, const char *Label = 0);
188 void dumpName(const NamedDecl *D);
189 bool hasNodes(const DeclContext *DC);
190 void dumpDeclContext(const DeclContext *DC);
191 void dumpLookups(const DeclContext *DC);
192 void dumpAttr(const Attr *A);
195 void dumpAccessSpecifier(AccessSpecifier AS);
196 void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
197 void dumpTemplateParameters(const TemplateParameterList *TPL);
198 void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
199 void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
200 void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
201 void dumpTemplateArgument(const TemplateArgument &A,
202 SourceRange R = SourceRange());
205 void VisitLabelDecl(const LabelDecl *D);
206 void VisitTypedefDecl(const TypedefDecl *D);
207 void VisitEnumDecl(const EnumDecl *D);
208 void VisitRecordDecl(const RecordDecl *D);
209 void VisitEnumConstantDecl(const EnumConstantDecl *D);
210 void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
211 void VisitFunctionDecl(const FunctionDecl *D);
212 void VisitFieldDecl(const FieldDecl *D);
213 void VisitVarDecl(const VarDecl *D);
214 void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D);
215 void VisitImportDecl(const ImportDecl *D);
218 void VisitNamespaceDecl(const NamespaceDecl *D);
219 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
220 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
221 void VisitTypeAliasDecl(const TypeAliasDecl *D);
222 void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
223 void VisitCXXRecordDecl(const CXXRecordDecl *D);
224 void VisitStaticAssertDecl(const StaticAssertDecl *D);
225 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
226 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
227 void VisitClassTemplateSpecializationDecl(
228 const ClassTemplateSpecializationDecl *D);
229 void VisitClassTemplatePartialSpecializationDecl(
230 const ClassTemplatePartialSpecializationDecl *D);
231 void VisitClassScopeFunctionSpecializationDecl(
232 const ClassScopeFunctionSpecializationDecl *D);
233 void VisitVarTemplateDecl(const VarTemplateDecl *D);
234 void VisitVarTemplateSpecializationDecl(
235 const VarTemplateSpecializationDecl *D);
236 void VisitVarTemplatePartialSpecializationDecl(
237 const VarTemplatePartialSpecializationDecl *D);
238 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
239 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
240 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
241 void VisitUsingDecl(const UsingDecl *D);
242 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
243 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
244 void VisitUsingShadowDecl(const UsingShadowDecl *D);
245 void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
246 void VisitAccessSpecDecl(const AccessSpecDecl *D);
247 void VisitFriendDecl(const FriendDecl *D);
250 void VisitObjCIvarDecl(const ObjCIvarDecl *D);
251 void VisitObjCMethodDecl(const ObjCMethodDecl *D);
252 void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
253 void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
254 void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
255 void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
256 void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
257 void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
258 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
259 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
260 void VisitBlockDecl(const BlockDecl *D);
263 void VisitStmt(const Stmt *Node);
264 void VisitDeclStmt(const DeclStmt *Node);
265 void VisitAttributedStmt(const AttributedStmt *Node);
266 void VisitLabelStmt(const LabelStmt *Node);
267 void VisitGotoStmt(const GotoStmt *Node);
268 void VisitCXXCatchStmt(const CXXCatchStmt *Node);
271 void VisitExpr(const Expr *Node);
272 void VisitCastExpr(const CastExpr *Node);
273 void VisitDeclRefExpr(const DeclRefExpr *Node);
274 void VisitPredefinedExpr(const PredefinedExpr *Node);
275 void VisitCharacterLiteral(const CharacterLiteral *Node);
276 void VisitIntegerLiteral(const IntegerLiteral *Node);
277 void VisitFloatingLiteral(const FloatingLiteral *Node);
278 void VisitStringLiteral(const StringLiteral *Str);
279 void VisitUnaryOperator(const UnaryOperator *Node);
280 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
281 void VisitMemberExpr(const MemberExpr *Node);
282 void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
283 void VisitBinaryOperator(const BinaryOperator *Node);
284 void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
285 void VisitAddrLabelExpr(const AddrLabelExpr *Node);
286 void VisitBlockExpr(const BlockExpr *Node);
287 void VisitOpaqueValueExpr(const OpaqueValueExpr *Node);
290 void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
291 void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
292 void VisitCXXThisExpr(const CXXThisExpr *Node);
293 void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
294 void VisitCXXConstructExpr(const CXXConstructExpr *Node);
295 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
296 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
297 void VisitExprWithCleanups(const ExprWithCleanups *Node);
298 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
299 void dumpCXXTemporary(const CXXTemporary *Temporary);
300 void VisitLambdaExpr(const LambdaExpr *Node) {
302 dumpDecl(Node->getLambdaClass());
306 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
307 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
308 void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
309 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
310 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
311 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
312 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
313 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
314 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
315 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
318 const char *getCommandName(unsigned CommandID);
319 void dumpComment(const Comment *C);
322 void visitTextComment(const TextComment *C);
323 void visitInlineCommandComment(const InlineCommandComment *C);
324 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
325 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
328 void visitBlockCommandComment(const BlockCommandComment *C);
329 void visitParamCommandComment(const ParamCommandComment *C);
330 void visitTParamCommandComment(const TParamCommandComment *C);
331 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
332 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
333 void visitVerbatimLineComment(const VerbatimLineComment *C);
337 //===----------------------------------------------------------------------===//
339 //===----------------------------------------------------------------------===//
341 // Print out the appropriate tree structure using the Indents vector.
342 // Example of tree and the Indents vector at each level.
345 // | `-C { IT_Child, IT_LastChild }
346 // `-D { IT_LastChild }
347 // |-E { IT_LastChild, IT_Child }
348 // `-F { IT_LastChild, IT_LastChild }
349 // Type non-last element, last element
350 // IT_Child "| " "|-"
351 // IT_LastChild " " "`-"
352 void ASTDumper::indent() {
358 ColorScope Color(*this, IndentColor);
359 for (SmallVectorImpl<IndentType>::const_iterator I = Indents.begin(),
376 llvm_unreachable("Invalid IndentType");
378 Indents.push_back(IT_Child);
381 void ASTDumper::unindent() {
385 // Call before each potential last child node is to be dumped. If MoreChildren
386 // is false, then this is the last child, otherwise treat as a regular node.
387 void ASTDumper::lastChild() {
388 if (!hasMoreChildren())
389 Indents.back() = IT_LastChild;
392 // MoreChildren should be set before calling another function that may print
393 // additional nodes to prevent conflicting final child nodes.
394 bool ASTDumper::hasMoreChildren() {
398 void ASTDumper::setMoreChildren(bool Value) {
399 MoreChildren = Value;
402 void ASTDumper::dumpPointer(const void *Ptr) {
403 ColorScope Color(*this, AddressColor);
407 void ASTDumper::dumpLocation(SourceLocation Loc) {
408 ColorScope Color(*this, LocationColor);
409 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
411 // The general format we print out is filename:line:col, but we drop pieces
412 // that haven't changed since the last loc printed.
413 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
415 if (PLoc.isInvalid()) {
416 OS << "<invalid sloc>";
420 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
421 OS << PLoc.getFilename() << ':' << PLoc.getLine()
422 << ':' << PLoc.getColumn();
423 LastLocFilename = PLoc.getFilename();
424 LastLocLine = PLoc.getLine();
425 } else if (PLoc.getLine() != LastLocLine) {
426 OS << "line" << ':' << PLoc.getLine()
427 << ':' << PLoc.getColumn();
428 LastLocLine = PLoc.getLine();
430 OS << "col" << ':' << PLoc.getColumn();
434 void ASTDumper::dumpSourceRange(SourceRange R) {
435 // Can't translate locations if a SourceManager isn't available.
440 dumpLocation(R.getBegin());
441 if (R.getBegin() != R.getEnd()) {
443 dumpLocation(R.getEnd());
447 // <t2.c:123:421[blah], t2.c:412:321>
451 void ASTDumper::dumpBareType(QualType T) {
452 ColorScope Color(*this, TypeColor);
454 SplitQualType T_split = T.split();
455 OS << "'" << QualType::getAsString(T_split) << "'";
458 // If the type is sugared, also dump a (shallow) desugared type.
459 SplitQualType D_split = T.getSplitDesugaredType();
460 if (T_split != D_split)
461 OS << ":'" << QualType::getAsString(D_split) << "'";
465 void ASTDumper::dumpType(QualType T) {
470 void ASTDumper::dumpBareDeclRef(const Decl *D) {
472 ColorScope Color(*this, DeclKindNameColor);
473 OS << D->getDeclKindName();
477 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
478 ColorScope Color(*this, DeclNameColor);
479 OS << " '" << ND->getDeclName() << '\'';
482 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
483 dumpType(VD->getType());
486 void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
490 IndentScope Indent(*this);
496 void ASTDumper::dumpName(const NamedDecl *ND) {
497 if (ND->getDeclName()) {
498 ColorScope Color(*this, DeclNameColor);
499 OS << ' ' << ND->getNameAsString();
503 bool ASTDumper::hasNodes(const DeclContext *DC) {
507 return DC->hasExternalLexicalStorage() ||
508 DC->noload_decls_begin() != DC->noload_decls_end();
511 void ASTDumper::dumpDeclContext(const DeclContext *DC) {
514 bool HasUndeserializedDecls = DC->hasExternalLexicalStorage();
515 for (DeclContext::decl_iterator I = DC->noload_decls_begin(),
516 E = DC->noload_decls_end();
518 DeclContext::decl_iterator Next = I;
520 if (Next == E && !HasUndeserializedDecls)
524 if (HasUndeserializedDecls) {
526 IndentScope Indent(*this);
527 ColorScope Color(*this, UndeserializedColor);
528 OS << "<undeserialized declarations>";
532 void ASTDumper::dumpLookups(const DeclContext *DC) {
533 IndentScope Indent(*this);
535 OS << "StoredDeclsMap ";
536 dumpBareDeclRef(cast<Decl>(DC));
538 const DeclContext *Primary = DC->getPrimaryContext();
541 dumpPointer(cast<Decl>(Primary));
544 bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
546 DeclContext::all_lookups_iterator I = Primary->noload_lookups_begin(),
547 E = Primary->noload_lookups_end();
549 DeclarationName Name = I.getLookupName();
550 DeclContextLookupResult R = *I++;
551 if (I == E && !HasUndeserializedLookups)
554 IndentScope Indent(*this);
555 OS << "DeclarationName ";
557 ColorScope Color(*this, DeclNameColor);
558 OS << '\'' << Name << '\'';
561 for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
566 if ((*RI)->isHidden())
571 if (HasUndeserializedLookups) {
573 IndentScope Indent(*this);
574 ColorScope Color(*this, UndeserializedColor);
575 OS << "<undeserialized lookups>";
579 void ASTDumper::dumpAttr(const Attr *A) {
580 IndentScope Indent(*this);
582 ColorScope Color(*this, AttrColor);
583 switch (A->getKind()) {
584 #define ATTR(X) case attr::X: OS << #X; break;
585 #include "clang/Basic/AttrList.inc"
586 default: llvm_unreachable("unexpected attribute kind");
591 dumpSourceRange(A->getRange());
592 #include "clang/AST/AttrDump.inc"
595 static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
598 static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
599 const T *First = D->getFirstDecl();
601 OS << " first " << First;
605 static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
606 const T *Prev = D->getPreviousDecl();
608 OS << " prev " << Prev;
611 /// Dump the previous declaration in the redeclaration chain for a declaration,
613 static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
614 switch (D->getKind()) {
615 #define DECL(DERIVED, BASE) \
616 case Decl::DERIVED: \
617 return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
618 #define ABSTRACT_DECL(DECL)
619 #include "clang/AST/DeclNodes.inc"
621 llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
624 //===----------------------------------------------------------------------===//
626 //===----------------------------------------------------------------------===//
628 void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
644 void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
645 IndentScope Indent(*this);
646 OS << "CXXCtorInitializer";
647 if (Init->isAnyMemberInitializer()) {
649 dumpBareDeclRef(Init->getAnyMember());
651 dumpType(QualType(Init->getBaseClass(), 0));
653 dumpStmt(Init->getInit());
656 void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
660 for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
665 void ASTDumper::dumpTemplateArgumentListInfo(
666 const TemplateArgumentListInfo &TALI) {
667 for (unsigned i = 0, e = TALI.size(); i < e; ++i) {
670 dumpTemplateArgumentLoc(TALI[i]);
674 void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
675 dumpTemplateArgument(A.getArgument(), A.getSourceRange());
678 void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
679 for (unsigned i = 0, e = TAL.size(); i < e; ++i)
680 dumpTemplateArgument(TAL[i]);
683 void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
684 IndentScope Indent(*this);
685 OS << "TemplateArgument";
689 switch (A.getKind()) {
690 case TemplateArgument::Null:
693 case TemplateArgument::Type:
696 dumpType(A.getAsType());
698 case TemplateArgument::Declaration:
701 dumpDeclRef(A.getAsDecl());
703 case TemplateArgument::NullPtr:
706 case TemplateArgument::Integral:
707 OS << " integral " << A.getAsIntegral();
709 case TemplateArgument::Template:
711 A.getAsTemplate().dump(OS);
713 case TemplateArgument::TemplateExpansion:
714 OS << " template expansion";
715 A.getAsTemplateOrTemplatePattern().dump(OS);
717 case TemplateArgument::Expression:
720 dumpStmt(A.getAsExpr());
722 case TemplateArgument::Pack:
724 for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
728 dumpTemplateArgument(*I);
734 //===----------------------------------------------------------------------===//
735 // Decl dumping methods.
736 //===----------------------------------------------------------------------===//
738 void ASTDumper::dumpDecl(const Decl *D) {
739 IndentScope Indent(*this);
742 ColorScope Color(*this, NullColor);
748 ColorScope Color(*this, DeclKindNameColor);
749 OS << D->getDeclKindName() << "Decl";
752 if (D->getLexicalDeclContext() != D->getDeclContext())
753 OS << " parent " << cast<Decl>(D->getDeclContext());
754 dumpPreviousDecl(OS, D);
755 dumpSourceRange(D->getSourceRange());
756 if (Module *M = D->getOwningModule())
757 OS << " in " << M->getFullModuleName();
758 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
762 bool HasAttrs = D->attr_begin() != D->attr_end();
763 const FullComment *Comment =
764 D->getASTContext().getLocalCommentForDeclUncached(D);
765 // Decls within functions are visited by the body
766 bool HasDeclContext = !isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
767 hasNodes(dyn_cast<DeclContext>(D));
769 setMoreChildren(HasAttrs || Comment || HasDeclContext);
770 ConstDeclVisitor<ASTDumper>::Visit(D);
772 setMoreChildren(Comment || HasDeclContext);
773 for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end();
780 setMoreChildren(HasDeclContext);
782 dumpFullComment(Comment);
784 if (D->isInvalidDecl())
787 setMoreChildren(false);
789 dumpDeclContext(cast<DeclContext>(D));
792 void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
796 void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
798 dumpType(D->getUnderlyingType());
799 if (D->isModulePrivate())
800 OS << " __module_private__";
803 void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
805 if (D->isScopedUsingClassTag())
811 if (D->isModulePrivate())
812 OS << " __module_private__";
814 dumpType(D->getIntegerType());
817 void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
818 OS << ' ' << D->getKindName();
820 if (D->isModulePrivate())
821 OS << " __module_private__";
822 if (D->isCompleteDefinition())
826 void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
828 dumpType(D->getType());
829 if (const Expr *Init = D->getInitExpr()) {
835 void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
837 dumpType(D->getType());
838 for (IndirectFieldDecl::chain_iterator I = D->chain_begin(),
847 void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
849 dumpType(D->getType());
851 StorageClass SC = D->getStorageClass();
853 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
854 if (D->isInlineSpecified())
856 if (D->isVirtualAsWritten())
858 if (D->isModulePrivate())
859 OS << " __module_private__";
863 else if (D->isDeletedAsWritten())
866 if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
867 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
868 switch (EPI.ExceptionSpecType) {
870 case EST_Unevaluated:
871 OS << " noexcept-unevaluated " << EPI.ExceptionSpecDecl;
873 case EST_Uninstantiated:
874 OS << " noexcept-uninstantiated " << EPI.ExceptionSpecTemplate;
879 bool OldMoreChildren = hasMoreChildren();
880 const FunctionTemplateSpecializationInfo *FTSI =
881 D->getTemplateSpecializationInfo();
882 bool HasTemplateSpecialization = FTSI;
884 bool HasNamedDecls = D->getDeclsInPrototypeScope().begin() !=
885 D->getDeclsInPrototypeScope().end();
887 bool HasFunctionDecls = D->param_begin() != D->param_end();
889 const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D);
890 bool HasCtorInitializers = C && C->init_begin() != C->init_end();
892 bool HasDeclarationBody = D->doesThisDeclarationHaveABody();
894 setMoreChildren(OldMoreChildren || HasNamedDecls || HasFunctionDecls ||
895 HasCtorInitializers || HasDeclarationBody);
896 if (HasTemplateSpecialization) {
898 dumpTemplateArgumentList(*FTSI->TemplateArguments);
901 setMoreChildren(OldMoreChildren || HasFunctionDecls ||
902 HasCtorInitializers || HasDeclarationBody);
903 for (ArrayRef<NamedDecl *>::iterator
904 I = D->getDeclsInPrototypeScope().begin(),
905 E = D->getDeclsInPrototypeScope().end(); I != E; ++I) {
911 setMoreChildren(OldMoreChildren || HasCtorInitializers || HasDeclarationBody);
912 for (FunctionDecl::param_const_iterator I = D->param_begin(),
920 setMoreChildren(OldMoreChildren || HasDeclarationBody);
921 if (HasCtorInitializers)
922 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
927 dumpCXXCtorInitializer(*I);
930 setMoreChildren(OldMoreChildren);
931 if (HasDeclarationBody) {
933 dumpStmt(D->getBody());
937 void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
939 dumpType(D->getType());
942 if (D->isModulePrivate())
943 OS << " __module_private__";
945 bool OldMoreChildren = hasMoreChildren();
946 bool IsBitField = D->isBitField();
947 Expr *Init = D->getInClassInitializer();
950 setMoreChildren(OldMoreChildren || HasInit);
953 dumpStmt(D->getBitWidth());
955 setMoreChildren(OldMoreChildren);
962 void ASTDumper::VisitVarDecl(const VarDecl *D) {
964 dumpType(D->getType());
965 StorageClass SC = D->getStorageClass();
967 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
968 switch (D->getTLSKind()) {
969 case VarDecl::TLS_None: break;
970 case VarDecl::TLS_Static: OS << " tls"; break;
971 case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
973 if (D->isModulePrivate())
974 OS << " __module_private__";
975 if (D->isNRVOVariable())
979 dumpStmt(D->getInit());
983 void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
985 dumpStmt(D->getAsmString());
988 void ASTDumper::VisitImportDecl(const ImportDecl *D) {
989 OS << ' ' << D->getImportedModule()->getFullModuleName();
992 //===----------------------------------------------------------------------===//
994 //===----------------------------------------------------------------------===//
996 void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
1000 if (!D->isOriginalNamespace())
1001 dumpDeclRef(D->getOriginalNamespace(), "original");
1004 void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
1006 dumpBareDeclRef(D->getNominatedNamespace());
1009 void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
1011 dumpDeclRef(D->getAliasedNamespace());
1014 void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
1016 dumpType(D->getUnderlyingType());
1019 void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
1021 dumpTemplateParameters(D->getTemplateParameters());
1022 dumpDecl(D->getTemplatedDecl());
1025 void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
1027 if (!D->isCompleteDefinition())
1030 for (CXXRecordDecl::base_class_const_iterator I = D->bases_begin(),
1033 IndentScope Indent(*this);
1036 dumpAccessSpecifier(I->getAccessSpecifier());
1037 dumpType(I->getType());
1038 if (I->isPackExpansion())
1043 void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
1044 dumpStmt(D->getAssertExpr());
1046 dumpStmt(D->getMessage());
1049 void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
1051 dumpTemplateParameters(D->getTemplateParameters());
1052 dumpDecl(D->getTemplatedDecl());
1053 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(),
1056 FunctionTemplateDecl::spec_iterator Next = I;
1060 switch (I->getTemplateSpecializationKind()) {
1061 case TSK_Undeclared:
1062 case TSK_ImplicitInstantiation:
1063 case TSK_ExplicitInstantiationDeclaration:
1064 case TSK_ExplicitInstantiationDefinition:
1065 if (D == D->getCanonicalDecl())
1070 case TSK_ExplicitSpecialization:
1077 void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
1079 dumpTemplateParameters(D->getTemplateParameters());
1081 ClassTemplateDecl::spec_iterator I = D->spec_begin();
1082 ClassTemplateDecl::spec_iterator E = D->spec_end();
1085 dumpDecl(D->getTemplatedDecl());
1086 for (; I != E; ++I) {
1087 ClassTemplateDecl::spec_iterator Next = I;
1091 switch (I->getTemplateSpecializationKind()) {
1092 case TSK_Undeclared:
1093 case TSK_ImplicitInstantiation:
1094 if (D == D->getCanonicalDecl())
1099 case TSK_ExplicitSpecialization:
1100 case TSK_ExplicitInstantiationDeclaration:
1101 case TSK_ExplicitInstantiationDefinition:
1108 void ASTDumper::VisitClassTemplateSpecializationDecl(
1109 const ClassTemplateSpecializationDecl *D) {
1110 VisitCXXRecordDecl(D);
1111 dumpTemplateArgumentList(D->getTemplateArgs());
1114 void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
1115 const ClassTemplatePartialSpecializationDecl *D) {
1116 VisitClassTemplateSpecializationDecl(D);
1117 dumpTemplateParameters(D->getTemplateParameters());
1120 void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
1121 const ClassScopeFunctionSpecializationDecl *D) {
1122 dumpDeclRef(D->getSpecialization());
1123 if (D->hasExplicitTemplateArgs())
1124 dumpTemplateArgumentListInfo(D->templateArgs());
1127 void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
1129 dumpTemplateParameters(D->getTemplateParameters());
1131 VarTemplateDecl::spec_iterator I = D->spec_begin();
1132 VarTemplateDecl::spec_iterator E = D->spec_end();
1135 dumpDecl(D->getTemplatedDecl());
1136 for (; I != E; ++I) {
1137 VarTemplateDecl::spec_iterator Next = I;
1141 switch (I->getTemplateSpecializationKind()) {
1142 case TSK_Undeclared:
1143 case TSK_ImplicitInstantiation:
1144 if (D == D->getCanonicalDecl())
1149 case TSK_ExplicitSpecialization:
1150 case TSK_ExplicitInstantiationDeclaration:
1151 case TSK_ExplicitInstantiationDefinition:
1158 void ASTDumper::VisitVarTemplateSpecializationDecl(
1159 const VarTemplateSpecializationDecl *D) {
1160 dumpTemplateArgumentList(D->getTemplateArgs());
1164 void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
1165 const VarTemplatePartialSpecializationDecl *D) {
1166 dumpTemplateParameters(D->getTemplateParameters());
1167 VisitVarTemplateSpecializationDecl(D);
1170 void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
1171 if (D->wasDeclaredWithTypename())
1175 if (D->isParameterPack())
1178 if (D->hasDefaultArgument())
1179 dumpType(D->getDefaultArgument());
1182 void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
1183 dumpType(D->getType());
1184 if (D->isParameterPack())
1187 if (D->hasDefaultArgument())
1188 dumpStmt(D->getDefaultArgument());
1191 void ASTDumper::VisitTemplateTemplateParmDecl(
1192 const TemplateTemplateParmDecl *D) {
1193 if (D->isParameterPack())
1196 dumpTemplateParameters(D->getTemplateParameters());
1197 if (D->hasDefaultArgument())
1198 dumpTemplateArgumentLoc(D->getDefaultArgument());
1201 void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
1203 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1204 OS << D->getNameAsString();
1207 void ASTDumper::VisitUnresolvedUsingTypenameDecl(
1208 const UnresolvedUsingTypenameDecl *D) {
1210 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1211 OS << D->getNameAsString();
1214 void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
1216 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1217 OS << D->getNameAsString();
1218 dumpType(D->getType());
1221 void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
1223 dumpBareDeclRef(D->getTargetDecl());
1226 void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
1227 switch (D->getLanguage()) {
1228 case LinkageSpecDecl::lang_c: OS << " C"; break;
1229 case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1233 void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
1235 dumpAccessSpecifier(D->getAccess());
1238 void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
1240 if (TypeSourceInfo *T = D->getFriendType())
1241 dumpType(T->getType());
1243 dumpDecl(D->getFriendDecl());
1246 //===----------------------------------------------------------------------===//
1247 // Obj-C Declarations
1248 //===----------------------------------------------------------------------===//
1250 void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
1252 dumpType(D->getType());
1253 if (D->getSynthesize())
1254 OS << " synthesize";
1255 if (D->getBackingIvarReferencedInAccessor())
1256 OS << " BackingIvarReferencedInAccessor";
1258 switch (D->getAccessControl()) {
1259 case ObjCIvarDecl::None:
1262 case ObjCIvarDecl::Private:
1265 case ObjCIvarDecl::Protected:
1268 case ObjCIvarDecl::Public:
1271 case ObjCIvarDecl::Package:
1277 void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
1278 if (D->isInstanceMethod())
1283 dumpType(D->getResultType());
1285 bool OldMoreChildren = hasMoreChildren();
1286 bool IsVariadic = D->isVariadic();
1287 bool HasBody = D->hasBody();
1289 setMoreChildren(OldMoreChildren || IsVariadic || HasBody);
1290 if (D->isThisDeclarationADefinition()) {
1294 for (ObjCMethodDecl::param_const_iterator I = D->param_begin(),
1303 setMoreChildren(OldMoreChildren || HasBody);
1306 IndentScope Indent(*this);
1310 setMoreChildren(OldMoreChildren);
1313 dumpStmt(D->getBody());
1317 void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
1319 dumpDeclRef(D->getClassInterface());
1320 if (D->protocol_begin() == D->protocol_end())
1322 dumpDeclRef(D->getImplementation());
1323 for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
1324 E = D->protocol_end();
1332 void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
1334 dumpDeclRef(D->getClassInterface());
1336 dumpDeclRef(D->getCategoryDecl());
1339 void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
1341 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
1342 E = D->protocol_end();
1350 void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
1352 dumpDeclRef(D->getSuperClass(), "super");
1353 if (D->protocol_begin() == D->protocol_end())
1355 dumpDeclRef(D->getImplementation());
1356 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
1357 E = D->protocol_end();
1365 void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
1367 dumpDeclRef(D->getSuperClass(), "super");
1368 if (D->init_begin() == D->init_end())
1370 dumpDeclRef(D->getClassInterface());
1371 for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
1376 dumpCXXCtorInitializer(*I);
1380 void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
1383 dumpDeclRef(D->getClassInterface());
1386 void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
1388 dumpType(D->getType());
1390 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1392 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1395 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1396 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1397 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1399 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1401 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1403 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1405 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1407 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1409 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1411 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1413 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1415 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1416 OS << " unsafe_unretained";
1417 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter) {
1418 if (!(Attrs & ObjCPropertyDecl::OBJC_PR_setter))
1420 dumpDeclRef(D->getGetterMethodDecl(), "getter");
1422 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter) {
1424 dumpDeclRef(D->getSetterMethodDecl(), "setter");
1429 void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
1430 dumpName(D->getPropertyDecl());
1431 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1432 OS << " synthesize";
1435 dumpDeclRef(D->getPropertyDecl());
1437 dumpDeclRef(D->getPropertyIvarDecl());
1440 void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
1441 for (BlockDecl::param_const_iterator I = D->param_begin(), E = D->param_end();
1445 if (D->isVariadic()) {
1446 IndentScope Indent(*this);
1450 if (D->capturesCXXThis()) {
1451 IndentScope Indent(*this);
1452 OS << "capture this";
1454 for (BlockDecl::capture_iterator I = D->capture_begin(), E = D->capture_end();
1456 IndentScope Indent(*this);
1462 if (I->getVariable()) {
1464 dumpBareDeclRef(I->getVariable());
1466 if (I->hasCopyExpr())
1467 dumpStmt(I->getCopyExpr());
1470 dumpStmt(D->getBody());
1473 //===----------------------------------------------------------------------===//
1474 // Stmt dumping methods.
1475 //===----------------------------------------------------------------------===//
1477 void ASTDumper::dumpStmt(const Stmt *S) {
1478 IndentScope Indent(*this);
1481 ColorScope Color(*this, NullColor);
1486 if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
1491 setMoreChildren(!S->children().empty());
1492 ConstStmtVisitor<ASTDumper>::Visit(S);
1493 setMoreChildren(false);
1494 for (Stmt::const_child_range CI = S->children(); CI; ++CI) {
1495 Stmt::const_child_range Next = CI;
1503 void ASTDumper::VisitStmt(const Stmt *Node) {
1505 ColorScope Color(*this, StmtColor);
1506 OS << Node->getStmtClassName();
1509 dumpSourceRange(Node->getSourceRange());
1512 void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
1514 for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
1515 E = Node->decl_end();
1523 void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
1525 for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
1526 E = Node->getAttrs().end();
1534 void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
1536 OS << " '" << Node->getName() << "'";
1539 void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
1541 OS << " '" << Node->getLabel()->getName() << "'";
1542 dumpPointer(Node->getLabel());
1545 void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
1547 dumpDecl(Node->getExceptionDecl());
1550 //===----------------------------------------------------------------------===//
1551 // Expr dumping methods.
1552 //===----------------------------------------------------------------------===//
1554 void ASTDumper::VisitExpr(const Expr *Node) {
1556 dumpType(Node->getType());
1559 ColorScope Color(*this, ValueKindColor);
1560 switch (Node->getValueKind()) {
1573 ColorScope Color(*this, ObjectKindColor);
1574 switch (Node->getObjectKind()) {
1580 case OK_ObjCProperty:
1581 OS << " objcproperty";
1583 case OK_ObjCSubscript:
1584 OS << " objcsubscript";
1586 case OK_VectorComponent:
1587 OS << " vectorcomponent";
1593 static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
1594 if (Node->path_empty())
1599 for (CastExpr::path_const_iterator I = Node->path_begin(),
1600 E = Node->path_end();
1602 const CXXBaseSpecifier *Base = *I;
1606 const CXXRecordDecl *RD =
1607 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1609 if (Base->isVirtual())
1611 OS << RD->getName();
1618 void ASTDumper::VisitCastExpr(const CastExpr *Node) {
1622 ColorScope Color(*this, CastColor);
1623 OS << Node->getCastKindName();
1625 dumpBasePath(OS, Node);
1629 void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
1633 dumpBareDeclRef(Node->getDecl());
1634 if (Node->getDecl() != Node->getFoundDecl()) {
1636 dumpBareDeclRef(Node->getFoundDecl());
1641 void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
1644 if (!Node->requiresADL())
1646 OS << "ADL) = '" << Node->getName() << '\'';
1648 UnresolvedLookupExpr::decls_iterator
1649 I = Node->decls_begin(), E = Node->decls_end();
1656 void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
1660 ColorScope Color(*this, DeclKindNameColor);
1661 OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
1663 OS << "='" << *Node->getDecl() << "'";
1664 dumpPointer(Node->getDecl());
1665 if (Node->isFreeIvar())
1666 OS << " isFreeIvar";
1669 void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
1671 switch (Node->getIdentType()) {
1672 default: llvm_unreachable("unknown case");
1673 case PredefinedExpr::Func: OS << " __func__"; break;
1674 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
1675 case PredefinedExpr::FuncDName: OS << " __FUNCDNAME__"; break;
1676 case PredefinedExpr::LFunction: OS << " L__FUNCTION__"; break;
1677 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
1681 void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
1683 ColorScope Color(*this, ValueColor);
1684 OS << " " << Node->getValue();
1687 void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
1690 bool isSigned = Node->getType()->isSignedIntegerType();
1691 ColorScope Color(*this, ValueColor);
1692 OS << " " << Node->getValue().toString(10, isSigned);
1695 void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
1697 ColorScope Color(*this, ValueColor);
1698 OS << " " << Node->getValueAsApproximateDouble();
1701 void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
1703 ColorScope Color(*this, ValueColor);
1705 Str->outputString(OS);
1708 void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
1710 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
1711 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
1714 void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
1715 const UnaryExprOrTypeTraitExpr *Node) {
1717 switch(Node->getKind()) {
1728 if (Node->isArgumentType())
1729 dumpType(Node->getArgumentType());
1732 void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
1734 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
1735 dumpPointer(Node->getMemberDecl());
1738 void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
1740 OS << " " << Node->getAccessor().getNameStart();
1743 void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
1745 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
1748 void ASTDumper::VisitCompoundAssignOperator(
1749 const CompoundAssignOperator *Node) {
1751 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
1752 << "' ComputeLHSTy=";
1753 dumpBareType(Node->getComputationLHSType());
1754 OS << " ComputeResultTy=";
1755 dumpBareType(Node->getComputationResultType());
1758 void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
1760 dumpDecl(Node->getBlockDecl());
1763 void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
1766 if (Expr *Source = Node->getSourceExpr()) {
1774 void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
1776 OS << " " << Node->getLabel()->getName();
1777 dumpPointer(Node->getLabel());
1780 //===----------------------------------------------------------------------===//
1782 //===----------------------------------------------------------------------===//
1784 void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
1786 OS << " " << Node->getCastName()
1787 << "<" << Node->getTypeAsWritten().getAsString() << ">"
1788 << " <" << Node->getCastKindName();
1789 dumpBasePath(OS, Node);
1793 void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
1795 OS << " " << (Node->getValue() ? "true" : "false");
1798 void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
1803 void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
1805 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
1806 << " <" << Node->getCastKindName() << ">";
1809 void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
1811 CXXConstructorDecl *Ctor = Node->getConstructor();
1812 dumpType(Ctor->getType());
1813 if (Node->isElidable())
1815 if (Node->requiresZeroInitialization())
1819 void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
1822 dumpCXXTemporary(Node->getTemporary());
1826 ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
1828 if (const ValueDecl *VD = Node->getExtendingDecl()) {
1829 OS << " extended by ";
1830 dumpBareDeclRef(VD);
1834 void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
1836 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
1837 dumpDeclRef(Node->getObject(i), "cleanup");
1840 void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
1841 OS << "(CXXTemporary";
1842 dumpPointer(Temporary);
1846 //===----------------------------------------------------------------------===//
1847 // Obj-C Expressions
1848 //===----------------------------------------------------------------------===//
1850 void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
1852 OS << " selector=" << Node->getSelector().getAsString();
1853 switch (Node->getReceiverKind()) {
1854 case ObjCMessageExpr::Instance:
1857 case ObjCMessageExpr::Class:
1859 dumpBareType(Node->getClassReceiver());
1862 case ObjCMessageExpr::SuperInstance:
1863 OS << " super (instance)";
1866 case ObjCMessageExpr::SuperClass:
1867 OS << " super (class)";
1872 void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
1874 OS << " selector=" << Node->getBoxingMethod()->getSelector().getAsString();
1877 void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
1879 if (const VarDecl *CatchParam = Node->getCatchParamDecl())
1880 dumpDecl(CatchParam);
1885 void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
1887 dumpType(Node->getEncodedType());
1890 void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
1893 OS << " " << Node->getSelector().getAsString();
1896 void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
1899 OS << ' ' << *Node->getProtocol();
1902 void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
1904 if (Node->isImplicitProperty()) {
1905 OS << " Kind=MethodRef Getter=\"";
1906 if (Node->getImplicitPropertyGetter())
1907 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
1911 OS << "\" Setter=\"";
1912 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
1913 OS << Setter->getSelector().getAsString();
1918 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
1921 if (Node->isSuperReceiver())
1924 OS << " Messaging=";
1925 if (Node->isMessagingGetter() && Node->isMessagingSetter())
1926 OS << "Getter&Setter";
1927 else if (Node->isMessagingGetter())
1929 else if (Node->isMessagingSetter())
1933 void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
1935 if (Node->isArraySubscriptRefExpr())
1936 OS << " Kind=ArraySubscript GetterForArray=\"";
1938 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
1939 if (Node->getAtIndexMethodDecl())
1940 OS << Node->getAtIndexMethodDecl()->getSelector().getAsString();
1944 if (Node->isArraySubscriptRefExpr())
1945 OS << "\" SetterForArray=\"";
1947 OS << "\" SetterForDictionary=\"";
1948 if (Node->setAtIndexMethodDecl())
1949 OS << Node->setAtIndexMethodDecl()->getSelector().getAsString();
1954 void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
1956 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
1959 //===----------------------------------------------------------------------===//
1961 //===----------------------------------------------------------------------===//
1963 const char *ASTDumper::getCommandName(unsigned CommandID) {
1965 return Traits->getCommandInfo(CommandID)->Name;
1966 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
1969 return "<not a builtin command>";
1972 void ASTDumper::dumpFullComment(const FullComment *C) {
1981 void ASTDumper::dumpComment(const Comment *C) {
1982 IndentScope Indent(*this);
1985 ColorScope Color(*this, NullColor);
1991 ColorScope Color(*this, CommentColor);
1992 OS << C->getCommentKindName();
1995 dumpSourceRange(C->getSourceRange());
1996 ConstCommentVisitor<ASTDumper>::visit(C);
1997 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
2005 void ASTDumper::visitTextComment(const TextComment *C) {
2006 OS << " Text=\"" << C->getText() << "\"";
2009 void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
2010 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2011 switch (C->getRenderKind()) {
2012 case InlineCommandComment::RenderNormal:
2013 OS << " RenderNormal";
2015 case InlineCommandComment::RenderBold:
2016 OS << " RenderBold";
2018 case InlineCommandComment::RenderMonospaced:
2019 OS << " RenderMonospaced";
2021 case InlineCommandComment::RenderEmphasized:
2022 OS << " RenderEmphasized";
2026 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2027 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2030 void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
2031 OS << " Name=\"" << C->getTagName() << "\"";
2032 if (C->getNumAttrs() != 0) {
2034 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
2035 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
2036 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
2039 if (C->isSelfClosing())
2040 OS << " SelfClosing";
2043 void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
2044 OS << " Name=\"" << C->getTagName() << "\"";
2047 void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
2048 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2049 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2050 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2053 void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
2054 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
2056 if (C->isDirectionExplicit())
2057 OS << " explicitly";
2059 OS << " implicitly";
2061 if (C->hasParamName()) {
2062 if (C->isParamIndexValid())
2063 OS << " Param=\"" << C->getParamName(FC) << "\"";
2065 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2068 if (C->isParamIndexValid())
2069 OS << " ParamIndex=" << C->getParamIndex();
2072 void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
2073 if (C->hasParamName()) {
2074 if (C->isPositionValid())
2075 OS << " Param=\"" << C->getParamName(FC) << "\"";
2077 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2080 if (C->isPositionValid()) {
2081 OS << " Position=<";
2082 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
2083 OS << C->getIndex(i);
2091 void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
2092 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
2093 " CloseName=\"" << C->getCloseName() << "\"";
2096 void ASTDumper::visitVerbatimBlockLineComment(
2097 const VerbatimBlockLineComment *C) {
2098 OS << " Text=\"" << C->getText() << "\"";
2101 void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
2102 OS << " Text=\"" << C->getText() << "\"";
2105 //===----------------------------------------------------------------------===//
2106 // Decl method implementations
2107 //===----------------------------------------------------------------------===//
2109 void Decl::dump() const {
2113 void Decl::dump(raw_ostream &OS) const {
2114 ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
2115 &getASTContext().getSourceManager());
2119 void Decl::dumpColor() const {
2120 ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
2121 &getASTContext().getSourceManager(), /*ShowColors*/true);
2125 void DeclContext::dumpLookups() const {
2126 dumpLookups(llvm::errs());
2129 void DeclContext::dumpLookups(raw_ostream &OS) const {
2130 const DeclContext *DC = this;
2131 while (!DC->isTranslationUnit())
2132 DC = DC->getParent();
2133 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
2134 ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
2135 P.dumpLookups(this);
2138 //===----------------------------------------------------------------------===//
2139 // Stmt method implementations
2140 //===----------------------------------------------------------------------===//
2142 void Stmt::dump(SourceManager &SM) const {
2143 dump(llvm::errs(), SM);
2146 void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
2147 ASTDumper P(OS, 0, &SM);
2151 void Stmt::dump() const {
2152 ASTDumper P(llvm::errs(), 0, 0);
2156 void Stmt::dumpColor() const {
2157 ASTDumper P(llvm::errs(), 0, 0, /*ShowColors*/true);
2161 //===----------------------------------------------------------------------===//
2162 // Comment method implementations
2163 //===----------------------------------------------------------------------===//
2165 void Comment::dump() const {
2166 dump(llvm::errs(), 0, 0);
2169 void Comment::dump(const ASTContext &Context) const {
2170 dump(llvm::errs(), &Context.getCommentCommandTraits(),
2171 &Context.getSourceManager());
2174 void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
2175 const SourceManager *SM) const {
2176 const FullComment *FC = dyn_cast<FullComment>(this);
2177 ASTDumper D(OS, Traits, SM);
2178 D.dumpFullComment(FC);
2181 void Comment::dumpColor() const {
2182 const FullComment *FC = dyn_cast<FullComment>(this);
2183 ASTDumper D(llvm::errs(), 0, 0, /*ShowColors*/true);
2184 D.dumpFullComment(FC);