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