2009-06-23 14:22:22 +08:00
|
|
|
//== ValueManager.cpp - Aggregate manager of symbols and SVals --*- C++ -*--==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines ValueManager, a class that manages symbolic values
|
|
|
|
// and SVals created for use by GRExprEngine and related classes. It
|
|
|
|
// wraps and owns SymbolManager, MemRegionManager, and BasicValueFactory.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-01-25 12:41:41 +08:00
|
|
|
#include "clang/Checker/PathSensitive/ValueManager.h"
|
|
|
|
#include "clang/Analysis/AnalysisContext.h"
|
2009-06-23 14:22:22 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility methods for constructing SVals.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-09-12 06:07:28 +08:00
|
|
|
DefinedOrUnknownSVal ValueManager::makeZeroVal(QualType T) {
|
2009-06-23 14:22:22 +08:00
|
|
|
if (Loc::IsLocType(T))
|
2009-06-23 17:02:15 +08:00
|
|
|
return makeNull();
|
2009-06-23 14:22:22 +08:00
|
|
|
|
|
|
|
if (T->isIntegerType())
|
2009-06-23 17:02:15 +08:00
|
|
|
return makeIntVal(0, T);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-23 14:22:22 +08:00
|
|
|
// FIXME: Handle floats.
|
|
|
|
// FIXME: Handle structs.
|
2009-09-09 23:08:12 +08:00
|
|
|
return UnknownVal();
|
2009-06-23 14:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility methods for constructing Non-Locs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
NonLoc ValueManager::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
|
|
|
|
const APSInt& v, QualType T) {
|
|
|
|
// The Environment ensures we always get a persistent APSInt in
|
|
|
|
// BasicValueFactory, so we don't need to get the APSInt from
|
|
|
|
// BasicValueFactory again.
|
|
|
|
assert(!Loc::IsLocType(T));
|
|
|
|
return nonloc::SymExprVal(SymMgr.getSymIntExpr(lhs, op, v, T));
|
|
|
|
}
|
|
|
|
|
|
|
|
NonLoc ValueManager::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
|
|
|
|
const SymExpr *rhs, QualType T) {
|
|
|
|
assert(SymMgr.getType(lhs) == SymMgr.getType(rhs));
|
|
|
|
assert(!Loc::IsLocType(T));
|
|
|
|
return nonloc::SymExprVal(SymMgr.getSymSymExpr(lhs, op, rhs, T));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-16 09:32:00 +08:00
|
|
|
SVal ValueManager::convertToArrayIndex(SVal V) {
|
2009-07-22 05:03:30 +08:00
|
|
|
if (V.isUnknownOrUndef())
|
|
|
|
return V;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-16 09:32:00 +08:00
|
|
|
// Common case: we have an appropriately sized integer.
|
|
|
|
if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&V)) {
|
|
|
|
const llvm::APSInt& I = CI->getValue();
|
|
|
|
if (I.getBitWidth() == ArrayIndexWidth && I.isSigned())
|
|
|
|
return V;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
return SVator->EvalCastNL(cast<NonLoc>(V), ArrayIndexTy);
|
2009-07-16 09:32:00 +08:00
|
|
|
}
|
|
|
|
|
2010-03-01 14:56:52 +08:00
|
|
|
DefinedOrUnknownSVal
|
|
|
|
ValueManager::getRegionValueSymbolVal(const TypedRegion* R) {
|
|
|
|
QualType T = R->getValueType(SymMgr.getContext());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
if (!SymbolManager::canSymbolicate(T))
|
|
|
|
return UnknownVal();
|
|
|
|
|
2010-03-01 14:56:52 +08:00
|
|
|
SymbolRef sym = SymMgr.getRegionValueSymbol(R);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
if (Loc::IsLocType(T))
|
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
return nonloc::SymbolVal(sym);
|
2009-06-23 14:22:22 +08:00
|
|
|
}
|
|
|
|
|
2009-09-28 04:45:21 +08:00
|
|
|
DefinedOrUnknownSVal ValueManager::getConjuredSymbolVal(const void *SymbolTag,
|
|
|
|
const Expr *E,
|
|
|
|
unsigned Count) {
|
2009-06-23 14:22:22 +08:00
|
|
|
QualType T = E->getType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
if (!SymbolManager::canSymbolicate(T))
|
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-28 04:45:21 +08:00
|
|
|
SymbolRef sym = SymMgr.getConjuredSymbol(E, Count, SymbolTag);
|
2009-06-23 14:22:22 +08:00
|
|
|
|
|
|
|
if (Loc::IsLocType(T))
|
2009-06-23 17:02:15 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
2009-06-23 14:22:22 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
return nonloc::SymbolVal(sym);
|
2009-06-23 14:22:22 +08:00
|
|
|
}
|
|
|
|
|
2009-09-28 04:45:21 +08:00
|
|
|
DefinedOrUnknownSVal ValueManager::getConjuredSymbolVal(const void *SymbolTag,
|
|
|
|
const Expr *E,
|
2009-09-12 06:07:28 +08:00
|
|
|
QualType T,
|
|
|
|
unsigned Count) {
|
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
if (!SymbolManager::canSymbolicate(T))
|
|
|
|
return UnknownVal();
|
2009-06-23 14:22:22 +08:00
|
|
|
|
2009-09-28 04:45:21 +08:00
|
|
|
SymbolRef sym = SymMgr.getConjuredSymbol(E, T, Count, SymbolTag);
|
2009-06-23 14:22:22 +08:00
|
|
|
|
|
|
|
if (Loc::IsLocType(T))
|
2009-06-23 17:02:15 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
2009-06-23 14:22:22 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
return nonloc::SymbolVal(sym);
|
2009-06-23 14:22:22 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 10:27:32 +08:00
|
|
|
|
2009-09-12 06:07:28 +08:00
|
|
|
DefinedOrUnknownSVal
|
|
|
|
ValueManager::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
|
|
|
|
const TypedRegion *R) {
|
2009-07-15 10:27:32 +08:00
|
|
|
QualType T = R->getValueType(R->getContext());
|
2009-08-01 14:17:29 +08:00
|
|
|
|
|
|
|
if (!SymbolManager::canSymbolicate(T))
|
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, R);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-15 10:27:32 +08:00
|
|
|
if (Loc::IsLocType(T))
|
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
return nonloc::SymbolVal(sym);
|
2009-07-15 10:27:32 +08:00
|
|
|
}
|
|
|
|
|
2009-09-12 06:07:28 +08:00
|
|
|
DefinedSVal ValueManager::getFunctionPointer(const FunctionDecl* FD) {
|
2009-12-04 08:26:31 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getFunctionTextRegion(FD));
|
2009-06-23 14:22:22 +08:00
|
|
|
}
|
2009-11-25 09:32:22 +08:00
|
|
|
|
2009-11-26 07:53:07 +08:00
|
|
|
DefinedSVal ValueManager::getBlockPointer(const BlockDecl *D,
|
|
|
|
CanQualType locTy,
|
|
|
|
const LocationContext *LC) {
|
2009-12-08 06:05:27 +08:00
|
|
|
const BlockTextRegion *BC =
|
|
|
|
MemMgr.getBlockTextRegion(D, locTy, LC->getAnalysisContext());
|
2009-12-04 08:26:31 +08:00
|
|
|
const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, LC);
|
2009-11-26 07:53:07 +08:00
|
|
|
return loc::MemRegionVal(BD);
|
2009-11-25 09:32:22 +08:00
|
|
|
}
|
|
|
|
|