blob: e44f0fc90b248d8d3eb306e008a87a4abc813563 [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);
Marcin Swiderski20b88732010-10-05 05:37:00 +0000310 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000311
Marcin Swiderski5e415732010-09-30 23:05:00 +0000312 // Local scopes creation.
313 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
314
Zhongxing Xu81714f22010-10-01 03:00:16 +0000315 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000316 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
317 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
318
319 void addLocalScopeAndDtors(Stmt* S);
320
321 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000322 void AppendStmt(CFGBlock *B, Stmt *S,
323 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
324 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000325 }
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000326 void appendInitializer(CFGBlock *B, CXXBaseOrMemberInitializer *I) {
327 B->appendInitializer(I, cfg->getBumpVectorContext());
328 }
Marcin Swiderski20b88732010-10-05 05:37:00 +0000329 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
330 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
331 }
332 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
333 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
334 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000335
Marcin Swiderski321a7072010-09-30 22:54:37 +0000336 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
337 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
338 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
339 LocalScope::const_iterator E, Stmt* S);
340 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
341 LocalScope::const_iterator B, LocalScope::const_iterator E);
342
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000343 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
344 B->addSuccessor(S, cfg->getBumpVectorContext());
345 }
Mike Stump11289f42009-09-09 15:08:12 +0000346
Ted Kremenek963cc312009-07-24 06:55:42 +0000347 /// TryResult - a class representing a variant over the values
348 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
349 /// and is used by the CFGBuilder to decide if a branch condition
350 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000351 class TryResult {
352 int X;
353 public:
354 TryResult(bool b) : X(b ? 1 : 0) {}
355 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000356
Ted Kremenek30754282009-07-24 04:47:11 +0000357 bool isTrue() const { return X == 1; }
358 bool isFalse() const { return X == 0; }
359 bool isKnown() const { return X >= 0; }
360 void negate() {
361 assert(isKnown());
362 X ^= 0x1;
363 }
364 };
Mike Stump11289f42009-09-09 15:08:12 +0000365
Mike Stump773582d2009-07-23 23:25:26 +0000366 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
367 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000368 TryResult TryEvaluateBool(Expr *S) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000369 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000370 return TryResult();
371
Mike Stump773582d2009-07-23 23:25:26 +0000372 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000373 if (!S->isTypeDependent() && !S->isValueDependent() &&
374 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000375 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000376
377 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000378 }
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000379};
Mike Stump31feda52009-07-17 01:31:16 +0000380
Douglas Gregor4619e432008-12-05 23:32:09 +0000381// FIXME: Add support for dependent-sized array types in C++?
382// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000383static VariableArrayType* FindVA(Type* t) {
384 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
385 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
386 if (vat->getSizeExpr())
387 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000388
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000389 t = vt->getElementType().getTypePtr();
390 }
Mike Stump31feda52009-07-17 01:31:16 +0000391
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000392 return 0;
393}
Mike Stump31feda52009-07-17 01:31:16 +0000394
395/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
396/// arbitrary statement. Examples include a single expression or a function
397/// body (compound statement). The ownership of the returned CFG is
398/// transferred to the caller. If CFG construction fails, this method returns
399/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000400CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000401 CFG::BuildOptions BO) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000402
Mike Stump0d76d072009-07-20 23:24:15 +0000403 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000404 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000405 if (!Statement)
406 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000407
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000408 BuildOpts = BO;
Mike Stump31feda52009-07-17 01:31:16 +0000409
410 // Create an empty block that will serve as the exit block for the CFG. Since
411 // this is the first block added to the CFG, it will be implicitly registered
412 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000413 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000414 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000415 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000416
Marcin Swiderski20b88732010-10-05 05:37:00 +0000417 if (BuildOpts.AddImplicitDtors)
418 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
419 addImplicitDtorsForDestructor(DD);
420
Ted Kremenek9aae5132007-08-23 21:42:29 +0000421 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000422 CFGBlock *B = addStmt(Statement);
423
424 if (badCFG)
425 return NULL;
426
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000427 // For C++ constructor add initializers to CFG.
428 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
429 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
430 E = CD->init_rend(); I != E; ++I) {
431 B = addInitializer(*I);
432 if (badCFG)
433 return NULL;
434 }
435 }
436
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000437 if (B)
438 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000439
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000440 // Backpatch the gotos whose label -> block mappings we didn't know when we
441 // encountered them.
442 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
443 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000444
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000445 CFGBlock* B = I->Block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000446 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
447 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000448
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000449 // If there is no target for the goto, then we are looking at an
450 // incomplete AST. Handle this by not registering a successor.
451 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000452
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000453 JumpTarget JT = LI->second;
Marcin Swiderski667ffec2010-10-01 00:23:17 +0000454 prependAutomaticObjDtorsWithTerminator(B, I->ScopePos, JT.ScopePos);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000455 AddSuccessor(B, JT.Block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000456 }
457
458 // Add successors to the Indirect Goto Dispatch block (if we have one).
459 if (CFGBlock* B = cfg->getIndirectGotoBlock())
460 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
461 E = AddressTakenLabels.end(); I != E; ++I ) {
462
463 // Lookup the target block.
464 LabelMapTy::iterator LI = LabelMap.find(*I);
465
466 // If there is no target block that contains label, then we are looking
467 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000468 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000469
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000470 AddSuccessor(B, LI->second.Block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000471 }
Mike Stump31feda52009-07-17 01:31:16 +0000472
Mike Stump31feda52009-07-17 01:31:16 +0000473 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000474 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000475
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000476 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000477}
Mike Stump31feda52009-07-17 01:31:16 +0000478
Ted Kremenek9aae5132007-08-23 21:42:29 +0000479/// createBlock - Used to lazily create blocks that are connected
480/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000481CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000482 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000483 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000484 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000485 return B;
486}
Mike Stump31feda52009-07-17 01:31:16 +0000487
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000488/// addInitializer - Add C++ base or member initializer element to CFG.
489CFGBlock *CFGBuilder::addInitializer(CXXBaseOrMemberInitializer *I) {
490 if (!BuildOpts.AddInitializers)
491 return Block;
492
493 autoCreateBlock();
494 appendInitializer(Block, I);
495
496 if (Expr *Init = I->getInit()) {
497 AddStmtChoice::Kind K = AddStmtChoice::NotAlwaysAdd;
498 if (FieldDecl *FD = I->getMember())
499 if (FD->getType()->isReferenceType())
500 K = AddStmtChoice::AsLValueNotAlwaysAdd;
501
502 return Visit(Init, AddStmtChoice(K));
503 }
504
505 return Block;
506}
507
Marcin Swiderski5e415732010-09-30 23:05:00 +0000508/// addAutomaticObjDtors - Add to current block automatic objects destructors
509/// for objects in range of local scope positions. Use S as trigger statement
510/// for destructors.
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000511void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
512 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000513 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000514 return;
515
Marcin Swiderski5e415732010-09-30 23:05:00 +0000516 if (B == E)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000517 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000518
519 autoCreateBlock();
520 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000521}
522
Marcin Swiderski20b88732010-10-05 05:37:00 +0000523/// addImplicitDtorsForDestructor - Add implicit destructors generated for
524/// base and member objects in destructor.
525void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
526 assert (BuildOpts.AddImplicitDtors
527 && "Can be called only when dtors should be added");
528 const CXXRecordDecl *RD = DD->getParent();
529
530 // At the end destroy virtual base objects.
531 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
532 VE = RD->vbases_end(); VI != VE; ++VI) {
533 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
534 if (!CD->hasTrivialDestructor()) {
535 autoCreateBlock();
536 appendBaseDtor(Block, VI);
537 }
538 }
539
540 // Before virtual bases destroy direct base objects.
541 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
542 BE = RD->bases_end(); BI != BE; ++BI) {
543 if (!BI->isVirtual()) {
544 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
545 if (!CD->hasTrivialDestructor()) {
546 autoCreateBlock();
547 appendBaseDtor(Block, BI);
548 }
549 }
550 }
551
552 // First destroy member objects.
553 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
554 FE = RD->field_end(); FI != FE; ++FI) {
555 if (const CXXRecordDecl *CD = FI->getType()->getAsCXXRecordDecl())
556 if (!CD->hasTrivialDestructor()) {
557 autoCreateBlock();
558 appendMemberDtor(Block, *FI);
559 }
560 }
561}
562
Marcin Swiderski5e415732010-09-30 23:05:00 +0000563/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
564/// way return valid LocalScope object.
565LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
566 if (!Scope) {
567 Scope = cfg->getAllocator().Allocate<LocalScope>();
568 new (Scope) LocalScope(ScopePos);
569 }
570 return Scope;
571}
572
573/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu81714f22010-10-01 03:00:16 +0000574/// that should create implicit scope (e.g. if/else substatements).
575void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000576 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu81714f22010-10-01 03:00:16 +0000577 return;
578
579 LocalScope *Scope = 0;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000580
581 // For compound statement we will be creating explicit scope.
582 if (CompoundStmt* CS = dyn_cast<CompoundStmt>(S)) {
583 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
584 ; BI != BE; ++BI) {
585 Stmt* SI = *BI;
586 if (LabelStmt* LS = dyn_cast<LabelStmt>(SI))
587 SI = LS->getSubStmt();
588 if (DeclStmt* DS = dyn_cast<DeclStmt>(SI))
589 Scope = addLocalScopeForDeclStmt(DS, Scope);
590 }
Zhongxing Xu81714f22010-10-01 03:00:16 +0000591 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000592 }
593
594 // For any other statement scope will be implicit and as such will be
595 // interesting only for DeclStmt.
596 if (LabelStmt* LS = dyn_cast<LabelStmt>(S))
597 S = LS->getSubStmt();
598 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Zhongxing Xu307701e2010-10-01 03:09:09 +0000599 addLocalScopeForDeclStmt(DS);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000600}
601
602/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
603/// reuse Scope if not NULL.
604LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000605 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000606 if (!BuildOpts.AddImplicitDtors)
607 return Scope;
608
609 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
610 ; DI != DE; ++DI) {
611 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
612 Scope = addLocalScopeForVarDecl(VD, Scope);
613 }
614 return Scope;
615}
616
617/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
618/// create add scope for automatic objects and temporary objects bound to
619/// const reference. Will reuse Scope if not NULL.
620LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000621 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000622 if (!BuildOpts.AddImplicitDtors)
623 return Scope;
624
625 // Check if variable is local.
626 switch (VD->getStorageClass()) {
627 case SC_None:
628 case SC_Auto:
629 case SC_Register:
630 break;
631 default: return Scope;
632 }
633
634 // Check for const references bound to temporary. Set type to pointee.
635 QualType QT = VD->getType();
636 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
637 QT = RT->getPointeeType();
638 if (!QT.isConstQualified())
639 return Scope;
640 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
641 return Scope;
642 }
643
644 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000645
646 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
647 if (!CD->hasTrivialDestructor()) {
648 // Add the variable to scope
649 Scope = createOrReuseLocalScope(Scope);
650 Scope->addVar(VD);
651 ScopePos = Scope->begin();
652 }
Marcin Swiderski5e415732010-09-30 23:05:00 +0000653 return Scope;
654}
655
656/// addLocalScopeAndDtors - For given statement add local scope for it and
657/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
658void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
659 if (!BuildOpts.AddImplicitDtors)
660 return;
661
662 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu81714f22010-10-01 03:00:16 +0000663 addLocalScopeForStmt(S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000664 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
665}
666
Marcin Swiderski321a7072010-09-30 22:54:37 +0000667/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
668/// automatic storage duration to CFGBlock's elements vector. Insertion will be
669/// performed in place specified with iterator.
670void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
671 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
672 BumpVectorContext& C = cfg->getBumpVectorContext();
673 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
674 while (B != E)
675 I = Blk->insertAutomaticObjDtor(I, *B++, S);
676}
677
678/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
679/// automatic storage duration to CFGBlock's elements vector. Elements will be
680/// appended to physical end of the vector which happens to be logical
681/// beginning.
682void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
683 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
684 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
685}
686
687/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
688/// variables with automatic storage duration to CFGBlock's elements vector.
689/// Elements will be prepended to physical beginning of the vector which
690/// happens to be logical end. Use blocks terminator as statement that specifies
691/// destructors call site.
692void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
693 LocalScope::const_iterator B, LocalScope::const_iterator E) {
694 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
695}
696
Ted Kremenek93668002009-07-17 22:18:43 +0000697/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000698/// blocks for ternary operators, &&, and ||. We also process "," and
699/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000700CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000701tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000702 if (!S) {
703 badCFG = true;
704 return 0;
705 }
Ted Kremenek93668002009-07-17 22:18:43 +0000706 switch (S->getStmtClass()) {
707 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000708 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000709
710 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000711 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000712
Ted Kremenek93668002009-07-17 22:18:43 +0000713 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000714 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000715
Ted Kremenek93668002009-07-17 22:18:43 +0000716 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000717 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000718
Ted Kremenek93668002009-07-17 22:18:43 +0000719 case Stmt::BreakStmtClass:
720 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000721
Ted Kremenek93668002009-07-17 22:18:43 +0000722 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000723 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000724 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000725
Ted Kremenek93668002009-07-17 22:18:43 +0000726 case Stmt::CaseStmtClass:
727 return VisitCaseStmt(cast<CaseStmt>(S));
728
729 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000730 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000731
Ted Kremenek93668002009-07-17 22:18:43 +0000732 case Stmt::CompoundStmtClass:
733 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000734
Ted Kremenek93668002009-07-17 22:18:43 +0000735 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000736 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000737
Ted Kremenek93668002009-07-17 22:18:43 +0000738 case Stmt::ContinueStmtClass:
739 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000740
Ted Kremenekb27378c2010-01-19 20:40:33 +0000741 case Stmt::CXXCatchStmtClass:
742 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
743
Ted Kremenek82bfc862010-08-28 00:19:02 +0000744 case Stmt::CXXExprWithTemporariesClass: {
745 // FIXME: Handle temporaries. For now, just visit the subexpression
746 // so we don't artificially create extra blocks.
Ted Kremenek128d04d2010-08-31 18:47:34 +0000747 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000748 }
749
Zhongxing Xu7e612172010-04-13 09:38:01 +0000750 case Stmt::CXXMemberCallExprClass:
751 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
752
Ted Kremenekb27378c2010-01-19 20:40:33 +0000753 case Stmt::CXXThrowExprClass:
754 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000755
Ted Kremenekb27378c2010-01-19 20:40:33 +0000756 case Stmt::CXXTryStmtClass:
757 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000758
Ted Kremenek93668002009-07-17 22:18:43 +0000759 case Stmt::DeclStmtClass:
760 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000761
Ted Kremenek93668002009-07-17 22:18:43 +0000762 case Stmt::DefaultStmtClass:
763 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000764
Ted Kremenek93668002009-07-17 22:18:43 +0000765 case Stmt::DoStmtClass:
766 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000767
Ted Kremenek93668002009-07-17 22:18:43 +0000768 case Stmt::ForStmtClass:
769 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000770
Ted Kremenek93668002009-07-17 22:18:43 +0000771 case Stmt::GotoStmtClass:
772 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000773
Ted Kremenek93668002009-07-17 22:18:43 +0000774 case Stmt::IfStmtClass:
775 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000776
Ted Kremenek93668002009-07-17 22:18:43 +0000777 case Stmt::IndirectGotoStmtClass:
778 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000779
Ted Kremenek93668002009-07-17 22:18:43 +0000780 case Stmt::LabelStmtClass:
781 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000782
Ted Kremenek5868ec62010-04-11 17:02:10 +0000783 case Stmt::MemberExprClass:
784 return VisitMemberExpr(cast<MemberExpr>(S), asc);
785
Ted Kremenek93668002009-07-17 22:18:43 +0000786 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000787 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
788
Ted Kremenek93668002009-07-17 22:18:43 +0000789 case Stmt::ObjCAtSynchronizedStmtClass:
790 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000791
Ted Kremenek93668002009-07-17 22:18:43 +0000792 case Stmt::ObjCAtThrowStmtClass:
793 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000794
Ted Kremenek93668002009-07-17 22:18:43 +0000795 case Stmt::ObjCAtTryStmtClass:
796 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000797
Ted Kremenek93668002009-07-17 22:18:43 +0000798 case Stmt::ObjCForCollectionStmtClass:
799 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000800
Ted Kremenek93668002009-07-17 22:18:43 +0000801 case Stmt::ParenExprClass:
802 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000803 goto tryAgain;
804
Ted Kremenek93668002009-07-17 22:18:43 +0000805 case Stmt::NullStmtClass:
806 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000807
Ted Kremenek93668002009-07-17 22:18:43 +0000808 case Stmt::ReturnStmtClass:
809 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000810
Ted Kremenek93668002009-07-17 22:18:43 +0000811 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000812 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000813
Ted Kremenek93668002009-07-17 22:18:43 +0000814 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000815 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000816
Ted Kremenek93668002009-07-17 22:18:43 +0000817 case Stmt::SwitchStmtClass:
818 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000819
Ted Kremenek93668002009-07-17 22:18:43 +0000820 case Stmt::WhileStmtClass:
821 return VisitWhileStmt(cast<WhileStmt>(S));
822 }
823}
Mike Stump11289f42009-09-09 15:08:12 +0000824
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000825CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
826 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000827 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000828 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000829 }
Mike Stump11289f42009-09-09 15:08:12 +0000830
Ted Kremenek93668002009-07-17 22:18:43 +0000831 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000832}
Mike Stump31feda52009-07-17 01:31:16 +0000833
Ted Kremenek93668002009-07-17 22:18:43 +0000834/// VisitChildren - Visit the children of a Stmt.
835CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
836 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000837 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000838 E = Terminator->child_end(); I != E; ++I) {
839 if (*I) B = Visit(*I);
840 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000841 return B;
842}
Mike Stump11289f42009-09-09 15:08:12 +0000843
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000844CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
845 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000846 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000847
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000848 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000849 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000850 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000851 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000852
Ted Kremenek9aae5132007-08-23 21:42:29 +0000853 return Block;
854}
Mike Stump11289f42009-09-09 15:08:12 +0000855
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000856CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
857 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000858 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000859 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000860 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000861
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000862 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000863 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000864
Ted Kremenek93668002009-07-17 22:18:43 +0000865 // create the block evaluating the LHS
866 CFGBlock* LHSBlock = createBlock(false);
867 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000868
Ted Kremenek93668002009-07-17 22:18:43 +0000869 // create the block evaluating the RHS
870 Succ = ConfluenceBlock;
871 Block = NULL;
872 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000873
874 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000875 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000876 return 0;
877 }
878 else {
879 // Create an empty block for cases where the RHS doesn't require
880 // any explicit statements in the CFG.
881 RHSBlock = createBlock();
882 }
Mike Stump11289f42009-09-09 15:08:12 +0000883
Mike Stump773582d2009-07-23 23:25:26 +0000884 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000885 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000886 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000887 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000888
Ted Kremenek93668002009-07-17 22:18:43 +0000889 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000890 if (B->getOpcode() == BO_LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000891 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
892 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000893 } else {
John McCalle3027922010-08-25 11:45:40 +0000894 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000895 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
896 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000897 }
Mike Stump11289f42009-09-09 15:08:12 +0000898
Ted Kremenek93668002009-07-17 22:18:43 +0000899 // Generate the blocks for evaluating the LHS.
900 Block = LHSBlock;
901 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000902 }
John McCalle3027922010-08-25 11:45:40 +0000903 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000904 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000905 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000906 addStmt(B->getRHS());
907 return addStmt(B->getLHS());
908 }
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000909 else if (B->isAssignmentOp()) {
910 if (asc.alwaysAdd()) {
911 autoCreateBlock();
912 AppendStmt(Block, B, asc);
913 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000914
Ted Kremenek8abff772010-09-14 01:13:32 +0000915 // If visiting RHS causes us to finish 'Block' and the LHS doesn't
916 // create a new block, then we should return RBlock. Otherwise
917 // we'll incorrectly return NULL.
918 CFGBlock *RBlock = Visit(B->getRHS());
919 CFGBlock *LBlock = Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
920 return LBlock ? LBlock : RBlock;
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000921 }
Mike Stump11289f42009-09-09 15:08:12 +0000922
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000923 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000924}
925
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000926CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
927 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000928 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000929 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000930 }
931 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000932}
933
Ted Kremenek93668002009-07-17 22:18:43 +0000934CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
935 // "break" is a control-flow statement. Thus we stop processing the current
936 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000937 if (badCFG)
938 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000939
Ted Kremenek93668002009-07-17 22:18:43 +0000940 // Now create a new block that ends with the break statement.
941 Block = createBlock(false);
942 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000943
Ted Kremenek93668002009-07-17 22:18:43 +0000944 // If there is no target for the break, then we are looking at an incomplete
945 // AST. This means that the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000946 if (BreakJumpTarget.Block) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +0000947 addAutomaticObjDtors(ScopePos, BreakJumpTarget.ScopePos, B);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000948 AddSuccessor(Block, BreakJumpTarget.Block);
949 } else
Ted Kremenek93668002009-07-17 22:18:43 +0000950 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000951
952
Ted Kremenek9aae5132007-08-23 21:42:29 +0000953 return Block;
954}
Mike Stump11289f42009-09-09 15:08:12 +0000955
Mike Stump04c68512010-01-21 15:20:48 +0000956static bool CanThrow(Expr *E) {
957 QualType Ty = E->getType();
958 if (Ty->isFunctionPointerType())
959 Ty = Ty->getAs<PointerType>()->getPointeeType();
960 else if (Ty->isBlockPointerType())
961 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000962
Mike Stump04c68512010-01-21 15:20:48 +0000963 const FunctionType *FT = Ty->getAs<FunctionType>();
964 if (FT) {
965 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
966 if (Proto->hasEmptyExceptionSpec())
967 return false;
968 }
969 return true;
970}
971
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000972CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000973 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000974 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000975 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000976 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000977 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000978
Mike Stump04c68512010-01-21 15:20:48 +0000979 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000980
981 // Languages without exceptions are assumed to not throw.
982 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000983 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +0000984 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +0000985 }
986
987 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000988 if (FD->hasAttr<NoReturnAttr>())
989 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +0000990 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +0000991 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000992 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000993
Mike Stump04c68512010-01-21 15:20:48 +0000994 if (!CanThrow(C->getCallee()))
995 AddEHEdge = false;
996
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000997 if (!NoReturn && !AddEHEdge) {
998 if (asc.asLValue())
999 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
1000 else
1001 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
1002 }
Mike Stump11289f42009-09-09 15:08:12 +00001003
Mike Stump92244b02010-01-19 22:00:14 +00001004 if (Block) {
1005 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001006 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +00001007 return 0;
1008 }
Mike Stump11289f42009-09-09 15:08:12 +00001009
Mike Stump92244b02010-01-19 22:00:14 +00001010 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001011 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +00001012
Mike Stump92244b02010-01-19 22:00:14 +00001013 if (NoReturn) {
1014 // Wire this to the exit block directly.
1015 AddSuccessor(Block, &cfg->getExit());
1016 }
Mike Stump04c68512010-01-21 15:20:48 +00001017 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +00001018 // Add exceptional edges.
1019 if (TryTerminatedBlock)
1020 AddSuccessor(Block, TryTerminatedBlock);
1021 else
1022 AddSuccessor(Block, &cfg->getExit());
1023 }
Mike Stump11289f42009-09-09 15:08:12 +00001024
Mike Stump8c5d7992009-07-25 21:26:53 +00001025 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +00001026}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001027
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001028CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1029 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +00001030 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001031 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001032 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001033 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001034
Ted Kremenek5868ec62010-04-11 17:02:10 +00001035 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1036 : AddStmtChoice::AlwaysAdd;
1037
Ted Kremenek21822592009-07-17 18:20:32 +00001038 Succ = ConfluenceBlock;
1039 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001040 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001041 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001042 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001043
Ted Kremenek21822592009-07-17 18:20:32 +00001044 Succ = ConfluenceBlock;
1045 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001046 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001047 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001048 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001049
Ted Kremenek21822592009-07-17 18:20:32 +00001050 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +00001051 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001052 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001053 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1054 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +00001055 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +00001056 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +00001057}
Mike Stump11289f42009-09-09 15:08:12 +00001058
1059
1060CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001061 addLocalScopeAndDtors(C);
Mike Stump11289f42009-09-09 15:08:12 +00001062 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001063
1064 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1065 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +00001066 // If we hit a segment of code just containing ';' (NullStmts), we can
1067 // get a null block back. In such cases, just use the LastBlock
1068 if (CFGBlock *newBlock = addStmt(*I))
1069 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001070
Ted Kremenekce499c22009-08-27 23:16:26 +00001071 if (badCFG)
1072 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001073 }
Mike Stump92244b02010-01-19 22:00:14 +00001074
Ted Kremenek93668002009-07-17 22:18:43 +00001075 return LastBlock;
1076}
Mike Stump11289f42009-09-09 15:08:12 +00001077
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001078CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1079 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +00001080 // Create the confluence block that will "merge" the results of the ternary
1081 // expression.
1082 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001083 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001084 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001085 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001086
Ted Kremenek5868ec62010-04-11 17:02:10 +00001087 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1088 : AddStmtChoice::AlwaysAdd;
1089
Ted Kremenek51d40b02009-07-17 18:15:54 +00001090 // Create a block for the LHS expression if there is an LHS expression. A
1091 // GCC extension allows LHS to be NULL, causing the condition to be the
1092 // value that is returned instead.
1093 // e.g: x ?: y is shorthand for: x ? x : y;
1094 Succ = ConfluenceBlock;
1095 Block = NULL;
1096 CFGBlock* LHSBlock = NULL;
1097 if (C->getLHS()) {
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001098 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001099 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001100 return 0;
1101 Block = NULL;
1102 }
Mike Stump11289f42009-09-09 15:08:12 +00001103
Ted Kremenek51d40b02009-07-17 18:15:54 +00001104 // Create the block for the RHS expression.
1105 Succ = ConfluenceBlock;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001106 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001107 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001108 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001109
Ted Kremenek51d40b02009-07-17 18:15:54 +00001110 // Create the block that will contain the condition.
1111 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +00001112
Mike Stump773582d2009-07-23 23:25:26 +00001113 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001114 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +00001115 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001116 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +00001117 } else {
Ted Kremenek30754282009-07-24 04:47:11 +00001118 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +00001119 // If we know the condition is false, add NULL as the successor for
1120 // the block containing the condition. In this case, the confluence
1121 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001122 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +00001123 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +00001124 } else {
1125 // If we have no LHS expression, add the ConfluenceBlock as a direct
1126 // successor for the block containing the condition. Moreover, we need to
1127 // reverse the order of the predecessors in the ConfluenceBlock because
1128 // the RHSBlock will have been added to the succcessors already, and we
1129 // want the first predecessor to the the block containing the expression
1130 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001131 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +00001132 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +00001133 std::reverse(ConfluenceBlock->pred_begin(),
1134 ConfluenceBlock->pred_end());
1135 }
Ted Kremenek51d40b02009-07-17 18:15:54 +00001136 }
Mike Stump11289f42009-09-09 15:08:12 +00001137
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001138 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001139 Block->setTerminator(C);
1140 return addStmt(C->getCond());
1141}
1142
Ted Kremenek93668002009-07-17 22:18:43 +00001143CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
1144 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001145
Ted Kremenek93668002009-07-17 22:18:43 +00001146 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001147 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +00001148 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +00001149 }
Mike Stump11289f42009-09-09 15:08:12 +00001150
Ted Kremenek93668002009-07-17 22:18:43 +00001151 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001152
Ted Kremenek93668002009-07-17 22:18:43 +00001153 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1154 typedef llvm::SmallVector<Decl*,10> BufTy;
1155 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +00001156
Ted Kremenek93668002009-07-17 22:18:43 +00001157 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1158 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1159 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1160 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +00001161
Ted Kremenek93668002009-07-17 22:18:43 +00001162 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1163 // automatically freed with the CFG.
1164 DeclGroupRef DG(*I);
1165 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001166 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +00001167 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +00001168
Ted Kremenek93668002009-07-17 22:18:43 +00001169 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001170 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +00001171 B = VisitDeclSubExpr(D);
1172 }
Mike Stump11289f42009-09-09 15:08:12 +00001173
1174 return B;
Ted Kremenek93668002009-07-17 22:18:43 +00001175}
Mike Stump11289f42009-09-09 15:08:12 +00001176
Ted Kremenek93668002009-07-17 22:18:43 +00001177/// VisitDeclSubExpr - Utility method to add block-level expressions for
1178/// initializers in Decls.
1179CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
1180 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +00001181
Ted Kremenek93668002009-07-17 22:18:43 +00001182 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +00001183
Ted Kremenek93668002009-07-17 22:18:43 +00001184 if (!VD)
1185 return Block;
Mike Stump11289f42009-09-09 15:08:12 +00001186
Ted Kremenek93668002009-07-17 22:18:43 +00001187 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +00001188
Ted Kremenek93668002009-07-17 22:18:43 +00001189 if (Init) {
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +00001190 AddStmtChoice::Kind k =
1191 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
1192 : AddStmtChoice::NotAlwaysAdd;
1193 Visit(Init, AddStmtChoice(k));
Ted Kremenek93668002009-07-17 22:18:43 +00001194 }
Mike Stump11289f42009-09-09 15:08:12 +00001195
Ted Kremenek93668002009-07-17 22:18:43 +00001196 // If the type of VD is a VLA, then we must process its size expressions.
1197 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
1198 VA = FindVA(VA->getElementType().getTypePtr()))
1199 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001200
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001201 // Remove variable from local scope.
1202 if (ScopePos && VD == *ScopePos)
1203 ++ScopePos;
1204
Ted Kremenek93668002009-07-17 22:18:43 +00001205 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001206}
1207
1208CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001209 // We may see an if statement in the middle of a basic block, or it may be the
1210 // first statement we are processing. In either case, we create a new basic
1211 // block. First, we create the blocks for the then...else statements, and
1212 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +00001213 // middle of a block, we stop processing that block. That block is then the
1214 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +00001215
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001216 // Save local scope position because in case of condition variable ScopePos
1217 // won't be restored when traversing AST.
1218 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1219
1220 // Create local scope for possible condition variable.
1221 // Store scope position. Add implicit destructor.
1222 if (VarDecl* VD = I->getConditionVariable()) {
1223 LocalScope::const_iterator BeginScopePos = ScopePos;
1224 addLocalScopeForVarDecl(VD);
1225 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1226 }
1227
Mike Stump31feda52009-07-17 01:31:16 +00001228 // The block we were proccessing is now finished. Make it the successor
1229 // block.
1230 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001231 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001232 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001233 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001234 }
Mike Stump31feda52009-07-17 01:31:16 +00001235
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001236 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001237 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001238
Ted Kremenek9aae5132007-08-23 21:42:29 +00001239 if (Stmt* Else = I->getElse()) {
1240 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001241
Ted Kremenek9aae5132007-08-23 21:42:29 +00001242 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +00001243 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001244 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001245
1246 // If branch is not a compound statement create implicit scope
1247 // and add destructors.
1248 if (!isa<CompoundStmt>(Else))
1249 addLocalScopeAndDtors(Else);
1250
Ted Kremenek93668002009-07-17 22:18:43 +00001251 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +00001252
Ted Kremenekbbad8ce2007-08-30 18:13:31 +00001253 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1254 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +00001255 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001256 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001257 return 0;
1258 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001259 }
Mike Stump31feda52009-07-17 01:31:16 +00001260
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001261 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001262 CFGBlock* ThenBlock;
1263 {
1264 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001265 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001266 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001267 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001268
1269 // If branch is not a compound statement create implicit scope
1270 // and add destructors.
1271 if (!isa<CompoundStmt>(Then))
1272 addLocalScopeAndDtors(Then);
1273
Ted Kremenek93668002009-07-17 22:18:43 +00001274 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +00001275
Ted Kremenek1b379512009-04-01 03:52:47 +00001276 if (!ThenBlock) {
1277 // We can reach here if the "then" body has all NullStmts.
1278 // Create an empty block so we can distinguish between true and false
1279 // branches in path-sensitive analyses.
1280 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001281 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001282 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001283 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001284 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001285 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001286 }
1287
Mike Stump31feda52009-07-17 01:31:16 +00001288 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001289 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001290
Ted Kremenek9aae5132007-08-23 21:42:29 +00001291 // Set the terminator of the new block to the If statement.
1292 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001293
Mike Stump773582d2009-07-23 23:25:26 +00001294 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001295 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001296
Ted Kremenek9aae5132007-08-23 21:42:29 +00001297 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001298 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1299 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001300
1301 // Add the condition as the last statement in the new block. This may create
1302 // new blocks as the condition may contain control-flow. Any newly created
1303 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001304 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001305
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001306 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1307 // and the condition variable initialization to the CFG.
1308 if (VarDecl *VD = I->getConditionVariable()) {
1309 if (Expr *Init = VD->getInit()) {
1310 autoCreateBlock();
1311 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1312 addStmt(Init);
1313 }
1314 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001315
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001316 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001317}
Mike Stump31feda52009-07-17 01:31:16 +00001318
1319
Ted Kremenek9aae5132007-08-23 21:42:29 +00001320CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001321 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001322 //
Mike Stump31feda52009-07-17 01:31:16 +00001323 // NOTE: If a "return" appears in the middle of a block, this means that the
1324 // code afterwards is DEAD (unreachable). We still keep a basic block
1325 // for that code; a simple "mark-and-sweep" from the entry block will be
1326 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001327
1328 // Create the new block.
1329 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001330
Ted Kremenek9aae5132007-08-23 21:42:29 +00001331 // The Exit block is the only successor.
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001332 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001333 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001334
1335 // Add the return statement to the block. This may create new blocks if R
1336 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001337 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001338}
1339
1340CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1341 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001342 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001343 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001344
Ted Kremenek93668002009-07-17 22:18:43 +00001345 if (!LabelBlock) // This can happen when the body is empty, i.e.
1346 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001347
Ted Kremenek93668002009-07-17 22:18:43 +00001348 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001349 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001350
1351 // Labels partition blocks, so this is the end of the basic block we were
1352 // processing (L is the block's label). Because this is label (and we have
1353 // already processed the substatement) there is no extra control-flow to worry
1354 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001355 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001356 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001357 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001358
1359 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001360 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001361
Ted Kremenek9aae5132007-08-23 21:42:29 +00001362 // This block is now the implicit successor of other blocks.
1363 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001364
Ted Kremenek9aae5132007-08-23 21:42:29 +00001365 return LabelBlock;
1366}
1367
1368CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001369 // Goto is a control-flow statement. Thus we stop processing the current
1370 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001371
Ted Kremenek9aae5132007-08-23 21:42:29 +00001372 Block = createBlock(false);
1373 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001374
1375 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001376 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001377
Ted Kremenek9aae5132007-08-23 21:42:29 +00001378 if (I == LabelMap.end())
1379 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001380 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1381 else {
1382 JumpTarget JT = I->second;
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001383 addAutomaticObjDtors(ScopePos, JT.ScopePos, G);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001384 AddSuccessor(Block, JT.Block);
1385 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001386
Mike Stump31feda52009-07-17 01:31:16 +00001387 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001388}
1389
1390CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001391 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001392
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001393 // Save local scope position because in case of condition variable ScopePos
1394 // won't be restored when traversing AST.
1395 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1396
1397 // Create local scope for init statement and possible condition variable.
1398 // Add destructor for init statement and condition variable.
1399 // Store scope position for continue statement.
1400 if (Stmt* Init = F->getInit())
1401 addLocalScopeForStmt(Init);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001402 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1403
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001404 if (VarDecl* VD = F->getConditionVariable())
1405 addLocalScopeForVarDecl(VD);
1406 LocalScope::const_iterator ContinueScopePos = ScopePos;
1407
1408 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1409
Mike Stump014b3ea2009-07-21 01:12:51 +00001410 // "for" is a control-flow statement. Thus we stop processing the current
1411 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001412 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001413 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001414 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001415 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001416 } else
1417 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001418
Ted Kremenek304a9532010-05-21 20:30:15 +00001419 // Save the current value for the break targets.
1420 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001421 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001422 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001423
Mike Stump31feda52009-07-17 01:31:16 +00001424 // Because of short-circuit evaluation, the condition of the loop can span
1425 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1426 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001427 CFGBlock* ExitConditionBlock = createBlock(false);
1428 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001429
Ted Kremenek81e14852007-08-27 19:46:09 +00001430 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001431 ExitConditionBlock->setTerminator(F);
1432
1433 // Now add the actual condition to the condition block. Because the condition
1434 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001435 if (Stmt* C = F->getCond()) {
1436 Block = ExitConditionBlock;
1437 EntryConditionBlock = addStmt(C);
Ted Kremenek7b31a612010-09-15 07:01:20 +00001438 assert(Block == EntryConditionBlock ||
1439 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001440
1441 // If this block contains a condition variable, add both the condition
1442 // variable and initializer to the CFG.
1443 if (VarDecl *VD = F->getConditionVariable()) {
1444 if (Expr *Init = VD->getInit()) {
1445 autoCreateBlock();
1446 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1447 EntryConditionBlock = addStmt(Init);
1448 assert(Block == EntryConditionBlock);
1449 }
1450 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001451
Ted Kremenek55957a82009-05-02 00:13:27 +00001452 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001453 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001454 return 0;
1455 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001456 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001457
Mike Stump31feda52009-07-17 01:31:16 +00001458 // The condition block is the implicit successor for the loop body as well as
1459 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001460 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001461
Mike Stump773582d2009-07-23 23:25:26 +00001462 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001463 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001464
Mike Stump773582d2009-07-23 23:25:26 +00001465 if (F->getCond())
1466 KnownVal = TryEvaluateBool(F->getCond());
1467
Ted Kremenek9aae5132007-08-23 21:42:29 +00001468 // Now create the loop body.
1469 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001470 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001471
Ted Kremenek304a9532010-05-21 20:30:15 +00001472 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001473 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1474 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001475
Ted Kremeneke9610502007-08-30 18:39:40 +00001476 // Create a new block to contain the (bottom) of the loop body.
1477 Block = NULL;
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001478
1479 // Loop body should end with destructor of Condition variable (if any).
1480 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump31feda52009-07-17 01:31:16 +00001481
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001482 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001483 // Generate increment code in its own basic block. This is the target of
1484 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001485 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001486 } else {
1487 // No increment code. Create a special, empty, block that is used as the
1488 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001489 assert(Succ == EntryConditionBlock);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001490 Succ = Block ? Block : createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001491 }
Mike Stump31feda52009-07-17 01:31:16 +00001492
Ted Kremenek902393b2009-04-28 00:51:56 +00001493 // Finish up the increment (or empty) block if it hasn't been already.
1494 if (Block) {
1495 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001496 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001497 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001498 Block = 0;
1499 }
Mike Stump31feda52009-07-17 01:31:16 +00001500
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001501 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001502
Ted Kremenek902393b2009-04-28 00:51:56 +00001503 // The starting block for the loop increment is the block that should
1504 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001505 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001506
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001507 // If body is not a compound statement create implicit scope
1508 // and add destructors.
1509 if (!isa<CompoundStmt>(F->getBody()))
1510 addLocalScopeAndDtors(F->getBody());
1511
Mike Stump31feda52009-07-17 01:31:16 +00001512 // Now populate the body block, and in the process create new blocks as we
1513 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001514 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001515
1516 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001517 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001518 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001519 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001520
Ted Kremenek30754282009-07-24 04:47:11 +00001521 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001522 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001523 }
Mike Stump31feda52009-07-17 01:31:16 +00001524
Ted Kremenek30754282009-07-24 04:47:11 +00001525 // Link up the condition block with the code that follows the loop. (the
1526 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001527 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001528
Ted Kremenek9aae5132007-08-23 21:42:29 +00001529 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001530 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001531 if (Stmt* I = F->getInit()) {
1532 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001533 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001534 } else {
1535 // There is no loop initialization. We are thus basically a while loop.
1536 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001537 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001538 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001539 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001540 }
1541}
1542
Ted Kremenek5868ec62010-04-11 17:02:10 +00001543CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1544 if (asc.alwaysAdd()) {
1545 autoCreateBlock();
1546 AppendStmt(Block, M, asc);
1547 }
1548 return Visit(M->getBase(),
1549 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1550 : AddStmtChoice::AsLValueNotAlwaysAdd);
1551}
1552
Ted Kremenek9d56e642008-11-11 17:10:00 +00001553CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1554 // Objective-C fast enumeration 'for' statements:
1555 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1556 //
1557 // for ( Type newVariable in collection_expression ) { statements }
1558 //
1559 // becomes:
1560 //
1561 // prologue:
1562 // 1. collection_expression
1563 // T. jump to loop_entry
1564 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001565 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001566 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1567 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1568 // TB:
1569 // statements
1570 // T. jump to loop_entry
1571 // FB:
1572 // what comes after
1573 //
1574 // and
1575 //
1576 // Type existingItem;
1577 // for ( existingItem in expression ) { statements }
1578 //
1579 // becomes:
1580 //
Mike Stump31feda52009-07-17 01:31:16 +00001581 // the same with newVariable replaced with existingItem; the binding works
1582 // the same except that for one ObjCForCollectionStmt::getElement() returns
1583 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001584 //
Mike Stump31feda52009-07-17 01:31:16 +00001585
Ted Kremenek9d56e642008-11-11 17:10:00 +00001586 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001587
Ted Kremenek9d56e642008-11-11 17:10:00 +00001588 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001589 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001590 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001591 LoopSuccessor = Block;
1592 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001593 } else
1594 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001595
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001596 // Build the condition blocks.
1597 CFGBlock* ExitConditionBlock = createBlock(false);
1598 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001599
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001600 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001601 ExitConditionBlock->setTerminator(S);
1602
1603 // The last statement in the block should be the ObjCForCollectionStmt, which
1604 // performs the actual binding to 'element' and determines if there are any
1605 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001606 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001607 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001608
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001609 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001610 // generate new blocks as necesary. We DON'T add the statement by default to
1611 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001612 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001613 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001614 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001615 return 0;
1616 Block = 0;
1617 }
Mike Stump31feda52009-07-17 01:31:16 +00001618
1619 // The condition block is the implicit successor for the loop body as well as
1620 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001621 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001622
Ted Kremenek9d56e642008-11-11 17:10:00 +00001623 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001624 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001625 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001626 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1627 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1628 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001629
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001630 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1631 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001632
Ted Kremenek93668002009-07-17 22:18:43 +00001633 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001634
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001635 if (!BodyBlock)
1636 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001637 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001638 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001639 return 0;
1640 }
Mike Stump31feda52009-07-17 01:31:16 +00001641
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001642 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001643 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001644 }
Mike Stump31feda52009-07-17 01:31:16 +00001645
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001646 // Link up the condition block with the code that follows the loop.
1647 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001648 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001649
Ted Kremenek9d56e642008-11-11 17:10:00 +00001650 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001651 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001652 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001653}
1654
Ted Kremenek49805452009-05-02 01:49:13 +00001655CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1656 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001657
Ted Kremenek49805452009-05-02 01:49:13 +00001658 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001659 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001660
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001661 // The sync body starts its own basic block. This makes it a little easier
1662 // for diagnostic clients.
1663 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001664 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001665 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001666
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001667 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001668 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001669 }
Mike Stump31feda52009-07-17 01:31:16 +00001670
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001671 // Add the @synchronized to the CFG.
1672 autoCreateBlock();
1673 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1674
Ted Kremenek49805452009-05-02 01:49:13 +00001675 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001676 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001677}
Mike Stump31feda52009-07-17 01:31:16 +00001678
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001679CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001680 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001681 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001682}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001683
Ted Kremenek9aae5132007-08-23 21:42:29 +00001684CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001685 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001686
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001687 // Save local scope position because in case of condition variable ScopePos
1688 // won't be restored when traversing AST.
1689 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1690
1691 // Create local scope for possible condition variable.
1692 // Store scope position for continue statement.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001693 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001694 if (VarDecl* VD = W->getConditionVariable()) {
1695 addLocalScopeForVarDecl(VD);
1696 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1697 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001698
Mike Stump014b3ea2009-07-21 01:12:51 +00001699 // "while" is a control-flow statement. Thus we stop processing the current
1700 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001701 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001702 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001703 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001704 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001705 } else
1706 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001707
1708 // Because of short-circuit evaluation, the condition of the loop can span
1709 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1710 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001711 CFGBlock* ExitConditionBlock = createBlock(false);
1712 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001713
Ted Kremenek81e14852007-08-27 19:46:09 +00001714 // Set the terminator for the "exit" condition block.
1715 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001716
1717 // Now add the actual condition to the condition block. Because the condition
1718 // itself may contain control-flow, new blocks may be created. Thus we update
1719 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001720 if (Stmt* C = W->getCond()) {
1721 Block = ExitConditionBlock;
1722 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001723 assert(Block == EntryConditionBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001724
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001725 // If this block contains a condition variable, add both the condition
1726 // variable and initializer to the CFG.
1727 if (VarDecl *VD = W->getConditionVariable()) {
1728 if (Expr *Init = VD->getInit()) {
1729 autoCreateBlock();
1730 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1731 EntryConditionBlock = addStmt(Init);
1732 assert(Block == EntryConditionBlock);
1733 }
1734 }
1735
Ted Kremenek55957a82009-05-02 00:13:27 +00001736 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001737 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001738 return 0;
1739 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001740 }
Mike Stump31feda52009-07-17 01:31:16 +00001741
1742 // The condition block is the implicit successor for the loop body as well as
1743 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001744 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001745
Mike Stump773582d2009-07-23 23:25:26 +00001746 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001747 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001748
Ted Kremenek9aae5132007-08-23 21:42:29 +00001749 // Process the loop body.
1750 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001751 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001752
1753 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001754 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1755 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1756 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001757
Mike Stump31feda52009-07-17 01:31:16 +00001758 // Create an empty block to represent the transition block for looping back
1759 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001760 Block = 0;
1761 assert(Succ == EntryConditionBlock);
1762 Succ = createBlock();
1763 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001764 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001765
Ted Kremenek9aae5132007-08-23 21:42:29 +00001766 // All breaks should go to the code following the loop.
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001767 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001768
Ted Kremenek9aae5132007-08-23 21:42:29 +00001769 // NULL out Block to force lazy instantiation of blocks for the body.
1770 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001771
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001772 // Loop body should end with destructor of Condition variable (if any).
1773 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1774
1775 // If body is not a compound statement create implicit scope
1776 // and add destructors.
1777 if (!isa<CompoundStmt>(W->getBody()))
1778 addLocalScopeAndDtors(W->getBody());
1779
Ted Kremenek9aae5132007-08-23 21:42:29 +00001780 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001781 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001782
Ted Kremeneke9610502007-08-30 18:39:40 +00001783 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001784 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001785 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001786 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001787 return 0;
1788 }
Mike Stump31feda52009-07-17 01:31:16 +00001789
Ted Kremenek30754282009-07-24 04:47:11 +00001790 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001791 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001792 }
Mike Stump31feda52009-07-17 01:31:16 +00001793
Ted Kremenek30754282009-07-24 04:47:11 +00001794 // Link up the condition block with the code that follows the loop. (the
1795 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001796 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001797
1798 // There can be no more statements in the condition block since we loop back
1799 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001800 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001801
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001802 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001803 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001804 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001805}
Mike Stump11289f42009-09-09 15:08:12 +00001806
1807
Ted Kremenek93668002009-07-17 22:18:43 +00001808CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1809 // FIXME: For now we pretend that @catch and the code it contains does not
1810 // exit.
1811 return Block;
1812}
Mike Stump31feda52009-07-17 01:31:16 +00001813
Ted Kremenek93041ba2008-12-09 20:20:09 +00001814CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1815 // FIXME: This isn't complete. We basically treat @throw like a return
1816 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001817
Ted Kremenek0868eea2009-09-24 18:45:41 +00001818 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001819 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001820 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001821
Ted Kremenek93041ba2008-12-09 20:20:09 +00001822 // Create the new block.
1823 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001824
Ted Kremenek93041ba2008-12-09 20:20:09 +00001825 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001826 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001827
1828 // Add the statement to the block. This may create new blocks if S contains
1829 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001830 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001831}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001832
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001833CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001834 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001835 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001836 return 0;
1837
1838 // Create the new block.
1839 Block = createBlock(false);
1840
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001841 if (TryTerminatedBlock)
1842 // The current try statement is the only successor.
1843 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001844 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001845 // otherwise the Exit block is the only successor.
1846 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001847
1848 // Add the statement to the block. This may create new blocks if S contains
1849 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001850 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001851}
1852
Ted Kremenek93668002009-07-17 22:18:43 +00001853CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001854 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001855
Mike Stump8d50b6a2009-07-21 01:27:50 +00001856 // "do...while" is a control-flow statement. Thus we stop processing the
1857 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001858 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001859 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001860 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001861 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001862 } else
1863 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001864
1865 // Because of short-circuit evaluation, the condition of the loop can span
1866 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1867 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001868 CFGBlock* ExitConditionBlock = createBlock(false);
1869 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001870
Ted Kremenek81e14852007-08-27 19:46:09 +00001871 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001872 ExitConditionBlock->setTerminator(D);
1873
1874 // Now add the actual condition to the condition block. Because the condition
1875 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001876 if (Stmt* C = D->getCond()) {
1877 Block = ExitConditionBlock;
1878 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001879 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001880 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001881 return 0;
1882 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001883 }
Mike Stump31feda52009-07-17 01:31:16 +00001884
Ted Kremeneka1523a32008-02-27 07:20:00 +00001885 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001886 Succ = EntryConditionBlock;
1887
Mike Stump773582d2009-07-23 23:25:26 +00001888 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001889 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001890
Ted Kremenek9aae5132007-08-23 21:42:29 +00001891 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001892 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001893 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001894 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001895
Ted Kremenek9aae5132007-08-23 21:42:29 +00001896 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001897 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1898 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1899 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001900
Ted Kremenek9aae5132007-08-23 21:42:29 +00001901 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001902 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001903
Ted Kremenek9aae5132007-08-23 21:42:29 +00001904 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001905 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001906
Ted Kremenek9aae5132007-08-23 21:42:29 +00001907 // NULL out Block to force lazy instantiation of blocks for the body.
1908 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001909
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001910 // If body is not a compound statement create implicit scope
1911 // and add destructors.
1912 if (!isa<CompoundStmt>(D->getBody()))
1913 addLocalScopeAndDtors(D->getBody());
1914
Ted Kremenek9aae5132007-08-23 21:42:29 +00001915 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001916 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001917
Ted Kremeneke9610502007-08-30 18:39:40 +00001918 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001919 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001920 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001921 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001922 return 0;
1923 }
Mike Stump31feda52009-07-17 01:31:16 +00001924
Ted Kremenek110974d2010-08-17 20:59:56 +00001925 if (!KnownVal.isFalse()) {
1926 // Add an intermediate block between the BodyBlock and the
1927 // ExitConditionBlock to represent the "loop back" transition. Create an
1928 // empty block to represent the transition block for looping back to the
1929 // head of the loop.
1930 // FIXME: Can we do this more efficiently without adding another block?
1931 Block = NULL;
1932 Succ = BodyBlock;
1933 CFGBlock *LoopBackBlock = createBlock();
1934 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001935
Ted Kremenek110974d2010-08-17 20:59:56 +00001936 // Add the loop body entry as a successor to the condition.
1937 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1938 }
1939 else
1940 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001941 }
Mike Stump31feda52009-07-17 01:31:16 +00001942
Ted Kremenek30754282009-07-24 04:47:11 +00001943 // Link up the condition block with the code that follows the loop.
1944 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001945 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001946
1947 // There can be no more statements in the body block(s) since we loop back to
1948 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001949 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001950
Ted Kremenek9aae5132007-08-23 21:42:29 +00001951 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001952 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001953 return BodyBlock;
1954}
1955
1956CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1957 // "continue" is a control-flow statement. Thus we stop processing the
1958 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001959 if (badCFG)
1960 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001961
Ted Kremenek9aae5132007-08-23 21:42:29 +00001962 // Now create a new block that ends with the continue statement.
1963 Block = createBlock(false);
1964 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001965
Ted Kremenek9aae5132007-08-23 21:42:29 +00001966 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001967 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001968 if (ContinueJumpTarget.Block) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001969 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001970 AddSuccessor(Block, ContinueJumpTarget.Block);
1971 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00001972 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001973
Ted Kremenek9aae5132007-08-23 21:42:29 +00001974 return Block;
1975}
Mike Stump11289f42009-09-09 15:08:12 +00001976
Ted Kremenek0747de62009-07-18 00:47:21 +00001977CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001978 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001979
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001980 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001981 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001982 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001983 }
Mike Stump11289f42009-09-09 15:08:12 +00001984
Ted Kremenek93668002009-07-17 22:18:43 +00001985 // VLA types have expressions that must be evaluated.
1986 if (E->isArgumentType()) {
1987 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1988 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1989 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001990 }
Mike Stump11289f42009-09-09 15:08:12 +00001991
Mike Stump31feda52009-07-17 01:31:16 +00001992 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001993}
Mike Stump11289f42009-09-09 15:08:12 +00001994
Ted Kremenek93668002009-07-17 22:18:43 +00001995/// VisitStmtExpr - Utility method to handle (nested) statement
1996/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001997CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1998 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001999 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002000 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00002001 }
Ted Kremenek93668002009-07-17 22:18:43 +00002002 return VisitCompoundStmt(SE->getSubStmt());
2003}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002004
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002005CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00002006 // "switch" is a control-flow statement. Thus we stop processing the current
2007 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002008 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002009
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002010 // Save local scope position because in case of condition variable ScopePos
2011 // won't be restored when traversing AST.
2012 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2013
2014 // Create local scope for possible condition variable.
2015 // Store scope position. Add implicit destructor.
2016 if (VarDecl* VD = Terminator->getConditionVariable()) {
2017 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2018 addLocalScopeForVarDecl(VD);
2019 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2020 }
2021
Ted Kremenek9aae5132007-08-23 21:42:29 +00002022 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002023 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002024 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002025 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00002026 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002027
2028 // Save the current "switch" context.
2029 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00002030 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002031 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00002032
Mike Stump31feda52009-07-17 01:31:16 +00002033 // Set the "default" case to be the block after the switch statement. If the
2034 // switch statement contains a "default:", this value will be overwritten with
2035 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00002036 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00002037
Ted Kremenek9aae5132007-08-23 21:42:29 +00002038 // Create a new block that will contain the switch statement.
2039 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00002040
Ted Kremenek9aae5132007-08-23 21:42:29 +00002041 // Now process the switch body. The code after the switch is the implicit
2042 // successor.
2043 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002044 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002045
2046 // When visiting the body, the case statements should automatically get linked
2047 // up to the switch. We also don't keep a pointer to the body, since all
2048 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002049 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00002050 Block = NULL;
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002051
2052 // If body is not a compound statement create implicit scope
2053 // and add destructors.
2054 if (!isa<CompoundStmt>(Terminator->getBody()))
2055 addLocalScopeAndDtors(Terminator->getBody());
2056
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002057 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00002058 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002059 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002060 return 0;
2061 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002062
Mike Stump31feda52009-07-17 01:31:16 +00002063 // If we have no "default:" case, the default transition is to the code
2064 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002065 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002066
Ted Kremenek81e14852007-08-27 19:46:09 +00002067 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002068 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002069 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00002070 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002071 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002072
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002073 // Finally, if the SwitchStmt contains a condition variable, add both the
2074 // SwitchStmt and the condition variable initialization to the CFG.
2075 if (VarDecl *VD = Terminator->getConditionVariable()) {
2076 if (Expr *Init = VD->getInit()) {
2077 autoCreateBlock();
2078 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
2079 addStmt(Init);
2080 }
2081 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002082
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002083 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002084}
2085
Ted Kremenek93668002009-07-17 22:18:43 +00002086CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00002087 // CaseStmts are essentially labels, so they are the first statement in a
2088 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00002089 CFGBlock *TopBlock = 0, *LastBlock = 0;
2090
2091 if (Stmt *Sub = CS->getSubStmt()) {
2092 // For deeply nested chains of CaseStmts, instead of doing a recursion
2093 // (which can blow out the stack), manually unroll and create blocks
2094 // along the way.
2095 while (isa<CaseStmt>(Sub)) {
2096 CFGBlock *CurrentBlock = createBlock(false);
2097 CurrentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00002098
Ted Kremenek60fa6572010-08-04 23:54:30 +00002099 if (TopBlock)
2100 AddSuccessor(LastBlock, CurrentBlock);
2101 else
2102 TopBlock = CurrentBlock;
2103
2104 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
2105 LastBlock = CurrentBlock;
2106
2107 CS = cast<CaseStmt>(Sub);
2108 Sub = CS->getSubStmt();
2109 }
2110
2111 addStmt(Sub);
2112 }
Mike Stump11289f42009-09-09 15:08:12 +00002113
Ted Kremenek55e91e82007-08-30 18:48:11 +00002114 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002115 if (!CaseBlock)
2116 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002117
2118 // Cases statements partition blocks, so this is the top of the basic block we
2119 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00002120 CaseBlock->setLabel(CS);
2121
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002122 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002123 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002124
2125 // Add this block to the list of successors for the block with the switch
2126 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00002127 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002128 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002129
Ted Kremenek9aae5132007-08-23 21:42:29 +00002130 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2131 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002132
Ted Kremenek60fa6572010-08-04 23:54:30 +00002133 if (TopBlock) {
2134 AddSuccessor(LastBlock, CaseBlock);
2135 Succ = TopBlock;
2136 }
2137 else {
2138 // This block is now the implicit successor of other blocks.
2139 Succ = CaseBlock;
2140 }
Mike Stump31feda52009-07-17 01:31:16 +00002141
Ted Kremenek60fa6572010-08-04 23:54:30 +00002142 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002143}
Mike Stump31feda52009-07-17 01:31:16 +00002144
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002145CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00002146 if (Terminator->getSubStmt())
2147 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00002148
Ted Kremenek654c78f2008-02-13 22:05:39 +00002149 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002150
2151 if (!DefaultCaseBlock)
2152 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002153
2154 // Default statements partition blocks, so this is the top of the basic block
2155 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002156 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00002157
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002158 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002159 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00002160
Mike Stump31feda52009-07-17 01:31:16 +00002161 // Unlike case statements, we don't add the default block to the successors
2162 // for the switch statement immediately. This is done when we finish
2163 // processing the switch statement. This allows for the default case
2164 // (including a fall-through to the code after the switch statement) to always
2165 // be the last successor of a switch-terminated block.
2166
Ted Kremenek654c78f2008-02-13 22:05:39 +00002167 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2168 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002169
Ted Kremenek654c78f2008-02-13 22:05:39 +00002170 // This block is now the implicit successor of other blocks.
2171 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002172
2173 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00002174}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002175
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002176CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2177 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2178 // current block.
2179 CFGBlock* TrySuccessor = NULL;
2180
2181 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002182 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002183 return 0;
2184 TrySuccessor = Block;
2185 } else TrySuccessor = Succ;
2186
Mike Stump0bdba6c2010-01-20 01:15:34 +00002187 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002188
2189 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00002190 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002191 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00002192 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002193
Mike Stump0bdba6c2010-01-20 01:15:34 +00002194 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002195 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2196 // The code after the try is the implicit successor.
2197 Succ = TrySuccessor;
2198 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002199 if (CS->getExceptionDecl() == 0) {
2200 HasCatchAll = true;
2201 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002202 Block = NULL;
2203 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2204 if (CatchBlock == 0)
2205 return 0;
2206 // Add this block to the list of successors for the block with the try
2207 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00002208 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002209 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00002210 if (!HasCatchAll) {
2211 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00002212 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002213 else
Mike Stump845384a2010-01-20 01:30:58 +00002214 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00002215 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002216
2217 // The code after the try is the implicit successor.
2218 Succ = TrySuccessor;
2219
Mike Stump845384a2010-01-20 01:30:58 +00002220 // Save the current "try" context.
2221 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2222 TryTerminatedBlock = NewTryTerminatedBlock;
2223
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002224 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002225 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002226 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002227 return Block;
2228}
2229
2230CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2231 // CXXCatchStmt are treated like labels, so they are the first statement in a
2232 // block.
2233
Marcin Swiderski3546b1a2010-10-01 01:46:52 +00002234 // Save local scope position because in case of exception variable ScopePos
2235 // won't be restored when traversing AST.
2236 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2237
2238 // Create local scope for possible exception variable.
2239 // Store scope position. Add implicit destructor.
2240 if (VarDecl* VD = CS->getExceptionDecl()) {
2241 LocalScope::const_iterator BeginScopePos = ScopePos;
2242 addLocalScopeForVarDecl(VD);
2243 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2244 }
2245
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002246 if (CS->getHandlerBlock())
2247 addStmt(CS->getHandlerBlock());
2248
2249 CFGBlock* CatchBlock = Block;
2250 if (!CatchBlock)
2251 CatchBlock = createBlock();
2252
2253 CatchBlock->setLabel(CS);
2254
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002255 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002256 return 0;
2257
2258 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2259 Block = NULL;
2260
2261 return CatchBlock;
2262}
2263
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002264CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00002265 AddStmtChoice asc) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002266 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00002267 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00002268 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00002269 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00002270 return VisitChildren(C);
2271}
2272
Ted Kremenekeda180e22007-08-28 19:26:49 +00002273CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00002274 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00002275 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002276
Ted Kremenekeda180e22007-08-28 19:26:49 +00002277 if (!IBlock) {
2278 IBlock = createBlock(false);
2279 cfg->setIndirectGotoBlock(IBlock);
2280 }
Mike Stump31feda52009-07-17 01:31:16 +00002281
Ted Kremenekeda180e22007-08-28 19:26:49 +00002282 // IndirectGoto is a control-flow statement. Thus we stop processing the
2283 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002284 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00002285 return 0;
2286
Ted Kremenekeda180e22007-08-28 19:26:49 +00002287 Block = createBlock(false);
2288 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002289 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00002290 return addStmt(I->getTarget());
2291}
2292
Ted Kremenek04cca642007-08-23 21:26:19 +00002293} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00002294
Mike Stump31feda52009-07-17 01:31:16 +00002295/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2296/// no successors or predecessors. If this is the first block created in the
2297/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00002298CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00002299 bool first_block = begin() == end();
2300
2301 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002302 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2303 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2304 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00002305
2306 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002307 if (first_block)
2308 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00002309
2310 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002311 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002312}
2313
Ted Kremenek889073f2007-08-23 16:51:22 +00002314/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2315/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00002316CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002317 BuildOptions BO) {
Ted Kremenek889073f2007-08-23 16:51:22 +00002318 CFGBuilder Builder;
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002319 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek889073f2007-08-23 16:51:22 +00002320}
2321
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002322//===----------------------------------------------------------------------===//
2323// CFG: Queries for BlkExprs.
2324//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002325
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002326namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002327 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002328}
2329
Ted Kremenekbff98442009-12-23 23:37:10 +00002330static void FindSubExprAssignments(Stmt *S,
2331 llvm::SmallPtrSet<Expr*,50>& Set) {
2332 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00002333 return;
Mike Stump31feda52009-07-17 01:31:16 +00002334
Ted Kremenekbff98442009-12-23 23:37:10 +00002335 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002336 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00002337 if (!child)
2338 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002339
Ted Kremenekbff98442009-12-23 23:37:10 +00002340 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002341 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00002342
Ted Kremenekbff98442009-12-23 23:37:10 +00002343 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00002344 }
2345}
2346
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002347static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2348 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00002349
2350 // Look for assignments that are used as subexpressions. These are the only
2351 // assignments that we want to *possibly* register as a block-level
2352 // expression. Basically, if an assignment occurs both in a subexpression and
2353 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00002354 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00002355
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002356 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002357 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002358 if (CFGStmt S = BI->getAs<CFGStmt>())
2359 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002360
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002361 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00002362
2363 // Iterate over the statements again on identify the Expr* and Stmt* at the
2364 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002365
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002366 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2367 CFGStmt CS = BI->getAs<CFGStmt>();
2368 if (!CS.isValid())
2369 continue;
2370 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00002371
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002372 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002373 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00002374 // expression are really "statements" whose value is never used by
2375 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002376 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002377 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002378 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2379 // Special handling for statement expressions. The last statement in
2380 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002381 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002382 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002383 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002384 (*M)[C->body_back()] = x;
2385 }
2386 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002387
Ted Kremenek95a123c2008-01-26 00:03:27 +00002388 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002389 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002390 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002391 }
Mike Stump31feda52009-07-17 01:31:16 +00002392
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002393 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002394
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002395 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002396
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002397 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002398 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002399 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002400 }
2401 }
Mike Stump31feda52009-07-17 01:31:16 +00002402
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002403 return M;
2404}
2405
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002406CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2407 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002408 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002409
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002410 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002411 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002412 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002413}
2414
2415unsigned CFG::getNumBlkExprs() {
2416 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2417 return M->size();
2418 else {
2419 // We assume callers interested in the number of BlkExprs will want
2420 // the map constructed if it doesn't already exist.
2421 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2422 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
2423 }
2424}
2425
Ted Kremenek6065ef62008-04-28 18:00:46 +00002426//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002427// Filtered walking of the CFG.
2428//===----------------------------------------------------------------------===//
2429
2430bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002431 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002432
2433 if (F.IgnoreDefaultsWithCoveredEnums) {
2434 // If the 'To' has no label or is labeled but the label isn't a
2435 // CaseStmt then filter this edge.
2436 if (const SwitchStmt *S =
Ted Kremenekf146cd12010-09-09 02:57:48 +00002437 dyn_cast_or_null<SwitchStmt>(From->getTerminator())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002438 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00002439 const Stmt *L = To->getLabel();
2440 if (!L || !isa<CaseStmt>(L))
2441 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002442 }
2443 }
2444 }
2445
2446 return false;
2447}
2448
2449//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002450// Cleanup: CFG dstor.
2451//===----------------------------------------------------------------------===//
2452
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002453CFG::~CFG() {
2454 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2455}
Mike Stump31feda52009-07-17 01:31:16 +00002456
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002457//===----------------------------------------------------------------------===//
2458// CFG pretty printing
2459//===----------------------------------------------------------------------===//
2460
Ted Kremenek7e776b12007-08-22 18:22:34 +00002461namespace {
2462
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002463class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002464 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002465 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002466 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002467 DeclMapTy DeclMap;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002468 signed CurrentBlock;
2469 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002470 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002471public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002472
Chris Lattnerc61089a2009-06-30 01:26:17 +00002473 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2474 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002475 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2476 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002477 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002478 BI != BEnd; ++BI, ++j ) {
2479 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2480 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2481 StmtMap[SE] = P;
2482
2483 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2484 DeclMap[DS->getSingleDecl()] = P;
2485
2486 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2487 if (VarDecl* VD = IS->getConditionVariable())
2488 DeclMap[VD] = P;
2489
2490 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2491 if (VarDecl* VD = FS->getConditionVariable())
2492 DeclMap[VD] = P;
2493
2494 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2495 if (VarDecl* VD = WS->getConditionVariable())
2496 DeclMap[VD] = P;
2497
2498 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2499 if (VarDecl* VD = SS->getConditionVariable())
2500 DeclMap[VD] = P;
2501
2502 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2503 if (VarDecl* VD = CS->getExceptionDecl())
2504 DeclMap[VD] = P;
2505 }
2506 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002507 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002508 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002509 }
Mike Stump31feda52009-07-17 01:31:16 +00002510
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002511 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00002512
Chris Lattnerc61089a2009-06-30 01:26:17 +00002513 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002514 void setBlockID(signed i) { CurrentBlock = i; }
2515 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00002516
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002517 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2518 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002519
2520 if (I == StmtMap.end())
2521 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002522
2523 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00002524 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002525 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002526 }
Mike Stump31feda52009-07-17 01:31:16 +00002527
Ted Kremenek60983dc2010-01-19 20:52:05 +00002528 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002529 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002530 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002531
2532 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2533 DeclMapTy::iterator I = DeclMap.find(D);
2534
2535 if (I == DeclMap.end())
2536 return false;
2537
2538 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2539 && I->second.second == CurrentStmt) {
2540 return false;
2541 }
2542
2543 OS << "[B" << I->second.first << "." << I->second.second << "]";
2544 return true;
2545 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002546};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002547} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002548
Chris Lattnerc61089a2009-06-30 01:26:17 +00002549
2550namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002551class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002552 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002553
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002554 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002555 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002556 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002557public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002558 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002559 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002560 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002561
Ted Kremenek9aae5132007-08-23 21:42:29 +00002562 void VisitIfStmt(IfStmt* I) {
2563 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002564 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002565 }
Mike Stump31feda52009-07-17 01:31:16 +00002566
Ted Kremenek9aae5132007-08-23 21:42:29 +00002567 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002568 void VisitStmt(Stmt* Terminator) {
2569 Terminator->printPretty(OS, Helper, Policy);
2570 }
2571
Ted Kremenek9aae5132007-08-23 21:42:29 +00002572 void VisitForStmt(ForStmt* F) {
2573 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002574 if (F->getInit())
2575 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002576 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002577 if (Stmt* C = F->getCond())
2578 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002579 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002580 if (F->getInc())
2581 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002582 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002583 }
Mike Stump31feda52009-07-17 01:31:16 +00002584
Ted Kremenek9aae5132007-08-23 21:42:29 +00002585 void VisitWhileStmt(WhileStmt* W) {
2586 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002587 if (Stmt* C = W->getCond())
2588 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002589 }
Mike Stump31feda52009-07-17 01:31:16 +00002590
Ted Kremenek9aae5132007-08-23 21:42:29 +00002591 void VisitDoStmt(DoStmt* D) {
2592 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002593 if (Stmt* C = D->getCond())
2594 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002595 }
Mike Stump31feda52009-07-17 01:31:16 +00002596
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002597 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002598 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002599 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002600 }
Mike Stump31feda52009-07-17 01:31:16 +00002601
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002602 void VisitCXXTryStmt(CXXTryStmt* CS) {
2603 OS << "try ...";
2604 }
2605
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002606 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002607 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002608 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002609 }
Mike Stump31feda52009-07-17 01:31:16 +00002610
Ted Kremenek391f94a2007-08-31 22:29:13 +00002611 void VisitChooseExpr(ChooseExpr* C) {
2612 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002613 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002614 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002615 }
Mike Stump31feda52009-07-17 01:31:16 +00002616
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002617 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2618 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002619 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002620 }
Mike Stump31feda52009-07-17 01:31:16 +00002621
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002622 void VisitBinaryOperator(BinaryOperator* B) {
2623 if (!B->isLogicalOp()) {
2624 VisitExpr(B);
2625 return;
2626 }
Mike Stump31feda52009-07-17 01:31:16 +00002627
Douglas Gregor7de59662009-05-29 20:38:28 +00002628 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002629
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002630 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002631 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002632 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002633 return;
John McCalle3027922010-08-25 11:45:40 +00002634 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002635 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002636 return;
2637 default:
2638 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00002639 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002640 }
Mike Stump31feda52009-07-17 01:31:16 +00002641
Ted Kremenek12687ff2007-08-27 21:54:41 +00002642 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002643 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002644 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00002645};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002646} // end anonymous namespace
2647
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002648static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00002649 const CFGElement &E) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002650 if (CFGStmt CS = E.getAs<CFGStmt>()) {
2651 Stmt *S = CS;
2652
2653 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00002654
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002655 // special printing for statement-expressions.
2656 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
2657 CompoundStmt* Sub = SE->getSubStmt();
2658
2659 if (Sub->child_begin() != Sub->child_end()) {
2660 OS << "({ ... ; ";
2661 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
2662 OS << " })\n";
2663 return;
2664 }
2665 }
2666 // special printing for comma expressions.
2667 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
2668 if (B->getOpcode() == BO_Comma) {
2669 OS << "... , ";
2670 Helper->handledStmt(B->getRHS(),OS);
2671 OS << '\n';
2672 return;
2673 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002674 }
2675 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002676 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00002677
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002678 if (isa<CXXOperatorCallExpr>(S)) {
2679 OS << " (OperatorCall)";
Mike Stump31feda52009-07-17 01:31:16 +00002680 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002681 else if (isa<CXXBindTemporaryExpr>(S)) {
2682 OS << " (BindTemporary)";
2683 }
Mike Stump31feda52009-07-17 01:31:16 +00002684
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002685 // Expressions need a newline.
2686 if (isa<Expr>(S))
2687 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002688
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002689 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
2690 CXXBaseOrMemberInitializer* I = IE;
2691 if (I->isBaseInitializer())
2692 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
2693 else OS << I->getMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00002694
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002695 OS << "(";
2696 if (Expr* IE = I->getInit())
2697 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2698 OS << ")";
2699
2700 if (I->isBaseInitializer())
2701 OS << " (Base initializer)\n";
2702 else OS << " (Member initializer)\n";
2703
2704 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
2705 VarDecl* VD = DE.getVarDecl();
2706 Helper->handleDecl(VD, OS);
2707
2708 Type* T = VD->getType().getTypePtr();
2709 if (const ReferenceType* RT = T->getAs<ReferenceType>())
2710 T = RT->getPointeeType().getTypePtr();
2711
2712 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
2713 OS << " (Implicit destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00002714
2715 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
2716 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
2717 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00002718 OS << " (Base object destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00002719
2720 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
2721 FieldDecl *FD = ME.getFieldDecl();
2722 OS << "this->" << FD->getName();
2723 OS << ".~" << FD->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00002724 OS << " (Member object destructor)\n";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002725 }
2726 }
Mike Stump31feda52009-07-17 01:31:16 +00002727
Chris Lattnerc61089a2009-06-30 01:26:17 +00002728static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2729 const CFGBlock& B,
2730 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00002731
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002732 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00002733
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002734 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00002735 OS << "\n [ B" << B.getBlockID();
2736
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002737 if (&B == &cfg->getEntry())
2738 OS << " (ENTRY) ]\n";
2739 else if (&B == &cfg->getExit())
2740 OS << " (EXIT) ]\n";
2741 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002742 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002743 else
2744 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00002745
Ted Kremenek71eca012007-08-29 23:20:49 +00002746 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00002747 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002748
2749 if (print_edges)
2750 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002751
Mike Stump92244b02010-01-19 22:00:14 +00002752 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002753 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00002754 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00002755 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002756 C->getLHS()->printPretty(OS, Helper,
2757 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002758 if (C->getRHS()) {
2759 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002760 C->getRHS()->printPretty(OS, Helper,
2761 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002762 }
Mike Stump92244b02010-01-19 22:00:14 +00002763 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002764 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00002765 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002766 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00002767 if (CS->getExceptionDecl())
2768 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2769 0);
2770 else
2771 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002772 OS << ")";
2773
2774 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002775 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00002776
Ted Kremenek71eca012007-08-29 23:20:49 +00002777 OS << ":\n";
2778 }
Mike Stump31feda52009-07-17 01:31:16 +00002779
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002780 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002781 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00002782
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002783 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2784 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00002785
Ted Kremenek71eca012007-08-29 23:20:49 +00002786 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002787 if (print_edges)
2788 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002789
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002790 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002791
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002792 if (Helper)
2793 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002794
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002795 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002796 }
Mike Stump31feda52009-07-17 01:31:16 +00002797
Ted Kremenek71eca012007-08-29 23:20:49 +00002798 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002799 if (B.getTerminator()) {
2800 if (print_edges)
2801 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002802
Ted Kremenek71eca012007-08-29 23:20:49 +00002803 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002804
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002805 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002806
Chris Lattnerc61089a2009-06-30 01:26:17 +00002807 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2808 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002809 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00002810 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002811 }
Mike Stump31feda52009-07-17 01:31:16 +00002812
Ted Kremenek71eca012007-08-29 23:20:49 +00002813 if (print_edges) {
2814 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002815 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002816 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002817
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002818 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2819 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002820
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002821 if (i == 8 || (i-8) == 0)
2822 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002823
Ted Kremenek71eca012007-08-29 23:20:49 +00002824 OS << " B" << (*I)->getBlockID();
2825 }
Mike Stump31feda52009-07-17 01:31:16 +00002826
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002827 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002828
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002829 // Print the successors of this block.
2830 OS << " Successors (" << B.succ_size() << "):";
2831 i = 0;
2832
2833 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2834 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002835
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002836 if (i == 8 || (i-8) % 10 == 0)
2837 OS << "\n ";
2838
Mike Stump0d76d072009-07-20 23:24:15 +00002839 if (*I)
2840 OS << " B" << (*I)->getBlockID();
2841 else
2842 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002843 }
Mike Stump31feda52009-07-17 01:31:16 +00002844
Ted Kremenek71eca012007-08-29 23:20:49 +00002845 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002846 }
Mike Stump31feda52009-07-17 01:31:16 +00002847}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002848
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002849
2850/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002851void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002852
2853/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002854void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2855 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002856
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002857 // Print the entry block.
2858 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002859
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002860 // Iterate through the CFGBlocks and print them one by one.
2861 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2862 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002863 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002864 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002865
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002866 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002867 }
Mike Stump31feda52009-07-17 01:31:16 +00002868
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002869 // Print the exit block.
2870 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002871 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002872}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002873
2874/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002875void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2876 print(llvm::errs(), cfg, LO);
2877}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002878
2879/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2880/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002881void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2882 const LangOptions &LO) const {
2883 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002884 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002885}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002886
Ted Kremenek15647632008-01-30 23:02:42 +00002887/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002888void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002889 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002890 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002891 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2892}
2893
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002894Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002895
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002896 if (!Terminator)
2897 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002898
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002899 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002900
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002901 switch (Terminator->getStmtClass()) {
2902 default:
2903 break;
Mike Stump31feda52009-07-17 01:31:16 +00002904
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002905 case Stmt::ForStmtClass:
2906 E = cast<ForStmt>(Terminator)->getCond();
2907 break;
Mike Stump31feda52009-07-17 01:31:16 +00002908
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002909 case Stmt::WhileStmtClass:
2910 E = cast<WhileStmt>(Terminator)->getCond();
2911 break;
Mike Stump31feda52009-07-17 01:31:16 +00002912
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002913 case Stmt::DoStmtClass:
2914 E = cast<DoStmt>(Terminator)->getCond();
2915 break;
Mike Stump31feda52009-07-17 01:31:16 +00002916
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002917 case Stmt::IfStmtClass:
2918 E = cast<IfStmt>(Terminator)->getCond();
2919 break;
Mike Stump31feda52009-07-17 01:31:16 +00002920
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002921 case Stmt::ChooseExprClass:
2922 E = cast<ChooseExpr>(Terminator)->getCond();
2923 break;
Mike Stump31feda52009-07-17 01:31:16 +00002924
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002925 case Stmt::IndirectGotoStmtClass:
2926 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2927 break;
Mike Stump31feda52009-07-17 01:31:16 +00002928
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002929 case Stmt::SwitchStmtClass:
2930 E = cast<SwitchStmt>(Terminator)->getCond();
2931 break;
Mike Stump31feda52009-07-17 01:31:16 +00002932
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002933 case Stmt::ConditionalOperatorClass:
2934 E = cast<ConditionalOperator>(Terminator)->getCond();
2935 break;
Mike Stump31feda52009-07-17 01:31:16 +00002936
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002937 case Stmt::BinaryOperatorClass: // '&&' and '||'
2938 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002939 break;
Mike Stump31feda52009-07-17 01:31:16 +00002940
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002941 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002942 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002943 }
Mike Stump31feda52009-07-17 01:31:16 +00002944
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002945 return E ? E->IgnoreParens() : NULL;
2946}
2947
Ted Kremenek92137a32008-05-16 16:06:00 +00002948bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002949
Ted Kremenek92137a32008-05-16 16:06:00 +00002950 if (!Terminator)
2951 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002952
Ted Kremenek92137a32008-05-16 16:06:00 +00002953 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002954
Ted Kremenek92137a32008-05-16 16:06:00 +00002955 switch (Terminator->getStmtClass()) {
2956 default:
2957 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002958
2959 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002960 case Stmt::WhileStmtClass:
2961 case Stmt::DoStmtClass:
2962 case Stmt::IfStmtClass:
2963 case Stmt::ChooseExprClass:
2964 case Stmt::ConditionalOperatorClass:
2965 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002966 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002967 }
Mike Stump31feda52009-07-17 01:31:16 +00002968
Ted Kremenek92137a32008-05-16 16:06:00 +00002969 return E ? E->IgnoreParens() : NULL;
2970}
2971
Ted Kremenek15647632008-01-30 23:02:42 +00002972
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002973//===----------------------------------------------------------------------===//
2974// CFG Graphviz Visualization
2975//===----------------------------------------------------------------------===//
2976
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002977
2978#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002979static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002980#endif
2981
Chris Lattnerc61089a2009-06-30 01:26:17 +00002982void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002983#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002984 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002985 GraphHelper = &H;
2986 llvm::ViewGraph(this,"CFG");
2987 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002988#endif
2989}
2990
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002991namespace llvm {
2992template<>
2993struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002994
2995 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2996
2997 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002998
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002999#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003000 std::string OutSStr;
3001 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003002 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003003 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003004
3005 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3006
3007 // Process string output to make it nicer...
3008 for (unsigned i = 0; i != OutStr.length(); ++i)
3009 if (OutStr[i] == '\n') { // Left justify
3010 OutStr[i] = '\\';
3011 OutStr.insert(OutStr.begin()+i+1, 'l');
3012 }
Mike Stump31feda52009-07-17 01:31:16 +00003013
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003014 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003015#else
3016 return "";
3017#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003018 }
3019};
3020} // end namespace llvm