blob: 65e90dec33d591f330beb7dd3957baabe0be22dd [file] [log] [blame]
Ted Kremenekc62abc12009-04-21 21:51:34 +00001//== Store.cpp - Interface for maps from Locations to Values ----*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defined the types Store and StoreManager.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/PathSensitive/Store.h"
15#include "clang/Analysis/PathSensitive/GRState.h"
16
17using namespace clang;
18
19StoreManager::StoreManager(GRStateManager &stateMgr)
20 : ValMgr(stateMgr.getValueManager()),
21 StateMgr(stateMgr),
22 MRMgr(ValMgr.getRegionManager()) {}
23
24StoreManager::CastResult
25StoreManager::CastRegion(const GRState* state, const MemRegion* R,
26 QualType CastToTy) {
27
Ted Kremenek30d1b992009-04-21 23:31:46 +000028 ASTContext& Ctx = StateMgr.getContext();
29
30 // We need to know the real type of CastToTy.
31 QualType ToTy = Ctx.getCanonicalType(CastToTy);
32
Ted Kremenekc62abc12009-04-21 21:51:34 +000033 // Return the same region if the region types are compatible.
34 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
Ted Kremenekc62abc12009-04-21 21:51:34 +000035 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
Ted Kremenek30d1b992009-04-21 23:31:46 +000036
37 if (Ta == ToTy)
Ted Kremenekc62abc12009-04-21 21:51:34 +000038 return CastResult(state, R);
39 }
40
Ted Kremenek30d1b992009-04-21 23:31:46 +000041 // Check if we are casting to 'void*'.
42 // FIXME: Handle arbitrary upcasts.
43 if (const PointerType* PTy = dyn_cast<PointerType>(ToTy.getTypePtr()))
44 if (PTy->getPointeeType()->isVoidType()) {
45
46 // Casts to void* only removes TypedViewRegion. If there is no
47 // TypedViewRegion, leave the region untouched. This happens when:
48 //
49 // void foo(void*);
50 // ...
51 // void bar() {
52 // int x;
53 // foo(&x);
54 // }
55
56 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R))
57 R = TR->removeViews();
58
59 return CastResult(state, R);
60 }
61
Ted Kremeneka8607d12009-05-01 19:22:20 +000062 // FIXME: Need to handle arbitrary downcasts.
63 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
64 // or an AllocaRegion is cast to another view, thus causing the memory
65 // to be re-used for a different purpose.
Ted Kremenek30d1b992009-04-21 23:31:46 +000066
Ted Kremeneka8607d12009-05-01 19:22:20 +000067 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
68 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
69 return CastResult(AddRegionView(state, ViewR, R), ViewR);
70 }
71
72 return CastResult(state, R);
Ted Kremenekc62abc12009-04-21 21:51:34 +000073}