blob: c97b9165bca225c826fadb0d02f532c262134f84 [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
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000055/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000056/// The builder is stateful: an instance of the builder should be used to only
57/// construct a single CFG.
58///
59/// Example usage:
60///
61/// CFGBuilder builder;
62/// CFG* cfg = builder.BuildAST(stmt1);
63///
Mike Stump31feda52009-07-17 01:31:16 +000064/// CFG construction is done via a recursive walk of an AST. We actually parse
65/// the AST in reverse order so that the successor of a basic block is
66/// constructed prior to its predecessor. This allows us to nicely capture
67/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000068///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000069class CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000070 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +000071 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +000072
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000073 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000074 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000075 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000076 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000077 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000078 CFGBlock* DefaultCaseBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +000079 CFGBlock* TryTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +000080
Ted Kremenekeda180e22007-08-28 19:26:49 +000081 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000082 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
83 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000084
85 // A list of blocks that end with a "goto" that must be backpatched to their
86 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000087 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000088 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000089
Ted Kremenekeda180e22007-08-28 19:26:49 +000090 // A list of labels whose address has been taken (for indirect gotos).
91 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
92 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000093
94public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +000095 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
96 Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000097 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Mike Stumpbbf5ba62010-01-19 02:20:09 +000098 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
99 TryTerminatedBlock(NULL) {}
Mike Stump31feda52009-07-17 01:31:16 +0000100
Ted Kremenek9aae5132007-08-23 21:42:29 +0000101 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000102 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
103 bool pruneTriviallyFalseEdges, bool AddEHEdges,
Mike Stump04c68512010-01-21 15:20:48 +0000104 bool AddScopes);
Mike Stump31feda52009-07-17 01:31:16 +0000105
Ted Kremenek93668002009-07-17 22:18:43 +0000106private:
107 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000108 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
109 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
110 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000111 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000112 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
113 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
114 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000115 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000116 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000117 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000118 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000119 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000120 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000121 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000122 CFGBlock *VisitDeclStmt(DeclStmt *DS);
123 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000124 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
125 CFGBlock *VisitDoStmt(DoStmt *D);
126 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000127 CFGBlock *VisitGotoStmt(GotoStmt* G);
128 CFGBlock *VisitIfStmt(IfStmt *I);
129 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
130 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000131 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000132 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
133 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
134 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
135 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
136 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
137 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000138 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
139 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000140 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
141 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000142
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000143 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
144 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000145 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000146
Ted Kremenek6065ef62008-04-28 18:00:46 +0000147 // NYS == Not Yet Supported
148 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000149 badCFG = true;
150 return Block;
151 }
Mike Stump31feda52009-07-17 01:31:16 +0000152
Mike Stump92244b02010-01-19 22:00:14 +0000153 CFGBlock *StartScope(Stmt *S, CFGBlock *B) {
154 if (!AddScopes)
155 return B;
156
157 if (B == 0)
158 B = createBlock();
159 B->StartScope(S, cfg->getBumpVectorContext());
160 return B;
161 }
162
163 void EndScope(Stmt *S) {
164 if (!AddScopes)
165 return;
166
167 if (Block == 0)
168 Block = createBlock();
169 Block->EndScope(S, cfg->getBumpVectorContext());
170 }
171
Ted Kremenek93668002009-07-17 22:18:43 +0000172 void autoCreateBlock() { if (!Block) Block = createBlock(); }
173 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000174 bool FinishBlock(CFGBlock* B);
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000175 CFGBlock *addStmt(Stmt *S) {
176 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000177 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000178
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000179 void AppendStmt(CFGBlock *B, Stmt *S,
180 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
181 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000182 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000183
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000184 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
185 B->addSuccessor(S, cfg->getBumpVectorContext());
186 }
Mike Stump11289f42009-09-09 15:08:12 +0000187
Ted Kremenek963cc312009-07-24 06:55:42 +0000188 /// TryResult - a class representing a variant over the values
189 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
190 /// and is used by the CFGBuilder to decide if a branch condition
191 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000192 class TryResult {
193 int X;
194 public:
195 TryResult(bool b) : X(b ? 1 : 0) {}
196 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000197
Ted Kremenek30754282009-07-24 04:47:11 +0000198 bool isTrue() const { return X == 1; }
199 bool isFalse() const { return X == 0; }
200 bool isKnown() const { return X >= 0; }
201 void negate() {
202 assert(isKnown());
203 X ^= 0x1;
204 }
205 };
Mike Stump11289f42009-09-09 15:08:12 +0000206
Mike Stump773582d2009-07-23 23:25:26 +0000207 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
208 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000209 TryResult TryEvaluateBool(Expr *S) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000210 if (!PruneTriviallyFalseEdges)
211 return TryResult();
212
Mike Stump773582d2009-07-23 23:25:26 +0000213 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000214 if (!S->isTypeDependent() && !S->isValueDependent() &&
215 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000216 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000217
218 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000219 }
220
Ted Kremenekb64d1832008-03-13 03:04:22 +0000221 bool badCFG;
Mike Stump04c68512010-01-21 15:20:48 +0000222
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000223 // True iff trivially false edges should be pruned from the CFG.
224 bool PruneTriviallyFalseEdges;
225
Mike Stump04c68512010-01-21 15:20:48 +0000226 // True iff EH edges on CallExprs should be added to the CFG.
227 bool AddEHEdges;
228
229 // True iff scope start and scope end notes should be added to the CFG.
Mike Stump92244b02010-01-19 22:00:14 +0000230 bool AddScopes;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000231};
Mike Stump31feda52009-07-17 01:31:16 +0000232
Douglas Gregor4619e432008-12-05 23:32:09 +0000233// FIXME: Add support for dependent-sized array types in C++?
234// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000235static VariableArrayType* FindVA(Type* t) {
236 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
237 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
238 if (vat->getSizeExpr())
239 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000240
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000241 t = vt->getElementType().getTypePtr();
242 }
Mike Stump31feda52009-07-17 01:31:16 +0000243
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000244 return 0;
245}
Mike Stump31feda52009-07-17 01:31:16 +0000246
247/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
248/// arbitrary statement. Examples include a single expression or a function
249/// body (compound statement). The ownership of the returned CFG is
250/// transferred to the caller. If CFG construction fails, this method returns
251/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000252CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000253 bool pruneTriviallyFalseEdges,
Mike Stumpcc3a8532010-01-21 17:21:23 +0000254 bool addehedges, bool AddScopes) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000255
Mike Stumpcc3a8532010-01-21 17:21:23 +0000256 AddEHEdges = addehedges;
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000257 PruneTriviallyFalseEdges = pruneTriviallyFalseEdges;
258
Mike Stump0d76d072009-07-20 23:24:15 +0000259 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000260 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000261 if (!Statement)
262 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000263
Mike Stump92244b02010-01-19 22:00:14 +0000264 this->AddScopes = AddScopes;
Ted Kremenekb64d1832008-03-13 03:04:22 +0000265 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000266
267 // Create an empty block that will serve as the exit block for the CFG. Since
268 // this is the first block added to the CFG, it will be implicitly registered
269 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000270 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000271 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000272 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000273
Ted Kremenek9aae5132007-08-23 21:42:29 +0000274 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000275 CFGBlock* B = addStmt(Statement);
Mike Stump6bf1c082010-01-21 02:21:40 +0000276
277 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
278 // FIXME: Add code for base initializers and member initializers.
279 (void)CD;
280 }
Ted Kremenek8aed4902009-10-20 23:46:25 +0000281 if (!B)
282 B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000283
Daniel Dunbar260918c2010-02-22 05:58:59 +0000284 if (B) {
285 // Finalize the last constructed block. This usually involves reversing the
286 // order of the statements in the block.
287 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000288
Daniel Dunbar260918c2010-02-22 05:58:59 +0000289 // Backpatch the gotos whose label -> block mappings we didn't know when we
290 // encountered them.
291 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000292 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000293
Daniel Dunbar260918c2010-02-22 05:58:59 +0000294 CFGBlock* B = *I;
295 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
296 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000297
Daniel Dunbar260918c2010-02-22 05:58:59 +0000298 // If there is no target for the goto, then we are looking at an
299 // incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000300 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000301
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000302 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000303 }
Mike Stump31feda52009-07-17 01:31:16 +0000304
Daniel Dunbar260918c2010-02-22 05:58:59 +0000305 // Add successors to the Indirect Goto Dispatch block (if we have one).
306 if (CFGBlock* B = cfg->getIndirectGotoBlock())
307 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
308 E = AddressTakenLabels.end(); I != E; ++I ) {
309
310 // Lookup the target block.
311 LabelMapTy::iterator LI = LabelMap.find(*I);
312
313 // If there is no target block that contains label, then we are looking
314 // at an incomplete AST. Handle this by not registering a successor.
315 if (LI == LabelMap.end()) continue;
316
317 AddSuccessor(B, LI->second);
318 }
319
320 Succ = B;
321 }
Mike Stump31feda52009-07-17 01:31:16 +0000322
323 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000324 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000325
Ted Kremenekab929bb2009-10-20 23:59:28 +0000326 return badCFG ? NULL : cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000327}
Mike Stump31feda52009-07-17 01:31:16 +0000328
Ted Kremenek9aae5132007-08-23 21:42:29 +0000329/// createBlock - Used to lazily create blocks that are connected
330/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000331CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000332 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000333 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000334 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000335 return B;
336}
Mike Stump31feda52009-07-17 01:31:16 +0000337
Ted Kremenek0868eea2009-09-24 18:45:41 +0000338/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek55957a82009-05-02 00:13:27 +0000339bool CFGBuilder::FinishBlock(CFGBlock* B) {
340 if (badCFG)
341 return false;
342
Ted Kremenek93668002009-07-17 22:18:43 +0000343 assert(B);
Ted Kremenek55957a82009-05-02 00:13:27 +0000344 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000345}
346
Ted Kremenek93668002009-07-17 22:18:43 +0000347/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000348/// blocks for ternary operators, &&, and ||. We also process "," and
349/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000350CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000351tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000352 if (!S) {
353 badCFG = true;
354 return 0;
355 }
Ted Kremenek93668002009-07-17 22:18:43 +0000356 switch (S->getStmtClass()) {
357 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000358 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000359
360 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000361 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000362
Ted Kremenek93668002009-07-17 22:18:43 +0000363 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000364 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000365
Ted Kremenek93668002009-07-17 22:18:43 +0000366 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000367 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000368
Ted Kremenek93668002009-07-17 22:18:43 +0000369 case Stmt::BreakStmtClass:
370 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000371
Ted Kremenek93668002009-07-17 22:18:43 +0000372 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000373 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000374 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000375
Ted Kremenek93668002009-07-17 22:18:43 +0000376 case Stmt::CaseStmtClass:
377 return VisitCaseStmt(cast<CaseStmt>(S));
378
379 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000380 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000381
Ted Kremenek93668002009-07-17 22:18:43 +0000382 case Stmt::CompoundStmtClass:
383 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000384
Ted Kremenek93668002009-07-17 22:18:43 +0000385 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000386 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000387
Ted Kremenek93668002009-07-17 22:18:43 +0000388 case Stmt::ContinueStmtClass:
389 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000390
Ted Kremenekb27378c2010-01-19 20:40:33 +0000391 case Stmt::CXXCatchStmtClass:
392 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
393
Ted Kremenek82bfc862010-08-28 00:19:02 +0000394 case Stmt::CXXExprWithTemporariesClass: {
395 // FIXME: Handle temporaries. For now, just visit the subexpression
396 // so we don't artificially create extra blocks.
Ted Kremenek128d04d2010-08-31 18:47:34 +0000397 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000398 }
399
Zhongxing Xu7e612172010-04-13 09:38:01 +0000400 case Stmt::CXXMemberCallExprClass:
401 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
402
Ted Kremenekb27378c2010-01-19 20:40:33 +0000403 case Stmt::CXXThrowExprClass:
404 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000405
Ted Kremenekb27378c2010-01-19 20:40:33 +0000406 case Stmt::CXXTryStmtClass:
407 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000408
Ted Kremenek93668002009-07-17 22:18:43 +0000409 case Stmt::DeclStmtClass:
410 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000411
Ted Kremenek93668002009-07-17 22:18:43 +0000412 case Stmt::DefaultStmtClass:
413 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000414
Ted Kremenek93668002009-07-17 22:18:43 +0000415 case Stmt::DoStmtClass:
416 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000417
Ted Kremenek93668002009-07-17 22:18:43 +0000418 case Stmt::ForStmtClass:
419 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000420
Ted Kremenek93668002009-07-17 22:18:43 +0000421 case Stmt::GotoStmtClass:
422 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000423
Ted Kremenek93668002009-07-17 22:18:43 +0000424 case Stmt::IfStmtClass:
425 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000426
Ted Kremenek93668002009-07-17 22:18:43 +0000427 case Stmt::IndirectGotoStmtClass:
428 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000429
Ted Kremenek93668002009-07-17 22:18:43 +0000430 case Stmt::LabelStmtClass:
431 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000432
Ted Kremenek5868ec62010-04-11 17:02:10 +0000433 case Stmt::MemberExprClass:
434 return VisitMemberExpr(cast<MemberExpr>(S), asc);
435
Ted Kremenek93668002009-07-17 22:18:43 +0000436 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000437 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
438
Ted Kremenek93668002009-07-17 22:18:43 +0000439 case Stmt::ObjCAtSynchronizedStmtClass:
440 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000441
Ted Kremenek93668002009-07-17 22:18:43 +0000442 case Stmt::ObjCAtThrowStmtClass:
443 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000444
Ted Kremenek93668002009-07-17 22:18:43 +0000445 case Stmt::ObjCAtTryStmtClass:
446 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000447
Ted Kremenek93668002009-07-17 22:18:43 +0000448 case Stmt::ObjCForCollectionStmtClass:
449 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000450
Ted Kremenek93668002009-07-17 22:18:43 +0000451 case Stmt::ParenExprClass:
452 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000453 goto tryAgain;
454
Ted Kremenek93668002009-07-17 22:18:43 +0000455 case Stmt::NullStmtClass:
456 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000457
Ted Kremenek93668002009-07-17 22:18:43 +0000458 case Stmt::ReturnStmtClass:
459 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000460
Ted Kremenek93668002009-07-17 22:18:43 +0000461 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000462 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000463
Ted Kremenek93668002009-07-17 22:18:43 +0000464 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000465 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000466
Ted Kremenek93668002009-07-17 22:18:43 +0000467 case Stmt::SwitchStmtClass:
468 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000469
Ted Kremenek93668002009-07-17 22:18:43 +0000470 case Stmt::WhileStmtClass:
471 return VisitWhileStmt(cast<WhileStmt>(S));
472 }
473}
Mike Stump11289f42009-09-09 15:08:12 +0000474
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000475CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
476 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000477 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000478 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000479 }
Mike Stump11289f42009-09-09 15:08:12 +0000480
Ted Kremenek93668002009-07-17 22:18:43 +0000481 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000482}
Mike Stump31feda52009-07-17 01:31:16 +0000483
Ted Kremenek93668002009-07-17 22:18:43 +0000484/// VisitChildren - Visit the children of a Stmt.
485CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
486 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000487 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000488 E = Terminator->child_end(); I != E; ++I) {
489 if (*I) B = Visit(*I);
490 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000491 return B;
492}
Mike Stump11289f42009-09-09 15:08:12 +0000493
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000494CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
495 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000496 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000497
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000498 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000499 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000500 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000501 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000502
Ted Kremenek9aae5132007-08-23 21:42:29 +0000503 return Block;
504}
Mike Stump11289f42009-09-09 15:08:12 +0000505
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000506CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
507 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000508 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000509 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000510 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000511
Ted Kremenek93668002009-07-17 22:18:43 +0000512 if (!FinishBlock(ConfluenceBlock))
513 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000514
Ted Kremenek93668002009-07-17 22:18:43 +0000515 // create the block evaluating the LHS
516 CFGBlock* LHSBlock = createBlock(false);
517 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000518
Ted Kremenek93668002009-07-17 22:18:43 +0000519 // create the block evaluating the RHS
520 Succ = ConfluenceBlock;
521 Block = NULL;
522 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000523
524 if (RHSBlock) {
525 if (!FinishBlock(RHSBlock))
526 return 0;
527 }
528 else {
529 // Create an empty block for cases where the RHS doesn't require
530 // any explicit statements in the CFG.
531 RHSBlock = createBlock();
532 }
Mike Stump11289f42009-09-09 15:08:12 +0000533
Mike Stump773582d2009-07-23 23:25:26 +0000534 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000535 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000536 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000537 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000538
Ted Kremenek93668002009-07-17 22:18:43 +0000539 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000540 if (B->getOpcode() == BO_LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000541 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
542 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000543 } else {
John McCalle3027922010-08-25 11:45:40 +0000544 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000545 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
546 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000547 }
Mike Stump11289f42009-09-09 15:08:12 +0000548
Ted Kremenek93668002009-07-17 22:18:43 +0000549 // Generate the blocks for evaluating the LHS.
550 Block = LHSBlock;
551 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000552 }
John McCalle3027922010-08-25 11:45:40 +0000553 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000554 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000555 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000556 addStmt(B->getRHS());
557 return addStmt(B->getLHS());
558 }
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000559 else if (B->isAssignmentOp()) {
560 if (asc.alwaysAdd()) {
561 autoCreateBlock();
562 AppendStmt(Block, B, asc);
563 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000564
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000565 Visit(B->getRHS());
566 return Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
567 }
Mike Stump11289f42009-09-09 15:08:12 +0000568
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000569 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000570}
571
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000572CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
573 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000574 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000575 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000576 }
577 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000578}
579
Ted Kremenek93668002009-07-17 22:18:43 +0000580CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
581 // "break" is a control-flow statement. Thus we stop processing the current
582 // block.
583 if (Block && !FinishBlock(Block))
584 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000585
Ted Kremenek93668002009-07-17 22:18:43 +0000586 // Now create a new block that ends with the break statement.
587 Block = createBlock(false);
588 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000589
Ted Kremenek93668002009-07-17 22:18:43 +0000590 // If there is no target for the break, then we are looking at an incomplete
591 // AST. This means that the CFG cannot be constructed.
592 if (BreakTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000593 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000594 else
595 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000596
597
Ted Kremenek9aae5132007-08-23 21:42:29 +0000598 return Block;
599}
Mike Stump11289f42009-09-09 15:08:12 +0000600
Mike Stump04c68512010-01-21 15:20:48 +0000601static bool CanThrow(Expr *E) {
602 QualType Ty = E->getType();
603 if (Ty->isFunctionPointerType())
604 Ty = Ty->getAs<PointerType>()->getPointeeType();
605 else if (Ty->isBlockPointerType())
606 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000607
Mike Stump04c68512010-01-21 15:20:48 +0000608 const FunctionType *FT = Ty->getAs<FunctionType>();
609 if (FT) {
610 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
611 if (Proto->hasEmptyExceptionSpec())
612 return false;
613 }
614 return true;
615}
616
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000617CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000618 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000619 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000620 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000621 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000622 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000623
Mike Stump04c68512010-01-21 15:20:48 +0000624 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000625
626 // Languages without exceptions are assumed to not throw.
627 if (Context->getLangOptions().Exceptions) {
Mike Stump04c68512010-01-21 15:20:48 +0000628 if (AddEHEdges)
629 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +0000630 }
631
632 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000633 if (FD->hasAttr<NoReturnAttr>())
634 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +0000635 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +0000636 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000637 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000638
Mike Stump04c68512010-01-21 15:20:48 +0000639 if (!CanThrow(C->getCallee()))
640 AddEHEdge = false;
641
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000642 if (!NoReturn && !AddEHEdge) {
643 if (asc.asLValue())
644 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
645 else
646 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
647 }
Mike Stump11289f42009-09-09 15:08:12 +0000648
Mike Stump92244b02010-01-19 22:00:14 +0000649 if (Block) {
650 Succ = Block;
651 if (!FinishBlock(Block))
652 return 0;
653 }
Mike Stump11289f42009-09-09 15:08:12 +0000654
Mike Stump92244b02010-01-19 22:00:14 +0000655 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000656 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000657
Mike Stump92244b02010-01-19 22:00:14 +0000658 if (NoReturn) {
659 // Wire this to the exit block directly.
660 AddSuccessor(Block, &cfg->getExit());
661 }
Mike Stump04c68512010-01-21 15:20:48 +0000662 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +0000663 // Add exceptional edges.
664 if (TryTerminatedBlock)
665 AddSuccessor(Block, TryTerminatedBlock);
666 else
667 AddSuccessor(Block, &cfg->getExit());
668 }
Mike Stump11289f42009-09-09 15:08:12 +0000669
Mike Stump8c5d7992009-07-25 21:26:53 +0000670 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000671}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000672
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000673CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
674 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000675 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000676 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000677 if (!FinishBlock(ConfluenceBlock))
678 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000679
Ted Kremenek5868ec62010-04-11 17:02:10 +0000680 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
681 : AddStmtChoice::AlwaysAdd;
682
Ted Kremenek21822592009-07-17 18:20:32 +0000683 Succ = ConfluenceBlock;
684 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000685 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000686 if (!FinishBlock(LHSBlock))
687 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000688
Ted Kremenek21822592009-07-17 18:20:32 +0000689 Succ = ConfluenceBlock;
690 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000691 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000692 if (!FinishBlock(RHSBlock))
693 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000694
Ted Kremenek21822592009-07-17 18:20:32 +0000695 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000696 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000697 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000698 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
699 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000700 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000701 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000702}
Mike Stump11289f42009-09-09 15:08:12 +0000703
704
705CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump92244b02010-01-19 22:00:14 +0000706 EndScope(C);
707
Mike Stump11289f42009-09-09 15:08:12 +0000708 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000709
710 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
711 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +0000712 // If we hit a segment of code just containing ';' (NullStmts), we can
713 // get a null block back. In such cases, just use the LastBlock
714 if (CFGBlock *newBlock = addStmt(*I))
715 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +0000716
Ted Kremenekce499c22009-08-27 23:16:26 +0000717 if (badCFG)
718 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000719 }
Mike Stump92244b02010-01-19 22:00:14 +0000720
721 LastBlock = StartScope(C, LastBlock);
722
Ted Kremenek93668002009-07-17 22:18:43 +0000723 return LastBlock;
724}
Mike Stump11289f42009-09-09 15:08:12 +0000725
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000726CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
727 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000728 // Create the confluence block that will "merge" the results of the ternary
729 // expression.
730 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000731 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000732 if (!FinishBlock(ConfluenceBlock))
733 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000734
Ted Kremenek5868ec62010-04-11 17:02:10 +0000735 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
736 : AddStmtChoice::AlwaysAdd;
737
Ted Kremenek51d40b02009-07-17 18:15:54 +0000738 // Create a block for the LHS expression if there is an LHS expression. A
739 // GCC extension allows LHS to be NULL, causing the condition to be the
740 // value that is returned instead.
741 // e.g: x ?: y is shorthand for: x ? x : y;
742 Succ = ConfluenceBlock;
743 Block = NULL;
744 CFGBlock* LHSBlock = NULL;
745 if (C->getLHS()) {
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000746 LHSBlock = Visit(C->getLHS(), asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000747 if (!FinishBlock(LHSBlock))
748 return 0;
749 Block = NULL;
750 }
Mike Stump11289f42009-09-09 15:08:12 +0000751
Ted Kremenek51d40b02009-07-17 18:15:54 +0000752 // Create the block for the RHS expression.
753 Succ = ConfluenceBlock;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000754 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000755 if (!FinishBlock(RHSBlock))
756 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000757
Ted Kremenek51d40b02009-07-17 18:15:54 +0000758 // Create the block that will contain the condition.
759 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000760
Mike Stump773582d2009-07-23 23:25:26 +0000761 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000762 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000763 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000764 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000765 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000766 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000767 // If we know the condition is false, add NULL as the successor for
768 // the block containing the condition. In this case, the confluence
769 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000770 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000771 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000772 } else {
773 // If we have no LHS expression, add the ConfluenceBlock as a direct
774 // successor for the block containing the condition. Moreover, we need to
775 // reverse the order of the predecessors in the ConfluenceBlock because
776 // the RHSBlock will have been added to the succcessors already, and we
777 // want the first predecessor to the the block containing the expression
778 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000779 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000780 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000781 std::reverse(ConfluenceBlock->pred_begin(),
782 ConfluenceBlock->pred_end());
783 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000784 }
Mike Stump11289f42009-09-09 15:08:12 +0000785
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000786 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000787 Block->setTerminator(C);
788 return addStmt(C->getCond());
789}
790
Ted Kremenek93668002009-07-17 22:18:43 +0000791CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
792 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000793
Ted Kremenek93668002009-07-17 22:18:43 +0000794 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000795 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000796 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000797 }
Mike Stump11289f42009-09-09 15:08:12 +0000798
Ted Kremenek93668002009-07-17 22:18:43 +0000799 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000800
Ted Kremenek93668002009-07-17 22:18:43 +0000801 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
802 typedef llvm::SmallVector<Decl*,10> BufTy;
803 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000804
Ted Kremenek93668002009-07-17 22:18:43 +0000805 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
806 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
807 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
808 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000809
Ted Kremenek93668002009-07-17 22:18:43 +0000810 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
811 // automatically freed with the CFG.
812 DeclGroupRef DG(*I);
813 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000814 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000815 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000816
Ted Kremenek93668002009-07-17 22:18:43 +0000817 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000818 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000819 B = VisitDeclSubExpr(D);
820 }
Mike Stump11289f42009-09-09 15:08:12 +0000821
822 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000823}
Mike Stump11289f42009-09-09 15:08:12 +0000824
Ted Kremenek93668002009-07-17 22:18:43 +0000825/// VisitDeclSubExpr - Utility method to add block-level expressions for
826/// initializers in Decls.
827CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
828 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000829
Ted Kremenek93668002009-07-17 22:18:43 +0000830 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000831
Ted Kremenek93668002009-07-17 22:18:43 +0000832 if (!VD)
833 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000834
Ted Kremenek93668002009-07-17 22:18:43 +0000835 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000836
Ted Kremenek93668002009-07-17 22:18:43 +0000837 if (Init) {
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000838 AddStmtChoice::Kind k =
839 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
840 : AddStmtChoice::NotAlwaysAdd;
841 Visit(Init, AddStmtChoice(k));
Ted Kremenek93668002009-07-17 22:18:43 +0000842 }
Mike Stump11289f42009-09-09 15:08:12 +0000843
Ted Kremenek93668002009-07-17 22:18:43 +0000844 // If the type of VD is a VLA, then we must process its size expressions.
845 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
846 VA = FindVA(VA->getElementType().getTypePtr()))
847 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000848
Ted Kremenek93668002009-07-17 22:18:43 +0000849 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000850}
851
852CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000853 // We may see an if statement in the middle of a basic block, or it may be the
854 // first statement we are processing. In either case, we create a new basic
855 // block. First, we create the blocks for the then...else statements, and
856 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000857 // middle of a block, we stop processing that block. That block is then the
858 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000859
860 // The block we were proccessing is now finished. Make it the successor
861 // block.
862 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000863 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000864 if (!FinishBlock(Block))
865 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000866 }
Mike Stump31feda52009-07-17 01:31:16 +0000867
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000868 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000869 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000870
Ted Kremenek9aae5132007-08-23 21:42:29 +0000871 if (Stmt* Else = I->getElse()) {
872 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000873
Ted Kremenek9aae5132007-08-23 21:42:29 +0000874 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000875 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000876 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000877 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000878
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000879 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
880 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000881 else if (Block) {
882 if (!FinishBlock(ElseBlock))
883 return 0;
884 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000885 }
Mike Stump31feda52009-07-17 01:31:16 +0000886
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000887 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000888 CFGBlock* ThenBlock;
889 {
890 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +0000891 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000892 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000893 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000894 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000895
Ted Kremenek1b379512009-04-01 03:52:47 +0000896 if (!ThenBlock) {
897 // We can reach here if the "then" body has all NullStmts.
898 // Create an empty block so we can distinguish between true and false
899 // branches in path-sensitive analyses.
900 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000901 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000902 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000903 if (!FinishBlock(ThenBlock))
904 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000905 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000906 }
907
Mike Stump31feda52009-07-17 01:31:16 +0000908 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000909 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000910
Ted Kremenek9aae5132007-08-23 21:42:29 +0000911 // Set the terminator of the new block to the If statement.
912 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000913
Mike Stump773582d2009-07-23 23:25:26 +0000914 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000915 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000916
Ted Kremenek9aae5132007-08-23 21:42:29 +0000917 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000918 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
919 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000920
921 // Add the condition as the last statement in the new block. This may create
922 // new blocks as the condition may contain control-flow. Any newly created
923 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000924 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000925
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000926 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
927 // and the condition variable initialization to the CFG.
928 if (VarDecl *VD = I->getConditionVariable()) {
929 if (Expr *Init = VD->getInit()) {
930 autoCreateBlock();
931 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
932 addStmt(Init);
933 }
934 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000935
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000936 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000937}
Mike Stump31feda52009-07-17 01:31:16 +0000938
939
Ted Kremenek9aae5132007-08-23 21:42:29 +0000940CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000941 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000942 //
Mike Stump31feda52009-07-17 01:31:16 +0000943 // NOTE: If a "return" appears in the middle of a block, this means that the
944 // code afterwards is DEAD (unreachable). We still keep a basic block
945 // for that code; a simple "mark-and-sweep" from the entry block will be
946 // able to report such dead blocks.
Ted Kremenek0868eea2009-09-24 18:45:41 +0000947 if (Block)
948 FinishBlock(Block);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000949
950 // Create the new block.
951 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000952
Ted Kremenek9aae5132007-08-23 21:42:29 +0000953 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000954 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000955
956 // Add the return statement to the block. This may create new blocks if R
957 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000958 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000959}
960
961CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
962 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000963 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000964 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000965
Ted Kremenek93668002009-07-17 22:18:43 +0000966 if (!LabelBlock) // This can happen when the body is empty, i.e.
967 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000968
Ted Kremenek93668002009-07-17 22:18:43 +0000969 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000970 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000971
972 // Labels partition blocks, so this is the end of the basic block we were
973 // processing (L is the block's label). Because this is label (and we have
974 // already processed the substatement) there is no extra control-flow to worry
975 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000976 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000977 if (!FinishBlock(LabelBlock))
978 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000979
980 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000981 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000982
Ted Kremenek9aae5132007-08-23 21:42:29 +0000983 // This block is now the implicit successor of other blocks.
984 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000985
Ted Kremenek9aae5132007-08-23 21:42:29 +0000986 return LabelBlock;
987}
988
989CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000990 // Goto is a control-flow statement. Thus we stop processing the current
991 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000992 if (Block)
993 FinishBlock(Block);
994
Ted Kremenek9aae5132007-08-23 21:42:29 +0000995 Block = createBlock(false);
996 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000997
998 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000999 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001000
Ted Kremenek9aae5132007-08-23 21:42:29 +00001001 if (I == LabelMap.end())
1002 // We will need to backpatch this block later.
1003 BackpatchBlocks.push_back(Block);
1004 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001005 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001006
Mike Stump31feda52009-07-17 01:31:16 +00001007 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001008}
1009
1010CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001011 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001012
Mike Stump014b3ea2009-07-21 01:12:51 +00001013 // "for" is a control-flow statement. Thus we stop processing the current
1014 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001015 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001016 if (!FinishBlock(Block))
1017 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001018 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001019 } else
1020 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001021
Ted Kremenek304a9532010-05-21 20:30:15 +00001022 // Save the current value for the break targets.
1023 // All breaks should go to the code following the loop.
1024 SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock);
1025 BreakTargetBlock = LoopSuccessor;
1026
Mike Stump31feda52009-07-17 01:31:16 +00001027 // Because of short-circuit evaluation, the condition of the loop can span
1028 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1029 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001030 CFGBlock* ExitConditionBlock = createBlock(false);
1031 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001032
Ted Kremenek81e14852007-08-27 19:46:09 +00001033 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001034 ExitConditionBlock->setTerminator(F);
1035
1036 // Now add the actual condition to the condition block. Because the condition
1037 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001038 if (Stmt* C = F->getCond()) {
1039 Block = ExitConditionBlock;
1040 EntryConditionBlock = addStmt(C);
Ted Kremenekec92f942009-12-24 01:49:06 +00001041 assert(Block == EntryConditionBlock);
1042
1043 // If this block contains a condition variable, add both the condition
1044 // variable and initializer to the CFG.
1045 if (VarDecl *VD = F->getConditionVariable()) {
1046 if (Expr *Init = VD->getInit()) {
1047 autoCreateBlock();
1048 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1049 EntryConditionBlock = addStmt(Init);
1050 assert(Block == EntryConditionBlock);
1051 }
1052 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001053
Ted Kremenek55957a82009-05-02 00:13:27 +00001054 if (Block) {
1055 if (!FinishBlock(EntryConditionBlock))
1056 return 0;
1057 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001058 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001059
Mike Stump31feda52009-07-17 01:31:16 +00001060 // The condition block is the implicit successor for the loop body as well as
1061 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001062 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001063
Mike Stump773582d2009-07-23 23:25:26 +00001064 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001065 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001066
Mike Stump773582d2009-07-23 23:25:26 +00001067 if (F->getCond())
1068 KnownVal = TryEvaluateBool(F->getCond());
1069
Ted Kremenek9aae5132007-08-23 21:42:29 +00001070 // Now create the loop body.
1071 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001072 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001073
Ted Kremenek304a9532010-05-21 20:30:15 +00001074 // Save the current values for Block, Succ, and continue targets.
1075 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1076 save_continue(ContinueTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001077
Ted Kremeneke9610502007-08-30 18:39:40 +00001078 // Create a new block to contain the (bottom) of the loop body.
1079 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001080
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001081 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001082 // Generate increment code in its own basic block. This is the target of
1083 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001084 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001085 } else {
1086 // No increment code. Create a special, empty, block that is used as the
1087 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001088 assert(Succ == EntryConditionBlock);
1089 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001090 }
Mike Stump31feda52009-07-17 01:31:16 +00001091
Ted Kremenek902393b2009-04-28 00:51:56 +00001092 // Finish up the increment (or empty) block if it hasn't been already.
1093 if (Block) {
1094 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +00001095 if (!FinishBlock(Block))
1096 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001097 Block = 0;
1098 }
Mike Stump31feda52009-07-17 01:31:16 +00001099
Ted Kremenek902393b2009-04-28 00:51:56 +00001100 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001101
Ted Kremenek902393b2009-04-28 00:51:56 +00001102 // The starting block for the loop increment is the block that should
1103 // represent the 'loop target' for looping back to the start of the loop.
1104 ContinueTargetBlock->setLoopTarget(F);
1105
Mike Stump31feda52009-07-17 01:31:16 +00001106 // Now populate the body block, and in the process create new blocks as we
1107 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001108 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001109
1110 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +00001111 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +00001112 else if (Block && !FinishBlock(BodyBlock))
1113 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001114
Ted Kremenek30754282009-07-24 04:47:11 +00001115 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001116 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001117 }
Mike Stump31feda52009-07-17 01:31:16 +00001118
Ted Kremenek30754282009-07-24 04:47:11 +00001119 // Link up the condition block with the code that follows the loop. (the
1120 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001121 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001122
Ted Kremenek9aae5132007-08-23 21:42:29 +00001123 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001124 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001125 if (Stmt* I = F->getInit()) {
1126 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001127 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001128 } else {
1129 // There is no loop initialization. We are thus basically a while loop.
1130 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001131 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001132 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001133 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001134 }
1135}
1136
Ted Kremenek5868ec62010-04-11 17:02:10 +00001137CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1138 if (asc.alwaysAdd()) {
1139 autoCreateBlock();
1140 AppendStmt(Block, M, asc);
1141 }
1142 return Visit(M->getBase(),
1143 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1144 : AddStmtChoice::AsLValueNotAlwaysAdd);
1145}
1146
Ted Kremenek9d56e642008-11-11 17:10:00 +00001147CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1148 // Objective-C fast enumeration 'for' statements:
1149 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1150 //
1151 // for ( Type newVariable in collection_expression ) { statements }
1152 //
1153 // becomes:
1154 //
1155 // prologue:
1156 // 1. collection_expression
1157 // T. jump to loop_entry
1158 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001159 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001160 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1161 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1162 // TB:
1163 // statements
1164 // T. jump to loop_entry
1165 // FB:
1166 // what comes after
1167 //
1168 // and
1169 //
1170 // Type existingItem;
1171 // for ( existingItem in expression ) { statements }
1172 //
1173 // becomes:
1174 //
Mike Stump31feda52009-07-17 01:31:16 +00001175 // the same with newVariable replaced with existingItem; the binding works
1176 // the same except that for one ObjCForCollectionStmt::getElement() returns
1177 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001178 //
Mike Stump31feda52009-07-17 01:31:16 +00001179
Ted Kremenek9d56e642008-11-11 17:10:00 +00001180 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001181
Ted Kremenek9d56e642008-11-11 17:10:00 +00001182 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001183 if (!FinishBlock(Block))
1184 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001185 LoopSuccessor = Block;
1186 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001187 } else
1188 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001189
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001190 // Build the condition blocks.
1191 CFGBlock* ExitConditionBlock = createBlock(false);
1192 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001193
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001194 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001195 ExitConditionBlock->setTerminator(S);
1196
1197 // The last statement in the block should be the ObjCForCollectionStmt, which
1198 // performs the actual binding to 'element' and determines if there are any
1199 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001200 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001201 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001202
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001203 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001204 // generate new blocks as necesary. We DON'T add the statement by default to
1205 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001206 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001207 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001208 if (!FinishBlock(EntryConditionBlock))
1209 return 0;
1210 Block = 0;
1211 }
Mike Stump31feda52009-07-17 01:31:16 +00001212
1213 // The condition block is the implicit successor for the loop body as well as
1214 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001215 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001216
Ted Kremenek9d56e642008-11-11 17:10:00 +00001217 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001218 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001219 // Save the current values for Succ, continue and break targets.
1220 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001221 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1222
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001223 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001224 ContinueTargetBlock = EntryConditionBlock;
1225
Ted Kremenek93668002009-07-17 22:18:43 +00001226 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001227
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001228 if (!BodyBlock)
1229 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001230 else if (Block) {
1231 if (!FinishBlock(BodyBlock))
1232 return 0;
1233 }
Mike Stump31feda52009-07-17 01:31:16 +00001234
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001235 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001236 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001237 }
Mike Stump31feda52009-07-17 01:31:16 +00001238
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001239 // Link up the condition block with the code that follows the loop.
1240 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001241 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001242
Ted Kremenek9d56e642008-11-11 17:10:00 +00001243 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001244 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001245 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001246}
1247
Ted Kremenek49805452009-05-02 01:49:13 +00001248CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1249 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001250
Ted Kremenek49805452009-05-02 01:49:13 +00001251 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001252 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001253
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001254 // The sync body starts its own basic block. This makes it a little easier
1255 // for diagnostic clients.
1256 if (SyncBlock) {
1257 if (!FinishBlock(SyncBlock))
1258 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001259
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001260 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001261 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001262 }
Mike Stump31feda52009-07-17 01:31:16 +00001263
Ted Kremenek49805452009-05-02 01:49:13 +00001264 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001265 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001266}
Mike Stump31feda52009-07-17 01:31:16 +00001267
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001268CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001269 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001270 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001271}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001272
Ted Kremenek9aae5132007-08-23 21:42:29 +00001273CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001274 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001275
Mike Stump014b3ea2009-07-21 01:12:51 +00001276 // "while" is a control-flow statement. Thus we stop processing the current
1277 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001278 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001279 if (!FinishBlock(Block))
1280 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001281 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001282 } else
1283 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001284
1285 // Because of short-circuit evaluation, the condition of the loop can span
1286 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1287 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001288 CFGBlock* ExitConditionBlock = createBlock(false);
1289 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001290
Ted Kremenek81e14852007-08-27 19:46:09 +00001291 // Set the terminator for the "exit" condition block.
1292 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001293
1294 // Now add the actual condition to the condition block. Because the condition
1295 // itself may contain control-flow, new blocks may be created. Thus we update
1296 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001297 if (Stmt* C = W->getCond()) {
1298 Block = ExitConditionBlock;
1299 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001300 assert(Block == EntryConditionBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001301
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001302 // If this block contains a condition variable, add both the condition
1303 // variable and initializer to the CFG.
1304 if (VarDecl *VD = W->getConditionVariable()) {
1305 if (Expr *Init = VD->getInit()) {
1306 autoCreateBlock();
1307 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1308 EntryConditionBlock = addStmt(Init);
1309 assert(Block == EntryConditionBlock);
1310 }
1311 }
1312
Ted Kremenek55957a82009-05-02 00:13:27 +00001313 if (Block) {
1314 if (!FinishBlock(EntryConditionBlock))
1315 return 0;
1316 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001317 }
Mike Stump31feda52009-07-17 01:31:16 +00001318
1319 // The condition block is the implicit successor for the loop body as well as
1320 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001321 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001322
Mike Stump773582d2009-07-23 23:25:26 +00001323 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001324 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001325
Ted Kremenek9aae5132007-08-23 21:42:29 +00001326 // Process the loop body.
1327 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001328 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001329
1330 // Save the current values for Block, Succ, and continue and break targets
1331 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1332 save_continue(ContinueTargetBlock),
1333 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001334
Mike Stump31feda52009-07-17 01:31:16 +00001335 // Create an empty block to represent the transition block for looping back
1336 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001337 Block = 0;
1338 assert(Succ == EntryConditionBlock);
1339 Succ = createBlock();
1340 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001341 ContinueTargetBlock = Succ;
1342
Ted Kremenek9aae5132007-08-23 21:42:29 +00001343 // All breaks should go to the code following the loop.
1344 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001345
Ted Kremenek9aae5132007-08-23 21:42:29 +00001346 // NULL out Block to force lazy instantiation of blocks for the body.
1347 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001348
Ted Kremenek9aae5132007-08-23 21:42:29 +00001349 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001350 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001351
Ted Kremeneke9610502007-08-30 18:39:40 +00001352 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001353 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001354 else if (Block) {
1355 if (!FinishBlock(BodyBlock))
1356 return 0;
1357 }
Mike Stump31feda52009-07-17 01:31:16 +00001358
Ted Kremenek30754282009-07-24 04:47:11 +00001359 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001360 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001361 }
Mike Stump31feda52009-07-17 01:31:16 +00001362
Ted Kremenek30754282009-07-24 04:47:11 +00001363 // Link up the condition block with the code that follows the loop. (the
1364 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001365 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001366
1367 // There can be no more statements in the condition block since we loop back
1368 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001369 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001370
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001371 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001372 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001373 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001374}
Mike Stump11289f42009-09-09 15:08:12 +00001375
1376
Ted Kremenek93668002009-07-17 22:18:43 +00001377CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1378 // FIXME: For now we pretend that @catch and the code it contains does not
1379 // exit.
1380 return Block;
1381}
Mike Stump31feda52009-07-17 01:31:16 +00001382
Ted Kremenek93041ba2008-12-09 20:20:09 +00001383CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1384 // FIXME: This isn't complete. We basically treat @throw like a return
1385 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001386
Ted Kremenek0868eea2009-09-24 18:45:41 +00001387 // If we were in the middle of a block we stop processing that block.
Ted Kremenek93668002009-07-17 22:18:43 +00001388 if (Block && !FinishBlock(Block))
1389 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001390
Ted Kremenek93041ba2008-12-09 20:20:09 +00001391 // Create the new block.
1392 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001393
Ted Kremenek93041ba2008-12-09 20:20:09 +00001394 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001395 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001396
1397 // Add the statement to the block. This may create new blocks if S contains
1398 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001399 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001400}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001401
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001402CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001403 // If we were in the middle of a block we stop processing that block.
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001404 if (Block && !FinishBlock(Block))
1405 return 0;
1406
1407 // Create the new block.
1408 Block = createBlock(false);
1409
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001410 if (TryTerminatedBlock)
1411 // The current try statement is the only successor.
1412 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001413 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001414 // otherwise the Exit block is the only successor.
1415 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001416
1417 // Add the statement to the block. This may create new blocks if S contains
1418 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001419 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001420}
1421
Ted Kremenek93668002009-07-17 22:18:43 +00001422CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001423 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001424
Mike Stump8d50b6a2009-07-21 01:27:50 +00001425 // "do...while" is a control-flow statement. Thus we stop processing the
1426 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001427 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001428 if (!FinishBlock(Block))
1429 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001430 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001431 } else
1432 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001433
1434 // Because of short-circuit evaluation, the condition of the loop can span
1435 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1436 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001437 CFGBlock* ExitConditionBlock = createBlock(false);
1438 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001439
Ted Kremenek81e14852007-08-27 19:46:09 +00001440 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001441 ExitConditionBlock->setTerminator(D);
1442
1443 // Now add the actual condition to the condition block. Because the condition
1444 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001445 if (Stmt* C = D->getCond()) {
1446 Block = ExitConditionBlock;
1447 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001448 if (Block) {
1449 if (!FinishBlock(EntryConditionBlock))
1450 return 0;
1451 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001452 }
Mike Stump31feda52009-07-17 01:31:16 +00001453
Ted Kremeneka1523a32008-02-27 07:20:00 +00001454 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001455 Succ = EntryConditionBlock;
1456
Mike Stump773582d2009-07-23 23:25:26 +00001457 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001458 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001459
Ted Kremenek9aae5132007-08-23 21:42:29 +00001460 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001461 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001462 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001463 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001464
Ted Kremenek9aae5132007-08-23 21:42:29 +00001465 // Save the current values for Block, Succ, and continue and break targets
1466 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001467 save_continue(ContinueTargetBlock),
1468 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001469
Ted Kremenek9aae5132007-08-23 21:42:29 +00001470 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001471 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001472
Ted Kremenek9aae5132007-08-23 21:42:29 +00001473 // All breaks should go to the code following the loop.
1474 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001475
Ted Kremenek9aae5132007-08-23 21:42:29 +00001476 // NULL out Block to force lazy instantiation of blocks for the body.
1477 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001478
Ted Kremenek9aae5132007-08-23 21:42:29 +00001479 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001480 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001481
Ted Kremeneke9610502007-08-30 18:39:40 +00001482 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001483 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001484 else if (Block) {
1485 if (!FinishBlock(BodyBlock))
1486 return 0;
1487 }
Mike Stump31feda52009-07-17 01:31:16 +00001488
Ted Kremenek110974d2010-08-17 20:59:56 +00001489 if (!KnownVal.isFalse()) {
1490 // Add an intermediate block between the BodyBlock and the
1491 // ExitConditionBlock to represent the "loop back" transition. Create an
1492 // empty block to represent the transition block for looping back to the
1493 // head of the loop.
1494 // FIXME: Can we do this more efficiently without adding another block?
1495 Block = NULL;
1496 Succ = BodyBlock;
1497 CFGBlock *LoopBackBlock = createBlock();
1498 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001499
Ted Kremenek110974d2010-08-17 20:59:56 +00001500 // Add the loop body entry as a successor to the condition.
1501 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1502 }
1503 else
1504 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001505 }
Mike Stump31feda52009-07-17 01:31:16 +00001506
Ted Kremenek30754282009-07-24 04:47:11 +00001507 // Link up the condition block with the code that follows the loop.
1508 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001509 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001510
1511 // There can be no more statements in the body block(s) since we loop back to
1512 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001513 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001514
Ted Kremenek9aae5132007-08-23 21:42:29 +00001515 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001516 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001517 return BodyBlock;
1518}
1519
1520CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1521 // "continue" is a control-flow statement. Thus we stop processing the
1522 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001523 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001524 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001525
Ted Kremenek9aae5132007-08-23 21:42:29 +00001526 // Now create a new block that ends with the continue statement.
1527 Block = createBlock(false);
1528 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001529
Ted Kremenek9aae5132007-08-23 21:42:29 +00001530 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001531 // incomplete AST. This means the CFG cannot be constructed.
1532 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001533 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001534 else
1535 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001536
Ted Kremenek9aae5132007-08-23 21:42:29 +00001537 return Block;
1538}
Mike Stump11289f42009-09-09 15:08:12 +00001539
Ted Kremenek0747de62009-07-18 00:47:21 +00001540CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001541 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001542
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001543 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001544 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001545 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001546 }
Mike Stump11289f42009-09-09 15:08:12 +00001547
Ted Kremenek93668002009-07-17 22:18:43 +00001548 // VLA types have expressions that must be evaluated.
1549 if (E->isArgumentType()) {
1550 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1551 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1552 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001553 }
Mike Stump11289f42009-09-09 15:08:12 +00001554
Mike Stump31feda52009-07-17 01:31:16 +00001555 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001556}
Mike Stump11289f42009-09-09 15:08:12 +00001557
Ted Kremenek93668002009-07-17 22:18:43 +00001558/// VisitStmtExpr - Utility method to handle (nested) statement
1559/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001560CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1561 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001562 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001563 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001564 }
Ted Kremenek93668002009-07-17 22:18:43 +00001565 return VisitCompoundStmt(SE->getSubStmt());
1566}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001567
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001568CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001569 // "switch" is a control-flow statement. Thus we stop processing the current
1570 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001571 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001572
Ted Kremenek9aae5132007-08-23 21:42:29 +00001573 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001574 if (!FinishBlock(Block))
1575 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001576 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001577 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001578
1579 // Save the current "switch" context.
1580 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001581 save_break(BreakTargetBlock),
1582 save_default(DefaultCaseBlock);
1583
Mike Stump31feda52009-07-17 01:31:16 +00001584 // Set the "default" case to be the block after the switch statement. If the
1585 // switch statement contains a "default:", this value will be overwritten with
1586 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001587 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001588
Ted Kremenek9aae5132007-08-23 21:42:29 +00001589 // Create a new block that will contain the switch statement.
1590 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001591
Ted Kremenek9aae5132007-08-23 21:42:29 +00001592 // Now process the switch body. The code after the switch is the implicit
1593 // successor.
1594 Succ = SwitchSuccessor;
1595 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001596
1597 // When visiting the body, the case statements should automatically get linked
1598 // up to the switch. We also don't keep a pointer to the body, since all
1599 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001600 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001601 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001602 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001603 if (Block) {
1604 if (!FinishBlock(BodyBlock))
1605 return 0;
1606 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001607
Mike Stump31feda52009-07-17 01:31:16 +00001608 // If we have no "default:" case, the default transition is to the code
1609 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001610 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001611
Ted Kremenek81e14852007-08-27 19:46:09 +00001612 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001613 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001614 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001615 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001616 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001617
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001618 // Finally, if the SwitchStmt contains a condition variable, add both the
1619 // SwitchStmt and the condition variable initialization to the CFG.
1620 if (VarDecl *VD = Terminator->getConditionVariable()) {
1621 if (Expr *Init = VD->getInit()) {
1622 autoCreateBlock();
1623 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1624 addStmt(Init);
1625 }
1626 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001627
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001628 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001629}
1630
Ted Kremenek93668002009-07-17 22:18:43 +00001631CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001632 // CaseStmts are essentially labels, so they are the first statement in a
1633 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00001634 CFGBlock *TopBlock = 0, *LastBlock = 0;
1635
1636 if (Stmt *Sub = CS->getSubStmt()) {
1637 // For deeply nested chains of CaseStmts, instead of doing a recursion
1638 // (which can blow out the stack), manually unroll and create blocks
1639 // along the way.
1640 while (isa<CaseStmt>(Sub)) {
1641 CFGBlock *CurrentBlock = createBlock(false);
1642 CurrentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00001643
Ted Kremenek60fa6572010-08-04 23:54:30 +00001644 if (TopBlock)
1645 AddSuccessor(LastBlock, CurrentBlock);
1646 else
1647 TopBlock = CurrentBlock;
1648
1649 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
1650 LastBlock = CurrentBlock;
1651
1652 CS = cast<CaseStmt>(Sub);
1653 Sub = CS->getSubStmt();
1654 }
1655
1656 addStmt(Sub);
1657 }
Mike Stump11289f42009-09-09 15:08:12 +00001658
Ted Kremenek55e91e82007-08-30 18:48:11 +00001659 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001660 if (!CaseBlock)
1661 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001662
1663 // Cases statements partition blocks, so this is the top of the basic block we
1664 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001665 CaseBlock->setLabel(CS);
1666
Ted Kremenek55957a82009-05-02 00:13:27 +00001667 if (!FinishBlock(CaseBlock))
1668 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001669
1670 // Add this block to the list of successors for the block with the switch
1671 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001672 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001673 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001674
Ted Kremenek9aae5132007-08-23 21:42:29 +00001675 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1676 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001677
Ted Kremenek60fa6572010-08-04 23:54:30 +00001678 if (TopBlock) {
1679 AddSuccessor(LastBlock, CaseBlock);
1680 Succ = TopBlock;
1681 }
1682 else {
1683 // This block is now the implicit successor of other blocks.
1684 Succ = CaseBlock;
1685 }
Mike Stump31feda52009-07-17 01:31:16 +00001686
Ted Kremenek60fa6572010-08-04 23:54:30 +00001687 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001688}
Mike Stump31feda52009-07-17 01:31:16 +00001689
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001690CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001691 if (Terminator->getSubStmt())
1692 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001693
Ted Kremenek654c78f2008-02-13 22:05:39 +00001694 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001695
1696 if (!DefaultCaseBlock)
1697 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001698
1699 // Default statements partition blocks, so this is the top of the basic block
1700 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001701 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001702
Ted Kremenek55957a82009-05-02 00:13:27 +00001703 if (!FinishBlock(DefaultCaseBlock))
1704 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001705
Mike Stump31feda52009-07-17 01:31:16 +00001706 // Unlike case statements, we don't add the default block to the successors
1707 // for the switch statement immediately. This is done when we finish
1708 // processing the switch statement. This allows for the default case
1709 // (including a fall-through to the code after the switch statement) to always
1710 // be the last successor of a switch-terminated block.
1711
Ted Kremenek654c78f2008-02-13 22:05:39 +00001712 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1713 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001714
Ted Kremenek654c78f2008-02-13 22:05:39 +00001715 // This block is now the implicit successor of other blocks.
1716 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001717
1718 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001719}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001720
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001721CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1722 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1723 // current block.
1724 CFGBlock* TrySuccessor = NULL;
1725
1726 if (Block) {
1727 if (!FinishBlock(Block))
1728 return 0;
1729 TrySuccessor = Block;
1730 } else TrySuccessor = Succ;
1731
Mike Stump0bdba6c2010-01-20 01:15:34 +00001732 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001733
1734 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00001735 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001736 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00001737 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001738
Mike Stump0bdba6c2010-01-20 01:15:34 +00001739 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001740 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1741 // The code after the try is the implicit successor.
1742 Succ = TrySuccessor;
1743 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001744 if (CS->getExceptionDecl() == 0) {
1745 HasCatchAll = true;
1746 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001747 Block = NULL;
1748 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1749 if (CatchBlock == 0)
1750 return 0;
1751 // Add this block to the list of successors for the block with the try
1752 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00001753 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001754 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00001755 if (!HasCatchAll) {
1756 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00001757 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001758 else
Mike Stump845384a2010-01-20 01:30:58 +00001759 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00001760 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001761
1762 // The code after the try is the implicit successor.
1763 Succ = TrySuccessor;
1764
Mike Stump845384a2010-01-20 01:30:58 +00001765 // Save the current "try" context.
1766 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1767 TryTerminatedBlock = NewTryTerminatedBlock;
1768
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001769 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001770 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001771 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001772 return Block;
1773}
1774
1775CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1776 // CXXCatchStmt are treated like labels, so they are the first statement in a
1777 // block.
1778
1779 if (CS->getHandlerBlock())
1780 addStmt(CS->getHandlerBlock());
1781
1782 CFGBlock* CatchBlock = Block;
1783 if (!CatchBlock)
1784 CatchBlock = createBlock();
1785
1786 CatchBlock->setLabel(CS);
1787
1788 if (!FinishBlock(CatchBlock))
1789 return 0;
1790
1791 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1792 Block = NULL;
1793
1794 return CatchBlock;
1795}
1796
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001797CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00001798 AddStmtChoice asc) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001799 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001800 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00001801 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001802 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00001803 return VisitChildren(C);
1804}
1805
Ted Kremenekeda180e22007-08-28 19:26:49 +00001806CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001807 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001808 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001809
Ted Kremenekeda180e22007-08-28 19:26:49 +00001810 if (!IBlock) {
1811 IBlock = createBlock(false);
1812 cfg->setIndirectGotoBlock(IBlock);
1813 }
Mike Stump31feda52009-07-17 01:31:16 +00001814
Ted Kremenekeda180e22007-08-28 19:26:49 +00001815 // IndirectGoto is a control-flow statement. Thus we stop processing the
1816 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001817 if (Block && !FinishBlock(Block))
1818 return 0;
1819
Ted Kremenekeda180e22007-08-28 19:26:49 +00001820 Block = createBlock(false);
1821 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001822 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001823 return addStmt(I->getTarget());
1824}
1825
Ted Kremenek04cca642007-08-23 21:26:19 +00001826} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001827
Mike Stump31feda52009-07-17 01:31:16 +00001828/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1829/// no successors or predecessors. If this is the first block created in the
1830/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001831CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001832 bool first_block = begin() == end();
1833
1834 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001835 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1836 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1837 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001838
1839 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001840 if (first_block)
1841 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001842
1843 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001844 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001845}
1846
Ted Kremenek889073f2007-08-23 16:51:22 +00001847/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1848/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00001849CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001850 bool PruneTriviallyFalse,
Mike Stump04c68512010-01-21 15:20:48 +00001851 bool AddEHEdges, bool AddScopes) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001852 CFGBuilder Builder;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001853 return Builder.buildCFG(D, Statement, C, PruneTriviallyFalse,
1854 AddEHEdges, AddScopes);
Ted Kremenek889073f2007-08-23 16:51:22 +00001855}
1856
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001857//===----------------------------------------------------------------------===//
1858// CFG: Queries for BlkExprs.
1859//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001860
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001861namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001862 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001863}
1864
Ted Kremenekbff98442009-12-23 23:37:10 +00001865static void FindSubExprAssignments(Stmt *S,
1866 llvm::SmallPtrSet<Expr*,50>& Set) {
1867 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001868 return;
Mike Stump31feda52009-07-17 01:31:16 +00001869
Ted Kremenekbff98442009-12-23 23:37:10 +00001870 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001871 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00001872 if (!child)
1873 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001874
Ted Kremenekbff98442009-12-23 23:37:10 +00001875 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001876 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001877
Ted Kremenekbff98442009-12-23 23:37:10 +00001878 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00001879 }
1880}
1881
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001882static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1883 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001884
1885 // Look for assignments that are used as subexpressions. These are the only
1886 // assignments that we want to *possibly* register as a block-level
1887 // expression. Basically, if an assignment occurs both in a subexpression and
1888 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001889 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001890
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001891 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001892 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001893 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001894
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001895 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001896
1897 // Iterate over the statements again on identify the Expr* and Stmt* at the
1898 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001899
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001900 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001901 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001902
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001903 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001904 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001905 // expression are really "statements" whose value is never used by
1906 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001907 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001908 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001909 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1910 // Special handling for statement expressions. The last statement in
1911 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001912 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001913 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001914 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001915 (*M)[C->body_back()] = x;
1916 }
1917 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001918
Ted Kremenek95a123c2008-01-26 00:03:27 +00001919 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001920 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001921 }
Mike Stump31feda52009-07-17 01:31:16 +00001922
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001923 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001924
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001925 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001926
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001927 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001928 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001929 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001930 }
1931 }
Mike Stump31feda52009-07-17 01:31:16 +00001932
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001933 return M;
1934}
1935
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001936CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1937 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001938 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001939
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001940 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001941 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00001942 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001943}
1944
1945unsigned CFG::getNumBlkExprs() {
1946 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1947 return M->size();
1948 else {
1949 // We assume callers interested in the number of BlkExprs will want
1950 // the map constructed if it doesn't already exist.
1951 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1952 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1953 }
1954}
1955
Ted Kremenek6065ef62008-04-28 18:00:46 +00001956//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001957// Cleanup: CFG dstor.
1958//===----------------------------------------------------------------------===//
1959
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001960CFG::~CFG() {
1961 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1962}
Mike Stump31feda52009-07-17 01:31:16 +00001963
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001964//===----------------------------------------------------------------------===//
1965// CFG pretty printing
1966//===----------------------------------------------------------------------===//
1967
Ted Kremenek7e776b12007-08-22 18:22:34 +00001968namespace {
1969
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001970class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001971 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1972 StmtMapTy StmtMap;
1973 signed CurrentBlock;
1974 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001975 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001976public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001977
Chris Lattnerc61089a2009-06-30 01:26:17 +00001978 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1979 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001980 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1981 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001982 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001983 BI != BEnd; ++BI, ++j )
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001984 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001985 }
1986 }
Mike Stump31feda52009-07-17 01:31:16 +00001987
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001988 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001989
Chris Lattnerc61089a2009-06-30 01:26:17 +00001990 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001991 void setBlockID(signed i) { CurrentBlock = i; }
1992 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001993
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001994 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001995
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001996 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001997
1998 if (I == StmtMap.end())
1999 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002000
2001 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00002002 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002003 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002004 }
Mike Stump31feda52009-07-17 01:31:16 +00002005
Ted Kremenek60983dc2010-01-19 20:52:05 +00002006 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002007 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002008 }
2009};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002010} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002011
Chris Lattnerc61089a2009-06-30 01:26:17 +00002012
2013namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002014class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002015 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002016
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002017 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002018 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002019 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002020public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002021 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002022 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002023 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002024
Ted Kremenek9aae5132007-08-23 21:42:29 +00002025 void VisitIfStmt(IfStmt* I) {
2026 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002027 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002028 }
Mike Stump31feda52009-07-17 01:31:16 +00002029
Ted Kremenek9aae5132007-08-23 21:42:29 +00002030 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002031 void VisitStmt(Stmt* Terminator) {
2032 Terminator->printPretty(OS, Helper, Policy);
2033 }
2034
Ted Kremenek9aae5132007-08-23 21:42:29 +00002035 void VisitForStmt(ForStmt* F) {
2036 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002037 if (F->getInit())
2038 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002039 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002040 if (Stmt* C = F->getCond())
2041 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002042 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002043 if (F->getInc())
2044 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002045 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002046 }
Mike Stump31feda52009-07-17 01:31:16 +00002047
Ted Kremenek9aae5132007-08-23 21:42:29 +00002048 void VisitWhileStmt(WhileStmt* W) {
2049 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002050 if (Stmt* C = W->getCond())
2051 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002052 }
Mike Stump31feda52009-07-17 01:31:16 +00002053
Ted Kremenek9aae5132007-08-23 21:42:29 +00002054 void VisitDoStmt(DoStmt* D) {
2055 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002056 if (Stmt* C = D->getCond())
2057 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002058 }
Mike Stump31feda52009-07-17 01:31:16 +00002059
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002060 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002061 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002062 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002063 }
Mike Stump31feda52009-07-17 01:31:16 +00002064
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002065 void VisitCXXTryStmt(CXXTryStmt* CS) {
2066 OS << "try ...";
2067 }
2068
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002069 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002070 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002071 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002072 }
Mike Stump31feda52009-07-17 01:31:16 +00002073
Ted Kremenek391f94a2007-08-31 22:29:13 +00002074 void VisitChooseExpr(ChooseExpr* C) {
2075 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002076 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002077 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002078 }
Mike Stump31feda52009-07-17 01:31:16 +00002079
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002080 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2081 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002082 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002083 }
Mike Stump31feda52009-07-17 01:31:16 +00002084
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002085 void VisitBinaryOperator(BinaryOperator* B) {
2086 if (!B->isLogicalOp()) {
2087 VisitExpr(B);
2088 return;
2089 }
Mike Stump31feda52009-07-17 01:31:16 +00002090
Douglas Gregor7de59662009-05-29 20:38:28 +00002091 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002092
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002093 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002094 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002095 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002096 return;
John McCalle3027922010-08-25 11:45:40 +00002097 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002098 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002099 return;
2100 default:
2101 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00002102 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002103 }
Mike Stump31feda52009-07-17 01:31:16 +00002104
Ted Kremenek12687ff2007-08-27 21:54:41 +00002105 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002106 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002107 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00002108};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002109} // end anonymous namespace
2110
Mike Stump31feda52009-07-17 01:31:16 +00002111
Chris Lattnerc61089a2009-06-30 01:26:17 +00002112static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00002113 const CFGElement &E) {
Mike Stump92244b02010-01-19 22:00:14 +00002114
2115 if (E.asStartScope()) {
2116 OS << "start scope\n";
2117 return;
2118 }
2119 if (E.asEndScope()) {
2120 OS << "end scope\n";
2121 return;
2122 }
2123
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002124 Stmt *S = E;
2125
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002126 if (Helper) {
2127 // special printing for statement-expressions.
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002128 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002129 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00002130
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002131 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00002132 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00002133 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00002134 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002135 return;
2136 }
2137 }
Mike Stump31feda52009-07-17 01:31:16 +00002138
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002139 // special printing for comma expressions.
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002140 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
John McCalle3027922010-08-25 11:45:40 +00002141 if (B->getOpcode() == BO_Comma) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002142 OS << "... , ";
2143 Helper->handledStmt(B->getRHS(),OS);
2144 OS << '\n';
2145 return;
Mike Stump31feda52009-07-17 01:31:16 +00002146 }
2147 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002148 }
Mike Stump31feda52009-07-17 01:31:16 +00002149
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002150 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2151
2152 if (isa<CXXOperatorCallExpr>(S)) {
2153 OS << " (OperatorCall)";
2154 }
2155 else if (isa<CXXBindTemporaryExpr>(S)) {
2156 OS << " (BindTemporary)";
2157 }
2158
Mike Stump31feda52009-07-17 01:31:16 +00002159
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002160 // Expressions need a newline.
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002161 if (isa<Expr>(S))
2162 OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002163}
Mike Stump31feda52009-07-17 01:31:16 +00002164
Chris Lattnerc61089a2009-06-30 01:26:17 +00002165static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2166 const CFGBlock& B,
2167 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00002168
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002169 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00002170
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002171 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00002172 OS << "\n [ B" << B.getBlockID();
2173
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002174 if (&B == &cfg->getEntry())
2175 OS << " (ENTRY) ]\n";
2176 else if (&B == &cfg->getExit())
2177 OS << " (EXIT) ]\n";
2178 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002179 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002180 else
2181 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00002182
Ted Kremenek71eca012007-08-29 23:20:49 +00002183 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00002184 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002185
2186 if (print_edges)
2187 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002188
Mike Stump92244b02010-01-19 22:00:14 +00002189 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002190 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00002191 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00002192 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002193 C->getLHS()->printPretty(OS, Helper,
2194 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002195 if (C->getRHS()) {
2196 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002197 C->getRHS()->printPretty(OS, Helper,
2198 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002199 }
Mike Stump92244b02010-01-19 22:00:14 +00002200 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002201 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00002202 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002203 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00002204 if (CS->getExceptionDecl())
2205 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2206 0);
2207 else
2208 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002209 OS << ")";
2210
2211 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002212 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00002213
Ted Kremenek71eca012007-08-29 23:20:49 +00002214 OS << ":\n";
2215 }
Mike Stump31feda52009-07-17 01:31:16 +00002216
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002217 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002218 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00002219
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002220 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2221 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00002222
Ted Kremenek71eca012007-08-29 23:20:49 +00002223 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002224 if (print_edges)
2225 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002226
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002227 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002228
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002229 if (Helper)
2230 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002231
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002232 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002233 }
Mike Stump31feda52009-07-17 01:31:16 +00002234
Ted Kremenek71eca012007-08-29 23:20:49 +00002235 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002236 if (B.getTerminator()) {
2237 if (print_edges)
2238 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002239
Ted Kremenek71eca012007-08-29 23:20:49 +00002240 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002241
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002242 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002243
Chris Lattnerc61089a2009-06-30 01:26:17 +00002244 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2245 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002246 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00002247 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002248 }
Mike Stump31feda52009-07-17 01:31:16 +00002249
Ted Kremenek71eca012007-08-29 23:20:49 +00002250 if (print_edges) {
2251 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002252 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002253 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002254
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002255 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2256 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002257
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002258 if (i == 8 || (i-8) == 0)
2259 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002260
Ted Kremenek71eca012007-08-29 23:20:49 +00002261 OS << " B" << (*I)->getBlockID();
2262 }
Mike Stump31feda52009-07-17 01:31:16 +00002263
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002264 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002265
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002266 // Print the successors of this block.
2267 OS << " Successors (" << B.succ_size() << "):";
2268 i = 0;
2269
2270 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2271 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002272
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002273 if (i == 8 || (i-8) % 10 == 0)
2274 OS << "\n ";
2275
Mike Stump0d76d072009-07-20 23:24:15 +00002276 if (*I)
2277 OS << " B" << (*I)->getBlockID();
2278 else
2279 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002280 }
Mike Stump31feda52009-07-17 01:31:16 +00002281
Ted Kremenek71eca012007-08-29 23:20:49 +00002282 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002283 }
Mike Stump31feda52009-07-17 01:31:16 +00002284}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002285
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002286
2287/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002288void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002289
2290/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002291void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2292 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002293
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002294 // Print the entry block.
2295 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002296
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002297 // Iterate through the CFGBlocks and print them one by one.
2298 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2299 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002300 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002301 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002302
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002303 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002304 }
Mike Stump31feda52009-07-17 01:31:16 +00002305
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002306 // Print the exit block.
2307 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002308 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002309}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002310
2311/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002312void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2313 print(llvm::errs(), cfg, LO);
2314}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002315
2316/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2317/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002318void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2319 const LangOptions &LO) const {
2320 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002321 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002322}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002323
Ted Kremenek15647632008-01-30 23:02:42 +00002324/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002325void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002326 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002327 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002328 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2329}
2330
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002331Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002332
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002333 if (!Terminator)
2334 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002335
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002336 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002337
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002338 switch (Terminator->getStmtClass()) {
2339 default:
2340 break;
Mike Stump31feda52009-07-17 01:31:16 +00002341
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002342 case Stmt::ForStmtClass:
2343 E = cast<ForStmt>(Terminator)->getCond();
2344 break;
Mike Stump31feda52009-07-17 01:31:16 +00002345
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002346 case Stmt::WhileStmtClass:
2347 E = cast<WhileStmt>(Terminator)->getCond();
2348 break;
Mike Stump31feda52009-07-17 01:31:16 +00002349
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002350 case Stmt::DoStmtClass:
2351 E = cast<DoStmt>(Terminator)->getCond();
2352 break;
Mike Stump31feda52009-07-17 01:31:16 +00002353
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002354 case Stmt::IfStmtClass:
2355 E = cast<IfStmt>(Terminator)->getCond();
2356 break;
Mike Stump31feda52009-07-17 01:31:16 +00002357
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002358 case Stmt::ChooseExprClass:
2359 E = cast<ChooseExpr>(Terminator)->getCond();
2360 break;
Mike Stump31feda52009-07-17 01:31:16 +00002361
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002362 case Stmt::IndirectGotoStmtClass:
2363 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2364 break;
Mike Stump31feda52009-07-17 01:31:16 +00002365
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002366 case Stmt::SwitchStmtClass:
2367 E = cast<SwitchStmt>(Terminator)->getCond();
2368 break;
Mike Stump31feda52009-07-17 01:31:16 +00002369
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002370 case Stmt::ConditionalOperatorClass:
2371 E = cast<ConditionalOperator>(Terminator)->getCond();
2372 break;
Mike Stump31feda52009-07-17 01:31:16 +00002373
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002374 case Stmt::BinaryOperatorClass: // '&&' and '||'
2375 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002376 break;
Mike Stump31feda52009-07-17 01:31:16 +00002377
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002378 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002379 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002380 }
Mike Stump31feda52009-07-17 01:31:16 +00002381
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002382 return E ? E->IgnoreParens() : NULL;
2383}
2384
Ted Kremenek92137a32008-05-16 16:06:00 +00002385bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002386
Ted Kremenek92137a32008-05-16 16:06:00 +00002387 if (!Terminator)
2388 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002389
Ted Kremenek92137a32008-05-16 16:06:00 +00002390 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002391
Ted Kremenek92137a32008-05-16 16:06:00 +00002392 switch (Terminator->getStmtClass()) {
2393 default:
2394 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002395
2396 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002397 case Stmt::WhileStmtClass:
2398 case Stmt::DoStmtClass:
2399 case Stmt::IfStmtClass:
2400 case Stmt::ChooseExprClass:
2401 case Stmt::ConditionalOperatorClass:
2402 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002403 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002404 }
Mike Stump31feda52009-07-17 01:31:16 +00002405
Ted Kremenek92137a32008-05-16 16:06:00 +00002406 return E ? E->IgnoreParens() : NULL;
2407}
2408
Ted Kremenek15647632008-01-30 23:02:42 +00002409
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002410//===----------------------------------------------------------------------===//
2411// CFG Graphviz Visualization
2412//===----------------------------------------------------------------------===//
2413
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002414
2415#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002416static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002417#endif
2418
Chris Lattnerc61089a2009-06-30 01:26:17 +00002419void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002420#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002421 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002422 GraphHelper = &H;
2423 llvm::ViewGraph(this,"CFG");
2424 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002425#endif
2426}
2427
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002428namespace llvm {
2429template<>
2430struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002431
2432 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2433
2434 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002435
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002436#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002437 std::string OutSStr;
2438 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002439 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002440 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002441
2442 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2443
2444 // Process string output to make it nicer...
2445 for (unsigned i = 0; i != OutStr.length(); ++i)
2446 if (OutStr[i] == '\n') { // Left justify
2447 OutStr[i] = '\\';
2448 OutStr.insert(OutStr.begin()+i+1, 'l');
2449 }
Mike Stump31feda52009-07-17 01:31:16 +00002450
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002451 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002452#else
2453 return "";
2454#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002455 }
2456};
2457} // end namespace llvm