blob: 0c7ebef81cbc28b8e5b04a0bc6289d7f04940b64 [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"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000019#include "llvm/Support/GraphWriter.h"
20#include "llvm/Support/Compiler.h"
21#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}
Mike Stump31feda52009-07-17 01:31:16 +000038
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000039/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000040/// The builder is stateful: an instance of the builder should be used to only
41/// construct a single CFG.
42///
43/// Example usage:
44///
45/// CFGBuilder builder;
46/// CFG* cfg = builder.BuildAST(stmt1);
47///
Mike Stump31feda52009-07-17 01:31:16 +000048/// CFG construction is done via a recursive walk of an AST. We actually parse
49/// the AST in reverse order so that the successor of a basic block is
50/// constructed prior to its predecessor. This allows us to nicely capture
51/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000052///
Ted Kremenek93668002009-07-17 22:18:43 +000053class VISIBILITY_HIDDEN CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000054 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +000055 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +000056
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000057 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000058 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000059 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000060 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000061 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000062 CFGBlock* DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +000063
Ted Kremenekeda180e22007-08-28 19:26:49 +000064 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000065 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
66 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000067
68 // A list of blocks that end with a "goto" that must be backpatched to their
69 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000070 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000071 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000072
Ted Kremenekeda180e22007-08-28 19:26:49 +000073 // A list of labels whose address has been taken (for indirect gotos).
74 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
75 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000076
77public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +000078 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
79 Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000080 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek289ae4f2009-10-12 20:55:07 +000081 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {}
Mike Stump31feda52009-07-17 01:31:16 +000082
Ted Kremenek9aae5132007-08-23 21:42:29 +000083 // buildCFG - Used by external clients to construct the CFG.
Mike Stump0d76d072009-07-20 23:24:15 +000084 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump31feda52009-07-17 01:31:16 +000085
Ted Kremenek93668002009-07-17 22:18:43 +000086private:
87 // Visitors to walk an AST and construct the CFG.
88 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
89 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
Ted Kremenek0747de62009-07-18 00:47:21 +000090 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
91 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +000092 CFGBlock *VisitBreakStmt(BreakStmt *B);
93 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
94 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek21822592009-07-17 18:20:32 +000095 CFGBlock *VisitChooseExpr(ChooseExpr *C);
96 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
97 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
98 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump8dd1b6b2009-07-22 22:56:04 +000099 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenek93668002009-07-17 22:18:43 +0000100 CFGBlock *VisitDeclStmt(DeclStmt *DS);
101 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000102 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
103 CFGBlock *VisitDoStmt(DoStmt *D);
104 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000105 CFGBlock *VisitGotoStmt(GotoStmt* G);
106 CFGBlock *VisitIfStmt(IfStmt *I);
107 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
108 CFGBlock *VisitLabelStmt(LabelStmt *L);
109 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
110 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
111 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
112 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
113 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
114 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek0747de62009-07-18 00:47:21 +0000115 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
116 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000117 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
118 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000119
Ted Kremenek93668002009-07-17 22:18:43 +0000120 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
121 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
122 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000123
Ted Kremenek6065ef62008-04-28 18:00:46 +0000124 // NYS == Not Yet Supported
125 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000126 badCFG = true;
127 return Block;
128 }
Mike Stump31feda52009-07-17 01:31:16 +0000129
Ted Kremenek93668002009-07-17 22:18:43 +0000130 void autoCreateBlock() { if (!Block) Block = createBlock(); }
131 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000132 bool FinishBlock(CFGBlock* B);
Ted Kremenek93668002009-07-17 22:18:43 +0000133 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000134
135 void AppendStmt(CFGBlock *B, Stmt *S) {
136 B->appendStmt(S, cfg->getBumpVectorContext());
137 }
138
139 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
140 B->addSuccessor(S, cfg->getBumpVectorContext());
141 }
Mike Stump11289f42009-09-09 15:08:12 +0000142
Ted Kremenek963cc312009-07-24 06:55:42 +0000143 /// TryResult - a class representing a variant over the values
144 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
145 /// and is used by the CFGBuilder to decide if a branch condition
146 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000147 class TryResult {
148 int X;
149 public:
150 TryResult(bool b) : X(b ? 1 : 0) {}
151 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000152
Ted Kremenek30754282009-07-24 04:47:11 +0000153 bool isTrue() const { return X == 1; }
154 bool isFalse() const { return X == 0; }
155 bool isKnown() const { return X >= 0; }
156 void negate() {
157 assert(isKnown());
158 X ^= 0x1;
159 }
160 };
Mike Stump11289f42009-09-09 15:08:12 +0000161
Mike Stump773582d2009-07-23 23:25:26 +0000162 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
163 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000164 TryResult TryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000165 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000166 if (!S->isTypeDependent() && !S->isValueDependent() &&
167 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000168 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000169
170 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000171 }
172
Ted Kremenekb64d1832008-03-13 03:04:22 +0000173 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000174};
Mike Stump31feda52009-07-17 01:31:16 +0000175
Douglas Gregor4619e432008-12-05 23:32:09 +0000176// FIXME: Add support for dependent-sized array types in C++?
177// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000178static VariableArrayType* FindVA(Type* t) {
179 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
180 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
181 if (vat->getSizeExpr())
182 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000183
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000184 t = vt->getElementType().getTypePtr();
185 }
Mike Stump31feda52009-07-17 01:31:16 +0000186
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000187 return 0;
188}
Mike Stump31feda52009-07-17 01:31:16 +0000189
190/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
191/// arbitrary statement. Examples include a single expression or a function
192/// body (compound statement). The ownership of the returned CFG is
193/// transferred to the caller. If CFG construction fails, this method returns
194/// NULL.
Mike Stump0d76d072009-07-20 23:24:15 +0000195CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
196 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000197 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000198 if (!Statement)
199 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000200
Ted Kremenekb64d1832008-03-13 03:04:22 +0000201 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000202
203 // Create an empty block that will serve as the exit block for the CFG. Since
204 // this is the first block added to the CFG, it will be implicitly registered
205 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000206 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000207 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000208 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000209
Ted Kremenek9aae5132007-08-23 21:42:29 +0000210 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000211 CFGBlock* B = addStmt(Statement);
Ted Kremenek8aed4902009-10-20 23:46:25 +0000212 if (!B)
213 B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000214
Ted Kremenek40d87632008-02-27 17:33:02 +0000215 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000216 // Finalize the last constructed block. This usually involves reversing the
217 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000218 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000219
220 // Backpatch the gotos whose label -> block mappings we didn't know when we
221 // encountered them.
222 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000223 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000224
Ted Kremenek9aae5132007-08-23 21:42:29 +0000225 CFGBlock* B = *I;
226 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
227 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
228
229 // If there is no target for the goto, then we are looking at an
230 // incomplete AST. Handle this by not registering a successor.
231 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000232
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000233 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000234 }
Mike Stump31feda52009-07-17 01:31:16 +0000235
Ted Kremenekeda180e22007-08-28 19:26:49 +0000236 // Add successors to the Indirect Goto Dispatch block (if we have one).
237 if (CFGBlock* B = cfg->getIndirectGotoBlock())
238 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
239 E = AddressTakenLabels.end(); I != E; ++I ) {
240
241 // Lookup the target block.
242 LabelMapTy::iterator LI = LabelMap.find(*I);
243
244 // If there is no target block that contains label, then we are looking
245 // at an incomplete AST. Handle this by not registering a successor.
246 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000247
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000248 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000249 }
Mike Stump31feda52009-07-17 01:31:16 +0000250
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000251 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000252 }
Mike Stump31feda52009-07-17 01:31:16 +0000253
254 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000255 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000256
Ted Kremenekab929bb2009-10-20 23:59:28 +0000257 return badCFG ? NULL : cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000258}
Mike Stump31feda52009-07-17 01:31:16 +0000259
Ted Kremenek9aae5132007-08-23 21:42:29 +0000260/// createBlock - Used to lazily create blocks that are connected
261/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000262CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000263 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000264 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000265 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000266 return B;
267}
Mike Stump31feda52009-07-17 01:31:16 +0000268
Ted Kremenek0868eea2009-09-24 18:45:41 +0000269/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek55957a82009-05-02 00:13:27 +0000270bool CFGBuilder::FinishBlock(CFGBlock* B) {
271 if (badCFG)
272 return false;
273
Ted Kremenek93668002009-07-17 22:18:43 +0000274 assert(B);
Ted Kremenek55957a82009-05-02 00:13:27 +0000275 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000276}
277
Ted Kremenek93668002009-07-17 22:18:43 +0000278/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000279/// blocks for ternary operators, &&, and ||. We also process "," and
280/// DeclStmts (which may contain nested control-flow).
Ted Kremenek93668002009-07-17 22:18:43 +0000281CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
282tryAgain:
283 switch (S->getStmtClass()) {
284 default:
285 return VisitStmt(S, alwaysAdd);
286
287 case Stmt::AddrLabelExprClass:
288 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000289
Ted Kremenek93668002009-07-17 22:18:43 +0000290 case Stmt::BinaryOperatorClass:
291 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000292
Ted Kremenek93668002009-07-17 22:18:43 +0000293 case Stmt::BlockExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000294 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000295
296 case Stmt::BlockDeclRefExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000297 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000298
Ted Kremenek93668002009-07-17 22:18:43 +0000299 case Stmt::BreakStmtClass:
300 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000301
Ted Kremenek93668002009-07-17 22:18:43 +0000302 case Stmt::CallExprClass:
303 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000304
Ted Kremenek93668002009-07-17 22:18:43 +0000305 case Stmt::CaseStmtClass:
306 return VisitCaseStmt(cast<CaseStmt>(S));
307
308 case Stmt::ChooseExprClass:
309 return VisitChooseExpr(cast<ChooseExpr>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000310
Ted Kremenek93668002009-07-17 22:18:43 +0000311 case Stmt::CompoundStmtClass:
312 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000313
Ted Kremenek93668002009-07-17 22:18:43 +0000314 case Stmt::ConditionalOperatorClass:
315 return VisitConditionalOperator(cast<ConditionalOperator>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000316
Ted Kremenek93668002009-07-17 22:18:43 +0000317 case Stmt::ContinueStmtClass:
318 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000319
Ted Kremenek93668002009-07-17 22:18:43 +0000320 case Stmt::DeclStmtClass:
321 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000322
Ted Kremenek93668002009-07-17 22:18:43 +0000323 case Stmt::DefaultStmtClass:
324 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000325
Ted Kremenek93668002009-07-17 22:18:43 +0000326 case Stmt::DoStmtClass:
327 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000328
Ted Kremenek93668002009-07-17 22:18:43 +0000329 case Stmt::ForStmtClass:
330 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000331
Ted Kremenek93668002009-07-17 22:18:43 +0000332 case Stmt::GotoStmtClass:
333 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000334
Ted Kremenek93668002009-07-17 22:18:43 +0000335 case Stmt::IfStmtClass:
336 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000337
Ted Kremenek93668002009-07-17 22:18:43 +0000338 case Stmt::IndirectGotoStmtClass:
339 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000340
Ted Kremenek93668002009-07-17 22:18:43 +0000341 case Stmt::LabelStmtClass:
342 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000343
Ted Kremenek93668002009-07-17 22:18:43 +0000344 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000345 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
346
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000347 case Stmt::CXXThrowExprClass:
348 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
349
Ted Kremenek93668002009-07-17 22:18:43 +0000350 case Stmt::ObjCAtSynchronizedStmtClass:
351 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000352
Ted Kremenek93668002009-07-17 22:18:43 +0000353 case Stmt::ObjCAtThrowStmtClass:
354 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000355
Ted Kremenek93668002009-07-17 22:18:43 +0000356 case Stmt::ObjCAtTryStmtClass:
357 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000358
Ted Kremenek93668002009-07-17 22:18:43 +0000359 case Stmt::ObjCForCollectionStmtClass:
360 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000361
Ted Kremenek93668002009-07-17 22:18:43 +0000362 case Stmt::ParenExprClass:
363 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000364 goto tryAgain;
365
Ted Kremenek93668002009-07-17 22:18:43 +0000366 case Stmt::NullStmtClass:
367 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000368
Ted Kremenek93668002009-07-17 22:18:43 +0000369 case Stmt::ReturnStmtClass:
370 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000371
Ted Kremenek93668002009-07-17 22:18:43 +0000372 case Stmt::SizeOfAlignOfExprClass:
Mike Stump11289f42009-09-09 15:08:12 +0000373 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
374
Ted Kremenek93668002009-07-17 22:18:43 +0000375 case Stmt::StmtExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000376 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000377
Ted Kremenek93668002009-07-17 22:18:43 +0000378 case Stmt::SwitchStmtClass:
379 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000380
Ted Kremenek93668002009-07-17 22:18:43 +0000381 case Stmt::WhileStmtClass:
382 return VisitWhileStmt(cast<WhileStmt>(S));
383 }
384}
Mike Stump11289f42009-09-09 15:08:12 +0000385
Ted Kremenek93668002009-07-17 22:18:43 +0000386CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
387 if (alwaysAdd) {
388 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000389 AppendStmt(Block, S);
Mike Stump31feda52009-07-17 01:31:16 +0000390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Ted Kremenek93668002009-07-17 22:18:43 +0000392 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000393}
Mike Stump31feda52009-07-17 01:31:16 +0000394
Ted Kremenek93668002009-07-17 22:18:43 +0000395/// VisitChildren - Visit the children of a Stmt.
396CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
397 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000398 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000399 E = Terminator->child_end(); I != E; ++I) {
400 if (*I) B = Visit(*I);
401 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000402 return B;
403}
Mike Stump11289f42009-09-09 15:08:12 +0000404
Ted Kremenek93668002009-07-17 22:18:43 +0000405CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
406 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000407
Ted Kremenek93668002009-07-17 22:18:43 +0000408 if (alwaysAdd) {
409 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000410 AppendStmt(Block, A);
Ted Kremenek93668002009-07-17 22:18:43 +0000411 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000412
Ted Kremenek9aae5132007-08-23 21:42:29 +0000413 return Block;
414}
Mike Stump11289f42009-09-09 15:08:12 +0000415
Ted Kremenek93668002009-07-17 22:18:43 +0000416CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
417 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000418 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000419 AppendStmt(ConfluenceBlock, B);
Mike Stump11289f42009-09-09 15:08:12 +0000420
Ted Kremenek93668002009-07-17 22:18:43 +0000421 if (!FinishBlock(ConfluenceBlock))
422 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000423
Ted Kremenek93668002009-07-17 22:18:43 +0000424 // create the block evaluating the LHS
425 CFGBlock* LHSBlock = createBlock(false);
426 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000427
Ted Kremenek93668002009-07-17 22:18:43 +0000428 // create the block evaluating the RHS
429 Succ = ConfluenceBlock;
430 Block = NULL;
431 CFGBlock* RHSBlock = addStmt(B->getRHS());
432 if (!FinishBlock(RHSBlock))
433 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000434
Mike Stump773582d2009-07-23 23:25:26 +0000435 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000436 TryResult KnownVal = TryEvaluateBool(B->getLHS());
437 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
438 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000439
Ted Kremenek93668002009-07-17 22:18:43 +0000440 // Now link the LHSBlock with RHSBlock.
441 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000442 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
443 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000444 } else {
Ted Kremenek93668002009-07-17 22:18:43 +0000445 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000446 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
447 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000448 }
Mike Stump11289f42009-09-09 15:08:12 +0000449
Ted Kremenek93668002009-07-17 22:18:43 +0000450 // Generate the blocks for evaluating the LHS.
451 Block = LHSBlock;
452 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000453 }
Ted Kremenek93668002009-07-17 22:18:43 +0000454 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000455 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000456 AppendStmt(Block, B);
Ted Kremenek93668002009-07-17 22:18:43 +0000457 addStmt(B->getRHS());
458 return addStmt(B->getLHS());
459 }
Mike Stump11289f42009-09-09 15:08:12 +0000460
Ted Kremenek93668002009-07-17 22:18:43 +0000461 return VisitStmt(B, alwaysAdd);
462}
463
Ted Kremenek470bfa42009-11-25 01:34:30 +0000464CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, bool alwaysAdd) {
465 if (alwaysAdd) {
466 autoCreateBlock();
467 AppendStmt(Block, E);
468 }
469 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000470}
471
Ted Kremenek0747de62009-07-18 00:47:21 +0000472CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
473 bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000474 // FIXME
475 return NYS();
476}
Mike Stump11289f42009-09-09 15:08:12 +0000477
Ted Kremenek93668002009-07-17 22:18:43 +0000478CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
479 // "break" is a control-flow statement. Thus we stop processing the current
480 // block.
481 if (Block && !FinishBlock(Block))
482 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000483
Ted Kremenek93668002009-07-17 22:18:43 +0000484 // Now create a new block that ends with the break statement.
485 Block = createBlock(false);
486 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000487
Ted Kremenek93668002009-07-17 22:18:43 +0000488 // If there is no target for the break, then we are looking at an incomplete
489 // AST. This means that the CFG cannot be constructed.
490 if (BreakTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000491 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000492 else
493 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000494
495
Ted Kremenek9aae5132007-08-23 21:42:29 +0000496 return Block;
497}
Mike Stump11289f42009-09-09 15:08:12 +0000498
Ted Kremenek93668002009-07-17 22:18:43 +0000499CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
500 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000501 bool NoReturn = false;
502 if (C->getCallee()->getType().getNoReturnAttr()) {
503 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000504 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000505
506 if (FunctionDecl *FD = C->getDirectCallee())
507 if (FD->hasAttr<NoReturnAttr>())
508 NoReturn = true;
509
510 if (!NoReturn)
511 return VisitStmt(C, alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000512
Mike Stump8c5d7992009-07-25 21:26:53 +0000513 if (Block && !FinishBlock(Block))
514 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000515
Mike Stump8c5d7992009-07-25 21:26:53 +0000516 // Create new block with no successor for the remaining pieces.
517 Block = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000518 AppendStmt(Block, C);
Mike Stump8c5d7992009-07-25 21:26:53 +0000519
520 // Wire this to the exit block directly.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000521 AddSuccessor(Block, &cfg->getExit());
Mike Stump11289f42009-09-09 15:08:12 +0000522
Mike Stump8c5d7992009-07-25 21:26:53 +0000523 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000524}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000525
Ted Kremenek21822592009-07-17 18:20:32 +0000526CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
527 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000528 AppendStmt(ConfluenceBlock, C);
Ted Kremenek21822592009-07-17 18:20:32 +0000529 if (!FinishBlock(ConfluenceBlock))
530 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000531
Ted Kremenek21822592009-07-17 18:20:32 +0000532 Succ = ConfluenceBlock;
533 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000534 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000535 if (!FinishBlock(LHSBlock))
536 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000537
Ted Kremenek21822592009-07-17 18:20:32 +0000538 Succ = ConfluenceBlock;
539 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000540 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000541 if (!FinishBlock(RHSBlock))
542 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000543
Ted Kremenek21822592009-07-17 18:20:32 +0000544 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000545 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000546 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000547 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
548 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000549 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000550 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000551}
Mike Stump11289f42009-09-09 15:08:12 +0000552
553
554CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
555 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000556
557 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
558 I != E; ++I ) {
559 LastBlock = addStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000560
Ted Kremenekce499c22009-08-27 23:16:26 +0000561 if (badCFG)
562 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000563 }
Ted Kremenek93668002009-07-17 22:18:43 +0000564 return LastBlock;
565}
Mike Stump11289f42009-09-09 15:08:12 +0000566
Ted Kremenek51d40b02009-07-17 18:15:54 +0000567CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
568 // Create the confluence block that will "merge" the results of the ternary
569 // expression.
570 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000571 AppendStmt(ConfluenceBlock, C);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000572 if (!FinishBlock(ConfluenceBlock))
573 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000574
Ted Kremenek51d40b02009-07-17 18:15:54 +0000575 // Create a block for the LHS expression if there is an LHS expression. A
576 // GCC extension allows LHS to be NULL, causing the condition to be the
577 // value that is returned instead.
578 // e.g: x ?: y is shorthand for: x ? x : y;
579 Succ = ConfluenceBlock;
580 Block = NULL;
581 CFGBlock* LHSBlock = NULL;
582 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000583 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000584 if (!FinishBlock(LHSBlock))
585 return 0;
586 Block = NULL;
587 }
Mike Stump11289f42009-09-09 15:08:12 +0000588
Ted Kremenek51d40b02009-07-17 18:15:54 +0000589 // Create the block for the RHS expression.
590 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000591 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000592 if (!FinishBlock(RHSBlock))
593 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000594
Ted Kremenek51d40b02009-07-17 18:15:54 +0000595 // Create the block that will contain the condition.
596 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000597
Mike Stump773582d2009-07-23 23:25:26 +0000598 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000599 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000600 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000601 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000602 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000603 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000604 // If we know the condition is false, add NULL as the successor for
605 // the block containing the condition. In this case, the confluence
606 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000607 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000608 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000609 } else {
610 // If we have no LHS expression, add the ConfluenceBlock as a direct
611 // successor for the block containing the condition. Moreover, we need to
612 // reverse the order of the predecessors in the ConfluenceBlock because
613 // the RHSBlock will have been added to the succcessors already, and we
614 // want the first predecessor to the the block containing the expression
615 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000616 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000617 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000618 std::reverse(ConfluenceBlock->pred_begin(),
619 ConfluenceBlock->pred_end());
620 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000621 }
Mike Stump11289f42009-09-09 15:08:12 +0000622
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000623 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000624 Block->setTerminator(C);
625 return addStmt(C->getCond());
626}
627
Ted Kremenek93668002009-07-17 22:18:43 +0000628CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
629 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000630
Ted Kremenek93668002009-07-17 22:18:43 +0000631 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000632 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000633 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000634 }
Mike Stump11289f42009-09-09 15:08:12 +0000635
Ted Kremenek93668002009-07-17 22:18:43 +0000636 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000637
Ted Kremenek93668002009-07-17 22:18:43 +0000638 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
639 typedef llvm::SmallVector<Decl*,10> BufTy;
640 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000641
Ted Kremenek93668002009-07-17 22:18:43 +0000642 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
643 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
644 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
645 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000646
Ted Kremenek93668002009-07-17 22:18:43 +0000647 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
648 // automatically freed with the CFG.
649 DeclGroupRef DG(*I);
650 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000651 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000652 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000653
Ted Kremenek93668002009-07-17 22:18:43 +0000654 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000655 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000656 B = VisitDeclSubExpr(D);
657 }
Mike Stump11289f42009-09-09 15:08:12 +0000658
659 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000660}
Mike Stump11289f42009-09-09 15:08:12 +0000661
Ted Kremenek93668002009-07-17 22:18:43 +0000662/// VisitDeclSubExpr - Utility method to add block-level expressions for
663/// initializers in Decls.
664CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
665 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000666
Ted Kremenek93668002009-07-17 22:18:43 +0000667 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000668
Ted Kremenek93668002009-07-17 22:18:43 +0000669 if (!VD)
670 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000671
Ted Kremenek93668002009-07-17 22:18:43 +0000672 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000673
Ted Kremenek93668002009-07-17 22:18:43 +0000674 if (Init) {
675 // Optimization: Don't create separate block-level statements for literals.
676 switch (Init->getStmtClass()) {
677 case Stmt::IntegerLiteralClass:
678 case Stmt::CharacterLiteralClass:
679 case Stmt::StringLiteralClass:
680 break;
681 default:
682 Block = addStmt(Init);
683 }
684 }
Mike Stump11289f42009-09-09 15:08:12 +0000685
Ted Kremenek93668002009-07-17 22:18:43 +0000686 // If the type of VD is a VLA, then we must process its size expressions.
687 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
688 VA = FindVA(VA->getElementType().getTypePtr()))
689 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000690
Ted Kremenek93668002009-07-17 22:18:43 +0000691 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000692}
693
694CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000695 // We may see an if statement in the middle of a basic block, or it may be the
696 // first statement we are processing. In either case, we create a new basic
697 // block. First, we create the blocks for the then...else statements, and
698 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000699 // middle of a block, we stop processing that block. That block is then the
700 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000701
702 // The block we were proccessing is now finished. Make it the successor
703 // block.
704 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000705 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000706 if (!FinishBlock(Block))
707 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000708 }
Mike Stump31feda52009-07-17 01:31:16 +0000709
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000710 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000711 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000712
Ted Kremenek9aae5132007-08-23 21:42:29 +0000713 if (Stmt* Else = I->getElse()) {
714 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000715
Ted Kremenek9aae5132007-08-23 21:42:29 +0000716 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000717 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000718 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000719 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000720
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000721 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
722 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000723 else if (Block) {
724 if (!FinishBlock(ElseBlock))
725 return 0;
726 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000727 }
Mike Stump31feda52009-07-17 01:31:16 +0000728
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000729 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000730 CFGBlock* ThenBlock;
731 {
732 Stmt* Then = I->getThen();
733 assert (Then);
734 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000735 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000736 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000737
Ted Kremenek1b379512009-04-01 03:52:47 +0000738 if (!ThenBlock) {
739 // We can reach here if the "then" body has all NullStmts.
740 // Create an empty block so we can distinguish between true and false
741 // branches in path-sensitive analyses.
742 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000743 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000744 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000745 if (!FinishBlock(ThenBlock))
746 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000747 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000748 }
749
Mike Stump31feda52009-07-17 01:31:16 +0000750 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000751 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000752
Ted Kremenek9aae5132007-08-23 21:42:29 +0000753 // Set the terminator of the new block to the If statement.
754 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000755
Mike Stump773582d2009-07-23 23:25:26 +0000756 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000757 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000758
Ted Kremenek9aae5132007-08-23 21:42:29 +0000759 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000760 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
761 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000762
763 // Add the condition as the last statement in the new block. This may create
764 // new blocks as the condition may contain control-flow. Any newly created
765 // blocks will be pointed to be "Block".
Ted Kremenek93668002009-07-17 22:18:43 +0000766 return addStmt(I->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000767}
Mike Stump31feda52009-07-17 01:31:16 +0000768
769
Ted Kremenek9aae5132007-08-23 21:42:29 +0000770CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000771 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000772 //
Mike Stump31feda52009-07-17 01:31:16 +0000773 // NOTE: If a "return" appears in the middle of a block, this means that the
774 // code afterwards is DEAD (unreachable). We still keep a basic block
775 // for that code; a simple "mark-and-sweep" from the entry block will be
776 // able to report such dead blocks.
Ted Kremenek0868eea2009-09-24 18:45:41 +0000777 if (Block)
778 FinishBlock(Block);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000779
780 // Create the new block.
781 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000782
Ted Kremenek9aae5132007-08-23 21:42:29 +0000783 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000784 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000785
786 // Add the return statement to the block. This may create new blocks if R
787 // contains control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +0000788 return VisitStmt(R, true);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000789}
790
791CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
792 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000793 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000794 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000795
Ted Kremenek93668002009-07-17 22:18:43 +0000796 if (!LabelBlock) // This can happen when the body is empty, i.e.
797 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000798
Ted Kremenek93668002009-07-17 22:18:43 +0000799 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000800 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000801
802 // Labels partition blocks, so this is the end of the basic block we were
803 // processing (L is the block's label). Because this is label (and we have
804 // already processed the substatement) there is no extra control-flow to worry
805 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000806 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000807 if (!FinishBlock(LabelBlock))
808 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000809
810 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000811 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000812
Ted Kremenek9aae5132007-08-23 21:42:29 +0000813 // This block is now the implicit successor of other blocks.
814 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000815
Ted Kremenek9aae5132007-08-23 21:42:29 +0000816 return LabelBlock;
817}
818
819CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000820 // Goto is a control-flow statement. Thus we stop processing the current
821 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000822 if (Block)
823 FinishBlock(Block);
824
Ted Kremenek9aae5132007-08-23 21:42:29 +0000825 Block = createBlock(false);
826 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000827
828 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000829 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000830
Ted Kremenek9aae5132007-08-23 21:42:29 +0000831 if (I == LabelMap.end())
832 // We will need to backpatch this block later.
833 BackpatchBlocks.push_back(Block);
834 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000835 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000836
Mike Stump31feda52009-07-17 01:31:16 +0000837 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000838}
839
840CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000841 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000842
Mike Stump014b3ea2009-07-21 01:12:51 +0000843 // "for" is a control-flow statement. Thus we stop processing the current
844 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000845 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000846 if (!FinishBlock(Block))
847 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000848 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000849 } else
850 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000851
852 // Because of short-circuit evaluation, the condition of the loop can span
853 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
854 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000855 CFGBlock* ExitConditionBlock = createBlock(false);
856 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000857
Ted Kremenek81e14852007-08-27 19:46:09 +0000858 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000859 ExitConditionBlock->setTerminator(F);
860
861 // Now add the actual condition to the condition block. Because the condition
862 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000863 if (Stmt* C = F->getCond()) {
864 Block = ExitConditionBlock;
865 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000866 if (Block) {
867 if (!FinishBlock(EntryConditionBlock))
868 return 0;
869 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000870 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000871
Mike Stump31feda52009-07-17 01:31:16 +0000872 // The condition block is the implicit successor for the loop body as well as
873 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000874 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000875
Mike Stump773582d2009-07-23 23:25:26 +0000876 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000877 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +0000878
Mike Stump773582d2009-07-23 23:25:26 +0000879 if (F->getCond())
880 KnownVal = TryEvaluateBool(F->getCond());
881
Ted Kremenek9aae5132007-08-23 21:42:29 +0000882 // Now create the loop body.
883 {
884 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000885
Ted Kremenek9aae5132007-08-23 21:42:29 +0000886 // Save the current values for Block, Succ, and continue and break targets
887 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
888 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000889 save_break(BreakTargetBlock);
890
Ted Kremeneke9610502007-08-30 18:39:40 +0000891 // Create a new block to contain the (bottom) of the loop body.
892 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000893
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000894 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000895 // Generate increment code in its own basic block. This is the target of
896 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000897 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000898 } else {
899 // No increment code. Create a special, empty, block that is used as the
900 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000901 assert(Succ == EntryConditionBlock);
902 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000903 }
Mike Stump31feda52009-07-17 01:31:16 +0000904
Ted Kremenek902393b2009-04-28 00:51:56 +0000905 // Finish up the increment (or empty) block if it hasn't been already.
906 if (Block) {
907 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000908 if (!FinishBlock(Block))
909 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000910 Block = 0;
911 }
Mike Stump31feda52009-07-17 01:31:16 +0000912
Ted Kremenek902393b2009-04-28 00:51:56 +0000913 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000914
Ted Kremenek902393b2009-04-28 00:51:56 +0000915 // The starting block for the loop increment is the block that should
916 // represent the 'loop target' for looping back to the start of the loop.
917 ContinueTargetBlock->setLoopTarget(F);
918
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000919 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000920 BreakTargetBlock = LoopSuccessor;
921
922 // Now populate the body block, and in the process create new blocks as we
923 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000924 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000925
926 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +0000927 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +0000928 else if (Block && !FinishBlock(BodyBlock))
929 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000930
Ted Kremenek30754282009-07-24 04:47:11 +0000931 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000932 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000933 }
Mike Stump31feda52009-07-17 01:31:16 +0000934
Ted Kremenek30754282009-07-24 04:47:11 +0000935 // Link up the condition block with the code that follows the loop. (the
936 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000937 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000938
Ted Kremenek9aae5132007-08-23 21:42:29 +0000939 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000940 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000941 if (Stmt* I = F->getInit()) {
942 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000943 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000944 } else {
945 // There is no loop initialization. We are thus basically a while loop.
946 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000947 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000948 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000949 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000950 }
951}
952
Ted Kremenek9d56e642008-11-11 17:10:00 +0000953CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
954 // Objective-C fast enumeration 'for' statements:
955 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
956 //
957 // for ( Type newVariable in collection_expression ) { statements }
958 //
959 // becomes:
960 //
961 // prologue:
962 // 1. collection_expression
963 // T. jump to loop_entry
964 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000965 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +0000966 // 1. ObjCForCollectionStmt [performs binding to newVariable]
967 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
968 // TB:
969 // statements
970 // T. jump to loop_entry
971 // FB:
972 // what comes after
973 //
974 // and
975 //
976 // Type existingItem;
977 // for ( existingItem in expression ) { statements }
978 //
979 // becomes:
980 //
Mike Stump31feda52009-07-17 01:31:16 +0000981 // the same with newVariable replaced with existingItem; the binding works
982 // the same except that for one ObjCForCollectionStmt::getElement() returns
983 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +0000984 //
Mike Stump31feda52009-07-17 01:31:16 +0000985
Ted Kremenek9d56e642008-11-11 17:10:00 +0000986 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +0000987
Ted Kremenek9d56e642008-11-11 17:10:00 +0000988 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000989 if (!FinishBlock(Block))
990 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +0000991 LoopSuccessor = Block;
992 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +0000993 } else
994 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000995
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000996 // Build the condition blocks.
997 CFGBlock* ExitConditionBlock = createBlock(false);
998 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000999
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001000 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001001 ExitConditionBlock->setTerminator(S);
1002
1003 // The last statement in the block should be the ObjCForCollectionStmt, which
1004 // performs the actual binding to 'element' and determines if there are any
1005 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001006 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001007 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001008
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001009 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001010 // generate new blocks as necesary. We DON'T add the statement by default to
1011 // the CFG unless it contains control-flow.
Ted Kremenek93668002009-07-17 22:18:43 +00001012 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump31feda52009-07-17 01:31:16 +00001013 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001014 if (!FinishBlock(EntryConditionBlock))
1015 return 0;
1016 Block = 0;
1017 }
Mike Stump31feda52009-07-17 01:31:16 +00001018
1019 // The condition block is the implicit successor for the loop body as well as
1020 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001021 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001022
Ted Kremenek9d56e642008-11-11 17:10:00 +00001023 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001024 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001025 // Save the current values for Succ, continue and break targets.
1026 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001027 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1028
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001029 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001030 ContinueTargetBlock = EntryConditionBlock;
1031
Ted Kremenek93668002009-07-17 22:18:43 +00001032 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001033
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001034 if (!BodyBlock)
1035 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001036 else if (Block) {
1037 if (!FinishBlock(BodyBlock))
1038 return 0;
1039 }
Mike Stump31feda52009-07-17 01:31:16 +00001040
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001041 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001042 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001043 }
Mike Stump31feda52009-07-17 01:31:16 +00001044
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001045 // Link up the condition block with the code that follows the loop.
1046 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001047 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001048
Ted Kremenek9d56e642008-11-11 17:10:00 +00001049 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001050 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001051 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001052}
1053
Ted Kremenek49805452009-05-02 01:49:13 +00001054CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1055 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001056
Ted Kremenek49805452009-05-02 01:49:13 +00001057 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001058 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001059
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001060 // The sync body starts its own basic block. This makes it a little easier
1061 // for diagnostic clients.
1062 if (SyncBlock) {
1063 if (!FinishBlock(SyncBlock))
1064 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001065
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001066 Block = 0;
1067 }
Mike Stump31feda52009-07-17 01:31:16 +00001068
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001069 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001070
Ted Kremenek49805452009-05-02 01:49:13 +00001071 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001072 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001073}
Mike Stump31feda52009-07-17 01:31:16 +00001074
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001075CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001076 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001077 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001078}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001079
Ted Kremenek9aae5132007-08-23 21:42:29 +00001080CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001081 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001082
Mike Stump014b3ea2009-07-21 01:12:51 +00001083 // "while" is a control-flow statement. Thus we stop processing the current
1084 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001085 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001086 if (!FinishBlock(Block))
1087 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001088 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001089 } else
1090 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001091
1092 // Because of short-circuit evaluation, the condition of the loop can span
1093 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1094 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001095 CFGBlock* ExitConditionBlock = createBlock(false);
1096 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001097
Ted Kremenek81e14852007-08-27 19:46:09 +00001098 // Set the terminator for the "exit" condition block.
1099 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001100
1101 // Now add the actual condition to the condition block. Because the condition
1102 // itself may contain control-flow, new blocks may be created. Thus we update
1103 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001104 if (Stmt* C = W->getCond()) {
1105 Block = ExitConditionBlock;
1106 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001107 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +00001108 if (Block) {
1109 if (!FinishBlock(EntryConditionBlock))
1110 return 0;
1111 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001112 }
Mike Stump31feda52009-07-17 01:31:16 +00001113
1114 // The condition block is the implicit successor for the loop body as well as
1115 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001116 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001117
Mike Stump773582d2009-07-23 23:25:26 +00001118 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001119 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001120
Ted Kremenek9aae5132007-08-23 21:42:29 +00001121 // Process the loop body.
1122 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001123 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001124
1125 // Save the current values for Block, Succ, and continue and break targets
1126 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1127 save_continue(ContinueTargetBlock),
1128 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001129
Mike Stump31feda52009-07-17 01:31:16 +00001130 // Create an empty block to represent the transition block for looping back
1131 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001132 Block = 0;
1133 assert(Succ == EntryConditionBlock);
1134 Succ = createBlock();
1135 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001136 ContinueTargetBlock = Succ;
1137
Ted Kremenek9aae5132007-08-23 21:42:29 +00001138 // All breaks should go to the code following the loop.
1139 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001140
Ted Kremenek9aae5132007-08-23 21:42:29 +00001141 // NULL out Block to force lazy instantiation of blocks for the body.
1142 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001143
Ted Kremenek9aae5132007-08-23 21:42:29 +00001144 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001145 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001146
Ted Kremeneke9610502007-08-30 18:39:40 +00001147 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001148 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001149 else if (Block) {
1150 if (!FinishBlock(BodyBlock))
1151 return 0;
1152 }
Mike Stump31feda52009-07-17 01:31:16 +00001153
Ted Kremenek30754282009-07-24 04:47:11 +00001154 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001155 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001156 }
Mike Stump31feda52009-07-17 01:31:16 +00001157
Ted Kremenek30754282009-07-24 04:47:11 +00001158 // Link up the condition block with the code that follows the loop. (the
1159 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001160 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001161
1162 // There can be no more statements in the condition block since we loop back
1163 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001164 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001165
Ted Kremenek9aae5132007-08-23 21:42:29 +00001166 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001167 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001168 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001169}
Mike Stump11289f42009-09-09 15:08:12 +00001170
1171
Ted Kremenek93668002009-07-17 22:18:43 +00001172CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1173 // FIXME: For now we pretend that @catch and the code it contains does not
1174 // exit.
1175 return Block;
1176}
Mike Stump31feda52009-07-17 01:31:16 +00001177
Ted Kremenek93041ba2008-12-09 20:20:09 +00001178CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1179 // FIXME: This isn't complete. We basically treat @throw like a return
1180 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001181
Ted Kremenek0868eea2009-09-24 18:45:41 +00001182 // If we were in the middle of a block we stop processing that block.
Ted Kremenek93668002009-07-17 22:18:43 +00001183 if (Block && !FinishBlock(Block))
1184 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001185
Ted Kremenek93041ba2008-12-09 20:20:09 +00001186 // Create the new block.
1187 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001188
Ted Kremenek93041ba2008-12-09 20:20:09 +00001189 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001190 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001191
1192 // Add the statement to the block. This may create new blocks if S contains
1193 // control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +00001194 return VisitStmt(S, true);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001195}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001196
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001197CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001198 // If we were in the middle of a block we stop processing that block.
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001199 if (Block && !FinishBlock(Block))
1200 return 0;
1201
1202 // Create the new block.
1203 Block = createBlock(false);
1204
1205 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001206 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001207
1208 // Add the statement to the block. This may create new blocks if S contains
1209 // control-flow (short-circuit operations).
1210 return VisitStmt(T, true);
1211}
1212
Ted Kremenek93668002009-07-17 22:18:43 +00001213CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001214 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001215
Mike Stump8d50b6a2009-07-21 01:27:50 +00001216 // "do...while" is a control-flow statement. Thus we stop processing the
1217 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001218 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001219 if (!FinishBlock(Block))
1220 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001221 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001222 } else
1223 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001224
1225 // Because of short-circuit evaluation, the condition of the loop can span
1226 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1227 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001228 CFGBlock* ExitConditionBlock = createBlock(false);
1229 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001230
Ted Kremenek81e14852007-08-27 19:46:09 +00001231 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001232 ExitConditionBlock->setTerminator(D);
1233
1234 // Now add the actual condition to the condition block. Because the condition
1235 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001236 if (Stmt* C = D->getCond()) {
1237 Block = ExitConditionBlock;
1238 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001239 if (Block) {
1240 if (!FinishBlock(EntryConditionBlock))
1241 return 0;
1242 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001243 }
Mike Stump31feda52009-07-17 01:31:16 +00001244
Ted Kremeneka1523a32008-02-27 07:20:00 +00001245 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001246 Succ = EntryConditionBlock;
1247
Mike Stump773582d2009-07-23 23:25:26 +00001248 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001249 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001250
Ted Kremenek9aae5132007-08-23 21:42:29 +00001251 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001252 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001253 {
1254 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001255
Ted Kremenek9aae5132007-08-23 21:42:29 +00001256 // Save the current values for Block, Succ, and continue and break targets
1257 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1258 save_continue(ContinueTargetBlock),
1259 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001260
Ted Kremenek9aae5132007-08-23 21:42:29 +00001261 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001262 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001263
Ted Kremenek9aae5132007-08-23 21:42:29 +00001264 // All breaks should go to the code following the loop.
1265 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001266
Ted Kremenek9aae5132007-08-23 21:42:29 +00001267 // NULL out Block to force lazy instantiation of blocks for the body.
1268 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001269
Ted Kremenek9aae5132007-08-23 21:42:29 +00001270 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001271 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001272
Ted Kremeneke9610502007-08-30 18:39:40 +00001273 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001274 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001275 else if (Block) {
1276 if (!FinishBlock(BodyBlock))
1277 return 0;
1278 }
Mike Stump31feda52009-07-17 01:31:16 +00001279
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001280 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001281 // ExitConditionBlock to represent the "loop back" transition. Create an
1282 // empty block to represent the transition block for looping back to the
1283 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001284 // FIXME: Can we do this more efficiently without adding another block?
1285 Block = NULL;
1286 Succ = BodyBlock;
1287 CFGBlock *LoopBackBlock = createBlock();
1288 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001289
Ted Kremenek30754282009-07-24 04:47:11 +00001290 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001291 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001292 }
Mike Stump31feda52009-07-17 01:31:16 +00001293
Ted Kremenek30754282009-07-24 04:47:11 +00001294 // Link up the condition block with the code that follows the loop.
1295 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001296 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001297
1298 // There can be no more statements in the body block(s) since we loop back to
1299 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001300 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001301
Ted Kremenek9aae5132007-08-23 21:42:29 +00001302 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001303 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001304 return BodyBlock;
1305}
1306
1307CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1308 // "continue" is a control-flow statement. Thus we stop processing the
1309 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001310 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001311 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001312
Ted Kremenek9aae5132007-08-23 21:42:29 +00001313 // Now create a new block that ends with the continue statement.
1314 Block = createBlock(false);
1315 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001316
Ted Kremenek9aae5132007-08-23 21:42:29 +00001317 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001318 // incomplete AST. This means the CFG cannot be constructed.
1319 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001320 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001321 else
1322 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001323
Ted Kremenek9aae5132007-08-23 21:42:29 +00001324 return Block;
1325}
Mike Stump11289f42009-09-09 15:08:12 +00001326
Ted Kremenek0747de62009-07-18 00:47:21 +00001327CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1328 bool alwaysAdd) {
1329
1330 if (alwaysAdd) {
1331 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001332 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001333 }
Mike Stump11289f42009-09-09 15:08:12 +00001334
Ted Kremenek93668002009-07-17 22:18:43 +00001335 // VLA types have expressions that must be evaluated.
1336 if (E->isArgumentType()) {
1337 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1338 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1339 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001340 }
Mike Stump11289f42009-09-09 15:08:12 +00001341
Mike Stump31feda52009-07-17 01:31:16 +00001342 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001343}
Mike Stump11289f42009-09-09 15:08:12 +00001344
Ted Kremenek93668002009-07-17 22:18:43 +00001345/// VisitStmtExpr - Utility method to handle (nested) statement
1346/// expressions (a GCC extension).
Ted Kremenek0747de62009-07-18 00:47:21 +00001347CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1348 if (alwaysAdd) {
1349 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001350 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001351 }
Ted Kremenek93668002009-07-17 22:18:43 +00001352 return VisitCompoundStmt(SE->getSubStmt());
1353}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001354
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001355CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001356 // "switch" is a control-flow statement. Thus we stop processing the current
1357 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001358 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001359
Ted Kremenek9aae5132007-08-23 21:42:29 +00001360 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001361 if (!FinishBlock(Block))
1362 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001363 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001364 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001365
1366 // Save the current "switch" context.
1367 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001368 save_break(BreakTargetBlock),
1369 save_default(DefaultCaseBlock);
1370
Mike Stump31feda52009-07-17 01:31:16 +00001371 // Set the "default" case to be the block after the switch statement. If the
1372 // switch statement contains a "default:", this value will be overwritten with
1373 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001374 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001375
Ted Kremenek9aae5132007-08-23 21:42:29 +00001376 // Create a new block that will contain the switch statement.
1377 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001378
Ted Kremenek9aae5132007-08-23 21:42:29 +00001379 // Now process the switch body. The code after the switch is the implicit
1380 // successor.
1381 Succ = SwitchSuccessor;
1382 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001383
1384 // When visiting the body, the case statements should automatically get linked
1385 // up to the switch. We also don't keep a pointer to the body, since all
1386 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001387 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001388 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001389 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001390 if (Block) {
1391 if (!FinishBlock(BodyBlock))
1392 return 0;
1393 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001394
Mike Stump31feda52009-07-17 01:31:16 +00001395 // If we have no "default:" case, the default transition is to the code
1396 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001397 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001398
Ted Kremenek81e14852007-08-27 19:46:09 +00001399 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001400 SwitchTerminatedBlock->setTerminator(Terminator);
1401 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001402 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001403
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001404 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001405}
1406
Ted Kremenek93668002009-07-17 22:18:43 +00001407CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001408 // CaseStmts are essentially labels, so they are the first statement in a
1409 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001410
Ted Kremenek93668002009-07-17 22:18:43 +00001411 if (CS->getSubStmt())
1412 addStmt(CS->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001413
Ted Kremenek55e91e82007-08-30 18:48:11 +00001414 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001415 if (!CaseBlock)
1416 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001417
1418 // Cases statements partition blocks, so this is the top of the basic block we
1419 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001420 CaseBlock->setLabel(CS);
1421
Ted Kremenek55957a82009-05-02 00:13:27 +00001422 if (!FinishBlock(CaseBlock))
1423 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001424
1425 // Add this block to the list of successors for the block with the switch
1426 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001427 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001428 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001429
Ted Kremenek9aae5132007-08-23 21:42:29 +00001430 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1431 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001432
Ted Kremenek9aae5132007-08-23 21:42:29 +00001433 // This block is now the implicit successor of other blocks.
1434 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001435
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001436 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001437}
Mike Stump31feda52009-07-17 01:31:16 +00001438
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001439CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001440 if (Terminator->getSubStmt())
1441 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001442
Ted Kremenek654c78f2008-02-13 22:05:39 +00001443 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001444
1445 if (!DefaultCaseBlock)
1446 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001447
1448 // Default statements partition blocks, so this is the top of the basic block
1449 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001450 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001451
Ted Kremenek55957a82009-05-02 00:13:27 +00001452 if (!FinishBlock(DefaultCaseBlock))
1453 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001454
Mike Stump31feda52009-07-17 01:31:16 +00001455 // Unlike case statements, we don't add the default block to the successors
1456 // for the switch statement immediately. This is done when we finish
1457 // processing the switch statement. This allows for the default case
1458 // (including a fall-through to the code after the switch statement) to always
1459 // be the last successor of a switch-terminated block.
1460
Ted Kremenek654c78f2008-02-13 22:05:39 +00001461 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1462 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001463
Ted Kremenek654c78f2008-02-13 22:05:39 +00001464 // This block is now the implicit successor of other blocks.
1465 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001466
1467 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001468}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001469
Ted Kremenekeda180e22007-08-28 19:26:49 +00001470CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001471 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001472 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001473
Ted Kremenekeda180e22007-08-28 19:26:49 +00001474 if (!IBlock) {
1475 IBlock = createBlock(false);
1476 cfg->setIndirectGotoBlock(IBlock);
1477 }
Mike Stump31feda52009-07-17 01:31:16 +00001478
Ted Kremenekeda180e22007-08-28 19:26:49 +00001479 // IndirectGoto is a control-flow statement. Thus we stop processing the
1480 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001481 if (Block && !FinishBlock(Block))
1482 return 0;
1483
Ted Kremenekeda180e22007-08-28 19:26:49 +00001484 Block = createBlock(false);
1485 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001486 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001487 return addStmt(I->getTarget());
1488}
1489
Ted Kremenek04cca642007-08-23 21:26:19 +00001490} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001491
Mike Stump31feda52009-07-17 01:31:16 +00001492/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1493/// no successors or predecessors. If this is the first block created in the
1494/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001495CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001496 bool first_block = begin() == end();
1497
1498 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001499 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1500 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1501 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001502
1503 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001504 if (first_block)
1505 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001506
1507 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001508 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001509}
1510
Ted Kremenek889073f2007-08-23 16:51:22 +00001511/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1512/// CFG is returned to the caller.
Mike Stump0d76d072009-07-20 23:24:15 +00001513CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001514 CFGBuilder Builder;
Mike Stump0d76d072009-07-20 23:24:15 +00001515 return Builder.buildCFG(Statement, C);
Ted Kremenek889073f2007-08-23 16:51:22 +00001516}
1517
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001518//===----------------------------------------------------------------------===//
1519// CFG: Queries for BlkExprs.
1520//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001521
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001522namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001523 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001524}
1525
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001526static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1527 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001528 return;
Mike Stump31feda52009-07-17 01:31:16 +00001529
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001530 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001531 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001532
Ted Kremenek95a123c2008-01-26 00:03:27 +00001533 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1534 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001535
Ted Kremenek95a123c2008-01-26 00:03:27 +00001536 FindSubExprAssignments(*I, Set);
1537 }
1538}
1539
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001540static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1541 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001542
1543 // Look for assignments that are used as subexpressions. These are the only
1544 // assignments that we want to *possibly* register as a block-level
1545 // expression. Basically, if an assignment occurs both in a subexpression and
1546 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001547 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001548
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001549 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001550 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001551 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001552
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001553 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001554
1555 // Iterate over the statements again on identify the Expr* and Stmt* at the
1556 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001557
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001558 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001559 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001560
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001561 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001562 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001563 // expression are really "statements" whose value is never used by
1564 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001565 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001566 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001567 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1568 // Special handling for statement expressions. The last statement in
1569 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001570 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001571 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001572 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001573 (*M)[C->body_back()] = x;
1574 }
1575 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001576
Ted Kremenek95a123c2008-01-26 00:03:27 +00001577 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001578 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001579 }
Mike Stump31feda52009-07-17 01:31:16 +00001580
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001581 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001582
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001583 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001584
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001585 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001586 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001587 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001588 }
1589 }
Mike Stump31feda52009-07-17 01:31:16 +00001590
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001591 return M;
1592}
1593
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001594CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1595 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001596 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001597
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001598 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001599 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001600
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001601 if (I == M->end()) return CFG::BlkExprNumTy();
1602 else return CFG::BlkExprNumTy(I->second);
1603}
1604
1605unsigned CFG::getNumBlkExprs() {
1606 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1607 return M->size();
1608 else {
1609 // We assume callers interested in the number of BlkExprs will want
1610 // the map constructed if it doesn't already exist.
1611 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1612 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1613 }
1614}
1615
Ted Kremenek6065ef62008-04-28 18:00:46 +00001616//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001617// Cleanup: CFG dstor.
1618//===----------------------------------------------------------------------===//
1619
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001620CFG::~CFG() {
1621 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1622}
Mike Stump31feda52009-07-17 01:31:16 +00001623
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001624//===----------------------------------------------------------------------===//
1625// CFG pretty printing
1626//===----------------------------------------------------------------------===//
1627
Ted Kremenek7e776b12007-08-22 18:22:34 +00001628namespace {
1629
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001630class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001631
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001632 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1633 StmtMapTy StmtMap;
1634 signed CurrentBlock;
1635 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001636 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001637public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001638
Chris Lattnerc61089a2009-06-30 01:26:17 +00001639 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1640 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001641 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1642 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001643 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001644 BI != BEnd; ++BI, ++j )
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001645 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001646 }
1647 }
Mike Stump31feda52009-07-17 01:31:16 +00001648
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001649 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001650
Chris Lattnerc61089a2009-06-30 01:26:17 +00001651 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001652 void setBlockID(signed i) { CurrentBlock = i; }
1653 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001654
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001655 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001656
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001657 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001658
1659 if (I == StmtMap.end())
1660 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001661
1662 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001663 && I->second.second == CurrentStmt)
1664 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001665
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001666 OS << "[B" << I->second.first << "." << I->second.second << "]";
1667 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001668 }
1669};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001670} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001671
Chris Lattnerc61089a2009-06-30 01:26:17 +00001672
1673namespace {
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001674class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1675 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001676
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001677 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001678 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001679 PrintingPolicy Policy;
1680
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001681public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001682 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001683 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001684 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001685
Ted Kremenek9aae5132007-08-23 21:42:29 +00001686 void VisitIfStmt(IfStmt* I) {
1687 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001688 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001689 }
Mike Stump31feda52009-07-17 01:31:16 +00001690
Ted Kremenek9aae5132007-08-23 21:42:29 +00001691 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001692 void VisitStmt(Stmt* Terminator) {
1693 Terminator->printPretty(OS, Helper, Policy);
1694 }
1695
Ted Kremenek9aae5132007-08-23 21:42:29 +00001696 void VisitForStmt(ForStmt* F) {
1697 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001698 if (F->getInit()) OS << "...";
1699 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001700 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001701 OS << "; ";
1702 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001703 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001704 }
Mike Stump31feda52009-07-17 01:31:16 +00001705
Ted Kremenek9aae5132007-08-23 21:42:29 +00001706 void VisitWhileStmt(WhileStmt* W) {
1707 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001708 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001709 }
Mike Stump31feda52009-07-17 01:31:16 +00001710
Ted Kremenek9aae5132007-08-23 21:42:29 +00001711 void VisitDoStmt(DoStmt* D) {
1712 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001713 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001714 }
Mike Stump31feda52009-07-17 01:31:16 +00001715
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001716 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001717 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001718 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001719 }
Mike Stump31feda52009-07-17 01:31:16 +00001720
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001721 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001722 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001723 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001724 }
Mike Stump31feda52009-07-17 01:31:16 +00001725
Ted Kremenek391f94a2007-08-31 22:29:13 +00001726 void VisitChooseExpr(ChooseExpr* C) {
1727 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001728 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001729 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001730 }
Mike Stump31feda52009-07-17 01:31:16 +00001731
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001732 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1733 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001734 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001735 }
Mike Stump31feda52009-07-17 01:31:16 +00001736
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001737 void VisitBinaryOperator(BinaryOperator* B) {
1738 if (!B->isLogicalOp()) {
1739 VisitExpr(B);
1740 return;
1741 }
Mike Stump31feda52009-07-17 01:31:16 +00001742
Douglas Gregor7de59662009-05-29 20:38:28 +00001743 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001744
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001745 switch (B->getOpcode()) {
1746 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001747 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001748 return;
1749 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001750 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001751 return;
1752 default:
1753 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001754 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001755 }
Mike Stump31feda52009-07-17 01:31:16 +00001756
Ted Kremenek12687ff2007-08-27 21:54:41 +00001757 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001758 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001759 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001760};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001761} // end anonymous namespace
1762
Mike Stump31feda52009-07-17 01:31:16 +00001763
Chris Lattnerc61089a2009-06-30 01:26:17 +00001764static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1765 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001766 if (Helper) {
1767 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001768 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001769 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001770
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001771 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001772 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001773 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001774 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001775 return;
1776 }
1777 }
Mike Stump31feda52009-07-17 01:31:16 +00001778
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001779 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001780 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001781 if (B->getOpcode() == BinaryOperator::Comma) {
1782 OS << "... , ";
1783 Helper->handledStmt(B->getRHS(),OS);
1784 OS << '\n';
1785 return;
Mike Stump31feda52009-07-17 01:31:16 +00001786 }
1787 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001788 }
Mike Stump31feda52009-07-17 01:31:16 +00001789
Chris Lattnerc61089a2009-06-30 01:26:17 +00001790 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001791
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001792 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001793 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001794}
Mike Stump31feda52009-07-17 01:31:16 +00001795
Chris Lattnerc61089a2009-06-30 01:26:17 +00001796static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1797 const CFGBlock& B,
1798 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001799
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001800 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001801
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001802 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001803 OS << "\n [ B" << B.getBlockID();
1804
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001805 if (&B == &cfg->getEntry())
1806 OS << " (ENTRY) ]\n";
1807 else if (&B == &cfg->getExit())
1808 OS << " (EXIT) ]\n";
1809 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001810 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001811 else
1812 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001813
Ted Kremenek71eca012007-08-29 23:20:49 +00001814 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001815 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001816
1817 if (print_edges)
1818 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001819
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001820 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001821 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001822 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001823 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001824 C->getLHS()->printPretty(OS, Helper,
1825 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001826 if (C->getRHS()) {
1827 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001828 C->getRHS()->printPretty(OS, Helper,
1829 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001830 }
Mike Stump31feda52009-07-17 01:31:16 +00001831 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001832 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001833 else
1834 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001835
Ted Kremenek71eca012007-08-29 23:20:49 +00001836 OS << ":\n";
1837 }
Mike Stump31feda52009-07-17 01:31:16 +00001838
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001839 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001840 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001841
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001842 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1843 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001844
Ted Kremenek71eca012007-08-29 23:20:49 +00001845 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001846 if (print_edges)
1847 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001848
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001849 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001850
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001851 if (Helper)
1852 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001853
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001854 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001855 }
Mike Stump31feda52009-07-17 01:31:16 +00001856
Ted Kremenek71eca012007-08-29 23:20:49 +00001857 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001858 if (B.getTerminator()) {
1859 if (print_edges)
1860 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001861
Ted Kremenek71eca012007-08-29 23:20:49 +00001862 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001863
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001864 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001865
Chris Lattnerc61089a2009-06-30 01:26:17 +00001866 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1867 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001868 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001869 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001870 }
Mike Stump31feda52009-07-17 01:31:16 +00001871
Ted Kremenek71eca012007-08-29 23:20:49 +00001872 if (print_edges) {
1873 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001874 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001875 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001876
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001877 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1878 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001879
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001880 if (i == 8 || (i-8) == 0)
1881 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001882
Ted Kremenek71eca012007-08-29 23:20:49 +00001883 OS << " B" << (*I)->getBlockID();
1884 }
Mike Stump31feda52009-07-17 01:31:16 +00001885
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001886 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001887
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001888 // Print the successors of this block.
1889 OS << " Successors (" << B.succ_size() << "):";
1890 i = 0;
1891
1892 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1893 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001894
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001895 if (i == 8 || (i-8) % 10 == 0)
1896 OS << "\n ";
1897
Mike Stump0d76d072009-07-20 23:24:15 +00001898 if (*I)
1899 OS << " B" << (*I)->getBlockID();
1900 else
1901 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001902 }
Mike Stump31feda52009-07-17 01:31:16 +00001903
Ted Kremenek71eca012007-08-29 23:20:49 +00001904 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001905 }
Mike Stump31feda52009-07-17 01:31:16 +00001906}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001907
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001908
1909/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001910void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001911
1912/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001913void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1914 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001915
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001916 // Print the entry block.
1917 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001918
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001919 // Iterate through the CFGBlocks and print them one by one.
1920 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1921 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001922 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001923 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001924
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001925 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001926 }
Mike Stump31feda52009-07-17 01:31:16 +00001927
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001928 // Print the exit block.
1929 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001930 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001931}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001932
1933/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001934void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1935 print(llvm::errs(), cfg, LO);
1936}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001937
1938/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1939/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001940void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1941 const LangOptions &LO) const {
1942 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001943 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00001944}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001945
Ted Kremenek15647632008-01-30 23:02:42 +00001946/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001947void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00001948 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001949 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00001950 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1951}
1952
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001953Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00001954
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001955 if (!Terminator)
1956 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001957
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001958 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001959
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001960 switch (Terminator->getStmtClass()) {
1961 default:
1962 break;
Mike Stump31feda52009-07-17 01:31:16 +00001963
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001964 case Stmt::ForStmtClass:
1965 E = cast<ForStmt>(Terminator)->getCond();
1966 break;
Mike Stump31feda52009-07-17 01:31:16 +00001967
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001968 case Stmt::WhileStmtClass:
1969 E = cast<WhileStmt>(Terminator)->getCond();
1970 break;
Mike Stump31feda52009-07-17 01:31:16 +00001971
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001972 case Stmt::DoStmtClass:
1973 E = cast<DoStmt>(Terminator)->getCond();
1974 break;
Mike Stump31feda52009-07-17 01:31:16 +00001975
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001976 case Stmt::IfStmtClass:
1977 E = cast<IfStmt>(Terminator)->getCond();
1978 break;
Mike Stump31feda52009-07-17 01:31:16 +00001979
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001980 case Stmt::ChooseExprClass:
1981 E = cast<ChooseExpr>(Terminator)->getCond();
1982 break;
Mike Stump31feda52009-07-17 01:31:16 +00001983
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001984 case Stmt::IndirectGotoStmtClass:
1985 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1986 break;
Mike Stump31feda52009-07-17 01:31:16 +00001987
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001988 case Stmt::SwitchStmtClass:
1989 E = cast<SwitchStmt>(Terminator)->getCond();
1990 break;
Mike Stump31feda52009-07-17 01:31:16 +00001991
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001992 case Stmt::ConditionalOperatorClass:
1993 E = cast<ConditionalOperator>(Terminator)->getCond();
1994 break;
Mike Stump31feda52009-07-17 01:31:16 +00001995
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001996 case Stmt::BinaryOperatorClass: // '&&' and '||'
1997 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001998 break;
Mike Stump31feda52009-07-17 01:31:16 +00001999
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002000 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002001 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002002 }
Mike Stump31feda52009-07-17 01:31:16 +00002003
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002004 return E ? E->IgnoreParens() : NULL;
2005}
2006
Ted Kremenek92137a32008-05-16 16:06:00 +00002007bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002008
Ted Kremenek92137a32008-05-16 16:06:00 +00002009 if (!Terminator)
2010 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002011
Ted Kremenek92137a32008-05-16 16:06:00 +00002012 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002013
Ted Kremenek92137a32008-05-16 16:06:00 +00002014 switch (Terminator->getStmtClass()) {
2015 default:
2016 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002017
2018 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002019 case Stmt::WhileStmtClass:
2020 case Stmt::DoStmtClass:
2021 case Stmt::IfStmtClass:
2022 case Stmt::ChooseExprClass:
2023 case Stmt::ConditionalOperatorClass:
2024 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002025 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002026 }
Mike Stump31feda52009-07-17 01:31:16 +00002027
Ted Kremenek92137a32008-05-16 16:06:00 +00002028 return E ? E->IgnoreParens() : NULL;
2029}
2030
Ted Kremenek15647632008-01-30 23:02:42 +00002031
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002032//===----------------------------------------------------------------------===//
2033// CFG Graphviz Visualization
2034//===----------------------------------------------------------------------===//
2035
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002036
2037#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002038static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002039#endif
2040
Chris Lattnerc61089a2009-06-30 01:26:17 +00002041void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002042#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002043 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002044 GraphHelper = &H;
2045 llvm::ViewGraph(this,"CFG");
2046 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002047#endif
2048}
2049
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002050namespace llvm {
2051template<>
2052struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson4d9e93c2009-06-24 17:37:55 +00002053 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
2054 bool ShortNames) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002055
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002056#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002057 std::string OutSStr;
2058 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002059 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002060 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002061
2062 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2063
2064 // Process string output to make it nicer...
2065 for (unsigned i = 0; i != OutStr.length(); ++i)
2066 if (OutStr[i] == '\n') { // Left justify
2067 OutStr[i] = '\\';
2068 OutStr.insert(OutStr.begin()+i+1, 'l');
2069 }
Mike Stump31feda52009-07-17 01:31:16 +00002070
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002071 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002072#else
2073 return "";
2074#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002075 }
2076};
2077} // end namespace llvm