blob: b79bfe519b3845d4cd0344e692ca806c5a2a760e [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);
Ted Kremenek65cfa562007-08-27 21:27:44 +000086 CFGBlock* VisitParenExpr(ParenExpr* Statement);
Ted Kremenek73543912007-08-23 21:42:29 +000087 CFGBlock* VisitCompoundStmt(CompoundStmt* C);
88 CFGBlock* VisitIfStmt(IfStmt* I);
89 CFGBlock* VisitReturnStmt(ReturnStmt* R);
90 CFGBlock* VisitLabelStmt(LabelStmt* L);
91 CFGBlock* VisitGotoStmt(GotoStmt* G);
92 CFGBlock* VisitForStmt(ForStmt* F);
93 CFGBlock* VisitWhileStmt(WhileStmt* W);
94 CFGBlock* VisitDoStmt(DoStmt* D);
95 CFGBlock* VisitContinueStmt(ContinueStmt* C);
96 CFGBlock* VisitBreakStmt(BreakStmt* B);
97 CFGBlock* VisitSwitchStmt(SwitchStmt* S);
98 CFGBlock* VisitSwitchCase(SwitchCase* S);
Ted Kremenek97f75312007-08-21 21:42:03 +000099
Ted Kremenek73543912007-08-23 21:42:29 +0000100private:
101 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000102 CFGBlock* addStmt(Stmt* S);
103 CFGBlock* WalkAST(Stmt* S, bool AlwaysAddStmt);
104 CFGBlock* WalkAST_VisitChildren(Stmt* S);
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
189/// add extra blocks for ternary operators, &&, and ||.
190CFGBlock* CFGBuilder::WalkAST(Stmt* S, bool AlwaysAddStmt = false) {
191 switch (S->getStmtClass()) {
192 case Stmt::ConditionalOperatorClass: {
193 ConditionalOperator* C = cast<ConditionalOperator>(S);
194
195 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
196 ConfluenceBlock->appendStmt(C);
197 FinishBlock(ConfluenceBlock);
198
199 Succ = ConfluenceBlock;
200 Block = NULL;
201 CFGBlock* LHSBlock = Visit(C->getLHS());
202
203 Succ = ConfluenceBlock;
204 Block = NULL;
205 CFGBlock* RHSBlock = Visit(C->getRHS());
206
207 Block = createBlock(false);
208 Block->addSuccessor(LHSBlock);
209 Block->addSuccessor(RHSBlock);
210 Block->setTerminator(C);
211 return addStmt(C->getCond());
212 }
Ted Kremenekcfaae762007-08-27 21:54:41 +0000213
214 case Stmt::ParenExprClass:
215 return WalkAST(cast<ParenExpr>(S)->getSubExpr(),AlwaysAddStmt);
216
217 case Stmt::BinaryOperatorClass: {
218 BinaryOperator* B = cast<BinaryOperator>(S);
219
220 if (B->isLogicalOp()) { // && or ||
221 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
222 ConfluenceBlock->appendStmt(B);
223 FinishBlock(ConfluenceBlock);
224
225 // create the block evaluating the LHS
226 CFGBlock* LHSBlock = createBlock(false);
227 LHSBlock->addSuccessor(ConfluenceBlock);
228 LHSBlock->setTerminator(B);
229
230 // create the block evaluating the RHS
231 Succ = ConfluenceBlock;
232 Block = NULL;
233 CFGBlock* RHSBlock = Visit(B->getRHS());
234 LHSBlock->addSuccessor(RHSBlock);
235
236 // Generate the blocks for evaluating the LHS.
237 Block = LHSBlock;
238 return addStmt(B->getLHS());
239 }
240
241 // Fall through to the default case.
242 }
243
Ted Kremenek65cfa562007-08-27 21:27:44 +0000244 default:
245 if (AlwaysAddStmt) Block->appendStmt(S);
246 return WalkAST_VisitChildren(S);
247 };
248}
249
250/// WalkAST_VisitChildren - Utility method to call WalkAST on the
251/// children of a Stmt.
Ted Kremenekcfaae762007-08-27 21:54:41 +0000252CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* S) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000253 CFGBlock* B = Block;
254 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end() ;
255 I != E; ++I)
256 B = WalkAST(*I);
257
258 return B;
259}
260
Ted Kremenek73543912007-08-23 21:42:29 +0000261/// VisitStmt - Handle statements with no branching control flow.
262CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
263 // We cannot assume that we are in the middle of a basic block, since
264 // the CFG might only be constructed for this single statement. If
265 // we have no current basic block, just create one lazily.
266 if (!Block) Block = createBlock();
267
268 // Simply add the statement to the current block. We actually
269 // insert statements in reverse order; this order is reversed later
270 // when processing the containing element in the AST.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000271 addStmt(Statement);
272
Ted Kremenek73543912007-08-23 21:42:29 +0000273 return Block;
274}
275
276CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
277 return Block;
278}
279
Ted Kremenek65cfa562007-08-27 21:27:44 +0000280CFGBlock* CFGBuilder::VisitParenExpr(ParenExpr* Statement) {
281 return Visit(Statement->getSubExpr());
282}
283
284
Ted Kremenek73543912007-08-23 21:42:29 +0000285CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
286 // The value returned from this function is the last created CFGBlock
287 // that represents the "entry" point for the translated AST node.
288 CFGBlock* LastBlock;
289
290 for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(),
291 E = C->body_rend(); I != E; ++I )
292 // Add the statement to the current block.
293 if (!(LastBlock=Visit(*I)))
294 return NULL;
295
296 return LastBlock;
297}
298
299CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
300 // We may see an if statement in the middle of a basic block, or
301 // it may be the first statement we are processing. In either case,
302 // we create a new basic block. First, we create the blocks for
303 // the then...else statements, and then we create the block containing
304 // the if statement. If we were in the middle of a block, we
305 // stop processing that block and reverse its statements. That block
306 // is then the implicit successor for the "then" and "else" clauses.
307
308 // The block we were proccessing is now finished. Make it the
309 // successor block.
310 if (Block) {
311 Succ = Block;
312 FinishBlock(Block);
313 }
314
315 // Process the false branch. NULL out Block so that the recursive
316 // call to Visit will create a new basic block.
317 // Null out Block so that all successor
318 CFGBlock* ElseBlock = Succ;
319
320 if (Stmt* Else = I->getElse()) {
321 SaveAndRestore<CFGBlock*> sv(Succ);
322
323 // NULL out Block so that the recursive call to Visit will
324 // create a new basic block.
325 Block = NULL;
326 ElseBlock = Visit(Else);
327 if (!ElseBlock) return NULL;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000328 if (Block) FinishBlock(ElseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000329 }
330
331 // Process the true branch. NULL out Block so that the recursive
332 // call to Visit will create a new basic block.
333 // Null out Block so that all successor
334 CFGBlock* ThenBlock;
335 {
336 Stmt* Then = I->getThen();
337 assert (Then);
338 SaveAndRestore<CFGBlock*> sv(Succ);
339 Block = NULL;
340 ThenBlock = Visit(Then);
341 if (!ThenBlock) return NULL;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000342 if (Block) FinishBlock(ThenBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000343 }
344
345 // Now create a new block containing the if statement.
346 Block = createBlock(false);
Ted Kremenek73543912007-08-23 21:42:29 +0000347
348 // Set the terminator of the new block to the If statement.
349 Block->setTerminator(I);
350
351 // Now add the successors.
352 Block->addSuccessor(ThenBlock);
353 Block->addSuccessor(ElseBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000354
355 // Add the condition as the last statement in the new block. This
356 // may create new blocks as the condition may contain control-flow. Any
357 // newly created blocks will be pointed to be "Block".
358 return addStmt(I->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +0000359}
360
361CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
362 // If we were in the middle of a block we stop processing that block
363 // and reverse its statements.
364 //
365 // NOTE: If a "return" appears in the middle of a block, this means
366 // that the code afterwards is DEAD (unreachable). We still
367 // keep a basic block for that code; a simple "mark-and-sweep"
368 // from the entry block will be able to report such dead
369 // blocks.
370 if (Block) FinishBlock(Block);
371
372 // Create the new block.
373 Block = createBlock(false);
374
375 // The Exit block is the only successor.
376 Block->addSuccessor(&cfg->getExit());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000377
Ted Kremenek73543912007-08-23 21:42:29 +0000378 // Also add the return statement as the terminator.
379 Block->setTerminator(R);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000380
381 // Add the return statement to the block. This may create new blocks
382 // if R contains control-flow (short-circuit operations).
383 return addStmt(R);
Ted Kremenek73543912007-08-23 21:42:29 +0000384}
385
386CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
387 // Get the block of the labeled statement. Add it to our map.
388 CFGBlock* LabelBlock = Visit(L->getSubStmt());
389 assert (LabelBlock);
390
391 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
392 LabelMap[ L ] = LabelBlock;
393
394 // Labels partition blocks, so this is the end of the basic block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000395 // we were processing (the label is the first statement). Add the label
396 // the to end (really the beginning) of the block. Because this is
397 // label (and we have already processed the substatement) there is no
398 // extra control-flow to worry about.
Ted Kremenek73543912007-08-23 21:42:29 +0000399 LabelBlock->appendStmt(L);
400 FinishBlock(LabelBlock);
401
402 // We set Block to NULL to allow lazy creation of a new block
403 // (if necessary);
404 Block = NULL;
405
406 // This block is now the implicit successor of other blocks.
407 Succ = LabelBlock;
408
409 return LabelBlock;
410}
411
412CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
413 // Goto is a control-flow statement. Thus we stop processing the
414 // current block and create a new one.
415 if (Block) FinishBlock(Block);
416 Block = createBlock(false);
417 Block->setTerminator(G);
418
419 // If we already know the mapping to the label block add the
420 // successor now.
421 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
422
423 if (I == LabelMap.end())
424 // We will need to backpatch this block later.
425 BackpatchBlocks.push_back(Block);
426 else
427 Block->addSuccessor(I->second);
428
429 return Block;
430}
431
432CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
433 // "for" is a control-flow statement. Thus we stop processing the
434 // current block.
435
436 CFGBlock* LoopSuccessor = NULL;
437
438 if (Block) {
439 FinishBlock(Block);
440 LoopSuccessor = Block;
441 }
442 else LoopSuccessor = Succ;
443
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000444 // Because of short-circuit evaluation, the condition of the loop
445 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
446 // blocks that evaluate the condition.
447 CFGBlock* ExitConditionBlock = createBlock(false);
448 CFGBlock* EntryConditionBlock = ExitConditionBlock;
449
450 // Set the terminator for the "exit" condition block.
451 ExitConditionBlock->setTerminator(F);
452
453 // Now add the actual condition to the condition block. Because the
454 // condition itself may contain control-flow, new blocks may be created.
455 if (Stmt* C = F->getCond()) {
456 Block = ExitConditionBlock;
457 EntryConditionBlock = addStmt(C);
458 if (Block) FinishBlock(EntryConditionBlock);
459 }
Ted Kremenek73543912007-08-23 21:42:29 +0000460
461 // The condition block is the implicit successor for the loop body as
462 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000463 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000464
465 // Now create the loop body.
466 {
467 assert (F->getBody());
468
469 // Save the current values for Block, Succ, and continue and break targets
470 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
471 save_continue(ContinueTargetBlock),
472 save_break(BreakTargetBlock);
473
474 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000475 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000476
477 // All breaks should go to the code following the loop.
478 BreakTargetBlock = LoopSuccessor;
479
480 // Create a new block to contain the (bottom) of the loop body.
481 Block = createBlock();
482
483 // If we have increment code, insert it at the end of the body block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000484 if (Stmt* I = F->getInc()) Block = addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000485
486 // Now populate the body block, and in the process create new blocks
487 // as we walk the body of the loop.
488 CFGBlock* BodyBlock = Visit(F->getBody());
489 assert (BodyBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000490 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000491
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000492 // This new body block is a successor to our "exit" condition block.
493 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000494 }
495
496 // Link up the condition block with the code that follows the loop.
497 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000498 ExitConditionBlock->addSuccessor(LoopSuccessor);
499
Ted Kremenek73543912007-08-23 21:42:29 +0000500 // If the loop contains initialization, create a new block for those
501 // statements. This block can also contain statements that precede
502 // the loop.
503 if (Stmt* I = F->getInit()) {
504 Block = createBlock();
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000505 return addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000506 }
507 else {
508 // There is no loop initialization. We are thus basically a while
509 // loop. NULL out Block to force lazy block construction.
510 Block = NULL;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000511 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000512 }
513}
514
515CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
516 // "while" is a control-flow statement. Thus we stop processing the
517 // current block.
518
519 CFGBlock* LoopSuccessor = NULL;
520
521 if (Block) {
522 FinishBlock(Block);
523 LoopSuccessor = Block;
524 }
525 else LoopSuccessor = Succ;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000526
527 // Because of short-circuit evaluation, the condition of the loop
528 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
529 // blocks that evaluate the condition.
530 CFGBlock* ExitConditionBlock = createBlock(false);
531 CFGBlock* EntryConditionBlock = ExitConditionBlock;
532
533 // Set the terminator for the "exit" condition block.
534 ExitConditionBlock->setTerminator(W);
535
536 // Now add the actual condition to the condition block. Because the
537 // condition itself may contain control-flow, new blocks may be created.
538 // Thus we update "Succ" after adding the condition.
539 if (Stmt* C = W->getCond()) {
540 Block = ExitConditionBlock;
541 EntryConditionBlock = addStmt(C);
542 if (Block) FinishBlock(EntryConditionBlock);
543 }
Ted Kremenek73543912007-08-23 21:42:29 +0000544
545 // The condition block is the implicit successor for the loop body as
546 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000547 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000548
549 // Process the loop body.
550 {
551 assert (W->getBody());
552
553 // Save the current values for Block, Succ, and continue and break targets
554 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
555 save_continue(ContinueTargetBlock),
556 save_break(BreakTargetBlock);
557
558 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000559 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000560
561 // All breaks should go to the code following the loop.
562 BreakTargetBlock = LoopSuccessor;
563
564 // NULL out Block to force lazy instantiation of blocks for the body.
565 Block = NULL;
566
567 // Create the body. The returned block is the entry to the loop body.
568 CFGBlock* BodyBlock = Visit(W->getBody());
569 assert (BodyBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000570 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000571
572 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000573 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000574 }
575
576 // Link up the condition block with the code that follows the loop.
577 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000578 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000579
580 // There can be no more statements in the condition block
581 // since we loop back to this block. NULL out Block to force
582 // lazy creation of another block.
583 Block = NULL;
584
585 // Return the condition block, which is the dominating block for the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000586 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000587}
588
589CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
590 // "do...while" is a control-flow statement. Thus we stop processing the
591 // current block.
592
593 CFGBlock* LoopSuccessor = NULL;
594
595 if (Block) {
596 FinishBlock(Block);
597 LoopSuccessor = Block;
598 }
599 else LoopSuccessor = Succ;
600
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000601 // Because of short-circuit evaluation, the condition of the loop
602 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
603 // blocks that evaluate the condition.
604 CFGBlock* ExitConditionBlock = createBlock(false);
605 CFGBlock* EntryConditionBlock = ExitConditionBlock;
606
607 // Set the terminator for the "exit" condition block.
608 ExitConditionBlock->setTerminator(D);
Ted Kremenek73543912007-08-23 21:42:29 +0000609
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000610 // Now add the actual condition to the condition block. Because the
611 // condition itself may contain control-flow, new blocks may be created.
612 if (Stmt* C = D->getCond()) {
613 Block = ExitConditionBlock;
614 EntryConditionBlock = addStmt(C);
615 if (Block) FinishBlock(EntryConditionBlock);
616 }
Ted Kremenek73543912007-08-23 21:42:29 +0000617
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000618 // The condition block is the implicit successor for the loop body as
619 // well as any code above the loop.
620 Succ = EntryConditionBlock;
621
622
Ted Kremenek73543912007-08-23 21:42:29 +0000623 // Process the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000624 CFGBlock* BodyBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000625 {
626 assert (D->getBody());
627
628 // Save the current values for Block, Succ, and continue and break targets
629 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
630 save_continue(ContinueTargetBlock),
631 save_break(BreakTargetBlock);
632
633 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000634 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000635
636 // All breaks should go to the code following the loop.
637 BreakTargetBlock = LoopSuccessor;
638
639 // NULL out Block to force lazy instantiation of blocks for the body.
640 Block = NULL;
641
642 // Create the body. The returned block is the entry to the loop body.
643 BodyBlock = Visit(D->getBody());
644 assert (BodyBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000645 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000646
647 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000648 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000649 }
650
651 // Link up the condition block with the code that follows the loop.
652 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000653 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000654
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000655 // There can be no more statements in the body block(s)
656 // since we loop back to the body. NULL out Block to force
Ted Kremenek73543912007-08-23 21:42:29 +0000657 // lazy creation of another block.
658 Block = NULL;
659
660 // Return the loop body, which is the dominating block for the loop.
661 return BodyBlock;
662}
663
664CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
665 // "continue" is a control-flow statement. Thus we stop processing the
666 // current block.
667 if (Block) FinishBlock(Block);
668
669 // Now create a new block that ends with the continue statement.
670 Block = createBlock(false);
671 Block->setTerminator(C);
672
673 // If there is no target for the continue, then we are looking at an
674 // incomplete AST. Handle this by not registering a successor.
675 if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
676
677 return Block;
678}
679
680CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
681 // "break" is a control-flow statement. Thus we stop processing the
682 // current block.
683 if (Block) FinishBlock(Block);
684
685 // Now create a new block that ends with the continue statement.
686 Block = createBlock(false);
687 Block->setTerminator(B);
688
689 // If there is no target for the break, then we are looking at an
690 // incomplete AST. Handle this by not registering a successor.
691 if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
692
693 return Block;
694}
695
696CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* S) {
697 // "switch" is a control-flow statement. Thus we stop processing the
698 // current block.
699 CFGBlock* SwitchSuccessor = NULL;
700
701 if (Block) {
702 FinishBlock(Block);
703 SwitchSuccessor = Block;
704 }
705 else SwitchSuccessor = Succ;
706
707 // Save the current "switch" context.
708 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
709 save_break(BreakTargetBlock);
710
711 // Create a new block that will contain the switch statement.
712 SwitchTerminatedBlock = createBlock(false);
713
Ted Kremenek73543912007-08-23 21:42:29 +0000714 // Now process the switch body. The code after the switch is the implicit
715 // successor.
716 Succ = SwitchSuccessor;
717 BreakTargetBlock = SwitchSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +0000718
719 // When visiting the body, the case statements should automatically get
720 // linked up to the switch. We also don't keep a pointer to the body,
721 // since all control-flow from the switch goes to case/default statements.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000722 assert (S->getBody() && "switch must contain a non-NULL body");
723 Block = NULL;
724 CFGBlock *BodyBlock = Visit(S->getBody());
725 if (Block) FinishBlock(BodyBlock);
726
727 // Add the terminator and condition in the switch block.
728 SwitchTerminatedBlock->setTerminator(S);
729 assert (S->getCond() && "switch condition must be non-NULL");
Ted Kremenek73543912007-08-23 21:42:29 +0000730 Block = SwitchTerminatedBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000731 return addStmt(S->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +0000732}
733
734CFGBlock* CFGBuilder::VisitSwitchCase(SwitchCase* S) {
735 // A SwitchCase is either a "default" or "case" statement. We handle
736 // both in the same way. They are essentially labels, so they are the
737 // first statement in a block.
738 CFGBlock* CaseBlock = Visit(S->getSubStmt());
739 assert (CaseBlock);
740
741 // Cases/Default statements parition block, so this is the top of
742 // the basic block we were processing (the case/default is the first stmt).
743 CaseBlock->appendStmt(S);
744 FinishBlock(CaseBlock);
745
746 // Add this block to the list of successors for the block with the
747 // switch statement.
748 if (SwitchTerminatedBlock) SwitchTerminatedBlock->addSuccessor(CaseBlock);
749
750 // We set Block to NULL to allow lazy creation of a new block (if necessary)
751 Block = NULL;
752
753 // This block is now the implicit successor of other blocks.
754 Succ = CaseBlock;
755
756 return CaseBlock;
757}
758
759
Ted Kremenekd6e50602007-08-23 21:26:19 +0000760} // end anonymous namespace
Ted Kremenek4db5b452007-08-23 16:51:22 +0000761
762/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
763/// block has no successors or predecessors. If this is the first block
764/// created in the CFG, it is automatically set to be the Entry and Exit
765/// of the CFG.
766CFGBlock* CFG::createBlock(unsigned blockID) {
767 bool first_block = begin() == end();
768
769 // Create the block.
770 Blocks.push_front(CFGBlock(blockID));
771
772 // If this is the first block, set it as the Entry and Exit.
773 if (first_block) Entry = Exit = &front();
774
775 // Return the block.
776 return &front();
Ted Kremenek97f75312007-08-21 21:42:03 +0000777}
778
Ted Kremenek4db5b452007-08-23 16:51:22 +0000779/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
780/// CFG is returned to the caller.
781CFG* CFG::buildCFG(Stmt* Statement) {
782 CFGBuilder Builder;
783 return Builder.buildCFG(Statement);
784}
785
786/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek97f75312007-08-21 21:42:03 +0000787void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
788
Ted Kremenek4db5b452007-08-23 16:51:22 +0000789/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek97f75312007-08-21 21:42:03 +0000790void CFG::dump() { print(std::cerr); }
791
Ted Kremenek4db5b452007-08-23 16:51:22 +0000792/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenek97f75312007-08-21 21:42:03 +0000793void CFG::print(std::ostream& OS) {
Ted Kremenek4db5b452007-08-23 16:51:22 +0000794 // Print the Entry block.
Ted Kremenekbec06e82007-08-22 21:05:42 +0000795 if (begin() != end()) {
796 CFGBlock& Entry = getEntry();
797 OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n";
798 Entry.print(OS);
799 }
800
Ted Kremenek4db5b452007-08-23 16:51:22 +0000801 // Iterate through the CFGBlocks and print them one by one.
Ted Kremenek97f75312007-08-21 21:42:03 +0000802 for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
Ted Kremenekbec06e82007-08-22 21:05:42 +0000803 // Skip the entry block, because we already printed it.
Ted Kremenek4db5b452007-08-23 16:51:22 +0000804 if (&(*I) == &getEntry() || &(*I) == &getExit()) continue;
Ted Kremenekbec06e82007-08-22 21:05:42 +0000805
Ted Kremenek4db5b452007-08-23 16:51:22 +0000806 OS << "\n [ B" << I->getBlockID() << " ]\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000807 I->print(OS);
808 }
Ted Kremenek73543912007-08-23 21:42:29 +0000809
Ted Kremenek4db5b452007-08-23 16:51:22 +0000810 // Print the Exit Block.
811 if (begin() != end()) {
812 CFGBlock& Exit = getExit();
813 OS << "\n [ B" << Exit.getBlockID() << " (EXIT) ]\n";
814 Exit.print(OS);
815 }
Ted Kremenek73543912007-08-23 21:42:29 +0000816
Ted Kremenek97f75312007-08-21 21:42:03 +0000817 OS << "\n";
818}
819
Ted Kremenekd8313202007-08-22 18:22:34 +0000820
821namespace {
822
Ted Kremenek73543912007-08-23 21:42:29 +0000823class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint,
824 void > {
825 std::ostream& OS;
826public:
827 CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {}
828
829 void VisitIfStmt(IfStmt* I) {
830 OS << "if ";
831 I->getCond()->printPretty(std::cerr);
832 OS << "\n";
833 }
834
835 // Default case.
836 void VisitStmt(Stmt* S) { S->printPretty(OS); }
837
838 void VisitForStmt(ForStmt* F) {
839 OS << "for (" ;
840 if (Stmt* I = F->getInit()) I->printPretty(OS);
841 OS << " ; ";
842 if (Stmt* C = F->getCond()) C->printPretty(OS);
843 OS << " ; ";
844 if (Stmt* I = F->getInc()) I->printPretty(OS);
845 OS << ")\n";
846 }
847
848 void VisitWhileStmt(WhileStmt* W) {
849 OS << "while " ;
850 if (Stmt* C = W->getCond()) C->printPretty(OS);
851 OS << "\n";
852 }
853
854 void VisitDoStmt(DoStmt* D) {
855 OS << "do ... while ";
856 if (Stmt* C = D->getCond()) C->printPretty(OS);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000857 OS << '\n';
858 }
859
860 void VisitSwitchStmt(SwitchStmt* S) {
861 OS << "switch ";
862 S->getCond()->printPretty(OS);
863 OS << '\n';
864 }
865
Ted Kremenekcfaae762007-08-27 21:54:41 +0000866 void VisitExpr(Expr* E) {
867 E->printPretty(OS);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000868 OS << '\n';
Ted Kremenekcfaae762007-08-27 21:54:41 +0000869 }
Ted Kremenek73543912007-08-23 21:42:29 +0000870};
871} // end anonymous namespace
Ted Kremenekd8313202007-08-22 18:22:34 +0000872
Ted Kremenek4db5b452007-08-23 16:51:22 +0000873/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek97f75312007-08-21 21:42:03 +0000874void CFGBlock::dump() { print(std::cerr); }
875
Ted Kremenek4db5b452007-08-23 16:51:22 +0000876/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
877/// Generally this will only be called from CFG::print.
Ted Kremenek97f75312007-08-21 21:42:03 +0000878void CFGBlock::print(std::ostream& OS) {
879
880 // Iterate through the statements in the block and print them.
881 OS << " ------------------------\n";
882 unsigned j = 1;
883 for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) {
Ted Kremenekc5de2222007-08-21 23:26:17 +0000884 // Print the statement # in the basic block.
885 OS << " " << std::setw(3) << j << ": ";
886
887 // Print the statement/expression.
888 Stmt* S = *I;
889
890 if (LabelStmt* L = dyn_cast<LabelStmt>(S))
891 OS << L->getName() << ": (LABEL)\n";
892 else
893 (*I)->printPretty(OS);
894
895 // Expressions need a newline.
Ted Kremenek97f75312007-08-21 21:42:03 +0000896 if (isa<Expr>(*I)) OS << '\n';
897 }
898 OS << " ------------------------\n";
899
900 // Print the predecessors of this block.
901 OS << " Predecessors (" << pred_size() << "):";
902 unsigned i = 0;
903 for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) {
904 if (i == 8 || (i-8) == 0) {
905 OS << "\n ";
906 }
907 OS << " B" << (*I)->getBlockID();
908 }
909
910 // Print the terminator of this block.
911 OS << "\n Terminator: ";
Ted Kremenekd8313202007-08-22 18:22:34 +0000912 if (ControlFlowStmt)
913 CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt);
914 else
915 OS << "<NULL>\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000916
917 // Print the successors of this block.
918 OS << " Successors (" << succ_size() << "):";
919 i = 0;
920 for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) {
921 if (i == 8 || (i-8) % 10 == 0) {
922 OS << "\n ";
923 }
924 OS << " B" << (*I)->getBlockID();
925 }
926 OS << '\n';
Ted Kremenek4db5b452007-08-23 16:51:22 +0000927}