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