CFG.cpp revision 4f88063677a2d990a44bb0d5f8437d2498b91d7f
1//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the CFG and CFGBuilder classes for representing and 11// building Control-Flow Graphs (CFGs) from ASTs. 12// 13//===----------------------------------------------------------------------===// 14 15#include "clang/Analysis/CFG.h" 16#include "clang/AST/StmtVisitor.h" 17#include "clang/AST/PrettyPrinter.h" 18#include "llvm/ADT/DenseMap.h" 19#include "llvm/ADT/SmallPtrSet.h" 20#include "llvm/Support/GraphWriter.h" 21#include "llvm/Support/Streams.h" 22#include "llvm/Support/Compiler.h" 23#include <llvm/Support/Allocator.h> 24#include <llvm/Support/Format.h> 25 26using namespace clang; 27 28namespace { 29 30// SaveAndRestore - A utility class that uses RIIA to save and restore 31// the value of a variable. 32template<typename T> 33struct VISIBILITY_HIDDEN SaveAndRestore { 34 SaveAndRestore(T& x) : X(x), old_value(x) {} 35 ~SaveAndRestore() { X = old_value; } 36 T get() { return old_value; } 37 38 T& X; 39 T old_value; 40}; 41 42static SourceLocation GetEndLoc(Decl* D) { 43 if (VarDecl* VD = dyn_cast<VarDecl>(D)) 44 if (Expr* Ex = VD->getInit()) 45 return Ex->getSourceRange().getEnd(); 46 47 return D->getLocation(); 48} 49 50/// CFGBuilder - This class implements CFG construction from an AST. 51/// The builder is stateful: an instance of the builder should be used to only 52/// construct a single CFG. 53/// 54/// Example usage: 55/// 56/// CFGBuilder builder; 57/// CFG* cfg = builder.BuildAST(stmt1); 58/// 59/// CFG construction is done via a recursive walk of an AST. We actually parse 60/// the AST in reverse order so that the successor of a basic block is 61/// constructed prior to its predecessor. This allows us to nicely capture 62/// implicit fall-throughs without extra basic blocks. 63/// 64class VISIBILITY_HIDDEN CFGBuilder { 65 CFG* cfg; 66 CFGBlock* Block; 67 CFGBlock* Succ; 68 CFGBlock* ContinueTargetBlock; 69 CFGBlock* BreakTargetBlock; 70 CFGBlock* SwitchTerminatedBlock; 71 CFGBlock* DefaultCaseBlock; 72 73 // LabelMap records the mapping from Label expressions to their blocks. 74 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy; 75 LabelMapTy LabelMap; 76 77 // A list of blocks that end with a "goto" that must be backpatched to their 78 // resolved targets upon completion of CFG construction. 79 typedef std::vector<CFGBlock*> BackpatchBlocksTy; 80 BackpatchBlocksTy BackpatchBlocks; 81 82 // A list of labels whose address has been taken (for indirect gotos). 83 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy; 84 LabelSetTy AddressTakenLabels; 85 86public: 87 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL), 88 ContinueTargetBlock(NULL), BreakTargetBlock(NULL), 89 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) { 90 // Create an empty CFG. 91 cfg = new CFG(); 92 } 93 94 ~CFGBuilder() { delete cfg; } 95 96 // buildCFG - Used by external clients to construct the CFG. 97 CFG* buildCFG(Stmt* Statement); 98 99private: 100 // Visitors to walk an AST and construct the CFG. 101 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd); 102 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd); 103 CFGBlock *VisitBlockExpr(BlockExpr* E); 104 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E); 105 CFGBlock *VisitBreakStmt(BreakStmt *B); 106 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd); 107 CFGBlock *VisitCaseStmt(CaseStmt *C); 108 CFGBlock *VisitChooseExpr(ChooseExpr *C); 109 CFGBlock *VisitCompoundStmt(CompoundStmt *C); 110 CFGBlock *VisitConditionalOperator(ConditionalOperator *C); 111 CFGBlock *VisitContinueStmt(ContinueStmt *C); 112 CFGBlock *VisitDeclStmt(DeclStmt *DS); 113 CFGBlock *VisitDeclSubExpr(Decl* D); 114 CFGBlock *VisitDefaultStmt(DefaultStmt *D); 115 CFGBlock *VisitDoStmt(DoStmt *D); 116 CFGBlock *VisitForStmt(ForStmt *F); 117 CFGBlock *VisitGotoStmt(GotoStmt* G); 118 CFGBlock *VisitIfStmt(IfStmt *I); 119 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I); 120 CFGBlock *VisitLabelStmt(LabelStmt *L); 121 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); 122 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); 123 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); 124 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S); 125 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); 126 CFGBlock *VisitReturnStmt(ReturnStmt* R); 127 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); 128 CFGBlock *VisitStmtExpr(StmtExpr *S); 129 CFGBlock *VisitSwitchStmt(SwitchStmt *S); 130 CFGBlock *VisitWhileStmt(WhileStmt *W); 131 132 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false); 133 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd); 134 CFGBlock *VisitChildren(Stmt* S); 135 136 // NYS == Not Yet Supported 137 CFGBlock* NYS() { 138 badCFG = true; 139 return Block; 140 } 141 142 void autoCreateBlock() { if (!Block) Block = createBlock(); } 143 CFGBlock *createBlock(bool add_successor = true); 144 bool FinishBlock(CFGBlock* B); 145 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); } 146 147 bool badCFG; 148}; 149 150// FIXME: Add support for dependent-sized array types in C++? 151// Does it even make sense to build a CFG for an uninstantiated template? 152static VariableArrayType* FindVA(Type* t) { 153 while (ArrayType* vt = dyn_cast<ArrayType>(t)) { 154 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt)) 155 if (vat->getSizeExpr()) 156 return vat; 157 158 t = vt->getElementType().getTypePtr(); 159 } 160 161 return 0; 162} 163 164/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an 165/// arbitrary statement. Examples include a single expression or a function 166/// body (compound statement). The ownership of the returned CFG is 167/// transferred to the caller. If CFG construction fails, this method returns 168/// NULL. 169CFG* CFGBuilder::buildCFG(Stmt* Statement) { 170 assert(cfg); 171 if (!Statement) 172 return NULL; 173 174 badCFG = false; 175 176 // Create an empty block that will serve as the exit block for the CFG. Since 177 // this is the first block added to the CFG, it will be implicitly registered 178 // as the exit block. 179 Succ = createBlock(); 180 assert (Succ == &cfg->getExit()); 181 Block = NULL; // the EXIT block is empty. Create all other blocks lazily. 182 183 // Visit the statements and create the CFG. 184 CFGBlock* B = addStmt(Statement); 185 if (!B) B = Succ; 186 187 if (B) { 188 // Finalize the last constructed block. This usually involves reversing the 189 // order of the statements in the block. 190 if (Block) FinishBlock(B); 191 192 // Backpatch the gotos whose label -> block mappings we didn't know when we 193 // encountered them. 194 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), 195 E = BackpatchBlocks.end(); I != E; ++I ) { 196 197 CFGBlock* B = *I; 198 GotoStmt* G = cast<GotoStmt>(B->getTerminator()); 199 LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); 200 201 // If there is no target for the goto, then we are looking at an 202 // incomplete AST. Handle this by not registering a successor. 203 if (LI == LabelMap.end()) continue; 204 205 B->addSuccessor(LI->second); 206 } 207 208 // Add successors to the Indirect Goto Dispatch block (if we have one). 209 if (CFGBlock* B = cfg->getIndirectGotoBlock()) 210 for (LabelSetTy::iterator I = AddressTakenLabels.begin(), 211 E = AddressTakenLabels.end(); I != E; ++I ) { 212 213 // Lookup the target block. 214 LabelMapTy::iterator LI = LabelMap.find(*I); 215 216 // If there is no target block that contains label, then we are looking 217 // at an incomplete AST. Handle this by not registering a successor. 218 if (LI == LabelMap.end()) continue; 219 220 B->addSuccessor(LI->second); 221 } 222 223 Succ = B; 224 } 225 226 // Create an empty entry block that has no predecessors. 227 cfg->setEntry(createBlock()); 228 229 if (badCFG) { 230 delete cfg; 231 cfg = NULL; 232 return NULL; 233 } 234 235 // NULL out cfg so that repeated calls to the builder will fail and that the 236 // ownership of the constructed CFG is passed to the caller. 237 CFG* t = cfg; 238 cfg = NULL; 239 return t; 240} 241 242/// createBlock - Used to lazily create blocks that are connected 243/// to the current (global) succcessor. 244CFGBlock* CFGBuilder::createBlock(bool add_successor) { 245 CFGBlock* B = cfg->createBlock(); 246 if (add_successor && Succ) 247 B->addSuccessor(Succ); 248 return B; 249} 250 251/// FinishBlock - When the last statement has been added to the block, we must 252/// reverse the statements because they have been inserted in reverse order. 253bool CFGBuilder::FinishBlock(CFGBlock* B) { 254 if (badCFG) 255 return false; 256 257 assert(B); 258 B->reverseStmts(); 259 return true; 260} 261 262/// Visit - Walk the subtree of a statement and add extra 263/// blocks for ternary operators, &&, and ||. We also process "," and 264/// DeclStmts (which may contain nested control-flow). 265CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) { 266tryAgain: 267 switch (S->getStmtClass()) { 268 default: 269 return VisitStmt(S, alwaysAdd); 270 271 case Stmt::AddrLabelExprClass: 272 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd); 273 274 case Stmt::BinaryOperatorClass: 275 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd); 276 277 case Stmt::BlockExprClass: 278 return VisitBlockExpr(cast<BlockExpr>(S)); 279 280 case Stmt::BlockDeclRefExprClass: 281 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S)); 282 283 case Stmt::BreakStmtClass: 284 return VisitBreakStmt(cast<BreakStmt>(S)); 285 286 case Stmt::CallExprClass: 287 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd); 288 289 case Stmt::CaseStmtClass: 290 return VisitCaseStmt(cast<CaseStmt>(S)); 291 292 case Stmt::ChooseExprClass: 293 return VisitChooseExpr(cast<ChooseExpr>(S)); 294 295 case Stmt::CompoundStmtClass: 296 return VisitCompoundStmt(cast<CompoundStmt>(S)); 297 298 case Stmt::ConditionalOperatorClass: 299 return VisitConditionalOperator(cast<ConditionalOperator>(S)); 300 301 case Stmt::ContinueStmtClass: 302 return VisitContinueStmt(cast<ContinueStmt>(S)); 303 304 case Stmt::DeclStmtClass: 305 return VisitDeclStmt(cast<DeclStmt>(S)); 306 307 case Stmt::DefaultStmtClass: 308 return VisitDefaultStmt(cast<DefaultStmt>(S)); 309 310 case Stmt::DoStmtClass: 311 return VisitDoStmt(cast<DoStmt>(S)); 312 313 case Stmt::ForStmtClass: 314 return VisitForStmt(cast<ForStmt>(S)); 315 316 case Stmt::GotoStmtClass: 317 return VisitGotoStmt(cast<GotoStmt>(S)); 318 319 case Stmt::IfStmtClass: 320 return VisitIfStmt(cast<IfStmt>(S)); 321 322 case Stmt::IndirectGotoStmtClass: 323 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S)); 324 325 case Stmt::LabelStmtClass: 326 return VisitLabelStmt(cast<LabelStmt>(S)); 327 328 case Stmt::ObjCAtCatchStmtClass: 329 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S)); 330 331 case Stmt::ObjCAtSynchronizedStmtClass: 332 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S)); 333 334 case Stmt::ObjCAtThrowStmtClass: 335 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S)); 336 337 case Stmt::ObjCAtTryStmtClass: 338 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S)); 339 340 case Stmt::ObjCForCollectionStmtClass: 341 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S)); 342 343 case Stmt::ParenExprClass: 344 S = cast<ParenExpr>(S)->getSubExpr(); 345 goto tryAgain; 346 347 case Stmt::NullStmtClass: 348 return Block; 349 350 case Stmt::ReturnStmtClass: 351 return VisitReturnStmt(cast<ReturnStmt>(S)); 352 353 case Stmt::SizeOfAlignOfExprClass: 354 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S)); 355 356 case Stmt::StmtExprClass: 357 return VisitStmtExpr(cast<StmtExpr>(S)); 358 359 case Stmt::SwitchStmtClass: 360 return VisitSwitchStmt(cast<SwitchStmt>(S)); 361 362 case Stmt::WhileStmtClass: 363 return VisitWhileStmt(cast<WhileStmt>(S)); 364 } 365} 366 367CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) { 368 if (alwaysAdd) { 369 autoCreateBlock(); 370 Block->appendStmt(S); 371 } 372 373 return VisitChildren(S); 374} 375 376/// VisitChildren - Visit the children of a Stmt. 377CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) { 378 CFGBlock *B = Block; 379 for (Stmt::child_iterator I = Terminator->child_begin(), 380 E = Terminator->child_end(); I != E; ++I) { 381 if (*I) B = Visit(*I); 382 } 383 return B; 384} 385 386CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) { 387 AddressTakenLabels.insert(A->getLabel()); 388 389 if (alwaysAdd) { 390 autoCreateBlock(); 391 Block->appendStmt(A); 392 } 393 394 return Block; 395} 396 397CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) { 398 if (B->isLogicalOp()) { // && or || 399 400 CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); 401 ConfluenceBlock->appendStmt(B); 402 403 if (!FinishBlock(ConfluenceBlock)) 404 return 0; 405 406 // create the block evaluating the LHS 407 CFGBlock* LHSBlock = createBlock(false); 408 LHSBlock->setTerminator(B); 409 410 // create the block evaluating the RHS 411 Succ = ConfluenceBlock; 412 Block = NULL; 413 CFGBlock* RHSBlock = addStmt(B->getRHS()); 414 if (!FinishBlock(RHSBlock)) 415 return 0; 416 417 // Now link the LHSBlock with RHSBlock. 418 if (B->getOpcode() == BinaryOperator::LOr) { 419 LHSBlock->addSuccessor(ConfluenceBlock); 420 LHSBlock->addSuccessor(RHSBlock); 421 } else { 422 assert (B->getOpcode() == BinaryOperator::LAnd); 423 LHSBlock->addSuccessor(RHSBlock); 424 LHSBlock->addSuccessor(ConfluenceBlock); 425 } 426 427 // Generate the blocks for evaluating the LHS. 428 Block = LHSBlock; 429 return addStmt(B->getLHS()); 430 } 431 else if (B->getOpcode() == BinaryOperator::Comma) { // , 432 Block->appendStmt(B); 433 addStmt(B->getRHS()); 434 return addStmt(B->getLHS()); 435 } 436 437 return VisitStmt(B, alwaysAdd); 438} 439 440CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E) { 441 // FIXME 442 return NYS(); 443} 444 445CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E) { 446 // FIXME 447 return NYS(); 448} 449 450CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) { 451 // "break" is a control-flow statement. Thus we stop processing the current 452 // block. 453 if (Block && !FinishBlock(Block)) 454 return 0; 455 456 // Now create a new block that ends with the break statement. 457 Block = createBlock(false); 458 Block->setTerminator(B); 459 460 // If there is no target for the break, then we are looking at an incomplete 461 // AST. This means that the CFG cannot be constructed. 462 if (BreakTargetBlock) 463 Block->addSuccessor(BreakTargetBlock); 464 else 465 badCFG = true; 466 467 468 return Block; 469} 470 471CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) { 472 // If this is a call to a no-return function, this stops the block here. 473 if (FunctionDecl *FD = C->getDirectCallee()) { 474 475 if (!FD->hasAttr<NoReturnAttr>()) 476 return VisitStmt(C, alwaysAdd); 477 478 if (Block && !FinishBlock(Block)) 479 return 0; 480 481 // Create new block with no successor for the remaining pieces. 482 Block = createBlock(false); 483 Block->appendStmt(C); 484 485 // Wire this to the exit block directly. 486 Block->addSuccessor(&cfg->getExit()); 487 488 return VisitChildren(C); 489 } 490 491 return VisitStmt(C, alwaysAdd); 492} 493 494CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) { 495 CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); 496 ConfluenceBlock->appendStmt(C); 497 if (!FinishBlock(ConfluenceBlock)) 498 return 0; 499 500 Succ = ConfluenceBlock; 501 Block = NULL; 502 CFGBlock* LHSBlock = addStmt(C->getLHS()); 503 if (!FinishBlock(LHSBlock)) 504 return 0; 505 506 Succ = ConfluenceBlock; 507 Block = NULL; 508 CFGBlock* RHSBlock = addStmt(C->getRHS()); 509 if (!FinishBlock(RHSBlock)) 510 return 0; 511 512 Block = createBlock(false); 513 Block->addSuccessor(LHSBlock); 514 Block->addSuccessor(RHSBlock); 515 Block->setTerminator(C); 516 return addStmt(C->getCond()); 517} 518 519 520CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) { 521 CFGBlock* LastBlock = Block; 522 523 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend(); 524 I != E; ++I ) { 525 LastBlock = addStmt(*I); 526 } 527 return LastBlock; 528} 529 530CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) { 531 // Create the confluence block that will "merge" the results of the ternary 532 // expression. 533 CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); 534 ConfluenceBlock->appendStmt(C); 535 if (!FinishBlock(ConfluenceBlock)) 536 return 0; 537 538 // Create a block for the LHS expression if there is an LHS expression. A 539 // GCC extension allows LHS to be NULL, causing the condition to be the 540 // value that is returned instead. 541 // e.g: x ?: y is shorthand for: x ? x : y; 542 Succ = ConfluenceBlock; 543 Block = NULL; 544 CFGBlock* LHSBlock = NULL; 545 if (C->getLHS()) { 546 LHSBlock = addStmt(C->getLHS()); 547 if (!FinishBlock(LHSBlock)) 548 return 0; 549 Block = NULL; 550 } 551 552 // Create the block for the RHS expression. 553 Succ = ConfluenceBlock; 554 CFGBlock* RHSBlock = addStmt(C->getRHS()); 555 if (!FinishBlock(RHSBlock)) 556 return 0; 557 558 // Create the block that will contain the condition. 559 Block = createBlock(false); 560 561 if (LHSBlock) 562 Block->addSuccessor(LHSBlock); 563 else { 564 // If we have no LHS expression, add the ConfluenceBlock as a direct 565 // successor for the block containing the condition. Moreover, we need to 566 // reverse the order of the predecessors in the ConfluenceBlock because 567 // the RHSBlock will have been added to the succcessors already, and we 568 // want the first predecessor to the the block containing the expression 569 // for the case when the ternary expression evaluates to true. 570 Block->addSuccessor(ConfluenceBlock); 571 assert (ConfluenceBlock->pred_size() == 2); 572 std::reverse(ConfluenceBlock->pred_begin(), 573 ConfluenceBlock->pred_end()); 574 } 575 576 Block->addSuccessor(RHSBlock); 577 578 Block->setTerminator(C); 579 return addStmt(C->getCond()); 580} 581 582CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) { 583 autoCreateBlock(); 584 585 if (DS->isSingleDecl()) { 586 Block->appendStmt(DS); 587 return VisitDeclSubExpr(DS->getSingleDecl()); 588 } 589 590 CFGBlock *B = 0; 591 592 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy. 593 typedef llvm::SmallVector<Decl*,10> BufTy; 594 BufTy Buf(DS->decl_begin(), DS->decl_end()); 595 596 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) { 597 // Get the alignment of the new DeclStmt, padding out to >=8 bytes. 598 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8 599 ? 8 : llvm::AlignOf<DeclStmt>::Alignment; 600 601 // Allocate the DeclStmt using the BumpPtrAllocator. It will get 602 // automatically freed with the CFG. 603 DeclGroupRef DG(*I); 604 Decl *D = *I; 605 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A); 606 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D)); 607 608 // Append the fake DeclStmt to block. 609 Block->appendStmt(DSNew); 610 B = VisitDeclSubExpr(D); 611 } 612 613 return B; 614} 615 616/// VisitDeclSubExpr - Utility method to add block-level expressions for 617/// initializers in Decls. 618CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) { 619 assert(Block); 620 621 VarDecl *VD = dyn_cast<VarDecl>(D); 622 623 if (!VD) 624 return Block; 625 626 Expr *Init = VD->getInit(); 627 628 if (Init) { 629 // Optimization: Don't create separate block-level statements for literals. 630 switch (Init->getStmtClass()) { 631 case Stmt::IntegerLiteralClass: 632 case Stmt::CharacterLiteralClass: 633 case Stmt::StringLiteralClass: 634 break; 635 default: 636 Block = addStmt(Init); 637 } 638 } 639 640 // If the type of VD is a VLA, then we must process its size expressions. 641 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0; 642 VA = FindVA(VA->getElementType().getTypePtr())) 643 Block = addStmt(VA->getSizeExpr()); 644 645 return Block; 646} 647 648CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) { 649 // We may see an if statement in the middle of a basic block, or it may be the 650 // first statement we are processing. In either case, we create a new basic 651 // block. First, we create the blocks for the then...else statements, and 652 // then we create the block containing the if statement. If we were in the 653 // middle of a block, we stop processing that block and reverse its 654 // statements. That block is then the implicit successor for the "then" and 655 // "else" clauses. 656 657 // The block we were proccessing is now finished. Make it the successor 658 // block. 659 if (Block) { 660 Succ = Block; 661 if (!FinishBlock(Block)) 662 return 0; 663 } 664 665 // Process the false branch. 666 CFGBlock* ElseBlock = Succ; 667 668 if (Stmt* Else = I->getElse()) { 669 SaveAndRestore<CFGBlock*> sv(Succ); 670 671 // NULL out Block so that the recursive call to Visit will 672 // create a new basic block. 673 Block = NULL; 674 ElseBlock = addStmt(Else); 675 676 if (!ElseBlock) // Can occur when the Else body has all NullStmts. 677 ElseBlock = sv.get(); 678 else if (Block) { 679 if (!FinishBlock(ElseBlock)) 680 return 0; 681 } 682 } 683 684 // Process the true branch. 685 CFGBlock* ThenBlock; 686 { 687 Stmt* Then = I->getThen(); 688 assert (Then); 689 SaveAndRestore<CFGBlock*> sv(Succ); 690 Block = NULL; 691 ThenBlock = addStmt(Then); 692 693 if (!ThenBlock) { 694 // We can reach here if the "then" body has all NullStmts. 695 // Create an empty block so we can distinguish between true and false 696 // branches in path-sensitive analyses. 697 ThenBlock = createBlock(false); 698 ThenBlock->addSuccessor(sv.get()); 699 } else if (Block) { 700 if (!FinishBlock(ThenBlock)) 701 return 0; 702 } 703 } 704 705 // Now create a new block containing the if statement. 706 Block = createBlock(false); 707 708 // Set the terminator of the new block to the If statement. 709 Block->setTerminator(I); 710 711 // Now add the successors. 712 Block->addSuccessor(ThenBlock); 713 Block->addSuccessor(ElseBlock); 714 715 // Add the condition as the last statement in the new block. This may create 716 // new blocks as the condition may contain control-flow. Any newly created 717 // blocks will be pointed to be "Block". 718 return addStmt(I->getCond()); 719} 720 721 722CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) { 723 // If we were in the middle of a block we stop processing that block and 724 // reverse its statements. 725 // 726 // NOTE: If a "return" appears in the middle of a block, this means that the 727 // code afterwards is DEAD (unreachable). We still keep a basic block 728 // for that code; a simple "mark-and-sweep" from the entry block will be 729 // able to report such dead blocks. 730 if (Block) FinishBlock(Block); 731 732 // Create the new block. 733 Block = createBlock(false); 734 735 // The Exit block is the only successor. 736 Block->addSuccessor(&cfg->getExit()); 737 738 // Add the return statement to the block. This may create new blocks if R 739 // contains control-flow (short-circuit operations). 740 return VisitStmt(R, true); 741} 742 743CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) { 744 // Get the block of the labeled statement. Add it to our map. 745 addStmt(L->getSubStmt()); 746 CFGBlock* LabelBlock = Block; 747 748 if (!LabelBlock) // This can happen when the body is empty, i.e. 749 LabelBlock = createBlock(); // scopes that only contains NullStmts. 750 751 assert(LabelMap.find(L) == LabelMap.end() && "label already in map"); 752 LabelMap[ L ] = LabelBlock; 753 754 // Labels partition blocks, so this is the end of the basic block we were 755 // processing (L is the block's label). Because this is label (and we have 756 // already processed the substatement) there is no extra control-flow to worry 757 // about. 758 LabelBlock->setLabel(L); 759 if (!FinishBlock(LabelBlock)) 760 return 0; 761 762 // We set Block to NULL to allow lazy creation of a new block (if necessary); 763 Block = NULL; 764 765 // This block is now the implicit successor of other blocks. 766 Succ = LabelBlock; 767 768 return LabelBlock; 769} 770 771CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) { 772 // Goto is a control-flow statement. Thus we stop processing the current 773 // block and create a new one. 774 if (Block) 775 FinishBlock(Block); 776 777 Block = createBlock(false); 778 Block->setTerminator(G); 779 780 // If we already know the mapping to the label block add the successor now. 781 LabelMapTy::iterator I = LabelMap.find(G->getLabel()); 782 783 if (I == LabelMap.end()) 784 // We will need to backpatch this block later. 785 BackpatchBlocks.push_back(Block); 786 else 787 Block->addSuccessor(I->second); 788 789 return Block; 790} 791 792CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) { 793 // "for" is a control-flow statement. Thus we stop processing the current 794 // block. 795 CFGBlock* LoopSuccessor = NULL; 796 797 if (Block) { 798 if (!FinishBlock(Block)) 799 return 0; 800 LoopSuccessor = Block; 801 } else 802 LoopSuccessor = Succ; 803 804 // Because of short-circuit evaluation, the condition of the loop can span 805 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that 806 // evaluate the condition. 807 CFGBlock* ExitConditionBlock = createBlock(false); 808 CFGBlock* EntryConditionBlock = ExitConditionBlock; 809 810 // Set the terminator for the "exit" condition block. 811 ExitConditionBlock->setTerminator(F); 812 813 // Now add the actual condition to the condition block. Because the condition 814 // itself may contain control-flow, new blocks may be created. 815 if (Stmt* C = F->getCond()) { 816 Block = ExitConditionBlock; 817 EntryConditionBlock = addStmt(C); 818 if (Block) { 819 if (!FinishBlock(EntryConditionBlock)) 820 return 0; 821 } 822 } 823 824 // The condition block is the implicit successor for the loop body as well as 825 // any code above the loop. 826 Succ = EntryConditionBlock; 827 828 // Now create the loop body. 829 { 830 assert (F->getBody()); 831 832 // Save the current values for Block, Succ, and continue and break targets 833 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), 834 save_continue(ContinueTargetBlock), 835 save_break(BreakTargetBlock); 836 837 // Create a new block to contain the (bottom) of the loop body. 838 Block = NULL; 839 840 if (Stmt* I = F->getInc()) { 841 // Generate increment code in its own basic block. This is the target of 842 // continue statements. 843 Succ = addStmt(I); 844 } else { 845 // No increment code. Create a special, empty, block that is used as the 846 // target block for "looping back" to the start of the loop. 847 assert(Succ == EntryConditionBlock); 848 Succ = createBlock(); 849 } 850 851 // Finish up the increment (or empty) block if it hasn't been already. 852 if (Block) { 853 assert(Block == Succ); 854 if (!FinishBlock(Block)) 855 return 0; 856 Block = 0; 857 } 858 859 ContinueTargetBlock = Succ; 860 861 // The starting block for the loop increment is the block that should 862 // represent the 'loop target' for looping back to the start of the loop. 863 ContinueTargetBlock->setLoopTarget(F); 864 865 // All breaks should go to the code following the loop. 866 BreakTargetBlock = LoopSuccessor; 867 868 // Now populate the body block, and in the process create new blocks as we 869 // walk the body of the loop. 870 CFGBlock* BodyBlock = addStmt(F->getBody()); 871 872 if (!BodyBlock) 873 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;" 874 else if (Block) { 875 if (!FinishBlock(BodyBlock)) 876 return 0; 877 } 878 879 // This new body block is a successor to our "exit" condition block. 880 ExitConditionBlock->addSuccessor(BodyBlock); 881 } 882 883 // Link up the condition block with the code that follows the loop. (the 884 // false branch). 885 ExitConditionBlock->addSuccessor(LoopSuccessor); 886 887 // If the loop contains initialization, create a new block for those 888 // statements. This block can also contain statements that precede the loop. 889 if (Stmt* I = F->getInit()) { 890 Block = createBlock(); 891 return addStmt(I); 892 } else { 893 // There is no loop initialization. We are thus basically a while loop. 894 // NULL out Block to force lazy block construction. 895 Block = NULL; 896 Succ = EntryConditionBlock; 897 return EntryConditionBlock; 898 } 899} 900 901CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) { 902 // Objective-C fast enumeration 'for' statements: 903 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC 904 // 905 // for ( Type newVariable in collection_expression ) { statements } 906 // 907 // becomes: 908 // 909 // prologue: 910 // 1. collection_expression 911 // T. jump to loop_entry 912 // loop_entry: 913 // 1. side-effects of element expression 914 // 1. ObjCForCollectionStmt [performs binding to newVariable] 915 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil] 916 // TB: 917 // statements 918 // T. jump to loop_entry 919 // FB: 920 // what comes after 921 // 922 // and 923 // 924 // Type existingItem; 925 // for ( existingItem in expression ) { statements } 926 // 927 // becomes: 928 // 929 // the same with newVariable replaced with existingItem; the binding works 930 // the same except that for one ObjCForCollectionStmt::getElement() returns 931 // a DeclStmt and the other returns a DeclRefExpr. 932 // 933 934 CFGBlock* LoopSuccessor = 0; 935 936 if (Block) { 937 if (!FinishBlock(Block)) 938 return 0; 939 LoopSuccessor = Block; 940 Block = 0; 941 } else 942 LoopSuccessor = Succ; 943 944 // Build the condition blocks. 945 CFGBlock* ExitConditionBlock = createBlock(false); 946 CFGBlock* EntryConditionBlock = ExitConditionBlock; 947 948 // Set the terminator for the "exit" condition block. 949 ExitConditionBlock->setTerminator(S); 950 951 // The last statement in the block should be the ObjCForCollectionStmt, which 952 // performs the actual binding to 'element' and determines if there are any 953 // more items in the collection. 954 ExitConditionBlock->appendStmt(S); 955 Block = ExitConditionBlock; 956 957 // Walk the 'element' expression to see if there are any side-effects. We 958 // generate new blocks as necesary. We DON'T add the statement by default to 959 // the CFG unless it contains control-flow. 960 EntryConditionBlock = Visit(S->getElement(), false); 961 if (Block) { 962 if (!FinishBlock(EntryConditionBlock)) 963 return 0; 964 Block = 0; 965 } 966 967 // The condition block is the implicit successor for the loop body as well as 968 // any code above the loop. 969 Succ = EntryConditionBlock; 970 971 // Now create the true branch. 972 { 973 // Save the current values for Succ, continue and break targets. 974 SaveAndRestore<CFGBlock*> save_Succ(Succ), 975 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock); 976 977 BreakTargetBlock = LoopSuccessor; 978 ContinueTargetBlock = EntryConditionBlock; 979 980 CFGBlock* BodyBlock = addStmt(S->getBody()); 981 982 if (!BodyBlock) 983 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;" 984 else if (Block) { 985 if (!FinishBlock(BodyBlock)) 986 return 0; 987 } 988 989 // This new body block is a successor to our "exit" condition block. 990 ExitConditionBlock->addSuccessor(BodyBlock); 991 } 992 993 // Link up the condition block with the code that follows the loop. 994 // (the false branch). 995 ExitConditionBlock->addSuccessor(LoopSuccessor); 996 997 // Now create a prologue block to contain the collection expression. 998 Block = createBlock(); 999 return addStmt(S->getCollection()); 1000} 1001 1002CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) { 1003 // FIXME: Add locking 'primitives' to CFG for @synchronized. 1004 1005 // Inline the body. 1006 CFGBlock *SyncBlock = addStmt(S->getSynchBody()); 1007 1008 // The sync body starts its own basic block. This makes it a little easier 1009 // for diagnostic clients. 1010 if (SyncBlock) { 1011 if (!FinishBlock(SyncBlock)) 1012 return 0; 1013 1014 Block = 0; 1015 } 1016 1017 Succ = SyncBlock; 1018 1019 // Inline the sync expression. 1020 return addStmt(S->getSynchExpr()); 1021} 1022 1023CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) { 1024 // FIXME 1025 return NYS(); 1026} 1027 1028CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) { 1029 // "while" is a control-flow statement. Thus we stop processing the current 1030 // block. 1031 1032 CFGBlock* LoopSuccessor = NULL; 1033 1034 if (Block) { 1035 if (!FinishBlock(Block)) 1036 return 0; 1037 LoopSuccessor = Block; 1038 } else 1039 LoopSuccessor = Succ; 1040 1041 // Because of short-circuit evaluation, the condition of the loop can span 1042 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that 1043 // evaluate the condition. 1044 CFGBlock* ExitConditionBlock = createBlock(false); 1045 CFGBlock* EntryConditionBlock = ExitConditionBlock; 1046 1047 // Set the terminator for the "exit" condition block. 1048 ExitConditionBlock->setTerminator(W); 1049 1050 // Now add the actual condition to the condition block. Because the condition 1051 // itself may contain control-flow, new blocks may be created. Thus we update 1052 // "Succ" after adding the condition. 1053 if (Stmt* C = W->getCond()) { 1054 Block = ExitConditionBlock; 1055 EntryConditionBlock = addStmt(C); 1056 assert(Block == EntryConditionBlock); 1057 if (Block) { 1058 if (!FinishBlock(EntryConditionBlock)) 1059 return 0; 1060 } 1061 } 1062 1063 // The condition block is the implicit successor for the loop body as well as 1064 // any code above the loop. 1065 Succ = EntryConditionBlock; 1066 1067 // Process the loop body. 1068 { 1069 assert(W->getBody()); 1070 1071 // Save the current values for Block, Succ, and continue and break targets 1072 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), 1073 save_continue(ContinueTargetBlock), 1074 save_break(BreakTargetBlock); 1075 1076 // Create an empty block to represent the transition block for looping back 1077 // to the head of the loop. 1078 Block = 0; 1079 assert(Succ == EntryConditionBlock); 1080 Succ = createBlock(); 1081 Succ->setLoopTarget(W); 1082 ContinueTargetBlock = Succ; 1083 1084 // All breaks should go to the code following the loop. 1085 BreakTargetBlock = LoopSuccessor; 1086 1087 // NULL out Block to force lazy instantiation of blocks for the body. 1088 Block = NULL; 1089 1090 // Create the body. The returned block is the entry to the loop body. 1091 CFGBlock* BodyBlock = addStmt(W->getBody()); 1092 1093 if (!BodyBlock) 1094 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;" 1095 else if (Block) { 1096 if (!FinishBlock(BodyBlock)) 1097 return 0; 1098 } 1099 1100 // Add the loop body entry as a successor to the condition. 1101 ExitConditionBlock->addSuccessor(BodyBlock); 1102 } 1103 1104 // Link up the condition block with the code that follows the loop. (the 1105 // false branch). 1106 ExitConditionBlock->addSuccessor(LoopSuccessor); 1107 1108 // There can be no more statements in the condition block since we loop back 1109 // to this block. NULL out Block to force lazy creation of another block. 1110 Block = NULL; 1111 1112 // Return the condition block, which is the dominating block for the loop. 1113 Succ = EntryConditionBlock; 1114 return EntryConditionBlock; 1115} 1116 1117 1118CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) { 1119 // FIXME: For now we pretend that @catch and the code it contains does not 1120 // exit. 1121 return Block; 1122} 1123 1124CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) { 1125 // FIXME: This isn't complete. We basically treat @throw like a return 1126 // statement. 1127 1128 // If we were in the middle of a block we stop processing that block and 1129 // reverse its statements. 1130 if (Block && !FinishBlock(Block)) 1131 return 0; 1132 1133 // Create the new block. 1134 Block = createBlock(false); 1135 1136 // The Exit block is the only successor. 1137 Block->addSuccessor(&cfg->getExit()); 1138 1139 // Add the statement to the block. This may create new blocks if S contains 1140 // control-flow (short-circuit operations). 1141 return VisitStmt(S, true); 1142} 1143 1144CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) { 1145 // "do...while" is a control-flow statement. Thus we stop processing the 1146 // current block. 1147 1148 CFGBlock* LoopSuccessor = NULL; 1149 1150 if (Block) { 1151 if (!FinishBlock(Block)) 1152 return 0; 1153 LoopSuccessor = Block; 1154 } else 1155 LoopSuccessor = Succ; 1156 1157 // Because of short-circuit evaluation, the condition of the loop can span 1158 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that 1159 // evaluate the condition. 1160 CFGBlock* ExitConditionBlock = createBlock(false); 1161 CFGBlock* EntryConditionBlock = ExitConditionBlock; 1162 1163 // Set the terminator for the "exit" condition block. 1164 ExitConditionBlock->setTerminator(D); 1165 1166 // Now add the actual condition to the condition block. Because the condition 1167 // itself may contain control-flow, new blocks may be created. 1168 if (Stmt* C = D->getCond()) { 1169 Block = ExitConditionBlock; 1170 EntryConditionBlock = addStmt(C); 1171 if (Block) { 1172 if (!FinishBlock(EntryConditionBlock)) 1173 return 0; 1174 } 1175 } 1176 1177 // The condition block is the implicit successor for the loop body. 1178 Succ = EntryConditionBlock; 1179 1180 // Process the loop body. 1181 CFGBlock* BodyBlock = NULL; 1182 { 1183 assert (D->getBody()); 1184 1185 // Save the current values for Block, Succ, and continue and break targets 1186 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), 1187 save_continue(ContinueTargetBlock), 1188 save_break(BreakTargetBlock); 1189 1190 // All continues within this loop should go to the condition block 1191 ContinueTargetBlock = EntryConditionBlock; 1192 1193 // All breaks should go to the code following the loop. 1194 BreakTargetBlock = LoopSuccessor; 1195 1196 // NULL out Block to force lazy instantiation of blocks for the body. 1197 Block = NULL; 1198 1199 // Create the body. The returned block is the entry to the loop body. 1200 BodyBlock = addStmt(D->getBody()); 1201 1202 if (!BodyBlock) 1203 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)" 1204 else if (Block) { 1205 if (!FinishBlock(BodyBlock)) 1206 return 0; 1207 } 1208 1209 // Add an intermediate block between the BodyBlock and the 1210 // ExitConditionBlock to represent the "loop back" transition. Create an 1211 // empty block to represent the transition block for looping back to the 1212 // head of the loop. 1213 // FIXME: Can we do this more efficiently without adding another block? 1214 Block = NULL; 1215 Succ = BodyBlock; 1216 CFGBlock *LoopBackBlock = createBlock(); 1217 LoopBackBlock->setLoopTarget(D); 1218 1219 // Add the loop body entry as a successor to the condition. 1220 ExitConditionBlock->addSuccessor(LoopBackBlock); 1221 } 1222 1223 // Link up the condition block with the code that follows the loop. 1224 // (the false branch). 1225 ExitConditionBlock->addSuccessor(LoopSuccessor); 1226 1227 // There can be no more statements in the body block(s) since we loop back to 1228 // the body. NULL out Block to force lazy creation of another block. 1229 Block = NULL; 1230 1231 // Return the loop body, which is the dominating block for the loop. 1232 Succ = BodyBlock; 1233 return BodyBlock; 1234} 1235 1236CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) { 1237 // "continue" is a control-flow statement. Thus we stop processing the 1238 // current block. 1239 if (Block && !FinishBlock(Block)) 1240 return 0; 1241 1242 // Now create a new block that ends with the continue statement. 1243 Block = createBlock(false); 1244 Block->setTerminator(C); 1245 1246 // If there is no target for the continue, then we are looking at an 1247 // incomplete AST. This means the CFG cannot be constructed. 1248 if (ContinueTargetBlock) 1249 Block->addSuccessor(ContinueTargetBlock); 1250 else 1251 badCFG = true; 1252 1253 return Block; 1254} 1255 1256CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { 1257 // VLA types have expressions that must be evaluated. 1258 if (E->isArgumentType()) { 1259 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr()); 1260 VA != 0; VA = FindVA(VA->getElementType().getTypePtr())) 1261 addStmt(VA->getSizeExpr()); 1262 } 1263 // Expressions in sizeof/alignof are not evaluated and thus have no 1264 // control flow. 1265 else { 1266 autoCreateBlock(); 1267 Block->appendStmt(E); 1268 } 1269 1270 return Block; 1271} 1272 1273/// VisitStmtExpr - Utility method to handle (nested) statement 1274/// expressions (a GCC extension). 1275CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE) { 1276 autoCreateBlock(); 1277 Block->appendStmt(SE); 1278 return VisitCompoundStmt(SE->getSubStmt()); 1279} 1280 1281CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) { 1282 // "switch" is a control-flow statement. Thus we stop processing the current 1283 // block. 1284 CFGBlock* SwitchSuccessor = NULL; 1285 1286 if (Block) { 1287 if (!FinishBlock(Block)) 1288 return 0; 1289 SwitchSuccessor = Block; 1290 } else SwitchSuccessor = Succ; 1291 1292 // Save the current "switch" context. 1293 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock), 1294 save_break(BreakTargetBlock), 1295 save_default(DefaultCaseBlock); 1296 1297 // Set the "default" case to be the block after the switch statement. If the 1298 // switch statement contains a "default:", this value will be overwritten with 1299 // the block for that code. 1300 DefaultCaseBlock = SwitchSuccessor; 1301 1302 // Create a new block that will contain the switch statement. 1303 SwitchTerminatedBlock = createBlock(false); 1304 1305 // Now process the switch body. The code after the switch is the implicit 1306 // successor. 1307 Succ = SwitchSuccessor; 1308 BreakTargetBlock = SwitchSuccessor; 1309 1310 // When visiting the body, the case statements should automatically get linked 1311 // up to the switch. We also don't keep a pointer to the body, since all 1312 // control-flow from the switch goes to case/default statements. 1313 assert (Terminator->getBody() && "switch must contain a non-NULL body"); 1314 Block = NULL; 1315 CFGBlock *BodyBlock = addStmt(Terminator->getBody()); 1316 if (Block) { 1317 if (!FinishBlock(BodyBlock)) 1318 return 0; 1319 } 1320 1321 // If we have no "default:" case, the default transition is to the code 1322 // following the switch body. 1323 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock); 1324 1325 // Add the terminator and condition in the switch block. 1326 SwitchTerminatedBlock->setTerminator(Terminator); 1327 assert (Terminator->getCond() && "switch condition must be non-NULL"); 1328 Block = SwitchTerminatedBlock; 1329 1330 return addStmt(Terminator->getCond()); 1331} 1332 1333CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) { 1334 // CaseStmts are essentially labels, so they are the first statement in a 1335 // block. 1336 1337 if (CS->getSubStmt()) 1338 addStmt(CS->getSubStmt()); 1339 1340 CFGBlock* CaseBlock = Block; 1341 if (!CaseBlock) 1342 CaseBlock = createBlock(); 1343 1344 // Cases statements partition blocks, so this is the top of the basic block we 1345 // were processing (the "case XXX:" is the label). 1346 CaseBlock->setLabel(CS); 1347 1348 if (!FinishBlock(CaseBlock)) 1349 return 0; 1350 1351 // Add this block to the list of successors for the block with the switch 1352 // statement. 1353 assert(SwitchTerminatedBlock); 1354 SwitchTerminatedBlock->addSuccessor(CaseBlock); 1355 1356 // We set Block to NULL to allow lazy creation of a new block (if necessary) 1357 Block = NULL; 1358 1359 // This block is now the implicit successor of other blocks. 1360 Succ = CaseBlock; 1361 1362 return CaseBlock; 1363} 1364 1365CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) { 1366 if (Terminator->getSubStmt()) 1367 addStmt(Terminator->getSubStmt()); 1368 1369 DefaultCaseBlock = Block; 1370 1371 if (!DefaultCaseBlock) 1372 DefaultCaseBlock = createBlock(); 1373 1374 // Default statements partition blocks, so this is the top of the basic block 1375 // we were processing (the "default:" is the label). 1376 DefaultCaseBlock->setLabel(Terminator); 1377 1378 if (!FinishBlock(DefaultCaseBlock)) 1379 return 0; 1380 1381 // Unlike case statements, we don't add the default block to the successors 1382 // for the switch statement immediately. This is done when we finish 1383 // processing the switch statement. This allows for the default case 1384 // (including a fall-through to the code after the switch statement) to always 1385 // be the last successor of a switch-terminated block. 1386 1387 // We set Block to NULL to allow lazy creation of a new block (if necessary) 1388 Block = NULL; 1389 1390 // This block is now the implicit successor of other blocks. 1391 Succ = DefaultCaseBlock; 1392 1393 return DefaultCaseBlock; 1394} 1395 1396CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) { 1397 // Lazily create the indirect-goto dispatch block if there isn't one already. 1398 CFGBlock* IBlock = cfg->getIndirectGotoBlock(); 1399 1400 if (!IBlock) { 1401 IBlock = createBlock(false); 1402 cfg->setIndirectGotoBlock(IBlock); 1403 } 1404 1405 // IndirectGoto is a control-flow statement. Thus we stop processing the 1406 // current block and create a new one. 1407 if (Block && !FinishBlock(Block)) 1408 return 0; 1409 1410 Block = createBlock(false); 1411 Block->setTerminator(I); 1412 Block->addSuccessor(IBlock); 1413 return addStmt(I->getTarget()); 1414} 1415 1416} // end anonymous namespace 1417 1418/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has 1419/// no successors or predecessors. If this is the first block created in the 1420/// CFG, it is automatically set to be the Entry and Exit of the CFG. 1421CFGBlock* CFG::createBlock() { 1422 bool first_block = begin() == end(); 1423 1424 // Create the block. 1425 Blocks.push_front(CFGBlock(NumBlockIDs++)); 1426 1427 // If this is the first block, set it as the Entry and Exit. 1428 if (first_block) Entry = Exit = &front(); 1429 1430 // Return the block. 1431 return &front(); 1432} 1433 1434/// buildCFG - Constructs a CFG from an AST. Ownership of the returned 1435/// CFG is returned to the caller. 1436CFG* CFG::buildCFG(Stmt* Statement) { 1437 CFGBuilder Builder; 1438 return Builder.buildCFG(Statement); 1439} 1440 1441/// reverseStmts - Reverses the orders of statements within a CFGBlock. 1442void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); } 1443 1444//===----------------------------------------------------------------------===// 1445// CFG: Queries for BlkExprs. 1446//===----------------------------------------------------------------------===// 1447 1448namespace { 1449 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy; 1450} 1451 1452static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) { 1453 if (!Terminator) 1454 return; 1455 1456 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) { 1457 if (!*I) continue; 1458 1459 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I)) 1460 if (B->isAssignmentOp()) Set.insert(B); 1461 1462 FindSubExprAssignments(*I, Set); 1463 } 1464} 1465 1466static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) { 1467 BlkExprMapTy* M = new BlkExprMapTy(); 1468 1469 // Look for assignments that are used as subexpressions. These are the only 1470 // assignments that we want to *possibly* register as a block-level 1471 // expression. Basically, if an assignment occurs both in a subexpression and 1472 // at the block-level, it is a block-level expression. 1473 llvm::SmallPtrSet<Expr*,50> SubExprAssignments; 1474 1475 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) 1476 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI) 1477 FindSubExprAssignments(*BI, SubExprAssignments); 1478 1479 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) { 1480 1481 // Iterate over the statements again on identify the Expr* and Stmt* at the 1482 // block-level that are block-level expressions. 1483 1484 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI) 1485 if (Expr* Exp = dyn_cast<Expr>(*BI)) { 1486 1487 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) { 1488 // Assignment expressions that are not nested within another 1489 // expression are really "statements" whose value is never used by 1490 // another expression. 1491 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp)) 1492 continue; 1493 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) { 1494 // Special handling for statement expressions. The last statement in 1495 // the statement expression is also a block-level expr. 1496 const CompoundStmt* C = Terminator->getSubStmt(); 1497 if (!C->body_empty()) { 1498 unsigned x = M->size(); 1499 (*M)[C->body_back()] = x; 1500 } 1501 } 1502 1503 unsigned x = M->size(); 1504 (*M)[Exp] = x; 1505 } 1506 1507 // Look at terminators. The condition is a block-level expression. 1508 1509 Stmt* S = I->getTerminatorCondition(); 1510 1511 if (S && M->find(S) == M->end()) { 1512 unsigned x = M->size(); 1513 (*M)[S] = x; 1514 } 1515 } 1516 1517 return M; 1518} 1519 1520CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) { 1521 assert(S != NULL); 1522 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); } 1523 1524 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap); 1525 BlkExprMapTy::iterator I = M->find(S); 1526 1527 if (I == M->end()) return CFG::BlkExprNumTy(); 1528 else return CFG::BlkExprNumTy(I->second); 1529} 1530 1531unsigned CFG::getNumBlkExprs() { 1532 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap)) 1533 return M->size(); 1534 else { 1535 // We assume callers interested in the number of BlkExprs will want 1536 // the map constructed if it doesn't already exist. 1537 BlkExprMap = (void*) PopulateBlkExprMap(*this); 1538 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size(); 1539 } 1540} 1541 1542//===----------------------------------------------------------------------===// 1543// Cleanup: CFG dstor. 1544//===----------------------------------------------------------------------===// 1545 1546CFG::~CFG() { 1547 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap); 1548} 1549 1550//===----------------------------------------------------------------------===// 1551// CFG pretty printing 1552//===----------------------------------------------------------------------===// 1553 1554namespace { 1555 1556class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper { 1557 1558 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy; 1559 StmtMapTy StmtMap; 1560 signed CurrentBlock; 1561 unsigned CurrentStmt; 1562 const LangOptions &LangOpts; 1563public: 1564 1565 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO) 1566 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) { 1567 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) { 1568 unsigned j = 1; 1569 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ; 1570 BI != BEnd; ++BI, ++j ) 1571 StmtMap[*BI] = std::make_pair(I->getBlockID(),j); 1572 } 1573 } 1574 1575 virtual ~StmtPrinterHelper() {} 1576 1577 const LangOptions &getLangOpts() const { return LangOpts; } 1578 void setBlockID(signed i) { CurrentBlock = i; } 1579 void setStmtID(unsigned i) { CurrentStmt = i; } 1580 1581 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) { 1582 1583 StmtMapTy::iterator I = StmtMap.find(Terminator); 1584 1585 if (I == StmtMap.end()) 1586 return false; 1587 1588 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock 1589 && I->second.second == CurrentStmt) 1590 return false; 1591 1592 OS << "[B" << I->second.first << "." << I->second.second << "]"; 1593 return true; 1594 } 1595}; 1596} // end anonymous namespace 1597 1598 1599namespace { 1600class VISIBILITY_HIDDEN CFGBlockTerminatorPrint 1601 : public StmtVisitor<CFGBlockTerminatorPrint,void> { 1602 1603 llvm::raw_ostream& OS; 1604 StmtPrinterHelper* Helper; 1605 PrintingPolicy Policy; 1606 1607public: 1608 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper, 1609 const PrintingPolicy &Policy) 1610 : OS(os), Helper(helper), Policy(Policy) {} 1611 1612 void VisitIfStmt(IfStmt* I) { 1613 OS << "if "; 1614 I->getCond()->printPretty(OS,Helper,Policy); 1615 } 1616 1617 // Default case. 1618 void VisitStmt(Stmt* Terminator) { 1619 Terminator->printPretty(OS, Helper, Policy); 1620 } 1621 1622 void VisitForStmt(ForStmt* F) { 1623 OS << "for (" ; 1624 if (F->getInit()) OS << "..."; 1625 OS << "; "; 1626 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy); 1627 OS << "; "; 1628 if (F->getInc()) OS << "..."; 1629 OS << ")"; 1630 } 1631 1632 void VisitWhileStmt(WhileStmt* W) { 1633 OS << "while " ; 1634 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy); 1635 } 1636 1637 void VisitDoStmt(DoStmt* D) { 1638 OS << "do ... while "; 1639 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy); 1640 } 1641 1642 void VisitSwitchStmt(SwitchStmt* Terminator) { 1643 OS << "switch "; 1644 Terminator->getCond()->printPretty(OS, Helper, Policy); 1645 } 1646 1647 void VisitConditionalOperator(ConditionalOperator* C) { 1648 C->getCond()->printPretty(OS, Helper, Policy); 1649 OS << " ? ... : ..."; 1650 } 1651 1652 void VisitChooseExpr(ChooseExpr* C) { 1653 OS << "__builtin_choose_expr( "; 1654 C->getCond()->printPretty(OS, Helper, Policy); 1655 OS << " )"; 1656 } 1657 1658 void VisitIndirectGotoStmt(IndirectGotoStmt* I) { 1659 OS << "goto *"; 1660 I->getTarget()->printPretty(OS, Helper, Policy); 1661 } 1662 1663 void VisitBinaryOperator(BinaryOperator* B) { 1664 if (!B->isLogicalOp()) { 1665 VisitExpr(B); 1666 return; 1667 } 1668 1669 B->getLHS()->printPretty(OS, Helper, Policy); 1670 1671 switch (B->getOpcode()) { 1672 case BinaryOperator::LOr: 1673 OS << " || ..."; 1674 return; 1675 case BinaryOperator::LAnd: 1676 OS << " && ..."; 1677 return; 1678 default: 1679 assert(false && "Invalid logical operator."); 1680 } 1681 } 1682 1683 void VisitExpr(Expr* E) { 1684 E->printPretty(OS, Helper, Policy); 1685 } 1686}; 1687} // end anonymous namespace 1688 1689 1690static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper, 1691 Stmt* Terminator) { 1692 if (Helper) { 1693 // special printing for statement-expressions. 1694 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) { 1695 CompoundStmt* Sub = SE->getSubStmt(); 1696 1697 if (Sub->child_begin() != Sub->child_end()) { 1698 OS << "({ ... ; "; 1699 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS); 1700 OS << " })\n"; 1701 return; 1702 } 1703 } 1704 1705 // special printing for comma expressions. 1706 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) { 1707 if (B->getOpcode() == BinaryOperator::Comma) { 1708 OS << "... , "; 1709 Helper->handledStmt(B->getRHS(),OS); 1710 OS << '\n'; 1711 return; 1712 } 1713 } 1714 } 1715 1716 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts())); 1717 1718 // Expressions need a newline. 1719 if (isa<Expr>(Terminator)) OS << '\n'; 1720} 1721 1722static void print_block(llvm::raw_ostream& OS, const CFG* cfg, 1723 const CFGBlock& B, 1724 StmtPrinterHelper* Helper, bool print_edges) { 1725 1726 if (Helper) Helper->setBlockID(B.getBlockID()); 1727 1728 // Print the header. 1729 OS << "\n [ B" << B.getBlockID(); 1730 1731 if (&B == &cfg->getEntry()) 1732 OS << " (ENTRY) ]\n"; 1733 else if (&B == &cfg->getExit()) 1734 OS << " (EXIT) ]\n"; 1735 else if (&B == cfg->getIndirectGotoBlock()) 1736 OS << " (INDIRECT GOTO DISPATCH) ]\n"; 1737 else 1738 OS << " ]\n"; 1739 1740 // Print the label of this block. 1741 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) { 1742 1743 if (print_edges) 1744 OS << " "; 1745 1746 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator)) 1747 OS << L->getName(); 1748 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) { 1749 OS << "case "; 1750 C->getLHS()->printPretty(OS, Helper, 1751 PrintingPolicy(Helper->getLangOpts())); 1752 if (C->getRHS()) { 1753 OS << " ... "; 1754 C->getRHS()->printPretty(OS, Helper, 1755 PrintingPolicy(Helper->getLangOpts())); 1756 } 1757 } else if (isa<DefaultStmt>(Terminator)) 1758 OS << "default"; 1759 else 1760 assert(false && "Invalid label statement in CFGBlock."); 1761 1762 OS << ":\n"; 1763 } 1764 1765 // Iterate through the statements in the block and print them. 1766 unsigned j = 1; 1767 1768 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ; 1769 I != E ; ++I, ++j ) { 1770 1771 // Print the statement # in the basic block and the statement itself. 1772 if (print_edges) 1773 OS << " "; 1774 1775 OS << llvm::format("%3d", j) << ": "; 1776 1777 if (Helper) 1778 Helper->setStmtID(j); 1779 1780 print_stmt(OS,Helper,*I); 1781 } 1782 1783 // Print the terminator of this block. 1784 if (B.getTerminator()) { 1785 if (print_edges) 1786 OS << " "; 1787 1788 OS << " T: "; 1789 1790 if (Helper) Helper->setBlockID(-1); 1791 1792 CFGBlockTerminatorPrint TPrinter(OS, Helper, 1793 PrintingPolicy(Helper->getLangOpts())); 1794 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator())); 1795 OS << '\n'; 1796 } 1797 1798 if (print_edges) { 1799 // Print the predecessors of this block. 1800 OS << " Predecessors (" << B.pred_size() << "):"; 1801 unsigned i = 0; 1802 1803 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end(); 1804 I != E; ++I, ++i) { 1805 1806 if (i == 8 || (i-8) == 0) 1807 OS << "\n "; 1808 1809 OS << " B" << (*I)->getBlockID(); 1810 } 1811 1812 OS << '\n'; 1813 1814 // Print the successors of this block. 1815 OS << " Successors (" << B.succ_size() << "):"; 1816 i = 0; 1817 1818 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end(); 1819 I != E; ++I, ++i) { 1820 1821 if (i == 8 || (i-8) % 10 == 0) 1822 OS << "\n "; 1823 1824 OS << " B" << (*I)->getBlockID(); 1825 } 1826 1827 OS << '\n'; 1828 } 1829} 1830 1831 1832/// dump - A simple pretty printer of a CFG that outputs to stderr. 1833void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); } 1834 1835/// print - A simple pretty printer of a CFG that outputs to an ostream. 1836void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const { 1837 StmtPrinterHelper Helper(this, LO); 1838 1839 // Print the entry block. 1840 print_block(OS, this, getEntry(), &Helper, true); 1841 1842 // Iterate through the CFGBlocks and print them one by one. 1843 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { 1844 // Skip the entry block, because we already printed it. 1845 if (&(*I) == &getEntry() || &(*I) == &getExit()) 1846 continue; 1847 1848 print_block(OS, this, *I, &Helper, true); 1849 } 1850 1851 // Print the exit block. 1852 print_block(OS, this, getExit(), &Helper, true); 1853 OS.flush(); 1854} 1855 1856/// dump - A simply pretty printer of a CFGBlock that outputs to stderr. 1857void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const { 1858 print(llvm::errs(), cfg, LO); 1859} 1860 1861/// print - A simple pretty printer of a CFGBlock that outputs to an ostream. 1862/// Generally this will only be called from CFG::print. 1863void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg, 1864 const LangOptions &LO) const { 1865 StmtPrinterHelper Helper(cfg, LO); 1866 print_block(OS, cfg, *this, &Helper, true); 1867} 1868 1869/// printTerminator - A simple pretty printer of the terminator of a CFGBlock. 1870void CFGBlock::printTerminator(llvm::raw_ostream &OS, 1871 const LangOptions &LO) const { 1872 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO)); 1873 TPrinter.Visit(const_cast<Stmt*>(getTerminator())); 1874} 1875 1876Stmt* CFGBlock::getTerminatorCondition() { 1877 1878 if (!Terminator) 1879 return NULL; 1880 1881 Expr* E = NULL; 1882 1883 switch (Terminator->getStmtClass()) { 1884 default: 1885 break; 1886 1887 case Stmt::ForStmtClass: 1888 E = cast<ForStmt>(Terminator)->getCond(); 1889 break; 1890 1891 case Stmt::WhileStmtClass: 1892 E = cast<WhileStmt>(Terminator)->getCond(); 1893 break; 1894 1895 case Stmt::DoStmtClass: 1896 E = cast<DoStmt>(Terminator)->getCond(); 1897 break; 1898 1899 case Stmt::IfStmtClass: 1900 E = cast<IfStmt>(Terminator)->getCond(); 1901 break; 1902 1903 case Stmt::ChooseExprClass: 1904 E = cast<ChooseExpr>(Terminator)->getCond(); 1905 break; 1906 1907 case Stmt::IndirectGotoStmtClass: 1908 E = cast<IndirectGotoStmt>(Terminator)->getTarget(); 1909 break; 1910 1911 case Stmt::SwitchStmtClass: 1912 E = cast<SwitchStmt>(Terminator)->getCond(); 1913 break; 1914 1915 case Stmt::ConditionalOperatorClass: 1916 E = cast<ConditionalOperator>(Terminator)->getCond(); 1917 break; 1918 1919 case Stmt::BinaryOperatorClass: // '&&' and '||' 1920 E = cast<BinaryOperator>(Terminator)->getLHS(); 1921 break; 1922 1923 case Stmt::ObjCForCollectionStmtClass: 1924 return Terminator; 1925 } 1926 1927 return E ? E->IgnoreParens() : NULL; 1928} 1929 1930bool CFGBlock::hasBinaryBranchTerminator() const { 1931 1932 if (!Terminator) 1933 return false; 1934 1935 Expr* E = NULL; 1936 1937 switch (Terminator->getStmtClass()) { 1938 default: 1939 return false; 1940 1941 case Stmt::ForStmtClass: 1942 case Stmt::WhileStmtClass: 1943 case Stmt::DoStmtClass: 1944 case Stmt::IfStmtClass: 1945 case Stmt::ChooseExprClass: 1946 case Stmt::ConditionalOperatorClass: 1947 case Stmt::BinaryOperatorClass: 1948 return true; 1949 } 1950 1951 return E ? E->IgnoreParens() : NULL; 1952} 1953 1954 1955//===----------------------------------------------------------------------===// 1956// CFG Graphviz Visualization 1957//===----------------------------------------------------------------------===// 1958 1959 1960#ifndef NDEBUG 1961static StmtPrinterHelper* GraphHelper; 1962#endif 1963 1964void CFG::viewCFG(const LangOptions &LO) const { 1965#ifndef NDEBUG 1966 StmtPrinterHelper H(this, LO); 1967 GraphHelper = &H; 1968 llvm::ViewGraph(this,"CFG"); 1969 GraphHelper = NULL; 1970#endif 1971} 1972 1973namespace llvm { 1974template<> 1975struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits { 1976 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph, 1977 bool ShortNames) { 1978 1979#ifndef NDEBUG 1980 std::string OutSStr; 1981 llvm::raw_string_ostream Out(OutSStr); 1982 print_block(Out,Graph, *Node, GraphHelper, false); 1983 std::string& OutStr = Out.str(); 1984 1985 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 1986 1987 // Process string output to make it nicer... 1988 for (unsigned i = 0; i != OutStr.length(); ++i) 1989 if (OutStr[i] == '\n') { // Left justify 1990 OutStr[i] = '\\'; 1991 OutStr.insert(OutStr.begin()+i+1, 'l'); 1992 } 1993 1994 return OutStr; 1995#else 1996 return ""; 1997#endif 1998 } 1999}; 2000} // end namespace llvm 2001