1 //===--- StmtDumper.cpp - Dumping implementation for Stmt ASTs ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "clang/AST/StmtVisitor.h"
16 #include "clang/AST/DeclObjC.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/PrettyPrinter.h"
19 #include "clang/Basic/SourceManager.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace clang;
23 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
28 class StmtDumper : public StmtVisitor<StmtDumper> {
30 llvm::raw_ostream &OS;
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
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;
44 StmtDumper(SourceManager *sm, llvm::raw_ostream &os, unsigned maxDepth)
45 : SM(sm), OS(os), IndentLevel(0-1), MaxDepth(maxDepth) {
50 void DumpSubTree(Stmt *S) {
51 // Prune the recursion if not using dump all.
52 if (MaxDepth == 0) return;
56 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
61 // Print out children.
62 Stmt::child_range CI = S->children();
78 void DumpDeclarator(Decl *D);
81 for (int i = 0, e = IndentLevel; i < e; ++i)
85 void DumpType(QualType T) {
86 SplitQualType T_split = T.split();
87 OS << "'" << QualType::getAsString(T_split) << "'";
90 // If the type is sugared, also dump a (shallow) desugared type.
91 SplitQualType D_split = T.getSplitDesugaredType();
92 if (T_split != D_split)
93 OS << ":'" << QualType::getAsString(D_split) << "'";
96 void DumpDeclRef(Decl *node);
97 void DumpStmt(const Stmt *Node) {
99 OS << "(" << Node->getStmtClassName()
100 << " " << (void*)Node;
101 DumpSourceRange(Node);
103 void DumpValueKind(ExprValueKind K) {
105 case VK_RValue: break;
106 case VK_LValue: OS << " lvalue"; break;
107 case VK_XValue: OS << " xvalue"; break;
110 void DumpObjectKind(ExprObjectKind K) {
112 case OK_Ordinary: break;
113 case OK_BitField: OS << " bitfield"; break;
114 case OK_ObjCProperty: OS << " objcproperty"; break;
115 case OK_VectorComponent: OS << " vectorcomponent"; break;
118 void DumpExpr(const Expr *Node) {
121 DumpType(Node->getType());
122 DumpValueKind(Node->getValueKind());
123 DumpObjectKind(Node->getObjectKind());
125 void DumpSourceRange(const Stmt *Node);
126 void DumpLocation(SourceLocation Loc);
129 void VisitStmt(Stmt *Node);
130 void VisitDeclStmt(DeclStmt *Node);
131 void VisitLabelStmt(LabelStmt *Node);
132 void VisitGotoStmt(GotoStmt *Node);
135 void VisitExpr(Expr *Node);
136 void VisitCastExpr(CastExpr *Node);
137 void VisitDeclRefExpr(DeclRefExpr *Node);
138 void VisitPredefinedExpr(PredefinedExpr *Node);
139 void VisitCharacterLiteral(CharacterLiteral *Node);
140 void VisitIntegerLiteral(IntegerLiteral *Node);
141 void VisitFloatingLiteral(FloatingLiteral *Node);
142 void VisitStringLiteral(StringLiteral *Str);
143 void VisitUnaryOperator(UnaryOperator *Node);
144 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node);
145 void VisitMemberExpr(MemberExpr *Node);
146 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
147 void VisitBinaryOperator(BinaryOperator *Node);
148 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
149 void VisitAddrLabelExpr(AddrLabelExpr *Node);
150 void VisitBlockExpr(BlockExpr *Node);
153 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
154 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
155 void VisitCXXThisExpr(CXXThisExpr *Node);
156 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
157 void VisitCXXConstructExpr(CXXConstructExpr *Node);
158 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node);
159 void VisitExprWithCleanups(ExprWithCleanups *Node);
160 void VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node);
161 void DumpCXXTemporary(CXXTemporary *Temporary);
164 void VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node);
165 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
166 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
167 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
168 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
169 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
170 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
174 //===----------------------------------------------------------------------===//
176 //===----------------------------------------------------------------------===//
178 void StmtDumper::DumpLocation(SourceLocation Loc) {
179 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
181 // The general format we print out is filename:line:col, but we drop pieces
182 // that haven't changed since the last loc printed.
183 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
185 if (PLoc.isInvalid()) {
186 OS << "<invalid sloc>";
190 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
191 OS << PLoc.getFilename() << ':' << PLoc.getLine()
192 << ':' << PLoc.getColumn();
193 LastLocFilename = PLoc.getFilename();
194 LastLocLine = PLoc.getLine();
195 } else if (PLoc.getLine() != LastLocLine) {
196 OS << "line" << ':' << PLoc.getLine()
197 << ':' << PLoc.getColumn();
198 LastLocLine = PLoc.getLine();
200 OS << "col" << ':' << PLoc.getColumn();
204 void StmtDumper::DumpSourceRange(const Stmt *Node) {
205 // Can't translate locations if a SourceManager isn't available.
208 // TODO: If the parent expression is available, we can print a delta vs its
210 SourceRange R = Node->getSourceRange();
213 DumpLocation(R.getBegin());
214 if (R.getBegin() != R.getEnd()) {
216 DumpLocation(R.getEnd());
220 // <t2.c:123:421[blah], t2.c:412:321>
225 //===----------------------------------------------------------------------===//
226 // Stmt printing methods.
227 //===----------------------------------------------------------------------===//
229 void StmtDumper::VisitStmt(Stmt *Node) {
233 void StmtDumper::DumpDeclarator(Decl *D) {
234 // FIXME: Need to complete/beautify this... this code simply shows the
235 // nodes are where they need to be.
236 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
237 OS << "\"typedef " << localType->getUnderlyingType().getAsString()
238 << ' ' << localType << '"';
239 } else if (TypeAliasDecl *localType = dyn_cast<TypeAliasDecl>(D)) {
240 OS << "\"using " << localType << " = "
241 << localType->getUnderlyingType().getAsString() << '"';
242 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
244 // Emit storage class for vardecls.
245 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
246 if (V->getStorageClass() != SC_None)
247 OS << VarDecl::getStorageClassSpecifierString(V->getStorageClass())
251 std::string Name = VD->getNameAsString();
252 VD->getType().getAsStringInternal(Name,
253 PrintingPolicy(VD->getASTContext().getLangOptions()));
256 // If this is a vardecl with an initializer, emit it.
257 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
260 DumpSubTree(V->getInit());
264 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
265 // print a free standing tag decl (e.g. "struct x;").
267 if (const IdentifierInfo *II = TD->getIdentifier())
268 tagname = II->getNameStart();
270 tagname = "<anonymous>";
271 OS << '"' << TD->getKindName() << ' ' << tagname << ";\"";
272 // FIXME: print tag bodies.
273 } else if (UsingDirectiveDecl *UD = dyn_cast<UsingDirectiveDecl>(D)) {
274 // print using-directive decl (e.g. "using namespace x;")
276 if (const IdentifierInfo *II = UD->getNominatedNamespace()->getIdentifier())
277 ns = II->getNameStart();
280 OS << '"' << UD->getDeclKindName() << ns << ";\"";
281 } else if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
282 // print using decl (e.g. "using std::string;")
283 const char *tn = UD->isTypeName() ? "typename " : "";
284 OS << '"' << UD->getDeclKindName() << tn;
285 UD->getQualifier()->print(OS,
286 PrintingPolicy(UD->getASTContext().getLangOptions()));
288 } else if (LabelDecl *LD = dyn_cast<LabelDecl>(D)) {
289 OS << "label " << LD->getNameAsString();
290 } else if (StaticAssertDecl *SAD = dyn_cast<StaticAssertDecl>(D)) {
291 OS << "\"static_assert(\n";
292 DumpSubTree(SAD->getAssertExpr());
294 DumpSubTree(SAD->getMessage());
297 assert(0 && "Unexpected decl");
301 void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
304 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
309 OS << (void*) D << " ";
317 void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
319 OS << " '" << Node->getName() << "'";
322 void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
324 OS << " '" << Node->getLabel()->getName()
325 << "':" << (void*)Node->getLabel();
328 //===----------------------------------------------------------------------===//
329 // Expr printing methods.
330 //===----------------------------------------------------------------------===//
332 void StmtDumper::VisitExpr(Expr *Node) {
336 static void DumpBasePath(llvm::raw_ostream &OS, CastExpr *Node) {
337 if (Node->path_empty())
342 for (CastExpr::path_iterator
343 I = Node->path_begin(), E = Node->path_end(); I != E; ++I) {
344 const CXXBaseSpecifier *Base = *I;
348 const CXXRecordDecl *RD =
349 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
351 if (Base->isVirtual())
360 void StmtDumper::VisitCastExpr(CastExpr *Node) {
362 OS << " <" << Node->getCastKindName();
363 DumpBasePath(OS, Node);
367 void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
371 DumpDeclRef(Node->getDecl());
372 if (Node->getDecl() != Node->getFoundDecl()) {
374 DumpDeclRef(Node->getFoundDecl());
379 void StmtDumper::DumpDeclRef(Decl *d) {
380 OS << d->getDeclKindName() << ' ' << (void*) d;
382 if (NamedDecl *nd = dyn_cast<NamedDecl>(d)) {
384 nd->getDeclName().printName(OS);
388 if (ValueDecl *vd = dyn_cast<ValueDecl>(d)) {
389 OS << ' '; DumpType(vd->getType());
393 void StmtDumper::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
396 if (!Node->requiresADL()) OS << "no ";
397 OS << "ADL) = '" << Node->getName() << '\'';
399 UnresolvedLookupExpr::decls_iterator
400 I = Node->decls_begin(), E = Node->decls_end();
401 if (I == E) OS << " empty";
403 OS << " " << (void*) *I;
406 void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
409 OS << " " << Node->getDecl()->getDeclKindName()
410 << "Decl='" << Node->getDecl()
411 << "' " << (void*)Node->getDecl();
412 if (Node->isFreeIvar())
416 void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
418 switch (Node->getIdentType()) {
419 default: assert(0 && "unknown case");
420 case PredefinedExpr::Func: OS << " __func__"; break;
421 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
422 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
426 void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
428 OS << Node->getValue();
431 void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
434 bool isSigned = Node->getType()->isSignedIntegerType();
435 OS << " " << Node->getValue().toString(10, isSigned);
437 void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
439 OS << " " << Node->getValueAsApproximateDouble();
442 void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
444 // FIXME: this doesn't print wstrings right.
449 OS.write_escaped(Str->getString());
453 void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
455 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
456 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
458 void StmtDumper::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node) {
460 switch(Node->getKind()) {
471 if (Node->isArgumentType())
472 DumpType(Node->getArgumentType());
475 void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
477 OS << " " << (Node->isArrow() ? "->" : ".")
478 << Node->getMemberDecl() << ' '
479 << (void*)Node->getMemberDecl();
481 void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
483 OS << " " << Node->getAccessor().getNameStart();
485 void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
487 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
489 void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
491 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
492 << "' ComputeLHSTy=";
493 DumpType(Node->getComputationLHSType());
494 OS << " ComputeResultTy=";
495 DumpType(Node->getComputationResultType());
498 void StmtDumper::VisitBlockExpr(BlockExpr *Node) {
502 BlockDecl *block = Node->getBlockDecl();
503 if (block->capturesCXXThis()) {
504 OS << '\n'; Indent(); OS << "(capture this)";
506 for (BlockDecl::capture_iterator
507 i = block->capture_begin(), e = block->capture_end(); i != e; ++i) {
511 if (i->isByRef()) OS << "byref ";
512 if (i->isNested()) OS << "nested ";
513 DumpDeclRef(i->getVariable());
514 if (i->hasCopyExpr()) DumpSubTree(i->getCopyExpr());
519 DumpSubTree(block->getBody());
524 void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
526 OS << " " << Node->getLabel()->getName()
527 << " " << (void*)Node->getLabel();
530 //===----------------------------------------------------------------------===//
532 //===----------------------------------------------------------------------===//
534 void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
536 OS << " " << Node->getCastName()
537 << "<" << Node->getTypeAsWritten().getAsString() << ">"
538 << " <" << Node->getCastKindName();
539 DumpBasePath(OS, Node);
543 void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
545 OS << " " << (Node->getValue() ? "true" : "false");
548 void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
553 void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
555 OS << " functional cast to " << Node->getTypeAsWritten().getAsString();
558 void StmtDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
560 CXXConstructorDecl *Ctor = Node->getConstructor();
561 DumpType(Ctor->getType());
562 if (Node->isElidable())
564 if (Node->requiresZeroInitialization())
568 void StmtDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
571 DumpCXXTemporary(Node->getTemporary());
574 void StmtDumper::VisitExprWithCleanups(ExprWithCleanups *Node) {
577 for (unsigned i = 0, e = Node->getNumTemporaries(); i != e; ++i) {
580 DumpCXXTemporary(Node->getTemporary(i));
585 void StmtDumper::DumpCXXTemporary(CXXTemporary *Temporary) {
586 OS << "(CXXTemporary " << (void *)Temporary << ")";
589 //===----------------------------------------------------------------------===//
591 //===----------------------------------------------------------------------===//
593 void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
595 OS << " selector=" << Node->getSelector().getAsString();
596 switch (Node->getReceiverKind()) {
597 case ObjCMessageExpr::Instance:
600 case ObjCMessageExpr::Class:
602 DumpType(Node->getClassReceiver());
605 case ObjCMessageExpr::SuperInstance:
606 OS << " super (instance)";
609 case ObjCMessageExpr::SuperClass:
610 OS << " super (class)";
615 void StmtDumper::VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node) {
617 if (VarDecl *CatchParam = Node->getCatchParamDecl()) {
618 OS << " catch parm = ";
619 DumpDeclarator(CatchParam);
625 void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
628 DumpType(Node->getEncodedType());
631 void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
634 OS << " " << Node->getSelector().getAsString();
637 void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
640 OS << ' ' << Node->getProtocol();
643 void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
645 if (Node->isImplicitProperty()) {
646 OS << " Kind=MethodRef Getter=\"";
647 if (Node->getImplicitPropertyGetter())
648 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
652 OS << "\" Setter=\"";
653 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
654 OS << Setter->getSelector().getAsString();
659 OS << " Kind=PropertyRef Property=\"" << Node->getExplicitProperty() << '"';
662 if (Node->isSuperReceiver())
666 //===----------------------------------------------------------------------===//
667 // Stmt method implementations
668 //===----------------------------------------------------------------------===//
670 /// dump - This does a local dump of the specified AST fragment. It dumps the
671 /// specified node and a few nodes underneath it, but not the whole subtree.
672 /// This is useful in a debugger.
673 void Stmt::dump(SourceManager &SM) const {
674 dump(llvm::errs(), SM);
677 void Stmt::dump(llvm::raw_ostream &OS, SourceManager &SM) const {
678 StmtDumper P(&SM, OS, 4);
679 P.DumpSubTree(const_cast<Stmt*>(this));
683 /// dump - This does a local dump of the specified AST fragment. It dumps the
684 /// specified node and a few nodes underneath it, but not the whole subtree.
685 /// This is useful in a debugger.
686 void Stmt::dump() const {
687 StmtDumper P(0, llvm::errs(), 4);
688 P.DumpSubTree(const_cast<Stmt*>(this));
689 llvm::errs() << "\n";
692 /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
693 void Stmt::dumpAll(SourceManager &SM) const {
694 StmtDumper P(&SM, llvm::errs(), ~0U);
695 P.DumpSubTree(const_cast<Stmt*>(this));
696 llvm::errs() << "\n";
699 /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
700 void Stmt::dumpAll() const {
701 StmtDumper P(0, llvm::errs(), ~0U);
702 P.DumpSubTree(const_cast<Stmt*>(this));
703 llvm::errs() << "\n";