blob: 6e6187151aee811abe8f1d941ecbef173ffb0145 [file] [log] [blame]
Zhongxing Xu2ace5cd2009-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
25SVal ValueManager::makeZeroVal(QualType T) {
26 if (Loc::IsLocType(T))
Zhongxing Xud91ee272009-06-23 09:02:15 +000027 return makeNull();
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +000028
29 if (T->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +000030 return makeIntVal(0, T);
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +000031
32 // FIXME: Handle floats.
33 // FIXME: Handle structs.
34 return UnknownVal();
35}
36
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +000037//===----------------------------------------------------------------------===//
38// Utility methods for constructing Non-Locs.
39//===----------------------------------------------------------------------===//
40
Zhongxing Xu2ace5cd2009-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 Xu2ace5cd2009-06-23 06:22:22 +000057
Ted Kremenek06669c82009-07-16 01:32:00 +000058SVal ValueManager::convertToArrayIndex(SVal V) {
Ted Kremenek32c3fa42009-07-21 21:03:30 +000059 if (V.isUnknownOrUndef())
60 return V;
61
Ted Kremenek06669c82009-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 }
68
Ted Kremenek32c3fa42009-07-21 21:03:30 +000069 return SVator->EvalCastNL(cast<NonLoc>(V), ArrayIndexTy);
Ted Kremenek06669c82009-07-16 01:32:00 +000070}
71
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +000072SVal ValueManager::getRegionValueSymbolVal(const MemRegion* R, QualType T) {
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +000073
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000074 if (T.isNull()) {
75 const TypedRegion* TR = cast<TypedRegion>(R);
76 T = TR->getValueType(SymMgr.getContext());
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +000077 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000078
79 if (!SymbolManager::canSymbolicate(T))
80 return UnknownVal();
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +000081
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000082 SymbolRef sym = SymMgr.getRegionValueSymbol(R, T);
83
84 // If T is of function pointer type or a block pointer type, create a
85 // CodeTextRegion wrapping that symbol.
86 if (T->isFunctionPointerType() || T->isBlockPointerType())
87 return loc::MemRegionVal(MemMgr.getCodeTextRegion(sym, T));
88
89 if (Loc::IsLocType(T))
90 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
91
92 return nonloc::SymbolVal(sym);
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +000093}
94
95SVal ValueManager::getConjuredSymbolVal(const Expr* E, unsigned Count) {
96 QualType T = E->getType();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000097
98 if (!SymbolManager::canSymbolicate(T))
99 return UnknownVal();
100
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000101 SymbolRef sym = SymMgr.getConjuredSymbol(E, Count);
102
Ted Kremenek675bef62009-07-18 06:27:01 +0000103 // If T is of function pointer type or a block pointer type, create a
104 // CodeTextRegion wrapping a symbol.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000105 if (T->isFunctionPointerType() || T->isBlockPointerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +0000106 return loc::MemRegionVal(MemMgr.getCodeTextRegion(sym, T));
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000107
108 if (Loc::IsLocType(T))
Zhongxing Xud91ee272009-06-23 09:02:15 +0000109 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000110
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000111 return nonloc::SymbolVal(sym);
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000112}
113
114SVal ValueManager::getConjuredSymbolVal(const Expr* E, QualType T,
115 unsigned Count) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000116
117 if (!SymbolManager::canSymbolicate(T))
118 return UnknownVal();
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000119
120 SymbolRef sym = SymMgr.getConjuredSymbol(E, T, Count);
121
Ted Kremenek675bef62009-07-18 06:27:01 +0000122 // If T is of function pointer type or a block pointer type, create a
123 // CodeTextRegion wrapping a symbol.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000124 if (T->isFunctionPointerType() || T->isBlockPointerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +0000125 return loc::MemRegionVal(MemMgr.getCodeTextRegion(sym, T));
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000126
127 if (Loc::IsLocType(T))
Zhongxing Xud91ee272009-06-23 09:02:15 +0000128 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000129
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000130 return nonloc::SymbolVal(sym);
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000131}
132
Ted Kremenekfb91c702009-07-15 02:27:32 +0000133
134SVal ValueManager::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
135 const TypedRegion *R) {
Ted Kremenekfb91c702009-07-15 02:27:32 +0000136 QualType T = R->getValueType(R->getContext());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000137
138 if (!SymbolManager::canSymbolicate(T))
139 return UnknownVal();
140
141 SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, R);
Ted Kremenekfb91c702009-07-15 02:27:32 +0000142
143 if (Loc::IsLocType(T))
144 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
145
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000146 return nonloc::SymbolVal(sym);
Ted Kremenekfb91c702009-07-15 02:27:32 +0000147}
148
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000149SVal ValueManager::getFunctionPointer(const FunctionDecl* FD) {
150 CodeTextRegion* R
151 = MemMgr.getCodeTextRegion(FD, Context.getPointerType(FD->getType()));
152 return loc::MemRegionVal(R);
153}