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 | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 203 | StateTy SetValue(StateTy St, Expr* S, const RValue& V); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 205 | StateTy SetValue(StateTy St, const Expr* S, const RValue& V) { |
| 206 | return SetValue(St, const_cast<Expr*>(S), V); |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 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. |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 211 | const StateTy::BufferTy& SetValue(StateTy St, Expr* S, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 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 | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 217 | inline RValue GetValue(const StateTy& St, Expr* S) { |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 218 | return StateMgr.GetValue(St, S); |
| 219 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 221 | inline RValue GetValue(const StateTy& St, Expr* S, bool& hasVal) { |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 222 | return StateMgr.GetValue(St, S, &hasVal); |
| 223 | } |
| 224 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 225 | inline RValue GetValue(const StateTy& St, const Expr* S) { |
| 226 | return GetValue(St, const_cast<Expr*>(S)); |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 235 | inline LValue GetLValue(const StateTy& St, Expr* S) { |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 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 |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 293 | void VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS, |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 294 | NodeTy* Pred, NodeSet& Dst); |
| 295 | |
| 296 | /// VisitLogicalExpr - Transfer function logic for '&&', '||' |
| 297 | void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame^] | 298 | |
| 299 | /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type). |
| 300 | void VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, NodeTy* Pred, |
| 301 | NodeSet& Dst); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 302 | }; |
| 303 | } // end anonymous namespace |
| 304 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 305 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 306 | GRConstants::StateTy |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 307 | GRConstants::SetValue(StateTy St, Expr* S, const RValue& V) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 308 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 309 | if (!StateCleaned) { |
| 310 | St = RemoveDeadBindings(CurrentStmt, St); |
| 311 | StateCleaned = true; |
| 312 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 314 | bool isBlkExpr = false; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 316 | if (S == CurrentStmt) { |
| 317 | isBlkExpr = getCFG().isBlkExpr(S); |
| 318 | |
| 319 | if (!isBlkExpr) |
| 320 | return St; |
| 321 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 322 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 323 | return StateMgr.SetValue(St, S, isBlkExpr, V); |
| 324 | } |
| 325 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 326 | const GRConstants::StateTy::BufferTy& |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 327 | GRConstants::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 328 | StateTy::BufferTy& RetBuf) { |
| 329 | |
| 330 | assert (RetBuf.empty()); |
| 331 | |
| 332 | for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I) |
| 333 | RetBuf.push_back(SetValue(St, S, *I)); |
| 334 | |
| 335 | return RetBuf; |
| 336 | } |
| 337 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 338 | GRConstants::StateTy |
| 339 | GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) { |
| 340 | |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 341 | if (LV.isUnknown()) |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 342 | return St; |
| 343 | |
| 344 | if (!StateCleaned) { |
| 345 | St = RemoveDeadBindings(CurrentStmt, St); |
| 346 | StateCleaned = true; |
| 347 | } |
| 348 | |
| 349 | return StateMgr.SetValue(St, LV, V); |
| 350 | } |
| 351 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 352 | void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term, |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 353 | BranchNodeBuilder& builder) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 354 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 355 | // Remove old bindings for subexpressions. |
| 356 | StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState()); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 358 | RValue V = GetValue(PrevState, Condition); |
| 359 | |
| 360 | switch (V.getBaseKind()) { |
| 361 | default: |
| 362 | break; |
| 363 | |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 364 | case RValue::UnknownKind: |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 365 | builder.generateNode(PrevState, true); |
| 366 | builder.generateNode(PrevState, false); |
| 367 | return; |
| 368 | |
| 369 | case RValue::UninitializedKind: { |
| 370 | NodeTy* N = builder.generateNode(PrevState, true); |
| 371 | |
| 372 | if (N) { |
| 373 | N->markAsSink(); |
| 374 | UninitBranches.insert(N); |
| 375 | } |
| 376 | |
| 377 | builder.markInfeasible(false); |
| 378 | return; |
| 379 | } |
| 380 | } |
| 381 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 382 | // Get the current block counter. |
| 383 | GRBlockCounter BC = builder.getBlockCounter(); |
| 384 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame^] | 385 | unsigned BlockID = builder.getTargetBlock(true)->getBlockID(); |
| 386 | unsigned NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 387 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 388 | if (isa<nonlval::ConcreteInt>(V) || |
| 389 | BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) { |
| 390 | |
| 391 | // Process the true branch. |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 392 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 393 | bool isFeasible = true; |
| 394 | |
| 395 | StateTy St = Assume(PrevState, V, true, isFeasible); |
| 396 | |
| 397 | if (isFeasible) |
| 398 | builder.generateNode(St, true); |
| 399 | else |
| 400 | builder.markInfeasible(true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 401 | } |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 402 | else |
| 403 | builder.markInfeasible(true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 404 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame^] | 405 | BlockID = builder.getTargetBlock(false)->getBlockID(); |
| 406 | NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 407 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 408 | if (isa<nonlval::ConcreteInt>(V) || |
| 409 | BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) { |
| 410 | |
| 411 | // Process the false branch. |
| 412 | |
| 413 | bool isFeasible = false; |
| 414 | |
| 415 | StateTy St = Assume(PrevState, V, false, isFeasible); |
| 416 | |
| 417 | if (isFeasible) |
| 418 | builder.generateNode(St, false); |
| 419 | else |
| 420 | builder.markInfeasible(false); |
| 421 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 422 | else |
| 423 | builder.markInfeasible(false); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 426 | |
| 427 | void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
| 428 | NodeSet& Dst) { |
| 429 | |
| 430 | bool hasR2; |
| 431 | StateTy PrevState = Pred->getState(); |
| 432 | |
| 433 | RValue R1 = GetValue(PrevState, B->getLHS()); |
| 434 | RValue R2 = GetValue(PrevState, B->getRHS(), hasR2); |
| 435 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 436 | if (isa<UnknownVal>(R1) && |
| 437 | (isa<UnknownVal>(R2) || |
| 438 | isa<UninitializedVal>(R2))) { |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 439 | |
| 440 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R2)); |
| 441 | return; |
| 442 | } |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 443 | else if (isa<UninitializedVal>(R1)) { |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 444 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R1)); |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | // R1 is an expression that can evaluate to either 'true' or 'false'. |
| 449 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 450 | // hasR2 == 'false' means that LHS evaluated to 'false' and that |
| 451 | // we short-circuited, leading to a value of '0' for the '&&' expression. |
| 452 | if (hasR2 == false) { |
| 453 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 454 | return; |
| 455 | } |
| 456 | } |
| 457 | else { |
| 458 | assert (B->getOpcode() == BinaryOperator::LOr); |
| 459 | // hasR2 == 'false' means that the LHS evaluate to 'true' and that |
| 460 | // we short-circuited, leading to a value of '1' for the '||' expression. |
| 461 | if (hasR2 == false) { |
| 462 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 463 | return; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // If we reach here we did not short-circuit. Assume R2 == true and |
| 468 | // R2 == false. |
| 469 | |
| 470 | bool isFeasible; |
| 471 | StateTy St = Assume(PrevState, R2, true, isFeasible); |
| 472 | |
| 473 | if (isFeasible) |
| 474 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 475 | |
| 476 | St = Assume(PrevState, R2, false, isFeasible); |
| 477 | |
| 478 | if (isFeasible) |
| 479 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 480 | } |
| 481 | |
| 482 | |
| 483 | |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 484 | void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 485 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 486 | |
| 487 | StmtEntryNode = builder.getLastNode(); |
| 488 | CurrentStmt = S; |
| 489 | NodeSet Dst; |
| 490 | StateCleaned = false; |
| 491 | |
| 492 | Visit(S, StmtEntryNode, Dst); |
| 493 | |
| 494 | // If no nodes were generated, generate a new node that has all the |
| 495 | // dead mappings removed. |
| 496 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 497 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 498 | builder.generateNode(S, St, StmtEntryNode); |
| 499 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 500 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 501 | CurrentStmt = NULL; |
| 502 | StmtEntryNode = NULL; |
| 503 | Builder = NULL; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 506 | GRConstants::NodeTy* |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 507 | GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 508 | |
| 509 | // If the state hasn't changed, don't generate a new node. |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 510 | if (St == Pred->getState()) |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 511 | return NULL; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 512 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 513 | NodeTy* N = Builder->generateNode(S, St, Pred); |
| 514 | Dst.Add(N); |
| 515 | return N; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 516 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 517 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 518 | void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, |
| 519 | const StateTy::BufferTy& SB) { |
| 520 | |
| 521 | for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I) |
| 522 | Nodify(Dst, S, Pred, *I); |
| 523 | } |
| 524 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 525 | void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) { |
| 526 | if (D != CurrentStmt) { |
| 527 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | // If we are here, we are loading the value of the decl and binding |
| 532 | // it to the block-level expression. |
| 533 | |
| 534 | StateTy St = Pred->getState(); |
| 535 | |
| 536 | Nodify(Dst, D, Pred, |
| 537 | SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl())))); |
| 538 | } |
| 539 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 540 | void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 541 | |
| 542 | QualType T = CastE->getType(); |
| 543 | |
| 544 | // Check for redundant casts. |
| 545 | if (E->getType() == T) { |
| 546 | Dst.Add(Pred); |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | NodeSet S1; |
| 551 | Visit(E, Pred, S1); |
| 552 | |
| 553 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 554 | NodeTy* N = *I1; |
| 555 | StateTy St = N->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 556 | const RValue& V = GetValue(St, E); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 557 | Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 558 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred, |
| 562 | GRConstants::NodeSet& Dst) { |
| 563 | |
| 564 | StateTy St = Pred->getState(); |
| 565 | |
| 566 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 567 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 568 | const Expr* E = VD->getInit(); |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 569 | St = SetValue(St, lval::DeclVal(VD), |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 570 | E ? GetValue(St, E) : UninitializedVal()); |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 571 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 572 | |
| 573 | Nodify(Dst, DS, Pred, St); |
| 574 | |
| 575 | if (Dst.empty()) |
| 576 | Dst.Add(Pred); |
| 577 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 578 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 579 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 580 | void GRConstants::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS, |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 581 | NodeTy* Pred, NodeSet& Dst) { |
| 582 | |
| 583 | StateTy St = Pred->getState(); |
| 584 | |
| 585 | RValue R = GetValue(St, LHS); |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 586 | if (isa<UnknownVal>(R)) R = GetValue(St, RHS); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 587 | |
| 588 | Nodify(Dst, S, Pred, SetValue(St, S, R)); |
| 589 | } |
| 590 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame^] | 591 | /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type). |
| 592 | void GRConstants::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, |
| 593 | NodeTy* Pred, |
| 594 | NodeSet& Dst) { |
| 595 | |
| 596 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 597 | |
| 598 | QualType T = S->getArgumentType(); |
| 599 | |
| 600 | // FIXME: Add support for VLAs. |
| 601 | if (isa<VariableArrayType>(T.getTypePtr())) |
| 602 | return; |
| 603 | |
| 604 | SourceLocation L = S->getExprLoc(); |
| 605 | uint64_t size = getContext().getTypeSize(T, L) / 8; |
| 606 | |
| 607 | Nodify(Dst, S, Pred, |
| 608 | SetValue(Pred->getState(), S, |
| 609 | NonLValue::GetValue(ValMgr, size, getContext().IntTy, L))); |
| 610 | |
| 611 | } |
| 612 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 613 | void GRConstants::VisitUnaryOperator(UnaryOperator* U, |
| 614 | GRConstants::NodeTy* Pred, |
| 615 | GRConstants::NodeSet& Dst) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 616 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 617 | NodeSet S1; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 618 | UnaryOperator::Opcode Op = U->getOpcode(); |
| 619 | |
| 620 | // FIXME: This is a hack so that for '*' and '&' we don't recurse |
| 621 | // on visiting the subexpression if it is a DeclRefExpr. We should |
| 622 | // probably just handle AddrOf and Deref in their own methods to make |
| 623 | // this cleaner. |
| 624 | if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) && |
| 625 | isa<DeclRefExpr>(U->getSubExpr())) |
| 626 | S1.Add(Pred); |
| 627 | else |
| 628 | Visit(U->getSubExpr(), Pred, S1); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 629 | |
| 630 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 631 | NodeTy* N1 = *I1; |
| 632 | StateTy St = N1->getState(); |
| 633 | |
| 634 | switch (U->getOpcode()) { |
| 635 | case UnaryOperator::PostInc: { |
| 636 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 637 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 638 | |
| 639 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add, |
| 640 | GetRValueConstant(1U, U)); |
| 641 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 642 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 643 | break; |
| 644 | } |
| 645 | |
| 646 | case UnaryOperator::PostDec: { |
| 647 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 648 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 649 | |
| 650 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub, |
| 651 | GetRValueConstant(1U, U)); |
| 652 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 653 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 654 | break; |
| 655 | } |
| 656 | |
| 657 | case UnaryOperator::PreInc: { |
| 658 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 659 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 660 | |
| 661 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add, |
| 662 | GetRValueConstant(1U, U)); |
| 663 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 664 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | case UnaryOperator::PreDec: { |
| 669 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 670 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 671 | |
| 672 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub, |
| 673 | GetRValueConstant(1U, U)); |
| 674 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 675 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 676 | break; |
| 677 | } |
| 678 | |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 679 | case UnaryOperator::Minus: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 680 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 681 | Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 682 | break; |
| 683 | } |
| 684 | |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 685 | case UnaryOperator::Not: { |
| 686 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 687 | Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr))); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 688 | break; |
| 689 | } |
| 690 | |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 691 | case UnaryOperator::LNot: { |
| 692 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 693 | // |
| 694 | // Note: technically we do "E == 0", but this is the same in the |
| 695 | // transfer functions as "0 == E". |
| 696 | |
| 697 | RValue V1 = GetValue(St, U->getSubExpr()); |
| 698 | |
| 699 | if (isa<LValue>(V1)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 700 | const LValue& L1 = cast<LValue>(V1); |
| 701 | lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
| 702 | Nodify(Dst, U, N1, |
| 703 | SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 704 | V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 705 | } |
| 706 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 707 | const NonLValue& R1 = cast<NonLValue>(V1); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 708 | nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 709 | Nodify(Dst, U, N1, |
| 710 | SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 711 | V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | break; |
| 715 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame^] | 716 | |
| 717 | case UnaryOperator::SizeOf: { |
| 718 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 719 | |
| 720 | QualType T = U->getSubExpr()->getType(); |
| 721 | |
| 722 | // FIXME: Add support for VLAs. |
| 723 | if (isa<VariableArrayType>(T.getTypePtr())) |
| 724 | return; |
| 725 | |
| 726 | SourceLocation L = U->getExprLoc(); |
| 727 | uint64_t size = getContext().getTypeSize(T, L) / 8; |
| 728 | |
| 729 | Nodify(Dst, U, N1, |
| 730 | SetValue(St, U, NonLValue::GetValue(ValMgr, size, |
| 731 | getContext().IntTy, L))); |
| 732 | |
| 733 | break; |
| 734 | } |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 735 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 736 | case UnaryOperator::AddrOf: { |
| 737 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 738 | Nodify(Dst, U, N1, SetValue(St, U, L1)); |
| 739 | break; |
| 740 | } |
| 741 | |
| 742 | case UnaryOperator::Deref: { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 743 | // FIXME: Stop when dereferencing an uninitialized value. |
| 744 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 745 | |
| 746 | const RValue& V = GetValue(St, U->getSubExpr()); |
| 747 | const LValue& L1 = cast<LValue>(V); |
| 748 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 749 | // After a dereference, one of two possible situations arise: |
| 750 | // (1) A crash, because the pointer was NULL. |
| 751 | // (2) The pointer is not NULL, and the dereference works. |
| 752 | // |
| 753 | // We add these assumptions. |
| 754 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 755 | bool isFeasibleNotNull; |
| 756 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 757 | // "Assume" that the pointer is Not-NULL. |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 758 | StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull); |
| 759 | |
| 760 | if (isFeasibleNotNull) { |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 761 | QualType T = U->getType(); |
| 762 | Nodify(Dst, U, N1, SetValue(StNotNull, U, |
| 763 | GetValue(StNotNull, L1, &T))); |
| 764 | } |
| 765 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 766 | bool isFeasibleNull; |
| 767 | |
| 768 | // "Assume" that the pointer is NULL. |
| 769 | StateTy StNull = Assume(St, L1, false, isFeasibleNull); |
| 770 | |
| 771 | if (isFeasibleNull) { |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 772 | // We don't use "Nodify" here because the node will be a sink |
| 773 | // and we have no intention of processing it later. |
| 774 | NodeTy* NullNode = Builder->generateNode(U, StNull, N1); |
| 775 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 776 | if (NullNode) { |
| 777 | NullNode->markAsSink(); |
| 778 | |
| 779 | if (isFeasibleNotNull) |
| 780 | ImplicitNullDeref.insert(NullNode); |
| 781 | else |
| 782 | ExplicitNullDeref.insert(NullNode); |
| 783 | } |
| 784 | } |
| 785 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 786 | break; |
| 787 | } |
| 788 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 789 | default: ; |
| 790 | assert (false && "Not implemented."); |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 795 | void GRConstants::VisitAssignmentLHS(Expr* E, GRConstants::NodeTy* Pred, |
| 796 | GRConstants::NodeSet& Dst) { |
| 797 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 798 | if (isa<DeclRefExpr>(E)) { |
| 799 | Dst.Add(Pred); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 800 | return; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 801 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 802 | |
| 803 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) { |
| 804 | if (U->getOpcode() == UnaryOperator::Deref) { |
| 805 | Visit(U->getSubExpr(), Pred, Dst); |
| 806 | return; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | Visit(E, Pred, Dst); |
| 811 | } |
| 812 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 813 | void GRConstants::VisitBinaryOperator(BinaryOperator* B, |
| 814 | GRConstants::NodeTy* Pred, |
| 815 | GRConstants::NodeSet& Dst) { |
| 816 | NodeSet S1; |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 817 | |
| 818 | if (B->isAssignmentOp()) |
| 819 | VisitAssignmentLHS(B->getLHS(), Pred, S1); |
| 820 | else |
| 821 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 822 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 823 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 824 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 825 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 826 | // When getting the value for the LHS, check if we are in an assignment. |
| 827 | // In such cases, we want to (initially) treat the LHS as an LValue, |
| 828 | // so we use GetLValue instead of GetValue so that DeclRefExpr's are |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 829 | // evaluated to LValueDecl's instead of to an NonLValue. |
| 830 | const RValue& V1 = |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 831 | B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS()) |
| 832 | : GetValue(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 833 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 834 | NodeSet S2; |
| 835 | Visit(B->getRHS(), N1, S2); |
| 836 | |
| 837 | for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 838 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 839 | NodeTy* N2 = *I2; |
| 840 | StateTy St = N2->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 841 | const RValue& V2 = GetValue(St, B->getRHS()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 842 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 843 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 844 | |
| 845 | if (Op <= BinaryOperator::Or) { |
| 846 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 847 | if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 848 | Nodify(Dst, B, N2, SetValue(St, B, V1)); |
| 849 | continue; |
| 850 | } |
| 851 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 852 | if (isa<LValue>(V1)) { |
| 853 | // FIXME: Add support for RHS being a non-lvalue. |
| 854 | const LValue& L1 = cast<LValue>(V1); |
| 855 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 856 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 857 | Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2))); |
| 858 | } |
| 859 | else { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 860 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 861 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 862 | |
| 863 | Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2))); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 864 | } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 865 | |
| 866 | continue; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 867 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | switch (Op) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 871 | case BinaryOperator::Assign: { |
| 872 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 3434b08 | 2008-02-06 04:41:14 +0000 | [diff] [blame] | 873 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 874 | break; |
| 875 | } |
| 876 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 877 | default: { // Compound assignment operators. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 878 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 879 | assert (B->isCompoundAssignmentOp()); |
| 880 | |
| 881 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 882 | RValue Result = cast<NonLValue>(UnknownVal()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 883 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 884 | if (Op >= BinaryOperator::AndAssign) |
| 885 | ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And); |
| 886 | else |
| 887 | ((int&) Op) -= BinaryOperator::MulAssign; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 888 | |
| 889 | if (isa<LValue>(V2)) { |
| 890 | // FIXME: Add support for Non-LValues on RHS. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 891 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 892 | Result = L1.EvalBinaryOp(ValMgr, Op, L2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 893 | } |
| 894 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 895 | const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 896 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 897 | Result = R1.EvalBinaryOp(ValMgr, Op, R2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 900 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 901 | break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 902 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 903 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 904 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 905 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 906 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 907 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 908 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 909 | void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred, |
| 910 | GRConstants::NodeSet& Dst) { |
| 911 | |
| 912 | // FIXME: add metadata to the CFG so that we can disable |
| 913 | // this check when we KNOW that there is no block-level subexpression. |
| 914 | // The motivation is that this check requires a hashtable lookup. |
| 915 | |
| 916 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 917 | Dst.Add(Pred); |
| 918 | return; |
| 919 | } |
| 920 | |
| 921 | switch (S->getStmtClass()) { |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 922 | case Stmt::BinaryOperatorClass: { |
| 923 | BinaryOperator* B = cast<BinaryOperator>(S); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 924 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 925 | if (B->isLogicalOp()) { |
| 926 | VisitLogicalExpr(B, Pred, Dst); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 927 | break; |
| 928 | } |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 929 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 930 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 931 | Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS()))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 932 | break; |
| 933 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame^] | 934 | |
| 935 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 936 | break; |
| 937 | } |
| 938 | |
| 939 | case Stmt::CastExprClass: { |
| 940 | CastExpr* C = cast<CastExpr>(S); |
| 941 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 942 | break; |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 943 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 944 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame^] | 945 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 946 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 947 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 948 | break; |
| 949 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 950 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 951 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 952 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 953 | break; |
| 954 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame^] | 955 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 956 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 957 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 958 | break; |
| 959 | } |
| 960 | |
| 961 | case Stmt::DeclRefExprClass: |
| 962 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 963 | break; |
| 964 | |
| 965 | case Stmt::DeclStmtClass: |
| 966 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 967 | break; |
| 968 | |
| 969 | case Stmt::ImplicitCastExprClass: { |
| 970 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 971 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 972 | break; |
| 973 | } |
| 974 | |
| 975 | case Stmt::ParenExprClass: |
| 976 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 977 | break; |
| 978 | |
| 979 | case Stmt::SizeOfAlignOfTypeExprClass: |
| 980 | VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst); |
| 981 | break; |
| 982 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 983 | case Stmt::StmtExprClass: { |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 984 | StmtExpr* SE = cast<StmtExpr>(S); |
| 985 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 986 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 987 | Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin()); |
| 988 | Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 989 | break; |
| 990 | } |
| 991 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame^] | 992 | case Stmt::ReturnStmtClass: { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 993 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 994 | Visit(R, Pred, Dst); |
| 995 | else |
| 996 | Dst.Add(Pred); |
| 997 | |
| 998 | break; |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame^] | 999 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1000 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame^] | 1001 | case Stmt::UnaryOperatorClass: |
| 1002 | VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst); |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1003 | break; |
| 1004 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1005 | default: |
| 1006 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 1007 | break; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 1008 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1011 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1012 | // "Assume" logic. |
| 1013 | //===----------------------------------------------------------------------===// |
| 1014 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1015 | GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond, |
| 1016 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1017 | bool& isFeasible) { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1018 | |
| 1019 | switch (Cond.getSubKind()) { |
| 1020 | default: |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1021 | assert (false && "'Assume' not implemented for this LValue."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1022 | return St; |
| 1023 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1024 | case lval::SymbolValKind: |
| 1025 | if (Assumption) |
| 1026 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1027 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1028 | else |
| 1029 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1030 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1031 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1032 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1033 | case lval::DeclValKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1034 | isFeasible = Assumption; |
| 1035 | return St; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1036 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1037 | case lval::ConcreteIntKind: { |
| 1038 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1039 | isFeasible = b ? Assumption : !Assumption; |
| 1040 | return St; |
| 1041 | } |
| 1042 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1045 | GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond, |
| 1046 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1047 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1048 | |
| 1049 | switch (Cond.getSubKind()) { |
| 1050 | default: |
| 1051 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 1052 | return St; |
| 1053 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1054 | |
| 1055 | case nonlval::SymbolValKind: { |
| 1056 | lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond); |
| 1057 | SymbolID sym = SV.getSymbol(); |
| 1058 | |
| 1059 | if (Assumption) |
| 1060 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1061 | isFeasible); |
| 1062 | else |
| 1063 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1064 | isFeasible); |
| 1065 | } |
| 1066 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1067 | case nonlval::SymIntConstraintValKind: |
| 1068 | return |
| 1069 | AssumeSymInt(St, Assumption, |
| 1070 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 1071 | isFeasible); |
| 1072 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1073 | case nonlval::ConcreteIntKind: { |
| 1074 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1075 | isFeasible = b ? Assumption : !Assumption; |
| 1076 | return St; |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1081 | GRConstants::StateTy |
| 1082 | GRConstants::AssumeSymNE(StateTy St, SymbolID sym, |
| 1083 | const llvm::APSInt& V, bool& isFeasible) { |
| 1084 | |
| 1085 | // First, determine if sym == X, where X != V. |
| 1086 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1087 | isFeasible = *X != V; |
| 1088 | return St; |
| 1089 | } |
| 1090 | |
| 1091 | // Second, determine if sym != V. |
| 1092 | if (St.isNotEqual(sym, V)) { |
| 1093 | isFeasible = true; |
| 1094 | return St; |
| 1095 | } |
| 1096 | |
| 1097 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 1098 | // Make that assumption. |
| 1099 | |
| 1100 | isFeasible = true; |
| 1101 | return StateMgr.AddNE(St, sym, V); |
| 1102 | } |
| 1103 | |
| 1104 | GRConstants::StateTy |
| 1105 | GRConstants::AssumeSymEQ(StateTy St, SymbolID sym, |
| 1106 | const llvm::APSInt& V, bool& isFeasible) { |
| 1107 | |
| 1108 | // First, determine if sym == X, where X != V. |
| 1109 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1110 | isFeasible = *X == V; |
| 1111 | return St; |
| 1112 | } |
| 1113 | |
| 1114 | // Second, determine if sym != V. |
| 1115 | if (St.isNotEqual(sym, V)) { |
| 1116 | isFeasible = false; |
| 1117 | return St; |
| 1118 | } |
| 1119 | |
| 1120 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 1121 | // Make that assumption. |
| 1122 | |
| 1123 | isFeasible = true; |
| 1124 | return StateMgr.AddEQ(St, sym, V); |
| 1125 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1126 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1127 | GRConstants::StateTy |
| 1128 | GRConstants::AssumeSymInt(StateTy St, bool Assumption, |
| 1129 | const SymIntConstraint& C, bool& isFeasible) { |
| 1130 | |
| 1131 | switch (C.getOpcode()) { |
| 1132 | default: |
| 1133 | // No logic yet for other operators. |
| 1134 | return St; |
| 1135 | |
| 1136 | case BinaryOperator::EQ: |
| 1137 | if (Assumption) |
| 1138 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1139 | else |
| 1140 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1141 | |
| 1142 | case BinaryOperator::NE: |
| 1143 | if (Assumption) |
| 1144 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1145 | else |
| 1146 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1147 | } |
| 1148 | } |
| 1149 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1150 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1151 | // Driver. |
| 1152 | //===----------------------------------------------------------------------===// |
| 1153 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1154 | #ifndef NDEBUG |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1155 | static GRConstants* GraphPrintCheckerState; |
| 1156 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1157 | namespace llvm { |
| 1158 | template<> |
| 1159 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> : |
| 1160 | public DefaultDOTGraphTraits { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1161 | |
| 1162 | static void PrintVarBindings(std::ostream& Out, GRConstants::StateTy St) { |
| 1163 | |
| 1164 | Out << "Variables:\\l"; |
| 1165 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1166 | bool isFirst = true; |
| 1167 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1168 | for (GRConstants::StateTy::vb_iterator I=St.vb_begin(), |
| 1169 | E=St.vb_end(); I!=E;++I) { |
| 1170 | |
| 1171 | if (isFirst) |
| 1172 | isFirst = false; |
| 1173 | else |
| 1174 | Out << "\\l"; |
| 1175 | |
| 1176 | Out << ' ' << I.getKey()->getName() << " : "; |
| 1177 | I.getData().print(Out); |
| 1178 | } |
| 1179 | |
| 1180 | } |
| 1181 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1182 | |
| 1183 | static void PrintSubExprBindings(std::ostream& Out, GRConstants::StateTy St) { |
| 1184 | |
| 1185 | bool isFirst = true; |
| 1186 | |
| 1187 | for (GRConstants::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end(); |
| 1188 | I != E;++I) { |
| 1189 | |
| 1190 | if (isFirst) { |
| 1191 | Out << "\\l\\lSub-Expressions:\\l"; |
| 1192 | isFirst = false; |
| 1193 | } |
| 1194 | else |
| 1195 | Out << "\\l"; |
| 1196 | |
| 1197 | Out << " (" << (void*) I.getKey() << ") "; |
| 1198 | I.getKey()->printPretty(Out); |
| 1199 | Out << " : "; |
| 1200 | I.getData().print(Out); |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | static void PrintBlkExprBindings(std::ostream& Out, GRConstants::StateTy St) { |
| 1205 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1206 | bool isFirst = true; |
| 1207 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1208 | for (GRConstants::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end(); |
| 1209 | I != E; ++I) { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1210 | if (isFirst) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1211 | Out << "\\l\\lBlock-level Expressions:\\l"; |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1212 | isFirst = false; |
| 1213 | } |
| 1214 | else |
| 1215 | Out << "\\l"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1216 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1217 | Out << " (" << (void*) I.getKey() << ") "; |
| 1218 | I.getKey()->printPretty(Out); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1219 | Out << " : "; |
| 1220 | I.getData().print(Out); |
| 1221 | } |
| 1222 | } |
| 1223 | |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1224 | static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) { |
| 1225 | ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq; |
| 1226 | |
| 1227 | if (CE.isEmpty()) |
| 1228 | return; |
| 1229 | |
| 1230 | Out << "\\l\\|'==' constraints:"; |
| 1231 | |
| 1232 | for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
| 1233 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1234 | } |
| 1235 | |
| 1236 | static void PrintNE(std::ostream& Out, GRConstants::StateTy St) { |
| 1237 | ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq; |
| 1238 | |
| 1239 | if (NE.isEmpty()) |
| 1240 | return; |
| 1241 | |
| 1242 | Out << "\\l\\|'!=' constraints:"; |
| 1243 | |
| 1244 | for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
| 1245 | I != EI; ++I){ |
| 1246 | |
| 1247 | Out << "\\l $" << I.getKey() << " : "; |
| 1248 | bool isFirst = true; |
| 1249 | |
| 1250 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1251 | EJ=I.getData().end(); |
| 1252 | for ( ; J != EJ; ++J) { |
| 1253 | if (isFirst) isFirst = false; |
| 1254 | else Out << ", "; |
| 1255 | |
| 1256 | Out << (*J)->toString(); |
| 1257 | } |
| 1258 | } |
| 1259 | } |
| 1260 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1261 | static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) { |
| 1262 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1263 | |
| 1264 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1265 | ProgramPoint Loc = N->getLocation(); |
| 1266 | |
| 1267 | switch (Loc.getKind()) { |
| 1268 | case ProgramPoint::BlockEntranceKind: |
| 1269 | Out << "Block Entrance: B" |
| 1270 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1271 | break; |
| 1272 | |
| 1273 | case ProgramPoint::BlockExitKind: |
| 1274 | assert (false); |
| 1275 | break; |
| 1276 | |
| 1277 | case ProgramPoint::PostStmtKind: { |
| 1278 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1279 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1280 | << (void*) L.getStmt() << ' '; |
| 1281 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1282 | L.getStmt()->printPretty(Out); |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1283 | |
| 1284 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) { |
| 1285 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 1286 | } |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1287 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) { |
| 1288 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 1289 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1290 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1291 | break; |
| 1292 | } |
| 1293 | |
| 1294 | default: { |
| 1295 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1296 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1297 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1298 | |
| 1299 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1300 | Out << "\\|Terminator: "; |
| 1301 | E.getSrc()->printTerminator(Out); |
| 1302 | |
| 1303 | if (isa<SwitchStmt>(T)) { |
| 1304 | // FIXME |
| 1305 | } |
| 1306 | else { |
| 1307 | Out << "\\lCondition: "; |
| 1308 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1309 | Out << "true"; |
| 1310 | else |
| 1311 | Out << "false"; |
| 1312 | } |
| 1313 | |
| 1314 | Out << "\\l"; |
| 1315 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1316 | |
| 1317 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1318 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1319 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1320 | } |
| 1321 | } |
| 1322 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1323 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1324 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1325 | N->getState().printDOT(Out); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1326 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1327 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1328 | return Out.str(); |
| 1329 | } |
| 1330 | }; |
| 1331 | } // end llvm namespace |
| 1332 | #endif |
| 1333 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1334 | namespace clang { |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 1335 | void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx, |
| 1336 | Diagnostic& Diag) { |
| 1337 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 1338 | GREngine<GRConstants> Engine(cfg, FD, Ctx); |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 1339 | Engine.ExecuteWorkList(); |
| 1340 | |
| 1341 | // Look for explicit-Null dereferences and warn about them. |
| 1342 | GRConstants* CheckerState = &Engine.getCheckerState(); |
| 1343 | |
| 1344 | for (GRConstants::null_iterator I=CheckerState->null_begin(), |
| 1345 | E=CheckerState->null_end(); I!=E; ++I) { |
| 1346 | |
| 1347 | const PostStmt& L = cast<PostStmt>((*I)->getLocation()); |
| 1348 | Expr* E = cast<Expr>(L.getStmt()); |
| 1349 | |
| 1350 | Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()), |
| 1351 | diag::chkr_null_deref_after_check); |
| 1352 | } |
| 1353 | |
| 1354 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1355 | #ifndef NDEBUG |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 1356 | GraphPrintCheckerState = CheckerState; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1357 | llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1358 | GraphPrintCheckerState = NULL; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1359 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1360 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1361 | } // end clang namespace |