blob: 13326ab31f8230a867460cb8ef01300247014272 [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,
Ted Kremenekfd6b4f32009-05-04 06:35:49 +000026 QualType CastToTy) {
Ted Kremenekc62abc12009-04-21 21:51:34 +000027
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)) {
Zhongxing Xuff697822009-05-09 00:50:33 +000035 QualType Ta = Ctx.getCanonicalType(TR->getLocationType(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 Kremenekfd6b4f32009-05-04 06:35:49 +000041 if (const PointerType* PTy = dyn_cast<PointerType>(ToTy.getTypePtr())) {
42 // Check if we are casting to 'void*'.
43 // FIXME: Handle arbitrary upcasts.
44 QualType Pointee = PTy->getPointeeType();
45 if (Pointee->isVoidType()) {
Ted Kremenek30d1b992009-04-21 23:31:46 +000046
Ted Kremenek42530512009-05-06 18:19:24 +000047 do {
48 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
49 // Casts to void* removes TypedViewRegion. This happens when:
50 //
51 // void foo(void*);
52 // ...
53 // void bar() {
54 // int x;
55 // foo(&x);
56 // }
57 //
58 R = TR->removeViews();
59 continue;
60 }
61 else if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
62 // Casts to void* also removes ElementRegions. This happens when:
63 //
64 // void foo(void*);
65 // ...
66 // void bar() {
67 // int x;
68 // foo((char*)&x);
69 // }
70 //
71 R = ER->getSuperRegion();
72 continue;
73 }
74 else
75 break;
76 }
77 while (0);
Ted Kremenek30d1b992009-04-21 23:31:46 +000078
79 return CastResult(state, R);
80 }
Ted Kremenekfd6b4f32009-05-04 06:35:49 +000081 else if (Pointee->isIntegerType()) {
82 // FIXME: At some point, it stands to reason that this 'dyn_cast' should
83 // become a 'cast' and that 'R' will always be a TypedRegion.
84 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
85 // Check if we are casting to a region with an integer type. We now
86 // the types aren't the same, so we construct an ElementRegion.
Ted Kremenekcd9392f2009-05-04 15:17:38 +000087 SVal Idx = ValMgr.makeZeroArrayIndex();
Ted Kremenek20bd7462009-05-04 07:04:36 +000088
89 // If the super region is an element region, strip it away.
90 // FIXME: Is this the right thing to do in all cases?
91 const TypedRegion *Base = isa<ElementRegion>(TR) ?
92 cast<TypedRegion>(TR->getSuperRegion()) : TR;
93 ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base);
Ted Kremenekfd6b4f32009-05-04 06:35:49 +000094 return CastResult(state, ER);
95 }
96 }
97 }
Ted Kremenek30d1b992009-04-21 23:31:46 +000098
Ted Kremeneka8607d12009-05-01 19:22:20 +000099 // FIXME: Need to handle arbitrary downcasts.
100 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
101 // or an AllocaRegion is cast to another view, thus causing the memory
102 // to be re-used for a different purpose.
Ted Kremenek30d1b992009-04-21 23:31:46 +0000103
Ted Kremeneka8607d12009-05-01 19:22:20 +0000104 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
105 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
106 return CastResult(AddRegionView(state, ViewR, R), ViewR);
107 }
108
109 return CastResult(state, R);
Ted Kremenekc62abc12009-04-21 21:51:34 +0000110}