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