Ted Kremenek | 97f7531 | 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 | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 19 | #include <iostream> |
| 20 | #include <iomanip> |
| 21 | #include <algorithm> |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
| 25 | |
Ted Kremenek | d6e5060 | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 26 | // SaveAndRestore - A utility class that uses RIIA to save and restore |
| 27 | // the value of a variable. |
| 28 | template<typename T> |
| 29 | struct SaveAndRestore { |
| 30 | SaveAndRestore(T& x) : X(x), old_value(x) {} |
| 31 | ~SaveAndRestore() { X = old_value; } |
| 32 | |
| 33 | T& X; |
| 34 | T old_value; |
| 35 | }; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 36 | |
| 37 | /// CFGBuilder - This class is implements CFG construction from an AST. |
| 38 | /// The builder is stateful: an instance of the builder should be used to only |
| 39 | /// construct a single CFG. |
| 40 | /// |
| 41 | /// Example usage: |
| 42 | /// |
| 43 | /// CFGBuilder builder; |
| 44 | /// CFG* cfg = builder.BuildAST(stmt1); |
| 45 | /// |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 46 | /// CFG construction is done via a recursive walk of an AST. |
| 47 | /// We actually parse the AST in reverse order so that the successor |
| 48 | /// of a basic block is constructed prior to its predecessor. This |
| 49 | /// allows us to nicely capture implicit fall-throughs without extra |
| 50 | /// basic blocks. |
| 51 | /// |
| 52 | class CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> { |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 53 | CFG* cfg; |
| 54 | CFGBlock* Block; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 55 | CFGBlock* Succ; |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 56 | CFGBlock* ContinueTargetBlock; |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 57 | CFGBlock* BreakTargetBlock; |
Ted Kremenek | e809ebf | 2007-08-23 18:43:24 +0000 | [diff] [blame] | 58 | CFGBlock* SwitchTerminatedBlock; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 59 | unsigned NumBlocks; |
| 60 | |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 61 | typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy; |
| 62 | LabelMapTy LabelMap; |
| 63 | |
Ted Kremenek | f5392b7 | 2007-08-22 15:40:58 +0000 | [diff] [blame] | 64 | typedef std::vector<CFGBlock*> BackpatchBlocksTy; |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 65 | BackpatchBlocksTy BackpatchBlocks; |
| 66 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 67 | public: |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 68 | explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL), |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 69 | ContinueTargetBlock(NULL), BreakTargetBlock(NULL), |
Ted Kremenek | e809ebf | 2007-08-23 18:43:24 +0000 | [diff] [blame] | 70 | SwitchTerminatedBlock(NULL), |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 71 | NumBlocks(0) { |
| 72 | // Create an empty CFG. |
| 73 | cfg = new CFG(); |
| 74 | } |
| 75 | |
| 76 | ~CFGBuilder() { delete cfg; } |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 78 | // buildCFG - Used by external clients to construct the CFG. |
| 79 | CFG* buildCFG(Stmt* Statement); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 80 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 81 | // Visitors to walk an AST and construct the CFG. Called by |
| 82 | // buildCFG. Do not call directly! |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 84 | CFGBlock* VisitStmt(Stmt* Statement); |
| 85 | CFGBlock* VisitNullStmt(NullStmt* Statement); |
| 86 | CFGBlock* VisitCompoundStmt(CompoundStmt* C); |
| 87 | CFGBlock* VisitIfStmt(IfStmt* I); |
| 88 | CFGBlock* VisitReturnStmt(ReturnStmt* R); |
| 89 | CFGBlock* VisitLabelStmt(LabelStmt* L); |
| 90 | CFGBlock* VisitGotoStmt(GotoStmt* G); |
| 91 | CFGBlock* VisitForStmt(ForStmt* F); |
| 92 | CFGBlock* VisitWhileStmt(WhileStmt* W); |
| 93 | CFGBlock* VisitDoStmt(DoStmt* D); |
| 94 | CFGBlock* VisitContinueStmt(ContinueStmt* C); |
| 95 | CFGBlock* VisitBreakStmt(BreakStmt* B); |
| 96 | CFGBlock* VisitSwitchStmt(SwitchStmt* S); |
| 97 | CFGBlock* VisitSwitchCase(SwitchCase* S); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 99 | private: |
| 100 | CFGBlock* createBlock(bool add_successor = true); |
Ted Kremenek | 65cfa56 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 101 | CFGBlock* addStmt(Stmt* S); |
| 102 | CFGBlock* WalkAST(Stmt* S, bool AlwaysAddStmt); |
| 103 | CFGBlock* WalkAST_VisitChildren(Stmt* S); |
Ted Kremenek | e822b62 | 2007-08-28 18:14:37 +0000 | [diff] [blame^] | 104 | CFGBlock* WalkAST_VisitVarDecl(VarDecl* D); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 105 | void FinishBlock(CFGBlock* B); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 107 | }; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 108 | |
| 109 | /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can |
| 110 | /// represent an arbitrary statement. Examples include a single expression |
| 111 | /// or a function body (compound statement). The ownership of the returned |
| 112 | /// CFG is transferred to the caller. If CFG construction fails, this method |
| 113 | /// returns NULL. |
| 114 | CFG* CFGBuilder::buildCFG(Stmt* Statement) { |
| 115 | if (!Statement) return NULL; |
| 116 | |
| 117 | // Create an empty block that will serve as the exit block for the CFG. |
| 118 | // Since this is the first block added to the CFG, it will be implicitly |
| 119 | // registered as the exit block. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 120 | Succ = createBlock(); |
| 121 | assert (Succ == &cfg->getExit()); |
| 122 | Block = NULL; // the EXIT block is empty. Create all other blocks lazily. |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 123 | |
| 124 | // Visit the statements and create the CFG. |
| 125 | if (CFGBlock* B = Visit(Statement)) { |
| 126 | // Finalize the last constructed block. This usually involves |
| 127 | // reversing the order of the statements in the block. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 128 | if (Block) FinishBlock(B); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 129 | |
| 130 | // Backpatch the gotos whose label -> block mappings we didn't know |
| 131 | // when we encountered them. |
| 132 | for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), |
| 133 | E = BackpatchBlocks.end(); I != E; ++I ) { |
| 134 | |
| 135 | CFGBlock* B = *I; |
| 136 | GotoStmt* G = cast<GotoStmt>(B->getTerminator()); |
| 137 | LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); |
| 138 | |
| 139 | // If there is no target for the goto, then we are looking at an |
| 140 | // incomplete AST. Handle this by not registering a successor. |
| 141 | if (LI == LabelMap.end()) continue; |
| 142 | |
| 143 | B->addSuccessor(LI->second); |
| 144 | } |
| 145 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 146 | if (B->pred_size() > 0) { |
| 147 | // create an empty entry block that has no predecessors. |
| 148 | Succ = B; |
| 149 | cfg->setEntry(createBlock()); |
| 150 | } |
| 151 | else cfg->setEntry(B); |
| 152 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 153 | // NULL out cfg so that repeated calls |
| 154 | CFG* t = cfg; |
| 155 | cfg = NULL; |
| 156 | return t; |
| 157 | } |
| 158 | else return NULL; |
| 159 | } |
| 160 | |
| 161 | /// createBlock - Used to lazily create blocks that are connected |
| 162 | /// to the current (global) succcessor. |
| 163 | CFGBlock* CFGBuilder::createBlock(bool add_successor) { |
| 164 | CFGBlock* B = cfg->createBlock(NumBlocks++); |
| 165 | if (add_successor && Succ) B->addSuccessor(Succ); |
| 166 | return B; |
| 167 | } |
| 168 | |
| 169 | /// FinishBlock - When the last statement has been added to the block, |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 170 | /// we must reverse the statements because they have been inserted |
| 171 | /// in reverse order. |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 172 | void CFGBuilder::FinishBlock(CFGBlock* B) { |
| 173 | assert (B); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 174 | B->reverseStmts(); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Ted Kremenek | 65cfa56 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 177 | /// addStmt - Used to add statements/expressions to the current CFGBlock |
| 178 | /// "Block". This method calls WalkAST on the passed statement to see if it |
| 179 | /// contains any short-circuit expressions. If so, it recursively creates |
| 180 | /// the necessary blocks for such expressions. It returns the "topmost" block |
| 181 | /// of the created blocks, or the original value of "Block" when this method |
| 182 | /// was called if no additional blocks are created. |
| 183 | CFGBlock* CFGBuilder::addStmt(Stmt* S) { |
| 184 | assert (Block); |
| 185 | return WalkAST(S,true); |
| 186 | } |
| 187 | |
| 188 | /// WalkAST - Used by addStmt to walk the subtree of a statement and |
Ted Kremenek | e822b62 | 2007-08-28 18:14:37 +0000 | [diff] [blame^] | 189 | /// add extra blocks for ternary operators, &&, and ||. We also |
| 190 | /// process "," and DeclStmts (which may contain nested control-flow). |
Ted Kremenek | 65cfa56 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 191 | CFGBlock* CFGBuilder::WalkAST(Stmt* S, bool AlwaysAddStmt = false) { |
| 192 | switch (S->getStmtClass()) { |
| 193 | case Stmt::ConditionalOperatorClass: { |
| 194 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 195 | |
| 196 | CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock(); |
| 197 | ConfluenceBlock->appendStmt(C); |
| 198 | FinishBlock(ConfluenceBlock); |
| 199 | |
| 200 | Succ = ConfluenceBlock; |
| 201 | Block = NULL; |
| 202 | CFGBlock* LHSBlock = Visit(C->getLHS()); |
| 203 | |
| 204 | Succ = ConfluenceBlock; |
| 205 | Block = NULL; |
| 206 | CFGBlock* RHSBlock = Visit(C->getRHS()); |
| 207 | |
| 208 | Block = createBlock(false); |
| 209 | Block->addSuccessor(LHSBlock); |
| 210 | Block->addSuccessor(RHSBlock); |
| 211 | Block->setTerminator(C); |
| 212 | return addStmt(C->getCond()); |
| 213 | } |
Ted Kremenek | 666a6af | 2007-08-28 16:18:58 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | e822b62 | 2007-08-28 18:14:37 +0000 | [diff] [blame^] | 215 | case Stmt::DeclStmtClass: |
| 216 | if (VarDecl* V = dyn_cast<VarDecl>(cast<DeclStmt>(S)->getDecl())) { |
| 217 | Block->appendStmt(S); |
| 218 | return WalkAST_VisitVarDecl(V); |
| 219 | } |
| 220 | else return Block; |
| 221 | |
Ted Kremenek | cfaae76 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 222 | case Stmt::BinaryOperatorClass: { |
| 223 | BinaryOperator* B = cast<BinaryOperator>(S); |
| 224 | |
| 225 | if (B->isLogicalOp()) { // && or || |
| 226 | CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock(); |
| 227 | ConfluenceBlock->appendStmt(B); |
| 228 | FinishBlock(ConfluenceBlock); |
| 229 | |
| 230 | // create the block evaluating the LHS |
| 231 | CFGBlock* LHSBlock = createBlock(false); |
| 232 | LHSBlock->addSuccessor(ConfluenceBlock); |
| 233 | LHSBlock->setTerminator(B); |
| 234 | |
| 235 | // create the block evaluating the RHS |
| 236 | Succ = ConfluenceBlock; |
| 237 | Block = NULL; |
| 238 | CFGBlock* RHSBlock = Visit(B->getRHS()); |
| 239 | LHSBlock->addSuccessor(RHSBlock); |
| 240 | |
| 241 | // Generate the blocks for evaluating the LHS. |
| 242 | Block = LHSBlock; |
| 243 | return addStmt(B->getLHS()); |
Ted Kremenek | e822b62 | 2007-08-28 18:14:37 +0000 | [diff] [blame^] | 244 | } |
| 245 | else if (B->getOpcode() == BinaryOperator::Comma) { // , |
| 246 | Block->appendStmt(B); |
| 247 | addStmt(B->getRHS()); |
| 248 | return addStmt(B->getLHS()); |
Ted Kremenek | cfaae76 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // Fall through to the default case. |
| 252 | } |
| 253 | |
Ted Kremenek | 65cfa56 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 254 | default: |
| 255 | if (AlwaysAddStmt) Block->appendStmt(S); |
| 256 | return WalkAST_VisitChildren(S); |
| 257 | }; |
| 258 | } |
| 259 | |
Ted Kremenek | e822b62 | 2007-08-28 18:14:37 +0000 | [diff] [blame^] | 260 | /// WalkAST_VisitVarDecl - Utility method to handle VarDecls contained in |
| 261 | /// DeclStmts. Because the initialization code for declarations can |
| 262 | /// contain arbitrary expressions, we must linearize declarations |
| 263 | /// to handle arbitrary control-flow induced by those expressions. |
| 264 | CFGBlock* CFGBuilder::WalkAST_VisitVarDecl(VarDecl* V) { |
| 265 | // We actually must parse the LAST declaration in a chain of |
| 266 | // declarations first, because we are building the CFG in reverse |
| 267 | // order. |
| 268 | if (Decl* D = V->getNextDeclarator()) |
| 269 | if (VarDecl* Next = cast<VarDecl>(D)) |
| 270 | Block = WalkAST_VisitVarDecl(Next); |
| 271 | |
| 272 | if (Expr* E = V->getInit()) |
| 273 | return addStmt(E); |
| 274 | |
| 275 | assert (Block); |
| 276 | return Block; |
| 277 | } |
| 278 | |
Ted Kremenek | 65cfa56 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 279 | /// WalkAST_VisitChildren - Utility method to call WalkAST on the |
| 280 | /// children of a Stmt. |
Ted Kremenek | cfaae76 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 281 | CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* S) { |
Ted Kremenek | 65cfa56 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 282 | CFGBlock* B = Block; |
| 283 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end() ; |
| 284 | I != E; ++I) |
| 285 | B = WalkAST(*I); |
| 286 | |
| 287 | return B; |
| 288 | } |
| 289 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 290 | /// VisitStmt - Handle statements with no branching control flow. |
| 291 | CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) { |
| 292 | // We cannot assume that we are in the middle of a basic block, since |
| 293 | // the CFG might only be constructed for this single statement. If |
| 294 | // we have no current basic block, just create one lazily. |
| 295 | if (!Block) Block = createBlock(); |
| 296 | |
| 297 | // Simply add the statement to the current block. We actually |
| 298 | // insert statements in reverse order; this order is reversed later |
| 299 | // when processing the containing element in the AST. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 300 | addStmt(Statement); |
| 301 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 302 | return Block; |
| 303 | } |
| 304 | |
| 305 | CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) { |
| 306 | return Block; |
| 307 | } |
| 308 | |
| 309 | CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) { |
| 310 | // The value returned from this function is the last created CFGBlock |
| 311 | // that represents the "entry" point for the translated AST node. |
| 312 | CFGBlock* LastBlock; |
| 313 | |
| 314 | for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(), |
| 315 | E = C->body_rend(); I != E; ++I ) |
| 316 | // Add the statement to the current block. |
| 317 | if (!(LastBlock=Visit(*I))) |
| 318 | return NULL; |
| 319 | |
| 320 | return LastBlock; |
| 321 | } |
| 322 | |
| 323 | CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) { |
| 324 | // We may see an if statement in the middle of a basic block, or |
| 325 | // it may be the first statement we are processing. In either case, |
| 326 | // we create a new basic block. First, we create the blocks for |
| 327 | // the then...else statements, and then we create the block containing |
| 328 | // the if statement. If we were in the middle of a block, we |
| 329 | // stop processing that block and reverse its statements. That block |
| 330 | // is then the implicit successor for the "then" and "else" clauses. |
| 331 | |
| 332 | // The block we were proccessing is now finished. Make it the |
| 333 | // successor block. |
| 334 | if (Block) { |
| 335 | Succ = Block; |
| 336 | FinishBlock(Block); |
| 337 | } |
| 338 | |
| 339 | // Process the false branch. NULL out Block so that the recursive |
| 340 | // call to Visit will create a new basic block. |
| 341 | // Null out Block so that all successor |
| 342 | CFGBlock* ElseBlock = Succ; |
| 343 | |
| 344 | if (Stmt* Else = I->getElse()) { |
| 345 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 346 | |
| 347 | // NULL out Block so that the recursive call to Visit will |
| 348 | // create a new basic block. |
| 349 | Block = NULL; |
| 350 | ElseBlock = Visit(Else); |
| 351 | if (!ElseBlock) return NULL; |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 352 | if (Block) FinishBlock(ElseBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Process the true branch. NULL out Block so that the recursive |
| 356 | // call to Visit will create a new basic block. |
| 357 | // Null out Block so that all successor |
| 358 | CFGBlock* ThenBlock; |
| 359 | { |
| 360 | Stmt* Then = I->getThen(); |
| 361 | assert (Then); |
| 362 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 363 | Block = NULL; |
| 364 | ThenBlock = Visit(Then); |
| 365 | if (!ThenBlock) return NULL; |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 366 | if (Block) FinishBlock(ThenBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | // Now create a new block containing the if statement. |
| 370 | Block = createBlock(false); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 371 | |
| 372 | // Set the terminator of the new block to the If statement. |
| 373 | Block->setTerminator(I); |
| 374 | |
| 375 | // Now add the successors. |
| 376 | Block->addSuccessor(ThenBlock); |
| 377 | Block->addSuccessor(ElseBlock); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 378 | |
| 379 | // Add the condition as the last statement in the new block. This |
| 380 | // may create new blocks as the condition may contain control-flow. Any |
| 381 | // newly created blocks will be pointed to be "Block". |
| 382 | return addStmt(I->getCond()); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) { |
| 386 | // If we were in the middle of a block we stop processing that block |
| 387 | // and reverse its statements. |
| 388 | // |
| 389 | // NOTE: If a "return" appears in the middle of a block, this means |
| 390 | // that the code afterwards is DEAD (unreachable). We still |
| 391 | // keep a basic block for that code; a simple "mark-and-sweep" |
| 392 | // from the entry block will be able to report such dead |
| 393 | // blocks. |
| 394 | if (Block) FinishBlock(Block); |
| 395 | |
| 396 | // Create the new block. |
| 397 | Block = createBlock(false); |
| 398 | |
| 399 | // The Exit block is the only successor. |
| 400 | Block->addSuccessor(&cfg->getExit()); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 401 | |
| 402 | // Add the return statement to the block. This may create new blocks |
| 403 | // if R contains control-flow (short-circuit operations). |
| 404 | return addStmt(R); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) { |
| 408 | // Get the block of the labeled statement. Add it to our map. |
| 409 | CFGBlock* LabelBlock = Visit(L->getSubStmt()); |
| 410 | assert (LabelBlock); |
| 411 | |
| 412 | assert (LabelMap.find(L) == LabelMap.end() && "label already in map"); |
| 413 | LabelMap[ L ] = LabelBlock; |
| 414 | |
| 415 | // Labels partition blocks, so this is the end of the basic block |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 416 | // we were processing (the label is the first statement). Add the label |
| 417 | // the to end (really the beginning) of the block. Because this is |
| 418 | // label (and we have already processed the substatement) there is no |
| 419 | // extra control-flow to worry about. |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 420 | LabelBlock->appendStmt(L); |
| 421 | FinishBlock(LabelBlock); |
| 422 | |
| 423 | // We set Block to NULL to allow lazy creation of a new block |
| 424 | // (if necessary); |
| 425 | Block = NULL; |
| 426 | |
| 427 | // This block is now the implicit successor of other blocks. |
| 428 | Succ = LabelBlock; |
| 429 | |
| 430 | return LabelBlock; |
| 431 | } |
| 432 | |
| 433 | CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) { |
| 434 | // Goto is a control-flow statement. Thus we stop processing the |
| 435 | // current block and create a new one. |
| 436 | if (Block) FinishBlock(Block); |
| 437 | Block = createBlock(false); |
| 438 | Block->setTerminator(G); |
| 439 | |
| 440 | // If we already know the mapping to the label block add the |
| 441 | // successor now. |
| 442 | LabelMapTy::iterator I = LabelMap.find(G->getLabel()); |
| 443 | |
| 444 | if (I == LabelMap.end()) |
| 445 | // We will need to backpatch this block later. |
| 446 | BackpatchBlocks.push_back(Block); |
| 447 | else |
| 448 | Block->addSuccessor(I->second); |
| 449 | |
| 450 | return Block; |
| 451 | } |
| 452 | |
| 453 | CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) { |
| 454 | // "for" is a control-flow statement. Thus we stop processing the |
| 455 | // current block. |
| 456 | |
| 457 | CFGBlock* LoopSuccessor = NULL; |
| 458 | |
| 459 | if (Block) { |
| 460 | FinishBlock(Block); |
| 461 | LoopSuccessor = Block; |
| 462 | } |
| 463 | else LoopSuccessor = Succ; |
| 464 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 465 | // Because of short-circuit evaluation, the condition of the loop |
| 466 | // can span multiple basic blocks. Thus we need the "Entry" and "Exit" |
| 467 | // blocks that evaluate the condition. |
| 468 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 469 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
| 470 | |
| 471 | // Set the terminator for the "exit" condition block. |
| 472 | ExitConditionBlock->setTerminator(F); |
| 473 | |
| 474 | // Now add the actual condition to the condition block. Because the |
| 475 | // condition itself may contain control-flow, new blocks may be created. |
| 476 | if (Stmt* C = F->getCond()) { |
| 477 | Block = ExitConditionBlock; |
| 478 | EntryConditionBlock = addStmt(C); |
| 479 | if (Block) FinishBlock(EntryConditionBlock); |
| 480 | } |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 481 | |
| 482 | // The condition block is the implicit successor for the loop body as |
| 483 | // well as any code above the loop. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 484 | Succ = EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 485 | |
| 486 | // Now create the loop body. |
| 487 | { |
| 488 | assert (F->getBody()); |
| 489 | |
| 490 | // Save the current values for Block, Succ, and continue and break targets |
| 491 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 492 | save_continue(ContinueTargetBlock), |
| 493 | save_break(BreakTargetBlock); |
| 494 | |
| 495 | // All continues within this loop should go to the condition block |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 496 | ContinueTargetBlock = EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 497 | |
| 498 | // All breaks should go to the code following the loop. |
| 499 | BreakTargetBlock = LoopSuccessor; |
| 500 | |
| 501 | // Create a new block to contain the (bottom) of the loop body. |
| 502 | Block = createBlock(); |
| 503 | |
| 504 | // If we have increment code, insert it at the end of the body block. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 505 | if (Stmt* I = F->getInc()) Block = addStmt(I); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 506 | |
| 507 | // Now populate the body block, and in the process create new blocks |
| 508 | // as we walk the body of the loop. |
| 509 | CFGBlock* BodyBlock = Visit(F->getBody()); |
| 510 | assert (BodyBlock); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 511 | if (Block) FinishBlock(BodyBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 512 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 513 | // This new body block is a successor to our "exit" condition block. |
| 514 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | // Link up the condition block with the code that follows the loop. |
| 518 | // (the false branch). |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 519 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
| 520 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 521 | // If the loop contains initialization, create a new block for those |
| 522 | // statements. This block can also contain statements that precede |
| 523 | // the loop. |
| 524 | if (Stmt* I = F->getInit()) { |
| 525 | Block = createBlock(); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 526 | return addStmt(I); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 527 | } |
| 528 | else { |
| 529 | // There is no loop initialization. We are thus basically a while |
| 530 | // loop. NULL out Block to force lazy block construction. |
| 531 | Block = NULL; |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 532 | return EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) { |
| 537 | // "while" is a control-flow statement. Thus we stop processing the |
| 538 | // current block. |
| 539 | |
| 540 | CFGBlock* LoopSuccessor = NULL; |
| 541 | |
| 542 | if (Block) { |
| 543 | FinishBlock(Block); |
| 544 | LoopSuccessor = Block; |
| 545 | } |
| 546 | else LoopSuccessor = Succ; |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 547 | |
| 548 | // Because of short-circuit evaluation, the condition of the loop |
| 549 | // can span multiple basic blocks. Thus we need the "Entry" and "Exit" |
| 550 | // blocks that evaluate the condition. |
| 551 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 552 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
| 553 | |
| 554 | // Set the terminator for the "exit" condition block. |
| 555 | ExitConditionBlock->setTerminator(W); |
| 556 | |
| 557 | // Now add the actual condition to the condition block. Because the |
| 558 | // condition itself may contain control-flow, new blocks may be created. |
| 559 | // Thus we update "Succ" after adding the condition. |
| 560 | if (Stmt* C = W->getCond()) { |
| 561 | Block = ExitConditionBlock; |
| 562 | EntryConditionBlock = addStmt(C); |
| 563 | if (Block) FinishBlock(EntryConditionBlock); |
| 564 | } |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 565 | |
| 566 | // The condition block is the implicit successor for the loop body as |
| 567 | // well as any code above the loop. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 568 | Succ = EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 569 | |
| 570 | // Process the loop body. |
| 571 | { |
| 572 | assert (W->getBody()); |
| 573 | |
| 574 | // Save the current values for Block, Succ, and continue and break targets |
| 575 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 576 | save_continue(ContinueTargetBlock), |
| 577 | save_break(BreakTargetBlock); |
| 578 | |
| 579 | // All continues within this loop should go to the condition block |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 580 | ContinueTargetBlock = EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 581 | |
| 582 | // All breaks should go to the code following the loop. |
| 583 | BreakTargetBlock = LoopSuccessor; |
| 584 | |
| 585 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 586 | Block = NULL; |
| 587 | |
| 588 | // Create the body. The returned block is the entry to the loop body. |
| 589 | CFGBlock* BodyBlock = Visit(W->getBody()); |
| 590 | assert (BodyBlock); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 591 | if (Block) FinishBlock(BodyBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 592 | |
| 593 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 594 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | // Link up the condition block with the code that follows the loop. |
| 598 | // (the false branch). |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 599 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 600 | |
| 601 | // There can be no more statements in the condition block |
| 602 | // since we loop back to this block. NULL out Block to force |
| 603 | // lazy creation of another block. |
| 604 | Block = NULL; |
| 605 | |
| 606 | // Return the condition block, which is the dominating block for the loop. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 607 | return EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) { |
| 611 | // "do...while" is a control-flow statement. Thus we stop processing the |
| 612 | // current block. |
| 613 | |
| 614 | CFGBlock* LoopSuccessor = NULL; |
| 615 | |
| 616 | if (Block) { |
| 617 | FinishBlock(Block); |
| 618 | LoopSuccessor = Block; |
| 619 | } |
| 620 | else LoopSuccessor = Succ; |
| 621 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 622 | // Because of short-circuit evaluation, the condition of the loop |
| 623 | // can span multiple basic blocks. Thus we need the "Entry" and "Exit" |
| 624 | // blocks that evaluate the condition. |
| 625 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 626 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
| 627 | |
| 628 | // Set the terminator for the "exit" condition block. |
| 629 | ExitConditionBlock->setTerminator(D); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 630 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 631 | // Now add the actual condition to the condition block. Because the |
| 632 | // condition itself may contain control-flow, new blocks may be created. |
| 633 | if (Stmt* C = D->getCond()) { |
| 634 | Block = ExitConditionBlock; |
| 635 | EntryConditionBlock = addStmt(C); |
| 636 | if (Block) FinishBlock(EntryConditionBlock); |
| 637 | } |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 638 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 639 | // The condition block is the implicit successor for the loop body as |
| 640 | // well as any code above the loop. |
| 641 | Succ = EntryConditionBlock; |
| 642 | |
| 643 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 644 | // Process the loop body. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 645 | CFGBlock* BodyBlock = NULL; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 646 | { |
| 647 | assert (D->getBody()); |
| 648 | |
| 649 | // Save the current values for Block, Succ, and continue and break targets |
| 650 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 651 | save_continue(ContinueTargetBlock), |
| 652 | save_break(BreakTargetBlock); |
| 653 | |
| 654 | // All continues within this loop should go to the condition block |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 655 | ContinueTargetBlock = EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 656 | |
| 657 | // All breaks should go to the code following the loop. |
| 658 | BreakTargetBlock = LoopSuccessor; |
| 659 | |
| 660 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 661 | Block = NULL; |
| 662 | |
| 663 | // Create the body. The returned block is the entry to the loop body. |
| 664 | BodyBlock = Visit(D->getBody()); |
| 665 | assert (BodyBlock); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 666 | if (Block) FinishBlock(BodyBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 667 | |
| 668 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 669 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | // Link up the condition block with the code that follows the loop. |
| 673 | // (the false branch). |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 674 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 675 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 676 | // There can be no more statements in the body block(s) |
| 677 | // since we loop back to the body. NULL out Block to force |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 678 | // lazy creation of another block. |
| 679 | Block = NULL; |
| 680 | |
| 681 | // Return the loop body, which is the dominating block for the loop. |
| 682 | return BodyBlock; |
| 683 | } |
| 684 | |
| 685 | CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) { |
| 686 | // "continue" is a control-flow statement. Thus we stop processing the |
| 687 | // current block. |
| 688 | if (Block) FinishBlock(Block); |
| 689 | |
| 690 | // Now create a new block that ends with the continue statement. |
| 691 | Block = createBlock(false); |
| 692 | Block->setTerminator(C); |
| 693 | |
| 694 | // If there is no target for the continue, then we are looking at an |
| 695 | // incomplete AST. Handle this by not registering a successor. |
| 696 | if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock); |
| 697 | |
| 698 | return Block; |
| 699 | } |
| 700 | |
| 701 | CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) { |
| 702 | // "break" is a control-flow statement. Thus we stop processing the |
| 703 | // current block. |
| 704 | if (Block) FinishBlock(Block); |
| 705 | |
| 706 | // Now create a new block that ends with the continue statement. |
| 707 | Block = createBlock(false); |
| 708 | Block->setTerminator(B); |
| 709 | |
| 710 | // If there is no target for the break, then we are looking at an |
| 711 | // incomplete AST. Handle this by not registering a successor. |
| 712 | if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock); |
| 713 | |
| 714 | return Block; |
| 715 | } |
| 716 | |
| 717 | CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* S) { |
| 718 | // "switch" is a control-flow statement. Thus we stop processing the |
| 719 | // current block. |
| 720 | CFGBlock* SwitchSuccessor = NULL; |
| 721 | |
| 722 | if (Block) { |
| 723 | FinishBlock(Block); |
| 724 | SwitchSuccessor = Block; |
| 725 | } |
| 726 | else SwitchSuccessor = Succ; |
| 727 | |
| 728 | // Save the current "switch" context. |
| 729 | SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock), |
| 730 | save_break(BreakTargetBlock); |
| 731 | |
| 732 | // Create a new block that will contain the switch statement. |
| 733 | SwitchTerminatedBlock = createBlock(false); |
| 734 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 735 | // Now process the switch body. The code after the switch is the implicit |
| 736 | // successor. |
| 737 | Succ = SwitchSuccessor; |
| 738 | BreakTargetBlock = SwitchSuccessor; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 739 | |
| 740 | // When visiting the body, the case statements should automatically get |
| 741 | // linked up to the switch. We also don't keep a pointer to the body, |
| 742 | // since all control-flow from the switch goes to case/default statements. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 743 | assert (S->getBody() && "switch must contain a non-NULL body"); |
| 744 | Block = NULL; |
| 745 | CFGBlock *BodyBlock = Visit(S->getBody()); |
| 746 | if (Block) FinishBlock(BodyBlock); |
| 747 | |
| 748 | // Add the terminator and condition in the switch block. |
| 749 | SwitchTerminatedBlock->setTerminator(S); |
| 750 | assert (S->getCond() && "switch condition must be non-NULL"); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 751 | Block = SwitchTerminatedBlock; |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 752 | return addStmt(S->getCond()); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | CFGBlock* CFGBuilder::VisitSwitchCase(SwitchCase* S) { |
| 756 | // A SwitchCase is either a "default" or "case" statement. We handle |
| 757 | // both in the same way. They are essentially labels, so they are the |
| 758 | // first statement in a block. |
| 759 | CFGBlock* CaseBlock = Visit(S->getSubStmt()); |
| 760 | assert (CaseBlock); |
| 761 | |
| 762 | // Cases/Default statements parition block, so this is the top of |
| 763 | // the basic block we were processing (the case/default is the first stmt). |
| 764 | CaseBlock->appendStmt(S); |
| 765 | FinishBlock(CaseBlock); |
| 766 | |
| 767 | // Add this block to the list of successors for the block with the |
| 768 | // switch statement. |
| 769 | if (SwitchTerminatedBlock) SwitchTerminatedBlock->addSuccessor(CaseBlock); |
| 770 | |
| 771 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 772 | Block = NULL; |
| 773 | |
| 774 | // This block is now the implicit successor of other blocks. |
| 775 | Succ = CaseBlock; |
| 776 | |
| 777 | return CaseBlock; |
| 778 | } |
| 779 | |
| 780 | |
Ted Kremenek | d6e5060 | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 781 | } // end anonymous namespace |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 782 | |
| 783 | /// createBlock - Constructs and adds a new CFGBlock to the CFG. The |
| 784 | /// block has no successors or predecessors. If this is the first block |
| 785 | /// created in the CFG, it is automatically set to be the Entry and Exit |
| 786 | /// of the CFG. |
| 787 | CFGBlock* CFG::createBlock(unsigned blockID) { |
| 788 | bool first_block = begin() == end(); |
| 789 | |
| 790 | // Create the block. |
| 791 | Blocks.push_front(CFGBlock(blockID)); |
| 792 | |
| 793 | // If this is the first block, set it as the Entry and Exit. |
| 794 | if (first_block) Entry = Exit = &front(); |
| 795 | |
| 796 | // Return the block. |
| 797 | return &front(); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 800 | /// buildCFG - Constructs a CFG from an AST. Ownership of the returned |
| 801 | /// CFG is returned to the caller. |
| 802 | CFG* CFG::buildCFG(Stmt* Statement) { |
| 803 | CFGBuilder Builder; |
| 804 | return Builder.buildCFG(Statement); |
| 805 | } |
| 806 | |
| 807 | /// reverseStmts - Reverses the orders of statements within a CFGBlock. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 808 | void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); } |
| 809 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 810 | /// dump - A simple pretty printer of a CFG that outputs to stderr. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 811 | void CFG::dump() { print(std::cerr); } |
| 812 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 813 | /// print - A simple pretty printer of a CFG that outputs to an ostream. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 814 | void CFG::print(std::ostream& OS) { |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 815 | // Print the Entry block. |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 816 | if (begin() != end()) { |
| 817 | CFGBlock& Entry = getEntry(); |
| 818 | OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n"; |
| 819 | Entry.print(OS); |
| 820 | } |
| 821 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 822 | // Iterate through the CFGBlocks and print them one by one. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 823 | for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 824 | // Skip the entry block, because we already printed it. |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 825 | if (&(*I) == &getEntry() || &(*I) == &getExit()) continue; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 826 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 827 | OS << "\n [ B" << I->getBlockID() << " ]\n"; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 828 | I->print(OS); |
| 829 | } |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 830 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 831 | // Print the Exit Block. |
| 832 | if (begin() != end()) { |
| 833 | CFGBlock& Exit = getExit(); |
| 834 | OS << "\n [ B" << Exit.getBlockID() << " (EXIT) ]\n"; |
| 835 | Exit.print(OS); |
| 836 | } |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 837 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 838 | OS << "\n"; |
| 839 | } |
| 840 | |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 841 | |
| 842 | namespace { |
| 843 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 844 | class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint, |
| 845 | void > { |
| 846 | std::ostream& OS; |
| 847 | public: |
| 848 | CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {} |
| 849 | |
| 850 | void VisitIfStmt(IfStmt* I) { |
| 851 | OS << "if "; |
| 852 | I->getCond()->printPretty(std::cerr); |
| 853 | OS << "\n"; |
| 854 | } |
| 855 | |
| 856 | // Default case. |
| 857 | void VisitStmt(Stmt* S) { S->printPretty(OS); } |
| 858 | |
| 859 | void VisitForStmt(ForStmt* F) { |
| 860 | OS << "for (" ; |
| 861 | if (Stmt* I = F->getInit()) I->printPretty(OS); |
| 862 | OS << " ; "; |
| 863 | if (Stmt* C = F->getCond()) C->printPretty(OS); |
| 864 | OS << " ; "; |
| 865 | if (Stmt* I = F->getInc()) I->printPretty(OS); |
| 866 | OS << ")\n"; |
| 867 | } |
| 868 | |
| 869 | void VisitWhileStmt(WhileStmt* W) { |
| 870 | OS << "while " ; |
| 871 | if (Stmt* C = W->getCond()) C->printPretty(OS); |
| 872 | OS << "\n"; |
| 873 | } |
| 874 | |
| 875 | void VisitDoStmt(DoStmt* D) { |
| 876 | OS << "do ... while "; |
| 877 | if (Stmt* C = D->getCond()) C->printPretty(OS); |
Ted Kremenek | 65cfa56 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 878 | OS << '\n'; |
| 879 | } |
| 880 | |
| 881 | void VisitSwitchStmt(SwitchStmt* S) { |
| 882 | OS << "switch "; |
| 883 | S->getCond()->printPretty(OS); |
| 884 | OS << '\n'; |
| 885 | } |
| 886 | |
Ted Kremenek | cfaae76 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 887 | void VisitExpr(Expr* E) { |
| 888 | E->printPretty(OS); |
Ted Kremenek | 65cfa56 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 889 | OS << '\n'; |
Ted Kremenek | cfaae76 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 890 | } |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 891 | }; |
| 892 | } // end anonymous namespace |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 893 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 894 | /// dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 895 | void CFGBlock::dump() { print(std::cerr); } |
| 896 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 897 | /// print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 898 | /// Generally this will only be called from CFG::print. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 899 | void CFGBlock::print(std::ostream& OS) { |
| 900 | |
| 901 | // Iterate through the statements in the block and print them. |
| 902 | OS << " ------------------------\n"; |
| 903 | unsigned j = 1; |
| 904 | for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) { |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 905 | // Print the statement # in the basic block. |
| 906 | OS << " " << std::setw(3) << j << ": "; |
| 907 | |
| 908 | // Print the statement/expression. |
| 909 | Stmt* S = *I; |
| 910 | |
| 911 | if (LabelStmt* L = dyn_cast<LabelStmt>(S)) |
| 912 | OS << L->getName() << ": (LABEL)\n"; |
| 913 | else |
| 914 | (*I)->printPretty(OS); |
| 915 | |
| 916 | // Expressions need a newline. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 917 | if (isa<Expr>(*I)) OS << '\n'; |
| 918 | } |
| 919 | OS << " ------------------------\n"; |
| 920 | |
| 921 | // Print the predecessors of this block. |
| 922 | OS << " Predecessors (" << pred_size() << "):"; |
| 923 | unsigned i = 0; |
| 924 | for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) { |
| 925 | if (i == 8 || (i-8) == 0) { |
| 926 | OS << "\n "; |
| 927 | } |
| 928 | OS << " B" << (*I)->getBlockID(); |
| 929 | } |
| 930 | |
| 931 | // Print the terminator of this block. |
| 932 | OS << "\n Terminator: "; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 933 | if (ControlFlowStmt) |
| 934 | CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt); |
| 935 | else |
| 936 | OS << "<NULL>\n"; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 937 | |
| 938 | // Print the successors of this block. |
| 939 | OS << " Successors (" << succ_size() << "):"; |
| 940 | i = 0; |
| 941 | for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) { |
| 942 | if (i == 8 || (i-8) % 10 == 0) { |
| 943 | OS << "\n "; |
| 944 | } |
| 945 | OS << " B" << (*I)->getBlockID(); |
| 946 | } |
| 947 | OS << '\n'; |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 948 | } |