blob: 5977de609062b7e8046cc922b1aee14c74789891 [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);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000174
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.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000275 CFGBlock *B = addStmt(Statement);
276
277 if (badCFG)
278 return NULL;
279
280 if (B)
281 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000282
283 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
284 // FIXME: Add code for base initializers and member initializers.
285 (void)CD;
286 }
Mike Stump31feda52009-07-17 01:31:16 +0000287
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000288 // Backpatch the gotos whose label -> block mappings we didn't know when we
289 // encountered them.
290 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
291 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000292
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000293 CFGBlock* B = *I;
294 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
295 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000296
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000297 // If there is no target for the goto, then we are looking at an
298 // incomplete AST. Handle this by not registering a successor.
299 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000300
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000301 AddSuccessor(B, LI->second);
302 }
303
304 // Add successors to the Indirect Goto Dispatch block (if we have one).
305 if (CFGBlock* B = cfg->getIndirectGotoBlock())
306 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
307 E = AddressTakenLabels.end(); I != E; ++I ) {
308
309 // Lookup the target block.
310 LabelMapTy::iterator LI = LabelMap.find(*I);
311
312 // If there is no target block that contains label, then we are looking
313 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000314 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000315
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000316 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000317 }
Mike Stump31feda52009-07-17 01:31:16 +0000318
Mike Stump31feda52009-07-17 01:31:16 +0000319 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000320 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000321
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000322 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000323}
Mike Stump31feda52009-07-17 01:31:16 +0000324
Ted Kremenek9aae5132007-08-23 21:42:29 +0000325/// createBlock - Used to lazily create blocks that are connected
326/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000327CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000328 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000329 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000330 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000331 return B;
332}
Mike Stump31feda52009-07-17 01:31:16 +0000333
Ted Kremenek93668002009-07-17 22:18:43 +0000334/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000335/// blocks for ternary operators, &&, and ||. We also process "," and
336/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000337CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000338tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000339 if (!S) {
340 badCFG = true;
341 return 0;
342 }
Ted Kremenek93668002009-07-17 22:18:43 +0000343 switch (S->getStmtClass()) {
344 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000345 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000346
347 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000348 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000349
Ted Kremenek93668002009-07-17 22:18:43 +0000350 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000351 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000352
Ted Kremenek93668002009-07-17 22:18:43 +0000353 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000354 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000355
Ted Kremenek93668002009-07-17 22:18:43 +0000356 case Stmt::BreakStmtClass:
357 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000358
Ted Kremenek93668002009-07-17 22:18:43 +0000359 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000360 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000361 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000362
Ted Kremenek93668002009-07-17 22:18:43 +0000363 case Stmt::CaseStmtClass:
364 return VisitCaseStmt(cast<CaseStmt>(S));
365
366 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000367 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000368
Ted Kremenek93668002009-07-17 22:18:43 +0000369 case Stmt::CompoundStmtClass:
370 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000371
Ted Kremenek93668002009-07-17 22:18:43 +0000372 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000373 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000374
Ted Kremenek93668002009-07-17 22:18:43 +0000375 case Stmt::ContinueStmtClass:
376 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000377
Ted Kremenekb27378c2010-01-19 20:40:33 +0000378 case Stmt::CXXCatchStmtClass:
379 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
380
Ted Kremenek82bfc862010-08-28 00:19:02 +0000381 case Stmt::CXXExprWithTemporariesClass: {
382 // FIXME: Handle temporaries. For now, just visit the subexpression
383 // so we don't artificially create extra blocks.
Ted Kremenek128d04d2010-08-31 18:47:34 +0000384 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000385 }
386
Zhongxing Xu7e612172010-04-13 09:38:01 +0000387 case Stmt::CXXMemberCallExprClass:
388 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
389
Ted Kremenekb27378c2010-01-19 20:40:33 +0000390 case Stmt::CXXThrowExprClass:
391 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000392
Ted Kremenekb27378c2010-01-19 20:40:33 +0000393 case Stmt::CXXTryStmtClass:
394 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000395
Ted Kremenek93668002009-07-17 22:18:43 +0000396 case Stmt::DeclStmtClass:
397 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000398
Ted Kremenek93668002009-07-17 22:18:43 +0000399 case Stmt::DefaultStmtClass:
400 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000401
Ted Kremenek93668002009-07-17 22:18:43 +0000402 case Stmt::DoStmtClass:
403 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000404
Ted Kremenek93668002009-07-17 22:18:43 +0000405 case Stmt::ForStmtClass:
406 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000407
Ted Kremenek93668002009-07-17 22:18:43 +0000408 case Stmt::GotoStmtClass:
409 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000410
Ted Kremenek93668002009-07-17 22:18:43 +0000411 case Stmt::IfStmtClass:
412 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000413
Ted Kremenek93668002009-07-17 22:18:43 +0000414 case Stmt::IndirectGotoStmtClass:
415 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000416
Ted Kremenek93668002009-07-17 22:18:43 +0000417 case Stmt::LabelStmtClass:
418 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000419
Ted Kremenek5868ec62010-04-11 17:02:10 +0000420 case Stmt::MemberExprClass:
421 return VisitMemberExpr(cast<MemberExpr>(S), asc);
422
Ted Kremenek93668002009-07-17 22:18:43 +0000423 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000424 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
425
Ted Kremenek93668002009-07-17 22:18:43 +0000426 case Stmt::ObjCAtSynchronizedStmtClass:
427 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000428
Ted Kremenek93668002009-07-17 22:18:43 +0000429 case Stmt::ObjCAtThrowStmtClass:
430 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000431
Ted Kremenek93668002009-07-17 22:18:43 +0000432 case Stmt::ObjCAtTryStmtClass:
433 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000434
Ted Kremenek93668002009-07-17 22:18:43 +0000435 case Stmt::ObjCForCollectionStmtClass:
436 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000437
Ted Kremenek93668002009-07-17 22:18:43 +0000438 case Stmt::ParenExprClass:
439 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000440 goto tryAgain;
441
Ted Kremenek93668002009-07-17 22:18:43 +0000442 case Stmt::NullStmtClass:
443 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000444
Ted Kremenek93668002009-07-17 22:18:43 +0000445 case Stmt::ReturnStmtClass:
446 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000447
Ted Kremenek93668002009-07-17 22:18:43 +0000448 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000449 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000450
Ted Kremenek93668002009-07-17 22:18:43 +0000451 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000452 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000453
Ted Kremenek93668002009-07-17 22:18:43 +0000454 case Stmt::SwitchStmtClass:
455 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000456
Ted Kremenek93668002009-07-17 22:18:43 +0000457 case Stmt::WhileStmtClass:
458 return VisitWhileStmt(cast<WhileStmt>(S));
459 }
460}
Mike Stump11289f42009-09-09 15:08:12 +0000461
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000462CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
463 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000464 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000465 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000466 }
Mike Stump11289f42009-09-09 15:08:12 +0000467
Ted Kremenek93668002009-07-17 22:18:43 +0000468 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000469}
Mike Stump31feda52009-07-17 01:31:16 +0000470
Ted Kremenek93668002009-07-17 22:18:43 +0000471/// VisitChildren - Visit the children of a Stmt.
472CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
473 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000474 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000475 E = Terminator->child_end(); I != E; ++I) {
476 if (*I) B = Visit(*I);
477 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000478 return B;
479}
Mike Stump11289f42009-09-09 15:08:12 +0000480
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000481CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
482 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000483 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000484
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000485 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000486 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000487 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000488 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000489
Ted Kremenek9aae5132007-08-23 21:42:29 +0000490 return Block;
491}
Mike Stump11289f42009-09-09 15:08:12 +0000492
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000493CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
494 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000495 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000496 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000497 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000498
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000499 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000500 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000501
Ted Kremenek93668002009-07-17 22:18:43 +0000502 // create the block evaluating the LHS
503 CFGBlock* LHSBlock = createBlock(false);
504 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000505
Ted Kremenek93668002009-07-17 22:18:43 +0000506 // create the block evaluating the RHS
507 Succ = ConfluenceBlock;
508 Block = NULL;
509 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000510
511 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000512 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000513 return 0;
514 }
515 else {
516 // Create an empty block for cases where the RHS doesn't require
517 // any explicit statements in the CFG.
518 RHSBlock = createBlock();
519 }
Mike Stump11289f42009-09-09 15:08:12 +0000520
Mike Stump773582d2009-07-23 23:25:26 +0000521 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000522 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000523 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000524 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000525
Ted Kremenek93668002009-07-17 22:18:43 +0000526 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000527 if (B->getOpcode() == BO_LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000528 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
529 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000530 } else {
John McCalle3027922010-08-25 11:45:40 +0000531 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000532 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
533 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000534 }
Mike Stump11289f42009-09-09 15:08:12 +0000535
Ted Kremenek93668002009-07-17 22:18:43 +0000536 // Generate the blocks for evaluating the LHS.
537 Block = LHSBlock;
538 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000539 }
John McCalle3027922010-08-25 11:45:40 +0000540 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000541 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000542 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000543 addStmt(B->getRHS());
544 return addStmt(B->getLHS());
545 }
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000546 else if (B->isAssignmentOp()) {
547 if (asc.alwaysAdd()) {
548 autoCreateBlock();
549 AppendStmt(Block, B, asc);
550 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000551
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000552 Visit(B->getRHS());
553 return Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
554 }
Mike Stump11289f42009-09-09 15:08:12 +0000555
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000556 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000557}
558
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000559CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
560 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000561 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000562 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000563 }
564 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000565}
566
Ted Kremenek93668002009-07-17 22:18:43 +0000567CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
568 // "break" is a control-flow statement. Thus we stop processing the current
569 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000570 if (badCFG)
571 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000572
Ted Kremenek93668002009-07-17 22:18:43 +0000573 // Now create a new block that ends with the break statement.
574 Block = createBlock(false);
575 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000576
Ted Kremenek93668002009-07-17 22:18:43 +0000577 // If there is no target for the break, then we are looking at an incomplete
578 // AST. This means that the CFG cannot be constructed.
579 if (BreakTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000580 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000581 else
582 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000583
584
Ted Kremenek9aae5132007-08-23 21:42:29 +0000585 return Block;
586}
Mike Stump11289f42009-09-09 15:08:12 +0000587
Mike Stump04c68512010-01-21 15:20:48 +0000588static bool CanThrow(Expr *E) {
589 QualType Ty = E->getType();
590 if (Ty->isFunctionPointerType())
591 Ty = Ty->getAs<PointerType>()->getPointeeType();
592 else if (Ty->isBlockPointerType())
593 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000594
Mike Stump04c68512010-01-21 15:20:48 +0000595 const FunctionType *FT = Ty->getAs<FunctionType>();
596 if (FT) {
597 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
598 if (Proto->hasEmptyExceptionSpec())
599 return false;
600 }
601 return true;
602}
603
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000604CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000605 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000606 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000607 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000608 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000609 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000610
Mike Stump04c68512010-01-21 15:20:48 +0000611 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000612
613 // Languages without exceptions are assumed to not throw.
614 if (Context->getLangOptions().Exceptions) {
Mike Stump04c68512010-01-21 15:20:48 +0000615 if (AddEHEdges)
616 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +0000617 }
618
619 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000620 if (FD->hasAttr<NoReturnAttr>())
621 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +0000622 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +0000623 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000624 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000625
Mike Stump04c68512010-01-21 15:20:48 +0000626 if (!CanThrow(C->getCallee()))
627 AddEHEdge = false;
628
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000629 if (!NoReturn && !AddEHEdge) {
630 if (asc.asLValue())
631 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
632 else
633 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
634 }
Mike Stump11289f42009-09-09 15:08:12 +0000635
Mike Stump92244b02010-01-19 22:00:14 +0000636 if (Block) {
637 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000638 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +0000639 return 0;
640 }
Mike Stump11289f42009-09-09 15:08:12 +0000641
Mike Stump92244b02010-01-19 22:00:14 +0000642 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000643 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000644
Mike Stump92244b02010-01-19 22:00:14 +0000645 if (NoReturn) {
646 // Wire this to the exit block directly.
647 AddSuccessor(Block, &cfg->getExit());
648 }
Mike Stump04c68512010-01-21 15:20:48 +0000649 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +0000650 // Add exceptional edges.
651 if (TryTerminatedBlock)
652 AddSuccessor(Block, TryTerminatedBlock);
653 else
654 AddSuccessor(Block, &cfg->getExit());
655 }
Mike Stump11289f42009-09-09 15:08:12 +0000656
Mike Stump8c5d7992009-07-25 21:26:53 +0000657 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000658}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000659
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000660CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
661 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000662 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000663 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000664 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000665 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000666
Ted Kremenek5868ec62010-04-11 17:02:10 +0000667 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
668 : AddStmtChoice::AlwaysAdd;
669
Ted Kremenek21822592009-07-17 18:20:32 +0000670 Succ = ConfluenceBlock;
671 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000672 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000673 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000674 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000675
Ted Kremenek21822592009-07-17 18:20:32 +0000676 Succ = ConfluenceBlock;
677 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000678 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000679 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000680 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000681
Ted Kremenek21822592009-07-17 18:20:32 +0000682 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000683 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000684 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000685 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
686 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000687 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000688 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000689}
Mike Stump11289f42009-09-09 15:08:12 +0000690
691
692CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump92244b02010-01-19 22:00:14 +0000693 EndScope(C);
694
Mike Stump11289f42009-09-09 15:08:12 +0000695 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000696
697 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
698 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +0000699 // If we hit a segment of code just containing ';' (NullStmts), we can
700 // get a null block back. In such cases, just use the LastBlock
701 if (CFGBlock *newBlock = addStmt(*I))
702 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +0000703
Ted Kremenekce499c22009-08-27 23:16:26 +0000704 if (badCFG)
705 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000706 }
Mike Stump92244b02010-01-19 22:00:14 +0000707
708 LastBlock = StartScope(C, LastBlock);
709
Ted Kremenek93668002009-07-17 22:18:43 +0000710 return LastBlock;
711}
Mike Stump11289f42009-09-09 15:08:12 +0000712
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000713CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
714 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000715 // Create the confluence block that will "merge" the results of the ternary
716 // expression.
717 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000718 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000719 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +0000720 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000721
Ted Kremenek5868ec62010-04-11 17:02:10 +0000722 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
723 : AddStmtChoice::AlwaysAdd;
724
Ted Kremenek51d40b02009-07-17 18:15:54 +0000725 // Create a block for the LHS expression if there is an LHS expression. A
726 // GCC extension allows LHS to be NULL, causing the condition to be the
727 // value that is returned instead.
728 // e.g: x ?: y is shorthand for: x ? x : y;
729 Succ = ConfluenceBlock;
730 Block = NULL;
731 CFGBlock* LHSBlock = NULL;
732 if (C->getLHS()) {
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000733 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000734 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +0000735 return 0;
736 Block = NULL;
737 }
Mike Stump11289f42009-09-09 15:08:12 +0000738
Ted Kremenek51d40b02009-07-17 18:15:54 +0000739 // Create the block for the RHS expression.
740 Succ = ConfluenceBlock;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000741 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000742 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +0000743 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000744
Ted Kremenek51d40b02009-07-17 18:15:54 +0000745 // Create the block that will contain the condition.
746 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000747
Mike Stump773582d2009-07-23 23:25:26 +0000748 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000749 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000750 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000751 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000752 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000753 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000754 // If we know the condition is false, add NULL as the successor for
755 // the block containing the condition. In this case, the confluence
756 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000757 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000758 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000759 } else {
760 // If we have no LHS expression, add the ConfluenceBlock as a direct
761 // successor for the block containing the condition. Moreover, we need to
762 // reverse the order of the predecessors in the ConfluenceBlock because
763 // the RHSBlock will have been added to the succcessors already, and we
764 // want the first predecessor to the the block containing the expression
765 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000766 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000767 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000768 std::reverse(ConfluenceBlock->pred_begin(),
769 ConfluenceBlock->pred_end());
770 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000771 }
Mike Stump11289f42009-09-09 15:08:12 +0000772
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000773 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000774 Block->setTerminator(C);
775 return addStmt(C->getCond());
776}
777
Ted Kremenek93668002009-07-17 22:18:43 +0000778CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
779 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000780
Ted Kremenek93668002009-07-17 22:18:43 +0000781 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000782 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000783 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000784 }
Mike Stump11289f42009-09-09 15:08:12 +0000785
Ted Kremenek93668002009-07-17 22:18:43 +0000786 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000787
Ted Kremenek93668002009-07-17 22:18:43 +0000788 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
789 typedef llvm::SmallVector<Decl*,10> BufTy;
790 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000791
Ted Kremenek93668002009-07-17 22:18:43 +0000792 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
793 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
794 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
795 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000796
Ted Kremenek93668002009-07-17 22:18:43 +0000797 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
798 // automatically freed with the CFG.
799 DeclGroupRef DG(*I);
800 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000801 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000802 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000803
Ted Kremenek93668002009-07-17 22:18:43 +0000804 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000805 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000806 B = VisitDeclSubExpr(D);
807 }
Mike Stump11289f42009-09-09 15:08:12 +0000808
809 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000810}
Mike Stump11289f42009-09-09 15:08:12 +0000811
Ted Kremenek93668002009-07-17 22:18:43 +0000812/// VisitDeclSubExpr - Utility method to add block-level expressions for
813/// initializers in Decls.
814CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
815 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000816
Ted Kremenek93668002009-07-17 22:18:43 +0000817 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000818
Ted Kremenek93668002009-07-17 22:18:43 +0000819 if (!VD)
820 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000821
Ted Kremenek93668002009-07-17 22:18:43 +0000822 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000823
Ted Kremenek93668002009-07-17 22:18:43 +0000824 if (Init) {
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000825 AddStmtChoice::Kind k =
826 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
827 : AddStmtChoice::NotAlwaysAdd;
828 Visit(Init, AddStmtChoice(k));
Ted Kremenek93668002009-07-17 22:18:43 +0000829 }
Mike Stump11289f42009-09-09 15:08:12 +0000830
Ted Kremenek93668002009-07-17 22:18:43 +0000831 // If the type of VD is a VLA, then we must process its size expressions.
832 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
833 VA = FindVA(VA->getElementType().getTypePtr()))
834 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000835
Ted Kremenek93668002009-07-17 22:18:43 +0000836 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000837}
838
839CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000840 // We may see an if statement in the middle of a basic block, or it may be the
841 // first statement we are processing. In either case, we create a new basic
842 // block. First, we create the blocks for the then...else statements, and
843 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000844 // middle of a block, we stop processing that block. That block is then the
845 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000846
847 // The block we were proccessing is now finished. Make it the successor
848 // block.
849 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000850 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000851 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000852 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000853 }
Mike Stump31feda52009-07-17 01:31:16 +0000854
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000855 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000856 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000857
Ted Kremenek9aae5132007-08-23 21:42:29 +0000858 if (Stmt* Else = I->getElse()) {
859 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000860
Ted Kremenek9aae5132007-08-23 21:42:29 +0000861 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000862 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000863 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000864 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000865
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000866 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
867 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000868 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000869 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000870 return 0;
871 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000872 }
Mike Stump31feda52009-07-17 01:31:16 +0000873
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000874 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000875 CFGBlock* ThenBlock;
876 {
877 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +0000878 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000879 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000880 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000881 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000882
Ted Kremenek1b379512009-04-01 03:52:47 +0000883 if (!ThenBlock) {
884 // We can reach here if the "then" body has all NullStmts.
885 // Create an empty block so we can distinguish between true and false
886 // branches in path-sensitive analyses.
887 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000888 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000889 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000890 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000891 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000892 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000893 }
894
Mike Stump31feda52009-07-17 01:31:16 +0000895 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000896 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000897
Ted Kremenek9aae5132007-08-23 21:42:29 +0000898 // Set the terminator of the new block to the If statement.
899 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000900
Mike Stump773582d2009-07-23 23:25:26 +0000901 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000902 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000903
Ted Kremenek9aae5132007-08-23 21:42:29 +0000904 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000905 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
906 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000907
908 // Add the condition as the last statement in the new block. This may create
909 // new blocks as the condition may contain control-flow. Any newly created
910 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000911 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000912
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000913 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
914 // and the condition variable initialization to the CFG.
915 if (VarDecl *VD = I->getConditionVariable()) {
916 if (Expr *Init = VD->getInit()) {
917 autoCreateBlock();
918 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
919 addStmt(Init);
920 }
921 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000922
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000923 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000924}
Mike Stump31feda52009-07-17 01:31:16 +0000925
926
Ted Kremenek9aae5132007-08-23 21:42:29 +0000927CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000928 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000929 //
Mike Stump31feda52009-07-17 01:31:16 +0000930 // NOTE: If a "return" appears in the middle of a block, this means that the
931 // code afterwards is DEAD (unreachable). We still keep a basic block
932 // for that code; a simple "mark-and-sweep" from the entry block will be
933 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000934
935 // Create the new block.
936 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000937
Ted Kremenek9aae5132007-08-23 21:42:29 +0000938 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000939 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000940
941 // Add the return statement to the block. This may create new blocks if R
942 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000943 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000944}
945
946CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
947 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000948 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000949 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000950
Ted Kremenek93668002009-07-17 22:18:43 +0000951 if (!LabelBlock) // This can happen when the body is empty, i.e.
952 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000953
Ted Kremenek93668002009-07-17 22:18:43 +0000954 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000955 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000956
957 // Labels partition blocks, so this is the end of the basic block we were
958 // processing (L is the block's label). Because this is label (and we have
959 // already processed the substatement) there is no extra control-flow to worry
960 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000961 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000962 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000963 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000964
965 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000966 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000967
Ted Kremenek9aae5132007-08-23 21:42:29 +0000968 // This block is now the implicit successor of other blocks.
969 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000970
Ted Kremenek9aae5132007-08-23 21:42:29 +0000971 return LabelBlock;
972}
973
974CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000975 // Goto is a control-flow statement. Thus we stop processing the current
976 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000977
Ted Kremenek9aae5132007-08-23 21:42:29 +0000978 Block = createBlock(false);
979 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000980
981 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000982 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000983
Ted Kremenek9aae5132007-08-23 21:42:29 +0000984 if (I == LabelMap.end())
985 // We will need to backpatch this block later.
986 BackpatchBlocks.push_back(Block);
987 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000988 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000989
Mike Stump31feda52009-07-17 01:31:16 +0000990 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000991}
992
993CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000994 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000995
Mike Stump014b3ea2009-07-21 01:12:51 +0000996 // "for" is a control-flow statement. Thus we stop processing the current
997 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000998 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000999 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001000 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001001 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001002 } else
1003 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001004
Ted Kremenek304a9532010-05-21 20:30:15 +00001005 // Save the current value for the break targets.
1006 // All breaks should go to the code following the loop.
1007 SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock);
1008 BreakTargetBlock = LoopSuccessor;
1009
Mike Stump31feda52009-07-17 01:31:16 +00001010 // Because of short-circuit evaluation, the condition of the loop can span
1011 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1012 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001013 CFGBlock* ExitConditionBlock = createBlock(false);
1014 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001015
Ted Kremenek81e14852007-08-27 19:46:09 +00001016 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001017 ExitConditionBlock->setTerminator(F);
1018
1019 // Now add the actual condition to the condition block. Because the condition
1020 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001021 if (Stmt* C = F->getCond()) {
1022 Block = ExitConditionBlock;
1023 EntryConditionBlock = addStmt(C);
Ted Kremenekec92f942009-12-24 01:49:06 +00001024 assert(Block == EntryConditionBlock);
1025
1026 // If this block contains a condition variable, add both the condition
1027 // variable and initializer to the CFG.
1028 if (VarDecl *VD = F->getConditionVariable()) {
1029 if (Expr *Init = VD->getInit()) {
1030 autoCreateBlock();
1031 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1032 EntryConditionBlock = addStmt(Init);
1033 assert(Block == EntryConditionBlock);
1034 }
1035 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001036
Ted Kremenek55957a82009-05-02 00:13:27 +00001037 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001038 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001039 return 0;
1040 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001041 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001042
Mike Stump31feda52009-07-17 01:31:16 +00001043 // The condition block is the implicit successor for the loop body as well as
1044 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001045 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001046
Mike Stump773582d2009-07-23 23:25:26 +00001047 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001048 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001049
Mike Stump773582d2009-07-23 23:25:26 +00001050 if (F->getCond())
1051 KnownVal = TryEvaluateBool(F->getCond());
1052
Ted Kremenek9aae5132007-08-23 21:42:29 +00001053 // Now create the loop body.
1054 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001055 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001056
Ted Kremenek304a9532010-05-21 20:30:15 +00001057 // Save the current values for Block, Succ, and continue targets.
1058 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1059 save_continue(ContinueTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001060
Ted Kremeneke9610502007-08-30 18:39:40 +00001061 // Create a new block to contain the (bottom) of the loop body.
1062 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001063
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001064 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001065 // Generate increment code in its own basic block. This is the target of
1066 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001067 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001068 } else {
1069 // No increment code. Create a special, empty, block that is used as the
1070 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001071 assert(Succ == EntryConditionBlock);
1072 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001073 }
Mike Stump31feda52009-07-17 01:31:16 +00001074
Ted Kremenek902393b2009-04-28 00:51:56 +00001075 // Finish up the increment (or empty) block if it hasn't been already.
1076 if (Block) {
1077 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001078 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001079 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001080 Block = 0;
1081 }
Mike Stump31feda52009-07-17 01:31:16 +00001082
Ted Kremenek902393b2009-04-28 00:51:56 +00001083 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001084
Ted Kremenek902393b2009-04-28 00:51:56 +00001085 // The starting block for the loop increment is the block that should
1086 // represent the 'loop target' for looping back to the start of the loop.
1087 ContinueTargetBlock->setLoopTarget(F);
1088
Mike Stump31feda52009-07-17 01:31:16 +00001089 // Now populate the body block, and in the process create new blocks as we
1090 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001091 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001092
1093 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +00001094 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001095 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001096 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001097
Ted Kremenek30754282009-07-24 04:47:11 +00001098 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001099 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001100 }
Mike Stump31feda52009-07-17 01:31:16 +00001101
Ted Kremenek30754282009-07-24 04:47:11 +00001102 // Link up the condition block with the code that follows the loop. (the
1103 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001104 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001105
Ted Kremenek9aae5132007-08-23 21:42:29 +00001106 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001107 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001108 if (Stmt* I = F->getInit()) {
1109 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001110 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001111 } else {
1112 // There is no loop initialization. We are thus basically a while loop.
1113 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001114 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001115 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001116 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001117 }
1118}
1119
Ted Kremenek5868ec62010-04-11 17:02:10 +00001120CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1121 if (asc.alwaysAdd()) {
1122 autoCreateBlock();
1123 AppendStmt(Block, M, asc);
1124 }
1125 return Visit(M->getBase(),
1126 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1127 : AddStmtChoice::AsLValueNotAlwaysAdd);
1128}
1129
Ted Kremenek9d56e642008-11-11 17:10:00 +00001130CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1131 // Objective-C fast enumeration 'for' statements:
1132 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1133 //
1134 // for ( Type newVariable in collection_expression ) { statements }
1135 //
1136 // becomes:
1137 //
1138 // prologue:
1139 // 1. collection_expression
1140 // T. jump to loop_entry
1141 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001142 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001143 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1144 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1145 // TB:
1146 // statements
1147 // T. jump to loop_entry
1148 // FB:
1149 // what comes after
1150 //
1151 // and
1152 //
1153 // Type existingItem;
1154 // for ( existingItem in expression ) { statements }
1155 //
1156 // becomes:
1157 //
Mike Stump31feda52009-07-17 01:31:16 +00001158 // the same with newVariable replaced with existingItem; the binding works
1159 // the same except that for one ObjCForCollectionStmt::getElement() returns
1160 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001161 //
Mike Stump31feda52009-07-17 01:31:16 +00001162
Ted Kremenek9d56e642008-11-11 17:10:00 +00001163 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001164
Ted Kremenek9d56e642008-11-11 17:10:00 +00001165 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001166 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001167 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001168 LoopSuccessor = Block;
1169 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001170 } else
1171 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001172
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001173 // Build the condition blocks.
1174 CFGBlock* ExitConditionBlock = createBlock(false);
1175 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001176
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001177 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001178 ExitConditionBlock->setTerminator(S);
1179
1180 // The last statement in the block should be the ObjCForCollectionStmt, which
1181 // performs the actual binding to 'element' and determines if there are any
1182 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001183 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001184 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001185
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001186 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001187 // generate new blocks as necesary. We DON'T add the statement by default to
1188 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001189 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001190 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001191 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001192 return 0;
1193 Block = 0;
1194 }
Mike Stump31feda52009-07-17 01:31:16 +00001195
1196 // The condition block is the implicit successor for the loop body as well as
1197 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001198 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001199
Ted Kremenek9d56e642008-11-11 17:10:00 +00001200 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001201 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001202 // Save the current values for Succ, continue and break targets.
1203 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001204 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1205
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001206 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001207 ContinueTargetBlock = EntryConditionBlock;
1208
Ted Kremenek93668002009-07-17 22:18:43 +00001209 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001210
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001211 if (!BodyBlock)
1212 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001213 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001214 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001215 return 0;
1216 }
Mike Stump31feda52009-07-17 01:31:16 +00001217
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001218 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001219 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001220 }
Mike Stump31feda52009-07-17 01:31:16 +00001221
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001222 // Link up the condition block with the code that follows the loop.
1223 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001224 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001225
Ted Kremenek9d56e642008-11-11 17:10:00 +00001226 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001227 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001228 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001229}
1230
Ted Kremenek49805452009-05-02 01:49:13 +00001231CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1232 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001233
Ted Kremenek49805452009-05-02 01:49:13 +00001234 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001235 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001236
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001237 // The sync body starts its own basic block. This makes it a little easier
1238 // for diagnostic clients.
1239 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001240 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001241 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001242
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001243 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001244 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001245 }
Mike Stump31feda52009-07-17 01:31:16 +00001246
Ted Kremenek49805452009-05-02 01:49:13 +00001247 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001248 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001249}
Mike Stump31feda52009-07-17 01:31:16 +00001250
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001251CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001252 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001253 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001254}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001255
Ted Kremenek9aae5132007-08-23 21:42:29 +00001256CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001257 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001258
Mike Stump014b3ea2009-07-21 01:12:51 +00001259 // "while" is a control-flow statement. Thus we stop processing the current
1260 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001261 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001262 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001263 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001264 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001265 } else
1266 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001267
1268 // Because of short-circuit evaluation, the condition of the loop can span
1269 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1270 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001271 CFGBlock* ExitConditionBlock = createBlock(false);
1272 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001273
Ted Kremenek81e14852007-08-27 19:46:09 +00001274 // Set the terminator for the "exit" condition block.
1275 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001276
1277 // Now add the actual condition to the condition block. Because the condition
1278 // itself may contain control-flow, new blocks may be created. Thus we update
1279 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001280 if (Stmt* C = W->getCond()) {
1281 Block = ExitConditionBlock;
1282 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001283 assert(Block == EntryConditionBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001284
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001285 // If this block contains a condition variable, add both the condition
1286 // variable and initializer to the CFG.
1287 if (VarDecl *VD = W->getConditionVariable()) {
1288 if (Expr *Init = VD->getInit()) {
1289 autoCreateBlock();
1290 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1291 EntryConditionBlock = addStmt(Init);
1292 assert(Block == EntryConditionBlock);
1293 }
1294 }
1295
Ted Kremenek55957a82009-05-02 00:13:27 +00001296 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001297 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001298 return 0;
1299 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001300 }
Mike Stump31feda52009-07-17 01:31:16 +00001301
1302 // The condition block is the implicit successor for the loop body as well as
1303 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001304 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001305
Mike Stump773582d2009-07-23 23:25:26 +00001306 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001307 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001308
Ted Kremenek9aae5132007-08-23 21:42:29 +00001309 // Process the loop body.
1310 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001311 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001312
1313 // Save the current values for Block, Succ, and continue and break targets
1314 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1315 save_continue(ContinueTargetBlock),
1316 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001317
Mike Stump31feda52009-07-17 01:31:16 +00001318 // Create an empty block to represent the transition block for looping back
1319 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001320 Block = 0;
1321 assert(Succ == EntryConditionBlock);
1322 Succ = createBlock();
1323 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001324 ContinueTargetBlock = Succ;
1325
Ted Kremenek9aae5132007-08-23 21:42:29 +00001326 // All breaks should go to the code following the loop.
1327 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001328
Ted Kremenek9aae5132007-08-23 21:42:29 +00001329 // NULL out Block to force lazy instantiation of blocks for the body.
1330 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001331
Ted Kremenek9aae5132007-08-23 21:42:29 +00001332 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001333 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001334
Ted Kremeneke9610502007-08-30 18:39:40 +00001335 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001336 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001337 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001338 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001339 return 0;
1340 }
Mike Stump31feda52009-07-17 01:31:16 +00001341
Ted Kremenek30754282009-07-24 04:47:11 +00001342 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001343 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001344 }
Mike Stump31feda52009-07-17 01:31:16 +00001345
Ted Kremenek30754282009-07-24 04:47:11 +00001346 // Link up the condition block with the code that follows the loop. (the
1347 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001348 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001349
1350 // There can be no more statements in the condition block since we loop back
1351 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001352 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001353
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001354 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001355 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001356 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001357}
Mike Stump11289f42009-09-09 15:08:12 +00001358
1359
Ted Kremenek93668002009-07-17 22:18:43 +00001360CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1361 // FIXME: For now we pretend that @catch and the code it contains does not
1362 // exit.
1363 return Block;
1364}
Mike Stump31feda52009-07-17 01:31:16 +00001365
Ted Kremenek93041ba2008-12-09 20:20:09 +00001366CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1367 // FIXME: This isn't complete. We basically treat @throw like a return
1368 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001369
Ted Kremenek0868eea2009-09-24 18:45:41 +00001370 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001371 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001372 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001373
Ted Kremenek93041ba2008-12-09 20:20:09 +00001374 // Create the new block.
1375 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001376
Ted Kremenek93041ba2008-12-09 20:20:09 +00001377 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001378 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001379
1380 // Add the statement to the block. This may create new blocks if S contains
1381 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001382 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001383}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001384
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001385CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001386 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001387 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001388 return 0;
1389
1390 // Create the new block.
1391 Block = createBlock(false);
1392
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001393 if (TryTerminatedBlock)
1394 // The current try statement is the only successor.
1395 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001396 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001397 // otherwise the Exit block is the only successor.
1398 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001399
1400 // Add the statement to the block. This may create new blocks if S contains
1401 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001402 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001403}
1404
Ted Kremenek93668002009-07-17 22:18:43 +00001405CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001406 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001407
Mike Stump8d50b6a2009-07-21 01:27:50 +00001408 // "do...while" is a control-flow statement. Thus we stop processing the
1409 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001410 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001411 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001412 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001413 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001414 } else
1415 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001416
1417 // Because of short-circuit evaluation, the condition of the loop can span
1418 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1419 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001420 CFGBlock* ExitConditionBlock = createBlock(false);
1421 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001422
Ted Kremenek81e14852007-08-27 19:46:09 +00001423 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001424 ExitConditionBlock->setTerminator(D);
1425
1426 // Now add the actual condition to the condition block. Because the condition
1427 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001428 if (Stmt* C = D->getCond()) {
1429 Block = ExitConditionBlock;
1430 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001431 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001432 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001433 return 0;
1434 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001435 }
Mike Stump31feda52009-07-17 01:31:16 +00001436
Ted Kremeneka1523a32008-02-27 07:20:00 +00001437 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001438 Succ = EntryConditionBlock;
1439
Mike Stump773582d2009-07-23 23:25:26 +00001440 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001441 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001442
Ted Kremenek9aae5132007-08-23 21:42:29 +00001443 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001444 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001445 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001446 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001447
Ted Kremenek9aae5132007-08-23 21:42:29 +00001448 // Save the current values for Block, Succ, and continue and break targets
1449 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001450 save_continue(ContinueTargetBlock),
1451 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001452
Ted Kremenek9aae5132007-08-23 21:42:29 +00001453 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001454 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001455
Ted Kremenek9aae5132007-08-23 21:42:29 +00001456 // All breaks should go to the code following the loop.
1457 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001458
Ted Kremenek9aae5132007-08-23 21:42:29 +00001459 // NULL out Block to force lazy instantiation of blocks for the body.
1460 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001461
Ted Kremenek9aae5132007-08-23 21:42:29 +00001462 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001463 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001464
Ted Kremeneke9610502007-08-30 18:39:40 +00001465 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001466 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001467 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001468 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001469 return 0;
1470 }
Mike Stump31feda52009-07-17 01:31:16 +00001471
Ted Kremenek110974d2010-08-17 20:59:56 +00001472 if (!KnownVal.isFalse()) {
1473 // Add an intermediate block between the BodyBlock and the
1474 // ExitConditionBlock to represent the "loop back" transition. Create an
1475 // empty block to represent the transition block for looping back to the
1476 // head of the loop.
1477 // FIXME: Can we do this more efficiently without adding another block?
1478 Block = NULL;
1479 Succ = BodyBlock;
1480 CFGBlock *LoopBackBlock = createBlock();
1481 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001482
Ted Kremenek110974d2010-08-17 20:59:56 +00001483 // Add the loop body entry as a successor to the condition.
1484 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1485 }
1486 else
1487 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001488 }
Mike Stump31feda52009-07-17 01:31:16 +00001489
Ted Kremenek30754282009-07-24 04:47:11 +00001490 // Link up the condition block with the code that follows the loop.
1491 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001492 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001493
1494 // There can be no more statements in the body block(s) since we loop back to
1495 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001496 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001497
Ted Kremenek9aae5132007-08-23 21:42:29 +00001498 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001499 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001500 return BodyBlock;
1501}
1502
1503CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1504 // "continue" is a control-flow statement. Thus we stop processing the
1505 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001506 if (badCFG)
1507 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001508
Ted Kremenek9aae5132007-08-23 21:42:29 +00001509 // Now create a new block that ends with the continue statement.
1510 Block = createBlock(false);
1511 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001512
Ted Kremenek9aae5132007-08-23 21:42:29 +00001513 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001514 // incomplete AST. This means the CFG cannot be constructed.
1515 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001516 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001517 else
1518 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001519
Ted Kremenek9aae5132007-08-23 21:42:29 +00001520 return Block;
1521}
Mike Stump11289f42009-09-09 15:08:12 +00001522
Ted Kremenek0747de62009-07-18 00:47:21 +00001523CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001524 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001525
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001526 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001527 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001528 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001529 }
Mike Stump11289f42009-09-09 15:08:12 +00001530
Ted Kremenek93668002009-07-17 22:18:43 +00001531 // VLA types have expressions that must be evaluated.
1532 if (E->isArgumentType()) {
1533 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1534 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1535 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001536 }
Mike Stump11289f42009-09-09 15:08:12 +00001537
Mike Stump31feda52009-07-17 01:31:16 +00001538 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001539}
Mike Stump11289f42009-09-09 15:08:12 +00001540
Ted Kremenek93668002009-07-17 22:18:43 +00001541/// VisitStmtExpr - Utility method to handle (nested) statement
1542/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001543CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1544 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001545 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001546 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001547 }
Ted Kremenek93668002009-07-17 22:18:43 +00001548 return VisitCompoundStmt(SE->getSubStmt());
1549}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001550
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001551CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001552 // "switch" is a control-flow statement. Thus we stop processing the current
1553 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001554 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001555
Ted Kremenek9aae5132007-08-23 21:42:29 +00001556 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001557 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001558 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001559 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001560 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001561
1562 // Save the current "switch" context.
1563 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001564 save_break(BreakTargetBlock),
1565 save_default(DefaultCaseBlock);
1566
Mike Stump31feda52009-07-17 01:31:16 +00001567 // Set the "default" case to be the block after the switch statement. If the
1568 // switch statement contains a "default:", this value will be overwritten with
1569 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001570 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001571
Ted Kremenek9aae5132007-08-23 21:42:29 +00001572 // Create a new block that will contain the switch statement.
1573 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001574
Ted Kremenek9aae5132007-08-23 21:42:29 +00001575 // Now process the switch body. The code after the switch is the implicit
1576 // successor.
1577 Succ = SwitchSuccessor;
1578 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001579
1580 // When visiting the body, the case statements should automatically get linked
1581 // up to the switch. We also don't keep a pointer to the body, since all
1582 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001583 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001584 Block = NULL;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001585 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001586 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001587 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001588 return 0;
1589 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001590
Mike Stump31feda52009-07-17 01:31:16 +00001591 // If we have no "default:" case, the default transition is to the code
1592 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001593 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001594
Ted Kremenek81e14852007-08-27 19:46:09 +00001595 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001596 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001597 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001598 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001599 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001600
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001601 // Finally, if the SwitchStmt contains a condition variable, add both the
1602 // SwitchStmt and the condition variable initialization to the CFG.
1603 if (VarDecl *VD = Terminator->getConditionVariable()) {
1604 if (Expr *Init = VD->getInit()) {
1605 autoCreateBlock();
1606 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1607 addStmt(Init);
1608 }
1609 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001610
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001611 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001612}
1613
Ted Kremenek93668002009-07-17 22:18:43 +00001614CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001615 // CaseStmts are essentially labels, so they are the first statement in a
1616 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00001617 CFGBlock *TopBlock = 0, *LastBlock = 0;
1618
1619 if (Stmt *Sub = CS->getSubStmt()) {
1620 // For deeply nested chains of CaseStmts, instead of doing a recursion
1621 // (which can blow out the stack), manually unroll and create blocks
1622 // along the way.
1623 while (isa<CaseStmt>(Sub)) {
1624 CFGBlock *CurrentBlock = createBlock(false);
1625 CurrentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00001626
Ted Kremenek60fa6572010-08-04 23:54:30 +00001627 if (TopBlock)
1628 AddSuccessor(LastBlock, CurrentBlock);
1629 else
1630 TopBlock = CurrentBlock;
1631
1632 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
1633 LastBlock = CurrentBlock;
1634
1635 CS = cast<CaseStmt>(Sub);
1636 Sub = CS->getSubStmt();
1637 }
1638
1639 addStmt(Sub);
1640 }
Mike Stump11289f42009-09-09 15:08:12 +00001641
Ted Kremenek55e91e82007-08-30 18:48:11 +00001642 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001643 if (!CaseBlock)
1644 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001645
1646 // Cases statements partition blocks, so this is the top of the basic block we
1647 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001648 CaseBlock->setLabel(CS);
1649
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001650 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001651 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001652
1653 // Add this block to the list of successors for the block with the switch
1654 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001655 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001656 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001657
Ted Kremenek9aae5132007-08-23 21:42:29 +00001658 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1659 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001660
Ted Kremenek60fa6572010-08-04 23:54:30 +00001661 if (TopBlock) {
1662 AddSuccessor(LastBlock, CaseBlock);
1663 Succ = TopBlock;
1664 }
1665 else {
1666 // This block is now the implicit successor of other blocks.
1667 Succ = CaseBlock;
1668 }
Mike Stump31feda52009-07-17 01:31:16 +00001669
Ted Kremenek60fa6572010-08-04 23:54:30 +00001670 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001671}
Mike Stump31feda52009-07-17 01:31:16 +00001672
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001673CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001674 if (Terminator->getSubStmt())
1675 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001676
Ted Kremenek654c78f2008-02-13 22:05:39 +00001677 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001678
1679 if (!DefaultCaseBlock)
1680 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001681
1682 // Default statements partition blocks, so this is the top of the basic block
1683 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001684 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001685
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001686 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001687 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001688
Mike Stump31feda52009-07-17 01:31:16 +00001689 // Unlike case statements, we don't add the default block to the successors
1690 // for the switch statement immediately. This is done when we finish
1691 // processing the switch statement. This allows for the default case
1692 // (including a fall-through to the code after the switch statement) to always
1693 // be the last successor of a switch-terminated block.
1694
Ted Kremenek654c78f2008-02-13 22:05:39 +00001695 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1696 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001697
Ted Kremenek654c78f2008-02-13 22:05:39 +00001698 // This block is now the implicit successor of other blocks.
1699 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001700
1701 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001702}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001703
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001704CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1705 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1706 // current block.
1707 CFGBlock* TrySuccessor = NULL;
1708
1709 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001710 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001711 return 0;
1712 TrySuccessor = Block;
1713 } else TrySuccessor = Succ;
1714
Mike Stump0bdba6c2010-01-20 01:15:34 +00001715 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001716
1717 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00001718 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001719 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00001720 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001721
Mike Stump0bdba6c2010-01-20 01:15:34 +00001722 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001723 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1724 // The code after the try is the implicit successor.
1725 Succ = TrySuccessor;
1726 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001727 if (CS->getExceptionDecl() == 0) {
1728 HasCatchAll = true;
1729 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001730 Block = NULL;
1731 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1732 if (CatchBlock == 0)
1733 return 0;
1734 // Add this block to the list of successors for the block with the try
1735 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00001736 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001737 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00001738 if (!HasCatchAll) {
1739 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00001740 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001741 else
Mike Stump845384a2010-01-20 01:30:58 +00001742 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00001743 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001744
1745 // The code after the try is the implicit successor.
1746 Succ = TrySuccessor;
1747
Mike Stump845384a2010-01-20 01:30:58 +00001748 // Save the current "try" context.
1749 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1750 TryTerminatedBlock = NewTryTerminatedBlock;
1751
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001752 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001753 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001754 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001755 return Block;
1756}
1757
1758CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1759 // CXXCatchStmt are treated like labels, so they are the first statement in a
1760 // block.
1761
1762 if (CS->getHandlerBlock())
1763 addStmt(CS->getHandlerBlock());
1764
1765 CFGBlock* CatchBlock = Block;
1766 if (!CatchBlock)
1767 CatchBlock = createBlock();
1768
1769 CatchBlock->setLabel(CS);
1770
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001771 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001772 return 0;
1773
1774 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1775 Block = NULL;
1776
1777 return CatchBlock;
1778}
1779
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001780CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00001781 AddStmtChoice asc) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001782 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001783 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00001784 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001785 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00001786 return VisitChildren(C);
1787}
1788
Ted Kremenekeda180e22007-08-28 19:26:49 +00001789CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001790 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001791 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001792
Ted Kremenekeda180e22007-08-28 19:26:49 +00001793 if (!IBlock) {
1794 IBlock = createBlock(false);
1795 cfg->setIndirectGotoBlock(IBlock);
1796 }
Mike Stump31feda52009-07-17 01:31:16 +00001797
Ted Kremenekeda180e22007-08-28 19:26:49 +00001798 // IndirectGoto is a control-flow statement. Thus we stop processing the
1799 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001800 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001801 return 0;
1802
Ted Kremenekeda180e22007-08-28 19:26:49 +00001803 Block = createBlock(false);
1804 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001805 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001806 return addStmt(I->getTarget());
1807}
1808
Ted Kremenek04cca642007-08-23 21:26:19 +00001809} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001810
Mike Stump31feda52009-07-17 01:31:16 +00001811/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1812/// no successors or predecessors. If this is the first block created in the
1813/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001814CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001815 bool first_block = begin() == end();
1816
1817 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001818 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1819 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1820 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001821
1822 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001823 if (first_block)
1824 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001825
1826 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001827 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001828}
1829
Ted Kremenek889073f2007-08-23 16:51:22 +00001830/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1831/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00001832CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001833 bool PruneTriviallyFalse,
Mike Stump04c68512010-01-21 15:20:48 +00001834 bool AddEHEdges, bool AddScopes) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001835 CFGBuilder Builder;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001836 return Builder.buildCFG(D, Statement, C, PruneTriviallyFalse,
1837 AddEHEdges, AddScopes);
Ted Kremenek889073f2007-08-23 16:51:22 +00001838}
1839
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001840//===----------------------------------------------------------------------===//
1841// CFG: Queries for BlkExprs.
1842//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001843
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001844namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001845 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001846}
1847
Ted Kremenekbff98442009-12-23 23:37:10 +00001848static void FindSubExprAssignments(Stmt *S,
1849 llvm::SmallPtrSet<Expr*,50>& Set) {
1850 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001851 return;
Mike Stump31feda52009-07-17 01:31:16 +00001852
Ted Kremenekbff98442009-12-23 23:37:10 +00001853 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001854 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00001855 if (!child)
1856 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001857
Ted Kremenekbff98442009-12-23 23:37:10 +00001858 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001859 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001860
Ted Kremenekbff98442009-12-23 23:37:10 +00001861 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00001862 }
1863}
1864
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001865static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1866 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001867
1868 // Look for assignments that are used as subexpressions. These are the only
1869 // assignments that we want to *possibly* register as a block-level
1870 // expression. Basically, if an assignment occurs both in a subexpression and
1871 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001872 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001873
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001874 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001875 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001876 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001877
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001878 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001879
1880 // Iterate over the statements again on identify the Expr* and Stmt* at the
1881 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001882
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001883 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001884 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001885
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001886 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001887 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001888 // expression are really "statements" whose value is never used by
1889 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001890 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001891 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001892 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1893 // Special handling for statement expressions. The last statement in
1894 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001895 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001896 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001897 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001898 (*M)[C->body_back()] = x;
1899 }
1900 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001901
Ted Kremenek95a123c2008-01-26 00:03:27 +00001902 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001903 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001904 }
Mike Stump31feda52009-07-17 01:31:16 +00001905
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001906 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001907
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001908 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001909
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001910 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001911 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001912 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001913 }
1914 }
Mike Stump31feda52009-07-17 01:31:16 +00001915
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001916 return M;
1917}
1918
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001919CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1920 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001921 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001922
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001923 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001924 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00001925 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001926}
1927
1928unsigned CFG::getNumBlkExprs() {
1929 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1930 return M->size();
1931 else {
1932 // We assume callers interested in the number of BlkExprs will want
1933 // the map constructed if it doesn't already exist.
1934 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1935 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1936 }
1937}
1938
Ted Kremenek6065ef62008-04-28 18:00:46 +00001939//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00001940// Filtered walking of the CFG.
1941//===----------------------------------------------------------------------===//
1942
1943bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00001944 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00001945
1946 if (F.IgnoreDefaultsWithCoveredEnums) {
1947 // If the 'To' has no label or is labeled but the label isn't a
1948 // CaseStmt then filter this edge.
1949 if (const SwitchStmt *S =
Ted Kremenekf146cd12010-09-09 02:57:48 +00001950 dyn_cast_or_null<SwitchStmt>(From->getTerminator())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00001951 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00001952 const Stmt *L = To->getLabel();
1953 if (!L || !isa<CaseStmt>(L))
1954 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00001955 }
1956 }
1957 }
1958
1959 return false;
1960}
1961
1962//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001963// Cleanup: CFG dstor.
1964//===----------------------------------------------------------------------===//
1965
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001966CFG::~CFG() {
1967 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1968}
Mike Stump31feda52009-07-17 01:31:16 +00001969
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001970//===----------------------------------------------------------------------===//
1971// CFG pretty printing
1972//===----------------------------------------------------------------------===//
1973
Ted Kremenek7e776b12007-08-22 18:22:34 +00001974namespace {
1975
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001976class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001977 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1978 StmtMapTy StmtMap;
1979 signed CurrentBlock;
1980 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001981 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001982public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001983
Chris Lattnerc61089a2009-06-30 01:26:17 +00001984 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1985 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001986 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1987 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001988 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001989 BI != BEnd; ++BI, ++j )
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001990 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001991 }
1992 }
Mike Stump31feda52009-07-17 01:31:16 +00001993
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001994 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001995
Chris Lattnerc61089a2009-06-30 01:26:17 +00001996 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001997 void setBlockID(signed i) { CurrentBlock = i; }
1998 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001999
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002000 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00002001
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002002 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002003
2004 if (I == StmtMap.end())
2005 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002006
2007 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00002008 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002009 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002010 }
Mike Stump31feda52009-07-17 01:31:16 +00002011
Ted Kremenek60983dc2010-01-19 20:52:05 +00002012 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002013 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002014 }
2015};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002016} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002017
Chris Lattnerc61089a2009-06-30 01:26:17 +00002018
2019namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002020class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002021 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002022
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002023 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002024 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002025 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002026public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002027 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002028 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002029 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002030
Ted Kremenek9aae5132007-08-23 21:42:29 +00002031 void VisitIfStmt(IfStmt* I) {
2032 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002033 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002034 }
Mike Stump31feda52009-07-17 01:31:16 +00002035
Ted Kremenek9aae5132007-08-23 21:42:29 +00002036 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002037 void VisitStmt(Stmt* Terminator) {
2038 Terminator->printPretty(OS, Helper, Policy);
2039 }
2040
Ted Kremenek9aae5132007-08-23 21:42:29 +00002041 void VisitForStmt(ForStmt* F) {
2042 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002043 if (F->getInit())
2044 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002045 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002046 if (Stmt* C = F->getCond())
2047 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002048 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002049 if (F->getInc())
2050 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002051 OS << ")";
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 VisitWhileStmt(WhileStmt* W) {
2055 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002056 if (Stmt* C = W->getCond())
2057 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002058 }
Mike Stump31feda52009-07-17 01:31:16 +00002059
Ted Kremenek9aae5132007-08-23 21:42:29 +00002060 void VisitDoStmt(DoStmt* D) {
2061 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002062 if (Stmt* C = D->getCond())
2063 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002064 }
Mike Stump31feda52009-07-17 01:31:16 +00002065
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002066 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002067 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002068 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002069 }
Mike Stump31feda52009-07-17 01:31:16 +00002070
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002071 void VisitCXXTryStmt(CXXTryStmt* CS) {
2072 OS << "try ...";
2073 }
2074
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002075 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002076 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002077 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002078 }
Mike Stump31feda52009-07-17 01:31:16 +00002079
Ted Kremenek391f94a2007-08-31 22:29:13 +00002080 void VisitChooseExpr(ChooseExpr* C) {
2081 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002082 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002083 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002084 }
Mike Stump31feda52009-07-17 01:31:16 +00002085
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002086 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2087 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002088 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002089 }
Mike Stump31feda52009-07-17 01:31:16 +00002090
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002091 void VisitBinaryOperator(BinaryOperator* B) {
2092 if (!B->isLogicalOp()) {
2093 VisitExpr(B);
2094 return;
2095 }
Mike Stump31feda52009-07-17 01:31:16 +00002096
Douglas Gregor7de59662009-05-29 20:38:28 +00002097 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002098
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002099 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002100 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002101 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002102 return;
John McCalle3027922010-08-25 11:45:40 +00002103 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002104 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002105 return;
2106 default:
2107 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00002108 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002109 }
Mike Stump31feda52009-07-17 01:31:16 +00002110
Ted Kremenek12687ff2007-08-27 21:54:41 +00002111 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002112 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002113 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00002114};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002115} // end anonymous namespace
2116
Mike Stump31feda52009-07-17 01:31:16 +00002117
Chris Lattnerc61089a2009-06-30 01:26:17 +00002118static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00002119 const CFGElement &E) {
Mike Stump92244b02010-01-19 22:00:14 +00002120
2121 if (E.asStartScope()) {
2122 OS << "start scope\n";
2123 return;
2124 }
2125 if (E.asEndScope()) {
2126 OS << "end scope\n";
2127 return;
2128 }
2129
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002130 Stmt *S = E;
2131
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002132 if (Helper) {
2133 // special printing for statement-expressions.
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002134 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002135 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00002136
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002137 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00002138 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00002139 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00002140 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002141 return;
2142 }
2143 }
Mike Stump31feda52009-07-17 01:31:16 +00002144
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002145 // special printing for comma expressions.
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002146 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
John McCalle3027922010-08-25 11:45:40 +00002147 if (B->getOpcode() == BO_Comma) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002148 OS << "... , ";
2149 Helper->handledStmt(B->getRHS(),OS);
2150 OS << '\n';
2151 return;
Mike Stump31feda52009-07-17 01:31:16 +00002152 }
2153 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002154 }
Mike Stump31feda52009-07-17 01:31:16 +00002155
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002156 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2157
2158 if (isa<CXXOperatorCallExpr>(S)) {
2159 OS << " (OperatorCall)";
2160 }
2161 else if (isa<CXXBindTemporaryExpr>(S)) {
2162 OS << " (BindTemporary)";
2163 }
2164
Mike Stump31feda52009-07-17 01:31:16 +00002165
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002166 // Expressions need a newline.
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002167 if (isa<Expr>(S))
2168 OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002169}
Mike Stump31feda52009-07-17 01:31:16 +00002170
Chris Lattnerc61089a2009-06-30 01:26:17 +00002171static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2172 const CFGBlock& B,
2173 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00002174
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002175 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00002176
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002177 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00002178 OS << "\n [ B" << B.getBlockID();
2179
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002180 if (&B == &cfg->getEntry())
2181 OS << " (ENTRY) ]\n";
2182 else if (&B == &cfg->getExit())
2183 OS << " (EXIT) ]\n";
2184 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002185 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002186 else
2187 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00002188
Ted Kremenek71eca012007-08-29 23:20:49 +00002189 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00002190 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002191
2192 if (print_edges)
2193 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002194
Mike Stump92244b02010-01-19 22:00:14 +00002195 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002196 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00002197 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00002198 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002199 C->getLHS()->printPretty(OS, Helper,
2200 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002201 if (C->getRHS()) {
2202 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002203 C->getRHS()->printPretty(OS, Helper,
2204 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002205 }
Mike Stump92244b02010-01-19 22:00:14 +00002206 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002207 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00002208 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002209 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00002210 if (CS->getExceptionDecl())
2211 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2212 0);
2213 else
2214 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002215 OS << ")";
2216
2217 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002218 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00002219
Ted Kremenek71eca012007-08-29 23:20:49 +00002220 OS << ":\n";
2221 }
Mike Stump31feda52009-07-17 01:31:16 +00002222
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002223 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002224 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00002225
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002226 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2227 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00002228
Ted Kremenek71eca012007-08-29 23:20:49 +00002229 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002230 if (print_edges)
2231 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002232
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002233 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002234
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002235 if (Helper)
2236 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002237
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002238 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002239 }
Mike Stump31feda52009-07-17 01:31:16 +00002240
Ted Kremenek71eca012007-08-29 23:20:49 +00002241 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002242 if (B.getTerminator()) {
2243 if (print_edges)
2244 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002245
Ted Kremenek71eca012007-08-29 23:20:49 +00002246 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002247
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002248 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002249
Chris Lattnerc61089a2009-06-30 01:26:17 +00002250 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2251 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002252 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00002253 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002254 }
Mike Stump31feda52009-07-17 01:31:16 +00002255
Ted Kremenek71eca012007-08-29 23:20:49 +00002256 if (print_edges) {
2257 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002258 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002259 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002260
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002261 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2262 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002263
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002264 if (i == 8 || (i-8) == 0)
2265 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002266
Ted Kremenek71eca012007-08-29 23:20:49 +00002267 OS << " B" << (*I)->getBlockID();
2268 }
Mike Stump31feda52009-07-17 01:31:16 +00002269
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002270 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002271
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002272 // Print the successors of this block.
2273 OS << " Successors (" << B.succ_size() << "):";
2274 i = 0;
2275
2276 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2277 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002278
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002279 if (i == 8 || (i-8) % 10 == 0)
2280 OS << "\n ";
2281
Mike Stump0d76d072009-07-20 23:24:15 +00002282 if (*I)
2283 OS << " B" << (*I)->getBlockID();
2284 else
2285 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002286 }
Mike Stump31feda52009-07-17 01:31:16 +00002287
Ted Kremenek71eca012007-08-29 23:20:49 +00002288 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002289 }
Mike Stump31feda52009-07-17 01:31:16 +00002290}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002291
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002292
2293/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002294void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002295
2296/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002297void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2298 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002299
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002300 // Print the entry block.
2301 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002302
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002303 // Iterate through the CFGBlocks and print them one by one.
2304 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2305 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002306 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002307 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002308
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002309 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002310 }
Mike Stump31feda52009-07-17 01:31:16 +00002311
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002312 // Print the exit block.
2313 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002314 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002315}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002316
2317/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002318void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2319 print(llvm::errs(), cfg, LO);
2320}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002321
2322/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2323/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002324void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2325 const LangOptions &LO) const {
2326 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002327 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002328}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002329
Ted Kremenek15647632008-01-30 23:02:42 +00002330/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002331void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002332 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002333 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002334 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2335}
2336
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002337Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002338
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002339 if (!Terminator)
2340 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002341
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002342 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002343
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002344 switch (Terminator->getStmtClass()) {
2345 default:
2346 break;
Mike Stump31feda52009-07-17 01:31:16 +00002347
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002348 case Stmt::ForStmtClass:
2349 E = cast<ForStmt>(Terminator)->getCond();
2350 break;
Mike Stump31feda52009-07-17 01:31:16 +00002351
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002352 case Stmt::WhileStmtClass:
2353 E = cast<WhileStmt>(Terminator)->getCond();
2354 break;
Mike Stump31feda52009-07-17 01:31:16 +00002355
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002356 case Stmt::DoStmtClass:
2357 E = cast<DoStmt>(Terminator)->getCond();
2358 break;
Mike Stump31feda52009-07-17 01:31:16 +00002359
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002360 case Stmt::IfStmtClass:
2361 E = cast<IfStmt>(Terminator)->getCond();
2362 break;
Mike Stump31feda52009-07-17 01:31:16 +00002363
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002364 case Stmt::ChooseExprClass:
2365 E = cast<ChooseExpr>(Terminator)->getCond();
2366 break;
Mike Stump31feda52009-07-17 01:31:16 +00002367
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002368 case Stmt::IndirectGotoStmtClass:
2369 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2370 break;
Mike Stump31feda52009-07-17 01:31:16 +00002371
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002372 case Stmt::SwitchStmtClass:
2373 E = cast<SwitchStmt>(Terminator)->getCond();
2374 break;
Mike Stump31feda52009-07-17 01:31:16 +00002375
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002376 case Stmt::ConditionalOperatorClass:
2377 E = cast<ConditionalOperator>(Terminator)->getCond();
2378 break;
Mike Stump31feda52009-07-17 01:31:16 +00002379
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002380 case Stmt::BinaryOperatorClass: // '&&' and '||'
2381 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002382 break;
Mike Stump31feda52009-07-17 01:31:16 +00002383
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002384 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002385 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002386 }
Mike Stump31feda52009-07-17 01:31:16 +00002387
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002388 return E ? E->IgnoreParens() : NULL;
2389}
2390
Ted Kremenek92137a32008-05-16 16:06:00 +00002391bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002392
Ted Kremenek92137a32008-05-16 16:06:00 +00002393 if (!Terminator)
2394 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002395
Ted Kremenek92137a32008-05-16 16:06:00 +00002396 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002397
Ted Kremenek92137a32008-05-16 16:06:00 +00002398 switch (Terminator->getStmtClass()) {
2399 default:
2400 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002401
2402 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002403 case Stmt::WhileStmtClass:
2404 case Stmt::DoStmtClass:
2405 case Stmt::IfStmtClass:
2406 case Stmt::ChooseExprClass:
2407 case Stmt::ConditionalOperatorClass:
2408 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002409 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002410 }
Mike Stump31feda52009-07-17 01:31:16 +00002411
Ted Kremenek92137a32008-05-16 16:06:00 +00002412 return E ? E->IgnoreParens() : NULL;
2413}
2414
Ted Kremenek15647632008-01-30 23:02:42 +00002415
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002416//===----------------------------------------------------------------------===//
2417// CFG Graphviz Visualization
2418//===----------------------------------------------------------------------===//
2419
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002420
2421#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002422static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002423#endif
2424
Chris Lattnerc61089a2009-06-30 01:26:17 +00002425void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002426#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002427 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002428 GraphHelper = &H;
2429 llvm::ViewGraph(this,"CFG");
2430 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002431#endif
2432}
2433
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002434namespace llvm {
2435template<>
2436struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002437
2438 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2439
2440 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002441
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002442#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002443 std::string OutSStr;
2444 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002445 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002446 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002447
2448 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2449
2450 // Process string output to make it nicer...
2451 for (unsigned i = 0; i != OutStr.length(); ++i)
2452 if (OutStr[i] == '\n') { // Left justify
2453 OutStr[i] = '\\';
2454 OutStr.insert(OutStr.begin()+i+1, 'l');
2455 }
Mike Stump31feda52009-07-17 01:31:16 +00002456
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002457 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002458#else
2459 return "";
2460#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002461 }
2462};
2463} // end namespace llvm