blob: 821eb55c728f1c061b2f99ad61a51107a415701f [file] [log] [blame]
Chris Lattner6000dac2007-08-08 22:51:59 +00001//===--- StmtDumper.cpp - Dumping implementation for Stmt ASTs ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner6000dac2007-08-08 22:51:59 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt::dump/Stmt::print methods, which dump out the
11// AST in a form that exposes type details and other fields.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Ted Kremenek91d1d7a2007-10-17 18:36:42 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000017#include "clang/AST/DeclCXX.h"
Chris Lattnere300c872007-08-30 06:17:34 +000018#include "clang/Basic/SourceManager.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000019#include "llvm/Support/Compiler.h"
20#include <cstdio>
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtDumper Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000028 class VISIBILITY_HIDDEN StmtDumper : public StmtVisitor<StmtDumper> {
Chris Lattnere300c872007-08-30 06:17:34 +000029 SourceManager *SM;
Chris Lattner6000dac2007-08-08 22:51:59 +000030 FILE *F;
31 unsigned IndentLevel;
32
33 /// MaxDepth - When doing a normal dump (not dumpAll) we only want to dump
34 /// the first few levels of an AST. This keeps track of how many ast levels
35 /// are left.
36 unsigned MaxDepth;
Chris Lattnere300c872007-08-30 06:17:34 +000037
38 /// LastLocFilename/LastLocLine - Keep track of the last location we print
39 /// out so that we can print out deltas from then on out.
40 const char *LastLocFilename;
41 unsigned LastLocLine;
Chris Lattner6000dac2007-08-08 22:51:59 +000042 public:
Chris Lattnere300c872007-08-30 06:17:34 +000043 StmtDumper(SourceManager *sm, FILE *f, unsigned maxDepth)
44 : SM(sm), F(f), IndentLevel(0-1), MaxDepth(maxDepth) {
45 LastLocFilename = "";
46 LastLocLine = ~0U;
47 }
Chris Lattner6000dac2007-08-08 22:51:59 +000048
Chris Lattnerf9e05812007-08-09 18:03:18 +000049 void DumpSubTree(Stmt *S) {
Chris Lattner6000dac2007-08-08 22:51:59 +000050 // Prune the recursion if not using dump all.
51 if (MaxDepth == 0) return;
52
Chris Lattnerf9e05812007-08-09 18:03:18 +000053 ++IndentLevel;
Chris Lattner6000dac2007-08-08 22:51:59 +000054 if (S) {
Ted Kremenek5399ce22007-12-12 06:59:42 +000055 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
56 VisitDeclStmt(DS);
57 else {
58 Visit(S);
59
60 // Print out children.
61 Stmt::child_iterator CI = S->child_begin(), CE = S->child_end();
62 if (CI != CE) {
63 while (CI != CE) {
64 fprintf(F, "\n");
65 DumpSubTree(*CI++);
66 }
Chris Lattnerb3938792007-08-30 00:53:54 +000067 }
Ted Kremenek5399ce22007-12-12 06:59:42 +000068 fprintf(F, ")");
Chris Lattnerb3938792007-08-30 00:53:54 +000069 }
Chris Lattner6000dac2007-08-08 22:51:59 +000070 } else {
71 Indent();
Chris Lattner4a70adb2007-08-26 03:53:29 +000072 fprintf(F, "<<<NULL>>>");
Chris Lattner6000dac2007-08-08 22:51:59 +000073 }
Chris Lattnerf9e05812007-08-09 18:03:18 +000074 --IndentLevel;
Chris Lattner6000dac2007-08-08 22:51:59 +000075 }
76
Chris Lattnerf9e05812007-08-09 18:03:18 +000077 void DumpDeclarator(Decl *D);
Chris Lattner6000dac2007-08-08 22:51:59 +000078
79 void Indent() const {
80 for (int i = 0, e = IndentLevel; i < e; ++i)
81 fprintf(F, " ");
82 }
83
Steve Naroff9dcbfa42007-09-01 21:08:38 +000084 void DumpType(QualType T) {
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000085 fprintf(F, "'%s'", T.getAsString().c_str());
86
Douglas Gregor61366e92008-12-24 00:01:03 +000087 if (!T.isNull()) {
88 // If the type is directly a typedef, strip off typedefness to give at
89 // least one level of concreteness.
90 if (TypedefType *TDT = dyn_cast<TypedefType>(T)) {
91 QualType Simplified =
92 TDT->LookThroughTypedefs().getQualifiedType(T.getCVRQualifiers());
93 fprintf(F, ":'%s'", Simplified.getAsString().c_str());
94 }
Chris Lattnerbad37852008-04-02 05:06:23 +000095 }
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000096 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +000097 void DumpStmt(const Stmt *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +000098 Indent();
99 fprintf(F, "(%s %p", Node->getStmtClassName(), (void*)Node);
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000100 DumpSourceRange(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000101 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000102 void DumpExpr(const Expr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000103 DumpStmt(Node);
Chris Lattnerfd8f7da2007-08-09 00:36:22 +0000104 fprintf(F, " ");
105 DumpType(Node->getType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000106 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000107 void DumpSourceRange(const Stmt *Node);
Chris Lattnere300c872007-08-30 06:17:34 +0000108 void DumpLocation(SourceLocation Loc);
Chris Lattnere300c872007-08-30 06:17:34 +0000109
Chris Lattner17a1a722007-08-30 01:00:35 +0000110 // Stmts.
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000111 void VisitStmt(Stmt *Node);
Ted Kremenek5399ce22007-12-12 06:59:42 +0000112 void VisitDeclStmt(DeclStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000113 void VisitLabelStmt(LabelStmt *Node);
114 void VisitGotoStmt(GotoStmt *Node);
115
116 // Exprs
117 void VisitExpr(Expr *Node);
118 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattnerd9f69102008-08-10 01:53:14 +0000119 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000120 void VisitCharacterLiteral(CharacterLiteral *Node);
121 void VisitIntegerLiteral(IntegerLiteral *Node);
122 void VisitFloatingLiteral(FloatingLiteral *Node);
123 void VisitStringLiteral(StringLiteral *Str);
124 void VisitUnaryOperator(UnaryOperator *Node);
Sebastian Redl05189992008-11-11 17:56:53 +0000125 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000126 void VisitMemberExpr(MemberExpr *Node);
Nate Begeman213541a2008-04-18 23:10:10 +0000127 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000128 void VisitBinaryOperator(BinaryOperator *Node);
129 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
130 void VisitAddrLabelExpr(AddrLabelExpr *Node);
131 void VisitTypesCompatibleExpr(TypesCompatibleExpr *Node);
132
133 // C++
Douglas Gregor49badde2008-10-27 19:41:14 +0000134 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000135 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000136 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000137 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000138
Chris Lattner17a1a722007-08-30 01:00:35 +0000139 // ObjC
140 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000141 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000142 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000143 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000144 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000145 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000146 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000147 void VisitObjCSuperExpr(ObjCSuperExpr *Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000148 };
149}
150
151//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000152// Utilities
153//===----------------------------------------------------------------------===//
154
155void StmtDumper::DumpLocation(SourceLocation Loc) {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000156 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000157
158 if (SpellingLoc.isInvalid()) {
159 fprintf(stderr, "<invalid sloc>");
160 return;
161 }
Chris Lattnere300c872007-08-30 06:17:34 +0000162
163 // The general format we print out is filename:line:col, but we drop pieces
164 // that haven't changed since the last loc printed.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000165 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
166
167 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
168 fprintf(stderr, "%s:%u:%u", PLoc.getFilename(), PLoc.getLine(),
169 PLoc.getColumn());
170 LastLocFilename = PLoc.getFilename();
171 LastLocLine = PLoc.getLine();
172 } else if (PLoc.getLine() != LastLocLine) {
173 fprintf(stderr, "line:%u:%u", PLoc.getLine(), PLoc.getColumn());
174 LastLocLine = PLoc.getLine();
Chris Lattnere300c872007-08-30 06:17:34 +0000175 } else {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000176 fprintf(stderr, "col:%u", PLoc.getColumn());
Chris Lattnere300c872007-08-30 06:17:34 +0000177 }
178}
179
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000180void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattnere300c872007-08-30 06:17:34 +0000181 // Can't translate locations if a SourceManager isn't available.
182 if (SM == 0) return;
183
184 // TODO: If the parent expression is available, we can print a delta vs its
185 // location.
186 SourceRange R = Node->getSourceRange();
187
188 fprintf(stderr, " <");
Chris Lattner311ff022007-10-16 22:36:42 +0000189 DumpLocation(R.getBegin());
190 if (R.getBegin() != R.getEnd()) {
Chris Lattnere300c872007-08-30 06:17:34 +0000191 fprintf(stderr, ", ");
Chris Lattner311ff022007-10-16 22:36:42 +0000192 DumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000193 }
194 fprintf(stderr, ">");
195
196 // <t2.c:123:421[blah], t2.c:412:321>
197
198}
199
200
201//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000202// Stmt printing methods.
203//===----------------------------------------------------------------------===//
204
205void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner17a1a722007-08-30 01:00:35 +0000206 DumpStmt(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000207}
208
Chris Lattnerf9e05812007-08-09 18:03:18 +0000209void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000210 // FIXME: Need to complete/beautify this... this code simply shows the
211 // nodes are where they need to be.
212 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000213 fprintf(F, "\"typedef %s %s\"",
214 localType->getUnderlyingType().getAsString().c_str(),
Chris Lattner8ec03f52008-11-24 03:54:41 +0000215 localType->getNameAsString().c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000216 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000217 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000218 // Emit storage class for vardecls.
219 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000220 if (V->getStorageClass() != VarDecl::None)
221 fprintf(F, "%s ",
222 VarDecl::getStorageClassSpecifierString(V->getStorageClass()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000223 }
224
Chris Lattner39f34e92008-11-24 04:00:27 +0000225 std::string Name = VD->getNameAsString();
Chris Lattner6000dac2007-08-08 22:51:59 +0000226 VD->getType().getAsStringInternal(Name);
Chris Lattnerf9e05812007-08-09 18:03:18 +0000227 fprintf(F, "%s", Name.c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000228
229 // If this is a vardecl with an initializer, emit it.
230 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
231 if (V->getInit()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000232 fprintf(F, " =\n");
233 DumpSubTree(V->getInit());
Chris Lattner6000dac2007-08-08 22:51:59 +0000234 }
235 }
Chris Lattnerf9e05812007-08-09 18:03:18 +0000236 fprintf(F, "\"");
Steve Naroff92199282007-11-17 21:37:36 +0000237 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
238 // print a free standing tag decl (e.g. "struct x;").
239 const char *tagname;
240 if (const IdentifierInfo *II = TD->getIdentifier())
241 tagname = II->getName();
242 else
243 tagname = "<anonymous>";
244 fprintf(F, "\"%s %s;\"", TD->getKindName(), tagname);
245 // FIXME: print tag bodies.
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000246 } else if (UsingDirectiveDecl *UD = dyn_cast<UsingDirectiveDecl>(D)) {
247 // print using-directive decl (e.g. "using namespace x;")
248 const char *ns;
249 if (const IdentifierInfo *II = UD->getNominatedNamespace()->getIdentifier())
250 ns = II->getName();
251 else
252 ns = "<anonymous>";
253 fprintf(F, "\"%s %s;\"",UD->getDeclKindName(), ns);
Chris Lattner6000dac2007-08-08 22:51:59 +0000254 } else {
Chris Lattner6000dac2007-08-08 22:51:59 +0000255 assert(0 && "Unexpected decl");
256 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000257}
258
Ted Kremenek5399ce22007-12-12 06:59:42 +0000259void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
260 DumpStmt(Node);
261 fprintf(F,"\n");
Ted Kremenek04a72b72008-10-06 18:38:35 +0000262 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
263 DI != DE; ++DI) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000264 Decl* D = *DI;
Ted Kremenek5399ce22007-12-12 06:59:42 +0000265 ++IndentLevel;
266 Indent();
267 fprintf(F, "%p ", (void*) D);
268 DumpDeclarator(D);
Chris Lattnerf2797252009-03-29 16:04:50 +0000269 if (DI+1 != DE)
Ted Kremenek5399ce22007-12-12 06:59:42 +0000270 fprintf(F,"\n");
271 --IndentLevel;
272 }
273}
274
Chris Lattner6000dac2007-08-08 22:51:59 +0000275void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
276 DumpStmt(Node);
Chris Lattner3cb640b2008-07-26 19:24:43 +0000277 fprintf(F, " '%s'", Node->getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000278}
279
Chris Lattner6000dac2007-08-08 22:51:59 +0000280void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
281 DumpStmt(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000282 fprintf(F, " '%s':%p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000283}
284
Chris Lattner6000dac2007-08-08 22:51:59 +0000285//===----------------------------------------------------------------------===//
286// Expr printing methods.
287//===----------------------------------------------------------------------===//
288
289void StmtDumper::VisitExpr(Expr *Node) {
290 DumpExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000291}
292
293void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
294 DumpExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +0000295
296 fprintf(F, " ");
297 switch (Node->getDecl()->getKind()) {
298 case Decl::Function: fprintf(F,"FunctionDecl"); break;
Steve Naroff248a7532008-04-15 22:42:06 +0000299 case Decl::Var: fprintf(F,"Var"); break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000300 case Decl::ParmVar: fprintf(F,"ParmVar"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000301 case Decl::EnumConstant: fprintf(F,"EnumConstant"); break;
302 case Decl::Typedef: fprintf(F,"Typedef"); break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000303 case Decl::Record: fprintf(F,"Record"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000304 case Decl::Enum: fprintf(F,"Enum"); break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000305 case Decl::CXXRecord: fprintf(F,"CXXRecord"); break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000306 case Decl::ObjCInterface: fprintf(F,"ObjCInterface"); break;
307 case Decl::ObjCClass: fprintf(F,"ObjCClass"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000308 default: fprintf(F,"Decl"); break;
309 }
310
Chris Lattner39f34e92008-11-24 04:00:27 +0000311 fprintf(F, "='%s' %p", Node->getDecl()->getNameAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000312 (void*)Node->getDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000313}
314
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000315void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroff466c2e32008-05-23 00:59:14 +0000316 DumpExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000317
Steve Naroff466c2e32008-05-23 00:59:14 +0000318 fprintf(F, " %sDecl='%s' %p", Node->getDecl()->getDeclKindName(),
Chris Lattner8ec03f52008-11-24 03:54:41 +0000319 Node->getDecl()->getNameAsString().c_str(), (void*)Node->getDecl());
Steve Naroff218543b2008-05-23 22:01:24 +0000320 if (Node->isFreeIvar())
321 fprintf(F, " isFreeIvar");
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000322}
323
Chris Lattnerd9f69102008-08-10 01:53:14 +0000324void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000325 DumpExpr(Node);
326 switch (Node->getIdentType()) {
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000327 default: assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000328 case PredefinedExpr::Func: fprintf(F, " __func__"); break;
329 case PredefinedExpr::Function: fprintf(F, " __FUNCTION__"); break;
330 case PredefinedExpr::PrettyFunction: fprintf(F, " __PRETTY_FUNCTION__");break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000331 }
332}
333
334void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000335 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000336 fprintf(F, " %d", Node->getValue());
Chris Lattner6000dac2007-08-08 22:51:59 +0000337}
338
339void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
340 DumpExpr(Node);
341
342 bool isSigned = Node->getType()->isSignedIntegerType();
Chris Lattnerb3938792007-08-30 00:53:54 +0000343 fprintf(F, " %s", Node->getValue().toString(10, isSigned).c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000344}
345void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
346 DumpExpr(Node);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000347 fprintf(F, " %f", Node->getValueAsApproximateDouble());
Chris Lattner6000dac2007-08-08 22:51:59 +0000348}
Chris Lattner5d661452007-08-26 03:42:43 +0000349
Chris Lattner6000dac2007-08-08 22:51:59 +0000350void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000351 DumpExpr(Str);
352 // FIXME: this doesn't print wstrings right.
Chris Lattner5fc61072007-08-09 17:14:24 +0000353 fprintf(F, " %s\"", Str->isWide() ? "L" : "");
Chris Lattnera0df31a2007-08-09 01:04:32 +0000354
Chris Lattner6000dac2007-08-08 22:51:59 +0000355 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner5fc61072007-08-09 17:14:24 +0000356 switch (char C = Str->getStrData()[i]) {
357 default:
358 if (isprint(C))
359 fputc(C, F);
360 else
361 fprintf(F, "\\%03o", C);
362 break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000363 // Handle some common ones to make dumps prettier.
Chris Lattner5fc61072007-08-09 17:14:24 +0000364 case '\\': fprintf(F, "\\\\"); break;
365 case '"': fprintf(F, "\\\""); break;
366 case '\n': fprintf(F, "\\n"); break;
367 case '\t': fprintf(F, "\\t"); break;
368 case '\a': fprintf(F, "\\a"); break;
369 case '\b': fprintf(F, "\\b"); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000370 }
371 }
Chris Lattnerb3938792007-08-30 00:53:54 +0000372 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000373}
Chris Lattner17a1a722007-08-30 01:00:35 +0000374
Chris Lattner6000dac2007-08-08 22:51:59 +0000375void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000376 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000377 fprintf(F, " %s '%s'", Node->isPostfix() ? "postfix" : "prefix",
Chris Lattner13cb21f2007-08-09 17:35:30 +0000378 UnaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000379}
Sebastian Redl05189992008-11-11 17:56:53 +0000380void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000381 DumpExpr(Node);
382 fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof");
Sebastian Redl05189992008-11-11 17:56:53 +0000383 if (Node->isArgumentType())
384 DumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000385}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000386
Chris Lattner6000dac2007-08-08 22:51:59 +0000387void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000388 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000389 fprintf(F, " %s%s %p", Node->isArrow() ? "->" : ".",
Chris Lattner077bf5e2008-11-24 03:33:13 +0000390 Node->getMemberDecl()->getNameAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000391 (void*)Node->getMemberDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000392}
Nate Begeman213541a2008-04-18 23:10:10 +0000393void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000394 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000395 fprintf(F, " %s", Node->getAccessor().getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000396}
Chris Lattner6000dac2007-08-08 22:51:59 +0000397void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
398 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000399 fprintf(F, " '%s'", BinaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattnereb14fe82007-08-25 02:00:02 +0000400}
401void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
402 DumpExpr(Node);
Eli Friedmanab3a8522009-03-28 01:22:36 +0000403 fprintf(F, " '%s' ComputeLHSTy=",
Chris Lattnereb14fe82007-08-25 02:00:02 +0000404 BinaryOperator::getOpcodeStr(Node->getOpcode()));
Eli Friedmanab3a8522009-03-28 01:22:36 +0000405 DumpType(Node->getComputationLHSType());
406 fprintf(F, " ComputeResultTy=");
407 DumpType(Node->getComputationResultType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000408}
Chris Lattner6000dac2007-08-08 22:51:59 +0000409
410// GNU extensions.
411
412void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000413 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000414 fprintf(F, " %s %p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000415}
416
Chris Lattner6000dac2007-08-08 22:51:59 +0000417void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000418 DumpExpr(Node);
419 fprintf(F, " ");
420 DumpType(Node->getArgType1());
421 fprintf(F, " ");
422 DumpType(Node->getArgType2());
Chris Lattner6000dac2007-08-08 22:51:59 +0000423}
424
Chris Lattnerf9e05812007-08-09 18:03:18 +0000425//===----------------------------------------------------------------------===//
426// C++ Expressions
427//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000428
Douglas Gregor49badde2008-10-27 19:41:14 +0000429void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000430 DumpExpr(Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000431 fprintf(F, " %s<%s>", Node->getCastName(),
432 Node->getTypeAsWritten().getAsString().c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000433}
434
435void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000436 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000437 fprintf(F, " %s", Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000438}
439
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000440void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
441 DumpExpr(Node);
442 fprintf(F, " this");
443}
444
Douglas Gregor49badde2008-10-27 19:41:14 +0000445void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
446 DumpExpr(Node);
447 fprintf(F, " functional cast to %s",
448 Node->getTypeAsWritten().getAsString().c_str());
449}
450
Anders Carlsson55085182007-08-21 17:43:55 +0000451//===----------------------------------------------------------------------===//
452// Obj-C Expressions
453//===----------------------------------------------------------------------===//
454
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000455void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
456 DumpExpr(Node);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000457 fprintf(F, " selector=%s", Node->getSelector().getAsString().c_str());
Ted Kremenekea958e572008-05-01 17:26:20 +0000458 IdentifierInfo* clsName = Node->getClassName();
459 if (clsName) fprintf(F, " class=%s", clsName->getName());
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000460}
461
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000462void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
463 DumpExpr(Node);
464
465 fprintf(F, " ");
466 DumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000467}
468
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000469void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
470 DumpExpr(Node);
471
472 fprintf(F, " ");
Chris Lattner077bf5e2008-11-24 03:33:13 +0000473 fprintf(F, "%s", Node->getSelector().getAsString().c_str());
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000474}
475
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000476void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
477 DumpExpr(Node);
478
479 fprintf(F, " ");
Chris Lattner8ec03f52008-11-24 03:54:41 +0000480 fprintf(F, "%s", Node->getProtocol()->getNameAsString().c_str());
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000481}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000482
483void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
484 DumpExpr(Node);
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000485
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000486 fprintf(F, " Kind=PropertyRef Property=\"%s\"",
Chris Lattner8ec03f52008-11-24 03:54:41 +0000487 Node->getProperty()->getNameAsString().c_str());
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000488}
489
490void StmtDumper::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
491 DumpExpr(Node);
492
493 ObjCMethodDecl *Getter = Node->getGetterMethod();
494 ObjCMethodDecl *Setter = Node->getSetterMethod();
495 fprintf(F, " Kind=MethodRef Getter=\"%s\" Setter=\"%s\"",
Chris Lattner077bf5e2008-11-24 03:33:13 +0000496 Getter->getSelector().getAsString().c_str(),
497 Setter ? Setter->getSelector().getAsString().c_str() : "(null)");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000498}
499
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000500void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
501 DumpExpr(Node);
502 fprintf(F, " super");
503}
504
Chris Lattner6000dac2007-08-08 22:51:59 +0000505//===----------------------------------------------------------------------===//
506// Stmt method implementations
507//===----------------------------------------------------------------------===//
508
509/// dump - This does a local dump of the specified AST fragment. It dumps the
510/// specified node and a few nodes underneath it, but not the whole subtree.
511/// This is useful in a debugger.
Chris Lattnere300c872007-08-30 06:17:34 +0000512void Stmt::dump(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000513 StmtDumper P(&SM, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000514 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000515 fprintf(stderr, "\n");
516}
517
518/// dump - This does a local dump of the specified AST fragment. It dumps the
519/// specified node and a few nodes underneath it, but not the whole subtree.
520/// This is useful in a debugger.
Chris Lattner6000dac2007-08-08 22:51:59 +0000521void Stmt::dump() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000522 StmtDumper P(0, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000523 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000524 fprintf(stderr, "\n");
525}
526
527/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattnere300c872007-08-30 06:17:34 +0000528void Stmt::dumpAll(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000529 StmtDumper P(&SM, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000530 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000531 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000532}
533
534/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
535void Stmt::dumpAll() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000536 StmtDumper P(0, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000537 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000538 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000539}