blob: ee6b5cbeeb358c244f1593c2c5e8d3e5df9f1067 [file] [log] [blame]
Zhongxing Xua3a5a282009-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 Xue32c7652009-06-23 09:02:15 +000027 return makeNull();
Zhongxing Xua3a5a282009-06-23 06:22:22 +000028
29 if (T->isIntegerType())
Zhongxing Xue32c7652009-06-23 09:02:15 +000030 return makeIntVal(0, T);
Zhongxing Xua3a5a282009-06-23 06:22:22 +000031
32 // FIXME: Handle floats.
33 // FIXME: Handle structs.
34 return UnknownVal();
35}
36
Zhongxing Xua3a5a282009-06-23 06:22:22 +000037//===----------------------------------------------------------------------===//
38// Utility methods for constructing Non-Locs.
39//===----------------------------------------------------------------------===//
40
Zhongxing Xua3a5a282009-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 Xua3a5a282009-06-23 06:22:22 +000057
Ted Kremenek40d9c582009-07-16 01:32:00 +000058SVal ValueManager::convertToArrayIndex(SVal V) {
Ted Kremenek6cd31072009-07-21 21:03:30 +000059 if (V.isUnknownOrUndef())
60 return V;
61
Ted Kremenek40d9c582009-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 Kremenek6cd31072009-07-21 21:03:30 +000069 return SVator->EvalCastNL(cast<NonLoc>(V), ArrayIndexTy);
Ted Kremenek40d9c582009-07-16 01:32:00 +000070}
71
Zhongxing Xua3a5a282009-06-23 06:22:22 +000072SVal ValueManager::getRegionValueSymbolVal(const MemRegion* R, QualType T) {
73 SymbolRef sym = SymMgr.getRegionValueSymbol(R, T);
74
75 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
76 if (T.isNull())
77 T = TR->getValueType(SymMgr.getContext());
78
Ted Kremenekfa0150c2009-07-18 06:27:01 +000079 // If T is of function pointer type or a block pointer type, create a
80 // CodeTextRegion wrapping that symbol.
81 if (T->isFunctionPointerType() || T->isBlockPointerType()) {
Zhongxing Xue32c7652009-06-23 09:02:15 +000082 return loc::MemRegionVal(MemMgr.getCodeTextRegion(sym, T));
Zhongxing Xua3a5a282009-06-23 06:22:22 +000083 }
84
85 if (Loc::IsLocType(T))
Zhongxing Xue32c7652009-06-23 09:02:15 +000086 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xua3a5a282009-06-23 06:22:22 +000087
88 // Only handle integers for now.
89 if (T->isIntegerType() && T->isScalarType())
Zhongxing Xue32c7652009-06-23 09:02:15 +000090 return nonloc::SymbolVal(sym);
Zhongxing Xua3a5a282009-06-23 06:22:22 +000091 }
92
93 return UnknownVal();
94}
95
96SVal ValueManager::getConjuredSymbolVal(const Expr* E, unsigned Count) {
97 QualType T = E->getType();
98 SymbolRef sym = SymMgr.getConjuredSymbol(E, Count);
99
Ted Kremenekfa0150c2009-07-18 06:27:01 +0000100 // If T is of function pointer type or a block pointer type, create a
101 // CodeTextRegion wrapping a symbol.
102 if (T->isFunctionPointerType() || T->isBlockPointerType()) {
Zhongxing Xue32c7652009-06-23 09:02:15 +0000103 return loc::MemRegionVal(MemMgr.getCodeTextRegion(sym, T));
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000104 }
105
106 if (Loc::IsLocType(T))
Zhongxing Xue32c7652009-06-23 09:02:15 +0000107 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000108
109 if (T->isIntegerType() && T->isScalarType())
Zhongxing Xue32c7652009-06-23 09:02:15 +0000110 return nonloc::SymbolVal(sym);
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000111
112 return UnknownVal();
113}
114
115SVal ValueManager::getConjuredSymbolVal(const Expr* E, QualType T,
116 unsigned Count) {
117
118 SymbolRef sym = SymMgr.getConjuredSymbol(E, T, Count);
119
Ted Kremenekfa0150c2009-07-18 06:27:01 +0000120 // If T is of function pointer type or a block pointer type, create a
121 // CodeTextRegion wrapping a symbol.
122 if (T->isFunctionPointerType() || T->isBlockPointerType()) {
Zhongxing Xue32c7652009-06-23 09:02:15 +0000123 return loc::MemRegionVal(MemMgr.getCodeTextRegion(sym, T));
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000124 }
125
126 if (Loc::IsLocType(T))
Zhongxing Xue32c7652009-06-23 09:02:15 +0000127 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000128
129 if (T->isIntegerType() && T->isScalarType())
Zhongxing Xue32c7652009-06-23 09:02:15 +0000130 return nonloc::SymbolVal(sym);
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000131
132 return UnknownVal();
133}
134
Ted Kremenek97876752009-07-15 02:27:32 +0000135
136SVal ValueManager::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
137 const TypedRegion *R) {
138 SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, R);
139
140 QualType T = R->getValueType(R->getContext());
141
142 if (Loc::IsLocType(T))
143 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
144
145 if (T->isIntegerType() && T->isScalarType())
146 return nonloc::SymbolVal(sym);
147
148 return UnknownVal();
149}
150
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000151SVal ValueManager::getFunctionPointer(const FunctionDecl* FD) {
152 CodeTextRegion* R
153 = MemMgr.getCodeTextRegion(FD, Context.getPointerType(FD->getType()));
154 return loc::MemRegionVal(R);
155}