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