blob: 524210e08a78d0751d9c3dae52096a12df0bb98d [file] [log] [blame]
Ted Kremenekfddd5182007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Kremenekfddd5182007-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 Kremenekbd048782009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Ted Kremenekc310e932007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000019#include "llvm/Support/GraphWriter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000020#include "llvm/Support/Allocator.h"
21#include "llvm/Support/Format.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000022#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000023#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek0ba497b2009-10-20 23:46:25 +000024#include "llvm/ADT/OwningPtr.h"
Ted Kremenek83c01da2008-01-11 00:40:29 +000025
Ted Kremenekfddd5182007-08-21 21:42:03 +000026using namespace clang;
27
28namespace {
29
Douglas Gregor4afa39d2009-01-20 01:17:11 +000030static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-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 Stump6d9828c2009-07-17 01:31:16 +000034
35 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000036}
Mike Stump6d9828c2009-07-17 01:31:16 +000037
Ted Kremeneka34ea072008-08-04 22:51:42 +000038/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-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 Stump6d9828c2009-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 Kremenekc310e932007-08-21 22:06:14 +000051///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000052class CFGBuilder {
Mike Stumpe5af3ce2009-07-20 23:24:15 +000053 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +000054 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +000055
Ted Kremenekfddd5182007-08-21 21:42:03 +000056 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000057 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000058 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000059 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000060 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000061 CFGBlock* DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +000062
Ted Kremenek19bb3562007-08-28 19:26:49 +000063 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000064 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
65 LabelMapTy LabelMap;
Mike Stump6d9828c2009-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 Kremenek4a2b8a12007-08-22 15:40:58 +000069 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000070 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +000071
Ted Kremenek19bb3562007-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 Stump6d9828c2009-07-17 01:31:16 +000075
76public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +000077 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
78 Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000079 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenekee82d9b2009-10-12 20:55:07 +000080 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {}
Mike Stump6d9828c2009-07-17 01:31:16 +000081
Ted Kremenekd4fdee32007-08-23 21:42:29 +000082 // buildCFG - Used by external clients to construct the CFG.
Mike Stumpe5af3ce2009-07-20 23:24:15 +000083 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump6d9828c2009-07-17 01:31:16 +000084
Ted Kremenek4f880632009-07-17 22:18:43 +000085private:
86 // Visitors to walk an AST and construct the CFG.
87 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
88 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
Ted Kremenek13fc08a2009-07-18 00:47:21 +000089 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +000090 CFGBlock *VisitBreakStmt(BreakStmt *B);
91 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
92 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +000093 CFGBlock *VisitChooseExpr(ChooseExpr *C);
94 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
95 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
96 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump0979d802009-07-22 22:56:04 +000097 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenek4f880632009-07-17 22:18:43 +000098 CFGBlock *VisitDeclStmt(DeclStmt *DS);
99 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000100 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
101 CFGBlock *VisitDoStmt(DoStmt *D);
102 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000103 CFGBlock *VisitGotoStmt(GotoStmt* G);
104 CFGBlock *VisitIfStmt(IfStmt *I);
105 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
106 CFGBlock *VisitLabelStmt(LabelStmt *L);
107 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
108 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
109 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
110 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
111 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
112 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000113 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
114 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000115 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
116 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000117
Ted Kremenek4f880632009-07-17 22:18:43 +0000118 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
119 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
120 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000121
Ted Kremenek274f4332008-04-28 18:00:46 +0000122 // NYS == Not Yet Supported
123 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000124 badCFG = true;
125 return Block;
126 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000127
Ted Kremenek4f880632009-07-17 22:18:43 +0000128 void autoCreateBlock() { if (!Block) Block = createBlock(); }
129 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000130 bool FinishBlock(CFGBlock* B);
Ted Kremenek4f880632009-07-17 22:18:43 +0000131 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000132
133 void AppendStmt(CFGBlock *B, Stmt *S) {
134 B->appendStmt(S, cfg->getBumpVectorContext());
135 }
136
137 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
138 B->addSuccessor(S, cfg->getBumpVectorContext());
139 }
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000141 /// TryResult - a class representing a variant over the values
142 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
143 /// and is used by the CFGBuilder to decide if a branch condition
144 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000145 class TryResult {
146 int X;
147 public:
148 TryResult(bool b) : X(b ? 1 : 0) {}
149 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Ted Kremenek941fde82009-07-24 04:47:11 +0000151 bool isTrue() const { return X == 1; }
152 bool isFalse() const { return X == 0; }
153 bool isKnown() const { return X >= 0; }
154 void negate() {
155 assert(isKnown());
156 X ^= 0x1;
157 }
158 };
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Mike Stump00998a02009-07-23 23:25:26 +0000160 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
161 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000162 TryResult TryEvaluateBool(Expr *S) {
Mike Stump00998a02009-07-23 23:25:26 +0000163 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000164 if (!S->isTypeDependent() && !S->isValueDependent() &&
165 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000166 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000167
168 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000169 }
170
Ted Kremenek4102af92008-03-13 03:04:22 +0000171 bool badCFG;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000172};
Mike Stump6d9828c2009-07-17 01:31:16 +0000173
Douglas Gregor898574e2008-12-05 23:32:09 +0000174// FIXME: Add support for dependent-sized array types in C++?
175// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000176static VariableArrayType* FindVA(Type* t) {
177 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
178 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
179 if (vat->getSizeExpr())
180 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000181
Ted Kremenek610a09e2008-09-26 22:58:57 +0000182 t = vt->getElementType().getTypePtr();
183 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000184
Ted Kremenek610a09e2008-09-26 22:58:57 +0000185 return 0;
186}
Mike Stump6d9828c2009-07-17 01:31:16 +0000187
188/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
189/// arbitrary statement. Examples include a single expression or a function
190/// body (compound statement). The ownership of the returned CFG is
191/// transferred to the caller. If CFG construction fails, this method returns
192/// NULL.
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000193CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
194 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000195 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000196 if (!Statement)
197 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000198
Ted Kremenek4102af92008-03-13 03:04:22 +0000199 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000200
201 // Create an empty block that will serve as the exit block for the CFG. Since
202 // this is the first block added to the CFG, it will be implicitly registered
203 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000204 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000205 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000206 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000207
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000208 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000209 CFGBlock* B = addStmt(Statement);
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000210 if (!B)
211 B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000212
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000213 if (B) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000214 // Finalize the last constructed block. This usually involves reversing the
215 // order of the statements in the block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000216 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000217
218 // Backpatch the gotos whose label -> block mappings we didn't know when we
219 // encountered them.
220 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000221 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000222
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000223 CFGBlock* B = *I;
224 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
225 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
226
227 // If there is no target for the goto, then we are looking at an
228 // incomplete AST. Handle this by not registering a successor.
229 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000230
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000231 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000232 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000233
Ted Kremenek19bb3562007-08-28 19:26:49 +0000234 // Add successors to the Indirect Goto Dispatch block (if we have one).
235 if (CFGBlock* B = cfg->getIndirectGotoBlock())
236 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
237 E = AddressTakenLabels.end(); I != E; ++I ) {
238
239 // Lookup the target block.
240 LabelMapTy::iterator LI = LabelMap.find(*I);
241
242 // If there is no target block that contains label, then we are looking
243 // at an incomplete AST. Handle this by not registering a successor.
244 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000245
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000246 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000247 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000248
Ted Kremenek94b33162007-09-17 16:18:02 +0000249 Succ = B;
Ted Kremenek322f58d2007-09-26 21:23:31 +0000250 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000251
252 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000253 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000254
Ted Kremenekda9b30e2009-10-20 23:59:28 +0000255 return badCFG ? NULL : cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000256}
Mike Stump6d9828c2009-07-17 01:31:16 +0000257
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000258/// createBlock - Used to lazily create blocks that are connected
259/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000260CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000261 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000262 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000263 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000264 return B;
265}
Mike Stump6d9828c2009-07-17 01:31:16 +0000266
Ted Kremenek6c249722009-09-24 18:45:41 +0000267/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000268bool CFGBuilder::FinishBlock(CFGBlock* B) {
269 if (badCFG)
270 return false;
271
Ted Kremenek4f880632009-07-17 22:18:43 +0000272 assert(B);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000273 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000274}
275
Ted Kremenek4f880632009-07-17 22:18:43 +0000276/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000277/// blocks for ternary operators, &&, and ||. We also process "," and
278/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4f880632009-07-17 22:18:43 +0000279CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
280tryAgain:
281 switch (S->getStmtClass()) {
282 default:
283 return VisitStmt(S, alwaysAdd);
284
285 case Stmt::AddrLabelExprClass:
286 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Ted Kremenek4f880632009-07-17 22:18:43 +0000288 case Stmt::BinaryOperatorClass:
289 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Ted Kremenek4f880632009-07-17 22:18:43 +0000291 case Stmt::BlockExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000292 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000293
Ted Kremenek4f880632009-07-17 22:18:43 +0000294 case Stmt::BreakStmtClass:
295 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Ted Kremenek4f880632009-07-17 22:18:43 +0000297 case Stmt::CallExprClass:
298 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenek4f880632009-07-17 22:18:43 +0000300 case Stmt::CaseStmtClass:
301 return VisitCaseStmt(cast<CaseStmt>(S));
302
303 case Stmt::ChooseExprClass:
304 return VisitChooseExpr(cast<ChooseExpr>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Ted Kremenek4f880632009-07-17 22:18:43 +0000306 case Stmt::CompoundStmtClass:
307 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Ted Kremenek4f880632009-07-17 22:18:43 +0000309 case Stmt::ConditionalOperatorClass:
310 return VisitConditionalOperator(cast<ConditionalOperator>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Ted Kremenek4f880632009-07-17 22:18:43 +0000312 case Stmt::ContinueStmtClass:
313 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Ted Kremenek4f880632009-07-17 22:18:43 +0000315 case Stmt::DeclStmtClass:
316 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenek4f880632009-07-17 22:18:43 +0000318 case Stmt::DefaultStmtClass:
319 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremenek4f880632009-07-17 22:18:43 +0000321 case Stmt::DoStmtClass:
322 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Ted Kremenek4f880632009-07-17 22:18:43 +0000324 case Stmt::ForStmtClass:
325 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Ted Kremenek4f880632009-07-17 22:18:43 +0000327 case Stmt::GotoStmtClass:
328 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Ted Kremenek4f880632009-07-17 22:18:43 +0000330 case Stmt::IfStmtClass:
331 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremenek4f880632009-07-17 22:18:43 +0000333 case Stmt::IndirectGotoStmtClass:
334 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremenek4f880632009-07-17 22:18:43 +0000336 case Stmt::LabelStmtClass:
337 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek4f880632009-07-17 22:18:43 +0000339 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000340 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
341
Mike Stump0979d802009-07-22 22:56:04 +0000342 case Stmt::CXXThrowExprClass:
343 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
344
Ted Kremenek4f880632009-07-17 22:18:43 +0000345 case Stmt::ObjCAtSynchronizedStmtClass:
346 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek4f880632009-07-17 22:18:43 +0000348 case Stmt::ObjCAtThrowStmtClass:
349 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek4f880632009-07-17 22:18:43 +0000351 case Stmt::ObjCAtTryStmtClass:
352 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremenek4f880632009-07-17 22:18:43 +0000354 case Stmt::ObjCForCollectionStmtClass:
355 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek4f880632009-07-17 22:18:43 +0000357 case Stmt::ParenExprClass:
358 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000359 goto tryAgain;
360
Ted Kremenek4f880632009-07-17 22:18:43 +0000361 case Stmt::NullStmtClass:
362 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek4f880632009-07-17 22:18:43 +0000364 case Stmt::ReturnStmtClass:
365 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek4f880632009-07-17 22:18:43 +0000367 case Stmt::SizeOfAlignOfExprClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000368 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
369
Ted Kremenek4f880632009-07-17 22:18:43 +0000370 case Stmt::StmtExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000371 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek4f880632009-07-17 22:18:43 +0000373 case Stmt::SwitchStmtClass:
374 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenek4f880632009-07-17 22:18:43 +0000376 case Stmt::WhileStmtClass:
377 return VisitWhileStmt(cast<WhileStmt>(S));
378 }
379}
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek4f880632009-07-17 22:18:43 +0000381CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
382 if (alwaysAdd) {
383 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000384 AppendStmt(Block, S);
Mike Stump6d9828c2009-07-17 01:31:16 +0000385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremenek4f880632009-07-17 22:18:43 +0000387 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000388}
Mike Stump6d9828c2009-07-17 01:31:16 +0000389
Ted Kremenek4f880632009-07-17 22:18:43 +0000390/// VisitChildren - Visit the children of a Stmt.
391CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
392 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000393 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000394 E = Terminator->child_end(); I != E; ++I) {
395 if (*I) B = Visit(*I);
396 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000397 return B;
398}
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek4f880632009-07-17 22:18:43 +0000400CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
401 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000402
Ted Kremenek4f880632009-07-17 22:18:43 +0000403 if (alwaysAdd) {
404 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000405 AppendStmt(Block, A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000406 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000407
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000408 return Block;
409}
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Ted Kremenek4f880632009-07-17 22:18:43 +0000411CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
412 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000413 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000414 AppendStmt(ConfluenceBlock, B);
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek4f880632009-07-17 22:18:43 +0000416 if (!FinishBlock(ConfluenceBlock))
417 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Ted Kremenek4f880632009-07-17 22:18:43 +0000419 // create the block evaluating the LHS
420 CFGBlock* LHSBlock = createBlock(false);
421 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Ted Kremenek4f880632009-07-17 22:18:43 +0000423 // create the block evaluating the RHS
424 Succ = ConfluenceBlock;
425 Block = NULL;
426 CFGBlock* RHSBlock = addStmt(B->getRHS());
427 if (!FinishBlock(RHSBlock))
428 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Mike Stump00998a02009-07-23 23:25:26 +0000430 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000431 TryResult KnownVal = TryEvaluateBool(B->getLHS());
432 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
433 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000434
Ted Kremenek4f880632009-07-17 22:18:43 +0000435 // Now link the LHSBlock with RHSBlock.
436 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000437 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
438 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000439 } else {
Ted Kremenek4f880632009-07-17 22:18:43 +0000440 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000441 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
442 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000443 }
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek4f880632009-07-17 22:18:43 +0000445 // Generate the blocks for evaluating the LHS.
446 Block = LHSBlock;
447 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000448 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000449 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000450 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000451 AppendStmt(Block, B);
Ted Kremenek4f880632009-07-17 22:18:43 +0000452 addStmt(B->getRHS());
453 return addStmt(B->getLHS());
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Ted Kremenek4f880632009-07-17 22:18:43 +0000456 return VisitStmt(B, alwaysAdd);
457}
458
Ted Kremenek721903e2009-11-25 01:34:30 +0000459CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, bool alwaysAdd) {
460 if (alwaysAdd) {
461 autoCreateBlock();
462 AppendStmt(Block, E);
463 }
464 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000465}
466
Ted Kremenek4f880632009-07-17 22:18:43 +0000467CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
468 // "break" is a control-flow statement. Thus we stop processing the current
469 // block.
470 if (Block && !FinishBlock(Block))
471 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Ted Kremenek4f880632009-07-17 22:18:43 +0000473 // Now create a new block that ends with the break statement.
474 Block = createBlock(false);
475 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Ted Kremenek4f880632009-07-17 22:18:43 +0000477 // If there is no target for the break, then we are looking at an incomplete
478 // AST. This means that the CFG cannot be constructed.
479 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000480 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000481 else
482 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000483
484
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000485 return Block;
486}
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Ted Kremenek4f880632009-07-17 22:18:43 +0000488CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
489 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000490 bool NoReturn = false;
491 if (C->getCallee()->getType().getNoReturnAttr()) {
492 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000493 }
Mike Stump24556362009-07-25 21:26:53 +0000494
495 if (FunctionDecl *FD = C->getDirectCallee())
496 if (FD->hasAttr<NoReturnAttr>())
497 NoReturn = true;
498
499 if (!NoReturn)
500 return VisitStmt(C, alwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Mike Stump24556362009-07-25 21:26:53 +0000502 if (Block && !FinishBlock(Block))
503 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Mike Stump24556362009-07-25 21:26:53 +0000505 // Create new block with no successor for the remaining pieces.
506 Block = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000507 AppendStmt(Block, C);
Mike Stump24556362009-07-25 21:26:53 +0000508
509 // Wire this to the exit block directly.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000510 AddSuccessor(Block, &cfg->getExit());
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Mike Stump24556362009-07-25 21:26:53 +0000512 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000513}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000514
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000515CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
516 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000517 AppendStmt(ConfluenceBlock, C);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000518 if (!FinishBlock(ConfluenceBlock))
519 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000521 Succ = ConfluenceBlock;
522 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000523 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000524 if (!FinishBlock(LHSBlock))
525 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000527 Succ = ConfluenceBlock;
528 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000529 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000530 if (!FinishBlock(RHSBlock))
531 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000533 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000534 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000535 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000536 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
537 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000538 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000539 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000540}
Mike Stump1eb44332009-09-09 15:08:12 +0000541
542
543CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
544 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000545
546 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
547 I != E; ++I ) {
548 LastBlock = addStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000550 if (badCFG)
551 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000552 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000553 return LastBlock;
554}
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000556CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
557 // Create the confluence block that will "merge" the results of the ternary
558 // expression.
559 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000560 AppendStmt(ConfluenceBlock, C);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000561 if (!FinishBlock(ConfluenceBlock))
562 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000564 // Create a block for the LHS expression if there is an LHS expression. A
565 // GCC extension allows LHS to be NULL, causing the condition to be the
566 // value that is returned instead.
567 // e.g: x ?: y is shorthand for: x ? x : y;
568 Succ = ConfluenceBlock;
569 Block = NULL;
570 CFGBlock* LHSBlock = NULL;
571 if (C->getLHS()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000572 LHSBlock = addStmt(C->getLHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000573 if (!FinishBlock(LHSBlock))
574 return 0;
575 Block = NULL;
576 }
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000578 // Create the block for the RHS expression.
579 Succ = ConfluenceBlock;
Ted Kremenek4f880632009-07-17 22:18:43 +0000580 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000581 if (!FinishBlock(RHSBlock))
582 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000584 // Create the block that will contain the condition.
585 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Mike Stump00998a02009-07-23 23:25:26 +0000587 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000588 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000589 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000590 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000591 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000592 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000593 // If we know the condition is false, add NULL as the successor for
594 // the block containing the condition. In this case, the confluence
595 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000596 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000597 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000598 } else {
599 // If we have no LHS expression, add the ConfluenceBlock as a direct
600 // successor for the block containing the condition. Moreover, we need to
601 // reverse the order of the predecessors in the ConfluenceBlock because
602 // the RHSBlock will have been added to the succcessors already, and we
603 // want the first predecessor to the the block containing the expression
604 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000605 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000606 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000607 std::reverse(ConfluenceBlock->pred_begin(),
608 ConfluenceBlock->pred_end());
609 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000610 }
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000612 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000613 Block->setTerminator(C);
614 return addStmt(C->getCond());
615}
616
Ted Kremenek4f880632009-07-17 22:18:43 +0000617CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
618 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000619
Ted Kremenek4f880632009-07-17 22:18:43 +0000620 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000621 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000622 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000623 }
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Ted Kremenek4f880632009-07-17 22:18:43 +0000625 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Ted Kremenek4f880632009-07-17 22:18:43 +0000627 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
628 typedef llvm::SmallVector<Decl*,10> BufTy;
629 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Ted Kremenek4f880632009-07-17 22:18:43 +0000631 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
632 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
633 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
634 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Ted Kremenek4f880632009-07-17 22:18:43 +0000636 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
637 // automatically freed with the CFG.
638 DeclGroupRef DG(*I);
639 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000640 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000641 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Ted Kremenek4f880632009-07-17 22:18:43 +0000643 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000644 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000645 B = VisitDeclSubExpr(D);
646 }
Mike Stump1eb44332009-09-09 15:08:12 +0000647
648 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000649}
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Ted Kremenek4f880632009-07-17 22:18:43 +0000651/// VisitDeclSubExpr - Utility method to add block-level expressions for
652/// initializers in Decls.
653CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
654 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000655
Ted Kremenek4f880632009-07-17 22:18:43 +0000656 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek4f880632009-07-17 22:18:43 +0000658 if (!VD)
659 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Ted Kremenek4f880632009-07-17 22:18:43 +0000661 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Ted Kremenek4f880632009-07-17 22:18:43 +0000663 if (Init) {
664 // Optimization: Don't create separate block-level statements for literals.
665 switch (Init->getStmtClass()) {
666 case Stmt::IntegerLiteralClass:
667 case Stmt::CharacterLiteralClass:
668 case Stmt::StringLiteralClass:
669 break;
670 default:
671 Block = addStmt(Init);
672 }
673 }
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Ted Kremenek4f880632009-07-17 22:18:43 +0000675 // If the type of VD is a VLA, then we must process its size expressions.
676 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
677 VA = FindVA(VA->getElementType().getTypePtr()))
678 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Ted Kremenek4f880632009-07-17 22:18:43 +0000680 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000681}
682
683CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000684 // We may see an if statement in the middle of a basic block, or it may be the
685 // first statement we are processing. In either case, we create a new basic
686 // block. First, we create the blocks for the then...else statements, and
687 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000688 // middle of a block, we stop processing that block. That block is then the
689 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000690
691 // The block we were proccessing is now finished. Make it the successor
692 // block.
693 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000694 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000695 if (!FinishBlock(Block))
696 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000697 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000698
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000699 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000700 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000701
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000702 if (Stmt* Else = I->getElse()) {
703 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000704
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000705 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000706 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000707 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000708 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000709
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000710 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
711 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000712 else if (Block) {
713 if (!FinishBlock(ElseBlock))
714 return 0;
715 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000716 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000717
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000718 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000719 CFGBlock* ThenBlock;
720 {
721 Stmt* Then = I->getThen();
722 assert (Then);
723 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000724 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000725 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000726
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000727 if (!ThenBlock) {
728 // We can reach here if the "then" body has all NullStmts.
729 // Create an empty block so we can distinguish between true and false
730 // branches in path-sensitive analyses.
731 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000732 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000733 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000734 if (!FinishBlock(ThenBlock))
735 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000736 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000737 }
738
Mike Stump6d9828c2009-07-17 01:31:16 +0000739 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000740 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000741
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000742 // Set the terminator of the new block to the If statement.
743 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000744
Mike Stump00998a02009-07-23 23:25:26 +0000745 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000746 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000747
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000748 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000749 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
750 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000751
752 // Add the condition as the last statement in the new block. This may create
753 // new blocks as the condition may contain control-flow. Any newly created
754 // blocks will be pointed to be "Block".
Ted Kremenek4f880632009-07-17 22:18:43 +0000755 return addStmt(I->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000756}
Mike Stump6d9828c2009-07-17 01:31:16 +0000757
758
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000759CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000760 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000761 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000762 // NOTE: If a "return" appears in the middle of a block, this means that the
763 // code afterwards is DEAD (unreachable). We still keep a basic block
764 // for that code; a simple "mark-and-sweep" from the entry block will be
765 // able to report such dead blocks.
Ted Kremenek6c249722009-09-24 18:45:41 +0000766 if (Block)
767 FinishBlock(Block);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000768
769 // Create the new block.
770 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000771
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000772 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000773 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000774
775 // Add the return statement to the block. This may create new blocks if R
776 // contains control-flow (short-circuit operations).
Ted Kremenek4f880632009-07-17 22:18:43 +0000777 return VisitStmt(R, true);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000778}
779
780CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
781 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000782 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000783 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000784
Ted Kremenek4f880632009-07-17 22:18:43 +0000785 if (!LabelBlock) // This can happen when the body is empty, i.e.
786 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000787
Ted Kremenek4f880632009-07-17 22:18:43 +0000788 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000789 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000790
791 // Labels partition blocks, so this is the end of the basic block we were
792 // processing (L is the block's label). Because this is label (and we have
793 // already processed the substatement) there is no extra control-flow to worry
794 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000795 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000796 if (!FinishBlock(LabelBlock))
797 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000798
799 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000800 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000801
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000802 // This block is now the implicit successor of other blocks.
803 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000804
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000805 return LabelBlock;
806}
807
808CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000809 // Goto is a control-flow statement. Thus we stop processing the current
810 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000811 if (Block)
812 FinishBlock(Block);
813
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000814 Block = createBlock(false);
815 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000816
817 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000818 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000819
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000820 if (I == LabelMap.end())
821 // We will need to backpatch this block later.
822 BackpatchBlocks.push_back(Block);
823 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000824 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000825
Mike Stump6d9828c2009-07-17 01:31:16 +0000826 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000827}
828
829CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000830 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000831
Mike Stumpfefb9f72009-07-21 01:12:51 +0000832 // "for" is a control-flow statement. Thus we stop processing the current
833 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000834 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000835 if (!FinishBlock(Block))
836 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000837 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000838 } else
839 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000840
841 // Because of short-circuit evaluation, the condition of the loop can span
842 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
843 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000844 CFGBlock* ExitConditionBlock = createBlock(false);
845 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000846
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000847 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000848 ExitConditionBlock->setTerminator(F);
849
850 // Now add the actual condition to the condition block. Because the condition
851 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000852 if (Stmt* C = F->getCond()) {
853 Block = ExitConditionBlock;
854 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000855 if (Block) {
856 if (!FinishBlock(EntryConditionBlock))
857 return 0;
858 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000859 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000860
Mike Stump6d9828c2009-07-17 01:31:16 +0000861 // The condition block is the implicit successor for the loop body as well as
862 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000863 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000864
Mike Stump00998a02009-07-23 23:25:26 +0000865 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000866 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Mike Stump00998a02009-07-23 23:25:26 +0000868 if (F->getCond())
869 KnownVal = TryEvaluateBool(F->getCond());
870
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000871 // Now create the loop body.
872 {
873 assert (F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000874
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000875 // Save the current values for Block, Succ, and continue and break targets
876 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
877 save_continue(ContinueTargetBlock),
Mike Stump6d9828c2009-07-17 01:31:16 +0000878 save_break(BreakTargetBlock);
879
Ted Kremenekaf603f72007-08-30 18:39:40 +0000880 // Create a new block to contain the (bottom) of the loop body.
881 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000882
Ted Kremeneke9334502008-09-04 21:48:47 +0000883 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000884 // Generate increment code in its own basic block. This is the target of
885 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +0000886 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000887 } else {
888 // No increment code. Create a special, empty, block that is used as the
889 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +0000890 assert(Succ == EntryConditionBlock);
891 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +0000892 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000893
Ted Kremenek3575f842009-04-28 00:51:56 +0000894 // Finish up the increment (or empty) block if it hasn't been already.
895 if (Block) {
896 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000897 if (!FinishBlock(Block))
898 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +0000899 Block = 0;
900 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000901
Ted Kremenek3575f842009-04-28 00:51:56 +0000902 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000903
Ted Kremenek3575f842009-04-28 00:51:56 +0000904 // The starting block for the loop increment is the block that should
905 // represent the 'loop target' for looping back to the start of the loop.
906 ContinueTargetBlock->setLoopTarget(F);
907
Ted Kremeneke9334502008-09-04 21:48:47 +0000908 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +0000909 BreakTargetBlock = LoopSuccessor;
910
911 // Now populate the body block, and in the process create new blocks as we
912 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +0000913 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000914
915 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +0000916 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +0000917 else if (Block && !FinishBlock(BodyBlock))
918 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000919
Ted Kremenek941fde82009-07-24 04:47:11 +0000920 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000921 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000922 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000923
Ted Kremenek941fde82009-07-24 04:47:11 +0000924 // Link up the condition block with the code that follows the loop. (the
925 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000926 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +0000927
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000928 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +0000929 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000930 if (Stmt* I = F->getInit()) {
931 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000932 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000933 } else {
934 // There is no loop initialization. We are thus basically a while loop.
935 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000936 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +0000937 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000938 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000939 }
940}
941
Ted Kremenek514de5a2008-11-11 17:10:00 +0000942CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
943 // Objective-C fast enumeration 'for' statements:
944 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
945 //
946 // for ( Type newVariable in collection_expression ) { statements }
947 //
948 // becomes:
949 //
950 // prologue:
951 // 1. collection_expression
952 // T. jump to loop_entry
953 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000954 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +0000955 // 1. ObjCForCollectionStmt [performs binding to newVariable]
956 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
957 // TB:
958 // statements
959 // T. jump to loop_entry
960 // FB:
961 // what comes after
962 //
963 // and
964 //
965 // Type existingItem;
966 // for ( existingItem in expression ) { statements }
967 //
968 // becomes:
969 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000970 // the same with newVariable replaced with existingItem; the binding works
971 // the same except that for one ObjCForCollectionStmt::getElement() returns
972 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +0000973 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000974
Ted Kremenek514de5a2008-11-11 17:10:00 +0000975 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000976
Ted Kremenek514de5a2008-11-11 17:10:00 +0000977 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000978 if (!FinishBlock(Block))
979 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +0000980 LoopSuccessor = Block;
981 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +0000982 } else
983 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000984
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000985 // Build the condition blocks.
986 CFGBlock* ExitConditionBlock = createBlock(false);
987 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000988
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000989 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000990 ExitConditionBlock->setTerminator(S);
991
992 // The last statement in the block should be the ObjCForCollectionStmt, which
993 // performs the actual binding to 'element' and determines if there are any
994 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000995 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000996 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000997
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000998 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +0000999 // generate new blocks as necesary. We DON'T add the statement by default to
1000 // the CFG unless it contains control-flow.
Ted Kremenek4f880632009-07-17 22:18:43 +00001001 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001002 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001003 if (!FinishBlock(EntryConditionBlock))
1004 return 0;
1005 Block = 0;
1006 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001007
1008 // The condition block is the implicit successor for the loop body as well as
1009 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001010 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001011
Ted Kremenek514de5a2008-11-11 17:10:00 +00001012 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001013 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001014 // Save the current values for Succ, continue and break targets.
1015 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001016 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1017
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001018 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001019 ContinueTargetBlock = EntryConditionBlock;
1020
Ted Kremenek4f880632009-07-17 22:18:43 +00001021 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001022
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001023 if (!BodyBlock)
1024 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001025 else if (Block) {
1026 if (!FinishBlock(BodyBlock))
1027 return 0;
1028 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001029
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001030 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001031 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001032 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001033
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001034 // Link up the condition block with the code that follows the loop.
1035 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001036 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001037
Ted Kremenek514de5a2008-11-11 17:10:00 +00001038 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001039 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001040 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001041}
1042
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001043CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1044 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001045
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001046 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001047 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001048
Ted Kremenekda5348e2009-05-05 23:11:51 +00001049 // The sync body starts its own basic block. This makes it a little easier
1050 // for diagnostic clients.
1051 if (SyncBlock) {
1052 if (!FinishBlock(SyncBlock))
1053 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001054
Ted Kremenekda5348e2009-05-05 23:11:51 +00001055 Block = 0;
1056 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001057
Ted Kremenekda5348e2009-05-05 23:11:51 +00001058 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001059
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001060 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001061 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001062}
Mike Stump6d9828c2009-07-17 01:31:16 +00001063
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001064CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001065 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001066 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001067}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001068
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001069CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001070 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001071
Mike Stumpfefb9f72009-07-21 01:12:51 +00001072 // "while" is a control-flow statement. Thus we stop processing the current
1073 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001074 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001075 if (!FinishBlock(Block))
1076 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001077 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001078 } else
1079 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001080
1081 // Because of short-circuit evaluation, the condition of the loop can span
1082 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1083 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001084 CFGBlock* ExitConditionBlock = createBlock(false);
1085 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001086
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001087 // Set the terminator for the "exit" condition block.
1088 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001089
1090 // Now add the actual condition to the condition block. Because the condition
1091 // itself may contain control-flow, new blocks may be created. Thus we update
1092 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001093 if (Stmt* C = W->getCond()) {
1094 Block = ExitConditionBlock;
1095 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001096 assert(Block == EntryConditionBlock);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001097 if (Block) {
1098 if (!FinishBlock(EntryConditionBlock))
1099 return 0;
1100 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001101 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001102
1103 // The condition block is the implicit successor for the loop body as well as
1104 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001105 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001106
Mike Stump00998a02009-07-23 23:25:26 +00001107 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001108 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001109
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001110 // Process the loop body.
1111 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001112 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001113
1114 // Save the current values for Block, Succ, and continue and break targets
1115 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1116 save_continue(ContinueTargetBlock),
1117 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001118
Mike Stump6d9828c2009-07-17 01:31:16 +00001119 // Create an empty block to represent the transition block for looping back
1120 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001121 Block = 0;
1122 assert(Succ == EntryConditionBlock);
1123 Succ = createBlock();
1124 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001125 ContinueTargetBlock = Succ;
1126
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001127 // All breaks should go to the code following the loop.
1128 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001129
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001130 // NULL out Block to force lazy instantiation of blocks for the body.
1131 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001132
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001133 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001134 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001135
Ted Kremenekaf603f72007-08-30 18:39:40 +00001136 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001137 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001138 else if (Block) {
1139 if (!FinishBlock(BodyBlock))
1140 return 0;
1141 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001142
Ted Kremenek941fde82009-07-24 04:47:11 +00001143 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001144 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001145 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001146
Ted Kremenek941fde82009-07-24 04:47:11 +00001147 // Link up the condition block with the code that follows the loop. (the
1148 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001149 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001150
1151 // There can be no more statements in the condition block since we loop back
1152 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001153 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001154
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001155 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001156 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001157 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001158}
Mike Stump1eb44332009-09-09 15:08:12 +00001159
1160
Ted Kremenek4f880632009-07-17 22:18:43 +00001161CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1162 // FIXME: For now we pretend that @catch and the code it contains does not
1163 // exit.
1164 return Block;
1165}
Mike Stump6d9828c2009-07-17 01:31:16 +00001166
Ted Kremenek2fda5042008-12-09 20:20:09 +00001167CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1168 // FIXME: This isn't complete. We basically treat @throw like a return
1169 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001170
Ted Kremenek6c249722009-09-24 18:45:41 +00001171 // If we were in the middle of a block we stop processing that block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001172 if (Block && !FinishBlock(Block))
1173 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001174
Ted Kremenek2fda5042008-12-09 20:20:09 +00001175 // Create the new block.
1176 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001177
Ted Kremenek2fda5042008-12-09 20:20:09 +00001178 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001179 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001180
1181 // Add the statement to the block. This may create new blocks if S contains
1182 // control-flow (short-circuit operations).
Ted Kremenek4f880632009-07-17 22:18:43 +00001183 return VisitStmt(S, true);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001184}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001185
Mike Stump0979d802009-07-22 22:56:04 +00001186CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001187 // If we were in the middle of a block we stop processing that block.
Mike Stump0979d802009-07-22 22:56:04 +00001188 if (Block && !FinishBlock(Block))
1189 return 0;
1190
1191 // Create the new block.
1192 Block = createBlock(false);
1193
1194 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001195 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001196
1197 // Add the statement to the block. This may create new blocks if S contains
1198 // control-flow (short-circuit operations).
1199 return VisitStmt(T, true);
1200}
1201
Ted Kremenek4f880632009-07-17 22:18:43 +00001202CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001203 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001204
Mike Stump8f9893a2009-07-21 01:27:50 +00001205 // "do...while" is a control-flow statement. Thus we stop processing the
1206 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001207 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001208 if (!FinishBlock(Block))
1209 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001210 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001211 } else
1212 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001213
1214 // Because of short-circuit evaluation, the condition of the loop can span
1215 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1216 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001217 CFGBlock* ExitConditionBlock = createBlock(false);
1218 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001219
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001220 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001221 ExitConditionBlock->setTerminator(D);
1222
1223 // Now add the actual condition to the condition block. Because the condition
1224 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001225 if (Stmt* C = D->getCond()) {
1226 Block = ExitConditionBlock;
1227 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001228 if (Block) {
1229 if (!FinishBlock(EntryConditionBlock))
1230 return 0;
1231 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001232 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001233
Ted Kremenek54827132008-02-27 07:20:00 +00001234 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001235 Succ = EntryConditionBlock;
1236
Mike Stump00998a02009-07-23 23:25:26 +00001237 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001238 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001239
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001240 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001241 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001242 {
1243 assert (D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001244
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001245 // Save the current values for Block, Succ, and continue and break targets
1246 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1247 save_continue(ContinueTargetBlock),
1248 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001249
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001250 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001251 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001252
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001253 // All breaks should go to the code following the loop.
1254 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001255
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001256 // NULL out Block to force lazy instantiation of blocks for the body.
1257 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001258
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001259 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001260 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001261
Ted Kremenekaf603f72007-08-30 18:39:40 +00001262 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001263 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001264 else if (Block) {
1265 if (!FinishBlock(BodyBlock))
1266 return 0;
1267 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001268
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001269 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001270 // ExitConditionBlock to represent the "loop back" transition. Create an
1271 // empty block to represent the transition block for looping back to the
1272 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001273 // FIXME: Can we do this more efficiently without adding another block?
1274 Block = NULL;
1275 Succ = BodyBlock;
1276 CFGBlock *LoopBackBlock = createBlock();
1277 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001278
Ted Kremenek941fde82009-07-24 04:47:11 +00001279 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001280 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001281 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001282
Ted Kremenek941fde82009-07-24 04:47:11 +00001283 // Link up the condition block with the code that follows the loop.
1284 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001285 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001286
1287 // There can be no more statements in the body block(s) since we loop back to
1288 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001289 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001290
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001291 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001292 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001293 return BodyBlock;
1294}
1295
1296CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1297 // "continue" is a control-flow statement. Thus we stop processing the
1298 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001299 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001300 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001301
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001302 // Now create a new block that ends with the continue statement.
1303 Block = createBlock(false);
1304 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001305
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001306 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001307 // incomplete AST. This means the CFG cannot be constructed.
1308 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001309 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001310 else
1311 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001312
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001313 return Block;
1314}
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001316CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1317 bool alwaysAdd) {
1318
1319 if (alwaysAdd) {
1320 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001321 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001322 }
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Ted Kremenek4f880632009-07-17 22:18:43 +00001324 // VLA types have expressions that must be evaluated.
1325 if (E->isArgumentType()) {
1326 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1327 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1328 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001329 }
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Mike Stump6d9828c2009-07-17 01:31:16 +00001331 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001332}
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Ted Kremenek4f880632009-07-17 22:18:43 +00001334/// VisitStmtExpr - Utility method to handle (nested) statement
1335/// expressions (a GCC extension).
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001336CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1337 if (alwaysAdd) {
1338 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001339 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001340 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001341 return VisitCompoundStmt(SE->getSubStmt());
1342}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001343
Ted Kremenek411cdee2008-04-16 21:10:48 +00001344CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001345 // "switch" is a control-flow statement. Thus we stop processing the current
1346 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001347 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001348
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001349 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001350 if (!FinishBlock(Block))
1351 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001352 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001353 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001354
1355 // Save the current "switch" context.
1356 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001357 save_break(BreakTargetBlock),
1358 save_default(DefaultCaseBlock);
1359
Mike Stump6d9828c2009-07-17 01:31:16 +00001360 // Set the "default" case to be the block after the switch statement. If the
1361 // switch statement contains a "default:", this value will be overwritten with
1362 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001363 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001364
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001365 // Create a new block that will contain the switch statement.
1366 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001367
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001368 // Now process the switch body. The code after the switch is the implicit
1369 // successor.
1370 Succ = SwitchSuccessor;
1371 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001372
1373 // When visiting the body, the case statements should automatically get linked
1374 // up to the switch. We also don't keep a pointer to the body, since all
1375 // control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001376 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001377 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001378 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001379 if (Block) {
1380 if (!FinishBlock(BodyBlock))
1381 return 0;
1382 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001383
Mike Stump6d9828c2009-07-17 01:31:16 +00001384 // If we have no "default:" case, the default transition is to the code
1385 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001386 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001387
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001388 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001389 SwitchTerminatedBlock->setTerminator(Terminator);
1390 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001391 Block = SwitchTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001392
Ted Kremenek411cdee2008-04-16 21:10:48 +00001393 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001394}
1395
Ted Kremenek4f880632009-07-17 22:18:43 +00001396CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001397 // CaseStmts are essentially labels, so they are the first statement in a
1398 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001399
Ted Kremenek4f880632009-07-17 22:18:43 +00001400 if (CS->getSubStmt())
1401 addStmt(CS->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001403 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001404 if (!CaseBlock)
1405 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001406
1407 // Cases statements partition blocks, so this is the top of the basic block we
1408 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001409 CaseBlock->setLabel(CS);
1410
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001411 if (!FinishBlock(CaseBlock))
1412 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001413
1414 // Add this block to the list of successors for the block with the switch
1415 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001416 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001417 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001418
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001419 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1420 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001421
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001422 // This block is now the implicit successor of other blocks.
1423 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001424
Ted Kremenek2677ea82008-03-15 07:45:02 +00001425 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001426}
Mike Stump6d9828c2009-07-17 01:31:16 +00001427
Ted Kremenek411cdee2008-04-16 21:10:48 +00001428CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001429 if (Terminator->getSubStmt())
1430 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001432 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001433
1434 if (!DefaultCaseBlock)
1435 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001436
1437 // Default statements partition blocks, so this is the top of the basic block
1438 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001439 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001441 if (!FinishBlock(DefaultCaseBlock))
1442 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001443
Mike Stump6d9828c2009-07-17 01:31:16 +00001444 // Unlike case statements, we don't add the default block to the successors
1445 // for the switch statement immediately. This is done when we finish
1446 // processing the switch statement. This allows for the default case
1447 // (including a fall-through to the code after the switch statement) to always
1448 // be the last successor of a switch-terminated block.
1449
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001450 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1451 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001452
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001453 // This block is now the implicit successor of other blocks.
1454 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001455
1456 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001457}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001458
Ted Kremenek19bb3562007-08-28 19:26:49 +00001459CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001460 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001461 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001462
Ted Kremenek19bb3562007-08-28 19:26:49 +00001463 if (!IBlock) {
1464 IBlock = createBlock(false);
1465 cfg->setIndirectGotoBlock(IBlock);
1466 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001467
Ted Kremenek19bb3562007-08-28 19:26:49 +00001468 // IndirectGoto is a control-flow statement. Thus we stop processing the
1469 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001470 if (Block && !FinishBlock(Block))
1471 return 0;
1472
Ted Kremenek19bb3562007-08-28 19:26:49 +00001473 Block = createBlock(false);
1474 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001475 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001476 return addStmt(I->getTarget());
1477}
1478
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001479} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001480
Mike Stump6d9828c2009-07-17 01:31:16 +00001481/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1482/// no successors or predecessors. If this is the first block created in the
1483/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001484CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001485 bool first_block = begin() == end();
1486
1487 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001488 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1489 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1490 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001491
1492 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001493 if (first_block)
1494 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001495
1496 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001497 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001498}
1499
Ted Kremenek026473c2007-08-23 16:51:22 +00001500/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1501/// CFG is returned to the caller.
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001502CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001503 CFGBuilder Builder;
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001504 return Builder.buildCFG(Statement, C);
Ted Kremenek026473c2007-08-23 16:51:22 +00001505}
1506
Ted Kremenek63f58872007-10-01 19:33:33 +00001507//===----------------------------------------------------------------------===//
1508// CFG: Queries for BlkExprs.
1509//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001510
Ted Kremenek63f58872007-10-01 19:33:33 +00001511namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001512 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001513}
1514
Ted Kremenek411cdee2008-04-16 21:10:48 +00001515static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1516 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001517 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001518
Ted Kremenek411cdee2008-04-16 21:10:48 +00001519 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001520 if (!*I) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001521
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001522 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1523 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001524
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001525 FindSubExprAssignments(*I, Set);
1526 }
1527}
1528
Ted Kremenek63f58872007-10-01 19:33:33 +00001529static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1530 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001531
1532 // Look for assignments that are used as subexpressions. These are the only
1533 // assignments that we want to *possibly* register as a block-level
1534 // expression. Basically, if an assignment occurs both in a subexpression and
1535 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001536 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001537
Ted Kremenek63f58872007-10-01 19:33:33 +00001538 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001539 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001540 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001541
Ted Kremenek411cdee2008-04-16 21:10:48 +00001542 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001543
1544 // Iterate over the statements again on identify the Expr* and Stmt* at the
1545 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001546
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001547 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001548 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001549
Ted Kremenek411cdee2008-04-16 21:10:48 +00001550 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001551 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001552 // expression are really "statements" whose value is never used by
1553 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001554 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001555 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001556 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1557 // Special handling for statement expressions. The last statement in
1558 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001559 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001560 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001561 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001562 (*M)[C->body_back()] = x;
1563 }
1564 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001565
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001566 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001567 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001568 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001569
Ted Kremenek411cdee2008-04-16 21:10:48 +00001570 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001571
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001572 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001573
Ted Kremenek390e48b2008-11-12 21:11:49 +00001574 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001575 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001576 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001577 }
1578 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001579
Ted Kremenek63f58872007-10-01 19:33:33 +00001580 return M;
1581}
1582
Ted Kremenek86946742008-01-17 20:48:37 +00001583CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1584 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001585 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001586
Ted Kremenek63f58872007-10-01 19:33:33 +00001587 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001588 BlkExprMapTy::iterator I = M->find(S);
Mike Stump6d9828c2009-07-17 01:31:16 +00001589
Ted Kremenek63f58872007-10-01 19:33:33 +00001590 if (I == M->end()) return CFG::BlkExprNumTy();
1591 else return CFG::BlkExprNumTy(I->second);
1592}
1593
1594unsigned CFG::getNumBlkExprs() {
1595 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1596 return M->size();
1597 else {
1598 // We assume callers interested in the number of BlkExprs will want
1599 // the map constructed if it doesn't already exist.
1600 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1601 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1602 }
1603}
1604
Ted Kremenek274f4332008-04-28 18:00:46 +00001605//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001606// Cleanup: CFG dstor.
1607//===----------------------------------------------------------------------===//
1608
Ted Kremenek63f58872007-10-01 19:33:33 +00001609CFG::~CFG() {
1610 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1611}
Mike Stump6d9828c2009-07-17 01:31:16 +00001612
Ted Kremenek7dba8602007-08-29 21:56:09 +00001613//===----------------------------------------------------------------------===//
1614// CFG pretty printing
1615//===----------------------------------------------------------------------===//
1616
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001617namespace {
1618
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001619class StmtPrinterHelper : public PrinterHelper {
Mike Stump6d9828c2009-07-17 01:31:16 +00001620
Ted Kremenek42a509f2007-08-31 21:30:12 +00001621 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1622 StmtMapTy StmtMap;
1623 signed CurrentBlock;
1624 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001625 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001626public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001627
Chris Lattnere4f21422009-06-30 01:26:17 +00001628 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1629 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001630 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1631 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001632 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001633 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001634 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001635 }
1636 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001637
Ted Kremenek42a509f2007-08-31 21:30:12 +00001638 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001639
Chris Lattnere4f21422009-06-30 01:26:17 +00001640 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001641 void setBlockID(signed i) { CurrentBlock = i; }
1642 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001643
Ted Kremeneka95d3752008-09-13 05:16:45 +00001644 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001645
Ted Kremenek411cdee2008-04-16 21:10:48 +00001646 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001647
1648 if (I == StmtMap.end())
1649 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001650
1651 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek42a509f2007-08-31 21:30:12 +00001652 && I->second.second == CurrentStmt)
1653 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001654
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001655 OS << "[B" << I->second.first << "." << I->second.second << "]";
1656 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001657 }
1658};
Chris Lattnere4f21422009-06-30 01:26:17 +00001659} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001660
Chris Lattnere4f21422009-06-30 01:26:17 +00001661
1662namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001663class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001664 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001665
Ted Kremeneka95d3752008-09-13 05:16:45 +00001666 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001667 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001668 PrintingPolicy Policy;
1669
Ted Kremenek42a509f2007-08-31 21:30:12 +00001670public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001671 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001672 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001673 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001674
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001675 void VisitIfStmt(IfStmt* I) {
1676 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001677 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001678 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001679
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001680 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001681 void VisitStmt(Stmt* Terminator) {
1682 Terminator->printPretty(OS, Helper, Policy);
1683 }
1684
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001685 void VisitForStmt(ForStmt* F) {
1686 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001687 if (F->getInit()) OS << "...";
1688 OS << "; ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001689 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001690 OS << "; ";
1691 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001692 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001693 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001694
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001695 void VisitWhileStmt(WhileStmt* W) {
1696 OS << "while " ;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001697 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001698 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001699
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001700 void VisitDoStmt(DoStmt* D) {
1701 OS << "do ... while ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001702 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001703 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001704
Ted Kremenek411cdee2008-04-16 21:10:48 +00001705 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001706 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001707 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001708 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001709
Ted Kremenek805e9a82007-08-31 21:49:40 +00001710 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001711 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001712 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001713 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001714
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001715 void VisitChooseExpr(ChooseExpr* C) {
1716 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001717 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001718 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001719 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001720
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001721 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1722 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001723 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001724 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001725
Ted Kremenek805e9a82007-08-31 21:49:40 +00001726 void VisitBinaryOperator(BinaryOperator* B) {
1727 if (!B->isLogicalOp()) {
1728 VisitExpr(B);
1729 return;
1730 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001731
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001732 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001733
Ted Kremenek805e9a82007-08-31 21:49:40 +00001734 switch (B->getOpcode()) {
1735 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001736 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001737 return;
1738 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001739 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001740 return;
1741 default:
1742 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001743 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00001744 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001745
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001746 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001747 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001748 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001749};
Chris Lattnere4f21422009-06-30 01:26:17 +00001750} // end anonymous namespace
1751
Mike Stump6d9828c2009-07-17 01:31:16 +00001752
Chris Lattnere4f21422009-06-30 01:26:17 +00001753static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1754 Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001755 if (Helper) {
1756 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001757 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001758 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00001759
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001760 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001761 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001762 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001763 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001764 return;
1765 }
1766 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001767
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001768 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001769 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001770 if (B->getOpcode() == BinaryOperator::Comma) {
1771 OS << "... , ";
1772 Helper->handledStmt(B->getRHS(),OS);
1773 OS << '\n';
1774 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001775 }
1776 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001777 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001778
Chris Lattnere4f21422009-06-30 01:26:17 +00001779 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00001780
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001781 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001782 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001783}
Mike Stump6d9828c2009-07-17 01:31:16 +00001784
Chris Lattnere4f21422009-06-30 01:26:17 +00001785static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1786 const CFGBlock& B,
1787 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001788
Ted Kremenek42a509f2007-08-31 21:30:12 +00001789 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00001790
Ted Kremenek7dba8602007-08-29 21:56:09 +00001791 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00001792 OS << "\n [ B" << B.getBlockID();
1793
Ted Kremenek42a509f2007-08-31 21:30:12 +00001794 if (&B == &cfg->getEntry())
1795 OS << " (ENTRY) ]\n";
1796 else if (&B == &cfg->getExit())
1797 OS << " (EXIT) ]\n";
1798 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001799 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001800 else
1801 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00001802
Ted Kremenek9cffe732007-08-29 23:20:49 +00001803 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001804 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001805
1806 if (print_edges)
1807 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001808
Ted Kremenek411cdee2008-04-16 21:10:48 +00001809 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001810 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001811 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001812 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001813 C->getLHS()->printPretty(OS, Helper,
1814 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001815 if (C->getRHS()) {
1816 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001817 C->getRHS()->printPretty(OS, Helper,
1818 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001819 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001820 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001821 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001822 else
1823 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001824
Ted Kremenek9cffe732007-08-29 23:20:49 +00001825 OS << ":\n";
1826 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001827
Ted Kremenekfddd5182007-08-21 21:42:03 +00001828 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001829 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00001830
Ted Kremenek42a509f2007-08-31 21:30:12 +00001831 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1832 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001833
Ted Kremenek9cffe732007-08-29 23:20:49 +00001834 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001835 if (print_edges)
1836 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001837
Ted Kremeneka95d3752008-09-13 05:16:45 +00001838 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001839
Ted Kremenek42a509f2007-08-31 21:30:12 +00001840 if (Helper)
1841 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00001842
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001843 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001844 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001845
Ted Kremenek9cffe732007-08-29 23:20:49 +00001846 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001847 if (B.getTerminator()) {
1848 if (print_edges)
1849 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001850
Ted Kremenek9cffe732007-08-29 23:20:49 +00001851 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001852
Ted Kremenek42a509f2007-08-31 21:30:12 +00001853 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00001854
Chris Lattnere4f21422009-06-30 01:26:17 +00001855 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1856 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00001857 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001858 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001859 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001860
Ted Kremenek9cffe732007-08-29 23:20:49 +00001861 if (print_edges) {
1862 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001863 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001864 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001865
Ted Kremenek42a509f2007-08-31 21:30:12 +00001866 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1867 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001868
Ted Kremenek42a509f2007-08-31 21:30:12 +00001869 if (i == 8 || (i-8) == 0)
1870 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001871
Ted Kremenek9cffe732007-08-29 23:20:49 +00001872 OS << " B" << (*I)->getBlockID();
1873 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001874
Ted Kremenek42a509f2007-08-31 21:30:12 +00001875 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00001876
Ted Kremenek42a509f2007-08-31 21:30:12 +00001877 // Print the successors of this block.
1878 OS << " Successors (" << B.succ_size() << "):";
1879 i = 0;
1880
1881 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1882 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001883
Ted Kremenek42a509f2007-08-31 21:30:12 +00001884 if (i == 8 || (i-8) % 10 == 0)
1885 OS << "\n ";
1886
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001887 if (*I)
1888 OS << " B" << (*I)->getBlockID();
1889 else
1890 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001891 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001892
Ted Kremenek9cffe732007-08-29 23:20:49 +00001893 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001894 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001895}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001896
Ted Kremenek42a509f2007-08-31 21:30:12 +00001897
1898/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001899void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001900
1901/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00001902void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1903 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00001904
Ted Kremenek42a509f2007-08-31 21:30:12 +00001905 // Print the entry block.
1906 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00001907
Ted Kremenek42a509f2007-08-31 21:30:12 +00001908 // Iterate through the CFGBlocks and print them one by one.
1909 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1910 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001911 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00001912 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001913
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001914 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001915 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001916
Ted Kremenek42a509f2007-08-31 21:30:12 +00001917 // Print the exit block.
1918 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00001919 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00001920}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001921
1922/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001923void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1924 print(llvm::errs(), cfg, LO);
1925}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001926
1927/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1928/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00001929void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1930 const LangOptions &LO) const {
1931 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001932 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00001933}
Ted Kremenek7dba8602007-08-29 21:56:09 +00001934
Ted Kremeneka2925852008-01-30 23:02:42 +00001935/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00001936void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00001937 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00001938 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00001939 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1940}
1941
Ted Kremenek390e48b2008-11-12 21:11:49 +00001942Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00001943
Ted Kremenek411cdee2008-04-16 21:10:48 +00001944 if (!Terminator)
1945 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001946
Ted Kremenek411cdee2008-04-16 21:10:48 +00001947 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001948
Ted Kremenek411cdee2008-04-16 21:10:48 +00001949 switch (Terminator->getStmtClass()) {
1950 default:
1951 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001952
Ted Kremenek411cdee2008-04-16 21:10:48 +00001953 case Stmt::ForStmtClass:
1954 E = cast<ForStmt>(Terminator)->getCond();
1955 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001956
Ted Kremenek411cdee2008-04-16 21:10:48 +00001957 case Stmt::WhileStmtClass:
1958 E = cast<WhileStmt>(Terminator)->getCond();
1959 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001960
Ted Kremenek411cdee2008-04-16 21:10:48 +00001961 case Stmt::DoStmtClass:
1962 E = cast<DoStmt>(Terminator)->getCond();
1963 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001964
Ted Kremenek411cdee2008-04-16 21:10:48 +00001965 case Stmt::IfStmtClass:
1966 E = cast<IfStmt>(Terminator)->getCond();
1967 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001968
Ted Kremenek411cdee2008-04-16 21:10:48 +00001969 case Stmt::ChooseExprClass:
1970 E = cast<ChooseExpr>(Terminator)->getCond();
1971 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001972
Ted Kremenek411cdee2008-04-16 21:10:48 +00001973 case Stmt::IndirectGotoStmtClass:
1974 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1975 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001976
Ted Kremenek411cdee2008-04-16 21:10:48 +00001977 case Stmt::SwitchStmtClass:
1978 E = cast<SwitchStmt>(Terminator)->getCond();
1979 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001980
Ted Kremenek411cdee2008-04-16 21:10:48 +00001981 case Stmt::ConditionalOperatorClass:
1982 E = cast<ConditionalOperator>(Terminator)->getCond();
1983 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001984
Ted Kremenek411cdee2008-04-16 21:10:48 +00001985 case Stmt::BinaryOperatorClass: // '&&' and '||'
1986 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001987 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001988
Ted Kremenek390e48b2008-11-12 21:11:49 +00001989 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00001990 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001991 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001992
Ted Kremenek411cdee2008-04-16 21:10:48 +00001993 return E ? E->IgnoreParens() : NULL;
1994}
1995
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001996bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00001997
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001998 if (!Terminator)
1999 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002000
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002001 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002002
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002003 switch (Terminator->getStmtClass()) {
2004 default:
2005 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002006
2007 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002008 case Stmt::WhileStmtClass:
2009 case Stmt::DoStmtClass:
2010 case Stmt::IfStmtClass:
2011 case Stmt::ChooseExprClass:
2012 case Stmt::ConditionalOperatorClass:
2013 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002014 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002015 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002016
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002017 return E ? E->IgnoreParens() : NULL;
2018}
2019
Ted Kremeneka2925852008-01-30 23:02:42 +00002020
Ted Kremenek7dba8602007-08-29 21:56:09 +00002021//===----------------------------------------------------------------------===//
2022// CFG Graphviz Visualization
2023//===----------------------------------------------------------------------===//
2024
Ted Kremenek42a509f2007-08-31 21:30:12 +00002025
2026#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002027static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002028#endif
2029
Chris Lattnere4f21422009-06-30 01:26:17 +00002030void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002031#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002032 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002033 GraphHelper = &H;
2034 llvm::ViewGraph(this,"CFG");
2035 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002036#endif
2037}
2038
Ted Kremenek7dba8602007-08-29 21:56:09 +00002039namespace llvm {
2040template<>
2041struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002042
2043 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2044
2045 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002046
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002047#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002048 std::string OutSStr;
2049 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002050 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002051 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002052
2053 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2054
2055 // Process string output to make it nicer...
2056 for (unsigned i = 0; i != OutStr.length(); ++i)
2057 if (OutStr[i] == '\n') { // Left justify
2058 OutStr[i] = '\\';
2059 OutStr.insert(OutStr.begin()+i+1, 'l');
2060 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002061
Ted Kremenek7dba8602007-08-29 21:56:09 +00002062 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002063#else
2064 return "";
2065#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002066 }
2067};
2068} // end namespace llvm