blob: 884188acd78a5a2b16d05cfa188b45bc0fd28434 [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}
Ted Kremenek852274d2009-12-16 03:18:58 +000037
38class AddStmtChoice {
39public:
40 enum Kind { NotAlwaysAdd = 0, AlwaysAdd, AlwaysAddAsLValue };
41public:
42 AddStmtChoice(Kind kind) : k(kind) {}
43 bool alwaysAdd() const { return k != NotAlwaysAdd; }
44 bool asLValue() const { return k == AlwaysAddAsLValue; }
45private:
46 Kind k;
47};
Mike Stump6d9828c2009-07-17 01:31:16 +000048
Ted Kremeneka34ea072008-08-04 22:51:42 +000049/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +000050/// The builder is stateful: an instance of the builder should be used to only
51/// construct a single CFG.
52///
53/// Example usage:
54///
55/// CFGBuilder builder;
56/// CFG* cfg = builder.BuildAST(stmt1);
57///
Mike Stump6d9828c2009-07-17 01:31:16 +000058/// CFG construction is done via a recursive walk of an AST. We actually parse
59/// the AST in reverse order so that the successor of a basic block is
60/// constructed prior to its predecessor. This allows us to nicely capture
61/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +000062///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000063class CFGBuilder {
Mike Stumpe5af3ce2009-07-20 23:24:15 +000064 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +000065 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +000066
Ted Kremenekfddd5182007-08-21 21:42:03 +000067 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000068 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000069 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000070 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000071 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000072 CFGBlock* DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +000073
Ted Kremenek19bb3562007-08-28 19:26:49 +000074 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000075 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
76 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +000077
78 // A list of blocks that end with a "goto" that must be backpatched to their
79 // resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +000080 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000081 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +000082
Ted Kremenek19bb3562007-08-28 19:26:49 +000083 // A list of labels whose address has been taken (for indirect gotos).
84 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
85 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +000086
87public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +000088 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
89 Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000090 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenekee82d9b2009-10-12 20:55:07 +000091 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {}
Mike Stump6d9828c2009-07-17 01:31:16 +000092
Ted Kremenekd4fdee32007-08-23 21:42:29 +000093 // buildCFG - Used by external clients to construct the CFG.
Mike Stumpe5af3ce2009-07-20 23:24:15 +000094 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump6d9828c2009-07-17 01:31:16 +000095
Ted Kremenek4f880632009-07-17 22:18:43 +000096private:
97 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +000098 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
99 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
100 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000101 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek852274d2009-12-16 03:18:58 +0000102 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000103 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000104 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000105 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000106 CFGBlock *VisitConditionalOperator(ConditionalOperator *C,
107 AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000108 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenekc768a0c2009-12-15 01:38:04 +0000109 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S) { return NYS(); }
Mike Stump0979d802009-07-22 22:56:04 +0000110 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenekc768a0c2009-12-15 01:38:04 +0000111 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S) { return NYS(); }
Ted Kremenek4f880632009-07-17 22:18:43 +0000112 CFGBlock *VisitDeclStmt(DeclStmt *DS);
113 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000114 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
115 CFGBlock *VisitDoStmt(DoStmt *D);
116 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000117 CFGBlock *VisitGotoStmt(GotoStmt* G);
118 CFGBlock *VisitIfStmt(IfStmt *I);
119 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
120 CFGBlock *VisitLabelStmt(LabelStmt *L);
121 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
122 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
123 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
124 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
125 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
126 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000127 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
128 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000129 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
130 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000131
Ted Kremenek852274d2009-12-16 03:18:58 +0000132 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
133 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000134 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000135
Ted Kremenek274f4332008-04-28 18:00:46 +0000136 // NYS == Not Yet Supported
137 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000138 badCFG = true;
139 return Block;
140 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000141
Ted Kremenek4f880632009-07-17 22:18:43 +0000142 void autoCreateBlock() { if (!Block) Block = createBlock(); }
143 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000144 bool FinishBlock(CFGBlock* B);
Ted Kremenek852274d2009-12-16 03:18:58 +0000145 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
146 return Visit(S, asc);
147 }
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000148
Ted Kremenek852274d2009-12-16 03:18:58 +0000149 void AppendStmt(CFGBlock *B, Stmt *S,
150 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
151 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000152 }
153
154 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
155 B->addSuccessor(S, cfg->getBumpVectorContext());
156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000158 /// TryResult - a class representing a variant over the values
159 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
160 /// and is used by the CFGBuilder to decide if a branch condition
161 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000162 class TryResult {
163 int X;
164 public:
165 TryResult(bool b) : X(b ? 1 : 0) {}
166 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenek941fde82009-07-24 04:47:11 +0000168 bool isTrue() const { return X == 1; }
169 bool isFalse() const { return X == 0; }
170 bool isKnown() const { return X >= 0; }
171 void negate() {
172 assert(isKnown());
173 X ^= 0x1;
174 }
175 };
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Mike Stump00998a02009-07-23 23:25:26 +0000177 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
178 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000179 TryResult TryEvaluateBool(Expr *S) {
Mike Stump00998a02009-07-23 23:25:26 +0000180 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000181 if (!S->isTypeDependent() && !S->isValueDependent() &&
182 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000183 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000184
185 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000186 }
187
Ted Kremenek4102af92008-03-13 03:04:22 +0000188 bool badCFG;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000189};
Mike Stump6d9828c2009-07-17 01:31:16 +0000190
Douglas Gregor898574e2008-12-05 23:32:09 +0000191// FIXME: Add support for dependent-sized array types in C++?
192// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000193static VariableArrayType* FindVA(Type* t) {
194 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
195 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
196 if (vat->getSizeExpr())
197 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000198
Ted Kremenek610a09e2008-09-26 22:58:57 +0000199 t = vt->getElementType().getTypePtr();
200 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000201
Ted Kremenek610a09e2008-09-26 22:58:57 +0000202 return 0;
203}
Mike Stump6d9828c2009-07-17 01:31:16 +0000204
205/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
206/// arbitrary statement. Examples include a single expression or a function
207/// body (compound statement). The ownership of the returned CFG is
208/// transferred to the caller. If CFG construction fails, this method returns
209/// NULL.
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000210CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
211 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000212 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000213 if (!Statement)
214 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000215
Ted Kremenek4102af92008-03-13 03:04:22 +0000216 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000217
218 // Create an empty block that will serve as the exit block for the CFG. Since
219 // this is the first block added to the CFG, it will be implicitly registered
220 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000221 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000222 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000223 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000224
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000225 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000226 CFGBlock* B = addStmt(Statement);
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000227 if (!B)
228 B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000229
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000230 if (B) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000231 // Finalize the last constructed block. This usually involves reversing the
232 // order of the statements in the block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000233 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000234
235 // Backpatch the gotos whose label -> block mappings we didn't know when we
236 // encountered them.
237 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000238 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000239
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000240 CFGBlock* B = *I;
241 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
242 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
243
244 // If there is no target for the goto, then we are looking at an
245 // incomplete AST. Handle this by not registering a successor.
246 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000247
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000248 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000249 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000250
Ted Kremenek19bb3562007-08-28 19:26:49 +0000251 // Add successors to the Indirect Goto Dispatch block (if we have one).
252 if (CFGBlock* B = cfg->getIndirectGotoBlock())
253 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
254 E = AddressTakenLabels.end(); I != E; ++I ) {
255
256 // Lookup the target block.
257 LabelMapTy::iterator LI = LabelMap.find(*I);
258
259 // If there is no target block that contains label, then we are looking
260 // at an incomplete AST. Handle this by not registering a successor.
261 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000262
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000263 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000264 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000265
Ted Kremenek94b33162007-09-17 16:18:02 +0000266 Succ = B;
Ted Kremenek322f58d2007-09-26 21:23:31 +0000267 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000268
269 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000270 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000271
Ted Kremenekda9b30e2009-10-20 23:59:28 +0000272 return badCFG ? NULL : cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000273}
Mike Stump6d9828c2009-07-17 01:31:16 +0000274
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000275/// createBlock - Used to lazily create blocks that are connected
276/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000277CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000278 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000279 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000280 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000281 return B;
282}
Mike Stump6d9828c2009-07-17 01:31:16 +0000283
Ted Kremenek6c249722009-09-24 18:45:41 +0000284/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000285bool CFGBuilder::FinishBlock(CFGBlock* B) {
286 if (badCFG)
287 return false;
288
Ted Kremenek4f880632009-07-17 22:18:43 +0000289 assert(B);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000290 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000291}
292
Ted Kremenek4f880632009-07-17 22:18:43 +0000293/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000294/// blocks for ternary operators, &&, and ||. We also process "," and
295/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000296CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000297tryAgain:
298 switch (S->getStmtClass()) {
299 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000300 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000301
302 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000303 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Ted Kremenek4f880632009-07-17 22:18:43 +0000305 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000306 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Ted Kremenek4f880632009-07-17 22:18:43 +0000308 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000309 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000310
Ted Kremenek4f880632009-07-17 22:18:43 +0000311 case Stmt::BreakStmtClass:
312 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenek4f880632009-07-17 22:18:43 +0000314 case Stmt::CallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000315 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremenek4f880632009-07-17 22:18:43 +0000317 case Stmt::CaseStmtClass:
318 return VisitCaseStmt(cast<CaseStmt>(S));
319
320 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000321 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenek4f880632009-07-17 22:18:43 +0000323 case Stmt::CompoundStmtClass:
324 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Ted Kremenek4f880632009-07-17 22:18:43 +0000326 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000327 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremenek4f880632009-07-17 22:18:43 +0000329 case Stmt::ContinueStmtClass:
330 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Ted Kremenek4f880632009-07-17 22:18:43 +0000332 case Stmt::DeclStmtClass:
333 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenek4f880632009-07-17 22:18:43 +0000335 case Stmt::DefaultStmtClass:
336 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Ted Kremenek4f880632009-07-17 22:18:43 +0000338 case Stmt::DoStmtClass:
339 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek4f880632009-07-17 22:18:43 +0000341 case Stmt::ForStmtClass:
342 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremenek4f880632009-07-17 22:18:43 +0000344 case Stmt::GotoStmtClass:
345 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Ted Kremenek4f880632009-07-17 22:18:43 +0000347 case Stmt::IfStmtClass:
348 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremenek4f880632009-07-17 22:18:43 +0000350 case Stmt::IndirectGotoStmtClass:
351 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Ted Kremenek4f880632009-07-17 22:18:43 +0000353 case Stmt::LabelStmtClass:
354 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Ted Kremenek4f880632009-07-17 22:18:43 +0000356 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000357 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
358
Mike Stump0979d802009-07-22 22:56:04 +0000359 case Stmt::CXXThrowExprClass:
360 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
361
Ted Kremenek4f880632009-07-17 22:18:43 +0000362 case Stmt::ObjCAtSynchronizedStmtClass:
363 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek4f880632009-07-17 22:18:43 +0000365 case Stmt::ObjCAtThrowStmtClass:
366 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek4f880632009-07-17 22:18:43 +0000368 case Stmt::ObjCAtTryStmtClass:
369 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Ted Kremenek4f880632009-07-17 22:18:43 +0000371 case Stmt::ObjCForCollectionStmtClass:
372 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremenek4f880632009-07-17 22:18:43 +0000374 case Stmt::ParenExprClass:
375 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000376 goto tryAgain;
377
Ted Kremenek4f880632009-07-17 22:18:43 +0000378 case Stmt::NullStmtClass:
379 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek4f880632009-07-17 22:18:43 +0000381 case Stmt::ReturnStmtClass:
382 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek4f880632009-07-17 22:18:43 +0000384 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000385 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremenek4f880632009-07-17 22:18:43 +0000387 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000388 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek4f880632009-07-17 22:18:43 +0000390 case Stmt::SwitchStmtClass:
391 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek4f880632009-07-17 22:18:43 +0000393 case Stmt::WhileStmtClass:
394 return VisitWhileStmt(cast<WhileStmt>(S));
395 }
396}
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremenek852274d2009-12-16 03:18:58 +0000398CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
399 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000400 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000401 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Ted Kremenek4f880632009-07-17 22:18:43 +0000404 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000405}
Mike Stump6d9828c2009-07-17 01:31:16 +0000406
Ted Kremenek4f880632009-07-17 22:18:43 +0000407/// VisitChildren - Visit the children of a Stmt.
408CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
409 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000410 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000411 E = Terminator->child_end(); I != E; ++I) {
412 if (*I) B = Visit(*I);
413 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000414 return B;
415}
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Ted Kremenek852274d2009-12-16 03:18:58 +0000417CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
418 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000419 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000420
Ted Kremenek852274d2009-12-16 03:18:58 +0000421 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000422 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000423 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000424 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000425
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000426 return Block;
427}
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Ted Kremenek852274d2009-12-16 03:18:58 +0000429CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
430 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000431 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000432 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000433 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Ted Kremenek4f880632009-07-17 22:18:43 +0000435 if (!FinishBlock(ConfluenceBlock))
436 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Ted Kremenek4f880632009-07-17 22:18:43 +0000438 // create the block evaluating the LHS
439 CFGBlock* LHSBlock = createBlock(false);
440 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Ted Kremenek4f880632009-07-17 22:18:43 +0000442 // create the block evaluating the RHS
443 Succ = ConfluenceBlock;
444 Block = NULL;
445 CFGBlock* RHSBlock = addStmt(B->getRHS());
446 if (!FinishBlock(RHSBlock))
447 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Mike Stump00998a02009-07-23 23:25:26 +0000449 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000450 TryResult KnownVal = TryEvaluateBool(B->getLHS());
451 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
452 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000453
Ted Kremenek4f880632009-07-17 22:18:43 +0000454 // Now link the LHSBlock with RHSBlock.
455 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000456 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
457 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000458 } else {
Ted Kremenek4f880632009-07-17 22:18:43 +0000459 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000460 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
461 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000462 }
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek4f880632009-07-17 22:18:43 +0000464 // Generate the blocks for evaluating the LHS.
465 Block = LHSBlock;
466 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000467 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000468 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000469 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000470 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000471 addStmt(B->getRHS());
472 return addStmt(B->getLHS());
473 }
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Ted Kremenek852274d2009-12-16 03:18:58 +0000475 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000476}
477
Ted Kremenek852274d2009-12-16 03:18:58 +0000478CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
479 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000480 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000481 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000482 }
483 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000484}
485
Ted Kremenek4f880632009-07-17 22:18:43 +0000486CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
487 // "break" is a control-flow statement. Thus we stop processing the current
488 // block.
489 if (Block && !FinishBlock(Block))
490 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Ted Kremenek4f880632009-07-17 22:18:43 +0000492 // Now create a new block that ends with the break statement.
493 Block = createBlock(false);
494 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremenek4f880632009-07-17 22:18:43 +0000496 // If there is no target for the break, then we are looking at an incomplete
497 // AST. This means that the CFG cannot be constructed.
498 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000499 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000500 else
501 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000502
503
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000504 return Block;
505}
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Ted Kremenek852274d2009-12-16 03:18:58 +0000507CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000508 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000509 bool NoReturn = false;
510 if (C->getCallee()->getType().getNoReturnAttr()) {
511 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000512 }
Mike Stump24556362009-07-25 21:26:53 +0000513
514 if (FunctionDecl *FD = C->getDirectCallee())
515 if (FD->hasAttr<NoReturnAttr>())
516 NoReturn = true;
517
518 if (!NoReturn)
Ted Kremenek852274d2009-12-16 03:18:58 +0000519 return VisitStmt(C, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Mike Stump24556362009-07-25 21:26:53 +0000521 if (Block && !FinishBlock(Block))
522 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Mike Stump24556362009-07-25 21:26:53 +0000524 // Create new block with no successor for the remaining pieces.
525 Block = createBlock(false);
Ted Kremenek852274d2009-12-16 03:18:58 +0000526 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000527
528 // Wire this to the exit block directly.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000529 AddSuccessor(Block, &cfg->getExit());
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Mike Stump24556362009-07-25 21:26:53 +0000531 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000532}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000533
Ted Kremenek852274d2009-12-16 03:18:58 +0000534CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
535 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000536 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000537 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000538 if (!FinishBlock(ConfluenceBlock))
539 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000541 Succ = ConfluenceBlock;
542 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000543 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000544 if (!FinishBlock(LHSBlock))
545 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000547 Succ = ConfluenceBlock;
548 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000549 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000550 if (!FinishBlock(RHSBlock))
551 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000553 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000554 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000555 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000556 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
557 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000558 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000559 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000560}
Mike Stump1eb44332009-09-09 15:08:12 +0000561
562
563CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
564 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000565
566 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
567 I != E; ++I ) {
568 LastBlock = addStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000570 if (badCFG)
571 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000572 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000573 return LastBlock;
574}
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Ted Kremenek852274d2009-12-16 03:18:58 +0000576CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
577 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000578 // Create the confluence block that will "merge" the results of the ternary
579 // expression.
580 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000581 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000582 if (!FinishBlock(ConfluenceBlock))
583 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000585 // Create a block for the LHS expression if there is an LHS expression. A
586 // GCC extension allows LHS to be NULL, causing the condition to be the
587 // value that is returned instead.
588 // e.g: x ?: y is shorthand for: x ? x : y;
589 Succ = ConfluenceBlock;
590 Block = NULL;
591 CFGBlock* LHSBlock = NULL;
592 if (C->getLHS()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000593 LHSBlock = addStmt(C->getLHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000594 if (!FinishBlock(LHSBlock))
595 return 0;
596 Block = NULL;
597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000599 // Create the block for the RHS expression.
600 Succ = ConfluenceBlock;
Ted Kremenek4f880632009-07-17 22:18:43 +0000601 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000602 if (!FinishBlock(RHSBlock))
603 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000605 // Create the block that will contain the condition.
606 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Mike Stump00998a02009-07-23 23:25:26 +0000608 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000609 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000610 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000611 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000612 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000613 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000614 // If we know the condition is false, add NULL as the successor for
615 // the block containing the condition. In this case, the confluence
616 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000617 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000618 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000619 } else {
620 // If we have no LHS expression, add the ConfluenceBlock as a direct
621 // successor for the block containing the condition. Moreover, we need to
622 // reverse the order of the predecessors in the ConfluenceBlock because
623 // the RHSBlock will have been added to the succcessors already, and we
624 // want the first predecessor to the the block containing the expression
625 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000626 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000627 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000628 std::reverse(ConfluenceBlock->pred_begin(),
629 ConfluenceBlock->pred_end());
630 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000633 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000634 Block->setTerminator(C);
635 return addStmt(C->getCond());
636}
637
Ted Kremenek4f880632009-07-17 22:18:43 +0000638CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
639 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000640
Ted Kremenek4f880632009-07-17 22:18:43 +0000641 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000642 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000643 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Ted Kremenek4f880632009-07-17 22:18:43 +0000646 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Ted Kremenek4f880632009-07-17 22:18:43 +0000648 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
649 typedef llvm::SmallVector<Decl*,10> BufTy;
650 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Ted Kremenek4f880632009-07-17 22:18:43 +0000652 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
653 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
654 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
655 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Ted Kremenek4f880632009-07-17 22:18:43 +0000657 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
658 // automatically freed with the CFG.
659 DeclGroupRef DG(*I);
660 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000661 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000662 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Ted Kremenek4f880632009-07-17 22:18:43 +0000664 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000665 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000666 B = VisitDeclSubExpr(D);
667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668
669 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000670}
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Ted Kremenek4f880632009-07-17 22:18:43 +0000672/// VisitDeclSubExpr - Utility method to add block-level expressions for
673/// initializers in Decls.
674CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
675 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000676
Ted Kremenek4f880632009-07-17 22:18:43 +0000677 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Ted Kremenek4f880632009-07-17 22:18:43 +0000679 if (!VD)
680 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Ted Kremenek4f880632009-07-17 22:18:43 +0000682 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Ted Kremenek4f880632009-07-17 22:18:43 +0000684 if (Init) {
685 // Optimization: Don't create separate block-level statements for literals.
686 switch (Init->getStmtClass()) {
687 case Stmt::IntegerLiteralClass:
688 case Stmt::CharacterLiteralClass:
689 case Stmt::StringLiteralClass:
690 break;
691 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000692 Block = addStmt(Init,
693 VD->getType()->isReferenceType()
694 ? AddStmtChoice::AlwaysAddAsLValue
695 : AddStmtChoice::AlwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000696 }
697 }
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Ted Kremenek4f880632009-07-17 22:18:43 +0000699 // If the type of VD is a VLA, then we must process its size expressions.
700 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
701 VA = FindVA(VA->getElementType().getTypePtr()))
702 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Ted Kremenek4f880632009-07-17 22:18:43 +0000704 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000705}
706
707CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000708 // We may see an if statement in the middle of a basic block, or it may be the
709 // first statement we are processing. In either case, we create a new basic
710 // block. First, we create the blocks for the then...else statements, and
711 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000712 // middle of a block, we stop processing that block. That block is then the
713 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000714
715 // The block we were proccessing is now finished. Make it the successor
716 // block.
717 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000718 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000719 if (!FinishBlock(Block))
720 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000721 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000722
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000723 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000724 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000725
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000726 if (Stmt* Else = I->getElse()) {
727 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000728
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000729 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000730 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000731 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000732 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000733
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000734 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
735 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000736 else if (Block) {
737 if (!FinishBlock(ElseBlock))
738 return 0;
739 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000740 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000741
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000742 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000743 CFGBlock* ThenBlock;
744 {
745 Stmt* Then = I->getThen();
746 assert (Then);
747 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000748 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000749 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000750
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000751 if (!ThenBlock) {
752 // We can reach here if the "then" body has all NullStmts.
753 // Create an empty block so we can distinguish between true and false
754 // branches in path-sensitive analyses.
755 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000756 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000757 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000758 if (!FinishBlock(ThenBlock))
759 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000760 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000761 }
762
Mike Stump6d9828c2009-07-17 01:31:16 +0000763 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000764 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000765
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000766 // Set the terminator of the new block to the If statement.
767 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000768
Mike Stump00998a02009-07-23 23:25:26 +0000769 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000770 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000771
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000772 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000773 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
774 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000775
776 // Add the condition as the last statement in the new block. This may create
777 // new blocks as the condition may contain control-flow. Any newly created
778 // blocks will be pointed to be "Block".
Ted Kremenek4f880632009-07-17 22:18:43 +0000779 return addStmt(I->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000780}
Mike Stump6d9828c2009-07-17 01:31:16 +0000781
782
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000783CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000784 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000785 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000786 // NOTE: If a "return" appears in the middle of a block, this means that the
787 // code afterwards is DEAD (unreachable). We still keep a basic block
788 // for that code; a simple "mark-and-sweep" from the entry block will be
789 // able to report such dead blocks.
Ted Kremenek6c249722009-09-24 18:45:41 +0000790 if (Block)
791 FinishBlock(Block);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000792
793 // Create the new block.
794 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000795
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000796 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000797 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000798
799 // Add the return statement to the block. This may create new blocks if R
800 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000801 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000802}
803
804CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
805 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000806 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000807 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000808
Ted Kremenek4f880632009-07-17 22:18:43 +0000809 if (!LabelBlock) // This can happen when the body is empty, i.e.
810 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000811
Ted Kremenek4f880632009-07-17 22:18:43 +0000812 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000813 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000814
815 // Labels partition blocks, so this is the end of the basic block we were
816 // processing (L is the block's label). Because this is label (and we have
817 // already processed the substatement) there is no extra control-flow to worry
818 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000819 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000820 if (!FinishBlock(LabelBlock))
821 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000822
823 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000824 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000825
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000826 // This block is now the implicit successor of other blocks.
827 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000828
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000829 return LabelBlock;
830}
831
832CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000833 // Goto is a control-flow statement. Thus we stop processing the current
834 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000835 if (Block)
836 FinishBlock(Block);
837
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000838 Block = createBlock(false);
839 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000840
841 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000842 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000843
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000844 if (I == LabelMap.end())
845 // We will need to backpatch this block later.
846 BackpatchBlocks.push_back(Block);
847 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000848 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000849
Mike Stump6d9828c2009-07-17 01:31:16 +0000850 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000851}
852
853CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000854 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000855
Mike Stumpfefb9f72009-07-21 01:12:51 +0000856 // "for" is a control-flow statement. Thus we stop processing the current
857 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000858 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000859 if (!FinishBlock(Block))
860 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000861 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000862 } else
863 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000864
865 // Because of short-circuit evaluation, the condition of the loop can span
866 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
867 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000868 CFGBlock* ExitConditionBlock = createBlock(false);
869 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000870
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000871 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000872 ExitConditionBlock->setTerminator(F);
873
874 // Now add the actual condition to the condition block. Because the condition
875 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000876 if (Stmt* C = F->getCond()) {
877 Block = ExitConditionBlock;
878 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000879 if (Block) {
880 if (!FinishBlock(EntryConditionBlock))
881 return 0;
882 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000883 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000884
Mike Stump6d9828c2009-07-17 01:31:16 +0000885 // The condition block is the implicit successor for the loop body as well as
886 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000887 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000888
Mike Stump00998a02009-07-23 23:25:26 +0000889 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000890 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Mike Stump00998a02009-07-23 23:25:26 +0000892 if (F->getCond())
893 KnownVal = TryEvaluateBool(F->getCond());
894
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000895 // Now create the loop body.
896 {
897 assert (F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000898
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000899 // Save the current values for Block, Succ, and continue and break targets
900 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
901 save_continue(ContinueTargetBlock),
Mike Stump6d9828c2009-07-17 01:31:16 +0000902 save_break(BreakTargetBlock);
903
Ted Kremenekaf603f72007-08-30 18:39:40 +0000904 // Create a new block to contain the (bottom) of the loop body.
905 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000906
Ted Kremeneke9334502008-09-04 21:48:47 +0000907 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000908 // Generate increment code in its own basic block. This is the target of
909 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +0000910 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000911 } else {
912 // No increment code. Create a special, empty, block that is used as the
913 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +0000914 assert(Succ == EntryConditionBlock);
915 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +0000916 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000917
Ted Kremenek3575f842009-04-28 00:51:56 +0000918 // Finish up the increment (or empty) block if it hasn't been already.
919 if (Block) {
920 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000921 if (!FinishBlock(Block))
922 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +0000923 Block = 0;
924 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000925
Ted Kremenek3575f842009-04-28 00:51:56 +0000926 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000927
Ted Kremenek3575f842009-04-28 00:51:56 +0000928 // The starting block for the loop increment is the block that should
929 // represent the 'loop target' for looping back to the start of the loop.
930 ContinueTargetBlock->setLoopTarget(F);
931
Ted Kremeneke9334502008-09-04 21:48:47 +0000932 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +0000933 BreakTargetBlock = LoopSuccessor;
934
935 // Now populate the body block, and in the process create new blocks as we
936 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +0000937 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000938
939 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +0000940 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +0000941 else if (Block && !FinishBlock(BodyBlock))
942 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000943
Ted Kremenek941fde82009-07-24 04:47:11 +0000944 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000945 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000946 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000947
Ted Kremenek941fde82009-07-24 04:47:11 +0000948 // Link up the condition block with the code that follows the loop. (the
949 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000950 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +0000951
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000952 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +0000953 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000954 if (Stmt* I = F->getInit()) {
955 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000956 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000957 } else {
958 // There is no loop initialization. We are thus basically a while loop.
959 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000960 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +0000961 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000962 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000963 }
964}
965
Ted Kremenek514de5a2008-11-11 17:10:00 +0000966CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
967 // Objective-C fast enumeration 'for' statements:
968 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
969 //
970 // for ( Type newVariable in collection_expression ) { statements }
971 //
972 // becomes:
973 //
974 // prologue:
975 // 1. collection_expression
976 // T. jump to loop_entry
977 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000978 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +0000979 // 1. ObjCForCollectionStmt [performs binding to newVariable]
980 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
981 // TB:
982 // statements
983 // T. jump to loop_entry
984 // FB:
985 // what comes after
986 //
987 // and
988 //
989 // Type existingItem;
990 // for ( existingItem in expression ) { statements }
991 //
992 // becomes:
993 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000994 // the same with newVariable replaced with existingItem; the binding works
995 // the same except that for one ObjCForCollectionStmt::getElement() returns
996 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +0000997 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000998
Ted Kremenek514de5a2008-11-11 17:10:00 +0000999 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001000
Ted Kremenek514de5a2008-11-11 17:10:00 +00001001 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001002 if (!FinishBlock(Block))
1003 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001004 LoopSuccessor = Block;
1005 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001006 } else
1007 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001008
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001009 // Build the condition blocks.
1010 CFGBlock* ExitConditionBlock = createBlock(false);
1011 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001012
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001013 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001014 ExitConditionBlock->setTerminator(S);
1015
1016 // The last statement in the block should be the ObjCForCollectionStmt, which
1017 // performs the actual binding to 'element' and determines if there are any
1018 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001019 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001020 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001021
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001022 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001023 // generate new blocks as necesary. We DON'T add the statement by default to
1024 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001025 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001026 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001027 if (!FinishBlock(EntryConditionBlock))
1028 return 0;
1029 Block = 0;
1030 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001031
1032 // The condition block is the implicit successor for the loop body as well as
1033 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001034 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001035
Ted Kremenek514de5a2008-11-11 17:10:00 +00001036 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001037 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001038 // Save the current values for Succ, continue and break targets.
1039 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001040 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1041
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001042 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001043 ContinueTargetBlock = EntryConditionBlock;
1044
Ted Kremenek4f880632009-07-17 22:18:43 +00001045 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001046
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001047 if (!BodyBlock)
1048 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001049 else if (Block) {
1050 if (!FinishBlock(BodyBlock))
1051 return 0;
1052 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001053
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001054 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001055 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001056 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001057
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001058 // Link up the condition block with the code that follows the loop.
1059 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001060 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001061
Ted Kremenek514de5a2008-11-11 17:10:00 +00001062 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001063 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001064 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001065}
1066
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001067CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1068 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001069
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001070 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001071 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001072
Ted Kremenekda5348e2009-05-05 23:11:51 +00001073 // The sync body starts its own basic block. This makes it a little easier
1074 // for diagnostic clients.
1075 if (SyncBlock) {
1076 if (!FinishBlock(SyncBlock))
1077 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001078
Ted Kremenekda5348e2009-05-05 23:11:51 +00001079 Block = 0;
1080 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001081
Ted Kremenekda5348e2009-05-05 23:11:51 +00001082 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001083
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001084 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001085 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001086}
Mike Stump6d9828c2009-07-17 01:31:16 +00001087
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001088CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001089 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001090 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001091}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001092
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001093CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001094 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001095
Mike Stumpfefb9f72009-07-21 01:12:51 +00001096 // "while" is a control-flow statement. Thus we stop processing the current
1097 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001098 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001099 if (!FinishBlock(Block))
1100 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001101 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001102 } else
1103 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001104
1105 // Because of short-circuit evaluation, the condition of the loop can span
1106 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1107 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001108 CFGBlock* ExitConditionBlock = createBlock(false);
1109 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001110
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001111 // Set the terminator for the "exit" condition block.
1112 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001113
1114 // Now add the actual condition to the condition block. Because the condition
1115 // itself may contain control-flow, new blocks may be created. Thus we update
1116 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001117 if (Stmt* C = W->getCond()) {
1118 Block = ExitConditionBlock;
1119 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001120 assert(Block == EntryConditionBlock);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001121 if (Block) {
1122 if (!FinishBlock(EntryConditionBlock))
1123 return 0;
1124 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001125 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001126
1127 // The condition block is the implicit successor for the loop body as well as
1128 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001129 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001130
Mike Stump00998a02009-07-23 23:25:26 +00001131 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001132 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001133
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001134 // Process the loop body.
1135 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001136 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001137
1138 // Save the current values for Block, Succ, and continue and break targets
1139 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1140 save_continue(ContinueTargetBlock),
1141 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001142
Mike Stump6d9828c2009-07-17 01:31:16 +00001143 // Create an empty block to represent the transition block for looping back
1144 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001145 Block = 0;
1146 assert(Succ == EntryConditionBlock);
1147 Succ = createBlock();
1148 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001149 ContinueTargetBlock = Succ;
1150
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001151 // All breaks should go to the code following the loop.
1152 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001153
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001154 // NULL out Block to force lazy instantiation of blocks for the body.
1155 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001156
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001157 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001158 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001159
Ted Kremenekaf603f72007-08-30 18:39:40 +00001160 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001161 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001162 else if (Block) {
1163 if (!FinishBlock(BodyBlock))
1164 return 0;
1165 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001166
Ted Kremenek941fde82009-07-24 04:47:11 +00001167 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001168 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001169 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001170
Ted Kremenek941fde82009-07-24 04:47:11 +00001171 // Link up the condition block with the code that follows the loop. (the
1172 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001173 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001174
1175 // There can be no more statements in the condition block since we loop back
1176 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001177 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001178
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001179 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001180 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001181 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001182}
Mike Stump1eb44332009-09-09 15:08:12 +00001183
1184
Ted Kremenek4f880632009-07-17 22:18:43 +00001185CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1186 // FIXME: For now we pretend that @catch and the code it contains does not
1187 // exit.
1188 return Block;
1189}
Mike Stump6d9828c2009-07-17 01:31:16 +00001190
Ted Kremenek2fda5042008-12-09 20:20:09 +00001191CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1192 // FIXME: This isn't complete. We basically treat @throw like a return
1193 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001194
Ted Kremenek6c249722009-09-24 18:45:41 +00001195 // If we were in the middle of a block we stop processing that block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001196 if (Block && !FinishBlock(Block))
1197 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001198
Ted Kremenek2fda5042008-12-09 20:20:09 +00001199 // Create the new block.
1200 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001201
Ted Kremenek2fda5042008-12-09 20:20:09 +00001202 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001203 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001204
1205 // Add the statement to the block. This may create new blocks if S contains
1206 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001207 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001208}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001209
Mike Stump0979d802009-07-22 22:56:04 +00001210CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001211 // If we were in the middle of a block we stop processing that block.
Mike Stump0979d802009-07-22 22:56:04 +00001212 if (Block && !FinishBlock(Block))
1213 return 0;
1214
1215 // Create the new block.
1216 Block = createBlock(false);
1217
1218 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001219 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001220
1221 // Add the statement to the block. This may create new blocks if S contains
1222 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001223 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001224}
1225
Ted Kremenek4f880632009-07-17 22:18:43 +00001226CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001227 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001228
Mike Stump8f9893a2009-07-21 01:27:50 +00001229 // "do...while" is a control-flow statement. Thus we stop processing the
1230 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001231 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001232 if (!FinishBlock(Block))
1233 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001234 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001235 } else
1236 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001237
1238 // Because of short-circuit evaluation, the condition of the loop can span
1239 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1240 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001241 CFGBlock* ExitConditionBlock = createBlock(false);
1242 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001243
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001244 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001245 ExitConditionBlock->setTerminator(D);
1246
1247 // Now add the actual condition to the condition block. Because the condition
1248 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001249 if (Stmt* C = D->getCond()) {
1250 Block = ExitConditionBlock;
1251 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001252 if (Block) {
1253 if (!FinishBlock(EntryConditionBlock))
1254 return 0;
1255 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001256 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001257
Ted Kremenek54827132008-02-27 07:20:00 +00001258 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001259 Succ = EntryConditionBlock;
1260
Mike Stump00998a02009-07-23 23:25:26 +00001261 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001262 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001263
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001264 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001265 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001266 {
1267 assert (D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001268
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001269 // Save the current values for Block, Succ, and continue and break targets
1270 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1271 save_continue(ContinueTargetBlock),
1272 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001273
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001274 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001275 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001276
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001277 // All breaks should go to the code following the loop.
1278 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001279
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001280 // NULL out Block to force lazy instantiation of blocks for the body.
1281 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001282
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001283 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001284 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001285
Ted Kremenekaf603f72007-08-30 18:39:40 +00001286 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001287 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001288 else if (Block) {
1289 if (!FinishBlock(BodyBlock))
1290 return 0;
1291 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001292
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001293 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001294 // ExitConditionBlock to represent the "loop back" transition. Create an
1295 // empty block to represent the transition block for looping back to the
1296 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001297 // FIXME: Can we do this more efficiently without adding another block?
1298 Block = NULL;
1299 Succ = BodyBlock;
1300 CFGBlock *LoopBackBlock = createBlock();
1301 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001302
Ted Kremenek941fde82009-07-24 04:47:11 +00001303 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001304 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001305 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001306
Ted Kremenek941fde82009-07-24 04:47:11 +00001307 // Link up the condition block with the code that follows the loop.
1308 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001309 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001310
1311 // There can be no more statements in the body block(s) since we loop back to
1312 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001313 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001314
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001315 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001316 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001317 return BodyBlock;
1318}
1319
1320CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1321 // "continue" is a control-flow statement. Thus we stop processing the
1322 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001323 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001324 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001325
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001326 // Now create a new block that ends with the continue statement.
1327 Block = createBlock(false);
1328 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001329
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001330 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001331 // incomplete AST. This means the CFG cannot be constructed.
1332 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001333 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001334 else
1335 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001336
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001337 return Block;
1338}
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001340CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001341 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001342
Ted Kremenek852274d2009-12-16 03:18:58 +00001343 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001344 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001345 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001346 }
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Ted Kremenek4f880632009-07-17 22:18:43 +00001348 // VLA types have expressions that must be evaluated.
1349 if (E->isArgumentType()) {
1350 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1351 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1352 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001353 }
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Mike Stump6d9828c2009-07-17 01:31:16 +00001355 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001356}
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Ted Kremenek4f880632009-07-17 22:18:43 +00001358/// VisitStmtExpr - Utility method to handle (nested) statement
1359/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001360CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1361 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001362 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001363 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001364 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001365 return VisitCompoundStmt(SE->getSubStmt());
1366}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001367
Ted Kremenek411cdee2008-04-16 21:10:48 +00001368CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001369 // "switch" is a control-flow statement. Thus we stop processing the current
1370 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001371 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001372
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001373 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001374 if (!FinishBlock(Block))
1375 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001376 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001377 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001378
1379 // Save the current "switch" context.
1380 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001381 save_break(BreakTargetBlock),
1382 save_default(DefaultCaseBlock);
1383
Mike Stump6d9828c2009-07-17 01:31:16 +00001384 // Set the "default" case to be the block after the switch statement. If the
1385 // switch statement contains a "default:", this value will be overwritten with
1386 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001387 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001388
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001389 // Create a new block that will contain the switch statement.
1390 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001391
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001392 // Now process the switch body. The code after the switch is the implicit
1393 // successor.
1394 Succ = SwitchSuccessor;
1395 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001396
1397 // When visiting the body, the case statements should automatically get linked
1398 // up to the switch. We also don't keep a pointer to the body, since all
1399 // control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001400 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001401 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001402 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001403 if (Block) {
1404 if (!FinishBlock(BodyBlock))
1405 return 0;
1406 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001407
Mike Stump6d9828c2009-07-17 01:31:16 +00001408 // If we have no "default:" case, the default transition is to the code
1409 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001410 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001411
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001412 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001413 SwitchTerminatedBlock->setTerminator(Terminator);
1414 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001415 Block = SwitchTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001416
Ted Kremenek411cdee2008-04-16 21:10:48 +00001417 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001418}
1419
Ted Kremenek4f880632009-07-17 22:18:43 +00001420CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001421 // CaseStmts are essentially labels, so they are the first statement in a
1422 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001423
Ted Kremenek4f880632009-07-17 22:18:43 +00001424 if (CS->getSubStmt())
1425 addStmt(CS->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001427 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001428 if (!CaseBlock)
1429 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001430
1431 // Cases statements partition blocks, so this is the top of the basic block we
1432 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001433 CaseBlock->setLabel(CS);
1434
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001435 if (!FinishBlock(CaseBlock))
1436 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001437
1438 // Add this block to the list of successors for the block with the switch
1439 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001440 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001441 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001442
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001443 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1444 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001445
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001446 // This block is now the implicit successor of other blocks.
1447 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001448
Ted Kremenek2677ea82008-03-15 07:45:02 +00001449 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001450}
Mike Stump6d9828c2009-07-17 01:31:16 +00001451
Ted Kremenek411cdee2008-04-16 21:10:48 +00001452CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001453 if (Terminator->getSubStmt())
1454 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001456 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001457
1458 if (!DefaultCaseBlock)
1459 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001460
1461 // Default statements partition blocks, so this is the top of the basic block
1462 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001463 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001465 if (!FinishBlock(DefaultCaseBlock))
1466 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001467
Mike Stump6d9828c2009-07-17 01:31:16 +00001468 // Unlike case statements, we don't add the default block to the successors
1469 // for the switch statement immediately. This is done when we finish
1470 // processing the switch statement. This allows for the default case
1471 // (including a fall-through to the code after the switch statement) to always
1472 // be the last successor of a switch-terminated block.
1473
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001474 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1475 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001476
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001477 // This block is now the implicit successor of other blocks.
1478 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001479
1480 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001481}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001482
Ted Kremenek19bb3562007-08-28 19:26:49 +00001483CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001484 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001485 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001486
Ted Kremenek19bb3562007-08-28 19:26:49 +00001487 if (!IBlock) {
1488 IBlock = createBlock(false);
1489 cfg->setIndirectGotoBlock(IBlock);
1490 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001491
Ted Kremenek19bb3562007-08-28 19:26:49 +00001492 // IndirectGoto is a control-flow statement. Thus we stop processing the
1493 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001494 if (Block && !FinishBlock(Block))
1495 return 0;
1496
Ted Kremenek19bb3562007-08-28 19:26:49 +00001497 Block = createBlock(false);
1498 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001499 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001500 return addStmt(I->getTarget());
1501}
1502
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001503} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001504
Mike Stump6d9828c2009-07-17 01:31:16 +00001505/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1506/// no successors or predecessors. If this is the first block created in the
1507/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001508CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001509 bool first_block = begin() == end();
1510
1511 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001512 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1513 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1514 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001515
1516 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001517 if (first_block)
1518 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001519
1520 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001521 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001522}
1523
Ted Kremenek026473c2007-08-23 16:51:22 +00001524/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1525/// CFG is returned to the caller.
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001526CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001527 CFGBuilder Builder;
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001528 return Builder.buildCFG(Statement, C);
Ted Kremenek026473c2007-08-23 16:51:22 +00001529}
1530
Ted Kremenek63f58872007-10-01 19:33:33 +00001531//===----------------------------------------------------------------------===//
1532// CFG: Queries for BlkExprs.
1533//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001534
Ted Kremenek63f58872007-10-01 19:33:33 +00001535namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001536 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001537}
1538
Ted Kremenek411cdee2008-04-16 21:10:48 +00001539static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1540 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001541 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001542
Ted Kremenek411cdee2008-04-16 21:10:48 +00001543 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001544 if (!*I) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001545
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001546 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1547 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001548
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001549 FindSubExprAssignments(*I, Set);
1550 }
1551}
1552
Ted Kremenek63f58872007-10-01 19:33:33 +00001553static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1554 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001555
1556 // Look for assignments that are used as subexpressions. These are the only
1557 // assignments that we want to *possibly* register as a block-level
1558 // expression. Basically, if an assignment occurs both in a subexpression and
1559 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001560 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001561
Ted Kremenek63f58872007-10-01 19:33:33 +00001562 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001563 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001564 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001565
Ted Kremenek411cdee2008-04-16 21:10:48 +00001566 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001567
1568 // Iterate over the statements again on identify the Expr* and Stmt* at the
1569 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001570
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001571 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001572 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001573
Ted Kremenek411cdee2008-04-16 21:10:48 +00001574 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001575 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001576 // expression are really "statements" whose value is never used by
1577 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001578 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001579 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001580 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1581 // Special handling for statement expressions. The last statement in
1582 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001583 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001584 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001585 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001586 (*M)[C->body_back()] = x;
1587 }
1588 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001589
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001590 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001591 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001592 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001593
Ted Kremenek411cdee2008-04-16 21:10:48 +00001594 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001595
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001596 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001597
Ted Kremenek390e48b2008-11-12 21:11:49 +00001598 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001599 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001600 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001601 }
1602 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001603
Ted Kremenek63f58872007-10-01 19:33:33 +00001604 return M;
1605}
1606
Ted Kremenek86946742008-01-17 20:48:37 +00001607CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1608 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001609 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001610
Ted Kremenek63f58872007-10-01 19:33:33 +00001611 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001612 BlkExprMapTy::iterator I = M->find(S);
Mike Stump6d9828c2009-07-17 01:31:16 +00001613
Ted Kremenek63f58872007-10-01 19:33:33 +00001614 if (I == M->end()) return CFG::BlkExprNumTy();
1615 else return CFG::BlkExprNumTy(I->second);
1616}
1617
1618unsigned CFG::getNumBlkExprs() {
1619 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1620 return M->size();
1621 else {
1622 // We assume callers interested in the number of BlkExprs will want
1623 // the map constructed if it doesn't already exist.
1624 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1625 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1626 }
1627}
1628
Ted Kremenek274f4332008-04-28 18:00:46 +00001629//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001630// Cleanup: CFG dstor.
1631//===----------------------------------------------------------------------===//
1632
Ted Kremenek63f58872007-10-01 19:33:33 +00001633CFG::~CFG() {
1634 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1635}
Mike Stump6d9828c2009-07-17 01:31:16 +00001636
Ted Kremenek7dba8602007-08-29 21:56:09 +00001637//===----------------------------------------------------------------------===//
1638// CFG pretty printing
1639//===----------------------------------------------------------------------===//
1640
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001641namespace {
1642
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001643class StmtPrinterHelper : public PrinterHelper {
Mike Stump6d9828c2009-07-17 01:31:16 +00001644
Ted Kremenek42a509f2007-08-31 21:30:12 +00001645 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1646 StmtMapTy StmtMap;
1647 signed CurrentBlock;
1648 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001649 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001650public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001651
Chris Lattnere4f21422009-06-30 01:26:17 +00001652 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1653 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001654 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1655 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001656 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001657 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001658 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001659 }
1660 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001661
Ted Kremenek42a509f2007-08-31 21:30:12 +00001662 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001663
Chris Lattnere4f21422009-06-30 01:26:17 +00001664 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001665 void setBlockID(signed i) { CurrentBlock = i; }
1666 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001667
Ted Kremeneka95d3752008-09-13 05:16:45 +00001668 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001669
Ted Kremenek411cdee2008-04-16 21:10:48 +00001670 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001671
1672 if (I == StmtMap.end())
1673 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001674
1675 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek42a509f2007-08-31 21:30:12 +00001676 && I->second.second == CurrentStmt)
1677 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001678
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001679 OS << "[B" << I->second.first << "." << I->second.second << "]";
1680 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001681 }
1682};
Chris Lattnere4f21422009-06-30 01:26:17 +00001683} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001684
Chris Lattnere4f21422009-06-30 01:26:17 +00001685
1686namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001687class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001688 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001689
Ted Kremeneka95d3752008-09-13 05:16:45 +00001690 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001691 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001692 PrintingPolicy Policy;
1693
Ted Kremenek42a509f2007-08-31 21:30:12 +00001694public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001695 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001696 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001697 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001698
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001699 void VisitIfStmt(IfStmt* I) {
1700 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001701 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001702 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001703
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001704 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001705 void VisitStmt(Stmt* Terminator) {
1706 Terminator->printPretty(OS, Helper, Policy);
1707 }
1708
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001709 void VisitForStmt(ForStmt* F) {
1710 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001711 if (F->getInit()) OS << "...";
1712 OS << "; ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001713 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001714 OS << "; ";
1715 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001716 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001717 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001718
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001719 void VisitWhileStmt(WhileStmt* W) {
1720 OS << "while " ;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001721 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001722 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001723
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001724 void VisitDoStmt(DoStmt* D) {
1725 OS << "do ... while ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001726 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001727 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001728
Ted Kremenek411cdee2008-04-16 21:10:48 +00001729 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001730 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001731 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001732 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001733
Ted Kremenek805e9a82007-08-31 21:49:40 +00001734 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001735 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001736 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001737 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001738
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001739 void VisitChooseExpr(ChooseExpr* C) {
1740 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001741 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001742 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001743 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001744
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001745 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1746 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001747 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001748 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001749
Ted Kremenek805e9a82007-08-31 21:49:40 +00001750 void VisitBinaryOperator(BinaryOperator* B) {
1751 if (!B->isLogicalOp()) {
1752 VisitExpr(B);
1753 return;
1754 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001755
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001756 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001757
Ted Kremenek805e9a82007-08-31 21:49:40 +00001758 switch (B->getOpcode()) {
1759 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001760 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001761 return;
1762 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001763 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001764 return;
1765 default:
1766 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001767 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00001768 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001769
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001770 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001771 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001772 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001773};
Chris Lattnere4f21422009-06-30 01:26:17 +00001774} // end anonymous namespace
1775
Mike Stump6d9828c2009-07-17 01:31:16 +00001776
Chris Lattnere4f21422009-06-30 01:26:17 +00001777static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1778 Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001779 if (Helper) {
1780 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001781 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001782 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00001783
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001784 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001785 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001786 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001787 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001788 return;
1789 }
1790 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001791
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001792 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001793 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001794 if (B->getOpcode() == BinaryOperator::Comma) {
1795 OS << "... , ";
1796 Helper->handledStmt(B->getRHS(),OS);
1797 OS << '\n';
1798 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001799 }
1800 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001801 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001802
Chris Lattnere4f21422009-06-30 01:26:17 +00001803 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00001804
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001805 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001806 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001807}
Mike Stump6d9828c2009-07-17 01:31:16 +00001808
Chris Lattnere4f21422009-06-30 01:26:17 +00001809static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1810 const CFGBlock& B,
1811 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001812
Ted Kremenek42a509f2007-08-31 21:30:12 +00001813 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00001814
Ted Kremenek7dba8602007-08-29 21:56:09 +00001815 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00001816 OS << "\n [ B" << B.getBlockID();
1817
Ted Kremenek42a509f2007-08-31 21:30:12 +00001818 if (&B == &cfg->getEntry())
1819 OS << " (ENTRY) ]\n";
1820 else if (&B == &cfg->getExit())
1821 OS << " (EXIT) ]\n";
1822 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001823 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001824 else
1825 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00001826
Ted Kremenek9cffe732007-08-29 23:20:49 +00001827 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001828 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001829
1830 if (print_edges)
1831 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001832
Ted Kremenek411cdee2008-04-16 21:10:48 +00001833 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001834 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001835 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001836 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001837 C->getLHS()->printPretty(OS, Helper,
1838 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001839 if (C->getRHS()) {
1840 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001841 C->getRHS()->printPretty(OS, Helper,
1842 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001843 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001844 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001845 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001846 else
1847 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001848
Ted Kremenek9cffe732007-08-29 23:20:49 +00001849 OS << ":\n";
1850 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001851
Ted Kremenekfddd5182007-08-21 21:42:03 +00001852 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001853 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00001854
Ted Kremenek42a509f2007-08-31 21:30:12 +00001855 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1856 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001857
Ted Kremenek9cffe732007-08-29 23:20:49 +00001858 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001859 if (print_edges)
1860 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001861
Ted Kremeneka95d3752008-09-13 05:16:45 +00001862 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001863
Ted Kremenek42a509f2007-08-31 21:30:12 +00001864 if (Helper)
1865 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00001866
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001867 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001868 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001869
Ted Kremenek9cffe732007-08-29 23:20:49 +00001870 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001871 if (B.getTerminator()) {
1872 if (print_edges)
1873 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001874
Ted Kremenek9cffe732007-08-29 23:20:49 +00001875 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001876
Ted Kremenek42a509f2007-08-31 21:30:12 +00001877 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00001878
Chris Lattnere4f21422009-06-30 01:26:17 +00001879 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1880 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00001881 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001882 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001883 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001884
Ted Kremenek9cffe732007-08-29 23:20:49 +00001885 if (print_edges) {
1886 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001887 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001888 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001889
Ted Kremenek42a509f2007-08-31 21:30:12 +00001890 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1891 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001892
Ted Kremenek42a509f2007-08-31 21:30:12 +00001893 if (i == 8 || (i-8) == 0)
1894 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001895
Ted Kremenek9cffe732007-08-29 23:20:49 +00001896 OS << " B" << (*I)->getBlockID();
1897 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001898
Ted Kremenek42a509f2007-08-31 21:30:12 +00001899 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00001900
Ted Kremenek42a509f2007-08-31 21:30:12 +00001901 // Print the successors of this block.
1902 OS << " Successors (" << B.succ_size() << "):";
1903 i = 0;
1904
1905 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1906 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001907
Ted Kremenek42a509f2007-08-31 21:30:12 +00001908 if (i == 8 || (i-8) % 10 == 0)
1909 OS << "\n ";
1910
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001911 if (*I)
1912 OS << " B" << (*I)->getBlockID();
1913 else
1914 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001915 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001916
Ted Kremenek9cffe732007-08-29 23:20:49 +00001917 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001918 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001919}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001920
Ted Kremenek42a509f2007-08-31 21:30:12 +00001921
1922/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001923void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001924
1925/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00001926void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1927 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00001928
Ted Kremenek42a509f2007-08-31 21:30:12 +00001929 // Print the entry block.
1930 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00001931
Ted Kremenek42a509f2007-08-31 21:30:12 +00001932 // Iterate through the CFGBlocks and print them one by one.
1933 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1934 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001935 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00001936 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001937
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001938 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001939 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001940
Ted Kremenek42a509f2007-08-31 21:30:12 +00001941 // Print the exit block.
1942 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00001943 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00001944}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001945
1946/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001947void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1948 print(llvm::errs(), cfg, LO);
1949}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001950
1951/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1952/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00001953void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1954 const LangOptions &LO) const {
1955 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001956 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00001957}
Ted Kremenek7dba8602007-08-29 21:56:09 +00001958
Ted Kremeneka2925852008-01-30 23:02:42 +00001959/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00001960void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00001961 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00001962 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00001963 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1964}
1965
Ted Kremenek390e48b2008-11-12 21:11:49 +00001966Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00001967
Ted Kremenek411cdee2008-04-16 21:10:48 +00001968 if (!Terminator)
1969 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001970
Ted Kremenek411cdee2008-04-16 21:10:48 +00001971 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001972
Ted Kremenek411cdee2008-04-16 21:10:48 +00001973 switch (Terminator->getStmtClass()) {
1974 default:
1975 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001976
Ted Kremenek411cdee2008-04-16 21:10:48 +00001977 case Stmt::ForStmtClass:
1978 E = cast<ForStmt>(Terminator)->getCond();
1979 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001980
Ted Kremenek411cdee2008-04-16 21:10:48 +00001981 case Stmt::WhileStmtClass:
1982 E = cast<WhileStmt>(Terminator)->getCond();
1983 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001984
Ted Kremenek411cdee2008-04-16 21:10:48 +00001985 case Stmt::DoStmtClass:
1986 E = cast<DoStmt>(Terminator)->getCond();
1987 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001988
Ted Kremenek411cdee2008-04-16 21:10:48 +00001989 case Stmt::IfStmtClass:
1990 E = cast<IfStmt>(Terminator)->getCond();
1991 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001992
Ted Kremenek411cdee2008-04-16 21:10:48 +00001993 case Stmt::ChooseExprClass:
1994 E = cast<ChooseExpr>(Terminator)->getCond();
1995 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001996
Ted Kremenek411cdee2008-04-16 21:10:48 +00001997 case Stmt::IndirectGotoStmtClass:
1998 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1999 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002000
Ted Kremenek411cdee2008-04-16 21:10:48 +00002001 case Stmt::SwitchStmtClass:
2002 E = cast<SwitchStmt>(Terminator)->getCond();
2003 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002004
Ted Kremenek411cdee2008-04-16 21:10:48 +00002005 case Stmt::ConditionalOperatorClass:
2006 E = cast<ConditionalOperator>(Terminator)->getCond();
2007 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002008
Ted Kremenek411cdee2008-04-16 21:10:48 +00002009 case Stmt::BinaryOperatorClass: // '&&' and '||'
2010 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002011 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002012
Ted Kremenek390e48b2008-11-12 21:11:49 +00002013 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002014 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002015 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002016
Ted Kremenek411cdee2008-04-16 21:10:48 +00002017 return E ? E->IgnoreParens() : NULL;
2018}
2019
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002020bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002021
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002022 if (!Terminator)
2023 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002024
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002025 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002026
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002027 switch (Terminator->getStmtClass()) {
2028 default:
2029 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002030
2031 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002032 case Stmt::WhileStmtClass:
2033 case Stmt::DoStmtClass:
2034 case Stmt::IfStmtClass:
2035 case Stmt::ChooseExprClass:
2036 case Stmt::ConditionalOperatorClass:
2037 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002038 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002039 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002040
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002041 return E ? E->IgnoreParens() : NULL;
2042}
2043
Ted Kremeneka2925852008-01-30 23:02:42 +00002044
Ted Kremenek7dba8602007-08-29 21:56:09 +00002045//===----------------------------------------------------------------------===//
2046// CFG Graphviz Visualization
2047//===----------------------------------------------------------------------===//
2048
Ted Kremenek42a509f2007-08-31 21:30:12 +00002049
2050#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002051static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002052#endif
2053
Chris Lattnere4f21422009-06-30 01:26:17 +00002054void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002055#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002056 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002057 GraphHelper = &H;
2058 llvm::ViewGraph(this,"CFG");
2059 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002060#endif
2061}
2062
Ted Kremenek7dba8602007-08-29 21:56:09 +00002063namespace llvm {
2064template<>
2065struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002066
2067 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2068
2069 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002070
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002071#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002072 std::string OutSStr;
2073 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002074 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002075 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002076
2077 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2078
2079 // Process string output to make it nicer...
2080 for (unsigned i = 0; i != OutStr.length(); ++i)
2081 if (OutStr[i] == '\n') { // Left justify
2082 OutStr[i] = '\\';
2083 OutStr.insert(OutStr.begin()+i+1, 'l');
2084 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002085
Ted Kremenek7dba8602007-08-29 21:56:09 +00002086 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002087#else
2088 return "";
2089#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002090 }
2091};
2092} // end namespace llvm