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