1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
//===--- StmtDumper.cpp - Dumping implementation for Stmt ASTs ------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the Stmt::dump/Stmt::print methods, which dump out the
// AST in a form that exposes type details and other fields.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/StmtVisitor.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/ExprCXX.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/SourceManager.h"
#include "llvm/Support/Compiler.h"
#include <cstdio>
using namespace clang;
//===----------------------------------------------------------------------===//
// StmtDumper Visitor
//===----------------------------------------------------------------------===//
namespace {
class VISIBILITY_HIDDEN StmtDumper : public StmtVisitor<StmtDumper> {
SourceManager *SM;
FILE *F;
unsigned IndentLevel;
/// MaxDepth - When doing a normal dump (not dumpAll) we only want to dump
/// the first few levels of an AST. This keeps track of how many ast levels
/// are left.
unsigned MaxDepth;
/// LastLocFilename/LastLocLine - Keep track of the last location we print
/// out so that we can print out deltas from then on out.
const char *LastLocFilename;
unsigned LastLocLine;
public:
StmtDumper(SourceManager *sm, FILE *f, unsigned maxDepth)
: SM(sm), F(f), IndentLevel(0-1), MaxDepth(maxDepth) {
LastLocFilename = "";
LastLocLine = ~0U;
}
void DumpSubTree(Stmt *S) {
// Prune the recursion if not using dump all.
if (MaxDepth == 0) return;
++IndentLevel;
if (S) {
if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
VisitDeclStmt(DS);
else {
Visit(S);
// Print out children.
Stmt::child_iterator CI = S->child_begin(), CE = S->child_end();
if (CI != CE) {
while (CI != CE) {
fprintf(F, "\n");
DumpSubTree(*CI++);
}
}
fprintf(F, ")");
}
} else {
Indent();
fprintf(F, "<<<NULL>>>");
}
--IndentLevel;
}
void DumpDeclarator(Decl *D);
void Indent() const {
for (int i = 0, e = IndentLevel; i < e; ++i)
fprintf(F, " ");
}
void DumpType(QualType T) {
fprintf(F, "'%s'", T.getAsString().c_str());
// If the type is directly a typedef, strip off typedefness to give at
// least one level of concreteness.
if (TypedefType *TDT = dyn_cast<TypedefType>(T)) {
QualType Simplified =
TDT->LookThroughTypedefs().getQualifiedType(T.getCVRQualifiers());
fprintf(F, ":'%s'", Simplified.getAsString().c_str());
}
}
void DumpStmt(const Stmt *Node) {
Indent();
fprintf(F, "(%s %p", Node->getStmtClassName(), (void*)Node);
DumpSourceRange(Node);
}
void DumpExpr(const Expr *Node) {
DumpStmt(Node);
fprintf(F, " ");
DumpType(Node->getType());
}
void DumpSourceRange(const Stmt *Node);
void DumpLocation(SourceLocation Loc);
// Stmts.
void VisitStmt(Stmt *Node);
void VisitDeclStmt(DeclStmt *Node);
void VisitLabelStmt(LabelStmt *Node);
void VisitGotoStmt(GotoStmt *Node);
// Exprs
void VisitExpr(Expr *Node);
void VisitDeclRefExpr(DeclRefExpr *Node);
void VisitPreDefinedExpr(PreDefinedExpr *Node);
void VisitCharacterLiteral(CharacterLiteral *Node);
void VisitIntegerLiteral(IntegerLiteral *Node);
void VisitFloatingLiteral(FloatingLiteral *Node);
void VisitStringLiteral(StringLiteral *Str);
void VisitUnaryOperator(UnaryOperator *Node);
void VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node);
void VisitMemberExpr(MemberExpr *Node);
void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
void VisitBinaryOperator(BinaryOperator *Node);
void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
void VisitAddrLabelExpr(AddrLabelExpr *Node);
void VisitTypesCompatibleExpr(TypesCompatibleExpr *Node);
// C++
void VisitCXXCastExpr(CXXCastExpr *Node);
void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
// ObjC
void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
void VisitObjCMessageExpr(ObjCMessageExpr* Node);
void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
};
}
//===----------------------------------------------------------------------===//
// Utilities
//===----------------------------------------------------------------------===//
void StmtDumper::DumpLocation(SourceLocation Loc) {
SourceLocation PhysLoc = SM->getPhysicalLoc(Loc);
// The general format we print out is filename:line:col, but we drop pieces
// that haven't changed since the last loc printed.
const char *Filename = SM->getSourceName(PhysLoc);
unsigned LineNo = SM->getLineNumber(PhysLoc);
if (strcmp(Filename, LastLocFilename) != 0) {
fprintf(stderr, "%s:%u:%u", Filename, LineNo, SM->getColumnNumber(PhysLoc));
LastLocFilename = Filename;
LastLocLine = LineNo;
} else if (LineNo != LastLocLine) {
fprintf(stderr, "line:%u:%u", LineNo, SM->getColumnNumber(PhysLoc));
LastLocLine = LineNo;
} else {
fprintf(stderr, "col:%u", SM->getColumnNumber(PhysLoc));
}
}
void StmtDumper::DumpSourceRange(const Stmt *Node) {
// Can't translate locations if a SourceManager isn't available.
if (SM == 0) return;
// TODO: If the parent expression is available, we can print a delta vs its
// location.
SourceRange R = Node->getSourceRange();
fprintf(stderr, " <");
DumpLocation(R.getBegin());
if (R.getBegin() != R.getEnd()) {
fprintf(stderr, ", ");
DumpLocation(R.getEnd());
}
fprintf(stderr, ">");
// <t2.c:123:421[blah], t2.c:412:321>
}
//===----------------------------------------------------------------------===//
// Stmt printing methods.
//===----------------------------------------------------------------------===//
void StmtDumper::VisitStmt(Stmt *Node) {
DumpStmt(Node);
}
void StmtDumper::DumpDeclarator(Decl *D) {
// FIXME: Need to complete/beautify this... this code simply shows the
// nodes are where they need to be.
if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
fprintf(F, "\"typedef %s %s\"",
localType->getUnderlyingType().getAsString().c_str(),
localType->getName());
} else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
fprintf(F, "\"");
// Emit storage class for vardecls.
if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
switch (V->getStorageClass()) {
default: assert(0 && "Unknown storage class!");
case VarDecl::None: break;
case VarDecl::Extern: fprintf(F, "extern "); break;
case VarDecl::Static: fprintf(F, "static "); break;
case VarDecl::Auto: fprintf(F, "auto "); break;
case VarDecl::Register: fprintf(F, "register "); break;
}
}
std::string Name = VD->getName();
VD->getType().getAsStringInternal(Name);
fprintf(F, "%s", Name.c_str());
// If this is a vardecl with an initializer, emit it.
if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
if (V->getInit()) {
fprintf(F, " =\n");
DumpSubTree(V->getInit());
}
}
fprintf(F, "\"");
} else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
// print a free standing tag decl (e.g. "struct x;").
const char *tagname;
if (const IdentifierInfo *II = TD->getIdentifier())
tagname = II->getName();
else
tagname = "<anonymous>";
fprintf(F, "\"%s %s;\"", TD->getKindName(), tagname);
// FIXME: print tag bodies.
} else {
assert(0 && "Unexpected decl");
}
}
void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
DumpStmt(Node);
fprintf(F,"\n");
for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
++IndentLevel;
Indent();
fprintf(F, "%p ", (void*) D);
DumpDeclarator(D);
if (D->getNextDeclarator())
fprintf(F,"\n");
--IndentLevel;
}
}
void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
DumpStmt(Node);
fprintf(F, " '%s'\n", Node->getName());
}
void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
DumpStmt(Node);
fprintf(F, " '%s':%p", Node->getLabel()->getName(), (void*)Node->getLabel());
}
//===----------------------------------------------------------------------===//
// Expr printing methods.
//===----------------------------------------------------------------------===//
void StmtDumper::VisitExpr(Expr *Node) {
DumpExpr(Node);
}
void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
DumpExpr(Node);
fprintf(F, " ");
switch (Node->getDecl()->getKind()) {
case Decl::Function: fprintf(F,"FunctionDecl"); break;
case Decl::Var: fprintf(F,"Var"); break;
case Decl::ParmVar: fprintf(F,"ParmVar"); break;
case Decl::EnumConstant: fprintf(F,"EnumConstant"); break;
case Decl::Typedef: fprintf(F,"Typedef"); break;
case Decl::Struct: fprintf(F,"Struct"); break;
case Decl::Union: fprintf(F,"Union"); break;
case Decl::Class: fprintf(F,"Class"); break;
case Decl::Enum: fprintf(F,"Enum"); break;
case Decl::ObjCInterface: fprintf(F,"ObjCInterface"); break;
case Decl::ObjCClass: fprintf(F,"ObjCClass"); break;
default: fprintf(F,"Decl"); break;
}
fprintf(F, "='%s' %p", Node->getDecl()->getName(), (void*)Node->getDecl());
}
void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
DumpExpr(Node->getBase());
fprintf(F, " ObjCIvarRefExpr");
fprintf(F, "='%s' %p", Node->getDecl()->getName(), (void*)Node->getDecl());
}
void StmtDumper::VisitPreDefinedExpr(PreDefinedExpr *Node) {
DumpExpr(Node);
switch (Node->getIdentType()) {
default:
assert(0 && "unknown case");
case PreDefinedExpr::Func:
fprintf(F, " __func__");
break;
case PreDefinedExpr::Function:
fprintf(F, " __FUNCTION__");
break;
case PreDefinedExpr::PrettyFunction:
fprintf(F, " __PRETTY_FUNCTION__");
break;
}
}
void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
DumpExpr(Node);
fprintf(F, " %d", Node->getValue());
}
void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
DumpExpr(Node);
bool isSigned = Node->getType()->isSignedIntegerType();
fprintf(F, " %s", Node->getValue().toString(10, isSigned).c_str());
}
void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
DumpExpr(Node);
fprintf(F, " %f", Node->getValueAsDouble());
}
void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
DumpExpr(Str);
// FIXME: this doesn't print wstrings right.
fprintf(F, " %s\"", Str->isWide() ? "L" : "");
for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
switch (char C = Str->getStrData()[i]) {
default:
if (isprint(C))
fputc(C, F);
else
fprintf(F, "\\%03o", C);
break;
// Handle some common ones to make dumps prettier.
case '\\': fprintf(F, "\\\\"); break;
case '"': fprintf(F, "\\\""); break;
case '\n': fprintf(F, "\\n"); break;
case '\t': fprintf(F, "\\t"); break;
case '\a': fprintf(F, "\\a"); break;
case '\b': fprintf(F, "\\b"); break;
}
}
fprintf(F, "\"");
}
void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
DumpExpr(Node);
fprintf(F, " %s '%s'", Node->isPostfix() ? "postfix" : "prefix",
UnaryOperator::getOpcodeStr(Node->getOpcode()));
}
void StmtDumper::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
DumpExpr(Node);
fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof");
DumpType(Node->getArgumentType());
}
void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
DumpExpr(Node);
fprintf(F, " %s%s %p", Node->isArrow() ? "->" : ".",
Node->getMemberDecl()->getName(), (void*)Node->getMemberDecl());
}
void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
DumpExpr(Node);
fprintf(F, " %s", Node->getAccessor().getName());
}
void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
DumpExpr(Node);
fprintf(F, " '%s'", BinaryOperator::getOpcodeStr(Node->getOpcode()));
}
void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
DumpExpr(Node);
fprintf(F, " '%s' ComputeTy=",
BinaryOperator::getOpcodeStr(Node->getOpcode()));
DumpType(Node->getComputationType());
}
// GNU extensions.
void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
DumpExpr(Node);
fprintf(F, " %s %p", Node->getLabel()->getName(), (void*)Node->getLabel());
}
void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
DumpExpr(Node);
fprintf(F, " ");
DumpType(Node->getArgType1());
fprintf(F, " ");
DumpType(Node->getArgType2());
}
//===----------------------------------------------------------------------===//
// C++ Expressions
//===----------------------------------------------------------------------===//
void StmtDumper::VisitCXXCastExpr(CXXCastExpr *Node) {
DumpExpr(Node);
fprintf(F, " %s", CXXCastExpr::getOpcodeStr(Node->getOpcode()));
}
void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
DumpExpr(Node);
fprintf(F, " %s", Node->getValue() ? "true" : "false");
}
//===----------------------------------------------------------------------===//
// Obj-C Expressions
//===----------------------------------------------------------------------===//
void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
DumpExpr(Node);
fprintf(F, " selector=%s", Node->getSelector().getName().c_str());
IdentifierInfo* clsName = Node->getClassName();
if (clsName) fprintf(F, " class=%s", clsName->getName());
}
void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
DumpExpr(Node);
fprintf(F, " ");
DumpType(Node->getEncodedType());
}
void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
DumpExpr(Node);
fprintf(F, " ");
Selector selector = Node->getSelector();
fprintf(F, "%s", selector.getName().c_str());
}
void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
DumpExpr(Node);
fprintf(F, " ");
fprintf(F, "%s", Node->getProtocol()->getName());
}
//===----------------------------------------------------------------------===//
// Stmt method implementations
//===----------------------------------------------------------------------===//
/// dump - This does a local dump of the specified AST fragment. It dumps the
/// specified node and a few nodes underneath it, but not the whole subtree.
/// This is useful in a debugger.
void Stmt::dump(SourceManager &SM) const {
StmtDumper P(&SM, stderr, 4);
P.DumpSubTree(const_cast<Stmt*>(this));
fprintf(stderr, "\n");
}
/// dump - This does a local dump of the specified AST fragment. It dumps the
/// specified node and a few nodes underneath it, but not the whole subtree.
/// This is useful in a debugger.
void Stmt::dump() const {
StmtDumper P(0, stderr, 4);
P.DumpSubTree(const_cast<Stmt*>(this));
fprintf(stderr, "\n");
}
/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
void Stmt::dumpAll(SourceManager &SM) const {
StmtDumper P(&SM, stderr, ~0U);
P.DumpSubTree(const_cast<Stmt*>(this));
fprintf(stderr, "\n");
}
/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
void Stmt::dumpAll() const {
StmtDumper P(0, stderr, ~0U);
P.DumpSubTree(const_cast<Stmt*>(this));
fprintf(stderr, "\n");
}
|