Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1 | //==- GRCoreEngine.cpp - Path-Sensitive Dataflow Engine ----------------*- C++ -*-// |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a generic engine for intraprocedural, path-sensitive, |
| 11 | // dataflow analysis via graph reachability engine. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/GRCoreEngine.h" |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
| 17 | #include "llvm/Support/Compiler.h" |
| 18 | #include "llvm/Support/Casting.h" |
| 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include <vector> |
| 21 | |
| 22 | using llvm::cast; |
| 23 | using llvm::isa; |
| 24 | using namespace clang; |
| 25 | |
| 26 | namespace { |
| 27 | class VISIBILITY_HIDDEN DFS : public GRWorkList { |
| 28 | llvm::SmallVector<GRWorkListUnit,20> Stack; |
| 29 | public: |
| 30 | virtual bool hasWork() const { |
| 31 | return !Stack.empty(); |
| 32 | } |
| 33 | |
| 34 | virtual void Enqueue(const GRWorkListUnit& U) { |
| 35 | Stack.push_back(U); |
| 36 | } |
| 37 | |
| 38 | virtual GRWorkListUnit Dequeue() { |
| 39 | assert (!Stack.empty()); |
| 40 | const GRWorkListUnit& U = Stack.back(); |
| 41 | Stack.pop_back(); // This technically "invalidates" U, but we are fine. |
| 42 | return U; |
| 43 | } |
| 44 | }; |
| 45 | } // end anonymous namespace |
| 46 | |
Ted Kremenek | d2500ab | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 47 | // Place the dstor for GRWorkList here because it contains virtual member |
| 48 | // functions, and we the code for the dstor generated in one compilation unit. |
| 49 | GRWorkList::~GRWorkList() {} |
| 50 | |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 51 | GRWorkList* GRWorkList::MakeDFS() { return new DFS(); } |
| 52 | |
| 53 | /// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps. |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 54 | bool GRCoreEngineImpl::ExecuteWorkList(unsigned Steps) { |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 55 | |
| 56 | if (G->num_roots() == 0) { // Initialize the analysis by constructing |
| 57 | // the root if none exists. |
| 58 | |
Ted Kremenek | 7c64741 | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 59 | CFGBlock* Entry = &getCFG().getEntry(); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 60 | |
| 61 | assert (Entry->empty() && |
| 62 | "Entry block must be empty."); |
| 63 | |
| 64 | assert (Entry->succ_size() == 1 && |
| 65 | "Entry block must have 1 successor."); |
| 66 | |
| 67 | // Get the solitary successor. |
| 68 | CFGBlock* Succ = *(Entry->succ_begin()); |
| 69 | |
| 70 | // Construct an edge representing the |
| 71 | // starting location in the function. |
Ted Kremenek | 7c64741 | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 72 | BlockEdge StartLoc(getCFG(), Entry, Succ); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 74 | // Set the current block counter to being empty. |
| 75 | WList->setBlockCounter(BCounterFactory.GetEmptyCounter()); |
| 76 | |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 77 | // Generate the root. |
| 78 | GenerateNode(StartLoc, getInitialState()); |
| 79 | } |
| 80 | |
| 81 | while (Steps && WList->hasWork()) { |
| 82 | --Steps; |
| 83 | const GRWorkListUnit& WU = WList->Dequeue(); |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 84 | |
| 85 | // Set the current block counter. |
| 86 | WList->setBlockCounter(WU.getBlockCounter()); |
| 87 | |
| 88 | // Retrieve the node. |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 89 | ExplodedNodeImpl* Node = WU.getNode(); |
| 90 | |
| 91 | // Dispatch on the location type. |
| 92 | switch (Node->getLocation().getKind()) { |
| 93 | default: |
| 94 | assert (isa<BlockEdge>(Node->getLocation())); |
| 95 | HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node); |
| 96 | break; |
| 97 | |
| 98 | case ProgramPoint::BlockEntranceKind: |
| 99 | HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node); |
| 100 | break; |
| 101 | |
| 102 | case ProgramPoint::BlockExitKind: |
Ted Kremenek | 3226a65 | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 103 | assert (false && "BlockExit location never occur in forward analysis."); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 104 | break; |
| 105 | |
| 106 | case ProgramPoint::PostStmtKind: |
| 107 | HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(), |
| 108 | WU.getIndex(), Node); |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return WList->hasWork(); |
| 114 | } |
| 115 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 116 | void GRCoreEngineImpl::HandleBlockEdge(const BlockEdge& L, ExplodedNodeImpl* Pred) { |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 117 | |
| 118 | CFGBlock* Blk = L.getDst(); |
| 119 | |
| 120 | // Check if we are entering the EXIT block. |
Ted Kremenek | 7c64741 | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 121 | if (Blk == &getCFG().getExit()) { |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 7c64741 | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 123 | assert (getCFG().getExit().size() == 0 |
| 124 | && "EXIT block cannot contain Stmts."); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 125 | |
| 126 | // Process the final state transition. |
| 127 | void* State = ProcessEOP(Blk, Pred->State); |
| 128 | |
| 129 | bool IsNew; |
| 130 | ExplodedNodeImpl* Node = G->getNodeImpl(BlockEntrance(Blk), State, &IsNew); |
| 131 | Node->addPredecessor(Pred); |
| 132 | |
| 133 | // If the node was freshly created, mark it as an "End-Of-Path" node. |
| 134 | if (IsNew) G->addEndOfPath(Node); |
| 135 | |
| 136 | // This path is done. Don't enqueue any more nodes. |
| 137 | return; |
| 138 | } |
Ted Kremenek | 5c6eeb1 | 2008-02-29 20:27:50 +0000 | [diff] [blame^] | 139 | |
| 140 | // FIXME: Should we allow ProcessBlockEntrance to also manipulate state? |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 5c6eeb1 | 2008-02-29 20:27:50 +0000 | [diff] [blame^] | 142 | if (ProcessBlockEntrance(Blk, Pred->State, WList->getBlockCounter())) |
| 143 | GenerateNode(BlockEntrance(Blk), Pred->State, Pred); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 146 | void GRCoreEngineImpl::HandleBlockEntrance(const BlockEntrance& L, |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 147 | ExplodedNodeImpl* Pred) { |
| 148 | |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 149 | // Increment the block counter. |
| 150 | GRBlockCounter Counter = WList->getBlockCounter(); |
| 151 | Counter = BCounterFactory.IncrementCount(Counter, L.getBlock()->getBlockID()); |
| 152 | WList->setBlockCounter(Counter); |
| 153 | |
| 154 | // Process the entrance of the block. |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 155 | if (Stmt* S = L.getFirstStmt()) { |
Ted Kremenek | 1118e58 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 156 | GRStmtNodeBuilderImpl Builder(L.getBlock(), 0, Pred, this); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 157 | ProcessStmt(S, Builder); |
| 158 | } |
Ted Kremenek | 3226a65 | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 159 | else |
| 160 | HandleBlockExit(L.getBlock(), Pred); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 164 | void GRCoreEngineImpl::HandleBlockExit(CFGBlock * B, ExplodedNodeImpl* Pred) { |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 166 | if (Stmt* Term = B->getTerminator()) { |
| 167 | switch (Term->getStmtClass()) { |
| 168 | default: |
| 169 | assert(false && "Analysis for this terminator not implemented."); |
| 170 | break; |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 171 | |
| 172 | case Stmt::BinaryOperatorClass: // '&&' and '||' |
| 173 | HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred); |
| 174 | return; |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 176 | case Stmt::ConditionalOperatorClass: |
| 177 | HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 178 | return; |
| 179 | |
| 180 | // FIXME: Use constant-folding in CFG construction to simplify this |
| 181 | // case. |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 182 | |
| 183 | case Stmt::ChooseExprClass: |
| 184 | HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 185 | return; |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 187 | case Stmt::DoStmtClass: |
| 188 | HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred); |
| 189 | return; |
| 190 | |
| 191 | case Stmt::ForStmtClass: |
| 192 | HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred); |
| 193 | return; |
Ted Kremenek | c22eb06 | 2008-02-13 16:56:51 +0000 | [diff] [blame] | 194 | |
| 195 | case Stmt::ContinueStmtClass: |
| 196 | case Stmt::BreakStmtClass: |
| 197 | case Stmt::GotoStmtClass: |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 198 | break; |
| 199 | |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 200 | case Stmt::IfStmtClass: |
| 201 | HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 202 | return; |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 203 | |
| 204 | case Stmt::IndirectGotoStmtClass: { |
| 205 | // Only 1 successor: the indirect goto dispatch block. |
| 206 | assert (B->succ_size() == 1); |
| 207 | |
| 208 | GRIndirectGotoNodeBuilderImpl |
| 209 | builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(), |
| 210 | *(B->succ_begin()), this); |
| 211 | |
| 212 | ProcessIndirectGoto(builder); |
| 213 | return; |
| 214 | } |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 215 | |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 216 | case Stmt::SwitchStmtClass: { |
| 217 | GRSwitchNodeBuilderImpl builder(Pred, B, |
| 218 | cast<SwitchStmt>(Term)->getCond(), |
| 219 | this); |
| 220 | |
| 221 | ProcessSwitch(builder); |
| 222 | return; |
| 223 | } |
| 224 | |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 225 | case Stmt::WhileStmtClass: |
| 226 | HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 227 | return; |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 230 | |
| 231 | assert (B->succ_size() == 1 && |
| 232 | "Blocks with no terminator should have at most 1 successor."); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 234 | GenerateNode(BlockEdge(getCFG(),B,*(B->succ_begin())), Pred->State, Pred); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 237 | void GRCoreEngineImpl::HandleBranch(Expr* Cond, Stmt* Term, CFGBlock * B, |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 238 | ExplodedNodeImpl* Pred) { |
| 239 | assert (B->succ_size() == 2); |
| 240 | |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 241 | GRBranchNodeBuilderImpl Builder(B, *(B->succ_begin()), *(B->succ_begin()+1), |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 242 | Pred, this); |
| 243 | |
| 244 | ProcessBranch(Cond, Term, Builder); |
| 245 | } |
| 246 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 247 | void GRCoreEngineImpl::HandlePostStmt(const PostStmt& L, CFGBlock* B, |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 248 | unsigned StmtIdx, ExplodedNodeImpl* Pred) { |
| 249 | |
| 250 | assert (!B->empty()); |
| 251 | |
Ted Kremenek | 3226a65 | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 252 | if (StmtIdx == B->size()) |
| 253 | HandleBlockExit(B, Pred); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 254 | else { |
Ted Kremenek | 1118e58 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 255 | GRStmtNodeBuilderImpl Builder(B, StmtIdx, Pred, this); |
Ted Kremenek | c0f1aae | 2008-01-16 22:13:19 +0000 | [diff] [blame] | 256 | ProcessStmt((*B)[StmtIdx], Builder); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | typedef llvm::DenseMap<Stmt*,Stmt*> ParentMapTy; |
| 261 | /// PopulateParentMap - Recurse the AST starting at 'Parent' and add the |
| 262 | /// mappings between child and parent to ParentMap. |
| 263 | static void PopulateParentMap(Stmt* Parent, ParentMapTy& M) { |
| 264 | for (Stmt::child_iterator I=Parent->child_begin(), |
| 265 | E=Parent->child_end(); I!=E; ++I) { |
| 266 | |
| 267 | assert (M.find(*I) == M.end()); |
| 268 | M[*I] = Parent; |
| 269 | PopulateParentMap(*I, M); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /// GenerateNode - Utility method to generate nodes, hook up successors, |
| 274 | /// and add nodes to the worklist. |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 275 | void GRCoreEngineImpl::GenerateNode(const ProgramPoint& Loc, void* State, |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 276 | ExplodedNodeImpl* Pred) { |
| 277 | |
| 278 | bool IsNew; |
| 279 | ExplodedNodeImpl* Node = G->getNodeImpl(Loc, State, &IsNew); |
| 280 | |
| 281 | if (Pred) |
| 282 | Node->addPredecessor(Pred); // Link 'Node' with its predecessor. |
| 283 | else { |
| 284 | assert (IsNew); |
| 285 | G->addRoot(Node); // 'Node' has no predecessor. Make it a root. |
| 286 | } |
| 287 | |
| 288 | // Only add 'Node' to the worklist if it was freshly generated. |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 289 | if (IsNew) WList->Enqueue(Node); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Ted Kremenek | 1118e58 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 292 | GRStmtNodeBuilderImpl::GRStmtNodeBuilderImpl(CFGBlock* b, unsigned idx, |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 293 | ExplodedNodeImpl* N, GRCoreEngineImpl* e) |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 294 | : Eng(*e), B(*b), Idx(idx), LastNode(N), Populated(false) { |
| 295 | Deferred.insert(N); |
| 296 | } |
| 297 | |
Ted Kremenek | 1118e58 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 298 | GRStmtNodeBuilderImpl::~GRStmtNodeBuilderImpl() { |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 299 | for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I) |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 300 | if (!(*I)->isSink()) |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 301 | GenerateAutoTransition(*I); |
| 302 | } |
| 303 | |
Ted Kremenek | 1118e58 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 304 | void GRStmtNodeBuilderImpl::GenerateAutoTransition(ExplodedNodeImpl* N) { |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 305 | assert (!N->isSink()); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 306 | |
| 307 | PostStmt Loc(getStmt()); |
| 308 | |
| 309 | if (Loc == N->getLocation()) { |
| 310 | // Note: 'N' should be a fresh node because otherwise it shouldn't be |
| 311 | // a member of Deferred. |
| 312 | Eng.WList->Enqueue(N, B, Idx+1); |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | bool IsNew; |
| 317 | ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(Loc, N->State, &IsNew); |
| 318 | Succ->addPredecessor(N); |
| 319 | |
| 320 | if (IsNew) |
| 321 | Eng.WList->Enqueue(Succ, B, Idx+1); |
| 322 | } |
| 323 | |
Ted Kremenek | 1118e58 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 324 | ExplodedNodeImpl* GRStmtNodeBuilderImpl::generateNodeImpl(Stmt* S, void* State, |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 325 | ExplodedNodeImpl* Pred) { |
| 326 | |
| 327 | bool IsNew; |
| 328 | ExplodedNodeImpl* N = Eng.G->getNodeImpl(PostStmt(S), State, &IsNew); |
| 329 | N->addPredecessor(Pred); |
| 330 | Deferred.erase(Pred); |
| 331 | |
| 332 | HasGeneratedNode = true; |
| 333 | |
| 334 | if (IsNew) { |
| 335 | Deferred.insert(N); |
| 336 | LastNode = N; |
| 337 | return N; |
| 338 | } |
| 339 | |
| 340 | LastNode = NULL; |
| 341 | return NULL; |
| 342 | } |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 343 | |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 344 | ExplodedNodeImpl* GRBranchNodeBuilderImpl::generateNodeImpl(void* State, |
| 345 | bool branch) { |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 346 | bool IsNew; |
| 347 | |
| 348 | ExplodedNodeImpl* Succ = |
| 349 | Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, branch ? DstT : DstF), |
| 350 | State, &IsNew); |
| 351 | |
| 352 | Succ->addPredecessor(Pred); |
| 353 | |
Ted Kremenek | 6ff3cea | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 354 | if (branch) GeneratedTrue = true; |
| 355 | else GeneratedFalse = true; |
| 356 | |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 357 | if (IsNew) { |
Ted Kremenek | 428d39e | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 358 | Deferred.push_back(Succ); |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 359 | return Succ; |
| 360 | } |
| 361 | |
| 362 | return NULL; |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 363 | } |
Ted Kremenek | 6ff3cea | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 364 | |
| 365 | GRBranchNodeBuilderImpl::~GRBranchNodeBuilderImpl() { |
| 366 | if (!GeneratedTrue) generateNodeImpl(Pred->State, true); |
| 367 | if (!GeneratedFalse) generateNodeImpl(Pred->State, false); |
Ted Kremenek | 428d39e | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 368 | |
| 369 | for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I) |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 370 | if (!(*I)->isSink()) Eng.WList->Enqueue(*I); |
Ted Kremenek | 6ff3cea | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 371 | } |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 372 | |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 373 | |
| 374 | ExplodedNodeImpl* |
Ted Kremenek | 79f63f5 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 375 | GRIndirectGotoNodeBuilderImpl::generateNodeImpl(const Iterator& I, |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 376 | void* St, |
| 377 | bool isSink) { |
| 378 | bool IsNew; |
| 379 | |
| 380 | ExplodedNodeImpl* Succ = |
Ted Kremenek | 79f63f5 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 381 | Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, I.getBlock(), true), |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 382 | St, &IsNew); |
| 383 | |
| 384 | Succ->addPredecessor(Pred); |
| 385 | |
| 386 | if (IsNew) { |
| 387 | |
| 388 | if (isSink) |
| 389 | Succ->markAsSink(); |
| 390 | else |
| 391 | Eng.WList->Enqueue(Succ); |
| 392 | |
| 393 | return Succ; |
| 394 | } |
| 395 | |
| 396 | return NULL; |
| 397 | } |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 398 | |
| 399 | |
| 400 | ExplodedNodeImpl* |
| 401 | GRSwitchNodeBuilderImpl::generateCaseStmtNodeImpl(const Iterator& I, void* St) { |
| 402 | |
| 403 | bool IsNew; |
| 404 | |
| 405 | ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, |
| 406 | I.getBlock()), |
| 407 | St, &IsNew); |
| 408 | Succ->addPredecessor(Pred); |
| 409 | |
| 410 | if (IsNew) { |
| 411 | Eng.WList->Enqueue(Succ); |
| 412 | return Succ; |
| 413 | } |
| 414 | |
| 415 | return NULL; |
| 416 | } |
| 417 | |
| 418 | |
| 419 | ExplodedNodeImpl* |
| 420 | GRSwitchNodeBuilderImpl::generateDefaultCaseNodeImpl(void* St, bool isSink) { |
| 421 | |
| 422 | // Get the block for the default case. |
| 423 | assert (Src->succ_rbegin() != Src->succ_rend()); |
| 424 | CFGBlock* DefaultBlock = *Src->succ_rbegin(); |
| 425 | |
| 426 | bool IsNew; |
| 427 | |
| 428 | ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, |
| 429 | DefaultBlock), |
| 430 | St, &IsNew); |
| 431 | Succ->addPredecessor(Pred); |
| 432 | |
| 433 | if (IsNew) { |
| 434 | if (isSink) |
| 435 | Succ->markAsSink(); |
| 436 | else |
| 437 | Eng.WList->Enqueue(Succ); |
| 438 | |
| 439 | return Succ; |
| 440 | } |
| 441 | |
| 442 | return NULL; |
| 443 | } |