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