blob: 658dfa1186cff47d0a16ced0faa1eccbb0780093 [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) {
59 // Common case: we have an appropriately sized integer.
60 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&V)) {
61 const llvm::APSInt& I = CI->getValue();
62 if (I.getBitWidth() == ArrayIndexWidth && I.isSigned())
63 return V;
64 }
65
66 return SVator->EvalCast(V, ArrayIndexTy);
67}
68
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +000069SVal ValueManager::getRegionValueSymbolVal(const MemRegion* R, QualType T) {
70 SymbolRef sym = SymMgr.getRegionValueSymbol(R, T);
71
72 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
73 if (T.isNull())
74 T = TR->getValueType(SymMgr.getContext());
75
Ted Kremenek675bef62009-07-18 06:27:01 +000076 // If T is of function pointer type or a block pointer type, create a
77 // CodeTextRegion wrapping that symbol.
78 if (T->isFunctionPointerType() || T->isBlockPointerType()) {
Zhongxing Xud91ee272009-06-23 09:02:15 +000079 return loc::MemRegionVal(MemMgr.getCodeTextRegion(sym, T));
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +000080 }
81
82 if (Loc::IsLocType(T))
Zhongxing Xud91ee272009-06-23 09:02:15 +000083 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +000084
85 // Only handle integers for now.
86 if (T->isIntegerType() && T->isScalarType())
Zhongxing Xud91ee272009-06-23 09:02:15 +000087 return nonloc::SymbolVal(sym);
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +000088 }
89
90 return UnknownVal();
91}
92
93SVal ValueManager::getConjuredSymbolVal(const Expr* E, unsigned Count) {
94 QualType T = E->getType();
95 SymbolRef sym = SymMgr.getConjuredSymbol(E, Count);
96
Ted Kremenek675bef62009-07-18 06:27:01 +000097 // If T is of function pointer type or a block pointer type, create a
98 // CodeTextRegion wrapping a symbol.
99 if (T->isFunctionPointerType() || T->isBlockPointerType()) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000100 return loc::MemRegionVal(MemMgr.getCodeTextRegion(sym, T));
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000101 }
102
103 if (Loc::IsLocType(T))
Zhongxing Xud91ee272009-06-23 09:02:15 +0000104 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000105
106 if (T->isIntegerType() && T->isScalarType())
Zhongxing Xud91ee272009-06-23 09:02:15 +0000107 return nonloc::SymbolVal(sym);
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000108
109 return UnknownVal();
110}
111
112SVal ValueManager::getConjuredSymbolVal(const Expr* E, QualType T,
113 unsigned Count) {
114
115 SymbolRef sym = SymMgr.getConjuredSymbol(E, T, Count);
116
Ted Kremenek675bef62009-07-18 06:27:01 +0000117 // If T is of function pointer type or a block pointer type, create a
118 // CodeTextRegion wrapping a symbol.
119 if (T->isFunctionPointerType() || T->isBlockPointerType()) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000120 return loc::MemRegionVal(MemMgr.getCodeTextRegion(sym, T));
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000121 }
122
123 if (Loc::IsLocType(T))
Zhongxing Xud91ee272009-06-23 09:02:15 +0000124 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000125
126 if (T->isIntegerType() && T->isScalarType())
Zhongxing Xud91ee272009-06-23 09:02:15 +0000127 return nonloc::SymbolVal(sym);
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000128
129 return UnknownVal();
130}
131
Ted Kremenekfb91c702009-07-15 02:27:32 +0000132
133SVal ValueManager::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
134 const TypedRegion *R) {
135 SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, R);
136
137 QualType T = R->getValueType(R->getContext());
138
139 if (Loc::IsLocType(T))
140 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
141
142 if (T->isIntegerType() && T->isScalarType())
143 return nonloc::SymbolVal(sym);
144
145 return UnknownVal();
146}
147
Zhongxing Xu2ace5cd2009-06-23 06:22:22 +0000148SVal ValueManager::getFunctionPointer(const FunctionDecl* FD) {
149 CodeTextRegion* R
150 = MemMgr.getCodeTextRegion(FD, Context.getPointerType(FD->getType()));
151 return loc::MemRegionVal(R);
152}