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; |
Ted Kremenek | 5f6b442 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 105 | |
| 106 | case ProgramPoint::PostLoadKind: |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 107 | case ProgramPoint::PostStmtKind: |
| 108 | HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(), |
| 109 | WU.getIndex(), Node); |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return WList->hasWork(); |
| 115 | } |
| 116 | |
Ted Kremenek | 6d7b801 | 2008-03-05 19:08:15 +0000 | [diff] [blame] | 117 | void GRCoreEngineImpl::HandleBlockEdge(const BlockEdge& L, |
| 118 | ExplodedNodeImpl* Pred) { |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 119 | |
| 120 | CFGBlock* Blk = L.getDst(); |
| 121 | |
| 122 | // Check if we are entering the EXIT block. |
Ted Kremenek | 7c64741 | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 123 | if (Blk == &getCFG().getExit()) { |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 124 | |
Ted Kremenek | 7c64741 | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 125 | assert (getCFG().getExit().size() == 0 |
| 126 | && "EXIT block cannot contain Stmts."); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | db4412d | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 128 | // Process the final state transition. |
| 129 | GREndPathNodeBuilderImpl Builder(Blk, Pred, this); |
| 130 | ProcessEndPath(Builder); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 132 | // This path is done. Don't enqueue any more nodes. |
| 133 | return; |
| 134 | } |
Ted Kremenek | 5c6eeb1 | 2008-02-29 20:27:50 +0000 | [diff] [blame] | 135 | |
| 136 | // FIXME: Should we allow ProcessBlockEntrance to also manipulate state? |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 137 | |
Ted Kremenek | 5c6eeb1 | 2008-02-29 20:27:50 +0000 | [diff] [blame] | 138 | if (ProcessBlockEntrance(Blk, Pred->State, WList->getBlockCounter())) |
| 139 | GenerateNode(BlockEntrance(Blk), Pred->State, Pred); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 142 | void GRCoreEngineImpl::HandleBlockEntrance(const BlockEntrance& L, |
Ted Kremenek | 6d7b801 | 2008-03-05 19:08:15 +0000 | [diff] [blame] | 143 | ExplodedNodeImpl* Pred) { |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 145 | // Increment the block counter. |
| 146 | GRBlockCounter Counter = WList->getBlockCounter(); |
| 147 | Counter = BCounterFactory.IncrementCount(Counter, L.getBlock()->getBlockID()); |
| 148 | WList->setBlockCounter(Counter); |
| 149 | |
| 150 | // Process the entrance of the block. |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 151 | if (Stmt* S = L.getFirstStmt()) { |
Ted Kremenek | 1118e58 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 152 | GRStmtNodeBuilderImpl Builder(L.getBlock(), 0, Pred, this); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 153 | ProcessStmt(S, Builder); |
| 154 | } |
Ted Kremenek | 3226a65 | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 155 | else |
| 156 | HandleBlockExit(L.getBlock(), Pred); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 160 | void GRCoreEngineImpl::HandleBlockExit(CFGBlock * B, ExplodedNodeImpl* Pred) { |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 162 | if (Stmt* Term = B->getTerminator()) { |
| 163 | switch (Term->getStmtClass()) { |
| 164 | default: |
| 165 | assert(false && "Analysis for this terminator not implemented."); |
| 166 | break; |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 167 | |
| 168 | case Stmt::BinaryOperatorClass: // '&&' and '||' |
| 169 | HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred); |
| 170 | return; |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 171 | |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 172 | case Stmt::ConditionalOperatorClass: |
| 173 | HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 174 | return; |
| 175 | |
| 176 | // FIXME: Use constant-folding in CFG construction to simplify this |
| 177 | // case. |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 178 | |
| 179 | case Stmt::ChooseExprClass: |
| 180 | HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 181 | return; |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 183 | case Stmt::DoStmtClass: |
| 184 | HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred); |
| 185 | return; |
| 186 | |
| 187 | case Stmt::ForStmtClass: |
| 188 | HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred); |
| 189 | return; |
Ted Kremenek | c22eb06 | 2008-02-13 16:56:51 +0000 | [diff] [blame] | 190 | |
| 191 | case Stmt::ContinueStmtClass: |
| 192 | case Stmt::BreakStmtClass: |
| 193 | case Stmt::GotoStmtClass: |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 194 | break; |
| 195 | |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 196 | case Stmt::IfStmtClass: |
| 197 | HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 198 | return; |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 199 | |
| 200 | case Stmt::IndirectGotoStmtClass: { |
| 201 | // Only 1 successor: the indirect goto dispatch block. |
| 202 | assert (B->succ_size() == 1); |
| 203 | |
| 204 | GRIndirectGotoNodeBuilderImpl |
| 205 | builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(), |
| 206 | *(B->succ_begin()), this); |
| 207 | |
| 208 | ProcessIndirectGoto(builder); |
| 209 | return; |
| 210 | } |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 212 | case Stmt::SwitchStmtClass: { |
| 213 | GRSwitchNodeBuilderImpl builder(Pred, B, |
| 214 | cast<SwitchStmt>(Term)->getCond(), |
| 215 | this); |
| 216 | |
| 217 | ProcessSwitch(builder); |
| 218 | return; |
| 219 | } |
| 220 | |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 221 | case Stmt::WhileStmtClass: |
| 222 | HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 223 | return; |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 226 | |
| 227 | assert (B->succ_size() == 1 && |
| 228 | "Blocks with no terminator should have at most 1 successor."); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 229 | |
Ted Kremenek | 6ff5218 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 230 | GenerateNode(BlockEdge(getCFG(),B,*(B->succ_begin())), Pred->State, Pred); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 233 | void GRCoreEngineImpl::HandleBranch(Expr* Cond, Stmt* Term, CFGBlock * B, |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 234 | ExplodedNodeImpl* Pred) { |
| 235 | assert (B->succ_size() == 2); |
| 236 | |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 237 | GRBranchNodeBuilderImpl Builder(B, *(B->succ_begin()), *(B->succ_begin()+1), |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 238 | Pred, this); |
| 239 | |
| 240 | ProcessBranch(Cond, Term, Builder); |
| 241 | } |
| 242 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 243 | void GRCoreEngineImpl::HandlePostStmt(const PostStmt& L, CFGBlock* B, |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 244 | unsigned StmtIdx, ExplodedNodeImpl* Pred) { |
| 245 | |
| 246 | assert (!B->empty()); |
| 247 | |
Ted Kremenek | 3226a65 | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 248 | if (StmtIdx == B->size()) |
| 249 | HandleBlockExit(B, Pred); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 250 | else { |
Ted Kremenek | 1118e58 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 251 | GRStmtNodeBuilderImpl Builder(B, StmtIdx, Pred, this); |
Ted Kremenek | c0f1aae | 2008-01-16 22:13:19 +0000 | [diff] [blame] | 252 | ProcessStmt((*B)[StmtIdx], Builder); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 256 | /// GenerateNode - Utility method to generate nodes, hook up successors, |
| 257 | /// and add nodes to the worklist. |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 258 | void GRCoreEngineImpl::GenerateNode(const ProgramPoint& Loc, const void* State, |
| 259 | ExplodedNodeImpl* Pred) { |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 260 | |
| 261 | bool IsNew; |
| 262 | ExplodedNodeImpl* Node = G->getNodeImpl(Loc, State, &IsNew); |
| 263 | |
| 264 | if (Pred) |
| 265 | Node->addPredecessor(Pred); // Link 'Node' with its predecessor. |
| 266 | else { |
| 267 | assert (IsNew); |
| 268 | G->addRoot(Node); // 'Node' has no predecessor. Make it a root. |
| 269 | } |
| 270 | |
| 271 | // Only add 'Node' to the worklist if it was freshly generated. |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 272 | if (IsNew) WList->Enqueue(Node); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Ted Kremenek | 1118e58 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 275 | GRStmtNodeBuilderImpl::GRStmtNodeBuilderImpl(CFGBlock* b, unsigned idx, |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 276 | ExplodedNodeImpl* N, GRCoreEngineImpl* e) |
Ted Kremenek | 0b03c6e | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 277 | : Eng(*e), B(*b), Idx(idx), Pred(N), LastNode(N) { |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 278 | Deferred.insert(N); |
| 279 | } |
| 280 | |
Ted Kremenek | 1118e58 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 281 | GRStmtNodeBuilderImpl::~GRStmtNodeBuilderImpl() { |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 282 | for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I) |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 283 | if (!(*I)->isSink()) |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 284 | GenerateAutoTransition(*I); |
| 285 | } |
| 286 | |
Ted Kremenek | 1118e58 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 287 | void GRStmtNodeBuilderImpl::GenerateAutoTransition(ExplodedNodeImpl* N) { |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 288 | assert (!N->isSink()); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 289 | |
| 290 | PostStmt Loc(getStmt()); |
| 291 | |
| 292 | if (Loc == N->getLocation()) { |
| 293 | // Note: 'N' should be a fresh node because otherwise it shouldn't be |
| 294 | // a member of Deferred. |
| 295 | Eng.WList->Enqueue(N, B, Idx+1); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | bool IsNew; |
| 300 | ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(Loc, N->State, &IsNew); |
| 301 | Succ->addPredecessor(N); |
| 302 | |
| 303 | if (IsNew) |
| 304 | Eng.WList->Enqueue(Succ, B, Idx+1); |
| 305 | } |
| 306 | |
Ted Kremenek | f05eec4 | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 307 | static inline ProgramPoint GetPostLoc(Stmt* S, ProgramPoint::Kind K) { |
| 308 | switch (K) { |
| 309 | default: |
| 310 | assert(false && "Invalid PostXXXKind."); |
| 311 | |
| 312 | case ProgramPoint::PostStmtKind: |
| 313 | return PostStmt(S); |
| 314 | |
| 315 | case ProgramPoint::PostLoadKind: |
| 316 | return PostLoad(S); |
| 317 | |
| 318 | case ProgramPoint::PostPurgeDeadSymbolsKind: |
| 319 | return PostPurgeDeadSymbols(S); |
| 320 | } |
| 321 | } |
| 322 | |
Ted Kremenek | 5f6b442 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 323 | ExplodedNodeImpl* |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 324 | GRStmtNodeBuilderImpl::generateNodeImpl(Stmt* S, const void* State, |
Ted Kremenek | f05eec4 | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 325 | ExplodedNodeImpl* Pred, |
| 326 | ProgramPoint::Kind K) { |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 327 | |
| 328 | bool IsNew; |
Ted Kremenek | f05eec4 | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 329 | ProgramPoint Loc = GetPostLoc(S, K); |
| 330 | |
Ted Kremenek | 5f6b442 | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 331 | ExplodedNodeImpl* N = Eng.G->getNodeImpl(Loc, State, &IsNew); |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 332 | N->addPredecessor(Pred); |
| 333 | Deferred.erase(Pred); |
| 334 | |
Ted Kremenek | ef27b4b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 335 | if (IsNew) { |
| 336 | Deferred.insert(N); |
| 337 | LastNode = N; |
| 338 | return N; |
| 339 | } |
| 340 | |
| 341 | LastNode = NULL; |
| 342 | return NULL; |
| 343 | } |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 344 | |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 345 | ExplodedNodeImpl* GRBranchNodeBuilderImpl::generateNodeImpl(const void* State, |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 346 | bool branch) { |
Ted Kremenek | 19fbb10 | 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 | 6ff3cea | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 355 | if (branch) GeneratedTrue = true; |
| 356 | else GeneratedFalse = true; |
| 357 | |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 358 | if (IsNew) { |
Ted Kremenek | 428d39e | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 359 | Deferred.push_back(Succ); |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 360 | return Succ; |
| 361 | } |
| 362 | |
| 363 | return NULL; |
Ted Kremenek | 19fbb10 | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 364 | } |
Ted Kremenek | 6ff3cea | 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 | 428d39e | 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 | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 371 | if (!(*I)->isSink()) Eng.WList->Enqueue(*I); |
Ted Kremenek | 6ff3cea | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 372 | } |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 373 | |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 374 | |
| 375 | ExplodedNodeImpl* |
Ted Kremenek | 79f63f5 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 376 | GRIndirectGotoNodeBuilderImpl::generateNodeImpl(const Iterator& I, |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 377 | const void* St, |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 378 | bool isSink) { |
| 379 | bool IsNew; |
| 380 | |
| 381 | ExplodedNodeImpl* Succ = |
Ted Kremenek | 79f63f5 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 382 | Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, I.getBlock(), true), |
Ted Kremenek | 677f4ef | 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 | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 399 | |
| 400 | |
| 401 | ExplodedNodeImpl* |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 402 | GRSwitchNodeBuilderImpl::generateCaseStmtNodeImpl(const Iterator& I, |
| 403 | const void* St) { |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 404 | |
| 405 | bool IsNew; |
| 406 | |
| 407 | ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, |
| 408 | I.getBlock()), |
| 409 | St, &IsNew); |
| 410 | Succ->addPredecessor(Pred); |
| 411 | |
| 412 | if (IsNew) { |
| 413 | Eng.WList->Enqueue(Succ); |
| 414 | return Succ; |
| 415 | } |
| 416 | |
| 417 | return NULL; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | ExplodedNodeImpl* |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 422 | GRSwitchNodeBuilderImpl::generateDefaultCaseNodeImpl(const void* St, |
| 423 | bool isSink) { |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 424 | |
| 425 | // Get the block for the default case. |
| 426 | assert (Src->succ_rbegin() != Src->succ_rend()); |
| 427 | CFGBlock* DefaultBlock = *Src->succ_rbegin(); |
| 428 | |
| 429 | bool IsNew; |
| 430 | |
| 431 | ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, |
| 432 | DefaultBlock), |
| 433 | St, &IsNew); |
| 434 | Succ->addPredecessor(Pred); |
| 435 | |
| 436 | if (IsNew) { |
| 437 | if (isSink) |
| 438 | Succ->markAsSink(); |
| 439 | else |
| 440 | Eng.WList->Enqueue(Succ); |
| 441 | |
| 442 | return Succ; |
| 443 | } |
| 444 | |
| 445 | return NULL; |
| 446 | } |
Ted Kremenek | db4412d | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 447 | |
| 448 | GREndPathNodeBuilderImpl::~GREndPathNodeBuilderImpl() { |
| 449 | // Auto-generate an EOP node if one has not been generated. |
| 450 | if (!HasGeneratedNode) generateNodeImpl(Pred->State); |
| 451 | } |
| 452 | |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 453 | ExplodedNodeImpl* GREndPathNodeBuilderImpl::generateNodeImpl(const void* State){ |
Ted Kremenek | db4412d | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 454 | HasGeneratedNode = true; |
| 455 | |
| 456 | bool IsNew; |
| 457 | |
| 458 | ExplodedNodeImpl* Node = |
Ted Kremenek | 1e54cc5 | 2008-04-16 22:30:40 +0000 | [diff] [blame] | 459 | Eng.G->getNodeImpl(BlockEntrance(&B), State, &IsNew); |
Ted Kremenek | db4412d | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 460 | |
| 461 | |
| 462 | Node->addPredecessor(Pred); |
| 463 | |
| 464 | if (IsNew) { |
| 465 | Node->markAsSink(); |
| 466 | Eng.G->addEndOfPath(Node); |
Ted Kremenek | cfc909d | 2008-04-18 16:30:14 +0000 | [diff] [blame] | 467 | return Node; |
Ted Kremenek | db4412d | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Ted Kremenek | cfc909d | 2008-04-18 16:30:14 +0000 | [diff] [blame] | 470 | return NULL; |
Ted Kremenek | db4412d | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 471 | } |