blob: 9fe16af6c9ffc84c461169a9ecf7080ad32d6415 [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);
Mike Stump25cf7602009-09-09 15:08:12 +000031
Zhongxing Xua3a5a282009-06-23 06:22:22 +000032 // FIXME: Handle floats.
33 // FIXME: Handle structs.
Mike Stump25cf7602009-09-09 15:08:12 +000034 return UnknownVal();
Zhongxing Xua3a5a282009-06-23 06:22:22 +000035}
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;
Mike Stump25cf7602009-09-09 15:08:12 +000061
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 }
Mike Stump25cf7602009-09-09 15:08:12 +000068
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) {
Zhongxing Xua3a5a282009-06-23 06:22:22 +000073
Ted Kremenek98137f72009-08-01 06:17:29 +000074 if (T.isNull()) {
75 const TypedRegion* TR = cast<TypedRegion>(R);
76 T = TR->getValueType(SymMgr.getContext());
Zhongxing Xua3a5a282009-06-23 06:22:22 +000077 }
Mike Stump25cf7602009-09-09 15:08:12 +000078
Ted Kremenek98137f72009-08-01 06:17:29 +000079 if (!SymbolManager::canSymbolicate(T))
80 return UnknownVal();
Zhongxing Xua3a5a282009-06-23 06:22:22 +000081
Ted Kremenek98137f72009-08-01 06:17:29 +000082 SymbolRef sym = SymMgr.getRegionValueSymbol(R, T);
Mike Stump25cf7602009-09-09 15:08:12 +000083
Ted Kremenek98137f72009-08-01 06:17:29 +000084 if (Loc::IsLocType(T))
85 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Mike Stump25cf7602009-09-09 15:08:12 +000086
Ted Kremenek98137f72009-08-01 06:17:29 +000087 return nonloc::SymbolVal(sym);
Zhongxing Xua3a5a282009-06-23 06:22:22 +000088}
89
Ted Kremenekf772e8b2009-08-28 04:49:15 +000090SVal ValueManager::getConjuredSymbolVal(const Expr *E, unsigned Count) {
Zhongxing Xua3a5a282009-06-23 06:22:22 +000091 QualType T = E->getType();
Mike Stump25cf7602009-09-09 15:08:12 +000092
Ted Kremenek98137f72009-08-01 06:17:29 +000093 if (!SymbolManager::canSymbolicate(T))
94 return UnknownVal();
Mike Stump25cf7602009-09-09 15:08:12 +000095
Zhongxing Xua3a5a282009-06-23 06:22:22 +000096 SymbolRef sym = SymMgr.getConjuredSymbol(E, Count);
97
Zhongxing Xua3a5a282009-06-23 06:22:22 +000098 if (Loc::IsLocType(T))
Zhongxing Xue32c7652009-06-23 09:02:15 +000099 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000100
Ted Kremenek98137f72009-08-01 06:17:29 +0000101 return nonloc::SymbolVal(sym);
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000102}
103
Ted Kremenekf772e8b2009-08-28 04:49:15 +0000104SVal ValueManager::getConjuredSymbolVal(const Expr *E, QualType T,
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000105 unsigned Count) {
Mike Stump25cf7602009-09-09 15:08:12 +0000106
Ted Kremenek98137f72009-08-01 06:17:29 +0000107 if (!SymbolManager::canSymbolicate(T))
108 return UnknownVal();
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000109
110 SymbolRef sym = SymMgr.getConjuredSymbol(E, T, Count);
111
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000112 if (Loc::IsLocType(T))
Zhongxing Xue32c7652009-06-23 09:02:15 +0000113 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000114
Ted Kremenek98137f72009-08-01 06:17:29 +0000115 return nonloc::SymbolVal(sym);
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000116}
117
Ted Kremenek97876752009-07-15 02:27:32 +0000118
119SVal ValueManager::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
120 const TypedRegion *R) {
Ted Kremenek97876752009-07-15 02:27:32 +0000121 QualType T = R->getValueType(R->getContext());
Ted Kremenek98137f72009-08-01 06:17:29 +0000122
123 if (!SymbolManager::canSymbolicate(T))
124 return UnknownVal();
Mike Stump25cf7602009-09-09 15:08:12 +0000125
Ted Kremenek98137f72009-08-01 06:17:29 +0000126 SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, R);
Mike Stump25cf7602009-09-09 15:08:12 +0000127
Ted Kremenek97876752009-07-15 02:27:32 +0000128 if (Loc::IsLocType(T))
129 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
Mike Stump25cf7602009-09-09 15:08:12 +0000130
Ted Kremenek98137f72009-08-01 06:17:29 +0000131 return nonloc::SymbolVal(sym);
Ted Kremenek97876752009-07-15 02:27:32 +0000132}
133
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000134SVal ValueManager::getFunctionPointer(const FunctionDecl* FD) {
Ted Kremenekf772e8b2009-08-28 04:49:15 +0000135 CodeTextRegion *R = MemMgr.getCodeTextRegion(FD);
Zhongxing Xua3a5a282009-06-23 06:22:22 +0000136 return loc::MemRegionVal(R);
137}