blob: a317e0452cf48d4936a45eb4c17e3df6a43d5218 [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"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000020#include "llvm/Support/Allocator.h"
21#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000022#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000023#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek8aed4902009-10-20 23:46:25 +000024#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000025
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000026using namespace clang;
27
28namespace {
29
Douglas Gregor6e6ad602009-01-20 01:17:11 +000030static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000031 if (VarDecl* VD = dyn_cast<VarDecl>(D))
32 if (Expr* Ex = VD->getInit())
33 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000034
35 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000036}
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000037
38class AddStmtChoice {
39public:
40 enum Kind { NotAlwaysAdd = 0, AlwaysAdd, AlwaysAddAsLValue };
41public:
42 AddStmtChoice(Kind kind) : k(kind) {}
43 bool alwaysAdd() const { return k != NotAlwaysAdd; }
44 bool asLValue() const { return k == AlwaysAddAsLValue; }
45private:
46 Kind k;
47};
Mike Stump31feda52009-07-17 01:31:16 +000048
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000049/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000050/// The builder is stateful: an instance of the builder should be used to only
51/// construct a single CFG.
52///
53/// Example usage:
54///
55/// CFGBuilder builder;
56/// CFG* cfg = builder.BuildAST(stmt1);
57///
Mike Stump31feda52009-07-17 01:31:16 +000058/// CFG construction is done via a recursive walk of an AST. We actually parse
59/// the AST in reverse order so that the successor of a basic block is
60/// constructed prior to its predecessor. This allows us to nicely capture
61/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000062///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000063class CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000064 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +000065 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +000066
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000067 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000068 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000069 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000070 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000071 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000072 CFGBlock* DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +000073
Ted Kremenekeda180e22007-08-28 19:26:49 +000074 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000075 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
76 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000077
78 // A list of blocks that end with a "goto" that must be backpatched to their
79 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000080 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000081 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000082
Ted Kremenekeda180e22007-08-28 19:26:49 +000083 // A list of labels whose address has been taken (for indirect gotos).
84 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
85 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000086
87public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +000088 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
89 Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000090 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek289ae4f2009-10-12 20:55:07 +000091 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {}
Mike Stump31feda52009-07-17 01:31:16 +000092
Ted Kremenek9aae5132007-08-23 21:42:29 +000093 // buildCFG - Used by external clients to construct the CFG.
Mike Stump0d76d072009-07-20 23:24:15 +000094 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump31feda52009-07-17 01:31:16 +000095
Ted Kremenek93668002009-07-17 22:18:43 +000096private:
97 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000098 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
99 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
100 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000101 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000102 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000103 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000104 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000105 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000106 CFGBlock *VisitConditionalOperator(ConditionalOperator *C,
107 AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000108 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenekc98cdd12009-12-15 01:38:04 +0000109 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S) { return NYS(); }
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000110 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenekc98cdd12009-12-15 01:38:04 +0000111 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S) { return NYS(); }
Ted Kremenek93668002009-07-17 22:18:43 +0000112 CFGBlock *VisitDeclStmt(DeclStmt *DS);
113 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000114 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
115 CFGBlock *VisitDoStmt(DoStmt *D);
116 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000117 CFGBlock *VisitGotoStmt(GotoStmt* G);
118 CFGBlock *VisitIfStmt(IfStmt *I);
119 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
120 CFGBlock *VisitLabelStmt(LabelStmt *L);
121 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
122 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
123 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
124 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
125 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
126 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000127 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
128 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000129 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
130 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000131
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000132 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
133 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000134 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000135
Ted Kremenek6065ef62008-04-28 18:00:46 +0000136 // NYS == Not Yet Supported
137 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000138 badCFG = true;
139 return Block;
140 }
Mike Stump31feda52009-07-17 01:31:16 +0000141
Ted Kremenek93668002009-07-17 22:18:43 +0000142 void autoCreateBlock() { if (!Block) Block = createBlock(); }
143 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000144 bool FinishBlock(CFGBlock* B);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000145 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
146 return Visit(S, asc);
147 }
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000148
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000149 void AppendStmt(CFGBlock *B, Stmt *S,
150 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
151 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000152 }
153
154 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
155 B->addSuccessor(S, cfg->getBumpVectorContext());
156 }
Mike Stump11289f42009-09-09 15:08:12 +0000157
Ted Kremenek963cc312009-07-24 06:55:42 +0000158 /// TryResult - a class representing a variant over the values
159 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
160 /// and is used by the CFGBuilder to decide if a branch condition
161 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000162 class TryResult {
163 int X;
164 public:
165 TryResult(bool b) : X(b ? 1 : 0) {}
166 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000167
Ted Kremenek30754282009-07-24 04:47:11 +0000168 bool isTrue() const { return X == 1; }
169 bool isFalse() const { return X == 0; }
170 bool isKnown() const { return X >= 0; }
171 void negate() {
172 assert(isKnown());
173 X ^= 0x1;
174 }
175 };
Mike Stump11289f42009-09-09 15:08:12 +0000176
Mike Stump773582d2009-07-23 23:25:26 +0000177 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
178 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000179 TryResult TryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000180 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000181 if (!S->isTypeDependent() && !S->isValueDependent() &&
182 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000183 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000184
185 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000186 }
187
Ted Kremenekb64d1832008-03-13 03:04:22 +0000188 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000189};
Mike Stump31feda52009-07-17 01:31:16 +0000190
Douglas Gregor4619e432008-12-05 23:32:09 +0000191// FIXME: Add support for dependent-sized array types in C++?
192// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000193static VariableArrayType* FindVA(Type* t) {
194 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
195 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
196 if (vat->getSizeExpr())
197 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000198
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000199 t = vt->getElementType().getTypePtr();
200 }
Mike Stump31feda52009-07-17 01:31:16 +0000201
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000202 return 0;
203}
Mike Stump31feda52009-07-17 01:31:16 +0000204
205/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
206/// arbitrary statement. Examples include a single expression or a function
207/// body (compound statement). The ownership of the returned CFG is
208/// transferred to the caller. If CFG construction fails, this method returns
209/// NULL.
Mike Stump0d76d072009-07-20 23:24:15 +0000210CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
211 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000212 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000213 if (!Statement)
214 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000215
Ted Kremenekb64d1832008-03-13 03:04:22 +0000216 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000217
218 // Create an empty block that will serve as the exit block for the CFG. Since
219 // this is the first block added to the CFG, it will be implicitly registered
220 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000221 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000222 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000223 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000224
Ted Kremenek9aae5132007-08-23 21:42:29 +0000225 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000226 CFGBlock* B = addStmt(Statement);
Ted Kremenek8aed4902009-10-20 23:46:25 +0000227 if (!B)
228 B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000229
Ted Kremenek40d87632008-02-27 17:33:02 +0000230 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000231 // Finalize the last constructed block. This usually involves reversing the
232 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000233 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000234
235 // Backpatch the gotos whose label -> block mappings we didn't know when we
236 // encountered them.
237 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000238 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000239
Ted Kremenek9aae5132007-08-23 21:42:29 +0000240 CFGBlock* B = *I;
241 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
242 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
243
244 // If there is no target for the goto, then we are looking at an
245 // 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 Kremenekeda180e22007-08-28 19:26:49 +0000251 // Add successors to the Indirect Goto Dispatch block (if we have one).
252 if (CFGBlock* B = cfg->getIndirectGotoBlock())
253 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
254 E = AddressTakenLabels.end(); I != E; ++I ) {
255
256 // Lookup the target block.
257 LabelMapTy::iterator LI = LabelMap.find(*I);
258
259 // If there is no target block that contains label, then we are looking
260 // at an incomplete AST. Handle this by not registering a successor.
261 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000262
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000263 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000264 }
Mike Stump31feda52009-07-17 01:31:16 +0000265
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000266 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000267 }
Mike Stump31feda52009-07-17 01:31:16 +0000268
269 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000270 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000271
Ted Kremenekab929bb2009-10-20 23:59:28 +0000272 return badCFG ? NULL : cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000273}
Mike Stump31feda52009-07-17 01:31:16 +0000274
Ted Kremenek9aae5132007-08-23 21:42:29 +0000275/// createBlock - Used to lazily create blocks that are connected
276/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000277CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000278 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000279 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000280 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000281 return B;
282}
Mike Stump31feda52009-07-17 01:31:16 +0000283
Ted Kremenek0868eea2009-09-24 18:45:41 +0000284/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek55957a82009-05-02 00:13:27 +0000285bool CFGBuilder::FinishBlock(CFGBlock* B) {
286 if (badCFG)
287 return false;
288
Ted Kremenek93668002009-07-17 22:18:43 +0000289 assert(B);
Ted Kremenek55957a82009-05-02 00:13:27 +0000290 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000291}
292
Ted Kremenek93668002009-07-17 22:18:43 +0000293/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000294/// blocks for ternary operators, &&, and ||. We also process "," and
295/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000296CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000297tryAgain:
298 switch (S->getStmtClass()) {
299 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000300 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000301
302 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000303 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000304
Ted Kremenek93668002009-07-17 22:18:43 +0000305 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000306 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000307
Ted Kremenek93668002009-07-17 22:18:43 +0000308 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000309 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000310
Ted Kremenek93668002009-07-17 22:18:43 +0000311 case Stmt::BreakStmtClass:
312 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000313
Ted Kremenek93668002009-07-17 22:18:43 +0000314 case Stmt::CallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000315 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000316
Ted Kremenek93668002009-07-17 22:18:43 +0000317 case Stmt::CaseStmtClass:
318 return VisitCaseStmt(cast<CaseStmt>(S));
319
320 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000321 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000322
Ted Kremenek93668002009-07-17 22:18:43 +0000323 case Stmt::CompoundStmtClass:
324 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000325
Ted Kremenek93668002009-07-17 22:18:43 +0000326 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000327 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000328
Ted Kremenek93668002009-07-17 22:18:43 +0000329 case Stmt::ContinueStmtClass:
330 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000331
Ted Kremenek93668002009-07-17 22:18:43 +0000332 case Stmt::DeclStmtClass:
333 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000334
Ted Kremenek93668002009-07-17 22:18:43 +0000335 case Stmt::DefaultStmtClass:
336 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000337
Ted Kremenek93668002009-07-17 22:18:43 +0000338 case Stmt::DoStmtClass:
339 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000340
Ted Kremenek93668002009-07-17 22:18:43 +0000341 case Stmt::ForStmtClass:
342 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000343
Ted Kremenek93668002009-07-17 22:18:43 +0000344 case Stmt::GotoStmtClass:
345 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000346
Ted Kremenek93668002009-07-17 22:18:43 +0000347 case Stmt::IfStmtClass:
348 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000349
Ted Kremenek93668002009-07-17 22:18:43 +0000350 case Stmt::IndirectGotoStmtClass:
351 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000352
Ted Kremenek93668002009-07-17 22:18:43 +0000353 case Stmt::LabelStmtClass:
354 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000355
Ted Kremenek93668002009-07-17 22:18:43 +0000356 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000357 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
358
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000359 case Stmt::CXXThrowExprClass:
360 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
361
Ted Kremenek93668002009-07-17 22:18:43 +0000362 case Stmt::ObjCAtSynchronizedStmtClass:
363 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000364
Ted Kremenek93668002009-07-17 22:18:43 +0000365 case Stmt::ObjCAtThrowStmtClass:
366 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000367
Ted Kremenek93668002009-07-17 22:18:43 +0000368 case Stmt::ObjCAtTryStmtClass:
369 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000370
Ted Kremenek93668002009-07-17 22:18:43 +0000371 case Stmt::ObjCForCollectionStmtClass:
372 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000373
Ted Kremenek93668002009-07-17 22:18:43 +0000374 case Stmt::ParenExprClass:
375 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000376 goto tryAgain;
377
Ted Kremenek93668002009-07-17 22:18:43 +0000378 case Stmt::NullStmtClass:
379 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000380
Ted Kremenek93668002009-07-17 22:18:43 +0000381 case Stmt::ReturnStmtClass:
382 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000383
Ted Kremenek93668002009-07-17 22:18:43 +0000384 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000385 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000386
Ted Kremenek93668002009-07-17 22:18:43 +0000387 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000388 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000389
Ted Kremenek93668002009-07-17 22:18:43 +0000390 case Stmt::SwitchStmtClass:
391 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000392
Ted Kremenek93668002009-07-17 22:18:43 +0000393 case Stmt::WhileStmtClass:
394 return VisitWhileStmt(cast<WhileStmt>(S));
395 }
396}
Mike Stump11289f42009-09-09 15:08:12 +0000397
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000398CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
399 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000400 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000401 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Ted Kremenek93668002009-07-17 22:18:43 +0000404 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000405}
Mike Stump31feda52009-07-17 01:31:16 +0000406
Ted Kremenek93668002009-07-17 22:18:43 +0000407/// VisitChildren - Visit the children of a Stmt.
408CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
409 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000410 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000411 E = Terminator->child_end(); I != E; ++I) {
412 if (*I) B = Visit(*I);
413 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000414 return B;
415}
Mike Stump11289f42009-09-09 15:08:12 +0000416
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000417CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
418 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000419 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000420
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000421 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000422 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000423 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000424 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000425
Ted Kremenek9aae5132007-08-23 21:42:29 +0000426 return Block;
427}
Mike Stump11289f42009-09-09 15:08:12 +0000428
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000429CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
430 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000431 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000432 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000433 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000434
Ted Kremenek93668002009-07-17 22:18:43 +0000435 if (!FinishBlock(ConfluenceBlock))
436 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000437
Ted Kremenek93668002009-07-17 22:18:43 +0000438 // create the block evaluating the LHS
439 CFGBlock* LHSBlock = createBlock(false);
440 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000441
Ted Kremenek93668002009-07-17 22:18:43 +0000442 // create the block evaluating the RHS
443 Succ = ConfluenceBlock;
444 Block = NULL;
445 CFGBlock* RHSBlock = addStmt(B->getRHS());
446 if (!FinishBlock(RHSBlock))
447 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000448
Mike Stump773582d2009-07-23 23:25:26 +0000449 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000450 TryResult KnownVal = TryEvaluateBool(B->getLHS());
451 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
452 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000453
Ted Kremenek93668002009-07-17 22:18:43 +0000454 // Now link the LHSBlock with RHSBlock.
455 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000456 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
457 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000458 } else {
Ted Kremenek93668002009-07-17 22:18:43 +0000459 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000460 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
461 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000462 }
Mike Stump11289f42009-09-09 15:08:12 +0000463
Ted Kremenek93668002009-07-17 22:18:43 +0000464 // Generate the blocks for evaluating the LHS.
465 Block = LHSBlock;
466 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000467 }
Ted Kremenek93668002009-07-17 22:18:43 +0000468 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000469 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000470 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000471 addStmt(B->getRHS());
472 return addStmt(B->getLHS());
473 }
Mike Stump11289f42009-09-09 15:08:12 +0000474
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000475 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000476}
477
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000478CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
479 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000480 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000481 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000482 }
483 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000484}
485
Ted Kremenek93668002009-07-17 22:18:43 +0000486CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
487 // "break" is a control-flow statement. Thus we stop processing the current
488 // block.
489 if (Block && !FinishBlock(Block))
490 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000491
Ted Kremenek93668002009-07-17 22:18:43 +0000492 // Now create a new block that ends with the break statement.
493 Block = createBlock(false);
494 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000495
Ted Kremenek93668002009-07-17 22:18:43 +0000496 // If there is no target for the break, then we are looking at an incomplete
497 // AST. This means that the CFG cannot be constructed.
498 if (BreakTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000499 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000500 else
501 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000502
503
Ted Kremenek9aae5132007-08-23 21:42:29 +0000504 return Block;
505}
Mike Stump11289f42009-09-09 15:08:12 +0000506
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000507CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000508 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000509 bool NoReturn = false;
510 if (C->getCallee()->getType().getNoReturnAttr()) {
511 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000512 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000513
514 if (FunctionDecl *FD = C->getDirectCallee())
515 if (FD->hasAttr<NoReturnAttr>())
516 NoReturn = true;
517
518 if (!NoReturn)
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000519 return VisitStmt(C, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000520
Mike Stump8c5d7992009-07-25 21:26:53 +0000521 if (Block && !FinishBlock(Block))
522 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000523
Mike Stump8c5d7992009-07-25 21:26:53 +0000524 // Create new block with no successor for the remaining pieces.
525 Block = createBlock(false);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000526 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000527
528 // Wire this to the exit block directly.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000529 AddSuccessor(Block, &cfg->getExit());
Mike Stump11289f42009-09-09 15:08:12 +0000530
Mike Stump8c5d7992009-07-25 21:26:53 +0000531 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000532}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000533
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000534CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
535 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000536 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000537 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000538 if (!FinishBlock(ConfluenceBlock))
539 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000540
Ted Kremenek21822592009-07-17 18:20:32 +0000541 Succ = ConfluenceBlock;
542 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000543 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000544 if (!FinishBlock(LHSBlock))
545 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000546
Ted Kremenek21822592009-07-17 18:20:32 +0000547 Succ = ConfluenceBlock;
548 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000549 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000550 if (!FinishBlock(RHSBlock))
551 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000552
Ted Kremenek21822592009-07-17 18:20:32 +0000553 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000554 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000555 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000556 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
557 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000558 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000559 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000560}
Mike Stump11289f42009-09-09 15:08:12 +0000561
562
563CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
564 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000565
566 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
567 I != E; ++I ) {
568 LastBlock = addStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000569
Ted Kremenekce499c22009-08-27 23:16:26 +0000570 if (badCFG)
571 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000572 }
Ted Kremenek93668002009-07-17 22:18:43 +0000573 return LastBlock;
574}
Mike Stump11289f42009-09-09 15:08:12 +0000575
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000576CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
577 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000578 // Create the confluence block that will "merge" the results of the ternary
579 // expression.
580 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000581 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000582 if (!FinishBlock(ConfluenceBlock))
583 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000584
Ted Kremenek51d40b02009-07-17 18:15:54 +0000585 // Create a block for the LHS expression if there is an LHS expression. A
586 // GCC extension allows LHS to be NULL, causing the condition to be the
587 // value that is returned instead.
588 // e.g: x ?: y is shorthand for: x ? x : y;
589 Succ = ConfluenceBlock;
590 Block = NULL;
591 CFGBlock* LHSBlock = NULL;
592 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000593 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000594 if (!FinishBlock(LHSBlock))
595 return 0;
596 Block = NULL;
597 }
Mike Stump11289f42009-09-09 15:08:12 +0000598
Ted Kremenek51d40b02009-07-17 18:15:54 +0000599 // Create the block for the RHS expression.
600 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000601 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000602 if (!FinishBlock(RHSBlock))
603 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000604
Ted Kremenek51d40b02009-07-17 18:15:54 +0000605 // Create the block that will contain the condition.
606 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000607
Mike Stump773582d2009-07-23 23:25:26 +0000608 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000609 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000610 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000611 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000612 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000613 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000614 // If we know the condition is false, add NULL as the successor for
615 // the block containing the condition. In this case, the confluence
616 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000617 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000618 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000619 } else {
620 // If we have no LHS expression, add the ConfluenceBlock as a direct
621 // successor for the block containing the condition. Moreover, we need to
622 // reverse the order of the predecessors in the ConfluenceBlock because
623 // the RHSBlock will have been added to the succcessors already, and we
624 // want the first predecessor to the the block containing the expression
625 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000626 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000627 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000628 std::reverse(ConfluenceBlock->pred_begin(),
629 ConfluenceBlock->pred_end());
630 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000631 }
Mike Stump11289f42009-09-09 15:08:12 +0000632
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000633 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000634 Block->setTerminator(C);
635 return addStmt(C->getCond());
636}
637
Ted Kremenek93668002009-07-17 22:18:43 +0000638CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
639 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000640
Ted Kremenek93668002009-07-17 22:18:43 +0000641 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000642 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000643 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000644 }
Mike Stump11289f42009-09-09 15:08:12 +0000645
Ted Kremenek93668002009-07-17 22:18:43 +0000646 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000647
Ted Kremenek93668002009-07-17 22:18:43 +0000648 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
649 typedef llvm::SmallVector<Decl*,10> BufTy;
650 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000651
Ted Kremenek93668002009-07-17 22:18:43 +0000652 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
653 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
654 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
655 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000656
Ted Kremenek93668002009-07-17 22:18:43 +0000657 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
658 // automatically freed with the CFG.
659 DeclGroupRef DG(*I);
660 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000661 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000662 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000663
Ted Kremenek93668002009-07-17 22:18:43 +0000664 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000665 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000666 B = VisitDeclSubExpr(D);
667 }
Mike Stump11289f42009-09-09 15:08:12 +0000668
669 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000670}
Mike Stump11289f42009-09-09 15:08:12 +0000671
Ted Kremenek93668002009-07-17 22:18:43 +0000672/// VisitDeclSubExpr - Utility method to add block-level expressions for
673/// initializers in Decls.
674CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
675 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000676
Ted Kremenek93668002009-07-17 22:18:43 +0000677 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000678
Ted Kremenek93668002009-07-17 22:18:43 +0000679 if (!VD)
680 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000681
Ted Kremenek93668002009-07-17 22:18:43 +0000682 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000683
Ted Kremenek93668002009-07-17 22:18:43 +0000684 if (Init) {
685 // Optimization: Don't create separate block-level statements for literals.
686 switch (Init->getStmtClass()) {
687 case Stmt::IntegerLiteralClass:
688 case Stmt::CharacterLiteralClass:
689 case Stmt::StringLiteralClass:
690 break;
691 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000692 Block = addStmt(Init,
693 VD->getType()->isReferenceType()
694 ? AddStmtChoice::AlwaysAddAsLValue
695 : AddStmtChoice::AlwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000696 }
697 }
Mike Stump11289f42009-09-09 15:08:12 +0000698
Ted Kremenek93668002009-07-17 22:18:43 +0000699 // If the type of VD is a VLA, then we must process its size expressions.
700 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
701 VA = FindVA(VA->getElementType().getTypePtr()))
702 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000703
Ted Kremenek93668002009-07-17 22:18:43 +0000704 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000705}
706
707CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000708 // We may see an if statement in the middle of a basic block, or it may be the
709 // first statement we are processing. In either case, we create a new basic
710 // block. First, we create the blocks for the then...else statements, and
711 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000712 // middle of a block, we stop processing that block. That block is then the
713 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000714
715 // The block we were proccessing is now finished. Make it the successor
716 // block.
717 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000718 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000719 if (!FinishBlock(Block))
720 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000721 }
Mike Stump31feda52009-07-17 01:31:16 +0000722
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000723 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000724 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000725
Ted Kremenek9aae5132007-08-23 21:42:29 +0000726 if (Stmt* Else = I->getElse()) {
727 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000728
Ted Kremenek9aae5132007-08-23 21:42:29 +0000729 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000730 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000731 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000732 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000733
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000734 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
735 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000736 else if (Block) {
737 if (!FinishBlock(ElseBlock))
738 return 0;
739 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000740 }
Mike Stump31feda52009-07-17 01:31:16 +0000741
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000742 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000743 CFGBlock* ThenBlock;
744 {
745 Stmt* Then = I->getThen();
746 assert (Then);
747 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000748 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000749 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000750
Ted Kremenek1b379512009-04-01 03:52:47 +0000751 if (!ThenBlock) {
752 // We can reach here if the "then" body has all NullStmts.
753 // Create an empty block so we can distinguish between true and false
754 // branches in path-sensitive analyses.
755 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000756 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000757 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000758 if (!FinishBlock(ThenBlock))
759 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000760 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000761 }
762
Mike Stump31feda52009-07-17 01:31:16 +0000763 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000764 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000765
Ted Kremenek9aae5132007-08-23 21:42:29 +0000766 // Set the terminator of the new block to the If statement.
767 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000768
Mike Stump773582d2009-07-23 23:25:26 +0000769 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000770 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000771
Ted Kremenek9aae5132007-08-23 21:42:29 +0000772 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000773 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
774 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000775
776 // Add the condition as the last statement in the new block. This may create
777 // new blocks as the condition may contain control-flow. Any newly created
778 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000779 Block = addStmt(I->getCond());
780
781 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
782 // and the condition variable initialization to the CFG.
783 if (VarDecl *VD = I->getConditionVariable()) {
784 if (Expr *Init = VD->getInit()) {
785 autoCreateBlock();
786 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
787 addStmt(Init);
788 }
789 }
790
791 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000792}
Mike Stump31feda52009-07-17 01:31:16 +0000793
794
Ted Kremenek9aae5132007-08-23 21:42:29 +0000795CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000796 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000797 //
Mike Stump31feda52009-07-17 01:31:16 +0000798 // NOTE: If a "return" appears in the middle of a block, this means that the
799 // code afterwards is DEAD (unreachable). We still keep a basic block
800 // for that code; a simple "mark-and-sweep" from the entry block will be
801 // able to report such dead blocks.
Ted Kremenek0868eea2009-09-24 18:45:41 +0000802 if (Block)
803 FinishBlock(Block);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000804
805 // Create the new block.
806 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000807
Ted Kremenek9aae5132007-08-23 21:42:29 +0000808 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000809 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000810
811 // Add the return statement to the block. This may create new blocks if R
812 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000813 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000814}
815
816CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
817 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000818 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000819 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000820
Ted Kremenek93668002009-07-17 22:18:43 +0000821 if (!LabelBlock) // This can happen when the body is empty, i.e.
822 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000823
Ted Kremenek93668002009-07-17 22:18:43 +0000824 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000825 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000826
827 // Labels partition blocks, so this is the end of the basic block we were
828 // processing (L is the block's label). Because this is label (and we have
829 // already processed the substatement) there is no extra control-flow to worry
830 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000831 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000832 if (!FinishBlock(LabelBlock))
833 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000834
835 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000836 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000837
Ted Kremenek9aae5132007-08-23 21:42:29 +0000838 // This block is now the implicit successor of other blocks.
839 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000840
Ted Kremenek9aae5132007-08-23 21:42:29 +0000841 return LabelBlock;
842}
843
844CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000845 // Goto is a control-flow statement. Thus we stop processing the current
846 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000847 if (Block)
848 FinishBlock(Block);
849
Ted Kremenek9aae5132007-08-23 21:42:29 +0000850 Block = createBlock(false);
851 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000852
853 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000854 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000855
Ted Kremenek9aae5132007-08-23 21:42:29 +0000856 if (I == LabelMap.end())
857 // We will need to backpatch this block later.
858 BackpatchBlocks.push_back(Block);
859 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000860 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000861
Mike Stump31feda52009-07-17 01:31:16 +0000862 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000863}
864
865CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000866 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000867
Mike Stump014b3ea2009-07-21 01:12:51 +0000868 // "for" is a control-flow statement. Thus we stop processing the current
869 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000870 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000871 if (!FinishBlock(Block))
872 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000873 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000874 } else
875 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000876
877 // Because of short-circuit evaluation, the condition of the loop can span
878 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
879 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000880 CFGBlock* ExitConditionBlock = createBlock(false);
881 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000882
Ted Kremenek81e14852007-08-27 19:46:09 +0000883 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000884 ExitConditionBlock->setTerminator(F);
885
886 // Now add the actual condition to the condition block. Because the condition
887 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000888 if (Stmt* C = F->getCond()) {
889 Block = ExitConditionBlock;
890 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000891 if (Block) {
892 if (!FinishBlock(EntryConditionBlock))
893 return 0;
894 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000895 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000896
Mike Stump31feda52009-07-17 01:31:16 +0000897 // The condition block is the implicit successor for the loop body as well as
898 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000899 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000900
Mike Stump773582d2009-07-23 23:25:26 +0000901 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000902 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +0000903
Mike Stump773582d2009-07-23 23:25:26 +0000904 if (F->getCond())
905 KnownVal = TryEvaluateBool(F->getCond());
906
Ted Kremenek9aae5132007-08-23 21:42:29 +0000907 // Now create the loop body.
908 {
909 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000910
Ted Kremenek9aae5132007-08-23 21:42:29 +0000911 // Save the current values for Block, Succ, and continue and break targets
912 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
913 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000914 save_break(BreakTargetBlock);
915
Ted Kremeneke9610502007-08-30 18:39:40 +0000916 // Create a new block to contain the (bottom) of the loop body.
917 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000918
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000919 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000920 // Generate increment code in its own basic block. This is the target of
921 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000922 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000923 } else {
924 // No increment code. Create a special, empty, block that is used as the
925 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000926 assert(Succ == EntryConditionBlock);
927 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000928 }
Mike Stump31feda52009-07-17 01:31:16 +0000929
Ted Kremenek902393b2009-04-28 00:51:56 +0000930 // Finish up the increment (or empty) block if it hasn't been already.
931 if (Block) {
932 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000933 if (!FinishBlock(Block))
934 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000935 Block = 0;
936 }
Mike Stump31feda52009-07-17 01:31:16 +0000937
Ted Kremenek902393b2009-04-28 00:51:56 +0000938 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000939
Ted Kremenek902393b2009-04-28 00:51:56 +0000940 // The starting block for the loop increment is the block that should
941 // represent the 'loop target' for looping back to the start of the loop.
942 ContinueTargetBlock->setLoopTarget(F);
943
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000944 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000945 BreakTargetBlock = LoopSuccessor;
946
947 // Now populate the body block, and in the process create new blocks as we
948 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000949 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000950
951 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +0000952 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +0000953 else if (Block && !FinishBlock(BodyBlock))
954 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000955
Ted Kremenek30754282009-07-24 04:47:11 +0000956 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000957 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000958 }
Mike Stump31feda52009-07-17 01:31:16 +0000959
Ted Kremenek30754282009-07-24 04:47:11 +0000960 // Link up the condition block with the code that follows the loop. (the
961 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000962 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000963
Ted Kremenek9aae5132007-08-23 21:42:29 +0000964 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000965 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000966 if (Stmt* I = F->getInit()) {
967 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000968 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000969 } else {
970 // There is no loop initialization. We are thus basically a while loop.
971 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000972 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000973 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000974 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000975 }
976}
977
Ted Kremenek9d56e642008-11-11 17:10:00 +0000978CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
979 // Objective-C fast enumeration 'for' statements:
980 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
981 //
982 // for ( Type newVariable in collection_expression ) { statements }
983 //
984 // becomes:
985 //
986 // prologue:
987 // 1. collection_expression
988 // T. jump to loop_entry
989 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000990 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +0000991 // 1. ObjCForCollectionStmt [performs binding to newVariable]
992 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
993 // TB:
994 // statements
995 // T. jump to loop_entry
996 // FB:
997 // what comes after
998 //
999 // and
1000 //
1001 // Type existingItem;
1002 // for ( existingItem in expression ) { statements }
1003 //
1004 // becomes:
1005 //
Mike Stump31feda52009-07-17 01:31:16 +00001006 // the same with newVariable replaced with existingItem; the binding works
1007 // the same except that for one ObjCForCollectionStmt::getElement() returns
1008 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001009 //
Mike Stump31feda52009-07-17 01:31:16 +00001010
Ted Kremenek9d56e642008-11-11 17:10:00 +00001011 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001012
Ted Kremenek9d56e642008-11-11 17:10:00 +00001013 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001014 if (!FinishBlock(Block))
1015 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001016 LoopSuccessor = Block;
1017 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001018 } else
1019 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001020
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001021 // Build the condition blocks.
1022 CFGBlock* ExitConditionBlock = createBlock(false);
1023 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001024
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001025 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001026 ExitConditionBlock->setTerminator(S);
1027
1028 // The last statement in the block should be the ObjCForCollectionStmt, which
1029 // performs the actual binding to 'element' and determines if there are any
1030 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001031 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001032 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001033
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001034 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001035 // generate new blocks as necesary. We DON'T add the statement by default to
1036 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001037 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001038 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001039 if (!FinishBlock(EntryConditionBlock))
1040 return 0;
1041 Block = 0;
1042 }
Mike Stump31feda52009-07-17 01:31:16 +00001043
1044 // The condition block is the implicit successor for the loop body as well as
1045 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001046 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001047
Ted Kremenek9d56e642008-11-11 17:10:00 +00001048 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001049 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001050 // Save the current values for Succ, continue and break targets.
1051 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001052 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1053
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001054 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001055 ContinueTargetBlock = EntryConditionBlock;
1056
Ted Kremenek93668002009-07-17 22:18:43 +00001057 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001058
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001059 if (!BodyBlock)
1060 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001061 else if (Block) {
1062 if (!FinishBlock(BodyBlock))
1063 return 0;
1064 }
Mike Stump31feda52009-07-17 01:31:16 +00001065
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001066 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001067 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001068 }
Mike Stump31feda52009-07-17 01:31:16 +00001069
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001070 // Link up the condition block with the code that follows the loop.
1071 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001072 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001073
Ted Kremenek9d56e642008-11-11 17:10:00 +00001074 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001075 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001076 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001077}
1078
Ted Kremenek49805452009-05-02 01:49:13 +00001079CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1080 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001081
Ted Kremenek49805452009-05-02 01:49:13 +00001082 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001083 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001084
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001085 // The sync body starts its own basic block. This makes it a little easier
1086 // for diagnostic clients.
1087 if (SyncBlock) {
1088 if (!FinishBlock(SyncBlock))
1089 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001090
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001091 Block = 0;
1092 }
Mike Stump31feda52009-07-17 01:31:16 +00001093
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001094 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001095
Ted Kremenek49805452009-05-02 01:49:13 +00001096 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001097 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001098}
Mike Stump31feda52009-07-17 01:31:16 +00001099
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001100CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001101 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001102 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001103}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001104
Ted Kremenek9aae5132007-08-23 21:42:29 +00001105CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001106 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001107
Mike Stump014b3ea2009-07-21 01:12:51 +00001108 // "while" is a control-flow statement. Thus we stop processing the current
1109 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001110 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001111 if (!FinishBlock(Block))
1112 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001113 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001114 } else
1115 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001116
1117 // Because of short-circuit evaluation, the condition of the loop can span
1118 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1119 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001120 CFGBlock* ExitConditionBlock = createBlock(false);
1121 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001122
Ted Kremenek81e14852007-08-27 19:46:09 +00001123 // Set the terminator for the "exit" condition block.
1124 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001125
1126 // Now add the actual condition to the condition block. Because the condition
1127 // itself may contain control-flow, new blocks may be created. Thus we update
1128 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001129 if (Stmt* C = W->getCond()) {
1130 Block = ExitConditionBlock;
1131 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001132 assert(Block == EntryConditionBlock);
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001133
1134 // If this block contains a condition variable, add both the condition
1135 // variable and initializer to the CFG.
1136 if (VarDecl *VD = W->getConditionVariable()) {
1137 if (Expr *Init = VD->getInit()) {
1138 autoCreateBlock();
1139 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1140 EntryConditionBlock = addStmt(Init);
1141 assert(Block == EntryConditionBlock);
1142 }
1143 }
1144
Ted Kremenek55957a82009-05-02 00:13:27 +00001145 if (Block) {
1146 if (!FinishBlock(EntryConditionBlock))
1147 return 0;
1148 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001149 }
Mike Stump31feda52009-07-17 01:31:16 +00001150
1151 // The condition block is the implicit successor for the loop body as well as
1152 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001153 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001154
Mike Stump773582d2009-07-23 23:25:26 +00001155 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001156 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001157
Ted Kremenek9aae5132007-08-23 21:42:29 +00001158 // Process the loop body.
1159 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001160 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001161
1162 // Save the current values for Block, Succ, and continue and break targets
1163 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1164 save_continue(ContinueTargetBlock),
1165 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001166
Mike Stump31feda52009-07-17 01:31:16 +00001167 // Create an empty block to represent the transition block for looping back
1168 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001169 Block = 0;
1170 assert(Succ == EntryConditionBlock);
1171 Succ = createBlock();
1172 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001173 ContinueTargetBlock = Succ;
1174
Ted Kremenek9aae5132007-08-23 21:42:29 +00001175 // All breaks should go to the code following the loop.
1176 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001177
Ted Kremenek9aae5132007-08-23 21:42:29 +00001178 // NULL out Block to force lazy instantiation of blocks for the body.
1179 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001180
Ted Kremenek9aae5132007-08-23 21:42:29 +00001181 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001182 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001183
Ted Kremeneke9610502007-08-30 18:39:40 +00001184 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001185 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001186 else if (Block) {
1187 if (!FinishBlock(BodyBlock))
1188 return 0;
1189 }
Mike Stump31feda52009-07-17 01:31:16 +00001190
Ted Kremenek30754282009-07-24 04:47:11 +00001191 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001192 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001193 }
Mike Stump31feda52009-07-17 01:31:16 +00001194
Ted Kremenek30754282009-07-24 04:47:11 +00001195 // Link up the condition block with the code that follows the loop. (the
1196 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001197 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001198
1199 // There can be no more statements in the condition block since we loop back
1200 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001201 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001202
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001203 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001204 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001205 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001206}
Mike Stump11289f42009-09-09 15:08:12 +00001207
1208
Ted Kremenek93668002009-07-17 22:18:43 +00001209CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1210 // FIXME: For now we pretend that @catch and the code it contains does not
1211 // exit.
1212 return Block;
1213}
Mike Stump31feda52009-07-17 01:31:16 +00001214
Ted Kremenek93041ba2008-12-09 20:20:09 +00001215CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1216 // FIXME: This isn't complete. We basically treat @throw like a return
1217 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001218
Ted Kremenek0868eea2009-09-24 18:45:41 +00001219 // If we were in the middle of a block we stop processing that block.
Ted Kremenek93668002009-07-17 22:18:43 +00001220 if (Block && !FinishBlock(Block))
1221 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001222
Ted Kremenek93041ba2008-12-09 20:20:09 +00001223 // Create the new block.
1224 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001225
Ted Kremenek93041ba2008-12-09 20:20:09 +00001226 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001227 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001228
1229 // Add the statement to the block. This may create new blocks if S contains
1230 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001231 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001232}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001233
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001234CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001235 // If we were in the middle of a block we stop processing that block.
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001236 if (Block && !FinishBlock(Block))
1237 return 0;
1238
1239 // Create the new block.
1240 Block = createBlock(false);
1241
1242 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001243 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001244
1245 // Add the statement to the block. This may create new blocks if S contains
1246 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001247 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001248}
1249
Ted Kremenek93668002009-07-17 22:18:43 +00001250CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001251 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001252
Mike Stump8d50b6a2009-07-21 01:27:50 +00001253 // "do...while" is a control-flow statement. Thus we stop processing the
1254 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001255 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001256 if (!FinishBlock(Block))
1257 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001258 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001259 } else
1260 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001261
1262 // Because of short-circuit evaluation, the condition of the loop can span
1263 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1264 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001265 CFGBlock* ExitConditionBlock = createBlock(false);
1266 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001267
Ted Kremenek81e14852007-08-27 19:46:09 +00001268 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001269 ExitConditionBlock->setTerminator(D);
1270
1271 // Now add the actual condition to the condition block. Because the condition
1272 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001273 if (Stmt* C = D->getCond()) {
1274 Block = ExitConditionBlock;
1275 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001276 if (Block) {
1277 if (!FinishBlock(EntryConditionBlock))
1278 return 0;
1279 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001280 }
Mike Stump31feda52009-07-17 01:31:16 +00001281
Ted Kremeneka1523a32008-02-27 07:20:00 +00001282 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001283 Succ = EntryConditionBlock;
1284
Mike Stump773582d2009-07-23 23:25:26 +00001285 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001286 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001287
Ted Kremenek9aae5132007-08-23 21:42:29 +00001288 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001289 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001290 {
1291 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001292
Ted Kremenek9aae5132007-08-23 21:42:29 +00001293 // Save the current values for Block, Succ, and continue and break targets
1294 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1295 save_continue(ContinueTargetBlock),
1296 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001297
Ted Kremenek9aae5132007-08-23 21:42:29 +00001298 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001299 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001300
Ted Kremenek9aae5132007-08-23 21:42:29 +00001301 // All breaks should go to the code following the loop.
1302 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001303
Ted Kremenek9aae5132007-08-23 21:42:29 +00001304 // NULL out Block to force lazy instantiation of blocks for the body.
1305 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001306
Ted Kremenek9aae5132007-08-23 21:42:29 +00001307 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001308 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001309
Ted Kremeneke9610502007-08-30 18:39:40 +00001310 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001311 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001312 else if (Block) {
1313 if (!FinishBlock(BodyBlock))
1314 return 0;
1315 }
Mike Stump31feda52009-07-17 01:31:16 +00001316
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001317 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001318 // ExitConditionBlock to represent the "loop back" transition. Create an
1319 // empty block to represent the transition block for looping back to the
1320 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001321 // FIXME: Can we do this more efficiently without adding another block?
1322 Block = NULL;
1323 Succ = BodyBlock;
1324 CFGBlock *LoopBackBlock = createBlock();
1325 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001326
Ted Kremenek30754282009-07-24 04:47:11 +00001327 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001328 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001329 }
Mike Stump31feda52009-07-17 01:31:16 +00001330
Ted Kremenek30754282009-07-24 04:47:11 +00001331 // Link up the condition block with the code that follows the loop.
1332 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001333 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001334
1335 // There can be no more statements in the body block(s) since we loop back to
1336 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001337 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001338
Ted Kremenek9aae5132007-08-23 21:42:29 +00001339 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001340 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001341 return BodyBlock;
1342}
1343
1344CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1345 // "continue" is a control-flow statement. Thus we stop processing the
1346 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001347 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001348 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001349
Ted Kremenek9aae5132007-08-23 21:42:29 +00001350 // Now create a new block that ends with the continue statement.
1351 Block = createBlock(false);
1352 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001353
Ted Kremenek9aae5132007-08-23 21:42:29 +00001354 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001355 // incomplete AST. This means the CFG cannot be constructed.
1356 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001357 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001358 else
1359 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001360
Ted Kremenek9aae5132007-08-23 21:42:29 +00001361 return Block;
1362}
Mike Stump11289f42009-09-09 15:08:12 +00001363
Ted Kremenek0747de62009-07-18 00:47:21 +00001364CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001365 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001366
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001367 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001368 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001369 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001370 }
Mike Stump11289f42009-09-09 15:08:12 +00001371
Ted Kremenek93668002009-07-17 22:18:43 +00001372 // VLA types have expressions that must be evaluated.
1373 if (E->isArgumentType()) {
1374 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1375 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1376 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001377 }
Mike Stump11289f42009-09-09 15:08:12 +00001378
Mike Stump31feda52009-07-17 01:31:16 +00001379 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001380}
Mike Stump11289f42009-09-09 15:08:12 +00001381
Ted Kremenek93668002009-07-17 22:18:43 +00001382/// VisitStmtExpr - Utility method to handle (nested) statement
1383/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001384CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1385 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001386 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001387 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001388 }
Ted Kremenek93668002009-07-17 22:18:43 +00001389 return VisitCompoundStmt(SE->getSubStmt());
1390}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001391
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001392CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001393 // "switch" is a control-flow statement. Thus we stop processing the current
1394 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001395 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001396
Ted Kremenek9aae5132007-08-23 21:42:29 +00001397 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001398 if (!FinishBlock(Block))
1399 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001400 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001401 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001402
1403 // Save the current "switch" context.
1404 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001405 save_break(BreakTargetBlock),
1406 save_default(DefaultCaseBlock);
1407
Mike Stump31feda52009-07-17 01:31:16 +00001408 // Set the "default" case to be the block after the switch statement. If the
1409 // switch statement contains a "default:", this value will be overwritten with
1410 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001411 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001412
Ted Kremenek9aae5132007-08-23 21:42:29 +00001413 // Create a new block that will contain the switch statement.
1414 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001415
Ted Kremenek9aae5132007-08-23 21:42:29 +00001416 // Now process the switch body. The code after the switch is the implicit
1417 // successor.
1418 Succ = SwitchSuccessor;
1419 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001420
1421 // When visiting the body, the case statements should automatically get linked
1422 // up to the switch. We also don't keep a pointer to the body, since all
1423 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001424 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001425 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001426 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001427 if (Block) {
1428 if (!FinishBlock(BodyBlock))
1429 return 0;
1430 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001431
Mike Stump31feda52009-07-17 01:31:16 +00001432 // If we have no "default:" case, the default transition is to the code
1433 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001434 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001435
Ted Kremenek81e14852007-08-27 19:46:09 +00001436 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001437 SwitchTerminatedBlock->setTerminator(Terminator);
1438 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001439 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001440 Block = addStmt(Terminator->getCond());
1441
1442 // Finally, if the SwitchStmt contains a condition variable, add both the
1443 // SwitchStmt and the condition variable initialization to the CFG.
1444 if (VarDecl *VD = Terminator->getConditionVariable()) {
1445 if (Expr *Init = VD->getInit()) {
1446 autoCreateBlock();
1447 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1448 addStmt(Init);
1449 }
1450 }
1451
1452 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001453}
1454
Ted Kremenek93668002009-07-17 22:18:43 +00001455CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001456 // CaseStmts are essentially labels, so they are the first statement in a
1457 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001458
Ted Kremenek93668002009-07-17 22:18:43 +00001459 if (CS->getSubStmt())
1460 addStmt(CS->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001461
Ted Kremenek55e91e82007-08-30 18:48:11 +00001462 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001463 if (!CaseBlock)
1464 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001465
1466 // Cases statements partition blocks, so this is the top of the basic block we
1467 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001468 CaseBlock->setLabel(CS);
1469
Ted Kremenek55957a82009-05-02 00:13:27 +00001470 if (!FinishBlock(CaseBlock))
1471 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001472
1473 // Add this block to the list of successors for the block with the switch
1474 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001475 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001476 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001477
Ted Kremenek9aae5132007-08-23 21:42:29 +00001478 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1479 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001480
Ted Kremenek9aae5132007-08-23 21:42:29 +00001481 // This block is now the implicit successor of other blocks.
1482 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001483
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001484 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001485}
Mike Stump31feda52009-07-17 01:31:16 +00001486
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001487CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001488 if (Terminator->getSubStmt())
1489 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001490
Ted Kremenek654c78f2008-02-13 22:05:39 +00001491 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001492
1493 if (!DefaultCaseBlock)
1494 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001495
1496 // Default statements partition blocks, so this is the top of the basic block
1497 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001498 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001499
Ted Kremenek55957a82009-05-02 00:13:27 +00001500 if (!FinishBlock(DefaultCaseBlock))
1501 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001502
Mike Stump31feda52009-07-17 01:31:16 +00001503 // Unlike case statements, we don't add the default block to the successors
1504 // for the switch statement immediately. This is done when we finish
1505 // processing the switch statement. This allows for the default case
1506 // (including a fall-through to the code after the switch statement) to always
1507 // be the last successor of a switch-terminated block.
1508
Ted Kremenek654c78f2008-02-13 22:05:39 +00001509 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1510 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001511
Ted Kremenek654c78f2008-02-13 22:05:39 +00001512 // This block is now the implicit successor of other blocks.
1513 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001514
1515 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001516}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001517
Ted Kremenekeda180e22007-08-28 19:26:49 +00001518CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001519 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001520 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001521
Ted Kremenekeda180e22007-08-28 19:26:49 +00001522 if (!IBlock) {
1523 IBlock = createBlock(false);
1524 cfg->setIndirectGotoBlock(IBlock);
1525 }
Mike Stump31feda52009-07-17 01:31:16 +00001526
Ted Kremenekeda180e22007-08-28 19:26:49 +00001527 // IndirectGoto is a control-flow statement. Thus we stop processing the
1528 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001529 if (Block && !FinishBlock(Block))
1530 return 0;
1531
Ted Kremenekeda180e22007-08-28 19:26:49 +00001532 Block = createBlock(false);
1533 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001534 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001535 return addStmt(I->getTarget());
1536}
1537
Ted Kremenek04cca642007-08-23 21:26:19 +00001538} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001539
Mike Stump31feda52009-07-17 01:31:16 +00001540/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1541/// no successors or predecessors. If this is the first block created in the
1542/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001543CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001544 bool first_block = begin() == end();
1545
1546 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001547 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1548 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1549 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001550
1551 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001552 if (first_block)
1553 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001554
1555 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001556 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001557}
1558
Ted Kremenek889073f2007-08-23 16:51:22 +00001559/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1560/// CFG is returned to the caller.
Mike Stump0d76d072009-07-20 23:24:15 +00001561CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001562 CFGBuilder Builder;
Mike Stump0d76d072009-07-20 23:24:15 +00001563 return Builder.buildCFG(Statement, C);
Ted Kremenek889073f2007-08-23 16:51:22 +00001564}
1565
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001566//===----------------------------------------------------------------------===//
1567// CFG: Queries for BlkExprs.
1568//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001569
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001570namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001571 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001572}
1573
Ted Kremenekbff98442009-12-23 23:37:10 +00001574static void FindSubExprAssignments(Stmt *S,
1575 llvm::SmallPtrSet<Expr*,50>& Set) {
1576 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001577 return;
Mike Stump31feda52009-07-17 01:31:16 +00001578
Ted Kremenekbff98442009-12-23 23:37:10 +00001579 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1580 Stmt *child = *I;
1581 if (!child)
1582 continue;
1583
1584 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001585 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001586
Ted Kremenekbff98442009-12-23 23:37:10 +00001587 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00001588 }
1589}
1590
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001591static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1592 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001593
1594 // Look for assignments that are used as subexpressions. These are the only
1595 // assignments that we want to *possibly* register as a block-level
1596 // expression. Basically, if an assignment occurs both in a subexpression and
1597 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001598 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001599
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001600 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001601 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001602 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001603
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001604 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001605
1606 // Iterate over the statements again on identify the Expr* and Stmt* at the
1607 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001608
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001609 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001610 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001611
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001612 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001613 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001614 // expression are really "statements" whose value is never used by
1615 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001616 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001617 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001618 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1619 // Special handling for statement expressions. The last statement in
1620 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001621 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001622 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001623 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001624 (*M)[C->body_back()] = x;
1625 }
1626 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001627
Ted Kremenek95a123c2008-01-26 00:03:27 +00001628 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001629 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001630 }
Mike Stump31feda52009-07-17 01:31:16 +00001631
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001632 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001633
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001634 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001635
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001636 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001637 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001638 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001639 }
1640 }
Mike Stump31feda52009-07-17 01:31:16 +00001641
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001642 return M;
1643}
1644
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001645CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1646 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001647 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001648
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001649 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001650 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001651
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001652 if (I == M->end()) return CFG::BlkExprNumTy();
1653 else return CFG::BlkExprNumTy(I->second);
1654}
1655
1656unsigned CFG::getNumBlkExprs() {
1657 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1658 return M->size();
1659 else {
1660 // We assume callers interested in the number of BlkExprs will want
1661 // the map constructed if it doesn't already exist.
1662 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1663 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1664 }
1665}
1666
Ted Kremenek6065ef62008-04-28 18:00:46 +00001667//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001668// Cleanup: CFG dstor.
1669//===----------------------------------------------------------------------===//
1670
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001671CFG::~CFG() {
1672 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1673}
Mike Stump31feda52009-07-17 01:31:16 +00001674
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001675//===----------------------------------------------------------------------===//
1676// CFG pretty printing
1677//===----------------------------------------------------------------------===//
1678
Ted Kremenek7e776b12007-08-22 18:22:34 +00001679namespace {
1680
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001681class StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001682
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001683 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1684 StmtMapTy StmtMap;
1685 signed CurrentBlock;
1686 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001687 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001688public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001689
Chris Lattnerc61089a2009-06-30 01:26:17 +00001690 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1691 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001692 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1693 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001694 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001695 BI != BEnd; ++BI, ++j )
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001696 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001697 }
1698 }
Mike Stump31feda52009-07-17 01:31:16 +00001699
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001700 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001701
Chris Lattnerc61089a2009-06-30 01:26:17 +00001702 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001703 void setBlockID(signed i) { CurrentBlock = i; }
1704 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001705
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001706 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001707
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001708 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001709
1710 if (I == StmtMap.end())
1711 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001712
1713 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001714 && I->second.second == CurrentStmt)
1715 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001716
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001717 OS << "[B" << I->second.first << "." << I->second.second << "]";
1718 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001719 }
1720};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001721} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001722
Chris Lattnerc61089a2009-06-30 01:26:17 +00001723
1724namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001725class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001726 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001727
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001728 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001729 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001730 PrintingPolicy Policy;
1731
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001732public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001733 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001734 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001735 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001736
Ted Kremenek9aae5132007-08-23 21:42:29 +00001737 void VisitIfStmt(IfStmt* I) {
1738 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001739 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001740 }
Mike Stump31feda52009-07-17 01:31:16 +00001741
Ted Kremenek9aae5132007-08-23 21:42:29 +00001742 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001743 void VisitStmt(Stmt* Terminator) {
1744 Terminator->printPretty(OS, Helper, Policy);
1745 }
1746
Ted Kremenek9aae5132007-08-23 21:42:29 +00001747 void VisitForStmt(ForStmt* F) {
1748 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001749 if (F->getInit()) OS << "...";
1750 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001751 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001752 OS << "; ";
1753 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001754 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001755 }
Mike Stump31feda52009-07-17 01:31:16 +00001756
Ted Kremenek9aae5132007-08-23 21:42:29 +00001757 void VisitWhileStmt(WhileStmt* W) {
1758 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001759 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001760 }
Mike Stump31feda52009-07-17 01:31:16 +00001761
Ted Kremenek9aae5132007-08-23 21:42:29 +00001762 void VisitDoStmt(DoStmt* D) {
1763 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001764 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001765 }
Mike Stump31feda52009-07-17 01:31:16 +00001766
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001767 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001768 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001769 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001770 }
Mike Stump31feda52009-07-17 01:31:16 +00001771
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001772 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001773 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001774 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001775 }
Mike Stump31feda52009-07-17 01:31:16 +00001776
Ted Kremenek391f94a2007-08-31 22:29:13 +00001777 void VisitChooseExpr(ChooseExpr* C) {
1778 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001779 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001780 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001781 }
Mike Stump31feda52009-07-17 01:31:16 +00001782
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001783 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1784 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001785 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001786 }
Mike Stump31feda52009-07-17 01:31:16 +00001787
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001788 void VisitBinaryOperator(BinaryOperator* B) {
1789 if (!B->isLogicalOp()) {
1790 VisitExpr(B);
1791 return;
1792 }
Mike Stump31feda52009-07-17 01:31:16 +00001793
Douglas Gregor7de59662009-05-29 20:38:28 +00001794 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001795
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001796 switch (B->getOpcode()) {
1797 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001798 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001799 return;
1800 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001801 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001802 return;
1803 default:
1804 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001805 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001806 }
Mike Stump31feda52009-07-17 01:31:16 +00001807
Ted Kremenek12687ff2007-08-27 21:54:41 +00001808 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001809 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001810 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001811};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001812} // end anonymous namespace
1813
Mike Stump31feda52009-07-17 01:31:16 +00001814
Chris Lattnerc61089a2009-06-30 01:26:17 +00001815static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1816 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001817 if (Helper) {
1818 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001819 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001820 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001821
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001822 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001823 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001824 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001825 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001826 return;
1827 }
1828 }
Mike Stump31feda52009-07-17 01:31:16 +00001829
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001830 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001831 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001832 if (B->getOpcode() == BinaryOperator::Comma) {
1833 OS << "... , ";
1834 Helper->handledStmt(B->getRHS(),OS);
1835 OS << '\n';
1836 return;
Mike Stump31feda52009-07-17 01:31:16 +00001837 }
1838 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001839 }
Mike Stump31feda52009-07-17 01:31:16 +00001840
Chris Lattnerc61089a2009-06-30 01:26:17 +00001841 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001842
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001843 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001844 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001845}
Mike Stump31feda52009-07-17 01:31:16 +00001846
Chris Lattnerc61089a2009-06-30 01:26:17 +00001847static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1848 const CFGBlock& B,
1849 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001850
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001851 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001852
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001853 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001854 OS << "\n [ B" << B.getBlockID();
1855
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001856 if (&B == &cfg->getEntry())
1857 OS << " (ENTRY) ]\n";
1858 else if (&B == &cfg->getExit())
1859 OS << " (EXIT) ]\n";
1860 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001861 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001862 else
1863 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001864
Ted Kremenek71eca012007-08-29 23:20:49 +00001865 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001866 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001867
1868 if (print_edges)
1869 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001870
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001871 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001872 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001873 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001874 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001875 C->getLHS()->printPretty(OS, Helper,
1876 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001877 if (C->getRHS()) {
1878 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001879 C->getRHS()->printPretty(OS, Helper,
1880 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001881 }
Mike Stump31feda52009-07-17 01:31:16 +00001882 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001883 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001884 else
1885 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001886
Ted Kremenek71eca012007-08-29 23:20:49 +00001887 OS << ":\n";
1888 }
Mike Stump31feda52009-07-17 01:31:16 +00001889
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001890 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001891 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001892
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001893 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1894 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001895
Ted Kremenek71eca012007-08-29 23:20:49 +00001896 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001897 if (print_edges)
1898 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001899
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001900 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001901
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001902 if (Helper)
1903 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001904
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001905 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001906 }
Mike Stump31feda52009-07-17 01:31:16 +00001907
Ted Kremenek71eca012007-08-29 23:20:49 +00001908 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001909 if (B.getTerminator()) {
1910 if (print_edges)
1911 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001912
Ted Kremenek71eca012007-08-29 23:20:49 +00001913 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001914
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001915 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001916
Chris Lattnerc61089a2009-06-30 01:26:17 +00001917 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1918 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001919 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001920 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001921 }
Mike Stump31feda52009-07-17 01:31:16 +00001922
Ted Kremenek71eca012007-08-29 23:20:49 +00001923 if (print_edges) {
1924 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001925 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001926 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001927
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001928 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1929 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001930
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001931 if (i == 8 || (i-8) == 0)
1932 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001933
Ted Kremenek71eca012007-08-29 23:20:49 +00001934 OS << " B" << (*I)->getBlockID();
1935 }
Mike Stump31feda52009-07-17 01:31:16 +00001936
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001937 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001938
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001939 // Print the successors of this block.
1940 OS << " Successors (" << B.succ_size() << "):";
1941 i = 0;
1942
1943 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1944 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001945
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001946 if (i == 8 || (i-8) % 10 == 0)
1947 OS << "\n ";
1948
Mike Stump0d76d072009-07-20 23:24:15 +00001949 if (*I)
1950 OS << " B" << (*I)->getBlockID();
1951 else
1952 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001953 }
Mike Stump31feda52009-07-17 01:31:16 +00001954
Ted Kremenek71eca012007-08-29 23:20:49 +00001955 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001956 }
Mike Stump31feda52009-07-17 01:31:16 +00001957}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001958
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001959
1960/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001961void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001962
1963/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001964void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1965 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001966
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001967 // Print the entry block.
1968 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001969
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001970 // Iterate through the CFGBlocks and print them one by one.
1971 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1972 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001973 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001974 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001975
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001976 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001977 }
Mike Stump31feda52009-07-17 01:31:16 +00001978
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001979 // Print the exit block.
1980 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001981 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001982}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001983
1984/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001985void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1986 print(llvm::errs(), cfg, LO);
1987}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001988
1989/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1990/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001991void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1992 const LangOptions &LO) const {
1993 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001994 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00001995}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001996
Ted Kremenek15647632008-01-30 23:02:42 +00001997/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001998void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00001999 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002000 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002001 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2002}
2003
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002004Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002005
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002006 if (!Terminator)
2007 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002008
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002009 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002010
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002011 switch (Terminator->getStmtClass()) {
2012 default:
2013 break;
Mike Stump31feda52009-07-17 01:31:16 +00002014
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002015 case Stmt::ForStmtClass:
2016 E = cast<ForStmt>(Terminator)->getCond();
2017 break;
Mike Stump31feda52009-07-17 01:31:16 +00002018
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002019 case Stmt::WhileStmtClass:
2020 E = cast<WhileStmt>(Terminator)->getCond();
2021 break;
Mike Stump31feda52009-07-17 01:31:16 +00002022
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002023 case Stmt::DoStmtClass:
2024 E = cast<DoStmt>(Terminator)->getCond();
2025 break;
Mike Stump31feda52009-07-17 01:31:16 +00002026
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002027 case Stmt::IfStmtClass:
2028 E = cast<IfStmt>(Terminator)->getCond();
2029 break;
Mike Stump31feda52009-07-17 01:31:16 +00002030
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002031 case Stmt::ChooseExprClass:
2032 E = cast<ChooseExpr>(Terminator)->getCond();
2033 break;
Mike Stump31feda52009-07-17 01:31:16 +00002034
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002035 case Stmt::IndirectGotoStmtClass:
2036 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2037 break;
Mike Stump31feda52009-07-17 01:31:16 +00002038
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002039 case Stmt::SwitchStmtClass:
2040 E = cast<SwitchStmt>(Terminator)->getCond();
2041 break;
Mike Stump31feda52009-07-17 01:31:16 +00002042
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002043 case Stmt::ConditionalOperatorClass:
2044 E = cast<ConditionalOperator>(Terminator)->getCond();
2045 break;
Mike Stump31feda52009-07-17 01:31:16 +00002046
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002047 case Stmt::BinaryOperatorClass: // '&&' and '||'
2048 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002049 break;
Mike Stump31feda52009-07-17 01:31:16 +00002050
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002051 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002052 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002053 }
Mike Stump31feda52009-07-17 01:31:16 +00002054
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002055 return E ? E->IgnoreParens() : NULL;
2056}
2057
Ted Kremenek92137a32008-05-16 16:06:00 +00002058bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002059
Ted Kremenek92137a32008-05-16 16:06:00 +00002060 if (!Terminator)
2061 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002062
Ted Kremenek92137a32008-05-16 16:06:00 +00002063 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002064
Ted Kremenek92137a32008-05-16 16:06:00 +00002065 switch (Terminator->getStmtClass()) {
2066 default:
2067 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002068
2069 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002070 case Stmt::WhileStmtClass:
2071 case Stmt::DoStmtClass:
2072 case Stmt::IfStmtClass:
2073 case Stmt::ChooseExprClass:
2074 case Stmt::ConditionalOperatorClass:
2075 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002076 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002077 }
Mike Stump31feda52009-07-17 01:31:16 +00002078
Ted Kremenek92137a32008-05-16 16:06:00 +00002079 return E ? E->IgnoreParens() : NULL;
2080}
2081
Ted Kremenek15647632008-01-30 23:02:42 +00002082
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002083//===----------------------------------------------------------------------===//
2084// CFG Graphviz Visualization
2085//===----------------------------------------------------------------------===//
2086
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002087
2088#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002089static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002090#endif
2091
Chris Lattnerc61089a2009-06-30 01:26:17 +00002092void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002093#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002094 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002095 GraphHelper = &H;
2096 llvm::ViewGraph(this,"CFG");
2097 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002098#endif
2099}
2100
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002101namespace llvm {
2102template<>
2103struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002104
2105 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2106
2107 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002108
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002109#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002110 std::string OutSStr;
2111 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002112 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002113 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002114
2115 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2116
2117 // Process string output to make it nicer...
2118 for (unsigned i = 0; i != OutStr.length(); ++i)
2119 if (OutStr[i] == '\n') { // Left justify
2120 OutStr[i] = '\\';
2121 OutStr.insert(OutStr.begin()+i+1, 'l');
2122 }
Mike Stump31feda52009-07-17 01:31:16 +00002123
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002124 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002125#else
2126 return "";
2127#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002128 }
2129};
2130} // end namespace llvm