CFG.cpp revision 208600
190075Sobrien//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===// 2169689Skan// 3169689Skan// The LLVM Compiler Infrastructure 490075Sobrien// 590075Sobrien// This file is distributed under the University of Illinois Open Source 690075Sobrien// License. See LICENSE.TXT for details. 790075Sobrien// 890075Sobrien//===----------------------------------------------------------------------===// 990075Sobrien// 1090075Sobrien// This file defines the CFG and CFGBuilder classes for representing and 1190075Sobrien// building Control-Flow Graphs (CFGs) from ASTs. 1290075Sobrien// 1390075Sobrien//===----------------------------------------------------------------------===// 1490075Sobrien 1590075Sobrien#include "clang/Analysis/Support/SaveAndRestore.h" 1690075Sobrien#include "clang/Analysis/CFG.h" 17169689Skan#include "clang/AST/DeclCXX.h" 1890075Sobrien#include "clang/AST/StmtVisitor.h" 1990075Sobrien#include "clang/AST/PrettyPrinter.h" 2090075Sobrien#include "llvm/Support/GraphWriter.h" 2190075Sobrien#include "llvm/Support/Allocator.h" 2290075Sobrien#include "llvm/Support/Format.h" 2390075Sobrien#include "llvm/ADT/DenseMap.h" 2490075Sobrien#include "llvm/ADT/SmallPtrSet.h" 2590075Sobrien#include "llvm/ADT/OwningPtr.h" 2690075Sobrien 2790075Sobrienusing namespace clang; 2890075Sobrien 2990075Sobriennamespace { 3090075Sobrien 3190075Sobrienstatic SourceLocation GetEndLoc(Decl* D) { 3290075Sobrien if (VarDecl* VD = dyn_cast<VarDecl>(D)) 3390075Sobrien if (Expr* Ex = VD->getInit()) 34169689Skan return Ex->getSourceRange().getEnd(); 3590075Sobrien 36169689Skan return D->getLocation(); 37169689Skan} 3890075Sobrien 39169689Skanclass AddStmtChoice { 40169689Skanpublic: 4190075Sobrien enum Kind { NotAlwaysAdd = 0, 4290075Sobrien AlwaysAdd = 1, 4390075Sobrien AsLValueNotAlwaysAdd = 2, 4490075Sobrien AlwaysAddAsLValue = 3 }; 4590075Sobrien 4690075Sobrien AddStmtChoice(Kind kind) : k(kind) {} 4790075Sobrien 4890075Sobrien bool alwaysAdd() const { return (unsigned)k & 0x1; } 49132718Skan bool asLValue() const { return k >= AsLValueNotAlwaysAdd; } 5090075Sobrien 5190075Sobrienprivate: 5290075Sobrien Kind k; 5390075Sobrien}; 5490075Sobrien 5590075Sobrien/// CFGBuilder - This class implements CFG construction from an AST. 5690075Sobrien/// The builder is stateful: an instance of the builder should be used to only 5790075Sobrien/// construct a single CFG. 5890075Sobrien/// 59132718Skan/// Example usage: 6090075Sobrien/// 6190075Sobrien/// CFGBuilder builder; 6290075Sobrien/// CFG* cfg = builder.BuildAST(stmt1); 63117395Skan/// 64117395Skan/// CFG construction is done via a recursive walk of an AST. We actually parse 65117395Skan/// the AST in reverse order so that the successor of a basic block is 66169689Skan/// constructed prior to its predecessor. This allows us to nicely capture 67169689Skan/// implicit fall-throughs without extra basic blocks. 68169689Skan/// 69169689Skanclass CFGBuilder { 70169689Skan ASTContext *Context; 71169689Skan llvm::OwningPtr<CFG> cfg; 72169689Skan 73169689Skan CFGBlock* Block; 74169689Skan CFGBlock* Succ; 75169689Skan CFGBlock* ContinueTargetBlock; 76169689Skan CFGBlock* BreakTargetBlock; 77169689Skan CFGBlock* SwitchTerminatedBlock; 78169689Skan CFGBlock* DefaultCaseBlock; 79132718Skan CFGBlock* TryTerminatedBlock; 80132718Skan 81132718Skan // LabelMap records the mapping from Label expressions to their blocks. 82117395Skan typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy; 83169689Skan LabelMapTy LabelMap; 84169689Skan 85169689Skan // A list of blocks that end with a "goto" that must be backpatched to their 86169689Skan // resolved targets upon completion of CFG construction. 87117395Skan typedef std::vector<CFGBlock*> BackpatchBlocksTy; 88117395Skan BackpatchBlocksTy BackpatchBlocks; 89117395Skan 90117395Skan // A list of labels whose address has been taken (for indirect gotos). 9190075Sobrien typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy; 9290075Sobrien LabelSetTy AddressTakenLabels; 9390075Sobrien 9490075Sobrienpublic: 9590075Sobrien explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG 96169689Skan Block(NULL), Succ(NULL), 97169689Skan ContinueTargetBlock(NULL), BreakTargetBlock(NULL), 98169689Skan SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL), 99169689Skan TryTerminatedBlock(NULL) {} 100117395Skan 101117395Skan // buildCFG - Used by external clients to construct the CFG. 102117395Skan CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C, bool AddEHEdges, 103117395Skan bool AddScopes); 104117395Skan 105117395Skanprivate: 106117395Skan // Visitors to walk an AST and construct the CFG. 107117395Skan CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc); 108169689Skan CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc); 109169689Skan CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc); 110169689Skan CFGBlock *VisitBreakStmt(BreakStmt *B); 111169689Skan CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S); 112117395Skan CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T); 113117395Skan CFGBlock *VisitCXXTryStmt(CXXTryStmt *S); 114117395Skan CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc); 115117395Skan CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc); 11690075Sobrien CFGBlock *VisitCaseStmt(CaseStmt *C); 11790075Sobrien CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc); 11890075Sobrien CFGBlock *VisitCompoundStmt(CompoundStmt *C); 11990075Sobrien CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc); 12090075Sobrien CFGBlock *VisitContinueStmt(ContinueStmt *C); 12190075Sobrien CFGBlock *VisitDeclStmt(DeclStmt *DS); 12290075Sobrien CFGBlock *VisitDeclSubExpr(Decl* D); 12390075Sobrien CFGBlock *VisitDefaultStmt(DefaultStmt *D); 12490075Sobrien CFGBlock *VisitDoStmt(DoStmt *D); 12590075Sobrien CFGBlock *VisitForStmt(ForStmt *F); 12690075Sobrien CFGBlock *VisitGotoStmt(GotoStmt* G); 12790075Sobrien CFGBlock *VisitIfStmt(IfStmt *I); 12890075Sobrien CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I); 12990075Sobrien CFGBlock *VisitLabelStmt(LabelStmt *L); 13090075Sobrien CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc); 13190075Sobrien CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); 13290075Sobrien CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); 13390075Sobrien CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); 13490075Sobrien CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S); 13590075Sobrien CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); 13690075Sobrien CFGBlock *VisitReturnStmt(ReturnStmt* R); 13790075Sobrien CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc); 13890075Sobrien CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc); 13990075Sobrien CFGBlock *VisitSwitchStmt(SwitchStmt *S); 140117395Skan CFGBlock *VisitWhileStmt(WhileStmt *W); 141117395Skan 142117395Skan CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd); 143169689Skan CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc); 144169689Skan CFGBlock *VisitChildren(Stmt* S); 145169689Skan 146169689Skan // NYS == Not Yet Supported 147169689Skan CFGBlock* NYS() { 148169689Skan badCFG = true; 149169689Skan return Block; 150169689Skan } 15190075Sobrien 15290075Sobrien CFGBlock *StartScope(Stmt *S, CFGBlock *B) { 153169689Skan if (!AddScopes) 154169689Skan return B; 155169689Skan 156169689Skan if (B == 0) 157169689Skan B = createBlock(); 158169689Skan B->StartScope(S, cfg->getBumpVectorContext()); 159169689Skan return B; 160169689Skan } 16190075Sobrien 16290075Sobrien void EndScope(Stmt *S) { 16390075Sobrien if (!AddScopes) 16490075Sobrien return; 16590075Sobrien 16690075Sobrien if (Block == 0) 16790075Sobrien Block = createBlock(); 168169689Skan Block->EndScope(S, cfg->getBumpVectorContext()); 169169689Skan } 170169689Skan 171169689Skan void autoCreateBlock() { if (!Block) Block = createBlock(); } 172169689Skan CFGBlock *createBlock(bool add_successor = true); 173169689Skan bool FinishBlock(CFGBlock* B); 174169689Skan CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) { 175169689Skan return Visit(S, asc); 176117395Skan } 177117395Skan 178117395Skan void AppendStmt(CFGBlock *B, Stmt *S, 179117395Skan AddStmtChoice asc = AddStmtChoice::AlwaysAdd) { 180117395Skan B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue()); 181117395Skan } 182117395Skan 183117395Skan void AddSuccessor(CFGBlock *B, CFGBlock *S) { 184117395Skan B->addSuccessor(S, cfg->getBumpVectorContext()); 185117395Skan } 186117395Skan 187117395Skan /// TryResult - a class representing a variant over the values 188117395Skan /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool, 189117395Skan /// and is used by the CFGBuilder to decide if a branch condition 190117395Skan /// can be decided up front during CFG construction. 191117395Skan class TryResult { 192132718Skan int X; 193132718Skan public: 194132718Skan TryResult(bool b) : X(b ? 1 : 0) {} 195132718Skan TryResult() : X(-1) {} 196132718Skan 197132718Skan bool isTrue() const { return X == 1; } 198132718Skan bool isFalse() const { return X == 0; } 199132718Skan bool isKnown() const { return X >= 0; } 200132718Skan void negate() { 201132718Skan assert(isKnown()); 202169689Skan X ^= 0x1; 203169689Skan } 204169689Skan }; 205169689Skan 206132718Skan /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1 207132718Skan /// if we can evaluate to a known value, otherwise return -1. 208132718Skan TryResult TryEvaluateBool(Expr *S) { 209132718Skan Expr::EvalResult Result; 210132718Skan if (!S->isTypeDependent() && !S->isValueDependent() && 211132718Skan S->Evaluate(Result, *Context) && Result.Val.isInt()) 212132718Skan return Result.Val.getInt().getBoolValue(); 213132718Skan 214132718Skan return TryResult(); 215132718Skan } 216132718Skan 217132718Skan bool badCFG; 218169689Skan 219169689Skan // True iff EH edges on CallExprs should be added to the CFG. 220169689Skan bool AddEHEdges; 221169689Skan 222169689Skan // True iff scope start and scope end notes should be added to the CFG. 223169689Skan bool AddScopes; 224169689Skan}; 225169689Skan 226169689Skan// FIXME: Add support for dependent-sized array types in C++? 227169689Skan// Does it even make sense to build a CFG for an uninstantiated template? 228169689Skanstatic VariableArrayType* FindVA(Type* t) { 229169689Skan while (ArrayType* vt = dyn_cast<ArrayType>(t)) { 230169689Skan if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt)) 231169689Skan if (vat->getSizeExpr()) 232169689Skan return vat; 233169689Skan 23490075Sobrien t = vt->getElementType().getTypePtr(); 23590075Sobrien } 23690075Sobrien 23790075Sobrien return 0; 23890075Sobrien} 23990075Sobrien 24090075Sobrien/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an 24190075Sobrien/// arbitrary statement. Examples include a single expression or a function 24290075Sobrien/// body (compound statement). The ownership of the returned CFG is 24390075Sobrien/// transferred to the caller. If CFG construction fails, this method returns 24490075Sobrien/// NULL. 24590075SobrienCFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C, 24690075Sobrien bool addehedges, bool AddScopes) { 24790075Sobrien AddEHEdges = addehedges; 24890075Sobrien Context = C; 24990075Sobrien assert(cfg.get()); 25090075Sobrien if (!Statement) 25190075Sobrien return NULL; 252117395Skan 253169689Skan this->AddScopes = AddScopes; 254169689Skan badCFG = false; 255169689Skan 256132718Skan // Create an empty block that will serve as the exit block for the CFG. Since 257169689Skan // this is the first block added to the CFG, it will be implicitly registered 258117395Skan // as the exit block. 25990075Sobrien Succ = createBlock(); 26090075Sobrien assert(Succ == &cfg->getExit()); 26190075Sobrien Block = NULL; // the EXIT block is empty. Create all other blocks lazily. 26290075Sobrien 263169689Skan // Visit the statements and create the CFG. 26490075Sobrien CFGBlock* B = addStmt(Statement); 265169689Skan 266117395Skan if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) { 267117395Skan // FIXME: Add code for base initializers and member initializers. 268117395Skan (void)CD; 269169689Skan } 27090075Sobrien if (!B) 271117395Skan B = Succ; 272117395Skan 273132718Skan if (B) { 274132718Skan // Finalize the last constructed block. This usually involves reversing the 275132718Skan // order of the statements in the block. 276169689Skan if (Block) FinishBlock(B); 277169689Skan 278169689Skan // Backpatch the gotos whose label -> block mappings we didn't know when we 279169689Skan // encountered them. 28090075Sobrien for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), 28190075Sobrien E = BackpatchBlocks.end(); I != E; ++I ) { 28290075Sobrien 28390075Sobrien CFGBlock* B = *I; 28490075Sobrien GotoStmt* G = cast<GotoStmt>(B->getTerminator()); 28590075Sobrien LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); 28690075Sobrien 28790075Sobrien // If there is no target for the goto, then we are looking at an 28890075Sobrien // incomplete AST. Handle this by not registering a successor. 289169689Skan if (LI == LabelMap.end()) continue; 290169689Skan 29190075Sobrien AddSuccessor(B, LI->second); 29290075Sobrien } 293132718Skan 294117395Skan // Add successors to the Indirect Goto Dispatch block (if we have one). 295117395Skan if (CFGBlock* B = cfg->getIndirectGotoBlock()) 296117395Skan for (LabelSetTy::iterator I = AddressTakenLabels.begin(), 297117395Skan E = AddressTakenLabels.end(); I != E; ++I ) { 298117395Skan 299132718Skan // Lookup the target block. 300132718Skan LabelMapTy::iterator LI = LabelMap.find(*I); 301132718Skan 302169689Skan // If there is no target block that contains label, then we are looking 303169689Skan // at an incomplete AST. Handle this by not registering a successor. 304169689Skan if (LI == LabelMap.end()) continue; 305169689Skan 306169689Skan AddSuccessor(B, LI->second); 307169689Skan } 308169689Skan 30990075Sobrien Succ = B; 310169689Skan } 311117395Skan 312117395Skan // Create an empty entry block that has no predecessors. 313117395Skan cfg->setEntry(createBlock()); 314117395Skan 315117395Skan return badCFG ? NULL : cfg.take(); 316117395Skan} 317117395Skan 318169689Skan/// createBlock - Used to lazily create blocks that are connected 319169689Skan/// to the current (global) succcessor. 320117395SkanCFGBlock* CFGBuilder::createBlock(bool add_successor) { 321117395Skan CFGBlock* B = cfg->createBlock(); 322132718Skan if (add_successor && Succ) 323117395Skan AddSuccessor(B, Succ); 324117395Skan return B; 325117395Skan} 326117395Skan 327117395Skan/// FinishBlock - "Finalize" the block by checking if we have a bad CFG. 328132718Skanbool CFGBuilder::FinishBlock(CFGBlock* B) { 329132718Skan if (badCFG) 330169689Skan return false; 331169689Skan 332169689Skan assert(B); 333169689Skan return true; 334169689Skan} 335169689Skan 336169689Skan/// Visit - Walk the subtree of a statement and add extra 337169689Skan/// blocks for ternary operators, &&, and ||. We also process "," and 33890075Sobrien/// DeclStmts (which may contain nested control-flow). 339169689SkanCFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) { 340220150SmmtryAgain: 341220150Smm if (!S) { 342169689Skan badCFG = true; 343169689Skan return 0; 344220150Smm } 345220150Smm switch (S->getStmtClass()) { 346169689Skan default: 347169689Skan return VisitStmt(S, asc); 348169689Skan 349169689Skan case Stmt::AddrLabelExprClass: 350169689Skan return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc); 351169689Skan 352169689Skan case Stmt::BinaryOperatorClass: 353169689Skan return VisitBinaryOperator(cast<BinaryOperator>(S), asc); 354132718Skan 35590075Sobrien case Stmt::BlockExprClass: 35690075Sobrien return VisitBlockExpr(cast<BlockExpr>(S), asc); 357117395Skan 35890075Sobrien case Stmt::BreakStmtClass: 359132718Skan return VisitBreakStmt(cast<BreakStmt>(S)); 360132718Skan 361132718Skan case Stmt::CallExprClass: 36290075Sobrien return VisitCallExpr(cast<CallExpr>(S), asc); 363132718Skan 36490075Sobrien case Stmt::CaseStmtClass: 365169689Skan return VisitCaseStmt(cast<CaseStmt>(S)); 366169689Skan 36790075Sobrien case Stmt::ChooseExprClass: 36890075Sobrien return VisitChooseExpr(cast<ChooseExpr>(S), asc); 36990075Sobrien 37090075Sobrien case Stmt::CompoundStmtClass: 37190075Sobrien return VisitCompoundStmt(cast<CompoundStmt>(S)); 37290075Sobrien 373117395Skan case Stmt::ConditionalOperatorClass: 374117395Skan return VisitConditionalOperator(cast<ConditionalOperator>(S), asc); 375117395Skan 376117395Skan case Stmt::ContinueStmtClass: 377117395Skan return VisitContinueStmt(cast<ContinueStmt>(S)); 378117395Skan 379117395Skan case Stmt::CXXCatchStmtClass: 380117395Skan return VisitCXXCatchStmt(cast<CXXCatchStmt>(S)); 381169689Skan 382169689Skan case Stmt::CXXMemberCallExprClass: 383169689Skan return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc); 384169689Skan 385169689Skan case Stmt::CXXThrowExprClass: 386169689Skan return VisitCXXThrowExpr(cast<CXXThrowExpr>(S)); 387169689Skan 388169689Skan case Stmt::CXXTryStmtClass: 389169689Skan return VisitCXXTryStmt(cast<CXXTryStmt>(S)); 390169689Skan 391169689Skan case Stmt::DeclStmtClass: 392169689Skan return VisitDeclStmt(cast<DeclStmt>(S)); 393132718Skan 394132718Skan case Stmt::DefaultStmtClass: 395132718Skan return VisitDefaultStmt(cast<DefaultStmt>(S)); 396132718Skan 397169689Skan case Stmt::DoStmtClass: 398169689Skan return VisitDoStmt(cast<DoStmt>(S)); 399169689Skan 400169689Skan case Stmt::ForStmtClass: 401169689Skan return VisitForStmt(cast<ForStmt>(S)); 402169689Skan 403169689Skan case Stmt::GotoStmtClass: 404169689Skan return VisitGotoStmt(cast<GotoStmt>(S)); 405169689Skan 406169689Skan case Stmt::IfStmtClass: 407169689Skan return VisitIfStmt(cast<IfStmt>(S)); 408169689Skan 409132718Skan case Stmt::IndirectGotoStmtClass: 410132718Skan return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S)); 411132718Skan 412132718Skan case Stmt::LabelStmtClass: 413169689Skan return VisitLabelStmt(cast<LabelStmt>(S)); 414117395Skan 415132718Skan case Stmt::MemberExprClass: 416132718Skan return VisitMemberExpr(cast<MemberExpr>(S), asc); 417117395Skan 418132718Skan case Stmt::ObjCAtCatchStmtClass: 419169689Skan return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S)); 420132718Skan 421169689Skan case Stmt::ObjCAtSynchronizedStmtClass: 422169689Skan return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S)); 423169689Skan 424169689Skan case Stmt::ObjCAtThrowStmtClass: 425132718Skan return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S)); 426117395Skan 427169689Skan case Stmt::ObjCAtTryStmtClass: 428117395Skan return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S)); 429169689Skan 430117395Skan case Stmt::ObjCForCollectionStmtClass: 431117395Skan return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S)); 432117395Skan 433169689Skan case Stmt::ParenExprClass: 434169689Skan S = cast<ParenExpr>(S)->getSubExpr(); 435132718Skan goto tryAgain; 436146895Skan 437169689Skan case Stmt::NullStmtClass: 43896263Sobrien return Block; 439132718Skan 440132718Skan case Stmt::ReturnStmtClass: 441132718Skan return VisitReturnStmt(cast<ReturnStmt>(S)); 442132718Skan 443117395Skan case Stmt::SizeOfAlignOfExprClass: 444117395Skan return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc); 445117395Skan 446117395Skan case Stmt::StmtExprClass: 447117395Skan return VisitStmtExpr(cast<StmtExpr>(S), asc); 448132718Skan 449117395Skan case Stmt::SwitchStmtClass: 450117395Skan return VisitSwitchStmt(cast<SwitchStmt>(S)); 451169689Skan 452169689Skan case Stmt::WhileStmtClass: 453169689Skan return VisitWhileStmt(cast<WhileStmt>(S)); 454169689Skan } 455169689Skan} 456169689Skan 457169689SkanCFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) { 458169689Skan if (asc.alwaysAdd()) { 459132718Skan autoCreateBlock(); 460132718Skan AppendStmt(Block, S, asc); 461132718Skan } 462132718Skan 463132718Skan return VisitChildren(S); 464132718Skan} 465132718Skan 466132718Skan/// VisitChildren - Visit the children of a Stmt. 467132718SkanCFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) { 468132718Skan CFGBlock *B = Block; 469169689Skan for (Stmt::child_iterator I = Terminator->child_begin(), 470132718Skan E = Terminator->child_end(); I != E; ++I) { 471169689Skan if (*I) B = Visit(*I); 472132718Skan } 473169689Skan return B; 474169689Skan} 475169689Skan 476169689SkanCFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, 477169689Skan AddStmtChoice asc) { 478169689Skan AddressTakenLabels.insert(A->getLabel()); 479169689Skan 480169689Skan if (asc.alwaysAdd()) { 481169689Skan autoCreateBlock(); 482169689Skan AppendStmt(Block, A, asc); 483169689Skan } 484169689Skan 485169689Skan return Block; 486169689Skan} 487169689Skan 488169689SkanCFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, 489169689Skan AddStmtChoice asc) { 490169689Skan if (B->isLogicalOp()) { // && or || 491169689Skan CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); 492169689Skan AppendStmt(ConfluenceBlock, B, asc); 493132718Skan 494132718Skan if (!FinishBlock(ConfluenceBlock)) 495132718Skan return 0; 496132718Skan 497132718Skan // create the block evaluating the LHS 498169689Skan CFGBlock* LHSBlock = createBlock(false); 499132718Skan LHSBlock->setTerminator(B); 500132718Skan 501132718Skan // create the block evaluating the RHS 502132718Skan Succ = ConfluenceBlock; 503169689Skan Block = NULL; 504169689Skan CFGBlock* RHSBlock = addStmt(B->getRHS()); 505169689Skan 506169689Skan if (RHSBlock) { 507169689Skan if (!FinishBlock(RHSBlock)) 508169689Skan return 0; 509169689Skan } 510169689Skan else { 511169689Skan // Create an empty block for cases where the RHS doesn't require 512169689Skan // any explicit statements in the CFG. 513169689Skan RHSBlock = createBlock(); 514169689Skan } 515132718Skan 516132718Skan // See if this is a known constant. 517132718Skan TryResult KnownVal = TryEvaluateBool(B->getLHS()); 518132718Skan if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr)) 519132718Skan KnownVal.negate(); 520132718Skan 521132718Skan // Now link the LHSBlock with RHSBlock. 522169689Skan if (B->getOpcode() == BinaryOperator::LOr) { 523132718Skan AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock); 524132718Skan AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock); 525132718Skan } else { 526132718Skan assert(B->getOpcode() == BinaryOperator::LAnd); 527132718Skan AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock); 528169689Skan AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock); 529169689Skan } 530169689Skan 531169689Skan // Generate the blocks for evaluating the LHS. 532169689Skan Block = LHSBlock; 533169689Skan return addStmt(B->getLHS()); 534132718Skan } 535132718Skan else if (B->getOpcode() == BinaryOperator::Comma) { // , 536169689Skan autoCreateBlock(); 537169689Skan AppendStmt(Block, B, asc); 538169689Skan addStmt(B->getRHS()); 539169689Skan return addStmt(B->getLHS()); 540169689Skan } 541169689Skan 542169689Skan return VisitStmt(B, asc); 543169689Skan} 544169689Skan 545169689SkanCFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) { 546169689Skan if (asc.alwaysAdd()) { 547169689Skan autoCreateBlock(); 548169689Skan AppendStmt(Block, E, asc); 549169689Skan } 550169689Skan return Block; 551169689Skan} 552169689Skan 553169689SkanCFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) { 554169689Skan // "break" is a control-flow statement. Thus we stop processing the current 555169689Skan // block. 556169689Skan if (Block && !FinishBlock(Block)) 557169689Skan return 0; 558169689Skan 559169689Skan // Now create a new block that ends with the break statement. 560169689Skan Block = createBlock(false); 561169689Skan Block->setTerminator(B); 562169689Skan 563169689Skan // If there is no target for the break, then we are looking at an incomplete 564169689Skan // AST. This means that the CFG cannot be constructed. 565169689Skan if (BreakTargetBlock) 566169689Skan AddSuccessor(Block, BreakTargetBlock); 567169689Skan else 568169689Skan badCFG = true; 569169689Skan 570169689Skan 571169689Skan return Block; 572169689Skan} 573169689Skan 574169689Skanstatic bool CanThrow(Expr *E) { 575169689Skan QualType Ty = E->getType(); 576169689Skan if (Ty->isFunctionPointerType()) 577169689Skan Ty = Ty->getAs<PointerType>()->getPointeeType(); 578169689Skan else if (Ty->isBlockPointerType()) 579169689Skan Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); 580169689Skan 581169689Skan const FunctionType *FT = Ty->getAs<FunctionType>(); 582169689Skan if (FT) { 583169689Skan if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) 584169689Skan if (Proto->hasEmptyExceptionSpec()) 585169689Skan return false; 586169689Skan } 587169689Skan return true; 588169689Skan} 589169689Skan 590259947SpfgCFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) { 591259947Spfg // If this is a call to a no-return function, this stops the block here. 592259947Spfg bool NoReturn = false; 593259947Spfg if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) { 594169689Skan NoReturn = true; 595169689Skan } 596169689Skan 597169689Skan bool AddEHEdge = false; 598169689Skan 599169689Skan // Languages without exceptions are assumed to not throw. 600169689Skan if (Context->getLangOptions().Exceptions) { 601169689Skan if (AddEHEdges) 602169689Skan AddEHEdge = true; 603169689Skan } 604169689Skan 605169689Skan if (FunctionDecl *FD = C->getDirectCallee()) { 606169689Skan if (FD->hasAttr<NoReturnAttr>()) 607169689Skan NoReturn = true; 608169689Skan if (FD->hasAttr<NoThrowAttr>()) 609169689Skan AddEHEdge = false; 610169689Skan } 611169689Skan 612169689Skan if (!CanThrow(C->getCallee())) 613259947Spfg AddEHEdge = false; 614169689Skan 615169689Skan if (!NoReturn && !AddEHEdge) 616169689Skan return VisitStmt(C, AddStmtChoice::AlwaysAdd); 617169689Skan 61890075Sobrien if (Block) { 61990075Sobrien Succ = Block; 62090075Sobrien if (!FinishBlock(Block)) 62190075Sobrien return 0; 62290075Sobrien } 623169689Skan 624169689Skan Block = createBlock(!NoReturn); 625169689Skan AppendStmt(Block, C, asc); 626169689Skan 62790075Sobrien if (NoReturn) { 62890075Sobrien // Wire this to the exit block directly. 62990075Sobrien AddSuccessor(Block, &cfg->getExit()); 63090075Sobrien } 63190075Sobrien if (AddEHEdge) { 63290075Sobrien // Add exceptional edges. 63390075Sobrien if (TryTerminatedBlock) 63496263Sobrien AddSuccessor(Block, TryTerminatedBlock); 635169689Skan else 636169689Skan AddSuccessor(Block, &cfg->getExit()); 637169689Skan } 63890075Sobrien 63990075Sobrien return VisitChildren(C); 640169689Skan} 641169689Skan 642146895SkanCFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C, 643132718Skan AddStmtChoice asc) { 64490075Sobrien CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); 645117395Skan AppendStmt(ConfluenceBlock, C, asc); 646132718Skan if (!FinishBlock(ConfluenceBlock)) 647132718Skan return 0; 648117395Skan 649132718Skan asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue 650169689Skan : AddStmtChoice::AlwaysAdd; 651132718Skan 652169689Skan Succ = ConfluenceBlock; 653169689Skan Block = NULL; 654169689Skan CFGBlock* LHSBlock = addStmt(C->getLHS(), asc); 655169689Skan if (!FinishBlock(LHSBlock)) 656132718Skan return 0; 657117395Skan 658117395Skan Succ = ConfluenceBlock; 659117395Skan Block = NULL; 660117395Skan CFGBlock* RHSBlock = addStmt(C->getRHS(), asc); 661169689Skan if (!FinishBlock(RHSBlock)) 662169689Skan return 0; 663169689Skan 664132718Skan Block = createBlock(false); 665169689Skan // See if this is a known constant. 666169689Skan const TryResult& KnownVal = TryEvaluateBool(C->getCond()); 667132718Skan AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock); 668132718Skan AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock); 669132718Skan Block->setTerminator(C); 670169689Skan return addStmt(C->getCond()); 671132718Skan} 672132718Skan 673132718Skan 674132718SkanCFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) { 675132718Skan EndScope(C); 676169689Skan 677132718Skan CFGBlock* LastBlock = Block; 678132718Skan 679169689Skan for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend(); 680169689Skan I != E; ++I ) { 681169689Skan LastBlock = addStmt(*I); 682169689Skan 683169689Skan if (badCFG) 684169689Skan return NULL; 685169689Skan } 686169689Skan 687169689Skan LastBlock = StartScope(C, LastBlock); 688169689Skan 689169689Skan return LastBlock; 690132718Skan} 691169689Skan 692169689SkanCFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C, 693169689Skan AddStmtChoice asc) { 694169689Skan // Create the confluence block that will "merge" the results of the ternary 695169689Skan // expression. 696169689Skan CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); 697169689Skan AppendStmt(ConfluenceBlock, C, asc); 69890075Sobrien if (!FinishBlock(ConfluenceBlock)) 699169689Skan return 0; 70096263Sobrien 701117395Skan asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue 702117395Skan : AddStmtChoice::AlwaysAdd; 703132718Skan 704132718Skan // Create a block for the LHS expression if there is an LHS expression. A 705132718Skan // GCC extension allows LHS to be NULL, causing the condition to be the 706169689Skan // value that is returned instead. 707169689Skan // e.g: x ?: y is shorthand for: x ? x : y; 708169689Skan Succ = ConfluenceBlock; 709169689Skan Block = NULL; 71090075Sobrien CFGBlock* LHSBlock = NULL; 71196263Sobrien if (C->getLHS()) { 71296263Sobrien LHSBlock = addStmt(C->getLHS(), asc); 713132718Skan if (!FinishBlock(LHSBlock)) 714 return 0; 715 Block = NULL; 716 } 717 718 // Create the block for the RHS expression. 719 Succ = ConfluenceBlock; 720 CFGBlock* RHSBlock = addStmt(C->getRHS(), asc); 721 if (!FinishBlock(RHSBlock)) 722 return 0; 723 724 // Create the block that will contain the condition. 725 Block = createBlock(false); 726 727 // See if this is a known constant. 728 const TryResult& KnownVal = TryEvaluateBool(C->getCond()); 729 if (LHSBlock) { 730 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock); 731 } else { 732 if (KnownVal.isFalse()) { 733 // If we know the condition is false, add NULL as the successor for 734 // the block containing the condition. In this case, the confluence 735 // block will have just one predecessor. 736 AddSuccessor(Block, 0); 737 assert(ConfluenceBlock->pred_size() == 1); 738 } else { 739 // If we have no LHS expression, add the ConfluenceBlock as a direct 740 // successor for the block containing the condition. Moreover, we need to 741 // reverse the order of the predecessors in the ConfluenceBlock because 742 // the RHSBlock will have been added to the succcessors already, and we 743 // want the first predecessor to the the block containing the expression 744 // for the case when the ternary expression evaluates to true. 745 AddSuccessor(Block, ConfluenceBlock); 746 assert(ConfluenceBlock->pred_size() == 2); 747 std::reverse(ConfluenceBlock->pred_begin(), 748 ConfluenceBlock->pred_end()); 749 } 750 } 751 752 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock); 753 Block->setTerminator(C); 754 return addStmt(C->getCond()); 755} 756 757CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) { 758 autoCreateBlock(); 759 760 if (DS->isSingleDecl()) { 761 AppendStmt(Block, DS); 762 return VisitDeclSubExpr(DS->getSingleDecl()); 763 } 764 765 CFGBlock *B = 0; 766 767 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy. 768 typedef llvm::SmallVector<Decl*,10> BufTy; 769 BufTy Buf(DS->decl_begin(), DS->decl_end()); 770 771 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) { 772 // Get the alignment of the new DeclStmt, padding out to >=8 bytes. 773 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8 774 ? 8 : llvm::AlignOf<DeclStmt>::Alignment; 775 776 // Allocate the DeclStmt using the BumpPtrAllocator. It will get 777 // automatically freed with the CFG. 778 DeclGroupRef DG(*I); 779 Decl *D = *I; 780 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A); 781 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D)); 782 783 // Append the fake DeclStmt to block. 784 AppendStmt(Block, DSNew); 785 B = VisitDeclSubExpr(D); 786 } 787 788 return B; 789} 790 791/// VisitDeclSubExpr - Utility method to add block-level expressions for 792/// initializers in Decls. 793CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) { 794 assert(Block); 795 796 VarDecl *VD = dyn_cast<VarDecl>(D); 797 798 if (!VD) 799 return Block; 800 801 Expr *Init = VD->getInit(); 802 803 if (Init) { 804 AddStmtChoice::Kind k = 805 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd 806 : AddStmtChoice::NotAlwaysAdd; 807 Visit(Init, AddStmtChoice(k)); 808 } 809 810 // If the type of VD is a VLA, then we must process its size expressions. 811 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0; 812 VA = FindVA(VA->getElementType().getTypePtr())) 813 Block = addStmt(VA->getSizeExpr()); 814 815 return Block; 816} 817 818CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) { 819 // We may see an if statement in the middle of a basic block, or it may be the 820 // first statement we are processing. In either case, we create a new basic 821 // block. First, we create the blocks for the then...else statements, and 822 // then we create the block containing the if statement. If we were in the 823 // middle of a block, we stop processing that block. That block is then the 824 // implicit successor for the "then" and "else" clauses. 825 826 // The block we were proccessing is now finished. Make it the successor 827 // block. 828 if (Block) { 829 Succ = Block; 830 if (!FinishBlock(Block)) 831 return 0; 832 } 833 834 // Process the false branch. 835 CFGBlock* ElseBlock = Succ; 836 837 if (Stmt* Else = I->getElse()) { 838 SaveAndRestore<CFGBlock*> sv(Succ); 839 840 // NULL out Block so that the recursive call to Visit will 841 // create a new basic block. 842 Block = NULL; 843 ElseBlock = addStmt(Else); 844 845 if (!ElseBlock) // Can occur when the Else body has all NullStmts. 846 ElseBlock = sv.get(); 847 else if (Block) { 848 if (!FinishBlock(ElseBlock)) 849 return 0; 850 } 851 } 852 853 // Process the true branch. 854 CFGBlock* ThenBlock; 855 { 856 Stmt* Then = I->getThen(); 857 assert(Then); 858 SaveAndRestore<CFGBlock*> sv(Succ); 859 Block = NULL; 860 ThenBlock = addStmt(Then); 861 862 if (!ThenBlock) { 863 // We can reach here if the "then" body has all NullStmts. 864 // Create an empty block so we can distinguish between true and false 865 // branches in path-sensitive analyses. 866 ThenBlock = createBlock(false); 867 AddSuccessor(ThenBlock, sv.get()); 868 } else if (Block) { 869 if (!FinishBlock(ThenBlock)) 870 return 0; 871 } 872 } 873 874 // Now create a new block containing the if statement. 875 Block = createBlock(false); 876 877 // Set the terminator of the new block to the If statement. 878 Block->setTerminator(I); 879 880 // See if this is a known constant. 881 const TryResult &KnownVal = TryEvaluateBool(I->getCond()); 882 883 // Now add the successors. 884 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock); 885 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock); 886 887 // Add the condition as the last statement in the new block. This may create 888 // new blocks as the condition may contain control-flow. Any newly created 889 // blocks will be pointed to be "Block". 890 Block = addStmt(I->getCond()); 891 892 // Finally, if the IfStmt contains a condition variable, add both the IfStmt 893 // and the condition variable initialization to the CFG. 894 if (VarDecl *VD = I->getConditionVariable()) { 895 if (Expr *Init = VD->getInit()) { 896 autoCreateBlock(); 897 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd); 898 addStmt(Init); 899 } 900 } 901 902 return Block; 903} 904 905 906CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) { 907 // If we were in the middle of a block we stop processing that block. 908 // 909 // NOTE: If a "return" appears in the middle of a block, this means that the 910 // code afterwards is DEAD (unreachable). We still keep a basic block 911 // for that code; a simple "mark-and-sweep" from the entry block will be 912 // able to report such dead blocks. 913 if (Block) 914 FinishBlock(Block); 915 916 // Create the new block. 917 Block = createBlock(false); 918 919 // The Exit block is the only successor. 920 AddSuccessor(Block, &cfg->getExit()); 921 922 // Add the return statement to the block. This may create new blocks if R 923 // contains control-flow (short-circuit operations). 924 return VisitStmt(R, AddStmtChoice::AlwaysAdd); 925} 926 927CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) { 928 // Get the block of the labeled statement. Add it to our map. 929 addStmt(L->getSubStmt()); 930 CFGBlock* LabelBlock = Block; 931 932 if (!LabelBlock) // This can happen when the body is empty, i.e. 933 LabelBlock = createBlock(); // scopes that only contains NullStmts. 934 935 assert(LabelMap.find(L) == LabelMap.end() && "label already in map"); 936 LabelMap[ L ] = LabelBlock; 937 938 // Labels partition blocks, so this is the end of the basic block we were 939 // processing (L is the block's label). Because this is label (and we have 940 // already processed the substatement) there is no extra control-flow to worry 941 // about. 942 LabelBlock->setLabel(L); 943 if (!FinishBlock(LabelBlock)) 944 return 0; 945 946 // We set Block to NULL to allow lazy creation of a new block (if necessary); 947 Block = NULL; 948 949 // This block is now the implicit successor of other blocks. 950 Succ = LabelBlock; 951 952 return LabelBlock; 953} 954 955CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) { 956 // Goto is a control-flow statement. Thus we stop processing the current 957 // block and create a new one. 958 if (Block) 959 FinishBlock(Block); 960 961 Block = createBlock(false); 962 Block->setTerminator(G); 963 964 // If we already know the mapping to the label block add the successor now. 965 LabelMapTy::iterator I = LabelMap.find(G->getLabel()); 966 967 if (I == LabelMap.end()) 968 // We will need to backpatch this block later. 969 BackpatchBlocks.push_back(Block); 970 else 971 AddSuccessor(Block, I->second); 972 973 return Block; 974} 975 976CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) { 977 CFGBlock* LoopSuccessor = NULL; 978 979 // "for" is a control-flow statement. Thus we stop processing the current 980 // block. 981 if (Block) { 982 if (!FinishBlock(Block)) 983 return 0; 984 LoopSuccessor = Block; 985 } else 986 LoopSuccessor = Succ; 987 988 // Save the current value for the break targets. 989 // All breaks should go to the code following the loop. 990 SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock); 991 BreakTargetBlock = LoopSuccessor; 992 993 // Because of short-circuit evaluation, the condition of the loop can span 994 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that 995 // evaluate the condition. 996 CFGBlock* ExitConditionBlock = createBlock(false); 997 CFGBlock* EntryConditionBlock = ExitConditionBlock; 998 999 // Set the terminator for the "exit" condition block. 1000 ExitConditionBlock->setTerminator(F); 1001 1002 // Now add the actual condition to the condition block. Because the condition 1003 // itself may contain control-flow, new blocks may be created. 1004 if (Stmt* C = F->getCond()) { 1005 Block = ExitConditionBlock; 1006 EntryConditionBlock = addStmt(C); 1007 assert(Block == EntryConditionBlock); 1008 1009 // If this block contains a condition variable, add both the condition 1010 // variable and initializer to the CFG. 1011 if (VarDecl *VD = F->getConditionVariable()) { 1012 if (Expr *Init = VD->getInit()) { 1013 autoCreateBlock(); 1014 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd); 1015 EntryConditionBlock = addStmt(Init); 1016 assert(Block == EntryConditionBlock); 1017 } 1018 } 1019 1020 if (Block) { 1021 if (!FinishBlock(EntryConditionBlock)) 1022 return 0; 1023 } 1024 } 1025 1026 // The condition block is the implicit successor for the loop body as well as 1027 // any code above the loop. 1028 Succ = EntryConditionBlock; 1029 1030 // See if this is a known constant. 1031 TryResult KnownVal(true); 1032 1033 if (F->getCond()) 1034 KnownVal = TryEvaluateBool(F->getCond()); 1035 1036 // Now create the loop body. 1037 { 1038 assert(F->getBody()); 1039 1040 // Save the current values for Block, Succ, and continue targets. 1041 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), 1042 save_continue(ContinueTargetBlock); 1043 1044 // Create a new block to contain the (bottom) of the loop body. 1045 Block = NULL; 1046 1047 if (Stmt* I = F->getInc()) { 1048 // Generate increment code in its own basic block. This is the target of 1049 // continue statements. 1050 Succ = addStmt(I); 1051 } else { 1052 // No increment code. Create a special, empty, block that is used as the 1053 // target block for "looping back" to the start of the loop. 1054 assert(Succ == EntryConditionBlock); 1055 Succ = createBlock(); 1056 } 1057 1058 // Finish up the increment (or empty) block if it hasn't been already. 1059 if (Block) { 1060 assert(Block == Succ); 1061 if (!FinishBlock(Block)) 1062 return 0; 1063 Block = 0; 1064 } 1065 1066 ContinueTargetBlock = Succ; 1067 1068 // The starting block for the loop increment is the block that should 1069 // represent the 'loop target' for looping back to the start of the loop. 1070 ContinueTargetBlock->setLoopTarget(F); 1071 1072 // Now populate the body block, and in the process create new blocks as we 1073 // walk the body of the loop. 1074 CFGBlock* BodyBlock = addStmt(F->getBody()); 1075 1076 if (!BodyBlock) 1077 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;" 1078 else if (Block && !FinishBlock(BodyBlock)) 1079 return 0; 1080 1081 // This new body block is a successor to our "exit" condition block. 1082 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock); 1083 } 1084 1085 // Link up the condition block with the code that follows the loop. (the 1086 // false branch). 1087 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor); 1088 1089 // If the loop contains initialization, create a new block for those 1090 // statements. This block can also contain statements that precede the loop. 1091 if (Stmt* I = F->getInit()) { 1092 Block = createBlock(); 1093 return addStmt(I); 1094 } else { 1095 // There is no loop initialization. We are thus basically a while loop. 1096 // NULL out Block to force lazy block construction. 1097 Block = NULL; 1098 Succ = EntryConditionBlock; 1099 return EntryConditionBlock; 1100 } 1101} 1102 1103CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) { 1104 if (asc.alwaysAdd()) { 1105 autoCreateBlock(); 1106 AppendStmt(Block, M, asc); 1107 } 1108 return Visit(M->getBase(), 1109 M->isArrow() ? AddStmtChoice::NotAlwaysAdd 1110 : AddStmtChoice::AsLValueNotAlwaysAdd); 1111} 1112 1113CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) { 1114 // Objective-C fast enumeration 'for' statements: 1115 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC 1116 // 1117 // for ( Type newVariable in collection_expression ) { statements } 1118 // 1119 // becomes: 1120 // 1121 // prologue: 1122 // 1. collection_expression 1123 // T. jump to loop_entry 1124 // loop_entry: 1125 // 1. side-effects of element expression 1126 // 1. ObjCForCollectionStmt [performs binding to newVariable] 1127 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil] 1128 // TB: 1129 // statements 1130 // T. jump to loop_entry 1131 // FB: 1132 // what comes after 1133 // 1134 // and 1135 // 1136 // Type existingItem; 1137 // for ( existingItem in expression ) { statements } 1138 // 1139 // becomes: 1140 // 1141 // the same with newVariable replaced with existingItem; the binding works 1142 // the same except that for one ObjCForCollectionStmt::getElement() returns 1143 // a DeclStmt and the other returns a DeclRefExpr. 1144 // 1145 1146 CFGBlock* LoopSuccessor = 0; 1147 1148 if (Block) { 1149 if (!FinishBlock(Block)) 1150 return 0; 1151 LoopSuccessor = Block; 1152 Block = 0; 1153 } else 1154 LoopSuccessor = Succ; 1155 1156 // Build the condition blocks. 1157 CFGBlock* ExitConditionBlock = createBlock(false); 1158 CFGBlock* EntryConditionBlock = ExitConditionBlock; 1159 1160 // Set the terminator for the "exit" condition block. 1161 ExitConditionBlock->setTerminator(S); 1162 1163 // The last statement in the block should be the ObjCForCollectionStmt, which 1164 // performs the actual binding to 'element' and determines if there are any 1165 // more items in the collection. 1166 AppendStmt(ExitConditionBlock, S); 1167 Block = ExitConditionBlock; 1168 1169 // Walk the 'element' expression to see if there are any side-effects. We 1170 // generate new blocks as necesary. We DON'T add the statement by default to 1171 // the CFG unless it contains control-flow. 1172 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd); 1173 if (Block) { 1174 if (!FinishBlock(EntryConditionBlock)) 1175 return 0; 1176 Block = 0; 1177 } 1178 1179 // The condition block is the implicit successor for the loop body as well as 1180 // any code above the loop. 1181 Succ = EntryConditionBlock; 1182 1183 // Now create the true branch. 1184 { 1185 // Save the current values for Succ, continue and break targets. 1186 SaveAndRestore<CFGBlock*> save_Succ(Succ), 1187 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock); 1188 1189 BreakTargetBlock = LoopSuccessor; 1190 ContinueTargetBlock = EntryConditionBlock; 1191 1192 CFGBlock* BodyBlock = addStmt(S->getBody()); 1193 1194 if (!BodyBlock) 1195 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;" 1196 else if (Block) { 1197 if (!FinishBlock(BodyBlock)) 1198 return 0; 1199 } 1200 1201 // This new body block is a successor to our "exit" condition block. 1202 AddSuccessor(ExitConditionBlock, BodyBlock); 1203 } 1204 1205 // Link up the condition block with the code that follows the loop. 1206 // (the false branch). 1207 AddSuccessor(ExitConditionBlock, LoopSuccessor); 1208 1209 // Now create a prologue block to contain the collection expression. 1210 Block = createBlock(); 1211 return addStmt(S->getCollection()); 1212} 1213 1214CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) { 1215 // FIXME: Add locking 'primitives' to CFG for @synchronized. 1216 1217 // Inline the body. 1218 CFGBlock *SyncBlock = addStmt(S->getSynchBody()); 1219 1220 // The sync body starts its own basic block. This makes it a little easier 1221 // for diagnostic clients. 1222 if (SyncBlock) { 1223 if (!FinishBlock(SyncBlock)) 1224 return 0; 1225 1226 Block = 0; 1227 Succ = SyncBlock; 1228 } 1229 1230 // Inline the sync expression. 1231 return addStmt(S->getSynchExpr()); 1232} 1233 1234CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) { 1235 // FIXME 1236 return NYS(); 1237} 1238 1239CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) { 1240 CFGBlock* LoopSuccessor = NULL; 1241 1242 // "while" is a control-flow statement. Thus we stop processing the current 1243 // block. 1244 if (Block) { 1245 if (!FinishBlock(Block)) 1246 return 0; 1247 LoopSuccessor = Block; 1248 } else 1249 LoopSuccessor = Succ; 1250 1251 // Because of short-circuit evaluation, the condition of the loop can span 1252 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that 1253 // evaluate the condition. 1254 CFGBlock* ExitConditionBlock = createBlock(false); 1255 CFGBlock* EntryConditionBlock = ExitConditionBlock; 1256 1257 // Set the terminator for the "exit" condition block. 1258 ExitConditionBlock->setTerminator(W); 1259 1260 // Now add the actual condition to the condition block. Because the condition 1261 // itself may contain control-flow, new blocks may be created. Thus we update 1262 // "Succ" after adding the condition. 1263 if (Stmt* C = W->getCond()) { 1264 Block = ExitConditionBlock; 1265 EntryConditionBlock = addStmt(C); 1266 assert(Block == EntryConditionBlock); 1267 1268 // If this block contains a condition variable, add both the condition 1269 // variable and initializer to the CFG. 1270 if (VarDecl *VD = W->getConditionVariable()) { 1271 if (Expr *Init = VD->getInit()) { 1272 autoCreateBlock(); 1273 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd); 1274 EntryConditionBlock = addStmt(Init); 1275 assert(Block == EntryConditionBlock); 1276 } 1277 } 1278 1279 if (Block) { 1280 if (!FinishBlock(EntryConditionBlock)) 1281 return 0; 1282 } 1283 } 1284 1285 // The condition block is the implicit successor for the loop body as well as 1286 // any code above the loop. 1287 Succ = EntryConditionBlock; 1288 1289 // See if this is a known constant. 1290 const TryResult& KnownVal = TryEvaluateBool(W->getCond()); 1291 1292 // Process the loop body. 1293 { 1294 assert(W->getBody()); 1295 1296 // Save the current values for Block, Succ, and continue and break targets 1297 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), 1298 save_continue(ContinueTargetBlock), 1299 save_break(BreakTargetBlock); 1300 1301 // Create an empty block to represent the transition block for looping back 1302 // to the head of the loop. 1303 Block = 0; 1304 assert(Succ == EntryConditionBlock); 1305 Succ = createBlock(); 1306 Succ->setLoopTarget(W); 1307 ContinueTargetBlock = Succ; 1308 1309 // All breaks should go to the code following the loop. 1310 BreakTargetBlock = LoopSuccessor; 1311 1312 // NULL out Block to force lazy instantiation of blocks for the body. 1313 Block = NULL; 1314 1315 // Create the body. The returned block is the entry to the loop body. 1316 CFGBlock* BodyBlock = addStmt(W->getBody()); 1317 1318 if (!BodyBlock) 1319 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;" 1320 else if (Block) { 1321 if (!FinishBlock(BodyBlock)) 1322 return 0; 1323 } 1324 1325 // Add the loop body entry as a successor to the condition. 1326 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock); 1327 } 1328 1329 // Link up the condition block with the code that follows the loop. (the 1330 // false branch). 1331 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor); 1332 1333 // There can be no more statements in the condition block since we loop back 1334 // to this block. NULL out Block to force lazy creation of another block. 1335 Block = NULL; 1336 1337 // Return the condition block, which is the dominating block for the loop. 1338 Succ = EntryConditionBlock; 1339 return EntryConditionBlock; 1340} 1341 1342 1343CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) { 1344 // FIXME: For now we pretend that @catch and the code it contains does not 1345 // exit. 1346 return Block; 1347} 1348 1349CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) { 1350 // FIXME: This isn't complete. We basically treat @throw like a return 1351 // statement. 1352 1353 // If we were in the middle of a block we stop processing that block. 1354 if (Block && !FinishBlock(Block)) 1355 return 0; 1356 1357 // Create the new block. 1358 Block = createBlock(false); 1359 1360 // The Exit block is the only successor. 1361 AddSuccessor(Block, &cfg->getExit()); 1362 1363 // Add the statement to the block. This may create new blocks if S contains 1364 // control-flow (short-circuit operations). 1365 return VisitStmt(S, AddStmtChoice::AlwaysAdd); 1366} 1367 1368CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) { 1369 // If we were in the middle of a block we stop processing that block. 1370 if (Block && !FinishBlock(Block)) 1371 return 0; 1372 1373 // Create the new block. 1374 Block = createBlock(false); 1375 1376 if (TryTerminatedBlock) 1377 // The current try statement is the only successor. 1378 AddSuccessor(Block, TryTerminatedBlock); 1379 else 1380 // otherwise the Exit block is the only successor. 1381 AddSuccessor(Block, &cfg->getExit()); 1382 1383 // Add the statement to the block. This may create new blocks if S contains 1384 // control-flow (short-circuit operations). 1385 return VisitStmt(T, AddStmtChoice::AlwaysAdd); 1386} 1387 1388CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) { 1389 CFGBlock* LoopSuccessor = NULL; 1390 1391 // "do...while" is a control-flow statement. Thus we stop processing the 1392 // current block. 1393 if (Block) { 1394 if (!FinishBlock(Block)) 1395 return 0; 1396 LoopSuccessor = Block; 1397 } else 1398 LoopSuccessor = Succ; 1399 1400 // Because of short-circuit evaluation, the condition of the loop can span 1401 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that 1402 // evaluate the condition. 1403 CFGBlock* ExitConditionBlock = createBlock(false); 1404 CFGBlock* EntryConditionBlock = ExitConditionBlock; 1405 1406 // Set the terminator for the "exit" condition block. 1407 ExitConditionBlock->setTerminator(D); 1408 1409 // Now add the actual condition to the condition block. Because the condition 1410 // itself may contain control-flow, new blocks may be created. 1411 if (Stmt* C = D->getCond()) { 1412 Block = ExitConditionBlock; 1413 EntryConditionBlock = addStmt(C); 1414 if (Block) { 1415 if (!FinishBlock(EntryConditionBlock)) 1416 return 0; 1417 } 1418 } 1419 1420 // The condition block is the implicit successor for the loop body. 1421 Succ = EntryConditionBlock; 1422 1423 // See if this is a known constant. 1424 const TryResult &KnownVal = TryEvaluateBool(D->getCond()); 1425 1426 // Process the loop body. 1427 CFGBlock* BodyBlock = NULL; 1428 { 1429 assert(D->getBody()); 1430 1431 // Save the current values for Block, Succ, and continue and break targets 1432 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), 1433 save_continue(ContinueTargetBlock), 1434 save_break(BreakTargetBlock); 1435 1436 // All continues within this loop should go to the condition block 1437 ContinueTargetBlock = EntryConditionBlock; 1438 1439 // All breaks should go to the code following the loop. 1440 BreakTargetBlock = LoopSuccessor; 1441 1442 // NULL out Block to force lazy instantiation of blocks for the body. 1443 Block = NULL; 1444 1445 // Create the body. The returned block is the entry to the loop body. 1446 BodyBlock = addStmt(D->getBody()); 1447 1448 if (!BodyBlock) 1449 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)" 1450 else if (Block) { 1451 if (!FinishBlock(BodyBlock)) 1452 return 0; 1453 } 1454 1455 // Add an intermediate block between the BodyBlock and the 1456 // ExitConditionBlock to represent the "loop back" transition. Create an 1457 // empty block to represent the transition block for looping back to the 1458 // head of the loop. 1459 // FIXME: Can we do this more efficiently without adding another block? 1460 Block = NULL; 1461 Succ = BodyBlock; 1462 CFGBlock *LoopBackBlock = createBlock(); 1463 LoopBackBlock->setLoopTarget(D); 1464 1465 // Add the loop body entry as a successor to the condition. 1466 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock); 1467 } 1468 1469 // Link up the condition block with the code that follows the loop. 1470 // (the false branch). 1471 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor); 1472 1473 // There can be no more statements in the body block(s) since we loop back to 1474 // the body. NULL out Block to force lazy creation of another block. 1475 Block = NULL; 1476 1477 // Return the loop body, which is the dominating block for the loop. 1478 Succ = BodyBlock; 1479 return BodyBlock; 1480} 1481 1482CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) { 1483 // "continue" is a control-flow statement. Thus we stop processing the 1484 // current block. 1485 if (Block && !FinishBlock(Block)) 1486 return 0; 1487 1488 // Now create a new block that ends with the continue statement. 1489 Block = createBlock(false); 1490 Block->setTerminator(C); 1491 1492 // If there is no target for the continue, then we are looking at an 1493 // incomplete AST. This means the CFG cannot be constructed. 1494 if (ContinueTargetBlock) 1495 AddSuccessor(Block, ContinueTargetBlock); 1496 else 1497 badCFG = true; 1498 1499 return Block; 1500} 1501 1502CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, 1503 AddStmtChoice asc) { 1504 1505 if (asc.alwaysAdd()) { 1506 autoCreateBlock(); 1507 AppendStmt(Block, E); 1508 } 1509 1510 // VLA types have expressions that must be evaluated. 1511 if (E->isArgumentType()) { 1512 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr()); 1513 VA != 0; VA = FindVA(VA->getElementType().getTypePtr())) 1514 addStmt(VA->getSizeExpr()); 1515 } 1516 1517 return Block; 1518} 1519 1520/// VisitStmtExpr - Utility method to handle (nested) statement 1521/// expressions (a GCC extension). 1522CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) { 1523 if (asc.alwaysAdd()) { 1524 autoCreateBlock(); 1525 AppendStmt(Block, SE); 1526 } 1527 return VisitCompoundStmt(SE->getSubStmt()); 1528} 1529 1530CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) { 1531 // "switch" is a control-flow statement. Thus we stop processing the current 1532 // block. 1533 CFGBlock* SwitchSuccessor = NULL; 1534 1535 if (Block) { 1536 if (!FinishBlock(Block)) 1537 return 0; 1538 SwitchSuccessor = Block; 1539 } else SwitchSuccessor = Succ; 1540 1541 // Save the current "switch" context. 1542 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock), 1543 save_break(BreakTargetBlock), 1544 save_default(DefaultCaseBlock); 1545 1546 // Set the "default" case to be the block after the switch statement. If the 1547 // switch statement contains a "default:", this value will be overwritten with 1548 // the block for that code. 1549 DefaultCaseBlock = SwitchSuccessor; 1550 1551 // Create a new block that will contain the switch statement. 1552 SwitchTerminatedBlock = createBlock(false); 1553 1554 // Now process the switch body. The code after the switch is the implicit 1555 // successor. 1556 Succ = SwitchSuccessor; 1557 BreakTargetBlock = SwitchSuccessor; 1558 1559 // When visiting the body, the case statements should automatically get linked 1560 // up to the switch. We also don't keep a pointer to the body, since all 1561 // control-flow from the switch goes to case/default statements. 1562 assert(Terminator->getBody() && "switch must contain a non-NULL body"); 1563 Block = NULL; 1564 CFGBlock *BodyBlock = addStmt(Terminator->getBody()); 1565 if (Block) { 1566 if (!FinishBlock(BodyBlock)) 1567 return 0; 1568 } 1569 1570 // If we have no "default:" case, the default transition is to the code 1571 // following the switch body. 1572 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock); 1573 1574 // Add the terminator and condition in the switch block. 1575 SwitchTerminatedBlock->setTerminator(Terminator); 1576 assert(Terminator->getCond() && "switch condition must be non-NULL"); 1577 Block = SwitchTerminatedBlock; 1578 Block = addStmt(Terminator->getCond()); 1579 1580 // Finally, if the SwitchStmt contains a condition variable, add both the 1581 // SwitchStmt and the condition variable initialization to the CFG. 1582 if (VarDecl *VD = Terminator->getConditionVariable()) { 1583 if (Expr *Init = VD->getInit()) { 1584 autoCreateBlock(); 1585 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd); 1586 addStmt(Init); 1587 } 1588 } 1589 1590 return Block; 1591} 1592 1593CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) { 1594 // CaseStmts are essentially labels, so they are the first statement in a 1595 // block. 1596 1597 if (CS->getSubStmt()) 1598 addStmt(CS->getSubStmt()); 1599 1600 CFGBlock* CaseBlock = Block; 1601 if (!CaseBlock) 1602 CaseBlock = createBlock(); 1603 1604 // Cases statements partition blocks, so this is the top of the basic block we 1605 // were processing (the "case XXX:" is the label). 1606 CaseBlock->setLabel(CS); 1607 1608 if (!FinishBlock(CaseBlock)) 1609 return 0; 1610 1611 // Add this block to the list of successors for the block with the switch 1612 // statement. 1613 assert(SwitchTerminatedBlock); 1614 AddSuccessor(SwitchTerminatedBlock, CaseBlock); 1615 1616 // We set Block to NULL to allow lazy creation of a new block (if necessary) 1617 Block = NULL; 1618 1619 // This block is now the implicit successor of other blocks. 1620 Succ = CaseBlock; 1621 1622 return CaseBlock; 1623} 1624 1625CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) { 1626 if (Terminator->getSubStmt()) 1627 addStmt(Terminator->getSubStmt()); 1628 1629 DefaultCaseBlock = Block; 1630 1631 if (!DefaultCaseBlock) 1632 DefaultCaseBlock = createBlock(); 1633 1634 // Default statements partition blocks, so this is the top of the basic block 1635 // we were processing (the "default:" is the label). 1636 DefaultCaseBlock->setLabel(Terminator); 1637 1638 if (!FinishBlock(DefaultCaseBlock)) 1639 return 0; 1640 1641 // Unlike case statements, we don't add the default block to the successors 1642 // for the switch statement immediately. This is done when we finish 1643 // processing the switch statement. This allows for the default case 1644 // (including a fall-through to the code after the switch statement) to always 1645 // be the last successor of a switch-terminated block. 1646 1647 // We set Block to NULL to allow lazy creation of a new block (if necessary) 1648 Block = NULL; 1649 1650 // This block is now the implicit successor of other blocks. 1651 Succ = DefaultCaseBlock; 1652 1653 return DefaultCaseBlock; 1654} 1655 1656CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) { 1657 // "try"/"catch" is a control-flow statement. Thus we stop processing the 1658 // current block. 1659 CFGBlock* TrySuccessor = NULL; 1660 1661 if (Block) { 1662 if (!FinishBlock(Block)) 1663 return 0; 1664 TrySuccessor = Block; 1665 } else TrySuccessor = Succ; 1666 1667 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock; 1668 1669 // Create a new block that will contain the try statement. 1670 CFGBlock *NewTryTerminatedBlock = createBlock(false); 1671 // Add the terminator in the try block. 1672 NewTryTerminatedBlock->setTerminator(Terminator); 1673 1674 bool HasCatchAll = false; 1675 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) { 1676 // The code after the try is the implicit successor. 1677 Succ = TrySuccessor; 1678 CXXCatchStmt *CS = Terminator->getHandler(h); 1679 if (CS->getExceptionDecl() == 0) { 1680 HasCatchAll = true; 1681 } 1682 Block = NULL; 1683 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS); 1684 if (CatchBlock == 0) 1685 return 0; 1686 // Add this block to the list of successors for the block with the try 1687 // statement. 1688 AddSuccessor(NewTryTerminatedBlock, CatchBlock); 1689 } 1690 if (!HasCatchAll) { 1691 if (PrevTryTerminatedBlock) 1692 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock); 1693 else 1694 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit()); 1695 } 1696 1697 // The code after the try is the implicit successor. 1698 Succ = TrySuccessor; 1699 1700 // Save the current "try" context. 1701 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock); 1702 TryTerminatedBlock = NewTryTerminatedBlock; 1703 1704 assert(Terminator->getTryBlock() && "try must contain a non-NULL body"); 1705 Block = NULL; 1706 Block = addStmt(Terminator->getTryBlock()); 1707 return Block; 1708} 1709 1710CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) { 1711 // CXXCatchStmt are treated like labels, so they are the first statement in a 1712 // block. 1713 1714 if (CS->getHandlerBlock()) 1715 addStmt(CS->getHandlerBlock()); 1716 1717 CFGBlock* CatchBlock = Block; 1718 if (!CatchBlock) 1719 CatchBlock = createBlock(); 1720 1721 CatchBlock->setLabel(CS); 1722 1723 if (!FinishBlock(CatchBlock)) 1724 return 0; 1725 1726 // We set Block to NULL to allow lazy creation of a new block (if necessary) 1727 Block = NULL; 1728 1729 return CatchBlock; 1730} 1731 1732CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C, 1733 AddStmtChoice asc) { 1734 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue 1735 : AddStmtChoice::AlwaysAdd; 1736 autoCreateBlock(); 1737 AppendStmt(Block, C, AddStmtChoice(K)); 1738 return VisitChildren(C); 1739} 1740 1741CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) { 1742 // Lazily create the indirect-goto dispatch block if there isn't one already. 1743 CFGBlock* IBlock = cfg->getIndirectGotoBlock(); 1744 1745 if (!IBlock) { 1746 IBlock = createBlock(false); 1747 cfg->setIndirectGotoBlock(IBlock); 1748 } 1749 1750 // IndirectGoto is a control-flow statement. Thus we stop processing the 1751 // current block and create a new one. 1752 if (Block && !FinishBlock(Block)) 1753 return 0; 1754 1755 Block = createBlock(false); 1756 Block->setTerminator(I); 1757 AddSuccessor(Block, IBlock); 1758 return addStmt(I->getTarget()); 1759} 1760 1761} // end anonymous namespace 1762 1763/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has 1764/// no successors or predecessors. If this is the first block created in the 1765/// CFG, it is automatically set to be the Entry and Exit of the CFG. 1766CFGBlock* CFG::createBlock() { 1767 bool first_block = begin() == end(); 1768 1769 // Create the block. 1770 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>(); 1771 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC); 1772 Blocks.push_back(Mem, BlkBVC); 1773 1774 // If this is the first block, set it as the Entry and Exit. 1775 if (first_block) 1776 Entry = Exit = &back(); 1777 1778 // Return the block. 1779 return &back(); 1780} 1781 1782/// buildCFG - Constructs a CFG from an AST. Ownership of the returned 1783/// CFG is returned to the caller. 1784CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C, 1785 bool AddEHEdges, bool AddScopes) { 1786 CFGBuilder Builder; 1787 return Builder.buildCFG(D, Statement, C, AddEHEdges, AddScopes); 1788} 1789 1790//===----------------------------------------------------------------------===// 1791// CFG: Queries for BlkExprs. 1792//===----------------------------------------------------------------------===// 1793 1794namespace { 1795 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy; 1796} 1797 1798static void FindSubExprAssignments(Stmt *S, 1799 llvm::SmallPtrSet<Expr*,50>& Set) { 1800 if (!S) 1801 return; 1802 1803 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) { 1804 Stmt *child = *I; 1805 if (!child) 1806 continue; 1807 1808 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child)) 1809 if (B->isAssignmentOp()) Set.insert(B); 1810 1811 FindSubExprAssignments(child, Set); 1812 } 1813} 1814 1815static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) { 1816 BlkExprMapTy* M = new BlkExprMapTy(); 1817 1818 // Look for assignments that are used as subexpressions. These are the only 1819 // assignments that we want to *possibly* register as a block-level 1820 // expression. Basically, if an assignment occurs both in a subexpression and 1821 // at the block-level, it is a block-level expression. 1822 llvm::SmallPtrSet<Expr*,50> SubExprAssignments; 1823 1824 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) 1825 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) 1826 FindSubExprAssignments(*BI, SubExprAssignments); 1827 1828 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) { 1829 1830 // Iterate over the statements again on identify the Expr* and Stmt* at the 1831 // block-level that are block-level expressions. 1832 1833 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) 1834 if (Expr* Exp = dyn_cast<Expr>(*BI)) { 1835 1836 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) { 1837 // Assignment expressions that are not nested within another 1838 // expression are really "statements" whose value is never used by 1839 // another expression. 1840 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp)) 1841 continue; 1842 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) { 1843 // Special handling for statement expressions. The last statement in 1844 // the statement expression is also a block-level expr. 1845 const CompoundStmt* C = Terminator->getSubStmt(); 1846 if (!C->body_empty()) { 1847 unsigned x = M->size(); 1848 (*M)[C->body_back()] = x; 1849 } 1850 } 1851 1852 unsigned x = M->size(); 1853 (*M)[Exp] = x; 1854 } 1855 1856 // Look at terminators. The condition is a block-level expression. 1857 1858 Stmt* S = (*I)->getTerminatorCondition(); 1859 1860 if (S && M->find(S) == M->end()) { 1861 unsigned x = M->size(); 1862 (*M)[S] = x; 1863 } 1864 } 1865 1866 return M; 1867} 1868 1869CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) { 1870 assert(S != NULL); 1871 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); } 1872 1873 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap); 1874 BlkExprMapTy::iterator I = M->find(S); 1875 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second); 1876} 1877 1878unsigned CFG::getNumBlkExprs() { 1879 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap)) 1880 return M->size(); 1881 else { 1882 // We assume callers interested in the number of BlkExprs will want 1883 // the map constructed if it doesn't already exist. 1884 BlkExprMap = (void*) PopulateBlkExprMap(*this); 1885 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size(); 1886 } 1887} 1888 1889//===----------------------------------------------------------------------===// 1890// Cleanup: CFG dstor. 1891//===----------------------------------------------------------------------===// 1892 1893CFG::~CFG() { 1894 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap); 1895} 1896 1897//===----------------------------------------------------------------------===// 1898// CFG pretty printing 1899//===----------------------------------------------------------------------===// 1900 1901namespace { 1902 1903class StmtPrinterHelper : public PrinterHelper { 1904 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy; 1905 StmtMapTy StmtMap; 1906 signed CurrentBlock; 1907 unsigned CurrentStmt; 1908 const LangOptions &LangOpts; 1909public: 1910 1911 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO) 1912 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) { 1913 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) { 1914 unsigned j = 1; 1915 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ; 1916 BI != BEnd; ++BI, ++j ) 1917 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j); 1918 } 1919 } 1920 1921 virtual ~StmtPrinterHelper() {} 1922 1923 const LangOptions &getLangOpts() const { return LangOpts; } 1924 void setBlockID(signed i) { CurrentBlock = i; } 1925 void setStmtID(unsigned i) { CurrentStmt = i; } 1926 1927 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) { 1928 1929 StmtMapTy::iterator I = StmtMap.find(Terminator); 1930 1931 if (I == StmtMap.end()) 1932 return false; 1933 1934 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock 1935 && I->second.second == CurrentStmt) { 1936 return false; 1937 } 1938 1939 OS << "[B" << I->second.first << "." << I->second.second << "]"; 1940 return true; 1941 } 1942}; 1943} // end anonymous namespace 1944 1945 1946namespace { 1947class CFGBlockTerminatorPrint 1948 : public StmtVisitor<CFGBlockTerminatorPrint,void> { 1949 1950 llvm::raw_ostream& OS; 1951 StmtPrinterHelper* Helper; 1952 PrintingPolicy Policy; 1953public: 1954 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper, 1955 const PrintingPolicy &Policy) 1956 : OS(os), Helper(helper), Policy(Policy) {} 1957 1958 void VisitIfStmt(IfStmt* I) { 1959 OS << "if "; 1960 I->getCond()->printPretty(OS,Helper,Policy); 1961 } 1962 1963 // Default case. 1964 void VisitStmt(Stmt* Terminator) { 1965 Terminator->printPretty(OS, Helper, Policy); 1966 } 1967 1968 void VisitForStmt(ForStmt* F) { 1969 OS << "for (" ; 1970 if (F->getInit()) 1971 OS << "..."; 1972 OS << "; "; 1973 if (Stmt* C = F->getCond()) 1974 C->printPretty(OS, Helper, Policy); 1975 OS << "; "; 1976 if (F->getInc()) 1977 OS << "..."; 1978 OS << ")"; 1979 } 1980 1981 void VisitWhileStmt(WhileStmt* W) { 1982 OS << "while " ; 1983 if (Stmt* C = W->getCond()) 1984 C->printPretty(OS, Helper, Policy); 1985 } 1986 1987 void VisitDoStmt(DoStmt* D) { 1988 OS << "do ... while "; 1989 if (Stmt* C = D->getCond()) 1990 C->printPretty(OS, Helper, Policy); 1991 } 1992 1993 void VisitSwitchStmt(SwitchStmt* Terminator) { 1994 OS << "switch "; 1995 Terminator->getCond()->printPretty(OS, Helper, Policy); 1996 } 1997 1998 void VisitCXXTryStmt(CXXTryStmt* CS) { 1999 OS << "try ..."; 2000 } 2001 2002 void VisitConditionalOperator(ConditionalOperator* C) { 2003 C->getCond()->printPretty(OS, Helper, Policy); 2004 OS << " ? ... : ..."; 2005 } 2006 2007 void VisitChooseExpr(ChooseExpr* C) { 2008 OS << "__builtin_choose_expr( "; 2009 C->getCond()->printPretty(OS, Helper, Policy); 2010 OS << " )"; 2011 } 2012 2013 void VisitIndirectGotoStmt(IndirectGotoStmt* I) { 2014 OS << "goto *"; 2015 I->getTarget()->printPretty(OS, Helper, Policy); 2016 } 2017 2018 void VisitBinaryOperator(BinaryOperator* B) { 2019 if (!B->isLogicalOp()) { 2020 VisitExpr(B); 2021 return; 2022 } 2023 2024 B->getLHS()->printPretty(OS, Helper, Policy); 2025 2026 switch (B->getOpcode()) { 2027 case BinaryOperator::LOr: 2028 OS << " || ..."; 2029 return; 2030 case BinaryOperator::LAnd: 2031 OS << " && ..."; 2032 return; 2033 default: 2034 assert(false && "Invalid logical operator."); 2035 } 2036 } 2037 2038 void VisitExpr(Expr* E) { 2039 E->printPretty(OS, Helper, Policy); 2040 } 2041}; 2042} // end anonymous namespace 2043 2044 2045static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper, 2046 const CFGElement &E) { 2047 Stmt *Terminator = E; 2048 2049 if (E.asStartScope()) { 2050 OS << "start scope\n"; 2051 return; 2052 } 2053 if (E.asEndScope()) { 2054 OS << "end scope\n"; 2055 return; 2056 } 2057 2058 if (Helper) { 2059 // special printing for statement-expressions. 2060 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) { 2061 CompoundStmt* Sub = SE->getSubStmt(); 2062 2063 if (Sub->child_begin() != Sub->child_end()) { 2064 OS << "({ ... ; "; 2065 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS); 2066 OS << " })\n"; 2067 return; 2068 } 2069 } 2070 2071 // special printing for comma expressions. 2072 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) { 2073 if (B->getOpcode() == BinaryOperator::Comma) { 2074 OS << "... , "; 2075 Helper->handledStmt(B->getRHS(),OS); 2076 OS << '\n'; 2077 return; 2078 } 2079 } 2080 } 2081 2082 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts())); 2083 2084 // Expressions need a newline. 2085 if (isa<Expr>(Terminator)) OS << '\n'; 2086} 2087 2088static void print_block(llvm::raw_ostream& OS, const CFG* cfg, 2089 const CFGBlock& B, 2090 StmtPrinterHelper* Helper, bool print_edges) { 2091 2092 if (Helper) Helper->setBlockID(B.getBlockID()); 2093 2094 // Print the header. 2095 OS << "\n [ B" << B.getBlockID(); 2096 2097 if (&B == &cfg->getEntry()) 2098 OS << " (ENTRY) ]\n"; 2099 else if (&B == &cfg->getExit()) 2100 OS << " (EXIT) ]\n"; 2101 else if (&B == cfg->getIndirectGotoBlock()) 2102 OS << " (INDIRECT GOTO DISPATCH) ]\n"; 2103 else 2104 OS << " ]\n"; 2105 2106 // Print the label of this block. 2107 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) { 2108 2109 if (print_edges) 2110 OS << " "; 2111 2112 if (LabelStmt* L = dyn_cast<LabelStmt>(Label)) 2113 OS << L->getName(); 2114 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { 2115 OS << "case "; 2116 C->getLHS()->printPretty(OS, Helper, 2117 PrintingPolicy(Helper->getLangOpts())); 2118 if (C->getRHS()) { 2119 OS << " ... "; 2120 C->getRHS()->printPretty(OS, Helper, 2121 PrintingPolicy(Helper->getLangOpts())); 2122 } 2123 } else if (isa<DefaultStmt>(Label)) 2124 OS << "default"; 2125 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) { 2126 OS << "catch ("; 2127 if (CS->getExceptionDecl()) 2128 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()), 2129 0); 2130 else 2131 OS << "..."; 2132 OS << ")"; 2133 2134 } else 2135 assert(false && "Invalid label statement in CFGBlock."); 2136 2137 OS << ":\n"; 2138 } 2139 2140 // Iterate through the statements in the block and print them. 2141 unsigned j = 1; 2142 2143 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ; 2144 I != E ; ++I, ++j ) { 2145 2146 // Print the statement # in the basic block and the statement itself. 2147 if (print_edges) 2148 OS << " "; 2149 2150 OS << llvm::format("%3d", j) << ": "; 2151 2152 if (Helper) 2153 Helper->setStmtID(j); 2154 2155 print_stmt(OS,Helper,*I); 2156 } 2157 2158 // Print the terminator of this block. 2159 if (B.getTerminator()) { 2160 if (print_edges) 2161 OS << " "; 2162 2163 OS << " T: "; 2164 2165 if (Helper) Helper->setBlockID(-1); 2166 2167 CFGBlockTerminatorPrint TPrinter(OS, Helper, 2168 PrintingPolicy(Helper->getLangOpts())); 2169 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator())); 2170 OS << '\n'; 2171 } 2172 2173 if (print_edges) { 2174 // Print the predecessors of this block. 2175 OS << " Predecessors (" << B.pred_size() << "):"; 2176 unsigned i = 0; 2177 2178 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end(); 2179 I != E; ++I, ++i) { 2180 2181 if (i == 8 || (i-8) == 0) 2182 OS << "\n "; 2183 2184 OS << " B" << (*I)->getBlockID(); 2185 } 2186 2187 OS << '\n'; 2188 2189 // Print the successors of this block. 2190 OS << " Successors (" << B.succ_size() << "):"; 2191 i = 0; 2192 2193 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end(); 2194 I != E; ++I, ++i) { 2195 2196 if (i == 8 || (i-8) % 10 == 0) 2197 OS << "\n "; 2198 2199 if (*I) 2200 OS << " B" << (*I)->getBlockID(); 2201 else 2202 OS << " NULL"; 2203 } 2204 2205 OS << '\n'; 2206 } 2207} 2208 2209 2210/// dump - A simple pretty printer of a CFG that outputs to stderr. 2211void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); } 2212 2213/// print - A simple pretty printer of a CFG that outputs to an ostream. 2214void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const { 2215 StmtPrinterHelper Helper(this, LO); 2216 2217 // Print the entry block. 2218 print_block(OS, this, getEntry(), &Helper, true); 2219 2220 // Iterate through the CFGBlocks and print them one by one. 2221 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { 2222 // Skip the entry block, because we already printed it. 2223 if (&(**I) == &getEntry() || &(**I) == &getExit()) 2224 continue; 2225 2226 print_block(OS, this, **I, &Helper, true); 2227 } 2228 2229 // Print the exit block. 2230 print_block(OS, this, getExit(), &Helper, true); 2231 OS.flush(); 2232} 2233 2234/// dump - A simply pretty printer of a CFGBlock that outputs to stderr. 2235void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const { 2236 print(llvm::errs(), cfg, LO); 2237} 2238 2239/// print - A simple pretty printer of a CFGBlock that outputs to an ostream. 2240/// Generally this will only be called from CFG::print. 2241void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg, 2242 const LangOptions &LO) const { 2243 StmtPrinterHelper Helper(cfg, LO); 2244 print_block(OS, cfg, *this, &Helper, true); 2245} 2246 2247/// printTerminator - A simple pretty printer of the terminator of a CFGBlock. 2248void CFGBlock::printTerminator(llvm::raw_ostream &OS, 2249 const LangOptions &LO) const { 2250 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO)); 2251 TPrinter.Visit(const_cast<Stmt*>(getTerminator())); 2252} 2253 2254Stmt* CFGBlock::getTerminatorCondition() { 2255 2256 if (!Terminator) 2257 return NULL; 2258 2259 Expr* E = NULL; 2260 2261 switch (Terminator->getStmtClass()) { 2262 default: 2263 break; 2264 2265 case Stmt::ForStmtClass: 2266 E = cast<ForStmt>(Terminator)->getCond(); 2267 break; 2268 2269 case Stmt::WhileStmtClass: 2270 E = cast<WhileStmt>(Terminator)->getCond(); 2271 break; 2272 2273 case Stmt::DoStmtClass: 2274 E = cast<DoStmt>(Terminator)->getCond(); 2275 break; 2276 2277 case Stmt::IfStmtClass: 2278 E = cast<IfStmt>(Terminator)->getCond(); 2279 break; 2280 2281 case Stmt::ChooseExprClass: 2282 E = cast<ChooseExpr>(Terminator)->getCond(); 2283 break; 2284 2285 case Stmt::IndirectGotoStmtClass: 2286 E = cast<IndirectGotoStmt>(Terminator)->getTarget(); 2287 break; 2288 2289 case Stmt::SwitchStmtClass: 2290 E = cast<SwitchStmt>(Terminator)->getCond(); 2291 break; 2292 2293 case Stmt::ConditionalOperatorClass: 2294 E = cast<ConditionalOperator>(Terminator)->getCond(); 2295 break; 2296 2297 case Stmt::BinaryOperatorClass: // '&&' and '||' 2298 E = cast<BinaryOperator>(Terminator)->getLHS(); 2299 break; 2300 2301 case Stmt::ObjCForCollectionStmtClass: 2302 return Terminator; 2303 } 2304 2305 return E ? E->IgnoreParens() : NULL; 2306} 2307 2308bool CFGBlock::hasBinaryBranchTerminator() const { 2309 2310 if (!Terminator) 2311 return false; 2312 2313 Expr* E = NULL; 2314 2315 switch (Terminator->getStmtClass()) { 2316 default: 2317 return false; 2318 2319 case Stmt::ForStmtClass: 2320 case Stmt::WhileStmtClass: 2321 case Stmt::DoStmtClass: 2322 case Stmt::IfStmtClass: 2323 case Stmt::ChooseExprClass: 2324 case Stmt::ConditionalOperatorClass: 2325 case Stmt::BinaryOperatorClass: 2326 return true; 2327 } 2328 2329 return E ? E->IgnoreParens() : NULL; 2330} 2331 2332 2333//===----------------------------------------------------------------------===// 2334// CFG Graphviz Visualization 2335//===----------------------------------------------------------------------===// 2336 2337 2338#ifndef NDEBUG 2339static StmtPrinterHelper* GraphHelper; 2340#endif 2341 2342void CFG::viewCFG(const LangOptions &LO) const { 2343#ifndef NDEBUG 2344 StmtPrinterHelper H(this, LO); 2345 GraphHelper = &H; 2346 llvm::ViewGraph(this,"CFG"); 2347 GraphHelper = NULL; 2348#endif 2349} 2350 2351namespace llvm { 2352template<> 2353struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits { 2354 2355 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} 2356 2357 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) { 2358 2359#ifndef NDEBUG 2360 std::string OutSStr; 2361 llvm::raw_string_ostream Out(OutSStr); 2362 print_block(Out,Graph, *Node, GraphHelper, false); 2363 std::string& OutStr = Out.str(); 2364 2365 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 2366 2367 // Process string output to make it nicer... 2368 for (unsigned i = 0; i != OutStr.length(); ++i) 2369 if (OutStr[i] == '\n') { // Left justify 2370 OutStr[i] = '\\'; 2371 OutStr.insert(OutStr.begin()+i+1, 'l'); 2372 } 2373 2374 return OutStr; 2375#else 2376 return ""; 2377#endif 2378 } 2379}; 2380} // end namespace llvm 2381