Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1 | //==- GRCoreEngine.cpp - Path-Sensitive Dataflow Engine ------------*- C++ -*-// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2 | // |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 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 | |
Zhongxing Xu | adf644d | 2010-07-22 13:52:13 +0000 | [diff] [blame] | 15 | #include "clang/Checker/PathSensitive/AnalysisManager.h" |
Ted Kremenek | d6b8708 | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 16 | #include "clang/Checker/PathSensitive/GRCoreEngine.h" |
| 17 | #include "clang/Checker/PathSensitive/GRExprEngine.h" |
Zhongxing Xu | adf644d | 2010-07-22 13:52:13 +0000 | [diff] [blame] | 18 | #include "clang/Index/TranslationUnit.h" |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Casting.h" |
| 21 | #include "llvm/ADT/DenseMap.h" |
| 22 | #include <vector> |
Ted Kremenek | d9de9f1 | 2008-12-16 22:13:33 +0000 | [diff] [blame] | 23 | #include <queue> |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 24 | |
| 25 | using llvm::cast; |
| 26 | using llvm::isa; |
| 27 | using namespace clang; |
| 28 | |
Zhongxing Xu | adf644d | 2010-07-22 13:52:13 +0000 | [diff] [blame] | 29 | // This should be removed in the future. |
| 30 | namespace clang { |
| 31 | GRTransferFuncs* MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled, |
| 32 | const LangOptions& lopts); |
| 33 | } |
| 34 | |
Ted Kremenek | d9de9f1 | 2008-12-16 22:13:33 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
| 36 | // Worklist classes for exploration of reachable states. |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 39 | namespace { |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 40 | class DFS : public GRWorkList { |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 41 | llvm::SmallVector<GRWorkListUnit,20> Stack; |
| 42 | public: |
| 43 | virtual bool hasWork() const { |
| 44 | return !Stack.empty(); |
| 45 | } |
| 46 | |
| 47 | virtual void Enqueue(const GRWorkListUnit& U) { |
| 48 | Stack.push_back(U); |
| 49 | } |
| 50 | |
| 51 | virtual GRWorkListUnit Dequeue() { |
| 52 | assert (!Stack.empty()); |
| 53 | const GRWorkListUnit& U = Stack.back(); |
| 54 | Stack.pop_back(); // This technically "invalidates" U, but we are fine. |
| 55 | return U; |
| 56 | } |
| 57 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 59 | class BFS : public GRWorkList { |
Ted Kremenek | 2c32773 | 2009-05-01 22:18:46 +0000 | [diff] [blame] | 60 | std::queue<GRWorkListUnit> Queue; |
| 61 | public: |
| 62 | virtual bool hasWork() const { |
| 63 | return !Queue.empty(); |
| 64 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | 2c32773 | 2009-05-01 22:18:46 +0000 | [diff] [blame] | 66 | virtual void Enqueue(const GRWorkListUnit& U) { |
| 67 | Queue.push(U); |
| 68 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | 2c32773 | 2009-05-01 22:18:46 +0000 | [diff] [blame] | 70 | virtual GRWorkListUnit Dequeue() { |
| 71 | // Don't use const reference. The subsequent pop_back() might make it |
| 72 | // unsafe. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | GRWorkListUnit U = Queue.front(); |
Ted Kremenek | 2c32773 | 2009-05-01 22:18:46 +0000 | [diff] [blame] | 74 | Queue.pop(); |
| 75 | return U; |
| 76 | } |
| 77 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 79 | } // end anonymous namespace |
| 80 | |
Ted Kremenek | 2e12c2e | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 81 | // Place the dstor for GRWorkList here because it contains virtual member |
| 82 | // functions, and we the code for the dstor generated in one compilation unit. |
| 83 | GRWorkList::~GRWorkList() {} |
| 84 | |
Ted Kremenek | 2c32773 | 2009-05-01 22:18:46 +0000 | [diff] [blame] | 85 | GRWorkList *GRWorkList::MakeDFS() { return new DFS(); } |
| 86 | GRWorkList *GRWorkList::MakeBFS() { return new BFS(); } |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | d9de9f1 | 2008-12-16 22:13:33 +0000 | [diff] [blame] | 88 | namespace { |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 89 | class BFSBlockDFSContents : public GRWorkList { |
Ted Kremenek | d9de9f1 | 2008-12-16 22:13:33 +0000 | [diff] [blame] | 90 | std::queue<GRWorkListUnit> Queue; |
| 91 | llvm::SmallVector<GRWorkListUnit,20> Stack; |
| 92 | public: |
| 93 | virtual bool hasWork() const { |
| 94 | return !Queue.empty() || !Stack.empty(); |
| 95 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | d9de9f1 | 2008-12-16 22:13:33 +0000 | [diff] [blame] | 97 | virtual void Enqueue(const GRWorkListUnit& U) { |
| 98 | if (isa<BlockEntrance>(U.getNode()->getLocation())) |
| 99 | Queue.push(U); |
| 100 | else |
| 101 | Stack.push_back(U); |
| 102 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Ted Kremenek | d9de9f1 | 2008-12-16 22:13:33 +0000 | [diff] [blame] | 104 | virtual GRWorkListUnit Dequeue() { |
| 105 | // Process all basic blocks to completion. |
| 106 | if (!Stack.empty()) { |
| 107 | const GRWorkListUnit& U = Stack.back(); |
| 108 | Stack.pop_back(); // This technically "invalidates" U, but we are fine. |
| 109 | return U; |
| 110 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | d9de9f1 | 2008-12-16 22:13:33 +0000 | [diff] [blame] | 112 | assert(!Queue.empty()); |
| 113 | // Don't use const reference. The subsequent pop_back() might make it |
| 114 | // unsafe. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | GRWorkListUnit U = Queue.front(); |
Ted Kremenek | d9de9f1 | 2008-12-16 22:13:33 +0000 | [diff] [blame] | 116 | Queue.pop(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | return U; |
Ted Kremenek | d9de9f1 | 2008-12-16 22:13:33 +0000 | [diff] [blame] | 118 | } |
| 119 | }; |
| 120 | } // end anonymous namespace |
| 121 | |
| 122 | GRWorkList* GRWorkList::MakeBFSBlockDFSContents() { |
| 123 | return new BFSBlockDFSContents(); |
| 124 | } |
| 125 | |
| 126 | //===----------------------------------------------------------------------===// |
| 127 | // Core analysis engine. |
| 128 | //===----------------------------------------------------------------------===// |
Douglas Gregor | a2fbc94 | 2010-02-25 19:01:53 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 130 | /// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps. |
Zhongxing Xu | adf644d | 2010-07-22 13:52:13 +0000 | [diff] [blame] | 131 | bool GRCoreEngine::ExecuteWorkList(const LocationContext *L, unsigned Steps, |
| 132 | const GRState *InitState) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 134 | if (G->num_roots() == 0) { // Initialize the analysis by constructing |
| 135 | // the root if none exists. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Zhongxing Xu | edb77fe | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 137 | const CFGBlock* Entry = &(L->getCFG()->getEntry()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
| 139 | assert (Entry->empty() && |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 140 | "Entry block must be empty."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 142 | assert (Entry->succ_size() == 1 && |
| 143 | "Entry block must have 1 successor."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 145 | // Get the solitary successor. |
Zhongxing Xu | edb77fe | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 146 | const CFGBlock* Succ = *(Entry->succ_begin()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 148 | // Construct an edge representing the |
| 149 | // starting location in the function. |
Zhongxing Xu | e1190f7 | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 150 | BlockEdge StartLoc(Entry, Succ, L); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Ted Kremenek | 90ae68f | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 152 | // Set the current block counter to being empty. |
| 153 | WList->setBlockCounter(BCounterFactory.GetEmptyCounter()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Zhongxing Xu | adf644d | 2010-07-22 13:52:13 +0000 | [diff] [blame] | 155 | if (!InitState) |
| 156 | // Generate the root. |
| 157 | GenerateNode(StartLoc, getInitialState(L), 0); |
| 158 | else |
| 159 | GenerateNode(StartLoc, InitState, 0); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 160 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 162 | while (Steps && WList->hasWork()) { |
| 163 | --Steps; |
| 164 | const GRWorkListUnit& WU = WList->Dequeue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | 90ae68f | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 166 | // Set the current block counter. |
| 167 | WList->setBlockCounter(WU.getBlockCounter()); |
| 168 | |
| 169 | // Retrieve the node. |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 170 | ExplodedNode* Node = WU.getNode(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 172 | // Dispatch on the location type. |
| 173 | switch (Node->getLocation().getKind()) { |
Ted Kremenek | d9de9f1 | 2008-12-16 22:13:33 +0000 | [diff] [blame] | 174 | case ProgramPoint::BlockEdgeKind: |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 175 | HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node); |
| 176 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 178 | case ProgramPoint::BlockEntranceKind: |
| 179 | HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node); |
| 180 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 182 | case ProgramPoint::BlockExitKind: |
Ted Kremenek | e584359 | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 183 | assert (false && "BlockExit location never occur in forward analysis."); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 184 | break; |
Ted Kremenek | d9de9f1 | 2008-12-16 22:13:33 +0000 | [diff] [blame] | 185 | |
Douglas Gregor | a2fbc94 | 2010-02-25 19:01:53 +0000 | [diff] [blame] | 186 | case ProgramPoint::CallEnterKind: |
| 187 | HandleCallEnter(cast<CallEnter>(Node->getLocation()), WU.getBlock(), |
| 188 | WU.getIndex(), Node); |
| 189 | break; |
| 190 | |
| 191 | case ProgramPoint::CallExitKind: |
| 192 | HandleCallExit(cast<CallExit>(Node->getLocation()), Node); |
| 193 | break; |
| 194 | |
Ted Kremenek | d9de9f1 | 2008-12-16 22:13:33 +0000 | [diff] [blame] | 195 | default: |
| 196 | assert(isa<PostStmt>(Node->getLocation())); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 197 | HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(), |
| 198 | WU.getIndex(), Node); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | break; |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 2b4adff | 2010-08-11 00:03:02 +0000 | [diff] [blame] | 203 | SubEngine.ProcessEndWorklist(hasWorkRemaining()); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 204 | return WList->hasWork(); |
| 205 | } |
| 206 | |
Zhongxing Xu | adf644d | 2010-07-22 13:52:13 +0000 | [diff] [blame] | 207 | void GRCoreEngine::ExecuteWorkListWithInitialState(const LocationContext *L, |
| 208 | unsigned Steps, |
| 209 | const GRState *InitState, |
| 210 | ExplodedNodeSet &Dst) { |
| 211 | ExecuteWorkList(L, Steps, InitState); |
| 212 | for (llvm::SmallVectorImpl<ExplodedNode*>::iterator I = G->EndNodes.begin(), |
| 213 | E = G->EndNodes.end(); I != E; ++I) { |
| 214 | Dst.Add(*I); |
| 215 | } |
| 216 | } |
| 217 | |
Douglas Gregor | a2fbc94 | 2010-02-25 19:01:53 +0000 | [diff] [blame] | 218 | void GRCoreEngine::HandleCallEnter(const CallEnter &L, const CFGBlock *Block, |
| 219 | unsigned Index, ExplodedNode *Pred) { |
Zhongxing Xu | 84f65e0 | 2010-07-19 01:31:21 +0000 | [diff] [blame] | 220 | GRCallEnterNodeBuilder Builder(*this, Pred, L.getCallExpr(), |
| 221 | L.getCalleeContext(), Block, Index); |
Douglas Gregor | a2fbc94 | 2010-02-25 19:01:53 +0000 | [diff] [blame] | 222 | ProcessCallEnter(Builder); |
| 223 | } |
| 224 | |
| 225 | void GRCoreEngine::HandleCallExit(const CallExit &L, ExplodedNode *Pred) { |
| 226 | GRCallExitNodeBuilder Builder(*this, Pred); |
| 227 | ProcessCallExit(Builder); |
| 228 | } |
Zhongxing Xu | 82003da | 2009-08-06 10:00:15 +0000 | [diff] [blame] | 229 | |
| 230 | void GRCoreEngine::HandleBlockEdge(const BlockEdge& L, ExplodedNode* Pred) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | |
Zhongxing Xu | edb77fe | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 232 | const CFGBlock* Blk = L.getDst(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
| 234 | // Check if we are entering the EXIT block. |
Zhongxing Xu | d2ab38e | 2009-12-23 08:54:57 +0000 | [diff] [blame] | 235 | if (Blk == &(L.getLocationContext()->getCFG()->getExit())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Zhongxing Xu | d2ab38e | 2009-12-23 08:54:57 +0000 | [diff] [blame] | 237 | assert (L.getLocationContext()->getCFG()->getExit().size() == 0 |
Ted Kremenek | 997d872 | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 238 | && "EXIT block cannot contain Stmts."); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 239 | |
Ted Kremenek | 811c2b4 | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 240 | // Process the final state transition. |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 241 | GREndPathNodeBuilder Builder(Blk, Pred, this); |
Ted Kremenek | 811c2b4 | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 242 | ProcessEndPath(Builder); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 243 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 244 | // This path is done. Don't enqueue any more nodes. |
| 245 | return; |
| 246 | } |
Ted Kremenek | 17f4dbd | 2008-02-29 20:27:50 +0000 | [diff] [blame] | 247 | |
| 248 | // FIXME: Should we allow ProcessBlockEntrance to also manipulate state? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
Zhongxing Xu | 3c0c81a | 2010-03-23 05:05:02 +0000 | [diff] [blame] | 250 | if (ProcessBlockEntrance(Blk, Pred, WList->getBlockCounter())) |
Ted Kremenek | 090d62e | 2010-06-29 21:58:54 +0000 | [diff] [blame] | 251 | GenerateNode(BlockEntrance(Blk, Pred->getLocationContext()), |
| 252 | Pred->State, Pred); |
Ted Kremenek | 2b4adff | 2010-08-11 00:03:02 +0000 | [diff] [blame] | 253 | else { |
| 254 | blocksAborted.push_back(std::make_pair(L, Pred)); |
| 255 | } |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Zhongxing Xu | 82003da | 2009-08-06 10:00:15 +0000 | [diff] [blame] | 258 | void GRCoreEngine::HandleBlockEntrance(const BlockEntrance& L, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 259 | ExplodedNode* Pred) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | |
Ted Kremenek | 90ae68f | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 261 | // Increment the block counter. |
| 262 | GRBlockCounter Counter = WList->getBlockCounter(); |
Zhongxing Xu | 3c0c81a | 2010-03-23 05:05:02 +0000 | [diff] [blame] | 263 | Counter = BCounterFactory.IncrementCount(Counter, |
| 264 | Pred->getLocationContext()->getCurrentStackFrame(), |
| 265 | L.getBlock()->getBlockID()); |
Ted Kremenek | 90ae68f | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 266 | WList->setBlockCounter(Counter); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
| 268 | // Process the entrance of the block. |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 269 | if (CFGElement E = L.getFirstElement()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | GRStmtNodeBuilder Builder(L.getBlock(), 0, Pred, this, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 271 | SubEngine.getStateManager()); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 272 | ProcessStmt(E, Builder); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 273 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | else |
Ted Kremenek | e584359 | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 275 | HandleBlockExit(L.getBlock(), Pred); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Zhongxing Xu | edb77fe | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 278 | void GRCoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode* Pred) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Zhongxing Xu | edb77fe | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 280 | if (const Stmt* Term = B->getTerminator()) { |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 281 | switch (Term->getStmtClass()) { |
| 282 | default: |
| 283 | assert(false && "Analysis for this terminator not implemented."); |
| 284 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 286 | case Stmt::BinaryOperatorClass: // '&&' and '||' |
| 287 | HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred); |
| 288 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
Ted Kremenek | 3f2f1ad | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 290 | case Stmt::ConditionalOperatorClass: |
| 291 | HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 292 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 294 | // FIXME: Use constant-folding in CFG construction to simplify this |
| 295 | // case. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
Ted Kremenek | 3f2f1ad | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 297 | case Stmt::ChooseExprClass: |
| 298 | HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 299 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 301 | case Stmt::DoStmtClass: |
| 302 | HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred); |
| 303 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 305 | case Stmt::ForStmtClass: |
| 306 | HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred); |
| 307 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
Ted Kremenek | 632bcb8 | 2008-02-13 16:56:51 +0000 | [diff] [blame] | 309 | case Stmt::ContinueStmtClass: |
| 310 | case Stmt::BreakStmtClass: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | case Stmt::GotoStmtClass: |
Ted Kremenek | 3f2f1ad | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 312 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 314 | case Stmt::IfStmtClass: |
| 315 | HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 316 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 318 | case Stmt::IndirectGotoStmtClass: { |
| 319 | // Only 1 successor: the indirect goto dispatch block. |
| 320 | assert (B->succ_size() == 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 322 | GRIndirectGotoNodeBuilder |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 323 | builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(), |
| 324 | *(B->succ_begin()), this); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 326 | ProcessIndirectGoto(builder); |
| 327 | return; |
| 328 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
Ted Kremenek | 1781080 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 330 | case Stmt::ObjCForCollectionStmtClass: { |
| 331 | // In the case of ObjCForCollectionStmt, it appears twice in a CFG: |
| 332 | // |
| 333 | // (1) inside a basic block, which represents the binding of the |
| 334 | // 'element' variable to a value. |
| 335 | // (2) in a terminator, which represents the branch. |
| 336 | // |
| 337 | // For (1), subengines will bind a value (i.e., 0 or 1) indicating |
| 338 | // whether or not collection contains any more elements. We cannot |
| 339 | // just test to see if the element is nil because a container can |
| 340 | // contain nil elements. |
| 341 | HandleBranch(Term, Term, B, Pred); |
| 342 | return; |
| 343 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 345 | case Stmt::SwitchStmtClass: { |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 346 | GRSwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(), |
| 347 | this); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 349 | ProcessSwitch(builder); |
| 350 | return; |
| 351 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 353 | case Stmt::WhileStmtClass: |
| 354 | HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred); |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 355 | return; |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
Ted Kremenek | 822f737 | 2008-02-12 21:51:20 +0000 | [diff] [blame] | 358 | |
| 359 | assert (B->succ_size() == 1 && |
| 360 | "Blocks with no terminator should have at most 1 successor."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
| 362 | GenerateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()), |
Zhongxing Xu | e1190f7 | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 363 | Pred->State, Pred); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Zhongxing Xu | edb77fe | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 366 | void GRCoreEngine::HandleBranch(const Stmt* Cond, const Stmt* Term, |
| 367 | const CFGBlock * B, ExplodedNode* Pred) { |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 368 | assert (B->succ_size() == 2); |
| 369 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 370 | GRBranchNodeBuilder Builder(B, *(B->succ_begin()), *(B->succ_begin()+1), |
| 371 | Pred, this); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 373 | ProcessBranch(Cond, Term, Builder); |
| 374 | } |
| 375 | |
Zhongxing Xu | edb77fe | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 376 | void GRCoreEngine::HandlePostStmt(const PostStmt& L, const CFGBlock* B, |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 377 | unsigned StmtIdx, ExplodedNode* Pred) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 379 | assert (!B->empty()); |
| 380 | |
Ted Kremenek | e584359 | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 381 | if (StmtIdx == B->size()) |
| 382 | HandleBlockExit(B, Pred); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 383 | else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | GRStmtNodeBuilder Builder(B, StmtIdx, Pred, this, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 385 | SubEngine.getStateManager()); |
Ted Kremenek | e914bb8 | 2008-01-16 22:13:19 +0000 | [diff] [blame] | 386 | ProcessStmt((*B)[StmtIdx], Builder); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 390 | /// GenerateNode - Utility method to generate nodes, hook up successors, |
| 391 | /// and add nodes to the worklist. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | void GRCoreEngine::GenerateNode(const ProgramPoint& Loc, |
Zhongxing Xu | 82003da | 2009-08-06 10:00:15 +0000 | [diff] [blame] | 393 | const GRState* State, ExplodedNode* Pred) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 395 | bool IsNew; |
Zhongxing Xu | c90a0c2 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 396 | ExplodedNode* Node = G->getNode(Loc, State, &IsNew); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
| 398 | if (Pred) |
Ted Kremenek | c3661de | 2009-10-07 00:42:52 +0000 | [diff] [blame] | 399 | Node->addPredecessor(Pred, *G); // Link 'Node' with its predecessor. |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 400 | else { |
| 401 | assert (IsNew); |
| 402 | G->addRoot(Node); // 'Node' has no predecessor. Make it a root. |
| 403 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 405 | // Only add 'Node' to the worklist if it was freshly generated. |
Ted Kremenek | 90ae68f | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 406 | if (IsNew) WList->Enqueue(Node); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Zhongxing Xu | edb77fe | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 409 | GRStmtNodeBuilder::GRStmtNodeBuilder(const CFGBlock* b, unsigned idx, |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 410 | ExplodedNode* N, GRCoreEngine* e, |
| 411 | GRStateManager &mgr) |
Zhongxing Xu | d041bc6 | 2010-02-26 02:38:09 +0000 | [diff] [blame] | 412 | : Eng(*e), B(*b), Idx(idx), Pred(N), Mgr(mgr), Auditor(0), |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 413 | PurgingDeadSymbols(false), BuildSinks(false), HasGeneratedNode(false), |
| 414 | PointKind(ProgramPoint::PostStmtKind), Tag(0) { |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 415 | Deferred.insert(N); |
Zhongxing Xu | d041bc6 | 2010-02-26 02:38:09 +0000 | [diff] [blame] | 416 | CleanedState = Pred->getState(); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 419 | GRStmtNodeBuilder::~GRStmtNodeBuilder() { |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 420 | for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I) |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 421 | if (!(*I)->isSink()) |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 422 | GenerateAutoTransition(*I); |
| 423 | } |
| 424 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 425 | void GRStmtNodeBuilder::GenerateAutoTransition(ExplodedNode* N) { |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 426 | assert (!N->isSink()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Douglas Gregor | a2fbc94 | 2010-02-25 19:01:53 +0000 | [diff] [blame] | 428 | // Check if this node entered a callee. |
| 429 | if (isa<CallEnter>(N->getLocation())) { |
| 430 | // Still use the index of the CallExpr. It's needed to create the callee |
| 431 | // StackFrameContext. |
Zhongxing Xu | edb77fe | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 432 | Eng.WList->Enqueue(N, &B, Idx); |
Douglas Gregor | a2fbc94 | 2010-02-25 19:01:53 +0000 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | |
Zhongxing Xu | e1190f7 | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 436 | PostStmt Loc(getStmt(), N->getLocationContext()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 438 | if (Loc == N->getLocation()) { |
| 439 | // Note: 'N' should be a fresh node because otherwise it shouldn't be |
| 440 | // a member of Deferred. |
Zhongxing Xu | edb77fe | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 441 | Eng.WList->Enqueue(N, &B, Idx+1); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 442 | return; |
| 443 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 445 | bool IsNew; |
Zhongxing Xu | c90a0c2 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 446 | ExplodedNode* Succ = Eng.G->getNode(Loc, N->State, &IsNew); |
Ted Kremenek | c3661de | 2009-10-07 00:42:52 +0000 | [diff] [blame] | 447 | Succ->addPredecessor(N, *Eng.G); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 448 | |
| 449 | if (IsNew) |
Zhongxing Xu | edb77fe | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 450 | Eng.WList->Enqueue(Succ, &B, Idx+1); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Zhongxing Xu | c2acbe0 | 2010-07-20 02:41:28 +0000 | [diff] [blame] | 453 | ExplodedNode* GRStmtNodeBuilder::MakeNode(ExplodedNodeSet& Dst, const Stmt* S, |
Zhongxing Xu | 5eb08f7 | 2010-04-14 06:35:09 +0000 | [diff] [blame] | 454 | ExplodedNode* Pred, const GRState* St, |
| 455 | ProgramPoint::Kind K) { |
| 456 | const GRState* PredState = GetState(Pred); |
| 457 | |
| 458 | // If the state hasn't changed, don't generate a new node. |
| 459 | if (!BuildSinks && St == PredState && Auditor == 0) { |
| 460 | Dst.Add(Pred); |
| 461 | return NULL; |
| 462 | } |
| 463 | |
| 464 | ExplodedNode* N = generateNode(S, St, Pred, K); |
| 465 | |
| 466 | if (N) { |
| 467 | if (BuildSinks) |
| 468 | N->markAsSink(); |
| 469 | else { |
| 470 | if (Auditor && Auditor->Audit(N, Mgr)) |
| 471 | N->markAsSink(); |
| 472 | |
| 473 | Dst.Add(N); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | return N; |
| 478 | } |
| 479 | |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 480 | static ProgramPoint GetProgramPoint(const Stmt *S, ProgramPoint::Kind K, |
| 481 | const LocationContext *LC, const void *tag){ |
Ted Kremenek | 9a935fb | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 482 | switch (K) { |
| 483 | default: |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 484 | assert(false && "Unhandled ProgramPoint kind"); |
| 485 | case ProgramPoint::PreStmtKind: |
| 486 | return PreStmt(S, LC, tag); |
Ted Kremenek | 9a935fb | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 487 | case ProgramPoint::PostStmtKind: |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 488 | return PostStmt(S, LC, tag); |
| 489 | case ProgramPoint::PreLoadKind: |
| 490 | return PreLoad(S, LC, tag); |
Ted Kremenek | 9a935fb | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 491 | case ProgramPoint::PostLoadKind: |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 492 | return PostLoad(S, LC, tag); |
| 493 | case ProgramPoint::PreStoreKind: |
| 494 | return PreStore(S, LC, tag); |
Ted Kremenek | a196618 | 2008-10-17 20:49:23 +0000 | [diff] [blame] | 495 | case ProgramPoint::PostStoreKind: |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 496 | return PostStore(S, LC, tag); |
Ted Kremenek | a6e0832 | 2009-05-07 18:27:16 +0000 | [diff] [blame] | 497 | case ProgramPoint::PostLValueKind: |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 498 | return PostLValue(S, LC, tag); |
Ted Kremenek | 9a935fb | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 499 | case ProgramPoint::PostPurgeDeadSymbolsKind: |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 500 | return PostPurgeDeadSymbols(S, LC, tag); |
Ted Kremenek | 9a935fb | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 504 | ExplodedNode* |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 505 | GRStmtNodeBuilder::generateNodeInternal(const Stmt* S, const GRState* state, |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 506 | ExplodedNode* Pred, |
Ted Kremenek | df24000 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 507 | ProgramPoint::Kind K, |
| 508 | const void *tag) { |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 509 | |
Zhongxing Xu | d2ab38e | 2009-12-23 08:54:57 +0000 | [diff] [blame] | 510 | const ProgramPoint &L = GetProgramPoint(S, K, Pred->getLocationContext(),tag); |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 511 | return generateNodeInternal(L, state, Pred); |
Ted Kremenek | 513f0b1 | 2009-02-19 23:45:28 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 514 | ExplodedNode* |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 515 | GRStmtNodeBuilder::generateNodeInternal(const ProgramPoint &Loc, |
Zhongxing Xu | c90a0c2 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 516 | const GRState* State, |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 517 | ExplodedNode* Pred) { |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 518 | bool IsNew; |
Zhongxing Xu | c90a0c2 | 2009-08-06 06:28:40 +0000 | [diff] [blame] | 519 | ExplodedNode* N = Eng.G->getNode(Loc, State, &IsNew); |
Ted Kremenek | c3661de | 2009-10-07 00:42:52 +0000 | [diff] [blame] | 520 | N->addPredecessor(Pred, *Eng.G); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 521 | Deferred.erase(Pred); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 523 | if (IsNew) { |
| 524 | Deferred.insert(N); |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 525 | return N; |
| 526 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | return NULL; |
Ted Kremenek | 3e74366 | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 529 | } |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 530 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 531 | ExplodedNode* GRBranchNodeBuilder::generateNode(const GRState* State, |
| 532 | bool branch) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | |
Ted Kremenek | af9f362 | 2009-07-20 18:44:36 +0000 | [diff] [blame] | 534 | // If the branch has been marked infeasible we should not generate a node. |
| 535 | if (!isFeasible(branch)) |
| 536 | return NULL; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 538 | bool IsNew; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 540 | ExplodedNode* Succ = |
Zhongxing Xu | e1190f7 | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 541 | Eng.G->getNode(BlockEdge(Src,branch ? DstT:DstF,Pred->getLocationContext()), |
| 542 | State, &IsNew); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | |
Ted Kremenek | c3661de | 2009-10-07 00:42:52 +0000 | [diff] [blame] | 544 | Succ->addPredecessor(Pred, *Eng.G); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Ted Kremenek | af9f362 | 2009-07-20 18:44:36 +0000 | [diff] [blame] | 546 | if (branch) |
| 547 | GeneratedTrue = true; |
| 548 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | GeneratedFalse = true; |
| 550 | |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 551 | if (IsNew) { |
Ted Kremenek | 2531fce | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 552 | Deferred.push_back(Succ); |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 553 | return Succ; |
| 554 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | |
Ted Kremenek | a50d985 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 556 | return NULL; |
Ted Kremenek | 9b4211d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 557 | } |
Ted Kremenek | 7ff1893 | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 558 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 559 | GRBranchNodeBuilder::~GRBranchNodeBuilder() { |
| 560 | if (!GeneratedTrue) generateNode(Pred->State, true); |
| 561 | if (!GeneratedFalse) generateNode(Pred->State, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Ted Kremenek | 2531fce | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 563 | for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I) |
Ted Kremenek | 90ae68f | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 564 | if (!(*I)->isSink()) Eng.WList->Enqueue(*I); |
Ted Kremenek | 7ff1893 | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 565 | } |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 566 | |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 567 | |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 568 | ExplodedNode* |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 569 | GRIndirectGotoNodeBuilder::generateNode(const iterator& I, const GRState* St, |
| 570 | bool isSink) { |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 571 | bool IsNew; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | |
| 573 | ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(), |
Zhongxing Xu | e1190f7 | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 574 | Pred->getLocationContext()), St, &IsNew); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | |
Ted Kremenek | c3661de | 2009-10-07 00:42:52 +0000 | [diff] [blame] | 576 | Succ->addPredecessor(Pred, *Eng.G); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 578 | if (IsNew) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 580 | if (isSink) |
| 581 | Succ->markAsSink(); |
| 582 | else |
| 583 | Eng.WList->Enqueue(Succ); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 585 | return Succ; |
| 586 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | |
Ted Kremenek | 7022efb | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 588 | return NULL; |
| 589 | } |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 590 | |
| 591 | |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 592 | ExplodedNode* |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 593 | GRSwitchNodeBuilder::generateCaseStmtNode(const iterator& I, const GRState* St){ |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 594 | |
| 595 | bool IsNew; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | |
Zhongxing Xu | e1190f7 | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 597 | ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(), |
| 598 | Pred->getLocationContext()), St, &IsNew); |
Ted Kremenek | c3661de | 2009-10-07 00:42:52 +0000 | [diff] [blame] | 599 | Succ->addPredecessor(Pred, *Eng.G); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 601 | if (IsNew) { |
| 602 | Eng.WList->Enqueue(Succ); |
| 603 | return Succ; |
| 604 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 606 | return NULL; |
| 607 | } |
| 608 | |
| 609 | |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 610 | ExplodedNode* |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 611 | GRSwitchNodeBuilder::generateDefaultCaseNode(const GRState* St, bool isSink) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 613 | // Get the block for the default case. |
| 614 | assert (Src->succ_rbegin() != Src->succ_rend()); |
| 615 | CFGBlock* DefaultBlock = *Src->succ_rbegin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 616 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 617 | bool IsNew; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 618 | |
Zhongxing Xu | e1190f7 | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 619 | ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock, |
| 620 | Pred->getLocationContext()), St, &IsNew); |
Ted Kremenek | c3661de | 2009-10-07 00:42:52 +0000 | [diff] [blame] | 621 | Succ->addPredecessor(Pred, *Eng.G); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 623 | if (IsNew) { |
| 624 | if (isSink) |
| 625 | Succ->markAsSink(); |
| 626 | else |
| 627 | Eng.WList->Enqueue(Succ); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 629 | return Succ; |
| 630 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
Ted Kremenek | 80ebc1d | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 632 | return NULL; |
| 633 | } |
Ted Kremenek | 811c2b4 | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 634 | |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 635 | GREndPathNodeBuilder::~GREndPathNodeBuilder() { |
Ted Kremenek | 811c2b4 | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 636 | // Auto-generate an EOP node if one has not been generated. |
Douglas Gregor | a2fbc94 | 2010-02-25 19:01:53 +0000 | [diff] [blame] | 637 | if (!HasGeneratedNode) { |
| 638 | // If we are in an inlined call, generate CallExit node. |
| 639 | if (Pred->getLocationContext()->getParent()) |
| 640 | GenerateCallExitNode(Pred->State); |
| 641 | else |
| 642 | generateNode(Pred->State); |
| 643 | } |
Ted Kremenek | 811c2b4 | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Zhongxing Xu | 20227f7 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 646 | ExplodedNode* |
Zhongxing Xu | 107f759 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 647 | GREndPathNodeBuilder::generateNode(const GRState* State, const void *tag, |
| 648 | ExplodedNode* P) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | HasGeneratedNode = true; |
Ted Kremenek | 811c2b4 | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 650 | bool IsNew; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 651 | |
| 652 | ExplodedNode* Node = Eng.G->getNode(BlockEntrance(&B, |
Zhongxing Xu | e1190f7 | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 653 | Pred->getLocationContext(), tag), State, &IsNew); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | |
Ted Kremenek | c3661de | 2009-10-07 00:42:52 +0000 | [diff] [blame] | 655 | Node->addPredecessor(P ? P : Pred, *Eng.G); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | |
Ted Kremenek | 811c2b4 | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 657 | if (IsNew) { |
Ted Kremenek | 811c2b4 | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 658 | Eng.G->addEndOfPath(Node); |
Ted Kremenek | d004c41 | 2008-04-18 16:30:14 +0000 | [diff] [blame] | 659 | return Node; |
Ted Kremenek | 811c2b4 | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 660 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | |
Ted Kremenek | d004c41 | 2008-04-18 16:30:14 +0000 | [diff] [blame] | 662 | return NULL; |
Ted Kremenek | 811c2b4 | 2008-04-11 22:03:04 +0000 | [diff] [blame] | 663 | } |
Douglas Gregor | a2fbc94 | 2010-02-25 19:01:53 +0000 | [diff] [blame] | 664 | |
| 665 | void GREndPathNodeBuilder::GenerateCallExitNode(const GRState *state) { |
| 666 | HasGeneratedNode = true; |
| 667 | // Create a CallExit node and enqueue it. |
| 668 | const StackFrameContext *LocCtx |
| 669 | = cast<StackFrameContext>(Pred->getLocationContext()); |
| 670 | const Stmt *CE = LocCtx->getCallSite(); |
| 671 | |
| 672 | // Use the the callee location context. |
| 673 | CallExit Loc(CE, LocCtx); |
| 674 | |
| 675 | bool isNew; |
| 676 | ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew); |
| 677 | Node->addPredecessor(Pred, *Eng.G); |
| 678 | |
| 679 | if (isNew) |
| 680 | Eng.WList->Enqueue(Node); |
| 681 | } |
| 682 | |
| 683 | |
| 684 | void GRCallEnterNodeBuilder::GenerateNode(const GRState *state, |
| 685 | const LocationContext *LocCtx) { |
Zhongxing Xu | 84f65e0 | 2010-07-19 01:31:21 +0000 | [diff] [blame] | 686 | // Check if the callee is in the same translation unit. |
| 687 | if (CalleeCtx->getTranslationUnit() != |
| 688 | Pred->getLocationContext()->getTranslationUnit()) { |
Zhongxing Xu | adf644d | 2010-07-22 13:52:13 +0000 | [diff] [blame] | 689 | // Create a new engine. We must be careful that the new engine should not |
| 690 | // reference data structures owned by the old engine. |
| 691 | |
| 692 | AnalysisManager &OldMgr = Eng.SubEngine.getAnalysisManager(); |
| 693 | |
| 694 | // Get the callee's translation unit. |
| 695 | idx::TranslationUnit *TU = CalleeCtx->getTranslationUnit(); |
| 696 | |
| 697 | // Create a new AnalysisManager with components of the callee's |
| 698 | // TranslationUnit. |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame^] | 699 | // The Diagnostic is actually shared when we create ASTUnits from AST files. |
Zhongxing Xu | adf644d | 2010-07-22 13:52:13 +0000 | [diff] [blame] | 700 | AnalysisManager AMgr(TU->getASTContext(), TU->getDiagnostic(), |
| 701 | OldMgr.getLangOptions(), |
| 702 | OldMgr.getPathDiagnosticClient(), |
| 703 | OldMgr.getStoreManagerCreator(), |
| 704 | OldMgr.getConstraintManagerCreator(), |
| 705 | OldMgr.getIndexer(), |
| 706 | OldMgr.getMaxNodes(), OldMgr.getMaxLoop(), |
| 707 | OldMgr.shouldVisualizeGraphviz(), |
| 708 | OldMgr.shouldVisualizeUbigraph(), |
| 709 | OldMgr.shouldPurgeDead(), |
| 710 | OldMgr.shouldEagerlyAssume(), |
| 711 | OldMgr.shouldTrimGraph(), |
Ted Kremenek | 4a2b237 | 2010-08-03 00:09:51 +0000 | [diff] [blame] | 712 | OldMgr.shouldInlineCall(), |
| 713 | OldMgr.getAnalysisContextManager().getUseUnoptimizedCFG()); |
Zhongxing Xu | adf644d | 2010-07-22 13:52:13 +0000 | [diff] [blame] | 714 | llvm::OwningPtr<GRTransferFuncs> TF(MakeCFRefCountTF(AMgr.getASTContext(), |
| 715 | /* GCEnabled */ false, |
| 716 | AMgr.getLangOptions())); |
| 717 | // Create the new engine. |
| 718 | GRExprEngine NewEng(AMgr, TF.take()); |
| 719 | |
| 720 | // Create the new LocationContext. |
| 721 | AnalysisContext *NewAnaCtx = AMgr.getAnalysisContext(CalleeCtx->getDecl(), |
| 722 | CalleeCtx->getTranslationUnit()); |
| 723 | const StackFrameContext *OldLocCtx = cast<StackFrameContext>(LocCtx); |
| 724 | const StackFrameContext *NewLocCtx = AMgr.getStackFrame(NewAnaCtx, |
| 725 | OldLocCtx->getParent(), |
| 726 | OldLocCtx->getCallSite(), |
| 727 | OldLocCtx->getCallSiteBlock(), |
| 728 | OldLocCtx->getIndex()); |
| 729 | |
| 730 | // Now create an initial state for the new engine. |
| 731 | const GRState *NewState = NewEng.getStateManager().MarshalState(state, |
| 732 | NewLocCtx); |
| 733 | ExplodedNodeSet ReturnNodes; |
| 734 | NewEng.ExecuteWorkListWithInitialState(NewLocCtx, AMgr.getMaxNodes(), |
| 735 | NewState, ReturnNodes); |
| 736 | return; |
Zhongxing Xu | 84f65e0 | 2010-07-19 01:31:21 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Douglas Gregor | a2fbc94 | 2010-02-25 19:01:53 +0000 | [diff] [blame] | 739 | // Get the callee entry block. |
| 740 | const CFGBlock *Entry = &(LocCtx->getCFG()->getEntry()); |
| 741 | assert(Entry->empty()); |
| 742 | assert(Entry->succ_size() == 1); |
| 743 | |
| 744 | // Get the solitary successor. |
| 745 | const CFGBlock *SuccB = *(Entry->succ_begin()); |
| 746 | |
| 747 | // Construct an edge representing the starting location in the callee. |
| 748 | BlockEdge Loc(Entry, SuccB, LocCtx); |
| 749 | |
| 750 | bool isNew; |
| 751 | ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew); |
| 752 | Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G); |
| 753 | |
| 754 | if (isNew) |
| 755 | Eng.WList->Enqueue(Node); |
| 756 | } |
| 757 | |
| 758 | void GRCallExitNodeBuilder::GenerateNode(const GRState *state) { |
| 759 | // Get the callee's location context. |
| 760 | const StackFrameContext *LocCtx |
| 761 | = cast<StackFrameContext>(Pred->getLocationContext()); |
| 762 | |
| 763 | PostStmt Loc(LocCtx->getCallSite(), LocCtx->getParent()); |
| 764 | bool isNew; |
| 765 | ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew); |
| 766 | Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G); |
| 767 | if (isNew) |
Zhongxing Xu | edb77fe | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 768 | Eng.WList->Enqueue(Node, LocCtx->getCallSiteBlock(), |
Douglas Gregor | a2fbc94 | 2010-02-25 19:01:53 +0000 | [diff] [blame] | 769 | LocCtx->getIndex() + 1); |
| 770 | } |