diff options
Diffstat (limited to 'lib/AST/StmtPrinter.cpp')
-rw-r--r-- | lib/AST/StmtPrinter.cpp | 137 |
1 files changed, 68 insertions, 69 deletions
diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp index 7a0d6d6349..a6f2b823fe 100644 --- a/lib/AST/StmtPrinter.cpp +++ b/lib/AST/StmtPrinter.cpp @@ -33,12 +33,12 @@ namespace { PrintingPolicy Policy; public: - StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper, + StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper, const PrintingPolicy &Policy, unsigned Indentation = 0) : OS(os), Context(C), IndentLevel(Indentation), Helper(helper), Policy(Policy) {} - + void PrintStmt(Stmt *S) { PrintStmt(S, Policy.Indentation); } @@ -63,29 +63,29 @@ namespace { void PrintRawDeclStmt(DeclStmt *S); void PrintRawIfStmt(IfStmt *If); void PrintRawCXXCatchStmt(CXXCatchStmt *Catch); - + void PrintExpr(Expr *E) { if (E) Visit(E); else OS << "<null expr>"; } - + llvm::raw_ostream &Indent(int Delta = 0) { for (int i = 0, e = IndentLevel+Delta; i < e; ++i) OS << " "; return OS; } - + bool PrintOffsetOfDesignator(Expr *E); void VisitUnaryOffsetOf(UnaryOperator *Node); - - void Visit(Stmt* S) { + + void Visit(Stmt* S) { if (Helper && Helper->handledStmt(S,OS)) return; else StmtVisitor<StmtPrinter>::Visit(S); } - + void VisitStmt(Stmt *Node); #define STMT(CLASS, PARENT) \ void Visit##CLASS(CLASS *Node); @@ -108,7 +108,7 @@ void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) { for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end(); I != E; ++I) PrintStmt(*I); - + Indent() << "}"; } @@ -119,7 +119,7 @@ void StmtPrinter::PrintRawDecl(Decl *D) { void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) { DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end(); llvm::SmallVector<Decl*, 2> Decls; - for ( ; Begin != End; ++Begin) + for ( ; Begin != End; ++Begin) Decls.push_back(*Begin); Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel); @@ -149,7 +149,7 @@ void StmtPrinter::VisitCaseStmt(CaseStmt *Node) { PrintExpr(Node->getRHS()); } OS << ":\n"; - + PrintStmt(Node->getSubStmt(), 0); } @@ -167,7 +167,7 @@ void StmtPrinter::PrintRawIfStmt(IfStmt *If) { OS << "if ("; PrintExpr(If->getCond()); OS << ')'; - + if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) { OS << ' '; PrintRawCompoundStmt(CS); @@ -177,10 +177,10 @@ void StmtPrinter::PrintRawIfStmt(IfStmt *If) { PrintStmt(If->getThen()); if (If->getElse()) Indent(); } - + if (Stmt *Else = If->getElse()) { OS << "else"; - + if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) { OS << ' '; PrintRawCompoundStmt(CS); @@ -204,7 +204,7 @@ void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) { Indent() << "switch ("; PrintExpr(Node->getCond()); OS << ")"; - + // Pretty print compoundstmt bodies (very common). if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { OS << " "; @@ -237,7 +237,7 @@ void StmtPrinter::VisitDoStmt(DoStmt *Node) { PrintStmt(Node->getBody()); Indent(); } - + OS << "while ("; PrintExpr(Node->getCond()); OS << ");\n"; @@ -262,7 +262,7 @@ void StmtPrinter::VisitForStmt(ForStmt *Node) { PrintExpr(Node->getInc()); } OS << ") "; - + if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { PrintRawCompoundStmt(CS); OS << "\n"; @@ -281,7 +281,7 @@ void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) { OS << " in "; PrintExpr(Node->getCollection()); OS << ") "; - + if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { PrintRawCompoundStmt(CS); OS << "\n"; @@ -322,63 +322,63 @@ void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { void StmtPrinter::VisitAsmStmt(AsmStmt *Node) { Indent() << "asm "; - + if (Node->isVolatile()) OS << "volatile "; - + OS << "("; VisitStringLiteral(Node->getAsmString()); - + // Outputs if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 || Node->getNumClobbers() != 0) OS << " : "; - + for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) { if (i != 0) OS << ", "; - + if (!Node->getOutputName(i).empty()) { OS << '['; OS << Node->getOutputName(i); OS << "] "; } - + VisitStringLiteral(Node->getOutputConstraintLiteral(i)); OS << " "; Visit(Node->getOutputExpr(i)); } - + // Inputs if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0) OS << " : "; - + for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) { if (i != 0) OS << ", "; - + if (!Node->getInputName(i).empty()) { OS << '['; OS << Node->getInputName(i); OS << "] "; } - + VisitStringLiteral(Node->getInputConstraintLiteral(i)); OS << " "; Visit(Node->getInputExpr(i)); } - + // Clobbers if (Node->getNumClobbers() != 0) OS << " : "; - + for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) { if (i != 0) OS << ", "; - + VisitStringLiteral(Node->getClobber(i)); } - + OS << ");\n"; } @@ -388,11 +388,11 @@ void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) { PrintRawCompoundStmt(TS); OS << "\n"; } - - for (ObjCAtCatchStmt *catchStmt = + + for (ObjCAtCatchStmt *catchStmt = static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts()); - catchStmt; - catchStmt = + catchStmt; + catchStmt = static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) { Indent() << "@catch("; if (catchStmt->getCatchParamDecl()) { @@ -400,19 +400,18 @@ void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) { PrintRawDecl(DS); } OS << ")"; - if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) - { - PrintRawCompoundStmt(CS); - OS << "\n"; - } + if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) { + PrintRawCompoundStmt(CS); + OS << "\n"; + } } - - if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>( - Node->getFinallyStmt())) { + + if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>( + Node->getFinallyStmt())) { Indent() << "@finally"; PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody())); OS << "\n"; - } + } } void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) { @@ -458,7 +457,7 @@ void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) { void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) { Indent() << "try "; PrintRawCompoundStmt(Node->getTryBlock()); - for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) { + for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) { OS << " "; PrintRawCXXCatchStmt(Node->getHandler(i)); } @@ -477,14 +476,14 @@ void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { OS << Node->getDecl()->getNameAsString(); } -void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) { +void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) { NamedDecl *D = Node->getDecl(); Node->getQualifier()->print(OS, Policy); OS << D->getNameAsString(); } -void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) { +void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) { Node->getQualifier()->print(OS, Policy); OS << Node->getDeclName().getAsString(); } @@ -523,7 +522,7 @@ void StmtPrinter::VisitObjCImplicitSetterGetterRefExpr( } if (Node->getGetterMethod()) OS << Node->getGetterMethod()->getNameAsString(); - + } void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { @@ -594,7 +593,7 @@ void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { bool isSigned = Node->getType()->isSignedIntegerType(); OS << Node->getValue().toString(10, isSigned); - + // Emit suffixes. Integer literals are always a builtin integer type. switch (Node->getType()->getAsBuiltinType()->getKind()) { default: assert(0 && "Unexpected type for integer literal!"); @@ -623,7 +622,7 @@ void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { // FIXME: this doesn't print wstrings right. for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) { unsigned char Char = Str->getStrData()[i]; - + switch (Char) { default: if (isprint(Char)) @@ -653,7 +652,7 @@ void StmtPrinter::VisitParenExpr(ParenExpr *Node) { void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { if (!Node->isPostfix()) { OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); - + // Print a space if this is an "identifier operator" like __real, or if // it might be concatenated incorrectly like '+'. switch (Node->getOpcode()) { @@ -671,7 +670,7 @@ void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { } } PrintExpr(Node->getSubExpr()); - + if (Node->isPostfix()) OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); } @@ -741,7 +740,7 @@ void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { Qualifier->print(OS, Policy); OS << Node->getMemberDecl()->getNameAsString(); - + if (Node->hasExplicitTemplateArgumentList()) OS << TemplateSpecializationType::PrintTemplateArgumentList( Node->getTemplateArgs(), @@ -788,7 +787,7 @@ void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { } void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { PrintExpr(Node->getCond()); - + if (Node->getLHS()) { OS << " ? "; PrintExpr(Node->getLHS()); @@ -797,7 +796,7 @@ void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { else { // Handle GCC extension where LHS can be NULL. OS << " ?: "; } - + PrintExpr(Node->getRHS()); } @@ -884,7 +883,7 @@ void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { } else { PrintExpr(Node->getArrayRangeStart(*D)); OS << " ... "; - PrintExpr(Node->getArrayRangeEnd(*D)); + PrintExpr(Node->getArrayRangeEnd(*D)); } OS << "]"; } @@ -1036,7 +1035,7 @@ void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { OS << Node->getType().getAsString(); OS << "("; for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), - ArgEnd = Node->arg_end(); + ArgEnd = Node->arg_end(); Arg != ArgEnd; ++Arg) { if (Arg != Node->arg_begin()) OS << ", "; @@ -1113,7 +1112,7 @@ void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { OS << '.'; if (E->getQualifier()) E->getQualifier()->print(OS, Policy); - + std::string TypeS; E->getDestroyedType().getAsStringInternal(TypeS, Policy); OS << TypeS; @@ -1132,13 +1131,13 @@ void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { PrintExpr(E->getSubExpr()); } -void +void StmtPrinter::VisitCXXUnresolvedConstructExpr( CXXUnresolvedConstructExpr *Node) { OS << Node->getTypeAsWritten().getAsString(); OS << "("; for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), - ArgEnd = Node->arg_end(); + ArgEnd = Node->arg_end(); Arg != ArgEnd; ++Arg) { if (Arg != Node->arg_begin()) OS << ", "; @@ -1155,9 +1154,9 @@ void StmtPrinter::VisitCXXUnresolvedMemberExpr(CXXUnresolvedMemberExpr *Node) { else if (Node->hasExplicitTemplateArgumentList()) // FIXME: Track use of "template" keyword explicitly? OS << "template "; - + OS << Node->getMember().getAsString(); - + if (Node->hasExplicitTemplateArgumentList()) { OS << TemplateSpecializationType::PrintTemplateArgumentList( Node->getTemplateArgs(), @@ -1192,7 +1191,7 @@ void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { << E->getQueriedType().getAsString() << ")"; } -// Obj-C +// Obj-C void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { OS << "@"; @@ -1230,7 +1229,7 @@ void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { OS << ":"; } else OS << ", "; // Handle variadic methods. - + PrintExpr(Mess->getArg(i)); } } @@ -1244,9 +1243,9 @@ void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) { void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { BlockDecl *BD = Node->getBlockDecl(); OS << "^"; - + const FunctionType *AFT = Node->getFunctionType(); - + if (isa<FunctionNoProtoType>(AFT)) { OS << "()"; } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { @@ -1259,7 +1258,7 @@ void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { (*AI)->getType().getAsStringInternal(ParamStr, Policy); OS << ParamStr; } - + const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); if (FT->isVariadic()) { if (!BD->param_empty()) OS << ", "; @@ -1294,7 +1293,7 @@ void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context, dump(Context.getSourceManager()); return; } - + StmtPrinter P(OS, Context, Helper, Policy, Indentation); P.Visit(const_cast<Stmt*>(this)); } |