blob: 706d69010b47fe34c4eec319a78210650e2e9ec6 [file] [log] [blame]
Ted Kremenekfddd5182007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Kremenekfddd5182007-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
Argyrios Kyrtzidisb2c60b02012-03-01 19:45:56 +000015#include "llvm/Support/SaveAndRestore.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stumpb978a442010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenekc310e932007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Ted Kremenekc56c0042011-02-23 05:11:46 +000020#include "clang/AST/CharUnits.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000021#include "llvm/Support/GraphWriter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000022#include "llvm/Support/Allocator.h"
23#include "llvm/Support/Format.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000024#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000025#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek0ba497b2009-10-20 23:46:25 +000026#include "llvm/ADT/OwningPtr.h"
Ted Kremenek83c01da2008-01-11 00:40:29 +000027
Ted Kremenekfddd5182007-08-21 21:42:03 +000028using namespace clang;
29
30namespace {
31
Ted Kremenek9c378f72011-08-12 23:37:29 +000032static SourceLocation GetEndLoc(Decl *D) {
33 if (VarDecl *VD = dyn_cast<VarDecl>(D))
34 if (Expr *Ex = VD->getInit())
Ted Kremenekc7eb9032008-08-06 23:20:50 +000035 return Ex->getSourceRange().getEnd();
Mike Stump6d9828c2009-07-17 01:31:16 +000036 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000037}
Ted Kremenekad5a8942010-08-02 23:46:59 +000038
Ted Kremenek3179a452011-03-10 01:14:11 +000039class CFGBuilder;
40
Zhanyong Wan94a3dcf2010-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 Kremenek852274d2009-12-16 03:18:58 +000054class AddStmtChoice {
55public:
Ted Kremenek892697d2010-12-16 07:46:53 +000056 enum Kind { NotAlwaysAdd = 0, AlwaysAdd = 1 };
Ted Kremenek5ba290a2010-03-02 21:43:54 +000057
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000058 AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {}
Ted Kremenek5ba290a2010-03-02 21:43:54 +000059
Ted Kremenek3179a452011-03-10 01:14:11 +000060 bool alwaysAdd(CFGBuilder &builder,
61 const Stmt *stmt) const;
Zhanyong Wan94a3dcf2010-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 Kremenek3179a452011-03-10 01:14:11 +000066 return AddStmtChoice(alwaysAdd ? AlwaysAdd : NotAlwaysAdd);
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000067 }
68
Ted Kremenek852274d2009-12-16 03:18:58 +000069private:
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000070 Kind kind;
Ted Kremenek852274d2009-12-16 03:18:58 +000071};
Mike Stump6d9828c2009-07-17 01:31:16 +000072
Marcin Swiderskif1308c72010-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 Swiderski35387a02010-09-30 22:42:32 +000083/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderskif1308c72010-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 Kremenekfe59b742011-02-15 02:47:45 +000096 typedef BumpVector<VarDecl*> AutomaticVarsTy;
Marcin Swiderskif1308c72010-09-25 11:05:21 +000097
98 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderski35387a02010-09-30 22:42:32 +000099 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderskif1308c72010-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
Ted Kremenek9c378f72011-08-12 23:37:29 +0000124 VarDecl *const* operator->() const {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000125 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 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000129 VarDecl *operator*() const {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000130 return *this->operator->();
131 }
132
Ted Kremenek9c378f72011-08-12 23:37:29 +0000133 const_iterator &operator++() {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000134 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 Swiderski35387a02010-09-30 22:42:32 +0000143 const_iterator operator++(int) {
144 const_iterator P = *this;
145 ++*this;
146 return P;
147 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000148
Ted Kremenek9c378f72011-08-12 23:37:29 +0000149 bool operator==(const const_iterator &rhs) const {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000150 return Scope == rhs.Scope && VarIter == rhs.VarIter;
151 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000152 bool operator!=(const const_iterator &rhs) const {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000153 return !(*this == rhs);
154 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000155
156 operator bool() const {
157 return *this != const_iterator();
158 }
159
160 int distance(const_iterator L);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000161 };
162
163 friend class const_iterator;
164
165private:
Ted Kremenekfe59b742011-02-15 02:47:45 +0000166 BumpVectorContext ctx;
167
Marcin Swiderskif1308c72010-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 Kremenekfe59b742011-02-15 02:47:45 +0000176 LocalScope(BumpVectorContext &ctx, const_iterator P)
177 : ctx(ctx), Vars(ctx, 4), Prev(P) {}
Marcin Swiderskif1308c72010-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 Swiderski35387a02010-09-30 22:42:32 +0000181
Ted Kremenek9c378f72011-08-12 23:37:29 +0000182 void addVar(VarDecl *VD) {
Ted Kremenekfe59b742011-02-15 02:47:45 +0000183 Vars.push_back(VD, ctx);
Marcin Swiderski35387a02010-09-30 22:42:32 +0000184 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000185};
186
Marcin Swiderski35387a02010-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) {
Ted Kremenek5290c802011-08-12 14:41:23 +0000194 assert (F != const_iterator()
195 && "L iterator is not reachable from F iterator.");
Marcin Swiderski35387a02010-09-30 22:42:32 +0000196 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 Swiderskif1308c72010-09-25 11:05:21 +0000206struct BlockScopePosPair {
Ted Kremenek9ce52702011-01-07 19:37:16 +0000207 BlockScopePosPair() : block(0) {}
Ted Kremenek9c378f72011-08-12 23:37:29 +0000208 BlockScopePosPair(CFGBlock *b, LocalScope::const_iterator scopePos)
Ted Kremenek9ce52702011-01-07 19:37:16 +0000209 : block(b), scopePosition(scopePos) {}
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000210
Ted Kremenek9ce52702011-01-07 19:37:16 +0000211 CFGBlock *block;
212 LocalScope::const_iterator scopePosition;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000213};
214
Ted Kremeneke71f3d52011-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 Kremeneka34ea072008-08-04 22:51:42 +0000234/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-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 Stump6d9828c2009-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 Kremenekc310e932007-08-21 22:06:14 +0000247///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000248class CFGBuilder {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000249 typedef BlockScopePosPair JumpTarget;
250 typedef BlockScopePosPair JumpSource;
251
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000252 ASTContext *Context;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000253 OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000254
Ted Kremenek9c378f72011-08-12 23:37:29 +0000255 CFGBlock *Block;
256 CFGBlock *Succ;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000257 JumpTarget ContinueJumpTarget;
258 JumpTarget BreakJumpTarget;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000259 CFGBlock *SwitchTerminatedBlock;
260 CFGBlock *DefaultCaseBlock;
261 CFGBlock *TryTerminatedBlock;
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000262
Marcin Swiderskif1308c72010-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 Lattnerad8dcf42011-02-17 07:39:24 +0000267 typedef llvm::DenseMap<LabelDecl*, JumpTarget> LabelMapTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000268 LabelMapTy LabelMap;
Mike Stump6d9828c2009-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 Swiderskif1308c72010-09-25 11:05:21 +0000272 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000273 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +0000274
Ted Kremenek19bb3562007-08-28 19:26:49 +0000275 // A list of labels whose address has been taken (for indirect gotos).
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000276 typedef llvm::SmallPtrSet<LabelDecl*, 5> LabelSetTy;
Ted Kremenek19bb3562007-08-28 19:26:49 +0000277 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +0000278
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000279 bool badCFG;
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +0000280 const CFG::BuildOptions &BuildOpts;
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000281
282 // State to track for building switch statements.
283 bool switchExclusivelyCovered;
Ted Kremenek04982472011-03-04 01:03:41 +0000284 Expr::EvalResult *switchCond;
Ted Kremenek0d28d362011-03-10 03:50:34 +0000285
286 CFG::BuildOptions::ForcedBlkExprs::value_type *cachedEntry;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000287 const Stmt *lastLookup;
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000288
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000289 // Caches boolean evaluations of expressions to avoid multiple re-evaluations
290 // during construction of branches for chained logical operators.
291 llvm::DenseMap<Expr *, TryResult> CachedBoolEvals;
292
Mike Stump6d9828c2009-07-17 01:31:16 +0000293public:
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +0000294 explicit CFGBuilder(ASTContext *astContext,
295 const CFG::BuildOptions &buildOpts)
296 : Context(astContext), cfg(new CFG()), // crew a new CFG
297 Block(NULL), Succ(NULL),
298 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
299 TryTerminatedBlock(NULL), badCFG(false), BuildOpts(buildOpts),
Ted Kremenek0d28d362011-03-10 03:50:34 +0000300 switchExclusivelyCovered(false), switchCond(0),
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000301 cachedEntry(0), lastLookup(0) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000302
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000303 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +0000304 CFG* buildCFG(const Decl *D, Stmt *Statement);
Mike Stump6d9828c2009-07-17 01:31:16 +0000305
Ted Kremenek0d28d362011-03-10 03:50:34 +0000306 bool alwaysAdd(const Stmt *stmt);
307
Ted Kremenek4f880632009-07-17 22:18:43 +0000308private:
309 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000310 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
311 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
Ted Kremenek9c378f72011-08-12 23:37:29 +0000312 CFGBlock *VisitBlockExpr(BlockExpr *E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000313 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000314 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
John McCall4765fa02010-12-06 08:20:24 +0000315 CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +0000316 AddStmtChoice asc);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000317 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
318 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Richard Smithad762fc2011-04-14 22:09:26 +0000319 CFGBlock *VisitCXXForRangeStmt(CXXForRangeStmt *S);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000320 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
321 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000322 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000323 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
324 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000325 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
326 AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000327 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000328 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000329 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000330 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
John McCall56ca35d2011-02-17 10:25:35 +0000331 CFGBlock *VisitConditionalOperator(AbstractConditionalOperator *C,
332 AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000333 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000334 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Ted Kremenek9c378f72011-08-12 23:37:29 +0000335 CFGBlock *VisitDeclSubExpr(DeclStmt *DS);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000336 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
337 CFGBlock *VisitDoStmt(DoStmt *D);
338 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek9c378f72011-08-12 23:37:29 +0000339 CFGBlock *VisitGotoStmt(GotoStmt *G);
Ted Kremenek4f880632009-07-17 22:18:43 +0000340 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000341 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000342 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
343 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000344 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000345 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
Ted Kremenek8e282c32012-03-06 23:40:47 +0000346 CFGBlock *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Ted Kremenek4f880632009-07-17 22:18:43 +0000347 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
348 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
349 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
350 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
Ted Kremenek9c378f72011-08-12 23:37:29 +0000351 CFGBlock *VisitReturnStmt(ReturnStmt *R);
John McCall4b9c2d22011-11-06 09:01:30 +0000352 CFGBlock *VisitPseudoObjectExpr(PseudoObjectExpr *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000353 CFGBlock *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E,
354 AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000355 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000356 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000357 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000358 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000359
Ted Kremenek852274d2009-12-16 03:18:58 +0000360 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
361 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek9c378f72011-08-12 23:37:29 +0000362 CFGBlock *VisitChildren(Stmt *S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000363
Marcin Swiderski8599e762010-11-03 06:19:35 +0000364 // Visitors to walk an AST and generate destructors of temporaries in
365 // full expression.
366 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
367 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
368 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
369 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
370 bool BindToTemporary);
John McCall56ca35d2011-02-17 10:25:35 +0000371 CFGBlock *
372 VisitConditionalOperatorForTemporaryDtors(AbstractConditionalOperator *E,
373 bool BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000374
Ted Kremenek274f4332008-04-28 18:00:46 +0000375 // NYS == Not Yet Supported
Ted Kremenek9c378f72011-08-12 23:37:29 +0000376 CFGBlock *NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000377 badCFG = true;
378 return Block;
379 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000380
Ted Kremenek4f880632009-07-17 22:18:43 +0000381 void autoCreateBlock() { if (!Block) Block = createBlock(); }
382 CFGBlock *createBlock(bool add_successor = true);
Chandler Carruthdba3fb52011-09-13 09:13:49 +0000383 CFGBlock *createNoReturnBlock();
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000384
Zhongxing Xudf119892010-06-03 06:43:23 +0000385 CFGBlock *addStmt(Stmt *S) {
386 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000387 }
Sean Huntcbb67482011-01-08 20:30:50 +0000388 CFGBlock *addInitializer(CXXCtorInitializer *I);
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000389 void addAutomaticObjDtors(LocalScope::const_iterator B,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000390 LocalScope::const_iterator E, Stmt *S);
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000391 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekad5a8942010-08-02 23:46:59 +0000392
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000393 // Local scopes creation.
394 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
395
Ted Kremenek9c378f72011-08-12 23:37:29 +0000396 void addLocalScopeForStmt(Stmt *S);
397 LocalScope* addLocalScopeForDeclStmt(DeclStmt *DS, LocalScope* Scope = NULL);
398 LocalScope* addLocalScopeForVarDecl(VarDecl *VD, LocalScope* Scope = NULL);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000399
Ted Kremenek9c378f72011-08-12 23:37:29 +0000400 void addLocalScopeAndDtors(Stmt *S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000401
402 // Interface to CFGBlock - adding CFGElements.
Ted Kremenekd40066b2011-04-04 23:29:12 +0000403 void appendStmt(CFGBlock *B, const Stmt *S) {
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000404 if (alwaysAdd(S) && cachedEntry)
Ted Kremenek0d28d362011-03-10 03:50:34 +0000405 cachedEntry->second = B;
Ted Kremenek0d28d362011-03-10 03:50:34 +0000406
Jordy Roseac73ea82011-06-10 08:49:37 +0000407 // All block-level expressions should have already been IgnoreParens()ed.
408 assert(!isa<Expr>(S) || cast<Expr>(S)->IgnoreParens() == S);
Ted Kremenekd40066b2011-04-04 23:29:12 +0000409 B->appendStmt(const_cast<Stmt*>(S), cfg->getBumpVectorContext());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000410 }
Sean Huntcbb67482011-01-08 20:30:50 +0000411 void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) {
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000412 B->appendInitializer(I, cfg->getBumpVectorContext());
413 }
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000414 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
415 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
416 }
417 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
418 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
419 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000420 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
421 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
422 }
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000423 void appendAutomaticObjDtor(CFGBlock *B, VarDecl *VD, Stmt *S) {
424 B->appendAutomaticObjDtor(VD, S, cfg->getBumpVectorContext());
425 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000426
Ted Kremenek9c378f72011-08-12 23:37:29 +0000427 void prependAutomaticObjDtorsWithTerminator(CFGBlock *Blk,
Marcin Swiderski53de1342010-09-30 22:54:37 +0000428 LocalScope::const_iterator B, LocalScope::const_iterator E);
429
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000430 void addSuccessor(CFGBlock *B, CFGBlock *S) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000431 B->addSuccessor(S, cfg->getBumpVectorContext());
432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000434 /// Try and evaluate an expression to an integer constant.
435 bool tryEvaluate(Expr *S, Expr::EvalResult &outResult) {
436 if (!BuildOpts.PruneTriviallyFalseEdges)
437 return false;
438 return !S->isTypeDependent() &&
Ted Kremenekf8adeef2011-04-04 20:30:58 +0000439 !S->isValueDependent() &&
Richard Smith51f47082011-10-29 00:50:52 +0000440 S->EvaluateAsRValue(outResult, *Context);
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000441 }
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000443 /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
Mike Stump00998a02009-07-23 23:25:26 +0000444 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000445 TryResult tryEvaluateBool(Expr *S) {
Richard Smith85df96c2011-10-14 20:22:00 +0000446 if (!BuildOpts.PruneTriviallyFalseEdges ||
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000447 S->isTypeDependent() || S->isValueDependent())
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000448 return TryResult();
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000449
450 if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(S)) {
451 if (Bop->isLogicalOp()) {
452 // Check the cache first.
453 typedef llvm::DenseMap<Expr *, TryResult>::iterator eval_iterator;
454 eval_iterator I;
455 bool Inserted;
456 llvm::tie(I, Inserted) =
457 CachedBoolEvals.insert(std::make_pair(S, TryResult()));
458 if (!Inserted)
459 return I->second; // already in map;
460
461 return (I->second = evaluateAsBooleanConditionNoCache(S));
462 }
463 }
464
465 return evaluateAsBooleanConditionNoCache(S);
466 }
467
468 /// \brief Evaluate as boolean \param E without using the cache.
469 TryResult evaluateAsBooleanConditionNoCache(Expr *E) {
470 if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(E)) {
471 if (Bop->isLogicalOp()) {
472 TryResult LHS = tryEvaluateBool(Bop->getLHS());
473 if (LHS.isKnown()) {
474 // We were able to evaluate the LHS, see if we can get away with not
475 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
476 if (LHS.isTrue() == (Bop->getOpcode() == BO_LOr))
477 return LHS.isTrue();
478
479 TryResult RHS = tryEvaluateBool(Bop->getRHS());
480 if (RHS.isKnown()) {
481 if (Bop->getOpcode() == BO_LOr)
482 return LHS.isTrue() || RHS.isTrue();
483 else
484 return LHS.isTrue() && RHS.isTrue();
485 }
486 } else {
487 TryResult RHS = tryEvaluateBool(Bop->getRHS());
488 if (RHS.isKnown()) {
489 // We can't evaluate the LHS; however, sometimes the result
490 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
491 if (RHS.isTrue() == (Bop->getOpcode() == BO_LOr))
492 return RHS.isTrue();
493 }
494 }
495
496 return TryResult();
497 }
498 }
499
500 bool Result;
501 if (E->EvaluateAsBooleanCondition(Result, *Context))
502 return Result;
503
504 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000505 }
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000506
Ted Kremenekfddd5182007-08-21 21:42:03 +0000507};
Mike Stump6d9828c2009-07-17 01:31:16 +0000508
Ted Kremenek0d28d362011-03-10 03:50:34 +0000509inline bool AddStmtChoice::alwaysAdd(CFGBuilder &builder,
510 const Stmt *stmt) const {
511 return builder.alwaysAdd(stmt) || kind == AlwaysAdd;
512}
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000513
Ted Kremenek0d28d362011-03-10 03:50:34 +0000514bool CFGBuilder::alwaysAdd(const Stmt *stmt) {
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000515 bool shouldAdd = BuildOpts.alwaysAdd(stmt);
516
Ted Kremenek0d28d362011-03-10 03:50:34 +0000517 if (!BuildOpts.forcedBlkExprs)
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000518 return shouldAdd;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000519
520 if (lastLookup == stmt) {
521 if (cachedEntry) {
522 assert(cachedEntry->first == stmt);
523 return true;
524 }
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000525 return shouldAdd;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000526 }
Ted Kremenek0d28d362011-03-10 03:50:34 +0000527
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000528 lastLookup = stmt;
529
530 // Perform the lookup!
Ted Kremenek0d28d362011-03-10 03:50:34 +0000531 CFG::BuildOptions::ForcedBlkExprs *fb = *BuildOpts.forcedBlkExprs;
532
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000533 if (!fb) {
534 // No need to update 'cachedEntry', since it will always be null.
535 assert(cachedEntry == 0);
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000536 return shouldAdd;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000537 }
Ted Kremenek0d28d362011-03-10 03:50:34 +0000538
539 CFG::BuildOptions::ForcedBlkExprs::iterator itr = fb->find(stmt);
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000540 if (itr == fb->end()) {
541 cachedEntry = 0;
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000542 return shouldAdd;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000543 }
544
Ted Kremenek0d28d362011-03-10 03:50:34 +0000545 cachedEntry = &*itr;
546 return true;
Ted Kremenek3179a452011-03-10 01:14:11 +0000547}
548
Douglas Gregor898574e2008-12-05 23:32:09 +0000549// FIXME: Add support for dependent-sized array types in C++?
550// Does it even make sense to build a CFG for an uninstantiated template?
John McCallf4c73712011-01-19 06:33:43 +0000551static const VariableArrayType *FindVA(const Type *t) {
552 while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
553 if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
Ted Kremenek610a09e2008-09-26 22:58:57 +0000554 if (vat->getSizeExpr())
555 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000556
Ted Kremenek610a09e2008-09-26 22:58:57 +0000557 t = vt->getElementType().getTypePtr();
558 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000559
Ted Kremenek610a09e2008-09-26 22:58:57 +0000560 return 0;
561}
Mike Stump6d9828c2009-07-17 01:31:16 +0000562
563/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
564/// arbitrary statement. Examples include a single expression or a function
565/// body (compound statement). The ownership of the returned CFG is
566/// transferred to the caller. If CFG construction fails, this method returns
567/// NULL.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000568CFG* CFGBuilder::buildCFG(const Decl *D, Stmt *Statement) {
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000569 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000570 if (!Statement)
571 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000572
Mike Stump6d9828c2009-07-17 01:31:16 +0000573 // Create an empty block that will serve as the exit block for the CFG. Since
574 // this is the first block added to the CFG, it will be implicitly registered
575 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000576 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000577 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000578 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000579
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000580 if (BuildOpts.AddImplicitDtors)
581 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
582 addImplicitDtorsForDestructor(DD);
583
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000584 // Visit the statements and create the CFG.
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000585 CFGBlock *B = addStmt(Statement);
586
587 if (badCFG)
588 return NULL;
589
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000590 // For C++ constructor add initializers to CFG.
591 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
592 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
593 E = CD->init_rend(); I != E; ++I) {
594 B = addInitializer(*I);
595 if (badCFG)
596 return NULL;
597 }
598 }
599
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000600 if (B)
601 Succ = B;
Mike Stumpb978a442010-01-21 02:21:40 +0000602
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000603 // Backpatch the gotos whose label -> block mappings we didn't know when we
604 // encountered them.
605 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
606 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000607
Ted Kremenek9c378f72011-08-12 23:37:29 +0000608 CFGBlock *B = I->block;
609 GotoStmt *G = cast<GotoStmt>(B->getTerminator());
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000610 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000611
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000612 // If there is no target for the goto, then we are looking at an
613 // incomplete AST. Handle this by not registering a successor.
614 if (LI == LabelMap.end()) continue;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000615
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000616 JumpTarget JT = LI->second;
Ted Kremenek9ce52702011-01-07 19:37:16 +0000617 prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
618 JT.scopePosition);
619 addSuccessor(B, JT.block);
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000620 }
621
622 // Add successors to the Indirect Goto Dispatch block (if we have one).
Ted Kremenek9c378f72011-08-12 23:37:29 +0000623 if (CFGBlock *B = cfg->getIndirectGotoBlock())
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000624 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
625 E = AddressTakenLabels.end(); I != E; ++I ) {
626
627 // Lookup the target block.
628 LabelMapTy::iterator LI = LabelMap.find(*I);
629
630 // If there is no target block that contains label, then we are looking
631 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000632 if (LI == LabelMap.end()) continue;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000633
Ted Kremenek9ce52702011-01-07 19:37:16 +0000634 addSuccessor(B, LI->second.block);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000635 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000636
Mike Stump6d9828c2009-07-17 01:31:16 +0000637 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000638 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000639
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000640 return cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000641}
Mike Stump6d9828c2009-07-17 01:31:16 +0000642
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000643/// createBlock - Used to lazily create blocks that are connected
644/// to the current (global) succcessor.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000645CFGBlock *CFGBuilder::createBlock(bool add_successor) {
646 CFGBlock *B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000647 if (add_successor && Succ)
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000648 addSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000649 return B;
650}
Mike Stump6d9828c2009-07-17 01:31:16 +0000651
Chandler Carruthdba3fb52011-09-13 09:13:49 +0000652/// createNoReturnBlock - Used to create a block is a 'noreturn' point in the
653/// CFG. It is *not* connected to the current (global) successor, and instead
654/// directly tied to the exit block in order to be reachable.
655CFGBlock *CFGBuilder::createNoReturnBlock() {
656 CFGBlock *B = createBlock(false);
Chandler Carruth83754162011-09-13 09:53:55 +0000657 B->setHasNoReturnElement();
Chandler Carruthdba3fb52011-09-13 09:13:49 +0000658 addSuccessor(B, &cfg->getExit());
659 return B;
660}
661
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000662/// addInitializer - Add C++ base or member initializer element to CFG.
Sean Huntcbb67482011-01-08 20:30:50 +0000663CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) {
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000664 if (!BuildOpts.AddInitializers)
665 return Block;
666
Marcin Swiderski8599e762010-11-03 06:19:35 +0000667 bool IsReference = false;
668 bool HasTemporaries = false;
669
670 // Destructors of temporaries in initialization expression should be called
671 // after initialization finishes.
672 Expr *Init = I->getInit();
673 if (Init) {
Francois Pichet00eb3f92010-12-04 09:14:42 +0000674 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski8599e762010-11-03 06:19:35 +0000675 IsReference = FD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +0000676 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000677
678 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
679 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +0000680 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +0000681 IsReference);
682 }
683 }
684
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000685 autoCreateBlock();
686 appendInitializer(Block, I);
687
Marcin Swiderski8599e762010-11-03 06:19:35 +0000688 if (Init) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000689 if (HasTemporaries) {
Marcin Swiderski8599e762010-11-03 06:19:35 +0000690 // For expression with temporaries go directly to subexpression to omit
691 // generating destructors for the second time.
Ted Kremenek892697d2010-12-16 07:46:53 +0000692 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
693 }
694 return Visit(Init);
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000695 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000696
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000697 return Block;
698}
699
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000700/// \brief Retrieve the type of the temporary object whose lifetime was
701/// extended by a local reference with the given initializer.
702static QualType getReferenceInitTemporaryType(ASTContext &Context,
703 const Expr *Init) {
704 while (true) {
705 // Skip parentheses.
706 Init = Init->IgnoreParens();
707
708 // Skip through cleanups.
709 if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Init)) {
710 Init = EWC->getSubExpr();
711 continue;
712 }
713
714 // Skip through the temporary-materialization expression.
715 if (const MaterializeTemporaryExpr *MTE
716 = dyn_cast<MaterializeTemporaryExpr>(Init)) {
717 Init = MTE->GetTemporaryExpr();
718 continue;
719 }
720
721 // Skip derived-to-base and no-op casts.
722 if (const CastExpr *CE = dyn_cast<CastExpr>(Init)) {
723 if ((CE->getCastKind() == CK_DerivedToBase ||
724 CE->getCastKind() == CK_UncheckedDerivedToBase ||
725 CE->getCastKind() == CK_NoOp) &&
726 Init->getType()->isRecordType()) {
727 Init = CE->getSubExpr();
728 continue;
729 }
730 }
731
732 // Skip member accesses into rvalues.
733 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Init)) {
734 if (!ME->isArrow() && ME->getBase()->isRValue()) {
735 Init = ME->getBase();
736 continue;
737 }
738 }
739
740 break;
741 }
742
743 return Init->getType();
744}
745
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000746/// addAutomaticObjDtors - Add to current block automatic objects destructors
747/// for objects in range of local scope positions. Use S as trigger statement
748/// for destructors.
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000749void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000750 LocalScope::const_iterator E, Stmt *S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000751 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000752 return;
753
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000754 if (B == E)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000755 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000756
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000757 // We need to append the destructors in reverse order, but any one of them
758 // may be a no-return destructor which changes the CFG. As a result, buffer
759 // this sequence up and replay them in reverse order when appending onto the
760 // CFGBlock(s).
761 SmallVector<VarDecl*, 10> Decls;
762 Decls.reserve(B.distance(E));
763 for (LocalScope::const_iterator I = B; I != E; ++I)
764 Decls.push_back(*I);
765
766 for (SmallVectorImpl<VarDecl*>::reverse_iterator I = Decls.rbegin(),
767 E = Decls.rend();
768 I != E; ++I) {
769 // If this destructor is marked as a no-return destructor, we need to
770 // create a new block for the destructor which does not have as a successor
771 // anything built thus far: control won't flow out of this block.
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000772 QualType Ty;
773 if ((*I)->getType()->isReferenceType()) {
774 Ty = getReferenceInitTemporaryType(*Context, (*I)->getInit());
775 } else {
776 Ty = Context->getBaseElementType((*I)->getType());
777 }
778
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000779 const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor();
David Blaikie23661d32012-01-24 04:51:48 +0000780 if (cast<FunctionType>(Dtor->getType())->getNoReturnAttr())
Chandler Carruthdba3fb52011-09-13 09:13:49 +0000781 Block = createNoReturnBlock();
782 else
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000783 autoCreateBlock();
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000784
785 appendAutomaticObjDtor(Block, *I, S);
786 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000787}
788
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000789/// addImplicitDtorsForDestructor - Add implicit destructors generated for
790/// base and member objects in destructor.
791void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
792 assert (BuildOpts.AddImplicitDtors
793 && "Can be called only when dtors should be added");
794 const CXXRecordDecl *RD = DD->getParent();
795
796 // At the end destroy virtual base objects.
797 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
798 VE = RD->vbases_end(); VI != VE; ++VI) {
799 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
800 if (!CD->hasTrivialDestructor()) {
801 autoCreateBlock();
802 appendBaseDtor(Block, VI);
803 }
804 }
805
806 // Before virtual bases destroy direct base objects.
807 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
808 BE = RD->bases_end(); BI != BE; ++BI) {
David Blaikie23661d32012-01-24 04:51:48 +0000809 if (!BI->isVirtual()) {
810 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
811 if (!CD->hasTrivialDestructor()) {
812 autoCreateBlock();
813 appendBaseDtor(Block, BI);
814 }
815 }
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000816 }
817
818 // First destroy member objects.
819 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
820 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +0000821 // Check for constant size array. Set type to array element type.
822 QualType QT = FI->getType();
823 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
824 if (AT->getSize() == 0)
825 continue;
826 QT = AT->getElementType();
827 }
828
829 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000830 if (!CD->hasTrivialDestructor()) {
831 autoCreateBlock();
832 appendMemberDtor(Block, *FI);
833 }
834 }
835}
836
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000837/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
838/// way return valid LocalScope object.
839LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
840 if (!Scope) {
Ted Kremenekfe59b742011-02-15 02:47:45 +0000841 llvm::BumpPtrAllocator &alloc = cfg->getAllocator();
842 Scope = alloc.Allocate<LocalScope>();
843 BumpVectorContext ctx(alloc);
844 new (Scope) LocalScope(ctx, ScopePos);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000845 }
846 return Scope;
847}
848
849/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000850/// that should create implicit scope (e.g. if/else substatements).
Ted Kremenek9c378f72011-08-12 23:37:29 +0000851void CFGBuilder::addLocalScopeForStmt(Stmt *S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000852 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000853 return;
854
855 LocalScope *Scope = 0;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000856
857 // For compound statement we will be creating explicit scope.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000858 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000859 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
860 ; BI != BE; ++BI) {
Chandler Carrutha1364be2011-09-10 00:02:34 +0000861 Stmt *SI = (*BI)->stripLabelLikeStatements();
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000862 if (DeclStmt *DS = dyn_cast<DeclStmt>(SI))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000863 Scope = addLocalScopeForDeclStmt(DS, Scope);
864 }
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000865 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000866 }
867
868 // For any other statement scope will be implicit and as such will be
869 // interesting only for DeclStmt.
Chandler Carrutha1364be2011-09-10 00:02:34 +0000870 if (DeclStmt *DS = dyn_cast<DeclStmt>(S->stripLabelLikeStatements()))
Zhongxing Xub6edff52010-10-01 03:09:09 +0000871 addLocalScopeForDeclStmt(DS);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000872}
873
874/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
875/// reuse Scope if not NULL.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000876LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt *DS,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000877 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000878 if (!BuildOpts.AddImplicitDtors)
879 return Scope;
880
881 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
882 ; DI != DE; ++DI) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000883 if (VarDecl *VD = dyn_cast<VarDecl>(*DI))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000884 Scope = addLocalScopeForVarDecl(VD, Scope);
885 }
886 return Scope;
887}
888
889/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
890/// create add scope for automatic objects and temporary objects bound to
891/// const reference. Will reuse Scope if not NULL.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000892LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl *VD,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000893 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000894 if (!BuildOpts.AddImplicitDtors)
895 return Scope;
896
897 // Check if variable is local.
898 switch (VD->getStorageClass()) {
899 case SC_None:
900 case SC_Auto:
901 case SC_Register:
902 break;
903 default: return Scope;
904 }
905
906 // Check for const references bound to temporary. Set type to pointee.
907 QualType QT = VD->getType();
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000908 if (QT.getTypePtr()->isReferenceType()) {
Douglas Gregor03e80032011-06-21 17:03:29 +0000909 if (!VD->extendsLifetimeOfTemporary())
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000910 return Scope;
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000911
912 QT = getReferenceInitTemporaryType(*Context, VD->getInit());
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000913 }
914
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000915 // Check for constant size array. Set type to array element type.
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000916 while (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000917 if (AT->getSize() == 0)
918 return Scope;
919 QT = AT->getElementType();
920 }
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000921
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000922 // Check if type is a C++ class with non-trivial destructor.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000923 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
David Blaikie23661d32012-01-24 04:51:48 +0000924 if (!CD->hasTrivialDestructor()) {
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000925 // Add the variable to scope
926 Scope = createOrReuseLocalScope(Scope);
927 Scope->addVar(VD);
928 ScopePos = Scope->begin();
929 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000930 return Scope;
931}
932
933/// addLocalScopeAndDtors - For given statement add local scope for it and
934/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000935void CFGBuilder::addLocalScopeAndDtors(Stmt *S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000936 if (!BuildOpts.AddImplicitDtors)
937 return;
938
939 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000940 addLocalScopeForStmt(S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000941 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
942}
943
Marcin Swiderski53de1342010-09-30 22:54:37 +0000944/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
945/// variables with automatic storage duration to CFGBlock's elements vector.
946/// Elements will be prepended to physical beginning of the vector which
947/// happens to be logical end. Use blocks terminator as statement that specifies
948/// destructors call site.
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000949/// FIXME: This mechanism for adding automatic destructors doesn't handle
950/// no-return destructors properly.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000951void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock *Blk,
Marcin Swiderski53de1342010-09-30 22:54:37 +0000952 LocalScope::const_iterator B, LocalScope::const_iterator E) {
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000953 BumpVectorContext &C = cfg->getBumpVectorContext();
954 CFGBlock::iterator InsertPos
955 = Blk->beginAutomaticObjDtorsInsert(Blk->end(), B.distance(E), C);
956 for (LocalScope::const_iterator I = B; I != E; ++I)
957 InsertPos = Blk->insertAutomaticObjDtor(InsertPos, *I,
958 Blk->getTerminator());
Marcin Swiderski53de1342010-09-30 22:54:37 +0000959}
960
Ted Kremenek4f880632009-07-17 22:18:43 +0000961/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000962/// blocks for ternary operators, &&, and ||. We also process "," and
963/// DeclStmts (which may contain nested control-flow).
Ted Kremenek9c378f72011-08-12 23:37:29 +0000964CFGBlock *CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenekf42e3372010-04-30 22:25:53 +0000965 if (!S) {
966 badCFG = true;
967 return 0;
968 }
Jordy Roseac73ea82011-06-10 08:49:37 +0000969
970 if (Expr *E = dyn_cast<Expr>(S))
971 S = E->IgnoreParens();
972
Ted Kremenek4f880632009-07-17 22:18:43 +0000973 switch (S->getStmtClass()) {
974 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000975 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000976
977 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000978 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000979
John McCall56ca35d2011-02-17 10:25:35 +0000980 case Stmt::BinaryConditionalOperatorClass:
981 return VisitConditionalOperator(cast<BinaryConditionalOperator>(S), asc);
982
Ted Kremenek4f880632009-07-17 22:18:43 +0000983 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000984 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Ted Kremenek4f880632009-07-17 22:18:43 +0000986 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000987 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000988
Ted Kremenek4f880632009-07-17 22:18:43 +0000989 case Stmt::BreakStmtClass:
990 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Ted Kremenek4f880632009-07-17 22:18:43 +0000992 case Stmt::CallExprClass:
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000993 case Stmt::CXXOperatorCallExprClass:
John McCall1de85332011-05-11 07:19:11 +0000994 case Stmt::CXXMemberCallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +0000995 case Stmt::UserDefinedLiteralClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000996 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Ted Kremenek4f880632009-07-17 22:18:43 +0000998 case Stmt::CaseStmtClass:
999 return VisitCaseStmt(cast<CaseStmt>(S));
1000
1001 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +00001002 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Ted Kremenek4f880632009-07-17 22:18:43 +00001004 case Stmt::CompoundStmtClass:
1005 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Ted Kremenek4f880632009-07-17 22:18:43 +00001007 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +00001008 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Ted Kremenek4f880632009-07-17 22:18:43 +00001010 case Stmt::ContinueStmtClass:
1011 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Ted Kremenek021c8af2010-01-19 20:40:33 +00001013 case Stmt::CXXCatchStmtClass:
1014 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
1015
John McCall4765fa02010-12-06 08:20:24 +00001016 case Stmt::ExprWithCleanupsClass:
1017 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +00001018
Zhongxing Xua725ed42010-11-01 13:04:58 +00001019 case Stmt::CXXBindTemporaryExprClass:
1020 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
1021
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00001022 case Stmt::CXXConstructExprClass:
1023 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
1024
Zhongxing Xua725ed42010-11-01 13:04:58 +00001025 case Stmt::CXXFunctionalCastExprClass:
1026 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
1027
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00001028 case Stmt::CXXTemporaryObjectExprClass:
1029 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
1030
Ted Kremenek021c8af2010-01-19 20:40:33 +00001031 case Stmt::CXXThrowExprClass:
1032 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +00001033
Ted Kremenek021c8af2010-01-19 20:40:33 +00001034 case Stmt::CXXTryStmtClass:
1035 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +00001036
Richard Smithad762fc2011-04-14 22:09:26 +00001037 case Stmt::CXXForRangeStmtClass:
1038 return VisitCXXForRangeStmt(cast<CXXForRangeStmt>(S));
1039
Ted Kremenek4f880632009-07-17 22:18:43 +00001040 case Stmt::DeclStmtClass:
1041 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Ted Kremenek4f880632009-07-17 22:18:43 +00001043 case Stmt::DefaultStmtClass:
1044 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Ted Kremenek4f880632009-07-17 22:18:43 +00001046 case Stmt::DoStmtClass:
1047 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Ted Kremenek4f880632009-07-17 22:18:43 +00001049 case Stmt::ForStmtClass:
1050 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Ted Kremenek4f880632009-07-17 22:18:43 +00001052 case Stmt::GotoStmtClass:
1053 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Ted Kremenek4f880632009-07-17 22:18:43 +00001055 case Stmt::IfStmtClass:
1056 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Ted Kremenek892697d2010-12-16 07:46:53 +00001058 case Stmt::ImplicitCastExprClass:
1059 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00001060
Ted Kremenek4f880632009-07-17 22:18:43 +00001061 case Stmt::IndirectGotoStmtClass:
1062 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremenek4f880632009-07-17 22:18:43 +00001064 case Stmt::LabelStmtClass:
1065 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Ted Kremenek115c1b92010-04-11 17:02:10 +00001067 case Stmt::MemberExprClass:
1068 return VisitMemberExpr(cast<MemberExpr>(S), asc);
1069
Ted Kremenek6a9065a2011-11-05 00:10:15 +00001070 case Stmt::NullStmtClass:
1071 return Block;
1072
Ted Kremenek4f880632009-07-17 22:18:43 +00001073 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +00001074 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
1075
Ted Kremenek8e282c32012-03-06 23:40:47 +00001076 case Stmt::ObjCAutoreleasePoolStmtClass:
1077 return VisitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(S));
1078
Ted Kremenek4f880632009-07-17 22:18:43 +00001079 case Stmt::ObjCAtSynchronizedStmtClass:
1080 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Ted Kremenek4f880632009-07-17 22:18:43 +00001082 case Stmt::ObjCAtThrowStmtClass:
1083 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Ted Kremenek4f880632009-07-17 22:18:43 +00001085 case Stmt::ObjCAtTryStmtClass:
1086 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Ted Kremenek4f880632009-07-17 22:18:43 +00001088 case Stmt::ObjCForCollectionStmtClass:
1089 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Ted Kremenek6a9065a2011-11-05 00:10:15 +00001091 case Stmt::OpaqueValueExprClass:
Ted Kremenek4f880632009-07-17 22:18:43 +00001092 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +00001093
John McCall4b9c2d22011-11-06 09:01:30 +00001094 case Stmt::PseudoObjectExprClass:
1095 return VisitPseudoObjectExpr(cast<PseudoObjectExpr>(S));
1096
Ted Kremenek4f880632009-07-17 22:18:43 +00001097 case Stmt::ReturnStmtClass:
1098 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001100 case Stmt::UnaryExprOrTypeTraitExprClass:
1101 return VisitUnaryExprOrTypeTraitExpr(cast<UnaryExprOrTypeTraitExpr>(S),
1102 asc);
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Ted Kremenek4f880632009-07-17 22:18:43 +00001104 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +00001105 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +00001106
Ted Kremenek4f880632009-07-17 22:18:43 +00001107 case Stmt::SwitchStmtClass:
1108 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001110 case Stmt::UnaryOperatorClass:
1111 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
1112
Ted Kremenek4f880632009-07-17 22:18:43 +00001113 case Stmt::WhileStmtClass:
1114 return VisitWhileStmt(cast<WhileStmt>(S));
1115 }
1116}
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Ted Kremenek852274d2009-12-16 03:18:58 +00001118CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001119 if (asc.alwaysAdd(*this, S)) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001120 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001121 appendStmt(Block, S);
Mike Stump6d9828c2009-07-17 01:31:16 +00001122 }
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Ted Kremenek4f880632009-07-17 22:18:43 +00001124 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001125}
Mike Stump6d9828c2009-07-17 01:31:16 +00001126
Ted Kremenek4f880632009-07-17 22:18:43 +00001127/// VisitChildren - Visit the children of a Stmt.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001128CFGBlock *CFGBuilder::VisitChildren(Stmt *Terminator) {
Ted Kremenek6b12da92011-02-21 22:11:26 +00001129 CFGBlock *lastBlock = Block;
1130 for (Stmt::child_range I = Terminator->children(); I; ++I)
1131 if (Stmt *child = *I)
1132 if (CFGBlock *b = Visit(child))
1133 lastBlock = b;
1134
1135 return lastBlock;
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001136}
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Ted Kremenek852274d2009-12-16 03:18:58 +00001138CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
1139 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001140 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001141
Ted Kremenek3179a452011-03-10 01:14:11 +00001142 if (asc.alwaysAdd(*this, A)) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001143 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001144 appendStmt(Block, A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001145 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001146
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001147 return Block;
1148}
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001150CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek892697d2010-12-16 07:46:53 +00001151 AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001152 if (asc.alwaysAdd(*this, U)) {
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001153 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001154 appendStmt(Block, U);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001155 }
1156
Ted Kremenek892697d2010-12-16 07:46:53 +00001157 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001158}
1159
Ted Kremenek852274d2009-12-16 03:18:58 +00001160CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
1161 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001162 if (B->isLogicalOp()) { // && or ||
Ted Kremenek9c378f72011-08-12 23:37:29 +00001163 CFGBlock *ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001164 appendStmt(ConfluenceBlock, B);
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001166 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001167 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Ted Kremenek4f880632009-07-17 22:18:43 +00001169 // create the block evaluating the LHS
Ted Kremenek9c378f72011-08-12 23:37:29 +00001170 CFGBlock *LHSBlock = createBlock(false);
Ted Kremenek4f880632009-07-17 22:18:43 +00001171 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Ted Kremenek4f880632009-07-17 22:18:43 +00001173 // create the block evaluating the RHS
1174 Succ = ConfluenceBlock;
1175 Block = NULL;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001176 CFGBlock *RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +00001177
1178 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001179 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +00001180 return 0;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001181 } else {
Ted Kremenek862b24f2010-04-29 01:10:26 +00001182 // Create an empty block for cases where the RHS doesn't require
1183 // any explicit statements in the CFG.
1184 RHSBlock = createBlock();
1185 }
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +00001187 // Generate the blocks for evaluating the LHS.
1188 Block = LHSBlock;
1189 CFGBlock *EntryLHSBlock = addStmt(B->getLHS());
1190
Mike Stump00998a02009-07-23 23:25:26 +00001191 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001192 TryResult KnownVal = tryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +00001193 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +00001194 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +00001195
Ted Kremenek4f880632009-07-17 22:18:43 +00001196 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +00001197 if (B->getOpcode() == BO_LOr) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001198 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
1199 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001200 } else {
John McCall2de56d12010-08-25 11:45:40 +00001201 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001202 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
1203 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001204 }
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +00001206 return EntryLHSBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001207 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001208
1209 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +00001210 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001211 appendStmt(Block, B);
Ted Kremenek4f880632009-07-17 22:18:43 +00001212 addStmt(B->getRHS());
1213 return addStmt(B->getLHS());
1214 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001215
1216 if (B->isAssignmentOp()) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001217 if (asc.alwaysAdd(*this, B)) {
Zhongxing Xufc61d942010-06-03 06:23:18 +00001218 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001219 appendStmt(Block, B);
Zhongxing Xufc61d942010-06-03 06:23:18 +00001220 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001221 Visit(B->getLHS());
Marcin Swiderskie1667192010-10-24 08:21:40 +00001222 return Visit(B->getRHS());
Zhongxing Xufc61d942010-06-03 06:23:18 +00001223 }
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Ted Kremenek3179a452011-03-10 01:14:11 +00001225 if (asc.alwaysAdd(*this, B)) {
Marcin Swiderskie1667192010-10-24 08:21:40 +00001226 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001227 appendStmt(Block, B);
Marcin Swiderskie1667192010-10-24 08:21:40 +00001228 }
1229
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001230 CFGBlock *RBlock = Visit(B->getRHS());
1231 CFGBlock *LBlock = Visit(B->getLHS());
1232 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1233 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1234 // return RBlock. Otherwise we'll incorrectly return NULL.
1235 return (LBlock ? LBlock : RBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001236}
1237
Ted Kremenek852274d2009-12-16 03:18:58 +00001238CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001239 if (asc.alwaysAdd(*this, E)) {
Ted Kremenek721903e2009-11-25 01:34:30 +00001240 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001241 appendStmt(Block, E);
Ted Kremenek721903e2009-11-25 01:34:30 +00001242 }
1243 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001244}
1245
Ted Kremenek4f880632009-07-17 22:18:43 +00001246CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1247 // "break" is a control-flow statement. Thus we stop processing the current
1248 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001249 if (badCFG)
1250 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Ted Kremenek4f880632009-07-17 22:18:43 +00001252 // Now create a new block that ends with the break statement.
1253 Block = createBlock(false);
1254 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Ted Kremenek4f880632009-07-17 22:18:43 +00001256 // If there is no target for the break, then we are looking at an incomplete
1257 // AST. This means that the CFG cannot be constructed.
Ted Kremenek9ce52702011-01-07 19:37:16 +00001258 if (BreakJumpTarget.block) {
1259 addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B);
1260 addSuccessor(Block, BreakJumpTarget.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001261 } else
Ted Kremenek4f880632009-07-17 22:18:43 +00001262 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001263
1264
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001265 return Block;
1266}
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001268static bool CanThrow(Expr *E, ASTContext &Ctx) {
Mike Stump4c45aa12010-01-21 15:20:48 +00001269 QualType Ty = E->getType();
1270 if (Ty->isFunctionPointerType())
1271 Ty = Ty->getAs<PointerType>()->getPointeeType();
1272 else if (Ty->isBlockPointerType())
1273 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +00001274
Mike Stump4c45aa12010-01-21 15:20:48 +00001275 const FunctionType *FT = Ty->getAs<FunctionType>();
1276 if (FT) {
1277 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001278 if (Proto->isNothrow(Ctx))
Mike Stump4c45aa12010-01-21 15:20:48 +00001279 return false;
1280 }
1281 return true;
1282}
1283
Ted Kremenek852274d2009-12-16 03:18:58 +00001284CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
John McCall1de85332011-05-11 07:19:11 +00001285 // Compute the callee type.
1286 QualType calleeType = C->getCallee()->getType();
1287 if (calleeType == Context->BoundMemberTy) {
1288 QualType boundType = Expr::findBoundMemberType(C->getCallee());
1289
1290 // We should only get a null bound type if processing a dependent
1291 // CFG. Recover by assuming nothing.
1292 if (!boundType.isNull()) calleeType = boundType;
Ted Kremenek4f880632009-07-17 22:18:43 +00001293 }
Mike Stump24556362009-07-25 21:26:53 +00001294
John McCall1de85332011-05-11 07:19:11 +00001295 // If this is a call to a no-return function, this stops the block here.
1296 bool NoReturn = getFunctionExtInfo(*calleeType).getNoReturn();
1297
Mike Stump4c45aa12010-01-21 15:20:48 +00001298 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001299
1300 // Languages without exceptions are assumed to not throw.
David Blaikie4e4d0842012-03-11 07:00:24 +00001301 if (Context->getLangOpts().Exceptions) {
Ted Kremenek6c52c782010-09-14 23:41:16 +00001302 if (BuildOpts.AddEHEdges)
Mike Stump4c45aa12010-01-21 15:20:48 +00001303 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +00001304 }
1305
1306 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +00001307 if (FD->hasAttr<NoReturnAttr>())
1308 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +00001309 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +00001310 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001311 }
Mike Stump24556362009-07-25 21:26:53 +00001312
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001313 if (!CanThrow(C->getCallee(), *Context))
Mike Stump4c45aa12010-01-21 15:20:48 +00001314 AddEHEdge = false;
1315
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001316 if (!NoReturn && !AddEHEdge)
1317 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Mike Stump079bd722010-01-19 22:00:14 +00001319 if (Block) {
1320 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001321 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +00001322 return 0;
1323 }
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Chandler Carruthdba3fb52011-09-13 09:13:49 +00001325 if (NoReturn)
1326 Block = createNoReturnBlock();
1327 else
1328 Block = createBlock();
1329
Ted Kremenek247e9662011-03-10 01:14:08 +00001330 appendStmt(Block, C);
Mike Stump24556362009-07-25 21:26:53 +00001331
Mike Stump4c45aa12010-01-21 15:20:48 +00001332 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +00001333 // Add exceptional edges.
1334 if (TryTerminatedBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001335 addSuccessor(Block, TryTerminatedBlock);
Mike Stump079bd722010-01-19 22:00:14 +00001336 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001337 addSuccessor(Block, &cfg->getExit());
Mike Stump079bd722010-01-19 22:00:14 +00001338 }
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Mike Stump24556362009-07-25 21:26:53 +00001340 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +00001341}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001342
Ted Kremenek852274d2009-12-16 03:18:58 +00001343CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1344 AddStmtChoice asc) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001345 CFGBlock *ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001346 appendStmt(ConfluenceBlock, C);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001347 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001348 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001350 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001351 Succ = ConfluenceBlock;
1352 Block = NULL;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001353 CFGBlock *LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001354 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001355 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001357 Succ = ConfluenceBlock;
1358 Block = NULL;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001359 CFGBlock *RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001360 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001361 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001363 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +00001364 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001365 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
1366 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1367 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001368 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001369 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001370}
Mike Stump1eb44332009-09-09 15:08:12 +00001371
1372
Ted Kremenek9c378f72011-08-12 23:37:29 +00001373CFGBlock *CFGBuilder::VisitCompoundStmt(CompoundStmt *C) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001374 addLocalScopeAndDtors(C);
Ted Kremenek9c378f72011-08-12 23:37:29 +00001375 CFGBlock *LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001376
1377 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1378 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +00001379 // If we hit a segment of code just containing ';' (NullStmts), we can
1380 // get a null block back. In such cases, just use the LastBlock
1381 if (CFGBlock *newBlock = addStmt(*I))
1382 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +00001384 if (badCFG)
1385 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001386 }
Mike Stump079bd722010-01-19 22:00:14 +00001387
Ted Kremenek4f880632009-07-17 22:18:43 +00001388 return LastBlock;
1389}
Mike Stump1eb44332009-09-09 15:08:12 +00001390
John McCall56ca35d2011-02-17 10:25:35 +00001391CFGBlock *CFGBuilder::VisitConditionalOperator(AbstractConditionalOperator *C,
Ted Kremenek852274d2009-12-16 03:18:58 +00001392 AddStmtChoice asc) {
John McCall56ca35d2011-02-17 10:25:35 +00001393 const BinaryConditionalOperator *BCO = dyn_cast<BinaryConditionalOperator>(C);
1394 const OpaqueValueExpr *opaqueValue = (BCO ? BCO->getOpaqueValue() : NULL);
1395
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001396 // Create the confluence block that will "merge" the results of the ternary
1397 // expression.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001398 CFGBlock *ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001399 appendStmt(ConfluenceBlock, C);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001400 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001401 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001403 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001404
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001405 // Create a block for the LHS expression if there is an LHS expression. A
1406 // GCC extension allows LHS to be NULL, causing the condition to be the
1407 // value that is returned instead.
1408 // e.g: x ?: y is shorthand for: x ? x : y;
1409 Succ = ConfluenceBlock;
1410 Block = NULL;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001411 CFGBlock *LHSBlock = 0;
John McCall56ca35d2011-02-17 10:25:35 +00001412 const Expr *trueExpr = C->getTrueExpr();
1413 if (trueExpr != opaqueValue) {
1414 LHSBlock = Visit(C->getTrueExpr(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001415 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001416 return 0;
1417 Block = NULL;
1418 }
Ted Kremenekf226d182011-02-24 03:09:15 +00001419 else
1420 LHSBlock = ConfluenceBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001422 // Create the block for the RHS expression.
1423 Succ = ConfluenceBlock;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001424 CFGBlock *RHSBlock = Visit(C->getFalseExpr(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001425 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001426 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001428 // Create the block that will contain the condition.
1429 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Mike Stump00998a02009-07-23 23:25:26 +00001431 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001432 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
Ted Kremenekf226d182011-02-24 03:09:15 +00001433 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001434 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001435 Block->setTerminator(C);
John McCall56ca35d2011-02-17 10:25:35 +00001436 Expr *condExpr = C->getCond();
John McCalld40baf62011-02-19 03:13:26 +00001437
Ted Kremenekf226d182011-02-24 03:09:15 +00001438 if (opaqueValue) {
1439 // Run the condition expression if it's not trivially expressed in
1440 // terms of the opaque value (or if there is no opaque value).
1441 if (condExpr != opaqueValue)
1442 addStmt(condExpr);
John McCalld40baf62011-02-19 03:13:26 +00001443
Ted Kremenekf226d182011-02-24 03:09:15 +00001444 // Before that, run the common subexpression if there was one.
1445 // At least one of this or the above will be run.
1446 return addStmt(BCO->getCommon());
1447 }
1448
1449 return addStmt(condExpr);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001450}
1451
Ted Kremenek4f880632009-07-17 22:18:43 +00001452CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Ted Kremenekbc869de2011-05-10 18:42:15 +00001453 // Check if the Decl is for an __label__. If so, elide it from the
1454 // CFG entirely.
1455 if (isa<LabelDecl>(*DS->decl_begin()))
1456 return Block;
1457
Ted Kremenek29c9e622011-05-24 20:41:31 +00001458 // This case also handles static_asserts.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001459 if (DS->isSingleDecl())
1460 return VisitDeclSubExpr(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Ted Kremenek4f880632009-07-17 22:18:43 +00001462 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Ted Kremenek4f880632009-07-17 22:18:43 +00001464 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001465 typedef SmallVector<Decl*,10> BufTy;
Ted Kremenek4f880632009-07-17 22:18:43 +00001466 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Ted Kremenek4f880632009-07-17 22:18:43 +00001468 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1469 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1470 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1471 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Ted Kremenek4f880632009-07-17 22:18:43 +00001473 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1474 // automatically freed with the CFG.
1475 DeclGroupRef DG(*I);
1476 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001477 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001478 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Ted Kremenek4f880632009-07-17 22:18:43 +00001480 // Append the fake DeclStmt to block.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001481 B = VisitDeclSubExpr(DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +00001482 }
Mike Stump1eb44332009-09-09 15:08:12 +00001483
1484 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +00001485}
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Ted Kremenek4f880632009-07-17 22:18:43 +00001487/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski8599e762010-11-03 06:19:35 +00001488/// DeclStmts and initializers in them.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001489CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt *DS) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001490 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenek29c9e622011-05-24 20:41:31 +00001491 Decl *D = DS->getSingleDecl();
1492
1493 if (isa<StaticAssertDecl>(D)) {
1494 // static_asserts aren't added to the CFG because they do not impact
1495 // runtime semantics.
1496 return Block;
1497 }
1498
Marcin Swiderski8599e762010-11-03 06:19:35 +00001499 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Marcin Swiderski8599e762010-11-03 06:19:35 +00001501 if (!VD) {
1502 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001503 appendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +00001504 return Block;
Marcin Swiderski8599e762010-11-03 06:19:35 +00001505 }
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Marcin Swiderski8599e762010-11-03 06:19:35 +00001507 bool IsReference = false;
1508 bool HasTemporaries = false;
1509
1510 // Destructors of temporaries in initialization expression should be called
1511 // after initialization finishes.
Ted Kremenek4f880632009-07-17 22:18:43 +00001512 Expr *Init = VD->getInit();
Marcin Swiderski8599e762010-11-03 06:19:35 +00001513 if (Init) {
1514 IsReference = VD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +00001515 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +00001516
1517 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1518 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +00001519 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +00001520 IsReference);
1521 }
1522 }
1523
1524 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001525 appendStmt(Block, DS);
Ted Kremenek550f2232012-03-22 05:57:43 +00001526
1527 // Keep track of the last non-null block, as 'Block' can be nulled out
1528 // if the initializer expression is something like a 'while' in a
1529 // statement-expression.
1530 CFGBlock *LastBlock = Block;
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Ted Kremenek4f880632009-07-17 22:18:43 +00001532 if (Init) {
Ted Kremenek550f2232012-03-22 05:57:43 +00001533 if (HasTemporaries) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001534 // For expression with temporaries go directly to subexpression to omit
1535 // generating destructors for the second time.
Ted Kremenek550f2232012-03-22 05:57:43 +00001536 ExprWithCleanups *EC = cast<ExprWithCleanups>(Init);
1537 if (CFGBlock *newBlock = Visit(EC->getSubExpr()))
1538 LastBlock = newBlock;
1539 }
1540 else {
1541 if (CFGBlock *newBlock = Visit(Init))
1542 LastBlock = newBlock;
1543 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001544 }
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Ted Kremenek4f880632009-07-17 22:18:43 +00001546 // If the type of VD is a VLA, then we must process its size expressions.
John McCallf4c73712011-01-19 06:33:43 +00001547 for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr());
1548 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek4f880632009-07-17 22:18:43 +00001549 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001551 // Remove variable from local scope.
1552 if (ScopePos && VD == *ScopePos)
1553 ++ScopePos;
1554
Ted Kremenek550f2232012-03-22 05:57:43 +00001555 return Block ? Block : LastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001556}
1557
Ted Kremenek9c378f72011-08-12 23:37:29 +00001558CFGBlock *CFGBuilder::VisitIfStmt(IfStmt *I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001559 // We may see an if statement in the middle of a basic block, or it may be the
1560 // first statement we are processing. In either case, we create a new basic
1561 // block. First, we create the blocks for the then...else statements, and
1562 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +00001563 // middle of a block, we stop processing that block. That block is then the
1564 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +00001565
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001566 // Save local scope position because in case of condition variable ScopePos
1567 // won't be restored when traversing AST.
1568 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1569
1570 // Create local scope for possible condition variable.
1571 // Store scope position. Add implicit destructor.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001572 if (VarDecl *VD = I->getConditionVariable()) {
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001573 LocalScope::const_iterator BeginScopePos = ScopePos;
1574 addLocalScopeForVarDecl(VD);
1575 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1576 }
1577
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001578 // The block we were processing is now finished. Make it the successor
Mike Stump6d9828c2009-07-17 01:31:16 +00001579 // block.
1580 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001581 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001582 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001583 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001584 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001585
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001586 // Process the false branch.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001587 CFGBlock *ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001588
Ted Kremenek9c378f72011-08-12 23:37:29 +00001589 if (Stmt *Else = I->getElse()) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001590 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001591
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001592 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +00001593 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001594 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001595
1596 // If branch is not a compound statement create implicit scope
1597 // and add destructors.
1598 if (!isa<CompoundStmt>(Else))
1599 addLocalScopeAndDtors(Else);
1600
Ted Kremenek4f880632009-07-17 22:18:43 +00001601 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +00001602
Ted Kremenekb6f7b722007-08-30 18:13:31 +00001603 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1604 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001605 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001606 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001607 return 0;
1608 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001609 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001610
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001611 // Process the true branch.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001612 CFGBlock *ThenBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001613 {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001614 Stmt *Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001615 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001616 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001617 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001618
1619 // If branch is not a compound statement create implicit scope
1620 // and add destructors.
1621 if (!isa<CompoundStmt>(Then))
1622 addLocalScopeAndDtors(Then);
1623
Ted Kremenek4f880632009-07-17 22:18:43 +00001624 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +00001625
Ted Kremenekdbdf7942009-04-01 03:52:47 +00001626 if (!ThenBlock) {
1627 // We can reach here if the "then" body has all NullStmts.
1628 // Create an empty block so we can distinguish between true and false
1629 // branches in path-sensitive analyses.
1630 ThenBlock = createBlock(false);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001631 addSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +00001632 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001633 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001634 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001635 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001636 }
1637
Mike Stump6d9828c2009-07-17 01:31:16 +00001638 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001639 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001640
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001641 // Set the terminator of the new block to the If statement.
1642 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001643
Mike Stump00998a02009-07-23 23:25:26 +00001644 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001645 const TryResult &KnownVal = tryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001646
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001647 // Now add the successors.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001648 addSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1649 addSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001650
1651 // Add the condition as the last statement in the new block. This may create
1652 // new blocks as the condition may contain control-flow. Any newly created
1653 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001654 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001655
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001656 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1657 // and the condition variable initialization to the CFG.
1658 if (VarDecl *VD = I->getConditionVariable()) {
1659 if (Expr *Init = VD->getInit()) {
1660 autoCreateBlock();
Ted Kremenekd40066b2011-04-04 23:29:12 +00001661 appendStmt(Block, I->getConditionVariableDeclStmt());
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001662 addStmt(Init);
1663 }
1664 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001665
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001666 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001667}
Mike Stump6d9828c2009-07-17 01:31:16 +00001668
1669
Ted Kremenek9c378f72011-08-12 23:37:29 +00001670CFGBlock *CFGBuilder::VisitReturnStmt(ReturnStmt *R) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001671 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001672 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001673 // NOTE: If a "return" appears in the middle of a block, this means that the
1674 // code afterwards is DEAD (unreachable). We still keep a basic block
1675 // for that code; a simple "mark-and-sweep" from the entry block will be
1676 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001677
1678 // Create the new block.
1679 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001680
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001681 // The Exit block is the only successor.
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001682 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001683 addSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001684
1685 // Add the return statement to the block. This may create new blocks if R
1686 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001687 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001688}
1689
Ted Kremenek9c378f72011-08-12 23:37:29 +00001690CFGBlock *CFGBuilder::VisitLabelStmt(LabelStmt *L) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001691 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +00001692 addStmt(L->getSubStmt());
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001693 CFGBlock *LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001694
Ted Kremenek4f880632009-07-17 22:18:43 +00001695 if (!LabelBlock) // This can happen when the body is empty, i.e.
1696 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +00001697
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001698 assert(LabelMap.find(L->getDecl()) == LabelMap.end() &&
1699 "label already in map");
1700 LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001701
1702 // Labels partition blocks, so this is the end of the basic block we were
1703 // processing (L is the block's label). Because this is label (and we have
1704 // already processed the substatement) there is no extra control-flow to worry
1705 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +00001706 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001707 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001708 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001709
1710 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001711 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001712
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001713 // This block is now the implicit successor of other blocks.
1714 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001715
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001716 return LabelBlock;
1717}
1718
Ted Kremenek9c378f72011-08-12 23:37:29 +00001719CFGBlock *CFGBuilder::VisitGotoStmt(GotoStmt *G) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001720 // Goto is a control-flow statement. Thus we stop processing the current
1721 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001722
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001723 Block = createBlock(false);
1724 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001725
1726 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001727 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001728
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001729 if (I == LabelMap.end())
1730 // We will need to backpatch this block later.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001731 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1732 else {
1733 JumpTarget JT = I->second;
Ted Kremenek9ce52702011-01-07 19:37:16 +00001734 addAutomaticObjDtors(ScopePos, JT.scopePosition, G);
1735 addSuccessor(Block, JT.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001736 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001737
Mike Stump6d9828c2009-07-17 01:31:16 +00001738 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001739}
1740
Ted Kremenek9c378f72011-08-12 23:37:29 +00001741CFGBlock *CFGBuilder::VisitForStmt(ForStmt *F) {
1742 CFGBlock *LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001743
Marcin Swiderski47575f12010-10-01 01:38:14 +00001744 // Save local scope position because in case of condition variable ScopePos
1745 // won't be restored when traversing AST.
1746 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1747
1748 // Create local scope for init statement and possible condition variable.
1749 // Add destructor for init statement and condition variable.
1750 // Store scope position for continue statement.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001751 if (Stmt *Init = F->getInit())
Marcin Swiderski47575f12010-10-01 01:38:14 +00001752 addLocalScopeForStmt(Init);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001753 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1754
Ted Kremenek9c378f72011-08-12 23:37:29 +00001755 if (VarDecl *VD = F->getConditionVariable())
Marcin Swiderski47575f12010-10-01 01:38:14 +00001756 addLocalScopeForVarDecl(VD);
1757 LocalScope::const_iterator ContinueScopePos = ScopePos;
1758
1759 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1760
Mike Stumpfefb9f72009-07-21 01:12:51 +00001761 // "for" is a control-flow statement. Thus we stop processing the current
1762 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001763 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001764 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001765 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001766 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001767 } else
1768 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001769
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001770 // Save the current value for the break targets.
1771 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001772 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001773 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001774
Mike Stump6d9828c2009-07-17 01:31:16 +00001775 // Because of short-circuit evaluation, the condition of the loop can span
1776 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1777 // evaluate the condition.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001778 CFGBlock *ExitConditionBlock = createBlock(false);
1779 CFGBlock *EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001780
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001781 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001782 ExitConditionBlock->setTerminator(F);
1783
1784 // Now add the actual condition to the condition block. Because the condition
1785 // itself may contain control-flow, new blocks may be created.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001786 if (Stmt *C = F->getCond()) {
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001787 Block = ExitConditionBlock;
1788 EntryConditionBlock = addStmt(C);
Ted Kremenek9ce52702011-01-07 19:37:16 +00001789 if (badCFG)
1790 return 0;
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001791 assert(Block == EntryConditionBlock ||
1792 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001793
1794 // If this block contains a condition variable, add both the condition
1795 // variable and initializer to the CFG.
1796 if (VarDecl *VD = F->getConditionVariable()) {
1797 if (Expr *Init = VD->getInit()) {
1798 autoCreateBlock();
Ted Kremenekd40066b2011-04-04 23:29:12 +00001799 appendStmt(Block, F->getConditionVariableDeclStmt());
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001800 EntryConditionBlock = addStmt(Init);
1801 assert(Block == EntryConditionBlock);
1802 }
1803 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001804
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001805 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001806 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001807 return 0;
1808 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001809 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001810
Mike Stump6d9828c2009-07-17 01:31:16 +00001811 // The condition block is the implicit successor for the loop body as well as
1812 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001813 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001814
Mike Stump00998a02009-07-23 23:25:26 +00001815 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001816 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Mike Stump00998a02009-07-23 23:25:26 +00001818 if (F->getCond())
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001819 KnownVal = tryEvaluateBool(F->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001820
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001821 // Now create the loop body.
1822 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001823 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001824
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001825 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001826 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1827 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001828
Ted Kremenekaf603f72007-08-30 18:39:40 +00001829 // Create a new block to contain the (bottom) of the loop body.
1830 Block = NULL;
Marcin Swiderski47575f12010-10-01 01:38:14 +00001831
1832 // Loop body should end with destructor of Condition variable (if any).
1833 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump6d9828c2009-07-17 01:31:16 +00001834
Ted Kremenek9c378f72011-08-12 23:37:29 +00001835 if (Stmt *I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001836 // Generate increment code in its own basic block. This is the target of
1837 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001838 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001839 } else {
1840 // No increment code. Create a special, empty, block that is used as the
1841 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001842 assert(Succ == EntryConditionBlock);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001843 Succ = Block ? Block : createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001844 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001845
Ted Kremenek3575f842009-04-28 00:51:56 +00001846 // Finish up the increment (or empty) block if it hasn't been already.
1847 if (Block) {
1848 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001849 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001850 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001851 Block = 0;
1852 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001853
Marcin Swiderski47575f12010-10-01 01:38:14 +00001854 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001855
Ted Kremenek3575f842009-04-28 00:51:56 +00001856 // The starting block for the loop increment is the block that should
1857 // represent the 'loop target' for looping back to the start of the loop.
Ted Kremenek9ce52702011-01-07 19:37:16 +00001858 ContinueJumpTarget.block->setLoopTarget(F);
Ted Kremenek3575f842009-04-28 00:51:56 +00001859
Marcin Swiderski47575f12010-10-01 01:38:14 +00001860 // If body is not a compound statement create implicit scope
1861 // and add destructors.
1862 if (!isa<CompoundStmt>(F->getBody()))
1863 addLocalScopeAndDtors(F->getBody());
1864
Mike Stump6d9828c2009-07-17 01:31:16 +00001865 // Now populate the body block, and in the process create new blocks as we
1866 // walk the body of the loop.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001867 CFGBlock *BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001868
1869 if (!BodyBlock)
Ted Kremenek9ce52702011-01-07 19:37:16 +00001870 BodyBlock = ContinueJumpTarget.block;//can happen for "for (...;...;...);"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001871 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001872 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001873
Ted Kremenek941fde82009-07-24 04:47:11 +00001874 // This new body block is a successor to our "exit" condition block.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001875 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001876 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001877
Ted Kremenek941fde82009-07-24 04:47:11 +00001878 // Link up the condition block with the code that follows the loop. (the
1879 // false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001880 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001881
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001882 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001883 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001884 if (Stmt *I = F->getInit()) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001885 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001886 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001887 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001888
1889 // There is no loop initialization. We are thus basically a while loop.
1890 // NULL out Block to force lazy block construction.
1891 Block = NULL;
1892 Succ = EntryConditionBlock;
1893 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001894}
1895
Ted Kremenek115c1b92010-04-11 17:02:10 +00001896CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001897 if (asc.alwaysAdd(*this, M)) {
Ted Kremenek115c1b92010-04-11 17:02:10 +00001898 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001899 appendStmt(Block, M);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001900 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001901 return Visit(M->getBase());
Ted Kremenek115c1b92010-04-11 17:02:10 +00001902}
1903
Ted Kremenek9c378f72011-08-12 23:37:29 +00001904CFGBlock *CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
Ted Kremenek514de5a2008-11-11 17:10:00 +00001905 // Objective-C fast enumeration 'for' statements:
1906 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1907 //
1908 // for ( Type newVariable in collection_expression ) { statements }
1909 //
1910 // becomes:
1911 //
1912 // prologue:
1913 // 1. collection_expression
1914 // T. jump to loop_entry
1915 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001916 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001917 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1918 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1919 // TB:
1920 // statements
1921 // T. jump to loop_entry
1922 // FB:
1923 // what comes after
1924 //
1925 // and
1926 //
1927 // Type existingItem;
1928 // for ( existingItem in expression ) { statements }
1929 //
1930 // becomes:
1931 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001932 // the same with newVariable replaced with existingItem; the binding works
1933 // the same except that for one ObjCForCollectionStmt::getElement() returns
1934 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001935 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001936
Ted Kremenek9c378f72011-08-12 23:37:29 +00001937 CFGBlock *LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001938
Ted Kremenek514de5a2008-11-11 17:10:00 +00001939 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001940 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001941 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001942 LoopSuccessor = Block;
1943 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001944 } else
1945 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001946
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001947 // Build the condition blocks.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001948 CFGBlock *ExitConditionBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001949
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001950 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001951 ExitConditionBlock->setTerminator(S);
1952
1953 // The last statement in the block should be the ObjCForCollectionStmt, which
1954 // performs the actual binding to 'element' and determines if there are any
1955 // more items in the collection.
Ted Kremenek892697d2010-12-16 07:46:53 +00001956 appendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001957 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001958
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001959 // Walk the 'element' expression to see if there are any side-effects. We
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001960 // generate new blocks as necessary. We DON'T add the statement by default to
Mike Stump6d9828c2009-07-17 01:31:16 +00001961 // the CFG unless it contains control-flow.
Ted Kremenek012614e2011-08-17 21:04:19 +00001962 CFGBlock *EntryConditionBlock = Visit(S->getElement(),
1963 AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001964 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001965 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001966 return 0;
1967 Block = 0;
1968 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001969
1970 // The condition block is the implicit successor for the loop body as well as
1971 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001972 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001973
Ted Kremenek514de5a2008-11-11 17:10:00 +00001974 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001975 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001976 // Save the current values for Succ, continue and break targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001977 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1978 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1979 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001980
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001981 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1982 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001983
Ted Kremenek9c378f72011-08-12 23:37:29 +00001984 CFGBlock *BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001985
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001986 if (!BodyBlock)
1987 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001988 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001989 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001990 return 0;
1991 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001992
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001993 // This new body block is a successor to our "exit" condition block.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001994 addSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001995 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001996
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001997 // Link up the condition block with the code that follows the loop.
1998 // (the false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001999 addSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00002000
Ted Kremenek514de5a2008-11-11 17:10:00 +00002001 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00002002 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00002003 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00002004}
2005
Ted Kremenek8e282c32012-03-06 23:40:47 +00002006CFGBlock *CFGBuilder::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
2007 // Inline the body.
2008 return addStmt(S->getSubStmt());
2009 // TODO: consider adding cleanups for the end of @autoreleasepool scope.
2010}
2011
Ted Kremenek9c378f72011-08-12 23:37:29 +00002012CFGBlock *CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
Ted Kremenekb3b0b362009-05-02 01:49:13 +00002013 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00002014
Ted Kremenekb3b0b362009-05-02 01:49:13 +00002015 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002016 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002017
Ted Kremenekda5348e2009-05-05 23:11:51 +00002018 // The sync body starts its own basic block. This makes it a little easier
2019 // for diagnostic clients.
2020 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002021 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00002022 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002023
Ted Kremenekda5348e2009-05-05 23:11:51 +00002024 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00002025 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00002026 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002027
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00002028 // Add the @synchronized to the CFG.
2029 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002030 appendStmt(Block, S);
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00002031
Ted Kremenekb3b0b362009-05-02 01:49:13 +00002032 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00002033 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00002034}
Mike Stump6d9828c2009-07-17 01:31:16 +00002035
Ted Kremenek9c378f72011-08-12 23:37:29 +00002036CFGBlock *CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002037 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00002038 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00002039}
Ted Kremenek514de5a2008-11-11 17:10:00 +00002040
John McCall4b9c2d22011-11-06 09:01:30 +00002041CFGBlock *CFGBuilder::VisitPseudoObjectExpr(PseudoObjectExpr *E) {
2042 autoCreateBlock();
2043
2044 // Add the PseudoObject as the last thing.
2045 appendStmt(Block, E);
2046
2047 CFGBlock *lastBlock = Block;
2048
2049 // Before that, evaluate all of the semantics in order. In
2050 // CFG-land, that means appending them in reverse order.
2051 for (unsigned i = E->getNumSemanticExprs(); i != 0; ) {
2052 Expr *Semantic = E->getSemanticExpr(--i);
2053
2054 // If the semantic is an opaque value, we're being asked to bind
2055 // it to its source expression.
2056 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
2057 Semantic = OVE->getSourceExpr();
2058
2059 if (CFGBlock *B = Visit(Semantic))
2060 lastBlock = B;
2061 }
2062
2063 return lastBlock;
2064}
2065
Ted Kremenek9c378f72011-08-12 23:37:29 +00002066CFGBlock *CFGBuilder::VisitWhileStmt(WhileStmt *W) {
2067 CFGBlock *LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002068
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002069 // Save local scope position because in case of condition variable ScopePos
2070 // won't be restored when traversing AST.
2071 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2072
2073 // Create local scope for possible condition variable.
2074 // Store scope position for continue statement.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002075 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Ted Kremenek9c378f72011-08-12 23:37:29 +00002076 if (VarDecl *VD = W->getConditionVariable()) {
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002077 addLocalScopeForVarDecl(VD);
2078 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
2079 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002080
Mike Stumpfefb9f72009-07-21 01:12:51 +00002081 // "while" is a control-flow statement. Thus we stop processing the current
2082 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002083 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002084 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002085 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002086 LoopSuccessor = Block;
Ted Kremenek6b12da92011-02-21 22:11:26 +00002087 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00002088 } else
2089 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00002090
2091 // Because of short-circuit evaluation, the condition of the loop can span
2092 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
2093 // evaluate the condition.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002094 CFGBlock *ExitConditionBlock = createBlock(false);
2095 CFGBlock *EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002096
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002097 // Set the terminator for the "exit" condition block.
2098 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00002099
2100 // Now add the actual condition to the condition block. Because the condition
2101 // itself may contain control-flow, new blocks may be created. Thus we update
2102 // "Succ" after adding the condition.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002103 if (Stmt *C = W->getCond()) {
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002104 Block = ExitConditionBlock;
2105 EntryConditionBlock = addStmt(C);
Zhongxing Xua1898dd2010-10-27 03:23:10 +00002106 // The condition might finish the current 'Block'.
2107 Block = EntryConditionBlock;
Ted Kremenekad5a8942010-08-02 23:46:59 +00002108
Ted Kremenek4ec010a2009-12-24 01:34:10 +00002109 // If this block contains a condition variable, add both the condition
2110 // variable and initializer to the CFG.
2111 if (VarDecl *VD = W->getConditionVariable()) {
2112 if (Expr *Init = VD->getInit()) {
2113 autoCreateBlock();
Ted Kremenekd40066b2011-04-04 23:29:12 +00002114 appendStmt(Block, W->getConditionVariableDeclStmt());
Ted Kremenek4ec010a2009-12-24 01:34:10 +00002115 EntryConditionBlock = addStmt(Init);
2116 assert(Block == EntryConditionBlock);
2117 }
2118 }
2119
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002120 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002121 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002122 return 0;
2123 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002124 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002125
2126 // The condition block is the implicit successor for the loop body as well as
2127 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002128 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002129
Mike Stump00998a02009-07-23 23:25:26 +00002130 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002131 const TryResult& KnownVal = tryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00002132
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002133 // Process the loop body.
2134 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00002135 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002136
2137 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002138 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2139 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2140 save_break(BreakJumpTarget);
Ted Kremenekf6e85412009-04-28 03:09:44 +00002141
Mike Stump6d9828c2009-07-17 01:31:16 +00002142 // Create an empty block to represent the transition block for looping back
2143 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00002144 Block = 0;
2145 assert(Succ == EntryConditionBlock);
2146 Succ = createBlock();
2147 Succ->setLoopTarget(W);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002148 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002149
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002150 // All breaks should go to the code following the loop.
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002151 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002152
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002153 // NULL out Block to force lazy instantiation of blocks for the body.
2154 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002155
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002156 // Loop body should end with destructor of Condition variable (if any).
2157 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
2158
2159 // If body is not a compound statement create implicit scope
2160 // and add destructors.
2161 if (!isa<CompoundStmt>(W->getBody()))
2162 addLocalScopeAndDtors(W->getBody());
2163
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002164 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002165 CFGBlock *BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002166
Ted Kremenekaf603f72007-08-30 18:39:40 +00002167 if (!BodyBlock)
Ted Kremenek9ce52702011-01-07 19:37:16 +00002168 BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002169 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002170 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002171 return 0;
2172 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002173
Ted Kremenek941fde82009-07-24 04:47:11 +00002174 // Add the loop body entry as a successor to the condition.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002175 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002176 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002177
Ted Kremenek941fde82009-07-24 04:47:11 +00002178 // Link up the condition block with the code that follows the loop. (the
2179 // false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002180 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002181
2182 // There can be no more statements in the condition block since we loop back
2183 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002184 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002185
Ted Kremenek4ec010a2009-12-24 01:34:10 +00002186 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002187 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002188 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002189}
Mike Stump1eb44332009-09-09 15:08:12 +00002190
2191
Ted Kremenek9c378f72011-08-12 23:37:29 +00002192CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002193 // FIXME: For now we pretend that @catch and the code it contains does not
2194 // exit.
2195 return Block;
2196}
Mike Stump6d9828c2009-07-17 01:31:16 +00002197
Ted Kremenek9c378f72011-08-12 23:37:29 +00002198CFGBlock *CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
Ted Kremenek2fda5042008-12-09 20:20:09 +00002199 // FIXME: This isn't complete. We basically treat @throw like a return
2200 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00002201
Ted Kremenek6c249722009-09-24 18:45:41 +00002202 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002203 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002204 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002205
Ted Kremenek2fda5042008-12-09 20:20:09 +00002206 // Create the new block.
2207 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002208
Ted Kremenek2fda5042008-12-09 20:20:09 +00002209 // The Exit block is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002210 addSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00002211
2212 // Add the statement to the block. This may create new blocks if S contains
2213 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00002214 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00002215}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002216
Ted Kremenek9c378f72011-08-12 23:37:29 +00002217CFGBlock *CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr *T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00002218 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002219 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00002220 return 0;
2221
2222 // Create the new block.
2223 Block = createBlock(false);
2224
Mike Stump5d1d2022010-01-19 02:20:09 +00002225 if (TryTerminatedBlock)
2226 // The current try statement is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002227 addSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00002228 else
Mike Stump5d1d2022010-01-19 02:20:09 +00002229 // otherwise the Exit block is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002230 addSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00002231
2232 // Add the statement to the block. This may create new blocks if S contains
2233 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00002234 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00002235}
2236
Ted Kremenek9c378f72011-08-12 23:37:29 +00002237CFGBlock *CFGBuilder::VisitDoStmt(DoStmt *D) {
2238 CFGBlock *LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002239
Mike Stump8f9893a2009-07-21 01:27:50 +00002240 // "do...while" is a control-flow statement. Thus we stop processing the
2241 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002242 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002243 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002244 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002245 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002246 } else
2247 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00002248
2249 // Because of short-circuit evaluation, the condition of the loop can span
2250 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
2251 // evaluate the condition.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002252 CFGBlock *ExitConditionBlock = createBlock(false);
2253 CFGBlock *EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002254
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002255 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00002256 ExitConditionBlock->setTerminator(D);
2257
2258 // Now add the actual condition to the condition block. Because the condition
2259 // itself may contain control-flow, new blocks may be created.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002260 if (Stmt *C = D->getCond()) {
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002261 Block = ExitConditionBlock;
2262 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002263 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002264 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002265 return 0;
2266 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002267 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002268
Ted Kremenek54827132008-02-27 07:20:00 +00002269 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002270 Succ = EntryConditionBlock;
2271
Mike Stump00998a02009-07-23 23:25:26 +00002272 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002273 const TryResult &KnownVal = tryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00002274
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002275 // Process the loop body.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002276 CFGBlock *BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002277 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002278 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002279
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002280 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002281 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2282 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2283 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00002284
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002285 // All continues within this loop should go to the condition block
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002286 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002287
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002288 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002289 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002290
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002291 // NULL out Block to force lazy instantiation of blocks for the body.
2292 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002293
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002294 // If body is not a compound statement create implicit scope
2295 // and add destructors.
2296 if (!isa<CompoundStmt>(D->getBody()))
2297 addLocalScopeAndDtors(D->getBody());
2298
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002299 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002300 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002301
Ted Kremenekaf603f72007-08-30 18:39:40 +00002302 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00002303 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002304 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002305 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002306 return 0;
2307 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002308
Ted Kremenekd173dc72010-08-17 20:59:56 +00002309 if (!KnownVal.isFalse()) {
2310 // Add an intermediate block between the BodyBlock and the
2311 // ExitConditionBlock to represent the "loop back" transition. Create an
2312 // empty block to represent the transition block for looping back to the
2313 // head of the loop.
2314 // FIXME: Can we do this more efficiently without adding another block?
2315 Block = NULL;
2316 Succ = BodyBlock;
2317 CFGBlock *LoopBackBlock = createBlock();
2318 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00002319
Ted Kremenekd173dc72010-08-17 20:59:56 +00002320 // Add the loop body entry as a successor to the condition.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002321 addSuccessor(ExitConditionBlock, LoopBackBlock);
Ted Kremenekd173dc72010-08-17 20:59:56 +00002322 }
2323 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002324 addSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002325 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002326
Ted Kremenek941fde82009-07-24 04:47:11 +00002327 // Link up the condition block with the code that follows the loop.
2328 // (the false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002329 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002330
2331 // There can be no more statements in the body block(s) since we loop back to
2332 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002333 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002334
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002335 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002336 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002337 return BodyBlock;
2338}
2339
Ted Kremenek9c378f72011-08-12 23:37:29 +00002340CFGBlock *CFGBuilder::VisitContinueStmt(ContinueStmt *C) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002341 // "continue" is a control-flow statement. Thus we stop processing the
2342 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002343 if (badCFG)
2344 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002345
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002346 // Now create a new block that ends with the continue statement.
2347 Block = createBlock(false);
2348 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00002349
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002350 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002351 // incomplete AST. This means the CFG cannot be constructed.
Ted Kremenek9ce52702011-01-07 19:37:16 +00002352 if (ContinueJumpTarget.block) {
2353 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C);
2354 addSuccessor(Block, ContinueJumpTarget.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002355 } else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002356 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00002357
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002358 return Block;
2359}
Mike Stump1eb44332009-09-09 15:08:12 +00002360
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002361CFGBlock *CFGBuilder::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E,
2362 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002363
Ted Kremenek3179a452011-03-10 01:14:11 +00002364 if (asc.alwaysAdd(*this, E)) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002365 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002366 appendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002367 }
Mike Stump1eb44332009-09-09 15:08:12 +00002368
Ted Kremenek4f880632009-07-17 22:18:43 +00002369 // VLA types have expressions that must be evaluated.
Ted Kremenek97e50712011-04-14 01:50:50 +00002370 CFGBlock *lastBlock = Block;
2371
Ted Kremenek4f880632009-07-17 22:18:43 +00002372 if (E->isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00002373 for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr());
Ted Kremenek4f880632009-07-17 22:18:43 +00002374 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek97e50712011-04-14 01:50:50 +00002375 lastBlock = addStmt(VA->getSizeExpr());
Ted Kremenekf91a5b02011-08-06 00:30:00 +00002376 }
Ted Kremenek97e50712011-04-14 01:50:50 +00002377 return lastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002378}
Mike Stump1eb44332009-09-09 15:08:12 +00002379
Ted Kremenek4f880632009-07-17 22:18:43 +00002380/// VisitStmtExpr - Utility method to handle (nested) statement
2381/// expressions (a GCC extension).
Ted Kremenek9c378f72011-08-12 23:37:29 +00002382CFGBlock *CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00002383 if (asc.alwaysAdd(*this, SE)) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002384 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002385 appendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002386 }
Ted Kremenek4f880632009-07-17 22:18:43 +00002387 return VisitCompoundStmt(SE->getSubStmt());
2388}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002389
Ted Kremenek9c378f72011-08-12 23:37:29 +00002390CFGBlock *CFGBuilder::VisitSwitchStmt(SwitchStmt *Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002391 // "switch" is a control-flow statement. Thus we stop processing the current
2392 // block.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002393 CFGBlock *SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002394
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002395 // Save local scope position because in case of condition variable ScopePos
2396 // won't be restored when traversing AST.
2397 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2398
2399 // Create local scope for possible condition variable.
2400 // Store scope position. Add implicit destructor.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002401 if (VarDecl *VD = Terminator->getConditionVariable()) {
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002402 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2403 addLocalScopeForVarDecl(VD);
2404 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2405 }
2406
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002407 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002408 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002409 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002410 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00002411 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002412
2413 // Save the current "switch" context.
2414 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002415 save_default(DefaultCaseBlock);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002416 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002417
Mike Stump6d9828c2009-07-17 01:31:16 +00002418 // Set the "default" case to be the block after the switch statement. If the
2419 // switch statement contains a "default:", this value will be overwritten with
2420 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002421 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00002422
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002423 // Create a new block that will contain the switch statement.
2424 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002425
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002426 // Now process the switch body. The code after the switch is the implicit
2427 // successor.
2428 Succ = SwitchSuccessor;
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002429 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002430
2431 // When visiting the body, the case statements should automatically get linked
2432 // up to the switch. We also don't keep a pointer to the body, since all
2433 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002434 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002435 Block = NULL;
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002436
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002437 // For pruning unreachable case statements, save the current state
2438 // for tracking the condition value.
2439 SaveAndRestore<bool> save_switchExclusivelyCovered(switchExclusivelyCovered,
2440 false);
Ted Kremenek04982472011-03-04 01:03:41 +00002441
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002442 // Determine if the switch condition can be explicitly evaluated.
2443 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek04982472011-03-04 01:03:41 +00002444 Expr::EvalResult result;
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002445 bool b = tryEvaluate(Terminator->getCond(), result);
2446 SaveAndRestore<Expr::EvalResult*> save_switchCond(switchCond,
2447 b ? &result : 0);
Ted Kremenek04982472011-03-04 01:03:41 +00002448
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002449 // If body is not a compound statement create implicit scope
2450 // and add destructors.
2451 if (!isa<CompoundStmt>(Terminator->getBody()))
2452 addLocalScopeAndDtors(Terminator->getBody());
2453
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002454 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002455 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002456 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002457 return 0;
2458 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002459
Mike Stump6d9828c2009-07-17 01:31:16 +00002460 // If we have no "default:" case, the default transition is to the code
Ted Kremenek432c4782011-03-16 04:32:01 +00002461 // following the switch body. Moreover, take into account if all the
2462 // cases of a switch are covered (e.g., switching on an enum value).
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002463 addSuccessor(SwitchTerminatedBlock,
Ted Kremenek432c4782011-03-16 04:32:01 +00002464 switchExclusivelyCovered || Terminator->isAllEnumCasesCovered()
2465 ? 0 : DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002466
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002467 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002468 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002469 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002470 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00002471
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002472 // Finally, if the SwitchStmt contains a condition variable, add both the
2473 // SwitchStmt and the condition variable initialization to the CFG.
2474 if (VarDecl *VD = Terminator->getConditionVariable()) {
2475 if (Expr *Init = VD->getInit()) {
2476 autoCreateBlock();
Ted Kremenekd40066b2011-04-04 23:29:12 +00002477 appendStmt(Block, Terminator->getConditionVariableDeclStmt());
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002478 addStmt(Init);
2479 }
2480 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00002481
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002482 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002483}
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002484
2485static bool shouldAddCase(bool &switchExclusivelyCovered,
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002486 const Expr::EvalResult *switchCond,
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002487 const CaseStmt *CS,
2488 ASTContext &Ctx) {
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002489 if (!switchCond)
2490 return true;
2491
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002492 bool addCase = false;
Ted Kremenek04982472011-03-04 01:03:41 +00002493
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002494 if (!switchExclusivelyCovered) {
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002495 if (switchCond->Val.isInt()) {
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002496 // Evaluate the LHS of the case value.
Richard Smith85df96c2011-10-14 20:22:00 +00002497 const llvm::APSInt &lhsInt = CS->getLHS()->EvaluateKnownConstInt(Ctx);
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002498 const llvm::APSInt &condInt = switchCond->Val.getInt();
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002499
2500 if (condInt == lhsInt) {
2501 addCase = true;
2502 switchExclusivelyCovered = true;
2503 }
2504 else if (condInt < lhsInt) {
2505 if (const Expr *RHS = CS->getRHS()) {
2506 // Evaluate the RHS of the case value.
Richard Smith85df96c2011-10-14 20:22:00 +00002507 const llvm::APSInt &V2 = RHS->EvaluateKnownConstInt(Ctx);
2508 if (V2 <= condInt) {
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002509 addCase = true;
2510 switchExclusivelyCovered = true;
2511 }
2512 }
2513 }
2514 }
2515 else
2516 addCase = true;
2517 }
2518 return addCase;
2519}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002520
Ted Kremenek9c378f72011-08-12 23:37:29 +00002521CFGBlock *CFGBuilder::VisitCaseStmt(CaseStmt *CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002522 // CaseStmts are essentially labels, so they are the first statement in a
2523 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002524 CFGBlock *TopBlock = 0, *LastBlock = 0;
Ted Kremenek04982472011-03-04 01:03:41 +00002525
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002526 if (Stmt *Sub = CS->getSubStmt()) {
2527 // For deeply nested chains of CaseStmts, instead of doing a recursion
2528 // (which can blow out the stack), manually unroll and create blocks
2529 // along the way.
2530 while (isa<CaseStmt>(Sub)) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002531 CFGBlock *currentBlock = createBlock(false);
2532 currentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002533
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002534 if (TopBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002535 addSuccessor(LastBlock, currentBlock);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002536 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002537 TopBlock = currentBlock;
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002538
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002539 addSuccessor(SwitchTerminatedBlock,
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002540 shouldAddCase(switchExclusivelyCovered, switchCond,
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002541 CS, *Context)
2542 ? currentBlock : 0);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002543
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002544 LastBlock = currentBlock;
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002545 CS = cast<CaseStmt>(Sub);
2546 Sub = CS->getSubStmt();
2547 }
2548
2549 addStmt(Sub);
2550 }
Mike Stump1eb44332009-09-09 15:08:12 +00002551
Ted Kremenek9c378f72011-08-12 23:37:29 +00002552 CFGBlock *CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002553 if (!CaseBlock)
2554 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002555
2556 // Cases statements partition blocks, so this is the top of the basic block we
2557 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00002558 CaseBlock->setLabel(CS);
2559
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002560 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002561 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002562
2563 // Add this block to the list of successors for the block with the switch
2564 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00002565 assert(SwitchTerminatedBlock);
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002566 addSuccessor(SwitchTerminatedBlock,
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002567 shouldAddCase(switchExclusivelyCovered, switchCond,
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002568 CS, *Context)
2569 ? CaseBlock : 0);
Mike Stump6d9828c2009-07-17 01:31:16 +00002570
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002571 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2572 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002573
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002574 if (TopBlock) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002575 addSuccessor(LastBlock, CaseBlock);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002576 Succ = TopBlock;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002577 } else {
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002578 // This block is now the implicit successor of other blocks.
2579 Succ = CaseBlock;
2580 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002581
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002582 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002583}
Mike Stump6d9828c2009-07-17 01:31:16 +00002584
Ted Kremenek9c378f72011-08-12 23:37:29 +00002585CFGBlock *CFGBuilder::VisitDefaultStmt(DefaultStmt *Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002586 if (Terminator->getSubStmt())
2587 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00002588
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002589 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002590
2591 if (!DefaultCaseBlock)
2592 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002593
2594 // Default statements partition blocks, so this is the top of the basic block
2595 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00002596 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00002597
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002598 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002599 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002600
Mike Stump6d9828c2009-07-17 01:31:16 +00002601 // Unlike case statements, we don't add the default block to the successors
2602 // for the switch statement immediately. This is done when we finish
2603 // processing the switch statement. This allows for the default case
2604 // (including a fall-through to the code after the switch statement) to always
2605 // be the last successor of a switch-terminated block.
2606
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002607 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2608 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002609
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002610 // This block is now the implicit successor of other blocks.
2611 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002612
2613 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00002614}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002615
Mike Stump5d1d2022010-01-19 02:20:09 +00002616CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2617 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2618 // current block.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002619 CFGBlock *TrySuccessor = NULL;
Mike Stump5d1d2022010-01-19 02:20:09 +00002620
2621 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002622 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002623 return 0;
2624 TrySuccessor = Block;
2625 } else TrySuccessor = Succ;
2626
Mike Stumpa1f93632010-01-20 01:15:34 +00002627 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00002628
2629 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002630 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00002631 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00002632 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00002633
Mike Stumpa1f93632010-01-20 01:15:34 +00002634 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00002635 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2636 // The code after the try is the implicit successor.
2637 Succ = TrySuccessor;
2638 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00002639 if (CS->getExceptionDecl() == 0) {
2640 HasCatchAll = true;
2641 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002642 Block = NULL;
2643 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2644 if (CatchBlock == 0)
2645 return 0;
2646 // Add this block to the list of successors for the block with the try
2647 // statement.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002648 addSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00002649 }
Mike Stumpa1f93632010-01-20 01:15:34 +00002650 if (!HasCatchAll) {
2651 if (PrevTryTerminatedBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002652 addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00002653 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002654 addSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00002655 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002656
2657 // The code after the try is the implicit successor.
2658 Succ = TrySuccessor;
2659
Mike Stumpf00cca52010-01-20 01:30:58 +00002660 // Save the current "try" context.
Ted Kremenekf0e71ae2011-08-23 23:05:07 +00002661 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock, NewTryTerminatedBlock);
2662 cfg->addTryDispatchBlock(TryTerminatedBlock);
Mike Stumpf00cca52010-01-20 01:30:58 +00002663
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002664 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00002665 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002666 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00002667 return Block;
2668}
2669
Ted Kremenek9c378f72011-08-12 23:37:29 +00002670CFGBlock *CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt *CS) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002671 // CXXCatchStmt are treated like labels, so they are the first statement in a
2672 // block.
2673
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002674 // Save local scope position because in case of exception variable ScopePos
2675 // won't be restored when traversing AST.
2676 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2677
2678 // Create local scope for possible exception variable.
2679 // Store scope position. Add implicit destructor.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002680 if (VarDecl *VD = CS->getExceptionDecl()) {
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002681 LocalScope::const_iterator BeginScopePos = ScopePos;
2682 addLocalScopeForVarDecl(VD);
2683 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2684 }
2685
Mike Stump5d1d2022010-01-19 02:20:09 +00002686 if (CS->getHandlerBlock())
2687 addStmt(CS->getHandlerBlock());
2688
Ted Kremenek9c378f72011-08-12 23:37:29 +00002689 CFGBlock *CatchBlock = Block;
Mike Stump5d1d2022010-01-19 02:20:09 +00002690 if (!CatchBlock)
2691 CatchBlock = createBlock();
Ted Kremenek337e4db2012-03-10 01:34:17 +00002692
2693 // CXXCatchStmt is more than just a label. They have semantic meaning
2694 // as well, as they implicitly "initialize" the catch variable. Add
2695 // it to the CFG as a CFGElement so that the control-flow of these
2696 // semantics gets captured.
2697 appendStmt(CatchBlock, CS);
Mike Stump5d1d2022010-01-19 02:20:09 +00002698
Ted Kremenek337e4db2012-03-10 01:34:17 +00002699 // Also add the CXXCatchStmt as a label, to mirror handling of regular
2700 // labels.
Mike Stump5d1d2022010-01-19 02:20:09 +00002701 CatchBlock->setLabel(CS);
2702
Ted Kremenek337e4db2012-03-10 01:34:17 +00002703 // Bail out if the CFG is bad.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002704 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002705 return 0;
2706
2707 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2708 Block = NULL;
2709
2710 return CatchBlock;
2711}
2712
Ted Kremenek9c378f72011-08-12 23:37:29 +00002713CFGBlock *CFGBuilder::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Richard Smithad762fc2011-04-14 22:09:26 +00002714 // C++0x for-range statements are specified as [stmt.ranged]:
2715 //
2716 // {
2717 // auto && __range = range-init;
2718 // for ( auto __begin = begin-expr,
2719 // __end = end-expr;
2720 // __begin != __end;
2721 // ++__begin ) {
2722 // for-range-declaration = *__begin;
2723 // statement
2724 // }
2725 // }
2726
2727 // Save local scope position before the addition of the implicit variables.
2728 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2729
2730 // Create local scopes and destructors for range, begin and end variables.
2731 if (Stmt *Range = S->getRangeStmt())
2732 addLocalScopeForStmt(Range);
2733 if (Stmt *BeginEnd = S->getBeginEndStmt())
2734 addLocalScopeForStmt(BeginEnd);
2735 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), S);
2736
2737 LocalScope::const_iterator ContinueScopePos = ScopePos;
2738
2739 // "for" is a control-flow statement. Thus we stop processing the current
2740 // block.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002741 CFGBlock *LoopSuccessor = NULL;
Richard Smithad762fc2011-04-14 22:09:26 +00002742 if (Block) {
2743 if (badCFG)
2744 return 0;
2745 LoopSuccessor = Block;
2746 } else
2747 LoopSuccessor = Succ;
2748
2749 // Save the current value for the break targets.
2750 // All breaks should go to the code following the loop.
2751 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
2752 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
2753
2754 // The block for the __begin != __end expression.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002755 CFGBlock *ConditionBlock = createBlock(false);
Richard Smithad762fc2011-04-14 22:09:26 +00002756 ConditionBlock->setTerminator(S);
2757
2758 // Now add the actual condition to the condition block.
2759 if (Expr *C = S->getCond()) {
2760 Block = ConditionBlock;
2761 CFGBlock *BeginConditionBlock = addStmt(C);
2762 if (badCFG)
2763 return 0;
2764 assert(BeginConditionBlock == ConditionBlock &&
2765 "condition block in for-range was unexpectedly complex");
2766 (void)BeginConditionBlock;
2767 }
2768
2769 // The condition block is the implicit successor for the loop body as well as
2770 // any code above the loop.
2771 Succ = ConditionBlock;
2772
2773 // See if this is a known constant.
2774 TryResult KnownVal(true);
2775
2776 if (S->getCond())
2777 KnownVal = tryEvaluateBool(S->getCond());
2778
2779 // Now create the loop body.
2780 {
2781 assert(S->getBody());
2782
2783 // Save the current values for Block, Succ, and continue targets.
2784 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2785 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
2786
2787 // Generate increment code in its own basic block. This is the target of
2788 // continue statements.
2789 Block = 0;
2790 Succ = addStmt(S->getInc());
2791 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
2792
2793 // The starting block for the loop increment is the block that should
2794 // represent the 'loop target' for looping back to the start of the loop.
2795 ContinueJumpTarget.block->setLoopTarget(S);
2796
2797 // Finish up the increment block and prepare to start the loop body.
2798 assert(Block);
2799 if (badCFG)
2800 return 0;
2801 Block = 0;
2802
2803
2804 // Add implicit scope and dtors for loop variable.
2805 addLocalScopeAndDtors(S->getLoopVarStmt());
2806
2807 // Populate a new block to contain the loop body and loop variable.
2808 Block = addStmt(S->getBody());
2809 if (badCFG)
2810 return 0;
2811 Block = addStmt(S->getLoopVarStmt());
2812 if (badCFG)
2813 return 0;
2814
2815 // This new body block is a successor to our condition block.
2816 addSuccessor(ConditionBlock, KnownVal.isFalse() ? 0 : Block);
2817 }
2818
2819 // Link up the condition block with the code that follows the loop (the
2820 // false branch).
2821 addSuccessor(ConditionBlock, KnownVal.isTrue() ? 0 : LoopSuccessor);
2822
2823 // Add the initialization statements.
2824 Block = createBlock();
Richard Smithb403d6d2011-04-18 15:49:25 +00002825 addStmt(S->getBeginEndStmt());
2826 return addStmt(S->getRangeStmt());
Richard Smithad762fc2011-04-14 22:09:26 +00002827}
2828
John McCall4765fa02010-12-06 08:20:24 +00002829CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +00002830 AddStmtChoice asc) {
2831 if (BuildOpts.AddImplicitDtors) {
2832 // If adding implicit destructors visit the full expression for adding
2833 // destructors of temporaries.
2834 VisitForTemporaryDtors(E->getSubExpr());
2835
2836 // Full expression has to be added as CFGStmt so it will be sequenced
2837 // before destructors of it's temporaries.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002838 asc = asc.withAlwaysAdd(true);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002839 }
2840 return Visit(E->getSubExpr(), asc);
2841}
2842
Zhongxing Xua725ed42010-11-01 13:04:58 +00002843CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2844 AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00002845 if (asc.alwaysAdd(*this, E)) {
Zhongxing Xua725ed42010-11-01 13:04:58 +00002846 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002847 appendStmt(Block, E);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002848
2849 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002850 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002851 }
2852 return Visit(E->getSubExpr(), asc);
2853}
2854
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002855CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2856 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002857 autoCreateBlock();
Zhongxing Xu97a72c32012-01-11 02:39:07 +00002858 appendStmt(Block, C);
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002859
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002860 return VisitChildren(C);
2861}
2862
Zhongxing Xua725ed42010-11-01 13:04:58 +00002863CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2864 AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00002865 if (asc.alwaysAdd(*this, E)) {
Zhongxing Xua725ed42010-11-01 13:04:58 +00002866 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002867 appendStmt(Block, E);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002868 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002869 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002870 }
2871 return Visit(E->getSubExpr(), asc);
2872}
2873
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002874CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2875 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002876 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002877 appendStmt(Block, C);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002878 return VisitChildren(C);
2879}
2880
Zhongxing Xua725ed42010-11-01 13:04:58 +00002881CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2882 AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00002883 if (asc.alwaysAdd(*this, E)) {
Zhongxing Xua725ed42010-11-01 13:04:58 +00002884 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002885 appendStmt(Block, E);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002886 }
Ted Kremenek892697d2010-12-16 07:46:53 +00002887 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xua725ed42010-11-01 13:04:58 +00002888}
2889
Ted Kremenek9c378f72011-08-12 23:37:29 +00002890CFGBlock *CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt *I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002891 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002892 CFGBlock *IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002893
Ted Kremenek19bb3562007-08-28 19:26:49 +00002894 if (!IBlock) {
2895 IBlock = createBlock(false);
2896 cfg->setIndirectGotoBlock(IBlock);
2897 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002898
Ted Kremenek19bb3562007-08-28 19:26:49 +00002899 // IndirectGoto is a control-flow statement. Thus we stop processing the
2900 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002901 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002902 return 0;
2903
Ted Kremenek19bb3562007-08-28 19:26:49 +00002904 Block = createBlock(false);
2905 Block->setTerminator(I);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002906 addSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00002907 return addStmt(I->getTarget());
2908}
2909
Marcin Swiderski8599e762010-11-03 06:19:35 +00002910CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2911tryAgain:
2912 if (!E) {
2913 badCFG = true;
2914 return NULL;
2915 }
2916 switch (E->getStmtClass()) {
2917 default:
2918 return VisitChildrenForTemporaryDtors(E);
2919
2920 case Stmt::BinaryOperatorClass:
2921 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2922
2923 case Stmt::CXXBindTemporaryExprClass:
2924 return VisitCXXBindTemporaryExprForTemporaryDtors(
2925 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2926
John McCall56ca35d2011-02-17 10:25:35 +00002927 case Stmt::BinaryConditionalOperatorClass:
Marcin Swiderski8599e762010-11-03 06:19:35 +00002928 case Stmt::ConditionalOperatorClass:
2929 return VisitConditionalOperatorForTemporaryDtors(
John McCall56ca35d2011-02-17 10:25:35 +00002930 cast<AbstractConditionalOperator>(E), BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002931
2932 case Stmt::ImplicitCastExprClass:
2933 // For implicit cast we want BindToTemporary to be passed further.
2934 E = cast<CastExpr>(E)->getSubExpr();
2935 goto tryAgain;
2936
2937 case Stmt::ParenExprClass:
2938 E = cast<ParenExpr>(E)->getSubExpr();
2939 goto tryAgain;
Douglas Gregor03e80032011-06-21 17:03:29 +00002940
2941 case Stmt::MaterializeTemporaryExprClass:
2942 E = cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr();
2943 goto tryAgain;
Marcin Swiderski8599e762010-11-03 06:19:35 +00002944 }
2945}
2946
2947CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2948 // When visiting children for destructors we want to visit them in reverse
2949 // order. Because there's no reverse iterator for children must to reverse
2950 // them in helper vector.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002951 typedef SmallVector<Stmt *, 4> ChildrenVect;
Marcin Swiderski8599e762010-11-03 06:19:35 +00002952 ChildrenVect ChildrenRev;
John McCall7502c1d2011-02-13 04:07:26 +00002953 for (Stmt::child_range I = E->children(); I; ++I) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002954 if (*I) ChildrenRev.push_back(*I);
2955 }
2956
2957 CFGBlock *B = Block;
2958 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2959 L = ChildrenRev.rend(); I != L; ++I) {
2960 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2961 B = R;
2962 }
2963 return B;
2964}
2965
2966CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2967 if (E->isLogicalOp()) {
2968 // Destructors for temporaries in LHS expression should be called after
2969 // those for RHS expression. Even if this will unnecessarily create a block,
2970 // this block will be used at least by the full expression.
2971 autoCreateBlock();
2972 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2973 if (badCFG)
2974 return NULL;
2975
2976 Succ = ConfluenceBlock;
2977 Block = NULL;
2978 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2979
2980 if (RHSBlock) {
2981 if (badCFG)
2982 return NULL;
2983
2984 // If RHS expression did produce destructors we need to connect created
2985 // blocks to CFG in same manner as for binary operator itself.
2986 CFGBlock *LHSBlock = createBlock(false);
2987 LHSBlock->setTerminator(CFGTerminator(E, true));
2988
2989 // For binary operator LHS block is before RHS in list of predecessors
2990 // of ConfluenceBlock.
2991 std::reverse(ConfluenceBlock->pred_begin(),
2992 ConfluenceBlock->pred_end());
2993
2994 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002995 TryResult KnownVal = tryEvaluateBool(E->getLHS());
Marcin Swiderski8599e762010-11-03 06:19:35 +00002996 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2997 KnownVal.negate();
2998
2999 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
3000 // itself.
3001 if (E->getOpcode() == BO_LOr) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003002 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
3003 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003004 } else {
3005 assert (E->getOpcode() == BO_LAnd);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003006 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
3007 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003008 }
3009
3010 Block = LHSBlock;
3011 return LHSBlock;
3012 }
3013
3014 Block = ConfluenceBlock;
3015 return ConfluenceBlock;
3016 }
3017
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003018 if (E->isAssignmentOp()) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00003019 // For assignment operator (=) LHS expression is visited
3020 // before RHS expression. For destructors visit them in reverse order.
3021 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
3022 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
3023 return LHSBlock ? LHSBlock : RHSBlock;
3024 }
3025
3026 // For any other binary operator RHS expression is visited before
3027 // LHS expression (order of children). For destructors visit them in reverse
3028 // order.
3029 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
3030 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
3031 return RHSBlock ? RHSBlock : LHSBlock;
3032}
3033
3034CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
3035 CXXBindTemporaryExpr *E, bool BindToTemporary) {
3036 // First add destructors for temporaries in subexpression.
3037 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xu249c9452010-11-14 15:23:50 +00003038 if (!BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00003039 // If lifetime of temporary is not prolonged (by assigning to constant
3040 // reference) add destructor for it.
Chandler Carruthc8cfc742011-09-13 06:09:01 +00003041
3042 // If the destructor is marked as a no-return destructor, we need to create
3043 // a new block for the destructor which does not have as a successor
3044 // anything built thus far. Control won't flow out of this block.
3045 const CXXDestructorDecl *Dtor = E->getTemporary()->getDestructor();
Chandler Carruthdba3fb52011-09-13 09:13:49 +00003046 if (cast<FunctionType>(Dtor->getType())->getNoReturnAttr())
3047 Block = createNoReturnBlock();
3048 else
Chandler Carruthc8cfc742011-09-13 06:09:01 +00003049 autoCreateBlock();
Chandler Carruthc8cfc742011-09-13 06:09:01 +00003050
Marcin Swiderski8599e762010-11-03 06:19:35 +00003051 appendTemporaryDtor(Block, E);
3052 B = Block;
3053 }
3054 return B;
3055}
3056
3057CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
John McCall56ca35d2011-02-17 10:25:35 +00003058 AbstractConditionalOperator *E, bool BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00003059 // First add destructors for condition expression. Even if this will
3060 // unnecessarily create a block, this block will be used at least by the full
3061 // expression.
3062 autoCreateBlock();
3063 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
3064 if (badCFG)
3065 return NULL;
John McCall56ca35d2011-02-17 10:25:35 +00003066 if (BinaryConditionalOperator *BCO
3067 = dyn_cast<BinaryConditionalOperator>(E)) {
3068 ConfluenceBlock = VisitForTemporaryDtors(BCO->getCommon());
Marcin Swiderski8599e762010-11-03 06:19:35 +00003069 if (badCFG)
3070 return NULL;
3071 }
3072
John McCall56ca35d2011-02-17 10:25:35 +00003073 // Try to add block with destructors for LHS expression.
3074 CFGBlock *LHSBlock = NULL;
3075 Succ = ConfluenceBlock;
3076 Block = NULL;
3077 LHSBlock = VisitForTemporaryDtors(E->getTrueExpr(), BindToTemporary);
3078 if (badCFG)
3079 return NULL;
3080
Marcin Swiderski8599e762010-11-03 06:19:35 +00003081 // Try to add block with destructors for RHS expression;
3082 Succ = ConfluenceBlock;
3083 Block = NULL;
John McCall56ca35d2011-02-17 10:25:35 +00003084 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getFalseExpr(),
3085 BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003086 if (badCFG)
3087 return NULL;
3088
3089 if (!RHSBlock && !LHSBlock) {
3090 // If neither LHS nor RHS expression had temporaries to destroy don't create
3091 // more blocks.
3092 Block = ConfluenceBlock;
3093 return Block;
3094 }
3095
3096 Block = createBlock(false);
3097 Block->setTerminator(CFGTerminator(E, true));
3098
3099 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003100 const TryResult &KnownVal = tryEvaluateBool(E->getCond());
Marcin Swiderski8599e762010-11-03 06:19:35 +00003101
3102 if (LHSBlock) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003103 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003104 } else if (KnownVal.isFalse()) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003105 addSuccessor(Block, NULL);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003106 } else {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003107 addSuccessor(Block, ConfluenceBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003108 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
3109 }
3110
3111 if (!RHSBlock)
3112 RHSBlock = ConfluenceBlock;
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003113 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003114
3115 return Block;
3116}
3117
Ted Kremenekbefef2f2007-08-23 21:26:19 +00003118} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00003119
Mike Stump6d9828c2009-07-17 01:31:16 +00003120/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
3121/// no successors or predecessors. If this is the first block created in the
3122/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003123CFGBlock *CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00003124 bool first_block = begin() == end();
3125
3126 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003127 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
Anna Zaks02f34c52011-12-05 21:33:11 +00003128 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC, this);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003129 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00003130
3131 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003132 if (first_block)
3133 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00003134
3135 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003136 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00003137}
3138
Ted Kremenek026473c2007-08-23 16:51:22 +00003139/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
3140/// CFG is returned to the caller.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003141CFG* CFG::buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +00003142 const BuildOptions &BO) {
3143 CFGBuilder Builder(C, BO);
3144 return Builder.buildCFG(D, Statement);
Ted Kremenek026473c2007-08-23 16:51:22 +00003145}
3146
Ted Kremenekc5aff442011-03-03 01:21:32 +00003147const CXXDestructorDecl *
3148CFGImplicitDtor::getDestructorDecl(ASTContext &astContext) const {
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003149 switch (getKind()) {
3150 case CFGElement::Invalid:
3151 case CFGElement::Statement:
3152 case CFGElement::Initializer:
3153 llvm_unreachable("getDestructorDecl should only be used with "
3154 "ImplicitDtors");
3155 case CFGElement::AutomaticObjectDtor: {
3156 const VarDecl *var = cast<CFGAutomaticObjDtor>(this)->getVarDecl();
3157 QualType ty = var->getType();
Ted Kremenek697d42d2011-03-03 01:01:03 +00003158 ty = ty.getNonReferenceType();
Ted Kremenek4cf22532012-03-19 23:48:41 +00003159 while (const ArrayType *arrayType = astContext.getAsArrayType(ty)) {
Ted Kremenekc5aff442011-03-03 01:21:32 +00003160 ty = arrayType->getElementType();
3161 }
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003162 const RecordType *recordType = ty->getAs<RecordType>();
3163 const CXXRecordDecl *classDecl =
Ted Kremenek697d42d2011-03-03 01:01:03 +00003164 cast<CXXRecordDecl>(recordType->getDecl());
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003165 return classDecl->getDestructor();
3166 }
3167 case CFGElement::TemporaryDtor: {
3168 const CXXBindTemporaryExpr *bindExpr =
3169 cast<CFGTemporaryDtor>(this)->getBindTemporaryExpr();
3170 const CXXTemporary *temp = bindExpr->getTemporary();
3171 return temp->getDestructor();
3172 }
3173 case CFGElement::BaseDtor:
3174 case CFGElement::MemberDtor:
3175
3176 // Not yet supported.
3177 return 0;
3178 }
Ted Kremenek697d42d2011-03-03 01:01:03 +00003179 llvm_unreachable("getKind() returned bogus value");
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003180}
3181
Ted Kremenekc5aff442011-03-03 01:21:32 +00003182bool CFGImplicitDtor::isNoReturn(ASTContext &astContext) const {
3183 if (const CXXDestructorDecl *cdecl = getDestructorDecl(astContext)) {
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003184 QualType ty = cdecl->getType();
3185 return cast<FunctionType>(ty)->getNoReturnAttr();
3186 }
3187 return false;
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003188}
3189
Ted Kremenek63f58872007-10-01 19:33:33 +00003190//===----------------------------------------------------------------------===//
3191// CFG: Queries for BlkExprs.
3192//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00003193
Ted Kremenek63f58872007-10-01 19:33:33 +00003194namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00003195 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00003196}
3197
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003198static void FindSubExprAssignments(const Stmt *S,
3199 llvm::SmallPtrSet<const Expr*,50>& Set) {
Ted Kremenek8a693662009-12-23 23:37:10 +00003200 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003201 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00003202
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003203 for (Stmt::const_child_range I = S->children(); I; ++I) {
3204 const Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00003205 if (!child)
3206 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00003207
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003208 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003209 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00003210
Ted Kremenek8a693662009-12-23 23:37:10 +00003211 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003212 }
3213}
3214
Ted Kremenek63f58872007-10-01 19:33:33 +00003215static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
3216 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00003217
3218 // Look for assignments that are used as subexpressions. These are the only
3219 // assignments that we want to *possibly* register as a block-level
3220 // expression. Basically, if an assignment occurs both in a subexpression and
3221 // at the block-level, it is a block-level expression.
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003222 llvm::SmallPtrSet<const Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00003223
Ted Kremenek63f58872007-10-01 19:33:33 +00003224 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003225 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003226 if (const CFGStmt *S = BI->getAs<CFGStmt>())
3227 FindSubExprAssignments(S->getStmt(), SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00003228
Ted Kremenek411cdee2008-04-16 21:10:48 +00003229 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003230
3231 // Iterate over the statements again on identify the Expr* and Stmt* at the
3232 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00003233
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00003234 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003235 const CFGStmt *CS = BI->getAs<CFGStmt>();
3236 if (!CS)
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00003237 continue;
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003238 if (const Expr *Exp = dyn_cast<Expr>(CS->getStmt())) {
Jordy Roseac73ea82011-06-10 08:49:37 +00003239 assert((Exp->IgnoreParens() == Exp) && "No parens on block-level exps");
Mike Stump6d9828c2009-07-17 01:31:16 +00003240
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003241 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003242 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00003243 // expression are really "statements" whose value is never used by
3244 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00003245 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003246 continue;
Ted Kremenek9c378f72011-08-12 23:37:29 +00003247 } else if (const StmtExpr *SE = dyn_cast<StmtExpr>(Exp)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003248 // Special handling for statement expressions. The last statement in
3249 // the statement expression is also a block-level expr.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003250 const CompoundStmt *C = SE->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00003251 if (!C->body_empty()) {
Jordy Roseac73ea82011-06-10 08:49:37 +00003252 const Stmt *Last = C->body_back();
3253 if (const Expr *LastEx = dyn_cast<Expr>(Last))
3254 Last = LastEx->IgnoreParens();
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003255 unsigned x = M->size();
Jordy Roseac73ea82011-06-10 08:49:37 +00003256 (*M)[Last] = x;
Ted Kremenek86946742008-01-17 20:48:37 +00003257 }
3258 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00003259
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003260 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00003261 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003262 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00003263 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003264
Ted Kremenek411cdee2008-04-16 21:10:48 +00003265 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00003266
Ted Kremenek9c378f72011-08-12 23:37:29 +00003267 Stmt *S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00003268
Ted Kremenek390e48b2008-11-12 21:11:49 +00003269 if (S && M->find(S) == M->end()) {
Jordy Roseac73ea82011-06-10 08:49:37 +00003270 unsigned x = M->size();
3271 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003272 }
3273 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003274
Ted Kremenek63f58872007-10-01 19:33:33 +00003275 return M;
3276}
3277
Ted Kremenek9c378f72011-08-12 23:37:29 +00003278CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt *S) {
Ted Kremenek86946742008-01-17 20:48:37 +00003279 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00003280 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00003281
Ted Kremenek63f58872007-10-01 19:33:33 +00003282 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00003283 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003284 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00003285}
3286
3287unsigned CFG::getNumBlkExprs() {
3288 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
3289 return M->size();
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003290
3291 // We assume callers interested in the number of BlkExprs will want
3292 // the map constructed if it doesn't already exist.
3293 BlkExprMap = (void*) PopulateBlkExprMap(*this);
3294 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenek63f58872007-10-01 19:33:33 +00003295}
3296
Ted Kremenek274f4332008-04-28 18:00:46 +00003297//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003298// Filtered walking of the CFG.
3299//===----------------------------------------------------------------------===//
3300
3301bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00003302 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003303
Ted Kremenek6e400352011-03-07 22:04:39 +00003304 if (To && F.IgnoreDefaultsWithCoveredEnums) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003305 // If the 'To' has no label or is labeled but the label isn't a
3306 // CaseStmt then filter this edge.
3307 if (const SwitchStmt *S =
Ted Kremenek6e400352011-03-07 22:04:39 +00003308 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003309 if (S->isAllEnumCasesCovered()) {
Ted Kremenek6e400352011-03-07 22:04:39 +00003310 const Stmt *L = To->getLabel();
3311 if (!L || !isa<CaseStmt>(L))
3312 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003313 }
3314 }
3315 }
3316
3317 return false;
3318}
3319
3320//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00003321// Cleanup: CFG dstor.
3322//===----------------------------------------------------------------------===//
3323
Ted Kremenek63f58872007-10-01 19:33:33 +00003324CFG::~CFG() {
3325 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
3326}
Mike Stump6d9828c2009-07-17 01:31:16 +00003327
Ted Kremenek7dba8602007-08-29 21:56:09 +00003328//===----------------------------------------------------------------------===//
3329// CFG pretty printing
3330//===----------------------------------------------------------------------===//
3331
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00003332namespace {
3333
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00003334class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003335 typedef llvm::DenseMap<const Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
3336 typedef llvm::DenseMap<const Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003337 StmtMapTy StmtMap;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003338 DeclMapTy DeclMap;
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003339 signed currentBlock;
3340 unsigned currentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00003341 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003342public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003343
Chris Lattnere4f21422009-06-30 01:26:17 +00003344 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003345 : currentBlock(0), currentStmt(0), LangOpts(LO)
3346 {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003347 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
3348 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003349 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003350 BI != BEnd; ++BI, ++j ) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003351 if (const CFGStmt *SE = BI->getAs<CFGStmt>()) {
3352 const Stmt *stmt= SE->getStmt();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003353 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003354 StmtMap[stmt] = P;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003355
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003356 switch (stmt->getStmtClass()) {
3357 case Stmt::DeclStmtClass:
3358 DeclMap[cast<DeclStmt>(stmt)->getSingleDecl()] = P;
3359 break;
3360 case Stmt::IfStmtClass: {
3361 const VarDecl *var = cast<IfStmt>(stmt)->getConditionVariable();
3362 if (var)
3363 DeclMap[var] = P;
3364 break;
3365 }
3366 case Stmt::ForStmtClass: {
3367 const VarDecl *var = cast<ForStmt>(stmt)->getConditionVariable();
3368 if (var)
3369 DeclMap[var] = P;
3370 break;
3371 }
3372 case Stmt::WhileStmtClass: {
3373 const VarDecl *var =
3374 cast<WhileStmt>(stmt)->getConditionVariable();
3375 if (var)
3376 DeclMap[var] = P;
3377 break;
3378 }
3379 case Stmt::SwitchStmtClass: {
3380 const VarDecl *var =
3381 cast<SwitchStmt>(stmt)->getConditionVariable();
3382 if (var)
3383 DeclMap[var] = P;
3384 break;
3385 }
3386 case Stmt::CXXCatchStmtClass: {
3387 const VarDecl *var =
3388 cast<CXXCatchStmt>(stmt)->getExceptionDecl();
3389 if (var)
3390 DeclMap[var] = P;
3391 break;
3392 }
3393 default:
3394 break;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003395 }
3396 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003397 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00003398 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003399 }
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003400
Mike Stump6d9828c2009-07-17 01:31:16 +00003401
Ted Kremenek42a509f2007-08-31 21:30:12 +00003402 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00003403
Chris Lattnere4f21422009-06-30 01:26:17 +00003404 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003405 void setBlockID(signed i) { currentBlock = i; }
3406 void setStmtID(unsigned i) { currentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00003407
Ted Kremenek9c378f72011-08-12 23:37:29 +00003408 virtual bool handledStmt(Stmt *S, raw_ostream &OS) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003409 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003410
3411 if (I == StmtMap.end())
3412 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003413
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003414 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3415 && I->second.second == currentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003416 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003417 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003418
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003419 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003420 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003421 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003422
Ted Kremenek9c378f72011-08-12 23:37:29 +00003423 bool handleDecl(const Decl *D, raw_ostream &OS) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003424 DeclMapTy::iterator I = DeclMap.find(D);
3425
3426 if (I == DeclMap.end())
3427 return false;
3428
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003429 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3430 && I->second.second == currentStmt) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003431 return false;
3432 }
3433
3434 OS << "[B" << I->second.first << "." << I->second.second << "]";
3435 return true;
3436 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003437};
Chris Lattnere4f21422009-06-30 01:26:17 +00003438} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00003439
Chris Lattnere4f21422009-06-30 01:26:17 +00003440
3441namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00003442class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00003443 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00003444
Ted Kremenek9c378f72011-08-12 23:37:29 +00003445 raw_ostream &OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003446 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003447 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003448public:
Ted Kremenek9c378f72011-08-12 23:37:29 +00003449 CFGBlockTerminatorPrint(raw_ostream &os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00003450 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003451 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00003452
Ted Kremenek9c378f72011-08-12 23:37:29 +00003453 void VisitIfStmt(IfStmt *I) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003454 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003455 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003456 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003457
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003458 // Default case.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003459 void VisitStmt(Stmt *Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003460 Terminator->printPretty(OS, Helper, Policy);
3461 }
3462
Ted Kremenek9c378f72011-08-12 23:37:29 +00003463 void VisitForStmt(ForStmt *F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003464 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003465 if (F->getInit())
3466 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00003467 OS << "; ";
Ted Kremenek9c378f72011-08-12 23:37:29 +00003468 if (Stmt *C = F->getCond())
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003469 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00003470 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003471 if (F->getInc())
3472 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00003473 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003474 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003475
Ted Kremenek9c378f72011-08-12 23:37:29 +00003476 void VisitWhileStmt(WhileStmt *W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003477 OS << "while " ;
Ted Kremenek9c378f72011-08-12 23:37:29 +00003478 if (Stmt *C = W->getCond())
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003479 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003480 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003481
Ted Kremenek9c378f72011-08-12 23:37:29 +00003482 void VisitDoStmt(DoStmt *D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003483 OS << "do ... while ";
Ted Kremenek9c378f72011-08-12 23:37:29 +00003484 if (Stmt *C = D->getCond())
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003485 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00003486 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003487
Ted Kremenek9c378f72011-08-12 23:37:29 +00003488 void VisitSwitchStmt(SwitchStmt *Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00003489 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003490 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00003491 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003492
Ted Kremenek9c378f72011-08-12 23:37:29 +00003493 void VisitCXXTryStmt(CXXTryStmt *CS) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003494 OS << "try ...";
3495 }
3496
John McCall56ca35d2011-02-17 10:25:35 +00003497 void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003498 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003499 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003500 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003501
Ted Kremenek9c378f72011-08-12 23:37:29 +00003502 void VisitChooseExpr(ChooseExpr *C) {
Ted Kremenekaeddbf62007-08-31 22:29:13 +00003503 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003504 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00003505 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00003506 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003507
Ted Kremenek9c378f72011-08-12 23:37:29 +00003508 void VisitIndirectGotoStmt(IndirectGotoStmt *I) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003509 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003510 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003511 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003512
Ted Kremenek805e9a82007-08-31 21:49:40 +00003513 void VisitBinaryOperator(BinaryOperator* B) {
3514 if (!B->isLogicalOp()) {
3515 VisitExpr(B);
3516 return;
3517 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003518
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003519 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003520
Ted Kremenek805e9a82007-08-31 21:49:40 +00003521 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00003522 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00003523 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003524 return;
John McCall2de56d12010-08-25 11:45:40 +00003525 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00003526 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003527 return;
3528 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003529 llvm_unreachable("Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003530 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00003531 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003532
Ted Kremenek9c378f72011-08-12 23:37:29 +00003533 void VisitExpr(Expr *E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003534 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003535 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003536};
Chris Lattnere4f21422009-06-30 01:26:17 +00003537} // end anonymous namespace
3538
Chris Lattner5f9e2722011-07-23 10:55:15 +00003539static void print_elem(raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00003540 const CFGElement &E) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003541 if (const CFGStmt *CS = E.getAs<CFGStmt>()) {
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003542 const Stmt *S = CS->getStmt();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003543
3544 if (Helper) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003545
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003546 // special printing for statement-expressions.
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003547 if (const StmtExpr *SE = dyn_cast<StmtExpr>(S)) {
3548 const CompoundStmt *Sub = SE->getSubStmt();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003549
John McCall7502c1d2011-02-13 04:07:26 +00003550 if (Sub->children()) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003551 OS << "({ ... ; ";
3552 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3553 OS << " })\n";
3554 return;
3555 }
3556 }
3557 // special printing for comma expressions.
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003558 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003559 if (B->getOpcode() == BO_Comma) {
3560 OS << "... , ";
3561 Helper->handledStmt(B->getRHS(),OS);
3562 OS << '\n';
3563 return;
3564 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003565 }
3566 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003567 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00003568
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003569 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003570 OS << " (OperatorCall)";
Ted Kremenek893d4142011-12-21 19:32:38 +00003571 }
3572 else if (isa<CXXBindTemporaryExpr>(S)) {
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003573 OS << " (BindTemporary)";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003574 }
Ted Kremenek3b7a48f2011-12-21 19:39:59 +00003575 else if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(S)) {
3576 OS << " (CXXConstructExpr, " << CCE->getType().getAsString() << ")";
3577 }
Ted Kremenek893d4142011-12-21 19:32:38 +00003578 else if (const CastExpr *CE = dyn_cast<CastExpr>(S)) {
3579 OS << " (" << CE->getStmtClassName() << ", "
3580 << CE->getCastKindName()
3581 << ", " << CE->getType().getAsString()
3582 << ")";
3583 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003584
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003585 // Expressions need a newline.
3586 if (isa<Expr>(S))
3587 OS << '\n';
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00003588
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003589 } else if (const CFGInitializer *IE = E.getAs<CFGInitializer>()) {
3590 const CXXCtorInitializer *I = IE->getInitializer();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003591 if (I->isBaseInitializer())
3592 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichet00eb3f92010-12-04 09:14:42 +00003593 else OS << I->getAnyMember()->getName();
Mike Stump6d9828c2009-07-17 01:31:16 +00003594
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003595 OS << "(";
Ted Kremenek9c378f72011-08-12 23:37:29 +00003596 if (Expr *IE = I->getInit())
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003597 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3598 OS << ")";
3599
3600 if (I->isBaseInitializer())
3601 OS << " (Base initializer)\n";
3602 else OS << " (Member initializer)\n";
3603
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003604 } else if (const CFGAutomaticObjDtor *DE = E.getAs<CFGAutomaticObjDtor>()){
Ted Kremenek9c378f72011-08-12 23:37:29 +00003605 const VarDecl *VD = DE->getVarDecl();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003606 Helper->handleDecl(VD, OS);
3607
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003608 const Type* T = VD->getType().getTypePtr();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003609 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3610 T = RT->getPointeeType().getTypePtr();
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003611 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3612 T = ET;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003613
3614 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3615 OS << " (Implicit destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003616
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003617 } else if (const CFGBaseDtor *BE = E.getAs<CFGBaseDtor>()) {
3618 const CXXBaseSpecifier *BS = BE->getBaseSpecifier();
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003619 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003620 OS << " (Base object destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003621
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003622 } else if (const CFGMemberDtor *ME = E.getAs<CFGMemberDtor>()) {
3623 const FieldDecl *FD = ME->getFieldDecl();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003624
3625 const Type *T = FD->getType().getTypePtr();
3626 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3627 T = ET;
3628
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003629 OS << "this->" << FD->getName();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003630 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003631 OS << " (Member object destructor)\n";
Marcin Swiderski8599e762010-11-03 06:19:35 +00003632
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003633 } else if (const CFGTemporaryDtor *TE = E.getAs<CFGTemporaryDtor>()) {
3634 const CXXBindTemporaryExpr *BT = TE->getBindTemporaryExpr();
Marcin Swiderski8599e762010-11-03 06:19:35 +00003635 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3636 OS << " (Temporary object destructor)\n";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003637 }
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00003638}
Mike Stump6d9828c2009-07-17 01:31:16 +00003639
Ted Kremenek9c378f72011-08-12 23:37:29 +00003640static void print_block(raw_ostream &OS, const CFG* cfg,
3641 const CFGBlock &B,
Ted Kremenek682060c2011-12-22 23:33:52 +00003642 StmtPrinterHelper* Helper, bool print_edges,
3643 bool ShowColors) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003644
Ted Kremenek682060c2011-12-22 23:33:52 +00003645 if (Helper)
3646 Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00003647
Ted Kremenek7dba8602007-08-29 21:56:09 +00003648 // Print the header.
Ted Kremenek682060c2011-12-22 23:33:52 +00003649 if (ShowColors)
3650 OS.changeColor(raw_ostream::YELLOW, true);
3651
3652 OS << "\n [B" << B.getBlockID();
Mike Stump6d9828c2009-07-17 01:31:16 +00003653
Ted Kremenek42a509f2007-08-31 21:30:12 +00003654 if (&B == &cfg->getEntry())
Ted Kremenek682060c2011-12-22 23:33:52 +00003655 OS << " (ENTRY)]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003656 else if (&B == &cfg->getExit())
Ted Kremenek682060c2011-12-22 23:33:52 +00003657 OS << " (EXIT)]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003658 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek682060c2011-12-22 23:33:52 +00003659 OS << " (INDIRECT GOTO DISPATCH)]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003660 else
Ted Kremenek682060c2011-12-22 23:33:52 +00003661 OS << "]\n";
3662
3663 if (ShowColors)
3664 OS.resetColor();
Mike Stump6d9828c2009-07-17 01:31:16 +00003665
Ted Kremenek9cffe732007-08-29 23:20:49 +00003666 // Print the label of this block.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003667 if (Stmt *Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003668
3669 if (print_edges)
Ted Kremenek682060c2011-12-22 23:33:52 +00003670 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003671
Ted Kremenek9c378f72011-08-12 23:37:29 +00003672 if (LabelStmt *L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003673 OS << L->getName();
Ted Kremenek9c378f72011-08-12 23:37:29 +00003674 else if (CaseStmt *C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00003675 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003676 C->getLHS()->printPretty(OS, Helper,
3677 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003678 if (C->getRHS()) {
3679 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003680 C->getRHS()->printPretty(OS, Helper,
3681 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003682 }
Mike Stump079bd722010-01-19 22:00:14 +00003683 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003684 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00003685 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003686 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00003687 if (CS->getExceptionDecl())
3688 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3689 0);
3690 else
3691 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00003692 OS << ")";
3693
3694 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00003695 llvm_unreachable("Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003696
Ted Kremenek9cffe732007-08-29 23:20:49 +00003697 OS << ":\n";
3698 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003699
Ted Kremenekfddd5182007-08-21 21:42:03 +00003700 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00003701 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00003702
Ted Kremenek42a509f2007-08-31 21:30:12 +00003703 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3704 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003705
Ted Kremenek9cffe732007-08-29 23:20:49 +00003706 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003707 if (print_edges)
Ted Kremenek682060c2011-12-22 23:33:52 +00003708 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003709
Ted Kremeneka95d3752008-09-13 05:16:45 +00003710 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003711
Ted Kremenek42a509f2007-08-31 21:30:12 +00003712 if (Helper)
3713 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00003714
Ted Kremenek682060c2011-12-22 23:33:52 +00003715 print_elem(OS, Helper, *I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00003716 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003717
Ted Kremenek9cffe732007-08-29 23:20:49 +00003718 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003719 if (B.getTerminator()) {
Ted Kremenek682060c2011-12-22 23:33:52 +00003720 if (ShowColors)
3721 OS.changeColor(raw_ostream::GREEN);
Mike Stump6d9828c2009-07-17 01:31:16 +00003722
Ted Kremenek682060c2011-12-22 23:33:52 +00003723 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003724
Ted Kremenek42a509f2007-08-31 21:30:12 +00003725 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00003726
Chris Lattnere4f21422009-06-30 01:26:17 +00003727 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3728 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003729 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003730 OS << '\n';
Ted Kremenek682060c2011-12-22 23:33:52 +00003731
3732 if (ShowColors)
3733 OS.resetColor();
Ted Kremenekfddd5182007-08-21 21:42:03 +00003734 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003735
Ted Kremenek9cffe732007-08-29 23:20:49 +00003736 if (print_edges) {
3737 // Print the predecessors of this block.
Ted Kremenek682060c2011-12-22 23:33:52 +00003738 if (!B.pred_empty()) {
3739 const raw_ostream::Colors Color = raw_ostream::BLUE;
3740 if (ShowColors)
3741 OS.changeColor(Color);
3742 OS << " Preds " ;
3743 if (ShowColors)
3744 OS.resetColor();
3745 OS << '(' << B.pred_size() << "):";
3746 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00003747
Ted Kremenek682060c2011-12-22 23:33:52 +00003748 if (ShowColors)
3749 OS.changeColor(Color);
3750
3751 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3752 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003753
Ted Kremenek682060c2011-12-22 23:33:52 +00003754 if (i == 8 || (i-8) == 0)
3755 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003756
Ted Kremenek682060c2011-12-22 23:33:52 +00003757 OS << " B" << (*I)->getBlockID();
3758 }
3759
3760 if (ShowColors)
3761 OS.resetColor();
3762
3763 OS << '\n';
Ted Kremenek9cffe732007-08-29 23:20:49 +00003764 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003765
Ted Kremenek42a509f2007-08-31 21:30:12 +00003766 // Print the successors of this block.
Ted Kremenek682060c2011-12-22 23:33:52 +00003767 if (!B.succ_empty()) {
3768 const raw_ostream::Colors Color = raw_ostream::MAGENTA;
3769 if (ShowColors)
3770 OS.changeColor(Color);
3771 OS << " Succs ";
3772 if (ShowColors)
3773 OS.resetColor();
3774 OS << '(' << B.succ_size() << "):";
3775 unsigned i = 0;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003776
Ted Kremenek682060c2011-12-22 23:33:52 +00003777 if (ShowColors)
3778 OS.changeColor(Color);
Mike Stump6d9828c2009-07-17 01:31:16 +00003779
Ted Kremenek682060c2011-12-22 23:33:52 +00003780 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3781 I != E; ++I, ++i) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003782
Ted Kremenek682060c2011-12-22 23:33:52 +00003783 if (i == 8 || (i-8) % 10 == 0)
3784 OS << "\n ";
3785
3786 if (*I)
3787 OS << " B" << (*I)->getBlockID();
3788 else
3789 OS << " NULL";
3790 }
3791
3792 if (ShowColors)
3793 OS.resetColor();
3794 OS << '\n';
Ted Kremenek42a509f2007-08-31 21:30:12 +00003795 }
Ted Kremenekfddd5182007-08-21 21:42:03 +00003796 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003797}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003798
Ted Kremenek42a509f2007-08-31 21:30:12 +00003799
3800/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek682060c2011-12-22 23:33:52 +00003801void CFG::dump(const LangOptions &LO, bool ShowColors) const {
3802 print(llvm::errs(), LO, ShowColors);
3803}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003804
3805/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenek682060c2011-12-22 23:33:52 +00003806void CFG::print(raw_ostream &OS, const LangOptions &LO, bool ShowColors) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003807 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00003808
Ted Kremenek42a509f2007-08-31 21:30:12 +00003809 // Print the entry block.
Ted Kremenek682060c2011-12-22 23:33:52 +00003810 print_block(OS, this, getEntry(), &Helper, true, ShowColors);
Mike Stump6d9828c2009-07-17 01:31:16 +00003811
Ted Kremenek42a509f2007-08-31 21:30:12 +00003812 // Iterate through the CFGBlocks and print them one by one.
3813 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3814 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003815 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00003816 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00003817
Ted Kremenek682060c2011-12-22 23:33:52 +00003818 print_block(OS, this, **I, &Helper, true, ShowColors);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003819 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003820
Ted Kremenek42a509f2007-08-31 21:30:12 +00003821 // Print the exit block.
Ted Kremenek682060c2011-12-22 23:33:52 +00003822 print_block(OS, this, getExit(), &Helper, true, ShowColors);
3823 OS << '\n';
Ted Kremenekd0172432008-11-24 20:50:24 +00003824 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00003825}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003826
3827/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek682060c2011-12-22 23:33:52 +00003828void CFGBlock::dump(const CFG* cfg, const LangOptions &LO,
3829 bool ShowColors) const {
3830 print(llvm::errs(), cfg, LO, ShowColors);
Chris Lattnere4f21422009-06-30 01:26:17 +00003831}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003832
3833/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3834/// Generally this will only be called from CFG::print.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003835void CFGBlock::print(raw_ostream &OS, const CFG* cfg,
Ted Kremenek682060c2011-12-22 23:33:52 +00003836 const LangOptions &LO, bool ShowColors) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003837 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek682060c2011-12-22 23:33:52 +00003838 print_block(OS, cfg, *this, &Helper, true, ShowColors);
3839 OS << '\n';
Ted Kremenek026473c2007-08-23 16:51:22 +00003840}
Ted Kremenek7dba8602007-08-29 21:56:09 +00003841
Ted Kremeneka2925852008-01-30 23:02:42 +00003842/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003843void CFGBlock::printTerminator(raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00003844 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003845 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003846 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003847}
3848
Ted Kremenek9c378f72011-08-12 23:37:29 +00003849Stmt *CFGBlock::getTerminatorCondition() {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003850 Stmt *Terminator = this->Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003851 if (!Terminator)
3852 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003853
Ted Kremenek9c378f72011-08-12 23:37:29 +00003854 Expr *E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003855
Ted Kremenek411cdee2008-04-16 21:10:48 +00003856 switch (Terminator->getStmtClass()) {
3857 default:
3858 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003859
Ted Kremenek411cdee2008-04-16 21:10:48 +00003860 case Stmt::ForStmtClass:
3861 E = cast<ForStmt>(Terminator)->getCond();
3862 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003863
Ted Kremenek411cdee2008-04-16 21:10:48 +00003864 case Stmt::WhileStmtClass:
3865 E = cast<WhileStmt>(Terminator)->getCond();
3866 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003867
Ted Kremenek411cdee2008-04-16 21:10:48 +00003868 case Stmt::DoStmtClass:
3869 E = cast<DoStmt>(Terminator)->getCond();
3870 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003871
Ted Kremenek411cdee2008-04-16 21:10:48 +00003872 case Stmt::IfStmtClass:
3873 E = cast<IfStmt>(Terminator)->getCond();
3874 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003875
Ted Kremenek411cdee2008-04-16 21:10:48 +00003876 case Stmt::ChooseExprClass:
3877 E = cast<ChooseExpr>(Terminator)->getCond();
3878 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003879
Ted Kremenek411cdee2008-04-16 21:10:48 +00003880 case Stmt::IndirectGotoStmtClass:
3881 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3882 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003883
Ted Kremenek411cdee2008-04-16 21:10:48 +00003884 case Stmt::SwitchStmtClass:
3885 E = cast<SwitchStmt>(Terminator)->getCond();
3886 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003887
John McCall56ca35d2011-02-17 10:25:35 +00003888 case Stmt::BinaryConditionalOperatorClass:
3889 E = cast<BinaryConditionalOperator>(Terminator)->getCond();
3890 break;
3891
Ted Kremenek411cdee2008-04-16 21:10:48 +00003892 case Stmt::ConditionalOperatorClass:
3893 E = cast<ConditionalOperator>(Terminator)->getCond();
3894 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003895
Ted Kremenek411cdee2008-04-16 21:10:48 +00003896 case Stmt::BinaryOperatorClass: // '&&' and '||'
3897 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00003898 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003899
Ted Kremenek390e48b2008-11-12 21:11:49 +00003900 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003901 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003902 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003903
Ted Kremenek411cdee2008-04-16 21:10:48 +00003904 return E ? E->IgnoreParens() : NULL;
3905}
3906
Ted Kremenek7dba8602007-08-29 21:56:09 +00003907//===----------------------------------------------------------------------===//
3908// CFG Graphviz Visualization
3909//===----------------------------------------------------------------------===//
3910
Ted Kremenek42a509f2007-08-31 21:30:12 +00003911
3912#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00003913static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003914#endif
3915
Chris Lattnere4f21422009-06-30 01:26:17 +00003916void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003917#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00003918 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003919 GraphHelper = &H;
3920 llvm::ViewGraph(this,"CFG");
3921 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003922#endif
3923}
3924
Ted Kremenek7dba8602007-08-29 21:56:09 +00003925namespace llvm {
3926template<>
3927struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00003928
3929 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3930
Ted Kremenek9c378f72011-08-12 23:37:29 +00003931 static std::string getNodeLabel(const CFGBlock *Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00003932
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003933#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00003934 std::string OutSStr;
3935 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek682060c2011-12-22 23:33:52 +00003936 print_block(Out,Graph, *Node, GraphHelper, false, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003937 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00003938
3939 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3940
3941 // Process string output to make it nicer...
3942 for (unsigned i = 0; i != OutStr.length(); ++i)
3943 if (OutStr[i] == '\n') { // Left justify
3944 OutStr[i] = '\\';
3945 OutStr.insert(OutStr.begin()+i+1, 'l');
3946 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003947
Ted Kremenek7dba8602007-08-29 21:56:09 +00003948 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003949#else
3950 return "";
3951#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00003952 }
3953};
3954} // end namespace llvm