blob: 42c0fdc0cb813c4ced646f186cf3bb0767318156 [file] [log] [blame]
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekb1c170e2009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek6796fbd2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stump6bf1c082010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000020#include "llvm/Support/GraphWriter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000021#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek8aed4902009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000026
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor6e6ad602009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000037}
Ted Kremenekdc03bd02010-08-02 23:46:59 +000038
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000039class AddStmtChoice {
40public:
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000041 enum Kind { NotAlwaysAdd = 0,
42 AlwaysAdd = 1,
43 AsLValueNotAlwaysAdd = 2,
44 AlwaysAddAsLValue = 3 };
45
Benjamin Kramera3b13412010-03-03 16:28:47 +000046 AddStmtChoice(Kind kind) : k(kind) {}
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000047
Benjamin Kramera3b13412010-03-03 16:28:47 +000048 bool alwaysAdd() const { return (unsigned)k & 0x1; }
Ted Kremenek66de6032010-04-11 17:02:04 +000049 bool asLValue() const { return k >= AsLValueNotAlwaysAdd; }
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000050
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000051private:
Benjamin Kramera3b13412010-03-03 16:28:47 +000052 Kind k;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000053};
Mike Stump31feda52009-07-17 01:31:16 +000054
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000055/// LocalScope - Node in tree of local scopes created for C++ implicit
56/// destructor calls generation. It contains list of automatic variables
57/// declared in the scope and link to position in previous scope this scope
58/// began in.
59///
60/// The process of creating local scopes is as follows:
61/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
62/// - Before processing statements in scope (e.g. CompoundStmt) create
63/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
64/// and set CFGBuilder::ScopePos to the end of new scope,
65/// - On every occurance of VarDecl increase CFGBuilder::ScopePos if it points
66/// at this VarDecl,
67/// - For every normal (without jump) end of scope add to CFGBlock destructors
68/// for objects in the current scope,
69/// - For every jump add to CFGBlock destructors for objects
70/// between CFGBuilder::ScopePos and local scope position saved for jump
71/// target. Thanks to C++ restrictions on goto jumps we can be sure that
72/// jump target position will be on the path to root from CFGBuilder::ScopePos
73/// (adding any variable that doesn't need constructor to be called to
74/// LocalScope can break this assumption),
75///
76class LocalScope {
77public:
78 typedef llvm::SmallVector<VarDecl*, 4> AutomaticVarsTy;
79
80 /// const_iterator - Iterates local scope backwards and jumps to previous
81 /// scope on reaching the begining of currently iterated scope.
82 class const_iterator {
83 const LocalScope* Scope;
84
85 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
86 /// Invalid iterator (with null Scope) has VarIter equal to 0.
87 unsigned VarIter;
88
89 public:
90 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
91 /// Incrementing invalid iterator is allowed and will result in invalid
92 /// iterator.
93 const_iterator()
94 : Scope(NULL), VarIter(0) {}
95
96 /// Create valid iterator. In case when S.Prev is an invalid iterator and
97 /// I is equal to 0, this will create invalid iterator.
98 const_iterator(const LocalScope& S, unsigned I)
99 : Scope(&S), VarIter(I) {
100 // Iterator to "end" of scope is not allowed. Handle it by going up
101 // in scopes tree possibly up to invalid iterator in the root.
102 if (VarIter == 0 && Scope)
103 *this = Scope->Prev;
104 }
105
106 VarDecl* const* operator->() const {
107 assert (Scope && "Dereferencing invalid iterator is not allowed");
108 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
109 return &Scope->Vars[VarIter - 1];
110 }
111 VarDecl* operator*() const {
112 return *this->operator->();
113 }
114
115 const_iterator& operator++() {
116 if (!Scope)
117 return *this;
118
119 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
120 --VarIter;
121 if (VarIter == 0)
122 *this = Scope->Prev;
123 return *this;
124 }
125
126 bool operator==(const const_iterator& rhs) const {
127 return Scope == rhs.Scope && VarIter == rhs.VarIter;
128 }
129 bool operator!=(const const_iterator& rhs) const {
130 return !(*this == rhs);
131 }
132 };
133
134 friend class const_iterator;
135
136private:
137 /// Automatic variables in order of declaration.
138 AutomaticVarsTy Vars;
139 /// Iterator to variable in previous scope that was declared just before
140 /// begin of this scope.
141 const_iterator Prev;
142
143public:
144 /// Constructs empty scope linked to previous scope in specified place.
145 LocalScope(const_iterator P)
146 : Vars()
147 , Prev(P) {}
148
149 /// Begin of scope in direction of CFG building (backwards).
150 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
151};
152
153/// BlockScopePosPair - Structure for specifing position in CFG during its build
154/// proces. It consists of CFGBlock that specifies position in CFG graph and
155/// LocalScope::const_iterator that specifies position in LocalScope graph.
156struct BlockScopePosPair {
157 BlockScopePosPair() {}
158 BlockScopePosPair(CFGBlock* B, LocalScope::const_iterator S)
159 : Block(B), ScopePos(S) {}
160
161 CFGBlock* Block;
162 LocalScope::const_iterator ScopePos;
163};
164
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000165/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000166/// The builder is stateful: an instance of the builder should be used to only
167/// construct a single CFG.
168///
169/// Example usage:
170///
171/// CFGBuilder builder;
172/// CFG* cfg = builder.BuildAST(stmt1);
173///
Mike Stump31feda52009-07-17 01:31:16 +0000174/// CFG construction is done via a recursive walk of an AST. We actually parse
175/// the AST in reverse order so that the successor of a basic block is
176/// constructed prior to its predecessor. This allows us to nicely capture
177/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +0000178///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000179class CFGBuilder {
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000180 typedef BlockScopePosPair JumpTarget;
181 typedef BlockScopePosPair JumpSource;
182
Mike Stump0d76d072009-07-20 23:24:15 +0000183 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000184 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000185
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000186 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000187 CFGBlock* Succ;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000188 JumpTarget ContinueJumpTarget;
189 JumpTarget BreakJumpTarget;
Ted Kremenek879d8e12007-08-23 18:43:24 +0000190 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +0000191 CFGBlock* DefaultCaseBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000192 CFGBlock* TryTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000193
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000194 // Current position in local scope.
195 LocalScope::const_iterator ScopePos;
196
197 // LabelMap records the mapping from Label expressions to their jump targets.
198 typedef llvm::DenseMap<LabelStmt*, JumpTarget> LabelMapTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000199 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +0000200
201 // A list of blocks that end with a "goto" that must be backpatched to their
202 // resolved targets upon completion of CFG construction.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000203 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000204 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +0000205
Ted Kremenekeda180e22007-08-28 19:26:49 +0000206 // A list of labels whose address has been taken (for indirect gotos).
207 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
208 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +0000209
Zhongxing Xud38fb842010-09-16 03:28:18 +0000210 bool badCFG;
211 CFG::BuildOptions BuildOpts;
212
Mike Stump31feda52009-07-17 01:31:16 +0000213public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000214 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
215 Block(NULL), Succ(NULL),
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000216 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xud38fb842010-09-16 03:28:18 +0000217 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump31feda52009-07-17 01:31:16 +0000218
Ted Kremenek9aae5132007-08-23 21:42:29 +0000219 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000220 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000221 CFG::BuildOptions BO);
Mike Stump31feda52009-07-17 01:31:16 +0000222
Ted Kremenek93668002009-07-17 22:18:43 +0000223private:
224 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000225 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
226 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
227 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000228 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000229 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
230 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
231 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000232 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000233 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000234 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000235 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000236 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000237 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000238 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000239 CFGBlock *VisitDeclStmt(DeclStmt *DS);
240 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000241 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
242 CFGBlock *VisitDoStmt(DoStmt *D);
243 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000244 CFGBlock *VisitGotoStmt(GotoStmt* G);
245 CFGBlock *VisitIfStmt(IfStmt *I);
246 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
247 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000248 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000249 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
250 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
251 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
252 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
253 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
254 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000255 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
256 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000257 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
258 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000259
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000260 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
261 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000262 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000263
Ted Kremenek6065ef62008-04-28 18:00:46 +0000264 // NYS == Not Yet Supported
265 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000266 badCFG = true;
267 return Block;
268 }
Mike Stump31feda52009-07-17 01:31:16 +0000269
Ted Kremenek93668002009-07-17 22:18:43 +0000270 void autoCreateBlock() { if (!Block) Block = createBlock(); }
271 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000272
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000273 CFGBlock *addStmt(Stmt *S) {
274 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000275 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000276
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000277 void AppendStmt(CFGBlock *B, Stmt *S,
278 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
279 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000280 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000281
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000282 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
283 B->addSuccessor(S, cfg->getBumpVectorContext());
284 }
Mike Stump11289f42009-09-09 15:08:12 +0000285
Ted Kremenek963cc312009-07-24 06:55:42 +0000286 /// TryResult - a class representing a variant over the values
287 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
288 /// and is used by the CFGBuilder to decide if a branch condition
289 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000290 class TryResult {
291 int X;
292 public:
293 TryResult(bool b) : X(b ? 1 : 0) {}
294 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000295
Ted Kremenek30754282009-07-24 04:47:11 +0000296 bool isTrue() const { return X == 1; }
297 bool isFalse() const { return X == 0; }
298 bool isKnown() const { return X >= 0; }
299 void negate() {
300 assert(isKnown());
301 X ^= 0x1;
302 }
303 };
Mike Stump11289f42009-09-09 15:08:12 +0000304
Mike Stump773582d2009-07-23 23:25:26 +0000305 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
306 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000307 TryResult TryEvaluateBool(Expr *S) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000308 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000309 return TryResult();
310
Mike Stump773582d2009-07-23 23:25:26 +0000311 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000312 if (!S->isTypeDependent() && !S->isValueDependent() &&
313 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000314 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000315
316 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000317 }
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000318};
Mike Stump31feda52009-07-17 01:31:16 +0000319
Douglas Gregor4619e432008-12-05 23:32:09 +0000320// FIXME: Add support for dependent-sized array types in C++?
321// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000322static VariableArrayType* FindVA(Type* t) {
323 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
324 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
325 if (vat->getSizeExpr())
326 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000327
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000328 t = vt->getElementType().getTypePtr();
329 }
Mike Stump31feda52009-07-17 01:31:16 +0000330
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000331 return 0;
332}
Mike Stump31feda52009-07-17 01:31:16 +0000333
334/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
335/// arbitrary statement. Examples include a single expression or a function
336/// body (compound statement). The ownership of the returned CFG is
337/// transferred to the caller. If CFG construction fails, this method returns
338/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000339CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000340 CFG::BuildOptions BO) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000341
Mike Stump0d76d072009-07-20 23:24:15 +0000342 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000343 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000344 if (!Statement)
345 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000346
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000347 BuildOpts = BO;
348 if (!C->getLangOptions().CPlusPlus)
349 BuildOpts.AddImplicitDtors = false;
Mike Stump31feda52009-07-17 01:31:16 +0000350
351 // Create an empty block that will serve as the exit block for the CFG. Since
352 // this is the first block added to the CFG, it will be implicitly registered
353 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000354 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000355 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000356 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000357
Ted Kremenek9aae5132007-08-23 21:42:29 +0000358 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000359 CFGBlock *B = addStmt(Statement);
360
361 if (badCFG)
362 return NULL;
363
364 if (B)
365 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000366
367 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
368 // FIXME: Add code for base initializers and member initializers.
369 (void)CD;
370 }
Mike Stump31feda52009-07-17 01:31:16 +0000371
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000372 // Backpatch the gotos whose label -> block mappings we didn't know when we
373 // encountered them.
374 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
375 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000376
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000377 CFGBlock* B = I->Block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000378 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
379 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000380
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000381 // If there is no target for the goto, then we are looking at an
382 // incomplete AST. Handle this by not registering a successor.
383 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000384
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000385 JumpTarget JT = LI->second;
386 AddSuccessor(B, JT.Block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000387 }
388
389 // Add successors to the Indirect Goto Dispatch block (if we have one).
390 if (CFGBlock* B = cfg->getIndirectGotoBlock())
391 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
392 E = AddressTakenLabels.end(); I != E; ++I ) {
393
394 // Lookup the target block.
395 LabelMapTy::iterator LI = LabelMap.find(*I);
396
397 // If there is no target block that contains label, then we are looking
398 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000399 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000400
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000401 AddSuccessor(B, LI->second.Block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000402 }
Mike Stump31feda52009-07-17 01:31:16 +0000403
Mike Stump31feda52009-07-17 01:31:16 +0000404 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000405 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000406
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000407 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000408}
Mike Stump31feda52009-07-17 01:31:16 +0000409
Ted Kremenek9aae5132007-08-23 21:42:29 +0000410/// createBlock - Used to lazily create blocks that are connected
411/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000412CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000413 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000414 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000415 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000416 return B;
417}
Mike Stump31feda52009-07-17 01:31:16 +0000418
Ted Kremenek93668002009-07-17 22:18:43 +0000419/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000420/// blocks for ternary operators, &&, and ||. We also process "," and
421/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000422CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000423tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000424 if (!S) {
425 badCFG = true;
426 return 0;
427 }
Ted Kremenek93668002009-07-17 22:18:43 +0000428 switch (S->getStmtClass()) {
429 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000430 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000431
432 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000433 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000434
Ted Kremenek93668002009-07-17 22:18:43 +0000435 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000436 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000437
Ted Kremenek93668002009-07-17 22:18:43 +0000438 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000439 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000440
Ted Kremenek93668002009-07-17 22:18:43 +0000441 case Stmt::BreakStmtClass:
442 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000443
Ted Kremenek93668002009-07-17 22:18:43 +0000444 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000445 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000446 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000447
Ted Kremenek93668002009-07-17 22:18:43 +0000448 case Stmt::CaseStmtClass:
449 return VisitCaseStmt(cast<CaseStmt>(S));
450
451 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000452 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000453
Ted Kremenek93668002009-07-17 22:18:43 +0000454 case Stmt::CompoundStmtClass:
455 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000456
Ted Kremenek93668002009-07-17 22:18:43 +0000457 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000458 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000459
Ted Kremenek93668002009-07-17 22:18:43 +0000460 case Stmt::ContinueStmtClass:
461 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000462
Ted Kremenekb27378c2010-01-19 20:40:33 +0000463 case Stmt::CXXCatchStmtClass:
464 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
465
Ted Kremenek82bfc862010-08-28 00:19:02 +0000466 case Stmt::CXXExprWithTemporariesClass: {
467 // FIXME: Handle temporaries. For now, just visit the subexpression
468 // so we don't artificially create extra blocks.
Ted Kremenek128d04d2010-08-31 18:47:34 +0000469 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000470 }
471
Zhongxing Xu7e612172010-04-13 09:38:01 +0000472 case Stmt::CXXMemberCallExprClass:
473 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
474
Ted Kremenekb27378c2010-01-19 20:40:33 +0000475 case Stmt::CXXThrowExprClass:
476 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000477
Ted Kremenekb27378c2010-01-19 20:40:33 +0000478 case Stmt::CXXTryStmtClass:
479 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000480
Ted Kremenek93668002009-07-17 22:18:43 +0000481 case Stmt::DeclStmtClass:
482 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000483
Ted Kremenek93668002009-07-17 22:18:43 +0000484 case Stmt::DefaultStmtClass:
485 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000486
Ted Kremenek93668002009-07-17 22:18:43 +0000487 case Stmt::DoStmtClass:
488 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000489
Ted Kremenek93668002009-07-17 22:18:43 +0000490 case Stmt::ForStmtClass:
491 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000492
Ted Kremenek93668002009-07-17 22:18:43 +0000493 case Stmt::GotoStmtClass:
494 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000495
Ted Kremenek93668002009-07-17 22:18:43 +0000496 case Stmt::IfStmtClass:
497 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000498
Ted Kremenek93668002009-07-17 22:18:43 +0000499 case Stmt::IndirectGotoStmtClass:
500 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000501
Ted Kremenek93668002009-07-17 22:18:43 +0000502 case Stmt::LabelStmtClass:
503 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000504
Ted Kremenek5868ec62010-04-11 17:02:10 +0000505 case Stmt::MemberExprClass:
506 return VisitMemberExpr(cast<MemberExpr>(S), asc);
507
Ted Kremenek93668002009-07-17 22:18:43 +0000508 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000509 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
510
Ted Kremenek93668002009-07-17 22:18:43 +0000511 case Stmt::ObjCAtSynchronizedStmtClass:
512 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000513
Ted Kremenek93668002009-07-17 22:18:43 +0000514 case Stmt::ObjCAtThrowStmtClass:
515 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000516
Ted Kremenek93668002009-07-17 22:18:43 +0000517 case Stmt::ObjCAtTryStmtClass:
518 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000519
Ted Kremenek93668002009-07-17 22:18:43 +0000520 case Stmt::ObjCForCollectionStmtClass:
521 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000522
Ted Kremenek93668002009-07-17 22:18:43 +0000523 case Stmt::ParenExprClass:
524 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000525 goto tryAgain;
526
Ted Kremenek93668002009-07-17 22:18:43 +0000527 case Stmt::NullStmtClass:
528 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000529
Ted Kremenek93668002009-07-17 22:18:43 +0000530 case Stmt::ReturnStmtClass:
531 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000532
Ted Kremenek93668002009-07-17 22:18:43 +0000533 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000534 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000535
Ted Kremenek93668002009-07-17 22:18:43 +0000536 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000537 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000538
Ted Kremenek93668002009-07-17 22:18:43 +0000539 case Stmt::SwitchStmtClass:
540 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000541
Ted Kremenek93668002009-07-17 22:18:43 +0000542 case Stmt::WhileStmtClass:
543 return VisitWhileStmt(cast<WhileStmt>(S));
544 }
545}
Mike Stump11289f42009-09-09 15:08:12 +0000546
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000547CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
548 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000549 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000550 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000551 }
Mike Stump11289f42009-09-09 15:08:12 +0000552
Ted Kremenek93668002009-07-17 22:18:43 +0000553 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000554}
Mike Stump31feda52009-07-17 01:31:16 +0000555
Ted Kremenek93668002009-07-17 22:18:43 +0000556/// VisitChildren - Visit the children of a Stmt.
557CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
558 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000559 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000560 E = Terminator->child_end(); I != E; ++I) {
561 if (*I) B = Visit(*I);
562 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000563 return B;
564}
Mike Stump11289f42009-09-09 15:08:12 +0000565
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000566CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
567 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000568 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000569
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000570 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000571 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000572 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000573 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000574
Ted Kremenek9aae5132007-08-23 21:42:29 +0000575 return Block;
576}
Mike Stump11289f42009-09-09 15:08:12 +0000577
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000578CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
579 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000580 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000581 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000582 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000583
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000584 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000585 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000586
Ted Kremenek93668002009-07-17 22:18:43 +0000587 // create the block evaluating the LHS
588 CFGBlock* LHSBlock = createBlock(false);
589 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000590
Ted Kremenek93668002009-07-17 22:18:43 +0000591 // create the block evaluating the RHS
592 Succ = ConfluenceBlock;
593 Block = NULL;
594 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000595
596 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000597 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000598 return 0;
599 }
600 else {
601 // Create an empty block for cases where the RHS doesn't require
602 // any explicit statements in the CFG.
603 RHSBlock = createBlock();
604 }
Mike Stump11289f42009-09-09 15:08:12 +0000605
Mike Stump773582d2009-07-23 23:25:26 +0000606 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000607 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000608 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000609 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000610
Ted Kremenek93668002009-07-17 22:18:43 +0000611 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000612 if (B->getOpcode() == BO_LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000613 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
614 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000615 } else {
John McCalle3027922010-08-25 11:45:40 +0000616 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000617 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
618 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000619 }
Mike Stump11289f42009-09-09 15:08:12 +0000620
Ted Kremenek93668002009-07-17 22:18:43 +0000621 // Generate the blocks for evaluating the LHS.
622 Block = LHSBlock;
623 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000624 }
John McCalle3027922010-08-25 11:45:40 +0000625 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000626 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000627 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000628 addStmt(B->getRHS());
629 return addStmt(B->getLHS());
630 }
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000631 else if (B->isAssignmentOp()) {
632 if (asc.alwaysAdd()) {
633 autoCreateBlock();
634 AppendStmt(Block, B, asc);
635 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000636
Ted Kremenek8abff772010-09-14 01:13:32 +0000637 // If visiting RHS causes us to finish 'Block' and the LHS doesn't
638 // create a new block, then we should return RBlock. Otherwise
639 // we'll incorrectly return NULL.
640 CFGBlock *RBlock = Visit(B->getRHS());
641 CFGBlock *LBlock = Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
642 return LBlock ? LBlock : RBlock;
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000643 }
Mike Stump11289f42009-09-09 15:08:12 +0000644
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000645 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000646}
647
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000648CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
649 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000650 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000651 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000652 }
653 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000654}
655
Ted Kremenek93668002009-07-17 22:18:43 +0000656CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
657 // "break" is a control-flow statement. Thus we stop processing the current
658 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000659 if (badCFG)
660 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000661
Ted Kremenek93668002009-07-17 22:18:43 +0000662 // Now create a new block that ends with the break statement.
663 Block = createBlock(false);
664 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000665
Ted Kremenek93668002009-07-17 22:18:43 +0000666 // If there is no target for the break, then we are looking at an incomplete
667 // AST. This means that the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000668 if (BreakJumpTarget.Block) {
669 AddSuccessor(Block, BreakJumpTarget.Block);
670 } else
Ted Kremenek93668002009-07-17 22:18:43 +0000671 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000672
673
Ted Kremenek9aae5132007-08-23 21:42:29 +0000674 return Block;
675}
Mike Stump11289f42009-09-09 15:08:12 +0000676
Mike Stump04c68512010-01-21 15:20:48 +0000677static bool CanThrow(Expr *E) {
678 QualType Ty = E->getType();
679 if (Ty->isFunctionPointerType())
680 Ty = Ty->getAs<PointerType>()->getPointeeType();
681 else if (Ty->isBlockPointerType())
682 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000683
Mike Stump04c68512010-01-21 15:20:48 +0000684 const FunctionType *FT = Ty->getAs<FunctionType>();
685 if (FT) {
686 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
687 if (Proto->hasEmptyExceptionSpec())
688 return false;
689 }
690 return true;
691}
692
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000693CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000694 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000695 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000696 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000697 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000698 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000699
Mike Stump04c68512010-01-21 15:20:48 +0000700 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000701
702 // Languages without exceptions are assumed to not throw.
703 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000704 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +0000705 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +0000706 }
707
708 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000709 if (FD->hasAttr<NoReturnAttr>())
710 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +0000711 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +0000712 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000713 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000714
Mike Stump04c68512010-01-21 15:20:48 +0000715 if (!CanThrow(C->getCallee()))
716 AddEHEdge = false;
717
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000718 if (!NoReturn && !AddEHEdge) {
719 if (asc.asLValue())
720 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
721 else
722 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
723 }
Mike Stump11289f42009-09-09 15:08:12 +0000724
Mike Stump92244b02010-01-19 22:00:14 +0000725 if (Block) {
726 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000727 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +0000728 return 0;
729 }
Mike Stump11289f42009-09-09 15:08:12 +0000730
Mike Stump92244b02010-01-19 22:00:14 +0000731 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000732 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000733
Mike Stump92244b02010-01-19 22:00:14 +0000734 if (NoReturn) {
735 // Wire this to the exit block directly.
736 AddSuccessor(Block, &cfg->getExit());
737 }
Mike Stump04c68512010-01-21 15:20:48 +0000738 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +0000739 // Add exceptional edges.
740 if (TryTerminatedBlock)
741 AddSuccessor(Block, TryTerminatedBlock);
742 else
743 AddSuccessor(Block, &cfg->getExit());
744 }
Mike Stump11289f42009-09-09 15:08:12 +0000745
Mike Stump8c5d7992009-07-25 21:26:53 +0000746 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000747}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000748
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000749CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
750 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000751 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000752 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000753 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000754 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000755
Ted Kremenek5868ec62010-04-11 17:02:10 +0000756 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
757 : AddStmtChoice::AlwaysAdd;
758
Ted Kremenek21822592009-07-17 18:20:32 +0000759 Succ = ConfluenceBlock;
760 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000761 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000762 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000763 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000764
Ted Kremenek21822592009-07-17 18:20:32 +0000765 Succ = ConfluenceBlock;
766 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000767 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000768 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000769 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000770
Ted Kremenek21822592009-07-17 18:20:32 +0000771 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000772 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000773 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000774 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
775 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000776 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000777 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000778}
Mike Stump11289f42009-09-09 15:08:12 +0000779
780
781CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
782 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000783
784 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
785 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +0000786 // If we hit a segment of code just containing ';' (NullStmts), we can
787 // get a null block back. In such cases, just use the LastBlock
788 if (CFGBlock *newBlock = addStmt(*I))
789 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +0000790
Ted Kremenekce499c22009-08-27 23:16:26 +0000791 if (badCFG)
792 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000793 }
Mike Stump92244b02010-01-19 22:00:14 +0000794
Ted Kremenek93668002009-07-17 22:18:43 +0000795 return LastBlock;
796}
Mike Stump11289f42009-09-09 15:08:12 +0000797
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000798CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
799 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000800 // Create the confluence block that will "merge" the results of the ternary
801 // expression.
802 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000803 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000804 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +0000805 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000806
Ted Kremenek5868ec62010-04-11 17:02:10 +0000807 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
808 : AddStmtChoice::AlwaysAdd;
809
Ted Kremenek51d40b02009-07-17 18:15:54 +0000810 // Create a block for the LHS expression if there is an LHS expression. A
811 // GCC extension allows LHS to be NULL, causing the condition to be the
812 // value that is returned instead.
813 // e.g: x ?: y is shorthand for: x ? x : y;
814 Succ = ConfluenceBlock;
815 Block = NULL;
816 CFGBlock* LHSBlock = NULL;
817 if (C->getLHS()) {
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000818 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000819 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +0000820 return 0;
821 Block = NULL;
822 }
Mike Stump11289f42009-09-09 15:08:12 +0000823
Ted Kremenek51d40b02009-07-17 18:15:54 +0000824 // Create the block for the RHS expression.
825 Succ = ConfluenceBlock;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000826 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000827 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +0000828 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000829
Ted Kremenek51d40b02009-07-17 18:15:54 +0000830 // Create the block that will contain the condition.
831 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000832
Mike Stump773582d2009-07-23 23:25:26 +0000833 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000834 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000835 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000836 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000837 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000838 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000839 // If we know the condition is false, add NULL as the successor for
840 // the block containing the condition. In this case, the confluence
841 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000842 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000843 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000844 } else {
845 // If we have no LHS expression, add the ConfluenceBlock as a direct
846 // successor for the block containing the condition. Moreover, we need to
847 // reverse the order of the predecessors in the ConfluenceBlock because
848 // the RHSBlock will have been added to the succcessors already, and we
849 // want the first predecessor to the the block containing the expression
850 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000851 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000852 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000853 std::reverse(ConfluenceBlock->pred_begin(),
854 ConfluenceBlock->pred_end());
855 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000856 }
Mike Stump11289f42009-09-09 15:08:12 +0000857
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000858 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000859 Block->setTerminator(C);
860 return addStmt(C->getCond());
861}
862
Ted Kremenek93668002009-07-17 22:18:43 +0000863CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
864 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000865
Ted Kremenek93668002009-07-17 22:18:43 +0000866 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000867 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000868 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000869 }
Mike Stump11289f42009-09-09 15:08:12 +0000870
Ted Kremenek93668002009-07-17 22:18:43 +0000871 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000872
Ted Kremenek93668002009-07-17 22:18:43 +0000873 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
874 typedef llvm::SmallVector<Decl*,10> BufTy;
875 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000876
Ted Kremenek93668002009-07-17 22:18:43 +0000877 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
878 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
879 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
880 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000881
Ted Kremenek93668002009-07-17 22:18:43 +0000882 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
883 // automatically freed with the CFG.
884 DeclGroupRef DG(*I);
885 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000886 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000887 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000888
Ted Kremenek93668002009-07-17 22:18:43 +0000889 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000890 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000891 B = VisitDeclSubExpr(D);
892 }
Mike Stump11289f42009-09-09 15:08:12 +0000893
894 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000895}
Mike Stump11289f42009-09-09 15:08:12 +0000896
Ted Kremenek93668002009-07-17 22:18:43 +0000897/// VisitDeclSubExpr - Utility method to add block-level expressions for
898/// initializers in Decls.
899CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
900 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000901
Ted Kremenek93668002009-07-17 22:18:43 +0000902 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000903
Ted Kremenek93668002009-07-17 22:18:43 +0000904 if (!VD)
905 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000906
Ted Kremenek93668002009-07-17 22:18:43 +0000907 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000908
Ted Kremenek93668002009-07-17 22:18:43 +0000909 if (Init) {
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000910 AddStmtChoice::Kind k =
911 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
912 : AddStmtChoice::NotAlwaysAdd;
913 Visit(Init, AddStmtChoice(k));
Ted Kremenek93668002009-07-17 22:18:43 +0000914 }
Mike Stump11289f42009-09-09 15:08:12 +0000915
Ted Kremenek93668002009-07-17 22:18:43 +0000916 // If the type of VD is a VLA, then we must process its size expressions.
917 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
918 VA = FindVA(VA->getElementType().getTypePtr()))
919 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000920
Ted Kremenek93668002009-07-17 22:18:43 +0000921 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000922}
923
924CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000925 // We may see an if statement in the middle of a basic block, or it may be the
926 // first statement we are processing. In either case, we create a new basic
927 // block. First, we create the blocks for the then...else statements, and
928 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000929 // middle of a block, we stop processing that block. That block is then the
930 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000931
932 // The block we were proccessing is now finished. Make it the successor
933 // block.
934 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000935 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000936 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000937 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000938 }
Mike Stump31feda52009-07-17 01:31:16 +0000939
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000940 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000941 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000942
Ted Kremenek9aae5132007-08-23 21:42:29 +0000943 if (Stmt* Else = I->getElse()) {
944 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000945
Ted Kremenek9aae5132007-08-23 21:42:29 +0000946 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000947 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000948 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000949 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000950
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000951 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
952 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000953 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000954 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000955 return 0;
956 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000957 }
Mike Stump31feda52009-07-17 01:31:16 +0000958
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000959 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000960 CFGBlock* ThenBlock;
961 {
962 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +0000963 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000964 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000965 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000966 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000967
Ted Kremenek1b379512009-04-01 03:52:47 +0000968 if (!ThenBlock) {
969 // We can reach here if the "then" body has all NullStmts.
970 // Create an empty block so we can distinguish between true and false
971 // branches in path-sensitive analyses.
972 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000973 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000974 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000975 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000976 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000977 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000978 }
979
Mike Stump31feda52009-07-17 01:31:16 +0000980 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000981 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000982
Ted Kremenek9aae5132007-08-23 21:42:29 +0000983 // Set the terminator of the new block to the If statement.
984 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000985
Mike Stump773582d2009-07-23 23:25:26 +0000986 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000987 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000988
Ted Kremenek9aae5132007-08-23 21:42:29 +0000989 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000990 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
991 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000992
993 // Add the condition as the last statement in the new block. This may create
994 // new blocks as the condition may contain control-flow. Any newly created
995 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000996 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000997
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000998 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
999 // and the condition variable initialization to the CFG.
1000 if (VarDecl *VD = I->getConditionVariable()) {
1001 if (Expr *Init = VD->getInit()) {
1002 autoCreateBlock();
1003 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1004 addStmt(Init);
1005 }
1006 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001007
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001008 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001009}
Mike Stump31feda52009-07-17 01:31:16 +00001010
1011
Ted Kremenek9aae5132007-08-23 21:42:29 +00001012CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001013 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001014 //
Mike Stump31feda52009-07-17 01:31:16 +00001015 // NOTE: If a "return" appears in the middle of a block, this means that the
1016 // code afterwards is DEAD (unreachable). We still keep a basic block
1017 // for that code; a simple "mark-and-sweep" from the entry block will be
1018 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001019
1020 // Create the new block.
1021 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001022
Ted Kremenek9aae5132007-08-23 21:42:29 +00001023 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001024 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001025
1026 // Add the return statement to the block. This may create new blocks if R
1027 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001028 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001029}
1030
1031CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1032 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001033 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001034 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001035
Ted Kremenek93668002009-07-17 22:18:43 +00001036 if (!LabelBlock) // This can happen when the body is empty, i.e.
1037 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001038
Ted Kremenek93668002009-07-17 22:18:43 +00001039 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001040 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001041
1042 // Labels partition blocks, so this is the end of the basic block we were
1043 // processing (L is the block's label). Because this is label (and we have
1044 // already processed the substatement) there is no extra control-flow to worry
1045 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001046 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001047 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001048 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001049
1050 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001051 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001052
Ted Kremenek9aae5132007-08-23 21:42:29 +00001053 // This block is now the implicit successor of other blocks.
1054 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001055
Ted Kremenek9aae5132007-08-23 21:42:29 +00001056 return LabelBlock;
1057}
1058
1059CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001060 // Goto is a control-flow statement. Thus we stop processing the current
1061 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001062
Ted Kremenek9aae5132007-08-23 21:42:29 +00001063 Block = createBlock(false);
1064 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001065
1066 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001067 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001068
Ted Kremenek9aae5132007-08-23 21:42:29 +00001069 if (I == LabelMap.end())
1070 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001071 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1072 else {
1073 JumpTarget JT = I->second;
1074 AddSuccessor(Block, JT.Block);
1075 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001076
Mike Stump31feda52009-07-17 01:31:16 +00001077 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001078}
1079
1080CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001081 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001082
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001083 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1084
Mike Stump014b3ea2009-07-21 01:12:51 +00001085 // "for" is a control-flow statement. Thus we stop processing the current
1086 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001087 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001088 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001089 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001090 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001091 } else
1092 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001093
Ted Kremenek304a9532010-05-21 20:30:15 +00001094 // Save the current value for the break targets.
1095 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001096 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
1097 BreakJumpTarget = JumpTarget(LoopSuccessor, LoopBeginScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001098
Mike Stump31feda52009-07-17 01:31:16 +00001099 // Because of short-circuit evaluation, the condition of the loop can span
1100 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1101 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001102 CFGBlock* ExitConditionBlock = createBlock(false);
1103 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001104
Ted Kremenek81e14852007-08-27 19:46:09 +00001105 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001106 ExitConditionBlock->setTerminator(F);
1107
1108 // Now add the actual condition to the condition block. Because the condition
1109 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001110 if (Stmt* C = F->getCond()) {
1111 Block = ExitConditionBlock;
1112 EntryConditionBlock = addStmt(C);
Ted Kremenek7b31a612010-09-15 07:01:20 +00001113 assert(Block == EntryConditionBlock ||
1114 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001115
1116 // If this block contains a condition variable, add both the condition
1117 // variable and initializer to the CFG.
1118 if (VarDecl *VD = F->getConditionVariable()) {
1119 if (Expr *Init = VD->getInit()) {
1120 autoCreateBlock();
1121 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1122 EntryConditionBlock = addStmt(Init);
1123 assert(Block == EntryConditionBlock);
1124 }
1125 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001126
Ted Kremenek55957a82009-05-02 00:13:27 +00001127 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001128 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001129 return 0;
1130 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001131 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001132
Mike Stump31feda52009-07-17 01:31:16 +00001133 // The condition block is the implicit successor for the loop body as well as
1134 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001135 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001136
Mike Stump773582d2009-07-23 23:25:26 +00001137 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001138 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001139
Mike Stump773582d2009-07-23 23:25:26 +00001140 if (F->getCond())
1141 KnownVal = TryEvaluateBool(F->getCond());
1142
Ted Kremenek9aae5132007-08-23 21:42:29 +00001143 // Now create the loop body.
1144 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001145 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001146
Ted Kremenek304a9532010-05-21 20:30:15 +00001147 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001148 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1149 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001150
Ted Kremeneke9610502007-08-30 18:39:40 +00001151 // Create a new block to contain the (bottom) of the loop body.
1152 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001153
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001154 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001155 // Generate increment code in its own basic block. This is the target of
1156 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001157 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001158 } else {
1159 // No increment code. Create a special, empty, block that is used as the
1160 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001161 assert(Succ == EntryConditionBlock);
1162 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001163 }
Mike Stump31feda52009-07-17 01:31:16 +00001164
Ted Kremenek902393b2009-04-28 00:51:56 +00001165 // Finish up the increment (or empty) block if it hasn't been already.
1166 if (Block) {
1167 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001168 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001169 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001170 Block = 0;
1171 }
Mike Stump31feda52009-07-17 01:31:16 +00001172
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001173 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001174
Ted Kremenek902393b2009-04-28 00:51:56 +00001175 // The starting block for the loop increment is the block that should
1176 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001177 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001178
Mike Stump31feda52009-07-17 01:31:16 +00001179 // Now populate the body block, and in the process create new blocks as we
1180 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001181 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001182
1183 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001184 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001185 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001186 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001187
Ted Kremenek30754282009-07-24 04:47:11 +00001188 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001189 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001190 }
Mike Stump31feda52009-07-17 01:31:16 +00001191
Ted Kremenek30754282009-07-24 04:47:11 +00001192 // Link up the condition block with the code that follows the loop. (the
1193 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001194 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001195
Ted Kremenek9aae5132007-08-23 21:42:29 +00001196 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001197 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001198 if (Stmt* I = F->getInit()) {
1199 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001200 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001201 } else {
1202 // There is no loop initialization. We are thus basically a while loop.
1203 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001204 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001205 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001206 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001207 }
1208}
1209
Ted Kremenek5868ec62010-04-11 17:02:10 +00001210CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1211 if (asc.alwaysAdd()) {
1212 autoCreateBlock();
1213 AppendStmt(Block, M, asc);
1214 }
1215 return Visit(M->getBase(),
1216 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1217 : AddStmtChoice::AsLValueNotAlwaysAdd);
1218}
1219
Ted Kremenek9d56e642008-11-11 17:10:00 +00001220CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1221 // Objective-C fast enumeration 'for' statements:
1222 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1223 //
1224 // for ( Type newVariable in collection_expression ) { statements }
1225 //
1226 // becomes:
1227 //
1228 // prologue:
1229 // 1. collection_expression
1230 // T. jump to loop_entry
1231 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001232 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001233 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1234 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1235 // TB:
1236 // statements
1237 // T. jump to loop_entry
1238 // FB:
1239 // what comes after
1240 //
1241 // and
1242 //
1243 // Type existingItem;
1244 // for ( existingItem in expression ) { statements }
1245 //
1246 // becomes:
1247 //
Mike Stump31feda52009-07-17 01:31:16 +00001248 // the same with newVariable replaced with existingItem; the binding works
1249 // the same except that for one ObjCForCollectionStmt::getElement() returns
1250 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001251 //
Mike Stump31feda52009-07-17 01:31:16 +00001252
Ted Kremenek9d56e642008-11-11 17:10:00 +00001253 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001254
Ted Kremenek9d56e642008-11-11 17:10:00 +00001255 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001256 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001257 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001258 LoopSuccessor = Block;
1259 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001260 } else
1261 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001262
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001263 // Build the condition blocks.
1264 CFGBlock* ExitConditionBlock = createBlock(false);
1265 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001266
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001267 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001268 ExitConditionBlock->setTerminator(S);
1269
1270 // The last statement in the block should be the ObjCForCollectionStmt, which
1271 // performs the actual binding to 'element' and determines if there are any
1272 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001273 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001274 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001275
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001276 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001277 // generate new blocks as necesary. We DON'T add the statement by default to
1278 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001279 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001280 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001281 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001282 return 0;
1283 Block = 0;
1284 }
Mike Stump31feda52009-07-17 01:31:16 +00001285
1286 // The condition block is the implicit successor for the loop body as well as
1287 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001288 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001289
Ted Kremenek9d56e642008-11-11 17:10:00 +00001290 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001291 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001292 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001293 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1294 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1295 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001296
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001297 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1298 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001299
Ted Kremenek93668002009-07-17 22:18:43 +00001300 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001301
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001302 if (!BodyBlock)
1303 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001304 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001305 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001306 return 0;
1307 }
Mike Stump31feda52009-07-17 01:31:16 +00001308
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001309 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001310 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001311 }
Mike Stump31feda52009-07-17 01:31:16 +00001312
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001313 // Link up the condition block with the code that follows the loop.
1314 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001315 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001316
Ted Kremenek9d56e642008-11-11 17:10:00 +00001317 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001318 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001319 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001320}
1321
Ted Kremenek49805452009-05-02 01:49:13 +00001322CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1323 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001324
Ted Kremenek49805452009-05-02 01:49:13 +00001325 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001326 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001327
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001328 // The sync body starts its own basic block. This makes it a little easier
1329 // for diagnostic clients.
1330 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001331 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001332 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001333
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001334 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001335 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001336 }
Mike Stump31feda52009-07-17 01:31:16 +00001337
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001338 // Add the @synchronized to the CFG.
1339 autoCreateBlock();
1340 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1341
Ted Kremenek49805452009-05-02 01:49:13 +00001342 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001343 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001344}
Mike Stump31feda52009-07-17 01:31:16 +00001345
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001346CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001347 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001348 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001349}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001350
Ted Kremenek9aae5132007-08-23 21:42:29 +00001351CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001352 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001353
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001354 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1355
Mike Stump014b3ea2009-07-21 01:12:51 +00001356 // "while" is a control-flow statement. Thus we stop processing the current
1357 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001358 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001359 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001360 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001361 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001362 } else
1363 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001364
1365 // Because of short-circuit evaluation, the condition of the loop can span
1366 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1367 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001368 CFGBlock* ExitConditionBlock = createBlock(false);
1369 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001370
Ted Kremenek81e14852007-08-27 19:46:09 +00001371 // Set the terminator for the "exit" condition block.
1372 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001373
1374 // Now add the actual condition to the condition block. Because the condition
1375 // itself may contain control-flow, new blocks may be created. Thus we update
1376 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001377 if (Stmt* C = W->getCond()) {
1378 Block = ExitConditionBlock;
1379 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001380 assert(Block == EntryConditionBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001381
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001382 // If this block contains a condition variable, add both the condition
1383 // variable and initializer to the CFG.
1384 if (VarDecl *VD = W->getConditionVariable()) {
1385 if (Expr *Init = VD->getInit()) {
1386 autoCreateBlock();
1387 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1388 EntryConditionBlock = addStmt(Init);
1389 assert(Block == EntryConditionBlock);
1390 }
1391 }
1392
Ted Kremenek55957a82009-05-02 00:13:27 +00001393 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001394 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001395 return 0;
1396 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001397 }
Mike Stump31feda52009-07-17 01:31:16 +00001398
1399 // The condition block is the implicit successor for the loop body as well as
1400 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001401 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001402
Mike Stump773582d2009-07-23 23:25:26 +00001403 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001404 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001405
Ted Kremenek9aae5132007-08-23 21:42:29 +00001406 // Process the loop body.
1407 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001408 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001409
1410 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001411 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1412 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1413 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001414
Mike Stump31feda52009-07-17 01:31:16 +00001415 // Create an empty block to represent the transition block for looping back
1416 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001417 Block = 0;
1418 assert(Succ == EntryConditionBlock);
1419 Succ = createBlock();
1420 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001421 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001422
Ted Kremenek9aae5132007-08-23 21:42:29 +00001423 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001424 BreakJumpTarget = JumpTarget(LoopSuccessor, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001425
Ted Kremenek9aae5132007-08-23 21:42:29 +00001426 // NULL out Block to force lazy instantiation of blocks for the body.
1427 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001428
Ted Kremenek9aae5132007-08-23 21:42:29 +00001429 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001430 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001431
Ted Kremeneke9610502007-08-30 18:39:40 +00001432 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001433 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001434 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001435 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001436 return 0;
1437 }
Mike Stump31feda52009-07-17 01:31:16 +00001438
Ted Kremenek30754282009-07-24 04:47:11 +00001439 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001440 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001441 }
Mike Stump31feda52009-07-17 01:31:16 +00001442
Ted Kremenek30754282009-07-24 04:47:11 +00001443 // Link up the condition block with the code that follows the loop. (the
1444 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001445 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001446
1447 // There can be no more statements in the condition block since we loop back
1448 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001449 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001450
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001451 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001452 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001453 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001454}
Mike Stump11289f42009-09-09 15:08:12 +00001455
1456
Ted Kremenek93668002009-07-17 22:18:43 +00001457CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1458 // FIXME: For now we pretend that @catch and the code it contains does not
1459 // exit.
1460 return Block;
1461}
Mike Stump31feda52009-07-17 01:31:16 +00001462
Ted Kremenek93041ba2008-12-09 20:20:09 +00001463CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1464 // FIXME: This isn't complete. We basically treat @throw like a return
1465 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001466
Ted Kremenek0868eea2009-09-24 18:45:41 +00001467 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001468 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001469 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001470
Ted Kremenek93041ba2008-12-09 20:20:09 +00001471 // Create the new block.
1472 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001473
Ted Kremenek93041ba2008-12-09 20:20:09 +00001474 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001475 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001476
1477 // Add the statement to the block. This may create new blocks if S contains
1478 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001479 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001480}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001481
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001482CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001483 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001484 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001485 return 0;
1486
1487 // Create the new block.
1488 Block = createBlock(false);
1489
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001490 if (TryTerminatedBlock)
1491 // The current try statement is the only successor.
1492 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001493 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001494 // otherwise the Exit block is the only successor.
1495 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001496
1497 // Add the statement to the block. This may create new blocks if S contains
1498 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001499 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001500}
1501
Ted Kremenek93668002009-07-17 22:18:43 +00001502CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001503 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001504
Mike Stump8d50b6a2009-07-21 01:27:50 +00001505 // "do...while" is a control-flow statement. Thus we stop processing the
1506 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001507 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001508 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001509 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001510 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001511 } else
1512 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001513
1514 // Because of short-circuit evaluation, the condition of the loop can span
1515 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1516 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001517 CFGBlock* ExitConditionBlock = createBlock(false);
1518 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001519
Ted Kremenek81e14852007-08-27 19:46:09 +00001520 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001521 ExitConditionBlock->setTerminator(D);
1522
1523 // Now add the actual condition to the condition block. Because the condition
1524 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001525 if (Stmt* C = D->getCond()) {
1526 Block = ExitConditionBlock;
1527 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001528 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001529 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001530 return 0;
1531 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001532 }
Mike Stump31feda52009-07-17 01:31:16 +00001533
Ted Kremeneka1523a32008-02-27 07:20:00 +00001534 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001535 Succ = EntryConditionBlock;
1536
Mike Stump773582d2009-07-23 23:25:26 +00001537 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001538 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001539
Ted Kremenek9aae5132007-08-23 21:42:29 +00001540 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001541 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001542 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001543 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001544
Ted Kremenek9aae5132007-08-23 21:42:29 +00001545 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001546 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1547 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1548 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001549
Ted Kremenek9aae5132007-08-23 21:42:29 +00001550 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001551 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001552
Ted Kremenek9aae5132007-08-23 21:42:29 +00001553 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001554 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001555
Ted Kremenek9aae5132007-08-23 21:42:29 +00001556 // NULL out Block to force lazy instantiation of blocks for the body.
1557 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001558
Ted Kremenek9aae5132007-08-23 21:42:29 +00001559 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001560 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001561
Ted Kremeneke9610502007-08-30 18:39:40 +00001562 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001563 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001564 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001565 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001566 return 0;
1567 }
Mike Stump31feda52009-07-17 01:31:16 +00001568
Ted Kremenek110974d2010-08-17 20:59:56 +00001569 if (!KnownVal.isFalse()) {
1570 // Add an intermediate block between the BodyBlock and the
1571 // ExitConditionBlock to represent the "loop back" transition. Create an
1572 // empty block to represent the transition block for looping back to the
1573 // head of the loop.
1574 // FIXME: Can we do this more efficiently without adding another block?
1575 Block = NULL;
1576 Succ = BodyBlock;
1577 CFGBlock *LoopBackBlock = createBlock();
1578 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001579
Ted Kremenek110974d2010-08-17 20:59:56 +00001580 // Add the loop body entry as a successor to the condition.
1581 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1582 }
1583 else
1584 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001585 }
Mike Stump31feda52009-07-17 01:31:16 +00001586
Ted Kremenek30754282009-07-24 04:47:11 +00001587 // Link up the condition block with the code that follows the loop.
1588 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001589 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001590
1591 // There can be no more statements in the body block(s) since we loop back to
1592 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001593 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001594
Ted Kremenek9aae5132007-08-23 21:42:29 +00001595 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001596 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001597 return BodyBlock;
1598}
1599
1600CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1601 // "continue" is a control-flow statement. Thus we stop processing the
1602 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001603 if (badCFG)
1604 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001605
Ted Kremenek9aae5132007-08-23 21:42:29 +00001606 // Now create a new block that ends with the continue statement.
1607 Block = createBlock(false);
1608 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001609
Ted Kremenek9aae5132007-08-23 21:42:29 +00001610 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001611 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001612 if (ContinueJumpTarget.Block) {
1613 AddSuccessor(Block, ContinueJumpTarget.Block);
1614 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00001615 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001616
Ted Kremenek9aae5132007-08-23 21:42:29 +00001617 return Block;
1618}
Mike Stump11289f42009-09-09 15:08:12 +00001619
Ted Kremenek0747de62009-07-18 00:47:21 +00001620CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001621 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001622
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001623 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001624 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001625 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001626 }
Mike Stump11289f42009-09-09 15:08:12 +00001627
Ted Kremenek93668002009-07-17 22:18:43 +00001628 // VLA types have expressions that must be evaluated.
1629 if (E->isArgumentType()) {
1630 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1631 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1632 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001633 }
Mike Stump11289f42009-09-09 15:08:12 +00001634
Mike Stump31feda52009-07-17 01:31:16 +00001635 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001636}
Mike Stump11289f42009-09-09 15:08:12 +00001637
Ted Kremenek93668002009-07-17 22:18:43 +00001638/// VisitStmtExpr - Utility method to handle (nested) statement
1639/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001640CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1641 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001642 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001643 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001644 }
Ted Kremenek93668002009-07-17 22:18:43 +00001645 return VisitCompoundStmt(SE->getSubStmt());
1646}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001647
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001648CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001649 // "switch" is a control-flow statement. Thus we stop processing the current
1650 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001651 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001652
Ted Kremenek9aae5132007-08-23 21:42:29 +00001653 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001654 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001655 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001656 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001657 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001658
1659 // Save the current "switch" context.
1660 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001661 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001662 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00001663
Mike Stump31feda52009-07-17 01:31:16 +00001664 // Set the "default" case to be the block after the switch statement. If the
1665 // switch statement contains a "default:", this value will be overwritten with
1666 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001667 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001668
Ted Kremenek9aae5132007-08-23 21:42:29 +00001669 // Create a new block that will contain the switch statement.
1670 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001671
Ted Kremenek9aae5132007-08-23 21:42:29 +00001672 // Now process the switch body. The code after the switch is the implicit
1673 // successor.
1674 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001675 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001676
1677 // When visiting the body, the case statements should automatically get linked
1678 // up to the switch. We also don't keep a pointer to the body, since all
1679 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001680 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001681 Block = NULL;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001682 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001683 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001684 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001685 return 0;
1686 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001687
Mike Stump31feda52009-07-17 01:31:16 +00001688 // If we have no "default:" case, the default transition is to the code
1689 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001690 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001691
Ted Kremenek81e14852007-08-27 19:46:09 +00001692 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001693 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001694 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001695 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001696 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001697
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001698 // Finally, if the SwitchStmt contains a condition variable, add both the
1699 // SwitchStmt and the condition variable initialization to the CFG.
1700 if (VarDecl *VD = Terminator->getConditionVariable()) {
1701 if (Expr *Init = VD->getInit()) {
1702 autoCreateBlock();
1703 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1704 addStmt(Init);
1705 }
1706 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001707
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001708 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001709}
1710
Ted Kremenek93668002009-07-17 22:18:43 +00001711CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001712 // CaseStmts are essentially labels, so they are the first statement in a
1713 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00001714 CFGBlock *TopBlock = 0, *LastBlock = 0;
1715
1716 if (Stmt *Sub = CS->getSubStmt()) {
1717 // For deeply nested chains of CaseStmts, instead of doing a recursion
1718 // (which can blow out the stack), manually unroll and create blocks
1719 // along the way.
1720 while (isa<CaseStmt>(Sub)) {
1721 CFGBlock *CurrentBlock = createBlock(false);
1722 CurrentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00001723
Ted Kremenek60fa6572010-08-04 23:54:30 +00001724 if (TopBlock)
1725 AddSuccessor(LastBlock, CurrentBlock);
1726 else
1727 TopBlock = CurrentBlock;
1728
1729 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
1730 LastBlock = CurrentBlock;
1731
1732 CS = cast<CaseStmt>(Sub);
1733 Sub = CS->getSubStmt();
1734 }
1735
1736 addStmt(Sub);
1737 }
Mike Stump11289f42009-09-09 15:08:12 +00001738
Ted Kremenek55e91e82007-08-30 18:48:11 +00001739 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001740 if (!CaseBlock)
1741 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001742
1743 // Cases statements partition blocks, so this is the top of the basic block we
1744 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001745 CaseBlock->setLabel(CS);
1746
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001747 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001748 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001749
1750 // Add this block to the list of successors for the block with the switch
1751 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001752 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001753 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001754
Ted Kremenek9aae5132007-08-23 21:42:29 +00001755 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1756 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001757
Ted Kremenek60fa6572010-08-04 23:54:30 +00001758 if (TopBlock) {
1759 AddSuccessor(LastBlock, CaseBlock);
1760 Succ = TopBlock;
1761 }
1762 else {
1763 // This block is now the implicit successor of other blocks.
1764 Succ = CaseBlock;
1765 }
Mike Stump31feda52009-07-17 01:31:16 +00001766
Ted Kremenek60fa6572010-08-04 23:54:30 +00001767 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001768}
Mike Stump31feda52009-07-17 01:31:16 +00001769
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001770CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001771 if (Terminator->getSubStmt())
1772 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001773
Ted Kremenek654c78f2008-02-13 22:05:39 +00001774 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001775
1776 if (!DefaultCaseBlock)
1777 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001778
1779 // Default statements partition blocks, so this is the top of the basic block
1780 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001781 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001782
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001783 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001784 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001785
Mike Stump31feda52009-07-17 01:31:16 +00001786 // Unlike case statements, we don't add the default block to the successors
1787 // for the switch statement immediately. This is done when we finish
1788 // processing the switch statement. This allows for the default case
1789 // (including a fall-through to the code after the switch statement) to always
1790 // be the last successor of a switch-terminated block.
1791
Ted Kremenek654c78f2008-02-13 22:05:39 +00001792 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1793 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001794
Ted Kremenek654c78f2008-02-13 22:05:39 +00001795 // This block is now the implicit successor of other blocks.
1796 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001797
1798 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001799}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001800
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001801CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1802 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1803 // current block.
1804 CFGBlock* TrySuccessor = NULL;
1805
1806 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001807 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001808 return 0;
1809 TrySuccessor = Block;
1810 } else TrySuccessor = Succ;
1811
Mike Stump0bdba6c2010-01-20 01:15:34 +00001812 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001813
1814 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00001815 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001816 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00001817 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001818
Mike Stump0bdba6c2010-01-20 01:15:34 +00001819 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001820 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1821 // The code after the try is the implicit successor.
1822 Succ = TrySuccessor;
1823 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001824 if (CS->getExceptionDecl() == 0) {
1825 HasCatchAll = true;
1826 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001827 Block = NULL;
1828 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1829 if (CatchBlock == 0)
1830 return 0;
1831 // Add this block to the list of successors for the block with the try
1832 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00001833 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001834 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00001835 if (!HasCatchAll) {
1836 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00001837 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001838 else
Mike Stump845384a2010-01-20 01:30:58 +00001839 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00001840 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001841
1842 // The code after the try is the implicit successor.
1843 Succ = TrySuccessor;
1844
Mike Stump845384a2010-01-20 01:30:58 +00001845 // Save the current "try" context.
1846 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1847 TryTerminatedBlock = NewTryTerminatedBlock;
1848
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001849 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001850 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001851 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001852 return Block;
1853}
1854
1855CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1856 // CXXCatchStmt are treated like labels, so they are the first statement in a
1857 // block.
1858
1859 if (CS->getHandlerBlock())
1860 addStmt(CS->getHandlerBlock());
1861
1862 CFGBlock* CatchBlock = Block;
1863 if (!CatchBlock)
1864 CatchBlock = createBlock();
1865
1866 CatchBlock->setLabel(CS);
1867
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001868 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001869 return 0;
1870
1871 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1872 Block = NULL;
1873
1874 return CatchBlock;
1875}
1876
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001877CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00001878 AddStmtChoice asc) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001879 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001880 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00001881 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001882 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00001883 return VisitChildren(C);
1884}
1885
Ted Kremenekeda180e22007-08-28 19:26:49 +00001886CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001887 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001888 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001889
Ted Kremenekeda180e22007-08-28 19:26:49 +00001890 if (!IBlock) {
1891 IBlock = createBlock(false);
1892 cfg->setIndirectGotoBlock(IBlock);
1893 }
Mike Stump31feda52009-07-17 01:31:16 +00001894
Ted Kremenekeda180e22007-08-28 19:26:49 +00001895 // IndirectGoto is a control-flow statement. Thus we stop processing the
1896 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001897 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001898 return 0;
1899
Ted Kremenekeda180e22007-08-28 19:26:49 +00001900 Block = createBlock(false);
1901 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001902 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001903 return addStmt(I->getTarget());
1904}
1905
Ted Kremenek04cca642007-08-23 21:26:19 +00001906} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001907
Mike Stump31feda52009-07-17 01:31:16 +00001908/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1909/// no successors or predecessors. If this is the first block created in the
1910/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001911CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001912 bool first_block = begin() == end();
1913
1914 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001915 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1916 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1917 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001918
1919 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001920 if (first_block)
1921 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001922
1923 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001924 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001925}
1926
Ted Kremenek889073f2007-08-23 16:51:22 +00001927/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1928/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00001929CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001930 BuildOptions BO) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001931 CFGBuilder Builder;
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001932 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek889073f2007-08-23 16:51:22 +00001933}
1934
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001935//===----------------------------------------------------------------------===//
1936// CFG: Queries for BlkExprs.
1937//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001938
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001939namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001940 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001941}
1942
Ted Kremenekbff98442009-12-23 23:37:10 +00001943static void FindSubExprAssignments(Stmt *S,
1944 llvm::SmallPtrSet<Expr*,50>& Set) {
1945 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001946 return;
Mike Stump31feda52009-07-17 01:31:16 +00001947
Ted Kremenekbff98442009-12-23 23:37:10 +00001948 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001949 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00001950 if (!child)
1951 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001952
Ted Kremenekbff98442009-12-23 23:37:10 +00001953 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001954 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001955
Ted Kremenekbff98442009-12-23 23:37:10 +00001956 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00001957 }
1958}
1959
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001960static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1961 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001962
1963 // Look for assignments that are used as subexpressions. These are the only
1964 // assignments that we want to *possibly* register as a block-level
1965 // expression. Basically, if an assignment occurs both in a subexpression and
1966 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001967 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001968
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001969 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001970 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00001971 if (CFGStmt S = BI->getAs<CFGStmt>())
1972 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001973
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001974 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001975
1976 // Iterate over the statements again on identify the Expr* and Stmt* at the
1977 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001978
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00001979 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
1980 CFGStmt CS = BI->getAs<CFGStmt>();
1981 if (!CS.isValid())
1982 continue;
1983 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00001984
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001985 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001986 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001987 // expression are really "statements" whose value is never used by
1988 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001989 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001990 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001991 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1992 // Special handling for statement expressions. The last statement in
1993 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001994 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001995 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001996 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001997 (*M)[C->body_back()] = x;
1998 }
1999 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002000
Ted Kremenek95a123c2008-01-26 00:03:27 +00002001 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002002 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002003 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002004 }
Mike Stump31feda52009-07-17 01:31:16 +00002005
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002006 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002007
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002008 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002009
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002010 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002011 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002012 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002013 }
2014 }
Mike Stump31feda52009-07-17 01:31:16 +00002015
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002016 return M;
2017}
2018
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002019CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2020 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002021 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002022
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002023 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002024 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002025 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002026}
2027
2028unsigned CFG::getNumBlkExprs() {
2029 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2030 return M->size();
2031 else {
2032 // We assume callers interested in the number of BlkExprs will want
2033 // the map constructed if it doesn't already exist.
2034 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2035 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
2036 }
2037}
2038
Ted Kremenek6065ef62008-04-28 18:00:46 +00002039//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002040// Filtered walking of the CFG.
2041//===----------------------------------------------------------------------===//
2042
2043bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002044 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002045
2046 if (F.IgnoreDefaultsWithCoveredEnums) {
2047 // If the 'To' has no label or is labeled but the label isn't a
2048 // CaseStmt then filter this edge.
2049 if (const SwitchStmt *S =
Ted Kremenekf146cd12010-09-09 02:57:48 +00002050 dyn_cast_or_null<SwitchStmt>(From->getTerminator())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002051 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00002052 const Stmt *L = To->getLabel();
2053 if (!L || !isa<CaseStmt>(L))
2054 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002055 }
2056 }
2057 }
2058
2059 return false;
2060}
2061
2062//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002063// Cleanup: CFG dstor.
2064//===----------------------------------------------------------------------===//
2065
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002066CFG::~CFG() {
2067 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2068}
Mike Stump31feda52009-07-17 01:31:16 +00002069
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002070//===----------------------------------------------------------------------===//
2071// CFG pretty printing
2072//===----------------------------------------------------------------------===//
2073
Ted Kremenek7e776b12007-08-22 18:22:34 +00002074namespace {
2075
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002076class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002077 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002078 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002079 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002080 DeclMapTy DeclMap;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002081 signed CurrentBlock;
2082 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002083 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002084public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002085
Chris Lattnerc61089a2009-06-30 01:26:17 +00002086 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2087 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002088 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2089 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002090 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002091 BI != BEnd; ++BI, ++j ) {
2092 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2093 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2094 StmtMap[SE] = P;
2095
2096 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2097 DeclMap[DS->getSingleDecl()] = P;
2098
2099 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2100 if (VarDecl* VD = IS->getConditionVariable())
2101 DeclMap[VD] = P;
2102
2103 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2104 if (VarDecl* VD = FS->getConditionVariable())
2105 DeclMap[VD] = P;
2106
2107 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2108 if (VarDecl* VD = WS->getConditionVariable())
2109 DeclMap[VD] = P;
2110
2111 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2112 if (VarDecl* VD = SS->getConditionVariable())
2113 DeclMap[VD] = P;
2114
2115 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2116 if (VarDecl* VD = CS->getExceptionDecl())
2117 DeclMap[VD] = P;
2118 }
2119 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002120 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002121 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002122 }
Mike Stump31feda52009-07-17 01:31:16 +00002123
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002124 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00002125
Chris Lattnerc61089a2009-06-30 01:26:17 +00002126 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002127 void setBlockID(signed i) { CurrentBlock = i; }
2128 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00002129
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002130 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2131 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002132
2133 if (I == StmtMap.end())
2134 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002135
2136 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00002137 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002138 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002139 }
Mike Stump31feda52009-07-17 01:31:16 +00002140
Ted Kremenek60983dc2010-01-19 20:52:05 +00002141 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002142 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002143 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002144
2145 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2146 DeclMapTy::iterator I = DeclMap.find(D);
2147
2148 if (I == DeclMap.end())
2149 return false;
2150
2151 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2152 && I->second.second == CurrentStmt) {
2153 return false;
2154 }
2155
2156 OS << "[B" << I->second.first << "." << I->second.second << "]";
2157 return true;
2158 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002159};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002160} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002161
Chris Lattnerc61089a2009-06-30 01:26:17 +00002162
2163namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002164class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002165 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002166
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002167 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002168 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002169 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002170public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002171 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002172 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002173 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002174
Ted Kremenek9aae5132007-08-23 21:42:29 +00002175 void VisitIfStmt(IfStmt* I) {
2176 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002177 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002178 }
Mike Stump31feda52009-07-17 01:31:16 +00002179
Ted Kremenek9aae5132007-08-23 21:42:29 +00002180 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002181 void VisitStmt(Stmt* Terminator) {
2182 Terminator->printPretty(OS, Helper, Policy);
2183 }
2184
Ted Kremenek9aae5132007-08-23 21:42:29 +00002185 void VisitForStmt(ForStmt* F) {
2186 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002187 if (F->getInit())
2188 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002189 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002190 if (Stmt* C = F->getCond())
2191 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002192 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002193 if (F->getInc())
2194 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002195 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002196 }
Mike Stump31feda52009-07-17 01:31:16 +00002197
Ted Kremenek9aae5132007-08-23 21:42:29 +00002198 void VisitWhileStmt(WhileStmt* W) {
2199 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002200 if (Stmt* C = W->getCond())
2201 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002202 }
Mike Stump31feda52009-07-17 01:31:16 +00002203
Ted Kremenek9aae5132007-08-23 21:42:29 +00002204 void VisitDoStmt(DoStmt* D) {
2205 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002206 if (Stmt* C = D->getCond())
2207 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002208 }
Mike Stump31feda52009-07-17 01:31:16 +00002209
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002210 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002211 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002212 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002213 }
Mike Stump31feda52009-07-17 01:31:16 +00002214
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002215 void VisitCXXTryStmt(CXXTryStmt* CS) {
2216 OS << "try ...";
2217 }
2218
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002219 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002220 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002221 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002222 }
Mike Stump31feda52009-07-17 01:31:16 +00002223
Ted Kremenek391f94a2007-08-31 22:29:13 +00002224 void VisitChooseExpr(ChooseExpr* C) {
2225 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002226 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002227 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002228 }
Mike Stump31feda52009-07-17 01:31:16 +00002229
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002230 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2231 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002232 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002233 }
Mike Stump31feda52009-07-17 01:31:16 +00002234
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002235 void VisitBinaryOperator(BinaryOperator* B) {
2236 if (!B->isLogicalOp()) {
2237 VisitExpr(B);
2238 return;
2239 }
Mike Stump31feda52009-07-17 01:31:16 +00002240
Douglas Gregor7de59662009-05-29 20:38:28 +00002241 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002242
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002243 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002244 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002245 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002246 return;
John McCalle3027922010-08-25 11:45:40 +00002247 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002248 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002249 return;
2250 default:
2251 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00002252 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002253 }
Mike Stump31feda52009-07-17 01:31:16 +00002254
Ted Kremenek12687ff2007-08-27 21:54:41 +00002255 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002256 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002257 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00002258};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002259} // end anonymous namespace
2260
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002261static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00002262 const CFGElement &E) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002263 if (CFGStmt CS = E.getAs<CFGStmt>()) {
2264 Stmt *S = CS;
2265
2266 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00002267
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002268 // special printing for statement-expressions.
2269 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
2270 CompoundStmt* Sub = SE->getSubStmt();
2271
2272 if (Sub->child_begin() != Sub->child_end()) {
2273 OS << "({ ... ; ";
2274 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
2275 OS << " })\n";
2276 return;
2277 }
2278 }
2279 // special printing for comma expressions.
2280 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
2281 if (B->getOpcode() == BO_Comma) {
2282 OS << "... , ";
2283 Helper->handledStmt(B->getRHS(),OS);
2284 OS << '\n';
2285 return;
2286 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002287 }
2288 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002289 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00002290
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002291 if (isa<CXXOperatorCallExpr>(S)) {
2292 OS << " (OperatorCall)";
Mike Stump31feda52009-07-17 01:31:16 +00002293 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002294 else if (isa<CXXBindTemporaryExpr>(S)) {
2295 OS << " (BindTemporary)";
2296 }
Mike Stump31feda52009-07-17 01:31:16 +00002297
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002298 // Expressions need a newline.
2299 if (isa<Expr>(S))
2300 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002301
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002302 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
2303 CXXBaseOrMemberInitializer* I = IE;
2304 if (I->isBaseInitializer())
2305 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
2306 else OS << I->getMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00002307
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002308 OS << "(";
2309 if (Expr* IE = I->getInit())
2310 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2311 OS << ")";
2312
2313 if (I->isBaseInitializer())
2314 OS << " (Base initializer)\n";
2315 else OS << " (Member initializer)\n";
2316
2317 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
2318 VarDecl* VD = DE.getVarDecl();
2319 Helper->handleDecl(VD, OS);
2320
2321 Type* T = VD->getType().getTypePtr();
2322 if (const ReferenceType* RT = T->getAs<ReferenceType>())
2323 T = RT->getPointeeType().getTypePtr();
2324
2325 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
2326 OS << " (Implicit destructor)\n";
2327 }
2328 }
Mike Stump31feda52009-07-17 01:31:16 +00002329
Chris Lattnerc61089a2009-06-30 01:26:17 +00002330static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2331 const CFGBlock& B,
2332 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00002333
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002334 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00002335
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002336 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00002337 OS << "\n [ B" << B.getBlockID();
2338
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002339 if (&B == &cfg->getEntry())
2340 OS << " (ENTRY) ]\n";
2341 else if (&B == &cfg->getExit())
2342 OS << " (EXIT) ]\n";
2343 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002344 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002345 else
2346 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00002347
Ted Kremenek71eca012007-08-29 23:20:49 +00002348 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00002349 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002350
2351 if (print_edges)
2352 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002353
Mike Stump92244b02010-01-19 22:00:14 +00002354 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002355 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00002356 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00002357 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002358 C->getLHS()->printPretty(OS, Helper,
2359 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002360 if (C->getRHS()) {
2361 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002362 C->getRHS()->printPretty(OS, Helper,
2363 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002364 }
Mike Stump92244b02010-01-19 22:00:14 +00002365 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002366 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00002367 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002368 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00002369 if (CS->getExceptionDecl())
2370 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2371 0);
2372 else
2373 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002374 OS << ")";
2375
2376 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002377 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00002378
Ted Kremenek71eca012007-08-29 23:20:49 +00002379 OS << ":\n";
2380 }
Mike Stump31feda52009-07-17 01:31:16 +00002381
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002382 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002383 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00002384
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002385 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2386 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00002387
Ted Kremenek71eca012007-08-29 23:20:49 +00002388 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002389 if (print_edges)
2390 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002391
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002392 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002393
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002394 if (Helper)
2395 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002396
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002397 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002398 }
Mike Stump31feda52009-07-17 01:31:16 +00002399
Ted Kremenek71eca012007-08-29 23:20:49 +00002400 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002401 if (B.getTerminator()) {
2402 if (print_edges)
2403 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002404
Ted Kremenek71eca012007-08-29 23:20:49 +00002405 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002406
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002407 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002408
Chris Lattnerc61089a2009-06-30 01:26:17 +00002409 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2410 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002411 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00002412 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002413 }
Mike Stump31feda52009-07-17 01:31:16 +00002414
Ted Kremenek71eca012007-08-29 23:20:49 +00002415 if (print_edges) {
2416 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002417 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002418 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002419
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002420 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2421 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002422
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002423 if (i == 8 || (i-8) == 0)
2424 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002425
Ted Kremenek71eca012007-08-29 23:20:49 +00002426 OS << " B" << (*I)->getBlockID();
2427 }
Mike Stump31feda52009-07-17 01:31:16 +00002428
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002429 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002430
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002431 // Print the successors of this block.
2432 OS << " Successors (" << B.succ_size() << "):";
2433 i = 0;
2434
2435 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2436 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002437
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002438 if (i == 8 || (i-8) % 10 == 0)
2439 OS << "\n ";
2440
Mike Stump0d76d072009-07-20 23:24:15 +00002441 if (*I)
2442 OS << " B" << (*I)->getBlockID();
2443 else
2444 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002445 }
Mike Stump31feda52009-07-17 01:31:16 +00002446
Ted Kremenek71eca012007-08-29 23:20:49 +00002447 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002448 }
Mike Stump31feda52009-07-17 01:31:16 +00002449}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002450
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002451
2452/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002453void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002454
2455/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002456void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2457 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002458
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002459 // Print the entry block.
2460 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002461
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002462 // Iterate through the CFGBlocks and print them one by one.
2463 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2464 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002465 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002466 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002467
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002468 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002469 }
Mike Stump31feda52009-07-17 01:31:16 +00002470
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002471 // Print the exit block.
2472 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002473 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002474}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002475
2476/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002477void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2478 print(llvm::errs(), cfg, LO);
2479}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002480
2481/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2482/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002483void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2484 const LangOptions &LO) const {
2485 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002486 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002487}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002488
Ted Kremenek15647632008-01-30 23:02:42 +00002489/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002490void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002491 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002492 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002493 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2494}
2495
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002496Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002497
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002498 if (!Terminator)
2499 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002500
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002501 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002502
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002503 switch (Terminator->getStmtClass()) {
2504 default:
2505 break;
Mike Stump31feda52009-07-17 01:31:16 +00002506
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002507 case Stmt::ForStmtClass:
2508 E = cast<ForStmt>(Terminator)->getCond();
2509 break;
Mike Stump31feda52009-07-17 01:31:16 +00002510
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002511 case Stmt::WhileStmtClass:
2512 E = cast<WhileStmt>(Terminator)->getCond();
2513 break;
Mike Stump31feda52009-07-17 01:31:16 +00002514
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002515 case Stmt::DoStmtClass:
2516 E = cast<DoStmt>(Terminator)->getCond();
2517 break;
Mike Stump31feda52009-07-17 01:31:16 +00002518
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002519 case Stmt::IfStmtClass:
2520 E = cast<IfStmt>(Terminator)->getCond();
2521 break;
Mike Stump31feda52009-07-17 01:31:16 +00002522
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002523 case Stmt::ChooseExprClass:
2524 E = cast<ChooseExpr>(Terminator)->getCond();
2525 break;
Mike Stump31feda52009-07-17 01:31:16 +00002526
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002527 case Stmt::IndirectGotoStmtClass:
2528 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2529 break;
Mike Stump31feda52009-07-17 01:31:16 +00002530
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002531 case Stmt::SwitchStmtClass:
2532 E = cast<SwitchStmt>(Terminator)->getCond();
2533 break;
Mike Stump31feda52009-07-17 01:31:16 +00002534
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002535 case Stmt::ConditionalOperatorClass:
2536 E = cast<ConditionalOperator>(Terminator)->getCond();
2537 break;
Mike Stump31feda52009-07-17 01:31:16 +00002538
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002539 case Stmt::BinaryOperatorClass: // '&&' and '||'
2540 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002541 break;
Mike Stump31feda52009-07-17 01:31:16 +00002542
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002543 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002544 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002545 }
Mike Stump31feda52009-07-17 01:31:16 +00002546
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002547 return E ? E->IgnoreParens() : NULL;
2548}
2549
Ted Kremenek92137a32008-05-16 16:06:00 +00002550bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002551
Ted Kremenek92137a32008-05-16 16:06:00 +00002552 if (!Terminator)
2553 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002554
Ted Kremenek92137a32008-05-16 16:06:00 +00002555 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002556
Ted Kremenek92137a32008-05-16 16:06:00 +00002557 switch (Terminator->getStmtClass()) {
2558 default:
2559 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002560
2561 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002562 case Stmt::WhileStmtClass:
2563 case Stmt::DoStmtClass:
2564 case Stmt::IfStmtClass:
2565 case Stmt::ChooseExprClass:
2566 case Stmt::ConditionalOperatorClass:
2567 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002568 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002569 }
Mike Stump31feda52009-07-17 01:31:16 +00002570
Ted Kremenek92137a32008-05-16 16:06:00 +00002571 return E ? E->IgnoreParens() : NULL;
2572}
2573
Ted Kremenek15647632008-01-30 23:02:42 +00002574
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002575//===----------------------------------------------------------------------===//
2576// CFG Graphviz Visualization
2577//===----------------------------------------------------------------------===//
2578
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002579
2580#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002581static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002582#endif
2583
Chris Lattnerc61089a2009-06-30 01:26:17 +00002584void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002585#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002586 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002587 GraphHelper = &H;
2588 llvm::ViewGraph(this,"CFG");
2589 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002590#endif
2591}
2592
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002593namespace llvm {
2594template<>
2595struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002596
2597 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2598
2599 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002600
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002601#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002602 std::string OutSStr;
2603 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002604 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002605 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002606
2607 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2608
2609 // Process string output to make it nicer...
2610 for (unsigned i = 0; i != OutStr.length(); ++i)
2611 if (OutStr[i] == '\n') { // Left justify
2612 OutStr[i] = '\\';
2613 OutStr.insert(OutStr.begin()+i+1, 'l');
2614 }
Mike Stump31feda52009-07-17 01:31:16 +00002615
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002616 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002617#else
2618 return "";
2619#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002620 }
2621};
2622} // end namespace llvm