Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 1 | //== 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 | |
| 17 | using namespace clang; |
| 18 | |
| 19 | StoreManager::StoreManager(GRStateManager &stateMgr) |
| 20 | : ValMgr(stateMgr.getValueManager()), |
| 21 | StateMgr(stateMgr), |
| 22 | MRMgr(ValMgr.getRegionManager()) {} |
| 23 | |
| 24 | StoreManager::CastResult |
| 25 | StoreManager::CastRegion(const GRState* state, const MemRegion* R, |
Ted Kremenek | fd6b4f3 | 2009-05-04 06:35:49 +0000 | [diff] [blame] | 26 | QualType CastToTy) { |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 28 | ASTContext& Ctx = StateMgr.getContext(); |
| 29 | |
| 30 | // We need to know the real type of CastToTy. |
| 31 | QualType ToTy = Ctx.getCanonicalType(CastToTy); |
| 32 | |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 33 | // Return the same region if the region types are compatible. |
| 34 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) { |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 35 | QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx)); |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 36 | |
| 37 | if (Ta == ToTy) |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 38 | return CastResult(state, R); |
| 39 | } |
| 40 | |
Ted Kremenek | fd6b4f3 | 2009-05-04 06:35:49 +0000 | [diff] [blame] | 41 | 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 Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 46 | |
| 47 | // Casts to void* only removes TypedViewRegion. If there is no |
| 48 | // TypedViewRegion, leave the region untouched. This happens when: |
| 49 | // |
| 50 | // void foo(void*); |
| 51 | // ... |
| 52 | // void bar() { |
| 53 | // int x; |
| 54 | // foo(&x); |
| 55 | // } |
| 56 | |
| 57 | if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) |
| 58 | R = TR->removeViews(); |
| 59 | |
| 60 | return CastResult(state, R); |
| 61 | } |
Ted Kremenek | fd6b4f3 | 2009-05-04 06:35:49 +0000 | [diff] [blame] | 62 | else if (Pointee->isIntegerType()) { |
| 63 | // FIXME: At some point, it stands to reason that this 'dyn_cast' should |
| 64 | // become a 'cast' and that 'R' will always be a TypedRegion. |
| 65 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 66 | // Check if we are casting to a region with an integer type. We now |
| 67 | // the types aren't the same, so we construct an ElementRegion. |
| 68 | // FIXME: We should have a standard query function to get the size |
| 69 | // of the array index. |
| 70 | SVal Idx = ValMgr.makeZeroVal(ValMgr.getContext().VoidPtrTy); |
Ted Kremenek | 20bd746 | 2009-05-04 07:04:36 +0000 | [diff] [blame^] | 71 | |
| 72 | // If the super region is an element region, strip it away. |
| 73 | // FIXME: Is this the right thing to do in all cases? |
| 74 | const TypedRegion *Base = isa<ElementRegion>(TR) ? |
| 75 | cast<TypedRegion>(TR->getSuperRegion()) : TR; |
| 76 | ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base); |
Ted Kremenek | fd6b4f3 | 2009-05-04 06:35:49 +0000 | [diff] [blame] | 77 | return CastResult(state, ER); |
| 78 | } |
| 79 | } |
| 80 | } |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | a8607d1 | 2009-05-01 19:22:20 +0000 | [diff] [blame] | 82 | // FIXME: Need to handle arbitrary downcasts. |
| 83 | // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion |
| 84 | // or an AllocaRegion is cast to another view, thus causing the memory |
| 85 | // to be re-used for a different purpose. |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | a8607d1 | 2009-05-01 19:22:20 +0000 | [diff] [blame] | 87 | if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) { |
| 88 | const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R); |
| 89 | return CastResult(AddRegionView(state, ViewR, R), ViewR); |
| 90 | } |
| 91 | |
| 92 | return CastResult(state, R); |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 93 | } |