Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 1 | //=== BasicValueFactory.cpp - Basic values for Path Sens analysis --*- C++ -*-// |
Ted Kremenek | 94e915e | 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 | 8ad1987 | 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 | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/BasicValueFactory.h" |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/RValues.h" |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
| 20 | |
Ted Kremenek | 465f25a | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 21 | typedef std::pair<RVal, uintptr_t> RValData; |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
Ted Kremenek | 465f25a | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 24 | template<> struct FoldingSetTrait<RValData> { |
| 25 | static inline void Profile(const RValData& X, llvm::FoldingSetNodeID& ID) { |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 26 | X.first.Profile(ID); |
Ted Kremenek | 465f25a | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 27 | ID.AddPointer( (void*) X.second); |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 28 | } |
| 29 | }; |
| 30 | } |
| 31 | |
Ted Kremenek | 465f25a | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 32 | typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<RValData> > |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 33 | PersistentRValsTy; |
| 34 | |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 35 | BasicValueFactory::~BasicValueFactory() { |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 36 | // Note that the dstor for the contents of APSIntSet will never be called, |
| 37 | // so we iterate over the set and invoke the dstor for each APSInt. This |
| 38 | // frees an aux. memory allocated to represent very large constants. |
| 39 | for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I) |
| 40 | I->getValue().~APSInt(); |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 41 | |
| 42 | delete (PersistentRValsTy*) PersistentRVals; |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 45 | const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) { |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 46 | llvm::FoldingSetNodeID ID; |
| 47 | void* InsertPos; |
| 48 | typedef llvm::FoldingSetNodeWrapper<llvm::APSInt> FoldNodeTy; |
| 49 | |
| 50 | X.Profile(ID); |
| 51 | FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos); |
| 52 | |
| 53 | if (!P) { |
| 54 | P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>(); |
| 55 | new (P) FoldNodeTy(X); |
| 56 | APSIntSet.InsertNode(P, InsertPos); |
| 57 | } |
| 58 | |
| 59 | return *P; |
| 60 | } |
| 61 | |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 62 | const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth, |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 63 | bool isUnsigned) { |
| 64 | llvm::APSInt V(BitWidth, isUnsigned); |
| 65 | V = X; |
| 66 | return getValue(V); |
| 67 | } |
| 68 | |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 69 | const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) { |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 71 | unsigned bits = Ctx.getTypeSize(T); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 72 | llvm::APSInt V(bits, T->isUnsignedIntegerType()); |
| 73 | V = X; |
| 74 | return getValue(V); |
| 75 | } |
| 76 | |
| 77 | const SymIntConstraint& |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 78 | BasicValueFactory::getConstraint(SymbolID sym, BinaryOperator::Opcode Op, |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 79 | const llvm::APSInt& V) { |
| 80 | |
| 81 | llvm::FoldingSetNodeID ID; |
| 82 | SymIntConstraint::Profile(ID, sym, Op, V); |
| 83 | void* InsertPos; |
| 84 | |
| 85 | SymIntConstraint* C = SymIntCSet.FindNodeOrInsertPos(ID, InsertPos); |
| 86 | |
| 87 | if (!C) { |
| 88 | C = (SymIntConstraint*) BPAlloc.Allocate<SymIntConstraint>(); |
| 89 | new (C) SymIntConstraint(sym, Op, V); |
| 90 | SymIntCSet.InsertNode(C, InsertPos); |
| 91 | } |
| 92 | |
| 93 | return *C; |
| 94 | } |
| 95 | |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 96 | const llvm::APSInt* |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 97 | BasicValueFactory::EvaluateAPSInt(BinaryOperator::Opcode Op, |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 98 | const llvm::APSInt& V1, const llvm::APSInt& V2) { |
| 99 | |
| 100 | switch (Op) { |
| 101 | default: |
| 102 | assert (false && "Invalid Opcode."); |
| 103 | |
| 104 | case BinaryOperator::Mul: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 105 | return &getValue( V1 * V2 ); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 106 | |
| 107 | case BinaryOperator::Div: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 108 | return &getValue( V1 / V2 ); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 109 | |
| 110 | case BinaryOperator::Rem: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 111 | return &getValue( V1 % V2 ); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 112 | |
| 113 | case BinaryOperator::Add: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 114 | return &getValue( V1 + V2 ); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 115 | |
| 116 | case BinaryOperator::Sub: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 117 | return &getValue( V1 - V2 ); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 119 | case BinaryOperator::Shl: { |
| 120 | |
| 121 | // FIXME: This logic should probably go higher up, where we can |
| 122 | // test these conditions symbolically. |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 124 | // FIXME: Expand these checks to include all undefined behavior. |
| 125 | |
| 126 | if (V2.isSigned() && V2.isNegative()) |
| 127 | return NULL; |
| 128 | |
| 129 | uint64_t Amt = V2.getZExtValue(); |
| 130 | |
| 131 | if (Amt > V1.getBitWidth()) |
| 132 | return NULL; |
| 133 | |
| 134 | return &getValue( V1.operator<<( (unsigned) Amt )); |
| 135 | } |
| 136 | |
| 137 | case BinaryOperator::Shr: { |
| 138 | |
| 139 | // FIXME: This logic should probably go higher up, where we can |
| 140 | // test these conditions symbolically. |
| 141 | |
| 142 | // FIXME: Expand these checks to include all undefined behavior. |
| 143 | |
| 144 | if (V2.isSigned() && V2.isNegative()) |
| 145 | return NULL; |
| 146 | |
| 147 | uint64_t Amt = V2.getZExtValue(); |
| 148 | |
| 149 | if (Amt > V1.getBitWidth()) |
| 150 | return NULL; |
| 151 | |
| 152 | return &getValue( V1.operator>>( (unsigned) Amt )); |
| 153 | } |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 154 | |
| 155 | case BinaryOperator::LT: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 156 | return &getTruthValue( V1 < V2 ); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 157 | |
| 158 | case BinaryOperator::GT: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 159 | return &getTruthValue( V1 > V2 ); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 160 | |
| 161 | case BinaryOperator::LE: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 162 | return &getTruthValue( V1 <= V2 ); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 163 | |
| 164 | case BinaryOperator::GE: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 165 | return &getTruthValue( V1 >= V2 ); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 166 | |
| 167 | case BinaryOperator::EQ: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 168 | return &getTruthValue( V1 == V2 ); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 169 | |
| 170 | case BinaryOperator::NE: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 171 | return &getTruthValue( V1 != V2 ); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 172 | |
| 173 | // Note: LAnd, LOr, Comma are handled specially by higher-level logic. |
| 174 | |
| 175 | case BinaryOperator::And: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 176 | return &getValue( V1 & V2 ); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 177 | |
| 178 | case BinaryOperator::Or: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 179 | return &getValue( V1 | V2 ); |
Ted Kremenek | 7c79a79 | 2008-02-19 20:53:37 +0000 | [diff] [blame] | 180 | |
| 181 | case BinaryOperator::Xor: |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 182 | return &getValue( V1 ^ V2 ); |
Ted Kremenek | 94e915e | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 185 | |
| 186 | |
Ted Kremenek | 465f25a | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 187 | const std::pair<RVal, uintptr_t>& |
| 188 | BasicValueFactory::getPersistentRValWithData(const RVal& V, uintptr_t Data) { |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 189 | |
| 190 | // Lazily create the folding set. |
| 191 | if (!PersistentRVals) PersistentRVals = new PersistentRValsTy(); |
| 192 | |
| 193 | llvm::FoldingSetNodeID ID; |
| 194 | void* InsertPos; |
| 195 | V.Profile(ID); |
Ted Kremenek | 465f25a | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 196 | ID.AddPointer((void*) Data); |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 197 | |
| 198 | PersistentRValsTy& Map = *((PersistentRValsTy*) PersistentRVals); |
| 199 | |
Ted Kremenek | 465f25a | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 200 | typedef llvm::FoldingSetNodeWrapper<RValData> FoldNodeTy; |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 201 | FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos); |
| 202 | |
| 203 | if (!P) { |
| 204 | P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>(); |
Ted Kremenek | 465f25a | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 205 | new (P) FoldNodeTy(std::make_pair(V, Data)); |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 206 | Map.InsertNode(P, InsertPos); |
| 207 | } |
| 208 | |
Ted Kremenek | 465f25a | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 209 | return P->getValue(); |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 210 | } |