blob: 64a75b20084d81b39597d0ce57b4e2ede4820ad2 [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 Kremenek73543912007-08-23 21:42:29 +0000104 void FinishBlock(CFGBlock* B);
Ted Kremenekd8313202007-08-22 18:22:34 +0000105
Ted Kremenek97f75312007-08-21 21:42:03 +0000106};
Ted Kremenek73543912007-08-23 21:42:29 +0000107
108/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
109/// represent an arbitrary statement. Examples include a single expression
110/// or a function body (compound statement). The ownership of the returned
111/// CFG is transferred to the caller. If CFG construction fails, this method
112/// returns NULL.
113CFG* CFGBuilder::buildCFG(Stmt* Statement) {
114 if (!Statement) return NULL;
115
116 // Create an empty block that will serve as the exit block for the CFG.
117 // Since this is the first block added to the CFG, it will be implicitly
118 // registered as the exit block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000119 Succ = createBlock();
120 assert (Succ == &cfg->getExit());
121 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenek73543912007-08-23 21:42:29 +0000122
123 // Visit the statements and create the CFG.
124 if (CFGBlock* B = Visit(Statement)) {
125 // Finalize the last constructed block. This usually involves
126 // reversing the order of the statements in the block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000127 if (Block) FinishBlock(B);
Ted Kremenek73543912007-08-23 21:42:29 +0000128
129 // Backpatch the gotos whose label -> block mappings we didn't know
130 // when we encountered them.
131 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
132 E = BackpatchBlocks.end(); I != E; ++I ) {
133
134 CFGBlock* B = *I;
135 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
136 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
137
138 // If there is no target for the goto, then we are looking at an
139 // incomplete AST. Handle this by not registering a successor.
140 if (LI == LabelMap.end()) continue;
141
142 B->addSuccessor(LI->second);
143 }
144
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000145 if (B->pred_size() > 0) {
146 // create an empty entry block that has no predecessors.
147 Succ = B;
148 cfg->setEntry(createBlock());
149 }
150 else cfg->setEntry(B);
151
Ted Kremenek73543912007-08-23 21:42:29 +0000152 // NULL out cfg so that repeated calls
153 CFG* t = cfg;
154 cfg = NULL;
155 return t;
156 }
157 else return NULL;
158}
159
160/// createBlock - Used to lazily create blocks that are connected
161/// to the current (global) succcessor.
162CFGBlock* CFGBuilder::createBlock(bool add_successor) {
163 CFGBlock* B = cfg->createBlock(NumBlocks++);
164 if (add_successor && Succ) B->addSuccessor(Succ);
165 return B;
166}
167
168/// FinishBlock - When the last statement has been added to the block,
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000169/// we must reverse the statements because they have been inserted
170/// in reverse order.
Ted Kremenek73543912007-08-23 21:42:29 +0000171void CFGBuilder::FinishBlock(CFGBlock* B) {
172 assert (B);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000173 B->reverseStmts();
Ted Kremenek73543912007-08-23 21:42:29 +0000174}
175
Ted Kremenek65cfa562007-08-27 21:27:44 +0000176/// addStmt - Used to add statements/expressions to the current CFGBlock
177/// "Block". This method calls WalkAST on the passed statement to see if it
178/// contains any short-circuit expressions. If so, it recursively creates
179/// the necessary blocks for such expressions. It returns the "topmost" block
180/// of the created blocks, or the original value of "Block" when this method
181/// was called if no additional blocks are created.
182CFGBlock* CFGBuilder::addStmt(Stmt* S) {
183 assert (Block);
184 return WalkAST(S,true);
185}
186
187/// WalkAST - Used by addStmt to walk the subtree of a statement and
188/// add extra blocks for ternary operators, &&, and ||.
189CFGBlock* CFGBuilder::WalkAST(Stmt* S, bool AlwaysAddStmt = false) {
190 switch (S->getStmtClass()) {
191 case Stmt::ConditionalOperatorClass: {
192 ConditionalOperator* C = cast<ConditionalOperator>(S);
193
194 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
195 ConfluenceBlock->appendStmt(C);
196 FinishBlock(ConfluenceBlock);
197
198 Succ = ConfluenceBlock;
199 Block = NULL;
200 CFGBlock* LHSBlock = Visit(C->getLHS());
201
202 Succ = ConfluenceBlock;
203 Block = NULL;
204 CFGBlock* RHSBlock = Visit(C->getRHS());
205
206 Block = createBlock(false);
207 Block->addSuccessor(LHSBlock);
208 Block->addSuccessor(RHSBlock);
209 Block->setTerminator(C);
210 return addStmt(C->getCond());
211 }
Ted Kremenek666a6af2007-08-28 16:18:58 +0000212
Ted Kremenekcfaae762007-08-27 21:54:41 +0000213 case Stmt::BinaryOperatorClass: {
214 BinaryOperator* B = cast<BinaryOperator>(S);
215
216 if (B->isLogicalOp()) { // && or ||
217 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
218 ConfluenceBlock->appendStmt(B);
219 FinishBlock(ConfluenceBlock);
220
221 // create the block evaluating the LHS
222 CFGBlock* LHSBlock = createBlock(false);
223 LHSBlock->addSuccessor(ConfluenceBlock);
224 LHSBlock->setTerminator(B);
225
226 // create the block evaluating the RHS
227 Succ = ConfluenceBlock;
228 Block = NULL;
229 CFGBlock* RHSBlock = Visit(B->getRHS());
230 LHSBlock->addSuccessor(RHSBlock);
231
232 // Generate the blocks for evaluating the LHS.
233 Block = LHSBlock;
234 return addStmt(B->getLHS());
235 }
236
237 // Fall through to the default case.
238 }
239
Ted Kremenek65cfa562007-08-27 21:27:44 +0000240 default:
241 if (AlwaysAddStmt) Block->appendStmt(S);
242 return WalkAST_VisitChildren(S);
243 };
244}
245
246/// WalkAST_VisitChildren - Utility method to call WalkAST on the
247/// children of a Stmt.
Ted Kremenekcfaae762007-08-27 21:54:41 +0000248CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* S) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000249 CFGBlock* B = Block;
250 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end() ;
251 I != E; ++I)
252 B = WalkAST(*I);
253
254 return B;
255}
256
Ted Kremenek73543912007-08-23 21:42:29 +0000257/// VisitStmt - Handle statements with no branching control flow.
258CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
259 // We cannot assume that we are in the middle of a basic block, since
260 // the CFG might only be constructed for this single statement. If
261 // we have no current basic block, just create one lazily.
262 if (!Block) Block = createBlock();
263
264 // Simply add the statement to the current block. We actually
265 // insert statements in reverse order; this order is reversed later
266 // when processing the containing element in the AST.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000267 addStmt(Statement);
268
Ted Kremenek73543912007-08-23 21:42:29 +0000269 return Block;
270}
271
272CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
273 return Block;
274}
275
276CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
277 // The value returned from this function is the last created CFGBlock
278 // that represents the "entry" point for the translated AST node.
279 CFGBlock* LastBlock;
280
281 for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(),
282 E = C->body_rend(); I != E; ++I )
283 // Add the statement to the current block.
284 if (!(LastBlock=Visit(*I)))
285 return NULL;
286
287 return LastBlock;
288}
289
290CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
291 // We may see an if statement in the middle of a basic block, or
292 // it may be the first statement we are processing. In either case,
293 // we create a new basic block. First, we create the blocks for
294 // the then...else statements, and then we create the block containing
295 // the if statement. If we were in the middle of a block, we
296 // stop processing that block and reverse its statements. That block
297 // is then the implicit successor for the "then" and "else" clauses.
298
299 // The block we were proccessing is now finished. Make it the
300 // successor block.
301 if (Block) {
302 Succ = Block;
303 FinishBlock(Block);
304 }
305
306 // Process the false branch. NULL out Block so that the recursive
307 // call to Visit will create a new basic block.
308 // Null out Block so that all successor
309 CFGBlock* ElseBlock = Succ;
310
311 if (Stmt* Else = I->getElse()) {
312 SaveAndRestore<CFGBlock*> sv(Succ);
313
314 // NULL out Block so that the recursive call to Visit will
315 // create a new basic block.
316 Block = NULL;
317 ElseBlock = Visit(Else);
318 if (!ElseBlock) return NULL;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000319 if (Block) FinishBlock(ElseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000320 }
321
322 // Process the true branch. NULL out Block so that the recursive
323 // call to Visit will create a new basic block.
324 // Null out Block so that all successor
325 CFGBlock* ThenBlock;
326 {
327 Stmt* Then = I->getThen();
328 assert (Then);
329 SaveAndRestore<CFGBlock*> sv(Succ);
330 Block = NULL;
331 ThenBlock = Visit(Then);
332 if (!ThenBlock) return NULL;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000333 if (Block) FinishBlock(ThenBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000334 }
335
336 // Now create a new block containing the if statement.
337 Block = createBlock(false);
Ted Kremenek73543912007-08-23 21:42:29 +0000338
339 // Set the terminator of the new block to the If statement.
340 Block->setTerminator(I);
341
342 // Now add the successors.
343 Block->addSuccessor(ThenBlock);
344 Block->addSuccessor(ElseBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000345
346 // Add the condition as the last statement in the new block. This
347 // may create new blocks as the condition may contain control-flow. Any
348 // newly created blocks will be pointed to be "Block".
349 return addStmt(I->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +0000350}
351
352CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
353 // If we were in the middle of a block we stop processing that block
354 // and reverse its statements.
355 //
356 // NOTE: If a "return" appears in the middle of a block, this means
357 // that the code afterwards is DEAD (unreachable). We still
358 // keep a basic block for that code; a simple "mark-and-sweep"
359 // from the entry block will be able to report such dead
360 // blocks.
361 if (Block) FinishBlock(Block);
362
363 // Create the new block.
364 Block = createBlock(false);
365
366 // The Exit block is the only successor.
367 Block->addSuccessor(&cfg->getExit());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000368
Ted Kremenek73543912007-08-23 21:42:29 +0000369 // Also add the return statement as the terminator.
370 Block->setTerminator(R);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000371
372 // Add the return statement to the block. This may create new blocks
373 // if R contains control-flow (short-circuit operations).
374 return addStmt(R);
Ted Kremenek73543912007-08-23 21:42:29 +0000375}
376
377CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
378 // Get the block of the labeled statement. Add it to our map.
379 CFGBlock* LabelBlock = Visit(L->getSubStmt());
380 assert (LabelBlock);
381
382 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
383 LabelMap[ L ] = LabelBlock;
384
385 // Labels partition blocks, so this is the end of the basic block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000386 // we were processing (the label is the first statement). Add the label
387 // the to end (really the beginning) of the block. Because this is
388 // label (and we have already processed the substatement) there is no
389 // extra control-flow to worry about.
Ted Kremenek73543912007-08-23 21:42:29 +0000390 LabelBlock->appendStmt(L);
391 FinishBlock(LabelBlock);
392
393 // We set Block to NULL to allow lazy creation of a new block
394 // (if necessary);
395 Block = NULL;
396
397 // This block is now the implicit successor of other blocks.
398 Succ = LabelBlock;
399
400 return LabelBlock;
401}
402
403CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
404 // Goto is a control-flow statement. Thus we stop processing the
405 // current block and create a new one.
406 if (Block) FinishBlock(Block);
407 Block = createBlock(false);
408 Block->setTerminator(G);
409
410 // If we already know the mapping to the label block add the
411 // successor now.
412 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
413
414 if (I == LabelMap.end())
415 // We will need to backpatch this block later.
416 BackpatchBlocks.push_back(Block);
417 else
418 Block->addSuccessor(I->second);
419
420 return Block;
421}
422
423CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
424 // "for" is a control-flow statement. Thus we stop processing the
425 // current block.
426
427 CFGBlock* LoopSuccessor = NULL;
428
429 if (Block) {
430 FinishBlock(Block);
431 LoopSuccessor = Block;
432 }
433 else LoopSuccessor = Succ;
434
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000435 // Because of short-circuit evaluation, the condition of the loop
436 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
437 // blocks that evaluate the condition.
438 CFGBlock* ExitConditionBlock = createBlock(false);
439 CFGBlock* EntryConditionBlock = ExitConditionBlock;
440
441 // Set the terminator for the "exit" condition block.
442 ExitConditionBlock->setTerminator(F);
443
444 // Now add the actual condition to the condition block. Because the
445 // condition itself may contain control-flow, new blocks may be created.
446 if (Stmt* C = F->getCond()) {
447 Block = ExitConditionBlock;
448 EntryConditionBlock = addStmt(C);
449 if (Block) FinishBlock(EntryConditionBlock);
450 }
Ted Kremenek73543912007-08-23 21:42:29 +0000451
452 // The condition block is the implicit successor for the loop body as
453 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000454 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000455
456 // Now create the loop body.
457 {
458 assert (F->getBody());
459
460 // Save the current values for Block, Succ, and continue and break targets
461 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
462 save_continue(ContinueTargetBlock),
463 save_break(BreakTargetBlock);
464
465 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000466 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000467
468 // All breaks should go to the code following the loop.
469 BreakTargetBlock = LoopSuccessor;
470
471 // Create a new block to contain the (bottom) of the loop body.
472 Block = createBlock();
473
474 // If we have increment code, insert it at the end of the body block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000475 if (Stmt* I = F->getInc()) Block = addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000476
477 // Now populate the body block, and in the process create new blocks
478 // as we walk the body of the loop.
479 CFGBlock* BodyBlock = Visit(F->getBody());
480 assert (BodyBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000481 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000482
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000483 // This new body block is a successor to our "exit" condition block.
484 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000485 }
486
487 // Link up the condition block with the code that follows the loop.
488 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000489 ExitConditionBlock->addSuccessor(LoopSuccessor);
490
Ted Kremenek73543912007-08-23 21:42:29 +0000491 // If the loop contains initialization, create a new block for those
492 // statements. This block can also contain statements that precede
493 // the loop.
494 if (Stmt* I = F->getInit()) {
495 Block = createBlock();
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000496 return addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000497 }
498 else {
499 // There is no loop initialization. We are thus basically a while
500 // loop. NULL out Block to force lazy block construction.
501 Block = NULL;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000502 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000503 }
504}
505
506CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
507 // "while" is a control-flow statement. Thus we stop processing the
508 // current block.
509
510 CFGBlock* LoopSuccessor = NULL;
511
512 if (Block) {
513 FinishBlock(Block);
514 LoopSuccessor = Block;
515 }
516 else LoopSuccessor = Succ;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000517
518 // Because of short-circuit evaluation, the condition of the loop
519 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
520 // blocks that evaluate the condition.
521 CFGBlock* ExitConditionBlock = createBlock(false);
522 CFGBlock* EntryConditionBlock = ExitConditionBlock;
523
524 // Set the terminator for the "exit" condition block.
525 ExitConditionBlock->setTerminator(W);
526
527 // Now add the actual condition to the condition block. Because the
528 // condition itself may contain control-flow, new blocks may be created.
529 // Thus we update "Succ" after adding the condition.
530 if (Stmt* C = W->getCond()) {
531 Block = ExitConditionBlock;
532 EntryConditionBlock = addStmt(C);
533 if (Block) FinishBlock(EntryConditionBlock);
534 }
Ted Kremenek73543912007-08-23 21:42:29 +0000535
536 // The condition block is the implicit successor for the loop body as
537 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000538 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000539
540 // Process the loop body.
541 {
542 assert (W->getBody());
543
544 // Save the current values for Block, Succ, and continue and break targets
545 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
546 save_continue(ContinueTargetBlock),
547 save_break(BreakTargetBlock);
548
549 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000550 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000551
552 // All breaks should go to the code following the loop.
553 BreakTargetBlock = LoopSuccessor;
554
555 // NULL out Block to force lazy instantiation of blocks for the body.
556 Block = NULL;
557
558 // Create the body. The returned block is the entry to the loop body.
559 CFGBlock* BodyBlock = Visit(W->getBody());
560 assert (BodyBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000561 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000562
563 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000564 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000565 }
566
567 // Link up the condition block with the code that follows the loop.
568 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000569 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000570
571 // There can be no more statements in the condition block
572 // since we loop back to this block. NULL out Block to force
573 // lazy creation of another block.
574 Block = NULL;
575
576 // Return the condition block, which is the dominating block for the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000577 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000578}
579
580CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
581 // "do...while" is a control-flow statement. Thus we stop processing the
582 // current block.
583
584 CFGBlock* LoopSuccessor = NULL;
585
586 if (Block) {
587 FinishBlock(Block);
588 LoopSuccessor = Block;
589 }
590 else LoopSuccessor = Succ;
591
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000592 // Because of short-circuit evaluation, the condition of the loop
593 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
594 // blocks that evaluate the condition.
595 CFGBlock* ExitConditionBlock = createBlock(false);
596 CFGBlock* EntryConditionBlock = ExitConditionBlock;
597
598 // Set the terminator for the "exit" condition block.
599 ExitConditionBlock->setTerminator(D);
Ted Kremenek73543912007-08-23 21:42:29 +0000600
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000601 // Now add the actual condition to the condition block. Because the
602 // condition itself may contain control-flow, new blocks may be created.
603 if (Stmt* C = D->getCond()) {
604 Block = ExitConditionBlock;
605 EntryConditionBlock = addStmt(C);
606 if (Block) FinishBlock(EntryConditionBlock);
607 }
Ted Kremenek73543912007-08-23 21:42:29 +0000608
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000609 // The condition block is the implicit successor for the loop body as
610 // well as any code above the loop.
611 Succ = EntryConditionBlock;
612
613
Ted Kremenek73543912007-08-23 21:42:29 +0000614 // Process the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000615 CFGBlock* BodyBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000616 {
617 assert (D->getBody());
618
619 // Save the current values for Block, Succ, and continue and break targets
620 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
621 save_continue(ContinueTargetBlock),
622 save_break(BreakTargetBlock);
623
624 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000625 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000626
627 // All breaks should go to the code following the loop.
628 BreakTargetBlock = LoopSuccessor;
629
630 // NULL out Block to force lazy instantiation of blocks for the body.
631 Block = NULL;
632
633 // Create the body. The returned block is the entry to the loop body.
634 BodyBlock = Visit(D->getBody());
635 assert (BodyBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000636 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000637
638 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000639 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000640 }
641
642 // Link up the condition block with the code that follows the loop.
643 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000644 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000645
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000646 // There can be no more statements in the body block(s)
647 // since we loop back to the body. NULL out Block to force
Ted Kremenek73543912007-08-23 21:42:29 +0000648 // lazy creation of another block.
649 Block = NULL;
650
651 // Return the loop body, which is the dominating block for the loop.
652 return BodyBlock;
653}
654
655CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
656 // "continue" is a control-flow statement. Thus we stop processing the
657 // current block.
658 if (Block) FinishBlock(Block);
659
660 // Now create a new block that ends with the continue statement.
661 Block = createBlock(false);
662 Block->setTerminator(C);
663
664 // If there is no target for the continue, then we are looking at an
665 // incomplete AST. Handle this by not registering a successor.
666 if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
667
668 return Block;
669}
670
671CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
672 // "break" is a control-flow statement. Thus we stop processing the
673 // current block.
674 if (Block) FinishBlock(Block);
675
676 // Now create a new block that ends with the continue statement.
677 Block = createBlock(false);
678 Block->setTerminator(B);
679
680 // If there is no target for the break, then we are looking at an
681 // incomplete AST. Handle this by not registering a successor.
682 if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
683
684 return Block;
685}
686
687CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* S) {
688 // "switch" is a control-flow statement. Thus we stop processing the
689 // current block.
690 CFGBlock* SwitchSuccessor = NULL;
691
692 if (Block) {
693 FinishBlock(Block);
694 SwitchSuccessor = Block;
695 }
696 else SwitchSuccessor = Succ;
697
698 // Save the current "switch" context.
699 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
700 save_break(BreakTargetBlock);
701
702 // Create a new block that will contain the switch statement.
703 SwitchTerminatedBlock = createBlock(false);
704
Ted Kremenek73543912007-08-23 21:42:29 +0000705 // Now process the switch body. The code after the switch is the implicit
706 // successor.
707 Succ = SwitchSuccessor;
708 BreakTargetBlock = SwitchSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +0000709
710 // When visiting the body, the case statements should automatically get
711 // linked up to the switch. We also don't keep a pointer to the body,
712 // since all control-flow from the switch goes to case/default statements.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000713 assert (S->getBody() && "switch must contain a non-NULL body");
714 Block = NULL;
715 CFGBlock *BodyBlock = Visit(S->getBody());
716 if (Block) FinishBlock(BodyBlock);
717
718 // Add the terminator and condition in the switch block.
719 SwitchTerminatedBlock->setTerminator(S);
720 assert (S->getCond() && "switch condition must be non-NULL");
Ted Kremenek73543912007-08-23 21:42:29 +0000721 Block = SwitchTerminatedBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000722 return addStmt(S->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +0000723}
724
725CFGBlock* CFGBuilder::VisitSwitchCase(SwitchCase* S) {
726 // A SwitchCase is either a "default" or "case" statement. We handle
727 // both in the same way. They are essentially labels, so they are the
728 // first statement in a block.
729 CFGBlock* CaseBlock = Visit(S->getSubStmt());
730 assert (CaseBlock);
731
732 // Cases/Default statements parition block, so this is the top of
733 // the basic block we were processing (the case/default is the first stmt).
734 CaseBlock->appendStmt(S);
735 FinishBlock(CaseBlock);
736
737 // Add this block to the list of successors for the block with the
738 // switch statement.
739 if (SwitchTerminatedBlock) SwitchTerminatedBlock->addSuccessor(CaseBlock);
740
741 // We set Block to NULL to allow lazy creation of a new block (if necessary)
742 Block = NULL;
743
744 // This block is now the implicit successor of other blocks.
745 Succ = CaseBlock;
746
747 return CaseBlock;
748}
749
750
Ted Kremenekd6e50602007-08-23 21:26:19 +0000751} // end anonymous namespace
Ted Kremenek4db5b452007-08-23 16:51:22 +0000752
753/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
754/// block has no successors or predecessors. If this is the first block
755/// created in the CFG, it is automatically set to be the Entry and Exit
756/// of the CFG.
757CFGBlock* CFG::createBlock(unsigned blockID) {
758 bool first_block = begin() == end();
759
760 // Create the block.
761 Blocks.push_front(CFGBlock(blockID));
762
763 // If this is the first block, set it as the Entry and Exit.
764 if (first_block) Entry = Exit = &front();
765
766 // Return the block.
767 return &front();
Ted Kremenek97f75312007-08-21 21:42:03 +0000768}
769
Ted Kremenek4db5b452007-08-23 16:51:22 +0000770/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
771/// CFG is returned to the caller.
772CFG* CFG::buildCFG(Stmt* Statement) {
773 CFGBuilder Builder;
774 return Builder.buildCFG(Statement);
775}
776
777/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek97f75312007-08-21 21:42:03 +0000778void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
779
Ted Kremenek4db5b452007-08-23 16:51:22 +0000780/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek97f75312007-08-21 21:42:03 +0000781void CFG::dump() { print(std::cerr); }
782
Ted Kremenek4db5b452007-08-23 16:51:22 +0000783/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenek97f75312007-08-21 21:42:03 +0000784void CFG::print(std::ostream& OS) {
Ted Kremenek4db5b452007-08-23 16:51:22 +0000785 // Print the Entry block.
Ted Kremenekbec06e82007-08-22 21:05:42 +0000786 if (begin() != end()) {
787 CFGBlock& Entry = getEntry();
788 OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n";
789 Entry.print(OS);
790 }
791
Ted Kremenek4db5b452007-08-23 16:51:22 +0000792 // Iterate through the CFGBlocks and print them one by one.
Ted Kremenek97f75312007-08-21 21:42:03 +0000793 for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
Ted Kremenekbec06e82007-08-22 21:05:42 +0000794 // Skip the entry block, because we already printed it.
Ted Kremenek4db5b452007-08-23 16:51:22 +0000795 if (&(*I) == &getEntry() || &(*I) == &getExit()) continue;
Ted Kremenekbec06e82007-08-22 21:05:42 +0000796
Ted Kremenek4db5b452007-08-23 16:51:22 +0000797 OS << "\n [ B" << I->getBlockID() << " ]\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000798 I->print(OS);
799 }
Ted Kremenek73543912007-08-23 21:42:29 +0000800
Ted Kremenek4db5b452007-08-23 16:51:22 +0000801 // Print the Exit Block.
802 if (begin() != end()) {
803 CFGBlock& Exit = getExit();
804 OS << "\n [ B" << Exit.getBlockID() << " (EXIT) ]\n";
805 Exit.print(OS);
806 }
Ted Kremenek73543912007-08-23 21:42:29 +0000807
Ted Kremenek97f75312007-08-21 21:42:03 +0000808 OS << "\n";
809}
810
Ted Kremenekd8313202007-08-22 18:22:34 +0000811
812namespace {
813
Ted Kremenek73543912007-08-23 21:42:29 +0000814class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint,
815 void > {
816 std::ostream& OS;
817public:
818 CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {}
819
820 void VisitIfStmt(IfStmt* I) {
821 OS << "if ";
822 I->getCond()->printPretty(std::cerr);
823 OS << "\n";
824 }
825
826 // Default case.
827 void VisitStmt(Stmt* S) { S->printPretty(OS); }
828
829 void VisitForStmt(ForStmt* F) {
830 OS << "for (" ;
831 if (Stmt* I = F->getInit()) I->printPretty(OS);
832 OS << " ; ";
833 if (Stmt* C = F->getCond()) C->printPretty(OS);
834 OS << " ; ";
835 if (Stmt* I = F->getInc()) I->printPretty(OS);
836 OS << ")\n";
837 }
838
839 void VisitWhileStmt(WhileStmt* W) {
840 OS << "while " ;
841 if (Stmt* C = W->getCond()) C->printPretty(OS);
842 OS << "\n";
843 }
844
845 void VisitDoStmt(DoStmt* D) {
846 OS << "do ... while ";
847 if (Stmt* C = D->getCond()) C->printPretty(OS);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000848 OS << '\n';
849 }
850
851 void VisitSwitchStmt(SwitchStmt* S) {
852 OS << "switch ";
853 S->getCond()->printPretty(OS);
854 OS << '\n';
855 }
856
Ted Kremenekcfaae762007-08-27 21:54:41 +0000857 void VisitExpr(Expr* E) {
858 E->printPretty(OS);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000859 OS << '\n';
Ted Kremenekcfaae762007-08-27 21:54:41 +0000860 }
Ted Kremenek73543912007-08-23 21:42:29 +0000861};
862} // end anonymous namespace
Ted Kremenekd8313202007-08-22 18:22:34 +0000863
Ted Kremenek4db5b452007-08-23 16:51:22 +0000864/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek97f75312007-08-21 21:42:03 +0000865void CFGBlock::dump() { print(std::cerr); }
866
Ted Kremenek4db5b452007-08-23 16:51:22 +0000867/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
868/// Generally this will only be called from CFG::print.
Ted Kremenek97f75312007-08-21 21:42:03 +0000869void CFGBlock::print(std::ostream& OS) {
870
871 // Iterate through the statements in the block and print them.
872 OS << " ------------------------\n";
873 unsigned j = 1;
874 for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) {
Ted Kremenekc5de2222007-08-21 23:26:17 +0000875 // Print the statement # in the basic block.
876 OS << " " << std::setw(3) << j << ": ";
877
878 // Print the statement/expression.
879 Stmt* S = *I;
880
881 if (LabelStmt* L = dyn_cast<LabelStmt>(S))
882 OS << L->getName() << ": (LABEL)\n";
883 else
884 (*I)->printPretty(OS);
885
886 // Expressions need a newline.
Ted Kremenek97f75312007-08-21 21:42:03 +0000887 if (isa<Expr>(*I)) OS << '\n';
888 }
889 OS << " ------------------------\n";
890
891 // Print the predecessors of this block.
892 OS << " Predecessors (" << pred_size() << "):";
893 unsigned i = 0;
894 for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) {
895 if (i == 8 || (i-8) == 0) {
896 OS << "\n ";
897 }
898 OS << " B" << (*I)->getBlockID();
899 }
900
901 // Print the terminator of this block.
902 OS << "\n Terminator: ";
Ted Kremenekd8313202007-08-22 18:22:34 +0000903 if (ControlFlowStmt)
904 CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt);
905 else
906 OS << "<NULL>\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000907
908 // Print the successors of this block.
909 OS << " Successors (" << succ_size() << "):";
910 i = 0;
911 for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) {
912 if (i == 8 || (i-8) % 10 == 0) {
913 OS << "\n ";
914 }
915 OS << " B" << (*I)->getBlockID();
916 }
917 OS << '\n';
Ted Kremenek4db5b452007-08-23 16:51:22 +0000918}