blob: 14d3625b8a458a63b1e1cf2ef80a844ae9a73526 [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 Kremenek2866bab2011-03-10 01:14:08 +0000387 void appendStmt(CFGBlock *B, Stmt *S) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000388 B->appendStmt(S, cfg->getBumpVectorContext());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000389 }
Alexis Hunt1d792652011-01-08 20:30:50 +0000390 void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000391 B->appendInitializer(I, cfg->getBumpVectorContext());
392 }
Marcin Swiderski20b88732010-10-05 05:37:00 +0000393 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
394 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
395 }
396 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
397 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
398 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000399 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
400 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
401 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000402
Marcin Swiderski321a7072010-09-30 22:54:37 +0000403 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
404 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
405 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
406 LocalScope::const_iterator E, Stmt* S);
407 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
408 LocalScope::const_iterator B, LocalScope::const_iterator E);
409
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000410 void addSuccessor(CFGBlock *B, CFGBlock *S) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000411 B->addSuccessor(S, cfg->getBumpVectorContext());
412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000414 /// Try and evaluate an expression to an integer constant.
415 bool tryEvaluate(Expr *S, Expr::EvalResult &outResult) {
416 if (!BuildOpts.PruneTriviallyFalseEdges)
417 return false;
418 return !S->isTypeDependent() &&
419 !S->isValueDependent() &&
420 S->Evaluate(outResult, *Context);
421 }
Mike Stump11289f42009-09-09 15:08:12 +0000422
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000423 /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
Mike Stump773582d2009-07-23 23:25:26 +0000424 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000425 TryResult tryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000426 Expr::EvalResult Result;
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000427 if (!tryEvaluate(S, Result))
428 return TryResult();
429
430 if (Result.Val.isInt())
431 return Result.Val.getInt().getBoolValue();
432
433 if (Result.Val.isLValue()) {
434 Expr *e = Result.Val.getLValueBase();
435 const CharUnits &c = Result.Val.getLValueOffset();
436 if (!e && c.isZero())
437 return false;
Ted Kremenek1a241d12011-02-23 05:11:46 +0000438 }
Ted Kremenek30754282009-07-24 04:47:11 +0000439 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000440 }
Ted Kremenekeff9a7f2011-03-01 23:12:55 +0000441
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000442};
Mike Stump31feda52009-07-17 01:31:16 +0000443
Douglas Gregor4619e432008-12-05 23:32:09 +0000444// FIXME: Add support for dependent-sized array types in C++?
445// Does it even make sense to build a CFG for an uninstantiated template?
John McCall424cec92011-01-19 06:33:43 +0000446static const VariableArrayType *FindVA(const Type *t) {
447 while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
448 if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000449 if (vat->getSizeExpr())
450 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000451
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000452 t = vt->getElementType().getTypePtr();
453 }
Mike Stump31feda52009-07-17 01:31:16 +0000454
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000455 return 0;
456}
Mike Stump31feda52009-07-17 01:31:16 +0000457
458/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
459/// arbitrary statement. Examples include a single expression or a function
460/// body (compound statement). The ownership of the returned CFG is
461/// transferred to the caller. If CFG construction fails, this method returns
462/// NULL.
Ted Kremenekf9d82902011-03-10 01:14:05 +0000463CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement) {
Ted Kremenek8aed4902009-10-20 23:46:25 +0000464 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000465 if (!Statement)
466 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000467
Mike Stump31feda52009-07-17 01:31:16 +0000468 // Create an empty block that will serve as the exit block for the CFG. Since
469 // this is the first block added to the CFG, it will be implicitly registered
470 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000471 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000472 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000473 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000474
Marcin Swiderski20b88732010-10-05 05:37:00 +0000475 if (BuildOpts.AddImplicitDtors)
476 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
477 addImplicitDtorsForDestructor(DD);
478
Ted Kremenek9aae5132007-08-23 21:42:29 +0000479 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000480 CFGBlock *B = addStmt(Statement);
481
482 if (badCFG)
483 return NULL;
484
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000485 // For C++ constructor add initializers to CFG.
486 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
487 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
488 E = CD->init_rend(); I != E; ++I) {
489 B = addInitializer(*I);
490 if (badCFG)
491 return NULL;
492 }
493 }
494
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000495 if (B)
496 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000497
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000498 // Backpatch the gotos whose label -> block mappings we didn't know when we
499 // encountered them.
500 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
501 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000502
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000503 CFGBlock* B = I->block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000504 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
505 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000506
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000507 // If there is no target for the goto, then we are looking at an
508 // incomplete AST. Handle this by not registering a successor.
509 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000510
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000511 JumpTarget JT = LI->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000512 prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
513 JT.scopePosition);
514 addSuccessor(B, JT.block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000515 }
516
517 // Add successors to the Indirect Goto Dispatch block (if we have one).
518 if (CFGBlock* B = cfg->getIndirectGotoBlock())
519 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
520 E = AddressTakenLabels.end(); I != E; ++I ) {
521
522 // Lookup the target block.
523 LabelMapTy::iterator LI = LabelMap.find(*I);
524
525 // If there is no target block that contains label, then we are looking
526 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000527 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000528
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000529 addSuccessor(B, LI->second.block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000530 }
Mike Stump31feda52009-07-17 01:31:16 +0000531
Mike Stump31feda52009-07-17 01:31:16 +0000532 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000533 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000534
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000535 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000536}
Mike Stump31feda52009-07-17 01:31:16 +0000537
Ted Kremenek9aae5132007-08-23 21:42:29 +0000538/// createBlock - Used to lazily create blocks that are connected
539/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000540CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000541 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000542 if (add_successor && Succ)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000543 addSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000544 return B;
545}
Mike Stump31feda52009-07-17 01:31:16 +0000546
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000547/// addInitializer - Add C++ base or member initializer element to CFG.
Alexis Hunt1d792652011-01-08 20:30:50 +0000548CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000549 if (!BuildOpts.AddInitializers)
550 return Block;
551
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000552 bool IsReference = false;
553 bool HasTemporaries = false;
554
555 // Destructors of temporaries in initialization expression should be called
556 // after initialization finishes.
557 Expr *Init = I->getInit();
558 if (Init) {
Francois Pichetd583da02010-12-04 09:14:42 +0000559 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000560 IsReference = FD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +0000561 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000562
563 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
564 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +0000565 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000566 IsReference);
567 }
568 }
569
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000570 autoCreateBlock();
571 appendInitializer(Block, I);
572
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000573 if (Init) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000574 if (HasTemporaries) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000575 // For expression with temporaries go directly to subexpression to omit
576 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +0000577 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
578 }
579 return Visit(Init);
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000580 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000581
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000582 return Block;
583}
584
Marcin Swiderski5e415732010-09-30 23:05:00 +0000585/// addAutomaticObjDtors - Add to current block automatic objects destructors
586/// for objects in range of local scope positions. Use S as trigger statement
587/// for destructors.
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000588void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
589 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000590 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000591 return;
592
Marcin Swiderski5e415732010-09-30 23:05:00 +0000593 if (B == E)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000594 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000595
596 autoCreateBlock();
597 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000598}
599
Marcin Swiderski20b88732010-10-05 05:37:00 +0000600/// addImplicitDtorsForDestructor - Add implicit destructors generated for
601/// base and member objects in destructor.
602void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
603 assert (BuildOpts.AddImplicitDtors
604 && "Can be called only when dtors should be added");
605 const CXXRecordDecl *RD = DD->getParent();
606
607 // At the end destroy virtual base objects.
608 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
609 VE = RD->vbases_end(); VI != VE; ++VI) {
610 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
611 if (!CD->hasTrivialDestructor()) {
612 autoCreateBlock();
613 appendBaseDtor(Block, VI);
614 }
615 }
616
617 // Before virtual bases destroy direct base objects.
618 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
619 BE = RD->bases_end(); BI != BE; ++BI) {
620 if (!BI->isVirtual()) {
621 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
622 if (!CD->hasTrivialDestructor()) {
623 autoCreateBlock();
624 appendBaseDtor(Block, BI);
625 }
626 }
627 }
628
629 // First destroy member objects.
630 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
631 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski01769902010-10-25 07:05:54 +0000632 // Check for constant size array. Set type to array element type.
633 QualType QT = FI->getType();
634 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
635 if (AT->getSize() == 0)
636 continue;
637 QT = AT->getElementType();
638 }
639
640 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski20b88732010-10-05 05:37:00 +0000641 if (!CD->hasTrivialDestructor()) {
642 autoCreateBlock();
643 appendMemberDtor(Block, *FI);
644 }
645 }
646}
647
Marcin Swiderski5e415732010-09-30 23:05:00 +0000648/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
649/// way return valid LocalScope object.
650LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
651 if (!Scope) {
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000652 llvm::BumpPtrAllocator &alloc = cfg->getAllocator();
653 Scope = alloc.Allocate<LocalScope>();
654 BumpVectorContext ctx(alloc);
655 new (Scope) LocalScope(ctx, ScopePos);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000656 }
657 return Scope;
658}
659
660/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu81714f22010-10-01 03:00:16 +0000661/// that should create implicit scope (e.g. if/else substatements).
662void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000663 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu81714f22010-10-01 03:00:16 +0000664 return;
665
666 LocalScope *Scope = 0;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000667
668 // For compound statement we will be creating explicit scope.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000669 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000670 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
671 ; BI != BE; ++BI) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000672 Stmt *SI = *BI;
673 if (LabelStmt *LS = dyn_cast<LabelStmt>(SI))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000674 SI = LS->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000675 if (DeclStmt *DS = dyn_cast<DeclStmt>(SI))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000676 Scope = addLocalScopeForDeclStmt(DS, Scope);
677 }
Zhongxing Xu81714f22010-10-01 03:00:16 +0000678 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000679 }
680
681 // For any other statement scope will be implicit and as such will be
682 // interesting only for DeclStmt.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000683 if (LabelStmt *LS = dyn_cast<LabelStmt>(S))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000684 S = LS->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000685 if (DeclStmt *DS = dyn_cast<DeclStmt>(S))
Zhongxing Xu307701e2010-10-01 03:09:09 +0000686 addLocalScopeForDeclStmt(DS);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000687}
688
689/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
690/// reuse Scope if not NULL.
691LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000692 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000693 if (!BuildOpts.AddImplicitDtors)
694 return Scope;
695
696 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
697 ; DI != DE; ++DI) {
698 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
699 Scope = addLocalScopeForVarDecl(VD, Scope);
700 }
701 return Scope;
702}
703
704/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
705/// create add scope for automatic objects and temporary objects bound to
706/// const reference. Will reuse Scope if not NULL.
707LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000708 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000709 if (!BuildOpts.AddImplicitDtors)
710 return Scope;
711
712 // Check if variable is local.
713 switch (VD->getStorageClass()) {
714 case SC_None:
715 case SC_Auto:
716 case SC_Register:
717 break;
718 default: return Scope;
719 }
720
721 // Check for const references bound to temporary. Set type to pointee.
722 QualType QT = VD->getType();
723 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
724 QT = RT->getPointeeType();
725 if (!QT.isConstQualified())
726 return Scope;
727 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
728 return Scope;
729 }
730
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000731 // Check for constant size array. Set type to array element type.
732 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
733 if (AT->getSize() == 0)
734 return Scope;
735 QT = AT->getElementType();
736 }
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000737
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000738 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000739 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
740 if (!CD->hasTrivialDestructor()) {
741 // Add the variable to scope
742 Scope = createOrReuseLocalScope(Scope);
743 Scope->addVar(VD);
744 ScopePos = Scope->begin();
745 }
Marcin Swiderski5e415732010-09-30 23:05:00 +0000746 return Scope;
747}
748
749/// addLocalScopeAndDtors - For given statement add local scope for it and
750/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
751void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
752 if (!BuildOpts.AddImplicitDtors)
753 return;
754
755 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu81714f22010-10-01 03:00:16 +0000756 addLocalScopeForStmt(S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000757 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
758}
759
Marcin Swiderski321a7072010-09-30 22:54:37 +0000760/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
761/// automatic storage duration to CFGBlock's elements vector. Insertion will be
762/// performed in place specified with iterator.
763void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
764 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
765 BumpVectorContext& C = cfg->getBumpVectorContext();
766 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
767 while (B != E)
768 I = Blk->insertAutomaticObjDtor(I, *B++, S);
769}
770
771/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
772/// automatic storage duration to CFGBlock's elements vector. Elements will be
773/// appended to physical end of the vector which happens to be logical
774/// beginning.
775void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
776 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
777 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
778}
779
780/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
781/// variables with automatic storage duration to CFGBlock's elements vector.
782/// Elements will be prepended to physical beginning of the vector which
783/// happens to be logical end. Use blocks terminator as statement that specifies
784/// destructors call site.
785void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
786 LocalScope::const_iterator B, LocalScope::const_iterator E) {
787 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
788}
789
Ted Kremenek93668002009-07-17 22:18:43 +0000790/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000791/// blocks for ternary operators, &&, and ||. We also process "," and
792/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000793CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000794tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000795 if (!S) {
796 badCFG = true;
797 return 0;
798 }
Ted Kremenek93668002009-07-17 22:18:43 +0000799 switch (S->getStmtClass()) {
800 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000801 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000802
803 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000804 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000805
John McCallc07a0c72011-02-17 10:25:35 +0000806 case Stmt::BinaryConditionalOperatorClass:
807 return VisitConditionalOperator(cast<BinaryConditionalOperator>(S), asc);
808
Ted Kremenek93668002009-07-17 22:18:43 +0000809 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000810 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000811
Ted Kremenek93668002009-07-17 22:18:43 +0000812 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000813 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000814
Ted Kremenek93668002009-07-17 22:18:43 +0000815 case Stmt::BreakStmtClass:
816 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000817
Ted Kremenek93668002009-07-17 22:18:43 +0000818 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000819 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000820 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000821
Ted Kremenek93668002009-07-17 22:18:43 +0000822 case Stmt::CaseStmtClass:
823 return VisitCaseStmt(cast<CaseStmt>(S));
824
825 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000826 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000827
Ted Kremenek93668002009-07-17 22:18:43 +0000828 case Stmt::CompoundStmtClass:
829 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000830
Ted Kremenek93668002009-07-17 22:18:43 +0000831 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000832 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000833
Ted Kremenek93668002009-07-17 22:18:43 +0000834 case Stmt::ContinueStmtClass:
835 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000836
Ted Kremenekb27378c2010-01-19 20:40:33 +0000837 case Stmt::CXXCatchStmtClass:
838 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
839
John McCall5d413782010-12-06 08:20:24 +0000840 case Stmt::ExprWithCleanupsClass:
841 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000842
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000843 case Stmt::CXXBindTemporaryExprClass:
844 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
845
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000846 case Stmt::CXXConstructExprClass:
847 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
848
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000849 case Stmt::CXXFunctionalCastExprClass:
850 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
851
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000852 case Stmt::CXXTemporaryObjectExprClass:
853 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
854
Zhongxing Xu7e612172010-04-13 09:38:01 +0000855 case Stmt::CXXMemberCallExprClass:
856 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
857
Ted Kremenekb27378c2010-01-19 20:40:33 +0000858 case Stmt::CXXThrowExprClass:
859 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000860
Ted Kremenekb27378c2010-01-19 20:40:33 +0000861 case Stmt::CXXTryStmtClass:
862 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000863
Ted Kremenek93668002009-07-17 22:18:43 +0000864 case Stmt::DeclStmtClass:
865 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000866
Ted Kremenek93668002009-07-17 22:18:43 +0000867 case Stmt::DefaultStmtClass:
868 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000869
Ted Kremenek93668002009-07-17 22:18:43 +0000870 case Stmt::DoStmtClass:
871 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000872
Ted Kremenek93668002009-07-17 22:18:43 +0000873 case Stmt::ForStmtClass:
874 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000875
Ted Kremenek93668002009-07-17 22:18:43 +0000876 case Stmt::GotoStmtClass:
877 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000878
Ted Kremenek93668002009-07-17 22:18:43 +0000879 case Stmt::IfStmtClass:
880 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000881
Ted Kremenek8219b822010-12-16 07:46:53 +0000882 case Stmt::ImplicitCastExprClass:
883 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000884
Ted Kremenek93668002009-07-17 22:18:43 +0000885 case Stmt::IndirectGotoStmtClass:
886 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000887
Ted Kremenek93668002009-07-17 22:18:43 +0000888 case Stmt::LabelStmtClass:
889 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000890
Ted Kremenek5868ec62010-04-11 17:02:10 +0000891 case Stmt::MemberExprClass:
892 return VisitMemberExpr(cast<MemberExpr>(S), asc);
893
Ted Kremenek93668002009-07-17 22:18:43 +0000894 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000895 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
896
Ted Kremenek93668002009-07-17 22:18:43 +0000897 case Stmt::ObjCAtSynchronizedStmtClass:
898 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000899
Ted Kremenek93668002009-07-17 22:18:43 +0000900 case Stmt::ObjCAtThrowStmtClass:
901 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000902
Ted Kremenek93668002009-07-17 22:18:43 +0000903 case Stmt::ObjCAtTryStmtClass:
904 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000905
Ted Kremenek93668002009-07-17 22:18:43 +0000906 case Stmt::ObjCForCollectionStmtClass:
907 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000908
Ted Kremenek93668002009-07-17 22:18:43 +0000909 case Stmt::ParenExprClass:
910 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000911 goto tryAgain;
912
Ted Kremenek93668002009-07-17 22:18:43 +0000913 case Stmt::NullStmtClass:
914 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000915
Ted Kremenek93668002009-07-17 22:18:43 +0000916 case Stmt::ReturnStmtClass:
917 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000918
Ted Kremenek93668002009-07-17 22:18:43 +0000919 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000920 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000921
Ted Kremenek93668002009-07-17 22:18:43 +0000922 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000923 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000924
Ted Kremenek93668002009-07-17 22:18:43 +0000925 case Stmt::SwitchStmtClass:
926 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000927
Zhanyong Wan6dace612010-11-22 08:45:56 +0000928 case Stmt::UnaryOperatorClass:
929 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
930
Ted Kremenek93668002009-07-17 22:18:43 +0000931 case Stmt::WhileStmtClass:
932 return VisitWhileStmt(cast<WhileStmt>(S));
933 }
934}
Mike Stump11289f42009-09-09 15:08:12 +0000935
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000936CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
937 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000938 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +0000939 appendStmt(Block, S);
Mike Stump31feda52009-07-17 01:31:16 +0000940 }
Mike Stump11289f42009-09-09 15:08:12 +0000941
Ted Kremenek93668002009-07-17 22:18:43 +0000942 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000943}
Mike Stump31feda52009-07-17 01:31:16 +0000944
Ted Kremenek93668002009-07-17 22:18:43 +0000945/// VisitChildren - Visit the children of a Stmt.
946CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
Ted Kremenek828f6312011-02-21 22:11:26 +0000947 CFGBlock *lastBlock = Block;
948 for (Stmt::child_range I = Terminator->children(); I; ++I)
949 if (Stmt *child = *I)
950 if (CFGBlock *b = Visit(child))
951 lastBlock = b;
952
953 return lastBlock;
Ted Kremenek9e248872007-08-27 21:27:44 +0000954}
Mike Stump11289f42009-09-09 15:08:12 +0000955
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000956CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
957 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000958 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000959
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000960 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000961 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +0000962 appendStmt(Block, A);
Ted Kremenek93668002009-07-17 22:18:43 +0000963 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000964
Ted Kremenek9aae5132007-08-23 21:42:29 +0000965 return Block;
966}
Mike Stump11289f42009-09-09 15:08:12 +0000967
Zhanyong Wan6dace612010-11-22 08:45:56 +0000968CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek8219b822010-12-16 07:46:53 +0000969 AddStmtChoice asc) {
Zhanyong Wan6dace612010-11-22 08:45:56 +0000970 if (asc.alwaysAdd()) {
971 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +0000972 appendStmt(Block, U);
Zhanyong Wan6dace612010-11-22 08:45:56 +0000973 }
974
Ted Kremenek8219b822010-12-16 07:46:53 +0000975 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan6dace612010-11-22 08:45:56 +0000976}
977
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000978CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
979 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000980 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000981 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +0000982 appendStmt(ConfluenceBlock, B);
Mike Stump11289f42009-09-09 15:08:12 +0000983
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000984 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000985 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000986
Ted Kremenek93668002009-07-17 22:18:43 +0000987 // create the block evaluating the LHS
988 CFGBlock* LHSBlock = createBlock(false);
989 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000990
Ted Kremenek93668002009-07-17 22:18:43 +0000991 // create the block evaluating the RHS
992 Succ = ConfluenceBlock;
993 Block = NULL;
994 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000995
996 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000997 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000998 return 0;
Zhanyong Wan59f09c72010-11-22 19:32:14 +0000999 } else {
Ted Kremenek989da5e2010-04-29 01:10:26 +00001000 // Create an empty block for cases where the RHS doesn't require
1001 // any explicit statements in the CFG.
1002 RHSBlock = createBlock();
1003 }
Mike Stump11289f42009-09-09 15:08:12 +00001004
Mike Stump773582d2009-07-23 23:25:26 +00001005 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001006 TryResult KnownVal = tryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +00001007 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +00001008 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +00001009
Ted Kremenek93668002009-07-17 22:18:43 +00001010 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +00001011 if (B->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001012 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
1013 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001014 } else {
John McCalle3027922010-08-25 11:45:40 +00001015 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001016 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
1017 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001018 }
Mike Stump11289f42009-09-09 15:08:12 +00001019
Ted Kremenek93668002009-07-17 22:18:43 +00001020 // Generate the blocks for evaluating the LHS.
1021 Block = LHSBlock;
1022 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +00001023 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001024
1025 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +00001026 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001027 appendStmt(Block, B);
Ted Kremenek93668002009-07-17 22:18:43 +00001028 addStmt(B->getRHS());
1029 return addStmt(B->getLHS());
1030 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001031
1032 if (B->isAssignmentOp()) {
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001033 if (asc.alwaysAdd()) {
1034 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001035 appendStmt(Block, B);
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001036 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001037 Visit(B->getLHS());
Marcin Swiderski77232492010-10-24 08:21:40 +00001038 return Visit(B->getRHS());
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001039 }
Mike Stump11289f42009-09-09 15:08:12 +00001040
Marcin Swiderski77232492010-10-24 08:21:40 +00001041 if (asc.alwaysAdd()) {
1042 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001043 appendStmt(Block, B);
Marcin Swiderski77232492010-10-24 08:21:40 +00001044 }
1045
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001046 CFGBlock *RBlock = Visit(B->getRHS());
1047 CFGBlock *LBlock = Visit(B->getLHS());
1048 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1049 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1050 // return RBlock. Otherwise we'll incorrectly return NULL.
1051 return (LBlock ? LBlock : RBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001052}
1053
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001054CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1055 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +00001056 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001057 appendStmt(Block, E);
Ted Kremenek470bfa42009-11-25 01:34:30 +00001058 }
1059 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001060}
1061
Ted Kremenek93668002009-07-17 22:18:43 +00001062CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1063 // "break" is a control-flow statement. Thus we stop processing the current
1064 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001065 if (badCFG)
1066 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001067
Ted Kremenek93668002009-07-17 22:18:43 +00001068 // Now create a new block that ends with the break statement.
1069 Block = createBlock(false);
1070 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +00001071
Ted Kremenek93668002009-07-17 22:18:43 +00001072 // If there is no target for the break, then we are looking at an incomplete
1073 // AST. This means that the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001074 if (BreakJumpTarget.block) {
1075 addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B);
1076 addSuccessor(Block, BreakJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001077 } else
Ted Kremenek93668002009-07-17 22:18:43 +00001078 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +00001079
1080
Ted Kremenek9aae5132007-08-23 21:42:29 +00001081 return Block;
1082}
Mike Stump11289f42009-09-09 15:08:12 +00001083
Mike Stump04c68512010-01-21 15:20:48 +00001084static bool CanThrow(Expr *E) {
1085 QualType Ty = E->getType();
1086 if (Ty->isFunctionPointerType())
1087 Ty = Ty->getAs<PointerType>()->getPointeeType();
1088 else if (Ty->isBlockPointerType())
1089 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001090
Mike Stump04c68512010-01-21 15:20:48 +00001091 const FunctionType *FT = Ty->getAs<FunctionType>();
1092 if (FT) {
1093 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1094 if (Proto->hasEmptyExceptionSpec())
1095 return false;
1096 }
1097 return true;
1098}
1099
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001100CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +00001101 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +00001102 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001103 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001104 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +00001105 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001106
Mike Stump04c68512010-01-21 15:20:48 +00001107 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001108
1109 // Languages without exceptions are assumed to not throw.
Anders Carlsson6dc07d42011-02-28 00:33:03 +00001110 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001111 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +00001112 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +00001113 }
1114
1115 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001116 if (FD->hasAttr<NoReturnAttr>())
1117 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +00001118 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +00001119 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001120 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001121
Mike Stump04c68512010-01-21 15:20:48 +00001122 if (!CanThrow(C->getCallee()))
1123 AddEHEdge = false;
1124
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001125 if (!NoReturn && !AddEHEdge)
1126 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump11289f42009-09-09 15:08:12 +00001127
Mike Stump92244b02010-01-19 22:00:14 +00001128 if (Block) {
1129 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001130 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +00001131 return 0;
1132 }
Mike Stump11289f42009-09-09 15:08:12 +00001133
Mike Stump92244b02010-01-19 22:00:14 +00001134 Block = createBlock(!NoReturn);
Ted Kremenek2866bab2011-03-10 01:14:08 +00001135 appendStmt(Block, C);
Mike Stump8c5d7992009-07-25 21:26:53 +00001136
Mike Stump92244b02010-01-19 22:00:14 +00001137 if (NoReturn) {
1138 // Wire this to the exit block directly.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001139 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001140 }
Mike Stump04c68512010-01-21 15:20:48 +00001141 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +00001142 // Add exceptional edges.
1143 if (TryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001144 addSuccessor(Block, TryTerminatedBlock);
Mike Stump92244b02010-01-19 22:00:14 +00001145 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001146 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001147 }
Mike Stump11289f42009-09-09 15:08:12 +00001148
Mike Stump8c5d7992009-07-25 21:26:53 +00001149 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +00001150}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001151
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001152CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1153 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +00001154 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001155 appendStmt(ConfluenceBlock, C);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001156 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001157 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001158
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001159 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek21822592009-07-17 18:20:32 +00001160 Succ = ConfluenceBlock;
1161 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001162 CFGBlock* LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001163 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001164 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001165
Ted Kremenek21822592009-07-17 18:20:32 +00001166 Succ = ConfluenceBlock;
1167 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001168 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001169 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001170 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001171
Ted Kremenek21822592009-07-17 18:20:32 +00001172 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +00001173 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001174 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
1175 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1176 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +00001177 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +00001178 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +00001179}
Mike Stump11289f42009-09-09 15:08:12 +00001180
1181
1182CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001183 addLocalScopeAndDtors(C);
Mike Stump11289f42009-09-09 15:08:12 +00001184 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001185
1186 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1187 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +00001188 // If we hit a segment of code just containing ';' (NullStmts), we can
1189 // get a null block back. In such cases, just use the LastBlock
1190 if (CFGBlock *newBlock = addStmt(*I))
1191 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001192
Ted Kremenekce499c22009-08-27 23:16:26 +00001193 if (badCFG)
1194 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001195 }
Mike Stump92244b02010-01-19 22:00:14 +00001196
Ted Kremenek93668002009-07-17 22:18:43 +00001197 return LastBlock;
1198}
Mike Stump11289f42009-09-09 15:08:12 +00001199
John McCallc07a0c72011-02-17 10:25:35 +00001200CFGBlock *CFGBuilder::VisitConditionalOperator(AbstractConditionalOperator *C,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001201 AddStmtChoice asc) {
John McCallc07a0c72011-02-17 10:25:35 +00001202 const BinaryConditionalOperator *BCO = dyn_cast<BinaryConditionalOperator>(C);
1203 const OpaqueValueExpr *opaqueValue = (BCO ? BCO->getOpaqueValue() : NULL);
1204
Ted Kremenek51d40b02009-07-17 18:15:54 +00001205 // Create the confluence block that will "merge" the results of the ternary
1206 // expression.
1207 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001208 appendStmt(ConfluenceBlock, C);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001209 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001210 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001211
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001212 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001213
Ted Kremenek51d40b02009-07-17 18:15:54 +00001214 // Create a block for the LHS expression if there is an LHS expression. A
1215 // GCC extension allows LHS to be NULL, causing the condition to be the
1216 // value that is returned instead.
1217 // e.g: x ?: y is shorthand for: x ? x : y;
1218 Succ = ConfluenceBlock;
1219 Block = NULL;
John McCallc07a0c72011-02-17 10:25:35 +00001220 CFGBlock* LHSBlock = 0;
1221 const Expr *trueExpr = C->getTrueExpr();
1222 if (trueExpr != opaqueValue) {
1223 LHSBlock = Visit(C->getTrueExpr(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001224 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001225 return 0;
1226 Block = NULL;
1227 }
Ted Kremenekd8138012011-02-24 03:09:15 +00001228 else
1229 LHSBlock = ConfluenceBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001230
Ted Kremenek51d40b02009-07-17 18:15:54 +00001231 // Create the block for the RHS expression.
1232 Succ = ConfluenceBlock;
John McCallc07a0c72011-02-17 10:25:35 +00001233 CFGBlock* RHSBlock = Visit(C->getFalseExpr(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001234 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001235 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001236
Ted Kremenek51d40b02009-07-17 18:15:54 +00001237 // Create the block that will contain the condition.
1238 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +00001239
Mike Stump773582d2009-07-23 23:25:26 +00001240 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001241 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
Ted Kremenekd8138012011-02-24 03:09:15 +00001242 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001243 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001244 Block->setTerminator(C);
John McCallc07a0c72011-02-17 10:25:35 +00001245 Expr *condExpr = C->getCond();
John McCall68cc3352011-02-19 03:13:26 +00001246
Ted Kremenekd8138012011-02-24 03:09:15 +00001247 if (opaqueValue) {
1248 // Run the condition expression if it's not trivially expressed in
1249 // terms of the opaque value (or if there is no opaque value).
1250 if (condExpr != opaqueValue)
1251 addStmt(condExpr);
John McCall68cc3352011-02-19 03:13:26 +00001252
Ted Kremenekd8138012011-02-24 03:09:15 +00001253 // Before that, run the common subexpression if there was one.
1254 // At least one of this or the above will be run.
1255 return addStmt(BCO->getCommon());
1256 }
1257
1258 return addStmt(condExpr);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001259}
1260
Ted Kremenek93668002009-07-17 22:18:43 +00001261CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001262 if (DS->isSingleDecl())
1263 return VisitDeclSubExpr(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001264
Ted Kremenek93668002009-07-17 22:18:43 +00001265 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001266
Ted Kremenek93668002009-07-17 22:18:43 +00001267 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1268 typedef llvm::SmallVector<Decl*,10> BufTy;
1269 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +00001270
Ted Kremenek93668002009-07-17 22:18:43 +00001271 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1272 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1273 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1274 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +00001275
Ted Kremenek93668002009-07-17 22:18:43 +00001276 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1277 // automatically freed with the CFG.
1278 DeclGroupRef DG(*I);
1279 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001280 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +00001281 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +00001282
Ted Kremenek93668002009-07-17 22:18:43 +00001283 // Append the fake DeclStmt to block.
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001284 B = VisitDeclSubExpr(DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +00001285 }
Mike Stump11289f42009-09-09 15:08:12 +00001286
1287 return B;
Ted Kremenek93668002009-07-17 22:18:43 +00001288}
Mike Stump11289f42009-09-09 15:08:12 +00001289
Ted Kremenek93668002009-07-17 22:18:43 +00001290/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001291/// DeclStmts and initializers in them.
1292CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1293 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekf6998822008-02-26 00:22:58 +00001294
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001295 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001296
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001297 if (!VD) {
1298 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001299 appendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +00001300 return Block;
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001301 }
Mike Stump11289f42009-09-09 15:08:12 +00001302
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001303 bool IsReference = false;
1304 bool HasTemporaries = false;
1305
1306 // Destructors of temporaries in initialization expression should be called
1307 // after initialization finishes.
Ted Kremenek93668002009-07-17 22:18:43 +00001308 Expr *Init = VD->getInit();
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001309 if (Init) {
1310 IsReference = VD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +00001311 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001312
1313 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1314 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +00001315 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001316 IsReference);
1317 }
1318 }
1319
1320 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001321 appendStmt(Block, DS);
Mike Stump11289f42009-09-09 15:08:12 +00001322
Ted Kremenek93668002009-07-17 22:18:43 +00001323 if (Init) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001324 if (HasTemporaries)
1325 // For expression with temporaries go directly to subexpression to omit
1326 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +00001327 Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001328 else
Ted Kremenek8219b822010-12-16 07:46:53 +00001329 Visit(Init);
Ted Kremenek93668002009-07-17 22:18:43 +00001330 }
Mike Stump11289f42009-09-09 15:08:12 +00001331
Ted Kremenek93668002009-07-17 22:18:43 +00001332 // If the type of VD is a VLA, then we must process its size expressions.
John McCall424cec92011-01-19 06:33:43 +00001333 for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr());
1334 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek93668002009-07-17 22:18:43 +00001335 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001336
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001337 // Remove variable from local scope.
1338 if (ScopePos && VD == *ScopePos)
1339 ++ScopePos;
1340
Ted Kremenek93668002009-07-17 22:18:43 +00001341 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001342}
1343
1344CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001345 // We may see an if statement in the middle of a basic block, or it may be the
1346 // first statement we are processing. In either case, we create a new basic
1347 // block. First, we create the blocks for the then...else statements, and
1348 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +00001349 // middle of a block, we stop processing that block. That block is then the
1350 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +00001351
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001352 // Save local scope position because in case of condition variable ScopePos
1353 // won't be restored when traversing AST.
1354 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1355
1356 // Create local scope for possible condition variable.
1357 // Store scope position. Add implicit destructor.
1358 if (VarDecl* VD = I->getConditionVariable()) {
1359 LocalScope::const_iterator BeginScopePos = ScopePos;
1360 addLocalScopeForVarDecl(VD);
1361 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1362 }
1363
Mike Stump31feda52009-07-17 01:31:16 +00001364 // The block we were proccessing is now finished. Make it the successor
1365 // block.
1366 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001367 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001368 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001369 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001370 }
Mike Stump31feda52009-07-17 01:31:16 +00001371
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001372 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001373 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001374
Ted Kremenek9aae5132007-08-23 21:42:29 +00001375 if (Stmt* Else = I->getElse()) {
1376 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001377
Ted Kremenek9aae5132007-08-23 21:42:29 +00001378 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +00001379 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001380 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001381
1382 // If branch is not a compound statement create implicit scope
1383 // and add destructors.
1384 if (!isa<CompoundStmt>(Else))
1385 addLocalScopeAndDtors(Else);
1386
Ted Kremenek93668002009-07-17 22:18:43 +00001387 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +00001388
Ted Kremenekbbad8ce2007-08-30 18:13:31 +00001389 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1390 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +00001391 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001392 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001393 return 0;
1394 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001395 }
Mike Stump31feda52009-07-17 01:31:16 +00001396
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001397 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001398 CFGBlock* ThenBlock;
1399 {
1400 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001401 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001402 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001403 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001404
1405 // If branch is not a compound statement create implicit scope
1406 // and add destructors.
1407 if (!isa<CompoundStmt>(Then))
1408 addLocalScopeAndDtors(Then);
1409
Ted Kremenek93668002009-07-17 22:18:43 +00001410 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +00001411
Ted Kremenek1b379512009-04-01 03:52:47 +00001412 if (!ThenBlock) {
1413 // We can reach here if the "then" body has all NullStmts.
1414 // Create an empty block so we can distinguish between true and false
1415 // branches in path-sensitive analyses.
1416 ThenBlock = createBlock(false);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001417 addSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001418 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001419 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001420 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001421 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001422 }
1423
Mike Stump31feda52009-07-17 01:31:16 +00001424 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001425 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001426
Ted Kremenek9aae5132007-08-23 21:42:29 +00001427 // Set the terminator of the new block to the If statement.
1428 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001429
Mike Stump773582d2009-07-23 23:25:26 +00001430 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001431 const TryResult &KnownVal = tryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001432
Ted Kremenek9aae5132007-08-23 21:42:29 +00001433 // Now add the successors.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001434 addSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1435 addSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001436
1437 // Add the condition as the last statement in the new block. This may create
1438 // new blocks as the condition may contain control-flow. Any newly created
1439 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001440 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001441
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001442 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1443 // and the condition variable initialization to the CFG.
1444 if (VarDecl *VD = I->getConditionVariable()) {
1445 if (Expr *Init = VD->getInit()) {
1446 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001447 appendStmt(Block, I);
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001448 addStmt(Init);
1449 }
1450 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001451
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001452 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001453}
Mike Stump31feda52009-07-17 01:31:16 +00001454
1455
Ted Kremenek9aae5132007-08-23 21:42:29 +00001456CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001457 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001458 //
Mike Stump31feda52009-07-17 01:31:16 +00001459 // NOTE: If a "return" appears in the middle of a block, this means that the
1460 // code afterwards is DEAD (unreachable). We still keep a basic block
1461 // for that code; a simple "mark-and-sweep" from the entry block will be
1462 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001463
1464 // Create the new block.
1465 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001466
Ted Kremenek9aae5132007-08-23 21:42:29 +00001467 // The Exit block is the only successor.
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001468 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001469 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001470
1471 // Add the return statement to the block. This may create new blocks if R
1472 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001473 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001474}
1475
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001476CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt *L) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001477 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001478 addStmt(L->getSubStmt());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001479 CFGBlock *LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001480
Ted Kremenek93668002009-07-17 22:18:43 +00001481 if (!LabelBlock) // This can happen when the body is empty, i.e.
1482 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001483
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001484 assert(LabelMap.find(L->getDecl()) == LabelMap.end() &&
1485 "label already in map");
1486 LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001487
1488 // Labels partition blocks, so this is the end of the basic block we were
1489 // processing (L is the block's label). Because this is label (and we have
1490 // already processed the substatement) there is no extra control-flow to worry
1491 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001492 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001493 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001494 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001495
1496 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001497 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001498
Ted Kremenek9aae5132007-08-23 21:42:29 +00001499 // This block is now the implicit successor of other blocks.
1500 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001501
Ted Kremenek9aae5132007-08-23 21:42:29 +00001502 return LabelBlock;
1503}
1504
1505CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001506 // Goto is a control-flow statement. Thus we stop processing the current
1507 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001508
Ted Kremenek9aae5132007-08-23 21:42:29 +00001509 Block = createBlock(false);
1510 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001511
1512 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001513 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001514
Ted Kremenek9aae5132007-08-23 21:42:29 +00001515 if (I == LabelMap.end())
1516 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001517 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1518 else {
1519 JumpTarget JT = I->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001520 addAutomaticObjDtors(ScopePos, JT.scopePosition, G);
1521 addSuccessor(Block, JT.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001522 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001523
Mike Stump31feda52009-07-17 01:31:16 +00001524 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001525}
1526
1527CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001528 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001529
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001530 // Save local scope position because in case of condition variable ScopePos
1531 // won't be restored when traversing AST.
1532 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1533
1534 // Create local scope for init statement and possible condition variable.
1535 // Add destructor for init statement and condition variable.
1536 // Store scope position for continue statement.
1537 if (Stmt* Init = F->getInit())
1538 addLocalScopeForStmt(Init);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001539 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1540
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001541 if (VarDecl* VD = F->getConditionVariable())
1542 addLocalScopeForVarDecl(VD);
1543 LocalScope::const_iterator ContinueScopePos = ScopePos;
1544
1545 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1546
Mike Stump014b3ea2009-07-21 01:12:51 +00001547 // "for" is a control-flow statement. Thus we stop processing the current
1548 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001549 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001550 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001551 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001552 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001553 } else
1554 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001555
Ted Kremenek304a9532010-05-21 20:30:15 +00001556 // Save the current value for the break targets.
1557 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001558 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001559 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001560
Mike Stump31feda52009-07-17 01:31:16 +00001561 // Because of short-circuit evaluation, the condition of the loop can span
1562 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1563 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001564 CFGBlock* ExitConditionBlock = createBlock(false);
1565 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001566
Ted Kremenek81e14852007-08-27 19:46:09 +00001567 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001568 ExitConditionBlock->setTerminator(F);
1569
1570 // Now add the actual condition to the condition block. Because the condition
1571 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001572 if (Stmt* C = F->getCond()) {
1573 Block = ExitConditionBlock;
1574 EntryConditionBlock = addStmt(C);
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001575 if (badCFG)
1576 return 0;
Ted Kremenek7b31a612010-09-15 07:01:20 +00001577 assert(Block == EntryConditionBlock ||
1578 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001579
1580 // If this block contains a condition variable, add both the condition
1581 // variable and initializer to the CFG.
1582 if (VarDecl *VD = F->getConditionVariable()) {
1583 if (Expr *Init = VD->getInit()) {
1584 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001585 appendStmt(Block, F);
Ted Kremenekec92f942009-12-24 01:49:06 +00001586 EntryConditionBlock = addStmt(Init);
1587 assert(Block == EntryConditionBlock);
1588 }
1589 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001590
Ted Kremenek55957a82009-05-02 00:13:27 +00001591 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001592 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001593 return 0;
1594 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001595 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001596
Mike Stump31feda52009-07-17 01:31:16 +00001597 // The condition block is the implicit successor for the loop body as well as
1598 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001599 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001600
Mike Stump773582d2009-07-23 23:25:26 +00001601 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001602 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001603
Mike Stump773582d2009-07-23 23:25:26 +00001604 if (F->getCond())
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001605 KnownVal = tryEvaluateBool(F->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001606
Ted Kremenek9aae5132007-08-23 21:42:29 +00001607 // Now create the loop body.
1608 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001609 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001610
Ted Kremenek304a9532010-05-21 20:30:15 +00001611 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001612 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1613 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001614
Ted Kremeneke9610502007-08-30 18:39:40 +00001615 // Create a new block to contain the (bottom) of the loop body.
1616 Block = NULL;
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001617
1618 // Loop body should end with destructor of Condition variable (if any).
1619 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump31feda52009-07-17 01:31:16 +00001620
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001621 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001622 // Generate increment code in its own basic block. This is the target of
1623 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001624 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001625 } else {
1626 // No increment code. Create a special, empty, block that is used as the
1627 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001628 assert(Succ == EntryConditionBlock);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001629 Succ = Block ? Block : createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001630 }
Mike Stump31feda52009-07-17 01:31:16 +00001631
Ted Kremenek902393b2009-04-28 00:51:56 +00001632 // Finish up the increment (or empty) block if it hasn't been already.
1633 if (Block) {
1634 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001635 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001636 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001637 Block = 0;
1638 }
Mike Stump31feda52009-07-17 01:31:16 +00001639
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001640 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001641
Ted Kremenek902393b2009-04-28 00:51:56 +00001642 // The starting block for the loop increment is the block that should
1643 // represent the 'loop target' for looping back to the start of the loop.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001644 ContinueJumpTarget.block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001645
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001646 // If body is not a compound statement create implicit scope
1647 // and add destructors.
1648 if (!isa<CompoundStmt>(F->getBody()))
1649 addLocalScopeAndDtors(F->getBody());
1650
Mike Stump31feda52009-07-17 01:31:16 +00001651 // Now populate the body block, and in the process create new blocks as we
1652 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001653 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001654
1655 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001656 BodyBlock = ContinueJumpTarget.block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001657 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001658 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001659
Ted Kremenek30754282009-07-24 04:47:11 +00001660 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001661 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001662 }
Mike Stump31feda52009-07-17 01:31:16 +00001663
Ted Kremenek30754282009-07-24 04:47:11 +00001664 // Link up the condition block with the code that follows the loop. (the
1665 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001666 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001667
Ted Kremenek9aae5132007-08-23 21:42:29 +00001668 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001669 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001670 if (Stmt* I = F->getInit()) {
1671 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001672 return addStmt(I);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001673 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001674
1675 // There is no loop initialization. We are thus basically a while loop.
1676 // NULL out Block to force lazy block construction.
1677 Block = NULL;
1678 Succ = EntryConditionBlock;
1679 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001680}
1681
Ted Kremenek5868ec62010-04-11 17:02:10 +00001682CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1683 if (asc.alwaysAdd()) {
1684 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001685 appendStmt(Block, M);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001686 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001687 return Visit(M->getBase());
Ted Kremenek5868ec62010-04-11 17:02:10 +00001688}
1689
Ted Kremenek9d56e642008-11-11 17:10:00 +00001690CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1691 // Objective-C fast enumeration 'for' statements:
1692 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1693 //
1694 // for ( Type newVariable in collection_expression ) { statements }
1695 //
1696 // becomes:
1697 //
1698 // prologue:
1699 // 1. collection_expression
1700 // T. jump to loop_entry
1701 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001702 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001703 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1704 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1705 // TB:
1706 // statements
1707 // T. jump to loop_entry
1708 // FB:
1709 // what comes after
1710 //
1711 // and
1712 //
1713 // Type existingItem;
1714 // for ( existingItem in expression ) { statements }
1715 //
1716 // becomes:
1717 //
Mike Stump31feda52009-07-17 01:31:16 +00001718 // the same with newVariable replaced with existingItem; the binding works
1719 // the same except that for one ObjCForCollectionStmt::getElement() returns
1720 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001721 //
Mike Stump31feda52009-07-17 01:31:16 +00001722
Ted Kremenek9d56e642008-11-11 17:10:00 +00001723 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001724
Ted Kremenek9d56e642008-11-11 17:10:00 +00001725 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001726 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001727 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001728 LoopSuccessor = Block;
1729 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001730 } else
1731 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001732
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001733 // Build the condition blocks.
1734 CFGBlock* ExitConditionBlock = createBlock(false);
1735 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001736
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001737 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001738 ExitConditionBlock->setTerminator(S);
1739
1740 // The last statement in the block should be the ObjCForCollectionStmt, which
1741 // performs the actual binding to 'element' and determines if there are any
1742 // more items in the collection.
Ted Kremenek8219b822010-12-16 07:46:53 +00001743 appendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001744 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001745
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001746 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001747 // generate new blocks as necesary. We DON'T add the statement by default to
1748 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001749 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001750 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001751 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001752 return 0;
1753 Block = 0;
1754 }
Mike Stump31feda52009-07-17 01:31:16 +00001755
1756 // The condition block is the implicit successor for the loop body as well as
1757 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001758 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001759
Ted Kremenek9d56e642008-11-11 17:10:00 +00001760 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001761 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001762 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001763 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1764 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1765 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001766
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001767 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1768 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001769
Ted Kremenek93668002009-07-17 22:18:43 +00001770 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001771
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001772 if (!BodyBlock)
1773 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001774 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001775 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001776 return 0;
1777 }
Mike Stump31feda52009-07-17 01:31:16 +00001778
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001779 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001780 addSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001781 }
Mike Stump31feda52009-07-17 01:31:16 +00001782
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001783 // Link up the condition block with the code that follows the loop.
1784 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001785 addSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001786
Ted Kremenek9d56e642008-11-11 17:10:00 +00001787 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001788 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001789 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001790}
1791
Ted Kremenek49805452009-05-02 01:49:13 +00001792CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1793 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001794
Ted Kremenek49805452009-05-02 01:49:13 +00001795 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001796 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001797
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001798 // The sync body starts its own basic block. This makes it a little easier
1799 // for diagnostic clients.
1800 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001801 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001802 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001803
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001804 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001805 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001806 }
Mike Stump31feda52009-07-17 01:31:16 +00001807
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001808 // Add the @synchronized to the CFG.
1809 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001810 appendStmt(Block, S);
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001811
Ted Kremenek49805452009-05-02 01:49:13 +00001812 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001813 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001814}
Mike Stump31feda52009-07-17 01:31:16 +00001815
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001816CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001817 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001818 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001819}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001820
Ted Kremenek9aae5132007-08-23 21:42:29 +00001821CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001822 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001823
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001824 // Save local scope position because in case of condition variable ScopePos
1825 // won't be restored when traversing AST.
1826 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1827
1828 // Create local scope for possible condition variable.
1829 // Store scope position for continue statement.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001830 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001831 if (VarDecl* VD = W->getConditionVariable()) {
1832 addLocalScopeForVarDecl(VD);
1833 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1834 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001835
Mike Stump014b3ea2009-07-21 01:12:51 +00001836 // "while" is a control-flow statement. Thus we stop processing the current
1837 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001838 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001839 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001840 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001841 LoopSuccessor = Block;
Ted Kremenek828f6312011-02-21 22:11:26 +00001842 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001843 } else
1844 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001845
1846 // Because of short-circuit evaluation, the condition of the loop can span
1847 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1848 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001849 CFGBlock* ExitConditionBlock = createBlock(false);
1850 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001851
Ted Kremenek81e14852007-08-27 19:46:09 +00001852 // Set the terminator for the "exit" condition block.
1853 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001854
1855 // Now add the actual condition to the condition block. Because the condition
1856 // itself may contain control-flow, new blocks may be created. Thus we update
1857 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001858 if (Stmt* C = W->getCond()) {
1859 Block = ExitConditionBlock;
1860 EntryConditionBlock = addStmt(C);
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001861 // The condition might finish the current 'Block'.
1862 Block = EntryConditionBlock;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001863
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001864 // If this block contains a condition variable, add both the condition
1865 // variable and initializer to the CFG.
1866 if (VarDecl *VD = W->getConditionVariable()) {
1867 if (Expr *Init = VD->getInit()) {
1868 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00001869 appendStmt(Block, W);
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001870 EntryConditionBlock = addStmt(Init);
1871 assert(Block == EntryConditionBlock);
1872 }
1873 }
1874
Ted Kremenek55957a82009-05-02 00:13:27 +00001875 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001876 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001877 return 0;
1878 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001879 }
Mike Stump31feda52009-07-17 01:31:16 +00001880
1881 // The condition block is the implicit successor for the loop body as well as
1882 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001883 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001884
Mike Stump773582d2009-07-23 23:25:26 +00001885 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001886 const TryResult& KnownVal = tryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001887
Ted Kremenek9aae5132007-08-23 21:42:29 +00001888 // Process the loop body.
1889 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001890 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001891
1892 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001893 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1894 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1895 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001896
Mike Stump31feda52009-07-17 01:31:16 +00001897 // Create an empty block to represent the transition block for looping back
1898 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001899 Block = 0;
1900 assert(Succ == EntryConditionBlock);
1901 Succ = createBlock();
1902 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001903 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001904
Ted Kremenek9aae5132007-08-23 21:42:29 +00001905 // All breaks should go to the code following the loop.
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001906 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001907
Ted Kremenek9aae5132007-08-23 21:42:29 +00001908 // NULL out Block to force lazy instantiation of blocks for the body.
1909 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001910
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001911 // Loop body should end with destructor of Condition variable (if any).
1912 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1913
1914 // If body is not a compound statement create implicit scope
1915 // and add destructors.
1916 if (!isa<CompoundStmt>(W->getBody()))
1917 addLocalScopeAndDtors(W->getBody());
1918
Ted Kremenek9aae5132007-08-23 21:42:29 +00001919 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001920 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001921
Ted Kremeneke9610502007-08-30 18:39:40 +00001922 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001923 BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001924 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001925 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001926 return 0;
1927 }
Mike Stump31feda52009-07-17 01:31:16 +00001928
Ted Kremenek30754282009-07-24 04:47:11 +00001929 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001930 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001931 }
Mike Stump31feda52009-07-17 01:31:16 +00001932
Ted Kremenek30754282009-07-24 04:47:11 +00001933 // Link up the condition block with the code that follows the loop. (the
1934 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001935 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001936
1937 // There can be no more statements in the condition block since we loop back
1938 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001939 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001940
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001941 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001942 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001943 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001944}
Mike Stump11289f42009-09-09 15:08:12 +00001945
1946
Ted Kremenek93668002009-07-17 22:18:43 +00001947CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1948 // FIXME: For now we pretend that @catch and the code it contains does not
1949 // exit.
1950 return Block;
1951}
Mike Stump31feda52009-07-17 01:31:16 +00001952
Ted Kremenek93041ba2008-12-09 20:20:09 +00001953CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1954 // FIXME: This isn't complete. We basically treat @throw like a return
1955 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001956
Ted Kremenek0868eea2009-09-24 18:45:41 +00001957 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001958 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001959 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001960
Ted Kremenek93041ba2008-12-09 20:20:09 +00001961 // Create the new block.
1962 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001963
Ted Kremenek93041ba2008-12-09 20:20:09 +00001964 // The Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001965 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001966
1967 // Add the statement to the block. This may create new blocks if S contains
1968 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001969 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001970}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001971
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001972CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001973 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001974 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001975 return 0;
1976
1977 // Create the new block.
1978 Block = createBlock(false);
1979
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001980 if (TryTerminatedBlock)
1981 // The current try statement is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001982 addSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001983 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001984 // otherwise the Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001985 addSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001986
1987 // Add the statement to the block. This may create new blocks if S contains
1988 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001989 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001990}
1991
Ted Kremenek93668002009-07-17 22:18:43 +00001992CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001993 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001994
Mike Stump8d50b6a2009-07-21 01:27:50 +00001995 // "do...while" is a control-flow statement. Thus we stop processing the
1996 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001997 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001998 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001999 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002000 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002001 } else
2002 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00002003
2004 // Because of short-circuit evaluation, the condition of the loop can span
2005 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
2006 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00002007 CFGBlock* ExitConditionBlock = createBlock(false);
2008 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002009
Ted Kremenek81e14852007-08-27 19:46:09 +00002010 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00002011 ExitConditionBlock->setTerminator(D);
2012
2013 // Now add the actual condition to the condition block. Because the condition
2014 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00002015 if (Stmt* C = D->getCond()) {
2016 Block = ExitConditionBlock;
2017 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00002018 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002019 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002020 return 0;
2021 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002022 }
Mike Stump31feda52009-07-17 01:31:16 +00002023
Ted Kremeneka1523a32008-02-27 07:20:00 +00002024 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002025 Succ = EntryConditionBlock;
2026
Mike Stump773582d2009-07-23 23:25:26 +00002027 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002028 const TryResult &KnownVal = tryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00002029
Ted Kremenek9aae5132007-08-23 21:42:29 +00002030 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002031 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002032 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002033 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002034
Ted Kremenek9aae5132007-08-23 21:42:29 +00002035 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002036 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2037 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2038 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00002039
Ted Kremenek9aae5132007-08-23 21:42:29 +00002040 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002041 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002042
Ted Kremenek9aae5132007-08-23 21:42:29 +00002043 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002044 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002045
Ted Kremenek9aae5132007-08-23 21:42:29 +00002046 // NULL out Block to force lazy instantiation of blocks for the body.
2047 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002048
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00002049 // If body is not a compound statement create implicit scope
2050 // and add destructors.
2051 if (!isa<CompoundStmt>(D->getBody()))
2052 addLocalScopeAndDtors(D->getBody());
2053
Ted Kremenek9aae5132007-08-23 21:42:29 +00002054 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00002055 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002056
Ted Kremeneke9610502007-08-30 18:39:40 +00002057 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00002058 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00002059 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002060 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002061 return 0;
2062 }
Mike Stump31feda52009-07-17 01:31:16 +00002063
Ted Kremenek110974d2010-08-17 20:59:56 +00002064 if (!KnownVal.isFalse()) {
2065 // Add an intermediate block between the BodyBlock and the
2066 // ExitConditionBlock to represent the "loop back" transition. Create an
2067 // empty block to represent the transition block for looping back to the
2068 // head of the loop.
2069 // FIXME: Can we do this more efficiently without adding another block?
2070 Block = NULL;
2071 Succ = BodyBlock;
2072 CFGBlock *LoopBackBlock = createBlock();
2073 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00002074
Ted Kremenek110974d2010-08-17 20:59:56 +00002075 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002076 addSuccessor(ExitConditionBlock, LoopBackBlock);
Ted Kremenek110974d2010-08-17 20:59:56 +00002077 }
2078 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002079 addSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002080 }
Mike Stump31feda52009-07-17 01:31:16 +00002081
Ted Kremenek30754282009-07-24 04:47:11 +00002082 // Link up the condition block with the code that follows the loop.
2083 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002084 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00002085
2086 // There can be no more statements in the body block(s) since we loop back to
2087 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002088 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002089
Ted Kremenek9aae5132007-08-23 21:42:29 +00002090 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00002091 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002092 return BodyBlock;
2093}
2094
2095CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2096 // "continue" is a control-flow statement. Thus we stop processing the
2097 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002098 if (badCFG)
2099 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002100
Ted Kremenek9aae5132007-08-23 21:42:29 +00002101 // Now create a new block that ends with the continue statement.
2102 Block = createBlock(false);
2103 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00002104
Ted Kremenek9aae5132007-08-23 21:42:29 +00002105 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00002106 // incomplete AST. This means the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00002107 if (ContinueJumpTarget.block) {
2108 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C);
2109 addSuccessor(Block, ContinueJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002110 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00002111 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00002112
Ted Kremenek9aae5132007-08-23 21:42:29 +00002113 return Block;
2114}
Mike Stump11289f42009-09-09 15:08:12 +00002115
Ted Kremenek0747de62009-07-18 00:47:21 +00002116CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002117 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002118
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002119 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002120 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002121 appendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00002122 }
Mike Stump11289f42009-09-09 15:08:12 +00002123
Ted Kremenek93668002009-07-17 22:18:43 +00002124 // VLA types have expressions that must be evaluated.
2125 if (E->isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00002126 for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr());
Ted Kremenek93668002009-07-17 22:18:43 +00002127 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2128 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00002129 }
Mike Stump11289f42009-09-09 15:08:12 +00002130
Mike Stump31feda52009-07-17 01:31:16 +00002131 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002132}
Mike Stump11289f42009-09-09 15:08:12 +00002133
Ted Kremenek93668002009-07-17 22:18:43 +00002134/// VisitStmtExpr - Utility method to handle (nested) statement
2135/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002136CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2137 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002138 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002139 appendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00002140 }
Ted Kremenek93668002009-07-17 22:18:43 +00002141 return VisitCompoundStmt(SE->getSubStmt());
2142}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002143
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002144CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00002145 // "switch" is a control-flow statement. Thus we stop processing the current
2146 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002147 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002148
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002149 // Save local scope position because in case of condition variable ScopePos
2150 // won't be restored when traversing AST.
2151 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2152
2153 // Create local scope for possible condition variable.
2154 // Store scope position. Add implicit destructor.
2155 if (VarDecl* VD = Terminator->getConditionVariable()) {
2156 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2157 addLocalScopeForVarDecl(VD);
2158 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2159 }
2160
Ted Kremenek9aae5132007-08-23 21:42:29 +00002161 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002162 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002163 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002164 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00002165 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002166
2167 // Save the current "switch" context.
2168 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00002169 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002170 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00002171
Mike Stump31feda52009-07-17 01:31:16 +00002172 // Set the "default" case to be the block after the switch statement. If the
2173 // switch statement contains a "default:", this value will be overwritten with
2174 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00002175 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00002176
Ted Kremenek9aae5132007-08-23 21:42:29 +00002177 // Create a new block that will contain the switch statement.
2178 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00002179
Ted Kremenek9aae5132007-08-23 21:42:29 +00002180 // Now process the switch body. The code after the switch is the implicit
2181 // successor.
2182 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002183 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002184
2185 // When visiting the body, the case statements should automatically get linked
2186 // up to the switch. We also don't keep a pointer to the body, since all
2187 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002188 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00002189 Block = NULL;
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002190
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002191 // For pruning unreachable case statements, save the current state
2192 // for tracking the condition value.
2193 SaveAndRestore<bool> save_switchExclusivelyCovered(switchExclusivelyCovered,
2194 false);
Ted Kremenekbe528712011-03-04 01:03:41 +00002195
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002196 // Determine if the switch condition can be explicitly evaluated.
2197 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekbe528712011-03-04 01:03:41 +00002198 Expr::EvalResult result;
2199 tryEvaluate(Terminator->getCond(), result);
2200 SaveAndRestore<Expr::EvalResult*> save_switchCond(switchCond, &result);
2201
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002202 // If body is not a compound statement create implicit scope
2203 // and add destructors.
2204 if (!isa<CompoundStmt>(Terminator->getBody()))
2205 addLocalScopeAndDtors(Terminator->getBody());
2206
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002207 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00002208 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002209 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002210 return 0;
2211 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002212
Mike Stump31feda52009-07-17 01:31:16 +00002213 // If we have no "default:" case, the default transition is to the code
2214 // following the switch body.
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002215 addSuccessor(SwitchTerminatedBlock,
2216 switchExclusivelyCovered ? 0 : DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002217
Ted Kremenek81e14852007-08-27 19:46:09 +00002218 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002219 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002220 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002221 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002222
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002223 // Finally, if the SwitchStmt contains a condition variable, add both the
2224 // SwitchStmt and the condition variable initialization to the CFG.
2225 if (VarDecl *VD = Terminator->getConditionVariable()) {
2226 if (Expr *Init = VD->getInit()) {
2227 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00002228 appendStmt(Block, Terminator);
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002229 addStmt(Init);
2230 }
2231 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002232
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002233 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002234}
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002235
2236static bool shouldAddCase(bool &switchExclusivelyCovered,
2237 const Expr::EvalResult &switchCond,
2238 const CaseStmt *CS,
2239 ASTContext &Ctx) {
2240 bool addCase = false;
Ted Kremenekbe528712011-03-04 01:03:41 +00002241
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002242 if (!switchExclusivelyCovered) {
2243 if (switchCond.Val.isInt()) {
2244 // Evaluate the LHS of the case value.
2245 Expr::EvalResult V1;
2246 CS->getLHS()->Evaluate(V1, Ctx);
2247 assert(V1.Val.isInt());
2248 const llvm::APSInt &condInt = switchCond.Val.getInt();
2249 const llvm::APSInt &lhsInt = V1.Val.getInt();
2250
2251 if (condInt == lhsInt) {
2252 addCase = true;
2253 switchExclusivelyCovered = true;
2254 }
2255 else if (condInt < lhsInt) {
2256 if (const Expr *RHS = CS->getRHS()) {
2257 // Evaluate the RHS of the case value.
2258 Expr::EvalResult V2;
2259 RHS->Evaluate(V2, Ctx);
2260 assert(V2.Val.isInt());
2261 if (V2.Val.getInt() <= condInt) {
2262 addCase = true;
2263 switchExclusivelyCovered = true;
2264 }
2265 }
2266 }
2267 }
2268 else
2269 addCase = true;
2270 }
2271 return addCase;
2272}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002273
Ted Kremenek93668002009-07-17 22:18:43 +00002274CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00002275 // CaseStmts are essentially labels, so they are the first statement in a
2276 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00002277 CFGBlock *TopBlock = 0, *LastBlock = 0;
Ted Kremenekbe528712011-03-04 01:03:41 +00002278 assert(switchCond);
2279
Ted Kremenek60fa6572010-08-04 23:54:30 +00002280 if (Stmt *Sub = CS->getSubStmt()) {
2281 // For deeply nested chains of CaseStmts, instead of doing a recursion
2282 // (which can blow out the stack), manually unroll and create blocks
2283 // along the way.
2284 while (isa<CaseStmt>(Sub)) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002285 CFGBlock *currentBlock = createBlock(false);
2286 currentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00002287
Ted Kremenek60fa6572010-08-04 23:54:30 +00002288 if (TopBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002289 addSuccessor(LastBlock, currentBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002290 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002291 TopBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002292
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002293 addSuccessor(SwitchTerminatedBlock,
Ted Kremenekbe528712011-03-04 01:03:41 +00002294 shouldAddCase(switchExclusivelyCovered, *switchCond,
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002295 CS, *Context)
2296 ? currentBlock : 0);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002297
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002298 LastBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002299 CS = cast<CaseStmt>(Sub);
2300 Sub = CS->getSubStmt();
2301 }
2302
2303 addStmt(Sub);
2304 }
Mike Stump11289f42009-09-09 15:08:12 +00002305
Ted Kremenek55e91e82007-08-30 18:48:11 +00002306 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002307 if (!CaseBlock)
2308 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002309
2310 // Cases statements partition blocks, so this is the top of the basic block we
2311 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00002312 CaseBlock->setLabel(CS);
2313
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002314 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002315 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002316
2317 // Add this block to the list of successors for the block with the switch
2318 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00002319 assert(SwitchTerminatedBlock);
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002320 addSuccessor(SwitchTerminatedBlock,
Ted Kremenekbe528712011-03-04 01:03:41 +00002321 shouldAddCase(switchExclusivelyCovered, *switchCond,
Ted Kremenekeff9a7f2011-03-01 23:12:55 +00002322 CS, *Context)
2323 ? CaseBlock : 0);
Mike Stump31feda52009-07-17 01:31:16 +00002324
Ted Kremenek9aae5132007-08-23 21:42:29 +00002325 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2326 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002327
Ted Kremenek60fa6572010-08-04 23:54:30 +00002328 if (TopBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002329 addSuccessor(LastBlock, CaseBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002330 Succ = TopBlock;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002331 } else {
Ted Kremenek60fa6572010-08-04 23:54:30 +00002332 // This block is now the implicit successor of other blocks.
2333 Succ = CaseBlock;
2334 }
Mike Stump31feda52009-07-17 01:31:16 +00002335
Ted Kremenek60fa6572010-08-04 23:54:30 +00002336 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002337}
Mike Stump31feda52009-07-17 01:31:16 +00002338
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002339CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00002340 if (Terminator->getSubStmt())
2341 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00002342
Ted Kremenek654c78f2008-02-13 22:05:39 +00002343 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002344
2345 if (!DefaultCaseBlock)
2346 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002347
2348 // Default statements partition blocks, so this is the top of the basic block
2349 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002350 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00002351
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002352 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002353 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00002354
Mike Stump31feda52009-07-17 01:31:16 +00002355 // Unlike case statements, we don't add the default block to the successors
2356 // for the switch statement immediately. This is done when we finish
2357 // processing the switch statement. This allows for the default case
2358 // (including a fall-through to the code after the switch statement) to always
2359 // be the last successor of a switch-terminated block.
2360
Ted Kremenek654c78f2008-02-13 22:05:39 +00002361 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2362 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002363
Ted Kremenek654c78f2008-02-13 22:05:39 +00002364 // This block is now the implicit successor of other blocks.
2365 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002366
2367 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00002368}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002369
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002370CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2371 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2372 // current block.
2373 CFGBlock* TrySuccessor = NULL;
2374
2375 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002376 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002377 return 0;
2378 TrySuccessor = Block;
2379 } else TrySuccessor = Succ;
2380
Mike Stump0bdba6c2010-01-20 01:15:34 +00002381 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002382
2383 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00002384 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002385 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00002386 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002387
Mike Stump0bdba6c2010-01-20 01:15:34 +00002388 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002389 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2390 // The code after the try is the implicit successor.
2391 Succ = TrySuccessor;
2392 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002393 if (CS->getExceptionDecl() == 0) {
2394 HasCatchAll = true;
2395 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002396 Block = NULL;
2397 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2398 if (CatchBlock == 0)
2399 return 0;
2400 // Add this block to the list of successors for the block with the try
2401 // statement.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002402 addSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002403 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00002404 if (!HasCatchAll) {
2405 if (PrevTryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002406 addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002407 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002408 addSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00002409 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002410
2411 // The code after the try is the implicit successor.
2412 Succ = TrySuccessor;
2413
Mike Stump845384a2010-01-20 01:30:58 +00002414 // Save the current "try" context.
2415 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2416 TryTerminatedBlock = NewTryTerminatedBlock;
2417
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002418 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002419 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002420 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002421 return Block;
2422}
2423
2424CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2425 // CXXCatchStmt are treated like labels, so they are the first statement in a
2426 // block.
2427
Marcin Swiderski3546b1a2010-10-01 01:46:52 +00002428 // Save local scope position because in case of exception variable ScopePos
2429 // won't be restored when traversing AST.
2430 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2431
2432 // Create local scope for possible exception variable.
2433 // Store scope position. Add implicit destructor.
2434 if (VarDecl* VD = CS->getExceptionDecl()) {
2435 LocalScope::const_iterator BeginScopePos = ScopePos;
2436 addLocalScopeForVarDecl(VD);
2437 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2438 }
2439
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002440 if (CS->getHandlerBlock())
2441 addStmt(CS->getHandlerBlock());
2442
2443 CFGBlock* CatchBlock = Block;
2444 if (!CatchBlock)
2445 CatchBlock = createBlock();
2446
2447 CatchBlock->setLabel(CS);
2448
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002449 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002450 return 0;
2451
2452 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2453 Block = NULL;
2454
2455 return CatchBlock;
2456}
2457
John McCall5d413782010-12-06 08:20:24 +00002458CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002459 AddStmtChoice asc) {
2460 if (BuildOpts.AddImplicitDtors) {
2461 // If adding implicit destructors visit the full expression for adding
2462 // destructors of temporaries.
2463 VisitForTemporaryDtors(E->getSubExpr());
2464
2465 // Full expression has to be added as CFGStmt so it will be sequenced
2466 // before destructors of it's temporaries.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002467 asc = asc.withAlwaysAdd(true);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002468 }
2469 return Visit(E->getSubExpr(), asc);
2470}
2471
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002472CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2473 AddStmtChoice asc) {
2474 if (asc.alwaysAdd()) {
2475 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00002476 appendStmt(Block, E);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002477
2478 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002479 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002480 }
2481 return Visit(E->getSubExpr(), asc);
2482}
2483
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002484CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2485 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002486 autoCreateBlock();
Zhongxing Xufb2f8162010-11-03 11:14:06 +00002487 if (!C->isElidable())
Ted Kremenek2866bab2011-03-10 01:14:08 +00002488 appendStmt(Block, C);
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002489
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002490 return VisitChildren(C);
2491}
2492
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002493CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2494 AddStmtChoice asc) {
2495 if (asc.alwaysAdd()) {
2496 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00002497 appendStmt(Block, E);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002498 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002499 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002500 }
2501 return Visit(E->getSubExpr(), asc);
2502}
2503
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002504CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2505 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002506 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00002507 appendStmt(Block, C);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002508 return VisitChildren(C);
2509}
2510
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002511CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00002512 AddStmtChoice asc) {
Zhongxing Xu7e612172010-04-13 09:38:01 +00002513 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00002514 appendStmt(Block, C);
Zhongxing Xu7e612172010-04-13 09:38:01 +00002515 return VisitChildren(C);
2516}
2517
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002518CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2519 AddStmtChoice asc) {
2520 if (asc.alwaysAdd()) {
2521 autoCreateBlock();
Ted Kremenek2866bab2011-03-10 01:14:08 +00002522 appendStmt(Block, E);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002523 }
Ted Kremenek8219b822010-12-16 07:46:53 +00002524 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002525}
2526
Ted Kremenekeda180e22007-08-28 19:26:49 +00002527CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00002528 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00002529 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002530
Ted Kremenekeda180e22007-08-28 19:26:49 +00002531 if (!IBlock) {
2532 IBlock = createBlock(false);
2533 cfg->setIndirectGotoBlock(IBlock);
2534 }
Mike Stump31feda52009-07-17 01:31:16 +00002535
Ted Kremenekeda180e22007-08-28 19:26:49 +00002536 // IndirectGoto is a control-flow statement. Thus we stop processing the
2537 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002538 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00002539 return 0;
2540
Ted Kremenekeda180e22007-08-28 19:26:49 +00002541 Block = createBlock(false);
2542 Block->setTerminator(I);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002543 addSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00002544 return addStmt(I->getTarget());
2545}
2546
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002547CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2548tryAgain:
2549 if (!E) {
2550 badCFG = true;
2551 return NULL;
2552 }
2553 switch (E->getStmtClass()) {
2554 default:
2555 return VisitChildrenForTemporaryDtors(E);
2556
2557 case Stmt::BinaryOperatorClass:
2558 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2559
2560 case Stmt::CXXBindTemporaryExprClass:
2561 return VisitCXXBindTemporaryExprForTemporaryDtors(
2562 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2563
John McCallc07a0c72011-02-17 10:25:35 +00002564 case Stmt::BinaryConditionalOperatorClass:
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002565 case Stmt::ConditionalOperatorClass:
2566 return VisitConditionalOperatorForTemporaryDtors(
John McCallc07a0c72011-02-17 10:25:35 +00002567 cast<AbstractConditionalOperator>(E), BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002568
2569 case Stmt::ImplicitCastExprClass:
2570 // For implicit cast we want BindToTemporary to be passed further.
2571 E = cast<CastExpr>(E)->getSubExpr();
2572 goto tryAgain;
2573
2574 case Stmt::ParenExprClass:
2575 E = cast<ParenExpr>(E)->getSubExpr();
2576 goto tryAgain;
2577 }
2578}
2579
2580CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2581 // When visiting children for destructors we want to visit them in reverse
2582 // order. Because there's no reverse iterator for children must to reverse
2583 // them in helper vector.
2584 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2585 ChildrenVect ChildrenRev;
John McCall8322c3a2011-02-13 04:07:26 +00002586 for (Stmt::child_range I = E->children(); I; ++I) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002587 if (*I) ChildrenRev.push_back(*I);
2588 }
2589
2590 CFGBlock *B = Block;
2591 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2592 L = ChildrenRev.rend(); I != L; ++I) {
2593 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2594 B = R;
2595 }
2596 return B;
2597}
2598
2599CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2600 if (E->isLogicalOp()) {
2601 // Destructors for temporaries in LHS expression should be called after
2602 // those for RHS expression. Even if this will unnecessarily create a block,
2603 // this block will be used at least by the full expression.
2604 autoCreateBlock();
2605 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2606 if (badCFG)
2607 return NULL;
2608
2609 Succ = ConfluenceBlock;
2610 Block = NULL;
2611 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2612
2613 if (RHSBlock) {
2614 if (badCFG)
2615 return NULL;
2616
2617 // If RHS expression did produce destructors we need to connect created
2618 // blocks to CFG in same manner as for binary operator itself.
2619 CFGBlock *LHSBlock = createBlock(false);
2620 LHSBlock->setTerminator(CFGTerminator(E, true));
2621
2622 // For binary operator LHS block is before RHS in list of predecessors
2623 // of ConfluenceBlock.
2624 std::reverse(ConfluenceBlock->pred_begin(),
2625 ConfluenceBlock->pred_end());
2626
2627 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002628 TryResult KnownVal = tryEvaluateBool(E->getLHS());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002629 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2630 KnownVal.negate();
2631
2632 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2633 // itself.
2634 if (E->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002635 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2636 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002637 } else {
2638 assert (E->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002639 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2640 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002641 }
2642
2643 Block = LHSBlock;
2644 return LHSBlock;
2645 }
2646
2647 Block = ConfluenceBlock;
2648 return ConfluenceBlock;
2649 }
2650
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002651 if (E->isAssignmentOp()) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002652 // For assignment operator (=) LHS expression is visited
2653 // before RHS expression. For destructors visit them in reverse order.
2654 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2655 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2656 return LHSBlock ? LHSBlock : RHSBlock;
2657 }
2658
2659 // For any other binary operator RHS expression is visited before
2660 // LHS expression (order of children). For destructors visit them in reverse
2661 // order.
2662 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2663 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2664 return RHSBlock ? RHSBlock : LHSBlock;
2665}
2666
2667CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2668 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2669 // First add destructors for temporaries in subexpression.
2670 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xufee455f2010-11-14 15:23:50 +00002671 if (!BindToTemporary) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002672 // If lifetime of temporary is not prolonged (by assigning to constant
2673 // reference) add destructor for it.
2674 autoCreateBlock();
2675 appendTemporaryDtor(Block, E);
2676 B = Block;
2677 }
2678 return B;
2679}
2680
2681CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
John McCallc07a0c72011-02-17 10:25:35 +00002682 AbstractConditionalOperator *E, bool BindToTemporary) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002683 // First add destructors for condition expression. Even if this will
2684 // unnecessarily create a block, this block will be used at least by the full
2685 // expression.
2686 autoCreateBlock();
2687 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2688 if (badCFG)
2689 return NULL;
John McCallc07a0c72011-02-17 10:25:35 +00002690 if (BinaryConditionalOperator *BCO
2691 = dyn_cast<BinaryConditionalOperator>(E)) {
2692 ConfluenceBlock = VisitForTemporaryDtors(BCO->getCommon());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002693 if (badCFG)
2694 return NULL;
2695 }
2696
John McCallc07a0c72011-02-17 10:25:35 +00002697 // Try to add block with destructors for LHS expression.
2698 CFGBlock *LHSBlock = NULL;
2699 Succ = ConfluenceBlock;
2700 Block = NULL;
2701 LHSBlock = VisitForTemporaryDtors(E->getTrueExpr(), BindToTemporary);
2702 if (badCFG)
2703 return NULL;
2704
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002705 // Try to add block with destructors for RHS expression;
2706 Succ = ConfluenceBlock;
2707 Block = NULL;
John McCallc07a0c72011-02-17 10:25:35 +00002708 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getFalseExpr(),
2709 BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002710 if (badCFG)
2711 return NULL;
2712
2713 if (!RHSBlock && !LHSBlock) {
2714 // If neither LHS nor RHS expression had temporaries to destroy don't create
2715 // more blocks.
2716 Block = ConfluenceBlock;
2717 return Block;
2718 }
2719
2720 Block = createBlock(false);
2721 Block->setTerminator(CFGTerminator(E, true));
2722
2723 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002724 const TryResult &KnownVal = tryEvaluateBool(E->getCond());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002725
2726 if (LHSBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002727 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002728 } else if (KnownVal.isFalse()) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002729 addSuccessor(Block, NULL);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002730 } else {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002731 addSuccessor(Block, ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002732 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2733 }
2734
2735 if (!RHSBlock)
2736 RHSBlock = ConfluenceBlock;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002737 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002738
2739 return Block;
2740}
2741
Ted Kremenek04cca642007-08-23 21:26:19 +00002742} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00002743
Mike Stump31feda52009-07-17 01:31:16 +00002744/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2745/// no successors or predecessors. If this is the first block created in the
2746/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00002747CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00002748 bool first_block = begin() == end();
2749
2750 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002751 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2752 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2753 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00002754
2755 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002756 if (first_block)
2757 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00002758
2759 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002760 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002761}
2762
Ted Kremenek889073f2007-08-23 16:51:22 +00002763/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2764/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00002765CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenekf9d82902011-03-10 01:14:05 +00002766 const BuildOptions &BO) {
2767 CFGBuilder Builder(C, BO);
2768 return Builder.buildCFG(D, Statement);
Ted Kremenek889073f2007-08-23 16:51:22 +00002769}
2770
Ted Kremenek8cfe2072011-03-03 01:21:32 +00002771const CXXDestructorDecl *
2772CFGImplicitDtor::getDestructorDecl(ASTContext &astContext) const {
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002773 switch (getKind()) {
2774 case CFGElement::Invalid:
2775 case CFGElement::Statement:
2776 case CFGElement::Initializer:
2777 llvm_unreachable("getDestructorDecl should only be used with "
2778 "ImplicitDtors");
2779 case CFGElement::AutomaticObjectDtor: {
2780 const VarDecl *var = cast<CFGAutomaticObjDtor>(this)->getVarDecl();
2781 QualType ty = var->getType();
Ted Kremenek1676a042011-03-03 01:01:03 +00002782 ty = ty.getNonReferenceType();
Ted Kremenek8cfe2072011-03-03 01:21:32 +00002783 if (const ArrayType *arrayType = astContext.getAsArrayType(ty)) {
2784 ty = arrayType->getElementType();
2785 }
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002786 const RecordType *recordType = ty->getAs<RecordType>();
2787 const CXXRecordDecl *classDecl =
Ted Kremenek1676a042011-03-03 01:01:03 +00002788 cast<CXXRecordDecl>(recordType->getDecl());
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002789 return classDecl->getDestructor();
2790 }
2791 case CFGElement::TemporaryDtor: {
2792 const CXXBindTemporaryExpr *bindExpr =
2793 cast<CFGTemporaryDtor>(this)->getBindTemporaryExpr();
2794 const CXXTemporary *temp = bindExpr->getTemporary();
2795 return temp->getDestructor();
2796 }
2797 case CFGElement::BaseDtor:
2798 case CFGElement::MemberDtor:
2799
2800 // Not yet supported.
2801 return 0;
2802 }
Ted Kremenek1676a042011-03-03 01:01:03 +00002803 llvm_unreachable("getKind() returned bogus value");
Matt Beaumont-Gay86b900b2011-03-03 00:48:05 +00002804 return 0;
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002805}
2806
Ted Kremenek8cfe2072011-03-03 01:21:32 +00002807bool CFGImplicitDtor::isNoReturn(ASTContext &astContext) const {
2808 if (const CXXDestructorDecl *cdecl = getDestructorDecl(astContext)) {
Ted Kremeneke06a55c2011-03-02 20:32:29 +00002809 QualType ty = cdecl->getType();
2810 return cast<FunctionType>(ty)->getNoReturnAttr();
2811 }
2812 return false;
Ted Kremenek96a7a592011-03-01 03:15:10 +00002813}
2814
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002815//===----------------------------------------------------------------------===//
2816// CFG: Queries for BlkExprs.
2817//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002818
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002819namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002820 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002821}
2822
Ted Kremenekbff98442009-12-23 23:37:10 +00002823static void FindSubExprAssignments(Stmt *S,
2824 llvm::SmallPtrSet<Expr*,50>& Set) {
2825 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00002826 return;
Mike Stump31feda52009-07-17 01:31:16 +00002827
John McCall8322c3a2011-02-13 04:07:26 +00002828 for (Stmt::child_range I = S->children(); I; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002829 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00002830 if (!child)
2831 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002832
Ted Kremenekbff98442009-12-23 23:37:10 +00002833 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002834 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00002835
Ted Kremenekbff98442009-12-23 23:37:10 +00002836 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00002837 }
2838}
2839
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002840static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2841 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00002842
2843 // Look for assignments that are used as subexpressions. These are the only
2844 // assignments that we want to *possibly* register as a block-level
2845 // expression. Basically, if an assignment occurs both in a subexpression and
2846 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00002847 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00002848
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002849 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002850 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek96a7a592011-03-01 03:15:10 +00002851 if (const CFGStmt *S = BI->getAs<CFGStmt>())
2852 FindSubExprAssignments(S->getStmt(), SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002853
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002854 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00002855
2856 // Iterate over the statements again on identify the Expr* and Stmt* at the
2857 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002858
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002859 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
Ted Kremenek96a7a592011-03-01 03:15:10 +00002860 const CFGStmt *CS = BI->getAs<CFGStmt>();
2861 if (!CS)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002862 continue;
Ted Kremenek96a7a592011-03-01 03:15:10 +00002863 if (Expr* Exp = dyn_cast<Expr>(CS->getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00002864
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002865 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002866 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00002867 // expression are really "statements" whose value is never used by
2868 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002869 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002870 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002871 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2872 // Special handling for statement expressions. The last statement in
2873 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002874 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002875 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002876 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002877 (*M)[C->body_back()] = x;
2878 }
2879 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002880
Ted Kremenek95a123c2008-01-26 00:03:27 +00002881 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002882 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002883 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002884 }
Mike Stump31feda52009-07-17 01:31:16 +00002885
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002886 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002887
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002888 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002889
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002890 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002891 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002892 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002893 }
2894 }
Mike Stump31feda52009-07-17 01:31:16 +00002895
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002896 return M;
2897}
2898
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002899CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2900 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002901 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002902
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002903 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002904 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002905 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002906}
2907
2908unsigned CFG::getNumBlkExprs() {
2909 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2910 return M->size();
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002911
2912 // We assume callers interested in the number of BlkExprs will want
2913 // the map constructed if it doesn't already exist.
2914 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2915 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002916}
2917
Ted Kremenek6065ef62008-04-28 18:00:46 +00002918//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002919// Filtered walking of the CFG.
2920//===----------------------------------------------------------------------===//
2921
2922bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002923 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002924
Ted Kremenek89794742011-03-07 22:04:39 +00002925 if (To && F.IgnoreDefaultsWithCoveredEnums) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002926 // If the 'To' has no label or is labeled but the label isn't a
2927 // CaseStmt then filter this edge.
2928 if (const SwitchStmt *S =
Ted Kremenek89794742011-03-07 22:04:39 +00002929 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002930 if (S->isAllEnumCasesCovered()) {
Ted Kremenek89794742011-03-07 22:04:39 +00002931 const Stmt *L = To->getLabel();
2932 if (!L || !isa<CaseStmt>(L))
2933 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002934 }
2935 }
2936 }
2937
2938 return false;
2939}
2940
2941//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002942// Cleanup: CFG dstor.
2943//===----------------------------------------------------------------------===//
2944
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002945CFG::~CFG() {
2946 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2947}
Mike Stump31feda52009-07-17 01:31:16 +00002948
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002949//===----------------------------------------------------------------------===//
2950// CFG pretty printing
2951//===----------------------------------------------------------------------===//
2952
Ted Kremenek7e776b12007-08-22 18:22:34 +00002953namespace {
2954
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002955class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek96a7a592011-03-01 03:15:10 +00002956 typedef llvm::DenseMap<const Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
2957 typedef llvm::DenseMap<const Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002958 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002959 DeclMapTy DeclMap;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002960 signed currentBlock;
2961 unsigned currentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002962 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002963public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002964
Chris Lattnerc61089a2009-06-30 01:26:17 +00002965 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
Ted Kremenek96a7a592011-03-01 03:15:10 +00002966 : currentBlock(0), currentStmt(0), LangOpts(LO)
2967 {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002968 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2969 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002970 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002971 BI != BEnd; ++BI, ++j ) {
Ted Kremenek96a7a592011-03-01 03:15:10 +00002972 if (const CFGStmt *SE = BI->getAs<CFGStmt>()) {
2973 const Stmt *stmt= SE->getStmt();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002974 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
Ted Kremenek96a7a592011-03-01 03:15:10 +00002975 StmtMap[stmt] = P;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002976
Ted Kremenek96a7a592011-03-01 03:15:10 +00002977 switch (stmt->getStmtClass()) {
2978 case Stmt::DeclStmtClass:
2979 DeclMap[cast<DeclStmt>(stmt)->getSingleDecl()] = P;
2980 break;
2981 case Stmt::IfStmtClass: {
2982 const VarDecl *var = cast<IfStmt>(stmt)->getConditionVariable();
2983 if (var)
2984 DeclMap[var] = P;
2985 break;
2986 }
2987 case Stmt::ForStmtClass: {
2988 const VarDecl *var = cast<ForStmt>(stmt)->getConditionVariable();
2989 if (var)
2990 DeclMap[var] = P;
2991 break;
2992 }
2993 case Stmt::WhileStmtClass: {
2994 const VarDecl *var =
2995 cast<WhileStmt>(stmt)->getConditionVariable();
2996 if (var)
2997 DeclMap[var] = P;
2998 break;
2999 }
3000 case Stmt::SwitchStmtClass: {
3001 const VarDecl *var =
3002 cast<SwitchStmt>(stmt)->getConditionVariable();
3003 if (var)
3004 DeclMap[var] = P;
3005 break;
3006 }
3007 case Stmt::CXXCatchStmtClass: {
3008 const VarDecl *var =
3009 cast<CXXCatchStmt>(stmt)->getExceptionDecl();
3010 if (var)
3011 DeclMap[var] = P;
3012 break;
3013 }
3014 default:
3015 break;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003016 }
3017 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003018 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00003019 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003020 }
Ted Kremenek96a7a592011-03-01 03:15:10 +00003021
Mike Stump31feda52009-07-17 01:31:16 +00003022
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003023 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00003024
Chris Lattnerc61089a2009-06-30 01:26:17 +00003025 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00003026 void setBlockID(signed i) { currentBlock = i; }
3027 void setStmtID(unsigned i) { currentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00003028
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003029 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
3030 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003031
3032 if (I == StmtMap.end())
3033 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003034
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00003035 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3036 && I->second.second == currentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003037 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00003038 }
Mike Stump31feda52009-07-17 01:31:16 +00003039
Ted Kremenek60983dc2010-01-19 20:52:05 +00003040 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003041 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003042 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003043
Ted Kremenek96a7a592011-03-01 03:15:10 +00003044 bool handleDecl(const Decl* D, llvm::raw_ostream& OS) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003045 DeclMapTy::iterator I = DeclMap.find(D);
3046
3047 if (I == DeclMap.end())
3048 return false;
3049
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00003050 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3051 && I->second.second == currentStmt) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003052 return false;
3053 }
3054
3055 OS << "[B" << I->second.first << "." << I->second.second << "]";
3056 return true;
3057 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003058};
Chris Lattnerc61089a2009-06-30 01:26:17 +00003059} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003060
Chris Lattnerc61089a2009-06-30 01:26:17 +00003061
3062namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00003063class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00003064 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00003065
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003066 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003067 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00003068 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003069public:
Douglas Gregor7de59662009-05-29 20:38:28 +00003070 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00003071 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00003072 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00003073
Ted Kremenek9aae5132007-08-23 21:42:29 +00003074 void VisitIfStmt(IfStmt* I) {
3075 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00003076 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00003077 }
Mike Stump31feda52009-07-17 01:31:16 +00003078
Ted Kremenek9aae5132007-08-23 21:42:29 +00003079 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00003080 void VisitStmt(Stmt* Terminator) {
3081 Terminator->printPretty(OS, Helper, Policy);
3082 }
3083
Ted Kremenek9aae5132007-08-23 21:42:29 +00003084 void VisitForStmt(ForStmt* F) {
3085 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00003086 if (F->getInit())
3087 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00003088 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00003089 if (Stmt* C = F->getCond())
3090 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00003091 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00003092 if (F->getInc())
3093 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00003094 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00003095 }
Mike Stump31feda52009-07-17 01:31:16 +00003096
Ted Kremenek9aae5132007-08-23 21:42:29 +00003097 void VisitWhileStmt(WhileStmt* W) {
3098 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00003099 if (Stmt* C = W->getCond())
3100 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00003101 }
Mike Stump31feda52009-07-17 01:31:16 +00003102
Ted Kremenek9aae5132007-08-23 21:42:29 +00003103 void VisitDoStmt(DoStmt* D) {
3104 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00003105 if (Stmt* C = D->getCond())
3106 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00003107 }
Mike Stump31feda52009-07-17 01:31:16 +00003108
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003109 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00003110 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00003111 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00003112 }
Mike Stump31feda52009-07-17 01:31:16 +00003113
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003114 void VisitCXXTryStmt(CXXTryStmt* CS) {
3115 OS << "try ...";
3116 }
3117
John McCallc07a0c72011-02-17 10:25:35 +00003118 void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00003119 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003120 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003121 }
Mike Stump31feda52009-07-17 01:31:16 +00003122
Ted Kremenek391f94a2007-08-31 22:29:13 +00003123 void VisitChooseExpr(ChooseExpr* C) {
3124 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00003125 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00003126 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00003127 }
Mike Stump31feda52009-07-17 01:31:16 +00003128
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003129 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
3130 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00003131 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003132 }
Mike Stump31feda52009-07-17 01:31:16 +00003133
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003134 void VisitBinaryOperator(BinaryOperator* B) {
3135 if (!B->isLogicalOp()) {
3136 VisitExpr(B);
3137 return;
3138 }
Mike Stump31feda52009-07-17 01:31:16 +00003139
Douglas Gregor7de59662009-05-29 20:38:28 +00003140 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003141
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003142 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00003143 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00003144 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003145 return;
John McCalle3027922010-08-25 11:45:40 +00003146 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00003147 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003148 return;
3149 default:
3150 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00003151 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003152 }
Mike Stump31feda52009-07-17 01:31:16 +00003153
Ted Kremenek12687ff2007-08-27 21:54:41 +00003154 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00003155 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003156 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00003157};
Chris Lattnerc61089a2009-06-30 01:26:17 +00003158} // end anonymous namespace
3159
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003160static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00003161 const CFGElement &E) {
Ted Kremenek96a7a592011-03-01 03:15:10 +00003162 if (const CFGStmt *CS = E.getAs<CFGStmt>()) {
3163 Stmt *S = CS->getStmt();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003164
3165 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00003166
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003167 // special printing for statement-expressions.
3168 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3169 CompoundStmt* Sub = SE->getSubStmt();
3170
John McCall8322c3a2011-02-13 04:07:26 +00003171 if (Sub->children()) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003172 OS << "({ ... ; ";
3173 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3174 OS << " })\n";
3175 return;
3176 }
3177 }
3178 // special printing for comma expressions.
3179 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3180 if (B->getOpcode() == BO_Comma) {
3181 OS << "... , ";
3182 Helper->handledStmt(B->getRHS(),OS);
3183 OS << '\n';
3184 return;
3185 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003186 }
3187 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003188 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00003189
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003190 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan59f09c72010-11-22 19:32:14 +00003191 OS << " (OperatorCall)";
3192 } else if (isa<CXXBindTemporaryExpr>(S)) {
3193 OS << " (BindTemporary)";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003194 }
Mike Stump31feda52009-07-17 01:31:16 +00003195
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003196 // Expressions need a newline.
3197 if (isa<Expr>(S))
3198 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00003199
Ted Kremenek96a7a592011-03-01 03:15:10 +00003200 } else if (const CFGInitializer *IE = E.getAs<CFGInitializer>()) {
3201 const CXXCtorInitializer *I = IE->getInitializer();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003202 if (I->isBaseInitializer())
3203 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichetd583da02010-12-04 09:14:42 +00003204 else OS << I->getAnyMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00003205
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003206 OS << "(";
3207 if (Expr* IE = I->getInit())
3208 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3209 OS << ")";
3210
3211 if (I->isBaseInitializer())
3212 OS << " (Base initializer)\n";
3213 else OS << " (Member initializer)\n";
3214
Ted Kremenek96a7a592011-03-01 03:15:10 +00003215 } else if (const CFGAutomaticObjDtor *DE = E.getAs<CFGAutomaticObjDtor>()){
3216 const VarDecl* VD = DE->getVarDecl();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003217 Helper->handleDecl(VD, OS);
3218
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003219 const Type* T = VD->getType().getTypePtr();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003220 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3221 T = RT->getPointeeType().getTypePtr();
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003222 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3223 T = ET;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003224
3225 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3226 OS << " (Implicit destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003227
Ted Kremenek96a7a592011-03-01 03:15:10 +00003228 } else if (const CFGBaseDtor *BE = E.getAs<CFGBaseDtor>()) {
3229 const CXXBaseSpecifier *BS = BE->getBaseSpecifier();
Marcin Swiderski20b88732010-10-05 05:37:00 +00003230 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003231 OS << " (Base object destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003232
Ted Kremenek96a7a592011-03-01 03:15:10 +00003233 } else if (const CFGMemberDtor *ME = E.getAs<CFGMemberDtor>()) {
3234 const FieldDecl *FD = ME->getFieldDecl();
Marcin Swiderski01769902010-10-25 07:05:54 +00003235
3236 const Type *T = FD->getType().getTypePtr();
3237 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3238 T = ET;
3239
Marcin Swiderski20b88732010-10-05 05:37:00 +00003240 OS << "this->" << FD->getName();
Marcin Swiderski01769902010-10-25 07:05:54 +00003241 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003242 OS << " (Member object destructor)\n";
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00003243
Ted Kremenek96a7a592011-03-01 03:15:10 +00003244 } else if (const CFGTemporaryDtor *TE = E.getAs<CFGTemporaryDtor>()) {
3245 const CXXBindTemporaryExpr *BT = TE->getBindTemporaryExpr();
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00003246 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3247 OS << " (Temporary object destructor)\n";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003248 }
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00003249}
Mike Stump31feda52009-07-17 01:31:16 +00003250
Chris Lattnerc61089a2009-06-30 01:26:17 +00003251static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3252 const CFGBlock& B,
3253 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00003254
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003255 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00003256
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003257 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00003258 OS << "\n [ B" << B.getBlockID();
3259
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003260 if (&B == &cfg->getEntry())
3261 OS << " (ENTRY) ]\n";
3262 else if (&B == &cfg->getExit())
3263 OS << " (EXIT) ]\n";
3264 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003265 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003266 else
3267 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00003268
Ted Kremenek71eca012007-08-29 23:20:49 +00003269 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00003270 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003271
3272 if (print_edges)
3273 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003274
Mike Stump92244b02010-01-19 22:00:14 +00003275 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003276 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00003277 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00003278 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003279 C->getLHS()->printPretty(OS, Helper,
3280 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003281 if (C->getRHS()) {
3282 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003283 C->getRHS()->printPretty(OS, Helper,
3284 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003285 }
Mike Stump92244b02010-01-19 22:00:14 +00003286 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003287 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00003288 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003289 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00003290 if (CS->getExceptionDecl())
3291 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3292 0);
3293 else
3294 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003295 OS << ")";
3296
3297 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003298 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00003299
Ted Kremenek71eca012007-08-29 23:20:49 +00003300 OS << ":\n";
3301 }
Mike Stump31feda52009-07-17 01:31:16 +00003302
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003303 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003304 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00003305
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003306 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3307 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00003308
Ted Kremenek71eca012007-08-29 23:20:49 +00003309 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003310 if (print_edges)
3311 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003312
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003313 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00003314
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003315 if (Helper)
3316 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00003317
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003318 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003319 }
Mike Stump31feda52009-07-17 01:31:16 +00003320
Ted Kremenek71eca012007-08-29 23:20:49 +00003321 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003322 if (B.getTerminator()) {
3323 if (print_edges)
3324 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003325
Ted Kremenek71eca012007-08-29 23:20:49 +00003326 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00003327
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003328 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00003329
Chris Lattnerc61089a2009-06-30 01:26:17 +00003330 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3331 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003332 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003333 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003334 }
Mike Stump31feda52009-07-17 01:31:16 +00003335
Ted Kremenek71eca012007-08-29 23:20:49 +00003336 if (print_edges) {
3337 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003338 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00003339 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00003340
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003341 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3342 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003343
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003344 if (i == 8 || (i-8) == 0)
3345 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00003346
Ted Kremenek71eca012007-08-29 23:20:49 +00003347 OS << " B" << (*I)->getBlockID();
3348 }
Mike Stump31feda52009-07-17 01:31:16 +00003349
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003350 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00003351
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003352 // Print the successors of this block.
3353 OS << " Successors (" << B.succ_size() << "):";
3354 i = 0;
3355
3356 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3357 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003358
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003359 if (i == 8 || (i-8) % 10 == 0)
3360 OS << "\n ";
3361
Mike Stump0d76d072009-07-20 23:24:15 +00003362 if (*I)
3363 OS << " B" << (*I)->getBlockID();
3364 else
3365 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003366 }
Mike Stump31feda52009-07-17 01:31:16 +00003367
Ted Kremenek71eca012007-08-29 23:20:49 +00003368 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003369 }
Mike Stump31feda52009-07-17 01:31:16 +00003370}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003371
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003372
3373/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003374void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003375
3376/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003377void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3378 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00003379
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003380 // Print the entry block.
3381 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00003382
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003383 // Iterate through the CFGBlocks and print them one by one.
3384 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3385 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003386 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003387 continue;
Mike Stump31feda52009-07-17 01:31:16 +00003388
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003389 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003390 }
Mike Stump31feda52009-07-17 01:31:16 +00003391
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003392 // Print the exit block.
3393 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00003394 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00003395}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003396
3397/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003398void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3399 print(llvm::errs(), cfg, LO);
3400}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003401
3402/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3403/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003404void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3405 const LangOptions &LO) const {
3406 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003407 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00003408}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003409
Ted Kremenek15647632008-01-30 23:02:42 +00003410/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003411void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00003412 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00003413 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003414 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003415}
3416
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003417Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003418 Stmt *Terminator = this->Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003419 if (!Terminator)
3420 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003421
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003422 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003423
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003424 switch (Terminator->getStmtClass()) {
3425 default:
3426 break;
Mike Stump31feda52009-07-17 01:31:16 +00003427
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003428 case Stmt::ForStmtClass:
3429 E = cast<ForStmt>(Terminator)->getCond();
3430 break;
Mike Stump31feda52009-07-17 01:31:16 +00003431
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003432 case Stmt::WhileStmtClass:
3433 E = cast<WhileStmt>(Terminator)->getCond();
3434 break;
Mike Stump31feda52009-07-17 01:31:16 +00003435
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003436 case Stmt::DoStmtClass:
3437 E = cast<DoStmt>(Terminator)->getCond();
3438 break;
Mike Stump31feda52009-07-17 01:31:16 +00003439
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003440 case Stmt::IfStmtClass:
3441 E = cast<IfStmt>(Terminator)->getCond();
3442 break;
Mike Stump31feda52009-07-17 01:31:16 +00003443
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003444 case Stmt::ChooseExprClass:
3445 E = cast<ChooseExpr>(Terminator)->getCond();
3446 break;
Mike Stump31feda52009-07-17 01:31:16 +00003447
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003448 case Stmt::IndirectGotoStmtClass:
3449 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3450 break;
Mike Stump31feda52009-07-17 01:31:16 +00003451
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003452 case Stmt::SwitchStmtClass:
3453 E = cast<SwitchStmt>(Terminator)->getCond();
3454 break;
Mike Stump31feda52009-07-17 01:31:16 +00003455
John McCallc07a0c72011-02-17 10:25:35 +00003456 case Stmt::BinaryConditionalOperatorClass:
3457 E = cast<BinaryConditionalOperator>(Terminator)->getCond();
3458 break;
3459
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003460 case Stmt::ConditionalOperatorClass:
3461 E = cast<ConditionalOperator>(Terminator)->getCond();
3462 break;
Mike Stump31feda52009-07-17 01:31:16 +00003463
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003464 case Stmt::BinaryOperatorClass: // '&&' and '||'
3465 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003466 break;
Mike Stump31feda52009-07-17 01:31:16 +00003467
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003468 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00003469 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003470 }
Mike Stump31feda52009-07-17 01:31:16 +00003471
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003472 return E ? E->IgnoreParens() : NULL;
3473}
3474
Ted Kremenek92137a32008-05-16 16:06:00 +00003475bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003476 const Stmt *Terminator = this->Terminator;
Ted Kremenek92137a32008-05-16 16:06:00 +00003477 if (!Terminator)
3478 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003479
Ted Kremenek92137a32008-05-16 16:06:00 +00003480 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003481
Ted Kremenek92137a32008-05-16 16:06:00 +00003482 switch (Terminator->getStmtClass()) {
3483 default:
3484 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003485
3486 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003487 case Stmt::WhileStmtClass:
3488 case Stmt::DoStmtClass:
3489 case Stmt::IfStmtClass:
3490 case Stmt::ChooseExprClass:
John McCallc07a0c72011-02-17 10:25:35 +00003491 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003492 case Stmt::ConditionalOperatorClass:
3493 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00003494 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00003495 }
Mike Stump31feda52009-07-17 01:31:16 +00003496
Ted Kremenek92137a32008-05-16 16:06:00 +00003497 return E ? E->IgnoreParens() : NULL;
3498}
3499
Ted Kremenek15647632008-01-30 23:02:42 +00003500
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003501//===----------------------------------------------------------------------===//
3502// CFG Graphviz Visualization
3503//===----------------------------------------------------------------------===//
3504
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003505
3506#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00003507static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003508#endif
3509
Chris Lattnerc61089a2009-06-30 01:26:17 +00003510void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003511#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00003512 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003513 GraphHelper = &H;
3514 llvm::ViewGraph(this,"CFG");
3515 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003516#endif
3517}
3518
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003519namespace llvm {
3520template<>
3521struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00003522
3523 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3524
3525 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003526
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003527#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003528 std::string OutSStr;
3529 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003530 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003531 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003532
3533 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3534
3535 // Process string output to make it nicer...
3536 for (unsigned i = 0; i != OutStr.length(); ++i)
3537 if (OutStr[i] == '\n') { // Left justify
3538 OutStr[i] = '\\';
3539 OutStr.insert(OutStr.begin()+i+1, 'l');
3540 }
Mike Stump31feda52009-07-17 01:31:16 +00003541
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003542 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003543#else
3544 return "";
3545#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003546 }
3547};
3548} // end namespace llvm