blob: 31417597f798950efda95a26a166952bdbf3bcde [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"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek0ba497b2009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremenek83c01da2008-01-11 00:40:29 +000026
Ted Kremenekfddd5182007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor4afa39d2009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump6d9828c2009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000037}
Mike Stump6d9828c2009-07-17 01:31:16 +000038
Ted Kremeneka34ea072008-08-04 22:51:42 +000039/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +000040/// The builder is stateful: an instance of the builder should be used to only
41/// construct a single CFG.
42///
43/// Example usage:
44///
45/// CFGBuilder builder;
46/// CFG* cfg = builder.BuildAST(stmt1);
47///
Mike Stump6d9828c2009-07-17 01:31:16 +000048/// CFG construction is done via a recursive walk of an AST. We actually parse
49/// the AST in reverse order so that the successor of a basic block is
50/// constructed prior to its predecessor. This allows us to nicely capture
51/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +000052///
Ted Kremenek4f880632009-07-17 22:18:43 +000053class VISIBILITY_HIDDEN CFGBuilder {
Mike Stumpe5af3ce2009-07-20 23:24:15 +000054 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +000055 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +000056
Ted Kremenekfddd5182007-08-21 21:42:03 +000057 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000058 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000059 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000060 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000061 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000062 CFGBlock* DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +000063
Ted Kremenek19bb3562007-08-28 19:26:49 +000064 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000065 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
66 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +000067
68 // A list of blocks that end with a "goto" that must be backpatched to their
69 // resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +000070 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000071 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +000072
Ted Kremenek19bb3562007-08-28 19:26:49 +000073 // A list of labels whose address has been taken (for indirect gotos).
74 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
75 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +000076
77public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +000078 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
79 Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000080 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenekee82d9b2009-10-12 20:55:07 +000081 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {}
Mike Stump6d9828c2009-07-17 01:31:16 +000082
Ted Kremenekd4fdee32007-08-23 21:42:29 +000083 // buildCFG - Used by external clients to construct the CFG.
Mike Stumpe5af3ce2009-07-20 23:24:15 +000084 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump6d9828c2009-07-17 01:31:16 +000085
Ted Kremenek4f880632009-07-17 22:18:43 +000086private:
87 // Visitors to walk an AST and construct the CFG.
88 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
89 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
Ted Kremenek13fc08a2009-07-18 00:47:21 +000090 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
91 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +000092 CFGBlock *VisitBreakStmt(BreakStmt *B);
93 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
94 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +000095 CFGBlock *VisitChooseExpr(ChooseExpr *C);
96 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
97 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
98 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump0979d802009-07-22 22:56:04 +000099 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenek4f880632009-07-17 22:18:43 +0000100 CFGBlock *VisitDeclStmt(DeclStmt *DS);
101 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000102 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
103 CFGBlock *VisitDoStmt(DoStmt *D);
104 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000105 CFGBlock *VisitGotoStmt(GotoStmt* G);
106 CFGBlock *VisitIfStmt(IfStmt *I);
107 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
108 CFGBlock *VisitLabelStmt(LabelStmt *L);
109 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
110 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
111 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
112 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
113 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
114 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000115 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
116 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000117 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
118 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000119
Ted Kremenek4f880632009-07-17 22:18:43 +0000120 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
121 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
122 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000123
Ted Kremenek274f4332008-04-28 18:00:46 +0000124 // NYS == Not Yet Supported
125 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000126 badCFG = true;
127 return Block;
128 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000129
Ted Kremenek4f880632009-07-17 22:18:43 +0000130 void autoCreateBlock() { if (!Block) Block = createBlock(); }
131 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000132 bool FinishBlock(CFGBlock* B);
Ted Kremenek4f880632009-07-17 22:18:43 +0000133 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000134
135 void AppendStmt(CFGBlock *B, Stmt *S) {
136 B->appendStmt(S, cfg->getBumpVectorContext());
137 }
138
139 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
140 B->addSuccessor(S, cfg->getBumpVectorContext());
141 }
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000143 /// TryResult - a class representing a variant over the values
144 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
145 /// and is used by the CFGBuilder to decide if a branch condition
146 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000147 class TryResult {
148 int X;
149 public:
150 TryResult(bool b) : X(b ? 1 : 0) {}
151 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Ted Kremenek941fde82009-07-24 04:47:11 +0000153 bool isTrue() const { return X == 1; }
154 bool isFalse() const { return X == 0; }
155 bool isKnown() const { return X >= 0; }
156 void negate() {
157 assert(isKnown());
158 X ^= 0x1;
159 }
160 };
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Mike Stump00998a02009-07-23 23:25:26 +0000162 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
163 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000164 TryResult TryEvaluateBool(Expr *S) {
Mike Stump00998a02009-07-23 23:25:26 +0000165 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000166 if (!S->isTypeDependent() && !S->isValueDependent() &&
167 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000168 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000169
170 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000171 }
172
Ted Kremenek4102af92008-03-13 03:04:22 +0000173 bool badCFG;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000174};
Mike Stump6d9828c2009-07-17 01:31:16 +0000175
Douglas Gregor898574e2008-12-05 23:32:09 +0000176// FIXME: Add support for dependent-sized array types in C++?
177// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000178static VariableArrayType* FindVA(Type* t) {
179 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
180 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
181 if (vat->getSizeExpr())
182 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000183
Ted Kremenek610a09e2008-09-26 22:58:57 +0000184 t = vt->getElementType().getTypePtr();
185 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000186
Ted Kremenek610a09e2008-09-26 22:58:57 +0000187 return 0;
188}
Mike Stump6d9828c2009-07-17 01:31:16 +0000189
190/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
191/// arbitrary statement. Examples include a single expression or a function
192/// body (compound statement). The ownership of the returned CFG is
193/// transferred to the caller. If CFG construction fails, this method returns
194/// NULL.
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000195CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
196 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000197 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000198 if (!Statement)
199 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000200
Ted Kremenek4102af92008-03-13 03:04:22 +0000201 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000202
203 // Create an empty block that will serve as the exit block for the CFG. Since
204 // this is the first block added to the CFG, it will be implicitly registered
205 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000206 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000207 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000208 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000209
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000210 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000211 CFGBlock* B = addStmt(Statement);
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000212 if (!B)
213 B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000214
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000215 if (B) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000216 // Finalize the last constructed block. This usually involves reversing the
217 // order of the statements in the block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000218 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000219
220 // Backpatch the gotos whose label -> block mappings we didn't know when we
221 // encountered them.
222 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000223 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000224
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000225 CFGBlock* B = *I;
226 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
227 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
228
229 // If there is no target for the goto, then we are looking at an
230 // incomplete AST. Handle this by not registering a successor.
231 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000232
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000233 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000234 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000235
Ted Kremenek19bb3562007-08-28 19:26:49 +0000236 // Add successors to the Indirect Goto Dispatch block (if we have one).
237 if (CFGBlock* B = cfg->getIndirectGotoBlock())
238 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
239 E = AddressTakenLabels.end(); I != E; ++I ) {
240
241 // Lookup the target block.
242 LabelMapTy::iterator LI = LabelMap.find(*I);
243
244 // If there is no target block that contains label, then we are looking
245 // at an incomplete AST. Handle this by not registering a successor.
246 if (LI == LabelMap.end()) continue;
Mike 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 Kremenek94b33162007-09-17 16:18:02 +0000251 Succ = B;
Ted Kremenek322f58d2007-09-26 21:23:31 +0000252 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000253
254 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000255 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000256
Ted Kremenekda9b30e2009-10-20 23:59:28 +0000257 return badCFG ? NULL : cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000258}
Mike Stump6d9828c2009-07-17 01:31:16 +0000259
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000260/// createBlock - Used to lazily create blocks that are connected
261/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000262CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000263 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000264 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000265 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000266 return B;
267}
Mike Stump6d9828c2009-07-17 01:31:16 +0000268
Ted Kremenek6c249722009-09-24 18:45:41 +0000269/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000270bool CFGBuilder::FinishBlock(CFGBlock* B) {
271 if (badCFG)
272 return false;
273
Ted Kremenek4f880632009-07-17 22:18:43 +0000274 assert(B);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000275 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000276}
277
Ted Kremenek4f880632009-07-17 22:18:43 +0000278/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000279/// blocks for ternary operators, &&, and ||. We also process "," and
280/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4f880632009-07-17 22:18:43 +0000281CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
282tryAgain:
283 switch (S->getStmtClass()) {
284 default:
285 return VisitStmt(S, alwaysAdd);
286
287 case Stmt::AddrLabelExprClass:
288 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Ted Kremenek4f880632009-07-17 22:18:43 +0000290 case Stmt::BinaryOperatorClass:
291 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Ted Kremenek4f880632009-07-17 22:18:43 +0000293 case Stmt::BlockExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000294 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000295
296 case Stmt::BlockDeclRefExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000297 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Ted Kremenek4f880632009-07-17 22:18:43 +0000299 case Stmt::BreakStmtClass:
300 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Ted Kremenek4f880632009-07-17 22:18:43 +0000302 case Stmt::CallExprClass:
303 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Ted Kremenek4f880632009-07-17 22:18:43 +0000305 case Stmt::CaseStmtClass:
306 return VisitCaseStmt(cast<CaseStmt>(S));
307
308 case Stmt::ChooseExprClass:
309 return VisitChooseExpr(cast<ChooseExpr>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Ted Kremenek4f880632009-07-17 22:18:43 +0000311 case Stmt::CompoundStmtClass:
312 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenek4f880632009-07-17 22:18:43 +0000314 case Stmt::ConditionalOperatorClass:
315 return VisitConditionalOperator(cast<ConditionalOperator>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremenek4f880632009-07-17 22:18:43 +0000317 case Stmt::ContinueStmtClass:
318 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Ted Kremenek4f880632009-07-17 22:18:43 +0000320 case Stmt::DeclStmtClass:
321 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenek4f880632009-07-17 22:18:43 +0000323 case Stmt::DefaultStmtClass:
324 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Ted Kremenek4f880632009-07-17 22:18:43 +0000326 case Stmt::DoStmtClass:
327 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremenek4f880632009-07-17 22:18:43 +0000329 case Stmt::ForStmtClass:
330 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Ted Kremenek4f880632009-07-17 22:18:43 +0000332 case Stmt::GotoStmtClass:
333 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenek4f880632009-07-17 22:18:43 +0000335 case Stmt::IfStmtClass:
336 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Ted Kremenek4f880632009-07-17 22:18:43 +0000338 case Stmt::IndirectGotoStmtClass:
339 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek4f880632009-07-17 22:18:43 +0000341 case Stmt::LabelStmtClass:
342 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremenek4f880632009-07-17 22:18:43 +0000344 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000345 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
346
Mike Stump0979d802009-07-22 22:56:04 +0000347 case Stmt::CXXThrowExprClass:
348 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
349
Ted Kremenek4f880632009-07-17 22:18:43 +0000350 case Stmt::ObjCAtSynchronizedStmtClass:
351 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Ted Kremenek4f880632009-07-17 22:18:43 +0000353 case Stmt::ObjCAtThrowStmtClass:
354 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Ted Kremenek4f880632009-07-17 22:18:43 +0000356 case Stmt::ObjCAtTryStmtClass:
357 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremenek4f880632009-07-17 22:18:43 +0000359 case Stmt::ObjCForCollectionStmtClass:
360 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek4f880632009-07-17 22:18:43 +0000362 case Stmt::ParenExprClass:
363 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000364 goto tryAgain;
365
Ted Kremenek4f880632009-07-17 22:18:43 +0000366 case Stmt::NullStmtClass:
367 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek4f880632009-07-17 22:18:43 +0000369 case Stmt::ReturnStmtClass:
370 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek4f880632009-07-17 22:18:43 +0000372 case Stmt::SizeOfAlignOfExprClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000373 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
374
Ted Kremenek4f880632009-07-17 22:18:43 +0000375 case Stmt::StmtExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000376 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenek4f880632009-07-17 22:18:43 +0000378 case Stmt::SwitchStmtClass:
379 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek4f880632009-07-17 22:18:43 +0000381 case Stmt::WhileStmtClass:
382 return VisitWhileStmt(cast<WhileStmt>(S));
383 }
384}
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremenek4f880632009-07-17 22:18:43 +0000386CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
387 if (alwaysAdd) {
388 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000389 AppendStmt(Block, S);
Mike Stump6d9828c2009-07-17 01:31:16 +0000390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek4f880632009-07-17 22:18:43 +0000392 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000393}
Mike Stump6d9828c2009-07-17 01:31:16 +0000394
Ted Kremenek4f880632009-07-17 22:18:43 +0000395/// VisitChildren - Visit the children of a Stmt.
396CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
397 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000398 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000399 E = Terminator->child_end(); I != E; ++I) {
400 if (*I) B = Visit(*I);
401 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000402 return B;
403}
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Ted Kremenek4f880632009-07-17 22:18:43 +0000405CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
406 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000407
Ted Kremenek4f880632009-07-17 22:18:43 +0000408 if (alwaysAdd) {
409 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000410 AppendStmt(Block, A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000411 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000412
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000413 return Block;
414}
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek4f880632009-07-17 22:18:43 +0000416CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
417 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000418 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000419 AppendStmt(ConfluenceBlock, B);
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek4f880632009-07-17 22:18:43 +0000421 if (!FinishBlock(ConfluenceBlock))
422 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek4f880632009-07-17 22:18:43 +0000424 // create the block evaluating the LHS
425 CFGBlock* LHSBlock = createBlock(false);
426 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Ted Kremenek4f880632009-07-17 22:18:43 +0000428 // create the block evaluating the RHS
429 Succ = ConfluenceBlock;
430 Block = NULL;
431 CFGBlock* RHSBlock = addStmt(B->getRHS());
432 if (!FinishBlock(RHSBlock))
433 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Mike Stump00998a02009-07-23 23:25:26 +0000435 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000436 TryResult KnownVal = TryEvaluateBool(B->getLHS());
437 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
438 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000439
Ted Kremenek4f880632009-07-17 22:18:43 +0000440 // Now link the LHSBlock with RHSBlock.
441 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000442 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
443 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000444 } else {
Ted Kremenek4f880632009-07-17 22:18:43 +0000445 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000446 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
447 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000448 }
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Ted Kremenek4f880632009-07-17 22:18:43 +0000450 // Generate the blocks for evaluating the LHS.
451 Block = LHSBlock;
452 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000453 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000454 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000455 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000456 AppendStmt(Block, B);
Ted Kremenek4f880632009-07-17 22:18:43 +0000457 addStmt(B->getRHS());
458 return addStmt(B->getLHS());
459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremenek4f880632009-07-17 22:18:43 +0000461 return VisitStmt(B, alwaysAdd);
462}
463
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000464CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E, bool alwaysAdd) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000465 // FIXME
466 return NYS();
467}
468
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000469CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
470 bool alwaysAdd) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000471 // FIXME
472 return NYS();
473}
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Ted Kremenek4f880632009-07-17 22:18:43 +0000475CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
476 // "break" is a control-flow statement. Thus we stop processing the current
477 // block.
478 if (Block && !FinishBlock(Block))
479 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Ted Kremenek4f880632009-07-17 22:18:43 +0000481 // Now create a new block that ends with the break statement.
482 Block = createBlock(false);
483 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Ted Kremenek4f880632009-07-17 22:18:43 +0000485 // If there is no target for the break, then we are looking at an incomplete
486 // AST. This means that the CFG cannot be constructed.
487 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000488 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000489 else
490 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000491
492
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000493 return Block;
494}
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremenek4f880632009-07-17 22:18:43 +0000496CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
497 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000498 bool NoReturn = false;
499 if (C->getCallee()->getType().getNoReturnAttr()) {
500 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000501 }
Mike Stump24556362009-07-25 21:26:53 +0000502
503 if (FunctionDecl *FD = C->getDirectCallee())
504 if (FD->hasAttr<NoReturnAttr>())
505 NoReturn = true;
506
507 if (!NoReturn)
508 return VisitStmt(C, alwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Mike Stump24556362009-07-25 21:26:53 +0000510 if (Block && !FinishBlock(Block))
511 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Mike Stump24556362009-07-25 21:26:53 +0000513 // Create new block with no successor for the remaining pieces.
514 Block = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000515 AppendStmt(Block, C);
Mike Stump24556362009-07-25 21:26:53 +0000516
517 // Wire this to the exit block directly.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000518 AddSuccessor(Block, &cfg->getExit());
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Mike Stump24556362009-07-25 21:26:53 +0000520 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000521}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000522
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000523CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
524 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000525 AppendStmt(ConfluenceBlock, C);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000526 if (!FinishBlock(ConfluenceBlock))
527 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000529 Succ = ConfluenceBlock;
530 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000531 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000532 if (!FinishBlock(LHSBlock))
533 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000535 Succ = ConfluenceBlock;
536 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000537 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000538 if (!FinishBlock(RHSBlock))
539 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000541 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000542 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000543 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000544 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
545 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000546 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000547 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000548}
Mike Stump1eb44332009-09-09 15:08:12 +0000549
550
551CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
552 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000553
554 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
555 I != E; ++I ) {
556 LastBlock = addStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000558 if (badCFG)
559 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000560 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000561 return LastBlock;
562}
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000564CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
565 // Create the confluence block that will "merge" the results of the ternary
566 // expression.
567 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000568 AppendStmt(ConfluenceBlock, C);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000569 if (!FinishBlock(ConfluenceBlock))
570 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000572 // Create a block for the LHS expression if there is an LHS expression. A
573 // GCC extension allows LHS to be NULL, causing the condition to be the
574 // value that is returned instead.
575 // e.g: x ?: y is shorthand for: x ? x : y;
576 Succ = ConfluenceBlock;
577 Block = NULL;
578 CFGBlock* LHSBlock = NULL;
579 if (C->getLHS()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000580 LHSBlock = addStmt(C->getLHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000581 if (!FinishBlock(LHSBlock))
582 return 0;
583 Block = NULL;
584 }
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000586 // Create the block for the RHS expression.
587 Succ = ConfluenceBlock;
Ted Kremenek4f880632009-07-17 22:18:43 +0000588 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000589 if (!FinishBlock(RHSBlock))
590 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000592 // Create the block that will contain the condition.
593 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Mike Stump00998a02009-07-23 23:25:26 +0000595 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000596 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000597 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000598 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000599 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000600 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000601 // If we know the condition is false, add NULL as the successor for
602 // the block containing the condition. In this case, the confluence
603 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000604 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000605 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000606 } else {
607 // If we have no LHS expression, add the ConfluenceBlock as a direct
608 // successor for the block containing the condition. Moreover, we need to
609 // reverse the order of the predecessors in the ConfluenceBlock because
610 // the RHSBlock will have been added to the succcessors already, and we
611 // want the first predecessor to the the block containing the expression
612 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000613 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000614 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000615 std::reverse(ConfluenceBlock->pred_begin(),
616 ConfluenceBlock->pred_end());
617 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000620 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000621 Block->setTerminator(C);
622 return addStmt(C->getCond());
623}
624
Ted Kremenek4f880632009-07-17 22:18:43 +0000625CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
626 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000627
Ted Kremenek4f880632009-07-17 22:18:43 +0000628 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000629 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000630 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Ted Kremenek4f880632009-07-17 22:18:43 +0000633 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Ted Kremenek4f880632009-07-17 22:18:43 +0000635 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
636 typedef llvm::SmallVector<Decl*,10> BufTy;
637 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek4f880632009-07-17 22:18:43 +0000639 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
640 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
641 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
642 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Ted Kremenek4f880632009-07-17 22:18:43 +0000644 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
645 // automatically freed with the CFG.
646 DeclGroupRef DG(*I);
647 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000648 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000649 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Ted Kremenek4f880632009-07-17 22:18:43 +0000651 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000652 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000653 B = VisitDeclSubExpr(D);
654 }
Mike Stump1eb44332009-09-09 15:08:12 +0000655
656 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000657}
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Ted Kremenek4f880632009-07-17 22:18:43 +0000659/// VisitDeclSubExpr - Utility method to add block-level expressions for
660/// initializers in Decls.
661CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
662 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000663
Ted Kremenek4f880632009-07-17 22:18:43 +0000664 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Ted Kremenek4f880632009-07-17 22:18:43 +0000666 if (!VD)
667 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Ted Kremenek4f880632009-07-17 22:18:43 +0000669 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Ted Kremenek4f880632009-07-17 22:18:43 +0000671 if (Init) {
672 // Optimization: Don't create separate block-level statements for literals.
673 switch (Init->getStmtClass()) {
674 case Stmt::IntegerLiteralClass:
675 case Stmt::CharacterLiteralClass:
676 case Stmt::StringLiteralClass:
677 break;
678 default:
679 Block = addStmt(Init);
680 }
681 }
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Ted Kremenek4f880632009-07-17 22:18:43 +0000683 // If the type of VD is a VLA, then we must process its size expressions.
684 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
685 VA = FindVA(VA->getElementType().getTypePtr()))
686 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Ted Kremenek4f880632009-07-17 22:18:43 +0000688 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000689}
690
691CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000692 // We may see an if statement in the middle of a basic block, or it may be the
693 // first statement we are processing. In either case, we create a new basic
694 // block. First, we create the blocks for the then...else statements, and
695 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000696 // middle of a block, we stop processing that block. That block is then the
697 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000698
699 // The block we were proccessing is now finished. Make it the successor
700 // block.
701 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000702 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000703 if (!FinishBlock(Block))
704 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000705 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000706
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000707 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000708 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000709
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000710 if (Stmt* Else = I->getElse()) {
711 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000712
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000713 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000714 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000715 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000716 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000717
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000718 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
719 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000720 else if (Block) {
721 if (!FinishBlock(ElseBlock))
722 return 0;
723 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000724 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000725
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000726 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000727 CFGBlock* ThenBlock;
728 {
729 Stmt* Then = I->getThen();
730 assert (Then);
731 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000732 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000733 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000734
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000735 if (!ThenBlock) {
736 // We can reach here if the "then" body has all NullStmts.
737 // Create an empty block so we can distinguish between true and false
738 // branches in path-sensitive analyses.
739 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000740 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000741 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000742 if (!FinishBlock(ThenBlock))
743 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000744 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000745 }
746
Mike Stump6d9828c2009-07-17 01:31:16 +0000747 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000748 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000749
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000750 // Set the terminator of the new block to the If statement.
751 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000752
Mike Stump00998a02009-07-23 23:25:26 +0000753 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000754 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000755
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000756 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000757 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
758 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000759
760 // Add the condition as the last statement in the new block. This may create
761 // new blocks as the condition may contain control-flow. Any newly created
762 // blocks will be pointed to be "Block".
Ted Kremenek4f880632009-07-17 22:18:43 +0000763 return addStmt(I->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000764}
Mike Stump6d9828c2009-07-17 01:31:16 +0000765
766
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000767CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000768 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000769 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000770 // NOTE: If a "return" appears in the middle of a block, this means that the
771 // code afterwards is DEAD (unreachable). We still keep a basic block
772 // for that code; a simple "mark-and-sweep" from the entry block will be
773 // able to report such dead blocks.
Ted Kremenek6c249722009-09-24 18:45:41 +0000774 if (Block)
775 FinishBlock(Block);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000776
777 // Create the new block.
778 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000779
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000780 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000781 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000782
783 // Add the return statement to the block. This may create new blocks if R
784 // contains control-flow (short-circuit operations).
Ted Kremenek4f880632009-07-17 22:18:43 +0000785 return VisitStmt(R, true);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000786}
787
788CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
789 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000790 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000791 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000792
Ted Kremenek4f880632009-07-17 22:18:43 +0000793 if (!LabelBlock) // This can happen when the body is empty, i.e.
794 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000795
Ted Kremenek4f880632009-07-17 22:18:43 +0000796 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000797 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000798
799 // Labels partition blocks, so this is the end of the basic block we were
800 // processing (L is the block's label). Because this is label (and we have
801 // already processed the substatement) there is no extra control-flow to worry
802 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000803 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000804 if (!FinishBlock(LabelBlock))
805 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000806
807 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000808 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000809
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000810 // This block is now the implicit successor of other blocks.
811 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000812
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000813 return LabelBlock;
814}
815
816CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000817 // Goto is a control-flow statement. Thus we stop processing the current
818 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000819 if (Block)
820 FinishBlock(Block);
821
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000822 Block = createBlock(false);
823 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000824
825 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000826 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000827
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000828 if (I == LabelMap.end())
829 // We will need to backpatch this block later.
830 BackpatchBlocks.push_back(Block);
831 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000832 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000833
Mike Stump6d9828c2009-07-17 01:31:16 +0000834 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000835}
836
837CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000838 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000839
Mike Stumpfefb9f72009-07-21 01:12:51 +0000840 // "for" is a control-flow statement. Thus we stop processing the current
841 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000842 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000843 if (!FinishBlock(Block))
844 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000845 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000846 } else
847 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000848
849 // Because of short-circuit evaluation, the condition of the loop can span
850 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
851 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000852 CFGBlock* ExitConditionBlock = createBlock(false);
853 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000854
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000855 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000856 ExitConditionBlock->setTerminator(F);
857
858 // Now add the actual condition to the condition block. Because the condition
859 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000860 if (Stmt* C = F->getCond()) {
861 Block = ExitConditionBlock;
862 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000863 if (Block) {
864 if (!FinishBlock(EntryConditionBlock))
865 return 0;
866 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000867 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000868
Mike Stump6d9828c2009-07-17 01:31:16 +0000869 // The condition block is the implicit successor for the loop body as well as
870 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000871 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000872
Mike Stump00998a02009-07-23 23:25:26 +0000873 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000874 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Mike Stump00998a02009-07-23 23:25:26 +0000876 if (F->getCond())
877 KnownVal = TryEvaluateBool(F->getCond());
878
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000879 // Now create the loop body.
880 {
881 assert (F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000882
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000883 // Save the current values for Block, Succ, and continue and break targets
884 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
885 save_continue(ContinueTargetBlock),
Mike Stump6d9828c2009-07-17 01:31:16 +0000886 save_break(BreakTargetBlock);
887
Ted Kremenekaf603f72007-08-30 18:39:40 +0000888 // Create a new block to contain the (bottom) of the loop body.
889 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000890
Ted Kremeneke9334502008-09-04 21:48:47 +0000891 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000892 // Generate increment code in its own basic block. This is the target of
893 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +0000894 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000895 } else {
896 // No increment code. Create a special, empty, block that is used as the
897 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +0000898 assert(Succ == EntryConditionBlock);
899 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +0000900 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000901
Ted Kremenek3575f842009-04-28 00:51:56 +0000902 // Finish up the increment (or empty) block if it hasn't been already.
903 if (Block) {
904 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000905 if (!FinishBlock(Block))
906 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +0000907 Block = 0;
908 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000909
Ted Kremenek3575f842009-04-28 00:51:56 +0000910 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000911
Ted Kremenek3575f842009-04-28 00:51:56 +0000912 // The starting block for the loop increment is the block that should
913 // represent the 'loop target' for looping back to the start of the loop.
914 ContinueTargetBlock->setLoopTarget(F);
915
Ted Kremeneke9334502008-09-04 21:48:47 +0000916 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +0000917 BreakTargetBlock = LoopSuccessor;
918
919 // Now populate the body block, and in the process create new blocks as we
920 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +0000921 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000922
923 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +0000924 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +0000925 else if (Block && !FinishBlock(BodyBlock))
926 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000927
Ted Kremenek941fde82009-07-24 04:47:11 +0000928 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000929 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000930 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000931
Ted Kremenek941fde82009-07-24 04:47:11 +0000932 // Link up the condition block with the code that follows the loop. (the
933 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000934 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +0000935
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000936 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +0000937 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000938 if (Stmt* I = F->getInit()) {
939 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000940 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000941 } else {
942 // There is no loop initialization. We are thus basically a while loop.
943 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000944 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +0000945 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000946 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000947 }
948}
949
Ted Kremenek514de5a2008-11-11 17:10:00 +0000950CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
951 // Objective-C fast enumeration 'for' statements:
952 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
953 //
954 // for ( Type newVariable in collection_expression ) { statements }
955 //
956 // becomes:
957 //
958 // prologue:
959 // 1. collection_expression
960 // T. jump to loop_entry
961 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000962 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +0000963 // 1. ObjCForCollectionStmt [performs binding to newVariable]
964 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
965 // TB:
966 // statements
967 // T. jump to loop_entry
968 // FB:
969 // what comes after
970 //
971 // and
972 //
973 // Type existingItem;
974 // for ( existingItem in expression ) { statements }
975 //
976 // becomes:
977 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000978 // the same with newVariable replaced with existingItem; the binding works
979 // the same except that for one ObjCForCollectionStmt::getElement() returns
980 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +0000981 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000982
Ted Kremenek514de5a2008-11-11 17:10:00 +0000983 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000984
Ted Kremenek514de5a2008-11-11 17:10:00 +0000985 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000986 if (!FinishBlock(Block))
987 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +0000988 LoopSuccessor = Block;
989 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +0000990 } else
991 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000992
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000993 // Build the condition blocks.
994 CFGBlock* ExitConditionBlock = createBlock(false);
995 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000996
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000997 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000998 ExitConditionBlock->setTerminator(S);
999
1000 // The last statement in the block should be the ObjCForCollectionStmt, which
1001 // performs the actual binding to 'element' and determines if there are any
1002 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001003 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001004 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001005
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001006 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001007 // generate new blocks as necesary. We DON'T add the statement by default to
1008 // the CFG unless it contains control-flow.
Ted Kremenek4f880632009-07-17 22:18:43 +00001009 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001010 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001011 if (!FinishBlock(EntryConditionBlock))
1012 return 0;
1013 Block = 0;
1014 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001015
1016 // The condition block is the implicit successor for the loop body as well as
1017 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001018 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001019
Ted Kremenek514de5a2008-11-11 17:10:00 +00001020 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001021 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001022 // Save the current values for Succ, continue and break targets.
1023 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001024 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1025
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001026 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001027 ContinueTargetBlock = EntryConditionBlock;
1028
Ted Kremenek4f880632009-07-17 22:18:43 +00001029 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001030
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001031 if (!BodyBlock)
1032 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001033 else if (Block) {
1034 if (!FinishBlock(BodyBlock))
1035 return 0;
1036 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001037
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001038 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001039 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001040 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001041
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001042 // Link up the condition block with the code that follows the loop.
1043 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001044 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001045
Ted Kremenek514de5a2008-11-11 17:10:00 +00001046 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001047 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001048 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001049}
1050
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001051CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1052 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001053
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001054 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001055 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001056
Ted Kremenekda5348e2009-05-05 23:11:51 +00001057 // The sync body starts its own basic block. This makes it a little easier
1058 // for diagnostic clients.
1059 if (SyncBlock) {
1060 if (!FinishBlock(SyncBlock))
1061 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001062
Ted Kremenekda5348e2009-05-05 23:11:51 +00001063 Block = 0;
1064 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001065
Ted Kremenekda5348e2009-05-05 23:11:51 +00001066 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001067
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001068 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001069 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001070}
Mike Stump6d9828c2009-07-17 01:31:16 +00001071
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001072CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001073 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001074 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001075}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001076
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001077CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001078 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001079
Mike Stumpfefb9f72009-07-21 01:12:51 +00001080 // "while" is a control-flow statement. Thus we stop processing the current
1081 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001082 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001083 if (!FinishBlock(Block))
1084 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001085 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001086 } else
1087 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001088
1089 // Because of short-circuit evaluation, the condition of the loop can span
1090 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1091 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001092 CFGBlock* ExitConditionBlock = createBlock(false);
1093 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001094
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001095 // Set the terminator for the "exit" condition block.
1096 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001097
1098 // Now add the actual condition to the condition block. Because the condition
1099 // itself may contain control-flow, new blocks may be created. Thus we update
1100 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001101 if (Stmt* C = W->getCond()) {
1102 Block = ExitConditionBlock;
1103 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001104 assert(Block == EntryConditionBlock);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001105 if (Block) {
1106 if (!FinishBlock(EntryConditionBlock))
1107 return 0;
1108 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001109 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001110
1111 // The condition block is the implicit successor for the loop body as well as
1112 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001113 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001114
Mike Stump00998a02009-07-23 23:25:26 +00001115 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001116 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001117
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001118 // Process the loop body.
1119 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001120 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001121
1122 // Save the current values for Block, Succ, and continue and break targets
1123 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1124 save_continue(ContinueTargetBlock),
1125 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001126
Mike Stump6d9828c2009-07-17 01:31:16 +00001127 // Create an empty block to represent the transition block for looping back
1128 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001129 Block = 0;
1130 assert(Succ == EntryConditionBlock);
1131 Succ = createBlock();
1132 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001133 ContinueTargetBlock = Succ;
1134
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001135 // All breaks should go to the code following the loop.
1136 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001137
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001138 // NULL out Block to force lazy instantiation of blocks for the body.
1139 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001140
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001141 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001142 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001143
Ted Kremenekaf603f72007-08-30 18:39:40 +00001144 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001145 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001146 else if (Block) {
1147 if (!FinishBlock(BodyBlock))
1148 return 0;
1149 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001150
Ted Kremenek941fde82009-07-24 04:47:11 +00001151 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001152 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001153 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001154
Ted Kremenek941fde82009-07-24 04:47:11 +00001155 // Link up the condition block with the code that follows the loop. (the
1156 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001157 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001158
1159 // There can be no more statements in the condition block since we loop back
1160 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001161 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001162
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001163 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001164 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001165 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001166}
Mike Stump1eb44332009-09-09 15:08:12 +00001167
1168
Ted Kremenek4f880632009-07-17 22:18:43 +00001169CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1170 // FIXME: For now we pretend that @catch and the code it contains does not
1171 // exit.
1172 return Block;
1173}
Mike Stump6d9828c2009-07-17 01:31:16 +00001174
Ted Kremenek2fda5042008-12-09 20:20:09 +00001175CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1176 // FIXME: This isn't complete. We basically treat @throw like a return
1177 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001178
Ted Kremenek6c249722009-09-24 18:45:41 +00001179 // If we were in the middle of a block we stop processing that block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001180 if (Block && !FinishBlock(Block))
1181 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001182
Ted Kremenek2fda5042008-12-09 20:20:09 +00001183 // Create the new block.
1184 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001185
Ted Kremenek2fda5042008-12-09 20:20:09 +00001186 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001187 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001188
1189 // Add the statement to the block. This may create new blocks if S contains
1190 // control-flow (short-circuit operations).
Ted Kremenek4f880632009-07-17 22:18:43 +00001191 return VisitStmt(S, true);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001192}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001193
Mike Stump0979d802009-07-22 22:56:04 +00001194CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001195 // If we were in the middle of a block we stop processing that block.
Mike Stump0979d802009-07-22 22:56:04 +00001196 if (Block && !FinishBlock(Block))
1197 return 0;
1198
1199 // Create the new block.
1200 Block = createBlock(false);
1201
1202 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001203 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001204
1205 // Add the statement to the block. This may create new blocks if S contains
1206 // control-flow (short-circuit operations).
1207 return VisitStmt(T, true);
1208}
1209
Ted Kremenek4f880632009-07-17 22:18:43 +00001210CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001211 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001212
Mike Stump8f9893a2009-07-21 01:27:50 +00001213 // "do...while" is a control-flow statement. Thus we stop processing the
1214 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001215 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001216 if (!FinishBlock(Block))
1217 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001218 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001219 } else
1220 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001221
1222 // Because of short-circuit evaluation, the condition of the loop can span
1223 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1224 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001225 CFGBlock* ExitConditionBlock = createBlock(false);
1226 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001227
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001228 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001229 ExitConditionBlock->setTerminator(D);
1230
1231 // Now add the actual condition to the condition block. Because the condition
1232 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001233 if (Stmt* C = D->getCond()) {
1234 Block = ExitConditionBlock;
1235 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001236 if (Block) {
1237 if (!FinishBlock(EntryConditionBlock))
1238 return 0;
1239 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001240 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001241
Ted Kremenek54827132008-02-27 07:20:00 +00001242 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001243 Succ = EntryConditionBlock;
1244
Mike Stump00998a02009-07-23 23:25:26 +00001245 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001246 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001247
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001248 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001249 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001250 {
1251 assert (D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001252
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001253 // Save the current values for Block, Succ, and continue and break targets
1254 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1255 save_continue(ContinueTargetBlock),
1256 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001257
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001258 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001259 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001260
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001261 // All breaks should go to the code following the loop.
1262 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001263
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001264 // NULL out Block to force lazy instantiation of blocks for the body.
1265 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001266
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001267 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001268 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001269
Ted Kremenekaf603f72007-08-30 18:39:40 +00001270 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001271 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001272 else if (Block) {
1273 if (!FinishBlock(BodyBlock))
1274 return 0;
1275 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001276
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001277 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001278 // ExitConditionBlock to represent the "loop back" transition. Create an
1279 // empty block to represent the transition block for looping back to the
1280 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001281 // FIXME: Can we do this more efficiently without adding another block?
1282 Block = NULL;
1283 Succ = BodyBlock;
1284 CFGBlock *LoopBackBlock = createBlock();
1285 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001286
Ted Kremenek941fde82009-07-24 04:47:11 +00001287 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001288 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001289 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001290
Ted Kremenek941fde82009-07-24 04:47:11 +00001291 // Link up the condition block with the code that follows the loop.
1292 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001293 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001294
1295 // There can be no more statements in the body block(s) since we loop back to
1296 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001297 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001298
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001299 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001300 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001301 return BodyBlock;
1302}
1303
1304CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1305 // "continue" is a control-flow statement. Thus we stop processing the
1306 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001307 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001308 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001309
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001310 // Now create a new block that ends with the continue statement.
1311 Block = createBlock(false);
1312 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001313
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001314 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001315 // incomplete AST. This means the CFG cannot be constructed.
1316 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001317 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001318 else
1319 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001320
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001321 return Block;
1322}
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001324CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1325 bool alwaysAdd) {
1326
1327 if (alwaysAdd) {
1328 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001329 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001330 }
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Ted Kremenek4f880632009-07-17 22:18:43 +00001332 // VLA types have expressions that must be evaluated.
1333 if (E->isArgumentType()) {
1334 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1335 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1336 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001337 }
Mike Stump1eb44332009-09-09 15:08:12 +00001338
Mike Stump6d9828c2009-07-17 01:31:16 +00001339 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001340}
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Ted Kremenek4f880632009-07-17 22:18:43 +00001342/// VisitStmtExpr - Utility method to handle (nested) statement
1343/// expressions (a GCC extension).
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001344CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1345 if (alwaysAdd) {
1346 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001347 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001348 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001349 return VisitCompoundStmt(SE->getSubStmt());
1350}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001351
Ted Kremenek411cdee2008-04-16 21:10:48 +00001352CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001353 // "switch" is a control-flow statement. Thus we stop processing the current
1354 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001355 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001356
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001357 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001358 if (!FinishBlock(Block))
1359 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001360 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001361 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001362
1363 // Save the current "switch" context.
1364 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001365 save_break(BreakTargetBlock),
1366 save_default(DefaultCaseBlock);
1367
Mike Stump6d9828c2009-07-17 01:31:16 +00001368 // Set the "default" case to be the block after the switch statement. If the
1369 // switch statement contains a "default:", this value will be overwritten with
1370 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001371 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001372
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001373 // Create a new block that will contain the switch statement.
1374 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001375
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001376 // Now process the switch body. The code after the switch is the implicit
1377 // successor.
1378 Succ = SwitchSuccessor;
1379 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001380
1381 // When visiting the body, the case statements should automatically get linked
1382 // up to the switch. We also don't keep a pointer to the body, since all
1383 // control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001384 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001385 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001386 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001387 if (Block) {
1388 if (!FinishBlock(BodyBlock))
1389 return 0;
1390 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001391
Mike Stump6d9828c2009-07-17 01:31:16 +00001392 // If we have no "default:" case, the default transition is to the code
1393 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001394 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001395
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001396 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001397 SwitchTerminatedBlock->setTerminator(Terminator);
1398 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001399 Block = SwitchTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001400
Ted Kremenek411cdee2008-04-16 21:10:48 +00001401 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001402}
1403
Ted Kremenek4f880632009-07-17 22:18:43 +00001404CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001405 // CaseStmts are essentially labels, so they are the first statement in a
1406 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001407
Ted Kremenek4f880632009-07-17 22:18:43 +00001408 if (CS->getSubStmt())
1409 addStmt(CS->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001411 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001412 if (!CaseBlock)
1413 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001414
1415 // Cases statements partition blocks, so this is the top of the basic block we
1416 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001417 CaseBlock->setLabel(CS);
1418
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001419 if (!FinishBlock(CaseBlock))
1420 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001421
1422 // Add this block to the list of successors for the block with the switch
1423 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001424 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001425 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001426
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001427 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1428 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001429
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001430 // This block is now the implicit successor of other blocks.
1431 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001432
Ted Kremenek2677ea82008-03-15 07:45:02 +00001433 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001434}
Mike Stump6d9828c2009-07-17 01:31:16 +00001435
Ted Kremenek411cdee2008-04-16 21:10:48 +00001436CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001437 if (Terminator->getSubStmt())
1438 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001440 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001441
1442 if (!DefaultCaseBlock)
1443 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001444
1445 // Default statements partition blocks, so this is the top of the basic block
1446 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001447 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001449 if (!FinishBlock(DefaultCaseBlock))
1450 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001451
Mike Stump6d9828c2009-07-17 01:31:16 +00001452 // Unlike case statements, we don't add the default block to the successors
1453 // for the switch statement immediately. This is done when we finish
1454 // processing the switch statement. This allows for the default case
1455 // (including a fall-through to the code after the switch statement) to always
1456 // be the last successor of a switch-terminated block.
1457
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001458 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1459 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001460
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001461 // This block is now the implicit successor of other blocks.
1462 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001463
1464 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001465}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001466
Ted Kremenek19bb3562007-08-28 19:26:49 +00001467CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001468 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001469 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001470
Ted Kremenek19bb3562007-08-28 19:26:49 +00001471 if (!IBlock) {
1472 IBlock = createBlock(false);
1473 cfg->setIndirectGotoBlock(IBlock);
1474 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001475
Ted Kremenek19bb3562007-08-28 19:26:49 +00001476 // IndirectGoto is a control-flow statement. Thus we stop processing the
1477 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001478 if (Block && !FinishBlock(Block))
1479 return 0;
1480
Ted Kremenek19bb3562007-08-28 19:26:49 +00001481 Block = createBlock(false);
1482 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001483 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001484 return addStmt(I->getTarget());
1485}
1486
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001487} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001488
Mike Stump6d9828c2009-07-17 01:31:16 +00001489/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1490/// no successors or predecessors. If this is the first block created in the
1491/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001492CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001493 bool first_block = begin() == end();
1494
1495 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001496 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1497 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1498 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001499
1500 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001501 if (first_block)
1502 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001503
1504 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001505 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001506}
1507
Ted Kremenek026473c2007-08-23 16:51:22 +00001508/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1509/// CFG is returned to the caller.
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001510CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001511 CFGBuilder Builder;
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001512 return Builder.buildCFG(Statement, C);
Ted Kremenek026473c2007-08-23 16:51:22 +00001513}
1514
Ted Kremenek63f58872007-10-01 19:33:33 +00001515//===----------------------------------------------------------------------===//
1516// CFG: Queries for BlkExprs.
1517//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001518
Ted Kremenek63f58872007-10-01 19:33:33 +00001519namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001520 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001521}
1522
Ted Kremenek411cdee2008-04-16 21:10:48 +00001523static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1524 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001525 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001526
Ted Kremenek411cdee2008-04-16 21:10:48 +00001527 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001528 if (!*I) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001529
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001530 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1531 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001532
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001533 FindSubExprAssignments(*I, Set);
1534 }
1535}
1536
Ted Kremenek63f58872007-10-01 19:33:33 +00001537static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1538 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001539
1540 // Look for assignments that are used as subexpressions. These are the only
1541 // assignments that we want to *possibly* register as a block-level
1542 // expression. Basically, if an assignment occurs both in a subexpression and
1543 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001544 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001545
Ted Kremenek63f58872007-10-01 19:33:33 +00001546 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001547 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001548 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001549
Ted Kremenek411cdee2008-04-16 21:10:48 +00001550 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001551
1552 // Iterate over the statements again on identify the Expr* and Stmt* at the
1553 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001554
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001555 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001556 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001557
Ted Kremenek411cdee2008-04-16 21:10:48 +00001558 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001559 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001560 // expression are really "statements" whose value is never used by
1561 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001562 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001563 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001564 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1565 // Special handling for statement expressions. The last statement in
1566 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001567 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001568 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001569 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001570 (*M)[C->body_back()] = x;
1571 }
1572 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001573
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001574 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001575 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001576 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001577
Ted Kremenek411cdee2008-04-16 21:10:48 +00001578 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001579
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001580 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001581
Ted Kremenek390e48b2008-11-12 21:11:49 +00001582 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001583 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001584 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001585 }
1586 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001587
Ted Kremenek63f58872007-10-01 19:33:33 +00001588 return M;
1589}
1590
Ted Kremenek86946742008-01-17 20:48:37 +00001591CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1592 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001593 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001594
Ted Kremenek63f58872007-10-01 19:33:33 +00001595 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001596 BlkExprMapTy::iterator I = M->find(S);
Mike Stump6d9828c2009-07-17 01:31:16 +00001597
Ted Kremenek63f58872007-10-01 19:33:33 +00001598 if (I == M->end()) return CFG::BlkExprNumTy();
1599 else return CFG::BlkExprNumTy(I->second);
1600}
1601
1602unsigned CFG::getNumBlkExprs() {
1603 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1604 return M->size();
1605 else {
1606 // We assume callers interested in the number of BlkExprs will want
1607 // the map constructed if it doesn't already exist.
1608 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1609 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1610 }
1611}
1612
Ted Kremenek274f4332008-04-28 18:00:46 +00001613//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001614// Cleanup: CFG dstor.
1615//===----------------------------------------------------------------------===//
1616
Ted Kremenek63f58872007-10-01 19:33:33 +00001617CFG::~CFG() {
1618 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1619}
Mike Stump6d9828c2009-07-17 01:31:16 +00001620
Ted Kremenek7dba8602007-08-29 21:56:09 +00001621//===----------------------------------------------------------------------===//
1622// CFG pretty printing
1623//===----------------------------------------------------------------------===//
1624
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001625namespace {
1626
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001627class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump6d9828c2009-07-17 01:31:16 +00001628
Ted Kremenek42a509f2007-08-31 21:30:12 +00001629 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1630 StmtMapTy StmtMap;
1631 signed CurrentBlock;
1632 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001633 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001634public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001635
Chris Lattnere4f21422009-06-30 01:26:17 +00001636 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1637 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001638 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1639 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001640 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001641 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001642 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001643 }
1644 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001645
Ted Kremenek42a509f2007-08-31 21:30:12 +00001646 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001647
Chris Lattnere4f21422009-06-30 01:26:17 +00001648 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001649 void setBlockID(signed i) { CurrentBlock = i; }
1650 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001651
Ted Kremeneka95d3752008-09-13 05:16:45 +00001652 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001653
Ted Kremenek411cdee2008-04-16 21:10:48 +00001654 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001655
1656 if (I == StmtMap.end())
1657 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001658
1659 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek42a509f2007-08-31 21:30:12 +00001660 && I->second.second == CurrentStmt)
1661 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001662
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001663 OS << "[B" << I->second.first << "." << I->second.second << "]";
1664 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001665 }
1666};
Chris Lattnere4f21422009-06-30 01:26:17 +00001667} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001668
Chris Lattnere4f21422009-06-30 01:26:17 +00001669
1670namespace {
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001671class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1672 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001673
Ted Kremeneka95d3752008-09-13 05:16:45 +00001674 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001675 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001676 PrintingPolicy Policy;
1677
Ted Kremenek42a509f2007-08-31 21:30:12 +00001678public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001679 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001680 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001681 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001682
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001683 void VisitIfStmt(IfStmt* I) {
1684 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001685 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001686 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001687
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001688 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001689 void VisitStmt(Stmt* Terminator) {
1690 Terminator->printPretty(OS, Helper, Policy);
1691 }
1692
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001693 void VisitForStmt(ForStmt* F) {
1694 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001695 if (F->getInit()) OS << "...";
1696 OS << "; ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001697 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001698 OS << "; ";
1699 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001700 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001701 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001702
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001703 void VisitWhileStmt(WhileStmt* W) {
1704 OS << "while " ;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001705 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001706 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001707
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001708 void VisitDoStmt(DoStmt* D) {
1709 OS << "do ... while ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001710 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001711 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001712
Ted Kremenek411cdee2008-04-16 21:10:48 +00001713 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001714 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001715 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001716 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001717
Ted Kremenek805e9a82007-08-31 21:49:40 +00001718 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001719 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001720 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001721 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001722
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001723 void VisitChooseExpr(ChooseExpr* C) {
1724 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001725 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001726 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001727 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001728
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001729 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1730 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001731 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001732 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001733
Ted Kremenek805e9a82007-08-31 21:49:40 +00001734 void VisitBinaryOperator(BinaryOperator* B) {
1735 if (!B->isLogicalOp()) {
1736 VisitExpr(B);
1737 return;
1738 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001739
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001740 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001741
Ted Kremenek805e9a82007-08-31 21:49:40 +00001742 switch (B->getOpcode()) {
1743 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001744 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001745 return;
1746 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001747 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001748 return;
1749 default:
1750 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001751 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00001752 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001753
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001754 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001755 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001756 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001757};
Chris Lattnere4f21422009-06-30 01:26:17 +00001758} // end anonymous namespace
1759
Mike Stump6d9828c2009-07-17 01:31:16 +00001760
Chris Lattnere4f21422009-06-30 01:26:17 +00001761static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1762 Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001763 if (Helper) {
1764 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001765 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001766 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00001767
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001768 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001769 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001770 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001771 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001772 return;
1773 }
1774 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001775
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001776 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001777 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001778 if (B->getOpcode() == BinaryOperator::Comma) {
1779 OS << "... , ";
1780 Helper->handledStmt(B->getRHS(),OS);
1781 OS << '\n';
1782 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001783 }
1784 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001785 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001786
Chris Lattnere4f21422009-06-30 01:26:17 +00001787 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00001788
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001789 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001790 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001791}
Mike Stump6d9828c2009-07-17 01:31:16 +00001792
Chris Lattnere4f21422009-06-30 01:26:17 +00001793static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1794 const CFGBlock& B,
1795 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001796
Ted Kremenek42a509f2007-08-31 21:30:12 +00001797 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00001798
Ted Kremenek7dba8602007-08-29 21:56:09 +00001799 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00001800 OS << "\n [ B" << B.getBlockID();
1801
Ted Kremenek42a509f2007-08-31 21:30:12 +00001802 if (&B == &cfg->getEntry())
1803 OS << " (ENTRY) ]\n";
1804 else if (&B == &cfg->getExit())
1805 OS << " (EXIT) ]\n";
1806 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001807 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001808 else
1809 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00001810
Ted Kremenek9cffe732007-08-29 23:20:49 +00001811 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001812 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001813
1814 if (print_edges)
1815 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001816
Ted Kremenek411cdee2008-04-16 21:10:48 +00001817 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001818 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001819 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001820 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001821 C->getLHS()->printPretty(OS, Helper,
1822 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001823 if (C->getRHS()) {
1824 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001825 C->getRHS()->printPretty(OS, Helper,
1826 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001827 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001828 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001829 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001830 else
1831 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001832
Ted Kremenek9cffe732007-08-29 23:20:49 +00001833 OS << ":\n";
1834 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001835
Ted Kremenekfddd5182007-08-21 21:42:03 +00001836 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001837 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00001838
Ted Kremenek42a509f2007-08-31 21:30:12 +00001839 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1840 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001841
Ted Kremenek9cffe732007-08-29 23:20:49 +00001842 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001843 if (print_edges)
1844 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001845
Ted Kremeneka95d3752008-09-13 05:16:45 +00001846 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001847
Ted Kremenek42a509f2007-08-31 21:30:12 +00001848 if (Helper)
1849 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00001850
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001851 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001852 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001853
Ted Kremenek9cffe732007-08-29 23:20:49 +00001854 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001855 if (B.getTerminator()) {
1856 if (print_edges)
1857 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001858
Ted Kremenek9cffe732007-08-29 23:20:49 +00001859 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001860
Ted Kremenek42a509f2007-08-31 21:30:12 +00001861 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00001862
Chris Lattnere4f21422009-06-30 01:26:17 +00001863 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1864 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00001865 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001866 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001867 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001868
Ted Kremenek9cffe732007-08-29 23:20:49 +00001869 if (print_edges) {
1870 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001871 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001872 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001873
Ted Kremenek42a509f2007-08-31 21:30:12 +00001874 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1875 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001876
Ted Kremenek42a509f2007-08-31 21:30:12 +00001877 if (i == 8 || (i-8) == 0)
1878 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001879
Ted Kremenek9cffe732007-08-29 23:20:49 +00001880 OS << " B" << (*I)->getBlockID();
1881 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001882
Ted Kremenek42a509f2007-08-31 21:30:12 +00001883 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00001884
Ted Kremenek42a509f2007-08-31 21:30:12 +00001885 // Print the successors of this block.
1886 OS << " Successors (" << B.succ_size() << "):";
1887 i = 0;
1888
1889 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1890 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001891
Ted Kremenek42a509f2007-08-31 21:30:12 +00001892 if (i == 8 || (i-8) % 10 == 0)
1893 OS << "\n ";
1894
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001895 if (*I)
1896 OS << " B" << (*I)->getBlockID();
1897 else
1898 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001899 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001900
Ted Kremenek9cffe732007-08-29 23:20:49 +00001901 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001902 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001903}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001904
Ted Kremenek42a509f2007-08-31 21:30:12 +00001905
1906/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001907void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001908
1909/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00001910void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1911 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00001912
Ted Kremenek42a509f2007-08-31 21:30:12 +00001913 // Print the entry block.
1914 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00001915
Ted Kremenek42a509f2007-08-31 21:30:12 +00001916 // Iterate through the CFGBlocks and print them one by one.
1917 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1918 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001919 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00001920 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001921
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001922 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001923 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001924
Ted Kremenek42a509f2007-08-31 21:30:12 +00001925 // Print the exit block.
1926 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00001927 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00001928}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001929
1930/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001931void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1932 print(llvm::errs(), cfg, LO);
1933}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001934
1935/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1936/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00001937void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1938 const LangOptions &LO) const {
1939 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001940 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00001941}
Ted Kremenek7dba8602007-08-29 21:56:09 +00001942
Ted Kremeneka2925852008-01-30 23:02:42 +00001943/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00001944void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00001945 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00001946 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00001947 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1948}
1949
Ted Kremenek390e48b2008-11-12 21:11:49 +00001950Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00001951
Ted Kremenek411cdee2008-04-16 21:10:48 +00001952 if (!Terminator)
1953 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001954
Ted Kremenek411cdee2008-04-16 21:10:48 +00001955 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001956
Ted Kremenek411cdee2008-04-16 21:10:48 +00001957 switch (Terminator->getStmtClass()) {
1958 default:
1959 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001960
Ted Kremenek411cdee2008-04-16 21:10:48 +00001961 case Stmt::ForStmtClass:
1962 E = cast<ForStmt>(Terminator)->getCond();
1963 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001964
Ted Kremenek411cdee2008-04-16 21:10:48 +00001965 case Stmt::WhileStmtClass:
1966 E = cast<WhileStmt>(Terminator)->getCond();
1967 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001968
Ted Kremenek411cdee2008-04-16 21:10:48 +00001969 case Stmt::DoStmtClass:
1970 E = cast<DoStmt>(Terminator)->getCond();
1971 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001972
Ted Kremenek411cdee2008-04-16 21:10:48 +00001973 case Stmt::IfStmtClass:
1974 E = cast<IfStmt>(Terminator)->getCond();
1975 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001976
Ted Kremenek411cdee2008-04-16 21:10:48 +00001977 case Stmt::ChooseExprClass:
1978 E = cast<ChooseExpr>(Terminator)->getCond();
1979 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001980
Ted Kremenek411cdee2008-04-16 21:10:48 +00001981 case Stmt::IndirectGotoStmtClass:
1982 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1983 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001984
Ted Kremenek411cdee2008-04-16 21:10:48 +00001985 case Stmt::SwitchStmtClass:
1986 E = cast<SwitchStmt>(Terminator)->getCond();
1987 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001988
Ted Kremenek411cdee2008-04-16 21:10:48 +00001989 case Stmt::ConditionalOperatorClass:
1990 E = cast<ConditionalOperator>(Terminator)->getCond();
1991 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001992
Ted Kremenek411cdee2008-04-16 21:10:48 +00001993 case Stmt::BinaryOperatorClass: // '&&' and '||'
1994 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001995 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001996
Ted Kremenek390e48b2008-11-12 21:11:49 +00001997 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00001998 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001999 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002000
Ted Kremenek411cdee2008-04-16 21:10:48 +00002001 return E ? E->IgnoreParens() : NULL;
2002}
2003
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002004bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002005
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002006 if (!Terminator)
2007 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002008
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002009 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002010
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002011 switch (Terminator->getStmtClass()) {
2012 default:
2013 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002014
2015 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002016 case Stmt::WhileStmtClass:
2017 case Stmt::DoStmtClass:
2018 case Stmt::IfStmtClass:
2019 case Stmt::ChooseExprClass:
2020 case Stmt::ConditionalOperatorClass:
2021 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002022 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002023 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002024
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002025 return E ? E->IgnoreParens() : NULL;
2026}
2027
Ted Kremeneka2925852008-01-30 23:02:42 +00002028
Ted Kremenek7dba8602007-08-29 21:56:09 +00002029//===----------------------------------------------------------------------===//
2030// CFG Graphviz Visualization
2031//===----------------------------------------------------------------------===//
2032
Ted Kremenek42a509f2007-08-31 21:30:12 +00002033
2034#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002035static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002036#endif
2037
Chris Lattnere4f21422009-06-30 01:26:17 +00002038void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002039#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002040 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002041 GraphHelper = &H;
2042 llvm::ViewGraph(this,"CFG");
2043 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002044#endif
2045}
2046
Ted Kremenek7dba8602007-08-29 21:56:09 +00002047namespace llvm {
2048template<>
2049struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson02995ce2009-06-24 17:37:55 +00002050 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
2051 bool ShortNames) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002052
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002053#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002054 std::string OutSStr;
2055 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002056 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002057 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002058
2059 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2060
2061 // Process string output to make it nicer...
2062 for (unsigned i = 0; i != OutStr.length(); ++i)
2063 if (OutStr[i] == '\n') { // Left justify
2064 OutStr[i] = '\\';
2065 OutStr.insert(OutStr.begin()+i+1, 'l');
2066 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002067
Ted Kremenek7dba8602007-08-29 21:56:09 +00002068 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002069#else
2070 return "";
2071#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002072 }
2073};
2074} // end namespace llvm