blob: 79ca8114a7bc7b725c10c009640ff01f9aa89ac7 [file] [log] [blame]
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek6796fbd2009-07-16 18:13:04 +000015#include "clang/Analysis/CFG.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000016#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000017#include "clang/AST/PrettyPrinter.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000018#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000019#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4e5f99d2007-08-29 21:56:09 +000020#include "llvm/Support/GraphWriter.h"
Ted Kremeneka59e0062007-12-17 19:35:20 +000021#include "llvm/Support/Streams.h"
Ted Kremenek83ebcef2008-01-08 18:15:10 +000022#include "llvm/Support/Compiler.h"
Ted Kremenek6065ef62008-04-28 18:00:46 +000023#include <llvm/Support/Allocator.h>
Ted Kremenek2d470fc2008-09-13 05:16:45 +000024#include <llvm/Support/Format.h>
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000025
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000026using namespace clang;
27
28namespace {
29
Ted Kremenek04cca642007-08-23 21:26:19 +000030// SaveAndRestore - A utility class that uses RIIA to save and restore
31// the value of a variable.
32template<typename T>
Ted Kremenek83ebcef2008-01-08 18:15:10 +000033struct VISIBILITY_HIDDEN SaveAndRestore {
Ted Kremenek04cca642007-08-23 21:26:19 +000034 SaveAndRestore(T& x) : X(x), old_value(x) {}
35 ~SaveAndRestore() { X = old_value; }
Ted Kremenekbbad8ce2007-08-30 18:13:31 +000036 T get() { return old_value; }
37
Ted Kremenek04cca642007-08-23 21:26:19 +000038 T& X;
39 T old_value;
40};
Mike Stump31feda52009-07-17 01:31:16 +000041
Douglas Gregor6e6ad602009-01-20 01:17:11 +000042static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000043 if (VarDecl* VD = dyn_cast<VarDecl>(D))
44 if (Expr* Ex = VD->getInit())
45 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000046
47 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000048}
Mike Stump31feda52009-07-17 01:31:16 +000049
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000050/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000051/// The builder is stateful: an instance of the builder should be used to only
52/// construct a single CFG.
53///
54/// Example usage:
55///
56/// CFGBuilder builder;
57/// CFG* cfg = builder.BuildAST(stmt1);
58///
Mike Stump31feda52009-07-17 01:31:16 +000059/// CFG construction is done via a recursive walk of an AST. We actually parse
60/// the AST in reverse order so that the successor of a basic block is
61/// constructed prior to its predecessor. This allows us to nicely capture
62/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000063///
Ted Kremenek93668002009-07-17 22:18:43 +000064class VISIBILITY_HIDDEN CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000065 ASTContext *Context;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000066 CFG* cfg;
67 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000068 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000069 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000070 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000071 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000072 CFGBlock* DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +000073
Ted Kremenekeda180e22007-08-28 19:26:49 +000074 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000075 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
76 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000077
78 // A list of blocks that end with a "goto" that must be backpatched to their
79 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000080 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000081 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000082
Ted Kremenekeda180e22007-08-28 19:26:49 +000083 // A list of labels whose address has been taken (for indirect gotos).
84 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
85 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000086
87public:
Ted Kremenek889073f2007-08-23 16:51:22 +000088 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000089 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek654c78f2008-02-13 22:05:39 +000090 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000091 // Create an empty CFG.
Mike Stump31feda52009-07-17 01:31:16 +000092 cfg = new CFG();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000093 }
Mike Stump31feda52009-07-17 01:31:16 +000094
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000095 ~CFGBuilder() { delete cfg; }
Mike Stump31feda52009-07-17 01:31:16 +000096
Ted Kremenek9aae5132007-08-23 21:42:29 +000097 // buildCFG - Used by external clients to construct the CFG.
Mike Stump0d76d072009-07-20 23:24:15 +000098 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump31feda52009-07-17 01:31:16 +000099
Ted Kremenek93668002009-07-17 22:18:43 +0000100private:
101 // Visitors to walk an AST and construct the CFG.
102 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
103 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
Ted Kremenek0747de62009-07-18 00:47:21 +0000104 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
105 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000106 CFGBlock *VisitBreakStmt(BreakStmt *B);
107 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
108 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek21822592009-07-17 18:20:32 +0000109 CFGBlock *VisitChooseExpr(ChooseExpr *C);
110 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
111 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
112 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000113 CFGBlock *VisitDeclStmt(DeclStmt *DS);
114 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000115 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
116 CFGBlock *VisitDoStmt(DoStmt *D);
117 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000118 CFGBlock *VisitGotoStmt(GotoStmt* G);
119 CFGBlock *VisitIfStmt(IfStmt *I);
120 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
121 CFGBlock *VisitLabelStmt(LabelStmt *L);
122 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
123 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
124 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
125 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
126 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
127 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek0747de62009-07-18 00:47:21 +0000128 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
129 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000130 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
131 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000132
Ted Kremenek93668002009-07-17 22:18:43 +0000133 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
134 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
135 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000136
Ted Kremenek6065ef62008-04-28 18:00:46 +0000137 // NYS == Not Yet Supported
138 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000139 badCFG = true;
140 return Block;
141 }
Mike Stump31feda52009-07-17 01:31:16 +0000142
Ted Kremenek93668002009-07-17 22:18:43 +0000143 void autoCreateBlock() { if (!Block) Block = createBlock(); }
144 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000145 bool FinishBlock(CFGBlock* B);
Ted Kremenek93668002009-07-17 22:18:43 +0000146 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
147
Ted Kremenekb64d1832008-03-13 03:04:22 +0000148 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000149};
Mike Stump31feda52009-07-17 01:31:16 +0000150
Douglas Gregor4619e432008-12-05 23:32:09 +0000151// FIXME: Add support for dependent-sized array types in C++?
152// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000153static VariableArrayType* FindVA(Type* t) {
154 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
155 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
156 if (vat->getSizeExpr())
157 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000158
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000159 t = vt->getElementType().getTypePtr();
160 }
Mike Stump31feda52009-07-17 01:31:16 +0000161
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000162 return 0;
163}
Mike Stump31feda52009-07-17 01:31:16 +0000164
165/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
166/// arbitrary statement. Examples include a single expression or a function
167/// body (compound statement). The ownership of the returned CFG is
168/// transferred to the caller. If CFG construction fails, this method returns
169/// NULL.
Mike Stump0d76d072009-07-20 23:24:15 +0000170CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
171 Context = C;
Ted Kremenek93668002009-07-17 22:18:43 +0000172 assert(cfg);
173 if (!Statement)
174 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000175
Ted Kremenekb64d1832008-03-13 03:04:22 +0000176 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000177
178 // Create an empty block that will serve as the exit block for the CFG. Since
179 // this is the first block added to the CFG, it will be implicitly registered
180 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000181 Succ = createBlock();
182 assert (Succ == &cfg->getExit());
183 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000184
Ted Kremenek9aae5132007-08-23 21:42:29 +0000185 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000186 CFGBlock* B = addStmt(Statement);
Ted Kremenek40d87632008-02-27 17:33:02 +0000187 if (!B) B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000188
Ted Kremenek40d87632008-02-27 17:33:02 +0000189 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000190 // Finalize the last constructed block. This usually involves reversing the
191 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000192 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000193
194 // Backpatch the gotos whose label -> block mappings we didn't know when we
195 // encountered them.
196 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000197 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000198
Ted Kremenek9aae5132007-08-23 21:42:29 +0000199 CFGBlock* B = *I;
200 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
201 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
202
203 // If there is no target for the goto, then we are looking at an
204 // incomplete AST. Handle this by not registering a successor.
205 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000206
207 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000208 }
Mike Stump31feda52009-07-17 01:31:16 +0000209
Ted Kremenekeda180e22007-08-28 19:26:49 +0000210 // Add successors to the Indirect Goto Dispatch block (if we have one).
211 if (CFGBlock* B = cfg->getIndirectGotoBlock())
212 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
213 E = AddressTakenLabels.end(); I != E; ++I ) {
214
215 // Lookup the target block.
216 LabelMapTy::iterator LI = LabelMap.find(*I);
217
218 // If there is no target block that contains label, then we are looking
219 // at an incomplete AST. Handle this by not registering a successor.
220 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000221
222 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000223 }
Mike Stump31feda52009-07-17 01:31:16 +0000224
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000225 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000226 }
Mike Stump31feda52009-07-17 01:31:16 +0000227
228 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000229 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000230
Ted Kremenekb64d1832008-03-13 03:04:22 +0000231 if (badCFG) {
232 delete cfg;
233 cfg = NULL;
234 return NULL;
235 }
Mike Stump31feda52009-07-17 01:31:16 +0000236
237 // NULL out cfg so that repeated calls to the builder will fail and that the
238 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000239 CFG* t = cfg;
240 cfg = NULL;
241 return t;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000242}
Mike Stump31feda52009-07-17 01:31:16 +0000243
Ted Kremenek9aae5132007-08-23 21:42:29 +0000244/// createBlock - Used to lazily create blocks that are connected
245/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000246CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000247 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000248 if (add_successor && Succ)
249 B->addSuccessor(Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000250 return B;
251}
Mike Stump31feda52009-07-17 01:31:16 +0000252
253/// FinishBlock - When the last statement has been added to the block, we must
254/// reverse the statements because they have been inserted in reverse order.
Ted Kremenek55957a82009-05-02 00:13:27 +0000255bool CFGBuilder::FinishBlock(CFGBlock* B) {
256 if (badCFG)
257 return false;
258
Ted Kremenek93668002009-07-17 22:18:43 +0000259 assert(B);
Ted Kremenek81e14852007-08-27 19:46:09 +0000260 B->reverseStmts();
Ted Kremenek55957a82009-05-02 00:13:27 +0000261 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000262}
263
Ted Kremenek93668002009-07-17 22:18:43 +0000264/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000265/// blocks for ternary operators, &&, and ||. We also process "," and
266/// DeclStmts (which may contain nested control-flow).
Ted Kremenek93668002009-07-17 22:18:43 +0000267CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
268tryAgain:
269 switch (S->getStmtClass()) {
270 default:
271 return VisitStmt(S, alwaysAdd);
272
273 case Stmt::AddrLabelExprClass:
274 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
275
276 case Stmt::BinaryOperatorClass:
277 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
278
279 case Stmt::BlockExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000280 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000281
282 case Stmt::BlockDeclRefExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000283 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000284
285 case Stmt::BreakStmtClass:
286 return VisitBreakStmt(cast<BreakStmt>(S));
287
288 case Stmt::CallExprClass:
289 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
290
291 case Stmt::CaseStmtClass:
292 return VisitCaseStmt(cast<CaseStmt>(S));
293
294 case Stmt::ChooseExprClass:
295 return VisitChooseExpr(cast<ChooseExpr>(S));
296
297 case Stmt::CompoundStmtClass:
298 return VisitCompoundStmt(cast<CompoundStmt>(S));
299
300 case Stmt::ConditionalOperatorClass:
301 return VisitConditionalOperator(cast<ConditionalOperator>(S));
302
303 case Stmt::ContinueStmtClass:
304 return VisitContinueStmt(cast<ContinueStmt>(S));
305
306 case Stmt::DeclStmtClass:
307 return VisitDeclStmt(cast<DeclStmt>(S));
308
309 case Stmt::DefaultStmtClass:
310 return VisitDefaultStmt(cast<DefaultStmt>(S));
311
312 case Stmt::DoStmtClass:
313 return VisitDoStmt(cast<DoStmt>(S));
314
315 case Stmt::ForStmtClass:
316 return VisitForStmt(cast<ForStmt>(S));
317
318 case Stmt::GotoStmtClass:
319 return VisitGotoStmt(cast<GotoStmt>(S));
320
321 case Stmt::IfStmtClass:
322 return VisitIfStmt(cast<IfStmt>(S));
323
324 case Stmt::IndirectGotoStmtClass:
325 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
326
327 case Stmt::LabelStmtClass:
328 return VisitLabelStmt(cast<LabelStmt>(S));
329
330 case Stmt::ObjCAtCatchStmtClass:
331 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
332
333 case Stmt::ObjCAtSynchronizedStmtClass:
334 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
335
336 case Stmt::ObjCAtThrowStmtClass:
337 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
338
339 case Stmt::ObjCAtTryStmtClass:
340 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
341
342 case Stmt::ObjCForCollectionStmtClass:
343 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
344
345 case Stmt::ParenExprClass:
346 S = cast<ParenExpr>(S)->getSubExpr();
347 goto tryAgain;
348
349 case Stmt::NullStmtClass:
350 return Block;
351
352 case Stmt::ReturnStmtClass:
353 return VisitReturnStmt(cast<ReturnStmt>(S));
354
355 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000356 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000357
358 case Stmt::StmtExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000359 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000360
361 case Stmt::SwitchStmtClass:
362 return VisitSwitchStmt(cast<SwitchStmt>(S));
363
364 case Stmt::WhileStmtClass:
365 return VisitWhileStmt(cast<WhileStmt>(S));
366 }
367}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000368
Ted Kremenek93668002009-07-17 22:18:43 +0000369CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
370 if (alwaysAdd) {
371 autoCreateBlock();
372 Block->appendStmt(S);
Mike Stump31feda52009-07-17 01:31:16 +0000373 }
Ted Kremenek93668002009-07-17 22:18:43 +0000374
375 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000376}
Mike Stump31feda52009-07-17 01:31:16 +0000377
Ted Kremenek93668002009-07-17 22:18:43 +0000378/// VisitChildren - Visit the children of a Stmt.
379CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
380 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000381 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000382 E = Terminator->child_end(); I != E; ++I) {
383 if (*I) B = Visit(*I);
384 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000385 return B;
386}
Ted Kremenek93668002009-07-17 22:18:43 +0000387
388CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
389 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000390
Ted Kremenek93668002009-07-17 22:18:43 +0000391 if (alwaysAdd) {
392 autoCreateBlock();
393 Block->appendStmt(A);
394 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000395
Ted Kremenek9aae5132007-08-23 21:42:29 +0000396 return Block;
397}
Ted Kremenek93668002009-07-17 22:18:43 +0000398
399CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
400 if (B->isLogicalOp()) { // && or ||
Ted Kremenek9aae5132007-08-23 21:42:29 +0000401
Mike Stump0d76d072009-07-20 23:24:15 +0000402 // See if this is a known constant.
403 bool KnownTrue = false;
404 bool KnownFalse = false;
405 Expr::EvalResult Result;
406 if (B->getLHS()->Evaluate(Result, *Context)
407 && Result.Val.isInt()) {
408 if ((B->getOpcode() == BinaryOperator::LAnd)
409 == Result.Val.getInt().getBoolValue())
410 KnownTrue = true;
411 else
412 KnownFalse = true;
413 }
414
Ted Kremenek93668002009-07-17 22:18:43 +0000415 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
416 ConfluenceBlock->appendStmt(B);
417
418 if (!FinishBlock(ConfluenceBlock))
419 return 0;
420
421 // create the block evaluating the LHS
422 CFGBlock* LHSBlock = createBlock(false);
423 LHSBlock->setTerminator(B);
424
425 // create the block evaluating the RHS
426 Succ = ConfluenceBlock;
427 Block = NULL;
428 CFGBlock* RHSBlock = addStmt(B->getRHS());
429 if (!FinishBlock(RHSBlock))
430 return 0;
431
432 // Now link the LHSBlock with RHSBlock.
433 if (B->getOpcode() == BinaryOperator::LOr) {
Mike Stump0d76d072009-07-20 23:24:15 +0000434 if (KnownTrue)
435 LHSBlock->addSuccessor(0);
436 else
437 LHSBlock->addSuccessor(ConfluenceBlock);
438 if (KnownFalse)
439 LHSBlock->addSuccessor(0);
440 else
441 LHSBlock->addSuccessor(RHSBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000442 } else {
443 assert (B->getOpcode() == BinaryOperator::LAnd);
Mike Stump0d76d072009-07-20 23:24:15 +0000444 if (KnownFalse)
445 LHSBlock->addSuccessor(0);
446 else
447 LHSBlock->addSuccessor(RHSBlock);
448 if (KnownTrue)
449 LHSBlock->addSuccessor(0);
450 else
451 LHSBlock->addSuccessor(ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000452 }
453
454 // Generate the blocks for evaluating the LHS.
455 Block = LHSBlock;
456 return addStmt(B->getLHS());
457 }
458 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000459 autoCreateBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000460 Block->appendStmt(B);
461 addStmt(B->getRHS());
462 return addStmt(B->getLHS());
463 }
464
465 return VisitStmt(B, alwaysAdd);
466}
467
Ted Kremenek0747de62009-07-18 00:47:21 +0000468CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E, bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000469 // FIXME
470 return NYS();
471}
472
Ted Kremenek0747de62009-07-18 00:47:21 +0000473CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
474 bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000475 // FIXME
476 return NYS();
477}
478
479CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
480 // "break" is a control-flow statement. Thus we stop processing the current
481 // block.
482 if (Block && !FinishBlock(Block))
483 return 0;
484
485 // Now create a new block that ends with the break statement.
486 Block = createBlock(false);
487 Block->setTerminator(B);
488
489 // If there is no target for the break, then we are looking at an incomplete
490 // AST. This means that the CFG cannot be constructed.
491 if (BreakTargetBlock)
492 Block->addSuccessor(BreakTargetBlock);
493 else
494 badCFG = true;
495
496
Ted Kremenek9aae5132007-08-23 21:42:29 +0000497 return Block;
498}
Ted Kremenek93668002009-07-17 22:18:43 +0000499
500CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
501 // If this is a call to a no-return function, this stops the block here.
502 if (FunctionDecl *FD = C->getDirectCallee()) {
503
504 if (!FD->hasAttr<NoReturnAttr>())
505 return VisitStmt(C, alwaysAdd);
506
507 if (Block && !FinishBlock(Block))
508 return 0;
509
510 // Create new block with no successor for the remaining pieces.
511 Block = createBlock(false);
512 Block->appendStmt(C);
513
514 // Wire this to the exit block directly.
515 Block->addSuccessor(&cfg->getExit());
516
517 return VisitChildren(C);
518 }
519
520 return VisitStmt(C, alwaysAdd);
521}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000522
Ted Kremenek21822592009-07-17 18:20:32 +0000523CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
Mike Stump3557ea82009-07-21 01:46:17 +0000524 // See if this is a known constant.
525 bool KnownTrue = false;
526 bool KnownFalse = false;
527 Expr::EvalResult Result;
528 if (C->getCond()->Evaluate(Result, *Context)
529 && Result.Val.isInt()) {
530 if (Result.Val.getInt().getBoolValue())
531 KnownTrue = true;
532 else
533 KnownFalse = true;
534 }
535
Ted Kremenek21822592009-07-17 18:20:32 +0000536 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
537 ConfluenceBlock->appendStmt(C);
538 if (!FinishBlock(ConfluenceBlock))
539 return 0;
540
541 Succ = ConfluenceBlock;
542 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000543 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000544 if (!FinishBlock(LHSBlock))
545 return 0;
546
547 Succ = ConfluenceBlock;
548 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000549 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000550 if (!FinishBlock(RHSBlock))
551 return 0;
552
553 Block = createBlock(false);
Mike Stump3557ea82009-07-21 01:46:17 +0000554 if (KnownFalse)
555 Block->addSuccessor(0);
556 else
557 Block->addSuccessor(LHSBlock);
558 if (KnownTrue)
559 Block->addSuccessor(0);
560 else
561 Block->addSuccessor(RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000562 Block->setTerminator(C);
563 return addStmt(C->getCond());
564}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000565
Ted Kremenek93668002009-07-17 22:18:43 +0000566
567CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
568 CFGBlock* LastBlock = Block;
569
570 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
571 I != E; ++I ) {
572 LastBlock = addStmt(*I);
573 }
574 return LastBlock;
575}
576
Ted Kremenek51d40b02009-07-17 18:15:54 +0000577CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
Mike Stump0d76d072009-07-20 23:24:15 +0000578 // See if this is a known constant.
579 bool KnownTrue = false;
580 bool KnownFalse = false;
581 Expr::EvalResult Result;
582 if (C->getCond()->Evaluate(Result, *Context)
583 && Result.Val.isInt()) {
584 if (Result.Val.getInt().getBoolValue())
585 KnownTrue = true;
586 else
587 KnownFalse = true;
588 }
589
Ted Kremenek51d40b02009-07-17 18:15:54 +0000590 // Create the confluence block that will "merge" the results of the ternary
591 // expression.
592 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
593 ConfluenceBlock->appendStmt(C);
594 if (!FinishBlock(ConfluenceBlock))
595 return 0;
596
597 // Create a block for the LHS expression if there is an LHS expression. A
598 // GCC extension allows LHS to be NULL, causing the condition to be the
599 // value that is returned instead.
600 // e.g: x ?: y is shorthand for: x ? x : y;
601 Succ = ConfluenceBlock;
602 Block = NULL;
603 CFGBlock* LHSBlock = NULL;
604 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000605 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000606 if (!FinishBlock(LHSBlock))
607 return 0;
608 Block = NULL;
609 }
610
611 // Create the block for the RHS expression.
612 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000613 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000614 if (!FinishBlock(RHSBlock))
615 return 0;
616
617 // Create the block that will contain the condition.
618 Block = createBlock(false);
619
Mike Stump0d76d072009-07-20 23:24:15 +0000620 if (LHSBlock) {
621 if (KnownFalse)
622 Block->addSuccessor(0);
623 else
624 Block->addSuccessor(LHSBlock);
625 } else {
626 if (KnownFalse) {
627 // If we know the condition is false, add NULL as the successor for
628 // the block containing the condition. In this case, the confluence
629 // block will have just one predecessor.
630 Block->addSuccessor(0);
631 assert (ConfluenceBlock->pred_size() == 1);
632 } else {
633 // If we have no LHS expression, add the ConfluenceBlock as a direct
634 // successor for the block containing the condition. Moreover, we need to
635 // reverse the order of the predecessors in the ConfluenceBlock because
636 // the RHSBlock will have been added to the succcessors already, and we
637 // want the first predecessor to the the block containing the expression
638 // for the case when the ternary expression evaluates to true.
639 Block->addSuccessor(ConfluenceBlock);
640 assert (ConfluenceBlock->pred_size() == 2);
641 std::reverse(ConfluenceBlock->pred_begin(),
642 ConfluenceBlock->pred_end());
643 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000644 }
645
Mike Stump0d76d072009-07-20 23:24:15 +0000646 if (KnownTrue)
647 Block->addSuccessor(0);
648 else
649 Block->addSuccessor(RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000650
651 Block->setTerminator(C);
652 return addStmt(C->getCond());
653}
654
Ted Kremenek93668002009-07-17 22:18:43 +0000655CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
656 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000657
Ted Kremenek93668002009-07-17 22:18:43 +0000658 if (DS->isSingleDecl()) {
659 Block->appendStmt(DS);
660 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000661 }
Ted Kremenek93668002009-07-17 22:18:43 +0000662
663 CFGBlock *B = 0;
664
665 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
666 typedef llvm::SmallVector<Decl*,10> BufTy;
667 BufTy Buf(DS->decl_begin(), DS->decl_end());
668
669 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
670 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
671 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
672 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
673
674 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
675 // automatically freed with the CFG.
676 DeclGroupRef DG(*I);
677 Decl *D = *I;
678 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
679 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
680
681 // Append the fake DeclStmt to block.
682 Block->appendStmt(DSNew);
683 B = VisitDeclSubExpr(D);
684 }
685
686 return B;
687}
688
689/// VisitDeclSubExpr - Utility method to add block-level expressions for
690/// initializers in Decls.
691CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
692 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000693
Ted Kremenek93668002009-07-17 22:18:43 +0000694 VarDecl *VD = dyn_cast<VarDecl>(D);
695
696 if (!VD)
697 return Block;
698
699 Expr *Init = VD->getInit();
700
701 if (Init) {
702 // Optimization: Don't create separate block-level statements for literals.
703 switch (Init->getStmtClass()) {
704 case Stmt::IntegerLiteralClass:
705 case Stmt::CharacterLiteralClass:
706 case Stmt::StringLiteralClass:
707 break;
708 default:
709 Block = addStmt(Init);
710 }
711 }
712
713 // If the type of VD is a VLA, then we must process its size expressions.
714 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
715 VA = FindVA(VA->getElementType().getTypePtr()))
716 Block = addStmt(VA->getSizeExpr());
717
718 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000719}
720
721CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump23a443b2009-07-21 00:38:52 +0000722 // See if this is a known constant.
Mike Stump0d76d072009-07-20 23:24:15 +0000723 bool KnownTrue = false;
724 bool KnownFalse = false;
725 Expr::EvalResult Result;
726 if (I->getCond()->Evaluate(Result, *Context)
727 && Result.Val.isInt()) {
728 if (Result.Val.getInt().getBoolValue())
729 KnownTrue = true;
730 else
731 KnownFalse = true;
732 }
733
Mike Stump31feda52009-07-17 01:31:16 +0000734 // We may see an if statement in the middle of a basic block, or it may be the
735 // first statement we are processing. In either case, we create a new basic
736 // block. First, we create the blocks for the then...else statements, and
737 // then we create the block containing the if statement. If we were in the
738 // middle of a block, we stop processing that block and reverse its
739 // statements. That block is then the implicit successor for the "then" and
740 // "else" clauses.
741
742 // The block we were proccessing is now finished. Make it the successor
743 // block.
744 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000745 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000746 if (!FinishBlock(Block))
747 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000748 }
Mike Stump31feda52009-07-17 01:31:16 +0000749
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000750 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000751 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000752
Ted Kremenek9aae5132007-08-23 21:42:29 +0000753 if (Stmt* Else = I->getElse()) {
754 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000755
Ted Kremenek9aae5132007-08-23 21:42:29 +0000756 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000757 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000758 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000759 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000760
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000761 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
762 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000763 else if (Block) {
764 if (!FinishBlock(ElseBlock))
765 return 0;
766 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000767 }
Mike Stump31feda52009-07-17 01:31:16 +0000768
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000769 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000770 CFGBlock* ThenBlock;
771 {
772 Stmt* Then = I->getThen();
773 assert (Then);
774 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000775 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000776 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000777
Ted Kremenek1b379512009-04-01 03:52:47 +0000778 if (!ThenBlock) {
779 // We can reach here if the "then" body has all NullStmts.
780 // Create an empty block so we can distinguish between true and false
781 // branches in path-sensitive analyses.
782 ThenBlock = createBlock(false);
783 ThenBlock->addSuccessor(sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000784 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000785 if (!FinishBlock(ThenBlock))
786 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000787 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000788 }
789
Mike Stump31feda52009-07-17 01:31:16 +0000790 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000791 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000792
Ted Kremenek9aae5132007-08-23 21:42:29 +0000793 // Set the terminator of the new block to the If statement.
794 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000795
Ted Kremenek9aae5132007-08-23 21:42:29 +0000796 // Now add the successors.
Mike Stump0d76d072009-07-20 23:24:15 +0000797 if (KnownFalse)
798 Block->addSuccessor(0);
799 else
800 Block->addSuccessor(ThenBlock);
801 if (KnownTrue)
802 Block->addSuccessor(0);
803 else
804 Block->addSuccessor(ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000805
806 // Add the condition as the last statement in the new block. This may create
807 // new blocks as the condition may contain control-flow. Any newly created
808 // blocks will be pointed to be "Block".
Ted Kremenek93668002009-07-17 22:18:43 +0000809 return addStmt(I->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000810}
Mike Stump31feda52009-07-17 01:31:16 +0000811
812
Ted Kremenek9aae5132007-08-23 21:42:29 +0000813CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump31feda52009-07-17 01:31:16 +0000814 // If we were in the middle of a block we stop processing that block and
815 // reverse its statements.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000816 //
Mike Stump31feda52009-07-17 01:31:16 +0000817 // NOTE: If a "return" appears in the middle of a block, this means that the
818 // code afterwards is DEAD (unreachable). We still keep a basic block
819 // for that code; a simple "mark-and-sweep" from the entry block will be
820 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000821 if (Block) FinishBlock(Block);
822
823 // Create the new block.
824 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000825
Ted Kremenek9aae5132007-08-23 21:42:29 +0000826 // The Exit block is the only successor.
827 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000828
829 // Add the return statement to the block. This may create new blocks if R
830 // contains control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +0000831 return VisitStmt(R, true);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000832}
833
834CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
835 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000836 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000837 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000838
Ted Kremenek93668002009-07-17 22:18:43 +0000839 if (!LabelBlock) // This can happen when the body is empty, i.e.
840 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000841
Ted Kremenek93668002009-07-17 22:18:43 +0000842 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000843 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000844
845 // Labels partition blocks, so this is the end of the basic block we were
846 // processing (L is the block's label). Because this is label (and we have
847 // already processed the substatement) there is no extra control-flow to worry
848 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000849 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000850 if (!FinishBlock(LabelBlock))
851 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000852
853 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000854 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000855
Ted Kremenek9aae5132007-08-23 21:42:29 +0000856 // This block is now the implicit successor of other blocks.
857 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000858
Ted Kremenek9aae5132007-08-23 21:42:29 +0000859 return LabelBlock;
860}
861
862CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000863 // Goto is a control-flow statement. Thus we stop processing the current
864 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000865 if (Block)
866 FinishBlock(Block);
867
Ted Kremenek9aae5132007-08-23 21:42:29 +0000868 Block = createBlock(false);
869 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000870
871 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000872 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000873
Ted Kremenek9aae5132007-08-23 21:42:29 +0000874 if (I == LabelMap.end())
875 // We will need to backpatch this block later.
876 BackpatchBlocks.push_back(Block);
877 else
878 Block->addSuccessor(I->second);
879
Mike Stump31feda52009-07-17 01:31:16 +0000880 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000881}
882
883CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Mike Stump014b3ea2009-07-21 01:12:51 +0000884 // See if this is a known constant.
885 bool KnownTrue = false;
886 bool KnownFalse = false;
887 Expr::EvalResult Result;
888 if (F->getCond() && F->getCond()->Evaluate(Result, *Context)
889 && Result.Val.isInt()) {
890 if (Result.Val.getInt().getBoolValue())
891 KnownTrue = true;
892 else
893 KnownFalse = true;
894 }
895 if (F->getCond() == 0)
896 KnownTrue = true;
897
Ted Kremenek9aae5132007-08-23 21:42:29 +0000898 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000899
Mike Stump014b3ea2009-07-21 01:12:51 +0000900 // "for" is a control-flow statement. Thus we stop processing the current
901 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000902 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000903 if (!FinishBlock(Block))
904 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000905 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000906 } else
907 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000908
909 // Because of short-circuit evaluation, the condition of the loop can span
910 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
911 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000912 CFGBlock* ExitConditionBlock = createBlock(false);
913 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000914
Ted Kremenek81e14852007-08-27 19:46:09 +0000915 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000916 ExitConditionBlock->setTerminator(F);
917
918 // Now add the actual condition to the condition block. Because the condition
919 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000920 if (Stmt* C = F->getCond()) {
921 Block = ExitConditionBlock;
922 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000923 if (Block) {
924 if (!FinishBlock(EntryConditionBlock))
925 return 0;
926 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000927 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000928
Mike Stump31feda52009-07-17 01:31:16 +0000929 // The condition block is the implicit successor for the loop body as well as
930 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000931 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000932
Ted Kremenek9aae5132007-08-23 21:42:29 +0000933 // Now create the loop body.
934 {
935 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000936
Ted Kremenek9aae5132007-08-23 21:42:29 +0000937 // Save the current values for Block, Succ, and continue and break targets
938 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
939 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000940 save_break(BreakTargetBlock);
941
Ted Kremeneke9610502007-08-30 18:39:40 +0000942 // Create a new block to contain the (bottom) of the loop body.
943 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000944
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000945 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000946 // Generate increment code in its own basic block. This is the target of
947 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000948 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000949 } else {
950 // No increment code. Create a special, empty, block that is used as the
951 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000952 assert(Succ == EntryConditionBlock);
953 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000954 }
Mike Stump31feda52009-07-17 01:31:16 +0000955
Ted Kremenek902393b2009-04-28 00:51:56 +0000956 // Finish up the increment (or empty) block if it hasn't been already.
957 if (Block) {
958 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000959 if (!FinishBlock(Block))
960 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000961 Block = 0;
962 }
Mike Stump31feda52009-07-17 01:31:16 +0000963
Ted Kremenek902393b2009-04-28 00:51:56 +0000964 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000965
Ted Kremenek902393b2009-04-28 00:51:56 +0000966 // The starting block for the loop increment is the block that should
967 // represent the 'loop target' for looping back to the start of the loop.
968 ContinueTargetBlock->setLoopTarget(F);
969
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000970 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000971 BreakTargetBlock = LoopSuccessor;
972
973 // Now populate the body block, and in the process create new blocks as we
974 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000975 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000976
977 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +0000978 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +0000979 else if (Block) {
980 if (!FinishBlock(BodyBlock))
981 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000982 }
983
Mike Stump014b3ea2009-07-21 01:12:51 +0000984 if (KnownFalse)
985 ExitConditionBlock->addSuccessor(0);
986 else {
987 // This new body block is a successor to our "exit" condition block.
988 ExitConditionBlock->addSuccessor(BodyBlock);
989 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000990 }
Mike Stump31feda52009-07-17 01:31:16 +0000991
Mike Stump014b3ea2009-07-21 01:12:51 +0000992 if (KnownTrue)
993 ExitConditionBlock->addSuccessor(0);
994 else {
995 // Link up the condition block with the code that follows the loop. (the
996 // false branch).
997 ExitConditionBlock->addSuccessor(LoopSuccessor);
998 }
Mike Stump31feda52009-07-17 01:31:16 +0000999
Ted Kremenek9aae5132007-08-23 21:42:29 +00001000 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001001 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001002 if (Stmt* I = F->getInit()) {
1003 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001004 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001005 } else {
1006 // There is no loop initialization. We are thus basically a while loop.
1007 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001008 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001009 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001010 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001011 }
1012}
1013
Ted Kremenek9d56e642008-11-11 17:10:00 +00001014CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1015 // Objective-C fast enumeration 'for' statements:
1016 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1017 //
1018 // for ( Type newVariable in collection_expression ) { statements }
1019 //
1020 // becomes:
1021 //
1022 // prologue:
1023 // 1. collection_expression
1024 // T. jump to loop_entry
1025 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001026 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001027 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1028 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1029 // TB:
1030 // statements
1031 // T. jump to loop_entry
1032 // FB:
1033 // what comes after
1034 //
1035 // and
1036 //
1037 // Type existingItem;
1038 // for ( existingItem in expression ) { statements }
1039 //
1040 // becomes:
1041 //
Mike Stump31feda52009-07-17 01:31:16 +00001042 // the same with newVariable replaced with existingItem; the binding works
1043 // the same except that for one ObjCForCollectionStmt::getElement() returns
1044 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001045 //
Mike Stump31feda52009-07-17 01:31:16 +00001046
Ted Kremenek9d56e642008-11-11 17:10:00 +00001047 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001048
Ted Kremenek9d56e642008-11-11 17:10:00 +00001049 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001050 if (!FinishBlock(Block))
1051 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001052 LoopSuccessor = Block;
1053 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001054 } else
1055 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001056
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001057 // Build the condition blocks.
1058 CFGBlock* ExitConditionBlock = createBlock(false);
1059 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001060
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001061 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001062 ExitConditionBlock->setTerminator(S);
1063
1064 // The last statement in the block should be the ObjCForCollectionStmt, which
1065 // performs the actual binding to 'element' and determines if there are any
1066 // more items in the collection.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001067 ExitConditionBlock->appendStmt(S);
1068 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001069
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001070 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001071 // generate new blocks as necesary. We DON'T add the statement by default to
1072 // the CFG unless it contains control-flow.
Ted Kremenek93668002009-07-17 22:18:43 +00001073 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump31feda52009-07-17 01:31:16 +00001074 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001075 if (!FinishBlock(EntryConditionBlock))
1076 return 0;
1077 Block = 0;
1078 }
Mike Stump31feda52009-07-17 01:31:16 +00001079
1080 // The condition block is the implicit successor for the loop body as well as
1081 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001082 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001083
Ted Kremenek9d56e642008-11-11 17:10:00 +00001084 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001085 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001086 // Save the current values for Succ, continue and break targets.
1087 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001088 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1089
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001090 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001091 ContinueTargetBlock = EntryConditionBlock;
1092
Ted Kremenek93668002009-07-17 22:18:43 +00001093 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001094
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001095 if (!BodyBlock)
1096 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001097 else if (Block) {
1098 if (!FinishBlock(BodyBlock))
1099 return 0;
1100 }
Mike Stump31feda52009-07-17 01:31:16 +00001101
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001102 // This new body block is a successor to our "exit" condition block.
1103 ExitConditionBlock->addSuccessor(BodyBlock);
1104 }
Mike Stump31feda52009-07-17 01:31:16 +00001105
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001106 // Link up the condition block with the code that follows the loop.
1107 // (the false branch).
1108 ExitConditionBlock->addSuccessor(LoopSuccessor);
1109
Ted Kremenek9d56e642008-11-11 17:10:00 +00001110 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001111 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001112 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001113}
1114
Ted Kremenek49805452009-05-02 01:49:13 +00001115CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1116 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001117
Ted Kremenek49805452009-05-02 01:49:13 +00001118 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001119 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001120
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001121 // The sync body starts its own basic block. This makes it a little easier
1122 // for diagnostic clients.
1123 if (SyncBlock) {
1124 if (!FinishBlock(SyncBlock))
1125 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001126
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001127 Block = 0;
1128 }
Mike Stump31feda52009-07-17 01:31:16 +00001129
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001130 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001131
Ted Kremenek49805452009-05-02 01:49:13 +00001132 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001133 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001134}
Mike Stump31feda52009-07-17 01:31:16 +00001135
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001136CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001137 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001138 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001139}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001140
Ted Kremenek9aae5132007-08-23 21:42:29 +00001141CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Mike Stump23a443b2009-07-21 00:38:52 +00001142 // See if this is a known constant.
1143 bool KnownTrue = false;
1144 bool KnownFalse = false;
1145 Expr::EvalResult Result;
1146 if (W->getCond()->Evaluate(Result, *Context)
1147 && Result.Val.isInt()) {
1148 if (Result.Val.getInt().getBoolValue())
1149 KnownTrue = true;
1150 else
1151 KnownFalse = true;
1152 }
1153
Ted Kremenek9aae5132007-08-23 21:42:29 +00001154 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001155
Mike Stump014b3ea2009-07-21 01:12:51 +00001156 // "while" is a control-flow statement. Thus we stop processing the current
1157 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001158 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001159 if (!FinishBlock(Block))
1160 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001161 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001162 } else
1163 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001164
1165 // Because of short-circuit evaluation, the condition of the loop can span
1166 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1167 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001168 CFGBlock* ExitConditionBlock = createBlock(false);
1169 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001170
Ted Kremenek81e14852007-08-27 19:46:09 +00001171 // Set the terminator for the "exit" condition block.
1172 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001173
1174 // Now add the actual condition to the condition block. Because the condition
1175 // itself may contain control-flow, new blocks may be created. Thus we update
1176 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001177 if (Stmt* C = W->getCond()) {
1178 Block = ExitConditionBlock;
1179 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001180 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +00001181 if (Block) {
1182 if (!FinishBlock(EntryConditionBlock))
1183 return 0;
1184 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001185 }
Mike Stump31feda52009-07-17 01:31:16 +00001186
1187 // The condition block is the implicit successor for the loop body as well as
1188 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001189 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001190
Ted Kremenek9aae5132007-08-23 21:42:29 +00001191 // Process the loop body.
1192 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001193 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001194
1195 // Save the current values for Block, Succ, and continue and break targets
1196 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1197 save_continue(ContinueTargetBlock),
1198 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001199
Mike Stump31feda52009-07-17 01:31:16 +00001200 // Create an empty block to represent the transition block for looping back
1201 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001202 Block = 0;
1203 assert(Succ == EntryConditionBlock);
1204 Succ = createBlock();
1205 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001206 ContinueTargetBlock = Succ;
1207
Ted Kremenek9aae5132007-08-23 21:42:29 +00001208 // All breaks should go to the code following the loop.
1209 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001210
Ted Kremenek9aae5132007-08-23 21:42:29 +00001211 // NULL out Block to force lazy instantiation of blocks for the body.
1212 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001213
Ted Kremenek9aae5132007-08-23 21:42:29 +00001214 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001215 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001216
Ted Kremeneke9610502007-08-30 18:39:40 +00001217 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001218 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001219 else if (Block) {
1220 if (!FinishBlock(BodyBlock))
1221 return 0;
1222 }
Mike Stump31feda52009-07-17 01:31:16 +00001223
Mike Stump23a443b2009-07-21 00:38:52 +00001224 if (KnownFalse)
1225 ExitConditionBlock->addSuccessor(0);
1226 else {
1227 // Add the loop body entry as a successor to the condition.
1228 ExitConditionBlock->addSuccessor(BodyBlock);
1229 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001230 }
Mike Stump31feda52009-07-17 01:31:16 +00001231
Mike Stump23a443b2009-07-21 00:38:52 +00001232 if (KnownTrue)
1233 ExitConditionBlock->addSuccessor(0);
1234 else {
1235 // Link up the condition block with the code that follows the loop. (the
1236 // false branch).
1237 ExitConditionBlock->addSuccessor(LoopSuccessor);
1238 }
Mike Stump31feda52009-07-17 01:31:16 +00001239
1240 // There can be no more statements in the condition block since we loop back
1241 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001242 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001243
Ted Kremenek9aae5132007-08-23 21:42:29 +00001244 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001245 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001246 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001247}
Ted Kremenek93668002009-07-17 22:18:43 +00001248
1249
1250CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1251 // FIXME: For now we pretend that @catch and the code it contains does not
1252 // exit.
1253 return Block;
1254}
Mike Stump31feda52009-07-17 01:31:16 +00001255
Ted Kremenek93041ba2008-12-09 20:20:09 +00001256CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1257 // FIXME: This isn't complete. We basically treat @throw like a return
1258 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001259
1260 // If we were in the middle of a block we stop processing that block and
1261 // reverse its statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001262 if (Block && !FinishBlock(Block))
1263 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001264
Ted Kremenek93041ba2008-12-09 20:20:09 +00001265 // Create the new block.
1266 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001267
Ted Kremenek93041ba2008-12-09 20:20:09 +00001268 // The Exit block is the only successor.
1269 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001270
1271 // Add the statement to the block. This may create new blocks if S contains
1272 // control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +00001273 return VisitStmt(S, true);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001274}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001275
Ted Kremenek93668002009-07-17 22:18:43 +00001276CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Mike Stump8d50b6a2009-07-21 01:27:50 +00001277 // See if this is a known constant.
1278 bool KnownTrue = false;
1279 bool KnownFalse = false;
1280 Expr::EvalResult Result;
1281 if (D->getCond()->Evaluate(Result, *Context)
1282 && Result.Val.isInt()) {
1283 if (Result.Val.getInt().getBoolValue())
1284 KnownTrue = true;
1285 else
1286 KnownFalse = true;
1287 }
Mike Stump31feda52009-07-17 01:31:16 +00001288
Ted Kremenek9aae5132007-08-23 21:42:29 +00001289 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001290
Mike Stump8d50b6a2009-07-21 01:27:50 +00001291 // "do...while" is a control-flow statement. Thus we stop processing the
1292 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001293 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001294 if (!FinishBlock(Block))
1295 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001296 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001297 } else
1298 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001299
1300 // Because of short-circuit evaluation, the condition of the loop can span
1301 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1302 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001303 CFGBlock* ExitConditionBlock = createBlock(false);
1304 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001305
Ted Kremenek81e14852007-08-27 19:46:09 +00001306 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001307 ExitConditionBlock->setTerminator(D);
1308
1309 // Now add the actual condition to the condition block. Because the condition
1310 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001311 if (Stmt* C = D->getCond()) {
1312 Block = ExitConditionBlock;
1313 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001314 if (Block) {
1315 if (!FinishBlock(EntryConditionBlock))
1316 return 0;
1317 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001318 }
Mike Stump31feda52009-07-17 01:31:16 +00001319
Ted Kremeneka1523a32008-02-27 07:20:00 +00001320 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001321 Succ = EntryConditionBlock;
1322
Ted Kremenek9aae5132007-08-23 21:42:29 +00001323 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001324 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001325 {
1326 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001327
Ted Kremenek9aae5132007-08-23 21:42:29 +00001328 // Save the current values for Block, Succ, and continue and break targets
1329 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1330 save_continue(ContinueTargetBlock),
1331 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001332
Ted Kremenek9aae5132007-08-23 21:42:29 +00001333 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001334 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001335
Ted Kremenek9aae5132007-08-23 21:42:29 +00001336 // All breaks should go to the code following the loop.
1337 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001338
Ted Kremenek9aae5132007-08-23 21:42:29 +00001339 // NULL out Block to force lazy instantiation of blocks for the body.
1340 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001341
Ted Kremenek9aae5132007-08-23 21:42:29 +00001342 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001343 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001344
Ted Kremeneke9610502007-08-30 18:39:40 +00001345 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001346 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001347 else if (Block) {
1348 if (!FinishBlock(BodyBlock))
1349 return 0;
1350 }
Mike Stump31feda52009-07-17 01:31:16 +00001351
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001352 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001353 // ExitConditionBlock to represent the "loop back" transition. Create an
1354 // empty block to represent the transition block for looping back to the
1355 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001356 // FIXME: Can we do this more efficiently without adding another block?
1357 Block = NULL;
1358 Succ = BodyBlock;
1359 CFGBlock *LoopBackBlock = createBlock();
1360 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001361
Mike Stump8d50b6a2009-07-21 01:27:50 +00001362 if (KnownFalse)
1363 ExitConditionBlock->addSuccessor(0);
1364 else {
1365 // Add the loop body entry as a successor to the condition.
1366 ExitConditionBlock->addSuccessor(LoopBackBlock);
1367 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001368 }
Mike Stump31feda52009-07-17 01:31:16 +00001369
Mike Stump8d50b6a2009-07-21 01:27:50 +00001370 if (KnownTrue)
1371 ExitConditionBlock->addSuccessor(0);
1372 else {
1373 // Link up the condition block with the code that follows the loop. (the
1374 // false branch).
1375 ExitConditionBlock->addSuccessor(LoopSuccessor);
1376 }
Mike Stump31feda52009-07-17 01:31:16 +00001377
1378 // There can be no more statements in the body block(s) since we loop back to
1379 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001380 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001381
Ted Kremenek9aae5132007-08-23 21:42:29 +00001382 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001383 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001384 return BodyBlock;
1385}
1386
1387CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1388 // "continue" is a control-flow statement. Thus we stop processing the
1389 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001390 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001391 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001392
Ted Kremenek9aae5132007-08-23 21:42:29 +00001393 // Now create a new block that ends with the continue statement.
1394 Block = createBlock(false);
1395 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001396
Ted Kremenek9aae5132007-08-23 21:42:29 +00001397 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001398 // incomplete AST. This means the CFG cannot be constructed.
1399 if (ContinueTargetBlock)
1400 Block->addSuccessor(ContinueTargetBlock);
1401 else
1402 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001403
Ted Kremenek9aae5132007-08-23 21:42:29 +00001404 return Block;
1405}
Ted Kremenek93668002009-07-17 22:18:43 +00001406
Ted Kremenek0747de62009-07-18 00:47:21 +00001407CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1408 bool alwaysAdd) {
1409
1410 if (alwaysAdd) {
1411 autoCreateBlock();
1412 Block->appendStmt(E);
1413 }
1414
Ted Kremenek93668002009-07-17 22:18:43 +00001415 // VLA types have expressions that must be evaluated.
1416 if (E->isArgumentType()) {
1417 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1418 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1419 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001420 }
Ted Kremenek93668002009-07-17 22:18:43 +00001421
Mike Stump31feda52009-07-17 01:31:16 +00001422 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001423}
Ted Kremenek93668002009-07-17 22:18:43 +00001424
1425/// VisitStmtExpr - Utility method to handle (nested) statement
1426/// expressions (a GCC extension).
Ted Kremenek0747de62009-07-18 00:47:21 +00001427CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1428 if (alwaysAdd) {
1429 autoCreateBlock();
1430 Block->appendStmt(SE);
1431 }
Ted Kremenek93668002009-07-17 22:18:43 +00001432 return VisitCompoundStmt(SE->getSubStmt());
1433}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001434
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001435CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001436 // "switch" is a control-flow statement. Thus we stop processing the current
1437 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001438 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001439
Ted Kremenek9aae5132007-08-23 21:42:29 +00001440 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001441 if (!FinishBlock(Block))
1442 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001443 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001444 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001445
1446 // Save the current "switch" context.
1447 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001448 save_break(BreakTargetBlock),
1449 save_default(DefaultCaseBlock);
1450
Mike Stump31feda52009-07-17 01:31:16 +00001451 // Set the "default" case to be the block after the switch statement. If the
1452 // switch statement contains a "default:", this value will be overwritten with
1453 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001454 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001455
Ted Kremenek9aae5132007-08-23 21:42:29 +00001456 // Create a new block that will contain the switch statement.
1457 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001458
Ted Kremenek9aae5132007-08-23 21:42:29 +00001459 // Now process the switch body. The code after the switch is the implicit
1460 // successor.
1461 Succ = SwitchSuccessor;
1462 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001463
1464 // When visiting the body, the case statements should automatically get linked
1465 // up to the switch. We also don't keep a pointer to the body, since all
1466 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001467 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001468 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001469 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001470 if (Block) {
1471 if (!FinishBlock(BodyBlock))
1472 return 0;
1473 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001474
Mike Stump31feda52009-07-17 01:31:16 +00001475 // If we have no "default:" case, the default transition is to the code
1476 // following the switch body.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001477 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001478
Ted Kremenek81e14852007-08-27 19:46:09 +00001479 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001480 SwitchTerminatedBlock->setTerminator(Terminator);
1481 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001482 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001483
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001484 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001485}
1486
Ted Kremenek93668002009-07-17 22:18:43 +00001487CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001488 // CaseStmts are essentially labels, so they are the first statement in a
1489 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001490
Ted Kremenek93668002009-07-17 22:18:43 +00001491 if (CS->getSubStmt())
1492 addStmt(CS->getSubStmt());
1493
Ted Kremenek55e91e82007-08-30 18:48:11 +00001494 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001495 if (!CaseBlock)
1496 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001497
1498 // Cases statements partition blocks, so this is the top of the basic block we
1499 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001500 CaseBlock->setLabel(CS);
1501
Ted Kremenek55957a82009-05-02 00:13:27 +00001502 if (!FinishBlock(CaseBlock))
1503 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001504
1505 // Add this block to the list of successors for the block with the switch
1506 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001507 assert(SwitchTerminatedBlock);
Ted Kremenek654c78f2008-02-13 22:05:39 +00001508 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001509
Ted Kremenek9aae5132007-08-23 21:42:29 +00001510 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1511 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001512
Ted Kremenek9aae5132007-08-23 21:42:29 +00001513 // This block is now the implicit successor of other blocks.
1514 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001515
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001516 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001517}
Mike Stump31feda52009-07-17 01:31:16 +00001518
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001519CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001520 if (Terminator->getSubStmt())
1521 addStmt(Terminator->getSubStmt());
1522
Ted Kremenek654c78f2008-02-13 22:05:39 +00001523 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001524
1525 if (!DefaultCaseBlock)
1526 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001527
1528 // Default statements partition blocks, so this is the top of the basic block
1529 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001530 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek93668002009-07-17 22:18:43 +00001531
Ted Kremenek55957a82009-05-02 00:13:27 +00001532 if (!FinishBlock(DefaultCaseBlock))
1533 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001534
Mike Stump31feda52009-07-17 01:31:16 +00001535 // Unlike case statements, we don't add the default block to the successors
1536 // for the switch statement immediately. This is done when we finish
1537 // processing the switch statement. This allows for the default case
1538 // (including a fall-through to the code after the switch statement) to always
1539 // be the last successor of a switch-terminated block.
1540
Ted Kremenek654c78f2008-02-13 22:05:39 +00001541 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1542 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001543
Ted Kremenek654c78f2008-02-13 22:05:39 +00001544 // This block is now the implicit successor of other blocks.
1545 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001546
1547 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001548}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001549
Ted Kremenekeda180e22007-08-28 19:26:49 +00001550CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001551 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001552 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001553
Ted Kremenekeda180e22007-08-28 19:26:49 +00001554 if (!IBlock) {
1555 IBlock = createBlock(false);
1556 cfg->setIndirectGotoBlock(IBlock);
1557 }
Mike Stump31feda52009-07-17 01:31:16 +00001558
Ted Kremenekeda180e22007-08-28 19:26:49 +00001559 // IndirectGoto is a control-flow statement. Thus we stop processing the
1560 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001561 if (Block && !FinishBlock(Block))
1562 return 0;
1563
Ted Kremenekeda180e22007-08-28 19:26:49 +00001564 Block = createBlock(false);
1565 Block->setTerminator(I);
1566 Block->addSuccessor(IBlock);
1567 return addStmt(I->getTarget());
1568}
1569
Ted Kremenek04cca642007-08-23 21:26:19 +00001570} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001571
Mike Stump31feda52009-07-17 01:31:16 +00001572/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1573/// no successors or predecessors. If this is the first block created in the
1574/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001575CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001576 bool first_block = begin() == end();
1577
1578 // Create the block.
Ted Kremenek813dd672007-09-05 20:02:05 +00001579 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek889073f2007-08-23 16:51:22 +00001580
1581 // If this is the first block, set it as the Entry and Exit.
1582 if (first_block) Entry = Exit = &front();
1583
1584 // Return the block.
1585 return &front();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001586}
1587
Ted Kremenek889073f2007-08-23 16:51:22 +00001588/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1589/// CFG is returned to the caller.
Mike Stump0d76d072009-07-20 23:24:15 +00001590CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001591 CFGBuilder Builder;
Mike Stump0d76d072009-07-20 23:24:15 +00001592 return Builder.buildCFG(Statement, C);
Ted Kremenek889073f2007-08-23 16:51:22 +00001593}
1594
1595/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001596void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1597
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001598//===----------------------------------------------------------------------===//
1599// CFG: Queries for BlkExprs.
1600//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001601
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001602namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001603 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001604}
1605
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001606static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1607 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001608 return;
Mike Stump31feda52009-07-17 01:31:16 +00001609
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001610 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001611 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001612
Ted Kremenek95a123c2008-01-26 00:03:27 +00001613 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1614 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001615
Ted Kremenek95a123c2008-01-26 00:03:27 +00001616 FindSubExprAssignments(*I, Set);
1617 }
1618}
1619
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001620static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1621 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001622
1623 // Look for assignments that are used as subexpressions. These are the only
1624 // assignments that we want to *possibly* register as a block-level
1625 // expression. Basically, if an assignment occurs both in a subexpression and
1626 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001627 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001628
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001629 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1630 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001631 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001632
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001633 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001634
1635 // Iterate over the statements again on identify the Expr* and Stmt* at the
1636 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001637
Ted Kremenek95a123c2008-01-26 00:03:27 +00001638 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001639 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001640
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001641 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001642 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001643 // expression are really "statements" whose value is never used by
1644 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001645 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001646 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001647 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1648 // Special handling for statement expressions. The last statement in
1649 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001650 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001651 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001652 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001653 (*M)[C->body_back()] = x;
1654 }
1655 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001656
Ted Kremenek95a123c2008-01-26 00:03:27 +00001657 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001658 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001659 }
Mike Stump31feda52009-07-17 01:31:16 +00001660
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001661 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001662
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001663 Stmt* S = I->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001664
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001665 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001666 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001667 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001668 }
1669 }
Mike Stump31feda52009-07-17 01:31:16 +00001670
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001671 return M;
1672}
1673
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001674CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1675 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001676 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001677
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001678 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001679 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001680
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001681 if (I == M->end()) return CFG::BlkExprNumTy();
1682 else return CFG::BlkExprNumTy(I->second);
1683}
1684
1685unsigned CFG::getNumBlkExprs() {
1686 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1687 return M->size();
1688 else {
1689 // We assume callers interested in the number of BlkExprs will want
1690 // the map constructed if it doesn't already exist.
1691 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1692 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1693 }
1694}
1695
Ted Kremenek6065ef62008-04-28 18:00:46 +00001696//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001697// Cleanup: CFG dstor.
1698//===----------------------------------------------------------------------===//
1699
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001700CFG::~CFG() {
1701 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1702}
Mike Stump31feda52009-07-17 01:31:16 +00001703
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001704//===----------------------------------------------------------------------===//
1705// CFG pretty printing
1706//===----------------------------------------------------------------------===//
1707
Ted Kremenek7e776b12007-08-22 18:22:34 +00001708namespace {
1709
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001710class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001711
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001712 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1713 StmtMapTy StmtMap;
1714 signed CurrentBlock;
1715 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001716 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001717public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001718
Chris Lattnerc61089a2009-06-30 01:26:17 +00001719 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1720 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001721 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1722 unsigned j = 1;
1723 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1724 BI != BEnd; ++BI, ++j )
1725 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1726 }
1727 }
Mike Stump31feda52009-07-17 01:31:16 +00001728
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001729 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001730
Chris Lattnerc61089a2009-06-30 01:26:17 +00001731 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001732 void setBlockID(signed i) { CurrentBlock = i; }
1733 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001734
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001735 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001736
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001737 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001738
1739 if (I == StmtMap.end())
1740 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001741
1742 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001743 && I->second.second == CurrentStmt)
1744 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001745
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001746 OS << "[B" << I->second.first << "." << I->second.second << "]";
1747 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001748 }
1749};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001750} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001751
Chris Lattnerc61089a2009-06-30 01:26:17 +00001752
1753namespace {
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001754class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1755 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001756
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001757 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001758 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001759 PrintingPolicy Policy;
1760
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001761public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001762 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001763 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001764 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001765
Ted Kremenek9aae5132007-08-23 21:42:29 +00001766 void VisitIfStmt(IfStmt* I) {
1767 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001768 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001769 }
Mike Stump31feda52009-07-17 01:31:16 +00001770
Ted Kremenek9aae5132007-08-23 21:42:29 +00001771 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001772 void VisitStmt(Stmt* Terminator) {
1773 Terminator->printPretty(OS, Helper, Policy);
1774 }
1775
Ted Kremenek9aae5132007-08-23 21:42:29 +00001776 void VisitForStmt(ForStmt* F) {
1777 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001778 if (F->getInit()) OS << "...";
1779 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001780 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001781 OS << "; ";
1782 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001783 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001784 }
Mike Stump31feda52009-07-17 01:31:16 +00001785
Ted Kremenek9aae5132007-08-23 21:42:29 +00001786 void VisitWhileStmt(WhileStmt* W) {
1787 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001788 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001789 }
Mike Stump31feda52009-07-17 01:31:16 +00001790
Ted Kremenek9aae5132007-08-23 21:42:29 +00001791 void VisitDoStmt(DoStmt* D) {
1792 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001793 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001794 }
Mike Stump31feda52009-07-17 01:31:16 +00001795
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001796 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001797 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001798 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001799 }
Mike Stump31feda52009-07-17 01:31:16 +00001800
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001801 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001802 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001803 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001804 }
Mike Stump31feda52009-07-17 01:31:16 +00001805
Ted Kremenek391f94a2007-08-31 22:29:13 +00001806 void VisitChooseExpr(ChooseExpr* C) {
1807 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001808 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001809 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001810 }
Mike Stump31feda52009-07-17 01:31:16 +00001811
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001812 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1813 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001814 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001815 }
Mike Stump31feda52009-07-17 01:31:16 +00001816
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001817 void VisitBinaryOperator(BinaryOperator* B) {
1818 if (!B->isLogicalOp()) {
1819 VisitExpr(B);
1820 return;
1821 }
Mike Stump31feda52009-07-17 01:31:16 +00001822
Douglas Gregor7de59662009-05-29 20:38:28 +00001823 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001824
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001825 switch (B->getOpcode()) {
1826 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001827 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001828 return;
1829 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001830 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001831 return;
1832 default:
1833 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001834 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001835 }
Mike Stump31feda52009-07-17 01:31:16 +00001836
Ted Kremenek12687ff2007-08-27 21:54:41 +00001837 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001838 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001839 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001840};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001841} // end anonymous namespace
1842
Mike Stump31feda52009-07-17 01:31:16 +00001843
Chris Lattnerc61089a2009-06-30 01:26:17 +00001844static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1845 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001846 if (Helper) {
1847 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001848 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001849 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001850
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001851 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001852 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001853 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001854 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001855 return;
1856 }
1857 }
Mike Stump31feda52009-07-17 01:31:16 +00001858
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001859 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001860 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001861 if (B->getOpcode() == BinaryOperator::Comma) {
1862 OS << "... , ";
1863 Helper->handledStmt(B->getRHS(),OS);
1864 OS << '\n';
1865 return;
Mike Stump31feda52009-07-17 01:31:16 +00001866 }
1867 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001868 }
Mike Stump31feda52009-07-17 01:31:16 +00001869
Chris Lattnerc61089a2009-06-30 01:26:17 +00001870 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001871
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001872 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001873 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001874}
Mike Stump31feda52009-07-17 01:31:16 +00001875
Chris Lattnerc61089a2009-06-30 01:26:17 +00001876static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1877 const CFGBlock& B,
1878 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001879
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001880 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001881
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001882 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001883 OS << "\n [ B" << B.getBlockID();
1884
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001885 if (&B == &cfg->getEntry())
1886 OS << " (ENTRY) ]\n";
1887 else if (&B == &cfg->getExit())
1888 OS << " (EXIT) ]\n";
1889 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001890 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001891 else
1892 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001893
Ted Kremenek71eca012007-08-29 23:20:49 +00001894 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001895 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001896
1897 if (print_edges)
1898 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001899
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001900 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001901 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001902 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001903 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001904 C->getLHS()->printPretty(OS, Helper,
1905 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001906 if (C->getRHS()) {
1907 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001908 C->getRHS()->printPretty(OS, Helper,
1909 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001910 }
Mike Stump31feda52009-07-17 01:31:16 +00001911 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001912 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001913 else
1914 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001915
Ted Kremenek71eca012007-08-29 23:20:49 +00001916 OS << ":\n";
1917 }
Mike Stump31feda52009-07-17 01:31:16 +00001918
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001919 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001920 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001921
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001922 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1923 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001924
Ted Kremenek71eca012007-08-29 23:20:49 +00001925 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001926 if (print_edges)
1927 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001928
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001929 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001930
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001931 if (Helper)
1932 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001933
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001934 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001935 }
Mike Stump31feda52009-07-17 01:31:16 +00001936
Ted Kremenek71eca012007-08-29 23:20:49 +00001937 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001938 if (B.getTerminator()) {
1939 if (print_edges)
1940 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001941
Ted Kremenek71eca012007-08-29 23:20:49 +00001942 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001943
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001944 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001945
Chris Lattnerc61089a2009-06-30 01:26:17 +00001946 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1947 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001948 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001949 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001950 }
Mike Stump31feda52009-07-17 01:31:16 +00001951
Ted Kremenek71eca012007-08-29 23:20:49 +00001952 if (print_edges) {
1953 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001954 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001955 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001956
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001957 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1958 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001959
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001960 if (i == 8 || (i-8) == 0)
1961 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001962
Ted Kremenek71eca012007-08-29 23:20:49 +00001963 OS << " B" << (*I)->getBlockID();
1964 }
Mike Stump31feda52009-07-17 01:31:16 +00001965
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001966 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001967
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001968 // Print the successors of this block.
1969 OS << " Successors (" << B.succ_size() << "):";
1970 i = 0;
1971
1972 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1973 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001974
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001975 if (i == 8 || (i-8) % 10 == 0)
1976 OS << "\n ";
1977
Mike Stump0d76d072009-07-20 23:24:15 +00001978 if (*I)
1979 OS << " B" << (*I)->getBlockID();
1980 else
1981 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001982 }
Mike Stump31feda52009-07-17 01:31:16 +00001983
Ted Kremenek71eca012007-08-29 23:20:49 +00001984 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001985 }
Mike Stump31feda52009-07-17 01:31:16 +00001986}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001987
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001988
1989/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001990void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001991
1992/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001993void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1994 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001995
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001996 // Print the entry block.
1997 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001998
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001999 // Iterate through the CFGBlocks and print them one by one.
2000 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2001 // Skip the entry block, because we already printed it.
2002 if (&(*I) == &getEntry() || &(*I) == &getExit())
2003 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002004
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002005 print_block(OS, this, *I, &Helper, true);
2006 }
Mike Stump31feda52009-07-17 01:31:16 +00002007
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002008 // Print the exit block.
2009 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002010 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002011}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002012
2013/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002014void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2015 print(llvm::errs(), cfg, LO);
2016}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002017
2018/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2019/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002020void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2021 const LangOptions &LO) const {
2022 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002023 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002024}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002025
Ted Kremenek15647632008-01-30 23:02:42 +00002026/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002027void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002028 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002029 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002030 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2031}
2032
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002033Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002034
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002035 if (!Terminator)
2036 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002037
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002038 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002039
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002040 switch (Terminator->getStmtClass()) {
2041 default:
2042 break;
Mike Stump31feda52009-07-17 01:31:16 +00002043
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002044 case Stmt::ForStmtClass:
2045 E = cast<ForStmt>(Terminator)->getCond();
2046 break;
Mike Stump31feda52009-07-17 01:31:16 +00002047
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002048 case Stmt::WhileStmtClass:
2049 E = cast<WhileStmt>(Terminator)->getCond();
2050 break;
Mike Stump31feda52009-07-17 01:31:16 +00002051
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002052 case Stmt::DoStmtClass:
2053 E = cast<DoStmt>(Terminator)->getCond();
2054 break;
Mike Stump31feda52009-07-17 01:31:16 +00002055
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002056 case Stmt::IfStmtClass:
2057 E = cast<IfStmt>(Terminator)->getCond();
2058 break;
Mike Stump31feda52009-07-17 01:31:16 +00002059
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002060 case Stmt::ChooseExprClass:
2061 E = cast<ChooseExpr>(Terminator)->getCond();
2062 break;
Mike Stump31feda52009-07-17 01:31:16 +00002063
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002064 case Stmt::IndirectGotoStmtClass:
2065 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2066 break;
Mike Stump31feda52009-07-17 01:31:16 +00002067
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002068 case Stmt::SwitchStmtClass:
2069 E = cast<SwitchStmt>(Terminator)->getCond();
2070 break;
Mike Stump31feda52009-07-17 01:31:16 +00002071
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002072 case Stmt::ConditionalOperatorClass:
2073 E = cast<ConditionalOperator>(Terminator)->getCond();
2074 break;
Mike Stump31feda52009-07-17 01:31:16 +00002075
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002076 case Stmt::BinaryOperatorClass: // '&&' and '||'
2077 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002078 break;
Mike Stump31feda52009-07-17 01:31:16 +00002079
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002080 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002081 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002082 }
Mike Stump31feda52009-07-17 01:31:16 +00002083
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002084 return E ? E->IgnoreParens() : NULL;
2085}
2086
Ted Kremenek92137a32008-05-16 16:06:00 +00002087bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002088
Ted Kremenek92137a32008-05-16 16:06:00 +00002089 if (!Terminator)
2090 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002091
Ted Kremenek92137a32008-05-16 16:06:00 +00002092 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002093
Ted Kremenek92137a32008-05-16 16:06:00 +00002094 switch (Terminator->getStmtClass()) {
2095 default:
2096 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002097
2098 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002099 case Stmt::WhileStmtClass:
2100 case Stmt::DoStmtClass:
2101 case Stmt::IfStmtClass:
2102 case Stmt::ChooseExprClass:
2103 case Stmt::ConditionalOperatorClass:
2104 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002105 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002106 }
Mike Stump31feda52009-07-17 01:31:16 +00002107
Ted Kremenek92137a32008-05-16 16:06:00 +00002108 return E ? E->IgnoreParens() : NULL;
2109}
2110
Ted Kremenek15647632008-01-30 23:02:42 +00002111
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002112//===----------------------------------------------------------------------===//
2113// CFG Graphviz Visualization
2114//===----------------------------------------------------------------------===//
2115
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002116
2117#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002118static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002119#endif
2120
Chris Lattnerc61089a2009-06-30 01:26:17 +00002121void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002122#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002123 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002124 GraphHelper = &H;
2125 llvm::ViewGraph(this,"CFG");
2126 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002127#endif
2128}
2129
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002130namespace llvm {
2131template<>
2132struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson4d9e93c2009-06-24 17:37:55 +00002133 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
2134 bool ShortNames) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002135
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002136#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002137 std::string OutSStr;
2138 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002139 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002140 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002141
2142 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2143
2144 // Process string output to make it nicer...
2145 for (unsigned i = 0; i != OutStr.length(); ++i)
2146 if (OutStr[i] == '\n') { // Left justify
2147 OutStr[i] = '\\';
2148 OutStr.insert(OutStr.begin()+i+1, 'l');
2149 }
Mike Stump31feda52009-07-17 01:31:16 +00002150
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002151 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002152#else
2153 return "";
2154#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002155 }
2156};
2157} // end namespace llvm