blob: 0f5b1fae5cf083011fef5a49801dce0c6b2af864 [file] [log] [blame]
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekb1c170e2009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek6796fbd2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stump6bf1c082010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Ted Kremenek1a241d12011-02-23 05:11:46 +000020#include "clang/AST/CharUnits.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000021#include "llvm/Support/GraphWriter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000022#include "llvm/Support/Allocator.h"
23#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000024#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000025#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek8aed4902009-10-20 23:46:25 +000026#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000027
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000028using namespace clang;
29
30namespace {
31
Douglas Gregor6e6ad602009-01-20 01:17:11 +000032static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000033 if (VarDecl* VD = dyn_cast<VarDecl>(D))
34 if (Expr* Ex = VD->getInit())
35 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000036 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000037}
Ted Kremenekdc03bd02010-08-02 23:46:59 +000038
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000039/// The CFG builder uses a recursive algorithm to build the CFG. When
40/// we process an expression, sometimes we know that we must add the
41/// subexpressions as block-level expressions. For example:
42///
43/// exp1 || exp2
44///
45/// When processing the '||' expression, we know that exp1 and exp2
46/// need to be added as block-level expressions, even though they
47/// might not normally need to be. AddStmtChoice records this
48/// contextual information. If AddStmtChoice is 'NotAlwaysAdd', then
49/// the builder has an option not to add a subexpression as a
50/// block-level expression.
51///
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000052class AddStmtChoice {
53public:
Ted Kremenek8219b822010-12-16 07:46:53 +000054 enum Kind { NotAlwaysAdd = 0, AlwaysAdd = 1 };
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000055
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000056 AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {}
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000057
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000058 bool alwaysAdd() const { return kind & AlwaysAdd; }
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000059
60 /// Return a copy of this object, except with the 'always-add' bit
61 /// set as specified.
62 AddStmtChoice withAlwaysAdd(bool alwaysAdd) const {
63 return AddStmtChoice(alwaysAdd ? Kind(kind | AlwaysAdd) :
64 Kind(kind & ~AlwaysAdd));
65 }
66
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000067private:
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000068 Kind kind;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000069};
Mike Stump31feda52009-07-17 01:31:16 +000070
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000071/// LocalScope - Node in tree of local scopes created for C++ implicit
72/// destructor calls generation. It contains list of automatic variables
73/// declared in the scope and link to position in previous scope this scope
74/// began in.
75///
76/// The process of creating local scopes is as follows:
77/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
78/// - Before processing statements in scope (e.g. CompoundStmt) create
79/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
80/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000081/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000082/// at this VarDecl,
83/// - For every normal (without jump) end of scope add to CFGBlock destructors
84/// for objects in the current scope,
85/// - For every jump add to CFGBlock destructors for objects
86/// between CFGBuilder::ScopePos and local scope position saved for jump
87/// target. Thanks to C++ restrictions on goto jumps we can be sure that
88/// jump target position will be on the path to root from CFGBuilder::ScopePos
89/// (adding any variable that doesn't need constructor to be called to
90/// LocalScope can break this assumption),
91///
92class LocalScope {
93public:
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +000094 typedef BumpVector<VarDecl*> AutomaticVarsTy;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000095
96 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000097 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000098 class const_iterator {
99 const LocalScope* Scope;
100
101 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
102 /// Invalid iterator (with null Scope) has VarIter equal to 0.
103 unsigned VarIter;
104
105 public:
106 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
107 /// Incrementing invalid iterator is allowed and will result in invalid
108 /// iterator.
109 const_iterator()
110 : Scope(NULL), VarIter(0) {}
111
112 /// Create valid iterator. In case when S.Prev is an invalid iterator and
113 /// I is equal to 0, this will create invalid iterator.
114 const_iterator(const LocalScope& S, unsigned I)
115 : Scope(&S), VarIter(I) {
116 // Iterator to "end" of scope is not allowed. Handle it by going up
117 // in scopes tree possibly up to invalid iterator in the root.
118 if (VarIter == 0 && Scope)
119 *this = Scope->Prev;
120 }
121
122 VarDecl* const* operator->() const {
123 assert (Scope && "Dereferencing invalid iterator is not allowed");
124 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
125 return &Scope->Vars[VarIter - 1];
126 }
127 VarDecl* operator*() const {
128 return *this->operator->();
129 }
130
131 const_iterator& operator++() {
132 if (!Scope)
133 return *this;
134
135 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
136 --VarIter;
137 if (VarIter == 0)
138 *this = Scope->Prev;
139 return *this;
140 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000141 const_iterator operator++(int) {
142 const_iterator P = *this;
143 ++*this;
144 return P;
145 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000146
147 bool operator==(const const_iterator& rhs) const {
148 return Scope == rhs.Scope && VarIter == rhs.VarIter;
149 }
150 bool operator!=(const const_iterator& rhs) const {
151 return !(*this == rhs);
152 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000153
154 operator bool() const {
155 return *this != const_iterator();
156 }
157
158 int distance(const_iterator L);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000159 };
160
161 friend class const_iterator;
162
163private:
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000164 BumpVectorContext ctx;
165
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000166 /// Automatic variables in order of declaration.
167 AutomaticVarsTy Vars;
168 /// Iterator to variable in previous scope that was declared just before
169 /// begin of this scope.
170 const_iterator Prev;
171
172public:
173 /// Constructs empty scope linked to previous scope in specified place.
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000174 LocalScope(BumpVectorContext &ctx, const_iterator P)
175 : ctx(ctx), Vars(ctx, 4), Prev(P) {}
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000176
177 /// Begin of scope in direction of CFG building (backwards).
178 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000179
180 void addVar(VarDecl* VD) {
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000181 Vars.push_back(VD, ctx);
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000182 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000183};
184
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000185/// distance - Calculates distance from this to L. L must be reachable from this
186/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
187/// number of scopes between this and L.
188int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
189 int D = 0;
190 const_iterator F = *this;
191 while (F.Scope != L.Scope) {
192 assert (F != const_iterator()
193 && "L iterator is not reachable from F iterator.");
194 D += F.VarIter;
195 F = F.Scope->Prev;
196 }
197 D += F.VarIter - L.VarIter;
198 return D;
199}
200
201/// BlockScopePosPair - Structure for specifying position in CFG during its
202/// build process. It consists of CFGBlock that specifies position in CFG graph
203/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000204struct BlockScopePosPair {
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000205 BlockScopePosPair() : block(0) {}
206 BlockScopePosPair(CFGBlock* b, LocalScope::const_iterator scopePos)
207 : block(b), scopePosition(scopePos) {}
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000208
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000209 CFGBlock *block;
210 LocalScope::const_iterator scopePosition;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000211};
212
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000213/// TryResult - a class representing a variant over the values
214/// 'true', 'false', or 'unknown'. This is returned by tryEvaluateBool,
215/// and is used by the CFGBuilder to decide if a branch condition
216/// can be decided up front during CFG construction.
217class TryResult {
218 int X;
219public:
220 TryResult(bool b) : X(b ? 1 : 0) {}
221 TryResult() : X(-1) {}
222
223 bool isTrue() const { return X == 1; }
224 bool isFalse() const { return X == 0; }
225 bool isKnown() const { return X >= 0; }
226 void negate() {
227 assert(isKnown());
228 X ^= 0x1;
229 }
230};
231
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000232/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000233/// The builder is stateful: an instance of the builder should be used to only
234/// construct a single CFG.
235///
236/// Example usage:
237///
238/// CFGBuilder builder;
239/// CFG* cfg = builder.BuildAST(stmt1);
240///
Mike Stump31feda52009-07-17 01:31:16 +0000241/// CFG construction is done via a recursive walk of an AST. We actually parse
242/// the AST in reverse order so that the successor of a basic block is
243/// constructed prior to its predecessor. This allows us to nicely capture
244/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +0000245///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000246class CFGBuilder {
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000247 typedef BlockScopePosPair JumpTarget;
248 typedef BlockScopePosPair JumpSource;
249
Mike Stump0d76d072009-07-20 23:24:15 +0000250 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000251 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000252
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000253 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000254 CFGBlock* Succ;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000255 JumpTarget ContinueJumpTarget;
256 JumpTarget BreakJumpTarget;
Ted Kremenek879d8e12007-08-23 18:43:24 +0000257 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +0000258 CFGBlock* DefaultCaseBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000259 CFGBlock* TryTerminatedBlock;
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000260
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000261 // Current position in local scope.
262 LocalScope::const_iterator ScopePos;
263
264 // LabelMap records the mapping from Label expressions to their jump targets.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000265 typedef llvm::DenseMap<LabelDecl*, JumpTarget> LabelMapTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000266 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +0000267
268 // A list of blocks that end with a "goto" that must be backpatched to their
269 // resolved targets upon completion of CFG construction.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000270 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000271 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +0000272
Ted Kremenekeda180e22007-08-28 19:26:49 +0000273 // A list of labels whose address has been taken (for indirect gotos).
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000274 typedef llvm::SmallPtrSet<LabelDecl*, 5> LabelSetTy;
Ted Kremenekeda180e22007-08-28 19:26:49 +0000275 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +0000276
Zhongxing Xud38fb842010-09-16 03:28:18 +0000277 bool badCFG;
Ted Kremenekf9d82902011-03-10 01:14:05 +0000278 const CFG::BuildOptions &BuildOpts;
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000279
280 // State to track for building switch statements.
281 bool switchExclusivelyCovered;
Ted Kremenekbe528712011-03-04 01:03:41 +0000282 Expr::EvalResult *switchCond;
Zhongxing Xud38fb842010-09-16 03:28:18 +0000283
Mike Stump31feda52009-07-17 01:31:16 +0000284public:
Ted Kremenekf9d82902011-03-10 01:14:05 +0000285 explicit CFGBuilder(ASTContext *astContext,
286 const CFG::BuildOptions &buildOpts)
287 : Context(astContext), cfg(new CFG()), // crew a new CFG
288 Block(NULL), Succ(NULL),
289 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
290 TryTerminatedBlock(NULL), badCFG(false), BuildOpts(buildOpts),
291 switchExclusivelyCovered(false), switchCond(0) {}
Mike Stump31feda52009-07-17 01:31:16 +0000292
Ted Kremenek9aae5132007-08-23 21:42:29 +0000293 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekf9d82902011-03-10 01:14:05 +0000294 CFG* buildCFG(const Decl *D, Stmt *Statement);
Mike Stump31feda52009-07-17 01:31:16 +0000295
Ted Kremenek93668002009-07-17 22:18:43 +0000296private:
297 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000298 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
299 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
300 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000301 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000302 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
John McCall5d413782010-12-06 08:20:24 +0000303 CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000304 AddStmtChoice asc);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000305 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
306 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000307 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
308 AddStmtChoice asc);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000309 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000310 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
311 AddStmtChoice asc);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000312 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
313 AddStmtChoice asc);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000314 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000315 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000316 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000317 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000318 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
John McCallc07a0c72011-02-17 10:25:35 +0000319 CFGBlock *VisitConditionalOperator(AbstractConditionalOperator *C,
320 AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000321 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000322 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000323 CFGBlock *VisitDeclSubExpr(DeclStmt* DS);
Ted Kremenek21822592009-07-17 18:20:32 +0000324 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
325 CFGBlock *VisitDoStmt(DoStmt *D);
326 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000327 CFGBlock *VisitGotoStmt(GotoStmt* G);
328 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000329 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000330 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
331 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000332 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000333 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
334 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
335 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
336 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
337 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
338 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000339 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
340 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000341 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan6dace612010-11-22 08:45:56 +0000342 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000343 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000344
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000345 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
346 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000347 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000348
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000349 // Visitors to walk an AST and generate destructors of temporaries in
350 // full expression.
351 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
352 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
353 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
354 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
355 bool BindToTemporary);
John McCallc07a0c72011-02-17 10:25:35 +0000356 CFGBlock *
357 VisitConditionalOperatorForTemporaryDtors(AbstractConditionalOperator *E,
358 bool BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000359
Ted Kremenek6065ef62008-04-28 18:00:46 +0000360 // NYS == Not Yet Supported
361 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000362 badCFG = true;
363 return Block;
364 }
Mike Stump31feda52009-07-17 01:31:16 +0000365
Ted Kremenek93668002009-07-17 22:18:43 +0000366 void autoCreateBlock() { if (!Block) Block = createBlock(); }
367 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000368
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000369 CFGBlock *addStmt(Stmt *S) {
370 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000371 }
Alexis Hunt1d792652011-01-08 20:30:50 +0000372 CFGBlock *addInitializer(CXXCtorInitializer *I);
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000373 void addAutomaticObjDtors(LocalScope::const_iterator B,
374 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski20b88732010-10-05 05:37:00 +0000375 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000376
Marcin Swiderski5e415732010-09-30 23:05:00 +0000377 // Local scopes creation.
378 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
379
Zhongxing Xu81714f22010-10-01 03:00:16 +0000380 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000381 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
382 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
383
384 void addLocalScopeAndDtors(Stmt* S);
385
386 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek8219b822010-12-16 07:46:53 +0000387 void appendStmt(CFGBlock *B, Stmt *S,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000388 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000389 B->appendStmt(S, cfg->getBumpVectorContext());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000390 }
Alexis Hunt1d792652011-01-08 20:30:50 +0000391 void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000392 B->appendInitializer(I, cfg->getBumpVectorContext());
393 }
Marcin Swiderski20b88732010-10-05 05:37:00 +0000394 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
395 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
396 }
397 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
398 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
399 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000400 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
401 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
402 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000403
Marcin Swiderski321a7072010-09-30 22:54:37 +0000404 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
405 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
406 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
407 LocalScope::const_iterator E, Stmt* S);
408 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
409 LocalScope::const_iterator B, LocalScope::const_iterator E);
410
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000411 void addSuccessor(CFGBlock *B, CFGBlock *S) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000412 B->addSuccessor(S, cfg->getBumpVectorContext());
413 }
Mike Stump11289f42009-09-09 15:08:12 +0000414
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000415 /// Try and evaluate an expression to an integer constant.
416 bool tryEvaluate(Expr *S, Expr::EvalResult &outResult) {
417 if (!BuildOpts.PruneTriviallyFalseEdges)
418 return false;
419 return !S->isTypeDependent() &&
420 !S->isValueDependent() &&
421 S->Evaluate(outResult, *Context);
422 }
Mike Stump11289f42009-09-09 15:08:12 +0000423
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000424 /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
Mike Stump773582d2009-07-23 23:25:26 +0000425 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000426 TryResult tryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000427 Expr::EvalResult Result;
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000428 if (!tryEvaluate(S, Result))
429 return TryResult();
430
431 if (Result.Val.isInt())
432 return Result.Val.getInt().getBoolValue();
433
434 if (Result.Val.isLValue()) {
435 Expr *e = Result.Val.getLValueBase();
436 const CharUnits &c = Result.Val.getLValueOffset();
437 if (!e && c.isZero())
438 return false;
Ted Kremenek1a241d12011-02-23 05:11:46 +0000439 }
Ted Kremenek30754282009-07-24 04:47:11 +0000440 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000441 }
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000442
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000443};
Mike Stump31feda52009-07-17 01:31:16 +0000444
Douglas Gregor4619e432008-12-05 23:32:09 +0000445// FIXME: Add support for dependent-sized array types in C++?
446// Does it even make sense to build a CFG for an uninstantiated template?
John McCall424cec92011-01-19 06:33:43 +0000447static const VariableArrayType *FindVA(const Type *t) {
448 while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
449 if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000450 if (vat->getSizeExpr())
451 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000452
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000453 t = vt->getElementType().getTypePtr();
454 }
Mike Stump31feda52009-07-17 01:31:16 +0000455
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000456 return 0;
457}
Mike Stump31feda52009-07-17 01:31:16 +0000458
459/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
460/// arbitrary statement. Examples include a single expression or a function
461/// body (compound statement). The ownership of the returned CFG is
462/// transferred to the caller. If CFG construction fails, this method returns
463/// NULL.
Ted Kremenekf9d82902011-03-10 01:14:05 +0000464CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement) {
Ted Kremenek8aed4902009-10-20 23:46:25 +0000465 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000466 if (!Statement)
467 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000468
Mike Stump31feda52009-07-17 01:31:16 +0000469 // Create an empty block that will serve as the exit block for the CFG. Since
470 // this is the first block added to the CFG, it will be implicitly registered
471 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000472 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000473 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000474 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000475
Marcin Swiderski20b88732010-10-05 05:37:00 +0000476 if (BuildOpts.AddImplicitDtors)
477 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
478 addImplicitDtorsForDestructor(DD);
479
Ted Kremenek9aae5132007-08-23 21:42:29 +0000480 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000481 CFGBlock *B = addStmt(Statement);
482
483 if (badCFG)
484 return NULL;
485
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000486 // For C++ constructor add initializers to CFG.
487 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
488 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
489 E = CD->init_rend(); I != E; ++I) {
490 B = addInitializer(*I);
491 if (badCFG)
492 return NULL;
493 }
494 }
495
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000496 if (B)
497 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000498
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000499 // Backpatch the gotos whose label -> block mappings we didn't know when we
500 // encountered them.
501 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
502 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000503
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000504 CFGBlock* B = I->block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000505 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
506 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000507
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000508 // If there is no target for the goto, then we are looking at an
509 // incomplete AST. Handle this by not registering a successor.
510 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000511
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000512 JumpTarget JT = LI->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000513 prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
514 JT.scopePosition);
515 addSuccessor(B, JT.block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000516 }
517
518 // Add successors to the Indirect Goto Dispatch block (if we have one).
519 if (CFGBlock* B = cfg->getIndirectGotoBlock())
520 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
521 E = AddressTakenLabels.end(); I != E; ++I ) {
522
523 // Lookup the target block.
524 LabelMapTy::iterator LI = LabelMap.find(*I);
525
526 // If there is no target block that contains label, then we are looking
527 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000528 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000529
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000530 addSuccessor(B, LI->second.block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000531 }
Mike Stump31feda52009-07-17 01:31:16 +0000532
Mike Stump31feda52009-07-17 01:31:16 +0000533 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000534 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000535
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000536 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000537}
Mike Stump31feda52009-07-17 01:31:16 +0000538
Ted Kremenek9aae5132007-08-23 21:42:29 +0000539/// createBlock - Used to lazily create blocks that are connected
540/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000541CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000542 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000543 if (add_successor && Succ)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000544 addSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000545 return B;
546}
Mike Stump31feda52009-07-17 01:31:16 +0000547
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000548/// addInitializer - Add C++ base or member initializer element to CFG.
Alexis Hunt1d792652011-01-08 20:30:50 +0000549CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000550 if (!BuildOpts.AddInitializers)
551 return Block;
552
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000553 bool IsReference = false;
554 bool HasTemporaries = false;
555
556 // Destructors of temporaries in initialization expression should be called
557 // after initialization finishes.
558 Expr *Init = I->getInit();
559 if (Init) {
Francois Pichetd583da02010-12-04 09:14:42 +0000560 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000561 IsReference = FD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +0000562 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000563
564 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
565 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +0000566 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000567 IsReference);
568 }
569 }
570
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000571 autoCreateBlock();
572 appendInitializer(Block, I);
573
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000574 if (Init) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000575 if (HasTemporaries) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000576 // For expression with temporaries go directly to subexpression to omit
577 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +0000578 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
579 }
580 return Visit(Init);
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000581 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000582
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000583 return Block;
584}
585
Marcin Swiderski5e415732010-09-30 23:05:00 +0000586/// addAutomaticObjDtors - Add to current block automatic objects destructors
587/// for objects in range of local scope positions. Use S as trigger statement
588/// for destructors.
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000589void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
590 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000591 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000592 return;
593
Marcin Swiderski5e415732010-09-30 23:05:00 +0000594 if (B == E)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000595 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000596
597 autoCreateBlock();
598 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000599}
600
Marcin Swiderski20b88732010-10-05 05:37:00 +0000601/// addImplicitDtorsForDestructor - Add implicit destructors generated for
602/// base and member objects in destructor.
603void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
604 assert (BuildOpts.AddImplicitDtors
605 && "Can be called only when dtors should be added");
606 const CXXRecordDecl *RD = DD->getParent();
607
608 // At the end destroy virtual base objects.
609 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
610 VE = RD->vbases_end(); VI != VE; ++VI) {
611 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
612 if (!CD->hasTrivialDestructor()) {
613 autoCreateBlock();
614 appendBaseDtor(Block, VI);
615 }
616 }
617
618 // Before virtual bases destroy direct base objects.
619 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
620 BE = RD->bases_end(); BI != BE; ++BI) {
621 if (!BI->isVirtual()) {
622 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
623 if (!CD->hasTrivialDestructor()) {
624 autoCreateBlock();
625 appendBaseDtor(Block, BI);
626 }
627 }
628 }
629
630 // First destroy member objects.
631 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
632 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski01769902010-10-25 07:05:54 +0000633 // Check for constant size array. Set type to array element type.
634 QualType QT = FI->getType();
635 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
636 if (AT->getSize() == 0)
637 continue;
638 QT = AT->getElementType();
639 }
640
641 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski20b88732010-10-05 05:37:00 +0000642 if (!CD->hasTrivialDestructor()) {
643 autoCreateBlock();
644 appendMemberDtor(Block, *FI);
645 }
646 }
647}
648
Marcin Swiderski5e415732010-09-30 23:05:00 +0000649/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
650/// way return valid LocalScope object.
651LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
652 if (!Scope) {
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000653 llvm::BumpPtrAllocator &alloc = cfg->getAllocator();
654 Scope = alloc.Allocate<LocalScope>();
655 BumpVectorContext ctx(alloc);
656 new (Scope) LocalScope(ctx, ScopePos);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000657 }
658 return Scope;
659}
660
661/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu81714f22010-10-01 03:00:16 +0000662/// that should create implicit scope (e.g. if/else substatements).
663void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000664 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu81714f22010-10-01 03:00:16 +0000665 return;
666
667 LocalScope *Scope = 0;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000668
669 // For compound statement we will be creating explicit scope.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000670 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000671 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
672 ; BI != BE; ++BI) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000673 Stmt *SI = *BI;
674 if (LabelStmt *LS = dyn_cast<LabelStmt>(SI))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000675 SI = LS->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000676 if (DeclStmt *DS = dyn_cast<DeclStmt>(SI))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000677 Scope = addLocalScopeForDeclStmt(DS, Scope);
678 }
Zhongxing Xu81714f22010-10-01 03:00:16 +0000679 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000680 }
681
682 // For any other statement scope will be implicit and as such will be
683 // interesting only for DeclStmt.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000684 if (LabelStmt *LS = dyn_cast<LabelStmt>(S))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000685 S = LS->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000686 if (DeclStmt *DS = dyn_cast<DeclStmt>(S))
Zhongxing Xu307701e2010-10-01 03:09:09 +0000687 addLocalScopeForDeclStmt(DS);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000688}
689
690/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
691/// reuse Scope if not NULL.
692LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000693 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000694 if (!BuildOpts.AddImplicitDtors)
695 return Scope;
696
697 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
698 ; DI != DE; ++DI) {
699 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
700 Scope = addLocalScopeForVarDecl(VD, Scope);
701 }
702 return Scope;
703}
704
705/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
706/// create add scope for automatic objects and temporary objects bound to
707/// const reference. Will reuse Scope if not NULL.
708LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000709 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000710 if (!BuildOpts.AddImplicitDtors)
711 return Scope;
712
713 // Check if variable is local.
714 switch (VD->getStorageClass()) {
715 case SC_None:
716 case SC_Auto:
717 case SC_Register:
718 break;
719 default: return Scope;
720 }
721
722 // Check for const references bound to temporary. Set type to pointee.
723 QualType QT = VD->getType();
724 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
725 QT = RT->getPointeeType();
726 if (!QT.isConstQualified())
727 return Scope;
728 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
729 return Scope;
730 }
731
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000732 // Check for constant size array. Set type to array element type.
733 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
734 if (AT->getSize() == 0)
735 return Scope;
736 QT = AT->getElementType();
737 }
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000738
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000739 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000740 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
741 if (!CD->hasTrivialDestructor()) {
742 // Add the variable to scope
743 Scope = createOrReuseLocalScope(Scope);
744 Scope->addVar(VD);
745 ScopePos = Scope->begin();
746 }
Marcin Swiderski5e415732010-09-30 23:05:00 +0000747 return Scope;
748}
749
750/// addLocalScopeAndDtors - For given statement add local scope for it and
751/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
752void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
753 if (!BuildOpts.AddImplicitDtors)
754 return;
755
756 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu81714f22010-10-01 03:00:16 +0000757 addLocalScopeForStmt(S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000758 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
759}
760
Marcin Swiderski321a7072010-09-30 22:54:37 +0000761/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
762/// automatic storage duration to CFGBlock's elements vector. Insertion will be
763/// performed in place specified with iterator.
764void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
765 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
766 BumpVectorContext& C = cfg->getBumpVectorContext();
767 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
768 while (B != E)
769 I = Blk->insertAutomaticObjDtor(I, *B++, S);
770}
771
772/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
773/// automatic storage duration to CFGBlock's elements vector. Elements will be
774/// appended to physical end of the vector which happens to be logical
775/// beginning.
776void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
777 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
778 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
779}
780
781/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
782/// variables with automatic storage duration to CFGBlock's elements vector.
783/// Elements will be prepended to physical beginning of the vector which
784/// happens to be logical end. Use blocks terminator as statement that specifies
785/// destructors call site.
786void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
787 LocalScope::const_iterator B, LocalScope::const_iterator E) {
788 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
789}
790
Ted Kremenek93668002009-07-17 22:18:43 +0000791/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000792/// blocks for ternary operators, &&, and ||. We also process "," and
793/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000794CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000795tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000796 if (!S) {
797 badCFG = true;
798 return 0;
799 }
Ted Kremenek93668002009-07-17 22:18:43 +0000800 switch (S->getStmtClass()) {
801 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000802 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000803
804 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000805 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000806
John McCallc07a0c72011-02-17 10:25:35 +0000807 case Stmt::BinaryConditionalOperatorClass:
808 return VisitConditionalOperator(cast<BinaryConditionalOperator>(S), asc);
809
Ted Kremenek93668002009-07-17 22:18:43 +0000810 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000811 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000812
Ted Kremenek93668002009-07-17 22:18:43 +0000813 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000814 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000815
Ted Kremenek93668002009-07-17 22:18:43 +0000816 case Stmt::BreakStmtClass:
817 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000818
Ted Kremenek93668002009-07-17 22:18:43 +0000819 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000820 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000821 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000822
Ted Kremenek93668002009-07-17 22:18:43 +0000823 case Stmt::CaseStmtClass:
824 return VisitCaseStmt(cast<CaseStmt>(S));
825
826 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000827 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000828
Ted Kremenek93668002009-07-17 22:18:43 +0000829 case Stmt::CompoundStmtClass:
830 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000831
Ted Kremenek93668002009-07-17 22:18:43 +0000832 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000833 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000834
Ted Kremenek93668002009-07-17 22:18:43 +0000835 case Stmt::ContinueStmtClass:
836 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000837
Ted Kremenekb27378c2010-01-19 20:40:33 +0000838 case Stmt::CXXCatchStmtClass:
839 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
840
John McCall5d413782010-12-06 08:20:24 +0000841 case Stmt::ExprWithCleanupsClass:
842 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000843
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000844 case Stmt::CXXBindTemporaryExprClass:
845 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
846
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000847 case Stmt::CXXConstructExprClass:
848 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
849
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000850 case Stmt::CXXFunctionalCastExprClass:
851 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
852
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000853 case Stmt::CXXTemporaryObjectExprClass:
854 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
855
Zhongxing Xu7e612172010-04-13 09:38:01 +0000856 case Stmt::CXXMemberCallExprClass:
857 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
858
Ted Kremenekb27378c2010-01-19 20:40:33 +0000859 case Stmt::CXXThrowExprClass:
860 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000861
Ted Kremenekb27378c2010-01-19 20:40:33 +0000862 case Stmt::CXXTryStmtClass:
863 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000864
Ted Kremenek93668002009-07-17 22:18:43 +0000865 case Stmt::DeclStmtClass:
866 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000867
Ted Kremenek93668002009-07-17 22:18:43 +0000868 case Stmt::DefaultStmtClass:
869 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000870
Ted Kremenek93668002009-07-17 22:18:43 +0000871 case Stmt::DoStmtClass:
872 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000873
Ted Kremenek93668002009-07-17 22:18:43 +0000874 case Stmt::ForStmtClass:
875 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000876
Ted Kremenek93668002009-07-17 22:18:43 +0000877 case Stmt::GotoStmtClass:
878 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000879
Ted Kremenek93668002009-07-17 22:18:43 +0000880 case Stmt::IfStmtClass:
881 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000882
Ted Kremenek8219b822010-12-16 07:46:53 +0000883 case Stmt::ImplicitCastExprClass:
884 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000885
Ted Kremenek93668002009-07-17 22:18:43 +0000886 case Stmt::IndirectGotoStmtClass:
887 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000888
Ted Kremenek93668002009-07-17 22:18:43 +0000889 case Stmt::LabelStmtClass:
890 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000891
Ted Kremenek5868ec62010-04-11 17:02:10 +0000892 case Stmt::MemberExprClass:
893 return VisitMemberExpr(cast<MemberExpr>(S), asc);
894
Ted Kremenek93668002009-07-17 22:18:43 +0000895 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000896 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
897
Ted Kremenek93668002009-07-17 22:18:43 +0000898 case Stmt::ObjCAtSynchronizedStmtClass:
899 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000900
Ted Kremenek93668002009-07-17 22:18:43 +0000901 case Stmt::ObjCAtThrowStmtClass:
902 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000903
Ted Kremenek93668002009-07-17 22:18:43 +0000904 case Stmt::ObjCAtTryStmtClass:
905 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000906
Ted Kremenek93668002009-07-17 22:18:43 +0000907 case Stmt::ObjCForCollectionStmtClass:
908 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000909
Ted Kremenek93668002009-07-17 22:18:43 +0000910 case Stmt::ParenExprClass:
911 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000912 goto tryAgain;
913
Ted Kremenek93668002009-07-17 22:18:43 +0000914 case Stmt::NullStmtClass:
915 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000916
Ted Kremenek93668002009-07-17 22:18:43 +0000917 case Stmt::ReturnStmtClass:
918 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000919
Ted Kremenek93668002009-07-17 22:18:43 +0000920 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000921 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000922
Ted Kremenek93668002009-07-17 22:18:43 +0000923 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000924 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000925
Ted Kremenek93668002009-07-17 22:18:43 +0000926 case Stmt::SwitchStmtClass:
927 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000928
Zhanyong Wan6dace612010-11-22 08:45:56 +0000929 case Stmt::UnaryOperatorClass:
930 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
931
Ted Kremenek93668002009-07-17 22:18:43 +0000932 case Stmt::WhileStmtClass:
933 return VisitWhileStmt(cast<WhileStmt>(S));
934 }
935}
Mike Stump11289f42009-09-09 15:08:12 +0000936
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000937CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
938 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000939 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000940 appendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000941 }
Mike Stump11289f42009-09-09 15:08:12 +0000942
Ted Kremenek93668002009-07-17 22:18:43 +0000943 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000944}
Mike Stump31feda52009-07-17 01:31:16 +0000945
Ted Kremenek93668002009-07-17 22:18:43 +0000946/// VisitChildren - Visit the children of a Stmt.
947CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
Ted Kremenek828f6312011-02-21 22:11:26 +0000948 CFGBlock *lastBlock = Block;
949 for (Stmt::child_range I = Terminator->children(); I; ++I)
950 if (Stmt *child = *I)
951 if (CFGBlock *b = Visit(child))
952 lastBlock = b;
953
954 return lastBlock;
Ted Kremenek9e248872007-08-27 21:27:44 +0000955}
Mike Stump11289f42009-09-09 15:08:12 +0000956
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000957CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
958 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000959 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000960
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000961 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000962 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000963 appendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000964 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000965
Ted Kremenek9aae5132007-08-23 21:42:29 +0000966 return Block;
967}
Mike Stump11289f42009-09-09 15:08:12 +0000968
Zhanyong Wan6dace612010-11-22 08:45:56 +0000969CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek8219b822010-12-16 07:46:53 +0000970 AddStmtChoice asc) {
Zhanyong Wan6dace612010-11-22 08:45:56 +0000971 if (asc.alwaysAdd()) {
972 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000973 appendStmt(Block, U, asc);
Zhanyong Wan6dace612010-11-22 08:45:56 +0000974 }
975
Ted Kremenek8219b822010-12-16 07:46:53 +0000976 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan6dace612010-11-22 08:45:56 +0000977}
978
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000979CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
980 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000981 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000982 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000983 appendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000984
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000985 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000986 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000987
Ted Kremenek93668002009-07-17 22:18:43 +0000988 // create the block evaluating the LHS
989 CFGBlock* LHSBlock = createBlock(false);
990 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000991
Ted Kremenek93668002009-07-17 22:18:43 +0000992 // create the block evaluating the RHS
993 Succ = ConfluenceBlock;
994 Block = NULL;
995 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000996
997 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000998 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000999 return 0;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001000 } else {
Ted Kremenek989da5e2010-04-29 01:10:26 +00001001 // Create an empty block for cases where the RHS doesn't require
1002 // any explicit statements in the CFG.
1003 RHSBlock = createBlock();
1004 }
Mike Stump11289f42009-09-09 15:08:12 +00001005
Mike Stump773582d2009-07-23 23:25:26 +00001006 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001007 TryResult KnownVal = tryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +00001008 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +00001009 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +00001010
Ted Kremenek93668002009-07-17 22:18:43 +00001011 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +00001012 if (B->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001013 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
1014 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001015 } else {
John McCalle3027922010-08-25 11:45:40 +00001016 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001017 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
1018 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001019 }
Mike Stump11289f42009-09-09 15:08:12 +00001020
Ted Kremenek93668002009-07-17 22:18:43 +00001021 // Generate the blocks for evaluating the LHS.
1022 Block = LHSBlock;
1023 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +00001024 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001025
1026 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +00001027 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001028 appendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +00001029 addStmt(B->getRHS());
1030 return addStmt(B->getLHS());
1031 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001032
1033 if (B->isAssignmentOp()) {
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001034 if (asc.alwaysAdd()) {
1035 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001036 appendStmt(Block, B, asc);
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001037 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001038 Visit(B->getLHS());
Marcin Swiderski77232492010-10-24 08:21:40 +00001039 return Visit(B->getRHS());
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001040 }
Mike Stump11289f42009-09-09 15:08:12 +00001041
Marcin Swiderski77232492010-10-24 08:21:40 +00001042 if (asc.alwaysAdd()) {
1043 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001044 appendStmt(Block, B, asc);
Marcin Swiderski77232492010-10-24 08:21:40 +00001045 }
1046
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001047 CFGBlock *RBlock = Visit(B->getRHS());
1048 CFGBlock *LBlock = Visit(B->getLHS());
1049 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1050 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1051 // return RBlock. Otherwise we'll incorrectly return NULL.
1052 return (LBlock ? LBlock : RBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001053}
1054
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001055CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1056 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +00001057 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001058 appendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +00001059 }
1060 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001061}
1062
Ted Kremenek93668002009-07-17 22:18:43 +00001063CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1064 // "break" is a control-flow statement. Thus we stop processing the current
1065 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001066 if (badCFG)
1067 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001068
Ted Kremenek93668002009-07-17 22:18:43 +00001069 // Now create a new block that ends with the break statement.
1070 Block = createBlock(false);
1071 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +00001072
Ted Kremenek93668002009-07-17 22:18:43 +00001073 // If there is no target for the break, then we are looking at an incomplete
1074 // AST. This means that the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001075 if (BreakJumpTarget.block) {
1076 addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B);
1077 addSuccessor(Block, BreakJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001078 } else
Ted Kremenek93668002009-07-17 22:18:43 +00001079 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +00001080
1081
Ted Kremenek9aae5132007-08-23 21:42:29 +00001082 return Block;
1083}
Mike Stump11289f42009-09-09 15:08:12 +00001084
Mike Stump04c68512010-01-21 15:20:48 +00001085static bool CanThrow(Expr *E) {
1086 QualType Ty = E->getType();
1087 if (Ty->isFunctionPointerType())
1088 Ty = Ty->getAs<PointerType>()->getPointeeType();
1089 else if (Ty->isBlockPointerType())
1090 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001091
Mike Stump04c68512010-01-21 15:20:48 +00001092 const FunctionType *FT = Ty->getAs<FunctionType>();
1093 if (FT) {
1094 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1095 if (Proto->hasEmptyExceptionSpec())
1096 return false;
1097 }
1098 return true;
1099}
1100
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001101CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +00001102 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +00001103 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001104 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001105 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +00001106 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001107
Mike Stump04c68512010-01-21 15:20:48 +00001108 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001109
1110 // Languages without exceptions are assumed to not throw.
Anders Carlsson6dc07d42011-02-28 00:33:03 +00001111 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001112 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +00001113 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +00001114 }
1115
1116 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001117 if (FD->hasAttr<NoReturnAttr>())
1118 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +00001119 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +00001120 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001121 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001122
Mike Stump04c68512010-01-21 15:20:48 +00001123 if (!CanThrow(C->getCallee()))
1124 AddEHEdge = false;
1125
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001126 if (!NoReturn && !AddEHEdge)
1127 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump11289f42009-09-09 15:08:12 +00001128
Mike Stump92244b02010-01-19 22:00:14 +00001129 if (Block) {
1130 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001131 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +00001132 return 0;
1133 }
Mike Stump11289f42009-09-09 15:08:12 +00001134
Mike Stump92244b02010-01-19 22:00:14 +00001135 Block = createBlock(!NoReturn);
Ted Kremenek8219b822010-12-16 07:46:53 +00001136 appendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +00001137
Mike Stump92244b02010-01-19 22:00:14 +00001138 if (NoReturn) {
1139 // Wire this to the exit block directly.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001140 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001141 }
Mike Stump04c68512010-01-21 15:20:48 +00001142 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +00001143 // Add exceptional edges.
1144 if (TryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001145 addSuccessor(Block, TryTerminatedBlock);
Mike Stump92244b02010-01-19 22:00:14 +00001146 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001147 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001148 }
Mike Stump11289f42009-09-09 15:08:12 +00001149
Mike Stump8c5d7992009-07-25 21:26:53 +00001150 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +00001151}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001152
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001153CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1154 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +00001155 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001156 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001157 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001158 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001159
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001160 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek21822592009-07-17 18:20:32 +00001161 Succ = ConfluenceBlock;
1162 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001163 CFGBlock* LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001164 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001165 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001166
Ted Kremenek21822592009-07-17 18:20:32 +00001167 Succ = ConfluenceBlock;
1168 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001169 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001170 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001171 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001172
Ted Kremenek21822592009-07-17 18:20:32 +00001173 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +00001174 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001175 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
1176 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1177 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +00001178 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +00001179 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +00001180}
Mike Stump11289f42009-09-09 15:08:12 +00001181
1182
1183CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001184 addLocalScopeAndDtors(C);
Mike Stump11289f42009-09-09 15:08:12 +00001185 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001186
1187 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1188 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +00001189 // If we hit a segment of code just containing ';' (NullStmts), we can
1190 // get a null block back. In such cases, just use the LastBlock
1191 if (CFGBlock *newBlock = addStmt(*I))
1192 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001193
Ted Kremenekce499c22009-08-27 23:16:26 +00001194 if (badCFG)
1195 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001196 }
Mike Stump92244b02010-01-19 22:00:14 +00001197
Ted Kremenek93668002009-07-17 22:18:43 +00001198 return LastBlock;
1199}
Mike Stump11289f42009-09-09 15:08:12 +00001200
John McCallc07a0c72011-02-17 10:25:35 +00001201CFGBlock *CFGBuilder::VisitConditionalOperator(AbstractConditionalOperator *C,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001202 AddStmtChoice asc) {
John McCallc07a0c72011-02-17 10:25:35 +00001203 const BinaryConditionalOperator *BCO = dyn_cast<BinaryConditionalOperator>(C);
1204 const OpaqueValueExpr *opaqueValue = (BCO ? BCO->getOpaqueValue() : NULL);
1205
Ted Kremenek51d40b02009-07-17 18:15:54 +00001206 // Create the confluence block that will "merge" the results of the ternary
1207 // expression.
1208 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001209 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001210 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001211 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001212
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001213 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001214
Ted Kremenek51d40b02009-07-17 18:15:54 +00001215 // Create a block for the LHS expression if there is an LHS expression. A
1216 // GCC extension allows LHS to be NULL, causing the condition to be the
1217 // value that is returned instead.
1218 // e.g: x ?: y is shorthand for: x ? x : y;
1219 Succ = ConfluenceBlock;
1220 Block = NULL;
John McCallc07a0c72011-02-17 10:25:35 +00001221 CFGBlock* LHSBlock = 0;
1222 const Expr *trueExpr = C->getTrueExpr();
1223 if (trueExpr != opaqueValue) {
1224 LHSBlock = Visit(C->getTrueExpr(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001225 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001226 return 0;
1227 Block = NULL;
1228 }
Ted Kremenekd8138012011-02-24 03:09:15 +00001229 else
1230 LHSBlock = ConfluenceBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001231
Ted Kremenek51d40b02009-07-17 18:15:54 +00001232 // Create the block for the RHS expression.
1233 Succ = ConfluenceBlock;
John McCallc07a0c72011-02-17 10:25:35 +00001234 CFGBlock* RHSBlock = Visit(C->getFalseExpr(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001235 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001236 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001237
Ted Kremenek51d40b02009-07-17 18:15:54 +00001238 // Create the block that will contain the condition.
1239 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +00001240
Mike Stump773582d2009-07-23 23:25:26 +00001241 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001242 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
Ted Kremenekd8138012011-02-24 03:09:15 +00001243 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001244 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001245 Block->setTerminator(C);
John McCallc07a0c72011-02-17 10:25:35 +00001246 Expr *condExpr = C->getCond();
John McCall68cc3352011-02-19 03:13:26 +00001247
Ted Kremenekd8138012011-02-24 03:09:15 +00001248 if (opaqueValue) {
1249 // Run the condition expression if it's not trivially expressed in
1250 // terms of the opaque value (or if there is no opaque value).
1251 if (condExpr != opaqueValue)
1252 addStmt(condExpr);
John McCall68cc3352011-02-19 03:13:26 +00001253
Ted Kremenekd8138012011-02-24 03:09:15 +00001254 // Before that, run the common subexpression if there was one.
1255 // At least one of this or the above will be run.
1256 return addStmt(BCO->getCommon());
1257 }
1258
1259 return addStmt(condExpr);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001260}
1261
Ted Kremenek93668002009-07-17 22:18:43 +00001262CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001263 if (DS->isSingleDecl())
1264 return VisitDeclSubExpr(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001265
Ted Kremenek93668002009-07-17 22:18:43 +00001266 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001267
Ted Kremenek93668002009-07-17 22:18:43 +00001268 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1269 typedef llvm::SmallVector<Decl*,10> BufTy;
1270 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +00001271
Ted Kremenek93668002009-07-17 22:18:43 +00001272 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1273 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1274 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1275 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +00001276
Ted Kremenek93668002009-07-17 22:18:43 +00001277 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1278 // automatically freed with the CFG.
1279 DeclGroupRef DG(*I);
1280 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001281 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +00001282 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +00001283
Ted Kremenek93668002009-07-17 22:18:43 +00001284 // Append the fake DeclStmt to block.
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001285 B = VisitDeclSubExpr(DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +00001286 }
Mike Stump11289f42009-09-09 15:08:12 +00001287
1288 return B;
Ted Kremenek93668002009-07-17 22:18:43 +00001289}
Mike Stump11289f42009-09-09 15:08:12 +00001290
Ted Kremenek93668002009-07-17 22:18:43 +00001291/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001292/// DeclStmts and initializers in them.
1293CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1294 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekf6998822008-02-26 00:22:58 +00001295
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001296 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001297
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001298 if (!VD) {
1299 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001300 appendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +00001301 return Block;
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001302 }
Mike Stump11289f42009-09-09 15:08:12 +00001303
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001304 bool IsReference = false;
1305 bool HasTemporaries = false;
1306
1307 // Destructors of temporaries in initialization expression should be called
1308 // after initialization finishes.
Ted Kremenek93668002009-07-17 22:18:43 +00001309 Expr *Init = VD->getInit();
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001310 if (Init) {
1311 IsReference = VD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +00001312 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001313
1314 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1315 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +00001316 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001317 IsReference);
1318 }
1319 }
1320
1321 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001322 appendStmt(Block, DS);
Mike Stump11289f42009-09-09 15:08:12 +00001323
Ted Kremenek93668002009-07-17 22:18:43 +00001324 if (Init) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001325 if (HasTemporaries)
1326 // For expression with temporaries go directly to subexpression to omit
1327 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +00001328 Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001329 else
Ted Kremenek8219b822010-12-16 07:46:53 +00001330 Visit(Init);
Ted Kremenek93668002009-07-17 22:18:43 +00001331 }
Mike Stump11289f42009-09-09 15:08:12 +00001332
Ted Kremenek93668002009-07-17 22:18:43 +00001333 // If the type of VD is a VLA, then we must process its size expressions.
John McCall424cec92011-01-19 06:33:43 +00001334 for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr());
1335 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek93668002009-07-17 22:18:43 +00001336 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001337
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001338 // Remove variable from local scope.
1339 if (ScopePos && VD == *ScopePos)
1340 ++ScopePos;
1341
Ted Kremenek93668002009-07-17 22:18:43 +00001342 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001343}
1344
1345CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001346 // We may see an if statement in the middle of a basic block, or it may be the
1347 // first statement we are processing. In either case, we create a new basic
1348 // block. First, we create the blocks for the then...else statements, and
1349 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +00001350 // middle of a block, we stop processing that block. That block is then the
1351 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +00001352
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001353 // Save local scope position because in case of condition variable ScopePos
1354 // won't be restored when traversing AST.
1355 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1356
1357 // Create local scope for possible condition variable.
1358 // Store scope position. Add implicit destructor.
1359 if (VarDecl* VD = I->getConditionVariable()) {
1360 LocalScope::const_iterator BeginScopePos = ScopePos;
1361 addLocalScopeForVarDecl(VD);
1362 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1363 }
1364
Mike Stump31feda52009-07-17 01:31:16 +00001365 // The block we were proccessing is now finished. Make it the successor
1366 // block.
1367 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001368 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001369 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001370 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001371 }
Mike Stump31feda52009-07-17 01:31:16 +00001372
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001373 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001374 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001375
Ted Kremenek9aae5132007-08-23 21:42:29 +00001376 if (Stmt* Else = I->getElse()) {
1377 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001378
Ted Kremenek9aae5132007-08-23 21:42:29 +00001379 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +00001380 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001381 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001382
1383 // If branch is not a compound statement create implicit scope
1384 // and add destructors.
1385 if (!isa<CompoundStmt>(Else))
1386 addLocalScopeAndDtors(Else);
1387
Ted Kremenek93668002009-07-17 22:18:43 +00001388 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +00001389
Ted Kremenekbbad8ce2007-08-30 18:13:31 +00001390 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1391 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +00001392 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001393 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001394 return 0;
1395 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001396 }
Mike Stump31feda52009-07-17 01:31:16 +00001397
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001398 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001399 CFGBlock* ThenBlock;
1400 {
1401 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001402 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001403 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001404 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001405
1406 // If branch is not a compound statement create implicit scope
1407 // and add destructors.
1408 if (!isa<CompoundStmt>(Then))
1409 addLocalScopeAndDtors(Then);
1410
Ted Kremenek93668002009-07-17 22:18:43 +00001411 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +00001412
Ted Kremenek1b379512009-04-01 03:52:47 +00001413 if (!ThenBlock) {
1414 // We can reach here if the "then" body has all NullStmts.
1415 // Create an empty block so we can distinguish between true and false
1416 // branches in path-sensitive analyses.
1417 ThenBlock = createBlock(false);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001418 addSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001419 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001420 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001421 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001422 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001423 }
1424
Mike Stump31feda52009-07-17 01:31:16 +00001425 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001426 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001427
Ted Kremenek9aae5132007-08-23 21:42:29 +00001428 // Set the terminator of the new block to the If statement.
1429 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001430
Mike Stump773582d2009-07-23 23:25:26 +00001431 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001432 const TryResult &KnownVal = tryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001433
Ted Kremenek9aae5132007-08-23 21:42:29 +00001434 // Now add the successors.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001435 addSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1436 addSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001437
1438 // Add the condition as the last statement in the new block. This may create
1439 // new blocks as the condition may contain control-flow. Any newly created
1440 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001441 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001442
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001443 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1444 // and the condition variable initialization to the CFG.
1445 if (VarDecl *VD = I->getConditionVariable()) {
1446 if (Expr *Init = VD->getInit()) {
1447 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001448 appendStmt(Block, I, AddStmtChoice::AlwaysAdd);
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001449 addStmt(Init);
1450 }
1451 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001452
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001453 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001454}
Mike Stump31feda52009-07-17 01:31:16 +00001455
1456
Ted Kremenek9aae5132007-08-23 21:42:29 +00001457CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001458 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001459 //
Mike Stump31feda52009-07-17 01:31:16 +00001460 // NOTE: If a "return" appears in the middle of a block, this means that the
1461 // code afterwards is DEAD (unreachable). We still keep a basic block
1462 // for that code; a simple "mark-and-sweep" from the entry block will be
1463 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001464
1465 // Create the new block.
1466 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001467
Ted Kremenek9aae5132007-08-23 21:42:29 +00001468 // The Exit block is the only successor.
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001469 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001470 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001471
1472 // Add the return statement to the block. This may create new blocks if R
1473 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001474 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001475}
1476
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001477CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt *L) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001478 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001479 addStmt(L->getSubStmt());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001480 CFGBlock *LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001481
Ted Kremenek93668002009-07-17 22:18:43 +00001482 if (!LabelBlock) // This can happen when the body is empty, i.e.
1483 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001484
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001485 assert(LabelMap.find(L->getDecl()) == LabelMap.end() &&
1486 "label already in map");
1487 LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001488
1489 // Labels partition blocks, so this is the end of the basic block we were
1490 // processing (L is the block's label). Because this is label (and we have
1491 // already processed the substatement) there is no extra control-flow to worry
1492 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001493 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001494 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001495 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001496
1497 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001498 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001499
Ted Kremenek9aae5132007-08-23 21:42:29 +00001500 // This block is now the implicit successor of other blocks.
1501 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001502
Ted Kremenek9aae5132007-08-23 21:42:29 +00001503 return LabelBlock;
1504}
1505
1506CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001507 // Goto is a control-flow statement. Thus we stop processing the current
1508 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001509
Ted Kremenek9aae5132007-08-23 21:42:29 +00001510 Block = createBlock(false);
1511 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001512
1513 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001514 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001515
Ted Kremenek9aae5132007-08-23 21:42:29 +00001516 if (I == LabelMap.end())
1517 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001518 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1519 else {
1520 JumpTarget JT = I->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001521 addAutomaticObjDtors(ScopePos, JT.scopePosition, G);
1522 addSuccessor(Block, JT.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001523 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001524
Mike Stump31feda52009-07-17 01:31:16 +00001525 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001526}
1527
1528CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001529 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001530
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001531 // Save local scope position because in case of condition variable ScopePos
1532 // won't be restored when traversing AST.
1533 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1534
1535 // Create local scope for init statement and possible condition variable.
1536 // Add destructor for init statement and condition variable.
1537 // Store scope position for continue statement.
1538 if (Stmt* Init = F->getInit())
1539 addLocalScopeForStmt(Init);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001540 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1541
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001542 if (VarDecl* VD = F->getConditionVariable())
1543 addLocalScopeForVarDecl(VD);
1544 LocalScope::const_iterator ContinueScopePos = ScopePos;
1545
1546 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1547
Mike Stump014b3ea2009-07-21 01:12:51 +00001548 // "for" is a control-flow statement. Thus we stop processing the current
1549 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001550 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001551 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001552 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001553 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001554 } else
1555 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001556
Ted Kremenek304a9532010-05-21 20:30:15 +00001557 // Save the current value for the break targets.
1558 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001559 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001560 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001561
Mike Stump31feda52009-07-17 01:31:16 +00001562 // Because of short-circuit evaluation, the condition of the loop can span
1563 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1564 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001565 CFGBlock* ExitConditionBlock = createBlock(false);
1566 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001567
Ted Kremenek81e14852007-08-27 19:46:09 +00001568 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001569 ExitConditionBlock->setTerminator(F);
1570
1571 // Now add the actual condition to the condition block. Because the condition
1572 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001573 if (Stmt* C = F->getCond()) {
1574 Block = ExitConditionBlock;
1575 EntryConditionBlock = addStmt(C);
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001576 if (badCFG)
1577 return 0;
Ted Kremenek7b31a612010-09-15 07:01:20 +00001578 assert(Block == EntryConditionBlock ||
1579 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001580
1581 // If this block contains a condition variable, add both the condition
1582 // variable and initializer to the CFG.
1583 if (VarDecl *VD = F->getConditionVariable()) {
1584 if (Expr *Init = VD->getInit()) {
1585 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001586 appendStmt(Block, F, AddStmtChoice::AlwaysAdd);
Ted Kremenekec92f942009-12-24 01:49:06 +00001587 EntryConditionBlock = addStmt(Init);
1588 assert(Block == EntryConditionBlock);
1589 }
1590 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001591
Ted Kremenek55957a82009-05-02 00:13:27 +00001592 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001593 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001594 return 0;
1595 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001596 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001597
Mike Stump31feda52009-07-17 01:31:16 +00001598 // The condition block is the implicit successor for the loop body as well as
1599 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001600 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001601
Mike Stump773582d2009-07-23 23:25:26 +00001602 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001603 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001604
Mike Stump773582d2009-07-23 23:25:26 +00001605 if (F->getCond())
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001606 KnownVal = tryEvaluateBool(F->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001607
Ted Kremenek9aae5132007-08-23 21:42:29 +00001608 // Now create the loop body.
1609 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001610 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001611
Ted Kremenek304a9532010-05-21 20:30:15 +00001612 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001613 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1614 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001615
Ted Kremeneke9610502007-08-30 18:39:40 +00001616 // Create a new block to contain the (bottom) of the loop body.
1617 Block = NULL;
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001618
1619 // Loop body should end with destructor of Condition variable (if any).
1620 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump31feda52009-07-17 01:31:16 +00001621
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001622 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001623 // Generate increment code in its own basic block. This is the target of
1624 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001625 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001626 } else {
1627 // No increment code. Create a special, empty, block that is used as the
1628 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001629 assert(Succ == EntryConditionBlock);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001630 Succ = Block ? Block : createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001631 }
Mike Stump31feda52009-07-17 01:31:16 +00001632
Ted Kremenek902393b2009-04-28 00:51:56 +00001633 // Finish up the increment (or empty) block if it hasn't been already.
1634 if (Block) {
1635 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001636 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001637 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001638 Block = 0;
1639 }
Mike Stump31feda52009-07-17 01:31:16 +00001640
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001641 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001642
Ted Kremenek902393b2009-04-28 00:51:56 +00001643 // The starting block for the loop increment is the block that should
1644 // represent the 'loop target' for looping back to the start of the loop.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001645 ContinueJumpTarget.block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001646
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001647 // If body is not a compound statement create implicit scope
1648 // and add destructors.
1649 if (!isa<CompoundStmt>(F->getBody()))
1650 addLocalScopeAndDtors(F->getBody());
1651
Mike Stump31feda52009-07-17 01:31:16 +00001652 // Now populate the body block, and in the process create new blocks as we
1653 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001654 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001655
1656 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001657 BodyBlock = ContinueJumpTarget.block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001658 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001659 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001660
Ted Kremenek30754282009-07-24 04:47:11 +00001661 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001662 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001663 }
Mike Stump31feda52009-07-17 01:31:16 +00001664
Ted Kremenek30754282009-07-24 04:47:11 +00001665 // Link up the condition block with the code that follows the loop. (the
1666 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001667 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001668
Ted Kremenek9aae5132007-08-23 21:42:29 +00001669 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001670 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001671 if (Stmt* I = F->getInit()) {
1672 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001673 return addStmt(I);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001674 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001675
1676 // There is no loop initialization. We are thus basically a while loop.
1677 // NULL out Block to force lazy block construction.
1678 Block = NULL;
1679 Succ = EntryConditionBlock;
1680 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001681}
1682
Ted Kremenek5868ec62010-04-11 17:02:10 +00001683CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1684 if (asc.alwaysAdd()) {
1685 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001686 appendStmt(Block, M, asc);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001687 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001688 return Visit(M->getBase());
Ted Kremenek5868ec62010-04-11 17:02:10 +00001689}
1690
Ted Kremenek9d56e642008-11-11 17:10:00 +00001691CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1692 // Objective-C fast enumeration 'for' statements:
1693 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1694 //
1695 // for ( Type newVariable in collection_expression ) { statements }
1696 //
1697 // becomes:
1698 //
1699 // prologue:
1700 // 1. collection_expression
1701 // T. jump to loop_entry
1702 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001703 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001704 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1705 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1706 // TB:
1707 // statements
1708 // T. jump to loop_entry
1709 // FB:
1710 // what comes after
1711 //
1712 // and
1713 //
1714 // Type existingItem;
1715 // for ( existingItem in expression ) { statements }
1716 //
1717 // becomes:
1718 //
Mike Stump31feda52009-07-17 01:31:16 +00001719 // the same with newVariable replaced with existingItem; the binding works
1720 // the same except that for one ObjCForCollectionStmt::getElement() returns
1721 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001722 //
Mike Stump31feda52009-07-17 01:31:16 +00001723
Ted Kremenek9d56e642008-11-11 17:10:00 +00001724 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001725
Ted Kremenek9d56e642008-11-11 17:10:00 +00001726 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001727 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001728 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001729 LoopSuccessor = Block;
1730 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001731 } else
1732 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001733
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001734 // Build the condition blocks.
1735 CFGBlock* ExitConditionBlock = createBlock(false);
1736 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001737
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001738 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001739 ExitConditionBlock->setTerminator(S);
1740
1741 // The last statement in the block should be the ObjCForCollectionStmt, which
1742 // performs the actual binding to 'element' and determines if there are any
1743 // more items in the collection.
Ted Kremenek8219b822010-12-16 07:46:53 +00001744 appendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001745 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001746
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001747 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001748 // generate new blocks as necesary. We DON'T add the statement by default to
1749 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001750 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001751 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001752 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001753 return 0;
1754 Block = 0;
1755 }
Mike Stump31feda52009-07-17 01:31:16 +00001756
1757 // The condition block is the implicit successor for the loop body as well as
1758 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001759 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001760
Ted Kremenek9d56e642008-11-11 17:10:00 +00001761 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001762 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001763 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001764 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1765 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1766 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001767
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001768 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1769 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001770
Ted Kremenek93668002009-07-17 22:18:43 +00001771 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001772
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001773 if (!BodyBlock)
1774 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001775 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001776 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001777 return 0;
1778 }
Mike Stump31feda52009-07-17 01:31:16 +00001779
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001780 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001781 addSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001782 }
Mike Stump31feda52009-07-17 01:31:16 +00001783
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001784 // Link up the condition block with the code that follows the loop.
1785 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001786 addSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001787
Ted Kremenek9d56e642008-11-11 17:10:00 +00001788 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001789 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001790 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001791}
1792
Ted Kremenek49805452009-05-02 01:49:13 +00001793CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1794 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001795
Ted Kremenek49805452009-05-02 01:49:13 +00001796 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001797 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001798
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001799 // The sync body starts its own basic block. This makes it a little easier
1800 // for diagnostic clients.
1801 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001802 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001803 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001804
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001805 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001806 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001807 }
Mike Stump31feda52009-07-17 01:31:16 +00001808
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001809 // Add the @synchronized to the CFG.
1810 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001811 appendStmt(Block, S, AddStmtChoice::AlwaysAdd);
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001812
Ted Kremenek49805452009-05-02 01:49:13 +00001813 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001814 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001815}
Mike Stump31feda52009-07-17 01:31:16 +00001816
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001817CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001818 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001819 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001820}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001821
Ted Kremenek9aae5132007-08-23 21:42:29 +00001822CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001823 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001824
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001825 // Save local scope position because in case of condition variable ScopePos
1826 // won't be restored when traversing AST.
1827 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1828
1829 // Create local scope for possible condition variable.
1830 // Store scope position for continue statement.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001831 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001832 if (VarDecl* VD = W->getConditionVariable()) {
1833 addLocalScopeForVarDecl(VD);
1834 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1835 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001836
Mike Stump014b3ea2009-07-21 01:12:51 +00001837 // "while" is a control-flow statement. Thus we stop processing the current
1838 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001839 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001840 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001841 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001842 LoopSuccessor = Block;
Ted Kremenek828f6312011-02-21 22:11:26 +00001843 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001844 } else
1845 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001846
1847 // Because of short-circuit evaluation, the condition of the loop can span
1848 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1849 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001850 CFGBlock* ExitConditionBlock = createBlock(false);
1851 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001852
Ted Kremenek81e14852007-08-27 19:46:09 +00001853 // Set the terminator for the "exit" condition block.
1854 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001855
1856 // Now add the actual condition to the condition block. Because the condition
1857 // itself may contain control-flow, new blocks may be created. Thus we update
1858 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001859 if (Stmt* C = W->getCond()) {
1860 Block = ExitConditionBlock;
1861 EntryConditionBlock = addStmt(C);
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001862 // The condition might finish the current 'Block'.
1863 Block = EntryConditionBlock;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001864
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001865 // If this block contains a condition variable, add both the condition
1866 // variable and initializer to the CFG.
1867 if (VarDecl *VD = W->getConditionVariable()) {
1868 if (Expr *Init = VD->getInit()) {
1869 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001870 appendStmt(Block, W, AddStmtChoice::AlwaysAdd);
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001871 EntryConditionBlock = addStmt(Init);
1872 assert(Block == EntryConditionBlock);
1873 }
1874 }
1875
Ted Kremenek55957a82009-05-02 00:13:27 +00001876 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001877 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001878 return 0;
1879 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001880 }
Mike Stump31feda52009-07-17 01:31:16 +00001881
1882 // The condition block is the implicit successor for the loop body as well as
1883 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001884 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001885
Mike Stump773582d2009-07-23 23:25:26 +00001886 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001887 const TryResult& KnownVal = tryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001888
Ted Kremenek9aae5132007-08-23 21:42:29 +00001889 // Process the loop body.
1890 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001891 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001892
1893 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001894 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1895 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1896 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001897
Mike Stump31feda52009-07-17 01:31:16 +00001898 // Create an empty block to represent the transition block for looping back
1899 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001900 Block = 0;
1901 assert(Succ == EntryConditionBlock);
1902 Succ = createBlock();
1903 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001904 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001905
Ted Kremenek9aae5132007-08-23 21:42:29 +00001906 // All breaks should go to the code following the loop.
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001907 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001908
Ted Kremenek9aae5132007-08-23 21:42:29 +00001909 // NULL out Block to force lazy instantiation of blocks for the body.
1910 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001911
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001912 // Loop body should end with destructor of Condition variable (if any).
1913 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1914
1915 // If body is not a compound statement create implicit scope
1916 // and add destructors.
1917 if (!isa<CompoundStmt>(W->getBody()))
1918 addLocalScopeAndDtors(W->getBody());
1919
Ted Kremenek9aae5132007-08-23 21:42:29 +00001920 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001921 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001922
Ted Kremeneke9610502007-08-30 18:39:40 +00001923 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001924 BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001925 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001926 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001927 return 0;
1928 }
Mike Stump31feda52009-07-17 01:31:16 +00001929
Ted Kremenek30754282009-07-24 04:47:11 +00001930 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001931 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001932 }
Mike Stump31feda52009-07-17 01:31:16 +00001933
Ted Kremenek30754282009-07-24 04:47:11 +00001934 // Link up the condition block with the code that follows the loop. (the
1935 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001936 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001937
1938 // There can be no more statements in the condition block since we loop back
1939 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001940 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001941
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001942 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001943 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001944 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001945}
Mike Stump11289f42009-09-09 15:08:12 +00001946
1947
Ted Kremenek93668002009-07-17 22:18:43 +00001948CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1949 // FIXME: For now we pretend that @catch and the code it contains does not
1950 // exit.
1951 return Block;
1952}
Mike Stump31feda52009-07-17 01:31:16 +00001953
Ted Kremenek93041ba2008-12-09 20:20:09 +00001954CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1955 // FIXME: This isn't complete. We basically treat @throw like a return
1956 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001957
Ted Kremenek0868eea2009-09-24 18:45:41 +00001958 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001959 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001960 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001961
Ted Kremenek93041ba2008-12-09 20:20:09 +00001962 // Create the new block.
1963 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001964
Ted Kremenek93041ba2008-12-09 20:20:09 +00001965 // The Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001966 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001967
1968 // Add the statement to the block. This may create new blocks if S contains
1969 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001970 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001971}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001972
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001973CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001974 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001975 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001976 return 0;
1977
1978 // Create the new block.
1979 Block = createBlock(false);
1980
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001981 if (TryTerminatedBlock)
1982 // The current try statement is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001983 addSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001984 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001985 // otherwise the Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001986 addSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001987
1988 // Add the statement to the block. This may create new blocks if S contains
1989 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001990 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001991}
1992
Ted Kremenek93668002009-07-17 22:18:43 +00001993CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001994 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001995
Mike Stump8d50b6a2009-07-21 01:27:50 +00001996 // "do...while" is a control-flow statement. Thus we stop processing the
1997 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001998 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001999 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002000 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002001 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002002 } else
2003 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00002004
2005 // Because of short-circuit evaluation, the condition of the loop can span
2006 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
2007 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00002008 CFGBlock* ExitConditionBlock = createBlock(false);
2009 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002010
Ted Kremenek81e14852007-08-27 19:46:09 +00002011 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00002012 ExitConditionBlock->setTerminator(D);
2013
2014 // Now add the actual condition to the condition block. Because the condition
2015 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00002016 if (Stmt* C = D->getCond()) {
2017 Block = ExitConditionBlock;
2018 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00002019 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002020 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002021 return 0;
2022 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002023 }
Mike Stump31feda52009-07-17 01:31:16 +00002024
Ted Kremeneka1523a32008-02-27 07:20:00 +00002025 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002026 Succ = EntryConditionBlock;
2027
Mike Stump773582d2009-07-23 23:25:26 +00002028 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002029 const TryResult &KnownVal = tryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00002030
Ted Kremenek9aae5132007-08-23 21:42:29 +00002031 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002032 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002033 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002034 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002035
Ted Kremenek9aae5132007-08-23 21:42:29 +00002036 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002037 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2038 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2039 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00002040
Ted Kremenek9aae5132007-08-23 21:42:29 +00002041 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002042 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002043
Ted Kremenek9aae5132007-08-23 21:42:29 +00002044 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002045 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002046
Ted Kremenek9aae5132007-08-23 21:42:29 +00002047 // NULL out Block to force lazy instantiation of blocks for the body.
2048 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002049
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00002050 // If body is not a compound statement create implicit scope
2051 // and add destructors.
2052 if (!isa<CompoundStmt>(D->getBody()))
2053 addLocalScopeAndDtors(D->getBody());
2054
Ted Kremenek9aae5132007-08-23 21:42:29 +00002055 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00002056 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002057
Ted Kremeneke9610502007-08-30 18:39:40 +00002058 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00002059 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00002060 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002061 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002062 return 0;
2063 }
Mike Stump31feda52009-07-17 01:31:16 +00002064
Ted Kremenek110974d2010-08-17 20:59:56 +00002065 if (!KnownVal.isFalse()) {
2066 // Add an intermediate block between the BodyBlock and the
2067 // ExitConditionBlock to represent the "loop back" transition. Create an
2068 // empty block to represent the transition block for looping back to the
2069 // head of the loop.
2070 // FIXME: Can we do this more efficiently without adding another block?
2071 Block = NULL;
2072 Succ = BodyBlock;
2073 CFGBlock *LoopBackBlock = createBlock();
2074 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00002075
Ted Kremenek110974d2010-08-17 20:59:56 +00002076 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002077 addSuccessor(ExitConditionBlock, LoopBackBlock);
Ted Kremenek110974d2010-08-17 20:59:56 +00002078 }
2079 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002080 addSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002081 }
Mike Stump31feda52009-07-17 01:31:16 +00002082
Ted Kremenek30754282009-07-24 04:47:11 +00002083 // Link up the condition block with the code that follows the loop.
2084 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002085 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00002086
2087 // There can be no more statements in the body block(s) since we loop back to
2088 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002089 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002090
Ted Kremenek9aae5132007-08-23 21:42:29 +00002091 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00002092 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002093 return BodyBlock;
2094}
2095
2096CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2097 // "continue" is a control-flow statement. Thus we stop processing the
2098 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002099 if (badCFG)
2100 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002101
Ted Kremenek9aae5132007-08-23 21:42:29 +00002102 // Now create a new block that ends with the continue statement.
2103 Block = createBlock(false);
2104 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00002105
Ted Kremenek9aae5132007-08-23 21:42:29 +00002106 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00002107 // incomplete AST. This means the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00002108 if (ContinueJumpTarget.block) {
2109 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C);
2110 addSuccessor(Block, ContinueJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002111 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00002112 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00002113
Ted Kremenek9aae5132007-08-23 21:42:29 +00002114 return Block;
2115}
Mike Stump11289f42009-09-09 15:08:12 +00002116
Ted Kremenek0747de62009-07-18 00:47:21 +00002117CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002118 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002119
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002120 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002121 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002122 appendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00002123 }
Mike Stump11289f42009-09-09 15:08:12 +00002124
Ted Kremenek93668002009-07-17 22:18:43 +00002125 // VLA types have expressions that must be evaluated.
2126 if (E->isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00002127 for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr());
Ted Kremenek93668002009-07-17 22:18:43 +00002128 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2129 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00002130 }
Mike Stump11289f42009-09-09 15:08:12 +00002131
Mike Stump31feda52009-07-17 01:31:16 +00002132 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002133}
Mike Stump11289f42009-09-09 15:08:12 +00002134
Ted Kremenek93668002009-07-17 22:18:43 +00002135/// VisitStmtExpr - Utility method to handle (nested) statement
2136/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002137CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2138 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002139 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002140 appendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00002141 }
Ted Kremenek93668002009-07-17 22:18:43 +00002142 return VisitCompoundStmt(SE->getSubStmt());
2143}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002144
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002145CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00002146 // "switch" is a control-flow statement. Thus we stop processing the current
2147 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002148 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002149
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002150 // Save local scope position because in case of condition variable ScopePos
2151 // won't be restored when traversing AST.
2152 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2153
2154 // Create local scope for possible condition variable.
2155 // Store scope position. Add implicit destructor.
2156 if (VarDecl* VD = Terminator->getConditionVariable()) {
2157 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2158 addLocalScopeForVarDecl(VD);
2159 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2160 }
2161
Ted Kremenek9aae5132007-08-23 21:42:29 +00002162 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002163 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002164 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002165 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00002166 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002167
2168 // Save the current "switch" context.
2169 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00002170 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002171 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00002172
Mike Stump31feda52009-07-17 01:31:16 +00002173 // Set the "default" case to be the block after the switch statement. If the
2174 // switch statement contains a "default:", this value will be overwritten with
2175 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00002176 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00002177
Ted Kremenek9aae5132007-08-23 21:42:29 +00002178 // Create a new block that will contain the switch statement.
2179 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00002180
Ted Kremenek9aae5132007-08-23 21:42:29 +00002181 // Now process the switch body. The code after the switch is the implicit
2182 // successor.
2183 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002184 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002185
2186 // When visiting the body, the case statements should automatically get linked
2187 // up to the switch. We also don't keep a pointer to the body, since all
2188 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002189 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00002190 Block = NULL;
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002191
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002192 // For pruning unreachable case statements, save the current state
2193 // for tracking the condition value.
2194 SaveAndRestore<bool> save_switchExclusivelyCovered(switchExclusivelyCovered,
2195 false);
Ted Kremenekbe528712011-03-04 01:03:41 +00002196
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002197 // Determine if the switch condition can be explicitly evaluated.
2198 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekbe528712011-03-04 01:03:41 +00002199 Expr::EvalResult result;
2200 tryEvaluate(Terminator->getCond(), result);
2201 SaveAndRestore<Expr::EvalResult*> save_switchCond(switchCond, &result);
2202
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002203 // If body is not a compound statement create implicit scope
2204 // and add destructors.
2205 if (!isa<CompoundStmt>(Terminator->getBody()))
2206 addLocalScopeAndDtors(Terminator->getBody());
2207
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002208 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00002209 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002210 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002211 return 0;
2212 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002213
Mike Stump31feda52009-07-17 01:31:16 +00002214 // If we have no "default:" case, the default transition is to the code
2215 // following the switch body.
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002216 addSuccessor(SwitchTerminatedBlock,
2217 switchExclusivelyCovered ? 0 : DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002218
Ted Kremenek81e14852007-08-27 19:46:09 +00002219 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002220 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002221 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002222 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002223
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002224 // Finally, if the SwitchStmt contains a condition variable, add both the
2225 // SwitchStmt and the condition variable initialization to the CFG.
2226 if (VarDecl *VD = Terminator->getConditionVariable()) {
2227 if (Expr *Init = VD->getInit()) {
2228 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002229 appendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002230 addStmt(Init);
2231 }
2232 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002233
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002234 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002235}
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002236
2237static bool shouldAddCase(bool &switchExclusivelyCovered,
2238 const Expr::EvalResult &switchCond,
2239 const CaseStmt *CS,
2240 ASTContext &Ctx) {
2241 bool addCase = false;
Ted Kremenekbe528712011-03-04 01:03:41 +00002242
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002243 if (!switchExclusivelyCovered) {
2244 if (switchCond.Val.isInt()) {
2245 // Evaluate the LHS of the case value.
2246 Expr::EvalResult V1;
2247 CS->getLHS()->Evaluate(V1, Ctx);
2248 assert(V1.Val.isInt());
2249 const llvm::APSInt &condInt = switchCond.Val.getInt();
2250 const llvm::APSInt &lhsInt = V1.Val.getInt();
2251
2252 if (condInt == lhsInt) {
2253 addCase = true;
2254 switchExclusivelyCovered = true;
2255 }
2256 else if (condInt < lhsInt) {
2257 if (const Expr *RHS = CS->getRHS()) {
2258 // Evaluate the RHS of the case value.
2259 Expr::EvalResult V2;
2260 RHS->Evaluate(V2, Ctx);
2261 assert(V2.Val.isInt());
2262 if (V2.Val.getInt() <= condInt) {
2263 addCase = true;
2264 switchExclusivelyCovered = true;
2265 }
2266 }
2267 }
2268 }
2269 else
2270 addCase = true;
2271 }
2272 return addCase;
2273}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002274
Ted Kremenek93668002009-07-17 22:18:43 +00002275CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00002276 // CaseStmts are essentially labels, so they are the first statement in a
2277 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00002278 CFGBlock *TopBlock = 0, *LastBlock = 0;
Ted Kremenekbe528712011-03-04 01:03:41 +00002279 assert(switchCond);
2280
Ted Kremenek60fa6572010-08-04 23:54:30 +00002281 if (Stmt *Sub = CS->getSubStmt()) {
2282 // For deeply nested chains of CaseStmts, instead of doing a recursion
2283 // (which can blow out the stack), manually unroll and create blocks
2284 // along the way.
2285 while (isa<CaseStmt>(Sub)) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002286 CFGBlock *currentBlock = createBlock(false);
2287 currentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00002288
Ted Kremenek60fa6572010-08-04 23:54:30 +00002289 if (TopBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002290 addSuccessor(LastBlock, currentBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002291 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002292 TopBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002293
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002294 addSuccessor(SwitchTerminatedBlock,
Ted Kremenekbe528712011-03-04 01:03:41 +00002295 shouldAddCase(switchExclusivelyCovered, *switchCond,
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002296 CS, *Context)
2297 ? currentBlock : 0);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002298
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002299 LastBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002300 CS = cast<CaseStmt>(Sub);
2301 Sub = CS->getSubStmt();
2302 }
2303
2304 addStmt(Sub);
2305 }
Mike Stump11289f42009-09-09 15:08:12 +00002306
Ted Kremenek55e91e82007-08-30 18:48:11 +00002307 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002308 if (!CaseBlock)
2309 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002310
2311 // Cases statements partition blocks, so this is the top of the basic block we
2312 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00002313 CaseBlock->setLabel(CS);
2314
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002315 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002316 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002317
2318 // Add this block to the list of successors for the block with the switch
2319 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00002320 assert(SwitchTerminatedBlock);
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002321 addSuccessor(SwitchTerminatedBlock,
Ted Kremenekbe528712011-03-04 01:03:41 +00002322 shouldAddCase(switchExclusivelyCovered, *switchCond,
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002323 CS, *Context)
2324 ? CaseBlock : 0);
Mike Stump31feda52009-07-17 01:31:16 +00002325
Ted Kremenek9aae5132007-08-23 21:42:29 +00002326 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2327 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002328
Ted Kremenek60fa6572010-08-04 23:54:30 +00002329 if (TopBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002330 addSuccessor(LastBlock, CaseBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002331 Succ = TopBlock;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002332 } else {
Ted Kremenek60fa6572010-08-04 23:54:30 +00002333 // This block is now the implicit successor of other blocks.
2334 Succ = CaseBlock;
2335 }
Mike Stump31feda52009-07-17 01:31:16 +00002336
Ted Kremenek60fa6572010-08-04 23:54:30 +00002337 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002338}
Mike Stump31feda52009-07-17 01:31:16 +00002339
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002340CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00002341 if (Terminator->getSubStmt())
2342 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00002343
Ted Kremenek654c78f2008-02-13 22:05:39 +00002344 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002345
2346 if (!DefaultCaseBlock)
2347 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002348
2349 // Default statements partition blocks, so this is the top of the basic block
2350 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002351 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00002352
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002353 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002354 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00002355
Mike Stump31feda52009-07-17 01:31:16 +00002356 // Unlike case statements, we don't add the default block to the successors
2357 // for the switch statement immediately. This is done when we finish
2358 // processing the switch statement. This allows for the default case
2359 // (including a fall-through to the code after the switch statement) to always
2360 // be the last successor of a switch-terminated block.
2361
Ted Kremenek654c78f2008-02-13 22:05:39 +00002362 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2363 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002364
Ted Kremenek654c78f2008-02-13 22:05:39 +00002365 // This block is now the implicit successor of other blocks.
2366 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002367
2368 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00002369}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002370
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002371CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2372 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2373 // current block.
2374 CFGBlock* TrySuccessor = NULL;
2375
2376 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002377 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002378 return 0;
2379 TrySuccessor = Block;
2380 } else TrySuccessor = Succ;
2381
Mike Stump0bdba6c2010-01-20 01:15:34 +00002382 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002383
2384 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00002385 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002386 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00002387 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002388
Mike Stump0bdba6c2010-01-20 01:15:34 +00002389 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002390 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2391 // The code after the try is the implicit successor.
2392 Succ = TrySuccessor;
2393 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002394 if (CS->getExceptionDecl() == 0) {
2395 HasCatchAll = true;
2396 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002397 Block = NULL;
2398 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2399 if (CatchBlock == 0)
2400 return 0;
2401 // Add this block to the list of successors for the block with the try
2402 // statement.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002403 addSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002404 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00002405 if (!HasCatchAll) {
2406 if (PrevTryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002407 addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002408 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002409 addSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00002410 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002411
2412 // The code after the try is the implicit successor.
2413 Succ = TrySuccessor;
2414
Mike Stump845384a2010-01-20 01:30:58 +00002415 // Save the current "try" context.
2416 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2417 TryTerminatedBlock = NewTryTerminatedBlock;
2418
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002419 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002420 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002421 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002422 return Block;
2423}
2424
2425CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2426 // CXXCatchStmt are treated like labels, so they are the first statement in a
2427 // block.
2428
Marcin Swiderski3546b1a2010-10-01 01:46:52 +00002429 // Save local scope position because in case of exception variable ScopePos
2430 // won't be restored when traversing AST.
2431 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2432
2433 // Create local scope for possible exception variable.
2434 // Store scope position. Add implicit destructor.
2435 if (VarDecl* VD = CS->getExceptionDecl()) {
2436 LocalScope::const_iterator BeginScopePos = ScopePos;
2437 addLocalScopeForVarDecl(VD);
2438 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2439 }
2440
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002441 if (CS->getHandlerBlock())
2442 addStmt(CS->getHandlerBlock());
2443
2444 CFGBlock* CatchBlock = Block;
2445 if (!CatchBlock)
2446 CatchBlock = createBlock();
2447
2448 CatchBlock->setLabel(CS);
2449
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002450 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002451 return 0;
2452
2453 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2454 Block = NULL;
2455
2456 return CatchBlock;
2457}
2458
John McCall5d413782010-12-06 08:20:24 +00002459CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002460 AddStmtChoice asc) {
2461 if (BuildOpts.AddImplicitDtors) {
2462 // If adding implicit destructors visit the full expression for adding
2463 // destructors of temporaries.
2464 VisitForTemporaryDtors(E->getSubExpr());
2465
2466 // Full expression has to be added as CFGStmt so it will be sequenced
2467 // before destructors of it's temporaries.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002468 asc = asc.withAlwaysAdd(true);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002469 }
2470 return Visit(E->getSubExpr(), asc);
2471}
2472
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002473CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2474 AddStmtChoice asc) {
2475 if (asc.alwaysAdd()) {
2476 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002477 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002478
2479 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002480 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002481 }
2482 return Visit(E->getSubExpr(), asc);
2483}
2484
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002485CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2486 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002487 autoCreateBlock();
Zhongxing Xufb2f8162010-11-03 11:14:06 +00002488 if (!C->isElidable())
Ted Kremenek8219b822010-12-16 07:46:53 +00002489 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002490
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002491 return VisitChildren(C);
2492}
2493
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002494CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2495 AddStmtChoice asc) {
2496 if (asc.alwaysAdd()) {
2497 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002498 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002499 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002500 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002501 }
2502 return Visit(E->getSubExpr(), asc);
2503}
2504
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002505CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2506 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002507 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002508 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002509 return VisitChildren(C);
2510}
2511
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002512CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00002513 AddStmtChoice asc) {
Zhongxing Xu7e612172010-04-13 09:38:01 +00002514 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002515 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu7e612172010-04-13 09:38:01 +00002516 return VisitChildren(C);
2517}
2518
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002519CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2520 AddStmtChoice asc) {
2521 if (asc.alwaysAdd()) {
2522 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002523 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002524 }
Ted Kremenek8219b822010-12-16 07:46:53 +00002525 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002526}
2527
Ted Kremenekeda180e22007-08-28 19:26:49 +00002528CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00002529 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00002530 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002531
Ted Kremenekeda180e22007-08-28 19:26:49 +00002532 if (!IBlock) {
2533 IBlock = createBlock(false);
2534 cfg->setIndirectGotoBlock(IBlock);
2535 }
Mike Stump31feda52009-07-17 01:31:16 +00002536
Ted Kremenekeda180e22007-08-28 19:26:49 +00002537 // IndirectGoto is a control-flow statement. Thus we stop processing the
2538 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002539 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00002540 return 0;
2541
Ted Kremenekeda180e22007-08-28 19:26:49 +00002542 Block = createBlock(false);
2543 Block->setTerminator(I);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002544 addSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00002545 return addStmt(I->getTarget());
2546}
2547
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002548CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2549tryAgain:
2550 if (!E) {
2551 badCFG = true;
2552 return NULL;
2553 }
2554 switch (E->getStmtClass()) {
2555 default:
2556 return VisitChildrenForTemporaryDtors(E);
2557
2558 case Stmt::BinaryOperatorClass:
2559 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2560
2561 case Stmt::CXXBindTemporaryExprClass:
2562 return VisitCXXBindTemporaryExprForTemporaryDtors(
2563 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2564
John McCallc07a0c72011-02-17 10:25:35 +00002565 case Stmt::BinaryConditionalOperatorClass:
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002566 case Stmt::ConditionalOperatorClass:
2567 return VisitConditionalOperatorForTemporaryDtors(
John McCallc07a0c72011-02-17 10:25:35 +00002568 cast<AbstractConditionalOperator>(E), BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002569
2570 case Stmt::ImplicitCastExprClass:
2571 // For implicit cast we want BindToTemporary to be passed further.
2572 E = cast<CastExpr>(E)->getSubExpr();
2573 goto tryAgain;
2574
2575 case Stmt::ParenExprClass:
2576 E = cast<ParenExpr>(E)->getSubExpr();
2577 goto tryAgain;
2578 }
2579}
2580
2581CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2582 // When visiting children for destructors we want to visit them in reverse
2583 // order. Because there's no reverse iterator for children must to reverse
2584 // them in helper vector.
2585 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2586 ChildrenVect ChildrenRev;
John McCall8322c3a2011-02-13 04:07:26 +00002587 for (Stmt::child_range I = E->children(); I; ++I) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002588 if (*I) ChildrenRev.push_back(*I);
2589 }
2590
2591 CFGBlock *B = Block;
2592 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2593 L = ChildrenRev.rend(); I != L; ++I) {
2594 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2595 B = R;
2596 }
2597 return B;
2598}
2599
2600CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2601 if (E->isLogicalOp()) {
2602 // Destructors for temporaries in LHS expression should be called after
2603 // those for RHS expression. Even if this will unnecessarily create a block,
2604 // this block will be used at least by the full expression.
2605 autoCreateBlock();
2606 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2607 if (badCFG)
2608 return NULL;
2609
2610 Succ = ConfluenceBlock;
2611 Block = NULL;
2612 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2613
2614 if (RHSBlock) {
2615 if (badCFG)
2616 return NULL;
2617
2618 // If RHS expression did produce destructors we need to connect created
2619 // blocks to CFG in same manner as for binary operator itself.
2620 CFGBlock *LHSBlock = createBlock(false);
2621 LHSBlock->setTerminator(CFGTerminator(E, true));
2622
2623 // For binary operator LHS block is before RHS in list of predecessors
2624 // of ConfluenceBlock.
2625 std::reverse(ConfluenceBlock->pred_begin(),
2626 ConfluenceBlock->pred_end());
2627
2628 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002629 TryResult KnownVal = tryEvaluateBool(E->getLHS());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002630 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2631 KnownVal.negate();
2632
2633 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2634 // itself.
2635 if (E->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002636 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2637 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002638 } else {
2639 assert (E->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002640 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2641 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002642 }
2643
2644 Block = LHSBlock;
2645 return LHSBlock;
2646 }
2647
2648 Block = ConfluenceBlock;
2649 return ConfluenceBlock;
2650 }
2651
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002652 if (E->isAssignmentOp()) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002653 // For assignment operator (=) LHS expression is visited
2654 // before RHS expression. For destructors visit them in reverse order.
2655 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2656 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2657 return LHSBlock ? LHSBlock : RHSBlock;
2658 }
2659
2660 // For any other binary operator RHS expression is visited before
2661 // LHS expression (order of children). For destructors visit them in reverse
2662 // order.
2663 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2664 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2665 return RHSBlock ? RHSBlock : LHSBlock;
2666}
2667
2668CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2669 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2670 // First add destructors for temporaries in subexpression.
2671 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xufee455f2010-11-14 15:23:50 +00002672 if (!BindToTemporary) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002673 // If lifetime of temporary is not prolonged (by assigning to constant
2674 // reference) add destructor for it.
2675 autoCreateBlock();
2676 appendTemporaryDtor(Block, E);
2677 B = Block;
2678 }
2679 return B;
2680}
2681
2682CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
John McCallc07a0c72011-02-17 10:25:35 +00002683 AbstractConditionalOperator *E, bool BindToTemporary) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002684 // First add destructors for condition expression. Even if this will
2685 // unnecessarily create a block, this block will be used at least by the full
2686 // expression.
2687 autoCreateBlock();
2688 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2689 if (badCFG)
2690 return NULL;
John McCallc07a0c72011-02-17 10:25:35 +00002691 if (BinaryConditionalOperator *BCO
2692 = dyn_cast<BinaryConditionalOperator>(E)) {
2693 ConfluenceBlock = VisitForTemporaryDtors(BCO->getCommon());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002694 if (badCFG)
2695 return NULL;
2696 }
2697
John McCallc07a0c72011-02-17 10:25:35 +00002698 // Try to add block with destructors for LHS expression.
2699 CFGBlock *LHSBlock = NULL;
2700 Succ = ConfluenceBlock;
2701 Block = NULL;
2702 LHSBlock = VisitForTemporaryDtors(E->getTrueExpr(), BindToTemporary);
2703 if (badCFG)
2704 return NULL;
2705
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002706 // Try to add block with destructors for RHS expression;
2707 Succ = ConfluenceBlock;
2708 Block = NULL;
John McCallc07a0c72011-02-17 10:25:35 +00002709 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getFalseExpr(),
2710 BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002711 if (badCFG)
2712 return NULL;
2713
2714 if (!RHSBlock && !LHSBlock) {
2715 // If neither LHS nor RHS expression had temporaries to destroy don't create
2716 // more blocks.
2717 Block = ConfluenceBlock;
2718 return Block;
2719 }
2720
2721 Block = createBlock(false);
2722 Block->setTerminator(CFGTerminator(E, true));
2723
2724 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002725 const TryResult &KnownVal = tryEvaluateBool(E->getCond());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002726
2727 if (LHSBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002728 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002729 } else if (KnownVal.isFalse()) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002730 addSuccessor(Block, NULL);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002731 } else {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002732 addSuccessor(Block, ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002733 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2734 }
2735
2736 if (!RHSBlock)
2737 RHSBlock = ConfluenceBlock;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002738 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002739
2740 return Block;
2741}
2742
Ted Kremenek04cca642007-08-23 21:26:19 +00002743} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00002744
Mike Stump31feda52009-07-17 01:31:16 +00002745/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2746/// no successors or predecessors. If this is the first block created in the
2747/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00002748CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00002749 bool first_block = begin() == end();
2750
2751 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002752 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2753 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2754 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00002755
2756 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002757 if (first_block)
2758 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00002759
2760 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002761 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002762}
2763
Ted Kremenek889073f2007-08-23 16:51:22 +00002764/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2765/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00002766CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenekf9d82902011-03-10 01:14:05 +00002767 const BuildOptions &BO) {
2768 CFGBuilder Builder(C, BO);
2769 return Builder.buildCFG(D, Statement);
Ted Kremenek889073f2007-08-23 16:51:22 +00002770}
2771
Ted Kremenek8cfe2072011-03-03 01:21:32 +00002772const CXXDestructorDecl *
2773CFGImplicitDtor::getDestructorDecl(ASTContext &astContext) const {
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002774 switch (getKind()) {
2775 case CFGElement::Invalid:
2776 case CFGElement::Statement:
2777 case CFGElement::Initializer:
2778 llvm_unreachable("getDestructorDecl should only be used with "
2779 "ImplicitDtors");
2780 case CFGElement::AutomaticObjectDtor: {
2781 const VarDecl *var = cast<CFGAutomaticObjDtor>(this)->getVarDecl();
2782 QualType ty = var->getType();
Ted Kremenek1676a042011-03-03 01:01:03 +00002783 ty = ty.getNonReferenceType();
Ted Kremenek8cfe2072011-03-03 01:21:32 +00002784 if (const ArrayType *arrayType = astContext.getAsArrayType(ty)) {
2785 ty = arrayType->getElementType();
2786 }
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002787 const RecordType *recordType = ty->getAs<RecordType>();
2788 const CXXRecordDecl *classDecl =
Ted Kremenek1676a042011-03-03 01:01:03 +00002789 cast<CXXRecordDecl>(recordType->getDecl());
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002790 return classDecl->getDestructor();
2791 }
2792 case CFGElement::TemporaryDtor: {
2793 const CXXBindTemporaryExpr *bindExpr =
2794 cast<CFGTemporaryDtor>(this)->getBindTemporaryExpr();
2795 const CXXTemporary *temp = bindExpr->getTemporary();
2796 return temp->getDestructor();
2797 }
2798 case CFGElement::BaseDtor:
2799 case CFGElement::MemberDtor:
2800
2801 // Not yet supported.
2802 return 0;
2803 }
Ted Kremenek1676a042011-03-03 01:01:03 +00002804 llvm_unreachable("getKind() returned bogus value");
Matt Beaumont-Gay86b900b2011-03-03 00:48:05 +00002805 return 0;
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002806}
2807
Ted Kremenek8cfe2072011-03-03 01:21:32 +00002808bool CFGImplicitDtor::isNoReturn(ASTContext &astContext) const {
2809 if (const CXXDestructorDecl *cdecl = getDestructorDecl(astContext)) {
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002810 QualType ty = cdecl->getType();
2811 return cast<FunctionType>(ty)->getNoReturnAttr();
2812 }
2813 return false;
Ted Kremenek96a7a592011-03-01 03:15:10 +00002814}
2815
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002816//===----------------------------------------------------------------------===//
2817// CFG: Queries for BlkExprs.
2818//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002819
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002820namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002821 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002822}
2823
Ted Kremenekbff98442009-12-23 23:37:10 +00002824static void FindSubExprAssignments(Stmt *S,
2825 llvm::SmallPtrSet<Expr*,50>& Set) {
2826 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00002827 return;
Mike Stump31feda52009-07-17 01:31:16 +00002828
John McCall8322c3a2011-02-13 04:07:26 +00002829 for (Stmt::child_range I = S->children(); I; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002830 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00002831 if (!child)
2832 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002833
Ted Kremenekbff98442009-12-23 23:37:10 +00002834 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002835 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00002836
Ted Kremenekbff98442009-12-23 23:37:10 +00002837 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00002838 }
2839}
2840
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002841static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2842 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00002843
2844 // Look for assignments that are used as subexpressions. These are the only
2845 // assignments that we want to *possibly* register as a block-level
2846 // expression. Basically, if an assignment occurs both in a subexpression and
2847 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00002848 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00002849
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002850 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002851 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek96a7a592011-03-01 03:15:10 +00002852 if (const CFGStmt *S = BI->getAs<CFGStmt>())
2853 FindSubExprAssignments(S->getStmt(), SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002854
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002855 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00002856
2857 // Iterate over the statements again on identify the Expr* and Stmt* at the
2858 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002859
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002860 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
Ted Kremenek96a7a592011-03-01 03:15:10 +00002861 const CFGStmt *CS = BI->getAs<CFGStmt>();
2862 if (!CS)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002863 continue;
Ted Kremenek96a7a592011-03-01 03:15:10 +00002864 if (Expr* Exp = dyn_cast<Expr>(CS->getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00002865
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002866 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002867 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00002868 // expression are really "statements" whose value is never used by
2869 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002870 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002871 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002872 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2873 // Special handling for statement expressions. The last statement in
2874 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002875 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002876 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002877 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002878 (*M)[C->body_back()] = x;
2879 }
2880 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002881
Ted Kremenek95a123c2008-01-26 00:03:27 +00002882 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002883 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002884 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002885 }
Mike Stump31feda52009-07-17 01:31:16 +00002886
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002887 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002888
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002889 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002890
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002891 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002892 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002893 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002894 }
2895 }
Mike Stump31feda52009-07-17 01:31:16 +00002896
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002897 return M;
2898}
2899
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002900CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2901 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002902 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002903
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002904 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002905 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002906 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002907}
2908
2909unsigned CFG::getNumBlkExprs() {
2910 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2911 return M->size();
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002912
2913 // We assume callers interested in the number of BlkExprs will want
2914 // the map constructed if it doesn't already exist.
2915 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2916 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002917}
2918
Ted Kremenek6065ef62008-04-28 18:00:46 +00002919//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002920// Filtered walking of the CFG.
2921//===----------------------------------------------------------------------===//
2922
2923bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002924 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002925
Ted Kremenek89794742011-03-07 22:04:39 +00002926 if (To && F.IgnoreDefaultsWithCoveredEnums) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002927 // If the 'To' has no label or is labeled but the label isn't a
2928 // CaseStmt then filter this edge.
2929 if (const SwitchStmt *S =
Ted Kremenek89794742011-03-07 22:04:39 +00002930 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002931 if (S->isAllEnumCasesCovered()) {
Ted Kremenek89794742011-03-07 22:04:39 +00002932 const Stmt *L = To->getLabel();
2933 if (!L || !isa<CaseStmt>(L))
2934 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002935 }
2936 }
2937 }
2938
2939 return false;
2940}
2941
2942//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002943// Cleanup: CFG dstor.
2944//===----------------------------------------------------------------------===//
2945
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002946CFG::~CFG() {
2947 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2948}
Mike Stump31feda52009-07-17 01:31:16 +00002949
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002950//===----------------------------------------------------------------------===//
2951// CFG pretty printing
2952//===----------------------------------------------------------------------===//
2953
Ted Kremenek7e776b12007-08-22 18:22:34 +00002954namespace {
2955
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002956class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek96a7a592011-03-01 03:15:10 +00002957 typedef llvm::DenseMap<const Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
2958 typedef llvm::DenseMap<const Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002959 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002960 DeclMapTy DeclMap;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002961 signed currentBlock;
2962 unsigned currentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002963 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002964public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002965
Chris Lattnerc61089a2009-06-30 01:26:17 +00002966 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
Ted Kremenek96a7a592011-03-01 03:15:10 +00002967 : currentBlock(0), currentStmt(0), LangOpts(LO)
2968 {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002969 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2970 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002971 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002972 BI != BEnd; ++BI, ++j ) {
Ted Kremenek96a7a592011-03-01 03:15:10 +00002973 if (const CFGStmt *SE = BI->getAs<CFGStmt>()) {
2974 const Stmt *stmt= SE->getStmt();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002975 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
Ted Kremenek96a7a592011-03-01 03:15:10 +00002976 StmtMap[stmt] = P;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002977
Ted Kremenek96a7a592011-03-01 03:15:10 +00002978 switch (stmt->getStmtClass()) {
2979 case Stmt::DeclStmtClass:
2980 DeclMap[cast<DeclStmt>(stmt)->getSingleDecl()] = P;
2981 break;
2982 case Stmt::IfStmtClass: {
2983 const VarDecl *var = cast<IfStmt>(stmt)->getConditionVariable();
2984 if (var)
2985 DeclMap[var] = P;
2986 break;
2987 }
2988 case Stmt::ForStmtClass: {
2989 const VarDecl *var = cast<ForStmt>(stmt)->getConditionVariable();
2990 if (var)
2991 DeclMap[var] = P;
2992 break;
2993 }
2994 case Stmt::WhileStmtClass: {
2995 const VarDecl *var =
2996 cast<WhileStmt>(stmt)->getConditionVariable();
2997 if (var)
2998 DeclMap[var] = P;
2999 break;
3000 }
3001 case Stmt::SwitchStmtClass: {
3002 const VarDecl *var =
3003 cast<SwitchStmt>(stmt)->getConditionVariable();
3004 if (var)
3005 DeclMap[var] = P;
3006 break;
3007 }
3008 case Stmt::CXXCatchStmtClass: {
3009 const VarDecl *var =
3010 cast<CXXCatchStmt>(stmt)->getExceptionDecl();
3011 if (var)
3012 DeclMap[var] = P;
3013 break;
3014 }
3015 default:
3016 break;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003017 }
3018 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003019 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00003020 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003021 }
Ted Kremenek96a7a592011-03-01 03:15:10 +00003022
Mike Stump31feda52009-07-17 01:31:16 +00003023
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003024 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00003025
Chris Lattnerc61089a2009-06-30 01:26:17 +00003026 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00003027 void setBlockID(signed i) { currentBlock = i; }
3028 void setStmtID(unsigned i) { currentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00003029
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003030 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
3031 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003032
3033 if (I == StmtMap.end())
3034 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003035
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00003036 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3037 && I->second.second == currentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003038 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00003039 }
Mike Stump31feda52009-07-17 01:31:16 +00003040
Ted Kremenek60983dc2010-01-19 20:52:05 +00003041 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003042 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003043 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003044
Ted Kremenek96a7a592011-03-01 03:15:10 +00003045 bool handleDecl(const Decl* D, llvm::raw_ostream& OS) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003046 DeclMapTy::iterator I = DeclMap.find(D);
3047
3048 if (I == DeclMap.end())
3049 return false;
3050
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00003051 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3052 && I->second.second == currentStmt) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003053 return false;
3054 }
3055
3056 OS << "[B" << I->second.first << "." << I->second.second << "]";
3057 return true;
3058 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003059};
Chris Lattnerc61089a2009-06-30 01:26:17 +00003060} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003061
Chris Lattnerc61089a2009-06-30 01:26:17 +00003062
3063namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00003064class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00003065 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00003066
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003067 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003068 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00003069 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003070public:
Douglas Gregor7de59662009-05-29 20:38:28 +00003071 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00003072 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00003073 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00003074
Ted Kremenek9aae5132007-08-23 21:42:29 +00003075 void VisitIfStmt(IfStmt* I) {
3076 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00003077 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00003078 }
Mike Stump31feda52009-07-17 01:31:16 +00003079
Ted Kremenek9aae5132007-08-23 21:42:29 +00003080 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00003081 void VisitStmt(Stmt* Terminator) {
3082 Terminator->printPretty(OS, Helper, Policy);
3083 }
3084
Ted Kremenek9aae5132007-08-23 21:42:29 +00003085 void VisitForStmt(ForStmt* F) {
3086 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00003087 if (F->getInit())
3088 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00003089 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00003090 if (Stmt* C = F->getCond())
3091 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00003092 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00003093 if (F->getInc())
3094 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00003095 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00003096 }
Mike Stump31feda52009-07-17 01:31:16 +00003097
Ted Kremenek9aae5132007-08-23 21:42:29 +00003098 void VisitWhileStmt(WhileStmt* W) {
3099 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00003100 if (Stmt* C = W->getCond())
3101 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00003102 }
Mike Stump31feda52009-07-17 01:31:16 +00003103
Ted Kremenek9aae5132007-08-23 21:42:29 +00003104 void VisitDoStmt(DoStmt* D) {
3105 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00003106 if (Stmt* C = D->getCond())
3107 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00003108 }
Mike Stump31feda52009-07-17 01:31:16 +00003109
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003110 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00003111 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00003112 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00003113 }
Mike Stump31feda52009-07-17 01:31:16 +00003114
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003115 void VisitCXXTryStmt(CXXTryStmt* CS) {
3116 OS << "try ...";
3117 }
3118
John McCallc07a0c72011-02-17 10:25:35 +00003119 void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00003120 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003121 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003122 }
Mike Stump31feda52009-07-17 01:31:16 +00003123
Ted Kremenek391f94a2007-08-31 22:29:13 +00003124 void VisitChooseExpr(ChooseExpr* C) {
3125 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00003126 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00003127 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00003128 }
Mike Stump31feda52009-07-17 01:31:16 +00003129
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003130 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
3131 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00003132 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003133 }
Mike Stump31feda52009-07-17 01:31:16 +00003134
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003135 void VisitBinaryOperator(BinaryOperator* B) {
3136 if (!B->isLogicalOp()) {
3137 VisitExpr(B);
3138 return;
3139 }
Mike Stump31feda52009-07-17 01:31:16 +00003140
Douglas Gregor7de59662009-05-29 20:38:28 +00003141 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003142
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003143 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00003144 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00003145 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003146 return;
John McCalle3027922010-08-25 11:45:40 +00003147 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00003148 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003149 return;
3150 default:
3151 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00003152 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003153 }
Mike Stump31feda52009-07-17 01:31:16 +00003154
Ted Kremenek12687ff2007-08-27 21:54:41 +00003155 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00003156 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003157 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00003158};
Chris Lattnerc61089a2009-06-30 01:26:17 +00003159} // end anonymous namespace
3160
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003161static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00003162 const CFGElement &E) {
Ted Kremenek96a7a592011-03-01 03:15:10 +00003163 if (const CFGStmt *CS = E.getAs<CFGStmt>()) {
3164 Stmt *S = CS->getStmt();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003165
3166 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00003167
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003168 // special printing for statement-expressions.
3169 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3170 CompoundStmt* Sub = SE->getSubStmt();
3171
John McCall8322c3a2011-02-13 04:07:26 +00003172 if (Sub->children()) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003173 OS << "({ ... ; ";
3174 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3175 OS << " })\n";
3176 return;
3177 }
3178 }
3179 // special printing for comma expressions.
3180 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3181 if (B->getOpcode() == BO_Comma) {
3182 OS << "... , ";
3183 Helper->handledStmt(B->getRHS(),OS);
3184 OS << '\n';
3185 return;
3186 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003187 }
3188 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003189 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00003190
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003191 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan59f09c72010-11-22 19:32:14 +00003192 OS << " (OperatorCall)";
3193 } else if (isa<CXXBindTemporaryExpr>(S)) {
3194 OS << " (BindTemporary)";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003195 }
Mike Stump31feda52009-07-17 01:31:16 +00003196
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003197 // Expressions need a newline.
3198 if (isa<Expr>(S))
3199 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00003200
Ted Kremenek96a7a592011-03-01 03:15:10 +00003201 } else if (const CFGInitializer *IE = E.getAs<CFGInitializer>()) {
3202 const CXXCtorInitializer *I = IE->getInitializer();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003203 if (I->isBaseInitializer())
3204 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichetd583da02010-12-04 09:14:42 +00003205 else OS << I->getAnyMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00003206
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003207 OS << "(";
3208 if (Expr* IE = I->getInit())
3209 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3210 OS << ")";
3211
3212 if (I->isBaseInitializer())
3213 OS << " (Base initializer)\n";
3214 else OS << " (Member initializer)\n";
3215
Ted Kremenek96a7a592011-03-01 03:15:10 +00003216 } else if (const CFGAutomaticObjDtor *DE = E.getAs<CFGAutomaticObjDtor>()){
3217 const VarDecl* VD = DE->getVarDecl();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003218 Helper->handleDecl(VD, OS);
3219
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003220 const Type* T = VD->getType().getTypePtr();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003221 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3222 T = RT->getPointeeType().getTypePtr();
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003223 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3224 T = ET;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003225
3226 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3227 OS << " (Implicit destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003228
Ted Kremenek96a7a592011-03-01 03:15:10 +00003229 } else if (const CFGBaseDtor *BE = E.getAs<CFGBaseDtor>()) {
3230 const CXXBaseSpecifier *BS = BE->getBaseSpecifier();
Marcin Swiderski20b88732010-10-05 05:37:00 +00003231 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003232 OS << " (Base object destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003233
Ted Kremenek96a7a592011-03-01 03:15:10 +00003234 } else if (const CFGMemberDtor *ME = E.getAs<CFGMemberDtor>()) {
3235 const FieldDecl *FD = ME->getFieldDecl();
Marcin Swiderski01769902010-10-25 07:05:54 +00003236
3237 const Type *T = FD->getType().getTypePtr();
3238 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3239 T = ET;
3240
Marcin Swiderski20b88732010-10-05 05:37:00 +00003241 OS << "this->" << FD->getName();
Marcin Swiderski01769902010-10-25 07:05:54 +00003242 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003243 OS << " (Member object destructor)\n";
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00003244
Ted Kremenek96a7a592011-03-01 03:15:10 +00003245 } else if (const CFGTemporaryDtor *TE = E.getAs<CFGTemporaryDtor>()) {
3246 const CXXBindTemporaryExpr *BT = TE->getBindTemporaryExpr();
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00003247 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3248 OS << " (Temporary object destructor)\n";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003249 }
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00003250}
Mike Stump31feda52009-07-17 01:31:16 +00003251
Chris Lattnerc61089a2009-06-30 01:26:17 +00003252static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3253 const CFGBlock& B,
3254 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00003255
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003256 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00003257
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003258 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00003259 OS << "\n [ B" << B.getBlockID();
3260
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003261 if (&B == &cfg->getEntry())
3262 OS << " (ENTRY) ]\n";
3263 else if (&B == &cfg->getExit())
3264 OS << " (EXIT) ]\n";
3265 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003266 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003267 else
3268 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00003269
Ted Kremenek71eca012007-08-29 23:20:49 +00003270 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00003271 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003272
3273 if (print_edges)
3274 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003275
Mike Stump92244b02010-01-19 22:00:14 +00003276 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003277 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00003278 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00003279 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003280 C->getLHS()->printPretty(OS, Helper,
3281 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003282 if (C->getRHS()) {
3283 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003284 C->getRHS()->printPretty(OS, Helper,
3285 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003286 }
Mike Stump92244b02010-01-19 22:00:14 +00003287 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003288 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00003289 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003290 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00003291 if (CS->getExceptionDecl())
3292 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3293 0);
3294 else
3295 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003296 OS << ")";
3297
3298 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003299 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00003300
Ted Kremenek71eca012007-08-29 23:20:49 +00003301 OS << ":\n";
3302 }
Mike Stump31feda52009-07-17 01:31:16 +00003303
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003304 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003305 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00003306
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003307 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3308 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00003309
Ted Kremenek71eca012007-08-29 23:20:49 +00003310 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003311 if (print_edges)
3312 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003313
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003314 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00003315
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003316 if (Helper)
3317 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00003318
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003319 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003320 }
Mike Stump31feda52009-07-17 01:31:16 +00003321
Ted Kremenek71eca012007-08-29 23:20:49 +00003322 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003323 if (B.getTerminator()) {
3324 if (print_edges)
3325 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003326
Ted Kremenek71eca012007-08-29 23:20:49 +00003327 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00003328
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003329 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00003330
Chris Lattnerc61089a2009-06-30 01:26:17 +00003331 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3332 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003333 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003334 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003335 }
Mike Stump31feda52009-07-17 01:31:16 +00003336
Ted Kremenek71eca012007-08-29 23:20:49 +00003337 if (print_edges) {
3338 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003339 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00003340 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00003341
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003342 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3343 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003344
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003345 if (i == 8 || (i-8) == 0)
3346 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00003347
Ted Kremenek71eca012007-08-29 23:20:49 +00003348 OS << " B" << (*I)->getBlockID();
3349 }
Mike Stump31feda52009-07-17 01:31:16 +00003350
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003351 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00003352
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003353 // Print the successors of this block.
3354 OS << " Successors (" << B.succ_size() << "):";
3355 i = 0;
3356
3357 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3358 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003359
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003360 if (i == 8 || (i-8) % 10 == 0)
3361 OS << "\n ";
3362
Mike Stump0d76d072009-07-20 23:24:15 +00003363 if (*I)
3364 OS << " B" << (*I)->getBlockID();
3365 else
3366 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003367 }
Mike Stump31feda52009-07-17 01:31:16 +00003368
Ted Kremenek71eca012007-08-29 23:20:49 +00003369 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003370 }
Mike Stump31feda52009-07-17 01:31:16 +00003371}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003372
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003373
3374/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003375void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003376
3377/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003378void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3379 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00003380
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003381 // Print the entry block.
3382 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00003383
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003384 // Iterate through the CFGBlocks and print them one by one.
3385 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3386 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003387 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003388 continue;
Mike Stump31feda52009-07-17 01:31:16 +00003389
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003390 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003391 }
Mike Stump31feda52009-07-17 01:31:16 +00003392
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003393 // Print the exit block.
3394 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00003395 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00003396}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003397
3398/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003399void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3400 print(llvm::errs(), cfg, LO);
3401}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003402
3403/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3404/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003405void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3406 const LangOptions &LO) const {
3407 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003408 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00003409}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003410
Ted Kremenek15647632008-01-30 23:02:42 +00003411/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003412void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00003413 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00003414 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003415 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003416}
3417
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003418Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003419 Stmt *Terminator = this->Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003420 if (!Terminator)
3421 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003422
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003423 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003424
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003425 switch (Terminator->getStmtClass()) {
3426 default:
3427 break;
Mike Stump31feda52009-07-17 01:31:16 +00003428
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003429 case Stmt::ForStmtClass:
3430 E = cast<ForStmt>(Terminator)->getCond();
3431 break;
Mike Stump31feda52009-07-17 01:31:16 +00003432
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003433 case Stmt::WhileStmtClass:
3434 E = cast<WhileStmt>(Terminator)->getCond();
3435 break;
Mike Stump31feda52009-07-17 01:31:16 +00003436
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003437 case Stmt::DoStmtClass:
3438 E = cast<DoStmt>(Terminator)->getCond();
3439 break;
Mike Stump31feda52009-07-17 01:31:16 +00003440
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003441 case Stmt::IfStmtClass:
3442 E = cast<IfStmt>(Terminator)->getCond();
3443 break;
Mike Stump31feda52009-07-17 01:31:16 +00003444
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003445 case Stmt::ChooseExprClass:
3446 E = cast<ChooseExpr>(Terminator)->getCond();
3447 break;
Mike Stump31feda52009-07-17 01:31:16 +00003448
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003449 case Stmt::IndirectGotoStmtClass:
3450 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3451 break;
Mike Stump31feda52009-07-17 01:31:16 +00003452
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003453 case Stmt::SwitchStmtClass:
3454 E = cast<SwitchStmt>(Terminator)->getCond();
3455 break;
Mike Stump31feda52009-07-17 01:31:16 +00003456
John McCallc07a0c72011-02-17 10:25:35 +00003457 case Stmt::BinaryConditionalOperatorClass:
3458 E = cast<BinaryConditionalOperator>(Terminator)->getCond();
3459 break;
3460
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003461 case Stmt::ConditionalOperatorClass:
3462 E = cast<ConditionalOperator>(Terminator)->getCond();
3463 break;
Mike Stump31feda52009-07-17 01:31:16 +00003464
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003465 case Stmt::BinaryOperatorClass: // '&&' and '||'
3466 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003467 break;
Mike Stump31feda52009-07-17 01:31:16 +00003468
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003469 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00003470 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003471 }
Mike Stump31feda52009-07-17 01:31:16 +00003472
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003473 return E ? E->IgnoreParens() : NULL;
3474}
3475
Ted Kremenek92137a32008-05-16 16:06:00 +00003476bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003477 const Stmt *Terminator = this->Terminator;
Ted Kremenek92137a32008-05-16 16:06:00 +00003478 if (!Terminator)
3479 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003480
Ted Kremenek92137a32008-05-16 16:06:00 +00003481 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003482
Ted Kremenek92137a32008-05-16 16:06:00 +00003483 switch (Terminator->getStmtClass()) {
3484 default:
3485 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003486
3487 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003488 case Stmt::WhileStmtClass:
3489 case Stmt::DoStmtClass:
3490 case Stmt::IfStmtClass:
3491 case Stmt::ChooseExprClass:
John McCallc07a0c72011-02-17 10:25:35 +00003492 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003493 case Stmt::ConditionalOperatorClass:
3494 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00003495 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00003496 }
Mike Stump31feda52009-07-17 01:31:16 +00003497
Ted Kremenek92137a32008-05-16 16:06:00 +00003498 return E ? E->IgnoreParens() : NULL;
3499}
3500
Ted Kremenek15647632008-01-30 23:02:42 +00003501
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003502//===----------------------------------------------------------------------===//
3503// CFG Graphviz Visualization
3504//===----------------------------------------------------------------------===//
3505
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003506
3507#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00003508static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003509#endif
3510
Chris Lattnerc61089a2009-06-30 01:26:17 +00003511void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003512#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00003513 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003514 GraphHelper = &H;
3515 llvm::ViewGraph(this,"CFG");
3516 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003517#endif
3518}
3519
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003520namespace llvm {
3521template<>
3522struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00003523
3524 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3525
3526 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003527
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003528#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003529 std::string OutSStr;
3530 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003531 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003532 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003533
3534 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3535
3536 // Process string output to make it nicer...
3537 for (unsigned i = 0; i != OutStr.length(); ++i)
3538 if (OutStr[i] == '\n') { // Left justify
3539 OutStr[i] = '\\';
3540 OutStr.insert(OutStr.begin()+i+1, 'l');
3541 }
Mike Stump31feda52009-07-17 01:31:16 +00003542
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003543 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003544#else
3545 return "";
3546#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003547 }
3548};
3549} // end namespace llvm