blob: 8f8c475d354d07d3852f1739be32d9097b898ec1 [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"
Ted Kremenek1a241d12011-02-23 05:11:46 +000020#include "clang/AST/CharUnits.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000021#include "llvm/Support/GraphWriter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000022#include "llvm/Support/Allocator.h"
23#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000024#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000025#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek8aed4902009-10-20 23:46:25 +000026#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000027
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000028using namespace clang;
29
30namespace {
31
Douglas Gregor6e6ad602009-01-20 01:17:11 +000032static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000033 if (VarDecl* VD = dyn_cast<VarDecl>(D))
34 if (Expr* Ex = VD->getInit())
35 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000036 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000037}
Ted Kremenekdc03bd02010-08-02 23:46:59 +000038
Ted Kremenek7c58d352011-03-10 01:14:11 +000039class CFGBuilder;
40
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000041/// The CFG builder uses a recursive algorithm to build the CFG. When
42/// we process an expression, sometimes we know that we must add the
43/// subexpressions as block-level expressions. For example:
44///
45/// exp1 || exp2
46///
47/// When processing the '||' expression, we know that exp1 and exp2
48/// need to be added as block-level expressions, even though they
49/// might not normally need to be. AddStmtChoice records this
50/// contextual information. If AddStmtChoice is 'NotAlwaysAdd', then
51/// the builder has an option not to add a subexpression as a
52/// block-level expression.
53///
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000054class AddStmtChoice {
55public:
Ted Kremenek8219b822010-12-16 07:46:53 +000056 enum Kind { NotAlwaysAdd = 0, AlwaysAdd = 1 };
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000057
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000058 AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {}
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000059
Ted Kremenek7c58d352011-03-10 01:14:11 +000060 bool alwaysAdd(CFGBuilder &builder,
61 const Stmt *stmt) const;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000062
63 /// Return a copy of this object, except with the 'always-add' bit
64 /// set as specified.
65 AddStmtChoice withAlwaysAdd(bool alwaysAdd) const {
Ted Kremenek7c58d352011-03-10 01:14:11 +000066 return AddStmtChoice(alwaysAdd ? AlwaysAdd : NotAlwaysAdd);
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000067 }
68
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000069private:
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000070 Kind kind;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000071};
Mike Stump31feda52009-07-17 01:31:16 +000072
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000073/// LocalScope - Node in tree of local scopes created for C++ implicit
74/// destructor calls generation. It contains list of automatic variables
75/// declared in the scope and link to position in previous scope this scope
76/// began in.
77///
78/// The process of creating local scopes is as follows:
79/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
80/// - Before processing statements in scope (e.g. CompoundStmt) create
81/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
82/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000083/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000084/// at this VarDecl,
85/// - For every normal (without jump) end of scope add to CFGBlock destructors
86/// for objects in the current scope,
87/// - For every jump add to CFGBlock destructors for objects
88/// between CFGBuilder::ScopePos and local scope position saved for jump
89/// target. Thanks to C++ restrictions on goto jumps we can be sure that
90/// jump target position will be on the path to root from CFGBuilder::ScopePos
91/// (adding any variable that doesn't need constructor to be called to
92/// LocalScope can break this assumption),
93///
94class LocalScope {
95public:
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +000096 typedef BumpVector<VarDecl*> AutomaticVarsTy;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000097
98 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000099 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000100 class const_iterator {
101 const LocalScope* Scope;
102
103 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
104 /// Invalid iterator (with null Scope) has VarIter equal to 0.
105 unsigned VarIter;
106
107 public:
108 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
109 /// Incrementing invalid iterator is allowed and will result in invalid
110 /// iterator.
111 const_iterator()
112 : Scope(NULL), VarIter(0) {}
113
114 /// Create valid iterator. In case when S.Prev is an invalid iterator and
115 /// I is equal to 0, this will create invalid iterator.
116 const_iterator(const LocalScope& S, unsigned I)
117 : Scope(&S), VarIter(I) {
118 // Iterator to "end" of scope is not allowed. Handle it by going up
119 // in scopes tree possibly up to invalid iterator in the root.
120 if (VarIter == 0 && Scope)
121 *this = Scope->Prev;
122 }
123
124 VarDecl* const* operator->() const {
125 assert (Scope && "Dereferencing invalid iterator is not allowed");
126 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
127 return &Scope->Vars[VarIter - 1];
128 }
129 VarDecl* operator*() const {
130 return *this->operator->();
131 }
132
133 const_iterator& operator++() {
134 if (!Scope)
135 return *this;
136
137 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
138 --VarIter;
139 if (VarIter == 0)
140 *this = Scope->Prev;
141 return *this;
142 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000143 const_iterator operator++(int) {
144 const_iterator P = *this;
145 ++*this;
146 return P;
147 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000148
149 bool operator==(const const_iterator& rhs) const {
150 return Scope == rhs.Scope && VarIter == rhs.VarIter;
151 }
152 bool operator!=(const const_iterator& rhs) const {
153 return !(*this == rhs);
154 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000155
156 operator bool() const {
157 return *this != const_iterator();
158 }
159
160 int distance(const_iterator L);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000161 };
162
163 friend class const_iterator;
164
165private:
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000166 BumpVectorContext ctx;
167
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000168 /// Automatic variables in order of declaration.
169 AutomaticVarsTy Vars;
170 /// Iterator to variable in previous scope that was declared just before
171 /// begin of this scope.
172 const_iterator Prev;
173
174public:
175 /// Constructs empty scope linked to previous scope in specified place.
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000176 LocalScope(BumpVectorContext &ctx, const_iterator P)
177 : ctx(ctx), Vars(ctx, 4), Prev(P) {}
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000178
179 /// Begin of scope in direction of CFG building (backwards).
180 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000181
182 void addVar(VarDecl* VD) {
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000183 Vars.push_back(VD, ctx);
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000184 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000185};
186
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000187/// distance - Calculates distance from this to L. L must be reachable from this
188/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
189/// number of scopes between this and L.
190int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
191 int D = 0;
192 const_iterator F = *this;
193 while (F.Scope != L.Scope) {
194 assert (F != const_iterator()
195 && "L iterator is not reachable from F iterator.");
196 D += F.VarIter;
197 F = F.Scope->Prev;
198 }
199 D += F.VarIter - L.VarIter;
200 return D;
201}
202
203/// BlockScopePosPair - Structure for specifying position in CFG during its
204/// build process. It consists of CFGBlock that specifies position in CFG graph
205/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000206struct BlockScopePosPair {
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000207 BlockScopePosPair() : block(0) {}
208 BlockScopePosPair(CFGBlock* b, LocalScope::const_iterator scopePos)
209 : block(b), scopePosition(scopePos) {}
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000210
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000211 CFGBlock *block;
212 LocalScope::const_iterator scopePosition;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000213};
214
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000215/// TryResult - a class representing a variant over the values
216/// 'true', 'false', or 'unknown'. This is returned by tryEvaluateBool,
217/// and is used by the CFGBuilder to decide if a branch condition
218/// can be decided up front during CFG construction.
219class TryResult {
220 int X;
221public:
222 TryResult(bool b) : X(b ? 1 : 0) {}
223 TryResult() : X(-1) {}
224
225 bool isTrue() const { return X == 1; }
226 bool isFalse() const { return X == 0; }
227 bool isKnown() const { return X >= 0; }
228 void negate() {
229 assert(isKnown());
230 X ^= 0x1;
231 }
232};
233
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000234/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000235/// The builder is stateful: an instance of the builder should be used to only
236/// construct a single CFG.
237///
238/// Example usage:
239///
240/// CFGBuilder builder;
241/// CFG* cfg = builder.BuildAST(stmt1);
242///
Mike Stump31feda52009-07-17 01:31:16 +0000243/// CFG construction is done via a recursive walk of an AST. We actually parse
244/// the AST in reverse order so that the successor of a basic block is
245/// constructed prior to its predecessor. This allows us to nicely capture
246/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +0000247///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000248class CFGBuilder {
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000249 typedef BlockScopePosPair JumpTarget;
250 typedef BlockScopePosPair JumpSource;
251
Mike Stump0d76d072009-07-20 23:24:15 +0000252 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000253 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000254
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000255 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000256 CFGBlock* Succ;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000257 JumpTarget ContinueJumpTarget;
258 JumpTarget BreakJumpTarget;
Ted Kremenek879d8e12007-08-23 18:43:24 +0000259 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +0000260 CFGBlock* DefaultCaseBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000261 CFGBlock* TryTerminatedBlock;
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000262
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000263 // Current position in local scope.
264 LocalScope::const_iterator ScopePos;
265
266 // LabelMap records the mapping from Label expressions to their jump targets.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000267 typedef llvm::DenseMap<LabelDecl*, JumpTarget> LabelMapTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000268 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +0000269
270 // A list of blocks that end with a "goto" that must be backpatched to their
271 // resolved targets upon completion of CFG construction.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000272 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000273 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +0000274
Ted Kremenekeda180e22007-08-28 19:26:49 +0000275 // A list of labels whose address has been taken (for indirect gotos).
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000276 typedef llvm::SmallPtrSet<LabelDecl*, 5> LabelSetTy;
Ted Kremenekeda180e22007-08-28 19:26:49 +0000277 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +0000278
Zhongxing Xud38fb842010-09-16 03:28:18 +0000279 bool badCFG;
Ted Kremenekf9d82902011-03-10 01:14:05 +0000280 const CFG::BuildOptions &BuildOpts;
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000281
282 // State to track for building switch statements.
283 bool switchExclusivelyCovered;
Ted Kremenekbe528712011-03-04 01:03:41 +0000284 Expr::EvalResult *switchCond;
Ted Kremeneka099c592011-03-10 03:50:34 +0000285
286 CFG::BuildOptions::ForcedBlkExprs::value_type *cachedEntry;
Zhongxing Xud38fb842010-09-16 03:28:18 +0000287
Mike Stump31feda52009-07-17 01:31:16 +0000288public:
Ted Kremenekf9d82902011-03-10 01:14:05 +0000289 explicit CFGBuilder(ASTContext *astContext,
290 const CFG::BuildOptions &buildOpts)
291 : Context(astContext), cfg(new CFG()), // crew a new CFG
292 Block(NULL), Succ(NULL),
293 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
294 TryTerminatedBlock(NULL), badCFG(false), BuildOpts(buildOpts),
Ted Kremeneka099c592011-03-10 03:50:34 +0000295 switchExclusivelyCovered(false), switchCond(0),
296 cachedEntry(0) {}
Mike Stump31feda52009-07-17 01:31:16 +0000297
Ted Kremenek9aae5132007-08-23 21:42:29 +0000298 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekf9d82902011-03-10 01:14:05 +0000299 CFG* buildCFG(const Decl *D, Stmt *Statement);
Mike Stump31feda52009-07-17 01:31:16 +0000300
Ted Kremeneka099c592011-03-10 03:50:34 +0000301 bool alwaysAdd(const Stmt *stmt);
302
Ted Kremenek93668002009-07-17 22:18:43 +0000303private:
304 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000305 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
306 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
307 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000308 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000309 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
John McCall5d413782010-12-06 08:20:24 +0000310 CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000311 AddStmtChoice asc);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000312 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
313 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000314 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
315 AddStmtChoice asc);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000316 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000317 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
318 AddStmtChoice asc);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000319 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
320 AddStmtChoice asc);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000321 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000322 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000323 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000324 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000325 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
John McCallc07a0c72011-02-17 10:25:35 +0000326 CFGBlock *VisitConditionalOperator(AbstractConditionalOperator *C,
327 AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000328 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000329 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000330 CFGBlock *VisitDeclSubExpr(DeclStmt* DS);
Ted Kremenek21822592009-07-17 18:20:32 +0000331 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
332 CFGBlock *VisitDoStmt(DoStmt *D);
333 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000334 CFGBlock *VisitGotoStmt(GotoStmt* G);
335 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000336 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000337 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
338 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000339 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000340 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
341 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
342 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
343 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
344 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
345 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000346 CFGBlock *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E,
347 AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000348 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000349 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan6dace612010-11-22 08:45:56 +0000350 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000351 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000352
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000353 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
354 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000355 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000356
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000357 // Visitors to walk an AST and generate destructors of temporaries in
358 // full expression.
359 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
360 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
361 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
362 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
363 bool BindToTemporary);
John McCallc07a0c72011-02-17 10:25:35 +0000364 CFGBlock *
365 VisitConditionalOperatorForTemporaryDtors(AbstractConditionalOperator *E,
366 bool BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000367
Ted Kremenek6065ef62008-04-28 18:00:46 +0000368 // NYS == Not Yet Supported
369 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000370 badCFG = true;
371 return Block;
372 }
Mike Stump31feda52009-07-17 01:31:16 +0000373
Ted Kremenek93668002009-07-17 22:18:43 +0000374 void autoCreateBlock() { if (!Block) Block = createBlock(); }
375 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000376
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000377 CFGBlock *addStmt(Stmt *S) {
378 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000379 }
Alexis Hunt1d792652011-01-08 20:30:50 +0000380 CFGBlock *addInitializer(CXXCtorInitializer *I);
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000381 void addAutomaticObjDtors(LocalScope::const_iterator B,
382 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski20b88732010-10-05 05:37:00 +0000383 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000384
Marcin Swiderski5e415732010-09-30 23:05:00 +0000385 // Local scopes creation.
386 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
387
Zhongxing Xu81714f22010-10-01 03:00:16 +0000388 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000389 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
390 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
391
392 void addLocalScopeAndDtors(Stmt* S);
393
394 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek2866bab2011-03-10 01:14:08 +0000395 void appendStmt(CFGBlock *B, Stmt *S) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000396 if (cachedEntry) {
397 assert(cachedEntry->first == S);
398 cachedEntry->second = B;
399 cachedEntry = 0;
400 }
401
Ted Kremenek8219b822010-12-16 07:46:53 +0000402 B->appendStmt(S, cfg->getBumpVectorContext());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000403 }
Alexis Hunt1d792652011-01-08 20:30:50 +0000404 void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000405 B->appendInitializer(I, cfg->getBumpVectorContext());
406 }
Marcin Swiderski20b88732010-10-05 05:37:00 +0000407 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
408 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
409 }
410 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
411 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
412 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000413 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
414 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
415 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000416
Marcin Swiderski321a7072010-09-30 22:54:37 +0000417 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
418 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
419 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
420 LocalScope::const_iterator E, Stmt* S);
421 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
422 LocalScope::const_iterator B, LocalScope::const_iterator E);
423
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000424 void addSuccessor(CFGBlock *B, CFGBlock *S) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000425 B->addSuccessor(S, cfg->getBumpVectorContext());
426 }
Mike Stump11289f42009-09-09 15:08:12 +0000427
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000428 /// Try and evaluate an expression to an integer constant.
429 bool tryEvaluate(Expr *S, Expr::EvalResult &outResult) {
430 if (!BuildOpts.PruneTriviallyFalseEdges)
431 return false;
432 return !S->isTypeDependent() &&
433 !S->isValueDependent() &&
434 S->Evaluate(outResult, *Context);
435 }
Mike Stump11289f42009-09-09 15:08:12 +0000436
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000437 /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
Mike Stump773582d2009-07-23 23:25:26 +0000438 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000439 TryResult tryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000440 Expr::EvalResult Result;
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000441 if (!tryEvaluate(S, Result))
442 return TryResult();
443
444 if (Result.Val.isInt())
445 return Result.Val.getInt().getBoolValue();
446
447 if (Result.Val.isLValue()) {
448 Expr *e = Result.Val.getLValueBase();
449 const CharUnits &c = Result.Val.getLValueOffset();
450 if (!e && c.isZero())
451 return false;
Ted Kremenek1a241d12011-02-23 05:11:46 +0000452 }
Ted Kremenek30754282009-07-24 04:47:11 +0000453 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000454 }
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000455
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000456};
Mike Stump31feda52009-07-17 01:31:16 +0000457
Ted Kremeneka099c592011-03-10 03:50:34 +0000458inline bool AddStmtChoice::alwaysAdd(CFGBuilder &builder,
459 const Stmt *stmt) const {
460 return builder.alwaysAdd(stmt) || kind == AlwaysAdd;
461}
462
463bool CFGBuilder::alwaysAdd(const Stmt *stmt) {
464 if (!BuildOpts.forcedBlkExprs)
465 return false;
466
467 CFG::BuildOptions::ForcedBlkExprs *fb = *BuildOpts.forcedBlkExprs;
468
469 if (!fb)
470 return false;
471
472 CFG::BuildOptions::ForcedBlkExprs::iterator itr = fb->find(stmt);
473 if (itr == fb->end())
474 return false;
475
476 cachedEntry = &*itr;
477 return true;
Ted Kremenek7c58d352011-03-10 01:14:11 +0000478}
479
Douglas Gregor4619e432008-12-05 23:32:09 +0000480// FIXME: Add support for dependent-sized array types in C++?
481// Does it even make sense to build a CFG for an uninstantiated template?
John McCall424cec92011-01-19 06:33:43 +0000482static const VariableArrayType *FindVA(const Type *t) {
483 while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
484 if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000485 if (vat->getSizeExpr())
486 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000487
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000488 t = vt->getElementType().getTypePtr();
489 }
Mike Stump31feda52009-07-17 01:31:16 +0000490
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000491 return 0;
492}
Mike Stump31feda52009-07-17 01:31:16 +0000493
494/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
495/// arbitrary statement. Examples include a single expression or a function
496/// body (compound statement). The ownership of the returned CFG is
497/// transferred to the caller. If CFG construction fails, this method returns
498/// NULL.
Ted Kremenekf9d82902011-03-10 01:14:05 +0000499CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement) {
Ted Kremenek8aed4902009-10-20 23:46:25 +0000500 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000501 if (!Statement)
502 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000503
Mike Stump31feda52009-07-17 01:31:16 +0000504 // Create an empty block that will serve as the exit block for the CFG. Since
505 // this is the first block added to the CFG, it will be implicitly registered
506 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000507 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000508 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000509 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000510
Marcin Swiderski20b88732010-10-05 05:37:00 +0000511 if (BuildOpts.AddImplicitDtors)
512 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
513 addImplicitDtorsForDestructor(DD);
514
Ted Kremenek9aae5132007-08-23 21:42:29 +0000515 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000516 CFGBlock *B = addStmt(Statement);
517
518 if (badCFG)
519 return NULL;
520
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000521 // For C++ constructor add initializers to CFG.
522 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
523 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
524 E = CD->init_rend(); I != E; ++I) {
525 B = addInitializer(*I);
526 if (badCFG)
527 return NULL;
528 }
529 }
530
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000531 if (B)
532 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000533
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000534 // Backpatch the gotos whose label -> block mappings we didn't know when we
535 // encountered them.
536 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
537 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000538
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000539 CFGBlock* B = I->block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000540 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
541 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000542
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000543 // If there is no target for the goto, then we are looking at an
544 // incomplete AST. Handle this by not registering a successor.
545 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000546
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000547 JumpTarget JT = LI->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000548 prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
549 JT.scopePosition);
550 addSuccessor(B, JT.block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000551 }
552
553 // Add successors to the Indirect Goto Dispatch block (if we have one).
554 if (CFGBlock* B = cfg->getIndirectGotoBlock())
555 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
556 E = AddressTakenLabels.end(); I != E; ++I ) {
557
558 // Lookup the target block.
559 LabelMapTy::iterator LI = LabelMap.find(*I);
560
561 // If there is no target block that contains label, then we are looking
562 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000563 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000564
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000565 addSuccessor(B, LI->second.block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000566 }
Mike Stump31feda52009-07-17 01:31:16 +0000567
Mike Stump31feda52009-07-17 01:31:16 +0000568 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000569 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000570
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000571 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000572}
Mike Stump31feda52009-07-17 01:31:16 +0000573
Ted Kremenek9aae5132007-08-23 21:42:29 +0000574/// createBlock - Used to lazily create blocks that are connected
575/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000576CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000577 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000578 if (add_successor && Succ)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000579 addSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000580 return B;
581}
Mike Stump31feda52009-07-17 01:31:16 +0000582
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000583/// addInitializer - Add C++ base or member initializer element to CFG.
Alexis Hunt1d792652011-01-08 20:30:50 +0000584CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000585 if (!BuildOpts.AddInitializers)
586 return Block;
587
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000588 bool IsReference = false;
589 bool HasTemporaries = false;
590
591 // Destructors of temporaries in initialization expression should be called
592 // after initialization finishes.
593 Expr *Init = I->getInit();
594 if (Init) {
Francois Pichetd583da02010-12-04 09:14:42 +0000595 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000596 IsReference = FD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +0000597 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000598
599 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
600 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +0000601 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000602 IsReference);
603 }
604 }
605
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000606 autoCreateBlock();
607 appendInitializer(Block, I);
608
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000609 if (Init) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000610 if (HasTemporaries) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000611 // For expression with temporaries go directly to subexpression to omit
612 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +0000613 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
614 }
615 return Visit(Init);
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000616 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000617
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000618 return Block;
619}
620
Marcin Swiderski5e415732010-09-30 23:05:00 +0000621/// addAutomaticObjDtors - Add to current block automatic objects destructors
622/// for objects in range of local scope positions. Use S as trigger statement
623/// for destructors.
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000624void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
625 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000626 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000627 return;
628
Marcin Swiderski5e415732010-09-30 23:05:00 +0000629 if (B == E)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000630 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000631
632 autoCreateBlock();
633 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000634}
635
Marcin Swiderski20b88732010-10-05 05:37:00 +0000636/// addImplicitDtorsForDestructor - Add implicit destructors generated for
637/// base and member objects in destructor.
638void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
639 assert (BuildOpts.AddImplicitDtors
640 && "Can be called only when dtors should be added");
641 const CXXRecordDecl *RD = DD->getParent();
642
643 // At the end destroy virtual base objects.
644 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
645 VE = RD->vbases_end(); VI != VE; ++VI) {
646 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
647 if (!CD->hasTrivialDestructor()) {
648 autoCreateBlock();
649 appendBaseDtor(Block, VI);
650 }
651 }
652
653 // Before virtual bases destroy direct base objects.
654 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
655 BE = RD->bases_end(); BI != BE; ++BI) {
656 if (!BI->isVirtual()) {
657 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
658 if (!CD->hasTrivialDestructor()) {
659 autoCreateBlock();
660 appendBaseDtor(Block, BI);
661 }
662 }
663 }
664
665 // First destroy member objects.
666 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
667 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski01769902010-10-25 07:05:54 +0000668 // Check for constant size array. Set type to array element type.
669 QualType QT = FI->getType();
670 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
671 if (AT->getSize() == 0)
672 continue;
673 QT = AT->getElementType();
674 }
675
676 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski20b88732010-10-05 05:37:00 +0000677 if (!CD->hasTrivialDestructor()) {
678 autoCreateBlock();
679 appendMemberDtor(Block, *FI);
680 }
681 }
682}
683
Marcin Swiderski5e415732010-09-30 23:05:00 +0000684/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
685/// way return valid LocalScope object.
686LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
687 if (!Scope) {
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000688 llvm::BumpPtrAllocator &alloc = cfg->getAllocator();
689 Scope = alloc.Allocate<LocalScope>();
690 BumpVectorContext ctx(alloc);
691 new (Scope) LocalScope(ctx, ScopePos);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000692 }
693 return Scope;
694}
695
696/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu81714f22010-10-01 03:00:16 +0000697/// that should create implicit scope (e.g. if/else substatements).
698void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000699 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu81714f22010-10-01 03:00:16 +0000700 return;
701
702 LocalScope *Scope = 0;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000703
704 // For compound statement we will be creating explicit scope.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000705 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000706 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
707 ; BI != BE; ++BI) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000708 Stmt *SI = *BI;
709 if (LabelStmt *LS = dyn_cast<LabelStmt>(SI))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000710 SI = LS->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000711 if (DeclStmt *DS = dyn_cast<DeclStmt>(SI))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000712 Scope = addLocalScopeForDeclStmt(DS, Scope);
713 }
Zhongxing Xu81714f22010-10-01 03:00:16 +0000714 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000715 }
716
717 // For any other statement scope will be implicit and as such will be
718 // interesting only for DeclStmt.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000719 if (LabelStmt *LS = dyn_cast<LabelStmt>(S))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000720 S = LS->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000721 if (DeclStmt *DS = dyn_cast<DeclStmt>(S))
Zhongxing Xu307701e2010-10-01 03:09:09 +0000722 addLocalScopeForDeclStmt(DS);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000723}
724
725/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
726/// reuse Scope if not NULL.
727LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000728 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000729 if (!BuildOpts.AddImplicitDtors)
730 return Scope;
731
732 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
733 ; DI != DE; ++DI) {
734 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
735 Scope = addLocalScopeForVarDecl(VD, Scope);
736 }
737 return Scope;
738}
739
740/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
741/// create add scope for automatic objects and temporary objects bound to
742/// const reference. Will reuse Scope if not NULL.
743LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000744 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000745 if (!BuildOpts.AddImplicitDtors)
746 return Scope;
747
748 // Check if variable is local.
749 switch (VD->getStorageClass()) {
750 case SC_None:
751 case SC_Auto:
752 case SC_Register:
753 break;
754 default: return Scope;
755 }
756
757 // Check for const references bound to temporary. Set type to pointee.
758 QualType QT = VD->getType();
759 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
760 QT = RT->getPointeeType();
761 if (!QT.isConstQualified())
762 return Scope;
763 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
764 return Scope;
765 }
766
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000767 // Check for constant size array. Set type to array element type.
768 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
769 if (AT->getSize() == 0)
770 return Scope;
771 QT = AT->getElementType();
772 }
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000773
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000774 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000775 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
776 if (!CD->hasTrivialDestructor()) {
777 // Add the variable to scope
778 Scope = createOrReuseLocalScope(Scope);
779 Scope->addVar(VD);
780 ScopePos = Scope->begin();
781 }
Marcin Swiderski5e415732010-09-30 23:05:00 +0000782 return Scope;
783}
784
785/// addLocalScopeAndDtors - For given statement add local scope for it and
786/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
787void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
788 if (!BuildOpts.AddImplicitDtors)
789 return;
790
791 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu81714f22010-10-01 03:00:16 +0000792 addLocalScopeForStmt(S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000793 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
794}
795
Marcin Swiderski321a7072010-09-30 22:54:37 +0000796/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
797/// automatic storage duration to CFGBlock's elements vector. Insertion will be
798/// performed in place specified with iterator.
799void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
800 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
801 BumpVectorContext& C = cfg->getBumpVectorContext();
802 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
803 while (B != E)
804 I = Blk->insertAutomaticObjDtor(I, *B++, S);
805}
806
807/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
808/// automatic storage duration to CFGBlock's elements vector. Elements will be
809/// appended to physical end of the vector which happens to be logical
810/// beginning.
811void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
812 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
813 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
814}
815
816/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
817/// variables with automatic storage duration to CFGBlock's elements vector.
818/// Elements will be prepended to physical beginning of the vector which
819/// happens to be logical end. Use blocks terminator as statement that specifies
820/// destructors call site.
821void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
822 LocalScope::const_iterator B, LocalScope::const_iterator E) {
823 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
824}
825
Ted Kremenek93668002009-07-17 22:18:43 +0000826/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000827/// blocks for ternary operators, &&, and ||. We also process "," and
828/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000829CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000830tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000831 if (!S) {
832 badCFG = true;
833 return 0;
834 }
Ted Kremenek93668002009-07-17 22:18:43 +0000835 switch (S->getStmtClass()) {
836 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000837 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000838
839 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000840 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000841
John McCallc07a0c72011-02-17 10:25:35 +0000842 case Stmt::BinaryConditionalOperatorClass:
843 return VisitConditionalOperator(cast<BinaryConditionalOperator>(S), asc);
844
Ted Kremenek93668002009-07-17 22:18:43 +0000845 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000846 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000847
Ted Kremenek93668002009-07-17 22:18:43 +0000848 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000849 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000850
Ted Kremenek93668002009-07-17 22:18:43 +0000851 case Stmt::BreakStmtClass:
852 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000853
Ted Kremenek93668002009-07-17 22:18:43 +0000854 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000855 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000856 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000857
Ted Kremenek93668002009-07-17 22:18:43 +0000858 case Stmt::CaseStmtClass:
859 return VisitCaseStmt(cast<CaseStmt>(S));
860
861 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000862 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000863
Ted Kremenek93668002009-07-17 22:18:43 +0000864 case Stmt::CompoundStmtClass:
865 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000866
Ted Kremenek93668002009-07-17 22:18:43 +0000867 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000868 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000869
Ted Kremenek93668002009-07-17 22:18:43 +0000870 case Stmt::ContinueStmtClass:
871 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000872
Ted Kremenekb27378c2010-01-19 20:40:33 +0000873 case Stmt::CXXCatchStmtClass:
874 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
875
John McCall5d413782010-12-06 08:20:24 +0000876 case Stmt::ExprWithCleanupsClass:
877 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000878
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000879 case Stmt::CXXBindTemporaryExprClass:
880 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
881
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000882 case Stmt::CXXConstructExprClass:
883 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
884
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000885 case Stmt::CXXFunctionalCastExprClass:
886 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
887
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000888 case Stmt::CXXTemporaryObjectExprClass:
889 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
890
Zhongxing Xu7e612172010-04-13 09:38:01 +0000891 case Stmt::CXXMemberCallExprClass:
892 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
893
Ted Kremenekb27378c2010-01-19 20:40:33 +0000894 case Stmt::CXXThrowExprClass:
895 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000896
Ted Kremenekb27378c2010-01-19 20:40:33 +0000897 case Stmt::CXXTryStmtClass:
898 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000899
Ted Kremenek93668002009-07-17 22:18:43 +0000900 case Stmt::DeclStmtClass:
901 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000902
Ted Kremenek93668002009-07-17 22:18:43 +0000903 case Stmt::DefaultStmtClass:
904 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000905
Ted Kremenek93668002009-07-17 22:18:43 +0000906 case Stmt::DoStmtClass:
907 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000908
Ted Kremenek93668002009-07-17 22:18:43 +0000909 case Stmt::ForStmtClass:
910 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000911
Ted Kremenek93668002009-07-17 22:18:43 +0000912 case Stmt::GotoStmtClass:
913 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000914
Ted Kremenek93668002009-07-17 22:18:43 +0000915 case Stmt::IfStmtClass:
916 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000917
Ted Kremenek8219b822010-12-16 07:46:53 +0000918 case Stmt::ImplicitCastExprClass:
919 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000920
Ted Kremenek93668002009-07-17 22:18:43 +0000921 case Stmt::IndirectGotoStmtClass:
922 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000923
Ted Kremenek93668002009-07-17 22:18:43 +0000924 case Stmt::LabelStmtClass:
925 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000926
Ted Kremenek5868ec62010-04-11 17:02:10 +0000927 case Stmt::MemberExprClass:
928 return VisitMemberExpr(cast<MemberExpr>(S), asc);
929
Ted Kremenek93668002009-07-17 22:18:43 +0000930 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000931 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
932
Ted Kremenek93668002009-07-17 22:18:43 +0000933 case Stmt::ObjCAtSynchronizedStmtClass:
934 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000935
Ted Kremenek93668002009-07-17 22:18:43 +0000936 case Stmt::ObjCAtThrowStmtClass:
937 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000938
Ted Kremenek93668002009-07-17 22:18:43 +0000939 case Stmt::ObjCAtTryStmtClass:
940 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000941
Ted Kremenek93668002009-07-17 22:18:43 +0000942 case Stmt::ObjCForCollectionStmtClass:
943 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000944
Ted Kremenek93668002009-07-17 22:18:43 +0000945 case Stmt::ParenExprClass:
946 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000947 goto tryAgain;
948
Ted Kremenek93668002009-07-17 22:18:43 +0000949 case Stmt::NullStmtClass:
950 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000951
Ted Kremenek93668002009-07-17 22:18:43 +0000952 case Stmt::ReturnStmtClass:
953 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000954
Peter Collingbournee190dee2011-03-11 19:24:49 +0000955 case Stmt::UnaryExprOrTypeTraitExprClass:
956 return VisitUnaryExprOrTypeTraitExpr(cast<UnaryExprOrTypeTraitExpr>(S),
957 asc);
Mike Stump11289f42009-09-09 15:08:12 +0000958
Ted Kremenek93668002009-07-17 22:18:43 +0000959 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000960 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000961
Ted Kremenek93668002009-07-17 22:18:43 +0000962 case Stmt::SwitchStmtClass:
963 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000964
Zhanyong Wan6dace612010-11-22 08:45:56 +0000965 case Stmt::UnaryOperatorClass:
966 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
967
Ted Kremenek93668002009-07-17 22:18:43 +0000968 case Stmt::WhileStmtClass:
969 return VisitWhileStmt(cast<WhileStmt>(S));
970 }
971}
Mike Stump11289f42009-09-09 15:08:12 +0000972
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000973CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
Ted Kremenek7c58d352011-03-10 01:14:11 +0000974 if (asc.alwaysAdd(*this, S)) {
Ted Kremenek93668002009-07-17 22:18:43 +0000975 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +0000976 appendStmt(Block, S);
Mike Stump31feda52009-07-17 01:31:16 +0000977 }
Mike Stump11289f42009-09-09 15:08:12 +0000978
Ted Kremenek93668002009-07-17 22:18:43 +0000979 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000980}
Mike Stump31feda52009-07-17 01:31:16 +0000981
Ted Kremenek93668002009-07-17 22:18:43 +0000982/// VisitChildren - Visit the children of a Stmt.
983CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
Ted Kremenek828f6312011-02-21 22:11:26 +0000984 CFGBlock *lastBlock = Block;
985 for (Stmt::child_range I = Terminator->children(); I; ++I)
986 if (Stmt *child = *I)
987 if (CFGBlock *b = Visit(child))
988 lastBlock = b;
989
990 return lastBlock;
Ted Kremenek9e248872007-08-27 21:27:44 +0000991}
Mike Stump11289f42009-09-09 15:08:12 +0000992
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000993CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
994 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000995 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000996
Ted Kremenek7c58d352011-03-10 01:14:11 +0000997 if (asc.alwaysAdd(*this, A)) {
Ted Kremenek93668002009-07-17 22:18:43 +0000998 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +0000999 appendStmt(Block, A);
Ted Kremenek93668002009-07-17 22:18:43 +00001000 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001001
Ted Kremenek9aae5132007-08-23 21:42:29 +00001002 return Block;
1003}
Mike Stump11289f42009-09-09 15:08:12 +00001004
Zhanyong Wan6dace612010-11-22 08:45:56 +00001005CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek8219b822010-12-16 07:46:53 +00001006 AddStmtChoice asc) {
Ted Kremenek7c58d352011-03-10 01:14:11 +00001007 if (asc.alwaysAdd(*this, U)) {
Zhanyong Wan6dace612010-11-22 08:45:56 +00001008 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001009 appendStmt(Block, U);
Zhanyong Wan6dace612010-11-22 08:45:56 +00001010 }
1011
Ted Kremenek8219b822010-12-16 07:46:53 +00001012 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan6dace612010-11-22 08:45:56 +00001013}
1014
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001015CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
1016 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +00001017 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +00001018 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001019 appendStmt(ConfluenceBlock, B);
Mike Stump11289f42009-09-09 15:08:12 +00001020
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001021 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001022 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001023
Ted Kremenek93668002009-07-17 22:18:43 +00001024 // create the block evaluating the LHS
1025 CFGBlock* LHSBlock = createBlock(false);
1026 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +00001027
Ted Kremenek93668002009-07-17 22:18:43 +00001028 // create the block evaluating the RHS
1029 Succ = ConfluenceBlock;
1030 Block = NULL;
1031 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +00001032
1033 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001034 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +00001035 return 0;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001036 } else {
Ted Kremenek989da5e2010-04-29 01:10:26 +00001037 // Create an empty block for cases where the RHS doesn't require
1038 // any explicit statements in the CFG.
1039 RHSBlock = createBlock();
1040 }
Mike Stump11289f42009-09-09 15:08:12 +00001041
Mike Stump773582d2009-07-23 23:25:26 +00001042 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001043 TryResult KnownVal = tryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +00001044 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +00001045 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +00001046
Ted Kremenek93668002009-07-17 22:18:43 +00001047 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +00001048 if (B->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001049 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
1050 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001051 } else {
John McCalle3027922010-08-25 11:45:40 +00001052 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001053 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
1054 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001055 }
Mike Stump11289f42009-09-09 15:08:12 +00001056
Ted Kremenek93668002009-07-17 22:18:43 +00001057 // Generate the blocks for evaluating the LHS.
1058 Block = LHSBlock;
1059 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +00001060 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001061
1062 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +00001063 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001064 appendStmt(Block, B);
Ted Kremenek93668002009-07-17 22:18:43 +00001065 addStmt(B->getRHS());
1066 return addStmt(B->getLHS());
1067 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001068
1069 if (B->isAssignmentOp()) {
Ted Kremenek7c58d352011-03-10 01:14:11 +00001070 if (asc.alwaysAdd(*this, B)) {
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001071 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001072 appendStmt(Block, B);
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001073 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001074 Visit(B->getLHS());
Marcin Swiderski77232492010-10-24 08:21:40 +00001075 return Visit(B->getRHS());
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001076 }
Mike Stump11289f42009-09-09 15:08:12 +00001077
Ted Kremenek7c58d352011-03-10 01:14:11 +00001078 if (asc.alwaysAdd(*this, B)) {
Marcin Swiderski77232492010-10-24 08:21:40 +00001079 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001080 appendStmt(Block, B);
Marcin Swiderski77232492010-10-24 08:21:40 +00001081 }
1082
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001083 CFGBlock *RBlock = Visit(B->getRHS());
1084 CFGBlock *LBlock = Visit(B->getLHS());
1085 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1086 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1087 // return RBlock. Otherwise we'll incorrectly return NULL.
1088 return (LBlock ? LBlock : RBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001089}
1090
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001091CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
Ted Kremenek7c58d352011-03-10 01:14:11 +00001092 if (asc.alwaysAdd(*this, E)) {
Ted Kremenek470bfa42009-11-25 01:34:30 +00001093 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001094 appendStmt(Block, E);
Ted Kremenek470bfa42009-11-25 01:34:30 +00001095 }
1096 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001097}
1098
Ted Kremenek93668002009-07-17 22:18:43 +00001099CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1100 // "break" is a control-flow statement. Thus we stop processing the current
1101 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001102 if (badCFG)
1103 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001104
Ted Kremenek93668002009-07-17 22:18:43 +00001105 // Now create a new block that ends with the break statement.
1106 Block = createBlock(false);
1107 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +00001108
Ted Kremenek93668002009-07-17 22:18:43 +00001109 // If there is no target for the break, then we are looking at an incomplete
1110 // AST. This means that the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001111 if (BreakJumpTarget.block) {
1112 addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B);
1113 addSuccessor(Block, BreakJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001114 } else
Ted Kremenek93668002009-07-17 22:18:43 +00001115 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +00001116
1117
Ted Kremenek9aae5132007-08-23 21:42:29 +00001118 return Block;
1119}
Mike Stump11289f42009-09-09 15:08:12 +00001120
Mike Stump04c68512010-01-21 15:20:48 +00001121static bool CanThrow(Expr *E) {
1122 QualType Ty = E->getType();
1123 if (Ty->isFunctionPointerType())
1124 Ty = Ty->getAs<PointerType>()->getPointeeType();
1125 else if (Ty->isBlockPointerType())
1126 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001127
Mike Stump04c68512010-01-21 15:20:48 +00001128 const FunctionType *FT = Ty->getAs<FunctionType>();
1129 if (FT) {
1130 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
Sebastian Redlfa453cf2011-03-12 11:50:43 +00001131 if (Proto->isNothrow())
Mike Stump04c68512010-01-21 15:20:48 +00001132 return false;
1133 }
1134 return true;
1135}
1136
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001137CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +00001138 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +00001139 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001140 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001141 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +00001142 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001143
Mike Stump04c68512010-01-21 15:20:48 +00001144 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001145
1146 // Languages without exceptions are assumed to not throw.
Anders Carlsson6dc07d42011-02-28 00:33:03 +00001147 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001148 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +00001149 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +00001150 }
1151
1152 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001153 if (FD->hasAttr<NoReturnAttr>())
1154 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +00001155 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +00001156 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001157 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001158
Mike Stump04c68512010-01-21 15:20:48 +00001159 if (!CanThrow(C->getCallee()))
1160 AddEHEdge = false;
1161
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001162 if (!NoReturn && !AddEHEdge)
1163 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump11289f42009-09-09 15:08:12 +00001164
Mike Stump92244b02010-01-19 22:00:14 +00001165 if (Block) {
1166 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001167 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +00001168 return 0;
1169 }
Mike Stump11289f42009-09-09 15:08:12 +00001170
Mike Stump92244b02010-01-19 22:00:14 +00001171 Block = createBlock(!NoReturn);
Ted Kremenek2866bab2011-03-10 01:14:08 +00001172 appendStmt(Block, C);
Mike Stump8c5d7992009-07-25 21:26:53 +00001173
Mike Stump92244b02010-01-19 22:00:14 +00001174 if (NoReturn) {
1175 // Wire this to the exit block directly.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001176 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001177 }
Mike Stump04c68512010-01-21 15:20:48 +00001178 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +00001179 // Add exceptional edges.
1180 if (TryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001181 addSuccessor(Block, TryTerminatedBlock);
Mike Stump92244b02010-01-19 22:00:14 +00001182 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001183 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001184 }
Mike Stump11289f42009-09-09 15:08:12 +00001185
Mike Stump8c5d7992009-07-25 21:26:53 +00001186 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +00001187}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001188
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001189CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1190 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +00001191 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001192 appendStmt(ConfluenceBlock, C);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001193 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001194 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001195
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001196 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek21822592009-07-17 18:20:32 +00001197 Succ = ConfluenceBlock;
1198 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001199 CFGBlock* LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001200 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001201 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001202
Ted Kremenek21822592009-07-17 18:20:32 +00001203 Succ = ConfluenceBlock;
1204 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001205 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001206 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001207 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001208
Ted Kremenek21822592009-07-17 18:20:32 +00001209 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +00001210 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001211 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
1212 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1213 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +00001214 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +00001215 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +00001216}
Mike Stump11289f42009-09-09 15:08:12 +00001217
1218
1219CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001220 addLocalScopeAndDtors(C);
Mike Stump11289f42009-09-09 15:08:12 +00001221 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001222
1223 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1224 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +00001225 // If we hit a segment of code just containing ';' (NullStmts), we can
1226 // get a null block back. In such cases, just use the LastBlock
1227 if (CFGBlock *newBlock = addStmt(*I))
1228 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001229
Ted Kremenekce499c22009-08-27 23:16:26 +00001230 if (badCFG)
1231 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001232 }
Mike Stump92244b02010-01-19 22:00:14 +00001233
Ted Kremenek93668002009-07-17 22:18:43 +00001234 return LastBlock;
1235}
Mike Stump11289f42009-09-09 15:08:12 +00001236
John McCallc07a0c72011-02-17 10:25:35 +00001237CFGBlock *CFGBuilder::VisitConditionalOperator(AbstractConditionalOperator *C,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001238 AddStmtChoice asc) {
John McCallc07a0c72011-02-17 10:25:35 +00001239 const BinaryConditionalOperator *BCO = dyn_cast<BinaryConditionalOperator>(C);
1240 const OpaqueValueExpr *opaqueValue = (BCO ? BCO->getOpaqueValue() : NULL);
1241
Ted Kremenek51d40b02009-07-17 18:15:54 +00001242 // Create the confluence block that will "merge" the results of the ternary
1243 // expression.
1244 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001245 appendStmt(ConfluenceBlock, C);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001246 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001247 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001248
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001249 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001250
Ted Kremenek51d40b02009-07-17 18:15:54 +00001251 // Create a block for the LHS expression if there is an LHS expression. A
1252 // GCC extension allows LHS to be NULL, causing the condition to be the
1253 // value that is returned instead.
1254 // e.g: x ?: y is shorthand for: x ? x : y;
1255 Succ = ConfluenceBlock;
1256 Block = NULL;
John McCallc07a0c72011-02-17 10:25:35 +00001257 CFGBlock* LHSBlock = 0;
1258 const Expr *trueExpr = C->getTrueExpr();
1259 if (trueExpr != opaqueValue) {
1260 LHSBlock = Visit(C->getTrueExpr(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001261 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001262 return 0;
1263 Block = NULL;
1264 }
Ted Kremenekd8138012011-02-24 03:09:15 +00001265 else
1266 LHSBlock = ConfluenceBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001267
Ted Kremenek51d40b02009-07-17 18:15:54 +00001268 // Create the block for the RHS expression.
1269 Succ = ConfluenceBlock;
John McCallc07a0c72011-02-17 10:25:35 +00001270 CFGBlock* RHSBlock = Visit(C->getFalseExpr(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001271 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001272 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001273
Ted Kremenek51d40b02009-07-17 18:15:54 +00001274 // Create the block that will contain the condition.
1275 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +00001276
Mike Stump773582d2009-07-23 23:25:26 +00001277 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001278 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
Ted Kremenekd8138012011-02-24 03:09:15 +00001279 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001280 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001281 Block->setTerminator(C);
John McCallc07a0c72011-02-17 10:25:35 +00001282 Expr *condExpr = C->getCond();
John McCall68cc3352011-02-19 03:13:26 +00001283
Ted Kremenekd8138012011-02-24 03:09:15 +00001284 if (opaqueValue) {
1285 // Run the condition expression if it's not trivially expressed in
1286 // terms of the opaque value (or if there is no opaque value).
1287 if (condExpr != opaqueValue)
1288 addStmt(condExpr);
John McCall68cc3352011-02-19 03:13:26 +00001289
Ted Kremenekd8138012011-02-24 03:09:15 +00001290 // Before that, run the common subexpression if there was one.
1291 // At least one of this or the above will be run.
1292 return addStmt(BCO->getCommon());
1293 }
1294
1295 return addStmt(condExpr);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001296}
1297
Ted Kremenek93668002009-07-17 22:18:43 +00001298CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001299 if (DS->isSingleDecl())
1300 return VisitDeclSubExpr(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001301
Ted Kremenek93668002009-07-17 22:18:43 +00001302 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001303
Ted Kremenek93668002009-07-17 22:18:43 +00001304 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1305 typedef llvm::SmallVector<Decl*,10> BufTy;
1306 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +00001307
Ted Kremenek93668002009-07-17 22:18:43 +00001308 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1309 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1310 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1311 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +00001312
Ted Kremenek93668002009-07-17 22:18:43 +00001313 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1314 // automatically freed with the CFG.
1315 DeclGroupRef DG(*I);
1316 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001317 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +00001318 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +00001319
Ted Kremenek93668002009-07-17 22:18:43 +00001320 // Append the fake DeclStmt to block.
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001321 B = VisitDeclSubExpr(DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +00001322 }
Mike Stump11289f42009-09-09 15:08:12 +00001323
1324 return B;
Ted Kremenek93668002009-07-17 22:18:43 +00001325}
Mike Stump11289f42009-09-09 15:08:12 +00001326
Ted Kremenek93668002009-07-17 22:18:43 +00001327/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001328/// DeclStmts and initializers in them.
1329CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1330 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekf6998822008-02-26 00:22:58 +00001331
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001332 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001333
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001334 if (!VD) {
1335 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001336 appendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +00001337 return Block;
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001338 }
Mike Stump11289f42009-09-09 15:08:12 +00001339
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001340 bool IsReference = false;
1341 bool HasTemporaries = false;
1342
1343 // Destructors of temporaries in initialization expression should be called
1344 // after initialization finishes.
Ted Kremenek93668002009-07-17 22:18:43 +00001345 Expr *Init = VD->getInit();
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001346 if (Init) {
1347 IsReference = VD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +00001348 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001349
1350 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1351 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +00001352 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001353 IsReference);
1354 }
1355 }
1356
1357 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001358 appendStmt(Block, DS);
Mike Stump11289f42009-09-09 15:08:12 +00001359
Ted Kremenek93668002009-07-17 22:18:43 +00001360 if (Init) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001361 if (HasTemporaries)
1362 // For expression with temporaries go directly to subexpression to omit
1363 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +00001364 Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001365 else
Ted Kremenek8219b822010-12-16 07:46:53 +00001366 Visit(Init);
Ted Kremenek93668002009-07-17 22:18:43 +00001367 }
Mike Stump11289f42009-09-09 15:08:12 +00001368
Ted Kremenek93668002009-07-17 22:18:43 +00001369 // If the type of VD is a VLA, then we must process its size expressions.
John McCall424cec92011-01-19 06:33:43 +00001370 for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr());
1371 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek93668002009-07-17 22:18:43 +00001372 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001373
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001374 // Remove variable from local scope.
1375 if (ScopePos && VD == *ScopePos)
1376 ++ScopePos;
1377
Ted Kremenek93668002009-07-17 22:18:43 +00001378 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001379}
1380
1381CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001382 // We may see an if statement in the middle of a basic block, or it may be the
1383 // first statement we are processing. In either case, we create a new basic
1384 // block. First, we create the blocks for the then...else statements, and
1385 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +00001386 // middle of a block, we stop processing that block. That block is then the
1387 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +00001388
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001389 // Save local scope position because in case of condition variable ScopePos
1390 // won't be restored when traversing AST.
1391 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1392
1393 // Create local scope for possible condition variable.
1394 // Store scope position. Add implicit destructor.
1395 if (VarDecl* VD = I->getConditionVariable()) {
1396 LocalScope::const_iterator BeginScopePos = ScopePos;
1397 addLocalScopeForVarDecl(VD);
1398 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1399 }
1400
Mike Stump31feda52009-07-17 01:31:16 +00001401 // The block we were proccessing is now finished. Make it the successor
1402 // block.
1403 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001404 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001405 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001406 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001407 }
Mike Stump31feda52009-07-17 01:31:16 +00001408
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001409 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001410 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001411
Ted Kremenek9aae5132007-08-23 21:42:29 +00001412 if (Stmt* Else = I->getElse()) {
1413 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001414
Ted Kremenek9aae5132007-08-23 21:42:29 +00001415 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +00001416 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001417 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001418
1419 // If branch is not a compound statement create implicit scope
1420 // and add destructors.
1421 if (!isa<CompoundStmt>(Else))
1422 addLocalScopeAndDtors(Else);
1423
Ted Kremenek93668002009-07-17 22:18:43 +00001424 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +00001425
Ted Kremenekbbad8ce2007-08-30 18:13:31 +00001426 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1427 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +00001428 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001429 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001430 return 0;
1431 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001432 }
Mike Stump31feda52009-07-17 01:31:16 +00001433
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001434 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001435 CFGBlock* ThenBlock;
1436 {
1437 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001438 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001439 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001440 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001441
1442 // If branch is not a compound statement create implicit scope
1443 // and add destructors.
1444 if (!isa<CompoundStmt>(Then))
1445 addLocalScopeAndDtors(Then);
1446
Ted Kremenek93668002009-07-17 22:18:43 +00001447 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +00001448
Ted Kremenek1b379512009-04-01 03:52:47 +00001449 if (!ThenBlock) {
1450 // We can reach here if the "then" body has all NullStmts.
1451 // Create an empty block so we can distinguish between true and false
1452 // branches in path-sensitive analyses.
1453 ThenBlock = createBlock(false);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001454 addSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001455 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001456 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001457 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001458 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001459 }
1460
Mike Stump31feda52009-07-17 01:31:16 +00001461 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001462 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001463
Ted Kremenek9aae5132007-08-23 21:42:29 +00001464 // Set the terminator of the new block to the If statement.
1465 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001466
Mike Stump773582d2009-07-23 23:25:26 +00001467 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001468 const TryResult &KnownVal = tryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001469
Ted Kremenek9aae5132007-08-23 21:42:29 +00001470 // Now add the successors.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001471 addSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1472 addSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001473
1474 // Add the condition as the last statement in the new block. This may create
1475 // new blocks as the condition may contain control-flow. Any newly created
1476 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001477 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001478
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001479 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1480 // and the condition variable initialization to the CFG.
1481 if (VarDecl *VD = I->getConditionVariable()) {
1482 if (Expr *Init = VD->getInit()) {
1483 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001484 appendStmt(Block, I);
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001485 addStmt(Init);
1486 }
1487 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001488
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001489 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001490}
Mike Stump31feda52009-07-17 01:31:16 +00001491
1492
Ted Kremenek9aae5132007-08-23 21:42:29 +00001493CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001494 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001495 //
Mike Stump31feda52009-07-17 01:31:16 +00001496 // NOTE: If a "return" appears in the middle of a block, this means that the
1497 // code afterwards is DEAD (unreachable). We still keep a basic block
1498 // for that code; a simple "mark-and-sweep" from the entry block will be
1499 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001500
1501 // Create the new block.
1502 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001503
Ted Kremenek9aae5132007-08-23 21:42:29 +00001504 // The Exit block is the only successor.
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001505 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001506 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001507
1508 // Add the return statement to the block. This may create new blocks if R
1509 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001510 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001511}
1512
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001513CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt *L) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001514 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001515 addStmt(L->getSubStmt());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001516 CFGBlock *LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001517
Ted Kremenek93668002009-07-17 22:18:43 +00001518 if (!LabelBlock) // This can happen when the body is empty, i.e.
1519 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001520
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001521 assert(LabelMap.find(L->getDecl()) == LabelMap.end() &&
1522 "label already in map");
1523 LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001524
1525 // Labels partition blocks, so this is the end of the basic block we were
1526 // processing (L is the block's label). Because this is label (and we have
1527 // already processed the substatement) there is no extra control-flow to worry
1528 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001529 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001530 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001531 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001532
1533 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001534 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001535
Ted Kremenek9aae5132007-08-23 21:42:29 +00001536 // This block is now the implicit successor of other blocks.
1537 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001538
Ted Kremenek9aae5132007-08-23 21:42:29 +00001539 return LabelBlock;
1540}
1541
1542CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001543 // Goto is a control-flow statement. Thus we stop processing the current
1544 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001545
Ted Kremenek9aae5132007-08-23 21:42:29 +00001546 Block = createBlock(false);
1547 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001548
1549 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001550 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001551
Ted Kremenek9aae5132007-08-23 21:42:29 +00001552 if (I == LabelMap.end())
1553 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001554 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1555 else {
1556 JumpTarget JT = I->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001557 addAutomaticObjDtors(ScopePos, JT.scopePosition, G);
1558 addSuccessor(Block, JT.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001559 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001560
Mike Stump31feda52009-07-17 01:31:16 +00001561 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001562}
1563
1564CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001565 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001566
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001567 // Save local scope position because in case of condition variable ScopePos
1568 // won't be restored when traversing AST.
1569 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1570
1571 // Create local scope for init statement and possible condition variable.
1572 // Add destructor for init statement and condition variable.
1573 // Store scope position for continue statement.
1574 if (Stmt* Init = F->getInit())
1575 addLocalScopeForStmt(Init);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001576 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1577
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001578 if (VarDecl* VD = F->getConditionVariable())
1579 addLocalScopeForVarDecl(VD);
1580 LocalScope::const_iterator ContinueScopePos = ScopePos;
1581
1582 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1583
Mike Stump014b3ea2009-07-21 01:12:51 +00001584 // "for" is a control-flow statement. Thus we stop processing the current
1585 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001586 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001587 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001588 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001589 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001590 } else
1591 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001592
Ted Kremenek304a9532010-05-21 20:30:15 +00001593 // Save the current value for the break targets.
1594 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001595 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001596 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001597
Mike Stump31feda52009-07-17 01:31:16 +00001598 // Because of short-circuit evaluation, the condition of the loop can span
1599 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1600 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001601 CFGBlock* ExitConditionBlock = createBlock(false);
1602 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001603
Ted Kremenek81e14852007-08-27 19:46:09 +00001604 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001605 ExitConditionBlock->setTerminator(F);
1606
1607 // Now add the actual condition to the condition block. Because the condition
1608 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001609 if (Stmt* C = F->getCond()) {
1610 Block = ExitConditionBlock;
1611 EntryConditionBlock = addStmt(C);
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001612 if (badCFG)
1613 return 0;
Ted Kremenek7b31a612010-09-15 07:01:20 +00001614 assert(Block == EntryConditionBlock ||
1615 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001616
1617 // If this block contains a condition variable, add both the condition
1618 // variable and initializer to the CFG.
1619 if (VarDecl *VD = F->getConditionVariable()) {
1620 if (Expr *Init = VD->getInit()) {
1621 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001622 appendStmt(Block, F);
Ted Kremenekec92f942009-12-24 01:49:06 +00001623 EntryConditionBlock = addStmt(Init);
1624 assert(Block == EntryConditionBlock);
1625 }
1626 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001627
Ted Kremenek55957a82009-05-02 00:13:27 +00001628 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001629 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001630 return 0;
1631 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001632 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001633
Mike Stump31feda52009-07-17 01:31:16 +00001634 // The condition block is the implicit successor for the loop body as well as
1635 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001636 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001637
Mike Stump773582d2009-07-23 23:25:26 +00001638 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001639 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001640
Mike Stump773582d2009-07-23 23:25:26 +00001641 if (F->getCond())
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001642 KnownVal = tryEvaluateBool(F->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001643
Ted Kremenek9aae5132007-08-23 21:42:29 +00001644 // Now create the loop body.
1645 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001646 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001647
Ted Kremenek304a9532010-05-21 20:30:15 +00001648 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001649 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1650 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001651
Ted Kremeneke9610502007-08-30 18:39:40 +00001652 // Create a new block to contain the (bottom) of the loop body.
1653 Block = NULL;
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001654
1655 // Loop body should end with destructor of Condition variable (if any).
1656 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump31feda52009-07-17 01:31:16 +00001657
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001658 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001659 // Generate increment code in its own basic block. This is the target of
1660 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001661 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001662 } else {
1663 // No increment code. Create a special, empty, block that is used as the
1664 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001665 assert(Succ == EntryConditionBlock);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001666 Succ = Block ? Block : createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001667 }
Mike Stump31feda52009-07-17 01:31:16 +00001668
Ted Kremenek902393b2009-04-28 00:51:56 +00001669 // Finish up the increment (or empty) block if it hasn't been already.
1670 if (Block) {
1671 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001672 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001673 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001674 Block = 0;
1675 }
Mike Stump31feda52009-07-17 01:31:16 +00001676
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001677 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001678
Ted Kremenek902393b2009-04-28 00:51:56 +00001679 // The starting block for the loop increment is the block that should
1680 // represent the 'loop target' for looping back to the start of the loop.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001681 ContinueJumpTarget.block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001682
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001683 // If body is not a compound statement create implicit scope
1684 // and add destructors.
1685 if (!isa<CompoundStmt>(F->getBody()))
1686 addLocalScopeAndDtors(F->getBody());
1687
Mike Stump31feda52009-07-17 01:31:16 +00001688 // Now populate the body block, and in the process create new blocks as we
1689 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001690 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001691
1692 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001693 BodyBlock = ContinueJumpTarget.block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001694 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001695 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001696
Ted Kremenek30754282009-07-24 04:47:11 +00001697 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001698 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001699 }
Mike Stump31feda52009-07-17 01:31:16 +00001700
Ted Kremenek30754282009-07-24 04:47:11 +00001701 // Link up the condition block with the code that follows the loop. (the
1702 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001703 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001704
Ted Kremenek9aae5132007-08-23 21:42:29 +00001705 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001706 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001707 if (Stmt* I = F->getInit()) {
1708 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001709 return addStmt(I);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001710 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001711
1712 // There is no loop initialization. We are thus basically a while loop.
1713 // NULL out Block to force lazy block construction.
1714 Block = NULL;
1715 Succ = EntryConditionBlock;
1716 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001717}
1718
Ted Kremenek5868ec62010-04-11 17:02:10 +00001719CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
Ted Kremenek7c58d352011-03-10 01:14:11 +00001720 if (asc.alwaysAdd(*this, M)) {
Ted Kremenek5868ec62010-04-11 17:02:10 +00001721 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001722 appendStmt(Block, M);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001723 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001724 return Visit(M->getBase());
Ted Kremenek5868ec62010-04-11 17:02:10 +00001725}
1726
Ted Kremenek9d56e642008-11-11 17:10:00 +00001727CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1728 // Objective-C fast enumeration 'for' statements:
1729 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1730 //
1731 // for ( Type newVariable in collection_expression ) { statements }
1732 //
1733 // becomes:
1734 //
1735 // prologue:
1736 // 1. collection_expression
1737 // T. jump to loop_entry
1738 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001739 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001740 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1741 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1742 // TB:
1743 // statements
1744 // T. jump to loop_entry
1745 // FB:
1746 // what comes after
1747 //
1748 // and
1749 //
1750 // Type existingItem;
1751 // for ( existingItem in expression ) { statements }
1752 //
1753 // becomes:
1754 //
Mike Stump31feda52009-07-17 01:31:16 +00001755 // the same with newVariable replaced with existingItem; the binding works
1756 // the same except that for one ObjCForCollectionStmt::getElement() returns
1757 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001758 //
Mike Stump31feda52009-07-17 01:31:16 +00001759
Ted Kremenek9d56e642008-11-11 17:10:00 +00001760 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001761
Ted Kremenek9d56e642008-11-11 17:10:00 +00001762 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001763 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001764 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001765 LoopSuccessor = Block;
1766 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001767 } else
1768 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001769
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001770 // Build the condition blocks.
1771 CFGBlock* ExitConditionBlock = createBlock(false);
1772 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001773
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001774 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001775 ExitConditionBlock->setTerminator(S);
1776
1777 // The last statement in the block should be the ObjCForCollectionStmt, which
1778 // performs the actual binding to 'element' and determines if there are any
1779 // more items in the collection.
Ted Kremenek8219b822010-12-16 07:46:53 +00001780 appendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001781 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001782
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001783 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001784 // generate new blocks as necesary. We DON'T add the statement by default to
1785 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001786 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001787 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001788 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001789 return 0;
1790 Block = 0;
1791 }
Mike Stump31feda52009-07-17 01:31:16 +00001792
1793 // The condition block is the implicit successor for the loop body as well as
1794 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001795 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001796
Ted Kremenek9d56e642008-11-11 17:10:00 +00001797 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001798 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001799 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001800 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1801 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1802 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001803
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001804 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1805 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001806
Ted Kremenek93668002009-07-17 22:18:43 +00001807 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001808
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001809 if (!BodyBlock)
1810 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001811 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001812 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001813 return 0;
1814 }
Mike Stump31feda52009-07-17 01:31:16 +00001815
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001816 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001817 addSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001818 }
Mike Stump31feda52009-07-17 01:31:16 +00001819
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001820 // Link up the condition block with the code that follows the loop.
1821 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001822 addSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001823
Ted Kremenek9d56e642008-11-11 17:10:00 +00001824 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001825 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001826 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001827}
1828
Ted Kremenek49805452009-05-02 01:49:13 +00001829CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1830 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001831
Ted Kremenek49805452009-05-02 01:49:13 +00001832 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001833 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001834
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001835 // The sync body starts its own basic block. This makes it a little easier
1836 // for diagnostic clients.
1837 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001838 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001839 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001840
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001841 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001842 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001843 }
Mike Stump31feda52009-07-17 01:31:16 +00001844
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001845 // Add the @synchronized to the CFG.
1846 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001847 appendStmt(Block, S);
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001848
Ted Kremenek49805452009-05-02 01:49:13 +00001849 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001850 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001851}
Mike Stump31feda52009-07-17 01:31:16 +00001852
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001853CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001854 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001855 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001856}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001857
Ted Kremenek9aae5132007-08-23 21:42:29 +00001858CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001859 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001860
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001861 // Save local scope position because in case of condition variable ScopePos
1862 // won't be restored when traversing AST.
1863 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1864
1865 // Create local scope for possible condition variable.
1866 // Store scope position for continue statement.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001867 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001868 if (VarDecl* VD = W->getConditionVariable()) {
1869 addLocalScopeForVarDecl(VD);
1870 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1871 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001872
Mike Stump014b3ea2009-07-21 01:12:51 +00001873 // "while" is a control-flow statement. Thus we stop processing the current
1874 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001875 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001876 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001877 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001878 LoopSuccessor = Block;
Ted Kremenek828f6312011-02-21 22:11:26 +00001879 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001880 } else
1881 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001882
1883 // Because of short-circuit evaluation, the condition of the loop can span
1884 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1885 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001886 CFGBlock* ExitConditionBlock = createBlock(false);
1887 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001888
Ted Kremenek81e14852007-08-27 19:46:09 +00001889 // Set the terminator for the "exit" condition block.
1890 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001891
1892 // Now add the actual condition to the condition block. Because the condition
1893 // itself may contain control-flow, new blocks may be created. Thus we update
1894 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001895 if (Stmt* C = W->getCond()) {
1896 Block = ExitConditionBlock;
1897 EntryConditionBlock = addStmt(C);
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001898 // The condition might finish the current 'Block'.
1899 Block = EntryConditionBlock;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001900
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001901 // If this block contains a condition variable, add both the condition
1902 // variable and initializer to the CFG.
1903 if (VarDecl *VD = W->getConditionVariable()) {
1904 if (Expr *Init = VD->getInit()) {
1905 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001906 appendStmt(Block, W);
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001907 EntryConditionBlock = addStmt(Init);
1908 assert(Block == EntryConditionBlock);
1909 }
1910 }
1911
Ted Kremenek55957a82009-05-02 00:13:27 +00001912 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001913 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001914 return 0;
1915 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001916 }
Mike Stump31feda52009-07-17 01:31:16 +00001917
1918 // The condition block is the implicit successor for the loop body as well as
1919 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001920 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001921
Mike Stump773582d2009-07-23 23:25:26 +00001922 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001923 const TryResult& KnownVal = tryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001924
Ted Kremenek9aae5132007-08-23 21:42:29 +00001925 // Process the loop body.
1926 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001927 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001928
1929 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001930 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1931 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1932 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001933
Mike Stump31feda52009-07-17 01:31:16 +00001934 // Create an empty block to represent the transition block for looping back
1935 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001936 Block = 0;
1937 assert(Succ == EntryConditionBlock);
1938 Succ = createBlock();
1939 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001940 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001941
Ted Kremenek9aae5132007-08-23 21:42:29 +00001942 // All breaks should go to the code following the loop.
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001943 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001944
Ted Kremenek9aae5132007-08-23 21:42:29 +00001945 // NULL out Block to force lazy instantiation of blocks for the body.
1946 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001947
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001948 // Loop body should end with destructor of Condition variable (if any).
1949 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1950
1951 // If body is not a compound statement create implicit scope
1952 // and add destructors.
1953 if (!isa<CompoundStmt>(W->getBody()))
1954 addLocalScopeAndDtors(W->getBody());
1955
Ted Kremenek9aae5132007-08-23 21:42:29 +00001956 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001957 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001958
Ted Kremeneke9610502007-08-30 18:39:40 +00001959 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001960 BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001961 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001962 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001963 return 0;
1964 }
Mike Stump31feda52009-07-17 01:31:16 +00001965
Ted Kremenek30754282009-07-24 04:47:11 +00001966 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001967 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001968 }
Mike Stump31feda52009-07-17 01:31:16 +00001969
Ted Kremenek30754282009-07-24 04:47:11 +00001970 // Link up the condition block with the code that follows the loop. (the
1971 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001972 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001973
1974 // There can be no more statements in the condition block since we loop back
1975 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001976 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001977
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001978 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001979 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001980 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001981}
Mike Stump11289f42009-09-09 15:08:12 +00001982
1983
Ted Kremenek93668002009-07-17 22:18:43 +00001984CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1985 // FIXME: For now we pretend that @catch and the code it contains does not
1986 // exit.
1987 return Block;
1988}
Mike Stump31feda52009-07-17 01:31:16 +00001989
Ted Kremenek93041ba2008-12-09 20:20:09 +00001990CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1991 // FIXME: This isn't complete. We basically treat @throw like a return
1992 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001993
Ted Kremenek0868eea2009-09-24 18:45:41 +00001994 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001995 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001996 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001997
Ted Kremenek93041ba2008-12-09 20:20:09 +00001998 // Create the new block.
1999 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00002000
Ted Kremenek93041ba2008-12-09 20:20:09 +00002001 // The Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002002 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00002003
2004 // Add the statement to the block. This may create new blocks if S contains
2005 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002006 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00002007}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002008
Mike Stump8dd1b6b2009-07-22 22:56:04 +00002009CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00002010 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002011 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00002012 return 0;
2013
2014 // Create the new block.
2015 Block = createBlock(false);
2016
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002017 if (TryTerminatedBlock)
2018 // The current try statement is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002019 addSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002020 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002021 // otherwise the Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002022 addSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00002023
2024 // Add the statement to the block. This may create new blocks if S contains
2025 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002026 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00002027}
2028
Ted Kremenek93668002009-07-17 22:18:43 +00002029CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00002030 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002031
Mike Stump8d50b6a2009-07-21 01:27:50 +00002032 // "do...while" is a control-flow statement. Thus we stop processing the
2033 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002034 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002035 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002036 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002037 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002038 } else
2039 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00002040
2041 // Because of short-circuit evaluation, the condition of the loop can span
2042 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
2043 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00002044 CFGBlock* ExitConditionBlock = createBlock(false);
2045 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002046
Ted Kremenek81e14852007-08-27 19:46:09 +00002047 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00002048 ExitConditionBlock->setTerminator(D);
2049
2050 // Now add the actual condition to the condition block. Because the condition
2051 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00002052 if (Stmt* C = D->getCond()) {
2053 Block = ExitConditionBlock;
2054 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00002055 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002056 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002057 return 0;
2058 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002059 }
Mike Stump31feda52009-07-17 01:31:16 +00002060
Ted Kremeneka1523a32008-02-27 07:20:00 +00002061 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002062 Succ = EntryConditionBlock;
2063
Mike Stump773582d2009-07-23 23:25:26 +00002064 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002065 const TryResult &KnownVal = tryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00002066
Ted Kremenek9aae5132007-08-23 21:42:29 +00002067 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002068 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002069 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002070 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002071
Ted Kremenek9aae5132007-08-23 21:42:29 +00002072 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002073 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2074 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2075 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00002076
Ted Kremenek9aae5132007-08-23 21:42:29 +00002077 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002078 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002079
Ted Kremenek9aae5132007-08-23 21:42:29 +00002080 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002081 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002082
Ted Kremenek9aae5132007-08-23 21:42:29 +00002083 // NULL out Block to force lazy instantiation of blocks for the body.
2084 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002085
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00002086 // If body is not a compound statement create implicit scope
2087 // and add destructors.
2088 if (!isa<CompoundStmt>(D->getBody()))
2089 addLocalScopeAndDtors(D->getBody());
2090
Ted Kremenek9aae5132007-08-23 21:42:29 +00002091 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00002092 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002093
Ted Kremeneke9610502007-08-30 18:39:40 +00002094 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00002095 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00002096 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002097 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002098 return 0;
2099 }
Mike Stump31feda52009-07-17 01:31:16 +00002100
Ted Kremenek110974d2010-08-17 20:59:56 +00002101 if (!KnownVal.isFalse()) {
2102 // Add an intermediate block between the BodyBlock and the
2103 // ExitConditionBlock to represent the "loop back" transition. Create an
2104 // empty block to represent the transition block for looping back to the
2105 // head of the loop.
2106 // FIXME: Can we do this more efficiently without adding another block?
2107 Block = NULL;
2108 Succ = BodyBlock;
2109 CFGBlock *LoopBackBlock = createBlock();
2110 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00002111
Ted Kremenek110974d2010-08-17 20:59:56 +00002112 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002113 addSuccessor(ExitConditionBlock, LoopBackBlock);
Ted Kremenek110974d2010-08-17 20:59:56 +00002114 }
2115 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002116 addSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002117 }
Mike Stump31feda52009-07-17 01:31:16 +00002118
Ted Kremenek30754282009-07-24 04:47:11 +00002119 // Link up the condition block with the code that follows the loop.
2120 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002121 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00002122
2123 // There can be no more statements in the body block(s) since we loop back to
2124 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002125 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002126
Ted Kremenek9aae5132007-08-23 21:42:29 +00002127 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00002128 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002129 return BodyBlock;
2130}
2131
2132CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2133 // "continue" is a control-flow statement. Thus we stop processing the
2134 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002135 if (badCFG)
2136 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002137
Ted Kremenek9aae5132007-08-23 21:42:29 +00002138 // Now create a new block that ends with the continue statement.
2139 Block = createBlock(false);
2140 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00002141
Ted Kremenek9aae5132007-08-23 21:42:29 +00002142 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00002143 // incomplete AST. This means the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00002144 if (ContinueJumpTarget.block) {
2145 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C);
2146 addSuccessor(Block, ContinueJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002147 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00002148 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00002149
Ted Kremenek9aae5132007-08-23 21:42:29 +00002150 return Block;
2151}
Mike Stump11289f42009-09-09 15:08:12 +00002152
Peter Collingbournee190dee2011-03-11 19:24:49 +00002153CFGBlock *CFGBuilder::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E,
2154 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002155
Ted Kremenek7c58d352011-03-10 01:14:11 +00002156 if (asc.alwaysAdd(*this, E)) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002157 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002158 appendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00002159 }
Mike Stump11289f42009-09-09 15:08:12 +00002160
Ted Kremenek93668002009-07-17 22:18:43 +00002161 // VLA types have expressions that must be evaluated.
2162 if (E->isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00002163 for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr());
Ted Kremenek93668002009-07-17 22:18:43 +00002164 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2165 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00002166 }
Mike Stump11289f42009-09-09 15:08:12 +00002167
Mike Stump31feda52009-07-17 01:31:16 +00002168 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002169}
Mike Stump11289f42009-09-09 15:08:12 +00002170
Ted Kremenek93668002009-07-17 22:18:43 +00002171/// VisitStmtExpr - Utility method to handle (nested) statement
2172/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002173CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
Ted Kremenek7c58d352011-03-10 01:14:11 +00002174 if (asc.alwaysAdd(*this, SE)) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002175 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002176 appendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00002177 }
Ted Kremenek93668002009-07-17 22:18:43 +00002178 return VisitCompoundStmt(SE->getSubStmt());
2179}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002180
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002181CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00002182 // "switch" is a control-flow statement. Thus we stop processing the current
2183 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002184 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002185
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002186 // Save local scope position because in case of condition variable ScopePos
2187 // won't be restored when traversing AST.
2188 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2189
2190 // Create local scope for possible condition variable.
2191 // Store scope position. Add implicit destructor.
2192 if (VarDecl* VD = Terminator->getConditionVariable()) {
2193 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2194 addLocalScopeForVarDecl(VD);
2195 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2196 }
2197
Ted Kremenek9aae5132007-08-23 21:42:29 +00002198 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002199 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002200 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002201 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00002202 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002203
2204 // Save the current "switch" context.
2205 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00002206 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002207 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00002208
Mike Stump31feda52009-07-17 01:31:16 +00002209 // Set the "default" case to be the block after the switch statement. If the
2210 // switch statement contains a "default:", this value will be overwritten with
2211 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00002212 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00002213
Ted Kremenek9aae5132007-08-23 21:42:29 +00002214 // Create a new block that will contain the switch statement.
2215 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00002216
Ted Kremenek9aae5132007-08-23 21:42:29 +00002217 // Now process the switch body. The code after the switch is the implicit
2218 // successor.
2219 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002220 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002221
2222 // When visiting the body, the case statements should automatically get linked
2223 // up to the switch. We also don't keep a pointer to the body, since all
2224 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002225 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00002226 Block = NULL;
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002227
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002228 // For pruning unreachable case statements, save the current state
2229 // for tracking the condition value.
2230 SaveAndRestore<bool> save_switchExclusivelyCovered(switchExclusivelyCovered,
2231 false);
Ted Kremenekbe528712011-03-04 01:03:41 +00002232
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002233 // Determine if the switch condition can be explicitly evaluated.
2234 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekbe528712011-03-04 01:03:41 +00002235 Expr::EvalResult result;
Ted Kremenek53e65382011-03-13 03:48:04 +00002236 bool b = tryEvaluate(Terminator->getCond(), result);
2237 SaveAndRestore<Expr::EvalResult*> save_switchCond(switchCond,
2238 b ? &result : 0);
Ted Kremenekbe528712011-03-04 01:03:41 +00002239
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002240 // If body is not a compound statement create implicit scope
2241 // and add destructors.
2242 if (!isa<CompoundStmt>(Terminator->getBody()))
2243 addLocalScopeAndDtors(Terminator->getBody());
2244
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002245 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00002246 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002247 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002248 return 0;
2249 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002250
Mike Stump31feda52009-07-17 01:31:16 +00002251 // If we have no "default:" case, the default transition is to the code
2252 // following the switch body.
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002253 addSuccessor(SwitchTerminatedBlock,
2254 switchExclusivelyCovered ? 0 : DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002255
Ted Kremenek81e14852007-08-27 19:46:09 +00002256 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002257 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002258 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002259 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002260
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002261 // Finally, if the SwitchStmt contains a condition variable, add both the
2262 // SwitchStmt and the condition variable initialization to the CFG.
2263 if (VarDecl *VD = Terminator->getConditionVariable()) {
2264 if (Expr *Init = VD->getInit()) {
2265 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00002266 appendStmt(Block, Terminator);
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002267 addStmt(Init);
2268 }
2269 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002270
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002271 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002272}
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002273
2274static bool shouldAddCase(bool &switchExclusivelyCovered,
Ted Kremenek53e65382011-03-13 03:48:04 +00002275 const Expr::EvalResult *switchCond,
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002276 const CaseStmt *CS,
2277 ASTContext &Ctx) {
Ted Kremenek53e65382011-03-13 03:48:04 +00002278 if (!switchCond)
2279 return true;
2280
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002281 bool addCase = false;
Ted Kremenekbe528712011-03-04 01:03:41 +00002282
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002283 if (!switchExclusivelyCovered) {
Ted Kremenek53e65382011-03-13 03:48:04 +00002284 if (switchCond->Val.isInt()) {
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002285 // Evaluate the LHS of the case value.
2286 Expr::EvalResult V1;
2287 CS->getLHS()->Evaluate(V1, Ctx);
2288 assert(V1.Val.isInt());
Ted Kremenek53e65382011-03-13 03:48:04 +00002289 const llvm::APSInt &condInt = switchCond->Val.getInt();
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002290 const llvm::APSInt &lhsInt = V1.Val.getInt();
2291
2292 if (condInt == lhsInt) {
2293 addCase = true;
2294 switchExclusivelyCovered = true;
2295 }
2296 else if (condInt < lhsInt) {
2297 if (const Expr *RHS = CS->getRHS()) {
2298 // Evaluate the RHS of the case value.
2299 Expr::EvalResult V2;
2300 RHS->Evaluate(V2, Ctx);
2301 assert(V2.Val.isInt());
2302 if (V2.Val.getInt() <= condInt) {
2303 addCase = true;
2304 switchExclusivelyCovered = true;
2305 }
2306 }
2307 }
2308 }
2309 else
2310 addCase = true;
2311 }
2312 return addCase;
2313}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002314
Ted Kremenek93668002009-07-17 22:18:43 +00002315CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00002316 // CaseStmts are essentially labels, so they are the first statement in a
2317 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00002318 CFGBlock *TopBlock = 0, *LastBlock = 0;
Ted Kremenekbe528712011-03-04 01:03:41 +00002319
Ted Kremenek60fa6572010-08-04 23:54:30 +00002320 if (Stmt *Sub = CS->getSubStmt()) {
2321 // For deeply nested chains of CaseStmts, instead of doing a recursion
2322 // (which can blow out the stack), manually unroll and create blocks
2323 // along the way.
2324 while (isa<CaseStmt>(Sub)) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002325 CFGBlock *currentBlock = createBlock(false);
2326 currentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00002327
Ted Kremenek60fa6572010-08-04 23:54:30 +00002328 if (TopBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002329 addSuccessor(LastBlock, currentBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002330 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002331 TopBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002332
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002333 addSuccessor(SwitchTerminatedBlock,
Ted Kremenek53e65382011-03-13 03:48:04 +00002334 shouldAddCase(switchExclusivelyCovered, switchCond,
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002335 CS, *Context)
2336 ? currentBlock : 0);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002337
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002338 LastBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002339 CS = cast<CaseStmt>(Sub);
2340 Sub = CS->getSubStmt();
2341 }
2342
2343 addStmt(Sub);
2344 }
Mike Stump11289f42009-09-09 15:08:12 +00002345
Ted Kremenek55e91e82007-08-30 18:48:11 +00002346 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002347 if (!CaseBlock)
2348 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002349
2350 // Cases statements partition blocks, so this is the top of the basic block we
2351 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00002352 CaseBlock->setLabel(CS);
2353
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002354 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002355 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002356
2357 // Add this block to the list of successors for the block with the switch
2358 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00002359 assert(SwitchTerminatedBlock);
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002360 addSuccessor(SwitchTerminatedBlock,
Ted Kremenek53e65382011-03-13 03:48:04 +00002361 shouldAddCase(switchExclusivelyCovered, switchCond,
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002362 CS, *Context)
2363 ? CaseBlock : 0);
Mike Stump31feda52009-07-17 01:31:16 +00002364
Ted Kremenek9aae5132007-08-23 21:42:29 +00002365 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2366 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002367
Ted Kremenek60fa6572010-08-04 23:54:30 +00002368 if (TopBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002369 addSuccessor(LastBlock, CaseBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002370 Succ = TopBlock;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002371 } else {
Ted Kremenek60fa6572010-08-04 23:54:30 +00002372 // This block is now the implicit successor of other blocks.
2373 Succ = CaseBlock;
2374 }
Mike Stump31feda52009-07-17 01:31:16 +00002375
Ted Kremenek60fa6572010-08-04 23:54:30 +00002376 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002377}
Mike Stump31feda52009-07-17 01:31:16 +00002378
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002379CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00002380 if (Terminator->getSubStmt())
2381 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00002382
Ted Kremenek654c78f2008-02-13 22:05:39 +00002383 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002384
2385 if (!DefaultCaseBlock)
2386 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002387
2388 // Default statements partition blocks, so this is the top of the basic block
2389 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002390 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00002391
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002392 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002393 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00002394
Mike Stump31feda52009-07-17 01:31:16 +00002395 // Unlike case statements, we don't add the default block to the successors
2396 // for the switch statement immediately. This is done when we finish
2397 // processing the switch statement. This allows for the default case
2398 // (including a fall-through to the code after the switch statement) to always
2399 // be the last successor of a switch-terminated block.
2400
Ted Kremenek654c78f2008-02-13 22:05:39 +00002401 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2402 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002403
Ted Kremenek654c78f2008-02-13 22:05:39 +00002404 // This block is now the implicit successor of other blocks.
2405 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002406
2407 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00002408}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002409
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002410CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2411 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2412 // current block.
2413 CFGBlock* TrySuccessor = NULL;
2414
2415 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002416 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002417 return 0;
2418 TrySuccessor = Block;
2419 } else TrySuccessor = Succ;
2420
Mike Stump0bdba6c2010-01-20 01:15:34 +00002421 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002422
2423 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00002424 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002425 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00002426 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002427
Mike Stump0bdba6c2010-01-20 01:15:34 +00002428 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002429 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2430 // The code after the try is the implicit successor.
2431 Succ = TrySuccessor;
2432 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002433 if (CS->getExceptionDecl() == 0) {
2434 HasCatchAll = true;
2435 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002436 Block = NULL;
2437 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2438 if (CatchBlock == 0)
2439 return 0;
2440 // Add this block to the list of successors for the block with the try
2441 // statement.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002442 addSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002443 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00002444 if (!HasCatchAll) {
2445 if (PrevTryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002446 addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002447 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002448 addSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00002449 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002450
2451 // The code after the try is the implicit successor.
2452 Succ = TrySuccessor;
2453
Mike Stump845384a2010-01-20 01:30:58 +00002454 // Save the current "try" context.
2455 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2456 TryTerminatedBlock = NewTryTerminatedBlock;
2457
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002458 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002459 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002460 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002461 return Block;
2462}
2463
2464CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2465 // CXXCatchStmt are treated like labels, so they are the first statement in a
2466 // block.
2467
Marcin Swiderski3546b1a2010-10-01 01:46:52 +00002468 // Save local scope position because in case of exception variable ScopePos
2469 // won't be restored when traversing AST.
2470 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2471
2472 // Create local scope for possible exception variable.
2473 // Store scope position. Add implicit destructor.
2474 if (VarDecl* VD = CS->getExceptionDecl()) {
2475 LocalScope::const_iterator BeginScopePos = ScopePos;
2476 addLocalScopeForVarDecl(VD);
2477 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2478 }
2479
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002480 if (CS->getHandlerBlock())
2481 addStmt(CS->getHandlerBlock());
2482
2483 CFGBlock* CatchBlock = Block;
2484 if (!CatchBlock)
2485 CatchBlock = createBlock();
2486
2487 CatchBlock->setLabel(CS);
2488
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002489 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002490 return 0;
2491
2492 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2493 Block = NULL;
2494
2495 return CatchBlock;
2496}
2497
John McCall5d413782010-12-06 08:20:24 +00002498CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002499 AddStmtChoice asc) {
2500 if (BuildOpts.AddImplicitDtors) {
2501 // If adding implicit destructors visit the full expression for adding
2502 // destructors of temporaries.
2503 VisitForTemporaryDtors(E->getSubExpr());
2504
2505 // Full expression has to be added as CFGStmt so it will be sequenced
2506 // before destructors of it's temporaries.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002507 asc = asc.withAlwaysAdd(true);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002508 }
2509 return Visit(E->getSubExpr(), asc);
2510}
2511
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002512CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2513 AddStmtChoice asc) {
Ted Kremenek7c58d352011-03-10 01:14:11 +00002514 if (asc.alwaysAdd(*this, E)) {
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002515 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00002516 appendStmt(Block, E);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002517
2518 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002519 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002520 }
2521 return Visit(E->getSubExpr(), asc);
2522}
2523
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002524CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2525 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002526 autoCreateBlock();
Zhongxing Xufb2f8162010-11-03 11:14:06 +00002527 if (!C->isElidable())
Ted Kremenek2866bab2011-03-10 01:14:08 +00002528 appendStmt(Block, C);
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002529
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002530 return VisitChildren(C);
2531}
2532
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002533CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2534 AddStmtChoice asc) {
Ted Kremenek7c58d352011-03-10 01:14:11 +00002535 if (asc.alwaysAdd(*this, E)) {
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002536 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00002537 appendStmt(Block, E);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002538 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002539 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002540 }
2541 return Visit(E->getSubExpr(), asc);
2542}
2543
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002544CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2545 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002546 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00002547 appendStmt(Block, C);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002548 return VisitChildren(C);
2549}
2550
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002551CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00002552 AddStmtChoice asc) {
Zhongxing Xu7e612172010-04-13 09:38:01 +00002553 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00002554 appendStmt(Block, C);
Zhongxing Xu7e612172010-04-13 09:38:01 +00002555 return VisitChildren(C);
2556}
2557
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002558CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2559 AddStmtChoice asc) {
Ted Kremenek7c58d352011-03-10 01:14:11 +00002560 if (asc.alwaysAdd(*this, E)) {
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002561 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00002562 appendStmt(Block, E);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002563 }
Ted Kremenek8219b822010-12-16 07:46:53 +00002564 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002565}
2566
Ted Kremenekeda180e22007-08-28 19:26:49 +00002567CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00002568 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00002569 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002570
Ted Kremenekeda180e22007-08-28 19:26:49 +00002571 if (!IBlock) {
2572 IBlock = createBlock(false);
2573 cfg->setIndirectGotoBlock(IBlock);
2574 }
Mike Stump31feda52009-07-17 01:31:16 +00002575
Ted Kremenekeda180e22007-08-28 19:26:49 +00002576 // IndirectGoto is a control-flow statement. Thus we stop processing the
2577 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002578 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00002579 return 0;
2580
Ted Kremenekeda180e22007-08-28 19:26:49 +00002581 Block = createBlock(false);
2582 Block->setTerminator(I);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002583 addSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00002584 return addStmt(I->getTarget());
2585}
2586
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002587CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2588tryAgain:
2589 if (!E) {
2590 badCFG = true;
2591 return NULL;
2592 }
2593 switch (E->getStmtClass()) {
2594 default:
2595 return VisitChildrenForTemporaryDtors(E);
2596
2597 case Stmt::BinaryOperatorClass:
2598 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2599
2600 case Stmt::CXXBindTemporaryExprClass:
2601 return VisitCXXBindTemporaryExprForTemporaryDtors(
2602 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2603
John McCallc07a0c72011-02-17 10:25:35 +00002604 case Stmt::BinaryConditionalOperatorClass:
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002605 case Stmt::ConditionalOperatorClass:
2606 return VisitConditionalOperatorForTemporaryDtors(
John McCallc07a0c72011-02-17 10:25:35 +00002607 cast<AbstractConditionalOperator>(E), BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002608
2609 case Stmt::ImplicitCastExprClass:
2610 // For implicit cast we want BindToTemporary to be passed further.
2611 E = cast<CastExpr>(E)->getSubExpr();
2612 goto tryAgain;
2613
2614 case Stmt::ParenExprClass:
2615 E = cast<ParenExpr>(E)->getSubExpr();
2616 goto tryAgain;
2617 }
2618}
2619
2620CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2621 // When visiting children for destructors we want to visit them in reverse
2622 // order. Because there's no reverse iterator for children must to reverse
2623 // them in helper vector.
2624 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2625 ChildrenVect ChildrenRev;
John McCall8322c3a2011-02-13 04:07:26 +00002626 for (Stmt::child_range I = E->children(); I; ++I) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002627 if (*I) ChildrenRev.push_back(*I);
2628 }
2629
2630 CFGBlock *B = Block;
2631 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2632 L = ChildrenRev.rend(); I != L; ++I) {
2633 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2634 B = R;
2635 }
2636 return B;
2637}
2638
2639CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2640 if (E->isLogicalOp()) {
2641 // Destructors for temporaries in LHS expression should be called after
2642 // those for RHS expression. Even if this will unnecessarily create a block,
2643 // this block will be used at least by the full expression.
2644 autoCreateBlock();
2645 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2646 if (badCFG)
2647 return NULL;
2648
2649 Succ = ConfluenceBlock;
2650 Block = NULL;
2651 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2652
2653 if (RHSBlock) {
2654 if (badCFG)
2655 return NULL;
2656
2657 // If RHS expression did produce destructors we need to connect created
2658 // blocks to CFG in same manner as for binary operator itself.
2659 CFGBlock *LHSBlock = createBlock(false);
2660 LHSBlock->setTerminator(CFGTerminator(E, true));
2661
2662 // For binary operator LHS block is before RHS in list of predecessors
2663 // of ConfluenceBlock.
2664 std::reverse(ConfluenceBlock->pred_begin(),
2665 ConfluenceBlock->pred_end());
2666
2667 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002668 TryResult KnownVal = tryEvaluateBool(E->getLHS());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002669 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2670 KnownVal.negate();
2671
2672 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2673 // itself.
2674 if (E->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002675 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2676 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002677 } else {
2678 assert (E->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002679 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2680 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002681 }
2682
2683 Block = LHSBlock;
2684 return LHSBlock;
2685 }
2686
2687 Block = ConfluenceBlock;
2688 return ConfluenceBlock;
2689 }
2690
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002691 if (E->isAssignmentOp()) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002692 // For assignment operator (=) LHS expression is visited
2693 // before RHS expression. For destructors visit them in reverse order.
2694 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2695 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2696 return LHSBlock ? LHSBlock : RHSBlock;
2697 }
2698
2699 // For any other binary operator RHS expression is visited before
2700 // LHS expression (order of children). For destructors visit them in reverse
2701 // order.
2702 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2703 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2704 return RHSBlock ? RHSBlock : LHSBlock;
2705}
2706
2707CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2708 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2709 // First add destructors for temporaries in subexpression.
2710 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xufee455f2010-11-14 15:23:50 +00002711 if (!BindToTemporary) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002712 // If lifetime of temporary is not prolonged (by assigning to constant
2713 // reference) add destructor for it.
2714 autoCreateBlock();
2715 appendTemporaryDtor(Block, E);
2716 B = Block;
2717 }
2718 return B;
2719}
2720
2721CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
John McCallc07a0c72011-02-17 10:25:35 +00002722 AbstractConditionalOperator *E, bool BindToTemporary) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002723 // First add destructors for condition expression. Even if this will
2724 // unnecessarily create a block, this block will be used at least by the full
2725 // expression.
2726 autoCreateBlock();
2727 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2728 if (badCFG)
2729 return NULL;
John McCallc07a0c72011-02-17 10:25:35 +00002730 if (BinaryConditionalOperator *BCO
2731 = dyn_cast<BinaryConditionalOperator>(E)) {
2732 ConfluenceBlock = VisitForTemporaryDtors(BCO->getCommon());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002733 if (badCFG)
2734 return NULL;
2735 }
2736
John McCallc07a0c72011-02-17 10:25:35 +00002737 // Try to add block with destructors for LHS expression.
2738 CFGBlock *LHSBlock = NULL;
2739 Succ = ConfluenceBlock;
2740 Block = NULL;
2741 LHSBlock = VisitForTemporaryDtors(E->getTrueExpr(), BindToTemporary);
2742 if (badCFG)
2743 return NULL;
2744
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002745 // Try to add block with destructors for RHS expression;
2746 Succ = ConfluenceBlock;
2747 Block = NULL;
John McCallc07a0c72011-02-17 10:25:35 +00002748 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getFalseExpr(),
2749 BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002750 if (badCFG)
2751 return NULL;
2752
2753 if (!RHSBlock && !LHSBlock) {
2754 // If neither LHS nor RHS expression had temporaries to destroy don't create
2755 // more blocks.
2756 Block = ConfluenceBlock;
2757 return Block;
2758 }
2759
2760 Block = createBlock(false);
2761 Block->setTerminator(CFGTerminator(E, true));
2762
2763 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002764 const TryResult &KnownVal = tryEvaluateBool(E->getCond());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002765
2766 if (LHSBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002767 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002768 } else if (KnownVal.isFalse()) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002769 addSuccessor(Block, NULL);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002770 } else {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002771 addSuccessor(Block, ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002772 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2773 }
2774
2775 if (!RHSBlock)
2776 RHSBlock = ConfluenceBlock;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002777 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002778
2779 return Block;
2780}
2781
Ted Kremenek04cca642007-08-23 21:26:19 +00002782} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00002783
Mike Stump31feda52009-07-17 01:31:16 +00002784/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2785/// no successors or predecessors. If this is the first block created in the
2786/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00002787CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00002788 bool first_block = begin() == end();
2789
2790 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002791 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2792 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2793 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00002794
2795 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002796 if (first_block)
2797 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00002798
2799 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002800 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002801}
2802
Ted Kremenek889073f2007-08-23 16:51:22 +00002803/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2804/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00002805CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenekf9d82902011-03-10 01:14:05 +00002806 const BuildOptions &BO) {
2807 CFGBuilder Builder(C, BO);
2808 return Builder.buildCFG(D, Statement);
Ted Kremenek889073f2007-08-23 16:51:22 +00002809}
2810
Ted Kremenek8cfe2072011-03-03 01:21:32 +00002811const CXXDestructorDecl *
2812CFGImplicitDtor::getDestructorDecl(ASTContext &astContext) const {
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002813 switch (getKind()) {
2814 case CFGElement::Invalid:
2815 case CFGElement::Statement:
2816 case CFGElement::Initializer:
2817 llvm_unreachable("getDestructorDecl should only be used with "
2818 "ImplicitDtors");
2819 case CFGElement::AutomaticObjectDtor: {
2820 const VarDecl *var = cast<CFGAutomaticObjDtor>(this)->getVarDecl();
2821 QualType ty = var->getType();
Ted Kremenek1676a042011-03-03 01:01:03 +00002822 ty = ty.getNonReferenceType();
Ted Kremenek8cfe2072011-03-03 01:21:32 +00002823 if (const ArrayType *arrayType = astContext.getAsArrayType(ty)) {
2824 ty = arrayType->getElementType();
2825 }
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002826 const RecordType *recordType = ty->getAs<RecordType>();
2827 const CXXRecordDecl *classDecl =
Ted Kremenek1676a042011-03-03 01:01:03 +00002828 cast<CXXRecordDecl>(recordType->getDecl());
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002829 return classDecl->getDestructor();
2830 }
2831 case CFGElement::TemporaryDtor: {
2832 const CXXBindTemporaryExpr *bindExpr =
2833 cast<CFGTemporaryDtor>(this)->getBindTemporaryExpr();
2834 const CXXTemporary *temp = bindExpr->getTemporary();
2835 return temp->getDestructor();
2836 }
2837 case CFGElement::BaseDtor:
2838 case CFGElement::MemberDtor:
2839
2840 // Not yet supported.
2841 return 0;
2842 }
Ted Kremenek1676a042011-03-03 01:01:03 +00002843 llvm_unreachable("getKind() returned bogus value");
Matt Beaumont-Gay86b900b2011-03-03 00:48:05 +00002844 return 0;
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002845}
2846
Ted Kremenek8cfe2072011-03-03 01:21:32 +00002847bool CFGImplicitDtor::isNoReturn(ASTContext &astContext) const {
2848 if (const CXXDestructorDecl *cdecl = getDestructorDecl(astContext)) {
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002849 QualType ty = cdecl->getType();
2850 return cast<FunctionType>(ty)->getNoReturnAttr();
2851 }
2852 return false;
Ted Kremenek96a7a592011-03-01 03:15:10 +00002853}
2854
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002855//===----------------------------------------------------------------------===//
2856// CFG: Queries for BlkExprs.
2857//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002858
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002859namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002860 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002861}
2862
Ted Kremenekbff98442009-12-23 23:37:10 +00002863static void FindSubExprAssignments(Stmt *S,
2864 llvm::SmallPtrSet<Expr*,50>& Set) {
2865 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00002866 return;
Mike Stump31feda52009-07-17 01:31:16 +00002867
John McCall8322c3a2011-02-13 04:07:26 +00002868 for (Stmt::child_range I = S->children(); I; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002869 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00002870 if (!child)
2871 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002872
Ted Kremenekbff98442009-12-23 23:37:10 +00002873 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002874 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00002875
Ted Kremenekbff98442009-12-23 23:37:10 +00002876 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00002877 }
2878}
2879
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002880static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2881 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00002882
2883 // Look for assignments that are used as subexpressions. These are the only
2884 // assignments that we want to *possibly* register as a block-level
2885 // expression. Basically, if an assignment occurs both in a subexpression and
2886 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00002887 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00002888
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002889 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002890 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek96a7a592011-03-01 03:15:10 +00002891 if (const CFGStmt *S = BI->getAs<CFGStmt>())
2892 FindSubExprAssignments(S->getStmt(), SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002893
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002894 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00002895
2896 // Iterate over the statements again on identify the Expr* and Stmt* at the
2897 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002898
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002899 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
Ted Kremenek96a7a592011-03-01 03:15:10 +00002900 const CFGStmt *CS = BI->getAs<CFGStmt>();
2901 if (!CS)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002902 continue;
Ted Kremenek96a7a592011-03-01 03:15:10 +00002903 if (Expr* Exp = dyn_cast<Expr>(CS->getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00002904
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002905 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002906 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00002907 // expression are really "statements" whose value is never used by
2908 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002909 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002910 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002911 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2912 // Special handling for statement expressions. The last statement in
2913 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002914 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002915 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002916 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002917 (*M)[C->body_back()] = x;
2918 }
2919 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002920
Ted Kremenek95a123c2008-01-26 00:03:27 +00002921 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002922 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002923 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002924 }
Mike Stump31feda52009-07-17 01:31:16 +00002925
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002926 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002927
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002928 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002929
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002930 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002931 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002932 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002933 }
2934 }
Mike Stump31feda52009-07-17 01:31:16 +00002935
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002936 return M;
2937}
2938
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002939CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2940 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002941 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002942
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002943 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002944 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002945 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002946}
2947
2948unsigned CFG::getNumBlkExprs() {
2949 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2950 return M->size();
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002951
2952 // We assume callers interested in the number of BlkExprs will want
2953 // the map constructed if it doesn't already exist.
2954 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2955 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002956}
2957
Ted Kremenek6065ef62008-04-28 18:00:46 +00002958//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002959// Filtered walking of the CFG.
2960//===----------------------------------------------------------------------===//
2961
2962bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002963 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002964
Ted Kremenek89794742011-03-07 22:04:39 +00002965 if (To && F.IgnoreDefaultsWithCoveredEnums) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002966 // If the 'To' has no label or is labeled but the label isn't a
2967 // CaseStmt then filter this edge.
2968 if (const SwitchStmt *S =
Ted Kremenek89794742011-03-07 22:04:39 +00002969 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002970 if (S->isAllEnumCasesCovered()) {
Ted Kremenek89794742011-03-07 22:04:39 +00002971 const Stmt *L = To->getLabel();
2972 if (!L || !isa<CaseStmt>(L))
2973 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002974 }
2975 }
2976 }
2977
2978 return false;
2979}
2980
2981//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002982// Cleanup: CFG dstor.
2983//===----------------------------------------------------------------------===//
2984
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002985CFG::~CFG() {
2986 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2987}
Mike Stump31feda52009-07-17 01:31:16 +00002988
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002989//===----------------------------------------------------------------------===//
2990// CFG pretty printing
2991//===----------------------------------------------------------------------===//
2992
Ted Kremenek7e776b12007-08-22 18:22:34 +00002993namespace {
2994
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002995class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek96a7a592011-03-01 03:15:10 +00002996 typedef llvm::DenseMap<const Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
2997 typedef llvm::DenseMap<const Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002998 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002999 DeclMapTy DeclMap;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00003000 signed currentBlock;
3001 unsigned currentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00003002 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00003003public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003004
Chris Lattnerc61089a2009-06-30 01:26:17 +00003005 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
Ted Kremenek96a7a592011-03-01 03:15:10 +00003006 : currentBlock(0), currentStmt(0), LangOpts(LO)
3007 {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003008 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
3009 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003010 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003011 BI != BEnd; ++BI, ++j ) {
Ted Kremenek96a7a592011-03-01 03:15:10 +00003012 if (const CFGStmt *SE = BI->getAs<CFGStmt>()) {
3013 const Stmt *stmt= SE->getStmt();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003014 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
Ted Kremenek96a7a592011-03-01 03:15:10 +00003015 StmtMap[stmt] = P;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003016
Ted Kremenek96a7a592011-03-01 03:15:10 +00003017 switch (stmt->getStmtClass()) {
3018 case Stmt::DeclStmtClass:
3019 DeclMap[cast<DeclStmt>(stmt)->getSingleDecl()] = P;
3020 break;
3021 case Stmt::IfStmtClass: {
3022 const VarDecl *var = cast<IfStmt>(stmt)->getConditionVariable();
3023 if (var)
3024 DeclMap[var] = P;
3025 break;
3026 }
3027 case Stmt::ForStmtClass: {
3028 const VarDecl *var = cast<ForStmt>(stmt)->getConditionVariable();
3029 if (var)
3030 DeclMap[var] = P;
3031 break;
3032 }
3033 case Stmt::WhileStmtClass: {
3034 const VarDecl *var =
3035 cast<WhileStmt>(stmt)->getConditionVariable();
3036 if (var)
3037 DeclMap[var] = P;
3038 break;
3039 }
3040 case Stmt::SwitchStmtClass: {
3041 const VarDecl *var =
3042 cast<SwitchStmt>(stmt)->getConditionVariable();
3043 if (var)
3044 DeclMap[var] = P;
3045 break;
3046 }
3047 case Stmt::CXXCatchStmtClass: {
3048 const VarDecl *var =
3049 cast<CXXCatchStmt>(stmt)->getExceptionDecl();
3050 if (var)
3051 DeclMap[var] = P;
3052 break;
3053 }
3054 default:
3055 break;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003056 }
3057 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003058 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00003059 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003060 }
Ted Kremenek96a7a592011-03-01 03:15:10 +00003061
Mike Stump31feda52009-07-17 01:31:16 +00003062
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003063 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00003064
Chris Lattnerc61089a2009-06-30 01:26:17 +00003065 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00003066 void setBlockID(signed i) { currentBlock = i; }
3067 void setStmtID(unsigned i) { currentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00003068
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003069 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
3070 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003071
3072 if (I == StmtMap.end())
3073 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003074
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00003075 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3076 && I->second.second == currentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003077 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00003078 }
Mike Stump31feda52009-07-17 01:31:16 +00003079
Ted Kremenek60983dc2010-01-19 20:52:05 +00003080 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003081 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003082 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003083
Ted Kremenek96a7a592011-03-01 03:15:10 +00003084 bool handleDecl(const Decl* D, llvm::raw_ostream& OS) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003085 DeclMapTy::iterator I = DeclMap.find(D);
3086
3087 if (I == DeclMap.end())
3088 return false;
3089
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00003090 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3091 && I->second.second == currentStmt) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003092 return false;
3093 }
3094
3095 OS << "[B" << I->second.first << "." << I->second.second << "]";
3096 return true;
3097 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003098};
Chris Lattnerc61089a2009-06-30 01:26:17 +00003099} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003100
Chris Lattnerc61089a2009-06-30 01:26:17 +00003101
3102namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00003103class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00003104 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00003105
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003106 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003107 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00003108 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003109public:
Douglas Gregor7de59662009-05-29 20:38:28 +00003110 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00003111 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00003112 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00003113
Ted Kremenek9aae5132007-08-23 21:42:29 +00003114 void VisitIfStmt(IfStmt* I) {
3115 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00003116 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00003117 }
Mike Stump31feda52009-07-17 01:31:16 +00003118
Ted Kremenek9aae5132007-08-23 21:42:29 +00003119 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00003120 void VisitStmt(Stmt* Terminator) {
3121 Terminator->printPretty(OS, Helper, Policy);
3122 }
3123
Ted Kremenek9aae5132007-08-23 21:42:29 +00003124 void VisitForStmt(ForStmt* F) {
3125 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00003126 if (F->getInit())
3127 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00003128 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00003129 if (Stmt* C = F->getCond())
3130 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00003131 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00003132 if (F->getInc())
3133 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00003134 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00003135 }
Mike Stump31feda52009-07-17 01:31:16 +00003136
Ted Kremenek9aae5132007-08-23 21:42:29 +00003137 void VisitWhileStmt(WhileStmt* W) {
3138 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00003139 if (Stmt* C = W->getCond())
3140 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00003141 }
Mike Stump31feda52009-07-17 01:31:16 +00003142
Ted Kremenek9aae5132007-08-23 21:42:29 +00003143 void VisitDoStmt(DoStmt* D) {
3144 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00003145 if (Stmt* C = D->getCond())
3146 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00003147 }
Mike Stump31feda52009-07-17 01:31:16 +00003148
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003149 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00003150 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00003151 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00003152 }
Mike Stump31feda52009-07-17 01:31:16 +00003153
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003154 void VisitCXXTryStmt(CXXTryStmt* CS) {
3155 OS << "try ...";
3156 }
3157
John McCallc07a0c72011-02-17 10:25:35 +00003158 void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00003159 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003160 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003161 }
Mike Stump31feda52009-07-17 01:31:16 +00003162
Ted Kremenek391f94a2007-08-31 22:29:13 +00003163 void VisitChooseExpr(ChooseExpr* C) {
3164 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00003165 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00003166 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00003167 }
Mike Stump31feda52009-07-17 01:31:16 +00003168
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003169 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
3170 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00003171 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003172 }
Mike Stump31feda52009-07-17 01:31:16 +00003173
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003174 void VisitBinaryOperator(BinaryOperator* B) {
3175 if (!B->isLogicalOp()) {
3176 VisitExpr(B);
3177 return;
3178 }
Mike Stump31feda52009-07-17 01:31:16 +00003179
Douglas Gregor7de59662009-05-29 20:38:28 +00003180 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003181
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003182 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00003183 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00003184 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003185 return;
John McCalle3027922010-08-25 11:45:40 +00003186 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00003187 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003188 return;
3189 default:
3190 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00003191 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003192 }
Mike Stump31feda52009-07-17 01:31:16 +00003193
Ted Kremenek12687ff2007-08-27 21:54:41 +00003194 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00003195 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003196 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00003197};
Chris Lattnerc61089a2009-06-30 01:26:17 +00003198} // end anonymous namespace
3199
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003200static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00003201 const CFGElement &E) {
Ted Kremenek96a7a592011-03-01 03:15:10 +00003202 if (const CFGStmt *CS = E.getAs<CFGStmt>()) {
3203 Stmt *S = CS->getStmt();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003204
3205 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00003206
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003207 // special printing for statement-expressions.
3208 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3209 CompoundStmt* Sub = SE->getSubStmt();
3210
John McCall8322c3a2011-02-13 04:07:26 +00003211 if (Sub->children()) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003212 OS << "({ ... ; ";
3213 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3214 OS << " })\n";
3215 return;
3216 }
3217 }
3218 // special printing for comma expressions.
3219 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3220 if (B->getOpcode() == BO_Comma) {
3221 OS << "... , ";
3222 Helper->handledStmt(B->getRHS(),OS);
3223 OS << '\n';
3224 return;
3225 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003226 }
3227 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003228 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00003229
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003230 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan59f09c72010-11-22 19:32:14 +00003231 OS << " (OperatorCall)";
3232 } else if (isa<CXXBindTemporaryExpr>(S)) {
3233 OS << " (BindTemporary)";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003234 }
Mike Stump31feda52009-07-17 01:31:16 +00003235
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003236 // Expressions need a newline.
3237 if (isa<Expr>(S))
3238 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00003239
Ted Kremenek96a7a592011-03-01 03:15:10 +00003240 } else if (const CFGInitializer *IE = E.getAs<CFGInitializer>()) {
3241 const CXXCtorInitializer *I = IE->getInitializer();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003242 if (I->isBaseInitializer())
3243 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichetd583da02010-12-04 09:14:42 +00003244 else OS << I->getAnyMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00003245
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003246 OS << "(";
3247 if (Expr* IE = I->getInit())
3248 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3249 OS << ")";
3250
3251 if (I->isBaseInitializer())
3252 OS << " (Base initializer)\n";
3253 else OS << " (Member initializer)\n";
3254
Ted Kremenek96a7a592011-03-01 03:15:10 +00003255 } else if (const CFGAutomaticObjDtor *DE = E.getAs<CFGAutomaticObjDtor>()){
3256 const VarDecl* VD = DE->getVarDecl();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003257 Helper->handleDecl(VD, OS);
3258
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003259 const Type* T = VD->getType().getTypePtr();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003260 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3261 T = RT->getPointeeType().getTypePtr();
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003262 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3263 T = ET;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003264
3265 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3266 OS << " (Implicit destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003267
Ted Kremenek96a7a592011-03-01 03:15:10 +00003268 } else if (const CFGBaseDtor *BE = E.getAs<CFGBaseDtor>()) {
3269 const CXXBaseSpecifier *BS = BE->getBaseSpecifier();
Marcin Swiderski20b88732010-10-05 05:37:00 +00003270 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003271 OS << " (Base object destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003272
Ted Kremenek96a7a592011-03-01 03:15:10 +00003273 } else if (const CFGMemberDtor *ME = E.getAs<CFGMemberDtor>()) {
3274 const FieldDecl *FD = ME->getFieldDecl();
Marcin Swiderski01769902010-10-25 07:05:54 +00003275
3276 const Type *T = FD->getType().getTypePtr();
3277 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3278 T = ET;
3279
Marcin Swiderski20b88732010-10-05 05:37:00 +00003280 OS << "this->" << FD->getName();
Marcin Swiderski01769902010-10-25 07:05:54 +00003281 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003282 OS << " (Member object destructor)\n";
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00003283
Ted Kremenek96a7a592011-03-01 03:15:10 +00003284 } else if (const CFGTemporaryDtor *TE = E.getAs<CFGTemporaryDtor>()) {
3285 const CXXBindTemporaryExpr *BT = TE->getBindTemporaryExpr();
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00003286 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3287 OS << " (Temporary object destructor)\n";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003288 }
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00003289}
Mike Stump31feda52009-07-17 01:31:16 +00003290
Chris Lattnerc61089a2009-06-30 01:26:17 +00003291static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3292 const CFGBlock& B,
3293 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00003294
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003295 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00003296
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003297 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00003298 OS << "\n [ B" << B.getBlockID();
3299
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003300 if (&B == &cfg->getEntry())
3301 OS << " (ENTRY) ]\n";
3302 else if (&B == &cfg->getExit())
3303 OS << " (EXIT) ]\n";
3304 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003305 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003306 else
3307 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00003308
Ted Kremenek71eca012007-08-29 23:20:49 +00003309 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00003310 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003311
3312 if (print_edges)
3313 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003314
Mike Stump92244b02010-01-19 22:00:14 +00003315 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003316 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00003317 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00003318 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003319 C->getLHS()->printPretty(OS, Helper,
3320 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003321 if (C->getRHS()) {
3322 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003323 C->getRHS()->printPretty(OS, Helper,
3324 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003325 }
Mike Stump92244b02010-01-19 22:00:14 +00003326 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003327 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00003328 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003329 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00003330 if (CS->getExceptionDecl())
3331 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3332 0);
3333 else
3334 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003335 OS << ")";
3336
3337 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003338 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00003339
Ted Kremenek71eca012007-08-29 23:20:49 +00003340 OS << ":\n";
3341 }
Mike Stump31feda52009-07-17 01:31:16 +00003342
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003343 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003344 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00003345
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003346 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3347 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00003348
Ted Kremenek71eca012007-08-29 23:20:49 +00003349 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003350 if (print_edges)
3351 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003352
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003353 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00003354
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003355 if (Helper)
3356 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00003357
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003358 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003359 }
Mike Stump31feda52009-07-17 01:31:16 +00003360
Ted Kremenek71eca012007-08-29 23:20:49 +00003361 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003362 if (B.getTerminator()) {
3363 if (print_edges)
3364 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003365
Ted Kremenek71eca012007-08-29 23:20:49 +00003366 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00003367
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003368 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00003369
Chris Lattnerc61089a2009-06-30 01:26:17 +00003370 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3371 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003372 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003373 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003374 }
Mike Stump31feda52009-07-17 01:31:16 +00003375
Ted Kremenek71eca012007-08-29 23:20:49 +00003376 if (print_edges) {
3377 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003378 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00003379 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00003380
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003381 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3382 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003383
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003384 if (i == 8 || (i-8) == 0)
3385 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00003386
Ted Kremenek71eca012007-08-29 23:20:49 +00003387 OS << " B" << (*I)->getBlockID();
3388 }
Mike Stump31feda52009-07-17 01:31:16 +00003389
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003390 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00003391
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003392 // Print the successors of this block.
3393 OS << " Successors (" << B.succ_size() << "):";
3394 i = 0;
3395
3396 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3397 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003398
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003399 if (i == 8 || (i-8) % 10 == 0)
3400 OS << "\n ";
3401
Mike Stump0d76d072009-07-20 23:24:15 +00003402 if (*I)
3403 OS << " B" << (*I)->getBlockID();
3404 else
3405 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003406 }
Mike Stump31feda52009-07-17 01:31:16 +00003407
Ted Kremenek71eca012007-08-29 23:20:49 +00003408 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003409 }
Mike Stump31feda52009-07-17 01:31:16 +00003410}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003411
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003412
3413/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003414void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003415
3416/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003417void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3418 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00003419
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003420 // Print the entry block.
3421 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00003422
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003423 // Iterate through the CFGBlocks and print them one by one.
3424 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3425 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003426 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003427 continue;
Mike Stump31feda52009-07-17 01:31:16 +00003428
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003429 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003430 }
Mike Stump31feda52009-07-17 01:31:16 +00003431
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003432 // Print the exit block.
3433 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00003434 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00003435}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003436
3437/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003438void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3439 print(llvm::errs(), cfg, LO);
3440}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003441
3442/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3443/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003444void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3445 const LangOptions &LO) const {
3446 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003447 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00003448}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003449
Ted Kremenek15647632008-01-30 23:02:42 +00003450/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003451void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00003452 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00003453 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003454 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003455}
3456
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003457Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003458 Stmt *Terminator = this->Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003459 if (!Terminator)
3460 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003461
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003462 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003463
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003464 switch (Terminator->getStmtClass()) {
3465 default:
3466 break;
Mike Stump31feda52009-07-17 01:31:16 +00003467
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003468 case Stmt::ForStmtClass:
3469 E = cast<ForStmt>(Terminator)->getCond();
3470 break;
Mike Stump31feda52009-07-17 01:31:16 +00003471
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003472 case Stmt::WhileStmtClass:
3473 E = cast<WhileStmt>(Terminator)->getCond();
3474 break;
Mike Stump31feda52009-07-17 01:31:16 +00003475
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003476 case Stmt::DoStmtClass:
3477 E = cast<DoStmt>(Terminator)->getCond();
3478 break;
Mike Stump31feda52009-07-17 01:31:16 +00003479
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003480 case Stmt::IfStmtClass:
3481 E = cast<IfStmt>(Terminator)->getCond();
3482 break;
Mike Stump31feda52009-07-17 01:31:16 +00003483
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003484 case Stmt::ChooseExprClass:
3485 E = cast<ChooseExpr>(Terminator)->getCond();
3486 break;
Mike Stump31feda52009-07-17 01:31:16 +00003487
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003488 case Stmt::IndirectGotoStmtClass:
3489 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3490 break;
Mike Stump31feda52009-07-17 01:31:16 +00003491
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003492 case Stmt::SwitchStmtClass:
3493 E = cast<SwitchStmt>(Terminator)->getCond();
3494 break;
Mike Stump31feda52009-07-17 01:31:16 +00003495
John McCallc07a0c72011-02-17 10:25:35 +00003496 case Stmt::BinaryConditionalOperatorClass:
3497 E = cast<BinaryConditionalOperator>(Terminator)->getCond();
3498 break;
3499
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003500 case Stmt::ConditionalOperatorClass:
3501 E = cast<ConditionalOperator>(Terminator)->getCond();
3502 break;
Mike Stump31feda52009-07-17 01:31:16 +00003503
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003504 case Stmt::BinaryOperatorClass: // '&&' and '||'
3505 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003506 break;
Mike Stump31feda52009-07-17 01:31:16 +00003507
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003508 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00003509 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003510 }
Mike Stump31feda52009-07-17 01:31:16 +00003511
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003512 return E ? E->IgnoreParens() : NULL;
3513}
3514
Ted Kremenek92137a32008-05-16 16:06:00 +00003515bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003516 const Stmt *Terminator = this->Terminator;
Ted Kremenek92137a32008-05-16 16:06:00 +00003517 if (!Terminator)
3518 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003519
Ted Kremenek92137a32008-05-16 16:06:00 +00003520 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003521
Ted Kremenek92137a32008-05-16 16:06:00 +00003522 switch (Terminator->getStmtClass()) {
3523 default:
3524 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003525
3526 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003527 case Stmt::WhileStmtClass:
3528 case Stmt::DoStmtClass:
3529 case Stmt::IfStmtClass:
3530 case Stmt::ChooseExprClass:
John McCallc07a0c72011-02-17 10:25:35 +00003531 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003532 case Stmt::ConditionalOperatorClass:
3533 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00003534 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00003535 }
Mike Stump31feda52009-07-17 01:31:16 +00003536
Ted Kremenek92137a32008-05-16 16:06:00 +00003537 return E ? E->IgnoreParens() : NULL;
3538}
3539
Ted Kremenek15647632008-01-30 23:02:42 +00003540
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003541//===----------------------------------------------------------------------===//
3542// CFG Graphviz Visualization
3543//===----------------------------------------------------------------------===//
3544
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003545
3546#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00003547static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003548#endif
3549
Chris Lattnerc61089a2009-06-30 01:26:17 +00003550void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003551#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00003552 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003553 GraphHelper = &H;
3554 llvm::ViewGraph(this,"CFG");
3555 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003556#endif
3557}
3558
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003559namespace llvm {
3560template<>
3561struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00003562
3563 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3564
3565 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003566
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003567#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003568 std::string OutSStr;
3569 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003570 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003571 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003572
3573 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3574
3575 // Process string output to make it nicer...
3576 for (unsigned i = 0; i != OutStr.length(); ++i)
3577 if (OutStr[i] == '\n') { // Left justify
3578 OutStr[i] = '\\';
3579 OutStr.insert(OutStr.begin()+i+1, 'l');
3580 }
Mike Stump31feda52009-07-17 01:31:16 +00003581
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003582 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003583#else
3584 return "";
3585#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003586 }
3587};
3588} // end namespace llvm