blob: 7442959201b2af6cd550d3b55816f0a27cb981ee [file] [log] [blame]
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001//===--- ASTDumper.cpp - Dumping implementation for ASTs ------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner6000dac2007-08-08 22:51:59 +00007//
8//===----------------------------------------------------------------------===//
9//
Alexander Kornienko40b66a002012-12-13 13:59:55 +000010// This file implements the AST dump methods, which dump out the
Chris Lattner6000dac2007-08-08 22:51:59 +000011// AST in a form that exposes type details and other fields.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "clang/AST/ASTContext.h"
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +000016#include "clang/AST/Attr.h"
Alexander Kornienkoacd356e2013-01-14 14:07:11 +000017#include "clang/AST/CommentVisitor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
Alexander Kornienkod538ed92012-12-20 02:09:13 +000020#include "clang/AST/DeclVisitor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/AST/StmtVisitor.h"
Alexander Kornienkod538ed92012-12-20 02:09:13 +000022#include "clang/Basic/Module.h"
Chris Lattnere300c872007-08-30 06:17:34 +000023#include "clang/Basic/SourceManager.h"
Daniel Dunbar806c12e2009-12-03 09:13:13 +000024#include "llvm/Support/raw_ostream.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000025using namespace clang;
Alexander Kornienkoacd356e2013-01-14 14:07:11 +000026using namespace clang::comments;
Chris Lattner6000dac2007-08-08 22:51:59 +000027
28//===----------------------------------------------------------------------===//
Alexander Kornienko40b66a002012-12-13 13:59:55 +000029// ASTDumper Visitor
Chris Lattner6000dac2007-08-08 22:51:59 +000030//===----------------------------------------------------------------------===//
31
32namespace {
Alexander Kornienkod538ed92012-12-20 02:09:13 +000033 class ASTDumper
Alexander Kornienkoacd356e2013-01-14 14:07:11 +000034 : public DeclVisitor<ASTDumper>, public StmtVisitor<ASTDumper>,
35 public ConstCommentVisitor<ASTDumper> {
Chris Lattner5f9e2722011-07-23 10:55:15 +000036 raw_ostream &OS;
Alexander Kornienkoacd356e2013-01-14 14:07:11 +000037 const CommandTraits *Traits;
38 const SourceManager *SM;
Chris Lattner6000dac2007-08-08 22:51:59 +000039 unsigned IndentLevel;
Manuel Klimekcb7b45e2012-11-07 00:33:12 +000040 bool IsFirstLine;
Mike Stump1eb44332009-09-09 15:08:12 +000041
Alexander Kornienko21c8b192012-12-11 15:28:09 +000042 /// Keep track of the last location we print out so that we can
43 /// print out deltas from then on out.
Chris Lattnere300c872007-08-30 06:17:34 +000044 const char *LastLocFilename;
45 unsigned LastLocLine;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000046
Alexander Kornienkoacd356e2013-01-14 14:07:11 +000047 /// The \c FullComment parent of the comment being dumped.
48 const FullComment *FC;
49
Manuel Klimekcb7b45e2012-11-07 00:33:12 +000050 class IndentScope {
Alexander Kornienko40b66a002012-12-13 13:59:55 +000051 ASTDumper &Dumper;
Manuel Klimekcb7b45e2012-11-07 00:33:12 +000052 public:
Alexander Kornienko40b66a002012-12-13 13:59:55 +000053 IndentScope(ASTDumper &Dumper) : Dumper(Dumper) {
Manuel Klimekcb7b45e2012-11-07 00:33:12 +000054 Dumper.indent();
55 }
56 ~IndentScope() {
57 Dumper.unindent();
58 }
59 };
60
Chris Lattner6000dac2007-08-08 22:51:59 +000061 public:
Alexander Kornienkoacd356e2013-01-14 14:07:11 +000062 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
63 const SourceManager *SM)
64 : OS(OS), Traits(Traits), SM(SM), IndentLevel(0), IsFirstLine(true),
65 LastLocFilename(""), LastLocLine(~0U), FC(0) { }
Mike Stump1eb44332009-09-09 15:08:12 +000066
Alexander Kornienko40b66a002012-12-13 13:59:55 +000067 ~ASTDumper() {
Manuel Klimekcb7b45e2012-11-07 00:33:12 +000068 OS << "\n";
69 }
70
Alexander Kornienkod5bc3592012-12-11 15:20:44 +000071 void dumpDecl(Decl *D);
72 void dumpStmt(Stmt *S);
Alexander Kornienkoacd356e2013-01-14 14:07:11 +000073 void dumpFullComment(const FullComment *C);
Mike Stump1eb44332009-09-09 15:08:12 +000074
Alexander Kornienko21c8b192012-12-11 15:28:09 +000075 // Utilities
76 void indent();
77 void unindent();
Alexander Kornienkod538ed92012-12-20 02:09:13 +000078 void dumpPointer(const void *Ptr);
79 void dumpSourceRange(SourceRange R);
Alexander Kornienko21c8b192012-12-11 15:28:09 +000080 void dumpLocation(SourceLocation Loc);
Alexander Kornienkod538ed92012-12-20 02:09:13 +000081 void dumpBareType(QualType T);
Alexander Kornienko21c8b192012-12-11 15:28:09 +000082 void dumpType(QualType T);
Alexander Kornienkoad7bb362012-12-20 11:08:38 +000083 void dumpBareDeclRef(const Decl *Node);
Alexander Kornienkoc9394532012-12-20 12:23:54 +000084 void dumpDeclRef(const Decl *Node, const char *Label = 0);
Alexander Kornienkoad7bb362012-12-20 11:08:38 +000085 void dumpName(const NamedDecl *D);
86 void dumpDeclContext(const DeclContext *DC);
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +000087 void dumpAttr(const Attr *A);
Alexander Kornienkod538ed92012-12-20 02:09:13 +000088
89 // C++ Utilities
90 void dumpAccessSpecifier(AccessSpecifier AS);
Alexander Kornienkoad7bb362012-12-20 11:08:38 +000091 void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
92 void dumpTemplateParameters(const TemplateParameterList *TPL);
Alexander Kornienkod538ed92012-12-20 02:09:13 +000093 void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
94 void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
95 void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
96 void dumpTemplateArgument(const TemplateArgument &A,
97 SourceRange R = SourceRange());
98
99 // Decls
100 void VisitLabelDecl(LabelDecl *D);
101 void VisitTypedefDecl(TypedefDecl *D);
102 void VisitEnumDecl(EnumDecl *D);
103 void VisitRecordDecl(RecordDecl *D);
104 void VisitEnumConstantDecl(EnumConstantDecl *D);
105 void VisitIndirectFieldDecl(IndirectFieldDecl *D);
106 void VisitFunctionDecl(FunctionDecl *D);
107 void VisitFieldDecl(FieldDecl *D);
108 void VisitVarDecl(VarDecl *D);
109 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
110 void VisitImportDecl(ImportDecl *D);
111
112 // C++ Decls
113 void VisitNamespaceDecl(NamespaceDecl *D);
114 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
115 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
116 void VisitTypeAliasDecl(TypeAliasDecl *D);
117 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
118 void VisitCXXRecordDecl(CXXRecordDecl *D);
119 void VisitStaticAssertDecl(StaticAssertDecl *D);
120 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
121 void VisitClassTemplateDecl(ClassTemplateDecl *D);
122 void VisitClassTemplateSpecializationDecl(
123 ClassTemplateSpecializationDecl *D);
124 void VisitClassTemplatePartialSpecializationDecl(
125 ClassTemplatePartialSpecializationDecl *D);
126 void VisitClassScopeFunctionSpecializationDecl(
127 ClassScopeFunctionSpecializationDecl *D);
128 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
129 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
130 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
131 void VisitUsingDecl(UsingDecl *D);
132 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
133 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
134 void VisitUsingShadowDecl(UsingShadowDecl *D);
135 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
136 void VisitAccessSpecDecl(AccessSpecDecl *D);
137 void VisitFriendDecl(FriendDecl *D);
138
139 // ObjC Decls
140 void VisitObjCIvarDecl(ObjCIvarDecl *D);
141 void VisitObjCMethodDecl(ObjCMethodDecl *D);
142 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
143 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
144 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
145 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
146 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
147 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
148 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
149 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
150 void VisitBlockDecl(BlockDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Chris Lattner17a1a722007-08-30 01:00:35 +0000152 // Stmts.
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000153 void VisitStmt(Stmt *Node);
Ted Kremenek5399ce22007-12-12 06:59:42 +0000154 void VisitDeclStmt(DeclStmt *Node);
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000155 void VisitAttributedStmt(AttributedStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000156 void VisitLabelStmt(LabelStmt *Node);
157 void VisitGotoStmt(GotoStmt *Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Chris Lattner17a1a722007-08-30 01:00:35 +0000159 // Exprs
160 void VisitExpr(Expr *Node);
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000161 void VisitCastExpr(CastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000162 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattnerd9f69102008-08-10 01:53:14 +0000163 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000164 void VisitCharacterLiteral(CharacterLiteral *Node);
165 void VisitIntegerLiteral(IntegerLiteral *Node);
166 void VisitFloatingLiteral(FloatingLiteral *Node);
167 void VisitStringLiteral(StringLiteral *Str);
168 void VisitUnaryOperator(UnaryOperator *Node);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000169 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000170 void VisitMemberExpr(MemberExpr *Node);
Nate Begeman213541a2008-04-18 23:10:10 +0000171 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000172 void VisitBinaryOperator(BinaryOperator *Node);
173 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
174 void VisitAddrLabelExpr(AddrLabelExpr *Node);
John McCall6b5a61b2011-02-07 10:33:21 +0000175 void VisitBlockExpr(BlockExpr *Node);
John McCall4b9c2d22011-11-06 09:01:30 +0000176 void VisitOpaqueValueExpr(OpaqueValueExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000177
178 // C++
Douglas Gregor49badde2008-10-27 19:41:14 +0000179 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000180 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000181 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000182 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000183 void VisitCXXConstructExpr(CXXConstructExpr *Node);
184 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node);
John McCall4765fa02010-12-06 08:20:24 +0000185 void VisitExprWithCleanups(ExprWithCleanups *Node);
John McCall9d5f35e2009-12-11 21:50:11 +0000186 void VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node);
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000187 void dumpCXXTemporary(CXXTemporary *Temporary);
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattner17a1a722007-08-30 01:00:35 +0000189 // ObjC
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000190 void VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000191 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000192 void VisitObjCMessageExpr(ObjCMessageExpr *Node);
193 void VisitObjCBoxedExpr(ObjCBoxedExpr *Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000194 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000195 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000196 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000197 void VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000198 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000199 void VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node);
Alexander Kornienkoacd356e2013-01-14 14:07:11 +0000200
201 // Comments.
202 const char *getCommandName(unsigned CommandID);
203 void dumpComment(const Comment *C);
204
205 // Inline comments.
206 void visitTextComment(const TextComment *C);
207 void visitInlineCommandComment(const InlineCommandComment *C);
208 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
209 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
210
211 // Block comments.
212 void visitBlockCommandComment(const BlockCommandComment *C);
213 void visitParamCommandComment(const ParamCommandComment *C);
214 void visitTParamCommandComment(const TParamCommandComment *C);
215 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
216 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
217 void visitVerbatimLineComment(const VerbatimLineComment *C);
Chris Lattner6000dac2007-08-08 22:51:59 +0000218 };
219}
220
221//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000222// Utilities
223//===----------------------------------------------------------------------===//
224
Alexander Kornienko40b66a002012-12-13 13:59:55 +0000225void ASTDumper::indent() {
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000226 if (IsFirstLine)
227 IsFirstLine = false;
228 else
229 OS << "\n";
230 OS.indent(IndentLevel * 2);
231 OS << "(";
232 IndentLevel++;
233}
234
Alexander Kornienko40b66a002012-12-13 13:59:55 +0000235void ASTDumper::unindent() {
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000236 OS << ")";
237 IndentLevel--;
238}
239
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000240void ASTDumper::dumpPointer(const void *Ptr) {
241 OS << ' ' << Ptr;
242}
243
Alexander Kornienko40b66a002012-12-13 13:59:55 +0000244void ASTDumper::dumpLocation(SourceLocation Loc) {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000245 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Chris Lattnere300c872007-08-30 06:17:34 +0000247 // The general format we print out is filename:line:col, but we drop pieces
248 // that haven't changed since the last loc printed.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000249 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
250
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000251 if (PLoc.isInvalid()) {
252 OS << "<invalid sloc>";
253 return;
254 }
255
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000256 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000257 OS << PLoc.getFilename() << ':' << PLoc.getLine()
258 << ':' << PLoc.getColumn();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000259 LastLocFilename = PLoc.getFilename();
260 LastLocLine = PLoc.getLine();
261 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000262 OS << "line" << ':' << PLoc.getLine()
263 << ':' << PLoc.getColumn();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000264 LastLocLine = PLoc.getLine();
Chris Lattnere300c872007-08-30 06:17:34 +0000265 } else {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000266 OS << "col" << ':' << PLoc.getColumn();
Chris Lattnere300c872007-08-30 06:17:34 +0000267 }
268}
269
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000270void ASTDumper::dumpSourceRange(SourceRange R) {
Chris Lattnere300c872007-08-30 06:17:34 +0000271 // Can't translate locations if a SourceManager isn't available.
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000272 if (!SM)
273 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000275 OS << " <";
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000276 dumpLocation(R.getBegin());
Chris Lattner311ff022007-10-16 22:36:42 +0000277 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000278 OS << ", ";
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000279 dumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000280 }
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000281 OS << ">";
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Chris Lattnere300c872007-08-30 06:17:34 +0000283 // <t2.c:123:421[blah], t2.c:412:321>
284
285}
286
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000287void ASTDumper::dumpBareType(QualType T) {
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000288 SplitQualType T_split = T.split();
289 OS << "'" << QualType::getAsString(T_split) << "'";
290
291 if (!T.isNull()) {
292 // If the type is sugared, also dump a (shallow) desugared type.
293 SplitQualType D_split = T.getSplitDesugaredType();
294 if (T_split != D_split)
295 OS << ":'" << QualType::getAsString(D_split) << "'";
296 }
297}
298
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000299void ASTDumper::dumpType(QualType T) {
300 OS << ' ';
301 dumpBareType(T);
302}
303
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000304void ASTDumper::dumpBareDeclRef(const Decl *D) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000305 OS << D->getDeclKindName();
306 dumpPointer(D);
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000307
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000308 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000309 OS << " '";
310 ND->getDeclName().printName(OS);
311 OS << "'";
312 }
313
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000314 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000315 dumpType(VD->getType());
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000316}
317
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000318void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000319 if (!D)
320 return;
321
322 IndentScope Indent(*this);
323 if (Label)
324 OS << Label << ' ';
325 dumpBareDeclRef(D);
326}
327
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000328void ASTDumper::dumpName(const NamedDecl *ND) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000329 if (ND->getDeclName())
330 OS << ' ' << ND->getNameAsString();
331}
332
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000333void ASTDumper::dumpDeclContext(const DeclContext *DC) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000334 if (!DC)
335 return;
336 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
337 I != E; ++I)
338 dumpDecl(*I);
339}
340
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000341void ASTDumper::dumpAttr(const Attr *A) {
342 IndentScope Indent(*this);
343 switch (A->getKind()) {
344#define ATTR(X) case attr::X: OS << #X; break;
345#include "clang/Basic/AttrList.inc"
346 default: llvm_unreachable("unexpected attribute kind");
347 }
348 OS << "Attr";
349 dumpPointer(A);
350 dumpSourceRange(A->getRange());
351#include "clang/AST/AttrDump.inc"
352}
353
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000354//===----------------------------------------------------------------------===//
355// C++ Utilities
356//===----------------------------------------------------------------------===//
357
358void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
359 switch (AS) {
360 case AS_none:
361 break;
362 case AS_public:
363 OS << "public";
364 break;
365 case AS_protected:
366 OS << "protected";
367 break;
368 case AS_private:
369 OS << "private";
370 break;
371 }
372}
373
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000374void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000375 IndentScope Indent(*this);
376 OS << "CXXCtorInitializer";
377 if (Init->isAnyMemberInitializer()) {
378 OS << ' ';
379 dumpBareDeclRef(Init->getAnyMember());
380 } else {
381 dumpType(QualType(Init->getBaseClass(), 0));
382 }
383 dumpStmt(Init->getInit());
384}
385
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000386void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000387 if (!TPL)
388 return;
389
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000390 for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000391 I != E; ++I)
392 dumpDecl(*I);
393}
394
395void ASTDumper::dumpTemplateArgumentListInfo(
396 const TemplateArgumentListInfo &TALI) {
397 for (unsigned i = 0, e = TALI.size(); i < e; ++i)
398 dumpTemplateArgumentLoc(TALI[i]);
399}
400
401void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
402 dumpTemplateArgument(A.getArgument(), A.getSourceRange());
403}
404
405void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
406 for (unsigned i = 0, e = TAL.size(); i < e; ++i)
407 dumpTemplateArgument(TAL[i]);
408}
409
410void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
411 IndentScope Indent(*this);
412 OS << "TemplateArgument";
413 if (R.isValid())
414 dumpSourceRange(R);
415
416 switch (A.getKind()) {
417 case TemplateArgument::Null:
418 OS << " null";
419 break;
420 case TemplateArgument::Type:
421 OS << " type";
422 dumpType(A.getAsType());
423 break;
424 case TemplateArgument::Declaration:
425 OS << " decl";
426 dumpDeclRef(A.getAsDecl());
427 break;
428 case TemplateArgument::NullPtr:
429 OS << " nullptr";
430 break;
431 case TemplateArgument::Integral:
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000432 OS << " integral " << A.getAsIntegral();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000433 break;
434 case TemplateArgument::Template:
435 OS << " template ";
436 A.getAsTemplate().dump(OS);
437 break;
438 case TemplateArgument::TemplateExpansion:
439 OS << " template expansion";
440 A.getAsTemplateOrTemplatePattern().dump(OS);
441 break;
442 case TemplateArgument::Expression:
443 OS << " expr";
444 dumpStmt(A.getAsExpr());
445 break;
446 case TemplateArgument::Pack:
447 OS << " pack";
448 for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
449 I != E; ++I)
450 dumpTemplateArgument(*I);
451 break;
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000452 }
453}
454
Chris Lattnere300c872007-08-30 06:17:34 +0000455//===----------------------------------------------------------------------===//
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000456// Decl dumping methods.
Chris Lattner6000dac2007-08-08 22:51:59 +0000457//===----------------------------------------------------------------------===//
458
Alexander Kornienko40b66a002012-12-13 13:59:55 +0000459void ASTDumper::dumpDecl(Decl *D) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000460 IndentScope Indent(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000462 if (!D) {
463 OS << "<<<NULL>>>";
464 return;
Chris Lattner6000dac2007-08-08 22:51:59 +0000465 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000466
467 OS << D->getDeclKindName() << "Decl";
468 dumpPointer(D);
469 dumpSourceRange(D->getSourceRange());
470 DeclVisitor<ASTDumper>::Visit(D);
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000471 if (D->hasAttrs()) {
472 for (AttrVec::const_iterator I = D->getAttrs().begin(),
473 E = D->getAttrs().end(); I != E; ++I)
474 dumpAttr(*I);
475 }
Alexander Kornienkoacd356e2013-01-14 14:07:11 +0000476 dumpFullComment(D->getASTContext().getCommentForDecl(D, 0));
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000477 // Decls within functions are visited by the body
478 if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D))
479 dumpDeclContext(dyn_cast<DeclContext>(D));
480}
481
482void ASTDumper::VisitLabelDecl(LabelDecl *D) {
483 dumpName(D);
484}
485
486void ASTDumper::VisitTypedefDecl(TypedefDecl *D) {
487 dumpName(D);
488 dumpType(D->getUnderlyingType());
489 if (D->isModulePrivate())
490 OS << " __module_private__";
491}
492
493void ASTDumper::VisitEnumDecl(EnumDecl *D) {
494 if (D->isScoped()) {
495 if (D->isScopedUsingClassTag())
496 OS << " class";
497 else
498 OS << " struct";
499 }
500 dumpName(D);
501 if (D->isModulePrivate())
502 OS << " __module_private__";
503 if (D->isFixed())
504 dumpType(D->getIntegerType());
505}
506
507void ASTDumper::VisitRecordDecl(RecordDecl *D) {
508 OS << ' ' << D->getKindName();
509 dumpName(D);
510 if (D->isModulePrivate())
511 OS << " __module_private__";
512}
513
514void ASTDumper::VisitEnumConstantDecl(EnumConstantDecl *D) {
515 dumpName(D);
516 dumpType(D->getType());
517 if (Expr *Init = D->getInitExpr())
518 dumpStmt(Init);
519}
520
521void ASTDumper::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
522 dumpName(D);
523 dumpType(D->getType());
524 for (IndirectFieldDecl::chain_iterator I = D->chain_begin(),
525 E = D->chain_end(); I != E; ++I)
526 dumpDeclRef(*I);
527}
528
529void ASTDumper::VisitFunctionDecl(FunctionDecl *D) {
530 dumpName(D);
531 dumpType(D->getType());
532
533 StorageClass SC = D->getStorageClassAsWritten();
534 if (SC != SC_None)
535 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
536 if (D->isInlineSpecified())
537 OS << " inline";
538 if (D->isVirtualAsWritten())
539 OS << " virtual";
540 if (D->isModulePrivate())
541 OS << " __module_private__";
542
543 if (D->isPure())
544 OS << " pure";
545 else if (D->isDeletedAsWritten())
546 OS << " delete";
547
548 if (const FunctionTemplateSpecializationInfo *FTSI =
549 D->getTemplateSpecializationInfo())
550 dumpTemplateArgumentList(*FTSI->TemplateArguments);
551
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000552 for (ArrayRef<NamedDecl *>::iterator
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000553 I = D->getDeclsInPrototypeScope().begin(),
554 E = D->getDeclsInPrototypeScope().end(); I != E; ++I)
555 dumpDecl(*I);
556
557 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
558 I != E; ++I)
559 dumpDecl(*I);
560
561 if (CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D))
562 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
563 E = C->init_end(); I != E; ++I)
564 dumpCXXCtorInitializer(*I);
565
566 if (D->doesThisDeclarationHaveABody())
567 dumpStmt(D->getBody());
568}
569
570void ASTDumper::VisitFieldDecl(FieldDecl *D) {
571 dumpName(D);
572 dumpType(D->getType());
573 if (D->isMutable())
574 OS << " mutable";
575 if (D->isModulePrivate())
576 OS << " __module_private__";
577 if (D->isBitField())
578 dumpStmt(D->getBitWidth());
579 if (Expr *Init = D->getInClassInitializer())
580 dumpStmt(Init);
581}
582
583void ASTDumper::VisitVarDecl(VarDecl *D) {
584 dumpName(D);
585 dumpType(D->getType());
586 StorageClass SC = D->getStorageClassAsWritten();
587 if (SC != SC_None)
588 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
589 if (D->isThreadSpecified())
590 OS << " __thread";
591 if (D->isModulePrivate())
592 OS << " __module_private__";
593 if (D->isNRVOVariable())
594 OS << " nrvo";
595 if (D->hasInit())
596 dumpStmt(D->getInit());
597}
598
599void ASTDumper::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
600 dumpStmt(D->getAsmString());
601}
602
603void ASTDumper::VisitImportDecl(ImportDecl *D) {
604 OS << ' ' << D->getImportedModule()->getFullModuleName();
605}
606
607//===----------------------------------------------------------------------===//
608// C++ Declarations
609//===----------------------------------------------------------------------===//
610
611void ASTDumper::VisitNamespaceDecl(NamespaceDecl *D) {
612 dumpName(D);
613 if (D->isInline())
614 OS << " inline";
615 if (!D->isOriginalNamespace())
616 dumpDeclRef(D->getOriginalNamespace(), "original");
617}
618
619void ASTDumper::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
620 OS << ' ';
621 dumpBareDeclRef(D->getNominatedNamespace());
622}
623
624void ASTDumper::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
625 dumpName(D);
626 dumpDeclRef(D->getAliasedNamespace());
627}
628
629void ASTDumper::VisitTypeAliasDecl(TypeAliasDecl *D) {
630 dumpName(D);
631 dumpType(D->getUnderlyingType());
632}
633
634void ASTDumper::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
635 dumpName(D);
636 dumpTemplateParameters(D->getTemplateParameters());
637 dumpDecl(D->getTemplatedDecl());
638}
639
640void ASTDumper::VisitCXXRecordDecl(CXXRecordDecl *D) {
641 VisitRecordDecl(D);
642 if (!D->isCompleteDefinition())
643 return;
644
645 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
646 E = D->bases_end(); I != E; ++I) {
647 IndentScope Indent(*this);
648 if (I->isVirtual())
649 OS << "virtual ";
650 dumpAccessSpecifier(I->getAccessSpecifier());
651 dumpType(I->getType());
652 if (I->isPackExpansion())
653 OS << "...";
654 }
655}
656
657void ASTDumper::VisitStaticAssertDecl(StaticAssertDecl *D) {
658 dumpStmt(D->getAssertExpr());
659 dumpStmt(D->getMessage());
660}
661
662void ASTDumper::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
663 dumpName(D);
664 dumpTemplateParameters(D->getTemplateParameters());
665 dumpDecl(D->getTemplatedDecl());
666 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(),
667 E = D->spec_end(); I != E; ++I) {
668 switch (I->getTemplateSpecializationKind()) {
669 case TSK_Undeclared:
670 case TSK_ImplicitInstantiation:
671 case TSK_ExplicitInstantiationDeclaration:
672 case TSK_ExplicitInstantiationDefinition:
673 dumpDecl(*I);
674 break;
675 case TSK_ExplicitSpecialization:
676 dumpDeclRef(*I);
677 break;
678 }
679 }
680}
681
682void ASTDumper::VisitClassTemplateDecl(ClassTemplateDecl *D) {
683 dumpName(D);
684 dumpTemplateParameters(D->getTemplateParameters());
685 dumpDecl(D->getTemplatedDecl());
686 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
687 I != E; ++I) {
688 switch (I->getTemplateSpecializationKind()) {
689 case TSK_Undeclared:
690 case TSK_ImplicitInstantiation:
691 dumpDecl(*I);
692 break;
693 case TSK_ExplicitSpecialization:
694 case TSK_ExplicitInstantiationDeclaration:
695 case TSK_ExplicitInstantiationDefinition:
696 dumpDeclRef(*I);
697 break;
698 }
699 }
700}
701
702void ASTDumper::VisitClassTemplateSpecializationDecl(
703 ClassTemplateSpecializationDecl *D) {
704 VisitCXXRecordDecl(D);
705 dumpTemplateArgumentList(D->getTemplateArgs());
706}
707
708void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
709 ClassTemplatePartialSpecializationDecl *D) {
710 VisitClassTemplateSpecializationDecl(D);
711 dumpTemplateParameters(D->getTemplateParameters());
712}
713
714void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
715 ClassScopeFunctionSpecializationDecl *D) {
716 dumpDeclRef(D->getSpecialization());
717 if (D->hasExplicitTemplateArgs())
718 dumpTemplateArgumentListInfo(D->templateArgs());
719}
720
721void ASTDumper::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
722 if (D->wasDeclaredWithTypename())
723 OS << " typename";
724 else
725 OS << " class";
726 if (D->isParameterPack())
727 OS << " ...";
728 dumpName(D);
729 if (D->hasDefaultArgument())
730 dumpType(D->getDefaultArgument());
731}
732
733void ASTDumper::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
734 dumpType(D->getType());
735 if (D->isParameterPack())
736 OS << " ...";
737 dumpName(D);
738 if (D->hasDefaultArgument())
739 dumpStmt(D->getDefaultArgument());
740}
741
742void ASTDumper::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
743 if (D->isParameterPack())
744 OS << " ...";
745 dumpName(D);
746 dumpTemplateParameters(D->getTemplateParameters());
747 if (D->hasDefaultArgument())
748 dumpTemplateArgumentLoc(D->getDefaultArgument());
749}
750
751void ASTDumper::VisitUsingDecl(UsingDecl *D) {
752 OS << ' ';
753 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
754 OS << D->getNameAsString();
755}
756
757void
758ASTDumper::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
759 OS << ' ';
760 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
761 OS << D->getNameAsString();
762}
763
764void ASTDumper::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
765 OS << ' ';
766 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
767 OS << D->getNameAsString();
768 dumpType(D->getType());
769}
770
771void ASTDumper::VisitUsingShadowDecl(UsingShadowDecl *D) {
772 OS << ' ';
773 dumpBareDeclRef(D->getTargetDecl());
774}
775
776void ASTDumper::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
777 switch (D->getLanguage()) {
778 case LinkageSpecDecl::lang_c: OS << " C"; break;
779 case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
780 }
781}
782
783void ASTDumper::VisitAccessSpecDecl(AccessSpecDecl *D) {
784 OS << ' ';
785 dumpAccessSpecifier(D->getAccess());
786}
787
788void ASTDumper::VisitFriendDecl(FriendDecl *D) {
789 if (TypeSourceInfo *T = D->getFriendType())
790 dumpType(T->getType());
791 else
792 dumpDecl(D->getFriendDecl());
793}
794
795//===----------------------------------------------------------------------===//
796// Obj-C Declarations
797//===----------------------------------------------------------------------===//
798
799void ASTDumper::VisitObjCIvarDecl(ObjCIvarDecl *D) {
800 dumpName(D);
801 dumpType(D->getType());
802 if (D->getSynthesize())
803 OS << " synthesize";
804
805 switch (D->getAccessControl()) {
806 case ObjCIvarDecl::None:
807 OS << " none";
808 break;
809 case ObjCIvarDecl::Private:
810 OS << " private";
811 break;
812 case ObjCIvarDecl::Protected:
813 OS << " protected";
814 break;
815 case ObjCIvarDecl::Public:
816 OS << " public";
817 break;
818 case ObjCIvarDecl::Package:
819 OS << " package";
820 break;
821 }
822}
823
824void ASTDumper::VisitObjCMethodDecl(ObjCMethodDecl *D) {
825 if (D->isInstanceMethod())
826 OS << " -";
827 else
828 OS << " +";
829 dumpName(D);
830 dumpType(D->getResultType());
831
832 if (D->isThisDeclarationADefinition())
833 dumpDeclContext(D);
834 else {
835 for (ObjCMethodDecl::param_iterator I = D->param_begin(),
836 E = D->param_end(); I != E; ++I) {
837 dumpDecl(*I);
838 }
839 }
840
841 if (D->isVariadic()) {
842 IndentScope Indent(*this);
843 OS << "...";
844 }
845
846 if (D->hasBody())
847 dumpStmt(D->getBody());
848}
849
850void ASTDumper::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
851 dumpName(D);
852 dumpDeclRef(D->getClassInterface());
853 dumpDeclRef(D->getImplementation());
854 for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
855 E = D->protocol_end(); I != E; ++I)
856 dumpDeclRef(*I);
857}
858
859void ASTDumper::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
860 dumpName(D);
861 dumpDeclRef(D->getClassInterface());
862 dumpDeclRef(D->getCategoryDecl());
863}
864
865void ASTDumper::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
866 dumpName(D);
867 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
868 E = D->protocol_end(); I != E; ++I)
869 dumpDeclRef(*I);
870}
871
872void ASTDumper::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
873 dumpName(D);
874 dumpDeclRef(D->getSuperClass(), "super");
875 dumpDeclRef(D->getImplementation());
876 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
877 E = D->protocol_end(); I != E; ++I)
878 dumpDeclRef(*I);
879}
880
881void ASTDumper::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
882 dumpName(D);
883 dumpDeclRef(D->getSuperClass(), "super");
884 dumpDeclRef(D->getClassInterface());
885 for (ObjCImplementationDecl::init_iterator I = D->init_begin(),
886 E = D->init_end(); I != E; ++I)
887 dumpCXXCtorInitializer(*I);
888}
889
890void ASTDumper::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
891 dumpName(D);
892 dumpDeclRef(D->getClassInterface());
893}
894
895void ASTDumper::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
896 dumpName(D);
897 dumpType(D->getType());
898
899 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
900 OS << " required";
901 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
902 OS << " optional";
903
904 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
905 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
906 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
907 OS << " readonly";
908 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
909 OS << " assign";
910 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
911 OS << " readwrite";
912 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
913 OS << " retain";
914 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
915 OS << " copy";
916 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
917 OS << " nonatomic";
918 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
919 OS << " atomic";
920 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
921 OS << " weak";
922 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
923 OS << " strong";
924 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
925 OS << " unsafe_unretained";
926 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
927 dumpDeclRef(D->getGetterMethodDecl(), "getter");
928 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
929 dumpDeclRef(D->getSetterMethodDecl(), "setter");
930 }
931}
932
933void ASTDumper::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
934 dumpName(D->getPropertyDecl());
935 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
936 OS << " synthesize";
937 else
938 OS << " dynamic";
939 dumpDeclRef(D->getPropertyDecl());
940 dumpDeclRef(D->getPropertyIvarDecl());
941}
942
943void ASTDumper::VisitBlockDecl(BlockDecl *D) {
944 for (BlockDecl::param_iterator I = D->param_begin(), E = D->param_end();
945 I != E; ++I)
946 dumpDecl(*I);
947
948 if (D->isVariadic()) {
949 IndentScope Indent(*this);
950 OS << "...";
951 }
952
953 if (D->capturesCXXThis()) {
954 IndentScope Indent(*this);
955 OS << "capture this";
956 }
957 for (BlockDecl::capture_iterator I = D->capture_begin(),
958 E = D->capture_end(); I != E; ++I) {
959 IndentScope Indent(*this);
960 OS << "capture";
961 if (I->isByRef())
962 OS << " byref";
963 if (I->isNested())
964 OS << " nested";
965 if (I->getVariable()) {
966 OS << ' ';
967 dumpBareDeclRef(I->getVariable());
968 }
969 if (I->hasCopyExpr())
970 dumpStmt(I->getCopyExpr());
971 }
972
973 dumpStmt(D->getBody());
Chris Lattner6000dac2007-08-08 22:51:59 +0000974}
975
Alexander Kornienkod5bc3592012-12-11 15:20:44 +0000976//===----------------------------------------------------------------------===//
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000977// Stmt dumping methods.
Alexander Kornienkod5bc3592012-12-11 15:20:44 +0000978//===----------------------------------------------------------------------===//
979
Alexander Kornienko40b66a002012-12-13 13:59:55 +0000980void ASTDumper::dumpStmt(Stmt *S) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +0000981 IndentScope Indent(*this);
982
983 if (!S) {
984 OS << "<<<NULL>>>";
985 return;
986 }
987
988 if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
989 VisitDeclStmt(DS);
990 return;
991 }
992
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000993 StmtVisitor<ASTDumper>::Visit(S);
Alexander Kornienkod5bc3592012-12-11 15:20:44 +0000994 for (Stmt::child_range CI = S->children(); CI; ++CI)
995 dumpStmt(*CI);
996}
997
Alexander Kornienko40b66a002012-12-13 13:59:55 +0000998void ASTDumper::VisitStmt(Stmt *Node) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000999 OS << Node->getStmtClassName();
1000 dumpPointer(Node);
1001 dumpSourceRange(Node->getSourceRange());
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001002}
1003
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001004void ASTDumper::VisitDeclStmt(DeclStmt *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001005 VisitStmt(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001006 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
1007 I != E; ++I)
1008 dumpDecl(*I);
Ted Kremenek5399ce22007-12-12 06:59:42 +00001009}
1010
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +00001011void ASTDumper::VisitAttributedStmt(AttributedStmt *Node) {
1012 VisitStmt(Node);
1013 for (ArrayRef<const Attr*>::iterator I = Node->getAttrs().begin(),
1014 E = Node->getAttrs().end(); I != E; ++I)
1015 dumpAttr(*I);
1016}
1017
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001018void ASTDumper::VisitLabelStmt(LabelStmt *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001019 VisitStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001020 OS << " '" << Node->getName() << "'";
Chris Lattner6000dac2007-08-08 22:51:59 +00001021}
1022
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001023void ASTDumper::VisitGotoStmt(GotoStmt *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001024 VisitStmt(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001025 OS << " '" << Node->getLabel()->getName() << "'";
1026 dumpPointer(Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +00001027}
1028
Chris Lattner6000dac2007-08-08 22:51:59 +00001029//===----------------------------------------------------------------------===//
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001030// Expr dumping methods.
Chris Lattner6000dac2007-08-08 22:51:59 +00001031//===----------------------------------------------------------------------===//
1032
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001033void ASTDumper::VisitExpr(Expr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001034 VisitStmt(Node);
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001035 dumpType(Node->getType());
1036
1037 switch (Node->getValueKind()) {
1038 case VK_RValue:
1039 break;
1040 case VK_LValue:
1041 OS << " lvalue";
1042 break;
1043 case VK_XValue:
1044 OS << " xvalue";
1045 break;
1046 }
1047
1048 switch (Node->getObjectKind()) {
1049 case OK_Ordinary:
1050 break;
1051 case OK_BitField:
1052 OS << " bitfield";
1053 break;
1054 case OK_ObjCProperty:
1055 OS << " objcproperty";
1056 break;
1057 case OK_ObjCSubscript:
1058 OS << " objcsubscript";
1059 break;
1060 case OK_VectorComponent:
1061 OS << " vectorcomponent";
1062 break;
1063 }
Chris Lattner6000dac2007-08-08 22:51:59 +00001064}
1065
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001066static void dumpBasePath(raw_ostream &OS, CastExpr *Node) {
John McCallf871d0c2010-08-07 06:22:56 +00001067 if (Node->path_empty())
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001068 return;
1069
1070 OS << " (";
1071 bool First = true;
John McCallf871d0c2010-08-07 06:22:56 +00001072 for (CastExpr::path_iterator
1073 I = Node->path_begin(), E = Node->path_end(); I != E; ++I) {
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001074 const CXXBaseSpecifier *Base = *I;
1075 if (!First)
1076 OS << " -> ";
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001077
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001078 const CXXRecordDecl *RD =
1079 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001080
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001081 if (Base->isVirtual())
1082 OS << "virtual ";
1083 OS << RD->getName();
1084 First = false;
1085 }
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001086
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001087 OS << ')';
1088}
1089
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001090void ASTDumper::VisitCastExpr(CastExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001091 VisitExpr(Node);
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001092 OS << " <" << Node->getCastKindName();
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001093 dumpBasePath(OS, Node);
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001094 OS << ">";
Anders Carlsson27a5b9b2009-08-22 23:33:40 +00001095}
1096
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001097void ASTDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001098 VisitExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +00001099
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001100 OS << " ";
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001101 dumpBareDeclRef(Node->getDecl());
Chandler Carruth3aa81402011-05-01 23:48:14 +00001102 if (Node->getDecl() != Node->getFoundDecl()) {
1103 OS << " (";
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001104 dumpBareDeclRef(Node->getFoundDecl());
Chandler Carruth3aa81402011-05-01 23:48:14 +00001105 OS << ")";
1106 }
John McCall6b5a61b2011-02-07 10:33:21 +00001107}
1108
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001109void ASTDumper::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001110 VisitExpr(Node);
John McCall9d5f35e2009-12-11 21:50:11 +00001111 OS << " (";
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001112 if (!Node->requiresADL())
1113 OS << "no ";
Benjamin Kramer900fc632010-04-17 09:33:03 +00001114 OS << "ADL) = '" << Node->getName() << '\'';
John McCall9d5f35e2009-12-11 21:50:11 +00001115
1116 UnresolvedLookupExpr::decls_iterator
1117 I = Node->decls_begin(), E = Node->decls_end();
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001118 if (I == E)
1119 OS << " empty";
John McCall9d5f35e2009-12-11 21:50:11 +00001120 for (; I != E; ++I)
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001121 dumpPointer(*I);
John McCall9d5f35e2009-12-11 21:50:11 +00001122}
1123
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001124void ASTDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001125 VisitExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +00001126
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001127 OS << " " << Node->getDecl()->getDeclKindName()
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001128 << "Decl='" << *Node->getDecl() << "'";
1129 dumpPointer(Node->getDecl());
Steve Naroff218543b2008-05-23 22:01:24 +00001130 if (Node->isFreeIvar())
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001131 OS << " isFreeIvar";
Steve Naroff3c64d9e2008-03-12 13:19:12 +00001132}
1133
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001134void ASTDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001135 VisitExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +00001136 switch (Node->getIdentType()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001137 default: llvm_unreachable("unknown case");
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001138 case PredefinedExpr::Func: OS << " __func__"; break;
1139 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001140 case PredefinedExpr::LFunction: OS << " L__FUNCTION__"; break;
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001141 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
Chris Lattner6000dac2007-08-08 22:51:59 +00001142 }
1143}
1144
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001145void ASTDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001146 VisitExpr(Node);
Richard Trieu49cf8842011-11-03 23:56:23 +00001147 OS << " " << Node->getValue();
Chris Lattner6000dac2007-08-08 22:51:59 +00001148}
1149
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001150void ASTDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001151 VisitExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +00001152
1153 bool isSigned = Node->getType()->isSignedIntegerType();
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001154 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattner6000dac2007-08-08 22:51:59 +00001155}
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001156
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001157void ASTDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001158 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001159 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattner6000dac2007-08-08 22:51:59 +00001160}
Chris Lattner5d661452007-08-26 03:42:43 +00001161
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001162void ASTDumper::VisitStringLiteral(StringLiteral *Str) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001163 VisitExpr(Str);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001164 OS << " ";
Richard Trieu8ab09da2012-06-13 20:25:24 +00001165 Str->outputString(OS);
Chris Lattner6000dac2007-08-08 22:51:59 +00001166}
Chris Lattner17a1a722007-08-30 01:00:35 +00001167
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001168void ASTDumper::VisitUnaryOperator(UnaryOperator *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001169 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001170 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
1171 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner6000dac2007-08-08 22:51:59 +00001172}
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001173
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001174void ASTDumper::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001175 VisitExpr(Node);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001176 switch(Node->getKind()) {
1177 case UETT_SizeOf:
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001178 OS << " sizeof";
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001179 break;
1180 case UETT_AlignOf:
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001181 OS << " alignof";
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001182 break;
1183 case UETT_VecStep:
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001184 OS << " vec_step";
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001185 break;
1186 }
Sebastian Redl05189992008-11-11 17:56:53 +00001187 if (Node->isArgumentType())
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001188 dumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +00001189}
Chris Lattner13cb21f2007-08-09 17:35:30 +00001190
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001191void ASTDumper::VisitMemberExpr(MemberExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001192 VisitExpr(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001193 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
1194 dumpPointer(Node->getMemberDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +00001195}
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001196
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001197void ASTDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001198 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001199 OS << " " << Node->getAccessor().getNameStart();
Chris Lattner6000dac2007-08-08 22:51:59 +00001200}
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001201
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001202void ASTDumper::VisitBinaryOperator(BinaryOperator *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001203 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001204 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnereb14fe82007-08-25 02:00:02 +00001205}
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001206
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001207void ASTDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001208 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001209 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
1210 << "' ComputeLHSTy=";
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001211 dumpBareType(Node->getComputationLHSType());
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001212 OS << " ComputeResultTy=";
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001213 dumpBareType(Node->getComputationResultType());
Chris Lattner6000dac2007-08-08 22:51:59 +00001214}
Chris Lattner6000dac2007-08-08 22:51:59 +00001215
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001216void ASTDumper::VisitBlockExpr(BlockExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001217 VisitExpr(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001218 dumpDecl(Node->getBlockDecl());
John McCall6b5a61b2011-02-07 10:33:21 +00001219}
1220
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001221void ASTDumper::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001222 VisitExpr(Node);
John McCall4b9c2d22011-11-06 09:01:30 +00001223
Manuel Klimekcb7b45e2012-11-07 00:33:12 +00001224 if (Expr *Source = Node->getSourceExpr())
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001225 dumpStmt(Source);
John McCall4b9c2d22011-11-06 09:01:30 +00001226}
1227
Chris Lattner6000dac2007-08-08 22:51:59 +00001228// GNU extensions.
1229
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001230void ASTDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001231 VisitExpr(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001232 OS << " " << Node->getLabel()->getName();
1233 dumpPointer(Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +00001234}
1235
Chris Lattnerf9e05812007-08-09 18:03:18 +00001236//===----------------------------------------------------------------------===//
1237// C++ Expressions
1238//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +00001239
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001240void ASTDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001241 VisitExpr(Node);
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001242 OS << " " << Node->getCastName()
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001243 << "<" << Node->getTypeAsWritten().getAsString() << ">"
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001244 << " <" << Node->getCastKindName();
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001245 dumpBasePath(OS, Node);
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001246 OS << ">";
Chris Lattner6000dac2007-08-08 22:51:59 +00001247}
1248
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001249void ASTDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001250 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001251 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +00001252}
1253
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001254void ASTDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001255 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001256 OS << " this";
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001257}
1258
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001259void ASTDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001260 VisitExpr(Node);
Eli Friedmancc2fca22011-09-02 17:38:59 +00001261 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
1262 << " <" << Node->getCastKindName() << ">";
Douglas Gregor49badde2008-10-27 19:41:14 +00001263}
1264
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001265void ASTDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001266 VisitExpr(Node);
John McCalld4bbdfe2010-02-02 19:03:45 +00001267 CXXConstructorDecl *Ctor = Node->getConstructor();
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001268 dumpType(Ctor->getType());
Anders Carlsson0eca1b62009-08-12 00:21:52 +00001269 if (Node->isElidable())
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001270 OS << " elidable";
John McCallf8cf0b02010-08-07 06:38:55 +00001271 if (Node->requiresZeroInitialization())
1272 OS << " zeroing";
Anders Carlsson0eca1b62009-08-12 00:21:52 +00001273}
1274
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001275void ASTDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001276 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001277 OS << " ";
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001278 dumpCXXTemporary(Node->getTemporary());
Anders Carlsson0eca1b62009-08-12 00:21:52 +00001279}
1280
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001281void ASTDumper::VisitExprWithCleanups(ExprWithCleanups *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001282 VisitExpr(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001283 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
1284 dumpDeclRef(Node->getObject(i), "cleanup");
Anders Carlsson0eca1b62009-08-12 00:21:52 +00001285}
1286
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001287void ASTDumper::dumpCXXTemporary(CXXTemporary *Temporary) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001288 OS << "(CXXTemporary";
1289 dumpPointer(Temporary);
1290 OS << ")";
Anders Carlsson0eca1b62009-08-12 00:21:52 +00001291}
1292
Anders Carlsson55085182007-08-21 17:43:55 +00001293//===----------------------------------------------------------------------===//
1294// Obj-C Expressions
1295//===----------------------------------------------------------------------===//
1296
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001297void ASTDumper::VisitObjCMessageExpr(ObjCMessageExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001298 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001299 OS << " selector=" << Node->getSelector().getAsString();
Douglas Gregor04badcf2010-04-21 00:45:42 +00001300 switch (Node->getReceiverKind()) {
1301 case ObjCMessageExpr::Instance:
1302 break;
1303
1304 case ObjCMessageExpr::Class:
1305 OS << " class=";
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001306 dumpBareType(Node->getClassReceiver());
Douglas Gregor04badcf2010-04-21 00:45:42 +00001307 break;
1308
1309 case ObjCMessageExpr::SuperInstance:
1310 OS << " super (instance)";
1311 break;
1312
1313 case ObjCMessageExpr::SuperClass:
1314 OS << " super (class)";
1315 break;
1316 }
Ted Kremenekb3d914b2008-02-29 22:04:05 +00001317}
1318
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001319void ASTDumper::VisitObjCBoxedExpr(ObjCBoxedExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001320 VisitExpr(Node);
Argyrios Kyrtzidis36faadd2012-05-10 20:02:31 +00001321 OS << " selector=" << Node->getBoxingMethod()->getSelector().getAsString();
1322}
1323
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001324void ASTDumper::VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001325 VisitStmt(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001326 if (VarDecl *CatchParam = Node->getCatchParamDecl())
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001327 dumpDecl(CatchParam);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001328 else
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001329 OS << " catch all";
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001330}
1331
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001332void ASTDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001333 VisitExpr(Node);
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001334 dumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001335}
1336
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001337void ASTDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001338 VisitExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001340 OS << " " << Node->getSelector().getAsString();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001341}
1342
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001343void ASTDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001344 VisitExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001346 OS << ' ' << *Node->getProtocol();
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001347}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001348
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001349void ASTDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001350 VisitExpr(Node);
John McCall12f78a62010-12-02 01:19:52 +00001351 if (Node->isImplicitProperty()) {
Fariborz Jahanian99130e52010-12-22 19:46:35 +00001352 OS << " Kind=MethodRef Getter=\"";
1353 if (Node->getImplicitPropertyGetter())
1354 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
1355 else
1356 OS << "(null)";
1357
1358 OS << "\" Setter=\"";
John McCall12f78a62010-12-02 01:19:52 +00001359 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
1360 OS << Setter->getSelector().getAsString();
1361 else
1362 OS << "(null)";
1363 OS << "\"";
1364 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001365 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
John McCall12f78a62010-12-02 01:19:52 +00001366 }
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001367
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00001368 if (Node->isSuperReceiver())
1369 OS << " super";
Argyrios Kyrtzidisb085d892012-03-30 00:19:18 +00001370
1371 OS << " Messaging=";
1372 if (Node->isMessagingGetter() && Node->isMessagingSetter())
1373 OS << "Getter&Setter";
1374 else if (Node->isMessagingGetter())
1375 OS << "Getter";
1376 else if (Node->isMessagingSetter())
1377 OS << "Setter";
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001378}
1379
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001380void ASTDumper::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001381 VisitExpr(Node);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001382 if (Node->isArraySubscriptRefExpr())
1383 OS << " Kind=ArraySubscript GetterForArray=\"";
1384 else
1385 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
1386 if (Node->getAtIndexMethodDecl())
1387 OS << Node->getAtIndexMethodDecl()->getSelector().getAsString();
1388 else
1389 OS << "(null)";
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001390
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001391 if (Node->isArraySubscriptRefExpr())
1392 OS << "\" SetterForArray=\"";
1393 else
1394 OS << "\" SetterForDictionary=\"";
1395 if (Node->setAtIndexMethodDecl())
1396 OS << Node->setAtIndexMethodDecl()->getSelector().getAsString();
1397 else
1398 OS << "(null)";
1399}
1400
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001401void ASTDumper::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001402 VisitExpr(Node);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001403 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
1404}
1405
Chris Lattner6000dac2007-08-08 22:51:59 +00001406//===----------------------------------------------------------------------===//
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001407// Comments
1408//===----------------------------------------------------------------------===//
1409
1410const char *ASTDumper::getCommandName(unsigned CommandID) {
1411 if (Traits)
1412 return Traits->getCommandInfo(CommandID)->Name;
1413 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
1414 if (Info)
1415 return Info->Name;
1416 return "<not a builtin command>";
1417}
1418
1419void ASTDumper::dumpFullComment(const FullComment *C) {
1420 if (!C)
1421 return;
1422
1423 FC = C;
1424 dumpComment(C);
1425 FC = 0;
1426}
1427
1428void ASTDumper::dumpComment(const Comment *C) {
1429 IndentScope Indent(*this);
1430
1431 if (!C) {
1432 OS << "<<<NULL>>>";
1433 return;
1434 }
1435
1436 OS << C->getCommentKindName();
1437 dumpPointer(C);
1438 dumpSourceRange(C->getSourceRange());
1439 ConstCommentVisitor<ASTDumper>::visit(C);
1440 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
1441 I != E; ++I)
1442 dumpComment(*I);
1443}
1444
1445void ASTDumper::visitTextComment(const TextComment *C) {
1446 OS << " Text=\"" << C->getText() << "\"";
1447}
1448
1449void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
1450 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
1451 switch (C->getRenderKind()) {
1452 case InlineCommandComment::RenderNormal:
1453 OS << " RenderNormal";
1454 break;
1455 case InlineCommandComment::RenderBold:
1456 OS << " RenderBold";
1457 break;
1458 case InlineCommandComment::RenderMonospaced:
1459 OS << " RenderMonospaced";
1460 break;
1461 case InlineCommandComment::RenderEmphasized:
1462 OS << " RenderEmphasized";
1463 break;
1464 }
1465
1466 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
1467 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
1468}
1469
1470void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
1471 OS << " Name=\"" << C->getTagName() << "\"";
1472 if (C->getNumAttrs() != 0) {
1473 OS << " Attrs: ";
1474 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
1475 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
1476 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
1477 }
1478 }
1479 if (C->isSelfClosing())
1480 OS << " SelfClosing";
1481}
1482
1483void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
1484 OS << " Name=\"" << C->getTagName() << "\"";
1485}
1486
1487void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
1488 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
1489 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
1490 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
1491}
1492
1493void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
1494 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
1495
1496 if (C->isDirectionExplicit())
1497 OS << " explicitly";
1498 else
1499 OS << " implicitly";
1500
1501 if (C->hasParamName()) {
1502 if (C->isParamIndexValid())
1503 OS << " Param=\"" << C->getParamName(FC) << "\"";
1504 else
1505 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
1506 }
1507
1508 if (C->isParamIndexValid())
1509 OS << " ParamIndex=" << C->getParamIndex();
1510}
1511
1512void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
1513 if (C->hasParamName()) {
1514 if (C->isPositionValid())
1515 OS << " Param=\"" << C->getParamName(FC) << "\"";
1516 else
1517 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
1518 }
1519
1520 if (C->isPositionValid()) {
1521 OS << " Position=<";
1522 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
1523 OS << C->getIndex(i);
1524 if (i != e - 1)
1525 OS << ", ";
1526 }
1527 OS << ">";
1528 }
1529}
1530
1531void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
1532 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
1533 " CloseName=\"" << C->getCloseName() << "\"";
1534}
1535
1536void ASTDumper::visitVerbatimBlockLineComment(
1537 const VerbatimBlockLineComment *C) {
1538 OS << " Text=\"" << C->getText() << "\"";
1539}
1540
1541void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
1542 OS << " Text=\"" << C->getText() << "\"";
1543}
1544
1545//===----------------------------------------------------------------------===//
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001546// Decl method implementations
1547//===----------------------------------------------------------------------===//
1548
1549void Decl::dump() const {
1550 dump(llvm::errs());
1551}
1552
1553void Decl::dump(raw_ostream &OS) const {
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001554 ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
1555 &getASTContext().getSourceManager());
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001556 P.dumpDecl(const_cast<Decl*>(this));
1557}
1558
1559//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +00001560// Stmt method implementations
1561//===----------------------------------------------------------------------===//
1562
Chris Lattnere300c872007-08-30 06:17:34 +00001563void Stmt::dump(SourceManager &SM) const {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001564 dump(llvm::errs(), SM);
1565}
1566
Chris Lattner5f9e2722011-07-23 10:55:15 +00001567void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001568 ASTDumper P(OS, 0, &SM);
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001569 P.dumpStmt(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +00001570}
1571
Chris Lattner6000dac2007-08-08 22:51:59 +00001572void Stmt::dump() const {
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001573 ASTDumper P(llvm::errs(), 0, 0);
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001574 P.dumpStmt(const_cast<Stmt*>(this));
Chris Lattner6000dac2007-08-08 22:51:59 +00001575}
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001576
1577//===----------------------------------------------------------------------===//
1578// Comment method implementations
1579//===----------------------------------------------------------------------===//
1580
1581void Comment::dump() const {
1582 dump(llvm::errs(), 0, 0);
1583}
1584
1585void Comment::dump(const ASTContext &Context) const {
1586 dump(llvm::errs(), &Context.getCommentCommandTraits(),
1587 &Context.getSourceManager());
1588}
1589
1590void Comment::dump(llvm::raw_ostream &OS, const CommandTraits *Traits,
1591 const SourceManager *SM) const {
1592 const FullComment *FC = dyn_cast<FullComment>(this);
1593 ASTDumper D(OS, Traits, SM);
1594 D.dumpFullComment(FC);
1595}