blob: 9c3dbdd24e5b9ea074380ce5fd4bc596d8d06c1b [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 Kremenek7020eae2009-09-11 22:07:28 +000091DefinedOrUnknownSVal ValueManager::getConjuredSymbolVal(const Expr *E, unsigned Count) {
Zhongxing Xu0808f702009-06-23 06:22:22 +000092 QualType T = E->getType();
Mike Stump11289f42009-09-09 15:08:12 +000093
Ted Kremenek1f22aa72009-08-01 06:17:29 +000094 if (!SymbolManager::canSymbolicate(T))
95 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +000096
Zhongxing Xu0808f702009-06-23 06:22:22 +000097 SymbolRef sym = SymMgr.getConjuredSymbol(E, Count);
98
Zhongxing Xu0808f702009-06-23 06:22:22 +000099 if (Loc::IsLocType(T))
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000100 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xu0808f702009-06-23 06:22:22 +0000101
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000102 return nonloc::SymbolVal(sym);
Zhongxing Xu0808f702009-06-23 06:22:22 +0000103}
104
Ted Kremenek7020eae2009-09-11 22:07:28 +0000105DefinedOrUnknownSVal ValueManager::getConjuredSymbolVal(const Expr *E,
106 QualType T,
107 unsigned Count) {
108
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000109 if (!SymbolManager::canSymbolicate(T))
110 return UnknownVal();
Zhongxing Xu0808f702009-06-23 06:22:22 +0000111
112 SymbolRef sym = SymMgr.getConjuredSymbol(E, T, Count);
113
Zhongxing Xu0808f702009-06-23 06:22:22 +0000114 if (Loc::IsLocType(T))
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000115 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xu0808f702009-06-23 06:22:22 +0000116
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000117 return nonloc::SymbolVal(sym);
Zhongxing Xu0808f702009-06-23 06:22:22 +0000118}
119
Ted Kremenekc6c21572009-07-15 02:27:32 +0000120
Ted Kremenek7020eae2009-09-11 22:07:28 +0000121DefinedOrUnknownSVal
122ValueManager::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
123 const TypedRegion *R) {
Ted Kremenekc6c21572009-07-15 02:27:32 +0000124 QualType T = R->getValueType(R->getContext());
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000125
126 if (!SymbolManager::canSymbolicate(T))
127 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000128
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000129 SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, R);
Mike Stump11289f42009-09-09 15:08:12 +0000130
Ted Kremenekc6c21572009-07-15 02:27:32 +0000131 if (Loc::IsLocType(T))
132 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Mike Stump11289f42009-09-09 15:08:12 +0000133
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000134 return nonloc::SymbolVal(sym);
Ted Kremenekc6c21572009-07-15 02:27:32 +0000135}
136
Ted Kremenek7020eae2009-09-11 22:07:28 +0000137DefinedSVal ValueManager::getFunctionPointer(const FunctionDecl* FD) {
Ted Kremenek198a8c52009-08-28 04:49:15 +0000138 CodeTextRegion *R = MemMgr.getCodeTextRegion(FD);
Zhongxing Xu0808f702009-06-23 06:22:22 +0000139 return loc::MemRegionVal(R);
140}