Ted Kremenek | f6c62f3 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1 | //==- GRCoreEngine.cpp - Path-Sensitive Dataflow Engine ----------------*- C++ -*-// |
Ted Kremenek | 3e74366 | 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 | f6c62f3 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/GRCoreEngine.h" |
Ted Kremenek | 3e74366 | 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 | 2e12c2e | 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 | 3e74366 | 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 | f6c62f3 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 54 | bool GRCoreEngineImpl::ExecuteWorkList(unsigned Steps) { |
Ted Kremenek | 3e74366 | 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 | 997d872 | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 59 | CFGBlock* Entry = &getCFG().getEntry(); |
Ted Kremenek | 3e74366 | 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 | 997d872 | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 72 | BlockEdge StartLoc(getCFG(), Entry, Succ); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | 90ae68f | 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 | 3e74366 | 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 | 90ae68f | 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 | 3e74366 | 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 | e584359 | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 103 | assert (false && "BlockExit location never occur in forward analysis."); |
Ted Kremenek | 3e74366 | 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 | df4a5b9 | 2008-03-05 19:08:15 +0000 | [diff] [blame^] | 116 | void GRCoreEngineImpl::HandleBlockEdge(const BlockEdge& L, |
| 117 | ExplodedNodeImpl* Pred) { |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 118 | |
| 119 | CFGBlock* Blk = L.getDst(); |
| 120 | |
| 121 | // Check if we are entering the EXIT block. |
Ted Kremenek | 997d872 | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 122 | if (Blk == &getCFG().getExit()) { |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | 997d872 | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 124 | assert (getCFG().getExit().size() == 0 |
| 125 | && "EXIT block cannot contain Stmts."); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 126 | |
| 127 | // Process the final state transition. |
| 128 | void* State = ProcessEOP(Blk, Pred->State); |
| 129 | |
| 130 | bool IsNew; |
| 131 | ExplodedNodeImpl* Node = G->getNodeImpl(BlockEntrance(Blk), State, &IsNew); |
| 132 | Node->addPredecessor(Pred); |
| 133 | |
| 134 | // If the node was freshly created, mark it as an "End-Of-Path" node. |
| 135 | if (IsNew) G->addEndOfPath(Node); |
| 136 | |
| 137 | // This path is done. Don't enqueue any more nodes. |
| 138 | return; |
| 139 | } |
Ted Kremenek | 17f4dbd | 2008-02-29 20:27:50 +0000 | [diff] [blame] | 140 | |
| 141 | // FIXME: Should we allow ProcessBlockEntrance to also manipulate state? |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 142 | |
Ted Kremenek | 17f4dbd | 2008-02-29 20:27:50 +0000 | [diff] [blame] | 143 | if (ProcessBlockEntrance(Blk, Pred->State, WList->getBlockCounter())) |
| 144 | GenerateNode(BlockEntrance(Blk), Pred->State, Pred); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Ted Kremenek | f6c62f3 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 147 | void GRCoreEngineImpl::HandleBlockEntrance(const BlockEntrance& L, |
Ted Kremenek | df4a5b9 | 2008-03-05 19:08:15 +0000 | [diff] [blame^] | 148 | ExplodedNodeImpl* Pred) { |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 149 | |
Ted Kremenek | 90ae68f | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 150 | // Increment the block counter. |
| 151 | GRBlockCounter Counter = WList->getBlockCounter(); |
| 152 | Counter = BCounterFactory.IncrementCount(Counter, L.getBlock()->getBlockID()); |
| 153 | WList->setBlockCounter(Counter); |
| 154 | |
| 155 | // Process the entrance of the block. |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 156 | if (Stmt* S = L.getFirstStmt()) { |
Ted Kremenek | b2cad31 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 157 | GRStmtNodeBuilderImpl Builder(L.getBlock(), 0, Pred, this); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 158 | ProcessStmt(S, Builder); |
| 159 | } |
Ted Kremenek | e584359 | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 160 | else |
| 161 | HandleBlockExit(L.getBlock(), Pred); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
Ted Kremenek | f6c62f3 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 165 | void GRCoreEngineImpl::HandleBlockExit(CFGBlock * B, ExplodedNodeImpl* Pred) { |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 166 | |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 167 | if (Stmt* Term = B->getTerminator()) { |
| 168 | switch (Term->getStmtClass()) { |
| 169 | default: |
| 170 | assert(false && "Analysis for this terminator not implemented."); |
| 171 | break; |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 172 | |
| 173 | case Stmt::BinaryOperatorClass: // '&&' and '||' |
| 174 | HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred); |
| 175 | return; |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 176 | |
Ted Kremenek | 3f2f1ad | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 177 | case Stmt::ConditionalOperatorClass: |
| 178 | HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 179 | return; |
| 180 | |
| 181 | // FIXME: Use constant-folding in CFG construction to simplify this |
| 182 | // case. |
Ted Kremenek | 3f2f1ad | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 183 | |
| 184 | case Stmt::ChooseExprClass: |
| 185 | HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 186 | return; |
Ted Kremenek | 3f2f1ad | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 188 | case Stmt::DoStmtClass: |
| 189 | HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred); |
| 190 | return; |
| 191 | |
| 192 | case Stmt::ForStmtClass: |
| 193 | HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred); |
| 194 | return; |
Ted Kremenek | 632bcb8 | 2008-02-13 16:56:51 +0000 | [diff] [blame] | 195 | |
| 196 | case Stmt::ContinueStmtClass: |
| 197 | case Stmt::BreakStmtClass: |
| 198 | case Stmt::GotoStmtClass: |
Ted Kremenek | 3f2f1ad | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 199 | break; |
| 200 | |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 201 | case Stmt::IfStmtClass: |
| 202 | HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 203 | return; |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 204 | |
| 205 | case Stmt::IndirectGotoStmtClass: { |
| 206 | // Only 1 successor: the indirect goto dispatch block. |
| 207 | assert (B->succ_size() == 1); |
| 208 | |
| 209 | GRIndirectGotoNodeBuilderImpl |
| 210 | builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(), |
| 211 | *(B->succ_begin()), this); |
| 212 | |
| 213 | ProcessIndirectGoto(builder); |
| 214 | return; |
| 215 | } |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 216 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 217 | case Stmt::SwitchStmtClass: { |
| 218 | GRSwitchNodeBuilderImpl builder(Pred, B, |
| 219 | cast<SwitchStmt>(Term)->getCond(), |
| 220 | this); |
| 221 | |
| 222 | ProcessSwitch(builder); |
| 223 | return; |
| 224 | } |
| 225 | |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 226 | case Stmt::WhileStmtClass: |
| 227 | HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 228 | return; |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 231 | |
| 232 | assert (B->succ_size() == 1 && |
| 233 | "Blocks with no terminator should have at most 1 successor."); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 234 | |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 235 | GenerateNode(BlockEdge(getCFG(),B,*(B->succ_begin())), Pred->State, Pred); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Ted Kremenek | f6c62f3 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 238 | void GRCoreEngineImpl::HandleBranch(Expr* Cond, Stmt* Term, CFGBlock * B, |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 239 | ExplodedNodeImpl* Pred) { |
| 240 | assert (B->succ_size() == 2); |
| 241 | |
Ted Kremenek | 90ae68f | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 242 | GRBranchNodeBuilderImpl Builder(B, *(B->succ_begin()), *(B->succ_begin()+1), |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 243 | Pred, this); |
| 244 | |
| 245 | ProcessBranch(Cond, Term, Builder); |
| 246 | } |
| 247 | |
Ted Kremenek | f6c62f3 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 248 | void GRCoreEngineImpl::HandlePostStmt(const PostStmt& L, CFGBlock* B, |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 249 | unsigned StmtIdx, ExplodedNodeImpl* Pred) { |
| 250 | |
| 251 | assert (!B->empty()); |
| 252 | |
Ted Kremenek | e584359 | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 253 | if (StmtIdx == B->size()) |
| 254 | HandleBlockExit(B, Pred); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 255 | else { |
Ted Kremenek | b2cad31 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 256 | GRStmtNodeBuilderImpl Builder(B, StmtIdx, Pred, this); |
Ted Kremenek | e914bb8 | 2008-01-16 22:13:19 +0000 | [diff] [blame] | 257 | ProcessStmt((*B)[StmtIdx], Builder); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | typedef llvm::DenseMap<Stmt*,Stmt*> ParentMapTy; |
| 262 | /// PopulateParentMap - Recurse the AST starting at 'Parent' and add the |
| 263 | /// mappings between child and parent to ParentMap. |
| 264 | static void PopulateParentMap(Stmt* Parent, ParentMapTy& M) { |
| 265 | for (Stmt::child_iterator I=Parent->child_begin(), |
| 266 | E=Parent->child_end(); I!=E; ++I) { |
| 267 | |
| 268 | assert (M.find(*I) == M.end()); |
| 269 | M[*I] = Parent; |
| 270 | PopulateParentMap(*I, M); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /// GenerateNode - Utility method to generate nodes, hook up successors, |
| 275 | /// and add nodes to the worklist. |
Ted Kremenek | f6c62f3 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 276 | void GRCoreEngineImpl::GenerateNode(const ProgramPoint& Loc, void* State, |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 277 | ExplodedNodeImpl* Pred) { |
| 278 | |
| 279 | bool IsNew; |
| 280 | ExplodedNodeImpl* Node = G->getNodeImpl(Loc, State, &IsNew); |
| 281 | |
| 282 | if (Pred) |
| 283 | Node->addPredecessor(Pred); // Link 'Node' with its predecessor. |
| 284 | else { |
| 285 | assert (IsNew); |
| 286 | G->addRoot(Node); // 'Node' has no predecessor. Make it a root. |
| 287 | } |
| 288 | |
| 289 | // Only add 'Node' to the worklist if it was freshly generated. |
Ted Kremenek | 90ae68f | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 290 | if (IsNew) WList->Enqueue(Node); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Ted Kremenek | b2cad31 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 293 | GRStmtNodeBuilderImpl::GRStmtNodeBuilderImpl(CFGBlock* b, unsigned idx, |
Ted Kremenek | f6c62f3 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 294 | ExplodedNodeImpl* N, GRCoreEngineImpl* e) |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 295 | : Eng(*e), B(*b), Idx(idx), LastNode(N), Populated(false) { |
| 296 | Deferred.insert(N); |
| 297 | } |
| 298 | |
Ted Kremenek | b2cad31 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 299 | GRStmtNodeBuilderImpl::~GRStmtNodeBuilderImpl() { |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 300 | for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I) |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 301 | if (!(*I)->isSink()) |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 302 | GenerateAutoTransition(*I); |
| 303 | } |
| 304 | |
Ted Kremenek | b2cad31 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 305 | void GRStmtNodeBuilderImpl::GenerateAutoTransition(ExplodedNodeImpl* N) { |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 306 | assert (!N->isSink()); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 307 | |
| 308 | PostStmt Loc(getStmt()); |
| 309 | |
| 310 | if (Loc == N->getLocation()) { |
| 311 | // Note: 'N' should be a fresh node because otherwise it shouldn't be |
| 312 | // a member of Deferred. |
| 313 | Eng.WList->Enqueue(N, B, Idx+1); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | bool IsNew; |
| 318 | ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(Loc, N->State, &IsNew); |
| 319 | Succ->addPredecessor(N); |
| 320 | |
| 321 | if (IsNew) |
| 322 | Eng.WList->Enqueue(Succ, B, Idx+1); |
| 323 | } |
| 324 | |
Ted Kremenek | b2cad31 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 325 | ExplodedNodeImpl* GRStmtNodeBuilderImpl::generateNodeImpl(Stmt* S, void* State, |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 326 | ExplodedNodeImpl* Pred) { |
| 327 | |
| 328 | bool IsNew; |
| 329 | ExplodedNodeImpl* N = Eng.G->getNodeImpl(PostStmt(S), State, &IsNew); |
| 330 | N->addPredecessor(Pred); |
| 331 | Deferred.erase(Pred); |
| 332 | |
| 333 | HasGeneratedNode = true; |
| 334 | |
| 335 | if (IsNew) { |
| 336 | Deferred.insert(N); |
| 337 | LastNode = N; |
| 338 | return N; |
| 339 | } |
| 340 | |
| 341 | LastNode = NULL; |
| 342 | return NULL; |
| 343 | } |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 344 | |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 345 | ExplodedNodeImpl* GRBranchNodeBuilderImpl::generateNodeImpl(void* State, |
| 346 | bool branch) { |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 347 | bool IsNew; |
| 348 | |
| 349 | ExplodedNodeImpl* Succ = |
| 350 | Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, branch ? DstT : DstF), |
| 351 | State, &IsNew); |
| 352 | |
| 353 | Succ->addPredecessor(Pred); |
| 354 | |
Ted Kremenek | 7ff1893 | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 355 | if (branch) GeneratedTrue = true; |
| 356 | else GeneratedFalse = true; |
| 357 | |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 358 | if (IsNew) { |
Ted Kremenek | 2531fce | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 359 | Deferred.push_back(Succ); |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 360 | return Succ; |
| 361 | } |
| 362 | |
| 363 | return NULL; |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 364 | } |
Ted Kremenek | 7ff1893 | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 365 | |
| 366 | GRBranchNodeBuilderImpl::~GRBranchNodeBuilderImpl() { |
| 367 | if (!GeneratedTrue) generateNodeImpl(Pred->State, true); |
| 368 | if (!GeneratedFalse) generateNodeImpl(Pred->State, false); |
Ted Kremenek | 2531fce | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 369 | |
| 370 | for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I) |
Ted Kremenek | 90ae68f | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 371 | if (!(*I)->isSink()) Eng.WList->Enqueue(*I); |
Ted Kremenek | 7ff1893 | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 372 | } |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 373 | |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 374 | |
| 375 | ExplodedNodeImpl* |
Ted Kremenek | 2bba901 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 376 | GRIndirectGotoNodeBuilderImpl::generateNodeImpl(const Iterator& I, |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 377 | void* St, |
| 378 | bool isSink) { |
| 379 | bool IsNew; |
| 380 | |
| 381 | ExplodedNodeImpl* Succ = |
Ted Kremenek | 2bba901 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 382 | Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, I.getBlock(), true), |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 383 | St, &IsNew); |
| 384 | |
| 385 | Succ->addPredecessor(Pred); |
| 386 | |
| 387 | if (IsNew) { |
| 388 | |
| 389 | if (isSink) |
| 390 | Succ->markAsSink(); |
| 391 | else |
| 392 | Eng.WList->Enqueue(Succ); |
| 393 | |
| 394 | return Succ; |
| 395 | } |
| 396 | |
| 397 | return NULL; |
| 398 | } |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 399 | |
| 400 | |
| 401 | ExplodedNodeImpl* |
| 402 | GRSwitchNodeBuilderImpl::generateCaseStmtNodeImpl(const Iterator& I, void* St) { |
| 403 | |
| 404 | bool IsNew; |
| 405 | |
| 406 | ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, |
| 407 | I.getBlock()), |
| 408 | St, &IsNew); |
| 409 | Succ->addPredecessor(Pred); |
| 410 | |
| 411 | if (IsNew) { |
| 412 | Eng.WList->Enqueue(Succ); |
| 413 | return Succ; |
| 414 | } |
| 415 | |
| 416 | return NULL; |
| 417 | } |
| 418 | |
| 419 | |
| 420 | ExplodedNodeImpl* |
| 421 | GRSwitchNodeBuilderImpl::generateDefaultCaseNodeImpl(void* St, bool isSink) { |
| 422 | |
| 423 | // Get the block for the default case. |
| 424 | assert (Src->succ_rbegin() != Src->succ_rend()); |
| 425 | CFGBlock* DefaultBlock = *Src->succ_rbegin(); |
| 426 | |
| 427 | bool IsNew; |
| 428 | |
| 429 | ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, |
| 430 | DefaultBlock), |
| 431 | St, &IsNew); |
| 432 | Succ->addPredecessor(Pred); |
| 433 | |
| 434 | if (IsNew) { |
| 435 | if (isSink) |
| 436 | Succ->markAsSink(); |
| 437 | else |
| 438 | Eng.WList->Enqueue(Succ); |
| 439 | |
| 440 | return Succ; |
| 441 | } |
| 442 | |
| 443 | return NULL; |
| 444 | } |