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" |
| 17 | #include <iostream> |
| 18 | #include <iomanip> |
| 19 | #include <algorithm> |
| 20 | using namespace clang; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | // SaveAndRestore - A utility class that uses RIIA to save and restore |
| 25 | // the value of a variable. |
| 26 | template<typename T> |
| 27 | struct SaveAndRestore { |
| 28 | SaveAndRestore(T& x) : X(x), old_value(x) {} |
| 29 | ~SaveAndRestore() { X = old_value; } |
| 30 | |
| 31 | T& X; |
| 32 | T old_value; |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | /// CFGBuilder - This class is implements CFG construction from an AST. |
| 37 | /// The builder is stateful: an instance of the builder should be used to only |
| 38 | /// construct a single CFG. |
| 39 | /// |
| 40 | /// Example usage: |
| 41 | /// |
| 42 | /// CFGBuilder builder; |
| 43 | /// CFG* cfg = builder.BuildAST(stmt1); |
| 44 | /// |
| 45 | class CFGBuilder { |
| 46 | CFG* cfg; |
| 47 | CFGBlock* Block; |
| 48 | CFGBlock* Exit; |
| 49 | CFGBlock* Succ; |
| 50 | unsigned NumBlocks; |
| 51 | |
| 52 | public: |
| 53 | explicit CFGBuilder() : cfg(NULL), Block(NULL), Exit(NULL), Succ(NULL), |
| 54 | NumBlocks(0) { |
| 55 | // Create an empty CFG. |
| 56 | cfg = new CFG(); |
| 57 | } |
| 58 | |
| 59 | ~CFGBuilder() { delete cfg; } |
| 60 | |
| 61 | /// buildCFG - Constructs a CFG from an AST (a Stmt*). The AST can |
| 62 | /// represent an arbitrary statement. Examples include a single expression |
| 63 | /// or a function body (compound statement). The ownership of the returned |
| 64 | /// CFG is transferred to the caller. If CFG construction fails, this method |
| 65 | /// returns NULL. |
| 66 | CFG* buildCFG(Stmt* Statement) { |
| 67 | if (!Statement) return NULL; |
| 68 | |
| 69 | assert (cfg && "CFGBuilder should only be used to construct one CFG"); |
| 70 | |
| 71 | // Create the exit block. |
| 72 | Block = createBlock(); |
| 73 | Exit = Block; |
| 74 | |
| 75 | // Visit the statements and create the CFG. |
| 76 | if (CFGBlock* B = visitStmt(Statement)) { |
| 77 | // Reverse the statements in the last constructed block. Statements |
| 78 | // are inserted into the blocks in reverse order. |
| 79 | B->reverseStmts(); |
| 80 | // NULL out cfg so that repeated calls |
| 81 | CFG* t = cfg; |
| 82 | cfg = NULL; |
| 83 | return t; |
| 84 | } |
| 85 | else { |
| 86 | // Error occured while building CFG: Delete the partially constructed CFG. |
| 87 | delete cfg; |
| 88 | cfg = NULL; |
| 89 | return NULL; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | |
| 95 | // createBlock - Used to lazily create blocks that are connected |
| 96 | // to the current (global) succcessor. |
| 97 | CFGBlock* createBlock( bool add_successor = true ) { |
| 98 | CFGBlock* B = cfg->createBlock(NumBlocks++); |
| 99 | if (add_successor && Succ) B->addSuccessor(Succ); |
| 100 | return B; |
| 101 | } |
| 102 | |
| 103 | // visitStmt - CFG construction is done via a recursive walk of an AST. |
| 104 | // We actually parse the AST in reverse order so that the successor |
| 105 | // of a basic block is constructed prior to its predecessor. This |
| 106 | // allows us to nicely capture implicit fall-throughs without extra |
| 107 | // basic blocks. |
| 108 | // |
| 109 | // The value returned from this function is the last created CFGBlock |
| 110 | // that represents the "entry" point for the translated AST node. |
| 111 | CFGBlock* visitStmt(Stmt* Statement) { |
| 112 | assert (Statement && "visitStmt does not accept NULL Stmt*"); |
| 113 | |
| 114 | switch (Statement->getStmtClass()) { |
| 115 | default: |
| 116 | assert (false && "statement case for CFGBuilder not yet implemented"); |
| 117 | return NULL; |
| 118 | |
| 119 | // Statements with no branching control flow. |
| 120 | case Stmt::NullStmtClass: |
| 121 | case Stmt::DeclStmtClass: |
| 122 | case Stmt::PreDefinedExprClass: |
| 123 | case Stmt::DeclRefExprClass: |
| 124 | case Stmt::IntegerLiteralClass: |
| 125 | case Stmt::FloatingLiteralClass: |
| 126 | case Stmt::StringLiteralClass: |
| 127 | case Stmt::CharacterLiteralClass: |
| 128 | case Stmt::ParenExprClass: |
| 129 | case Stmt::UnaryOperatorClass: |
| 130 | case Stmt::SizeOfAlignOfTypeExprClass: |
| 131 | case Stmt::ArraySubscriptExprClass: |
| 132 | case Stmt::CallExprClass: |
| 133 | case Stmt::BinaryOperatorClass: |
| 134 | case Stmt::ImplicitCastExprClass: |
| 135 | case Stmt::CompoundLiteralExprClass: |
| 136 | case Stmt::OCUVectorElementExprClass: |
| 137 | // We cannot assume that we are in the middle of a basic block, since |
| 138 | // the CFG might only be constructed for this single statement. If |
| 139 | // we have no current basic block, just create one lazily. |
| 140 | if (!Block) Block = createBlock(); |
| 141 | |
| 142 | // Simply add the statement to the current block. We actually |
| 143 | // insert statements in reverse order; this order is reversed later |
| 144 | // when processing the containing element in the AST. |
| 145 | Block->appendStmt(Statement); |
| 146 | break; |
| 147 | |
| 148 | case Stmt::CompoundStmtClass: { |
| 149 | // Iterate through the statements of the compound statement in reverse |
| 150 | // order. Because this statement may contain statements that have |
| 151 | // complicated control flow, the value of "Block" may change at any |
| 152 | // time. This means that statements in the compound statement will |
| 153 | // automatically be distributed across multiple basic blocks when |
| 154 | // necessary. |
| 155 | CompoundStmt* C = cast<CompoundStmt>(Statement); |
| 156 | |
| 157 | for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(), |
| 158 | E = C->body_rend(); I != E; ++I ) |
| 159 | // Add the statement to the current block. |
| 160 | if (!visitStmt(*I)) return NULL; |
| 161 | |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | case Stmt::IfStmtClass: { |
| 166 | IfStmt* I = cast<IfStmt>(Statement); |
| 167 | |
| 168 | // We may see an if statement in the middle of a basic block, or |
| 169 | // it may be the first statement we are processing. In either case, |
| 170 | // we create a new basic block. First, we create the blocks for |
| 171 | // the then...else statements, and then we create the block containing |
| 172 | // the if statement. If we were in the middle of a block, we |
| 173 | // stop processing that block and reverse its statements. That block |
| 174 | // is then the implicit successor for the "then" and "else" clauses. |
| 175 | |
| 176 | // The block we were proccessing is now finished. Make it the |
| 177 | // successor block. |
| 178 | if (Block) { |
| 179 | Succ = Block; |
| 180 | Block->reverseStmts(); |
| 181 | } |
| 182 | |
| 183 | // Process the false branch. NULL out Block so that the recursive |
| 184 | // call to visitStmt will create a new basic block. |
| 185 | // Null out Block so that all successor |
| 186 | CFGBlock* ElseBlock = Succ; |
| 187 | |
| 188 | if (Stmt* Else = I->getElse()) { |
| 189 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 190 | |
| 191 | // NULL out Block so that the recursive call to visitStmt will |
| 192 | // create a new basic block. |
| 193 | Block = NULL; |
| 194 | ElseBlock = visitStmt(Else); |
| 195 | if (!ElseBlock) return NULL; |
| 196 | ElseBlock->reverseStmts(); |
| 197 | } |
| 198 | |
| 199 | // Process the true branch. NULL out Block so that the recursive |
| 200 | // call to visitStmt will create a new basic block. |
| 201 | // Null out Block so that all successor |
| 202 | CFGBlock* ThenBlock; |
| 203 | { |
| 204 | Stmt* Then = I->getThen(); |
| 205 | assert (Then); |
| 206 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 207 | Block = NULL; |
| 208 | ThenBlock = visitStmt(Then); |
| 209 | if (!ThenBlock) return NULL; |
| 210 | ThenBlock->reverseStmts(); |
| 211 | } |
| 212 | |
| 213 | // Now create a new block containing the if statement. |
| 214 | Block = createBlock(false); |
| 215 | |
| 216 | // Add the condition as the last statement in the new block. |
| 217 | Block->appendStmt(I->getCond()); |
| 218 | |
| 219 | // Set the terminator of the new block to the If statement. |
| 220 | Block->setTerminator(I); |
| 221 | |
| 222 | // Now add the successors. |
| 223 | Block->addSuccessor(ThenBlock); |
| 224 | Block->addSuccessor(ElseBlock); |
| 225 | |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | case Stmt::ReturnStmtClass: { |
| 230 | ReturnStmt* R = cast<ReturnStmt>(Statement); |
| 231 | |
| 232 | // If we were in the middle of a block we stop processing that block |
| 233 | // and reverse its statements. |
| 234 | // |
| 235 | // NOTE: If a "return" appears in the middle of a block, this means |
| 236 | // that the code afterwards is DEAD (unreachable). We still |
| 237 | // keep a basic block for that code; a simple "mark-and-sweep" |
| 238 | // from the entry block will be able to report such dead |
| 239 | // blocks. |
| 240 | if (Block) Block->reverseStmts(); |
| 241 | |
| 242 | // Create the new block. |
| 243 | Block = createBlock(false); |
| 244 | |
| 245 | // The Exit block is the only successor. |
| 246 | Block->addSuccessor(Exit); |
| 247 | |
| 248 | // Add the return expression to the block. |
| 249 | Block->appendStmt(R); |
| 250 | |
| 251 | // Add the return statement itself to the block. |
| 252 | if (R->getRetValue()) Block->appendStmt(R->getRetValue()); |
| 253 | |
| 254 | break; |
| 255 | } |
| 256 | } // end dispatch on statement class |
| 257 | |
| 258 | return Block; |
| 259 | } |
| 260 | |
| 261 | }; |
| 262 | |
| 263 | // BuildCFG - A helper function that builds CFGs from ASTS. |
| 264 | CFG* CFG::BuildCFG( Stmt* Statement ) { |
| 265 | CFGBuilder Builder; |
| 266 | return Builder.buildCFG(Statement); |
| 267 | } |
| 268 | |
| 269 | // reverseStmts - A method that reverses the order of the statements within |
| 270 | // a CFGBlock. |
| 271 | void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); } |
| 272 | |
| 273 | // dump - A simple pretty printer of a CFG that outputs to stderr. |
| 274 | void CFG::dump() { print(std::cerr); } |
| 275 | |
| 276 | // print - A simple pretty printer of a CFG that outputs to an ostream. |
| 277 | void CFG::print(std::ostream& OS) { |
| 278 | // Iterate through the CFGBlocks and print them one by one. Specially |
| 279 | // designate the Entry and Exit blocks. |
| 280 | for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
| 281 | OS << "\n [ B" << I->getBlockID(); |
| 282 | if (&(*I) == getExit()) OS << " (EXIT) ]\n"; |
| 283 | else if (&(*I) == getEntry()) OS << " (ENTRY) ]\n"; |
| 284 | else OS << " ]\n"; |
| 285 | I->print(OS); |
| 286 | } |
| 287 | OS << "\n"; |
| 288 | } |
| 289 | |
| 290 | // dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
| 291 | void CFGBlock::dump() { print(std::cerr); } |
| 292 | |
| 293 | // print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 294 | // Generally this will only be called from CFG::print. |
| 295 | void CFGBlock::print(std::ostream& OS) { |
| 296 | |
| 297 | // Iterate through the statements in the block and print them. |
| 298 | OS << " ------------------------\n"; |
| 299 | unsigned j = 1; |
| 300 | for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) { |
| 301 | OS << " " << std::setw(3) << j << ": "; |
| 302 | (*I)->printPretty(OS); |
| 303 | if (isa<Expr>(*I)) OS << '\n'; |
| 304 | } |
| 305 | OS << " ------------------------\n"; |
| 306 | |
| 307 | // Print the predecessors of this block. |
| 308 | OS << " Predecessors (" << pred_size() << "):"; |
| 309 | unsigned i = 0; |
| 310 | for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) { |
| 311 | if (i == 8 || (i-8) == 0) { |
| 312 | OS << "\n "; |
| 313 | } |
| 314 | OS << " B" << (*I)->getBlockID(); |
| 315 | } |
| 316 | |
| 317 | // Print the terminator of this block. |
| 318 | OS << "\n Terminator: "; |
| 319 | if (ControlFlowStmt) { |
| 320 | switch (ControlFlowStmt->getStmtClass()) { |
| 321 | case Stmt::IfStmtClass: { |
| 322 | IfStmt* I = cast<IfStmt>(ControlFlowStmt); |
| 323 | OS << "if "; |
| 324 | I->getCond()->printPretty(std::cerr); |
| 325 | OS << "\n"; |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | case Stmt::ReturnStmtClass: { |
| 330 | ReturnStmt* R = cast<ReturnStmt>(ControlFlowStmt); |
| 331 | R->printPretty(std::cerr); |
| 332 | break; |
| 333 | } |
| 334 | |
| 335 | default: |
| 336 | assert(false && "terminator print not fully implemented"); |
| 337 | } |
| 338 | } |
| 339 | else OS << "<NULL>\n"; |
| 340 | |
| 341 | // Print the successors of this block. |
| 342 | OS << " Successors (" << succ_size() << "):"; |
| 343 | i = 0; |
| 344 | for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) { |
| 345 | if (i == 8 || (i-8) % 10 == 0) { |
| 346 | OS << "\n "; |
| 347 | } |
| 348 | OS << " B" << (*I)->getBlockID(); |
| 349 | } |
| 350 | OS << '\n'; |
| 351 | } |