"llvm/git@repo.hca.bsc.es:rferrer/llvm-epi-0.8.git" did not exist on "a565dfda8e3cf0b2a6ff7fdedd5b772fe4f63f48"
Newer
Older
//== 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.
//
//===----------------------------------------------------------------------===//
Ted Kremenek
committed
#include "clang/Analysis/Analyses/LiveVariables.h"
#include "llvm/ADT/ImmutableMap.h"
#include "llvm/Support/Compiler.h"
Ted Kremenek
committed
#include "llvm/Support/Streams.h"
using namespace clang;
Ted Kremenek
committed
using store::Region;
using store::RegionExtent;
typedef llvm::ImmutableMap<VarDecl*,RVal> VarBindingsTy;
namespace {
class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
VarBindingsTy::Factory VBFactory;
Ted Kremenek
committed
GRStateManager& StMgr;
public:
Ted Kremenek
committed
BasicStoreManager(GRStateManager& mgr) : StMgr(mgr) {}
virtual ~BasicStoreManager() {}
virtual RVal GetRVal(Store St, LVal LV, QualType T);
virtual Store SetRVal(Store St, LVal LV, RVal V);
virtual Store Remove(Store St, LVal LV);
virtual Store getInitialStore(GRStateManager& StateMgr);
virtual Store RemoveDeadBindings(Store store, Stmt* Loc,
const LiveVariables& Live,
DeclRootsTy& DRoots, LiveSymbolsTy& LSymbols,
DeadSymbolsTy& DSymbols);
virtual void iterBindings(Store store, BindingsHandler& f);
virtual Store AddDecl(Store store, GRStateManager& StateMgr,
const VarDecl* VD, Expr* Ex,
RVal InitVal = UndefinedVal(), unsigned Count = 0);
static inline VarBindingsTy GetVarBindings(Store store) {
return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremenek
committed
}
virtual void print(Store store, std::ostream& Out,
const char* nl, const char *sep);
Ted Kremenek
committed
virtual RegionExtent getExtent(Region R);
Ted Kremenek
committed
/// BindingAsString - Returns a string representing the given binding.
virtual std::string BindingAsString(store::Binding binding);
/// getRVal - Returns the bound RVal for a given binding.
virtual RVal getRVal(Store store, store::Binding binding);
};
class VISIBILITY_HIDDEN VarRegion : public store::Region {
public:
VarRegion(VarDecl* VD) : Region(VD) {}
VarDecl* getDecl() const { return (VarDecl*) Data; }
static bool classof(const store::Region*) { return true; }
};
class VISIBILITY_HIDDEN VarBinding : public store::Binding {
public:
VarBinding(VarBindingsTy::value_type* T) : store::Binding(T) {}
const VarBindingsTy::value_type_ref getValue() const {
return *static_cast<const VarBindingsTy::value_type*>(first);
}
std::string getName() const {
return getValue().first->getName();
}
RVal getRVal() const {
return getValue().second;
}
static inline bool classof(const store::Binding*) { return true; }
};
} // end anonymous namespace
Ted Kremenek
committed
StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
return new BasicStoreManager(StMgr);
Ted Kremenek
committed
RegionExtent BasicStoreManager::getExtent(Region R) {
QualType T = cast<VarRegion>(&R)->getDecl()->getType();
Ted Kremenek
committed
// FIXME: Add support for VLAs. This may require passing in additional
// information, or tracking a different region type.
if (!T.getTypePtr()->isConstantSizeType())
return store::UnknownExtent();
ASTContext& C = StMgr.getContext();
assert (!T->isObjCInterfaceType()); // @interface not a possible VarDecl type.
assert (T != C.VoidTy); // void not a possible VarDecl type.
return store::FixedExtent(StMgr.getBasicVals().getValue(C.getTypeSize(T),
C.VoidPtrTy));
}
Ted Kremenek
committed
RVal BasicStoreManager::GetRVal(Store St, LVal LV, QualType T) {
if (isa<UnknownVal>(LV))
return UnknownVal();
assert (!isa<UndefinedVal>(LV));
switch (LV.getSubKind()) {
case lval::DeclValKind: {
VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(St));
VarBindingsTy::data_type* T = B.lookup(cast<lval::DeclVal>(LV).getDecl());
return T ? *T : UnknownVal();
}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
return UnknownVal();
case lval::ConcreteIntKind:
// Some clients may call GetRVal with such an option simply because
// they are doing a quick scan through their LVals (potentially to
// invalidate their bindings). Just return Undefined.
return UndefinedVal();
case lval::ArrayOffsetKind:
case lval::FieldOffsetKind:
return UnknownVal();
case lval::FuncValKind:
return LV;
case lval::StringLiteralValKind:
// FIXME: Implement better support for fetching characters from strings.
return UnknownVal();
default:
assert (false && "Invalid LVal.");
break;
}
return UnknownVal();
}
Store BasicStoreManager::SetRVal(Store store, LVal LV, RVal V) {
switch (LV.getSubKind()) {
case lval::DeclValKind: {
VarBindingsTy B = GetVarBindings(store);
return V.isUnknown()
? VBFactory.Remove(B,cast<lval::DeclVal>(LV).getDecl()).getRoot()
: VBFactory.Add(B, cast<lval::DeclVal>(LV).getDecl(), V).getRoot();
}
default:
assert ("SetRVal for given LVal type not yet implemented.");
return store;
}
}
Store BasicStoreManager::Remove(Store store, LVal LV) {
switch (LV.getSubKind()) {
case lval::DeclValKind: {
VarBindingsTy B = GetVarBindings(store);
return VBFactory.Remove(B,cast<lval::DeclVal>(LV).getDecl()).getRoot();
}
default:
assert ("Remove for given LVal type not yet implemented.");
return store;
}
}
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
Store BasicStoreManager::RemoveDeadBindings(Store store,
Stmt* Loc,
const LiveVariables& Liveness,
DeclRootsTy& DRoots,
LiveSymbolsTy& LSymbols,
DeadSymbolsTy& DSymbols) {
VarBindingsTy B = GetVarBindings(store);
typedef RVal::symbol_iterator symbol_iterator;
// Iterate over the variable bindings.
for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
if (Liveness.isLive(Loc, I.getKey())) {
DRoots.push_back(I.getKey());
RVal X = I.getData();
for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
LSymbols.insert(*SI);
}
// Scan for live variables and live symbols.
llvm::SmallPtrSet<ValueDecl*, 10> Marked;
while (!DRoots.empty()) {
ValueDecl* V = DRoots.back();
DRoots.pop_back();
if (Marked.count(V))
continue;
Marked.insert(V);
RVal X = GetRVal(store, lval::DeclVal(cast<VarDecl>(V)), QualType());
for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
LSymbols.insert(*SI);
if (!isa<lval::DeclVal>(X))
continue;
const lval::DeclVal& LVD = cast<lval::DeclVal>(X);
DRoots.push_back(LVD.getDecl());
}
// Remove dead variable bindings.
for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
if (!Marked.count(I.getKey())) {
store = Remove(store, lval::DeclVal(I.getKey()));
RVal X = I.getData();
for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
}
return store;
}
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
Store BasicStoreManager::getInitialStore(GRStateManager& StateMgr) {
// 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) {
ScopedDecl* SD = const_cast<ScopedDecl*>(I->first);
if (VarDecl* VD = dyn_cast<VarDecl>(SD)) {
// Punt on static variables for now.
if (VD->getStorageClass() == VarDecl::Static)
continue;
// Only handle pointers and integers for now.
QualType T = VD->getType();
if (LVal::IsLValType(T) || T->isIntegerType()) {
// Initialize globals and parameters to symbolic values.
// Initialize local variables to undefined.
RVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
isa<ImplicitParamDecl>(VD))
? RVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
: UndefinedVal();
St = SetRVal(St, lval::DeclVal(VD), X);
}
}
}
return St;
}
Ted Kremenek
committed
Store BasicStoreManager::AddDecl(Store store, GRStateManager& StateMgr,
const VarDecl* VD, Expr* Ex,
RVal InitVal, unsigned Count) {
BasicValueFactory& BasicVals = StateMgr.getBasicVals();
SymbolManager& SymMgr = StateMgr.getSymbolManager();
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// 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();
if (LVal::IsLValType(T))
store = SetRVal(store, lval::DeclVal(VD),
lval::ConcreteInt(BasicVals.getValue(0, T)));
else if (T->isIntegerType())
store = SetRVal(store, lval::DeclVal(VD),
nonlval::ConcreteInt(BasicVals.getValue(0, T)));
else {
// assert(0 && "ignore other types of variables");
}
} else {
store = SetRVal(store, lval::DeclVal(VD), InitVal);
}
}
} else {
// Process local scalar variables.
QualType T = VD->getType();
if (LVal::IsLValType(T) || T->isIntegerType()) {
RVal V = Ex ? InitVal : UndefinedVal();
if (Ex && InitVal.isUnknown()) {
// EXPERIMENTAL: "Conjured" symbols.
SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
V = LVal::IsLValType(Ex->getType())
? cast<RVal>(lval::SymbolVal(Sym))
: cast<RVal>(nonlval::SymbolVal(Sym));
}
store = SetRVal(store, lval::DeclVal(VD), V);
}
}
return store;
}
Ted Kremenek
committed
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);
}
}
Ted Kremenek
committed
void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
VarBindingsTy B = GetVarBindings(store);
Ted Kremenek
committed
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
VarBinding binding(&(*I));
f.HandleBinding(*this, store, binding);
}
}
std::string BasicStoreManager::BindingAsString(store::Binding binding) {
return cast<VarBinding>(binding).getName();
}
RVal BasicStoreManager::getRVal(Store store, store::Binding binding) {
return cast<VarBinding>(binding).getRVal();
}
//==------------------------------------------------------------------------==//
// Generic store operations.
//==------------------------------------------------------------------------==//
namespace {
class VISIBILITY_HIDDEN GetBindingsIterator : public StoreManager::BindingsHandler {
SymbolID Sym;
llvm::SmallVectorImpl<store::Binding>& bindings;
public:
GetBindingsIterator(SymbolID s, llvm::SmallVectorImpl<store::Binding>& b)
: Sym(s), bindings(b) {}
Ted Kremenek
committed
virtual bool HandleBinding(StoreManager& SMgr, Store store,
store::Binding binding) {
RVal V = SMgr.getRVal(store, binding);
Ted Kremenek
committed
if (const lval::SymbolVal* SV=dyn_cast<lval::SymbolVal>(&V)) {
if (SV->getSymbol() == Sym)
bindings.push_back(binding);
}
else if (const nonlval::SymbolVal* SV=dyn_cast<nonlval::SymbolVal>(&V)){
Ted Kremenek
committed
if (SV->getSymbol() == Sym)
Ted Kremenek
committed
}
Ted Kremenek
committed
}
Ted Kremenek
committed
void StoreManager::getBindings(llvm::SmallVectorImpl<store::Binding>& bindings,
Store store, SymbolID Sym) {
GetBindingsIterator BI(Sym, bindings);
iterBindings(store, BI);
Ted Kremenek
committed
}
StoreManager::BindingsHandler::~BindingsHandler() {}