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