Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 1 | //=== BasicValueFactory.cpp - Basic values for Path Sens analysis --*- C++ -*-// |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 10 | // This file defines BasicValueFactory, a class that manages the lifetime |
| 11 | // of APSInt objects and symbolic constraints used by GRExprEngine |
| 12 | // and related classes. |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/BasicValueFactory.h" |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
| 19 | |
Zhongxing Xu | 6764b72 | 2008-10-30 04:58:00 +0000 | [diff] [blame] | 20 | void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T, |
Ted Kremenek | 632e8b8 | 2008-10-30 17:44:46 +0000 | [diff] [blame] | 21 | llvm::ImmutableList<SVal> L) { |
Zhongxing Xu | 6764b72 | 2008-10-30 04:58:00 +0000 | [diff] [blame] | 22 | T.Profile(ID); |
Ted Kremenek | 632e8b8 | 2008-10-30 17:44:46 +0000 | [diff] [blame] | 23 | ID.AddPointer(L.getInternalPointer()); |
Zhongxing Xu | 6764b72 | 2008-10-30 04:58:00 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 26 | typedef std::pair<SVal, uintptr_t> SValData; |
| 27 | typedef std::pair<SVal, SVal> SValPair; |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 29 | namespace llvm { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 30 | template<> struct FoldingSetTrait<SValData> { |
| 31 | static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) { |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 32 | X.first.Profile(ID); |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 33 | ID.AddPointer( (void*) X.second); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 34 | } |
| 35 | }; |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 36 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 37 | template<> struct FoldingSetTrait<SValPair> { |
| 38 | static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) { |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 39 | X.first.Profile(ID); |
| 40 | X.second.Profile(ID); |
| 41 | } |
| 42 | }; |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 45 | typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData> > |
| 46 | PersistentSValsTy; |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 47 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 48 | typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair> > |
| 49 | PersistentSValPairsTy; |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 51 | BasicValueFactory::~BasicValueFactory() { |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 52 | // Note that the dstor for the contents of APSIntSet will never be called, |
| 53 | // so we iterate over the set and invoke the dstor for each APSInt. This |
| 54 | // frees an aux. memory allocated to represent very large constants. |
| 55 | for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I) |
| 56 | I->getValue().~APSInt(); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 57 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 58 | delete (PersistentSValsTy*) PersistentSVals; |
| 59 | delete (PersistentSValPairsTy*) PersistentSValPairs; |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 62 | const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) { |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 63 | llvm::FoldingSetNodeID ID; |
| 64 | void* InsertPos; |
| 65 | typedef llvm::FoldingSetNodeWrapper<llvm::APSInt> FoldNodeTy; |
| 66 | |
| 67 | X.Profile(ID); |
| 68 | FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos); |
| 69 | |
| 70 | if (!P) { |
| 71 | P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>(); |
| 72 | new (P) FoldNodeTy(X); |
| 73 | APSIntSet.InsertNode(P, InsertPos); |
| 74 | } |
| 75 | |
| 76 | return *P; |
| 77 | } |
| 78 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 79 | const llvm::APSInt& BasicValueFactory::getValue(const llvm::APInt& X, |
| 80 | bool isUnsigned) { |
| 81 | llvm::APSInt V(X, isUnsigned); |
| 82 | return getValue(V); |
| 83 | } |
| 84 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 85 | const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth, |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 86 | bool isUnsigned) { |
| 87 | llvm::APSInt V(BitWidth, isUnsigned); |
| 88 | V = X; |
| 89 | return getValue(V); |
| 90 | } |
| 91 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 92 | const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) { |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 94 | unsigned bits = Ctx.getTypeSize(T); |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 95 | llvm::APSInt V(bits, T->isUnsignedIntegerType() || Loc::IsLocType(T)); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 96 | V = X; |
| 97 | return getValue(V); |
| 98 | } |
| 99 | |
Zhongxing Xu | 6764b72 | 2008-10-30 04:58:00 +0000 | [diff] [blame] | 100 | const CompoundValData* |
Ted Kremenek | 632e8b8 | 2008-10-30 17:44:46 +0000 | [diff] [blame] | 101 | BasicValueFactory::getCompoundValData(QualType T, |
| 102 | llvm::ImmutableList<SVal> Vals) { |
| 103 | |
Zhongxing Xu | 6764b72 | 2008-10-30 04:58:00 +0000 | [diff] [blame] | 104 | llvm::FoldingSetNodeID ID; |
Ted Kremenek | 632e8b8 | 2008-10-30 17:44:46 +0000 | [diff] [blame] | 105 | CompoundValData::Profile(ID, T, Vals); |
Zhongxing Xu | 6764b72 | 2008-10-30 04:58:00 +0000 | [diff] [blame] | 106 | void* InsertPos; |
| 107 | |
| 108 | CompoundValData* D = CompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos); |
| 109 | |
| 110 | if (!D) { |
| 111 | D = (CompoundValData*) BPAlloc.Allocate<CompoundValData>(); |
Ted Kremenek | 632e8b8 | 2008-10-30 17:44:46 +0000 | [diff] [blame] | 112 | new (D) CompoundValData(T, Vals); |
Zhongxing Xu | 6764b72 | 2008-10-30 04:58:00 +0000 | [diff] [blame] | 113 | CompoundValDataSet.InsertNode(D, InsertPos); |
| 114 | } |
| 115 | |
| 116 | return D; |
| 117 | } |
| 118 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 119 | const llvm::APSInt* |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 120 | BasicValueFactory::EvaluateAPSInt(BinaryOperator::Opcode Op, |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 121 | const llvm::APSInt& V1, const llvm::APSInt& V2) { |
| 122 | |
| 123 | switch (Op) { |
| 124 | default: |
| 125 | assert (false && "Invalid Opcode."); |
| 126 | |
| 127 | case BinaryOperator::Mul: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 128 | return &getValue( V1 * V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 129 | |
| 130 | case BinaryOperator::Div: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 131 | return &getValue( V1 / V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 132 | |
| 133 | case BinaryOperator::Rem: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 134 | return &getValue( V1 % V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 135 | |
| 136 | case BinaryOperator::Add: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 137 | return &getValue( V1 + V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 138 | |
| 139 | case BinaryOperator::Sub: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 140 | return &getValue( V1 - V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 142 | case BinaryOperator::Shl: { |
| 143 | |
| 144 | // FIXME: This logic should probably go higher up, where we can |
| 145 | // test these conditions symbolically. |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 147 | // FIXME: Expand these checks to include all undefined behavior. |
| 148 | |
| 149 | if (V2.isSigned() && V2.isNegative()) |
| 150 | return NULL; |
| 151 | |
| 152 | uint64_t Amt = V2.getZExtValue(); |
| 153 | |
| 154 | if (Amt > V1.getBitWidth()) |
| 155 | return NULL; |
| 156 | |
| 157 | return &getValue( V1.operator<<( (unsigned) Amt )); |
| 158 | } |
| 159 | |
| 160 | case BinaryOperator::Shr: { |
| 161 | |
| 162 | // FIXME: This logic should probably go higher up, where we can |
| 163 | // test these conditions symbolically. |
| 164 | |
| 165 | // FIXME: Expand these checks to include all undefined behavior. |
| 166 | |
| 167 | if (V2.isSigned() && V2.isNegative()) |
| 168 | return NULL; |
| 169 | |
| 170 | uint64_t Amt = V2.getZExtValue(); |
| 171 | |
| 172 | if (Amt > V1.getBitWidth()) |
| 173 | return NULL; |
| 174 | |
| 175 | return &getValue( V1.operator>>( (unsigned) Amt )); |
| 176 | } |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 177 | |
| 178 | case BinaryOperator::LT: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 179 | return &getTruthValue( V1 < V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 180 | |
| 181 | case BinaryOperator::GT: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 182 | return &getTruthValue( V1 > V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 183 | |
| 184 | case BinaryOperator::LE: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 185 | return &getTruthValue( V1 <= V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 186 | |
| 187 | case BinaryOperator::GE: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 188 | return &getTruthValue( V1 >= V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 189 | |
| 190 | case BinaryOperator::EQ: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 191 | return &getTruthValue( V1 == V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 192 | |
| 193 | case BinaryOperator::NE: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 194 | return &getTruthValue( V1 != V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 195 | |
| 196 | // Note: LAnd, LOr, Comma are handled specially by higher-level logic. |
| 197 | |
| 198 | case BinaryOperator::And: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 199 | return &getValue( V1 & V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 200 | |
| 201 | case BinaryOperator::Or: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 202 | return &getValue( V1 | V2 ); |
Ted Kremenek | 1caf26a | 2008-02-19 20:53:37 +0000 | [diff] [blame] | 203 | |
| 204 | case BinaryOperator::Xor: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 205 | return &getValue( V1 ^ V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 208 | |
| 209 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 210 | const std::pair<SVal, uintptr_t>& |
| 211 | BasicValueFactory::getPersistentSValWithData(const SVal& V, uintptr_t Data) { |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 212 | |
| 213 | // Lazily create the folding set. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 214 | if (!PersistentSVals) PersistentSVals = new PersistentSValsTy(); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 215 | |
| 216 | llvm::FoldingSetNodeID ID; |
| 217 | void* InsertPos; |
| 218 | V.Profile(ID); |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 219 | ID.AddPointer((void*) Data); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 220 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 221 | PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 222 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 223 | typedef llvm::FoldingSetNodeWrapper<SValData> FoldNodeTy; |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 224 | FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos); |
| 225 | |
| 226 | if (!P) { |
| 227 | P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>(); |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 228 | new (P) FoldNodeTy(std::make_pair(V, Data)); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 229 | Map.InsertNode(P, InsertPos); |
| 230 | } |
| 231 | |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 232 | return P->getValue(); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 233 | } |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 234 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 235 | const std::pair<SVal, SVal>& |
| 236 | BasicValueFactory::getPersistentSValPair(const SVal& V1, const SVal& V2) { |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 237 | |
| 238 | // Lazily create the folding set. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 239 | if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy(); |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 240 | |
| 241 | llvm::FoldingSetNodeID ID; |
| 242 | void* InsertPos; |
| 243 | V1.Profile(ID); |
| 244 | V2.Profile(ID); |
| 245 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 246 | PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs); |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 247 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 248 | typedef llvm::FoldingSetNodeWrapper<SValPair> FoldNodeTy; |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 249 | FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos); |
| 250 | |
| 251 | if (!P) { |
| 252 | P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>(); |
| 253 | new (P) FoldNodeTy(std::make_pair(V1, V2)); |
| 254 | Map.InsertNode(P, InsertPos); |
| 255 | } |
| 256 | |
| 257 | return P->getValue(); |
| 258 | } |
| 259 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 260 | const SVal* BasicValueFactory::getPersistentSVal(SVal X) { |
| 261 | return &getPersistentSValWithData(X, 0).first; |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | |