Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1 | //===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Ted Kremenek and is distributed under |
| 6 | // the University of Illinois Open Source 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/AST/CFG.h" |
| 16 | #include "clang/AST/Expr.h" |
Ted Kremenek | c310e93 | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/PrettyPrinter.h" |
Ted Kremenek | 0cebe3e | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 21 | #include "llvm/Support/GraphWriter.h" |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 22 | #include <iostream> |
| 23 | #include <iomanip> |
| 24 | #include <algorithm> |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 25 | #include <sstream> |
| 26 | |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
| 29 | namespace { |
| 30 | |
Ted Kremenek | befef2f | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 31 | // SaveAndRestore - A utility class that uses RIIA to save and restore |
| 32 | // the value of a variable. |
| 33 | template<typename T> |
| 34 | struct SaveAndRestore { |
| 35 | SaveAndRestore(T& x) : X(x), old_value(x) {} |
| 36 | ~SaveAndRestore() { X = old_value; } |
Ted Kremenek | b6f7b72 | 2007-08-30 18:13:31 +0000 | [diff] [blame] | 37 | T get() { return old_value; } |
| 38 | |
Ted Kremenek | befef2f | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 39 | T& X; |
| 40 | T old_value; |
| 41 | }; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 42 | |
| 43 | /// CFGBuilder - This class is implements CFG construction from an AST. |
| 44 | /// The builder is stateful: an instance of the builder should be used to only |
| 45 | /// construct a single CFG. |
| 46 | /// |
| 47 | /// Example usage: |
| 48 | /// |
| 49 | /// CFGBuilder builder; |
| 50 | /// CFG* cfg = builder.BuildAST(stmt1); |
| 51 | /// |
Ted Kremenek | c310e93 | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 52 | /// CFG construction is done via a recursive walk of an AST. |
| 53 | /// We actually parse the AST in reverse order so that the successor |
| 54 | /// of a basic block is constructed prior to its predecessor. This |
| 55 | /// allows us to nicely capture implicit fall-throughs without extra |
| 56 | /// basic blocks. |
| 57 | /// |
| 58 | class CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> { |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 59 | CFG* cfg; |
| 60 | CFGBlock* Block; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 61 | CFGBlock* Succ; |
Ted Kremenek | bf15b27 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 62 | CFGBlock* ContinueTargetBlock; |
Ted Kremenek | 8a29471 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 63 | CFGBlock* BreakTargetBlock; |
Ted Kremenek | b5c13b0 | 2007-08-23 18:43:24 +0000 | [diff] [blame] | 64 | CFGBlock* SwitchTerminatedBlock; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 66 | // LabelMap records the mapping from Label expressions to their blocks. |
Ted Kremenek | 0cebe3e | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 67 | typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy; |
| 68 | LabelMapTy LabelMap; |
| 69 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 70 | // A list of blocks that end with a "goto" that must be backpatched to |
| 71 | // their resolved targets upon completion of CFG construction. |
Ted Kremenek | 4a2b8a1 | 2007-08-22 15:40:58 +0000 | [diff] [blame] | 72 | typedef std::vector<CFGBlock*> BackpatchBlocksTy; |
Ted Kremenek | 0cebe3e | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 73 | BackpatchBlocksTy BackpatchBlocks; |
| 74 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 75 | // A list of labels whose address has been taken (for indirect gotos). |
| 76 | typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy; |
| 77 | LabelSetTy AddressTakenLabels; |
| 78 | |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 79 | public: |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 80 | explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL), |
Ted Kremenek | 8a29471 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 81 | ContinueTargetBlock(NULL), BreakTargetBlock(NULL), |
Ted Kremenek | 9438252 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 82 | SwitchTerminatedBlock(NULL) { |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 83 | // Create an empty CFG. |
| 84 | cfg = new CFG(); |
| 85 | } |
| 86 | |
| 87 | ~CFGBuilder() { delete cfg; } |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 89 | // buildCFG - Used by external clients to construct the CFG. |
| 90 | CFG* buildCFG(Stmt* Statement); |
Ted Kremenek | c310e93 | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 92 | // Visitors to walk an AST and construct the CFG. Called by |
| 93 | // buildCFG. Do not call directly! |
Ted Kremenek | e8ee26b | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 95 | CFGBlock* VisitStmt(Stmt* Statement); |
| 96 | CFGBlock* VisitNullStmt(NullStmt* Statement); |
| 97 | CFGBlock* VisitCompoundStmt(CompoundStmt* C); |
| 98 | CFGBlock* VisitIfStmt(IfStmt* I); |
| 99 | CFGBlock* VisitReturnStmt(ReturnStmt* R); |
| 100 | CFGBlock* VisitLabelStmt(LabelStmt* L); |
| 101 | CFGBlock* VisitGotoStmt(GotoStmt* G); |
| 102 | CFGBlock* VisitForStmt(ForStmt* F); |
| 103 | CFGBlock* VisitWhileStmt(WhileStmt* W); |
| 104 | CFGBlock* VisitDoStmt(DoStmt* D); |
| 105 | CFGBlock* VisitContinueStmt(ContinueStmt* C); |
| 106 | CFGBlock* VisitBreakStmt(BreakStmt* B); |
| 107 | CFGBlock* VisitSwitchStmt(SwitchStmt* S); |
| 108 | CFGBlock* VisitSwitchCase(SwitchCase* S); |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 109 | CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 111 | private: |
| 112 | CFGBlock* createBlock(bool add_successor = true); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 113 | CFGBlock* addStmt(Stmt* S); |
| 114 | CFGBlock* WalkAST(Stmt* S, bool AlwaysAddStmt); |
| 115 | CFGBlock* WalkAST_VisitChildren(Stmt* S); |
Ted Kremenek | 8f54c1f | 2007-10-30 21:48:34 +0000 | [diff] [blame] | 116 | CFGBlock* WalkAST_VisitDeclSubExprs(StmtIterator& I); |
Ted Kremenek | 15c27a8 | 2007-08-28 18:30:10 +0000 | [diff] [blame] | 117 | CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* S); |
Ted Kremenek | f50ec10 | 2007-09-11 21:29:43 +0000 | [diff] [blame] | 118 | CFGBlock* WalkAST_VisitCallExpr(CallExpr* C); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 119 | void FinishBlock(CFGBlock* B); |
Ted Kremenek | e8ee26b | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 121 | }; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 122 | |
| 123 | /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can |
| 124 | /// represent an arbitrary statement. Examples include a single expression |
| 125 | /// or a function body (compound statement). The ownership of the returned |
| 126 | /// CFG is transferred to the caller. If CFG construction fails, this method |
| 127 | /// returns NULL. |
| 128 | CFG* CFGBuilder::buildCFG(Stmt* Statement) { |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 129 | assert (cfg); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 130 | if (!Statement) return NULL; |
| 131 | |
| 132 | // Create an empty block that will serve as the exit block for the CFG. |
| 133 | // Since this is the first block added to the CFG, it will be implicitly |
| 134 | // registered as the exit block. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 135 | Succ = createBlock(); |
| 136 | assert (Succ == &cfg->getExit()); |
| 137 | Block = NULL; // the EXIT block is empty. Create all other blocks lazily. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 138 | |
| 139 | // Visit the statements and create the CFG. |
| 140 | if (CFGBlock* B = Visit(Statement)) { |
| 141 | // Finalize the last constructed block. This usually involves |
| 142 | // reversing the order of the statements in the block. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 143 | if (Block) FinishBlock(B); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 144 | |
| 145 | // Backpatch the gotos whose label -> block mappings we didn't know |
| 146 | // when we encountered them. |
| 147 | for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), |
| 148 | E = BackpatchBlocks.end(); I != E; ++I ) { |
| 149 | |
| 150 | CFGBlock* B = *I; |
| 151 | GotoStmt* G = cast<GotoStmt>(B->getTerminator()); |
| 152 | LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); |
| 153 | |
| 154 | // If there is no target for the goto, then we are looking at an |
| 155 | // incomplete AST. Handle this by not registering a successor. |
| 156 | if (LI == LabelMap.end()) continue; |
| 157 | |
| 158 | B->addSuccessor(LI->second); |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 159 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 160 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 161 | // Add successors to the Indirect Goto Dispatch block (if we have one). |
| 162 | if (CFGBlock* B = cfg->getIndirectGotoBlock()) |
| 163 | for (LabelSetTy::iterator I = AddressTakenLabels.begin(), |
| 164 | E = AddressTakenLabels.end(); I != E; ++I ) { |
| 165 | |
| 166 | // Lookup the target block. |
| 167 | LabelMapTy::iterator LI = LabelMap.find(*I); |
| 168 | |
| 169 | // If there is no target block that contains label, then we are looking |
| 170 | // at an incomplete AST. Handle this by not registering a successor. |
| 171 | if (LI == LabelMap.end()) continue; |
| 172 | |
| 173 | B->addSuccessor(LI->second); |
| 174 | } |
Ted Kremenek | 322f58d | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | 94b3316 | 2007-09-17 16:18:02 +0000 | [diff] [blame] | 176 | Succ = B; |
Ted Kremenek | 322f58d | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Create an empty entry block that has no predecessors. |
| 180 | cfg->setEntry(createBlock()); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | 322f58d | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 182 | // NULL out cfg so that repeated calls to the builder will fail and that |
| 183 | // the ownership of the constructed CFG is passed to the caller. |
| 184 | CFG* t = cfg; |
| 185 | cfg = NULL; |
| 186 | return t; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /// createBlock - Used to lazily create blocks that are connected |
| 190 | /// to the current (global) succcessor. |
| 191 | CFGBlock* CFGBuilder::createBlock(bool add_successor) { |
Ted Kremenek | 9438252 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 192 | CFGBlock* B = cfg->createBlock(); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 193 | if (add_successor && Succ) B->addSuccessor(Succ); |
| 194 | return B; |
| 195 | } |
| 196 | |
| 197 | /// FinishBlock - When the last statement has been added to the block, |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 198 | /// we must reverse the statements because they have been inserted |
| 199 | /// in reverse order. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 200 | void CFGBuilder::FinishBlock(CFGBlock* B) { |
| 201 | assert (B); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 202 | B->reverseStmts(); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 205 | /// addStmt - Used to add statements/expressions to the current CFGBlock |
| 206 | /// "Block". This method calls WalkAST on the passed statement to see if it |
| 207 | /// contains any short-circuit expressions. If so, it recursively creates |
| 208 | /// the necessary blocks for such expressions. It returns the "topmost" block |
| 209 | /// of the created blocks, or the original value of "Block" when this method |
| 210 | /// was called if no additional blocks are created. |
| 211 | CFGBlock* CFGBuilder::addStmt(Stmt* S) { |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 212 | if (!Block) Block = createBlock(); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 213 | return WalkAST(S,true); |
| 214 | } |
| 215 | |
| 216 | /// WalkAST - Used by addStmt to walk the subtree of a statement and |
Ted Kremenek | b49e1aa | 2007-08-28 18:14:37 +0000 | [diff] [blame] | 217 | /// add extra blocks for ternary operators, &&, and ||. We also |
| 218 | /// process "," and DeclStmts (which may contain nested control-flow). |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 219 | CFGBlock* CFGBuilder::WalkAST(Stmt* S, bool AlwaysAddStmt = false) { |
| 220 | switch (S->getStmtClass()) { |
| 221 | case Stmt::ConditionalOperatorClass: { |
| 222 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 223 | |
| 224 | // Create the confluence block that will "merge" the results |
| 225 | // of the ternary expression. |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 226 | CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock(); |
| 227 | ConfluenceBlock->appendStmt(C); |
Ted Kremenek | a651e0e | 2007-12-13 22:44:18 +0000 | [diff] [blame^] | 228 | FinishBlock(ConfluenceBlock); |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 229 | |
| 230 | // Create a block for the LHS expression if there is an LHS expression. |
| 231 | // A GCC extension allows LHS to be NULL, causing the condition to |
| 232 | // be the value that is returned instead. |
| 233 | // e.g: x ?: y is shorthand for: x ? x : y; |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 234 | Succ = ConfluenceBlock; |
| 235 | Block = NULL; |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 236 | CFGBlock* LHSBlock = NULL; |
| 237 | if (C->getLHS()) { |
| 238 | LHSBlock = Visit(C->getLHS()); |
| 239 | FinishBlock(LHSBlock); |
| 240 | Block = NULL; |
| 241 | } |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 243 | // Create the block for the RHS expression. |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 244 | Succ = ConfluenceBlock; |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 245 | CFGBlock* RHSBlock = Visit(C->getRHS()); |
Ted Kremenek | f50ec10 | 2007-09-11 21:29:43 +0000 | [diff] [blame] | 246 | FinishBlock(RHSBlock); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 247 | |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 248 | // Create the block that will contain the condition. |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 249 | Block = createBlock(false); |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 250 | |
| 251 | if (LHSBlock) |
| 252 | Block->addSuccessor(LHSBlock); |
| 253 | else { |
| 254 | // If we have no LHS expression, add the ConfluenceBlock as a direct |
| 255 | // successor for the block containing the condition. Moreover, |
| 256 | // we need to reverse the order of the predecessors in the |
| 257 | // ConfluenceBlock because the RHSBlock will have been added to |
| 258 | // the succcessors already, and we want the first predecessor to the |
| 259 | // the block containing the expression for the case when the ternary |
| 260 | // expression evaluates to true. |
| 261 | Block->addSuccessor(ConfluenceBlock); |
| 262 | assert (ConfluenceBlock->pred_size() == 2); |
| 263 | std::reverse(ConfluenceBlock->pred_begin(), |
| 264 | ConfluenceBlock->pred_end()); |
| 265 | } |
| 266 | |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 267 | Block->addSuccessor(RHSBlock); |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 268 | |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 269 | Block->setTerminator(C); |
| 270 | return addStmt(C->getCond()); |
| 271 | } |
Ted Kremenek | 49a436d | 2007-08-31 17:03:41 +0000 | [diff] [blame] | 272 | |
| 273 | case Stmt::ChooseExprClass: { |
| 274 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 275 | |
| 276 | CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock(); |
| 277 | ConfluenceBlock->appendStmt(C); |
| 278 | FinishBlock(ConfluenceBlock); |
| 279 | |
| 280 | Succ = ConfluenceBlock; |
| 281 | Block = NULL; |
| 282 | CFGBlock* LHSBlock = Visit(C->getLHS()); |
Ted Kremenek | f50ec10 | 2007-09-11 21:29:43 +0000 | [diff] [blame] | 283 | FinishBlock(LHSBlock); |
| 284 | |
Ted Kremenek | 49a436d | 2007-08-31 17:03:41 +0000 | [diff] [blame] | 285 | Succ = ConfluenceBlock; |
| 286 | Block = NULL; |
| 287 | CFGBlock* RHSBlock = Visit(C->getRHS()); |
Ted Kremenek | f50ec10 | 2007-09-11 21:29:43 +0000 | [diff] [blame] | 288 | FinishBlock(RHSBlock); |
Ted Kremenek | 49a436d | 2007-08-31 17:03:41 +0000 | [diff] [blame] | 289 | |
| 290 | Block = createBlock(false); |
| 291 | Block->addSuccessor(LHSBlock); |
| 292 | Block->addSuccessor(RHSBlock); |
| 293 | Block->setTerminator(C); |
| 294 | return addStmt(C->getCond()); |
| 295 | } |
Ted Kremenek | 7926f7c | 2007-08-28 16:18:58 +0000 | [diff] [blame] | 296 | |
Ted Kremenek | 8f54c1f | 2007-10-30 21:48:34 +0000 | [diff] [blame] | 297 | case Stmt::DeclStmtClass: { |
| 298 | ScopedDecl* D = cast<DeclStmt>(S)->getDecl(); |
| 299 | Block->appendStmt(S); |
| 300 | |
| 301 | StmtIterator I(D); |
| 302 | return WalkAST_VisitDeclSubExprs(I); |
| 303 | } |
Ted Kremenek | 15c27a8 | 2007-08-28 18:30:10 +0000 | [diff] [blame] | 304 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 305 | case Stmt::AddrLabelExprClass: { |
| 306 | AddrLabelExpr* A = cast<AddrLabelExpr>(S); |
| 307 | AddressTakenLabels.insert(A->getLabel()); |
| 308 | |
| 309 | if (AlwaysAddStmt) Block->appendStmt(S); |
| 310 | return Block; |
| 311 | } |
Ted Kremenek | f50ec10 | 2007-09-11 21:29:43 +0000 | [diff] [blame] | 312 | |
| 313 | case Stmt::CallExprClass: |
| 314 | return WalkAST_VisitCallExpr(cast<CallExpr>(S)); |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | 15c27a8 | 2007-08-28 18:30:10 +0000 | [diff] [blame] | 316 | case Stmt::StmtExprClass: |
| 317 | return WalkAST_VisitStmtExpr(cast<StmtExpr>(S)); |
Ted Kremenek | b49e1aa | 2007-08-28 18:14:37 +0000 | [diff] [blame] | 318 | |
Ted Kremenek | a651e0e | 2007-12-13 22:44:18 +0000 | [diff] [blame^] | 319 | case Stmt::UnaryOperatorClass: { |
| 320 | UnaryOperator* U = cast<UnaryOperator>(S); |
| 321 | |
| 322 | // sizeof(expressions). For such expressions, |
| 323 | // the subexpression is not really evaluated, so |
| 324 | // we don't care about control-flow within the sizeof. |
| 325 | if (U->getOpcode() == UnaryOperator::SizeOf) { |
| 326 | Block->appendStmt(S); |
| 327 | return Block; |
| 328 | } |
| 329 | |
| 330 | break; |
| 331 | } |
| 332 | |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 333 | case Stmt::BinaryOperatorClass: { |
| 334 | BinaryOperator* B = cast<BinaryOperator>(S); |
| 335 | |
| 336 | if (B->isLogicalOp()) { // && or || |
| 337 | CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock(); |
| 338 | ConfluenceBlock->appendStmt(B); |
| 339 | FinishBlock(ConfluenceBlock); |
| 340 | |
| 341 | // create the block evaluating the LHS |
| 342 | CFGBlock* LHSBlock = createBlock(false); |
| 343 | LHSBlock->addSuccessor(ConfluenceBlock); |
| 344 | LHSBlock->setTerminator(B); |
| 345 | |
| 346 | // create the block evaluating the RHS |
| 347 | Succ = ConfluenceBlock; |
| 348 | Block = NULL; |
| 349 | CFGBlock* RHSBlock = Visit(B->getRHS()); |
| 350 | LHSBlock->addSuccessor(RHSBlock); |
| 351 | |
| 352 | // Generate the blocks for evaluating the LHS. |
| 353 | Block = LHSBlock; |
| 354 | return addStmt(B->getLHS()); |
Ted Kremenek | b49e1aa | 2007-08-28 18:14:37 +0000 | [diff] [blame] | 355 | } |
| 356 | else if (B->getOpcode() == BinaryOperator::Comma) { // , |
| 357 | Block->appendStmt(B); |
| 358 | addStmt(B->getRHS()); |
| 359 | return addStmt(B->getLHS()); |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 360 | } |
Ted Kremenek | a651e0e | 2007-12-13 22:44:18 +0000 | [diff] [blame^] | 361 | |
| 362 | break; |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 365 | default: |
Ted Kremenek | a651e0e | 2007-12-13 22:44:18 +0000 | [diff] [blame^] | 366 | break; |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 367 | }; |
Ted Kremenek | a651e0e | 2007-12-13 22:44:18 +0000 | [diff] [blame^] | 368 | |
| 369 | if (AlwaysAddStmt) Block->appendStmt(S); |
| 370 | return WalkAST_VisitChildren(S); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Ted Kremenek | 8f54c1f | 2007-10-30 21:48:34 +0000 | [diff] [blame] | 373 | /// WalkAST_VisitDeclSubExprs - Utility method to handle Decls contained in |
| 374 | /// DeclStmts. Because the initialization code (and sometimes the |
| 375 | /// the type declarations) for DeclStmts can contain arbitrary expressions, |
| 376 | /// we must linearize declarations to handle arbitrary control-flow induced by |
| 377 | /// those expressions. |
| 378 | CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExprs(StmtIterator& I) { |
Ted Kremenek | d660322 | 2007-11-18 20:06:01 +0000 | [diff] [blame] | 379 | if (I == StmtIterator()) |
| 380 | return Block; |
| 381 | |
Ted Kremenek | 8f54c1f | 2007-10-30 21:48:34 +0000 | [diff] [blame] | 382 | Stmt* S = *I; |
| 383 | ++I; |
Ted Kremenek | d660322 | 2007-11-18 20:06:01 +0000 | [diff] [blame] | 384 | WalkAST_VisitDeclSubExprs(I); |
Ted Kremenek | 8f54c1f | 2007-10-30 21:48:34 +0000 | [diff] [blame] | 385 | |
| 386 | Block = addStmt(S); |
Ted Kremenek | b49e1aa | 2007-08-28 18:14:37 +0000 | [diff] [blame] | 387 | return Block; |
| 388 | } |
| 389 | |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 390 | /// WalkAST_VisitChildren - Utility method to call WalkAST on the |
| 391 | /// children of a Stmt. |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 392 | CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* S) { |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 393 | CFGBlock* B = Block; |
| 394 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end() ; |
| 395 | I != E; ++I) |
Ted Kremenek | 322f58d | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 396 | if (*I) B = WalkAST(*I); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 397 | |
| 398 | return B; |
| 399 | } |
| 400 | |
Ted Kremenek | 15c27a8 | 2007-08-28 18:30:10 +0000 | [diff] [blame] | 401 | /// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement |
| 402 | /// expressions (a GCC extension). |
| 403 | CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* S) { |
| 404 | Block->appendStmt(S); |
| 405 | return VisitCompoundStmt(S->getSubStmt()); |
| 406 | } |
| 407 | |
Ted Kremenek | f50ec10 | 2007-09-11 21:29:43 +0000 | [diff] [blame] | 408 | /// WalkAST_VisitCallExpr - Utility method to handle function calls that |
| 409 | /// are nested in expressions. The idea is that each function call should |
| 410 | /// appear as a distinct statement in the CFGBlock. |
| 411 | CFGBlock* CFGBuilder::WalkAST_VisitCallExpr(CallExpr* C) { |
| 412 | Block->appendStmt(C); |
| 413 | return WalkAST_VisitChildren(C); |
| 414 | } |
| 415 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 416 | /// VisitStmt - Handle statements with no branching control flow. |
| 417 | CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) { |
| 418 | // We cannot assume that we are in the middle of a basic block, since |
| 419 | // the CFG might only be constructed for this single statement. If |
| 420 | // we have no current basic block, just create one lazily. |
| 421 | if (!Block) Block = createBlock(); |
| 422 | |
| 423 | // Simply add the statement to the current block. We actually |
| 424 | // insert statements in reverse order; this order is reversed later |
| 425 | // when processing the containing element in the AST. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 426 | addStmt(Statement); |
| 427 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 428 | return Block; |
| 429 | } |
| 430 | |
| 431 | CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) { |
| 432 | return Block; |
| 433 | } |
| 434 | |
| 435 | CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) { |
| 436 | // The value returned from this function is the last created CFGBlock |
| 437 | // that represents the "entry" point for the translated AST node. |
Chris Lattner | 271f1a6 | 2007-09-27 15:15:46 +0000 | [diff] [blame] | 438 | CFGBlock* LastBlock = 0; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 439 | |
| 440 | for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(), |
| 441 | E = C->body_rend(); I != E; ++I ) |
| 442 | // Add the statement to the current block. |
| 443 | if (!(LastBlock=Visit(*I))) |
| 444 | return NULL; |
| 445 | |
| 446 | return LastBlock; |
| 447 | } |
| 448 | |
| 449 | CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) { |
| 450 | // We may see an if statement in the middle of a basic block, or |
| 451 | // it may be the first statement we are processing. In either case, |
| 452 | // we create a new basic block. First, we create the blocks for |
| 453 | // the then...else statements, and then we create the block containing |
| 454 | // the if statement. If we were in the middle of a block, we |
| 455 | // stop processing that block and reverse its statements. That block |
| 456 | // is then the implicit successor for the "then" and "else" clauses. |
| 457 | |
| 458 | // The block we were proccessing is now finished. Make it the |
| 459 | // successor block. |
| 460 | if (Block) { |
| 461 | Succ = Block; |
| 462 | FinishBlock(Block); |
| 463 | } |
| 464 | |
| 465 | // Process the false branch. NULL out Block so that the recursive |
| 466 | // call to Visit will create a new basic block. |
| 467 | // Null out Block so that all successor |
| 468 | CFGBlock* ElseBlock = Succ; |
| 469 | |
| 470 | if (Stmt* Else = I->getElse()) { |
| 471 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 472 | |
| 473 | // NULL out Block so that the recursive call to Visit will |
| 474 | // create a new basic block. |
| 475 | Block = NULL; |
Ted Kremenek | b6f7b72 | 2007-08-30 18:13:31 +0000 | [diff] [blame] | 476 | ElseBlock = Visit(Else); |
| 477 | |
| 478 | if (!ElseBlock) // Can occur when the Else body has all NullStmts. |
| 479 | ElseBlock = sv.get(); |
| 480 | else if (Block) |
| 481 | FinishBlock(ElseBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | // Process the true branch. NULL out Block so that the recursive |
| 485 | // call to Visit will create a new basic block. |
| 486 | // Null out Block so that all successor |
| 487 | CFGBlock* ThenBlock; |
| 488 | { |
| 489 | Stmt* Then = I->getThen(); |
| 490 | assert (Then); |
| 491 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 492 | Block = NULL; |
Ted Kremenek | b6f7b72 | 2007-08-30 18:13:31 +0000 | [diff] [blame] | 493 | ThenBlock = Visit(Then); |
| 494 | |
| 495 | if (!ThenBlock) // Can occur when the Then body has all NullStmts. |
| 496 | ThenBlock = sv.get(); |
| 497 | else if (Block) |
| 498 | FinishBlock(ThenBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // Now create a new block containing the if statement. |
| 502 | Block = createBlock(false); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 503 | |
| 504 | // Set the terminator of the new block to the If statement. |
| 505 | Block->setTerminator(I); |
| 506 | |
| 507 | // Now add the successors. |
| 508 | Block->addSuccessor(ThenBlock); |
| 509 | Block->addSuccessor(ElseBlock); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 510 | |
| 511 | // Add the condition as the last statement in the new block. This |
| 512 | // may create new blocks as the condition may contain control-flow. Any |
| 513 | // newly created blocks will be pointed to be "Block". |
| 514 | return addStmt(I->getCond()); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 515 | } |
Ted Kremenek | f50ec10 | 2007-09-11 21:29:43 +0000 | [diff] [blame] | 516 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 517 | |
| 518 | CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) { |
| 519 | // If we were in the middle of a block we stop processing that block |
| 520 | // and reverse its statements. |
| 521 | // |
| 522 | // NOTE: If a "return" appears in the middle of a block, this means |
| 523 | // that the code afterwards is DEAD (unreachable). We still |
| 524 | // keep a basic block for that code; a simple "mark-and-sweep" |
| 525 | // from the entry block will be able to report such dead |
| 526 | // blocks. |
| 527 | if (Block) FinishBlock(Block); |
| 528 | |
| 529 | // Create the new block. |
| 530 | Block = createBlock(false); |
| 531 | |
| 532 | // The Exit block is the only successor. |
| 533 | Block->addSuccessor(&cfg->getExit()); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 534 | |
| 535 | // Add the return statement to the block. This may create new blocks |
| 536 | // if R contains control-flow (short-circuit operations). |
| 537 | return addStmt(R); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) { |
| 541 | // Get the block of the labeled statement. Add it to our map. |
| 542 | CFGBlock* LabelBlock = Visit(L->getSubStmt()); |
Ted Kremenek | 16e4dc8 | 2007-08-30 18:20:57 +0000 | [diff] [blame] | 543 | |
| 544 | if (!LabelBlock) // This can happen when the body is empty, i.e. |
| 545 | LabelBlock=createBlock(); // scopes that only contains NullStmts. |
| 546 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 547 | assert (LabelMap.find(L) == LabelMap.end() && "label already in map"); |
| 548 | LabelMap[ L ] = LabelBlock; |
| 549 | |
| 550 | // Labels partition blocks, so this is the end of the basic block |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 551 | // we were processing (L is the block's label). Because this is |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 552 | // label (and we have already processed the substatement) there is no |
| 553 | // extra control-flow to worry about. |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 554 | LabelBlock->setLabel(L); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 555 | FinishBlock(LabelBlock); |
| 556 | |
| 557 | // We set Block to NULL to allow lazy creation of a new block |
| 558 | // (if necessary); |
| 559 | Block = NULL; |
| 560 | |
| 561 | // This block is now the implicit successor of other blocks. |
| 562 | Succ = LabelBlock; |
| 563 | |
| 564 | return LabelBlock; |
| 565 | } |
| 566 | |
| 567 | CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) { |
| 568 | // Goto is a control-flow statement. Thus we stop processing the |
| 569 | // current block and create a new one. |
| 570 | if (Block) FinishBlock(Block); |
| 571 | Block = createBlock(false); |
| 572 | Block->setTerminator(G); |
| 573 | |
| 574 | // If we already know the mapping to the label block add the |
| 575 | // successor now. |
| 576 | LabelMapTy::iterator I = LabelMap.find(G->getLabel()); |
| 577 | |
| 578 | if (I == LabelMap.end()) |
| 579 | // We will need to backpatch this block later. |
| 580 | BackpatchBlocks.push_back(Block); |
| 581 | else |
| 582 | Block->addSuccessor(I->second); |
| 583 | |
| 584 | return Block; |
| 585 | } |
| 586 | |
| 587 | CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) { |
| 588 | // "for" is a control-flow statement. Thus we stop processing the |
| 589 | // current block. |
| 590 | |
| 591 | CFGBlock* LoopSuccessor = NULL; |
| 592 | |
| 593 | if (Block) { |
| 594 | FinishBlock(Block); |
| 595 | LoopSuccessor = Block; |
| 596 | } |
| 597 | else LoopSuccessor = Succ; |
| 598 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 599 | // Because of short-circuit evaluation, the condition of the loop |
| 600 | // can span multiple basic blocks. Thus we need the "Entry" and "Exit" |
| 601 | // blocks that evaluate the condition. |
| 602 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 603 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
| 604 | |
| 605 | // Set the terminator for the "exit" condition block. |
| 606 | ExitConditionBlock->setTerminator(F); |
| 607 | |
| 608 | // Now add the actual condition to the condition block. Because the |
| 609 | // condition itself may contain control-flow, new blocks may be created. |
| 610 | if (Stmt* C = F->getCond()) { |
| 611 | Block = ExitConditionBlock; |
| 612 | EntryConditionBlock = addStmt(C); |
| 613 | if (Block) FinishBlock(EntryConditionBlock); |
| 614 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 615 | |
| 616 | // The condition block is the implicit successor for the loop body as |
| 617 | // well as any code above the loop. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 618 | Succ = EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 619 | |
| 620 | // Now create the loop body. |
| 621 | { |
| 622 | assert (F->getBody()); |
| 623 | |
| 624 | // Save the current values for Block, Succ, and continue and break targets |
| 625 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 626 | save_continue(ContinueTargetBlock), |
| 627 | save_break(BreakTargetBlock); |
| 628 | |
| 629 | // All continues within this loop should go to the condition block |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 630 | ContinueTargetBlock = EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 631 | |
| 632 | // All breaks should go to the code following the loop. |
| 633 | BreakTargetBlock = LoopSuccessor; |
| 634 | |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 635 | // Create a new block to contain the (bottom) of the loop body. |
| 636 | Block = NULL; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 637 | |
| 638 | // If we have increment code, insert it at the end of the body block. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 639 | if (Stmt* I = F->getInc()) Block = addStmt(I); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 640 | |
| 641 | // Now populate the body block, and in the process create new blocks |
| 642 | // as we walk the body of the loop. |
| 643 | CFGBlock* BodyBlock = Visit(F->getBody()); |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 644 | |
| 645 | if (!BodyBlock) |
| 646 | BodyBlock = ExitConditionBlock; // can happen for "for (...;...; ) ;" |
| 647 | else if (Block) |
| 648 | FinishBlock(BodyBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 649 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 650 | // This new body block is a successor to our "exit" condition block. |
| 651 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | // Link up the condition block with the code that follows the loop. |
| 655 | // (the false branch). |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 656 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
| 657 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 658 | // If the loop contains initialization, create a new block for those |
| 659 | // statements. This block can also contain statements that precede |
| 660 | // the loop. |
| 661 | if (Stmt* I = F->getInit()) { |
| 662 | Block = createBlock(); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 663 | return addStmt(I); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 664 | } |
| 665 | else { |
| 666 | // There is no loop initialization. We are thus basically a while |
| 667 | // loop. NULL out Block to force lazy block construction. |
| 668 | Block = NULL; |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 669 | return EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
| 673 | CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) { |
| 674 | // "while" is a control-flow statement. Thus we stop processing the |
| 675 | // current block. |
| 676 | |
| 677 | CFGBlock* LoopSuccessor = NULL; |
| 678 | |
| 679 | if (Block) { |
| 680 | FinishBlock(Block); |
| 681 | LoopSuccessor = Block; |
| 682 | } |
| 683 | else LoopSuccessor = Succ; |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 684 | |
| 685 | // Because of short-circuit evaluation, the condition of the loop |
| 686 | // can span multiple basic blocks. Thus we need the "Entry" and "Exit" |
| 687 | // blocks that evaluate the condition. |
| 688 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 689 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
| 690 | |
| 691 | // Set the terminator for the "exit" condition block. |
| 692 | ExitConditionBlock->setTerminator(W); |
| 693 | |
| 694 | // Now add the actual condition to the condition block. Because the |
| 695 | // condition itself may contain control-flow, new blocks may be created. |
| 696 | // Thus we update "Succ" after adding the condition. |
| 697 | if (Stmt* C = W->getCond()) { |
| 698 | Block = ExitConditionBlock; |
| 699 | EntryConditionBlock = addStmt(C); |
| 700 | if (Block) FinishBlock(EntryConditionBlock); |
| 701 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 702 | |
| 703 | // The condition block is the implicit successor for the loop body as |
| 704 | // well as any code above the loop. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 705 | Succ = EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 706 | |
| 707 | // Process the loop body. |
| 708 | { |
| 709 | assert (W->getBody()); |
| 710 | |
| 711 | // Save the current values for Block, Succ, and continue and break targets |
| 712 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 713 | save_continue(ContinueTargetBlock), |
| 714 | save_break(BreakTargetBlock); |
| 715 | |
| 716 | // All continues within this loop should go to the condition block |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 717 | ContinueTargetBlock = EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 718 | |
| 719 | // All breaks should go to the code following the loop. |
| 720 | BreakTargetBlock = LoopSuccessor; |
| 721 | |
| 722 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 723 | Block = NULL; |
| 724 | |
| 725 | // Create the body. The returned block is the entry to the loop body. |
| 726 | CFGBlock* BodyBlock = Visit(W->getBody()); |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 727 | |
| 728 | if (!BodyBlock) |
| 729 | BodyBlock = ExitConditionBlock; // can happen for "while(...) ;" |
| 730 | else if (Block) |
| 731 | FinishBlock(BodyBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 732 | |
| 733 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 734 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | // Link up the condition block with the code that follows the loop. |
| 738 | // (the false branch). |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 739 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 740 | |
| 741 | // There can be no more statements in the condition block |
| 742 | // since we loop back to this block. NULL out Block to force |
| 743 | // lazy creation of another block. |
| 744 | Block = NULL; |
| 745 | |
| 746 | // Return the condition block, which is the dominating block for the loop. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 747 | return EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) { |
| 751 | // "do...while" is a control-flow statement. Thus we stop processing the |
| 752 | // current block. |
| 753 | |
| 754 | CFGBlock* LoopSuccessor = NULL; |
| 755 | |
| 756 | if (Block) { |
| 757 | FinishBlock(Block); |
| 758 | LoopSuccessor = Block; |
| 759 | } |
| 760 | else LoopSuccessor = Succ; |
| 761 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 762 | // Because of short-circuit evaluation, the condition of the loop |
| 763 | // can span multiple basic blocks. Thus we need the "Entry" and "Exit" |
| 764 | // blocks that evaluate the condition. |
| 765 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 766 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
| 767 | |
| 768 | // Set the terminator for the "exit" condition block. |
| 769 | ExitConditionBlock->setTerminator(D); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 770 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 771 | // Now add the actual condition to the condition block. Because the |
| 772 | // condition itself may contain control-flow, new blocks may be created. |
| 773 | if (Stmt* C = D->getCond()) { |
| 774 | Block = ExitConditionBlock; |
| 775 | EntryConditionBlock = addStmt(C); |
| 776 | if (Block) FinishBlock(EntryConditionBlock); |
| 777 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 778 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 779 | // The condition block is the implicit successor for the loop body as |
| 780 | // well as any code above the loop. |
| 781 | Succ = EntryConditionBlock; |
| 782 | |
| 783 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 784 | // Process the loop body. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 785 | CFGBlock* BodyBlock = NULL; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 786 | { |
| 787 | assert (D->getBody()); |
| 788 | |
| 789 | // Save the current values for Block, Succ, and continue and break targets |
| 790 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 791 | save_continue(ContinueTargetBlock), |
| 792 | save_break(BreakTargetBlock); |
| 793 | |
| 794 | // All continues within this loop should go to the condition block |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 795 | ContinueTargetBlock = EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 796 | |
| 797 | // All breaks should go to the code following the loop. |
| 798 | BreakTargetBlock = LoopSuccessor; |
| 799 | |
| 800 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 801 | Block = NULL; |
| 802 | |
| 803 | // Create the body. The returned block is the entry to the loop body. |
| 804 | BodyBlock = Visit(D->getBody()); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 805 | |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 806 | if (!BodyBlock) |
| 807 | BodyBlock = ExitConditionBlock; // can happen for "do ; while(...)" |
| 808 | else if (Block) |
| 809 | FinishBlock(BodyBlock); |
| 810 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 811 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 812 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | // Link up the condition block with the code that follows the loop. |
| 816 | // (the false branch). |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 817 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 818 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 819 | // There can be no more statements in the body block(s) |
| 820 | // since we loop back to the body. NULL out Block to force |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 821 | // lazy creation of another block. |
| 822 | Block = NULL; |
| 823 | |
| 824 | // Return the loop body, which is the dominating block for the loop. |
| 825 | return BodyBlock; |
| 826 | } |
| 827 | |
| 828 | CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) { |
| 829 | // "continue" is a control-flow statement. Thus we stop processing the |
| 830 | // current block. |
| 831 | if (Block) FinishBlock(Block); |
| 832 | |
| 833 | // Now create a new block that ends with the continue statement. |
| 834 | Block = createBlock(false); |
| 835 | Block->setTerminator(C); |
| 836 | |
| 837 | // If there is no target for the continue, then we are looking at an |
| 838 | // incomplete AST. Handle this by not registering a successor. |
| 839 | if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock); |
| 840 | |
| 841 | return Block; |
| 842 | } |
| 843 | |
| 844 | CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) { |
| 845 | // "break" is a control-flow statement. Thus we stop processing the |
| 846 | // current block. |
| 847 | if (Block) FinishBlock(Block); |
| 848 | |
| 849 | // Now create a new block that ends with the continue statement. |
| 850 | Block = createBlock(false); |
| 851 | Block->setTerminator(B); |
| 852 | |
| 853 | // If there is no target for the break, then we are looking at an |
| 854 | // incomplete AST. Handle this by not registering a successor. |
| 855 | if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock); |
| 856 | |
| 857 | return Block; |
| 858 | } |
| 859 | |
| 860 | CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* S) { |
| 861 | // "switch" is a control-flow statement. Thus we stop processing the |
| 862 | // current block. |
| 863 | CFGBlock* SwitchSuccessor = NULL; |
| 864 | |
| 865 | if (Block) { |
| 866 | FinishBlock(Block); |
| 867 | SwitchSuccessor = Block; |
| 868 | } |
| 869 | else SwitchSuccessor = Succ; |
| 870 | |
| 871 | // Save the current "switch" context. |
| 872 | SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock), |
| 873 | save_break(BreakTargetBlock); |
| 874 | |
| 875 | // Create a new block that will contain the switch statement. |
| 876 | SwitchTerminatedBlock = createBlock(false); |
| 877 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 878 | // Now process the switch body. The code after the switch is the implicit |
| 879 | // successor. |
| 880 | Succ = SwitchSuccessor; |
| 881 | BreakTargetBlock = SwitchSuccessor; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 882 | |
| 883 | // When visiting the body, the case statements should automatically get |
| 884 | // linked up to the switch. We also don't keep a pointer to the body, |
| 885 | // since all control-flow from the switch goes to case/default statements. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 886 | assert (S->getBody() && "switch must contain a non-NULL body"); |
| 887 | Block = NULL; |
| 888 | CFGBlock *BodyBlock = Visit(S->getBody()); |
| 889 | if (Block) FinishBlock(BodyBlock); |
| 890 | |
| 891 | // Add the terminator and condition in the switch block. |
| 892 | SwitchTerminatedBlock->setTerminator(S); |
| 893 | assert (S->getCond() && "switch condition must be non-NULL"); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 894 | Block = SwitchTerminatedBlock; |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 895 | return addStmt(S->getCond()); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | CFGBlock* CFGBuilder::VisitSwitchCase(SwitchCase* S) { |
| 899 | // A SwitchCase is either a "default" or "case" statement. We handle |
| 900 | // both in the same way. They are essentially labels, so they are the |
| 901 | // first statement in a block. |
Ted Kremenek | 29ccaa1 | 2007-08-30 18:48:11 +0000 | [diff] [blame] | 902 | |
| 903 | if (S->getSubStmt()) Visit(S->getSubStmt()); |
| 904 | CFGBlock* CaseBlock = Block; |
| 905 | if (!CaseBlock) CaseBlock = createBlock(); |
| 906 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 907 | // Cases/Default statements partition block, so this is the top of |
| 908 | // the basic block we were processing (the case/default is the label). |
| 909 | CaseBlock->setLabel(S); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 910 | FinishBlock(CaseBlock); |
| 911 | |
| 912 | // Add this block to the list of successors for the block with the |
| 913 | // switch statement. |
| 914 | if (SwitchTerminatedBlock) SwitchTerminatedBlock->addSuccessor(CaseBlock); |
| 915 | |
| 916 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 917 | Block = NULL; |
| 918 | |
| 919 | // This block is now the implicit successor of other blocks. |
| 920 | Succ = CaseBlock; |
| 921 | |
| 922 | return CaseBlock; |
| 923 | } |
| 924 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 925 | CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) { |
| 926 | // Lazily create the indirect-goto dispatch block if there isn't one |
| 927 | // already. |
| 928 | CFGBlock* IBlock = cfg->getIndirectGotoBlock(); |
| 929 | |
| 930 | if (!IBlock) { |
| 931 | IBlock = createBlock(false); |
| 932 | cfg->setIndirectGotoBlock(IBlock); |
| 933 | } |
| 934 | |
| 935 | // IndirectGoto is a control-flow statement. Thus we stop processing the |
| 936 | // current block and create a new one. |
| 937 | if (Block) FinishBlock(Block); |
| 938 | Block = createBlock(false); |
| 939 | Block->setTerminator(I); |
| 940 | Block->addSuccessor(IBlock); |
| 941 | return addStmt(I->getTarget()); |
| 942 | } |
| 943 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 944 | |
Ted Kremenek | befef2f | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 945 | } // end anonymous namespace |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 946 | |
| 947 | /// createBlock - Constructs and adds a new CFGBlock to the CFG. The |
| 948 | /// block has no successors or predecessors. If this is the first block |
| 949 | /// created in the CFG, it is automatically set to be the Entry and Exit |
| 950 | /// of the CFG. |
Ted Kremenek | 9438252 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 951 | CFGBlock* CFG::createBlock() { |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 952 | bool first_block = begin() == end(); |
| 953 | |
| 954 | // Create the block. |
Ted Kremenek | 9438252 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 955 | Blocks.push_front(CFGBlock(NumBlockIDs++)); |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 956 | |
| 957 | // If this is the first block, set it as the Entry and Exit. |
| 958 | if (first_block) Entry = Exit = &front(); |
| 959 | |
| 960 | // Return the block. |
| 961 | return &front(); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 964 | /// buildCFG - Constructs a CFG from an AST. Ownership of the returned |
| 965 | /// CFG is returned to the caller. |
| 966 | CFG* CFG::buildCFG(Stmt* Statement) { |
| 967 | CFGBuilder Builder; |
| 968 | return Builder.buildCFG(Statement); |
| 969 | } |
| 970 | |
| 971 | /// reverseStmts - Reverses the orders of statements within a CFGBlock. |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 972 | void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); } |
| 973 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 974 | //===----------------------------------------------------------------------===// |
| 975 | // CFG: Queries for BlkExprs. |
| 976 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 977 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 978 | namespace { |
| 979 | typedef llvm::DenseMap<const Expr*,unsigned> BlkExprMapTy; |
| 980 | } |
| 981 | |
| 982 | static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) { |
| 983 | BlkExprMapTy* M = new BlkExprMapTy(); |
| 984 | |
| 985 | for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) |
| 986 | for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI) |
Ted Kremenek | 9a8385d | 2007-12-10 23:58:39 +0000 | [diff] [blame] | 987 | if (const Expr* E = dyn_cast<Expr>(*BI)) { |
| 988 | unsigned x = M->size(); |
| 989 | (*M)[E] = x; |
| 990 | } |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 991 | |
| 992 | return M; |
| 993 | } |
| 994 | |
| 995 | bool CFG::isBlkExpr(const Stmt* S) { |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 996 | assert (S != NULL); |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 997 | if (const Expr* E = dyn_cast<Expr>(S)) return getBlkExprNum(E); |
| 998 | else return true; // Statements are by default "block-level expressions." |
| 999 | } |
| 1000 | |
| 1001 | CFG::BlkExprNumTy CFG::getBlkExprNum(const Expr* E) { |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 1002 | assert(E != NULL); |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1003 | if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); } |
| 1004 | |
| 1005 | BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap); |
| 1006 | BlkExprMapTy::iterator I = M->find(E); |
| 1007 | |
| 1008 | if (I == M->end()) return CFG::BlkExprNumTy(); |
| 1009 | else return CFG::BlkExprNumTy(I->second); |
| 1010 | } |
| 1011 | |
| 1012 | unsigned CFG::getNumBlkExprs() { |
| 1013 | if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap)) |
| 1014 | return M->size(); |
| 1015 | else { |
| 1016 | // We assume callers interested in the number of BlkExprs will want |
| 1017 | // the map constructed if it doesn't already exist. |
| 1018 | BlkExprMap = (void*) PopulateBlkExprMap(*this); |
| 1019 | return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size(); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | CFG::~CFG() { |
| 1024 | delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap); |
| 1025 | } |
| 1026 | |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1027 | //===----------------------------------------------------------------------===// |
| 1028 | // CFG pretty printing |
| 1029 | //===----------------------------------------------------------------------===// |
| 1030 | |
Ted Kremenek | e8ee26b | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 1031 | namespace { |
| 1032 | |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1033 | class StmtPrinterHelper : public PrinterHelper { |
| 1034 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1035 | typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy; |
| 1036 | StmtMapTy StmtMap; |
| 1037 | signed CurrentBlock; |
| 1038 | unsigned CurrentStmt; |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1039 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1040 | public: |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1041 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1042 | StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) { |
| 1043 | for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) { |
| 1044 | unsigned j = 1; |
| 1045 | for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ; |
| 1046 | BI != BEnd; ++BI, ++j ) |
| 1047 | StmtMap[*BI] = std::make_pair(I->getBlockID(),j); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | virtual ~StmtPrinterHelper() {} |
| 1052 | |
| 1053 | void setBlockID(signed i) { CurrentBlock = i; } |
| 1054 | void setStmtID(unsigned i) { CurrentStmt = i; } |
| 1055 | |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1056 | virtual bool handledStmt(Stmt* S, std::ostream& OS) { |
| 1057 | |
| 1058 | StmtMapTy::iterator I = StmtMap.find(S); |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1059 | |
| 1060 | if (I == StmtMap.end()) |
| 1061 | return false; |
| 1062 | |
| 1063 | if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock |
| 1064 | && I->second.second == CurrentStmt) |
| 1065 | return false; |
| 1066 | |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1067 | OS << "[B" << I->second.first << "." << I->second.second << "]"; |
| 1068 | return true; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1069 | } |
| 1070 | }; |
| 1071 | |
| 1072 | class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint, |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1073 | void > |
| 1074 | { |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1075 | std::ostream& OS; |
| 1076 | StmtPrinterHelper* Helper; |
| 1077 | public: |
| 1078 | CFGBlockTerminatorPrint(std::ostream& os, StmtPrinterHelper* helper) |
| 1079 | : OS(os), Helper(helper) {} |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1080 | |
| 1081 | void VisitIfStmt(IfStmt* I) { |
| 1082 | OS << "if "; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1083 | I->getCond()->printPretty(OS,Helper); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1084 | OS << "\n"; |
| 1085 | } |
| 1086 | |
| 1087 | // Default case. |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1088 | void VisitStmt(Stmt* S) { S->printPretty(OS); } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1089 | |
| 1090 | void VisitForStmt(ForStmt* F) { |
| 1091 | OS << "for (" ; |
Ted Kremenek | 535bb20 | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 1092 | if (F->getInit()) OS << "..."; |
| 1093 | OS << "; "; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1094 | if (Stmt* C = F->getCond()) C->printPretty(OS,Helper); |
Ted Kremenek | 535bb20 | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 1095 | OS << "; "; |
| 1096 | if (F->getInc()) OS << "..."; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1097 | OS << ")\n"; |
| 1098 | } |
| 1099 | |
| 1100 | void VisitWhileStmt(WhileStmt* W) { |
| 1101 | OS << "while " ; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1102 | if (Stmt* C = W->getCond()) C->printPretty(OS,Helper); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1103 | OS << "\n"; |
| 1104 | } |
| 1105 | |
| 1106 | void VisitDoStmt(DoStmt* D) { |
| 1107 | OS << "do ... while "; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1108 | if (Stmt* C = D->getCond()) C->printPretty(OS,Helper); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1109 | OS << '\n'; |
| 1110 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1111 | |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1112 | void VisitSwitchStmt(SwitchStmt* S) { |
| 1113 | OS << "switch "; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1114 | S->getCond()->printPretty(OS,Helper); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1115 | OS << '\n'; |
| 1116 | } |
| 1117 | |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1118 | void VisitConditionalOperator(ConditionalOperator* C) { |
| 1119 | C->getCond()->printPretty(OS,Helper); |
| 1120 | OS << " ? ... : ...\n"; |
| 1121 | } |
| 1122 | |
Ted Kremenek | aeddbf6 | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 1123 | void VisitChooseExpr(ChooseExpr* C) { |
| 1124 | OS << "__builtin_choose_expr( "; |
| 1125 | C->getCond()->printPretty(OS,Helper); |
| 1126 | OS << " )\n"; |
| 1127 | } |
| 1128 | |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1129 | void VisitIndirectGotoStmt(IndirectGotoStmt* I) { |
| 1130 | OS << "goto *"; |
| 1131 | I->getTarget()->printPretty(OS,Helper); |
| 1132 | OS << '\n'; |
| 1133 | } |
| 1134 | |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1135 | void VisitBinaryOperator(BinaryOperator* B) { |
| 1136 | if (!B->isLogicalOp()) { |
| 1137 | VisitExpr(B); |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | B->getLHS()->printPretty(OS,Helper); |
| 1142 | |
| 1143 | switch (B->getOpcode()) { |
| 1144 | case BinaryOperator::LOr: |
| 1145 | OS << " || ...\n"; |
| 1146 | return; |
| 1147 | case BinaryOperator::LAnd: |
| 1148 | OS << " && ...\n"; |
| 1149 | return; |
| 1150 | default: |
| 1151 | assert(false && "Invalid logical operator."); |
| 1152 | } |
| 1153 | } |
| 1154 | |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 1155 | void VisitExpr(Expr* E) { |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1156 | E->printPretty(OS,Helper); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1157 | OS << '\n'; |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 1158 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1159 | }; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1160 | |
| 1161 | |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1162 | void print_stmt(std::ostream&OS, StmtPrinterHelper* Helper, Stmt* S) { |
| 1163 | if (Helper) { |
| 1164 | // special printing for statement-expressions. |
| 1165 | if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) { |
| 1166 | CompoundStmt* Sub = SE->getSubStmt(); |
| 1167 | |
| 1168 | if (Sub->child_begin() != Sub->child_end()) { |
Ted Kremenek | 60266e8 | 2007-08-31 22:47:06 +0000 | [diff] [blame] | 1169 | OS << "({ ... ; "; |
Ted Kremenek | 7a9d9d7 | 2007-10-29 20:41:04 +0000 | [diff] [blame] | 1170 | Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS); |
Ted Kremenek | 60266e8 | 2007-08-31 22:47:06 +0000 | [diff] [blame] | 1171 | OS << " })\n"; |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1172 | return; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | // special printing for comma expressions. |
| 1177 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
| 1178 | if (B->getOpcode() == BinaryOperator::Comma) { |
| 1179 | OS << "... , "; |
| 1180 | Helper->handledStmt(B->getRHS(),OS); |
| 1181 | OS << '\n'; |
| 1182 | return; |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | S->printPretty(OS, Helper); |
| 1188 | |
| 1189 | // Expressions need a newline. |
| 1190 | if (isa<Expr>(S)) OS << '\n'; |
| 1191 | } |
| 1192 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1193 | void print_block(std::ostream& OS, const CFG* cfg, const CFGBlock& B, |
| 1194 | StmtPrinterHelper* Helper, bool print_edges) { |
| 1195 | |
| 1196 | if (Helper) Helper->setBlockID(B.getBlockID()); |
| 1197 | |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1198 | // Print the header. |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1199 | OS << "\n [ B" << B.getBlockID(); |
| 1200 | |
| 1201 | if (&B == &cfg->getEntry()) |
| 1202 | OS << " (ENTRY) ]\n"; |
| 1203 | else if (&B == &cfg->getExit()) |
| 1204 | OS << " (EXIT) ]\n"; |
| 1205 | else if (&B == cfg->getIndirectGotoBlock()) |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1206 | OS << " (INDIRECT GOTO DISPATCH) ]\n"; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1207 | else |
| 1208 | OS << " ]\n"; |
| 1209 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1210 | // Print the label of this block. |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1211 | if (Stmt* S = const_cast<Stmt*>(B.getLabel())) { |
| 1212 | |
| 1213 | if (print_edges) |
| 1214 | OS << " "; |
| 1215 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1216 | if (LabelStmt* L = dyn_cast<LabelStmt>(S)) |
| 1217 | OS << L->getName(); |
| 1218 | else if (CaseStmt* C = dyn_cast<CaseStmt>(S)) { |
| 1219 | OS << "case "; |
| 1220 | C->getLHS()->printPretty(OS); |
| 1221 | if (C->getRHS()) { |
| 1222 | OS << " ... "; |
| 1223 | C->getRHS()->printPretty(OS); |
| 1224 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1225 | } |
Chris Lattner | f874c13 | 2007-09-16 19:11:53 +0000 | [diff] [blame] | 1226 | else if (isa<DefaultStmt>(S)) |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1227 | OS << "default"; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1228 | else |
| 1229 | assert(false && "Invalid label statement in CFGBlock."); |
| 1230 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1231 | OS << ":\n"; |
| 1232 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1233 | |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1234 | // Iterate through the statements in the block and print them. |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1235 | unsigned j = 1; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1236 | |
| 1237 | for (CFGBlock::const_iterator I = B.begin(), E = B.end() ; |
| 1238 | I != E ; ++I, ++j ) { |
| 1239 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1240 | // Print the statement # in the basic block and the statement itself. |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1241 | if (print_edges) |
| 1242 | OS << " "; |
| 1243 | |
| 1244 | OS << std::setw(3) << j << ": "; |
| 1245 | |
| 1246 | if (Helper) |
| 1247 | Helper->setStmtID(j); |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1248 | |
| 1249 | print_stmt(OS,Helper,*I); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1250 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1251 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1252 | // Print the terminator of this block. |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1253 | if (B.getTerminator()) { |
| 1254 | if (print_edges) |
| 1255 | OS << " "; |
| 1256 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1257 | OS << " T: "; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1258 | |
| 1259 | if (Helper) Helper->setBlockID(-1); |
| 1260 | |
| 1261 | CFGBlockTerminatorPrint TPrinter(OS,Helper); |
| 1262 | TPrinter.Visit(const_cast<Stmt*>(B.getTerminator())); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1263 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1264 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1265 | if (print_edges) { |
| 1266 | // Print the predecessors of this block. |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1267 | OS << " Predecessors (" << B.pred_size() << "):"; |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1268 | unsigned i = 0; |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1269 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1270 | for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end(); |
| 1271 | I != E; ++I, ++i) { |
| 1272 | |
| 1273 | if (i == 8 || (i-8) == 0) |
| 1274 | OS << "\n "; |
| 1275 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1276 | OS << " B" << (*I)->getBlockID(); |
| 1277 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1278 | |
| 1279 | OS << '\n'; |
| 1280 | |
| 1281 | // Print the successors of this block. |
| 1282 | OS << " Successors (" << B.succ_size() << "):"; |
| 1283 | i = 0; |
| 1284 | |
| 1285 | for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end(); |
| 1286 | I != E; ++I, ++i) { |
| 1287 | |
| 1288 | if (i == 8 || (i-8) % 10 == 0) |
| 1289 | OS << "\n "; |
| 1290 | |
| 1291 | OS << " B" << (*I)->getBlockID(); |
| 1292 | } |
| 1293 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1294 | OS << '\n'; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1295 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | } // end anonymous namespace |
| 1299 | |
| 1300 | /// dump - A simple pretty printer of a CFG that outputs to stderr. |
| 1301 | void CFG::dump() const { print(std::cerr); } |
| 1302 | |
| 1303 | /// print - A simple pretty printer of a CFG that outputs to an ostream. |
| 1304 | void CFG::print(std::ostream& OS) const { |
| 1305 | |
| 1306 | StmtPrinterHelper Helper(this); |
| 1307 | |
| 1308 | // Print the entry block. |
| 1309 | print_block(OS, this, getEntry(), &Helper, true); |
| 1310 | |
| 1311 | // Iterate through the CFGBlocks and print them one by one. |
| 1312 | for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
| 1313 | // Skip the entry block, because we already printed it. |
| 1314 | if (&(*I) == &getEntry() || &(*I) == &getExit()) |
| 1315 | continue; |
| 1316 | |
| 1317 | print_block(OS, this, *I, &Helper, true); |
| 1318 | } |
| 1319 | |
| 1320 | // Print the exit block. |
| 1321 | print_block(OS, this, getExit(), &Helper, true); |
| 1322 | } |
| 1323 | |
| 1324 | /// dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
| 1325 | void CFGBlock::dump(const CFG* cfg) const { print(std::cerr, cfg); } |
| 1326 | |
| 1327 | /// print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 1328 | /// Generally this will only be called from CFG::print. |
| 1329 | void CFGBlock::print(std::ostream& OS, const CFG* cfg) const { |
| 1330 | StmtPrinterHelper Helper(cfg); |
| 1331 | print_block(OS, cfg, *this, &Helper, true); |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1332 | } |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1333 | |
| 1334 | //===----------------------------------------------------------------------===// |
| 1335 | // CFG Graphviz Visualization |
| 1336 | //===----------------------------------------------------------------------===// |
| 1337 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1338 | |
| 1339 | #ifndef NDEBUG |
Chris Lattner | 0012351 | 2007-09-17 06:16:32 +0000 | [diff] [blame] | 1340 | static StmtPrinterHelper* GraphHelper; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1341 | #endif |
| 1342 | |
| 1343 | void CFG::viewCFG() const { |
| 1344 | #ifndef NDEBUG |
| 1345 | StmtPrinterHelper H(this); |
| 1346 | GraphHelper = &H; |
| 1347 | llvm::ViewGraph(this,"CFG"); |
| 1348 | GraphHelper = NULL; |
| 1349 | #else |
| 1350 | std::cerr << "CFG::viewCFG is only available in debug builds on " |
Hartmut Kaiser | 3860c11 | 2007-09-17 12:29:55 +0000 | [diff] [blame] | 1351 | << "systems with Graphviz or gv!\n"; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1352 | #endif |
| 1353 | } |
| 1354 | |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1355 | namespace llvm { |
| 1356 | template<> |
| 1357 | struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits { |
| 1358 | static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) { |
| 1359 | |
Hartmut Kaiser | bd250b4 | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 1360 | #ifndef NDEBUG |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1361 | std::ostringstream Out; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1362 | print_block(Out,Graph, *Node, GraphHelper, false); |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1363 | std::string OutStr = Out.str(); |
| 1364 | |
| 1365 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 1366 | |
| 1367 | // Process string output to make it nicer... |
| 1368 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 1369 | if (OutStr[i] == '\n') { // Left justify |
| 1370 | OutStr[i] = '\\'; |
| 1371 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 1372 | } |
| 1373 | |
| 1374 | return OutStr; |
Hartmut Kaiser | bd250b4 | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 1375 | #else |
| 1376 | return ""; |
| 1377 | #endif |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1378 | } |
| 1379 | }; |
| 1380 | } // end namespace llvm |