blob: 4eac9ae351b72a27d392c294bc4274a217e39bac [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"
Mike Stump6bf1c082010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000020#include "llvm/Support/GraphWriter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000021#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek8aed4902009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000026
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor6e6ad602009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000037}
Ted Kremenekdc03bd02010-08-02 23:46:59 +000038
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000039class AddStmtChoice {
40public:
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000041 enum Kind { NotAlwaysAdd = 0,
42 AlwaysAdd = 1,
43 AsLValueNotAlwaysAdd = 2,
44 AlwaysAddAsLValue = 3 };
45
Benjamin Kramera3b13412010-03-03 16:28:47 +000046 AddStmtChoice(Kind kind) : k(kind) {}
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000047
Benjamin Kramera3b13412010-03-03 16:28:47 +000048 bool alwaysAdd() const { return (unsigned)k & 0x1; }
Ted Kremenek66de6032010-04-11 17:02:04 +000049 bool asLValue() const { return k >= AsLValueNotAlwaysAdd; }
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000050
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000051private:
Benjamin Kramera3b13412010-03-03 16:28:47 +000052 Kind k;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000053};
Mike Stump31feda52009-07-17 01:31:16 +000054
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000055/// LocalScope - Node in tree of local scopes created for C++ implicit
56/// destructor calls generation. It contains list of automatic variables
57/// declared in the scope and link to position in previous scope this scope
58/// began in.
59///
60/// The process of creating local scopes is as follows:
61/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
62/// - Before processing statements in scope (e.g. CompoundStmt) create
63/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
64/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000065/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000066/// at this VarDecl,
67/// - For every normal (without jump) end of scope add to CFGBlock destructors
68/// for objects in the current scope,
69/// - For every jump add to CFGBlock destructors for objects
70/// between CFGBuilder::ScopePos and local scope position saved for jump
71/// target. Thanks to C++ restrictions on goto jumps we can be sure that
72/// jump target position will be on the path to root from CFGBuilder::ScopePos
73/// (adding any variable that doesn't need constructor to be called to
74/// LocalScope can break this assumption),
75///
76class LocalScope {
77public:
78 typedef llvm::SmallVector<VarDecl*, 4> AutomaticVarsTy;
79
80 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000081 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000082 class const_iterator {
83 const LocalScope* Scope;
84
85 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
86 /// Invalid iterator (with null Scope) has VarIter equal to 0.
87 unsigned VarIter;
88
89 public:
90 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
91 /// Incrementing invalid iterator is allowed and will result in invalid
92 /// iterator.
93 const_iterator()
94 : Scope(NULL), VarIter(0) {}
95
96 /// Create valid iterator. In case when S.Prev is an invalid iterator and
97 /// I is equal to 0, this will create invalid iterator.
98 const_iterator(const LocalScope& S, unsigned I)
99 : Scope(&S), VarIter(I) {
100 // Iterator to "end" of scope is not allowed. Handle it by going up
101 // in scopes tree possibly up to invalid iterator in the root.
102 if (VarIter == 0 && Scope)
103 *this = Scope->Prev;
104 }
105
106 VarDecl* const* operator->() const {
107 assert (Scope && "Dereferencing invalid iterator is not allowed");
108 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
109 return &Scope->Vars[VarIter - 1];
110 }
111 VarDecl* operator*() const {
112 return *this->operator->();
113 }
114
115 const_iterator& operator++() {
116 if (!Scope)
117 return *this;
118
119 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
120 --VarIter;
121 if (VarIter == 0)
122 *this = Scope->Prev;
123 return *this;
124 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000125 const_iterator operator++(int) {
126 const_iterator P = *this;
127 ++*this;
128 return P;
129 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000130
131 bool operator==(const const_iterator& rhs) const {
132 return Scope == rhs.Scope && VarIter == rhs.VarIter;
133 }
134 bool operator!=(const const_iterator& rhs) const {
135 return !(*this == rhs);
136 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000137
138 operator bool() const {
139 return *this != const_iterator();
140 }
141
142 int distance(const_iterator L);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000143 };
144
145 friend class const_iterator;
146
147private:
148 /// Automatic variables in order of declaration.
149 AutomaticVarsTy Vars;
150 /// Iterator to variable in previous scope that was declared just before
151 /// begin of this scope.
152 const_iterator Prev;
153
154public:
155 /// Constructs empty scope linked to previous scope in specified place.
156 LocalScope(const_iterator P)
157 : Vars()
158 , Prev(P) {}
159
160 /// Begin of scope in direction of CFG building (backwards).
161 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000162
163 void addVar(VarDecl* VD) {
164 Vars.push_back(VD);
165 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000166};
167
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000168/// distance - Calculates distance from this to L. L must be reachable from this
169/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
170/// number of scopes between this and L.
171int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
172 int D = 0;
173 const_iterator F = *this;
174 while (F.Scope != L.Scope) {
175 assert (F != const_iterator()
176 && "L iterator is not reachable from F iterator.");
177 D += F.VarIter;
178 F = F.Scope->Prev;
179 }
180 D += F.VarIter - L.VarIter;
181 return D;
182}
183
184/// BlockScopePosPair - Structure for specifying position in CFG during its
185/// build process. It consists of CFGBlock that specifies position in CFG graph
186/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000187struct BlockScopePosPair {
188 BlockScopePosPair() {}
189 BlockScopePosPair(CFGBlock* B, LocalScope::const_iterator S)
190 : Block(B), ScopePos(S) {}
191
192 CFGBlock* Block;
193 LocalScope::const_iterator ScopePos;
194};
195
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000196/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000197/// The builder is stateful: an instance of the builder should be used to only
198/// construct a single CFG.
199///
200/// Example usage:
201///
202/// CFGBuilder builder;
203/// CFG* cfg = builder.BuildAST(stmt1);
204///
Mike Stump31feda52009-07-17 01:31:16 +0000205/// CFG construction is done via a recursive walk of an AST. We actually parse
206/// the AST in reverse order so that the successor of a basic block is
207/// constructed prior to its predecessor. This allows us to nicely capture
208/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +0000209///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000210class CFGBuilder {
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000211 typedef BlockScopePosPair JumpTarget;
212 typedef BlockScopePosPair JumpSource;
213
Mike Stump0d76d072009-07-20 23:24:15 +0000214 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000215 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000216
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000217 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000218 CFGBlock* Succ;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000219 JumpTarget ContinueJumpTarget;
220 JumpTarget BreakJumpTarget;
Ted Kremenek879d8e12007-08-23 18:43:24 +0000221 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +0000222 CFGBlock* DefaultCaseBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000223 CFGBlock* TryTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000224
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000225 // Current position in local scope.
226 LocalScope::const_iterator ScopePos;
227
228 // LabelMap records the mapping from Label expressions to their jump targets.
229 typedef llvm::DenseMap<LabelStmt*, JumpTarget> LabelMapTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000230 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +0000231
232 // A list of blocks that end with a "goto" that must be backpatched to their
233 // resolved targets upon completion of CFG construction.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000234 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000235 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +0000236
Ted Kremenekeda180e22007-08-28 19:26:49 +0000237 // A list of labels whose address has been taken (for indirect gotos).
238 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
239 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +0000240
Zhongxing Xud38fb842010-09-16 03:28:18 +0000241 bool badCFG;
242 CFG::BuildOptions BuildOpts;
243
Mike Stump31feda52009-07-17 01:31:16 +0000244public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000245 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
246 Block(NULL), Succ(NULL),
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000247 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xud38fb842010-09-16 03:28:18 +0000248 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump31feda52009-07-17 01:31:16 +0000249
Ted Kremenek9aae5132007-08-23 21:42:29 +0000250 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000251 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000252 CFG::BuildOptions BO);
Mike Stump31feda52009-07-17 01:31:16 +0000253
Ted Kremenek93668002009-07-17 22:18:43 +0000254private:
255 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000256 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
257 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
258 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000259 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000260 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
261 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
262 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000263 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000264 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000265 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000266 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000267 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000268 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000269 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000270 CFGBlock *VisitDeclStmt(DeclStmt *DS);
271 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000272 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
273 CFGBlock *VisitDoStmt(DoStmt *D);
274 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000275 CFGBlock *VisitGotoStmt(GotoStmt* G);
276 CFGBlock *VisitIfStmt(IfStmt *I);
277 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
278 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000279 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000280 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
281 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
282 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
283 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
284 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
285 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000286 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
287 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000288 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
289 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000290
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000291 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
292 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000293 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000294
Ted Kremenek6065ef62008-04-28 18:00:46 +0000295 // NYS == Not Yet Supported
296 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000297 badCFG = true;
298 return Block;
299 }
Mike Stump31feda52009-07-17 01:31:16 +0000300
Ted Kremenek93668002009-07-17 22:18:43 +0000301 void autoCreateBlock() { if (!Block) Block = createBlock(); }
302 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000303
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000304 CFGBlock *addStmt(Stmt *S) {
305 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000306 }
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000307 CFGBlock *addInitializer(CXXBaseOrMemberInitializer *I);
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000308 void addAutomaticObjDtors(LocalScope::const_iterator B,
309 LocalScope::const_iterator E, Stmt* S);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000310
Marcin Swiderski5e415732010-09-30 23:05:00 +0000311 // Local scopes creation.
312 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
313
Zhongxing Xu81714f22010-10-01 03:00:16 +0000314 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000315 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
316 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
317
318 void addLocalScopeAndDtors(Stmt* S);
319
320 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000321 void AppendStmt(CFGBlock *B, Stmt *S,
322 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
323 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000324 }
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000325 void appendInitializer(CFGBlock *B, CXXBaseOrMemberInitializer *I) {
326 B->appendInitializer(I, cfg->getBumpVectorContext());
327 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000328
Marcin Swiderski321a7072010-09-30 22:54:37 +0000329 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
330 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
331 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
332 LocalScope::const_iterator E, Stmt* S);
333 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
334 LocalScope::const_iterator B, LocalScope::const_iterator E);
335
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000336 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
337 B->addSuccessor(S, cfg->getBumpVectorContext());
338 }
Mike Stump11289f42009-09-09 15:08:12 +0000339
Ted Kremenek963cc312009-07-24 06:55:42 +0000340 /// TryResult - a class representing a variant over the values
341 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
342 /// and is used by the CFGBuilder to decide if a branch condition
343 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000344 class TryResult {
345 int X;
346 public:
347 TryResult(bool b) : X(b ? 1 : 0) {}
348 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000349
Ted Kremenek30754282009-07-24 04:47:11 +0000350 bool isTrue() const { return X == 1; }
351 bool isFalse() const { return X == 0; }
352 bool isKnown() const { return X >= 0; }
353 void negate() {
354 assert(isKnown());
355 X ^= 0x1;
356 }
357 };
Mike Stump11289f42009-09-09 15:08:12 +0000358
Mike Stump773582d2009-07-23 23:25:26 +0000359 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
360 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000361 TryResult TryEvaluateBool(Expr *S) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000362 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000363 return TryResult();
364
Mike Stump773582d2009-07-23 23:25:26 +0000365 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000366 if (!S->isTypeDependent() && !S->isValueDependent() &&
367 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000368 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000369
370 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000371 }
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000372};
Mike Stump31feda52009-07-17 01:31:16 +0000373
Douglas Gregor4619e432008-12-05 23:32:09 +0000374// FIXME: Add support for dependent-sized array types in C++?
375// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000376static VariableArrayType* FindVA(Type* t) {
377 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
378 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
379 if (vat->getSizeExpr())
380 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000381
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000382 t = vt->getElementType().getTypePtr();
383 }
Mike Stump31feda52009-07-17 01:31:16 +0000384
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000385 return 0;
386}
Mike Stump31feda52009-07-17 01:31:16 +0000387
388/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
389/// arbitrary statement. Examples include a single expression or a function
390/// body (compound statement). The ownership of the returned CFG is
391/// transferred to the caller. If CFG construction fails, this method returns
392/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000393CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000394 CFG::BuildOptions BO) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000395
Mike Stump0d76d072009-07-20 23:24:15 +0000396 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000397 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000398 if (!Statement)
399 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000400
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000401 BuildOpts = BO;
Mike Stump31feda52009-07-17 01:31:16 +0000402
403 // Create an empty block that will serve as the exit block for the CFG. Since
404 // this is the first block added to the CFG, it will be implicitly registered
405 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000406 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000407 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000408 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000409
Ted Kremenek9aae5132007-08-23 21:42:29 +0000410 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000411 CFGBlock *B = addStmt(Statement);
412
413 if (badCFG)
414 return NULL;
415
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000416 // For C++ constructor add initializers to CFG.
417 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
418 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
419 E = CD->init_rend(); I != E; ++I) {
420 B = addInitializer(*I);
421 if (badCFG)
422 return NULL;
423 }
424 }
425
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000426 if (B)
427 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000428
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000429 // Backpatch the gotos whose label -> block mappings we didn't know when we
430 // encountered them.
431 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
432 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000433
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000434 CFGBlock* B = I->Block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000435 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
436 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000437
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000438 // If there is no target for the goto, then we are looking at an
439 // incomplete AST. Handle this by not registering a successor.
440 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000441
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000442 JumpTarget JT = LI->second;
Marcin Swiderski667ffec2010-10-01 00:23:17 +0000443 prependAutomaticObjDtorsWithTerminator(B, I->ScopePos, JT.ScopePos);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000444 AddSuccessor(B, JT.Block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000445 }
446
447 // Add successors to the Indirect Goto Dispatch block (if we have one).
448 if (CFGBlock* B = cfg->getIndirectGotoBlock())
449 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
450 E = AddressTakenLabels.end(); I != E; ++I ) {
451
452 // Lookup the target block.
453 LabelMapTy::iterator LI = LabelMap.find(*I);
454
455 // If there is no target block that contains label, then we are looking
456 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000457 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000458
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000459 AddSuccessor(B, LI->second.Block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000460 }
Mike Stump31feda52009-07-17 01:31:16 +0000461
Mike Stump31feda52009-07-17 01:31:16 +0000462 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000463 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000464
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000465 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000466}
Mike Stump31feda52009-07-17 01:31:16 +0000467
Ted Kremenek9aae5132007-08-23 21:42:29 +0000468/// createBlock - Used to lazily create blocks that are connected
469/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000470CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000471 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000472 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000473 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000474 return B;
475}
Mike Stump31feda52009-07-17 01:31:16 +0000476
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000477/// addInitializer - Add C++ base or member initializer element to CFG.
478CFGBlock *CFGBuilder::addInitializer(CXXBaseOrMemberInitializer *I) {
479 if (!BuildOpts.AddInitializers)
480 return Block;
481
482 autoCreateBlock();
483 appendInitializer(Block, I);
484
485 if (Expr *Init = I->getInit()) {
486 AddStmtChoice::Kind K = AddStmtChoice::NotAlwaysAdd;
487 if (FieldDecl *FD = I->getMember())
488 if (FD->getType()->isReferenceType())
489 K = AddStmtChoice::AsLValueNotAlwaysAdd;
490
491 return Visit(Init, AddStmtChoice(K));
492 }
493
494 return Block;
495}
496
Marcin Swiderski5e415732010-09-30 23:05:00 +0000497/// addAutomaticObjDtors - Add to current block automatic objects destructors
498/// for objects in range of local scope positions. Use S as trigger statement
499/// for destructors.
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000500void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
501 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000502 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000503 return;
504
Marcin Swiderski5e415732010-09-30 23:05:00 +0000505 if (B == E)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000506 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000507
508 autoCreateBlock();
509 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000510}
511
512/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
513/// way return valid LocalScope object.
514LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
515 if (!Scope) {
516 Scope = cfg->getAllocator().Allocate<LocalScope>();
517 new (Scope) LocalScope(ScopePos);
518 }
519 return Scope;
520}
521
522/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu81714f22010-10-01 03:00:16 +0000523/// that should create implicit scope (e.g. if/else substatements).
524void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000525 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu81714f22010-10-01 03:00:16 +0000526 return;
527
528 LocalScope *Scope = 0;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000529
530 // For compound statement we will be creating explicit scope.
531 if (CompoundStmt* CS = dyn_cast<CompoundStmt>(S)) {
532 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
533 ; BI != BE; ++BI) {
534 Stmt* SI = *BI;
535 if (LabelStmt* LS = dyn_cast<LabelStmt>(SI))
536 SI = LS->getSubStmt();
537 if (DeclStmt* DS = dyn_cast<DeclStmt>(SI))
538 Scope = addLocalScopeForDeclStmt(DS, Scope);
539 }
Zhongxing Xu81714f22010-10-01 03:00:16 +0000540 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000541 }
542
543 // For any other statement scope will be implicit and as such will be
544 // interesting only for DeclStmt.
545 if (LabelStmt* LS = dyn_cast<LabelStmt>(S))
546 S = LS->getSubStmt();
547 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Zhongxing Xu307701e2010-10-01 03:09:09 +0000548 addLocalScopeForDeclStmt(DS);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000549}
550
551/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
552/// reuse Scope if not NULL.
553LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000554 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000555 if (!BuildOpts.AddImplicitDtors)
556 return Scope;
557
558 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
559 ; DI != DE; ++DI) {
560 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
561 Scope = addLocalScopeForVarDecl(VD, Scope);
562 }
563 return Scope;
564}
565
566/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
567/// create add scope for automatic objects and temporary objects bound to
568/// const reference. Will reuse Scope if not NULL.
569LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000570 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000571 if (!BuildOpts.AddImplicitDtors)
572 return Scope;
573
574 // Check if variable is local.
575 switch (VD->getStorageClass()) {
576 case SC_None:
577 case SC_Auto:
578 case SC_Register:
579 break;
580 default: return Scope;
581 }
582
583 // Check for const references bound to temporary. Set type to pointee.
584 QualType QT = VD->getType();
585 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
586 QT = RT->getPointeeType();
587 if (!QT.isConstQualified())
588 return Scope;
589 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
590 return Scope;
591 }
592
593 // Check if type is a C++ class with non-trivial destructor.
Marcin Swiderskie84cb972010-10-01 00:31:22 +0000594 if (const RecordType* RT = QT.getTypePtr()->getAs<RecordType>())
595 if (const CXXRecordDecl* CD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
Zhongxing Xuea360a32010-10-01 02:47:11 +0000596 if (!CD->hasTrivialDestructor()) {
597 // Add the variable to scope
598 Scope = createOrReuseLocalScope(Scope);
599 Scope->addVar(VD);
600 ScopePos = Scope->begin();
601 }
Marcin Swiderski5e415732010-09-30 23:05:00 +0000602 return Scope;
603}
604
605/// addLocalScopeAndDtors - For given statement add local scope for it and
606/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
607void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
608 if (!BuildOpts.AddImplicitDtors)
609 return;
610
611 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu81714f22010-10-01 03:00:16 +0000612 addLocalScopeForStmt(S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000613 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
614}
615
Marcin Swiderski321a7072010-09-30 22:54:37 +0000616/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
617/// automatic storage duration to CFGBlock's elements vector. Insertion will be
618/// performed in place specified with iterator.
619void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
620 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
621 BumpVectorContext& C = cfg->getBumpVectorContext();
622 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
623 while (B != E)
624 I = Blk->insertAutomaticObjDtor(I, *B++, S);
625}
626
627/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
628/// automatic storage duration to CFGBlock's elements vector. Elements will be
629/// appended to physical end of the vector which happens to be logical
630/// beginning.
631void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
632 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
633 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
634}
635
636/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
637/// variables with automatic storage duration to CFGBlock's elements vector.
638/// Elements will be prepended to physical beginning of the vector which
639/// happens to be logical end. Use blocks terminator as statement that specifies
640/// destructors call site.
641void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
642 LocalScope::const_iterator B, LocalScope::const_iterator E) {
643 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
644}
645
Ted Kremenek93668002009-07-17 22:18:43 +0000646/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000647/// blocks for ternary operators, &&, and ||. We also process "," and
648/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000649CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000650tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000651 if (!S) {
652 badCFG = true;
653 return 0;
654 }
Ted Kremenek93668002009-07-17 22:18:43 +0000655 switch (S->getStmtClass()) {
656 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000657 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000658
659 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000660 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000661
Ted Kremenek93668002009-07-17 22:18:43 +0000662 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000663 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000664
Ted Kremenek93668002009-07-17 22:18:43 +0000665 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000666 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000667
Ted Kremenek93668002009-07-17 22:18:43 +0000668 case Stmt::BreakStmtClass:
669 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000670
Ted Kremenek93668002009-07-17 22:18:43 +0000671 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000672 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000673 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000674
Ted Kremenek93668002009-07-17 22:18:43 +0000675 case Stmt::CaseStmtClass:
676 return VisitCaseStmt(cast<CaseStmt>(S));
677
678 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000679 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000680
Ted Kremenek93668002009-07-17 22:18:43 +0000681 case Stmt::CompoundStmtClass:
682 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000683
Ted Kremenek93668002009-07-17 22:18:43 +0000684 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000685 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000686
Ted Kremenek93668002009-07-17 22:18:43 +0000687 case Stmt::ContinueStmtClass:
688 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000689
Ted Kremenekb27378c2010-01-19 20:40:33 +0000690 case Stmt::CXXCatchStmtClass:
691 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
692
Ted Kremenek82bfc862010-08-28 00:19:02 +0000693 case Stmt::CXXExprWithTemporariesClass: {
694 // FIXME: Handle temporaries. For now, just visit the subexpression
695 // so we don't artificially create extra blocks.
Ted Kremenek128d04d2010-08-31 18:47:34 +0000696 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000697 }
698
Zhongxing Xu7e612172010-04-13 09:38:01 +0000699 case Stmt::CXXMemberCallExprClass:
700 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
701
Ted Kremenekb27378c2010-01-19 20:40:33 +0000702 case Stmt::CXXThrowExprClass:
703 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000704
Ted Kremenekb27378c2010-01-19 20:40:33 +0000705 case Stmt::CXXTryStmtClass:
706 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000707
Ted Kremenek93668002009-07-17 22:18:43 +0000708 case Stmt::DeclStmtClass:
709 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000710
Ted Kremenek93668002009-07-17 22:18:43 +0000711 case Stmt::DefaultStmtClass:
712 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000713
Ted Kremenek93668002009-07-17 22:18:43 +0000714 case Stmt::DoStmtClass:
715 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000716
Ted Kremenek93668002009-07-17 22:18:43 +0000717 case Stmt::ForStmtClass:
718 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000719
Ted Kremenek93668002009-07-17 22:18:43 +0000720 case Stmt::GotoStmtClass:
721 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000722
Ted Kremenek93668002009-07-17 22:18:43 +0000723 case Stmt::IfStmtClass:
724 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000725
Ted Kremenek93668002009-07-17 22:18:43 +0000726 case Stmt::IndirectGotoStmtClass:
727 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000728
Ted Kremenek93668002009-07-17 22:18:43 +0000729 case Stmt::LabelStmtClass:
730 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000731
Ted Kremenek5868ec62010-04-11 17:02:10 +0000732 case Stmt::MemberExprClass:
733 return VisitMemberExpr(cast<MemberExpr>(S), asc);
734
Ted Kremenek93668002009-07-17 22:18:43 +0000735 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000736 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
737
Ted Kremenek93668002009-07-17 22:18:43 +0000738 case Stmt::ObjCAtSynchronizedStmtClass:
739 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000740
Ted Kremenek93668002009-07-17 22:18:43 +0000741 case Stmt::ObjCAtThrowStmtClass:
742 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000743
Ted Kremenek93668002009-07-17 22:18:43 +0000744 case Stmt::ObjCAtTryStmtClass:
745 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000746
Ted Kremenek93668002009-07-17 22:18:43 +0000747 case Stmt::ObjCForCollectionStmtClass:
748 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000749
Ted Kremenek93668002009-07-17 22:18:43 +0000750 case Stmt::ParenExprClass:
751 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000752 goto tryAgain;
753
Ted Kremenek93668002009-07-17 22:18:43 +0000754 case Stmt::NullStmtClass:
755 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000756
Ted Kremenek93668002009-07-17 22:18:43 +0000757 case Stmt::ReturnStmtClass:
758 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000759
Ted Kremenek93668002009-07-17 22:18:43 +0000760 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000761 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000762
Ted Kremenek93668002009-07-17 22:18:43 +0000763 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000764 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000765
Ted Kremenek93668002009-07-17 22:18:43 +0000766 case Stmt::SwitchStmtClass:
767 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000768
Ted Kremenek93668002009-07-17 22:18:43 +0000769 case Stmt::WhileStmtClass:
770 return VisitWhileStmt(cast<WhileStmt>(S));
771 }
772}
Mike Stump11289f42009-09-09 15:08:12 +0000773
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000774CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
775 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000776 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000777 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000778 }
Mike Stump11289f42009-09-09 15:08:12 +0000779
Ted Kremenek93668002009-07-17 22:18:43 +0000780 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000781}
Mike Stump31feda52009-07-17 01:31:16 +0000782
Ted Kremenek93668002009-07-17 22:18:43 +0000783/// VisitChildren - Visit the children of a Stmt.
784CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
785 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000786 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000787 E = Terminator->child_end(); I != E; ++I) {
788 if (*I) B = Visit(*I);
789 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000790 return B;
791}
Mike Stump11289f42009-09-09 15:08:12 +0000792
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000793CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
794 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000795 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000796
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000797 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000798 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000799 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000800 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000801
Ted Kremenek9aae5132007-08-23 21:42:29 +0000802 return Block;
803}
Mike Stump11289f42009-09-09 15:08:12 +0000804
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000805CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
806 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000807 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000808 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000809 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000810
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000811 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000812 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000813
Ted Kremenek93668002009-07-17 22:18:43 +0000814 // create the block evaluating the LHS
815 CFGBlock* LHSBlock = createBlock(false);
816 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000817
Ted Kremenek93668002009-07-17 22:18:43 +0000818 // create the block evaluating the RHS
819 Succ = ConfluenceBlock;
820 Block = NULL;
821 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000822
823 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000824 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000825 return 0;
826 }
827 else {
828 // Create an empty block for cases where the RHS doesn't require
829 // any explicit statements in the CFG.
830 RHSBlock = createBlock();
831 }
Mike Stump11289f42009-09-09 15:08:12 +0000832
Mike Stump773582d2009-07-23 23:25:26 +0000833 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000834 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000835 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000836 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000837
Ted Kremenek93668002009-07-17 22:18:43 +0000838 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000839 if (B->getOpcode() == BO_LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000840 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
841 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000842 } else {
John McCalle3027922010-08-25 11:45:40 +0000843 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000844 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
845 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000846 }
Mike Stump11289f42009-09-09 15:08:12 +0000847
Ted Kremenek93668002009-07-17 22:18:43 +0000848 // Generate the blocks for evaluating the LHS.
849 Block = LHSBlock;
850 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000851 }
John McCalle3027922010-08-25 11:45:40 +0000852 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000853 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000854 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000855 addStmt(B->getRHS());
856 return addStmt(B->getLHS());
857 }
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000858 else if (B->isAssignmentOp()) {
859 if (asc.alwaysAdd()) {
860 autoCreateBlock();
861 AppendStmt(Block, B, asc);
862 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000863
Ted Kremenek8abff772010-09-14 01:13:32 +0000864 // If visiting RHS causes us to finish 'Block' and the LHS doesn't
865 // create a new block, then we should return RBlock. Otherwise
866 // we'll incorrectly return NULL.
867 CFGBlock *RBlock = Visit(B->getRHS());
868 CFGBlock *LBlock = Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
869 return LBlock ? LBlock : RBlock;
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000870 }
Mike Stump11289f42009-09-09 15:08:12 +0000871
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000872 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000873}
874
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000875CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
876 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000877 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000878 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000879 }
880 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000881}
882
Ted Kremenek93668002009-07-17 22:18:43 +0000883CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
884 // "break" is a control-flow statement. Thus we stop processing the current
885 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000886 if (badCFG)
887 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000888
Ted Kremenek93668002009-07-17 22:18:43 +0000889 // Now create a new block that ends with the break statement.
890 Block = createBlock(false);
891 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000892
Ted Kremenek93668002009-07-17 22:18:43 +0000893 // If there is no target for the break, then we are looking at an incomplete
894 // AST. This means that the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000895 if (BreakJumpTarget.Block) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +0000896 addAutomaticObjDtors(ScopePos, BreakJumpTarget.ScopePos, B);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000897 AddSuccessor(Block, BreakJumpTarget.Block);
898 } else
Ted Kremenek93668002009-07-17 22:18:43 +0000899 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000900
901
Ted Kremenek9aae5132007-08-23 21:42:29 +0000902 return Block;
903}
Mike Stump11289f42009-09-09 15:08:12 +0000904
Mike Stump04c68512010-01-21 15:20:48 +0000905static bool CanThrow(Expr *E) {
906 QualType Ty = E->getType();
907 if (Ty->isFunctionPointerType())
908 Ty = Ty->getAs<PointerType>()->getPointeeType();
909 else if (Ty->isBlockPointerType())
910 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000911
Mike Stump04c68512010-01-21 15:20:48 +0000912 const FunctionType *FT = Ty->getAs<FunctionType>();
913 if (FT) {
914 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
915 if (Proto->hasEmptyExceptionSpec())
916 return false;
917 }
918 return true;
919}
920
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000921CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000922 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000923 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000924 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000925 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000926 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000927
Mike Stump04c68512010-01-21 15:20:48 +0000928 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000929
930 // Languages without exceptions are assumed to not throw.
931 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000932 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +0000933 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +0000934 }
935
936 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000937 if (FD->hasAttr<NoReturnAttr>())
938 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +0000939 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +0000940 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000941 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000942
Mike Stump04c68512010-01-21 15:20:48 +0000943 if (!CanThrow(C->getCallee()))
944 AddEHEdge = false;
945
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000946 if (!NoReturn && !AddEHEdge) {
947 if (asc.asLValue())
948 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
949 else
950 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
951 }
Mike Stump11289f42009-09-09 15:08:12 +0000952
Mike Stump92244b02010-01-19 22:00:14 +0000953 if (Block) {
954 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000955 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +0000956 return 0;
957 }
Mike Stump11289f42009-09-09 15:08:12 +0000958
Mike Stump92244b02010-01-19 22:00:14 +0000959 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000960 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000961
Mike Stump92244b02010-01-19 22:00:14 +0000962 if (NoReturn) {
963 // Wire this to the exit block directly.
964 AddSuccessor(Block, &cfg->getExit());
965 }
Mike Stump04c68512010-01-21 15:20:48 +0000966 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +0000967 // Add exceptional edges.
968 if (TryTerminatedBlock)
969 AddSuccessor(Block, TryTerminatedBlock);
970 else
971 AddSuccessor(Block, &cfg->getExit());
972 }
Mike Stump11289f42009-09-09 15:08:12 +0000973
Mike Stump8c5d7992009-07-25 21:26:53 +0000974 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000975}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000976
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000977CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
978 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000979 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000980 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000981 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000982 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000983
Ted Kremenek5868ec62010-04-11 17:02:10 +0000984 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
985 : AddStmtChoice::AlwaysAdd;
986
Ted Kremenek21822592009-07-17 18:20:32 +0000987 Succ = ConfluenceBlock;
988 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000989 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000990 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000991 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000992
Ted Kremenek21822592009-07-17 18:20:32 +0000993 Succ = ConfluenceBlock;
994 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000995 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000996 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000997 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000998
Ted Kremenek21822592009-07-17 18:20:32 +0000999 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +00001000 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001001 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001002 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1003 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +00001004 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +00001005 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +00001006}
Mike Stump11289f42009-09-09 15:08:12 +00001007
1008
1009CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001010 addLocalScopeAndDtors(C);
Mike Stump11289f42009-09-09 15:08:12 +00001011 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001012
1013 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1014 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +00001015 // If we hit a segment of code just containing ';' (NullStmts), we can
1016 // get a null block back. In such cases, just use the LastBlock
1017 if (CFGBlock *newBlock = addStmt(*I))
1018 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001019
Ted Kremenekce499c22009-08-27 23:16:26 +00001020 if (badCFG)
1021 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001022 }
Mike Stump92244b02010-01-19 22:00:14 +00001023
Ted Kremenek93668002009-07-17 22:18:43 +00001024 return LastBlock;
1025}
Mike Stump11289f42009-09-09 15:08:12 +00001026
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001027CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1028 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +00001029 // Create the confluence block that will "merge" the results of the ternary
1030 // expression.
1031 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001032 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001033 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001034 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001035
Ted Kremenek5868ec62010-04-11 17:02:10 +00001036 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1037 : AddStmtChoice::AlwaysAdd;
1038
Ted Kremenek51d40b02009-07-17 18:15:54 +00001039 // Create a block for the LHS expression if there is an LHS expression. A
1040 // GCC extension allows LHS to be NULL, causing the condition to be the
1041 // value that is returned instead.
1042 // e.g: x ?: y is shorthand for: x ? x : y;
1043 Succ = ConfluenceBlock;
1044 Block = NULL;
1045 CFGBlock* LHSBlock = NULL;
1046 if (C->getLHS()) {
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001047 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001048 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001049 return 0;
1050 Block = NULL;
1051 }
Mike Stump11289f42009-09-09 15:08:12 +00001052
Ted Kremenek51d40b02009-07-17 18:15:54 +00001053 // Create the block for the RHS expression.
1054 Succ = ConfluenceBlock;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001055 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001056 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001057 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001058
Ted Kremenek51d40b02009-07-17 18:15:54 +00001059 // Create the block that will contain the condition.
1060 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +00001061
Mike Stump773582d2009-07-23 23:25:26 +00001062 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001063 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +00001064 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001065 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +00001066 } else {
Ted Kremenek30754282009-07-24 04:47:11 +00001067 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +00001068 // If we know the condition is false, add NULL as the successor for
1069 // the block containing the condition. In this case, the confluence
1070 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001071 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +00001072 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +00001073 } else {
1074 // If we have no LHS expression, add the ConfluenceBlock as a direct
1075 // successor for the block containing the condition. Moreover, we need to
1076 // reverse the order of the predecessors in the ConfluenceBlock because
1077 // the RHSBlock will have been added to the succcessors already, and we
1078 // want the first predecessor to the the block containing the expression
1079 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001080 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +00001081 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +00001082 std::reverse(ConfluenceBlock->pred_begin(),
1083 ConfluenceBlock->pred_end());
1084 }
Ted Kremenek51d40b02009-07-17 18:15:54 +00001085 }
Mike Stump11289f42009-09-09 15:08:12 +00001086
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001087 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001088 Block->setTerminator(C);
1089 return addStmt(C->getCond());
1090}
1091
Ted Kremenek93668002009-07-17 22:18:43 +00001092CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
1093 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001094
Ted Kremenek93668002009-07-17 22:18:43 +00001095 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001096 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +00001097 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +00001098 }
Mike Stump11289f42009-09-09 15:08:12 +00001099
Ted Kremenek93668002009-07-17 22:18:43 +00001100 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001101
Ted Kremenek93668002009-07-17 22:18:43 +00001102 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1103 typedef llvm::SmallVector<Decl*,10> BufTy;
1104 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +00001105
Ted Kremenek93668002009-07-17 22:18:43 +00001106 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1107 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1108 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1109 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +00001110
Ted Kremenek93668002009-07-17 22:18:43 +00001111 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1112 // automatically freed with the CFG.
1113 DeclGroupRef DG(*I);
1114 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001115 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +00001116 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +00001117
Ted Kremenek93668002009-07-17 22:18:43 +00001118 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001119 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +00001120 B = VisitDeclSubExpr(D);
1121 }
Mike Stump11289f42009-09-09 15:08:12 +00001122
1123 return B;
Ted Kremenek93668002009-07-17 22:18:43 +00001124}
Mike Stump11289f42009-09-09 15:08:12 +00001125
Ted Kremenek93668002009-07-17 22:18:43 +00001126/// VisitDeclSubExpr - Utility method to add block-level expressions for
1127/// initializers in Decls.
1128CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
1129 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +00001130
Ted Kremenek93668002009-07-17 22:18:43 +00001131 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +00001132
Ted Kremenek93668002009-07-17 22:18:43 +00001133 if (!VD)
1134 return Block;
Mike Stump11289f42009-09-09 15:08:12 +00001135
Ted Kremenek93668002009-07-17 22:18:43 +00001136 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +00001137
Ted Kremenek93668002009-07-17 22:18:43 +00001138 if (Init) {
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +00001139 AddStmtChoice::Kind k =
1140 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
1141 : AddStmtChoice::NotAlwaysAdd;
1142 Visit(Init, AddStmtChoice(k));
Ted Kremenek93668002009-07-17 22:18:43 +00001143 }
Mike Stump11289f42009-09-09 15:08:12 +00001144
Ted Kremenek93668002009-07-17 22:18:43 +00001145 // If the type of VD is a VLA, then we must process its size expressions.
1146 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
1147 VA = FindVA(VA->getElementType().getTypePtr()))
1148 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001149
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001150 // Remove variable from local scope.
1151 if (ScopePos && VD == *ScopePos)
1152 ++ScopePos;
1153
Ted Kremenek93668002009-07-17 22:18:43 +00001154 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001155}
1156
1157CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001158 // We may see an if statement in the middle of a basic block, or it may be the
1159 // first statement we are processing. In either case, we create a new basic
1160 // block. First, we create the blocks for the then...else statements, and
1161 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +00001162 // middle of a block, we stop processing that block. That block is then the
1163 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +00001164
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001165 // Save local scope position because in case of condition variable ScopePos
1166 // won't be restored when traversing AST.
1167 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1168
1169 // Create local scope for possible condition variable.
1170 // Store scope position. Add implicit destructor.
1171 if (VarDecl* VD = I->getConditionVariable()) {
1172 LocalScope::const_iterator BeginScopePos = ScopePos;
1173 addLocalScopeForVarDecl(VD);
1174 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1175 }
1176
Mike Stump31feda52009-07-17 01:31:16 +00001177 // The block we were proccessing is now finished. Make it the successor
1178 // block.
1179 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001180 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001181 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001182 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001183 }
Mike Stump31feda52009-07-17 01:31:16 +00001184
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001185 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001186 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001187
Ted Kremenek9aae5132007-08-23 21:42:29 +00001188 if (Stmt* Else = I->getElse()) {
1189 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001190
Ted Kremenek9aae5132007-08-23 21:42:29 +00001191 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +00001192 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001193 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001194
1195 // If branch is not a compound statement create implicit scope
1196 // and add destructors.
1197 if (!isa<CompoundStmt>(Else))
1198 addLocalScopeAndDtors(Else);
1199
Ted Kremenek93668002009-07-17 22:18:43 +00001200 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +00001201
Ted Kremenekbbad8ce2007-08-30 18:13:31 +00001202 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1203 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +00001204 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001205 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001206 return 0;
1207 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001208 }
Mike Stump31feda52009-07-17 01:31:16 +00001209
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001210 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001211 CFGBlock* ThenBlock;
1212 {
1213 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001214 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001215 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001216 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001217
1218 // If branch is not a compound statement create implicit scope
1219 // and add destructors.
1220 if (!isa<CompoundStmt>(Then))
1221 addLocalScopeAndDtors(Then);
1222
Ted Kremenek93668002009-07-17 22:18:43 +00001223 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +00001224
Ted Kremenek1b379512009-04-01 03:52:47 +00001225 if (!ThenBlock) {
1226 // We can reach here if the "then" body has all NullStmts.
1227 // Create an empty block so we can distinguish between true and false
1228 // branches in path-sensitive analyses.
1229 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001230 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001231 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001232 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001233 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001234 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001235 }
1236
Mike Stump31feda52009-07-17 01:31:16 +00001237 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001238 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001239
Ted Kremenek9aae5132007-08-23 21:42:29 +00001240 // Set the terminator of the new block to the If statement.
1241 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001242
Mike Stump773582d2009-07-23 23:25:26 +00001243 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001244 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001245
Ted Kremenek9aae5132007-08-23 21:42:29 +00001246 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001247 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1248 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001249
1250 // Add the condition as the last statement in the new block. This may create
1251 // new blocks as the condition may contain control-flow. Any newly created
1252 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001253 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001254
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001255 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1256 // and the condition variable initialization to the CFG.
1257 if (VarDecl *VD = I->getConditionVariable()) {
1258 if (Expr *Init = VD->getInit()) {
1259 autoCreateBlock();
1260 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1261 addStmt(Init);
1262 }
1263 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001264
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001265 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001266}
Mike Stump31feda52009-07-17 01:31:16 +00001267
1268
Ted Kremenek9aae5132007-08-23 21:42:29 +00001269CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001270 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001271 //
Mike Stump31feda52009-07-17 01:31:16 +00001272 // NOTE: If a "return" appears in the middle of a block, this means that the
1273 // code afterwards is DEAD (unreachable). We still keep a basic block
1274 // for that code; a simple "mark-and-sweep" from the entry block will be
1275 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001276
1277 // Create the new block.
1278 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001279
Ted Kremenek9aae5132007-08-23 21:42:29 +00001280 // The Exit block is the only successor.
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001281 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001282 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001283
1284 // Add the return statement to the block. This may create new blocks if R
1285 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001286 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001287}
1288
1289CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1290 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001291 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001292 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001293
Ted Kremenek93668002009-07-17 22:18:43 +00001294 if (!LabelBlock) // This can happen when the body is empty, i.e.
1295 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001296
Ted Kremenek93668002009-07-17 22:18:43 +00001297 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001298 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001299
1300 // Labels partition blocks, so this is the end of the basic block we were
1301 // processing (L is the block's label). Because this is label (and we have
1302 // already processed the substatement) there is no extra control-flow to worry
1303 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001304 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001305 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001306 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001307
1308 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001309 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001310
Ted Kremenek9aae5132007-08-23 21:42:29 +00001311 // This block is now the implicit successor of other blocks.
1312 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001313
Ted Kremenek9aae5132007-08-23 21:42:29 +00001314 return LabelBlock;
1315}
1316
1317CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001318 // Goto is a control-flow statement. Thus we stop processing the current
1319 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001320
Ted Kremenek9aae5132007-08-23 21:42:29 +00001321 Block = createBlock(false);
1322 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001323
1324 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001325 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001326
Ted Kremenek9aae5132007-08-23 21:42:29 +00001327 if (I == LabelMap.end())
1328 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001329 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1330 else {
1331 JumpTarget JT = I->second;
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001332 addAutomaticObjDtors(ScopePos, JT.ScopePos, G);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001333 AddSuccessor(Block, JT.Block);
1334 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001335
Mike Stump31feda52009-07-17 01:31:16 +00001336 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001337}
1338
1339CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001340 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001341
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001342 // Save local scope position because in case of condition variable ScopePos
1343 // won't be restored when traversing AST.
1344 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1345
1346 // Create local scope for init statement and possible condition variable.
1347 // Add destructor for init statement and condition variable.
1348 // Store scope position for continue statement.
1349 if (Stmt* Init = F->getInit())
1350 addLocalScopeForStmt(Init);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001351 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1352
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001353 if (VarDecl* VD = F->getConditionVariable())
1354 addLocalScopeForVarDecl(VD);
1355 LocalScope::const_iterator ContinueScopePos = ScopePos;
1356
1357 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1358
Mike Stump014b3ea2009-07-21 01:12:51 +00001359 // "for" is a control-flow statement. Thus we stop processing the current
1360 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001361 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001362 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001363 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001364 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001365 } else
1366 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001367
Ted Kremenek304a9532010-05-21 20:30:15 +00001368 // Save the current value for the break targets.
1369 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001370 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001371 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001372
Mike Stump31feda52009-07-17 01:31:16 +00001373 // Because of short-circuit evaluation, the condition of the loop can span
1374 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1375 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001376 CFGBlock* ExitConditionBlock = createBlock(false);
1377 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001378
Ted Kremenek81e14852007-08-27 19:46:09 +00001379 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001380 ExitConditionBlock->setTerminator(F);
1381
1382 // Now add the actual condition to the condition block. Because the condition
1383 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001384 if (Stmt* C = F->getCond()) {
1385 Block = ExitConditionBlock;
1386 EntryConditionBlock = addStmt(C);
Ted Kremenek7b31a612010-09-15 07:01:20 +00001387 assert(Block == EntryConditionBlock ||
1388 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001389
1390 // If this block contains a condition variable, add both the condition
1391 // variable and initializer to the CFG.
1392 if (VarDecl *VD = F->getConditionVariable()) {
1393 if (Expr *Init = VD->getInit()) {
1394 autoCreateBlock();
1395 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1396 EntryConditionBlock = addStmt(Init);
1397 assert(Block == EntryConditionBlock);
1398 }
1399 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001400
Ted Kremenek55957a82009-05-02 00:13:27 +00001401 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001402 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001403 return 0;
1404 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001405 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001406
Mike Stump31feda52009-07-17 01:31:16 +00001407 // The condition block is the implicit successor for the loop body as well as
1408 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001409 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001410
Mike Stump773582d2009-07-23 23:25:26 +00001411 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001412 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001413
Mike Stump773582d2009-07-23 23:25:26 +00001414 if (F->getCond())
1415 KnownVal = TryEvaluateBool(F->getCond());
1416
Ted Kremenek9aae5132007-08-23 21:42:29 +00001417 // Now create the loop body.
1418 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001419 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001420
Ted Kremenek304a9532010-05-21 20:30:15 +00001421 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001422 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1423 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001424
Ted Kremeneke9610502007-08-30 18:39:40 +00001425 // Create a new block to contain the (bottom) of the loop body.
1426 Block = NULL;
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001427
1428 // Loop body should end with destructor of Condition variable (if any).
1429 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump31feda52009-07-17 01:31:16 +00001430
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001431 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001432 // Generate increment code in its own basic block. This is the target of
1433 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001434 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001435 } else {
1436 // No increment code. Create a special, empty, block that is used as the
1437 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001438 assert(Succ == EntryConditionBlock);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001439 Succ = Block ? Block : createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001440 }
Mike Stump31feda52009-07-17 01:31:16 +00001441
Ted Kremenek902393b2009-04-28 00:51:56 +00001442 // Finish up the increment (or empty) block if it hasn't been already.
1443 if (Block) {
1444 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001445 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001446 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001447 Block = 0;
1448 }
Mike Stump31feda52009-07-17 01:31:16 +00001449
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001450 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001451
Ted Kremenek902393b2009-04-28 00:51:56 +00001452 // The starting block for the loop increment is the block that should
1453 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001454 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001455
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001456 // If body is not a compound statement create implicit scope
1457 // and add destructors.
1458 if (!isa<CompoundStmt>(F->getBody()))
1459 addLocalScopeAndDtors(F->getBody());
1460
Mike Stump31feda52009-07-17 01:31:16 +00001461 // Now populate the body block, and in the process create new blocks as we
1462 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001463 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001464
1465 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001466 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001467 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001468 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001469
Ted Kremenek30754282009-07-24 04:47:11 +00001470 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001471 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001472 }
Mike Stump31feda52009-07-17 01:31:16 +00001473
Ted Kremenek30754282009-07-24 04:47:11 +00001474 // Link up the condition block with the code that follows the loop. (the
1475 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001476 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001477
Ted Kremenek9aae5132007-08-23 21:42:29 +00001478 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001479 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001480 if (Stmt* I = F->getInit()) {
1481 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001482 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001483 } else {
1484 // There is no loop initialization. We are thus basically a while loop.
1485 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001486 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001487 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001488 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001489 }
1490}
1491
Ted Kremenek5868ec62010-04-11 17:02:10 +00001492CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1493 if (asc.alwaysAdd()) {
1494 autoCreateBlock();
1495 AppendStmt(Block, M, asc);
1496 }
1497 return Visit(M->getBase(),
1498 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1499 : AddStmtChoice::AsLValueNotAlwaysAdd);
1500}
1501
Ted Kremenek9d56e642008-11-11 17:10:00 +00001502CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1503 // Objective-C fast enumeration 'for' statements:
1504 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1505 //
1506 // for ( Type newVariable in collection_expression ) { statements }
1507 //
1508 // becomes:
1509 //
1510 // prologue:
1511 // 1. collection_expression
1512 // T. jump to loop_entry
1513 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001514 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001515 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1516 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1517 // TB:
1518 // statements
1519 // T. jump to loop_entry
1520 // FB:
1521 // what comes after
1522 //
1523 // and
1524 //
1525 // Type existingItem;
1526 // for ( existingItem in expression ) { statements }
1527 //
1528 // becomes:
1529 //
Mike Stump31feda52009-07-17 01:31:16 +00001530 // the same with newVariable replaced with existingItem; the binding works
1531 // the same except that for one ObjCForCollectionStmt::getElement() returns
1532 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001533 //
Mike Stump31feda52009-07-17 01:31:16 +00001534
Ted Kremenek9d56e642008-11-11 17:10:00 +00001535 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001536
Ted Kremenek9d56e642008-11-11 17:10:00 +00001537 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001538 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001539 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001540 LoopSuccessor = Block;
1541 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001542 } else
1543 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001544
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001545 // Build the condition blocks.
1546 CFGBlock* ExitConditionBlock = createBlock(false);
1547 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001548
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001549 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001550 ExitConditionBlock->setTerminator(S);
1551
1552 // The last statement in the block should be the ObjCForCollectionStmt, which
1553 // performs the actual binding to 'element' and determines if there are any
1554 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001555 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001556 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001557
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001558 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001559 // generate new blocks as necesary. We DON'T add the statement by default to
1560 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001561 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001562 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001563 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001564 return 0;
1565 Block = 0;
1566 }
Mike Stump31feda52009-07-17 01:31:16 +00001567
1568 // The condition block is the implicit successor for the loop body as well as
1569 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001570 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001571
Ted Kremenek9d56e642008-11-11 17:10:00 +00001572 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001573 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001574 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001575 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1576 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1577 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001578
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001579 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1580 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001581
Ted Kremenek93668002009-07-17 22:18:43 +00001582 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001583
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001584 if (!BodyBlock)
1585 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001586 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001587 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001588 return 0;
1589 }
Mike Stump31feda52009-07-17 01:31:16 +00001590
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001591 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001592 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001593 }
Mike Stump31feda52009-07-17 01:31:16 +00001594
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001595 // Link up the condition block with the code that follows the loop.
1596 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001597 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001598
Ted Kremenek9d56e642008-11-11 17:10:00 +00001599 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001600 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001601 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001602}
1603
Ted Kremenek49805452009-05-02 01:49:13 +00001604CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1605 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001606
Ted Kremenek49805452009-05-02 01:49:13 +00001607 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001608 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001609
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001610 // The sync body starts its own basic block. This makes it a little easier
1611 // for diagnostic clients.
1612 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001613 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001614 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001615
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001616 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001617 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001618 }
Mike Stump31feda52009-07-17 01:31:16 +00001619
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001620 // Add the @synchronized to the CFG.
1621 autoCreateBlock();
1622 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1623
Ted Kremenek49805452009-05-02 01:49:13 +00001624 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001625 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001626}
Mike Stump31feda52009-07-17 01:31:16 +00001627
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001628CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001629 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001630 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001631}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001632
Ted Kremenek9aae5132007-08-23 21:42:29 +00001633CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001634 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001635
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001636 // Save local scope position because in case of condition variable ScopePos
1637 // won't be restored when traversing AST.
1638 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1639
1640 // Create local scope for possible condition variable.
1641 // Store scope position for continue statement.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001642 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001643 if (VarDecl* VD = W->getConditionVariable()) {
1644 addLocalScopeForVarDecl(VD);
1645 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1646 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001647
Mike Stump014b3ea2009-07-21 01:12:51 +00001648 // "while" is a control-flow statement. Thus we stop processing the current
1649 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001650 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001651 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001652 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001653 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001654 } else
1655 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001656
1657 // Because of short-circuit evaluation, the condition of the loop can span
1658 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1659 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001660 CFGBlock* ExitConditionBlock = createBlock(false);
1661 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001662
Ted Kremenek81e14852007-08-27 19:46:09 +00001663 // Set the terminator for the "exit" condition block.
1664 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001665
1666 // Now add the actual condition to the condition block. Because the condition
1667 // itself may contain control-flow, new blocks may be created. Thus we update
1668 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001669 if (Stmt* C = W->getCond()) {
1670 Block = ExitConditionBlock;
1671 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001672 assert(Block == EntryConditionBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001673
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001674 // If this block contains a condition variable, add both the condition
1675 // variable and initializer to the CFG.
1676 if (VarDecl *VD = W->getConditionVariable()) {
1677 if (Expr *Init = VD->getInit()) {
1678 autoCreateBlock();
1679 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1680 EntryConditionBlock = addStmt(Init);
1681 assert(Block == EntryConditionBlock);
1682 }
1683 }
1684
Ted Kremenek55957a82009-05-02 00:13:27 +00001685 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001686 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001687 return 0;
1688 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001689 }
Mike Stump31feda52009-07-17 01:31:16 +00001690
1691 // The condition block is the implicit successor for the loop body as well as
1692 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001693 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001694
Mike Stump773582d2009-07-23 23:25:26 +00001695 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001696 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001697
Ted Kremenek9aae5132007-08-23 21:42:29 +00001698 // Process the loop body.
1699 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001700 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001701
1702 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001703 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1704 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1705 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001706
Mike Stump31feda52009-07-17 01:31:16 +00001707 // Create an empty block to represent the transition block for looping back
1708 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001709 Block = 0;
1710 assert(Succ == EntryConditionBlock);
1711 Succ = createBlock();
1712 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001713 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001714
Ted Kremenek9aae5132007-08-23 21:42:29 +00001715 // All breaks should go to the code following the loop.
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001716 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001717
Ted Kremenek9aae5132007-08-23 21:42:29 +00001718 // NULL out Block to force lazy instantiation of blocks for the body.
1719 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001720
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001721 // Loop body should end with destructor of Condition variable (if any).
1722 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1723
1724 // If body is not a compound statement create implicit scope
1725 // and add destructors.
1726 if (!isa<CompoundStmt>(W->getBody()))
1727 addLocalScopeAndDtors(W->getBody());
1728
Ted Kremenek9aae5132007-08-23 21:42:29 +00001729 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001730 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001731
Ted Kremeneke9610502007-08-30 18:39:40 +00001732 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001733 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001734 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001735 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001736 return 0;
1737 }
Mike Stump31feda52009-07-17 01:31:16 +00001738
Ted Kremenek30754282009-07-24 04:47:11 +00001739 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001740 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001741 }
Mike Stump31feda52009-07-17 01:31:16 +00001742
Ted Kremenek30754282009-07-24 04:47:11 +00001743 // Link up the condition block with the code that follows the loop. (the
1744 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001745 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001746
1747 // There can be no more statements in the condition block since we loop back
1748 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001749 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001750
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001751 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001752 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001753 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001754}
Mike Stump11289f42009-09-09 15:08:12 +00001755
1756
Ted Kremenek93668002009-07-17 22:18:43 +00001757CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1758 // FIXME: For now we pretend that @catch and the code it contains does not
1759 // exit.
1760 return Block;
1761}
Mike Stump31feda52009-07-17 01:31:16 +00001762
Ted Kremenek93041ba2008-12-09 20:20:09 +00001763CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1764 // FIXME: This isn't complete. We basically treat @throw like a return
1765 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001766
Ted Kremenek0868eea2009-09-24 18:45:41 +00001767 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001768 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001769 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001770
Ted Kremenek93041ba2008-12-09 20:20:09 +00001771 // Create the new block.
1772 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001773
Ted Kremenek93041ba2008-12-09 20:20:09 +00001774 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001775 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001776
1777 // Add the statement to the block. This may create new blocks if S contains
1778 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001779 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001780}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001781
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001782CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001783 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001784 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001785 return 0;
1786
1787 // Create the new block.
1788 Block = createBlock(false);
1789
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001790 if (TryTerminatedBlock)
1791 // The current try statement is the only successor.
1792 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001793 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001794 // otherwise the Exit block is the only successor.
1795 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001796
1797 // Add the statement to the block. This may create new blocks if S contains
1798 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001799 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001800}
1801
Ted Kremenek93668002009-07-17 22:18:43 +00001802CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001803 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001804
Mike Stump8d50b6a2009-07-21 01:27:50 +00001805 // "do...while" is a control-flow statement. Thus we stop processing the
1806 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001807 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001808 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001809 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001810 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001811 } else
1812 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001813
1814 // Because of short-circuit evaluation, the condition of the loop can span
1815 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1816 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001817 CFGBlock* ExitConditionBlock = createBlock(false);
1818 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001819
Ted Kremenek81e14852007-08-27 19:46:09 +00001820 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001821 ExitConditionBlock->setTerminator(D);
1822
1823 // Now add the actual condition to the condition block. Because the condition
1824 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001825 if (Stmt* C = D->getCond()) {
1826 Block = ExitConditionBlock;
1827 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001828 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001829 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001830 return 0;
1831 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001832 }
Mike Stump31feda52009-07-17 01:31:16 +00001833
Ted Kremeneka1523a32008-02-27 07:20:00 +00001834 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001835 Succ = EntryConditionBlock;
1836
Mike Stump773582d2009-07-23 23:25:26 +00001837 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001838 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001839
Ted Kremenek9aae5132007-08-23 21:42:29 +00001840 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001841 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001842 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001843 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001844
Ted Kremenek9aae5132007-08-23 21:42:29 +00001845 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001846 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1847 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1848 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001849
Ted Kremenek9aae5132007-08-23 21:42:29 +00001850 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001851 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001852
Ted Kremenek9aae5132007-08-23 21:42:29 +00001853 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001854 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001855
Ted Kremenek9aae5132007-08-23 21:42:29 +00001856 // NULL out Block to force lazy instantiation of blocks for the body.
1857 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001858
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001859 // If body is not a compound statement create implicit scope
1860 // and add destructors.
1861 if (!isa<CompoundStmt>(D->getBody()))
1862 addLocalScopeAndDtors(D->getBody());
1863
Ted Kremenek9aae5132007-08-23 21:42:29 +00001864 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001865 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001866
Ted Kremeneke9610502007-08-30 18:39:40 +00001867 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001868 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001869 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001870 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001871 return 0;
1872 }
Mike Stump31feda52009-07-17 01:31:16 +00001873
Ted Kremenek110974d2010-08-17 20:59:56 +00001874 if (!KnownVal.isFalse()) {
1875 // Add an intermediate block between the BodyBlock and the
1876 // ExitConditionBlock to represent the "loop back" transition. Create an
1877 // empty block to represent the transition block for looping back to the
1878 // head of the loop.
1879 // FIXME: Can we do this more efficiently without adding another block?
1880 Block = NULL;
1881 Succ = BodyBlock;
1882 CFGBlock *LoopBackBlock = createBlock();
1883 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001884
Ted Kremenek110974d2010-08-17 20:59:56 +00001885 // Add the loop body entry as a successor to the condition.
1886 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1887 }
1888 else
1889 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001890 }
Mike Stump31feda52009-07-17 01:31:16 +00001891
Ted Kremenek30754282009-07-24 04:47:11 +00001892 // Link up the condition block with the code that follows the loop.
1893 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001894 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001895
1896 // There can be no more statements in the body block(s) since we loop back to
1897 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001898 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001899
Ted Kremenek9aae5132007-08-23 21:42:29 +00001900 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001901 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001902 return BodyBlock;
1903}
1904
1905CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1906 // "continue" is a control-flow statement. Thus we stop processing the
1907 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001908 if (badCFG)
1909 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001910
Ted Kremenek9aae5132007-08-23 21:42:29 +00001911 // Now create a new block that ends with the continue statement.
1912 Block = createBlock(false);
1913 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001914
Ted Kremenek9aae5132007-08-23 21:42:29 +00001915 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001916 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001917 if (ContinueJumpTarget.Block) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001918 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001919 AddSuccessor(Block, ContinueJumpTarget.Block);
1920 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00001921 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001922
Ted Kremenek9aae5132007-08-23 21:42:29 +00001923 return Block;
1924}
Mike Stump11289f42009-09-09 15:08:12 +00001925
Ted Kremenek0747de62009-07-18 00:47:21 +00001926CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001927 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001928
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001929 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001930 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001931 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001932 }
Mike Stump11289f42009-09-09 15:08:12 +00001933
Ted Kremenek93668002009-07-17 22:18:43 +00001934 // VLA types have expressions that must be evaluated.
1935 if (E->isArgumentType()) {
1936 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1937 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1938 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001939 }
Mike Stump11289f42009-09-09 15:08:12 +00001940
Mike Stump31feda52009-07-17 01:31:16 +00001941 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001942}
Mike Stump11289f42009-09-09 15:08:12 +00001943
Ted Kremenek93668002009-07-17 22:18:43 +00001944/// VisitStmtExpr - Utility method to handle (nested) statement
1945/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001946CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1947 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001948 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001949 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001950 }
Ted Kremenek93668002009-07-17 22:18:43 +00001951 return VisitCompoundStmt(SE->getSubStmt());
1952}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001953
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001954CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001955 // "switch" is a control-flow statement. Thus we stop processing the current
1956 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001957 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001958
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00001959 // Save local scope position because in case of condition variable ScopePos
1960 // won't be restored when traversing AST.
1961 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1962
1963 // Create local scope for possible condition variable.
1964 // Store scope position. Add implicit destructor.
1965 if (VarDecl* VD = Terminator->getConditionVariable()) {
1966 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
1967 addLocalScopeForVarDecl(VD);
1968 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
1969 }
1970
Ted Kremenek9aae5132007-08-23 21:42:29 +00001971 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001972 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001973 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001974 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001975 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001976
1977 // Save the current "switch" context.
1978 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001979 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001980 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00001981
Mike Stump31feda52009-07-17 01:31:16 +00001982 // Set the "default" case to be the block after the switch statement. If the
1983 // switch statement contains a "default:", this value will be overwritten with
1984 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001985 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001986
Ted Kremenek9aae5132007-08-23 21:42:29 +00001987 // Create a new block that will contain the switch statement.
1988 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001989
Ted Kremenek9aae5132007-08-23 21:42:29 +00001990 // Now process the switch body. The code after the switch is the implicit
1991 // successor.
1992 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001993 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001994
1995 // When visiting the body, the case statements should automatically get linked
1996 // up to the switch. We also don't keep a pointer to the body, since all
1997 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001998 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001999 Block = NULL;
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002000
2001 // If body is not a compound statement create implicit scope
2002 // and add destructors.
2003 if (!isa<CompoundStmt>(Terminator->getBody()))
2004 addLocalScopeAndDtors(Terminator->getBody());
2005
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002006 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00002007 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002008 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002009 return 0;
2010 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002011
Mike Stump31feda52009-07-17 01:31:16 +00002012 // If we have no "default:" case, the default transition is to the code
2013 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002014 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002015
Ted Kremenek81e14852007-08-27 19:46:09 +00002016 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002017 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002018 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00002019 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002020 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002021
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002022 // Finally, if the SwitchStmt contains a condition variable, add both the
2023 // SwitchStmt and the condition variable initialization to the CFG.
2024 if (VarDecl *VD = Terminator->getConditionVariable()) {
2025 if (Expr *Init = VD->getInit()) {
2026 autoCreateBlock();
2027 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
2028 addStmt(Init);
2029 }
2030 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002031
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002032 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002033}
2034
Ted Kremenek93668002009-07-17 22:18:43 +00002035CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00002036 // CaseStmts are essentially labels, so they are the first statement in a
2037 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00002038 CFGBlock *TopBlock = 0, *LastBlock = 0;
2039
2040 if (Stmt *Sub = CS->getSubStmt()) {
2041 // For deeply nested chains of CaseStmts, instead of doing a recursion
2042 // (which can blow out the stack), manually unroll and create blocks
2043 // along the way.
2044 while (isa<CaseStmt>(Sub)) {
2045 CFGBlock *CurrentBlock = createBlock(false);
2046 CurrentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00002047
Ted Kremenek60fa6572010-08-04 23:54:30 +00002048 if (TopBlock)
2049 AddSuccessor(LastBlock, CurrentBlock);
2050 else
2051 TopBlock = CurrentBlock;
2052
2053 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
2054 LastBlock = CurrentBlock;
2055
2056 CS = cast<CaseStmt>(Sub);
2057 Sub = CS->getSubStmt();
2058 }
2059
2060 addStmt(Sub);
2061 }
Mike Stump11289f42009-09-09 15:08:12 +00002062
Ted Kremenek55e91e82007-08-30 18:48:11 +00002063 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002064 if (!CaseBlock)
2065 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002066
2067 // Cases statements partition blocks, so this is the top of the basic block we
2068 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00002069 CaseBlock->setLabel(CS);
2070
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002071 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002072 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002073
2074 // Add this block to the list of successors for the block with the switch
2075 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00002076 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002077 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002078
Ted Kremenek9aae5132007-08-23 21:42:29 +00002079 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2080 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002081
Ted Kremenek60fa6572010-08-04 23:54:30 +00002082 if (TopBlock) {
2083 AddSuccessor(LastBlock, CaseBlock);
2084 Succ = TopBlock;
2085 }
2086 else {
2087 // This block is now the implicit successor of other blocks.
2088 Succ = CaseBlock;
2089 }
Mike Stump31feda52009-07-17 01:31:16 +00002090
Ted Kremenek60fa6572010-08-04 23:54:30 +00002091 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002092}
Mike Stump31feda52009-07-17 01:31:16 +00002093
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002094CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00002095 if (Terminator->getSubStmt())
2096 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00002097
Ted Kremenek654c78f2008-02-13 22:05:39 +00002098 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002099
2100 if (!DefaultCaseBlock)
2101 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002102
2103 // Default statements partition blocks, so this is the top of the basic block
2104 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002105 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00002106
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002107 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002108 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00002109
Mike Stump31feda52009-07-17 01:31:16 +00002110 // Unlike case statements, we don't add the default block to the successors
2111 // for the switch statement immediately. This is done when we finish
2112 // processing the switch statement. This allows for the default case
2113 // (including a fall-through to the code after the switch statement) to always
2114 // be the last successor of a switch-terminated block.
2115
Ted Kremenek654c78f2008-02-13 22:05:39 +00002116 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2117 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002118
Ted Kremenek654c78f2008-02-13 22:05:39 +00002119 // This block is now the implicit successor of other blocks.
2120 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002121
2122 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00002123}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002124
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002125CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2126 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2127 // current block.
2128 CFGBlock* TrySuccessor = NULL;
2129
2130 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002131 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002132 return 0;
2133 TrySuccessor = Block;
2134 } else TrySuccessor = Succ;
2135
Mike Stump0bdba6c2010-01-20 01:15:34 +00002136 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002137
2138 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00002139 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002140 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00002141 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002142
Mike Stump0bdba6c2010-01-20 01:15:34 +00002143 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002144 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2145 // The code after the try is the implicit successor.
2146 Succ = TrySuccessor;
2147 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002148 if (CS->getExceptionDecl() == 0) {
2149 HasCatchAll = true;
2150 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002151 Block = NULL;
2152 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2153 if (CatchBlock == 0)
2154 return 0;
2155 // Add this block to the list of successors for the block with the try
2156 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00002157 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002158 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00002159 if (!HasCatchAll) {
2160 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00002161 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002162 else
Mike Stump845384a2010-01-20 01:30:58 +00002163 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00002164 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002165
2166 // The code after the try is the implicit successor.
2167 Succ = TrySuccessor;
2168
Mike Stump845384a2010-01-20 01:30:58 +00002169 // Save the current "try" context.
2170 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2171 TryTerminatedBlock = NewTryTerminatedBlock;
2172
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002173 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002174 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002175 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002176 return Block;
2177}
2178
2179CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2180 // CXXCatchStmt are treated like labels, so they are the first statement in a
2181 // block.
2182
Marcin Swiderski3546b1a2010-10-01 01:46:52 +00002183 // Save local scope position because in case of exception variable ScopePos
2184 // won't be restored when traversing AST.
2185 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2186
2187 // Create local scope for possible exception variable.
2188 // Store scope position. Add implicit destructor.
2189 if (VarDecl* VD = CS->getExceptionDecl()) {
2190 LocalScope::const_iterator BeginScopePos = ScopePos;
2191 addLocalScopeForVarDecl(VD);
2192 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2193 }
2194
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002195 if (CS->getHandlerBlock())
2196 addStmt(CS->getHandlerBlock());
2197
2198 CFGBlock* CatchBlock = Block;
2199 if (!CatchBlock)
2200 CatchBlock = createBlock();
2201
2202 CatchBlock->setLabel(CS);
2203
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002204 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002205 return 0;
2206
2207 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2208 Block = NULL;
2209
2210 return CatchBlock;
2211}
2212
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002213CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00002214 AddStmtChoice asc) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002215 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00002216 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00002217 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00002218 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00002219 return VisitChildren(C);
2220}
2221
Ted Kremenekeda180e22007-08-28 19:26:49 +00002222CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00002223 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00002224 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002225
Ted Kremenekeda180e22007-08-28 19:26:49 +00002226 if (!IBlock) {
2227 IBlock = createBlock(false);
2228 cfg->setIndirectGotoBlock(IBlock);
2229 }
Mike Stump31feda52009-07-17 01:31:16 +00002230
Ted Kremenekeda180e22007-08-28 19:26:49 +00002231 // IndirectGoto is a control-flow statement. Thus we stop processing the
2232 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002233 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00002234 return 0;
2235
Ted Kremenekeda180e22007-08-28 19:26:49 +00002236 Block = createBlock(false);
2237 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002238 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00002239 return addStmt(I->getTarget());
2240}
2241
Ted Kremenek04cca642007-08-23 21:26:19 +00002242} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00002243
Mike Stump31feda52009-07-17 01:31:16 +00002244/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2245/// no successors or predecessors. If this is the first block created in the
2246/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00002247CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00002248 bool first_block = begin() == end();
2249
2250 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002251 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2252 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2253 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00002254
2255 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002256 if (first_block)
2257 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00002258
2259 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002260 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002261}
2262
Ted Kremenek889073f2007-08-23 16:51:22 +00002263/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2264/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00002265CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002266 BuildOptions BO) {
Ted Kremenek889073f2007-08-23 16:51:22 +00002267 CFGBuilder Builder;
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002268 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek889073f2007-08-23 16:51:22 +00002269}
2270
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002271//===----------------------------------------------------------------------===//
2272// CFG: Queries for BlkExprs.
2273//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002274
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002275namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002276 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002277}
2278
Ted Kremenekbff98442009-12-23 23:37:10 +00002279static void FindSubExprAssignments(Stmt *S,
2280 llvm::SmallPtrSet<Expr*,50>& Set) {
2281 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00002282 return;
Mike Stump31feda52009-07-17 01:31:16 +00002283
Ted Kremenekbff98442009-12-23 23:37:10 +00002284 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002285 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00002286 if (!child)
2287 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002288
Ted Kremenekbff98442009-12-23 23:37:10 +00002289 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002290 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00002291
Ted Kremenekbff98442009-12-23 23:37:10 +00002292 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00002293 }
2294}
2295
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002296static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2297 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00002298
2299 // Look for assignments that are used as subexpressions. These are the only
2300 // assignments that we want to *possibly* register as a block-level
2301 // expression. Basically, if an assignment occurs both in a subexpression and
2302 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00002303 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00002304
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002305 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002306 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002307 if (CFGStmt S = BI->getAs<CFGStmt>())
2308 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002309
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002310 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00002311
2312 // Iterate over the statements again on identify the Expr* and Stmt* at the
2313 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002314
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002315 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2316 CFGStmt CS = BI->getAs<CFGStmt>();
2317 if (!CS.isValid())
2318 continue;
2319 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00002320
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002321 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002322 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00002323 // expression are really "statements" whose value is never used by
2324 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002325 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002326 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002327 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2328 // Special handling for statement expressions. The last statement in
2329 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002330 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002331 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002332 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002333 (*M)[C->body_back()] = x;
2334 }
2335 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002336
Ted Kremenek95a123c2008-01-26 00:03:27 +00002337 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002338 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002339 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002340 }
Mike Stump31feda52009-07-17 01:31:16 +00002341
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002342 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002343
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002344 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002345
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002346 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002347 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002348 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002349 }
2350 }
Mike Stump31feda52009-07-17 01:31:16 +00002351
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002352 return M;
2353}
2354
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002355CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2356 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002357 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002358
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002359 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002360 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002361 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002362}
2363
2364unsigned CFG::getNumBlkExprs() {
2365 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2366 return M->size();
2367 else {
2368 // We assume callers interested in the number of BlkExprs will want
2369 // the map constructed if it doesn't already exist.
2370 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2371 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
2372 }
2373}
2374
Ted Kremenek6065ef62008-04-28 18:00:46 +00002375//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002376// Filtered walking of the CFG.
2377//===----------------------------------------------------------------------===//
2378
2379bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002380 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002381
2382 if (F.IgnoreDefaultsWithCoveredEnums) {
2383 // If the 'To' has no label or is labeled but the label isn't a
2384 // CaseStmt then filter this edge.
2385 if (const SwitchStmt *S =
Ted Kremenekf146cd12010-09-09 02:57:48 +00002386 dyn_cast_or_null<SwitchStmt>(From->getTerminator())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002387 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00002388 const Stmt *L = To->getLabel();
2389 if (!L || !isa<CaseStmt>(L))
2390 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002391 }
2392 }
2393 }
2394
2395 return false;
2396}
2397
2398//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002399// Cleanup: CFG dstor.
2400//===----------------------------------------------------------------------===//
2401
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002402CFG::~CFG() {
2403 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2404}
Mike Stump31feda52009-07-17 01:31:16 +00002405
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002406//===----------------------------------------------------------------------===//
2407// CFG pretty printing
2408//===----------------------------------------------------------------------===//
2409
Ted Kremenek7e776b12007-08-22 18:22:34 +00002410namespace {
2411
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002412class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002413 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002414 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002415 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002416 DeclMapTy DeclMap;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002417 signed CurrentBlock;
2418 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002419 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002420public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002421
Chris Lattnerc61089a2009-06-30 01:26:17 +00002422 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2423 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002424 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2425 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002426 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002427 BI != BEnd; ++BI, ++j ) {
2428 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2429 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2430 StmtMap[SE] = P;
2431
2432 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2433 DeclMap[DS->getSingleDecl()] = P;
2434
2435 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2436 if (VarDecl* VD = IS->getConditionVariable())
2437 DeclMap[VD] = P;
2438
2439 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2440 if (VarDecl* VD = FS->getConditionVariable())
2441 DeclMap[VD] = P;
2442
2443 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2444 if (VarDecl* VD = WS->getConditionVariable())
2445 DeclMap[VD] = P;
2446
2447 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2448 if (VarDecl* VD = SS->getConditionVariable())
2449 DeclMap[VD] = P;
2450
2451 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2452 if (VarDecl* VD = CS->getExceptionDecl())
2453 DeclMap[VD] = P;
2454 }
2455 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002456 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002457 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002458 }
Mike Stump31feda52009-07-17 01:31:16 +00002459
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002460 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00002461
Chris Lattnerc61089a2009-06-30 01:26:17 +00002462 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002463 void setBlockID(signed i) { CurrentBlock = i; }
2464 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00002465
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002466 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2467 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002468
2469 if (I == StmtMap.end())
2470 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002471
2472 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00002473 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002474 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002475 }
Mike Stump31feda52009-07-17 01:31:16 +00002476
Ted Kremenek60983dc2010-01-19 20:52:05 +00002477 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002478 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002479 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002480
2481 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2482 DeclMapTy::iterator I = DeclMap.find(D);
2483
2484 if (I == DeclMap.end())
2485 return false;
2486
2487 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2488 && I->second.second == CurrentStmt) {
2489 return false;
2490 }
2491
2492 OS << "[B" << I->second.first << "." << I->second.second << "]";
2493 return true;
2494 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002495};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002496} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002497
Chris Lattnerc61089a2009-06-30 01:26:17 +00002498
2499namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002500class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002501 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002502
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002503 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002504 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002505 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002506public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002507 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002508 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002509 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002510
Ted Kremenek9aae5132007-08-23 21:42:29 +00002511 void VisitIfStmt(IfStmt* I) {
2512 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002513 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002514 }
Mike Stump31feda52009-07-17 01:31:16 +00002515
Ted Kremenek9aae5132007-08-23 21:42:29 +00002516 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002517 void VisitStmt(Stmt* Terminator) {
2518 Terminator->printPretty(OS, Helper, Policy);
2519 }
2520
Ted Kremenek9aae5132007-08-23 21:42:29 +00002521 void VisitForStmt(ForStmt* F) {
2522 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002523 if (F->getInit())
2524 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002525 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002526 if (Stmt* C = F->getCond())
2527 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002528 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002529 if (F->getInc())
2530 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002531 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002532 }
Mike Stump31feda52009-07-17 01:31:16 +00002533
Ted Kremenek9aae5132007-08-23 21:42:29 +00002534 void VisitWhileStmt(WhileStmt* W) {
2535 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002536 if (Stmt* C = W->getCond())
2537 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002538 }
Mike Stump31feda52009-07-17 01:31:16 +00002539
Ted Kremenek9aae5132007-08-23 21:42:29 +00002540 void VisitDoStmt(DoStmt* D) {
2541 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002542 if (Stmt* C = D->getCond())
2543 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002544 }
Mike Stump31feda52009-07-17 01:31:16 +00002545
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002546 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002547 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002548 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002549 }
Mike Stump31feda52009-07-17 01:31:16 +00002550
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002551 void VisitCXXTryStmt(CXXTryStmt* CS) {
2552 OS << "try ...";
2553 }
2554
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002555 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002556 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002557 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002558 }
Mike Stump31feda52009-07-17 01:31:16 +00002559
Ted Kremenek391f94a2007-08-31 22:29:13 +00002560 void VisitChooseExpr(ChooseExpr* C) {
2561 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002562 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002563 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002564 }
Mike Stump31feda52009-07-17 01:31:16 +00002565
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002566 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2567 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002568 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002569 }
Mike Stump31feda52009-07-17 01:31:16 +00002570
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002571 void VisitBinaryOperator(BinaryOperator* B) {
2572 if (!B->isLogicalOp()) {
2573 VisitExpr(B);
2574 return;
2575 }
Mike Stump31feda52009-07-17 01:31:16 +00002576
Douglas Gregor7de59662009-05-29 20:38:28 +00002577 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002578
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002579 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002580 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002581 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002582 return;
John McCalle3027922010-08-25 11:45:40 +00002583 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002584 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002585 return;
2586 default:
2587 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00002588 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002589 }
Mike Stump31feda52009-07-17 01:31:16 +00002590
Ted Kremenek12687ff2007-08-27 21:54:41 +00002591 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002592 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002593 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00002594};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002595} // end anonymous namespace
2596
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002597static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00002598 const CFGElement &E) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002599 if (CFGStmt CS = E.getAs<CFGStmt>()) {
2600 Stmt *S = CS;
2601
2602 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00002603
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002604 // special printing for statement-expressions.
2605 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
2606 CompoundStmt* Sub = SE->getSubStmt();
2607
2608 if (Sub->child_begin() != Sub->child_end()) {
2609 OS << "({ ... ; ";
2610 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
2611 OS << " })\n";
2612 return;
2613 }
2614 }
2615 // special printing for comma expressions.
2616 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
2617 if (B->getOpcode() == BO_Comma) {
2618 OS << "... , ";
2619 Helper->handledStmt(B->getRHS(),OS);
2620 OS << '\n';
2621 return;
2622 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002623 }
2624 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002625 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00002626
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002627 if (isa<CXXOperatorCallExpr>(S)) {
2628 OS << " (OperatorCall)";
Mike Stump31feda52009-07-17 01:31:16 +00002629 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002630 else if (isa<CXXBindTemporaryExpr>(S)) {
2631 OS << " (BindTemporary)";
2632 }
Mike Stump31feda52009-07-17 01:31:16 +00002633
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002634 // Expressions need a newline.
2635 if (isa<Expr>(S))
2636 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002637
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002638 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
2639 CXXBaseOrMemberInitializer* I = IE;
2640 if (I->isBaseInitializer())
2641 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
2642 else OS << I->getMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00002643
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002644 OS << "(";
2645 if (Expr* IE = I->getInit())
2646 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2647 OS << ")";
2648
2649 if (I->isBaseInitializer())
2650 OS << " (Base initializer)\n";
2651 else OS << " (Member initializer)\n";
2652
2653 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
2654 VarDecl* VD = DE.getVarDecl();
2655 Helper->handleDecl(VD, OS);
2656
2657 Type* T = VD->getType().getTypePtr();
2658 if (const ReferenceType* RT = T->getAs<ReferenceType>())
2659 T = RT->getPointeeType().getTypePtr();
2660
2661 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
2662 OS << " (Implicit destructor)\n";
2663 }
2664 }
Mike Stump31feda52009-07-17 01:31:16 +00002665
Chris Lattnerc61089a2009-06-30 01:26:17 +00002666static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2667 const CFGBlock& B,
2668 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00002669
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002670 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00002671
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002672 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00002673 OS << "\n [ B" << B.getBlockID();
2674
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002675 if (&B == &cfg->getEntry())
2676 OS << " (ENTRY) ]\n";
2677 else if (&B == &cfg->getExit())
2678 OS << " (EXIT) ]\n";
2679 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002680 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002681 else
2682 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00002683
Ted Kremenek71eca012007-08-29 23:20:49 +00002684 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00002685 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002686
2687 if (print_edges)
2688 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002689
Mike Stump92244b02010-01-19 22:00:14 +00002690 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002691 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00002692 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00002693 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002694 C->getLHS()->printPretty(OS, Helper,
2695 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002696 if (C->getRHS()) {
2697 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002698 C->getRHS()->printPretty(OS, Helper,
2699 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002700 }
Mike Stump92244b02010-01-19 22:00:14 +00002701 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002702 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00002703 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002704 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00002705 if (CS->getExceptionDecl())
2706 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2707 0);
2708 else
2709 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002710 OS << ")";
2711
2712 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002713 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00002714
Ted Kremenek71eca012007-08-29 23:20:49 +00002715 OS << ":\n";
2716 }
Mike Stump31feda52009-07-17 01:31:16 +00002717
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002718 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002719 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00002720
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002721 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2722 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00002723
Ted Kremenek71eca012007-08-29 23:20:49 +00002724 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002725 if (print_edges)
2726 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002727
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002728 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002729
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002730 if (Helper)
2731 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002732
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002733 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002734 }
Mike Stump31feda52009-07-17 01:31:16 +00002735
Ted Kremenek71eca012007-08-29 23:20:49 +00002736 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002737 if (B.getTerminator()) {
2738 if (print_edges)
2739 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002740
Ted Kremenek71eca012007-08-29 23:20:49 +00002741 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002742
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002743 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002744
Chris Lattnerc61089a2009-06-30 01:26:17 +00002745 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2746 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002747 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00002748 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002749 }
Mike Stump31feda52009-07-17 01:31:16 +00002750
Ted Kremenek71eca012007-08-29 23:20:49 +00002751 if (print_edges) {
2752 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002753 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002754 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002755
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002756 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2757 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002758
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002759 if (i == 8 || (i-8) == 0)
2760 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002761
Ted Kremenek71eca012007-08-29 23:20:49 +00002762 OS << " B" << (*I)->getBlockID();
2763 }
Mike Stump31feda52009-07-17 01:31:16 +00002764
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002765 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002766
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002767 // Print the successors of this block.
2768 OS << " Successors (" << B.succ_size() << "):";
2769 i = 0;
2770
2771 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2772 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002773
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002774 if (i == 8 || (i-8) % 10 == 0)
2775 OS << "\n ";
2776
Mike Stump0d76d072009-07-20 23:24:15 +00002777 if (*I)
2778 OS << " B" << (*I)->getBlockID();
2779 else
2780 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002781 }
Mike Stump31feda52009-07-17 01:31:16 +00002782
Ted Kremenek71eca012007-08-29 23:20:49 +00002783 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002784 }
Mike Stump31feda52009-07-17 01:31:16 +00002785}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002786
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002787
2788/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002789void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002790
2791/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002792void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2793 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002794
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002795 // Print the entry block.
2796 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002797
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002798 // Iterate through the CFGBlocks and print them one by one.
2799 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2800 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002801 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002802 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002803
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002804 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002805 }
Mike Stump31feda52009-07-17 01:31:16 +00002806
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002807 // Print the exit block.
2808 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002809 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002810}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002811
2812/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002813void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2814 print(llvm::errs(), cfg, LO);
2815}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002816
2817/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2818/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002819void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2820 const LangOptions &LO) const {
2821 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002822 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002823}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002824
Ted Kremenek15647632008-01-30 23:02:42 +00002825/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002826void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002827 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002828 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002829 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2830}
2831
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002832Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002833
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002834 if (!Terminator)
2835 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002836
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002837 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002838
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002839 switch (Terminator->getStmtClass()) {
2840 default:
2841 break;
Mike Stump31feda52009-07-17 01:31:16 +00002842
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002843 case Stmt::ForStmtClass:
2844 E = cast<ForStmt>(Terminator)->getCond();
2845 break;
Mike Stump31feda52009-07-17 01:31:16 +00002846
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002847 case Stmt::WhileStmtClass:
2848 E = cast<WhileStmt>(Terminator)->getCond();
2849 break;
Mike Stump31feda52009-07-17 01:31:16 +00002850
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002851 case Stmt::DoStmtClass:
2852 E = cast<DoStmt>(Terminator)->getCond();
2853 break;
Mike Stump31feda52009-07-17 01:31:16 +00002854
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002855 case Stmt::IfStmtClass:
2856 E = cast<IfStmt>(Terminator)->getCond();
2857 break;
Mike Stump31feda52009-07-17 01:31:16 +00002858
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002859 case Stmt::ChooseExprClass:
2860 E = cast<ChooseExpr>(Terminator)->getCond();
2861 break;
Mike Stump31feda52009-07-17 01:31:16 +00002862
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002863 case Stmt::IndirectGotoStmtClass:
2864 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2865 break;
Mike Stump31feda52009-07-17 01:31:16 +00002866
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002867 case Stmt::SwitchStmtClass:
2868 E = cast<SwitchStmt>(Terminator)->getCond();
2869 break;
Mike Stump31feda52009-07-17 01:31:16 +00002870
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002871 case Stmt::ConditionalOperatorClass:
2872 E = cast<ConditionalOperator>(Terminator)->getCond();
2873 break;
Mike Stump31feda52009-07-17 01:31:16 +00002874
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002875 case Stmt::BinaryOperatorClass: // '&&' and '||'
2876 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002877 break;
Mike Stump31feda52009-07-17 01:31:16 +00002878
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002879 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002880 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002881 }
Mike Stump31feda52009-07-17 01:31:16 +00002882
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002883 return E ? E->IgnoreParens() : NULL;
2884}
2885
Ted Kremenek92137a32008-05-16 16:06:00 +00002886bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002887
Ted Kremenek92137a32008-05-16 16:06:00 +00002888 if (!Terminator)
2889 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002890
Ted Kremenek92137a32008-05-16 16:06:00 +00002891 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002892
Ted Kremenek92137a32008-05-16 16:06:00 +00002893 switch (Terminator->getStmtClass()) {
2894 default:
2895 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002896
2897 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002898 case Stmt::WhileStmtClass:
2899 case Stmt::DoStmtClass:
2900 case Stmt::IfStmtClass:
2901 case Stmt::ChooseExprClass:
2902 case Stmt::ConditionalOperatorClass:
2903 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002904 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002905 }
Mike Stump31feda52009-07-17 01:31:16 +00002906
Ted Kremenek92137a32008-05-16 16:06:00 +00002907 return E ? E->IgnoreParens() : NULL;
2908}
2909
Ted Kremenek15647632008-01-30 23:02:42 +00002910
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002911//===----------------------------------------------------------------------===//
2912// CFG Graphviz Visualization
2913//===----------------------------------------------------------------------===//
2914
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002915
2916#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002917static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002918#endif
2919
Chris Lattnerc61089a2009-06-30 01:26:17 +00002920void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002921#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002922 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002923 GraphHelper = &H;
2924 llvm::ViewGraph(this,"CFG");
2925 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002926#endif
2927}
2928
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002929namespace llvm {
2930template<>
2931struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002932
2933 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2934
2935 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002936
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002937#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002938 std::string OutSStr;
2939 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002940 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002941 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002942
2943 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2944
2945 // Process string output to make it nicer...
2946 for (unsigned i = 0; i != OutStr.length(); ++i)
2947 if (OutStr[i] == '\n') { // Left justify
2948 OutStr[i] = '\\';
2949 OutStr.insert(OutStr.begin()+i+1, 'l');
2950 }
Mike Stump31feda52009-07-17 01:31:16 +00002951
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002952 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002953#else
2954 return "";
2955#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002956 }
2957};
2958} // end namespace llvm