blob: 8f2a5719f68ba1d3613f452dfc4191bd4fb4fe2a [file] [log] [blame]
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Kremenek4aa1e8b2007-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
Ted Kremenekb1c170e2009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek6796fbd2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000019#include "llvm/Support/GraphWriter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000020#include "llvm/Support/Allocator.h"
21#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000022#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000023#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek8aed4902009-10-20 23:46:25 +000024#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000025
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000026using namespace clang;
27
28namespace {
29
Douglas Gregor6e6ad602009-01-20 01:17:11 +000030static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000031 if (VarDecl* VD = dyn_cast<VarDecl>(D))
32 if (Expr* Ex = VD->getInit())
33 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000034
35 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000036}
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000037
38class AddStmtChoice {
39public:
40 enum Kind { NotAlwaysAdd = 0, AlwaysAdd, AlwaysAddAsLValue };
41public:
42 AddStmtChoice(Kind kind) : k(kind) {}
43 bool alwaysAdd() const { return k != NotAlwaysAdd; }
44 bool asLValue() const { return k == AlwaysAddAsLValue; }
45private:
46 Kind k;
47};
Mike Stump31feda52009-07-17 01:31:16 +000048
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000049/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000050/// The builder is stateful: an instance of the builder should be used to only
51/// construct a single CFG.
52///
53/// Example usage:
54///
55/// CFGBuilder builder;
56/// CFG* cfg = builder.BuildAST(stmt1);
57///
Mike Stump31feda52009-07-17 01:31:16 +000058/// CFG construction is done via a recursive walk of an AST. We actually parse
59/// the AST in reverse order so that the successor of a basic block is
60/// constructed prior to its predecessor. This allows us to nicely capture
61/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000062///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000063class CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000064 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +000065 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +000066
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000067 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000068 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000069 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000070 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000071 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000072 CFGBlock* DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +000073
Ted Kremenekeda180e22007-08-28 19:26:49 +000074 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000075 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
76 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000077
78 // A list of blocks that end with a "goto" that must be backpatched to their
79 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000080 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000081 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000082
Ted Kremenekeda180e22007-08-28 19:26:49 +000083 // A list of labels whose address has been taken (for indirect gotos).
84 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
85 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000086
87public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +000088 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
89 Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000090 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek289ae4f2009-10-12 20:55:07 +000091 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {}
Mike Stump31feda52009-07-17 01:31:16 +000092
Ted Kremenek9aae5132007-08-23 21:42:29 +000093 // buildCFG - Used by external clients to construct the CFG.
Mike Stump0d76d072009-07-20 23:24:15 +000094 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump31feda52009-07-17 01:31:16 +000095
Ted Kremenek93668002009-07-17 22:18:43 +000096private:
97 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000098 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
99 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
100 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000101 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000102 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000103 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000104 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000105 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000106 CFGBlock *VisitConditionalOperator(ConditionalOperator *C,
107 AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000108 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenekc98cdd12009-12-15 01:38:04 +0000109 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S) { return NYS(); }
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000110 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenekc98cdd12009-12-15 01:38:04 +0000111 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S) { return NYS(); }
Ted Kremenek93668002009-07-17 22:18:43 +0000112 CFGBlock *VisitDeclStmt(DeclStmt *DS);
113 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000114 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
115 CFGBlock *VisitDoStmt(DoStmt *D);
116 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000117 CFGBlock *VisitGotoStmt(GotoStmt* G);
118 CFGBlock *VisitIfStmt(IfStmt *I);
119 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
120 CFGBlock *VisitLabelStmt(LabelStmt *L);
121 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
122 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
123 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
124 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
125 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
126 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000127 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
128 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000129 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
130 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000131
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000132 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
133 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000134 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000135
Ted Kremenek6065ef62008-04-28 18:00:46 +0000136 // NYS == Not Yet Supported
137 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000138 badCFG = true;
139 return Block;
140 }
Mike Stump31feda52009-07-17 01:31:16 +0000141
Ted Kremenek93668002009-07-17 22:18:43 +0000142 void autoCreateBlock() { if (!Block) Block = createBlock(); }
143 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000144 bool FinishBlock(CFGBlock* B);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000145 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
146 return Visit(S, asc);
147 }
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000148
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000149 void AppendStmt(CFGBlock *B, Stmt *S,
150 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
151 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000152 }
153
154 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
155 B->addSuccessor(S, cfg->getBumpVectorContext());
156 }
Mike Stump11289f42009-09-09 15:08:12 +0000157
Ted Kremenek963cc312009-07-24 06:55:42 +0000158 /// TryResult - a class representing a variant over the values
159 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
160 /// and is used by the CFGBuilder to decide if a branch condition
161 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000162 class TryResult {
163 int X;
164 public:
165 TryResult(bool b) : X(b ? 1 : 0) {}
166 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000167
Ted Kremenek30754282009-07-24 04:47:11 +0000168 bool isTrue() const { return X == 1; }
169 bool isFalse() const { return X == 0; }
170 bool isKnown() const { return X >= 0; }
171 void negate() {
172 assert(isKnown());
173 X ^= 0x1;
174 }
175 };
Mike Stump11289f42009-09-09 15:08:12 +0000176
Mike Stump773582d2009-07-23 23:25:26 +0000177 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
178 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000179 TryResult TryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000180 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000181 if (!S->isTypeDependent() && !S->isValueDependent() &&
182 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000183 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000184
185 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000186 }
187
Ted Kremenekb64d1832008-03-13 03:04:22 +0000188 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000189};
Mike Stump31feda52009-07-17 01:31:16 +0000190
Douglas Gregor4619e432008-12-05 23:32:09 +0000191// FIXME: Add support for dependent-sized array types in C++?
192// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000193static VariableArrayType* FindVA(Type* t) {
194 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
195 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
196 if (vat->getSizeExpr())
197 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000198
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000199 t = vt->getElementType().getTypePtr();
200 }
Mike Stump31feda52009-07-17 01:31:16 +0000201
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000202 return 0;
203}
Mike Stump31feda52009-07-17 01:31:16 +0000204
205/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
206/// arbitrary statement. Examples include a single expression or a function
207/// body (compound statement). The ownership of the returned CFG is
208/// transferred to the caller. If CFG construction fails, this method returns
209/// NULL.
Mike Stump0d76d072009-07-20 23:24:15 +0000210CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
211 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000212 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000213 if (!Statement)
214 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000215
Ted Kremenekb64d1832008-03-13 03:04:22 +0000216 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000217
218 // Create an empty block that will serve as the exit block for the CFG. Since
219 // this is the first block added to the CFG, it will be implicitly registered
220 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000221 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000222 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000223 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000224
Ted Kremenek9aae5132007-08-23 21:42:29 +0000225 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000226 CFGBlock* B = addStmt(Statement);
Ted Kremenek8aed4902009-10-20 23:46:25 +0000227 if (!B)
228 B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000229
Ted Kremenek40d87632008-02-27 17:33:02 +0000230 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000231 // Finalize the last constructed block. This usually involves reversing the
232 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000233 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000234
235 // Backpatch the gotos whose label -> block mappings we didn't know when we
236 // encountered them.
237 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000238 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000239
Ted Kremenek9aae5132007-08-23 21:42:29 +0000240 CFGBlock* B = *I;
241 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
242 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
243
244 // If there is no target for the goto, then we are looking at an
245 // incomplete AST. Handle this by not registering a successor.
246 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000247
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000248 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000249 }
Mike Stump31feda52009-07-17 01:31:16 +0000250
Ted Kremenekeda180e22007-08-28 19:26:49 +0000251 // Add successors to the Indirect Goto Dispatch block (if we have one).
252 if (CFGBlock* B = cfg->getIndirectGotoBlock())
253 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
254 E = AddressTakenLabels.end(); I != E; ++I ) {
255
256 // Lookup the target block.
257 LabelMapTy::iterator LI = LabelMap.find(*I);
258
259 // If there is no target block that contains label, then we are looking
260 // at an incomplete AST. Handle this by not registering a successor.
261 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000262
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000263 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000264 }
Mike Stump31feda52009-07-17 01:31:16 +0000265
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000266 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000267 }
Mike Stump31feda52009-07-17 01:31:16 +0000268
269 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000270 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000271
Ted Kremenekab929bb2009-10-20 23:59:28 +0000272 return badCFG ? NULL : cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000273}
Mike Stump31feda52009-07-17 01:31:16 +0000274
Ted Kremenek9aae5132007-08-23 21:42:29 +0000275/// createBlock - Used to lazily create blocks that are connected
276/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000277CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000278 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000279 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000280 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000281 return B;
282}
Mike Stump31feda52009-07-17 01:31:16 +0000283
Ted Kremenek0868eea2009-09-24 18:45:41 +0000284/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek55957a82009-05-02 00:13:27 +0000285bool CFGBuilder::FinishBlock(CFGBlock* B) {
286 if (badCFG)
287 return false;
288
Ted Kremenek93668002009-07-17 22:18:43 +0000289 assert(B);
Ted Kremenek55957a82009-05-02 00:13:27 +0000290 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000291}
292
Ted Kremenek93668002009-07-17 22:18:43 +0000293/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000294/// blocks for ternary operators, &&, and ||. We also process "," and
295/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000296CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000297tryAgain:
298 switch (S->getStmtClass()) {
299 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000300 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000301
302 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000303 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000304
Ted Kremenek93668002009-07-17 22:18:43 +0000305 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000306 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000307
Ted Kremenek93668002009-07-17 22:18:43 +0000308 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000309 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000310
Ted Kremenek93668002009-07-17 22:18:43 +0000311 case Stmt::BreakStmtClass:
312 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000313
Ted Kremenek93668002009-07-17 22:18:43 +0000314 case Stmt::CallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000315 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000316
Ted Kremenek93668002009-07-17 22:18:43 +0000317 case Stmt::CaseStmtClass:
318 return VisitCaseStmt(cast<CaseStmt>(S));
319
320 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000321 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000322
Ted Kremenek93668002009-07-17 22:18:43 +0000323 case Stmt::CompoundStmtClass:
324 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000325
Ted Kremenek93668002009-07-17 22:18:43 +0000326 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000327 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000328
Ted Kremenek93668002009-07-17 22:18:43 +0000329 case Stmt::ContinueStmtClass:
330 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000331
Ted Kremenek93668002009-07-17 22:18:43 +0000332 case Stmt::DeclStmtClass:
333 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000334
Ted Kremenek93668002009-07-17 22:18:43 +0000335 case Stmt::DefaultStmtClass:
336 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000337
Ted Kremenek93668002009-07-17 22:18:43 +0000338 case Stmt::DoStmtClass:
339 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000340
Ted Kremenek93668002009-07-17 22:18:43 +0000341 case Stmt::ForStmtClass:
342 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000343
Ted Kremenek93668002009-07-17 22:18:43 +0000344 case Stmt::GotoStmtClass:
345 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000346
Ted Kremenek93668002009-07-17 22:18:43 +0000347 case Stmt::IfStmtClass:
348 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000349
Ted Kremenek93668002009-07-17 22:18:43 +0000350 case Stmt::IndirectGotoStmtClass:
351 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000352
Ted Kremenek93668002009-07-17 22:18:43 +0000353 case Stmt::LabelStmtClass:
354 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000355
Ted Kremenek93668002009-07-17 22:18:43 +0000356 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000357 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
358
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000359 case Stmt::CXXThrowExprClass:
360 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
361
Ted Kremenek93668002009-07-17 22:18:43 +0000362 case Stmt::ObjCAtSynchronizedStmtClass:
363 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000364
Ted Kremenek93668002009-07-17 22:18:43 +0000365 case Stmt::ObjCAtThrowStmtClass:
366 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000367
Ted Kremenek93668002009-07-17 22:18:43 +0000368 case Stmt::ObjCAtTryStmtClass:
369 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000370
Ted Kremenek93668002009-07-17 22:18:43 +0000371 case Stmt::ObjCForCollectionStmtClass:
372 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000373
Ted Kremenek93668002009-07-17 22:18:43 +0000374 case Stmt::ParenExprClass:
375 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000376 goto tryAgain;
377
Ted Kremenek93668002009-07-17 22:18:43 +0000378 case Stmt::NullStmtClass:
379 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000380
Ted Kremenek93668002009-07-17 22:18:43 +0000381 case Stmt::ReturnStmtClass:
382 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000383
Ted Kremenek93668002009-07-17 22:18:43 +0000384 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000385 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000386
Ted Kremenek93668002009-07-17 22:18:43 +0000387 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000388 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000389
Ted Kremenek93668002009-07-17 22:18:43 +0000390 case Stmt::SwitchStmtClass:
391 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000392
Ted Kremenek93668002009-07-17 22:18:43 +0000393 case Stmt::WhileStmtClass:
394 return VisitWhileStmt(cast<WhileStmt>(S));
395 }
396}
Mike Stump11289f42009-09-09 15:08:12 +0000397
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000398CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
399 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000400 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000401 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Ted Kremenek93668002009-07-17 22:18:43 +0000404 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000405}
Mike Stump31feda52009-07-17 01:31:16 +0000406
Ted Kremenek93668002009-07-17 22:18:43 +0000407/// VisitChildren - Visit the children of a Stmt.
408CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
409 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000410 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000411 E = Terminator->child_end(); I != E; ++I) {
412 if (*I) B = Visit(*I);
413 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000414 return B;
415}
Mike Stump11289f42009-09-09 15:08:12 +0000416
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000417CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
418 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000419 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000420
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000421 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000422 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000423 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000424 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000425
Ted Kremenek9aae5132007-08-23 21:42:29 +0000426 return Block;
427}
Mike Stump11289f42009-09-09 15:08:12 +0000428
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000429CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
430 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000431 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000432 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000433 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000434
Ted Kremenek93668002009-07-17 22:18:43 +0000435 if (!FinishBlock(ConfluenceBlock))
436 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000437
Ted Kremenek93668002009-07-17 22:18:43 +0000438 // create the block evaluating the LHS
439 CFGBlock* LHSBlock = createBlock(false);
440 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000441
Ted Kremenek93668002009-07-17 22:18:43 +0000442 // create the block evaluating the RHS
443 Succ = ConfluenceBlock;
444 Block = NULL;
445 CFGBlock* RHSBlock = addStmt(B->getRHS());
446 if (!FinishBlock(RHSBlock))
447 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000448
Mike Stump773582d2009-07-23 23:25:26 +0000449 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000450 TryResult KnownVal = TryEvaluateBool(B->getLHS());
451 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
452 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000453
Ted Kremenek93668002009-07-17 22:18:43 +0000454 // Now link the LHSBlock with RHSBlock.
455 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000456 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
457 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000458 } else {
Ted Kremenek93668002009-07-17 22:18:43 +0000459 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000460 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
461 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000462 }
Mike Stump11289f42009-09-09 15:08:12 +0000463
Ted Kremenek93668002009-07-17 22:18:43 +0000464 // Generate the blocks for evaluating the LHS.
465 Block = LHSBlock;
466 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000467 }
Ted Kremenek93668002009-07-17 22:18:43 +0000468 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000469 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000470 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000471 addStmt(B->getRHS());
472 return addStmt(B->getLHS());
473 }
Mike Stump11289f42009-09-09 15:08:12 +0000474
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000475 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000476}
477
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000478CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
479 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000480 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000481 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000482 }
483 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000484}
485
Ted Kremenek93668002009-07-17 22:18:43 +0000486CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
487 // "break" is a control-flow statement. Thus we stop processing the current
488 // block.
489 if (Block && !FinishBlock(Block))
490 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000491
Ted Kremenek93668002009-07-17 22:18:43 +0000492 // Now create a new block that ends with the break statement.
493 Block = createBlock(false);
494 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000495
Ted Kremenek93668002009-07-17 22:18:43 +0000496 // If there is no target for the break, then we are looking at an incomplete
497 // AST. This means that the CFG cannot be constructed.
498 if (BreakTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000499 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000500 else
501 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000502
503
Ted Kremenek9aae5132007-08-23 21:42:29 +0000504 return Block;
505}
Mike Stump11289f42009-09-09 15:08:12 +0000506
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000507CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000508 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000509 bool NoReturn = false;
510 if (C->getCallee()->getType().getNoReturnAttr()) {
511 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000512 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000513
514 if (FunctionDecl *FD = C->getDirectCallee())
515 if (FD->hasAttr<NoReturnAttr>())
516 NoReturn = true;
517
518 if (!NoReturn)
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000519 return VisitStmt(C, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000520
Mike Stump8c5d7992009-07-25 21:26:53 +0000521 if (Block && !FinishBlock(Block))
522 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000523
Mike Stump8c5d7992009-07-25 21:26:53 +0000524 // Create new block with no successor for the remaining pieces.
525 Block = createBlock(false);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000526 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000527
528 // Wire this to the exit block directly.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000529 AddSuccessor(Block, &cfg->getExit());
Mike Stump11289f42009-09-09 15:08:12 +0000530
Mike Stump8c5d7992009-07-25 21:26:53 +0000531 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000532}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000533
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000534CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
535 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000536 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000537 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000538 if (!FinishBlock(ConfluenceBlock))
539 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000540
Ted Kremenek21822592009-07-17 18:20:32 +0000541 Succ = ConfluenceBlock;
542 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000543 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000544 if (!FinishBlock(LHSBlock))
545 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000546
Ted Kremenek21822592009-07-17 18:20:32 +0000547 Succ = ConfluenceBlock;
548 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000549 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000550 if (!FinishBlock(RHSBlock))
551 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000552
Ted Kremenek21822592009-07-17 18:20:32 +0000553 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000554 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000555 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000556 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
557 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000558 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000559 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000560}
Mike Stump11289f42009-09-09 15:08:12 +0000561
562
563CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
564 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000565
566 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
567 I != E; ++I ) {
568 LastBlock = addStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000569
Ted Kremenekce499c22009-08-27 23:16:26 +0000570 if (badCFG)
571 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000572 }
Ted Kremenek93668002009-07-17 22:18:43 +0000573 return LastBlock;
574}
Mike Stump11289f42009-09-09 15:08:12 +0000575
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000576CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
577 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000578 // Create the confluence block that will "merge" the results of the ternary
579 // expression.
580 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000581 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000582 if (!FinishBlock(ConfluenceBlock))
583 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000584
Ted Kremenek51d40b02009-07-17 18:15:54 +0000585 // Create a block for the LHS expression if there is an LHS expression. A
586 // GCC extension allows LHS to be NULL, causing the condition to be the
587 // value that is returned instead.
588 // e.g: x ?: y is shorthand for: x ? x : y;
589 Succ = ConfluenceBlock;
590 Block = NULL;
591 CFGBlock* LHSBlock = NULL;
592 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000593 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000594 if (!FinishBlock(LHSBlock))
595 return 0;
596 Block = NULL;
597 }
Mike Stump11289f42009-09-09 15:08:12 +0000598
Ted Kremenek51d40b02009-07-17 18:15:54 +0000599 // Create the block for the RHS expression.
600 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000601 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000602 if (!FinishBlock(RHSBlock))
603 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000604
Ted Kremenek51d40b02009-07-17 18:15:54 +0000605 // Create the block that will contain the condition.
606 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000607
Mike Stump773582d2009-07-23 23:25:26 +0000608 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000609 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000610 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000611 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000612 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000613 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000614 // If we know the condition is false, add NULL as the successor for
615 // the block containing the condition. In this case, the confluence
616 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000617 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000618 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000619 } else {
620 // If we have no LHS expression, add the ConfluenceBlock as a direct
621 // successor for the block containing the condition. Moreover, we need to
622 // reverse the order of the predecessors in the ConfluenceBlock because
623 // the RHSBlock will have been added to the succcessors already, and we
624 // want the first predecessor to the the block containing the expression
625 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000626 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000627 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000628 std::reverse(ConfluenceBlock->pred_begin(),
629 ConfluenceBlock->pred_end());
630 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000631 }
Mike Stump11289f42009-09-09 15:08:12 +0000632
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000633 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000634 Block->setTerminator(C);
635 return addStmt(C->getCond());
636}
637
Ted Kremenek93668002009-07-17 22:18:43 +0000638CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
639 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000640
Ted Kremenek93668002009-07-17 22:18:43 +0000641 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000642 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000643 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000644 }
Mike Stump11289f42009-09-09 15:08:12 +0000645
Ted Kremenek93668002009-07-17 22:18:43 +0000646 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000647
Ted Kremenek93668002009-07-17 22:18:43 +0000648 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
649 typedef llvm::SmallVector<Decl*,10> BufTy;
650 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000651
Ted Kremenek93668002009-07-17 22:18:43 +0000652 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
653 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
654 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
655 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000656
Ted Kremenek93668002009-07-17 22:18:43 +0000657 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
658 // automatically freed with the CFG.
659 DeclGroupRef DG(*I);
660 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000661 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000662 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000663
Ted Kremenek93668002009-07-17 22:18:43 +0000664 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000665 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000666 B = VisitDeclSubExpr(D);
667 }
Mike Stump11289f42009-09-09 15:08:12 +0000668
669 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000670}
Mike Stump11289f42009-09-09 15:08:12 +0000671
Ted Kremenek93668002009-07-17 22:18:43 +0000672/// VisitDeclSubExpr - Utility method to add block-level expressions for
673/// initializers in Decls.
674CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
675 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000676
Ted Kremenek93668002009-07-17 22:18:43 +0000677 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000678
Ted Kremenek93668002009-07-17 22:18:43 +0000679 if (!VD)
680 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000681
Ted Kremenek93668002009-07-17 22:18:43 +0000682 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000683
Ted Kremenek93668002009-07-17 22:18:43 +0000684 if (Init) {
685 // Optimization: Don't create separate block-level statements for literals.
686 switch (Init->getStmtClass()) {
687 case Stmt::IntegerLiteralClass:
688 case Stmt::CharacterLiteralClass:
689 case Stmt::StringLiteralClass:
690 break;
691 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000692 Block = addStmt(Init,
693 VD->getType()->isReferenceType()
694 ? AddStmtChoice::AlwaysAddAsLValue
695 : AddStmtChoice::AlwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000696 }
697 }
Mike Stump11289f42009-09-09 15:08:12 +0000698
Ted Kremenek93668002009-07-17 22:18:43 +0000699 // If the type of VD is a VLA, then we must process its size expressions.
700 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
701 VA = FindVA(VA->getElementType().getTypePtr()))
702 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000703
Ted Kremenek93668002009-07-17 22:18:43 +0000704 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000705}
706
707CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000708 // We may see an if statement in the middle of a basic block, or it may be the
709 // first statement we are processing. In either case, we create a new basic
710 // block. First, we create the blocks for the then...else statements, and
711 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000712 // middle of a block, we stop processing that block. That block is then the
713 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000714
715 // The block we were proccessing is now finished. Make it the successor
716 // block.
717 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000718 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000719 if (!FinishBlock(Block))
720 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000721 }
Mike Stump31feda52009-07-17 01:31:16 +0000722
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000723 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000724 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000725
Ted Kremenek9aae5132007-08-23 21:42:29 +0000726 if (Stmt* Else = I->getElse()) {
727 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000728
Ted Kremenek9aae5132007-08-23 21:42:29 +0000729 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000730 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000731 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000732 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000733
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000734 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
735 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000736 else if (Block) {
737 if (!FinishBlock(ElseBlock))
738 return 0;
739 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000740 }
Mike Stump31feda52009-07-17 01:31:16 +0000741
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000742 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000743 CFGBlock* ThenBlock;
744 {
745 Stmt* Then = I->getThen();
746 assert (Then);
747 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000748 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000749 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000750
Ted Kremenek1b379512009-04-01 03:52:47 +0000751 if (!ThenBlock) {
752 // We can reach here if the "then" body has all NullStmts.
753 // Create an empty block so we can distinguish between true and false
754 // branches in path-sensitive analyses.
755 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000756 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000757 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000758 if (!FinishBlock(ThenBlock))
759 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000760 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000761 }
762
Mike Stump31feda52009-07-17 01:31:16 +0000763 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000764 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000765
Ted Kremenek9aae5132007-08-23 21:42:29 +0000766 // Set the terminator of the new block to the If statement.
767 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000768
Mike Stump773582d2009-07-23 23:25:26 +0000769 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000770 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000771
Ted Kremenek9aae5132007-08-23 21:42:29 +0000772 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000773 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
774 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000775
776 // Add the condition as the last statement in the new block. This may create
777 // new blocks as the condition may contain control-flow. Any newly created
778 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000779 Block = addStmt(I->getCond());
780
781 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
782 // and the condition variable initialization to the CFG.
783 if (VarDecl *VD = I->getConditionVariable()) {
784 if (Expr *Init = VD->getInit()) {
785 autoCreateBlock();
786 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
787 addStmt(Init);
788 }
789 }
790
791 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000792}
Mike Stump31feda52009-07-17 01:31:16 +0000793
794
Ted Kremenek9aae5132007-08-23 21:42:29 +0000795CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000796 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000797 //
Mike Stump31feda52009-07-17 01:31:16 +0000798 // NOTE: If a "return" appears in the middle of a block, this means that the
799 // code afterwards is DEAD (unreachable). We still keep a basic block
800 // for that code; a simple "mark-and-sweep" from the entry block will be
801 // able to report such dead blocks.
Ted Kremenek0868eea2009-09-24 18:45:41 +0000802 if (Block)
803 FinishBlock(Block);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000804
805 // Create the new block.
806 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000807
Ted Kremenek9aae5132007-08-23 21:42:29 +0000808 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000809 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000810
811 // Add the return statement to the block. This may create new blocks if R
812 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000813 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000814}
815
816CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
817 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000818 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000819 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000820
Ted Kremenek93668002009-07-17 22:18:43 +0000821 if (!LabelBlock) // This can happen when the body is empty, i.e.
822 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000823
Ted Kremenek93668002009-07-17 22:18:43 +0000824 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000825 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000826
827 // Labels partition blocks, so this is the end of the basic block we were
828 // processing (L is the block's label). Because this is label (and we have
829 // already processed the substatement) there is no extra control-flow to worry
830 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000831 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000832 if (!FinishBlock(LabelBlock))
833 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000834
835 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000836 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000837
Ted Kremenek9aae5132007-08-23 21:42:29 +0000838 // This block is now the implicit successor of other blocks.
839 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000840
Ted Kremenek9aae5132007-08-23 21:42:29 +0000841 return LabelBlock;
842}
843
844CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000845 // Goto is a control-flow statement. Thus we stop processing the current
846 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000847 if (Block)
848 FinishBlock(Block);
849
Ted Kremenek9aae5132007-08-23 21:42:29 +0000850 Block = createBlock(false);
851 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000852
853 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000854 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000855
Ted Kremenek9aae5132007-08-23 21:42:29 +0000856 if (I == LabelMap.end())
857 // We will need to backpatch this block later.
858 BackpatchBlocks.push_back(Block);
859 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000860 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000861
Mike Stump31feda52009-07-17 01:31:16 +0000862 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000863}
864
865CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000866 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000867
Mike Stump014b3ea2009-07-21 01:12:51 +0000868 // "for" is a control-flow statement. Thus we stop processing the current
869 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000870 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000871 if (!FinishBlock(Block))
872 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000873 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000874 } else
875 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000876
877 // Because of short-circuit evaluation, the condition of the loop can span
878 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
879 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000880 CFGBlock* ExitConditionBlock = createBlock(false);
881 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000882
Ted Kremenek81e14852007-08-27 19:46:09 +0000883 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000884 ExitConditionBlock->setTerminator(F);
885
886 // Now add the actual condition to the condition block. Because the condition
887 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000888 if (Stmt* C = F->getCond()) {
889 Block = ExitConditionBlock;
890 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000891 if (Block) {
892 if (!FinishBlock(EntryConditionBlock))
893 return 0;
894 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000895 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000896
Mike Stump31feda52009-07-17 01:31:16 +0000897 // The condition block is the implicit successor for the loop body as well as
898 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000899 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000900
Mike Stump773582d2009-07-23 23:25:26 +0000901 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000902 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +0000903
Mike Stump773582d2009-07-23 23:25:26 +0000904 if (F->getCond())
905 KnownVal = TryEvaluateBool(F->getCond());
906
Ted Kremenek9aae5132007-08-23 21:42:29 +0000907 // Now create the loop body.
908 {
909 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000910
Ted Kremenek9aae5132007-08-23 21:42:29 +0000911 // Save the current values for Block, Succ, and continue and break targets
912 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
913 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000914 save_break(BreakTargetBlock);
915
Ted Kremeneke9610502007-08-30 18:39:40 +0000916 // Create a new block to contain the (bottom) of the loop body.
917 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000918
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000919 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000920 // Generate increment code in its own basic block. This is the target of
921 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000922 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000923 } else {
924 // No increment code. Create a special, empty, block that is used as the
925 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000926 assert(Succ == EntryConditionBlock);
927 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000928 }
Mike Stump31feda52009-07-17 01:31:16 +0000929
Ted Kremenek902393b2009-04-28 00:51:56 +0000930 // Finish up the increment (or empty) block if it hasn't been already.
931 if (Block) {
932 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000933 if (!FinishBlock(Block))
934 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000935 Block = 0;
936 }
Mike Stump31feda52009-07-17 01:31:16 +0000937
Ted Kremenek902393b2009-04-28 00:51:56 +0000938 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000939
Ted Kremenek902393b2009-04-28 00:51:56 +0000940 // The starting block for the loop increment is the block that should
941 // represent the 'loop target' for looping back to the start of the loop.
942 ContinueTargetBlock->setLoopTarget(F);
943
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000944 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000945 BreakTargetBlock = LoopSuccessor;
946
947 // Now populate the body block, and in the process create new blocks as we
948 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000949 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000950
951 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +0000952 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +0000953 else if (Block && !FinishBlock(BodyBlock))
954 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000955
Ted Kremenek30754282009-07-24 04:47:11 +0000956 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000957 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000958 }
Mike Stump31feda52009-07-17 01:31:16 +0000959
Ted Kremenek30754282009-07-24 04:47:11 +0000960 // Link up the condition block with the code that follows the loop. (the
961 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000962 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000963
Ted Kremenek9aae5132007-08-23 21:42:29 +0000964 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000965 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000966 if (Stmt* I = F->getInit()) {
967 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000968 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000969 } else {
970 // There is no loop initialization. We are thus basically a while loop.
971 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000972 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000973 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000974 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000975 }
976}
977
Ted Kremenek9d56e642008-11-11 17:10:00 +0000978CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
979 // Objective-C fast enumeration 'for' statements:
980 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
981 //
982 // for ( Type newVariable in collection_expression ) { statements }
983 //
984 // becomes:
985 //
986 // prologue:
987 // 1. collection_expression
988 // T. jump to loop_entry
989 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000990 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +0000991 // 1. ObjCForCollectionStmt [performs binding to newVariable]
992 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
993 // TB:
994 // statements
995 // T. jump to loop_entry
996 // FB:
997 // what comes after
998 //
999 // and
1000 //
1001 // Type existingItem;
1002 // for ( existingItem in expression ) { statements }
1003 //
1004 // becomes:
1005 //
Mike Stump31feda52009-07-17 01:31:16 +00001006 // the same with newVariable replaced with existingItem; the binding works
1007 // the same except that for one ObjCForCollectionStmt::getElement() returns
1008 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001009 //
Mike Stump31feda52009-07-17 01:31:16 +00001010
Ted Kremenek9d56e642008-11-11 17:10:00 +00001011 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001012
Ted Kremenek9d56e642008-11-11 17:10:00 +00001013 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001014 if (!FinishBlock(Block))
1015 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001016 LoopSuccessor = Block;
1017 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001018 } else
1019 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001020
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001021 // Build the condition blocks.
1022 CFGBlock* ExitConditionBlock = createBlock(false);
1023 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001024
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001025 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001026 ExitConditionBlock->setTerminator(S);
1027
1028 // The last statement in the block should be the ObjCForCollectionStmt, which
1029 // performs the actual binding to 'element' and determines if there are any
1030 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001031 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001032 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001033
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001034 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001035 // generate new blocks as necesary. We DON'T add the statement by default to
1036 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001037 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001038 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001039 if (!FinishBlock(EntryConditionBlock))
1040 return 0;
1041 Block = 0;
1042 }
Mike Stump31feda52009-07-17 01:31:16 +00001043
1044 // The condition block is the implicit successor for the loop body as well as
1045 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001046 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001047
Ted Kremenek9d56e642008-11-11 17:10:00 +00001048 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001049 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001050 // Save the current values for Succ, continue and break targets.
1051 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001052 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1053
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001054 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001055 ContinueTargetBlock = EntryConditionBlock;
1056
Ted Kremenek93668002009-07-17 22:18:43 +00001057 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001058
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001059 if (!BodyBlock)
1060 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001061 else if (Block) {
1062 if (!FinishBlock(BodyBlock))
1063 return 0;
1064 }
Mike Stump31feda52009-07-17 01:31:16 +00001065
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001066 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001067 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001068 }
Mike Stump31feda52009-07-17 01:31:16 +00001069
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001070 // Link up the condition block with the code that follows the loop.
1071 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001072 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001073
Ted Kremenek9d56e642008-11-11 17:10:00 +00001074 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001075 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001076 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001077}
1078
Ted Kremenek49805452009-05-02 01:49:13 +00001079CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1080 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001081
Ted Kremenek49805452009-05-02 01:49:13 +00001082 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001083 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001084
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001085 // The sync body starts its own basic block. This makes it a little easier
1086 // for diagnostic clients.
1087 if (SyncBlock) {
1088 if (!FinishBlock(SyncBlock))
1089 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001090
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001091 Block = 0;
1092 }
Mike Stump31feda52009-07-17 01:31:16 +00001093
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001094 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001095
Ted Kremenek49805452009-05-02 01:49:13 +00001096 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001097 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001098}
Mike Stump31feda52009-07-17 01:31:16 +00001099
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001100CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001101 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001102 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001103}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001104
Ted Kremenek9aae5132007-08-23 21:42:29 +00001105CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001106 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001107
Mike Stump014b3ea2009-07-21 01:12:51 +00001108 // "while" is a control-flow statement. Thus we stop processing the current
1109 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001110 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001111 if (!FinishBlock(Block))
1112 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001113 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001114 } else
1115 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001116
1117 // Because of short-circuit evaluation, the condition of the loop can span
1118 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1119 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001120 CFGBlock* ExitConditionBlock = createBlock(false);
1121 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001122
Ted Kremenek81e14852007-08-27 19:46:09 +00001123 // Set the terminator for the "exit" condition block.
1124 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001125
1126 // Now add the actual condition to the condition block. Because the condition
1127 // itself may contain control-flow, new blocks may be created. Thus we update
1128 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001129 if (Stmt* C = W->getCond()) {
1130 Block = ExitConditionBlock;
1131 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001132 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +00001133 if (Block) {
1134 if (!FinishBlock(EntryConditionBlock))
1135 return 0;
1136 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001137 }
Mike Stump31feda52009-07-17 01:31:16 +00001138
1139 // The condition block is the implicit successor for the loop body as well as
1140 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001141 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001142
Mike Stump773582d2009-07-23 23:25:26 +00001143 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001144 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001145
Ted Kremenek9aae5132007-08-23 21:42:29 +00001146 // Process the loop body.
1147 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001148 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001149
1150 // Save the current values for Block, Succ, and continue and break targets
1151 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1152 save_continue(ContinueTargetBlock),
1153 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001154
Mike Stump31feda52009-07-17 01:31:16 +00001155 // Create an empty block to represent the transition block for looping back
1156 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001157 Block = 0;
1158 assert(Succ == EntryConditionBlock);
1159 Succ = createBlock();
1160 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001161 ContinueTargetBlock = Succ;
1162
Ted Kremenek9aae5132007-08-23 21:42:29 +00001163 // All breaks should go to the code following the loop.
1164 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001165
Ted Kremenek9aae5132007-08-23 21:42:29 +00001166 // NULL out Block to force lazy instantiation of blocks for the body.
1167 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001168
Ted Kremenek9aae5132007-08-23 21:42:29 +00001169 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001170 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001171
Ted Kremeneke9610502007-08-30 18:39:40 +00001172 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001173 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001174 else if (Block) {
1175 if (!FinishBlock(BodyBlock))
1176 return 0;
1177 }
Mike Stump31feda52009-07-17 01:31:16 +00001178
Ted Kremenek30754282009-07-24 04:47:11 +00001179 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001180 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001181 }
Mike Stump31feda52009-07-17 01:31:16 +00001182
Ted Kremenek30754282009-07-24 04:47:11 +00001183 // Link up the condition block with the code that follows the loop. (the
1184 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001185 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001186
1187 // There can be no more statements in the condition block since we loop back
1188 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001189 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001190
Ted Kremenek9aae5132007-08-23 21:42:29 +00001191 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001192 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001193 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001194}
Mike Stump11289f42009-09-09 15:08:12 +00001195
1196
Ted Kremenek93668002009-07-17 22:18:43 +00001197CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1198 // FIXME: For now we pretend that @catch and the code it contains does not
1199 // exit.
1200 return Block;
1201}
Mike Stump31feda52009-07-17 01:31:16 +00001202
Ted Kremenek93041ba2008-12-09 20:20:09 +00001203CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1204 // FIXME: This isn't complete. We basically treat @throw like a return
1205 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001206
Ted Kremenek0868eea2009-09-24 18:45:41 +00001207 // If we were in the middle of a block we stop processing that block.
Ted Kremenek93668002009-07-17 22:18:43 +00001208 if (Block && !FinishBlock(Block))
1209 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001210
Ted Kremenek93041ba2008-12-09 20:20:09 +00001211 // Create the new block.
1212 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001213
Ted Kremenek93041ba2008-12-09 20:20:09 +00001214 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001215 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001216
1217 // Add the statement to the block. This may create new blocks if S contains
1218 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001219 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001220}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001221
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001222CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001223 // If we were in the middle of a block we stop processing that block.
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001224 if (Block && !FinishBlock(Block))
1225 return 0;
1226
1227 // Create the new block.
1228 Block = createBlock(false);
1229
1230 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001231 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001232
1233 // Add the statement to the block. This may create new blocks if S contains
1234 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001235 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001236}
1237
Ted Kremenek93668002009-07-17 22:18:43 +00001238CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001239 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001240
Mike Stump8d50b6a2009-07-21 01:27:50 +00001241 // "do...while" is a control-flow statement. Thus we stop processing the
1242 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001243 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001244 if (!FinishBlock(Block))
1245 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001246 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001247 } else
1248 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001249
1250 // Because of short-circuit evaluation, the condition of the loop can span
1251 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1252 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001253 CFGBlock* ExitConditionBlock = createBlock(false);
1254 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001255
Ted Kremenek81e14852007-08-27 19:46:09 +00001256 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001257 ExitConditionBlock->setTerminator(D);
1258
1259 // Now add the actual condition to the condition block. Because the condition
1260 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001261 if (Stmt* C = D->getCond()) {
1262 Block = ExitConditionBlock;
1263 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001264 if (Block) {
1265 if (!FinishBlock(EntryConditionBlock))
1266 return 0;
1267 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001268 }
Mike Stump31feda52009-07-17 01:31:16 +00001269
Ted Kremeneka1523a32008-02-27 07:20:00 +00001270 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001271 Succ = EntryConditionBlock;
1272
Mike Stump773582d2009-07-23 23:25:26 +00001273 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001274 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001275
Ted Kremenek9aae5132007-08-23 21:42:29 +00001276 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001277 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001278 {
1279 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001280
Ted Kremenek9aae5132007-08-23 21:42:29 +00001281 // Save the current values for Block, Succ, and continue and break targets
1282 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1283 save_continue(ContinueTargetBlock),
1284 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001285
Ted Kremenek9aae5132007-08-23 21:42:29 +00001286 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001287 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001288
Ted Kremenek9aae5132007-08-23 21:42:29 +00001289 // All breaks should go to the code following the loop.
1290 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001291
Ted Kremenek9aae5132007-08-23 21:42:29 +00001292 // NULL out Block to force lazy instantiation of blocks for the body.
1293 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001294
Ted Kremenek9aae5132007-08-23 21:42:29 +00001295 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001296 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001297
Ted Kremeneke9610502007-08-30 18:39:40 +00001298 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001299 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001300 else if (Block) {
1301 if (!FinishBlock(BodyBlock))
1302 return 0;
1303 }
Mike Stump31feda52009-07-17 01:31:16 +00001304
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001305 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001306 // ExitConditionBlock to represent the "loop back" transition. Create an
1307 // empty block to represent the transition block for looping back to the
1308 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001309 // FIXME: Can we do this more efficiently without adding another block?
1310 Block = NULL;
1311 Succ = BodyBlock;
1312 CFGBlock *LoopBackBlock = createBlock();
1313 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001314
Ted Kremenek30754282009-07-24 04:47:11 +00001315 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001316 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001317 }
Mike Stump31feda52009-07-17 01:31:16 +00001318
Ted Kremenek30754282009-07-24 04:47:11 +00001319 // Link up the condition block with the code that follows the loop.
1320 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001321 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001322
1323 // There can be no more statements in the body block(s) since we loop back to
1324 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001325 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001326
Ted Kremenek9aae5132007-08-23 21:42:29 +00001327 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001328 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001329 return BodyBlock;
1330}
1331
1332CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1333 // "continue" is a control-flow statement. Thus we stop processing the
1334 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001335 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001336 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001337
Ted Kremenek9aae5132007-08-23 21:42:29 +00001338 // Now create a new block that ends with the continue statement.
1339 Block = createBlock(false);
1340 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001341
Ted Kremenek9aae5132007-08-23 21:42:29 +00001342 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001343 // incomplete AST. This means the CFG cannot be constructed.
1344 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001345 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001346 else
1347 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001348
Ted Kremenek9aae5132007-08-23 21:42:29 +00001349 return Block;
1350}
Mike Stump11289f42009-09-09 15:08:12 +00001351
Ted Kremenek0747de62009-07-18 00:47:21 +00001352CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001353 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001354
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001355 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001356 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001357 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001358 }
Mike Stump11289f42009-09-09 15:08:12 +00001359
Ted Kremenek93668002009-07-17 22:18:43 +00001360 // VLA types have expressions that must be evaluated.
1361 if (E->isArgumentType()) {
1362 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1363 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1364 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001365 }
Mike Stump11289f42009-09-09 15:08:12 +00001366
Mike Stump31feda52009-07-17 01:31:16 +00001367 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001368}
Mike Stump11289f42009-09-09 15:08:12 +00001369
Ted Kremenek93668002009-07-17 22:18:43 +00001370/// VisitStmtExpr - Utility method to handle (nested) statement
1371/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001372CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1373 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001374 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001375 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001376 }
Ted Kremenek93668002009-07-17 22:18:43 +00001377 return VisitCompoundStmt(SE->getSubStmt());
1378}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001379
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001380CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001381 // "switch" is a control-flow statement. Thus we stop processing the current
1382 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001383 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001384
Ted Kremenek9aae5132007-08-23 21:42:29 +00001385 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001386 if (!FinishBlock(Block))
1387 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001388 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001389 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001390
1391 // Save the current "switch" context.
1392 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001393 save_break(BreakTargetBlock),
1394 save_default(DefaultCaseBlock);
1395
Mike Stump31feda52009-07-17 01:31:16 +00001396 // Set the "default" case to be the block after the switch statement. If the
1397 // switch statement contains a "default:", this value will be overwritten with
1398 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001399 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001400
Ted Kremenek9aae5132007-08-23 21:42:29 +00001401 // Create a new block that will contain the switch statement.
1402 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001403
Ted Kremenek9aae5132007-08-23 21:42:29 +00001404 // Now process the switch body. The code after the switch is the implicit
1405 // successor.
1406 Succ = SwitchSuccessor;
1407 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001408
1409 // When visiting the body, the case statements should automatically get linked
1410 // up to the switch. We also don't keep a pointer to the body, since all
1411 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001412 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001413 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001414 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001415 if (Block) {
1416 if (!FinishBlock(BodyBlock))
1417 return 0;
1418 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001419
Mike Stump31feda52009-07-17 01:31:16 +00001420 // If we have no "default:" case, the default transition is to the code
1421 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001422 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001423
Ted Kremenek81e14852007-08-27 19:46:09 +00001424 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001425 SwitchTerminatedBlock->setTerminator(Terminator);
1426 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001427 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001428
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001429 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001430}
1431
Ted Kremenek93668002009-07-17 22:18:43 +00001432CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001433 // CaseStmts are essentially labels, so they are the first statement in a
1434 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001435
Ted Kremenek93668002009-07-17 22:18:43 +00001436 if (CS->getSubStmt())
1437 addStmt(CS->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001438
Ted Kremenek55e91e82007-08-30 18:48:11 +00001439 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001440 if (!CaseBlock)
1441 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001442
1443 // Cases statements partition blocks, so this is the top of the basic block we
1444 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001445 CaseBlock->setLabel(CS);
1446
Ted Kremenek55957a82009-05-02 00:13:27 +00001447 if (!FinishBlock(CaseBlock))
1448 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001449
1450 // Add this block to the list of successors for the block with the switch
1451 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001452 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001453 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001454
Ted Kremenek9aae5132007-08-23 21:42:29 +00001455 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1456 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001457
Ted Kremenek9aae5132007-08-23 21:42:29 +00001458 // This block is now the implicit successor of other blocks.
1459 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001460
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001461 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001462}
Mike Stump31feda52009-07-17 01:31:16 +00001463
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001464CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001465 if (Terminator->getSubStmt())
1466 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001467
Ted Kremenek654c78f2008-02-13 22:05:39 +00001468 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001469
1470 if (!DefaultCaseBlock)
1471 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001472
1473 // Default statements partition blocks, so this is the top of the basic block
1474 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001475 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001476
Ted Kremenek55957a82009-05-02 00:13:27 +00001477 if (!FinishBlock(DefaultCaseBlock))
1478 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001479
Mike Stump31feda52009-07-17 01:31:16 +00001480 // Unlike case statements, we don't add the default block to the successors
1481 // for the switch statement immediately. This is done when we finish
1482 // processing the switch statement. This allows for the default case
1483 // (including a fall-through to the code after the switch statement) to always
1484 // be the last successor of a switch-terminated block.
1485
Ted Kremenek654c78f2008-02-13 22:05:39 +00001486 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1487 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001488
Ted Kremenek654c78f2008-02-13 22:05:39 +00001489 // This block is now the implicit successor of other blocks.
1490 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001491
1492 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001493}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001494
Ted Kremenekeda180e22007-08-28 19:26:49 +00001495CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001496 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001497 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001498
Ted Kremenekeda180e22007-08-28 19:26:49 +00001499 if (!IBlock) {
1500 IBlock = createBlock(false);
1501 cfg->setIndirectGotoBlock(IBlock);
1502 }
Mike Stump31feda52009-07-17 01:31:16 +00001503
Ted Kremenekeda180e22007-08-28 19:26:49 +00001504 // IndirectGoto is a control-flow statement. Thus we stop processing the
1505 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001506 if (Block && !FinishBlock(Block))
1507 return 0;
1508
Ted Kremenekeda180e22007-08-28 19:26:49 +00001509 Block = createBlock(false);
1510 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001511 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001512 return addStmt(I->getTarget());
1513}
1514
Ted Kremenek04cca642007-08-23 21:26:19 +00001515} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001516
Mike Stump31feda52009-07-17 01:31:16 +00001517/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1518/// no successors or predecessors. If this is the first block created in the
1519/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001520CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001521 bool first_block = begin() == end();
1522
1523 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001524 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1525 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1526 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001527
1528 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001529 if (first_block)
1530 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001531
1532 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001533 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001534}
1535
Ted Kremenek889073f2007-08-23 16:51:22 +00001536/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1537/// CFG is returned to the caller.
Mike Stump0d76d072009-07-20 23:24:15 +00001538CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001539 CFGBuilder Builder;
Mike Stump0d76d072009-07-20 23:24:15 +00001540 return Builder.buildCFG(Statement, C);
Ted Kremenek889073f2007-08-23 16:51:22 +00001541}
1542
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001543//===----------------------------------------------------------------------===//
1544// CFG: Queries for BlkExprs.
1545//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001546
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001547namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001548 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001549}
1550
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001551static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1552 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001553 return;
Mike Stump31feda52009-07-17 01:31:16 +00001554
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001555 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001556 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001557
Ted Kremenek95a123c2008-01-26 00:03:27 +00001558 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1559 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001560
Ted Kremenek95a123c2008-01-26 00:03:27 +00001561 FindSubExprAssignments(*I, Set);
1562 }
1563}
1564
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001565static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1566 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001567
1568 // Look for assignments that are used as subexpressions. These are the only
1569 // assignments that we want to *possibly* register as a block-level
1570 // expression. Basically, if an assignment occurs both in a subexpression and
1571 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001572 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001573
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001574 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001575 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001576 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001577
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001578 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001579
1580 // Iterate over the statements again on identify the Expr* and Stmt* at the
1581 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001582
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001583 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001584 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001585
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001586 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001587 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001588 // expression are really "statements" whose value is never used by
1589 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001590 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001591 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001592 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1593 // Special handling for statement expressions. The last statement in
1594 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001595 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001596 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001597 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001598 (*M)[C->body_back()] = x;
1599 }
1600 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001601
Ted Kremenek95a123c2008-01-26 00:03:27 +00001602 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001603 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001604 }
Mike Stump31feda52009-07-17 01:31:16 +00001605
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001606 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001607
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001608 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001609
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001610 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001611 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001612 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001613 }
1614 }
Mike Stump31feda52009-07-17 01:31:16 +00001615
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001616 return M;
1617}
1618
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001619CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1620 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001621 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001622
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001623 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001624 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001625
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001626 if (I == M->end()) return CFG::BlkExprNumTy();
1627 else return CFG::BlkExprNumTy(I->second);
1628}
1629
1630unsigned CFG::getNumBlkExprs() {
1631 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1632 return M->size();
1633 else {
1634 // We assume callers interested in the number of BlkExprs will want
1635 // the map constructed if it doesn't already exist.
1636 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1637 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1638 }
1639}
1640
Ted Kremenek6065ef62008-04-28 18:00:46 +00001641//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001642// Cleanup: CFG dstor.
1643//===----------------------------------------------------------------------===//
1644
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001645CFG::~CFG() {
1646 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1647}
Mike Stump31feda52009-07-17 01:31:16 +00001648
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001649//===----------------------------------------------------------------------===//
1650// CFG pretty printing
1651//===----------------------------------------------------------------------===//
1652
Ted Kremenek7e776b12007-08-22 18:22:34 +00001653namespace {
1654
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001655class StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001656
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001657 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1658 StmtMapTy StmtMap;
1659 signed CurrentBlock;
1660 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001661 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001662public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001663
Chris Lattnerc61089a2009-06-30 01:26:17 +00001664 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1665 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001666 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1667 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001668 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001669 BI != BEnd; ++BI, ++j )
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001670 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001671 }
1672 }
Mike Stump31feda52009-07-17 01:31:16 +00001673
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001674 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001675
Chris Lattnerc61089a2009-06-30 01:26:17 +00001676 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001677 void setBlockID(signed i) { CurrentBlock = i; }
1678 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001679
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001680 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001681
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001682 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001683
1684 if (I == StmtMap.end())
1685 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001686
1687 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001688 && I->second.second == CurrentStmt)
1689 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001690
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001691 OS << "[B" << I->second.first << "." << I->second.second << "]";
1692 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001693 }
1694};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001695} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001696
Chris Lattnerc61089a2009-06-30 01:26:17 +00001697
1698namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001699class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001700 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001701
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001702 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001703 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001704 PrintingPolicy Policy;
1705
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001706public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001707 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001708 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001709 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001710
Ted Kremenek9aae5132007-08-23 21:42:29 +00001711 void VisitIfStmt(IfStmt* I) {
1712 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001713 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001714 }
Mike Stump31feda52009-07-17 01:31:16 +00001715
Ted Kremenek9aae5132007-08-23 21:42:29 +00001716 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001717 void VisitStmt(Stmt* Terminator) {
1718 Terminator->printPretty(OS, Helper, Policy);
1719 }
1720
Ted Kremenek9aae5132007-08-23 21:42:29 +00001721 void VisitForStmt(ForStmt* F) {
1722 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001723 if (F->getInit()) OS << "...";
1724 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001725 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001726 OS << "; ";
1727 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001728 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001729 }
Mike Stump31feda52009-07-17 01:31:16 +00001730
Ted Kremenek9aae5132007-08-23 21:42:29 +00001731 void VisitWhileStmt(WhileStmt* W) {
1732 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001733 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001734 }
Mike Stump31feda52009-07-17 01:31:16 +00001735
Ted Kremenek9aae5132007-08-23 21:42:29 +00001736 void VisitDoStmt(DoStmt* D) {
1737 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001738 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001739 }
Mike Stump31feda52009-07-17 01:31:16 +00001740
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001741 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001742 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001743 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001744 }
Mike Stump31feda52009-07-17 01:31:16 +00001745
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001746 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001747 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001748 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001749 }
Mike Stump31feda52009-07-17 01:31:16 +00001750
Ted Kremenek391f94a2007-08-31 22:29:13 +00001751 void VisitChooseExpr(ChooseExpr* C) {
1752 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001753 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001754 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001755 }
Mike Stump31feda52009-07-17 01:31:16 +00001756
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001757 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1758 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001759 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001760 }
Mike Stump31feda52009-07-17 01:31:16 +00001761
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001762 void VisitBinaryOperator(BinaryOperator* B) {
1763 if (!B->isLogicalOp()) {
1764 VisitExpr(B);
1765 return;
1766 }
Mike Stump31feda52009-07-17 01:31:16 +00001767
Douglas Gregor7de59662009-05-29 20:38:28 +00001768 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001769
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001770 switch (B->getOpcode()) {
1771 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001772 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001773 return;
1774 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001775 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001776 return;
1777 default:
1778 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001779 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001780 }
Mike Stump31feda52009-07-17 01:31:16 +00001781
Ted Kremenek12687ff2007-08-27 21:54:41 +00001782 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001783 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001784 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001785};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001786} // end anonymous namespace
1787
Mike Stump31feda52009-07-17 01:31:16 +00001788
Chris Lattnerc61089a2009-06-30 01:26:17 +00001789static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1790 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001791 if (Helper) {
1792 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001793 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001794 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001795
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001796 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001797 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001798 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001799 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001800 return;
1801 }
1802 }
Mike Stump31feda52009-07-17 01:31:16 +00001803
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001804 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001805 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001806 if (B->getOpcode() == BinaryOperator::Comma) {
1807 OS << "... , ";
1808 Helper->handledStmt(B->getRHS(),OS);
1809 OS << '\n';
1810 return;
Mike Stump31feda52009-07-17 01:31:16 +00001811 }
1812 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001813 }
Mike Stump31feda52009-07-17 01:31:16 +00001814
Chris Lattnerc61089a2009-06-30 01:26:17 +00001815 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001816
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001817 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001818 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001819}
Mike Stump31feda52009-07-17 01:31:16 +00001820
Chris Lattnerc61089a2009-06-30 01:26:17 +00001821static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1822 const CFGBlock& B,
1823 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001824
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001825 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001826
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001827 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001828 OS << "\n [ B" << B.getBlockID();
1829
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001830 if (&B == &cfg->getEntry())
1831 OS << " (ENTRY) ]\n";
1832 else if (&B == &cfg->getExit())
1833 OS << " (EXIT) ]\n";
1834 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001835 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001836 else
1837 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001838
Ted Kremenek71eca012007-08-29 23:20:49 +00001839 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001840 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001841
1842 if (print_edges)
1843 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001844
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001845 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001846 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001847 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001848 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001849 C->getLHS()->printPretty(OS, Helper,
1850 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001851 if (C->getRHS()) {
1852 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001853 C->getRHS()->printPretty(OS, Helper,
1854 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001855 }
Mike Stump31feda52009-07-17 01:31:16 +00001856 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001857 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001858 else
1859 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001860
Ted Kremenek71eca012007-08-29 23:20:49 +00001861 OS << ":\n";
1862 }
Mike Stump31feda52009-07-17 01:31:16 +00001863
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001864 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001865 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001866
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001867 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1868 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001869
Ted Kremenek71eca012007-08-29 23:20:49 +00001870 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001871 if (print_edges)
1872 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001873
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001874 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001875
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001876 if (Helper)
1877 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001878
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001879 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001880 }
Mike Stump31feda52009-07-17 01:31:16 +00001881
Ted Kremenek71eca012007-08-29 23:20:49 +00001882 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001883 if (B.getTerminator()) {
1884 if (print_edges)
1885 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001886
Ted Kremenek71eca012007-08-29 23:20:49 +00001887 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001888
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001889 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001890
Chris Lattnerc61089a2009-06-30 01:26:17 +00001891 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1892 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001893 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001894 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001895 }
Mike Stump31feda52009-07-17 01:31:16 +00001896
Ted Kremenek71eca012007-08-29 23:20:49 +00001897 if (print_edges) {
1898 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001899 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001900 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001901
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001902 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1903 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001904
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001905 if (i == 8 || (i-8) == 0)
1906 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001907
Ted Kremenek71eca012007-08-29 23:20:49 +00001908 OS << " B" << (*I)->getBlockID();
1909 }
Mike Stump31feda52009-07-17 01:31:16 +00001910
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001911 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001912
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001913 // Print the successors of this block.
1914 OS << " Successors (" << B.succ_size() << "):";
1915 i = 0;
1916
1917 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1918 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001919
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001920 if (i == 8 || (i-8) % 10 == 0)
1921 OS << "\n ";
1922
Mike Stump0d76d072009-07-20 23:24:15 +00001923 if (*I)
1924 OS << " B" << (*I)->getBlockID();
1925 else
1926 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001927 }
Mike Stump31feda52009-07-17 01:31:16 +00001928
Ted Kremenek71eca012007-08-29 23:20:49 +00001929 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001930 }
Mike Stump31feda52009-07-17 01:31:16 +00001931}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001932
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001933
1934/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001935void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001936
1937/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001938void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1939 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001940
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001941 // Print the entry block.
1942 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001943
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001944 // Iterate through the CFGBlocks and print them one by one.
1945 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1946 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001947 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001948 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001949
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001950 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001951 }
Mike Stump31feda52009-07-17 01:31:16 +00001952
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001953 // Print the exit block.
1954 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001955 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001956}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001957
1958/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001959void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1960 print(llvm::errs(), cfg, LO);
1961}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001962
1963/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1964/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001965void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1966 const LangOptions &LO) const {
1967 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001968 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00001969}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001970
Ted Kremenek15647632008-01-30 23:02:42 +00001971/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001972void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00001973 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001974 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00001975 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1976}
1977
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001978Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00001979
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001980 if (!Terminator)
1981 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001982
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001983 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001984
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001985 switch (Terminator->getStmtClass()) {
1986 default:
1987 break;
Mike Stump31feda52009-07-17 01:31:16 +00001988
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001989 case Stmt::ForStmtClass:
1990 E = cast<ForStmt>(Terminator)->getCond();
1991 break;
Mike Stump31feda52009-07-17 01:31:16 +00001992
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001993 case Stmt::WhileStmtClass:
1994 E = cast<WhileStmt>(Terminator)->getCond();
1995 break;
Mike Stump31feda52009-07-17 01:31:16 +00001996
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001997 case Stmt::DoStmtClass:
1998 E = cast<DoStmt>(Terminator)->getCond();
1999 break;
Mike Stump31feda52009-07-17 01:31:16 +00002000
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002001 case Stmt::IfStmtClass:
2002 E = cast<IfStmt>(Terminator)->getCond();
2003 break;
Mike Stump31feda52009-07-17 01:31:16 +00002004
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002005 case Stmt::ChooseExprClass:
2006 E = cast<ChooseExpr>(Terminator)->getCond();
2007 break;
Mike Stump31feda52009-07-17 01:31:16 +00002008
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002009 case Stmt::IndirectGotoStmtClass:
2010 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2011 break;
Mike Stump31feda52009-07-17 01:31:16 +00002012
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002013 case Stmt::SwitchStmtClass:
2014 E = cast<SwitchStmt>(Terminator)->getCond();
2015 break;
Mike Stump31feda52009-07-17 01:31:16 +00002016
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002017 case Stmt::ConditionalOperatorClass:
2018 E = cast<ConditionalOperator>(Terminator)->getCond();
2019 break;
Mike Stump31feda52009-07-17 01:31:16 +00002020
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002021 case Stmt::BinaryOperatorClass: // '&&' and '||'
2022 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002023 break;
Mike Stump31feda52009-07-17 01:31:16 +00002024
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002025 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002026 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002027 }
Mike Stump31feda52009-07-17 01:31:16 +00002028
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002029 return E ? E->IgnoreParens() : NULL;
2030}
2031
Ted Kremenek92137a32008-05-16 16:06:00 +00002032bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002033
Ted Kremenek92137a32008-05-16 16:06:00 +00002034 if (!Terminator)
2035 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002036
Ted Kremenek92137a32008-05-16 16:06:00 +00002037 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002038
Ted Kremenek92137a32008-05-16 16:06:00 +00002039 switch (Terminator->getStmtClass()) {
2040 default:
2041 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002042
2043 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002044 case Stmt::WhileStmtClass:
2045 case Stmt::DoStmtClass:
2046 case Stmt::IfStmtClass:
2047 case Stmt::ChooseExprClass:
2048 case Stmt::ConditionalOperatorClass:
2049 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002050 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002051 }
Mike Stump31feda52009-07-17 01:31:16 +00002052
Ted Kremenek92137a32008-05-16 16:06:00 +00002053 return E ? E->IgnoreParens() : NULL;
2054}
2055
Ted Kremenek15647632008-01-30 23:02:42 +00002056
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002057//===----------------------------------------------------------------------===//
2058// CFG Graphviz Visualization
2059//===----------------------------------------------------------------------===//
2060
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002061
2062#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002063static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002064#endif
2065
Chris Lattnerc61089a2009-06-30 01:26:17 +00002066void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002067#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002068 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002069 GraphHelper = &H;
2070 llvm::ViewGraph(this,"CFG");
2071 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002072#endif
2073}
2074
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002075namespace llvm {
2076template<>
2077struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002078
2079 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2080
2081 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002082
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002083#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002084 std::string OutSStr;
2085 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002086 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002087 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002088
2089 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2090
2091 // Process string output to make it nicer...
2092 for (unsigned i = 0; i != OutStr.length(); ++i)
2093 if (OutStr[i] == '\n') { // Left justify
2094 OutStr[i] = '\\';
2095 OutStr.insert(OutStr.begin()+i+1, 'l');
2096 }
Mike Stump31feda52009-07-17 01:31:16 +00002097
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002098 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002099#else
2100 return "";
2101#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002102 }
2103};
2104} // end namespace llvm