blob: d134e646eb91a154d944a6104b9233a0f0b0d3ad [file] [log] [blame]
Alexander Kornienko18ec81b2012-12-13 13:59:55 +00001//===--- ASTDumper.cpp - Dumping implementation for ASTs ------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnercbe4f772007-08-08 22:51:59 +00007//
8//===----------------------------------------------------------------------===//
9//
Alexander Kornienko18ec81b2012-12-13 13:59:55 +000010// This file implements the AST dump methods, which dump out the
Chris Lattnercbe4f772007-08-08 22:51:59 +000011// AST in a form that exposes type details and other fields.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/AST/ASTContext.h"
Alexander Kornienko5bc364e2013-01-07 17:53:08 +000016#include "clang/AST/Attr.h"
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000017#include "clang/AST/CommentVisitor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/DeclCXX.h"
Richard Smith33937e72013-06-22 21:49:40 +000019#include "clang/AST/DeclLookups.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclObjC.h"
Alexander Kornienko90ff6072012-12-20 02:09:13 +000021#include "clang/AST/DeclVisitor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/AST/StmtVisitor.h"
Alexander Kornienko90ff6072012-12-20 02:09:13 +000023#include "clang/Basic/Module.h"
Chris Lattner11e30d32007-08-30 06:17:34 +000024#include "clang/Basic/SourceManager.h"
Daniel Dunbar34a96c82009-12-03 09:13:13 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattnercbe4f772007-08-08 22:51:59 +000026using namespace clang;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000027using namespace clang::comments;
Chris Lattnercbe4f772007-08-08 22:51:59 +000028
29//===----------------------------------------------------------------------===//
Alexander Kornienko18ec81b2012-12-13 13:59:55 +000030// ASTDumper Visitor
Chris Lattnercbe4f772007-08-08 22:51:59 +000031//===----------------------------------------------------------------------===//
32
33namespace {
Richard Trieud215b8d2013-01-26 01:31:20 +000034 // Colors used for various parts of the AST dump
Richard Trieu532018f2014-03-06 01:09:03 +000035 // Do not use bold yellow for any text. It is hard to read on white screens.
Richard Trieud215b8d2013-01-26 01:31:20 +000036
37 struct TerminalColor {
38 raw_ostream::Colors Color;
39 bool Bold;
40 };
41
Richard Trieu532018f2014-03-06 01:09:03 +000042 // Red - CastColor
43 // Green - TypeColor
44 // Bold Green - DeclKindNameColor, UndeserializedColor
45 // Yellow - AddressColor, LocationColor
46 // Blue - CommentColor, NullColor, IndentColor
47 // Bold Blue - AttrColor
48 // Bold Magenta - StmtColor
49 // Cyan - ValueKindColor, ObjectKindColor
50 // Bold Cyan - ValueColor, DeclNameColor
51
Richard Trieud215b8d2013-01-26 01:31:20 +000052 // Decl kind names (VarDecl, FunctionDecl, etc)
53 static const TerminalColor DeclKindNameColor = { raw_ostream::GREEN, true };
54 // Attr names (CleanupAttr, GuardedByAttr, etc)
55 static const TerminalColor AttrColor = { raw_ostream::BLUE, true };
56 // Statement names (DeclStmt, ImplicitCastExpr, etc)
57 static const TerminalColor StmtColor = { raw_ostream::MAGENTA, true };
58 // Comment names (FullComment, ParagraphComment, TextComment, etc)
Richard Trieu532018f2014-03-06 01:09:03 +000059 static const TerminalColor CommentColor = { raw_ostream::BLUE, false };
Richard Trieud215b8d2013-01-26 01:31:20 +000060
61 // Type names (int, float, etc, plus user defined types)
62 static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
63
64 // Pointer address
65 static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
66 // Source locations
67 static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
68
69 // lvalue/xvalue
70 static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
71 // bitfield/objcproperty/objcsubscript/vectorcomponent
72 static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
73
74 // Null statements
75 static const TerminalColor NullColor = { raw_ostream::BLUE, false };
76
Richard Smith1d209d02013-05-23 01:49:11 +000077 // Undeserialized entities
78 static const TerminalColor UndeserializedColor = { raw_ostream::GREEN, true };
79
Richard Trieud215b8d2013-01-26 01:31:20 +000080 // CastKind from CastExpr's
81 static const TerminalColor CastColor = { raw_ostream::RED, false };
82
83 // Value of the statement
84 static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
85 // Decl names
86 static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
87
Richard Trieude5cc7d2013-01-31 01:44:26 +000088 // Indents ( `, -. | )
89 static const TerminalColor IndentColor = { raw_ostream::BLUE, false };
90
Alexander Kornienko90ff6072012-12-20 02:09:13 +000091 class ASTDumper
Alexander Kornienko540bacb2013-02-01 12:35:51 +000092 : public ConstDeclVisitor<ASTDumper>, public ConstStmtVisitor<ASTDumper>,
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000093 public ConstCommentVisitor<ASTDumper> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000094 raw_ostream &OS;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000095 const CommandTraits *Traits;
96 const SourceManager *SM;
Manuel Klimek874030e2012-11-07 00:33:12 +000097 bool IsFirstLine;
Mike Stump11289f42009-09-09 15:08:12 +000098
Richard Trieude5cc7d2013-01-31 01:44:26 +000099 // Indicates whether more child are expected at the current tree depth
100 enum IndentType { IT_Child, IT_LastChild };
101
102 /// Indents[i] indicates if another child exists at level i.
103 /// Used by Indent() to print the tree structure.
104 llvm::SmallVector<IndentType, 32> Indents;
105
106 /// Indicates that more children will be needed at this indent level.
107 /// If true, prevents lastChild() from marking the node as the last child.
108 /// This is used when there are multiple collections of children to be
109 /// dumped as well as during conditional node dumping.
110 bool MoreChildren;
111
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000112 /// Keep track of the last location we print out so that we can
113 /// print out deltas from then on out.
Chris Lattner11e30d32007-08-30 06:17:34 +0000114 const char *LastLocFilename;
115 unsigned LastLocLine;
Douglas Gregor7de59662009-05-29 20:38:28 +0000116
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000117 /// The \c FullComment parent of the comment being dumped.
118 const FullComment *FC;
119
Richard Trieud215b8d2013-01-26 01:31:20 +0000120 bool ShowColors;
121
Manuel Klimek874030e2012-11-07 00:33:12 +0000122 class IndentScope {
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000123 ASTDumper &Dumper;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000124 // Preserve the Dumper's MoreChildren value from the previous IndentScope
125 bool MoreChildren;
Manuel Klimek874030e2012-11-07 00:33:12 +0000126 public:
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000127 IndentScope(ASTDumper &Dumper) : Dumper(Dumper) {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000128 MoreChildren = Dumper.hasMoreChildren();
129 Dumper.setMoreChildren(false);
Manuel Klimek874030e2012-11-07 00:33:12 +0000130 Dumper.indent();
131 }
132 ~IndentScope() {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000133 Dumper.setMoreChildren(MoreChildren);
Manuel Klimek874030e2012-11-07 00:33:12 +0000134 Dumper.unindent();
135 }
136 };
137
Richard Trieud215b8d2013-01-26 01:31:20 +0000138 class ColorScope {
139 ASTDumper &Dumper;
140 public:
141 ColorScope(ASTDumper &Dumper, TerminalColor Color)
142 : Dumper(Dumper) {
143 if (Dumper.ShowColors)
144 Dumper.OS.changeColor(Color.Color, Color.Bold);
145 }
146 ~ColorScope() {
147 if (Dumper.ShowColors)
148 Dumper.OS.resetColor();
149 }
150 };
151
Richard Smithdcc2c452014-03-17 23:00:06 +0000152 class ChildDumper {
153 ASTDumper &Dumper;
154
155 const Decl *Prev;
156 bool PrevRef;
157 public:
Craig Topper36250ad2014-05-12 05:36:57 +0000158 ChildDumper(ASTDumper &Dumper) : Dumper(Dumper), Prev(nullptr) {}
Richard Smithdcc2c452014-03-17 23:00:06 +0000159 ~ChildDumper() {
160 if (Prev) {
161 Dumper.lastChild();
Craig Topper36250ad2014-05-12 05:36:57 +0000162 dump(nullptr);
Richard Smithdcc2c452014-03-17 23:00:06 +0000163 }
164 }
165
166 // FIXME: This should take an arbitrary callable as the dumping action.
167 void dump(const Decl *D, bool Ref = false) {
168 if (Prev) {
169 if (PrevRef)
170 Dumper.dumpDeclRef(Prev);
171 else
172 Dumper.dumpDecl(Prev);
173 }
174 Prev = D;
175 PrevRef = Ref;
176 }
177 void dumpRef(const Decl *D) { dump(D, true); }
178
179 // Give up ownership of the children of the node. By calling this,
180 // the caller takes back responsibility for calling lastChild().
Craig Topper36250ad2014-05-12 05:36:57 +0000181 void release() { dump(nullptr); }
Richard Smithdcc2c452014-03-17 23:00:06 +0000182 };
183
Chris Lattnercbe4f772007-08-08 22:51:59 +0000184 public:
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000185 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
186 const SourceManager *SM)
Richard Smith56d12152013-01-31 02:04:38 +0000187 : OS(OS), Traits(Traits), SM(SM), IsFirstLine(true), MoreChildren(false),
Craig Topper36250ad2014-05-12 05:36:57 +0000188 LastLocFilename(""), LastLocLine(~0U), FC(nullptr),
Richard Trieud215b8d2013-01-26 01:31:20 +0000189 ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
190
191 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
192 const SourceManager *SM, bool ShowColors)
Richard Smith56d12152013-01-31 02:04:38 +0000193 : OS(OS), Traits(Traits), SM(SM), IsFirstLine(true), MoreChildren(false),
194 LastLocFilename(""), LastLocLine(~0U),
Richard Trieude5cc7d2013-01-31 01:44:26 +0000195 ShowColors(ShowColors) { }
Mike Stump11289f42009-09-09 15:08:12 +0000196
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000197 ~ASTDumper() {
Manuel Klimek874030e2012-11-07 00:33:12 +0000198 OS << "\n";
199 }
200
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000201 void dumpDecl(const Decl *D);
202 void dumpStmt(const Stmt *S);
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000203 void dumpFullComment(const FullComment *C);
Mike Stump11289f42009-09-09 15:08:12 +0000204
Richard Trieude5cc7d2013-01-31 01:44:26 +0000205 // Formatting
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000206 void indent();
207 void unindent();
Richard Trieude5cc7d2013-01-31 01:44:26 +0000208 void lastChild();
209 bool hasMoreChildren();
210 void setMoreChildren(bool Value);
211
212 // Utilities
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000213 void dumpPointer(const void *Ptr);
214 void dumpSourceRange(SourceRange R);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000215 void dumpLocation(SourceLocation Loc);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000216 void dumpBareType(QualType T);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000217 void dumpType(QualType T);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000218 void dumpBareDeclRef(const Decl *Node);
Craig Topper36250ad2014-05-12 05:36:57 +0000219 void dumpDeclRef(const Decl *Node, const char *Label = nullptr);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000220 void dumpName(const NamedDecl *D);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000221 bool hasNodes(const DeclContext *DC);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000222 void dumpDeclContext(const DeclContext *DC);
Richard Smith35f986d2014-08-11 22:11:07 +0000223 void dumpLookups(const DeclContext *DC, bool DumpDecls);
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000224 void dumpAttr(const Attr *A);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000225
226 // C++ Utilities
227 void dumpAccessSpecifier(AccessSpecifier AS);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000228 void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
229 void dumpTemplateParameters(const TemplateParameterList *TPL);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000230 void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
231 void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
232 void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
233 void dumpTemplateArgument(const TemplateArgument &A,
234 SourceRange R = SourceRange());
235
236 // Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000237 void VisitLabelDecl(const LabelDecl *D);
238 void VisitTypedefDecl(const TypedefDecl *D);
239 void VisitEnumDecl(const EnumDecl *D);
240 void VisitRecordDecl(const RecordDecl *D);
241 void VisitEnumConstantDecl(const EnumConstantDecl *D);
242 void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
243 void VisitFunctionDecl(const FunctionDecl *D);
244 void VisitFieldDecl(const FieldDecl *D);
245 void VisitVarDecl(const VarDecl *D);
246 void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D);
247 void VisitImportDecl(const ImportDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000248
249 // C++ Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000250 void VisitNamespaceDecl(const NamespaceDecl *D);
251 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
252 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
253 void VisitTypeAliasDecl(const TypeAliasDecl *D);
254 void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
255 void VisitCXXRecordDecl(const CXXRecordDecl *D);
256 void VisitStaticAssertDecl(const StaticAssertDecl *D);
Richard Smithcbdf7332014-03-18 02:07:28 +0000257 template<typename SpecializationDecl>
258 void VisitTemplateDeclSpecialization(ChildDumper &Children,
259 const SpecializationDecl *D,
260 bool DumpExplicitInst,
261 bool DumpRefOnly);
Richard Smith20ade552014-03-17 23:34:53 +0000262 template<typename TemplateDecl>
263 void VisitTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000264 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
265 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000266 void VisitClassTemplateSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000267 const ClassTemplateSpecializationDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000268 void VisitClassTemplatePartialSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000269 const ClassTemplatePartialSpecializationDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000270 void VisitClassScopeFunctionSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000271 const ClassScopeFunctionSpecializationDecl *D);
Richard Smithd25789a2013-09-18 01:36:02 +0000272 void VisitVarTemplateDecl(const VarTemplateDecl *D);
273 void VisitVarTemplateSpecializationDecl(
274 const VarTemplateSpecializationDecl *D);
275 void VisitVarTemplatePartialSpecializationDecl(
276 const VarTemplatePartialSpecializationDecl *D);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000277 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
278 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
279 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
280 void VisitUsingDecl(const UsingDecl *D);
281 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
282 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
283 void VisitUsingShadowDecl(const UsingShadowDecl *D);
284 void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
285 void VisitAccessSpecDecl(const AccessSpecDecl *D);
286 void VisitFriendDecl(const FriendDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000287
288 // ObjC Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000289 void VisitObjCIvarDecl(const ObjCIvarDecl *D);
290 void VisitObjCMethodDecl(const ObjCMethodDecl *D);
291 void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
292 void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
293 void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
294 void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
295 void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
296 void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
297 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
298 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
299 void VisitBlockDecl(const BlockDecl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000300
Chris Lattner84ca3762007-08-30 01:00:35 +0000301 // Stmts.
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000302 void VisitStmt(const Stmt *Node);
303 void VisitDeclStmt(const DeclStmt *Node);
304 void VisitAttributedStmt(const AttributedStmt *Node);
305 void VisitLabelStmt(const LabelStmt *Node);
306 void VisitGotoStmt(const GotoStmt *Node);
Pavel Labath1ef83422013-09-04 14:35:00 +0000307 void VisitCXXCatchStmt(const CXXCatchStmt *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000308
Chris Lattner84ca3762007-08-30 01:00:35 +0000309 // Exprs
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000310 void VisitExpr(const Expr *Node);
311 void VisitCastExpr(const CastExpr *Node);
312 void VisitDeclRefExpr(const DeclRefExpr *Node);
313 void VisitPredefinedExpr(const PredefinedExpr *Node);
314 void VisitCharacterLiteral(const CharacterLiteral *Node);
315 void VisitIntegerLiteral(const IntegerLiteral *Node);
316 void VisitFloatingLiteral(const FloatingLiteral *Node);
317 void VisitStringLiteral(const StringLiteral *Str);
Richard Smithf0514962014-06-03 08:24:28 +0000318 void VisitInitListExpr(const InitListExpr *ILE);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000319 void VisitUnaryOperator(const UnaryOperator *Node);
320 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
321 void VisitMemberExpr(const MemberExpr *Node);
322 void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
323 void VisitBinaryOperator(const BinaryOperator *Node);
324 void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
325 void VisitAddrLabelExpr(const AddrLabelExpr *Node);
326 void VisitBlockExpr(const BlockExpr *Node);
327 void VisitOpaqueValueExpr(const OpaqueValueExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000328
329 // C++
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000330 void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
331 void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
332 void VisitCXXThisExpr(const CXXThisExpr *Node);
333 void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
334 void VisitCXXConstructExpr(const CXXConstructExpr *Node);
335 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
Richard Smithe6c01442013-06-05 00:46:14 +0000336 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000337 void VisitExprWithCleanups(const ExprWithCleanups *Node);
338 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
339 void dumpCXXTemporary(const CXXTemporary *Temporary);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000340 void VisitLambdaExpr(const LambdaExpr *Node) {
341 VisitExpr(Node);
342 dumpDecl(Node->getLambdaClass());
343 }
Mike Stump11289f42009-09-09 15:08:12 +0000344
Chris Lattner84ca3762007-08-30 01:00:35 +0000345 // ObjC
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000346 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
347 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
348 void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
349 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
350 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
351 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
352 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
353 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
354 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
355 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000356
357 // Comments.
358 const char *getCommandName(unsigned CommandID);
359 void dumpComment(const Comment *C);
360
361 // Inline comments.
362 void visitTextComment(const TextComment *C);
363 void visitInlineCommandComment(const InlineCommandComment *C);
364 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
365 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
366
367 // Block comments.
368 void visitBlockCommandComment(const BlockCommandComment *C);
369 void visitParamCommandComment(const ParamCommandComment *C);
370 void visitTParamCommandComment(const TParamCommandComment *C);
371 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
372 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
373 void visitVerbatimLineComment(const VerbatimLineComment *C);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000374 };
375}
376
377//===----------------------------------------------------------------------===//
Chris Lattner11e30d32007-08-30 06:17:34 +0000378// Utilities
379//===----------------------------------------------------------------------===//
380
Richard Trieude5cc7d2013-01-31 01:44:26 +0000381// Print out the appropriate tree structure using the Indents vector.
382// Example of tree and the Indents vector at each level.
383// A { }
384// |-B { IT_Child }
385// | `-C { IT_Child, IT_LastChild }
386// `-D { IT_LastChild }
387// |-E { IT_LastChild, IT_Child }
388// `-F { IT_LastChild, IT_LastChild }
389// Type non-last element, last element
390// IT_Child "| " "|-"
391// IT_LastChild " " "`-"
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000392void ASTDumper::indent() {
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000393 if (IsFirstLine)
394 IsFirstLine = false;
395 else
396 OS << "\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000397
398 ColorScope Color(*this, IndentColor);
Craig Topper2341c0d2013-07-04 03:08:24 +0000399 for (SmallVectorImpl<IndentType>::const_iterator I = Indents.begin(),
400 E = Indents.end();
Richard Trieude5cc7d2013-01-31 01:44:26 +0000401 I != E; ++I) {
402 switch (*I) {
Richard Smith56d12152013-01-31 02:04:38 +0000403 case IT_Child:
404 if (I == E - 1)
405 OS << "|-";
406 else
407 OS << "| ";
408 continue;
409 case IT_LastChild:
410 if (I == E - 1)
411 OS << "`-";
412 else
413 OS << " ";
414 continue;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000415 }
Richard Smith56d12152013-01-31 02:04:38 +0000416 llvm_unreachable("Invalid IndentType");
Richard Trieude5cc7d2013-01-31 01:44:26 +0000417 }
418 Indents.push_back(IT_Child);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000419}
420
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000421void ASTDumper::unindent() {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000422 Indents.pop_back();
423}
424
425// Call before each potential last child node is to be dumped. If MoreChildren
426// is false, then this is the last child, otherwise treat as a regular node.
427void ASTDumper::lastChild() {
428 if (!hasMoreChildren())
429 Indents.back() = IT_LastChild;
430}
431
432// MoreChildren should be set before calling another function that may print
433// additional nodes to prevent conflicting final child nodes.
434bool ASTDumper::hasMoreChildren() {
435 return MoreChildren;
436}
437
438void ASTDumper::setMoreChildren(bool Value) {
439 MoreChildren = Value;
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000440}
441
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000442void ASTDumper::dumpPointer(const void *Ptr) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000443 ColorScope Color(*this, AddressColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000444 OS << ' ' << Ptr;
445}
446
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000447void ASTDumper::dumpLocation(SourceLocation Loc) {
Alex McCarthye14ddef2014-05-02 20:24:11 +0000448 if (!SM)
449 return;
450
Richard Trieud215b8d2013-01-26 01:31:20 +0000451 ColorScope Color(*this, LocationColor);
Chris Lattner53e384f2009-01-16 07:00:02 +0000452 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000453
Chris Lattner11e30d32007-08-30 06:17:34 +0000454 // The general format we print out is filename:line:col, but we drop pieces
455 // that haven't changed since the last loc printed.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000456 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
457
Douglas Gregor453b0122010-11-12 07:15:47 +0000458 if (PLoc.isInvalid()) {
459 OS << "<invalid sloc>";
460 return;
461 }
462
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000463 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000464 OS << PLoc.getFilename() << ':' << PLoc.getLine()
465 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000466 LastLocFilename = PLoc.getFilename();
467 LastLocLine = PLoc.getLine();
468 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000469 OS << "line" << ':' << PLoc.getLine()
470 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000471 LastLocLine = PLoc.getLine();
Chris Lattner11e30d32007-08-30 06:17:34 +0000472 } else {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000473 OS << "col" << ':' << PLoc.getColumn();
Chris Lattner11e30d32007-08-30 06:17:34 +0000474 }
475}
476
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000477void ASTDumper::dumpSourceRange(SourceRange R) {
Chris Lattner11e30d32007-08-30 06:17:34 +0000478 // Can't translate locations if a SourceManager isn't available.
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000479 if (!SM)
480 return;
Mike Stump11289f42009-09-09 15:08:12 +0000481
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000482 OS << " <";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000483 dumpLocation(R.getBegin());
Chris Lattnera7c19fe2007-10-16 22:36:42 +0000484 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000485 OS << ", ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000486 dumpLocation(R.getEnd());
Chris Lattner11e30d32007-08-30 06:17:34 +0000487 }
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000488 OS << ">";
Mike Stump11289f42009-09-09 15:08:12 +0000489
Chris Lattner11e30d32007-08-30 06:17:34 +0000490 // <t2.c:123:421[blah], t2.c:412:321>
491
492}
493
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000494void ASTDumper::dumpBareType(QualType T) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000495 ColorScope Color(*this, TypeColor);
496
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000497 SplitQualType T_split = T.split();
498 OS << "'" << QualType::getAsString(T_split) << "'";
499
500 if (!T.isNull()) {
501 // If the type is sugared, also dump a (shallow) desugared type.
502 SplitQualType D_split = T.getSplitDesugaredType();
503 if (T_split != D_split)
504 OS << ":'" << QualType::getAsString(D_split) << "'";
505 }
506}
507
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000508void ASTDumper::dumpType(QualType T) {
509 OS << ' ';
510 dumpBareType(T);
511}
512
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000513void ASTDumper::dumpBareDeclRef(const Decl *D) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000514 {
515 ColorScope Color(*this, DeclKindNameColor);
516 OS << D->getDeclKindName();
517 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000518 dumpPointer(D);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000519
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000520 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000521 ColorScope Color(*this, DeclNameColor);
David Blaikied4da8722013-05-14 21:04:00 +0000522 OS << " '" << ND->getDeclName() << '\'';
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000523 }
524
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000525 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000526 dumpType(VD->getType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000527}
528
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000529void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000530 if (!D)
531 return;
532
533 IndentScope Indent(*this);
534 if (Label)
535 OS << Label << ' ';
536 dumpBareDeclRef(D);
537}
538
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000539void ASTDumper::dumpName(const NamedDecl *ND) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000540 if (ND->getDeclName()) {
541 ColorScope Color(*this, DeclNameColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000542 OS << ' ' << ND->getNameAsString();
Richard Trieud215b8d2013-01-26 01:31:20 +0000543 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000544}
545
Richard Trieude5cc7d2013-01-31 01:44:26 +0000546bool ASTDumper::hasNodes(const DeclContext *DC) {
547 if (!DC)
548 return false;
549
Richard Smith1d209d02013-05-23 01:49:11 +0000550 return DC->hasExternalLexicalStorage() ||
551 DC->noload_decls_begin() != DC->noload_decls_end();
Richard Trieude5cc7d2013-01-31 01:44:26 +0000552}
553
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000554void ASTDumper::dumpDeclContext(const DeclContext *DC) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000555 if (!DC)
556 return;
Richard Smithdcc2c452014-03-17 23:00:06 +0000557
558 ChildDumper Children(*this);
559 for (auto *D : DC->noload_decls())
560 Children.dump(D);
561
562 if (DC->hasExternalLexicalStorage()) {
563 Children.release();
564
Richard Smith1d209d02013-05-23 01:49:11 +0000565 lastChild();
566 IndentScope Indent(*this);
567 ColorScope Color(*this, UndeserializedColor);
568 OS << "<undeserialized declarations>";
569 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000570}
571
Richard Smith35f986d2014-08-11 22:11:07 +0000572void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
Richard Smith33937e72013-06-22 21:49:40 +0000573 IndentScope Indent(*this);
574
575 OS << "StoredDeclsMap ";
576 dumpBareDeclRef(cast<Decl>(DC));
577
578 const DeclContext *Primary = DC->getPrimaryContext();
579 if (Primary != DC) {
580 OS << " primary";
581 dumpPointer(cast<Decl>(Primary));
582 }
583
584 bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
585
586 DeclContext::all_lookups_iterator I = Primary->noload_lookups_begin(),
587 E = Primary->noload_lookups_end();
588 while (I != E) {
589 DeclarationName Name = I.getLookupName();
590 DeclContextLookupResult R = *I++;
591 if (I == E && !HasUndeserializedLookups)
592 lastChild();
593
594 IndentScope Indent(*this);
595 OS << "DeclarationName ";
596 {
597 ColorScope Color(*this, DeclNameColor);
598 OS << '\'' << Name << '\'';
599 }
600
601 for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
602 RI != RE; ++RI) {
603 if (RI + 1 == RE)
604 lastChild();
Richard Smith35f986d2014-08-11 22:11:07 +0000605
606 IndentScope LookupIndent(*this);
607 dumpBareDeclRef(*RI);
608
Richard Smith15fc7df2013-10-22 23:50:38 +0000609 if ((*RI)->isHidden())
610 OS << " hidden";
Richard Smith35f986d2014-08-11 22:11:07 +0000611
612 // If requested, dump the redecl chain for this lookup.
613 if (DumpDecls) {
614 // Dump earliest decl first.
615 std::function<void(Decl*)> DumpPrev = [&](Decl *D) {
616 if (Decl *Prev = D->getPreviousDecl()) {
617 DumpPrev(Prev);
618 dumpDecl(Prev);
619 }
620 };
621 DumpPrev(*RI);
622 lastChild();
623 dumpDecl(*RI);
624 }
Richard Smith33937e72013-06-22 21:49:40 +0000625 }
626 }
627
628 if (HasUndeserializedLookups) {
629 lastChild();
630 IndentScope Indent(*this);
631 ColorScope Color(*this, UndeserializedColor);
632 OS << "<undeserialized lookups>";
633 }
634}
635
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000636void ASTDumper::dumpAttr(const Attr *A) {
637 IndentScope Indent(*this);
Richard Trieud215b8d2013-01-26 01:31:20 +0000638 {
639 ColorScope Color(*this, AttrColor);
Aaron Ballman36a53502014-01-16 13:03:14 +0000640
Richard Trieud215b8d2013-01-26 01:31:20 +0000641 switch (A->getKind()) {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000642#define ATTR(X) case attr::X: OS << #X; break;
643#include "clang/Basic/AttrList.inc"
Richard Trieud215b8d2013-01-26 01:31:20 +0000644 default: llvm_unreachable("unexpected attribute kind");
645 }
646 OS << "Attr";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000647 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000648 dumpPointer(A);
649 dumpSourceRange(A->getRange());
Hans Wennborgb0a8b4a2014-05-31 04:05:57 +0000650 if (A->isInherited())
651 OS << " Inherited";
Aaron Ballman36a53502014-01-16 13:03:14 +0000652 if (A->isImplicit())
653 OS << " Implicit";
Hans Wennborgb0a8b4a2014-05-31 04:05:57 +0000654#include "clang/AST/AttrDump.inc"
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000655}
656
Richard Smith71bec062013-10-15 21:58:30 +0000657static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
658
659template<typename T>
660static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
Rafael Espindola8db352d2013-10-17 15:37:26 +0000661 const T *First = D->getFirstDecl();
Richard Smith71bec062013-10-15 21:58:30 +0000662 if (First != D)
663 OS << " first " << First;
Richard Smithf5f43542013-02-07 01:35:44 +0000664}
665
666template<typename T>
Richard Smith71bec062013-10-15 21:58:30 +0000667static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
668 const T *Prev = D->getPreviousDecl();
669 if (Prev)
670 OS << " prev " << Prev;
Richard Smithf5f43542013-02-07 01:35:44 +0000671}
672
Richard Smith71bec062013-10-15 21:58:30 +0000673/// Dump the previous declaration in the redeclaration chain for a declaration,
674/// if any.
675static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
Richard Smithf5f43542013-02-07 01:35:44 +0000676 switch (D->getKind()) {
677#define DECL(DERIVED, BASE) \
678 case Decl::DERIVED: \
Richard Smith71bec062013-10-15 21:58:30 +0000679 return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
Richard Smithf5f43542013-02-07 01:35:44 +0000680#define ABSTRACT_DECL(DECL)
681#include "clang/AST/DeclNodes.inc"
682 }
683 llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
684}
685
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000686//===----------------------------------------------------------------------===//
687// C++ Utilities
688//===----------------------------------------------------------------------===//
689
690void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
691 switch (AS) {
692 case AS_none:
693 break;
694 case AS_public:
695 OS << "public";
696 break;
697 case AS_protected:
698 OS << "protected";
699 break;
700 case AS_private:
701 OS << "private";
702 break;
703 }
704}
705
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000706void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000707 IndentScope Indent(*this);
708 OS << "CXXCtorInitializer";
709 if (Init->isAnyMemberInitializer()) {
710 OS << ' ';
711 dumpBareDeclRef(Init->getAnyMember());
Richard Trieu40bcd9f2014-09-12 21:20:53 +0000712 } else if (Init->isBaseInitializer()) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000713 dumpType(QualType(Init->getBaseClass(), 0));
Richard Trieu40bcd9f2014-09-12 21:20:53 +0000714 } else if (Init->isDelegatingInitializer()) {
715 dumpType(Init->getTypeSourceInfo()->getType());
716 } else {
717 llvm_unreachable("Unknown initializer type");
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000718 }
719 dumpStmt(Init->getInit());
720}
721
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000722void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000723 if (!TPL)
724 return;
725
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000726 for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000727 I != E; ++I)
728 dumpDecl(*I);
729}
730
731void ASTDumper::dumpTemplateArgumentListInfo(
732 const TemplateArgumentListInfo &TALI) {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000733 for (unsigned i = 0, e = TALI.size(); i < e; ++i) {
734 if (i + 1 == e)
735 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000736 dumpTemplateArgumentLoc(TALI[i]);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000737 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000738}
739
740void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
741 dumpTemplateArgument(A.getArgument(), A.getSourceRange());
742}
743
744void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
745 for (unsigned i = 0, e = TAL.size(); i < e; ++i)
746 dumpTemplateArgument(TAL[i]);
747}
748
749void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
750 IndentScope Indent(*this);
751 OS << "TemplateArgument";
752 if (R.isValid())
753 dumpSourceRange(R);
754
755 switch (A.getKind()) {
756 case TemplateArgument::Null:
757 OS << " null";
758 break;
759 case TemplateArgument::Type:
760 OS << " type";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000761 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000762 dumpType(A.getAsType());
763 break;
764 case TemplateArgument::Declaration:
765 OS << " decl";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000766 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000767 dumpDeclRef(A.getAsDecl());
768 break;
769 case TemplateArgument::NullPtr:
770 OS << " nullptr";
771 break;
772 case TemplateArgument::Integral:
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000773 OS << " integral " << A.getAsIntegral();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000774 break;
775 case TemplateArgument::Template:
776 OS << " template ";
777 A.getAsTemplate().dump(OS);
778 break;
779 case TemplateArgument::TemplateExpansion:
780 OS << " template expansion";
781 A.getAsTemplateOrTemplatePattern().dump(OS);
782 break;
783 case TemplateArgument::Expression:
784 OS << " expr";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000785 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000786 dumpStmt(A.getAsExpr());
787 break;
788 case TemplateArgument::Pack:
789 OS << " pack";
790 for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
Richard Trieude5cc7d2013-01-31 01:44:26 +0000791 I != E; ++I) {
792 if (I + 1 == E)
793 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000794 dumpTemplateArgument(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000795 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000796 break;
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000797 }
798}
799
Chris Lattner11e30d32007-08-30 06:17:34 +0000800//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000801// Decl dumping methods.
Chris Lattnercbe4f772007-08-08 22:51:59 +0000802//===----------------------------------------------------------------------===//
803
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000804void ASTDumper::dumpDecl(const Decl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000805 IndentScope Indent(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000806
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000807 if (!D) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000808 ColorScope Color(*this, NullColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000809 OS << "<<<NULL>>>";
810 return;
Chris Lattnercbe4f772007-08-08 22:51:59 +0000811 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000812
Richard Trieud215b8d2013-01-26 01:31:20 +0000813 {
814 ColorScope Color(*this, DeclKindNameColor);
815 OS << D->getDeclKindName() << "Decl";
816 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000817 dumpPointer(D);
Richard Smithf5f43542013-02-07 01:35:44 +0000818 if (D->getLexicalDeclContext() != D->getDeclContext())
819 OS << " parent " << cast<Decl>(D->getDeclContext());
Richard Smith71bec062013-10-15 21:58:30 +0000820 dumpPreviousDecl(OS, D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000821 dumpSourceRange(D->getSourceRange());
David Blaikie5ee3d002014-04-02 05:48:29 +0000822 OS << ' ';
823 dumpLocation(D->getLocation());
Richard Smith15fc7df2013-10-22 23:50:38 +0000824 if (Module *M = D->getOwningModule())
825 OS << " in " << M->getFullModuleName();
826 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
827 if (ND->isHidden())
828 OS << " hidden";
Alexander Kornienko83a4e182014-05-27 21:29:22 +0000829 if (D->isImplicit())
830 OS << " implicit";
Richard Smith71b09ac2014-06-13 02:24:47 +0000831 if (D->isUsed())
832 OS << " used";
Richard Smithbb853c72014-08-13 01:23:33 +0000833 else if (D->isThisDeclarationReferenced())
Richard Smith71b09ac2014-06-13 02:24:47 +0000834 OS << " referenced";
835 if (D->isInvalidDecl())
836 OS << " invalid";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000837
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000838 bool HasAttrs = D->hasAttrs();
Richard Smithb39b9d52013-05-21 05:24:00 +0000839 const FullComment *Comment =
840 D->getASTContext().getLocalCommentForDeclUncached(D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000841 // Decls within functions are visited by the body
Richard Trieude5cc7d2013-01-31 01:44:26 +0000842 bool HasDeclContext = !isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
843 hasNodes(dyn_cast<DeclContext>(D));
844
Richard Smithb39b9d52013-05-21 05:24:00 +0000845 setMoreChildren(HasAttrs || Comment || HasDeclContext);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000846 ConstDeclVisitor<ASTDumper>::Visit(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000847
Richard Smithb39b9d52013-05-21 05:24:00 +0000848 setMoreChildren(Comment || HasDeclContext);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000849 for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end();
850 I != E; ++I) {
851 if (I + 1 == E)
852 lastChild();
853 dumpAttr(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000854 }
855
856 setMoreChildren(HasDeclContext);
857 lastChild();
Richard Smithb39b9d52013-05-21 05:24:00 +0000858 dumpFullComment(Comment);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000859
860 setMoreChildren(false);
861 if (HasDeclContext)
Richard Smithf5f43542013-02-07 01:35:44 +0000862 dumpDeclContext(cast<DeclContext>(D));
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000863}
864
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000865void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000866 dumpName(D);
867}
868
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000869void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000870 dumpName(D);
871 dumpType(D->getUnderlyingType());
872 if (D->isModulePrivate())
873 OS << " __module_private__";
874}
875
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000876void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000877 if (D->isScoped()) {
878 if (D->isScopedUsingClassTag())
879 OS << " class";
880 else
881 OS << " struct";
882 }
883 dumpName(D);
884 if (D->isModulePrivate())
885 OS << " __module_private__";
886 if (D->isFixed())
887 dumpType(D->getIntegerType());
888}
889
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000890void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000891 OS << ' ' << D->getKindName();
892 dumpName(D);
893 if (D->isModulePrivate())
894 OS << " __module_private__";
Richard Smith99bc1b92013-08-30 05:32:29 +0000895 if (D->isCompleteDefinition())
896 OS << " definition";
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000897}
898
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000899void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000900 dumpName(D);
901 dumpType(D->getType());
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000902 if (const Expr *Init = D->getInitExpr()) {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000903 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000904 dumpStmt(Init);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000905 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000906}
907
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000908void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000909 dumpName(D);
910 dumpType(D->getType());
Richard Smithdcc2c452014-03-17 23:00:06 +0000911
912 ChildDumper Children(*this);
Richard Smith8aa49222014-03-18 00:35:12 +0000913 for (auto *Child : D->chain())
914 Children.dumpRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000915}
916
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000917void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000918 dumpName(D);
919 dumpType(D->getType());
920
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000921 StorageClass SC = D->getStorageClass();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000922 if (SC != SC_None)
923 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
924 if (D->isInlineSpecified())
925 OS << " inline";
926 if (D->isVirtualAsWritten())
927 OS << " virtual";
928 if (D->isModulePrivate())
929 OS << " __module_private__";
930
931 if (D->isPure())
932 OS << " pure";
933 else if (D->isDeletedAsWritten())
934 OS << " delete";
935
Richard Smithadaa0152013-05-17 02:09:46 +0000936 if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
937 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
Richard Smith8acb4282014-07-31 21:57:55 +0000938 switch (EPI.ExceptionSpec.Type) {
Richard Smithadaa0152013-05-17 02:09:46 +0000939 default: break;
940 case EST_Unevaluated:
Richard Smith8acb4282014-07-31 21:57:55 +0000941 OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl;
Richard Smithadaa0152013-05-17 02:09:46 +0000942 break;
943 case EST_Uninstantiated:
Richard Smith8acb4282014-07-31 21:57:55 +0000944 OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate;
Richard Smithadaa0152013-05-17 02:09:46 +0000945 break;
946 }
947 }
948
Richard Trieude5cc7d2013-01-31 01:44:26 +0000949 bool OldMoreChildren = hasMoreChildren();
950 const FunctionTemplateSpecializationInfo *FTSI =
951 D->getTemplateSpecializationInfo();
952 bool HasTemplateSpecialization = FTSI;
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000953
Richard Trieude5cc7d2013-01-31 01:44:26 +0000954 bool HasNamedDecls = D->getDeclsInPrototypeScope().begin() !=
955 D->getDeclsInPrototypeScope().end();
956
957 bool HasFunctionDecls = D->param_begin() != D->param_end();
958
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000959 const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000960 bool HasCtorInitializers = C && C->init_begin() != C->init_end();
961
962 bool HasDeclarationBody = D->doesThisDeclarationHaveABody();
963
964 setMoreChildren(OldMoreChildren || HasNamedDecls || HasFunctionDecls ||
965 HasCtorInitializers || HasDeclarationBody);
966 if (HasTemplateSpecialization) {
967 lastChild();
968 dumpTemplateArgumentList(*FTSI->TemplateArguments);
969 }
970
971 setMoreChildren(OldMoreChildren || HasFunctionDecls ||
972 HasCtorInitializers || HasDeclarationBody);
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000973 for (ArrayRef<NamedDecl *>::iterator
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000974 I = D->getDeclsInPrototypeScope().begin(),
Richard Trieude5cc7d2013-01-31 01:44:26 +0000975 E = D->getDeclsInPrototypeScope().end(); I != E; ++I) {
976 if (I + 1 == E)
977 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000978 dumpDecl(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000979 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000980
Richard Trieude5cc7d2013-01-31 01:44:26 +0000981 setMoreChildren(OldMoreChildren || HasCtorInitializers || HasDeclarationBody);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000982 for (FunctionDecl::param_const_iterator I = D->param_begin(),
983 E = D->param_end();
Richard Trieude5cc7d2013-01-31 01:44:26 +0000984 I != E; ++I) {
985 if (I + 1 == E)
986 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000987 dumpDecl(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000988 }
989
990 setMoreChildren(OldMoreChildren || HasDeclarationBody);
991 if (HasCtorInitializers)
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000992 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000993 E = C->init_end();
994 I != E; ++I) {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000995 if (I + 1 == E)
996 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000997 dumpCXXCtorInitializer(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000998 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000999
Richard Trieude5cc7d2013-01-31 01:44:26 +00001000 setMoreChildren(OldMoreChildren);
1001 if (HasDeclarationBody) {
1002 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001003 dumpStmt(D->getBody());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001004 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001005}
1006
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001007void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001008 dumpName(D);
1009 dumpType(D->getType());
1010 if (D->isMutable())
1011 OS << " mutable";
1012 if (D->isModulePrivate())
1013 OS << " __module_private__";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001014
1015 bool OldMoreChildren = hasMoreChildren();
1016 bool IsBitField = D->isBitField();
1017 Expr *Init = D->getInClassInitializer();
1018 bool HasInit = Init;
1019
1020 setMoreChildren(OldMoreChildren || HasInit);
1021 if (IsBitField) {
1022 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001023 dumpStmt(D->getBitWidth());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001024 }
1025 setMoreChildren(OldMoreChildren);
1026 if (HasInit) {
1027 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001028 dumpStmt(Init);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001029 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001030}
1031
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001032void ASTDumper::VisitVarDecl(const VarDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001033 dumpName(D);
1034 dumpType(D->getType());
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001035 StorageClass SC = D->getStorageClass();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001036 if (SC != SC_None)
1037 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
Richard Smithfd3834f2013-04-13 02:43:54 +00001038 switch (D->getTLSKind()) {
1039 case VarDecl::TLS_None: break;
1040 case VarDecl::TLS_Static: OS << " tls"; break;
1041 case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
1042 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001043 if (D->isModulePrivate())
1044 OS << " __module_private__";
1045 if (D->isNRVOVariable())
1046 OS << " nrvo";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001047 if (D->hasInit()) {
Richard Smithd9967cc2014-07-10 22:54:03 +00001048 switch (D->getInitStyle()) {
1049 case VarDecl::CInit: OS << " cinit"; break;
1050 case VarDecl::CallInit: OS << " callinit"; break;
1051 case VarDecl::ListInit: OS << " listinit"; break;
1052 }
Richard Trieude5cc7d2013-01-31 01:44:26 +00001053 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001054 dumpStmt(D->getInit());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001055 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001056}
1057
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001058void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001059 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001060 dumpStmt(D->getAsmString());
1061}
1062
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001063void ASTDumper::VisitImportDecl(const ImportDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001064 OS << ' ' << D->getImportedModule()->getFullModuleName();
1065}
1066
1067//===----------------------------------------------------------------------===//
1068// C++ Declarations
1069//===----------------------------------------------------------------------===//
1070
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001071void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001072 dumpName(D);
1073 if (D->isInline())
1074 OS << " inline";
1075 if (!D->isOriginalNamespace())
1076 dumpDeclRef(D->getOriginalNamespace(), "original");
1077}
1078
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001079void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001080 OS << ' ';
1081 dumpBareDeclRef(D->getNominatedNamespace());
1082}
1083
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001084void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001085 dumpName(D);
1086 dumpDeclRef(D->getAliasedNamespace());
1087}
1088
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001089void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001090 dumpName(D);
1091 dumpType(D->getUnderlyingType());
1092}
1093
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001094void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001095 dumpName(D);
1096 dumpTemplateParameters(D->getTemplateParameters());
1097 dumpDecl(D->getTemplatedDecl());
1098}
1099
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001100void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001101 VisitRecordDecl(D);
1102 if (!D->isCompleteDefinition())
1103 return;
1104
Aaron Ballman574705e2014-03-13 15:41:46 +00001105 for (const auto &I : D->bases()) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001106 IndentScope Indent(*this);
Aaron Ballman574705e2014-03-13 15:41:46 +00001107 if (I.isVirtual())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001108 OS << "virtual ";
Aaron Ballman574705e2014-03-13 15:41:46 +00001109 dumpAccessSpecifier(I.getAccessSpecifier());
1110 dumpType(I.getType());
1111 if (I.isPackExpansion())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001112 OS << "...";
1113 }
1114}
1115
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001116void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001117 dumpStmt(D->getAssertExpr());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001118 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001119 dumpStmt(D->getMessage());
1120}
1121
Richard Smithcbdf7332014-03-18 02:07:28 +00001122template<typename SpecializationDecl>
1123void ASTDumper::VisitTemplateDeclSpecialization(ChildDumper &Children,
1124 const SpecializationDecl *D,
1125 bool DumpExplicitInst,
1126 bool DumpRefOnly) {
1127 bool DumpedAny = false;
1128 for (auto *RedeclWithBadType : D->redecls()) {
1129 // FIXME: The redecls() range sometimes has elements of a less-specific
1130 // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
1131 // us TagDecls, and should give CXXRecordDecls).
1132 auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
1133 if (!Redecl) {
1134 // Found the injected-class-name for a class template. This will be dumped
1135 // as part of its surrounding class so we don't need to dump it here.
1136 assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
1137 "expected an injected-class-name");
1138 continue;
1139 }
1140
1141 switch (Redecl->getTemplateSpecializationKind()) {
1142 case TSK_ExplicitInstantiationDeclaration:
1143 case TSK_ExplicitInstantiationDefinition:
1144 if (!DumpExplicitInst)
1145 break;
1146 // Fall through.
1147 case TSK_Undeclared:
1148 case TSK_ImplicitInstantiation:
1149 Children.dump(Redecl, DumpRefOnly);
1150 DumpedAny = true;
1151 break;
1152 case TSK_ExplicitSpecialization:
1153 break;
1154 }
1155 }
1156
1157 // Ensure we dump at least one decl for each specialization.
1158 if (!DumpedAny)
1159 Children.dumpRef(D);
1160}
1161
Richard Smith20ade552014-03-17 23:34:53 +00001162template<typename TemplateDecl>
1163void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
1164 bool DumpExplicitInst) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001165 dumpName(D);
1166 dumpTemplateParameters(D->getTemplateParameters());
Richard Smithdcc2c452014-03-17 23:00:06 +00001167
1168 ChildDumper Children(*this);
1169 Children.dump(D->getTemplatedDecl());
1170
Richard Smithcbdf7332014-03-18 02:07:28 +00001171 for (auto *Child : D->specializations())
1172 VisitTemplateDeclSpecialization(Children, Child, DumpExplicitInst,
1173 !D->isCanonicalDecl());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001174}
1175
Richard Smith20ade552014-03-17 23:34:53 +00001176void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
1177 // FIXME: We don't add a declaration of a function template specialization
1178 // to its context when it's explicitly instantiated, so dump explicit
1179 // instantiations when we dump the template itself.
1180 VisitTemplateDecl(D, true);
1181}
1182
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001183void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
Richard Smith20ade552014-03-17 23:34:53 +00001184 VisitTemplateDecl(D, false);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001185}
1186
1187void ASTDumper::VisitClassTemplateSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001188 const ClassTemplateSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001189 VisitCXXRecordDecl(D);
1190 dumpTemplateArgumentList(D->getTemplateArgs());
1191}
1192
1193void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001194 const ClassTemplatePartialSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001195 VisitClassTemplateSpecializationDecl(D);
1196 dumpTemplateParameters(D->getTemplateParameters());
1197}
1198
1199void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001200 const ClassScopeFunctionSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001201 dumpDeclRef(D->getSpecialization());
1202 if (D->hasExplicitTemplateArgs())
1203 dumpTemplateArgumentListInfo(D->templateArgs());
1204}
1205
Richard Smithd25789a2013-09-18 01:36:02 +00001206void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
Richard Smith20ade552014-03-17 23:34:53 +00001207 VisitTemplateDecl(D, false);
Richard Smithd25789a2013-09-18 01:36:02 +00001208}
1209
1210void ASTDumper::VisitVarTemplateSpecializationDecl(
1211 const VarTemplateSpecializationDecl *D) {
1212 dumpTemplateArgumentList(D->getTemplateArgs());
1213 VisitVarDecl(D);
1214}
1215
1216void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
1217 const VarTemplatePartialSpecializationDecl *D) {
1218 dumpTemplateParameters(D->getTemplateParameters());
1219 VisitVarTemplateSpecializationDecl(D);
1220}
1221
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001222void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001223 if (D->wasDeclaredWithTypename())
1224 OS << " typename";
1225 else
1226 OS << " class";
1227 if (D->isParameterPack())
1228 OS << " ...";
1229 dumpName(D);
Richard Smithecf74ff2014-03-23 20:50:39 +00001230 if (D->hasDefaultArgument()) {
1231 lastChild();
1232 dumpTemplateArgument(D->getDefaultArgument());
1233 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001234}
1235
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001236void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001237 dumpType(D->getType());
1238 if (D->isParameterPack())
1239 OS << " ...";
1240 dumpName(D);
Richard Smithecf74ff2014-03-23 20:50:39 +00001241 if (D->hasDefaultArgument()) {
1242 lastChild();
1243 dumpTemplateArgument(D->getDefaultArgument());
1244 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001245}
1246
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001247void ASTDumper::VisitTemplateTemplateParmDecl(
1248 const TemplateTemplateParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001249 if (D->isParameterPack())
1250 OS << " ...";
1251 dumpName(D);
1252 dumpTemplateParameters(D->getTemplateParameters());
Richard Smithecf74ff2014-03-23 20:50:39 +00001253 if (D->hasDefaultArgument()) {
1254 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001255 dumpTemplateArgumentLoc(D->getDefaultArgument());
Richard Smithecf74ff2014-03-23 20:50:39 +00001256 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001257}
1258
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001259void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001260 OS << ' ';
1261 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1262 OS << D->getNameAsString();
1263}
1264
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001265void ASTDumper::VisitUnresolvedUsingTypenameDecl(
1266 const UnresolvedUsingTypenameDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001267 OS << ' ';
1268 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1269 OS << D->getNameAsString();
1270}
1271
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001272void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001273 OS << ' ';
1274 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1275 OS << D->getNameAsString();
1276 dumpType(D->getType());
1277}
1278
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001279void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001280 OS << ' ';
1281 dumpBareDeclRef(D->getTargetDecl());
1282}
1283
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001284void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001285 switch (D->getLanguage()) {
1286 case LinkageSpecDecl::lang_c: OS << " C"; break;
1287 case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1288 }
1289}
1290
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001291void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001292 OS << ' ';
1293 dumpAccessSpecifier(D->getAccess());
1294}
1295
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001296void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
Richard Smithf5f43542013-02-07 01:35:44 +00001297 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001298 if (TypeSourceInfo *T = D->getFriendType())
1299 dumpType(T->getType());
1300 else
1301 dumpDecl(D->getFriendDecl());
1302}
1303
1304//===----------------------------------------------------------------------===//
1305// Obj-C Declarations
1306//===----------------------------------------------------------------------===//
1307
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001308void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001309 dumpName(D);
1310 dumpType(D->getType());
1311 if (D->getSynthesize())
1312 OS << " synthesize";
1313
1314 switch (D->getAccessControl()) {
1315 case ObjCIvarDecl::None:
1316 OS << " none";
1317 break;
1318 case ObjCIvarDecl::Private:
1319 OS << " private";
1320 break;
1321 case ObjCIvarDecl::Protected:
1322 OS << " protected";
1323 break;
1324 case ObjCIvarDecl::Public:
1325 OS << " public";
1326 break;
1327 case ObjCIvarDecl::Package:
1328 OS << " package";
1329 break;
1330 }
1331}
1332
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001333void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001334 if (D->isInstanceMethod())
1335 OS << " -";
1336 else
1337 OS << " +";
1338 dumpName(D);
Alp Toker314cc812014-01-25 16:55:45 +00001339 dumpType(D->getReturnType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001340
Richard Trieude5cc7d2013-01-31 01:44:26 +00001341 bool OldMoreChildren = hasMoreChildren();
1342 bool IsVariadic = D->isVariadic();
1343 bool HasBody = D->hasBody();
1344
1345 setMoreChildren(OldMoreChildren || IsVariadic || HasBody);
1346 if (D->isThisDeclarationADefinition()) {
1347 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001348 dumpDeclContext(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001349 } else {
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001350 for (ObjCMethodDecl::param_const_iterator I = D->param_begin(),
1351 E = D->param_end();
1352 I != E; ++I) {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001353 if (I + 1 == E)
1354 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001355 dumpDecl(*I);
1356 }
1357 }
1358
Richard Trieude5cc7d2013-01-31 01:44:26 +00001359 setMoreChildren(OldMoreChildren || HasBody);
1360 if (IsVariadic) {
1361 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001362 IndentScope Indent(*this);
1363 OS << "...";
1364 }
1365
Richard Trieude5cc7d2013-01-31 01:44:26 +00001366 setMoreChildren(OldMoreChildren);
1367 if (HasBody) {
1368 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001369 dumpStmt(D->getBody());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001370 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001371}
1372
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001373void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001374 dumpName(D);
1375 dumpDeclRef(D->getClassInterface());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001376 if (D->protocol_begin() == D->protocol_end())
1377 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001378 dumpDeclRef(D->getImplementation());
1379 for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001380 E = D->protocol_end();
1381 I != E; ++I) {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001382 if (I + 1 == E)
1383 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001384 dumpDeclRef(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001385 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001386}
1387
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001388void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001389 dumpName(D);
1390 dumpDeclRef(D->getClassInterface());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001391 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001392 dumpDeclRef(D->getCategoryDecl());
1393}
1394
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001395void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001396 dumpName(D);
Richard Smithdcc2c452014-03-17 23:00:06 +00001397
1398 ChildDumper Children(*this);
Richard Smith7fcb35f2014-03-18 02:37:59 +00001399 for (auto *Child : D->protocols())
1400 Children.dumpRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001401}
1402
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001403void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001404 dumpName(D);
1405 dumpDeclRef(D->getSuperClass(), "super");
Richard Smithdcc2c452014-03-17 23:00:06 +00001406
1407 ChildDumper Children(*this);
1408 Children.dumpRef(D->getImplementation());
Richard Smith7fcb35f2014-03-18 02:37:59 +00001409 for (auto *Child : D->protocols())
1410 Children.dumpRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001411}
1412
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001413void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001414 dumpName(D);
1415 dumpDeclRef(D->getSuperClass(), "super");
Richard Trieude5cc7d2013-01-31 01:44:26 +00001416 if (D->init_begin() == D->init_end())
1417 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001418 dumpDeclRef(D->getClassInterface());
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001419 for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
1420 E = D->init_end();
1421 I != E; ++I) {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001422 if (I + 1 == E)
1423 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001424 dumpCXXCtorInitializer(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001425 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001426}
1427
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001428void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001429 dumpName(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001430 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001431 dumpDeclRef(D->getClassInterface());
1432}
1433
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001434void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001435 dumpName(D);
1436 dumpType(D->getType());
1437
1438 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1439 OS << " required";
1440 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1441 OS << " optional";
1442
1443 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1444 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1445 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1446 OS << " readonly";
1447 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1448 OS << " assign";
1449 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1450 OS << " readwrite";
1451 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1452 OS << " retain";
1453 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1454 OS << " copy";
1455 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1456 OS << " nonatomic";
1457 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1458 OS << " atomic";
1459 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1460 OS << " weak";
1461 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1462 OS << " strong";
1463 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1464 OS << " unsafe_unretained";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001465 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter) {
1466 if (!(Attrs & ObjCPropertyDecl::OBJC_PR_setter))
1467 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001468 dumpDeclRef(D->getGetterMethodDecl(), "getter");
Richard Trieude5cc7d2013-01-31 01:44:26 +00001469 }
1470 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter) {
1471 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001472 dumpDeclRef(D->getSetterMethodDecl(), "setter");
Richard Trieude5cc7d2013-01-31 01:44:26 +00001473 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001474 }
1475}
1476
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001477void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001478 dumpName(D->getPropertyDecl());
1479 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1480 OS << " synthesize";
1481 else
1482 OS << " dynamic";
1483 dumpDeclRef(D->getPropertyDecl());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001484 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001485 dumpDeclRef(D->getPropertyIvarDecl());
1486}
1487
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001488void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00001489 for (auto I : D->params())
1490 dumpDecl(I);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001491
1492 if (D->isVariadic()) {
1493 IndentScope Indent(*this);
1494 OS << "...";
1495 }
1496
1497 if (D->capturesCXXThis()) {
1498 IndentScope Indent(*this);
1499 OS << "capture this";
1500 }
Aaron Ballman9371dd22014-03-14 18:34:04 +00001501 for (const auto &I : D->captures()) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001502 IndentScope Indent(*this);
1503 OS << "capture";
Aaron Ballman9371dd22014-03-14 18:34:04 +00001504 if (I.isByRef())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001505 OS << " byref";
Aaron Ballman9371dd22014-03-14 18:34:04 +00001506 if (I.isNested())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001507 OS << " nested";
Aaron Ballman9371dd22014-03-14 18:34:04 +00001508 if (I.getVariable()) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001509 OS << ' ';
Aaron Ballman9371dd22014-03-14 18:34:04 +00001510 dumpBareDeclRef(I.getVariable());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001511 }
Aaron Ballman9371dd22014-03-14 18:34:04 +00001512 if (I.hasCopyExpr())
1513 dumpStmt(I.getCopyExpr());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001514 }
Richard Trieude5cc7d2013-01-31 01:44:26 +00001515 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001516 dumpStmt(D->getBody());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001517}
1518
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001519//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001520// Stmt dumping methods.
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001521//===----------------------------------------------------------------------===//
1522
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001523void ASTDumper::dumpStmt(const Stmt *S) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001524 IndentScope Indent(*this);
1525
1526 if (!S) {
Richard Trieud215b8d2013-01-26 01:31:20 +00001527 ColorScope Color(*this, NullColor);
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001528 OS << "<<<NULL>>>";
1529 return;
1530 }
1531
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001532 if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001533 VisitDeclStmt(DS);
1534 return;
1535 }
1536
David Blaikie7d170102013-05-15 07:37:26 +00001537 setMoreChildren(!S->children().empty());
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001538 ConstStmtVisitor<ASTDumper>::Visit(S);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001539 setMoreChildren(false);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001540 for (Stmt::const_child_range CI = S->children(); CI; ++CI) {
1541 Stmt::const_child_range Next = CI;
Richard Trieude5cc7d2013-01-31 01:44:26 +00001542 ++Next;
1543 if (!Next)
1544 lastChild();
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001545 dumpStmt(*CI);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001546 }
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001547}
1548
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001549void ASTDumper::VisitStmt(const Stmt *Node) {
Richard Trieud215b8d2013-01-26 01:31:20 +00001550 {
1551 ColorScope Color(*this, StmtColor);
1552 OS << Node->getStmtClassName();
1553 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001554 dumpPointer(Node);
1555 dumpSourceRange(Node->getSourceRange());
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001556}
1557
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001558void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001559 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001560 for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
1561 E = Node->decl_end();
Richard Trieude5cc7d2013-01-31 01:44:26 +00001562 I != E; ++I) {
1563 if (I + 1 == E)
1564 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001565 dumpDecl(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001566 }
Ted Kremenek433a4922007-12-12 06:59:42 +00001567}
1568
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001569void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001570 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001571 for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
1572 E = Node->getAttrs().end();
1573 I != E; ++I) {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001574 if (I + 1 == E)
1575 lastChild();
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001576 dumpAttr(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001577 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001578}
1579
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001580void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001581 VisitStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001582 OS << " '" << Node->getName() << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001583}
1584
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001585void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001586 VisitStmt(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001587 OS << " '" << Node->getLabel()->getName() << "'";
1588 dumpPointer(Node->getLabel());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001589}
1590
Pavel Labath1ef83422013-09-04 14:35:00 +00001591void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
1592 VisitStmt(Node);
1593 dumpDecl(Node->getExceptionDecl());
1594}
1595
Chris Lattnercbe4f772007-08-08 22:51:59 +00001596//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001597// Expr dumping methods.
Chris Lattnercbe4f772007-08-08 22:51:59 +00001598//===----------------------------------------------------------------------===//
1599
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001600void ASTDumper::VisitExpr(const Expr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001601 VisitStmt(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001602 dumpType(Node->getType());
1603
Richard Trieud215b8d2013-01-26 01:31:20 +00001604 {
1605 ColorScope Color(*this, ValueKindColor);
1606 switch (Node->getValueKind()) {
1607 case VK_RValue:
1608 break;
1609 case VK_LValue:
1610 OS << " lvalue";
1611 break;
1612 case VK_XValue:
1613 OS << " xvalue";
1614 break;
1615 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001616 }
1617
Richard Trieud215b8d2013-01-26 01:31:20 +00001618 {
1619 ColorScope Color(*this, ObjectKindColor);
1620 switch (Node->getObjectKind()) {
1621 case OK_Ordinary:
1622 break;
1623 case OK_BitField:
1624 OS << " bitfield";
1625 break;
1626 case OK_ObjCProperty:
1627 OS << " objcproperty";
1628 break;
1629 case OK_ObjCSubscript:
1630 OS << " objcsubscript";
1631 break;
1632 case OK_VectorComponent:
1633 OS << " vectorcomponent";
1634 break;
1635 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001636 }
Chris Lattnercbe4f772007-08-08 22:51:59 +00001637}
1638
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001639static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
John McCallcf142162010-08-07 06:22:56 +00001640 if (Node->path_empty())
Anders Carlssona70cff62010-04-24 19:06:50 +00001641 return;
1642
1643 OS << " (";
1644 bool First = true;
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001645 for (CastExpr::path_const_iterator I = Node->path_begin(),
1646 E = Node->path_end();
1647 I != E; ++I) {
Anders Carlssona70cff62010-04-24 19:06:50 +00001648 const CXXBaseSpecifier *Base = *I;
1649 if (!First)
1650 OS << " -> ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001651
Anders Carlssona70cff62010-04-24 19:06:50 +00001652 const CXXRecordDecl *RD =
1653 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001654
Anders Carlssona70cff62010-04-24 19:06:50 +00001655 if (Base->isVirtual())
1656 OS << "virtual ";
1657 OS << RD->getName();
1658 First = false;
1659 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001660
Anders Carlssona70cff62010-04-24 19:06:50 +00001661 OS << ')';
1662}
1663
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001664void ASTDumper::VisitCastExpr(const CastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001665 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001666 OS << " <";
1667 {
1668 ColorScope Color(*this, CastColor);
1669 OS << Node->getCastKindName();
1670 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001671 dumpBasePath(OS, Node);
Anders Carlssona70cff62010-04-24 19:06:50 +00001672 OS << ">";
Anders Carlssond7923c62009-08-22 23:33:40 +00001673}
1674
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001675void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001676 VisitExpr(Node);
Ted Kremenek5f64ca82007-09-10 17:32:55 +00001677
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001678 OS << " ";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001679 dumpBareDeclRef(Node->getDecl());
Chandler Carruth8d26bb02011-05-01 23:48:14 +00001680 if (Node->getDecl() != Node->getFoundDecl()) {
1681 OS << " (";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001682 dumpBareDeclRef(Node->getFoundDecl());
Chandler Carruth8d26bb02011-05-01 23:48:14 +00001683 OS << ")";
1684 }
John McCall351762c2011-02-07 10:33:21 +00001685}
1686
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001687void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001688 VisitExpr(Node);
John McCall76d09942009-12-11 21:50:11 +00001689 OS << " (";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001690 if (!Node->requiresADL())
1691 OS << "no ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001692 OS << "ADL) = '" << Node->getName() << '\'';
John McCall76d09942009-12-11 21:50:11 +00001693
1694 UnresolvedLookupExpr::decls_iterator
1695 I = Node->decls_begin(), E = Node->decls_end();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001696 if (I == E)
1697 OS << " empty";
John McCall76d09942009-12-11 21:50:11 +00001698 for (; I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001699 dumpPointer(*I);
John McCall76d09942009-12-11 21:50:11 +00001700}
1701
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001702void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001703 VisitExpr(Node);
Steve Naroff5d5efca2008-03-12 13:19:12 +00001704
Richard Trieud215b8d2013-01-26 01:31:20 +00001705 {
1706 ColorScope Color(*this, DeclKindNameColor);
1707 OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
1708 }
1709 OS << "='" << *Node->getDecl() << "'";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001710 dumpPointer(Node->getDecl());
Steve Naroffb3424a92008-05-23 22:01:24 +00001711 if (Node->isFreeIvar())
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001712 OS << " isFreeIvar";
Steve Naroff5d5efca2008-03-12 13:19:12 +00001713}
1714
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001715void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001716 VisitExpr(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001717 switch (Node->getIdentType()) {
David Blaikie83d382b2011-09-23 05:06:16 +00001718 default: llvm_unreachable("unknown case");
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001719 case PredefinedExpr::Func: OS << " __func__"; break;
1720 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
David Majnemerbed356a2013-11-06 23:31:56 +00001721 case PredefinedExpr::FuncDName: OS << " __FUNCDNAME__"; break;
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001722 case PredefinedExpr::LFunction: OS << " L__FUNCTION__"; break;
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001723 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
Reid Kleckner52eddda2014-04-08 18:13:24 +00001724 case PredefinedExpr::FuncSig: OS << " __FUNCSIG__"; break;
Chris Lattnercbe4f772007-08-08 22:51:59 +00001725 }
1726}
1727
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001728void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001729 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001730 ColorScope Color(*this, ValueColor);
Richard Trieu364ee422011-11-03 23:56:23 +00001731 OS << " " << Node->getValue();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001732}
1733
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001734void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001735 VisitExpr(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001736
1737 bool isSigned = Node->getType()->isSignedIntegerType();
Richard Trieud215b8d2013-01-26 01:31:20 +00001738 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001739 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001740}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001741
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001742void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001743 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001744 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001745 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001746}
Chris Lattner1c20a172007-08-26 03:42:43 +00001747
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001748void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001749 VisitExpr(Str);
Richard Trieud215b8d2013-01-26 01:31:20 +00001750 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001751 OS << " ";
Richard Trieudc355912012-06-13 20:25:24 +00001752 Str->outputString(OS);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001753}
Chris Lattner84ca3762007-08-30 01:00:35 +00001754
Richard Smithf0514962014-06-03 08:24:28 +00001755void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
1756 VisitExpr(ILE);
1757 if (auto *Filler = ILE->getArrayFiller()) {
1758 if (!ILE->getNumInits())
1759 lastChild();
1760 IndentScope Indent(*this);
1761 OS << "array filler";
1762 lastChild();
1763 dumpStmt(Filler);
1764 }
1765 if (auto *Field = ILE->getInitializedFieldInUnion()) {
1766 OS << " field ";
1767 dumpBareDeclRef(Field);
1768 }
1769}
1770
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001771void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001772 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001773 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
1774 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001775}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001776
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001777void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
1778 const UnaryExprOrTypeTraitExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001779 VisitExpr(Node);
Peter Collingbournee190dee2011-03-11 19:24:49 +00001780 switch(Node->getKind()) {
1781 case UETT_SizeOf:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001782 OS << " sizeof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001783 break;
1784 case UETT_AlignOf:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001785 OS << " alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001786 break;
1787 case UETT_VecStep:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001788 OS << " vec_step";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001789 break;
1790 }
Sebastian Redl6f282892008-11-11 17:56:53 +00001791 if (Node->isArgumentType())
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001792 dumpType(Node->getArgumentType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001793}
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +00001794
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001795void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001796 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001797 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
1798 dumpPointer(Node->getMemberDecl());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001799}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001800
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001801void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001802 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001803 OS << " " << Node->getAccessor().getNameStart();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001804}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001805
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001806void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001807 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001808 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner86928112007-08-25 02:00:02 +00001809}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001810
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001811void ASTDumper::VisitCompoundAssignOperator(
1812 const CompoundAssignOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001813 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001814 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
1815 << "' ComputeLHSTy=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001816 dumpBareType(Node->getComputationLHSType());
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001817 OS << " ComputeResultTy=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001818 dumpBareType(Node->getComputationResultType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001819}
Chris Lattnercbe4f772007-08-08 22:51:59 +00001820
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001821void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001822 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001823 dumpDecl(Node->getBlockDecl());
John McCall351762c2011-02-07 10:33:21 +00001824}
1825
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001826void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001827 VisitExpr(Node);
John McCallfe96e0b2011-11-06 09:01:30 +00001828
Richard Trieude5cc7d2013-01-31 01:44:26 +00001829 if (Expr *Source = Node->getSourceExpr()) {
1830 lastChild();
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001831 dumpStmt(Source);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001832 }
John McCallfe96e0b2011-11-06 09:01:30 +00001833}
1834
Chris Lattnercbe4f772007-08-08 22:51:59 +00001835// GNU extensions.
1836
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001837void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001838 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001839 OS << " " << Node->getLabel()->getName();
1840 dumpPointer(Node->getLabel());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001841}
1842
Chris Lattner8f184b12007-08-09 18:03:18 +00001843//===----------------------------------------------------------------------===//
1844// C++ Expressions
1845//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00001846
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001847void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001848 VisitExpr(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001849 OS << " " << Node->getCastName()
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001850 << "<" << Node->getTypeAsWritten().getAsString() << ">"
Anders Carlssona70cff62010-04-24 19:06:50 +00001851 << " <" << Node->getCastKindName();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001852 dumpBasePath(OS, Node);
Anders Carlssona70cff62010-04-24 19:06:50 +00001853 OS << ">";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001854}
1855
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001856void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001857 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001858 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattnercbe4f772007-08-08 22:51:59 +00001859}
1860
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001861void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001862 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001863 OS << " this";
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001864}
1865
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001866void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001867 VisitExpr(Node);
Eli Friedman29538892011-09-02 17:38:59 +00001868 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
1869 << " <" << Node->getCastKindName() << ">";
Douglas Gregore200adc2008-10-27 19:41:14 +00001870}
1871
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001872void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001873 VisitExpr(Node);
John McCalleba90cd2010-02-02 19:03:45 +00001874 CXXConstructorDecl *Ctor = Node->getConstructor();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001875 dumpType(Ctor->getType());
Anders Carlsson073846832009-08-12 00:21:52 +00001876 if (Node->isElidable())
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001877 OS << " elidable";
John McCall85370042010-08-07 06:38:55 +00001878 if (Node->requiresZeroInitialization())
1879 OS << " zeroing";
Anders Carlsson073846832009-08-12 00:21:52 +00001880}
1881
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001882void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001883 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001884 OS << " ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001885 dumpCXXTemporary(Node->getTemporary());
Anders Carlsson073846832009-08-12 00:21:52 +00001886}
1887
Richard Smithe6c01442013-06-05 00:46:14 +00001888void
1889ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
1890 VisitExpr(Node);
1891 if (const ValueDecl *VD = Node->getExtendingDecl()) {
1892 OS << " extended by ";
1893 dumpBareDeclRef(VD);
1894 }
1895}
1896
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001897void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001898 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001899 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
1900 dumpDeclRef(Node->getObject(i), "cleanup");
Anders Carlsson073846832009-08-12 00:21:52 +00001901}
1902
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001903void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001904 OS << "(CXXTemporary";
1905 dumpPointer(Temporary);
1906 OS << ")";
Anders Carlsson073846832009-08-12 00:21:52 +00001907}
1908
Anders Carlsson76f4a902007-08-21 17:43:55 +00001909//===----------------------------------------------------------------------===//
1910// Obj-C Expressions
1911//===----------------------------------------------------------------------===//
1912
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001913void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001914 VisitExpr(Node);
Aaron Ballmanb190f972014-01-03 17:59:55 +00001915 OS << " selector=";
1916 Node->getSelector().print(OS);
Douglas Gregor9a129192010-04-21 00:45:42 +00001917 switch (Node->getReceiverKind()) {
1918 case ObjCMessageExpr::Instance:
1919 break;
1920
1921 case ObjCMessageExpr::Class:
1922 OS << " class=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001923 dumpBareType(Node->getClassReceiver());
Douglas Gregor9a129192010-04-21 00:45:42 +00001924 break;
1925
1926 case ObjCMessageExpr::SuperInstance:
1927 OS << " super (instance)";
1928 break;
1929
1930 case ObjCMessageExpr::SuperClass:
1931 OS << " super (class)";
1932 break;
1933 }
Ted Kremenek36748da2008-02-29 22:04:05 +00001934}
1935
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001936void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001937 VisitExpr(Node);
Aaron Ballmanb190f972014-01-03 17:59:55 +00001938 OS << " selector=";
1939 Node->getBoxingMethod()->getSelector().print(OS);
Argyrios Kyrtzidis9dd40892012-05-10 20:02:31 +00001940}
1941
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001942void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001943 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001944 if (const VarDecl *CatchParam = Node->getCatchParamDecl())
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001945 dumpDecl(CatchParam);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001946 else
Douglas Gregor96c79492010-04-23 22:50:49 +00001947 OS << " catch all";
Douglas Gregor96c79492010-04-23 22:50:49 +00001948}
1949
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001950void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001951 VisitExpr(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001952 dumpType(Node->getEncodedType());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001953}
1954
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001955void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001956 VisitExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +00001957
Aaron Ballmanb190f972014-01-03 17:59:55 +00001958 OS << " ";
1959 Node->getSelector().print(OS);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001960}
1961
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001962void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001963 VisitExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +00001964
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001965 OS << ' ' << *Node->getProtocol();
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001966}
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001967
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001968void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001969 VisitExpr(Node);
John McCallb7bd14f2010-12-02 01:19:52 +00001970 if (Node->isImplicitProperty()) {
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +00001971 OS << " Kind=MethodRef Getter=\"";
1972 if (Node->getImplicitPropertyGetter())
Aaron Ballmanb190f972014-01-03 17:59:55 +00001973 Node->getImplicitPropertyGetter()->getSelector().print(OS);
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +00001974 else
1975 OS << "(null)";
1976
1977 OS << "\" Setter=\"";
John McCallb7bd14f2010-12-02 01:19:52 +00001978 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
Aaron Ballmanb190f972014-01-03 17:59:55 +00001979 Setter->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +00001980 else
1981 OS << "(null)";
1982 OS << "\"";
1983 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001984 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
John McCallb7bd14f2010-12-02 01:19:52 +00001985 }
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00001986
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001987 if (Node->isSuperReceiver())
1988 OS << " super";
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +00001989
1990 OS << " Messaging=";
1991 if (Node->isMessagingGetter() && Node->isMessagingSetter())
1992 OS << "Getter&Setter";
1993 else if (Node->isMessagingGetter())
1994 OS << "Getter";
1995 else if (Node->isMessagingSetter())
1996 OS << "Setter";
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001997}
1998
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001999void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002000 VisitExpr(Node);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002001 if (Node->isArraySubscriptRefExpr())
2002 OS << " Kind=ArraySubscript GetterForArray=\"";
2003 else
2004 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
2005 if (Node->getAtIndexMethodDecl())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002006 Node->getAtIndexMethodDecl()->getSelector().print(OS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002007 else
2008 OS << "(null)";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002009
Ted Kremeneke65b0862012-03-06 20:05:56 +00002010 if (Node->isArraySubscriptRefExpr())
2011 OS << "\" SetterForArray=\"";
2012 else
2013 OS << "\" SetterForDictionary=\"";
2014 if (Node->setAtIndexMethodDecl())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002015 Node->setAtIndexMethodDecl()->getSelector().print(OS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002016 else
2017 OS << "(null)";
2018}
2019
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002020void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002021 VisitExpr(Node);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002022 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
2023}
2024
Chris Lattnercbe4f772007-08-08 22:51:59 +00002025//===----------------------------------------------------------------------===//
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002026// Comments
2027//===----------------------------------------------------------------------===//
2028
2029const char *ASTDumper::getCommandName(unsigned CommandID) {
2030 if (Traits)
2031 return Traits->getCommandInfo(CommandID)->Name;
2032 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
2033 if (Info)
2034 return Info->Name;
2035 return "<not a builtin command>";
2036}
2037
2038void ASTDumper::dumpFullComment(const FullComment *C) {
2039 if (!C)
2040 return;
2041
2042 FC = C;
2043 dumpComment(C);
Craig Topper36250ad2014-05-12 05:36:57 +00002044 FC = nullptr;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002045}
2046
2047void ASTDumper::dumpComment(const Comment *C) {
2048 IndentScope Indent(*this);
2049
2050 if (!C) {
Richard Trieud215b8d2013-01-26 01:31:20 +00002051 ColorScope Color(*this, NullColor);
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002052 OS << "<<<NULL>>>";
2053 return;
2054 }
2055
Richard Trieud215b8d2013-01-26 01:31:20 +00002056 {
2057 ColorScope Color(*this, CommentColor);
2058 OS << C->getCommentKindName();
2059 }
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002060 dumpPointer(C);
2061 dumpSourceRange(C->getSourceRange());
2062 ConstCommentVisitor<ASTDumper>::visit(C);
2063 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
Richard Trieude5cc7d2013-01-31 01:44:26 +00002064 I != E; ++I) {
2065 if (I + 1 == E)
2066 lastChild();
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002067 dumpComment(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +00002068 }
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002069}
2070
2071void ASTDumper::visitTextComment(const TextComment *C) {
2072 OS << " Text=\"" << C->getText() << "\"";
2073}
2074
2075void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
2076 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2077 switch (C->getRenderKind()) {
2078 case InlineCommandComment::RenderNormal:
2079 OS << " RenderNormal";
2080 break;
2081 case InlineCommandComment::RenderBold:
2082 OS << " RenderBold";
2083 break;
2084 case InlineCommandComment::RenderMonospaced:
2085 OS << " RenderMonospaced";
2086 break;
2087 case InlineCommandComment::RenderEmphasized:
2088 OS << " RenderEmphasized";
2089 break;
2090 }
2091
2092 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2093 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2094}
2095
2096void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
2097 OS << " Name=\"" << C->getTagName() << "\"";
2098 if (C->getNumAttrs() != 0) {
2099 OS << " Attrs: ";
2100 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
2101 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
2102 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
2103 }
2104 }
2105 if (C->isSelfClosing())
2106 OS << " SelfClosing";
2107}
2108
2109void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
2110 OS << " Name=\"" << C->getTagName() << "\"";
2111}
2112
2113void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
2114 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2115 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2116 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2117}
2118
2119void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
2120 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
2121
2122 if (C->isDirectionExplicit())
2123 OS << " explicitly";
2124 else
2125 OS << " implicitly";
2126
2127 if (C->hasParamName()) {
2128 if (C->isParamIndexValid())
2129 OS << " Param=\"" << C->getParamName(FC) << "\"";
2130 else
2131 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2132 }
2133
Dmitri Gribenkodbff5c72014-03-19 14:03:47 +00002134 if (C->isParamIndexValid() && !C->isVarArgParam())
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002135 OS << " ParamIndex=" << C->getParamIndex();
2136}
2137
2138void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
2139 if (C->hasParamName()) {
2140 if (C->isPositionValid())
2141 OS << " Param=\"" << C->getParamName(FC) << "\"";
2142 else
2143 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2144 }
2145
2146 if (C->isPositionValid()) {
2147 OS << " Position=<";
2148 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
2149 OS << C->getIndex(i);
2150 if (i != e - 1)
2151 OS << ", ";
2152 }
2153 OS << ">";
2154 }
2155}
2156
2157void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
2158 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
2159 " CloseName=\"" << C->getCloseName() << "\"";
2160}
2161
2162void ASTDumper::visitVerbatimBlockLineComment(
2163 const VerbatimBlockLineComment *C) {
2164 OS << " Text=\"" << C->getText() << "\"";
2165}
2166
2167void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
2168 OS << " Text=\"" << C->getText() << "\"";
2169}
2170
2171//===----------------------------------------------------------------------===//
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002172// Decl method implementations
2173//===----------------------------------------------------------------------===//
2174
Alp Tokeref6b0072014-01-04 13:47:14 +00002175LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002176
Alp Tokeref6b0072014-01-04 13:47:14 +00002177LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS) const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002178 ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
2179 &getASTContext().getSourceManager());
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002180 P.dumpDecl(this);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002181}
2182
Alp Tokeref6b0072014-01-04 13:47:14 +00002183LLVM_DUMP_METHOD void Decl::dumpColor() const {
Richard Trieud215b8d2013-01-26 01:31:20 +00002184 ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
2185 &getASTContext().getSourceManager(), /*ShowColors*/true);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002186 P.dumpDecl(this);
Richard Trieud215b8d2013-01-26 01:31:20 +00002187}
Richard Smith33937e72013-06-22 21:49:40 +00002188
Alp Tokeref6b0072014-01-04 13:47:14 +00002189LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
Richard Smith6ea05822013-06-24 01:45:33 +00002190 dumpLookups(llvm::errs());
2191}
2192
Richard Smith35f986d2014-08-11 22:11:07 +00002193LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
2194 bool DumpDecls) const {
Richard Smith33937e72013-06-22 21:49:40 +00002195 const DeclContext *DC = this;
2196 while (!DC->isTranslationUnit())
2197 DC = DC->getParent();
2198 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smith6ea05822013-06-24 01:45:33 +00002199 ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
Richard Smith35f986d2014-08-11 22:11:07 +00002200 P.dumpLookups(this, DumpDecls);
Richard Smith33937e72013-06-22 21:49:40 +00002201}
2202
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002203//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00002204// Stmt method implementations
2205//===----------------------------------------------------------------------===//
2206
Alp Tokeref6b0072014-01-04 13:47:14 +00002207LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00002208 dump(llvm::errs(), SM);
2209}
2210
Alp Tokeref6b0072014-01-04 13:47:14 +00002211LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
Craig Topper36250ad2014-05-12 05:36:57 +00002212 ASTDumper P(OS, nullptr, &SM);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002213 P.dumpStmt(this);
Chris Lattner779d5d92007-08-30 00:40:08 +00002214}
2215
Alp Tokeref6b0072014-01-04 13:47:14 +00002216LLVM_DUMP_METHOD void Stmt::dump() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002217 ASTDumper P(llvm::errs(), nullptr, nullptr);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002218 P.dumpStmt(this);
Chris Lattnercbe4f772007-08-08 22:51:59 +00002219}
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002220
Alp Tokeref6b0072014-01-04 13:47:14 +00002221LLVM_DUMP_METHOD void Stmt::dumpColor() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002222 ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002223 P.dumpStmt(this);
Richard Trieud215b8d2013-01-26 01:31:20 +00002224}
2225
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002226//===----------------------------------------------------------------------===//
2227// Comment method implementations
2228//===----------------------------------------------------------------------===//
2229
Craig Topper36250ad2014-05-12 05:36:57 +00002230LLVM_DUMP_METHOD void Comment::dump() const {
2231 dump(llvm::errs(), nullptr, nullptr);
2232}
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002233
Alp Tokeref6b0072014-01-04 13:47:14 +00002234LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002235 dump(llvm::errs(), &Context.getCommentCommandTraits(),
2236 &Context.getSourceManager());
2237}
2238
Alexander Kornienko00911f12013-01-15 12:20:21 +00002239void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002240 const SourceManager *SM) const {
2241 const FullComment *FC = dyn_cast<FullComment>(this);
2242 ASTDumper D(OS, Traits, SM);
2243 D.dumpFullComment(FC);
2244}
Richard Trieud215b8d2013-01-26 01:31:20 +00002245
Alp Tokeref6b0072014-01-04 13:47:14 +00002246LLVM_DUMP_METHOD void Comment::dumpColor() const {
Richard Trieud215b8d2013-01-26 01:31:20 +00002247 const FullComment *FC = dyn_cast<FullComment>(this);
Craig Topper36250ad2014-05-12 05:36:57 +00002248 ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
Richard Trieud215b8d2013-01-26 01:31:20 +00002249 D.dumpFullComment(FC);
2250}