blob: e601d4ff69eb4bbc0078cabe21389e22f8145053 [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//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek97f75312007-08-21 21:42:03 +00007//
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"
Ted Kremenek95e854d2007-08-21 22:06:14 +000016#include "clang/AST/StmtVisitor.h"
Ted Kremenek08176a52007-08-31 21:30:12 +000017#include "clang/AST/PrettyPrinter.h"
Ted Kremenekc5de2222007-08-21 23:26:17 +000018#include "llvm/ADT/DenseMap.h"
Ted Kremenek0edd3a92007-08-28 19:26:49 +000019#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenekb3bb91b2007-08-29 21:56:09 +000020#include "llvm/Support/GraphWriter.h"
Ted Kremenek56c939e2007-12-17 19:35:20 +000021#include "llvm/Support/Streams.h"
Ted Kremenek98cee3a2008-01-08 18:15:10 +000022#include "llvm/Support/Compiler.h"
Ted Kremenekd058a9c2008-04-28 18:00:46 +000023#include <llvm/Support/Allocator.h>
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000024#include <llvm/Support/Format.h>
Ted Kremenek97f75312007-08-21 21:42:03 +000025#include <iomanip>
26#include <algorithm>
Ted Kremenekb3bb91b2007-08-29 21:56:09 +000027#include <sstream>
Ted Kremenek5ee98a72008-01-11 00:40:29 +000028
Ted Kremenek97f75312007-08-21 21:42:03 +000029using namespace clang;
30
31namespace {
32
Ted Kremenekd6e50602007-08-23 21:26:19 +000033// SaveAndRestore - A utility class that uses RIIA to save and restore
34// the value of a variable.
35template<typename T>
Ted Kremenek98cee3a2008-01-08 18:15:10 +000036struct VISIBILITY_HIDDEN SaveAndRestore {
Ted Kremenekd6e50602007-08-23 21:26:19 +000037 SaveAndRestore(T& x) : X(x), old_value(x) {}
38 ~SaveAndRestore() { X = old_value; }
Ted Kremenek44db7872007-08-30 18:13:31 +000039 T get() { return old_value; }
40
Ted Kremenekd6e50602007-08-23 21:26:19 +000041 T& X;
42 T old_value;
43};
Ted Kremenek97f75312007-08-21 21:42:03 +000044
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000045static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek0865a992008-08-06 23:20:50 +000046 if (VarDecl* VD = dyn_cast<VarDecl>(D))
47 if (Expr* Ex = VD->getInit())
48 return Ex->getSourceRange().getEnd();
49
50 return D->getLocation();
51}
52
Ted Kremeneka3195a32008-08-04 22:51:42 +000053/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek97f75312007-08-21 21:42:03 +000054/// The builder is stateful: an instance of the builder should be used to only
55/// construct a single CFG.
56///
57/// Example usage:
58///
59/// CFGBuilder builder;
60/// CFG* cfg = builder.BuildAST(stmt1);
61///
Ted Kremenek95e854d2007-08-21 22:06:14 +000062/// CFG construction is done via a recursive walk of an AST.
63/// We actually parse the AST in reverse order so that the successor
64/// of a basic block is constructed prior to its predecessor. This
65/// allows us to nicely capture implicit fall-throughs without extra
66/// basic blocks.
67///
Ted Kremenek98cee3a2008-01-08 18:15:10 +000068class VISIBILITY_HIDDEN CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenek97f75312007-08-21 21:42:03 +000069 CFG* cfg;
70 CFGBlock* Block;
Ted Kremenek97f75312007-08-21 21:42:03 +000071 CFGBlock* Succ;
Ted Kremenekf511d672007-08-22 21:36:54 +000072 CFGBlock* ContinueTargetBlock;
Ted Kremenekf308d372007-08-22 21:51:58 +000073 CFGBlock* BreakTargetBlock;
Ted Kremeneke809ebf2007-08-23 18:43:24 +000074 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek97bc3422008-02-13 22:05:39 +000075 CFGBlock* DefaultCaseBlock;
Ted Kremenek97f75312007-08-21 21:42:03 +000076
Ted Kremenek0edd3a92007-08-28 19:26:49 +000077 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenekc5de2222007-08-21 23:26:17 +000078 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
79 LabelMapTy LabelMap;
80
Ted Kremenek0edd3a92007-08-28 19:26:49 +000081 // A list of blocks that end with a "goto" that must be backpatched to
82 // their resolved targets upon completion of CFG construction.
Ted Kremenekf5392b72007-08-22 15:40:58 +000083 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenekc5de2222007-08-21 23:26:17 +000084 BackpatchBlocksTy BackpatchBlocks;
85
Ted Kremenek0edd3a92007-08-28 19:26:49 +000086 // A list of labels whose address has been taken (for indirect gotos).
87 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
88 LabelSetTy AddressTakenLabels;
89
Ted Kremenek97f75312007-08-21 21:42:03 +000090public:
Ted Kremenek4db5b452007-08-23 16:51:22 +000091 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenekf308d372007-08-22 21:51:58 +000092 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek97bc3422008-02-13 22:05:39 +000093 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenek97f75312007-08-21 21:42:03 +000094 // Create an empty CFG.
95 cfg = new CFG();
96 }
97
98 ~CFGBuilder() { delete cfg; }
Ted Kremenek97f75312007-08-21 21:42:03 +000099
Ted Kremenek73543912007-08-23 21:42:29 +0000100 // buildCFG - Used by external clients to construct the CFG.
101 CFG* buildCFG(Stmt* Statement);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000102
Ted Kremenek73543912007-08-23 21:42:29 +0000103 // Visitors to walk an AST and construct the CFG. Called by
104 // buildCFG. Do not call directly!
Ted Kremenekd8313202007-08-22 18:22:34 +0000105
Ted Kremenek73543912007-08-23 21:42:29 +0000106 CFGBlock* VisitBreakStmt(BreakStmt* B);
Ted Kremenek79f0a632008-04-16 21:10:48 +0000107 CFGBlock* VisitCaseStmt(CaseStmt* Terminator);
Ted Kremenek05335162008-11-11 17:10:00 +0000108 CFGBlock* VisitCompoundStmt(CompoundStmt* C);
109 CFGBlock* VisitContinueStmt(ContinueStmt* C);
Ted Kremenekc07a8af2008-02-13 21:46:34 +0000110 CFGBlock* VisitDefaultStmt(DefaultStmt* D);
Ted Kremenek05335162008-11-11 17:10:00 +0000111 CFGBlock* VisitDoStmt(DoStmt* D);
112 CFGBlock* VisitForStmt(ForStmt* F);
113 CFGBlock* VisitGotoStmt(GotoStmt* G);
114 CFGBlock* VisitIfStmt(IfStmt* I);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000115 CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I);
Ted Kremenek05335162008-11-11 17:10:00 +0000116 CFGBlock* VisitLabelStmt(LabelStmt* L);
117 CFGBlock* VisitNullStmt(NullStmt* Statement);
118 CFGBlock* VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
119 CFGBlock* VisitReturnStmt(ReturnStmt* R);
120 CFGBlock* VisitStmt(Stmt* Statement);
121 CFGBlock* VisitSwitchStmt(SwitchStmt* Terminator);
122 CFGBlock* VisitWhileStmt(WhileStmt* W);
Ted Kremenek97f75312007-08-21 21:42:03 +0000123
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000124 // FIXME: Add support for ObjC-specific control-flow structures.
125
Ted Kremenekd058a9c2008-04-28 18:00:46 +0000126 // NYS == Not Yet Supported
127 CFGBlock* NYS() {
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000128 badCFG = true;
129 return Block;
130 }
131
Ted Kremenek72037962009-03-30 22:29:21 +0000132 CFGBlock* VisitObjCAtTryStmt(ObjCAtTryStmt* S);
133 CFGBlock* VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
134 // FIXME: For now we pretend that @catch and the code it contains
135 // does not exit.
136 return Block;
137 }
138
Ted Kremenekc74ac3e2008-12-09 20:20:09 +0000139 // FIXME: This is not completely supported. We basically @throw like
140 // a 'return'.
141 CFGBlock* VisitObjCAtThrowStmt(ObjCAtThrowStmt* S);
Ted Kremenekd058a9c2008-04-28 18:00:46 +0000142
143 CFGBlock* VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S){
144 return NYS();
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000145 }
146
Ted Kremenekd68a8d32008-09-26 18:17:07 +0000147 // Blocks.
148 CFGBlock* VisitBlockExpr(BlockExpr* E) { return NYS(); }
149 CFGBlock* VisitBlockDeclRefExpr(BlockDeclRefExpr* E) { return NYS(); }
150
Ted Kremenek73543912007-08-23 21:42:29 +0000151private:
152 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenek79f0a632008-04-16 21:10:48 +0000153 CFGBlock* addStmt(Stmt* Terminator);
154 CFGBlock* WalkAST(Stmt* Terminator, bool AlwaysAddStmt);
155 CFGBlock* WalkAST_VisitChildren(Stmt* Terminator);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000156 CFGBlock* WalkAST_VisitDeclSubExpr(Decl* D);
Ted Kremenek79f0a632008-04-16 21:10:48 +0000157 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* Terminator);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000158 bool FinishBlock(CFGBlock* B);
Ted Kremenekd8313202007-08-22 18:22:34 +0000159
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000160 bool badCFG;
Ted Kremenek97f75312007-08-21 21:42:03 +0000161};
Ted Kremenek09535672008-09-26 22:58:57 +0000162
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000163// FIXME: Add support for dependent-sized array types in C++?
164// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek09535672008-09-26 22:58:57 +0000165static VariableArrayType* FindVA(Type* t) {
166 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
167 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
168 if (vat->getSizeExpr())
169 return vat;
170
171 t = vt->getElementType().getTypePtr();
172 }
173
174 return 0;
175}
Ted Kremenek73543912007-08-23 21:42:29 +0000176
177/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
178/// represent an arbitrary statement. Examples include a single expression
179/// or a function body (compound statement). The ownership of the returned
180/// CFG is transferred to the caller. If CFG construction fails, this method
181/// returns NULL.
182CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000183 assert (cfg);
Ted Kremenek73543912007-08-23 21:42:29 +0000184 if (!Statement) return NULL;
185
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000186 badCFG = false;
187
Ted Kremenek73543912007-08-23 21:42:29 +0000188 // Create an empty block that will serve as the exit block for the CFG.
189 // Since this is the first block added to the CFG, it will be implicitly
190 // registered as the exit block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000191 Succ = createBlock();
192 assert (Succ == &cfg->getExit());
193 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenek73543912007-08-23 21:42:29 +0000194
195 // Visit the statements and create the CFG.
Ted Kremenekfa38c7a2008-02-27 17:33:02 +0000196 CFGBlock* B = Visit(Statement);
197 if (!B) B = Succ;
198
199 if (B) {
Ted Kremenek73543912007-08-23 21:42:29 +0000200 // Finalize the last constructed block. This usually involves
201 // reversing the order of the statements in the block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000202 if (Block) FinishBlock(B);
Ted Kremenek73543912007-08-23 21:42:29 +0000203
204 // Backpatch the gotos whose label -> block mappings we didn't know
205 // when we encountered them.
206 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
207 E = BackpatchBlocks.end(); I != E; ++I ) {
208
209 CFGBlock* B = *I;
210 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
211 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
212
213 // If there is no target for the goto, then we are looking at an
214 // incomplete AST. Handle this by not registering a successor.
215 if (LI == LabelMap.end()) continue;
216
217 B->addSuccessor(LI->second);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000218 }
Ted Kremenek73543912007-08-23 21:42:29 +0000219
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000220 // Add successors to the Indirect Goto Dispatch block (if we have one).
221 if (CFGBlock* B = cfg->getIndirectGotoBlock())
222 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
223 E = AddressTakenLabels.end(); I != E; ++I ) {
224
225 // Lookup the target block.
226 LabelMapTy::iterator LI = LabelMap.find(*I);
227
228 // If there is no target block that contains label, then we are looking
229 // at an incomplete AST. Handle this by not registering a successor.
230 if (LI == LabelMap.end()) continue;
231
232 B->addSuccessor(LI->second);
233 }
Ted Kremenek680fcb82007-09-26 21:23:31 +0000234
Ted Kremenek844cb4d2007-09-17 16:18:02 +0000235 Succ = B;
Ted Kremenek680fcb82007-09-26 21:23:31 +0000236 }
237
238 // Create an empty entry block that has no predecessors.
239 cfg->setEntry(createBlock());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000240
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000241 if (badCFG) {
242 delete cfg;
243 cfg = NULL;
244 return NULL;
245 }
246
Ted Kremenek680fcb82007-09-26 21:23:31 +0000247 // NULL out cfg so that repeated calls to the builder will fail and that
248 // the ownership of the constructed CFG is passed to the caller.
249 CFG* t = cfg;
250 cfg = NULL;
251 return t;
Ted Kremenek73543912007-08-23 21:42:29 +0000252}
253
254/// createBlock - Used to lazily create blocks that are connected
255/// to the current (global) succcessor.
256CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek14594572007-09-05 20:02:05 +0000257 CFGBlock* B = cfg->createBlock();
Ted Kremenek73543912007-08-23 21:42:29 +0000258 if (add_successor && Succ) B->addSuccessor(Succ);
259 return B;
260}
261
262/// FinishBlock - When the last statement has been added to the block,
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000263/// we must reverse the statements because they have been inserted
264/// in reverse order.
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000265bool CFGBuilder::FinishBlock(CFGBlock* B) {
266 if (badCFG)
267 return false;
268
Ted Kremenek73543912007-08-23 21:42:29 +0000269 assert (B);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000270 B->reverseStmts();
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000271 return true;
Ted Kremenek73543912007-08-23 21:42:29 +0000272}
273
Ted Kremenek65cfa562007-08-27 21:27:44 +0000274/// addStmt - Used to add statements/expressions to the current CFGBlock
275/// "Block". This method calls WalkAST on the passed statement to see if it
276/// contains any short-circuit expressions. If so, it recursively creates
277/// the necessary blocks for such expressions. It returns the "topmost" block
278/// of the created blocks, or the original value of "Block" when this method
279/// was called if no additional blocks are created.
Ted Kremenek79f0a632008-04-16 21:10:48 +0000280CFGBlock* CFGBuilder::addStmt(Stmt* Terminator) {
Ted Kremenek390b9762007-08-30 18:39:40 +0000281 if (!Block) Block = createBlock();
Ted Kremenek79f0a632008-04-16 21:10:48 +0000282 return WalkAST(Terminator,true);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000283}
284
285/// WalkAST - Used by addStmt to walk the subtree of a statement and
Ted Kremeneke822b622007-08-28 18:14:37 +0000286/// add extra blocks for ternary operators, &&, and ||. We also
287/// process "," and DeclStmts (which may contain nested control-flow).
Ted Kremenek79f0a632008-04-16 21:10:48 +0000288CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) {
289 switch (Terminator->getStmtClass()) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000290 case Stmt::ConditionalOperatorClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000291 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000292
293 // Create the confluence block that will "merge" the results
294 // of the ternary expression.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000295 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
296 ConfluenceBlock->appendStmt(C);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000297 if (!FinishBlock(ConfluenceBlock))
298 return 0;
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000299
300 // Create a block for the LHS expression if there is an LHS expression.
301 // A GCC extension allows LHS to be NULL, causing the condition to
302 // be the value that is returned instead.
303 // e.g: x ?: y is shorthand for: x ? x : y;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000304 Succ = ConfluenceBlock;
305 Block = NULL;
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000306 CFGBlock* LHSBlock = NULL;
307 if (C->getLHS()) {
308 LHSBlock = Visit(C->getLHS());
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000309 if (!FinishBlock(LHSBlock))
310 return 0;
311 Block = NULL;
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000312 }
Ted Kremenek65cfa562007-08-27 21:27:44 +0000313
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000314 // Create the block for the RHS expression.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000315 Succ = ConfluenceBlock;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000316 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000317 if (!FinishBlock(RHSBlock))
318 return 0;
319
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000320 // Create the block that will contain the condition.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000321 Block = createBlock(false);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000322
323 if (LHSBlock)
324 Block->addSuccessor(LHSBlock);
325 else {
326 // If we have no LHS expression, add the ConfluenceBlock as a direct
327 // successor for the block containing the condition. Moreover,
328 // we need to reverse the order of the predecessors in the
329 // ConfluenceBlock because the RHSBlock will have been added to
330 // the succcessors already, and we want the first predecessor to the
331 // the block containing the expression for the case when the ternary
332 // expression evaluates to true.
333 Block->addSuccessor(ConfluenceBlock);
334 assert (ConfluenceBlock->pred_size() == 2);
335 std::reverse(ConfluenceBlock->pred_begin(),
336 ConfluenceBlock->pred_end());
337 }
338
Ted Kremenek65cfa562007-08-27 21:27:44 +0000339 Block->addSuccessor(RHSBlock);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000340
Ted Kremenek65cfa562007-08-27 21:27:44 +0000341 Block->setTerminator(C);
342 return addStmt(C->getCond());
343 }
Ted Kremenek7f788422007-08-31 17:03:41 +0000344
345 case Stmt::ChooseExprClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000346 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Ted Kremenek7f788422007-08-31 17:03:41 +0000347
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000348 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek7f788422007-08-31 17:03:41 +0000349 ConfluenceBlock->appendStmt(C);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000350 if (!FinishBlock(ConfluenceBlock))
351 return 0;
Ted Kremenek7f788422007-08-31 17:03:41 +0000352
353 Succ = ConfluenceBlock;
354 Block = NULL;
355 CFGBlock* LHSBlock = Visit(C->getLHS());
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000356 if (!FinishBlock(LHSBlock))
357 return 0;
Ted Kremenekd11620d2007-09-11 21:29:43 +0000358
Ted Kremenek7f788422007-08-31 17:03:41 +0000359 Succ = ConfluenceBlock;
360 Block = NULL;
361 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000362 if (!FinishBlock(RHSBlock))
363 return 0;
Ted Kremenek7f788422007-08-31 17:03:41 +0000364
365 Block = createBlock(false);
366 Block->addSuccessor(LHSBlock);
367 Block->addSuccessor(RHSBlock);
368 Block->setTerminator(C);
369 return addStmt(C->getCond());
370 }
Ted Kremenek666a6af2007-08-28 16:18:58 +0000371
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000372 case Stmt::DeclStmtClass: {
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000373 DeclStmt *DS = cast<DeclStmt>(Terminator);
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000374 if (DS->isSingleDecl()) {
Ted Kremenek0865a992008-08-06 23:20:50 +0000375 Block->appendStmt(Terminator);
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000376 return WalkAST_VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenek0865a992008-08-06 23:20:50 +0000377 }
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000378
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000379 CFGBlock* B = 0;
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000380
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000381 // FIXME: Add a reverse iterator for DeclStmt to avoid this
382 // extra copy.
Chris Lattnerffae0dd2009-03-28 06:53:40 +0000383 typedef llvm::SmallVector<Decl*,10> BufTy;
384 BufTy Buf(DS->decl_begin(), DS->decl_end());
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000385
386 for (BufTy::reverse_iterator I=Buf.rbegin(), E=Buf.rend(); I!=E; ++I) {
387 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
388 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
389 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000390
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000391 // Allocate the DeclStmt using the BumpPtrAllocator. It will
392 // get automatically freed with the CFG.
393 DeclGroupRef DG(*I);
394 Decl* D = *I;
395 void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
396
Chris Lattnerffae0dd2009-03-28 06:53:40 +0000397 DeclStmt* DS = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000398
399 // Append the fake DeclStmt to block.
400 Block->appendStmt(DS);
401 B = WalkAST_VisitDeclSubExpr(D);
Ted Kremenek0865a992008-08-06 23:20:50 +0000402 }
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000403 return B;
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000404 }
Ted Kremenek0865a992008-08-06 23:20:50 +0000405
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000406 case Stmt::AddrLabelExprClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000407 AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000408 AddressTakenLabels.insert(A->getLabel());
409
Ted Kremenek79f0a632008-04-16 21:10:48 +0000410 if (AlwaysAddStmt) Block->appendStmt(Terminator);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000411 return Block;
412 }
Ted Kremenekd11620d2007-09-11 21:29:43 +0000413
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000414 case Stmt::StmtExprClass:
Ted Kremenek79f0a632008-04-16 21:10:48 +0000415 return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
Ted Kremeneke822b622007-08-28 18:14:37 +0000416
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000417 case Stmt::SizeOfAlignOfExprClass: {
418 SizeOfAlignOfExpr* E = cast<SizeOfAlignOfExpr>(Terminator);
Ted Kremenek09535672008-09-26 22:58:57 +0000419
420 // VLA types have expressions that must be evaluated.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000421 if (E->isArgumentType()) {
422 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
423 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
424 addStmt(VA->getSizeExpr());
425 }
426 // Expressions in sizeof/alignof are not evaluated and thus have no
427 // control flow.
428 else
429 Block->appendStmt(Terminator);
Ted Kremenek09535672008-09-26 22:58:57 +0000430
431 return Block;
432 }
433
Ted Kremenekcfaae762007-08-27 21:54:41 +0000434 case Stmt::BinaryOperatorClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000435 BinaryOperator* B = cast<BinaryOperator>(Terminator);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000436
437 if (B->isLogicalOp()) { // && or ||
438 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
439 ConfluenceBlock->appendStmt(B);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000440 if (!FinishBlock(ConfluenceBlock))
441 return 0;
Ted Kremenekcfaae762007-08-27 21:54:41 +0000442
443 // create the block evaluating the LHS
444 CFGBlock* LHSBlock = createBlock(false);
Ted Kremenekb2348522007-12-21 19:49:00 +0000445 LHSBlock->setTerminator(B);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000446
447 // create the block evaluating the RHS
448 Succ = ConfluenceBlock;
449 Block = NULL;
450 CFGBlock* RHSBlock = Visit(B->getRHS());
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000451 if (!FinishBlock(RHSBlock))
452 return 0;
Ted Kremenekb2348522007-12-21 19:49:00 +0000453
454 // Now link the LHSBlock with RHSBlock.
455 if (B->getOpcode() == BinaryOperator::LOr) {
456 LHSBlock->addSuccessor(ConfluenceBlock);
457 LHSBlock->addSuccessor(RHSBlock);
458 }
459 else {
460 assert (B->getOpcode() == BinaryOperator::LAnd);
461 LHSBlock->addSuccessor(RHSBlock);
462 LHSBlock->addSuccessor(ConfluenceBlock);
463 }
Ted Kremenekcfaae762007-08-27 21:54:41 +0000464
465 // Generate the blocks for evaluating the LHS.
466 Block = LHSBlock;
467 return addStmt(B->getLHS());
Ted Kremeneke822b622007-08-28 18:14:37 +0000468 }
469 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
470 Block->appendStmt(B);
471 addStmt(B->getRHS());
472 return addStmt(B->getLHS());
Ted Kremenek3a819822007-10-01 19:33:33 +0000473 }
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000474
475 break;
Ted Kremenekcfaae762007-08-27 21:54:41 +0000476 }
Ted Kremenekd68a8d32008-09-26 18:17:07 +0000477
478 // Blocks: No support for blocks ... yet
479 case Stmt::BlockExprClass:
480 case Stmt::BlockDeclRefExprClass:
481 return NYS();
Ted Kremeneka9ba5cc2008-02-26 02:37:08 +0000482
483 case Stmt::ParenExprClass:
Ted Kremenek79f0a632008-04-16 21:10:48 +0000484 return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000485
Ted Kremenek65cfa562007-08-27 21:27:44 +0000486 default:
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000487 break;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000488 };
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000489
Ted Kremenek79f0a632008-04-16 21:10:48 +0000490 if (AlwaysAddStmt) Block->appendStmt(Terminator);
491 return WalkAST_VisitChildren(Terminator);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000492}
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000493
Ted Kremenek0865a992008-08-06 23:20:50 +0000494/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions
495/// for initializers in Decls.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000496CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(Decl* D) {
Ted Kremenek0865a992008-08-06 23:20:50 +0000497 VarDecl* VD = dyn_cast<VarDecl>(D);
498
499 if (!VD)
Ted Kremenekf4e35622007-11-18 20:06:01 +0000500 return Block;
501
Ted Kremenek0865a992008-08-06 23:20:50 +0000502 Expr* Init = VD->getInit();
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000503
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000504 if (Init) {
Mike Stumpd8472f22009-02-26 08:00:25 +0000505 // Optimization: Don't create separate block-level statements for literals.
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000506 switch (Init->getStmtClass()) {
507 case Stmt::IntegerLiteralClass:
508 case Stmt::CharacterLiteralClass:
509 case Stmt::StringLiteralClass:
510 break;
511 default:
512 Block = addStmt(Init);
513 }
Ted Kremenek4ad64e82008-02-29 22:32:24 +0000514 }
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000515
516 // If the type of VD is a VLA, then we must process its size expressions.
517 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
518 VA = FindVA(VA->getElementType().getTypePtr()))
519 Block = addStmt(VA->getSizeExpr());
Ted Kremenek4ad64e82008-02-29 22:32:24 +0000520
Ted Kremeneke822b622007-08-28 18:14:37 +0000521 return Block;
522}
523
Ted Kremenek65cfa562007-08-27 21:27:44 +0000524/// WalkAST_VisitChildren - Utility method to call WalkAST on the
525/// children of a Stmt.
Ted Kremenek79f0a632008-04-16 21:10:48 +0000526CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000527 CFGBlock* B = Block;
Mike Stumpd8472f22009-02-26 08:00:25 +0000528 for (Stmt::child_iterator I = Terminator->child_begin(),
529 E = Terminator->child_end();
Ted Kremenek65cfa562007-08-27 21:27:44 +0000530 I != E; ++I)
Ted Kremenek680fcb82007-09-26 21:23:31 +0000531 if (*I) B = WalkAST(*I);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000532
533 return B;
534}
535
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000536/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
537/// expressions (a GCC extension).
Ted Kremenek79f0a632008-04-16 21:10:48 +0000538CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
539 Block->appendStmt(Terminator);
540 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000541}
542
Ted Kremenek73543912007-08-23 21:42:29 +0000543/// VisitStmt - Handle statements with no branching control flow.
544CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
545 // We cannot assume that we are in the middle of a basic block, since
546 // the CFG might only be constructed for this single statement. If
547 // we have no current basic block, just create one lazily.
548 if (!Block) Block = createBlock();
549
550 // Simply add the statement to the current block. We actually
551 // insert statements in reverse order; this order is reversed later
552 // when processing the containing element in the AST.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000553 addStmt(Statement);
554
Ted Kremenek73543912007-08-23 21:42:29 +0000555 return Block;
556}
557
558CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
559 return Block;
560}
561
562CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000563
564 CFGBlock* LastBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000565
Ted Kremenekfeb0e992008-02-26 00:22:58 +0000566 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
567 I != E; ++I ) {
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000568 LastBlock = Visit(*I);
Ted Kremenekfeb0e992008-02-26 00:22:58 +0000569 }
570
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000571 return LastBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000572}
573
574CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
575 // We may see an if statement in the middle of a basic block, or
576 // it may be the first statement we are processing. In either case,
577 // we create a new basic block. First, we create the blocks for
578 // the then...else statements, and then we create the block containing
579 // the if statement. If we were in the middle of a block, we
580 // stop processing that block and reverse its statements. That block
581 // is then the implicit successor for the "then" and "else" clauses.
582
583 // The block we were proccessing is now finished. Make it the
584 // successor block.
585 if (Block) {
586 Succ = Block;
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000587 if (!FinishBlock(Block))
588 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +0000589 }
590
591 // Process the false branch. NULL out Block so that the recursive
592 // call to Visit will create a new basic block.
593 // Null out Block so that all successor
594 CFGBlock* ElseBlock = Succ;
595
596 if (Stmt* Else = I->getElse()) {
597 SaveAndRestore<CFGBlock*> sv(Succ);
598
599 // NULL out Block so that the recursive call to Visit will
600 // create a new basic block.
601 Block = NULL;
Ted Kremenek44db7872007-08-30 18:13:31 +0000602 ElseBlock = Visit(Else);
603
604 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
605 ElseBlock = sv.get();
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000606 else if (Block) {
607 if (!FinishBlock(ElseBlock))
608 return 0;
609 }
Ted Kremenek73543912007-08-23 21:42:29 +0000610 }
611
612 // Process the true branch. NULL out Block so that the recursive
613 // call to Visit will create a new basic block.
614 // Null out Block so that all successor
615 CFGBlock* ThenBlock;
616 {
617 Stmt* Then = I->getThen();
618 assert (Then);
619 SaveAndRestore<CFGBlock*> sv(Succ);
620 Block = NULL;
Ted Kremenek44db7872007-08-30 18:13:31 +0000621 ThenBlock = Visit(Then);
622
Ted Kremeneke5a3c022009-04-01 03:52:47 +0000623 if (!ThenBlock) {
624 // We can reach here if the "then" body has all NullStmts.
625 // Create an empty block so we can distinguish between true and false
626 // branches in path-sensitive analyses.
627 ThenBlock = createBlock(false);
628 ThenBlock->addSuccessor(sv.get());
629 }
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000630 else if (Block) {
631 if (!FinishBlock(ThenBlock))
632 return 0;
633 }
Ted Kremenek73543912007-08-23 21:42:29 +0000634 }
635
636 // Now create a new block containing the if statement.
637 Block = createBlock(false);
Ted Kremenek73543912007-08-23 21:42:29 +0000638
639 // Set the terminator of the new block to the If statement.
640 Block->setTerminator(I);
641
642 // Now add the successors.
643 Block->addSuccessor(ThenBlock);
644 Block->addSuccessor(ElseBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000645
646 // Add the condition as the last statement in the new block. This
647 // may create new blocks as the condition may contain control-flow. Any
648 // newly created blocks will be pointed to be "Block".
Ted Kremenek1eaa6712008-01-30 23:02:42 +0000649 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenek73543912007-08-23 21:42:29 +0000650}
Ted Kremenekd11620d2007-09-11 21:29:43 +0000651
Ted Kremenek73543912007-08-23 21:42:29 +0000652
653CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
654 // If we were in the middle of a block we stop processing that block
655 // and reverse its statements.
656 //
657 // NOTE: If a "return" appears in the middle of a block, this means
658 // that the code afterwards is DEAD (unreachable). We still
659 // keep a basic block for that code; a simple "mark-and-sweep"
660 // from the entry block will be able to report such dead
661 // blocks.
662 if (Block) FinishBlock(Block);
663
664 // Create the new block.
665 Block = createBlock(false);
666
667 // The Exit block is the only successor.
668 Block->addSuccessor(&cfg->getExit());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000669
670 // Add the return statement to the block. This may create new blocks
671 // if R contains control-flow (short-circuit operations).
672 return addStmt(R);
Ted Kremenek73543912007-08-23 21:42:29 +0000673}
674
675CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
676 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek82e8a192008-03-15 07:45:02 +0000677 Visit(L->getSubStmt());
678 CFGBlock* LabelBlock = Block;
Ted Kremenek9b0d1b62007-08-30 18:20:57 +0000679
680 if (!LabelBlock) // This can happen when the body is empty, i.e.
681 LabelBlock=createBlock(); // scopes that only contains NullStmts.
682
Ted Kremenek73543912007-08-23 21:42:29 +0000683 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
684 LabelMap[ L ] = LabelBlock;
685
686 // Labels partition blocks, so this is the end of the basic block
Ted Kremenekec055e12007-08-29 23:20:49 +0000687 // we were processing (L is the block's label). Because this is
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000688 // label (and we have already processed the substatement) there is no
689 // extra control-flow to worry about.
Ted Kremenekec055e12007-08-29 23:20:49 +0000690 LabelBlock->setLabel(L);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000691 if (!FinishBlock(LabelBlock))
692 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +0000693
694 // We set Block to NULL to allow lazy creation of a new block
695 // (if necessary);
696 Block = NULL;
697
698 // This block is now the implicit successor of other blocks.
699 Succ = LabelBlock;
700
701 return LabelBlock;
702}
703
704CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
705 // Goto is a control-flow statement. Thus we stop processing the
706 // current block and create a new one.
707 if (Block) FinishBlock(Block);
708 Block = createBlock(false);
709 Block->setTerminator(G);
710
711 // If we already know the mapping to the label block add the
712 // successor now.
713 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
714
715 if (I == LabelMap.end())
716 // We will need to backpatch this block later.
717 BackpatchBlocks.push_back(Block);
718 else
719 Block->addSuccessor(I->second);
720
721 return Block;
722}
723
724CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
725 // "for" is a control-flow statement. Thus we stop processing the
726 // current block.
727
728 CFGBlock* LoopSuccessor = NULL;
729
730 if (Block) {
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000731 if (!FinishBlock(Block))
732 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +0000733 LoopSuccessor = Block;
734 }
735 else LoopSuccessor = Succ;
736
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000737 // Because of short-circuit evaluation, the condition of the loop
738 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
739 // blocks that evaluate the condition.
740 CFGBlock* ExitConditionBlock = createBlock(false);
741 CFGBlock* EntryConditionBlock = ExitConditionBlock;
742
743 // Set the terminator for the "exit" condition block.
744 ExitConditionBlock->setTerminator(F);
745
746 // Now add the actual condition to the condition block. Because the
747 // condition itself may contain control-flow, new blocks may be created.
748 if (Stmt* C = F->getCond()) {
749 Block = ExitConditionBlock;
750 EntryConditionBlock = addStmt(C);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000751 if (Block) {
752 if (!FinishBlock(EntryConditionBlock))
753 return 0;
754 }
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000755 }
Ted Kremenek73543912007-08-23 21:42:29 +0000756
757 // The condition block is the implicit successor for the loop body as
758 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000759 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000760
761 // Now create the loop body.
762 {
763 assert (F->getBody());
764
765 // Save the current values for Block, Succ, and continue and break targets
766 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
767 save_continue(ContinueTargetBlock),
768 save_break(BreakTargetBlock);
Ted Kremenek77f93372008-09-04 21:48:47 +0000769
Ted Kremenek390b9762007-08-30 18:39:40 +0000770 // Create a new block to contain the (bottom) of the loop body.
771 Block = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000772
Ted Kremenek77f93372008-09-04 21:48:47 +0000773 if (Stmt* I = F->getInc()) {
774 // Generate increment code in its own basic block. This is the target
775 // of continue statements.
Ted Kremenekd19e99e2008-11-24 20:50:24 +0000776 Succ = Visit(I);
Ted Kremenek77f93372008-09-04 21:48:47 +0000777 }
778 else {
Ted Kremenekca7cdcf2009-04-28 00:51:56 +0000779 // No increment code. Create a special, empty, block that is used as
780 // the target block for "looping back" to the start of the loop.
781 assert(Succ == EntryConditionBlock);
782 Succ = createBlock();
Ted Kremenek77f93372008-09-04 21:48:47 +0000783 }
784
Ted Kremenekca7cdcf2009-04-28 00:51:56 +0000785 // Finish up the increment (or empty) block if it hasn't been already.
786 if (Block) {
787 assert(Block == Succ);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000788 if (!FinishBlock(Block))
789 return 0;
Ted Kremenekca7cdcf2009-04-28 00:51:56 +0000790 Block = 0;
791 }
792
793 ContinueTargetBlock = Succ;
794
795 // The starting block for the loop increment is the block that should
796 // represent the 'loop target' for looping back to the start of the loop.
797 ContinueTargetBlock->setLoopTarget(F);
798
Ted Kremenek77f93372008-09-04 21:48:47 +0000799 // All breaks should go to the code following the loop.
800 BreakTargetBlock = LoopSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +0000801
802 // Now populate the body block, and in the process create new blocks
803 // as we walk the body of the loop.
804 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenek390b9762007-08-30 18:39:40 +0000805
806 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +0000807 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000808 else if (Block) {
809 if (!FinishBlock(BodyBlock))
810 return 0;
811 }
Ted Kremenek73543912007-08-23 21:42:29 +0000812
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000813 // This new body block is a successor to our "exit" condition block.
814 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000815 }
816
817 // Link up the condition block with the code that follows the loop.
818 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000819 ExitConditionBlock->addSuccessor(LoopSuccessor);
820
Ted Kremenek73543912007-08-23 21:42:29 +0000821 // If the loop contains initialization, create a new block for those
822 // statements. This block can also contain statements that precede
823 // the loop.
824 if (Stmt* I = F->getInit()) {
825 Block = createBlock();
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000826 return addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000827 }
828 else {
829 // There is no loop initialization. We are thus basically a while
830 // loop. NULL out Block to force lazy block construction.
831 Block = NULL;
Ted Kremenek9ff572c2008-02-27 07:20:00 +0000832 Succ = EntryConditionBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000833 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000834 }
835}
836
Ted Kremenek05335162008-11-11 17:10:00 +0000837CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
838 // Objective-C fast enumeration 'for' statements:
839 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
840 //
841 // for ( Type newVariable in collection_expression ) { statements }
842 //
843 // becomes:
844 //
845 // prologue:
846 // 1. collection_expression
847 // T. jump to loop_entry
848 // loop_entry:
Ted Kremenek65514842008-11-14 01:57:41 +0000849 // 1. side-effects of element expression
Ted Kremenek05335162008-11-11 17:10:00 +0000850 // 1. ObjCForCollectionStmt [performs binding to newVariable]
851 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
852 // TB:
853 // statements
854 // T. jump to loop_entry
855 // FB:
856 // what comes after
857 //
858 // and
859 //
860 // Type existingItem;
861 // for ( existingItem in expression ) { statements }
862 //
863 // becomes:
864 //
865 // the same with newVariable replaced with existingItem; the binding
866 // works the same except that for one ObjCForCollectionStmt::getElement()
867 // returns a DeclStmt and the other returns a DeclRefExpr.
868 //
869
870 CFGBlock* LoopSuccessor = 0;
871
872 if (Block) {
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000873 if (!FinishBlock(Block))
874 return 0;
Ted Kremenek05335162008-11-11 17:10:00 +0000875 LoopSuccessor = Block;
876 Block = 0;
877 }
878 else LoopSuccessor = Succ;
879
Ted Kremenek65514842008-11-14 01:57:41 +0000880 // Build the condition blocks.
881 CFGBlock* ExitConditionBlock = createBlock(false);
882 CFGBlock* EntryConditionBlock = ExitConditionBlock;
883
884 // Set the terminator for the "exit" condition block.
885 ExitConditionBlock->setTerminator(S);
886
887 // The last statement in the block should be the ObjCForCollectionStmt,
888 // which performs the actual binding to 'element' and determines if there
889 // are any more items in the collection.
890 ExitConditionBlock->appendStmt(S);
891 Block = ExitConditionBlock;
892
893 // Walk the 'element' expression to see if there are any side-effects. We
894 // generate new blocks as necesary. We DON'T add the statement by default
895 // to the CFG unless it contains control-flow.
896 EntryConditionBlock = WalkAST(S->getElement(), false);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000897 if (Block) {
898 if (!FinishBlock(EntryConditionBlock))
899 return 0;
900 Block = 0;
901 }
Ted Kremenek65514842008-11-14 01:57:41 +0000902
903 // The condition block is the implicit successor for the loop body as
904 // well as any code above the loop.
905 Succ = EntryConditionBlock;
Ted Kremenek05335162008-11-11 17:10:00 +0000906
907 // Now create the true branch.
Ted Kremenek65514842008-11-14 01:57:41 +0000908 {
909 // Save the current values for Succ, continue and break targets.
910 SaveAndRestore<CFGBlock*> save_Succ(Succ),
911 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
912
913 BreakTargetBlock = LoopSuccessor;
914 ContinueTargetBlock = EntryConditionBlock;
915
916 CFGBlock* BodyBlock = Visit(S->getBody());
917
918 if (!BodyBlock)
919 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000920 else if (Block) {
921 if (!FinishBlock(BodyBlock))
922 return 0;
923 }
Ted Kremenek65514842008-11-14 01:57:41 +0000924
925 // This new body block is a successor to our "exit" condition block.
926 ExitConditionBlock->addSuccessor(BodyBlock);
927 }
Ted Kremenekf5383072008-11-13 06:36:45 +0000928
Ted Kremenek65514842008-11-14 01:57:41 +0000929 // Link up the condition block with the code that follows the loop.
930 // (the false branch).
931 ExitConditionBlock->addSuccessor(LoopSuccessor);
932
Ted Kremenek05335162008-11-11 17:10:00 +0000933 // Now create a prologue block to contain the collection expression.
Ted Kremenek65514842008-11-14 01:57:41 +0000934 Block = createBlock();
Ted Kremenek05335162008-11-11 17:10:00 +0000935 return addStmt(S->getCollection());
936}
Ted Kremenek72037962009-03-30 22:29:21 +0000937
938CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek417d4692009-04-07 04:26:02 +0000939 return NYS();
Ted Kremenek72037962009-03-30 22:29:21 +0000940}
Ted Kremenek05335162008-11-11 17:10:00 +0000941
Ted Kremenek73543912007-08-23 21:42:29 +0000942CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
943 // "while" is a control-flow statement. Thus we stop processing the
944 // current block.
945
946 CFGBlock* LoopSuccessor = NULL;
947
948 if (Block) {
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000949 if (!FinishBlock(Block))
950 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +0000951 LoopSuccessor = Block;
952 }
953 else LoopSuccessor = Succ;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000954
955 // Because of short-circuit evaluation, the condition of the loop
956 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
957 // blocks that evaluate the condition.
958 CFGBlock* ExitConditionBlock = createBlock(false);
959 CFGBlock* EntryConditionBlock = ExitConditionBlock;
960
961 // Set the terminator for the "exit" condition block.
962 ExitConditionBlock->setTerminator(W);
963
964 // Now add the actual condition to the condition block. Because the
965 // condition itself may contain control-flow, new blocks may be created.
966 // Thus we update "Succ" after adding the condition.
967 if (Stmt* C = W->getCond()) {
968 Block = ExitConditionBlock;
969 EntryConditionBlock = addStmt(C);
Ted Kremenek64f918f2009-04-28 03:09:44 +0000970 assert(Block == EntryConditionBlock);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000971 if (Block) {
972 if (!FinishBlock(EntryConditionBlock))
973 return 0;
974 }
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000975 }
Ted Kremenek73543912007-08-23 21:42:29 +0000976
977 // The condition block is the implicit successor for the loop body as
978 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000979 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000980
981 // Process the loop body.
982 {
Ted Kremenek64f918f2009-04-28 03:09:44 +0000983 assert(W->getBody());
Ted Kremenek73543912007-08-23 21:42:29 +0000984
985 // Save the current values for Block, Succ, and continue and break targets
986 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
987 save_continue(ContinueTargetBlock),
988 save_break(BreakTargetBlock);
Ted Kremenek64f918f2009-04-28 03:09:44 +0000989
990 // Create an empty block to represent the transition block for looping
991 // back to the head of the loop.
992 Block = 0;
993 assert(Succ == EntryConditionBlock);
994 Succ = createBlock();
995 Succ->setLoopTarget(W);
996 ContinueTargetBlock = Succ;
Ted Kremenek73543912007-08-23 21:42:29 +0000997
998 // All breaks should go to the code following the loop.
999 BreakTargetBlock = LoopSuccessor;
1000
1001 // NULL out Block to force lazy instantiation of blocks for the body.
1002 Block = NULL;
1003
1004 // Create the body. The returned block is the entry to the loop body.
1005 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenek390b9762007-08-30 18:39:40 +00001006
1007 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +00001008 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001009 else if (Block) {
1010 if (!FinishBlock(BodyBlock))
1011 return 0;
1012 }
Ted Kremenek73543912007-08-23 21:42:29 +00001013
1014 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001015 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +00001016 }
1017
1018 // Link up the condition block with the code that follows the loop.
1019 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001020 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +00001021
1022 // There can be no more statements in the condition block
1023 // since we loop back to this block. NULL out Block to force
1024 // lazy creation of another block.
1025 Block = NULL;
1026
1027 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek9ff572c2008-02-27 07:20:00 +00001028 Succ = EntryConditionBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001029 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001030}
Ted Kremenekc74ac3e2008-12-09 20:20:09 +00001031
1032CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1033 // FIXME: This isn't complete. We basically treat @throw like a return
1034 // statement.
1035
1036 // If we were in the middle of a block we stop processing that block
1037 // and reverse its statements.
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001038 if (Block) {
1039 if (!FinishBlock(Block))
1040 return 0;
1041 }
Ted Kremenekc74ac3e2008-12-09 20:20:09 +00001042
1043 // Create the new block.
1044 Block = createBlock(false);
1045
1046 // The Exit block is the only successor.
1047 Block->addSuccessor(&cfg->getExit());
1048
1049 // Add the statement to the block. This may create new blocks
1050 // if S contains control-flow (short-circuit operations).
1051 return addStmt(S);
1052}
Ted Kremenek73543912007-08-23 21:42:29 +00001053
1054CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
1055 // "do...while" is a control-flow statement. Thus we stop processing the
1056 // current block.
1057
1058 CFGBlock* LoopSuccessor = NULL;
1059
1060 if (Block) {
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001061 if (!FinishBlock(Block))
1062 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +00001063 LoopSuccessor = Block;
1064 }
1065 else LoopSuccessor = Succ;
1066
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001067 // Because of short-circuit evaluation, the condition of the loop
1068 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
1069 // blocks that evaluate the condition.
1070 CFGBlock* ExitConditionBlock = createBlock(false);
1071 CFGBlock* EntryConditionBlock = ExitConditionBlock;
1072
1073 // Set the terminator for the "exit" condition block.
1074 ExitConditionBlock->setTerminator(D);
Ted Kremenek73543912007-08-23 21:42:29 +00001075
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001076 // Now add the actual condition to the condition block. Because the
1077 // condition itself may contain control-flow, new blocks may be created.
1078 if (Stmt* C = D->getCond()) {
1079 Block = ExitConditionBlock;
1080 EntryConditionBlock = addStmt(C);
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001081 if (Block) {
1082 if (!FinishBlock(EntryConditionBlock))
1083 return 0;
1084 }
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001085 }
Ted Kremenek73543912007-08-23 21:42:29 +00001086
Ted Kremenek9ff572c2008-02-27 07:20:00 +00001087 // The condition block is the implicit successor for the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001088 Succ = EntryConditionBlock;
1089
Ted Kremenek73543912007-08-23 21:42:29 +00001090 // Process the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001091 CFGBlock* BodyBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +00001092 {
1093 assert (D->getBody());
1094
1095 // Save the current values for Block, Succ, and continue and break targets
1096 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1097 save_continue(ContinueTargetBlock),
1098 save_break(BreakTargetBlock);
1099
1100 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001101 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001102
1103 // All breaks should go to the code following the loop.
1104 BreakTargetBlock = LoopSuccessor;
1105
1106 // NULL out Block to force lazy instantiation of blocks for the body.
1107 Block = NULL;
1108
1109 // Create the body. The returned block is the entry to the loop body.
1110 BodyBlock = Visit(D->getBody());
Ted Kremenek73543912007-08-23 21:42:29 +00001111
Ted Kremenek390b9762007-08-30 18:39:40 +00001112 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +00001113 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001114 else if (Block) {
1115 if (!FinishBlock(BodyBlock))
1116 return 0;
1117 }
Ted Kremenek390b9762007-08-30 18:39:40 +00001118
Ted Kremenek1b697ca2009-04-28 04:22:00 +00001119 // Add an intermediate block between the BodyBlock and the
1120 // ExitConditionBlock to represent the "loop back" transition.
1121 // Create an empty block to represent the transition block for looping
1122 // back to the head of the loop.
1123 // FIXME: Can we do this more efficiently without adding another block?
1124 Block = NULL;
1125 Succ = BodyBlock;
1126 CFGBlock *LoopBackBlock = createBlock();
1127 LoopBackBlock->setLoopTarget(D);
1128
Ted Kremenek73543912007-08-23 21:42:29 +00001129 // Add the loop body entry as a successor to the condition.
Ted Kremenek1b697ca2009-04-28 04:22:00 +00001130 ExitConditionBlock->addSuccessor(LoopBackBlock);
Ted Kremenek73543912007-08-23 21:42:29 +00001131 }
1132
1133 // Link up the condition block with the code that follows the loop.
1134 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001135 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +00001136
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001137 // There can be no more statements in the body block(s)
1138 // since we loop back to the body. NULL out Block to force
Ted Kremenek73543912007-08-23 21:42:29 +00001139 // lazy creation of another block.
1140 Block = NULL;
1141
1142 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek9ff572c2008-02-27 07:20:00 +00001143 Succ = BodyBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001144 return BodyBlock;
1145}
1146
1147CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1148 // "continue" is a control-flow statement. Thus we stop processing the
1149 // current block.
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001150 if (Block) {
1151 if (!FinishBlock(Block))
1152 return 0;
1153 }
Ted Kremenek73543912007-08-23 21:42:29 +00001154
1155 // Now create a new block that ends with the continue statement.
1156 Block = createBlock(false);
1157 Block->setTerminator(C);
1158
1159 // If there is no target for the continue, then we are looking at an
Ted Kremenek819b7c02009-04-07 18:53:24 +00001160 // incomplete AST. This means the CFG cannot be constructed.
1161 if (ContinueTargetBlock)
1162 Block->addSuccessor(ContinueTargetBlock);
1163 else
1164 badCFG = true;
Ted Kremenek73543912007-08-23 21:42:29 +00001165
1166 return Block;
1167}
1168
1169CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
1170 // "break" is a control-flow statement. Thus we stop processing the
1171 // current block.
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001172 if (Block) {
1173 if (!FinishBlock(Block))
1174 return 0;
1175 }
Ted Kremenek73543912007-08-23 21:42:29 +00001176
1177 // Now create a new block that ends with the continue statement.
1178 Block = createBlock(false);
1179 Block->setTerminator(B);
1180
1181 // If there is no target for the break, then we are looking at an
Ted Kremenek819b7c02009-04-07 18:53:24 +00001182 // incomplete AST. This means that the CFG cannot be constructed.
1183 if (BreakTargetBlock)
1184 Block->addSuccessor(BreakTargetBlock);
1185 else
1186 badCFG = true;
1187
Ted Kremenek73543912007-08-23 21:42:29 +00001188
1189 return Block;
1190}
1191
Ted Kremenek79f0a632008-04-16 21:10:48 +00001192CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek73543912007-08-23 21:42:29 +00001193 // "switch" is a control-flow statement. Thus we stop processing the
1194 // current block.
1195 CFGBlock* SwitchSuccessor = NULL;
1196
1197 if (Block) {
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001198 if (!FinishBlock(Block))
1199 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +00001200 SwitchSuccessor = Block;
1201 }
1202 else SwitchSuccessor = Succ;
1203
1204 // Save the current "switch" context.
1205 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek97bc3422008-02-13 22:05:39 +00001206 save_break(BreakTargetBlock),
1207 save_default(DefaultCaseBlock);
1208
1209 // Set the "default" case to be the block after the switch statement.
1210 // If the switch statement contains a "default:", this value will
1211 // be overwritten with the block for that code.
1212 DefaultCaseBlock = SwitchSuccessor;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001213
Ted Kremenek73543912007-08-23 21:42:29 +00001214 // Create a new block that will contain the switch statement.
1215 SwitchTerminatedBlock = createBlock(false);
1216
Ted Kremenek73543912007-08-23 21:42:29 +00001217 // Now process the switch body. The code after the switch is the implicit
1218 // successor.
1219 Succ = SwitchSuccessor;
1220 BreakTargetBlock = SwitchSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +00001221
1222 // When visiting the body, the case statements should automatically get
1223 // linked up to the switch. We also don't keep a pointer to the body,
1224 // since all control-flow from the switch goes to case/default statements.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001225 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001226 Block = NULL;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001227 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001228 if (Block) {
1229 if (!FinishBlock(BodyBlock))
1230 return 0;
1231 }
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001232
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001233 // If we have no "default:" case, the default transition is to the
1234 // code following the switch body.
Ted Kremenek97bc3422008-02-13 22:05:39 +00001235 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001236
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001237 // Add the terminator and condition in the switch block.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001238 SwitchTerminatedBlock->setTerminator(Terminator);
1239 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek73543912007-08-23 21:42:29 +00001240 Block = SwitchTerminatedBlock;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001241
Ted Kremenek79f0a632008-04-16 21:10:48 +00001242 return addStmt(Terminator->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +00001243}
1244
Ted Kremenek79f0a632008-04-16 21:10:48 +00001245CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Ted Kremenek97bc3422008-02-13 22:05:39 +00001246 // CaseStmts are essentially labels, so they are the
Ted Kremenek73543912007-08-23 21:42:29 +00001247 // first statement in a block.
Ted Kremenek44659d82007-08-30 18:48:11 +00001248
Ted Kremenek79f0a632008-04-16 21:10:48 +00001249 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek44659d82007-08-30 18:48:11 +00001250 CFGBlock* CaseBlock = Block;
1251 if (!CaseBlock) CaseBlock = createBlock();
1252
Ted Kremenek97bc3422008-02-13 22:05:39 +00001253 // Cases statements partition blocks, so this is the top of
1254 // the basic block we were processing (the "case XXX:" is the label).
Ted Kremenek79f0a632008-04-16 21:10:48 +00001255 CaseBlock->setLabel(Terminator);
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001256 if (!FinishBlock(CaseBlock))
1257 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +00001258
1259 // Add this block to the list of successors for the block with the
1260 // switch statement.
Ted Kremenek97bc3422008-02-13 22:05:39 +00001261 assert (SwitchTerminatedBlock);
1262 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +00001263
1264 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1265 Block = NULL;
1266
1267 // This block is now the implicit successor of other blocks.
1268 Succ = CaseBlock;
1269
Ted Kremenek82e8a192008-03-15 07:45:02 +00001270 return CaseBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001271}
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001272
Ted Kremenek79f0a632008-04-16 21:10:48 +00001273CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1274 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek97bc3422008-02-13 22:05:39 +00001275 DefaultCaseBlock = Block;
1276 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1277
1278 // Default statements partition blocks, so this is the top of
1279 // the basic block we were processing (the "default:" is the label).
Ted Kremenek79f0a632008-04-16 21:10:48 +00001280 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001281 if (!FinishBlock(DefaultCaseBlock))
1282 return 0;
Ted Kremenek97bc3422008-02-13 22:05:39 +00001283
1284 // Unlike case statements, we don't add the default block to the
1285 // successors for the switch statement immediately. This is done
1286 // when we finish processing the switch statement. This allows for
1287 // the default case (including a fall-through to the code after the
1288 // switch statement) to always be the last successor of a switch-terminated
1289 // block.
1290
1291 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1292 Block = NULL;
1293
1294 // This block is now the implicit successor of other blocks.
1295 Succ = DefaultCaseBlock;
1296
1297 return DefaultCaseBlock;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001298}
Ted Kremenek73543912007-08-23 21:42:29 +00001299
Ted Kremenek0edd3a92007-08-28 19:26:49 +00001300CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1301 // Lazily create the indirect-goto dispatch block if there isn't one
1302 // already.
1303 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
1304
1305 if (!IBlock) {
1306 IBlock = createBlock(false);
1307 cfg->setIndirectGotoBlock(IBlock);
1308 }
1309
1310 // IndirectGoto is a control-flow statement. Thus we stop processing the
1311 // current block and create a new one.
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001312 if (Block) {
1313 if (!FinishBlock(Block))
1314 return 0;
1315 }
Ted Kremenek0edd3a92007-08-28 19:26:49 +00001316 Block = createBlock(false);
1317 Block->setTerminator(I);
1318 Block->addSuccessor(IBlock);
1319 return addStmt(I->getTarget());
1320}
1321
Ted Kremenek73543912007-08-23 21:42:29 +00001322
Ted Kremenekd6e50602007-08-23 21:26:19 +00001323} // end anonymous namespace
Ted Kremenek4db5b452007-08-23 16:51:22 +00001324
1325/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
1326/// block has no successors or predecessors. If this is the first block
1327/// created in the CFG, it is automatically set to be the Entry and Exit
1328/// of the CFG.
Ted Kremenek14594572007-09-05 20:02:05 +00001329CFGBlock* CFG::createBlock() {
Ted Kremenek4db5b452007-08-23 16:51:22 +00001330 bool first_block = begin() == end();
1331
1332 // Create the block.
Ted Kremenek14594572007-09-05 20:02:05 +00001333 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek4db5b452007-08-23 16:51:22 +00001334
1335 // If this is the first block, set it as the Entry and Exit.
1336 if (first_block) Entry = Exit = &front();
1337
1338 // Return the block.
1339 return &front();
Ted Kremenek97f75312007-08-21 21:42:03 +00001340}
1341
Ted Kremenek4db5b452007-08-23 16:51:22 +00001342/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1343/// CFG is returned to the caller.
1344CFG* CFG::buildCFG(Stmt* Statement) {
1345 CFGBuilder Builder;
1346 return Builder.buildCFG(Statement);
1347}
1348
1349/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek97f75312007-08-21 21:42:03 +00001350void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1351
Ted Kremenek3a819822007-10-01 19:33:33 +00001352//===----------------------------------------------------------------------===//
1353// CFG: Queries for BlkExprs.
1354//===----------------------------------------------------------------------===//
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001355
Ted Kremenek3a819822007-10-01 19:33:33 +00001356namespace {
Ted Kremenekab6c5902008-01-17 20:48:37 +00001357 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek3a819822007-10-01 19:33:33 +00001358}
1359
Ted Kremenek79f0a632008-04-16 21:10:48 +00001360static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1361 if (!Terminator)
Ted Kremenekc6fda602008-01-26 00:03:27 +00001362 return;
1363
Ted Kremenek79f0a632008-04-16 21:10:48 +00001364 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001365 if (!*I) continue;
1366
1367 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1368 if (B->isAssignmentOp()) Set.insert(B);
1369
1370 FindSubExprAssignments(*I, Set);
1371 }
1372}
1373
Ted Kremenek3a819822007-10-01 19:33:33 +00001374static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1375 BlkExprMapTy* M = new BlkExprMapTy();
1376
Ted Kremenekc6fda602008-01-26 00:03:27 +00001377 // Look for assignments that are used as subexpressions. These are the
Ted Kremenek79f0a632008-04-16 21:10:48 +00001378 // only assignments that we want to *possibly* register as a block-level
1379 // expression. Basically, if an assignment occurs both in a subexpression
1380 // and at the block-level, it is a block-level expression.
Ted Kremenekc6fda602008-01-26 00:03:27 +00001381 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
1382
Ted Kremenek3a819822007-10-01 19:33:33 +00001383 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1384 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc6fda602008-01-26 00:03:27 +00001385 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenekab6c5902008-01-17 20:48:37 +00001386
Ted Kremenek79f0a632008-04-16 21:10:48 +00001387 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
1388
1389 // Iterate over the statements again on identify the Expr* and Stmt* at
1390 // the block-level that are block-level expressions.
1391
Ted Kremenekc6fda602008-01-26 00:03:27 +00001392 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek79f0a632008-04-16 21:10:48 +00001393 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001394
Ted Kremenek79f0a632008-04-16 21:10:48 +00001395 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001396 // Assignment expressions that are not nested within another
1397 // expression are really "statements" whose value is never
1398 // used by another expression.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001399 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenekc6fda602008-01-26 00:03:27 +00001400 continue;
1401 }
Ted Kremenek79f0a632008-04-16 21:10:48 +00001402 else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001403 // Special handling for statement expressions. The last statement
1404 // in the statement expression is also a block-level expr.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001405 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenekab6c5902008-01-17 20:48:37 +00001406 if (!C->body_empty()) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001407 unsigned x = M->size();
Ted Kremenekab6c5902008-01-17 20:48:37 +00001408 (*M)[C->body_back()] = x;
1409 }
1410 }
Ted Kremenek5b4eb172008-01-25 23:22:27 +00001411
Ted Kremenekc6fda602008-01-26 00:03:27 +00001412 unsigned x = M->size();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001413 (*M)[Exp] = x;
Ted Kremenekc6fda602008-01-26 00:03:27 +00001414 }
1415
Ted Kremenek79f0a632008-04-16 21:10:48 +00001416 // Look at terminators. The condition is a block-level expression.
1417
Ted Kremenek16516e22008-11-12 21:11:49 +00001418 Stmt* S = I->getTerminatorCondition();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001419
Ted Kremenek16516e22008-11-12 21:11:49 +00001420 if (S && M->find(S) == M->end()) {
Ted Kremenek79f0a632008-04-16 21:10:48 +00001421 unsigned x = M->size();
Ted Kremenek16516e22008-11-12 21:11:49 +00001422 (*M)[S] = x;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001423 }
1424 }
1425
Ted Kremenek3a819822007-10-01 19:33:33 +00001426 return M;
1427}
1428
Ted Kremenekab6c5902008-01-17 20:48:37 +00001429CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1430 assert(S != NULL);
Ted Kremenek3a819822007-10-01 19:33:33 +00001431 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
1432
1433 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenekab6c5902008-01-17 20:48:37 +00001434 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3a819822007-10-01 19:33:33 +00001435
1436 if (I == M->end()) return CFG::BlkExprNumTy();
1437 else return CFG::BlkExprNumTy(I->second);
1438}
1439
1440unsigned CFG::getNumBlkExprs() {
1441 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1442 return M->size();
1443 else {
1444 // We assume callers interested in the number of BlkExprs will want
1445 // the map constructed if it doesn't already exist.
1446 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1447 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1448 }
1449}
1450
Ted Kremenekd058a9c2008-04-28 18:00:46 +00001451//===----------------------------------------------------------------------===//
Ted Kremenekd058a9c2008-04-28 18:00:46 +00001452// Cleanup: CFG dstor.
1453//===----------------------------------------------------------------------===//
1454
Ted Kremenek3a819822007-10-01 19:33:33 +00001455CFG::~CFG() {
1456 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1457}
1458
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001459//===----------------------------------------------------------------------===//
1460// CFG pretty printing
1461//===----------------------------------------------------------------------===//
1462
Ted Kremenekd8313202007-08-22 18:22:34 +00001463namespace {
1464
Ted Kremenek98cee3a2008-01-08 18:15:10 +00001465class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Ted Kremenek86afc042007-08-31 22:26:13 +00001466
Ted Kremenek08176a52007-08-31 21:30:12 +00001467 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1468 StmtMapTy StmtMap;
1469 signed CurrentBlock;
1470 unsigned CurrentStmt;
Ted Kremenek86afc042007-08-31 22:26:13 +00001471
Ted Kremenek73543912007-08-23 21:42:29 +00001472public:
Ted Kremenek86afc042007-08-31 22:26:13 +00001473
Ted Kremenek08176a52007-08-31 21:30:12 +00001474 StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) {
1475 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1476 unsigned j = 1;
1477 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1478 BI != BEnd; ++BI, ++j )
1479 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1480 }
1481 }
1482
1483 virtual ~StmtPrinterHelper() {}
1484
1485 void setBlockID(signed i) { CurrentBlock = i; }
1486 void setStmtID(unsigned i) { CurrentStmt = i; }
1487
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001488 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001489
Ted Kremenek79f0a632008-04-16 21:10:48 +00001490 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek08176a52007-08-31 21:30:12 +00001491
1492 if (I == StmtMap.end())
1493 return false;
1494
1495 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
1496 && I->second.second == CurrentStmt)
1497 return false;
1498
Ted Kremenek86afc042007-08-31 22:26:13 +00001499 OS << "[B" << I->second.first << "." << I->second.second << "]";
1500 return true;
Ted Kremenek08176a52007-08-31 21:30:12 +00001501 }
1502};
1503
Ted Kremenek98cee3a2008-01-08 18:15:10 +00001504class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1505 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
1506
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001507 llvm::raw_ostream& OS;
Ted Kremenek08176a52007-08-31 21:30:12 +00001508 StmtPrinterHelper* Helper;
1509public:
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001510 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper)
Ted Kremenek08176a52007-08-31 21:30:12 +00001511 : OS(os), Helper(helper) {}
Ted Kremenek73543912007-08-23 21:42:29 +00001512
1513 void VisitIfStmt(IfStmt* I) {
1514 OS << "if ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001515 I->getCond()->printPretty(OS,Helper);
Ted Kremenek73543912007-08-23 21:42:29 +00001516 }
1517
1518 // Default case.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001519 void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS); }
Ted Kremenek73543912007-08-23 21:42:29 +00001520
1521 void VisitForStmt(ForStmt* F) {
1522 OS << "for (" ;
Ted Kremenek23a1d662007-08-30 21:28:02 +00001523 if (F->getInit()) OS << "...";
1524 OS << "; ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001525 if (Stmt* C = F->getCond()) C->printPretty(OS,Helper);
Ted Kremenek23a1d662007-08-30 21:28:02 +00001526 OS << "; ";
1527 if (F->getInc()) OS << "...";
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001528 OS << ")";
Ted Kremenek73543912007-08-23 21:42:29 +00001529 }
1530
1531 void VisitWhileStmt(WhileStmt* W) {
1532 OS << "while " ;
Ted Kremenek08176a52007-08-31 21:30:12 +00001533 if (Stmt* C = W->getCond()) C->printPretty(OS,Helper);
Ted Kremenek73543912007-08-23 21:42:29 +00001534 }
1535
1536 void VisitDoStmt(DoStmt* D) {
1537 OS << "do ... while ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001538 if (Stmt* C = D->getCond()) C->printPretty(OS,Helper);
Ted Kremenek65cfa562007-08-27 21:27:44 +00001539 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001540
Ted Kremenek79f0a632008-04-16 21:10:48 +00001541 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek65cfa562007-08-27 21:27:44 +00001542 OS << "switch ";
Ted Kremenek79f0a632008-04-16 21:10:48 +00001543 Terminator->getCond()->printPretty(OS,Helper);
Ted Kremenek65cfa562007-08-27 21:27:44 +00001544 }
1545
Ted Kremenek621e1592007-08-31 21:49:40 +00001546 void VisitConditionalOperator(ConditionalOperator* C) {
1547 C->getCond()->printPretty(OS,Helper);
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001548 OS << " ? ... : ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001549 }
1550
Ted Kremenek2025cc92007-08-31 22:29:13 +00001551 void VisitChooseExpr(ChooseExpr* C) {
1552 OS << "__builtin_choose_expr( ";
1553 C->getCond()->printPretty(OS,Helper);
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001554 OS << " )";
Ted Kremenek2025cc92007-08-31 22:29:13 +00001555 }
1556
Ted Kremenek86afc042007-08-31 22:26:13 +00001557 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1558 OS << "goto *";
1559 I->getTarget()->printPretty(OS,Helper);
Ted Kremenek86afc042007-08-31 22:26:13 +00001560 }
1561
Ted Kremenek621e1592007-08-31 21:49:40 +00001562 void VisitBinaryOperator(BinaryOperator* B) {
1563 if (!B->isLogicalOp()) {
1564 VisitExpr(B);
1565 return;
1566 }
1567
1568 B->getLHS()->printPretty(OS,Helper);
1569
1570 switch (B->getOpcode()) {
1571 case BinaryOperator::LOr:
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001572 OS << " || ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001573 return;
1574 case BinaryOperator::LAnd:
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001575 OS << " && ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001576 return;
1577 default:
1578 assert(false && "Invalid logical operator.");
1579 }
1580 }
1581
Ted Kremenekcfaae762007-08-27 21:54:41 +00001582 void VisitExpr(Expr* E) {
Ted Kremenek08176a52007-08-31 21:30:12 +00001583 E->printPretty(OS,Helper);
Ted Kremenekcfaae762007-08-27 21:54:41 +00001584 }
Ted Kremenek73543912007-08-23 21:42:29 +00001585};
Ted Kremenek08176a52007-08-31 21:30:12 +00001586
1587
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001588void print_stmt(llvm::raw_ostream&OS, StmtPrinterHelper* Helper, Stmt* Terminator) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001589 if (Helper) {
1590 // special printing for statement-expressions.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001591 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001592 CompoundStmt* Sub = SE->getSubStmt();
1593
1594 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek16e3b9a2007-08-31 22:47:06 +00001595 OS << "({ ... ; ";
Ted Kremenek256a2592007-10-29 20:41:04 +00001596 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek16e3b9a2007-08-31 22:47:06 +00001597 OS << " })\n";
Ted Kremenek86afc042007-08-31 22:26:13 +00001598 return;
1599 }
1600 }
1601
1602 // special printing for comma expressions.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001603 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001604 if (B->getOpcode() == BinaryOperator::Comma) {
1605 OS << "... , ";
1606 Helper->handledStmt(B->getRHS(),OS);
1607 OS << '\n';
1608 return;
1609 }
1610 }
1611 }
1612
Ted Kremenek79f0a632008-04-16 21:10:48 +00001613 Terminator->printPretty(OS, Helper);
Ted Kremenek86afc042007-08-31 22:26:13 +00001614
1615 // Expressions need a newline.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001616 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek86afc042007-08-31 22:26:13 +00001617}
1618
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001619void print_block(llvm::raw_ostream& OS, const CFG* cfg, const CFGBlock& B,
Ted Kremenek08176a52007-08-31 21:30:12 +00001620 StmtPrinterHelper* Helper, bool print_edges) {
1621
1622 if (Helper) Helper->setBlockID(B.getBlockID());
1623
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001624 // Print the header.
Ted Kremenek08176a52007-08-31 21:30:12 +00001625 OS << "\n [ B" << B.getBlockID();
1626
1627 if (&B == &cfg->getEntry())
1628 OS << " (ENTRY) ]\n";
1629 else if (&B == &cfg->getExit())
1630 OS << " (EXIT) ]\n";
1631 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001632 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek08176a52007-08-31 21:30:12 +00001633 else
1634 OS << " ]\n";
1635
Ted Kremenekec055e12007-08-29 23:20:49 +00001636 // Print the label of this block.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001637 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek08176a52007-08-31 21:30:12 +00001638
1639 if (print_edges)
1640 OS << " ";
1641
Ted Kremenek79f0a632008-04-16 21:10:48 +00001642 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenekec055e12007-08-29 23:20:49 +00001643 OS << L->getName();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001644 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenekec055e12007-08-29 23:20:49 +00001645 OS << "case ";
1646 C->getLHS()->printPretty(OS);
1647 if (C->getRHS()) {
1648 OS << " ... ";
1649 C->getRHS()->printPretty(OS);
1650 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001651 }
Ted Kremenek79f0a632008-04-16 21:10:48 +00001652 else if (isa<DefaultStmt>(Terminator))
Ted Kremenekec055e12007-08-29 23:20:49 +00001653 OS << "default";
Ted Kremenek08176a52007-08-31 21:30:12 +00001654 else
1655 assert(false && "Invalid label statement in CFGBlock.");
1656
Ted Kremenekec055e12007-08-29 23:20:49 +00001657 OS << ":\n";
1658 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001659
Ted Kremenek97f75312007-08-21 21:42:03 +00001660 // Iterate through the statements in the block and print them.
Ted Kremenek97f75312007-08-21 21:42:03 +00001661 unsigned j = 1;
Ted Kremenek08176a52007-08-31 21:30:12 +00001662
1663 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1664 I != E ; ++I, ++j ) {
1665
Ted Kremenekec055e12007-08-29 23:20:49 +00001666 // Print the statement # in the basic block and the statement itself.
Ted Kremenek08176a52007-08-31 21:30:12 +00001667 if (print_edges)
1668 OS << " ";
1669
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001670 OS << llvm::format("%3d", j) << ": ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001671
1672 if (Helper)
1673 Helper->setStmtID(j);
Ted Kremenek86afc042007-08-31 22:26:13 +00001674
1675 print_stmt(OS,Helper,*I);
Ted Kremenek97f75312007-08-21 21:42:03 +00001676 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001677
Ted Kremenekec055e12007-08-29 23:20:49 +00001678 // Print the terminator of this block.
Ted Kremenek08176a52007-08-31 21:30:12 +00001679 if (B.getTerminator()) {
1680 if (print_edges)
1681 OS << " ";
1682
Ted Kremenekec055e12007-08-29 23:20:49 +00001683 OS << " T: ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001684
1685 if (Helper) Helper->setBlockID(-1);
1686
1687 CFGBlockTerminatorPrint TPrinter(OS,Helper);
1688 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001689 OS << '\n';
Ted Kremenek97f75312007-08-21 21:42:03 +00001690 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001691
Ted Kremenekec055e12007-08-29 23:20:49 +00001692 if (print_edges) {
1693 // Print the predecessors of this block.
Ted Kremenek08176a52007-08-31 21:30:12 +00001694 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenekec055e12007-08-29 23:20:49 +00001695 unsigned i = 0;
Ted Kremenekec055e12007-08-29 23:20:49 +00001696
Ted Kremenek08176a52007-08-31 21:30:12 +00001697 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1698 I != E; ++I, ++i) {
1699
1700 if (i == 8 || (i-8) == 0)
1701 OS << "\n ";
1702
Ted Kremenekec055e12007-08-29 23:20:49 +00001703 OS << " B" << (*I)->getBlockID();
1704 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001705
1706 OS << '\n';
1707
1708 // Print the successors of this block.
1709 OS << " Successors (" << B.succ_size() << "):";
1710 i = 0;
1711
1712 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1713 I != E; ++I, ++i) {
1714
1715 if (i == 8 || (i-8) % 10 == 0)
1716 OS << "\n ";
1717
1718 OS << " B" << (*I)->getBlockID();
1719 }
1720
Ted Kremenekec055e12007-08-29 23:20:49 +00001721 OS << '\n';
Ted Kremenek97f75312007-08-21 21:42:03 +00001722 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001723}
1724
1725} // end anonymous namespace
1726
1727/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001728void CFG::dump() const { print(llvm::errs()); }
Ted Kremenek08176a52007-08-31 21:30:12 +00001729
1730/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001731void CFG::print(llvm::raw_ostream& OS) const {
Ted Kremenek08176a52007-08-31 21:30:12 +00001732
1733 StmtPrinterHelper Helper(this);
1734
1735 // Print the entry block.
1736 print_block(OS, this, getEntry(), &Helper, true);
1737
1738 // Iterate through the CFGBlocks and print them one by one.
1739 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1740 // Skip the entry block, because we already printed it.
1741 if (&(*I) == &getEntry() || &(*I) == &getExit())
1742 continue;
1743
1744 print_block(OS, this, *I, &Helper, true);
1745 }
1746
1747 // Print the exit block.
1748 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd19e99e2008-11-24 20:50:24 +00001749 OS.flush();
Ted Kremenek08176a52007-08-31 21:30:12 +00001750}
1751
1752/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001753void CFGBlock::dump(const CFG* cfg) const { print(llvm::errs(), cfg); }
Ted Kremenek08176a52007-08-31 21:30:12 +00001754
1755/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1756/// Generally this will only be called from CFG::print.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001757void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg) const {
Ted Kremenek08176a52007-08-31 21:30:12 +00001758 StmtPrinterHelper Helper(cfg);
1759 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek4db5b452007-08-23 16:51:22 +00001760}
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001761
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001762/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001763void CFGBlock::printTerminator(llvm::raw_ostream& OS) const {
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001764 CFGBlockTerminatorPrint TPrinter(OS,NULL);
1765 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1766}
1767
Ted Kremenek16516e22008-11-12 21:11:49 +00001768Stmt* CFGBlock::getTerminatorCondition() {
Ted Kremenek79f0a632008-04-16 21:10:48 +00001769
1770 if (!Terminator)
1771 return NULL;
1772
1773 Expr* E = NULL;
1774
1775 switch (Terminator->getStmtClass()) {
1776 default:
1777 break;
1778
1779 case Stmt::ForStmtClass:
1780 E = cast<ForStmt>(Terminator)->getCond();
1781 break;
1782
1783 case Stmt::WhileStmtClass:
1784 E = cast<WhileStmt>(Terminator)->getCond();
1785 break;
1786
1787 case Stmt::DoStmtClass:
1788 E = cast<DoStmt>(Terminator)->getCond();
1789 break;
1790
1791 case Stmt::IfStmtClass:
1792 E = cast<IfStmt>(Terminator)->getCond();
1793 break;
1794
1795 case Stmt::ChooseExprClass:
1796 E = cast<ChooseExpr>(Terminator)->getCond();
1797 break;
1798
1799 case Stmt::IndirectGotoStmtClass:
1800 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1801 break;
1802
1803 case Stmt::SwitchStmtClass:
1804 E = cast<SwitchStmt>(Terminator)->getCond();
1805 break;
1806
1807 case Stmt::ConditionalOperatorClass:
1808 E = cast<ConditionalOperator>(Terminator)->getCond();
1809 break;
1810
1811 case Stmt::BinaryOperatorClass: // '&&' and '||'
1812 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek16516e22008-11-12 21:11:49 +00001813 break;
1814
1815 case Stmt::ObjCForCollectionStmtClass:
1816 return Terminator;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001817 }
1818
1819 return E ? E->IgnoreParens() : NULL;
1820}
1821
Ted Kremenekbdbd1b52008-05-16 16:06:00 +00001822bool CFGBlock::hasBinaryBranchTerminator() const {
1823
1824 if (!Terminator)
1825 return false;
1826
1827 Expr* E = NULL;
1828
1829 switch (Terminator->getStmtClass()) {
1830 default:
1831 return false;
1832
1833 case Stmt::ForStmtClass:
1834 case Stmt::WhileStmtClass:
1835 case Stmt::DoStmtClass:
1836 case Stmt::IfStmtClass:
1837 case Stmt::ChooseExprClass:
1838 case Stmt::ConditionalOperatorClass:
1839 case Stmt::BinaryOperatorClass:
1840 return true;
1841 }
1842
1843 return E ? E->IgnoreParens() : NULL;
1844}
1845
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001846
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001847//===----------------------------------------------------------------------===//
1848// CFG Graphviz Visualization
1849//===----------------------------------------------------------------------===//
1850
Ted Kremenek08176a52007-08-31 21:30:12 +00001851
1852#ifndef NDEBUG
Chris Lattner26002172007-09-17 06:16:32 +00001853static StmtPrinterHelper* GraphHelper;
Ted Kremenek08176a52007-08-31 21:30:12 +00001854#endif
1855
1856void CFG::viewCFG() const {
1857#ifndef NDEBUG
1858 StmtPrinterHelper H(this);
1859 GraphHelper = &H;
1860 llvm::ViewGraph(this,"CFG");
1861 GraphHelper = NULL;
Ted Kremenek08176a52007-08-31 21:30:12 +00001862#endif
1863}
1864
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001865namespace llvm {
1866template<>
1867struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
1868 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
1869
Hartmut Kaiser752a0052007-09-16 00:28:28 +00001870#ifndef NDEBUG
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001871 std::string OutSStr;
1872 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek08176a52007-08-31 21:30:12 +00001873 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001874 std::string& OutStr = Out.str();
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001875
1876 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1877
1878 // Process string output to make it nicer...
1879 for (unsigned i = 0; i != OutStr.length(); ++i)
1880 if (OutStr[i] == '\n') { // Left justify
1881 OutStr[i] = '\\';
1882 OutStr.insert(OutStr.begin()+i+1, 'l');
1883 }
1884
1885 return OutStr;
Hartmut Kaiser752a0052007-09-16 00:28:28 +00001886#else
1887 return "";
1888#endif
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001889 }
1890};
1891} // end namespace llvm