blob: 18bbbb1c86cfd6844dc2637d8295f665c23ca547 [file] [log] [blame]
Ted Kremenekfddd5182007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenekfddd5182007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekbd048782009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stumpb978a442010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenekc310e932007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Ted Kremenekc56c0042011-02-23 05:11:46 +000020#include "clang/AST/CharUnits.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000021#include "llvm/Support/GraphWriter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000022#include "llvm/Support/Allocator.h"
23#include "llvm/Support/Format.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000024#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000025#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek0ba497b2009-10-20 23:46:25 +000026#include "llvm/ADT/OwningPtr.h"
Ted Kremenek83c01da2008-01-11 00:40:29 +000027
Ted Kremenekfddd5182007-08-21 21:42:03 +000028using namespace clang;
29
30namespace {
31
Douglas Gregor4afa39d2009-01-20 01:17:11 +000032static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-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 Stump6d9828c2009-07-17 01:31:16 +000036 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000037}
Ted Kremenekad5a8942010-08-02 23:46:59 +000038
Zhanyong Wan94a3dcf2010-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 Kremenek852274d2009-12-16 03:18:58 +000052class AddStmtChoice {
53public:
Ted Kremenek892697d2010-12-16 07:46:53 +000054 enum Kind { NotAlwaysAdd = 0, AlwaysAdd = 1 };
Ted Kremenek5ba290a2010-03-02 21:43:54 +000055
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000056 AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {}
Ted Kremenek5ba290a2010-03-02 21:43:54 +000057
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000058 bool alwaysAdd() const { return kind & AlwaysAdd; }
Zhanyong Wan94a3dcf2010-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 Kremenek852274d2009-12-16 03:18:58 +000067private:
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000068 Kind kind;
Ted Kremenek852274d2009-12-16 03:18:58 +000069};
Mike Stump6d9828c2009-07-17 01:31:16 +000070
Marcin Swiderskif1308c72010-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 Swiderski35387a02010-09-30 22:42:32 +000081/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderskif1308c72010-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 Kremenekfe59b742011-02-15 02:47:45 +000094 typedef BumpVector<VarDecl*> AutomaticVarsTy;
Marcin Swiderskif1308c72010-09-25 11:05:21 +000095
96 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderski35387a02010-09-30 22:42:32 +000097 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderskif1308c72010-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 Swiderski35387a02010-09-30 22:42:32 +0000141 const_iterator operator++(int) {
142 const_iterator P = *this;
143 ++*this;
144 return P;
145 }
Marcin Swiderskif1308c72010-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 Swiderski35387a02010-09-30 22:42:32 +0000153
154 operator bool() const {
155 return *this != const_iterator();
156 }
157
158 int distance(const_iterator L);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000159 };
160
161 friend class const_iterator;
162
163private:
Ted Kremenekfe59b742011-02-15 02:47:45 +0000164 BumpVectorContext ctx;
165
Marcin Swiderskif1308c72010-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 Kremenekfe59b742011-02-15 02:47:45 +0000174 LocalScope(BumpVectorContext &ctx, const_iterator P)
175 : ctx(ctx), Vars(ctx, 4), Prev(P) {}
Marcin Swiderskif1308c72010-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 Swiderski35387a02010-09-30 22:42:32 +0000179
180 void addVar(VarDecl* VD) {
Ted Kremenekfe59b742011-02-15 02:47:45 +0000181 Vars.push_back(VD, ctx);
Marcin Swiderski35387a02010-09-30 22:42:32 +0000182 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000183};
184
Marcin Swiderski35387a02010-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 Swiderskif1308c72010-09-25 11:05:21 +0000204struct BlockScopePosPair {
Ted Kremenek9ce52702011-01-07 19:37:16 +0000205 BlockScopePosPair() : block(0) {}
206 BlockScopePosPair(CFGBlock* b, LocalScope::const_iterator scopePos)
207 : block(b), scopePosition(scopePos) {}
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000208
Ted Kremenek9ce52702011-01-07 19:37:16 +0000209 CFGBlock *block;
210 LocalScope::const_iterator scopePosition;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000211};
212
Ted Kremeneke71f3d52011-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 Kremeneka34ea072008-08-04 22:51:42 +0000232/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-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 Stump6d9828c2009-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 Kremenekc310e932007-08-21 22:06:14 +0000245///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000246class CFGBuilder {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000247 typedef BlockScopePosPair JumpTarget;
248 typedef BlockScopePosPair JumpSource;
249
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000250 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000251 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000252
Ted Kremenekfddd5182007-08-21 21:42:03 +0000253 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000254 CFGBlock* Succ;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000255 JumpTarget ContinueJumpTarget;
256 JumpTarget BreakJumpTarget;
Ted Kremenekb5c13b02007-08-23 18:43:24 +0000257 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +0000258 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +0000259 CFGBlock* TryTerminatedBlock;
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000260
Marcin Swiderskif1308c72010-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 Lattnerad8dcf42011-02-17 07:39:24 +0000265 typedef llvm::DenseMap<LabelDecl*, JumpTarget> LabelMapTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000266 LabelMapTy LabelMap;
Mike Stump6d9828c2009-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 Swiderskif1308c72010-09-25 11:05:21 +0000270 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000271 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +0000272
Ted Kremenek19bb3562007-08-28 19:26:49 +0000273 // A list of labels whose address has been taken (for indirect gotos).
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000274 typedef llvm::SmallPtrSet<LabelDecl*, 5> LabelSetTy;
Ted Kremenek19bb3562007-08-28 19:26:49 +0000275 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +0000276
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000277 bool badCFG;
278 CFG::BuildOptions BuildOpts;
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000279
280 // State to track for building switch statements.
281 bool switchExclusivelyCovered;
282 Expr::EvalResult switchCond;
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000283
Mike Stump6d9828c2009-07-17 01:31:16 +0000284public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000285 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
286 Block(NULL), Succ(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +0000287 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000288 TryTerminatedBlock(NULL), badCFG(false),
289 switchExclusivelyCovered(false) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000290
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000291 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekad5a8942010-08-02 23:46:59 +0000292 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000293 CFG::BuildOptions BO);
Mike Stump6d9828c2009-07-17 01:31:16 +0000294
Ted Kremenek4f880632009-07-17 22:18:43 +0000295private:
296 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000297 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
298 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
299 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000300 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000301 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
John McCall4765fa02010-12-06 08:20:24 +0000302 CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +0000303 AddStmtChoice asc);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000304 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
305 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000306 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
307 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000308 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000309 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
310 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000311 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
312 AddStmtChoice asc);
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000313 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000314 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000315 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000316 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000317 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
John McCall56ca35d2011-02-17 10:25:35 +0000318 CFGBlock *VisitConditionalOperator(AbstractConditionalOperator *C,
319 AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000320 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000321 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000322 CFGBlock *VisitDeclSubExpr(DeclStmt* DS);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000323 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
324 CFGBlock *VisitDoStmt(DoStmt *D);
325 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000326 CFGBlock *VisitGotoStmt(GotoStmt* G);
327 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000328 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000329 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
330 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000331 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000332 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
333 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
334 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
335 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
336 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
337 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000338 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
339 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000340 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000341 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000342 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000343
Ted Kremenek852274d2009-12-16 03:18:58 +0000344 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
345 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000346 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000347
Marcin Swiderski8599e762010-11-03 06:19:35 +0000348 // Visitors to walk an AST and generate destructors of temporaries in
349 // full expression.
350 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
351 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
352 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
353 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
354 bool BindToTemporary);
John McCall56ca35d2011-02-17 10:25:35 +0000355 CFGBlock *
356 VisitConditionalOperatorForTemporaryDtors(AbstractConditionalOperator *E,
357 bool BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000358
Ted Kremenek274f4332008-04-28 18:00:46 +0000359 // NYS == Not Yet Supported
360 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000361 badCFG = true;
362 return Block;
363 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000364
Ted Kremenek4f880632009-07-17 22:18:43 +0000365 void autoCreateBlock() { if (!Block) Block = createBlock(); }
366 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000367
Zhongxing Xudf119892010-06-03 06:43:23 +0000368 CFGBlock *addStmt(Stmt *S) {
369 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000370 }
Sean Huntcbb67482011-01-08 20:30:50 +0000371 CFGBlock *addInitializer(CXXCtorInitializer *I);
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000372 void addAutomaticObjDtors(LocalScope::const_iterator B,
373 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000374 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekad5a8942010-08-02 23:46:59 +0000375
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000376 // Local scopes creation.
377 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
378
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000379 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000380 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
381 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
382
383 void addLocalScopeAndDtors(Stmt* S);
384
385 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek892697d2010-12-16 07:46:53 +0000386 void appendStmt(CFGBlock *B, Stmt *S,
Ted Kremenek852274d2009-12-16 03:18:58 +0000387 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000388 B->appendStmt(S, cfg->getBumpVectorContext());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000389 }
Sean Huntcbb67482011-01-08 20:30:50 +0000390 void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) {
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000391 B->appendInitializer(I, cfg->getBumpVectorContext());
392 }
Marcin Swiderski7c625d82010-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 Swiderski8599e762010-11-03 06:19:35 +0000399 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
400 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
401 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000402
Marcin Swiderski53de1342010-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 Kremenek0a3ed312010-12-17 04:44:39 +0000410 void addSuccessor(CFGBlock *B, CFGBlock *S) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000411 B->addSuccessor(S, cfg->getBumpVectorContext());
412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Ted Kremeneke71f3d52011-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 Stump1eb44332009-09-09 15:08:12 +0000422
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000423 /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
Mike Stump00998a02009-07-23 23:25:26 +0000424 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000425 TryResult tryEvaluateBool(Expr *S) {
Mike Stump00998a02009-07-23 23:25:26 +0000426 Expr::EvalResult Result;
Ted Kremeneke71f3d52011-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 Kremenekc56c0042011-02-23 05:11:46 +0000438 }
Ted Kremenek941fde82009-07-24 04:47:11 +0000439 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000440 }
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000441
Ted Kremenekfddd5182007-08-21 21:42:03 +0000442};
Mike Stump6d9828c2009-07-17 01:31:16 +0000443
Douglas Gregor898574e2008-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 McCallf4c73712011-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 Kremenek610a09e2008-09-26 22:58:57 +0000449 if (vat->getSizeExpr())
450 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000451
Ted Kremenek610a09e2008-09-26 22:58:57 +0000452 t = vt->getElementType().getTypePtr();
453 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000454
Ted Kremenek610a09e2008-09-26 22:58:57 +0000455 return 0;
456}
Mike Stump6d9828c2009-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.
Mike Stumpb978a442010-01-21 02:21:40 +0000463CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000464 CFG::BuildOptions BO) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000465
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000466 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000467 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000468 if (!Statement)
469 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000470
Ted Kremenek6c52c782010-09-14 23:41:16 +0000471 BuildOpts = BO;
Mike Stump6d9828c2009-07-17 01:31:16 +0000472
473 // Create an empty block that will serve as the exit block for the CFG. Since
474 // this is the first block added to the CFG, it will be implicitly registered
475 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000476 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000477 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000478 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000479
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000480 if (BuildOpts.AddImplicitDtors)
481 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
482 addImplicitDtorsForDestructor(DD);
483
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000484 // Visit the statements and create the CFG.
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000485 CFGBlock *B = addStmt(Statement);
486
487 if (badCFG)
488 return NULL;
489
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000490 // For C++ constructor add initializers to CFG.
491 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
492 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
493 E = CD->init_rend(); I != E; ++I) {
494 B = addInitializer(*I);
495 if (badCFG)
496 return NULL;
497 }
498 }
499
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000500 if (B)
501 Succ = B;
Mike Stumpb978a442010-01-21 02:21:40 +0000502
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000503 // Backpatch the gotos whose label -> block mappings we didn't know when we
504 // encountered them.
505 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
506 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000507
Ted Kremenek9ce52702011-01-07 19:37:16 +0000508 CFGBlock* B = I->block;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000509 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
510 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000511
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000512 // If there is no target for the goto, then we are looking at an
513 // incomplete AST. Handle this by not registering a successor.
514 if (LI == LabelMap.end()) continue;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000515
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000516 JumpTarget JT = LI->second;
Ted Kremenek9ce52702011-01-07 19:37:16 +0000517 prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
518 JT.scopePosition);
519 addSuccessor(B, JT.block);
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000520 }
521
522 // Add successors to the Indirect Goto Dispatch block (if we have one).
523 if (CFGBlock* B = cfg->getIndirectGotoBlock())
524 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
525 E = AddressTakenLabels.end(); I != E; ++I ) {
526
527 // Lookup the target block.
528 LabelMapTy::iterator LI = LabelMap.find(*I);
529
530 // If there is no target block that contains label, then we are looking
531 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000532 if (LI == LabelMap.end()) continue;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000533
Ted Kremenek9ce52702011-01-07 19:37:16 +0000534 addSuccessor(B, LI->second.block);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000535 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000536
Mike Stump6d9828c2009-07-17 01:31:16 +0000537 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000538 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000539
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000540 return cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000541}
Mike Stump6d9828c2009-07-17 01:31:16 +0000542
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000543/// createBlock - Used to lazily create blocks that are connected
544/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000545CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000546 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000547 if (add_successor && Succ)
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000548 addSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000549 return B;
550}
Mike Stump6d9828c2009-07-17 01:31:16 +0000551
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000552/// addInitializer - Add C++ base or member initializer element to CFG.
Sean Huntcbb67482011-01-08 20:30:50 +0000553CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) {
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000554 if (!BuildOpts.AddInitializers)
555 return Block;
556
Marcin Swiderski8599e762010-11-03 06:19:35 +0000557 bool IsReference = false;
558 bool HasTemporaries = false;
559
560 // Destructors of temporaries in initialization expression should be called
561 // after initialization finishes.
562 Expr *Init = I->getInit();
563 if (Init) {
Francois Pichet00eb3f92010-12-04 09:14:42 +0000564 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski8599e762010-11-03 06:19:35 +0000565 IsReference = FD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +0000566 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000567
568 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
569 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +0000570 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +0000571 IsReference);
572 }
573 }
574
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000575 autoCreateBlock();
576 appendInitializer(Block, I);
577
Marcin Swiderski8599e762010-11-03 06:19:35 +0000578 if (Init) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000579 if (HasTemporaries) {
Marcin Swiderski8599e762010-11-03 06:19:35 +0000580 // For expression with temporaries go directly to subexpression to omit
581 // generating destructors for the second time.
Ted Kremenek892697d2010-12-16 07:46:53 +0000582 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
583 }
584 return Visit(Init);
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000585 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000586
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000587 return Block;
588}
589
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000590/// addAutomaticObjDtors - Add to current block automatic objects destructors
591/// for objects in range of local scope positions. Use S as trigger statement
592/// for destructors.
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000593void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
594 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000595 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000596 return;
597
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000598 if (B == E)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000599 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000600
601 autoCreateBlock();
602 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000603}
604
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000605/// addImplicitDtorsForDestructor - Add implicit destructors generated for
606/// base and member objects in destructor.
607void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
608 assert (BuildOpts.AddImplicitDtors
609 && "Can be called only when dtors should be added");
610 const CXXRecordDecl *RD = DD->getParent();
611
612 // At the end destroy virtual base objects.
613 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
614 VE = RD->vbases_end(); VI != VE; ++VI) {
615 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
616 if (!CD->hasTrivialDestructor()) {
617 autoCreateBlock();
618 appendBaseDtor(Block, VI);
619 }
620 }
621
622 // Before virtual bases destroy direct base objects.
623 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
624 BE = RD->bases_end(); BI != BE; ++BI) {
625 if (!BI->isVirtual()) {
626 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
627 if (!CD->hasTrivialDestructor()) {
628 autoCreateBlock();
629 appendBaseDtor(Block, BI);
630 }
631 }
632 }
633
634 // First destroy member objects.
635 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
636 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +0000637 // Check for constant size array. Set type to array element type.
638 QualType QT = FI->getType();
639 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
640 if (AT->getSize() == 0)
641 continue;
642 QT = AT->getElementType();
643 }
644
645 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000646 if (!CD->hasTrivialDestructor()) {
647 autoCreateBlock();
648 appendMemberDtor(Block, *FI);
649 }
650 }
651}
652
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000653/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
654/// way return valid LocalScope object.
655LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
656 if (!Scope) {
Ted Kremenekfe59b742011-02-15 02:47:45 +0000657 llvm::BumpPtrAllocator &alloc = cfg->getAllocator();
658 Scope = alloc.Allocate<LocalScope>();
659 BumpVectorContext ctx(alloc);
660 new (Scope) LocalScope(ctx, ScopePos);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000661 }
662 return Scope;
663}
664
665/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000666/// that should create implicit scope (e.g. if/else substatements).
667void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000668 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000669 return;
670
671 LocalScope *Scope = 0;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000672
673 // For compound statement we will be creating explicit scope.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000674 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000675 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
676 ; BI != BE; ++BI) {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000677 Stmt *SI = *BI;
678 if (LabelStmt *LS = dyn_cast<LabelStmt>(SI))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000679 SI = LS->getSubStmt();
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000680 if (DeclStmt *DS = dyn_cast<DeclStmt>(SI))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000681 Scope = addLocalScopeForDeclStmt(DS, Scope);
682 }
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000683 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000684 }
685
686 // For any other statement scope will be implicit and as such will be
687 // interesting only for DeclStmt.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000688 if (LabelStmt *LS = dyn_cast<LabelStmt>(S))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000689 S = LS->getSubStmt();
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000690 if (DeclStmt *DS = dyn_cast<DeclStmt>(S))
Zhongxing Xub6edff52010-10-01 03:09:09 +0000691 addLocalScopeForDeclStmt(DS);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000692}
693
694/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
695/// reuse Scope if not NULL.
696LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000697 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000698 if (!BuildOpts.AddImplicitDtors)
699 return Scope;
700
701 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
702 ; DI != DE; ++DI) {
703 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
704 Scope = addLocalScopeForVarDecl(VD, Scope);
705 }
706 return Scope;
707}
708
709/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
710/// create add scope for automatic objects and temporary objects bound to
711/// const reference. Will reuse Scope if not NULL.
712LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000713 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000714 if (!BuildOpts.AddImplicitDtors)
715 return Scope;
716
717 // Check if variable is local.
718 switch (VD->getStorageClass()) {
719 case SC_None:
720 case SC_Auto:
721 case SC_Register:
722 break;
723 default: return Scope;
724 }
725
726 // Check for const references bound to temporary. Set type to pointee.
727 QualType QT = VD->getType();
728 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
729 QT = RT->getPointeeType();
730 if (!QT.isConstQualified())
731 return Scope;
732 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
733 return Scope;
734 }
735
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000736 // Check for constant size array. Set type to array element type.
737 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
738 if (AT->getSize() == 0)
739 return Scope;
740 QT = AT->getElementType();
741 }
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000742
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000743 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000744 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
745 if (!CD->hasTrivialDestructor()) {
746 // Add the variable to scope
747 Scope = createOrReuseLocalScope(Scope);
748 Scope->addVar(VD);
749 ScopePos = Scope->begin();
750 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000751 return Scope;
752}
753
754/// addLocalScopeAndDtors - For given statement add local scope for it and
755/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
756void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
757 if (!BuildOpts.AddImplicitDtors)
758 return;
759
760 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000761 addLocalScopeForStmt(S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000762 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
763}
764
Marcin Swiderski53de1342010-09-30 22:54:37 +0000765/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
766/// automatic storage duration to CFGBlock's elements vector. Insertion will be
767/// performed in place specified with iterator.
768void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
769 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
770 BumpVectorContext& C = cfg->getBumpVectorContext();
771 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
772 while (B != E)
773 I = Blk->insertAutomaticObjDtor(I, *B++, S);
774}
775
776/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
777/// automatic storage duration to CFGBlock's elements vector. Elements will be
778/// appended to physical end of the vector which happens to be logical
779/// beginning.
780void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
781 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
782 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
783}
784
785/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
786/// variables with automatic storage duration to CFGBlock's elements vector.
787/// Elements will be prepended to physical beginning of the vector which
788/// happens to be logical end. Use blocks terminator as statement that specifies
789/// destructors call site.
790void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
791 LocalScope::const_iterator B, LocalScope::const_iterator E) {
792 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
793}
794
Ted Kremenek4f880632009-07-17 22:18:43 +0000795/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000796/// blocks for ternary operators, &&, and ||. We also process "," and
797/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000798CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000799tryAgain:
Ted Kremenekf42e3372010-04-30 22:25:53 +0000800 if (!S) {
801 badCFG = true;
802 return 0;
803 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000804 switch (S->getStmtClass()) {
805 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000806 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000807
808 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000809 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000810
John McCall56ca35d2011-02-17 10:25:35 +0000811 case Stmt::BinaryConditionalOperatorClass:
812 return VisitConditionalOperator(cast<BinaryConditionalOperator>(S), asc);
813
Ted Kremenek4f880632009-07-17 22:18:43 +0000814 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000815 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Ted Kremenek4f880632009-07-17 22:18:43 +0000817 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000818 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000819
Ted Kremenek4f880632009-07-17 22:18:43 +0000820 case Stmt::BreakStmtClass:
821 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Ted Kremenek4f880632009-07-17 22:18:43 +0000823 case Stmt::CallExprClass:
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000824 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000825 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenek4f880632009-07-17 22:18:43 +0000827 case Stmt::CaseStmtClass:
828 return VisitCaseStmt(cast<CaseStmt>(S));
829
830 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000831 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Ted Kremenek4f880632009-07-17 22:18:43 +0000833 case Stmt::CompoundStmtClass:
834 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Ted Kremenek4f880632009-07-17 22:18:43 +0000836 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000837 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenek4f880632009-07-17 22:18:43 +0000839 case Stmt::ContinueStmtClass:
840 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Ted Kremenek021c8af2010-01-19 20:40:33 +0000842 case Stmt::CXXCatchStmtClass:
843 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
844
John McCall4765fa02010-12-06 08:20:24 +0000845 case Stmt::ExprWithCleanupsClass:
846 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +0000847
Zhongxing Xua725ed42010-11-01 13:04:58 +0000848 case Stmt::CXXBindTemporaryExprClass:
849 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
850
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000851 case Stmt::CXXConstructExprClass:
852 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
853
Zhongxing Xua725ed42010-11-01 13:04:58 +0000854 case Stmt::CXXFunctionalCastExprClass:
855 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
856
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000857 case Stmt::CXXTemporaryObjectExprClass:
858 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
859
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000860 case Stmt::CXXMemberCallExprClass:
861 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
862
Ted Kremenek021c8af2010-01-19 20:40:33 +0000863 case Stmt::CXXThrowExprClass:
864 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000865
Ted Kremenek021c8af2010-01-19 20:40:33 +0000866 case Stmt::CXXTryStmtClass:
867 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000868
Ted Kremenek4f880632009-07-17 22:18:43 +0000869 case Stmt::DeclStmtClass:
870 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Ted Kremenek4f880632009-07-17 22:18:43 +0000872 case Stmt::DefaultStmtClass:
873 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Ted Kremenek4f880632009-07-17 22:18:43 +0000875 case Stmt::DoStmtClass:
876 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Ted Kremenek4f880632009-07-17 22:18:43 +0000878 case Stmt::ForStmtClass:
879 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenek4f880632009-07-17 22:18:43 +0000881 case Stmt::GotoStmtClass:
882 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Ted Kremenek4f880632009-07-17 22:18:43 +0000884 case Stmt::IfStmtClass:
885 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Ted Kremenek892697d2010-12-16 07:46:53 +0000887 case Stmt::ImplicitCastExprClass:
888 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000889
Ted Kremenek4f880632009-07-17 22:18:43 +0000890 case Stmt::IndirectGotoStmtClass:
891 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Ted Kremenek4f880632009-07-17 22:18:43 +0000893 case Stmt::LabelStmtClass:
894 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Ted Kremenek115c1b92010-04-11 17:02:10 +0000896 case Stmt::MemberExprClass:
897 return VisitMemberExpr(cast<MemberExpr>(S), asc);
898
Ted Kremenek4f880632009-07-17 22:18:43 +0000899 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000900 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
901
Ted Kremenek4f880632009-07-17 22:18:43 +0000902 case Stmt::ObjCAtSynchronizedStmtClass:
903 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Ted Kremenek4f880632009-07-17 22:18:43 +0000905 case Stmt::ObjCAtThrowStmtClass:
906 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Ted Kremenek4f880632009-07-17 22:18:43 +0000908 case Stmt::ObjCAtTryStmtClass:
909 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Ted Kremenek4f880632009-07-17 22:18:43 +0000911 case Stmt::ObjCForCollectionStmtClass:
912 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Ted Kremenek4f880632009-07-17 22:18:43 +0000914 case Stmt::ParenExprClass:
915 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000916 goto tryAgain;
917
Ted Kremenek4f880632009-07-17 22:18:43 +0000918 case Stmt::NullStmtClass:
919 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Ted Kremenek4f880632009-07-17 22:18:43 +0000921 case Stmt::ReturnStmtClass:
922 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Ted Kremenek4f880632009-07-17 22:18:43 +0000924 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000925 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Ted Kremenek4f880632009-07-17 22:18:43 +0000927 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000928 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Ted Kremenek4f880632009-07-17 22:18:43 +0000930 case Stmt::SwitchStmtClass:
931 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000933 case Stmt::UnaryOperatorClass:
934 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
935
Ted Kremenek4f880632009-07-17 22:18:43 +0000936 case Stmt::WhileStmtClass:
937 return VisitWhileStmt(cast<WhileStmt>(S));
938 }
939}
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Ted Kremenek852274d2009-12-16 03:18:58 +0000941CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
942 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000943 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000944 appendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000945 }
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Ted Kremenek4f880632009-07-17 22:18:43 +0000947 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000948}
Mike Stump6d9828c2009-07-17 01:31:16 +0000949
Ted Kremenek4f880632009-07-17 22:18:43 +0000950/// VisitChildren - Visit the children of a Stmt.
951CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
Ted Kremenek6b12da92011-02-21 22:11:26 +0000952 CFGBlock *lastBlock = Block;
953 for (Stmt::child_range I = Terminator->children(); I; ++I)
954 if (Stmt *child = *I)
955 if (CFGBlock *b = Visit(child))
956 lastBlock = b;
957
958 return lastBlock;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000959}
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Ted Kremenek852274d2009-12-16 03:18:58 +0000961CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
962 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000963 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000964
Ted Kremenek852274d2009-12-16 03:18:58 +0000965 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000966 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000967 appendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000968 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000969
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000970 return Block;
971}
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000973CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek892697d2010-12-16 07:46:53 +0000974 AddStmtChoice asc) {
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000975 if (asc.alwaysAdd()) {
976 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000977 appendStmt(Block, U, asc);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000978 }
979
Ted Kremenek892697d2010-12-16 07:46:53 +0000980 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000981}
982
Ted Kremenek852274d2009-12-16 03:18:58 +0000983CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
984 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000985 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000986 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000987 appendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000989 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +0000990 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Ted Kremenek4f880632009-07-17 22:18:43 +0000992 // create the block evaluating the LHS
993 CFGBlock* LHSBlock = createBlock(false);
994 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Ted Kremenek4f880632009-07-17 22:18:43 +0000996 // create the block evaluating the RHS
997 Succ = ConfluenceBlock;
998 Block = NULL;
999 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +00001000
1001 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001002 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +00001003 return 0;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001004 } else {
Ted Kremenek862b24f2010-04-29 01:10:26 +00001005 // Create an empty block for cases where the RHS doesn't require
1006 // any explicit statements in the CFG.
1007 RHSBlock = createBlock();
1008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Mike Stump00998a02009-07-23 23:25:26 +00001010 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001011 TryResult KnownVal = tryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +00001012 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +00001013 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +00001014
Ted Kremenek4f880632009-07-17 22:18:43 +00001015 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +00001016 if (B->getOpcode() == BO_LOr) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001017 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
1018 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001019 } else {
John McCall2de56d12010-08-25 11:45:40 +00001020 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001021 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
1022 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremenek4f880632009-07-17 22:18:43 +00001025 // Generate the blocks for evaluating the LHS.
1026 Block = LHSBlock;
1027 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +00001028 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001029
1030 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +00001031 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001032 appendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +00001033 addStmt(B->getRHS());
1034 return addStmt(B->getLHS());
1035 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001036
1037 if (B->isAssignmentOp()) {
Zhongxing Xufc61d942010-06-03 06:23:18 +00001038 if (asc.alwaysAdd()) {
1039 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001040 appendStmt(Block, B, asc);
Zhongxing Xufc61d942010-06-03 06:23:18 +00001041 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001042 Visit(B->getLHS());
Marcin Swiderskie1667192010-10-24 08:21:40 +00001043 return Visit(B->getRHS());
Zhongxing Xufc61d942010-06-03 06:23:18 +00001044 }
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Marcin Swiderskie1667192010-10-24 08:21:40 +00001046 if (asc.alwaysAdd()) {
1047 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001048 appendStmt(Block, B, asc);
Marcin Swiderskie1667192010-10-24 08:21:40 +00001049 }
1050
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001051 CFGBlock *RBlock = Visit(B->getRHS());
1052 CFGBlock *LBlock = Visit(B->getLHS());
1053 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1054 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1055 // return RBlock. Otherwise we'll incorrectly return NULL.
1056 return (LBlock ? LBlock : RBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001057}
1058
Ted Kremenek852274d2009-12-16 03:18:58 +00001059CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1060 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +00001061 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001062 appendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +00001063 }
1064 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001065}
1066
Ted Kremenek4f880632009-07-17 22:18:43 +00001067CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1068 // "break" is a control-flow statement. Thus we stop processing the current
1069 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001070 if (badCFG)
1071 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Ted Kremenek4f880632009-07-17 22:18:43 +00001073 // Now create a new block that ends with the break statement.
1074 Block = createBlock(false);
1075 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Ted Kremenek4f880632009-07-17 22:18:43 +00001077 // If there is no target for the break, then we are looking at an incomplete
1078 // AST. This means that the CFG cannot be constructed.
Ted Kremenek9ce52702011-01-07 19:37:16 +00001079 if (BreakJumpTarget.block) {
1080 addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B);
1081 addSuccessor(Block, BreakJumpTarget.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001082 } else
Ted Kremenek4f880632009-07-17 22:18:43 +00001083 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001084
1085
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001086 return Block;
1087}
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Mike Stump4c45aa12010-01-21 15:20:48 +00001089static bool CanThrow(Expr *E) {
1090 QualType Ty = E->getType();
1091 if (Ty->isFunctionPointerType())
1092 Ty = Ty->getAs<PointerType>()->getPointeeType();
1093 else if (Ty->isBlockPointerType())
1094 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +00001095
Mike Stump4c45aa12010-01-21 15:20:48 +00001096 const FunctionType *FT = Ty->getAs<FunctionType>();
1097 if (FT) {
1098 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1099 if (Proto->hasEmptyExceptionSpec())
1100 return false;
1101 }
1102 return true;
1103}
1104
Ted Kremenek852274d2009-12-16 03:18:58 +00001105CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001106 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +00001107 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +00001108 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +00001109 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +00001110 }
Mike Stump24556362009-07-25 21:26:53 +00001111
Mike Stump4c45aa12010-01-21 15:20:48 +00001112 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001113
1114 // Languages without exceptions are assumed to not throw.
Anders Carlsson7a178512011-02-28 00:33:03 +00001115 if (Context->getLangOptions().Exceptions) {
Ted Kremenek6c52c782010-09-14 23:41:16 +00001116 if (BuildOpts.AddEHEdges)
Mike Stump4c45aa12010-01-21 15:20:48 +00001117 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +00001118 }
1119
1120 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +00001121 if (FD->hasAttr<NoReturnAttr>())
1122 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +00001123 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +00001124 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001125 }
Mike Stump24556362009-07-25 21:26:53 +00001126
Mike Stump4c45aa12010-01-21 15:20:48 +00001127 if (!CanThrow(C->getCallee()))
1128 AddEHEdge = false;
1129
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001130 if (!NoReturn && !AddEHEdge)
1131 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Mike Stump079bd722010-01-19 22:00:14 +00001133 if (Block) {
1134 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001135 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +00001136 return 0;
1137 }
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Mike Stump079bd722010-01-19 22:00:14 +00001139 Block = createBlock(!NoReturn);
Ted Kremenek892697d2010-12-16 07:46:53 +00001140 appendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +00001141
Mike Stump079bd722010-01-19 22:00:14 +00001142 if (NoReturn) {
1143 // Wire this to the exit block directly.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001144 addSuccessor(Block, &cfg->getExit());
Mike Stump079bd722010-01-19 22:00:14 +00001145 }
Mike Stump4c45aa12010-01-21 15:20:48 +00001146 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +00001147 // Add exceptional edges.
1148 if (TryTerminatedBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001149 addSuccessor(Block, TryTerminatedBlock);
Mike Stump079bd722010-01-19 22:00:14 +00001150 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001151 addSuccessor(Block, &cfg->getExit());
Mike Stump079bd722010-01-19 22:00:14 +00001152 }
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Mike Stump24556362009-07-25 21:26:53 +00001154 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +00001155}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001156
Ted Kremenek852274d2009-12-16 03:18:58 +00001157CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1158 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001159 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001160 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001161 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001162 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001164 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001165 Succ = ConfluenceBlock;
1166 Block = NULL;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001167 CFGBlock* LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001168 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001169 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001171 Succ = ConfluenceBlock;
1172 Block = NULL;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001173 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001174 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001175 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001177 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +00001178 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001179 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
1180 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1181 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001182 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001183 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001184}
Mike Stump1eb44332009-09-09 15:08:12 +00001185
1186
1187CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001188 addLocalScopeAndDtors(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001189 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001190
1191 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1192 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +00001193 // If we hit a segment of code just containing ';' (NullStmts), we can
1194 // get a null block back. In such cases, just use the LastBlock
1195 if (CFGBlock *newBlock = addStmt(*I))
1196 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +00001198 if (badCFG)
1199 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001200 }
Mike Stump079bd722010-01-19 22:00:14 +00001201
Ted Kremenek4f880632009-07-17 22:18:43 +00001202 return LastBlock;
1203}
Mike Stump1eb44332009-09-09 15:08:12 +00001204
John McCall56ca35d2011-02-17 10:25:35 +00001205CFGBlock *CFGBuilder::VisitConditionalOperator(AbstractConditionalOperator *C,
Ted Kremenek852274d2009-12-16 03:18:58 +00001206 AddStmtChoice asc) {
John McCall56ca35d2011-02-17 10:25:35 +00001207 const BinaryConditionalOperator *BCO = dyn_cast<BinaryConditionalOperator>(C);
1208 const OpaqueValueExpr *opaqueValue = (BCO ? BCO->getOpaqueValue() : NULL);
1209
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001210 // Create the confluence block that will "merge" the results of the ternary
1211 // expression.
1212 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001213 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001214 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001215 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001217 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001218
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001219 // Create a block for the LHS expression if there is an LHS expression. A
1220 // GCC extension allows LHS to be NULL, causing the condition to be the
1221 // value that is returned instead.
1222 // e.g: x ?: y is shorthand for: x ? x : y;
1223 Succ = ConfluenceBlock;
1224 Block = NULL;
John McCall56ca35d2011-02-17 10:25:35 +00001225 CFGBlock* LHSBlock = 0;
1226 const Expr *trueExpr = C->getTrueExpr();
1227 if (trueExpr != opaqueValue) {
1228 LHSBlock = Visit(C->getTrueExpr(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001229 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001230 return 0;
1231 Block = NULL;
1232 }
Ted Kremenekf226d182011-02-24 03:09:15 +00001233 else
1234 LHSBlock = ConfluenceBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001236 // Create the block for the RHS expression.
1237 Succ = ConfluenceBlock;
John McCall56ca35d2011-02-17 10:25:35 +00001238 CFGBlock* RHSBlock = Visit(C->getFalseExpr(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001239 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001240 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001242 // Create the block that will contain the condition.
1243 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Mike Stump00998a02009-07-23 23:25:26 +00001245 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001246 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
Ted Kremenekf226d182011-02-24 03:09:15 +00001247 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001248 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001249 Block->setTerminator(C);
John McCall56ca35d2011-02-17 10:25:35 +00001250 Expr *condExpr = C->getCond();
John McCalld40baf62011-02-19 03:13:26 +00001251
Ted Kremenekf226d182011-02-24 03:09:15 +00001252 if (opaqueValue) {
1253 // Run the condition expression if it's not trivially expressed in
1254 // terms of the opaque value (or if there is no opaque value).
1255 if (condExpr != opaqueValue)
1256 addStmt(condExpr);
John McCalld40baf62011-02-19 03:13:26 +00001257
Ted Kremenekf226d182011-02-24 03:09:15 +00001258 // Before that, run the common subexpression if there was one.
1259 // At least one of this or the above will be run.
1260 return addStmt(BCO->getCommon());
1261 }
1262
1263 return addStmt(condExpr);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001264}
1265
Ted Kremenek4f880632009-07-17 22:18:43 +00001266CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001267 if (DS->isSingleDecl())
1268 return VisitDeclSubExpr(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Ted Kremenek4f880632009-07-17 22:18:43 +00001270 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Ted Kremenek4f880632009-07-17 22:18:43 +00001272 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1273 typedef llvm::SmallVector<Decl*,10> BufTy;
1274 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Ted Kremenek4f880632009-07-17 22:18:43 +00001276 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1277 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1278 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1279 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Ted Kremenek4f880632009-07-17 22:18:43 +00001281 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1282 // automatically freed with the CFG.
1283 DeclGroupRef DG(*I);
1284 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001285 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001286 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Ted Kremenek4f880632009-07-17 22:18:43 +00001288 // Append the fake DeclStmt to block.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001289 B = VisitDeclSubExpr(DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +00001290 }
Mike Stump1eb44332009-09-09 15:08:12 +00001291
1292 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +00001293}
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Ted Kremenek4f880632009-07-17 22:18:43 +00001295/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski8599e762010-11-03 06:19:35 +00001296/// DeclStmts and initializers in them.
1297CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1298 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekd34066c2008-02-26 00:22:58 +00001299
Marcin Swiderski8599e762010-11-03 06:19:35 +00001300 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Marcin Swiderski8599e762010-11-03 06:19:35 +00001302 if (!VD) {
1303 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001304 appendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +00001305 return Block;
Marcin Swiderski8599e762010-11-03 06:19:35 +00001306 }
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Marcin Swiderski8599e762010-11-03 06:19:35 +00001308 bool IsReference = false;
1309 bool HasTemporaries = false;
1310
1311 // Destructors of temporaries in initialization expression should be called
1312 // after initialization finishes.
Ted Kremenek4f880632009-07-17 22:18:43 +00001313 Expr *Init = VD->getInit();
Marcin Swiderski8599e762010-11-03 06:19:35 +00001314 if (Init) {
1315 IsReference = VD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +00001316 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +00001317
1318 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1319 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +00001320 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +00001321 IsReference);
1322 }
1323 }
1324
1325 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001326 appendStmt(Block, DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Ted Kremenek4f880632009-07-17 22:18:43 +00001328 if (Init) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001329 if (HasTemporaries)
1330 // For expression with temporaries go directly to subexpression to omit
1331 // generating destructors for the second time.
Ted Kremenek892697d2010-12-16 07:46:53 +00001332 Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
Marcin Swiderski8599e762010-11-03 06:19:35 +00001333 else
Ted Kremenek892697d2010-12-16 07:46:53 +00001334 Visit(Init);
Ted Kremenek4f880632009-07-17 22:18:43 +00001335 }
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Ted Kremenek4f880632009-07-17 22:18:43 +00001337 // If the type of VD is a VLA, then we must process its size expressions.
John McCallf4c73712011-01-19 06:33:43 +00001338 for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr());
1339 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek4f880632009-07-17 22:18:43 +00001340 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001342 // Remove variable from local scope.
1343 if (ScopePos && VD == *ScopePos)
1344 ++ScopePos;
1345
Ted Kremenek4f880632009-07-17 22:18:43 +00001346 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001347}
1348
1349CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001350 // We may see an if statement in the middle of a basic block, or it may be the
1351 // first statement we are processing. In either case, we create a new basic
1352 // block. First, we create the blocks for the then...else statements, and
1353 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +00001354 // middle of a block, we stop processing that block. That block is then the
1355 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +00001356
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001357 // Save local scope position because in case of condition variable ScopePos
1358 // won't be restored when traversing AST.
1359 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1360
1361 // Create local scope for possible condition variable.
1362 // Store scope position. Add implicit destructor.
1363 if (VarDecl* VD = I->getConditionVariable()) {
1364 LocalScope::const_iterator BeginScopePos = ScopePos;
1365 addLocalScopeForVarDecl(VD);
1366 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1367 }
1368
Mike Stump6d9828c2009-07-17 01:31:16 +00001369 // The block we were proccessing is now finished. Make it the successor
1370 // block.
1371 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001372 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001373 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001374 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001375 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001376
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001377 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001378 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001379
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001380 if (Stmt* Else = I->getElse()) {
1381 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001382
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001383 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +00001384 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001385 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001386
1387 // If branch is not a compound statement create implicit scope
1388 // and add destructors.
1389 if (!isa<CompoundStmt>(Else))
1390 addLocalScopeAndDtors(Else);
1391
Ted Kremenek4f880632009-07-17 22:18:43 +00001392 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +00001393
Ted Kremenekb6f7b722007-08-30 18:13:31 +00001394 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1395 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001396 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001397 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001398 return 0;
1399 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001400 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001401
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001402 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001403 CFGBlock* ThenBlock;
1404 {
1405 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001406 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001407 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001408 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001409
1410 // If branch is not a compound statement create implicit scope
1411 // and add destructors.
1412 if (!isa<CompoundStmt>(Then))
1413 addLocalScopeAndDtors(Then);
1414
Ted Kremenek4f880632009-07-17 22:18:43 +00001415 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +00001416
Ted Kremenekdbdf7942009-04-01 03:52:47 +00001417 if (!ThenBlock) {
1418 // We can reach here if the "then" body has all NullStmts.
1419 // Create an empty block so we can distinguish between true and false
1420 // branches in path-sensitive analyses.
1421 ThenBlock = createBlock(false);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001422 addSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +00001423 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001424 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001425 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001426 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001427 }
1428
Mike Stump6d9828c2009-07-17 01:31:16 +00001429 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001430 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001431
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001432 // Set the terminator of the new block to the If statement.
1433 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001434
Mike Stump00998a02009-07-23 23:25:26 +00001435 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001436 const TryResult &KnownVal = tryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001437
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001438 // Now add the successors.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001439 addSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1440 addSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001441
1442 // Add the condition as the last statement in the new block. This may create
1443 // new blocks as the condition may contain control-flow. Any newly created
1444 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001445 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001446
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001447 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1448 // and the condition variable initialization to the CFG.
1449 if (VarDecl *VD = I->getConditionVariable()) {
1450 if (Expr *Init = VD->getInit()) {
1451 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001452 appendStmt(Block, I, AddStmtChoice::AlwaysAdd);
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001453 addStmt(Init);
1454 }
1455 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001456
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001457 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001458}
Mike Stump6d9828c2009-07-17 01:31:16 +00001459
1460
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001461CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001462 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001463 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001464 // NOTE: If a "return" appears in the middle of a block, this means that the
1465 // code afterwards is DEAD (unreachable). We still keep a basic block
1466 // for that code; a simple "mark-and-sweep" from the entry block will be
1467 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001468
1469 // Create the new block.
1470 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001471
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001472 // The Exit block is the only successor.
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001473 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001474 addSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001475
1476 // Add the return statement to the block. This may create new blocks if R
1477 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001478 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001479}
1480
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001481CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt *L) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001482 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +00001483 addStmt(L->getSubStmt());
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001484 CFGBlock *LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001485
Ted Kremenek4f880632009-07-17 22:18:43 +00001486 if (!LabelBlock) // This can happen when the body is empty, i.e.
1487 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +00001488
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001489 assert(LabelMap.find(L->getDecl()) == LabelMap.end() &&
1490 "label already in map");
1491 LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001492
1493 // Labels partition blocks, so this is the end of the basic block we were
1494 // processing (L is the block's label). Because this is label (and we have
1495 // already processed the substatement) there is no extra control-flow to worry
1496 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +00001497 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001498 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001499 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001500
1501 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001502 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001503
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001504 // This block is now the implicit successor of other blocks.
1505 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001506
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001507 return LabelBlock;
1508}
1509
1510CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001511 // Goto is a control-flow statement. Thus we stop processing the current
1512 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001513
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001514 Block = createBlock(false);
1515 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001516
1517 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001518 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001519
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001520 if (I == LabelMap.end())
1521 // We will need to backpatch this block later.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001522 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1523 else {
1524 JumpTarget JT = I->second;
Ted Kremenek9ce52702011-01-07 19:37:16 +00001525 addAutomaticObjDtors(ScopePos, JT.scopePosition, G);
1526 addSuccessor(Block, JT.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001527 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001528
Mike Stump6d9828c2009-07-17 01:31:16 +00001529 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001530}
1531
1532CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001533 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001534
Marcin Swiderski47575f12010-10-01 01:38:14 +00001535 // Save local scope position because in case of condition variable ScopePos
1536 // won't be restored when traversing AST.
1537 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1538
1539 // Create local scope for init statement and possible condition variable.
1540 // Add destructor for init statement and condition variable.
1541 // Store scope position for continue statement.
1542 if (Stmt* Init = F->getInit())
1543 addLocalScopeForStmt(Init);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001544 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1545
Marcin Swiderski47575f12010-10-01 01:38:14 +00001546 if (VarDecl* VD = F->getConditionVariable())
1547 addLocalScopeForVarDecl(VD);
1548 LocalScope::const_iterator ContinueScopePos = ScopePos;
1549
1550 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1551
Mike Stumpfefb9f72009-07-21 01:12:51 +00001552 // "for" is a control-flow statement. Thus we stop processing the current
1553 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001554 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001555 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001556 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001557 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001558 } else
1559 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001560
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001561 // Save the current value for the break targets.
1562 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001563 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001564 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001565
Mike Stump6d9828c2009-07-17 01:31:16 +00001566 // Because of short-circuit evaluation, the condition of the loop can span
1567 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1568 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001569 CFGBlock* ExitConditionBlock = createBlock(false);
1570 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001571
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001572 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001573 ExitConditionBlock->setTerminator(F);
1574
1575 // Now add the actual condition to the condition block. Because the condition
1576 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001577 if (Stmt* C = F->getCond()) {
1578 Block = ExitConditionBlock;
1579 EntryConditionBlock = addStmt(C);
Ted Kremenek9ce52702011-01-07 19:37:16 +00001580 if (badCFG)
1581 return 0;
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001582 assert(Block == EntryConditionBlock ||
1583 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001584
1585 // If this block contains a condition variable, add both the condition
1586 // variable and initializer to the CFG.
1587 if (VarDecl *VD = F->getConditionVariable()) {
1588 if (Expr *Init = VD->getInit()) {
1589 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001590 appendStmt(Block, F, AddStmtChoice::AlwaysAdd);
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001591 EntryConditionBlock = addStmt(Init);
1592 assert(Block == EntryConditionBlock);
1593 }
1594 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001595
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001596 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001597 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001598 return 0;
1599 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001600 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001601
Mike Stump6d9828c2009-07-17 01:31:16 +00001602 // The condition block is the implicit successor for the loop body as well as
1603 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001604 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001605
Mike Stump00998a02009-07-23 23:25:26 +00001606 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001607 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Mike Stump00998a02009-07-23 23:25:26 +00001609 if (F->getCond())
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001610 KnownVal = tryEvaluateBool(F->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001611
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001612 // Now create the loop body.
1613 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001614 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001615
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001616 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001617 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1618 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001619
Ted Kremenekaf603f72007-08-30 18:39:40 +00001620 // Create a new block to contain the (bottom) of the loop body.
1621 Block = NULL;
Marcin Swiderski47575f12010-10-01 01:38:14 +00001622
1623 // Loop body should end with destructor of Condition variable (if any).
1624 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump6d9828c2009-07-17 01:31:16 +00001625
Ted Kremeneke9334502008-09-04 21:48:47 +00001626 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001627 // Generate increment code in its own basic block. This is the target of
1628 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001629 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001630 } else {
1631 // No increment code. Create a special, empty, block that is used as the
1632 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001633 assert(Succ == EntryConditionBlock);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001634 Succ = Block ? Block : createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001635 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001636
Ted Kremenek3575f842009-04-28 00:51:56 +00001637 // Finish up the increment (or empty) block if it hasn't been already.
1638 if (Block) {
1639 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001640 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001641 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001642 Block = 0;
1643 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001644
Marcin Swiderski47575f12010-10-01 01:38:14 +00001645 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001646
Ted Kremenek3575f842009-04-28 00:51:56 +00001647 // The starting block for the loop increment is the block that should
1648 // represent the 'loop target' for looping back to the start of the loop.
Ted Kremenek9ce52702011-01-07 19:37:16 +00001649 ContinueJumpTarget.block->setLoopTarget(F);
Ted Kremenek3575f842009-04-28 00:51:56 +00001650
Marcin Swiderski47575f12010-10-01 01:38:14 +00001651 // If body is not a compound statement create implicit scope
1652 // and add destructors.
1653 if (!isa<CompoundStmt>(F->getBody()))
1654 addLocalScopeAndDtors(F->getBody());
1655
Mike Stump6d9828c2009-07-17 01:31:16 +00001656 // Now populate the body block, and in the process create new blocks as we
1657 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001658 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001659
1660 if (!BodyBlock)
Ted Kremenek9ce52702011-01-07 19:37:16 +00001661 BodyBlock = ContinueJumpTarget.block;//can happen for "for (...;...;...);"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001662 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001663 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001664
Ted Kremenek941fde82009-07-24 04:47:11 +00001665 // This new body block is a successor to our "exit" condition block.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001666 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001667 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001668
Ted Kremenek941fde82009-07-24 04:47:11 +00001669 // Link up the condition block with the code that follows the loop. (the
1670 // false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001671 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001672
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001673 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001674 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001675 if (Stmt* I = F->getInit()) {
1676 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001677 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001678 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001679
1680 // There is no loop initialization. We are thus basically a while loop.
1681 // NULL out Block to force lazy block construction.
1682 Block = NULL;
1683 Succ = EntryConditionBlock;
1684 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001685}
1686
Ted Kremenek115c1b92010-04-11 17:02:10 +00001687CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1688 if (asc.alwaysAdd()) {
1689 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001690 appendStmt(Block, M, asc);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001691 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001692 return Visit(M->getBase());
Ted Kremenek115c1b92010-04-11 17:02:10 +00001693}
1694
Ted Kremenek514de5a2008-11-11 17:10:00 +00001695CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1696 // Objective-C fast enumeration 'for' statements:
1697 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1698 //
1699 // for ( Type newVariable in collection_expression ) { statements }
1700 //
1701 // becomes:
1702 //
1703 // prologue:
1704 // 1. collection_expression
1705 // T. jump to loop_entry
1706 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001707 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001708 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1709 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1710 // TB:
1711 // statements
1712 // T. jump to loop_entry
1713 // FB:
1714 // what comes after
1715 //
1716 // and
1717 //
1718 // Type existingItem;
1719 // for ( existingItem in expression ) { statements }
1720 //
1721 // becomes:
1722 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001723 // the same with newVariable replaced with existingItem; the binding works
1724 // the same except that for one ObjCForCollectionStmt::getElement() returns
1725 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001726 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001727
Ted Kremenek514de5a2008-11-11 17:10:00 +00001728 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001729
Ted Kremenek514de5a2008-11-11 17:10:00 +00001730 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001731 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001732 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001733 LoopSuccessor = Block;
1734 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001735 } else
1736 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001737
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001738 // Build the condition blocks.
1739 CFGBlock* ExitConditionBlock = createBlock(false);
1740 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001741
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001742 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001743 ExitConditionBlock->setTerminator(S);
1744
1745 // The last statement in the block should be the ObjCForCollectionStmt, which
1746 // performs the actual binding to 'element' and determines if there are any
1747 // more items in the collection.
Ted Kremenek892697d2010-12-16 07:46:53 +00001748 appendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001749 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001750
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001751 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001752 // generate new blocks as necesary. We DON'T add the statement by default to
1753 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001754 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001755 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001756 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001757 return 0;
1758 Block = 0;
1759 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001760
1761 // The condition block is the implicit successor for the loop body as well as
1762 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001763 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001764
Ted Kremenek514de5a2008-11-11 17:10:00 +00001765 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001766 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001767 // Save the current values for Succ, continue and break targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001768 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1769 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1770 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001771
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001772 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1773 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001774
Ted Kremenek4f880632009-07-17 22:18:43 +00001775 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001776
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001777 if (!BodyBlock)
1778 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001779 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001780 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001781 return 0;
1782 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001783
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001784 // This new body block is a successor to our "exit" condition block.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001785 addSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001786 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001787
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001788 // Link up the condition block with the code that follows the loop.
1789 // (the false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001790 addSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001791
Ted Kremenek514de5a2008-11-11 17:10:00 +00001792 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001793 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001794 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001795}
1796
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001797CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1798 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001799
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001800 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001801 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001802
Ted Kremenekda5348e2009-05-05 23:11:51 +00001803 // The sync body starts its own basic block. This makes it a little easier
1804 // for diagnostic clients.
1805 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001806 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00001807 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001808
Ted Kremenekda5348e2009-05-05 23:11:51 +00001809 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001810 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001811 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001812
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001813 // Add the @synchronized to the CFG.
1814 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001815 appendStmt(Block, S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001816
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001817 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001818 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001819}
Mike Stump6d9828c2009-07-17 01:31:16 +00001820
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001821CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001822 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001823 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001824}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001825
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001826CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001827 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001828
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001829 // Save local scope position because in case of condition variable ScopePos
1830 // won't be restored when traversing AST.
1831 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1832
1833 // Create local scope for possible condition variable.
1834 // Store scope position for continue statement.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001835 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001836 if (VarDecl* VD = W->getConditionVariable()) {
1837 addLocalScopeForVarDecl(VD);
1838 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1839 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001840
Mike Stumpfefb9f72009-07-21 01:12:51 +00001841 // "while" is a control-flow statement. Thus we stop processing the current
1842 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001843 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001844 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001845 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001846 LoopSuccessor = Block;
Ted Kremenek6b12da92011-02-21 22:11:26 +00001847 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001848 } else
1849 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001850
1851 // Because of short-circuit evaluation, the condition of the loop can span
1852 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1853 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001854 CFGBlock* ExitConditionBlock = createBlock(false);
1855 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001856
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001857 // Set the terminator for the "exit" condition block.
1858 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001859
1860 // Now add the actual condition to the condition block. Because the condition
1861 // itself may contain control-flow, new blocks may be created. Thus we update
1862 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001863 if (Stmt* C = W->getCond()) {
1864 Block = ExitConditionBlock;
1865 EntryConditionBlock = addStmt(C);
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001866 // The condition might finish the current 'Block'.
1867 Block = EntryConditionBlock;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001868
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001869 // If this block contains a condition variable, add both the condition
1870 // variable and initializer to the CFG.
1871 if (VarDecl *VD = W->getConditionVariable()) {
1872 if (Expr *Init = VD->getInit()) {
1873 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001874 appendStmt(Block, W, AddStmtChoice::AlwaysAdd);
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001875 EntryConditionBlock = addStmt(Init);
1876 assert(Block == EntryConditionBlock);
1877 }
1878 }
1879
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001880 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001881 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001882 return 0;
1883 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001884 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001885
1886 // The condition block is the implicit successor for the loop body as well as
1887 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001888 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001889
Mike Stump00998a02009-07-23 23:25:26 +00001890 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001891 const TryResult& KnownVal = tryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001892
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001893 // Process the loop body.
1894 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001895 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001896
1897 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001898 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1899 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1900 save_break(BreakJumpTarget);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001901
Mike Stump6d9828c2009-07-17 01:31:16 +00001902 // Create an empty block to represent the transition block for looping back
1903 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001904 Block = 0;
1905 assert(Succ == EntryConditionBlock);
1906 Succ = createBlock();
1907 Succ->setLoopTarget(W);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001908 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001909
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001910 // All breaks should go to the code following the loop.
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001911 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001912
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001913 // NULL out Block to force lazy instantiation of blocks for the body.
1914 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001915
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001916 // Loop body should end with destructor of Condition variable (if any).
1917 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1918
1919 // If body is not a compound statement create implicit scope
1920 // and add destructors.
1921 if (!isa<CompoundStmt>(W->getBody()))
1922 addLocalScopeAndDtors(W->getBody());
1923
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001924 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001925 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001926
Ted Kremenekaf603f72007-08-30 18:39:40 +00001927 if (!BodyBlock)
Ted Kremenek9ce52702011-01-07 19:37:16 +00001928 BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001929 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001930 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001931 return 0;
1932 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001933
Ted Kremenek941fde82009-07-24 04:47:11 +00001934 // Add the loop body entry as a successor to the condition.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001935 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001936 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001937
Ted Kremenek941fde82009-07-24 04:47:11 +00001938 // Link up the condition block with the code that follows the loop. (the
1939 // false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001940 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001941
1942 // There can be no more statements in the condition block since we loop back
1943 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001944 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001945
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001946 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001947 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001948 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001949}
Mike Stump1eb44332009-09-09 15:08:12 +00001950
1951
Ted Kremenek4f880632009-07-17 22:18:43 +00001952CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1953 // FIXME: For now we pretend that @catch and the code it contains does not
1954 // exit.
1955 return Block;
1956}
Mike Stump6d9828c2009-07-17 01:31:16 +00001957
Ted Kremenek2fda5042008-12-09 20:20:09 +00001958CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1959 // FIXME: This isn't complete. We basically treat @throw like a return
1960 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001961
Ted Kremenek6c249722009-09-24 18:45:41 +00001962 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001963 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001964 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001965
Ted Kremenek2fda5042008-12-09 20:20:09 +00001966 // Create the new block.
1967 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001968
Ted Kremenek2fda5042008-12-09 20:20:09 +00001969 // The Exit block is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001970 addSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001971
1972 // Add the statement to the block. This may create new blocks if S contains
1973 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001974 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001975}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001976
Mike Stump0979d802009-07-22 22:56:04 +00001977CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001978 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001979 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00001980 return 0;
1981
1982 // Create the new block.
1983 Block = createBlock(false);
1984
Mike Stump5d1d2022010-01-19 02:20:09 +00001985 if (TryTerminatedBlock)
1986 // The current try statement is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001987 addSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001988 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001989 // otherwise the Exit block is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001990 addSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001991
1992 // Add the statement to the block. This may create new blocks if S contains
1993 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001994 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001995}
1996
Ted Kremenek4f880632009-07-17 22:18:43 +00001997CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001998 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001999
Mike Stump8f9893a2009-07-21 01:27:50 +00002000 // "do...while" is a control-flow statement. Thus we stop processing the
2001 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002002 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002003 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002004 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002005 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002006 } else
2007 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00002008
2009 // Because of short-circuit evaluation, the condition of the loop can span
2010 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
2011 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002012 CFGBlock* ExitConditionBlock = createBlock(false);
2013 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002014
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002015 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00002016 ExitConditionBlock->setTerminator(D);
2017
2018 // Now add the actual condition to the condition block. Because the condition
2019 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002020 if (Stmt* C = D->getCond()) {
2021 Block = ExitConditionBlock;
2022 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002023 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002024 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002025 return 0;
2026 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002027 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002028
Ted Kremenek54827132008-02-27 07:20:00 +00002029 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002030 Succ = EntryConditionBlock;
2031
Mike Stump00998a02009-07-23 23:25:26 +00002032 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002033 const TryResult &KnownVal = tryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00002034
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002035 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002036 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002037 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002038 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002039
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002040 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002041 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2042 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2043 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00002044
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002045 // All continues within this loop should go to the condition block
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002046 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002047
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002048 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002049 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002050
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002051 // NULL out Block to force lazy instantiation of blocks for the body.
2052 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002053
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002054 // If body is not a compound statement create implicit scope
2055 // and add destructors.
2056 if (!isa<CompoundStmt>(D->getBody()))
2057 addLocalScopeAndDtors(D->getBody());
2058
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002059 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002060 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002061
Ted Kremenekaf603f72007-08-30 18:39:40 +00002062 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00002063 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002064 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002065 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002066 return 0;
2067 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002068
Ted Kremenekd173dc72010-08-17 20:59:56 +00002069 if (!KnownVal.isFalse()) {
2070 // Add an intermediate block between the BodyBlock and the
2071 // ExitConditionBlock to represent the "loop back" transition. Create an
2072 // empty block to represent the transition block for looping back to the
2073 // head of the loop.
2074 // FIXME: Can we do this more efficiently without adding another block?
2075 Block = NULL;
2076 Succ = BodyBlock;
2077 CFGBlock *LoopBackBlock = createBlock();
2078 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00002079
Ted Kremenekd173dc72010-08-17 20:59:56 +00002080 // Add the loop body entry as a successor to the condition.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002081 addSuccessor(ExitConditionBlock, LoopBackBlock);
Ted Kremenekd173dc72010-08-17 20:59:56 +00002082 }
2083 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002084 addSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002085 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002086
Ted Kremenek941fde82009-07-24 04:47:11 +00002087 // Link up the condition block with the code that follows the loop.
2088 // (the false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002089 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002090
2091 // There can be no more statements in the body block(s) since we loop back to
2092 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002093 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002094
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002095 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002096 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002097 return BodyBlock;
2098}
2099
2100CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2101 // "continue" is a control-flow statement. Thus we stop processing the
2102 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002103 if (badCFG)
2104 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002105
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002106 // Now create a new block that ends with the continue statement.
2107 Block = createBlock(false);
2108 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00002109
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002110 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002111 // incomplete AST. This means the CFG cannot be constructed.
Ted Kremenek9ce52702011-01-07 19:37:16 +00002112 if (ContinueJumpTarget.block) {
2113 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C);
2114 addSuccessor(Block, ContinueJumpTarget.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002115 } else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002116 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00002117
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002118 return Block;
2119}
Mike Stump1eb44332009-09-09 15:08:12 +00002120
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002121CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00002122 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002123
Ted Kremenek852274d2009-12-16 03:18:58 +00002124 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002125 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002126 appendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002127 }
Mike Stump1eb44332009-09-09 15:08:12 +00002128
Ted Kremenek4f880632009-07-17 22:18:43 +00002129 // VLA types have expressions that must be evaluated.
2130 if (E->isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00002131 for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr());
Ted Kremenek4f880632009-07-17 22:18:43 +00002132 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2133 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002134 }
Mike Stump1eb44332009-09-09 15:08:12 +00002135
Mike Stump6d9828c2009-07-17 01:31:16 +00002136 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002137}
Mike Stump1eb44332009-09-09 15:08:12 +00002138
Ted Kremenek4f880632009-07-17 22:18:43 +00002139/// VisitStmtExpr - Utility method to handle (nested) statement
2140/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00002141CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2142 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002143 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002144 appendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002145 }
Ted Kremenek4f880632009-07-17 22:18:43 +00002146 return VisitCompoundStmt(SE->getSubStmt());
2147}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002148
Ted Kremenek411cdee2008-04-16 21:10:48 +00002149CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002150 // "switch" is a control-flow statement. Thus we stop processing the current
2151 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002152 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002153
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002154 // Save local scope position because in case of condition variable ScopePos
2155 // won't be restored when traversing AST.
2156 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2157
2158 // Create local scope for possible condition variable.
2159 // Store scope position. Add implicit destructor.
2160 if (VarDecl* VD = Terminator->getConditionVariable()) {
2161 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2162 addLocalScopeForVarDecl(VD);
2163 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2164 }
2165
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002166 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002167 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002168 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002169 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00002170 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002171
2172 // Save the current "switch" context.
2173 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002174 save_default(DefaultCaseBlock);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002175 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002176
Mike Stump6d9828c2009-07-17 01:31:16 +00002177 // Set the "default" case to be the block after the switch statement. If the
2178 // switch statement contains a "default:", this value will be overwritten with
2179 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002180 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00002181
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002182 // Create a new block that will contain the switch statement.
2183 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002184
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002185 // Now process the switch body. The code after the switch is the implicit
2186 // successor.
2187 Succ = SwitchSuccessor;
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002188 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002189
2190 // When visiting the body, the case statements should automatically get linked
2191 // up to the switch. We also don't keep a pointer to the body, since all
2192 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002193 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002194 Block = NULL;
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002195
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002196 // For pruning unreachable case statements, save the current state
2197 // for tracking the condition value.
2198 SaveAndRestore<bool> save_switchExclusivelyCovered(switchExclusivelyCovered,
2199 false);
2200 SaveAndRestore<Expr::EvalResult> save_switchCond(switchCond);
2201
2202
2203 // Determine if the switch condition can be explicitly evaluated.
2204 assert(Terminator->getCond() && "switch condition must be non-NULL");
2205 tryEvaluate(Terminator->getCond(), switchCond);
2206
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002207 // If body is not a compound statement create implicit scope
2208 // and add destructors.
2209 if (!isa<CompoundStmt>(Terminator->getBody()))
2210 addLocalScopeAndDtors(Terminator->getBody());
2211
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002212 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002213 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002214 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002215 return 0;
2216 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002217
Mike Stump6d9828c2009-07-17 01:31:16 +00002218 // If we have no "default:" case, the default transition is to the code
2219 // following the switch body.
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002220 addSuccessor(SwitchTerminatedBlock,
2221 switchExclusivelyCovered ? 0 : DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002222
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002223 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002224 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002225 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002226 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00002227
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002228 // Finally, if the SwitchStmt contains a condition variable, add both the
2229 // SwitchStmt and the condition variable initialization to the CFG.
2230 if (VarDecl *VD = Terminator->getConditionVariable()) {
2231 if (Expr *Init = VD->getInit()) {
2232 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002233 appendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002234 addStmt(Init);
2235 }
2236 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00002237
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002238 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002239}
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002240
2241static bool shouldAddCase(bool &switchExclusivelyCovered,
2242 const Expr::EvalResult &switchCond,
2243 const CaseStmt *CS,
2244 ASTContext &Ctx) {
2245 bool addCase = false;
2246
2247 if (!switchExclusivelyCovered) {
2248 if (switchCond.Val.isInt()) {
2249 // Evaluate the LHS of the case value.
2250 Expr::EvalResult V1;
2251 CS->getLHS()->Evaluate(V1, Ctx);
2252 assert(V1.Val.isInt());
2253 const llvm::APSInt &condInt = switchCond.Val.getInt();
2254 const llvm::APSInt &lhsInt = V1.Val.getInt();
2255
2256 if (condInt == lhsInt) {
2257 addCase = true;
2258 switchExclusivelyCovered = true;
2259 }
2260 else if (condInt < lhsInt) {
2261 if (const Expr *RHS = CS->getRHS()) {
2262 // Evaluate the RHS of the case value.
2263 Expr::EvalResult V2;
2264 RHS->Evaluate(V2, Ctx);
2265 assert(V2.Val.isInt());
2266 if (V2.Val.getInt() <= condInt) {
2267 addCase = true;
2268 switchExclusivelyCovered = true;
2269 }
2270 }
2271 }
2272 }
2273 else
2274 addCase = true;
2275 }
2276 return addCase;
2277}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002278
Ted Kremenek4f880632009-07-17 22:18:43 +00002279CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002280 // CaseStmts are essentially labels, so they are the first statement in a
2281 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002282 CFGBlock *TopBlock = 0, *LastBlock = 0;
2283
2284 if (Stmt *Sub = CS->getSubStmt()) {
2285 // For deeply nested chains of CaseStmts, instead of doing a recursion
2286 // (which can blow out the stack), manually unroll and create blocks
2287 // along the way.
2288 while (isa<CaseStmt>(Sub)) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002289 CFGBlock *currentBlock = createBlock(false);
2290 currentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002291
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002292 if (TopBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002293 addSuccessor(LastBlock, currentBlock);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002294 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002295 TopBlock = currentBlock;
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002296
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002297 addSuccessor(SwitchTerminatedBlock,
2298 shouldAddCase(switchExclusivelyCovered, switchCond,
2299 CS, *Context)
2300 ? currentBlock : 0);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002301
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002302 LastBlock = currentBlock;
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002303 CS = cast<CaseStmt>(Sub);
2304 Sub = CS->getSubStmt();
2305 }
2306
2307 addStmt(Sub);
2308 }
Mike Stump1eb44332009-09-09 15:08:12 +00002309
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002310 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002311 if (!CaseBlock)
2312 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002313
2314 // Cases statements partition blocks, so this is the top of the basic block we
2315 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00002316 CaseBlock->setLabel(CS);
2317
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002318 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002319 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002320
2321 // Add this block to the list of successors for the block with the switch
2322 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00002323 assert(SwitchTerminatedBlock);
Ted Kremeneke71f3d52011-03-01 23:12:55 +00002324 addSuccessor(SwitchTerminatedBlock,
2325 shouldAddCase(switchExclusivelyCovered, switchCond,
2326 CS, *Context)
2327 ? CaseBlock : 0);
Mike Stump6d9828c2009-07-17 01:31:16 +00002328
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002329 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2330 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002331
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002332 if (TopBlock) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002333 addSuccessor(LastBlock, CaseBlock);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002334 Succ = TopBlock;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002335 } else {
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002336 // This block is now the implicit successor of other blocks.
2337 Succ = CaseBlock;
2338 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002339
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002340 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002341}
Mike Stump6d9828c2009-07-17 01:31:16 +00002342
Ted Kremenek411cdee2008-04-16 21:10:48 +00002343CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002344 if (Terminator->getSubStmt())
2345 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00002346
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002347 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002348
2349 if (!DefaultCaseBlock)
2350 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002351
2352 // Default statements partition blocks, so this is the top of the basic block
2353 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00002354 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00002355
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002356 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002357 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002358
Mike Stump6d9828c2009-07-17 01:31:16 +00002359 // Unlike case statements, we don't add the default block to the successors
2360 // for the switch statement immediately. This is done when we finish
2361 // processing the switch statement. This allows for the default case
2362 // (including a fall-through to the code after the switch statement) to always
2363 // be the last successor of a switch-terminated block.
2364
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002365 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2366 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002367
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002368 // This block is now the implicit successor of other blocks.
2369 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002370
2371 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00002372}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002373
Mike Stump5d1d2022010-01-19 02:20:09 +00002374CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2375 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2376 // current block.
2377 CFGBlock* TrySuccessor = NULL;
2378
2379 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002380 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002381 return 0;
2382 TrySuccessor = Block;
2383 } else TrySuccessor = Succ;
2384
Mike Stumpa1f93632010-01-20 01:15:34 +00002385 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00002386
2387 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002388 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00002389 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00002390 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00002391
Mike Stumpa1f93632010-01-20 01:15:34 +00002392 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00002393 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2394 // The code after the try is the implicit successor.
2395 Succ = TrySuccessor;
2396 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00002397 if (CS->getExceptionDecl() == 0) {
2398 HasCatchAll = true;
2399 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002400 Block = NULL;
2401 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2402 if (CatchBlock == 0)
2403 return 0;
2404 // Add this block to the list of successors for the block with the try
2405 // statement.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002406 addSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00002407 }
Mike Stumpa1f93632010-01-20 01:15:34 +00002408 if (!HasCatchAll) {
2409 if (PrevTryTerminatedBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002410 addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00002411 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002412 addSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00002413 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002414
2415 // The code after the try is the implicit successor.
2416 Succ = TrySuccessor;
2417
Mike Stumpf00cca52010-01-20 01:30:58 +00002418 // Save the current "try" context.
2419 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2420 TryTerminatedBlock = NewTryTerminatedBlock;
2421
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002422 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00002423 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002424 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00002425 return Block;
2426}
2427
2428CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2429 // CXXCatchStmt are treated like labels, so they are the first statement in a
2430 // block.
2431
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002432 // Save local scope position because in case of exception variable ScopePos
2433 // won't be restored when traversing AST.
2434 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2435
2436 // Create local scope for possible exception variable.
2437 // Store scope position. Add implicit destructor.
2438 if (VarDecl* VD = CS->getExceptionDecl()) {
2439 LocalScope::const_iterator BeginScopePos = ScopePos;
2440 addLocalScopeForVarDecl(VD);
2441 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2442 }
2443
Mike Stump5d1d2022010-01-19 02:20:09 +00002444 if (CS->getHandlerBlock())
2445 addStmt(CS->getHandlerBlock());
2446
2447 CFGBlock* CatchBlock = Block;
2448 if (!CatchBlock)
2449 CatchBlock = createBlock();
2450
2451 CatchBlock->setLabel(CS);
2452
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002453 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002454 return 0;
2455
2456 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2457 Block = NULL;
2458
2459 return CatchBlock;
2460}
2461
John McCall4765fa02010-12-06 08:20:24 +00002462CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +00002463 AddStmtChoice asc) {
2464 if (BuildOpts.AddImplicitDtors) {
2465 // If adding implicit destructors visit the full expression for adding
2466 // destructors of temporaries.
2467 VisitForTemporaryDtors(E->getSubExpr());
2468
2469 // Full expression has to be added as CFGStmt so it will be sequenced
2470 // before destructors of it's temporaries.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002471 asc = asc.withAlwaysAdd(true);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002472 }
2473 return Visit(E->getSubExpr(), asc);
2474}
2475
Zhongxing Xua725ed42010-11-01 13:04:58 +00002476CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2477 AddStmtChoice asc) {
2478 if (asc.alwaysAdd()) {
2479 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002480 appendStmt(Block, E, asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002481
2482 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002483 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002484 }
2485 return Visit(E->getSubExpr(), asc);
2486}
2487
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002488CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2489 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002490 autoCreateBlock();
Zhongxing Xu3ff5b262010-11-03 11:14:06 +00002491 if (!C->isElidable())
Ted Kremenek892697d2010-12-16 07:46:53 +00002492 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002493
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002494 return VisitChildren(C);
2495}
2496
Zhongxing Xua725ed42010-11-01 13:04:58 +00002497CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2498 AddStmtChoice asc) {
2499 if (asc.alwaysAdd()) {
2500 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002501 appendStmt(Block, E, asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002502 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002503 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002504 }
2505 return Visit(E->getSubExpr(), asc);
2506}
2507
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002508CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2509 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002510 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002511 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002512 return VisitChildren(C);
2513}
2514
Ted Kremenekad5a8942010-08-02 23:46:59 +00002515CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002516 AddStmtChoice asc) {
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002517 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002518 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002519 return VisitChildren(C);
2520}
2521
Zhongxing Xua725ed42010-11-01 13:04:58 +00002522CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2523 AddStmtChoice asc) {
2524 if (asc.alwaysAdd()) {
2525 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002526 appendStmt(Block, E, asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002527 }
Ted Kremenek892697d2010-12-16 07:46:53 +00002528 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xua725ed42010-11-01 13:04:58 +00002529}
2530
Ted Kremenek19bb3562007-08-28 19:26:49 +00002531CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002532 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00002533 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002534
Ted Kremenek19bb3562007-08-28 19:26:49 +00002535 if (!IBlock) {
2536 IBlock = createBlock(false);
2537 cfg->setIndirectGotoBlock(IBlock);
2538 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002539
Ted Kremenek19bb3562007-08-28 19:26:49 +00002540 // IndirectGoto is a control-flow statement. Thus we stop processing the
2541 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002542 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002543 return 0;
2544
Ted Kremenek19bb3562007-08-28 19:26:49 +00002545 Block = createBlock(false);
2546 Block->setTerminator(I);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002547 addSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00002548 return addStmt(I->getTarget());
2549}
2550
Marcin Swiderski8599e762010-11-03 06:19:35 +00002551CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2552tryAgain:
2553 if (!E) {
2554 badCFG = true;
2555 return NULL;
2556 }
2557 switch (E->getStmtClass()) {
2558 default:
2559 return VisitChildrenForTemporaryDtors(E);
2560
2561 case Stmt::BinaryOperatorClass:
2562 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2563
2564 case Stmt::CXXBindTemporaryExprClass:
2565 return VisitCXXBindTemporaryExprForTemporaryDtors(
2566 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2567
John McCall56ca35d2011-02-17 10:25:35 +00002568 case Stmt::BinaryConditionalOperatorClass:
Marcin Swiderski8599e762010-11-03 06:19:35 +00002569 case Stmt::ConditionalOperatorClass:
2570 return VisitConditionalOperatorForTemporaryDtors(
John McCall56ca35d2011-02-17 10:25:35 +00002571 cast<AbstractConditionalOperator>(E), BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002572
2573 case Stmt::ImplicitCastExprClass:
2574 // For implicit cast we want BindToTemporary to be passed further.
2575 E = cast<CastExpr>(E)->getSubExpr();
2576 goto tryAgain;
2577
2578 case Stmt::ParenExprClass:
2579 E = cast<ParenExpr>(E)->getSubExpr();
2580 goto tryAgain;
2581 }
2582}
2583
2584CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2585 // When visiting children for destructors we want to visit them in reverse
2586 // order. Because there's no reverse iterator for children must to reverse
2587 // them in helper vector.
2588 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2589 ChildrenVect ChildrenRev;
John McCall7502c1d2011-02-13 04:07:26 +00002590 for (Stmt::child_range I = E->children(); I; ++I) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002591 if (*I) ChildrenRev.push_back(*I);
2592 }
2593
2594 CFGBlock *B = Block;
2595 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2596 L = ChildrenRev.rend(); I != L; ++I) {
2597 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2598 B = R;
2599 }
2600 return B;
2601}
2602
2603CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2604 if (E->isLogicalOp()) {
2605 // Destructors for temporaries in LHS expression should be called after
2606 // those for RHS expression. Even if this will unnecessarily create a block,
2607 // this block will be used at least by the full expression.
2608 autoCreateBlock();
2609 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2610 if (badCFG)
2611 return NULL;
2612
2613 Succ = ConfluenceBlock;
2614 Block = NULL;
2615 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2616
2617 if (RHSBlock) {
2618 if (badCFG)
2619 return NULL;
2620
2621 // If RHS expression did produce destructors we need to connect created
2622 // blocks to CFG in same manner as for binary operator itself.
2623 CFGBlock *LHSBlock = createBlock(false);
2624 LHSBlock->setTerminator(CFGTerminator(E, true));
2625
2626 // For binary operator LHS block is before RHS in list of predecessors
2627 // of ConfluenceBlock.
2628 std::reverse(ConfluenceBlock->pred_begin(),
2629 ConfluenceBlock->pred_end());
2630
2631 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002632 TryResult KnownVal = tryEvaluateBool(E->getLHS());
Marcin Swiderski8599e762010-11-03 06:19:35 +00002633 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2634 KnownVal.negate();
2635
2636 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2637 // itself.
2638 if (E->getOpcode() == BO_LOr) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002639 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2640 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002641 } else {
2642 assert (E->getOpcode() == BO_LAnd);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002643 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2644 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002645 }
2646
2647 Block = LHSBlock;
2648 return LHSBlock;
2649 }
2650
2651 Block = ConfluenceBlock;
2652 return ConfluenceBlock;
2653 }
2654
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002655 if (E->isAssignmentOp()) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002656 // For assignment operator (=) LHS expression is visited
2657 // before RHS expression. For destructors visit them in reverse order.
2658 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2659 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2660 return LHSBlock ? LHSBlock : RHSBlock;
2661 }
2662
2663 // For any other binary operator RHS expression is visited before
2664 // LHS expression (order of children). For destructors visit them in reverse
2665 // order.
2666 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2667 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2668 return RHSBlock ? RHSBlock : LHSBlock;
2669}
2670
2671CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2672 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2673 // First add destructors for temporaries in subexpression.
2674 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xu249c9452010-11-14 15:23:50 +00002675 if (!BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002676 // If lifetime of temporary is not prolonged (by assigning to constant
2677 // reference) add destructor for it.
2678 autoCreateBlock();
2679 appendTemporaryDtor(Block, E);
2680 B = Block;
2681 }
2682 return B;
2683}
2684
2685CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
John McCall56ca35d2011-02-17 10:25:35 +00002686 AbstractConditionalOperator *E, bool BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002687 // First add destructors for condition expression. Even if this will
2688 // unnecessarily create a block, this block will be used at least by the full
2689 // expression.
2690 autoCreateBlock();
2691 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2692 if (badCFG)
2693 return NULL;
John McCall56ca35d2011-02-17 10:25:35 +00002694 if (BinaryConditionalOperator *BCO
2695 = dyn_cast<BinaryConditionalOperator>(E)) {
2696 ConfluenceBlock = VisitForTemporaryDtors(BCO->getCommon());
Marcin Swiderski8599e762010-11-03 06:19:35 +00002697 if (badCFG)
2698 return NULL;
2699 }
2700
John McCall56ca35d2011-02-17 10:25:35 +00002701 // Try to add block with destructors for LHS expression.
2702 CFGBlock *LHSBlock = NULL;
2703 Succ = ConfluenceBlock;
2704 Block = NULL;
2705 LHSBlock = VisitForTemporaryDtors(E->getTrueExpr(), BindToTemporary);
2706 if (badCFG)
2707 return NULL;
2708
Marcin Swiderski8599e762010-11-03 06:19:35 +00002709 // Try to add block with destructors for RHS expression;
2710 Succ = ConfluenceBlock;
2711 Block = NULL;
John McCall56ca35d2011-02-17 10:25:35 +00002712 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getFalseExpr(),
2713 BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002714 if (badCFG)
2715 return NULL;
2716
2717 if (!RHSBlock && !LHSBlock) {
2718 // If neither LHS nor RHS expression had temporaries to destroy don't create
2719 // more blocks.
2720 Block = ConfluenceBlock;
2721 return Block;
2722 }
2723
2724 Block = createBlock(false);
2725 Block->setTerminator(CFGTerminator(E, true));
2726
2727 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002728 const TryResult &KnownVal = tryEvaluateBool(E->getCond());
Marcin Swiderski8599e762010-11-03 06:19:35 +00002729
2730 if (LHSBlock) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002731 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002732 } else if (KnownVal.isFalse()) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002733 addSuccessor(Block, NULL);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002734 } else {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002735 addSuccessor(Block, ConfluenceBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002736 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2737 }
2738
2739 if (!RHSBlock)
2740 RHSBlock = ConfluenceBlock;
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002741 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002742
2743 return Block;
2744}
2745
Ted Kremenekbefef2f2007-08-23 21:26:19 +00002746} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00002747
Mike Stump6d9828c2009-07-17 01:31:16 +00002748/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2749/// no successors or predecessors. If this is the first block created in the
2750/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00002751CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00002752 bool first_block = begin() == end();
2753
2754 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002755 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2756 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2757 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00002758
2759 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002760 if (first_block)
2761 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00002762
2763 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002764 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00002765}
2766
Ted Kremenek026473c2007-08-23 16:51:22 +00002767/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2768/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00002769CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +00002770 BuildOptions BO) {
Ted Kremenek026473c2007-08-23 16:51:22 +00002771 CFGBuilder Builder;
Ted Kremenek6c52c782010-09-14 23:41:16 +00002772 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek026473c2007-08-23 16:51:22 +00002773}
2774
Ted Kremenek3c0349e2011-03-01 03:15:10 +00002775const CXXDestructorDecl *CFGImplicitDtor::getDestructorDecl() const {
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00002776 switch (getKind()) {
Matt Beaumont-Gay8e23e6d2011-03-02 23:25:06 +00002777 default: assert(0 && "Unknown CFGElement");
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +00002778 case CFGElement::Invalid:
2779 case CFGElement::Statement:
2780 case CFGElement::Initializer:
2781 llvm_unreachable("getDestructorDecl should only be used with "
2782 "ImplicitDtors");
2783 case CFGElement::AutomaticObjectDtor: {
2784 const VarDecl *var = cast<CFGAutomaticObjDtor>(this)->getVarDecl();
2785 QualType ty = var->getType();
2786 const RecordType *recordType = ty->getAs<RecordType>();
2787 const CXXRecordDecl *classDecl =
2788 cast<CXXRecordDecl>(recordType->getDecl());
2789 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 }
2803}
2804
2805bool CFGImplicitDtor::isNoReturn() const {
2806 if (const CXXDestructorDecl *cdecl = getDestructorDecl()) {
2807 QualType ty = cdecl->getType();
2808 return cast<FunctionType>(ty)->getNoReturnAttr();
2809 }
2810 return false;
Ted Kremenek3c0349e2011-03-01 03:15:10 +00002811}
2812
Ted Kremenek63f58872007-10-01 19:33:33 +00002813//===----------------------------------------------------------------------===//
2814// CFG: Queries for BlkExprs.
2815//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00002816
Ted Kremenek63f58872007-10-01 19:33:33 +00002817namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00002818 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00002819}
2820
Ted Kremenek8a693662009-12-23 23:37:10 +00002821static void FindSubExprAssignments(Stmt *S,
2822 llvm::SmallPtrSet<Expr*,50>& Set) {
2823 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002824 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002825
John McCall7502c1d2011-02-13 04:07:26 +00002826 for (Stmt::child_range I = S->children(); I; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002827 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00002828 if (!child)
2829 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00002830
Ted Kremenek8a693662009-12-23 23:37:10 +00002831 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002832 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00002833
Ted Kremenek8a693662009-12-23 23:37:10 +00002834 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002835 }
2836}
2837
Ted Kremenek63f58872007-10-01 19:33:33 +00002838static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2839 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00002840
2841 // Look for assignments that are used as subexpressions. These are the only
2842 // assignments that we want to *possibly* register as a block-level
2843 // expression. Basically, if an assignment occurs both in a subexpression and
2844 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002845 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00002846
Ted Kremenek63f58872007-10-01 19:33:33 +00002847 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002848 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek3c0349e2011-03-01 03:15:10 +00002849 if (const CFGStmt *S = BI->getAs<CFGStmt>())
2850 FindSubExprAssignments(S->getStmt(), SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00002851
Ted Kremenek411cdee2008-04-16 21:10:48 +00002852 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002853
2854 // Iterate over the statements again on identify the Expr* and Stmt* at the
2855 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002856
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002857 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00002858 const CFGStmt *CS = BI->getAs<CFGStmt>();
2859 if (!CS)
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002860 continue;
Ted Kremenek3c0349e2011-03-01 03:15:10 +00002861 if (Expr* Exp = dyn_cast<Expr>(CS->getStmt())) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002862
Ted Kremenek411cdee2008-04-16 21:10:48 +00002863 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002864 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00002865 // expression are really "statements" whose value is never used by
2866 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002867 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002868 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002869 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2870 // Special handling for statement expressions. The last statement in
2871 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002872 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00002873 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002874 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00002875 (*M)[C->body_back()] = x;
2876 }
2877 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00002878
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002879 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00002880 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002881 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002882 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002883
Ted Kremenek411cdee2008-04-16 21:10:48 +00002884 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00002885
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002886 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00002887
Ted Kremenek390e48b2008-11-12 21:11:49 +00002888 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00002889 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002890 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002891 }
2892 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002893
Ted Kremenek63f58872007-10-01 19:33:33 +00002894 return M;
2895}
2896
Ted Kremenek86946742008-01-17 20:48:37 +00002897CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2898 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00002899 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00002900
Ted Kremenek63f58872007-10-01 19:33:33 +00002901 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00002902 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002903 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00002904}
2905
2906unsigned CFG::getNumBlkExprs() {
2907 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2908 return M->size();
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002909
2910 // We assume callers interested in the number of BlkExprs will want
2911 // the map constructed if it doesn't already exist.
2912 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2913 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenek63f58872007-10-01 19:33:33 +00002914}
2915
Ted Kremenek274f4332008-04-28 18:00:46 +00002916//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002917// Filtered walking of the CFG.
2918//===----------------------------------------------------------------------===//
2919
2920bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00002921 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002922
2923 if (F.IgnoreDefaultsWithCoveredEnums) {
2924 // If the 'To' has no label or is labeled but the label isn't a
2925 // CaseStmt then filter this edge.
2926 if (const SwitchStmt *S =
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00002927 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002928 if (S->isAllEnumCasesCovered()) {
Ted Kremenekbe39a562010-09-09 02:57:48 +00002929 const Stmt *L = To->getLabel();
2930 if (!L || !isa<CaseStmt>(L))
2931 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002932 }
2933 }
2934 }
2935
2936 return false;
2937}
2938
2939//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00002940// Cleanup: CFG dstor.
2941//===----------------------------------------------------------------------===//
2942
Ted Kremenek63f58872007-10-01 19:33:33 +00002943CFG::~CFG() {
2944 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2945}
Mike Stump6d9828c2009-07-17 01:31:16 +00002946
Ted Kremenek7dba8602007-08-29 21:56:09 +00002947//===----------------------------------------------------------------------===//
2948// CFG pretty printing
2949//===----------------------------------------------------------------------===//
2950
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00002951namespace {
2952
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002953class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00002954 typedef llvm::DenseMap<const Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
2955 typedef llvm::DenseMap<const Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002956 StmtMapTy StmtMap;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002957 DeclMapTy DeclMap;
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002958 signed currentBlock;
2959 unsigned currentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00002960 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002961public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002962
Chris Lattnere4f21422009-06-30 01:26:17 +00002963 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
Ted Kremenek3c0349e2011-03-01 03:15:10 +00002964 : currentBlock(0), currentStmt(0), LangOpts(LO)
2965 {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002966 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2967 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002968 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002969 BI != BEnd; ++BI, ++j ) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00002970 if (const CFGStmt *SE = BI->getAs<CFGStmt>()) {
2971 const Stmt *stmt= SE->getStmt();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002972 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
Ted Kremenek3c0349e2011-03-01 03:15:10 +00002973 StmtMap[stmt] = P;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002974
Ted Kremenek3c0349e2011-03-01 03:15:10 +00002975 switch (stmt->getStmtClass()) {
2976 case Stmt::DeclStmtClass:
2977 DeclMap[cast<DeclStmt>(stmt)->getSingleDecl()] = P;
2978 break;
2979 case Stmt::IfStmtClass: {
2980 const VarDecl *var = cast<IfStmt>(stmt)->getConditionVariable();
2981 if (var)
2982 DeclMap[var] = P;
2983 break;
2984 }
2985 case Stmt::ForStmtClass: {
2986 const VarDecl *var = cast<ForStmt>(stmt)->getConditionVariable();
2987 if (var)
2988 DeclMap[var] = P;
2989 break;
2990 }
2991 case Stmt::WhileStmtClass: {
2992 const VarDecl *var =
2993 cast<WhileStmt>(stmt)->getConditionVariable();
2994 if (var)
2995 DeclMap[var] = P;
2996 break;
2997 }
2998 case Stmt::SwitchStmtClass: {
2999 const VarDecl *var =
3000 cast<SwitchStmt>(stmt)->getConditionVariable();
3001 if (var)
3002 DeclMap[var] = P;
3003 break;
3004 }
3005 case Stmt::CXXCatchStmtClass: {
3006 const VarDecl *var =
3007 cast<CXXCatchStmt>(stmt)->getExceptionDecl();
3008 if (var)
3009 DeclMap[var] = P;
3010 break;
3011 }
3012 default:
3013 break;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003014 }
3015 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003016 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00003017 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003018 }
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003019
Mike Stump6d9828c2009-07-17 01:31:16 +00003020
Ted Kremenek42a509f2007-08-31 21:30:12 +00003021 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00003022
Chris Lattnere4f21422009-06-30 01:26:17 +00003023 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003024 void setBlockID(signed i) { currentBlock = i; }
3025 void setStmtID(unsigned i) { currentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00003026
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003027 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
3028 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003029
3030 if (I == StmtMap.end())
3031 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003032
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003033 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3034 && I->second.second == currentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003035 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003036 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003037
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003038 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003039 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003040 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003041
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003042 bool handleDecl(const Decl* D, llvm::raw_ostream& OS) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003043 DeclMapTy::iterator I = DeclMap.find(D);
3044
3045 if (I == DeclMap.end())
3046 return false;
3047
Ted Kremenek0a3ed312010-12-17 04:44:39 +00003048 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
3049 && I->second.second == currentStmt) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003050 return false;
3051 }
3052
3053 OS << "[B" << I->second.first << "." << I->second.second << "]";
3054 return true;
3055 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003056};
Chris Lattnere4f21422009-06-30 01:26:17 +00003057} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00003058
Chris Lattnere4f21422009-06-30 01:26:17 +00003059
3060namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00003061class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00003062 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00003063
Ted Kremeneka95d3752008-09-13 05:16:45 +00003064 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003065 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003066 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003067public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003068 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00003069 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003070 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00003071
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003072 void VisitIfStmt(IfStmt* I) {
3073 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003074 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003075 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003076
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003077 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00003078 void VisitStmt(Stmt* Terminator) {
3079 Terminator->printPretty(OS, Helper, Policy);
3080 }
3081
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003082 void VisitForStmt(ForStmt* F) {
3083 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003084 if (F->getInit())
3085 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00003086 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003087 if (Stmt* C = F->getCond())
3088 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00003089 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003090 if (F->getInc())
3091 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00003092 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003093 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003094
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003095 void VisitWhileStmt(WhileStmt* W) {
3096 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003097 if (Stmt* C = W->getCond())
3098 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003099 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003100
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003101 void VisitDoStmt(DoStmt* D) {
3102 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00003103 if (Stmt* C = D->getCond())
3104 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00003105 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003106
Ted Kremenek411cdee2008-04-16 21:10:48 +00003107 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00003108 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003109 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00003110 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003111
Mike Stump5d1d2022010-01-19 02:20:09 +00003112 void VisitCXXTryStmt(CXXTryStmt* CS) {
3113 OS << "try ...";
3114 }
3115
John McCall56ca35d2011-02-17 10:25:35 +00003116 void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003117 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003118 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003119 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003120
Ted Kremenekaeddbf62007-08-31 22:29:13 +00003121 void VisitChooseExpr(ChooseExpr* C) {
3122 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003123 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00003124 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00003125 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003126
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003127 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
3128 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003129 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003130 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003131
Ted Kremenek805e9a82007-08-31 21:49:40 +00003132 void VisitBinaryOperator(BinaryOperator* B) {
3133 if (!B->isLogicalOp()) {
3134 VisitExpr(B);
3135 return;
3136 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003137
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003138 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003139
Ted Kremenek805e9a82007-08-31 21:49:40 +00003140 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00003141 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00003142 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003143 return;
John McCall2de56d12010-08-25 11:45:40 +00003144 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00003145 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003146 return;
3147 default:
3148 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003149 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00003150 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003151
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00003152 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003153 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003154 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003155};
Chris Lattnere4f21422009-06-30 01:26:17 +00003156} // end anonymous namespace
3157
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003158static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00003159 const CFGElement &E) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003160 if (const CFGStmt *CS = E.getAs<CFGStmt>()) {
3161 Stmt *S = CS->getStmt();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003162
3163 if (Helper) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003164
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003165 // special printing for statement-expressions.
3166 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3167 CompoundStmt* Sub = SE->getSubStmt();
3168
John McCall7502c1d2011-02-13 04:07:26 +00003169 if (Sub->children()) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003170 OS << "({ ... ; ";
3171 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3172 OS << " })\n";
3173 return;
3174 }
3175 }
3176 // special printing for comma expressions.
3177 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3178 if (B->getOpcode() == BO_Comma) {
3179 OS << "... , ";
3180 Helper->handledStmt(B->getRHS(),OS);
3181 OS << '\n';
3182 return;
3183 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003184 }
3185 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003186 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00003187
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003188 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003189 OS << " (OperatorCall)";
3190 } else if (isa<CXXBindTemporaryExpr>(S)) {
3191 OS << " (BindTemporary)";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003192 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003193
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003194 // Expressions need a newline.
3195 if (isa<Expr>(S))
3196 OS << '\n';
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00003197
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003198 } else if (const CFGInitializer *IE = E.getAs<CFGInitializer>()) {
3199 const CXXCtorInitializer *I = IE->getInitializer();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003200 if (I->isBaseInitializer())
3201 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichet00eb3f92010-12-04 09:14:42 +00003202 else OS << I->getAnyMember()->getName();
Mike Stump6d9828c2009-07-17 01:31:16 +00003203
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003204 OS << "(";
3205 if (Expr* IE = I->getInit())
3206 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3207 OS << ")";
3208
3209 if (I->isBaseInitializer())
3210 OS << " (Base initializer)\n";
3211 else OS << " (Member initializer)\n";
3212
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003213 } else if (const CFGAutomaticObjDtor *DE = E.getAs<CFGAutomaticObjDtor>()){
3214 const VarDecl* VD = DE->getVarDecl();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003215 Helper->handleDecl(VD, OS);
3216
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003217 const Type* T = VD->getType().getTypePtr();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003218 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3219 T = RT->getPointeeType().getTypePtr();
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003220 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3221 T = ET;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003222
3223 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3224 OS << " (Implicit destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003225
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003226 } else if (const CFGBaseDtor *BE = E.getAs<CFGBaseDtor>()) {
3227 const CXXBaseSpecifier *BS = BE->getBaseSpecifier();
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003228 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003229 OS << " (Base object destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003230
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003231 } else if (const CFGMemberDtor *ME = E.getAs<CFGMemberDtor>()) {
3232 const FieldDecl *FD = ME->getFieldDecl();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003233
3234 const Type *T = FD->getType().getTypePtr();
3235 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3236 T = ET;
3237
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003238 OS << "this->" << FD->getName();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003239 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003240 OS << " (Member object destructor)\n";
Marcin Swiderski8599e762010-11-03 06:19:35 +00003241
Ted Kremenek3c0349e2011-03-01 03:15:10 +00003242 } else if (const CFGTemporaryDtor *TE = E.getAs<CFGTemporaryDtor>()) {
3243 const CXXBindTemporaryExpr *BT = TE->getBindTemporaryExpr();
Marcin Swiderski8599e762010-11-03 06:19:35 +00003244 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3245 OS << " (Temporary object destructor)\n";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003246 }
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00003247}
Mike Stump6d9828c2009-07-17 01:31:16 +00003248
Chris Lattnere4f21422009-06-30 01:26:17 +00003249static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3250 const CFGBlock& B,
3251 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003252
Ted Kremenek42a509f2007-08-31 21:30:12 +00003253 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00003254
Ted Kremenek7dba8602007-08-29 21:56:09 +00003255 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00003256 OS << "\n [ B" << B.getBlockID();
3257
Ted Kremenek42a509f2007-08-31 21:30:12 +00003258 if (&B == &cfg->getEntry())
3259 OS << " (ENTRY) ]\n";
3260 else if (&B == &cfg->getExit())
3261 OS << " (EXIT) ]\n";
3262 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00003263 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003264 else
3265 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00003266
Ted Kremenek9cffe732007-08-29 23:20:49 +00003267 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00003268 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003269
3270 if (print_edges)
3271 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003272
Mike Stump079bd722010-01-19 22:00:14 +00003273 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003274 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00003275 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00003276 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003277 C->getLHS()->printPretty(OS, Helper,
3278 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003279 if (C->getRHS()) {
3280 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003281 C->getRHS()->printPretty(OS, Helper,
3282 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003283 }
Mike Stump079bd722010-01-19 22:00:14 +00003284 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003285 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00003286 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003287 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00003288 if (CS->getExceptionDecl())
3289 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3290 0);
3291 else
3292 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00003293 OS << ")";
3294
3295 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00003296 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003297
Ted Kremenek9cffe732007-08-29 23:20:49 +00003298 OS << ":\n";
3299 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003300
Ted Kremenekfddd5182007-08-21 21:42:03 +00003301 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00003302 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00003303
Ted Kremenek42a509f2007-08-31 21:30:12 +00003304 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3305 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003306
Ted Kremenek9cffe732007-08-29 23:20:49 +00003307 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003308 if (print_edges)
3309 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003310
Ted Kremeneka95d3752008-09-13 05:16:45 +00003311 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003312
Ted Kremenek42a509f2007-08-31 21:30:12 +00003313 if (Helper)
3314 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00003315
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003316 print_elem(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00003317 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003318
Ted Kremenek9cffe732007-08-29 23:20:49 +00003319 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003320 if (B.getTerminator()) {
3321 if (print_edges)
3322 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003323
Ted Kremenek9cffe732007-08-29 23:20:49 +00003324 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003325
Ted Kremenek42a509f2007-08-31 21:30:12 +00003326 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00003327
Chris Lattnere4f21422009-06-30 01:26:17 +00003328 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3329 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003330 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003331 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003332 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003333
Ted Kremenek9cffe732007-08-29 23:20:49 +00003334 if (print_edges) {
3335 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003336 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00003337 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00003338
Ted Kremenek42a509f2007-08-31 21:30:12 +00003339 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3340 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003341
Ted Kremenek42a509f2007-08-31 21:30:12 +00003342 if (i == 8 || (i-8) == 0)
3343 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003344
Ted Kremenek9cffe732007-08-29 23:20:49 +00003345 OS << " B" << (*I)->getBlockID();
3346 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003347
Ted Kremenek42a509f2007-08-31 21:30:12 +00003348 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00003349
Ted Kremenek42a509f2007-08-31 21:30:12 +00003350 // Print the successors of this block.
3351 OS << " Successors (" << B.succ_size() << "):";
3352 i = 0;
3353
3354 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3355 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003356
Ted Kremenek42a509f2007-08-31 21:30:12 +00003357 if (i == 8 || (i-8) % 10 == 0)
3358 OS << "\n ";
3359
Mike Stumpe5af3ce2009-07-20 23:24:15 +00003360 if (*I)
3361 OS << " B" << (*I)->getBlockID();
3362 else
3363 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003364 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003365
Ted Kremenek9cffe732007-08-29 23:20:49 +00003366 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003367 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003368}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003369
Ted Kremenek42a509f2007-08-31 21:30:12 +00003370
3371/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003372void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003373
3374/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00003375void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3376 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00003377
Ted Kremenek42a509f2007-08-31 21:30:12 +00003378 // Print the entry block.
3379 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00003380
Ted Kremenek42a509f2007-08-31 21:30:12 +00003381 // Iterate through the CFGBlocks and print them one by one.
3382 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3383 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003384 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00003385 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00003386
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003387 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003388 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003389
Ted Kremenek42a509f2007-08-31 21:30:12 +00003390 // Print the exit block.
3391 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00003392 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00003393}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003394
3395/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003396void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3397 print(llvm::errs(), cfg, LO);
3398}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003399
3400/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3401/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00003402void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3403 const LangOptions &LO) const {
3404 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003405 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00003406}
Ted Kremenek7dba8602007-08-29 21:56:09 +00003407
Ted Kremeneka2925852008-01-30 23:02:42 +00003408/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00003409void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00003410 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003411 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003412 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003413}
3414
Ted Kremenek390e48b2008-11-12 21:11:49 +00003415Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003416 Stmt *Terminator = this->Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003417 if (!Terminator)
3418 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003419
Ted Kremenek411cdee2008-04-16 21:10:48 +00003420 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003421
Ted Kremenek411cdee2008-04-16 21:10:48 +00003422 switch (Terminator->getStmtClass()) {
3423 default:
3424 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003425
Ted Kremenek411cdee2008-04-16 21:10:48 +00003426 case Stmt::ForStmtClass:
3427 E = cast<ForStmt>(Terminator)->getCond();
3428 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003429
Ted Kremenek411cdee2008-04-16 21:10:48 +00003430 case Stmt::WhileStmtClass:
3431 E = cast<WhileStmt>(Terminator)->getCond();
3432 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003433
Ted Kremenek411cdee2008-04-16 21:10:48 +00003434 case Stmt::DoStmtClass:
3435 E = cast<DoStmt>(Terminator)->getCond();
3436 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003437
Ted Kremenek411cdee2008-04-16 21:10:48 +00003438 case Stmt::IfStmtClass:
3439 E = cast<IfStmt>(Terminator)->getCond();
3440 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003441
Ted Kremenek411cdee2008-04-16 21:10:48 +00003442 case Stmt::ChooseExprClass:
3443 E = cast<ChooseExpr>(Terminator)->getCond();
3444 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003445
Ted Kremenek411cdee2008-04-16 21:10:48 +00003446 case Stmt::IndirectGotoStmtClass:
3447 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3448 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003449
Ted Kremenek411cdee2008-04-16 21:10:48 +00003450 case Stmt::SwitchStmtClass:
3451 E = cast<SwitchStmt>(Terminator)->getCond();
3452 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003453
John McCall56ca35d2011-02-17 10:25:35 +00003454 case Stmt::BinaryConditionalOperatorClass:
3455 E = cast<BinaryConditionalOperator>(Terminator)->getCond();
3456 break;
3457
Ted Kremenek411cdee2008-04-16 21:10:48 +00003458 case Stmt::ConditionalOperatorClass:
3459 E = cast<ConditionalOperator>(Terminator)->getCond();
3460 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003461
Ted Kremenek411cdee2008-04-16 21:10:48 +00003462 case Stmt::BinaryOperatorClass: // '&&' and '||'
3463 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00003464 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003465
Ted Kremenek390e48b2008-11-12 21:11:49 +00003466 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003467 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003468 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003469
Ted Kremenek411cdee2008-04-16 21:10:48 +00003470 return E ? E->IgnoreParens() : NULL;
3471}
3472
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003473bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003474 const Stmt *Terminator = this->Terminator;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003475 if (!Terminator)
3476 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003477
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003478 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003479
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003480 switch (Terminator->getStmtClass()) {
3481 default:
3482 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003483
3484 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003485 case Stmt::WhileStmtClass:
3486 case Stmt::DoStmtClass:
3487 case Stmt::IfStmtClass:
3488 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +00003489 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003490 case Stmt::ConditionalOperatorClass:
3491 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003492 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003493 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003494
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003495 return E ? E->IgnoreParens() : NULL;
3496}
3497
Ted Kremeneka2925852008-01-30 23:02:42 +00003498
Ted Kremenek7dba8602007-08-29 21:56:09 +00003499//===----------------------------------------------------------------------===//
3500// CFG Graphviz Visualization
3501//===----------------------------------------------------------------------===//
3502
Ted Kremenek42a509f2007-08-31 21:30:12 +00003503
3504#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00003505static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003506#endif
3507
Chris Lattnere4f21422009-06-30 01:26:17 +00003508void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003509#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00003510 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003511 GraphHelper = &H;
3512 llvm::ViewGraph(this,"CFG");
3513 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003514#endif
3515}
3516
Ted Kremenek7dba8602007-08-29 21:56:09 +00003517namespace llvm {
3518template<>
3519struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00003520
3521 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3522
3523 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00003524
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003525#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00003526 std::string OutSStr;
3527 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003528 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003529 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00003530
3531 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3532
3533 // Process string output to make it nicer...
3534 for (unsigned i = 0; i != OutStr.length(); ++i)
3535 if (OutStr[i] == '\n') { // Left justify
3536 OutStr[i] = '\\';
3537 OutStr.insert(OutStr.begin()+i+1, 'l');
3538 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003539
Ted Kremenek7dba8602007-08-29 21:56:09 +00003540 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003541#else
3542 return "";
3543#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00003544 }
3545};
3546} // end namespace llvm