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