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> {
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, 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_ObjCSubscript: OS << " objcsubscript"; break;
116 case OK_VectorComponent: OS << " vectorcomponent"; break;
119 void DumpExpr(const Expr *Node) {
122 DumpType(Node->getType());
123 DumpValueKind(Node->getValueKind());
124 DumpObjectKind(Node->getObjectKind());
126 void DumpSourceRange(const Stmt *Node);
127 void DumpLocation(SourceLocation Loc);
130 void VisitStmt(Stmt *Node);
131 void VisitDeclStmt(DeclStmt *Node);
132 void VisitLabelStmt(LabelStmt *Node);
133 void VisitGotoStmt(GotoStmt *Node);
136 void VisitExpr(Expr *Node);
137 void VisitCastExpr(CastExpr *Node);
138 void VisitDeclRefExpr(DeclRefExpr *Node);
139 void VisitPredefinedExpr(PredefinedExpr *Node);
140 void VisitCharacterLiteral(CharacterLiteral *Node);
141 void VisitIntegerLiteral(IntegerLiteral *Node);
142 void VisitFloatingLiteral(FloatingLiteral *Node);
143 void VisitStringLiteral(StringLiteral *Str);
144 void VisitUnaryOperator(UnaryOperator *Node);
145 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node);
146 void VisitMemberExpr(MemberExpr *Node);
147 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
148 void VisitBinaryOperator(BinaryOperator *Node);
149 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
150 void VisitAddrLabelExpr(AddrLabelExpr *Node);
151 void VisitBlockExpr(BlockExpr *Node);
152 void VisitOpaqueValueExpr(OpaqueValueExpr *Node);
155 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
156 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
157 void VisitCXXThisExpr(CXXThisExpr *Node);
158 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
159 void VisitCXXConstructExpr(CXXConstructExpr *Node);
160 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node);
161 void VisitExprWithCleanups(ExprWithCleanups *Node);
162 void VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node);
163 void DumpCXXTemporary(CXXTemporary *Temporary);
166 void VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node);
167 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
168 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
169 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
170 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
171 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
172 void VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node);
173 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
174 void VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node);
178 //===----------------------------------------------------------------------===//
180 //===----------------------------------------------------------------------===//
182 void StmtDumper::DumpLocation(SourceLocation Loc) {
183 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
185 // The general format we print out is filename:line:col, but we drop pieces
186 // that haven't changed since the last loc printed.
187 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
189 if (PLoc.isInvalid()) {
190 OS << "<invalid sloc>";
194 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
195 OS << PLoc.getFilename() << ':' << PLoc.getLine()
196 << ':' << PLoc.getColumn();
197 LastLocFilename = PLoc.getFilename();
198 LastLocLine = PLoc.getLine();
199 } else if (PLoc.getLine() != LastLocLine) {
200 OS << "line" << ':' << PLoc.getLine()
201 << ':' << PLoc.getColumn();
202 LastLocLine = PLoc.getLine();
204 OS << "col" << ':' << PLoc.getColumn();
208 void StmtDumper::DumpSourceRange(const Stmt *Node) {
209 // Can't translate locations if a SourceManager isn't available.
212 // TODO: If the parent expression is available, we can print a delta vs its
214 SourceRange R = Node->getSourceRange();
217 DumpLocation(R.getBegin());
218 if (R.getBegin() != R.getEnd()) {
220 DumpLocation(R.getEnd());
224 // <t2.c:123:421[blah], t2.c:412:321>
229 //===----------------------------------------------------------------------===//
230 // Stmt printing methods.
231 //===----------------------------------------------------------------------===//
233 void StmtDumper::VisitStmt(Stmt *Node) {
237 void StmtDumper::DumpDeclarator(Decl *D) {
238 // FIXME: Need to complete/beautify this... this code simply shows the
239 // nodes are where they need to be.
240 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
241 OS << "\"typedef " << localType->getUnderlyingType().getAsString()
242 << ' ' << *localType << '"';
243 } else if (TypeAliasDecl *localType = dyn_cast<TypeAliasDecl>(D)) {
244 OS << "\"using " << *localType << " = "
245 << localType->getUnderlyingType().getAsString() << '"';
246 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
248 // Emit storage class for vardecls.
249 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
250 if (V->getStorageClass() != SC_None)
251 OS << VarDecl::getStorageClassSpecifierString(V->getStorageClass())
255 std::string Name = VD->getNameAsString();
256 VD->getType().getAsStringInternal(Name,
257 PrintingPolicy(VD->getASTContext().getLangOpts()));
260 // If this is a vardecl with an initializer, emit it.
261 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
264 DumpSubTree(V->getInit());
268 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
269 // print a free standing tag decl (e.g. "struct x;").
271 if (const IdentifierInfo *II = TD->getIdentifier())
272 tagname = II->getNameStart();
274 tagname = "<anonymous>";
275 OS << '"' << TD->getKindName() << ' ' << tagname << ";\"";
276 // FIXME: print tag bodies.
277 } else if (UsingDirectiveDecl *UD = dyn_cast<UsingDirectiveDecl>(D)) {
278 // print using-directive decl (e.g. "using namespace x;")
280 if (const IdentifierInfo *II = UD->getNominatedNamespace()->getIdentifier())
281 ns = II->getNameStart();
284 OS << '"' << UD->getDeclKindName() << ns << ";\"";
285 } else if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
286 // print using decl (e.g. "using std::string;")
287 const char *tn = UD->isTypeName() ? "typename " : "";
288 OS << '"' << UD->getDeclKindName() << tn;
289 UD->getQualifier()->print(OS,
290 PrintingPolicy(UD->getASTContext().getLangOpts()));
292 } else if (LabelDecl *LD = dyn_cast<LabelDecl>(D)) {
293 OS << "label " << *LD;
294 } else if (StaticAssertDecl *SAD = dyn_cast<StaticAssertDecl>(D)) {
295 OS << "\"static_assert(\n";
296 DumpSubTree(SAD->getAssertExpr());
298 DumpSubTree(SAD->getMessage());
301 llvm_unreachable("Unexpected decl");
305 void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
308 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
313 OS << (void*) D << " ";
321 void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
323 OS << " '" << Node->getName() << "'";
326 void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
328 OS << " '" << Node->getLabel()->getName()
329 << "':" << (void*)Node->getLabel();
332 //===----------------------------------------------------------------------===//
333 // Expr printing methods.
334 //===----------------------------------------------------------------------===//
336 void StmtDumper::VisitExpr(Expr *Node) {
340 static void DumpBasePath(raw_ostream &OS, CastExpr *Node) {
341 if (Node->path_empty())
346 for (CastExpr::path_iterator
347 I = Node->path_begin(), E = Node->path_end(); I != E; ++I) {
348 const CXXBaseSpecifier *Base = *I;
352 const CXXRecordDecl *RD =
353 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
355 if (Base->isVirtual())
364 void StmtDumper::VisitCastExpr(CastExpr *Node) {
366 OS << " <" << Node->getCastKindName();
367 DumpBasePath(OS, Node);
371 void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
375 DumpDeclRef(Node->getDecl());
376 if (Node->getDecl() != Node->getFoundDecl()) {
378 DumpDeclRef(Node->getFoundDecl());
383 void StmtDumper::DumpDeclRef(Decl *d) {
384 OS << d->getDeclKindName() << ' ' << (void*) d;
386 if (NamedDecl *nd = dyn_cast<NamedDecl>(d)) {
388 nd->getDeclName().printName(OS);
392 if (ValueDecl *vd = dyn_cast<ValueDecl>(d)) {
393 OS << ' '; DumpType(vd->getType());
397 void StmtDumper::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
400 if (!Node->requiresADL()) OS << "no ";
401 OS << "ADL) = '" << Node->getName() << '\'';
403 UnresolvedLookupExpr::decls_iterator
404 I = Node->decls_begin(), E = Node->decls_end();
405 if (I == E) OS << " empty";
407 OS << " " << (void*) *I;
410 void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
413 OS << " " << Node->getDecl()->getDeclKindName()
414 << "Decl='" << *Node->getDecl()
415 << "' " << (void*)Node->getDecl();
416 if (Node->isFreeIvar())
420 void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
422 switch (Node->getIdentType()) {
423 default: llvm_unreachable("unknown case");
424 case PredefinedExpr::Func: OS << " __func__"; break;
425 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
426 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
430 void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
432 OS << " " << Node->getValue();
435 void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
438 bool isSigned = Node->getType()->isSignedIntegerType();
439 OS << " " << Node->getValue().toString(10, isSigned);
441 void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
443 OS << " " << Node->getValueAsApproximateDouble();
446 void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
448 // FIXME: this doesn't print wstrings right.
450 switch (Str->getKind()) {
451 case StringLiteral::Ascii: break; // No prefix
452 case StringLiteral::Wide: OS << 'L'; break;
453 case StringLiteral::UTF8: OS << "u8"; break;
454 case StringLiteral::UTF16: OS << 'u'; break;
455 case StringLiteral::UTF32: OS << 'U'; break;
458 OS.write_escaped(Str->getString());
462 void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
464 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
465 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
467 void StmtDumper::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node) {
469 switch(Node->getKind()) {
480 if (Node->isArgumentType())
481 DumpType(Node->getArgumentType());
484 void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
486 OS << " " << (Node->isArrow() ? "->" : ".")
487 << *Node->getMemberDecl() << ' '
488 << (void*)Node->getMemberDecl();
490 void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
492 OS << " " << Node->getAccessor().getNameStart();
494 void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
496 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
498 void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
500 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
501 << "' ComputeLHSTy=";
502 DumpType(Node->getComputationLHSType());
503 OS << " ComputeResultTy=";
504 DumpType(Node->getComputationResultType());
507 void StmtDumper::VisitBlockExpr(BlockExpr *Node) {
510 BlockDecl *block = Node->getBlockDecl();
511 OS << " decl=" << block;
514 if (block->capturesCXXThis()) {
515 OS << '\n'; Indent(); OS << "(capture this)";
517 for (BlockDecl::capture_iterator
518 i = block->capture_begin(), e = block->capture_end(); i != e; ++i) {
522 if (i->isByRef()) OS << "byref ";
523 if (i->isNested()) OS << "nested ";
524 if (i->getVariable())
525 DumpDeclRef(i->getVariable());
526 if (i->hasCopyExpr()) DumpSubTree(i->getCopyExpr());
532 DumpSubTree(block->getBody());
535 void StmtDumper::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
538 if (Expr *Source = Node->getSourceExpr()) {
546 void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
548 OS << " " << Node->getLabel()->getName()
549 << " " << (void*)Node->getLabel();
552 //===----------------------------------------------------------------------===//
554 //===----------------------------------------------------------------------===//
556 void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
558 OS << " " << Node->getCastName()
559 << "<" << Node->getTypeAsWritten().getAsString() << ">"
560 << " <" << Node->getCastKindName();
561 DumpBasePath(OS, Node);
565 void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
567 OS << " " << (Node->getValue() ? "true" : "false");
570 void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
575 void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
577 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
578 << " <" << Node->getCastKindName() << ">";
581 void StmtDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
583 CXXConstructorDecl *Ctor = Node->getConstructor();
584 DumpType(Ctor->getType());
585 if (Node->isElidable())
587 if (Node->requiresZeroInitialization())
591 void StmtDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
594 DumpCXXTemporary(Node->getTemporary());
597 void StmtDumper::VisitExprWithCleanups(ExprWithCleanups *Node) {
600 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i) {
604 DumpDeclRef(Node->getObject(i));
610 void StmtDumper::DumpCXXTemporary(CXXTemporary *Temporary) {
611 OS << "(CXXTemporary " << (void *)Temporary << ")";
614 //===----------------------------------------------------------------------===//
616 //===----------------------------------------------------------------------===//
618 void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
620 OS << " selector=" << Node->getSelector().getAsString();
621 switch (Node->getReceiverKind()) {
622 case ObjCMessageExpr::Instance:
625 case ObjCMessageExpr::Class:
627 DumpType(Node->getClassReceiver());
630 case ObjCMessageExpr::SuperInstance:
631 OS << " super (instance)";
634 case ObjCMessageExpr::SuperClass:
635 OS << " super (class)";
640 void StmtDumper::VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node) {
642 if (VarDecl *CatchParam = Node->getCatchParamDecl()) {
643 OS << " catch parm = ";
644 DumpDeclarator(CatchParam);
650 void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
653 DumpType(Node->getEncodedType());
656 void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
659 OS << " " << Node->getSelector().getAsString();
662 void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
665 OS << ' ' <<* Node->getProtocol();
668 void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
670 if (Node->isImplicitProperty()) {
671 OS << " Kind=MethodRef Getter=\"";
672 if (Node->getImplicitPropertyGetter())
673 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
677 OS << "\" Setter=\"";
678 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
679 OS << Setter->getSelector().getAsString();
684 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
687 if (Node->isSuperReceiver())
691 if (Node->isMessagingGetter() && Node->isMessagingSetter())
692 OS << "Getter&Setter";
693 else if (Node->isMessagingGetter())
695 else if (Node->isMessagingSetter())
699 void StmtDumper::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
701 if (Node->isArraySubscriptRefExpr())
702 OS << " Kind=ArraySubscript GetterForArray=\"";
704 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
705 if (Node->getAtIndexMethodDecl())
706 OS << Node->getAtIndexMethodDecl()->getSelector().getAsString();
710 if (Node->isArraySubscriptRefExpr())
711 OS << "\" SetterForArray=\"";
713 OS << "\" SetterForDictionary=\"";
714 if (Node->setAtIndexMethodDecl())
715 OS << Node->setAtIndexMethodDecl()->getSelector().getAsString();
720 void StmtDumper::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
722 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
725 //===----------------------------------------------------------------------===//
726 // Stmt method implementations
727 //===----------------------------------------------------------------------===//
729 /// dump - This does a local dump of the specified AST fragment. It dumps the
730 /// specified node and a few nodes underneath it, but not the whole subtree.
731 /// This is useful in a debugger.
732 void Stmt::dump(SourceManager &SM) const {
733 dump(llvm::errs(), SM);
736 void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
737 StmtDumper P(&SM, OS, 4);
738 P.DumpSubTree(const_cast<Stmt*>(this));
742 /// dump - This does a local dump of the specified AST fragment. It dumps the
743 /// specified node and a few nodes underneath it, but not the whole subtree.
744 /// This is useful in a debugger.
745 void Stmt::dump() const {
746 StmtDumper P(0, llvm::errs(), 4);
747 P.DumpSubTree(const_cast<Stmt*>(this));
748 llvm::errs() << "\n";
751 /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
752 void Stmt::dumpAll(SourceManager &SM) const {
753 StmtDumper P(&SM, llvm::errs(), ~0U);
754 P.DumpSubTree(const_cast<Stmt*>(this));
755 llvm::errs() << "\n";
758 /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
759 void Stmt::dumpAll() const {
760 StmtDumper P(0, llvm::errs(), ~0U);
761 P.DumpSubTree(const_cast<Stmt*>(this));
762 llvm::errs() << "\n";