blob: f5b604554d895d7fb931744c25f7ca6c907cc518 [file] [log] [blame]
Ted Kremenek97f75312007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Kremenek97f75312007-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
15#include "clang/AST/CFG.h"
Ted Kremenek95e854d2007-08-21 22:06:14 +000016#include "clang/AST/StmtVisitor.h"
Ted Kremenek08176a52007-08-31 21:30:12 +000017#include "clang/AST/PrettyPrinter.h"
Ted Kremenekc5de2222007-08-21 23:26:17 +000018#include "llvm/ADT/DenseMap.h"
Ted Kremenek0edd3a92007-08-28 19:26:49 +000019#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenekb3bb91b2007-08-29 21:56:09 +000020#include "llvm/Support/GraphWriter.h"
Ted Kremenek56c939e2007-12-17 19:35:20 +000021#include "llvm/Support/Streams.h"
Ted Kremenek98cee3a2008-01-08 18:15:10 +000022#include "llvm/Support/Compiler.h"
Ted Kremenekd058a9c2008-04-28 18:00:46 +000023#include <llvm/Support/Allocator.h>
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000024#include <llvm/Support/Format.h>
Ted Kremenek97f75312007-08-21 21:42:03 +000025#include <iomanip>
26#include <algorithm>
Ted Kremenekb3bb91b2007-08-29 21:56:09 +000027#include <sstream>
Ted Kremenek5ee98a72008-01-11 00:40:29 +000028
Ted Kremenek97f75312007-08-21 21:42:03 +000029using namespace clang;
30
31namespace {
32
Ted Kremenekd6e50602007-08-23 21:26:19 +000033// SaveAndRestore - A utility class that uses RIIA to save and restore
34// the value of a variable.
35template<typename T>
Ted Kremenek98cee3a2008-01-08 18:15:10 +000036struct VISIBILITY_HIDDEN SaveAndRestore {
Ted Kremenekd6e50602007-08-23 21:26:19 +000037 SaveAndRestore(T& x) : X(x), old_value(x) {}
38 ~SaveAndRestore() { X = old_value; }
Ted Kremenek44db7872007-08-30 18:13:31 +000039 T get() { return old_value; }
40
Ted Kremenekd6e50602007-08-23 21:26:19 +000041 T& X;
42 T old_value;
43};
Ted Kremenek97f75312007-08-21 21:42:03 +000044
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000045static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek0865a992008-08-06 23:20:50 +000046 if (VarDecl* VD = dyn_cast<VarDecl>(D))
47 if (Expr* Ex = VD->getInit())
48 return Ex->getSourceRange().getEnd();
49
50 return D->getLocation();
51}
52
Ted Kremeneka3195a32008-08-04 22:51:42 +000053/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek97f75312007-08-21 21:42:03 +000054/// The builder is stateful: an instance of the builder should be used to only
55/// construct a single CFG.
56///
57/// Example usage:
58///
59/// CFGBuilder builder;
60/// CFG* cfg = builder.BuildAST(stmt1);
61///
Ted Kremenek95e854d2007-08-21 22:06:14 +000062/// CFG construction is done via a recursive walk of an AST.
63/// We actually parse the AST in reverse order so that the successor
64/// of a basic block is constructed prior to its predecessor. This
65/// allows us to nicely capture implicit fall-throughs without extra
66/// basic blocks.
67///
Ted Kremenek98cee3a2008-01-08 18:15:10 +000068class VISIBILITY_HIDDEN CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenek97f75312007-08-21 21:42:03 +000069 CFG* cfg;
70 CFGBlock* Block;
Ted Kremenek97f75312007-08-21 21:42:03 +000071 CFGBlock* Succ;
Ted Kremenekf511d672007-08-22 21:36:54 +000072 CFGBlock* ContinueTargetBlock;
Ted Kremenekf308d372007-08-22 21:51:58 +000073 CFGBlock* BreakTargetBlock;
Ted Kremeneke809ebf2007-08-23 18:43:24 +000074 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek97bc3422008-02-13 22:05:39 +000075 CFGBlock* DefaultCaseBlock;
Ted Kremenek97f75312007-08-21 21:42:03 +000076
Ted Kremenek0edd3a92007-08-28 19:26:49 +000077 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenekc5de2222007-08-21 23:26:17 +000078 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
79 LabelMapTy LabelMap;
80
Ted Kremenek0edd3a92007-08-28 19:26:49 +000081 // A list of blocks that end with a "goto" that must be backpatched to
82 // their resolved targets upon completion of CFG construction.
Ted Kremenekf5392b72007-08-22 15:40:58 +000083 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenekc5de2222007-08-21 23:26:17 +000084 BackpatchBlocksTy BackpatchBlocks;
85
Ted Kremenek0edd3a92007-08-28 19:26:49 +000086 // A list of labels whose address has been taken (for indirect gotos).
87 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
88 LabelSetTy AddressTakenLabels;
89
Ted Kremenek97f75312007-08-21 21:42:03 +000090public:
Ted Kremenek4db5b452007-08-23 16:51:22 +000091 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenekf308d372007-08-22 21:51:58 +000092 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek97bc3422008-02-13 22:05:39 +000093 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenek97f75312007-08-21 21:42:03 +000094 // Create an empty CFG.
95 cfg = new CFG();
96 }
97
98 ~CFGBuilder() { delete cfg; }
Ted Kremenek97f75312007-08-21 21:42:03 +000099
Ted Kremenek73543912007-08-23 21:42:29 +0000100 // buildCFG - Used by external clients to construct the CFG.
101 CFG* buildCFG(Stmt* Statement);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000102
Ted Kremenek73543912007-08-23 21:42:29 +0000103 // Visitors to walk an AST and construct the CFG. Called by
104 // buildCFG. Do not call directly!
Ted Kremenekd8313202007-08-22 18:22:34 +0000105
Ted Kremenek73543912007-08-23 21:42:29 +0000106 CFGBlock* VisitBreakStmt(BreakStmt* B);
Ted Kremenek79f0a632008-04-16 21:10:48 +0000107 CFGBlock* VisitCaseStmt(CaseStmt* Terminator);
Ted Kremenek05335162008-11-11 17:10:00 +0000108 CFGBlock* VisitCompoundStmt(CompoundStmt* C);
109 CFGBlock* VisitContinueStmt(ContinueStmt* C);
Ted Kremenekc07a8af2008-02-13 21:46:34 +0000110 CFGBlock* VisitDefaultStmt(DefaultStmt* D);
Ted Kremenek05335162008-11-11 17:10:00 +0000111 CFGBlock* VisitDoStmt(DoStmt* D);
112 CFGBlock* VisitForStmt(ForStmt* F);
113 CFGBlock* VisitGotoStmt(GotoStmt* G);
114 CFGBlock* VisitIfStmt(IfStmt* I);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000115 CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I);
Ted Kremenek05335162008-11-11 17:10:00 +0000116 CFGBlock* VisitLabelStmt(LabelStmt* L);
117 CFGBlock* VisitNullStmt(NullStmt* Statement);
118 CFGBlock* VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
119 CFGBlock* VisitReturnStmt(ReturnStmt* R);
120 CFGBlock* VisitStmt(Stmt* Statement);
121 CFGBlock* VisitSwitchStmt(SwitchStmt* Terminator);
122 CFGBlock* VisitWhileStmt(WhileStmt* W);
Ted Kremenek97f75312007-08-21 21:42:03 +0000123
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000124 // FIXME: Add support for ObjC-specific control-flow structures.
125
Ted Kremenekd058a9c2008-04-28 18:00:46 +0000126 // NYS == Not Yet Supported
127 CFGBlock* NYS() {
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000128 badCFG = true;
129 return Block;
130 }
131
Ted Kremenek72037962009-03-30 22:29:21 +0000132 CFGBlock* VisitObjCAtTryStmt(ObjCAtTryStmt* S);
133 CFGBlock* VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
134 // FIXME: For now we pretend that @catch and the code it contains
135 // does not exit.
136 return Block;
137 }
138
Ted Kremenekc74ac3e2008-12-09 20:20:09 +0000139 // FIXME: This is not completely supported. We basically @throw like
140 // a 'return'.
141 CFGBlock* VisitObjCAtThrowStmt(ObjCAtThrowStmt* S);
Ted Kremenekd058a9c2008-04-28 18:00:46 +0000142
Ted Kremenek9b8c9522009-05-02 01:49:13 +0000143 CFGBlock* VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S);
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000144
Ted Kremenekd68a8d32008-09-26 18:17:07 +0000145 // Blocks.
146 CFGBlock* VisitBlockExpr(BlockExpr* E) { return NYS(); }
147 CFGBlock* VisitBlockDeclRefExpr(BlockDeclRefExpr* E) { return NYS(); }
148
Ted Kremenek73543912007-08-23 21:42:29 +0000149private:
150 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenek79f0a632008-04-16 21:10:48 +0000151 CFGBlock* addStmt(Stmt* Terminator);
152 CFGBlock* WalkAST(Stmt* Terminator, bool AlwaysAddStmt);
153 CFGBlock* WalkAST_VisitChildren(Stmt* Terminator);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000154 CFGBlock* WalkAST_VisitDeclSubExpr(Decl* D);
Ted Kremenek79f0a632008-04-16 21:10:48 +0000155 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* Terminator);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000156 bool FinishBlock(CFGBlock* B);
Ted Kremenekd8313202007-08-22 18:22:34 +0000157
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000158 bool badCFG;
Ted Kremenek97f75312007-08-21 21:42:03 +0000159};
Ted Kremenek09535672008-09-26 22:58:57 +0000160
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000161// FIXME: Add support for dependent-sized array types in C++?
162// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek09535672008-09-26 22:58:57 +0000163static VariableArrayType* FindVA(Type* t) {
164 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
165 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
166 if (vat->getSizeExpr())
167 return vat;
168
169 t = vt->getElementType().getTypePtr();
170 }
171
172 return 0;
173}
Ted Kremenek73543912007-08-23 21:42:29 +0000174
175/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
176/// represent an arbitrary statement. Examples include a single expression
177/// or a function body (compound statement). The ownership of the returned
178/// CFG is transferred to the caller. If CFG construction fails, this method
179/// returns NULL.
180CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000181 assert (cfg);
Ted Kremenek73543912007-08-23 21:42:29 +0000182 if (!Statement) return NULL;
183
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000184 badCFG = false;
185
Ted Kremenek73543912007-08-23 21:42:29 +0000186 // Create an empty block that will serve as the exit block for the CFG.
187 // Since this is the first block added to the CFG, it will be implicitly
188 // registered as the exit block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000189 Succ = createBlock();
190 assert (Succ == &cfg->getExit());
191 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenek73543912007-08-23 21:42:29 +0000192
193 // Visit the statements and create the CFG.
Ted Kremenekfa38c7a2008-02-27 17:33:02 +0000194 CFGBlock* B = Visit(Statement);
195 if (!B) B = Succ;
196
197 if (B) {
Ted Kremenek73543912007-08-23 21:42:29 +0000198 // Finalize the last constructed block. This usually involves
199 // reversing the order of the statements in the block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000200 if (Block) FinishBlock(B);
Ted Kremenek73543912007-08-23 21:42:29 +0000201
202 // Backpatch the gotos whose label -> block mappings we didn't know
203 // when we encountered them.
204 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
205 E = BackpatchBlocks.end(); I != E; ++I ) {
206
207 CFGBlock* B = *I;
208 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
209 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
210
211 // If there is no target for the goto, then we are looking at an
212 // incomplete AST. Handle this by not registering a successor.
213 if (LI == LabelMap.end()) continue;
214
215 B->addSuccessor(LI->second);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000216 }
Ted Kremenek73543912007-08-23 21:42:29 +0000217
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000218 // Add successors to the Indirect Goto Dispatch block (if we have one).
219 if (CFGBlock* B = cfg->getIndirectGotoBlock())
220 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
221 E = AddressTakenLabels.end(); I != E; ++I ) {
222
223 // Lookup the target block.
224 LabelMapTy::iterator LI = LabelMap.find(*I);
225
226 // If there is no target block that contains label, then we are looking
227 // at an incomplete AST. Handle this by not registering a successor.
228 if (LI == LabelMap.end()) continue;
229
230 B->addSuccessor(LI->second);
231 }
Ted Kremenek680fcb82007-09-26 21:23:31 +0000232
Ted Kremenek844cb4d2007-09-17 16:18:02 +0000233 Succ = B;
Ted Kremenek680fcb82007-09-26 21:23:31 +0000234 }
235
236 // Create an empty entry block that has no predecessors.
237 cfg->setEntry(createBlock());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000238
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000239 if (badCFG) {
240 delete cfg;
241 cfg = NULL;
242 return NULL;
243 }
244
Ted Kremenek680fcb82007-09-26 21:23:31 +0000245 // NULL out cfg so that repeated calls to the builder will fail and that
246 // the ownership of the constructed CFG is passed to the caller.
247 CFG* t = cfg;
248 cfg = NULL;
249 return t;
Ted Kremenek73543912007-08-23 21:42:29 +0000250}
251
252/// createBlock - Used to lazily create blocks that are connected
253/// to the current (global) succcessor.
254CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek14594572007-09-05 20:02:05 +0000255 CFGBlock* B = cfg->createBlock();
Ted Kremenek73543912007-08-23 21:42:29 +0000256 if (add_successor && Succ) B->addSuccessor(Succ);
257 return B;
258}
259
260/// FinishBlock - When the last statement has been added to the block,
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000261/// we must reverse the statements because they have been inserted
262/// in reverse order.
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000263bool CFGBuilder::FinishBlock(CFGBlock* B) {
264 if (badCFG)
265 return false;
266
Ted Kremenek73543912007-08-23 21:42:29 +0000267 assert (B);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000268 B->reverseStmts();
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000269 return true;
Ted Kremenek73543912007-08-23 21:42:29 +0000270}
271
Ted Kremenek65cfa562007-08-27 21:27:44 +0000272/// addStmt - Used to add statements/expressions to the current CFGBlock
273/// "Block". This method calls WalkAST on the passed statement to see if it
274/// contains any short-circuit expressions. If so, it recursively creates
275/// the necessary blocks for such expressions. It returns the "topmost" block
276/// of the created blocks, or the original value of "Block" when this method
277/// was called if no additional blocks are created.
Ted Kremenek79f0a632008-04-16 21:10:48 +0000278CFGBlock* CFGBuilder::addStmt(Stmt* Terminator) {
Ted Kremenek390b9762007-08-30 18:39:40 +0000279 if (!Block) Block = createBlock();
Ted Kremenek79f0a632008-04-16 21:10:48 +0000280 return WalkAST(Terminator,true);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000281}
282
283/// WalkAST - Used by addStmt to walk the subtree of a statement and
Ted Kremeneke822b622007-08-28 18:14:37 +0000284/// add extra blocks for ternary operators, &&, and ||. We also
285/// process "," and DeclStmts (which may contain nested control-flow).
Ted Kremenek79f0a632008-04-16 21:10:48 +0000286CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) {
287 switch (Terminator->getStmtClass()) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000288 case Stmt::ConditionalOperatorClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000289 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000290
291 // Create the confluence block that will "merge" the results
292 // of the ternary expression.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000293 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
294 ConfluenceBlock->appendStmt(C);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000295 if (!FinishBlock(ConfluenceBlock))
296 return 0;
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000297
298 // Create a block for the LHS expression if there is an LHS expression.
299 // A GCC extension allows LHS to be NULL, causing the condition to
300 // be the value that is returned instead.
301 // e.g: x ?: y is shorthand for: x ? x : y;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000302 Succ = ConfluenceBlock;
303 Block = NULL;
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000304 CFGBlock* LHSBlock = NULL;
305 if (C->getLHS()) {
306 LHSBlock = Visit(C->getLHS());
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000307 if (!FinishBlock(LHSBlock))
308 return 0;
309 Block = NULL;
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000310 }
Ted Kremenek65cfa562007-08-27 21:27:44 +0000311
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000312 // Create the block for the RHS expression.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000313 Succ = ConfluenceBlock;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000314 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000315 if (!FinishBlock(RHSBlock))
316 return 0;
317
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000318 // Create the block that will contain the condition.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000319 Block = createBlock(false);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000320
321 if (LHSBlock)
322 Block->addSuccessor(LHSBlock);
323 else {
324 // If we have no LHS expression, add the ConfluenceBlock as a direct
325 // successor for the block containing the condition. Moreover,
326 // we need to reverse the order of the predecessors in the
327 // ConfluenceBlock because the RHSBlock will have been added to
328 // the succcessors already, and we want the first predecessor to the
329 // the block containing the expression for the case when the ternary
330 // expression evaluates to true.
331 Block->addSuccessor(ConfluenceBlock);
332 assert (ConfluenceBlock->pred_size() == 2);
333 std::reverse(ConfluenceBlock->pred_begin(),
334 ConfluenceBlock->pred_end());
335 }
336
Ted Kremenek65cfa562007-08-27 21:27:44 +0000337 Block->addSuccessor(RHSBlock);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000338
Ted Kremenek65cfa562007-08-27 21:27:44 +0000339 Block->setTerminator(C);
340 return addStmt(C->getCond());
341 }
Ted Kremenek7f788422007-08-31 17:03:41 +0000342
343 case Stmt::ChooseExprClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000344 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Ted Kremenek7f788422007-08-31 17:03:41 +0000345
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000346 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek7f788422007-08-31 17:03:41 +0000347 ConfluenceBlock->appendStmt(C);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000348 if (!FinishBlock(ConfluenceBlock))
349 return 0;
Ted Kremenek7f788422007-08-31 17:03:41 +0000350
351 Succ = ConfluenceBlock;
352 Block = NULL;
353 CFGBlock* LHSBlock = Visit(C->getLHS());
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000354 if (!FinishBlock(LHSBlock))
355 return 0;
Ted Kremenekd11620d2007-09-11 21:29:43 +0000356
Ted Kremenek7f788422007-08-31 17:03:41 +0000357 Succ = ConfluenceBlock;
358 Block = NULL;
359 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000360 if (!FinishBlock(RHSBlock))
361 return 0;
Ted Kremenek7f788422007-08-31 17:03:41 +0000362
363 Block = createBlock(false);
364 Block->addSuccessor(LHSBlock);
365 Block->addSuccessor(RHSBlock);
366 Block->setTerminator(C);
367 return addStmt(C->getCond());
368 }
Ted Kremenek666a6af2007-08-28 16:18:58 +0000369
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000370 case Stmt::DeclStmtClass: {
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000371 DeclStmt *DS = cast<DeclStmt>(Terminator);
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000372 if (DS->isSingleDecl()) {
Ted Kremenek0865a992008-08-06 23:20:50 +0000373 Block->appendStmt(Terminator);
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000374 return WalkAST_VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenek0865a992008-08-06 23:20:50 +0000375 }
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000376
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000377 CFGBlock* B = 0;
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000378
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000379 // FIXME: Add a reverse iterator for DeclStmt to avoid this
380 // extra copy.
Chris Lattnerffae0dd2009-03-28 06:53:40 +0000381 typedef llvm::SmallVector<Decl*,10> BufTy;
382 BufTy Buf(DS->decl_begin(), DS->decl_end());
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000383
384 for (BufTy::reverse_iterator I=Buf.rbegin(), E=Buf.rend(); I!=E; ++I) {
385 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
386 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
387 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000388
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000389 // Allocate the DeclStmt using the BumpPtrAllocator. It will
390 // get automatically freed with the CFG.
391 DeclGroupRef DG(*I);
392 Decl* D = *I;
393 void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
394
Chris Lattnerffae0dd2009-03-28 06:53:40 +0000395 DeclStmt* DS = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000396
397 // Append the fake DeclStmt to block.
398 Block->appendStmt(DS);
399 B = WalkAST_VisitDeclSubExpr(D);
Ted Kremenek0865a992008-08-06 23:20:50 +0000400 }
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000401 return B;
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000402 }
Ted Kremenek0865a992008-08-06 23:20:50 +0000403
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000404 case Stmt::AddrLabelExprClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000405 AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000406 AddressTakenLabels.insert(A->getLabel());
407
Ted Kremenek79f0a632008-04-16 21:10:48 +0000408 if (AlwaysAddStmt) Block->appendStmt(Terminator);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000409 return Block;
410 }
Ted Kremenekd11620d2007-09-11 21:29:43 +0000411
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000412 case Stmt::StmtExprClass:
Ted Kremenek79f0a632008-04-16 21:10:48 +0000413 return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
Ted Kremeneke822b622007-08-28 18:14:37 +0000414
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000415 case Stmt::SizeOfAlignOfExprClass: {
416 SizeOfAlignOfExpr* E = cast<SizeOfAlignOfExpr>(Terminator);
Ted Kremenek09535672008-09-26 22:58:57 +0000417
418 // VLA types have expressions that must be evaluated.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000419 if (E->isArgumentType()) {
420 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
421 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
422 addStmt(VA->getSizeExpr());
423 }
424 // Expressions in sizeof/alignof are not evaluated and thus have no
425 // control flow.
426 else
427 Block->appendStmt(Terminator);
Ted Kremenek09535672008-09-26 22:58:57 +0000428
429 return Block;
430 }
431
Ted Kremenekcfaae762007-08-27 21:54:41 +0000432 case Stmt::BinaryOperatorClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000433 BinaryOperator* B = cast<BinaryOperator>(Terminator);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000434
435 if (B->isLogicalOp()) { // && or ||
436 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
437 ConfluenceBlock->appendStmt(B);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000438 if (!FinishBlock(ConfluenceBlock))
439 return 0;
Ted Kremenekcfaae762007-08-27 21:54:41 +0000440
441 // create the block evaluating the LHS
442 CFGBlock* LHSBlock = createBlock(false);
Ted Kremenekb2348522007-12-21 19:49:00 +0000443 LHSBlock->setTerminator(B);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000444
445 // create the block evaluating the RHS
446 Succ = ConfluenceBlock;
447 Block = NULL;
448 CFGBlock* RHSBlock = Visit(B->getRHS());
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000449 if (!FinishBlock(RHSBlock))
450 return 0;
Ted Kremenekb2348522007-12-21 19:49:00 +0000451
452 // Now link the LHSBlock with RHSBlock.
453 if (B->getOpcode() == BinaryOperator::LOr) {
454 LHSBlock->addSuccessor(ConfluenceBlock);
455 LHSBlock->addSuccessor(RHSBlock);
456 }
457 else {
458 assert (B->getOpcode() == BinaryOperator::LAnd);
459 LHSBlock->addSuccessor(RHSBlock);
460 LHSBlock->addSuccessor(ConfluenceBlock);
461 }
Ted Kremenekcfaae762007-08-27 21:54:41 +0000462
463 // Generate the blocks for evaluating the LHS.
464 Block = LHSBlock;
465 return addStmt(B->getLHS());
Ted Kremeneke822b622007-08-28 18:14:37 +0000466 }
467 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
468 Block->appendStmt(B);
469 addStmt(B->getRHS());
470 return addStmt(B->getLHS());
Ted Kremenek3a819822007-10-01 19:33:33 +0000471 }
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000472
473 break;
Ted Kremenekcfaae762007-08-27 21:54:41 +0000474 }
Ted Kremenekd68a8d32008-09-26 18:17:07 +0000475
476 // Blocks: No support for blocks ... yet
477 case Stmt::BlockExprClass:
478 case Stmt::BlockDeclRefExprClass:
479 return NYS();
Ted Kremeneka9ba5cc2008-02-26 02:37:08 +0000480
481 case Stmt::ParenExprClass:
Ted Kremenek79f0a632008-04-16 21:10:48 +0000482 return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000483
Ted Kremenek65cfa562007-08-27 21:27:44 +0000484 default:
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000485 break;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000486 };
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000487
Ted Kremenek79f0a632008-04-16 21:10:48 +0000488 if (AlwaysAddStmt) Block->appendStmt(Terminator);
489 return WalkAST_VisitChildren(Terminator);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000490}
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000491
Ted Kremenek0865a992008-08-06 23:20:50 +0000492/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions
493/// for initializers in Decls.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000494CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(Decl* D) {
Ted Kremenek0865a992008-08-06 23:20:50 +0000495 VarDecl* VD = dyn_cast<VarDecl>(D);
496
497 if (!VD)
Ted Kremenekf4e35622007-11-18 20:06:01 +0000498 return Block;
499
Ted Kremenek0865a992008-08-06 23:20:50 +0000500 Expr* Init = VD->getInit();
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000501
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000502 if (Init) {
Mike Stumpd8472f22009-02-26 08:00:25 +0000503 // Optimization: Don't create separate block-level statements for literals.
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000504 switch (Init->getStmtClass()) {
505 case Stmt::IntegerLiteralClass:
506 case Stmt::CharacterLiteralClass:
507 case Stmt::StringLiteralClass:
508 break;
509 default:
510 Block = addStmt(Init);
511 }
Ted Kremenek4ad64e82008-02-29 22:32:24 +0000512 }
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000513
514 // If the type of VD is a VLA, then we must process its size expressions.
515 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
516 VA = FindVA(VA->getElementType().getTypePtr()))
517 Block = addStmt(VA->getSizeExpr());
Ted Kremenek4ad64e82008-02-29 22:32:24 +0000518
Ted Kremeneke822b622007-08-28 18:14:37 +0000519 return Block;
520}
521
Ted Kremenek65cfa562007-08-27 21:27:44 +0000522/// WalkAST_VisitChildren - Utility method to call WalkAST on the
523/// children of a Stmt.
Ted Kremenek79f0a632008-04-16 21:10:48 +0000524CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000525 CFGBlock* B = Block;
Mike Stumpd8472f22009-02-26 08:00:25 +0000526 for (Stmt::child_iterator I = Terminator->child_begin(),
527 E = Terminator->child_end();
Ted Kremenek65cfa562007-08-27 21:27:44 +0000528 I != E; ++I)
Ted Kremenek680fcb82007-09-26 21:23:31 +0000529 if (*I) B = WalkAST(*I);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000530
531 return B;
532}
533
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000534/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
535/// expressions (a GCC extension).
Ted Kremenek79f0a632008-04-16 21:10:48 +0000536CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
537 Block->appendStmt(Terminator);
538 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000539}
540
Ted Kremenek73543912007-08-23 21:42:29 +0000541/// VisitStmt - Handle statements with no branching control flow.
542CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
543 // We cannot assume that we are in the middle of a basic block, since
544 // the CFG might only be constructed for this single statement. If
545 // we have no current basic block, just create one lazily.
546 if (!Block) Block = createBlock();
547
548 // Simply add the statement to the current block. We actually
549 // insert statements in reverse order; this order is reversed later
550 // when processing the containing element in the AST.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000551 addStmt(Statement);
552
Ted Kremenek73543912007-08-23 21:42:29 +0000553 return Block;
554}
555
556CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
557 return Block;
558}
559
560CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000561
562 CFGBlock* LastBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000563
Ted Kremenekfeb0e992008-02-26 00:22:58 +0000564 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
565 I != E; ++I ) {
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000566 LastBlock = Visit(*I);
Ted Kremenekfeb0e992008-02-26 00:22:58 +0000567 }
568
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000569 return LastBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000570}
571
572CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
573 // We may see an if statement in the middle of a basic block, or
574 // it may be the first statement we are processing. In either case,
575 // we create a new basic block. First, we create the blocks for
576 // the then...else statements, and then we create the block containing
577 // the if statement. If we were in the middle of a block, we
578 // stop processing that block and reverse its statements. That block
579 // is then the implicit successor for the "then" and "else" clauses.
580
581 // The block we were proccessing is now finished. Make it the
582 // successor block.
583 if (Block) {
584 Succ = Block;
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000585 if (!FinishBlock(Block))
586 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +0000587 }
588
589 // Process the false branch. NULL out Block so that the recursive
590 // call to Visit will create a new basic block.
591 // Null out Block so that all successor
592 CFGBlock* ElseBlock = Succ;
593
594 if (Stmt* Else = I->getElse()) {
595 SaveAndRestore<CFGBlock*> sv(Succ);
596
597 // NULL out Block so that the recursive call to Visit will
598 // create a new basic block.
599 Block = NULL;
Ted Kremenek44db7872007-08-30 18:13:31 +0000600 ElseBlock = Visit(Else);
601
602 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
603 ElseBlock = sv.get();
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000604 else if (Block) {
605 if (!FinishBlock(ElseBlock))
606 return 0;
607 }
Ted Kremenek73543912007-08-23 21:42:29 +0000608 }
609
610 // Process the true branch. NULL out Block so that the recursive
611 // call to Visit will create a new basic block.
612 // Null out Block so that all successor
613 CFGBlock* ThenBlock;
614 {
615 Stmt* Then = I->getThen();
616 assert (Then);
617 SaveAndRestore<CFGBlock*> sv(Succ);
618 Block = NULL;
Ted Kremenek44db7872007-08-30 18:13:31 +0000619 ThenBlock = Visit(Then);
620
Ted Kremeneke5a3c022009-04-01 03:52:47 +0000621 if (!ThenBlock) {
622 // We can reach here if the "then" body has all NullStmts.
623 // Create an empty block so we can distinguish between true and false
624 // branches in path-sensitive analyses.
625 ThenBlock = createBlock(false);
626 ThenBlock->addSuccessor(sv.get());
627 }
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000628 else if (Block) {
629 if (!FinishBlock(ThenBlock))
630 return 0;
631 }
Ted Kremenek73543912007-08-23 21:42:29 +0000632 }
633
634 // Now create a new block containing the if statement.
635 Block = createBlock(false);
Ted Kremenek73543912007-08-23 21:42:29 +0000636
637 // Set the terminator of the new block to the If statement.
638 Block->setTerminator(I);
639
640 // Now add the successors.
641 Block->addSuccessor(ThenBlock);
642 Block->addSuccessor(ElseBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000643
644 // Add the condition as the last statement in the new block. This
645 // may create new blocks as the condition may contain control-flow. Any
646 // newly created blocks will be pointed to be "Block".
Ted Kremenek1eaa6712008-01-30 23:02:42 +0000647 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenek73543912007-08-23 21:42:29 +0000648}
Ted Kremenekd11620d2007-09-11 21:29:43 +0000649
Ted Kremenek73543912007-08-23 21:42:29 +0000650
651CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
652 // If we were in the middle of a block we stop processing that block
653 // and reverse its statements.
654 //
655 // NOTE: If a "return" appears in the middle of a block, this means
656 // that the code afterwards is DEAD (unreachable). We still
657 // keep a basic block for that code; a simple "mark-and-sweep"
658 // from the entry block will be able to report such dead
659 // blocks.
660 if (Block) FinishBlock(Block);
661
662 // Create the new block.
663 Block = createBlock(false);
664
665 // The Exit block is the only successor.
666 Block->addSuccessor(&cfg->getExit());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000667
668 // Add the return statement to the block. This may create new blocks
669 // if R contains control-flow (short-circuit operations).
670 return addStmt(R);
Ted Kremenek73543912007-08-23 21:42:29 +0000671}
672
673CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
674 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek82e8a192008-03-15 07:45:02 +0000675 Visit(L->getSubStmt());
676 CFGBlock* LabelBlock = Block;
Ted Kremenek9b0d1b62007-08-30 18:20:57 +0000677
678 if (!LabelBlock) // This can happen when the body is empty, i.e.
679 LabelBlock=createBlock(); // scopes that only contains NullStmts.
680
Ted Kremenek73543912007-08-23 21:42:29 +0000681 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
682 LabelMap[ L ] = LabelBlock;
683
684 // Labels partition blocks, so this is the end of the basic block
Ted Kremenekec055e12007-08-29 23:20:49 +0000685 // we were processing (L is the block's label). Because this is
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000686 // label (and we have already processed the substatement) there is no
687 // extra control-flow to worry about.
Ted Kremenekec055e12007-08-29 23:20:49 +0000688 LabelBlock->setLabel(L);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000689 if (!FinishBlock(LabelBlock))
690 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +0000691
692 // We set Block to NULL to allow lazy creation of a new block
693 // (if necessary);
694 Block = NULL;
695
696 // This block is now the implicit successor of other blocks.
697 Succ = LabelBlock;
698
699 return LabelBlock;
700}
701
702CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
703 // Goto is a control-flow statement. Thus we stop processing the
704 // current block and create a new one.
705 if (Block) FinishBlock(Block);
706 Block = createBlock(false);
707 Block->setTerminator(G);
708
709 // If we already know the mapping to the label block add the
710 // successor now.
711 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
712
713 if (I == LabelMap.end())
714 // We will need to backpatch this block later.
715 BackpatchBlocks.push_back(Block);
716 else
717 Block->addSuccessor(I->second);
718
719 return Block;
720}
721
722CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
723 // "for" is a control-flow statement. Thus we stop processing the
724 // current block.
725
726 CFGBlock* LoopSuccessor = NULL;
727
728 if (Block) {
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000729 if (!FinishBlock(Block))
730 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +0000731 LoopSuccessor = Block;
732 }
733 else LoopSuccessor = Succ;
734
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000735 // Because of short-circuit evaluation, the condition of the loop
736 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
737 // blocks that evaluate the condition.
738 CFGBlock* ExitConditionBlock = createBlock(false);
739 CFGBlock* EntryConditionBlock = ExitConditionBlock;
740
741 // Set the terminator for the "exit" condition block.
742 ExitConditionBlock->setTerminator(F);
743
744 // Now add the actual condition to the condition block. Because the
745 // condition itself may contain control-flow, new blocks may be created.
746 if (Stmt* C = F->getCond()) {
747 Block = ExitConditionBlock;
748 EntryConditionBlock = addStmt(C);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000749 if (Block) {
750 if (!FinishBlock(EntryConditionBlock))
751 return 0;
752 }
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000753 }
Ted Kremenek73543912007-08-23 21:42:29 +0000754
755 // The condition block is the implicit successor for the loop body as
756 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000757 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000758
759 // Now create the loop body.
760 {
761 assert (F->getBody());
762
763 // Save the current values for Block, Succ, and continue and break targets
764 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
765 save_continue(ContinueTargetBlock),
766 save_break(BreakTargetBlock);
Ted Kremenek77f93372008-09-04 21:48:47 +0000767
Ted Kremenek390b9762007-08-30 18:39:40 +0000768 // Create a new block to contain the (bottom) of the loop body.
769 Block = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000770
Ted Kremenek77f93372008-09-04 21:48:47 +0000771 if (Stmt* I = F->getInc()) {
772 // Generate increment code in its own basic block. This is the target
773 // of continue statements.
Ted Kremenekd19e99e2008-11-24 20:50:24 +0000774 Succ = Visit(I);
Ted Kremenek77f93372008-09-04 21:48:47 +0000775 }
776 else {
Ted Kremenekca7cdcf2009-04-28 00:51:56 +0000777 // No increment code. Create a special, empty, block that is used as
778 // the target block for "looping back" to the start of the loop.
779 assert(Succ == EntryConditionBlock);
780 Succ = createBlock();
Ted Kremenek77f93372008-09-04 21:48:47 +0000781 }
782
Ted Kremenekca7cdcf2009-04-28 00:51:56 +0000783 // Finish up the increment (or empty) block if it hasn't been already.
784 if (Block) {
785 assert(Block == Succ);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000786 if (!FinishBlock(Block))
787 return 0;
Ted Kremenekca7cdcf2009-04-28 00:51:56 +0000788 Block = 0;
789 }
790
791 ContinueTargetBlock = Succ;
792
793 // The starting block for the loop increment is the block that should
794 // represent the 'loop target' for looping back to the start of the loop.
795 ContinueTargetBlock->setLoopTarget(F);
796
Ted Kremenek77f93372008-09-04 21:48:47 +0000797 // All breaks should go to the code following the loop.
798 BreakTargetBlock = LoopSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +0000799
800 // Now populate the body block, and in the process create new blocks
801 // as we walk the body of the loop.
802 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenek390b9762007-08-30 18:39:40 +0000803
804 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +0000805 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000806 else if (Block) {
807 if (!FinishBlock(BodyBlock))
808 return 0;
809 }
Ted Kremenek73543912007-08-23 21:42:29 +0000810
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000811 // This new body block is a successor to our "exit" condition block.
812 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000813 }
814
815 // Link up the condition block with the code that follows the loop.
816 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000817 ExitConditionBlock->addSuccessor(LoopSuccessor);
818
Ted Kremenek73543912007-08-23 21:42:29 +0000819 // If the loop contains initialization, create a new block for those
820 // statements. This block can also contain statements that precede
821 // the loop.
822 if (Stmt* I = F->getInit()) {
823 Block = createBlock();
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000824 return addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000825 }
826 else {
827 // There is no loop initialization. We are thus basically a while
828 // loop. NULL out Block to force lazy block construction.
829 Block = NULL;
Ted Kremenek9ff572c2008-02-27 07:20:00 +0000830 Succ = EntryConditionBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000831 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000832 }
833}
834
Ted Kremenek05335162008-11-11 17:10:00 +0000835CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
836 // Objective-C fast enumeration 'for' statements:
837 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
838 //
839 // for ( Type newVariable in collection_expression ) { statements }
840 //
841 // becomes:
842 //
843 // prologue:
844 // 1. collection_expression
845 // T. jump to loop_entry
846 // loop_entry:
Ted Kremenek65514842008-11-14 01:57:41 +0000847 // 1. side-effects of element expression
Ted Kremenek05335162008-11-11 17:10:00 +0000848 // 1. ObjCForCollectionStmt [performs binding to newVariable]
849 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
850 // TB:
851 // statements
852 // T. jump to loop_entry
853 // FB:
854 // what comes after
855 //
856 // and
857 //
858 // Type existingItem;
859 // for ( existingItem in expression ) { statements }
860 //
861 // becomes:
862 //
863 // the same with newVariable replaced with existingItem; the binding
864 // works the same except that for one ObjCForCollectionStmt::getElement()
865 // returns a DeclStmt and the other returns a DeclRefExpr.
866 //
867
868 CFGBlock* LoopSuccessor = 0;
869
870 if (Block) {
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000871 if (!FinishBlock(Block))
872 return 0;
Ted Kremenek05335162008-11-11 17:10:00 +0000873 LoopSuccessor = Block;
874 Block = 0;
875 }
876 else LoopSuccessor = Succ;
877
Ted Kremenek65514842008-11-14 01:57:41 +0000878 // Build the condition blocks.
879 CFGBlock* ExitConditionBlock = createBlock(false);
880 CFGBlock* EntryConditionBlock = ExitConditionBlock;
881
882 // Set the terminator for the "exit" condition block.
883 ExitConditionBlock->setTerminator(S);
884
885 // The last statement in the block should be the ObjCForCollectionStmt,
886 // which performs the actual binding to 'element' and determines if there
887 // are any more items in the collection.
888 ExitConditionBlock->appendStmt(S);
889 Block = ExitConditionBlock;
890
891 // Walk the 'element' expression to see if there are any side-effects. We
892 // generate new blocks as necesary. We DON'T add the statement by default
893 // to the CFG unless it contains control-flow.
894 EntryConditionBlock = WalkAST(S->getElement(), false);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000895 if (Block) {
896 if (!FinishBlock(EntryConditionBlock))
897 return 0;
898 Block = 0;
899 }
Ted Kremenek65514842008-11-14 01:57:41 +0000900
901 // The condition block is the implicit successor for the loop body as
902 // well as any code above the loop.
903 Succ = EntryConditionBlock;
Ted Kremenek05335162008-11-11 17:10:00 +0000904
905 // Now create the true branch.
Ted Kremenek65514842008-11-14 01:57:41 +0000906 {
907 // Save the current values for Succ, continue and break targets.
908 SaveAndRestore<CFGBlock*> save_Succ(Succ),
909 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
910
911 BreakTargetBlock = LoopSuccessor;
912 ContinueTargetBlock = EntryConditionBlock;
913
914 CFGBlock* BodyBlock = Visit(S->getBody());
915
916 if (!BodyBlock)
917 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000918 else if (Block) {
919 if (!FinishBlock(BodyBlock))
920 return 0;
921 }
Ted Kremenek65514842008-11-14 01:57:41 +0000922
923 // This new body block is a successor to our "exit" condition block.
924 ExitConditionBlock->addSuccessor(BodyBlock);
925 }
Ted Kremenekf5383072008-11-13 06:36:45 +0000926
Ted Kremenek65514842008-11-14 01:57:41 +0000927 // Link up the condition block with the code that follows the loop.
928 // (the false branch).
929 ExitConditionBlock->addSuccessor(LoopSuccessor);
930
Ted Kremenek05335162008-11-11 17:10:00 +0000931 // Now create a prologue block to contain the collection expression.
Ted Kremenek65514842008-11-14 01:57:41 +0000932 Block = createBlock();
Ted Kremenek05335162008-11-11 17:10:00 +0000933 return addStmt(S->getCollection());
934}
Ted Kremenek72037962009-03-30 22:29:21 +0000935
Ted Kremenek9b8c9522009-05-02 01:49:13 +0000936CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
937 // FIXME: Add locking 'primitives' to CFG for @synchronized.
938
939 // Inline the body.
940 Visit(S->getSynchBody());
941
942 // Inline the sync expression.
943 return Visit(S->getSynchExpr());
944}
945
Ted Kremenek72037962009-03-30 22:29:21 +0000946CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek417d4692009-04-07 04:26:02 +0000947 return NYS();
Ted Kremenek72037962009-03-30 22:29:21 +0000948}
Ted Kremenek05335162008-11-11 17:10:00 +0000949
Ted Kremenek73543912007-08-23 21:42:29 +0000950CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
951 // "while" is a control-flow statement. Thus we stop processing the
952 // current block.
953
954 CFGBlock* LoopSuccessor = NULL;
955
956 if (Block) {
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000957 if (!FinishBlock(Block))
958 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +0000959 LoopSuccessor = Block;
960 }
961 else LoopSuccessor = Succ;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000962
963 // Because of short-circuit evaluation, the condition of the loop
964 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
965 // blocks that evaluate the condition.
966 CFGBlock* ExitConditionBlock = createBlock(false);
967 CFGBlock* EntryConditionBlock = ExitConditionBlock;
968
969 // Set the terminator for the "exit" condition block.
970 ExitConditionBlock->setTerminator(W);
971
972 // Now add the actual condition to the condition block. Because the
973 // condition itself may contain control-flow, new blocks may be created.
974 // Thus we update "Succ" after adding the condition.
975 if (Stmt* C = W->getCond()) {
976 Block = ExitConditionBlock;
977 EntryConditionBlock = addStmt(C);
Ted Kremenek64f918f2009-04-28 03:09:44 +0000978 assert(Block == EntryConditionBlock);
Ted Kremenek2504e8b2009-05-02 00:13:27 +0000979 if (Block) {
980 if (!FinishBlock(EntryConditionBlock))
981 return 0;
982 }
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000983 }
Ted Kremenek73543912007-08-23 21:42:29 +0000984
985 // The condition block is the implicit successor for the loop body as
986 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000987 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000988
989 // Process the loop body.
990 {
Ted Kremenek64f918f2009-04-28 03:09:44 +0000991 assert(W->getBody());
Ted Kremenek73543912007-08-23 21:42:29 +0000992
993 // Save the current values for Block, Succ, and continue and break targets
994 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
995 save_continue(ContinueTargetBlock),
996 save_break(BreakTargetBlock);
Ted Kremenek64f918f2009-04-28 03:09:44 +0000997
998 // Create an empty block to represent the transition block for looping
999 // back to the head of the loop.
1000 Block = 0;
1001 assert(Succ == EntryConditionBlock);
1002 Succ = createBlock();
1003 Succ->setLoopTarget(W);
1004 ContinueTargetBlock = Succ;
Ted Kremenek73543912007-08-23 21:42:29 +00001005
1006 // All breaks should go to the code following the loop.
1007 BreakTargetBlock = LoopSuccessor;
1008
1009 // NULL out Block to force lazy instantiation of blocks for the body.
1010 Block = NULL;
1011
1012 // Create the body. The returned block is the entry to the loop body.
1013 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenek390b9762007-08-30 18:39:40 +00001014
1015 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +00001016 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001017 else if (Block) {
1018 if (!FinishBlock(BodyBlock))
1019 return 0;
1020 }
Ted Kremenek73543912007-08-23 21:42:29 +00001021
1022 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001023 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +00001024 }
1025
1026 // Link up the condition block with the code that follows the loop.
1027 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001028 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +00001029
1030 // There can be no more statements in the condition block
1031 // since we loop back to this block. NULL out Block to force
1032 // lazy creation of another block.
1033 Block = NULL;
1034
1035 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek9ff572c2008-02-27 07:20:00 +00001036 Succ = EntryConditionBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001037 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001038}
Ted Kremenekc74ac3e2008-12-09 20:20:09 +00001039
1040CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1041 // FIXME: This isn't complete. We basically treat @throw like a return
1042 // statement.
1043
1044 // If we were in the middle of a block we stop processing that block
1045 // and reverse its statements.
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001046 if (Block) {
1047 if (!FinishBlock(Block))
1048 return 0;
1049 }
Ted Kremenekc74ac3e2008-12-09 20:20:09 +00001050
1051 // Create the new block.
1052 Block = createBlock(false);
1053
1054 // The Exit block is the only successor.
1055 Block->addSuccessor(&cfg->getExit());
1056
1057 // Add the statement to the block. This may create new blocks
1058 // if S contains control-flow (short-circuit operations).
1059 return addStmt(S);
1060}
Ted Kremenek73543912007-08-23 21:42:29 +00001061
1062CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
1063 // "do...while" is a control-flow statement. Thus we stop processing the
1064 // current block.
1065
1066 CFGBlock* LoopSuccessor = NULL;
1067
1068 if (Block) {
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001069 if (!FinishBlock(Block))
1070 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +00001071 LoopSuccessor = Block;
1072 }
1073 else LoopSuccessor = Succ;
1074
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001075 // Because of short-circuit evaluation, the condition of the loop
1076 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
1077 // blocks that evaluate the condition.
1078 CFGBlock* ExitConditionBlock = createBlock(false);
1079 CFGBlock* EntryConditionBlock = ExitConditionBlock;
1080
1081 // Set the terminator for the "exit" condition block.
1082 ExitConditionBlock->setTerminator(D);
Ted Kremenek73543912007-08-23 21:42:29 +00001083
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001084 // Now add the actual condition to the condition block. Because the
1085 // condition itself may contain control-flow, new blocks may be created.
1086 if (Stmt* C = D->getCond()) {
1087 Block = ExitConditionBlock;
1088 EntryConditionBlock = addStmt(C);
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001089 if (Block) {
1090 if (!FinishBlock(EntryConditionBlock))
1091 return 0;
1092 }
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001093 }
Ted Kremenek73543912007-08-23 21:42:29 +00001094
Ted Kremenek9ff572c2008-02-27 07:20:00 +00001095 // The condition block is the implicit successor for the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001096 Succ = EntryConditionBlock;
1097
Ted Kremenek73543912007-08-23 21:42:29 +00001098 // Process the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001099 CFGBlock* BodyBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +00001100 {
1101 assert (D->getBody());
1102
1103 // Save the current values for Block, Succ, and continue and break targets
1104 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1105 save_continue(ContinueTargetBlock),
1106 save_break(BreakTargetBlock);
1107
1108 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001109 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001110
1111 // All breaks should go to the code following the loop.
1112 BreakTargetBlock = LoopSuccessor;
1113
1114 // NULL out Block to force lazy instantiation of blocks for the body.
1115 Block = NULL;
1116
1117 // Create the body. The returned block is the entry to the loop body.
1118 BodyBlock = Visit(D->getBody());
Ted Kremenek73543912007-08-23 21:42:29 +00001119
Ted Kremenek390b9762007-08-30 18:39:40 +00001120 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +00001121 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001122 else if (Block) {
1123 if (!FinishBlock(BodyBlock))
1124 return 0;
1125 }
Ted Kremenek390b9762007-08-30 18:39:40 +00001126
Ted Kremenek1b697ca2009-04-28 04:22:00 +00001127 // Add an intermediate block between the BodyBlock and the
1128 // ExitConditionBlock to represent the "loop back" transition.
1129 // Create an empty block to represent the transition block for looping
1130 // back to the head of the loop.
1131 // FIXME: Can we do this more efficiently without adding another block?
1132 Block = NULL;
1133 Succ = BodyBlock;
1134 CFGBlock *LoopBackBlock = createBlock();
1135 LoopBackBlock->setLoopTarget(D);
1136
Ted Kremenek73543912007-08-23 21:42:29 +00001137 // Add the loop body entry as a successor to the condition.
Ted Kremenek1b697ca2009-04-28 04:22:00 +00001138 ExitConditionBlock->addSuccessor(LoopBackBlock);
Ted Kremenek73543912007-08-23 21:42:29 +00001139 }
1140
1141 // Link up the condition block with the code that follows the loop.
1142 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001143 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +00001144
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001145 // There can be no more statements in the body block(s)
1146 // since we loop back to the body. NULL out Block to force
Ted Kremenek73543912007-08-23 21:42:29 +00001147 // lazy creation of another block.
1148 Block = NULL;
1149
1150 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek9ff572c2008-02-27 07:20:00 +00001151 Succ = BodyBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001152 return BodyBlock;
1153}
1154
1155CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1156 // "continue" is a control-flow statement. Thus we stop processing the
1157 // current block.
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001158 if (Block) {
1159 if (!FinishBlock(Block))
1160 return 0;
1161 }
Ted Kremenek73543912007-08-23 21:42:29 +00001162
1163 // Now create a new block that ends with the continue statement.
1164 Block = createBlock(false);
1165 Block->setTerminator(C);
1166
1167 // If there is no target for the continue, then we are looking at an
Ted Kremenek819b7c02009-04-07 18:53:24 +00001168 // incomplete AST. This means the CFG cannot be constructed.
1169 if (ContinueTargetBlock)
1170 Block->addSuccessor(ContinueTargetBlock);
1171 else
1172 badCFG = true;
Ted Kremenek73543912007-08-23 21:42:29 +00001173
1174 return Block;
1175}
1176
1177CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
1178 // "break" is a control-flow statement. Thus we stop processing the
1179 // current block.
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001180 if (Block) {
1181 if (!FinishBlock(Block))
1182 return 0;
1183 }
Ted Kremenek73543912007-08-23 21:42:29 +00001184
1185 // Now create a new block that ends with the continue statement.
1186 Block = createBlock(false);
1187 Block->setTerminator(B);
1188
1189 // If there is no target for the break, then we are looking at an
Ted Kremenek819b7c02009-04-07 18:53:24 +00001190 // incomplete AST. This means that the CFG cannot be constructed.
1191 if (BreakTargetBlock)
1192 Block->addSuccessor(BreakTargetBlock);
1193 else
1194 badCFG = true;
1195
Ted Kremenek73543912007-08-23 21:42:29 +00001196
1197 return Block;
1198}
1199
Ted Kremenek79f0a632008-04-16 21:10:48 +00001200CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek73543912007-08-23 21:42:29 +00001201 // "switch" is a control-flow statement. Thus we stop processing the
1202 // current block.
1203 CFGBlock* SwitchSuccessor = NULL;
1204
1205 if (Block) {
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001206 if (!FinishBlock(Block))
1207 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +00001208 SwitchSuccessor = Block;
1209 }
1210 else SwitchSuccessor = Succ;
1211
1212 // Save the current "switch" context.
1213 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek97bc3422008-02-13 22:05:39 +00001214 save_break(BreakTargetBlock),
1215 save_default(DefaultCaseBlock);
1216
1217 // Set the "default" case to be the block after the switch statement.
1218 // If the switch statement contains a "default:", this value will
1219 // be overwritten with the block for that code.
1220 DefaultCaseBlock = SwitchSuccessor;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001221
Ted Kremenek73543912007-08-23 21:42:29 +00001222 // Create a new block that will contain the switch statement.
1223 SwitchTerminatedBlock = createBlock(false);
1224
Ted Kremenek73543912007-08-23 21:42:29 +00001225 // Now process the switch body. The code after the switch is the implicit
1226 // successor.
1227 Succ = SwitchSuccessor;
1228 BreakTargetBlock = SwitchSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +00001229
1230 // When visiting the body, the case statements should automatically get
1231 // linked up to the switch. We also don't keep a pointer to the body,
1232 // since all control-flow from the switch goes to case/default statements.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001233 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001234 Block = NULL;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001235 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001236 if (Block) {
1237 if (!FinishBlock(BodyBlock))
1238 return 0;
1239 }
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001240
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001241 // If we have no "default:" case, the default transition is to the
1242 // code following the switch body.
Ted Kremenek97bc3422008-02-13 22:05:39 +00001243 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001244
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001245 // Add the terminator and condition in the switch block.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001246 SwitchTerminatedBlock->setTerminator(Terminator);
1247 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek73543912007-08-23 21:42:29 +00001248 Block = SwitchTerminatedBlock;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001249
Ted Kremenek79f0a632008-04-16 21:10:48 +00001250 return addStmt(Terminator->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +00001251}
1252
Ted Kremenek79f0a632008-04-16 21:10:48 +00001253CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Ted Kremenek97bc3422008-02-13 22:05:39 +00001254 // CaseStmts are essentially labels, so they are the
Ted Kremenek73543912007-08-23 21:42:29 +00001255 // first statement in a block.
Ted Kremenek44659d82007-08-30 18:48:11 +00001256
Ted Kremenek79f0a632008-04-16 21:10:48 +00001257 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek44659d82007-08-30 18:48:11 +00001258 CFGBlock* CaseBlock = Block;
1259 if (!CaseBlock) CaseBlock = createBlock();
1260
Ted Kremenek97bc3422008-02-13 22:05:39 +00001261 // Cases statements partition blocks, so this is the top of
1262 // the basic block we were processing (the "case XXX:" is the label).
Ted Kremenek79f0a632008-04-16 21:10:48 +00001263 CaseBlock->setLabel(Terminator);
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001264 if (!FinishBlock(CaseBlock))
1265 return 0;
Ted Kremenek73543912007-08-23 21:42:29 +00001266
1267 // Add this block to the list of successors for the block with the
1268 // switch statement.
Ted Kremenek97bc3422008-02-13 22:05:39 +00001269 assert (SwitchTerminatedBlock);
1270 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +00001271
1272 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1273 Block = NULL;
1274
1275 // This block is now the implicit successor of other blocks.
1276 Succ = CaseBlock;
1277
Ted Kremenek82e8a192008-03-15 07:45:02 +00001278 return CaseBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001279}
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001280
Ted Kremenek79f0a632008-04-16 21:10:48 +00001281CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1282 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek97bc3422008-02-13 22:05:39 +00001283 DefaultCaseBlock = Block;
1284 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1285
1286 // Default statements partition blocks, so this is the top of
1287 // the basic block we were processing (the "default:" is the label).
Ted Kremenek79f0a632008-04-16 21:10:48 +00001288 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001289 if (!FinishBlock(DefaultCaseBlock))
1290 return 0;
Ted Kremenek97bc3422008-02-13 22:05:39 +00001291
1292 // Unlike case statements, we don't add the default block to the
1293 // successors for the switch statement immediately. This is done
1294 // when we finish processing the switch statement. This allows for
1295 // the default case (including a fall-through to the code after the
1296 // switch statement) to always be the last successor of a switch-terminated
1297 // block.
1298
1299 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1300 Block = NULL;
1301
1302 // This block is now the implicit successor of other blocks.
1303 Succ = DefaultCaseBlock;
1304
1305 return DefaultCaseBlock;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001306}
Ted Kremenek73543912007-08-23 21:42:29 +00001307
Ted Kremenek0edd3a92007-08-28 19:26:49 +00001308CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1309 // Lazily create the indirect-goto dispatch block if there isn't one
1310 // already.
1311 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
1312
1313 if (!IBlock) {
1314 IBlock = createBlock(false);
1315 cfg->setIndirectGotoBlock(IBlock);
1316 }
1317
1318 // IndirectGoto is a control-flow statement. Thus we stop processing the
1319 // current block and create a new one.
Ted Kremenek2504e8b2009-05-02 00:13:27 +00001320 if (Block) {
1321 if (!FinishBlock(Block))
1322 return 0;
1323 }
Ted Kremenek0edd3a92007-08-28 19:26:49 +00001324 Block = createBlock(false);
1325 Block->setTerminator(I);
1326 Block->addSuccessor(IBlock);
1327 return addStmt(I->getTarget());
1328}
1329
Ted Kremenek73543912007-08-23 21:42:29 +00001330
Ted Kremenekd6e50602007-08-23 21:26:19 +00001331} // end anonymous namespace
Ted Kremenek4db5b452007-08-23 16:51:22 +00001332
1333/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
1334/// block has no successors or predecessors. If this is the first block
1335/// created in the CFG, it is automatically set to be the Entry and Exit
1336/// of the CFG.
Ted Kremenek14594572007-09-05 20:02:05 +00001337CFGBlock* CFG::createBlock() {
Ted Kremenek4db5b452007-08-23 16:51:22 +00001338 bool first_block = begin() == end();
1339
1340 // Create the block.
Ted Kremenek14594572007-09-05 20:02:05 +00001341 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek4db5b452007-08-23 16:51:22 +00001342
1343 // If this is the first block, set it as the Entry and Exit.
1344 if (first_block) Entry = Exit = &front();
1345
1346 // Return the block.
1347 return &front();
Ted Kremenek97f75312007-08-21 21:42:03 +00001348}
1349
Ted Kremenek4db5b452007-08-23 16:51:22 +00001350/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1351/// CFG is returned to the caller.
1352CFG* CFG::buildCFG(Stmt* Statement) {
1353 CFGBuilder Builder;
1354 return Builder.buildCFG(Statement);
1355}
1356
1357/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek97f75312007-08-21 21:42:03 +00001358void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1359
Ted Kremenek3a819822007-10-01 19:33:33 +00001360//===----------------------------------------------------------------------===//
1361// CFG: Queries for BlkExprs.
1362//===----------------------------------------------------------------------===//
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001363
Ted Kremenek3a819822007-10-01 19:33:33 +00001364namespace {
Ted Kremenekab6c5902008-01-17 20:48:37 +00001365 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek3a819822007-10-01 19:33:33 +00001366}
1367
Ted Kremenek79f0a632008-04-16 21:10:48 +00001368static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1369 if (!Terminator)
Ted Kremenekc6fda602008-01-26 00:03:27 +00001370 return;
1371
Ted Kremenek79f0a632008-04-16 21:10:48 +00001372 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001373 if (!*I) continue;
1374
1375 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1376 if (B->isAssignmentOp()) Set.insert(B);
1377
1378 FindSubExprAssignments(*I, Set);
1379 }
1380}
1381
Ted Kremenek3a819822007-10-01 19:33:33 +00001382static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1383 BlkExprMapTy* M = new BlkExprMapTy();
1384
Ted Kremenekc6fda602008-01-26 00:03:27 +00001385 // Look for assignments that are used as subexpressions. These are the
Ted Kremenek79f0a632008-04-16 21:10:48 +00001386 // only assignments that we want to *possibly* register as a block-level
1387 // expression. Basically, if an assignment occurs both in a subexpression
1388 // and at the block-level, it is a block-level expression.
Ted Kremenekc6fda602008-01-26 00:03:27 +00001389 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
1390
Ted Kremenek3a819822007-10-01 19:33:33 +00001391 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1392 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc6fda602008-01-26 00:03:27 +00001393 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenekab6c5902008-01-17 20:48:37 +00001394
Ted Kremenek79f0a632008-04-16 21:10:48 +00001395 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
1396
1397 // Iterate over the statements again on identify the Expr* and Stmt* at
1398 // the block-level that are block-level expressions.
1399
Ted Kremenekc6fda602008-01-26 00:03:27 +00001400 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek79f0a632008-04-16 21:10:48 +00001401 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001402
Ted Kremenek79f0a632008-04-16 21:10:48 +00001403 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001404 // Assignment expressions that are not nested within another
1405 // expression are really "statements" whose value is never
1406 // used by another expression.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001407 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenekc6fda602008-01-26 00:03:27 +00001408 continue;
1409 }
Ted Kremenek79f0a632008-04-16 21:10:48 +00001410 else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001411 // Special handling for statement expressions. The last statement
1412 // in the statement expression is also a block-level expr.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001413 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenekab6c5902008-01-17 20:48:37 +00001414 if (!C->body_empty()) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001415 unsigned x = M->size();
Ted Kremenekab6c5902008-01-17 20:48:37 +00001416 (*M)[C->body_back()] = x;
1417 }
1418 }
Ted Kremenek5b4eb172008-01-25 23:22:27 +00001419
Ted Kremenekc6fda602008-01-26 00:03:27 +00001420 unsigned x = M->size();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001421 (*M)[Exp] = x;
Ted Kremenekc6fda602008-01-26 00:03:27 +00001422 }
1423
Ted Kremenek79f0a632008-04-16 21:10:48 +00001424 // Look at terminators. The condition is a block-level expression.
1425
Ted Kremenek16516e22008-11-12 21:11:49 +00001426 Stmt* S = I->getTerminatorCondition();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001427
Ted Kremenek16516e22008-11-12 21:11:49 +00001428 if (S && M->find(S) == M->end()) {
Ted Kremenek79f0a632008-04-16 21:10:48 +00001429 unsigned x = M->size();
Ted Kremenek16516e22008-11-12 21:11:49 +00001430 (*M)[S] = x;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001431 }
1432 }
1433
Ted Kremenek3a819822007-10-01 19:33:33 +00001434 return M;
1435}
1436
Ted Kremenekab6c5902008-01-17 20:48:37 +00001437CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1438 assert(S != NULL);
Ted Kremenek3a819822007-10-01 19:33:33 +00001439 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
1440
1441 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenekab6c5902008-01-17 20:48:37 +00001442 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3a819822007-10-01 19:33:33 +00001443
1444 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 Kremenekd058a9c2008-04-28 18:00:46 +00001459//===----------------------------------------------------------------------===//
Ted Kremenekd058a9c2008-04-28 18:00:46 +00001460// Cleanup: CFG dstor.
1461//===----------------------------------------------------------------------===//
1462
Ted Kremenek3a819822007-10-01 19:33:33 +00001463CFG::~CFG() {
1464 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1465}
1466
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001467//===----------------------------------------------------------------------===//
1468// CFG pretty printing
1469//===----------------------------------------------------------------------===//
1470
Ted Kremenekd8313202007-08-22 18:22:34 +00001471namespace {
1472
Ted Kremenek98cee3a2008-01-08 18:15:10 +00001473class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Ted Kremenek86afc042007-08-31 22:26:13 +00001474
Ted Kremenek08176a52007-08-31 21:30:12 +00001475 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1476 StmtMapTy StmtMap;
1477 signed CurrentBlock;
1478 unsigned CurrentStmt;
Ted Kremenek86afc042007-08-31 22:26:13 +00001479
Ted Kremenek73543912007-08-23 21:42:29 +00001480public:
Ted Kremenek86afc042007-08-31 22:26:13 +00001481
Ted Kremenek08176a52007-08-31 21:30:12 +00001482 StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) {
1483 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1484 unsigned j = 1;
1485 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1486 BI != BEnd; ++BI, ++j )
1487 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1488 }
1489 }
1490
1491 virtual ~StmtPrinterHelper() {}
1492
1493 void setBlockID(signed i) { CurrentBlock = i; }
1494 void setStmtID(unsigned i) { CurrentStmt = i; }
1495
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001496 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001497
Ted Kremenek79f0a632008-04-16 21:10:48 +00001498 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek08176a52007-08-31 21:30:12 +00001499
1500 if (I == StmtMap.end())
1501 return false;
1502
1503 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
1504 && I->second.second == CurrentStmt)
1505 return false;
1506
Ted Kremenek86afc042007-08-31 22:26:13 +00001507 OS << "[B" << I->second.first << "." << I->second.second << "]";
1508 return true;
Ted Kremenek08176a52007-08-31 21:30:12 +00001509 }
1510};
1511
Ted Kremenek98cee3a2008-01-08 18:15:10 +00001512class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1513 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
1514
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001515 llvm::raw_ostream& OS;
Ted Kremenek08176a52007-08-31 21:30:12 +00001516 StmtPrinterHelper* Helper;
1517public:
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001518 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper)
Ted Kremenek08176a52007-08-31 21:30:12 +00001519 : OS(os), Helper(helper) {}
Ted Kremenek73543912007-08-23 21:42:29 +00001520
1521 void VisitIfStmt(IfStmt* I) {
1522 OS << "if ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001523 I->getCond()->printPretty(OS,Helper);
Ted Kremenek73543912007-08-23 21:42:29 +00001524 }
1525
1526 // Default case.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001527 void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS); }
Ted Kremenek73543912007-08-23 21:42:29 +00001528
1529 void VisitForStmt(ForStmt* F) {
1530 OS << "for (" ;
Ted Kremenek23a1d662007-08-30 21:28:02 +00001531 if (F->getInit()) OS << "...";
1532 OS << "; ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001533 if (Stmt* C = F->getCond()) C->printPretty(OS,Helper);
Ted Kremenek23a1d662007-08-30 21:28:02 +00001534 OS << "; ";
1535 if (F->getInc()) OS << "...";
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001536 OS << ")";
Ted Kremenek73543912007-08-23 21:42:29 +00001537 }
1538
1539 void VisitWhileStmt(WhileStmt* W) {
1540 OS << "while " ;
Ted Kremenek08176a52007-08-31 21:30:12 +00001541 if (Stmt* C = W->getCond()) C->printPretty(OS,Helper);
Ted Kremenek73543912007-08-23 21:42:29 +00001542 }
1543
1544 void VisitDoStmt(DoStmt* D) {
1545 OS << "do ... while ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001546 if (Stmt* C = D->getCond()) C->printPretty(OS,Helper);
Ted Kremenek65cfa562007-08-27 21:27:44 +00001547 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001548
Ted Kremenek79f0a632008-04-16 21:10:48 +00001549 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek65cfa562007-08-27 21:27:44 +00001550 OS << "switch ";
Ted Kremenek79f0a632008-04-16 21:10:48 +00001551 Terminator->getCond()->printPretty(OS,Helper);
Ted Kremenek65cfa562007-08-27 21:27:44 +00001552 }
1553
Ted Kremenek621e1592007-08-31 21:49:40 +00001554 void VisitConditionalOperator(ConditionalOperator* C) {
1555 C->getCond()->printPretty(OS,Helper);
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001556 OS << " ? ... : ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001557 }
1558
Ted Kremenek2025cc92007-08-31 22:29:13 +00001559 void VisitChooseExpr(ChooseExpr* C) {
1560 OS << "__builtin_choose_expr( ";
1561 C->getCond()->printPretty(OS,Helper);
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001562 OS << " )";
Ted Kremenek2025cc92007-08-31 22:29:13 +00001563 }
1564
Ted Kremenek86afc042007-08-31 22:26:13 +00001565 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1566 OS << "goto *";
1567 I->getTarget()->printPretty(OS,Helper);
Ted Kremenek86afc042007-08-31 22:26:13 +00001568 }
1569
Ted Kremenek621e1592007-08-31 21:49:40 +00001570 void VisitBinaryOperator(BinaryOperator* B) {
1571 if (!B->isLogicalOp()) {
1572 VisitExpr(B);
1573 return;
1574 }
1575
1576 B->getLHS()->printPretty(OS,Helper);
1577
1578 switch (B->getOpcode()) {
1579 case BinaryOperator::LOr:
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001580 OS << " || ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001581 return;
1582 case BinaryOperator::LAnd:
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001583 OS << " && ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001584 return;
1585 default:
1586 assert(false && "Invalid logical operator.");
1587 }
1588 }
1589
Ted Kremenekcfaae762007-08-27 21:54:41 +00001590 void VisitExpr(Expr* E) {
Ted Kremenek08176a52007-08-31 21:30:12 +00001591 E->printPretty(OS,Helper);
Ted Kremenekcfaae762007-08-27 21:54:41 +00001592 }
Ted Kremenek73543912007-08-23 21:42:29 +00001593};
Ted Kremenek08176a52007-08-31 21:30:12 +00001594
1595
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001596void print_stmt(llvm::raw_ostream&OS, StmtPrinterHelper* Helper, Stmt* Terminator) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001597 if (Helper) {
1598 // special printing for statement-expressions.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001599 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001600 CompoundStmt* Sub = SE->getSubStmt();
1601
1602 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek16e3b9a2007-08-31 22:47:06 +00001603 OS << "({ ... ; ";
Ted Kremenek256a2592007-10-29 20:41:04 +00001604 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek16e3b9a2007-08-31 22:47:06 +00001605 OS << " })\n";
Ted Kremenek86afc042007-08-31 22:26:13 +00001606 return;
1607 }
1608 }
1609
1610 // special printing for comma expressions.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001611 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001612 if (B->getOpcode() == BinaryOperator::Comma) {
1613 OS << "... , ";
1614 Helper->handledStmt(B->getRHS(),OS);
1615 OS << '\n';
1616 return;
1617 }
1618 }
1619 }
1620
Ted Kremenek79f0a632008-04-16 21:10:48 +00001621 Terminator->printPretty(OS, Helper);
Ted Kremenek86afc042007-08-31 22:26:13 +00001622
1623 // Expressions need a newline.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001624 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek86afc042007-08-31 22:26:13 +00001625}
1626
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001627void print_block(llvm::raw_ostream& OS, const CFG* cfg, const CFGBlock& B,
Ted Kremenek08176a52007-08-31 21:30:12 +00001628 StmtPrinterHelper* Helper, bool print_edges) {
1629
1630 if (Helper) Helper->setBlockID(B.getBlockID());
1631
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001632 // Print the header.
Ted Kremenek08176a52007-08-31 21:30:12 +00001633 OS << "\n [ B" << B.getBlockID();
1634
1635 if (&B == &cfg->getEntry())
1636 OS << " (ENTRY) ]\n";
1637 else if (&B == &cfg->getExit())
1638 OS << " (EXIT) ]\n";
1639 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001640 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek08176a52007-08-31 21:30:12 +00001641 else
1642 OS << " ]\n";
1643
Ted Kremenekec055e12007-08-29 23:20:49 +00001644 // Print the label of this block.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001645 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek08176a52007-08-31 21:30:12 +00001646
1647 if (print_edges)
1648 OS << " ";
1649
Ted Kremenek79f0a632008-04-16 21:10:48 +00001650 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenekec055e12007-08-29 23:20:49 +00001651 OS << L->getName();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001652 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenekec055e12007-08-29 23:20:49 +00001653 OS << "case ";
1654 C->getLHS()->printPretty(OS);
1655 if (C->getRHS()) {
1656 OS << " ... ";
1657 C->getRHS()->printPretty(OS);
1658 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001659 }
Ted Kremenek79f0a632008-04-16 21:10:48 +00001660 else if (isa<DefaultStmt>(Terminator))
Ted Kremenekec055e12007-08-29 23:20:49 +00001661 OS << "default";
Ted Kremenek08176a52007-08-31 21:30:12 +00001662 else
1663 assert(false && "Invalid label statement in CFGBlock.");
1664
Ted Kremenekec055e12007-08-29 23:20:49 +00001665 OS << ":\n";
1666 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001667
Ted Kremenek97f75312007-08-21 21:42:03 +00001668 // Iterate through the statements in the block and print them.
Ted Kremenek97f75312007-08-21 21:42:03 +00001669 unsigned j = 1;
Ted Kremenek08176a52007-08-31 21:30:12 +00001670
1671 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1672 I != E ; ++I, ++j ) {
1673
Ted Kremenekec055e12007-08-29 23:20:49 +00001674 // Print the statement # in the basic block and the statement itself.
Ted Kremenek08176a52007-08-31 21:30:12 +00001675 if (print_edges)
1676 OS << " ";
1677
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001678 OS << llvm::format("%3d", j) << ": ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001679
1680 if (Helper)
1681 Helper->setStmtID(j);
Ted Kremenek86afc042007-08-31 22:26:13 +00001682
1683 print_stmt(OS,Helper,*I);
Ted Kremenek97f75312007-08-21 21:42:03 +00001684 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001685
Ted Kremenekec055e12007-08-29 23:20:49 +00001686 // Print the terminator of this block.
Ted Kremenek08176a52007-08-31 21:30:12 +00001687 if (B.getTerminator()) {
1688 if (print_edges)
1689 OS << " ";
1690
Ted Kremenekec055e12007-08-29 23:20:49 +00001691 OS << " T: ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001692
1693 if (Helper) Helper->setBlockID(-1);
1694
1695 CFGBlockTerminatorPrint TPrinter(OS,Helper);
1696 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001697 OS << '\n';
Ted Kremenek97f75312007-08-21 21:42:03 +00001698 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001699
Ted Kremenekec055e12007-08-29 23:20:49 +00001700 if (print_edges) {
1701 // Print the predecessors of this block.
Ted Kremenek08176a52007-08-31 21:30:12 +00001702 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenekec055e12007-08-29 23:20:49 +00001703 unsigned i = 0;
Ted Kremenekec055e12007-08-29 23:20:49 +00001704
Ted Kremenek08176a52007-08-31 21:30:12 +00001705 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1706 I != E; ++I, ++i) {
1707
1708 if (i == 8 || (i-8) == 0)
1709 OS << "\n ";
1710
Ted Kremenekec055e12007-08-29 23:20:49 +00001711 OS << " B" << (*I)->getBlockID();
1712 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001713
1714 OS << '\n';
1715
1716 // Print the successors of this block.
1717 OS << " Successors (" << B.succ_size() << "):";
1718 i = 0;
1719
1720 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1721 I != E; ++I, ++i) {
1722
1723 if (i == 8 || (i-8) % 10 == 0)
1724 OS << "\n ";
1725
1726 OS << " B" << (*I)->getBlockID();
1727 }
1728
Ted Kremenekec055e12007-08-29 23:20:49 +00001729 OS << '\n';
Ted Kremenek97f75312007-08-21 21:42:03 +00001730 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001731}
1732
1733} // end anonymous namespace
1734
1735/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001736void CFG::dump() const { print(llvm::errs()); }
Ted Kremenek08176a52007-08-31 21:30:12 +00001737
1738/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001739void CFG::print(llvm::raw_ostream& OS) const {
Ted Kremenek08176a52007-08-31 21:30:12 +00001740
1741 StmtPrinterHelper Helper(this);
1742
1743 // Print the entry block.
1744 print_block(OS, this, getEntry(), &Helper, true);
1745
1746 // Iterate through the CFGBlocks and print them one by one.
1747 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1748 // Skip the entry block, because we already printed it.
1749 if (&(*I) == &getEntry() || &(*I) == &getExit())
1750 continue;
1751
1752 print_block(OS, this, *I, &Helper, true);
1753 }
1754
1755 // Print the exit block.
1756 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd19e99e2008-11-24 20:50:24 +00001757 OS.flush();
Ted Kremenek08176a52007-08-31 21:30:12 +00001758}
1759
1760/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001761void CFGBlock::dump(const CFG* cfg) const { print(llvm::errs(), cfg); }
Ted Kremenek08176a52007-08-31 21:30:12 +00001762
1763/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1764/// Generally this will only be called from CFG::print.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001765void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg) const {
Ted Kremenek08176a52007-08-31 21:30:12 +00001766 StmtPrinterHelper Helper(cfg);
1767 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek4db5b452007-08-23 16:51:22 +00001768}
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001769
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001770/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001771void CFGBlock::printTerminator(llvm::raw_ostream& OS) const {
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001772 CFGBlockTerminatorPrint TPrinter(OS,NULL);
1773 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1774}
1775
Ted Kremenek16516e22008-11-12 21:11:49 +00001776Stmt* CFGBlock::getTerminatorCondition() {
Ted Kremenek79f0a632008-04-16 21:10:48 +00001777
1778 if (!Terminator)
1779 return NULL;
1780
1781 Expr* E = NULL;
1782
1783 switch (Terminator->getStmtClass()) {
1784 default:
1785 break;
1786
1787 case Stmt::ForStmtClass:
1788 E = cast<ForStmt>(Terminator)->getCond();
1789 break;
1790
1791 case Stmt::WhileStmtClass:
1792 E = cast<WhileStmt>(Terminator)->getCond();
1793 break;
1794
1795 case Stmt::DoStmtClass:
1796 E = cast<DoStmt>(Terminator)->getCond();
1797 break;
1798
1799 case Stmt::IfStmtClass:
1800 E = cast<IfStmt>(Terminator)->getCond();
1801 break;
1802
1803 case Stmt::ChooseExprClass:
1804 E = cast<ChooseExpr>(Terminator)->getCond();
1805 break;
1806
1807 case Stmt::IndirectGotoStmtClass:
1808 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1809 break;
1810
1811 case Stmt::SwitchStmtClass:
1812 E = cast<SwitchStmt>(Terminator)->getCond();
1813 break;
1814
1815 case Stmt::ConditionalOperatorClass:
1816 E = cast<ConditionalOperator>(Terminator)->getCond();
1817 break;
1818
1819 case Stmt::BinaryOperatorClass: // '&&' and '||'
1820 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek16516e22008-11-12 21:11:49 +00001821 break;
1822
1823 case Stmt::ObjCForCollectionStmtClass:
1824 return Terminator;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001825 }
1826
1827 return E ? E->IgnoreParens() : NULL;
1828}
1829
Ted Kremenekbdbd1b52008-05-16 16:06:00 +00001830bool CFGBlock::hasBinaryBranchTerminator() const {
1831
1832 if (!Terminator)
1833 return false;
1834
1835 Expr* E = NULL;
1836
1837 switch (Terminator->getStmtClass()) {
1838 default:
1839 return false;
1840
1841 case Stmt::ForStmtClass:
1842 case Stmt::WhileStmtClass:
1843 case Stmt::DoStmtClass:
1844 case Stmt::IfStmtClass:
1845 case Stmt::ChooseExprClass:
1846 case Stmt::ConditionalOperatorClass:
1847 case Stmt::BinaryOperatorClass:
1848 return true;
1849 }
1850
1851 return E ? E->IgnoreParens() : NULL;
1852}
1853
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001854
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001855//===----------------------------------------------------------------------===//
1856// CFG Graphviz Visualization
1857//===----------------------------------------------------------------------===//
1858
Ted Kremenek08176a52007-08-31 21:30:12 +00001859
1860#ifndef NDEBUG
Chris Lattner26002172007-09-17 06:16:32 +00001861static StmtPrinterHelper* GraphHelper;
Ted Kremenek08176a52007-08-31 21:30:12 +00001862#endif
1863
1864void CFG::viewCFG() const {
1865#ifndef NDEBUG
1866 StmtPrinterHelper H(this);
1867 GraphHelper = &H;
1868 llvm::ViewGraph(this,"CFG");
1869 GraphHelper = NULL;
Ted Kremenek08176a52007-08-31 21:30:12 +00001870#endif
1871}
1872
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001873namespace llvm {
1874template<>
1875struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
1876 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
1877
Hartmut Kaiser752a0052007-09-16 00:28:28 +00001878#ifndef NDEBUG
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001879 std::string OutSStr;
1880 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek08176a52007-08-31 21:30:12 +00001881 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001882 std::string& OutStr = Out.str();
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001883
1884 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1885
1886 // Process string output to make it nicer...
1887 for (unsigned i = 0; i != OutStr.length(); ++i)
1888 if (OutStr[i] == '\n') { // Left justify
1889 OutStr[i] = '\\';
1890 OutStr.insert(OutStr.begin()+i+1, 'l');
1891 }
1892
1893 return OutStr;
Hartmut Kaiser752a0052007-09-16 00:28:28 +00001894#else
1895 return "";
1896#endif
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001897 }
1898};
1899} // end namespace llvm