blob: b445acf70f2409cd31f1cc99d39b7c3ab2160152 [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///
Mike Stump31feda52009-07-17 01:31:16 +000064class VISIBILITY_HIDDEN CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000065 CFG* cfg;
66 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000067 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000068 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000069 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000070 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000071 CFGBlock* DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +000072
Ted Kremenekeda180e22007-08-28 19:26:49 +000073 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000074 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
75 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000076
77 // A list of blocks that end with a "goto" that must be backpatched to their
78 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000079 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000080 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000081
Ted Kremenekeda180e22007-08-28 19:26:49 +000082 // A list of labels whose address has been taken (for indirect gotos).
83 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
84 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000085
86public:
Ted Kremenek889073f2007-08-23 16:51:22 +000087 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000088 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek654c78f2008-02-13 22:05:39 +000089 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000090 // Create an empty CFG.
Mike Stump31feda52009-07-17 01:31:16 +000091 cfg = new CFG();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000092 }
Mike Stump31feda52009-07-17 01:31:16 +000093
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000094 ~CFGBuilder() { delete cfg; }
Mike Stump31feda52009-07-17 01:31:16 +000095
Ted Kremenek9aae5132007-08-23 21:42:29 +000096 // buildCFG - Used by external clients to construct the CFG.
97 CFG* buildCFG(Stmt* Statement);
Mike Stump31feda52009-07-17 01:31:16 +000098
99 // Visitors to walk an AST and construct the CFG. Called by buildCFG. Do not
100 // call directly!
101
Ted Kremenek9aae5132007-08-23 21:42:29 +0000102 CFGBlock* VisitBreakStmt(BreakStmt* B);
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000103 CFGBlock* VisitCaseStmt(CaseStmt* Terminator);
Ted Kremenek9d56e642008-11-11 17:10:00 +0000104 CFGBlock* VisitCompoundStmt(CompoundStmt* C);
105 CFGBlock* VisitContinueStmt(ContinueStmt* C);
Ted Kremenek9682be12008-02-13 21:46:34 +0000106 CFGBlock* VisitDefaultStmt(DefaultStmt* D);
Ted Kremenek9d56e642008-11-11 17:10:00 +0000107 CFGBlock* VisitDoStmt(DoStmt* D);
108 CFGBlock* VisitForStmt(ForStmt* F);
109 CFGBlock* VisitGotoStmt(GotoStmt* G);
110 CFGBlock* VisitIfStmt(IfStmt* I);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000111 CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I);
Ted Kremenek9d56e642008-11-11 17:10:00 +0000112 CFGBlock* VisitLabelStmt(LabelStmt* L);
113 CFGBlock* VisitNullStmt(NullStmt* Statement);
114 CFGBlock* VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
115 CFGBlock* VisitReturnStmt(ReturnStmt* R);
116 CFGBlock* VisitStmt(Stmt* Statement);
117 CFGBlock* VisitSwitchStmt(SwitchStmt* Terminator);
118 CFGBlock* VisitWhileStmt(WhileStmt* W);
Mike Stump48871a22009-07-17 01:04:31 +0000119
Ted Kremenekb64d1832008-03-13 03:04:22 +0000120 // FIXME: Add support for ObjC-specific control-flow structures.
Mike Stump48871a22009-07-17 01:04:31 +0000121
Ted Kremenek6065ef62008-04-28 18:00:46 +0000122 // NYS == Not Yet Supported
123 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000124 badCFG = true;
125 return Block;
126 }
Mike Stump31feda52009-07-17 01:31:16 +0000127
Ted Kremenek89cc8ea2009-03-30 22:29:21 +0000128 CFGBlock* VisitObjCAtTryStmt(ObjCAtTryStmt* S);
Mike Stump31feda52009-07-17 01:31:16 +0000129 CFGBlock* VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
130 // FIXME: For now we pretend that @catch and the code it contains does not
131 // exit.
Ted Kremenek89cc8ea2009-03-30 22:29:21 +0000132 return Block;
133 }
134
Mike Stump31feda52009-07-17 01:31:16 +0000135 // FIXME: This is not completely supported. We basically @throw like a
136 // 'return'.
Ted Kremenek93041ba2008-12-09 20:20:09 +0000137 CFGBlock* VisitObjCAtThrowStmt(ObjCAtThrowStmt* S);
Ted Kremenek6065ef62008-04-28 18:00:46 +0000138
Ted Kremenek49805452009-05-02 01:49:13 +0000139 CFGBlock* VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S);
Mike Stump31feda52009-07-17 01:31:16 +0000140
Ted Kremenekac49ea62008-09-26 18:17:07 +0000141 // Blocks.
142 CFGBlock* VisitBlockExpr(BlockExpr* E) { return NYS(); }
Mike Stump31feda52009-07-17 01:31:16 +0000143 CFGBlock* VisitBlockDeclRefExpr(BlockDeclRefExpr* E) { return NYS(); }
144
Ted Kremenek9aae5132007-08-23 21:42:29 +0000145private:
146 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000147 CFGBlock* addStmt(Stmt* Terminator);
148 CFGBlock* WalkAST(Stmt* Terminator, bool AlwaysAddStmt);
149 CFGBlock* WalkAST_VisitChildren(Stmt* Terminator);
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000150 CFGBlock* WalkAST_VisitDeclSubExpr(Decl* D);
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000151 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* Terminator);
Ted Kremenek55957a82009-05-02 00:13:27 +0000152 bool FinishBlock(CFGBlock* B);
Mike Stump31feda52009-07-17 01:31:16 +0000153
Ted Kremenekb64d1832008-03-13 03:04:22 +0000154 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000155};
Mike Stump31feda52009-07-17 01:31:16 +0000156
Douglas Gregor4619e432008-12-05 23:32:09 +0000157// FIXME: Add support for dependent-sized array types in C++?
158// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000159static VariableArrayType* FindVA(Type* t) {
160 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
161 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
162 if (vat->getSizeExpr())
163 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000164
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000165 t = vt->getElementType().getTypePtr();
166 }
Mike Stump31feda52009-07-17 01:31:16 +0000167
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000168 return 0;
169}
Mike Stump31feda52009-07-17 01:31:16 +0000170
171/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
172/// arbitrary statement. Examples include a single expression or a function
173/// body (compound statement). The ownership of the returned CFG is
174/// transferred to the caller. If CFG construction fails, this method returns
175/// NULL.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000176CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenekeda180e22007-08-28 19:26:49 +0000177 assert (cfg);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000178 if (!Statement) return NULL;
179
Ted Kremenekb64d1832008-03-13 03:04:22 +0000180 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000181
182 // Create an empty block that will serve as the exit block for the CFG. Since
183 // this is the first block added to the CFG, it will be implicitly registered
184 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000185 Succ = createBlock();
186 assert (Succ == &cfg->getExit());
187 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000188
Ted Kremenek9aae5132007-08-23 21:42:29 +0000189 // Visit the statements and create the CFG.
Ted Kremenek40d87632008-02-27 17:33:02 +0000190 CFGBlock* B = Visit(Statement);
191 if (!B) B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000192
Ted Kremenek40d87632008-02-27 17:33:02 +0000193 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000194 // Finalize the last constructed block. This usually involves reversing the
195 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000196 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000197
198 // Backpatch the gotos whose label -> block mappings we didn't know when we
199 // encountered them.
200 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000201 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000202
Ted Kremenek9aae5132007-08-23 21:42:29 +0000203 CFGBlock* B = *I;
204 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
205 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
206
207 // If there is no target for the goto, then we are looking at an
208 // incomplete AST. Handle this by not registering a successor.
209 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000210
211 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000212 }
Mike Stump31feda52009-07-17 01:31:16 +0000213
Ted Kremenekeda180e22007-08-28 19:26:49 +0000214 // Add successors to the Indirect Goto Dispatch block (if we have one).
215 if (CFGBlock* B = cfg->getIndirectGotoBlock())
216 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
217 E = AddressTakenLabels.end(); I != E; ++I ) {
218
219 // Lookup the target block.
220 LabelMapTy::iterator LI = LabelMap.find(*I);
221
222 // If there is no target block that contains label, then we are looking
223 // at an incomplete AST. Handle this by not registering a successor.
224 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000225
226 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000227 }
Mike Stump31feda52009-07-17 01:31:16 +0000228
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000229 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000230 }
Mike Stump31feda52009-07-17 01:31:16 +0000231
232 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000233 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000234
Ted Kremenekb64d1832008-03-13 03:04:22 +0000235 if (badCFG) {
236 delete cfg;
237 cfg = NULL;
238 return NULL;
239 }
Mike Stump31feda52009-07-17 01:31:16 +0000240
241 // NULL out cfg so that repeated calls to the builder will fail and that the
242 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000243 CFG* t = cfg;
244 cfg = NULL;
245 return t;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000246}
Mike Stump31feda52009-07-17 01:31:16 +0000247
Ted Kremenek9aae5132007-08-23 21:42:29 +0000248/// createBlock - Used to lazily create blocks that are connected
249/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000250CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000251 CFGBlock* B = cfg->createBlock();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000252 if (add_successor && Succ) B->addSuccessor(Succ);
253 return B;
254}
Mike Stump31feda52009-07-17 01:31:16 +0000255
256/// FinishBlock - When the last statement has been added to the block, we must
257/// reverse the statements because they have been inserted in reverse order.
Ted Kremenek55957a82009-05-02 00:13:27 +0000258bool CFGBuilder::FinishBlock(CFGBlock* B) {
259 if (badCFG)
260 return false;
261
Ted Kremenek9aae5132007-08-23 21:42:29 +0000262 assert (B);
Ted Kremenek81e14852007-08-27 19:46:09 +0000263 B->reverseStmts();
Ted Kremenek55957a82009-05-02 00:13:27 +0000264 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000265}
266
Mike Stump31feda52009-07-17 01:31:16 +0000267/// addStmt - Used to add statements/expressions to the current CFGBlock
Ted Kremenek9e248872007-08-27 21:27:44 +0000268/// "Block". This method calls WalkAST on the passed statement to see if it
Mike Stump31feda52009-07-17 01:31:16 +0000269/// contains any short-circuit expressions. If so, it recursively creates the
270/// necessary blocks for such expressions. It returns the "topmost" block of
271/// the created blocks, or the original value of "Block" when this method was
272/// called if no additional blocks are created.
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000273CFGBlock* CFGBuilder::addStmt(Stmt* Terminator) {
Ted Kremeneke9610502007-08-30 18:39:40 +0000274 if (!Block) Block = createBlock();
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000275 return WalkAST(Terminator,true);
Ted Kremenek9e248872007-08-27 21:27:44 +0000276}
277
Mike Stump31feda52009-07-17 01:31:16 +0000278/// WalkAST - Used by addStmt to walk the subtree of a statement and add extra
279/// blocks for ternary operators, &&, and ||. We also process "," and
280/// DeclStmts (which may contain nested control-flow).
Mike Stump48871a22009-07-17 01:04:31 +0000281CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000282 switch (Terminator->getStmtClass()) {
Mike Stump31feda52009-07-17 01:31:16 +0000283 case Stmt::ConditionalOperatorClass: {
284 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Ted Kremenek13898872007-11-26 18:20:26 +0000285
Mike Stump31feda52009-07-17 01:31:16 +0000286 // Create the confluence block that will "merge" the results of the ternary
287 // expression.
288 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
289 ConfluenceBlock->appendStmt(C);
290 if (!FinishBlock(ConfluenceBlock))
291 return 0;
Ted Kremenek13898872007-11-26 18:20:26 +0000292
Mike Stump31feda52009-07-17 01:31:16 +0000293 // Create a block for the LHS expression if there is an LHS expression. A
294 // GCC extension allows LHS to be NULL, causing the condition to be the
295 // value that is returned instead.
296 // e.g: x ?: y is shorthand for: x ? x : y;
297 Succ = ConfluenceBlock;
298 Block = NULL;
299 CFGBlock* LHSBlock = NULL;
300 if (C->getLHS()) {
301 LHSBlock = Visit(C->getLHS());
Ted Kremenek55957a82009-05-02 00:13:27 +0000302 if (!FinishBlock(LHSBlock))
303 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000304 Block = NULL;
305 }
Ted Kremenek3dd952b2007-09-11 21:29:43 +0000306
Mike Stump31feda52009-07-17 01:31:16 +0000307 // Create the block for the RHS expression.
308 Succ = ConfluenceBlock;
309 CFGBlock* RHSBlock = Visit(C->getRHS());
310 if (!FinishBlock(RHSBlock))
311 return 0;
312
313 // Create the block that will contain the condition.
314 Block = createBlock(false);
315
316 if (LHSBlock)
317 Block->addSuccessor(LHSBlock);
318 else {
319 // If we have no LHS expression, add the ConfluenceBlock as a direct
320 // successor for the block containing the condition. Moreover, we need to
321 // reverse the order of the predecessors in the ConfluenceBlock because
322 // the RHSBlock will have been added to the succcessors already, and we
323 // want the first predecessor to the the block containing the expression
324 // for the case when the ternary expression evaluates to true.
325 Block->addSuccessor(ConfluenceBlock);
326 assert (ConfluenceBlock->pred_size() == 2);
327 std::reverse(ConfluenceBlock->pred_begin(),
328 ConfluenceBlock->pred_end());
329 }
330
331 Block->addSuccessor(RHSBlock);
332
333 Block->setTerminator(C);
334 return addStmt(C->getCond());
335 }
336
337 case Stmt::ChooseExprClass: {
338 ChooseExpr* C = cast<ChooseExpr>(Terminator);
339
340 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
341 ConfluenceBlock->appendStmt(C);
342 if (!FinishBlock(ConfluenceBlock))
343 return 0;
344
345 Succ = ConfluenceBlock;
346 Block = NULL;
347 CFGBlock* LHSBlock = Visit(C->getLHS());
348 if (!FinishBlock(LHSBlock))
349 return 0;
350
351 Succ = ConfluenceBlock;
352 Block = NULL;
353 CFGBlock* RHSBlock = Visit(C->getRHS());
354 if (!FinishBlock(RHSBlock))
355 return 0;
356
357 Block = createBlock(false);
358 Block->addSuccessor(LHSBlock);
359 Block->addSuccessor(RHSBlock);
360 Block->setTerminator(C);
361 return addStmt(C->getCond());
362 }
363
364 case Stmt::DeclStmtClass: {
365 DeclStmt *DS = cast<DeclStmt>(Terminator);
366 if (DS->isSingleDecl()) {
367 Block->appendStmt(Terminator);
368 return WalkAST_VisitDeclSubExpr(DS->getSingleDecl());
369 }
370
371 CFGBlock* B = 0;
372
373 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
374 typedef llvm::SmallVector<Decl*,10> BufTy;
375 BufTy Buf(DS->decl_begin(), DS->decl_end());
376
377 for (BufTy::reverse_iterator I=Buf.rbegin(), E=Buf.rend(); I!=E; ++I) {
378 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
379 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
380 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
381
382 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
383 // automatically freed with the CFG.
384 DeclGroupRef DG(*I);
385 Decl* D = *I;
386 void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
387
388 DeclStmt* DS = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
389
390 // Append the fake DeclStmt to block.
391 Block->appendStmt(DS);
392 B = WalkAST_VisitDeclSubExpr(D);
393 }
394 return B;
395 }
396
397 case Stmt::AddrLabelExprClass: {
398 AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
399 AddressTakenLabels.insert(A->getLabel());
400
401 if (AlwaysAddStmt) Block->appendStmt(Terminator);
402 return Block;
403 }
404
405 case Stmt::StmtExprClass:
406 return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
407
408 case Stmt::SizeOfAlignOfExprClass: {
409 SizeOfAlignOfExpr* E = cast<SizeOfAlignOfExpr>(Terminator);
410
411 // VLA types have expressions that must be evaluated.
412 if (E->isArgumentType()) {
413 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
414 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
415 addStmt(VA->getSizeExpr());
416 }
417 // Expressions in sizeof/alignof are not evaluated and thus have no
418 // control flow.
419 else
420 Block->appendStmt(Terminator);
421
422 return Block;
423 }
424
425 case Stmt::BinaryOperatorClass: {
426 BinaryOperator* B = cast<BinaryOperator>(Terminator);
427
428 if (B->isLogicalOp()) { // && or ||
429 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
430 ConfluenceBlock->appendStmt(B);
431 if (!FinishBlock(ConfluenceBlock))
432 return 0;
433
434 // create the block evaluating the LHS
435 CFGBlock* LHSBlock = createBlock(false);
436 LHSBlock->setTerminator(B);
437
438 // create the block evaluating the RHS
Ted Kremenek527ec812007-08-31 17:03:41 +0000439 Succ = ConfluenceBlock;
440 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000441 CFGBlock* RHSBlock = Visit(B->getRHS());
Ted Kremenek55957a82009-05-02 00:13:27 +0000442 if (!FinishBlock(RHSBlock))
443 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000444
445 // Now link the LHSBlock with RHSBlock.
446 if (B->getOpcode() == BinaryOperator::LOr) {
447 LHSBlock->addSuccessor(ConfluenceBlock);
448 LHSBlock->addSuccessor(RHSBlock);
449 } else {
450 assert (B->getOpcode() == BinaryOperator::LAnd);
451 LHSBlock->addSuccessor(RHSBlock);
452 LHSBlock->addSuccessor(ConfluenceBlock);
453 }
454
455 // Generate the blocks for evaluating the LHS.
456 Block = LHSBlock;
457 return addStmt(B->getLHS());
458 } else if (B->getOpcode() == BinaryOperator::Comma) { // ,
459 Block->appendStmt(B);
460 addStmt(B->getRHS());
461 return addStmt(B->getLHS());
Ted Kremenek527ec812007-08-31 17:03:41 +0000462 }
Ted Kremenek91e8d2a2007-08-28 16:18:58 +0000463
Mike Stump31feda52009-07-17 01:31:16 +0000464 break;
465 }
Ted Kremenekef52c642008-10-06 20:56:19 +0000466
Ted Kremenekac49ea62008-09-26 18:17:07 +0000467 // Blocks: No support for blocks ... yet
Mike Stump31feda52009-07-17 01:31:16 +0000468 case Stmt::BlockExprClass:
469 case Stmt::BlockDeclRefExprClass:
470 return NYS();
471
472 case Stmt::ParenExprClass:
473 return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
474
Mike Stump48871a22009-07-17 01:04:31 +0000475 case Stmt::CallExprClass: {
Chris Lattnerc960b3e2009-07-17 15:50:19 +0000476 // If this is a call to a no-return function, this stops the block here.
477 FunctionDecl *FD = cast<CallExpr>(Terminator)->getDirectCallee();
478 if (FD == 0 || !FD->hasAttr<NoReturnAttr>())
Mike Stump48871a22009-07-17 01:04:31 +0000479 break;
480
Chris Lattnerc960b3e2009-07-17 15:50:19 +0000481 if (Block && !FinishBlock(Block))
482 return 0;
Mike Stump48871a22009-07-17 01:04:31 +0000483
484 // Create new block with no successor for the remaining pieces.
485 Block = createBlock(false);
486 Block->appendStmt(Terminator);
487
488 // Wire this to the exit block directly.
489 Block->addSuccessor(&cfg->getExit());
490
491 return WalkAST_VisitChildren(Terminator);
492 }
493
Mike Stump31feda52009-07-17 01:31:16 +0000494 default:
Chris Lattnerc960b3e2009-07-17 15:50:19 +0000495 // TODO: We can follow objective-c methods (message sends).
Mike Stump31feda52009-07-17 01:31:16 +0000496 break;
Ted Kremenek9e248872007-08-27 21:27:44 +0000497 };
Mike Stump31feda52009-07-17 01:31:16 +0000498
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000499 if (AlwaysAddStmt) Block->appendStmt(Terminator);
500 return WalkAST_VisitChildren(Terminator);
Ted Kremenek9e248872007-08-27 21:27:44 +0000501}
Mike Stump31feda52009-07-17 01:31:16 +0000502
503/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions for
504/// initializers in Decls.
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000505CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +0000506 VarDecl* VD = dyn_cast<VarDecl>(D);
507
508 if (!VD)
Ted Kremenek7016e4d2007-11-18 20:06:01 +0000509 return Block;
Mike Stump31feda52009-07-17 01:31:16 +0000510
Ted Kremenek8889bb32008-08-06 23:20:50 +0000511 Expr* Init = VD->getInit();
Mike Stump31feda52009-07-17 01:31:16 +0000512
Ted Kremenek61a625f2008-09-26 16:26:36 +0000513 if (Init) {
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000514 // Optimization: Don't create separate block-level statements for literals.
Ted Kremenek61a625f2008-09-26 16:26:36 +0000515 switch (Init->getStmtClass()) {
516 case Stmt::IntegerLiteralClass:
517 case Stmt::CharacterLiteralClass:
518 case Stmt::StringLiteralClass:
519 break;
520 default:
521 Block = addStmt(Init);
522 }
Ted Kremenekfcf68632008-02-29 22:32:24 +0000523 }
Mike Stump31feda52009-07-17 01:31:16 +0000524
Ted Kremenek61a625f2008-09-26 16:26:36 +0000525 // If the type of VD is a VLA, then we must process its size expressions.
526 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
527 VA = FindVA(VA->getElementType().getTypePtr()))
Mike Stump31feda52009-07-17 01:31:16 +0000528 Block = addStmt(VA->getSizeExpr());
529
Ted Kremenek25fb6812007-08-28 18:14:37 +0000530 return Block;
531}
532
Mike Stump31feda52009-07-17 01:31:16 +0000533/// WalkAST_VisitChildren - Utility method to call WalkAST on the children of a
534/// Stmt.
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000535CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +0000536 CFGBlock* B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000537 for (Stmt::child_iterator I = Terminator->child_begin(),
538 E = Terminator->child_end();
Ted Kremenek9e248872007-08-27 21:27:44 +0000539 I != E; ++I)
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000540 if (*I) B = WalkAST(*I);
Mike Stump31feda52009-07-17 01:31:16 +0000541
Ted Kremenek9e248872007-08-27 21:27:44 +0000542 return B;
543}
544
Ted Kremenek105efce2007-08-28 18:30:10 +0000545/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
546/// expressions (a GCC extension).
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000547CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
548 Block->appendStmt(Terminator);
Mike Stump31feda52009-07-17 01:31:16 +0000549 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek105efce2007-08-28 18:30:10 +0000550}
551
Ted Kremenek9aae5132007-08-23 21:42:29 +0000552/// VisitStmt - Handle statements with no branching control flow.
553CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
Mike Stump31feda52009-07-17 01:31:16 +0000554 // We cannot assume that we are in the middle of a basic block, since the CFG
555 // might only be constructed for this single statement. If we have no current
556 // basic block, just create one lazily.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000557 if (!Block) Block = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000558
559 // Simply add the statement to the current block. We actually insert
560 // statements in reverse order; this order is reversed later when processing
561 // the containing element in the AST.
Ted Kremenek81e14852007-08-27 19:46:09 +0000562 addStmt(Statement);
563
Ted Kremenek9aae5132007-08-23 21:42:29 +0000564 return Block;
565}
566
567CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
568 return Block;
569}
570
571CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump31feda52009-07-17 01:31:16 +0000572
Ted Kremenek0b0f2062009-07-03 00:10:50 +0000573 CFGBlock* LastBlock = Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000574
Ted Kremenekf6998822008-02-26 00:22:58 +0000575 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
576 I != E; ++I ) {
Ted Kremenekfff4fc72008-03-17 17:19:44 +0000577 LastBlock = Visit(*I);
Ted Kremenekf6998822008-02-26 00:22:58 +0000578 }
579
Ted Kremenekfff4fc72008-03-17 17:19:44 +0000580 return LastBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000581}
582
583CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000584 // We may see an if statement in the middle of a basic block, or it may be the
585 // first statement we are processing. In either case, we create a new basic
586 // block. First, we create the blocks for the then...else statements, and
587 // then we create the block containing the if statement. If we were in the
588 // middle of a block, we stop processing that block and reverse its
589 // statements. That block is then the implicit successor for the "then" and
590 // "else" clauses.
591
592 // The block we were proccessing is now finished. Make it the successor
593 // block.
594 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000595 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000596 if (!FinishBlock(Block))
597 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000598 }
Mike Stump31feda52009-07-17 01:31:16 +0000599
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000600 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000601 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000602
Ted Kremenek9aae5132007-08-23 21:42:29 +0000603 if (Stmt* Else = I->getElse()) {
604 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000605
Ted Kremenek9aae5132007-08-23 21:42:29 +0000606 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000607 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000608 Block = NULL;
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000609 ElseBlock = Visit(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000610
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000611 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
612 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000613 else if (Block) {
614 if (!FinishBlock(ElseBlock))
615 return 0;
616 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000617 }
Mike Stump31feda52009-07-17 01:31:16 +0000618
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000619 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000620 CFGBlock* ThenBlock;
621 {
622 Stmt* Then = I->getThen();
623 assert (Then);
624 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000625 Block = NULL;
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000626 ThenBlock = Visit(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000627
Ted Kremenek1b379512009-04-01 03:52:47 +0000628 if (!ThenBlock) {
629 // We can reach here if the "then" body has all NullStmts.
630 // Create an empty block so we can distinguish between true and false
631 // branches in path-sensitive analyses.
632 ThenBlock = createBlock(false);
633 ThenBlock->addSuccessor(sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000634 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000635 if (!FinishBlock(ThenBlock))
636 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000637 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000638 }
639
Mike Stump31feda52009-07-17 01:31:16 +0000640 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000641 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000642
Ted Kremenek9aae5132007-08-23 21:42:29 +0000643 // Set the terminator of the new block to the If statement.
644 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000645
Ted Kremenek9aae5132007-08-23 21:42:29 +0000646 // Now add the successors.
647 Block->addSuccessor(ThenBlock);
648 Block->addSuccessor(ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000649
650 // Add the condition as the last statement in the new block. This may create
651 // new blocks as the condition may contain control-flow. Any newly created
652 // blocks will be pointed to be "Block".
Ted Kremenek15647632008-01-30 23:02:42 +0000653 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000654}
Mike Stump31feda52009-07-17 01:31:16 +0000655
656
Ted Kremenek9aae5132007-08-23 21:42:29 +0000657CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump31feda52009-07-17 01:31:16 +0000658 // If we were in the middle of a block we stop processing that block and
659 // reverse its statements.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000660 //
Mike Stump31feda52009-07-17 01:31:16 +0000661 // NOTE: If a "return" appears in the middle of a block, this means that the
662 // code afterwards is DEAD (unreachable). We still keep a basic block
663 // for that code; a simple "mark-and-sweep" from the entry block will be
664 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000665 if (Block) FinishBlock(Block);
666
667 // Create the new block.
668 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000669
Ted Kremenek9aae5132007-08-23 21:42:29 +0000670 // The Exit block is the only successor.
671 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000672
673 // Add the return statement to the block. This may create new blocks if R
674 // contains control-flow (short-circuit operations).
Ted Kremenek81e14852007-08-27 19:46:09 +0000675 return addStmt(R);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000676}
677
678CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
679 // Get the block of the labeled statement. Add it to our map.
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000680 Visit(L->getSubStmt());
681 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000682
Ted Kremenekf9b17562007-08-30 18:20:57 +0000683 if (!LabelBlock) // This can happen when the body is empty, i.e.
684 LabelBlock=createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000685
Ted Kremenek9aae5132007-08-23 21:42:29 +0000686 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
687 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000688
689 // Labels partition blocks, so this is the end of the basic block we were
690 // processing (L is the block's label). Because this is label (and we have
691 // already processed the substatement) there is no extra control-flow to worry
692 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000693 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000694 if (!FinishBlock(LabelBlock))
695 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000696
697 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000698 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000699
Ted Kremenek9aae5132007-08-23 21:42:29 +0000700 // This block is now the implicit successor of other blocks.
701 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000702
Ted Kremenek9aae5132007-08-23 21:42:29 +0000703 return LabelBlock;
704}
705
706CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000707 // Goto is a control-flow statement. Thus we stop processing the current
708 // block and create a new one.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000709 if (Block) FinishBlock(Block);
710 Block = createBlock(false);
711 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000712
713 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000714 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000715
Ted Kremenek9aae5132007-08-23 21:42:29 +0000716 if (I == LabelMap.end())
717 // We will need to backpatch this block later.
718 BackpatchBlocks.push_back(Block);
719 else
720 Block->addSuccessor(I->second);
721
Mike Stump31feda52009-07-17 01:31:16 +0000722 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000723}
724
725CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Mike Stump31feda52009-07-17 01:31:16 +0000726 // "for" is a control-flow statement. Thus we stop processing the current
727 // block.
728
Ted Kremenek9aae5132007-08-23 21:42:29 +0000729 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000730
Ted Kremenek9aae5132007-08-23 21:42:29 +0000731 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000732 if (!FinishBlock(Block))
733 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000734 LoopSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000735 } else LoopSuccessor = Succ;
736
737 // Because of short-circuit evaluation, the condition of the loop can span
738 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
739 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000740 CFGBlock* ExitConditionBlock = createBlock(false);
741 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000742
Ted Kremenek81e14852007-08-27 19:46:09 +0000743 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000744 ExitConditionBlock->setTerminator(F);
745
746 // Now add the actual condition to the condition block. Because the condition
747 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000748 if (Stmt* C = F->getCond()) {
749 Block = ExitConditionBlock;
750 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000751 if (Block) {
752 if (!FinishBlock(EntryConditionBlock))
753 return 0;
754 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000755 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000756
Mike Stump31feda52009-07-17 01:31:16 +0000757 // The condition block is the implicit successor for the loop body as well as
758 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000759 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000760
Ted Kremenek9aae5132007-08-23 21:42:29 +0000761 // Now create the loop body.
762 {
763 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000764
Ted Kremenek9aae5132007-08-23 21:42:29 +0000765 // Save the current values for Block, Succ, and continue and break targets
766 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
767 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000768 save_break(BreakTargetBlock);
769
Ted Kremeneke9610502007-08-30 18:39:40 +0000770 // Create a new block to contain the (bottom) of the loop body.
771 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000772
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000773 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000774 // Generate increment code in its own basic block. This is the target of
775 // continue statements.
Ted Kremeneke03879b2008-11-24 20:50:24 +0000776 Succ = Visit(I);
Mike Stump31feda52009-07-17 01:31:16 +0000777 } else {
778 // No increment code. Create a special, empty, block that is used as the
779 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000780 assert(Succ == EntryConditionBlock);
781 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000782 }
Mike Stump31feda52009-07-17 01:31:16 +0000783
Ted Kremenek902393b2009-04-28 00:51:56 +0000784 // Finish up the increment (or empty) block if it hasn't been already.
785 if (Block) {
786 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000787 if (!FinishBlock(Block))
788 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000789 Block = 0;
790 }
Mike Stump31feda52009-07-17 01:31:16 +0000791
Ted Kremenek902393b2009-04-28 00:51:56 +0000792 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000793
Ted Kremenek902393b2009-04-28 00:51:56 +0000794 // The starting block for the loop increment is the block that should
795 // represent the 'loop target' for looping back to the start of the loop.
796 ContinueTargetBlock->setLoopTarget(F);
797
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000798 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000799 BreakTargetBlock = LoopSuccessor;
800
801 // Now populate the body block, and in the process create new blocks as we
802 // walk the body of the loop.
803 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000804
805 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +0000806 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +0000807 else if (Block) {
808 if (!FinishBlock(BodyBlock))
809 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000810 }
811
Ted Kremenek81e14852007-08-27 19:46:09 +0000812 // This new body block is a successor to our "exit" condition block.
813 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000814 }
Mike Stump31feda52009-07-17 01:31:16 +0000815
816 // Link up the condition block with the code that follows the loop. (the
817 // false branch).
Ted Kremenek81e14852007-08-27 19:46:09 +0000818 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000819
Ted Kremenek9aae5132007-08-23 21:42:29 +0000820 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000821 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000822 if (Stmt* I = F->getInit()) {
823 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000824 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000825 } else {
826 // There is no loop initialization. We are thus basically a while loop.
827 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000828 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000829 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000830 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000831 }
832}
833
Ted Kremenek9d56e642008-11-11 17:10:00 +0000834CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
835 // Objective-C fast enumeration 'for' statements:
836 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
837 //
838 // for ( Type newVariable in collection_expression ) { statements }
839 //
840 // becomes:
841 //
842 // prologue:
843 // 1. collection_expression
844 // T. jump to loop_entry
845 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000846 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +0000847 // 1. ObjCForCollectionStmt [performs binding to newVariable]
848 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
849 // TB:
850 // statements
851 // T. jump to loop_entry
852 // FB:
853 // what comes after
854 //
855 // and
856 //
857 // Type existingItem;
858 // for ( existingItem in expression ) { statements }
859 //
860 // becomes:
861 //
Mike Stump31feda52009-07-17 01:31:16 +0000862 // the same with newVariable replaced with existingItem; the binding works
863 // the same except that for one ObjCForCollectionStmt::getElement() returns
864 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +0000865 //
Mike Stump31feda52009-07-17 01:31:16 +0000866
Ted Kremenek9d56e642008-11-11 17:10:00 +0000867 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +0000868
Ted Kremenek9d56e642008-11-11 17:10:00 +0000869 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000870 if (!FinishBlock(Block))
871 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +0000872 LoopSuccessor = Block;
873 Block = 0;
Mike Stump31feda52009-07-17 01:31:16 +0000874 } else LoopSuccessor = Succ;
875
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000876 // Build the condition blocks.
877 CFGBlock* ExitConditionBlock = createBlock(false);
878 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000879
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000880 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000881 ExitConditionBlock->setTerminator(S);
882
883 // The last statement in the block should be the ObjCForCollectionStmt, which
884 // performs the actual binding to 'element' and determines if there are any
885 // more items in the collection.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000886 ExitConditionBlock->appendStmt(S);
887 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000888
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000889 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +0000890 // generate new blocks as necesary. We DON'T add the statement by default to
891 // the CFG unless it contains control-flow.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000892 EntryConditionBlock = WalkAST(S->getElement(), false);
Mike Stump31feda52009-07-17 01:31:16 +0000893 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000894 if (!FinishBlock(EntryConditionBlock))
895 return 0;
896 Block = 0;
897 }
Mike Stump31feda52009-07-17 01:31:16 +0000898
899 // The condition block is the implicit successor for the loop body as well as
900 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000901 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000902
Ted Kremenek9d56e642008-11-11 17:10:00 +0000903 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +0000904 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000905 // Save the current values for Succ, continue and break targets.
906 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +0000907 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
908
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000909 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +0000910 ContinueTargetBlock = EntryConditionBlock;
911
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000912 CFGBlock* BodyBlock = Visit(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000913
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000914 if (!BodyBlock)
915 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +0000916 else if (Block) {
917 if (!FinishBlock(BodyBlock))
918 return 0;
919 }
Mike Stump31feda52009-07-17 01:31:16 +0000920
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000921 // This new body block is a successor to our "exit" condition block.
922 ExitConditionBlock->addSuccessor(BodyBlock);
923 }
Mike Stump31feda52009-07-17 01:31:16 +0000924
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000925 // Link up the condition block with the code that follows the loop.
926 // (the false branch).
927 ExitConditionBlock->addSuccessor(LoopSuccessor);
928
Ted Kremenek9d56e642008-11-11 17:10:00 +0000929 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000930 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +0000931 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +0000932}
933
Ted Kremenek49805452009-05-02 01:49:13 +0000934CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
935 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +0000936
Ted Kremenek49805452009-05-02 01:49:13 +0000937 // Inline the body.
Ted Kremenekb3c657b2009-05-05 23:11:51 +0000938 CFGBlock *SyncBlock = Visit(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +0000939
Ted Kremenekb3c657b2009-05-05 23:11:51 +0000940 // The sync body starts its own basic block. This makes it a little easier
941 // for diagnostic clients.
942 if (SyncBlock) {
943 if (!FinishBlock(SyncBlock))
944 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000945
Ted Kremenekb3c657b2009-05-05 23:11:51 +0000946 Block = 0;
947 }
Mike Stump31feda52009-07-17 01:31:16 +0000948
Ted Kremenekb3c657b2009-05-05 23:11:51 +0000949 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000950
Ted Kremenek49805452009-05-02 01:49:13 +0000951 // Inline the sync expression.
952 return Visit(S->getSynchExpr());
953}
Mike Stump31feda52009-07-17 01:31:16 +0000954
Ted Kremenek89cc8ea2009-03-30 22:29:21 +0000955CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek89be6522009-04-07 04:26:02 +0000956 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +0000957}
Ted Kremenek9d56e642008-11-11 17:10:00 +0000958
Ted Kremenek9aae5132007-08-23 21:42:29 +0000959CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Mike Stump31feda52009-07-17 01:31:16 +0000960 // "while" is a control-flow statement. Thus we stop processing the current
961 // block.
962
Ted Kremenek9aae5132007-08-23 21:42:29 +0000963 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000964
Ted Kremenek9aae5132007-08-23 21:42:29 +0000965 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000966 if (!FinishBlock(Block))
967 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000968 LoopSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000969 } else LoopSuccessor = Succ;
970
971 // Because of short-circuit evaluation, the condition of the loop can span
972 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
973 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000974 CFGBlock* ExitConditionBlock = createBlock(false);
975 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000976
Ted Kremenek81e14852007-08-27 19:46:09 +0000977 // Set the terminator for the "exit" condition block.
978 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +0000979
980 // Now add the actual condition to the condition block. Because the condition
981 // itself may contain control-flow, new blocks may be created. Thus we update
982 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000983 if (Stmt* C = W->getCond()) {
984 Block = ExitConditionBlock;
985 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +0000986 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +0000987 if (Block) {
988 if (!FinishBlock(EntryConditionBlock))
989 return 0;
990 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000991 }
Mike Stump31feda52009-07-17 01:31:16 +0000992
993 // The condition block is the implicit successor for the loop body as well as
994 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000995 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000996
Ted Kremenek9aae5132007-08-23 21:42:29 +0000997 // Process the loop body.
998 {
Ted Kremenek49936f72009-04-28 03:09:44 +0000999 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001000
1001 // Save the current values for Block, Succ, and continue and break targets
1002 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1003 save_continue(ContinueTargetBlock),
1004 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001005
Mike Stump31feda52009-07-17 01:31:16 +00001006 // Create an empty block to represent the transition block for looping back
1007 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001008 Block = 0;
1009 assert(Succ == EntryConditionBlock);
1010 Succ = createBlock();
1011 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001012 ContinueTargetBlock = Succ;
1013
Ted Kremenek9aae5132007-08-23 21:42:29 +00001014 // All breaks should go to the code following the loop.
1015 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001016
Ted Kremenek9aae5132007-08-23 21:42:29 +00001017 // NULL out Block to force lazy instantiation of blocks for the body.
1018 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001019
Ted Kremenek9aae5132007-08-23 21:42:29 +00001020 // Create the body. The returned block is the entry to the loop body.
1021 CFGBlock* BodyBlock = Visit(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001022
Ted Kremeneke9610502007-08-30 18:39:40 +00001023 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001024 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001025 else if (Block) {
1026 if (!FinishBlock(BodyBlock))
1027 return 0;
1028 }
Mike Stump31feda52009-07-17 01:31:16 +00001029
Ted Kremenek9aae5132007-08-23 21:42:29 +00001030 // Add the loop body entry as a successor to the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001031 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001032 }
Mike Stump31feda52009-07-17 01:31:16 +00001033
1034 // Link up the condition block with the code that follows the loop. (the
1035 // false branch).
Ted Kremenek81e14852007-08-27 19:46:09 +00001036 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001037
1038 // There can be no more statements in the condition block since we loop back
1039 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001040 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001041
Ted Kremenek9aae5132007-08-23 21:42:29 +00001042 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001043 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001044 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001045}
Mike Stump31feda52009-07-17 01:31:16 +00001046
Ted Kremenek93041ba2008-12-09 20:20:09 +00001047CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1048 // FIXME: This isn't complete. We basically treat @throw like a return
1049 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001050
1051 // If we were in the middle of a block we stop processing that block and
1052 // reverse its statements.
Ted Kremenek55957a82009-05-02 00:13:27 +00001053 if (Block) {
1054 if (!FinishBlock(Block))
1055 return 0;
1056 }
Mike Stump31feda52009-07-17 01:31:16 +00001057
Ted Kremenek93041ba2008-12-09 20:20:09 +00001058 // Create the new block.
1059 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001060
Ted Kremenek93041ba2008-12-09 20:20:09 +00001061 // The Exit block is the only successor.
1062 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001063
1064 // Add the statement to the block. This may create new blocks if S contains
1065 // control-flow (short-circuit operations).
Ted Kremenek93041ba2008-12-09 20:20:09 +00001066 return addStmt(S);
1067}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001068
1069CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
1070 // "do...while" is a control-flow statement. Thus we stop processing the
1071 // current block.
Mike Stump31feda52009-07-17 01:31:16 +00001072
Ted Kremenek9aae5132007-08-23 21:42:29 +00001073 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001074
Ted Kremenek9aae5132007-08-23 21:42:29 +00001075 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001076 if (!FinishBlock(Block))
1077 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001078 LoopSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001079 } else LoopSuccessor = Succ;
1080
1081 // Because of short-circuit evaluation, the condition of the loop can span
1082 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1083 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001084 CFGBlock* ExitConditionBlock = createBlock(false);
1085 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001086
Ted Kremenek81e14852007-08-27 19:46:09 +00001087 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001088 ExitConditionBlock->setTerminator(D);
1089
1090 // Now add the actual condition to the condition block. Because the condition
1091 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001092 if (Stmt* C = D->getCond()) {
1093 Block = ExitConditionBlock;
1094 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001095 if (Block) {
1096 if (!FinishBlock(EntryConditionBlock))
1097 return 0;
1098 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001099 }
Mike Stump31feda52009-07-17 01:31:16 +00001100
Ted Kremeneka1523a32008-02-27 07:20:00 +00001101 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001102 Succ = EntryConditionBlock;
1103
Ted Kremenek9aae5132007-08-23 21:42:29 +00001104 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001105 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001106 {
1107 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001108
Ted Kremenek9aae5132007-08-23 21:42:29 +00001109 // Save the current values for Block, Succ, and continue and break targets
1110 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1111 save_continue(ContinueTargetBlock),
1112 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001113
Ted Kremenek9aae5132007-08-23 21:42:29 +00001114 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001115 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001116
Ted Kremenek9aae5132007-08-23 21:42:29 +00001117 // All breaks should go to the code following the loop.
1118 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001119
Ted Kremenek9aae5132007-08-23 21:42:29 +00001120 // NULL out Block to force lazy instantiation of blocks for the body.
1121 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001122
Ted Kremenek9aae5132007-08-23 21:42:29 +00001123 // Create the body. The returned block is the entry to the loop body.
1124 BodyBlock = Visit(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001125
Ted Kremeneke9610502007-08-30 18:39:40 +00001126 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001127 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001128 else if (Block) {
1129 if (!FinishBlock(BodyBlock))
1130 return 0;
1131 }
Mike Stump31feda52009-07-17 01:31:16 +00001132
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001133 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001134 // ExitConditionBlock to represent the "loop back" transition. Create an
1135 // empty block to represent the transition block for looping back to the
1136 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001137 // FIXME: Can we do this more efficiently without adding another block?
1138 Block = NULL;
1139 Succ = BodyBlock;
1140 CFGBlock *LoopBackBlock = createBlock();
1141 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001142
Ted Kremenek9aae5132007-08-23 21:42:29 +00001143 // Add the loop body entry as a successor to the condition.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001144 ExitConditionBlock->addSuccessor(LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001145 }
Mike Stump31feda52009-07-17 01:31:16 +00001146
Ted Kremenek9aae5132007-08-23 21:42:29 +00001147 // Link up the condition block with the code that follows the loop.
1148 // (the false branch).
Ted Kremenek81e14852007-08-27 19:46:09 +00001149 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001150
1151 // There can be no more statements in the body block(s) since we loop back to
1152 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001153 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001154
Ted Kremenek9aae5132007-08-23 21:42:29 +00001155 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001156 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001157 return BodyBlock;
1158}
1159
1160CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1161 // "continue" is a control-flow statement. Thus we stop processing the
1162 // current block.
Ted Kremenek55957a82009-05-02 00:13:27 +00001163 if (Block) {
1164 if (!FinishBlock(Block))
1165 return 0;
1166 }
Mike Stump31feda52009-07-17 01:31:16 +00001167
Ted Kremenek9aae5132007-08-23 21:42:29 +00001168 // Now create a new block that ends with the continue statement.
1169 Block = createBlock(false);
1170 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001171
Ted Kremenek9aae5132007-08-23 21:42:29 +00001172 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001173 // incomplete AST. This means the CFG cannot be constructed.
1174 if (ContinueTargetBlock)
1175 Block->addSuccessor(ContinueTargetBlock);
1176 else
1177 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001178
Ted Kremenek9aae5132007-08-23 21:42:29 +00001179 return Block;
1180}
1181
1182CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
Mike Stump31feda52009-07-17 01:31:16 +00001183 // "break" is a control-flow statement. Thus we stop processing the current
1184 // block.
Ted Kremenek55957a82009-05-02 00:13:27 +00001185 if (Block) {
1186 if (!FinishBlock(Block))
1187 return 0;
1188 }
Mike Stump31feda52009-07-17 01:31:16 +00001189
Mike Stump48871a22009-07-17 01:04:31 +00001190 // Now create a new block that ends with the break statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001191 Block = createBlock(false);
1192 Block->setTerminator(B);
Mike Stump31feda52009-07-17 01:31:16 +00001193
1194 // If there is no target for the break, then we are looking at an incomplete
1195 // AST. This means that the CFG cannot be constructed.
Ted Kremenek882cf062009-04-07 18:53:24 +00001196 if (BreakTargetBlock)
1197 Block->addSuccessor(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001198 else
Ted Kremenek882cf062009-04-07 18:53:24 +00001199 badCFG = true;
1200
Ted Kremenek9aae5132007-08-23 21:42:29 +00001201
Mike Stump31feda52009-07-17 01:31:16 +00001202 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001203}
1204
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001205CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001206 // "switch" is a control-flow statement. Thus we stop processing the current
1207 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001208 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001209
Ted Kremenek9aae5132007-08-23 21:42:29 +00001210 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001211 if (!FinishBlock(Block))
1212 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001213 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001214 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001215
1216 // Save the current "switch" context.
1217 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001218 save_break(BreakTargetBlock),
1219 save_default(DefaultCaseBlock);
1220
Mike Stump31feda52009-07-17 01:31:16 +00001221 // Set the "default" case to be the block after the switch statement. If the
1222 // switch statement contains a "default:", this value will be overwritten with
1223 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001224 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001225
Ted Kremenek9aae5132007-08-23 21:42:29 +00001226 // Create a new block that will contain the switch statement.
1227 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001228
Ted Kremenek9aae5132007-08-23 21:42:29 +00001229 // Now process the switch body. The code after the switch is the implicit
1230 // successor.
1231 Succ = SwitchSuccessor;
1232 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001233
1234 // When visiting the body, the case statements should automatically get linked
1235 // up to the switch. We also don't keep a pointer to the body, since all
1236 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001237 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001238 Block = NULL;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001239 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001240 if (Block) {
1241 if (!FinishBlock(BodyBlock))
1242 return 0;
1243 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001244
Mike Stump31feda52009-07-17 01:31:16 +00001245 // If we have no "default:" case, the default transition is to the code
1246 // following the switch body.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001247 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001248
Ted Kremenek81e14852007-08-27 19:46:09 +00001249 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001250 SwitchTerminatedBlock->setTerminator(Terminator);
1251 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001252 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001253
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001254 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001255}
1256
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001257CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001258 // CaseStmts are essentially labels, so they are the first statement in a
1259 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001260
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001261 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek55e91e82007-08-30 18:48:11 +00001262 CFGBlock* CaseBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001263 if (!CaseBlock) CaseBlock = createBlock();
1264
1265 // Cases statements partition blocks, so this is the top of the basic block we
1266 // were processing (the "case XXX:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001267 CaseBlock->setLabel(Terminator);
Ted Kremenek55957a82009-05-02 00:13:27 +00001268 if (!FinishBlock(CaseBlock))
1269 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001270
1271 // Add this block to the list of successors for the block with the switch
1272 // statement.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001273 assert (SwitchTerminatedBlock);
1274 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001275
Ted Kremenek9aae5132007-08-23 21:42:29 +00001276 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1277 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001278
Ted Kremenek9aae5132007-08-23 21:42:29 +00001279 // This block is now the implicit successor of other blocks.
1280 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001281
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001282 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001283}
Mike Stump31feda52009-07-17 01:31:16 +00001284
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001285CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1286 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek654c78f2008-02-13 22:05:39 +00001287 DefaultCaseBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001288 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1289
1290 // Default statements partition blocks, so this is the top of the basic block
1291 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001292 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek55957a82009-05-02 00:13:27 +00001293 if (!FinishBlock(DefaultCaseBlock))
1294 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001295
Mike Stump31feda52009-07-17 01:31:16 +00001296 // Unlike case statements, we don't add the default block to the successors
1297 // for the switch statement immediately. This is done when we finish
1298 // processing the switch statement. This allows for the default case
1299 // (including a fall-through to the code after the switch statement) to always
1300 // be the last successor of a switch-terminated block.
1301
Ted Kremenek654c78f2008-02-13 22:05:39 +00001302 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1303 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001304
Ted Kremenek654c78f2008-02-13 22:05:39 +00001305 // This block is now the implicit successor of other blocks.
1306 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001307
1308 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001309}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001310
Ted Kremenekeda180e22007-08-28 19:26:49 +00001311CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001312 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001313 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001314
Ted Kremenekeda180e22007-08-28 19:26:49 +00001315 if (!IBlock) {
1316 IBlock = createBlock(false);
1317 cfg->setIndirectGotoBlock(IBlock);
1318 }
Mike Stump31feda52009-07-17 01:31:16 +00001319
Ted Kremenekeda180e22007-08-28 19:26:49 +00001320 // IndirectGoto is a control-flow statement. Thus we stop processing the
1321 // current block and create a new one.
Ted Kremenek55957a82009-05-02 00:13:27 +00001322 if (Block) {
1323 if (!FinishBlock(Block))
1324 return 0;
1325 }
Ted Kremenekeda180e22007-08-28 19:26:49 +00001326 Block = createBlock(false);
1327 Block->setTerminator(I);
1328 Block->addSuccessor(IBlock);
1329 return addStmt(I->getTarget());
1330}
1331
Ted Kremenek9aae5132007-08-23 21:42:29 +00001332
Ted Kremenek04cca642007-08-23 21:26:19 +00001333} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001334
Mike Stump31feda52009-07-17 01:31:16 +00001335/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1336/// no successors or predecessors. If this is the first block created in the
1337/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001338CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001339 bool first_block = begin() == end();
1340
1341 // Create the block.
Ted Kremenek813dd672007-09-05 20:02:05 +00001342 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek889073f2007-08-23 16:51:22 +00001343
1344 // If this is the first block, set it as the Entry and Exit.
1345 if (first_block) Entry = Exit = &front();
1346
1347 // Return the block.
1348 return &front();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001349}
1350
Ted Kremenek889073f2007-08-23 16:51:22 +00001351/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1352/// CFG is returned to the caller.
1353CFG* CFG::buildCFG(Stmt* Statement) {
1354 CFGBuilder Builder;
1355 return Builder.buildCFG(Statement);
1356}
1357
1358/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001359void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1360
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001361//===----------------------------------------------------------------------===//
1362// CFG: Queries for BlkExprs.
1363//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001364
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001365namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001366 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001367}
1368
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001369static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1370 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001371 return;
Mike Stump31feda52009-07-17 01:31:16 +00001372
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001373 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001374 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001375
Ted Kremenek95a123c2008-01-26 00:03:27 +00001376 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1377 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001378
Ted Kremenek95a123c2008-01-26 00:03:27 +00001379 FindSubExprAssignments(*I, Set);
1380 }
1381}
1382
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001383static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1384 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001385
1386 // Look for assignments that are used as subexpressions. These are the only
1387 // assignments that we want to *possibly* register as a block-level
1388 // expression. Basically, if an assignment occurs both in a subexpression and
1389 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001390 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001391
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001392 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1393 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001394 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001395
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001396 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001397
1398 // Iterate over the statements again on identify the Expr* and Stmt* at the
1399 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001400
Ted Kremenek95a123c2008-01-26 00:03:27 +00001401 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001402 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001403
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001404 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001405 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001406 // expression are really "statements" whose value is never used by
1407 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001408 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001409 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001410 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1411 // Special handling for statement expressions. The last statement in
1412 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001413 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001414 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001415 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001416 (*M)[C->body_back()] = x;
1417 }
1418 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001419
Ted Kremenek95a123c2008-01-26 00:03:27 +00001420 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001421 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001422 }
Mike Stump31feda52009-07-17 01:31:16 +00001423
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001424 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001425
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001426 Stmt* S = I->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001427
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001428 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001429 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001430 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001431 }
1432 }
Mike Stump31feda52009-07-17 01:31:16 +00001433
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001434 return M;
1435}
1436
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001437CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1438 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001439 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001440
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001441 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001442 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001443
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001444 if (I == M->end()) return CFG::BlkExprNumTy();
1445 else return CFG::BlkExprNumTy(I->second);
1446}
1447
1448unsigned CFG::getNumBlkExprs() {
1449 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1450 return M->size();
1451 else {
1452 // We assume callers interested in the number of BlkExprs will want
1453 // the map constructed if it doesn't already exist.
1454 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1455 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1456 }
1457}
1458
Ted Kremenek6065ef62008-04-28 18:00:46 +00001459//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001460// Cleanup: CFG dstor.
1461//===----------------------------------------------------------------------===//
1462
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001463CFG::~CFG() {
1464 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1465}
Mike Stump31feda52009-07-17 01:31:16 +00001466
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001467//===----------------------------------------------------------------------===//
1468// CFG pretty printing
1469//===----------------------------------------------------------------------===//
1470
Ted Kremenek7e776b12007-08-22 18:22:34 +00001471namespace {
1472
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001473class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001474
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001475 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1476 StmtMapTy StmtMap;
1477 signed CurrentBlock;
1478 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001479 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001480public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001481
Chris Lattnerc61089a2009-06-30 01:26:17 +00001482 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1483 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001484 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1485 unsigned j = 1;
1486 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1487 BI != BEnd; ++BI, ++j )
1488 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1489 }
1490 }
Mike Stump31feda52009-07-17 01:31:16 +00001491
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001492 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001493
Chris Lattnerc61089a2009-06-30 01:26:17 +00001494 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001495 void setBlockID(signed i) { CurrentBlock = i; }
1496 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001497
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001498 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001499
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001500 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001501
1502 if (I == StmtMap.end())
1503 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001504
1505 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001506 && I->second.second == CurrentStmt)
1507 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001508
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001509 OS << "[B" << I->second.first << "." << I->second.second << "]";
1510 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001511 }
1512};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001513} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001514
Chris Lattnerc61089a2009-06-30 01:26:17 +00001515
1516namespace {
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001517class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1518 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001519
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001520 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001521 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001522 PrintingPolicy Policy;
1523
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001524public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001525 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001526 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001527 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001528
Ted Kremenek9aae5132007-08-23 21:42:29 +00001529 void VisitIfStmt(IfStmt* I) {
1530 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001531 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001532 }
Mike Stump31feda52009-07-17 01:31:16 +00001533
Ted Kremenek9aae5132007-08-23 21:42:29 +00001534 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001535 void VisitStmt(Stmt* Terminator) {
1536 Terminator->printPretty(OS, Helper, Policy);
1537 }
1538
Ted Kremenek9aae5132007-08-23 21:42:29 +00001539 void VisitForStmt(ForStmt* F) {
1540 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001541 if (F->getInit()) OS << "...";
1542 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001543 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001544 OS << "; ";
1545 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001546 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001547 }
Mike Stump31feda52009-07-17 01:31:16 +00001548
Ted Kremenek9aae5132007-08-23 21:42:29 +00001549 void VisitWhileStmt(WhileStmt* W) {
1550 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001551 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001552 }
Mike Stump31feda52009-07-17 01:31:16 +00001553
Ted Kremenek9aae5132007-08-23 21:42:29 +00001554 void VisitDoStmt(DoStmt* D) {
1555 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001556 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001557 }
Mike Stump31feda52009-07-17 01:31:16 +00001558
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001559 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001560 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001561 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001562 }
Mike Stump31feda52009-07-17 01:31:16 +00001563
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001564 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001565 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001566 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001567 }
Mike Stump31feda52009-07-17 01:31:16 +00001568
Ted Kremenek391f94a2007-08-31 22:29:13 +00001569 void VisitChooseExpr(ChooseExpr* C) {
1570 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001571 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001572 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001573 }
Mike Stump31feda52009-07-17 01:31:16 +00001574
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001575 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1576 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001577 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001578 }
Mike Stump31feda52009-07-17 01:31:16 +00001579
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001580 void VisitBinaryOperator(BinaryOperator* B) {
1581 if (!B->isLogicalOp()) {
1582 VisitExpr(B);
1583 return;
1584 }
Mike Stump31feda52009-07-17 01:31:16 +00001585
Douglas Gregor7de59662009-05-29 20:38:28 +00001586 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001587
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001588 switch (B->getOpcode()) {
1589 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001590 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001591 return;
1592 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001593 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001594 return;
1595 default:
1596 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001597 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001598 }
Mike Stump31feda52009-07-17 01:31:16 +00001599
Ted Kremenek12687ff2007-08-27 21:54:41 +00001600 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001601 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001602 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001603};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001604} // end anonymous namespace
1605
Mike Stump31feda52009-07-17 01:31:16 +00001606
Chris Lattnerc61089a2009-06-30 01:26:17 +00001607static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1608 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001609 if (Helper) {
1610 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001611 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001612 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001613
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001614 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001615 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001616 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001617 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001618 return;
1619 }
1620 }
Mike Stump31feda52009-07-17 01:31:16 +00001621
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001622 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001623 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001624 if (B->getOpcode() == BinaryOperator::Comma) {
1625 OS << "... , ";
1626 Helper->handledStmt(B->getRHS(),OS);
1627 OS << '\n';
1628 return;
Mike Stump31feda52009-07-17 01:31:16 +00001629 }
1630 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001631 }
Mike Stump31feda52009-07-17 01:31:16 +00001632
Chris Lattnerc61089a2009-06-30 01:26:17 +00001633 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001634
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001635 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001636 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001637}
Mike Stump31feda52009-07-17 01:31:16 +00001638
Chris Lattnerc61089a2009-06-30 01:26:17 +00001639static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1640 const CFGBlock& B,
1641 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001642
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001643 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001644
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001645 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001646 OS << "\n [ B" << B.getBlockID();
1647
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001648 if (&B == &cfg->getEntry())
1649 OS << " (ENTRY) ]\n";
1650 else if (&B == &cfg->getExit())
1651 OS << " (EXIT) ]\n";
1652 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001653 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001654 else
1655 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001656
Ted Kremenek71eca012007-08-29 23:20:49 +00001657 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001658 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001659
1660 if (print_edges)
1661 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001662
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001663 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001664 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001665 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001666 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001667 C->getLHS()->printPretty(OS, Helper,
1668 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001669 if (C->getRHS()) {
1670 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001671 C->getRHS()->printPretty(OS, Helper,
1672 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001673 }
Mike Stump31feda52009-07-17 01:31:16 +00001674 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001675 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001676 else
1677 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001678
Ted Kremenek71eca012007-08-29 23:20:49 +00001679 OS << ":\n";
1680 }
Mike Stump31feda52009-07-17 01:31:16 +00001681
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001682 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001683 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001684
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001685 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1686 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001687
Ted Kremenek71eca012007-08-29 23:20:49 +00001688 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001689 if (print_edges)
1690 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001691
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001692 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001693
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001694 if (Helper)
1695 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001696
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001697 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001698 }
Mike Stump31feda52009-07-17 01:31:16 +00001699
Ted Kremenek71eca012007-08-29 23:20:49 +00001700 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001701 if (B.getTerminator()) {
1702 if (print_edges)
1703 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001704
Ted Kremenek71eca012007-08-29 23:20:49 +00001705 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001706
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001707 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001708
Chris Lattnerc61089a2009-06-30 01:26:17 +00001709 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1710 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001711 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001712 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001713 }
Mike Stump31feda52009-07-17 01:31:16 +00001714
Ted Kremenek71eca012007-08-29 23:20:49 +00001715 if (print_edges) {
1716 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001717 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001718 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001719
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001720 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1721 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001722
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001723 if (i == 8 || (i-8) == 0)
1724 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001725
Ted Kremenek71eca012007-08-29 23:20:49 +00001726 OS << " B" << (*I)->getBlockID();
1727 }
Mike Stump31feda52009-07-17 01:31:16 +00001728
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001729 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001730
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001731 // Print the successors of this block.
1732 OS << " Successors (" << B.succ_size() << "):";
1733 i = 0;
1734
1735 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1736 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001737
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001738 if (i == 8 || (i-8) % 10 == 0)
1739 OS << "\n ";
1740
1741 OS << " B" << (*I)->getBlockID();
1742 }
Mike Stump31feda52009-07-17 01:31:16 +00001743
Ted Kremenek71eca012007-08-29 23:20:49 +00001744 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001745 }
Mike Stump31feda52009-07-17 01:31:16 +00001746}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001747
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001748
1749/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001750void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001751
1752/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001753void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1754 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001755
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001756 // Print the entry block.
1757 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001758
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001759 // Iterate through the CFGBlocks and print them one by one.
1760 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1761 // Skip the entry block, because we already printed it.
1762 if (&(*I) == &getEntry() || &(*I) == &getExit())
1763 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001764
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001765 print_block(OS, this, *I, &Helper, true);
1766 }
Mike Stump31feda52009-07-17 01:31:16 +00001767
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001768 // Print the exit block.
1769 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001770 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001771}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001772
1773/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001774void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1775 print(llvm::errs(), cfg, LO);
1776}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001777
1778/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1779/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001780void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1781 const LangOptions &LO) const {
1782 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001783 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00001784}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001785
Ted Kremenek15647632008-01-30 23:02:42 +00001786/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001787void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00001788 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001789 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00001790 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1791}
1792
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001793Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00001794
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001795 if (!Terminator)
1796 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001797
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001798 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001799
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001800 switch (Terminator->getStmtClass()) {
1801 default:
1802 break;
Mike Stump31feda52009-07-17 01:31:16 +00001803
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001804 case Stmt::ForStmtClass:
1805 E = cast<ForStmt>(Terminator)->getCond();
1806 break;
Mike Stump31feda52009-07-17 01:31:16 +00001807
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001808 case Stmt::WhileStmtClass:
1809 E = cast<WhileStmt>(Terminator)->getCond();
1810 break;
Mike Stump31feda52009-07-17 01:31:16 +00001811
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001812 case Stmt::DoStmtClass:
1813 E = cast<DoStmt>(Terminator)->getCond();
1814 break;
Mike Stump31feda52009-07-17 01:31:16 +00001815
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001816 case Stmt::IfStmtClass:
1817 E = cast<IfStmt>(Terminator)->getCond();
1818 break;
Mike Stump31feda52009-07-17 01:31:16 +00001819
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001820 case Stmt::ChooseExprClass:
1821 E = cast<ChooseExpr>(Terminator)->getCond();
1822 break;
Mike Stump31feda52009-07-17 01:31:16 +00001823
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001824 case Stmt::IndirectGotoStmtClass:
1825 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1826 break;
Mike Stump31feda52009-07-17 01:31:16 +00001827
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001828 case Stmt::SwitchStmtClass:
1829 E = cast<SwitchStmt>(Terminator)->getCond();
1830 break;
Mike Stump31feda52009-07-17 01:31:16 +00001831
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001832 case Stmt::ConditionalOperatorClass:
1833 E = cast<ConditionalOperator>(Terminator)->getCond();
1834 break;
Mike Stump31feda52009-07-17 01:31:16 +00001835
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001836 case Stmt::BinaryOperatorClass: // '&&' and '||'
1837 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001838 break;
Mike Stump31feda52009-07-17 01:31:16 +00001839
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001840 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00001841 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001842 }
Mike Stump31feda52009-07-17 01:31:16 +00001843
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001844 return E ? E->IgnoreParens() : NULL;
1845}
1846
Ted Kremenek92137a32008-05-16 16:06:00 +00001847bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00001848
Ted Kremenek92137a32008-05-16 16:06:00 +00001849 if (!Terminator)
1850 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001851
Ted Kremenek92137a32008-05-16 16:06:00 +00001852 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001853
Ted Kremenek92137a32008-05-16 16:06:00 +00001854 switch (Terminator->getStmtClass()) {
1855 default:
1856 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001857
1858 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00001859 case Stmt::WhileStmtClass:
1860 case Stmt::DoStmtClass:
1861 case Stmt::IfStmtClass:
1862 case Stmt::ChooseExprClass:
1863 case Stmt::ConditionalOperatorClass:
1864 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00001865 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00001866 }
Mike Stump31feda52009-07-17 01:31:16 +00001867
Ted Kremenek92137a32008-05-16 16:06:00 +00001868 return E ? E->IgnoreParens() : NULL;
1869}
1870
Ted Kremenek15647632008-01-30 23:02:42 +00001871
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001872//===----------------------------------------------------------------------===//
1873// CFG Graphviz Visualization
1874//===----------------------------------------------------------------------===//
1875
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001876
1877#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00001878static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001879#endif
1880
Chris Lattnerc61089a2009-06-30 01:26:17 +00001881void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001882#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00001883 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001884 GraphHelper = &H;
1885 llvm::ViewGraph(this,"CFG");
1886 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001887#endif
1888}
1889
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001890namespace llvm {
1891template<>
1892struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson4d9e93c2009-06-24 17:37:55 +00001893 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
1894 bool ShortNames) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001895
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00001896#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001897 std::string OutSStr;
1898 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001899 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001900 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001901
1902 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1903
1904 // Process string output to make it nicer...
1905 for (unsigned i = 0; i != OutStr.length(); ++i)
1906 if (OutStr[i] == '\n') { // Left justify
1907 OutStr[i] = '\\';
1908 OutStr.insert(OutStr.begin()+i+1, 'l');
1909 }
Mike Stump31feda52009-07-17 01:31:16 +00001910
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001911 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00001912#else
1913 return "";
1914#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001915 }
1916};
1917} // end namespace llvm