blob: 7b1d50cb3aeeadc1c1a07227a22495f4be374571 [file] [log] [blame]
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekb1c170e2009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek6796fbd2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000019#include "llvm/Support/GraphWriter.h"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted 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}
Mike Stump31feda52009-07-17 01:31:16 +000037
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000038/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000039/// The builder is stateful: an instance of the builder should be used to only
40/// construct a single CFG.
41///
42/// Example usage:
43///
44/// CFGBuilder builder;
45/// CFG* cfg = builder.BuildAST(stmt1);
46///
Mike Stump31feda52009-07-17 01:31:16 +000047/// CFG construction is done via a recursive walk of an AST. We actually parse
48/// the AST in reverse order so that the successor of a basic block is
49/// constructed prior to its predecessor. This allows us to nicely capture
50/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000051///
Ted Kremenek93668002009-07-17 22:18:43 +000052class VISIBILITY_HIDDEN CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000053 ASTContext *Context;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000054 CFG* cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +000055
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000056 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000057 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000058 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000059 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000060 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000061 CFGBlock* DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +000062
Ted Kremenekeda180e22007-08-28 19:26:49 +000063 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000064 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
65 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000066
67 // A list of blocks that end with a "goto" that must be backpatched to their
68 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000069 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000070 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000071
Ted Kremenekeda180e22007-08-28 19:26:49 +000072 // A list of labels whose address has been taken (for indirect gotos).
73 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
74 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000075
76public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +000077 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
78 Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000079 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek289ae4f2009-10-12 20:55:07 +000080 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {}
Mike Stump31feda52009-07-17 01:31:16 +000081
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000082 ~CFGBuilder() { delete cfg; }
Mike Stump31feda52009-07-17 01:31:16 +000083
Ted Kremenek9aae5132007-08-23 21:42:29 +000084 // buildCFG - Used by external clients to construct the CFG.
Mike Stump0d76d072009-07-20 23:24:15 +000085 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump31feda52009-07-17 01:31:16 +000086
Ted Kremenek93668002009-07-17 22:18:43 +000087private:
88 // Visitors to walk an AST and construct the CFG.
89 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
90 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
Ted Kremenek0747de62009-07-18 00:47:21 +000091 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
92 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +000093 CFGBlock *VisitBreakStmt(BreakStmt *B);
94 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
95 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek21822592009-07-17 18:20:32 +000096 CFGBlock *VisitChooseExpr(ChooseExpr *C);
97 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
98 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
99 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000100 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenek93668002009-07-17 22:18:43 +0000101 CFGBlock *VisitDeclStmt(DeclStmt *DS);
102 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000103 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
104 CFGBlock *VisitDoStmt(DoStmt *D);
105 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000106 CFGBlock *VisitGotoStmt(GotoStmt* G);
107 CFGBlock *VisitIfStmt(IfStmt *I);
108 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
109 CFGBlock *VisitLabelStmt(LabelStmt *L);
110 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
111 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
112 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
113 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
114 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
115 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek0747de62009-07-18 00:47:21 +0000116 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
117 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000118 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
119 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000120
Ted Kremenek93668002009-07-17 22:18:43 +0000121 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
122 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
123 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000124
Ted Kremenek6065ef62008-04-28 18:00:46 +0000125 // NYS == Not Yet Supported
126 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000127 badCFG = true;
128 return Block;
129 }
Mike Stump31feda52009-07-17 01:31:16 +0000130
Ted Kremenek93668002009-07-17 22:18:43 +0000131 void autoCreateBlock() { if (!Block) Block = createBlock(); }
132 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000133 bool FinishBlock(CFGBlock* B);
Ted Kremenek93668002009-07-17 22:18:43 +0000134 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000135
136 void AppendStmt(CFGBlock *B, Stmt *S) {
137 B->appendStmt(S, cfg->getBumpVectorContext());
138 }
139
140 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
141 B->addSuccessor(S, cfg->getBumpVectorContext());
142 }
Mike Stump11289f42009-09-09 15:08:12 +0000143
Ted Kremenek963cc312009-07-24 06:55:42 +0000144 /// TryResult - a class representing a variant over the values
145 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
146 /// and is used by the CFGBuilder to decide if a branch condition
147 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000148 class TryResult {
149 int X;
150 public:
151 TryResult(bool b) : X(b ? 1 : 0) {}
152 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000153
Ted Kremenek30754282009-07-24 04:47:11 +0000154 bool isTrue() const { return X == 1; }
155 bool isFalse() const { return X == 0; }
156 bool isKnown() const { return X >= 0; }
157 void negate() {
158 assert(isKnown());
159 X ^= 0x1;
160 }
161 };
Mike Stump11289f42009-09-09 15:08:12 +0000162
Mike Stump773582d2009-07-23 23:25:26 +0000163 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
164 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000165 TryResult TryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000166 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000167 if (!S->isTypeDependent() && !S->isValueDependent() &&
168 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000169 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000170
171 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000172 }
173
Ted Kremenekb64d1832008-03-13 03:04:22 +0000174 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000175};
Mike Stump31feda52009-07-17 01:31:16 +0000176
Douglas Gregor4619e432008-12-05 23:32:09 +0000177// FIXME: Add support for dependent-sized array types in C++?
178// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000179static VariableArrayType* FindVA(Type* t) {
180 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
181 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
182 if (vat->getSizeExpr())
183 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000184
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000185 t = vt->getElementType().getTypePtr();
186 }
Mike Stump31feda52009-07-17 01:31:16 +0000187
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000188 return 0;
189}
Mike Stump31feda52009-07-17 01:31:16 +0000190
191/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
192/// arbitrary statement. Examples include a single expression or a function
193/// body (compound statement). The ownership of the returned CFG is
194/// transferred to the caller. If CFG construction fails, this method returns
195/// NULL.
Mike Stump0d76d072009-07-20 23:24:15 +0000196CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
197 Context = C;
Ted Kremenek93668002009-07-17 22:18:43 +0000198 assert(cfg);
199 if (!Statement)
200 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000201
Ted Kremenekb64d1832008-03-13 03:04:22 +0000202 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000203
204 // Create an empty block that will serve as the exit block for the CFG. Since
205 // this is the first block added to the CFG, it will be implicitly registered
206 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000207 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000208 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000209 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000210
Ted Kremenek9aae5132007-08-23 21:42:29 +0000211 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000212 CFGBlock* B = addStmt(Statement);
Ted Kremenek40d87632008-02-27 17:33:02 +0000213 if (!B) B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000214
Ted Kremenek40d87632008-02-27 17:33:02 +0000215 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000216 // Finalize the last constructed block. This usually involves reversing the
217 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000218 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000219
220 // Backpatch the gotos whose label -> block mappings we didn't know when we
221 // encountered them.
222 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000223 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000224
Ted Kremenek9aae5132007-08-23 21:42:29 +0000225 CFGBlock* B = *I;
226 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
227 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
228
229 // If there is no target for the goto, then we are looking at an
230 // incomplete AST. Handle this by not registering a successor.
231 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000232
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000233 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000234 }
Mike Stump31feda52009-07-17 01:31:16 +0000235
Ted Kremenekeda180e22007-08-28 19:26:49 +0000236 // Add successors to the Indirect Goto Dispatch block (if we have one).
237 if (CFGBlock* B = cfg->getIndirectGotoBlock())
238 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
239 E = AddressTakenLabels.end(); I != E; ++I ) {
240
241 // Lookup the target block.
242 LabelMapTy::iterator LI = LabelMap.find(*I);
243
244 // If there is no target block that contains label, then we are looking
245 // at an incomplete AST. Handle this by not registering a successor.
246 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000247
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000248 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000249 }
Mike Stump31feda52009-07-17 01:31:16 +0000250
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000251 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000252 }
Mike Stump31feda52009-07-17 01:31:16 +0000253
254 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000255 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000256
Ted Kremenekb64d1832008-03-13 03:04:22 +0000257 if (badCFG) {
258 delete cfg;
259 cfg = NULL;
260 return NULL;
261 }
Mike Stump31feda52009-07-17 01:31:16 +0000262
263 // NULL out cfg so that repeated calls to the builder will fail and that the
264 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000265 CFG* t = cfg;
266 cfg = NULL;
267 return t;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000268}
Mike Stump31feda52009-07-17 01:31:16 +0000269
Ted Kremenek9aae5132007-08-23 21:42:29 +0000270/// createBlock - Used to lazily create blocks that are connected
271/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000272CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000273 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000274 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000275 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000276 return B;
277}
Mike Stump31feda52009-07-17 01:31:16 +0000278
Ted Kremenek0868eea2009-09-24 18:45:41 +0000279/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek55957a82009-05-02 00:13:27 +0000280bool CFGBuilder::FinishBlock(CFGBlock* B) {
281 if (badCFG)
282 return false;
283
Ted Kremenek93668002009-07-17 22:18:43 +0000284 assert(B);
Ted Kremenek55957a82009-05-02 00:13:27 +0000285 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000286}
287
Ted Kremenek93668002009-07-17 22:18:43 +0000288/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000289/// blocks for ternary operators, &&, and ||. We also process "," and
290/// DeclStmts (which may contain nested control-flow).
Ted Kremenek93668002009-07-17 22:18:43 +0000291CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
292tryAgain:
293 switch (S->getStmtClass()) {
294 default:
295 return VisitStmt(S, alwaysAdd);
296
297 case Stmt::AddrLabelExprClass:
298 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000299
Ted Kremenek93668002009-07-17 22:18:43 +0000300 case Stmt::BinaryOperatorClass:
301 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000302
Ted Kremenek93668002009-07-17 22:18:43 +0000303 case Stmt::BlockExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000304 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000305
306 case Stmt::BlockDeclRefExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000307 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000308
Ted Kremenek93668002009-07-17 22:18:43 +0000309 case Stmt::BreakStmtClass:
310 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000311
Ted Kremenek93668002009-07-17 22:18:43 +0000312 case Stmt::CallExprClass:
313 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000314
Ted Kremenek93668002009-07-17 22:18:43 +0000315 case Stmt::CaseStmtClass:
316 return VisitCaseStmt(cast<CaseStmt>(S));
317
318 case Stmt::ChooseExprClass:
319 return VisitChooseExpr(cast<ChooseExpr>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000320
Ted Kremenek93668002009-07-17 22:18:43 +0000321 case Stmt::CompoundStmtClass:
322 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000323
Ted Kremenek93668002009-07-17 22:18:43 +0000324 case Stmt::ConditionalOperatorClass:
325 return VisitConditionalOperator(cast<ConditionalOperator>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000326
Ted Kremenek93668002009-07-17 22:18:43 +0000327 case Stmt::ContinueStmtClass:
328 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000329
Ted Kremenek93668002009-07-17 22:18:43 +0000330 case Stmt::DeclStmtClass:
331 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000332
Ted Kremenek93668002009-07-17 22:18:43 +0000333 case Stmt::DefaultStmtClass:
334 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000335
Ted Kremenek93668002009-07-17 22:18:43 +0000336 case Stmt::DoStmtClass:
337 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000338
Ted Kremenek93668002009-07-17 22:18:43 +0000339 case Stmt::ForStmtClass:
340 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000341
Ted Kremenek93668002009-07-17 22:18:43 +0000342 case Stmt::GotoStmtClass:
343 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000344
Ted Kremenek93668002009-07-17 22:18:43 +0000345 case Stmt::IfStmtClass:
346 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000347
Ted Kremenek93668002009-07-17 22:18:43 +0000348 case Stmt::IndirectGotoStmtClass:
349 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000350
Ted Kremenek93668002009-07-17 22:18:43 +0000351 case Stmt::LabelStmtClass:
352 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000353
Ted Kremenek93668002009-07-17 22:18:43 +0000354 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000355 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
356
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000357 case Stmt::CXXThrowExprClass:
358 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
359
Ted Kremenek93668002009-07-17 22:18:43 +0000360 case Stmt::ObjCAtSynchronizedStmtClass:
361 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000362
Ted Kremenek93668002009-07-17 22:18:43 +0000363 case Stmt::ObjCAtThrowStmtClass:
364 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000365
Ted Kremenek93668002009-07-17 22:18:43 +0000366 case Stmt::ObjCAtTryStmtClass:
367 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000368
Ted Kremenek93668002009-07-17 22:18:43 +0000369 case Stmt::ObjCForCollectionStmtClass:
370 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000371
Ted Kremenek93668002009-07-17 22:18:43 +0000372 case Stmt::ParenExprClass:
373 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000374 goto tryAgain;
375
Ted Kremenek93668002009-07-17 22:18:43 +0000376 case Stmt::NullStmtClass:
377 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000378
Ted Kremenek93668002009-07-17 22:18:43 +0000379 case Stmt::ReturnStmtClass:
380 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000381
Ted Kremenek93668002009-07-17 22:18:43 +0000382 case Stmt::SizeOfAlignOfExprClass:
Mike Stump11289f42009-09-09 15:08:12 +0000383 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
384
Ted Kremenek93668002009-07-17 22:18:43 +0000385 case Stmt::StmtExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000386 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000387
Ted Kremenek93668002009-07-17 22:18:43 +0000388 case Stmt::SwitchStmtClass:
389 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000390
Ted Kremenek93668002009-07-17 22:18:43 +0000391 case Stmt::WhileStmtClass:
392 return VisitWhileStmt(cast<WhileStmt>(S));
393 }
394}
Mike Stump11289f42009-09-09 15:08:12 +0000395
Ted Kremenek93668002009-07-17 22:18:43 +0000396CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
397 if (alwaysAdd) {
398 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000399 AppendStmt(Block, S);
Mike Stump31feda52009-07-17 01:31:16 +0000400 }
Mike Stump11289f42009-09-09 15:08:12 +0000401
Ted Kremenek93668002009-07-17 22:18:43 +0000402 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000403}
Mike Stump31feda52009-07-17 01:31:16 +0000404
Ted Kremenek93668002009-07-17 22:18:43 +0000405/// VisitChildren - Visit the children of a Stmt.
406CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
407 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000408 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000409 E = Terminator->child_end(); I != E; ++I) {
410 if (*I) B = Visit(*I);
411 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000412 return B;
413}
Mike Stump11289f42009-09-09 15:08:12 +0000414
Ted Kremenek93668002009-07-17 22:18:43 +0000415CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
416 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000417
Ted Kremenek93668002009-07-17 22:18:43 +0000418 if (alwaysAdd) {
419 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000420 AppendStmt(Block, A);
Ted Kremenek93668002009-07-17 22:18:43 +0000421 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000422
Ted Kremenek9aae5132007-08-23 21:42:29 +0000423 return Block;
424}
Mike Stump11289f42009-09-09 15:08:12 +0000425
Ted Kremenek93668002009-07-17 22:18:43 +0000426CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
427 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000428 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000429 AppendStmt(ConfluenceBlock, B);
Mike Stump11289f42009-09-09 15:08:12 +0000430
Ted Kremenek93668002009-07-17 22:18:43 +0000431 if (!FinishBlock(ConfluenceBlock))
432 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000433
Ted Kremenek93668002009-07-17 22:18:43 +0000434 // create the block evaluating the LHS
435 CFGBlock* LHSBlock = createBlock(false);
436 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000437
Ted Kremenek93668002009-07-17 22:18:43 +0000438 // create the block evaluating the RHS
439 Succ = ConfluenceBlock;
440 Block = NULL;
441 CFGBlock* RHSBlock = addStmt(B->getRHS());
442 if (!FinishBlock(RHSBlock))
443 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000444
Mike Stump773582d2009-07-23 23:25:26 +0000445 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000446 TryResult KnownVal = TryEvaluateBool(B->getLHS());
447 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
448 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000449
Ted Kremenek93668002009-07-17 22:18:43 +0000450 // Now link the LHSBlock with RHSBlock.
451 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000452 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
453 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000454 } else {
Ted Kremenek93668002009-07-17 22:18:43 +0000455 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000456 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
457 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000458 }
Mike Stump11289f42009-09-09 15:08:12 +0000459
Ted Kremenek93668002009-07-17 22:18:43 +0000460 // Generate the blocks for evaluating the LHS.
461 Block = LHSBlock;
462 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000463 }
Ted Kremenek93668002009-07-17 22:18:43 +0000464 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000465 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000466 AppendStmt(Block, B);
Ted Kremenek93668002009-07-17 22:18:43 +0000467 addStmt(B->getRHS());
468 return addStmt(B->getLHS());
469 }
Mike Stump11289f42009-09-09 15:08:12 +0000470
Ted Kremenek93668002009-07-17 22:18:43 +0000471 return VisitStmt(B, alwaysAdd);
472}
473
Ted Kremenek0747de62009-07-18 00:47:21 +0000474CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E, bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000475 // FIXME
476 return NYS();
477}
478
Ted Kremenek0747de62009-07-18 00:47:21 +0000479CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
480 bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000481 // FIXME
482 return NYS();
483}
Mike Stump11289f42009-09-09 15:08:12 +0000484
Ted Kremenek93668002009-07-17 22:18:43 +0000485CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
486 // "break" is a control-flow statement. Thus we stop processing the current
487 // block.
488 if (Block && !FinishBlock(Block))
489 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000490
Ted Kremenek93668002009-07-17 22:18:43 +0000491 // Now create a new block that ends with the break statement.
492 Block = createBlock(false);
493 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000494
Ted Kremenek93668002009-07-17 22:18:43 +0000495 // If there is no target for the break, then we are looking at an incomplete
496 // AST. This means that the CFG cannot be constructed.
497 if (BreakTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000498 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000499 else
500 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000501
502
Ted Kremenek9aae5132007-08-23 21:42:29 +0000503 return Block;
504}
Mike Stump11289f42009-09-09 15:08:12 +0000505
Ted Kremenek93668002009-07-17 22:18:43 +0000506CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
507 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000508 bool NoReturn = false;
509 if (C->getCallee()->getType().getNoReturnAttr()) {
510 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000511 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000512
513 if (FunctionDecl *FD = C->getDirectCallee())
514 if (FD->hasAttr<NoReturnAttr>())
515 NoReturn = true;
516
517 if (!NoReturn)
518 return VisitStmt(C, alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000519
Mike Stump8c5d7992009-07-25 21:26:53 +0000520 if (Block && !FinishBlock(Block))
521 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000522
Mike Stump8c5d7992009-07-25 21:26:53 +0000523 // Create new block with no successor for the remaining pieces.
524 Block = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000525 AppendStmt(Block, C);
Mike Stump8c5d7992009-07-25 21:26:53 +0000526
527 // Wire this to the exit block directly.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000528 AddSuccessor(Block, &cfg->getExit());
Mike Stump11289f42009-09-09 15:08:12 +0000529
Mike Stump8c5d7992009-07-25 21:26:53 +0000530 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000531}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000532
Ted Kremenek21822592009-07-17 18:20:32 +0000533CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
534 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000535 AppendStmt(ConfluenceBlock, C);
Ted Kremenek21822592009-07-17 18:20:32 +0000536 if (!FinishBlock(ConfluenceBlock))
537 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000538
Ted Kremenek21822592009-07-17 18:20:32 +0000539 Succ = ConfluenceBlock;
540 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000541 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000542 if (!FinishBlock(LHSBlock))
543 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000544
Ted Kremenek21822592009-07-17 18:20:32 +0000545 Succ = ConfluenceBlock;
546 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000547 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000548 if (!FinishBlock(RHSBlock))
549 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000550
Ted Kremenek21822592009-07-17 18:20:32 +0000551 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000552 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000553 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000554 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
555 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000556 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000557 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000558}
Mike Stump11289f42009-09-09 15:08:12 +0000559
560
561CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
562 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000563
564 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
565 I != E; ++I ) {
566 LastBlock = addStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000567
Ted Kremenekce499c22009-08-27 23:16:26 +0000568 if (badCFG)
569 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000570 }
Ted Kremenek93668002009-07-17 22:18:43 +0000571 return LastBlock;
572}
Mike Stump11289f42009-09-09 15:08:12 +0000573
Ted Kremenek51d40b02009-07-17 18:15:54 +0000574CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
575 // Create the confluence block that will "merge" the results of the ternary
576 // expression.
577 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000578 AppendStmt(ConfluenceBlock, C);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000579 if (!FinishBlock(ConfluenceBlock))
580 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000581
Ted Kremenek51d40b02009-07-17 18:15:54 +0000582 // Create a block for the LHS expression if there is an LHS expression. A
583 // GCC extension allows LHS to be NULL, causing the condition to be the
584 // value that is returned instead.
585 // e.g: x ?: y is shorthand for: x ? x : y;
586 Succ = ConfluenceBlock;
587 Block = NULL;
588 CFGBlock* LHSBlock = NULL;
589 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000590 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000591 if (!FinishBlock(LHSBlock))
592 return 0;
593 Block = NULL;
594 }
Mike Stump11289f42009-09-09 15:08:12 +0000595
Ted Kremenek51d40b02009-07-17 18:15:54 +0000596 // Create the block for the RHS expression.
597 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000598 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000599 if (!FinishBlock(RHSBlock))
600 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000601
Ted Kremenek51d40b02009-07-17 18:15:54 +0000602 // Create the block that will contain the condition.
603 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000604
Mike Stump773582d2009-07-23 23:25:26 +0000605 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000606 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000607 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000608 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000609 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000610 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000611 // If we know the condition is false, add NULL as the successor for
612 // the block containing the condition. In this case, the confluence
613 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000614 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000615 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000616 } else {
617 // If we have no LHS expression, add the ConfluenceBlock as a direct
618 // successor for the block containing the condition. Moreover, we need to
619 // reverse the order of the predecessors in the ConfluenceBlock because
620 // the RHSBlock will have been added to the succcessors already, and we
621 // want the first predecessor to the the block containing the expression
622 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000623 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000624 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000625 std::reverse(ConfluenceBlock->pred_begin(),
626 ConfluenceBlock->pred_end());
627 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000628 }
Mike Stump11289f42009-09-09 15:08:12 +0000629
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000630 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000631 Block->setTerminator(C);
632 return addStmt(C->getCond());
633}
634
Ted Kremenek93668002009-07-17 22:18:43 +0000635CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
636 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000637
Ted Kremenek93668002009-07-17 22:18:43 +0000638 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000639 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000640 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000641 }
Mike Stump11289f42009-09-09 15:08:12 +0000642
Ted Kremenek93668002009-07-17 22:18:43 +0000643 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000644
Ted Kremenek93668002009-07-17 22:18:43 +0000645 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
646 typedef llvm::SmallVector<Decl*,10> BufTy;
647 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000648
Ted Kremenek93668002009-07-17 22:18:43 +0000649 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
650 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
651 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
652 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000653
Ted Kremenek93668002009-07-17 22:18:43 +0000654 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
655 // automatically freed with the CFG.
656 DeclGroupRef DG(*I);
657 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000658 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000659 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000660
Ted Kremenek93668002009-07-17 22:18:43 +0000661 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000662 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000663 B = VisitDeclSubExpr(D);
664 }
Mike Stump11289f42009-09-09 15:08:12 +0000665
666 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000667}
Mike Stump11289f42009-09-09 15:08:12 +0000668
Ted Kremenek93668002009-07-17 22:18:43 +0000669/// VisitDeclSubExpr - Utility method to add block-level expressions for
670/// initializers in Decls.
671CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
672 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000673
Ted Kremenek93668002009-07-17 22:18:43 +0000674 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000675
Ted Kremenek93668002009-07-17 22:18:43 +0000676 if (!VD)
677 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000678
Ted Kremenek93668002009-07-17 22:18:43 +0000679 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000680
Ted Kremenek93668002009-07-17 22:18:43 +0000681 if (Init) {
682 // Optimization: Don't create separate block-level statements for literals.
683 switch (Init->getStmtClass()) {
684 case Stmt::IntegerLiteralClass:
685 case Stmt::CharacterLiteralClass:
686 case Stmt::StringLiteralClass:
687 break;
688 default:
689 Block = addStmt(Init);
690 }
691 }
Mike Stump11289f42009-09-09 15:08:12 +0000692
Ted Kremenek93668002009-07-17 22:18:43 +0000693 // If the type of VD is a VLA, then we must process its size expressions.
694 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
695 VA = FindVA(VA->getElementType().getTypePtr()))
696 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000697
Ted Kremenek93668002009-07-17 22:18:43 +0000698 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000699}
700
701CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000702 // We may see an if statement in the middle of a basic block, or it may be the
703 // first statement we are processing. In either case, we create a new basic
704 // block. First, we create the blocks for the then...else statements, and
705 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000706 // middle of a block, we stop processing that block. That block is then the
707 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000708
709 // The block we were proccessing is now finished. Make it the successor
710 // block.
711 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000712 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000713 if (!FinishBlock(Block))
714 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000715 }
Mike Stump31feda52009-07-17 01:31:16 +0000716
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000717 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000718 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000719
Ted Kremenek9aae5132007-08-23 21:42:29 +0000720 if (Stmt* Else = I->getElse()) {
721 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000722
Ted Kremenek9aae5132007-08-23 21:42:29 +0000723 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000724 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000725 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000726 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000727
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000728 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
729 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000730 else if (Block) {
731 if (!FinishBlock(ElseBlock))
732 return 0;
733 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000734 }
Mike Stump31feda52009-07-17 01:31:16 +0000735
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000736 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000737 CFGBlock* ThenBlock;
738 {
739 Stmt* Then = I->getThen();
740 assert (Then);
741 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000742 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000743 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000744
Ted Kremenek1b379512009-04-01 03:52:47 +0000745 if (!ThenBlock) {
746 // We can reach here if the "then" body has all NullStmts.
747 // Create an empty block so we can distinguish between true and false
748 // branches in path-sensitive analyses.
749 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000750 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000751 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000752 if (!FinishBlock(ThenBlock))
753 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000754 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000755 }
756
Mike Stump31feda52009-07-17 01:31:16 +0000757 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000758 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000759
Ted Kremenek9aae5132007-08-23 21:42:29 +0000760 // Set the terminator of the new block to the If statement.
761 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000762
Mike Stump773582d2009-07-23 23:25:26 +0000763 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000764 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000765
Ted Kremenek9aae5132007-08-23 21:42:29 +0000766 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000767 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
768 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000769
770 // Add the condition as the last statement in the new block. This may create
771 // new blocks as the condition may contain control-flow. Any newly created
772 // blocks will be pointed to be "Block".
Ted Kremenek93668002009-07-17 22:18:43 +0000773 return addStmt(I->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000774}
Mike Stump31feda52009-07-17 01:31:16 +0000775
776
Ted Kremenek9aae5132007-08-23 21:42:29 +0000777CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000778 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000779 //
Mike Stump31feda52009-07-17 01:31:16 +0000780 // NOTE: If a "return" appears in the middle of a block, this means that the
781 // code afterwards is DEAD (unreachable). We still keep a basic block
782 // for that code; a simple "mark-and-sweep" from the entry block will be
783 // able to report such dead blocks.
Ted Kremenek0868eea2009-09-24 18:45:41 +0000784 if (Block)
785 FinishBlock(Block);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000786
787 // Create the new block.
788 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000789
Ted Kremenek9aae5132007-08-23 21:42:29 +0000790 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000791 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000792
793 // Add the return statement to the block. This may create new blocks if R
794 // contains control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +0000795 return VisitStmt(R, true);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000796}
797
798CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
799 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000800 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000801 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000802
Ted Kremenek93668002009-07-17 22:18:43 +0000803 if (!LabelBlock) // This can happen when the body is empty, i.e.
804 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000805
Ted Kremenek93668002009-07-17 22:18:43 +0000806 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000807 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000808
809 // Labels partition blocks, so this is the end of the basic block we were
810 // processing (L is the block's label). Because this is label (and we have
811 // already processed the substatement) there is no extra control-flow to worry
812 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000813 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000814 if (!FinishBlock(LabelBlock))
815 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000816
817 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000818 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000819
Ted Kremenek9aae5132007-08-23 21:42:29 +0000820 // This block is now the implicit successor of other blocks.
821 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000822
Ted Kremenek9aae5132007-08-23 21:42:29 +0000823 return LabelBlock;
824}
825
826CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000827 // Goto is a control-flow statement. Thus we stop processing the current
828 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000829 if (Block)
830 FinishBlock(Block);
831
Ted Kremenek9aae5132007-08-23 21:42:29 +0000832 Block = createBlock(false);
833 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000834
835 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000836 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000837
Ted Kremenek9aae5132007-08-23 21:42:29 +0000838 if (I == LabelMap.end())
839 // We will need to backpatch this block later.
840 BackpatchBlocks.push_back(Block);
841 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000842 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000843
Mike Stump31feda52009-07-17 01:31:16 +0000844 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000845}
846
847CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000848 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000849
Mike Stump014b3ea2009-07-21 01:12:51 +0000850 // "for" is a control-flow statement. Thus we stop processing the current
851 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000852 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000853 if (!FinishBlock(Block))
854 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000855 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000856 } else
857 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000858
859 // Because of short-circuit evaluation, the condition of the loop can span
860 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
861 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000862 CFGBlock* ExitConditionBlock = createBlock(false);
863 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000864
Ted Kremenek81e14852007-08-27 19:46:09 +0000865 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000866 ExitConditionBlock->setTerminator(F);
867
868 // Now add the actual condition to the condition block. Because the condition
869 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000870 if (Stmt* C = F->getCond()) {
871 Block = ExitConditionBlock;
872 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000873 if (Block) {
874 if (!FinishBlock(EntryConditionBlock))
875 return 0;
876 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000877 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000878
Mike Stump31feda52009-07-17 01:31:16 +0000879 // The condition block is the implicit successor for the loop body as well as
880 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000881 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000882
Mike Stump773582d2009-07-23 23:25:26 +0000883 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000884 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +0000885
Mike Stump773582d2009-07-23 23:25:26 +0000886 if (F->getCond())
887 KnownVal = TryEvaluateBool(F->getCond());
888
Ted Kremenek9aae5132007-08-23 21:42:29 +0000889 // Now create the loop body.
890 {
891 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000892
Ted Kremenek9aae5132007-08-23 21:42:29 +0000893 // Save the current values for Block, Succ, and continue and break targets
894 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
895 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000896 save_break(BreakTargetBlock);
897
Ted Kremeneke9610502007-08-30 18:39:40 +0000898 // Create a new block to contain the (bottom) of the loop body.
899 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000900
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000901 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000902 // Generate increment code in its own basic block. This is the target of
903 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000904 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000905 } else {
906 // No increment code. Create a special, empty, block that is used as the
907 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000908 assert(Succ == EntryConditionBlock);
909 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000910 }
Mike Stump31feda52009-07-17 01:31:16 +0000911
Ted Kremenek902393b2009-04-28 00:51:56 +0000912 // Finish up the increment (or empty) block if it hasn't been already.
913 if (Block) {
914 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000915 if (!FinishBlock(Block))
916 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000917 Block = 0;
918 }
Mike Stump31feda52009-07-17 01:31:16 +0000919
Ted Kremenek902393b2009-04-28 00:51:56 +0000920 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000921
Ted Kremenek902393b2009-04-28 00:51:56 +0000922 // The starting block for the loop increment is the block that should
923 // represent the 'loop target' for looping back to the start of the loop.
924 ContinueTargetBlock->setLoopTarget(F);
925
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000926 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000927 BreakTargetBlock = LoopSuccessor;
928
929 // Now populate the body block, and in the process create new blocks as we
930 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000931 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000932
933 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +0000934 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +0000935 else if (Block && !FinishBlock(BodyBlock))
936 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000937
Ted Kremenek30754282009-07-24 04:47:11 +0000938 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000939 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000940 }
Mike Stump31feda52009-07-17 01:31:16 +0000941
Ted Kremenek30754282009-07-24 04:47:11 +0000942 // Link up the condition block with the code that follows the loop. (the
943 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000944 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000945
Ted Kremenek9aae5132007-08-23 21:42:29 +0000946 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000947 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000948 if (Stmt* I = F->getInit()) {
949 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000950 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000951 } else {
952 // There is no loop initialization. We are thus basically a while loop.
953 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000954 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000955 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000956 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000957 }
958}
959
Ted Kremenek9d56e642008-11-11 17:10:00 +0000960CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
961 // Objective-C fast enumeration 'for' statements:
962 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
963 //
964 // for ( Type newVariable in collection_expression ) { statements }
965 //
966 // becomes:
967 //
968 // prologue:
969 // 1. collection_expression
970 // T. jump to loop_entry
971 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000972 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +0000973 // 1. ObjCForCollectionStmt [performs binding to newVariable]
974 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
975 // TB:
976 // statements
977 // T. jump to loop_entry
978 // FB:
979 // what comes after
980 //
981 // and
982 //
983 // Type existingItem;
984 // for ( existingItem in expression ) { statements }
985 //
986 // becomes:
987 //
Mike Stump31feda52009-07-17 01:31:16 +0000988 // the same with newVariable replaced with existingItem; the binding works
989 // the same except that for one ObjCForCollectionStmt::getElement() returns
990 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +0000991 //
Mike Stump31feda52009-07-17 01:31:16 +0000992
Ted Kremenek9d56e642008-11-11 17:10:00 +0000993 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +0000994
Ted Kremenek9d56e642008-11-11 17:10:00 +0000995 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000996 if (!FinishBlock(Block))
997 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +0000998 LoopSuccessor = Block;
999 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001000 } else
1001 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001002
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001003 // Build the condition blocks.
1004 CFGBlock* ExitConditionBlock = createBlock(false);
1005 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001006
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001007 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001008 ExitConditionBlock->setTerminator(S);
1009
1010 // The last statement in the block should be the ObjCForCollectionStmt, which
1011 // performs the actual binding to 'element' and determines if there are any
1012 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001013 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001014 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001015
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001016 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001017 // generate new blocks as necesary. We DON'T add the statement by default to
1018 // the CFG unless it contains control-flow.
Ted Kremenek93668002009-07-17 22:18:43 +00001019 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump31feda52009-07-17 01:31:16 +00001020 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001021 if (!FinishBlock(EntryConditionBlock))
1022 return 0;
1023 Block = 0;
1024 }
Mike Stump31feda52009-07-17 01:31:16 +00001025
1026 // The condition block is the implicit successor for the loop body as well as
1027 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001028 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001029
Ted Kremenek9d56e642008-11-11 17:10:00 +00001030 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001031 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001032 // Save the current values for Succ, continue and break targets.
1033 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001034 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1035
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001036 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001037 ContinueTargetBlock = EntryConditionBlock;
1038
Ted Kremenek93668002009-07-17 22:18:43 +00001039 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001040
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001041 if (!BodyBlock)
1042 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001043 else if (Block) {
1044 if (!FinishBlock(BodyBlock))
1045 return 0;
1046 }
Mike Stump31feda52009-07-17 01:31:16 +00001047
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001048 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001049 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001050 }
Mike Stump31feda52009-07-17 01:31:16 +00001051
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001052 // Link up the condition block with the code that follows the loop.
1053 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001054 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001055
Ted Kremenek9d56e642008-11-11 17:10:00 +00001056 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001057 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001058 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001059}
1060
Ted Kremenek49805452009-05-02 01:49:13 +00001061CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1062 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001063
Ted Kremenek49805452009-05-02 01:49:13 +00001064 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001065 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001066
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001067 // The sync body starts its own basic block. This makes it a little easier
1068 // for diagnostic clients.
1069 if (SyncBlock) {
1070 if (!FinishBlock(SyncBlock))
1071 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001072
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001073 Block = 0;
1074 }
Mike Stump31feda52009-07-17 01:31:16 +00001075
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001076 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001077
Ted Kremenek49805452009-05-02 01:49:13 +00001078 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001079 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001080}
Mike Stump31feda52009-07-17 01:31:16 +00001081
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001082CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001083 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001084 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001085}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001086
Ted Kremenek9aae5132007-08-23 21:42:29 +00001087CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001088 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001089
Mike Stump014b3ea2009-07-21 01:12:51 +00001090 // "while" is a control-flow statement. Thus we stop processing the current
1091 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001092 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001093 if (!FinishBlock(Block))
1094 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001095 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001096 } else
1097 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001098
1099 // Because of short-circuit evaluation, the condition of the loop can span
1100 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1101 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001102 CFGBlock* ExitConditionBlock = createBlock(false);
1103 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001104
Ted Kremenek81e14852007-08-27 19:46:09 +00001105 // Set the terminator for the "exit" condition block.
1106 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001107
1108 // Now add the actual condition to the condition block. Because the condition
1109 // itself may contain control-flow, new blocks may be created. Thus we update
1110 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001111 if (Stmt* C = W->getCond()) {
1112 Block = ExitConditionBlock;
1113 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001114 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +00001115 if (Block) {
1116 if (!FinishBlock(EntryConditionBlock))
1117 return 0;
1118 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001119 }
Mike Stump31feda52009-07-17 01:31:16 +00001120
1121 // The condition block is the implicit successor for the loop body as well as
1122 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001123 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001124
Mike Stump773582d2009-07-23 23:25:26 +00001125 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001126 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001127
Ted Kremenek9aae5132007-08-23 21:42:29 +00001128 // Process the loop body.
1129 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001130 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001131
1132 // Save the current values for Block, Succ, and continue and break targets
1133 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1134 save_continue(ContinueTargetBlock),
1135 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001136
Mike Stump31feda52009-07-17 01:31:16 +00001137 // Create an empty block to represent the transition block for looping back
1138 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001139 Block = 0;
1140 assert(Succ == EntryConditionBlock);
1141 Succ = createBlock();
1142 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001143 ContinueTargetBlock = Succ;
1144
Ted Kremenek9aae5132007-08-23 21:42:29 +00001145 // All breaks should go to the code following the loop.
1146 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001147
Ted Kremenek9aae5132007-08-23 21:42:29 +00001148 // NULL out Block to force lazy instantiation of blocks for the body.
1149 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001150
Ted Kremenek9aae5132007-08-23 21:42:29 +00001151 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001152 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001153
Ted Kremeneke9610502007-08-30 18:39:40 +00001154 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001155 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001156 else if (Block) {
1157 if (!FinishBlock(BodyBlock))
1158 return 0;
1159 }
Mike Stump31feda52009-07-17 01:31:16 +00001160
Ted Kremenek30754282009-07-24 04:47:11 +00001161 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001162 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001163 }
Mike Stump31feda52009-07-17 01:31:16 +00001164
Ted Kremenek30754282009-07-24 04:47:11 +00001165 // Link up the condition block with the code that follows the loop. (the
1166 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001167 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001168
1169 // There can be no more statements in the condition block since we loop back
1170 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001171 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001172
Ted Kremenek9aae5132007-08-23 21:42:29 +00001173 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001174 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001175 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001176}
Mike Stump11289f42009-09-09 15:08:12 +00001177
1178
Ted Kremenek93668002009-07-17 22:18:43 +00001179CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1180 // FIXME: For now we pretend that @catch and the code it contains does not
1181 // exit.
1182 return Block;
1183}
Mike Stump31feda52009-07-17 01:31:16 +00001184
Ted Kremenek93041ba2008-12-09 20:20:09 +00001185CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1186 // FIXME: This isn't complete. We basically treat @throw like a return
1187 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001188
Ted Kremenek0868eea2009-09-24 18:45:41 +00001189 // If we were in the middle of a block we stop processing that block.
Ted Kremenek93668002009-07-17 22:18:43 +00001190 if (Block && !FinishBlock(Block))
1191 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001192
Ted Kremenek93041ba2008-12-09 20:20:09 +00001193 // Create the new block.
1194 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001195
Ted Kremenek93041ba2008-12-09 20:20:09 +00001196 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001197 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001198
1199 // Add the statement to the block. This may create new blocks if S contains
1200 // control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +00001201 return VisitStmt(S, true);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001202}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001203
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001204CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001205 // If we were in the middle of a block we stop processing that block.
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001206 if (Block && !FinishBlock(Block))
1207 return 0;
1208
1209 // Create the new block.
1210 Block = createBlock(false);
1211
1212 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001213 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001214
1215 // Add the statement to the block. This may create new blocks if S contains
1216 // control-flow (short-circuit operations).
1217 return VisitStmt(T, true);
1218}
1219
Ted Kremenek93668002009-07-17 22:18:43 +00001220CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001221 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001222
Mike Stump8d50b6a2009-07-21 01:27:50 +00001223 // "do...while" is a control-flow statement. Thus we stop processing the
1224 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001225 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001226 if (!FinishBlock(Block))
1227 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001228 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001229 } else
1230 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001231
1232 // Because of short-circuit evaluation, the condition of the loop can span
1233 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1234 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001235 CFGBlock* ExitConditionBlock = createBlock(false);
1236 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001237
Ted Kremenek81e14852007-08-27 19:46:09 +00001238 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001239 ExitConditionBlock->setTerminator(D);
1240
1241 // Now add the actual condition to the condition block. Because the condition
1242 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001243 if (Stmt* C = D->getCond()) {
1244 Block = ExitConditionBlock;
1245 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001246 if (Block) {
1247 if (!FinishBlock(EntryConditionBlock))
1248 return 0;
1249 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001250 }
Mike Stump31feda52009-07-17 01:31:16 +00001251
Ted Kremeneka1523a32008-02-27 07:20:00 +00001252 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001253 Succ = EntryConditionBlock;
1254
Mike Stump773582d2009-07-23 23:25:26 +00001255 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001256 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001257
Ted Kremenek9aae5132007-08-23 21:42:29 +00001258 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001259 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001260 {
1261 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001262
Ted Kremenek9aae5132007-08-23 21:42:29 +00001263 // Save the current values for Block, Succ, and continue and break targets
1264 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1265 save_continue(ContinueTargetBlock),
1266 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001267
Ted Kremenek9aae5132007-08-23 21:42:29 +00001268 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001269 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001270
Ted Kremenek9aae5132007-08-23 21:42:29 +00001271 // All breaks should go to the code following the loop.
1272 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001273
Ted Kremenek9aae5132007-08-23 21:42:29 +00001274 // NULL out Block to force lazy instantiation of blocks for the body.
1275 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001276
Ted Kremenek9aae5132007-08-23 21:42:29 +00001277 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001278 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001279
Ted Kremeneke9610502007-08-30 18:39:40 +00001280 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001281 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001282 else if (Block) {
1283 if (!FinishBlock(BodyBlock))
1284 return 0;
1285 }
Mike Stump31feda52009-07-17 01:31:16 +00001286
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001287 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001288 // ExitConditionBlock to represent the "loop back" transition. Create an
1289 // empty block to represent the transition block for looping back to the
1290 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001291 // FIXME: Can we do this more efficiently without adding another block?
1292 Block = NULL;
1293 Succ = BodyBlock;
1294 CFGBlock *LoopBackBlock = createBlock();
1295 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001296
Ted Kremenek30754282009-07-24 04:47:11 +00001297 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001298 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001299 }
Mike Stump31feda52009-07-17 01:31:16 +00001300
Ted Kremenek30754282009-07-24 04:47:11 +00001301 // Link up the condition block with the code that follows the loop.
1302 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001303 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001304
1305 // There can be no more statements in the body block(s) since we loop back to
1306 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001307 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001308
Ted Kremenek9aae5132007-08-23 21:42:29 +00001309 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001310 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001311 return BodyBlock;
1312}
1313
1314CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1315 // "continue" is a control-flow statement. Thus we stop processing the
1316 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001317 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001318 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001319
Ted Kremenek9aae5132007-08-23 21:42:29 +00001320 // Now create a new block that ends with the continue statement.
1321 Block = createBlock(false);
1322 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001323
Ted Kremenek9aae5132007-08-23 21:42:29 +00001324 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001325 // incomplete AST. This means the CFG cannot be constructed.
1326 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001327 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001328 else
1329 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001330
Ted Kremenek9aae5132007-08-23 21:42:29 +00001331 return Block;
1332}
Mike Stump11289f42009-09-09 15:08:12 +00001333
Ted Kremenek0747de62009-07-18 00:47:21 +00001334CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1335 bool alwaysAdd) {
1336
1337 if (alwaysAdd) {
1338 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001339 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001340 }
Mike Stump11289f42009-09-09 15:08:12 +00001341
Ted Kremenek93668002009-07-17 22:18:43 +00001342 // VLA types have expressions that must be evaluated.
1343 if (E->isArgumentType()) {
1344 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1345 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1346 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001347 }
Mike Stump11289f42009-09-09 15:08:12 +00001348
Mike Stump31feda52009-07-17 01:31:16 +00001349 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001350}
Mike Stump11289f42009-09-09 15:08:12 +00001351
Ted Kremenek93668002009-07-17 22:18:43 +00001352/// VisitStmtExpr - Utility method to handle (nested) statement
1353/// expressions (a GCC extension).
Ted Kremenek0747de62009-07-18 00:47:21 +00001354CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1355 if (alwaysAdd) {
1356 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001357 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001358 }
Ted Kremenek93668002009-07-17 22:18:43 +00001359 return VisitCompoundStmt(SE->getSubStmt());
1360}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001361
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001362CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001363 // "switch" is a control-flow statement. Thus we stop processing the current
1364 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001365 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001366
Ted Kremenek9aae5132007-08-23 21:42:29 +00001367 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001368 if (!FinishBlock(Block))
1369 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001370 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001371 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001372
1373 // Save the current "switch" context.
1374 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001375 save_break(BreakTargetBlock),
1376 save_default(DefaultCaseBlock);
1377
Mike Stump31feda52009-07-17 01:31:16 +00001378 // Set the "default" case to be the block after the switch statement. If the
1379 // switch statement contains a "default:", this value will be overwritten with
1380 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001381 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001382
Ted Kremenek9aae5132007-08-23 21:42:29 +00001383 // Create a new block that will contain the switch statement.
1384 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001385
Ted Kremenek9aae5132007-08-23 21:42:29 +00001386 // Now process the switch body. The code after the switch is the implicit
1387 // successor.
1388 Succ = SwitchSuccessor;
1389 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001390
1391 // When visiting the body, the case statements should automatically get linked
1392 // up to the switch. We also don't keep a pointer to the body, since all
1393 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001394 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001395 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001396 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001397 if (Block) {
1398 if (!FinishBlock(BodyBlock))
1399 return 0;
1400 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001401
Mike Stump31feda52009-07-17 01:31:16 +00001402 // If we have no "default:" case, the default transition is to the code
1403 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001404 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001405
Ted Kremenek81e14852007-08-27 19:46:09 +00001406 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001407 SwitchTerminatedBlock->setTerminator(Terminator);
1408 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001409 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001410
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001411 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001412}
1413
Ted Kremenek93668002009-07-17 22:18:43 +00001414CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001415 // CaseStmts are essentially labels, so they are the first statement in a
1416 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001417
Ted Kremenek93668002009-07-17 22:18:43 +00001418 if (CS->getSubStmt())
1419 addStmt(CS->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001420
Ted Kremenek55e91e82007-08-30 18:48:11 +00001421 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001422 if (!CaseBlock)
1423 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001424
1425 // Cases statements partition blocks, so this is the top of the basic block we
1426 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001427 CaseBlock->setLabel(CS);
1428
Ted Kremenek55957a82009-05-02 00:13:27 +00001429 if (!FinishBlock(CaseBlock))
1430 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001431
1432 // Add this block to the list of successors for the block with the switch
1433 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001434 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001435 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001436
Ted Kremenek9aae5132007-08-23 21:42:29 +00001437 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1438 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001439
Ted Kremenek9aae5132007-08-23 21:42:29 +00001440 // This block is now the implicit successor of other blocks.
1441 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001442
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001443 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001444}
Mike Stump31feda52009-07-17 01:31:16 +00001445
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001446CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001447 if (Terminator->getSubStmt())
1448 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001449
Ted Kremenek654c78f2008-02-13 22:05:39 +00001450 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001451
1452 if (!DefaultCaseBlock)
1453 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001454
1455 // Default statements partition blocks, so this is the top of the basic block
1456 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001457 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001458
Ted Kremenek55957a82009-05-02 00:13:27 +00001459 if (!FinishBlock(DefaultCaseBlock))
1460 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001461
Mike Stump31feda52009-07-17 01:31:16 +00001462 // Unlike case statements, we don't add the default block to the successors
1463 // for the switch statement immediately. This is done when we finish
1464 // processing the switch statement. This allows for the default case
1465 // (including a fall-through to the code after the switch statement) to always
1466 // be the last successor of a switch-terminated block.
1467
Ted Kremenek654c78f2008-02-13 22:05:39 +00001468 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1469 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001470
Ted Kremenek654c78f2008-02-13 22:05:39 +00001471 // This block is now the implicit successor of other blocks.
1472 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001473
1474 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001475}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001476
Ted Kremenekeda180e22007-08-28 19:26:49 +00001477CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001478 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001479 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001480
Ted Kremenekeda180e22007-08-28 19:26:49 +00001481 if (!IBlock) {
1482 IBlock = createBlock(false);
1483 cfg->setIndirectGotoBlock(IBlock);
1484 }
Mike Stump31feda52009-07-17 01:31:16 +00001485
Ted Kremenekeda180e22007-08-28 19:26:49 +00001486 // IndirectGoto is a control-flow statement. Thus we stop processing the
1487 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001488 if (Block && !FinishBlock(Block))
1489 return 0;
1490
Ted Kremenekeda180e22007-08-28 19:26:49 +00001491 Block = createBlock(false);
1492 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001493 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001494 return addStmt(I->getTarget());
1495}
1496
Ted Kremenek04cca642007-08-23 21:26:19 +00001497} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001498
Mike Stump31feda52009-07-17 01:31:16 +00001499/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1500/// no successors or predecessors. If this is the first block created in the
1501/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001502CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001503 bool first_block = begin() == end();
1504
1505 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001506 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1507 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1508 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001509
1510 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001511 if (first_block)
1512 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001513
1514 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001515 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001516}
1517
Ted Kremenek889073f2007-08-23 16:51:22 +00001518/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1519/// CFG is returned to the caller.
Mike Stump0d76d072009-07-20 23:24:15 +00001520CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001521 CFGBuilder Builder;
Mike Stump0d76d072009-07-20 23:24:15 +00001522 return Builder.buildCFG(Statement, C);
Ted Kremenek889073f2007-08-23 16:51:22 +00001523}
1524
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001525//===----------------------------------------------------------------------===//
1526// CFG: Queries for BlkExprs.
1527//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001528
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001529namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001530 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001531}
1532
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001533static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1534 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001535 return;
Mike Stump31feda52009-07-17 01:31:16 +00001536
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001537 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001538 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001539
Ted Kremenek95a123c2008-01-26 00:03:27 +00001540 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1541 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001542
Ted Kremenek95a123c2008-01-26 00:03:27 +00001543 FindSubExprAssignments(*I, Set);
1544 }
1545}
1546
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001547static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1548 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001549
1550 // Look for assignments that are used as subexpressions. These are the only
1551 // assignments that we want to *possibly* register as a block-level
1552 // expression. Basically, if an assignment occurs both in a subexpression and
1553 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001554 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001555
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001556 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001557 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001558 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001559
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001560 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001561
1562 // Iterate over the statements again on identify the Expr* and Stmt* at the
1563 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001564
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001565 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001566 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001567
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001568 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001569 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001570 // expression are really "statements" whose value is never used by
1571 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001572 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001573 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001574 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1575 // Special handling for statement expressions. The last statement in
1576 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001577 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001578 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001579 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001580 (*M)[C->body_back()] = x;
1581 }
1582 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001583
Ted Kremenek95a123c2008-01-26 00:03:27 +00001584 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001585 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001586 }
Mike Stump31feda52009-07-17 01:31:16 +00001587
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001588 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001589
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001590 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001591
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001592 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001593 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001594 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001595 }
1596 }
Mike Stump31feda52009-07-17 01:31:16 +00001597
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001598 return M;
1599}
1600
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001601CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1602 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001603 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001604
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001605 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001606 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001607
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001608 if (I == M->end()) return CFG::BlkExprNumTy();
1609 else return CFG::BlkExprNumTy(I->second);
1610}
1611
1612unsigned CFG::getNumBlkExprs() {
1613 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1614 return M->size();
1615 else {
1616 // We assume callers interested in the number of BlkExprs will want
1617 // the map constructed if it doesn't already exist.
1618 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1619 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1620 }
1621}
1622
Ted Kremenek6065ef62008-04-28 18:00:46 +00001623//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001624// Cleanup: CFG dstor.
1625//===----------------------------------------------------------------------===//
1626
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001627CFG::~CFG() {
1628 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1629}
Mike Stump31feda52009-07-17 01:31:16 +00001630
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001631//===----------------------------------------------------------------------===//
1632// CFG pretty printing
1633//===----------------------------------------------------------------------===//
1634
Ted Kremenek7e776b12007-08-22 18:22:34 +00001635namespace {
1636
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001637class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001638
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001639 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1640 StmtMapTy StmtMap;
1641 signed CurrentBlock;
1642 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001643 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001644public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001645
Chris Lattnerc61089a2009-06-30 01:26:17 +00001646 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1647 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001648 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1649 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001650 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001651 BI != BEnd; ++BI, ++j )
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001652 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001653 }
1654 }
Mike Stump31feda52009-07-17 01:31:16 +00001655
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001656 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001657
Chris Lattnerc61089a2009-06-30 01:26:17 +00001658 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001659 void setBlockID(signed i) { CurrentBlock = i; }
1660 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001661
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001662 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001663
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001664 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001665
1666 if (I == StmtMap.end())
1667 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001668
1669 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001670 && I->second.second == CurrentStmt)
1671 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001672
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001673 OS << "[B" << I->second.first << "." << I->second.second << "]";
1674 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001675 }
1676};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001677} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001678
Chris Lattnerc61089a2009-06-30 01:26:17 +00001679
1680namespace {
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001681class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1682 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001683
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001684 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001685 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001686 PrintingPolicy Policy;
1687
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001688public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001689 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001690 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001691 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001692
Ted Kremenek9aae5132007-08-23 21:42:29 +00001693 void VisitIfStmt(IfStmt* I) {
1694 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001695 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001696 }
Mike Stump31feda52009-07-17 01:31:16 +00001697
Ted Kremenek9aae5132007-08-23 21:42:29 +00001698 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001699 void VisitStmt(Stmt* Terminator) {
1700 Terminator->printPretty(OS, Helper, Policy);
1701 }
1702
Ted Kremenek9aae5132007-08-23 21:42:29 +00001703 void VisitForStmt(ForStmt* F) {
1704 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001705 if (F->getInit()) OS << "...";
1706 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001707 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001708 OS << "; ";
1709 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001710 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001711 }
Mike Stump31feda52009-07-17 01:31:16 +00001712
Ted Kremenek9aae5132007-08-23 21:42:29 +00001713 void VisitWhileStmt(WhileStmt* W) {
1714 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001715 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001716 }
Mike Stump31feda52009-07-17 01:31:16 +00001717
Ted Kremenek9aae5132007-08-23 21:42:29 +00001718 void VisitDoStmt(DoStmt* D) {
1719 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001720 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001721 }
Mike Stump31feda52009-07-17 01:31:16 +00001722
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001723 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001724 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001725 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001726 }
Mike Stump31feda52009-07-17 01:31:16 +00001727
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001728 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001729 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001730 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001731 }
Mike Stump31feda52009-07-17 01:31:16 +00001732
Ted Kremenek391f94a2007-08-31 22:29:13 +00001733 void VisitChooseExpr(ChooseExpr* C) {
1734 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001735 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001736 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001737 }
Mike Stump31feda52009-07-17 01:31:16 +00001738
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001739 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1740 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001741 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001742 }
Mike Stump31feda52009-07-17 01:31:16 +00001743
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001744 void VisitBinaryOperator(BinaryOperator* B) {
1745 if (!B->isLogicalOp()) {
1746 VisitExpr(B);
1747 return;
1748 }
Mike Stump31feda52009-07-17 01:31:16 +00001749
Douglas Gregor7de59662009-05-29 20:38:28 +00001750 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001751
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001752 switch (B->getOpcode()) {
1753 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001754 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001755 return;
1756 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001757 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001758 return;
1759 default:
1760 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001761 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001762 }
Mike Stump31feda52009-07-17 01:31:16 +00001763
Ted Kremenek12687ff2007-08-27 21:54:41 +00001764 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001765 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001766 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001767};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001768} // end anonymous namespace
1769
Mike Stump31feda52009-07-17 01:31:16 +00001770
Chris Lattnerc61089a2009-06-30 01:26:17 +00001771static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1772 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001773 if (Helper) {
1774 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001775 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001776 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001777
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001778 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001779 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001780 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001781 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001782 return;
1783 }
1784 }
Mike Stump31feda52009-07-17 01:31:16 +00001785
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001786 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001787 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001788 if (B->getOpcode() == BinaryOperator::Comma) {
1789 OS << "... , ";
1790 Helper->handledStmt(B->getRHS(),OS);
1791 OS << '\n';
1792 return;
Mike Stump31feda52009-07-17 01:31:16 +00001793 }
1794 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001795 }
Mike Stump31feda52009-07-17 01:31:16 +00001796
Chris Lattnerc61089a2009-06-30 01:26:17 +00001797 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001798
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001799 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001800 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001801}
Mike Stump31feda52009-07-17 01:31:16 +00001802
Chris Lattnerc61089a2009-06-30 01:26:17 +00001803static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1804 const CFGBlock& B,
1805 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001806
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001807 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001808
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001809 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001810 OS << "\n [ B" << B.getBlockID();
1811
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001812 if (&B == &cfg->getEntry())
1813 OS << " (ENTRY) ]\n";
1814 else if (&B == &cfg->getExit())
1815 OS << " (EXIT) ]\n";
1816 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001817 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001818 else
1819 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001820
Ted Kremenek71eca012007-08-29 23:20:49 +00001821 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001822 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001823
1824 if (print_edges)
1825 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001826
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001827 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001828 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001829 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001830 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001831 C->getLHS()->printPretty(OS, Helper,
1832 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001833 if (C->getRHS()) {
1834 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001835 C->getRHS()->printPretty(OS, Helper,
1836 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001837 }
Mike Stump31feda52009-07-17 01:31:16 +00001838 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001839 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001840 else
1841 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001842
Ted Kremenek71eca012007-08-29 23:20:49 +00001843 OS << ":\n";
1844 }
Mike Stump31feda52009-07-17 01:31:16 +00001845
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001846 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001847 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001848
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001849 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1850 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001851
Ted Kremenek71eca012007-08-29 23:20:49 +00001852 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001853 if (print_edges)
1854 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001855
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001856 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001857
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001858 if (Helper)
1859 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001860
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001861 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001862 }
Mike Stump31feda52009-07-17 01:31:16 +00001863
Ted Kremenek71eca012007-08-29 23:20:49 +00001864 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001865 if (B.getTerminator()) {
1866 if (print_edges)
1867 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001868
Ted Kremenek71eca012007-08-29 23:20:49 +00001869 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001870
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001871 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001872
Chris Lattnerc61089a2009-06-30 01:26:17 +00001873 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1874 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001875 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001876 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001877 }
Mike Stump31feda52009-07-17 01:31:16 +00001878
Ted Kremenek71eca012007-08-29 23:20:49 +00001879 if (print_edges) {
1880 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001881 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001882 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001883
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001884 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1885 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001886
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001887 if (i == 8 || (i-8) == 0)
1888 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001889
Ted Kremenek71eca012007-08-29 23:20:49 +00001890 OS << " B" << (*I)->getBlockID();
1891 }
Mike Stump31feda52009-07-17 01:31:16 +00001892
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001893 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001894
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001895 // Print the successors of this block.
1896 OS << " Successors (" << B.succ_size() << "):";
1897 i = 0;
1898
1899 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1900 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001901
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001902 if (i == 8 || (i-8) % 10 == 0)
1903 OS << "\n ";
1904
Mike Stump0d76d072009-07-20 23:24:15 +00001905 if (*I)
1906 OS << " B" << (*I)->getBlockID();
1907 else
1908 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001909 }
Mike Stump31feda52009-07-17 01:31:16 +00001910
Ted Kremenek71eca012007-08-29 23:20:49 +00001911 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001912 }
Mike Stump31feda52009-07-17 01:31:16 +00001913}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001914
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001915
1916/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001917void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001918
1919/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001920void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1921 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001922
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001923 // Print the entry block.
1924 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001925
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001926 // Iterate through the CFGBlocks and print them one by one.
1927 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1928 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001929 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001930 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001931
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001932 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001933 }
Mike Stump31feda52009-07-17 01:31:16 +00001934
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001935 // Print the exit block.
1936 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001937 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001938}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001939
1940/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001941void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1942 print(llvm::errs(), cfg, LO);
1943}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001944
1945/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1946/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001947void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1948 const LangOptions &LO) const {
1949 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001950 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00001951}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001952
Ted Kremenek15647632008-01-30 23:02:42 +00001953/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001954void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00001955 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001956 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00001957 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1958}
1959
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001960Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00001961
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001962 if (!Terminator)
1963 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001964
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001965 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001966
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001967 switch (Terminator->getStmtClass()) {
1968 default:
1969 break;
Mike Stump31feda52009-07-17 01:31:16 +00001970
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001971 case Stmt::ForStmtClass:
1972 E = cast<ForStmt>(Terminator)->getCond();
1973 break;
Mike Stump31feda52009-07-17 01:31:16 +00001974
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001975 case Stmt::WhileStmtClass:
1976 E = cast<WhileStmt>(Terminator)->getCond();
1977 break;
Mike Stump31feda52009-07-17 01:31:16 +00001978
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001979 case Stmt::DoStmtClass:
1980 E = cast<DoStmt>(Terminator)->getCond();
1981 break;
Mike Stump31feda52009-07-17 01:31:16 +00001982
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001983 case Stmt::IfStmtClass:
1984 E = cast<IfStmt>(Terminator)->getCond();
1985 break;
Mike Stump31feda52009-07-17 01:31:16 +00001986
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001987 case Stmt::ChooseExprClass:
1988 E = cast<ChooseExpr>(Terminator)->getCond();
1989 break;
Mike Stump31feda52009-07-17 01:31:16 +00001990
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001991 case Stmt::IndirectGotoStmtClass:
1992 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1993 break;
Mike Stump31feda52009-07-17 01:31:16 +00001994
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001995 case Stmt::SwitchStmtClass:
1996 E = cast<SwitchStmt>(Terminator)->getCond();
1997 break;
Mike Stump31feda52009-07-17 01:31:16 +00001998
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001999 case Stmt::ConditionalOperatorClass:
2000 E = cast<ConditionalOperator>(Terminator)->getCond();
2001 break;
Mike Stump31feda52009-07-17 01:31:16 +00002002
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002003 case Stmt::BinaryOperatorClass: // '&&' and '||'
2004 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002005 break;
Mike Stump31feda52009-07-17 01:31:16 +00002006
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002007 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002008 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002009 }
Mike Stump31feda52009-07-17 01:31:16 +00002010
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002011 return E ? E->IgnoreParens() : NULL;
2012}
2013
Ted Kremenek92137a32008-05-16 16:06:00 +00002014bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002015
Ted Kremenek92137a32008-05-16 16:06:00 +00002016 if (!Terminator)
2017 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002018
Ted Kremenek92137a32008-05-16 16:06:00 +00002019 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002020
Ted Kremenek92137a32008-05-16 16:06:00 +00002021 switch (Terminator->getStmtClass()) {
2022 default:
2023 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002024
2025 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002026 case Stmt::WhileStmtClass:
2027 case Stmt::DoStmtClass:
2028 case Stmt::IfStmtClass:
2029 case Stmt::ChooseExprClass:
2030 case Stmt::ConditionalOperatorClass:
2031 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002032 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002033 }
Mike Stump31feda52009-07-17 01:31:16 +00002034
Ted Kremenek92137a32008-05-16 16:06:00 +00002035 return E ? E->IgnoreParens() : NULL;
2036}
2037
Ted Kremenek15647632008-01-30 23:02:42 +00002038
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002039//===----------------------------------------------------------------------===//
2040// CFG Graphviz Visualization
2041//===----------------------------------------------------------------------===//
2042
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002043
2044#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002045static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002046#endif
2047
Chris Lattnerc61089a2009-06-30 01:26:17 +00002048void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002049#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002050 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002051 GraphHelper = &H;
2052 llvm::ViewGraph(this,"CFG");
2053 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002054#endif
2055}
2056
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002057namespace llvm {
2058template<>
2059struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson4d9e93c2009-06-24 17:37:55 +00002060 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
2061 bool ShortNames) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002062
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002063#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002064 std::string OutSStr;
2065 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002066 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002067 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002068
2069 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2070
2071 // Process string output to make it nicer...
2072 for (unsigned i = 0; i != OutStr.length(); ++i)
2073 if (OutStr[i] == '\n') { // Left justify
2074 OutStr[i] = '\\';
2075 OutStr.insert(OutStr.begin()+i+1, 'l');
2076 }
Mike Stump31feda52009-07-17 01:31:16 +00002077
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002078 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002079#else
2080 return "";
2081#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002082 }
2083};
2084} // end namespace llvm