Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1 | //===-- GRExprEngine.cpp - Simple, Path-Sens. Constant Prop. -----*- C++ -*-==// |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 2 | // |
Ted Kremenek | 4af8431 | 2008-01-31 06:49:09 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Constant Propagation via Graph Reachability |
| 11 | // |
| 12 | // This files defines a simple analysis that performs path-sensitive |
| 13 | // constant propagation within a function. An example use of this analysis |
| 14 | // is to perform simple checks for NULL dereferences. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 18 | #include "ValueState.h" |
| 19 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/PathSensitive/GRCoreEngine.h" |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
| 22 | #include "GRSimpleVals.h" |
| 23 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 25 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 26 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 27 | #include "clang/Basic/Diagnostic.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 28 | |
| 29 | #include "llvm/Support/Casting.h" |
| 30 | #include "llvm/Support/DataTypes.h" |
| 31 | #include "llvm/ADT/APSInt.h" |
| 32 | #include "llvm/ADT/FoldingSet.h" |
| 33 | #include "llvm/ADT/ImmutableMap.h" |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Allocator.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Streams.h" |
| 39 | |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 40 | #include <functional> |
| 41 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 42 | #ifndef NDEBUG |
| 43 | #include "llvm/Support/GraphWriter.h" |
| 44 | #include <sstream> |
| 45 | #endif |
| 46 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 47 | using namespace clang; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 48 | using llvm::dyn_cast; |
| 49 | using llvm::cast; |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 50 | using llvm::APSInt; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 51 | |
| 52 | //===----------------------------------------------------------------------===// |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 53 | // The Checker. |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 54 | // |
| 55 | // FIXME: This checker logic should be eventually broken into two components. |
| 56 | // The first is the "meta"-level checking logic; the code that |
| 57 | // does the Stmt visitation, fetching values from the map, etc. |
| 58 | // The second part does the actual state manipulation. This way we |
| 59 | // get more of a separate of concerns of these two pieces, with the |
| 60 | // latter potentially being refactored back into the main checking |
| 61 | // logic. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 62 | //===----------------------------------------------------------------------===// |
| 63 | |
| 64 | namespace { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 66 | class VISIBILITY_HIDDEN GRExprEngine { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 67 | |
| 68 | public: |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 69 | typedef ValueStateManager::StateTy StateTy; |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 70 | typedef ExplodedGraph<GRExprEngine> GraphTy; |
| 71 | typedef GraphTy::NodeTy NodeTy; |
| 72 | |
| 73 | // Builders. |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 74 | typedef GRStmtNodeBuilder<GRExprEngine> StmtNodeBuilder; |
| 75 | typedef GRBranchNodeBuilder<GRExprEngine> BranchNodeBuilder; |
| 76 | typedef GRIndirectGotoNodeBuilder<GRExprEngine> IndirectGotoNodeBuilder; |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 77 | typedef GRSwitchNodeBuilder<GRExprEngine> SwitchNodeBuilder; |
| 78 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 79 | class NodeSet { |
| 80 | typedef llvm::SmallVector<NodeTy*,3> ImplTy; |
| 81 | ImplTy Impl; |
| 82 | public: |
| 83 | |
| 84 | NodeSet() {} |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 85 | NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 87 | void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 88 | |
| 89 | typedef ImplTy::iterator iterator; |
| 90 | typedef ImplTy::const_iterator const_iterator; |
| 91 | |
| 92 | unsigned size() const { return Impl.size(); } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 93 | bool empty() const { return Impl.empty(); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 94 | |
| 95 | iterator begin() { return Impl.begin(); } |
| 96 | iterator end() { return Impl.end(); } |
| 97 | |
| 98 | const_iterator begin() const { return Impl.begin(); } |
| 99 | const_iterator end() const { return Impl.end(); } |
| 100 | }; |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 102 | protected: |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 103 | /// G - the simulation graph. |
| 104 | GraphTy& G; |
| 105 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 106 | /// Liveness - live-variables information the ValueDecl* and block-level |
| 107 | /// Expr* in the CFG. Used to prune out dead state. |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 108 | LiveVariables Liveness; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 110 | /// Builder - The current GRStmtNodeBuilder which is used when building the |
| 111 | /// nodes for a given statement. |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 112 | StmtNodeBuilder* Builder; |
| 113 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 114 | /// StateMgr - Object that manages the data for all created states. |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 115 | ValueStateManager StateMgr; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 116 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 117 | /// ValueMgr - Object that manages the data for all created RValues. |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 118 | ValueManager& ValMgr; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 119 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 120 | /// TF - Object that represents a bundle of transfer functions |
| 121 | /// for manipulating and creating RValues. |
| 122 | GRTransferFuncs& TF; |
| 123 | |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 124 | /// SymMgr - Object that manages the symbol information. |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 125 | SymbolManager& SymMgr; |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 127 | /// StmtEntryNode - The immediate predecessor node. |
| 128 | NodeTy* StmtEntryNode; |
| 129 | |
| 130 | /// CurrentStmt - The current block-level statement. |
| 131 | Stmt* CurrentStmt; |
| 132 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 133 | /// UninitBranches - Nodes in the ExplodedGraph that result from |
| 134 | /// taking a branch based on an uninitialized value. |
| 135 | typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy; |
| 136 | UninitBranchesTy UninitBranches; |
| 137 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 138 | /// ImplicitNullDeref - Nodes in the ExplodedGraph that result from |
| 139 | /// taking a dereference on a symbolic pointer that may be NULL. |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 140 | typedef llvm::SmallPtrSet<NodeTy*,5> NullDerefTy; |
| 141 | NullDerefTy ImplicitNullDeref; |
| 142 | NullDerefTy ExplicitNullDeref; |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 143 | |
| 144 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 145 | bool StateCleaned; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 147 | public: |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 148 | GRExprEngine(GraphTy& g) : |
| 149 | G(g), Liveness(G.getCFG(), G.getFunctionDecl()), |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 150 | Builder(NULL), |
Ted Kremenek | 768ad16 | 2008-02-05 05:15:51 +0000 | [diff] [blame] | 151 | StateMgr(G.getContext(), G.getAllocator()), |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 152 | ValMgr(StateMgr.getValueManager()), |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 153 | TF(*(new GRSimpleVals())), // FIXME. |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 154 | SymMgr(StateMgr.getSymbolManager()), |
| 155 | StmtEntryNode(NULL), CurrentStmt(NULL) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 156 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 157 | // Compute liveness information. |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 158 | Liveness.runOnCFG(G.getCFG()); |
| 159 | Liveness.runOnAllBlocks(G.getCFG(), NULL, true); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 160 | } |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 162 | /// getContext - Return the ASTContext associated with this analysis. |
| 163 | ASTContext& getContext() const { return G.getContext(); } |
| 164 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 165 | /// getCFG - Returns the CFG associated with this analysis. |
| 166 | CFG& getCFG() { return G.getCFG(); } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 167 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 168 | /// getInitialState - Return the initial state used for the root vertex |
| 169 | /// in the ExplodedGraph. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 170 | StateTy getInitialState() { |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 171 | StateTy St = StateMgr.getInitialState(); |
Ted Kremenek | ff6e3c5 | 2008-01-29 00:43:03 +0000 | [diff] [blame] | 172 | |
| 173 | // Iterate the parameters. |
| 174 | FunctionDecl& F = G.getFunctionDecl(); |
| 175 | |
| 176 | for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end(); |
Ted Kremenek | 4150abf | 2008-01-31 00:09:56 +0000 | [diff] [blame] | 177 | I!=E; ++I) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 178 | St = SetValue(St, lval::DeclVal(*I), RValue::GetSymbolValue(SymMgr, *I)); |
Ted Kremenek | ff6e3c5 | 2008-01-29 00:43:03 +0000 | [diff] [blame] | 179 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 180 | return St; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 181 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 182 | |
| 183 | bool isUninitControlFlow(const NodeTy* N) const { |
| 184 | return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0; |
| 185 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 186 | |
| 187 | bool isImplicitNullDeref(const NodeTy* N) const { |
| 188 | return N->isSink() && ImplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0; |
| 189 | } |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 190 | |
| 191 | bool isExplicitNullDeref(const NodeTy* N) const { |
| 192 | return N->isSink() && ExplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0; |
| 193 | } |
| 194 | |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 195 | typedef NullDerefTy::iterator null_iterator; |
| 196 | null_iterator null_begin() { return ExplicitNullDeref.begin(); } |
| 197 | null_iterator null_end() { return ExplicitNullDeref.end(); } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 199 | /// ProcessStmt - Called by GRCoreEngine. Used to generate new successor |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 200 | /// nodes by processing the 'effects' of a block-level statement. |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 201 | void ProcessStmt(Stmt* S, StmtNodeBuilder& builder); |
| 202 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 203 | /// ProcessBranch - Called by GRCoreEngine. Used to generate successor |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 204 | /// nodes by processing the 'effects' of a branch condition. |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 205 | void ProcessBranch(Expr* Condition, Stmt* Term, BranchNodeBuilder& builder); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 206 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 207 | /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 208 | /// nodes by processing the 'effects' of a computed goto jump. |
| 209 | void ProcessIndirectGoto(IndirectGotoNodeBuilder& builder); |
| 210 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 211 | /// ProcessSwitch - Called by GRCoreEngine. Used to generate successor |
| 212 | /// nodes by processing the 'effects' of a switch statement. |
| 213 | void ProcessSwitch(SwitchNodeBuilder& builder); |
| 214 | |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 215 | /// RemoveDeadBindings - Return a new state that is the same as 'St' except |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 216 | /// that all subexpression mappings are removed and that any |
| 217 | /// block-level expressions that are not live at 'S' also have their |
| 218 | /// mappings removed. |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 219 | inline StateTy RemoveDeadBindings(Stmt* S, StateTy St) { |
| 220 | return StateMgr.RemoveDeadBindings(St, S, Liveness); |
| 221 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 222 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 223 | StateTy SetValue(StateTy St, Expr* S, const RValue& V); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 224 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 225 | StateTy SetValue(StateTy St, const Expr* S, const RValue& V) { |
| 226 | return SetValue(St, const_cast<Expr*>(S), V); |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 229 | /// SetValue - This version of SetValue is used to batch process a set |
| 230 | /// of different possible RValues and return a set of different states. |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 231 | const StateTy::BufferTy& SetValue(StateTy St, Expr* S, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 232 | const RValue::BufferTy& V, |
| 233 | StateTy::BufferTy& RetBuf); |
| 234 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 235 | StateTy SetValue(StateTy St, const LValue& LV, const RValue& V); |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 237 | inline RValue GetValue(const StateTy& St, Expr* S) { |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 238 | return StateMgr.GetValue(St, S); |
| 239 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 240 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 241 | inline RValue GetValue(const StateTy& St, Expr* S, bool& hasVal) { |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 242 | return StateMgr.GetValue(St, S, &hasVal); |
| 243 | } |
| 244 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 245 | inline RValue GetValue(const StateTy& St, const Expr* S) { |
| 246 | return GetValue(St, const_cast<Expr*>(S)); |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 249 | inline RValue GetValue(const StateTy& St, const LValue& LV, |
| 250 | QualType* T = NULL) { |
| 251 | |
| 252 | return StateMgr.GetValue(St, LV, T); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 255 | inline LValue GetLValue(const StateTy& St, Expr* S) { |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 256 | return StateMgr.GetLValue(St, S); |
| 257 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 258 | |
| 259 | inline NonLValue GetRValueConstant(uint64_t X, Expr* E) { |
| 260 | return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart()); |
| 261 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 262 | |
| 263 | /// Assume - Create new state by assuming that a given expression |
| 264 | /// is true or false. |
| 265 | inline StateTy Assume(StateTy St, RValue Cond, bool Assumption, |
| 266 | bool& isFeasible) { |
| 267 | if (isa<LValue>(Cond)) |
| 268 | return Assume(St, cast<LValue>(Cond), Assumption, isFeasible); |
| 269 | else |
| 270 | return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible); |
| 271 | } |
| 272 | |
| 273 | StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible); |
| 274 | StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 275 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 276 | StateTy AssumeSymNE(StateTy St, SymbolID sym, const llvm::APSInt& V, |
| 277 | bool& isFeasible); |
| 278 | |
| 279 | StateTy AssumeSymEQ(StateTy St, SymbolID sym, const llvm::APSInt& V, |
| 280 | bool& isFeasible); |
| 281 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 282 | StateTy AssumeSymInt(StateTy St, bool Assumption, const SymIntConstraint& C, |
| 283 | bool& isFeasible); |
| 284 | |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 285 | NodeTy* Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 287 | /// Nodify - This version of Nodify is used to batch process a set of states. |
| 288 | /// The states are not guaranteed to be unique. |
| 289 | void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB); |
| 290 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 291 | /// Visit - Transfer function logic for all statements. Dispatches to |
| 292 | /// other functions that handle specific kinds of statements. |
| 293 | void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 295 | /// VisitBinaryOperator - Transfer function logic for binary operators. |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 296 | void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
| 297 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 298 | void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 299 | |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 300 | /// VisitCast - Transfer function logic for all casts (implicit and explicit). |
| 301 | void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst); |
| 302 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 303 | /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs. |
| 304 | void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst); |
| 305 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 306 | /// VisitDeclStmt - Transfer function logic for DeclStmts. |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 307 | void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst); |
| 308 | |
| 309 | /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 310 | void VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS, |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 311 | NodeTy* Pred, NodeSet& Dst); |
| 312 | |
| 313 | /// VisitLogicalExpr - Transfer function logic for '&&', '||' |
| 314 | void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 315 | |
| 316 | /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type). |
| 317 | void VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, NodeTy* Pred, |
| 318 | NodeSet& Dst); |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 319 | |
| 320 | /// VisitUnaryOperator - Transfer function logic for unary operators. |
| 321 | void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
| 322 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 323 | |
| 324 | inline RValue EvalCast(ValueManager& ValMgr, RValue R, Expr* CastExpr) { |
| 325 | return TF.EvalCast(ValMgr, R, CastExpr); |
| 326 | } |
| 327 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 328 | inline NonLValue EvalMinus(ValueManager& ValMgr, UnaryOperator* U, |
| 329 | NonLValue X) { |
| 330 | return TF.EvalMinus(ValMgr, U, X); |
| 331 | } |
| 332 | |
| 333 | inline NonLValue EvalComplement(ValueManager& ValMgr, NonLValue X) { |
| 334 | return TF.EvalComplement(ValMgr, X); |
| 335 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 336 | }; |
| 337 | } // end anonymous namespace |
| 338 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 339 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 340 | GRExprEngine::StateTy |
| 341 | GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& V) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 342 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 343 | if (!StateCleaned) { |
| 344 | St = RemoveDeadBindings(CurrentStmt, St); |
| 345 | StateCleaned = true; |
| 346 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 347 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 348 | bool isBlkExpr = false; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 349 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 350 | if (S == CurrentStmt) { |
| 351 | isBlkExpr = getCFG().isBlkExpr(S); |
| 352 | |
| 353 | if (!isBlkExpr) |
| 354 | return St; |
| 355 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 356 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 357 | return StateMgr.SetValue(St, S, isBlkExpr, V); |
| 358 | } |
| 359 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 360 | const GRExprEngine::StateTy::BufferTy& |
| 361 | GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 362 | StateTy::BufferTy& RetBuf) { |
| 363 | |
| 364 | assert (RetBuf.empty()); |
| 365 | |
| 366 | for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I) |
| 367 | RetBuf.push_back(SetValue(St, S, *I)); |
| 368 | |
| 369 | return RetBuf; |
| 370 | } |
| 371 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 372 | GRExprEngine::StateTy |
| 373 | GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) { |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 374 | |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 375 | if (LV.isUnknown()) |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 376 | return St; |
| 377 | |
| 378 | if (!StateCleaned) { |
| 379 | St = RemoveDeadBindings(CurrentStmt, St); |
| 380 | StateCleaned = true; |
| 381 | } |
| 382 | |
| 383 | return StateMgr.SetValue(St, LV, V); |
| 384 | } |
| 385 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 386 | void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term, |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 387 | BranchNodeBuilder& builder) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 388 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 389 | // Remove old bindings for subexpressions. |
| 390 | StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState()); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 391 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 392 | RValue V = GetValue(PrevState, Condition); |
| 393 | |
| 394 | switch (V.getBaseKind()) { |
| 395 | default: |
| 396 | break; |
| 397 | |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 398 | case RValue::UnknownKind: |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 399 | builder.generateNode(PrevState, true); |
| 400 | builder.generateNode(PrevState, false); |
| 401 | return; |
| 402 | |
| 403 | case RValue::UninitializedKind: { |
| 404 | NodeTy* N = builder.generateNode(PrevState, true); |
| 405 | |
| 406 | if (N) { |
| 407 | N->markAsSink(); |
| 408 | UninitBranches.insert(N); |
| 409 | } |
| 410 | |
| 411 | builder.markInfeasible(false); |
| 412 | return; |
| 413 | } |
| 414 | } |
| 415 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 416 | // Get the current block counter. |
| 417 | GRBlockCounter BC = builder.getBlockCounter(); |
| 418 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 419 | unsigned BlockID = builder.getTargetBlock(true)->getBlockID(); |
| 420 | unsigned NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 421 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 422 | if (isa<nonlval::ConcreteInt>(V) || |
| 423 | BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) { |
| 424 | |
| 425 | // Process the true branch. |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 426 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 427 | bool isFeasible = true; |
| 428 | |
| 429 | StateTy St = Assume(PrevState, V, true, isFeasible); |
| 430 | |
| 431 | if (isFeasible) |
| 432 | builder.generateNode(St, true); |
| 433 | else |
| 434 | builder.markInfeasible(true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 435 | } |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 436 | else |
| 437 | builder.markInfeasible(true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 438 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 439 | BlockID = builder.getTargetBlock(false)->getBlockID(); |
| 440 | NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 441 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 442 | if (isa<nonlval::ConcreteInt>(V) || |
| 443 | BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) { |
| 444 | |
| 445 | // Process the false branch. |
| 446 | |
| 447 | bool isFeasible = false; |
| 448 | |
| 449 | StateTy St = Assume(PrevState, V, false, isFeasible); |
| 450 | |
| 451 | if (isFeasible) |
| 452 | builder.generateNode(St, false); |
| 453 | else |
| 454 | builder.markInfeasible(false); |
| 455 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 456 | else |
| 457 | builder.markInfeasible(false); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 460 | /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 461 | /// nodes by processing the 'effects' of a computed goto jump. |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 462 | void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) { |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 463 | |
| 464 | StateTy St = builder.getState(); |
| 465 | LValue V = cast<LValue>(GetValue(St, builder.getTarget())); |
| 466 | |
| 467 | // Three possibilities: |
| 468 | // |
| 469 | // (1) We know the computed label. |
| 470 | // (2) The label is NULL (or some other constant), or Uninitialized. |
| 471 | // (3) We have no clue about the label. Dispatch to all targets. |
| 472 | // |
| 473 | |
| 474 | typedef IndirectGotoNodeBuilder::iterator iterator; |
| 475 | |
| 476 | if (isa<lval::GotoLabel>(V)) { |
| 477 | LabelStmt* L = cast<lval::GotoLabel>(V).getLabel(); |
| 478 | |
| 479 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) { |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 480 | if (I.getLabel() == L) { |
| 481 | builder.generateNode(I, St); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 482 | return; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | assert (false && "No block with label."); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) { |
| 491 | // Dispatch to the first target and mark it as a sink. |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 492 | NodeTy* N = builder.generateNode(builder.begin(), St, true); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 493 | UninitBranches.insert(N); |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | // This is really a catch-all. We don't support symbolics yet. |
| 498 | |
| 499 | assert (isa<UnknownVal>(V)); |
| 500 | |
| 501 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 502 | builder.generateNode(I, St); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 503 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 504 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 505 | /// ProcessSwitch - Called by GRCoreEngine. Used to generate successor |
| 506 | /// nodes by processing the 'effects' of a switch statement. |
| 507 | void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) { |
| 508 | |
| 509 | typedef SwitchNodeBuilder::iterator iterator; |
| 510 | |
| 511 | StateTy St = builder.getState(); |
| 512 | NonLValue CondV = cast<NonLValue>(GetValue(St, builder.getCondition())); |
| 513 | |
| 514 | if (isa<UninitializedVal>(CondV)) { |
| 515 | NodeTy* N = builder.generateDefaultCaseNode(St, true); |
| 516 | UninitBranches.insert(N); |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | StateTy DefaultSt = St; |
| 521 | |
| 522 | // While most of this can be assumed (such as the signedness), having it |
| 523 | // just computed makes sure everything makes the same assumptions end-to-end. |
| 524 | unsigned bits = getContext().getTypeSize(getContext().IntTy,SourceLocation()); |
| 525 | APSInt V1(bits, false); |
| 526 | APSInt V2 = V1; |
| 527 | |
| 528 | for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) { |
| 529 | |
| 530 | CaseStmt* Case = cast<CaseStmt>(I.getCase()); |
| 531 | |
| 532 | // Evaluate the case. |
| 533 | if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) { |
| 534 | assert (false && "Case condition must evaluate to an integer constant."); |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | // Get the RHS of the case, if it exists. |
| 539 | |
| 540 | if (Expr* E = Case->getRHS()) { |
| 541 | if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) { |
| 542 | assert (false && |
| 543 | "Case condition (RHS) must evaluate to an integer constant."); |
| 544 | return ; |
| 545 | } |
| 546 | |
| 547 | assert (V1 <= V2); |
| 548 | } |
| 549 | else V2 = V1; |
| 550 | |
| 551 | // FIXME: Eventually we should replace the logic below with a range |
| 552 | // comparison, rather than concretize the values within the range. |
| 553 | // This should be easy once we have "ranges" for NonLValues. |
| 554 | |
| 555 | do { |
| 556 | nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1)); |
| 557 | |
| 558 | NonLValue Result = |
| 559 | CondV.EvalBinaryOp(ValMgr, BinaryOperator::EQ, CaseVal); |
| 560 | |
| 561 | // Now "assume" that the case matches. |
| 562 | bool isFeasible; |
| 563 | |
| 564 | StateTy StNew = Assume(St, Result, true, isFeasible); |
| 565 | |
| 566 | if (isFeasible) { |
| 567 | builder.generateCaseStmtNode(I, StNew); |
| 568 | |
| 569 | // If CondV evaluates to a constant, then we know that this |
| 570 | // is the *only* case that we can take, so stop evaluating the |
| 571 | // others. |
| 572 | if (isa<nonlval::ConcreteInt>(CondV)) |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | // Now "assume" that the case doesn't match. Add this state |
| 577 | // to the default state (if it is feasible). |
| 578 | |
| 579 | StNew = Assume(DefaultSt, Result, false, isFeasible); |
| 580 | |
| 581 | if (isFeasible) |
| 582 | DefaultSt = StNew; |
| 583 | |
| 584 | // Concretize the next value in the range. |
| 585 | ++V1; |
| 586 | |
| 587 | } while (V1 < V2); |
| 588 | } |
| 589 | |
| 590 | // If we reach here, than we know that the default branch is |
| 591 | // possible. |
| 592 | builder.generateDefaultCaseNode(DefaultSt); |
| 593 | } |
| 594 | |
| 595 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 596 | void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 597 | NodeSet& Dst) { |
| 598 | |
| 599 | bool hasR2; |
| 600 | StateTy PrevState = Pred->getState(); |
| 601 | |
| 602 | RValue R1 = GetValue(PrevState, B->getLHS()); |
| 603 | RValue R2 = GetValue(PrevState, B->getRHS(), hasR2); |
| 604 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 605 | if (isa<UnknownVal>(R1) && |
| 606 | (isa<UnknownVal>(R2) || |
| 607 | isa<UninitializedVal>(R2))) { |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 608 | |
| 609 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R2)); |
| 610 | return; |
| 611 | } |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 612 | else if (isa<UninitializedVal>(R1)) { |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 613 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R1)); |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | // R1 is an expression that can evaluate to either 'true' or 'false'. |
| 618 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 619 | // hasR2 == 'false' means that LHS evaluated to 'false' and that |
| 620 | // we short-circuited, leading to a value of '0' for the '&&' expression. |
| 621 | if (hasR2 == false) { |
| 622 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 623 | return; |
| 624 | } |
| 625 | } |
| 626 | else { |
| 627 | assert (B->getOpcode() == BinaryOperator::LOr); |
| 628 | // hasR2 == 'false' means that the LHS evaluate to 'true' and that |
| 629 | // we short-circuited, leading to a value of '1' for the '||' expression. |
| 630 | if (hasR2 == false) { |
| 631 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 632 | return; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | // If we reach here we did not short-circuit. Assume R2 == true and |
| 637 | // R2 == false. |
| 638 | |
| 639 | bool isFeasible; |
| 640 | StateTy St = Assume(PrevState, R2, true, isFeasible); |
| 641 | |
| 642 | if (isFeasible) |
| 643 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 644 | |
| 645 | St = Assume(PrevState, R2, false, isFeasible); |
| 646 | |
| 647 | if (isFeasible) |
| 648 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 649 | } |
| 650 | |
| 651 | |
| 652 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 653 | void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 654 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 655 | |
| 656 | StmtEntryNode = builder.getLastNode(); |
| 657 | CurrentStmt = S; |
| 658 | NodeSet Dst; |
| 659 | StateCleaned = false; |
| 660 | |
| 661 | Visit(S, StmtEntryNode, Dst); |
| 662 | |
| 663 | // If no nodes were generated, generate a new node that has all the |
| 664 | // dead mappings removed. |
| 665 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 666 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 667 | builder.generateNode(S, St, StmtEntryNode); |
| 668 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 669 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 670 | CurrentStmt = NULL; |
| 671 | StmtEntryNode = NULL; |
| 672 | Builder = NULL; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 675 | GRExprEngine::NodeTy* |
| 676 | GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 677 | |
| 678 | // If the state hasn't changed, don't generate a new node. |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 679 | if (St == Pred->getState()) |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 680 | return NULL; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 681 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 682 | NodeTy* N = Builder->generateNode(S, St, Pred); |
| 683 | Dst.Add(N); |
| 684 | return N; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 685 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 686 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 687 | void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 688 | const StateTy::BufferTy& SB) { |
| 689 | |
| 690 | for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I) |
| 691 | Nodify(Dst, S, Pred, *I); |
| 692 | } |
| 693 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 694 | void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 695 | if (D != CurrentStmt) { |
| 696 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | // If we are here, we are loading the value of the decl and binding |
| 701 | // it to the block-level expression. |
| 702 | |
| 703 | StateTy St = Pred->getState(); |
| 704 | |
| 705 | Nodify(Dst, D, Pred, |
| 706 | SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl())))); |
| 707 | } |
| 708 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 709 | void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 710 | |
| 711 | QualType T = CastE->getType(); |
| 712 | |
| 713 | // Check for redundant casts. |
| 714 | if (E->getType() == T) { |
| 715 | Dst.Add(Pred); |
| 716 | return; |
| 717 | } |
| 718 | |
| 719 | NodeSet S1; |
| 720 | Visit(E, Pred, S1); |
| 721 | |
| 722 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 723 | NodeTy* N = *I1; |
| 724 | StateTy St = N->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 725 | const RValue& V = GetValue(St, E); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 726 | Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 727 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 730 | void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred, |
| 731 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 732 | |
| 733 | StateTy St = Pred->getState(); |
| 734 | |
| 735 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 736 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 737 | const Expr* E = VD->getInit(); |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 738 | St = SetValue(St, lval::DeclVal(VD), |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 739 | E ? GetValue(St, E) : UninitializedVal()); |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 740 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 741 | |
| 742 | Nodify(Dst, DS, Pred, St); |
| 743 | |
| 744 | if (Dst.empty()) |
| 745 | Dst.Add(Pred); |
| 746 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 747 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 748 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 749 | void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS, |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 750 | NodeTy* Pred, NodeSet& Dst) { |
| 751 | |
| 752 | StateTy St = Pred->getState(); |
| 753 | |
| 754 | RValue R = GetValue(St, LHS); |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 755 | if (isa<UnknownVal>(R)) R = GetValue(St, RHS); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 756 | |
| 757 | Nodify(Dst, S, Pred, SetValue(St, S, R)); |
| 758 | } |
| 759 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 760 | /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type). |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 761 | void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 762 | NodeTy* Pred, |
| 763 | NodeSet& Dst) { |
| 764 | |
| 765 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 766 | |
| 767 | QualType T = S->getArgumentType(); |
| 768 | |
| 769 | // FIXME: Add support for VLAs. |
| 770 | if (isa<VariableArrayType>(T.getTypePtr())) |
| 771 | return; |
| 772 | |
| 773 | SourceLocation L = S->getExprLoc(); |
| 774 | uint64_t size = getContext().getTypeSize(T, L) / 8; |
| 775 | |
| 776 | Nodify(Dst, S, Pred, |
| 777 | SetValue(Pred->getState(), S, |
| 778 | NonLValue::GetValue(ValMgr, size, getContext().IntTy, L))); |
| 779 | |
| 780 | } |
| 781 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 782 | void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, |
| 783 | GRExprEngine::NodeTy* Pred, |
| 784 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 785 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 786 | NodeSet S1; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 787 | UnaryOperator::Opcode Op = U->getOpcode(); |
| 788 | |
| 789 | // FIXME: This is a hack so that for '*' and '&' we don't recurse |
| 790 | // on visiting the subexpression if it is a DeclRefExpr. We should |
| 791 | // probably just handle AddrOf and Deref in their own methods to make |
| 792 | // this cleaner. |
| 793 | if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) && |
| 794 | isa<DeclRefExpr>(U->getSubExpr())) |
| 795 | S1.Add(Pred); |
| 796 | else |
| 797 | Visit(U->getSubExpr(), Pred, S1); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 798 | |
| 799 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 800 | NodeTy* N1 = *I1; |
| 801 | StateTy St = N1->getState(); |
| 802 | |
| 803 | switch (U->getOpcode()) { |
| 804 | case UnaryOperator::PostInc: { |
| 805 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 806 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 807 | |
| 808 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add, |
| 809 | GetRValueConstant(1U, U)); |
| 810 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 811 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 812 | break; |
| 813 | } |
| 814 | |
| 815 | case UnaryOperator::PostDec: { |
| 816 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 817 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 818 | |
| 819 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub, |
| 820 | GetRValueConstant(1U, U)); |
| 821 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 822 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 823 | break; |
| 824 | } |
| 825 | |
| 826 | case UnaryOperator::PreInc: { |
| 827 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 828 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 829 | |
| 830 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add, |
| 831 | GetRValueConstant(1U, U)); |
| 832 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 833 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 834 | break; |
| 835 | } |
| 836 | |
| 837 | case UnaryOperator::PreDec: { |
| 838 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 839 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 840 | |
| 841 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub, |
| 842 | GetRValueConstant(1U, U)); |
| 843 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 844 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 845 | break; |
| 846 | } |
| 847 | |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 848 | case UnaryOperator::Minus: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 849 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 850 | Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 851 | break; |
| 852 | } |
| 853 | |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 854 | case UnaryOperator::Not: { |
| 855 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 856 | Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1))); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 857 | break; |
| 858 | } |
| 859 | |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 860 | case UnaryOperator::LNot: { |
| 861 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 862 | // |
| 863 | // Note: technically we do "E == 0", but this is the same in the |
| 864 | // transfer functions as "0 == E". |
| 865 | |
| 866 | RValue V1 = GetValue(St, U->getSubExpr()); |
| 867 | |
| 868 | if (isa<LValue>(V1)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 869 | const LValue& L1 = cast<LValue>(V1); |
| 870 | lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
| 871 | Nodify(Dst, U, N1, |
| 872 | SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 873 | V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 874 | } |
| 875 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 876 | const NonLValue& R1 = cast<NonLValue>(V1); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 877 | nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 878 | Nodify(Dst, U, N1, |
| 879 | SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 880 | V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | break; |
| 884 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 885 | |
| 886 | case UnaryOperator::SizeOf: { |
| 887 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 888 | |
| 889 | QualType T = U->getSubExpr()->getType(); |
| 890 | |
| 891 | // FIXME: Add support for VLAs. |
| 892 | if (isa<VariableArrayType>(T.getTypePtr())) |
| 893 | return; |
| 894 | |
| 895 | SourceLocation L = U->getExprLoc(); |
| 896 | uint64_t size = getContext().getTypeSize(T, L) / 8; |
| 897 | |
| 898 | Nodify(Dst, U, N1, |
| 899 | SetValue(St, U, NonLValue::GetValue(ValMgr, size, |
| 900 | getContext().IntTy, L))); |
| 901 | |
| 902 | break; |
| 903 | } |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 904 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 905 | case UnaryOperator::AddrOf: { |
| 906 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 907 | Nodify(Dst, U, N1, SetValue(St, U, L1)); |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | case UnaryOperator::Deref: { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 912 | // FIXME: Stop when dereferencing an uninitialized value. |
| 913 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 914 | |
| 915 | const RValue& V = GetValue(St, U->getSubExpr()); |
| 916 | const LValue& L1 = cast<LValue>(V); |
| 917 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 918 | // After a dereference, one of two possible situations arise: |
| 919 | // (1) A crash, because the pointer was NULL. |
| 920 | // (2) The pointer is not NULL, and the dereference works. |
| 921 | // |
| 922 | // We add these assumptions. |
| 923 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 924 | bool isFeasibleNotNull; |
| 925 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 926 | // "Assume" that the pointer is Not-NULL. |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 927 | StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull); |
| 928 | |
| 929 | if (isFeasibleNotNull) { |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 930 | QualType T = U->getType(); |
| 931 | Nodify(Dst, U, N1, SetValue(StNotNull, U, |
| 932 | GetValue(StNotNull, L1, &T))); |
| 933 | } |
| 934 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 935 | bool isFeasibleNull; |
| 936 | |
| 937 | // "Assume" that the pointer is NULL. |
| 938 | StateTy StNull = Assume(St, L1, false, isFeasibleNull); |
| 939 | |
| 940 | if (isFeasibleNull) { |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 941 | // We don't use "Nodify" here because the node will be a sink |
| 942 | // and we have no intention of processing it later. |
| 943 | NodeTy* NullNode = Builder->generateNode(U, StNull, N1); |
| 944 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 945 | if (NullNode) { |
| 946 | NullNode->markAsSink(); |
| 947 | |
| 948 | if (isFeasibleNotNull) |
| 949 | ImplicitNullDeref.insert(NullNode); |
| 950 | else |
| 951 | ExplicitNullDeref.insert(NullNode); |
| 952 | } |
| 953 | } |
| 954 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 955 | break; |
| 956 | } |
| 957 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 958 | default: ; |
| 959 | assert (false && "Not implemented."); |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 964 | void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred, |
| 965 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 966 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 967 | if (isa<DeclRefExpr>(E)) { |
| 968 | Dst.Add(Pred); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 969 | return; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 970 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 971 | |
| 972 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) { |
| 973 | if (U->getOpcode() == UnaryOperator::Deref) { |
| 974 | Visit(U->getSubExpr(), Pred, Dst); |
| 975 | return; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | Visit(E, Pred, Dst); |
| 980 | } |
| 981 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 982 | void GRExprEngine::VisitBinaryOperator(BinaryOperator* B, |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 983 | GRExprEngine::NodeTy* Pred, |
| 984 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 985 | NodeSet S1; |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 986 | |
| 987 | if (B->isAssignmentOp()) |
| 988 | VisitAssignmentLHS(B->getLHS(), Pred, S1); |
| 989 | else |
| 990 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 991 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 992 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 993 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 994 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 995 | // When getting the value for the LHS, check if we are in an assignment. |
| 996 | // In such cases, we want to (initially) treat the LHS as an LValue, |
| 997 | // so we use GetLValue instead of GetValue so that DeclRefExpr's are |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 998 | // evaluated to LValueDecl's instead of to an NonLValue. |
| 999 | const RValue& V1 = |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1000 | B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS()) |
| 1001 | : GetValue(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1002 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1003 | NodeSet S2; |
| 1004 | Visit(B->getRHS(), N1, S2); |
| 1005 | |
| 1006 | for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1007 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1008 | NodeTy* N2 = *I2; |
| 1009 | StateTy St = N2->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1010 | const RValue& V2 = GetValue(St, B->getRHS()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1011 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1012 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 1013 | |
| 1014 | if (Op <= BinaryOperator::Or) { |
| 1015 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 1016 | if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1017 | Nodify(Dst, B, N2, SetValue(St, B, V1)); |
| 1018 | continue; |
| 1019 | } |
| 1020 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1021 | if (isa<LValue>(V1)) { |
| 1022 | // FIXME: Add support for RHS being a non-lvalue. |
| 1023 | const LValue& L1 = cast<LValue>(V1); |
| 1024 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1025 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1026 | Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2))); |
| 1027 | } |
| 1028 | else { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1029 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 1030 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1031 | |
| 1032 | Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2))); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1033 | } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1034 | |
| 1035 | continue; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 1036 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | switch (Op) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1040 | case BinaryOperator::Assign: { |
| 1041 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 3434b08 | 2008-02-06 04:41:14 +0000 | [diff] [blame] | 1042 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1043 | break; |
| 1044 | } |
| 1045 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1046 | default: { // Compound assignment operators. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1047 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1048 | assert (B->isCompoundAssignmentOp()); |
| 1049 | |
| 1050 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 1051 | RValue Result = cast<NonLValue>(UnknownVal()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1052 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1053 | if (Op >= BinaryOperator::AndAssign) |
| 1054 | ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And); |
| 1055 | else |
| 1056 | ((int&) Op) -= BinaryOperator::MulAssign; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1057 | |
| 1058 | if (isa<LValue>(V2)) { |
| 1059 | // FIXME: Add support for Non-LValues on RHS. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1060 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1061 | Result = L1.EvalBinaryOp(ValMgr, Op, L2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1062 | } |
| 1063 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1064 | const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1065 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1066 | Result = R1.EvalBinaryOp(ValMgr, Op, R2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1069 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1070 | break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1071 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1072 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1073 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1074 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1075 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1076 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1077 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1078 | void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred, |
| 1079 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1080 | |
| 1081 | // FIXME: add metadata to the CFG so that we can disable |
| 1082 | // this check when we KNOW that there is no block-level subexpression. |
| 1083 | // The motivation is that this check requires a hashtable lookup. |
| 1084 | |
| 1085 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 1086 | Dst.Add(Pred); |
| 1087 | return; |
| 1088 | } |
| 1089 | |
| 1090 | switch (S->getStmtClass()) { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1091 | |
| 1092 | default: |
| 1093 | // Cases we intentionally have "default" handle: |
| 1094 | // AddrLabelExpr, CharacterLiteral, IntegerLiteral |
| 1095 | |
| 1096 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 1097 | break; |
| 1098 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1099 | case Stmt::BinaryOperatorClass: { |
| 1100 | BinaryOperator* B = cast<BinaryOperator>(S); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1101 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1102 | if (B->isLogicalOp()) { |
| 1103 | VisitLogicalExpr(B, Pred, Dst); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1104 | break; |
| 1105 | } |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1106 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1107 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1108 | Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS()))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1109 | break; |
| 1110 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1111 | |
| 1112 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 1113 | break; |
| 1114 | } |
| 1115 | |
| 1116 | case Stmt::CastExprClass: { |
| 1117 | CastExpr* C = cast<CastExpr>(S); |
| 1118 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1119 | break; |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1120 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1121 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1122 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 1123 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 1124 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1125 | break; |
| 1126 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1127 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1128 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1129 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 1130 | break; |
| 1131 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1132 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 1133 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 1134 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1135 | break; |
| 1136 | } |
| 1137 | |
| 1138 | case Stmt::DeclRefExprClass: |
| 1139 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 1140 | break; |
| 1141 | |
| 1142 | case Stmt::DeclStmtClass: |
| 1143 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 1144 | break; |
| 1145 | |
| 1146 | case Stmt::ImplicitCastExprClass: { |
| 1147 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 1148 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1149 | break; |
| 1150 | } |
| 1151 | |
| 1152 | case Stmt::ParenExprClass: |
| 1153 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 1154 | break; |
| 1155 | |
| 1156 | case Stmt::SizeOfAlignOfTypeExprClass: |
| 1157 | VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst); |
| 1158 | break; |
| 1159 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1160 | case Stmt::StmtExprClass: { |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1161 | StmtExpr* SE = cast<StmtExpr>(S); |
| 1162 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1163 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1164 | Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin()); |
| 1165 | Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1166 | break; |
| 1167 | } |
| 1168 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1169 | case Stmt::ReturnStmtClass: { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1170 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 1171 | Visit(R, Pred, Dst); |
| 1172 | else |
| 1173 | Dst.Add(Pred); |
| 1174 | |
| 1175 | break; |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1176 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1177 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1178 | case Stmt::UnaryOperatorClass: |
| 1179 | VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst); |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1180 | break; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 1181 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1184 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1185 | // "Assume" logic. |
| 1186 | //===----------------------------------------------------------------------===// |
| 1187 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1188 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1189 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1190 | bool& isFeasible) { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1191 | |
| 1192 | switch (Cond.getSubKind()) { |
| 1193 | default: |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1194 | assert (false && "'Assume' not implemented for this LValue."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1195 | return St; |
| 1196 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1197 | case lval::SymbolValKind: |
| 1198 | if (Assumption) |
| 1199 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1200 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1201 | else |
| 1202 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1203 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1204 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1205 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1206 | case lval::DeclValKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1207 | isFeasible = Assumption; |
| 1208 | return St; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1209 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1210 | case lval::ConcreteIntKind: { |
| 1211 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1212 | isFeasible = b ? Assumption : !Assumption; |
| 1213 | return St; |
| 1214 | } |
| 1215 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1218 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1219 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1220 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1221 | |
| 1222 | switch (Cond.getSubKind()) { |
| 1223 | default: |
| 1224 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 1225 | return St; |
| 1226 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1227 | |
| 1228 | case nonlval::SymbolValKind: { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1229 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1230 | SymbolID sym = SV.getSymbol(); |
| 1231 | |
| 1232 | if (Assumption) |
| 1233 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1234 | isFeasible); |
| 1235 | else |
| 1236 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1237 | isFeasible); |
| 1238 | } |
| 1239 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1240 | case nonlval::SymIntConstraintValKind: |
| 1241 | return |
| 1242 | AssumeSymInt(St, Assumption, |
| 1243 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 1244 | isFeasible); |
| 1245 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1246 | case nonlval::ConcreteIntKind: { |
| 1247 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1248 | isFeasible = b ? Assumption : !Assumption; |
| 1249 | return St; |
| 1250 | } |
| 1251 | } |
| 1252 | } |
| 1253 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1254 | GRExprEngine::StateTy |
| 1255 | GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1256 | const llvm::APSInt& V, bool& isFeasible) { |
| 1257 | |
| 1258 | // First, determine if sym == X, where X != V. |
| 1259 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1260 | isFeasible = *X != V; |
| 1261 | return St; |
| 1262 | } |
| 1263 | |
| 1264 | // Second, determine if sym != V. |
| 1265 | if (St.isNotEqual(sym, V)) { |
| 1266 | isFeasible = true; |
| 1267 | return St; |
| 1268 | } |
| 1269 | |
| 1270 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 1271 | // Make that assumption. |
| 1272 | |
| 1273 | isFeasible = true; |
| 1274 | return StateMgr.AddNE(St, sym, V); |
| 1275 | } |
| 1276 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1277 | GRExprEngine::StateTy |
| 1278 | GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1279 | const llvm::APSInt& V, bool& isFeasible) { |
| 1280 | |
| 1281 | // First, determine if sym == X, where X != V. |
| 1282 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1283 | isFeasible = *X == V; |
| 1284 | return St; |
| 1285 | } |
| 1286 | |
| 1287 | // Second, determine if sym != V. |
| 1288 | if (St.isNotEqual(sym, V)) { |
| 1289 | isFeasible = false; |
| 1290 | return St; |
| 1291 | } |
| 1292 | |
| 1293 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 1294 | // Make that assumption. |
| 1295 | |
| 1296 | isFeasible = true; |
| 1297 | return StateMgr.AddEQ(St, sym, V); |
| 1298 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1299 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1300 | GRExprEngine::StateTy |
| 1301 | GRExprEngine::AssumeSymInt(StateTy St, bool Assumption, |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1302 | const SymIntConstraint& C, bool& isFeasible) { |
| 1303 | |
| 1304 | switch (C.getOpcode()) { |
| 1305 | default: |
| 1306 | // No logic yet for other operators. |
| 1307 | return St; |
| 1308 | |
| 1309 | case BinaryOperator::EQ: |
| 1310 | if (Assumption) |
| 1311 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1312 | else |
| 1313 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1314 | |
| 1315 | case BinaryOperator::NE: |
| 1316 | if (Assumption) |
| 1317 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1318 | else |
| 1319 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1320 | } |
| 1321 | } |
| 1322 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1323 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1324 | // Driver. |
| 1325 | //===----------------------------------------------------------------------===// |
| 1326 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1327 | #ifndef NDEBUG |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1328 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1329 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1330 | namespace llvm { |
| 1331 | template<> |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1332 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> : |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1333 | public DefaultDOTGraphTraits { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1334 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1335 | static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1336 | |
| 1337 | Out << "Variables:\\l"; |
| 1338 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1339 | bool isFirst = true; |
| 1340 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1341 | for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(), |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1342 | E=St.vb_end(); I!=E;++I) { |
| 1343 | |
| 1344 | if (isFirst) |
| 1345 | isFirst = false; |
| 1346 | else |
| 1347 | Out << "\\l"; |
| 1348 | |
| 1349 | Out << ' ' << I.getKey()->getName() << " : "; |
| 1350 | I.getData().print(Out); |
| 1351 | } |
| 1352 | |
| 1353 | } |
| 1354 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1355 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1356 | static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1357 | |
| 1358 | bool isFirst = true; |
| 1359 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1360 | for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end(); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1361 | I != E;++I) { |
| 1362 | |
| 1363 | if (isFirst) { |
| 1364 | Out << "\\l\\lSub-Expressions:\\l"; |
| 1365 | isFirst = false; |
| 1366 | } |
| 1367 | else |
| 1368 | Out << "\\l"; |
| 1369 | |
| 1370 | Out << " (" << (void*) I.getKey() << ") "; |
| 1371 | I.getKey()->printPretty(Out); |
| 1372 | Out << " : "; |
| 1373 | I.getData().print(Out); |
| 1374 | } |
| 1375 | } |
| 1376 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1377 | static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1378 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1379 | bool isFirst = true; |
| 1380 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1381 | for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end(); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1382 | I != E; ++I) { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1383 | if (isFirst) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1384 | Out << "\\l\\lBlock-level Expressions:\\l"; |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1385 | isFirst = false; |
| 1386 | } |
| 1387 | else |
| 1388 | Out << "\\l"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1389 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1390 | Out << " (" << (void*) I.getKey() << ") "; |
| 1391 | I.getKey()->printPretty(Out); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1392 | Out << " : "; |
| 1393 | I.getData().print(Out); |
| 1394 | } |
| 1395 | } |
| 1396 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1397 | static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1398 | ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq; |
| 1399 | |
| 1400 | if (CE.isEmpty()) |
| 1401 | return; |
| 1402 | |
| 1403 | Out << "\\l\\|'==' constraints:"; |
| 1404 | |
| 1405 | for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
| 1406 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1407 | } |
| 1408 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1409 | static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1410 | ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq; |
| 1411 | |
| 1412 | if (NE.isEmpty()) |
| 1413 | return; |
| 1414 | |
| 1415 | Out << "\\l\\|'!=' constraints:"; |
| 1416 | |
| 1417 | for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
| 1418 | I != EI; ++I){ |
| 1419 | |
| 1420 | Out << "\\l $" << I.getKey() << " : "; |
| 1421 | bool isFirst = true; |
| 1422 | |
| 1423 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1424 | EJ=I.getData().end(); |
| 1425 | for ( ; J != EJ; ++J) { |
| 1426 | if (isFirst) isFirst = false; |
| 1427 | else Out << ", "; |
| 1428 | |
| 1429 | Out << (*J)->toString(); |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1434 | static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1435 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1436 | |
| 1437 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1438 | ProgramPoint Loc = N->getLocation(); |
| 1439 | |
| 1440 | switch (Loc.getKind()) { |
| 1441 | case ProgramPoint::BlockEntranceKind: |
| 1442 | Out << "Block Entrance: B" |
| 1443 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1444 | break; |
| 1445 | |
| 1446 | case ProgramPoint::BlockExitKind: |
| 1447 | assert (false); |
| 1448 | break; |
| 1449 | |
| 1450 | case ProgramPoint::PostStmtKind: { |
| 1451 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1452 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1453 | << (void*) L.getStmt() << ' '; |
| 1454 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1455 | L.getStmt()->printPretty(Out); |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1456 | |
| 1457 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) { |
| 1458 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 1459 | } |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1460 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) { |
| 1461 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 1462 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1463 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1464 | break; |
| 1465 | } |
| 1466 | |
| 1467 | default: { |
| 1468 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1469 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1470 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1471 | |
| 1472 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1473 | Out << "\\|Terminator: "; |
| 1474 | E.getSrc()->printTerminator(Out); |
| 1475 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 1476 | if (isa<SwitchStmt>(T)) { |
| 1477 | Stmt* Label = E.getDst()->getLabel(); |
| 1478 | |
| 1479 | if (Label) { |
| 1480 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 1481 | Out << "\\lcase "; |
| 1482 | C->getLHS()->printPretty(Out); |
| 1483 | |
| 1484 | if (Stmt* RHS = C->getRHS()) { |
| 1485 | Out << " .. "; |
| 1486 | RHS->printPretty(Out); |
| 1487 | } |
| 1488 | |
| 1489 | Out << ":"; |
| 1490 | } |
| 1491 | else { |
| 1492 | assert (isa<DefaultStmt>(Label)); |
| 1493 | Out << "\\ldefault:"; |
| 1494 | } |
| 1495 | } |
| 1496 | else |
| 1497 | Out << "\\l(implicit) default:"; |
| 1498 | } |
| 1499 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1500 | // FIXME |
| 1501 | } |
| 1502 | else { |
| 1503 | Out << "\\lCondition: "; |
| 1504 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1505 | Out << "true"; |
| 1506 | else |
| 1507 | Out << "false"; |
| 1508 | } |
| 1509 | |
| 1510 | Out << "\\l"; |
| 1511 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1512 | |
| 1513 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1514 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1515 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1516 | } |
| 1517 | } |
| 1518 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1519 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1520 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1521 | N->getState().printDOT(Out); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1522 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1523 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1524 | return Out.str(); |
| 1525 | } |
| 1526 | }; |
| 1527 | } // end llvm namespace |
| 1528 | #endif |
| 1529 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1530 | namespace clang { |
Ted Kremenek | 0ee2571 | 2008-02-13 17:45:18 +0000 | [diff] [blame] | 1531 | void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx, |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 1532 | Diagnostic& Diag) { |
| 1533 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1534 | GRCoreEngine<GRExprEngine> Engine(cfg, FD, Ctx); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 1535 | GRExprEngine* CheckerState = &Engine.getCheckerState(); |
| 1536 | |
| 1537 | // Execute the worklist algorithm. |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 1538 | Engine.ExecuteWorkList(); |
| 1539 | |
| 1540 | // Look for explicit-Null dereferences and warn about them. |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 1541 | |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 1542 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1543 | for (GRExprEngine::null_iterator I=CheckerState->null_begin(), |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 1544 | E=CheckerState->null_end(); I!=E; ++I) { |
| 1545 | |
| 1546 | const PostStmt& L = cast<PostStmt>((*I)->getLocation()); |
| 1547 | Expr* E = cast<Expr>(L.getStmt()); |
| 1548 | |
| 1549 | Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()), |
| 1550 | diag::chkr_null_deref_after_check); |
| 1551 | } |
| 1552 | |
| 1553 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1554 | #ifndef NDEBUG |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 1555 | GraphPrintCheckerState = CheckerState; |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1556 | llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRExprEngine"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1557 | GraphPrintCheckerState = NULL; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1558 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1559 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1560 | } // end clang namespace |