blob: 5291c8bc42d156c66db030ee12ec33c5e858f612 [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 {
Richard Trieu7ba443a2013-01-26 01:31:20 +000033 // Colors used for various parts of the AST dump
34
35 struct TerminalColor {
36 raw_ostream::Colors Color;
37 bool Bold;
38 };
39
40 // Decl kind names (VarDecl, FunctionDecl, etc)
41 static const TerminalColor DeclKindNameColor = { raw_ostream::GREEN, true };
42 // Attr names (CleanupAttr, GuardedByAttr, etc)
43 static const TerminalColor AttrColor = { raw_ostream::BLUE, true };
44 // Statement names (DeclStmt, ImplicitCastExpr, etc)
45 static const TerminalColor StmtColor = { raw_ostream::MAGENTA, true };
46 // Comment names (FullComment, ParagraphComment, TextComment, etc)
47 static const TerminalColor CommentColor = { raw_ostream::YELLOW, true };
48
49 // Type names (int, float, etc, plus user defined types)
50 static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
51
52 // Pointer address
53 static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
54 // Source locations
55 static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
56
57 // lvalue/xvalue
58 static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
59 // bitfield/objcproperty/objcsubscript/vectorcomponent
60 static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
61
62 // Null statements
63 static const TerminalColor NullColor = { raw_ostream::BLUE, false };
64
65 // CastKind from CastExpr's
66 static const TerminalColor CastColor = { raw_ostream::RED, false };
67
68 // Value of the statement
69 static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
70 // Decl names
71 static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
72
Richard Trieue8d41192013-01-31 01:44:26 +000073 // Indents ( `, -. | )
74 static const TerminalColor IndentColor = { raw_ostream::BLUE, false };
75
Alexander Kornienkod538ed92012-12-20 02:09:13 +000076 class ASTDumper
Alexander Kornienkoacd356e2013-01-14 14:07:11 +000077 : public DeclVisitor<ASTDumper>, public StmtVisitor<ASTDumper>,
78 public ConstCommentVisitor<ASTDumper> {
Chris Lattner5f9e2722011-07-23 10:55:15 +000079 raw_ostream &OS;
Alexander Kornienkoacd356e2013-01-14 14:07:11 +000080 const CommandTraits *Traits;
81 const SourceManager *SM;
Chris Lattner6000dac2007-08-08 22:51:59 +000082 unsigned IndentLevel;
Manuel Klimekcb7b45e2012-11-07 00:33:12 +000083 bool IsFirstLine;
Mike Stump1eb44332009-09-09 15:08:12 +000084
Richard Trieue8d41192013-01-31 01:44:26 +000085 // Indicates whether more child are expected at the current tree depth
86 enum IndentType { IT_Child, IT_LastChild };
87
88 /// Indents[i] indicates if another child exists at level i.
89 /// Used by Indent() to print the tree structure.
90 llvm::SmallVector<IndentType, 32> Indents;
91
92 /// Indicates that more children will be needed at this indent level.
93 /// If true, prevents lastChild() from marking the node as the last child.
94 /// This is used when there are multiple collections of children to be
95 /// dumped as well as during conditional node dumping.
96 bool MoreChildren;
97
Alexander Kornienko21c8b192012-12-11 15:28:09 +000098 /// Keep track of the last location we print out so that we can
99 /// print out deltas from then on out.
Chris Lattnere300c872007-08-30 06:17:34 +0000100 const char *LastLocFilename;
101 unsigned LastLocLine;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000102
Alexander Kornienkoacd356e2013-01-14 14:07:11 +0000103 /// The \c FullComment parent of the comment being dumped.
104 const FullComment *FC;
105
Richard Trieu7ba443a2013-01-26 01:31:20 +0000106 bool ShowColors;
107
Manuel Klimekcb7b45e2012-11-07 00:33:12 +0000108 class IndentScope {
Alexander Kornienko40b66a002012-12-13 13:59:55 +0000109 ASTDumper &Dumper;
Richard Trieue8d41192013-01-31 01:44:26 +0000110 // Preserve the Dumper's MoreChildren value from the previous IndentScope
111 bool MoreChildren;
Manuel Klimekcb7b45e2012-11-07 00:33:12 +0000112 public:
Alexander Kornienko40b66a002012-12-13 13:59:55 +0000113 IndentScope(ASTDumper &Dumper) : Dumper(Dumper) {
Richard Trieue8d41192013-01-31 01:44:26 +0000114 MoreChildren = Dumper.hasMoreChildren();
115 Dumper.setMoreChildren(false);
Manuel Klimekcb7b45e2012-11-07 00:33:12 +0000116 Dumper.indent();
117 }
118 ~IndentScope() {
Richard Trieue8d41192013-01-31 01:44:26 +0000119 Dumper.setMoreChildren(MoreChildren);
Manuel Klimekcb7b45e2012-11-07 00:33:12 +0000120 Dumper.unindent();
121 }
122 };
123
Richard Trieu7ba443a2013-01-26 01:31:20 +0000124 class ColorScope {
125 ASTDumper &Dumper;
126 public:
127 ColorScope(ASTDumper &Dumper, TerminalColor Color)
128 : Dumper(Dumper) {
129 if (Dumper.ShowColors)
130 Dumper.OS.changeColor(Color.Color, Color.Bold);
131 }
132 ~ColorScope() {
133 if (Dumper.ShowColors)
134 Dumper.OS.resetColor();
135 }
136 };
137
Chris Lattner6000dac2007-08-08 22:51:59 +0000138 public:
Alexander Kornienkoacd356e2013-01-14 14:07:11 +0000139 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
140 const SourceManager *SM)
141 : OS(OS), Traits(Traits), SM(SM), IndentLevel(0), IsFirstLine(true),
Richard Trieue8d41192013-01-31 01:44:26 +0000142 MoreChildren(false), LastLocFilename(""), LastLocLine(~0U), FC(0),
Richard Trieu7ba443a2013-01-26 01:31:20 +0000143 ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
144
145 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
146 const SourceManager *SM, bool ShowColors)
147 : OS(OS), Traits(Traits), SM(SM), IndentLevel(0), IsFirstLine(true),
Richard Trieue8d41192013-01-31 01:44:26 +0000148 MoreChildren(false), LastLocFilename(""), LastLocLine(~0U),
149 ShowColors(ShowColors) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Alexander Kornienko40b66a002012-12-13 13:59:55 +0000151 ~ASTDumper() {
Manuel Klimekcb7b45e2012-11-07 00:33:12 +0000152 OS << "\n";
153 }
154
Alexander Kornienkod5bc3592012-12-11 15:20:44 +0000155 void dumpDecl(Decl *D);
156 void dumpStmt(Stmt *S);
Alexander Kornienkoacd356e2013-01-14 14:07:11 +0000157 void dumpFullComment(const FullComment *C);
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Richard Trieue8d41192013-01-31 01:44:26 +0000159 // Formatting
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000160 void indent();
161 void unindent();
Richard Trieue8d41192013-01-31 01:44:26 +0000162 void lastChild();
163 bool hasMoreChildren();
164 void setMoreChildren(bool Value);
165
166 // Utilities
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000167 void dumpPointer(const void *Ptr);
168 void dumpSourceRange(SourceRange R);
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000169 void dumpLocation(SourceLocation Loc);
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000170 void dumpBareType(QualType T);
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000171 void dumpType(QualType T);
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000172 void dumpBareDeclRef(const Decl *Node);
Alexander Kornienkoc9394532012-12-20 12:23:54 +0000173 void dumpDeclRef(const Decl *Node, const char *Label = 0);
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000174 void dumpName(const NamedDecl *D);
Richard Trieue8d41192013-01-31 01:44:26 +0000175 bool hasNodes(const DeclContext *DC);
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000176 void dumpDeclContext(const DeclContext *DC);
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000177 void dumpAttr(const Attr *A);
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000178
179 // C++ Utilities
180 void dumpAccessSpecifier(AccessSpecifier AS);
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000181 void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
182 void dumpTemplateParameters(const TemplateParameterList *TPL);
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000183 void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
184 void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
185 void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
186 void dumpTemplateArgument(const TemplateArgument &A,
187 SourceRange R = SourceRange());
188
189 // Decls
190 void VisitLabelDecl(LabelDecl *D);
191 void VisitTypedefDecl(TypedefDecl *D);
192 void VisitEnumDecl(EnumDecl *D);
193 void VisitRecordDecl(RecordDecl *D);
194 void VisitEnumConstantDecl(EnumConstantDecl *D);
195 void VisitIndirectFieldDecl(IndirectFieldDecl *D);
196 void VisitFunctionDecl(FunctionDecl *D);
197 void VisitFieldDecl(FieldDecl *D);
198 void VisitVarDecl(VarDecl *D);
199 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
200 void VisitImportDecl(ImportDecl *D);
201
202 // C++ Decls
203 void VisitNamespaceDecl(NamespaceDecl *D);
204 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
205 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
206 void VisitTypeAliasDecl(TypeAliasDecl *D);
207 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
208 void VisitCXXRecordDecl(CXXRecordDecl *D);
209 void VisitStaticAssertDecl(StaticAssertDecl *D);
210 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
211 void VisitClassTemplateDecl(ClassTemplateDecl *D);
212 void VisitClassTemplateSpecializationDecl(
213 ClassTemplateSpecializationDecl *D);
214 void VisitClassTemplatePartialSpecializationDecl(
215 ClassTemplatePartialSpecializationDecl *D);
216 void VisitClassScopeFunctionSpecializationDecl(
217 ClassScopeFunctionSpecializationDecl *D);
218 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
219 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
220 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
221 void VisitUsingDecl(UsingDecl *D);
222 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
223 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
224 void VisitUsingShadowDecl(UsingShadowDecl *D);
225 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
226 void VisitAccessSpecDecl(AccessSpecDecl *D);
227 void VisitFriendDecl(FriendDecl *D);
228
229 // ObjC Decls
230 void VisitObjCIvarDecl(ObjCIvarDecl *D);
231 void VisitObjCMethodDecl(ObjCMethodDecl *D);
232 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
233 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
234 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
235 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
236 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
237 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
238 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
239 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
240 void VisitBlockDecl(BlockDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattner17a1a722007-08-30 01:00:35 +0000242 // Stmts.
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000243 void VisitStmt(Stmt *Node);
Ted Kremenek5399ce22007-12-12 06:59:42 +0000244 void VisitDeclStmt(DeclStmt *Node);
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000245 void VisitAttributedStmt(AttributedStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000246 void VisitLabelStmt(LabelStmt *Node);
247 void VisitGotoStmt(GotoStmt *Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Chris Lattner17a1a722007-08-30 01:00:35 +0000249 // Exprs
250 void VisitExpr(Expr *Node);
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000251 void VisitCastExpr(CastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000252 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattnerd9f69102008-08-10 01:53:14 +0000253 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000254 void VisitCharacterLiteral(CharacterLiteral *Node);
255 void VisitIntegerLiteral(IntegerLiteral *Node);
256 void VisitFloatingLiteral(FloatingLiteral *Node);
257 void VisitStringLiteral(StringLiteral *Str);
258 void VisitUnaryOperator(UnaryOperator *Node);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000259 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000260 void VisitMemberExpr(MemberExpr *Node);
Nate Begeman213541a2008-04-18 23:10:10 +0000261 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000262 void VisitBinaryOperator(BinaryOperator *Node);
263 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
264 void VisitAddrLabelExpr(AddrLabelExpr *Node);
John McCall6b5a61b2011-02-07 10:33:21 +0000265 void VisitBlockExpr(BlockExpr *Node);
John McCall4b9c2d22011-11-06 09:01:30 +0000266 void VisitOpaqueValueExpr(OpaqueValueExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000267
268 // C++
Douglas Gregor49badde2008-10-27 19:41:14 +0000269 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000270 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000271 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000272 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000273 void VisitCXXConstructExpr(CXXConstructExpr *Node);
274 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node);
John McCall4765fa02010-12-06 08:20:24 +0000275 void VisitExprWithCleanups(ExprWithCleanups *Node);
John McCall9d5f35e2009-12-11 21:50:11 +0000276 void VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node);
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000277 void dumpCXXTemporary(CXXTemporary *Temporary);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Chris Lattner17a1a722007-08-30 01:00:35 +0000279 // ObjC
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000280 void VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000281 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000282 void VisitObjCMessageExpr(ObjCMessageExpr *Node);
283 void VisitObjCBoxedExpr(ObjCBoxedExpr *Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000284 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000285 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000286 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000287 void VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000288 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000289 void VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node);
Alexander Kornienkoacd356e2013-01-14 14:07:11 +0000290
291 // Comments.
292 const char *getCommandName(unsigned CommandID);
293 void dumpComment(const Comment *C);
294
295 // Inline comments.
296 void visitTextComment(const TextComment *C);
297 void visitInlineCommandComment(const InlineCommandComment *C);
298 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
299 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
300
301 // Block comments.
302 void visitBlockCommandComment(const BlockCommandComment *C);
303 void visitParamCommandComment(const ParamCommandComment *C);
304 void visitTParamCommandComment(const TParamCommandComment *C);
305 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
306 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
307 void visitVerbatimLineComment(const VerbatimLineComment *C);
Chris Lattner6000dac2007-08-08 22:51:59 +0000308 };
309}
310
311//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000312// Utilities
313//===----------------------------------------------------------------------===//
314
Richard Trieue8d41192013-01-31 01:44:26 +0000315// Print out the appropriate tree structure using the Indents vector.
316// Example of tree and the Indents vector at each level.
317// A { }
318// |-B { IT_Child }
319// | `-C { IT_Child, IT_LastChild }
320// `-D { IT_LastChild }
321// |-E { IT_LastChild, IT_Child }
322// `-F { IT_LastChild, IT_LastChild }
323// Type non-last element, last element
324// IT_Child "| " "|-"
325// IT_LastChild " " "`-"
Alexander Kornienko40b66a002012-12-13 13:59:55 +0000326void ASTDumper::indent() {
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000327 if (IsFirstLine)
328 IsFirstLine = false;
329 else
330 OS << "\n";
Richard Trieue8d41192013-01-31 01:44:26 +0000331
332 ColorScope Color(*this, IndentColor);
333 for (llvm::SmallVector<IndentType, 32>::const_iterator I =
334 Indents.begin(), E = Indents.end();
335 I != E; ++I) {
336 switch (*I) {
337 case IT_Child:
338 if (I == E - 1)
339 OS << "|-";
340 else
341 OS << "| ";
342 break;
343 case IT_LastChild:
344 if (I == E - 1)
345 OS << "`-";
346 else
347 OS << " ";
348 break;
349 default:
350 llvm_unreachable("Invalid IndentType");
351 }
352 }
353 Indents.push_back(IT_Child);
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000354}
355
Alexander Kornienko40b66a002012-12-13 13:59:55 +0000356void ASTDumper::unindent() {
Richard Trieue8d41192013-01-31 01:44:26 +0000357 Indents.pop_back();
358}
359
360// Call before each potential last child node is to be dumped. If MoreChildren
361// is false, then this is the last child, otherwise treat as a regular node.
362void ASTDumper::lastChild() {
363 if (!hasMoreChildren())
364 Indents.back() = IT_LastChild;
365}
366
367// MoreChildren should be set before calling another function that may print
368// additional nodes to prevent conflicting final child nodes.
369bool ASTDumper::hasMoreChildren() {
370 return MoreChildren;
371}
372
373void ASTDumper::setMoreChildren(bool Value) {
374 MoreChildren = Value;
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000375}
376
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000377void ASTDumper::dumpPointer(const void *Ptr) {
Richard Trieu7ba443a2013-01-26 01:31:20 +0000378 ColorScope Color(*this, AddressColor);
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000379 OS << ' ' << Ptr;
380}
381
Alexander Kornienko40b66a002012-12-13 13:59:55 +0000382void ASTDumper::dumpLocation(SourceLocation Loc) {
Richard Trieu7ba443a2013-01-26 01:31:20 +0000383 ColorScope Color(*this, LocationColor);
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000384 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Chris Lattnere300c872007-08-30 06:17:34 +0000386 // The general format we print out is filename:line:col, but we drop pieces
387 // that haven't changed since the last loc printed.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000388 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
389
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000390 if (PLoc.isInvalid()) {
391 OS << "<invalid sloc>";
392 return;
393 }
394
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000395 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000396 OS << PLoc.getFilename() << ':' << PLoc.getLine()
397 << ':' << PLoc.getColumn();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000398 LastLocFilename = PLoc.getFilename();
399 LastLocLine = PLoc.getLine();
400 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000401 OS << "line" << ':' << PLoc.getLine()
402 << ':' << PLoc.getColumn();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000403 LastLocLine = PLoc.getLine();
Chris Lattnere300c872007-08-30 06:17:34 +0000404 } else {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000405 OS << "col" << ':' << PLoc.getColumn();
Chris Lattnere300c872007-08-30 06:17:34 +0000406 }
407}
408
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000409void ASTDumper::dumpSourceRange(SourceRange R) {
Chris Lattnere300c872007-08-30 06:17:34 +0000410 // Can't translate locations if a SourceManager isn't available.
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000411 if (!SM)
412 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000414 OS << " <";
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000415 dumpLocation(R.getBegin());
Chris Lattner311ff022007-10-16 22:36:42 +0000416 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000417 OS << ", ";
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000418 dumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000419 }
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000420 OS << ">";
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattnere300c872007-08-30 06:17:34 +0000422 // <t2.c:123:421[blah], t2.c:412:321>
423
424}
425
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000426void ASTDumper::dumpBareType(QualType T) {
Richard Trieu7ba443a2013-01-26 01:31:20 +0000427 ColorScope Color(*this, TypeColor);
428
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000429 SplitQualType T_split = T.split();
430 OS << "'" << QualType::getAsString(T_split) << "'";
431
432 if (!T.isNull()) {
433 // If the type is sugared, also dump a (shallow) desugared type.
434 SplitQualType D_split = T.getSplitDesugaredType();
435 if (T_split != D_split)
436 OS << ":'" << QualType::getAsString(D_split) << "'";
437 }
438}
439
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000440void ASTDumper::dumpType(QualType T) {
441 OS << ' ';
442 dumpBareType(T);
443}
444
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000445void ASTDumper::dumpBareDeclRef(const Decl *D) {
Richard Trieu7ba443a2013-01-26 01:31:20 +0000446 {
447 ColorScope Color(*this, DeclKindNameColor);
448 OS << D->getDeclKindName();
449 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000450 dumpPointer(D);
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000451
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000452 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Richard Trieu7ba443a2013-01-26 01:31:20 +0000453 ColorScope Color(*this, DeclNameColor);
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000454 OS << " '";
455 ND->getDeclName().printName(OS);
456 OS << "'";
457 }
458
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000459 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000460 dumpType(VD->getType());
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000461}
462
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000463void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000464 if (!D)
465 return;
466
467 IndentScope Indent(*this);
468 if (Label)
469 OS << Label << ' ';
470 dumpBareDeclRef(D);
471}
472
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000473void ASTDumper::dumpName(const NamedDecl *ND) {
Richard Trieu7ba443a2013-01-26 01:31:20 +0000474 if (ND->getDeclName()) {
475 ColorScope Color(*this, DeclNameColor);
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000476 OS << ' ' << ND->getNameAsString();
Richard Trieu7ba443a2013-01-26 01:31:20 +0000477 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000478}
479
Richard Trieue8d41192013-01-31 01:44:26 +0000480bool ASTDumper::hasNodes(const DeclContext *DC) {
481 if (!DC)
482 return false;
483
484 return DC->decls_begin() != DC->decls_end();
485}
486
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000487void ASTDumper::dumpDeclContext(const DeclContext *DC) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000488 if (!DC)
489 return;
490 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
Richard Trieue8d41192013-01-31 01:44:26 +0000491 I != E; ++I) {
492 DeclContext::decl_iterator Next = I;
493 ++Next;
494 if (Next == E)
495 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000496 dumpDecl(*I);
Richard Trieue8d41192013-01-31 01:44:26 +0000497 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000498}
499
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000500void ASTDumper::dumpAttr(const Attr *A) {
501 IndentScope Indent(*this);
Richard Trieu7ba443a2013-01-26 01:31:20 +0000502 {
503 ColorScope Color(*this, AttrColor);
504 switch (A->getKind()) {
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000505#define ATTR(X) case attr::X: OS << #X; break;
506#include "clang/Basic/AttrList.inc"
Richard Trieu7ba443a2013-01-26 01:31:20 +0000507 default: llvm_unreachable("unexpected attribute kind");
508 }
509 OS << "Attr";
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000510 }
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000511 dumpPointer(A);
512 dumpSourceRange(A->getRange());
513#include "clang/AST/AttrDump.inc"
514}
515
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000516//===----------------------------------------------------------------------===//
517// C++ Utilities
518//===----------------------------------------------------------------------===//
519
520void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
521 switch (AS) {
522 case AS_none:
523 break;
524 case AS_public:
525 OS << "public";
526 break;
527 case AS_protected:
528 OS << "protected";
529 break;
530 case AS_private:
531 OS << "private";
532 break;
533 }
534}
535
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000536void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000537 IndentScope Indent(*this);
538 OS << "CXXCtorInitializer";
539 if (Init->isAnyMemberInitializer()) {
540 OS << ' ';
541 dumpBareDeclRef(Init->getAnyMember());
542 } else {
543 dumpType(QualType(Init->getBaseClass(), 0));
544 }
545 dumpStmt(Init->getInit());
546}
547
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000548void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000549 if (!TPL)
550 return;
551
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000552 for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000553 I != E; ++I)
554 dumpDecl(*I);
555}
556
557void ASTDumper::dumpTemplateArgumentListInfo(
558 const TemplateArgumentListInfo &TALI) {
Richard Trieue8d41192013-01-31 01:44:26 +0000559 for (unsigned i = 0, e = TALI.size(); i < e; ++i) {
560 if (i + 1 == e)
561 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000562 dumpTemplateArgumentLoc(TALI[i]);
Richard Trieue8d41192013-01-31 01:44:26 +0000563 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000564}
565
566void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
567 dumpTemplateArgument(A.getArgument(), A.getSourceRange());
568}
569
570void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
571 for (unsigned i = 0, e = TAL.size(); i < e; ++i)
572 dumpTemplateArgument(TAL[i]);
573}
574
575void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
576 IndentScope Indent(*this);
577 OS << "TemplateArgument";
578 if (R.isValid())
579 dumpSourceRange(R);
580
581 switch (A.getKind()) {
582 case TemplateArgument::Null:
583 OS << " null";
584 break;
585 case TemplateArgument::Type:
586 OS << " type";
Richard Trieue8d41192013-01-31 01:44:26 +0000587 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000588 dumpType(A.getAsType());
589 break;
590 case TemplateArgument::Declaration:
591 OS << " decl";
Richard Trieue8d41192013-01-31 01:44:26 +0000592 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000593 dumpDeclRef(A.getAsDecl());
594 break;
595 case TemplateArgument::NullPtr:
596 OS << " nullptr";
597 break;
598 case TemplateArgument::Integral:
Alexander Kornienkoad7bb362012-12-20 11:08:38 +0000599 OS << " integral " << A.getAsIntegral();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000600 break;
601 case TemplateArgument::Template:
602 OS << " template ";
603 A.getAsTemplate().dump(OS);
604 break;
605 case TemplateArgument::TemplateExpansion:
606 OS << " template expansion";
607 A.getAsTemplateOrTemplatePattern().dump(OS);
608 break;
609 case TemplateArgument::Expression:
610 OS << " expr";
Richard Trieue8d41192013-01-31 01:44:26 +0000611 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000612 dumpStmt(A.getAsExpr());
613 break;
614 case TemplateArgument::Pack:
615 OS << " pack";
616 for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
Richard Trieue8d41192013-01-31 01:44:26 +0000617 I != E; ++I) {
618 if (I + 1 == E)
619 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000620 dumpTemplateArgument(*I);
Richard Trieue8d41192013-01-31 01:44:26 +0000621 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000622 break;
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000623 }
624}
625
Chris Lattnere300c872007-08-30 06:17:34 +0000626//===----------------------------------------------------------------------===//
Alexander Kornienko21c8b192012-12-11 15:28:09 +0000627// Decl dumping methods.
Chris Lattner6000dac2007-08-08 22:51:59 +0000628//===----------------------------------------------------------------------===//
629
Alexander Kornienko40b66a002012-12-13 13:59:55 +0000630void ASTDumper::dumpDecl(Decl *D) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000631 IndentScope Indent(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000633 if (!D) {
Richard Trieu7ba443a2013-01-26 01:31:20 +0000634 ColorScope Color(*this, NullColor);
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000635 OS << "<<<NULL>>>";
636 return;
Chris Lattner6000dac2007-08-08 22:51:59 +0000637 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000638
Richard Trieu7ba443a2013-01-26 01:31:20 +0000639 {
640 ColorScope Color(*this, DeclKindNameColor);
641 OS << D->getDeclKindName() << "Decl";
642 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000643 dumpPointer(D);
644 dumpSourceRange(D->getSourceRange());
Richard Trieue8d41192013-01-31 01:44:26 +0000645
646 bool HasAttrs = D->hasAttrs() && D->getAttrs().begin() != D->getAttrs().end();
647 bool HasComment = D->getASTContext().getCommentForDecl(D, 0);
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000648 // Decls within functions are visited by the body
Richard Trieue8d41192013-01-31 01:44:26 +0000649 bool HasDeclContext = !isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
650 hasNodes(dyn_cast<DeclContext>(D));
651
652 setMoreChildren(HasAttrs || HasComment || HasDeclContext);
653 DeclVisitor<ASTDumper>::Visit(D);
654
655 setMoreChildren(HasComment || HasDeclContext);
656 if (HasAttrs) {
657 for (AttrVec::const_iterator I = D->getAttrs().begin(),
658 E = D->getAttrs().end(); I != E; ++I) {
659 if (I + 1 == E)
660 lastChild();
661 dumpAttr(*I);
662 }
663 }
664
665 setMoreChildren(HasDeclContext);
666 lastChild();
667 dumpFullComment(D->getASTContext().getCommentForDecl(D, 0));
668
669 setMoreChildren(false);
670 if (HasDeclContext)
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000671 dumpDeclContext(dyn_cast<DeclContext>(D));
672}
673
674void ASTDumper::VisitLabelDecl(LabelDecl *D) {
675 dumpName(D);
676}
677
678void ASTDumper::VisitTypedefDecl(TypedefDecl *D) {
679 dumpName(D);
680 dumpType(D->getUnderlyingType());
681 if (D->isModulePrivate())
682 OS << " __module_private__";
683}
684
685void ASTDumper::VisitEnumDecl(EnumDecl *D) {
686 if (D->isScoped()) {
687 if (D->isScopedUsingClassTag())
688 OS << " class";
689 else
690 OS << " struct";
691 }
692 dumpName(D);
693 if (D->isModulePrivate())
694 OS << " __module_private__";
695 if (D->isFixed())
696 dumpType(D->getIntegerType());
697}
698
699void ASTDumper::VisitRecordDecl(RecordDecl *D) {
700 OS << ' ' << D->getKindName();
701 dumpName(D);
702 if (D->isModulePrivate())
703 OS << " __module_private__";
704}
705
706void ASTDumper::VisitEnumConstantDecl(EnumConstantDecl *D) {
707 dumpName(D);
708 dumpType(D->getType());
Richard Trieue8d41192013-01-31 01:44:26 +0000709 if (Expr *Init = D->getInitExpr()) {
710 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000711 dumpStmt(Init);
Richard Trieue8d41192013-01-31 01:44:26 +0000712 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000713}
714
715void ASTDumper::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
716 dumpName(D);
717 dumpType(D->getType());
718 for (IndirectFieldDecl::chain_iterator I = D->chain_begin(),
Richard Trieue8d41192013-01-31 01:44:26 +0000719 E = D->chain_end(); I != E; ++I) {
720 if (I + 1 == E)
721 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000722 dumpDeclRef(*I);
Richard Trieue8d41192013-01-31 01:44:26 +0000723 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000724}
725
726void ASTDumper::VisitFunctionDecl(FunctionDecl *D) {
727 dumpName(D);
728 dumpType(D->getType());
729
730 StorageClass SC = D->getStorageClassAsWritten();
731 if (SC != SC_None)
732 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
733 if (D->isInlineSpecified())
734 OS << " inline";
735 if (D->isVirtualAsWritten())
736 OS << " virtual";
737 if (D->isModulePrivate())
738 OS << " __module_private__";
739
740 if (D->isPure())
741 OS << " pure";
742 else if (D->isDeletedAsWritten())
743 OS << " delete";
744
Richard Trieue8d41192013-01-31 01:44:26 +0000745 bool OldMoreChildren = hasMoreChildren();
746 const FunctionTemplateSpecializationInfo *FTSI =
747 D->getTemplateSpecializationInfo();
748 bool HasTemplateSpecialization = FTSI;
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000749
Richard Trieue8d41192013-01-31 01:44:26 +0000750 bool HasNamedDecls = D->getDeclsInPrototypeScope().begin() !=
751 D->getDeclsInPrototypeScope().end();
752
753 bool HasFunctionDecls = D->param_begin() != D->param_end();
754
755 CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D);
756 bool HasCtorInitializers = C && C->init_begin() != C->init_end();
757
758 bool HasDeclarationBody = D->doesThisDeclarationHaveABody();
759
760 setMoreChildren(OldMoreChildren || HasNamedDecls || HasFunctionDecls ||
761 HasCtorInitializers || HasDeclarationBody);
762 if (HasTemplateSpecialization) {
763 lastChild();
764 dumpTemplateArgumentList(*FTSI->TemplateArguments);
765 }
766
767 setMoreChildren(OldMoreChildren || HasFunctionDecls ||
768 HasCtorInitializers || HasDeclarationBody);
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000769 for (ArrayRef<NamedDecl *>::iterator
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000770 I = D->getDeclsInPrototypeScope().begin(),
Richard Trieue8d41192013-01-31 01:44:26 +0000771 E = D->getDeclsInPrototypeScope().end(); I != E; ++I) {
772 if (I + 1 == E)
773 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000774 dumpDecl(*I);
Richard Trieue8d41192013-01-31 01:44:26 +0000775 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000776
Richard Trieue8d41192013-01-31 01:44:26 +0000777 setMoreChildren(OldMoreChildren || HasCtorInitializers || HasDeclarationBody);
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000778 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
Richard Trieue8d41192013-01-31 01:44:26 +0000779 I != E; ++I) {
780 if (I + 1 == E)
781 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000782 dumpDecl(*I);
Richard Trieue8d41192013-01-31 01:44:26 +0000783 }
784
785 setMoreChildren(OldMoreChildren || HasDeclarationBody);
786 if (HasCtorInitializers)
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000787 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
Richard Trieue8d41192013-01-31 01:44:26 +0000788 E = C->init_end(); I != E; ++I) {
789 if (I + 1 == E)
790 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000791 dumpCXXCtorInitializer(*I);
Richard Trieue8d41192013-01-31 01:44:26 +0000792 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000793
Richard Trieue8d41192013-01-31 01:44:26 +0000794 setMoreChildren(OldMoreChildren);
795 if (HasDeclarationBody) {
796 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000797 dumpStmt(D->getBody());
Richard Trieue8d41192013-01-31 01:44:26 +0000798 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000799}
800
801void ASTDumper::VisitFieldDecl(FieldDecl *D) {
802 dumpName(D);
803 dumpType(D->getType());
804 if (D->isMutable())
805 OS << " mutable";
806 if (D->isModulePrivate())
807 OS << " __module_private__";
Richard Trieue8d41192013-01-31 01:44:26 +0000808
809 bool OldMoreChildren = hasMoreChildren();
810 bool IsBitField = D->isBitField();
811 Expr *Init = D->getInClassInitializer();
812 bool HasInit = Init;
813
814 setMoreChildren(OldMoreChildren || HasInit);
815 if (IsBitField) {
816 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000817 dumpStmt(D->getBitWidth());
Richard Trieue8d41192013-01-31 01:44:26 +0000818 }
819 setMoreChildren(OldMoreChildren);
820 if (HasInit) {
821 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000822 dumpStmt(Init);
Richard Trieue8d41192013-01-31 01:44:26 +0000823 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000824}
825
826void ASTDumper::VisitVarDecl(VarDecl *D) {
827 dumpName(D);
828 dumpType(D->getType());
829 StorageClass SC = D->getStorageClassAsWritten();
830 if (SC != SC_None)
831 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
832 if (D->isThreadSpecified())
833 OS << " __thread";
834 if (D->isModulePrivate())
835 OS << " __module_private__";
836 if (D->isNRVOVariable())
837 OS << " nrvo";
Richard Trieue8d41192013-01-31 01:44:26 +0000838 if (D->hasInit()) {
839 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000840 dumpStmt(D->getInit());
Richard Trieue8d41192013-01-31 01:44:26 +0000841 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000842}
843
844void ASTDumper::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
Richard Trieue8d41192013-01-31 01:44:26 +0000845 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000846 dumpStmt(D->getAsmString());
847}
848
849void ASTDumper::VisitImportDecl(ImportDecl *D) {
850 OS << ' ' << D->getImportedModule()->getFullModuleName();
851}
852
853//===----------------------------------------------------------------------===//
854// C++ Declarations
855//===----------------------------------------------------------------------===//
856
857void ASTDumper::VisitNamespaceDecl(NamespaceDecl *D) {
858 dumpName(D);
859 if (D->isInline())
860 OS << " inline";
861 if (!D->isOriginalNamespace())
862 dumpDeclRef(D->getOriginalNamespace(), "original");
863}
864
865void ASTDumper::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
866 OS << ' ';
867 dumpBareDeclRef(D->getNominatedNamespace());
868}
869
870void ASTDumper::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
871 dumpName(D);
872 dumpDeclRef(D->getAliasedNamespace());
873}
874
875void ASTDumper::VisitTypeAliasDecl(TypeAliasDecl *D) {
876 dumpName(D);
877 dumpType(D->getUnderlyingType());
878}
879
880void ASTDumper::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
881 dumpName(D);
882 dumpTemplateParameters(D->getTemplateParameters());
883 dumpDecl(D->getTemplatedDecl());
884}
885
886void ASTDumper::VisitCXXRecordDecl(CXXRecordDecl *D) {
887 VisitRecordDecl(D);
888 if (!D->isCompleteDefinition())
889 return;
890
891 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
892 E = D->bases_end(); I != E; ++I) {
893 IndentScope Indent(*this);
894 if (I->isVirtual())
895 OS << "virtual ";
896 dumpAccessSpecifier(I->getAccessSpecifier());
897 dumpType(I->getType());
898 if (I->isPackExpansion())
899 OS << "...";
900 }
901}
902
903void ASTDumper::VisitStaticAssertDecl(StaticAssertDecl *D) {
904 dumpStmt(D->getAssertExpr());
Richard Trieue8d41192013-01-31 01:44:26 +0000905 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000906 dumpStmt(D->getMessage());
907}
908
909void ASTDumper::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
910 dumpName(D);
911 dumpTemplateParameters(D->getTemplateParameters());
912 dumpDecl(D->getTemplatedDecl());
913 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(),
914 E = D->spec_end(); I != E; ++I) {
Richard Trieue8d41192013-01-31 01:44:26 +0000915 FunctionTemplateDecl::spec_iterator Next = I;
916 ++Next;
917 if (Next == E)
918 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000919 switch (I->getTemplateSpecializationKind()) {
920 case TSK_Undeclared:
921 case TSK_ImplicitInstantiation:
922 case TSK_ExplicitInstantiationDeclaration:
923 case TSK_ExplicitInstantiationDefinition:
924 dumpDecl(*I);
925 break;
926 case TSK_ExplicitSpecialization:
927 dumpDeclRef(*I);
928 break;
929 }
930 }
931}
932
933void ASTDumper::VisitClassTemplateDecl(ClassTemplateDecl *D) {
934 dumpName(D);
935 dumpTemplateParameters(D->getTemplateParameters());
Richard Trieue8d41192013-01-31 01:44:26 +0000936
937 if (D->spec_begin() == D->spec_end())
938 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000939 dumpDecl(D->getTemplatedDecl());
940 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
941 I != E; ++I) {
Richard Trieue8d41192013-01-31 01:44:26 +0000942 ClassTemplateDecl::spec_iterator Next = I;
943 ++Next;
944 if (Next == E)
945 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000946 switch (I->getTemplateSpecializationKind()) {
947 case TSK_Undeclared:
948 case TSK_ImplicitInstantiation:
949 dumpDecl(*I);
950 break;
951 case TSK_ExplicitSpecialization:
952 case TSK_ExplicitInstantiationDeclaration:
953 case TSK_ExplicitInstantiationDefinition:
954 dumpDeclRef(*I);
955 break;
956 }
957 }
958}
959
960void ASTDumper::VisitClassTemplateSpecializationDecl(
961 ClassTemplateSpecializationDecl *D) {
962 VisitCXXRecordDecl(D);
963 dumpTemplateArgumentList(D->getTemplateArgs());
964}
965
966void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
967 ClassTemplatePartialSpecializationDecl *D) {
968 VisitClassTemplateSpecializationDecl(D);
969 dumpTemplateParameters(D->getTemplateParameters());
970}
971
972void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
973 ClassScopeFunctionSpecializationDecl *D) {
974 dumpDeclRef(D->getSpecialization());
975 if (D->hasExplicitTemplateArgs())
976 dumpTemplateArgumentListInfo(D->templateArgs());
977}
978
979void ASTDumper::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
980 if (D->wasDeclaredWithTypename())
981 OS << " typename";
982 else
983 OS << " class";
984 if (D->isParameterPack())
985 OS << " ...";
986 dumpName(D);
987 if (D->hasDefaultArgument())
988 dumpType(D->getDefaultArgument());
989}
990
991void ASTDumper::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
992 dumpType(D->getType());
993 if (D->isParameterPack())
994 OS << " ...";
995 dumpName(D);
996 if (D->hasDefaultArgument())
997 dumpStmt(D->getDefaultArgument());
998}
999
1000void ASTDumper::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
1001 if (D->isParameterPack())
1002 OS << " ...";
1003 dumpName(D);
1004 dumpTemplateParameters(D->getTemplateParameters());
1005 if (D->hasDefaultArgument())
1006 dumpTemplateArgumentLoc(D->getDefaultArgument());
1007}
1008
1009void ASTDumper::VisitUsingDecl(UsingDecl *D) {
1010 OS << ' ';
1011 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1012 OS << D->getNameAsString();
1013}
1014
1015void
1016ASTDumper::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1017 OS << ' ';
1018 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1019 OS << D->getNameAsString();
1020}
1021
1022void ASTDumper::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1023 OS << ' ';
1024 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1025 OS << D->getNameAsString();
1026 dumpType(D->getType());
1027}
1028
1029void ASTDumper::VisitUsingShadowDecl(UsingShadowDecl *D) {
1030 OS << ' ';
1031 dumpBareDeclRef(D->getTargetDecl());
1032}
1033
1034void ASTDumper::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1035 switch (D->getLanguage()) {
1036 case LinkageSpecDecl::lang_c: OS << " C"; break;
1037 case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1038 }
1039}
1040
1041void ASTDumper::VisitAccessSpecDecl(AccessSpecDecl *D) {
1042 OS << ' ';
1043 dumpAccessSpecifier(D->getAccess());
1044}
1045
1046void ASTDumper::VisitFriendDecl(FriendDecl *D) {
1047 if (TypeSourceInfo *T = D->getFriendType())
1048 dumpType(T->getType());
1049 else
1050 dumpDecl(D->getFriendDecl());
1051}
1052
1053//===----------------------------------------------------------------------===//
1054// Obj-C Declarations
1055//===----------------------------------------------------------------------===//
1056
1057void ASTDumper::VisitObjCIvarDecl(ObjCIvarDecl *D) {
1058 dumpName(D);
1059 dumpType(D->getType());
1060 if (D->getSynthesize())
1061 OS << " synthesize";
1062
1063 switch (D->getAccessControl()) {
1064 case ObjCIvarDecl::None:
1065 OS << " none";
1066 break;
1067 case ObjCIvarDecl::Private:
1068 OS << " private";
1069 break;
1070 case ObjCIvarDecl::Protected:
1071 OS << " protected";
1072 break;
1073 case ObjCIvarDecl::Public:
1074 OS << " public";
1075 break;
1076 case ObjCIvarDecl::Package:
1077 OS << " package";
1078 break;
1079 }
1080}
1081
1082void ASTDumper::VisitObjCMethodDecl(ObjCMethodDecl *D) {
1083 if (D->isInstanceMethod())
1084 OS << " -";
1085 else
1086 OS << " +";
1087 dumpName(D);
1088 dumpType(D->getResultType());
1089
Richard Trieue8d41192013-01-31 01:44:26 +00001090 bool OldMoreChildren = hasMoreChildren();
1091 bool IsVariadic = D->isVariadic();
1092 bool HasBody = D->hasBody();
1093
1094 setMoreChildren(OldMoreChildren || IsVariadic || HasBody);
1095 if (D->isThisDeclarationADefinition()) {
1096 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001097 dumpDeclContext(D);
Richard Trieue8d41192013-01-31 01:44:26 +00001098 } else {
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001099 for (ObjCMethodDecl::param_iterator I = D->param_begin(),
1100 E = D->param_end(); I != E; ++I) {
Richard Trieue8d41192013-01-31 01:44:26 +00001101 if (I + 1 == E)
1102 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001103 dumpDecl(*I);
1104 }
1105 }
1106
Richard Trieue8d41192013-01-31 01:44:26 +00001107 setMoreChildren(OldMoreChildren || HasBody);
1108 if (IsVariadic) {
1109 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001110 IndentScope Indent(*this);
1111 OS << "...";
1112 }
1113
Richard Trieue8d41192013-01-31 01:44:26 +00001114 setMoreChildren(OldMoreChildren);
1115 if (HasBody) {
1116 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001117 dumpStmt(D->getBody());
Richard Trieue8d41192013-01-31 01:44:26 +00001118 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001119}
1120
1121void ASTDumper::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
1122 dumpName(D);
1123 dumpDeclRef(D->getClassInterface());
Richard Trieue8d41192013-01-31 01:44:26 +00001124 if (D->protocol_begin() == D->protocol_end())
1125 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001126 dumpDeclRef(D->getImplementation());
1127 for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
Richard Trieue8d41192013-01-31 01:44:26 +00001128 E = D->protocol_end(); I != E; ++I) {
1129 if (I + 1 == E)
1130 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001131 dumpDeclRef(*I);
Richard Trieue8d41192013-01-31 01:44:26 +00001132 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001133}
1134
1135void ASTDumper::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
1136 dumpName(D);
1137 dumpDeclRef(D->getClassInterface());
Richard Trieue8d41192013-01-31 01:44:26 +00001138 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001139 dumpDeclRef(D->getCategoryDecl());
1140}
1141
1142void ASTDumper::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
1143 dumpName(D);
1144 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
Richard Trieue8d41192013-01-31 01:44:26 +00001145 E = D->protocol_end(); I != E; ++I) {
1146 if (I + 1 == E)
1147 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001148 dumpDeclRef(*I);
Richard Trieue8d41192013-01-31 01:44:26 +00001149 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001150}
1151
1152void ASTDumper::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
1153 dumpName(D);
1154 dumpDeclRef(D->getSuperClass(), "super");
Richard Trieue8d41192013-01-31 01:44:26 +00001155 if (D->protocol_begin() == D->protocol_end())
1156 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001157 dumpDeclRef(D->getImplementation());
1158 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
Richard Trieue8d41192013-01-31 01:44:26 +00001159 E = D->protocol_end(); I != E; ++I) {
1160 if (I + 1 == E)
1161 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001162 dumpDeclRef(*I);
Richard Trieue8d41192013-01-31 01:44:26 +00001163 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001164}
1165
1166void ASTDumper::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
1167 dumpName(D);
1168 dumpDeclRef(D->getSuperClass(), "super");
Richard Trieue8d41192013-01-31 01:44:26 +00001169 if (D->init_begin() == D->init_end())
1170 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001171 dumpDeclRef(D->getClassInterface());
1172 for (ObjCImplementationDecl::init_iterator I = D->init_begin(),
Richard Trieue8d41192013-01-31 01:44:26 +00001173 E = D->init_end(); I != E; ++I) {
1174 if (I + 1 == E)
1175 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001176 dumpCXXCtorInitializer(*I);
Richard Trieue8d41192013-01-31 01:44:26 +00001177 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001178}
1179
1180void ASTDumper::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
1181 dumpName(D);
Richard Trieue8d41192013-01-31 01:44:26 +00001182 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001183 dumpDeclRef(D->getClassInterface());
1184}
1185
1186void ASTDumper::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
1187 dumpName(D);
1188 dumpType(D->getType());
1189
1190 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1191 OS << " required";
1192 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1193 OS << " optional";
1194
1195 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1196 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1197 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1198 OS << " readonly";
1199 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1200 OS << " assign";
1201 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1202 OS << " readwrite";
1203 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1204 OS << " retain";
1205 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1206 OS << " copy";
1207 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1208 OS << " nonatomic";
1209 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1210 OS << " atomic";
1211 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1212 OS << " weak";
1213 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1214 OS << " strong";
1215 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1216 OS << " unsafe_unretained";
Richard Trieue8d41192013-01-31 01:44:26 +00001217 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter) {
1218 if (!(Attrs & ObjCPropertyDecl::OBJC_PR_setter))
1219 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001220 dumpDeclRef(D->getGetterMethodDecl(), "getter");
Richard Trieue8d41192013-01-31 01:44:26 +00001221 }
1222 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter) {
1223 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001224 dumpDeclRef(D->getSetterMethodDecl(), "setter");
Richard Trieue8d41192013-01-31 01:44:26 +00001225 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001226 }
1227}
1228
1229void ASTDumper::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
1230 dumpName(D->getPropertyDecl());
1231 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1232 OS << " synthesize";
1233 else
1234 OS << " dynamic";
1235 dumpDeclRef(D->getPropertyDecl());
Richard Trieue8d41192013-01-31 01:44:26 +00001236 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001237 dumpDeclRef(D->getPropertyIvarDecl());
1238}
1239
1240void ASTDumper::VisitBlockDecl(BlockDecl *D) {
1241 for (BlockDecl::param_iterator I = D->param_begin(), E = D->param_end();
1242 I != E; ++I)
1243 dumpDecl(*I);
1244
1245 if (D->isVariadic()) {
1246 IndentScope Indent(*this);
1247 OS << "...";
1248 }
1249
1250 if (D->capturesCXXThis()) {
1251 IndentScope Indent(*this);
1252 OS << "capture this";
1253 }
1254 for (BlockDecl::capture_iterator I = D->capture_begin(),
1255 E = D->capture_end(); I != E; ++I) {
1256 IndentScope Indent(*this);
1257 OS << "capture";
1258 if (I->isByRef())
1259 OS << " byref";
1260 if (I->isNested())
1261 OS << " nested";
1262 if (I->getVariable()) {
1263 OS << ' ';
1264 dumpBareDeclRef(I->getVariable());
1265 }
1266 if (I->hasCopyExpr())
1267 dumpStmt(I->getCopyExpr());
1268 }
Richard Trieue8d41192013-01-31 01:44:26 +00001269 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001270 dumpStmt(D->getBody());
Chris Lattner6000dac2007-08-08 22:51:59 +00001271}
1272
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001273//===----------------------------------------------------------------------===//
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001274// Stmt dumping methods.
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001275//===----------------------------------------------------------------------===//
1276
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001277void ASTDumper::dumpStmt(Stmt *S) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001278 IndentScope Indent(*this);
1279
1280 if (!S) {
Richard Trieu7ba443a2013-01-26 01:31:20 +00001281 ColorScope Color(*this, NullColor);
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001282 OS << "<<<NULL>>>";
1283 return;
1284 }
1285
1286 if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
1287 VisitDeclStmt(DS);
1288 return;
1289 }
1290
Richard Trieue8d41192013-01-31 01:44:26 +00001291 setMoreChildren(S->children());
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001292 StmtVisitor<ASTDumper>::Visit(S);
Richard Trieue8d41192013-01-31 01:44:26 +00001293 setMoreChildren(false);
1294 for (Stmt::child_range CI = S->children(); CI; ++CI) {
1295 Stmt::child_range Next = CI;
1296 ++Next;
1297 if (!Next)
1298 lastChild();
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001299 dumpStmt(*CI);
Richard Trieue8d41192013-01-31 01:44:26 +00001300 }
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001301}
1302
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001303void ASTDumper::VisitStmt(Stmt *Node) {
Richard Trieu7ba443a2013-01-26 01:31:20 +00001304 {
1305 ColorScope Color(*this, StmtColor);
1306 OS << Node->getStmtClassName();
1307 }
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001308 dumpPointer(Node);
1309 dumpSourceRange(Node->getSourceRange());
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001310}
1311
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001312void ASTDumper::VisitDeclStmt(DeclStmt *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001313 VisitStmt(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001314 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
Richard Trieue8d41192013-01-31 01:44:26 +00001315 I != E; ++I) {
1316 if (I + 1 == E)
1317 lastChild();
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001318 dumpDecl(*I);
Richard Trieue8d41192013-01-31 01:44:26 +00001319 }
Ted Kremenek5399ce22007-12-12 06:59:42 +00001320}
1321
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +00001322void ASTDumper::VisitAttributedStmt(AttributedStmt *Node) {
1323 VisitStmt(Node);
1324 for (ArrayRef<const Attr*>::iterator I = Node->getAttrs().begin(),
Richard Trieue8d41192013-01-31 01:44:26 +00001325 E = Node->getAttrs().end(); I != E; ++I) {
1326 if (I + 1 == E)
1327 lastChild();
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +00001328 dumpAttr(*I);
Richard Trieue8d41192013-01-31 01:44:26 +00001329 }
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +00001330}
1331
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001332void ASTDumper::VisitLabelStmt(LabelStmt *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001333 VisitStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001334 OS << " '" << Node->getName() << "'";
Chris Lattner6000dac2007-08-08 22:51:59 +00001335}
1336
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001337void ASTDumper::VisitGotoStmt(GotoStmt *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001338 VisitStmt(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001339 OS << " '" << Node->getLabel()->getName() << "'";
1340 dumpPointer(Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +00001341}
1342
Chris Lattner6000dac2007-08-08 22:51:59 +00001343//===----------------------------------------------------------------------===//
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001344// Expr dumping methods.
Chris Lattner6000dac2007-08-08 22:51:59 +00001345//===----------------------------------------------------------------------===//
1346
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001347void ASTDumper::VisitExpr(Expr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001348 VisitStmt(Node);
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001349 dumpType(Node->getType());
1350
Richard Trieu7ba443a2013-01-26 01:31:20 +00001351 {
1352 ColorScope Color(*this, ValueKindColor);
1353 switch (Node->getValueKind()) {
1354 case VK_RValue:
1355 break;
1356 case VK_LValue:
1357 OS << " lvalue";
1358 break;
1359 case VK_XValue:
1360 OS << " xvalue";
1361 break;
1362 }
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001363 }
1364
Richard Trieu7ba443a2013-01-26 01:31:20 +00001365 {
1366 ColorScope Color(*this, ObjectKindColor);
1367 switch (Node->getObjectKind()) {
1368 case OK_Ordinary:
1369 break;
1370 case OK_BitField:
1371 OS << " bitfield";
1372 break;
1373 case OK_ObjCProperty:
1374 OS << " objcproperty";
1375 break;
1376 case OK_ObjCSubscript:
1377 OS << " objcsubscript";
1378 break;
1379 case OK_VectorComponent:
1380 OS << " vectorcomponent";
1381 break;
1382 }
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001383 }
Chris Lattner6000dac2007-08-08 22:51:59 +00001384}
1385
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001386static void dumpBasePath(raw_ostream &OS, CastExpr *Node) {
John McCallf871d0c2010-08-07 06:22:56 +00001387 if (Node->path_empty())
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001388 return;
1389
1390 OS << " (";
1391 bool First = true;
John McCallf871d0c2010-08-07 06:22:56 +00001392 for (CastExpr::path_iterator
1393 I = Node->path_begin(), E = Node->path_end(); I != E; ++I) {
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001394 const CXXBaseSpecifier *Base = *I;
1395 if (!First)
1396 OS << " -> ";
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001397
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001398 const CXXRecordDecl *RD =
1399 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001400
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001401 if (Base->isVirtual())
1402 OS << "virtual ";
1403 OS << RD->getName();
1404 First = false;
1405 }
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001406
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001407 OS << ')';
1408}
1409
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001410void ASTDumper::VisitCastExpr(CastExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001411 VisitExpr(Node);
Richard Trieu7ba443a2013-01-26 01:31:20 +00001412 OS << " <";
1413 {
1414 ColorScope Color(*this, CastColor);
1415 OS << Node->getCastKindName();
1416 }
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001417 dumpBasePath(OS, Node);
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001418 OS << ">";
Anders Carlsson27a5b9b2009-08-22 23:33:40 +00001419}
1420
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001421void ASTDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001422 VisitExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +00001423
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001424 OS << " ";
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001425 dumpBareDeclRef(Node->getDecl());
Chandler Carruth3aa81402011-05-01 23:48:14 +00001426 if (Node->getDecl() != Node->getFoundDecl()) {
1427 OS << " (";
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001428 dumpBareDeclRef(Node->getFoundDecl());
Chandler Carruth3aa81402011-05-01 23:48:14 +00001429 OS << ")";
1430 }
John McCall6b5a61b2011-02-07 10:33:21 +00001431}
1432
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001433void ASTDumper::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001434 VisitExpr(Node);
John McCall9d5f35e2009-12-11 21:50:11 +00001435 OS << " (";
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001436 if (!Node->requiresADL())
1437 OS << "no ";
Benjamin Kramer900fc632010-04-17 09:33:03 +00001438 OS << "ADL) = '" << Node->getName() << '\'';
John McCall9d5f35e2009-12-11 21:50:11 +00001439
1440 UnresolvedLookupExpr::decls_iterator
1441 I = Node->decls_begin(), E = Node->decls_end();
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001442 if (I == E)
1443 OS << " empty";
John McCall9d5f35e2009-12-11 21:50:11 +00001444 for (; I != E; ++I)
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001445 dumpPointer(*I);
John McCall9d5f35e2009-12-11 21:50:11 +00001446}
1447
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001448void ASTDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001449 VisitExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +00001450
Richard Trieu7ba443a2013-01-26 01:31:20 +00001451 {
1452 ColorScope Color(*this, DeclKindNameColor);
1453 OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
1454 }
1455 OS << "='" << *Node->getDecl() << "'";
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001456 dumpPointer(Node->getDecl());
Steve Naroff218543b2008-05-23 22:01:24 +00001457 if (Node->isFreeIvar())
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001458 OS << " isFreeIvar";
Steve Naroff3c64d9e2008-03-12 13:19:12 +00001459}
1460
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001461void ASTDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001462 VisitExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +00001463 switch (Node->getIdentType()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001464 default: llvm_unreachable("unknown case");
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001465 case PredefinedExpr::Func: OS << " __func__"; break;
1466 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001467 case PredefinedExpr::LFunction: OS << " L__FUNCTION__"; break;
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001468 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
Chris Lattner6000dac2007-08-08 22:51:59 +00001469 }
1470}
1471
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001472void ASTDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001473 VisitExpr(Node);
Richard Trieu7ba443a2013-01-26 01:31:20 +00001474 ColorScope Color(*this, ValueColor);
Richard Trieu49cf8842011-11-03 23:56:23 +00001475 OS << " " << Node->getValue();
Chris Lattner6000dac2007-08-08 22:51:59 +00001476}
1477
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001478void ASTDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001479 VisitExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +00001480
1481 bool isSigned = Node->getType()->isSignedIntegerType();
Richard Trieu7ba443a2013-01-26 01:31:20 +00001482 ColorScope Color(*this, ValueColor);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001483 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattner6000dac2007-08-08 22:51:59 +00001484}
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001485
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001486void ASTDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001487 VisitExpr(Node);
Richard Trieu7ba443a2013-01-26 01:31:20 +00001488 ColorScope Color(*this, ValueColor);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001489 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattner6000dac2007-08-08 22:51:59 +00001490}
Chris Lattner5d661452007-08-26 03:42:43 +00001491
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001492void ASTDumper::VisitStringLiteral(StringLiteral *Str) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001493 VisitExpr(Str);
Richard Trieu7ba443a2013-01-26 01:31:20 +00001494 ColorScope Color(*this, ValueColor);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001495 OS << " ";
Richard Trieu8ab09da2012-06-13 20:25:24 +00001496 Str->outputString(OS);
Chris Lattner6000dac2007-08-08 22:51:59 +00001497}
Chris Lattner17a1a722007-08-30 01:00:35 +00001498
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001499void ASTDumper::VisitUnaryOperator(UnaryOperator *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001500 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001501 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
1502 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner6000dac2007-08-08 22:51:59 +00001503}
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001504
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001505void ASTDumper::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001506 VisitExpr(Node);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001507 switch(Node->getKind()) {
1508 case UETT_SizeOf:
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001509 OS << " sizeof";
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001510 break;
1511 case UETT_AlignOf:
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001512 OS << " alignof";
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001513 break;
1514 case UETT_VecStep:
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001515 OS << " vec_step";
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001516 break;
1517 }
Sebastian Redl05189992008-11-11 17:56:53 +00001518 if (Node->isArgumentType())
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001519 dumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +00001520}
Chris Lattner13cb21f2007-08-09 17:35:30 +00001521
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001522void ASTDumper::VisitMemberExpr(MemberExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001523 VisitExpr(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001524 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
1525 dumpPointer(Node->getMemberDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +00001526}
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001527
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001528void ASTDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001529 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001530 OS << " " << Node->getAccessor().getNameStart();
Chris Lattner6000dac2007-08-08 22:51:59 +00001531}
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001532
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001533void ASTDumper::VisitBinaryOperator(BinaryOperator *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001534 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001535 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnereb14fe82007-08-25 02:00:02 +00001536}
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001537
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001538void ASTDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001539 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001540 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
1541 << "' ComputeLHSTy=";
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001542 dumpBareType(Node->getComputationLHSType());
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001543 OS << " ComputeResultTy=";
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001544 dumpBareType(Node->getComputationResultType());
Chris Lattner6000dac2007-08-08 22:51:59 +00001545}
Chris Lattner6000dac2007-08-08 22:51:59 +00001546
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001547void ASTDumper::VisitBlockExpr(BlockExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001548 VisitExpr(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001549 dumpDecl(Node->getBlockDecl());
John McCall6b5a61b2011-02-07 10:33:21 +00001550}
1551
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001552void ASTDumper::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001553 VisitExpr(Node);
John McCall4b9c2d22011-11-06 09:01:30 +00001554
Richard Trieue8d41192013-01-31 01:44:26 +00001555 if (Expr *Source = Node->getSourceExpr()) {
1556 lastChild();
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001557 dumpStmt(Source);
Richard Trieue8d41192013-01-31 01:44:26 +00001558 }
John McCall4b9c2d22011-11-06 09:01:30 +00001559}
1560
Chris Lattner6000dac2007-08-08 22:51:59 +00001561// GNU extensions.
1562
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001563void ASTDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001564 VisitExpr(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001565 OS << " " << Node->getLabel()->getName();
1566 dumpPointer(Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +00001567}
1568
Chris Lattnerf9e05812007-08-09 18:03:18 +00001569//===----------------------------------------------------------------------===//
1570// C++ Expressions
1571//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +00001572
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001573void ASTDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001574 VisitExpr(Node);
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001575 OS << " " << Node->getCastName()
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001576 << "<" << Node->getTypeAsWritten().getAsString() << ">"
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001577 << " <" << Node->getCastKindName();
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001578 dumpBasePath(OS, Node);
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001579 OS << ">";
Chris Lattner6000dac2007-08-08 22:51:59 +00001580}
1581
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001582void ASTDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001583 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001584 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +00001585}
1586
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001587void ASTDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001588 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001589 OS << " this";
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001590}
1591
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001592void ASTDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001593 VisitExpr(Node);
Eli Friedmancc2fca22011-09-02 17:38:59 +00001594 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
1595 << " <" << Node->getCastKindName() << ">";
Douglas Gregor49badde2008-10-27 19:41:14 +00001596}
1597
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001598void ASTDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001599 VisitExpr(Node);
John McCalld4bbdfe2010-02-02 19:03:45 +00001600 CXXConstructorDecl *Ctor = Node->getConstructor();
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001601 dumpType(Ctor->getType());
Anders Carlsson0eca1b62009-08-12 00:21:52 +00001602 if (Node->isElidable())
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001603 OS << " elidable";
John McCallf8cf0b02010-08-07 06:38:55 +00001604 if (Node->requiresZeroInitialization())
1605 OS << " zeroing";
Anders Carlsson0eca1b62009-08-12 00:21:52 +00001606}
1607
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001608void ASTDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001609 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001610 OS << " ";
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001611 dumpCXXTemporary(Node->getTemporary());
Anders Carlsson0eca1b62009-08-12 00:21:52 +00001612}
1613
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001614void ASTDumper::VisitExprWithCleanups(ExprWithCleanups *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001615 VisitExpr(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001616 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
1617 dumpDeclRef(Node->getObject(i), "cleanup");
Anders Carlsson0eca1b62009-08-12 00:21:52 +00001618}
1619
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001620void ASTDumper::dumpCXXTemporary(CXXTemporary *Temporary) {
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001621 OS << "(CXXTemporary";
1622 dumpPointer(Temporary);
1623 OS << ")";
Anders Carlsson0eca1b62009-08-12 00:21:52 +00001624}
1625
Anders Carlsson55085182007-08-21 17:43:55 +00001626//===----------------------------------------------------------------------===//
1627// Obj-C Expressions
1628//===----------------------------------------------------------------------===//
1629
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001630void ASTDumper::VisitObjCMessageExpr(ObjCMessageExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001631 VisitExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001632 OS << " selector=" << Node->getSelector().getAsString();
Douglas Gregor04badcf2010-04-21 00:45:42 +00001633 switch (Node->getReceiverKind()) {
1634 case ObjCMessageExpr::Instance:
1635 break;
1636
1637 case ObjCMessageExpr::Class:
1638 OS << " class=";
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001639 dumpBareType(Node->getClassReceiver());
Douglas Gregor04badcf2010-04-21 00:45:42 +00001640 break;
1641
1642 case ObjCMessageExpr::SuperInstance:
1643 OS << " super (instance)";
1644 break;
1645
1646 case ObjCMessageExpr::SuperClass:
1647 OS << " super (class)";
1648 break;
1649 }
Ted Kremenekb3d914b2008-02-29 22:04:05 +00001650}
1651
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001652void ASTDumper::VisitObjCBoxedExpr(ObjCBoxedExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001653 VisitExpr(Node);
Argyrios Kyrtzidis36faadd2012-05-10 20:02:31 +00001654 OS << " selector=" << Node->getBoxingMethod()->getSelector().getAsString();
1655}
1656
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001657void ASTDumper::VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001658 VisitStmt(Node);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001659 if (VarDecl *CatchParam = Node->getCatchParamDecl())
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001660 dumpDecl(CatchParam);
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001661 else
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001662 OS << " catch all";
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001663}
1664
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001665void ASTDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001666 VisitExpr(Node);
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001667 dumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001668}
1669
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001670void ASTDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001671 VisitExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Daniel Dunbar806c12e2009-12-03 09:13:13 +00001673 OS << " " << Node->getSelector().getAsString();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001674}
1675
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001676void ASTDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001677 VisitExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001679 OS << ' ' << *Node->getProtocol();
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001680}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001681
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001682void ASTDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001683 VisitExpr(Node);
John McCall12f78a62010-12-02 01:19:52 +00001684 if (Node->isImplicitProperty()) {
Fariborz Jahanian99130e52010-12-22 19:46:35 +00001685 OS << " Kind=MethodRef Getter=\"";
1686 if (Node->getImplicitPropertyGetter())
1687 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
1688 else
1689 OS << "(null)";
1690
1691 OS << "\" Setter=\"";
John McCall12f78a62010-12-02 01:19:52 +00001692 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
1693 OS << Setter->getSelector().getAsString();
1694 else
1695 OS << "(null)";
1696 OS << "\"";
1697 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001698 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
John McCall12f78a62010-12-02 01:19:52 +00001699 }
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001700
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00001701 if (Node->isSuperReceiver())
1702 OS << " super";
Argyrios Kyrtzidisb085d892012-03-30 00:19:18 +00001703
1704 OS << " Messaging=";
1705 if (Node->isMessagingGetter() && Node->isMessagingSetter())
1706 OS << "Getter&Setter";
1707 else if (Node->isMessagingGetter())
1708 OS << "Getter";
1709 else if (Node->isMessagingSetter())
1710 OS << "Setter";
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001711}
1712
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001713void ASTDumper::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001714 VisitExpr(Node);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001715 if (Node->isArraySubscriptRefExpr())
1716 OS << " Kind=ArraySubscript GetterForArray=\"";
1717 else
1718 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
1719 if (Node->getAtIndexMethodDecl())
1720 OS << Node->getAtIndexMethodDecl()->getSelector().getAsString();
1721 else
1722 OS << "(null)";
Alexander Kornienko21c8b192012-12-11 15:28:09 +00001723
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001724 if (Node->isArraySubscriptRefExpr())
1725 OS << "\" SetterForArray=\"";
1726 else
1727 OS << "\" SetterForDictionary=\"";
1728 if (Node->setAtIndexMethodDecl())
1729 OS << Node->setAtIndexMethodDecl()->getSelector().getAsString();
1730 else
1731 OS << "(null)";
1732}
1733
Alexander Kornienko40b66a002012-12-13 13:59:55 +00001734void ASTDumper::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001735 VisitExpr(Node);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001736 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
1737}
1738
Chris Lattner6000dac2007-08-08 22:51:59 +00001739//===----------------------------------------------------------------------===//
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001740// Comments
1741//===----------------------------------------------------------------------===//
1742
1743const char *ASTDumper::getCommandName(unsigned CommandID) {
1744 if (Traits)
1745 return Traits->getCommandInfo(CommandID)->Name;
1746 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
1747 if (Info)
1748 return Info->Name;
1749 return "<not a builtin command>";
1750}
1751
1752void ASTDumper::dumpFullComment(const FullComment *C) {
1753 if (!C)
1754 return;
1755
1756 FC = C;
1757 dumpComment(C);
1758 FC = 0;
1759}
1760
1761void ASTDumper::dumpComment(const Comment *C) {
1762 IndentScope Indent(*this);
1763
1764 if (!C) {
Richard Trieu7ba443a2013-01-26 01:31:20 +00001765 ColorScope Color(*this, NullColor);
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001766 OS << "<<<NULL>>>";
1767 return;
1768 }
1769
Richard Trieu7ba443a2013-01-26 01:31:20 +00001770 {
1771 ColorScope Color(*this, CommentColor);
1772 OS << C->getCommentKindName();
1773 }
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001774 dumpPointer(C);
1775 dumpSourceRange(C->getSourceRange());
1776 ConstCommentVisitor<ASTDumper>::visit(C);
1777 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
Richard Trieue8d41192013-01-31 01:44:26 +00001778 I != E; ++I) {
1779 if (I + 1 == E)
1780 lastChild();
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001781 dumpComment(*I);
Richard Trieue8d41192013-01-31 01:44:26 +00001782 }
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001783}
1784
1785void ASTDumper::visitTextComment(const TextComment *C) {
1786 OS << " Text=\"" << C->getText() << "\"";
1787}
1788
1789void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
1790 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
1791 switch (C->getRenderKind()) {
1792 case InlineCommandComment::RenderNormal:
1793 OS << " RenderNormal";
1794 break;
1795 case InlineCommandComment::RenderBold:
1796 OS << " RenderBold";
1797 break;
1798 case InlineCommandComment::RenderMonospaced:
1799 OS << " RenderMonospaced";
1800 break;
1801 case InlineCommandComment::RenderEmphasized:
1802 OS << " RenderEmphasized";
1803 break;
1804 }
1805
1806 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
1807 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
1808}
1809
1810void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
1811 OS << " Name=\"" << C->getTagName() << "\"";
1812 if (C->getNumAttrs() != 0) {
1813 OS << " Attrs: ";
1814 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
1815 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
1816 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
1817 }
1818 }
1819 if (C->isSelfClosing())
1820 OS << " SelfClosing";
1821}
1822
1823void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
1824 OS << " Name=\"" << C->getTagName() << "\"";
1825}
1826
1827void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
1828 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
1829 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
1830 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
1831}
1832
1833void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
1834 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
1835
1836 if (C->isDirectionExplicit())
1837 OS << " explicitly";
1838 else
1839 OS << " implicitly";
1840
1841 if (C->hasParamName()) {
1842 if (C->isParamIndexValid())
1843 OS << " Param=\"" << C->getParamName(FC) << "\"";
1844 else
1845 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
1846 }
1847
1848 if (C->isParamIndexValid())
1849 OS << " ParamIndex=" << C->getParamIndex();
1850}
1851
1852void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
1853 if (C->hasParamName()) {
1854 if (C->isPositionValid())
1855 OS << " Param=\"" << C->getParamName(FC) << "\"";
1856 else
1857 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
1858 }
1859
1860 if (C->isPositionValid()) {
1861 OS << " Position=<";
1862 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
1863 OS << C->getIndex(i);
1864 if (i != e - 1)
1865 OS << ", ";
1866 }
1867 OS << ">";
1868 }
1869}
1870
1871void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
1872 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
1873 " CloseName=\"" << C->getCloseName() << "\"";
1874}
1875
1876void ASTDumper::visitVerbatimBlockLineComment(
1877 const VerbatimBlockLineComment *C) {
1878 OS << " Text=\"" << C->getText() << "\"";
1879}
1880
1881void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
1882 OS << " Text=\"" << C->getText() << "\"";
1883}
1884
1885//===----------------------------------------------------------------------===//
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001886// Decl method implementations
1887//===----------------------------------------------------------------------===//
1888
1889void Decl::dump() const {
1890 dump(llvm::errs());
1891}
1892
1893void Decl::dump(raw_ostream &OS) const {
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001894 ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
1895 &getASTContext().getSourceManager());
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001896 P.dumpDecl(const_cast<Decl*>(this));
1897}
1898
Richard Trieu7ba443a2013-01-26 01:31:20 +00001899void Decl::dumpColor() const {
1900 ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
1901 &getASTContext().getSourceManager(), /*ShowColors*/true);
1902 P.dumpDecl(const_cast<Decl*>(this));
1903}
Alexander Kornienkod538ed92012-12-20 02:09:13 +00001904//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +00001905// Stmt method implementations
1906//===----------------------------------------------------------------------===//
1907
Chris Lattnere300c872007-08-30 06:17:34 +00001908void Stmt::dump(SourceManager &SM) const {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001909 dump(llvm::errs(), SM);
1910}
1911
Chris Lattner5f9e2722011-07-23 10:55:15 +00001912void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001913 ASTDumper P(OS, 0, &SM);
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001914 P.dumpStmt(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +00001915}
1916
Chris Lattner6000dac2007-08-08 22:51:59 +00001917void Stmt::dump() const {
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001918 ASTDumper P(llvm::errs(), 0, 0);
Alexander Kornienkod5bc3592012-12-11 15:20:44 +00001919 P.dumpStmt(const_cast<Stmt*>(this));
Chris Lattner6000dac2007-08-08 22:51:59 +00001920}
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001921
Richard Trieu7ba443a2013-01-26 01:31:20 +00001922void Stmt::dumpColor() const {
1923 ASTDumper P(llvm::errs(), 0, 0, /*ShowColors*/true);
1924 P.dumpStmt(const_cast<Stmt*>(this));
1925}
1926
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001927//===----------------------------------------------------------------------===//
1928// Comment method implementations
1929//===----------------------------------------------------------------------===//
1930
1931void Comment::dump() const {
1932 dump(llvm::errs(), 0, 0);
1933}
1934
1935void Comment::dump(const ASTContext &Context) const {
1936 dump(llvm::errs(), &Context.getCommentCommandTraits(),
1937 &Context.getSourceManager());
1938}
1939
Alexander Kornienko51ccafd2013-01-15 12:20:21 +00001940void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
Alexander Kornienkoacd356e2013-01-14 14:07:11 +00001941 const SourceManager *SM) const {
1942 const FullComment *FC = dyn_cast<FullComment>(this);
1943 ASTDumper D(OS, Traits, SM);
1944 D.dumpFullComment(FC);
1945}
Richard Trieu7ba443a2013-01-26 01:31:20 +00001946
1947void Comment::dumpColor() const {
1948 const FullComment *FC = dyn_cast<FullComment>(this);
1949 ASTDumper D(llvm::errs(), 0, 0, /*ShowColors*/true);
1950 D.dumpFullComment(FC);
1951}