blob: 69e82f2b832c86825fe23f7a832558cf3e9588a3 [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
Ted Kremenek0865a992008-08-06 23:20:50 +000045static SourceLocation GetEndLoc(ScopedDecl* D) {
46 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 Kremenekd058a9c2008-04-28 18:00:46 +0000132 CFGBlock* VisitObjCAtTryStmt(ObjCAtTryStmt* S) { return NYS(); }
133 CFGBlock* VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) { return NYS(); }
134 CFGBlock* VisitObjCAtFinallyStmt(ObjCAtFinallyStmt* S) { return NYS(); }
135 CFGBlock* VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) { return NYS(); }
136
137 CFGBlock* VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S){
138 return NYS();
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000139 }
140
Ted Kremenekd68a8d32008-09-26 18:17:07 +0000141 // Blocks.
142 CFGBlock* VisitBlockExpr(BlockExpr* E) { return NYS(); }
143 CFGBlock* VisitBlockDeclRefExpr(BlockDeclRefExpr* E) { return NYS(); }
144
Ted Kremenek73543912007-08-23 21:42:29 +0000145private:
146 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenek79f0a632008-04-16 21:10:48 +0000147 CFGBlock* addStmt(Stmt* Terminator);
148 CFGBlock* WalkAST(Stmt* Terminator, bool AlwaysAddStmt);
149 CFGBlock* WalkAST_VisitChildren(Stmt* Terminator);
Ted Kremenek0865a992008-08-06 23:20:50 +0000150 CFGBlock* WalkAST_VisitDeclSubExpr(ScopedDecl* D);
Ted Kremenek79f0a632008-04-16 21:10:48 +0000151 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* Terminator);
Ted Kremenek73543912007-08-23 21:42:29 +0000152 void FinishBlock(CFGBlock* B);
Ted Kremenekd8313202007-08-22 18:22:34 +0000153
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000154 bool badCFG;
Ted Kremenek97f75312007-08-21 21:42:03 +0000155};
Ted Kremenek09535672008-09-26 22:58:57 +0000156
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000157// FIXME: Add support for dependent-sized array types in C++?
158// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek09535672008-09-26 22:58:57 +0000159static VariableArrayType* FindVA(Type* t) {
160 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
161 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
162 if (vat->getSizeExpr())
163 return vat;
164
165 t = vt->getElementType().getTypePtr();
166 }
167
168 return 0;
169}
Ted Kremenek73543912007-08-23 21:42:29 +0000170
171/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
172/// represent an arbitrary statement. Examples include a single expression
173/// or a function body (compound statement). The ownership of the returned
174/// CFG is transferred to the caller. If CFG construction fails, this method
175/// returns NULL.
176CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000177 assert (cfg);
Ted Kremenek73543912007-08-23 21:42:29 +0000178 if (!Statement) return NULL;
179
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000180 badCFG = false;
181
Ted Kremenek73543912007-08-23 21:42:29 +0000182 // Create an empty block that will serve as the exit block for the CFG.
183 // Since this is the first block added to the CFG, it will be implicitly
184 // registered as the exit block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000185 Succ = createBlock();
186 assert (Succ == &cfg->getExit());
187 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenek73543912007-08-23 21:42:29 +0000188
189 // Visit the statements and create the CFG.
Ted Kremenekfa38c7a2008-02-27 17:33:02 +0000190 CFGBlock* B = Visit(Statement);
191 if (!B) B = Succ;
192
193 if (B) {
Ted Kremenek73543912007-08-23 21:42:29 +0000194 // Finalize the last constructed block. This usually involves
195 // reversing the order of the statements in the block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000196 if (Block) FinishBlock(B);
Ted Kremenek73543912007-08-23 21:42:29 +0000197
198 // Backpatch the gotos whose label -> block mappings we didn't know
199 // when we encountered them.
200 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
201 E = BackpatchBlocks.end(); I != E; ++I ) {
202
203 CFGBlock* B = *I;
204 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
205 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
206
207 // If there is no target for the goto, then we are looking at an
208 // incomplete AST. Handle this by not registering a successor.
209 if (LI == LabelMap.end()) continue;
210
211 B->addSuccessor(LI->second);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000212 }
Ted Kremenek73543912007-08-23 21:42:29 +0000213
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000214 // Add successors to the Indirect Goto Dispatch block (if we have one).
215 if (CFGBlock* B = cfg->getIndirectGotoBlock())
216 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
217 E = AddressTakenLabels.end(); I != E; ++I ) {
218
219 // Lookup the target block.
220 LabelMapTy::iterator LI = LabelMap.find(*I);
221
222 // If there is no target block that contains label, then we are looking
223 // at an incomplete AST. Handle this by not registering a successor.
224 if (LI == LabelMap.end()) continue;
225
226 B->addSuccessor(LI->second);
227 }
Ted Kremenek680fcb82007-09-26 21:23:31 +0000228
Ted Kremenek844cb4d2007-09-17 16:18:02 +0000229 Succ = B;
Ted Kremenek680fcb82007-09-26 21:23:31 +0000230 }
231
232 // Create an empty entry block that has no predecessors.
233 cfg->setEntry(createBlock());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000234
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000235 if (badCFG) {
236 delete cfg;
237 cfg = NULL;
238 return NULL;
239 }
240
Ted Kremenek680fcb82007-09-26 21:23:31 +0000241 // NULL out cfg so that repeated calls to the builder will fail and that
242 // the ownership of the constructed CFG is passed to the caller.
243 CFG* t = cfg;
244 cfg = NULL;
245 return t;
Ted Kremenek73543912007-08-23 21:42:29 +0000246}
247
248/// createBlock - Used to lazily create blocks that are connected
249/// to the current (global) succcessor.
250CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek14594572007-09-05 20:02:05 +0000251 CFGBlock* B = cfg->createBlock();
Ted Kremenek73543912007-08-23 21:42:29 +0000252 if (add_successor && Succ) B->addSuccessor(Succ);
253 return B;
254}
255
256/// FinishBlock - When the last statement has been added to the block,
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000257/// we must reverse the statements because they have been inserted
258/// in reverse order.
Ted Kremenek73543912007-08-23 21:42:29 +0000259void CFGBuilder::FinishBlock(CFGBlock* B) {
260 assert (B);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000261 B->reverseStmts();
Ted Kremenek73543912007-08-23 21:42:29 +0000262}
263
Ted Kremenek65cfa562007-08-27 21:27:44 +0000264/// addStmt - Used to add statements/expressions to the current CFGBlock
265/// "Block". This method calls WalkAST on the passed statement to see if it
266/// contains any short-circuit expressions. If so, it recursively creates
267/// the necessary blocks for such expressions. It returns the "topmost" block
268/// of the created blocks, or the original value of "Block" when this method
269/// was called if no additional blocks are created.
Ted Kremenek79f0a632008-04-16 21:10:48 +0000270CFGBlock* CFGBuilder::addStmt(Stmt* Terminator) {
Ted Kremenek390b9762007-08-30 18:39:40 +0000271 if (!Block) Block = createBlock();
Ted Kremenek79f0a632008-04-16 21:10:48 +0000272 return WalkAST(Terminator,true);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000273}
274
275/// WalkAST - Used by addStmt to walk the subtree of a statement and
Ted Kremeneke822b622007-08-28 18:14:37 +0000276/// add extra blocks for ternary operators, &&, and ||. We also
277/// process "," and DeclStmts (which may contain nested control-flow).
Ted Kremenek79f0a632008-04-16 21:10:48 +0000278CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) {
279 switch (Terminator->getStmtClass()) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000280 case Stmt::ConditionalOperatorClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000281 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000282
283 // Create the confluence block that will "merge" the results
284 // of the ternary expression.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000285 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
286 ConfluenceBlock->appendStmt(C);
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000287 FinishBlock(ConfluenceBlock);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000288
289 // Create a block for the LHS expression if there is an LHS expression.
290 // A GCC extension allows LHS to be NULL, causing the condition to
291 // be the value that is returned instead.
292 // e.g: x ?: y is shorthand for: x ? x : y;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000293 Succ = ConfluenceBlock;
294 Block = NULL;
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000295 CFGBlock* LHSBlock = NULL;
296 if (C->getLHS()) {
297 LHSBlock = Visit(C->getLHS());
298 FinishBlock(LHSBlock);
299 Block = NULL;
300 }
Ted Kremenek65cfa562007-08-27 21:27:44 +0000301
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000302 // Create the block for the RHS expression.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000303 Succ = ConfluenceBlock;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000304 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekd11620d2007-09-11 21:29:43 +0000305 FinishBlock(RHSBlock);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000306
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000307 // Create the block that will contain the condition.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000308 Block = createBlock(false);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000309
310 if (LHSBlock)
311 Block->addSuccessor(LHSBlock);
312 else {
313 // If we have no LHS expression, add the ConfluenceBlock as a direct
314 // successor for the block containing the condition. Moreover,
315 // we need to reverse the order of the predecessors in the
316 // ConfluenceBlock because the RHSBlock will have been added to
317 // the succcessors already, and we want the first predecessor to the
318 // the block containing the expression for the case when the ternary
319 // expression evaluates to true.
320 Block->addSuccessor(ConfluenceBlock);
321 assert (ConfluenceBlock->pred_size() == 2);
322 std::reverse(ConfluenceBlock->pred_begin(),
323 ConfluenceBlock->pred_end());
324 }
325
Ted Kremenek65cfa562007-08-27 21:27:44 +0000326 Block->addSuccessor(RHSBlock);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000327
Ted Kremenek65cfa562007-08-27 21:27:44 +0000328 Block->setTerminator(C);
329 return addStmt(C->getCond());
330 }
Ted Kremenek7f788422007-08-31 17:03:41 +0000331
332 case Stmt::ChooseExprClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000333 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Ted Kremenek7f788422007-08-31 17:03:41 +0000334
335 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
336 ConfluenceBlock->appendStmt(C);
337 FinishBlock(ConfluenceBlock);
338
339 Succ = ConfluenceBlock;
340 Block = NULL;
341 CFGBlock* LHSBlock = Visit(C->getLHS());
Ted Kremenekd11620d2007-09-11 21:29:43 +0000342 FinishBlock(LHSBlock);
343
Ted Kremenek7f788422007-08-31 17:03:41 +0000344 Succ = ConfluenceBlock;
345 Block = NULL;
346 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekd11620d2007-09-11 21:29:43 +0000347 FinishBlock(RHSBlock);
Ted Kremenek7f788422007-08-31 17:03:41 +0000348
349 Block = createBlock(false);
350 Block->addSuccessor(LHSBlock);
351 Block->addSuccessor(RHSBlock);
352 Block->setTerminator(C);
353 return addStmt(C->getCond());
354 }
Ted Kremenek666a6af2007-08-28 16:18:58 +0000355
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000356 case Stmt::DeclStmtClass: {
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000357 DeclStmt *DS = cast<DeclStmt>(Terminator);
358 if (DS->hasSolitaryDecl()) {
Ted Kremenek0865a992008-08-06 23:20:50 +0000359 Block->appendStmt(Terminator);
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000360 return WalkAST_VisitDeclSubExpr(DS->getSolitaryDecl());
Ted Kremenek0865a992008-08-06 23:20:50 +0000361 }
362 else {
363 typedef llvm::SmallVector<ScopedDecl*,10> BufTy;
364 BufTy Buf;
365 CFGBlock* B = 0;
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000366
367 // FIXME: Add a reverse iterator for DeclStmt to avoid this
368 // extra copy.
369 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
370 DI != DE; ++DI)
371 Buf.push_back(*DI);
372
Ted Kremenek0865a992008-08-06 23:20:50 +0000373 for (BufTy::reverse_iterator I=Buf.rbegin(), E=Buf.rend(); I!=E; ++I) {
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000374 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
375 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
376 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Ted Kremenek0865a992008-08-06 23:20:50 +0000377
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000378 // Allocate the DeclStmt using the BumpPtrAllocator. It will
379 // get automatically freed with the CFG. Note that even though
380 // we are using a DeclGroupOwningRef that wraps a singe Decl*,
381 // that Decl* will not get deallocated because the destroy method
382 // of DG is never called.
383 DeclGroupOwningRef DG(*I);
384 ScopedDecl* D = *I;
385 void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
386
387 DeclStmt* DS = new (Mem) DeclStmt(DG, D->getLocation(),
388 GetEndLoc(D));
389
Ted Kremenek0865a992008-08-06 23:20:50 +0000390 // Append the fake DeclStmt to block.
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000391 Block->appendStmt(DS);
392 B = WalkAST_VisitDeclSubExpr(D);
Ted Kremenek0865a992008-08-06 23:20:50 +0000393 }
394 return B;
395 }
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000396 }
Ted Kremenek0865a992008-08-06 23:20:50 +0000397
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000398 case Stmt::AddrLabelExprClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000399 AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000400 AddressTakenLabels.insert(A->getLabel());
401
Ted Kremenek79f0a632008-04-16 21:10:48 +0000402 if (AlwaysAddStmt) Block->appendStmt(Terminator);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000403 return Block;
404 }
Ted Kremenekd11620d2007-09-11 21:29:43 +0000405
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000406 case Stmt::StmtExprClass:
Ted Kremenek79f0a632008-04-16 21:10:48 +0000407 return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
Ted Kremeneke822b622007-08-28 18:14:37 +0000408
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000409 case Stmt::SizeOfAlignOfExprClass: {
410 SizeOfAlignOfExpr* E = cast<SizeOfAlignOfExpr>(Terminator);
Ted Kremenek09535672008-09-26 22:58:57 +0000411
412 // VLA types have expressions that must be evaluated.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000413 if (E->isArgumentType()) {
414 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
415 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
416 addStmt(VA->getSizeExpr());
417 }
418 // Expressions in sizeof/alignof are not evaluated and thus have no
419 // control flow.
420 else
421 Block->appendStmt(Terminator);
Ted Kremenek09535672008-09-26 22:58:57 +0000422
423 return Block;
424 }
425
Ted Kremenekcfaae762007-08-27 21:54:41 +0000426 case Stmt::BinaryOperatorClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000427 BinaryOperator* B = cast<BinaryOperator>(Terminator);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000428
429 if (B->isLogicalOp()) { // && or ||
430 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
431 ConfluenceBlock->appendStmt(B);
432 FinishBlock(ConfluenceBlock);
433
434 // create the block evaluating the LHS
435 CFGBlock* LHSBlock = createBlock(false);
Ted Kremenekb2348522007-12-21 19:49:00 +0000436 LHSBlock->setTerminator(B);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000437
438 // create the block evaluating the RHS
439 Succ = ConfluenceBlock;
440 Block = NULL;
441 CFGBlock* RHSBlock = Visit(B->getRHS());
Zhongxing Xu7636e912008-10-04 05:48:38 +0000442 FinishBlock(RHSBlock);
Ted Kremenekb2348522007-12-21 19:49:00 +0000443
444 // Now link the LHSBlock with RHSBlock.
445 if (B->getOpcode() == BinaryOperator::LOr) {
446 LHSBlock->addSuccessor(ConfluenceBlock);
447 LHSBlock->addSuccessor(RHSBlock);
448 }
449 else {
450 assert (B->getOpcode() == BinaryOperator::LAnd);
451 LHSBlock->addSuccessor(RHSBlock);
452 LHSBlock->addSuccessor(ConfluenceBlock);
453 }
Ted Kremenekcfaae762007-08-27 21:54:41 +0000454
455 // Generate the blocks for evaluating the LHS.
456 Block = LHSBlock;
457 return addStmt(B->getLHS());
Ted Kremeneke822b622007-08-28 18:14:37 +0000458 }
459 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
460 Block->appendStmt(B);
461 addStmt(B->getRHS());
462 return addStmt(B->getLHS());
Ted Kremenek3a819822007-10-01 19:33:33 +0000463 }
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000464
465 break;
Ted Kremenekcfaae762007-08-27 21:54:41 +0000466 }
Ted Kremenekd68a8d32008-09-26 18:17:07 +0000467
468 // Blocks: No support for blocks ... yet
469 case Stmt::BlockExprClass:
470 case Stmt::BlockDeclRefExprClass:
471 return NYS();
Ted Kremeneka9ba5cc2008-02-26 02:37:08 +0000472
473 case Stmt::ParenExprClass:
Ted Kremenek79f0a632008-04-16 21:10:48 +0000474 return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000475
Ted Kremenek65cfa562007-08-27 21:27:44 +0000476 default:
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000477 break;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000478 };
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000479
Ted Kremenek79f0a632008-04-16 21:10:48 +0000480 if (AlwaysAddStmt) Block->appendStmt(Terminator);
481 return WalkAST_VisitChildren(Terminator);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000482}
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000483
Ted Kremenek0865a992008-08-06 23:20:50 +0000484/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions
485/// for initializers in Decls.
486CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(ScopedDecl* D) {
487 VarDecl* VD = dyn_cast<VarDecl>(D);
488
489 if (!VD)
Ted Kremenekf4e35622007-11-18 20:06:01 +0000490 return Block;
491
Ted Kremenek0865a992008-08-06 23:20:50 +0000492 Expr* Init = VD->getInit();
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000493
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000494 if (Init) {
495 // Optimization: Don't create separate block-level statements for literals.
496 switch (Init->getStmtClass()) {
497 case Stmt::IntegerLiteralClass:
498 case Stmt::CharacterLiteralClass:
499 case Stmt::StringLiteralClass:
500 break;
501 default:
502 Block = addStmt(Init);
503 }
Ted Kremenek4ad64e82008-02-29 22:32:24 +0000504 }
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000505
506 // If the type of VD is a VLA, then we must process its size expressions.
507 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
508 VA = FindVA(VA->getElementType().getTypePtr()))
509 Block = addStmt(VA->getSizeExpr());
Ted Kremenek4ad64e82008-02-29 22:32:24 +0000510
Ted Kremeneke822b622007-08-28 18:14:37 +0000511 return Block;
512}
513
Ted Kremenek65cfa562007-08-27 21:27:44 +0000514/// WalkAST_VisitChildren - Utility method to call WalkAST on the
515/// children of a Stmt.
Ted Kremenek79f0a632008-04-16 21:10:48 +0000516CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000517 CFGBlock* B = Block;
Ted Kremenek79f0a632008-04-16 21:10:48 +0000518 for (Stmt::child_iterator I = Terminator->child_begin(), E = Terminator->child_end() ;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000519 I != E; ++I)
Ted Kremenek680fcb82007-09-26 21:23:31 +0000520 if (*I) B = WalkAST(*I);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000521
522 return B;
523}
524
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000525/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
526/// expressions (a GCC extension).
Ted Kremenek79f0a632008-04-16 21:10:48 +0000527CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
528 Block->appendStmt(Terminator);
529 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000530}
531
Ted Kremenek73543912007-08-23 21:42:29 +0000532/// VisitStmt - Handle statements with no branching control flow.
533CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
534 // We cannot assume that we are in the middle of a basic block, since
535 // the CFG might only be constructed for this single statement. If
536 // we have no current basic block, just create one lazily.
537 if (!Block) Block = createBlock();
538
539 // Simply add the statement to the current block. We actually
540 // insert statements in reverse order; this order is reversed later
541 // when processing the containing element in the AST.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000542 addStmt(Statement);
543
Ted Kremenek73543912007-08-23 21:42:29 +0000544 return Block;
545}
546
547CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
548 return Block;
549}
550
551CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000552
553 CFGBlock* LastBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000554
Ted Kremenekfeb0e992008-02-26 00:22:58 +0000555 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
556 I != E; ++I ) {
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000557 LastBlock = Visit(*I);
Ted Kremenekfeb0e992008-02-26 00:22:58 +0000558 }
559
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000560 return LastBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000561}
562
563CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
564 // We may see an if statement in the middle of a basic block, or
565 // it may be the first statement we are processing. In either case,
566 // we create a new basic block. First, we create the blocks for
567 // the then...else statements, and then we create the block containing
568 // the if statement. If we were in the middle of a block, we
569 // stop processing that block and reverse its statements. That block
570 // is then the implicit successor for the "then" and "else" clauses.
571
572 // The block we were proccessing is now finished. Make it the
573 // successor block.
574 if (Block) {
575 Succ = Block;
576 FinishBlock(Block);
577 }
578
579 // Process the false branch. NULL out Block so that the recursive
580 // call to Visit will create a new basic block.
581 // Null out Block so that all successor
582 CFGBlock* ElseBlock = Succ;
583
584 if (Stmt* Else = I->getElse()) {
585 SaveAndRestore<CFGBlock*> sv(Succ);
586
587 // NULL out Block so that the recursive call to Visit will
588 // create a new basic block.
589 Block = NULL;
Ted Kremenek44db7872007-08-30 18:13:31 +0000590 ElseBlock = Visit(Else);
591
592 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
593 ElseBlock = sv.get();
594 else if (Block)
595 FinishBlock(ElseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000596 }
597
598 // Process the true branch. NULL out Block so that the recursive
599 // call to Visit will create a new basic block.
600 // Null out Block so that all successor
601 CFGBlock* ThenBlock;
602 {
603 Stmt* Then = I->getThen();
604 assert (Then);
605 SaveAndRestore<CFGBlock*> sv(Succ);
606 Block = NULL;
Ted Kremenek44db7872007-08-30 18:13:31 +0000607 ThenBlock = Visit(Then);
608
609 if (!ThenBlock) // Can occur when the Then body has all NullStmts.
610 ThenBlock = sv.get();
611 else if (Block)
612 FinishBlock(ThenBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000613 }
614
615 // Now create a new block containing the if statement.
616 Block = createBlock(false);
Ted Kremenek73543912007-08-23 21:42:29 +0000617
618 // Set the terminator of the new block to the If statement.
619 Block->setTerminator(I);
620
621 // Now add the successors.
622 Block->addSuccessor(ThenBlock);
623 Block->addSuccessor(ElseBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000624
625 // Add the condition as the last statement in the new block. This
626 // may create new blocks as the condition may contain control-flow. Any
627 // newly created blocks will be pointed to be "Block".
Ted Kremenek1eaa6712008-01-30 23:02:42 +0000628 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenek73543912007-08-23 21:42:29 +0000629}
Ted Kremenekd11620d2007-09-11 21:29:43 +0000630
Ted Kremenek73543912007-08-23 21:42:29 +0000631
632CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
633 // If we were in the middle of a block we stop processing that block
634 // and reverse its statements.
635 //
636 // NOTE: If a "return" appears in the middle of a block, this means
637 // that the code afterwards is DEAD (unreachable). We still
638 // keep a basic block for that code; a simple "mark-and-sweep"
639 // from the entry block will be able to report such dead
640 // blocks.
641 if (Block) FinishBlock(Block);
642
643 // Create the new block.
644 Block = createBlock(false);
645
646 // The Exit block is the only successor.
647 Block->addSuccessor(&cfg->getExit());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000648
649 // Add the return statement to the block. This may create new blocks
650 // if R contains control-flow (short-circuit operations).
651 return addStmt(R);
Ted Kremenek73543912007-08-23 21:42:29 +0000652}
653
654CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
655 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek82e8a192008-03-15 07:45:02 +0000656 Visit(L->getSubStmt());
657 CFGBlock* LabelBlock = Block;
Ted Kremenek9b0d1b62007-08-30 18:20:57 +0000658
659 if (!LabelBlock) // This can happen when the body is empty, i.e.
660 LabelBlock=createBlock(); // scopes that only contains NullStmts.
661
Ted Kremenek73543912007-08-23 21:42:29 +0000662 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
663 LabelMap[ L ] = LabelBlock;
664
665 // Labels partition blocks, so this is the end of the basic block
Ted Kremenekec055e12007-08-29 23:20:49 +0000666 // we were processing (L is the block's label). Because this is
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000667 // label (and we have already processed the substatement) there is no
668 // extra control-flow to worry about.
Ted Kremenekec055e12007-08-29 23:20:49 +0000669 LabelBlock->setLabel(L);
Ted Kremenek73543912007-08-23 21:42:29 +0000670 FinishBlock(LabelBlock);
671
672 // We set Block to NULL to allow lazy creation of a new block
673 // (if necessary);
674 Block = NULL;
675
676 // This block is now the implicit successor of other blocks.
677 Succ = LabelBlock;
678
679 return LabelBlock;
680}
681
682CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
683 // Goto is a control-flow statement. Thus we stop processing the
684 // current block and create a new one.
685 if (Block) FinishBlock(Block);
686 Block = createBlock(false);
687 Block->setTerminator(G);
688
689 // If we already know the mapping to the label block add the
690 // successor now.
691 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
692
693 if (I == LabelMap.end())
694 // We will need to backpatch this block later.
695 BackpatchBlocks.push_back(Block);
696 else
697 Block->addSuccessor(I->second);
698
699 return Block;
700}
701
702CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
703 // "for" is a control-flow statement. Thus we stop processing the
704 // current block.
705
706 CFGBlock* LoopSuccessor = NULL;
707
708 if (Block) {
709 FinishBlock(Block);
710 LoopSuccessor = Block;
711 }
712 else LoopSuccessor = Succ;
713
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000714 // Because of short-circuit evaluation, the condition of the loop
715 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
716 // blocks that evaluate the condition.
717 CFGBlock* ExitConditionBlock = createBlock(false);
718 CFGBlock* EntryConditionBlock = ExitConditionBlock;
719
720 // Set the terminator for the "exit" condition block.
721 ExitConditionBlock->setTerminator(F);
722
723 // Now add the actual condition to the condition block. Because the
724 // condition itself may contain control-flow, new blocks may be created.
725 if (Stmt* C = F->getCond()) {
726 Block = ExitConditionBlock;
727 EntryConditionBlock = addStmt(C);
728 if (Block) FinishBlock(EntryConditionBlock);
729 }
Ted Kremenek73543912007-08-23 21:42:29 +0000730
731 // The condition block is the implicit successor for the loop body as
732 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000733 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000734
735 // Now create the loop body.
736 {
737 assert (F->getBody());
738
739 // Save the current values for Block, Succ, and continue and break targets
740 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
741 save_continue(ContinueTargetBlock),
742 save_break(BreakTargetBlock);
Ted Kremenek77f93372008-09-04 21:48:47 +0000743
Ted Kremenek390b9762007-08-30 18:39:40 +0000744 // Create a new block to contain the (bottom) of the loop body.
745 Block = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000746
Ted Kremenek77f93372008-09-04 21:48:47 +0000747 if (Stmt* I = F->getInc()) {
748 // Generate increment code in its own basic block. This is the target
749 // of continue statements.
Ted Kremenekd19e99e2008-11-24 20:50:24 +0000750 Succ = Visit(I);
751
752 // Finish up the increment block if it hasn't been already.
753 if (Block) {
754 assert (Block == Succ);
755 FinishBlock(Block);
756 Block = 0;
757 }
758
Ted Kremenek77f93372008-09-04 21:48:47 +0000759 ContinueTargetBlock = Succ;
760 }
761 else {
762 // No increment code. Continues should go the the entry condition block.
763 ContinueTargetBlock = EntryConditionBlock;
764 }
765
766 // All breaks should go to the code following the loop.
767 BreakTargetBlock = LoopSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +0000768
769 // Now populate the body block, and in the process create new blocks
770 // as we walk the body of the loop.
771 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenek390b9762007-08-30 18:39:40 +0000772
773 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +0000774 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek390b9762007-08-30 18:39:40 +0000775 else if (Block)
776 FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000777
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000778 // This new body block is a successor to our "exit" condition block.
779 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000780 }
781
782 // Link up the condition block with the code that follows the loop.
783 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000784 ExitConditionBlock->addSuccessor(LoopSuccessor);
785
Ted Kremenek73543912007-08-23 21:42:29 +0000786 // If the loop contains initialization, create a new block for those
787 // statements. This block can also contain statements that precede
788 // the loop.
789 if (Stmt* I = F->getInit()) {
790 Block = createBlock();
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000791 return addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000792 }
793 else {
794 // There is no loop initialization. We are thus basically a while
795 // loop. NULL out Block to force lazy block construction.
796 Block = NULL;
Ted Kremenek9ff572c2008-02-27 07:20:00 +0000797 Succ = EntryConditionBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000798 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000799 }
800}
801
Ted Kremenek05335162008-11-11 17:10:00 +0000802CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
803 // Objective-C fast enumeration 'for' statements:
804 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
805 //
806 // for ( Type newVariable in collection_expression ) { statements }
807 //
808 // becomes:
809 //
810 // prologue:
811 // 1. collection_expression
812 // T. jump to loop_entry
813 // loop_entry:
Ted Kremenek65514842008-11-14 01:57:41 +0000814 // 1. side-effects of element expression
Ted Kremenek05335162008-11-11 17:10:00 +0000815 // 1. ObjCForCollectionStmt [performs binding to newVariable]
816 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
817 // TB:
818 // statements
819 // T. jump to loop_entry
820 // FB:
821 // what comes after
822 //
823 // and
824 //
825 // Type existingItem;
826 // for ( existingItem in expression ) { statements }
827 //
828 // becomes:
829 //
830 // the same with newVariable replaced with existingItem; the binding
831 // works the same except that for one ObjCForCollectionStmt::getElement()
832 // returns a DeclStmt and the other returns a DeclRefExpr.
833 //
834
835 CFGBlock* LoopSuccessor = 0;
836
837 if (Block) {
838 FinishBlock(Block);
839 LoopSuccessor = Block;
840 Block = 0;
841 }
842 else LoopSuccessor = Succ;
843
Ted Kremenek65514842008-11-14 01:57:41 +0000844 // Build the condition blocks.
845 CFGBlock* ExitConditionBlock = createBlock(false);
846 CFGBlock* EntryConditionBlock = ExitConditionBlock;
847
848 // Set the terminator for the "exit" condition block.
849 ExitConditionBlock->setTerminator(S);
850
851 // The last statement in the block should be the ObjCForCollectionStmt,
852 // which performs the actual binding to 'element' and determines if there
853 // are any more items in the collection.
854 ExitConditionBlock->appendStmt(S);
855 Block = ExitConditionBlock;
856
857 // Walk the 'element' expression to see if there are any side-effects. We
858 // generate new blocks as necesary. We DON'T add the statement by default
859 // to the CFG unless it contains control-flow.
860 EntryConditionBlock = WalkAST(S->getElement(), false);
861 if (Block) { FinishBlock(EntryConditionBlock); Block = 0; }
862
863 // The condition block is the implicit successor for the loop body as
864 // well as any code above the loop.
865 Succ = EntryConditionBlock;
Ted Kremenek05335162008-11-11 17:10:00 +0000866
867 // Now create the true branch.
Ted Kremenek65514842008-11-14 01:57:41 +0000868 {
869 // Save the current values for Succ, continue and break targets.
870 SaveAndRestore<CFGBlock*> save_Succ(Succ),
871 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
872
873 BreakTargetBlock = LoopSuccessor;
874 ContinueTargetBlock = EntryConditionBlock;
875
876 CFGBlock* BodyBlock = Visit(S->getBody());
877
878 if (!BodyBlock)
879 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
880 else if (Block)
881 FinishBlock(BodyBlock);
882
883 // This new body block is a successor to our "exit" condition block.
884 ExitConditionBlock->addSuccessor(BodyBlock);
885 }
Ted Kremenekf5383072008-11-13 06:36:45 +0000886
Ted Kremenek65514842008-11-14 01:57:41 +0000887 // Link up the condition block with the code that follows the loop.
888 // (the false branch).
889 ExitConditionBlock->addSuccessor(LoopSuccessor);
890
Ted Kremenek05335162008-11-11 17:10:00 +0000891 // Now create a prologue block to contain the collection expression.
Ted Kremenek65514842008-11-14 01:57:41 +0000892 Block = createBlock();
Ted Kremenek05335162008-11-11 17:10:00 +0000893 return addStmt(S->getCollection());
894}
895
896
Ted Kremenek73543912007-08-23 21:42:29 +0000897CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
898 // "while" is a control-flow statement. Thus we stop processing the
899 // current block.
900
901 CFGBlock* LoopSuccessor = NULL;
902
903 if (Block) {
904 FinishBlock(Block);
905 LoopSuccessor = Block;
906 }
907 else LoopSuccessor = Succ;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000908
909 // Because of short-circuit evaluation, the condition of the loop
910 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
911 // blocks that evaluate the condition.
912 CFGBlock* ExitConditionBlock = createBlock(false);
913 CFGBlock* EntryConditionBlock = ExitConditionBlock;
914
915 // Set the terminator for the "exit" condition block.
916 ExitConditionBlock->setTerminator(W);
917
918 // Now add the actual condition to the condition block. Because the
919 // condition itself may contain control-flow, new blocks may be created.
920 // Thus we update "Succ" after adding the condition.
921 if (Stmt* C = W->getCond()) {
922 Block = ExitConditionBlock;
923 EntryConditionBlock = addStmt(C);
Ted Kremenekd0c87602008-02-27 00:28:17 +0000924 assert (Block == EntryConditionBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000925 if (Block) FinishBlock(EntryConditionBlock);
926 }
Ted Kremenek73543912007-08-23 21:42:29 +0000927
928 // The condition block is the implicit successor for the loop body as
929 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000930 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000931
932 // Process the loop body.
933 {
934 assert (W->getBody());
935
936 // Save the current values for Block, Succ, and continue and break targets
937 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
938 save_continue(ContinueTargetBlock),
939 save_break(BreakTargetBlock);
940
941 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000942 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000943
944 // All breaks should go to the code following the loop.
945 BreakTargetBlock = LoopSuccessor;
946
947 // NULL out Block to force lazy instantiation of blocks for the body.
948 Block = NULL;
949
950 // Create the body. The returned block is the entry to the loop body.
951 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenek390b9762007-08-30 18:39:40 +0000952
953 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +0000954 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek390b9762007-08-30 18:39:40 +0000955 else if (Block)
956 FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000957
958 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000959 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000960 }
961
962 // Link up the condition block with the code that follows the loop.
963 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000964 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000965
966 // There can be no more statements in the condition block
967 // since we loop back to this block. NULL out Block to force
968 // lazy creation of another block.
969 Block = NULL;
970
971 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek9ff572c2008-02-27 07:20:00 +0000972 Succ = EntryConditionBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000973 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000974}
975
976CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
977 // "do...while" is a control-flow statement. Thus we stop processing the
978 // current block.
979
980 CFGBlock* LoopSuccessor = NULL;
981
982 if (Block) {
983 FinishBlock(Block);
984 LoopSuccessor = Block;
985 }
986 else LoopSuccessor = Succ;
987
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000988 // Because of short-circuit evaluation, the condition of the loop
989 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
990 // blocks that evaluate the condition.
991 CFGBlock* ExitConditionBlock = createBlock(false);
992 CFGBlock* EntryConditionBlock = ExitConditionBlock;
993
994 // Set the terminator for the "exit" condition block.
995 ExitConditionBlock->setTerminator(D);
Ted Kremenek73543912007-08-23 21:42:29 +0000996
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000997 // Now add the actual condition to the condition block. Because the
998 // condition itself may contain control-flow, new blocks may be created.
999 if (Stmt* C = D->getCond()) {
1000 Block = ExitConditionBlock;
1001 EntryConditionBlock = addStmt(C);
1002 if (Block) FinishBlock(EntryConditionBlock);
1003 }
Ted Kremenek73543912007-08-23 21:42:29 +00001004
Ted Kremenek9ff572c2008-02-27 07:20:00 +00001005 // The condition block is the implicit successor for the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001006 Succ = EntryConditionBlock;
1007
Ted Kremenek73543912007-08-23 21:42:29 +00001008 // Process the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001009 CFGBlock* BodyBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +00001010 {
1011 assert (D->getBody());
1012
1013 // Save the current values for Block, Succ, and continue and break targets
1014 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1015 save_continue(ContinueTargetBlock),
1016 save_break(BreakTargetBlock);
1017
1018 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001019 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001020
1021 // All breaks should go to the code following the loop.
1022 BreakTargetBlock = LoopSuccessor;
1023
1024 // NULL out Block to force lazy instantiation of blocks for the body.
1025 Block = NULL;
1026
1027 // Create the body. The returned block is the entry to the loop body.
1028 BodyBlock = Visit(D->getBody());
Ted Kremenek73543912007-08-23 21:42:29 +00001029
Ted Kremenek390b9762007-08-30 18:39:40 +00001030 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +00001031 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek390b9762007-08-30 18:39:40 +00001032 else if (Block)
1033 FinishBlock(BodyBlock);
1034
Ted Kremenek73543912007-08-23 21:42:29 +00001035 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001036 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +00001037 }
1038
1039 // Link up the condition block with the code that follows the loop.
1040 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001041 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +00001042
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001043 // There can be no more statements in the body block(s)
1044 // since we loop back to the body. NULL out Block to force
Ted Kremenek73543912007-08-23 21:42:29 +00001045 // lazy creation of another block.
1046 Block = NULL;
1047
1048 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek9ff572c2008-02-27 07:20:00 +00001049 Succ = BodyBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001050 return BodyBlock;
1051}
1052
1053CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1054 // "continue" is a control-flow statement. Thus we stop processing the
1055 // current block.
1056 if (Block) FinishBlock(Block);
1057
1058 // Now create a new block that ends with the continue statement.
1059 Block = createBlock(false);
1060 Block->setTerminator(C);
1061
1062 // If there is no target for the continue, then we are looking at an
1063 // incomplete AST. Handle this by not registering a successor.
1064 if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
1065
1066 return Block;
1067}
1068
1069CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
1070 // "break" is a control-flow statement. Thus we stop processing the
1071 // current block.
1072 if (Block) FinishBlock(Block);
1073
1074 // Now create a new block that ends with the continue statement.
1075 Block = createBlock(false);
1076 Block->setTerminator(B);
1077
1078 // If there is no target for the break, then we are looking at an
1079 // incomplete AST. Handle this by not registering a successor.
1080 if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
1081
1082 return Block;
1083}
1084
Ted Kremenek79f0a632008-04-16 21:10:48 +00001085CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek73543912007-08-23 21:42:29 +00001086 // "switch" is a control-flow statement. Thus we stop processing the
1087 // current block.
1088 CFGBlock* SwitchSuccessor = NULL;
1089
1090 if (Block) {
1091 FinishBlock(Block);
1092 SwitchSuccessor = Block;
1093 }
1094 else SwitchSuccessor = Succ;
1095
1096 // Save the current "switch" context.
1097 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek97bc3422008-02-13 22:05:39 +00001098 save_break(BreakTargetBlock),
1099 save_default(DefaultCaseBlock);
1100
1101 // Set the "default" case to be the block after the switch statement.
1102 // If the switch statement contains a "default:", this value will
1103 // be overwritten with the block for that code.
1104 DefaultCaseBlock = SwitchSuccessor;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001105
Ted Kremenek73543912007-08-23 21:42:29 +00001106 // Create a new block that will contain the switch statement.
1107 SwitchTerminatedBlock = createBlock(false);
1108
Ted Kremenek73543912007-08-23 21:42:29 +00001109 // Now process the switch body. The code after the switch is the implicit
1110 // successor.
1111 Succ = SwitchSuccessor;
1112 BreakTargetBlock = SwitchSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +00001113
1114 // When visiting the body, the case statements should automatically get
1115 // linked up to the switch. We also don't keep a pointer to the body,
1116 // since all control-flow from the switch goes to case/default statements.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001117 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001118 Block = NULL;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001119 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001120 if (Block) FinishBlock(BodyBlock);
1121
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001122 // If we have no "default:" case, the default transition is to the
1123 // code following the switch body.
Ted Kremenek97bc3422008-02-13 22:05:39 +00001124 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001125
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001126 // Add the terminator and condition in the switch block.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001127 SwitchTerminatedBlock->setTerminator(Terminator);
1128 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek73543912007-08-23 21:42:29 +00001129 Block = SwitchTerminatedBlock;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001130
Ted Kremenek79f0a632008-04-16 21:10:48 +00001131 return addStmt(Terminator->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +00001132}
1133
Ted Kremenek79f0a632008-04-16 21:10:48 +00001134CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Ted Kremenek97bc3422008-02-13 22:05:39 +00001135 // CaseStmts are essentially labels, so they are the
Ted Kremenek73543912007-08-23 21:42:29 +00001136 // first statement in a block.
Ted Kremenek44659d82007-08-30 18:48:11 +00001137
Ted Kremenek79f0a632008-04-16 21:10:48 +00001138 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek44659d82007-08-30 18:48:11 +00001139 CFGBlock* CaseBlock = Block;
1140 if (!CaseBlock) CaseBlock = createBlock();
1141
Ted Kremenek97bc3422008-02-13 22:05:39 +00001142 // Cases statements partition blocks, so this is the top of
1143 // the basic block we were processing (the "case XXX:" is the label).
Ted Kremenek79f0a632008-04-16 21:10:48 +00001144 CaseBlock->setLabel(Terminator);
Ted Kremenek73543912007-08-23 21:42:29 +00001145 FinishBlock(CaseBlock);
1146
1147 // Add this block to the list of successors for the block with the
1148 // switch statement.
Ted Kremenek97bc3422008-02-13 22:05:39 +00001149 assert (SwitchTerminatedBlock);
1150 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +00001151
1152 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1153 Block = NULL;
1154
1155 // This block is now the implicit successor of other blocks.
1156 Succ = CaseBlock;
1157
Ted Kremenek82e8a192008-03-15 07:45:02 +00001158 return CaseBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001159}
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001160
Ted Kremenek79f0a632008-04-16 21:10:48 +00001161CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1162 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek97bc3422008-02-13 22:05:39 +00001163 DefaultCaseBlock = Block;
1164 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1165
1166 // Default statements partition blocks, so this is the top of
1167 // the basic block we were processing (the "default:" is the label).
Ted Kremenek79f0a632008-04-16 21:10:48 +00001168 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek97bc3422008-02-13 22:05:39 +00001169 FinishBlock(DefaultCaseBlock);
1170
1171 // Unlike case statements, we don't add the default block to the
1172 // successors for the switch statement immediately. This is done
1173 // when we finish processing the switch statement. This allows for
1174 // the default case (including a fall-through to the code after the
1175 // switch statement) to always be the last successor of a switch-terminated
1176 // block.
1177
1178 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1179 Block = NULL;
1180
1181 // This block is now the implicit successor of other blocks.
1182 Succ = DefaultCaseBlock;
1183
1184 return DefaultCaseBlock;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001185}
Ted Kremenek73543912007-08-23 21:42:29 +00001186
Ted Kremenek0edd3a92007-08-28 19:26:49 +00001187CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1188 // Lazily create the indirect-goto dispatch block if there isn't one
1189 // already.
1190 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
1191
1192 if (!IBlock) {
1193 IBlock = createBlock(false);
1194 cfg->setIndirectGotoBlock(IBlock);
1195 }
1196
1197 // IndirectGoto is a control-flow statement. Thus we stop processing the
1198 // current block and create a new one.
1199 if (Block) FinishBlock(Block);
1200 Block = createBlock(false);
1201 Block->setTerminator(I);
1202 Block->addSuccessor(IBlock);
1203 return addStmt(I->getTarget());
1204}
1205
Ted Kremenek73543912007-08-23 21:42:29 +00001206
Ted Kremenekd6e50602007-08-23 21:26:19 +00001207} // end anonymous namespace
Ted Kremenek4db5b452007-08-23 16:51:22 +00001208
1209/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
1210/// block has no successors or predecessors. If this is the first block
1211/// created in the CFG, it is automatically set to be the Entry and Exit
1212/// of the CFG.
Ted Kremenek14594572007-09-05 20:02:05 +00001213CFGBlock* CFG::createBlock() {
Ted Kremenek4db5b452007-08-23 16:51:22 +00001214 bool first_block = begin() == end();
1215
1216 // Create the block.
Ted Kremenek14594572007-09-05 20:02:05 +00001217 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek4db5b452007-08-23 16:51:22 +00001218
1219 // If this is the first block, set it as the Entry and Exit.
1220 if (first_block) Entry = Exit = &front();
1221
1222 // Return the block.
1223 return &front();
Ted Kremenek97f75312007-08-21 21:42:03 +00001224}
1225
Ted Kremenek4db5b452007-08-23 16:51:22 +00001226/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1227/// CFG is returned to the caller.
1228CFG* CFG::buildCFG(Stmt* Statement) {
1229 CFGBuilder Builder;
1230 return Builder.buildCFG(Statement);
1231}
1232
1233/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek97f75312007-08-21 21:42:03 +00001234void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1235
Ted Kremenek3a819822007-10-01 19:33:33 +00001236//===----------------------------------------------------------------------===//
1237// CFG: Queries for BlkExprs.
1238//===----------------------------------------------------------------------===//
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001239
Ted Kremenek3a819822007-10-01 19:33:33 +00001240namespace {
Ted Kremenekab6c5902008-01-17 20:48:37 +00001241 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek3a819822007-10-01 19:33:33 +00001242}
1243
Ted Kremenek79f0a632008-04-16 21:10:48 +00001244static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1245 if (!Terminator)
Ted Kremenekc6fda602008-01-26 00:03:27 +00001246 return;
1247
Ted Kremenek79f0a632008-04-16 21:10:48 +00001248 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001249 if (!*I) continue;
1250
1251 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1252 if (B->isAssignmentOp()) Set.insert(B);
1253
1254 FindSubExprAssignments(*I, Set);
1255 }
1256}
1257
Ted Kremenek3a819822007-10-01 19:33:33 +00001258static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1259 BlkExprMapTy* M = new BlkExprMapTy();
1260
Ted Kremenekc6fda602008-01-26 00:03:27 +00001261 // Look for assignments that are used as subexpressions. These are the
Ted Kremenek79f0a632008-04-16 21:10:48 +00001262 // only assignments that we want to *possibly* register as a block-level
1263 // expression. Basically, if an assignment occurs both in a subexpression
1264 // and at the block-level, it is a block-level expression.
Ted Kremenekc6fda602008-01-26 00:03:27 +00001265 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
1266
Ted Kremenek3a819822007-10-01 19:33:33 +00001267 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1268 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc6fda602008-01-26 00:03:27 +00001269 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenekab6c5902008-01-17 20:48:37 +00001270
Ted Kremenek79f0a632008-04-16 21:10:48 +00001271 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
1272
1273 // Iterate over the statements again on identify the Expr* and Stmt* at
1274 // the block-level that are block-level expressions.
1275
Ted Kremenekc6fda602008-01-26 00:03:27 +00001276 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek79f0a632008-04-16 21:10:48 +00001277 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001278
Ted Kremenek79f0a632008-04-16 21:10:48 +00001279 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001280 // Assignment expressions that are not nested within another
1281 // expression are really "statements" whose value is never
1282 // used by another expression.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001283 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenekc6fda602008-01-26 00:03:27 +00001284 continue;
1285 }
Ted Kremenek79f0a632008-04-16 21:10:48 +00001286 else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001287 // Special handling for statement expressions. The last statement
1288 // in the statement expression is also a block-level expr.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001289 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenekab6c5902008-01-17 20:48:37 +00001290 if (!C->body_empty()) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001291 unsigned x = M->size();
Ted Kremenekab6c5902008-01-17 20:48:37 +00001292 (*M)[C->body_back()] = x;
1293 }
1294 }
Ted Kremenek5b4eb172008-01-25 23:22:27 +00001295
Ted Kremenekc6fda602008-01-26 00:03:27 +00001296 unsigned x = M->size();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001297 (*M)[Exp] = x;
Ted Kremenekc6fda602008-01-26 00:03:27 +00001298 }
1299
Ted Kremenek79f0a632008-04-16 21:10:48 +00001300 // Look at terminators. The condition is a block-level expression.
1301
Ted Kremenek16516e22008-11-12 21:11:49 +00001302 Stmt* S = I->getTerminatorCondition();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001303
Ted Kremenek16516e22008-11-12 21:11:49 +00001304 if (S && M->find(S) == M->end()) {
Ted Kremenek79f0a632008-04-16 21:10:48 +00001305 unsigned x = M->size();
Ted Kremenek16516e22008-11-12 21:11:49 +00001306 (*M)[S] = x;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001307 }
1308 }
1309
Ted Kremenek3a819822007-10-01 19:33:33 +00001310 return M;
1311}
1312
Ted Kremenekab6c5902008-01-17 20:48:37 +00001313CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1314 assert(S != NULL);
Ted Kremenek3a819822007-10-01 19:33:33 +00001315 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
1316
1317 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenekab6c5902008-01-17 20:48:37 +00001318 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3a819822007-10-01 19:33:33 +00001319
1320 if (I == M->end()) return CFG::BlkExprNumTy();
1321 else return CFG::BlkExprNumTy(I->second);
1322}
1323
1324unsigned CFG::getNumBlkExprs() {
1325 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1326 return M->size();
1327 else {
1328 // We assume callers interested in the number of BlkExprs will want
1329 // the map constructed if it doesn't already exist.
1330 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1331 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1332 }
1333}
1334
Ted Kremenekd058a9c2008-04-28 18:00:46 +00001335//===----------------------------------------------------------------------===//
Ted Kremenekd058a9c2008-04-28 18:00:46 +00001336// Cleanup: CFG dstor.
1337//===----------------------------------------------------------------------===//
1338
Ted Kremenek3a819822007-10-01 19:33:33 +00001339CFG::~CFG() {
1340 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1341}
1342
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001343//===----------------------------------------------------------------------===//
1344// CFG pretty printing
1345//===----------------------------------------------------------------------===//
1346
Ted Kremenekd8313202007-08-22 18:22:34 +00001347namespace {
1348
Ted Kremenek98cee3a2008-01-08 18:15:10 +00001349class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Ted Kremenek86afc042007-08-31 22:26:13 +00001350
Ted Kremenek08176a52007-08-31 21:30:12 +00001351 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1352 StmtMapTy StmtMap;
1353 signed CurrentBlock;
1354 unsigned CurrentStmt;
Ted Kremenek86afc042007-08-31 22:26:13 +00001355
Ted Kremenek73543912007-08-23 21:42:29 +00001356public:
Ted Kremenek86afc042007-08-31 22:26:13 +00001357
Ted Kremenek08176a52007-08-31 21:30:12 +00001358 StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) {
1359 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1360 unsigned j = 1;
1361 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1362 BI != BEnd; ++BI, ++j )
1363 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1364 }
1365 }
1366
1367 virtual ~StmtPrinterHelper() {}
1368
1369 void setBlockID(signed i) { CurrentBlock = i; }
1370 void setStmtID(unsigned i) { CurrentStmt = i; }
1371
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001372 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001373
Ted Kremenek79f0a632008-04-16 21:10:48 +00001374 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek08176a52007-08-31 21:30:12 +00001375
1376 if (I == StmtMap.end())
1377 return false;
1378
1379 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
1380 && I->second.second == CurrentStmt)
1381 return false;
1382
Ted Kremenek86afc042007-08-31 22:26:13 +00001383 OS << "[B" << I->second.first << "." << I->second.second << "]";
1384 return true;
Ted Kremenek08176a52007-08-31 21:30:12 +00001385 }
1386};
1387
Ted Kremenek98cee3a2008-01-08 18:15:10 +00001388class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1389 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
1390
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001391 llvm::raw_ostream& OS;
Ted Kremenek08176a52007-08-31 21:30:12 +00001392 StmtPrinterHelper* Helper;
1393public:
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001394 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper)
Ted Kremenek08176a52007-08-31 21:30:12 +00001395 : OS(os), Helper(helper) {}
Ted Kremenek73543912007-08-23 21:42:29 +00001396
1397 void VisitIfStmt(IfStmt* I) {
1398 OS << "if ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001399 I->getCond()->printPretty(OS,Helper);
Ted Kremenek73543912007-08-23 21:42:29 +00001400 }
1401
1402 // Default case.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001403 void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS); }
Ted Kremenek73543912007-08-23 21:42:29 +00001404
1405 void VisitForStmt(ForStmt* F) {
1406 OS << "for (" ;
Ted Kremenek23a1d662007-08-30 21:28:02 +00001407 if (F->getInit()) OS << "...";
1408 OS << "; ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001409 if (Stmt* C = F->getCond()) C->printPretty(OS,Helper);
Ted Kremenek23a1d662007-08-30 21:28:02 +00001410 OS << "; ";
1411 if (F->getInc()) OS << "...";
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001412 OS << ")";
Ted Kremenek73543912007-08-23 21:42:29 +00001413 }
1414
1415 void VisitWhileStmt(WhileStmt* W) {
1416 OS << "while " ;
Ted Kremenek08176a52007-08-31 21:30:12 +00001417 if (Stmt* C = W->getCond()) C->printPretty(OS,Helper);
Ted Kremenek73543912007-08-23 21:42:29 +00001418 }
1419
1420 void VisitDoStmt(DoStmt* D) {
1421 OS << "do ... while ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001422 if (Stmt* C = D->getCond()) C->printPretty(OS,Helper);
Ted Kremenek65cfa562007-08-27 21:27:44 +00001423 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001424
Ted Kremenek79f0a632008-04-16 21:10:48 +00001425 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek65cfa562007-08-27 21:27:44 +00001426 OS << "switch ";
Ted Kremenek79f0a632008-04-16 21:10:48 +00001427 Terminator->getCond()->printPretty(OS,Helper);
Ted Kremenek65cfa562007-08-27 21:27:44 +00001428 }
1429
Ted Kremenek621e1592007-08-31 21:49:40 +00001430 void VisitConditionalOperator(ConditionalOperator* C) {
1431 C->getCond()->printPretty(OS,Helper);
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001432 OS << " ? ... : ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001433 }
1434
Ted Kremenek2025cc92007-08-31 22:29:13 +00001435 void VisitChooseExpr(ChooseExpr* C) {
1436 OS << "__builtin_choose_expr( ";
1437 C->getCond()->printPretty(OS,Helper);
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001438 OS << " )";
Ted Kremenek2025cc92007-08-31 22:29:13 +00001439 }
1440
Ted Kremenek86afc042007-08-31 22:26:13 +00001441 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1442 OS << "goto *";
1443 I->getTarget()->printPretty(OS,Helper);
Ted Kremenek86afc042007-08-31 22:26:13 +00001444 }
1445
Ted Kremenek621e1592007-08-31 21:49:40 +00001446 void VisitBinaryOperator(BinaryOperator* B) {
1447 if (!B->isLogicalOp()) {
1448 VisitExpr(B);
1449 return;
1450 }
1451
1452 B->getLHS()->printPretty(OS,Helper);
1453
1454 switch (B->getOpcode()) {
1455 case BinaryOperator::LOr:
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001456 OS << " || ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001457 return;
1458 case BinaryOperator::LAnd:
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001459 OS << " && ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001460 return;
1461 default:
1462 assert(false && "Invalid logical operator.");
1463 }
1464 }
1465
Ted Kremenekcfaae762007-08-27 21:54:41 +00001466 void VisitExpr(Expr* E) {
Ted Kremenek08176a52007-08-31 21:30:12 +00001467 E->printPretty(OS,Helper);
Ted Kremenekcfaae762007-08-27 21:54:41 +00001468 }
Ted Kremenek73543912007-08-23 21:42:29 +00001469};
Ted Kremenek08176a52007-08-31 21:30:12 +00001470
1471
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001472void print_stmt(llvm::raw_ostream&OS, StmtPrinterHelper* Helper, Stmt* Terminator) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001473 if (Helper) {
1474 // special printing for statement-expressions.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001475 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001476 CompoundStmt* Sub = SE->getSubStmt();
1477
1478 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek16e3b9a2007-08-31 22:47:06 +00001479 OS << "({ ... ; ";
Ted Kremenek256a2592007-10-29 20:41:04 +00001480 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek16e3b9a2007-08-31 22:47:06 +00001481 OS << " })\n";
Ted Kremenek86afc042007-08-31 22:26:13 +00001482 return;
1483 }
1484 }
1485
1486 // special printing for comma expressions.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001487 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001488 if (B->getOpcode() == BinaryOperator::Comma) {
1489 OS << "... , ";
1490 Helper->handledStmt(B->getRHS(),OS);
1491 OS << '\n';
1492 return;
1493 }
1494 }
1495 }
1496
Ted Kremenek79f0a632008-04-16 21:10:48 +00001497 Terminator->printPretty(OS, Helper);
Ted Kremenek86afc042007-08-31 22:26:13 +00001498
1499 // Expressions need a newline.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001500 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek86afc042007-08-31 22:26:13 +00001501}
1502
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001503void print_block(llvm::raw_ostream& OS, const CFG* cfg, const CFGBlock& B,
Ted Kremenek08176a52007-08-31 21:30:12 +00001504 StmtPrinterHelper* Helper, bool print_edges) {
1505
1506 if (Helper) Helper->setBlockID(B.getBlockID());
1507
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001508 // Print the header.
Ted Kremenek08176a52007-08-31 21:30:12 +00001509 OS << "\n [ B" << B.getBlockID();
1510
1511 if (&B == &cfg->getEntry())
1512 OS << " (ENTRY) ]\n";
1513 else if (&B == &cfg->getExit())
1514 OS << " (EXIT) ]\n";
1515 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001516 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek08176a52007-08-31 21:30:12 +00001517 else
1518 OS << " ]\n";
1519
Ted Kremenekec055e12007-08-29 23:20:49 +00001520 // Print the label of this block.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001521 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek08176a52007-08-31 21:30:12 +00001522
1523 if (print_edges)
1524 OS << " ";
1525
Ted Kremenek79f0a632008-04-16 21:10:48 +00001526 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenekec055e12007-08-29 23:20:49 +00001527 OS << L->getName();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001528 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenekec055e12007-08-29 23:20:49 +00001529 OS << "case ";
1530 C->getLHS()->printPretty(OS);
1531 if (C->getRHS()) {
1532 OS << " ... ";
1533 C->getRHS()->printPretty(OS);
1534 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001535 }
Ted Kremenek79f0a632008-04-16 21:10:48 +00001536 else if (isa<DefaultStmt>(Terminator))
Ted Kremenekec055e12007-08-29 23:20:49 +00001537 OS << "default";
Ted Kremenek08176a52007-08-31 21:30:12 +00001538 else
1539 assert(false && "Invalid label statement in CFGBlock.");
1540
Ted Kremenekec055e12007-08-29 23:20:49 +00001541 OS << ":\n";
1542 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001543
Ted Kremenek97f75312007-08-21 21:42:03 +00001544 // Iterate through the statements in the block and print them.
Ted Kremenek97f75312007-08-21 21:42:03 +00001545 unsigned j = 1;
Ted Kremenek08176a52007-08-31 21:30:12 +00001546
1547 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1548 I != E ; ++I, ++j ) {
1549
Ted Kremenekec055e12007-08-29 23:20:49 +00001550 // Print the statement # in the basic block and the statement itself.
Ted Kremenek08176a52007-08-31 21:30:12 +00001551 if (print_edges)
1552 OS << " ";
1553
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001554 OS << llvm::format("%3d", j) << ": ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001555
1556 if (Helper)
1557 Helper->setStmtID(j);
Ted Kremenek86afc042007-08-31 22:26:13 +00001558
1559 print_stmt(OS,Helper,*I);
Ted Kremenek97f75312007-08-21 21:42:03 +00001560 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001561
Ted Kremenekec055e12007-08-29 23:20:49 +00001562 // Print the terminator of this block.
Ted Kremenek08176a52007-08-31 21:30:12 +00001563 if (B.getTerminator()) {
1564 if (print_edges)
1565 OS << " ";
1566
Ted Kremenekec055e12007-08-29 23:20:49 +00001567 OS << " T: ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001568
1569 if (Helper) Helper->setBlockID(-1);
1570
1571 CFGBlockTerminatorPrint TPrinter(OS,Helper);
1572 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001573 OS << '\n';
Ted Kremenek97f75312007-08-21 21:42:03 +00001574 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001575
Ted Kremenekec055e12007-08-29 23:20:49 +00001576 if (print_edges) {
1577 // Print the predecessors of this block.
Ted Kremenek08176a52007-08-31 21:30:12 +00001578 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenekec055e12007-08-29 23:20:49 +00001579 unsigned i = 0;
Ted Kremenekec055e12007-08-29 23:20:49 +00001580
Ted Kremenek08176a52007-08-31 21:30:12 +00001581 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1582 I != E; ++I, ++i) {
1583
1584 if (i == 8 || (i-8) == 0)
1585 OS << "\n ";
1586
Ted Kremenekec055e12007-08-29 23:20:49 +00001587 OS << " B" << (*I)->getBlockID();
1588 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001589
1590 OS << '\n';
1591
1592 // Print the successors of this block.
1593 OS << " Successors (" << B.succ_size() << "):";
1594 i = 0;
1595
1596 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1597 I != E; ++I, ++i) {
1598
1599 if (i == 8 || (i-8) % 10 == 0)
1600 OS << "\n ";
1601
1602 OS << " B" << (*I)->getBlockID();
1603 }
1604
Ted Kremenekec055e12007-08-29 23:20:49 +00001605 OS << '\n';
Ted Kremenek97f75312007-08-21 21:42:03 +00001606 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001607}
1608
1609} // end anonymous namespace
1610
1611/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001612void CFG::dump() const { print(llvm::errs()); }
Ted Kremenek08176a52007-08-31 21:30:12 +00001613
1614/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001615void CFG::print(llvm::raw_ostream& OS) const {
Ted Kremenek08176a52007-08-31 21:30:12 +00001616
1617 StmtPrinterHelper Helper(this);
1618
1619 // Print the entry block.
1620 print_block(OS, this, getEntry(), &Helper, true);
1621
1622 // Iterate through the CFGBlocks and print them one by one.
1623 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1624 // Skip the entry block, because we already printed it.
1625 if (&(*I) == &getEntry() || &(*I) == &getExit())
1626 continue;
1627
1628 print_block(OS, this, *I, &Helper, true);
1629 }
1630
1631 // Print the exit block.
1632 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd19e99e2008-11-24 20:50:24 +00001633 OS.flush();
Ted Kremenek08176a52007-08-31 21:30:12 +00001634}
1635
1636/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001637void CFGBlock::dump(const CFG* cfg) const { print(llvm::errs(), cfg); }
Ted Kremenek08176a52007-08-31 21:30:12 +00001638
1639/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1640/// Generally this will only be called from CFG::print.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001641void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg) const {
Ted Kremenek08176a52007-08-31 21:30:12 +00001642 StmtPrinterHelper Helper(cfg);
1643 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek4db5b452007-08-23 16:51:22 +00001644}
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001645
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001646/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001647void CFGBlock::printTerminator(llvm::raw_ostream& OS) const {
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001648 CFGBlockTerminatorPrint TPrinter(OS,NULL);
1649 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1650}
1651
Ted Kremenek16516e22008-11-12 21:11:49 +00001652Stmt* CFGBlock::getTerminatorCondition() {
Ted Kremenek79f0a632008-04-16 21:10:48 +00001653
1654 if (!Terminator)
1655 return NULL;
1656
1657 Expr* E = NULL;
1658
1659 switch (Terminator->getStmtClass()) {
1660 default:
1661 break;
1662
1663 case Stmt::ForStmtClass:
1664 E = cast<ForStmt>(Terminator)->getCond();
1665 break;
1666
1667 case Stmt::WhileStmtClass:
1668 E = cast<WhileStmt>(Terminator)->getCond();
1669 break;
1670
1671 case Stmt::DoStmtClass:
1672 E = cast<DoStmt>(Terminator)->getCond();
1673 break;
1674
1675 case Stmt::IfStmtClass:
1676 E = cast<IfStmt>(Terminator)->getCond();
1677 break;
1678
1679 case Stmt::ChooseExprClass:
1680 E = cast<ChooseExpr>(Terminator)->getCond();
1681 break;
1682
1683 case Stmt::IndirectGotoStmtClass:
1684 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1685 break;
1686
1687 case Stmt::SwitchStmtClass:
1688 E = cast<SwitchStmt>(Terminator)->getCond();
1689 break;
1690
1691 case Stmt::ConditionalOperatorClass:
1692 E = cast<ConditionalOperator>(Terminator)->getCond();
1693 break;
1694
1695 case Stmt::BinaryOperatorClass: // '&&' and '||'
1696 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek16516e22008-11-12 21:11:49 +00001697 break;
1698
1699 case Stmt::ObjCForCollectionStmtClass:
1700 return Terminator;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001701 }
1702
1703 return E ? E->IgnoreParens() : NULL;
1704}
1705
Ted Kremenekbdbd1b52008-05-16 16:06:00 +00001706bool CFGBlock::hasBinaryBranchTerminator() const {
1707
1708 if (!Terminator)
1709 return false;
1710
1711 Expr* E = NULL;
1712
1713 switch (Terminator->getStmtClass()) {
1714 default:
1715 return false;
1716
1717 case Stmt::ForStmtClass:
1718 case Stmt::WhileStmtClass:
1719 case Stmt::DoStmtClass:
1720 case Stmt::IfStmtClass:
1721 case Stmt::ChooseExprClass:
1722 case Stmt::ConditionalOperatorClass:
1723 case Stmt::BinaryOperatorClass:
1724 return true;
1725 }
1726
1727 return E ? E->IgnoreParens() : NULL;
1728}
1729
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001730
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001731//===----------------------------------------------------------------------===//
1732// CFG Graphviz Visualization
1733//===----------------------------------------------------------------------===//
1734
Ted Kremenek08176a52007-08-31 21:30:12 +00001735
1736#ifndef NDEBUG
Chris Lattner26002172007-09-17 06:16:32 +00001737static StmtPrinterHelper* GraphHelper;
Ted Kremenek08176a52007-08-31 21:30:12 +00001738#endif
1739
1740void CFG::viewCFG() const {
1741#ifndef NDEBUG
1742 StmtPrinterHelper H(this);
1743 GraphHelper = &H;
1744 llvm::ViewGraph(this,"CFG");
1745 GraphHelper = NULL;
Ted Kremenek08176a52007-08-31 21:30:12 +00001746#endif
1747}
1748
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001749namespace llvm {
1750template<>
1751struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
1752 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
1753
Hartmut Kaiser752a0052007-09-16 00:28:28 +00001754#ifndef NDEBUG
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001755 std::string OutSStr;
1756 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek08176a52007-08-31 21:30:12 +00001757 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001758 std::string& OutStr = Out.str();
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001759
1760 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1761
1762 // Process string output to make it nicer...
1763 for (unsigned i = 0; i != OutStr.length(); ++i)
1764 if (OutStr[i] == '\n') { // Left justify
1765 OutStr[i] = '\\';
1766 OutStr.insert(OutStr.begin()+i+1, 'l');
1767 }
1768
1769 return OutStr;
Hartmut Kaiser752a0052007-09-16 00:28:28 +00001770#else
1771 return "";
1772#endif
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001773 }
1774};
1775} // end namespace llvm