blob: 68471ac955ae69e5c4d379e9c644040d631e6763 [file] [log] [blame]
Ted Kremenek97f75312007-08-21 21:42:03 +00001//===--- 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 Kremenek95e854d2007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenekc5de2222007-08-21 23:26:17 +000018#include "llvm/ADT/DenseMap.h"
Ted Kremenek97f75312007-08-21 21:42:03 +000019#include <iostream>
20#include <iomanip>
21#include <algorithm>
22using namespace clang;
23
24namespace {
25
Ted Kremenekd6e50602007-08-23 21:26:19 +000026// SaveAndRestore - A utility class that uses RIIA to save and restore
27// the value of a variable.
28template<typename T>
29struct 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 Kremenek97f75312007-08-21 21:42:03 +000036
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 Kremenek95e854d2007-08-21 22:06:14 +000046/// 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///
52class CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenek97f75312007-08-21 21:42:03 +000053 CFG* cfg;
54 CFGBlock* Block;
Ted Kremenek97f75312007-08-21 21:42:03 +000055 CFGBlock* Succ;
Ted Kremenekf511d672007-08-22 21:36:54 +000056 CFGBlock* ContinueTargetBlock;
Ted Kremenekf308d372007-08-22 21:51:58 +000057 CFGBlock* BreakTargetBlock;
Ted Kremeneke809ebf2007-08-23 18:43:24 +000058 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek97f75312007-08-21 21:42:03 +000059 unsigned NumBlocks;
60
Ted Kremenekc5de2222007-08-21 23:26:17 +000061 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
62 LabelMapTy LabelMap;
63
Ted Kremenekf5392b72007-08-22 15:40:58 +000064 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenekc5de2222007-08-21 23:26:17 +000065 BackpatchBlocksTy BackpatchBlocks;
66
Ted Kremenek97f75312007-08-21 21:42:03 +000067public:
Ted Kremenek4db5b452007-08-23 16:51:22 +000068 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenekf308d372007-08-22 21:51:58 +000069 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremeneke809ebf2007-08-23 18:43:24 +000070 SwitchTerminatedBlock(NULL),
Ted Kremenek97f75312007-08-21 21:42:03 +000071 NumBlocks(0) {
72 // Create an empty CFG.
73 cfg = new CFG();
74 }
75
76 ~CFGBuilder() { delete cfg; }
Ted Kremenek97f75312007-08-21 21:42:03 +000077
Ted Kremenek73543912007-08-23 21:42:29 +000078 // buildCFG - Used by external clients to construct the CFG.
79 CFG* buildCFG(Stmt* Statement);
Ted Kremenek95e854d2007-08-21 22:06:14 +000080
Ted Kremenek73543912007-08-23 21:42:29 +000081 // Visitors to walk an AST and construct the CFG. Called by
82 // buildCFG. Do not call directly!
Ted Kremenekd8313202007-08-22 18:22:34 +000083
Ted Kremenek73543912007-08-23 21:42:29 +000084 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 Kremenek97f75312007-08-21 21:42:03 +000098
Ted Kremenek73543912007-08-23 21:42:29 +000099private:
100 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000101 CFGBlock* addStmt(Stmt* S);
102 CFGBlock* WalkAST(Stmt* S, bool AlwaysAddStmt);
103 CFGBlock* WalkAST_VisitChildren(Stmt* S);
Ted Kremeneke822b622007-08-28 18:14:37 +0000104 CFGBlock* WalkAST_VisitVarDecl(VarDecl* D);
Ted Kremenek73543912007-08-23 21:42:29 +0000105 void FinishBlock(CFGBlock* B);
Ted Kremenekd8313202007-08-22 18:22:34 +0000106
Ted Kremenek97f75312007-08-21 21:42:03 +0000107};
Ted Kremenek73543912007-08-23 21:42:29 +0000108
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.
114CFG* 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 Kremenekcfee50c2007-08-27 19:46:09 +0000120 Succ = createBlock();
121 assert (Succ == &cfg->getExit());
122 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenek73543912007-08-23 21:42:29 +0000123
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 Kremenekcfee50c2007-08-27 19:46:09 +0000128 if (Block) FinishBlock(B);
Ted Kremenek73543912007-08-23 21:42:29 +0000129
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 Kremenekcfee50c2007-08-27 19:46:09 +0000146 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 Kremenek73543912007-08-23 21:42:29 +0000153 // 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.
163CFGBlock* 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 Kremenekcfee50c2007-08-27 19:46:09 +0000170/// we must reverse the statements because they have been inserted
171/// in reverse order.
Ted Kremenek73543912007-08-23 21:42:29 +0000172void CFGBuilder::FinishBlock(CFGBlock* B) {
173 assert (B);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000174 B->reverseStmts();
Ted Kremenek73543912007-08-23 21:42:29 +0000175}
176
Ted Kremenek65cfa562007-08-27 21:27:44 +0000177/// 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.
183CFGBlock* 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 Kremeneke822b622007-08-28 18:14:37 +0000189/// add extra blocks for ternary operators, &&, and ||. We also
190/// process "," and DeclStmts (which may contain nested control-flow).
Ted Kremenek65cfa562007-08-27 21:27:44 +0000191CFGBlock* 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 Kremenek666a6af2007-08-28 16:18:58 +0000214
Ted Kremeneke822b622007-08-28 18:14:37 +0000215 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 Kremenekcfaae762007-08-27 21:54:41 +0000222 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 Kremeneke822b622007-08-28 18:14:37 +0000244 }
245 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
246 Block->appendStmt(B);
247 addStmt(B->getRHS());
248 return addStmt(B->getLHS());
Ted Kremenekcfaae762007-08-27 21:54:41 +0000249 }
250
251 // Fall through to the default case.
252 }
253
Ted Kremenek65cfa562007-08-27 21:27:44 +0000254 default:
255 if (AlwaysAddStmt) Block->appendStmt(S);
256 return WalkAST_VisitChildren(S);
257 };
258}
259
Ted Kremeneke822b622007-08-28 18:14:37 +0000260/// 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.
264CFGBlock* 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 Kremenek65cfa562007-08-27 21:27:44 +0000279/// WalkAST_VisitChildren - Utility method to call WalkAST on the
280/// children of a Stmt.
Ted Kremenekcfaae762007-08-27 21:54:41 +0000281CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* S) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000282 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 Kremenek73543912007-08-23 21:42:29 +0000290/// VisitStmt - Handle statements with no branching control flow.
291CFGBlock* 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 Kremenekcfee50c2007-08-27 19:46:09 +0000300 addStmt(Statement);
301
Ted Kremenek73543912007-08-23 21:42:29 +0000302 return Block;
303}
304
305CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
306 return Block;
307}
308
309CFGBlock* 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
323CFGBlock* 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 Kremenekcfee50c2007-08-27 19:46:09 +0000352 if (Block) FinishBlock(ElseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000353 }
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 Kremenekcfee50c2007-08-27 19:46:09 +0000366 if (Block) FinishBlock(ThenBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000367 }
368
369 // Now create a new block containing the if statement.
370 Block = createBlock(false);
Ted Kremenek73543912007-08-23 21:42:29 +0000371
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 Kremenekcfee50c2007-08-27 19:46:09 +0000378
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 Kremenek73543912007-08-23 21:42:29 +0000383}
384
385CFGBlock* 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 Kremenekcfee50c2007-08-27 19:46:09 +0000401
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 Kremenek73543912007-08-23 21:42:29 +0000405}
406
407CFGBlock* 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 Kremenekcfee50c2007-08-27 19:46:09 +0000416 // 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 Kremenek73543912007-08-23 21:42:29 +0000420 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
433CFGBlock* 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
453CFGBlock* 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 Kremenekcfee50c2007-08-27 19:46:09 +0000465 // 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 Kremenek73543912007-08-23 21:42:29 +0000481
482 // The condition block is the implicit successor for the loop body as
483 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000484 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000485
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 Kremenekcfee50c2007-08-27 19:46:09 +0000496 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000497
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 Kremenekcfee50c2007-08-27 19:46:09 +0000505 if (Stmt* I = F->getInc()) Block = addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000506
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 Kremenekcfee50c2007-08-27 19:46:09 +0000511 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000512
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000513 // This new body block is a successor to our "exit" condition block.
514 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000515 }
516
517 // Link up the condition block with the code that follows the loop.
518 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000519 ExitConditionBlock->addSuccessor(LoopSuccessor);
520
Ted Kremenek73543912007-08-23 21:42:29 +0000521 // 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 Kremenekcfee50c2007-08-27 19:46:09 +0000526 return addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000527 }
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 Kremenekcfee50c2007-08-27 19:46:09 +0000532 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000533 }
534}
535
536CFGBlock* 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 Kremenekcfee50c2007-08-27 19:46:09 +0000547
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 Kremenek73543912007-08-23 21:42:29 +0000565
566 // The condition block is the implicit successor for the loop body as
567 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000568 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000569
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 Kremenekcfee50c2007-08-27 19:46:09 +0000580 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000581
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 Kremenekcfee50c2007-08-27 19:46:09 +0000591 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000592
593 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000594 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000595 }
596
597 // Link up the condition block with the code that follows the loop.
598 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000599 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000600
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 Kremenekcfee50c2007-08-27 19:46:09 +0000607 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000608}
609
610CFGBlock* 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 Kremenekcfee50c2007-08-27 19:46:09 +0000622 // 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 Kremenek73543912007-08-23 21:42:29 +0000630
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000631 // 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 Kremenek73543912007-08-23 21:42:29 +0000638
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000639 // 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 Kremenek73543912007-08-23 21:42:29 +0000644 // Process the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000645 CFGBlock* BodyBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000646 {
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 Kremenekcfee50c2007-08-27 19:46:09 +0000655 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000656
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 Kremenekcfee50c2007-08-27 19:46:09 +0000666 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000667
668 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000669 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000670 }
671
672 // Link up the condition block with the code that follows the loop.
673 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000674 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000675
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000676 // 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 Kremenek73543912007-08-23 21:42:29 +0000678 // 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
685CFGBlock* 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
701CFGBlock* 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
717CFGBlock* 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 Kremenek73543912007-08-23 21:42:29 +0000735 // Now process the switch body. The code after the switch is the implicit
736 // successor.
737 Succ = SwitchSuccessor;
738 BreakTargetBlock = SwitchSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +0000739
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 Kremenekcfee50c2007-08-27 19:46:09 +0000743 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 Kremenek73543912007-08-23 21:42:29 +0000751 Block = SwitchTerminatedBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000752 return addStmt(S->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +0000753}
754
755CFGBlock* 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 Kremenekd6e50602007-08-23 21:26:19 +0000781} // end anonymous namespace
Ted Kremenek4db5b452007-08-23 16:51:22 +0000782
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.
787CFGBlock* 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 Kremenek97f75312007-08-21 21:42:03 +0000798}
799
Ted Kremenek4db5b452007-08-23 16:51:22 +0000800/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
801/// CFG is returned to the caller.
802CFG* 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 Kremenek97f75312007-08-21 21:42:03 +0000808void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
809
Ted Kremenek4db5b452007-08-23 16:51:22 +0000810/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek97f75312007-08-21 21:42:03 +0000811void CFG::dump() { print(std::cerr); }
812
Ted Kremenek4db5b452007-08-23 16:51:22 +0000813/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenek97f75312007-08-21 21:42:03 +0000814void CFG::print(std::ostream& OS) {
Ted Kremenek4db5b452007-08-23 16:51:22 +0000815 // Print the Entry block.
Ted Kremenekbec06e82007-08-22 21:05:42 +0000816 if (begin() != end()) {
817 CFGBlock& Entry = getEntry();
818 OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n";
819 Entry.print(OS);
820 }
821
Ted Kremenek4db5b452007-08-23 16:51:22 +0000822 // Iterate through the CFGBlocks and print them one by one.
Ted Kremenek97f75312007-08-21 21:42:03 +0000823 for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
Ted Kremenekbec06e82007-08-22 21:05:42 +0000824 // Skip the entry block, because we already printed it.
Ted Kremenek4db5b452007-08-23 16:51:22 +0000825 if (&(*I) == &getEntry() || &(*I) == &getExit()) continue;
Ted Kremenekbec06e82007-08-22 21:05:42 +0000826
Ted Kremenek4db5b452007-08-23 16:51:22 +0000827 OS << "\n [ B" << I->getBlockID() << " ]\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000828 I->print(OS);
829 }
Ted Kremenek73543912007-08-23 21:42:29 +0000830
Ted Kremenek4db5b452007-08-23 16:51:22 +0000831 // 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 Kremenek73543912007-08-23 21:42:29 +0000837
Ted Kremenek97f75312007-08-21 21:42:03 +0000838 OS << "\n";
839}
840
Ted Kremenekd8313202007-08-22 18:22:34 +0000841
842namespace {
843
Ted Kremenek73543912007-08-23 21:42:29 +0000844class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint,
845 void > {
846 std::ostream& OS;
847public:
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 Kremenek65cfa562007-08-27 21:27:44 +0000878 OS << '\n';
879 }
880
881 void VisitSwitchStmt(SwitchStmt* S) {
882 OS << "switch ";
883 S->getCond()->printPretty(OS);
884 OS << '\n';
885 }
886
Ted Kremenekcfaae762007-08-27 21:54:41 +0000887 void VisitExpr(Expr* E) {
888 E->printPretty(OS);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000889 OS << '\n';
Ted Kremenekcfaae762007-08-27 21:54:41 +0000890 }
Ted Kremenek73543912007-08-23 21:42:29 +0000891};
892} // end anonymous namespace
Ted Kremenekd8313202007-08-22 18:22:34 +0000893
Ted Kremenek4db5b452007-08-23 16:51:22 +0000894/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek97f75312007-08-21 21:42:03 +0000895void CFGBlock::dump() { print(std::cerr); }
896
Ted Kremenek4db5b452007-08-23 16:51:22 +0000897/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
898/// Generally this will only be called from CFG::print.
Ted Kremenek97f75312007-08-21 21:42:03 +0000899void 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 Kremenekc5de2222007-08-21 23:26:17 +0000905 // 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 Kremenek97f75312007-08-21 21:42:03 +0000917 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 Kremenekd8313202007-08-22 18:22:34 +0000933 if (ControlFlowStmt)
934 CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt);
935 else
936 OS << "<NULL>\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000937
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 Kremenek4db5b452007-08-23 16:51:22 +0000948}