blob: 1e7a5fced0acf3a98f244aade204cc9588ceb246 [file] [log] [blame]
Zhongxing Xu0808f702009-06-23 06:22:22 +00001//== ValueManager.cpp - Aggregate manager of symbols and SVals --*- 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 defines ValueManager, a class that manages symbolic values
11// and SVals created for use by GRExprEngine and related classes. It
12// wraps and owns SymbolManager, MemRegionManager, and BasicValueFactory.
13//
14//===----------------------------------------------------------------------===//
15
16#include "clang/Analysis/PathSensitive/ValueManager.h"
17
18using namespace clang;
19using namespace llvm;
20
21//===----------------------------------------------------------------------===//
22// Utility methods for constructing SVals.
23//===----------------------------------------------------------------------===//
24
Ted Kremenek7020eae2009-09-11 22:07:28 +000025DefinedOrUnknownSVal ValueManager::makeZeroVal(QualType T) {
Zhongxing Xu0808f702009-06-23 06:22:22 +000026 if (Loc::IsLocType(T))
Zhongxing Xu7718ae42009-06-23 09:02:15 +000027 return makeNull();
Zhongxing Xu0808f702009-06-23 06:22:22 +000028
29 if (T->isIntegerType())
Zhongxing Xu7718ae42009-06-23 09:02:15 +000030 return makeIntVal(0, T);
Mike Stump11289f42009-09-09 15:08:12 +000031
Zhongxing Xu0808f702009-06-23 06:22:22 +000032 // FIXME: Handle floats.
33 // FIXME: Handle structs.
Mike Stump11289f42009-09-09 15:08:12 +000034 return UnknownVal();
Zhongxing Xu0808f702009-06-23 06:22:22 +000035}
36
Zhongxing Xu0808f702009-06-23 06:22:22 +000037//===----------------------------------------------------------------------===//
38// Utility methods for constructing Non-Locs.
39//===----------------------------------------------------------------------===//
40
Zhongxing Xu0808f702009-06-23 06:22:22 +000041NonLoc ValueManager::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
42 const APSInt& v, QualType T) {
43 // The Environment ensures we always get a persistent APSInt in
44 // BasicValueFactory, so we don't need to get the APSInt from
45 // BasicValueFactory again.
46 assert(!Loc::IsLocType(T));
47 return nonloc::SymExprVal(SymMgr.getSymIntExpr(lhs, op, v, T));
48}
49
50NonLoc ValueManager::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
51 const SymExpr *rhs, QualType T) {
52 assert(SymMgr.getType(lhs) == SymMgr.getType(rhs));
53 assert(!Loc::IsLocType(T));
54 return nonloc::SymExprVal(SymMgr.getSymSymExpr(lhs, op, rhs, T));
55}
56
Zhongxing Xu0808f702009-06-23 06:22:22 +000057
Ted Kremenekf267a152009-07-16 01:32:00 +000058SVal ValueManager::convertToArrayIndex(SVal V) {
Ted Kremenekac7c7242009-07-21 21:03:30 +000059 if (V.isUnknownOrUndef())
60 return V;
Mike Stump11289f42009-09-09 15:08:12 +000061
Ted Kremenekf267a152009-07-16 01:32:00 +000062 // Common case: we have an appropriately sized integer.
63 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&V)) {
64 const llvm::APSInt& I = CI->getValue();
65 if (I.getBitWidth() == ArrayIndexWidth && I.isSigned())
66 return V;
67 }
Mike Stump11289f42009-09-09 15:08:12 +000068
Ted Kremenekac7c7242009-07-21 21:03:30 +000069 return SVator->EvalCastNL(cast<NonLoc>(V), ArrayIndexTy);
Ted Kremenekf267a152009-07-16 01:32:00 +000070}
71
Ted Kremenek7020eae2009-09-11 22:07:28 +000072DefinedOrUnknownSVal ValueManager::getRegionValueSymbolVal(const MemRegion* R,
73 QualType T) {
Zhongxing Xu0808f702009-06-23 06:22:22 +000074
Ted Kremenek1f22aa72009-08-01 06:17:29 +000075 if (T.isNull()) {
76 const TypedRegion* TR = cast<TypedRegion>(R);
77 T = TR->getValueType(SymMgr.getContext());
Zhongxing Xu0808f702009-06-23 06:22:22 +000078 }
Mike Stump11289f42009-09-09 15:08:12 +000079
Ted Kremenek1f22aa72009-08-01 06:17:29 +000080 if (!SymbolManager::canSymbolicate(T))
81 return UnknownVal();
Zhongxing Xu0808f702009-06-23 06:22:22 +000082
Ted Kremenek1f22aa72009-08-01 06:17:29 +000083 SymbolRef sym = SymMgr.getRegionValueSymbol(R, T);
Mike Stump11289f42009-09-09 15:08:12 +000084
Ted Kremenek1f22aa72009-08-01 06:17:29 +000085 if (Loc::IsLocType(T))
86 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Mike Stump11289f42009-09-09 15:08:12 +000087
Ted Kremenek1f22aa72009-08-01 06:17:29 +000088 return nonloc::SymbolVal(sym);
Zhongxing Xu0808f702009-06-23 06:22:22 +000089}
90
Ted Kremeneke41b81e2009-09-27 20:45:21 +000091DefinedOrUnknownSVal ValueManager::getConjuredSymbolVal(const void *SymbolTag,
92 const Expr *E,
93 unsigned Count) {
Zhongxing Xu0808f702009-06-23 06:22:22 +000094 QualType T = E->getType();
Mike Stump11289f42009-09-09 15:08:12 +000095
Ted Kremenek1f22aa72009-08-01 06:17:29 +000096 if (!SymbolManager::canSymbolicate(T))
97 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +000098
Ted Kremeneke41b81e2009-09-27 20:45:21 +000099 SymbolRef sym = SymMgr.getConjuredSymbol(E, Count, SymbolTag);
Zhongxing Xu0808f702009-06-23 06:22:22 +0000100
Zhongxing Xu0808f702009-06-23 06:22:22 +0000101 if (Loc::IsLocType(T))
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000102 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xu0808f702009-06-23 06:22:22 +0000103
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000104 return nonloc::SymbolVal(sym);
Zhongxing Xu0808f702009-06-23 06:22:22 +0000105}
106
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000107DefinedOrUnknownSVal ValueManager::getConjuredSymbolVal(const void *SymbolTag,
108 const Expr *E,
Ted Kremenek7020eae2009-09-11 22:07:28 +0000109 QualType T,
110 unsigned Count) {
111
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000112 if (!SymbolManager::canSymbolicate(T))
113 return UnknownVal();
Zhongxing Xu0808f702009-06-23 06:22:22 +0000114
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000115 SymbolRef sym = SymMgr.getConjuredSymbol(E, T, Count, SymbolTag);
Zhongxing Xu0808f702009-06-23 06:22:22 +0000116
Zhongxing Xu0808f702009-06-23 06:22:22 +0000117 if (Loc::IsLocType(T))
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000118 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xu0808f702009-06-23 06:22:22 +0000119
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000120 return nonloc::SymbolVal(sym);
Zhongxing Xu0808f702009-06-23 06:22:22 +0000121}
122
Ted Kremenekc6c21572009-07-15 02:27:32 +0000123
Ted Kremenek7020eae2009-09-11 22:07:28 +0000124DefinedOrUnknownSVal
125ValueManager::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
126 const TypedRegion *R) {
Ted Kremenekc6c21572009-07-15 02:27:32 +0000127 QualType T = R->getValueType(R->getContext());
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000128
129 if (!SymbolManager::canSymbolicate(T))
130 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000131
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000132 SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, R);
Mike Stump11289f42009-09-09 15:08:12 +0000133
Ted Kremenekc6c21572009-07-15 02:27:32 +0000134 if (Loc::IsLocType(T))
135 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Mike Stump11289f42009-09-09 15:08:12 +0000136
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000137 return nonloc::SymbolVal(sym);
Ted Kremenekc6c21572009-07-15 02:27:32 +0000138}
139
Ted Kremenek7020eae2009-09-11 22:07:28 +0000140DefinedSVal ValueManager::getFunctionPointer(const FunctionDecl* FD) {
Ted Kremenek721fcc02009-12-04 00:26:31 +0000141 return loc::MemRegionVal(MemMgr.getFunctionTextRegion(FD));
Zhongxing Xu0808f702009-06-23 06:22:22 +0000142}
Ted Kremenek10a50e72009-11-25 01:32:22 +0000143
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000144DefinedSVal ValueManager::getBlockPointer(const BlockDecl *D,
145 CanQualType locTy,
146 const LocationContext *LC) {
Ted Kremenek721fcc02009-12-04 00:26:31 +0000147 const BlockTextRegion *BC = MemMgr.getBlockTextRegion(D, locTy);
148 const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, LC);
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000149 return loc::MemRegionVal(BD);
Ted Kremenek10a50e72009-11-25 01:32:22 +0000150}
151