blob: bc3699ba68ab8eb90a38975db666168f74c1e370 [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 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000036}
Ted Kremenekdc03bd02010-08-02 23:46:59 +000037
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000038/// The CFG builder uses a recursive algorithm to build the CFG. When
39/// we process an expression, sometimes we know that we must add the
40/// subexpressions as block-level expressions. For example:
41///
42/// exp1 || exp2
43///
44/// When processing the '||' expression, we know that exp1 and exp2
45/// need to be added as block-level expressions, even though they
46/// might not normally need to be. AddStmtChoice records this
47/// contextual information. If AddStmtChoice is 'NotAlwaysAdd', then
48/// the builder has an option not to add a subexpression as a
49/// block-level expression.
50///
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000051class AddStmtChoice {
52public:
Ted Kremenek8219b822010-12-16 07:46:53 +000053 enum Kind { NotAlwaysAdd = 0, AlwaysAdd = 1 };
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000054
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000055 AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {}
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000056
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000057 bool alwaysAdd() const { return kind & AlwaysAdd; }
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000058
59 /// Return a copy of this object, except with the 'always-add' bit
60 /// set as specified.
61 AddStmtChoice withAlwaysAdd(bool alwaysAdd) const {
62 return AddStmtChoice(alwaysAdd ? Kind(kind | AlwaysAdd) :
63 Kind(kind & ~AlwaysAdd));
64 }
65
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000066private:
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000067 Kind kind;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000068};
Mike Stump31feda52009-07-17 01:31:16 +000069
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000070/// LocalScope - Node in tree of local scopes created for C++ implicit
71/// destructor calls generation. It contains list of automatic variables
72/// declared in the scope and link to position in previous scope this scope
73/// began in.
74///
75/// The process of creating local scopes is as follows:
76/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
77/// - Before processing statements in scope (e.g. CompoundStmt) create
78/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
79/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000080/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000081/// at this VarDecl,
82/// - For every normal (without jump) end of scope add to CFGBlock destructors
83/// for objects in the current scope,
84/// - For every jump add to CFGBlock destructors for objects
85/// between CFGBuilder::ScopePos and local scope position saved for jump
86/// target. Thanks to C++ restrictions on goto jumps we can be sure that
87/// jump target position will be on the path to root from CFGBuilder::ScopePos
88/// (adding any variable that doesn't need constructor to be called to
89/// LocalScope can break this assumption),
90///
91class LocalScope {
92public:
93 typedef llvm::SmallVector<VarDecl*, 4> AutomaticVarsTy;
94
95 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000096 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000097 class const_iterator {
98 const LocalScope* Scope;
99
100 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
101 /// Invalid iterator (with null Scope) has VarIter equal to 0.
102 unsigned VarIter;
103
104 public:
105 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
106 /// Incrementing invalid iterator is allowed and will result in invalid
107 /// iterator.
108 const_iterator()
109 : Scope(NULL), VarIter(0) {}
110
111 /// Create valid iterator. In case when S.Prev is an invalid iterator and
112 /// I is equal to 0, this will create invalid iterator.
113 const_iterator(const LocalScope& S, unsigned I)
114 : Scope(&S), VarIter(I) {
115 // Iterator to "end" of scope is not allowed. Handle it by going up
116 // in scopes tree possibly up to invalid iterator in the root.
117 if (VarIter == 0 && Scope)
118 *this = Scope->Prev;
119 }
120
121 VarDecl* const* operator->() const {
122 assert (Scope && "Dereferencing invalid iterator is not allowed");
123 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
124 return &Scope->Vars[VarIter - 1];
125 }
126 VarDecl* operator*() const {
127 return *this->operator->();
128 }
129
130 const_iterator& operator++() {
131 if (!Scope)
132 return *this;
133
134 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
135 --VarIter;
136 if (VarIter == 0)
137 *this = Scope->Prev;
138 return *this;
139 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000140 const_iterator operator++(int) {
141 const_iterator P = *this;
142 ++*this;
143 return P;
144 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000145
146 bool operator==(const const_iterator& rhs) const {
147 return Scope == rhs.Scope && VarIter == rhs.VarIter;
148 }
149 bool operator!=(const const_iterator& rhs) const {
150 return !(*this == rhs);
151 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000152
153 operator bool() const {
154 return *this != const_iterator();
155 }
156
157 int distance(const_iterator L);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000158 };
159
160 friend class const_iterator;
161
162private:
163 /// Automatic variables in order of declaration.
164 AutomaticVarsTy Vars;
165 /// Iterator to variable in previous scope that was declared just before
166 /// begin of this scope.
167 const_iterator Prev;
168
169public:
170 /// Constructs empty scope linked to previous scope in specified place.
171 LocalScope(const_iterator P)
172 : Vars()
173 , Prev(P) {}
174
175 /// Begin of scope in direction of CFG building (backwards).
176 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000177
178 void addVar(VarDecl* VD) {
179 Vars.push_back(VD);
180 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000181};
182
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000183/// distance - Calculates distance from this to L. L must be reachable from this
184/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
185/// number of scopes between this and L.
186int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
187 int D = 0;
188 const_iterator F = *this;
189 while (F.Scope != L.Scope) {
190 assert (F != const_iterator()
191 && "L iterator is not reachable from F iterator.");
192 D += F.VarIter;
193 F = F.Scope->Prev;
194 }
195 D += F.VarIter - L.VarIter;
196 return D;
197}
198
199/// BlockScopePosPair - Structure for specifying position in CFG during its
200/// build process. It consists of CFGBlock that specifies position in CFG graph
201/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000202struct BlockScopePosPair {
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000203 BlockScopePosPair() : block(0) {}
204 BlockScopePosPair(CFGBlock* b, LocalScope::const_iterator scopePos)
205 : block(b), scopePosition(scopePos) {}
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000206
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000207 CFGBlock *block;
208 LocalScope::const_iterator scopePosition;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000209};
210
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000211/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000212/// The builder is stateful: an instance of the builder should be used to only
213/// construct a single CFG.
214///
215/// Example usage:
216///
217/// CFGBuilder builder;
218/// CFG* cfg = builder.BuildAST(stmt1);
219///
Mike Stump31feda52009-07-17 01:31:16 +0000220/// CFG construction is done via a recursive walk of an AST. We actually parse
221/// the AST in reverse order so that the successor of a basic block is
222/// constructed prior to its predecessor. This allows us to nicely capture
223/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +0000224///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000225class CFGBuilder {
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000226 typedef BlockScopePosPair JumpTarget;
227 typedef BlockScopePosPair JumpSource;
228
Mike Stump0d76d072009-07-20 23:24:15 +0000229 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000230 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000231
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000232 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000233 CFGBlock* Succ;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000234 JumpTarget ContinueJumpTarget;
235 JumpTarget BreakJumpTarget;
Ted Kremenek879d8e12007-08-23 18:43:24 +0000236 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +0000237 CFGBlock* DefaultCaseBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000238 CFGBlock* TryTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000239
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000240 // Current position in local scope.
241 LocalScope::const_iterator ScopePos;
242
243 // LabelMap records the mapping from Label expressions to their jump targets.
244 typedef llvm::DenseMap<LabelStmt*, JumpTarget> LabelMapTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000245 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +0000246
247 // A list of blocks that end with a "goto" that must be backpatched to their
248 // resolved targets upon completion of CFG construction.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000249 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000250 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +0000251
Ted Kremenekeda180e22007-08-28 19:26:49 +0000252 // A list of labels whose address has been taken (for indirect gotos).
253 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
254 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +0000255
Zhongxing Xud38fb842010-09-16 03:28:18 +0000256 bool badCFG;
257 CFG::BuildOptions BuildOpts;
258
Mike Stump31feda52009-07-17 01:31:16 +0000259public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000260 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
261 Block(NULL), Succ(NULL),
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000262 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xud38fb842010-09-16 03:28:18 +0000263 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump31feda52009-07-17 01:31:16 +0000264
Ted Kremenek9aae5132007-08-23 21:42:29 +0000265 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000266 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000267 CFG::BuildOptions BO);
Mike Stump31feda52009-07-17 01:31:16 +0000268
Ted Kremenek93668002009-07-17 22:18:43 +0000269private:
270 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000271 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
272 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
273 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000274 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000275 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
John McCall5d413782010-12-06 08:20:24 +0000276 CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000277 AddStmtChoice asc);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000278 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
279 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000280 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
281 AddStmtChoice asc);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000282 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000283 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
284 AddStmtChoice asc);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000285 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
286 AddStmtChoice asc);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000287 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000288 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000289 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000290 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000291 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000292 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000293 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000294 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000295 CFGBlock *VisitDeclSubExpr(DeclStmt* DS);
Ted Kremenek21822592009-07-17 18:20:32 +0000296 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
297 CFGBlock *VisitDoStmt(DoStmt *D);
298 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000299 CFGBlock *VisitGotoStmt(GotoStmt* G);
300 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000301 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000302 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
303 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000304 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000305 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
306 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
307 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
308 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
309 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
310 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000311 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
312 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000313 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan6dace612010-11-22 08:45:56 +0000314 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000315 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000316
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000317 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
318 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000319 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000320
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000321 // Visitors to walk an AST and generate destructors of temporaries in
322 // full expression.
323 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
324 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
325 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
326 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
327 bool BindToTemporary);
328 CFGBlock *VisitConditionalOperatorForTemporaryDtors(ConditionalOperator *E,
329 bool BindToTemporary);
330
Ted Kremenek6065ef62008-04-28 18:00:46 +0000331 // NYS == Not Yet Supported
332 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000333 badCFG = true;
334 return Block;
335 }
Mike Stump31feda52009-07-17 01:31:16 +0000336
Ted Kremenek93668002009-07-17 22:18:43 +0000337 void autoCreateBlock() { if (!Block) Block = createBlock(); }
338 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000339
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000340 CFGBlock *addStmt(Stmt *S) {
341 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000342 }
Alexis Hunt1d792652011-01-08 20:30:50 +0000343 CFGBlock *addInitializer(CXXCtorInitializer *I);
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000344 void addAutomaticObjDtors(LocalScope::const_iterator B,
345 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski20b88732010-10-05 05:37:00 +0000346 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000347
Marcin Swiderski5e415732010-09-30 23:05:00 +0000348 // Local scopes creation.
349 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
350
Zhongxing Xu81714f22010-10-01 03:00:16 +0000351 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000352 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
353 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
354
355 void addLocalScopeAndDtors(Stmt* S);
356
357 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek8219b822010-12-16 07:46:53 +0000358 void appendStmt(CFGBlock *B, Stmt *S,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000359 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000360 B->appendStmt(S, cfg->getBumpVectorContext());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000361 }
Alexis Hunt1d792652011-01-08 20:30:50 +0000362 void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000363 B->appendInitializer(I, cfg->getBumpVectorContext());
364 }
Marcin Swiderski20b88732010-10-05 05:37:00 +0000365 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
366 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
367 }
368 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
369 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
370 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000371 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
372 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
373 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000374
Marcin Swiderski321a7072010-09-30 22:54:37 +0000375 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
376 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
377 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
378 LocalScope::const_iterator E, Stmt* S);
379 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
380 LocalScope::const_iterator B, LocalScope::const_iterator E);
381
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000382 void addSuccessor(CFGBlock *B, CFGBlock *S) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000383 B->addSuccessor(S, cfg->getBumpVectorContext());
384 }
Mike Stump11289f42009-09-09 15:08:12 +0000385
Ted Kremenek963cc312009-07-24 06:55:42 +0000386 /// TryResult - a class representing a variant over the values
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000387 /// 'true', 'false', or 'unknown'. This is returned by tryEvaluateBool,
Ted Kremenek963cc312009-07-24 06:55:42 +0000388 /// and is used by the CFGBuilder to decide if a branch condition
389 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000390 class TryResult {
391 int X;
392 public:
393 TryResult(bool b) : X(b ? 1 : 0) {}
394 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000395
Ted Kremenek30754282009-07-24 04:47:11 +0000396 bool isTrue() const { return X == 1; }
397 bool isFalse() const { return X == 0; }
398 bool isKnown() const { return X >= 0; }
399 void negate() {
400 assert(isKnown());
401 X ^= 0x1;
402 }
403 };
Mike Stump11289f42009-09-09 15:08:12 +0000404
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000405 /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
Mike Stump773582d2009-07-23 23:25:26 +0000406 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000407 TryResult tryEvaluateBool(Expr *S) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000408 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000409 return TryResult();
410
Mike Stump773582d2009-07-23 23:25:26 +0000411 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000412 if (!S->isTypeDependent() && !S->isValueDependent() &&
413 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000414 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000415
416 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000417 }
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000418};
Mike Stump31feda52009-07-17 01:31:16 +0000419
Douglas Gregor4619e432008-12-05 23:32:09 +0000420// FIXME: Add support for dependent-sized array types in C++?
421// Does it even make sense to build a CFG for an uninstantiated template?
John McCall424cec92011-01-19 06:33:43 +0000422static const VariableArrayType *FindVA(const Type *t) {
423 while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
424 if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000425 if (vat->getSizeExpr())
426 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000427
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000428 t = vt->getElementType().getTypePtr();
429 }
Mike Stump31feda52009-07-17 01:31:16 +0000430
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000431 return 0;
432}
Mike Stump31feda52009-07-17 01:31:16 +0000433
434/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
435/// arbitrary statement. Examples include a single expression or a function
436/// body (compound statement). The ownership of the returned CFG is
437/// transferred to the caller. If CFG construction fails, this method returns
438/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000439CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000440 CFG::BuildOptions BO) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000441
Mike Stump0d76d072009-07-20 23:24:15 +0000442 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000443 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000444 if (!Statement)
445 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000446
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000447 BuildOpts = BO;
Mike Stump31feda52009-07-17 01:31:16 +0000448
449 // Create an empty block that will serve as the exit block for the CFG. Since
450 // this is the first block added to the CFG, it will be implicitly registered
451 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000452 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000453 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000454 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000455
Marcin Swiderski20b88732010-10-05 05:37:00 +0000456 if (BuildOpts.AddImplicitDtors)
457 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
458 addImplicitDtorsForDestructor(DD);
459
Ted Kremenek9aae5132007-08-23 21:42:29 +0000460 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000461 CFGBlock *B = addStmt(Statement);
462
463 if (badCFG)
464 return NULL;
465
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000466 // For C++ constructor add initializers to CFG.
467 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
468 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
469 E = CD->init_rend(); I != E; ++I) {
470 B = addInitializer(*I);
471 if (badCFG)
472 return NULL;
473 }
474 }
475
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000476 if (B)
477 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000478
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000479 // Backpatch the gotos whose label -> block mappings we didn't know when we
480 // encountered them.
481 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
482 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000483
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000484 CFGBlock* B = I->block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000485 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
486 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000487
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000488 // If there is no target for the goto, then we are looking at an
489 // incomplete AST. Handle this by not registering a successor.
490 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000491
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000492 JumpTarget JT = LI->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000493 prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
494 JT.scopePosition);
495 addSuccessor(B, JT.block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000496 }
497
498 // Add successors to the Indirect Goto Dispatch block (if we have one).
499 if (CFGBlock* B = cfg->getIndirectGotoBlock())
500 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
501 E = AddressTakenLabels.end(); I != E; ++I ) {
502
503 // Lookup the target block.
504 LabelMapTy::iterator LI = LabelMap.find(*I);
505
506 // If there is no target block that contains label, then we are looking
507 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000508 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000509
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000510 addSuccessor(B, LI->second.block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000511 }
Mike Stump31feda52009-07-17 01:31:16 +0000512
Mike Stump31feda52009-07-17 01:31:16 +0000513 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000514 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000515
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000516 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000517}
Mike Stump31feda52009-07-17 01:31:16 +0000518
Ted Kremenek9aae5132007-08-23 21:42:29 +0000519/// createBlock - Used to lazily create blocks that are connected
520/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000521CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000522 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000523 if (add_successor && Succ)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000524 addSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000525 return B;
526}
Mike Stump31feda52009-07-17 01:31:16 +0000527
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000528/// addInitializer - Add C++ base or member initializer element to CFG.
Alexis Hunt1d792652011-01-08 20:30:50 +0000529CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000530 if (!BuildOpts.AddInitializers)
531 return Block;
532
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000533 bool IsReference = false;
534 bool HasTemporaries = false;
535
536 // Destructors of temporaries in initialization expression should be called
537 // after initialization finishes.
538 Expr *Init = I->getInit();
539 if (Init) {
Francois Pichetd583da02010-12-04 09:14:42 +0000540 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000541 IsReference = FD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +0000542 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000543
544 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
545 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +0000546 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000547 IsReference);
548 }
549 }
550
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000551 autoCreateBlock();
552 appendInitializer(Block, I);
553
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000554 if (Init) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000555 if (HasTemporaries) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000556 // For expression with temporaries go directly to subexpression to omit
557 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +0000558 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
559 }
560 return Visit(Init);
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000561 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000562
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000563 return Block;
564}
565
Marcin Swiderski5e415732010-09-30 23:05:00 +0000566/// addAutomaticObjDtors - Add to current block automatic objects destructors
567/// for objects in range of local scope positions. Use S as trigger statement
568/// for destructors.
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000569void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
570 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000571 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000572 return;
573
Marcin Swiderski5e415732010-09-30 23:05:00 +0000574 if (B == E)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000575 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000576
577 autoCreateBlock();
578 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000579}
580
Marcin Swiderski20b88732010-10-05 05:37:00 +0000581/// addImplicitDtorsForDestructor - Add implicit destructors generated for
582/// base and member objects in destructor.
583void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
584 assert (BuildOpts.AddImplicitDtors
585 && "Can be called only when dtors should be added");
586 const CXXRecordDecl *RD = DD->getParent();
587
588 // At the end destroy virtual base objects.
589 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
590 VE = RD->vbases_end(); VI != VE; ++VI) {
591 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
592 if (!CD->hasTrivialDestructor()) {
593 autoCreateBlock();
594 appendBaseDtor(Block, VI);
595 }
596 }
597
598 // Before virtual bases destroy direct base objects.
599 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
600 BE = RD->bases_end(); BI != BE; ++BI) {
601 if (!BI->isVirtual()) {
602 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
603 if (!CD->hasTrivialDestructor()) {
604 autoCreateBlock();
605 appendBaseDtor(Block, BI);
606 }
607 }
608 }
609
610 // First destroy member objects.
611 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
612 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski01769902010-10-25 07:05:54 +0000613 // Check for constant size array. Set type to array element type.
614 QualType QT = FI->getType();
615 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
616 if (AT->getSize() == 0)
617 continue;
618 QT = AT->getElementType();
619 }
620
621 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski20b88732010-10-05 05:37:00 +0000622 if (!CD->hasTrivialDestructor()) {
623 autoCreateBlock();
624 appendMemberDtor(Block, *FI);
625 }
626 }
627}
628
Marcin Swiderski5e415732010-09-30 23:05:00 +0000629/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
630/// way return valid LocalScope object.
631LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
632 if (!Scope) {
633 Scope = cfg->getAllocator().Allocate<LocalScope>();
634 new (Scope) LocalScope(ScopePos);
635 }
636 return Scope;
637}
638
639/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu81714f22010-10-01 03:00:16 +0000640/// that should create implicit scope (e.g. if/else substatements).
641void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000642 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu81714f22010-10-01 03:00:16 +0000643 return;
644
645 LocalScope *Scope = 0;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000646
647 // For compound statement we will be creating explicit scope.
648 if (CompoundStmt* CS = dyn_cast<CompoundStmt>(S)) {
649 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
650 ; BI != BE; ++BI) {
651 Stmt* SI = *BI;
652 if (LabelStmt* LS = dyn_cast<LabelStmt>(SI))
653 SI = LS->getSubStmt();
654 if (DeclStmt* DS = dyn_cast<DeclStmt>(SI))
655 Scope = addLocalScopeForDeclStmt(DS, Scope);
656 }
Zhongxing Xu81714f22010-10-01 03:00:16 +0000657 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000658 }
659
660 // For any other statement scope will be implicit and as such will be
661 // interesting only for DeclStmt.
662 if (LabelStmt* LS = dyn_cast<LabelStmt>(S))
663 S = LS->getSubStmt();
664 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Zhongxing Xu307701e2010-10-01 03:09:09 +0000665 addLocalScopeForDeclStmt(DS);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000666}
667
668/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
669/// reuse Scope if not NULL.
670LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000671 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000672 if (!BuildOpts.AddImplicitDtors)
673 return Scope;
674
675 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
676 ; DI != DE; ++DI) {
677 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
678 Scope = addLocalScopeForVarDecl(VD, Scope);
679 }
680 return Scope;
681}
682
683/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
684/// create add scope for automatic objects and temporary objects bound to
685/// const reference. Will reuse Scope if not NULL.
686LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000687 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000688 if (!BuildOpts.AddImplicitDtors)
689 return Scope;
690
691 // Check if variable is local.
692 switch (VD->getStorageClass()) {
693 case SC_None:
694 case SC_Auto:
695 case SC_Register:
696 break;
697 default: return Scope;
698 }
699
700 // Check for const references bound to temporary. Set type to pointee.
701 QualType QT = VD->getType();
702 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
703 QT = RT->getPointeeType();
704 if (!QT.isConstQualified())
705 return Scope;
706 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
707 return Scope;
708 }
709
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000710 // Check for constant size array. Set type to array element type.
711 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
712 if (AT->getSize() == 0)
713 return Scope;
714 QT = AT->getElementType();
715 }
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000716
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000717 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000718 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
719 if (!CD->hasTrivialDestructor()) {
720 // Add the variable to scope
721 Scope = createOrReuseLocalScope(Scope);
722 Scope->addVar(VD);
723 ScopePos = Scope->begin();
724 }
Marcin Swiderski5e415732010-09-30 23:05:00 +0000725 return Scope;
726}
727
728/// addLocalScopeAndDtors - For given statement add local scope for it and
729/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
730void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
731 if (!BuildOpts.AddImplicitDtors)
732 return;
733
734 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu81714f22010-10-01 03:00:16 +0000735 addLocalScopeForStmt(S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000736 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
737}
738
Marcin Swiderski321a7072010-09-30 22:54:37 +0000739/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
740/// automatic storage duration to CFGBlock's elements vector. Insertion will be
741/// performed in place specified with iterator.
742void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
743 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
744 BumpVectorContext& C = cfg->getBumpVectorContext();
745 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
746 while (B != E)
747 I = Blk->insertAutomaticObjDtor(I, *B++, S);
748}
749
750/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
751/// automatic storage duration to CFGBlock's elements vector. Elements will be
752/// appended to physical end of the vector which happens to be logical
753/// beginning.
754void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
755 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
756 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
757}
758
759/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
760/// variables with automatic storage duration to CFGBlock's elements vector.
761/// Elements will be prepended to physical beginning of the vector which
762/// happens to be logical end. Use blocks terminator as statement that specifies
763/// destructors call site.
764void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
765 LocalScope::const_iterator B, LocalScope::const_iterator E) {
766 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
767}
768
Ted Kremenek93668002009-07-17 22:18:43 +0000769/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000770/// blocks for ternary operators, &&, and ||. We also process "," and
771/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000772CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000773tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000774 if (!S) {
775 badCFG = true;
776 return 0;
777 }
Ted Kremenek93668002009-07-17 22:18:43 +0000778 switch (S->getStmtClass()) {
779 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000780 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000781
782 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000783 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000784
Ted Kremenek93668002009-07-17 22:18:43 +0000785 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000786 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000787
Ted Kremenek93668002009-07-17 22:18:43 +0000788 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000789 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000790
Ted Kremenek93668002009-07-17 22:18:43 +0000791 case Stmt::BreakStmtClass:
792 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000793
Ted Kremenek93668002009-07-17 22:18:43 +0000794 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000795 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000796 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000797
Ted Kremenek93668002009-07-17 22:18:43 +0000798 case Stmt::CaseStmtClass:
799 return VisitCaseStmt(cast<CaseStmt>(S));
800
801 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000802 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000803
Ted Kremenek93668002009-07-17 22:18:43 +0000804 case Stmt::CompoundStmtClass:
805 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000806
Ted Kremenek93668002009-07-17 22:18:43 +0000807 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000808 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000809
Ted Kremenek93668002009-07-17 22:18:43 +0000810 case Stmt::ContinueStmtClass:
811 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000812
Ted Kremenekb27378c2010-01-19 20:40:33 +0000813 case Stmt::CXXCatchStmtClass:
814 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
815
John McCall5d413782010-12-06 08:20:24 +0000816 case Stmt::ExprWithCleanupsClass:
817 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000818
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000819 case Stmt::CXXBindTemporaryExprClass:
820 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
821
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000822 case Stmt::CXXConstructExprClass:
823 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
824
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000825 case Stmt::CXXFunctionalCastExprClass:
826 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
827
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000828 case Stmt::CXXTemporaryObjectExprClass:
829 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
830
Zhongxing Xu7e612172010-04-13 09:38:01 +0000831 case Stmt::CXXMemberCallExprClass:
832 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
833
Ted Kremenekb27378c2010-01-19 20:40:33 +0000834 case Stmt::CXXThrowExprClass:
835 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000836
Ted Kremenekb27378c2010-01-19 20:40:33 +0000837 case Stmt::CXXTryStmtClass:
838 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000839
Ted Kremenek93668002009-07-17 22:18:43 +0000840 case Stmt::DeclStmtClass:
841 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000842
Ted Kremenek93668002009-07-17 22:18:43 +0000843 case Stmt::DefaultStmtClass:
844 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000845
Ted Kremenek93668002009-07-17 22:18:43 +0000846 case Stmt::DoStmtClass:
847 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000848
Ted Kremenek93668002009-07-17 22:18:43 +0000849 case Stmt::ForStmtClass:
850 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000851
Ted Kremenek93668002009-07-17 22:18:43 +0000852 case Stmt::GotoStmtClass:
853 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000854
Ted Kremenek93668002009-07-17 22:18:43 +0000855 case Stmt::IfStmtClass:
856 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000857
Ted Kremenek8219b822010-12-16 07:46:53 +0000858 case Stmt::ImplicitCastExprClass:
859 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000860
Ted Kremenek93668002009-07-17 22:18:43 +0000861 case Stmt::IndirectGotoStmtClass:
862 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000863
Ted Kremenek93668002009-07-17 22:18:43 +0000864 case Stmt::LabelStmtClass:
865 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000866
Ted Kremenek5868ec62010-04-11 17:02:10 +0000867 case Stmt::MemberExprClass:
868 return VisitMemberExpr(cast<MemberExpr>(S), asc);
869
Ted Kremenek93668002009-07-17 22:18:43 +0000870 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000871 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
872
Ted Kremenek93668002009-07-17 22:18:43 +0000873 case Stmt::ObjCAtSynchronizedStmtClass:
874 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000875
Ted Kremenek93668002009-07-17 22:18:43 +0000876 case Stmt::ObjCAtThrowStmtClass:
877 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000878
Ted Kremenek93668002009-07-17 22:18:43 +0000879 case Stmt::ObjCAtTryStmtClass:
880 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000881
Ted Kremenek93668002009-07-17 22:18:43 +0000882 case Stmt::ObjCForCollectionStmtClass:
883 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000884
Ted Kremenek93668002009-07-17 22:18:43 +0000885 case Stmt::ParenExprClass:
886 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000887 goto tryAgain;
888
Ted Kremenek93668002009-07-17 22:18:43 +0000889 case Stmt::NullStmtClass:
890 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000891
Ted Kremenek93668002009-07-17 22:18:43 +0000892 case Stmt::ReturnStmtClass:
893 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000894
Ted Kremenek93668002009-07-17 22:18:43 +0000895 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000896 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000897
Ted Kremenek93668002009-07-17 22:18:43 +0000898 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000899 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000900
Ted Kremenek93668002009-07-17 22:18:43 +0000901 case Stmt::SwitchStmtClass:
902 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000903
Zhanyong Wan6dace612010-11-22 08:45:56 +0000904 case Stmt::UnaryOperatorClass:
905 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
906
Ted Kremenek93668002009-07-17 22:18:43 +0000907 case Stmt::WhileStmtClass:
908 return VisitWhileStmt(cast<WhileStmt>(S));
909 }
910}
Mike Stump11289f42009-09-09 15:08:12 +0000911
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000912CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
913 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000914 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000915 appendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000916 }
Mike Stump11289f42009-09-09 15:08:12 +0000917
Ted Kremenek93668002009-07-17 22:18:43 +0000918 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000919}
Mike Stump31feda52009-07-17 01:31:16 +0000920
Ted Kremenek93668002009-07-17 22:18:43 +0000921/// VisitChildren - Visit the children of a Stmt.
922CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
923 CFGBlock *B = Block;
John McCall8322c3a2011-02-13 04:07:26 +0000924 for (Stmt::child_range I = Terminator->children(); I; ++I) {
Ted Kremenek93668002009-07-17 22:18:43 +0000925 if (*I) B = Visit(*I);
926 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000927 return B;
928}
Mike Stump11289f42009-09-09 15:08:12 +0000929
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000930CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
931 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000932 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000933
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000934 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000935 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000936 appendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000937 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000938
Ted Kremenek9aae5132007-08-23 21:42:29 +0000939 return Block;
940}
Mike Stump11289f42009-09-09 15:08:12 +0000941
Zhanyong Wan6dace612010-11-22 08:45:56 +0000942CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek8219b822010-12-16 07:46:53 +0000943 AddStmtChoice asc) {
Zhanyong Wan6dace612010-11-22 08:45:56 +0000944 if (asc.alwaysAdd()) {
945 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000946 appendStmt(Block, U, asc);
Zhanyong Wan6dace612010-11-22 08:45:56 +0000947 }
948
Ted Kremenek8219b822010-12-16 07:46:53 +0000949 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan6dace612010-11-22 08:45:56 +0000950}
951
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000952CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
953 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000954 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000955 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000956 appendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000957
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000958 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000959 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000960
Ted Kremenek93668002009-07-17 22:18:43 +0000961 // create the block evaluating the LHS
962 CFGBlock* LHSBlock = createBlock(false);
963 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000964
Ted Kremenek93668002009-07-17 22:18:43 +0000965 // create the block evaluating the RHS
966 Succ = ConfluenceBlock;
967 Block = NULL;
968 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000969
970 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000971 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000972 return 0;
Zhanyong Wan59f09c72010-11-22 19:32:14 +0000973 } else {
Ted Kremenek989da5e2010-04-29 01:10:26 +0000974 // Create an empty block for cases where the RHS doesn't require
975 // any explicit statements in the CFG.
976 RHSBlock = createBlock();
977 }
Mike Stump11289f42009-09-09 15:08:12 +0000978
Mike Stump773582d2009-07-23 23:25:26 +0000979 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000980 TryResult KnownVal = tryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000981 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000982 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000983
Ted Kremenek93668002009-07-17 22:18:43 +0000984 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000985 if (B->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000986 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
987 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000988 } else {
John McCalle3027922010-08-25 11:45:40 +0000989 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000990 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
991 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000992 }
Mike Stump11289f42009-09-09 15:08:12 +0000993
Ted Kremenek93668002009-07-17 22:18:43 +0000994 // Generate the blocks for evaluating the LHS.
995 Block = LHSBlock;
996 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000997 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +0000998
999 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +00001000 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001001 appendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +00001002 addStmt(B->getRHS());
1003 return addStmt(B->getLHS());
1004 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001005
1006 if (B->isAssignmentOp()) {
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001007 if (asc.alwaysAdd()) {
1008 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001009 appendStmt(Block, B, asc);
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001010 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001011 Visit(B->getLHS());
Marcin Swiderski77232492010-10-24 08:21:40 +00001012 return Visit(B->getRHS());
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001013 }
Mike Stump11289f42009-09-09 15:08:12 +00001014
Marcin Swiderski77232492010-10-24 08:21:40 +00001015 if (asc.alwaysAdd()) {
1016 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001017 appendStmt(Block, B, asc);
Marcin Swiderski77232492010-10-24 08:21:40 +00001018 }
1019
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001020 CFGBlock *RBlock = Visit(B->getRHS());
1021 CFGBlock *LBlock = Visit(B->getLHS());
1022 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1023 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1024 // return RBlock. Otherwise we'll incorrectly return NULL.
1025 return (LBlock ? LBlock : RBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001026}
1027
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001028CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1029 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +00001030 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001031 appendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +00001032 }
1033 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001034}
1035
Ted Kremenek93668002009-07-17 22:18:43 +00001036CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1037 // "break" is a control-flow statement. Thus we stop processing the current
1038 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001039 if (badCFG)
1040 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001041
Ted Kremenek93668002009-07-17 22:18:43 +00001042 // Now create a new block that ends with the break statement.
1043 Block = createBlock(false);
1044 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +00001045
Ted Kremenek93668002009-07-17 22:18:43 +00001046 // If there is no target for the break, then we are looking at an incomplete
1047 // AST. This means that the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001048 if (BreakJumpTarget.block) {
1049 addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B);
1050 addSuccessor(Block, BreakJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001051 } else
Ted Kremenek93668002009-07-17 22:18:43 +00001052 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +00001053
1054
Ted Kremenek9aae5132007-08-23 21:42:29 +00001055 return Block;
1056}
Mike Stump11289f42009-09-09 15:08:12 +00001057
Mike Stump04c68512010-01-21 15:20:48 +00001058static bool CanThrow(Expr *E) {
1059 QualType Ty = E->getType();
1060 if (Ty->isFunctionPointerType())
1061 Ty = Ty->getAs<PointerType>()->getPointeeType();
1062 else if (Ty->isBlockPointerType())
1063 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001064
Mike Stump04c68512010-01-21 15:20:48 +00001065 const FunctionType *FT = Ty->getAs<FunctionType>();
1066 if (FT) {
1067 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1068 if (Proto->hasEmptyExceptionSpec())
1069 return false;
1070 }
1071 return true;
1072}
1073
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001074CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +00001075 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +00001076 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001077 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001078 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +00001079 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001080
Mike Stump04c68512010-01-21 15:20:48 +00001081 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001082
1083 // Languages without exceptions are assumed to not throw.
1084 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001085 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +00001086 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +00001087 }
1088
1089 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001090 if (FD->hasAttr<NoReturnAttr>())
1091 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +00001092 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +00001093 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001094 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001095
Mike Stump04c68512010-01-21 15:20:48 +00001096 if (!CanThrow(C->getCallee()))
1097 AddEHEdge = false;
1098
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001099 if (!NoReturn && !AddEHEdge)
1100 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump11289f42009-09-09 15:08:12 +00001101
Mike Stump92244b02010-01-19 22:00:14 +00001102 if (Block) {
1103 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001104 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +00001105 return 0;
1106 }
Mike Stump11289f42009-09-09 15:08:12 +00001107
Mike Stump92244b02010-01-19 22:00:14 +00001108 Block = createBlock(!NoReturn);
Ted Kremenek8219b822010-12-16 07:46:53 +00001109 appendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +00001110
Mike Stump92244b02010-01-19 22:00:14 +00001111 if (NoReturn) {
1112 // Wire this to the exit block directly.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001113 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001114 }
Mike Stump04c68512010-01-21 15:20:48 +00001115 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +00001116 // Add exceptional edges.
1117 if (TryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001118 addSuccessor(Block, TryTerminatedBlock);
Mike Stump92244b02010-01-19 22:00:14 +00001119 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001120 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001121 }
Mike Stump11289f42009-09-09 15:08:12 +00001122
Mike Stump8c5d7992009-07-25 21:26:53 +00001123 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +00001124}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001125
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001126CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1127 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +00001128 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001129 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001130 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001131 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001132
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001133 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek21822592009-07-17 18:20:32 +00001134 Succ = ConfluenceBlock;
1135 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001136 CFGBlock* LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001137 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001138 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001139
Ted Kremenek21822592009-07-17 18:20:32 +00001140 Succ = ConfluenceBlock;
1141 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001142 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001143 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001144 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001145
Ted Kremenek21822592009-07-17 18:20:32 +00001146 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +00001147 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001148 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
1149 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1150 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +00001151 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +00001152 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +00001153}
Mike Stump11289f42009-09-09 15:08:12 +00001154
1155
1156CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001157 addLocalScopeAndDtors(C);
Mike Stump11289f42009-09-09 15:08:12 +00001158 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001159
1160 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1161 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +00001162 // If we hit a segment of code just containing ';' (NullStmts), we can
1163 // get a null block back. In such cases, just use the LastBlock
1164 if (CFGBlock *newBlock = addStmt(*I))
1165 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001166
Ted Kremenekce499c22009-08-27 23:16:26 +00001167 if (badCFG)
1168 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001169 }
Mike Stump92244b02010-01-19 22:00:14 +00001170
Ted Kremenek93668002009-07-17 22:18:43 +00001171 return LastBlock;
1172}
Mike Stump11289f42009-09-09 15:08:12 +00001173
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001174CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1175 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +00001176 // Create the confluence block that will "merge" the results of the ternary
1177 // expression.
1178 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001179 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001180 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001181 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001182
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001183 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001184
Ted Kremenek51d40b02009-07-17 18:15:54 +00001185 // Create a block for the LHS expression if there is an LHS expression. A
1186 // GCC extension allows LHS to be NULL, causing the condition to be the
1187 // value that is returned instead.
1188 // e.g: x ?: y is shorthand for: x ? x : y;
1189 Succ = ConfluenceBlock;
1190 Block = NULL;
1191 CFGBlock* LHSBlock = NULL;
1192 if (C->getLHS()) {
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001193 LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001194 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001195 return 0;
1196 Block = NULL;
1197 }
Mike Stump11289f42009-09-09 15:08:12 +00001198
Ted Kremenek51d40b02009-07-17 18:15:54 +00001199 // Create the block for the RHS expression.
1200 Succ = ConfluenceBlock;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001201 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001202 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001203 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001204
Ted Kremenek51d40b02009-07-17 18:15:54 +00001205 // Create the block that will contain the condition.
1206 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +00001207
Mike Stump773582d2009-07-23 23:25:26 +00001208 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001209 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +00001210 if (LHSBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001211 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +00001212 } else {
Ted Kremenek30754282009-07-24 04:47:11 +00001213 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +00001214 // If we know the condition is false, add NULL as the successor for
1215 // the block containing the condition. In this case, the confluence
1216 // block will have just one predecessor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001217 addSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +00001218 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +00001219 } else {
1220 // If we have no LHS expression, add the ConfluenceBlock as a direct
1221 // successor for the block containing the condition. Moreover, we need to
1222 // reverse the order of the predecessors in the ConfluenceBlock because
1223 // the RHSBlock will have been added to the succcessors already, and we
1224 // want the first predecessor to the the block containing the expression
1225 // for the case when the ternary expression evaluates to true.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001226 addSuccessor(Block, ConfluenceBlock);
Ted Kremenek18fb1662010-11-15 22:59:22 +00001227 // Note that there can possibly only be one predecessor if one of the
1228 // subexpressions resulted in calling a noreturn function.
Mike Stump0d76d072009-07-20 23:24:15 +00001229 std::reverse(ConfluenceBlock->pred_begin(),
1230 ConfluenceBlock->pred_end());
1231 }
Ted Kremenek51d40b02009-07-17 18:15:54 +00001232 }
Mike Stump11289f42009-09-09 15:08:12 +00001233
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001234 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001235 Block->setTerminator(C);
1236 return addStmt(C->getCond());
1237}
1238
Ted Kremenek93668002009-07-17 22:18:43 +00001239CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001240 if (DS->isSingleDecl())
1241 return VisitDeclSubExpr(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001242
Ted Kremenek93668002009-07-17 22:18:43 +00001243 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001244
Ted Kremenek93668002009-07-17 22:18:43 +00001245 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1246 typedef llvm::SmallVector<Decl*,10> BufTy;
1247 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +00001248
Ted Kremenek93668002009-07-17 22:18:43 +00001249 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1250 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1251 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1252 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +00001253
Ted Kremenek93668002009-07-17 22:18:43 +00001254 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1255 // automatically freed with the CFG.
1256 DeclGroupRef DG(*I);
1257 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001258 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +00001259 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +00001260
Ted Kremenek93668002009-07-17 22:18:43 +00001261 // Append the fake DeclStmt to block.
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001262 B = VisitDeclSubExpr(DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +00001263 }
Mike Stump11289f42009-09-09 15:08:12 +00001264
1265 return B;
Ted Kremenek93668002009-07-17 22:18:43 +00001266}
Mike Stump11289f42009-09-09 15:08:12 +00001267
Ted Kremenek93668002009-07-17 22:18:43 +00001268/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001269/// DeclStmts and initializers in them.
1270CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1271 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekf6998822008-02-26 00:22:58 +00001272
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001273 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001274
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001275 if (!VD) {
1276 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001277 appendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +00001278 return Block;
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001279 }
Mike Stump11289f42009-09-09 15:08:12 +00001280
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001281 bool IsReference = false;
1282 bool HasTemporaries = false;
1283
1284 // Destructors of temporaries in initialization expression should be called
1285 // after initialization finishes.
Ted Kremenek93668002009-07-17 22:18:43 +00001286 Expr *Init = VD->getInit();
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001287 if (Init) {
1288 IsReference = VD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +00001289 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001290
1291 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1292 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +00001293 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001294 IsReference);
1295 }
1296 }
1297
1298 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001299 appendStmt(Block, DS);
Mike Stump11289f42009-09-09 15:08:12 +00001300
Ted Kremenek93668002009-07-17 22:18:43 +00001301 if (Init) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001302 if (HasTemporaries)
1303 // For expression with temporaries go directly to subexpression to omit
1304 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +00001305 Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001306 else
Ted Kremenek8219b822010-12-16 07:46:53 +00001307 Visit(Init);
Ted Kremenek93668002009-07-17 22:18:43 +00001308 }
Mike Stump11289f42009-09-09 15:08:12 +00001309
Ted Kremenek93668002009-07-17 22:18:43 +00001310 // If the type of VD is a VLA, then we must process its size expressions.
John McCall424cec92011-01-19 06:33:43 +00001311 for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr());
1312 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek93668002009-07-17 22:18:43 +00001313 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001314
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001315 // Remove variable from local scope.
1316 if (ScopePos && VD == *ScopePos)
1317 ++ScopePos;
1318
Ted Kremenek93668002009-07-17 22:18:43 +00001319 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001320}
1321
1322CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001323 // We may see an if statement in the middle of a basic block, or it may be the
1324 // first statement we are processing. In either case, we create a new basic
1325 // block. First, we create the blocks for the then...else statements, and
1326 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +00001327 // middle of a block, we stop processing that block. That block is then the
1328 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +00001329
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001330 // Save local scope position because in case of condition variable ScopePos
1331 // won't be restored when traversing AST.
1332 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1333
1334 // Create local scope for possible condition variable.
1335 // Store scope position. Add implicit destructor.
1336 if (VarDecl* VD = I->getConditionVariable()) {
1337 LocalScope::const_iterator BeginScopePos = ScopePos;
1338 addLocalScopeForVarDecl(VD);
1339 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1340 }
1341
Mike Stump31feda52009-07-17 01:31:16 +00001342 // The block we were proccessing is now finished. Make it the successor
1343 // block.
1344 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001345 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001346 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001347 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001348 }
Mike Stump31feda52009-07-17 01:31:16 +00001349
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001350 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001351 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001352
Ted Kremenek9aae5132007-08-23 21:42:29 +00001353 if (Stmt* Else = I->getElse()) {
1354 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001355
Ted Kremenek9aae5132007-08-23 21:42:29 +00001356 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +00001357 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001358 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001359
1360 // If branch is not a compound statement create implicit scope
1361 // and add destructors.
1362 if (!isa<CompoundStmt>(Else))
1363 addLocalScopeAndDtors(Else);
1364
Ted Kremenek93668002009-07-17 22:18:43 +00001365 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +00001366
Ted Kremenekbbad8ce2007-08-30 18:13:31 +00001367 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1368 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +00001369 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001370 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001371 return 0;
1372 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001373 }
Mike Stump31feda52009-07-17 01:31:16 +00001374
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001375 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001376 CFGBlock* ThenBlock;
1377 {
1378 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001379 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001380 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001381 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001382
1383 // If branch is not a compound statement create implicit scope
1384 // and add destructors.
1385 if (!isa<CompoundStmt>(Then))
1386 addLocalScopeAndDtors(Then);
1387
Ted Kremenek93668002009-07-17 22:18:43 +00001388 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +00001389
Ted Kremenek1b379512009-04-01 03:52:47 +00001390 if (!ThenBlock) {
1391 // We can reach here if the "then" body has all NullStmts.
1392 // Create an empty block so we can distinguish between true and false
1393 // branches in path-sensitive analyses.
1394 ThenBlock = createBlock(false);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001395 addSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001396 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001397 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001398 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001399 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001400 }
1401
Mike Stump31feda52009-07-17 01:31:16 +00001402 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001403 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001404
Ted Kremenek9aae5132007-08-23 21:42:29 +00001405 // Set the terminator of the new block to the If statement.
1406 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001407
Mike Stump773582d2009-07-23 23:25:26 +00001408 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001409 const TryResult &KnownVal = tryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001410
Ted Kremenek9aae5132007-08-23 21:42:29 +00001411 // Now add the successors.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001412 addSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1413 addSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001414
1415 // Add the condition as the last statement in the new block. This may create
1416 // new blocks as the condition may contain control-flow. Any newly created
1417 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001418 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001419
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001420 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1421 // and the condition variable initialization to the CFG.
1422 if (VarDecl *VD = I->getConditionVariable()) {
1423 if (Expr *Init = VD->getInit()) {
1424 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001425 appendStmt(Block, I, AddStmtChoice::AlwaysAdd);
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001426 addStmt(Init);
1427 }
1428 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001429
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001430 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001431}
Mike Stump31feda52009-07-17 01:31:16 +00001432
1433
Ted Kremenek9aae5132007-08-23 21:42:29 +00001434CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001435 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001436 //
Mike Stump31feda52009-07-17 01:31:16 +00001437 // NOTE: If a "return" appears in the middle of a block, this means that the
1438 // code afterwards is DEAD (unreachable). We still keep a basic block
1439 // for that code; a simple "mark-and-sweep" from the entry block will be
1440 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001441
1442 // Create the new block.
1443 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001444
Ted Kremenek9aae5132007-08-23 21:42:29 +00001445 // The Exit block is the only successor.
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001446 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001447 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001448
1449 // Add the return statement to the block. This may create new blocks if R
1450 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001451 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001452}
1453
1454CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1455 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001456 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001457 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001458
Ted Kremenek93668002009-07-17 22:18:43 +00001459 if (!LabelBlock) // This can happen when the body is empty, i.e.
1460 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001461
Ted Kremenek93668002009-07-17 22:18:43 +00001462 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001463 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001464
1465 // Labels partition blocks, so this is the end of the basic block we were
1466 // processing (L is the block's label). Because this is label (and we have
1467 // already processed the substatement) there is no extra control-flow to worry
1468 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001469 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001470 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001471 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001472
1473 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001474 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001475
Ted Kremenek9aae5132007-08-23 21:42:29 +00001476 // This block is now the implicit successor of other blocks.
1477 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001478
Ted Kremenek9aae5132007-08-23 21:42:29 +00001479 return LabelBlock;
1480}
1481
1482CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001483 // Goto is a control-flow statement. Thus we stop processing the current
1484 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001485
Ted Kremenek9aae5132007-08-23 21:42:29 +00001486 Block = createBlock(false);
1487 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001488
1489 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001490 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001491
Ted Kremenek9aae5132007-08-23 21:42:29 +00001492 if (I == LabelMap.end())
1493 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001494 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1495 else {
1496 JumpTarget JT = I->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001497 addAutomaticObjDtors(ScopePos, JT.scopePosition, G);
1498 addSuccessor(Block, JT.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001499 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001500
Mike Stump31feda52009-07-17 01:31:16 +00001501 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001502}
1503
1504CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001505 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001506
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001507 // Save local scope position because in case of condition variable ScopePos
1508 // won't be restored when traversing AST.
1509 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1510
1511 // Create local scope for init statement and possible condition variable.
1512 // Add destructor for init statement and condition variable.
1513 // Store scope position for continue statement.
1514 if (Stmt* Init = F->getInit())
1515 addLocalScopeForStmt(Init);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001516 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1517
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001518 if (VarDecl* VD = F->getConditionVariable())
1519 addLocalScopeForVarDecl(VD);
1520 LocalScope::const_iterator ContinueScopePos = ScopePos;
1521
1522 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1523
Mike Stump014b3ea2009-07-21 01:12:51 +00001524 // "for" is a control-flow statement. Thus we stop processing the current
1525 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001526 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001527 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001528 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001529 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001530 } else
1531 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001532
Ted Kremenek304a9532010-05-21 20:30:15 +00001533 // Save the current value for the break targets.
1534 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001535 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001536 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001537
Mike Stump31feda52009-07-17 01:31:16 +00001538 // Because of short-circuit evaluation, the condition of the loop can span
1539 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1540 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001541 CFGBlock* ExitConditionBlock = createBlock(false);
1542 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001543
Ted Kremenek81e14852007-08-27 19:46:09 +00001544 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001545 ExitConditionBlock->setTerminator(F);
1546
1547 // Now add the actual condition to the condition block. Because the condition
1548 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001549 if (Stmt* C = F->getCond()) {
1550 Block = ExitConditionBlock;
1551 EntryConditionBlock = addStmt(C);
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001552 if (badCFG)
1553 return 0;
Ted Kremenek7b31a612010-09-15 07:01:20 +00001554 assert(Block == EntryConditionBlock ||
1555 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001556
1557 // If this block contains a condition variable, add both the condition
1558 // variable and initializer to the CFG.
1559 if (VarDecl *VD = F->getConditionVariable()) {
1560 if (Expr *Init = VD->getInit()) {
1561 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001562 appendStmt(Block, F, AddStmtChoice::AlwaysAdd);
Ted Kremenekec92f942009-12-24 01:49:06 +00001563 EntryConditionBlock = addStmt(Init);
1564 assert(Block == EntryConditionBlock);
1565 }
1566 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001567
Ted Kremenek55957a82009-05-02 00:13:27 +00001568 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001569 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001570 return 0;
1571 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001572 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001573
Mike Stump31feda52009-07-17 01:31:16 +00001574 // The condition block is the implicit successor for the loop body as well as
1575 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001576 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001577
Mike Stump773582d2009-07-23 23:25:26 +00001578 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001579 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001580
Mike Stump773582d2009-07-23 23:25:26 +00001581 if (F->getCond())
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001582 KnownVal = tryEvaluateBool(F->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001583
Ted Kremenek9aae5132007-08-23 21:42:29 +00001584 // Now create the loop body.
1585 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001586 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001587
Ted Kremenek304a9532010-05-21 20:30:15 +00001588 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001589 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1590 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001591
Ted Kremeneke9610502007-08-30 18:39:40 +00001592 // Create a new block to contain the (bottom) of the loop body.
1593 Block = NULL;
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001594
1595 // Loop body should end with destructor of Condition variable (if any).
1596 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump31feda52009-07-17 01:31:16 +00001597
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001598 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001599 // Generate increment code in its own basic block. This is the target of
1600 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001601 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001602 } else {
1603 // No increment code. Create a special, empty, block that is used as the
1604 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001605 assert(Succ == EntryConditionBlock);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001606 Succ = Block ? Block : createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001607 }
Mike Stump31feda52009-07-17 01:31:16 +00001608
Ted Kremenek902393b2009-04-28 00:51:56 +00001609 // Finish up the increment (or empty) block if it hasn't been already.
1610 if (Block) {
1611 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001612 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001613 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001614 Block = 0;
1615 }
Mike Stump31feda52009-07-17 01:31:16 +00001616
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001617 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001618
Ted Kremenek902393b2009-04-28 00:51:56 +00001619 // The starting block for the loop increment is the block that should
1620 // represent the 'loop target' for looping back to the start of the loop.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001621 ContinueJumpTarget.block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001622
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001623 // If body is not a compound statement create implicit scope
1624 // and add destructors.
1625 if (!isa<CompoundStmt>(F->getBody()))
1626 addLocalScopeAndDtors(F->getBody());
1627
Mike Stump31feda52009-07-17 01:31:16 +00001628 // Now populate the body block, and in the process create new blocks as we
1629 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001630 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001631
1632 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001633 BodyBlock = ContinueJumpTarget.block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001634 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001635 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001636
Ted Kremenek30754282009-07-24 04:47:11 +00001637 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001638 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001639 }
Mike Stump31feda52009-07-17 01:31:16 +00001640
Ted Kremenek30754282009-07-24 04:47:11 +00001641 // Link up the condition block with the code that follows the loop. (the
1642 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001643 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001644
Ted Kremenek9aae5132007-08-23 21:42:29 +00001645 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001646 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001647 if (Stmt* I = F->getInit()) {
1648 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001649 return addStmt(I);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001650 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001651
1652 // There is no loop initialization. We are thus basically a while loop.
1653 // NULL out Block to force lazy block construction.
1654 Block = NULL;
1655 Succ = EntryConditionBlock;
1656 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001657}
1658
Ted Kremenek5868ec62010-04-11 17:02:10 +00001659CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1660 if (asc.alwaysAdd()) {
1661 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001662 appendStmt(Block, M, asc);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001663 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001664 return Visit(M->getBase());
Ted Kremenek5868ec62010-04-11 17:02:10 +00001665}
1666
Ted Kremenek9d56e642008-11-11 17:10:00 +00001667CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1668 // Objective-C fast enumeration 'for' statements:
1669 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1670 //
1671 // for ( Type newVariable in collection_expression ) { statements }
1672 //
1673 // becomes:
1674 //
1675 // prologue:
1676 // 1. collection_expression
1677 // T. jump to loop_entry
1678 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001679 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001680 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1681 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1682 // TB:
1683 // statements
1684 // T. jump to loop_entry
1685 // FB:
1686 // what comes after
1687 //
1688 // and
1689 //
1690 // Type existingItem;
1691 // for ( existingItem in expression ) { statements }
1692 //
1693 // becomes:
1694 //
Mike Stump31feda52009-07-17 01:31:16 +00001695 // the same with newVariable replaced with existingItem; the binding works
1696 // the same except that for one ObjCForCollectionStmt::getElement() returns
1697 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001698 //
Mike Stump31feda52009-07-17 01:31:16 +00001699
Ted Kremenek9d56e642008-11-11 17:10:00 +00001700 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001701
Ted Kremenek9d56e642008-11-11 17:10:00 +00001702 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001703 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001704 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001705 LoopSuccessor = Block;
1706 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001707 } else
1708 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001709
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001710 // Build the condition blocks.
1711 CFGBlock* ExitConditionBlock = createBlock(false);
1712 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001713
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001714 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001715 ExitConditionBlock->setTerminator(S);
1716
1717 // The last statement in the block should be the ObjCForCollectionStmt, which
1718 // performs the actual binding to 'element' and determines if there are any
1719 // more items in the collection.
Ted Kremenek8219b822010-12-16 07:46:53 +00001720 appendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001721 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001722
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001723 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001724 // generate new blocks as necesary. We DON'T add the statement by default to
1725 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001726 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001727 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001728 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001729 return 0;
1730 Block = 0;
1731 }
Mike Stump31feda52009-07-17 01:31:16 +00001732
1733 // The condition block is the implicit successor for the loop body as well as
1734 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001735 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001736
Ted Kremenek9d56e642008-11-11 17:10:00 +00001737 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001738 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001739 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001740 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1741 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1742 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001743
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001744 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1745 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001746
Ted Kremenek93668002009-07-17 22:18:43 +00001747 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001748
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001749 if (!BodyBlock)
1750 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001751 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001752 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001753 return 0;
1754 }
Mike Stump31feda52009-07-17 01:31:16 +00001755
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001756 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001757 addSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001758 }
Mike Stump31feda52009-07-17 01:31:16 +00001759
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001760 // Link up the condition block with the code that follows the loop.
1761 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001762 addSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001763
Ted Kremenek9d56e642008-11-11 17:10:00 +00001764 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001765 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001766 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001767}
1768
Ted Kremenek49805452009-05-02 01:49:13 +00001769CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1770 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001771
Ted Kremenek49805452009-05-02 01:49:13 +00001772 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001773 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001774
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001775 // The sync body starts its own basic block. This makes it a little easier
1776 // for diagnostic clients.
1777 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001778 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001779 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001780
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001781 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001782 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001783 }
Mike Stump31feda52009-07-17 01:31:16 +00001784
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001785 // Add the @synchronized to the CFG.
1786 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001787 appendStmt(Block, S, AddStmtChoice::AlwaysAdd);
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001788
Ted Kremenek49805452009-05-02 01:49:13 +00001789 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001790 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001791}
Mike Stump31feda52009-07-17 01:31:16 +00001792
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001793CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001794 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001795 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001796}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001797
Ted Kremenek9aae5132007-08-23 21:42:29 +00001798CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001799 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001800
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001801 // Save local scope position because in case of condition variable ScopePos
1802 // won't be restored when traversing AST.
1803 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1804
1805 // Create local scope for possible condition variable.
1806 // Store scope position for continue statement.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001807 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001808 if (VarDecl* VD = W->getConditionVariable()) {
1809 addLocalScopeForVarDecl(VD);
1810 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1811 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001812
Mike Stump014b3ea2009-07-21 01:12:51 +00001813 // "while" is a control-flow statement. Thus we stop processing the current
1814 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001815 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001816 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001817 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001818 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001819 } else
1820 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001821
1822 // Because of short-circuit evaluation, the condition of the loop can span
1823 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1824 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001825 CFGBlock* ExitConditionBlock = createBlock(false);
1826 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001827
Ted Kremenek81e14852007-08-27 19:46:09 +00001828 // Set the terminator for the "exit" condition block.
1829 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001830
1831 // Now add the actual condition to the condition block. Because the condition
1832 // itself may contain control-flow, new blocks may be created. Thus we update
1833 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001834 if (Stmt* C = W->getCond()) {
1835 Block = ExitConditionBlock;
1836 EntryConditionBlock = addStmt(C);
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001837 // The condition might finish the current 'Block'.
1838 Block = EntryConditionBlock;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001839
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001840 // If this block contains a condition variable, add both the condition
1841 // variable and initializer to the CFG.
1842 if (VarDecl *VD = W->getConditionVariable()) {
1843 if (Expr *Init = VD->getInit()) {
1844 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001845 appendStmt(Block, W, AddStmtChoice::AlwaysAdd);
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001846 EntryConditionBlock = addStmt(Init);
1847 assert(Block == EntryConditionBlock);
1848 }
1849 }
1850
Ted Kremenek55957a82009-05-02 00:13:27 +00001851 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001852 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001853 return 0;
1854 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001855 }
Mike Stump31feda52009-07-17 01:31:16 +00001856
1857 // The condition block is the implicit successor for the loop body as well as
1858 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001859 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001860
Mike Stump773582d2009-07-23 23:25:26 +00001861 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001862 const TryResult& KnownVal = tryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001863
Ted Kremenek9aae5132007-08-23 21:42:29 +00001864 // Process the loop body.
1865 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001866 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001867
1868 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001869 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1870 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1871 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001872
Mike Stump31feda52009-07-17 01:31:16 +00001873 // Create an empty block to represent the transition block for looping back
1874 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001875 Block = 0;
1876 assert(Succ == EntryConditionBlock);
1877 Succ = createBlock();
1878 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001879 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001880
Ted Kremenek9aae5132007-08-23 21:42:29 +00001881 // All breaks should go to the code following the loop.
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001882 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001883
Ted Kremenek9aae5132007-08-23 21:42:29 +00001884 // NULL out Block to force lazy instantiation of blocks for the body.
1885 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001886
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001887 // Loop body should end with destructor of Condition variable (if any).
1888 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1889
1890 // If body is not a compound statement create implicit scope
1891 // and add destructors.
1892 if (!isa<CompoundStmt>(W->getBody()))
1893 addLocalScopeAndDtors(W->getBody());
1894
Ted Kremenek9aae5132007-08-23 21:42:29 +00001895 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001896 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001897
Ted Kremeneke9610502007-08-30 18:39:40 +00001898 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001899 BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001900 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001901 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001902 return 0;
1903 }
Mike Stump31feda52009-07-17 01:31:16 +00001904
Ted Kremenek30754282009-07-24 04:47:11 +00001905 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001906 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001907 }
Mike Stump31feda52009-07-17 01:31:16 +00001908
Ted Kremenek30754282009-07-24 04:47:11 +00001909 // Link up the condition block with the code that follows the loop. (the
1910 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001911 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001912
1913 // There can be no more statements in the condition block since we loop back
1914 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001915 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001916
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001917 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001918 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001919 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001920}
Mike Stump11289f42009-09-09 15:08:12 +00001921
1922
Ted Kremenek93668002009-07-17 22:18:43 +00001923CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1924 // FIXME: For now we pretend that @catch and the code it contains does not
1925 // exit.
1926 return Block;
1927}
Mike Stump31feda52009-07-17 01:31:16 +00001928
Ted Kremenek93041ba2008-12-09 20:20:09 +00001929CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1930 // FIXME: This isn't complete. We basically treat @throw like a return
1931 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001932
Ted Kremenek0868eea2009-09-24 18:45:41 +00001933 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001934 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001935 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001936
Ted Kremenek93041ba2008-12-09 20:20:09 +00001937 // Create the new block.
1938 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001939
Ted Kremenek93041ba2008-12-09 20:20:09 +00001940 // The Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001941 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001942
1943 // Add the statement to the block. This may create new blocks if S contains
1944 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001945 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001946}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001947
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001948CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001949 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001950 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001951 return 0;
1952
1953 // Create the new block.
1954 Block = createBlock(false);
1955
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001956 if (TryTerminatedBlock)
1957 // The current try statement is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001958 addSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001959 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001960 // otherwise the Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001961 addSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001962
1963 // Add the statement to the block. This may create new blocks if S contains
1964 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001965 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001966}
1967
Ted Kremenek93668002009-07-17 22:18:43 +00001968CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001969 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001970
Mike Stump8d50b6a2009-07-21 01:27:50 +00001971 // "do...while" is a control-flow statement. Thus we stop processing the
1972 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001973 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001974 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001975 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001976 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001977 } else
1978 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001979
1980 // Because of short-circuit evaluation, the condition of the loop can span
1981 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1982 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001983 CFGBlock* ExitConditionBlock = createBlock(false);
1984 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001985
Ted Kremenek81e14852007-08-27 19:46:09 +00001986 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001987 ExitConditionBlock->setTerminator(D);
1988
1989 // Now add the actual condition to the condition block. Because the condition
1990 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001991 if (Stmt* C = D->getCond()) {
1992 Block = ExitConditionBlock;
1993 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001994 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001995 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001996 return 0;
1997 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001998 }
Mike Stump31feda52009-07-17 01:31:16 +00001999
Ted Kremeneka1523a32008-02-27 07:20:00 +00002000 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002001 Succ = EntryConditionBlock;
2002
Mike Stump773582d2009-07-23 23:25:26 +00002003 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002004 const TryResult &KnownVal = tryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00002005
Ted Kremenek9aae5132007-08-23 21:42:29 +00002006 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002007 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002008 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002009 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002010
Ted Kremenek9aae5132007-08-23 21:42:29 +00002011 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002012 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2013 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2014 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00002015
Ted Kremenek9aae5132007-08-23 21:42:29 +00002016 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002017 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002018
Ted Kremenek9aae5132007-08-23 21:42:29 +00002019 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002020 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002021
Ted Kremenek9aae5132007-08-23 21:42:29 +00002022 // NULL out Block to force lazy instantiation of blocks for the body.
2023 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002024
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00002025 // If body is not a compound statement create implicit scope
2026 // and add destructors.
2027 if (!isa<CompoundStmt>(D->getBody()))
2028 addLocalScopeAndDtors(D->getBody());
2029
Ted Kremenek9aae5132007-08-23 21:42:29 +00002030 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00002031 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002032
Ted Kremeneke9610502007-08-30 18:39:40 +00002033 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00002034 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00002035 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002036 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002037 return 0;
2038 }
Mike Stump31feda52009-07-17 01:31:16 +00002039
Ted Kremenek110974d2010-08-17 20:59:56 +00002040 if (!KnownVal.isFalse()) {
2041 // Add an intermediate block between the BodyBlock and the
2042 // ExitConditionBlock to represent the "loop back" transition. Create an
2043 // empty block to represent the transition block for looping back to the
2044 // head of the loop.
2045 // FIXME: Can we do this more efficiently without adding another block?
2046 Block = NULL;
2047 Succ = BodyBlock;
2048 CFGBlock *LoopBackBlock = createBlock();
2049 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00002050
Ted Kremenek110974d2010-08-17 20:59:56 +00002051 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002052 addSuccessor(ExitConditionBlock, LoopBackBlock);
Ted Kremenek110974d2010-08-17 20:59:56 +00002053 }
2054 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002055 addSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002056 }
Mike Stump31feda52009-07-17 01:31:16 +00002057
Ted Kremenek30754282009-07-24 04:47:11 +00002058 // Link up the condition block with the code that follows the loop.
2059 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002060 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00002061
2062 // There can be no more statements in the body block(s) since we loop back to
2063 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002064 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002065
Ted Kremenek9aae5132007-08-23 21:42:29 +00002066 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00002067 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002068 return BodyBlock;
2069}
2070
2071CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2072 // "continue" is a control-flow statement. Thus we stop processing the
2073 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002074 if (badCFG)
2075 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002076
Ted Kremenek9aae5132007-08-23 21:42:29 +00002077 // Now create a new block that ends with the continue statement.
2078 Block = createBlock(false);
2079 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00002080
Ted Kremenek9aae5132007-08-23 21:42:29 +00002081 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00002082 // incomplete AST. This means the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00002083 if (ContinueJumpTarget.block) {
2084 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C);
2085 addSuccessor(Block, ContinueJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002086 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00002087 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00002088
Ted Kremenek9aae5132007-08-23 21:42:29 +00002089 return Block;
2090}
Mike Stump11289f42009-09-09 15:08:12 +00002091
Ted Kremenek0747de62009-07-18 00:47:21 +00002092CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002093 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002094
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002095 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002096 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002097 appendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00002098 }
Mike Stump11289f42009-09-09 15:08:12 +00002099
Ted Kremenek93668002009-07-17 22:18:43 +00002100 // VLA types have expressions that must be evaluated.
2101 if (E->isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00002102 for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr());
Ted Kremenek93668002009-07-17 22:18:43 +00002103 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2104 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00002105 }
Mike Stump11289f42009-09-09 15:08:12 +00002106
Mike Stump31feda52009-07-17 01:31:16 +00002107 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002108}
Mike Stump11289f42009-09-09 15:08:12 +00002109
Ted Kremenek93668002009-07-17 22:18:43 +00002110/// VisitStmtExpr - Utility method to handle (nested) statement
2111/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002112CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2113 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002114 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002115 appendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00002116 }
Ted Kremenek93668002009-07-17 22:18:43 +00002117 return VisitCompoundStmt(SE->getSubStmt());
2118}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002119
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002120CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00002121 // "switch" is a control-flow statement. Thus we stop processing the current
2122 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002123 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002124
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002125 // Save local scope position because in case of condition variable ScopePos
2126 // won't be restored when traversing AST.
2127 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2128
2129 // Create local scope for possible condition variable.
2130 // Store scope position. Add implicit destructor.
2131 if (VarDecl* VD = Terminator->getConditionVariable()) {
2132 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2133 addLocalScopeForVarDecl(VD);
2134 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2135 }
2136
Ted Kremenek9aae5132007-08-23 21:42:29 +00002137 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002138 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002139 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002140 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00002141 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002142
2143 // Save the current "switch" context.
2144 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00002145 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002146 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00002147
Mike Stump31feda52009-07-17 01:31:16 +00002148 // Set the "default" case to be the block after the switch statement. If the
2149 // switch statement contains a "default:", this value will be overwritten with
2150 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00002151 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00002152
Ted Kremenek9aae5132007-08-23 21:42:29 +00002153 // Create a new block that will contain the switch statement.
2154 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00002155
Ted Kremenek9aae5132007-08-23 21:42:29 +00002156 // Now process the switch body. The code after the switch is the implicit
2157 // successor.
2158 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002159 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002160
2161 // When visiting the body, the case statements should automatically get linked
2162 // up to the switch. We also don't keep a pointer to the body, since all
2163 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002164 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00002165 Block = NULL;
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002166
2167 // If body is not a compound statement create implicit scope
2168 // and add destructors.
2169 if (!isa<CompoundStmt>(Terminator->getBody()))
2170 addLocalScopeAndDtors(Terminator->getBody());
2171
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002172 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00002173 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002174 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002175 return 0;
2176 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002177
Mike Stump31feda52009-07-17 01:31:16 +00002178 // If we have no "default:" case, the default transition is to the code
2179 // following the switch body.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002180 addSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002181
Ted Kremenek81e14852007-08-27 19:46:09 +00002182 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002183 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002184 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00002185 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002186 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002187
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002188 // Finally, if the SwitchStmt contains a condition variable, add both the
2189 // SwitchStmt and the condition variable initialization to the CFG.
2190 if (VarDecl *VD = Terminator->getConditionVariable()) {
2191 if (Expr *Init = VD->getInit()) {
2192 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002193 appendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002194 addStmt(Init);
2195 }
2196 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002197
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002198 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002199}
2200
Ted Kremenek93668002009-07-17 22:18:43 +00002201CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00002202 // CaseStmts are essentially labels, so they are the first statement in a
2203 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00002204 CFGBlock *TopBlock = 0, *LastBlock = 0;
2205
2206 if (Stmt *Sub = CS->getSubStmt()) {
2207 // For deeply nested chains of CaseStmts, instead of doing a recursion
2208 // (which can blow out the stack), manually unroll and create blocks
2209 // along the way.
2210 while (isa<CaseStmt>(Sub)) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002211 CFGBlock *currentBlock = createBlock(false);
2212 currentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00002213
Ted Kremenek60fa6572010-08-04 23:54:30 +00002214 if (TopBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002215 addSuccessor(LastBlock, currentBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002216 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002217 TopBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002218
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002219 addSuccessor(SwitchTerminatedBlock, currentBlock);
2220 LastBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002221
2222 CS = cast<CaseStmt>(Sub);
2223 Sub = CS->getSubStmt();
2224 }
2225
2226 addStmt(Sub);
2227 }
Mike Stump11289f42009-09-09 15:08:12 +00002228
Ted Kremenek55e91e82007-08-30 18:48:11 +00002229 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002230 if (!CaseBlock)
2231 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002232
2233 // Cases statements partition blocks, so this is the top of the basic block we
2234 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00002235 CaseBlock->setLabel(CS);
2236
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002237 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002238 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002239
2240 // Add this block to the list of successors for the block with the switch
2241 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00002242 assert(SwitchTerminatedBlock);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002243 addSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002244
Ted Kremenek9aae5132007-08-23 21:42:29 +00002245 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2246 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002247
Ted Kremenek60fa6572010-08-04 23:54:30 +00002248 if (TopBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002249 addSuccessor(LastBlock, CaseBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002250 Succ = TopBlock;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002251 } else {
Ted Kremenek60fa6572010-08-04 23:54:30 +00002252 // This block is now the implicit successor of other blocks.
2253 Succ = CaseBlock;
2254 }
Mike Stump31feda52009-07-17 01:31:16 +00002255
Ted Kremenek60fa6572010-08-04 23:54:30 +00002256 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002257}
Mike Stump31feda52009-07-17 01:31:16 +00002258
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002259CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00002260 if (Terminator->getSubStmt())
2261 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00002262
Ted Kremenek654c78f2008-02-13 22:05:39 +00002263 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002264
2265 if (!DefaultCaseBlock)
2266 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002267
2268 // Default statements partition blocks, so this is the top of the basic block
2269 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002270 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00002271
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002272 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002273 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00002274
Mike Stump31feda52009-07-17 01:31:16 +00002275 // Unlike case statements, we don't add the default block to the successors
2276 // for the switch statement immediately. This is done when we finish
2277 // processing the switch statement. This allows for the default case
2278 // (including a fall-through to the code after the switch statement) to always
2279 // be the last successor of a switch-terminated block.
2280
Ted Kremenek654c78f2008-02-13 22:05:39 +00002281 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2282 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002283
Ted Kremenek654c78f2008-02-13 22:05:39 +00002284 // This block is now the implicit successor of other blocks.
2285 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002286
2287 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00002288}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002289
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002290CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2291 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2292 // current block.
2293 CFGBlock* TrySuccessor = NULL;
2294
2295 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002296 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002297 return 0;
2298 TrySuccessor = Block;
2299 } else TrySuccessor = Succ;
2300
Mike Stump0bdba6c2010-01-20 01:15:34 +00002301 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002302
2303 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00002304 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002305 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00002306 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002307
Mike Stump0bdba6c2010-01-20 01:15:34 +00002308 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002309 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2310 // The code after the try is the implicit successor.
2311 Succ = TrySuccessor;
2312 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002313 if (CS->getExceptionDecl() == 0) {
2314 HasCatchAll = true;
2315 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002316 Block = NULL;
2317 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2318 if (CatchBlock == 0)
2319 return 0;
2320 // Add this block to the list of successors for the block with the try
2321 // statement.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002322 addSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002323 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00002324 if (!HasCatchAll) {
2325 if (PrevTryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002326 addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002327 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002328 addSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00002329 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002330
2331 // The code after the try is the implicit successor.
2332 Succ = TrySuccessor;
2333
Mike Stump845384a2010-01-20 01:30:58 +00002334 // Save the current "try" context.
2335 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2336 TryTerminatedBlock = NewTryTerminatedBlock;
2337
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002338 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002339 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002340 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002341 return Block;
2342}
2343
2344CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2345 // CXXCatchStmt are treated like labels, so they are the first statement in a
2346 // block.
2347
Marcin Swiderski3546b1a2010-10-01 01:46:52 +00002348 // Save local scope position because in case of exception variable ScopePos
2349 // won't be restored when traversing AST.
2350 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2351
2352 // Create local scope for possible exception variable.
2353 // Store scope position. Add implicit destructor.
2354 if (VarDecl* VD = CS->getExceptionDecl()) {
2355 LocalScope::const_iterator BeginScopePos = ScopePos;
2356 addLocalScopeForVarDecl(VD);
2357 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2358 }
2359
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002360 if (CS->getHandlerBlock())
2361 addStmt(CS->getHandlerBlock());
2362
2363 CFGBlock* CatchBlock = Block;
2364 if (!CatchBlock)
2365 CatchBlock = createBlock();
2366
2367 CatchBlock->setLabel(CS);
2368
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002369 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002370 return 0;
2371
2372 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2373 Block = NULL;
2374
2375 return CatchBlock;
2376}
2377
John McCall5d413782010-12-06 08:20:24 +00002378CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002379 AddStmtChoice asc) {
2380 if (BuildOpts.AddImplicitDtors) {
2381 // If adding implicit destructors visit the full expression for adding
2382 // destructors of temporaries.
2383 VisitForTemporaryDtors(E->getSubExpr());
2384
2385 // Full expression has to be added as CFGStmt so it will be sequenced
2386 // before destructors of it's temporaries.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002387 asc = asc.withAlwaysAdd(true);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002388 }
2389 return Visit(E->getSubExpr(), asc);
2390}
2391
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002392CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2393 AddStmtChoice asc) {
2394 if (asc.alwaysAdd()) {
2395 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002396 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002397
2398 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002399 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002400 }
2401 return Visit(E->getSubExpr(), asc);
2402}
2403
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002404CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2405 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002406 autoCreateBlock();
Zhongxing Xufb2f8162010-11-03 11:14:06 +00002407 if (!C->isElidable())
Ted Kremenek8219b822010-12-16 07:46:53 +00002408 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002409
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002410 return VisitChildren(C);
2411}
2412
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002413CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2414 AddStmtChoice asc) {
2415 if (asc.alwaysAdd()) {
2416 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002417 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002418 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002419 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002420 }
2421 return Visit(E->getSubExpr(), asc);
2422}
2423
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002424CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2425 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002426 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002427 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002428 return VisitChildren(C);
2429}
2430
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002431CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00002432 AddStmtChoice asc) {
Zhongxing Xu7e612172010-04-13 09:38:01 +00002433 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002434 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu7e612172010-04-13 09:38:01 +00002435 return VisitChildren(C);
2436}
2437
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002438CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2439 AddStmtChoice asc) {
2440 if (asc.alwaysAdd()) {
2441 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002442 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002443 }
Ted Kremenek8219b822010-12-16 07:46:53 +00002444 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002445}
2446
Ted Kremenekeda180e22007-08-28 19:26:49 +00002447CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00002448 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00002449 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002450
Ted Kremenekeda180e22007-08-28 19:26:49 +00002451 if (!IBlock) {
2452 IBlock = createBlock(false);
2453 cfg->setIndirectGotoBlock(IBlock);
2454 }
Mike Stump31feda52009-07-17 01:31:16 +00002455
Ted Kremenekeda180e22007-08-28 19:26:49 +00002456 // IndirectGoto is a control-flow statement. Thus we stop processing the
2457 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002458 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00002459 return 0;
2460
Ted Kremenekeda180e22007-08-28 19:26:49 +00002461 Block = createBlock(false);
2462 Block->setTerminator(I);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002463 addSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00002464 return addStmt(I->getTarget());
2465}
2466
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002467CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2468tryAgain:
2469 if (!E) {
2470 badCFG = true;
2471 return NULL;
2472 }
2473 switch (E->getStmtClass()) {
2474 default:
2475 return VisitChildrenForTemporaryDtors(E);
2476
2477 case Stmt::BinaryOperatorClass:
2478 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2479
2480 case Stmt::CXXBindTemporaryExprClass:
2481 return VisitCXXBindTemporaryExprForTemporaryDtors(
2482 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2483
2484 case Stmt::ConditionalOperatorClass:
2485 return VisitConditionalOperatorForTemporaryDtors(
2486 cast<ConditionalOperator>(E), BindToTemporary);
2487
2488 case Stmt::ImplicitCastExprClass:
2489 // For implicit cast we want BindToTemporary to be passed further.
2490 E = cast<CastExpr>(E)->getSubExpr();
2491 goto tryAgain;
2492
2493 case Stmt::ParenExprClass:
2494 E = cast<ParenExpr>(E)->getSubExpr();
2495 goto tryAgain;
2496 }
2497}
2498
2499CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2500 // When visiting children for destructors we want to visit them in reverse
2501 // order. Because there's no reverse iterator for children must to reverse
2502 // them in helper vector.
2503 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2504 ChildrenVect ChildrenRev;
John McCall8322c3a2011-02-13 04:07:26 +00002505 for (Stmt::child_range I = E->children(); I; ++I) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002506 if (*I) ChildrenRev.push_back(*I);
2507 }
2508
2509 CFGBlock *B = Block;
2510 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2511 L = ChildrenRev.rend(); I != L; ++I) {
2512 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2513 B = R;
2514 }
2515 return B;
2516}
2517
2518CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2519 if (E->isLogicalOp()) {
2520 // Destructors for temporaries in LHS expression should be called after
2521 // those for RHS expression. Even if this will unnecessarily create a block,
2522 // this block will be used at least by the full expression.
2523 autoCreateBlock();
2524 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2525 if (badCFG)
2526 return NULL;
2527
2528 Succ = ConfluenceBlock;
2529 Block = NULL;
2530 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2531
2532 if (RHSBlock) {
2533 if (badCFG)
2534 return NULL;
2535
2536 // If RHS expression did produce destructors we need to connect created
2537 // blocks to CFG in same manner as for binary operator itself.
2538 CFGBlock *LHSBlock = createBlock(false);
2539 LHSBlock->setTerminator(CFGTerminator(E, true));
2540
2541 // For binary operator LHS block is before RHS in list of predecessors
2542 // of ConfluenceBlock.
2543 std::reverse(ConfluenceBlock->pred_begin(),
2544 ConfluenceBlock->pred_end());
2545
2546 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002547 TryResult KnownVal = tryEvaluateBool(E->getLHS());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002548 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2549 KnownVal.negate();
2550
2551 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2552 // itself.
2553 if (E->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002554 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2555 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002556 } else {
2557 assert (E->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002558 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2559 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002560 }
2561
2562 Block = LHSBlock;
2563 return LHSBlock;
2564 }
2565
2566 Block = ConfluenceBlock;
2567 return ConfluenceBlock;
2568 }
2569
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002570 if (E->isAssignmentOp()) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002571 // For assignment operator (=) LHS expression is visited
2572 // before RHS expression. For destructors visit them in reverse order.
2573 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2574 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2575 return LHSBlock ? LHSBlock : RHSBlock;
2576 }
2577
2578 // For any other binary operator RHS expression is visited before
2579 // LHS expression (order of children). For destructors visit them in reverse
2580 // order.
2581 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2582 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2583 return RHSBlock ? RHSBlock : LHSBlock;
2584}
2585
2586CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2587 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2588 // First add destructors for temporaries in subexpression.
2589 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xufee455f2010-11-14 15:23:50 +00002590 if (!BindToTemporary) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002591 // If lifetime of temporary is not prolonged (by assigning to constant
2592 // reference) add destructor for it.
2593 autoCreateBlock();
2594 appendTemporaryDtor(Block, E);
2595 B = Block;
2596 }
2597 return B;
2598}
2599
2600CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
2601 ConditionalOperator *E, bool BindToTemporary) {
2602 // First add destructors for condition expression. Even if this will
2603 // unnecessarily create a block, this block will be used at least by the full
2604 // expression.
2605 autoCreateBlock();
2606 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2607 if (badCFG)
2608 return NULL;
2609
2610 // Try to add block with destructors for LHS expression.
2611 CFGBlock *LHSBlock = NULL;
2612 if (E->getLHS()) {
2613 Succ = ConfluenceBlock;
2614 Block = NULL;
2615 LHSBlock = VisitForTemporaryDtors(E->getLHS(), BindToTemporary);
2616 if (badCFG)
2617 return NULL;
2618 }
2619
2620 // Try to add block with destructors for RHS expression;
2621 Succ = ConfluenceBlock;
2622 Block = NULL;
2623 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS(), BindToTemporary);
2624 if (badCFG)
2625 return NULL;
2626
2627 if (!RHSBlock && !LHSBlock) {
2628 // If neither LHS nor RHS expression had temporaries to destroy don't create
2629 // more blocks.
2630 Block = ConfluenceBlock;
2631 return Block;
2632 }
2633
2634 Block = createBlock(false);
2635 Block->setTerminator(CFGTerminator(E, true));
2636
2637 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002638 const TryResult &KnownVal = tryEvaluateBool(E->getCond());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002639
2640 if (LHSBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002641 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002642 } else if (KnownVal.isFalse()) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002643 addSuccessor(Block, NULL);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002644 } else {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002645 addSuccessor(Block, ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002646 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2647 }
2648
2649 if (!RHSBlock)
2650 RHSBlock = ConfluenceBlock;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002651 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002652
2653 return Block;
2654}
2655
Ted Kremenek04cca642007-08-23 21:26:19 +00002656} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00002657
Mike Stump31feda52009-07-17 01:31:16 +00002658/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2659/// no successors or predecessors. If this is the first block created in the
2660/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00002661CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00002662 bool first_block = begin() == end();
2663
2664 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002665 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2666 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2667 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00002668
2669 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002670 if (first_block)
2671 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00002672
2673 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002674 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002675}
2676
Ted Kremenek889073f2007-08-23 16:51:22 +00002677/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2678/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00002679CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002680 BuildOptions BO) {
Ted Kremenek889073f2007-08-23 16:51:22 +00002681 CFGBuilder Builder;
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002682 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek889073f2007-08-23 16:51:22 +00002683}
2684
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002685//===----------------------------------------------------------------------===//
2686// CFG: Queries for BlkExprs.
2687//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002688
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002689namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002690 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002691}
2692
Ted Kremenekbff98442009-12-23 23:37:10 +00002693static void FindSubExprAssignments(Stmt *S,
2694 llvm::SmallPtrSet<Expr*,50>& Set) {
2695 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00002696 return;
Mike Stump31feda52009-07-17 01:31:16 +00002697
John McCall8322c3a2011-02-13 04:07:26 +00002698 for (Stmt::child_range I = S->children(); I; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002699 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00002700 if (!child)
2701 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002702
Ted Kremenekbff98442009-12-23 23:37:10 +00002703 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002704 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00002705
Ted Kremenekbff98442009-12-23 23:37:10 +00002706 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00002707 }
2708}
2709
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002710static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2711 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00002712
2713 // Look for assignments that are used as subexpressions. These are the only
2714 // assignments that we want to *possibly* register as a block-level
2715 // expression. Basically, if an assignment occurs both in a subexpression and
2716 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00002717 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00002718
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002719 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002720 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002721 if (CFGStmt S = BI->getAs<CFGStmt>())
2722 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002723
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002724 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00002725
2726 // Iterate over the statements again on identify the Expr* and Stmt* at the
2727 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002728
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002729 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2730 CFGStmt CS = BI->getAs<CFGStmt>();
2731 if (!CS.isValid())
2732 continue;
2733 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00002734
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002735 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002736 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00002737 // expression are really "statements" whose value is never used by
2738 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002739 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002740 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002741 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2742 // Special handling for statement expressions. The last statement in
2743 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002744 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002745 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002746 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002747 (*M)[C->body_back()] = x;
2748 }
2749 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002750
Ted Kremenek95a123c2008-01-26 00:03:27 +00002751 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002752 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002753 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002754 }
Mike Stump31feda52009-07-17 01:31:16 +00002755
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002756 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002757
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002758 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002759
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002760 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002761 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002762 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002763 }
2764 }
Mike Stump31feda52009-07-17 01:31:16 +00002765
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002766 return M;
2767}
2768
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002769CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2770 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002771 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002772
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002773 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002774 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002775 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002776}
2777
2778unsigned CFG::getNumBlkExprs() {
2779 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2780 return M->size();
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002781
2782 // We assume callers interested in the number of BlkExprs will want
2783 // the map constructed if it doesn't already exist.
2784 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2785 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002786}
2787
Ted Kremenek6065ef62008-04-28 18:00:46 +00002788//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002789// Filtered walking of the CFG.
2790//===----------------------------------------------------------------------===//
2791
2792bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002793 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002794
2795 if (F.IgnoreDefaultsWithCoveredEnums) {
2796 // If the 'To' has no label or is labeled but the label isn't a
2797 // CaseStmt then filter this edge.
2798 if (const SwitchStmt *S =
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00002799 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002800 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00002801 const Stmt *L = To->getLabel();
2802 if (!L || !isa<CaseStmt>(L))
2803 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002804 }
2805 }
2806 }
2807
2808 return false;
2809}
2810
2811//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002812// Cleanup: CFG dstor.
2813//===----------------------------------------------------------------------===//
2814
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002815CFG::~CFG() {
2816 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2817}
Mike Stump31feda52009-07-17 01:31:16 +00002818
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002819//===----------------------------------------------------------------------===//
2820// CFG pretty printing
2821//===----------------------------------------------------------------------===//
2822
Ted Kremenek7e776b12007-08-22 18:22:34 +00002823namespace {
2824
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002825class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002826 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002827 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002828 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002829 DeclMapTy DeclMap;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002830 signed currentBlock;
2831 unsigned currentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002832 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002833public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002834
Chris Lattnerc61089a2009-06-30 01:26:17 +00002835 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002836 : currentBlock(0), currentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002837 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2838 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002839 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002840 BI != BEnd; ++BI, ++j ) {
2841 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2842 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2843 StmtMap[SE] = P;
2844
2845 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2846 DeclMap[DS->getSingleDecl()] = P;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002847
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002848 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2849 if (VarDecl* VD = IS->getConditionVariable())
2850 DeclMap[VD] = P;
2851
2852 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2853 if (VarDecl* VD = FS->getConditionVariable())
2854 DeclMap[VD] = P;
2855
2856 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2857 if (VarDecl* VD = WS->getConditionVariable())
2858 DeclMap[VD] = P;
2859
2860 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2861 if (VarDecl* VD = SS->getConditionVariable())
2862 DeclMap[VD] = P;
2863
2864 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2865 if (VarDecl* VD = CS->getExceptionDecl())
2866 DeclMap[VD] = P;
2867 }
2868 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002869 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002870 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002871 }
Mike Stump31feda52009-07-17 01:31:16 +00002872
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002873 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00002874
Chris Lattnerc61089a2009-06-30 01:26:17 +00002875 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002876 void setBlockID(signed i) { currentBlock = i; }
2877 void setStmtID(unsigned i) { currentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00002878
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002879 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2880 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002881
2882 if (I == StmtMap.end())
2883 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002884
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002885 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
2886 && I->second.second == currentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002887 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002888 }
Mike Stump31feda52009-07-17 01:31:16 +00002889
Ted Kremenek60983dc2010-01-19 20:52:05 +00002890 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002891 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002892 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002893
2894 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2895 DeclMapTy::iterator I = DeclMap.find(D);
2896
2897 if (I == DeclMap.end())
2898 return false;
2899
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002900 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
2901 && I->second.second == currentStmt) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002902 return false;
2903 }
2904
2905 OS << "[B" << I->second.first << "." << I->second.second << "]";
2906 return true;
2907 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002908};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002909} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002910
Chris Lattnerc61089a2009-06-30 01:26:17 +00002911
2912namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002913class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002914 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002915
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002916 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002917 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002918 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002919public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002920 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002921 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002922 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002923
Ted Kremenek9aae5132007-08-23 21:42:29 +00002924 void VisitIfStmt(IfStmt* I) {
2925 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002926 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002927 }
Mike Stump31feda52009-07-17 01:31:16 +00002928
Ted Kremenek9aae5132007-08-23 21:42:29 +00002929 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002930 void VisitStmt(Stmt* Terminator) {
2931 Terminator->printPretty(OS, Helper, Policy);
2932 }
2933
Ted Kremenek9aae5132007-08-23 21:42:29 +00002934 void VisitForStmt(ForStmt* F) {
2935 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002936 if (F->getInit())
2937 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002938 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002939 if (Stmt* C = F->getCond())
2940 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002941 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002942 if (F->getInc())
2943 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002944 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002945 }
Mike Stump31feda52009-07-17 01:31:16 +00002946
Ted Kremenek9aae5132007-08-23 21:42:29 +00002947 void VisitWhileStmt(WhileStmt* W) {
2948 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002949 if (Stmt* C = W->getCond())
2950 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002951 }
Mike Stump31feda52009-07-17 01:31:16 +00002952
Ted Kremenek9aae5132007-08-23 21:42:29 +00002953 void VisitDoStmt(DoStmt* D) {
2954 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002955 if (Stmt* C = D->getCond())
2956 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002957 }
Mike Stump31feda52009-07-17 01:31:16 +00002958
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002959 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002960 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002961 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002962 }
Mike Stump31feda52009-07-17 01:31:16 +00002963
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002964 void VisitCXXTryStmt(CXXTryStmt* CS) {
2965 OS << "try ...";
2966 }
2967
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002968 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002969 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002970 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002971 }
Mike Stump31feda52009-07-17 01:31:16 +00002972
Ted Kremenek391f94a2007-08-31 22:29:13 +00002973 void VisitChooseExpr(ChooseExpr* C) {
2974 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002975 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002976 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002977 }
Mike Stump31feda52009-07-17 01:31:16 +00002978
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002979 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2980 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002981 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002982 }
Mike Stump31feda52009-07-17 01:31:16 +00002983
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002984 void VisitBinaryOperator(BinaryOperator* B) {
2985 if (!B->isLogicalOp()) {
2986 VisitExpr(B);
2987 return;
2988 }
Mike Stump31feda52009-07-17 01:31:16 +00002989
Douglas Gregor7de59662009-05-29 20:38:28 +00002990 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002991
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002992 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002993 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002994 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002995 return;
John McCalle3027922010-08-25 11:45:40 +00002996 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002997 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002998 return;
2999 default:
3000 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00003001 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003002 }
Mike Stump31feda52009-07-17 01:31:16 +00003003
Ted Kremenek12687ff2007-08-27 21:54:41 +00003004 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00003005 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003006 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00003007};
Chris Lattnerc61089a2009-06-30 01:26:17 +00003008} // end anonymous namespace
3009
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003010static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00003011 const CFGElement &E) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003012 if (CFGStmt CS = E.getAs<CFGStmt>()) {
3013 Stmt *S = CS;
3014
3015 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00003016
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003017 // special printing for statement-expressions.
3018 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3019 CompoundStmt* Sub = SE->getSubStmt();
3020
John McCall8322c3a2011-02-13 04:07:26 +00003021 if (Sub->children()) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003022 OS << "({ ... ; ";
3023 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3024 OS << " })\n";
3025 return;
3026 }
3027 }
3028 // special printing for comma expressions.
3029 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3030 if (B->getOpcode() == BO_Comma) {
3031 OS << "... , ";
3032 Helper->handledStmt(B->getRHS(),OS);
3033 OS << '\n';
3034 return;
3035 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003036 }
3037 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003038 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00003039
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003040 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan59f09c72010-11-22 19:32:14 +00003041 OS << " (OperatorCall)";
3042 } else if (isa<CXXBindTemporaryExpr>(S)) {
3043 OS << " (BindTemporary)";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003044 }
Mike Stump31feda52009-07-17 01:31:16 +00003045
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003046 // Expressions need a newline.
3047 if (isa<Expr>(S))
3048 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00003049
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003050 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
Alexis Hunt1d792652011-01-08 20:30:50 +00003051 CXXCtorInitializer* I = IE;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003052 if (I->isBaseInitializer())
3053 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichetd583da02010-12-04 09:14:42 +00003054 else OS << I->getAnyMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00003055
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003056 OS << "(";
3057 if (Expr* IE = I->getInit())
3058 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3059 OS << ")";
3060
3061 if (I->isBaseInitializer())
3062 OS << " (Base initializer)\n";
3063 else OS << " (Member initializer)\n";
3064
3065 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
3066 VarDecl* VD = DE.getVarDecl();
3067 Helper->handleDecl(VD, OS);
3068
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003069 const Type* T = VD->getType().getTypePtr();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003070 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3071 T = RT->getPointeeType().getTypePtr();
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003072 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3073 T = ET;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003074
3075 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3076 OS << " (Implicit destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003077
3078 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
3079 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
3080 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003081 OS << " (Base object destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003082
3083 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
3084 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski01769902010-10-25 07:05:54 +00003085
3086 const Type *T = FD->getType().getTypePtr();
3087 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3088 T = ET;
3089
Marcin Swiderski20b88732010-10-05 05:37:00 +00003090 OS << "this->" << FD->getName();
Marcin Swiderski01769902010-10-25 07:05:54 +00003091 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003092 OS << " (Member object destructor)\n";
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00003093
3094 } else if (CFGTemporaryDtor TE = E.getAs<CFGTemporaryDtor>()) {
3095 CXXBindTemporaryExpr *BT = TE.getBindTemporaryExpr();
3096 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3097 OS << " (Temporary object destructor)\n";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003098 }
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00003099}
Mike Stump31feda52009-07-17 01:31:16 +00003100
Chris Lattnerc61089a2009-06-30 01:26:17 +00003101static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3102 const CFGBlock& B,
3103 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00003104
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003105 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00003106
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003107 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00003108 OS << "\n [ B" << B.getBlockID();
3109
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003110 if (&B == &cfg->getEntry())
3111 OS << " (ENTRY) ]\n";
3112 else if (&B == &cfg->getExit())
3113 OS << " (EXIT) ]\n";
3114 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003115 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003116 else
3117 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00003118
Ted Kremenek71eca012007-08-29 23:20:49 +00003119 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00003120 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003121
3122 if (print_edges)
3123 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003124
Mike Stump92244b02010-01-19 22:00:14 +00003125 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003126 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00003127 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00003128 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003129 C->getLHS()->printPretty(OS, Helper,
3130 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003131 if (C->getRHS()) {
3132 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003133 C->getRHS()->printPretty(OS, Helper,
3134 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003135 }
Mike Stump92244b02010-01-19 22:00:14 +00003136 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003137 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00003138 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003139 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00003140 if (CS->getExceptionDecl())
3141 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3142 0);
3143 else
3144 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003145 OS << ")";
3146
3147 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003148 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00003149
Ted Kremenek71eca012007-08-29 23:20:49 +00003150 OS << ":\n";
3151 }
Mike Stump31feda52009-07-17 01:31:16 +00003152
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003153 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003154 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00003155
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003156 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3157 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00003158
Ted Kremenek71eca012007-08-29 23:20:49 +00003159 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003160 if (print_edges)
3161 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003162
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003163 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00003164
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003165 if (Helper)
3166 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00003167
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003168 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003169 }
Mike Stump31feda52009-07-17 01:31:16 +00003170
Ted Kremenek71eca012007-08-29 23:20:49 +00003171 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003172 if (B.getTerminator()) {
3173 if (print_edges)
3174 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003175
Ted Kremenek71eca012007-08-29 23:20:49 +00003176 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00003177
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003178 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00003179
Chris Lattnerc61089a2009-06-30 01:26:17 +00003180 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3181 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003182 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003183 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003184 }
Mike Stump31feda52009-07-17 01:31:16 +00003185
Ted Kremenek71eca012007-08-29 23:20:49 +00003186 if (print_edges) {
3187 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003188 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00003189 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00003190
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003191 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3192 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003193
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003194 if (i == 8 || (i-8) == 0)
3195 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00003196
Ted Kremenek71eca012007-08-29 23:20:49 +00003197 OS << " B" << (*I)->getBlockID();
3198 }
Mike Stump31feda52009-07-17 01:31:16 +00003199
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003200 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00003201
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003202 // Print the successors of this block.
3203 OS << " Successors (" << B.succ_size() << "):";
3204 i = 0;
3205
3206 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3207 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003208
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003209 if (i == 8 || (i-8) % 10 == 0)
3210 OS << "\n ";
3211
Mike Stump0d76d072009-07-20 23:24:15 +00003212 if (*I)
3213 OS << " B" << (*I)->getBlockID();
3214 else
3215 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003216 }
Mike Stump31feda52009-07-17 01:31:16 +00003217
Ted Kremenek71eca012007-08-29 23:20:49 +00003218 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003219 }
Mike Stump31feda52009-07-17 01:31:16 +00003220}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003221
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003222
3223/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003224void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003225
3226/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003227void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3228 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00003229
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003230 // Print the entry block.
3231 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00003232
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003233 // Iterate through the CFGBlocks and print them one by one.
3234 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3235 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003236 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003237 continue;
Mike Stump31feda52009-07-17 01:31:16 +00003238
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003239 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003240 }
Mike Stump31feda52009-07-17 01:31:16 +00003241
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003242 // Print the exit block.
3243 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00003244 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00003245}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003246
3247/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003248void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3249 print(llvm::errs(), cfg, LO);
3250}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003251
3252/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3253/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003254void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3255 const LangOptions &LO) const {
3256 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003257 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00003258}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003259
Ted Kremenek15647632008-01-30 23:02:42 +00003260/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003261void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00003262 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00003263 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003264 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003265}
3266
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003267Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003268 Stmt *Terminator = this->Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003269 if (!Terminator)
3270 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003271
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003272 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003273
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003274 switch (Terminator->getStmtClass()) {
3275 default:
3276 break;
Mike Stump31feda52009-07-17 01:31:16 +00003277
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003278 case Stmt::ForStmtClass:
3279 E = cast<ForStmt>(Terminator)->getCond();
3280 break;
Mike Stump31feda52009-07-17 01:31:16 +00003281
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003282 case Stmt::WhileStmtClass:
3283 E = cast<WhileStmt>(Terminator)->getCond();
3284 break;
Mike Stump31feda52009-07-17 01:31:16 +00003285
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003286 case Stmt::DoStmtClass:
3287 E = cast<DoStmt>(Terminator)->getCond();
3288 break;
Mike Stump31feda52009-07-17 01:31:16 +00003289
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003290 case Stmt::IfStmtClass:
3291 E = cast<IfStmt>(Terminator)->getCond();
3292 break;
Mike Stump31feda52009-07-17 01:31:16 +00003293
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003294 case Stmt::ChooseExprClass:
3295 E = cast<ChooseExpr>(Terminator)->getCond();
3296 break;
Mike Stump31feda52009-07-17 01:31:16 +00003297
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003298 case Stmt::IndirectGotoStmtClass:
3299 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3300 break;
Mike Stump31feda52009-07-17 01:31:16 +00003301
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003302 case Stmt::SwitchStmtClass:
3303 E = cast<SwitchStmt>(Terminator)->getCond();
3304 break;
Mike Stump31feda52009-07-17 01:31:16 +00003305
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003306 case Stmt::ConditionalOperatorClass:
3307 E = cast<ConditionalOperator>(Terminator)->getCond();
3308 break;
Mike Stump31feda52009-07-17 01:31:16 +00003309
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003310 case Stmt::BinaryOperatorClass: // '&&' and '||'
3311 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003312 break;
Mike Stump31feda52009-07-17 01:31:16 +00003313
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003314 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00003315 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003316 }
Mike Stump31feda52009-07-17 01:31:16 +00003317
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003318 return E ? E->IgnoreParens() : NULL;
3319}
3320
Ted Kremenek92137a32008-05-16 16:06:00 +00003321bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003322 const Stmt *Terminator = this->Terminator;
Ted Kremenek92137a32008-05-16 16:06:00 +00003323 if (!Terminator)
3324 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003325
Ted Kremenek92137a32008-05-16 16:06:00 +00003326 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003327
Ted Kremenek92137a32008-05-16 16:06:00 +00003328 switch (Terminator->getStmtClass()) {
3329 default:
3330 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003331
3332 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003333 case Stmt::WhileStmtClass:
3334 case Stmt::DoStmtClass:
3335 case Stmt::IfStmtClass:
3336 case Stmt::ChooseExprClass:
3337 case Stmt::ConditionalOperatorClass:
3338 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00003339 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00003340 }
Mike Stump31feda52009-07-17 01:31:16 +00003341
Ted Kremenek92137a32008-05-16 16:06:00 +00003342 return E ? E->IgnoreParens() : NULL;
3343}
3344
Ted Kremenek15647632008-01-30 23:02:42 +00003345
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003346//===----------------------------------------------------------------------===//
3347// CFG Graphviz Visualization
3348//===----------------------------------------------------------------------===//
3349
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003350
3351#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00003352static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003353#endif
3354
Chris Lattnerc61089a2009-06-30 01:26:17 +00003355void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003356#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00003357 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003358 GraphHelper = &H;
3359 llvm::ViewGraph(this,"CFG");
3360 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003361#endif
3362}
3363
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003364namespace llvm {
3365template<>
3366struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00003367
3368 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3369
3370 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003371
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003372#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003373 std::string OutSStr;
3374 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003375 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003376 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003377
3378 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3379
3380 // Process string output to make it nicer...
3381 for (unsigned i = 0; i != OutStr.length(); ++i)
3382 if (OutStr[i] == '\n') { // Left justify
3383 OutStr[i] = '\\';
3384 OutStr.insert(OutStr.begin()+i+1, 'l');
3385 }
Mike Stump31feda52009-07-17 01:31:16 +00003386
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003387 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003388#else
3389 return "";
3390#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003391 }
3392};
3393} // end namespace llvm