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 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 20 | BasicValueFactory::~BasicValueFactory() { |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 21 | // Note that the dstor for the contents of APSIntSet will never be called, |
| 22 | // so we iterate over the set and invoke the dstor for each APSInt. This |
| 23 | // frees an aux. memory allocated to represent very large constants. |
| 24 | for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I) |
| 25 | I->getValue().~APSInt(); |
| 26 | } |
| 27 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 28 | const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) { |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 29 | llvm::FoldingSetNodeID ID; |
| 30 | void* InsertPos; |
| 31 | typedef llvm::FoldingSetNodeWrapper<llvm::APSInt> FoldNodeTy; |
| 32 | |
| 33 | X.Profile(ID); |
| 34 | FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos); |
| 35 | |
| 36 | if (!P) { |
| 37 | P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>(); |
| 38 | new (P) FoldNodeTy(X); |
| 39 | APSIntSet.InsertNode(P, InsertPos); |
| 40 | } |
| 41 | |
| 42 | return *P; |
| 43 | } |
| 44 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 45 | const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth, |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 46 | bool isUnsigned) { |
| 47 | llvm::APSInt V(BitWidth, isUnsigned); |
| 48 | V = X; |
| 49 | return getValue(V); |
| 50 | } |
| 51 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 52 | const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) { |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 54 | unsigned bits = Ctx.getTypeSize(T); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 55 | llvm::APSInt V(bits, T->isUnsignedIntegerType()); |
| 56 | V = X; |
| 57 | return getValue(V); |
| 58 | } |
| 59 | |
| 60 | const SymIntConstraint& |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 61 | BasicValueFactory::getConstraint(SymbolID sym, BinaryOperator::Opcode Op, |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 62 | const llvm::APSInt& V) { |
| 63 | |
| 64 | llvm::FoldingSetNodeID ID; |
| 65 | SymIntConstraint::Profile(ID, sym, Op, V); |
| 66 | void* InsertPos; |
| 67 | |
| 68 | SymIntConstraint* C = SymIntCSet.FindNodeOrInsertPos(ID, InsertPos); |
| 69 | |
| 70 | if (!C) { |
| 71 | C = (SymIntConstraint*) BPAlloc.Allocate<SymIntConstraint>(); |
| 72 | new (C) SymIntConstraint(sym, Op, V); |
| 73 | SymIntCSet.InsertNode(C, InsertPos); |
| 74 | } |
| 75 | |
| 76 | return *C; |
| 77 | } |
| 78 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 79 | const llvm::APSInt* |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 80 | BasicValueFactory::EvaluateAPSInt(BinaryOperator::Opcode Op, |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 81 | const llvm::APSInt& V1, const llvm::APSInt& V2) { |
| 82 | |
| 83 | switch (Op) { |
| 84 | default: |
| 85 | assert (false && "Invalid Opcode."); |
| 86 | |
| 87 | case BinaryOperator::Mul: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 88 | return &getValue( V1 * V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 89 | |
| 90 | case BinaryOperator::Div: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 91 | return &getValue( V1 / V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 92 | |
| 93 | case BinaryOperator::Rem: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 94 | return &getValue( V1 % V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 95 | |
| 96 | case BinaryOperator::Add: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 97 | return &getValue( V1 + V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 98 | |
| 99 | case BinaryOperator::Sub: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 100 | return &getValue( V1 - V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 102 | case BinaryOperator::Shl: { |
| 103 | |
| 104 | // FIXME: This logic should probably go higher up, where we can |
| 105 | // test these conditions symbolically. |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 107 | // FIXME: Expand these checks to include all undefined behavior. |
| 108 | |
| 109 | if (V2.isSigned() && V2.isNegative()) |
| 110 | return NULL; |
| 111 | |
| 112 | uint64_t Amt = V2.getZExtValue(); |
| 113 | |
| 114 | if (Amt > V1.getBitWidth()) |
| 115 | return NULL; |
| 116 | |
| 117 | return &getValue( V1.operator<<( (unsigned) Amt )); |
| 118 | } |
| 119 | |
| 120 | case BinaryOperator::Shr: { |
| 121 | |
| 122 | // FIXME: This logic should probably go higher up, where we can |
| 123 | // test these conditions symbolically. |
| 124 | |
| 125 | // FIXME: Expand these checks to include all undefined behavior. |
| 126 | |
| 127 | if (V2.isSigned() && V2.isNegative()) |
| 128 | return NULL; |
| 129 | |
| 130 | uint64_t Amt = V2.getZExtValue(); |
| 131 | |
| 132 | if (Amt > V1.getBitWidth()) |
| 133 | return NULL; |
| 134 | |
| 135 | return &getValue( V1.operator>>( (unsigned) Amt )); |
| 136 | } |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 137 | |
| 138 | case BinaryOperator::LT: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 139 | return &getTruthValue( V1 < V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 140 | |
| 141 | case BinaryOperator::GT: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 142 | return &getTruthValue( V1 > V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 143 | |
| 144 | case BinaryOperator::LE: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 145 | return &getTruthValue( V1 <= V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 146 | |
| 147 | case BinaryOperator::GE: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 148 | return &getTruthValue( V1 >= V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 149 | |
| 150 | case BinaryOperator::EQ: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 151 | return &getTruthValue( V1 == V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 152 | |
| 153 | case BinaryOperator::NE: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 154 | return &getTruthValue( V1 != V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 155 | |
| 156 | // Note: LAnd, LOr, Comma are handled specially by higher-level logic. |
| 157 | |
| 158 | case BinaryOperator::And: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 159 | return &getValue( V1 & V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 160 | |
| 161 | case BinaryOperator::Or: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 162 | return &getValue( V1 | V2 ); |
Ted Kremenek | 1caf26a | 2008-02-19 20:53:37 +0000 | [diff] [blame] | 163 | |
| 164 | case BinaryOperator::Xor: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 165 | return &getValue( V1 ^ V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 166 | } |
| 167 | } |