blob: 4c2ecb92f606094d009b8e7ba9731ae666f0e88a [file] [log] [blame]
Ted Kremenekfddd5182007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenekfddd5182007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekbd048782009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Ted Kremenekc310e932007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000019#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000020#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek7dba8602007-08-29 21:56:09 +000021#include "llvm/Support/GraphWriter.h"
Ted Kremenek7e3a89d2007-12-17 19:35:20 +000022#include "llvm/Support/Streams.h"
Ted Kremenek6fa9b882008-01-08 18:15:10 +000023#include "llvm/Support/Compiler.h"
Ted Kremenek274f4332008-04-28 18:00:46 +000024#include <llvm/Support/Allocator.h>
Ted Kremeneka95d3752008-09-13 05:16:45 +000025#include <llvm/Support/Format.h>
Ted Kremenek83c01da2008-01-11 00:40:29 +000026
Ted Kremenekfddd5182007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor4afa39d2009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump6d9828c2009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000037}
Mike Stump6d9828c2009-07-17 01:31:16 +000038
Ted Kremeneka34ea072008-08-04 22:51:42 +000039/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +000040/// The builder is stateful: an instance of the builder should be used to only
41/// construct a single CFG.
42///
43/// Example usage:
44///
45/// CFGBuilder builder;
46/// CFG* cfg = builder.BuildAST(stmt1);
47///
Mike Stump6d9828c2009-07-17 01:31:16 +000048/// CFG construction is done via a recursive walk of an AST. We actually parse
49/// the AST in reverse order so that the successor of a basic block is
50/// constructed prior to its predecessor. This allows us to nicely capture
51/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +000052///
Ted Kremenek4f880632009-07-17 22:18:43 +000053class VISIBILITY_HIDDEN CFGBuilder {
Mike Stumpe5af3ce2009-07-20 23:24:15 +000054 ASTContext *Context;
Ted Kremenekfddd5182007-08-21 21:42:03 +000055 CFG* cfg;
56 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000057 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000058 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000059 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000060 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000061 CFGBlock* DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +000062
Ted Kremenek19bb3562007-08-28 19:26:49 +000063 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000064 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
65 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +000066
67 // A list of blocks that end with a "goto" that must be backpatched to their
68 // resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +000069 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000070 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +000071
Ted Kremenek19bb3562007-08-28 19:26:49 +000072 // A list of labels whose address has been taken (for indirect gotos).
73 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
74 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +000075
76public:
Ted Kremenek026473c2007-08-23 16:51:22 +000077 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000078 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000079 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenekfddd5182007-08-21 21:42:03 +000080 // Create an empty CFG.
Mike Stump6d9828c2009-07-17 01:31:16 +000081 cfg = new CFG();
Ted Kremenekfddd5182007-08-21 21:42:03 +000082 }
Mike Stump6d9828c2009-07-17 01:31:16 +000083
Ted Kremenekfddd5182007-08-21 21:42:03 +000084 ~CFGBuilder() { delete cfg; }
Mike Stump6d9828c2009-07-17 01:31:16 +000085
Ted Kremenekd4fdee32007-08-23 21:42:29 +000086 // buildCFG - Used by external clients to construct the CFG.
Mike Stumpe5af3ce2009-07-20 23:24:15 +000087 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump6d9828c2009-07-17 01:31:16 +000088
Ted Kremenek4f880632009-07-17 22:18:43 +000089private:
90 // Visitors to walk an AST and construct the CFG.
91 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
92 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
Ted Kremenek13fc08a2009-07-18 00:47:21 +000093 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
94 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +000095 CFGBlock *VisitBreakStmt(BreakStmt *B);
96 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
97 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +000098 CFGBlock *VisitChooseExpr(ChooseExpr *C);
99 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
100 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
101 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000102 CFGBlock *VisitDeclStmt(DeclStmt *DS);
103 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000104 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
105 CFGBlock *VisitDoStmt(DoStmt *D);
106 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000107 CFGBlock *VisitGotoStmt(GotoStmt* G);
108 CFGBlock *VisitIfStmt(IfStmt *I);
109 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
110 CFGBlock *VisitLabelStmt(LabelStmt *L);
111 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
112 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
113 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
114 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
115 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
116 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000117 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
118 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000119 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
120 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000121
Ted Kremenek4f880632009-07-17 22:18:43 +0000122 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
123 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
124 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000125
Ted Kremenek274f4332008-04-28 18:00:46 +0000126 // NYS == Not Yet Supported
127 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000128 badCFG = true;
129 return Block;
130 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000131
Ted Kremenek4f880632009-07-17 22:18:43 +0000132 void autoCreateBlock() { if (!Block) Block = createBlock(); }
133 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000134 bool FinishBlock(CFGBlock* B);
Ted Kremenek4f880632009-07-17 22:18:43 +0000135 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
136
Ted Kremenek4102af92008-03-13 03:04:22 +0000137 bool badCFG;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000138};
Mike Stump6d9828c2009-07-17 01:31:16 +0000139
Douglas Gregor898574e2008-12-05 23:32:09 +0000140// FIXME: Add support for dependent-sized array types in C++?
141// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000142static VariableArrayType* FindVA(Type* t) {
143 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
144 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
145 if (vat->getSizeExpr())
146 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000147
Ted Kremenek610a09e2008-09-26 22:58:57 +0000148 t = vt->getElementType().getTypePtr();
149 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000150
Ted Kremenek610a09e2008-09-26 22:58:57 +0000151 return 0;
152}
Mike Stump6d9828c2009-07-17 01:31:16 +0000153
154/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
155/// arbitrary statement. Examples include a single expression or a function
156/// body (compound statement). The ownership of the returned CFG is
157/// transferred to the caller. If CFG construction fails, this method returns
158/// NULL.
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000159CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
160 Context = C;
Ted Kremenek4f880632009-07-17 22:18:43 +0000161 assert(cfg);
162 if (!Statement)
163 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000164
Ted Kremenek4102af92008-03-13 03:04:22 +0000165 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000166
167 // Create an empty block that will serve as the exit block for the CFG. Since
168 // this is the first block added to the CFG, it will be implicitly registered
169 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000170 Succ = createBlock();
171 assert (Succ == &cfg->getExit());
172 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000173
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000174 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000175 CFGBlock* B = addStmt(Statement);
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000176 if (!B) B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000177
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000178 if (B) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000179 // Finalize the last constructed block. This usually involves reversing the
180 // order of the statements in the block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000181 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000182
183 // Backpatch the gotos whose label -> block mappings we didn't know when we
184 // encountered them.
185 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000186 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000187
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000188 CFGBlock* B = *I;
189 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
190 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
191
192 // If there is no target for the goto, then we are looking at an
193 // incomplete AST. Handle this by not registering a successor.
194 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000195
196 B->addSuccessor(LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000197 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000198
Ted Kremenek19bb3562007-08-28 19:26:49 +0000199 // Add successors to the Indirect Goto Dispatch block (if we have one).
200 if (CFGBlock* B = cfg->getIndirectGotoBlock())
201 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
202 E = AddressTakenLabels.end(); I != E; ++I ) {
203
204 // Lookup the target block.
205 LabelMapTy::iterator LI = LabelMap.find(*I);
206
207 // If there is no target block that contains label, then we are looking
208 // at an incomplete AST. Handle this by not registering a successor.
209 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000210
211 B->addSuccessor(LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000212 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000213
Ted Kremenek94b33162007-09-17 16:18:02 +0000214 Succ = B;
Ted Kremenek322f58d2007-09-26 21:23:31 +0000215 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000216
217 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000218 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000219
Ted Kremenek4102af92008-03-13 03:04:22 +0000220 if (badCFG) {
221 delete cfg;
222 cfg = NULL;
223 return NULL;
224 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000225
226 // NULL out cfg so that repeated calls to the builder will fail and that the
227 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000228 CFG* t = cfg;
229 cfg = NULL;
230 return t;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000231}
Mike Stump6d9828c2009-07-17 01:31:16 +0000232
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000233/// createBlock - Used to lazily create blocks that are connected
234/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000235CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000236 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000237 if (add_successor && Succ)
238 B->addSuccessor(Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000239 return B;
240}
Mike Stump6d9828c2009-07-17 01:31:16 +0000241
242/// FinishBlock - When the last statement has been added to the block, we must
243/// reverse the statements because they have been inserted in reverse order.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000244bool CFGBuilder::FinishBlock(CFGBlock* B) {
245 if (badCFG)
246 return false;
247
Ted Kremenek4f880632009-07-17 22:18:43 +0000248 assert(B);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000249 B->reverseStmts();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000250 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000251}
252
Ted Kremenek4f880632009-07-17 22:18:43 +0000253/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000254/// blocks for ternary operators, &&, and ||. We also process "," and
255/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4f880632009-07-17 22:18:43 +0000256CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
257tryAgain:
258 switch (S->getStmtClass()) {
259 default:
260 return VisitStmt(S, alwaysAdd);
261
262 case Stmt::AddrLabelExprClass:
263 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
264
265 case Stmt::BinaryOperatorClass:
266 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
267
268 case Stmt::BlockExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000269 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000270
271 case Stmt::BlockDeclRefExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000272 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000273
274 case Stmt::BreakStmtClass:
275 return VisitBreakStmt(cast<BreakStmt>(S));
276
277 case Stmt::CallExprClass:
278 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
279
280 case Stmt::CaseStmtClass:
281 return VisitCaseStmt(cast<CaseStmt>(S));
282
283 case Stmt::ChooseExprClass:
284 return VisitChooseExpr(cast<ChooseExpr>(S));
285
286 case Stmt::CompoundStmtClass:
287 return VisitCompoundStmt(cast<CompoundStmt>(S));
288
289 case Stmt::ConditionalOperatorClass:
290 return VisitConditionalOperator(cast<ConditionalOperator>(S));
291
292 case Stmt::ContinueStmtClass:
293 return VisitContinueStmt(cast<ContinueStmt>(S));
294
295 case Stmt::DeclStmtClass:
296 return VisitDeclStmt(cast<DeclStmt>(S));
297
298 case Stmt::DefaultStmtClass:
299 return VisitDefaultStmt(cast<DefaultStmt>(S));
300
301 case Stmt::DoStmtClass:
302 return VisitDoStmt(cast<DoStmt>(S));
303
304 case Stmt::ForStmtClass:
305 return VisitForStmt(cast<ForStmt>(S));
306
307 case Stmt::GotoStmtClass:
308 return VisitGotoStmt(cast<GotoStmt>(S));
309
310 case Stmt::IfStmtClass:
311 return VisitIfStmt(cast<IfStmt>(S));
312
313 case Stmt::IndirectGotoStmtClass:
314 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
315
316 case Stmt::LabelStmtClass:
317 return VisitLabelStmt(cast<LabelStmt>(S));
318
319 case Stmt::ObjCAtCatchStmtClass:
320 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
321
322 case Stmt::ObjCAtSynchronizedStmtClass:
323 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
324
325 case Stmt::ObjCAtThrowStmtClass:
326 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
327
328 case Stmt::ObjCAtTryStmtClass:
329 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
330
331 case Stmt::ObjCForCollectionStmtClass:
332 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
333
334 case Stmt::ParenExprClass:
335 S = cast<ParenExpr>(S)->getSubExpr();
336 goto tryAgain;
337
338 case Stmt::NullStmtClass:
339 return Block;
340
341 case Stmt::ReturnStmtClass:
342 return VisitReturnStmt(cast<ReturnStmt>(S));
343
344 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000345 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000346
347 case Stmt::StmtExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000348 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000349
350 case Stmt::SwitchStmtClass:
351 return VisitSwitchStmt(cast<SwitchStmt>(S));
352
353 case Stmt::WhileStmtClass:
354 return VisitWhileStmt(cast<WhileStmt>(S));
355 }
356}
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000357
Ted Kremenek4f880632009-07-17 22:18:43 +0000358CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
359 if (alwaysAdd) {
360 autoCreateBlock();
361 Block->appendStmt(S);
Mike Stump6d9828c2009-07-17 01:31:16 +0000362 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000363
364 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000365}
Mike Stump6d9828c2009-07-17 01:31:16 +0000366
Ted Kremenek4f880632009-07-17 22:18:43 +0000367/// VisitChildren - Visit the children of a Stmt.
368CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
369 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000370 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000371 E = Terminator->child_end(); I != E; ++I) {
372 if (*I) B = Visit(*I);
373 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000374 return B;
375}
Ted Kremenek4f880632009-07-17 22:18:43 +0000376
377CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
378 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000379
Ted Kremenek4f880632009-07-17 22:18:43 +0000380 if (alwaysAdd) {
381 autoCreateBlock();
382 Block->appendStmt(A);
383 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000384
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000385 return Block;
386}
Ted Kremenek4f880632009-07-17 22:18:43 +0000387
388CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
389 if (B->isLogicalOp()) { // && or ||
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000390
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000391 // See if this is a known constant.
392 bool KnownTrue = false;
393 bool KnownFalse = false;
394 Expr::EvalResult Result;
395 if (B->getLHS()->Evaluate(Result, *Context)
396 && Result.Val.isInt()) {
397 if ((B->getOpcode() == BinaryOperator::LAnd)
398 == Result.Val.getInt().getBoolValue())
399 KnownTrue = true;
400 else
401 KnownFalse = true;
402 }
403
Ted Kremenek4f880632009-07-17 22:18:43 +0000404 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
405 ConfluenceBlock->appendStmt(B);
406
407 if (!FinishBlock(ConfluenceBlock))
408 return 0;
409
410 // create the block evaluating the LHS
411 CFGBlock* LHSBlock = createBlock(false);
412 LHSBlock->setTerminator(B);
413
414 // create the block evaluating the RHS
415 Succ = ConfluenceBlock;
416 Block = NULL;
417 CFGBlock* RHSBlock = addStmt(B->getRHS());
418 if (!FinishBlock(RHSBlock))
419 return 0;
420
421 // Now link the LHSBlock with RHSBlock.
422 if (B->getOpcode() == BinaryOperator::LOr) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000423 if (KnownTrue)
424 LHSBlock->addSuccessor(0);
425 else
426 LHSBlock->addSuccessor(ConfluenceBlock);
427 if (KnownFalse)
428 LHSBlock->addSuccessor(0);
429 else
430 LHSBlock->addSuccessor(RHSBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000431 } else {
432 assert (B->getOpcode() == BinaryOperator::LAnd);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000433 if (KnownFalse)
434 LHSBlock->addSuccessor(0);
435 else
436 LHSBlock->addSuccessor(RHSBlock);
437 if (KnownTrue)
438 LHSBlock->addSuccessor(0);
439 else
440 LHSBlock->addSuccessor(ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000441 }
442
443 // Generate the blocks for evaluating the LHS.
444 Block = LHSBlock;
445 return addStmt(B->getLHS());
446 }
447 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000448 autoCreateBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000449 Block->appendStmt(B);
450 addStmt(B->getRHS());
451 return addStmt(B->getLHS());
452 }
453
454 return VisitStmt(B, alwaysAdd);
455}
456
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000457CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E, bool alwaysAdd) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000458 // FIXME
459 return NYS();
460}
461
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000462CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
463 bool alwaysAdd) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000464 // FIXME
465 return NYS();
466}
467
468CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
469 // "break" is a control-flow statement. Thus we stop processing the current
470 // block.
471 if (Block && !FinishBlock(Block))
472 return 0;
473
474 // Now create a new block that ends with the break statement.
475 Block = createBlock(false);
476 Block->setTerminator(B);
477
478 // If there is no target for the break, then we are looking at an incomplete
479 // AST. This means that the CFG cannot be constructed.
480 if (BreakTargetBlock)
481 Block->addSuccessor(BreakTargetBlock);
482 else
483 badCFG = true;
484
485
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000486 return Block;
487}
Ted Kremenek4f880632009-07-17 22:18:43 +0000488
489CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
490 // If this is a call to a no-return function, this stops the block here.
491 if (FunctionDecl *FD = C->getDirectCallee()) {
492
493 if (!FD->hasAttr<NoReturnAttr>())
494 return VisitStmt(C, alwaysAdd);
495
496 if (Block && !FinishBlock(Block))
497 return 0;
498
499 // Create new block with no successor for the remaining pieces.
500 Block = createBlock(false);
501 Block->appendStmt(C);
502
503 // Wire this to the exit block directly.
504 Block->addSuccessor(&cfg->getExit());
505
506 return VisitChildren(C);
507 }
508
509 return VisitStmt(C, alwaysAdd);
510}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000511
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000512CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
Mike Stump22cd6582009-07-21 01:46:17 +0000513 // See if this is a known constant.
514 bool KnownTrue = false;
515 bool KnownFalse = false;
516 Expr::EvalResult Result;
517 if (C->getCond()->Evaluate(Result, *Context)
518 && Result.Val.isInt()) {
519 if (Result.Val.getInt().getBoolValue())
520 KnownTrue = true;
521 else
522 KnownFalse = true;
523 }
524
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000525 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
526 ConfluenceBlock->appendStmt(C);
527 if (!FinishBlock(ConfluenceBlock))
528 return 0;
529
530 Succ = ConfluenceBlock;
531 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000532 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000533 if (!FinishBlock(LHSBlock))
534 return 0;
535
536 Succ = ConfluenceBlock;
537 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000538 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000539 if (!FinishBlock(RHSBlock))
540 return 0;
541
542 Block = createBlock(false);
Mike Stump22cd6582009-07-21 01:46:17 +0000543 if (KnownFalse)
544 Block->addSuccessor(0);
545 else
546 Block->addSuccessor(LHSBlock);
547 if (KnownTrue)
548 Block->addSuccessor(0);
549 else
550 Block->addSuccessor(RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000551 Block->setTerminator(C);
552 return addStmt(C->getCond());
553}
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000554
Ted Kremenek4f880632009-07-17 22:18:43 +0000555
556CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
557 CFGBlock* LastBlock = Block;
558
559 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
560 I != E; ++I ) {
561 LastBlock = addStmt(*I);
562 }
563 return LastBlock;
564}
565
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000566CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000567 // See if this is a known constant.
568 bool KnownTrue = false;
569 bool KnownFalse = false;
570 Expr::EvalResult Result;
571 if (C->getCond()->Evaluate(Result, *Context)
572 && Result.Val.isInt()) {
573 if (Result.Val.getInt().getBoolValue())
574 KnownTrue = true;
575 else
576 KnownFalse = true;
577 }
578
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000579 // Create the confluence block that will "merge" the results of the ternary
580 // expression.
581 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
582 ConfluenceBlock->appendStmt(C);
583 if (!FinishBlock(ConfluenceBlock))
584 return 0;
585
586 // Create a block for the LHS expression if there is an LHS expression. A
587 // GCC extension allows LHS to be NULL, causing the condition to be the
588 // value that is returned instead.
589 // e.g: x ?: y is shorthand for: x ? x : y;
590 Succ = ConfluenceBlock;
591 Block = NULL;
592 CFGBlock* LHSBlock = NULL;
593 if (C->getLHS()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000594 LHSBlock = addStmt(C->getLHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000595 if (!FinishBlock(LHSBlock))
596 return 0;
597 Block = NULL;
598 }
599
600 // Create the block for the RHS expression.
601 Succ = ConfluenceBlock;
Ted Kremenek4f880632009-07-17 22:18:43 +0000602 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000603 if (!FinishBlock(RHSBlock))
604 return 0;
605
606 // Create the block that will contain the condition.
607 Block = createBlock(false);
608
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000609 if (LHSBlock) {
610 if (KnownFalse)
611 Block->addSuccessor(0);
612 else
613 Block->addSuccessor(LHSBlock);
614 } else {
615 if (KnownFalse) {
616 // If we know the condition is false, add NULL as the successor for
617 // the block containing the condition. In this case, the confluence
618 // block will have just one predecessor.
619 Block->addSuccessor(0);
620 assert (ConfluenceBlock->pred_size() == 1);
621 } else {
622 // If we have no LHS expression, add the ConfluenceBlock as a direct
623 // successor for the block containing the condition. Moreover, we need to
624 // reverse the order of the predecessors in the ConfluenceBlock because
625 // the RHSBlock will have been added to the succcessors already, and we
626 // want the first predecessor to the the block containing the expression
627 // for the case when the ternary expression evaluates to true.
628 Block->addSuccessor(ConfluenceBlock);
629 assert (ConfluenceBlock->pred_size() == 2);
630 std::reverse(ConfluenceBlock->pred_begin(),
631 ConfluenceBlock->pred_end());
632 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000633 }
634
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000635 if (KnownTrue)
636 Block->addSuccessor(0);
637 else
638 Block->addSuccessor(RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000639
640 Block->setTerminator(C);
641 return addStmt(C->getCond());
642}
643
Ted Kremenek4f880632009-07-17 22:18:43 +0000644CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
645 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000646
Ted Kremenek4f880632009-07-17 22:18:43 +0000647 if (DS->isSingleDecl()) {
648 Block->appendStmt(DS);
649 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000650 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000651
652 CFGBlock *B = 0;
653
654 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
655 typedef llvm::SmallVector<Decl*,10> BufTy;
656 BufTy Buf(DS->decl_begin(), DS->decl_end());
657
658 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
659 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
660 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
661 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
662
663 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
664 // automatically freed with the CFG.
665 DeclGroupRef DG(*I);
666 Decl *D = *I;
667 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
668 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
669
670 // Append the fake DeclStmt to block.
671 Block->appendStmt(DSNew);
672 B = VisitDeclSubExpr(D);
673 }
674
675 return B;
676}
677
678/// VisitDeclSubExpr - Utility method to add block-level expressions for
679/// initializers in Decls.
680CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
681 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000682
Ted Kremenek4f880632009-07-17 22:18:43 +0000683 VarDecl *VD = dyn_cast<VarDecl>(D);
684
685 if (!VD)
686 return Block;
687
688 Expr *Init = VD->getInit();
689
690 if (Init) {
691 // Optimization: Don't create separate block-level statements for literals.
692 switch (Init->getStmtClass()) {
693 case Stmt::IntegerLiteralClass:
694 case Stmt::CharacterLiteralClass:
695 case Stmt::StringLiteralClass:
696 break;
697 default:
698 Block = addStmt(Init);
699 }
700 }
701
702 // If the type of VD is a VLA, then we must process its size expressions.
703 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
704 VA = FindVA(VA->getElementType().getTypePtr()))
705 Block = addStmt(VA->getSizeExpr());
706
707 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000708}
709
710CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump5f203632009-07-21 00:38:52 +0000711 // See if this is a known constant.
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000712 bool KnownTrue = false;
713 bool KnownFalse = false;
714 Expr::EvalResult Result;
715 if (I->getCond()->Evaluate(Result, *Context)
716 && Result.Val.isInt()) {
717 if (Result.Val.getInt().getBoolValue())
718 KnownTrue = true;
719 else
720 KnownFalse = true;
721 }
722
Mike Stump6d9828c2009-07-17 01:31:16 +0000723 // We may see an if statement in the middle of a basic block, or it may be the
724 // first statement we are processing. In either case, we create a new basic
725 // block. First, we create the blocks for the then...else statements, and
726 // then we create the block containing the if statement. If we were in the
727 // middle of a block, we stop processing that block and reverse its
728 // statements. That block is then the implicit successor for the "then" and
729 // "else" clauses.
730
731 // The block we were proccessing is now finished. Make it the successor
732 // block.
733 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000734 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000735 if (!FinishBlock(Block))
736 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000737 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000738
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000739 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000740 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000741
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000742 if (Stmt* Else = I->getElse()) {
743 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000744
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000745 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000746 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000747 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000748 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000749
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000750 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
751 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000752 else if (Block) {
753 if (!FinishBlock(ElseBlock))
754 return 0;
755 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000756 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000757
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000758 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000759 CFGBlock* ThenBlock;
760 {
761 Stmt* Then = I->getThen();
762 assert (Then);
763 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000764 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000765 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000766
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000767 if (!ThenBlock) {
768 // We can reach here if the "then" body has all NullStmts.
769 // Create an empty block so we can distinguish between true and false
770 // branches in path-sensitive analyses.
771 ThenBlock = createBlock(false);
772 ThenBlock->addSuccessor(sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000773 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000774 if (!FinishBlock(ThenBlock))
775 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000776 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000777 }
778
Mike Stump6d9828c2009-07-17 01:31:16 +0000779 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000780 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000781
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000782 // Set the terminator of the new block to the If statement.
783 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000784
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000785 // Now add the successors.
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000786 if (KnownFalse)
787 Block->addSuccessor(0);
788 else
789 Block->addSuccessor(ThenBlock);
790 if (KnownTrue)
791 Block->addSuccessor(0);
792 else
793 Block->addSuccessor(ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000794
795 // Add the condition as the last statement in the new block. This may create
796 // new blocks as the condition may contain control-flow. Any newly created
797 // blocks will be pointed to be "Block".
Ted Kremenek4f880632009-07-17 22:18:43 +0000798 return addStmt(I->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000799}
Mike Stump6d9828c2009-07-17 01:31:16 +0000800
801
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000802CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000803 // If we were in the middle of a block we stop processing that block and
804 // reverse its statements.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000805 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000806 // NOTE: If a "return" appears in the middle of a block, this means that the
807 // code afterwards is DEAD (unreachable). We still keep a basic block
808 // for that code; a simple "mark-and-sweep" from the entry block will be
809 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000810 if (Block) FinishBlock(Block);
811
812 // Create the new block.
813 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000814
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000815 // The Exit block is the only successor.
816 Block->addSuccessor(&cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000817
818 // Add the return statement to the block. This may create new blocks if R
819 // contains control-flow (short-circuit operations).
Ted Kremenek4f880632009-07-17 22:18:43 +0000820 return VisitStmt(R, true);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000821}
822
823CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
824 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000825 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000826 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000827
Ted Kremenek4f880632009-07-17 22:18:43 +0000828 if (!LabelBlock) // This can happen when the body is empty, i.e.
829 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000830
Ted Kremenek4f880632009-07-17 22:18:43 +0000831 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000832 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000833
834 // Labels partition blocks, so this is the end of the basic block we were
835 // processing (L is the block's label). Because this is label (and we have
836 // already processed the substatement) there is no extra control-flow to worry
837 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000838 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000839 if (!FinishBlock(LabelBlock))
840 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000841
842 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000843 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000844
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000845 // This block is now the implicit successor of other blocks.
846 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000847
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000848 return LabelBlock;
849}
850
851CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000852 // Goto is a control-flow statement. Thus we stop processing the current
853 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000854 if (Block)
855 FinishBlock(Block);
856
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000857 Block = createBlock(false);
858 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000859
860 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000861 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000862
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000863 if (I == LabelMap.end())
864 // We will need to backpatch this block later.
865 BackpatchBlocks.push_back(Block);
866 else
867 Block->addSuccessor(I->second);
868
Mike Stump6d9828c2009-07-17 01:31:16 +0000869 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000870}
871
872CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Mike Stumpfefb9f72009-07-21 01:12:51 +0000873 // See if this is a known constant.
874 bool KnownTrue = false;
875 bool KnownFalse = false;
876 Expr::EvalResult Result;
877 if (F->getCond() && F->getCond()->Evaluate(Result, *Context)
878 && Result.Val.isInt()) {
879 if (Result.Val.getInt().getBoolValue())
880 KnownTrue = true;
881 else
882 KnownFalse = true;
883 }
884 if (F->getCond() == 0)
885 KnownTrue = true;
886
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000887 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000888
Mike Stumpfefb9f72009-07-21 01:12:51 +0000889 // "for" is a control-flow statement. Thus we stop processing the current
890 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000891 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000892 if (!FinishBlock(Block))
893 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000894 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000895 } else
896 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000897
898 // Because of short-circuit evaluation, the condition of the loop can span
899 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
900 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000901 CFGBlock* ExitConditionBlock = createBlock(false);
902 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000903
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000904 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000905 ExitConditionBlock->setTerminator(F);
906
907 // Now add the actual condition to the condition block. Because the condition
908 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000909 if (Stmt* C = F->getCond()) {
910 Block = ExitConditionBlock;
911 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000912 if (Block) {
913 if (!FinishBlock(EntryConditionBlock))
914 return 0;
915 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000916 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000917
Mike Stump6d9828c2009-07-17 01:31:16 +0000918 // The condition block is the implicit successor for the loop body as well as
919 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000920 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000921
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000922 // Now create the loop body.
923 {
924 assert (F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000925
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000926 // Save the current values for Block, Succ, and continue and break targets
927 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
928 save_continue(ContinueTargetBlock),
Mike Stump6d9828c2009-07-17 01:31:16 +0000929 save_break(BreakTargetBlock);
930
Ted Kremenekaf603f72007-08-30 18:39:40 +0000931 // Create a new block to contain the (bottom) of the loop body.
932 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000933
Ted Kremeneke9334502008-09-04 21:48:47 +0000934 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000935 // Generate increment code in its own basic block. This is the target of
936 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +0000937 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000938 } else {
939 // No increment code. Create a special, empty, block that is used as the
940 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +0000941 assert(Succ == EntryConditionBlock);
942 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +0000943 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000944
Ted Kremenek3575f842009-04-28 00:51:56 +0000945 // Finish up the increment (or empty) block if it hasn't been already.
946 if (Block) {
947 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000948 if (!FinishBlock(Block))
949 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +0000950 Block = 0;
951 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000952
Ted Kremenek3575f842009-04-28 00:51:56 +0000953 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000954
Ted Kremenek3575f842009-04-28 00:51:56 +0000955 // The starting block for the loop increment is the block that should
956 // represent the 'loop target' for looping back to the start of the loop.
957 ContinueTargetBlock->setLoopTarget(F);
958
Ted Kremeneke9334502008-09-04 21:48:47 +0000959 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +0000960 BreakTargetBlock = LoopSuccessor;
961
962 // Now populate the body block, and in the process create new blocks as we
963 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +0000964 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000965
966 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000967 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000968 else if (Block) {
969 if (!FinishBlock(BodyBlock))
970 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000971 }
972
Mike Stumpfefb9f72009-07-21 01:12:51 +0000973 if (KnownFalse)
974 ExitConditionBlock->addSuccessor(0);
975 else {
976 // This new body block is a successor to our "exit" condition block.
977 ExitConditionBlock->addSuccessor(BodyBlock);
978 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000979 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000980
Mike Stumpfefb9f72009-07-21 01:12:51 +0000981 if (KnownTrue)
982 ExitConditionBlock->addSuccessor(0);
983 else {
984 // Link up the condition block with the code that follows the loop. (the
985 // false branch).
986 ExitConditionBlock->addSuccessor(LoopSuccessor);
987 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000988
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000989 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +0000990 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000991 if (Stmt* I = F->getInit()) {
992 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000993 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000994 } else {
995 // There is no loop initialization. We are thus basically a while loop.
996 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000997 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +0000998 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000999 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001000 }
1001}
1002
Ted Kremenek514de5a2008-11-11 17:10:00 +00001003CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1004 // Objective-C fast enumeration 'for' statements:
1005 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1006 //
1007 // for ( Type newVariable in collection_expression ) { statements }
1008 //
1009 // becomes:
1010 //
1011 // prologue:
1012 // 1. collection_expression
1013 // T. jump to loop_entry
1014 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001015 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001016 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1017 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1018 // TB:
1019 // statements
1020 // T. jump to loop_entry
1021 // FB:
1022 // what comes after
1023 //
1024 // and
1025 //
1026 // Type existingItem;
1027 // for ( existingItem in expression ) { statements }
1028 //
1029 // becomes:
1030 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001031 // the same with newVariable replaced with existingItem; the binding works
1032 // the same except that for one ObjCForCollectionStmt::getElement() returns
1033 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001034 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001035
Ted Kremenek514de5a2008-11-11 17:10:00 +00001036 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001037
Ted Kremenek514de5a2008-11-11 17:10:00 +00001038 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001039 if (!FinishBlock(Block))
1040 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001041 LoopSuccessor = Block;
1042 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001043 } else
1044 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001045
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001046 // Build the condition blocks.
1047 CFGBlock* ExitConditionBlock = createBlock(false);
1048 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001049
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001050 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001051 ExitConditionBlock->setTerminator(S);
1052
1053 // The last statement in the block should be the ObjCForCollectionStmt, which
1054 // performs the actual binding to 'element' and determines if there are any
1055 // more items in the collection.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001056 ExitConditionBlock->appendStmt(S);
1057 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001058
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001059 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001060 // generate new blocks as necesary. We DON'T add the statement by default to
1061 // the CFG unless it contains control-flow.
Ted Kremenek4f880632009-07-17 22:18:43 +00001062 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001063 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001064 if (!FinishBlock(EntryConditionBlock))
1065 return 0;
1066 Block = 0;
1067 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001068
1069 // The condition block is the implicit successor for the loop body as well as
1070 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001071 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001072
Ted Kremenek514de5a2008-11-11 17:10:00 +00001073 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001074 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001075 // Save the current values for Succ, continue and break targets.
1076 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001077 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1078
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001079 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001080 ContinueTargetBlock = EntryConditionBlock;
1081
Ted Kremenek4f880632009-07-17 22:18:43 +00001082 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001083
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001084 if (!BodyBlock)
1085 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001086 else if (Block) {
1087 if (!FinishBlock(BodyBlock))
1088 return 0;
1089 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001090
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001091 // This new body block is a successor to our "exit" condition block.
1092 ExitConditionBlock->addSuccessor(BodyBlock);
1093 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001094
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001095 // Link up the condition block with the code that follows the loop.
1096 // (the false branch).
1097 ExitConditionBlock->addSuccessor(LoopSuccessor);
1098
Ted Kremenek514de5a2008-11-11 17:10:00 +00001099 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001100 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001101 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001102}
1103
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001104CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1105 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001106
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001107 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001108 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001109
Ted Kremenekda5348e2009-05-05 23:11:51 +00001110 // The sync body starts its own basic block. This makes it a little easier
1111 // for diagnostic clients.
1112 if (SyncBlock) {
1113 if (!FinishBlock(SyncBlock))
1114 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001115
Ted Kremenekda5348e2009-05-05 23:11:51 +00001116 Block = 0;
1117 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001118
Ted Kremenekda5348e2009-05-05 23:11:51 +00001119 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001120
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001121 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001122 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001123}
Mike Stump6d9828c2009-07-17 01:31:16 +00001124
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001125CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001126 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001127 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001128}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001129
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001130CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Mike Stump5f203632009-07-21 00:38:52 +00001131 // See if this is a known constant.
1132 bool KnownTrue = false;
1133 bool KnownFalse = false;
1134 Expr::EvalResult Result;
1135 if (W->getCond()->Evaluate(Result, *Context)
1136 && Result.Val.isInt()) {
1137 if (Result.Val.getInt().getBoolValue())
1138 KnownTrue = true;
1139 else
1140 KnownFalse = true;
1141 }
1142
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001143 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001144
Mike Stumpfefb9f72009-07-21 01:12:51 +00001145 // "while" is a control-flow statement. Thus we stop processing the current
1146 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001147 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001148 if (!FinishBlock(Block))
1149 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001150 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001151 } else
1152 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001153
1154 // Because of short-circuit evaluation, the condition of the loop can span
1155 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1156 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001157 CFGBlock* ExitConditionBlock = createBlock(false);
1158 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001159
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001160 // Set the terminator for the "exit" condition block.
1161 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001162
1163 // Now add the actual condition to the condition block. Because the condition
1164 // itself may contain control-flow, new blocks may be created. Thus we update
1165 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001166 if (Stmt* C = W->getCond()) {
1167 Block = ExitConditionBlock;
1168 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001169 assert(Block == EntryConditionBlock);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001170 if (Block) {
1171 if (!FinishBlock(EntryConditionBlock))
1172 return 0;
1173 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001174 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001175
1176 // The condition block is the implicit successor for the loop body as well as
1177 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001178 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001179
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001180 // Process the loop body.
1181 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001182 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001183
1184 // Save the current values for Block, Succ, and continue and break targets
1185 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1186 save_continue(ContinueTargetBlock),
1187 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001188
Mike Stump6d9828c2009-07-17 01:31:16 +00001189 // Create an empty block to represent the transition block for looping back
1190 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001191 Block = 0;
1192 assert(Succ == EntryConditionBlock);
1193 Succ = createBlock();
1194 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001195 ContinueTargetBlock = Succ;
1196
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001197 // All breaks should go to the code following the loop.
1198 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001199
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001200 // NULL out Block to force lazy instantiation of blocks for the body.
1201 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001202
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001203 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001204 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001205
Ted Kremenekaf603f72007-08-30 18:39:40 +00001206 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001207 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001208 else if (Block) {
1209 if (!FinishBlock(BodyBlock))
1210 return 0;
1211 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001212
Mike Stump5f203632009-07-21 00:38:52 +00001213 if (KnownFalse)
1214 ExitConditionBlock->addSuccessor(0);
1215 else {
1216 // Add the loop body entry as a successor to the condition.
1217 ExitConditionBlock->addSuccessor(BodyBlock);
1218 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001219 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001220
Mike Stump5f203632009-07-21 00:38:52 +00001221 if (KnownTrue)
1222 ExitConditionBlock->addSuccessor(0);
1223 else {
1224 // Link up the condition block with the code that follows the loop. (the
1225 // false branch).
1226 ExitConditionBlock->addSuccessor(LoopSuccessor);
1227 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001228
1229 // There can be no more statements in the condition block since we loop back
1230 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001231 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001232
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001233 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001234 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001235 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001236}
Ted Kremenek4f880632009-07-17 22:18:43 +00001237
1238
1239CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1240 // FIXME: For now we pretend that @catch and the code it contains does not
1241 // exit.
1242 return Block;
1243}
Mike Stump6d9828c2009-07-17 01:31:16 +00001244
Ted Kremenek2fda5042008-12-09 20:20:09 +00001245CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1246 // FIXME: This isn't complete. We basically treat @throw like a return
1247 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001248
1249 // If we were in the middle of a block we stop processing that block and
1250 // reverse its statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001251 if (Block && !FinishBlock(Block))
1252 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001253
Ted Kremenek2fda5042008-12-09 20:20:09 +00001254 // Create the new block.
1255 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001256
Ted Kremenek2fda5042008-12-09 20:20:09 +00001257 // The Exit block is the only successor.
1258 Block->addSuccessor(&cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001259
1260 // Add the statement to the block. This may create new blocks if S contains
1261 // control-flow (short-circuit operations).
Ted Kremenek4f880632009-07-17 22:18:43 +00001262 return VisitStmt(S, true);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001263}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001264
Ted Kremenek4f880632009-07-17 22:18:43 +00001265CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Mike Stump8f9893a2009-07-21 01:27:50 +00001266 // See if this is a known constant.
1267 bool KnownTrue = false;
1268 bool KnownFalse = false;
1269 Expr::EvalResult Result;
1270 if (D->getCond()->Evaluate(Result, *Context)
1271 && Result.Val.isInt()) {
1272 if (Result.Val.getInt().getBoolValue())
1273 KnownTrue = true;
1274 else
1275 KnownFalse = true;
1276 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001277
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001278 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001279
Mike Stump8f9893a2009-07-21 01:27:50 +00001280 // "do...while" is a control-flow statement. Thus we stop processing the
1281 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001282 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001283 if (!FinishBlock(Block))
1284 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001285 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001286 } else
1287 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001288
1289 // Because of short-circuit evaluation, the condition of the loop can span
1290 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1291 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001292 CFGBlock* ExitConditionBlock = createBlock(false);
1293 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001294
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001295 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001296 ExitConditionBlock->setTerminator(D);
1297
1298 // Now add the actual condition to the condition block. Because the condition
1299 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001300 if (Stmt* C = D->getCond()) {
1301 Block = ExitConditionBlock;
1302 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001303 if (Block) {
1304 if (!FinishBlock(EntryConditionBlock))
1305 return 0;
1306 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001307 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001308
Ted Kremenek54827132008-02-27 07:20:00 +00001309 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001310 Succ = EntryConditionBlock;
1311
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001312 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001313 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001314 {
1315 assert (D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001316
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001317 // Save the current values for Block, Succ, and continue and break targets
1318 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1319 save_continue(ContinueTargetBlock),
1320 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001321
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001322 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001323 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001324
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001325 // All breaks should go to the code following the loop.
1326 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001327
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001328 // NULL out Block to force lazy instantiation of blocks for the body.
1329 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001330
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001331 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001332 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001333
Ted Kremenekaf603f72007-08-30 18:39:40 +00001334 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001335 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001336 else if (Block) {
1337 if (!FinishBlock(BodyBlock))
1338 return 0;
1339 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001340
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001341 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001342 // ExitConditionBlock to represent the "loop back" transition. Create an
1343 // empty block to represent the transition block for looping back to the
1344 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001345 // FIXME: Can we do this more efficiently without adding another block?
1346 Block = NULL;
1347 Succ = BodyBlock;
1348 CFGBlock *LoopBackBlock = createBlock();
1349 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001350
Mike Stump8f9893a2009-07-21 01:27:50 +00001351 if (KnownFalse)
1352 ExitConditionBlock->addSuccessor(0);
1353 else {
1354 // Add the loop body entry as a successor to the condition.
1355 ExitConditionBlock->addSuccessor(LoopBackBlock);
1356 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001357 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001358
Mike Stump8f9893a2009-07-21 01:27:50 +00001359 if (KnownTrue)
1360 ExitConditionBlock->addSuccessor(0);
1361 else {
1362 // Link up the condition block with the code that follows the loop. (the
1363 // false branch).
1364 ExitConditionBlock->addSuccessor(LoopSuccessor);
1365 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001366
1367 // There can be no more statements in the body block(s) since we loop back to
1368 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001369 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001370
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001371 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001372 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001373 return BodyBlock;
1374}
1375
1376CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1377 // "continue" is a control-flow statement. Thus we stop processing the
1378 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001379 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001380 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001381
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001382 // Now create a new block that ends with the continue statement.
1383 Block = createBlock(false);
1384 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001385
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001386 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001387 // incomplete AST. This means the CFG cannot be constructed.
1388 if (ContinueTargetBlock)
1389 Block->addSuccessor(ContinueTargetBlock);
1390 else
1391 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001392
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001393 return Block;
1394}
Ted Kremenek4f880632009-07-17 22:18:43 +00001395
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001396CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1397 bool alwaysAdd) {
1398
1399 if (alwaysAdd) {
1400 autoCreateBlock();
1401 Block->appendStmt(E);
1402 }
1403
Ted Kremenek4f880632009-07-17 22:18:43 +00001404 // VLA types have expressions that must be evaluated.
1405 if (E->isArgumentType()) {
1406 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1407 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1408 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001409 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001410
Mike Stump6d9828c2009-07-17 01:31:16 +00001411 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001412}
Ted Kremenek4f880632009-07-17 22:18:43 +00001413
1414/// VisitStmtExpr - Utility method to handle (nested) statement
1415/// expressions (a GCC extension).
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001416CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1417 if (alwaysAdd) {
1418 autoCreateBlock();
1419 Block->appendStmt(SE);
1420 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001421 return VisitCompoundStmt(SE->getSubStmt());
1422}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001423
Ted Kremenek411cdee2008-04-16 21:10:48 +00001424CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001425 // "switch" is a control-flow statement. Thus we stop processing the current
1426 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001427 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001428
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001429 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001430 if (!FinishBlock(Block))
1431 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001432 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001433 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001434
1435 // Save the current "switch" context.
1436 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001437 save_break(BreakTargetBlock),
1438 save_default(DefaultCaseBlock);
1439
Mike Stump6d9828c2009-07-17 01:31:16 +00001440 // Set the "default" case to be the block after the switch statement. If the
1441 // switch statement contains a "default:", this value will be overwritten with
1442 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001443 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001444
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001445 // Create a new block that will contain the switch statement.
1446 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001447
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001448 // Now process the switch body. The code after the switch is the implicit
1449 // successor.
1450 Succ = SwitchSuccessor;
1451 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001452
1453 // When visiting the body, the case statements should automatically get linked
1454 // up to the switch. We also don't keep a pointer to the body, since all
1455 // control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001456 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001457 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001458 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001459 if (Block) {
1460 if (!FinishBlock(BodyBlock))
1461 return 0;
1462 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001463
Mike Stump6d9828c2009-07-17 01:31:16 +00001464 // If we have no "default:" case, the default transition is to the code
1465 // following the switch body.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001466 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001467
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001468 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001469 SwitchTerminatedBlock->setTerminator(Terminator);
1470 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001471 Block = SwitchTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001472
Ted Kremenek411cdee2008-04-16 21:10:48 +00001473 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001474}
1475
Ted Kremenek4f880632009-07-17 22:18:43 +00001476CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001477 // CaseStmts are essentially labels, so they are the first statement in a
1478 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001479
Ted Kremenek4f880632009-07-17 22:18:43 +00001480 if (CS->getSubStmt())
1481 addStmt(CS->getSubStmt());
1482
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001483 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001484 if (!CaseBlock)
1485 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001486
1487 // Cases statements partition blocks, so this is the top of the basic block we
1488 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001489 CaseBlock->setLabel(CS);
1490
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001491 if (!FinishBlock(CaseBlock))
1492 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001493
1494 // Add this block to the list of successors for the block with the switch
1495 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001496 assert(SwitchTerminatedBlock);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001497 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001498
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001499 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1500 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001501
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001502 // This block is now the implicit successor of other blocks.
1503 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001504
Ted Kremenek2677ea82008-03-15 07:45:02 +00001505 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001506}
Mike Stump6d9828c2009-07-17 01:31:16 +00001507
Ted Kremenek411cdee2008-04-16 21:10:48 +00001508CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001509 if (Terminator->getSubStmt())
1510 addStmt(Terminator->getSubStmt());
1511
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001512 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001513
1514 if (!DefaultCaseBlock)
1515 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001516
1517 // Default statements partition blocks, so this is the top of the basic block
1518 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001519 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek4f880632009-07-17 22:18:43 +00001520
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001521 if (!FinishBlock(DefaultCaseBlock))
1522 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001523
Mike Stump6d9828c2009-07-17 01:31:16 +00001524 // Unlike case statements, we don't add the default block to the successors
1525 // for the switch statement immediately. This is done when we finish
1526 // processing the switch statement. This allows for the default case
1527 // (including a fall-through to the code after the switch statement) to always
1528 // be the last successor of a switch-terminated block.
1529
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001530 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1531 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001532
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001533 // This block is now the implicit successor of other blocks.
1534 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001535
1536 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001537}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001538
Ted Kremenek19bb3562007-08-28 19:26:49 +00001539CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001540 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001541 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001542
Ted Kremenek19bb3562007-08-28 19:26:49 +00001543 if (!IBlock) {
1544 IBlock = createBlock(false);
1545 cfg->setIndirectGotoBlock(IBlock);
1546 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001547
Ted Kremenek19bb3562007-08-28 19:26:49 +00001548 // IndirectGoto is a control-flow statement. Thus we stop processing the
1549 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001550 if (Block && !FinishBlock(Block))
1551 return 0;
1552
Ted Kremenek19bb3562007-08-28 19:26:49 +00001553 Block = createBlock(false);
1554 Block->setTerminator(I);
1555 Block->addSuccessor(IBlock);
1556 return addStmt(I->getTarget());
1557}
1558
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001559} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001560
Mike Stump6d9828c2009-07-17 01:31:16 +00001561/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1562/// no successors or predecessors. If this is the first block created in the
1563/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001564CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001565 bool first_block = begin() == end();
1566
1567 // Create the block.
Ted Kremenek94382522007-09-05 20:02:05 +00001568 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek026473c2007-08-23 16:51:22 +00001569
1570 // If this is the first block, set it as the Entry and Exit.
1571 if (first_block) Entry = Exit = &front();
1572
1573 // Return the block.
1574 return &front();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001575}
1576
Ted Kremenek026473c2007-08-23 16:51:22 +00001577/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1578/// CFG is returned to the caller.
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001579CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001580 CFGBuilder Builder;
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001581 return Builder.buildCFG(Statement, C);
Ted Kremenek026473c2007-08-23 16:51:22 +00001582}
1583
1584/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001585void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1586
Ted Kremenek63f58872007-10-01 19:33:33 +00001587//===----------------------------------------------------------------------===//
1588// CFG: Queries for BlkExprs.
1589//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001590
Ted Kremenek63f58872007-10-01 19:33:33 +00001591namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001592 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001593}
1594
Ted Kremenek411cdee2008-04-16 21:10:48 +00001595static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1596 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001597 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001598
Ted Kremenek411cdee2008-04-16 21:10:48 +00001599 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001600 if (!*I) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001601
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001602 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1603 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001604
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001605 FindSubExprAssignments(*I, Set);
1606 }
1607}
1608
Ted Kremenek63f58872007-10-01 19:33:33 +00001609static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1610 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001611
1612 // Look for assignments that are used as subexpressions. These are the only
1613 // assignments that we want to *possibly* register as a block-level
1614 // expression. Basically, if an assignment occurs both in a subexpression and
1615 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001616 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001617
Ted Kremenek63f58872007-10-01 19:33:33 +00001618 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1619 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001620 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001621
Ted Kremenek411cdee2008-04-16 21:10:48 +00001622 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001623
1624 // Iterate over the statements again on identify the Expr* and Stmt* at the
1625 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001626
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001627 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001628 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001629
Ted Kremenek411cdee2008-04-16 21:10:48 +00001630 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001631 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001632 // expression are really "statements" whose value is never used by
1633 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001634 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001635 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001636 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1637 // Special handling for statement expressions. The last statement in
1638 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001639 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001640 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001641 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001642 (*M)[C->body_back()] = x;
1643 }
1644 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001645
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001646 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001647 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001648 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001649
Ted Kremenek411cdee2008-04-16 21:10:48 +00001650 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001651
Ted Kremenek390e48b2008-11-12 21:11:49 +00001652 Stmt* S = I->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001653
Ted Kremenek390e48b2008-11-12 21:11:49 +00001654 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001655 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001656 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001657 }
1658 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001659
Ted Kremenek63f58872007-10-01 19:33:33 +00001660 return M;
1661}
1662
Ted Kremenek86946742008-01-17 20:48:37 +00001663CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1664 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001665 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001666
Ted Kremenek63f58872007-10-01 19:33:33 +00001667 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001668 BlkExprMapTy::iterator I = M->find(S);
Mike Stump6d9828c2009-07-17 01:31:16 +00001669
Ted Kremenek63f58872007-10-01 19:33:33 +00001670 if (I == M->end()) return CFG::BlkExprNumTy();
1671 else return CFG::BlkExprNumTy(I->second);
1672}
1673
1674unsigned CFG::getNumBlkExprs() {
1675 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1676 return M->size();
1677 else {
1678 // We assume callers interested in the number of BlkExprs will want
1679 // the map constructed if it doesn't already exist.
1680 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1681 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1682 }
1683}
1684
Ted Kremenek274f4332008-04-28 18:00:46 +00001685//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001686// Cleanup: CFG dstor.
1687//===----------------------------------------------------------------------===//
1688
Ted Kremenek63f58872007-10-01 19:33:33 +00001689CFG::~CFG() {
1690 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1691}
Mike Stump6d9828c2009-07-17 01:31:16 +00001692
Ted Kremenek7dba8602007-08-29 21:56:09 +00001693//===----------------------------------------------------------------------===//
1694// CFG pretty printing
1695//===----------------------------------------------------------------------===//
1696
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001697namespace {
1698
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001699class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump6d9828c2009-07-17 01:31:16 +00001700
Ted Kremenek42a509f2007-08-31 21:30:12 +00001701 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1702 StmtMapTy StmtMap;
1703 signed CurrentBlock;
1704 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001705 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001706public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001707
Chris Lattnere4f21422009-06-30 01:26:17 +00001708 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1709 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001710 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1711 unsigned j = 1;
1712 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1713 BI != BEnd; ++BI, ++j )
1714 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1715 }
1716 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001717
Ted Kremenek42a509f2007-08-31 21:30:12 +00001718 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001719
Chris Lattnere4f21422009-06-30 01:26:17 +00001720 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001721 void setBlockID(signed i) { CurrentBlock = i; }
1722 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001723
Ted Kremeneka95d3752008-09-13 05:16:45 +00001724 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001725
Ted Kremenek411cdee2008-04-16 21:10:48 +00001726 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001727
1728 if (I == StmtMap.end())
1729 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001730
1731 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek42a509f2007-08-31 21:30:12 +00001732 && I->second.second == CurrentStmt)
1733 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001734
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001735 OS << "[B" << I->second.first << "." << I->second.second << "]";
1736 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001737 }
1738};
Chris Lattnere4f21422009-06-30 01:26:17 +00001739} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001740
Chris Lattnere4f21422009-06-30 01:26:17 +00001741
1742namespace {
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001743class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1744 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001745
Ted Kremeneka95d3752008-09-13 05:16:45 +00001746 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001747 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001748 PrintingPolicy Policy;
1749
Ted Kremenek42a509f2007-08-31 21:30:12 +00001750public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001751 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001752 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001753 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001754
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001755 void VisitIfStmt(IfStmt* I) {
1756 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001757 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001758 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001759
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001760 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001761 void VisitStmt(Stmt* Terminator) {
1762 Terminator->printPretty(OS, Helper, Policy);
1763 }
1764
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001765 void VisitForStmt(ForStmt* F) {
1766 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001767 if (F->getInit()) OS << "...";
1768 OS << "; ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001769 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001770 OS << "; ";
1771 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001772 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001773 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001774
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001775 void VisitWhileStmt(WhileStmt* W) {
1776 OS << "while " ;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001777 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001778 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001779
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001780 void VisitDoStmt(DoStmt* D) {
1781 OS << "do ... while ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001782 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001783 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001784
Ted Kremenek411cdee2008-04-16 21:10:48 +00001785 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001786 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001787 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001788 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001789
Ted Kremenek805e9a82007-08-31 21:49:40 +00001790 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001791 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001792 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001793 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001794
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001795 void VisitChooseExpr(ChooseExpr* C) {
1796 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001797 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001798 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001799 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001800
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001801 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1802 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001803 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001804 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001805
Ted Kremenek805e9a82007-08-31 21:49:40 +00001806 void VisitBinaryOperator(BinaryOperator* B) {
1807 if (!B->isLogicalOp()) {
1808 VisitExpr(B);
1809 return;
1810 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001811
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001812 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001813
Ted Kremenek805e9a82007-08-31 21:49:40 +00001814 switch (B->getOpcode()) {
1815 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001816 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001817 return;
1818 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001819 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001820 return;
1821 default:
1822 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001823 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00001824 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001825
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001826 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001827 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001828 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001829};
Chris Lattnere4f21422009-06-30 01:26:17 +00001830} // end anonymous namespace
1831
Mike Stump6d9828c2009-07-17 01:31:16 +00001832
Chris Lattnere4f21422009-06-30 01:26:17 +00001833static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1834 Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001835 if (Helper) {
1836 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001837 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001838 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00001839
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001840 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001841 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001842 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001843 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001844 return;
1845 }
1846 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001847
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001848 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001849 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001850 if (B->getOpcode() == BinaryOperator::Comma) {
1851 OS << "... , ";
1852 Helper->handledStmt(B->getRHS(),OS);
1853 OS << '\n';
1854 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001855 }
1856 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001857 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001858
Chris Lattnere4f21422009-06-30 01:26:17 +00001859 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00001860
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001861 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001862 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001863}
Mike Stump6d9828c2009-07-17 01:31:16 +00001864
Chris Lattnere4f21422009-06-30 01:26:17 +00001865static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1866 const CFGBlock& B,
1867 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001868
Ted Kremenek42a509f2007-08-31 21:30:12 +00001869 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00001870
Ted Kremenek7dba8602007-08-29 21:56:09 +00001871 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00001872 OS << "\n [ B" << B.getBlockID();
1873
Ted Kremenek42a509f2007-08-31 21:30:12 +00001874 if (&B == &cfg->getEntry())
1875 OS << " (ENTRY) ]\n";
1876 else if (&B == &cfg->getExit())
1877 OS << " (EXIT) ]\n";
1878 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001879 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001880 else
1881 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00001882
Ted Kremenek9cffe732007-08-29 23:20:49 +00001883 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001884 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001885
1886 if (print_edges)
1887 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001888
Ted Kremenek411cdee2008-04-16 21:10:48 +00001889 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001890 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001891 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001892 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001893 C->getLHS()->printPretty(OS, Helper,
1894 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001895 if (C->getRHS()) {
1896 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001897 C->getRHS()->printPretty(OS, Helper,
1898 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001899 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001900 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001901 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001902 else
1903 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001904
Ted Kremenek9cffe732007-08-29 23:20:49 +00001905 OS << ":\n";
1906 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001907
Ted Kremenekfddd5182007-08-21 21:42:03 +00001908 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001909 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00001910
Ted Kremenek42a509f2007-08-31 21:30:12 +00001911 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1912 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001913
Ted Kremenek9cffe732007-08-29 23:20:49 +00001914 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001915 if (print_edges)
1916 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001917
Ted Kremeneka95d3752008-09-13 05:16:45 +00001918 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001919
Ted Kremenek42a509f2007-08-31 21:30:12 +00001920 if (Helper)
1921 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00001922
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001923 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001924 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001925
Ted Kremenek9cffe732007-08-29 23:20:49 +00001926 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001927 if (B.getTerminator()) {
1928 if (print_edges)
1929 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001930
Ted Kremenek9cffe732007-08-29 23:20:49 +00001931 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001932
Ted Kremenek42a509f2007-08-31 21:30:12 +00001933 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00001934
Chris Lattnere4f21422009-06-30 01:26:17 +00001935 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1936 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00001937 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001938 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001939 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001940
Ted Kremenek9cffe732007-08-29 23:20:49 +00001941 if (print_edges) {
1942 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001943 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001944 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001945
Ted Kremenek42a509f2007-08-31 21:30:12 +00001946 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1947 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001948
Ted Kremenek42a509f2007-08-31 21:30:12 +00001949 if (i == 8 || (i-8) == 0)
1950 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001951
Ted Kremenek9cffe732007-08-29 23:20:49 +00001952 OS << " B" << (*I)->getBlockID();
1953 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001954
Ted Kremenek42a509f2007-08-31 21:30:12 +00001955 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00001956
Ted Kremenek42a509f2007-08-31 21:30:12 +00001957 // Print the successors of this block.
1958 OS << " Successors (" << B.succ_size() << "):";
1959 i = 0;
1960
1961 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1962 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001963
Ted Kremenek42a509f2007-08-31 21:30:12 +00001964 if (i == 8 || (i-8) % 10 == 0)
1965 OS << "\n ";
1966
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001967 if (*I)
1968 OS << " B" << (*I)->getBlockID();
1969 else
1970 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001971 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001972
Ted Kremenek9cffe732007-08-29 23:20:49 +00001973 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001974 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001975}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001976
Ted Kremenek42a509f2007-08-31 21:30:12 +00001977
1978/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001979void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001980
1981/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00001982void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1983 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00001984
Ted Kremenek42a509f2007-08-31 21:30:12 +00001985 // Print the entry block.
1986 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00001987
Ted Kremenek42a509f2007-08-31 21:30:12 +00001988 // Iterate through the CFGBlocks and print them one by one.
1989 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1990 // Skip the entry block, because we already printed it.
1991 if (&(*I) == &getEntry() || &(*I) == &getExit())
1992 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001993
Ted Kremenek42a509f2007-08-31 21:30:12 +00001994 print_block(OS, this, *I, &Helper, true);
1995 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001996
Ted Kremenek42a509f2007-08-31 21:30:12 +00001997 // Print the exit block.
1998 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00001999 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002000}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002001
2002/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002003void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2004 print(llvm::errs(), cfg, LO);
2005}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002006
2007/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2008/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002009void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2010 const LangOptions &LO) const {
2011 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002012 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002013}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002014
Ted Kremeneka2925852008-01-30 23:02:42 +00002015/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002016void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002017 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002018 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002019 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2020}
2021
Ted Kremenek390e48b2008-11-12 21:11:49 +00002022Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002023
Ted Kremenek411cdee2008-04-16 21:10:48 +00002024 if (!Terminator)
2025 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002026
Ted Kremenek411cdee2008-04-16 21:10:48 +00002027 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002028
Ted Kremenek411cdee2008-04-16 21:10:48 +00002029 switch (Terminator->getStmtClass()) {
2030 default:
2031 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002032
Ted Kremenek411cdee2008-04-16 21:10:48 +00002033 case Stmt::ForStmtClass:
2034 E = cast<ForStmt>(Terminator)->getCond();
2035 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002036
Ted Kremenek411cdee2008-04-16 21:10:48 +00002037 case Stmt::WhileStmtClass:
2038 E = cast<WhileStmt>(Terminator)->getCond();
2039 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002040
Ted Kremenek411cdee2008-04-16 21:10:48 +00002041 case Stmt::DoStmtClass:
2042 E = cast<DoStmt>(Terminator)->getCond();
2043 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002044
Ted Kremenek411cdee2008-04-16 21:10:48 +00002045 case Stmt::IfStmtClass:
2046 E = cast<IfStmt>(Terminator)->getCond();
2047 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002048
Ted Kremenek411cdee2008-04-16 21:10:48 +00002049 case Stmt::ChooseExprClass:
2050 E = cast<ChooseExpr>(Terminator)->getCond();
2051 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002052
Ted Kremenek411cdee2008-04-16 21:10:48 +00002053 case Stmt::IndirectGotoStmtClass:
2054 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2055 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002056
Ted Kremenek411cdee2008-04-16 21:10:48 +00002057 case Stmt::SwitchStmtClass:
2058 E = cast<SwitchStmt>(Terminator)->getCond();
2059 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002060
Ted Kremenek411cdee2008-04-16 21:10:48 +00002061 case Stmt::ConditionalOperatorClass:
2062 E = cast<ConditionalOperator>(Terminator)->getCond();
2063 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002064
Ted Kremenek411cdee2008-04-16 21:10:48 +00002065 case Stmt::BinaryOperatorClass: // '&&' and '||'
2066 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002067 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002068
Ted Kremenek390e48b2008-11-12 21:11:49 +00002069 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002070 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002071 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002072
Ted Kremenek411cdee2008-04-16 21:10:48 +00002073 return E ? E->IgnoreParens() : NULL;
2074}
2075
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002076bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002077
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002078 if (!Terminator)
2079 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002080
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002081 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002082
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002083 switch (Terminator->getStmtClass()) {
2084 default:
2085 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002086
2087 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002088 case Stmt::WhileStmtClass:
2089 case Stmt::DoStmtClass:
2090 case Stmt::IfStmtClass:
2091 case Stmt::ChooseExprClass:
2092 case Stmt::ConditionalOperatorClass:
2093 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002094 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002095 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002096
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002097 return E ? E->IgnoreParens() : NULL;
2098}
2099
Ted Kremeneka2925852008-01-30 23:02:42 +00002100
Ted Kremenek7dba8602007-08-29 21:56:09 +00002101//===----------------------------------------------------------------------===//
2102// CFG Graphviz Visualization
2103//===----------------------------------------------------------------------===//
2104
Ted Kremenek42a509f2007-08-31 21:30:12 +00002105
2106#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002107static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002108#endif
2109
Chris Lattnere4f21422009-06-30 01:26:17 +00002110void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002111#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002112 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002113 GraphHelper = &H;
2114 llvm::ViewGraph(this,"CFG");
2115 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002116#endif
2117}
2118
Ted Kremenek7dba8602007-08-29 21:56:09 +00002119namespace llvm {
2120template<>
2121struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson02995ce2009-06-24 17:37:55 +00002122 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
2123 bool ShortNames) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002124
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002125#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002126 std::string OutSStr;
2127 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002128 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002129 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002130
2131 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2132
2133 // Process string output to make it nicer...
2134 for (unsigned i = 0; i != OutStr.length(); ++i)
2135 if (OutStr[i] == '\n') { // Left justify
2136 OutStr[i] = '\\';
2137 OutStr.insert(OutStr.begin()+i+1, 'l');
2138 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002139
Ted Kremenek7dba8602007-08-29 21:56:09 +00002140 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002141#else
2142 return "";
2143#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002144 }
2145};
2146} // end namespace llvm