2009-04-22 05:51:34 +08:00
|
|
|
//== Store.cpp - Interface for maps 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 types Store and StoreManager.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
2010-01-12 01:06:35 +08:00
|
|
|
#include "clang/AST/CharUnits.h"
|
2012-01-28 20:06:22 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-04-22 05:51:34 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-04-22 05:51:34 +08:00
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
StoreManager::StoreManager(ProgramStateManager &stateMgr)
|
2010-12-02 15:49:45 +08:00
|
|
|
: svalBuilder(stateMgr.getSValBuilder()), StateMgr(stateMgr),
|
|
|
|
MRMgr(svalBuilder.getRegionManager()), Ctx(stateMgr.getContext()) {}
|
2009-04-22 05:51:34 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
StoreRef StoreManager::enterStackFrame(ProgramStateRef state,
|
2012-01-13 03:25:46 +08:00
|
|
|
const LocationContext *callerCtx,
|
|
|
|
const StackFrameContext *calleeCtx) {
|
2011-02-19 09:59:33 +08:00
|
|
|
return StoreRef(state->getStore(), *this);
|
2010-08-04 04:44:35 +08:00
|
|
|
}
|
|
|
|
|
2009-10-14 14:55:01 +08:00
|
|
|
const MemRegion *StoreManager::MakeElementRegion(const MemRegion *Base,
|
2009-11-16 12:49:44 +08:00
|
|
|
QualType EleTy, uint64_t index) {
|
2010-12-02 15:49:45 +08:00
|
|
|
NonLoc idx = svalBuilder.makeArrayIndex(index);
|
|
|
|
return MRMgr.getElementRegion(EleTy, idx, Base, svalBuilder.getContext());
|
2009-07-07 06:23:45 +08:00
|
|
|
}
|
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// FIXME: Merge with the implementation of the same method in MemRegion.cpp
|
2009-07-07 07:47:19 +08:00
|
|
|
static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
|
2009-07-30 05:53:49 +08:00
|
|
|
if (const RecordType *RT = Ty->getAs<RecordType>()) {
|
2009-07-07 07:47:19 +08:00
|
|
|
const RecordDecl *D = RT->getDecl();
|
2010-02-11 09:04:33 +08:00
|
|
|
if (!D->getDefinition())
|
2009-07-07 07:47:19 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-07 07:47:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-19 09:59:33 +08:00
|
|
|
StoreRef StoreManager::BindDefault(Store store, const MemRegion *R, SVal V) {
|
|
|
|
return StoreRef(store, *this);
|
|
|
|
}
|
|
|
|
|
2010-04-19 19:47:28 +08:00
|
|
|
const ElementRegion *StoreManager::GetElementZeroRegion(const MemRegion *R,
|
|
|
|
QualType T) {
|
2010-12-02 15:49:45 +08:00
|
|
|
NonLoc idx = svalBuilder.makeZeroArrayIndex();
|
2010-04-19 19:47:28 +08:00
|
|
|
assert(!T.isNull());
|
|
|
|
return MRMgr.getElementRegion(T, idx, R, Ctx);
|
|
|
|
}
|
|
|
|
|
2011-02-12 03:48:15 +08:00
|
|
|
const MemRegion *StoreManager::castRegion(const MemRegion *R, QualType CastToTy) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
ASTContext &Ctx = StateMgr.getContext();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-07 06:39:40 +08:00
|
|
|
// Handle casts to Objective-C objects.
|
2009-08-01 14:17:29 +08:00
|
|
|
if (CastToTy->isObjCObjectPointerType())
|
2009-11-10 10:17:20 +08:00
|
|
|
return R->StripCasts();
|
2009-08-01 14:17:29 +08:00
|
|
|
|
2009-07-18 14:27:51 +08:00
|
|
|
if (CastToTy->isBlockPointerType()) {
|
2009-08-28 12:49:15 +08:00
|
|
|
// FIXME: We may need different solutions, depending on the symbol
|
2009-07-18 14:27:51 +08:00
|
|
|
// involved. Blocks can be casted to/from 'id', as they can be treated
|
2009-08-28 12:49:15 +08:00
|
|
|
// as Objective-C objects. This could possibly be handled by enhancing
|
2009-09-09 23:08:12 +08:00
|
|
|
// our reasoning of downcasts of symbolic objects.
|
2009-08-28 12:49:15 +08:00
|
|
|
if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R))
|
2009-10-14 14:55:01 +08:00
|
|
|
return R;
|
2009-07-18 14:27:51 +08:00
|
|
|
|
|
|
|
// We don't know what to make of it. Return a NULL region, which
|
|
|
|
// will be interpretted as UnknownVal.
|
2009-10-14 14:55:01 +08:00
|
|
|
return NULL;
|
2009-07-18 14:27:51 +08:00
|
|
|
}
|
2009-07-07 06:23:45 +08:00
|
|
|
|
2009-07-07 04:21:51 +08:00
|
|
|
// Now assume we are casting from pointer to pointer. Other cases should
|
|
|
|
// already be handled.
|
2011-02-19 16:03:18 +08:00
|
|
|
QualType PointeeTy = CastToTy->getPointeeType();
|
2009-08-02 12:12:53 +08:00
|
|
|
QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
|
|
|
|
|
|
|
|
// Handle casts to void*. We just pass the region through.
|
First part of changes to eliminate problems with cv-qualifiers and
sugared types. The basic problem is that our qualifier accessors
(getQualifiers, getCVRQualifiers, isConstQualified, etc.) only look at
the current QualType and not at any qualifiers that come from sugared
types, meaning that we won't see these qualifiers through, e.g.,
typedefs:
typedef const int CInt;
typedef CInt Self;
Self.isConstQualified() currently returns false!
Various bugs (e.g., PR5383) have cropped up all over the front end due
to such problems. I'm addressing this problem by splitting each
qualifier accessor into two versions:
- the "local" version only returns qualifiers on this particular
QualType instance
- the "normal" version that will eventually combine qualifiers from this
QualType instance with the qualifiers on the canonical type to
produce the full set of qualifiers.
This commit adds the local versions and switches a few callers from
the "normal" version (e.g., isConstQualified) over to the "local"
version (e.g., isLocalConstQualified) when that is the right thing to
do, e.g., because we're printing or serializing the qualifiers. Also,
switch a bunch of
Context.getCanonicalType(T1).getUnqualifiedType() == Context.getCanonicalType(T2).getQualifiedType()
expressions over to
Context.hasSameUnqualifiedType(T1, T2)
llvm-svn: 88969
2009-11-17 05:35:15 +08:00
|
|
|
if (CanonPointeeTy.getLocalUnqualifiedType() == Ctx.VoidTy)
|
2009-10-14 14:55:01 +08:00
|
|
|
return R;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-02 12:12:53 +08:00
|
|
|
// Handle casts from compatible types.
|
2009-08-01 14:17:29 +08:00
|
|
|
if (R->isBoundable())
|
2011-08-13 04:02:48 +08:00
|
|
|
if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
|
2010-08-11 14:10:55 +08:00
|
|
|
QualType ObjTy = Ctx.getCanonicalType(TR->getValueType());
|
2009-08-02 12:12:53 +08:00
|
|
|
if (CanonPointeeTy == ObjTy)
|
2009-10-14 14:55:01 +08:00
|
|
|
return R;
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
|
|
|
|
2009-07-07 04:21:51 +08:00
|
|
|
// Process region cast according to the kind of the region being cast.
|
2009-07-07 06:56:37 +08:00
|
|
|
switch (R->getKind()) {
|
2010-01-05 10:18:06 +08:00
|
|
|
case MemRegion::CXXThisRegionKind:
|
2009-12-08 06:05:27 +08:00
|
|
|
case MemRegion::GenericMemSpaceRegionKind:
|
|
|
|
case MemRegion::StackLocalsSpaceRegionKind:
|
|
|
|
case MemRegion::StackArgumentsSpaceRegionKind:
|
|
|
|
case MemRegion::HeapSpaceRegionKind:
|
2009-12-11 14:43:27 +08:00
|
|
|
case MemRegion::UnknownSpaceRegionKind:
|
2012-01-05 07:54:01 +08:00
|
|
|
case MemRegion::StaticGlobalSpaceRegionKind:
|
|
|
|
case MemRegion::GlobalInternalSpaceRegionKind:
|
|
|
|
case MemRegion::GlobalSystemSpaceRegionKind:
|
|
|
|
case MemRegion::GlobalImmutableSpaceRegionKind: {
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Invalid region cast");
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2010-09-02 07:00:46 +08:00
|
|
|
|
2009-11-25 09:32:22 +08:00
|
|
|
case MemRegion::FunctionTextRegionKind:
|
2009-11-26 07:58:21 +08:00
|
|
|
case MemRegion::BlockTextRegionKind:
|
2010-09-02 07:00:46 +08:00
|
|
|
case MemRegion::BlockDataRegionKind:
|
2009-07-07 06:56:37 +08:00
|
|
|
case MemRegion::StringRegionKind:
|
|
|
|
// FIXME: Need to handle arbitrary downcasts.
|
2009-08-01 14:17:29 +08:00
|
|
|
case MemRegion::SymbolicRegionKind:
|
|
|
|
case MemRegion::AllocaRegionKind:
|
2009-07-07 06:56:37 +08:00
|
|
|
case MemRegion::CompoundLiteralRegionKind:
|
|
|
|
case MemRegion::FieldRegionKind:
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
2012-02-28 08:56:05 +08:00
|
|
|
case MemRegion::ObjCStringRegionKind:
|
2009-09-09 23:08:12 +08:00
|
|
|
case MemRegion::VarRegionKind:
|
2010-11-26 16:52:48 +08:00
|
|
|
case MemRegion::CXXTempObjectRegionKind:
|
|
|
|
case MemRegion::CXXBaseObjectRegionKind:
|
2009-10-14 14:55:01 +08:00
|
|
|
return MakeElementRegion(R, PointeeTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
case MemRegion::ElementRegionKind: {
|
|
|
|
// If we are casting from an ElementRegion to another type, the
|
|
|
|
// algorithm is as follows:
|
|
|
|
//
|
|
|
|
// (1) Compute the "raw offset" of the ElementRegion from the
|
|
|
|
// base region. This is done by calling 'getAsRawOffset()'.
|
|
|
|
//
|
2009-09-09 23:08:12 +08:00
|
|
|
// (2a) If we get a 'RegionRawOffset' after calling
|
2009-08-01 14:17:29 +08:00
|
|
|
// 'getAsRawOffset()', determine if the absolute offset
|
2009-09-09 23:08:12 +08:00
|
|
|
// can be exactly divided into chunks of the size of the
|
|
|
|
// casted-pointee type. If so, create a new ElementRegion with
|
2009-08-01 14:17:29 +08:00
|
|
|
// the pointee-cast type as the new ElementType and the index
|
|
|
|
// being the offset divded by the chunk size. If not, create
|
|
|
|
// a new ElementRegion at offset 0 off the raw offset region.
|
|
|
|
//
|
|
|
|
// (2b) If we don't a get a 'RegionRawOffset' after calling
|
|
|
|
// 'getAsRawOffset()', it means that we are at offset 0.
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2009-08-01 14:17:29 +08:00
|
|
|
// FIXME: Handle symbolic raw offsets.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
const ElementRegion *elementR = cast<ElementRegion>(R);
|
2010-08-02 12:56:14 +08:00
|
|
|
const RegionRawOffset &rawOff = elementR->getAsArrayOffset();
|
2009-08-01 14:17:29 +08:00
|
|
|
const MemRegion *baseR = rawOff.getRegion();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// If we cannot compute a raw offset, throw up our hands and return
|
|
|
|
// a NULL MemRegion*.
|
|
|
|
if (!baseR)
|
2009-10-14 14:55:01 +08:00
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-01-24 09:55:39 +08:00
|
|
|
CharUnits off = rawOff.getOffset();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-12 01:06:35 +08:00
|
|
|
if (off.isZero()) {
|
2009-08-01 14:17:29 +08:00
|
|
|
// Edge case: we are at 0 bytes off the beginning of baseR. We
|
|
|
|
// check to see if type we are casting to is the same as the base
|
2009-09-09 23:08:12 +08:00
|
|
|
// region. If so, just return the base region.
|
2011-08-13 04:02:48 +08:00
|
|
|
if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(baseR)) {
|
2010-08-11 14:10:55 +08:00
|
|
|
QualType ObjTy = Ctx.getCanonicalType(TR->getValueType());
|
2009-08-01 14:17:29 +08:00
|
|
|
QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
|
|
|
|
if (CanonPointeeTy == ObjTy)
|
2009-10-14 14:55:01 +08:00
|
|
|
return baseR;
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// Otherwise, create a new ElementRegion at offset 0.
|
2009-10-14 14:55:01 +08:00
|
|
|
return MakeElementRegion(baseR, PointeeTy);
|
2009-07-07 07:47:19 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// We have a non-zero offset from the base region. We want to determine
|
|
|
|
// if the offset can be evenly divided by sizeof(PointeeTy). If so,
|
|
|
|
// we create an ElementRegion whose index is that value. Otherwise, we
|
|
|
|
// create two ElementRegions, one that reflects a raw offset and the other
|
|
|
|
// that reflects the cast.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// Compute the index for the new ElementRegion.
|
|
|
|
int64_t newIndex = 0;
|
|
|
|
const MemRegion *newSuperR = 0;
|
2009-07-07 07:47:19 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// We can only compute sizeof(PointeeTy) if it is a complete type.
|
|
|
|
if (IsCompleteType(Ctx, PointeeTy)) {
|
|
|
|
// Compute the size in **bytes**.
|
2010-01-12 01:06:35 +08:00
|
|
|
CharUnits pointeeTySize = Ctx.getTypeSizeInChars(PointeeTy);
|
2010-04-07 08:46:49 +08:00
|
|
|
if (!pointeeTySize.isZero()) {
|
|
|
|
// Is the offset a multiple of the size? If so, we can layer the
|
|
|
|
// ElementRegion (with elementType == PointeeTy) directly on top of
|
|
|
|
// the base region.
|
|
|
|
if (off % pointeeTySize == 0) {
|
|
|
|
newIndex = off / pointeeTySize;
|
|
|
|
newSuperR = baseR;
|
|
|
|
}
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
if (!newSuperR) {
|
|
|
|
// Create an intermediate ElementRegion to represent the raw byte.
|
|
|
|
// This will be the super region of the final ElementRegion.
|
2010-01-12 01:06:35 +08:00
|
|
|
newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off.getQuantity());
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-14 14:55:01 +08:00
|
|
|
return MakeElementRegion(newSuperR, PointeeTy, newIndex);
|
2009-07-07 04:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("unreachable");
|
2009-07-07 04:21:51 +08:00
|
|
|
}
|
2009-08-26 04:51:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
/// CastRetrievedVal - Used by subclasses of StoreManager to implement
|
|
|
|
/// implicit casts that arise from loads from regions that are reinterpreted
|
|
|
|
/// as another region.
|
2011-08-13 04:02:48 +08:00
|
|
|
SVal StoreManager::CastRetrievedVal(SVal V, const TypedValueRegion *R,
|
2010-01-11 10:33:26 +08:00
|
|
|
QualType castTy, bool performTestOnly) {
|
Add (initial?) static analyzer support for handling C++ references.
This change was a lot bigger than I originally anticipated; among
other things it requires us storing more information in the CFG to
record what block-level expressions need to be evaluated as lvalues.
The big change is that CFGBlocks no longer contain Stmt*'s by
CFGElements. Currently CFGElements just wrap Stmt*, but they also
store a bit indicating whether the block-level expression should be
evalauted as an lvalue. DeclStmts involving the initialization of a
reference require us treating the initialization expression as an
lvalue, even though that information isn't recorded in the AST.
Conceptually this change isn't that complicated, but it required
bubbling up the data through the CFGBuilder, to GRCoreEngine, and
eventually to GRExprEngine.
The addition of CFGElement is also useful for when we want to handle
more control-flow constructs or other data we want to keep in the CFG
that isn't represented well with just a block of statements.
In GRExprEngine, this patch introduces logic for evaluating the
lvalues of references, which currently retrieves the internal "pointer
value" that the reference represents. EvalLoad does a two stage load
to catch null dereferences involving an invalid reference (although
this could possibly be caught earlier during the initialization of a
reference).
Symbols are currently symbolicated using the reference type, instead
of a pointer type, and special handling is required creating
ElementRegions that layer on SymbolicRegions (see the changes to
RegionStoreManager).
Along the way, the DeadStoresChecker also silences warnings involving
dead stores to references. This was the original change I introduced
(which I wrote test cases for) that I realized caused GRExprEngine to
crash.
llvm-svn: 91501
2009-12-16 11:18:58 +08:00
|
|
|
|
2011-12-07 07:12:27 +08:00
|
|
|
if (castTy.isNull() || V.isUnknownOrUndef())
|
2009-11-16 12:49:44 +08:00
|
|
|
return V;
|
Add (initial?) static analyzer support for handling C++ references.
This change was a lot bigger than I originally anticipated; among
other things it requires us storing more information in the CFG to
record what block-level expressions need to be evaluated as lvalues.
The big change is that CFGBlocks no longer contain Stmt*'s by
CFGElements. Currently CFGElements just wrap Stmt*, but they also
store a bit indicating whether the block-level expression should be
evalauted as an lvalue. DeclStmts involving the initialization of a
reference require us treating the initialization expression as an
lvalue, even though that information isn't recorded in the AST.
Conceptually this change isn't that complicated, but it required
bubbling up the data through the CFGBuilder, to GRCoreEngine, and
eventually to GRExprEngine.
The addition of CFGElement is also useful for when we want to handle
more control-flow constructs or other data we want to keep in the CFG
that isn't represented well with just a block of statements.
In GRExprEngine, this patch introduces logic for evaluating the
lvalues of references, which currently retrieves the internal "pointer
value" that the reference represents. EvalLoad does a two stage load
to catch null dereferences involving an invalid reference (although
this could possibly be caught earlier during the initialization of a
reference).
Symbols are currently symbolicated using the reference type, instead
of a pointer type, and special handling is required creating
ElementRegions that layer on SymbolicRegions (see the changes to
RegionStoreManager).
Along the way, the DeadStoresChecker also silences warnings involving
dead stores to references. This was the original change I introduced
(which I wrote test cases for) that I realized caused GRExprEngine to
crash.
llvm-svn: 91501
2009-12-16 11:18:58 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
ASTContext &Ctx = svalBuilder.getContext();
|
2009-12-09 16:32:57 +08:00
|
|
|
|
2010-01-11 10:33:26 +08:00
|
|
|
if (performTestOnly) {
|
|
|
|
// Automatically translate references to pointers.
|
2010-08-11 14:10:55 +08:00
|
|
|
QualType T = R->getValueType();
|
2010-01-11 10:33:26 +08:00
|
|
|
if (const ReferenceType *RT = T->getAs<ReferenceType>())
|
|
|
|
T = Ctx.getPointerType(RT->getPointeeType());
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
assert(svalBuilder.getContext().hasSameUnqualifiedType(castTy, T));
|
2010-01-11 10:33:26 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2011-12-07 07:12:27 +08:00
|
|
|
return svalBuilder.dispatchCast(V, castTy);
|
2009-08-26 04:51:30 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
SVal StoreManager::getLValueFieldOrIvar(const Decl *D, SVal Base) {
|
2010-02-08 15:58:06 +08:00
|
|
|
if (Base.isUnknownOrUndef())
|
|
|
|
return Base;
|
|
|
|
|
|
|
|
Loc BaseL = cast<Loc>(Base);
|
|
|
|
const MemRegion* BaseR = 0;
|
|
|
|
|
|
|
|
switch (BaseL.getSubKind()) {
|
|
|
|
case loc::MemRegionKind:
|
|
|
|
BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case loc::GotoLabelKind:
|
|
|
|
// These are anormal cases. Flag an undefined value.
|
|
|
|
return UndefinedVal();
|
|
|
|
|
|
|
|
case loc::ConcreteIntKind:
|
|
|
|
// 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:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Unhandled Base.");
|
2010-02-08 15:58:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: We must have this check first because ObjCIvarDecl is a subclass
|
|
|
|
// of FieldDecl.
|
|
|
|
if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
|
|
|
|
return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
|
|
|
|
|
|
|
|
return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
|
|
|
|
}
|
2010-02-08 16:17:02 +08:00
|
|
|
|
2012-01-28 20:06:22 +08:00
|
|
|
SVal StoreManager::getLValueIvar(const ObjCIvarDecl *decl, SVal base) {
|
|
|
|
return getLValueFieldOrIvar(decl, base);
|
|
|
|
}
|
|
|
|
|
2010-09-15 11:13:30 +08:00
|
|
|
SVal StoreManager::getLValueElement(QualType elementType, NonLoc Offset,
|
2010-02-08 16:17:02 +08:00
|
|
|
SVal Base) {
|
|
|
|
|
|
|
|
// If the base is an unknown or undefined value, just return it back.
|
|
|
|
// FIXME: For absolute pointer addresses, we just return that value back as
|
|
|
|
// well, although in reality we should return the offset added to that
|
|
|
|
// value.
|
|
|
|
if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
|
|
|
|
return Base;
|
|
|
|
|
|
|
|
const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
|
|
|
|
|
|
|
|
// Pointer of any type can be cast and used as array base.
|
|
|
|
const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
|
|
|
|
|
|
|
|
// Convert the offset to the appropriate size and signedness.
|
2010-12-02 15:49:45 +08:00
|
|
|
Offset = cast<NonLoc>(svalBuilder.convertToArrayIndex(Offset));
|
2010-02-08 16:17:02 +08:00
|
|
|
|
|
|
|
if (!ElemR) {
|
|
|
|
//
|
|
|
|
// If the base region is not an ElementRegion, create one.
|
|
|
|
// This can happen in the following example:
|
|
|
|
//
|
|
|
|
// char *p = __builtin_alloc(10);
|
|
|
|
// p[1] = 8;
|
|
|
|
//
|
|
|
|
// Observe that 'p' binds to an AllocaRegion.
|
|
|
|
//
|
|
|
|
return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
|
|
|
|
BaseRegion, Ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
SVal BaseIdx = ElemR->getIndex();
|
|
|
|
|
|
|
|
if (!isa<nonloc::ConcreteInt>(BaseIdx))
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
|
2010-08-16 09:15:17 +08:00
|
|
|
|
|
|
|
// Only allow non-integer offsets if the base region has no offset itself.
|
|
|
|
// FIXME: This is a somewhat arbitrary restriction. We should be using
|
2010-12-02 05:28:31 +08:00
|
|
|
// SValBuilder here to add the two offsets without checking their types.
|
2010-08-16 09:15:17 +08:00
|
|
|
if (!isa<nonloc::ConcreteInt>(Offset)) {
|
|
|
|
if (isa<ElementRegion>(BaseRegion->StripCasts()))
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
|
|
|
|
ElemR->getSuperRegion(),
|
|
|
|
Ctx));
|
|
|
|
}
|
|
|
|
|
2010-02-08 16:17:02 +08:00
|
|
|
const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
|
|
|
|
assert(BaseIdxI.isSigned());
|
|
|
|
|
|
|
|
// Compute the new index.
|
2010-12-02 15:49:45 +08:00
|
|
|
nonloc::ConcreteInt NewIdx(svalBuilder.getBasicValueFactory().getValue(BaseIdxI +
|
2010-09-15 11:13:30 +08:00
|
|
|
OffI));
|
2010-02-08 16:17:02 +08:00
|
|
|
|
|
|
|
// Construct the new ElementRegion.
|
|
|
|
const MemRegion *ArrayR = ElemR->getSuperRegion();
|
|
|
|
return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
|
|
|
|
Ctx));
|
|
|
|
}
|
2011-07-29 07:08:16 +08:00
|
|
|
|
|
|
|
StoreManager::BindingsHandler::~BindingsHandler() {}
|
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void SubRegionMap::anchor() { }
|
|
|
|
void SubRegionMap::Visitor::anchor() { }
|