blob: 6f2cb41a6e40c846b813547bf29359b5cdc6e8dd [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 Kremenek4cad5fc2009-12-16 03:18:58 +000038
39class 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.
Mike Stump04c68512010-01-21 15:20:48 +0000102 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C, bool AddEHEdges,
103 bool AddScopes);
Mike Stump31feda52009-07-17 01:31:16 +0000104
Ted Kremenek93668002009-07-17 22:18:43 +0000105private:
106 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000107 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
108 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
109 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000110 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000111 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
112 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
113 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000114 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000115 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000116 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000117 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000118 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000119 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000120 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000121 CFGBlock *VisitDeclStmt(DeclStmt *DS);
122 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000123 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
124 CFGBlock *VisitDoStmt(DoStmt *D);
125 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000126 CFGBlock *VisitGotoStmt(GotoStmt* G);
127 CFGBlock *VisitIfStmt(IfStmt *I);
128 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
129 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000130 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000131 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
132 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
133 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
134 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
135 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
136 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000137 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
138 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000139 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
140 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000141
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000142 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
143 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000144 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000145
Ted Kremenek6065ef62008-04-28 18:00:46 +0000146 // NYS == Not Yet Supported
147 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000148 badCFG = true;
149 return Block;
150 }
Mike Stump31feda52009-07-17 01:31:16 +0000151
Mike Stump92244b02010-01-19 22:00:14 +0000152 CFGBlock *StartScope(Stmt *S, CFGBlock *B) {
153 if (!AddScopes)
154 return B;
155
156 if (B == 0)
157 B = createBlock();
158 B->StartScope(S, cfg->getBumpVectorContext());
159 return B;
160 }
161
162 void EndScope(Stmt *S) {
163 if (!AddScopes)
164 return;
165
166 if (Block == 0)
167 Block = createBlock();
168 Block->EndScope(S, cfg->getBumpVectorContext());
169 }
170
Ted Kremenek93668002009-07-17 22:18:43 +0000171 void autoCreateBlock() { if (!Block) Block = createBlock(); }
172 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000173 bool FinishBlock(CFGBlock* B);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000174 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
175 return Visit(S, asc);
176 }
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000177
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000178 void AppendStmt(CFGBlock *B, Stmt *S,
179 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
180 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000181 }
182
183 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
184 B->addSuccessor(S, cfg->getBumpVectorContext());
185 }
Mike Stump11289f42009-09-09 15:08:12 +0000186
Ted Kremenek963cc312009-07-24 06:55:42 +0000187 /// TryResult - a class representing a variant over the values
188 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
189 /// and is used by the CFGBuilder to decide if a branch condition
190 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000191 class TryResult {
192 int X;
193 public:
194 TryResult(bool b) : X(b ? 1 : 0) {}
195 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000196
Ted Kremenek30754282009-07-24 04:47:11 +0000197 bool isTrue() const { return X == 1; }
198 bool isFalse() const { return X == 0; }
199 bool isKnown() const { return X >= 0; }
200 void negate() {
201 assert(isKnown());
202 X ^= 0x1;
203 }
204 };
Mike Stump11289f42009-09-09 15:08:12 +0000205
Mike Stump773582d2009-07-23 23:25:26 +0000206 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
207 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000208 TryResult TryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000209 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000210 if (!S->isTypeDependent() && !S->isValueDependent() &&
211 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000212 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000213
214 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000215 }
216
Ted Kremenekb64d1832008-03-13 03:04:22 +0000217 bool badCFG;
Mike Stump04c68512010-01-21 15:20:48 +0000218
219 // True iff EH edges on CallExprs should be added to the CFG.
220 bool AddEHEdges;
221
222 // True iff scope start and scope end notes should be added to the CFG.
Mike Stump92244b02010-01-19 22:00:14 +0000223 bool AddScopes;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000224};
Mike Stump31feda52009-07-17 01:31:16 +0000225
Douglas Gregor4619e432008-12-05 23:32:09 +0000226// FIXME: Add support for dependent-sized array types in C++?
227// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000228static VariableArrayType* FindVA(Type* t) {
229 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
230 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
231 if (vat->getSizeExpr())
232 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000233
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000234 t = vt->getElementType().getTypePtr();
235 }
Mike Stump31feda52009-07-17 01:31:16 +0000236
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000237 return 0;
238}
Mike Stump31feda52009-07-17 01:31:16 +0000239
240/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
241/// arbitrary statement. Examples include a single expression or a function
242/// body (compound statement). The ownership of the returned CFG is
243/// transferred to the caller. If CFG construction fails, this method returns
244/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000245CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Mike Stumpcc3a8532010-01-21 17:21:23 +0000246 bool addehedges, bool AddScopes) {
247 AddEHEdges = addehedges;
Mike Stump0d76d072009-07-20 23:24:15 +0000248 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000249 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000250 if (!Statement)
251 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000252
Mike Stump92244b02010-01-19 22:00:14 +0000253 this->AddScopes = AddScopes;
Ted Kremenekb64d1832008-03-13 03:04:22 +0000254 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000255
256 // Create an empty block that will serve as the exit block for the CFG. Since
257 // this is the first block added to the CFG, it will be implicitly registered
258 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000259 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000260 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000261 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000262
Ted Kremenek9aae5132007-08-23 21:42:29 +0000263 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000264 CFGBlock* B = addStmt(Statement);
Mike Stump6bf1c082010-01-21 02:21:40 +0000265
266 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
267 // FIXME: Add code for base initializers and member initializers.
268 (void)CD;
269 }
Ted Kremenek8aed4902009-10-20 23:46:25 +0000270 if (!B)
271 B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000272
Daniel Dunbar260918c2010-02-22 05:58:59 +0000273 if (B) {
274 // Finalize the last constructed block. This usually involves reversing the
275 // order of the statements in the block.
276 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000277
Daniel Dunbar260918c2010-02-22 05:58:59 +0000278 // Backpatch the gotos whose label -> block mappings we didn't know when we
279 // encountered them.
280 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000281 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000282
Daniel Dunbar260918c2010-02-22 05:58:59 +0000283 CFGBlock* B = *I;
284 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
285 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000286
Daniel Dunbar260918c2010-02-22 05:58:59 +0000287 // If there is no target for the goto, then we are looking at an
288 // incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000289 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000290
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000291 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000292 }
Mike Stump31feda52009-07-17 01:31:16 +0000293
Daniel Dunbar260918c2010-02-22 05:58:59 +0000294 // Add successors to the Indirect Goto Dispatch block (if we have one).
295 if (CFGBlock* B = cfg->getIndirectGotoBlock())
296 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
297 E = AddressTakenLabels.end(); I != E; ++I ) {
298
299 // Lookup the target block.
300 LabelMapTy::iterator LI = LabelMap.find(*I);
301
302 // If there is no target block that contains label, then we are looking
303 // at an incomplete AST. Handle this by not registering a successor.
304 if (LI == LabelMap.end()) continue;
305
306 AddSuccessor(B, LI->second);
307 }
308
309 Succ = B;
310 }
Mike Stump31feda52009-07-17 01:31:16 +0000311
312 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000313 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000314
Ted Kremenekab929bb2009-10-20 23:59:28 +0000315 return badCFG ? NULL : cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000316}
Mike Stump31feda52009-07-17 01:31:16 +0000317
Ted Kremenek9aae5132007-08-23 21:42:29 +0000318/// createBlock - Used to lazily create blocks that are connected
319/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000320CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000321 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000322 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000323 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000324 return B;
325}
Mike Stump31feda52009-07-17 01:31:16 +0000326
Ted Kremenek0868eea2009-09-24 18:45:41 +0000327/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek55957a82009-05-02 00:13:27 +0000328bool CFGBuilder::FinishBlock(CFGBlock* B) {
329 if (badCFG)
330 return false;
331
Ted Kremenek93668002009-07-17 22:18:43 +0000332 assert(B);
Ted Kremenek55957a82009-05-02 00:13:27 +0000333 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000334}
335
Ted Kremenek93668002009-07-17 22:18:43 +0000336/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000337/// blocks for ternary operators, &&, and ||. We also process "," and
338/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000339CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000340tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000341 if (!S) {
342 badCFG = true;
343 return 0;
344 }
Ted Kremenek93668002009-07-17 22:18:43 +0000345 switch (S->getStmtClass()) {
346 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000347 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000348
349 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000350 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000351
Ted Kremenek93668002009-07-17 22:18:43 +0000352 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000353 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000354
Ted Kremenek93668002009-07-17 22:18:43 +0000355 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000356 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000357
Ted Kremenek93668002009-07-17 22:18:43 +0000358 case Stmt::BreakStmtClass:
359 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000360
Ted Kremenek93668002009-07-17 22:18:43 +0000361 case Stmt::CallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000362 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000363
Ted Kremenek93668002009-07-17 22:18:43 +0000364 case Stmt::CaseStmtClass:
365 return VisitCaseStmt(cast<CaseStmt>(S));
366
367 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000368 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000369
Ted Kremenek93668002009-07-17 22:18:43 +0000370 case Stmt::CompoundStmtClass:
371 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000372
Ted Kremenek93668002009-07-17 22:18:43 +0000373 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000374 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000375
Ted Kremenek93668002009-07-17 22:18:43 +0000376 case Stmt::ContinueStmtClass:
377 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000378
Ted Kremenekb27378c2010-01-19 20:40:33 +0000379 case Stmt::CXXCatchStmtClass:
380 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
381
Zhongxing Xu7e612172010-04-13 09:38:01 +0000382 case Stmt::CXXMemberCallExprClass:
383 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
384
Ted Kremenekb27378c2010-01-19 20:40:33 +0000385 case Stmt::CXXThrowExprClass:
386 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
387
388 case Stmt::CXXTryStmtClass:
389 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
390
Ted Kremenek93668002009-07-17 22:18:43 +0000391 case Stmt::DeclStmtClass:
392 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000393
Ted Kremenek93668002009-07-17 22:18:43 +0000394 case Stmt::DefaultStmtClass:
395 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000396
Ted Kremenek93668002009-07-17 22:18:43 +0000397 case Stmt::DoStmtClass:
398 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000399
Ted Kremenek93668002009-07-17 22:18:43 +0000400 case Stmt::ForStmtClass:
401 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000402
Ted Kremenek93668002009-07-17 22:18:43 +0000403 case Stmt::GotoStmtClass:
404 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000405
Ted Kremenek93668002009-07-17 22:18:43 +0000406 case Stmt::IfStmtClass:
407 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000408
Ted Kremenek93668002009-07-17 22:18:43 +0000409 case Stmt::IndirectGotoStmtClass:
410 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000411
Ted Kremenek93668002009-07-17 22:18:43 +0000412 case Stmt::LabelStmtClass:
413 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000414
Ted Kremenek5868ec62010-04-11 17:02:10 +0000415 case Stmt::MemberExprClass:
416 return VisitMemberExpr(cast<MemberExpr>(S), asc);
417
Ted Kremenek93668002009-07-17 22:18:43 +0000418 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000419 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
420
Ted Kremenek93668002009-07-17 22:18:43 +0000421 case Stmt::ObjCAtSynchronizedStmtClass:
422 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000423
Ted Kremenek93668002009-07-17 22:18:43 +0000424 case Stmt::ObjCAtThrowStmtClass:
425 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000426
Ted Kremenek93668002009-07-17 22:18:43 +0000427 case Stmt::ObjCAtTryStmtClass:
428 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000429
Ted Kremenek93668002009-07-17 22:18:43 +0000430 case Stmt::ObjCForCollectionStmtClass:
431 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000432
Ted Kremenek93668002009-07-17 22:18:43 +0000433 case Stmt::ParenExprClass:
434 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000435 goto tryAgain;
436
Ted Kremenek93668002009-07-17 22:18:43 +0000437 case Stmt::NullStmtClass:
438 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000439
Ted Kremenek93668002009-07-17 22:18:43 +0000440 case Stmt::ReturnStmtClass:
441 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000442
Ted Kremenek93668002009-07-17 22:18:43 +0000443 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000444 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000445
Ted Kremenek93668002009-07-17 22:18:43 +0000446 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000447 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000448
Ted Kremenek93668002009-07-17 22:18:43 +0000449 case Stmt::SwitchStmtClass:
450 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000451
Ted Kremenek93668002009-07-17 22:18:43 +0000452 case Stmt::WhileStmtClass:
453 return VisitWhileStmt(cast<WhileStmt>(S));
454 }
455}
Mike Stump11289f42009-09-09 15:08:12 +0000456
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000457CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
458 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000459 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000460 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000461 }
Mike Stump11289f42009-09-09 15:08:12 +0000462
Ted Kremenek93668002009-07-17 22:18:43 +0000463 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000464}
Mike Stump31feda52009-07-17 01:31:16 +0000465
Ted Kremenek93668002009-07-17 22:18:43 +0000466/// VisitChildren - Visit the children of a Stmt.
467CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
468 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000469 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000470 E = Terminator->child_end(); I != E; ++I) {
471 if (*I) B = Visit(*I);
472 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000473 return B;
474}
Mike Stump11289f42009-09-09 15:08:12 +0000475
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000476CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
477 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000478 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000479
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000480 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000481 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000482 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000483 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000484
Ted Kremenek9aae5132007-08-23 21:42:29 +0000485 return Block;
486}
Mike Stump11289f42009-09-09 15:08:12 +0000487
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000488CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
489 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000490 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000491 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000492 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000493
Ted Kremenek93668002009-07-17 22:18:43 +0000494 if (!FinishBlock(ConfluenceBlock))
495 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000496
Ted Kremenek93668002009-07-17 22:18:43 +0000497 // create the block evaluating the LHS
498 CFGBlock* LHSBlock = createBlock(false);
499 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000500
Ted Kremenek93668002009-07-17 22:18:43 +0000501 // create the block evaluating the RHS
502 Succ = ConfluenceBlock;
503 Block = NULL;
504 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000505
506 if (RHSBlock) {
507 if (!FinishBlock(RHSBlock))
508 return 0;
509 }
510 else {
511 // Create an empty block for cases where the RHS doesn't require
512 // any explicit statements in the CFG.
513 RHSBlock = createBlock();
514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Mike Stump773582d2009-07-23 23:25:26 +0000516 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000517 TryResult KnownVal = TryEvaluateBool(B->getLHS());
518 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
519 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000520
Ted Kremenek93668002009-07-17 22:18:43 +0000521 // Now link the LHSBlock with RHSBlock.
522 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000523 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
524 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000525 } else {
Ted Kremenek1362b8b2010-01-19 20:46:35 +0000526 assert(B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000527 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
528 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000529 }
Mike Stump11289f42009-09-09 15:08:12 +0000530
Ted Kremenek93668002009-07-17 22:18:43 +0000531 // Generate the blocks for evaluating the LHS.
532 Block = LHSBlock;
533 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000534 }
Ted Kremenek93668002009-07-17 22:18:43 +0000535 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000536 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000537 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000538 addStmt(B->getRHS());
539 return addStmt(B->getLHS());
540 }
Mike Stump11289f42009-09-09 15:08:12 +0000541
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000542 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000543}
544
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000545CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
546 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000547 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000548 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000549 }
550 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000551}
552
Ted Kremenek93668002009-07-17 22:18:43 +0000553CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
554 // "break" is a control-flow statement. Thus we stop processing the current
555 // block.
556 if (Block && !FinishBlock(Block))
557 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000558
Ted Kremenek93668002009-07-17 22:18:43 +0000559 // Now create a new block that ends with the break statement.
560 Block = createBlock(false);
561 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000562
Ted Kremenek93668002009-07-17 22:18:43 +0000563 // If there is no target for the break, then we are looking at an incomplete
564 // AST. This means that the CFG cannot be constructed.
565 if (BreakTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000566 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000567 else
568 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000569
570
Ted Kremenek9aae5132007-08-23 21:42:29 +0000571 return Block;
572}
Mike Stump11289f42009-09-09 15:08:12 +0000573
Mike Stump04c68512010-01-21 15:20:48 +0000574static bool CanThrow(Expr *E) {
575 QualType Ty = E->getType();
576 if (Ty->isFunctionPointerType())
577 Ty = Ty->getAs<PointerType>()->getPointeeType();
578 else if (Ty->isBlockPointerType())
579 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
580
581 const FunctionType *FT = Ty->getAs<FunctionType>();
582 if (FT) {
583 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
584 if (Proto->hasEmptyExceptionSpec())
585 return false;
586 }
587 return true;
588}
589
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000590CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000591 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000592 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000593 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000594 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000595 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000596
Mike Stump04c68512010-01-21 15:20:48 +0000597 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000598
599 // Languages without exceptions are assumed to not throw.
600 if (Context->getLangOptions().Exceptions) {
Mike Stump04c68512010-01-21 15:20:48 +0000601 if (AddEHEdges)
602 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +0000603 }
604
605 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000606 if (FD->hasAttr<NoReturnAttr>())
607 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +0000608 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +0000609 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000610 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000611
Mike Stump04c68512010-01-21 15:20:48 +0000612 if (!CanThrow(C->getCallee()))
613 AddEHEdge = false;
614
615 if (!NoReturn && !AddEHEdge)
Zhongxing Xua396e612010-02-24 02:19:28 +0000616 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000617
Mike Stump92244b02010-01-19 22:00:14 +0000618 if (Block) {
619 Succ = Block;
620 if (!FinishBlock(Block))
621 return 0;
622 }
Mike Stump11289f42009-09-09 15:08:12 +0000623
Mike Stump92244b02010-01-19 22:00:14 +0000624 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000625 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000626
Mike Stump92244b02010-01-19 22:00:14 +0000627 if (NoReturn) {
628 // Wire this to the exit block directly.
629 AddSuccessor(Block, &cfg->getExit());
630 }
Mike Stump04c68512010-01-21 15:20:48 +0000631 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +0000632 // Add exceptional edges.
633 if (TryTerminatedBlock)
634 AddSuccessor(Block, TryTerminatedBlock);
635 else
636 AddSuccessor(Block, &cfg->getExit());
637 }
Mike Stump11289f42009-09-09 15:08:12 +0000638
Mike Stump8c5d7992009-07-25 21:26:53 +0000639 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000640}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000641
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000642CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
643 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000644 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000645 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000646 if (!FinishBlock(ConfluenceBlock))
647 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000648
Ted Kremenek5868ec62010-04-11 17:02:10 +0000649 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
650 : AddStmtChoice::AlwaysAdd;
651
Ted Kremenek21822592009-07-17 18:20:32 +0000652 Succ = ConfluenceBlock;
653 Block = NULL;
Ted Kremenek5868ec62010-04-11 17:02:10 +0000654 CFGBlock* LHSBlock = addStmt(C->getLHS(), asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000655 if (!FinishBlock(LHSBlock))
656 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000657
Ted Kremenek21822592009-07-17 18:20:32 +0000658 Succ = ConfluenceBlock;
659 Block = NULL;
Ted Kremenek5868ec62010-04-11 17:02:10 +0000660 CFGBlock* RHSBlock = addStmt(C->getRHS(), asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000661 if (!FinishBlock(RHSBlock))
662 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000663
Ted Kremenek21822592009-07-17 18:20:32 +0000664 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000665 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000666 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000667 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
668 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000669 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000670 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000671}
Mike Stump11289f42009-09-09 15:08:12 +0000672
673
674CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump92244b02010-01-19 22:00:14 +0000675 EndScope(C);
676
Mike Stump11289f42009-09-09 15:08:12 +0000677 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000678
679 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
680 I != E; ++I ) {
681 LastBlock = addStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000682
Ted Kremenekce499c22009-08-27 23:16:26 +0000683 if (badCFG)
684 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000685 }
Mike Stump92244b02010-01-19 22:00:14 +0000686
687 LastBlock = StartScope(C, LastBlock);
688
Ted Kremenek93668002009-07-17 22:18:43 +0000689 return LastBlock;
690}
Mike Stump11289f42009-09-09 15:08:12 +0000691
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000692CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
693 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000694 // Create the confluence block that will "merge" the results of the ternary
695 // expression.
696 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000697 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000698 if (!FinishBlock(ConfluenceBlock))
699 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000700
Ted Kremenek5868ec62010-04-11 17:02:10 +0000701 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
702 : AddStmtChoice::AlwaysAdd;
703
Ted Kremenek51d40b02009-07-17 18:15:54 +0000704 // Create a block for the LHS expression if there is an LHS expression. A
705 // GCC extension allows LHS to be NULL, causing the condition to be the
706 // value that is returned instead.
707 // e.g: x ?: y is shorthand for: x ? x : y;
708 Succ = ConfluenceBlock;
709 Block = NULL;
710 CFGBlock* LHSBlock = NULL;
711 if (C->getLHS()) {
Ted Kremenek5868ec62010-04-11 17:02:10 +0000712 LHSBlock = addStmt(C->getLHS(), asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000713 if (!FinishBlock(LHSBlock))
714 return 0;
715 Block = NULL;
716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Ted Kremenek51d40b02009-07-17 18:15:54 +0000718 // Create the block for the RHS expression.
719 Succ = ConfluenceBlock;
Ted Kremenek5868ec62010-04-11 17:02:10 +0000720 CFGBlock* RHSBlock = addStmt(C->getRHS(), asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000721 if (!FinishBlock(RHSBlock))
722 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000723
Ted Kremenek51d40b02009-07-17 18:15:54 +0000724 // Create the block that will contain the condition.
725 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000726
Mike Stump773582d2009-07-23 23:25:26 +0000727 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000728 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000729 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000730 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000731 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000732 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000733 // If we know the condition is false, add NULL as the successor for
734 // the block containing the condition. In this case, the confluence
735 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000736 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000737 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000738 } else {
739 // If we have no LHS expression, add the ConfluenceBlock as a direct
740 // successor for the block containing the condition. Moreover, we need to
741 // reverse the order of the predecessors in the ConfluenceBlock because
742 // the RHSBlock will have been added to the succcessors already, and we
743 // want the first predecessor to the the block containing the expression
744 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000745 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000746 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000747 std::reverse(ConfluenceBlock->pred_begin(),
748 ConfluenceBlock->pred_end());
749 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000750 }
Mike Stump11289f42009-09-09 15:08:12 +0000751
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000752 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000753 Block->setTerminator(C);
754 return addStmt(C->getCond());
755}
756
Ted Kremenek93668002009-07-17 22:18:43 +0000757CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
758 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000759
Ted Kremenek93668002009-07-17 22:18:43 +0000760 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000761 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000762 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000763 }
Mike Stump11289f42009-09-09 15:08:12 +0000764
Ted Kremenek93668002009-07-17 22:18:43 +0000765 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000766
Ted Kremenek93668002009-07-17 22:18:43 +0000767 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
768 typedef llvm::SmallVector<Decl*,10> BufTy;
769 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000770
Ted Kremenek93668002009-07-17 22:18:43 +0000771 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
772 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
773 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
774 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000775
Ted Kremenek93668002009-07-17 22:18:43 +0000776 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
777 // automatically freed with the CFG.
778 DeclGroupRef DG(*I);
779 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000780 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000781 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000782
Ted Kremenek93668002009-07-17 22:18:43 +0000783 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000784 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000785 B = VisitDeclSubExpr(D);
786 }
Mike Stump11289f42009-09-09 15:08:12 +0000787
788 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000789}
Mike Stump11289f42009-09-09 15:08:12 +0000790
Ted Kremenek93668002009-07-17 22:18:43 +0000791/// VisitDeclSubExpr - Utility method to add block-level expressions for
792/// initializers in Decls.
793CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
794 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000795
Ted Kremenek93668002009-07-17 22:18:43 +0000796 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000797
Ted Kremenek93668002009-07-17 22:18:43 +0000798 if (!VD)
799 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000800
Ted Kremenek93668002009-07-17 22:18:43 +0000801 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000802
Ted Kremenek93668002009-07-17 22:18:43 +0000803 if (Init) {
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000804 AddStmtChoice::Kind k =
805 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
806 : AddStmtChoice::NotAlwaysAdd;
807 Visit(Init, AddStmtChoice(k));
Ted Kremenek93668002009-07-17 22:18:43 +0000808 }
Mike Stump11289f42009-09-09 15:08:12 +0000809
Ted Kremenek93668002009-07-17 22:18:43 +0000810 // If the type of VD is a VLA, then we must process its size expressions.
811 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
812 VA = FindVA(VA->getElementType().getTypePtr()))
813 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000814
Ted Kremenek93668002009-07-17 22:18:43 +0000815 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000816}
817
818CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000819 // We may see an if statement in the middle of a basic block, or it may be the
820 // first statement we are processing. In either case, we create a new basic
821 // block. First, we create the blocks for the then...else statements, and
822 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000823 // middle of a block, we stop processing that block. That block is then the
824 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000825
826 // The block we were proccessing is now finished. Make it the successor
827 // block.
828 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000829 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000830 if (!FinishBlock(Block))
831 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000832 }
Mike Stump31feda52009-07-17 01:31:16 +0000833
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000834 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000835 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000836
Ted Kremenek9aae5132007-08-23 21:42:29 +0000837 if (Stmt* Else = I->getElse()) {
838 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000839
Ted Kremenek9aae5132007-08-23 21:42:29 +0000840 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000841 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000842 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000843 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000844
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000845 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
846 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000847 else if (Block) {
848 if (!FinishBlock(ElseBlock))
849 return 0;
850 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000851 }
Mike Stump31feda52009-07-17 01:31:16 +0000852
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000853 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000854 CFGBlock* ThenBlock;
855 {
856 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +0000857 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000858 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000859 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000860 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000861
Ted Kremenek1b379512009-04-01 03:52:47 +0000862 if (!ThenBlock) {
863 // We can reach here if the "then" body has all NullStmts.
864 // Create an empty block so we can distinguish between true and false
865 // branches in path-sensitive analyses.
866 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000867 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000868 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000869 if (!FinishBlock(ThenBlock))
870 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000871 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000872 }
873
Mike Stump31feda52009-07-17 01:31:16 +0000874 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000875 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000876
Ted Kremenek9aae5132007-08-23 21:42:29 +0000877 // Set the terminator of the new block to the If statement.
878 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000879
Mike Stump773582d2009-07-23 23:25:26 +0000880 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000881 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000882
Ted Kremenek9aae5132007-08-23 21:42:29 +0000883 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000884 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
885 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000886
887 // Add the condition as the last statement in the new block. This may create
888 // new blocks as the condition may contain control-flow. Any newly created
889 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000890 Block = addStmt(I->getCond());
891
892 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
893 // and the condition variable initialization to the CFG.
894 if (VarDecl *VD = I->getConditionVariable()) {
895 if (Expr *Init = VD->getInit()) {
896 autoCreateBlock();
897 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
898 addStmt(Init);
899 }
900 }
901
902 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000903}
Mike Stump31feda52009-07-17 01:31:16 +0000904
905
Ted Kremenek9aae5132007-08-23 21:42:29 +0000906CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000907 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000908 //
Mike Stump31feda52009-07-17 01:31:16 +0000909 // NOTE: If a "return" appears in the middle of a block, this means that the
910 // code afterwards is DEAD (unreachable). We still keep a basic block
911 // for that code; a simple "mark-and-sweep" from the entry block will be
912 // able to report such dead blocks.
Ted Kremenek0868eea2009-09-24 18:45:41 +0000913 if (Block)
914 FinishBlock(Block);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000915
916 // Create the new block.
917 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000918
Ted Kremenek9aae5132007-08-23 21:42:29 +0000919 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000920 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000921
922 // Add the return statement to the block. This may create new blocks if R
923 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000924 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000925}
926
927CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
928 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000929 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000930 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000931
Ted Kremenek93668002009-07-17 22:18:43 +0000932 if (!LabelBlock) // This can happen when the body is empty, i.e.
933 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000934
Ted Kremenek93668002009-07-17 22:18:43 +0000935 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000936 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000937
938 // Labels partition blocks, so this is the end of the basic block we were
939 // processing (L is the block's label). Because this is label (and we have
940 // already processed the substatement) there is no extra control-flow to worry
941 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000942 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000943 if (!FinishBlock(LabelBlock))
944 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000945
946 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000947 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000948
Ted Kremenek9aae5132007-08-23 21:42:29 +0000949 // This block is now the implicit successor of other blocks.
950 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000951
Ted Kremenek9aae5132007-08-23 21:42:29 +0000952 return LabelBlock;
953}
954
955CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000956 // Goto is a control-flow statement. Thus we stop processing the current
957 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000958 if (Block)
959 FinishBlock(Block);
960
Ted Kremenek9aae5132007-08-23 21:42:29 +0000961 Block = createBlock(false);
962 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000963
964 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000965 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000966
Ted Kremenek9aae5132007-08-23 21:42:29 +0000967 if (I == LabelMap.end())
968 // We will need to backpatch this block later.
969 BackpatchBlocks.push_back(Block);
970 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000971 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000972
Mike Stump31feda52009-07-17 01:31:16 +0000973 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000974}
975
976CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000977 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000978
Mike Stump014b3ea2009-07-21 01:12:51 +0000979 // "for" is a control-flow statement. Thus we stop processing the current
980 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000981 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000982 if (!FinishBlock(Block))
983 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000984 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000985 } else
986 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000987
Ted Kremenek304a9532010-05-21 20:30:15 +0000988 // Save the current value for the break targets.
989 // All breaks should go to the code following the loop.
990 SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock);
991 BreakTargetBlock = LoopSuccessor;
992
Mike Stump31feda52009-07-17 01:31:16 +0000993 // Because of short-circuit evaluation, the condition of the loop can span
994 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
995 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000996 CFGBlock* ExitConditionBlock = createBlock(false);
997 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000998
Ted Kremenek81e14852007-08-27 19:46:09 +0000999 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001000 ExitConditionBlock->setTerminator(F);
1001
1002 // Now add the actual condition to the condition block. Because the condition
1003 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001004 if (Stmt* C = F->getCond()) {
1005 Block = ExitConditionBlock;
1006 EntryConditionBlock = addStmt(C);
Ted Kremenekec92f942009-12-24 01:49:06 +00001007 assert(Block == EntryConditionBlock);
1008
1009 // If this block contains a condition variable, add both the condition
1010 // variable and initializer to the CFG.
1011 if (VarDecl *VD = F->getConditionVariable()) {
1012 if (Expr *Init = VD->getInit()) {
1013 autoCreateBlock();
1014 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1015 EntryConditionBlock = addStmt(Init);
1016 assert(Block == EntryConditionBlock);
1017 }
1018 }
1019
Ted Kremenek55957a82009-05-02 00:13:27 +00001020 if (Block) {
1021 if (!FinishBlock(EntryConditionBlock))
1022 return 0;
1023 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001024 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001025
Mike Stump31feda52009-07-17 01:31:16 +00001026 // The condition block is the implicit successor for the loop body as well as
1027 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001028 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001029
Mike Stump773582d2009-07-23 23:25:26 +00001030 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001031 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001032
Mike Stump773582d2009-07-23 23:25:26 +00001033 if (F->getCond())
1034 KnownVal = TryEvaluateBool(F->getCond());
1035
Ted Kremenek9aae5132007-08-23 21:42:29 +00001036 // Now create the loop body.
1037 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001038 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001039
Ted Kremenek304a9532010-05-21 20:30:15 +00001040 // Save the current values for Block, Succ, and continue targets.
1041 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1042 save_continue(ContinueTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001043
Ted Kremeneke9610502007-08-30 18:39:40 +00001044 // Create a new block to contain the (bottom) of the loop body.
1045 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001046
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001047 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001048 // Generate increment code in its own basic block. This is the target of
1049 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001050 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001051 } else {
1052 // No increment code. Create a special, empty, block that is used as the
1053 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001054 assert(Succ == EntryConditionBlock);
1055 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001056 }
Mike Stump31feda52009-07-17 01:31:16 +00001057
Ted Kremenek902393b2009-04-28 00:51:56 +00001058 // Finish up the increment (or empty) block if it hasn't been already.
1059 if (Block) {
1060 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +00001061 if (!FinishBlock(Block))
1062 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001063 Block = 0;
1064 }
Mike Stump31feda52009-07-17 01:31:16 +00001065
Ted Kremenek902393b2009-04-28 00:51:56 +00001066 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001067
Ted Kremenek902393b2009-04-28 00:51:56 +00001068 // The starting block for the loop increment is the block that should
1069 // represent the 'loop target' for looping back to the start of the loop.
1070 ContinueTargetBlock->setLoopTarget(F);
1071
Mike Stump31feda52009-07-17 01:31:16 +00001072 // Now populate the body block, and in the process create new blocks as we
1073 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001074 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001075
1076 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +00001077 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +00001078 else if (Block && !FinishBlock(BodyBlock))
1079 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001080
Ted Kremenek30754282009-07-24 04:47:11 +00001081 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001082 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001083 }
Mike Stump31feda52009-07-17 01:31:16 +00001084
Ted Kremenek30754282009-07-24 04:47:11 +00001085 // Link up the condition block with the code that follows the loop. (the
1086 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001087 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001088
Ted Kremenek9aae5132007-08-23 21:42:29 +00001089 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001090 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001091 if (Stmt* I = F->getInit()) {
1092 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001093 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001094 } else {
1095 // There is no loop initialization. We are thus basically a while loop.
1096 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001097 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001098 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001099 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001100 }
1101}
1102
Ted Kremenek5868ec62010-04-11 17:02:10 +00001103CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1104 if (asc.alwaysAdd()) {
1105 autoCreateBlock();
1106 AppendStmt(Block, M, asc);
1107 }
1108 return Visit(M->getBase(),
1109 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1110 : AddStmtChoice::AsLValueNotAlwaysAdd);
1111}
1112
Ted Kremenek9d56e642008-11-11 17:10:00 +00001113CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1114 // Objective-C fast enumeration 'for' statements:
1115 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1116 //
1117 // for ( Type newVariable in collection_expression ) { statements }
1118 //
1119 // becomes:
1120 //
1121 // prologue:
1122 // 1. collection_expression
1123 // T. jump to loop_entry
1124 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001125 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001126 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1127 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1128 // TB:
1129 // statements
1130 // T. jump to loop_entry
1131 // FB:
1132 // what comes after
1133 //
1134 // and
1135 //
1136 // Type existingItem;
1137 // for ( existingItem in expression ) { statements }
1138 //
1139 // becomes:
1140 //
Mike Stump31feda52009-07-17 01:31:16 +00001141 // the same with newVariable replaced with existingItem; the binding works
1142 // the same except that for one ObjCForCollectionStmt::getElement() returns
1143 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001144 //
Mike Stump31feda52009-07-17 01:31:16 +00001145
Ted Kremenek9d56e642008-11-11 17:10:00 +00001146 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001147
Ted Kremenek9d56e642008-11-11 17:10:00 +00001148 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001149 if (!FinishBlock(Block))
1150 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001151 LoopSuccessor = Block;
1152 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001153 } else
1154 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001155
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001156 // Build the condition blocks.
1157 CFGBlock* ExitConditionBlock = createBlock(false);
1158 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001159
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001160 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001161 ExitConditionBlock->setTerminator(S);
1162
1163 // The last statement in the block should be the ObjCForCollectionStmt, which
1164 // performs the actual binding to 'element' and determines if there are any
1165 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001166 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001167 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001168
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001169 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001170 // generate new blocks as necesary. We DON'T add the statement by default to
1171 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001172 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001173 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001174 if (!FinishBlock(EntryConditionBlock))
1175 return 0;
1176 Block = 0;
1177 }
Mike Stump31feda52009-07-17 01:31:16 +00001178
1179 // The condition block is the implicit successor for the loop body as well as
1180 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001181 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001182
Ted Kremenek9d56e642008-11-11 17:10:00 +00001183 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001184 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001185 // Save the current values for Succ, continue and break targets.
1186 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001187 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1188
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001189 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001190 ContinueTargetBlock = EntryConditionBlock;
1191
Ted Kremenek93668002009-07-17 22:18:43 +00001192 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001193
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001194 if (!BodyBlock)
1195 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001196 else if (Block) {
1197 if (!FinishBlock(BodyBlock))
1198 return 0;
1199 }
Mike Stump31feda52009-07-17 01:31:16 +00001200
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001201 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001202 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001203 }
Mike Stump31feda52009-07-17 01:31:16 +00001204
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001205 // Link up the condition block with the code that follows the loop.
1206 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001207 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001208
Ted Kremenek9d56e642008-11-11 17:10:00 +00001209 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001210 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001211 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001212}
1213
Ted Kremenek49805452009-05-02 01:49:13 +00001214CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1215 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001216
Ted Kremenek49805452009-05-02 01:49:13 +00001217 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001218 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001219
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001220 // The sync body starts its own basic block. This makes it a little easier
1221 // for diagnostic clients.
1222 if (SyncBlock) {
1223 if (!FinishBlock(SyncBlock))
1224 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001225
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001226 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001227 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001228 }
Mike Stump31feda52009-07-17 01:31:16 +00001229
Ted Kremenek49805452009-05-02 01:49:13 +00001230 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001231 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001232}
Mike Stump31feda52009-07-17 01:31:16 +00001233
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001234CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001235 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001236 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001237}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001238
Ted Kremenek9aae5132007-08-23 21:42:29 +00001239CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001240 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001241
Mike Stump014b3ea2009-07-21 01:12:51 +00001242 // "while" is a control-flow statement. Thus we stop processing the current
1243 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001244 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001245 if (!FinishBlock(Block))
1246 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001247 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001248 } else
1249 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001250
1251 // Because of short-circuit evaluation, the condition of the loop can span
1252 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1253 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001254 CFGBlock* ExitConditionBlock = createBlock(false);
1255 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001256
Ted Kremenek81e14852007-08-27 19:46:09 +00001257 // Set the terminator for the "exit" condition block.
1258 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001259
1260 // Now add the actual condition to the condition block. Because the condition
1261 // itself may contain control-flow, new blocks may be created. Thus we update
1262 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001263 if (Stmt* C = W->getCond()) {
1264 Block = ExitConditionBlock;
1265 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001266 assert(Block == EntryConditionBlock);
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001267
1268 // If this block contains a condition variable, add both the condition
1269 // variable and initializer to the CFG.
1270 if (VarDecl *VD = W->getConditionVariable()) {
1271 if (Expr *Init = VD->getInit()) {
1272 autoCreateBlock();
1273 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1274 EntryConditionBlock = addStmt(Init);
1275 assert(Block == EntryConditionBlock);
1276 }
1277 }
1278
Ted Kremenek55957a82009-05-02 00:13:27 +00001279 if (Block) {
1280 if (!FinishBlock(EntryConditionBlock))
1281 return 0;
1282 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001283 }
Mike Stump31feda52009-07-17 01:31:16 +00001284
1285 // The condition block is the implicit successor for the loop body as well as
1286 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001287 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001288
Mike Stump773582d2009-07-23 23:25:26 +00001289 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001290 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001291
Ted Kremenek9aae5132007-08-23 21:42:29 +00001292 // Process the loop body.
1293 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001294 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001295
1296 // Save the current values for Block, Succ, and continue and break targets
1297 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1298 save_continue(ContinueTargetBlock),
1299 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001300
Mike Stump31feda52009-07-17 01:31:16 +00001301 // Create an empty block to represent the transition block for looping back
1302 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001303 Block = 0;
1304 assert(Succ == EntryConditionBlock);
1305 Succ = createBlock();
1306 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001307 ContinueTargetBlock = Succ;
1308
Ted Kremenek9aae5132007-08-23 21:42:29 +00001309 // All breaks should go to the code following the loop.
1310 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001311
Ted Kremenek9aae5132007-08-23 21:42:29 +00001312 // NULL out Block to force lazy instantiation of blocks for the body.
1313 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001314
Ted Kremenek9aae5132007-08-23 21:42:29 +00001315 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001316 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001317
Ted Kremeneke9610502007-08-30 18:39:40 +00001318 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001319 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001320 else if (Block) {
1321 if (!FinishBlock(BodyBlock))
1322 return 0;
1323 }
Mike Stump31feda52009-07-17 01:31:16 +00001324
Ted Kremenek30754282009-07-24 04:47:11 +00001325 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001326 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001327 }
Mike Stump31feda52009-07-17 01:31:16 +00001328
Ted Kremenek30754282009-07-24 04:47:11 +00001329 // Link up the condition block with the code that follows the loop. (the
1330 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001331 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001332
1333 // There can be no more statements in the condition block since we loop back
1334 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001335 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001336
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001337 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001338 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001339 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001340}
Mike Stump11289f42009-09-09 15:08:12 +00001341
1342
Ted Kremenek93668002009-07-17 22:18:43 +00001343CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1344 // FIXME: For now we pretend that @catch and the code it contains does not
1345 // exit.
1346 return Block;
1347}
Mike Stump31feda52009-07-17 01:31:16 +00001348
Ted Kremenek93041ba2008-12-09 20:20:09 +00001349CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1350 // FIXME: This isn't complete. We basically treat @throw like a return
1351 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001352
Ted Kremenek0868eea2009-09-24 18:45:41 +00001353 // If we were in the middle of a block we stop processing that block.
Ted Kremenek93668002009-07-17 22:18:43 +00001354 if (Block && !FinishBlock(Block))
1355 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001356
Ted Kremenek93041ba2008-12-09 20:20:09 +00001357 // Create the new block.
1358 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001359
Ted Kremenek93041ba2008-12-09 20:20:09 +00001360 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001361 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001362
1363 // Add the statement to the block. This may create new blocks if S contains
1364 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001365 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001366}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001367
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001368CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001369 // If we were in the middle of a block we stop processing that block.
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001370 if (Block && !FinishBlock(Block))
1371 return 0;
1372
1373 // Create the new block.
1374 Block = createBlock(false);
1375
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001376 if (TryTerminatedBlock)
1377 // The current try statement is the only successor.
1378 AddSuccessor(Block, TryTerminatedBlock);
1379 else
1380 // otherwise the Exit block is the only successor.
1381 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001382
1383 // Add the statement to the block. This may create new blocks if S contains
1384 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001385 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001386}
1387
Ted Kremenek93668002009-07-17 22:18:43 +00001388CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001389 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001390
Mike Stump8d50b6a2009-07-21 01:27:50 +00001391 // "do...while" is a control-flow statement. Thus we stop processing the
1392 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001393 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001394 if (!FinishBlock(Block))
1395 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001396 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001397 } else
1398 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001399
1400 // Because of short-circuit evaluation, the condition of the loop can span
1401 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1402 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001403 CFGBlock* ExitConditionBlock = createBlock(false);
1404 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001405
Ted Kremenek81e14852007-08-27 19:46:09 +00001406 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001407 ExitConditionBlock->setTerminator(D);
1408
1409 // Now add the actual condition to the condition block. Because the condition
1410 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001411 if (Stmt* C = D->getCond()) {
1412 Block = ExitConditionBlock;
1413 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001414 if (Block) {
1415 if (!FinishBlock(EntryConditionBlock))
1416 return 0;
1417 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001418 }
Mike Stump31feda52009-07-17 01:31:16 +00001419
Ted Kremeneka1523a32008-02-27 07:20:00 +00001420 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001421 Succ = EntryConditionBlock;
1422
Mike Stump773582d2009-07-23 23:25:26 +00001423 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001424 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001425
Ted Kremenek9aae5132007-08-23 21:42:29 +00001426 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001427 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001428 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001429 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001430
Ted Kremenek9aae5132007-08-23 21:42:29 +00001431 // Save the current values for Block, Succ, and continue and break targets
1432 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001433 save_continue(ContinueTargetBlock),
1434 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001435
Ted Kremenek9aae5132007-08-23 21:42:29 +00001436 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001437 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001438
Ted Kremenek9aae5132007-08-23 21:42:29 +00001439 // All breaks should go to the code following the loop.
1440 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001441
Ted Kremenek9aae5132007-08-23 21:42:29 +00001442 // NULL out Block to force lazy instantiation of blocks for the body.
1443 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001444
Ted Kremenek9aae5132007-08-23 21:42:29 +00001445 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001446 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001447
Ted Kremeneke9610502007-08-30 18:39:40 +00001448 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001449 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001450 else if (Block) {
1451 if (!FinishBlock(BodyBlock))
1452 return 0;
1453 }
Mike Stump31feda52009-07-17 01:31:16 +00001454
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001455 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001456 // ExitConditionBlock to represent the "loop back" transition. Create an
1457 // empty block to represent the transition block for looping back to the
1458 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001459 // FIXME: Can we do this more efficiently without adding another block?
1460 Block = NULL;
1461 Succ = BodyBlock;
1462 CFGBlock *LoopBackBlock = createBlock();
1463 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001464
Ted Kremenek30754282009-07-24 04:47:11 +00001465 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001466 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001467 }
Mike Stump31feda52009-07-17 01:31:16 +00001468
Ted Kremenek30754282009-07-24 04:47:11 +00001469 // Link up the condition block with the code that follows the loop.
1470 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001471 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001472
1473 // There can be no more statements in the body block(s) since we loop back to
1474 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001475 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001476
Ted Kremenek9aae5132007-08-23 21:42:29 +00001477 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001478 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001479 return BodyBlock;
1480}
1481
1482CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1483 // "continue" is a control-flow statement. Thus we stop processing the
1484 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001485 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001486 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001487
Ted Kremenek9aae5132007-08-23 21:42:29 +00001488 // Now create a new block that ends with the continue statement.
1489 Block = createBlock(false);
1490 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001491
Ted Kremenek9aae5132007-08-23 21:42:29 +00001492 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001493 // incomplete AST. This means the CFG cannot be constructed.
1494 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001495 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001496 else
1497 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001498
Ted Kremenek9aae5132007-08-23 21:42:29 +00001499 return Block;
1500}
Mike Stump11289f42009-09-09 15:08:12 +00001501
Ted Kremenek0747de62009-07-18 00:47:21 +00001502CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001503 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001504
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001505 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001506 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001507 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001508 }
Mike Stump11289f42009-09-09 15:08:12 +00001509
Ted Kremenek93668002009-07-17 22:18:43 +00001510 // VLA types have expressions that must be evaluated.
1511 if (E->isArgumentType()) {
1512 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1513 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1514 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001515 }
Mike Stump11289f42009-09-09 15:08:12 +00001516
Mike Stump31feda52009-07-17 01:31:16 +00001517 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001518}
Mike Stump11289f42009-09-09 15:08:12 +00001519
Ted Kremenek93668002009-07-17 22:18:43 +00001520/// VisitStmtExpr - Utility method to handle (nested) statement
1521/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001522CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1523 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001524 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001525 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001526 }
Ted Kremenek93668002009-07-17 22:18:43 +00001527 return VisitCompoundStmt(SE->getSubStmt());
1528}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001529
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001530CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001531 // "switch" is a control-flow statement. Thus we stop processing the current
1532 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001533 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001534
Ted Kremenek9aae5132007-08-23 21:42:29 +00001535 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001536 if (!FinishBlock(Block))
1537 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001538 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001539 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001540
1541 // Save the current "switch" context.
1542 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001543 save_break(BreakTargetBlock),
1544 save_default(DefaultCaseBlock);
1545
Mike Stump31feda52009-07-17 01:31:16 +00001546 // Set the "default" case to be the block after the switch statement. If the
1547 // switch statement contains a "default:", this value will be overwritten with
1548 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001549 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001550
Ted Kremenek9aae5132007-08-23 21:42:29 +00001551 // Create a new block that will contain the switch statement.
1552 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001553
Ted Kremenek9aae5132007-08-23 21:42:29 +00001554 // Now process the switch body. The code after the switch is the implicit
1555 // successor.
1556 Succ = SwitchSuccessor;
1557 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001558
1559 // When visiting the body, the case statements should automatically get linked
1560 // up to the switch. We also don't keep a pointer to the body, since all
1561 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001562 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001563 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001564 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001565 if (Block) {
1566 if (!FinishBlock(BodyBlock))
1567 return 0;
1568 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001569
Mike Stump31feda52009-07-17 01:31:16 +00001570 // If we have no "default:" case, the default transition is to the code
1571 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001572 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001573
Ted Kremenek81e14852007-08-27 19:46:09 +00001574 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001575 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001576 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001577 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001578 Block = addStmt(Terminator->getCond());
1579
1580 // Finally, if the SwitchStmt contains a condition variable, add both the
1581 // SwitchStmt and the condition variable initialization to the CFG.
1582 if (VarDecl *VD = Terminator->getConditionVariable()) {
1583 if (Expr *Init = VD->getInit()) {
1584 autoCreateBlock();
1585 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1586 addStmt(Init);
1587 }
1588 }
1589
1590 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001591}
1592
Ted Kremenek93668002009-07-17 22:18:43 +00001593CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001594 // CaseStmts are essentially labels, so they are the first statement in a
1595 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001596
Ted Kremenek93668002009-07-17 22:18:43 +00001597 if (CS->getSubStmt())
1598 addStmt(CS->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001599
Ted Kremenek55e91e82007-08-30 18:48:11 +00001600 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001601 if (!CaseBlock)
1602 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001603
1604 // Cases statements partition blocks, so this is the top of the basic block we
1605 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001606 CaseBlock->setLabel(CS);
1607
Ted Kremenek55957a82009-05-02 00:13:27 +00001608 if (!FinishBlock(CaseBlock))
1609 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001610
1611 // Add this block to the list of successors for the block with the switch
1612 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001613 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001614 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001615
Ted Kremenek9aae5132007-08-23 21:42:29 +00001616 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1617 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001618
Ted Kremenek9aae5132007-08-23 21:42:29 +00001619 // This block is now the implicit successor of other blocks.
1620 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001621
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001622 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001623}
Mike Stump31feda52009-07-17 01:31:16 +00001624
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001625CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001626 if (Terminator->getSubStmt())
1627 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001628
Ted Kremenek654c78f2008-02-13 22:05:39 +00001629 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001630
1631 if (!DefaultCaseBlock)
1632 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001633
1634 // Default statements partition blocks, so this is the top of the basic block
1635 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001636 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001637
Ted Kremenek55957a82009-05-02 00:13:27 +00001638 if (!FinishBlock(DefaultCaseBlock))
1639 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001640
Mike Stump31feda52009-07-17 01:31:16 +00001641 // Unlike case statements, we don't add the default block to the successors
1642 // for the switch statement immediately. This is done when we finish
1643 // processing the switch statement. This allows for the default case
1644 // (including a fall-through to the code after the switch statement) to always
1645 // be the last successor of a switch-terminated block.
1646
Ted Kremenek654c78f2008-02-13 22:05:39 +00001647 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1648 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001649
Ted Kremenek654c78f2008-02-13 22:05:39 +00001650 // This block is now the implicit successor of other blocks.
1651 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001652
1653 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001654}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001655
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001656CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1657 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1658 // current block.
1659 CFGBlock* TrySuccessor = NULL;
1660
1661 if (Block) {
1662 if (!FinishBlock(Block))
1663 return 0;
1664 TrySuccessor = Block;
1665 } else TrySuccessor = Succ;
1666
Mike Stump0bdba6c2010-01-20 01:15:34 +00001667 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001668
1669 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00001670 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001671 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00001672 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001673
Mike Stump0bdba6c2010-01-20 01:15:34 +00001674 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001675 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1676 // The code after the try is the implicit successor.
1677 Succ = TrySuccessor;
1678 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001679 if (CS->getExceptionDecl() == 0) {
1680 HasCatchAll = true;
1681 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001682 Block = NULL;
1683 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1684 if (CatchBlock == 0)
1685 return 0;
1686 // Add this block to the list of successors for the block with the try
1687 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00001688 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001689 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00001690 if (!HasCatchAll) {
1691 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00001692 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001693 else
Mike Stump845384a2010-01-20 01:30:58 +00001694 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00001695 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001696
1697 // The code after the try is the implicit successor.
1698 Succ = TrySuccessor;
1699
Mike Stump845384a2010-01-20 01:30:58 +00001700 // Save the current "try" context.
1701 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1702 TryTerminatedBlock = NewTryTerminatedBlock;
1703
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001704 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001705 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001706 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001707 return Block;
1708}
1709
1710CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1711 // CXXCatchStmt are treated like labels, so they are the first statement in a
1712 // block.
1713
1714 if (CS->getHandlerBlock())
1715 addStmt(CS->getHandlerBlock());
1716
1717 CFGBlock* CatchBlock = Block;
1718 if (!CatchBlock)
1719 CatchBlock = createBlock();
1720
1721 CatchBlock->setLabel(CS);
1722
1723 if (!FinishBlock(CatchBlock))
1724 return 0;
1725
1726 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1727 Block = NULL;
1728
1729 return CatchBlock;
1730}
1731
Zhongxing Xu7e612172010-04-13 09:38:01 +00001732CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
1733 AddStmtChoice asc) {
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001734 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1735 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00001736 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001737 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00001738 return VisitChildren(C);
1739}
1740
Ted Kremenekeda180e22007-08-28 19:26:49 +00001741CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001742 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001743 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001744
Ted Kremenekeda180e22007-08-28 19:26:49 +00001745 if (!IBlock) {
1746 IBlock = createBlock(false);
1747 cfg->setIndirectGotoBlock(IBlock);
1748 }
Mike Stump31feda52009-07-17 01:31:16 +00001749
Ted Kremenekeda180e22007-08-28 19:26:49 +00001750 // IndirectGoto is a control-flow statement. Thus we stop processing the
1751 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001752 if (Block && !FinishBlock(Block))
1753 return 0;
1754
Ted Kremenekeda180e22007-08-28 19:26:49 +00001755 Block = createBlock(false);
1756 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001757 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001758 return addStmt(I->getTarget());
1759}
1760
Ted Kremenek04cca642007-08-23 21:26:19 +00001761} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001762
Mike Stump31feda52009-07-17 01:31:16 +00001763/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1764/// no successors or predecessors. If this is the first block created in the
1765/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001766CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001767 bool first_block = begin() == end();
1768
1769 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001770 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1771 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1772 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001773
1774 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001775 if (first_block)
1776 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001777
1778 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001779 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001780}
1781
Ted Kremenek889073f2007-08-23 16:51:22 +00001782/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1783/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00001784CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Mike Stump04c68512010-01-21 15:20:48 +00001785 bool AddEHEdges, bool AddScopes) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001786 CFGBuilder Builder;
Mike Stump04c68512010-01-21 15:20:48 +00001787 return Builder.buildCFG(D, Statement, C, AddEHEdges, AddScopes);
Ted Kremenek889073f2007-08-23 16:51:22 +00001788}
1789
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001790//===----------------------------------------------------------------------===//
1791// CFG: Queries for BlkExprs.
1792//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001793
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001794namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001795 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001796}
1797
Ted Kremenekbff98442009-12-23 23:37:10 +00001798static void FindSubExprAssignments(Stmt *S,
1799 llvm::SmallPtrSet<Expr*,50>& Set) {
1800 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001801 return;
Mike Stump31feda52009-07-17 01:31:16 +00001802
Ted Kremenekbff98442009-12-23 23:37:10 +00001803 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1804 Stmt *child = *I;
1805 if (!child)
1806 continue;
1807
1808 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001809 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001810
Ted Kremenekbff98442009-12-23 23:37:10 +00001811 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00001812 }
1813}
1814
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001815static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1816 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001817
1818 // Look for assignments that are used as subexpressions. These are the only
1819 // assignments that we want to *possibly* register as a block-level
1820 // expression. Basically, if an assignment occurs both in a subexpression and
1821 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001822 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001823
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001824 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001825 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001826 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001827
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001828 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001829
1830 // Iterate over the statements again on identify the Expr* and Stmt* at the
1831 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001832
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001833 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001834 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001835
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001836 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001837 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001838 // expression are really "statements" whose value is never used by
1839 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001840 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001841 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001842 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1843 // Special handling for statement expressions. The last statement in
1844 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001845 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001846 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001847 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001848 (*M)[C->body_back()] = x;
1849 }
1850 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001851
Ted Kremenek95a123c2008-01-26 00:03:27 +00001852 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001853 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001854 }
Mike Stump31feda52009-07-17 01:31:16 +00001855
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001856 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001857
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001858 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001859
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001860 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001861 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001862 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001863 }
1864 }
Mike Stump31feda52009-07-17 01:31:16 +00001865
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001866 return M;
1867}
1868
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001869CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1870 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001871 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001872
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001873 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001874 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00001875 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001876}
1877
1878unsigned CFG::getNumBlkExprs() {
1879 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1880 return M->size();
1881 else {
1882 // We assume callers interested in the number of BlkExprs will want
1883 // the map constructed if it doesn't already exist.
1884 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1885 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1886 }
1887}
1888
Ted Kremenek6065ef62008-04-28 18:00:46 +00001889//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001890// Cleanup: CFG dstor.
1891//===----------------------------------------------------------------------===//
1892
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001893CFG::~CFG() {
1894 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1895}
Mike Stump31feda52009-07-17 01:31:16 +00001896
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001897//===----------------------------------------------------------------------===//
1898// CFG pretty printing
1899//===----------------------------------------------------------------------===//
1900
Ted Kremenek7e776b12007-08-22 18:22:34 +00001901namespace {
1902
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001903class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001904 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1905 StmtMapTy StmtMap;
1906 signed CurrentBlock;
1907 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001908 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001909public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001910
Chris Lattnerc61089a2009-06-30 01:26:17 +00001911 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1912 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001913 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1914 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001915 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001916 BI != BEnd; ++BI, ++j )
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001917 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001918 }
1919 }
Mike Stump31feda52009-07-17 01:31:16 +00001920
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001921 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001922
Chris Lattnerc61089a2009-06-30 01:26:17 +00001923 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001924 void setBlockID(signed i) { CurrentBlock = i; }
1925 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001926
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001927 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001928
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001929 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001930
1931 if (I == StmtMap.end())
1932 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001933
1934 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00001935 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001936 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001937 }
Mike Stump31feda52009-07-17 01:31:16 +00001938
Ted Kremenek60983dc2010-01-19 20:52:05 +00001939 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001940 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001941 }
1942};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001943} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001944
Chris Lattnerc61089a2009-06-30 01:26:17 +00001945
1946namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001947class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001948 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001949
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001950 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001951 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001952 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001953public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001954 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001955 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001956 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001957
Ted Kremenek9aae5132007-08-23 21:42:29 +00001958 void VisitIfStmt(IfStmt* I) {
1959 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001960 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001961 }
Mike Stump31feda52009-07-17 01:31:16 +00001962
Ted Kremenek9aae5132007-08-23 21:42:29 +00001963 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001964 void VisitStmt(Stmt* Terminator) {
1965 Terminator->printPretty(OS, Helper, Policy);
1966 }
1967
Ted Kremenek9aae5132007-08-23 21:42:29 +00001968 void VisitForStmt(ForStmt* F) {
1969 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001970 if (F->getInit())
1971 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001972 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00001973 if (Stmt* C = F->getCond())
1974 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001975 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00001976 if (F->getInc())
1977 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001978 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001979 }
Mike Stump31feda52009-07-17 01:31:16 +00001980
Ted Kremenek9aae5132007-08-23 21:42:29 +00001981 void VisitWhileStmt(WhileStmt* W) {
1982 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001983 if (Stmt* C = W->getCond())
1984 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001985 }
Mike Stump31feda52009-07-17 01:31:16 +00001986
Ted Kremenek9aae5132007-08-23 21:42:29 +00001987 void VisitDoStmt(DoStmt* D) {
1988 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00001989 if (Stmt* C = D->getCond())
1990 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001991 }
Mike Stump31feda52009-07-17 01:31:16 +00001992
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001993 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001994 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001995 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001996 }
Mike Stump31feda52009-07-17 01:31:16 +00001997
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001998 void VisitCXXTryStmt(CXXTryStmt* CS) {
1999 OS << "try ...";
2000 }
2001
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002002 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002003 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002004 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002005 }
Mike Stump31feda52009-07-17 01:31:16 +00002006
Ted Kremenek391f94a2007-08-31 22:29:13 +00002007 void VisitChooseExpr(ChooseExpr* C) {
2008 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002009 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002010 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002011 }
Mike Stump31feda52009-07-17 01:31:16 +00002012
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002013 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2014 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002015 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002016 }
Mike Stump31feda52009-07-17 01:31:16 +00002017
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002018 void VisitBinaryOperator(BinaryOperator* B) {
2019 if (!B->isLogicalOp()) {
2020 VisitExpr(B);
2021 return;
2022 }
Mike Stump31feda52009-07-17 01:31:16 +00002023
Douglas Gregor7de59662009-05-29 20:38:28 +00002024 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002025
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002026 switch (B->getOpcode()) {
2027 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002028 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002029 return;
2030 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002031 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002032 return;
2033 default:
2034 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00002035 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002036 }
Mike Stump31feda52009-07-17 01:31:16 +00002037
Ted Kremenek12687ff2007-08-27 21:54:41 +00002038 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002039 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002040 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00002041};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002042} // end anonymous namespace
2043
Mike Stump31feda52009-07-17 01:31:16 +00002044
Chris Lattnerc61089a2009-06-30 01:26:17 +00002045static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00002046 const CFGElement &E) {
2047 Stmt *Terminator = E;
2048
2049 if (E.asStartScope()) {
2050 OS << "start scope\n";
2051 return;
2052 }
2053 if (E.asEndScope()) {
2054 OS << "end scope\n";
2055 return;
2056 }
2057
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002058 if (Helper) {
2059 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002060 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002061 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00002062
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002063 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00002064 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00002065 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00002066 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002067 return;
2068 }
2069 }
Mike Stump31feda52009-07-17 01:31:16 +00002070
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002071 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002072 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002073 if (B->getOpcode() == BinaryOperator::Comma) {
2074 OS << "... , ";
2075 Helper->handledStmt(B->getRHS(),OS);
2076 OS << '\n';
2077 return;
Mike Stump31feda52009-07-17 01:31:16 +00002078 }
2079 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002080 }
Mike Stump31feda52009-07-17 01:31:16 +00002081
Chris Lattnerc61089a2009-06-30 01:26:17 +00002082 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00002083
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002084 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002085 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002086}
Mike Stump31feda52009-07-17 01:31:16 +00002087
Chris Lattnerc61089a2009-06-30 01:26:17 +00002088static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2089 const CFGBlock& B,
2090 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00002091
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002092 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00002093
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002094 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00002095 OS << "\n [ B" << B.getBlockID();
2096
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002097 if (&B == &cfg->getEntry())
2098 OS << " (ENTRY) ]\n";
2099 else if (&B == &cfg->getExit())
2100 OS << " (EXIT) ]\n";
2101 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002102 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002103 else
2104 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00002105
Ted Kremenek71eca012007-08-29 23:20:49 +00002106 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00002107 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002108
2109 if (print_edges)
2110 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002111
Mike Stump92244b02010-01-19 22:00:14 +00002112 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002113 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00002114 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00002115 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002116 C->getLHS()->printPretty(OS, Helper,
2117 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002118 if (C->getRHS()) {
2119 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002120 C->getRHS()->printPretty(OS, Helper,
2121 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002122 }
Mike Stump92244b02010-01-19 22:00:14 +00002123 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002124 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00002125 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002126 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00002127 if (CS->getExceptionDecl())
2128 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2129 0);
2130 else
2131 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002132 OS << ")";
2133
2134 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002135 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00002136
Ted Kremenek71eca012007-08-29 23:20:49 +00002137 OS << ":\n";
2138 }
Mike Stump31feda52009-07-17 01:31:16 +00002139
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002140 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002141 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00002142
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002143 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2144 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00002145
Ted Kremenek71eca012007-08-29 23:20:49 +00002146 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002147 if (print_edges)
2148 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002149
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002150 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002151
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002152 if (Helper)
2153 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002154
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002155 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002156 }
Mike Stump31feda52009-07-17 01:31:16 +00002157
Ted Kremenek71eca012007-08-29 23:20:49 +00002158 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002159 if (B.getTerminator()) {
2160 if (print_edges)
2161 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002162
Ted Kremenek71eca012007-08-29 23:20:49 +00002163 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002164
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002165 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002166
Chris Lattnerc61089a2009-06-30 01:26:17 +00002167 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2168 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002169 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00002170 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002171 }
Mike Stump31feda52009-07-17 01:31:16 +00002172
Ted Kremenek71eca012007-08-29 23:20:49 +00002173 if (print_edges) {
2174 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002175 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002176 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002177
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002178 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2179 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002180
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002181 if (i == 8 || (i-8) == 0)
2182 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002183
Ted Kremenek71eca012007-08-29 23:20:49 +00002184 OS << " B" << (*I)->getBlockID();
2185 }
Mike Stump31feda52009-07-17 01:31:16 +00002186
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002187 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002188
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002189 // Print the successors of this block.
2190 OS << " Successors (" << B.succ_size() << "):";
2191 i = 0;
2192
2193 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2194 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002195
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002196 if (i == 8 || (i-8) % 10 == 0)
2197 OS << "\n ";
2198
Mike Stump0d76d072009-07-20 23:24:15 +00002199 if (*I)
2200 OS << " B" << (*I)->getBlockID();
2201 else
2202 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002203 }
Mike Stump31feda52009-07-17 01:31:16 +00002204
Ted Kremenek71eca012007-08-29 23:20:49 +00002205 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002206 }
Mike Stump31feda52009-07-17 01:31:16 +00002207}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002208
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002209
2210/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002211void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002212
2213/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002214void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2215 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002216
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002217 // Print the entry block.
2218 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002219
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002220 // Iterate through the CFGBlocks and print them one by one.
2221 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2222 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002223 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002224 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002225
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002226 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002227 }
Mike Stump31feda52009-07-17 01:31:16 +00002228
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002229 // Print the exit block.
2230 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002231 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002232}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002233
2234/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002235void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2236 print(llvm::errs(), cfg, LO);
2237}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002238
2239/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2240/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002241void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2242 const LangOptions &LO) const {
2243 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002244 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002245}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002246
Ted Kremenek15647632008-01-30 23:02:42 +00002247/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002248void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002249 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002250 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002251 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2252}
2253
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002254Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002255
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002256 if (!Terminator)
2257 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002258
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002259 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002260
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002261 switch (Terminator->getStmtClass()) {
2262 default:
2263 break;
Mike Stump31feda52009-07-17 01:31:16 +00002264
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002265 case Stmt::ForStmtClass:
2266 E = cast<ForStmt>(Terminator)->getCond();
2267 break;
Mike Stump31feda52009-07-17 01:31:16 +00002268
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002269 case Stmt::WhileStmtClass:
2270 E = cast<WhileStmt>(Terminator)->getCond();
2271 break;
Mike Stump31feda52009-07-17 01:31:16 +00002272
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002273 case Stmt::DoStmtClass:
2274 E = cast<DoStmt>(Terminator)->getCond();
2275 break;
Mike Stump31feda52009-07-17 01:31:16 +00002276
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002277 case Stmt::IfStmtClass:
2278 E = cast<IfStmt>(Terminator)->getCond();
2279 break;
Mike Stump31feda52009-07-17 01:31:16 +00002280
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002281 case Stmt::ChooseExprClass:
2282 E = cast<ChooseExpr>(Terminator)->getCond();
2283 break;
Mike Stump31feda52009-07-17 01:31:16 +00002284
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002285 case Stmt::IndirectGotoStmtClass:
2286 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2287 break;
Mike Stump31feda52009-07-17 01:31:16 +00002288
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002289 case Stmt::SwitchStmtClass:
2290 E = cast<SwitchStmt>(Terminator)->getCond();
2291 break;
Mike Stump31feda52009-07-17 01:31:16 +00002292
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002293 case Stmt::ConditionalOperatorClass:
2294 E = cast<ConditionalOperator>(Terminator)->getCond();
2295 break;
Mike Stump31feda52009-07-17 01:31:16 +00002296
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002297 case Stmt::BinaryOperatorClass: // '&&' and '||'
2298 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002299 break;
Mike Stump31feda52009-07-17 01:31:16 +00002300
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002301 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002302 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002303 }
Mike Stump31feda52009-07-17 01:31:16 +00002304
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002305 return E ? E->IgnoreParens() : NULL;
2306}
2307
Ted Kremenek92137a32008-05-16 16:06:00 +00002308bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002309
Ted Kremenek92137a32008-05-16 16:06:00 +00002310 if (!Terminator)
2311 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002312
Ted Kremenek92137a32008-05-16 16:06:00 +00002313 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002314
Ted Kremenek92137a32008-05-16 16:06:00 +00002315 switch (Terminator->getStmtClass()) {
2316 default:
2317 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002318
2319 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002320 case Stmt::WhileStmtClass:
2321 case Stmt::DoStmtClass:
2322 case Stmt::IfStmtClass:
2323 case Stmt::ChooseExprClass:
2324 case Stmt::ConditionalOperatorClass:
2325 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002326 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002327 }
Mike Stump31feda52009-07-17 01:31:16 +00002328
Ted Kremenek92137a32008-05-16 16:06:00 +00002329 return E ? E->IgnoreParens() : NULL;
2330}
2331
Ted Kremenek15647632008-01-30 23:02:42 +00002332
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002333//===----------------------------------------------------------------------===//
2334// CFG Graphviz Visualization
2335//===----------------------------------------------------------------------===//
2336
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002337
2338#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002339static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002340#endif
2341
Chris Lattnerc61089a2009-06-30 01:26:17 +00002342void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002343#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002344 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002345 GraphHelper = &H;
2346 llvm::ViewGraph(this,"CFG");
2347 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002348#endif
2349}
2350
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002351namespace llvm {
2352template<>
2353struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002354
2355 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2356
2357 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002358
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002359#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002360 std::string OutSStr;
2361 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002362 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002363 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002364
2365 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2366
2367 // Process string output to make it nicer...
2368 for (unsigned i = 0; i != OutStr.length(); ++i)
2369 if (OutStr[i] == '\n') { // Left justify
2370 OutStr[i] = '\\';
2371 OutStr.insert(OutStr.begin()+i+1, 'l');
2372 }
Mike Stump31feda52009-07-17 01:31:16 +00002373
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002374 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002375#else
2376 return "";
2377#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002378 }
2379};
2380} // end namespace llvm