2008-03-08 04:13:31 +08:00
|
|
|
//=== BasicValueFactory.cpp - Basic values for Path Sens analysis --*- C++ -*-//
|
2008-02-16 09:12:31 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-03-08 04:13:31 +08:00
|
|
|
// This file defines BasicValueFactory, a class that manages the lifetime
|
|
|
|
// of APSInt objects and symbolic constraints used by GRExprEngine
|
|
|
|
// and related classes.
|
2008-02-16 09:12:31 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-08 04:13:31 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/BasicValueFactory.h"
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-10-30 12:58:00 +08:00
|
|
|
void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T,
|
2008-10-31 01:44:46 +08:00
|
|
|
llvm::ImmutableList<SVal> L) {
|
2008-10-30 12:58:00 +08:00
|
|
|
T.Profile(ID);
|
2008-10-31 01:44:46 +08:00
|
|
|
ID.AddPointer(L.getInternalPointer());
|
2008-10-30 12:58:00 +08:00
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef std::pair<SVal, uintptr_t> SValData;
|
|
|
|
typedef std::pair<SVal, SVal> SValPair;
|
2008-04-30 07:24:44 +08:00
|
|
|
|
2008-04-23 05:10:18 +08:00
|
|
|
namespace llvm {
|
2008-10-17 13:57:07 +08:00
|
|
|
template<> struct FoldingSetTrait<SValData> {
|
|
|
|
static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) {
|
2008-04-23 05:10:18 +08:00
|
|
|
X.first.Profile(ID);
|
2008-04-30 06:17:41 +08:00
|
|
|
ID.AddPointer( (void*) X.second);
|
2008-04-23 05:10:18 +08:00
|
|
|
}
|
|
|
|
};
|
2008-04-30 07:24:44 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
template<> struct FoldingSetTrait<SValPair> {
|
|
|
|
static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) {
|
2008-04-30 07:24:44 +08:00
|
|
|
X.first.Profile(ID);
|
|
|
|
X.second.Profile(ID);
|
|
|
|
}
|
|
|
|
};
|
2008-04-23 05:10:18 +08:00
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData> >
|
|
|
|
PersistentSValsTy;
|
2008-04-23 05:10:18 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair> >
|
|
|
|
PersistentSValPairsTy;
|
2008-04-30 07:24:44 +08:00
|
|
|
|
2008-03-08 04:13:31 +08:00
|
|
|
BasicValueFactory::~BasicValueFactory() {
|
2008-02-16 09:12:31 +08:00
|
|
|
// Note that the dstor for the contents of APSIntSet will never be called,
|
|
|
|
// so we iterate over the set and invoke the dstor for each APSInt. This
|
|
|
|
// frees an aux. memory allocated to represent very large constants.
|
|
|
|
for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I)
|
|
|
|
I->getValue().~APSInt();
|
2008-04-23 05:10:18 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
delete (PersistentSValsTy*) PersistentSVals;
|
|
|
|
delete (PersistentSValPairsTy*) PersistentSValPairs;
|
2008-02-16 09:12:31 +08:00
|
|
|
}
|
|
|
|
|
2008-03-08 04:13:31 +08:00
|
|
|
const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) {
|
2008-02-16 09:12:31 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
void* InsertPos;
|
|
|
|
typedef llvm::FoldingSetNodeWrapper<llvm::APSInt> FoldNodeTy;
|
|
|
|
|
|
|
|
X.Profile(ID);
|
|
|
|
FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
|
|
|
|
if (!P) {
|
|
|
|
P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
|
|
|
|
new (P) FoldNodeTy(X);
|
|
|
|
APSIntSet.InsertNode(P, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *P;
|
|
|
|
}
|
|
|
|
|
2008-11-22 21:21:46 +08:00
|
|
|
const llvm::APSInt& BasicValueFactory::getValue(const llvm::APInt& X,
|
|
|
|
bool isUnsigned) {
|
|
|
|
llvm::APSInt V(X, isUnsigned);
|
|
|
|
return getValue(V);
|
|
|
|
}
|
|
|
|
|
2008-03-08 04:13:31 +08:00
|
|
|
const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth,
|
2008-02-16 09:12:31 +08:00
|
|
|
bool isUnsigned) {
|
|
|
|
llvm::APSInt V(BitWidth, isUnsigned);
|
|
|
|
V = X;
|
|
|
|
return getValue(V);
|
|
|
|
}
|
|
|
|
|
2008-03-08 04:13:31 +08:00
|
|
|
const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) {
|
2008-02-16 09:12:31 +08:00
|
|
|
|
2008-03-06 02:54:05 +08:00
|
|
|
unsigned bits = Ctx.getTypeSize(T);
|
2008-02-16 09:12:31 +08:00
|
|
|
llvm::APSInt V(bits, T->isUnsignedIntegerType());
|
|
|
|
V = X;
|
|
|
|
return getValue(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
const SymIntConstraint&
|
2008-12-05 10:27:51 +08:00
|
|
|
BasicValueFactory::getConstraint(SymbolRef sym, BinaryOperator::Opcode Op,
|
2008-02-16 09:12:31 +08:00
|
|
|
const llvm::APSInt& V) {
|
|
|
|
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
SymIntConstraint::Profile(ID, sym, Op, V);
|
|
|
|
void* InsertPos;
|
|
|
|
|
|
|
|
SymIntConstraint* C = SymIntCSet.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
|
|
|
|
if (!C) {
|
|
|
|
C = (SymIntConstraint*) BPAlloc.Allocate<SymIntConstraint>();
|
|
|
|
new (C) SymIntConstraint(sym, Op, V);
|
|
|
|
SymIntCSet.InsertNode(C, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *C;
|
|
|
|
}
|
|
|
|
|
2008-10-30 12:58:00 +08:00
|
|
|
const CompoundValData*
|
2008-10-31 01:44:46 +08:00
|
|
|
BasicValueFactory::getCompoundValData(QualType T,
|
|
|
|
llvm::ImmutableList<SVal> Vals) {
|
|
|
|
|
2008-10-30 12:58:00 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2008-10-31 01:44:46 +08:00
|
|
|
CompoundValData::Profile(ID, T, Vals);
|
2008-10-30 12:58:00 +08:00
|
|
|
void* InsertPos;
|
|
|
|
|
|
|
|
CompoundValData* D = CompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
|
|
|
|
if (!D) {
|
|
|
|
D = (CompoundValData*) BPAlloc.Allocate<CompoundValData>();
|
2008-10-31 01:44:46 +08:00
|
|
|
new (D) CompoundValData(T, Vals);
|
2008-10-30 12:58:00 +08:00
|
|
|
CompoundValDataSet.InsertNode(D, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
const llvm::APSInt*
|
2008-03-08 04:13:31 +08:00
|
|
|
BasicValueFactory::EvaluateAPSInt(BinaryOperator::Opcode Op,
|
2008-02-16 09:12:31 +08:00
|
|
|
const llvm::APSInt& V1, const llvm::APSInt& V2) {
|
|
|
|
|
|
|
|
switch (Op) {
|
|
|
|
default:
|
|
|
|
assert (false && "Invalid Opcode.");
|
|
|
|
|
|
|
|
case BinaryOperator::Mul:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 * V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
case BinaryOperator::Div:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 / V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
case BinaryOperator::Rem:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 % V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
case BinaryOperator::Add:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 + V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
case BinaryOperator::Sub:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 - V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
case BinaryOperator::Shl: {
|
|
|
|
|
|
|
|
// FIXME: This logic should probably go higher up, where we can
|
|
|
|
// test these conditions symbolically.
|
|
|
|
|
|
|
|
// FIXME: Expand these checks to include all undefined behavior.
|
|
|
|
|
|
|
|
if (V2.isSigned() && V2.isNegative())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
uint64_t Amt = V2.getZExtValue();
|
|
|
|
|
|
|
|
if (Amt > V1.getBitWidth())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return &getValue( V1.operator<<( (unsigned) Amt ));
|
|
|
|
}
|
|
|
|
|
|
|
|
case BinaryOperator::Shr: {
|
|
|
|
|
|
|
|
// FIXME: This logic should probably go higher up, where we can
|
|
|
|
// test these conditions symbolically.
|
|
|
|
|
|
|
|
// FIXME: Expand these checks to include all undefined behavior.
|
|
|
|
|
|
|
|
if (V2.isSigned() && V2.isNegative())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
uint64_t Amt = V2.getZExtValue();
|
|
|
|
|
|
|
|
if (Amt > V1.getBitWidth())
|
|
|
|
return NULL;
|
2008-02-16 09:12:31 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1.operator>>( (unsigned) Amt ));
|
|
|
|
}
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
case BinaryOperator::LT:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getTruthValue( V1 < V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
case BinaryOperator::GT:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getTruthValue( V1 > V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
case BinaryOperator::LE:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getTruthValue( V1 <= V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
case BinaryOperator::GE:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getTruthValue( V1 >= V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
case BinaryOperator::EQ:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getTruthValue( V1 == V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
case BinaryOperator::NE:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getTruthValue( V1 != V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
// Note: LAnd, LOr, Comma are handled specially by higher-level logic.
|
|
|
|
|
|
|
|
case BinaryOperator::And:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 & V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
case BinaryOperator::Or:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 | V2 );
|
2008-02-20 04:53:37 +08:00
|
|
|
|
|
|
|
case BinaryOperator::Xor:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 ^ V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
}
|
|
|
|
}
|
2008-04-23 05:10:18 +08:00
|
|
|
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
const std::pair<SVal, uintptr_t>&
|
|
|
|
BasicValueFactory::getPersistentSValWithData(const SVal& V, uintptr_t Data) {
|
2008-04-23 05:10:18 +08:00
|
|
|
|
|
|
|
// Lazily create the folding set.
|
2008-10-17 13:57:07 +08:00
|
|
|
if (!PersistentSVals) PersistentSVals = new PersistentSValsTy();
|
2008-04-23 05:10:18 +08:00
|
|
|
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
void* InsertPos;
|
|
|
|
V.Profile(ID);
|
2008-04-30 06:17:41 +08:00
|
|
|
ID.AddPointer((void*) Data);
|
2008-04-23 05:10:18 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals);
|
2008-04-23 05:10:18 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef llvm::FoldingSetNodeWrapper<SValData> FoldNodeTy;
|
2008-04-23 05:10:18 +08:00
|
|
|
FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
|
|
|
|
if (!P) {
|
|
|
|
P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
|
2008-04-30 06:17:41 +08:00
|
|
|
new (P) FoldNodeTy(std::make_pair(V, Data));
|
2008-04-23 05:10:18 +08:00
|
|
|
Map.InsertNode(P, InsertPos);
|
|
|
|
}
|
|
|
|
|
2008-04-30 06:17:41 +08:00
|
|
|
return P->getValue();
|
2008-04-23 05:10:18 +08:00
|
|
|
}
|
2008-04-30 07:24:44 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
const std::pair<SVal, SVal>&
|
|
|
|
BasicValueFactory::getPersistentSValPair(const SVal& V1, const SVal& V2) {
|
2008-04-30 07:24:44 +08:00
|
|
|
|
|
|
|
// Lazily create the folding set.
|
2008-10-17 13:57:07 +08:00
|
|
|
if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy();
|
2008-04-30 07:24:44 +08:00
|
|
|
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
void* InsertPos;
|
|
|
|
V1.Profile(ID);
|
|
|
|
V2.Profile(ID);
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs);
|
2008-04-30 07:24:44 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef llvm::FoldingSetNodeWrapper<SValPair> FoldNodeTy;
|
2008-04-30 07:24:44 +08:00
|
|
|
FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
|
|
|
|
if (!P) {
|
|
|
|
P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
|
|
|
|
new (P) FoldNodeTy(std::make_pair(V1, V2));
|
|
|
|
Map.InsertNode(P, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return P->getValue();
|
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
const SVal* BasicValueFactory::getPersistentSVal(SVal X) {
|
|
|
|
return &getPersistentSValWithData(X, 0).first;
|
2008-09-19 07:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|