2008-07-11 06:03:41 +08:00
|
|
|
//== BasicStore.cpp - Basic map from Locations to Values --------*- C++ -*--==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defined the BasicStore and BasicStoreManager classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-08-29 07:31:31 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2008-08-20 00:51:45 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRState.h"
|
2008-07-11 06:03:41 +08:00
|
|
|
#include "llvm/ADT/ImmutableMap.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-08-20 06:24:03 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2008-07-11 06:03:41 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef llvm::ImmutableMap<const VarDecl*,SVal> VarBindingsTy;
|
2008-09-03 11:06:11 +08:00
|
|
|
|
2008-07-11 06:03:41 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
|
|
|
|
VarBindingsTy::Factory VBFactory;
|
2008-10-05 20:12:48 +08:00
|
|
|
GRStateManager& StateMgr;
|
2008-10-07 09:31:04 +08:00
|
|
|
MemRegionManager MRMgr;
|
2008-07-11 06:03:41 +08:00
|
|
|
|
|
|
|
public:
|
2008-10-07 09:31:04 +08:00
|
|
|
BasicStoreManager(GRStateManager& mgr)
|
|
|
|
: StateMgr(mgr), MRMgr(StateMgr.getAllocator()) {}
|
2008-08-26 03:33:03 +08:00
|
|
|
|
2008-07-11 06:03:41 +08:00
|
|
|
virtual ~BasicStoreManager() {}
|
|
|
|
|
2008-10-21 14:27:32 +08:00
|
|
|
virtual SVal Retrieve(Store St, Loc LV, QualType T);
|
|
|
|
virtual Store Bind(Store St, Loc LV, SVal V);
|
2008-10-17 13:57:07 +08:00
|
|
|
virtual Store Remove(Store St, Loc LV);
|
2008-07-11 06:03:41 +08:00
|
|
|
|
2008-10-05 20:12:48 +08:00
|
|
|
virtual Store getInitialStore();
|
2008-10-07 09:31:04 +08:00
|
|
|
|
|
|
|
virtual MemRegionManager& getRegionManager() { return MRMgr; }
|
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
// FIXME: Investigate what is using this. This method should be removed.
|
2008-10-17 13:57:07 +08:00
|
|
|
virtual Loc getLoc(const VarDecl* VD) {
|
|
|
|
return loc::MemRegionVal(MRMgr.getVarRegion(VD));
|
2008-10-07 09:31:04 +08:00
|
|
|
}
|
2008-10-17 08:51:01 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal getLValueVar(const GRState* St, const VarDecl* VD);
|
|
|
|
SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
|
2008-10-22 17:00:19 +08:00
|
|
|
SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
|
2008-10-23 11:10:39 +08:00
|
|
|
|
|
|
|
SVal ArrayToPointer(SVal Array) { return Array; }
|
2008-07-18 02:38:48 +08:00
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
virtual Store
|
|
|
|
RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
|
|
|
|
llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
|
|
|
|
LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
|
2008-08-22 06:34:01 +08:00
|
|
|
|
2008-09-03 11:06:11 +08:00
|
|
|
virtual void iterBindings(Store store, BindingsHandler& f);
|
|
|
|
|
2008-10-05 20:12:48 +08:00
|
|
|
virtual Store AddDecl(Store store,
|
2008-08-23 08:50:55 +08:00
|
|
|
const VarDecl* VD, Expr* Ex,
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal InitVal = UndefinedVal(), unsigned Count = 0);
|
2008-08-22 06:34:01 +08:00
|
|
|
|
2008-07-18 02:38:48 +08:00
|
|
|
static inline VarBindingsTy GetVarBindings(Store store) {
|
|
|
|
return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
|
2008-08-20 06:24:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void print(Store store, std::ostream& Out,
|
|
|
|
const char* nl, const char *sep);
|
2008-10-07 09:31:04 +08:00
|
|
|
|
2008-09-03 11:06:11 +08:00
|
|
|
};
|
2008-10-04 13:50:14 +08:00
|
|
|
|
2008-07-11 06:03:41 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
2008-08-29 07:31:31 +08:00
|
|
|
StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
|
|
|
|
return new BasicStoreManager(StMgr);
|
2008-08-26 03:33:03 +08:00
|
|
|
}
|
2008-10-21 14:54:23 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
|
|
|
|
return loc::MemRegionVal(MRMgr.getVarRegion(VD));
|
2008-10-17 08:51:01 +08:00
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
|
|
|
|
SVal Base) {
|
2008-10-17 08:51:01 +08:00
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-22 17:00:19 +08:00
|
|
|
SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
|
|
|
|
const FieldDecl* D) {
|
2008-10-18 04:28:54 +08:00
|
|
|
|
|
|
|
if (Base.isUnknownOrUndef())
|
|
|
|
return Base;
|
|
|
|
|
|
|
|
Loc BaseL = cast<Loc>(Base);
|
|
|
|
const MemRegion* BaseR = 0;
|
|
|
|
|
|
|
|
switch(BaseL.getSubKind()) {
|
|
|
|
case loc::SymbolValKind:
|
|
|
|
BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case loc::GotoLabelKind:
|
|
|
|
case loc::FuncValKind:
|
|
|
|
// Technically we can get here if people do funny things with casts.
|
|
|
|
return UndefinedVal();
|
|
|
|
|
|
|
|
case loc::MemRegionKind:
|
|
|
|
BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case loc::ConcreteIntKind:
|
|
|
|
case loc::StringLiteralValKind:
|
|
|
|
// While these seem funny, this can happen through casts.
|
|
|
|
// FIXME: What we should return is the field offset. For example,
|
|
|
|
// add the field offset to the integer value. That way funny things
|
|
|
|
// like this work properly: &(((struct foo *) 0xa)->f)
|
|
|
|
return Base;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert ("Unhandled Base.");
|
|
|
|
return Base;
|
|
|
|
}
|
|
|
|
|
|
|
|
return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
|
2008-10-17 08:51:01 +08:00
|
|
|
}
|
2008-08-26 03:33:03 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
|
|
|
|
SVal Offset) {
|
2008-10-18 06:52:40 +08:00
|
|
|
// Total hack: Just return "Base" for now.
|
|
|
|
return Base;
|
2008-10-16 14:09:51 +08:00
|
|
|
}
|
|
|
|
|
2008-10-21 14:27:32 +08:00
|
|
|
SVal BasicStoreManager::Retrieve(Store St, Loc LV, QualType T) {
|
2008-07-11 06:03:41 +08:00
|
|
|
|
|
|
|
if (isa<UnknownVal>(LV))
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
assert (!isa<UndefinedVal>(LV));
|
|
|
|
|
|
|
|
switch (LV.getSubKind()) {
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::MemRegionKind: {
|
2008-10-18 04:28:54 +08:00
|
|
|
const VarRegion* R =
|
2008-10-17 13:57:07 +08:00
|
|
|
dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
|
2008-10-04 13:50:14 +08:00
|
|
|
|
|
|
|
if (!R)
|
|
|
|
return UnknownVal();
|
|
|
|
|
2008-07-11 06:03:41 +08:00
|
|
|
VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(St));
|
2008-10-04 13:50:14 +08:00
|
|
|
VarBindingsTy::data_type* T = B.lookup(R->getDecl());
|
2008-07-11 06:03:41 +08:00
|
|
|
return T ? *T : UnknownVal();
|
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::SymbolValKind:
|
2008-07-11 06:03:41 +08:00
|
|
|
return UnknownVal();
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::ConcreteIntKind:
|
|
|
|
// Some clients may call GetSVal with such an option simply because
|
|
|
|
// they are doing a quick scan through their Locs (potentially to
|
2008-07-11 06:03:41 +08:00
|
|
|
// invalidate their bindings). Just return Undefined.
|
2008-10-17 08:51:01 +08:00
|
|
|
return UndefinedVal();
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::FuncValKind:
|
2008-07-11 06:03:41 +08:00
|
|
|
return LV;
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::StringLiteralValKind:
|
2008-07-11 06:03:41 +08:00
|
|
|
// FIXME: Implement better support for fetching characters from strings.
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
default:
|
2008-10-17 13:57:07 +08:00
|
|
|
assert (false && "Invalid Loc.");
|
2008-07-11 06:03:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
2008-10-17 08:03:18 +08:00
|
|
|
|
2008-10-21 14:27:32 +08:00
|
|
|
Store BasicStoreManager::Bind(Store store, Loc LV, SVal V) {
|
2008-07-18 02:38:48 +08:00
|
|
|
switch (LV.getSubKind()) {
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::MemRegionKind: {
|
2008-10-18 04:28:54 +08:00
|
|
|
const VarRegion* R =
|
2008-10-17 13:57:07 +08:00
|
|
|
dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
|
2008-10-04 13:50:14 +08:00
|
|
|
|
|
|
|
if (!R)
|
|
|
|
return store;
|
|
|
|
|
2008-07-18 02:38:48 +08:00
|
|
|
VarBindingsTy B = GetVarBindings(store);
|
2008-07-11 06:03:41 +08:00
|
|
|
return V.isUnknown()
|
2008-10-04 13:50:14 +08:00
|
|
|
? VBFactory.Remove(B, R->getDecl()).getRoot()
|
|
|
|
: VBFactory.Add(B, R->getDecl(), V).getRoot();
|
2008-07-18 02:38:48 +08:00
|
|
|
}
|
2008-07-11 06:03:41 +08:00
|
|
|
default:
|
2008-10-17 13:57:07 +08:00
|
|
|
assert ("SetSVal for given Loc type not yet implemented.");
|
2008-07-18 02:38:48 +08:00
|
|
|
return store;
|
2008-07-11 06:03:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
Store BasicStoreManager::Remove(Store store, Loc LV) {
|
2008-07-11 06:03:41 +08:00
|
|
|
switch (LV.getSubKind()) {
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::MemRegionKind: {
|
2008-10-18 04:28:54 +08:00
|
|
|
const VarRegion* R =
|
|
|
|
dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion());
|
2008-10-04 13:50:14 +08:00
|
|
|
|
|
|
|
if (!R)
|
|
|
|
return store;
|
|
|
|
|
2008-07-18 02:38:48 +08:00
|
|
|
VarBindingsTy B = GetVarBindings(store);
|
2008-10-04 13:50:14 +08:00
|
|
|
return VBFactory.Remove(B,R->getDecl()).getRoot();
|
2008-07-18 02:38:48 +08:00
|
|
|
}
|
2008-07-11 06:03:41 +08:00
|
|
|
default:
|
2008-10-17 13:57:07 +08:00
|
|
|
assert ("Remove for given Loc type not yet implemented.");
|
2008-07-18 02:38:48 +08:00
|
|
|
return store;
|
2008-07-11 06:03:41 +08:00
|
|
|
}
|
|
|
|
}
|
2008-07-18 02:38:48 +08:00
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
Store
|
|
|
|
BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
|
|
|
|
const LiveVariables& Liveness,
|
|
|
|
llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
|
|
|
|
LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
|
2008-07-18 02:38:48 +08:00
|
|
|
|
|
|
|
VarBindingsTy B = GetVarBindings(store);
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef SVal::symbol_iterator symbol_iterator;
|
2008-07-18 02:38:48 +08:00
|
|
|
|
|
|
|
// Iterate over the variable bindings.
|
|
|
|
for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
|
|
|
|
if (Liveness.isLive(Loc, I.getKey())) {
|
2008-10-07 09:31:04 +08:00
|
|
|
RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal X = I.getData();
|
2008-07-18 02:38:48 +08:00
|
|
|
|
|
|
|
for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
|
|
|
|
LSymbols.insert(*SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan for live variables and live symbols.
|
2008-10-04 13:50:14 +08:00
|
|
|
llvm::SmallPtrSet<const VarRegion*, 10> Marked;
|
2008-07-18 02:38:48 +08:00
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
while (!RegionRoots.empty()) {
|
2008-10-18 06:52:40 +08:00
|
|
|
const MemRegion* MR = RegionRoots.back();
|
2008-10-04 13:50:14 +08:00
|
|
|
RegionRoots.pop_back();
|
2008-07-18 02:38:48 +08:00
|
|
|
|
2008-10-18 06:52:40 +08:00
|
|
|
while (MR) {
|
|
|
|
if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
|
|
|
|
LSymbols.insert(SymR->getSymbol());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) {
|
|
|
|
if (Marked.count(R))
|
|
|
|
break;
|
|
|
|
|
|
|
|
Marked.insert(R);
|
|
|
|
SVal X = GetRegionSVal(store, R);
|
2008-07-18 02:38:48 +08:00
|
|
|
|
2008-10-18 06:52:40 +08:00
|
|
|
// FIXME: We need to handle symbols nested in region definitions.
|
|
|
|
for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
|
|
|
|
LSymbols.insert(*SI);
|
2008-07-18 02:38:48 +08:00
|
|
|
|
2008-10-18 06:52:40 +08:00
|
|
|
if (!isa<loc::MemRegionVal>(X))
|
|
|
|
break;
|
2008-07-18 02:38:48 +08:00
|
|
|
|
2008-10-18 06:52:40 +08:00
|
|
|
const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
|
|
|
|
RegionRoots.push_back(LVD.getRegion());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
|
|
|
|
MR = R->getSuperRegion();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2008-07-18 02:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove dead variable bindings.
|
2008-10-04 13:50:14 +08:00
|
|
|
for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
|
2008-10-07 09:31:04 +08:00
|
|
|
const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
|
2008-10-04 13:50:14 +08:00
|
|
|
|
|
|
|
if (!Marked.count(R)) {
|
2008-10-17 13:57:07 +08:00
|
|
|
store = Remove(store, loc::MemRegionVal(R));
|
|
|
|
SVal X = I.getData();
|
2008-07-18 02:38:48 +08:00
|
|
|
|
|
|
|
for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
|
|
|
|
if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
|
|
|
|
}
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2008-07-18 02:38:48 +08:00
|
|
|
return store;
|
|
|
|
}
|
2008-08-20 00:51:45 +08:00
|
|
|
|
2008-10-05 20:12:48 +08:00
|
|
|
Store BasicStoreManager::getInitialStore() {
|
2008-08-20 00:51:45 +08:00
|
|
|
// The LiveVariables information already has a compilation of all VarDecls
|
|
|
|
// used in the function. Iterate through this set, and "symbolicate"
|
|
|
|
// any VarDecl whose value originally comes from outside the function.
|
|
|
|
|
|
|
|
typedef LiveVariables::AnalysisDataTy LVDataTy;
|
|
|
|
LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
|
|
|
|
|
|
|
|
Store St = VBFactory.GetEmptyMap().getRoot();
|
|
|
|
|
|
|
|
for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
|
2008-10-22 00:13:35 +08:00
|
|
|
NamedDecl* ND = const_cast<NamedDecl*>(I->first);
|
2008-08-20 00:51:45 +08:00
|
|
|
|
2008-10-22 00:13:35 +08:00
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
|
2008-08-20 00:51:45 +08:00
|
|
|
// Punt on static variables for now.
|
|
|
|
if (VD->getStorageClass() == VarDecl::Static)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Only handle pointers and integers for now.
|
|
|
|
QualType T = VD->getType();
|
2008-10-17 13:57:07 +08:00
|
|
|
if (Loc::IsLocType(T) || T->isIntegerType()) {
|
2008-08-20 00:51:45 +08:00
|
|
|
// Initialize globals and parameters to symbolic values.
|
|
|
|
// Initialize local variables to undefined.
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
|
2008-08-20 00:51:45 +08:00
|
|
|
isa<ImplicitParamDecl>(VD))
|
2008-10-17 13:57:07 +08:00
|
|
|
? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
|
2008-08-20 00:51:45 +08:00
|
|
|
: UndefinedVal();
|
|
|
|
|
2008-10-21 14:27:32 +08:00
|
|
|
St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(VD)), X);
|
2008-08-20 00:51:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return St;
|
|
|
|
}
|
2008-08-20 06:24:03 +08:00
|
|
|
|
2008-10-05 20:12:48 +08:00
|
|
|
Store BasicStoreManager::AddDecl(Store store,
|
2008-08-23 08:50:55 +08:00
|
|
|
const VarDecl* VD, Expr* Ex,
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal InitVal, unsigned Count) {
|
2008-08-23 08:50:55 +08:00
|
|
|
|
|
|
|
BasicValueFactory& BasicVals = StateMgr.getBasicVals();
|
|
|
|
SymbolManager& SymMgr = StateMgr.getSymbolManager();
|
|
|
|
|
2008-08-22 06:34:01 +08:00
|
|
|
// BasicStore does not model arrays and structs.
|
|
|
|
if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
|
|
|
|
return store;
|
|
|
|
|
|
|
|
if (VD->hasGlobalStorage()) {
|
|
|
|
// Handle variables with global storage: extern, static, PrivateExtern.
|
|
|
|
|
|
|
|
// FIXME:: static variables may have an initializer, but the second time a
|
|
|
|
// function is called those values may not be current. Currently, a function
|
|
|
|
// will not be called more than once.
|
|
|
|
|
|
|
|
// Static global variables should not be visited here.
|
|
|
|
assert(!(VD->getStorageClass() == VarDecl::Static &&
|
|
|
|
VD->isFileVarDecl()));
|
|
|
|
|
|
|
|
// Process static variables.
|
|
|
|
if (VD->getStorageClass() == VarDecl::Static) {
|
|
|
|
// C99: 6.7.8 Initialization
|
|
|
|
// If an object that has static storage duration is not initialized
|
|
|
|
// explicitly, then:
|
|
|
|
// —if it has pointer type, it is initialized to a null pointer;
|
|
|
|
// —if it has arithmetic type, it is initialized to (positive or
|
|
|
|
// unsigned) zero;
|
|
|
|
if (!Ex) {
|
|
|
|
QualType T = VD->getType();
|
2008-10-17 13:57:07 +08:00
|
|
|
if (Loc::IsLocType(T))
|
2008-10-21 14:27:32 +08:00
|
|
|
store = Bind(store, getLoc(VD),
|
|
|
|
loc::ConcreteInt(BasicVals.getValue(0, T)));
|
2008-08-22 06:34:01 +08:00
|
|
|
else if (T->isIntegerType())
|
2008-10-21 14:27:32 +08:00
|
|
|
store = Bind(store, getLoc(VD),
|
|
|
|
nonloc::ConcreteInt(BasicVals.getValue(0, T)));
|
2008-08-22 06:34:01 +08:00
|
|
|
else {
|
|
|
|
// assert(0 && "ignore other types of variables");
|
|
|
|
}
|
|
|
|
} else {
|
2008-10-21 14:27:32 +08:00
|
|
|
store = Bind(store, getLoc(VD), InitVal);
|
2008-08-22 06:34:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Process local scalar variables.
|
|
|
|
QualType T = VD->getType();
|
2008-10-17 13:57:07 +08:00
|
|
|
if (Loc::IsLocType(T) || T->isIntegerType()) {
|
|
|
|
SVal V = Ex ? InitVal : UndefinedVal();
|
2008-08-22 06:34:01 +08:00
|
|
|
|
|
|
|
if (Ex && InitVal.isUnknown()) {
|
|
|
|
// EXPERIMENTAL: "Conjured" symbols.
|
|
|
|
SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
V = Loc::IsLocType(Ex->getType())
|
|
|
|
? cast<SVal>(loc::SymbolVal(Sym))
|
|
|
|
: cast<SVal>(nonloc::SymbolVal(Sym));
|
2008-08-22 06:34:01 +08:00
|
|
|
}
|
|
|
|
|
2008-10-21 14:27:32 +08:00
|
|
|
store = Bind(store, getLoc(VD), V);
|
2008-08-22 06:34:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
2008-08-20 06:24:03 +08:00
|
|
|
void BasicStoreManager::print(Store store, std::ostream& Out,
|
|
|
|
const char* nl, const char *sep) {
|
|
|
|
|
|
|
|
VarBindingsTy B = GetVarBindings(store);
|
|
|
|
Out << "Variables:" << nl;
|
|
|
|
|
|
|
|
bool isFirst = true;
|
|
|
|
|
|
|
|
for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
|
|
|
|
if (isFirst) isFirst = false;
|
|
|
|
else Out << nl;
|
|
|
|
|
|
|
|
Out << ' ' << I.getKey()->getName() << " : ";
|
|
|
|
I.getData().print(Out);
|
|
|
|
}
|
|
|
|
}
|
2008-08-29 08:47:32 +08:00
|
|
|
|
2008-09-03 11:06:11 +08:00
|
|
|
|
|
|
|
void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
|
|
|
|
VarBindingsTy B = GetVarBindings(store);
|
2008-08-29 08:47:32 +08:00
|
|
|
|
2008-09-03 11:06:11 +08:00
|
|
|
for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
|
|
|
|
|
2008-10-07 09:31:04 +08:00
|
|
|
f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
|
2008-08-29 08:47:32 +08:00
|
|
|
}
|
|
|
|
}
|
2008-09-03 11:06:11 +08:00
|
|
|
|
|
|
|
StoreManager::BindingsHandler::~BindingsHandler() {}
|