mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-30 12:36:06 +00:00

This patch overhauls the "memory region" abstraction that was prototyped (but never really used) as part of the Store.h. This patch adds MemRegion.h and MemRegion.cpp, which defines the class MemRegion and its subclasses. This classes serve to define an abstract representation of memory, with regions being layered on other regions to to capture the relationships between fields and variables, variables and the address space they are allocated in, and so on. The main motivation of this patch is that key parts of the analyzer assumed that all value bindings were to VarDecls. In the future this won't be the case, and this patch removes lval::DeclVal and replaces it with lval::MemRegionVal. Now all pieces of the analyzer must reason about abstract memory blocks instead of just variables. There should be no functionality change from this patch, but it opens the door for significant improvements to the analyzer such as field-sensitivity and object-sensitivity, both which were on hold until the memory abstraction got generalized. The memory region abstraction also allows type-information to literally be affixed to a memory region. This will allow the some now redundant logic to be removed from the retain/release checker. llvm-svn: 57042
153 lines
4.3 KiB
C++
153 lines
4.3 KiB
C++
//== Environment.cpp - Map from Expr* to Locations/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 Environment and EnvironmentManager classes.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Analysis/PathSensitive/Environment.h"
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
|
#include "llvm/ADT/ImmutableMap.h"
|
|
#include "llvm/Support/Streams.h"
|
|
|
|
using namespace clang;
|
|
|
|
RVal Environment::GetRVal(Expr* E, BasicValueFactory& BasicVals) const {
|
|
|
|
for (;;) {
|
|
|
|
switch (E->getStmtClass()) {
|
|
|
|
case Stmt::AddrLabelExprClass:
|
|
return LVal::MakeVal(cast<AddrLabelExpr>(E));
|
|
|
|
// ParenExprs are no-ops.
|
|
|
|
case Stmt::ParenExprClass:
|
|
E = cast<ParenExpr>(E)->getSubExpr();
|
|
continue;
|
|
|
|
case Stmt::CharacterLiteralClass: {
|
|
CharacterLiteral* C = cast<CharacterLiteral>(E);
|
|
return NonLVal::MakeVal(BasicVals, C->getValue(), C->getType());
|
|
}
|
|
|
|
case Stmt::IntegerLiteralClass: {
|
|
return NonLVal::MakeVal(BasicVals, cast<IntegerLiteral>(E));
|
|
}
|
|
|
|
case Stmt::StringLiteralClass:
|
|
return LVal::MakeVal(cast<StringLiteral>(E));
|
|
|
|
// Casts where the source and target type are the same
|
|
// are no-ops. We blast through these to get the descendant
|
|
// subexpression that has a value.
|
|
|
|
case Stmt::ImplicitCastExprClass:
|
|
case Stmt::ExplicitCastExprClass: {
|
|
CastExpr* C = cast<CastExpr>(E);
|
|
QualType CT = C->getType();
|
|
QualType ST = C->getSubExpr()->getType();
|
|
|
|
if (CT->isVoidType())
|
|
return UnknownVal();
|
|
|
|
break;
|
|
}
|
|
|
|
// Handle all other Expr* using a lookup.
|
|
|
|
default:
|
|
break;
|
|
};
|
|
|
|
break;
|
|
}
|
|
|
|
return LookupExpr(E);
|
|
}
|
|
|
|
RVal Environment::GetBlkExprRVal(Expr* E, BasicValueFactory& BasicVals) const {
|
|
|
|
E = E->IgnoreParens();
|
|
|
|
switch (E->getStmtClass()) {
|
|
case Stmt::CharacterLiteralClass: {
|
|
CharacterLiteral* C = cast<CharacterLiteral>(E);
|
|
return NonLVal::MakeVal(BasicVals, C->getValue(), C->getType());
|
|
}
|
|
|
|
case Stmt::IntegerLiteralClass: {
|
|
return NonLVal::MakeVal(BasicVals, cast<IntegerLiteral>(E));
|
|
}
|
|
|
|
default:
|
|
return LookupBlkExpr(E);
|
|
}
|
|
}
|
|
|
|
Environment EnvironmentManager::SetRVal(const Environment& Env, Expr* E, RVal V,
|
|
bool isBlkExpr, bool Invalidate) {
|
|
assert (E);
|
|
|
|
if (V.isUnknown()) {
|
|
if (Invalidate)
|
|
return isBlkExpr ? RemoveBlkExpr(Env, E) : RemoveSubExpr(Env, E);
|
|
else
|
|
return Env;
|
|
}
|
|
|
|
return isBlkExpr ? AddBlkExpr(Env, E, V) : AddSubExpr(Env, E, V);
|
|
}
|
|
|
|
Environment
|
|
EnvironmentManager::RemoveDeadBindings(Environment Env, Stmt* Loc,
|
|
const LiveVariables& Liveness,
|
|
llvm::SmallVectorImpl<const MemRegion*>& DRoots,
|
|
StoreManager::LiveSymbolsTy& LSymbols) {
|
|
|
|
// Drop bindings for subexpressions.
|
|
Env = RemoveSubExprBindings(Env);
|
|
|
|
// Iterate over the block-expr bindings.
|
|
for (Environment::beb_iterator I = Env.beb_begin(), E = Env.beb_end();
|
|
I != E; ++I) {
|
|
Expr* BlkExpr = I.getKey();
|
|
|
|
if (Liveness.isLive(Loc, BlkExpr)) {
|
|
RVal X = I.getData();
|
|
|
|
// If the block expr's value is a memory region, then mark that region.
|
|
if (isa<lval::MemRegionVal>(X))
|
|
DRoots.push_back(cast<lval::MemRegionVal>(X).getRegion());
|
|
|
|
|
|
// Mark all symbols in the block expr's value.
|
|
for (RVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
|
|
SI != SE; ++SI) {
|
|
LSymbols.insert(*SI);
|
|
}
|
|
} else {
|
|
// The block expr is dead.
|
|
RVal X = I.getData();
|
|
|
|
// Do not misclean LogicalExpr or ConditionalOperator. It is dead at the
|
|
// beginning of itself, but we need its UndefinedVal to determine its
|
|
// RVal.
|
|
|
|
if (X.isUndef() && cast<UndefinedVal>(X).getData())
|
|
continue;
|
|
|
|
Env = RemoveBlkExpr(Env, BlkExpr);
|
|
}
|
|
}
|
|
|
|
return Env;
|
|
}
|