blob: b69fbd6afb61b808d45887a273ba17371d8f01d4 [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"
Benjamin Kramer478851c2012-07-04 17:04:04 +000017#include "clang/AST/ASTContext.h"
Mike Stumpb978a442010-01-21 02:21:40 +000018#include "clang/AST/DeclCXX.h"
Ted Kremenekc310e932007-08-21 22:06:14 +000019#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000020#include "clang/AST/PrettyPrinter.h"
Ted Kremenekc56c0042011-02-23 05:11:46 +000021#include "clang/AST/CharUnits.h"
Richard Smith534986f2012-04-14 00:33:13 +000022#include "clang/Basic/AttrKinds.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000023#include "llvm/Support/GraphWriter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000024#include "llvm/Support/Allocator.h"
25#include "llvm/Support/Format.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000026#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000027#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek0ba497b2009-10-20 23:46:25 +000028#include "llvm/ADT/OwningPtr.h"
Ted Kremenek83c01da2008-01-11 00:40:29 +000029
Ted Kremenekfddd5182007-08-21 21:42:03 +000030using namespace clang;
31
32namespace {
33
Ted Kremenek9c378f72011-08-12 23:37:29 +000034static SourceLocation GetEndLoc(Decl *D) {
35 if (VarDecl *VD = dyn_cast<VarDecl>(D))
36 if (Expr *Ex = VD->getInit())
Ted Kremenekc7eb9032008-08-06 23:20:50 +000037 return Ex->getSourceRange().getEnd();
Mike Stump6d9828c2009-07-17 01:31:16 +000038 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000039}
Ted Kremenekad5a8942010-08-02 23:46:59 +000040
Ted Kremenek3179a452011-03-10 01:14:11 +000041class CFGBuilder;
42
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000043/// The CFG builder uses a recursive algorithm to build the CFG. When
44/// we process an expression, sometimes we know that we must add the
45/// subexpressions as block-level expressions. For example:
46///
47/// exp1 || exp2
48///
49/// When processing the '||' expression, we know that exp1 and exp2
50/// need to be added as block-level expressions, even though they
51/// might not normally need to be. AddStmtChoice records this
52/// contextual information. If AddStmtChoice is 'NotAlwaysAdd', then
53/// the builder has an option not to add a subexpression as a
54/// block-level expression.
55///
Ted Kremenek852274d2009-12-16 03:18:58 +000056class AddStmtChoice {
57public:
Ted Kremenek892697d2010-12-16 07:46:53 +000058 enum Kind { NotAlwaysAdd = 0, AlwaysAdd = 1 };
Ted Kremenek5ba290a2010-03-02 21:43:54 +000059
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000060 AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {}
Ted Kremenek5ba290a2010-03-02 21:43:54 +000061
Ted Kremenek3179a452011-03-10 01:14:11 +000062 bool alwaysAdd(CFGBuilder &builder,
63 const Stmt *stmt) const;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000064
65 /// Return a copy of this object, except with the 'always-add' bit
66 /// set as specified.
67 AddStmtChoice withAlwaysAdd(bool alwaysAdd) const {
Ted Kremenek3179a452011-03-10 01:14:11 +000068 return AddStmtChoice(alwaysAdd ? AlwaysAdd : NotAlwaysAdd);
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000069 }
70
Ted Kremenek852274d2009-12-16 03:18:58 +000071private:
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000072 Kind kind;
Ted Kremenek852274d2009-12-16 03:18:58 +000073};
Mike Stump6d9828c2009-07-17 01:31:16 +000074
Marcin Swiderskif1308c72010-09-25 11:05:21 +000075/// LocalScope - Node in tree of local scopes created for C++ implicit
76/// destructor calls generation. It contains list of automatic variables
77/// declared in the scope and link to position in previous scope this scope
78/// began in.
79///
80/// The process of creating local scopes is as follows:
81/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
82/// - Before processing statements in scope (e.g. CompoundStmt) create
83/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
84/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderski35387a02010-09-30 22:42:32 +000085/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderskif1308c72010-09-25 11:05:21 +000086/// at this VarDecl,
87/// - For every normal (without jump) end of scope add to CFGBlock destructors
88/// for objects in the current scope,
89/// - For every jump add to CFGBlock destructors for objects
90/// between CFGBuilder::ScopePos and local scope position saved for jump
91/// target. Thanks to C++ restrictions on goto jumps we can be sure that
92/// jump target position will be on the path to root from CFGBuilder::ScopePos
93/// (adding any variable that doesn't need constructor to be called to
94/// LocalScope can break this assumption),
95///
96class LocalScope {
97public:
Ted Kremenekfe59b742011-02-15 02:47:45 +000098 typedef BumpVector<VarDecl*> AutomaticVarsTy;
Marcin Swiderskif1308c72010-09-25 11:05:21 +000099
100 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderski35387a02010-09-30 22:42:32 +0000101 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000102 class const_iterator {
103 const LocalScope* Scope;
104
105 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
106 /// Invalid iterator (with null Scope) has VarIter equal to 0.
107 unsigned VarIter;
108
109 public:
110 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
111 /// Incrementing invalid iterator is allowed and will result in invalid
112 /// iterator.
113 const_iterator()
114 : Scope(NULL), VarIter(0) {}
115
116 /// Create valid iterator. In case when S.Prev is an invalid iterator and
117 /// I is equal to 0, this will create invalid iterator.
118 const_iterator(const LocalScope& S, unsigned I)
119 : Scope(&S), VarIter(I) {
120 // Iterator to "end" of scope is not allowed. Handle it by going up
121 // in scopes tree possibly up to invalid iterator in the root.
122 if (VarIter == 0 && Scope)
123 *this = Scope->Prev;
124 }
125
Ted Kremenek9c378f72011-08-12 23:37:29 +0000126 VarDecl *const* operator->() const {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000127 assert (Scope && "Dereferencing invalid iterator is not allowed");
128 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
129 return &Scope->Vars[VarIter - 1];
130 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000131 VarDecl *operator*() const {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000132 return *this->operator->();
133 }
134
Ted Kremenek9c378f72011-08-12 23:37:29 +0000135 const_iterator &operator++() {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000136 if (!Scope)
137 return *this;
138
139 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
140 --VarIter;
141 if (VarIter == 0)
142 *this = Scope->Prev;
143 return *this;
144 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000145 const_iterator operator++(int) {
146 const_iterator P = *this;
147 ++*this;
148 return P;
149 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000150
Ted Kremenek9c378f72011-08-12 23:37:29 +0000151 bool operator==(const const_iterator &rhs) const {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000152 return Scope == rhs.Scope && VarIter == rhs.VarIter;
153 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000154 bool operator!=(const const_iterator &rhs) const {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000155 return !(*this == rhs);
156 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000157
158 operator bool() const {
159 return *this != const_iterator();
160 }
161
162 int distance(const_iterator L);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000163 };
164
165 friend class const_iterator;
166
167private:
Ted Kremenekfe59b742011-02-15 02:47:45 +0000168 BumpVectorContext ctx;
169
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000170 /// Automatic variables in order of declaration.
171 AutomaticVarsTy Vars;
172 /// Iterator to variable in previous scope that was declared just before
173 /// begin of this scope.
174 const_iterator Prev;
175
176public:
177 /// Constructs empty scope linked to previous scope in specified place.
Ted Kremenekfe59b742011-02-15 02:47:45 +0000178 LocalScope(BumpVectorContext &ctx, const_iterator P)
179 : ctx(ctx), Vars(ctx, 4), Prev(P) {}
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000180
181 /// Begin of scope in direction of CFG building (backwards).
182 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000183
Ted Kremenek9c378f72011-08-12 23:37:29 +0000184 void addVar(VarDecl *VD) {
Ted Kremenekfe59b742011-02-15 02:47:45 +0000185 Vars.push_back(VD, ctx);
Marcin Swiderski35387a02010-09-30 22:42:32 +0000186 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000187};
188
Marcin Swiderski35387a02010-09-30 22:42:32 +0000189/// distance - Calculates distance from this to L. L must be reachable from this
190/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
191/// number of scopes between this and L.
192int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
193 int D = 0;
194 const_iterator F = *this;
195 while (F.Scope != L.Scope) {
Ted Kremenek5290c802011-08-12 14:41:23 +0000196 assert (F != const_iterator()
197 && "L iterator is not reachable from F iterator.");
Marcin Swiderski35387a02010-09-30 22:42:32 +0000198 D += F.VarIter;
199 F = F.Scope->Prev;
200 }
201 D += F.VarIter - L.VarIter;
202 return D;
203}
204
205/// BlockScopePosPair - Structure for specifying position in CFG during its
206/// build process. It consists of CFGBlock that specifies position in CFG graph
207/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000208struct BlockScopePosPair {
Ted Kremenek9ce52702011-01-07 19:37:16 +0000209 BlockScopePosPair() : block(0) {}
Ted Kremenek9c378f72011-08-12 23:37:29 +0000210 BlockScopePosPair(CFGBlock *b, LocalScope::const_iterator scopePos)
Ted Kremenek9ce52702011-01-07 19:37:16 +0000211 : block(b), scopePosition(scopePos) {}
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000212
Ted Kremenek9ce52702011-01-07 19:37:16 +0000213 CFGBlock *block;
214 LocalScope::const_iterator scopePosition;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000215};
216
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000217/// TryResult - a class representing a variant over the values
218/// 'true', 'false', or 'unknown'. This is returned by tryEvaluateBool,
219/// and is used by the CFGBuilder to decide if a branch condition
220/// can be decided up front during CFG construction.
221class TryResult {
222 int X;
223public:
224 TryResult(bool b) : X(b ? 1 : 0) {}
225 TryResult() : X(-1) {}
226
227 bool isTrue() const { return X == 1; }
228 bool isFalse() const { return X == 0; }
229 bool isKnown() const { return X >= 0; }
230 void negate() {
231 assert(isKnown());
232 X ^= 0x1;
233 }
234};
235
Ted Kremeneka34ea072008-08-04 22:51:42 +0000236/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +0000237/// The builder is stateful: an instance of the builder should be used to only
238/// construct a single CFG.
239///
240/// Example usage:
241///
242/// CFGBuilder builder;
243/// CFG* cfg = builder.BuildAST(stmt1);
244///
Mike Stump6d9828c2009-07-17 01:31:16 +0000245/// CFG construction is done via a recursive walk of an AST. We actually parse
246/// the AST in reverse order so that the successor of a basic block is
247/// constructed prior to its predecessor. This allows us to nicely capture
248/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +0000249///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000250class CFGBuilder {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000251 typedef BlockScopePosPair JumpTarget;
252 typedef BlockScopePosPair JumpSource;
253
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000254 ASTContext *Context;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000255 OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000256
Ted Kremenek9c378f72011-08-12 23:37:29 +0000257 CFGBlock *Block;
258 CFGBlock *Succ;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000259 JumpTarget ContinueJumpTarget;
260 JumpTarget BreakJumpTarget;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000261 CFGBlock *SwitchTerminatedBlock;
262 CFGBlock *DefaultCaseBlock;
263 CFGBlock *TryTerminatedBlock;
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000264
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000265 // Current position in local scope.
266 LocalScope::const_iterator ScopePos;
267
268 // LabelMap records the mapping from Label expressions to their jump targets.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000269 typedef llvm::DenseMap<LabelDecl*, JumpTarget> LabelMapTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000270 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +0000271
272 // A list of blocks that end with a "goto" that must be backpatched to their
273 // resolved targets upon completion of CFG construction.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000274 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000275 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +0000276
Ted Kremenek19bb3562007-08-28 19:26:49 +0000277 // A list of labels whose address has been taken (for indirect gotos).
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000278 typedef llvm::SmallPtrSet<LabelDecl*, 5> LabelSetTy;
Ted Kremenek19bb3562007-08-28 19:26:49 +0000279 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +0000280
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000281 bool badCFG;
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +0000282 const CFG::BuildOptions &BuildOpts;
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000283
284 // State to track for building switch statements.
285 bool switchExclusivelyCovered;
Ted Kremenek04982472011-03-04 01:03:41 +0000286 Expr::EvalResult *switchCond;
Ted Kremenek0d28d362011-03-10 03:50:34 +0000287
288 CFG::BuildOptions::ForcedBlkExprs::value_type *cachedEntry;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000289 const Stmt *lastLookup;
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000290
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000291 // Caches boolean evaluations of expressions to avoid multiple re-evaluations
292 // during construction of branches for chained logical operators.
NAKAMURA Takumi6955da22012-03-25 06:30:37 +0000293 typedef llvm::DenseMap<Expr *, TryResult> CachedBoolEvalsTy;
294 CachedBoolEvalsTy CachedBoolEvals;
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000295
Mike Stump6d9828c2009-07-17 01:31:16 +0000296public:
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +0000297 explicit CFGBuilder(ASTContext *astContext,
298 const CFG::BuildOptions &buildOpts)
299 : Context(astContext), cfg(new CFG()), // crew a new CFG
300 Block(NULL), Succ(NULL),
301 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
302 TryTerminatedBlock(NULL), badCFG(false), BuildOpts(buildOpts),
Ted Kremenek0d28d362011-03-10 03:50:34 +0000303 switchExclusivelyCovered(false), switchCond(0),
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000304 cachedEntry(0), lastLookup(0) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000305
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000306 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +0000307 CFG* buildCFG(const Decl *D, Stmt *Statement);
Mike Stump6d9828c2009-07-17 01:31:16 +0000308
Ted Kremenek0d28d362011-03-10 03:50:34 +0000309 bool alwaysAdd(const Stmt *stmt);
310
Ted Kremenek4f880632009-07-17 22:18:43 +0000311private:
312 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000313 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
314 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000315 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000316 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
John McCall4765fa02010-12-06 08:20:24 +0000317 CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +0000318 AddStmtChoice asc);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000319 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
320 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Richard Smithad762fc2011-04-14 22:09:26 +0000321 CFGBlock *VisitCXXForRangeStmt(CXXForRangeStmt *S);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000322 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
323 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000324 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000325 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
326 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000327 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
328 AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000329 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000330 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000331 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000332 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
John McCall56ca35d2011-02-17 10:25:35 +0000333 CFGBlock *VisitConditionalOperator(AbstractConditionalOperator *C,
334 AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000335 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000336 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Ted Kremenek9c378f72011-08-12 23:37:29 +0000337 CFGBlock *VisitDeclSubExpr(DeclStmt *DS);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000338 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
339 CFGBlock *VisitDoStmt(DoStmt *D);
Ted Kremenek55331da2012-04-12 20:03:44 +0000340 CFGBlock *VisitLambdaExpr(LambdaExpr *E, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000341 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek9c378f72011-08-12 23:37:29 +0000342 CFGBlock *VisitGotoStmt(GotoStmt *G);
Ted Kremenek4f880632009-07-17 22:18:43 +0000343 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000344 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000345 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
346 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek83748e22012-04-12 20:34:52 +0000347 CFGBlock *VisitLambdaExpr(LambdaExpr *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000348 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000349 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
Ted Kremenek8e282c32012-03-06 23:40:47 +0000350 CFGBlock *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Ted Kremenek4f880632009-07-17 22:18:43 +0000351 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
352 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
353 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
354 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
Ted Kremenek9c378f72011-08-12 23:37:29 +0000355 CFGBlock *VisitReturnStmt(ReturnStmt *R);
John McCall4b9c2d22011-11-06 09:01:30 +0000356 CFGBlock *VisitPseudoObjectExpr(PseudoObjectExpr *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000357 CFGBlock *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E,
358 AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000359 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000360 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000361 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000362 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000363
Ted Kremenek852274d2009-12-16 03:18:58 +0000364 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
365 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek9c378f72011-08-12 23:37:29 +0000366 CFGBlock *VisitChildren(Stmt *S);
Ted Kremenek55331da2012-04-12 20:03:44 +0000367 CFGBlock *VisitNoRecurse(Expr *E, AddStmtChoice asc);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000368
Marcin Swiderski8599e762010-11-03 06:19:35 +0000369 // Visitors to walk an AST and generate destructors of temporaries in
370 // full expression.
371 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
372 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
373 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
374 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
375 bool BindToTemporary);
John McCall56ca35d2011-02-17 10:25:35 +0000376 CFGBlock *
377 VisitConditionalOperatorForTemporaryDtors(AbstractConditionalOperator *E,
378 bool BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000379
Ted Kremenek274f4332008-04-28 18:00:46 +0000380 // NYS == Not Yet Supported
Ted Kremenek9c378f72011-08-12 23:37:29 +0000381 CFGBlock *NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000382 badCFG = true;
383 return Block;
384 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000385
Ted Kremenek4f880632009-07-17 22:18:43 +0000386 void autoCreateBlock() { if (!Block) Block = createBlock(); }
387 CFGBlock *createBlock(bool add_successor = true);
Chandler Carruthdba3fb52011-09-13 09:13:49 +0000388 CFGBlock *createNoReturnBlock();
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000389
Zhongxing Xudf119892010-06-03 06:43:23 +0000390 CFGBlock *addStmt(Stmt *S) {
391 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000392 }
Sean Huntcbb67482011-01-08 20:30:50 +0000393 CFGBlock *addInitializer(CXXCtorInitializer *I);
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000394 void addAutomaticObjDtors(LocalScope::const_iterator B,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000395 LocalScope::const_iterator E, Stmt *S);
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000396 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekad5a8942010-08-02 23:46:59 +0000397
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000398 // Local scopes creation.
399 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
400
Ted Kremenek9c378f72011-08-12 23:37:29 +0000401 void addLocalScopeForStmt(Stmt *S);
402 LocalScope* addLocalScopeForDeclStmt(DeclStmt *DS, LocalScope* Scope = NULL);
403 LocalScope* addLocalScopeForVarDecl(VarDecl *VD, LocalScope* Scope = NULL);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000404
Ted Kremenek9c378f72011-08-12 23:37:29 +0000405 void addLocalScopeAndDtors(Stmt *S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000406
407 // Interface to CFGBlock - adding CFGElements.
Ted Kremenekd40066b2011-04-04 23:29:12 +0000408 void appendStmt(CFGBlock *B, const Stmt *S) {
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000409 if (alwaysAdd(S) && cachedEntry)
Ted Kremenek0d28d362011-03-10 03:50:34 +0000410 cachedEntry->second = B;
Ted Kremenek0d28d362011-03-10 03:50:34 +0000411
Jordy Roseac73ea82011-06-10 08:49:37 +0000412 // All block-level expressions should have already been IgnoreParens()ed.
413 assert(!isa<Expr>(S) || cast<Expr>(S)->IgnoreParens() == S);
Ted Kremenekd40066b2011-04-04 23:29:12 +0000414 B->appendStmt(const_cast<Stmt*>(S), cfg->getBumpVectorContext());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000415 }
Sean Huntcbb67482011-01-08 20:30:50 +0000416 void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) {
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000417 B->appendInitializer(I, cfg->getBumpVectorContext());
418 }
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000419 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
420 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
421 }
422 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
423 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
424 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000425 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
426 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
427 }
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000428 void appendAutomaticObjDtor(CFGBlock *B, VarDecl *VD, Stmt *S) {
429 B->appendAutomaticObjDtor(VD, S, cfg->getBumpVectorContext());
430 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000431
Ted Kremenek9c378f72011-08-12 23:37:29 +0000432 void prependAutomaticObjDtorsWithTerminator(CFGBlock *Blk,
Marcin Swiderski53de1342010-09-30 22:54:37 +0000433 LocalScope::const_iterator B, LocalScope::const_iterator E);
434
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000435 void addSuccessor(CFGBlock *B, CFGBlock *S) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000436 B->addSuccessor(S, cfg->getBumpVectorContext());
437 }
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000439 /// Try and evaluate an expression to an integer constant.
440 bool tryEvaluate(Expr *S, Expr::EvalResult &outResult) {
441 if (!BuildOpts.PruneTriviallyFalseEdges)
442 return false;
443 return !S->isTypeDependent() &&
Ted Kremenekf8adeef2011-04-04 20:30:58 +0000444 !S->isValueDependent() &&
Richard Smith51f47082011-10-29 00:50:52 +0000445 S->EvaluateAsRValue(outResult, *Context);
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000448 /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
Mike Stump00998a02009-07-23 23:25:26 +0000449 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000450 TryResult tryEvaluateBool(Expr *S) {
Richard Smith85df96c2011-10-14 20:22:00 +0000451 if (!BuildOpts.PruneTriviallyFalseEdges ||
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000452 S->isTypeDependent() || S->isValueDependent())
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000453 return TryResult();
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000454
455 if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(S)) {
456 if (Bop->isLogicalOp()) {
457 // Check the cache first.
NAKAMURA Takumi6955da22012-03-25 06:30:37 +0000458 CachedBoolEvalsTy::iterator I = CachedBoolEvals.find(S);
459 if (I != CachedBoolEvals.end())
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000460 return I->second; // already in map;
NAKAMURA Takumi9260f612012-03-25 06:30:32 +0000461
462 // Retrieve result at first, or the map might be updated.
463 TryResult Result = evaluateAsBooleanConditionNoCache(S);
464 CachedBoolEvals[S] = Result; // update or insert
465 return Result;
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +0000466 }
467 }
468
469 return evaluateAsBooleanConditionNoCache(S);
470 }
471
472 /// \brief Evaluate as boolean \param E without using the cache.
473 TryResult evaluateAsBooleanConditionNoCache(Expr *E) {
474 if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(E)) {
475 if (Bop->isLogicalOp()) {
476 TryResult LHS = tryEvaluateBool(Bop->getLHS());
477 if (LHS.isKnown()) {
478 // We were able to evaluate the LHS, see if we can get away with not
479 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
480 if (LHS.isTrue() == (Bop->getOpcode() == BO_LOr))
481 return LHS.isTrue();
482
483 TryResult RHS = tryEvaluateBool(Bop->getRHS());
484 if (RHS.isKnown()) {
485 if (Bop->getOpcode() == BO_LOr)
486 return LHS.isTrue() || RHS.isTrue();
487 else
488 return LHS.isTrue() && RHS.isTrue();
489 }
490 } else {
491 TryResult RHS = tryEvaluateBool(Bop->getRHS());
492 if (RHS.isKnown()) {
493 // We can't evaluate the LHS; however, sometimes the result
494 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
495 if (RHS.isTrue() == (Bop->getOpcode() == BO_LOr))
496 return RHS.isTrue();
497 }
498 }
499
500 return TryResult();
501 }
502 }
503
504 bool Result;
505 if (E->EvaluateAsBooleanCondition(Result, *Context))
506 return Result;
507
508 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000509 }
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000510
Ted Kremenekfddd5182007-08-21 21:42:03 +0000511};
Mike Stump6d9828c2009-07-17 01:31:16 +0000512
Ted Kremenek0d28d362011-03-10 03:50:34 +0000513inline bool AddStmtChoice::alwaysAdd(CFGBuilder &builder,
514 const Stmt *stmt) const {
515 return builder.alwaysAdd(stmt) || kind == AlwaysAdd;
516}
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000517
Ted Kremenek0d28d362011-03-10 03:50:34 +0000518bool CFGBuilder::alwaysAdd(const Stmt *stmt) {
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000519 bool shouldAdd = BuildOpts.alwaysAdd(stmt);
520
Ted Kremenek0d28d362011-03-10 03:50:34 +0000521 if (!BuildOpts.forcedBlkExprs)
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000522 return shouldAdd;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000523
524 if (lastLookup == stmt) {
525 if (cachedEntry) {
526 assert(cachedEntry->first == stmt);
527 return true;
528 }
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000529 return shouldAdd;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000530 }
Ted Kremenek0d28d362011-03-10 03:50:34 +0000531
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000532 lastLookup = stmt;
533
534 // Perform the lookup!
Ted Kremenek0d28d362011-03-10 03:50:34 +0000535 CFG::BuildOptions::ForcedBlkExprs *fb = *BuildOpts.forcedBlkExprs;
536
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000537 if (!fb) {
538 // No need to update 'cachedEntry', since it will always be null.
539 assert(cachedEntry == 0);
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000540 return shouldAdd;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000541 }
Ted Kremenek0d28d362011-03-10 03:50:34 +0000542
543 CFG::BuildOptions::ForcedBlkExprs::iterator itr = fb->find(stmt);
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000544 if (itr == fb->end()) {
545 cachedEntry = 0;
Ted Kremenek74fb1a42011-07-19 14:18:43 +0000546 return shouldAdd;
Ted Kremeneka8d459e2011-03-23 21:33:21 +0000547 }
548
Ted Kremenek0d28d362011-03-10 03:50:34 +0000549 cachedEntry = &*itr;
550 return true;
Ted Kremenek3179a452011-03-10 01:14:11 +0000551}
552
Douglas Gregor898574e2008-12-05 23:32:09 +0000553// FIXME: Add support for dependent-sized array types in C++?
554// Does it even make sense to build a CFG for an uninstantiated template?
John McCallf4c73712011-01-19 06:33:43 +0000555static const VariableArrayType *FindVA(const Type *t) {
556 while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
557 if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
Ted Kremenek610a09e2008-09-26 22:58:57 +0000558 if (vat->getSizeExpr())
559 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000560
Ted Kremenek610a09e2008-09-26 22:58:57 +0000561 t = vt->getElementType().getTypePtr();
562 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000563
Ted Kremenek610a09e2008-09-26 22:58:57 +0000564 return 0;
565}
Mike Stump6d9828c2009-07-17 01:31:16 +0000566
567/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
568/// arbitrary statement. Examples include a single expression or a function
569/// body (compound statement). The ownership of the returned CFG is
570/// transferred to the caller. If CFG construction fails, this method returns
571/// NULL.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000572CFG* CFGBuilder::buildCFG(const Decl *D, Stmt *Statement) {
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000573 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000574 if (!Statement)
575 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000576
Mike Stump6d9828c2009-07-17 01:31:16 +0000577 // Create an empty block that will serve as the exit block for the CFG. Since
578 // this is the first block added to the CFG, it will be implicitly registered
579 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000580 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000581 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000582 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000583
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000584 if (BuildOpts.AddImplicitDtors)
585 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
586 addImplicitDtorsForDestructor(DD);
587
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000588 // Visit the statements and create the CFG.
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000589 CFGBlock *B = addStmt(Statement);
590
591 if (badCFG)
592 return NULL;
593
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000594 // For C++ constructor add initializers to CFG.
595 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
596 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
597 E = CD->init_rend(); I != E; ++I) {
598 B = addInitializer(*I);
599 if (badCFG)
600 return NULL;
601 }
602 }
603
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000604 if (B)
605 Succ = B;
Mike Stumpb978a442010-01-21 02:21:40 +0000606
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000607 // Backpatch the gotos whose label -> block mappings we didn't know when we
608 // encountered them.
609 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
610 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000611
Ted Kremenek9c378f72011-08-12 23:37:29 +0000612 CFGBlock *B = I->block;
613 GotoStmt *G = cast<GotoStmt>(B->getTerminator());
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000614 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000615
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000616 // If there is no target for the goto, then we are looking at an
617 // incomplete AST. Handle this by not registering a successor.
618 if (LI == LabelMap.end()) continue;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000619
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000620 JumpTarget JT = LI->second;
Ted Kremenek9ce52702011-01-07 19:37:16 +0000621 prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
622 JT.scopePosition);
623 addSuccessor(B, JT.block);
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000624 }
625
626 // Add successors to the Indirect Goto Dispatch block (if we have one).
Ted Kremenek9c378f72011-08-12 23:37:29 +0000627 if (CFGBlock *B = cfg->getIndirectGotoBlock())
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000628 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
629 E = AddressTakenLabels.end(); I != E; ++I ) {
630
631 // Lookup the target block.
632 LabelMapTy::iterator LI = LabelMap.find(*I);
633
634 // If there is no target block that contains label, then we are looking
635 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000636 if (LI == LabelMap.end()) continue;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000637
Ted Kremenek9ce52702011-01-07 19:37:16 +0000638 addSuccessor(B, LI->second.block);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000639 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000640
Mike Stump6d9828c2009-07-17 01:31:16 +0000641 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000642 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000643
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000644 return cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000645}
Mike Stump6d9828c2009-07-17 01:31:16 +0000646
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000647/// createBlock - Used to lazily create blocks that are connected
648/// to the current (global) succcessor.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000649CFGBlock *CFGBuilder::createBlock(bool add_successor) {
650 CFGBlock *B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000651 if (add_successor && Succ)
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000652 addSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000653 return B;
654}
Mike Stump6d9828c2009-07-17 01:31:16 +0000655
Chandler Carruthdba3fb52011-09-13 09:13:49 +0000656/// createNoReturnBlock - Used to create a block is a 'noreturn' point in the
657/// CFG. It is *not* connected to the current (global) successor, and instead
658/// directly tied to the exit block in order to be reachable.
659CFGBlock *CFGBuilder::createNoReturnBlock() {
660 CFGBlock *B = createBlock(false);
Chandler Carruth83754162011-09-13 09:53:55 +0000661 B->setHasNoReturnElement();
Chandler Carruthdba3fb52011-09-13 09:13:49 +0000662 addSuccessor(B, &cfg->getExit());
663 return B;
664}
665
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000666/// addInitializer - Add C++ base or member initializer element to CFG.
Sean Huntcbb67482011-01-08 20:30:50 +0000667CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) {
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000668 if (!BuildOpts.AddInitializers)
669 return Block;
670
Marcin Swiderski8599e762010-11-03 06:19:35 +0000671 bool IsReference = false;
672 bool HasTemporaries = false;
673
674 // Destructors of temporaries in initialization expression should be called
675 // after initialization finishes.
676 Expr *Init = I->getInit();
677 if (Init) {
Francois Pichet00eb3f92010-12-04 09:14:42 +0000678 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski8599e762010-11-03 06:19:35 +0000679 IsReference = FD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +0000680 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000681
682 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
683 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +0000684 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +0000685 IsReference);
686 }
687 }
688
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000689 autoCreateBlock();
690 appendInitializer(Block, I);
691
Marcin Swiderski8599e762010-11-03 06:19:35 +0000692 if (Init) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000693 if (HasTemporaries) {
Marcin Swiderski8599e762010-11-03 06:19:35 +0000694 // For expression with temporaries go directly to subexpression to omit
695 // generating destructors for the second time.
Ted Kremenek892697d2010-12-16 07:46:53 +0000696 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
697 }
698 return Visit(Init);
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000699 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000700
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000701 return Block;
702}
703
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000704/// \brief Retrieve the type of the temporary object whose lifetime was
705/// extended by a local reference with the given initializer.
706static QualType getReferenceInitTemporaryType(ASTContext &Context,
707 const Expr *Init) {
708 while (true) {
709 // Skip parentheses.
710 Init = Init->IgnoreParens();
711
712 // Skip through cleanups.
713 if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Init)) {
714 Init = EWC->getSubExpr();
715 continue;
716 }
717
718 // Skip through the temporary-materialization expression.
719 if (const MaterializeTemporaryExpr *MTE
720 = dyn_cast<MaterializeTemporaryExpr>(Init)) {
721 Init = MTE->GetTemporaryExpr();
722 continue;
723 }
724
725 // Skip derived-to-base and no-op casts.
726 if (const CastExpr *CE = dyn_cast<CastExpr>(Init)) {
727 if ((CE->getCastKind() == CK_DerivedToBase ||
728 CE->getCastKind() == CK_UncheckedDerivedToBase ||
729 CE->getCastKind() == CK_NoOp) &&
730 Init->getType()->isRecordType()) {
731 Init = CE->getSubExpr();
732 continue;
733 }
734 }
735
736 // Skip member accesses into rvalues.
737 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Init)) {
738 if (!ME->isArrow() && ME->getBase()->isRValue()) {
739 Init = ME->getBase();
740 continue;
741 }
742 }
743
744 break;
745 }
746
747 return Init->getType();
748}
749
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000750/// addAutomaticObjDtors - Add to current block automatic objects destructors
751/// for objects in range of local scope positions. Use S as trigger statement
752/// for destructors.
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000753void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000754 LocalScope::const_iterator E, Stmt *S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000755 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000756 return;
757
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000758 if (B == E)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000759 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000760
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000761 // We need to append the destructors in reverse order, but any one of them
762 // may be a no-return destructor which changes the CFG. As a result, buffer
763 // this sequence up and replay them in reverse order when appending onto the
764 // CFGBlock(s).
765 SmallVector<VarDecl*, 10> Decls;
766 Decls.reserve(B.distance(E));
767 for (LocalScope::const_iterator I = B; I != E; ++I)
768 Decls.push_back(*I);
769
770 for (SmallVectorImpl<VarDecl*>::reverse_iterator I = Decls.rbegin(),
771 E = Decls.rend();
772 I != E; ++I) {
773 // If this destructor is marked as a no-return destructor, we need to
774 // create a new block for the destructor which does not have as a successor
775 // anything built thus far: control won't flow out of this block.
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000776 QualType Ty;
777 if ((*I)->getType()->isReferenceType()) {
778 Ty = getReferenceInitTemporaryType(*Context, (*I)->getInit());
779 } else {
780 Ty = Context->getBaseElementType((*I)->getType());
781 }
782
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000783 const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor();
David Blaikie23661d32012-01-24 04:51:48 +0000784 if (cast<FunctionType>(Dtor->getType())->getNoReturnAttr())
Chandler Carruthdba3fb52011-09-13 09:13:49 +0000785 Block = createNoReturnBlock();
786 else
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000787 autoCreateBlock();
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000788
789 appendAutomaticObjDtor(Block, *I, S);
790 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000791}
792
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000793/// addImplicitDtorsForDestructor - Add implicit destructors generated for
794/// base and member objects in destructor.
795void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
796 assert (BuildOpts.AddImplicitDtors
797 && "Can be called only when dtors should be added");
798 const CXXRecordDecl *RD = DD->getParent();
799
800 // At the end destroy virtual base objects.
801 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
802 VE = RD->vbases_end(); VI != VE; ++VI) {
803 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
804 if (!CD->hasTrivialDestructor()) {
805 autoCreateBlock();
806 appendBaseDtor(Block, VI);
807 }
808 }
809
810 // Before virtual bases destroy direct base objects.
811 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
812 BE = RD->bases_end(); BI != BE; ++BI) {
David Blaikie23661d32012-01-24 04:51:48 +0000813 if (!BI->isVirtual()) {
814 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
815 if (!CD->hasTrivialDestructor()) {
816 autoCreateBlock();
817 appendBaseDtor(Block, BI);
818 }
819 }
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000820 }
821
822 // First destroy member objects.
823 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
824 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +0000825 // Check for constant size array. Set type to array element type.
826 QualType QT = FI->getType();
827 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
828 if (AT->getSize() == 0)
829 continue;
830 QT = AT->getElementType();
831 }
832
833 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000834 if (!CD->hasTrivialDestructor()) {
835 autoCreateBlock();
David Blaikie581deb32012-06-06 20:45:41 +0000836 appendMemberDtor(Block, *FI);
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000837 }
838 }
839}
840
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000841/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
842/// way return valid LocalScope object.
843LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
844 if (!Scope) {
Ted Kremenekfe59b742011-02-15 02:47:45 +0000845 llvm::BumpPtrAllocator &alloc = cfg->getAllocator();
846 Scope = alloc.Allocate<LocalScope>();
847 BumpVectorContext ctx(alloc);
848 new (Scope) LocalScope(ctx, ScopePos);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000849 }
850 return Scope;
851}
852
853/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000854/// that should create implicit scope (e.g. if/else substatements).
Ted Kremenek9c378f72011-08-12 23:37:29 +0000855void CFGBuilder::addLocalScopeForStmt(Stmt *S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000856 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000857 return;
858
859 LocalScope *Scope = 0;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000860
861 // For compound statement we will be creating explicit scope.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000862 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000863 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
864 ; BI != BE; ++BI) {
Chandler Carrutha1364be2011-09-10 00:02:34 +0000865 Stmt *SI = (*BI)->stripLabelLikeStatements();
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000866 if (DeclStmt *DS = dyn_cast<DeclStmt>(SI))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000867 Scope = addLocalScopeForDeclStmt(DS, Scope);
868 }
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000869 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000870 }
871
872 // For any other statement scope will be implicit and as such will be
873 // interesting only for DeclStmt.
Chandler Carrutha1364be2011-09-10 00:02:34 +0000874 if (DeclStmt *DS = dyn_cast<DeclStmt>(S->stripLabelLikeStatements()))
Zhongxing Xub6edff52010-10-01 03:09:09 +0000875 addLocalScopeForDeclStmt(DS);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000876}
877
878/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
879/// reuse Scope if not NULL.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000880LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt *DS,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000881 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000882 if (!BuildOpts.AddImplicitDtors)
883 return Scope;
884
885 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
886 ; DI != DE; ++DI) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000887 if (VarDecl *VD = dyn_cast<VarDecl>(*DI))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000888 Scope = addLocalScopeForVarDecl(VD, Scope);
889 }
890 return Scope;
891}
892
893/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
894/// create add scope for automatic objects and temporary objects bound to
895/// const reference. Will reuse Scope if not NULL.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000896LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl *VD,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000897 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000898 if (!BuildOpts.AddImplicitDtors)
899 return Scope;
900
901 // Check if variable is local.
902 switch (VD->getStorageClass()) {
903 case SC_None:
904 case SC_Auto:
905 case SC_Register:
906 break;
907 default: return Scope;
908 }
909
910 // Check for const references bound to temporary. Set type to pointee.
911 QualType QT = VD->getType();
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000912 if (QT.getTypePtr()->isReferenceType()) {
Douglas Gregor03e80032011-06-21 17:03:29 +0000913 if (!VD->extendsLifetimeOfTemporary())
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000914 return Scope;
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000915
916 QT = getReferenceInitTemporaryType(*Context, VD->getInit());
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000917 }
918
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000919 // Check for constant size array. Set type to array element type.
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000920 while (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000921 if (AT->getSize() == 0)
922 return Scope;
923 QT = AT->getElementType();
924 }
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000925
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000926 // Check if type is a C++ class with non-trivial destructor.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000927 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
David Blaikie23661d32012-01-24 04:51:48 +0000928 if (!CD->hasTrivialDestructor()) {
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000929 // Add the variable to scope
930 Scope = createOrReuseLocalScope(Scope);
931 Scope->addVar(VD);
932 ScopePos = Scope->begin();
933 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000934 return Scope;
935}
936
937/// addLocalScopeAndDtors - For given statement add local scope for it and
938/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000939void CFGBuilder::addLocalScopeAndDtors(Stmt *S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000940 if (!BuildOpts.AddImplicitDtors)
941 return;
942
943 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000944 addLocalScopeForStmt(S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000945 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
946}
947
Marcin Swiderski53de1342010-09-30 22:54:37 +0000948/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
949/// variables with automatic storage duration to CFGBlock's elements vector.
950/// Elements will be prepended to physical beginning of the vector which
951/// happens to be logical end. Use blocks terminator as statement that specifies
952/// destructors call site.
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000953/// FIXME: This mechanism for adding automatic destructors doesn't handle
954/// no-return destructors properly.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000955void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock *Blk,
Marcin Swiderski53de1342010-09-30 22:54:37 +0000956 LocalScope::const_iterator B, LocalScope::const_iterator E) {
Chandler Carruthc8cfc742011-09-13 06:09:01 +0000957 BumpVectorContext &C = cfg->getBumpVectorContext();
958 CFGBlock::iterator InsertPos
959 = Blk->beginAutomaticObjDtorsInsert(Blk->end(), B.distance(E), C);
960 for (LocalScope::const_iterator I = B; I != E; ++I)
961 InsertPos = Blk->insertAutomaticObjDtor(InsertPos, *I,
962 Blk->getTerminator());
Marcin Swiderski53de1342010-09-30 22:54:37 +0000963}
964
Ted Kremenek4f880632009-07-17 22:18:43 +0000965/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000966/// blocks for ternary operators, &&, and ||. We also process "," and
967/// DeclStmts (which may contain nested control-flow).
Ted Kremenek9c378f72011-08-12 23:37:29 +0000968CFGBlock *CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenekf42e3372010-04-30 22:25:53 +0000969 if (!S) {
970 badCFG = true;
971 return 0;
972 }
Jordy Roseac73ea82011-06-10 08:49:37 +0000973
974 if (Expr *E = dyn_cast<Expr>(S))
975 S = E->IgnoreParens();
976
Ted Kremenek4f880632009-07-17 22:18:43 +0000977 switch (S->getStmtClass()) {
978 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000979 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000980
981 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000982 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000983
John McCall56ca35d2011-02-17 10:25:35 +0000984 case Stmt::BinaryConditionalOperatorClass:
985 return VisitConditionalOperator(cast<BinaryConditionalOperator>(S), asc);
986
Ted Kremenek4f880632009-07-17 22:18:43 +0000987 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000988 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Ted Kremenek4f880632009-07-17 22:18:43 +0000990 case Stmt::BlockExprClass:
Ted Kremenek55331da2012-04-12 20:03:44 +0000991 return VisitNoRecurse(cast<Expr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000992
Ted Kremenek4f880632009-07-17 22:18:43 +0000993 case Stmt::BreakStmtClass:
994 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Ted Kremenek4f880632009-07-17 22:18:43 +0000996 case Stmt::CallExprClass:
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000997 case Stmt::CXXOperatorCallExprClass:
John McCall1de85332011-05-11 07:19:11 +0000998 case Stmt::CXXMemberCallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +0000999 case Stmt::UserDefinedLiteralClass:
Ted Kremenek852274d2009-12-16 03:18:58 +00001000 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Ted Kremenek4f880632009-07-17 22:18:43 +00001002 case Stmt::CaseStmtClass:
1003 return VisitCaseStmt(cast<CaseStmt>(S));
1004
1005 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +00001006 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Ted Kremenek4f880632009-07-17 22:18:43 +00001008 case Stmt::CompoundStmtClass:
1009 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Ted Kremenek4f880632009-07-17 22:18:43 +00001011 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +00001012 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Ted Kremenek4f880632009-07-17 22:18:43 +00001014 case Stmt::ContinueStmtClass:
1015 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Ted Kremenek021c8af2010-01-19 20:40:33 +00001017 case Stmt::CXXCatchStmtClass:
1018 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
1019
John McCall4765fa02010-12-06 08:20:24 +00001020 case Stmt::ExprWithCleanupsClass:
1021 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +00001022
Zhongxing Xua725ed42010-11-01 13:04:58 +00001023 case Stmt::CXXBindTemporaryExprClass:
1024 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
1025
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00001026 case Stmt::CXXConstructExprClass:
1027 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
1028
Zhongxing Xua725ed42010-11-01 13:04:58 +00001029 case Stmt::CXXFunctionalCastExprClass:
1030 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
1031
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00001032 case Stmt::CXXTemporaryObjectExprClass:
1033 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
1034
Ted Kremenek021c8af2010-01-19 20:40:33 +00001035 case Stmt::CXXThrowExprClass:
1036 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +00001037
Ted Kremenek021c8af2010-01-19 20:40:33 +00001038 case Stmt::CXXTryStmtClass:
1039 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +00001040
Richard Smithad762fc2011-04-14 22:09:26 +00001041 case Stmt::CXXForRangeStmtClass:
1042 return VisitCXXForRangeStmt(cast<CXXForRangeStmt>(S));
1043
Ted Kremenek4f880632009-07-17 22:18:43 +00001044 case Stmt::DeclStmtClass:
1045 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Ted Kremenek4f880632009-07-17 22:18:43 +00001047 case Stmt::DefaultStmtClass:
1048 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Ted Kremenek4f880632009-07-17 22:18:43 +00001050 case Stmt::DoStmtClass:
1051 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Ted Kremenek4f880632009-07-17 22:18:43 +00001053 case Stmt::ForStmtClass:
1054 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Ted Kremenek4f880632009-07-17 22:18:43 +00001056 case Stmt::GotoStmtClass:
1057 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Ted Kremenek4f880632009-07-17 22:18:43 +00001059 case Stmt::IfStmtClass:
1060 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Ted Kremenek892697d2010-12-16 07:46:53 +00001062 case Stmt::ImplicitCastExprClass:
1063 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00001064
Ted Kremenek4f880632009-07-17 22:18:43 +00001065 case Stmt::IndirectGotoStmtClass:
1066 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Ted Kremenek4f880632009-07-17 22:18:43 +00001068 case Stmt::LabelStmtClass:
1069 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Ted Kremenek83748e22012-04-12 20:34:52 +00001071 case Stmt::LambdaExprClass:
1072 return VisitLambdaExpr(cast<LambdaExpr>(S), asc);
1073
Ted Kremenek115c1b92010-04-11 17:02:10 +00001074 case Stmt::MemberExprClass:
1075 return VisitMemberExpr(cast<MemberExpr>(S), asc);
1076
Ted Kremenek6a9065a2011-11-05 00:10:15 +00001077 case Stmt::NullStmtClass:
1078 return Block;
1079
Ted Kremenek4f880632009-07-17 22:18:43 +00001080 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +00001081 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
1082
Ted Kremenek8e282c32012-03-06 23:40:47 +00001083 case Stmt::ObjCAutoreleasePoolStmtClass:
1084 return VisitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(S));
1085
Ted Kremenek4f880632009-07-17 22:18:43 +00001086 case Stmt::ObjCAtSynchronizedStmtClass:
1087 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Ted Kremenek4f880632009-07-17 22:18:43 +00001089 case Stmt::ObjCAtThrowStmtClass:
1090 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Ted Kremenek4f880632009-07-17 22:18:43 +00001092 case Stmt::ObjCAtTryStmtClass:
1093 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Ted Kremenek4f880632009-07-17 22:18:43 +00001095 case Stmt::ObjCForCollectionStmtClass:
1096 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Ted Kremenek6a9065a2011-11-05 00:10:15 +00001098 case Stmt::OpaqueValueExprClass:
Ted Kremenek4f880632009-07-17 22:18:43 +00001099 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +00001100
John McCall4b9c2d22011-11-06 09:01:30 +00001101 case Stmt::PseudoObjectExprClass:
1102 return VisitPseudoObjectExpr(cast<PseudoObjectExpr>(S));
1103
Ted Kremenek4f880632009-07-17 22:18:43 +00001104 case Stmt::ReturnStmtClass:
1105 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001106
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001107 case Stmt::UnaryExprOrTypeTraitExprClass:
1108 return VisitUnaryExprOrTypeTraitExpr(cast<UnaryExprOrTypeTraitExpr>(S),
1109 asc);
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Ted Kremenek4f880632009-07-17 22:18:43 +00001111 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +00001112 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Ted Kremenek4f880632009-07-17 22:18:43 +00001114 case Stmt::SwitchStmtClass:
1115 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001117 case Stmt::UnaryOperatorClass:
1118 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
1119
Ted Kremenek4f880632009-07-17 22:18:43 +00001120 case Stmt::WhileStmtClass:
1121 return VisitWhileStmt(cast<WhileStmt>(S));
1122 }
1123}
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Ted Kremenek852274d2009-12-16 03:18:58 +00001125CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001126 if (asc.alwaysAdd(*this, S)) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001127 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001128 appendStmt(Block, S);
Mike Stump6d9828c2009-07-17 01:31:16 +00001129 }
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Ted Kremenek4f880632009-07-17 22:18:43 +00001131 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001132}
Mike Stump6d9828c2009-07-17 01:31:16 +00001133
Ted Kremenek4f880632009-07-17 22:18:43 +00001134/// VisitChildren - Visit the children of a Stmt.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001135CFGBlock *CFGBuilder::VisitChildren(Stmt *Terminator) {
Richard Smith534986f2012-04-14 00:33:13 +00001136 CFGBlock *lastBlock = Block;
Ted Kremenek6b12da92011-02-21 22:11:26 +00001137 for (Stmt::child_range I = Terminator->children(); I; ++I)
1138 if (Stmt *child = *I)
1139 if (CFGBlock *b = Visit(child))
1140 lastBlock = b;
1141
1142 return lastBlock;
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001143}
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Ted Kremenek852274d2009-12-16 03:18:58 +00001145CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
1146 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001147 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001148
Ted Kremenek3179a452011-03-10 01:14:11 +00001149 if (asc.alwaysAdd(*this, A)) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001150 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001151 appendStmt(Block, A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001152 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001153
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001154 return Block;
1155}
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001157CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek892697d2010-12-16 07:46:53 +00001158 AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001159 if (asc.alwaysAdd(*this, U)) {
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001160 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001161 appendStmt(Block, U);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001162 }
1163
Ted Kremenek892697d2010-12-16 07:46:53 +00001164 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan99cae5b2010-11-22 08:45:56 +00001165}
1166
Ted Kremenek852274d2009-12-16 03:18:58 +00001167CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
1168 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001169 if (B->isLogicalOp()) { // && or ||
Ted Kremenek9c378f72011-08-12 23:37:29 +00001170 CFGBlock *ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001171 appendStmt(ConfluenceBlock, B);
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001173 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001174 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Ted Kremenek4f880632009-07-17 22:18:43 +00001176 // create the block evaluating the LHS
Ted Kremenek9c378f72011-08-12 23:37:29 +00001177 CFGBlock *LHSBlock = createBlock(false);
Ted Kremenek4f880632009-07-17 22:18:43 +00001178 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Ted Kremenek4f880632009-07-17 22:18:43 +00001180 // create the block evaluating the RHS
1181 Succ = ConfluenceBlock;
1182 Block = NULL;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001183 CFGBlock *RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +00001184
1185 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001186 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +00001187 return 0;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001188 } else {
Ted Kremenek862b24f2010-04-29 01:10:26 +00001189 // Create an empty block for cases where the RHS doesn't require
1190 // any explicit statements in the CFG.
1191 RHSBlock = createBlock();
1192 }
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +00001194 // Generate the blocks for evaluating the LHS.
1195 Block = LHSBlock;
1196 CFGBlock *EntryLHSBlock = addStmt(B->getLHS());
1197
Mike Stump00998a02009-07-23 23:25:26 +00001198 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001199 TryResult KnownVal = tryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +00001200 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +00001201 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +00001202
Ted Kremenek4f880632009-07-17 22:18:43 +00001203 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +00001204 if (B->getOpcode() == BO_LOr) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001205 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
1206 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001207 } else {
John McCall2de56d12010-08-25 11:45:40 +00001208 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001209 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
1210 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001211 }
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Argyrios Kyrtzidis8c6d3602012-03-23 00:59:17 +00001213 return EntryLHSBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001214 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001215
1216 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +00001217 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001218 appendStmt(Block, B);
Ted Kremenek4f880632009-07-17 22:18:43 +00001219 addStmt(B->getRHS());
1220 return addStmt(B->getLHS());
1221 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001222
1223 if (B->isAssignmentOp()) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001224 if (asc.alwaysAdd(*this, B)) {
Zhongxing Xufc61d942010-06-03 06:23:18 +00001225 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001226 appendStmt(Block, B);
Zhongxing Xufc61d942010-06-03 06:23:18 +00001227 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001228 Visit(B->getLHS());
Marcin Swiderskie1667192010-10-24 08:21:40 +00001229 return Visit(B->getRHS());
Zhongxing Xufc61d942010-06-03 06:23:18 +00001230 }
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Ted Kremenek3179a452011-03-10 01:14:11 +00001232 if (asc.alwaysAdd(*this, B)) {
Marcin Swiderskie1667192010-10-24 08:21:40 +00001233 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001234 appendStmt(Block, B);
Marcin Swiderskie1667192010-10-24 08:21:40 +00001235 }
1236
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001237 CFGBlock *RBlock = Visit(B->getRHS());
1238 CFGBlock *LBlock = Visit(B->getLHS());
1239 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1240 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1241 // return RBlock. Otherwise we'll incorrectly return NULL.
1242 return (LBlock ? LBlock : RBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001243}
1244
Ted Kremenek55331da2012-04-12 20:03:44 +00001245CFGBlock *CFGBuilder::VisitNoRecurse(Expr *E, AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001246 if (asc.alwaysAdd(*this, E)) {
Ted Kremenek721903e2009-11-25 01:34:30 +00001247 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001248 appendStmt(Block, E);
Ted Kremenek721903e2009-11-25 01:34:30 +00001249 }
1250 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001251}
1252
Ted Kremenek4f880632009-07-17 22:18:43 +00001253CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1254 // "break" is a control-flow statement. Thus we stop processing the current
1255 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001256 if (badCFG)
1257 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Ted Kremenek4f880632009-07-17 22:18:43 +00001259 // Now create a new block that ends with the break statement.
1260 Block = createBlock(false);
1261 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Ted Kremenek4f880632009-07-17 22:18:43 +00001263 // If there is no target for the break, then we are looking at an incomplete
1264 // AST. This means that the CFG cannot be constructed.
Ted Kremenek9ce52702011-01-07 19:37:16 +00001265 if (BreakJumpTarget.block) {
1266 addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B);
1267 addSuccessor(Block, BreakJumpTarget.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001268 } else
Ted Kremenek4f880632009-07-17 22:18:43 +00001269 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001270
1271
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001272 return Block;
1273}
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001275static bool CanThrow(Expr *E, ASTContext &Ctx) {
Mike Stump4c45aa12010-01-21 15:20:48 +00001276 QualType Ty = E->getType();
1277 if (Ty->isFunctionPointerType())
1278 Ty = Ty->getAs<PointerType>()->getPointeeType();
1279 else if (Ty->isBlockPointerType())
1280 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +00001281
Mike Stump4c45aa12010-01-21 15:20:48 +00001282 const FunctionType *FT = Ty->getAs<FunctionType>();
1283 if (FT) {
1284 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
Richard Smithe6975e92012-04-17 00:58:00 +00001285 if (Proto->getExceptionSpecType() != EST_Uninstantiated &&
1286 Proto->isNothrow(Ctx))
Mike Stump4c45aa12010-01-21 15:20:48 +00001287 return false;
1288 }
1289 return true;
1290}
1291
Ted Kremenek852274d2009-12-16 03:18:58 +00001292CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
John McCall1de85332011-05-11 07:19:11 +00001293 // Compute the callee type.
1294 QualType calleeType = C->getCallee()->getType();
1295 if (calleeType == Context->BoundMemberTy) {
1296 QualType boundType = Expr::findBoundMemberType(C->getCallee());
1297
1298 // We should only get a null bound type if processing a dependent
1299 // CFG. Recover by assuming nothing.
1300 if (!boundType.isNull()) calleeType = boundType;
Ted Kremenek4f880632009-07-17 22:18:43 +00001301 }
Mike Stump24556362009-07-25 21:26:53 +00001302
John McCall1de85332011-05-11 07:19:11 +00001303 // If this is a call to a no-return function, this stops the block here.
1304 bool NoReturn = getFunctionExtInfo(*calleeType).getNoReturn();
1305
Mike Stump4c45aa12010-01-21 15:20:48 +00001306 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001307
1308 // Languages without exceptions are assumed to not throw.
David Blaikie4e4d0842012-03-11 07:00:24 +00001309 if (Context->getLangOpts().Exceptions) {
Ted Kremenek6c52c782010-09-14 23:41:16 +00001310 if (BuildOpts.AddEHEdges)
Mike Stump4c45aa12010-01-21 15:20:48 +00001311 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +00001312 }
1313
1314 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +00001315 if (FD->hasAttr<NoReturnAttr>())
1316 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +00001317 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +00001318 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001319 }
Mike Stump24556362009-07-25 21:26:53 +00001320
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001321 if (!CanThrow(C->getCallee(), *Context))
Mike Stump4c45aa12010-01-21 15:20:48 +00001322 AddEHEdge = false;
1323
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001324 if (!NoReturn && !AddEHEdge)
1325 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Mike Stump079bd722010-01-19 22:00:14 +00001327 if (Block) {
1328 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001329 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +00001330 return 0;
1331 }
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Chandler Carruthdba3fb52011-09-13 09:13:49 +00001333 if (NoReturn)
1334 Block = createNoReturnBlock();
1335 else
1336 Block = createBlock();
1337
Ted Kremenek247e9662011-03-10 01:14:08 +00001338 appendStmt(Block, C);
Mike Stump24556362009-07-25 21:26:53 +00001339
Mike Stump4c45aa12010-01-21 15:20:48 +00001340 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +00001341 // Add exceptional edges.
1342 if (TryTerminatedBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001343 addSuccessor(Block, TryTerminatedBlock);
Mike Stump079bd722010-01-19 22:00:14 +00001344 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001345 addSuccessor(Block, &cfg->getExit());
Mike Stump079bd722010-01-19 22:00:14 +00001346 }
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Mike Stump24556362009-07-25 21:26:53 +00001348 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +00001349}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001350
Ted Kremenek852274d2009-12-16 03:18:58 +00001351CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1352 AddStmtChoice asc) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001353 CFGBlock *ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001354 appendStmt(ConfluenceBlock, C);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001355 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001356 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001358 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001359 Succ = ConfluenceBlock;
1360 Block = NULL;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001361 CFGBlock *LHSBlock = Visit(C->getLHS(), 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 Succ = ConfluenceBlock;
1366 Block = NULL;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001367 CFGBlock *RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001368 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001369 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001371 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +00001372 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001373 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
1374 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1375 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001376 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001377 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001378}
Mike Stump1eb44332009-09-09 15:08:12 +00001379
1380
Ted Kremenek9c378f72011-08-12 23:37:29 +00001381CFGBlock *CFGBuilder::VisitCompoundStmt(CompoundStmt *C) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001382 addLocalScopeAndDtors(C);
Ted Kremenek9c378f72011-08-12 23:37:29 +00001383 CFGBlock *LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001384
1385 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1386 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +00001387 // If we hit a segment of code just containing ';' (NullStmts), we can
1388 // get a null block back. In such cases, just use the LastBlock
1389 if (CFGBlock *newBlock = addStmt(*I))
1390 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +00001392 if (badCFG)
1393 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001394 }
Mike Stump079bd722010-01-19 22:00:14 +00001395
Ted Kremenek4f880632009-07-17 22:18:43 +00001396 return LastBlock;
1397}
Mike Stump1eb44332009-09-09 15:08:12 +00001398
John McCall56ca35d2011-02-17 10:25:35 +00001399CFGBlock *CFGBuilder::VisitConditionalOperator(AbstractConditionalOperator *C,
Ted Kremenek852274d2009-12-16 03:18:58 +00001400 AddStmtChoice asc) {
John McCall56ca35d2011-02-17 10:25:35 +00001401 const BinaryConditionalOperator *BCO = dyn_cast<BinaryConditionalOperator>(C);
1402 const OpaqueValueExpr *opaqueValue = (BCO ? BCO->getOpaqueValue() : NULL);
1403
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001404 // Create the confluence block that will "merge" the results of the ternary
1405 // expression.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001406 CFGBlock *ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001407 appendStmt(ConfluenceBlock, C);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001408 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001409 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001411 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001412
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001413 // Create a block for the LHS expression if there is an LHS expression. A
1414 // GCC extension allows LHS to be NULL, causing the condition to be the
1415 // value that is returned instead.
1416 // e.g: x ?: y is shorthand for: x ? x : y;
1417 Succ = ConfluenceBlock;
1418 Block = NULL;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001419 CFGBlock *LHSBlock = 0;
John McCall56ca35d2011-02-17 10:25:35 +00001420 const Expr *trueExpr = C->getTrueExpr();
1421 if (trueExpr != opaqueValue) {
1422 LHSBlock = Visit(C->getTrueExpr(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001423 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001424 return 0;
1425 Block = NULL;
1426 }
Ted Kremenekf226d182011-02-24 03:09:15 +00001427 else
1428 LHSBlock = ConfluenceBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001430 // Create the block for the RHS expression.
1431 Succ = ConfluenceBlock;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001432 CFGBlock *RHSBlock = Visit(C->getFalseExpr(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001433 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001434 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001436 // Create the block that will contain the condition.
1437 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Mike Stump00998a02009-07-23 23:25:26 +00001439 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001440 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
Ted Kremenekf226d182011-02-24 03:09:15 +00001441 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001442 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001443 Block->setTerminator(C);
John McCall56ca35d2011-02-17 10:25:35 +00001444 Expr *condExpr = C->getCond();
John McCalld40baf62011-02-19 03:13:26 +00001445
Ted Kremenekf226d182011-02-24 03:09:15 +00001446 if (opaqueValue) {
1447 // Run the condition expression if it's not trivially expressed in
1448 // terms of the opaque value (or if there is no opaque value).
1449 if (condExpr != opaqueValue)
1450 addStmt(condExpr);
John McCalld40baf62011-02-19 03:13:26 +00001451
Ted Kremenekf226d182011-02-24 03:09:15 +00001452 // Before that, run the common subexpression if there was one.
1453 // At least one of this or the above will be run.
1454 return addStmt(BCO->getCommon());
1455 }
1456
1457 return addStmt(condExpr);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001458}
1459
Ted Kremenek4f880632009-07-17 22:18:43 +00001460CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Ted Kremenekbc869de2011-05-10 18:42:15 +00001461 // Check if the Decl is for an __label__. If so, elide it from the
1462 // CFG entirely.
1463 if (isa<LabelDecl>(*DS->decl_begin()))
1464 return Block;
1465
Ted Kremenek29c9e622011-05-24 20:41:31 +00001466 // This case also handles static_asserts.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001467 if (DS->isSingleDecl())
1468 return VisitDeclSubExpr(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Ted Kremenek4f880632009-07-17 22:18:43 +00001470 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Ted Kremenek4f880632009-07-17 22:18:43 +00001472 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001473 typedef SmallVector<Decl*,10> BufTy;
Ted Kremenek4f880632009-07-17 22:18:43 +00001474 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Ted Kremenek4f880632009-07-17 22:18:43 +00001476 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1477 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1478 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1479 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Ted Kremenek4f880632009-07-17 22:18:43 +00001481 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1482 // automatically freed with the CFG.
1483 DeclGroupRef DG(*I);
1484 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001485 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001486 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Ted Kremenek4f880632009-07-17 22:18:43 +00001488 // Append the fake DeclStmt to block.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001489 B = VisitDeclSubExpr(DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +00001490 }
Mike Stump1eb44332009-09-09 15:08:12 +00001491
1492 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +00001493}
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Ted Kremenek4f880632009-07-17 22:18:43 +00001495/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski8599e762010-11-03 06:19:35 +00001496/// DeclStmts and initializers in them.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001497CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt *DS) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001498 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenek29c9e622011-05-24 20:41:31 +00001499 Decl *D = DS->getSingleDecl();
1500
1501 if (isa<StaticAssertDecl>(D)) {
1502 // static_asserts aren't added to the CFG because they do not impact
1503 // runtime semantics.
1504 return Block;
1505 }
1506
Marcin Swiderski8599e762010-11-03 06:19:35 +00001507 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Marcin Swiderski8599e762010-11-03 06:19:35 +00001509 if (!VD) {
1510 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001511 appendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +00001512 return Block;
Marcin Swiderski8599e762010-11-03 06:19:35 +00001513 }
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Marcin Swiderski8599e762010-11-03 06:19:35 +00001515 bool IsReference = false;
1516 bool HasTemporaries = false;
1517
1518 // Destructors of temporaries in initialization expression should be called
1519 // after initialization finishes.
Ted Kremenek4f880632009-07-17 22:18:43 +00001520 Expr *Init = VD->getInit();
Marcin Swiderski8599e762010-11-03 06:19:35 +00001521 if (Init) {
1522 IsReference = VD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +00001523 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +00001524
1525 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1526 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +00001527 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +00001528 IsReference);
1529 }
1530 }
1531
1532 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001533 appendStmt(Block, DS);
Ted Kremenek550f2232012-03-22 05:57:43 +00001534
1535 // Keep track of the last non-null block, as 'Block' can be nulled out
1536 // if the initializer expression is something like a 'while' in a
1537 // statement-expression.
1538 CFGBlock *LastBlock = Block;
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Ted Kremenek4f880632009-07-17 22:18:43 +00001540 if (Init) {
Ted Kremenek550f2232012-03-22 05:57:43 +00001541 if (HasTemporaries) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001542 // For expression with temporaries go directly to subexpression to omit
1543 // generating destructors for the second time.
Ted Kremenek550f2232012-03-22 05:57:43 +00001544 ExprWithCleanups *EC = cast<ExprWithCleanups>(Init);
1545 if (CFGBlock *newBlock = Visit(EC->getSubExpr()))
1546 LastBlock = newBlock;
1547 }
1548 else {
1549 if (CFGBlock *newBlock = Visit(Init))
1550 LastBlock = newBlock;
1551 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001552 }
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Ted Kremenek4f880632009-07-17 22:18:43 +00001554 // If the type of VD is a VLA, then we must process its size expressions.
John McCallf4c73712011-01-19 06:33:43 +00001555 for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr());
1556 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek4f880632009-07-17 22:18:43 +00001557 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001559 // Remove variable from local scope.
1560 if (ScopePos && VD == *ScopePos)
1561 ++ScopePos;
1562
Ted Kremenek550f2232012-03-22 05:57:43 +00001563 return Block ? Block : LastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001564}
1565
Ted Kremenek9c378f72011-08-12 23:37:29 +00001566CFGBlock *CFGBuilder::VisitIfStmt(IfStmt *I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001567 // We may see an if statement in the middle of a basic block, or it may be the
1568 // first statement we are processing. In either case, we create a new basic
1569 // block. First, we create the blocks for the then...else statements, and
1570 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +00001571 // middle of a block, we stop processing that block. That block is then the
1572 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +00001573
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001574 // Save local scope position because in case of condition variable ScopePos
1575 // won't be restored when traversing AST.
1576 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1577
1578 // Create local scope for possible condition variable.
1579 // Store scope position. Add implicit destructor.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001580 if (VarDecl *VD = I->getConditionVariable()) {
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001581 LocalScope::const_iterator BeginScopePos = ScopePos;
1582 addLocalScopeForVarDecl(VD);
1583 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1584 }
1585
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001586 // The block we were processing is now finished. Make it the successor
Mike Stump6d9828c2009-07-17 01:31:16 +00001587 // block.
1588 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001589 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001590 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001591 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001592 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001593
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001594 // Process the false branch.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001595 CFGBlock *ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001596
Ted Kremenek9c378f72011-08-12 23:37:29 +00001597 if (Stmt *Else = I->getElse()) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001598 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001599
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001600 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +00001601 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001602 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001603
1604 // If branch is not a compound statement create implicit scope
1605 // and add destructors.
1606 if (!isa<CompoundStmt>(Else))
1607 addLocalScopeAndDtors(Else);
1608
Ted Kremenek4f880632009-07-17 22:18:43 +00001609 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +00001610
Ted Kremenekb6f7b722007-08-30 18:13:31 +00001611 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1612 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001613 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001614 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001615 return 0;
1616 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001617 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001618
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001619 // Process the true branch.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001620 CFGBlock *ThenBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001621 {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001622 Stmt *Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001623 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001624 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001625 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001626
1627 // If branch is not a compound statement create implicit scope
1628 // and add destructors.
1629 if (!isa<CompoundStmt>(Then))
1630 addLocalScopeAndDtors(Then);
1631
Ted Kremenek4f880632009-07-17 22:18:43 +00001632 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +00001633
Ted Kremenekdbdf7942009-04-01 03:52:47 +00001634 if (!ThenBlock) {
1635 // We can reach here if the "then" body has all NullStmts.
1636 // Create an empty block so we can distinguish between true and false
1637 // branches in path-sensitive analyses.
1638 ThenBlock = createBlock(false);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001639 addSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +00001640 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001641 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001642 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001643 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001644 }
1645
Mike Stump6d9828c2009-07-17 01:31:16 +00001646 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001647 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001648
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001649 // Set the terminator of the new block to the If statement.
1650 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001651
Mike Stump00998a02009-07-23 23:25:26 +00001652 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001653 const TryResult &KnownVal = tryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001654
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001655 // Now add the successors.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001656 addSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1657 addSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001658
1659 // Add the condition as the last statement in the new block. This may create
1660 // new blocks as the condition may contain control-flow. Any newly created
1661 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001662 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001663
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001664 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1665 // and the condition variable initialization to the CFG.
1666 if (VarDecl *VD = I->getConditionVariable()) {
1667 if (Expr *Init = VD->getInit()) {
1668 autoCreateBlock();
Ted Kremenekd40066b2011-04-04 23:29:12 +00001669 appendStmt(Block, I->getConditionVariableDeclStmt());
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001670 addStmt(Init);
1671 }
1672 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001673
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001674 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001675}
Mike Stump6d9828c2009-07-17 01:31:16 +00001676
1677
Ted Kremenek9c378f72011-08-12 23:37:29 +00001678CFGBlock *CFGBuilder::VisitReturnStmt(ReturnStmt *R) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001679 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001680 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001681 // NOTE: If a "return" appears in the middle of a block, this means that the
1682 // code afterwards is DEAD (unreachable). We still keep a basic block
1683 // for that code; a simple "mark-and-sweep" from the entry block will be
1684 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001685
1686 // Create the new block.
1687 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001688
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001689 // The Exit block is the only successor.
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001690 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001691 addSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001692
1693 // Add the return statement to the block. This may create new blocks if R
1694 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001695 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001696}
1697
Ted Kremenek9c378f72011-08-12 23:37:29 +00001698CFGBlock *CFGBuilder::VisitLabelStmt(LabelStmt *L) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001699 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +00001700 addStmt(L->getSubStmt());
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001701 CFGBlock *LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001702
Ted Kremenek4f880632009-07-17 22:18:43 +00001703 if (!LabelBlock) // This can happen when the body is empty, i.e.
1704 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +00001705
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001706 assert(LabelMap.find(L->getDecl()) == LabelMap.end() &&
1707 "label already in map");
1708 LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001709
1710 // Labels partition blocks, so this is the end of the basic block we were
1711 // processing (L is the block's label). Because this is label (and we have
1712 // already processed the substatement) there is no extra control-flow to worry
1713 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +00001714 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001715 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001716 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001717
1718 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001719 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001720
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001721 // This block is now the implicit successor of other blocks.
1722 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001723
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001724 return LabelBlock;
1725}
1726
Ted Kremenek83748e22012-04-12 20:34:52 +00001727CFGBlock *CFGBuilder::VisitLambdaExpr(LambdaExpr *E, AddStmtChoice asc) {
1728 CFGBlock *LastBlock = VisitNoRecurse(E, asc);
1729 for (LambdaExpr::capture_init_iterator it = E->capture_init_begin(),
1730 et = E->capture_init_end(); it != et; ++it) {
1731 if (Expr *Init = *it) {
1732 CFGBlock *Tmp = Visit(Init);
1733 if (Tmp != 0)
1734 LastBlock = Tmp;
1735 }
1736 }
1737 return LastBlock;
1738}
1739
Ted Kremenek9c378f72011-08-12 23:37:29 +00001740CFGBlock *CFGBuilder::VisitGotoStmt(GotoStmt *G) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001741 // Goto is a control-flow statement. Thus we stop processing the current
1742 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001743
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001744 Block = createBlock(false);
1745 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001746
1747 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001748 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001749
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001750 if (I == LabelMap.end())
1751 // We will need to backpatch this block later.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001752 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1753 else {
1754 JumpTarget JT = I->second;
Ted Kremenek9ce52702011-01-07 19:37:16 +00001755 addAutomaticObjDtors(ScopePos, JT.scopePosition, G);
1756 addSuccessor(Block, JT.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001757 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001758
Mike Stump6d9828c2009-07-17 01:31:16 +00001759 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001760}
1761
Ted Kremenek9c378f72011-08-12 23:37:29 +00001762CFGBlock *CFGBuilder::VisitForStmt(ForStmt *F) {
1763 CFGBlock *LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001764
Marcin Swiderski47575f12010-10-01 01:38:14 +00001765 // Save local scope position because in case of condition variable ScopePos
1766 // won't be restored when traversing AST.
1767 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1768
1769 // Create local scope for init statement and possible condition variable.
1770 // Add destructor for init statement and condition variable.
1771 // Store scope position for continue statement.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001772 if (Stmt *Init = F->getInit())
Marcin Swiderski47575f12010-10-01 01:38:14 +00001773 addLocalScopeForStmt(Init);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001774 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1775
Ted Kremenek9c378f72011-08-12 23:37:29 +00001776 if (VarDecl *VD = F->getConditionVariable())
Marcin Swiderski47575f12010-10-01 01:38:14 +00001777 addLocalScopeForVarDecl(VD);
1778 LocalScope::const_iterator ContinueScopePos = ScopePos;
1779
1780 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1781
Mike Stumpfefb9f72009-07-21 01:12:51 +00001782 // "for" is a control-flow statement. Thus we stop processing the current
1783 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001784 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001785 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001786 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001787 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001788 } else
1789 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001790
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001791 // Save the current value for the break targets.
1792 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001793 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001794 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001795
Mike Stump6d9828c2009-07-17 01:31:16 +00001796 // Because of short-circuit evaluation, the condition of the loop can span
1797 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1798 // evaluate the condition.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001799 CFGBlock *ExitConditionBlock = createBlock(false);
1800 CFGBlock *EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001801
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001802 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001803 ExitConditionBlock->setTerminator(F);
1804
1805 // Now add the actual condition to the condition block. Because the condition
1806 // itself may contain control-flow, new blocks may be created.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001807 if (Stmt *C = F->getCond()) {
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001808 Block = ExitConditionBlock;
1809 EntryConditionBlock = addStmt(C);
Ted Kremenek9ce52702011-01-07 19:37:16 +00001810 if (badCFG)
1811 return 0;
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001812 assert(Block == EntryConditionBlock ||
1813 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001814
1815 // If this block contains a condition variable, add both the condition
1816 // variable and initializer to the CFG.
1817 if (VarDecl *VD = F->getConditionVariable()) {
1818 if (Expr *Init = VD->getInit()) {
1819 autoCreateBlock();
Ted Kremenekd40066b2011-04-04 23:29:12 +00001820 appendStmt(Block, F->getConditionVariableDeclStmt());
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001821 EntryConditionBlock = addStmt(Init);
1822 assert(Block == EntryConditionBlock);
1823 }
1824 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001825
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001826 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001827 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001828 return 0;
1829 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001830 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001831
Mike Stump6d9828c2009-07-17 01:31:16 +00001832 // The condition block is the implicit successor for the loop body as well as
1833 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001834 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001835
Mike Stump00998a02009-07-23 23:25:26 +00001836 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001837 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Mike Stump00998a02009-07-23 23:25:26 +00001839 if (F->getCond())
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001840 KnownVal = tryEvaluateBool(F->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001841
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001842 // Now create the loop body.
1843 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001844 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001845
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001846 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001847 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1848 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001849
Ted Kremenekaf603f72007-08-30 18:39:40 +00001850 // Create a new block to contain the (bottom) of the loop body.
1851 Block = NULL;
Marcin Swiderski47575f12010-10-01 01:38:14 +00001852
1853 // Loop body should end with destructor of Condition variable (if any).
1854 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump6d9828c2009-07-17 01:31:16 +00001855
Ted Kremenek9c378f72011-08-12 23:37:29 +00001856 if (Stmt *I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001857 // Generate increment code in its own basic block. This is the target of
1858 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001859 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001860 } else {
1861 // No increment code. Create a special, empty, block that is used as the
1862 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001863 assert(Succ == EntryConditionBlock);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001864 Succ = Block ? Block : createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001865 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001866
Ted Kremenek3575f842009-04-28 00:51:56 +00001867 // Finish up the increment (or empty) block if it hasn't been already.
1868 if (Block) {
1869 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001870 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001871 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001872 Block = 0;
1873 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001874
Marcin Swiderski47575f12010-10-01 01:38:14 +00001875 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001876
Ted Kremenek3575f842009-04-28 00:51:56 +00001877 // The starting block for the loop increment is the block that should
1878 // represent the 'loop target' for looping back to the start of the loop.
Ted Kremenek9ce52702011-01-07 19:37:16 +00001879 ContinueJumpTarget.block->setLoopTarget(F);
Ted Kremenek3575f842009-04-28 00:51:56 +00001880
Marcin Swiderski47575f12010-10-01 01:38:14 +00001881 // If body is not a compound statement create implicit scope
1882 // and add destructors.
1883 if (!isa<CompoundStmt>(F->getBody()))
1884 addLocalScopeAndDtors(F->getBody());
1885
Mike Stump6d9828c2009-07-17 01:31:16 +00001886 // Now populate the body block, and in the process create new blocks as we
1887 // walk the body of the loop.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001888 CFGBlock *BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001889
1890 if (!BodyBlock)
Ted Kremenek9ce52702011-01-07 19:37:16 +00001891 BodyBlock = ContinueJumpTarget.block;//can happen for "for (...;...;...);"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001892 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001893 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001894
Ted Kremenek941fde82009-07-24 04:47:11 +00001895 // This new body block is a successor to our "exit" condition block.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001896 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001897 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001898
Ted Kremenek941fde82009-07-24 04:47:11 +00001899 // Link up the condition block with the code that follows the loop. (the
1900 // false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001901 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001902
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001903 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001904 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001905 if (Stmt *I = F->getInit()) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001906 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001907 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001908 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001909
1910 // There is no loop initialization. We are thus basically a while loop.
1911 // NULL out Block to force lazy block construction.
1912 Block = NULL;
1913 Succ = EntryConditionBlock;
1914 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001915}
1916
Ted Kremenek115c1b92010-04-11 17:02:10 +00001917CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00001918 if (asc.alwaysAdd(*this, M)) {
Ted Kremenek115c1b92010-04-11 17:02:10 +00001919 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00001920 appendStmt(Block, M);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001921 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001922 return Visit(M->getBase());
Ted Kremenek115c1b92010-04-11 17:02:10 +00001923}
1924
Ted Kremenek9c378f72011-08-12 23:37:29 +00001925CFGBlock *CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
Ted Kremenek514de5a2008-11-11 17:10:00 +00001926 // Objective-C fast enumeration 'for' statements:
1927 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1928 //
1929 // for ( Type newVariable in collection_expression ) { statements }
1930 //
1931 // becomes:
1932 //
1933 // prologue:
1934 // 1. collection_expression
1935 // T. jump to loop_entry
1936 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001937 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001938 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1939 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1940 // TB:
1941 // statements
1942 // T. jump to loop_entry
1943 // FB:
1944 // what comes after
1945 //
1946 // and
1947 //
1948 // Type existingItem;
1949 // for ( existingItem in expression ) { statements }
1950 //
1951 // becomes:
1952 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001953 // the same with newVariable replaced with existingItem; the binding works
1954 // the same except that for one ObjCForCollectionStmt::getElement() returns
1955 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001956 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001957
Ted Kremenek9c378f72011-08-12 23:37:29 +00001958 CFGBlock *LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001959
Ted Kremenek514de5a2008-11-11 17:10:00 +00001960 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001961 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001962 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001963 LoopSuccessor = Block;
1964 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001965 } else
1966 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001967
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001968 // Build the condition blocks.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001969 CFGBlock *ExitConditionBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001970
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001971 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001972 ExitConditionBlock->setTerminator(S);
1973
1974 // The last statement in the block should be the ObjCForCollectionStmt, which
1975 // performs the actual binding to 'element' and determines if there are any
1976 // more items in the collection.
Ted Kremenek892697d2010-12-16 07:46:53 +00001977 appendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001978 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001979
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001980 // Walk the 'element' expression to see if there are any side-effects. We
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001981 // generate new blocks as necessary. We DON'T add the statement by default to
Mike Stump6d9828c2009-07-17 01:31:16 +00001982 // the CFG unless it contains control-flow.
Ted Kremenek012614e2011-08-17 21:04:19 +00001983 CFGBlock *EntryConditionBlock = Visit(S->getElement(),
1984 AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001985 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001986 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001987 return 0;
1988 Block = 0;
1989 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001990
1991 // The condition block is the implicit successor for the loop body as well as
1992 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001993 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001994
Ted Kremenek514de5a2008-11-11 17:10:00 +00001995 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001996 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001997 // Save the current values for Succ, continue and break targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001998 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1999 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2000 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00002001
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002002 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
2003 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002004
Ted Kremenek9c378f72011-08-12 23:37:29 +00002005 CFGBlock *BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002006
Ted Kremenek4cb3a852008-11-14 01:57:41 +00002007 if (!BodyBlock)
2008 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002009 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002010 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002011 return 0;
2012 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002013
Ted Kremenek4cb3a852008-11-14 01:57:41 +00002014 // This new body block is a successor to our "exit" condition block.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002015 addSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00002016 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002017
Ted Kremenek4cb3a852008-11-14 01:57:41 +00002018 // Link up the condition block with the code that follows the loop.
2019 // (the false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002020 addSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00002021
Ted Kremenek514de5a2008-11-11 17:10:00 +00002022 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00002023 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00002024 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00002025}
2026
Ted Kremenek8e282c32012-03-06 23:40:47 +00002027CFGBlock *CFGBuilder::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
2028 // Inline the body.
2029 return addStmt(S->getSubStmt());
2030 // TODO: consider adding cleanups for the end of @autoreleasepool scope.
2031}
2032
Ted Kremenek9c378f72011-08-12 23:37:29 +00002033CFGBlock *CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
Ted Kremenekb3b0b362009-05-02 01:49:13 +00002034 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00002035
Ted Kremenekb3b0b362009-05-02 01:49:13 +00002036 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002037 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002038
Ted Kremenekda5348e2009-05-05 23:11:51 +00002039 // The sync body starts its own basic block. This makes it a little easier
2040 // for diagnostic clients.
2041 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002042 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00002043 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002044
Ted Kremenekda5348e2009-05-05 23:11:51 +00002045 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00002046 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00002047 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002048
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00002049 // Add the @synchronized to the CFG.
2050 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002051 appendStmt(Block, S);
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00002052
Ted Kremenekb3b0b362009-05-02 01:49:13 +00002053 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00002054 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00002055}
Mike Stump6d9828c2009-07-17 01:31:16 +00002056
Ted Kremenek9c378f72011-08-12 23:37:29 +00002057CFGBlock *CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002058 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00002059 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00002060}
Ted Kremenek514de5a2008-11-11 17:10:00 +00002061
John McCall4b9c2d22011-11-06 09:01:30 +00002062CFGBlock *CFGBuilder::VisitPseudoObjectExpr(PseudoObjectExpr *E) {
2063 autoCreateBlock();
2064
2065 // Add the PseudoObject as the last thing.
2066 appendStmt(Block, E);
2067
2068 CFGBlock *lastBlock = Block;
2069
2070 // Before that, evaluate all of the semantics in order. In
2071 // CFG-land, that means appending them in reverse order.
2072 for (unsigned i = E->getNumSemanticExprs(); i != 0; ) {
2073 Expr *Semantic = E->getSemanticExpr(--i);
2074
2075 // If the semantic is an opaque value, we're being asked to bind
2076 // it to its source expression.
2077 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
2078 Semantic = OVE->getSourceExpr();
2079
2080 if (CFGBlock *B = Visit(Semantic))
2081 lastBlock = B;
2082 }
2083
2084 return lastBlock;
2085}
2086
Ted Kremenek9c378f72011-08-12 23:37:29 +00002087CFGBlock *CFGBuilder::VisitWhileStmt(WhileStmt *W) {
2088 CFGBlock *LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002089
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002090 // Save local scope position because in case of condition variable ScopePos
2091 // won't be restored when traversing AST.
2092 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2093
2094 // Create local scope for possible condition variable.
2095 // Store scope position for continue statement.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002096 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Ted Kremenek9c378f72011-08-12 23:37:29 +00002097 if (VarDecl *VD = W->getConditionVariable()) {
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002098 addLocalScopeForVarDecl(VD);
2099 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
2100 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002101
Mike Stumpfefb9f72009-07-21 01:12:51 +00002102 // "while" is a control-flow statement. Thus we stop processing the current
2103 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002104 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002105 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002106 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002107 LoopSuccessor = Block;
Ted Kremenek6b12da92011-02-21 22:11:26 +00002108 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00002109 } else
2110 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00002111
2112 // Because of short-circuit evaluation, the condition of the loop can span
2113 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
2114 // evaluate the condition.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002115 CFGBlock *ExitConditionBlock = createBlock(false);
2116 CFGBlock *EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002117
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002118 // Set the terminator for the "exit" condition block.
2119 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00002120
2121 // Now add the actual condition to the condition block. Because the condition
2122 // itself may contain control-flow, new blocks may be created. Thus we update
2123 // "Succ" after adding the condition.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002124 if (Stmt *C = W->getCond()) {
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002125 Block = ExitConditionBlock;
2126 EntryConditionBlock = addStmt(C);
Zhongxing Xua1898dd2010-10-27 03:23:10 +00002127 // The condition might finish the current 'Block'.
2128 Block = EntryConditionBlock;
Ted Kremenekad5a8942010-08-02 23:46:59 +00002129
Ted Kremenek4ec010a2009-12-24 01:34:10 +00002130 // If this block contains a condition variable, add both the condition
2131 // variable and initializer to the CFG.
2132 if (VarDecl *VD = W->getConditionVariable()) {
2133 if (Expr *Init = VD->getInit()) {
2134 autoCreateBlock();
Ted Kremenekd40066b2011-04-04 23:29:12 +00002135 appendStmt(Block, W->getConditionVariableDeclStmt());
Ted Kremenek4ec010a2009-12-24 01:34:10 +00002136 EntryConditionBlock = addStmt(Init);
2137 assert(Block == EntryConditionBlock);
2138 }
2139 }
2140
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002141 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002142 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002143 return 0;
2144 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002145 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002146
2147 // The condition block is the implicit successor for the loop body as well as
2148 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002149 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002150
Mike Stump00998a02009-07-23 23:25:26 +00002151 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002152 const TryResult& KnownVal = tryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00002153
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002154 // Process the loop body.
2155 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00002156 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002157
2158 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002159 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2160 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2161 save_break(BreakJumpTarget);
Ted Kremenekf6e85412009-04-28 03:09:44 +00002162
Mike Stump6d9828c2009-07-17 01:31:16 +00002163 // Create an empty block to represent the transition block for looping back
2164 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00002165 Block = 0;
2166 assert(Succ == EntryConditionBlock);
2167 Succ = createBlock();
2168 Succ->setLoopTarget(W);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002169 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002170
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002171 // All breaks should go to the code following the loop.
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002172 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002173
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002174 // NULL out Block to force lazy instantiation of blocks for the body.
2175 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002176
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002177 // Loop body should end with destructor of Condition variable (if any).
2178 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
2179
2180 // If body is not a compound statement create implicit scope
2181 // and add destructors.
2182 if (!isa<CompoundStmt>(W->getBody()))
2183 addLocalScopeAndDtors(W->getBody());
2184
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002185 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002186 CFGBlock *BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002187
Ted Kremenekaf603f72007-08-30 18:39:40 +00002188 if (!BodyBlock)
Ted Kremenek9ce52702011-01-07 19:37:16 +00002189 BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002190 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002191 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002192 return 0;
2193 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002194
Ted Kremenek941fde82009-07-24 04:47:11 +00002195 // Add the loop body entry as a successor to the condition.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002196 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002197 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002198
Ted Kremenek941fde82009-07-24 04:47:11 +00002199 // Link up the condition block with the code that follows the loop. (the
2200 // false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002201 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002202
2203 // There can be no more statements in the condition block since we loop back
2204 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002205 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002206
Ted Kremenek4ec010a2009-12-24 01:34:10 +00002207 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002208 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002209 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002210}
Mike Stump1eb44332009-09-09 15:08:12 +00002211
2212
Ted Kremenek9c378f72011-08-12 23:37:29 +00002213CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002214 // FIXME: For now we pretend that @catch and the code it contains does not
2215 // exit.
2216 return Block;
2217}
Mike Stump6d9828c2009-07-17 01:31:16 +00002218
Ted Kremenek9c378f72011-08-12 23:37:29 +00002219CFGBlock *CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
Ted Kremenek2fda5042008-12-09 20:20:09 +00002220 // FIXME: This isn't complete. We basically treat @throw like a return
2221 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00002222
Ted Kremenek6c249722009-09-24 18:45:41 +00002223 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002224 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002225 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002226
Ted Kremenek2fda5042008-12-09 20:20:09 +00002227 // Create the new block.
2228 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002229
Ted Kremenek2fda5042008-12-09 20:20:09 +00002230 // The Exit block is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002231 addSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00002232
2233 // Add the statement to the block. This may create new blocks if S contains
2234 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00002235 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00002236}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002237
Ted Kremenek9c378f72011-08-12 23:37:29 +00002238CFGBlock *CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr *T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00002239 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002240 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00002241 return 0;
2242
2243 // Create the new block.
2244 Block = createBlock(false);
2245
Mike Stump5d1d2022010-01-19 02:20:09 +00002246 if (TryTerminatedBlock)
2247 // The current try statement is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002248 addSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00002249 else
Mike Stump5d1d2022010-01-19 02:20:09 +00002250 // otherwise the Exit block is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002251 addSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00002252
2253 // Add the statement to the block. This may create new blocks if S contains
2254 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00002255 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00002256}
2257
Ted Kremenek9c378f72011-08-12 23:37:29 +00002258CFGBlock *CFGBuilder::VisitDoStmt(DoStmt *D) {
2259 CFGBlock *LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002260
Mike Stump8f9893a2009-07-21 01:27:50 +00002261 // "do...while" is a control-flow statement. Thus we stop processing the
2262 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002263 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002264 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002265 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002266 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002267 } else
2268 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00002269
2270 // Because of short-circuit evaluation, the condition of the loop can span
2271 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
2272 // evaluate the condition.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002273 CFGBlock *ExitConditionBlock = createBlock(false);
2274 CFGBlock *EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002275
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002276 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00002277 ExitConditionBlock->setTerminator(D);
2278
2279 // Now add the actual condition to the condition block. Because the condition
2280 // itself may contain control-flow, new blocks may be created.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002281 if (Stmt *C = D->getCond()) {
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002282 Block = ExitConditionBlock;
2283 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002284 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002285 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002286 return 0;
2287 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002288 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002289
Ted Kremenek54827132008-02-27 07:20:00 +00002290 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002291 Succ = EntryConditionBlock;
2292
Mike Stump00998a02009-07-23 23:25:26 +00002293 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002294 const TryResult &KnownVal = tryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00002295
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002296 // Process the loop body.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002297 CFGBlock *BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002298 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002299 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002300
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002301 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002302 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2303 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2304 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00002305
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002306 // All continues within this loop should go to the condition block
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002307 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002308
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002309 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002310 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002311
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002312 // NULL out Block to force lazy instantiation of blocks for the body.
2313 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002314
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002315 // If body is not a compound statement create implicit scope
2316 // and add destructors.
2317 if (!isa<CompoundStmt>(D->getBody()))
2318 addLocalScopeAndDtors(D->getBody());
2319
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002320 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002321 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002322
Ted Kremenekaf603f72007-08-30 18:39:40 +00002323 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00002324 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002325 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002326 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002327 return 0;
2328 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002329
Ted Kremenekd173dc72010-08-17 20:59:56 +00002330 if (!KnownVal.isFalse()) {
2331 // Add an intermediate block between the BodyBlock and the
2332 // ExitConditionBlock to represent the "loop back" transition. Create an
2333 // empty block to represent the transition block for looping back to the
2334 // head of the loop.
2335 // FIXME: Can we do this more efficiently without adding another block?
2336 Block = NULL;
2337 Succ = BodyBlock;
2338 CFGBlock *LoopBackBlock = createBlock();
2339 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00002340
Ted Kremenekd173dc72010-08-17 20:59:56 +00002341 // Add the loop body entry as a successor to the condition.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002342 addSuccessor(ExitConditionBlock, LoopBackBlock);
Ted Kremenekd173dc72010-08-17 20:59:56 +00002343 }
2344 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002345 addSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002346 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002347
Ted Kremenek941fde82009-07-24 04:47:11 +00002348 // Link up the condition block with the code that follows the loop.
2349 // (the false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002350 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002351
2352 // There can be no more statements in the body block(s) since we loop back to
2353 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002354 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002355
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002356 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002357 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002358 return BodyBlock;
2359}
2360
Ted Kremenek9c378f72011-08-12 23:37:29 +00002361CFGBlock *CFGBuilder::VisitContinueStmt(ContinueStmt *C) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002362 // "continue" is a control-flow statement. Thus we stop processing the
2363 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002364 if (badCFG)
2365 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002366
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002367 // Now create a new block that ends with the continue statement.
2368 Block = createBlock(false);
2369 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00002370
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002371 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002372 // incomplete AST. This means the CFG cannot be constructed.
Ted Kremenek9ce52702011-01-07 19:37:16 +00002373 if (ContinueJumpTarget.block) {
2374 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C);
2375 addSuccessor(Block, ContinueJumpTarget.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002376 } else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002377 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00002378
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002379 return Block;
2380}
Mike Stump1eb44332009-09-09 15:08:12 +00002381
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002382CFGBlock *CFGBuilder::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E,
2383 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002384
Ted Kremenek3179a452011-03-10 01:14:11 +00002385 if (asc.alwaysAdd(*this, E)) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002386 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002387 appendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002388 }
Mike Stump1eb44332009-09-09 15:08:12 +00002389
Ted Kremenek4f880632009-07-17 22:18:43 +00002390 // VLA types have expressions that must be evaluated.
Ted Kremenek97e50712011-04-14 01:50:50 +00002391 CFGBlock *lastBlock = Block;
2392
Ted Kremenek4f880632009-07-17 22:18:43 +00002393 if (E->isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00002394 for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr());
Ted Kremenek4f880632009-07-17 22:18:43 +00002395 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek97e50712011-04-14 01:50:50 +00002396 lastBlock = addStmt(VA->getSizeExpr());
Ted Kremenekf91a5b02011-08-06 00:30:00 +00002397 }
Ted Kremenek97e50712011-04-14 01:50:50 +00002398 return lastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002399}
Mike Stump1eb44332009-09-09 15:08:12 +00002400
Ted Kremenek4f880632009-07-17 22:18:43 +00002401/// VisitStmtExpr - Utility method to handle (nested) statement
2402/// expressions (a GCC extension).
Ted Kremenek9c378f72011-08-12 23:37:29 +00002403CFGBlock *CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00002404 if (asc.alwaysAdd(*this, SE)) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002405 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002406 appendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002407 }
Ted Kremenek4f880632009-07-17 22:18:43 +00002408 return VisitCompoundStmt(SE->getSubStmt());
2409}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002410
Ted Kremenek9c378f72011-08-12 23:37:29 +00002411CFGBlock *CFGBuilder::VisitSwitchStmt(SwitchStmt *Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002412 // "switch" is a control-flow statement. Thus we stop processing the current
2413 // block.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002414 CFGBlock *SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002415
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002416 // Save local scope position because in case of condition variable ScopePos
2417 // won't be restored when traversing AST.
2418 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2419
2420 // Create local scope for possible condition variable.
2421 // Store scope position. Add implicit destructor.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002422 if (VarDecl *VD = Terminator->getConditionVariable()) {
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002423 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2424 addLocalScopeForVarDecl(VD);
2425 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2426 }
2427
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002428 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002429 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002430 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002431 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00002432 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002433
2434 // Save the current "switch" context.
2435 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002436 save_default(DefaultCaseBlock);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002437 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002438
Mike Stump6d9828c2009-07-17 01:31:16 +00002439 // Set the "default" case to be the block after the switch statement. If the
2440 // switch statement contains a "default:", this value will be overwritten with
2441 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002442 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00002443
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002444 // Create a new block that will contain the switch statement.
2445 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002446
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002447 // Now process the switch body. The code after the switch is the implicit
2448 // successor.
2449 Succ = SwitchSuccessor;
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002450 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002451
2452 // When visiting the body, the case statements should automatically get linked
2453 // up to the switch. We also don't keep a pointer to the body, since all
2454 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002455 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002456 Block = NULL;
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002457
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002458 // For pruning unreachable case statements, save the current state
2459 // for tracking the condition value.
2460 SaveAndRestore<bool> save_switchExclusivelyCovered(switchExclusivelyCovered,
2461 false);
Ted Kremenek04982472011-03-04 01:03:41 +00002462
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002463 // Determine if the switch condition can be explicitly evaluated.
2464 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek04982472011-03-04 01:03:41 +00002465 Expr::EvalResult result;
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002466 bool b = tryEvaluate(Terminator->getCond(), result);
2467 SaveAndRestore<Expr::EvalResult*> save_switchCond(switchCond,
2468 b ? &result : 0);
Ted Kremenek04982472011-03-04 01:03:41 +00002469
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002470 // If body is not a compound statement create implicit scope
2471 // and add destructors.
2472 if (!isa<CompoundStmt>(Terminator->getBody()))
2473 addLocalScopeAndDtors(Terminator->getBody());
2474
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002475 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002476 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002477 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002478 return 0;
2479 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002480
Mike Stump6d9828c2009-07-17 01:31:16 +00002481 // If we have no "default:" case, the default transition is to the code
Ted Kremenek432c4782011-03-16 04:32:01 +00002482 // following the switch body. Moreover, take into account if all the
2483 // cases of a switch are covered (e.g., switching on an enum value).
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002484 addSuccessor(SwitchTerminatedBlock,
Ted Kremenek432c4782011-03-16 04:32:01 +00002485 switchExclusivelyCovered || Terminator->isAllEnumCasesCovered()
2486 ? 0 : DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002487
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002488 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002489 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002490 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002491 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00002492
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002493 // Finally, if the SwitchStmt contains a condition variable, add both the
2494 // SwitchStmt and the condition variable initialization to the CFG.
2495 if (VarDecl *VD = Terminator->getConditionVariable()) {
2496 if (Expr *Init = VD->getInit()) {
2497 autoCreateBlock();
Ted Kremenekd40066b2011-04-04 23:29:12 +00002498 appendStmt(Block, Terminator->getConditionVariableDeclStmt());
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002499 addStmt(Init);
2500 }
2501 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00002502
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002503 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002504}
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002505
2506static bool shouldAddCase(bool &switchExclusivelyCovered,
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002507 const Expr::EvalResult *switchCond,
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002508 const CaseStmt *CS,
2509 ASTContext &Ctx) {
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002510 if (!switchCond)
2511 return true;
2512
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002513 bool addCase = false;
Ted Kremenek04982472011-03-04 01:03:41 +00002514
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002515 if (!switchExclusivelyCovered) {
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002516 if (switchCond->Val.isInt()) {
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002517 // Evaluate the LHS of the case value.
Richard Smith85df96c2011-10-14 20:22:00 +00002518 const llvm::APSInt &lhsInt = CS->getLHS()->EvaluateKnownConstInt(Ctx);
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002519 const llvm::APSInt &condInt = switchCond->Val.getInt();
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002520
2521 if (condInt == lhsInt) {
2522 addCase = true;
2523 switchExclusivelyCovered = true;
2524 }
2525 else if (condInt < lhsInt) {
2526 if (const Expr *RHS = CS->getRHS()) {
2527 // Evaluate the RHS of the case value.
Richard Smith85df96c2011-10-14 20:22:00 +00002528 const llvm::APSInt &V2 = RHS->EvaluateKnownConstInt(Ctx);
2529 if (V2 <= condInt) {
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002530 addCase = true;
2531 switchExclusivelyCovered = true;
2532 }
2533 }
2534 }
2535 }
2536 else
2537 addCase = true;
2538 }
2539 return addCase;
2540}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002541
Ted Kremenek9c378f72011-08-12 23:37:29 +00002542CFGBlock *CFGBuilder::VisitCaseStmt(CaseStmt *CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002543 // CaseStmts are essentially labels, so they are the first statement in a
2544 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002545 CFGBlock *TopBlock = 0, *LastBlock = 0;
Ted Kremenek04982472011-03-04 01:03:41 +00002546
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002547 if (Stmt *Sub = CS->getSubStmt()) {
2548 // For deeply nested chains of CaseStmts, instead of doing a recursion
2549 // (which can blow out the stack), manually unroll and create blocks
2550 // along the way.
2551 while (isa<CaseStmt>(Sub)) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002552 CFGBlock *currentBlock = createBlock(false);
2553 currentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002554
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002555 if (TopBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002556 addSuccessor(LastBlock, currentBlock);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002557 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002558 TopBlock = currentBlock;
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002559
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002560 addSuccessor(SwitchTerminatedBlock,
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002561 shouldAddCase(switchExclusivelyCovered, switchCond,
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002562 CS, *Context)
2563 ? currentBlock : 0);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002564
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002565 LastBlock = currentBlock;
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002566 CS = cast<CaseStmt>(Sub);
2567 Sub = CS->getSubStmt();
2568 }
2569
2570 addStmt(Sub);
2571 }
Mike Stump1eb44332009-09-09 15:08:12 +00002572
Ted Kremenek9c378f72011-08-12 23:37:29 +00002573 CFGBlock *CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002574 if (!CaseBlock)
2575 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002576
2577 // Cases statements partition blocks, so this is the top of the basic block we
2578 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00002579 CaseBlock->setLabel(CS);
2580
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002581 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002582 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002583
2584 // Add this block to the list of successors for the block with the switch
2585 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00002586 assert(SwitchTerminatedBlock);
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002587 addSuccessor(SwitchTerminatedBlock,
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00002588 shouldAddCase(switchExclusivelyCovered, switchCond,
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002589 CS, *Context)
2590 ? CaseBlock : 0);
Mike Stump6d9828c2009-07-17 01:31:16 +00002591
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002592 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2593 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002594
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002595 if (TopBlock) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002596 addSuccessor(LastBlock, CaseBlock);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002597 Succ = TopBlock;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002598 } else {
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002599 // This block is now the implicit successor of other blocks.
2600 Succ = CaseBlock;
2601 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002602
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002603 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002604}
Mike Stump6d9828c2009-07-17 01:31:16 +00002605
Ted Kremenek9c378f72011-08-12 23:37:29 +00002606CFGBlock *CFGBuilder::VisitDefaultStmt(DefaultStmt *Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002607 if (Terminator->getSubStmt())
2608 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002610 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002611
2612 if (!DefaultCaseBlock)
2613 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002614
2615 // Default statements partition blocks, so this is the top of the basic block
2616 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00002617 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00002618
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002619 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002620 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002621
Mike Stump6d9828c2009-07-17 01:31:16 +00002622 // Unlike case statements, we don't add the default block to the successors
2623 // for the switch statement immediately. This is done when we finish
2624 // processing the switch statement. This allows for the default case
2625 // (including a fall-through to the code after the switch statement) to always
2626 // be the last successor of a switch-terminated block.
2627
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002628 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2629 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002630
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002631 // This block is now the implicit successor of other blocks.
2632 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002633
2634 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00002635}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002636
Mike Stump5d1d2022010-01-19 02:20:09 +00002637CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2638 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2639 // current block.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002640 CFGBlock *TrySuccessor = NULL;
Mike Stump5d1d2022010-01-19 02:20:09 +00002641
2642 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002643 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002644 return 0;
2645 TrySuccessor = Block;
2646 } else TrySuccessor = Succ;
2647
Mike Stumpa1f93632010-01-20 01:15:34 +00002648 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00002649
2650 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002651 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00002652 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00002653 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00002654
Mike Stumpa1f93632010-01-20 01:15:34 +00002655 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00002656 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2657 // The code after the try is the implicit successor.
2658 Succ = TrySuccessor;
2659 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00002660 if (CS->getExceptionDecl() == 0) {
2661 HasCatchAll = true;
2662 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002663 Block = NULL;
2664 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2665 if (CatchBlock == 0)
2666 return 0;
2667 // Add this block to the list of successors for the block with the try
2668 // statement.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002669 addSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00002670 }
Mike Stumpa1f93632010-01-20 01:15:34 +00002671 if (!HasCatchAll) {
2672 if (PrevTryTerminatedBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002673 addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00002674 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002675 addSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00002676 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002677
2678 // The code after the try is the implicit successor.
2679 Succ = TrySuccessor;
2680
Mike Stumpf00cca52010-01-20 01:30:58 +00002681 // Save the current "try" context.
Ted Kremenekf0e71ae2011-08-23 23:05:07 +00002682 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock, NewTryTerminatedBlock);
2683 cfg->addTryDispatchBlock(TryTerminatedBlock);
Mike Stumpf00cca52010-01-20 01:30:58 +00002684
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002685 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00002686 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002687 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00002688 return Block;
2689}
2690
Ted Kremenek9c378f72011-08-12 23:37:29 +00002691CFGBlock *CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt *CS) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002692 // CXXCatchStmt are treated like labels, so they are the first statement in a
2693 // block.
2694
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002695 // Save local scope position because in case of exception variable ScopePos
2696 // won't be restored when traversing AST.
2697 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2698
2699 // Create local scope for possible exception variable.
2700 // Store scope position. Add implicit destructor.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002701 if (VarDecl *VD = CS->getExceptionDecl()) {
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002702 LocalScope::const_iterator BeginScopePos = ScopePos;
2703 addLocalScopeForVarDecl(VD);
2704 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2705 }
2706
Mike Stump5d1d2022010-01-19 02:20:09 +00002707 if (CS->getHandlerBlock())
2708 addStmt(CS->getHandlerBlock());
2709
Ted Kremenek9c378f72011-08-12 23:37:29 +00002710 CFGBlock *CatchBlock = Block;
Mike Stump5d1d2022010-01-19 02:20:09 +00002711 if (!CatchBlock)
2712 CatchBlock = createBlock();
Ted Kremenek337e4db2012-03-10 01:34:17 +00002713
2714 // CXXCatchStmt is more than just a label. They have semantic meaning
2715 // as well, as they implicitly "initialize" the catch variable. Add
2716 // it to the CFG as a CFGElement so that the control-flow of these
2717 // semantics gets captured.
2718 appendStmt(CatchBlock, CS);
Mike Stump5d1d2022010-01-19 02:20:09 +00002719
Ted Kremenek337e4db2012-03-10 01:34:17 +00002720 // Also add the CXXCatchStmt as a label, to mirror handling of regular
2721 // labels.
Mike Stump5d1d2022010-01-19 02:20:09 +00002722 CatchBlock->setLabel(CS);
2723
Ted Kremenek337e4db2012-03-10 01:34:17 +00002724 // Bail out if the CFG is bad.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002725 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002726 return 0;
2727
2728 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2729 Block = NULL;
2730
2731 return CatchBlock;
2732}
2733
Ted Kremenek9c378f72011-08-12 23:37:29 +00002734CFGBlock *CFGBuilder::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Richard Smithad762fc2011-04-14 22:09:26 +00002735 // C++0x for-range statements are specified as [stmt.ranged]:
2736 //
2737 // {
2738 // auto && __range = range-init;
2739 // for ( auto __begin = begin-expr,
2740 // __end = end-expr;
2741 // __begin != __end;
2742 // ++__begin ) {
2743 // for-range-declaration = *__begin;
2744 // statement
2745 // }
2746 // }
2747
2748 // Save local scope position before the addition of the implicit variables.
2749 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2750
2751 // Create local scopes and destructors for range, begin and end variables.
2752 if (Stmt *Range = S->getRangeStmt())
2753 addLocalScopeForStmt(Range);
2754 if (Stmt *BeginEnd = S->getBeginEndStmt())
2755 addLocalScopeForStmt(BeginEnd);
2756 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), S);
2757
2758 LocalScope::const_iterator ContinueScopePos = ScopePos;
2759
2760 // "for" is a control-flow statement. Thus we stop processing the current
2761 // block.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002762 CFGBlock *LoopSuccessor = NULL;
Richard Smithad762fc2011-04-14 22:09:26 +00002763 if (Block) {
2764 if (badCFG)
2765 return 0;
2766 LoopSuccessor = Block;
2767 } else
2768 LoopSuccessor = Succ;
2769
2770 // Save the current value for the break targets.
2771 // All breaks should go to the code following the loop.
2772 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
2773 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
2774
2775 // The block for the __begin != __end expression.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002776 CFGBlock *ConditionBlock = createBlock(false);
Richard Smithad762fc2011-04-14 22:09:26 +00002777 ConditionBlock->setTerminator(S);
2778
2779 // Now add the actual condition to the condition block.
2780 if (Expr *C = S->getCond()) {
2781 Block = ConditionBlock;
2782 CFGBlock *BeginConditionBlock = addStmt(C);
2783 if (badCFG)
2784 return 0;
2785 assert(BeginConditionBlock == ConditionBlock &&
2786 "condition block in for-range was unexpectedly complex");
2787 (void)BeginConditionBlock;
2788 }
2789
2790 // The condition block is the implicit successor for the loop body as well as
2791 // any code above the loop.
2792 Succ = ConditionBlock;
2793
2794 // See if this is a known constant.
2795 TryResult KnownVal(true);
2796
2797 if (S->getCond())
2798 KnownVal = tryEvaluateBool(S->getCond());
2799
2800 // Now create the loop body.
2801 {
2802 assert(S->getBody());
2803
2804 // Save the current values for Block, Succ, and continue targets.
2805 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2806 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
2807
2808 // Generate increment code in its own basic block. This is the target of
2809 // continue statements.
2810 Block = 0;
2811 Succ = addStmt(S->getInc());
2812 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
2813
2814 // The starting block for the loop increment is the block that should
2815 // represent the 'loop target' for looping back to the start of the loop.
2816 ContinueJumpTarget.block->setLoopTarget(S);
2817
2818 // Finish up the increment block and prepare to start the loop body.
2819 assert(Block);
2820 if (badCFG)
2821 return 0;
2822 Block = 0;
2823
2824
2825 // Add implicit scope and dtors for loop variable.
2826 addLocalScopeAndDtors(S->getLoopVarStmt());
2827
2828 // Populate a new block to contain the loop body and loop variable.
2829 Block = addStmt(S->getBody());
2830 if (badCFG)
2831 return 0;
2832 Block = addStmt(S->getLoopVarStmt());
2833 if (badCFG)
2834 return 0;
2835
2836 // This new body block is a successor to our condition block.
2837 addSuccessor(ConditionBlock, KnownVal.isFalse() ? 0 : Block);
2838 }
2839
2840 // Link up the condition block with the code that follows the loop (the
2841 // false branch).
2842 addSuccessor(ConditionBlock, KnownVal.isTrue() ? 0 : LoopSuccessor);
2843
2844 // Add the initialization statements.
2845 Block = createBlock();
Richard Smithb403d6d2011-04-18 15:49:25 +00002846 addStmt(S->getBeginEndStmt());
2847 return addStmt(S->getRangeStmt());
Richard Smithad762fc2011-04-14 22:09:26 +00002848}
2849
John McCall4765fa02010-12-06 08:20:24 +00002850CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +00002851 AddStmtChoice asc) {
2852 if (BuildOpts.AddImplicitDtors) {
2853 // If adding implicit destructors visit the full expression for adding
2854 // destructors of temporaries.
2855 VisitForTemporaryDtors(E->getSubExpr());
2856
2857 // Full expression has to be added as CFGStmt so it will be sequenced
2858 // before destructors of it's temporaries.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002859 asc = asc.withAlwaysAdd(true);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002860 }
2861 return Visit(E->getSubExpr(), asc);
2862}
2863
Zhongxing Xua725ed42010-11-01 13:04:58 +00002864CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2865 AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00002866 if (asc.alwaysAdd(*this, E)) {
Zhongxing Xua725ed42010-11-01 13:04:58 +00002867 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002868 appendStmt(Block, E);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002869
2870 // 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::VisitCXXConstructExpr(CXXConstructExpr *C,
2877 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002878 autoCreateBlock();
Zhongxing Xu97a72c32012-01-11 02:39:07 +00002879 appendStmt(Block, C);
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002880
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002881 return VisitChildren(C);
2882}
2883
Zhongxing Xua725ed42010-11-01 13:04:58 +00002884CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2885 AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00002886 if (asc.alwaysAdd(*this, E)) {
Zhongxing Xua725ed42010-11-01 13:04:58 +00002887 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002888 appendStmt(Block, E);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002889 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002890 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002891 }
2892 return Visit(E->getSubExpr(), asc);
2893}
2894
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002895CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2896 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002897 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002898 appendStmt(Block, C);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002899 return VisitChildren(C);
2900}
2901
Zhongxing Xua725ed42010-11-01 13:04:58 +00002902CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2903 AddStmtChoice asc) {
Ted Kremenek3179a452011-03-10 01:14:11 +00002904 if (asc.alwaysAdd(*this, E)) {
Zhongxing Xua725ed42010-11-01 13:04:58 +00002905 autoCreateBlock();
Ted Kremenek247e9662011-03-10 01:14:08 +00002906 appendStmt(Block, E);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002907 }
Ted Kremenek892697d2010-12-16 07:46:53 +00002908 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xua725ed42010-11-01 13:04:58 +00002909}
2910
Ted Kremenek9c378f72011-08-12 23:37:29 +00002911CFGBlock *CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt *I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002912 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002913 CFGBlock *IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002914
Ted Kremenek19bb3562007-08-28 19:26:49 +00002915 if (!IBlock) {
2916 IBlock = createBlock(false);
2917 cfg->setIndirectGotoBlock(IBlock);
2918 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002919
Ted Kremenek19bb3562007-08-28 19:26:49 +00002920 // IndirectGoto is a control-flow statement. Thus we stop processing the
2921 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002922 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002923 return 0;
2924
Ted Kremenek19bb3562007-08-28 19:26:49 +00002925 Block = createBlock(false);
2926 Block->setTerminator(I);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002927 addSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00002928 return addStmt(I->getTarget());
2929}
2930
Marcin Swiderski8599e762010-11-03 06:19:35 +00002931CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2932tryAgain:
2933 if (!E) {
2934 badCFG = true;
2935 return NULL;
2936 }
2937 switch (E->getStmtClass()) {
2938 default:
2939 return VisitChildrenForTemporaryDtors(E);
2940
2941 case Stmt::BinaryOperatorClass:
2942 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2943
2944 case Stmt::CXXBindTemporaryExprClass:
2945 return VisitCXXBindTemporaryExprForTemporaryDtors(
2946 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2947
John McCall56ca35d2011-02-17 10:25:35 +00002948 case Stmt::BinaryConditionalOperatorClass:
Marcin Swiderski8599e762010-11-03 06:19:35 +00002949 case Stmt::ConditionalOperatorClass:
2950 return VisitConditionalOperatorForTemporaryDtors(
John McCall56ca35d2011-02-17 10:25:35 +00002951 cast<AbstractConditionalOperator>(E), BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002952
2953 case Stmt::ImplicitCastExprClass:
2954 // For implicit cast we want BindToTemporary to be passed further.
2955 E = cast<CastExpr>(E)->getSubExpr();
2956 goto tryAgain;
2957
2958 case Stmt::ParenExprClass:
2959 E = cast<ParenExpr>(E)->getSubExpr();
2960 goto tryAgain;
Douglas Gregor03e80032011-06-21 17:03:29 +00002961
2962 case Stmt::MaterializeTemporaryExprClass:
2963 E = cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr();
2964 goto tryAgain;
Marcin Swiderski8599e762010-11-03 06:19:35 +00002965 }
2966}
2967
2968CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2969 // When visiting children for destructors we want to visit them in reverse
2970 // order. Because there's no reverse iterator for children must to reverse
2971 // them in helper vector.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002972 typedef SmallVector<Stmt *, 4> ChildrenVect;
Marcin Swiderski8599e762010-11-03 06:19:35 +00002973 ChildrenVect ChildrenRev;
John McCall7502c1d2011-02-13 04:07:26 +00002974 for (Stmt::child_range I = E->children(); I; ++I) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002975 if (*I) ChildrenRev.push_back(*I);
2976 }
2977
2978 CFGBlock *B = Block;
2979 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2980 L = ChildrenRev.rend(); I != L; ++I) {
2981 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2982 B = R;
2983 }
2984 return B;
2985}
2986
2987CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2988 if (E->isLogicalOp()) {
2989 // Destructors for temporaries in LHS expression should be called after
2990 // those for RHS expression. Even if this will unnecessarily create a block,
2991 // this block will be used at least by the full expression.
2992 autoCreateBlock();
2993 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2994 if (badCFG)
2995 return NULL;
2996
2997 Succ = ConfluenceBlock;
2998 Block = NULL;
2999 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
3000
3001 if (RHSBlock) {
3002 if (badCFG)
3003 return NULL;
3004
3005 // If RHS expression did produce destructors we need to connect created
3006 // blocks to CFG in same manner as for binary operator itself.
3007 CFGBlock *LHSBlock = createBlock(false);
3008 LHSBlock->setTerminator(CFGTerminator(E, true));
3009
3010 // For binary operator LHS block is before RHS in list of predecessors
3011 // of ConfluenceBlock.
3012 std::reverse(ConfluenceBlock->pred_begin(),
3013 ConfluenceBlock->pred_end());
3014
3015 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003016 TryResult KnownVal = tryEvaluateBool(E->getLHS());
Marcin Swiderski8599e762010-11-03 06:19:35 +00003017 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
3018 KnownVal.negate();
3019
3020 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
3021 // itself.
3022 if (E->getOpcode() == BO_LOr) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003023 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
3024 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003025 } else {
3026 assert (E->getOpcode() == BO_LAnd);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003027 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
3028 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003029 }
3030
3031 Block = LHSBlock;
3032 return LHSBlock;
3033 }
3034
3035 Block = ConfluenceBlock;
3036 return ConfluenceBlock;
3037 }
3038
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003039 if (E->isAssignmentOp()) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00003040 // For assignment operator (=) LHS expression is visited
3041 // before RHS expression. For destructors visit them in reverse order.
3042 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
3043 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
3044 return LHSBlock ? LHSBlock : RHSBlock;
3045 }
3046
3047 // For any other binary operator RHS expression is visited before
3048 // LHS expression (order of children). For destructors visit them in reverse
3049 // order.
3050 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
3051 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
3052 return RHSBlock ? RHSBlock : LHSBlock;
3053}
3054
3055CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
3056 CXXBindTemporaryExpr *E, bool BindToTemporary) {
3057 // First add destructors for temporaries in subexpression.
3058 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xu249c9452010-11-14 15:23:50 +00003059 if (!BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00003060 // If lifetime of temporary is not prolonged (by assigning to constant
3061 // reference) add destructor for it.
Chandler Carruthc8cfc742011-09-13 06:09:01 +00003062
3063 // If the destructor is marked as a no-return destructor, we need to create
3064 // a new block for the destructor which does not have as a successor
3065 // anything built thus far. Control won't flow out of this block.
3066 const CXXDestructorDecl *Dtor = E->getTemporary()->getDestructor();
Chandler Carruthdba3fb52011-09-13 09:13:49 +00003067 if (cast<FunctionType>(Dtor->getType())->getNoReturnAttr())
3068 Block = createNoReturnBlock();
3069 else
Chandler Carruthc8cfc742011-09-13 06:09:01 +00003070 autoCreateBlock();
Chandler Carruthc8cfc742011-09-13 06:09:01 +00003071
Marcin Swiderski8599e762010-11-03 06:19:35 +00003072 appendTemporaryDtor(Block, E);
3073 B = Block;
3074 }
3075 return B;
3076}
3077
3078CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
John McCall56ca35d2011-02-17 10:25:35 +00003079 AbstractConditionalOperator *E, bool BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00003080 // First add destructors for condition expression. Even if this will
3081 // unnecessarily create a block, this block will be used at least by the full
3082 // expression.
3083 autoCreateBlock();
3084 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
3085 if (badCFG)
3086 return NULL;
John McCall56ca35d2011-02-17 10:25:35 +00003087 if (BinaryConditionalOperator *BCO
3088 = dyn_cast<BinaryConditionalOperator>(E)) {
3089 ConfluenceBlock = VisitForTemporaryDtors(BCO->getCommon());
Marcin Swiderski8599e762010-11-03 06:19:35 +00003090 if (badCFG)
3091 return NULL;
3092 }
3093
John McCall56ca35d2011-02-17 10:25:35 +00003094 // Try to add block with destructors for LHS expression.
3095 CFGBlock *LHSBlock = NULL;
3096 Succ = ConfluenceBlock;
3097 Block = NULL;
3098 LHSBlock = VisitForTemporaryDtors(E->getTrueExpr(), BindToTemporary);
3099 if (badCFG)
3100 return NULL;
3101
Marcin Swiderski8599e762010-11-03 06:19:35 +00003102 // Try to add block with destructors for RHS expression;
3103 Succ = ConfluenceBlock;
3104 Block = NULL;
John McCall56ca35d2011-02-17 10:25:35 +00003105 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getFalseExpr(),
3106 BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003107 if (badCFG)
3108 return NULL;
3109
3110 if (!RHSBlock && !LHSBlock) {
3111 // If neither LHS nor RHS expression had temporaries to destroy don't create
3112 // more blocks.
3113 Block = ConfluenceBlock;
3114 return Block;
3115 }
3116
3117 Block = createBlock(false);
3118 Block->setTerminator(CFGTerminator(E, true));
3119
3120 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003121 const TryResult &KnownVal = tryEvaluateBool(E->getCond());
Marcin Swiderski8599e762010-11-03 06:19:35 +00003122
3123 if (LHSBlock) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003124 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003125 } else if (KnownVal.isFalse()) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003126 addSuccessor(Block, NULL);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003127 } else {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003128 addSuccessor(Block, ConfluenceBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003129 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
3130 }
3131
3132 if (!RHSBlock)
3133 RHSBlock = ConfluenceBlock;
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003134 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00003135
3136 return Block;
3137}
3138
Ted Kremenekbefef2f2007-08-23 21:26:19 +00003139} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00003140
Mike Stump6d9828c2009-07-17 01:31:16 +00003141/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
3142/// no successors or predecessors. If this is the first block created in the
3143/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003144CFGBlock *CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00003145 bool first_block = begin() == end();
3146
3147 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003148 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
Anna Zaks02f34c52011-12-05 21:33:11 +00003149 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC, this);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003150 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00003151
3152 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003153 if (first_block)
3154 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00003155
3156 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003157 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00003158}
3159
Ted Kremenek026473c2007-08-23 16:51:22 +00003160/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
3161/// CFG is returned to the caller.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003162CFG* CFG::buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +00003163 const BuildOptions &BO) {
3164 CFGBuilder Builder(C, BO);
3165 return Builder.buildCFG(D, Statement);
Ted Kremenek026473c2007-08-23 16:51:22 +00003166}
3167
Ted Kremenekc5aff442011-03-03 01:21:32 +00003168const CXXDestructorDecl *
3169CFGImplicitDtor::getDestructorDecl(ASTContext &astContext) const {
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003170 switch (getKind()) {
3171 case CFGElement::Invalid:
3172 case CFGElement::Statement:
3173 case CFGElement::Initializer:
3174 llvm_unreachable("getDestructorDecl should only be used with "
3175 "ImplicitDtors");
3176 case CFGElement::AutomaticObjectDtor: {
3177 const VarDecl *var = cast<CFGAutomaticObjDtor>(this)->getVarDecl();
3178 QualType ty = var->getType();
Ted Kremenek697d42d2011-03-03 01:01:03 +00003179 ty = ty.getNonReferenceType();
Ted Kremenek4cf22532012-03-19 23:48:41 +00003180 while (const ArrayType *arrayType = astContext.getAsArrayType(ty)) {
Ted Kremenekc5aff442011-03-03 01:21:32 +00003181 ty = arrayType->getElementType();
3182 }
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003183 const RecordType *recordType = ty->getAs<RecordType>();
3184 const CXXRecordDecl *classDecl =
Ted Kremenek697d42d2011-03-03 01:01:03 +00003185 cast<CXXRecordDecl>(recordType->getDecl());
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003186 return classDecl->getDestructor();
3187 }
3188 case CFGElement::TemporaryDtor: {
3189 const CXXBindTemporaryExpr *bindExpr =
3190 cast<CFGTemporaryDtor>(this)->getBindTemporaryExpr();
3191 const CXXTemporary *temp = bindExpr->getTemporary();
3192 return temp->getDestructor();
3193 }
3194 case CFGElement::BaseDtor:
3195 case CFGElement::MemberDtor:
3196
3197 // Not yet supported.
3198 return 0;
3199 }
Ted Kremenek697d42d2011-03-03 01:01:03 +00003200 llvm_unreachable("getKind() returned bogus value");
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003201}
3202
Ted Kremenekc5aff442011-03-03 01:21:32 +00003203bool CFGImplicitDtor::isNoReturn(ASTContext &astContext) const {
Francois Picheta08e7bc2012-06-06 12:00:10 +00003204 if (const CXXDestructorDecl *decl = getDestructorDecl(astContext)) {
3205 QualType ty = decl->getType();
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00003206 return cast<FunctionType>(ty)->getNoReturnAttr();
3207 }
3208 return false;
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003209}
3210
Ted Kremenek63f58872007-10-01 19:33:33 +00003211//===----------------------------------------------------------------------===//
3212// CFG: Queries for BlkExprs.
3213//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00003214
Ted Kremenek63f58872007-10-01 19:33:33 +00003215namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00003216 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00003217}
3218
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003219static void FindSubExprAssignments(const Stmt *S,
3220 llvm::SmallPtrSet<const Expr*,50>& Set) {
Ted Kremenek8a693662009-12-23 23:37:10 +00003221 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003222 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00003223
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003224 for (Stmt::const_child_range I = S->children(); I; ++I) {
3225 const Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00003226 if (!child)
3227 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00003228
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003229 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003230 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00003231
Ted Kremenek8a693662009-12-23 23:37:10 +00003232 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003233 }
3234}
3235
Ted Kremenek63f58872007-10-01 19:33:33 +00003236static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
3237 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00003238
3239 // Look for assignments that are used as subexpressions. These are the only
3240 // assignments that we want to *possibly* register as a block-level
3241 // expression. Basically, if an assignment occurs both in a subexpression and
3242 // at the block-level, it is a block-level expression.
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003243 llvm::SmallPtrSet<const Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00003244
Ted Kremenek63f58872007-10-01 19:33:33 +00003245 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003246 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003247 if (const CFGStmt *S = BI->getAs<CFGStmt>())
3248 FindSubExprAssignments(S->getStmt(), SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00003249
Ted Kremenek411cdee2008-04-16 21:10:48 +00003250 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003251
3252 // Iterate over the statements again on identify the Expr* and Stmt* at the
3253 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00003254
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00003255 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003256 const CFGStmt *CS = BI->getAs<CFGStmt>();
3257 if (!CS)
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00003258 continue;
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003259 if (const Expr *Exp = dyn_cast<Expr>(CS->getStmt())) {
Jordy Roseac73ea82011-06-10 08:49:37 +00003260 assert((Exp->IgnoreParens() == Exp) && "No parens on block-level exps");
Mike Stump6d9828c2009-07-17 01:31:16 +00003261
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003262 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003263 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00003264 // expression are really "statements" whose value is never used by
3265 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00003266 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003267 continue;
Ted Kremenek9c378f72011-08-12 23:37:29 +00003268 } else if (const StmtExpr *SE = dyn_cast<StmtExpr>(Exp)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003269 // Special handling for statement expressions. The last statement in
3270 // the statement expression is also a block-level expr.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003271 const CompoundStmt *C = SE->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00003272 if (!C->body_empty()) {
Jordy Roseac73ea82011-06-10 08:49:37 +00003273 const Stmt *Last = C->body_back();
3274 if (const Expr *LastEx = dyn_cast<Expr>(Last))
3275 Last = LastEx->IgnoreParens();
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003276 unsigned x = M->size();
Jordy Roseac73ea82011-06-10 08:49:37 +00003277 (*M)[Last] = x;
Ted Kremenek86946742008-01-17 20:48:37 +00003278 }
3279 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00003280
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003281 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00003282 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00003283 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00003284 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003285
Ted Kremenek411cdee2008-04-16 21:10:48 +00003286 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00003287
Ted Kremenek9c378f72011-08-12 23:37:29 +00003288 Stmt *S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00003289
Ted Kremenek390e48b2008-11-12 21:11:49 +00003290 if (S && M->find(S) == M->end()) {
Jordy Roseac73ea82011-06-10 08:49:37 +00003291 unsigned x = M->size();
3292 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003293 }
3294 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003295
Ted Kremenek63f58872007-10-01 19:33:33 +00003296 return M;
3297}
3298
Ted Kremenek9c378f72011-08-12 23:37:29 +00003299CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt *S) {
Ted Kremenek86946742008-01-17 20:48:37 +00003300 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00003301 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00003302
Ted Kremenek63f58872007-10-01 19:33:33 +00003303 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00003304 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003305 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00003306}
3307
3308unsigned CFG::getNumBlkExprs() {
3309 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
3310 return M->size();
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003311
3312 // We assume callers interested in the number of BlkExprs will want
3313 // the map constructed if it doesn't already exist.
3314 BlkExprMap = (void*) PopulateBlkExprMap(*this);
3315 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenek63f58872007-10-01 19:33:33 +00003316}
3317
Ted Kremenek274f4332008-04-28 18:00:46 +00003318//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003319// Filtered walking of the CFG.
3320//===----------------------------------------------------------------------===//
3321
3322bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00003323 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003324
Ted Kremenek6e400352011-03-07 22:04:39 +00003325 if (To && F.IgnoreDefaultsWithCoveredEnums) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003326 // If the 'To' has no label or is labeled but the label isn't a
3327 // CaseStmt then filter this edge.
3328 if (const SwitchStmt *S =
Ted Kremenek6e400352011-03-07 22:04:39 +00003329 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003330 if (S->isAllEnumCasesCovered()) {
Ted Kremenek6e400352011-03-07 22:04:39 +00003331 const Stmt *L = To->getLabel();
3332 if (!L || !isa<CaseStmt>(L))
3333 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00003334 }
3335 }
3336 }
3337
3338 return false;
3339}
3340
3341//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00003342// Cleanup: CFG dstor.
3343//===----------------------------------------------------------------------===//
3344
Ted Kremenek63f58872007-10-01 19:33:33 +00003345CFG::~CFG() {
3346 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
3347}
Mike Stump6d9828c2009-07-17 01:31:16 +00003348
Ted Kremenek7dba8602007-08-29 21:56:09 +00003349//===----------------------------------------------------------------------===//
3350// CFG pretty printing
3351//===----------------------------------------------------------------------===//
3352
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00003353namespace {
3354
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00003355class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003356 typedef llvm::DenseMap<const Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
3357 typedef llvm::DenseMap<const Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003358 StmtMapTy StmtMap;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003359 DeclMapTy DeclMap;
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003360 signed currentBlock;
3361 unsigned currentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00003362 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003363public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003364
Chris Lattnere4f21422009-06-30 01:26:17 +00003365 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003366 : currentBlock(0), currentStmt(0), LangOpts(LO)
3367 {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003368 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
3369 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003370 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003371 BI != BEnd; ++BI, ++j ) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003372 if (const CFGStmt *SE = BI->getAs<CFGStmt>()) {
3373 const Stmt *stmt= SE->getStmt();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003374 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003375 StmtMap[stmt] = P;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003376
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003377 switch (stmt->getStmtClass()) {
3378 case Stmt::DeclStmtClass:
3379 DeclMap[cast<DeclStmt>(stmt)->getSingleDecl()] = P;
3380 break;
3381 case Stmt::IfStmtClass: {
3382 const VarDecl *var = cast<IfStmt>(stmt)->getConditionVariable();
3383 if (var)
3384 DeclMap[var] = P;
3385 break;
3386 }
3387 case Stmt::ForStmtClass: {
3388 const VarDecl *var = cast<ForStmt>(stmt)->getConditionVariable();
3389 if (var)
3390 DeclMap[var] = P;
3391 break;
3392 }
3393 case Stmt::WhileStmtClass: {
3394 const VarDecl *var =
3395 cast<WhileStmt>(stmt)->getConditionVariable();
3396 if (var)
3397 DeclMap[var] = P;
3398 break;
3399 }
3400 case Stmt::SwitchStmtClass: {
3401 const VarDecl *var =
3402 cast<SwitchStmt>(stmt)->getConditionVariable();
3403 if (var)
3404 DeclMap[var] = P;
3405 break;
3406 }
3407 case Stmt::CXXCatchStmtClass: {
3408 const VarDecl *var =
3409 cast<CXXCatchStmt>(stmt)->getExceptionDecl();
3410 if (var)
3411 DeclMap[var] = P;
3412 break;
3413 }
3414 default:
3415 break;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003416 }
3417 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003418 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00003419 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003420 }
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003421
Mike Stump6d9828c2009-07-17 01:31:16 +00003422
Ted Kremenek42a509f2007-08-31 21:30:12 +00003423 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00003424
Chris Lattnere4f21422009-06-30 01:26:17 +00003425 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003426 void setBlockID(signed i) { currentBlock = i; }
3427 void setStmtID(unsigned i) { currentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00003428
Ted Kremenek9c378f72011-08-12 23:37:29 +00003429 virtual bool handledStmt(Stmt *S, raw_ostream &OS) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003430 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003431
3432 if (I == StmtMap.end())
3433 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003434
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003435 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3436 && I->second.second == currentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003437 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003438 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003439
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003440 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003441 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003442 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003443
Ted Kremenek9c378f72011-08-12 23:37:29 +00003444 bool handleDecl(const Decl *D, raw_ostream &OS) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003445 DeclMapTy::iterator I = DeclMap.find(D);
3446
3447 if (I == DeclMap.end())
3448 return false;
3449
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003450 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3451 && I->second.second == currentStmt) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003452 return false;
3453 }
3454
3455 OS << "[B" << I->second.first << "." << I->second.second << "]";
3456 return true;
3457 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003458};
Chris Lattnere4f21422009-06-30 01:26:17 +00003459} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00003460
Chris Lattnere4f21422009-06-30 01:26:17 +00003461
3462namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00003463class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00003464 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00003465
Ted Kremenek9c378f72011-08-12 23:37:29 +00003466 raw_ostream &OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003467 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003468 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003469public:
Ted Kremenek9c378f72011-08-12 23:37:29 +00003470 CFGBlockTerminatorPrint(raw_ostream &os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00003471 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003472 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00003473
Ted Kremenek9c378f72011-08-12 23:37:29 +00003474 void VisitIfStmt(IfStmt *I) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003475 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003476 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003477 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003478
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003479 // Default case.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003480 void VisitStmt(Stmt *Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003481 Terminator->printPretty(OS, Helper, Policy);
3482 }
3483
Ted Kremenek9c378f72011-08-12 23:37:29 +00003484 void VisitForStmt(ForStmt *F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003485 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003486 if (F->getInit())
3487 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00003488 OS << "; ";
Ted Kremenek9c378f72011-08-12 23:37:29 +00003489 if (Stmt *C = F->getCond())
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003490 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00003491 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003492 if (F->getInc())
3493 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00003494 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003495 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003496
Ted Kremenek9c378f72011-08-12 23:37:29 +00003497 void VisitWhileStmt(WhileStmt *W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003498 OS << "while " ;
Ted Kremenek9c378f72011-08-12 23:37:29 +00003499 if (Stmt *C = W->getCond())
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003500 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003501 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003502
Ted Kremenek9c378f72011-08-12 23:37:29 +00003503 void VisitDoStmt(DoStmt *D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003504 OS << "do ... while ";
Ted Kremenek9c378f72011-08-12 23:37:29 +00003505 if (Stmt *C = D->getCond())
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003506 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00003507 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003508
Ted Kremenek9c378f72011-08-12 23:37:29 +00003509 void VisitSwitchStmt(SwitchStmt *Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00003510 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003511 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00003512 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003513
Ted Kremenek9c378f72011-08-12 23:37:29 +00003514 void VisitCXXTryStmt(CXXTryStmt *CS) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003515 OS << "try ...";
3516 }
3517
John McCall56ca35d2011-02-17 10:25:35 +00003518 void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003519 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003520 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003521 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003522
Ted Kremenek9c378f72011-08-12 23:37:29 +00003523 void VisitChooseExpr(ChooseExpr *C) {
Ted Kremenekaeddbf62007-08-31 22:29:13 +00003524 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003525 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00003526 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00003527 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003528
Ted Kremenek9c378f72011-08-12 23:37:29 +00003529 void VisitIndirectGotoStmt(IndirectGotoStmt *I) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003530 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003531 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003532 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003533
Ted Kremenek805e9a82007-08-31 21:49:40 +00003534 void VisitBinaryOperator(BinaryOperator* B) {
3535 if (!B->isLogicalOp()) {
3536 VisitExpr(B);
3537 return;
3538 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003539
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003540 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003541
Ted Kremenek805e9a82007-08-31 21:49:40 +00003542 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00003543 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00003544 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003545 return;
John McCall2de56d12010-08-25 11:45:40 +00003546 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00003547 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003548 return;
3549 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003550 llvm_unreachable("Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003551 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00003552 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003553
Ted Kremenek9c378f72011-08-12 23:37:29 +00003554 void VisitExpr(Expr *E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003555 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003556 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003557};
Chris Lattnere4f21422009-06-30 01:26:17 +00003558} // end anonymous namespace
3559
Chris Lattner5f9e2722011-07-23 10:55:15 +00003560static void print_elem(raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00003561 const CFGElement &E) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003562 if (const CFGStmt *CS = E.getAs<CFGStmt>()) {
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003563 const Stmt *S = CS->getStmt();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003564
3565 if (Helper) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003566
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003567 // special printing for statement-expressions.
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003568 if (const StmtExpr *SE = dyn_cast<StmtExpr>(S)) {
3569 const CompoundStmt *Sub = SE->getSubStmt();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003570
John McCall7502c1d2011-02-13 04:07:26 +00003571 if (Sub->children()) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003572 OS << "({ ... ; ";
3573 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3574 OS << " })\n";
3575 return;
3576 }
3577 }
3578 // special printing for comma expressions.
Ted Kremenekf1d10d92011-08-23 23:05:04 +00003579 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003580 if (B->getOpcode() == BO_Comma) {
3581 OS << "... , ";
3582 Helper->handledStmt(B->getRHS(),OS);
3583 OS << '\n';
3584 return;
3585 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003586 }
3587 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003588 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00003589
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003590 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003591 OS << " (OperatorCall)";
Ted Kremenek893d4142011-12-21 19:32:38 +00003592 }
3593 else if (isa<CXXBindTemporaryExpr>(S)) {
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003594 OS << " (BindTemporary)";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003595 }
Ted Kremenek3b7a48f2011-12-21 19:39:59 +00003596 else if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(S)) {
3597 OS << " (CXXConstructExpr, " << CCE->getType().getAsString() << ")";
3598 }
Ted Kremenek893d4142011-12-21 19:32:38 +00003599 else if (const CastExpr *CE = dyn_cast<CastExpr>(S)) {
3600 OS << " (" << CE->getStmtClassName() << ", "
3601 << CE->getCastKindName()
3602 << ", " << CE->getType().getAsString()
3603 << ")";
3604 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003605
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003606 // Expressions need a newline.
3607 if (isa<Expr>(S))
3608 OS << '\n';
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00003609
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003610 } else if (const CFGInitializer *IE = E.getAs<CFGInitializer>()) {
3611 const CXXCtorInitializer *I = IE->getInitializer();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003612 if (I->isBaseInitializer())
3613 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichet00eb3f92010-12-04 09:14:42 +00003614 else OS << I->getAnyMember()->getName();
Mike Stump6d9828c2009-07-17 01:31:16 +00003615
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003616 OS << "(";
Ted Kremenek9c378f72011-08-12 23:37:29 +00003617 if (Expr *IE = I->getInit())
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003618 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3619 OS << ")";
3620
3621 if (I->isBaseInitializer())
3622 OS << " (Base initializer)\n";
3623 else OS << " (Member initializer)\n";
3624
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003625 } else if (const CFGAutomaticObjDtor *DE = E.getAs<CFGAutomaticObjDtor>()){
Ted Kremenek9c378f72011-08-12 23:37:29 +00003626 const VarDecl *VD = DE->getVarDecl();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003627 Helper->handleDecl(VD, OS);
3628
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003629 const Type* T = VD->getType().getTypePtr();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003630 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3631 T = RT->getPointeeType().getTypePtr();
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003632 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3633 T = ET;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003634
3635 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3636 OS << " (Implicit destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003637
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003638 } else if (const CFGBaseDtor *BE = E.getAs<CFGBaseDtor>()) {
3639 const CXXBaseSpecifier *BS = BE->getBaseSpecifier();
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003640 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003641 OS << " (Base object destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003642
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003643 } else if (const CFGMemberDtor *ME = E.getAs<CFGMemberDtor>()) {
3644 const FieldDecl *FD = ME->getFieldDecl();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003645
3646 const Type *T = FD->getType().getTypePtr();
3647 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3648 T = ET;
3649
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003650 OS << "this->" << FD->getName();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003651 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003652 OS << " (Member object destructor)\n";
Marcin Swiderski8599e762010-11-03 06:19:35 +00003653
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003654 } else if (const CFGTemporaryDtor *TE = E.getAs<CFGTemporaryDtor>()) {
3655 const CXXBindTemporaryExpr *BT = TE->getBindTemporaryExpr();
Marcin Swiderski8599e762010-11-03 06:19:35 +00003656 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3657 OS << " (Temporary object destructor)\n";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003658 }
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00003659}
Mike Stump6d9828c2009-07-17 01:31:16 +00003660
Ted Kremenek9c378f72011-08-12 23:37:29 +00003661static void print_block(raw_ostream &OS, const CFG* cfg,
3662 const CFGBlock &B,
Ted Kremenek682060c2011-12-22 23:33:52 +00003663 StmtPrinterHelper* Helper, bool print_edges,
3664 bool ShowColors) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003665
Ted Kremenek682060c2011-12-22 23:33:52 +00003666 if (Helper)
3667 Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00003668
Ted Kremenek7dba8602007-08-29 21:56:09 +00003669 // Print the header.
Ted Kremenek682060c2011-12-22 23:33:52 +00003670 if (ShowColors)
3671 OS.changeColor(raw_ostream::YELLOW, true);
3672
3673 OS << "\n [B" << B.getBlockID();
Mike Stump6d9828c2009-07-17 01:31:16 +00003674
Ted Kremenek42a509f2007-08-31 21:30:12 +00003675 if (&B == &cfg->getEntry())
Ted Kremenek682060c2011-12-22 23:33:52 +00003676 OS << " (ENTRY)]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003677 else if (&B == &cfg->getExit())
Ted Kremenek682060c2011-12-22 23:33:52 +00003678 OS << " (EXIT)]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003679 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek682060c2011-12-22 23:33:52 +00003680 OS << " (INDIRECT GOTO DISPATCH)]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003681 else
Ted Kremenek682060c2011-12-22 23:33:52 +00003682 OS << "]\n";
3683
3684 if (ShowColors)
3685 OS.resetColor();
Mike Stump6d9828c2009-07-17 01:31:16 +00003686
Ted Kremenek9cffe732007-08-29 23:20:49 +00003687 // Print the label of this block.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003688 if (Stmt *Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003689
3690 if (print_edges)
Ted Kremenek682060c2011-12-22 23:33:52 +00003691 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003692
Ted Kremenek9c378f72011-08-12 23:37:29 +00003693 if (LabelStmt *L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003694 OS << L->getName();
Ted Kremenek9c378f72011-08-12 23:37:29 +00003695 else if (CaseStmt *C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00003696 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003697 C->getLHS()->printPretty(OS, Helper,
3698 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003699 if (C->getRHS()) {
3700 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003701 C->getRHS()->printPretty(OS, Helper,
3702 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003703 }
Mike Stump079bd722010-01-19 22:00:14 +00003704 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003705 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00003706 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003707 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00003708 if (CS->getExceptionDecl())
3709 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3710 0);
3711 else
3712 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00003713 OS << ")";
3714
3715 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00003716 llvm_unreachable("Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003717
Ted Kremenek9cffe732007-08-29 23:20:49 +00003718 OS << ":\n";
3719 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003720
Ted Kremenekfddd5182007-08-21 21:42:03 +00003721 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00003722 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00003723
Ted Kremenek42a509f2007-08-31 21:30:12 +00003724 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3725 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003726
Ted Kremenek9cffe732007-08-29 23:20:49 +00003727 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003728 if (print_edges)
Ted Kremenek682060c2011-12-22 23:33:52 +00003729 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003730
Ted Kremeneka95d3752008-09-13 05:16:45 +00003731 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003732
Ted Kremenek42a509f2007-08-31 21:30:12 +00003733 if (Helper)
3734 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00003735
Ted Kremenek682060c2011-12-22 23:33:52 +00003736 print_elem(OS, Helper, *I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00003737 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003738
Ted Kremenek9cffe732007-08-29 23:20:49 +00003739 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003740 if (B.getTerminator()) {
Ted Kremenek682060c2011-12-22 23:33:52 +00003741 if (ShowColors)
3742 OS.changeColor(raw_ostream::GREEN);
Mike Stump6d9828c2009-07-17 01:31:16 +00003743
Ted Kremenek682060c2011-12-22 23:33:52 +00003744 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003745
Ted Kremenek42a509f2007-08-31 21:30:12 +00003746 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00003747
Chris Lattnere4f21422009-06-30 01:26:17 +00003748 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3749 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003750 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003751 OS << '\n';
Ted Kremenek682060c2011-12-22 23:33:52 +00003752
3753 if (ShowColors)
3754 OS.resetColor();
Ted Kremenekfddd5182007-08-21 21:42:03 +00003755 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003756
Ted Kremenek9cffe732007-08-29 23:20:49 +00003757 if (print_edges) {
3758 // Print the predecessors of this block.
Ted Kremenek682060c2011-12-22 23:33:52 +00003759 if (!B.pred_empty()) {
3760 const raw_ostream::Colors Color = raw_ostream::BLUE;
3761 if (ShowColors)
3762 OS.changeColor(Color);
3763 OS << " Preds " ;
3764 if (ShowColors)
3765 OS.resetColor();
3766 OS << '(' << B.pred_size() << "):";
3767 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00003768
Ted Kremenek682060c2011-12-22 23:33:52 +00003769 if (ShowColors)
3770 OS.changeColor(Color);
3771
3772 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3773 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003774
Ted Kremenek682060c2011-12-22 23:33:52 +00003775 if (i == 8 || (i-8) == 0)
3776 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003777
Ted Kremenek682060c2011-12-22 23:33:52 +00003778 OS << " B" << (*I)->getBlockID();
3779 }
3780
3781 if (ShowColors)
3782 OS.resetColor();
3783
3784 OS << '\n';
Ted Kremenek9cffe732007-08-29 23:20:49 +00003785 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003786
Ted Kremenek42a509f2007-08-31 21:30:12 +00003787 // Print the successors of this block.
Ted Kremenek682060c2011-12-22 23:33:52 +00003788 if (!B.succ_empty()) {
3789 const raw_ostream::Colors Color = raw_ostream::MAGENTA;
3790 if (ShowColors)
3791 OS.changeColor(Color);
3792 OS << " Succs ";
3793 if (ShowColors)
3794 OS.resetColor();
3795 OS << '(' << B.succ_size() << "):";
3796 unsigned i = 0;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003797
Ted Kremenek682060c2011-12-22 23:33:52 +00003798 if (ShowColors)
3799 OS.changeColor(Color);
Mike Stump6d9828c2009-07-17 01:31:16 +00003800
Ted Kremenek682060c2011-12-22 23:33:52 +00003801 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3802 I != E; ++I, ++i) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003803
Ted Kremenek682060c2011-12-22 23:33:52 +00003804 if (i == 8 || (i-8) % 10 == 0)
3805 OS << "\n ";
3806
3807 if (*I)
3808 OS << " B" << (*I)->getBlockID();
3809 else
3810 OS << " NULL";
3811 }
3812
3813 if (ShowColors)
3814 OS.resetColor();
3815 OS << '\n';
Ted Kremenek42a509f2007-08-31 21:30:12 +00003816 }
Ted Kremenekfddd5182007-08-21 21:42:03 +00003817 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003818}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003819
Ted Kremenek42a509f2007-08-31 21:30:12 +00003820
3821/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek682060c2011-12-22 23:33:52 +00003822void CFG::dump(const LangOptions &LO, bool ShowColors) const {
3823 print(llvm::errs(), LO, ShowColors);
3824}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003825
3826/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenek682060c2011-12-22 23:33:52 +00003827void CFG::print(raw_ostream &OS, const LangOptions &LO, bool ShowColors) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003828 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00003829
Ted Kremenek42a509f2007-08-31 21:30:12 +00003830 // Print the entry block.
Ted Kremenek682060c2011-12-22 23:33:52 +00003831 print_block(OS, this, getEntry(), &Helper, true, ShowColors);
Mike Stump6d9828c2009-07-17 01:31:16 +00003832
Ted Kremenek42a509f2007-08-31 21:30:12 +00003833 // Iterate through the CFGBlocks and print them one by one.
3834 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3835 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003836 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00003837 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00003838
Ted Kremenek682060c2011-12-22 23:33:52 +00003839 print_block(OS, this, **I, &Helper, true, ShowColors);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003840 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003841
Ted Kremenek42a509f2007-08-31 21:30:12 +00003842 // Print the exit block.
Ted Kremenek682060c2011-12-22 23:33:52 +00003843 print_block(OS, this, getExit(), &Helper, true, ShowColors);
3844 OS << '\n';
Ted Kremenekd0172432008-11-24 20:50:24 +00003845 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00003846}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003847
3848/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek682060c2011-12-22 23:33:52 +00003849void CFGBlock::dump(const CFG* cfg, const LangOptions &LO,
3850 bool ShowColors) const {
3851 print(llvm::errs(), cfg, LO, ShowColors);
Chris Lattnere4f21422009-06-30 01:26:17 +00003852}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003853
3854/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3855/// Generally this will only be called from CFG::print.
Ted Kremenek9c378f72011-08-12 23:37:29 +00003856void CFGBlock::print(raw_ostream &OS, const CFG* cfg,
Ted Kremenek682060c2011-12-22 23:33:52 +00003857 const LangOptions &LO, bool ShowColors) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003858 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek682060c2011-12-22 23:33:52 +00003859 print_block(OS, cfg, *this, &Helper, true, ShowColors);
3860 OS << '\n';
Ted Kremenek026473c2007-08-23 16:51:22 +00003861}
Ted Kremenek7dba8602007-08-29 21:56:09 +00003862
Ted Kremeneka2925852008-01-30 23:02:42 +00003863/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003864void CFGBlock::printTerminator(raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00003865 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003866 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003867 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003868}
3869
Ted Kremenek9c378f72011-08-12 23:37:29 +00003870Stmt *CFGBlock::getTerminatorCondition() {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003871 Stmt *Terminator = this->Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003872 if (!Terminator)
3873 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003874
Ted Kremenek9c378f72011-08-12 23:37:29 +00003875 Expr *E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003876
Ted Kremenek411cdee2008-04-16 21:10:48 +00003877 switch (Terminator->getStmtClass()) {
3878 default:
3879 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003880
Ted Kremenek411cdee2008-04-16 21:10:48 +00003881 case Stmt::ForStmtClass:
3882 E = cast<ForStmt>(Terminator)->getCond();
3883 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003884
Ted Kremenek411cdee2008-04-16 21:10:48 +00003885 case Stmt::WhileStmtClass:
3886 E = cast<WhileStmt>(Terminator)->getCond();
3887 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003888
Ted Kremenek411cdee2008-04-16 21:10:48 +00003889 case Stmt::DoStmtClass:
3890 E = cast<DoStmt>(Terminator)->getCond();
3891 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003892
Ted Kremenek411cdee2008-04-16 21:10:48 +00003893 case Stmt::IfStmtClass:
3894 E = cast<IfStmt>(Terminator)->getCond();
3895 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003896
Ted Kremenek411cdee2008-04-16 21:10:48 +00003897 case Stmt::ChooseExprClass:
3898 E = cast<ChooseExpr>(Terminator)->getCond();
3899 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003900
Ted Kremenek411cdee2008-04-16 21:10:48 +00003901 case Stmt::IndirectGotoStmtClass:
3902 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3903 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003904
Ted Kremenek411cdee2008-04-16 21:10:48 +00003905 case Stmt::SwitchStmtClass:
3906 E = cast<SwitchStmt>(Terminator)->getCond();
3907 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003908
John McCall56ca35d2011-02-17 10:25:35 +00003909 case Stmt::BinaryConditionalOperatorClass:
3910 E = cast<BinaryConditionalOperator>(Terminator)->getCond();
3911 break;
3912
Ted Kremenek411cdee2008-04-16 21:10:48 +00003913 case Stmt::ConditionalOperatorClass:
3914 E = cast<ConditionalOperator>(Terminator)->getCond();
3915 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003916
Ted Kremenek411cdee2008-04-16 21:10:48 +00003917 case Stmt::BinaryOperatorClass: // '&&' and '||'
3918 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00003919 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003920
Ted Kremenek390e48b2008-11-12 21:11:49 +00003921 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003922 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003923 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003924
Ted Kremenek411cdee2008-04-16 21:10:48 +00003925 return E ? E->IgnoreParens() : NULL;
3926}
3927
Ted Kremenek7dba8602007-08-29 21:56:09 +00003928//===----------------------------------------------------------------------===//
3929// CFG Graphviz Visualization
3930//===----------------------------------------------------------------------===//
3931
Ted Kremenek42a509f2007-08-31 21:30:12 +00003932
3933#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00003934static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003935#endif
3936
Chris Lattnere4f21422009-06-30 01:26:17 +00003937void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003938#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00003939 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003940 GraphHelper = &H;
3941 llvm::ViewGraph(this,"CFG");
3942 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003943#endif
3944}
3945
Ted Kremenek7dba8602007-08-29 21:56:09 +00003946namespace llvm {
3947template<>
3948struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00003949
3950 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3951
Ted Kremenek9c378f72011-08-12 23:37:29 +00003952 static std::string getNodeLabel(const CFGBlock *Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00003953
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003954#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00003955 std::string OutSStr;
3956 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek682060c2011-12-22 23:33:52 +00003957 print_block(Out,Graph, *Node, GraphHelper, false, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003958 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00003959
3960 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3961
3962 // Process string output to make it nicer...
3963 for (unsigned i = 0; i != OutStr.length(); ++i)
3964 if (OutStr[i] == '\n') { // Left justify
3965 OutStr[i] = '\\';
3966 OutStr.insert(OutStr.begin()+i+1, 'l');
3967 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003968
Ted Kremenek7dba8602007-08-29 21:56:09 +00003969 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003970#else
3971 return "";
3972#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00003973 }
3974};
3975} // end namespace llvm