blob: 82336a44e08ebfad1538f3413860e0eb102d4ac0 [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
157static VariableArrayType* FindVA(Type* t) {
158 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
159 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
160 if (vat->getSizeExpr())
161 return vat;
162
163 t = vt->getElementType().getTypePtr();
164 }
165
166 return 0;
167}
Ted Kremenek73543912007-08-23 21:42:29 +0000168
169/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
170/// represent an arbitrary statement. Examples include a single expression
171/// or a function body (compound statement). The ownership of the returned
172/// CFG is transferred to the caller. If CFG construction fails, this method
173/// returns NULL.
174CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000175 assert (cfg);
Ted Kremenek73543912007-08-23 21:42:29 +0000176 if (!Statement) return NULL;
177
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000178 badCFG = false;
179
Ted Kremenek73543912007-08-23 21:42:29 +0000180 // Create an empty block that will serve as the exit block for the CFG.
181 // Since this is the first block added to the CFG, it will be implicitly
182 // registered as the exit block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000183 Succ = createBlock();
184 assert (Succ == &cfg->getExit());
185 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenek73543912007-08-23 21:42:29 +0000186
187 // Visit the statements and create the CFG.
Ted Kremenekfa38c7a2008-02-27 17:33:02 +0000188 CFGBlock* B = Visit(Statement);
189 if (!B) B = Succ;
190
191 if (B) {
Ted Kremenek73543912007-08-23 21:42:29 +0000192 // Finalize the last constructed block. This usually involves
193 // reversing the order of the statements in the block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000194 if (Block) FinishBlock(B);
Ted Kremenek73543912007-08-23 21:42:29 +0000195
196 // Backpatch the gotos whose label -> block mappings we didn't know
197 // when we encountered them.
198 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
199 E = BackpatchBlocks.end(); I != E; ++I ) {
200
201 CFGBlock* B = *I;
202 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
203 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
204
205 // If there is no target for the goto, then we are looking at an
206 // incomplete AST. Handle this by not registering a successor.
207 if (LI == LabelMap.end()) continue;
208
209 B->addSuccessor(LI->second);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000210 }
Ted Kremenek73543912007-08-23 21:42:29 +0000211
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000212 // Add successors to the Indirect Goto Dispatch block (if we have one).
213 if (CFGBlock* B = cfg->getIndirectGotoBlock())
214 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
215 E = AddressTakenLabels.end(); I != E; ++I ) {
216
217 // Lookup the target block.
218 LabelMapTy::iterator LI = LabelMap.find(*I);
219
220 // If there is no target block that contains label, then we are looking
221 // at an incomplete AST. Handle this by not registering a successor.
222 if (LI == LabelMap.end()) continue;
223
224 B->addSuccessor(LI->second);
225 }
Ted Kremenek680fcb82007-09-26 21:23:31 +0000226
Ted Kremenek844cb4d2007-09-17 16:18:02 +0000227 Succ = B;
Ted Kremenek680fcb82007-09-26 21:23:31 +0000228 }
229
230 // Create an empty entry block that has no predecessors.
231 cfg->setEntry(createBlock());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000232
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000233 if (badCFG) {
234 delete cfg;
235 cfg = NULL;
236 return NULL;
237 }
238
Ted Kremenek680fcb82007-09-26 21:23:31 +0000239 // NULL out cfg so that repeated calls to the builder will fail and that
240 // the ownership of the constructed CFG is passed to the caller.
241 CFG* t = cfg;
242 cfg = NULL;
243 return t;
Ted Kremenek73543912007-08-23 21:42:29 +0000244}
245
246/// createBlock - Used to lazily create blocks that are connected
247/// to the current (global) succcessor.
248CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek14594572007-09-05 20:02:05 +0000249 CFGBlock* B = cfg->createBlock();
Ted Kremenek73543912007-08-23 21:42:29 +0000250 if (add_successor && Succ) B->addSuccessor(Succ);
251 return B;
252}
253
254/// FinishBlock - When the last statement has been added to the block,
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000255/// we must reverse the statements because they have been inserted
256/// in reverse order.
Ted Kremenek73543912007-08-23 21:42:29 +0000257void CFGBuilder::FinishBlock(CFGBlock* B) {
258 assert (B);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000259 B->reverseStmts();
Ted Kremenek73543912007-08-23 21:42:29 +0000260}
261
Ted Kremenek65cfa562007-08-27 21:27:44 +0000262/// addStmt - Used to add statements/expressions to the current CFGBlock
263/// "Block". This method calls WalkAST on the passed statement to see if it
264/// contains any short-circuit expressions. If so, it recursively creates
265/// the necessary blocks for such expressions. It returns the "topmost" block
266/// of the created blocks, or the original value of "Block" when this method
267/// was called if no additional blocks are created.
Ted Kremenek79f0a632008-04-16 21:10:48 +0000268CFGBlock* CFGBuilder::addStmt(Stmt* Terminator) {
Ted Kremenek390b9762007-08-30 18:39:40 +0000269 if (!Block) Block = createBlock();
Ted Kremenek79f0a632008-04-16 21:10:48 +0000270 return WalkAST(Terminator,true);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000271}
272
273/// WalkAST - Used by addStmt to walk the subtree of a statement and
Ted Kremeneke822b622007-08-28 18:14:37 +0000274/// add extra blocks for ternary operators, &&, and ||. We also
275/// process "," and DeclStmts (which may contain nested control-flow).
Ted Kremenek79f0a632008-04-16 21:10:48 +0000276CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) {
277 switch (Terminator->getStmtClass()) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000278 case Stmt::ConditionalOperatorClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000279 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000280
281 // Create the confluence block that will "merge" the results
282 // of the ternary expression.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000283 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
284 ConfluenceBlock->appendStmt(C);
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000285 FinishBlock(ConfluenceBlock);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000286
287 // Create a block for the LHS expression if there is an LHS expression.
288 // A GCC extension allows LHS to be NULL, causing the condition to
289 // be the value that is returned instead.
290 // e.g: x ?: y is shorthand for: x ? x : y;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000291 Succ = ConfluenceBlock;
292 Block = NULL;
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000293 CFGBlock* LHSBlock = NULL;
294 if (C->getLHS()) {
295 LHSBlock = Visit(C->getLHS());
296 FinishBlock(LHSBlock);
297 Block = NULL;
298 }
Ted Kremenek65cfa562007-08-27 21:27:44 +0000299
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000300 // Create the block for the RHS expression.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000301 Succ = ConfluenceBlock;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000302 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekd11620d2007-09-11 21:29:43 +0000303 FinishBlock(RHSBlock);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000304
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000305 // Create the block that will contain the condition.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000306 Block = createBlock(false);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000307
308 if (LHSBlock)
309 Block->addSuccessor(LHSBlock);
310 else {
311 // If we have no LHS expression, add the ConfluenceBlock as a direct
312 // successor for the block containing the condition. Moreover,
313 // we need to reverse the order of the predecessors in the
314 // ConfluenceBlock because the RHSBlock will have been added to
315 // the succcessors already, and we want the first predecessor to the
316 // the block containing the expression for the case when the ternary
317 // expression evaluates to true.
318 Block->addSuccessor(ConfluenceBlock);
319 assert (ConfluenceBlock->pred_size() == 2);
320 std::reverse(ConfluenceBlock->pred_begin(),
321 ConfluenceBlock->pred_end());
322 }
323
Ted Kremenek65cfa562007-08-27 21:27:44 +0000324 Block->addSuccessor(RHSBlock);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000325
Ted Kremenek65cfa562007-08-27 21:27:44 +0000326 Block->setTerminator(C);
327 return addStmt(C->getCond());
328 }
Ted Kremenek7f788422007-08-31 17:03:41 +0000329
330 case Stmt::ChooseExprClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000331 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Ted Kremenek7f788422007-08-31 17:03:41 +0000332
333 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
334 ConfluenceBlock->appendStmt(C);
335 FinishBlock(ConfluenceBlock);
336
337 Succ = ConfluenceBlock;
338 Block = NULL;
339 CFGBlock* LHSBlock = Visit(C->getLHS());
Ted Kremenekd11620d2007-09-11 21:29:43 +0000340 FinishBlock(LHSBlock);
341
Ted Kremenek7f788422007-08-31 17:03:41 +0000342 Succ = ConfluenceBlock;
343 Block = NULL;
344 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekd11620d2007-09-11 21:29:43 +0000345 FinishBlock(RHSBlock);
Ted Kremenek7f788422007-08-31 17:03:41 +0000346
347 Block = createBlock(false);
348 Block->addSuccessor(LHSBlock);
349 Block->addSuccessor(RHSBlock);
350 Block->setTerminator(C);
351 return addStmt(C->getCond());
352 }
Ted Kremenek666a6af2007-08-28 16:18:58 +0000353
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000354 case Stmt::DeclStmtClass: {
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000355 DeclStmt *DS = cast<DeclStmt>(Terminator);
356 if (DS->hasSolitaryDecl()) {
Ted Kremenek0865a992008-08-06 23:20:50 +0000357 Block->appendStmt(Terminator);
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000358 return WalkAST_VisitDeclSubExpr(DS->getSolitaryDecl());
Ted Kremenek0865a992008-08-06 23:20:50 +0000359 }
360 else {
361 typedef llvm::SmallVector<ScopedDecl*,10> BufTy;
362 BufTy Buf;
363 CFGBlock* B = 0;
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000364
365 // FIXME: Add a reverse iterator for DeclStmt to avoid this
366 // extra copy.
367 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
368 DI != DE; ++DI)
369 Buf.push_back(*DI);
370
Ted Kremenek0865a992008-08-06 23:20:50 +0000371 for (BufTy::reverse_iterator I=Buf.rbegin(), E=Buf.rend(); I!=E; ++I) {
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000372 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
373 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
374 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Ted Kremenek0865a992008-08-06 23:20:50 +0000375
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000376 // Allocate the DeclStmt using the BumpPtrAllocator. It will
377 // get automatically freed with the CFG. Note that even though
378 // we are using a DeclGroupOwningRef that wraps a singe Decl*,
379 // that Decl* will not get deallocated because the destroy method
380 // of DG is never called.
381 DeclGroupOwningRef DG(*I);
382 ScopedDecl* D = *I;
383 void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
384
385 DeclStmt* DS = new (Mem) DeclStmt(DG, D->getLocation(),
386 GetEndLoc(D));
387
Ted Kremenek0865a992008-08-06 23:20:50 +0000388 // Append the fake DeclStmt to block.
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000389 Block->appendStmt(DS);
390 B = WalkAST_VisitDeclSubExpr(D);
Ted Kremenek0865a992008-08-06 23:20:50 +0000391 }
392 return B;
393 }
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000394 }
Ted Kremenek0865a992008-08-06 23:20:50 +0000395
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000396 case Stmt::AddrLabelExprClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000397 AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000398 AddressTakenLabels.insert(A->getLabel());
399
Ted Kremenek79f0a632008-04-16 21:10:48 +0000400 if (AlwaysAddStmt) Block->appendStmt(Terminator);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000401 return Block;
402 }
Ted Kremenekd11620d2007-09-11 21:29:43 +0000403
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000404 case Stmt::StmtExprClass:
Ted Kremenek79f0a632008-04-16 21:10:48 +0000405 return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
Ted Kremeneke822b622007-08-28 18:14:37 +0000406
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000407 case Stmt::SizeOfAlignOfExprClass: {
408 SizeOfAlignOfExpr* E = cast<SizeOfAlignOfExpr>(Terminator);
Ted Kremenek09535672008-09-26 22:58:57 +0000409
410 // VLA types have expressions that must be evaluated.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000411 if (E->isArgumentType()) {
412 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
413 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
414 addStmt(VA->getSizeExpr());
415 }
416 // Expressions in sizeof/alignof are not evaluated and thus have no
417 // control flow.
418 else
419 Block->appendStmt(Terminator);
Ted Kremenek09535672008-09-26 22:58:57 +0000420
421 return Block;
422 }
423
Ted Kremenekcfaae762007-08-27 21:54:41 +0000424 case Stmt::BinaryOperatorClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000425 BinaryOperator* B = cast<BinaryOperator>(Terminator);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000426
427 if (B->isLogicalOp()) { // && or ||
428 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
429 ConfluenceBlock->appendStmt(B);
430 FinishBlock(ConfluenceBlock);
431
432 // create the block evaluating the LHS
433 CFGBlock* LHSBlock = createBlock(false);
Ted Kremenekb2348522007-12-21 19:49:00 +0000434 LHSBlock->setTerminator(B);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000435
436 // create the block evaluating the RHS
437 Succ = ConfluenceBlock;
438 Block = NULL;
439 CFGBlock* RHSBlock = Visit(B->getRHS());
Zhongxing Xu7636e912008-10-04 05:48:38 +0000440 FinishBlock(RHSBlock);
Ted Kremenekb2348522007-12-21 19:49:00 +0000441
442 // Now link the LHSBlock with RHSBlock.
443 if (B->getOpcode() == BinaryOperator::LOr) {
444 LHSBlock->addSuccessor(ConfluenceBlock);
445 LHSBlock->addSuccessor(RHSBlock);
446 }
447 else {
448 assert (B->getOpcode() == BinaryOperator::LAnd);
449 LHSBlock->addSuccessor(RHSBlock);
450 LHSBlock->addSuccessor(ConfluenceBlock);
451 }
Ted Kremenekcfaae762007-08-27 21:54:41 +0000452
453 // Generate the blocks for evaluating the LHS.
454 Block = LHSBlock;
455 return addStmt(B->getLHS());
Ted Kremeneke822b622007-08-28 18:14:37 +0000456 }
457 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
458 Block->appendStmt(B);
459 addStmt(B->getRHS());
460 return addStmt(B->getLHS());
Ted Kremenek3a819822007-10-01 19:33:33 +0000461 }
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000462
463 break;
Ted Kremenekcfaae762007-08-27 21:54:41 +0000464 }
Ted Kremenekd68a8d32008-09-26 18:17:07 +0000465
466 // Blocks: No support for blocks ... yet
467 case Stmt::BlockExprClass:
468 case Stmt::BlockDeclRefExprClass:
469 return NYS();
Ted Kremeneka9ba5cc2008-02-26 02:37:08 +0000470
471 case Stmt::ParenExprClass:
Ted Kremenek79f0a632008-04-16 21:10:48 +0000472 return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000473
Ted Kremenek65cfa562007-08-27 21:27:44 +0000474 default:
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000475 break;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000476 };
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000477
Ted Kremenek79f0a632008-04-16 21:10:48 +0000478 if (AlwaysAddStmt) Block->appendStmt(Terminator);
479 return WalkAST_VisitChildren(Terminator);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000480}
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000481
Ted Kremenek0865a992008-08-06 23:20:50 +0000482/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions
483/// for initializers in Decls.
484CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(ScopedDecl* D) {
485 VarDecl* VD = dyn_cast<VarDecl>(D);
486
487 if (!VD)
Ted Kremenekf4e35622007-11-18 20:06:01 +0000488 return Block;
489
Ted Kremenek0865a992008-08-06 23:20:50 +0000490 Expr* Init = VD->getInit();
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000491
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000492 if (Init) {
493 // Optimization: Don't create separate block-level statements for literals.
494 switch (Init->getStmtClass()) {
495 case Stmt::IntegerLiteralClass:
496 case Stmt::CharacterLiteralClass:
497 case Stmt::StringLiteralClass:
498 break;
499 default:
500 Block = addStmt(Init);
501 }
Ted Kremenek4ad64e82008-02-29 22:32:24 +0000502 }
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000503
504 // If the type of VD is a VLA, then we must process its size expressions.
505 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
506 VA = FindVA(VA->getElementType().getTypePtr()))
507 Block = addStmt(VA->getSizeExpr());
Ted Kremenek4ad64e82008-02-29 22:32:24 +0000508
Ted Kremeneke822b622007-08-28 18:14:37 +0000509 return Block;
510}
511
Ted Kremenek65cfa562007-08-27 21:27:44 +0000512/// WalkAST_VisitChildren - Utility method to call WalkAST on the
513/// children of a Stmt.
Ted Kremenek79f0a632008-04-16 21:10:48 +0000514CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000515 CFGBlock* B = Block;
Ted Kremenek79f0a632008-04-16 21:10:48 +0000516 for (Stmt::child_iterator I = Terminator->child_begin(), E = Terminator->child_end() ;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000517 I != E; ++I)
Ted Kremenek680fcb82007-09-26 21:23:31 +0000518 if (*I) B = WalkAST(*I);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000519
520 return B;
521}
522
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000523/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
524/// expressions (a GCC extension).
Ted Kremenek79f0a632008-04-16 21:10:48 +0000525CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
526 Block->appendStmt(Terminator);
527 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000528}
529
Ted Kremenek73543912007-08-23 21:42:29 +0000530/// VisitStmt - Handle statements with no branching control flow.
531CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
532 // We cannot assume that we are in the middle of a basic block, since
533 // the CFG might only be constructed for this single statement. If
534 // we have no current basic block, just create one lazily.
535 if (!Block) Block = createBlock();
536
537 // Simply add the statement to the current block. We actually
538 // insert statements in reverse order; this order is reversed later
539 // when processing the containing element in the AST.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000540 addStmt(Statement);
541
Ted Kremenek73543912007-08-23 21:42:29 +0000542 return Block;
543}
544
545CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
546 return Block;
547}
548
549CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000550
551 CFGBlock* LastBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000552
Ted Kremenekfeb0e992008-02-26 00:22:58 +0000553 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
554 I != E; ++I ) {
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000555 LastBlock = Visit(*I);
Ted Kremenekfeb0e992008-02-26 00:22:58 +0000556 }
557
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000558 return LastBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000559}
560
561CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
562 // We may see an if statement in the middle of a basic block, or
563 // it may be the first statement we are processing. In either case,
564 // we create a new basic block. First, we create the blocks for
565 // the then...else statements, and then we create the block containing
566 // the if statement. If we were in the middle of a block, we
567 // stop processing that block and reverse its statements. That block
568 // is then the implicit successor for the "then" and "else" clauses.
569
570 // The block we were proccessing is now finished. Make it the
571 // successor block.
572 if (Block) {
573 Succ = Block;
574 FinishBlock(Block);
575 }
576
577 // Process the false branch. NULL out Block so that the recursive
578 // call to Visit will create a new basic block.
579 // Null out Block so that all successor
580 CFGBlock* ElseBlock = Succ;
581
582 if (Stmt* Else = I->getElse()) {
583 SaveAndRestore<CFGBlock*> sv(Succ);
584
585 // NULL out Block so that the recursive call to Visit will
586 // create a new basic block.
587 Block = NULL;
Ted Kremenek44db7872007-08-30 18:13:31 +0000588 ElseBlock = Visit(Else);
589
590 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
591 ElseBlock = sv.get();
592 else if (Block)
593 FinishBlock(ElseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000594 }
595
596 // Process the true branch. NULL out Block so that the recursive
597 // call to Visit will create a new basic block.
598 // Null out Block so that all successor
599 CFGBlock* ThenBlock;
600 {
601 Stmt* Then = I->getThen();
602 assert (Then);
603 SaveAndRestore<CFGBlock*> sv(Succ);
604 Block = NULL;
Ted Kremenek44db7872007-08-30 18:13:31 +0000605 ThenBlock = Visit(Then);
606
607 if (!ThenBlock) // Can occur when the Then body has all NullStmts.
608 ThenBlock = sv.get();
609 else if (Block)
610 FinishBlock(ThenBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000611 }
612
613 // Now create a new block containing the if statement.
614 Block = createBlock(false);
Ted Kremenek73543912007-08-23 21:42:29 +0000615
616 // Set the terminator of the new block to the If statement.
617 Block->setTerminator(I);
618
619 // Now add the successors.
620 Block->addSuccessor(ThenBlock);
621 Block->addSuccessor(ElseBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000622
623 // Add the condition as the last statement in the new block. This
624 // may create new blocks as the condition may contain control-flow. Any
625 // newly created blocks will be pointed to be "Block".
Ted Kremenek1eaa6712008-01-30 23:02:42 +0000626 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenek73543912007-08-23 21:42:29 +0000627}
Ted Kremenekd11620d2007-09-11 21:29:43 +0000628
Ted Kremenek73543912007-08-23 21:42:29 +0000629
630CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
631 // If we were in the middle of a block we stop processing that block
632 // and reverse its statements.
633 //
634 // NOTE: If a "return" appears in the middle of a block, this means
635 // that the code afterwards is DEAD (unreachable). We still
636 // keep a basic block for that code; a simple "mark-and-sweep"
637 // from the entry block will be able to report such dead
638 // blocks.
639 if (Block) FinishBlock(Block);
640
641 // Create the new block.
642 Block = createBlock(false);
643
644 // The Exit block is the only successor.
645 Block->addSuccessor(&cfg->getExit());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000646
647 // Add the return statement to the block. This may create new blocks
648 // if R contains control-flow (short-circuit operations).
649 return addStmt(R);
Ted Kremenek73543912007-08-23 21:42:29 +0000650}
651
652CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
653 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek82e8a192008-03-15 07:45:02 +0000654 Visit(L->getSubStmt());
655 CFGBlock* LabelBlock = Block;
Ted Kremenek9b0d1b62007-08-30 18:20:57 +0000656
657 if (!LabelBlock) // This can happen when the body is empty, i.e.
658 LabelBlock=createBlock(); // scopes that only contains NullStmts.
659
Ted Kremenek73543912007-08-23 21:42:29 +0000660 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
661 LabelMap[ L ] = LabelBlock;
662
663 // Labels partition blocks, so this is the end of the basic block
Ted Kremenekec055e12007-08-29 23:20:49 +0000664 // we were processing (L is the block's label). Because this is
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000665 // label (and we have already processed the substatement) there is no
666 // extra control-flow to worry about.
Ted Kremenekec055e12007-08-29 23:20:49 +0000667 LabelBlock->setLabel(L);
Ted Kremenek73543912007-08-23 21:42:29 +0000668 FinishBlock(LabelBlock);
669
670 // We set Block to NULL to allow lazy creation of a new block
671 // (if necessary);
672 Block = NULL;
673
674 // This block is now the implicit successor of other blocks.
675 Succ = LabelBlock;
676
677 return LabelBlock;
678}
679
680CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
681 // Goto is a control-flow statement. Thus we stop processing the
682 // current block and create a new one.
683 if (Block) FinishBlock(Block);
684 Block = createBlock(false);
685 Block->setTerminator(G);
686
687 // If we already know the mapping to the label block add the
688 // successor now.
689 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
690
691 if (I == LabelMap.end())
692 // We will need to backpatch this block later.
693 BackpatchBlocks.push_back(Block);
694 else
695 Block->addSuccessor(I->second);
696
697 return Block;
698}
699
700CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
701 // "for" is a control-flow statement. Thus we stop processing the
702 // current block.
703
704 CFGBlock* LoopSuccessor = NULL;
705
706 if (Block) {
707 FinishBlock(Block);
708 LoopSuccessor = Block;
709 }
710 else LoopSuccessor = Succ;
711
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000712 // Because of short-circuit evaluation, the condition of the loop
713 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
714 // blocks that evaluate the condition.
715 CFGBlock* ExitConditionBlock = createBlock(false);
716 CFGBlock* EntryConditionBlock = ExitConditionBlock;
717
718 // Set the terminator for the "exit" condition block.
719 ExitConditionBlock->setTerminator(F);
720
721 // Now add the actual condition to the condition block. Because the
722 // condition itself may contain control-flow, new blocks may be created.
723 if (Stmt* C = F->getCond()) {
724 Block = ExitConditionBlock;
725 EntryConditionBlock = addStmt(C);
726 if (Block) FinishBlock(EntryConditionBlock);
727 }
Ted Kremenek73543912007-08-23 21:42:29 +0000728
729 // The condition block is the implicit successor for the loop body as
730 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000731 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000732
733 // Now create the loop body.
734 {
735 assert (F->getBody());
736
737 // Save the current values for Block, Succ, and continue and break targets
738 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
739 save_continue(ContinueTargetBlock),
740 save_break(BreakTargetBlock);
Ted Kremenek77f93372008-09-04 21:48:47 +0000741
Ted Kremenek390b9762007-08-30 18:39:40 +0000742 // Create a new block to contain the (bottom) of the loop body.
743 Block = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000744
Ted Kremenek77f93372008-09-04 21:48:47 +0000745 if (Stmt* I = F->getInc()) {
746 // Generate increment code in its own basic block. This is the target
747 // of continue statements.
Ted Kremenekd19e99e2008-11-24 20:50:24 +0000748 Succ = Visit(I);
749
750 // Finish up the increment block if it hasn't been already.
751 if (Block) {
752 assert (Block == Succ);
753 FinishBlock(Block);
754 Block = 0;
755 }
756
Ted Kremenek77f93372008-09-04 21:48:47 +0000757 ContinueTargetBlock = Succ;
758 }
759 else {
760 // No increment code. Continues should go the the entry condition block.
761 ContinueTargetBlock = EntryConditionBlock;
762 }
763
764 // All breaks should go to the code following the loop.
765 BreakTargetBlock = LoopSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +0000766
767 // Now populate the body block, and in the process create new blocks
768 // as we walk the body of the loop.
769 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenek390b9762007-08-30 18:39:40 +0000770
771 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +0000772 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek390b9762007-08-30 18:39:40 +0000773 else if (Block)
774 FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000775
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000776 // This new body block is a successor to our "exit" condition block.
777 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000778 }
779
780 // Link up the condition block with the code that follows the loop.
781 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000782 ExitConditionBlock->addSuccessor(LoopSuccessor);
783
Ted Kremenek73543912007-08-23 21:42:29 +0000784 // If the loop contains initialization, create a new block for those
785 // statements. This block can also contain statements that precede
786 // the loop.
787 if (Stmt* I = F->getInit()) {
788 Block = createBlock();
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000789 return addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000790 }
791 else {
792 // There is no loop initialization. We are thus basically a while
793 // loop. NULL out Block to force lazy block construction.
794 Block = NULL;
Ted Kremenek9ff572c2008-02-27 07:20:00 +0000795 Succ = EntryConditionBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000796 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000797 }
798}
799
Ted Kremenek05335162008-11-11 17:10:00 +0000800CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
801 // Objective-C fast enumeration 'for' statements:
802 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
803 //
804 // for ( Type newVariable in collection_expression ) { statements }
805 //
806 // becomes:
807 //
808 // prologue:
809 // 1. collection_expression
810 // T. jump to loop_entry
811 // loop_entry:
Ted Kremenek65514842008-11-14 01:57:41 +0000812 // 1. side-effects of element expression
Ted Kremenek05335162008-11-11 17:10:00 +0000813 // 1. ObjCForCollectionStmt [performs binding to newVariable]
814 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
815 // TB:
816 // statements
817 // T. jump to loop_entry
818 // FB:
819 // what comes after
820 //
821 // and
822 //
823 // Type existingItem;
824 // for ( existingItem in expression ) { statements }
825 //
826 // becomes:
827 //
828 // the same with newVariable replaced with existingItem; the binding
829 // works the same except that for one ObjCForCollectionStmt::getElement()
830 // returns a DeclStmt and the other returns a DeclRefExpr.
831 //
832
833 CFGBlock* LoopSuccessor = 0;
834
835 if (Block) {
836 FinishBlock(Block);
837 LoopSuccessor = Block;
838 Block = 0;
839 }
840 else LoopSuccessor = Succ;
841
Ted Kremenek65514842008-11-14 01:57:41 +0000842 // Build the condition blocks.
843 CFGBlock* ExitConditionBlock = createBlock(false);
844 CFGBlock* EntryConditionBlock = ExitConditionBlock;
845
846 // Set the terminator for the "exit" condition block.
847 ExitConditionBlock->setTerminator(S);
848
849 // The last statement in the block should be the ObjCForCollectionStmt,
850 // which performs the actual binding to 'element' and determines if there
851 // are any more items in the collection.
852 ExitConditionBlock->appendStmt(S);
853 Block = ExitConditionBlock;
854
855 // Walk the 'element' expression to see if there are any side-effects. We
856 // generate new blocks as necesary. We DON'T add the statement by default
857 // to the CFG unless it contains control-flow.
858 EntryConditionBlock = WalkAST(S->getElement(), false);
859 if (Block) { FinishBlock(EntryConditionBlock); Block = 0; }
860
861 // The condition block is the implicit successor for the loop body as
862 // well as any code above the loop.
863 Succ = EntryConditionBlock;
Ted Kremenek05335162008-11-11 17:10:00 +0000864
865 // Now create the true branch.
Ted Kremenek65514842008-11-14 01:57:41 +0000866 {
867 // Save the current values for Succ, continue and break targets.
868 SaveAndRestore<CFGBlock*> save_Succ(Succ),
869 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
870
871 BreakTargetBlock = LoopSuccessor;
872 ContinueTargetBlock = EntryConditionBlock;
873
874 CFGBlock* BodyBlock = Visit(S->getBody());
875
876 if (!BodyBlock)
877 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
878 else if (Block)
879 FinishBlock(BodyBlock);
880
881 // This new body block is a successor to our "exit" condition block.
882 ExitConditionBlock->addSuccessor(BodyBlock);
883 }
Ted Kremenekf5383072008-11-13 06:36:45 +0000884
Ted Kremenek65514842008-11-14 01:57:41 +0000885 // Link up the condition block with the code that follows the loop.
886 // (the false branch).
887 ExitConditionBlock->addSuccessor(LoopSuccessor);
888
Ted Kremenek05335162008-11-11 17:10:00 +0000889 // Now create a prologue block to contain the collection expression.
Ted Kremenek65514842008-11-14 01:57:41 +0000890 Block = createBlock();
Ted Kremenek05335162008-11-11 17:10:00 +0000891 return addStmt(S->getCollection());
892}
893
894
Ted Kremenek73543912007-08-23 21:42:29 +0000895CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
896 // "while" is a control-flow statement. Thus we stop processing the
897 // current block.
898
899 CFGBlock* LoopSuccessor = NULL;
900
901 if (Block) {
902 FinishBlock(Block);
903 LoopSuccessor = Block;
904 }
905 else LoopSuccessor = Succ;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000906
907 // Because of short-circuit evaluation, the condition of the loop
908 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
909 // blocks that evaluate the condition.
910 CFGBlock* ExitConditionBlock = createBlock(false);
911 CFGBlock* EntryConditionBlock = ExitConditionBlock;
912
913 // Set the terminator for the "exit" condition block.
914 ExitConditionBlock->setTerminator(W);
915
916 // Now add the actual condition to the condition block. Because the
917 // condition itself may contain control-flow, new blocks may be created.
918 // Thus we update "Succ" after adding the condition.
919 if (Stmt* C = W->getCond()) {
920 Block = ExitConditionBlock;
921 EntryConditionBlock = addStmt(C);
Ted Kremenekd0c87602008-02-27 00:28:17 +0000922 assert (Block == EntryConditionBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000923 if (Block) FinishBlock(EntryConditionBlock);
924 }
Ted Kremenek73543912007-08-23 21:42:29 +0000925
926 // The condition block is the implicit successor for the loop body as
927 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000928 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000929
930 // Process the loop body.
931 {
932 assert (W->getBody());
933
934 // Save the current values for Block, Succ, and continue and break targets
935 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
936 save_continue(ContinueTargetBlock),
937 save_break(BreakTargetBlock);
938
939 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000940 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000941
942 // All breaks should go to the code following the loop.
943 BreakTargetBlock = LoopSuccessor;
944
945 // NULL out Block to force lazy instantiation of blocks for the body.
946 Block = NULL;
947
948 // Create the body. The returned block is the entry to the loop body.
949 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenek390b9762007-08-30 18:39:40 +0000950
951 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +0000952 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek390b9762007-08-30 18:39:40 +0000953 else if (Block)
954 FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000955
956 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000957 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000958 }
959
960 // Link up the condition block with the code that follows the loop.
961 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000962 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000963
964 // There can be no more statements in the condition block
965 // since we loop back to this block. NULL out Block to force
966 // lazy creation of another block.
967 Block = NULL;
968
969 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek9ff572c2008-02-27 07:20:00 +0000970 Succ = EntryConditionBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000971 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000972}
973
974CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
975 // "do...while" is a control-flow statement. Thus we stop processing the
976 // current block.
977
978 CFGBlock* LoopSuccessor = NULL;
979
980 if (Block) {
981 FinishBlock(Block);
982 LoopSuccessor = Block;
983 }
984 else LoopSuccessor = Succ;
985
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000986 // Because of short-circuit evaluation, the condition of the loop
987 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
988 // blocks that evaluate the condition.
989 CFGBlock* ExitConditionBlock = createBlock(false);
990 CFGBlock* EntryConditionBlock = ExitConditionBlock;
991
992 // Set the terminator for the "exit" condition block.
993 ExitConditionBlock->setTerminator(D);
Ted Kremenek73543912007-08-23 21:42:29 +0000994
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000995 // Now add the actual condition to the condition block. Because the
996 // condition itself may contain control-flow, new blocks may be created.
997 if (Stmt* C = D->getCond()) {
998 Block = ExitConditionBlock;
999 EntryConditionBlock = addStmt(C);
1000 if (Block) FinishBlock(EntryConditionBlock);
1001 }
Ted Kremenek73543912007-08-23 21:42:29 +00001002
Ted Kremenek9ff572c2008-02-27 07:20:00 +00001003 // The condition block is the implicit successor for the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001004 Succ = EntryConditionBlock;
1005
Ted Kremenek73543912007-08-23 21:42:29 +00001006 // Process the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001007 CFGBlock* BodyBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +00001008 {
1009 assert (D->getBody());
1010
1011 // Save the current values for Block, Succ, and continue and break targets
1012 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1013 save_continue(ContinueTargetBlock),
1014 save_break(BreakTargetBlock);
1015
1016 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001017 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001018
1019 // All breaks should go to the code following the loop.
1020 BreakTargetBlock = LoopSuccessor;
1021
1022 // NULL out Block to force lazy instantiation of blocks for the body.
1023 Block = NULL;
1024
1025 // Create the body. The returned block is the entry to the loop body.
1026 BodyBlock = Visit(D->getBody());
Ted Kremenek73543912007-08-23 21:42:29 +00001027
Ted Kremenek390b9762007-08-30 18:39:40 +00001028 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +00001029 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek390b9762007-08-30 18:39:40 +00001030 else if (Block)
1031 FinishBlock(BodyBlock);
1032
Ted Kremenek73543912007-08-23 21:42:29 +00001033 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001034 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +00001035 }
1036
1037 // Link up the condition block with the code that follows the loop.
1038 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001039 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +00001040
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001041 // There can be no more statements in the body block(s)
1042 // since we loop back to the body. NULL out Block to force
Ted Kremenek73543912007-08-23 21:42:29 +00001043 // lazy creation of another block.
1044 Block = NULL;
1045
1046 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek9ff572c2008-02-27 07:20:00 +00001047 Succ = BodyBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001048 return BodyBlock;
1049}
1050
1051CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1052 // "continue" is a control-flow statement. Thus we stop processing the
1053 // current block.
1054 if (Block) FinishBlock(Block);
1055
1056 // Now create a new block that ends with the continue statement.
1057 Block = createBlock(false);
1058 Block->setTerminator(C);
1059
1060 // If there is no target for the continue, then we are looking at an
1061 // incomplete AST. Handle this by not registering a successor.
1062 if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
1063
1064 return Block;
1065}
1066
1067CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
1068 // "break" is a control-flow statement. Thus we stop processing the
1069 // current block.
1070 if (Block) FinishBlock(Block);
1071
1072 // Now create a new block that ends with the continue statement.
1073 Block = createBlock(false);
1074 Block->setTerminator(B);
1075
1076 // If there is no target for the break, then we are looking at an
1077 // incomplete AST. Handle this by not registering a successor.
1078 if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
1079
1080 return Block;
1081}
1082
Ted Kremenek79f0a632008-04-16 21:10:48 +00001083CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek73543912007-08-23 21:42:29 +00001084 // "switch" is a control-flow statement. Thus we stop processing the
1085 // current block.
1086 CFGBlock* SwitchSuccessor = NULL;
1087
1088 if (Block) {
1089 FinishBlock(Block);
1090 SwitchSuccessor = Block;
1091 }
1092 else SwitchSuccessor = Succ;
1093
1094 // Save the current "switch" context.
1095 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek97bc3422008-02-13 22:05:39 +00001096 save_break(BreakTargetBlock),
1097 save_default(DefaultCaseBlock);
1098
1099 // Set the "default" case to be the block after the switch statement.
1100 // If the switch statement contains a "default:", this value will
1101 // be overwritten with the block for that code.
1102 DefaultCaseBlock = SwitchSuccessor;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001103
Ted Kremenek73543912007-08-23 21:42:29 +00001104 // Create a new block that will contain the switch statement.
1105 SwitchTerminatedBlock = createBlock(false);
1106
Ted Kremenek73543912007-08-23 21:42:29 +00001107 // Now process the switch body. The code after the switch is the implicit
1108 // successor.
1109 Succ = SwitchSuccessor;
1110 BreakTargetBlock = SwitchSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +00001111
1112 // When visiting the body, the case statements should automatically get
1113 // linked up to the switch. We also don't keep a pointer to the body,
1114 // since all control-flow from the switch goes to case/default statements.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001115 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001116 Block = NULL;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001117 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001118 if (Block) FinishBlock(BodyBlock);
1119
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001120 // If we have no "default:" case, the default transition is to the
1121 // code following the switch body.
Ted Kremenek97bc3422008-02-13 22:05:39 +00001122 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001123
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001124 // Add the terminator and condition in the switch block.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001125 SwitchTerminatedBlock->setTerminator(Terminator);
1126 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek73543912007-08-23 21:42:29 +00001127 Block = SwitchTerminatedBlock;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001128
Ted Kremenek79f0a632008-04-16 21:10:48 +00001129 return addStmt(Terminator->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +00001130}
1131
Ted Kremenek79f0a632008-04-16 21:10:48 +00001132CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Ted Kremenek97bc3422008-02-13 22:05:39 +00001133 // CaseStmts are essentially labels, so they are the
Ted Kremenek73543912007-08-23 21:42:29 +00001134 // first statement in a block.
Ted Kremenek44659d82007-08-30 18:48:11 +00001135
Ted Kremenek79f0a632008-04-16 21:10:48 +00001136 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek44659d82007-08-30 18:48:11 +00001137 CFGBlock* CaseBlock = Block;
1138 if (!CaseBlock) CaseBlock = createBlock();
1139
Ted Kremenek97bc3422008-02-13 22:05:39 +00001140 // Cases statements partition blocks, so this is the top of
1141 // the basic block we were processing (the "case XXX:" is the label).
Ted Kremenek79f0a632008-04-16 21:10:48 +00001142 CaseBlock->setLabel(Terminator);
Ted Kremenek73543912007-08-23 21:42:29 +00001143 FinishBlock(CaseBlock);
1144
1145 // Add this block to the list of successors for the block with the
1146 // switch statement.
Ted Kremenek97bc3422008-02-13 22:05:39 +00001147 assert (SwitchTerminatedBlock);
1148 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +00001149
1150 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1151 Block = NULL;
1152
1153 // This block is now the implicit successor of other blocks.
1154 Succ = CaseBlock;
1155
Ted Kremenek82e8a192008-03-15 07:45:02 +00001156 return CaseBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001157}
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001158
Ted Kremenek79f0a632008-04-16 21:10:48 +00001159CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1160 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek97bc3422008-02-13 22:05:39 +00001161 DefaultCaseBlock = Block;
1162 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1163
1164 // Default statements partition blocks, so this is the top of
1165 // the basic block we were processing (the "default:" is the label).
Ted Kremenek79f0a632008-04-16 21:10:48 +00001166 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek97bc3422008-02-13 22:05:39 +00001167 FinishBlock(DefaultCaseBlock);
1168
1169 // Unlike case statements, we don't add the default block to the
1170 // successors for the switch statement immediately. This is done
1171 // when we finish processing the switch statement. This allows for
1172 // the default case (including a fall-through to the code after the
1173 // switch statement) to always be the last successor of a switch-terminated
1174 // block.
1175
1176 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1177 Block = NULL;
1178
1179 // This block is now the implicit successor of other blocks.
1180 Succ = DefaultCaseBlock;
1181
1182 return DefaultCaseBlock;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001183}
Ted Kremenek73543912007-08-23 21:42:29 +00001184
Ted Kremenek0edd3a92007-08-28 19:26:49 +00001185CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1186 // Lazily create the indirect-goto dispatch block if there isn't one
1187 // already.
1188 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
1189
1190 if (!IBlock) {
1191 IBlock = createBlock(false);
1192 cfg->setIndirectGotoBlock(IBlock);
1193 }
1194
1195 // IndirectGoto is a control-flow statement. Thus we stop processing the
1196 // current block and create a new one.
1197 if (Block) FinishBlock(Block);
1198 Block = createBlock(false);
1199 Block->setTerminator(I);
1200 Block->addSuccessor(IBlock);
1201 return addStmt(I->getTarget());
1202}
1203
Ted Kremenek73543912007-08-23 21:42:29 +00001204
Ted Kremenekd6e50602007-08-23 21:26:19 +00001205} // end anonymous namespace
Ted Kremenek4db5b452007-08-23 16:51:22 +00001206
1207/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
1208/// block has no successors or predecessors. If this is the first block
1209/// created in the CFG, it is automatically set to be the Entry and Exit
1210/// of the CFG.
Ted Kremenek14594572007-09-05 20:02:05 +00001211CFGBlock* CFG::createBlock() {
Ted Kremenek4db5b452007-08-23 16:51:22 +00001212 bool first_block = begin() == end();
1213
1214 // Create the block.
Ted Kremenek14594572007-09-05 20:02:05 +00001215 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek4db5b452007-08-23 16:51:22 +00001216
1217 // If this is the first block, set it as the Entry and Exit.
1218 if (first_block) Entry = Exit = &front();
1219
1220 // Return the block.
1221 return &front();
Ted Kremenek97f75312007-08-21 21:42:03 +00001222}
1223
Ted Kremenek4db5b452007-08-23 16:51:22 +00001224/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1225/// CFG is returned to the caller.
1226CFG* CFG::buildCFG(Stmt* Statement) {
1227 CFGBuilder Builder;
1228 return Builder.buildCFG(Statement);
1229}
1230
1231/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek97f75312007-08-21 21:42:03 +00001232void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1233
Ted Kremenek3a819822007-10-01 19:33:33 +00001234//===----------------------------------------------------------------------===//
1235// CFG: Queries for BlkExprs.
1236//===----------------------------------------------------------------------===//
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001237
Ted Kremenek3a819822007-10-01 19:33:33 +00001238namespace {
Ted Kremenekab6c5902008-01-17 20:48:37 +00001239 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek3a819822007-10-01 19:33:33 +00001240}
1241
Ted Kremenek79f0a632008-04-16 21:10:48 +00001242static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1243 if (!Terminator)
Ted Kremenekc6fda602008-01-26 00:03:27 +00001244 return;
1245
Ted Kremenek79f0a632008-04-16 21:10:48 +00001246 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001247 if (!*I) continue;
1248
1249 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1250 if (B->isAssignmentOp()) Set.insert(B);
1251
1252 FindSubExprAssignments(*I, Set);
1253 }
1254}
1255
Ted Kremenek3a819822007-10-01 19:33:33 +00001256static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1257 BlkExprMapTy* M = new BlkExprMapTy();
1258
Ted Kremenekc6fda602008-01-26 00:03:27 +00001259 // Look for assignments that are used as subexpressions. These are the
Ted Kremenek79f0a632008-04-16 21:10:48 +00001260 // only assignments that we want to *possibly* register as a block-level
1261 // expression. Basically, if an assignment occurs both in a subexpression
1262 // and at the block-level, it is a block-level expression.
Ted Kremenekc6fda602008-01-26 00:03:27 +00001263 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
1264
Ted Kremenek3a819822007-10-01 19:33:33 +00001265 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1266 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc6fda602008-01-26 00:03:27 +00001267 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenekab6c5902008-01-17 20:48:37 +00001268
Ted Kremenek79f0a632008-04-16 21:10:48 +00001269 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
1270
1271 // Iterate over the statements again on identify the Expr* and Stmt* at
1272 // the block-level that are block-level expressions.
1273
Ted Kremenekc6fda602008-01-26 00:03:27 +00001274 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek79f0a632008-04-16 21:10:48 +00001275 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001276
Ted Kremenek79f0a632008-04-16 21:10:48 +00001277 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001278 // Assignment expressions that are not nested within another
1279 // expression are really "statements" whose value is never
1280 // used by another expression.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001281 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenekc6fda602008-01-26 00:03:27 +00001282 continue;
1283 }
Ted Kremenek79f0a632008-04-16 21:10:48 +00001284 else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001285 // Special handling for statement expressions. The last statement
1286 // in the statement expression is also a block-level expr.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001287 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenekab6c5902008-01-17 20:48:37 +00001288 if (!C->body_empty()) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001289 unsigned x = M->size();
Ted Kremenekab6c5902008-01-17 20:48:37 +00001290 (*M)[C->body_back()] = x;
1291 }
1292 }
Ted Kremenek5b4eb172008-01-25 23:22:27 +00001293
Ted Kremenekc6fda602008-01-26 00:03:27 +00001294 unsigned x = M->size();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001295 (*M)[Exp] = x;
Ted Kremenekc6fda602008-01-26 00:03:27 +00001296 }
1297
Ted Kremenek79f0a632008-04-16 21:10:48 +00001298 // Look at terminators. The condition is a block-level expression.
1299
Ted Kremenek16516e22008-11-12 21:11:49 +00001300 Stmt* S = I->getTerminatorCondition();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001301
Ted Kremenek16516e22008-11-12 21:11:49 +00001302 if (S && M->find(S) == M->end()) {
Ted Kremenek79f0a632008-04-16 21:10:48 +00001303 unsigned x = M->size();
Ted Kremenek16516e22008-11-12 21:11:49 +00001304 (*M)[S] = x;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001305 }
1306 }
1307
Ted Kremenek3a819822007-10-01 19:33:33 +00001308 return M;
1309}
1310
Ted Kremenekab6c5902008-01-17 20:48:37 +00001311CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1312 assert(S != NULL);
Ted Kremenek3a819822007-10-01 19:33:33 +00001313 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
1314
1315 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenekab6c5902008-01-17 20:48:37 +00001316 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3a819822007-10-01 19:33:33 +00001317
1318 if (I == M->end()) return CFG::BlkExprNumTy();
1319 else return CFG::BlkExprNumTy(I->second);
1320}
1321
1322unsigned CFG::getNumBlkExprs() {
1323 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1324 return M->size();
1325 else {
1326 // We assume callers interested in the number of BlkExprs will want
1327 // the map constructed if it doesn't already exist.
1328 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1329 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1330 }
1331}
1332
Ted Kremenekd058a9c2008-04-28 18:00:46 +00001333//===----------------------------------------------------------------------===//
Ted Kremenekd058a9c2008-04-28 18:00:46 +00001334// Cleanup: CFG dstor.
1335//===----------------------------------------------------------------------===//
1336
Ted Kremenek3a819822007-10-01 19:33:33 +00001337CFG::~CFG() {
1338 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1339}
1340
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001341//===----------------------------------------------------------------------===//
1342// CFG pretty printing
1343//===----------------------------------------------------------------------===//
1344
Ted Kremenekd8313202007-08-22 18:22:34 +00001345namespace {
1346
Ted Kremenek98cee3a2008-01-08 18:15:10 +00001347class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Ted Kremenek86afc042007-08-31 22:26:13 +00001348
Ted Kremenek08176a52007-08-31 21:30:12 +00001349 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1350 StmtMapTy StmtMap;
1351 signed CurrentBlock;
1352 unsigned CurrentStmt;
Ted Kremenek86afc042007-08-31 22:26:13 +00001353
Ted Kremenek73543912007-08-23 21:42:29 +00001354public:
Ted Kremenek86afc042007-08-31 22:26:13 +00001355
Ted Kremenek08176a52007-08-31 21:30:12 +00001356 StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) {
1357 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1358 unsigned j = 1;
1359 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1360 BI != BEnd; ++BI, ++j )
1361 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1362 }
1363 }
1364
1365 virtual ~StmtPrinterHelper() {}
1366
1367 void setBlockID(signed i) { CurrentBlock = i; }
1368 void setStmtID(unsigned i) { CurrentStmt = i; }
1369
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001370 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001371
Ted Kremenek79f0a632008-04-16 21:10:48 +00001372 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek08176a52007-08-31 21:30:12 +00001373
1374 if (I == StmtMap.end())
1375 return false;
1376
1377 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
1378 && I->second.second == CurrentStmt)
1379 return false;
1380
Ted Kremenek86afc042007-08-31 22:26:13 +00001381 OS << "[B" << I->second.first << "." << I->second.second << "]";
1382 return true;
Ted Kremenek08176a52007-08-31 21:30:12 +00001383 }
1384};
1385
Ted Kremenek98cee3a2008-01-08 18:15:10 +00001386class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1387 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
1388
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001389 llvm::raw_ostream& OS;
Ted Kremenek08176a52007-08-31 21:30:12 +00001390 StmtPrinterHelper* Helper;
1391public:
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001392 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper)
Ted Kremenek08176a52007-08-31 21:30:12 +00001393 : OS(os), Helper(helper) {}
Ted Kremenek73543912007-08-23 21:42:29 +00001394
1395 void VisitIfStmt(IfStmt* I) {
1396 OS << "if ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001397 I->getCond()->printPretty(OS,Helper);
Ted Kremenek73543912007-08-23 21:42:29 +00001398 }
1399
1400 // Default case.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001401 void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS); }
Ted Kremenek73543912007-08-23 21:42:29 +00001402
1403 void VisitForStmt(ForStmt* F) {
1404 OS << "for (" ;
Ted Kremenek23a1d662007-08-30 21:28:02 +00001405 if (F->getInit()) OS << "...";
1406 OS << "; ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001407 if (Stmt* C = F->getCond()) C->printPretty(OS,Helper);
Ted Kremenek23a1d662007-08-30 21:28:02 +00001408 OS << "; ";
1409 if (F->getInc()) OS << "...";
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001410 OS << ")";
Ted Kremenek73543912007-08-23 21:42:29 +00001411 }
1412
1413 void VisitWhileStmt(WhileStmt* W) {
1414 OS << "while " ;
Ted Kremenek08176a52007-08-31 21:30:12 +00001415 if (Stmt* C = W->getCond()) C->printPretty(OS,Helper);
Ted Kremenek73543912007-08-23 21:42:29 +00001416 }
1417
1418 void VisitDoStmt(DoStmt* D) {
1419 OS << "do ... while ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001420 if (Stmt* C = D->getCond()) C->printPretty(OS,Helper);
Ted Kremenek65cfa562007-08-27 21:27:44 +00001421 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001422
Ted Kremenek79f0a632008-04-16 21:10:48 +00001423 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek65cfa562007-08-27 21:27:44 +00001424 OS << "switch ";
Ted Kremenek79f0a632008-04-16 21:10:48 +00001425 Terminator->getCond()->printPretty(OS,Helper);
Ted Kremenek65cfa562007-08-27 21:27:44 +00001426 }
1427
Ted Kremenek621e1592007-08-31 21:49:40 +00001428 void VisitConditionalOperator(ConditionalOperator* C) {
1429 C->getCond()->printPretty(OS,Helper);
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001430 OS << " ? ... : ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001431 }
1432
Ted Kremenek2025cc92007-08-31 22:29:13 +00001433 void VisitChooseExpr(ChooseExpr* C) {
1434 OS << "__builtin_choose_expr( ";
1435 C->getCond()->printPretty(OS,Helper);
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001436 OS << " )";
Ted Kremenek2025cc92007-08-31 22:29:13 +00001437 }
1438
Ted Kremenek86afc042007-08-31 22:26:13 +00001439 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1440 OS << "goto *";
1441 I->getTarget()->printPretty(OS,Helper);
Ted Kremenek86afc042007-08-31 22:26:13 +00001442 }
1443
Ted Kremenek621e1592007-08-31 21:49:40 +00001444 void VisitBinaryOperator(BinaryOperator* B) {
1445 if (!B->isLogicalOp()) {
1446 VisitExpr(B);
1447 return;
1448 }
1449
1450 B->getLHS()->printPretty(OS,Helper);
1451
1452 switch (B->getOpcode()) {
1453 case BinaryOperator::LOr:
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001454 OS << " || ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001455 return;
1456 case BinaryOperator::LAnd:
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001457 OS << " && ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001458 return;
1459 default:
1460 assert(false && "Invalid logical operator.");
1461 }
1462 }
1463
Ted Kremenekcfaae762007-08-27 21:54:41 +00001464 void VisitExpr(Expr* E) {
Ted Kremenek08176a52007-08-31 21:30:12 +00001465 E->printPretty(OS,Helper);
Ted Kremenekcfaae762007-08-27 21:54:41 +00001466 }
Ted Kremenek73543912007-08-23 21:42:29 +00001467};
Ted Kremenek08176a52007-08-31 21:30:12 +00001468
1469
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001470void print_stmt(llvm::raw_ostream&OS, StmtPrinterHelper* Helper, Stmt* Terminator) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001471 if (Helper) {
1472 // special printing for statement-expressions.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001473 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001474 CompoundStmt* Sub = SE->getSubStmt();
1475
1476 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek16e3b9a2007-08-31 22:47:06 +00001477 OS << "({ ... ; ";
Ted Kremenek256a2592007-10-29 20:41:04 +00001478 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek16e3b9a2007-08-31 22:47:06 +00001479 OS << " })\n";
Ted Kremenek86afc042007-08-31 22:26:13 +00001480 return;
1481 }
1482 }
1483
1484 // special printing for comma expressions.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001485 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001486 if (B->getOpcode() == BinaryOperator::Comma) {
1487 OS << "... , ";
1488 Helper->handledStmt(B->getRHS(),OS);
1489 OS << '\n';
1490 return;
1491 }
1492 }
1493 }
1494
Ted Kremenek79f0a632008-04-16 21:10:48 +00001495 Terminator->printPretty(OS, Helper);
Ted Kremenek86afc042007-08-31 22:26:13 +00001496
1497 // Expressions need a newline.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001498 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek86afc042007-08-31 22:26:13 +00001499}
1500
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001501void print_block(llvm::raw_ostream& OS, const CFG* cfg, const CFGBlock& B,
Ted Kremenek08176a52007-08-31 21:30:12 +00001502 StmtPrinterHelper* Helper, bool print_edges) {
1503
1504 if (Helper) Helper->setBlockID(B.getBlockID());
1505
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001506 // Print the header.
Ted Kremenek08176a52007-08-31 21:30:12 +00001507 OS << "\n [ B" << B.getBlockID();
1508
1509 if (&B == &cfg->getEntry())
1510 OS << " (ENTRY) ]\n";
1511 else if (&B == &cfg->getExit())
1512 OS << " (EXIT) ]\n";
1513 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001514 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek08176a52007-08-31 21:30:12 +00001515 else
1516 OS << " ]\n";
1517
Ted Kremenekec055e12007-08-29 23:20:49 +00001518 // Print the label of this block.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001519 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek08176a52007-08-31 21:30:12 +00001520
1521 if (print_edges)
1522 OS << " ";
1523
Ted Kremenek79f0a632008-04-16 21:10:48 +00001524 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenekec055e12007-08-29 23:20:49 +00001525 OS << L->getName();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001526 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenekec055e12007-08-29 23:20:49 +00001527 OS << "case ";
1528 C->getLHS()->printPretty(OS);
1529 if (C->getRHS()) {
1530 OS << " ... ";
1531 C->getRHS()->printPretty(OS);
1532 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001533 }
Ted Kremenek79f0a632008-04-16 21:10:48 +00001534 else if (isa<DefaultStmt>(Terminator))
Ted Kremenekec055e12007-08-29 23:20:49 +00001535 OS << "default";
Ted Kremenek08176a52007-08-31 21:30:12 +00001536 else
1537 assert(false && "Invalid label statement in CFGBlock.");
1538
Ted Kremenekec055e12007-08-29 23:20:49 +00001539 OS << ":\n";
1540 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001541
Ted Kremenek97f75312007-08-21 21:42:03 +00001542 // Iterate through the statements in the block and print them.
Ted Kremenek97f75312007-08-21 21:42:03 +00001543 unsigned j = 1;
Ted Kremenek08176a52007-08-31 21:30:12 +00001544
1545 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1546 I != E ; ++I, ++j ) {
1547
Ted Kremenekec055e12007-08-29 23:20:49 +00001548 // Print the statement # in the basic block and the statement itself.
Ted Kremenek08176a52007-08-31 21:30:12 +00001549 if (print_edges)
1550 OS << " ";
1551
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001552 OS << llvm::format("%3d", j) << ": ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001553
1554 if (Helper)
1555 Helper->setStmtID(j);
Ted Kremenek86afc042007-08-31 22:26:13 +00001556
1557 print_stmt(OS,Helper,*I);
Ted Kremenek97f75312007-08-21 21:42:03 +00001558 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001559
Ted Kremenekec055e12007-08-29 23:20:49 +00001560 // Print the terminator of this block.
Ted Kremenek08176a52007-08-31 21:30:12 +00001561 if (B.getTerminator()) {
1562 if (print_edges)
1563 OS << " ";
1564
Ted Kremenekec055e12007-08-29 23:20:49 +00001565 OS << " T: ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001566
1567 if (Helper) Helper->setBlockID(-1);
1568
1569 CFGBlockTerminatorPrint TPrinter(OS,Helper);
1570 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001571 OS << '\n';
Ted Kremenek97f75312007-08-21 21:42:03 +00001572 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001573
Ted Kremenekec055e12007-08-29 23:20:49 +00001574 if (print_edges) {
1575 // Print the predecessors of this block.
Ted Kremenek08176a52007-08-31 21:30:12 +00001576 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenekec055e12007-08-29 23:20:49 +00001577 unsigned i = 0;
Ted Kremenekec055e12007-08-29 23:20:49 +00001578
Ted Kremenek08176a52007-08-31 21:30:12 +00001579 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1580 I != E; ++I, ++i) {
1581
1582 if (i == 8 || (i-8) == 0)
1583 OS << "\n ";
1584
Ted Kremenekec055e12007-08-29 23:20:49 +00001585 OS << " B" << (*I)->getBlockID();
1586 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001587
1588 OS << '\n';
1589
1590 // Print the successors of this block.
1591 OS << " Successors (" << B.succ_size() << "):";
1592 i = 0;
1593
1594 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1595 I != E; ++I, ++i) {
1596
1597 if (i == 8 || (i-8) % 10 == 0)
1598 OS << "\n ";
1599
1600 OS << " B" << (*I)->getBlockID();
1601 }
1602
Ted Kremenekec055e12007-08-29 23:20:49 +00001603 OS << '\n';
Ted Kremenek97f75312007-08-21 21:42:03 +00001604 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001605}
1606
1607} // end anonymous namespace
1608
1609/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001610void CFG::dump() const { print(llvm::errs()); }
Ted Kremenek08176a52007-08-31 21:30:12 +00001611
1612/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001613void CFG::print(llvm::raw_ostream& OS) const {
Ted Kremenek08176a52007-08-31 21:30:12 +00001614
1615 StmtPrinterHelper Helper(this);
1616
1617 // Print the entry block.
1618 print_block(OS, this, getEntry(), &Helper, true);
1619
1620 // Iterate through the CFGBlocks and print them one by one.
1621 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1622 // Skip the entry block, because we already printed it.
1623 if (&(*I) == &getEntry() || &(*I) == &getExit())
1624 continue;
1625
1626 print_block(OS, this, *I, &Helper, true);
1627 }
1628
1629 // Print the exit block.
1630 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd19e99e2008-11-24 20:50:24 +00001631 OS.flush();
Ted Kremenek08176a52007-08-31 21:30:12 +00001632}
1633
1634/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001635void CFGBlock::dump(const CFG* cfg) const { print(llvm::errs(), cfg); }
Ted Kremenek08176a52007-08-31 21:30:12 +00001636
1637/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1638/// Generally this will only be called from CFG::print.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001639void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg) const {
Ted Kremenek08176a52007-08-31 21:30:12 +00001640 StmtPrinterHelper Helper(cfg);
1641 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek4db5b452007-08-23 16:51:22 +00001642}
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001643
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001644/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001645void CFGBlock::printTerminator(llvm::raw_ostream& OS) const {
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001646 CFGBlockTerminatorPrint TPrinter(OS,NULL);
1647 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1648}
1649
Ted Kremenek16516e22008-11-12 21:11:49 +00001650Stmt* CFGBlock::getTerminatorCondition() {
Ted Kremenek79f0a632008-04-16 21:10:48 +00001651
1652 if (!Terminator)
1653 return NULL;
1654
1655 Expr* E = NULL;
1656
1657 switch (Terminator->getStmtClass()) {
1658 default:
1659 break;
1660
1661 case Stmt::ForStmtClass:
1662 E = cast<ForStmt>(Terminator)->getCond();
1663 break;
1664
1665 case Stmt::WhileStmtClass:
1666 E = cast<WhileStmt>(Terminator)->getCond();
1667 break;
1668
1669 case Stmt::DoStmtClass:
1670 E = cast<DoStmt>(Terminator)->getCond();
1671 break;
1672
1673 case Stmt::IfStmtClass:
1674 E = cast<IfStmt>(Terminator)->getCond();
1675 break;
1676
1677 case Stmt::ChooseExprClass:
1678 E = cast<ChooseExpr>(Terminator)->getCond();
1679 break;
1680
1681 case Stmt::IndirectGotoStmtClass:
1682 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1683 break;
1684
1685 case Stmt::SwitchStmtClass:
1686 E = cast<SwitchStmt>(Terminator)->getCond();
1687 break;
1688
1689 case Stmt::ConditionalOperatorClass:
1690 E = cast<ConditionalOperator>(Terminator)->getCond();
1691 break;
1692
1693 case Stmt::BinaryOperatorClass: // '&&' and '||'
1694 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek16516e22008-11-12 21:11:49 +00001695 break;
1696
1697 case Stmt::ObjCForCollectionStmtClass:
1698 return Terminator;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001699 }
1700
1701 return E ? E->IgnoreParens() : NULL;
1702}
1703
Ted Kremenekbdbd1b52008-05-16 16:06:00 +00001704bool CFGBlock::hasBinaryBranchTerminator() const {
1705
1706 if (!Terminator)
1707 return false;
1708
1709 Expr* E = NULL;
1710
1711 switch (Terminator->getStmtClass()) {
1712 default:
1713 return false;
1714
1715 case Stmt::ForStmtClass:
1716 case Stmt::WhileStmtClass:
1717 case Stmt::DoStmtClass:
1718 case Stmt::IfStmtClass:
1719 case Stmt::ChooseExprClass:
1720 case Stmt::ConditionalOperatorClass:
1721 case Stmt::BinaryOperatorClass:
1722 return true;
1723 }
1724
1725 return E ? E->IgnoreParens() : NULL;
1726}
1727
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001728
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001729//===----------------------------------------------------------------------===//
1730// CFG Graphviz Visualization
1731//===----------------------------------------------------------------------===//
1732
Ted Kremenek08176a52007-08-31 21:30:12 +00001733
1734#ifndef NDEBUG
Chris Lattner26002172007-09-17 06:16:32 +00001735static StmtPrinterHelper* GraphHelper;
Ted Kremenek08176a52007-08-31 21:30:12 +00001736#endif
1737
1738void CFG::viewCFG() const {
1739#ifndef NDEBUG
1740 StmtPrinterHelper H(this);
1741 GraphHelper = &H;
1742 llvm::ViewGraph(this,"CFG");
1743 GraphHelper = NULL;
Ted Kremenek08176a52007-08-31 21:30:12 +00001744#endif
1745}
1746
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001747namespace llvm {
1748template<>
1749struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
1750 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
1751
Hartmut Kaiser752a0052007-09-16 00:28:28 +00001752#ifndef NDEBUG
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001753 std::string OutSStr;
1754 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek08176a52007-08-31 21:30:12 +00001755 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001756 std::string& OutStr = Out.str();
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001757
1758 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1759
1760 // Process string output to make it nicer...
1761 for (unsigned i = 0; i != OutStr.length(); ++i)
1762 if (OutStr[i] == '\n') { // Left justify
1763 OutStr[i] = '\\';
1764 OutStr.insert(OutStr.begin()+i+1, 'l');
1765 }
1766
1767 return OutStr;
Hartmut Kaiser752a0052007-09-16 00:28:28 +00001768#else
1769 return "";
1770#endif
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001771 }
1772};
1773} // end namespace llvm