blob: 97d494d7e9d75f1047690ff1d6c44b447fdaea66 [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.
NAKAMURA Takumi6955da22012-03-25 06:30:37 +0000291 typedef llvm::DenseMap<Expr *, TryResult> CachedBoolEvalsTy;
292 CachedBoolEvalsTy CachedBoolEvals;
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000293
Mike Stump6d9828c2009-07-17 01:31:16 +0000294public:
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +0000295 explicit CFGBuilder(ASTContext *astContext,
296 const CFG::BuildOptions &buildOpts)
297 : Context(astContext), cfg(new CFG()), // crew a new CFG
298 Block(NULL), Succ(NULL),
299 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
300 TryTerminatedBlock(NULL), badCFG(false), BuildOpts(buildOpts),
Ted Kremenek0d28d362011-03-10 03:50:34 +0000301 switchExclusivelyCovered(false), switchCond(0),
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000302 cachedEntry(0), lastLookup(0) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000303
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000304 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +0000305 CFG* buildCFG(const Decl *D, Stmt *Statement);
Mike Stump6d9828c2009-07-17 01:31:16 +0000306
Ted Kremenek0d28d362011-03-10 03:50:34 +0000307 bool alwaysAdd(const Stmt *stmt);
308
Ted Kremenek4f880632009-07-17 22:18:43 +0000309private:
310 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000311 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
312 CFGBlock *VisitBinaryOperator(BinaryOperator *B, 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);
Ted Kremenek55331da2012-04-12 20:03:44 +0000338 CFGBlock *VisitLambdaExpr(LambdaExpr *E, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000339 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek9c378f72011-08-12 23:37:29 +0000340 CFGBlock *VisitGotoStmt(GotoStmt *G);
Ted Kremenek4f880632009-07-17 22:18:43 +0000341 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000342 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000343 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
344 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000345 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000346 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
Ted Kremenek8e282c32012-03-06 23:40:47 +0000347 CFGBlock *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Ted Kremenek4f880632009-07-17 22:18:43 +0000348 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
349 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
350 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
351 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
Ted Kremenek9c378f72011-08-12 23:37:29 +0000352 CFGBlock *VisitReturnStmt(ReturnStmt *R);
John McCall4b9c2d22011-11-06 09:01:30 +0000353 CFGBlock *VisitPseudoObjectExpr(PseudoObjectExpr *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000354 CFGBlock *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E,
355 AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000356 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000357 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000358 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000359 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000360
Ted Kremenek852274d2009-12-16 03:18:58 +0000361 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
362 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek9c378f72011-08-12 23:37:29 +0000363 CFGBlock *VisitChildren(Stmt *S);
Ted Kremenek55331da2012-04-12 20:03:44 +0000364 CFGBlock *VisitNoRecurse(Expr *E, AddStmtChoice asc);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000365
Marcin Swiderski8599e762010-11-03 06:19:35 +0000366 // Visitors to walk an AST and generate destructors of temporaries in
367 // full expression.
368 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
369 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
370 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
371 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
372 bool BindToTemporary);
John McCall56ca35d2011-02-17 10:25:35 +0000373 CFGBlock *
374 VisitConditionalOperatorForTemporaryDtors(AbstractConditionalOperator *E,
375 bool BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000376
Ted Kremenek274f4332008-04-28 18:00:46 +0000377 // NYS == Not Yet Supported
Ted Kremenek9c378f72011-08-12 23:37:29 +0000378 CFGBlock *NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000379 badCFG = true;
380 return Block;
381 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000382
Ted Kremenek4f880632009-07-17 22:18:43 +0000383 void autoCreateBlock() { if (!Block) Block = createBlock(); }
384 CFGBlock *createBlock(bool add_successor = true);
Chandler Carruthdba3fb52011-09-13 09:13:49 +0000385 CFGBlock *createNoReturnBlock();
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000386
Zhongxing Xudf119892010-06-03 06:43:23 +0000387 CFGBlock *addStmt(Stmt *S) {
388 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000389 }
Sean Huntcbb67482011-01-08 20:30:50 +0000390 CFGBlock *addInitializer(CXXCtorInitializer *I);
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000391 void addAutomaticObjDtors(LocalScope::const_iterator B,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000392 LocalScope::const_iterator E, Stmt *S);
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000393 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekad5a8942010-08-02 23:46:59 +0000394
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000395 // Local scopes creation.
396 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
397
Ted Kremenek9c378f72011-08-12 23:37:29 +0000398 void addLocalScopeForStmt(Stmt *S);
399 LocalScope* addLocalScopeForDeclStmt(DeclStmt *DS, LocalScope* Scope = NULL);
400 LocalScope* addLocalScopeForVarDecl(VarDecl *VD, LocalScope* Scope = NULL);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000401
Ted Kremenek9c378f72011-08-12 23:37:29 +0000402 void addLocalScopeAndDtors(Stmt *S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000403
404 // Interface to CFGBlock - adding CFGElements.
Ted Kremenekd40066b2011-04-04 23:29:12 +0000405 void appendStmt(CFGBlock *B, const Stmt *S) {
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000406 if (alwaysAdd(S) && cachedEntry)
Ted Kremenek0d28d362011-03-10 03:50:34 +0000407 cachedEntry->second = B;
Ted Kremenek0d28d362011-03-10 03:50:34 +0000408
Jordy Roseac73ea82011-06-10 08:49:37 +0000409 // All block-level expressions should have already been IgnoreParens()ed.
410 assert(!isa<Expr>(S) || cast<Expr>(S)->IgnoreParens() == S);
Ted Kremenekd40066b2011-04-04 23:29:12 +0000411 B->appendStmt(const_cast<Stmt*>(S), cfg->getBumpVectorContext());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000412 }
Sean Huntcbb67482011-01-08 20:30:50 +0000413 void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) {
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000414 B->appendInitializer(I, cfg->getBumpVectorContext());
415 }
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000416 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
417 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
418 }
419 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
420 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
421 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000422 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
423 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
424 }
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000425 void appendAutomaticObjDtor(CFGBlock *B, VarDecl *VD, Stmt *S) {
426 B->appendAutomaticObjDtor(VD, S, cfg->getBumpVectorContext());
427 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000428
Ted Kremenek9c378f72011-08-12 23:37:29 +0000429 void prependAutomaticObjDtorsWithTerminator(CFGBlock *Blk,
Marcin Swiderski53de1342010-09-30 22:54:37 +0000430 LocalScope::const_iterator B, LocalScope::const_iterator E);
431
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000432 void addSuccessor(CFGBlock *B, CFGBlock *S) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000433 B->addSuccessor(S, cfg->getBumpVectorContext());
434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000436 /// Try and evaluate an expression to an integer constant.
437 bool tryEvaluate(Expr *S, Expr::EvalResult &outResult) {
438 if (!BuildOpts.PruneTriviallyFalseEdges)
439 return false;
440 return !S->isTypeDependent() &&
Ted Kremenekf8adeef2011-04-04 20:30:58 +0000441 !S->isValueDependent() &&
Richard Smith51f47082011-10-29 00:50:52 +0000442 S->EvaluateAsRValue(outResult, *Context);
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000443 }
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000445 /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
Mike Stump00998a02009-07-23 23:25:26 +0000446 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000447 TryResult tryEvaluateBool(Expr *S) {
Richard Smith85df96c2011-10-14 20:22:00 +0000448 if (!BuildOpts.PruneTriviallyFalseEdges ||
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000449 S->isTypeDependent() || S->isValueDependent())
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000450 return TryResult();
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000451
452 if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(S)) {
453 if (Bop->isLogicalOp()) {
454 // Check the cache first.
NAKAMURA Takumi6955da22012-03-25 06:30:37 +0000455 CachedBoolEvalsTy::iterator I = CachedBoolEvals.find(S);
456 if (I != CachedBoolEvals.end())
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000457 return I->second; // already in map;
NAKAMURA Takumi9260f612012-03-25 06:30:32 +0000458
459 // Retrieve result at first, or the map might be updated.
460 TryResult Result = evaluateAsBooleanConditionNoCache(S);
461 CachedBoolEvals[S] = Result; // update or insert
462 return Result;
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000463 }
464 }
465
466 return evaluateAsBooleanConditionNoCache(S);
467 }
468
469 /// \brief Evaluate as boolean \param E without using the cache.
470 TryResult evaluateAsBooleanConditionNoCache(Expr *E) {
471 if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(E)) {
472 if (Bop->isLogicalOp()) {
473 TryResult LHS = tryEvaluateBool(Bop->getLHS());
474 if (LHS.isKnown()) {
475 // We were able to evaluate the LHS, see if we can get away with not
476 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
477 if (LHS.isTrue() == (Bop->getOpcode() == BO_LOr))
478 return LHS.isTrue();
479
480 TryResult RHS = tryEvaluateBool(Bop->getRHS());
481 if (RHS.isKnown()) {
482 if (Bop->getOpcode() == BO_LOr)
483 return LHS.isTrue() || RHS.isTrue();
484 else
485 return LHS.isTrue() && RHS.isTrue();
486 }
487 } else {
488 TryResult RHS = tryEvaluateBool(Bop->getRHS());
489 if (RHS.isKnown()) {
490 // We can't evaluate the LHS; however, sometimes the result
491 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
492 if (RHS.isTrue() == (Bop->getOpcode() == BO_LOr))
493 return RHS.isTrue();
494 }
495 }
496
497 return TryResult();
498 }
499 }
500
501 bool Result;
502 if (E->EvaluateAsBooleanCondition(Result, *Context))
503 return Result;
504
505 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000506 }
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000507
Ted Kremenekfddd5182007-08-21 21:42:03 +0000508};
Mike Stump6d9828c2009-07-17 01:31:16 +0000509
Ted Kremenek0d28d362011-03-10 03:50:34 +0000510inline bool AddStmtChoice::alwaysAdd(CFGBuilder &builder,
511 const Stmt *stmt) const {
512 return builder.alwaysAdd(stmt) || kind == AlwaysAdd;
513}
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000514
Ted Kremenek0d28d362011-03-10 03:50:34 +0000515bool CFGBuilder::alwaysAdd(const Stmt *stmt) {
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000516 bool shouldAdd = BuildOpts.alwaysAdd(stmt);
517
Ted Kremenek0d28d362011-03-10 03:50:34 +0000518 if (!BuildOpts.forcedBlkExprs)
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000519 return shouldAdd;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000520
521 if (lastLookup == stmt) {
522 if (cachedEntry) {
523 assert(cachedEntry->first == stmt);
524 return true;
525 }
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000526 return shouldAdd;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000527 }
Ted Kremenek0d28d362011-03-10 03:50:34 +0000528
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000529 lastLookup = stmt;
530
531 // Perform the lookup!
Ted Kremenek0d28d362011-03-10 03:50:34 +0000532 CFG::BuildOptions::ForcedBlkExprs *fb = *BuildOpts.forcedBlkExprs;
533
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000534 if (!fb) {
535 // No need to update 'cachedEntry', since it will always be null.
536 assert(cachedEntry == 0);
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000537 return shouldAdd;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000538 }
Ted Kremenek0d28d362011-03-10 03:50:34 +0000539
540 CFG::BuildOptions::ForcedBlkExprs::iterator itr = fb->find(stmt);
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000541 if (itr == fb->end()) {
542 cachedEntry = 0;
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000543 return shouldAdd;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000544 }
545
Ted Kremenek0d28d362011-03-10 03:50:34 +0000546 cachedEntry = &*itr;
547 return true;
Ted Kremenek3179a452011-03-10 01:14:11 +0000548}
549
Douglas Gregor898574e2008-12-05 23:32:09 +0000550// FIXME: Add support for dependent-sized array types in C++?
551// Does it even make sense to build a CFG for an uninstantiated template?
John McCallf4c73712011-01-19 06:33:43 +0000552static const VariableArrayType *FindVA(const Type *t) {
553 while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
554 if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
Ted Kremenek610a09e2008-09-26 22:58:57 +0000555 if (vat->getSizeExpr())
556 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000557
Ted Kremenek610a09e2008-09-26 22:58:57 +0000558 t = vt->getElementType().getTypePtr();
559 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000560
Ted Kremenek610a09e2008-09-26 22:58:57 +0000561 return 0;
562}
Mike Stump6d9828c2009-07-17 01:31:16 +0000563
564/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
565/// arbitrary statement. Examples include a single expression or a function
566/// body (compound statement). The ownership of the returned CFG is
567/// transferred to the caller. If CFG construction fails, this method returns
568/// NULL.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000569CFG* CFGBuilder::buildCFG(const Decl *D, Stmt *Statement) {
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000570 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000571 if (!Statement)
572 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000573
Mike Stump6d9828c2009-07-17 01:31:16 +0000574 // Create an empty block that will serve as the exit block for the CFG. Since
575 // this is the first block added to the CFG, it will be implicitly registered
576 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000577 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000578 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000579 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000580
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000581 if (BuildOpts.AddImplicitDtors)
582 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
583 addImplicitDtorsForDestructor(DD);
584
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000585 // Visit the statements and create the CFG.
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000586 CFGBlock *B = addStmt(Statement);
587
588 if (badCFG)
589 return NULL;
590
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000591 // For C++ constructor add initializers to CFG.
592 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
593 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
594 E = CD->init_rend(); I != E; ++I) {
595 B = addInitializer(*I);
596 if (badCFG)
597 return NULL;
598 }
599 }
600
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000601 if (B)
602 Succ = B;
Mike Stumpb978a442010-01-21 02:21:40 +0000603
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000604 // Backpatch the gotos whose label -> block mappings we didn't know when we
605 // encountered them.
606 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
607 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000608
Ted Kremenek9c378f72011-08-12 23:37:29 +0000609 CFGBlock *B = I->block;
610 GotoStmt *G = cast<GotoStmt>(B->getTerminator());
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000611 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000612
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000613 // If there is no target for the goto, then we are looking at an
614 // incomplete AST. Handle this by not registering a successor.
615 if (LI == LabelMap.end()) continue;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000616
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000617 JumpTarget JT = LI->second;
Ted Kremenek9ce52702011-01-07 19:37:16 +0000618 prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
619 JT.scopePosition);
620 addSuccessor(B, JT.block);
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000621 }
622
623 // Add successors to the Indirect Goto Dispatch block (if we have one).
Ted Kremenek9c378f72011-08-12 23:37:29 +0000624 if (CFGBlock *B = cfg->getIndirectGotoBlock())
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000625 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
626 E = AddressTakenLabels.end(); I != E; ++I ) {
627
628 // Lookup the target block.
629 LabelMapTy::iterator LI = LabelMap.find(*I);
630
631 // If there is no target block that contains label, then we are looking
632 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000633 if (LI == LabelMap.end()) continue;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000634
Ted Kremenek9ce52702011-01-07 19:37:16 +0000635 addSuccessor(B, LI->second.block);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000636 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000637
Mike Stump6d9828c2009-07-17 01:31:16 +0000638 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000639 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000640
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000641 return cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000642}
Mike Stump6d9828c2009-07-17 01:31:16 +0000643
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000644/// createBlock - Used to lazily create blocks that are connected
645/// to the current (global) succcessor.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000646CFGBlock *CFGBuilder::createBlock(bool add_successor) {
647 CFGBlock *B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000648 if (add_successor && Succ)
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000649 addSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000650 return B;
651}
Mike Stump6d9828c2009-07-17 01:31:16 +0000652
Chandler Carruthdba3fb52011-09-13 09:13:49 +0000653/// createNoReturnBlock - Used to create a block is a 'noreturn' point in the
654/// CFG. It is *not* connected to the current (global) successor, and instead
655/// directly tied to the exit block in order to be reachable.
656CFGBlock *CFGBuilder::createNoReturnBlock() {
657 CFGBlock *B = createBlock(false);
Chandler Carruth83754162011-09-13 09:53:55 +0000658 B->setHasNoReturnElement();
Chandler Carruthdba3fb52011-09-13 09:13:49 +0000659 addSuccessor(B, &cfg->getExit());
660 return B;
661}
662
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000663/// addInitializer - Add C++ base or member initializer element to CFG.
Sean Huntcbb67482011-01-08 20:30:50 +0000664CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) {
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000665 if (!BuildOpts.AddInitializers)
666 return Block;
667
Marcin Swiderski8599e762010-11-03 06:19:35 +0000668 bool IsReference = false;
669 bool HasTemporaries = false;
670
671 // Destructors of temporaries in initialization expression should be called
672 // after initialization finishes.
673 Expr *Init = I->getInit();
674 if (Init) {
Francois Pichet00eb3f92010-12-04 09:14:42 +0000675 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski8599e762010-11-03 06:19:35 +0000676 IsReference = FD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +0000677 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000678
679 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
680 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +0000681 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +0000682 IsReference);
683 }
684 }
685
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000686 autoCreateBlock();
687 appendInitializer(Block, I);
688
Marcin Swiderski8599e762010-11-03 06:19:35 +0000689 if (Init) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000690 if (HasTemporaries) {
Marcin Swiderski8599e762010-11-03 06:19:35 +0000691 // For expression with temporaries go directly to subexpression to omit
692 // generating destructors for the second time.
Ted Kremenek892697d2010-12-16 07:46:53 +0000693 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
694 }
695 return Visit(Init);
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000696 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000697
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000698 return Block;
699}
700
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000701/// \brief Retrieve the type of the temporary object whose lifetime was
702/// extended by a local reference with the given initializer.
703static QualType getReferenceInitTemporaryType(ASTContext &Context,
704 const Expr *Init) {
705 while (true) {
706 // Skip parentheses.
707 Init = Init->IgnoreParens();
708
709 // Skip through cleanups.
710 if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Init)) {
711 Init = EWC->getSubExpr();
712 continue;
713 }
714
715 // Skip through the temporary-materialization expression.
716 if (const MaterializeTemporaryExpr *MTE
717 = dyn_cast<MaterializeTemporaryExpr>(Init)) {
718 Init = MTE->GetTemporaryExpr();
719 continue;
720 }
721
722 // Skip derived-to-base and no-op casts.
723 if (const CastExpr *CE = dyn_cast<CastExpr>(Init)) {
724 if ((CE->getCastKind() == CK_DerivedToBase ||
725 CE->getCastKind() == CK_UncheckedDerivedToBase ||
726 CE->getCastKind() == CK_NoOp) &&
727 Init->getType()->isRecordType()) {
728 Init = CE->getSubExpr();
729 continue;
730 }
731 }
732
733 // Skip member accesses into rvalues.
734 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Init)) {
735 if (!ME->isArrow() && ME->getBase()->isRValue()) {
736 Init = ME->getBase();
737 continue;
738 }
739 }
740
741 break;
742 }
743
744 return Init->getType();
745}
746
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000747/// addAutomaticObjDtors - Add to current block automatic objects destructors
748/// for objects in range of local scope positions. Use S as trigger statement
749/// for destructors.
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000750void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000751 LocalScope::const_iterator E, Stmt *S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000752 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000753 return;
754
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000755 if (B == E)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000756 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000757
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000758 // We need to append the destructors in reverse order, but any one of them
759 // may be a no-return destructor which changes the CFG. As a result, buffer
760 // this sequence up and replay them in reverse order when appending onto the
761 // CFGBlock(s).
762 SmallVector<VarDecl*, 10> Decls;
763 Decls.reserve(B.distance(E));
764 for (LocalScope::const_iterator I = B; I != E; ++I)
765 Decls.push_back(*I);
766
767 for (SmallVectorImpl<VarDecl*>::reverse_iterator I = Decls.rbegin(),
768 E = Decls.rend();
769 I != E; ++I) {
770 // If this destructor is marked as a no-return destructor, we need to
771 // create a new block for the destructor which does not have as a successor
772 // anything built thus far: control won't flow out of this block.
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000773 QualType Ty;
774 if ((*I)->getType()->isReferenceType()) {
775 Ty = getReferenceInitTemporaryType(*Context, (*I)->getInit());
776 } else {
777 Ty = Context->getBaseElementType((*I)->getType());
778 }
779
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000780 const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor();
David Blaikie23661d32012-01-24 04:51:48 +0000781 if (cast<FunctionType>(Dtor->getType())->getNoReturnAttr())
Chandler Carruthdba3fb52011-09-13 09:13:49 +0000782 Block = createNoReturnBlock();
783 else
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000784 autoCreateBlock();
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000785
786 appendAutomaticObjDtor(Block, *I, S);
787 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000788}
789
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000790/// addImplicitDtorsForDestructor - Add implicit destructors generated for
791/// base and member objects in destructor.
792void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
793 assert (BuildOpts.AddImplicitDtors
794 && "Can be called only when dtors should be added");
795 const CXXRecordDecl *RD = DD->getParent();
796
797 // At the end destroy virtual base objects.
798 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
799 VE = RD->vbases_end(); VI != VE; ++VI) {
800 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
801 if (!CD->hasTrivialDestructor()) {
802 autoCreateBlock();
803 appendBaseDtor(Block, VI);
804 }
805 }
806
807 // Before virtual bases destroy direct base objects.
808 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
809 BE = RD->bases_end(); BI != BE; ++BI) {
David Blaikie23661d32012-01-24 04:51:48 +0000810 if (!BI->isVirtual()) {
811 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
812 if (!CD->hasTrivialDestructor()) {
813 autoCreateBlock();
814 appendBaseDtor(Block, BI);
815 }
816 }
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000817 }
818
819 // First destroy member objects.
820 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
821 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +0000822 // Check for constant size array. Set type to array element type.
823 QualType QT = FI->getType();
824 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
825 if (AT->getSize() == 0)
826 continue;
827 QT = AT->getElementType();
828 }
829
830 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000831 if (!CD->hasTrivialDestructor()) {
832 autoCreateBlock();
833 appendMemberDtor(Block, *FI);
834 }
835 }
836}
837
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000838/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
839/// way return valid LocalScope object.
840LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
841 if (!Scope) {
Ted Kremenekfe59b742011-02-15 02:47:45 +0000842 llvm::BumpPtrAllocator &alloc = cfg->getAllocator();
843 Scope = alloc.Allocate<LocalScope>();
844 BumpVectorContext ctx(alloc);
845 new (Scope) LocalScope(ctx, ScopePos);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000846 }
847 return Scope;
848}
849
850/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000851/// that should create implicit scope (e.g. if/else substatements).
Ted Kremenek9c378f72011-08-12 23:37:29 +0000852void CFGBuilder::addLocalScopeForStmt(Stmt *S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000853 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000854 return;
855
856 LocalScope *Scope = 0;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000857
858 // For compound statement we will be creating explicit scope.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000859 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000860 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
861 ; BI != BE; ++BI) {
Chandler Carrutha1364be2011-09-10 00:02:34 +0000862 Stmt *SI = (*BI)->stripLabelLikeStatements();
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000863 if (DeclStmt *DS = dyn_cast<DeclStmt>(SI))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000864 Scope = addLocalScopeForDeclStmt(DS, Scope);
865 }
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000866 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000867 }
868
869 // For any other statement scope will be implicit and as such will be
870 // interesting only for DeclStmt.
Chandler Carrutha1364be2011-09-10 00:02:34 +0000871 if (DeclStmt *DS = dyn_cast<DeclStmt>(S->stripLabelLikeStatements()))
Zhongxing Xub6edff52010-10-01 03:09:09 +0000872 addLocalScopeForDeclStmt(DS);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000873}
874
875/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
876/// reuse Scope if not NULL.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000877LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt *DS,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000878 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000879 if (!BuildOpts.AddImplicitDtors)
880 return Scope;
881
882 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
883 ; DI != DE; ++DI) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000884 if (VarDecl *VD = dyn_cast<VarDecl>(*DI))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000885 Scope = addLocalScopeForVarDecl(VD, Scope);
886 }
887 return Scope;
888}
889
890/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
891/// create add scope for automatic objects and temporary objects bound to
892/// const reference. Will reuse Scope if not NULL.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000893LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl *VD,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000894 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000895 if (!BuildOpts.AddImplicitDtors)
896 return Scope;
897
898 // Check if variable is local.
899 switch (VD->getStorageClass()) {
900 case SC_None:
901 case SC_Auto:
902 case SC_Register:
903 break;
904 default: return Scope;
905 }
906
907 // Check for const references bound to temporary. Set type to pointee.
908 QualType QT = VD->getType();
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000909 if (QT.getTypePtr()->isReferenceType()) {
Douglas Gregor03e80032011-06-21 17:03:29 +0000910 if (!VD->extendsLifetimeOfTemporary())
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000911 return Scope;
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000912
913 QT = getReferenceInitTemporaryType(*Context, VD->getInit());
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000914 }
915
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000916 // Check for constant size array. Set type to array element type.
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000917 while (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000918 if (AT->getSize() == 0)
919 return Scope;
920 QT = AT->getElementType();
921 }
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000922
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000923 // Check if type is a C++ class with non-trivial destructor.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000924 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
David Blaikie23661d32012-01-24 04:51:48 +0000925 if (!CD->hasTrivialDestructor()) {
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000926 // Add the variable to scope
927 Scope = createOrReuseLocalScope(Scope);
928 Scope->addVar(VD);
929 ScopePos = Scope->begin();
930 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000931 return Scope;
932}
933
934/// addLocalScopeAndDtors - For given statement add local scope for it and
935/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000936void CFGBuilder::addLocalScopeAndDtors(Stmt *S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000937 if (!BuildOpts.AddImplicitDtors)
938 return;
939
940 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000941 addLocalScopeForStmt(S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000942 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
943}
944
Marcin Swiderski53de1342010-09-30 22:54:37 +0000945/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
946/// variables with automatic storage duration to CFGBlock's elements vector.
947/// Elements will be prepended to physical beginning of the vector which
948/// happens to be logical end. Use blocks terminator as statement that specifies
949/// destructors call site.
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000950/// FIXME: This mechanism for adding automatic destructors doesn't handle
951/// no-return destructors properly.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000952void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock *Blk,
Marcin Swiderski53de1342010-09-30 22:54:37 +0000953 LocalScope::const_iterator B, LocalScope::const_iterator E) {
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000954 BumpVectorContext &C = cfg->getBumpVectorContext();
955 CFGBlock::iterator InsertPos
956 = Blk->beginAutomaticObjDtorsInsert(Blk->end(), B.distance(E), C);
957 for (LocalScope::const_iterator I = B; I != E; ++I)
958 InsertPos = Blk->insertAutomaticObjDtor(InsertPos, *I,
959 Blk->getTerminator());
Marcin Swiderski53de1342010-09-30 22:54:37 +0000960}
961
Ted Kremenek4f880632009-07-17 22:18:43 +0000962/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000963/// blocks for ternary operators, &&, and ||. We also process "," and
964/// DeclStmts (which may contain nested control-flow).
Ted Kremenek9c378f72011-08-12 23:37:29 +0000965CFGBlock *CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenekf42e3372010-04-30 22:25:53 +0000966 if (!S) {
967 badCFG = true;
968 return 0;
969 }
Jordy Roseac73ea82011-06-10 08:49:37 +0000970
971 if (Expr *E = dyn_cast<Expr>(S))
972 S = E->IgnoreParens();
973
Ted Kremenek4f880632009-07-17 22:18:43 +0000974 switch (S->getStmtClass()) {
975 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000976 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000977
978 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000979 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000980
John McCall56ca35d2011-02-17 10:25:35 +0000981 case Stmt::BinaryConditionalOperatorClass:
982 return VisitConditionalOperator(cast<BinaryConditionalOperator>(S), asc);
983
Ted Kremenek4f880632009-07-17 22:18:43 +0000984 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000985 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Ted Kremenek4f880632009-07-17 22:18:43 +0000987 case Stmt::BlockExprClass:
Ted Kremenek55331da2012-04-12 20:03:44 +0000988 case Stmt::LambdaExprClass:
989 return VisitNoRecurse(cast<Expr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000990
Ted Kremenek4f880632009-07-17 22:18:43 +0000991 case Stmt::BreakStmtClass:
992 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Ted Kremenek4f880632009-07-17 22:18:43 +0000994 case Stmt::CallExprClass:
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000995 case Stmt::CXXOperatorCallExprClass:
John McCall1de85332011-05-11 07:19:11 +0000996 case Stmt::CXXMemberCallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +0000997 case Stmt::UserDefinedLiteralClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000998 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Ted Kremenek4f880632009-07-17 22:18:43 +00001000 case Stmt::CaseStmtClass:
1001 return VisitCaseStmt(cast<CaseStmt>(S));
1002
1003 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +00001004 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Ted Kremenek4f880632009-07-17 22:18:43 +00001006 case Stmt::CompoundStmtClass:
1007 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Ted Kremenek4f880632009-07-17 22:18:43 +00001009 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +00001010 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Ted Kremenek4f880632009-07-17 22:18:43 +00001012 case Stmt::ContinueStmtClass:
1013 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Ted Kremenek021c8af2010-01-19 20:40:33 +00001015 case Stmt::CXXCatchStmtClass:
1016 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
1017
John McCall4765fa02010-12-06 08:20:24 +00001018 case Stmt::ExprWithCleanupsClass:
1019 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +00001020
Zhongxing Xua725ed42010-11-01 13:04:58 +00001021 case Stmt::CXXBindTemporaryExprClass:
1022 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
1023
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00001024 case Stmt::CXXConstructExprClass:
1025 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
1026
Zhongxing Xua725ed42010-11-01 13:04:58 +00001027 case Stmt::CXXFunctionalCastExprClass:
1028 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
1029
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00001030 case Stmt::CXXTemporaryObjectExprClass:
1031 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
1032
Ted Kremenek021c8af2010-01-19 20:40:33 +00001033 case Stmt::CXXThrowExprClass:
1034 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +00001035
Ted Kremenek021c8af2010-01-19 20:40:33 +00001036 case Stmt::CXXTryStmtClass:
1037 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +00001038
Richard Smithad762fc2011-04-14 22:09:26 +00001039 case Stmt::CXXForRangeStmtClass:
1040 return VisitCXXForRangeStmt(cast<CXXForRangeStmt>(S));
1041
Ted Kremenek4f880632009-07-17 22:18:43 +00001042 case Stmt::DeclStmtClass:
1043 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Ted Kremenek4f880632009-07-17 22:18:43 +00001045 case Stmt::DefaultStmtClass:
1046 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Ted Kremenek4f880632009-07-17 22:18:43 +00001048 case Stmt::DoStmtClass:
1049 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Ted Kremenek4f880632009-07-17 22:18:43 +00001051 case Stmt::ForStmtClass:
1052 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Ted Kremenek4f880632009-07-17 22:18:43 +00001054 case Stmt::GotoStmtClass:
1055 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Ted Kremenek4f880632009-07-17 22:18:43 +00001057 case Stmt::IfStmtClass:
1058 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Ted Kremenek892697d2010-12-16 07:46:53 +00001060 case Stmt::ImplicitCastExprClass:
1061 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00001062
Ted Kremenek4f880632009-07-17 22:18:43 +00001063 case Stmt::IndirectGotoStmtClass:
1064 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Ted Kremenek4f880632009-07-17 22:18:43 +00001066 case Stmt::LabelStmtClass:
1067 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Ted Kremenek115c1b92010-04-11 17:02:10 +00001069 case Stmt::MemberExprClass:
1070 return VisitMemberExpr(cast<MemberExpr>(S), asc);
1071
Ted Kremenek6a9065a2011-11-05 00:10:15 +00001072 case Stmt::NullStmtClass:
1073 return Block;
1074
Ted Kremenek4f880632009-07-17 22:18:43 +00001075 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +00001076 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
1077
Ted Kremenek8e282c32012-03-06 23:40:47 +00001078 case Stmt::ObjCAutoreleasePoolStmtClass:
1079 return VisitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(S));
1080
Ted Kremenek4f880632009-07-17 22:18:43 +00001081 case Stmt::ObjCAtSynchronizedStmtClass:
1082 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Ted Kremenek4f880632009-07-17 22:18:43 +00001084 case Stmt::ObjCAtThrowStmtClass:
1085 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Ted Kremenek4f880632009-07-17 22:18:43 +00001087 case Stmt::ObjCAtTryStmtClass:
1088 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Ted Kremenek4f880632009-07-17 22:18:43 +00001090 case Stmt::ObjCForCollectionStmtClass:
1091 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Ted Kremenek6a9065a2011-11-05 00:10:15 +00001093 case Stmt::OpaqueValueExprClass:
Ted Kremenek4f880632009-07-17 22:18:43 +00001094 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +00001095
John McCall4b9c2d22011-11-06 09:01:30 +00001096 case Stmt::PseudoObjectExprClass:
1097 return VisitPseudoObjectExpr(cast<PseudoObjectExpr>(S));
1098
Ted Kremenek4f880632009-07-17 22:18:43 +00001099 case Stmt::ReturnStmtClass:
1100 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001102 case Stmt::UnaryExprOrTypeTraitExprClass:
1103 return VisitUnaryExprOrTypeTraitExpr(cast<UnaryExprOrTypeTraitExpr>(S),
1104 asc);
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Ted Kremenek4f880632009-07-17 22:18:43 +00001106 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +00001107 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Ted Kremenek4f880632009-07-17 22:18:43 +00001109 case Stmt::SwitchStmtClass:
1110 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001112 case Stmt::UnaryOperatorClass:
1113 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
1114
Ted Kremenek4f880632009-07-17 22:18:43 +00001115 case Stmt::WhileStmtClass:
1116 return VisitWhileStmt(cast<WhileStmt>(S));
1117 }
1118}
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Ted Kremenek852274d2009-12-16 03:18:58 +00001120CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001121 if (asc.alwaysAdd(*this, S)) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001122 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001123 appendStmt(Block, S);
Mike Stump6d9828c2009-07-17 01:31:16 +00001124 }
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Ted Kremenek4f880632009-07-17 22:18:43 +00001126 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001127}
Mike Stump6d9828c2009-07-17 01:31:16 +00001128
Ted Kremenek4f880632009-07-17 22:18:43 +00001129/// VisitChildren - Visit the children of a Stmt.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001130CFGBlock *CFGBuilder::VisitChildren(Stmt *Terminator) {
Ted Kremenek6b12da92011-02-21 22:11:26 +00001131 CFGBlock *lastBlock = Block;
1132 for (Stmt::child_range I = Terminator->children(); I; ++I)
1133 if (Stmt *child = *I)
1134 if (CFGBlock *b = Visit(child))
1135 lastBlock = b;
1136
1137 return lastBlock;
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001138}
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Ted Kremenek852274d2009-12-16 03:18:58 +00001140CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
1141 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001142 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001143
Ted Kremenek3179a452011-03-10 01:14:11 +00001144 if (asc.alwaysAdd(*this, A)) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001145 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001146 appendStmt(Block, A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001147 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001148
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001149 return Block;
1150}
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001152CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek892697d2010-12-16 07:46:53 +00001153 AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001154 if (asc.alwaysAdd(*this, U)) {
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001155 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001156 appendStmt(Block, U);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001157 }
1158
Ted Kremenek892697d2010-12-16 07:46:53 +00001159 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001160}
1161
Ted Kremenek852274d2009-12-16 03:18:58 +00001162CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
1163 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001164 if (B->isLogicalOp()) { // && or ||
Ted Kremenek9c378f72011-08-12 23:37:29 +00001165 CFGBlock *ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001166 appendStmt(ConfluenceBlock, B);
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001168 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001169 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Ted Kremenek4f880632009-07-17 22:18:43 +00001171 // create the block evaluating the LHS
Ted Kremenek9c378f72011-08-12 23:37:29 +00001172 CFGBlock *LHSBlock = createBlock(false);
Ted Kremenek4f880632009-07-17 22:18:43 +00001173 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Ted Kremenek4f880632009-07-17 22:18:43 +00001175 // create the block evaluating the RHS
1176 Succ = ConfluenceBlock;
1177 Block = NULL;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001178 CFGBlock *RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +00001179
1180 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001181 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +00001182 return 0;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001183 } else {
Ted Kremenek862b24f2010-04-29 01:10:26 +00001184 // Create an empty block for cases where the RHS doesn't require
1185 // any explicit statements in the CFG.
1186 RHSBlock = createBlock();
1187 }
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +00001189 // Generate the blocks for evaluating the LHS.
1190 Block = LHSBlock;
1191 CFGBlock *EntryLHSBlock = addStmt(B->getLHS());
1192
Mike Stump00998a02009-07-23 23:25:26 +00001193 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001194 TryResult KnownVal = tryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +00001195 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +00001196 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +00001197
Ted Kremenek4f880632009-07-17 22:18:43 +00001198 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +00001199 if (B->getOpcode() == BO_LOr) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001200 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
1201 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001202 } else {
John McCall2de56d12010-08-25 11:45:40 +00001203 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001204 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
1205 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001206 }
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +00001208 return EntryLHSBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001209 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001210
1211 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +00001212 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001213 appendStmt(Block, B);
Ted Kremenek4f880632009-07-17 22:18:43 +00001214 addStmt(B->getRHS());
1215 return addStmt(B->getLHS());
1216 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001217
1218 if (B->isAssignmentOp()) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001219 if (asc.alwaysAdd(*this, B)) {
Zhongxing Xufc61d942010-06-03 06:23:18 +00001220 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001221 appendStmt(Block, B);
Zhongxing Xufc61d942010-06-03 06:23:18 +00001222 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001223 Visit(B->getLHS());
Marcin Swiderskie1667192010-10-24 08:21:40 +00001224 return Visit(B->getRHS());
Zhongxing Xufc61d942010-06-03 06:23:18 +00001225 }
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Ted Kremenek3179a452011-03-10 01:14:11 +00001227 if (asc.alwaysAdd(*this, B)) {
Marcin Swiderskie1667192010-10-24 08:21:40 +00001228 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001229 appendStmt(Block, B);
Marcin Swiderskie1667192010-10-24 08:21:40 +00001230 }
1231
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001232 CFGBlock *RBlock = Visit(B->getRHS());
1233 CFGBlock *LBlock = Visit(B->getLHS());
1234 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1235 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1236 // return RBlock. Otherwise we'll incorrectly return NULL.
1237 return (LBlock ? LBlock : RBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001238}
1239
Ted Kremenek55331da2012-04-12 20:03:44 +00001240CFGBlock *CFGBuilder::VisitNoRecurse(Expr *E, AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001241 if (asc.alwaysAdd(*this, E)) {
Ted Kremenek721903e2009-11-25 01:34:30 +00001242 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001243 appendStmt(Block, E);
Ted Kremenek721903e2009-11-25 01:34:30 +00001244 }
1245 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001246}
1247
Ted Kremenek4f880632009-07-17 22:18:43 +00001248CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1249 // "break" is a control-flow statement. Thus we stop processing the current
1250 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001251 if (badCFG)
1252 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Ted Kremenek4f880632009-07-17 22:18:43 +00001254 // Now create a new block that ends with the break statement.
1255 Block = createBlock(false);
1256 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Ted Kremenek4f880632009-07-17 22:18:43 +00001258 // If there is no target for the break, then we are looking at an incomplete
1259 // AST. This means that the CFG cannot be constructed.
Ted Kremenek9ce52702011-01-07 19:37:16 +00001260 if (BreakJumpTarget.block) {
1261 addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B);
1262 addSuccessor(Block, BreakJumpTarget.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001263 } else
Ted Kremenek4f880632009-07-17 22:18:43 +00001264 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001265
1266
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001267 return Block;
1268}
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001270static bool CanThrow(Expr *E, ASTContext &Ctx) {
Mike Stump4c45aa12010-01-21 15:20:48 +00001271 QualType Ty = E->getType();
1272 if (Ty->isFunctionPointerType())
1273 Ty = Ty->getAs<PointerType>()->getPointeeType();
1274 else if (Ty->isBlockPointerType())
1275 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +00001276
Mike Stump4c45aa12010-01-21 15:20:48 +00001277 const FunctionType *FT = Ty->getAs<FunctionType>();
1278 if (FT) {
1279 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001280 if (Proto->isNothrow(Ctx))
Mike Stump4c45aa12010-01-21 15:20:48 +00001281 return false;
1282 }
1283 return true;
1284}
1285
Ted Kremenek852274d2009-12-16 03:18:58 +00001286CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
John McCall1de85332011-05-11 07:19:11 +00001287 // Compute the callee type.
1288 QualType calleeType = C->getCallee()->getType();
1289 if (calleeType == Context->BoundMemberTy) {
1290 QualType boundType = Expr::findBoundMemberType(C->getCallee());
1291
1292 // We should only get a null bound type if processing a dependent
1293 // CFG. Recover by assuming nothing.
1294 if (!boundType.isNull()) calleeType = boundType;
Ted Kremenek4f880632009-07-17 22:18:43 +00001295 }
Mike Stump24556362009-07-25 21:26:53 +00001296
John McCall1de85332011-05-11 07:19:11 +00001297 // If this is a call to a no-return function, this stops the block here.
1298 bool NoReturn = getFunctionExtInfo(*calleeType).getNoReturn();
1299
Mike Stump4c45aa12010-01-21 15:20:48 +00001300 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001301
1302 // Languages without exceptions are assumed to not throw.
David Blaikie4e4d0842012-03-11 07:00:24 +00001303 if (Context->getLangOpts().Exceptions) {
Ted Kremenek6c52c782010-09-14 23:41:16 +00001304 if (BuildOpts.AddEHEdges)
Mike Stump4c45aa12010-01-21 15:20:48 +00001305 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +00001306 }
1307
1308 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +00001309 if (FD->hasAttr<NoReturnAttr>())
1310 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +00001311 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +00001312 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001313 }
Mike Stump24556362009-07-25 21:26:53 +00001314
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001315 if (!CanThrow(C->getCallee(), *Context))
Mike Stump4c45aa12010-01-21 15:20:48 +00001316 AddEHEdge = false;
1317
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001318 if (!NoReturn && !AddEHEdge)
1319 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Mike Stump079bd722010-01-19 22:00:14 +00001321 if (Block) {
1322 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001323 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +00001324 return 0;
1325 }
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Chandler Carruthdba3fb52011-09-13 09:13:49 +00001327 if (NoReturn)
1328 Block = createNoReturnBlock();
1329 else
1330 Block = createBlock();
1331
Ted Kremenek247e9662011-03-10 01:14:08 +00001332 appendStmt(Block, C);
Mike Stump24556362009-07-25 21:26:53 +00001333
Mike Stump4c45aa12010-01-21 15:20:48 +00001334 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +00001335 // Add exceptional edges.
1336 if (TryTerminatedBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001337 addSuccessor(Block, TryTerminatedBlock);
Mike Stump079bd722010-01-19 22:00:14 +00001338 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001339 addSuccessor(Block, &cfg->getExit());
Mike Stump079bd722010-01-19 22:00:14 +00001340 }
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Mike Stump24556362009-07-25 21:26:53 +00001342 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +00001343}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001344
Ted Kremenek852274d2009-12-16 03:18:58 +00001345CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1346 AddStmtChoice asc) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001347 CFGBlock *ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001348 appendStmt(ConfluenceBlock, C);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001349 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001350 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001352 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001353 Succ = ConfluenceBlock;
1354 Block = NULL;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001355 CFGBlock *LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001356 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001357 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001359 Succ = ConfluenceBlock;
1360 Block = NULL;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001361 CFGBlock *RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001362 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001363 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001365 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +00001366 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001367 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
1368 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1369 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001370 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001371 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001372}
Mike Stump1eb44332009-09-09 15:08:12 +00001373
1374
Ted Kremenek9c378f72011-08-12 23:37:29 +00001375CFGBlock *CFGBuilder::VisitCompoundStmt(CompoundStmt *C) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001376 addLocalScopeAndDtors(C);
Ted Kremenek9c378f72011-08-12 23:37:29 +00001377 CFGBlock *LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001378
1379 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1380 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +00001381 // If we hit a segment of code just containing ';' (NullStmts), we can
1382 // get a null block back. In such cases, just use the LastBlock
1383 if (CFGBlock *newBlock = addStmt(*I))
1384 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +00001386 if (badCFG)
1387 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001388 }
Mike Stump079bd722010-01-19 22:00:14 +00001389
Ted Kremenek4f880632009-07-17 22:18:43 +00001390 return LastBlock;
1391}
Mike Stump1eb44332009-09-09 15:08:12 +00001392
John McCall56ca35d2011-02-17 10:25:35 +00001393CFGBlock *CFGBuilder::VisitConditionalOperator(AbstractConditionalOperator *C,
Ted Kremenek852274d2009-12-16 03:18:58 +00001394 AddStmtChoice asc) {
John McCall56ca35d2011-02-17 10:25:35 +00001395 const BinaryConditionalOperator *BCO = dyn_cast<BinaryConditionalOperator>(C);
1396 const OpaqueValueExpr *opaqueValue = (BCO ? BCO->getOpaqueValue() : NULL);
1397
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001398 // Create the confluence block that will "merge" the results of the ternary
1399 // expression.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001400 CFGBlock *ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001401 appendStmt(ConfluenceBlock, C);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001402 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001403 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001405 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001406
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001407 // Create a block for the LHS expression if there is an LHS expression. A
1408 // GCC extension allows LHS to be NULL, causing the condition to be the
1409 // value that is returned instead.
1410 // e.g: x ?: y is shorthand for: x ? x : y;
1411 Succ = ConfluenceBlock;
1412 Block = NULL;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001413 CFGBlock *LHSBlock = 0;
John McCall56ca35d2011-02-17 10:25:35 +00001414 const Expr *trueExpr = C->getTrueExpr();
1415 if (trueExpr != opaqueValue) {
1416 LHSBlock = Visit(C->getTrueExpr(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001417 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001418 return 0;
1419 Block = NULL;
1420 }
Ted Kremenekf226d182011-02-24 03:09:15 +00001421 else
1422 LHSBlock = ConfluenceBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001424 // Create the block for the RHS expression.
1425 Succ = ConfluenceBlock;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001426 CFGBlock *RHSBlock = Visit(C->getFalseExpr(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001427 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001428 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001430 // Create the block that will contain the condition.
1431 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001432
Mike Stump00998a02009-07-23 23:25:26 +00001433 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001434 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
Ted Kremenekf226d182011-02-24 03:09:15 +00001435 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001436 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001437 Block->setTerminator(C);
John McCall56ca35d2011-02-17 10:25:35 +00001438 Expr *condExpr = C->getCond();
John McCalld40baf62011-02-19 03:13:26 +00001439
Ted Kremenekf226d182011-02-24 03:09:15 +00001440 if (opaqueValue) {
1441 // Run the condition expression if it's not trivially expressed in
1442 // terms of the opaque value (or if there is no opaque value).
1443 if (condExpr != opaqueValue)
1444 addStmt(condExpr);
John McCalld40baf62011-02-19 03:13:26 +00001445
Ted Kremenekf226d182011-02-24 03:09:15 +00001446 // Before that, run the common subexpression if there was one.
1447 // At least one of this or the above will be run.
1448 return addStmt(BCO->getCommon());
1449 }
1450
1451 return addStmt(condExpr);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001452}
1453
Ted Kremenek4f880632009-07-17 22:18:43 +00001454CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Ted Kremenekbc869de2011-05-10 18:42:15 +00001455 // Check if the Decl is for an __label__. If so, elide it from the
1456 // CFG entirely.
1457 if (isa<LabelDecl>(*DS->decl_begin()))
1458 return Block;
1459
Ted Kremenek29c9e622011-05-24 20:41:31 +00001460 // This case also handles static_asserts.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001461 if (DS->isSingleDecl())
1462 return VisitDeclSubExpr(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Ted Kremenek4f880632009-07-17 22:18:43 +00001464 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Ted Kremenek4f880632009-07-17 22:18:43 +00001466 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001467 typedef SmallVector<Decl*,10> BufTy;
Ted Kremenek4f880632009-07-17 22:18:43 +00001468 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Ted Kremenek4f880632009-07-17 22:18:43 +00001470 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1471 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1472 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1473 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Ted Kremenek4f880632009-07-17 22:18:43 +00001475 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1476 // automatically freed with the CFG.
1477 DeclGroupRef DG(*I);
1478 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001479 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001480 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Ted Kremenek4f880632009-07-17 22:18:43 +00001482 // Append the fake DeclStmt to block.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001483 B = VisitDeclSubExpr(DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +00001484 }
Mike Stump1eb44332009-09-09 15:08:12 +00001485
1486 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +00001487}
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Ted Kremenek4f880632009-07-17 22:18:43 +00001489/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski8599e762010-11-03 06:19:35 +00001490/// DeclStmts and initializers in them.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001491CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt *DS) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001492 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenek29c9e622011-05-24 20:41:31 +00001493 Decl *D = DS->getSingleDecl();
1494
1495 if (isa<StaticAssertDecl>(D)) {
1496 // static_asserts aren't added to the CFG because they do not impact
1497 // runtime semantics.
1498 return Block;
1499 }
1500
Marcin Swiderski8599e762010-11-03 06:19:35 +00001501 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Marcin Swiderski8599e762010-11-03 06:19:35 +00001503 if (!VD) {
1504 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001505 appendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +00001506 return Block;
Marcin Swiderski8599e762010-11-03 06:19:35 +00001507 }
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Marcin Swiderski8599e762010-11-03 06:19:35 +00001509 bool IsReference = false;
1510 bool HasTemporaries = false;
1511
1512 // Destructors of temporaries in initialization expression should be called
1513 // after initialization finishes.
Ted Kremenek4f880632009-07-17 22:18:43 +00001514 Expr *Init = VD->getInit();
Marcin Swiderski8599e762010-11-03 06:19:35 +00001515 if (Init) {
1516 IsReference = VD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +00001517 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +00001518
1519 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1520 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +00001521 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +00001522 IsReference);
1523 }
1524 }
1525
1526 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001527 appendStmt(Block, DS);
Ted Kremenek550f2232012-03-22 05:57:43 +00001528
1529 // Keep track of the last non-null block, as 'Block' can be nulled out
1530 // if the initializer expression is something like a 'while' in a
1531 // statement-expression.
1532 CFGBlock *LastBlock = Block;
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Ted Kremenek4f880632009-07-17 22:18:43 +00001534 if (Init) {
Ted Kremenek550f2232012-03-22 05:57:43 +00001535 if (HasTemporaries) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001536 // For expression with temporaries go directly to subexpression to omit
1537 // generating destructors for the second time.
Ted Kremenek550f2232012-03-22 05:57:43 +00001538 ExprWithCleanups *EC = cast<ExprWithCleanups>(Init);
1539 if (CFGBlock *newBlock = Visit(EC->getSubExpr()))
1540 LastBlock = newBlock;
1541 }
1542 else {
1543 if (CFGBlock *newBlock = Visit(Init))
1544 LastBlock = newBlock;
1545 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001546 }
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Ted Kremenek4f880632009-07-17 22:18:43 +00001548 // If the type of VD is a VLA, then we must process its size expressions.
John McCallf4c73712011-01-19 06:33:43 +00001549 for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr());
1550 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek4f880632009-07-17 22:18:43 +00001551 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001553 // Remove variable from local scope.
1554 if (ScopePos && VD == *ScopePos)
1555 ++ScopePos;
1556
Ted Kremenek550f2232012-03-22 05:57:43 +00001557 return Block ? Block : LastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001558}
1559
Ted Kremenek9c378f72011-08-12 23:37:29 +00001560CFGBlock *CFGBuilder::VisitIfStmt(IfStmt *I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001561 // We may see an if statement in the middle of a basic block, or it may be the
1562 // first statement we are processing. In either case, we create a new basic
1563 // block. First, we create the blocks for the then...else statements, and
1564 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +00001565 // middle of a block, we stop processing that block. That block is then the
1566 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +00001567
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001568 // Save local scope position because in case of condition variable ScopePos
1569 // won't be restored when traversing AST.
1570 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1571
1572 // Create local scope for possible condition variable.
1573 // Store scope position. Add implicit destructor.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001574 if (VarDecl *VD = I->getConditionVariable()) {
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001575 LocalScope::const_iterator BeginScopePos = ScopePos;
1576 addLocalScopeForVarDecl(VD);
1577 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1578 }
1579
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001580 // The block we were processing is now finished. Make it the successor
Mike Stump6d9828c2009-07-17 01:31:16 +00001581 // block.
1582 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001583 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001584 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001585 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001586 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001587
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001588 // Process the false branch.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001589 CFGBlock *ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001590
Ted Kremenek9c378f72011-08-12 23:37:29 +00001591 if (Stmt *Else = I->getElse()) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001592 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001593
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001594 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +00001595 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001596 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001597
1598 // If branch is not a compound statement create implicit scope
1599 // and add destructors.
1600 if (!isa<CompoundStmt>(Else))
1601 addLocalScopeAndDtors(Else);
1602
Ted Kremenek4f880632009-07-17 22:18:43 +00001603 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +00001604
Ted Kremenekb6f7b722007-08-30 18:13:31 +00001605 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1606 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001607 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001608 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001609 return 0;
1610 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001611 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001612
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001613 // Process the true branch.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001614 CFGBlock *ThenBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001615 {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001616 Stmt *Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001617 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001618 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001619 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001620
1621 // If branch is not a compound statement create implicit scope
1622 // and add destructors.
1623 if (!isa<CompoundStmt>(Then))
1624 addLocalScopeAndDtors(Then);
1625
Ted Kremenek4f880632009-07-17 22:18:43 +00001626 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +00001627
Ted Kremenekdbdf7942009-04-01 03:52:47 +00001628 if (!ThenBlock) {
1629 // We can reach here if the "then" body has all NullStmts.
1630 // Create an empty block so we can distinguish between true and false
1631 // branches in path-sensitive analyses.
1632 ThenBlock = createBlock(false);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001633 addSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +00001634 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001635 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001636 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001637 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001638 }
1639
Mike Stump6d9828c2009-07-17 01:31:16 +00001640 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001641 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001642
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001643 // Set the terminator of the new block to the If statement.
1644 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001645
Mike Stump00998a02009-07-23 23:25:26 +00001646 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001647 const TryResult &KnownVal = tryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001648
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001649 // Now add the successors.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001650 addSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1651 addSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001652
1653 // Add the condition as the last statement in the new block. This may create
1654 // new blocks as the condition may contain control-flow. Any newly created
1655 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001656 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001657
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001658 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1659 // and the condition variable initialization to the CFG.
1660 if (VarDecl *VD = I->getConditionVariable()) {
1661 if (Expr *Init = VD->getInit()) {
1662 autoCreateBlock();
Ted Kremenekd40066b2011-04-04 23:29:12 +00001663 appendStmt(Block, I->getConditionVariableDeclStmt());
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001664 addStmt(Init);
1665 }
1666 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001667
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001668 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001669}
Mike Stump6d9828c2009-07-17 01:31:16 +00001670
1671
Ted Kremenek9c378f72011-08-12 23:37:29 +00001672CFGBlock *CFGBuilder::VisitReturnStmt(ReturnStmt *R) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001673 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001674 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001675 // NOTE: If a "return" appears in the middle of a block, this means that the
1676 // code afterwards is DEAD (unreachable). We still keep a basic block
1677 // for that code; a simple "mark-and-sweep" from the entry block will be
1678 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001679
1680 // Create the new block.
1681 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001682
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001683 // The Exit block is the only successor.
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001684 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001685 addSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001686
1687 // Add the return statement to the block. This may create new blocks if R
1688 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001689 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001690}
1691
Ted Kremenek9c378f72011-08-12 23:37:29 +00001692CFGBlock *CFGBuilder::VisitLabelStmt(LabelStmt *L) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001693 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +00001694 addStmt(L->getSubStmt());
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001695 CFGBlock *LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001696
Ted Kremenek4f880632009-07-17 22:18:43 +00001697 if (!LabelBlock) // This can happen when the body is empty, i.e.
1698 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +00001699
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001700 assert(LabelMap.find(L->getDecl()) == LabelMap.end() &&
1701 "label already in map");
1702 LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001703
1704 // Labels partition blocks, so this is the end of the basic block we were
1705 // processing (L is the block's label). Because this is label (and we have
1706 // already processed the substatement) there is no extra control-flow to worry
1707 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +00001708 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001709 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001710 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001711
1712 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001713 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001714
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001715 // This block is now the implicit successor of other blocks.
1716 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001717
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001718 return LabelBlock;
1719}
1720
Ted Kremenek9c378f72011-08-12 23:37:29 +00001721CFGBlock *CFGBuilder::VisitGotoStmt(GotoStmt *G) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001722 // Goto is a control-flow statement. Thus we stop processing the current
1723 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001724
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001725 Block = createBlock(false);
1726 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001727
1728 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001729 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001730
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001731 if (I == LabelMap.end())
1732 // We will need to backpatch this block later.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001733 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1734 else {
1735 JumpTarget JT = I->second;
Ted Kremenek9ce52702011-01-07 19:37:16 +00001736 addAutomaticObjDtors(ScopePos, JT.scopePosition, G);
1737 addSuccessor(Block, JT.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001738 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001739
Mike Stump6d9828c2009-07-17 01:31:16 +00001740 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001741}
1742
Ted Kremenek9c378f72011-08-12 23:37:29 +00001743CFGBlock *CFGBuilder::VisitForStmt(ForStmt *F) {
1744 CFGBlock *LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001745
Marcin Swiderski47575f12010-10-01 01:38:14 +00001746 // Save local scope position because in case of condition variable ScopePos
1747 // won't be restored when traversing AST.
1748 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1749
1750 // Create local scope for init statement and possible condition variable.
1751 // Add destructor for init statement and condition variable.
1752 // Store scope position for continue statement.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001753 if (Stmt *Init = F->getInit())
Marcin Swiderski47575f12010-10-01 01:38:14 +00001754 addLocalScopeForStmt(Init);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001755 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1756
Ted Kremenek9c378f72011-08-12 23:37:29 +00001757 if (VarDecl *VD = F->getConditionVariable())
Marcin Swiderski47575f12010-10-01 01:38:14 +00001758 addLocalScopeForVarDecl(VD);
1759 LocalScope::const_iterator ContinueScopePos = ScopePos;
1760
1761 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1762
Mike Stumpfefb9f72009-07-21 01:12:51 +00001763 // "for" is a control-flow statement. Thus we stop processing the current
1764 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001765 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001766 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001767 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001768 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001769 } else
1770 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001771
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001772 // Save the current value for the break targets.
1773 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001774 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001775 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001776
Mike Stump6d9828c2009-07-17 01:31:16 +00001777 // Because of short-circuit evaluation, the condition of the loop can span
1778 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1779 // evaluate the condition.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001780 CFGBlock *ExitConditionBlock = createBlock(false);
1781 CFGBlock *EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001782
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001783 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001784 ExitConditionBlock->setTerminator(F);
1785
1786 // Now add the actual condition to the condition block. Because the condition
1787 // itself may contain control-flow, new blocks may be created.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001788 if (Stmt *C = F->getCond()) {
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001789 Block = ExitConditionBlock;
1790 EntryConditionBlock = addStmt(C);
Ted Kremenek9ce52702011-01-07 19:37:16 +00001791 if (badCFG)
1792 return 0;
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001793 assert(Block == EntryConditionBlock ||
1794 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001795
1796 // If this block contains a condition variable, add both the condition
1797 // variable and initializer to the CFG.
1798 if (VarDecl *VD = F->getConditionVariable()) {
1799 if (Expr *Init = VD->getInit()) {
1800 autoCreateBlock();
Ted Kremenekd40066b2011-04-04 23:29:12 +00001801 appendStmt(Block, F->getConditionVariableDeclStmt());
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001802 EntryConditionBlock = addStmt(Init);
1803 assert(Block == EntryConditionBlock);
1804 }
1805 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001806
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001807 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001808 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001809 return 0;
1810 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001811 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001812
Mike Stump6d9828c2009-07-17 01:31:16 +00001813 // The condition block is the implicit successor for the loop body as well as
1814 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001815 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001816
Mike Stump00998a02009-07-23 23:25:26 +00001817 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001818 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Mike Stump00998a02009-07-23 23:25:26 +00001820 if (F->getCond())
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001821 KnownVal = tryEvaluateBool(F->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001822
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001823 // Now create the loop body.
1824 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001825 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001826
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001827 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001828 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1829 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001830
Ted Kremenekaf603f72007-08-30 18:39:40 +00001831 // Create a new block to contain the (bottom) of the loop body.
1832 Block = NULL;
Marcin Swiderski47575f12010-10-01 01:38:14 +00001833
1834 // Loop body should end with destructor of Condition variable (if any).
1835 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump6d9828c2009-07-17 01:31:16 +00001836
Ted Kremenek9c378f72011-08-12 23:37:29 +00001837 if (Stmt *I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001838 // Generate increment code in its own basic block. This is the target of
1839 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001840 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001841 } else {
1842 // No increment code. Create a special, empty, block that is used as the
1843 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001844 assert(Succ == EntryConditionBlock);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001845 Succ = Block ? Block : createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001846 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001847
Ted Kremenek3575f842009-04-28 00:51:56 +00001848 // Finish up the increment (or empty) block if it hasn't been already.
1849 if (Block) {
1850 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001851 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001852 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001853 Block = 0;
1854 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001855
Marcin Swiderski47575f12010-10-01 01:38:14 +00001856 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001857
Ted Kremenek3575f842009-04-28 00:51:56 +00001858 // The starting block for the loop increment is the block that should
1859 // represent the 'loop target' for looping back to the start of the loop.
Ted Kremenek9ce52702011-01-07 19:37:16 +00001860 ContinueJumpTarget.block->setLoopTarget(F);
Ted Kremenek3575f842009-04-28 00:51:56 +00001861
Marcin Swiderski47575f12010-10-01 01:38:14 +00001862 // If body is not a compound statement create implicit scope
1863 // and add destructors.
1864 if (!isa<CompoundStmt>(F->getBody()))
1865 addLocalScopeAndDtors(F->getBody());
1866
Mike Stump6d9828c2009-07-17 01:31:16 +00001867 // Now populate the body block, and in the process create new blocks as we
1868 // walk the body of the loop.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001869 CFGBlock *BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001870
1871 if (!BodyBlock)
Ted Kremenek9ce52702011-01-07 19:37:16 +00001872 BodyBlock = ContinueJumpTarget.block;//can happen for "for (...;...;...);"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001873 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001874 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001875
Ted Kremenek941fde82009-07-24 04:47:11 +00001876 // This new body block is a successor to our "exit" condition block.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001877 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001878 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001879
Ted Kremenek941fde82009-07-24 04:47:11 +00001880 // Link up the condition block with the code that follows the loop. (the
1881 // false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001882 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001883
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001884 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001885 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001886 if (Stmt *I = F->getInit()) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001887 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001888 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001889 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001890
1891 // There is no loop initialization. We are thus basically a while loop.
1892 // NULL out Block to force lazy block construction.
1893 Block = NULL;
1894 Succ = EntryConditionBlock;
1895 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001896}
1897
Ted Kremenek115c1b92010-04-11 17:02:10 +00001898CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001899 if (asc.alwaysAdd(*this, M)) {
Ted Kremenek115c1b92010-04-11 17:02:10 +00001900 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001901 appendStmt(Block, M);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001902 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001903 return Visit(M->getBase());
Ted Kremenek115c1b92010-04-11 17:02:10 +00001904}
1905
Ted Kremenek9c378f72011-08-12 23:37:29 +00001906CFGBlock *CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
Ted Kremenek514de5a2008-11-11 17:10:00 +00001907 // Objective-C fast enumeration 'for' statements:
1908 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1909 //
1910 // for ( Type newVariable in collection_expression ) { statements }
1911 //
1912 // becomes:
1913 //
1914 // prologue:
1915 // 1. collection_expression
1916 // T. jump to loop_entry
1917 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001918 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001919 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1920 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1921 // TB:
1922 // statements
1923 // T. jump to loop_entry
1924 // FB:
1925 // what comes after
1926 //
1927 // and
1928 //
1929 // Type existingItem;
1930 // for ( existingItem in expression ) { statements }
1931 //
1932 // becomes:
1933 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001934 // the same with newVariable replaced with existingItem; the binding works
1935 // the same except that for one ObjCForCollectionStmt::getElement() returns
1936 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001937 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001938
Ted Kremenek9c378f72011-08-12 23:37:29 +00001939 CFGBlock *LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001940
Ted Kremenek514de5a2008-11-11 17:10:00 +00001941 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001942 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001943 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001944 LoopSuccessor = Block;
1945 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001946 } else
1947 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001948
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001949 // Build the condition blocks.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001950 CFGBlock *ExitConditionBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001951
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001952 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001953 ExitConditionBlock->setTerminator(S);
1954
1955 // The last statement in the block should be the ObjCForCollectionStmt, which
1956 // performs the actual binding to 'element' and determines if there are any
1957 // more items in the collection.
Ted Kremenek892697d2010-12-16 07:46:53 +00001958 appendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001959 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001960
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001961 // Walk the 'element' expression to see if there are any side-effects. We
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001962 // generate new blocks as necessary. We DON'T add the statement by default to
Mike Stump6d9828c2009-07-17 01:31:16 +00001963 // the CFG unless it contains control-flow.
Ted Kremenek012614e2011-08-17 21:04:19 +00001964 CFGBlock *EntryConditionBlock = Visit(S->getElement(),
1965 AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001966 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001967 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001968 return 0;
1969 Block = 0;
1970 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001971
1972 // The condition block is the implicit successor for the loop body as well as
1973 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001974 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001975
Ted Kremenek514de5a2008-11-11 17:10:00 +00001976 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001977 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001978 // Save the current values for Succ, continue and break targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001979 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1980 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1981 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001982
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001983 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1984 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001985
Ted Kremenek9c378f72011-08-12 23:37:29 +00001986 CFGBlock *BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001987
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001988 if (!BodyBlock)
1989 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001990 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001991 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001992 return 0;
1993 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001994
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001995 // This new body block is a successor to our "exit" condition block.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001996 addSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001997 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001998
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001999 // Link up the condition block with the code that follows the loop.
2000 // (the false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002001 addSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00002002
Ted Kremenek514de5a2008-11-11 17:10:00 +00002003 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00002004 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00002005 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00002006}
2007
Ted Kremenek8e282c32012-03-06 23:40:47 +00002008CFGBlock *CFGBuilder::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
2009 // Inline the body.
2010 return addStmt(S->getSubStmt());
2011 // TODO: consider adding cleanups for the end of @autoreleasepool scope.
2012}
2013
Ted Kremenek9c378f72011-08-12 23:37:29 +00002014CFGBlock *CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
Ted Kremenekb3b0b362009-05-02 01:49:13 +00002015 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00002016
Ted Kremenekb3b0b362009-05-02 01:49:13 +00002017 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002018 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002019
Ted Kremenekda5348e2009-05-05 23:11:51 +00002020 // The sync body starts its own basic block. This makes it a little easier
2021 // for diagnostic clients.
2022 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002023 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00002024 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002025
Ted Kremenekda5348e2009-05-05 23:11:51 +00002026 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00002027 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00002028 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002029
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00002030 // Add the @synchronized to the CFG.
2031 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002032 appendStmt(Block, S);
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00002033
Ted Kremenekb3b0b362009-05-02 01:49:13 +00002034 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00002035 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00002036}
Mike Stump6d9828c2009-07-17 01:31:16 +00002037
Ted Kremenek9c378f72011-08-12 23:37:29 +00002038CFGBlock *CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002039 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00002040 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00002041}
Ted Kremenek514de5a2008-11-11 17:10:00 +00002042
John McCall4b9c2d22011-11-06 09:01:30 +00002043CFGBlock *CFGBuilder::VisitPseudoObjectExpr(PseudoObjectExpr *E) {
2044 autoCreateBlock();
2045
2046 // Add the PseudoObject as the last thing.
2047 appendStmt(Block, E);
2048
2049 CFGBlock *lastBlock = Block;
2050
2051 // Before that, evaluate all of the semantics in order. In
2052 // CFG-land, that means appending them in reverse order.
2053 for (unsigned i = E->getNumSemanticExprs(); i != 0; ) {
2054 Expr *Semantic = E->getSemanticExpr(--i);
2055
2056 // If the semantic is an opaque value, we're being asked to bind
2057 // it to its source expression.
2058 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
2059 Semantic = OVE->getSourceExpr();
2060
2061 if (CFGBlock *B = Visit(Semantic))
2062 lastBlock = B;
2063 }
2064
2065 return lastBlock;
2066}
2067
Ted Kremenek9c378f72011-08-12 23:37:29 +00002068CFGBlock *CFGBuilder::VisitWhileStmt(WhileStmt *W) {
2069 CFGBlock *LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002070
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002071 // Save local scope position because in case of condition variable ScopePos
2072 // won't be restored when traversing AST.
2073 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2074
2075 // Create local scope for possible condition variable.
2076 // Store scope position for continue statement.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002077 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Ted Kremenek9c378f72011-08-12 23:37:29 +00002078 if (VarDecl *VD = W->getConditionVariable()) {
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002079 addLocalScopeForVarDecl(VD);
2080 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
2081 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002082
Mike Stumpfefb9f72009-07-21 01:12:51 +00002083 // "while" is a control-flow statement. Thus we stop processing the current
2084 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002085 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002086 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002087 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002088 LoopSuccessor = Block;
Ted Kremenek6b12da92011-02-21 22:11:26 +00002089 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00002090 } else
2091 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00002092
2093 // Because of short-circuit evaluation, the condition of the loop can span
2094 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
2095 // evaluate the condition.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002096 CFGBlock *ExitConditionBlock = createBlock(false);
2097 CFGBlock *EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002098
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002099 // Set the terminator for the "exit" condition block.
2100 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00002101
2102 // Now add the actual condition to the condition block. Because the condition
2103 // itself may contain control-flow, new blocks may be created. Thus we update
2104 // "Succ" after adding the condition.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002105 if (Stmt *C = W->getCond()) {
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002106 Block = ExitConditionBlock;
2107 EntryConditionBlock = addStmt(C);
Zhongxing Xua1898dd2010-10-27 03:23:10 +00002108 // The condition might finish the current 'Block'.
2109 Block = EntryConditionBlock;
Ted Kremenekad5a8942010-08-02 23:46:59 +00002110
Ted Kremenek4ec010a2009-12-24 01:34:10 +00002111 // If this block contains a condition variable, add both the condition
2112 // variable and initializer to the CFG.
2113 if (VarDecl *VD = W->getConditionVariable()) {
2114 if (Expr *Init = VD->getInit()) {
2115 autoCreateBlock();
Ted Kremenekd40066b2011-04-04 23:29:12 +00002116 appendStmt(Block, W->getConditionVariableDeclStmt());
Ted Kremenek4ec010a2009-12-24 01:34:10 +00002117 EntryConditionBlock = addStmt(Init);
2118 assert(Block == EntryConditionBlock);
2119 }
2120 }
2121
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002122 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002123 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002124 return 0;
2125 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002126 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002127
2128 // The condition block is the implicit successor for the loop body as well as
2129 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002130 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002131
Mike Stump00998a02009-07-23 23:25:26 +00002132 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002133 const TryResult& KnownVal = tryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00002134
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002135 // Process the loop body.
2136 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00002137 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002138
2139 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002140 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2141 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2142 save_break(BreakJumpTarget);
Ted Kremenekf6e85412009-04-28 03:09:44 +00002143
Mike Stump6d9828c2009-07-17 01:31:16 +00002144 // Create an empty block to represent the transition block for looping back
2145 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00002146 Block = 0;
2147 assert(Succ == EntryConditionBlock);
2148 Succ = createBlock();
2149 Succ->setLoopTarget(W);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002150 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002151
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002152 // All breaks should go to the code following the loop.
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002153 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002154
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002155 // NULL out Block to force lazy instantiation of blocks for the body.
2156 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002157
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002158 // Loop body should end with destructor of Condition variable (if any).
2159 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
2160
2161 // If body is not a compound statement create implicit scope
2162 // and add destructors.
2163 if (!isa<CompoundStmt>(W->getBody()))
2164 addLocalScopeAndDtors(W->getBody());
2165
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002166 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002167 CFGBlock *BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002168
Ted Kremenekaf603f72007-08-30 18:39:40 +00002169 if (!BodyBlock)
Ted Kremenek9ce52702011-01-07 19:37:16 +00002170 BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002171 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002172 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002173 return 0;
2174 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002175
Ted Kremenek941fde82009-07-24 04:47:11 +00002176 // Add the loop body entry as a successor to the condition.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002177 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002178 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002179
Ted Kremenek941fde82009-07-24 04:47:11 +00002180 // Link up the condition block with the code that follows the loop. (the
2181 // false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002182 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002183
2184 // There can be no more statements in the condition block since we loop back
2185 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002186 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002187
Ted Kremenek4ec010a2009-12-24 01:34:10 +00002188 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002189 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002190 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002191}
Mike Stump1eb44332009-09-09 15:08:12 +00002192
2193
Ted Kremenek9c378f72011-08-12 23:37:29 +00002194CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002195 // FIXME: For now we pretend that @catch and the code it contains does not
2196 // exit.
2197 return Block;
2198}
Mike Stump6d9828c2009-07-17 01:31:16 +00002199
Ted Kremenek9c378f72011-08-12 23:37:29 +00002200CFGBlock *CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
Ted Kremenek2fda5042008-12-09 20:20:09 +00002201 // FIXME: This isn't complete. We basically treat @throw like a return
2202 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00002203
Ted Kremenek6c249722009-09-24 18:45:41 +00002204 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002205 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002206 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002207
Ted Kremenek2fda5042008-12-09 20:20:09 +00002208 // Create the new block.
2209 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002210
Ted Kremenek2fda5042008-12-09 20:20:09 +00002211 // The Exit block is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002212 addSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00002213
2214 // Add the statement to the block. This may create new blocks if S contains
2215 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00002216 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00002217}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002218
Ted Kremenek9c378f72011-08-12 23:37:29 +00002219CFGBlock *CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr *T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00002220 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002221 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00002222 return 0;
2223
2224 // Create the new block.
2225 Block = createBlock(false);
2226
Mike Stump5d1d2022010-01-19 02:20:09 +00002227 if (TryTerminatedBlock)
2228 // The current try statement is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002229 addSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00002230 else
Mike Stump5d1d2022010-01-19 02:20:09 +00002231 // otherwise the Exit block is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002232 addSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00002233
2234 // Add the statement to the block. This may create new blocks if S contains
2235 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00002236 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00002237}
2238
Ted Kremenek9c378f72011-08-12 23:37:29 +00002239CFGBlock *CFGBuilder::VisitDoStmt(DoStmt *D) {
2240 CFGBlock *LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002241
Mike Stump8f9893a2009-07-21 01:27:50 +00002242 // "do...while" is a control-flow statement. Thus we stop processing the
2243 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002244 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002245 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002246 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002247 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002248 } else
2249 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00002250
2251 // Because of short-circuit evaluation, the condition of the loop can span
2252 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
2253 // evaluate the condition.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002254 CFGBlock *ExitConditionBlock = createBlock(false);
2255 CFGBlock *EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002256
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002257 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00002258 ExitConditionBlock->setTerminator(D);
2259
2260 // Now add the actual condition to the condition block. Because the condition
2261 // itself may contain control-flow, new blocks may be created.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002262 if (Stmt *C = D->getCond()) {
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002263 Block = ExitConditionBlock;
2264 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002265 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002266 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002267 return 0;
2268 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002269 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002270
Ted Kremenek54827132008-02-27 07:20:00 +00002271 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002272 Succ = EntryConditionBlock;
2273
Mike Stump00998a02009-07-23 23:25:26 +00002274 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002275 const TryResult &KnownVal = tryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00002276
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002277 // Process the loop body.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002278 CFGBlock *BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002279 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002280 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002281
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002282 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002283 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2284 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2285 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00002286
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002287 // All continues within this loop should go to the condition block
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002288 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002289
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002290 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002291 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002292
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002293 // NULL out Block to force lazy instantiation of blocks for the body.
2294 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002295
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002296 // If body is not a compound statement create implicit scope
2297 // and add destructors.
2298 if (!isa<CompoundStmt>(D->getBody()))
2299 addLocalScopeAndDtors(D->getBody());
2300
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002301 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002302 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002303
Ted Kremenekaf603f72007-08-30 18:39:40 +00002304 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00002305 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002306 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002307 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002308 return 0;
2309 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002310
Ted Kremenekd173dc72010-08-17 20:59:56 +00002311 if (!KnownVal.isFalse()) {
2312 // Add an intermediate block between the BodyBlock and the
2313 // ExitConditionBlock to represent the "loop back" transition. Create an
2314 // empty block to represent the transition block for looping back to the
2315 // head of the loop.
2316 // FIXME: Can we do this more efficiently without adding another block?
2317 Block = NULL;
2318 Succ = BodyBlock;
2319 CFGBlock *LoopBackBlock = createBlock();
2320 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00002321
Ted Kremenekd173dc72010-08-17 20:59:56 +00002322 // Add the loop body entry as a successor to the condition.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002323 addSuccessor(ExitConditionBlock, LoopBackBlock);
Ted Kremenekd173dc72010-08-17 20:59:56 +00002324 }
2325 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002326 addSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002327 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002328
Ted Kremenek941fde82009-07-24 04:47:11 +00002329 // Link up the condition block with the code that follows the loop.
2330 // (the false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002331 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002332
2333 // There can be no more statements in the body block(s) since we loop back to
2334 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002335 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002336
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002337 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002338 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002339 return BodyBlock;
2340}
2341
Ted Kremenek9c378f72011-08-12 23:37:29 +00002342CFGBlock *CFGBuilder::VisitContinueStmt(ContinueStmt *C) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002343 // "continue" is a control-flow statement. Thus we stop processing the
2344 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002345 if (badCFG)
2346 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002347
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002348 // Now create a new block that ends with the continue statement.
2349 Block = createBlock(false);
2350 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00002351
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002352 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002353 // incomplete AST. This means the CFG cannot be constructed.
Ted Kremenek9ce52702011-01-07 19:37:16 +00002354 if (ContinueJumpTarget.block) {
2355 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C);
2356 addSuccessor(Block, ContinueJumpTarget.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002357 } else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002358 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00002359
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002360 return Block;
2361}
Mike Stump1eb44332009-09-09 15:08:12 +00002362
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002363CFGBlock *CFGBuilder::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E,
2364 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002365
Ted Kremenek3179a452011-03-10 01:14:11 +00002366 if (asc.alwaysAdd(*this, E)) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002367 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002368 appendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002369 }
Mike Stump1eb44332009-09-09 15:08:12 +00002370
Ted Kremenek4f880632009-07-17 22:18:43 +00002371 // VLA types have expressions that must be evaluated.
Ted Kremenek97e50712011-04-14 01:50:50 +00002372 CFGBlock *lastBlock = Block;
2373
Ted Kremenek4f880632009-07-17 22:18:43 +00002374 if (E->isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00002375 for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr());
Ted Kremenek4f880632009-07-17 22:18:43 +00002376 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek97e50712011-04-14 01:50:50 +00002377 lastBlock = addStmt(VA->getSizeExpr());
Ted Kremenekf91a5b02011-08-06 00:30:00 +00002378 }
Ted Kremenek97e50712011-04-14 01:50:50 +00002379 return lastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002380}
Mike Stump1eb44332009-09-09 15:08:12 +00002381
Ted Kremenek4f880632009-07-17 22:18:43 +00002382/// VisitStmtExpr - Utility method to handle (nested) statement
2383/// expressions (a GCC extension).
Ted Kremenek9c378f72011-08-12 23:37:29 +00002384CFGBlock *CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00002385 if (asc.alwaysAdd(*this, SE)) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002386 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002387 appendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002388 }
Ted Kremenek4f880632009-07-17 22:18:43 +00002389 return VisitCompoundStmt(SE->getSubStmt());
2390}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002391
Ted Kremenek9c378f72011-08-12 23:37:29 +00002392CFGBlock *CFGBuilder::VisitSwitchStmt(SwitchStmt *Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002393 // "switch" is a control-flow statement. Thus we stop processing the current
2394 // block.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002395 CFGBlock *SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002396
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002397 // Save local scope position because in case of condition variable ScopePos
2398 // won't be restored when traversing AST.
2399 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2400
2401 // Create local scope for possible condition variable.
2402 // Store scope position. Add implicit destructor.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002403 if (VarDecl *VD = Terminator->getConditionVariable()) {
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002404 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2405 addLocalScopeForVarDecl(VD);
2406 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2407 }
2408
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002409 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002410 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002411 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002412 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00002413 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002414
2415 // Save the current "switch" context.
2416 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002417 save_default(DefaultCaseBlock);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002418 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002419
Mike Stump6d9828c2009-07-17 01:31:16 +00002420 // Set the "default" case to be the block after the switch statement. If the
2421 // switch statement contains a "default:", this value will be overwritten with
2422 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002423 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00002424
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002425 // Create a new block that will contain the switch statement.
2426 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002427
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002428 // Now process the switch body. The code after the switch is the implicit
2429 // successor.
2430 Succ = SwitchSuccessor;
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002431 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002432
2433 // When visiting the body, the case statements should automatically get linked
2434 // up to the switch. We also don't keep a pointer to the body, since all
2435 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002436 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002437 Block = NULL;
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002438
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002439 // For pruning unreachable case statements, save the current state
2440 // for tracking the condition value.
2441 SaveAndRestore<bool> save_switchExclusivelyCovered(switchExclusivelyCovered,
2442 false);
Ted Kremenek04982472011-03-04 01:03:41 +00002443
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002444 // Determine if the switch condition can be explicitly evaluated.
2445 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek04982472011-03-04 01:03:41 +00002446 Expr::EvalResult result;
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002447 bool b = tryEvaluate(Terminator->getCond(), result);
2448 SaveAndRestore<Expr::EvalResult*> save_switchCond(switchCond,
2449 b ? &result : 0);
Ted Kremenek04982472011-03-04 01:03:41 +00002450
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002451 // If body is not a compound statement create implicit scope
2452 // and add destructors.
2453 if (!isa<CompoundStmt>(Terminator->getBody()))
2454 addLocalScopeAndDtors(Terminator->getBody());
2455
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002456 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002457 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002458 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002459 return 0;
2460 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002461
Mike Stump6d9828c2009-07-17 01:31:16 +00002462 // If we have no "default:" case, the default transition is to the code
Ted Kremenek432c4782011-03-16 04:32:01 +00002463 // following the switch body. Moreover, take into account if all the
2464 // cases of a switch are covered (e.g., switching on an enum value).
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002465 addSuccessor(SwitchTerminatedBlock,
Ted Kremenek432c4782011-03-16 04:32:01 +00002466 switchExclusivelyCovered || Terminator->isAllEnumCasesCovered()
2467 ? 0 : DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002468
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002469 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002470 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002471 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002472 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00002473
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002474 // Finally, if the SwitchStmt contains a condition variable, add both the
2475 // SwitchStmt and the condition variable initialization to the CFG.
2476 if (VarDecl *VD = Terminator->getConditionVariable()) {
2477 if (Expr *Init = VD->getInit()) {
2478 autoCreateBlock();
Ted Kremenekd40066b2011-04-04 23:29:12 +00002479 appendStmt(Block, Terminator->getConditionVariableDeclStmt());
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002480 addStmt(Init);
2481 }
2482 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00002483
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002484 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002485}
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002486
2487static bool shouldAddCase(bool &switchExclusivelyCovered,
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002488 const Expr::EvalResult *switchCond,
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002489 const CaseStmt *CS,
2490 ASTContext &Ctx) {
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002491 if (!switchCond)
2492 return true;
2493
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002494 bool addCase = false;
Ted Kremenek04982472011-03-04 01:03:41 +00002495
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002496 if (!switchExclusivelyCovered) {
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002497 if (switchCond->Val.isInt()) {
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002498 // Evaluate the LHS of the case value.
Richard Smith85df96c2011-10-14 20:22:00 +00002499 const llvm::APSInt &lhsInt = CS->getLHS()->EvaluateKnownConstInt(Ctx);
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002500 const llvm::APSInt &condInt = switchCond->Val.getInt();
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002501
2502 if (condInt == lhsInt) {
2503 addCase = true;
2504 switchExclusivelyCovered = true;
2505 }
2506 else if (condInt < lhsInt) {
2507 if (const Expr *RHS = CS->getRHS()) {
2508 // Evaluate the RHS of the case value.
Richard Smith85df96c2011-10-14 20:22:00 +00002509 const llvm::APSInt &V2 = RHS->EvaluateKnownConstInt(Ctx);
2510 if (V2 <= condInt) {
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002511 addCase = true;
2512 switchExclusivelyCovered = true;
2513 }
2514 }
2515 }
2516 }
2517 else
2518 addCase = true;
2519 }
2520 return addCase;
2521}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002522
Ted Kremenek9c378f72011-08-12 23:37:29 +00002523CFGBlock *CFGBuilder::VisitCaseStmt(CaseStmt *CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002524 // CaseStmts are essentially labels, so they are the first statement in a
2525 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002526 CFGBlock *TopBlock = 0, *LastBlock = 0;
Ted Kremenek04982472011-03-04 01:03:41 +00002527
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002528 if (Stmt *Sub = CS->getSubStmt()) {
2529 // For deeply nested chains of CaseStmts, instead of doing a recursion
2530 // (which can blow out the stack), manually unroll and create blocks
2531 // along the way.
2532 while (isa<CaseStmt>(Sub)) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002533 CFGBlock *currentBlock = createBlock(false);
2534 currentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002535
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002536 if (TopBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002537 addSuccessor(LastBlock, currentBlock);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002538 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002539 TopBlock = currentBlock;
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002540
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002541 addSuccessor(SwitchTerminatedBlock,
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002542 shouldAddCase(switchExclusivelyCovered, switchCond,
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002543 CS, *Context)
2544 ? currentBlock : 0);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002545
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002546 LastBlock = currentBlock;
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002547 CS = cast<CaseStmt>(Sub);
2548 Sub = CS->getSubStmt();
2549 }
2550
2551 addStmt(Sub);
2552 }
Mike Stump1eb44332009-09-09 15:08:12 +00002553
Ted Kremenek9c378f72011-08-12 23:37:29 +00002554 CFGBlock *CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002555 if (!CaseBlock)
2556 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002557
2558 // Cases statements partition blocks, so this is the top of the basic block we
2559 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00002560 CaseBlock->setLabel(CS);
2561
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002562 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002563 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002564
2565 // Add this block to the list of successors for the block with the switch
2566 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00002567 assert(SwitchTerminatedBlock);
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002568 addSuccessor(SwitchTerminatedBlock,
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002569 shouldAddCase(switchExclusivelyCovered, switchCond,
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002570 CS, *Context)
2571 ? CaseBlock : 0);
Mike Stump6d9828c2009-07-17 01:31:16 +00002572
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002573 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2574 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002575
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002576 if (TopBlock) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002577 addSuccessor(LastBlock, CaseBlock);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002578 Succ = TopBlock;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002579 } else {
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002580 // This block is now the implicit successor of other blocks.
2581 Succ = CaseBlock;
2582 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002583
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002584 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002585}
Mike Stump6d9828c2009-07-17 01:31:16 +00002586
Ted Kremenek9c378f72011-08-12 23:37:29 +00002587CFGBlock *CFGBuilder::VisitDefaultStmt(DefaultStmt *Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002588 if (Terminator->getSubStmt())
2589 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00002590
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002591 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002592
2593 if (!DefaultCaseBlock)
2594 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002595
2596 // Default statements partition blocks, so this is the top of the basic block
2597 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00002598 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00002599
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002600 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002601 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002602
Mike Stump6d9828c2009-07-17 01:31:16 +00002603 // Unlike case statements, we don't add the default block to the successors
2604 // for the switch statement immediately. This is done when we finish
2605 // processing the switch statement. This allows for the default case
2606 // (including a fall-through to the code after the switch statement) to always
2607 // be the last successor of a switch-terminated block.
2608
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002609 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2610 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002611
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002612 // This block is now the implicit successor of other blocks.
2613 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002614
2615 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00002616}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002617
Mike Stump5d1d2022010-01-19 02:20:09 +00002618CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2619 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2620 // current block.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002621 CFGBlock *TrySuccessor = NULL;
Mike Stump5d1d2022010-01-19 02:20:09 +00002622
2623 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002624 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002625 return 0;
2626 TrySuccessor = Block;
2627 } else TrySuccessor = Succ;
2628
Mike Stumpa1f93632010-01-20 01:15:34 +00002629 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00002630
2631 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002632 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00002633 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00002634 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00002635
Mike Stumpa1f93632010-01-20 01:15:34 +00002636 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00002637 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2638 // The code after the try is the implicit successor.
2639 Succ = TrySuccessor;
2640 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00002641 if (CS->getExceptionDecl() == 0) {
2642 HasCatchAll = true;
2643 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002644 Block = NULL;
2645 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2646 if (CatchBlock == 0)
2647 return 0;
2648 // Add this block to the list of successors for the block with the try
2649 // statement.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002650 addSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00002651 }
Mike Stumpa1f93632010-01-20 01:15:34 +00002652 if (!HasCatchAll) {
2653 if (PrevTryTerminatedBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002654 addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00002655 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002656 addSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00002657 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002658
2659 // The code after the try is the implicit successor.
2660 Succ = TrySuccessor;
2661
Mike Stumpf00cca52010-01-20 01:30:58 +00002662 // Save the current "try" context.
Ted Kremenekf0e71ae2011-08-23 23:05:07 +00002663 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock, NewTryTerminatedBlock);
2664 cfg->addTryDispatchBlock(TryTerminatedBlock);
Mike Stumpf00cca52010-01-20 01:30:58 +00002665
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002666 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00002667 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002668 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00002669 return Block;
2670}
2671
Ted Kremenek9c378f72011-08-12 23:37:29 +00002672CFGBlock *CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt *CS) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002673 // CXXCatchStmt are treated like labels, so they are the first statement in a
2674 // block.
2675
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002676 // Save local scope position because in case of exception variable ScopePos
2677 // won't be restored when traversing AST.
2678 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2679
2680 // Create local scope for possible exception variable.
2681 // Store scope position. Add implicit destructor.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002682 if (VarDecl *VD = CS->getExceptionDecl()) {
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002683 LocalScope::const_iterator BeginScopePos = ScopePos;
2684 addLocalScopeForVarDecl(VD);
2685 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2686 }
2687
Mike Stump5d1d2022010-01-19 02:20:09 +00002688 if (CS->getHandlerBlock())
2689 addStmt(CS->getHandlerBlock());
2690
Ted Kremenek9c378f72011-08-12 23:37:29 +00002691 CFGBlock *CatchBlock = Block;
Mike Stump5d1d2022010-01-19 02:20:09 +00002692 if (!CatchBlock)
2693 CatchBlock = createBlock();
Ted Kremenek337e4db2012-03-10 01:34:17 +00002694
2695 // CXXCatchStmt is more than just a label. They have semantic meaning
2696 // as well, as they implicitly "initialize" the catch variable. Add
2697 // it to the CFG as a CFGElement so that the control-flow of these
2698 // semantics gets captured.
2699 appendStmt(CatchBlock, CS);
Mike Stump5d1d2022010-01-19 02:20:09 +00002700
Ted Kremenek337e4db2012-03-10 01:34:17 +00002701 // Also add the CXXCatchStmt as a label, to mirror handling of regular
2702 // labels.
Mike Stump5d1d2022010-01-19 02:20:09 +00002703 CatchBlock->setLabel(CS);
2704
Ted Kremenek337e4db2012-03-10 01:34:17 +00002705 // Bail out if the CFG is bad.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002706 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002707 return 0;
2708
2709 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2710 Block = NULL;
2711
2712 return CatchBlock;
2713}
2714
Ted Kremenek9c378f72011-08-12 23:37:29 +00002715CFGBlock *CFGBuilder::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Richard Smithad762fc2011-04-14 22:09:26 +00002716 // C++0x for-range statements are specified as [stmt.ranged]:
2717 //
2718 // {
2719 // auto && __range = range-init;
2720 // for ( auto __begin = begin-expr,
2721 // __end = end-expr;
2722 // __begin != __end;
2723 // ++__begin ) {
2724 // for-range-declaration = *__begin;
2725 // statement
2726 // }
2727 // }
2728
2729 // Save local scope position before the addition of the implicit variables.
2730 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2731
2732 // Create local scopes and destructors for range, begin and end variables.
2733 if (Stmt *Range = S->getRangeStmt())
2734 addLocalScopeForStmt(Range);
2735 if (Stmt *BeginEnd = S->getBeginEndStmt())
2736 addLocalScopeForStmt(BeginEnd);
2737 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), S);
2738
2739 LocalScope::const_iterator ContinueScopePos = ScopePos;
2740
2741 // "for" is a control-flow statement. Thus we stop processing the current
2742 // block.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002743 CFGBlock *LoopSuccessor = NULL;
Richard Smithad762fc2011-04-14 22:09:26 +00002744 if (Block) {
2745 if (badCFG)
2746 return 0;
2747 LoopSuccessor = Block;
2748 } else
2749 LoopSuccessor = Succ;
2750
2751 // Save the current value for the break targets.
2752 // All breaks should go to the code following the loop.
2753 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
2754 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
2755
2756 // The block for the __begin != __end expression.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002757 CFGBlock *ConditionBlock = createBlock(false);
Richard Smithad762fc2011-04-14 22:09:26 +00002758 ConditionBlock->setTerminator(S);
2759
2760 // Now add the actual condition to the condition block.
2761 if (Expr *C = S->getCond()) {
2762 Block = ConditionBlock;
2763 CFGBlock *BeginConditionBlock = addStmt(C);
2764 if (badCFG)
2765 return 0;
2766 assert(BeginConditionBlock == ConditionBlock &&
2767 "condition block in for-range was unexpectedly complex");
2768 (void)BeginConditionBlock;
2769 }
2770
2771 // The condition block is the implicit successor for the loop body as well as
2772 // any code above the loop.
2773 Succ = ConditionBlock;
2774
2775 // See if this is a known constant.
2776 TryResult KnownVal(true);
2777
2778 if (S->getCond())
2779 KnownVal = tryEvaluateBool(S->getCond());
2780
2781 // Now create the loop body.
2782 {
2783 assert(S->getBody());
2784
2785 // Save the current values for Block, Succ, and continue targets.
2786 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2787 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
2788
2789 // Generate increment code in its own basic block. This is the target of
2790 // continue statements.
2791 Block = 0;
2792 Succ = addStmt(S->getInc());
2793 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
2794
2795 // The starting block for the loop increment is the block that should
2796 // represent the 'loop target' for looping back to the start of the loop.
2797 ContinueJumpTarget.block->setLoopTarget(S);
2798
2799 // Finish up the increment block and prepare to start the loop body.
2800 assert(Block);
2801 if (badCFG)
2802 return 0;
2803 Block = 0;
2804
2805
2806 // Add implicit scope and dtors for loop variable.
2807 addLocalScopeAndDtors(S->getLoopVarStmt());
2808
2809 // Populate a new block to contain the loop body and loop variable.
2810 Block = addStmt(S->getBody());
2811 if (badCFG)
2812 return 0;
2813 Block = addStmt(S->getLoopVarStmt());
2814 if (badCFG)
2815 return 0;
2816
2817 // This new body block is a successor to our condition block.
2818 addSuccessor(ConditionBlock, KnownVal.isFalse() ? 0 : Block);
2819 }
2820
2821 // Link up the condition block with the code that follows the loop (the
2822 // false branch).
2823 addSuccessor(ConditionBlock, KnownVal.isTrue() ? 0 : LoopSuccessor);
2824
2825 // Add the initialization statements.
2826 Block = createBlock();
Richard Smithb403d6d2011-04-18 15:49:25 +00002827 addStmt(S->getBeginEndStmt());
2828 return addStmt(S->getRangeStmt());
Richard Smithad762fc2011-04-14 22:09:26 +00002829}
2830
John McCall4765fa02010-12-06 08:20:24 +00002831CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +00002832 AddStmtChoice asc) {
2833 if (BuildOpts.AddImplicitDtors) {
2834 // If adding implicit destructors visit the full expression for adding
2835 // destructors of temporaries.
2836 VisitForTemporaryDtors(E->getSubExpr());
2837
2838 // Full expression has to be added as CFGStmt so it will be sequenced
2839 // before destructors of it's temporaries.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002840 asc = asc.withAlwaysAdd(true);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002841 }
2842 return Visit(E->getSubExpr(), asc);
2843}
2844
Zhongxing Xua725ed42010-11-01 13:04:58 +00002845CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2846 AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00002847 if (asc.alwaysAdd(*this, E)) {
Zhongxing Xua725ed42010-11-01 13:04:58 +00002848 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002849 appendStmt(Block, E);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002850
2851 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002852 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002853 }
2854 return Visit(E->getSubExpr(), asc);
2855}
2856
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002857CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2858 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002859 autoCreateBlock();
Zhongxing Xu97a72c32012-01-11 02:39:07 +00002860 appendStmt(Block, C);
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002861
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002862 return VisitChildren(C);
2863}
2864
Zhongxing Xua725ed42010-11-01 13:04:58 +00002865CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2866 AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00002867 if (asc.alwaysAdd(*this, E)) {
Zhongxing Xua725ed42010-11-01 13:04:58 +00002868 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002869 appendStmt(Block, E);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002870 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002871 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002872 }
2873 return Visit(E->getSubExpr(), asc);
2874}
2875
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002876CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2877 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002878 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002879 appendStmt(Block, C);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002880 return VisitChildren(C);
2881}
2882
Zhongxing Xua725ed42010-11-01 13:04:58 +00002883CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2884 AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00002885 if (asc.alwaysAdd(*this, E)) {
Zhongxing Xua725ed42010-11-01 13:04:58 +00002886 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002887 appendStmt(Block, E);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002888 }
Ted Kremenek892697d2010-12-16 07:46:53 +00002889 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xua725ed42010-11-01 13:04:58 +00002890}
2891
Ted Kremenek9c378f72011-08-12 23:37:29 +00002892CFGBlock *CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt *I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002893 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002894 CFGBlock *IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002895
Ted Kremenek19bb3562007-08-28 19:26:49 +00002896 if (!IBlock) {
2897 IBlock = createBlock(false);
2898 cfg->setIndirectGotoBlock(IBlock);
2899 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002900
Ted Kremenek19bb3562007-08-28 19:26:49 +00002901 // IndirectGoto is a control-flow statement. Thus we stop processing the
2902 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002903 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002904 return 0;
2905
Ted Kremenek19bb3562007-08-28 19:26:49 +00002906 Block = createBlock(false);
2907 Block->setTerminator(I);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002908 addSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00002909 return addStmt(I->getTarget());
2910}
2911
Marcin Swiderski8599e762010-11-03 06:19:35 +00002912CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2913tryAgain:
2914 if (!E) {
2915 badCFG = true;
2916 return NULL;
2917 }
2918 switch (E->getStmtClass()) {
2919 default:
2920 return VisitChildrenForTemporaryDtors(E);
2921
2922 case Stmt::BinaryOperatorClass:
2923 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2924
2925 case Stmt::CXXBindTemporaryExprClass:
2926 return VisitCXXBindTemporaryExprForTemporaryDtors(
2927 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2928
John McCall56ca35d2011-02-17 10:25:35 +00002929 case Stmt::BinaryConditionalOperatorClass:
Marcin Swiderski8599e762010-11-03 06:19:35 +00002930 case Stmt::ConditionalOperatorClass:
2931 return VisitConditionalOperatorForTemporaryDtors(
John McCall56ca35d2011-02-17 10:25:35 +00002932 cast<AbstractConditionalOperator>(E), BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002933
2934 case Stmt::ImplicitCastExprClass:
2935 // For implicit cast we want BindToTemporary to be passed further.
2936 E = cast<CastExpr>(E)->getSubExpr();
2937 goto tryAgain;
2938
2939 case Stmt::ParenExprClass:
2940 E = cast<ParenExpr>(E)->getSubExpr();
2941 goto tryAgain;
Douglas Gregor03e80032011-06-21 17:03:29 +00002942
2943 case Stmt::MaterializeTemporaryExprClass:
2944 E = cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr();
2945 goto tryAgain;
Marcin Swiderski8599e762010-11-03 06:19:35 +00002946 }
2947}
2948
2949CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2950 // When visiting children for destructors we want to visit them in reverse
2951 // order. Because there's no reverse iterator for children must to reverse
2952 // them in helper vector.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002953 typedef SmallVector<Stmt *, 4> ChildrenVect;
Marcin Swiderski8599e762010-11-03 06:19:35 +00002954 ChildrenVect ChildrenRev;
John McCall7502c1d2011-02-13 04:07:26 +00002955 for (Stmt::child_range I = E->children(); I; ++I) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002956 if (*I) ChildrenRev.push_back(*I);
2957 }
2958
2959 CFGBlock *B = Block;
2960 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2961 L = ChildrenRev.rend(); I != L; ++I) {
2962 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2963 B = R;
2964 }
2965 return B;
2966}
2967
2968CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2969 if (E->isLogicalOp()) {
2970 // Destructors for temporaries in LHS expression should be called after
2971 // those for RHS expression. Even if this will unnecessarily create a block,
2972 // this block will be used at least by the full expression.
2973 autoCreateBlock();
2974 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2975 if (badCFG)
2976 return NULL;
2977
2978 Succ = ConfluenceBlock;
2979 Block = NULL;
2980 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2981
2982 if (RHSBlock) {
2983 if (badCFG)
2984 return NULL;
2985
2986 // If RHS expression did produce destructors we need to connect created
2987 // blocks to CFG in same manner as for binary operator itself.
2988 CFGBlock *LHSBlock = createBlock(false);
2989 LHSBlock->setTerminator(CFGTerminator(E, true));
2990
2991 // For binary operator LHS block is before RHS in list of predecessors
2992 // of ConfluenceBlock.
2993 std::reverse(ConfluenceBlock->pred_begin(),
2994 ConfluenceBlock->pred_end());
2995
2996 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002997 TryResult KnownVal = tryEvaluateBool(E->getLHS());
Marcin Swiderski8599e762010-11-03 06:19:35 +00002998 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2999 KnownVal.negate();
3000
3001 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
3002 // itself.
3003 if (E->getOpcode() == BO_LOr) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003004 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
3005 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003006 } else {
3007 assert (E->getOpcode() == BO_LAnd);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003008 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
3009 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003010 }
3011
3012 Block = LHSBlock;
3013 return LHSBlock;
3014 }
3015
3016 Block = ConfluenceBlock;
3017 return ConfluenceBlock;
3018 }
3019
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003020 if (E->isAssignmentOp()) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00003021 // For assignment operator (=) LHS expression is visited
3022 // before RHS expression. For destructors visit them in reverse order.
3023 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
3024 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
3025 return LHSBlock ? LHSBlock : RHSBlock;
3026 }
3027
3028 // For any other binary operator RHS expression is visited before
3029 // LHS expression (order of children). For destructors visit them in reverse
3030 // order.
3031 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
3032 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
3033 return RHSBlock ? RHSBlock : LHSBlock;
3034}
3035
3036CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
3037 CXXBindTemporaryExpr *E, bool BindToTemporary) {
3038 // First add destructors for temporaries in subexpression.
3039 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xu249c9452010-11-14 15:23:50 +00003040 if (!BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00003041 // If lifetime of temporary is not prolonged (by assigning to constant
3042 // reference) add destructor for it.
Chandler Carruthc8cfc742011-09-13 06:09:01 +00003043
3044 // If the destructor is marked as a no-return destructor, we need to create
3045 // a new block for the destructor which does not have as a successor
3046 // anything built thus far. Control won't flow out of this block.
3047 const CXXDestructorDecl *Dtor = E->getTemporary()->getDestructor();
Chandler Carruthdba3fb52011-09-13 09:13:49 +00003048 if (cast<FunctionType>(Dtor->getType())->getNoReturnAttr())
3049 Block = createNoReturnBlock();
3050 else
Chandler Carruthc8cfc742011-09-13 06:09:01 +00003051 autoCreateBlock();
Chandler Carruthc8cfc742011-09-13 06:09:01 +00003052
Marcin Swiderski8599e762010-11-03 06:19:35 +00003053 appendTemporaryDtor(Block, E);
3054 B = Block;
3055 }
3056 return B;
3057}
3058
3059CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
John McCall56ca35d2011-02-17 10:25:35 +00003060 AbstractConditionalOperator *E, bool BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00003061 // First add destructors for condition expression. Even if this will
3062 // unnecessarily create a block, this block will be used at least by the full
3063 // expression.
3064 autoCreateBlock();
3065 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
3066 if (badCFG)
3067 return NULL;
John McCall56ca35d2011-02-17 10:25:35 +00003068 if (BinaryConditionalOperator *BCO
3069 = dyn_cast<BinaryConditionalOperator>(E)) {
3070 ConfluenceBlock = VisitForTemporaryDtors(BCO->getCommon());
Marcin Swiderski8599e762010-11-03 06:19:35 +00003071 if (badCFG)
3072 return NULL;
3073 }
3074
John McCall56ca35d2011-02-17 10:25:35 +00003075 // Try to add block with destructors for LHS expression.
3076 CFGBlock *LHSBlock = NULL;
3077 Succ = ConfluenceBlock;
3078 Block = NULL;
3079 LHSBlock = VisitForTemporaryDtors(E->getTrueExpr(), BindToTemporary);
3080 if (badCFG)
3081 return NULL;
3082
Marcin Swiderski8599e762010-11-03 06:19:35 +00003083 // Try to add block with destructors for RHS expression;
3084 Succ = ConfluenceBlock;
3085 Block = NULL;
John McCall56ca35d2011-02-17 10:25:35 +00003086 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getFalseExpr(),
3087 BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003088 if (badCFG)
3089 return NULL;
3090
3091 if (!RHSBlock && !LHSBlock) {
3092 // If neither LHS nor RHS expression had temporaries to destroy don't create
3093 // more blocks.
3094 Block = ConfluenceBlock;
3095 return Block;
3096 }
3097
3098 Block = createBlock(false);
3099 Block->setTerminator(CFGTerminator(E, true));
3100
3101 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003102 const TryResult &KnownVal = tryEvaluateBool(E->getCond());
Marcin Swiderski8599e762010-11-03 06:19:35 +00003103
3104 if (LHSBlock) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003105 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003106 } else if (KnownVal.isFalse()) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003107 addSuccessor(Block, NULL);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003108 } else {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003109 addSuccessor(Block, ConfluenceBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003110 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
3111 }
3112
3113 if (!RHSBlock)
3114 RHSBlock = ConfluenceBlock;
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003115 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003116
3117 return Block;
3118}
3119
Ted Kremenekbefef2f2007-08-23 21:26:19 +00003120} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00003121
Mike Stump6d9828c2009-07-17 01:31:16 +00003122/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
3123/// no successors or predecessors. If this is the first block created in the
3124/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003125CFGBlock *CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00003126 bool first_block = begin() == end();
3127
3128 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003129 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
Anna Zaks02f34c52011-12-05 21:33:11 +00003130 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC, this);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003131 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00003132
3133 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003134 if (first_block)
3135 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00003136
3137 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003138 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00003139}
3140
Ted Kremenek026473c2007-08-23 16:51:22 +00003141/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
3142/// CFG is returned to the caller.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003143CFG* CFG::buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +00003144 const BuildOptions &BO) {
3145 CFGBuilder Builder(C, BO);
3146 return Builder.buildCFG(D, Statement);
Ted Kremenek026473c2007-08-23 16:51:22 +00003147}
3148
Ted Kremenekc5aff442011-03-03 01:21:32 +00003149const CXXDestructorDecl *
3150CFGImplicitDtor::getDestructorDecl(ASTContext &astContext) const {
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003151 switch (getKind()) {
3152 case CFGElement::Invalid:
3153 case CFGElement::Statement:
3154 case CFGElement::Initializer:
3155 llvm_unreachable("getDestructorDecl should only be used with "
3156 "ImplicitDtors");
3157 case CFGElement::AutomaticObjectDtor: {
3158 const VarDecl *var = cast<CFGAutomaticObjDtor>(this)->getVarDecl();
3159 QualType ty = var->getType();
Ted Kremenek697d42d2011-03-03 01:01:03 +00003160 ty = ty.getNonReferenceType();
Ted Kremenek4cf22532012-03-19 23:48:41 +00003161 while (const ArrayType *arrayType = astContext.getAsArrayType(ty)) {
Ted Kremenekc5aff442011-03-03 01:21:32 +00003162 ty = arrayType->getElementType();
3163 }
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003164 const RecordType *recordType = ty->getAs<RecordType>();
3165 const CXXRecordDecl *classDecl =
Ted Kremenek697d42d2011-03-03 01:01:03 +00003166 cast<CXXRecordDecl>(recordType->getDecl());
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003167 return classDecl->getDestructor();
3168 }
3169 case CFGElement::TemporaryDtor: {
3170 const CXXBindTemporaryExpr *bindExpr =
3171 cast<CFGTemporaryDtor>(this)->getBindTemporaryExpr();
3172 const CXXTemporary *temp = bindExpr->getTemporary();
3173 return temp->getDestructor();
3174 }
3175 case CFGElement::BaseDtor:
3176 case CFGElement::MemberDtor:
3177
3178 // Not yet supported.
3179 return 0;
3180 }
Ted Kremenek697d42d2011-03-03 01:01:03 +00003181 llvm_unreachable("getKind() returned bogus value");
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003182}
3183
Ted Kremenekc5aff442011-03-03 01:21:32 +00003184bool CFGImplicitDtor::isNoReturn(ASTContext &astContext) const {
3185 if (const CXXDestructorDecl *cdecl = getDestructorDecl(astContext)) {
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003186 QualType ty = cdecl->getType();
3187 return cast<FunctionType>(ty)->getNoReturnAttr();
3188 }
3189 return false;
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003190}
3191
Ted Kremenek63f58872007-10-01 19:33:33 +00003192//===----------------------------------------------------------------------===//
3193// CFG: Queries for BlkExprs.
3194//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00003195
Ted Kremenek63f58872007-10-01 19:33:33 +00003196namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00003197 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00003198}
3199
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003200static void FindSubExprAssignments(const Stmt *S,
3201 llvm::SmallPtrSet<const Expr*,50>& Set) {
Ted Kremenek8a693662009-12-23 23:37:10 +00003202 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003203 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00003204
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003205 for (Stmt::const_child_range I = S->children(); I; ++I) {
3206 const Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00003207 if (!child)
3208 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00003209
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003210 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003211 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00003212
Ted Kremenek8a693662009-12-23 23:37:10 +00003213 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003214 }
3215}
3216
Ted Kremenek63f58872007-10-01 19:33:33 +00003217static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
3218 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00003219
3220 // Look for assignments that are used as subexpressions. These are the only
3221 // assignments that we want to *possibly* register as a block-level
3222 // expression. Basically, if an assignment occurs both in a subexpression and
3223 // at the block-level, it is a block-level expression.
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003224 llvm::SmallPtrSet<const Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00003225
Ted Kremenek63f58872007-10-01 19:33:33 +00003226 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003227 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003228 if (const CFGStmt *S = BI->getAs<CFGStmt>())
3229 FindSubExprAssignments(S->getStmt(), SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00003230
Ted Kremenek411cdee2008-04-16 21:10:48 +00003231 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003232
3233 // Iterate over the statements again on identify the Expr* and Stmt* at the
3234 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00003235
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00003236 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003237 const CFGStmt *CS = BI->getAs<CFGStmt>();
3238 if (!CS)
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00003239 continue;
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003240 if (const Expr *Exp = dyn_cast<Expr>(CS->getStmt())) {
Jordy Roseac73ea82011-06-10 08:49:37 +00003241 assert((Exp->IgnoreParens() == Exp) && "No parens on block-level exps");
Mike Stump6d9828c2009-07-17 01:31:16 +00003242
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003243 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003244 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00003245 // expression are really "statements" whose value is never used by
3246 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00003247 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003248 continue;
Ted Kremenek9c378f72011-08-12 23:37:29 +00003249 } else if (const StmtExpr *SE = dyn_cast<StmtExpr>(Exp)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003250 // Special handling for statement expressions. The last statement in
3251 // the statement expression is also a block-level expr.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003252 const CompoundStmt *C = SE->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00003253 if (!C->body_empty()) {
Jordy Roseac73ea82011-06-10 08:49:37 +00003254 const Stmt *Last = C->body_back();
3255 if (const Expr *LastEx = dyn_cast<Expr>(Last))
3256 Last = LastEx->IgnoreParens();
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003257 unsigned x = M->size();
Jordy Roseac73ea82011-06-10 08:49:37 +00003258 (*M)[Last] = x;
Ted Kremenek86946742008-01-17 20:48:37 +00003259 }
3260 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00003261
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003262 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00003263 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003264 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00003265 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003266
Ted Kremenek411cdee2008-04-16 21:10:48 +00003267 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00003268
Ted Kremenek9c378f72011-08-12 23:37:29 +00003269 Stmt *S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00003270
Ted Kremenek390e48b2008-11-12 21:11:49 +00003271 if (S && M->find(S) == M->end()) {
Jordy Roseac73ea82011-06-10 08:49:37 +00003272 unsigned x = M->size();
3273 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003274 }
3275 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003276
Ted Kremenek63f58872007-10-01 19:33:33 +00003277 return M;
3278}
3279
Ted Kremenek9c378f72011-08-12 23:37:29 +00003280CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt *S) {
Ted Kremenek86946742008-01-17 20:48:37 +00003281 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00003282 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00003283
Ted Kremenek63f58872007-10-01 19:33:33 +00003284 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00003285 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003286 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00003287}
3288
3289unsigned CFG::getNumBlkExprs() {
3290 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
3291 return M->size();
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003292
3293 // We assume callers interested in the number of BlkExprs will want
3294 // the map constructed if it doesn't already exist.
3295 BlkExprMap = (void*) PopulateBlkExprMap(*this);
3296 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenek63f58872007-10-01 19:33:33 +00003297}
3298
Ted Kremenek274f4332008-04-28 18:00:46 +00003299//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003300// Filtered walking of the CFG.
3301//===----------------------------------------------------------------------===//
3302
3303bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00003304 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003305
Ted Kremenek6e400352011-03-07 22:04:39 +00003306 if (To && F.IgnoreDefaultsWithCoveredEnums) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003307 // If the 'To' has no label or is labeled but the label isn't a
3308 // CaseStmt then filter this edge.
3309 if (const SwitchStmt *S =
Ted Kremenek6e400352011-03-07 22:04:39 +00003310 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003311 if (S->isAllEnumCasesCovered()) {
Ted Kremenek6e400352011-03-07 22:04:39 +00003312 const Stmt *L = To->getLabel();
3313 if (!L || !isa<CaseStmt>(L))
3314 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003315 }
3316 }
3317 }
3318
3319 return false;
3320}
3321
3322//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00003323// Cleanup: CFG dstor.
3324//===----------------------------------------------------------------------===//
3325
Ted Kremenek63f58872007-10-01 19:33:33 +00003326CFG::~CFG() {
3327 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
3328}
Mike Stump6d9828c2009-07-17 01:31:16 +00003329
Ted Kremenek7dba8602007-08-29 21:56:09 +00003330//===----------------------------------------------------------------------===//
3331// CFG pretty printing
3332//===----------------------------------------------------------------------===//
3333
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00003334namespace {
3335
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00003336class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003337 typedef llvm::DenseMap<const Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
3338 typedef llvm::DenseMap<const Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003339 StmtMapTy StmtMap;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003340 DeclMapTy DeclMap;
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003341 signed currentBlock;
3342 unsigned currentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00003343 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003344public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003345
Chris Lattnere4f21422009-06-30 01:26:17 +00003346 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003347 : currentBlock(0), currentStmt(0), LangOpts(LO)
3348 {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003349 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
3350 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003351 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003352 BI != BEnd; ++BI, ++j ) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003353 if (const CFGStmt *SE = BI->getAs<CFGStmt>()) {
3354 const Stmt *stmt= SE->getStmt();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003355 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003356 StmtMap[stmt] = P;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003357
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003358 switch (stmt->getStmtClass()) {
3359 case Stmt::DeclStmtClass:
3360 DeclMap[cast<DeclStmt>(stmt)->getSingleDecl()] = P;
3361 break;
3362 case Stmt::IfStmtClass: {
3363 const VarDecl *var = cast<IfStmt>(stmt)->getConditionVariable();
3364 if (var)
3365 DeclMap[var] = P;
3366 break;
3367 }
3368 case Stmt::ForStmtClass: {
3369 const VarDecl *var = cast<ForStmt>(stmt)->getConditionVariable();
3370 if (var)
3371 DeclMap[var] = P;
3372 break;
3373 }
3374 case Stmt::WhileStmtClass: {
3375 const VarDecl *var =
3376 cast<WhileStmt>(stmt)->getConditionVariable();
3377 if (var)
3378 DeclMap[var] = P;
3379 break;
3380 }
3381 case Stmt::SwitchStmtClass: {
3382 const VarDecl *var =
3383 cast<SwitchStmt>(stmt)->getConditionVariable();
3384 if (var)
3385 DeclMap[var] = P;
3386 break;
3387 }
3388 case Stmt::CXXCatchStmtClass: {
3389 const VarDecl *var =
3390 cast<CXXCatchStmt>(stmt)->getExceptionDecl();
3391 if (var)
3392 DeclMap[var] = P;
3393 break;
3394 }
3395 default:
3396 break;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003397 }
3398 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003399 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00003400 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003401 }
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003402
Mike Stump6d9828c2009-07-17 01:31:16 +00003403
Ted Kremenek42a509f2007-08-31 21:30:12 +00003404 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00003405
Chris Lattnere4f21422009-06-30 01:26:17 +00003406 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003407 void setBlockID(signed i) { currentBlock = i; }
3408 void setStmtID(unsigned i) { currentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00003409
Ted Kremenek9c378f72011-08-12 23:37:29 +00003410 virtual bool handledStmt(Stmt *S, raw_ostream &OS) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003411 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003412
3413 if (I == StmtMap.end())
3414 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003415
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003416 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3417 && I->second.second == currentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003418 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003419 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003420
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003421 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003422 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003423 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003424
Ted Kremenek9c378f72011-08-12 23:37:29 +00003425 bool handleDecl(const Decl *D, raw_ostream &OS) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003426 DeclMapTy::iterator I = DeclMap.find(D);
3427
3428 if (I == DeclMap.end())
3429 return false;
3430
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003431 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3432 && I->second.second == currentStmt) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003433 return false;
3434 }
3435
3436 OS << "[B" << I->second.first << "." << I->second.second << "]";
3437 return true;
3438 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003439};
Chris Lattnere4f21422009-06-30 01:26:17 +00003440} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00003441
Chris Lattnere4f21422009-06-30 01:26:17 +00003442
3443namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00003444class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00003445 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00003446
Ted Kremenek9c378f72011-08-12 23:37:29 +00003447 raw_ostream &OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003448 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003449 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003450public:
Ted Kremenek9c378f72011-08-12 23:37:29 +00003451 CFGBlockTerminatorPrint(raw_ostream &os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00003452 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003453 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00003454
Ted Kremenek9c378f72011-08-12 23:37:29 +00003455 void VisitIfStmt(IfStmt *I) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003456 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003457 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003458 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003459
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003460 // Default case.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003461 void VisitStmt(Stmt *Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003462 Terminator->printPretty(OS, Helper, Policy);
3463 }
3464
Ted Kremenek9c378f72011-08-12 23:37:29 +00003465 void VisitForStmt(ForStmt *F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003466 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003467 if (F->getInit())
3468 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00003469 OS << "; ";
Ted Kremenek9c378f72011-08-12 23:37:29 +00003470 if (Stmt *C = F->getCond())
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003471 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00003472 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003473 if (F->getInc())
3474 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00003475 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003476 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003477
Ted Kremenek9c378f72011-08-12 23:37:29 +00003478 void VisitWhileStmt(WhileStmt *W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003479 OS << "while " ;
Ted Kremenek9c378f72011-08-12 23:37:29 +00003480 if (Stmt *C = W->getCond())
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003481 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003482 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003483
Ted Kremenek9c378f72011-08-12 23:37:29 +00003484 void VisitDoStmt(DoStmt *D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003485 OS << "do ... while ";
Ted Kremenek9c378f72011-08-12 23:37:29 +00003486 if (Stmt *C = D->getCond())
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003487 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00003488 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003489
Ted Kremenek9c378f72011-08-12 23:37:29 +00003490 void VisitSwitchStmt(SwitchStmt *Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00003491 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003492 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00003493 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003494
Ted Kremenek9c378f72011-08-12 23:37:29 +00003495 void VisitCXXTryStmt(CXXTryStmt *CS) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003496 OS << "try ...";
3497 }
3498
John McCall56ca35d2011-02-17 10:25:35 +00003499 void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003500 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003501 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003502 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003503
Ted Kremenek9c378f72011-08-12 23:37:29 +00003504 void VisitChooseExpr(ChooseExpr *C) {
Ted Kremenekaeddbf62007-08-31 22:29:13 +00003505 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003506 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00003507 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00003508 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003509
Ted Kremenek9c378f72011-08-12 23:37:29 +00003510 void VisitIndirectGotoStmt(IndirectGotoStmt *I) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003511 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003512 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003513 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003514
Ted Kremenek805e9a82007-08-31 21:49:40 +00003515 void VisitBinaryOperator(BinaryOperator* B) {
3516 if (!B->isLogicalOp()) {
3517 VisitExpr(B);
3518 return;
3519 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003520
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003521 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003522
Ted Kremenek805e9a82007-08-31 21:49:40 +00003523 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00003524 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00003525 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003526 return;
John McCall2de56d12010-08-25 11:45:40 +00003527 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00003528 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003529 return;
3530 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003531 llvm_unreachable("Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003532 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00003533 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003534
Ted Kremenek9c378f72011-08-12 23:37:29 +00003535 void VisitExpr(Expr *E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003536 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003537 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003538};
Chris Lattnere4f21422009-06-30 01:26:17 +00003539} // end anonymous namespace
3540
Chris Lattner5f9e2722011-07-23 10:55:15 +00003541static void print_elem(raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00003542 const CFGElement &E) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003543 if (const CFGStmt *CS = E.getAs<CFGStmt>()) {
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003544 const Stmt *S = CS->getStmt();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003545
3546 if (Helper) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003547
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003548 // special printing for statement-expressions.
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003549 if (const StmtExpr *SE = dyn_cast<StmtExpr>(S)) {
3550 const CompoundStmt *Sub = SE->getSubStmt();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003551
John McCall7502c1d2011-02-13 04:07:26 +00003552 if (Sub->children()) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003553 OS << "({ ... ; ";
3554 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3555 OS << " })\n";
3556 return;
3557 }
3558 }
3559 // special printing for comma expressions.
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003560 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003561 if (B->getOpcode() == BO_Comma) {
3562 OS << "... , ";
3563 Helper->handledStmt(B->getRHS(),OS);
3564 OS << '\n';
3565 return;
3566 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003567 }
3568 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003569 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00003570
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003571 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003572 OS << " (OperatorCall)";
Ted Kremenek893d4142011-12-21 19:32:38 +00003573 }
3574 else if (isa<CXXBindTemporaryExpr>(S)) {
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003575 OS << " (BindTemporary)";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003576 }
Ted Kremenek3b7a48f2011-12-21 19:39:59 +00003577 else if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(S)) {
3578 OS << " (CXXConstructExpr, " << CCE->getType().getAsString() << ")";
3579 }
Ted Kremenek893d4142011-12-21 19:32:38 +00003580 else if (const CastExpr *CE = dyn_cast<CastExpr>(S)) {
3581 OS << " (" << CE->getStmtClassName() << ", "
3582 << CE->getCastKindName()
3583 << ", " << CE->getType().getAsString()
3584 << ")";
3585 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003586
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003587 // Expressions need a newline.
3588 if (isa<Expr>(S))
3589 OS << '\n';
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00003590
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003591 } else if (const CFGInitializer *IE = E.getAs<CFGInitializer>()) {
3592 const CXXCtorInitializer *I = IE->getInitializer();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003593 if (I->isBaseInitializer())
3594 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichet00eb3f92010-12-04 09:14:42 +00003595 else OS << I->getAnyMember()->getName();
Mike Stump6d9828c2009-07-17 01:31:16 +00003596
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003597 OS << "(";
Ted Kremenek9c378f72011-08-12 23:37:29 +00003598 if (Expr *IE = I->getInit())
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003599 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3600 OS << ")";
3601
3602 if (I->isBaseInitializer())
3603 OS << " (Base initializer)\n";
3604 else OS << " (Member initializer)\n";
3605
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003606 } else if (const CFGAutomaticObjDtor *DE = E.getAs<CFGAutomaticObjDtor>()){
Ted Kremenek9c378f72011-08-12 23:37:29 +00003607 const VarDecl *VD = DE->getVarDecl();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003608 Helper->handleDecl(VD, OS);
3609
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003610 const Type* T = VD->getType().getTypePtr();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003611 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3612 T = RT->getPointeeType().getTypePtr();
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003613 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3614 T = ET;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003615
3616 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3617 OS << " (Implicit destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003618
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003619 } else if (const CFGBaseDtor *BE = E.getAs<CFGBaseDtor>()) {
3620 const CXXBaseSpecifier *BS = BE->getBaseSpecifier();
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003621 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003622 OS << " (Base object destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003623
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003624 } else if (const CFGMemberDtor *ME = E.getAs<CFGMemberDtor>()) {
3625 const FieldDecl *FD = ME->getFieldDecl();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003626
3627 const Type *T = FD->getType().getTypePtr();
3628 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3629 T = ET;
3630
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003631 OS << "this->" << FD->getName();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003632 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003633 OS << " (Member object destructor)\n";
Marcin Swiderski8599e762010-11-03 06:19:35 +00003634
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003635 } else if (const CFGTemporaryDtor *TE = E.getAs<CFGTemporaryDtor>()) {
3636 const CXXBindTemporaryExpr *BT = TE->getBindTemporaryExpr();
Marcin Swiderski8599e762010-11-03 06:19:35 +00003637 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3638 OS << " (Temporary object destructor)\n";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003639 }
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00003640}
Mike Stump6d9828c2009-07-17 01:31:16 +00003641
Ted Kremenek9c378f72011-08-12 23:37:29 +00003642static void print_block(raw_ostream &OS, const CFG* cfg,
3643 const CFGBlock &B,
Ted Kremenek682060c2011-12-22 23:33:52 +00003644 StmtPrinterHelper* Helper, bool print_edges,
3645 bool ShowColors) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003646
Ted Kremenek682060c2011-12-22 23:33:52 +00003647 if (Helper)
3648 Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00003649
Ted Kremenek7dba8602007-08-29 21:56:09 +00003650 // Print the header.
Ted Kremenek682060c2011-12-22 23:33:52 +00003651 if (ShowColors)
3652 OS.changeColor(raw_ostream::YELLOW, true);
3653
3654 OS << "\n [B" << B.getBlockID();
Mike Stump6d9828c2009-07-17 01:31:16 +00003655
Ted Kremenek42a509f2007-08-31 21:30:12 +00003656 if (&B == &cfg->getEntry())
Ted Kremenek682060c2011-12-22 23:33:52 +00003657 OS << " (ENTRY)]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003658 else if (&B == &cfg->getExit())
Ted Kremenek682060c2011-12-22 23:33:52 +00003659 OS << " (EXIT)]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003660 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek682060c2011-12-22 23:33:52 +00003661 OS << " (INDIRECT GOTO DISPATCH)]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003662 else
Ted Kremenek682060c2011-12-22 23:33:52 +00003663 OS << "]\n";
3664
3665 if (ShowColors)
3666 OS.resetColor();
Mike Stump6d9828c2009-07-17 01:31:16 +00003667
Ted Kremenek9cffe732007-08-29 23:20:49 +00003668 // Print the label of this block.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003669 if (Stmt *Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003670
3671 if (print_edges)
Ted Kremenek682060c2011-12-22 23:33:52 +00003672 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003673
Ted Kremenek9c378f72011-08-12 23:37:29 +00003674 if (LabelStmt *L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003675 OS << L->getName();
Ted Kremenek9c378f72011-08-12 23:37:29 +00003676 else if (CaseStmt *C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00003677 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003678 C->getLHS()->printPretty(OS, Helper,
3679 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003680 if (C->getRHS()) {
3681 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003682 C->getRHS()->printPretty(OS, Helper,
3683 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003684 }
Mike Stump079bd722010-01-19 22:00:14 +00003685 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003686 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00003687 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003688 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00003689 if (CS->getExceptionDecl())
3690 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3691 0);
3692 else
3693 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00003694 OS << ")";
3695
3696 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00003697 llvm_unreachable("Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003698
Ted Kremenek9cffe732007-08-29 23:20:49 +00003699 OS << ":\n";
3700 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003701
Ted Kremenekfddd5182007-08-21 21:42:03 +00003702 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00003703 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00003704
Ted Kremenek42a509f2007-08-31 21:30:12 +00003705 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3706 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003707
Ted Kremenek9cffe732007-08-29 23:20:49 +00003708 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003709 if (print_edges)
Ted Kremenek682060c2011-12-22 23:33:52 +00003710 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003711
Ted Kremeneka95d3752008-09-13 05:16:45 +00003712 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003713
Ted Kremenek42a509f2007-08-31 21:30:12 +00003714 if (Helper)
3715 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00003716
Ted Kremenek682060c2011-12-22 23:33:52 +00003717 print_elem(OS, Helper, *I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00003718 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003719
Ted Kremenek9cffe732007-08-29 23:20:49 +00003720 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003721 if (B.getTerminator()) {
Ted Kremenek682060c2011-12-22 23:33:52 +00003722 if (ShowColors)
3723 OS.changeColor(raw_ostream::GREEN);
Mike Stump6d9828c2009-07-17 01:31:16 +00003724
Ted Kremenek682060c2011-12-22 23:33:52 +00003725 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003726
Ted Kremenek42a509f2007-08-31 21:30:12 +00003727 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00003728
Chris Lattnere4f21422009-06-30 01:26:17 +00003729 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3730 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003731 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003732 OS << '\n';
Ted Kremenek682060c2011-12-22 23:33:52 +00003733
3734 if (ShowColors)
3735 OS.resetColor();
Ted Kremenekfddd5182007-08-21 21:42:03 +00003736 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003737
Ted Kremenek9cffe732007-08-29 23:20:49 +00003738 if (print_edges) {
3739 // Print the predecessors of this block.
Ted Kremenek682060c2011-12-22 23:33:52 +00003740 if (!B.pred_empty()) {
3741 const raw_ostream::Colors Color = raw_ostream::BLUE;
3742 if (ShowColors)
3743 OS.changeColor(Color);
3744 OS << " Preds " ;
3745 if (ShowColors)
3746 OS.resetColor();
3747 OS << '(' << B.pred_size() << "):";
3748 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00003749
Ted Kremenek682060c2011-12-22 23:33:52 +00003750 if (ShowColors)
3751 OS.changeColor(Color);
3752
3753 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3754 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003755
Ted Kremenek682060c2011-12-22 23:33:52 +00003756 if (i == 8 || (i-8) == 0)
3757 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003758
Ted Kremenek682060c2011-12-22 23:33:52 +00003759 OS << " B" << (*I)->getBlockID();
3760 }
3761
3762 if (ShowColors)
3763 OS.resetColor();
3764
3765 OS << '\n';
Ted Kremenek9cffe732007-08-29 23:20:49 +00003766 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003767
Ted Kremenek42a509f2007-08-31 21:30:12 +00003768 // Print the successors of this block.
Ted Kremenek682060c2011-12-22 23:33:52 +00003769 if (!B.succ_empty()) {
3770 const raw_ostream::Colors Color = raw_ostream::MAGENTA;
3771 if (ShowColors)
3772 OS.changeColor(Color);
3773 OS << " Succs ";
3774 if (ShowColors)
3775 OS.resetColor();
3776 OS << '(' << B.succ_size() << "):";
3777 unsigned i = 0;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003778
Ted Kremenek682060c2011-12-22 23:33:52 +00003779 if (ShowColors)
3780 OS.changeColor(Color);
Mike Stump6d9828c2009-07-17 01:31:16 +00003781
Ted Kremenek682060c2011-12-22 23:33:52 +00003782 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3783 I != E; ++I, ++i) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003784
Ted Kremenek682060c2011-12-22 23:33:52 +00003785 if (i == 8 || (i-8) % 10 == 0)
3786 OS << "\n ";
3787
3788 if (*I)
3789 OS << " B" << (*I)->getBlockID();
3790 else
3791 OS << " NULL";
3792 }
3793
3794 if (ShowColors)
3795 OS.resetColor();
3796 OS << '\n';
Ted Kremenek42a509f2007-08-31 21:30:12 +00003797 }
Ted Kremenekfddd5182007-08-21 21:42:03 +00003798 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003799}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003800
Ted Kremenek42a509f2007-08-31 21:30:12 +00003801
3802/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek682060c2011-12-22 23:33:52 +00003803void CFG::dump(const LangOptions &LO, bool ShowColors) const {
3804 print(llvm::errs(), LO, ShowColors);
3805}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003806
3807/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenek682060c2011-12-22 23:33:52 +00003808void CFG::print(raw_ostream &OS, const LangOptions &LO, bool ShowColors) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003809 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00003810
Ted Kremenek42a509f2007-08-31 21:30:12 +00003811 // Print the entry block.
Ted Kremenek682060c2011-12-22 23:33:52 +00003812 print_block(OS, this, getEntry(), &Helper, true, ShowColors);
Mike Stump6d9828c2009-07-17 01:31:16 +00003813
Ted Kremenek42a509f2007-08-31 21:30:12 +00003814 // Iterate through the CFGBlocks and print them one by one.
3815 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3816 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003817 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00003818 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00003819
Ted Kremenek682060c2011-12-22 23:33:52 +00003820 print_block(OS, this, **I, &Helper, true, ShowColors);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003821 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003822
Ted Kremenek42a509f2007-08-31 21:30:12 +00003823 // Print the exit block.
Ted Kremenek682060c2011-12-22 23:33:52 +00003824 print_block(OS, this, getExit(), &Helper, true, ShowColors);
3825 OS << '\n';
Ted Kremenekd0172432008-11-24 20:50:24 +00003826 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00003827}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003828
3829/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek682060c2011-12-22 23:33:52 +00003830void CFGBlock::dump(const CFG* cfg, const LangOptions &LO,
3831 bool ShowColors) const {
3832 print(llvm::errs(), cfg, LO, ShowColors);
Chris Lattnere4f21422009-06-30 01:26:17 +00003833}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003834
3835/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3836/// Generally this will only be called from CFG::print.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003837void CFGBlock::print(raw_ostream &OS, const CFG* cfg,
Ted Kremenek682060c2011-12-22 23:33:52 +00003838 const LangOptions &LO, bool ShowColors) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003839 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek682060c2011-12-22 23:33:52 +00003840 print_block(OS, cfg, *this, &Helper, true, ShowColors);
3841 OS << '\n';
Ted Kremenek026473c2007-08-23 16:51:22 +00003842}
Ted Kremenek7dba8602007-08-29 21:56:09 +00003843
Ted Kremeneka2925852008-01-30 23:02:42 +00003844/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003845void CFGBlock::printTerminator(raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00003846 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003847 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003848 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003849}
3850
Ted Kremenek9c378f72011-08-12 23:37:29 +00003851Stmt *CFGBlock::getTerminatorCondition() {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003852 Stmt *Terminator = this->Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003853 if (!Terminator)
3854 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003855
Ted Kremenek9c378f72011-08-12 23:37:29 +00003856 Expr *E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003857
Ted Kremenek411cdee2008-04-16 21:10:48 +00003858 switch (Terminator->getStmtClass()) {
3859 default:
3860 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003861
Ted Kremenek411cdee2008-04-16 21:10:48 +00003862 case Stmt::ForStmtClass:
3863 E = cast<ForStmt>(Terminator)->getCond();
3864 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003865
Ted Kremenek411cdee2008-04-16 21:10:48 +00003866 case Stmt::WhileStmtClass:
3867 E = cast<WhileStmt>(Terminator)->getCond();
3868 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003869
Ted Kremenek411cdee2008-04-16 21:10:48 +00003870 case Stmt::DoStmtClass:
3871 E = cast<DoStmt>(Terminator)->getCond();
3872 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003873
Ted Kremenek411cdee2008-04-16 21:10:48 +00003874 case Stmt::IfStmtClass:
3875 E = cast<IfStmt>(Terminator)->getCond();
3876 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003877
Ted Kremenek411cdee2008-04-16 21:10:48 +00003878 case Stmt::ChooseExprClass:
3879 E = cast<ChooseExpr>(Terminator)->getCond();
3880 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003881
Ted Kremenek411cdee2008-04-16 21:10:48 +00003882 case Stmt::IndirectGotoStmtClass:
3883 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3884 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003885
Ted Kremenek411cdee2008-04-16 21:10:48 +00003886 case Stmt::SwitchStmtClass:
3887 E = cast<SwitchStmt>(Terminator)->getCond();
3888 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003889
John McCall56ca35d2011-02-17 10:25:35 +00003890 case Stmt::BinaryConditionalOperatorClass:
3891 E = cast<BinaryConditionalOperator>(Terminator)->getCond();
3892 break;
3893
Ted Kremenek411cdee2008-04-16 21:10:48 +00003894 case Stmt::ConditionalOperatorClass:
3895 E = cast<ConditionalOperator>(Terminator)->getCond();
3896 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003897
Ted Kremenek411cdee2008-04-16 21:10:48 +00003898 case Stmt::BinaryOperatorClass: // '&&' and '||'
3899 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00003900 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003901
Ted Kremenek390e48b2008-11-12 21:11:49 +00003902 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003903 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003904 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003905
Ted Kremenek411cdee2008-04-16 21:10:48 +00003906 return E ? E->IgnoreParens() : NULL;
3907}
3908
Ted Kremenek7dba8602007-08-29 21:56:09 +00003909//===----------------------------------------------------------------------===//
3910// CFG Graphviz Visualization
3911//===----------------------------------------------------------------------===//
3912
Ted Kremenek42a509f2007-08-31 21:30:12 +00003913
3914#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00003915static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003916#endif
3917
Chris Lattnere4f21422009-06-30 01:26:17 +00003918void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003919#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00003920 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003921 GraphHelper = &H;
3922 llvm::ViewGraph(this,"CFG");
3923 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003924#endif
3925}
3926
Ted Kremenek7dba8602007-08-29 21:56:09 +00003927namespace llvm {
3928template<>
3929struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00003930
3931 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3932
Ted Kremenek9c378f72011-08-12 23:37:29 +00003933 static std::string getNodeLabel(const CFGBlock *Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00003934
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003935#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00003936 std::string OutSStr;
3937 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek682060c2011-12-22 23:33:52 +00003938 print_block(Out,Graph, *Node, GraphHelper, false, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003939 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00003940
3941 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3942
3943 // Process string output to make it nicer...
3944 for (unsigned i = 0; i != OutStr.length(); ++i)
3945 if (OutStr[i] == '\n') { // Left justify
3946 OutStr[i] = '\\';
3947 OutStr.insert(OutStr.begin()+i+1, 'l');
3948 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003949
Ted Kremenek7dba8602007-08-29 21:56:09 +00003950 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003951#else
3952 return "";
3953#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00003954 }
3955};
3956} // end namespace llvm