blob: 217df459b8bb18db1e48955ee1d1d125fa7aa58e [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"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000019#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000020#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek7dba8602007-08-29 21:56:09 +000021#include "llvm/Support/GraphWriter.h"
Ted Kremenek7e3a89d2007-12-17 19:35:20 +000022#include "llvm/Support/Streams.h"
Ted Kremenek6fa9b882008-01-08 18:15:10 +000023#include "llvm/Support/Compiler.h"
Ted Kremenek274f4332008-04-28 18:00:46 +000024#include <llvm/Support/Allocator.h>
Ted Kremeneka95d3752008-09-13 05:16:45 +000025#include <llvm/Support/Format.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 Kremenekfddd5182007-08-21 21:42:03 +000055 CFG* cfg;
56 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000057 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000058 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000059 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000060 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000061 CFGBlock* DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +000062
Ted Kremenek19bb3562007-08-28 19:26:49 +000063 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000064 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
65 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +000066
67 // A list of blocks that end with a "goto" that must be backpatched to their
68 // resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +000069 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000070 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +000071
Ted Kremenek19bb3562007-08-28 19:26:49 +000072 // A list of labels whose address has been taken (for indirect gotos).
73 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
74 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +000075
76public:
Ted Kremenek026473c2007-08-23 16:51:22 +000077 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000078 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000079 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenekfddd5182007-08-21 21:42:03 +000080 // Create an empty CFG.
Mike Stump6d9828c2009-07-17 01:31:16 +000081 cfg = new CFG();
Ted Kremenekfddd5182007-08-21 21:42:03 +000082 }
Mike Stump6d9828c2009-07-17 01:31:16 +000083
Ted Kremenekfddd5182007-08-21 21:42:03 +000084 ~CFGBuilder() { delete cfg; }
Mike Stump6d9828c2009-07-17 01:31:16 +000085
Ted Kremenekd4fdee32007-08-23 21:42:29 +000086 // buildCFG - Used by external clients to construct the CFG.
Mike Stumpe5af3ce2009-07-20 23:24:15 +000087 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump6d9828c2009-07-17 01:31:16 +000088
Ted Kremenek4f880632009-07-17 22:18:43 +000089private:
90 // Visitors to walk an AST and construct the CFG.
91 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
92 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
Ted Kremenek13fc08a2009-07-18 00:47:21 +000093 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
94 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +000095 CFGBlock *VisitBreakStmt(BreakStmt *B);
96 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
97 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +000098 CFGBlock *VisitChooseExpr(ChooseExpr *C);
99 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
100 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
101 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump0979d802009-07-22 22:56:04 +0000102 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenek4f880632009-07-17 22:18:43 +0000103 CFGBlock *VisitDeclStmt(DeclStmt *DS);
104 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000105 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
106 CFGBlock *VisitDoStmt(DoStmt *D);
107 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000108 CFGBlock *VisitGotoStmt(GotoStmt* G);
109 CFGBlock *VisitIfStmt(IfStmt *I);
110 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
111 CFGBlock *VisitLabelStmt(LabelStmt *L);
112 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
113 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
114 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
115 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
116 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
117 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000118 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
119 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000120 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
121 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000122
Ted Kremenek4f880632009-07-17 22:18:43 +0000123 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
124 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
125 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000126
Ted Kremenek274f4332008-04-28 18:00:46 +0000127 // NYS == Not Yet Supported
128 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000129 badCFG = true;
130 return Block;
131 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000132
Ted Kremenek4f880632009-07-17 22:18:43 +0000133 void autoCreateBlock() { if (!Block) Block = createBlock(); }
134 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000135 bool FinishBlock(CFGBlock* B);
Ted Kremenek4f880632009-07-17 22:18:43 +0000136 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
137
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000138
139 /// TryResult - a class representing a variant over the values
140 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
141 /// and is used by the CFGBuilder to decide if a branch condition
142 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000143 class TryResult {
144 int X;
145 public:
146 TryResult(bool b) : X(b ? 1 : 0) {}
147 TryResult() : X(-1) {}
148
149 bool isTrue() const { return X == 1; }
150 bool isFalse() const { return X == 0; }
151 bool isKnown() const { return X >= 0; }
152 void negate() {
153 assert(isKnown());
154 X ^= 0x1;
155 }
156 };
157
Mike Stump00998a02009-07-23 23:25:26 +0000158 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
159 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000160 TryResult TryEvaluateBool(Expr *S) {
Mike Stump00998a02009-07-23 23:25:26 +0000161 Expr::EvalResult Result;
Ted Kremenek941fde82009-07-24 04:47:11 +0000162 if (S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000163 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000164
165 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000166 }
167
Ted Kremenek4102af92008-03-13 03:04:22 +0000168 bool badCFG;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000169};
Mike Stump6d9828c2009-07-17 01:31:16 +0000170
Douglas Gregor898574e2008-12-05 23:32:09 +0000171// FIXME: Add support for dependent-sized array types in C++?
172// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000173static VariableArrayType* FindVA(Type* t) {
174 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
175 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
176 if (vat->getSizeExpr())
177 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000178
Ted Kremenek610a09e2008-09-26 22:58:57 +0000179 t = vt->getElementType().getTypePtr();
180 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000181
Ted Kremenek610a09e2008-09-26 22:58:57 +0000182 return 0;
183}
Mike Stump6d9828c2009-07-17 01:31:16 +0000184
185/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
186/// arbitrary statement. Examples include a single expression or a function
187/// body (compound statement). The ownership of the returned CFG is
188/// transferred to the caller. If CFG construction fails, this method returns
189/// NULL.
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000190CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
191 Context = C;
Ted Kremenek4f880632009-07-17 22:18:43 +0000192 assert(cfg);
193 if (!Statement)
194 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000195
Ted Kremenek4102af92008-03-13 03:04:22 +0000196 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000197
198 // Create an empty block that will serve as the exit block for the CFG. Since
199 // this is the first block added to the CFG, it will be implicitly registered
200 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000201 Succ = createBlock();
202 assert (Succ == &cfg->getExit());
203 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000204
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000205 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000206 CFGBlock* B = addStmt(Statement);
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000207 if (!B) B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000208
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000209 if (B) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000210 // Finalize the last constructed block. This usually involves reversing the
211 // order of the statements in the block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000212 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000213
214 // Backpatch the gotos whose label -> block mappings we didn't know when we
215 // encountered them.
216 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000217 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000218
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000219 CFGBlock* B = *I;
220 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
221 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
222
223 // If there is no target for the goto, then we are looking at an
224 // incomplete AST. Handle this by not registering a successor.
225 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000226
227 B->addSuccessor(LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000228 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000229
Ted Kremenek19bb3562007-08-28 19:26:49 +0000230 // Add successors to the Indirect Goto Dispatch block (if we have one).
231 if (CFGBlock* B = cfg->getIndirectGotoBlock())
232 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
233 E = AddressTakenLabels.end(); I != E; ++I ) {
234
235 // Lookup the target block.
236 LabelMapTy::iterator LI = LabelMap.find(*I);
237
238 // If there is no target block that contains label, then we are looking
239 // at an incomplete AST. Handle this by not registering a successor.
240 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000241
242 B->addSuccessor(LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000243 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000244
Ted Kremenek94b33162007-09-17 16:18:02 +0000245 Succ = B;
Ted Kremenek322f58d2007-09-26 21:23:31 +0000246 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000247
248 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000249 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000250
Ted Kremenek4102af92008-03-13 03:04:22 +0000251 if (badCFG) {
252 delete cfg;
253 cfg = NULL;
254 return NULL;
255 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000256
257 // NULL out cfg so that repeated calls to the builder will fail and that the
258 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000259 CFG* t = cfg;
260 cfg = NULL;
261 return t;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000262}
Mike Stump6d9828c2009-07-17 01:31:16 +0000263
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000264/// createBlock - Used to lazily create blocks that are connected
265/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000266CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000267 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000268 if (add_successor && Succ)
269 B->addSuccessor(Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000270 return B;
271}
Mike Stump6d9828c2009-07-17 01:31:16 +0000272
273/// FinishBlock - When the last statement has been added to the block, we must
274/// reverse the statements because they have been inserted in reverse order.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000275bool CFGBuilder::FinishBlock(CFGBlock* B) {
276 if (badCFG)
277 return false;
278
Ted Kremenek4f880632009-07-17 22:18:43 +0000279 assert(B);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000280 B->reverseStmts();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000281 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000282}
283
Ted Kremenek4f880632009-07-17 22:18:43 +0000284/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000285/// blocks for ternary operators, &&, and ||. We also process "," and
286/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4f880632009-07-17 22:18:43 +0000287CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
288tryAgain:
289 switch (S->getStmtClass()) {
290 default:
291 return VisitStmt(S, alwaysAdd);
292
293 case Stmt::AddrLabelExprClass:
294 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
295
296 case Stmt::BinaryOperatorClass:
297 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
298
299 case Stmt::BlockExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000300 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000301
302 case Stmt::BlockDeclRefExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000303 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000304
305 case Stmt::BreakStmtClass:
306 return VisitBreakStmt(cast<BreakStmt>(S));
307
308 case Stmt::CallExprClass:
309 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
310
311 case Stmt::CaseStmtClass:
312 return VisitCaseStmt(cast<CaseStmt>(S));
313
314 case Stmt::ChooseExprClass:
315 return VisitChooseExpr(cast<ChooseExpr>(S));
316
317 case Stmt::CompoundStmtClass:
318 return VisitCompoundStmt(cast<CompoundStmt>(S));
319
320 case Stmt::ConditionalOperatorClass:
321 return VisitConditionalOperator(cast<ConditionalOperator>(S));
322
323 case Stmt::ContinueStmtClass:
324 return VisitContinueStmt(cast<ContinueStmt>(S));
325
326 case Stmt::DeclStmtClass:
327 return VisitDeclStmt(cast<DeclStmt>(S));
328
329 case Stmt::DefaultStmtClass:
330 return VisitDefaultStmt(cast<DefaultStmt>(S));
331
332 case Stmt::DoStmtClass:
333 return VisitDoStmt(cast<DoStmt>(S));
334
335 case Stmt::ForStmtClass:
336 return VisitForStmt(cast<ForStmt>(S));
337
338 case Stmt::GotoStmtClass:
339 return VisitGotoStmt(cast<GotoStmt>(S));
340
341 case Stmt::IfStmtClass:
342 return VisitIfStmt(cast<IfStmt>(S));
343
344 case Stmt::IndirectGotoStmtClass:
345 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
346
347 case Stmt::LabelStmtClass:
348 return VisitLabelStmt(cast<LabelStmt>(S));
349
350 case Stmt::ObjCAtCatchStmtClass:
351 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
352
Mike Stump0979d802009-07-22 22:56:04 +0000353 case Stmt::CXXThrowExprClass:
354 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
355
Ted Kremenek4f880632009-07-17 22:18:43 +0000356 case Stmt::ObjCAtSynchronizedStmtClass:
357 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
358
359 case Stmt::ObjCAtThrowStmtClass:
360 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
361
362 case Stmt::ObjCAtTryStmtClass:
363 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
364
365 case Stmt::ObjCForCollectionStmtClass:
366 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
367
368 case Stmt::ParenExprClass:
369 S = cast<ParenExpr>(S)->getSubExpr();
370 goto tryAgain;
371
372 case Stmt::NullStmtClass:
373 return Block;
374
375 case Stmt::ReturnStmtClass:
376 return VisitReturnStmt(cast<ReturnStmt>(S));
377
378 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000379 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000380
381 case Stmt::StmtExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000382 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000383
384 case Stmt::SwitchStmtClass:
385 return VisitSwitchStmt(cast<SwitchStmt>(S));
386
387 case Stmt::WhileStmtClass:
388 return VisitWhileStmt(cast<WhileStmt>(S));
389 }
390}
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000391
Ted Kremenek4f880632009-07-17 22:18:43 +0000392CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
393 if (alwaysAdd) {
394 autoCreateBlock();
395 Block->appendStmt(S);
Mike Stump6d9828c2009-07-17 01:31:16 +0000396 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000397
398 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000399}
Mike Stump6d9828c2009-07-17 01:31:16 +0000400
Ted Kremenek4f880632009-07-17 22:18:43 +0000401/// VisitChildren - Visit the children of a Stmt.
402CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
403 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000404 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000405 E = Terminator->child_end(); I != E; ++I) {
406 if (*I) B = Visit(*I);
407 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000408 return B;
409}
Ted Kremenek4f880632009-07-17 22:18:43 +0000410
411CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
412 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000413
Ted Kremenek4f880632009-07-17 22:18:43 +0000414 if (alwaysAdd) {
415 autoCreateBlock();
416 Block->appendStmt(A);
417 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000418
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000419 return Block;
420}
Ted Kremenek4f880632009-07-17 22:18:43 +0000421
422CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
423 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000424 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
425 ConfluenceBlock->appendStmt(B);
426
427 if (!FinishBlock(ConfluenceBlock))
428 return 0;
429
430 // create the block evaluating the LHS
431 CFGBlock* LHSBlock = createBlock(false);
432 LHSBlock->setTerminator(B);
433
434 // create the block evaluating the RHS
435 Succ = ConfluenceBlock;
436 Block = NULL;
437 CFGBlock* RHSBlock = addStmt(B->getRHS());
438 if (!FinishBlock(RHSBlock))
439 return 0;
440
Mike Stump00998a02009-07-23 23:25:26 +0000441 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000442 TryResult KnownVal = TryEvaluateBool(B->getLHS());
443 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
444 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000445
Ted Kremenek4f880632009-07-17 22:18:43 +0000446 // Now link the LHSBlock with RHSBlock.
447 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek941fde82009-07-24 04:47:11 +0000448 LHSBlock->addSuccessor(KnownVal.isTrue() ? NULL : ConfluenceBlock);
449 LHSBlock->addSuccessor(KnownVal.isFalse() ? NULL : RHSBlock);
450 } else {
Ted Kremenek4f880632009-07-17 22:18:43 +0000451 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek941fde82009-07-24 04:47:11 +0000452 LHSBlock->addSuccessor(KnownVal.isFalse() ? NULL : RHSBlock);
453 LHSBlock->addSuccessor(KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000454 }
455
456 // Generate the blocks for evaluating the LHS.
457 Block = LHSBlock;
458 return addStmt(B->getLHS());
459 }
460 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000461 autoCreateBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000462 Block->appendStmt(B);
463 addStmt(B->getRHS());
464 return addStmt(B->getLHS());
465 }
466
467 return VisitStmt(B, alwaysAdd);
468}
469
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000470CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E, bool alwaysAdd) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000471 // FIXME
472 return NYS();
473}
474
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000475CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
476 bool alwaysAdd) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000477 // FIXME
478 return NYS();
479}
480
481CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
482 // "break" is a control-flow statement. Thus we stop processing the current
483 // block.
484 if (Block && !FinishBlock(Block))
485 return 0;
486
487 // Now create a new block that ends with the break statement.
488 Block = createBlock(false);
489 Block->setTerminator(B);
490
491 // If there is no target for the break, then we are looking at an incomplete
492 // AST. This means that the CFG cannot be constructed.
493 if (BreakTargetBlock)
494 Block->addSuccessor(BreakTargetBlock);
495 else
496 badCFG = true;
497
498
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000499 return Block;
500}
Ted Kremenek4f880632009-07-17 22:18:43 +0000501
502CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
503 // If this is a call to a no-return function, this stops the block here.
504 if (FunctionDecl *FD = C->getDirectCallee()) {
505
506 if (!FD->hasAttr<NoReturnAttr>())
507 return VisitStmt(C, alwaysAdd);
508
509 if (Block && !FinishBlock(Block))
510 return 0;
511
512 // Create new block with no successor for the remaining pieces.
513 Block = createBlock(false);
514 Block->appendStmt(C);
515
516 // Wire this to the exit block directly.
517 Block->addSuccessor(&cfg->getExit());
518
519 return VisitChildren(C);
520 }
521
522 return VisitStmt(C, alwaysAdd);
523}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000524
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000525CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
526 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
527 ConfluenceBlock->appendStmt(C);
528 if (!FinishBlock(ConfluenceBlock))
529 return 0;
530
531 Succ = ConfluenceBlock;
532 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000533 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000534 if (!FinishBlock(LHSBlock))
535 return 0;
536
537 Succ = ConfluenceBlock;
538 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000539 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000540 if (!FinishBlock(RHSBlock))
541 return 0;
542
543 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000544 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000545 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
546 Block->addSuccessor(KnownVal.isFalse() ? NULL : LHSBlock);
547 Block->addSuccessor(KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000548 Block->setTerminator(C);
549 return addStmt(C->getCond());
550}
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000551
Ted Kremenek4f880632009-07-17 22:18:43 +0000552
553CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
554 CFGBlock* LastBlock = Block;
555
556 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
557 I != E; ++I ) {
558 LastBlock = addStmt(*I);
559 }
560 return LastBlock;
561}
562
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000563CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
564 // Create the confluence block that will "merge" the results of the ternary
565 // expression.
566 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
567 ConfluenceBlock->appendStmt(C);
568 if (!FinishBlock(ConfluenceBlock))
569 return 0;
570
571 // Create a block for the LHS expression if there is an LHS expression. A
572 // GCC extension allows LHS to be NULL, causing the condition to be the
573 // value that is returned instead.
574 // e.g: x ?: y is shorthand for: x ? x : y;
575 Succ = ConfluenceBlock;
576 Block = NULL;
577 CFGBlock* LHSBlock = NULL;
578 if (C->getLHS()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000579 LHSBlock = addStmt(C->getLHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000580 if (!FinishBlock(LHSBlock))
581 return 0;
582 Block = NULL;
583 }
584
585 // Create the block for the RHS expression.
586 Succ = ConfluenceBlock;
Ted Kremenek4f880632009-07-17 22:18:43 +0000587 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000588 if (!FinishBlock(RHSBlock))
589 return 0;
590
591 // Create the block that will contain the condition.
592 Block = createBlock(false);
593
Mike Stump00998a02009-07-23 23:25:26 +0000594 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000595 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000596 if (LHSBlock) {
Ted Kremenek941fde82009-07-24 04:47:11 +0000597 Block->addSuccessor(KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000598 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000599 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000600 // If we know the condition is false, add NULL as the successor for
601 // the block containing the condition. In this case, the confluence
602 // block will have just one predecessor.
603 Block->addSuccessor(0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000604 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000605 } else {
606 // If we have no LHS expression, add the ConfluenceBlock as a direct
607 // successor for the block containing the condition. Moreover, we need to
608 // reverse the order of the predecessors in the ConfluenceBlock because
609 // the RHSBlock will have been added to the succcessors already, and we
610 // want the first predecessor to the the block containing the expression
611 // for the case when the ternary expression evaluates to true.
612 Block->addSuccessor(ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000613 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000614 std::reverse(ConfluenceBlock->pred_begin(),
615 ConfluenceBlock->pred_end());
616 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000617 }
618
Ted Kremenek941fde82009-07-24 04:47:11 +0000619 Block->addSuccessor(KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000620 Block->setTerminator(C);
621 return addStmt(C->getCond());
622}
623
Ted Kremenek4f880632009-07-17 22:18:43 +0000624CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
625 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000626
Ted Kremenek4f880632009-07-17 22:18:43 +0000627 if (DS->isSingleDecl()) {
628 Block->appendStmt(DS);
629 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000630 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000631
632 CFGBlock *B = 0;
633
634 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
635 typedef llvm::SmallVector<Decl*,10> BufTy;
636 BufTy Buf(DS->decl_begin(), DS->decl_end());
637
638 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
639 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
640 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
641 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
642
643 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
644 // automatically freed with the CFG.
645 DeclGroupRef DG(*I);
646 Decl *D = *I;
647 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
648 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
649
650 // Append the fake DeclStmt to block.
651 Block->appendStmt(DSNew);
652 B = VisitDeclSubExpr(D);
653 }
654
655 return B;
656}
657
658/// VisitDeclSubExpr - Utility method to add block-level expressions for
659/// initializers in Decls.
660CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
661 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000662
Ted Kremenek4f880632009-07-17 22:18:43 +0000663 VarDecl *VD = dyn_cast<VarDecl>(D);
664
665 if (!VD)
666 return Block;
667
668 Expr *Init = VD->getInit();
669
670 if (Init) {
671 // Optimization: Don't create separate block-level statements for literals.
672 switch (Init->getStmtClass()) {
673 case Stmt::IntegerLiteralClass:
674 case Stmt::CharacterLiteralClass:
675 case Stmt::StringLiteralClass:
676 break;
677 default:
678 Block = addStmt(Init);
679 }
680 }
681
682 // If the type of VD is a VLA, then we must process its size expressions.
683 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
684 VA = FindVA(VA->getElementType().getTypePtr()))
685 Block = addStmt(VA->getSizeExpr());
686
687 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000688}
689
690CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000691 // We may see an if statement in the middle of a basic block, or it may be the
692 // first statement we are processing. In either case, we create a new basic
693 // block. First, we create the blocks for the then...else statements, and
694 // then we create the block containing the if statement. If we were in the
695 // middle of a block, we stop processing that block and reverse its
696 // statements. That block is then the implicit successor for the "then" and
697 // "else" clauses.
698
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);
740 ThenBlock->addSuccessor(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 Kremenek941fde82009-07-24 04:47:11 +0000757 Block->addSuccessor(KnownVal.isFalse() ? NULL : ThenBlock);
758 Block->addSuccessor(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) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000768 // If we were in the middle of a block we stop processing that block and
769 // reverse its statements.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000770 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000771 // NOTE: If a "return" appears in the middle of a block, this means that the
772 // code afterwards is DEAD (unreachable). We still keep a basic block
773 // for that code; a simple "mark-and-sweep" from the entry block will be
774 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000775 if (Block) FinishBlock(Block);
776
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.
781 Block->addSuccessor(&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
832 Block->addSuccessor(I->second);
833
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);
875
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)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000924 BodyBlock = EntryConditionBlock; // 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.
929 ExitConditionBlock->addSuccessor(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).
934 ExitConditionBlock->addSuccessor(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 Kremenek4cb3a852008-11-14 01:57:41 +00001003 ExitConditionBlock->appendStmt(S);
1004 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.
1039 ExitConditionBlock->addSuccessor(BodyBlock);
1040 }
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).
1044 ExitConditionBlock->addSuccessor(LoopSuccessor);
1045
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)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001145 BodyBlock = EntryConditionBlock; // 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.
1152 ExitConditionBlock->addSuccessor(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).
1157 ExitConditionBlock->addSuccessor(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}
Ted Kremenek4f880632009-07-17 22:18:43 +00001167
1168
1169CFGBlock *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
1179 // If we were in the middle of a block we stop processing that block and
1180 // reverse its statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001181 if (Block && !FinishBlock(Block))
1182 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001183
Ted Kremenek2fda5042008-12-09 20:20:09 +00001184 // Create the new block.
1185 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001186
Ted Kremenek2fda5042008-12-09 20:20:09 +00001187 // The Exit block is the only successor.
1188 Block->addSuccessor(&cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001189
1190 // Add the statement to the block. This may create new blocks if S contains
1191 // control-flow (short-circuit operations).
Ted Kremenek4f880632009-07-17 22:18:43 +00001192 return VisitStmt(S, true);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001193}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001194
Mike Stump0979d802009-07-22 22:56:04 +00001195CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
1196 // If we were in the middle of a block we stop processing that block and
1197 // reverse its statements.
1198 if (Block && !FinishBlock(Block))
1199 return 0;
1200
1201 // Create the new block.
1202 Block = createBlock(false);
1203
1204 // The Exit block is the only successor.
1205 Block->addSuccessor(&cfg->getExit());
1206
1207 // Add the statement to the block. This may create new blocks if S contains
1208 // control-flow (short-circuit operations).
1209 return VisitStmt(T, true);
1210}
1211
Ted Kremenek4f880632009-07-17 22:18:43 +00001212CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001213 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001214
Mike Stump8f9893a2009-07-21 01:27:50 +00001215 // "do...while" is a control-flow statement. Thus we stop processing the
1216 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001217 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001218 if (!FinishBlock(Block))
1219 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001220 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001221 } else
1222 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001223
1224 // Because of short-circuit evaluation, the condition of the loop can span
1225 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1226 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001227 CFGBlock* ExitConditionBlock = createBlock(false);
1228 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001229
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001230 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001231 ExitConditionBlock->setTerminator(D);
1232
1233 // Now add the actual condition to the condition block. Because the condition
1234 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001235 if (Stmt* C = D->getCond()) {
1236 Block = ExitConditionBlock;
1237 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001238 if (Block) {
1239 if (!FinishBlock(EntryConditionBlock))
1240 return 0;
1241 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001242 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001243
Ted Kremenek54827132008-02-27 07:20:00 +00001244 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001245 Succ = EntryConditionBlock;
1246
Mike Stump00998a02009-07-23 23:25:26 +00001247 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001248 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001249
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001250 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001251 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001252 {
1253 assert (D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001254
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001255 // Save the current values for Block, Succ, and continue and break targets
1256 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1257 save_continue(ContinueTargetBlock),
1258 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001259
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001260 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001261 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001262
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001263 // All breaks should go to the code following the loop.
1264 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001265
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001266 // NULL out Block to force lazy instantiation of blocks for the body.
1267 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001268
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001269 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001270 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001271
Ted Kremenekaf603f72007-08-30 18:39:40 +00001272 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001273 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001274 else if (Block) {
1275 if (!FinishBlock(BodyBlock))
1276 return 0;
1277 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001278
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001279 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001280 // ExitConditionBlock to represent the "loop back" transition. Create an
1281 // empty block to represent the transition block for looping back to the
1282 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001283 // FIXME: Can we do this more efficiently without adding another block?
1284 Block = NULL;
1285 Succ = BodyBlock;
1286 CFGBlock *LoopBackBlock = createBlock();
1287 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001288
Ted Kremenek941fde82009-07-24 04:47:11 +00001289 // Add the loop body entry as a successor to the condition.
1290 ExitConditionBlock->addSuccessor(KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001291 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001292
Ted Kremenek941fde82009-07-24 04:47:11 +00001293 // Link up the condition block with the code that follows the loop.
1294 // (the false branch).
1295 ExitConditionBlock->addSuccessor(KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001296
1297 // There can be no more statements in the body block(s) since we loop back to
1298 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001299 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001300
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001301 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001302 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001303 return BodyBlock;
1304}
1305
1306CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1307 // "continue" is a control-flow statement. Thus we stop processing the
1308 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001309 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001310 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001311
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001312 // Now create a new block that ends with the continue statement.
1313 Block = createBlock(false);
1314 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001315
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001316 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001317 // incomplete AST. This means the CFG cannot be constructed.
1318 if (ContinueTargetBlock)
1319 Block->addSuccessor(ContinueTargetBlock);
1320 else
1321 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001322
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001323 return Block;
1324}
Ted Kremenek4f880632009-07-17 22:18:43 +00001325
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001326CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1327 bool alwaysAdd) {
1328
1329 if (alwaysAdd) {
1330 autoCreateBlock();
1331 Block->appendStmt(E);
1332 }
1333
Ted Kremenek4f880632009-07-17 22:18:43 +00001334 // VLA types have expressions that must be evaluated.
1335 if (E->isArgumentType()) {
1336 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1337 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1338 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001339 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001340
Mike Stump6d9828c2009-07-17 01:31:16 +00001341 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001342}
Ted Kremenek4f880632009-07-17 22:18:43 +00001343
1344/// VisitStmtExpr - Utility method to handle (nested) statement
1345/// expressions (a GCC extension).
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001346CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1347 if (alwaysAdd) {
1348 autoCreateBlock();
1349 Block->appendStmt(SE);
1350 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001351 return VisitCompoundStmt(SE->getSubStmt());
1352}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001353
Ted Kremenek411cdee2008-04-16 21:10:48 +00001354CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001355 // "switch" is a control-flow statement. Thus we stop processing the current
1356 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001357 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001358
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001359 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001360 if (!FinishBlock(Block))
1361 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001362 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001363 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001364
1365 // Save the current "switch" context.
1366 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001367 save_break(BreakTargetBlock),
1368 save_default(DefaultCaseBlock);
1369
Mike Stump6d9828c2009-07-17 01:31:16 +00001370 // Set the "default" case to be the block after the switch statement. If the
1371 // switch statement contains a "default:", this value will be overwritten with
1372 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001373 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001374
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001375 // Create a new block that will contain the switch statement.
1376 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001377
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001378 // Now process the switch body. The code after the switch is the implicit
1379 // successor.
1380 Succ = SwitchSuccessor;
1381 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001382
1383 // When visiting the body, the case statements should automatically get linked
1384 // up to the switch. We also don't keep a pointer to the body, since all
1385 // control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001386 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001387 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001388 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001389 if (Block) {
1390 if (!FinishBlock(BodyBlock))
1391 return 0;
1392 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001393
Mike Stump6d9828c2009-07-17 01:31:16 +00001394 // If we have no "default:" case, the default transition is to the code
1395 // following the switch body.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001396 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001397
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001398 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001399 SwitchTerminatedBlock->setTerminator(Terminator);
1400 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001401 Block = SwitchTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001402
Ted Kremenek411cdee2008-04-16 21:10:48 +00001403 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001404}
1405
Ted Kremenek4f880632009-07-17 22:18:43 +00001406CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001407 // CaseStmts are essentially labels, so they are the first statement in a
1408 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001409
Ted Kremenek4f880632009-07-17 22:18:43 +00001410 if (CS->getSubStmt())
1411 addStmt(CS->getSubStmt());
1412
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001413 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001414 if (!CaseBlock)
1415 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001416
1417 // Cases statements partition blocks, so this is the top of the basic block we
1418 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001419 CaseBlock->setLabel(CS);
1420
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001421 if (!FinishBlock(CaseBlock))
1422 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001423
1424 // Add this block to the list of successors for the block with the switch
1425 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001426 assert(SwitchTerminatedBlock);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001427 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001428
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001429 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1430 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001431
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001432 // This block is now the implicit successor of other blocks.
1433 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001434
Ted Kremenek2677ea82008-03-15 07:45:02 +00001435 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001436}
Mike Stump6d9828c2009-07-17 01:31:16 +00001437
Ted Kremenek411cdee2008-04-16 21:10:48 +00001438CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001439 if (Terminator->getSubStmt())
1440 addStmt(Terminator->getSubStmt());
1441
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001442 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001443
1444 if (!DefaultCaseBlock)
1445 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001446
1447 // Default statements partition blocks, so this is the top of the basic block
1448 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001449 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek4f880632009-07-17 22:18:43 +00001450
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001451 if (!FinishBlock(DefaultCaseBlock))
1452 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001453
Mike Stump6d9828c2009-07-17 01:31:16 +00001454 // Unlike case statements, we don't add the default block to the successors
1455 // for the switch statement immediately. This is done when we finish
1456 // processing the switch statement. This allows for the default case
1457 // (including a fall-through to the code after the switch statement) to always
1458 // be the last successor of a switch-terminated block.
1459
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001460 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1461 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001462
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001463 // This block is now the implicit successor of other blocks.
1464 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001465
1466 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001467}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001468
Ted Kremenek19bb3562007-08-28 19:26:49 +00001469CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001470 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001471 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001472
Ted Kremenek19bb3562007-08-28 19:26:49 +00001473 if (!IBlock) {
1474 IBlock = createBlock(false);
1475 cfg->setIndirectGotoBlock(IBlock);
1476 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001477
Ted Kremenek19bb3562007-08-28 19:26:49 +00001478 // IndirectGoto is a control-flow statement. Thus we stop processing the
1479 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001480 if (Block && !FinishBlock(Block))
1481 return 0;
1482
Ted Kremenek19bb3562007-08-28 19:26:49 +00001483 Block = createBlock(false);
1484 Block->setTerminator(I);
1485 Block->addSuccessor(IBlock);
1486 return addStmt(I->getTarget());
1487}
1488
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001489} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001490
Mike Stump6d9828c2009-07-17 01:31:16 +00001491/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1492/// no successors or predecessors. If this is the first block created in the
1493/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001494CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001495 bool first_block = begin() == end();
1496
1497 // Create the block.
Ted Kremenek94382522007-09-05 20:02:05 +00001498 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek026473c2007-08-23 16:51:22 +00001499
1500 // If this is the first block, set it as the Entry and Exit.
1501 if (first_block) Entry = Exit = &front();
1502
1503 // Return the block.
1504 return &front();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001505}
1506
Ted Kremenek026473c2007-08-23 16:51:22 +00001507/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1508/// CFG is returned to the caller.
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001509CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001510 CFGBuilder Builder;
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001511 return Builder.buildCFG(Statement, C);
Ted Kremenek026473c2007-08-23 16:51:22 +00001512}
1513
1514/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001515void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1516
Ted Kremenek63f58872007-10-01 19:33:33 +00001517//===----------------------------------------------------------------------===//
1518// CFG: Queries for BlkExprs.
1519//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001520
Ted Kremenek63f58872007-10-01 19:33:33 +00001521namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001522 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001523}
1524
Ted Kremenek411cdee2008-04-16 21:10:48 +00001525static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1526 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001527 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001528
Ted Kremenek411cdee2008-04-16 21:10:48 +00001529 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001530 if (!*I) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001531
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001532 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1533 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001534
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001535 FindSubExprAssignments(*I, Set);
1536 }
1537}
1538
Ted Kremenek63f58872007-10-01 19:33:33 +00001539static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1540 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001541
1542 // Look for assignments that are used as subexpressions. These are the only
1543 // assignments that we want to *possibly* register as a block-level
1544 // expression. Basically, if an assignment occurs both in a subexpression and
1545 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001546 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001547
Ted Kremenek63f58872007-10-01 19:33:33 +00001548 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1549 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001550 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001551
Ted Kremenek411cdee2008-04-16 21:10:48 +00001552 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001553
1554 // Iterate over the statements again on identify the Expr* and Stmt* at the
1555 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001556
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001557 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001558 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001559
Ted Kremenek411cdee2008-04-16 21:10:48 +00001560 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001561 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001562 // expression are really "statements" whose value is never used by
1563 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001564 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001565 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001566 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1567 // Special handling for statement expressions. The last statement in
1568 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001569 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001570 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001571 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001572 (*M)[C->body_back()] = x;
1573 }
1574 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001575
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001576 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001577 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001578 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001579
Ted Kremenek411cdee2008-04-16 21:10:48 +00001580 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001581
Ted Kremenek390e48b2008-11-12 21:11:49 +00001582 Stmt* S = I->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001583
Ted Kremenek390e48b2008-11-12 21:11:49 +00001584 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001585 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001586 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001587 }
1588 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001589
Ted Kremenek63f58872007-10-01 19:33:33 +00001590 return M;
1591}
1592
Ted Kremenek86946742008-01-17 20:48:37 +00001593CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1594 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001595 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001596
Ted Kremenek63f58872007-10-01 19:33:33 +00001597 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001598 BlkExprMapTy::iterator I = M->find(S);
Mike Stump6d9828c2009-07-17 01:31:16 +00001599
Ted Kremenek63f58872007-10-01 19:33:33 +00001600 if (I == M->end()) return CFG::BlkExprNumTy();
1601 else return CFG::BlkExprNumTy(I->second);
1602}
1603
1604unsigned CFG::getNumBlkExprs() {
1605 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1606 return M->size();
1607 else {
1608 // We assume callers interested in the number of BlkExprs will want
1609 // the map constructed if it doesn't already exist.
1610 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1611 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1612 }
1613}
1614
Ted Kremenek274f4332008-04-28 18:00:46 +00001615//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001616// Cleanup: CFG dstor.
1617//===----------------------------------------------------------------------===//
1618
Ted Kremenek63f58872007-10-01 19:33:33 +00001619CFG::~CFG() {
1620 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1621}
Mike Stump6d9828c2009-07-17 01:31:16 +00001622
Ted Kremenek7dba8602007-08-29 21:56:09 +00001623//===----------------------------------------------------------------------===//
1624// CFG pretty printing
1625//===----------------------------------------------------------------------===//
1626
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001627namespace {
1628
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001629class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump6d9828c2009-07-17 01:31:16 +00001630
Ted Kremenek42a509f2007-08-31 21:30:12 +00001631 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1632 StmtMapTy StmtMap;
1633 signed CurrentBlock;
1634 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001635 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001636public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001637
Chris Lattnere4f21422009-06-30 01:26:17 +00001638 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1639 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001640 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1641 unsigned j = 1;
1642 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1643 BI != BEnd; ++BI, ++j )
1644 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1645 }
1646 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001647
Ted Kremenek42a509f2007-08-31 21:30:12 +00001648 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001649
Chris Lattnere4f21422009-06-30 01:26:17 +00001650 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001651 void setBlockID(signed i) { CurrentBlock = i; }
1652 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001653
Ted Kremeneka95d3752008-09-13 05:16:45 +00001654 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001655
Ted Kremenek411cdee2008-04-16 21:10:48 +00001656 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001657
1658 if (I == StmtMap.end())
1659 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001660
1661 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek42a509f2007-08-31 21:30:12 +00001662 && I->second.second == CurrentStmt)
1663 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001664
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001665 OS << "[B" << I->second.first << "." << I->second.second << "]";
1666 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001667 }
1668};
Chris Lattnere4f21422009-06-30 01:26:17 +00001669} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001670
Chris Lattnere4f21422009-06-30 01:26:17 +00001671
1672namespace {
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001673class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1674 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001675
Ted Kremeneka95d3752008-09-13 05:16:45 +00001676 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001677 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001678 PrintingPolicy Policy;
1679
Ted Kremenek42a509f2007-08-31 21:30:12 +00001680public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001681 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001682 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001683 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001684
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001685 void VisitIfStmt(IfStmt* I) {
1686 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001687 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001688 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001689
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001690 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001691 void VisitStmt(Stmt* Terminator) {
1692 Terminator->printPretty(OS, Helper, Policy);
1693 }
1694
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001695 void VisitForStmt(ForStmt* F) {
1696 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001697 if (F->getInit()) OS << "...";
1698 OS << "; ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001699 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001700 OS << "; ";
1701 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001702 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001703 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001704
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001705 void VisitWhileStmt(WhileStmt* W) {
1706 OS << "while " ;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001707 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001708 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001709
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001710 void VisitDoStmt(DoStmt* D) {
1711 OS << "do ... while ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001712 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001713 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001714
Ted Kremenek411cdee2008-04-16 21:10:48 +00001715 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001716 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001717 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001718 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001719
Ted Kremenek805e9a82007-08-31 21:49:40 +00001720 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001721 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001722 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001723 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001724
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001725 void VisitChooseExpr(ChooseExpr* C) {
1726 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001727 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001728 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001729 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001730
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001731 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1732 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001733 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001734 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001735
Ted Kremenek805e9a82007-08-31 21:49:40 +00001736 void VisitBinaryOperator(BinaryOperator* B) {
1737 if (!B->isLogicalOp()) {
1738 VisitExpr(B);
1739 return;
1740 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001741
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001742 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001743
Ted Kremenek805e9a82007-08-31 21:49:40 +00001744 switch (B->getOpcode()) {
1745 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001746 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001747 return;
1748 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001749 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001750 return;
1751 default:
1752 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001753 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00001754 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001755
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001756 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001757 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001758 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001759};
Chris Lattnere4f21422009-06-30 01:26:17 +00001760} // end anonymous namespace
1761
Mike Stump6d9828c2009-07-17 01:31:16 +00001762
Chris Lattnere4f21422009-06-30 01:26:17 +00001763static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1764 Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001765 if (Helper) {
1766 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001767 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001768 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00001769
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001770 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001771 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001772 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001773 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001774 return;
1775 }
1776 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001777
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001778 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001779 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001780 if (B->getOpcode() == BinaryOperator::Comma) {
1781 OS << "... , ";
1782 Helper->handledStmt(B->getRHS(),OS);
1783 OS << '\n';
1784 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001785 }
1786 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001787 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001788
Chris Lattnere4f21422009-06-30 01:26:17 +00001789 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00001790
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001791 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001792 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001793}
Mike Stump6d9828c2009-07-17 01:31:16 +00001794
Chris Lattnere4f21422009-06-30 01:26:17 +00001795static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1796 const CFGBlock& B,
1797 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001798
Ted Kremenek42a509f2007-08-31 21:30:12 +00001799 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00001800
Ted Kremenek7dba8602007-08-29 21:56:09 +00001801 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00001802 OS << "\n [ B" << B.getBlockID();
1803
Ted Kremenek42a509f2007-08-31 21:30:12 +00001804 if (&B == &cfg->getEntry())
1805 OS << " (ENTRY) ]\n";
1806 else if (&B == &cfg->getExit())
1807 OS << " (EXIT) ]\n";
1808 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001809 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001810 else
1811 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00001812
Ted Kremenek9cffe732007-08-29 23:20:49 +00001813 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001814 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001815
1816 if (print_edges)
1817 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001818
Ted Kremenek411cdee2008-04-16 21:10:48 +00001819 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001820 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001821 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001822 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001823 C->getLHS()->printPretty(OS, Helper,
1824 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001825 if (C->getRHS()) {
1826 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001827 C->getRHS()->printPretty(OS, Helper,
1828 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001829 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001830 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001831 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001832 else
1833 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001834
Ted Kremenek9cffe732007-08-29 23:20:49 +00001835 OS << ":\n";
1836 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001837
Ted Kremenekfddd5182007-08-21 21:42:03 +00001838 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001839 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00001840
Ted Kremenek42a509f2007-08-31 21:30:12 +00001841 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1842 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001843
Ted Kremenek9cffe732007-08-29 23:20:49 +00001844 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001845 if (print_edges)
1846 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001847
Ted Kremeneka95d3752008-09-13 05:16:45 +00001848 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001849
Ted Kremenek42a509f2007-08-31 21:30:12 +00001850 if (Helper)
1851 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00001852
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001853 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001854 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001855
Ted Kremenek9cffe732007-08-29 23:20:49 +00001856 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001857 if (B.getTerminator()) {
1858 if (print_edges)
1859 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001860
Ted Kremenek9cffe732007-08-29 23:20:49 +00001861 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001862
Ted Kremenek42a509f2007-08-31 21:30:12 +00001863 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00001864
Chris Lattnere4f21422009-06-30 01:26:17 +00001865 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1866 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00001867 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001868 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001869 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001870
Ted Kremenek9cffe732007-08-29 23:20:49 +00001871 if (print_edges) {
1872 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001873 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001874 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001875
Ted Kremenek42a509f2007-08-31 21:30:12 +00001876 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1877 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001878
Ted Kremenek42a509f2007-08-31 21:30:12 +00001879 if (i == 8 || (i-8) == 0)
1880 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001881
Ted Kremenek9cffe732007-08-29 23:20:49 +00001882 OS << " B" << (*I)->getBlockID();
1883 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001884
Ted Kremenek42a509f2007-08-31 21:30:12 +00001885 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00001886
Ted Kremenek42a509f2007-08-31 21:30:12 +00001887 // Print the successors of this block.
1888 OS << " Successors (" << B.succ_size() << "):";
1889 i = 0;
1890
1891 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1892 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001893
Ted Kremenek42a509f2007-08-31 21:30:12 +00001894 if (i == 8 || (i-8) % 10 == 0)
1895 OS << "\n ";
1896
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001897 if (*I)
1898 OS << " B" << (*I)->getBlockID();
1899 else
1900 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001901 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001902
Ted Kremenek9cffe732007-08-29 23:20:49 +00001903 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001904 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001905}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001906
Ted Kremenek42a509f2007-08-31 21:30:12 +00001907
1908/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001909void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001910
1911/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00001912void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1913 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00001914
Ted Kremenek42a509f2007-08-31 21:30:12 +00001915 // Print the entry block.
1916 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00001917
Ted Kremenek42a509f2007-08-31 21:30:12 +00001918 // Iterate through the CFGBlocks and print them one by one.
1919 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1920 // Skip the entry block, because we already printed it.
1921 if (&(*I) == &getEntry() || &(*I) == &getExit())
1922 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001923
Ted Kremenek42a509f2007-08-31 21:30:12 +00001924 print_block(OS, this, *I, &Helper, true);
1925 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001926
Ted Kremenek42a509f2007-08-31 21:30:12 +00001927 // Print the exit block.
1928 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00001929 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00001930}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001931
1932/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001933void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1934 print(llvm::errs(), cfg, LO);
1935}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001936
1937/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1938/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00001939void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1940 const LangOptions &LO) const {
1941 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001942 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00001943}
Ted Kremenek7dba8602007-08-29 21:56:09 +00001944
Ted Kremeneka2925852008-01-30 23:02:42 +00001945/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00001946void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00001947 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00001948 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00001949 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1950}
1951
Ted Kremenek390e48b2008-11-12 21:11:49 +00001952Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00001953
Ted Kremenek411cdee2008-04-16 21:10:48 +00001954 if (!Terminator)
1955 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001956
Ted Kremenek411cdee2008-04-16 21:10:48 +00001957 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001958
Ted Kremenek411cdee2008-04-16 21:10:48 +00001959 switch (Terminator->getStmtClass()) {
1960 default:
1961 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001962
Ted Kremenek411cdee2008-04-16 21:10:48 +00001963 case Stmt::ForStmtClass:
1964 E = cast<ForStmt>(Terminator)->getCond();
1965 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001966
Ted Kremenek411cdee2008-04-16 21:10:48 +00001967 case Stmt::WhileStmtClass:
1968 E = cast<WhileStmt>(Terminator)->getCond();
1969 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001970
Ted Kremenek411cdee2008-04-16 21:10:48 +00001971 case Stmt::DoStmtClass:
1972 E = cast<DoStmt>(Terminator)->getCond();
1973 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001974
Ted Kremenek411cdee2008-04-16 21:10:48 +00001975 case Stmt::IfStmtClass:
1976 E = cast<IfStmt>(Terminator)->getCond();
1977 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001978
Ted Kremenek411cdee2008-04-16 21:10:48 +00001979 case Stmt::ChooseExprClass:
1980 E = cast<ChooseExpr>(Terminator)->getCond();
1981 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001982
Ted Kremenek411cdee2008-04-16 21:10:48 +00001983 case Stmt::IndirectGotoStmtClass:
1984 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1985 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001986
Ted Kremenek411cdee2008-04-16 21:10:48 +00001987 case Stmt::SwitchStmtClass:
1988 E = cast<SwitchStmt>(Terminator)->getCond();
1989 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001990
Ted Kremenek411cdee2008-04-16 21:10:48 +00001991 case Stmt::ConditionalOperatorClass:
1992 E = cast<ConditionalOperator>(Terminator)->getCond();
1993 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001994
Ted Kremenek411cdee2008-04-16 21:10:48 +00001995 case Stmt::BinaryOperatorClass: // '&&' and '||'
1996 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001997 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001998
Ted Kremenek390e48b2008-11-12 21:11:49 +00001999 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002000 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002001 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002002
Ted Kremenek411cdee2008-04-16 21:10:48 +00002003 return E ? E->IgnoreParens() : NULL;
2004}
2005
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002006bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002007
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002008 if (!Terminator)
2009 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002010
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002011 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002012
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002013 switch (Terminator->getStmtClass()) {
2014 default:
2015 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002016
2017 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002018 case Stmt::WhileStmtClass:
2019 case Stmt::DoStmtClass:
2020 case Stmt::IfStmtClass:
2021 case Stmt::ChooseExprClass:
2022 case Stmt::ConditionalOperatorClass:
2023 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002024 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002025 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002026
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002027 return E ? E->IgnoreParens() : NULL;
2028}
2029
Ted Kremeneka2925852008-01-30 23:02:42 +00002030
Ted Kremenek7dba8602007-08-29 21:56:09 +00002031//===----------------------------------------------------------------------===//
2032// CFG Graphviz Visualization
2033//===----------------------------------------------------------------------===//
2034
Ted Kremenek42a509f2007-08-31 21:30:12 +00002035
2036#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002037static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002038#endif
2039
Chris Lattnere4f21422009-06-30 01:26:17 +00002040void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002041#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002042 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002043 GraphHelper = &H;
2044 llvm::ViewGraph(this,"CFG");
2045 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002046#endif
2047}
2048
Ted Kremenek7dba8602007-08-29 21:56:09 +00002049namespace llvm {
2050template<>
2051struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson02995ce2009-06-24 17:37:55 +00002052 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
2053 bool ShortNames) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002054
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002055#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002056 std::string OutSStr;
2057 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002058 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002059 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002060
2061 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2062
2063 // Process string output to make it nicer...
2064 for (unsigned i = 0; i != OutStr.length(); ++i)
2065 if (OutStr[i] == '\n') { // Left justify
2066 OutStr[i] = '\\';
2067 OutStr.insert(OutStr.begin()+i+1, 'l');
2068 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002069
Ted Kremenek7dba8602007-08-29 21:56:09 +00002070 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002071#else
2072 return "";
2073#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002074 }
2075};
2076} // end namespace llvm