blob: 9225f994d3f0f0412b8bf698938d1a5767a32e3b [file] [log] [blame]
Ted Kremenek72197902008-01-31 19:34:24 +00001//= RValues.cpp - Abstract RValues for Path-Sens. Value Tracking -*- 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//
Zhongxing Xu097fc982008-10-17 05:57:07 +000010// This file defines SVal, Loc, and NonLoc, classes that represent
Ted Kremenek72197902008-01-31 19:34:24 +000011// abstract r-values for use with path-sensitive value tracking.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekb15eba42008-10-04 05:50:14 +000015#include "clang/Analysis/PathSensitive/GRState.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000016#include "clang/Basic/IdentifierTable.h"
Ted Kremenek94e915e2008-02-16 01:12:31 +000017#include "llvm/Support/Streams.h"
Ted Kremenek72197902008-01-31 19:34:24 +000018
19using namespace clang;
20using llvm::dyn_cast;
21using llvm::cast;
22using llvm::APSInt;
23
24//===----------------------------------------------------------------------===//
Ted Kremenek74556a12009-03-26 03:35:11 +000025// Symbol iteration within an SVal.
Ted Kremenek0e39dcf2008-02-14 23:25:54 +000026//===----------------------------------------------------------------------===//
Ted Kremenek6e24a802008-02-01 06:36:40 +000027
Ted Kremenek465f25a2008-04-29 22:17:41 +000028
Ted Kremenek74556a12009-03-26 03:35:11 +000029//===----------------------------------------------------------------------===//
30// Utility methods.
31//===----------------------------------------------------------------------===//
Ted Kremenek6e24a802008-02-01 06:36:40 +000032
Ted Kremenek9577c1e2009-03-03 22:06:47 +000033/// getAsLocSymbol - If this SVal is a location (subclasses Loc) and
34/// wraps a symbol, return that SymbolRef. Otherwise return a SymbolRef
35/// where 'isValid()' returns false.
36SymbolRef SVal::getAsLocSymbol() const {
37 if (const loc::SymbolVal *X = dyn_cast<loc::SymbolVal>(this))
38 return X->getSymbol();
39
40 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this)) {
41 const MemRegion *R = X->getRegion();
42
43 while (R) {
44 // Blast through region views.
45 if (const TypedViewRegion *View = dyn_cast<TypedViewRegion>(R)) {
46 R = View->getSuperRegion();
47 continue;
48 }
49
50 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
51 return SymR->getSymbol();
52
53 break;
54 }
55 }
56
Ted Kremenek74556a12009-03-26 03:35:11 +000057 return 0;
Ted Kremenek9577c1e2009-03-03 22:06:47 +000058}
59
60/// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
61/// Otherwise return a SymbolRef where 'isValid()' returns false.
62SymbolRef SVal::getAsSymbol() const {
63 if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
64 return X->getSymbol();
65
Ted Kremenek74556a12009-03-26 03:35:11 +000066 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
67 if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression()))
68 return Y;
69
Ted Kremenek9577c1e2009-03-03 22:06:47 +000070 return getAsLocSymbol();
71}
72
Ted Kremenek74556a12009-03-26 03:35:11 +000073/// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
74/// return that expression. Otherwise return NULL.
75const SymExpr *SVal::getAsSymbolicExpression() const {
76 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
77 return X->getSymbolicExpression();
78
79 return getAsSymbol();
80}
81
82bool SVal::symbol_iterator::operator==(const symbol_iterator &X) const {
83 return itr == X.itr;
84}
85
86bool SVal::symbol_iterator::operator!=(const symbol_iterator &X) const {
87 return itr != X.itr;
88}
89
90SVal::symbol_iterator::symbol_iterator(const SymExpr *SE) {
91 itr.push_back(SE);
92 while (!isa<SymbolData>(itr.back())) expand();
93}
94
95SVal::symbol_iterator& SVal::symbol_iterator::operator++() {
96 assert(!itr.empty() && "attempting to iterate on an 'end' iterator");
97 assert(isa<SymbolData>(itr.back()));
98 itr.pop_back();
99 if (!itr.empty())
100 while (!isa<SymbolData>(itr.back())) expand();
101 return *this;
102}
103
104SymbolRef SVal::symbol_iterator::operator*() {
105 assert(!itr.empty() && "attempting to dereference an 'end' iterator");
106 return cast<SymbolData>(itr.back());
107}
108
109void SVal::symbol_iterator::expand() {
110 const SymExpr *SE = itr.back();
111 itr.pop_back();
112
113 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
114 itr.push_back(SIE->getLHS());
115 return;
116 }
117 else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
118 itr.push_back(SSE->getLHS());
119 itr.push_back(SSE->getRHS());
120 return;
121 }
122
123 assert(false && "unhandled expansion case");
124}
125
Ted Kremenek15cb0782008-02-06 22:50:25 +0000126//===----------------------------------------------------------------------===//
Ted Kremenekb667c9f2008-10-30 18:01:28 +0000127// Other Iterators.
128//===----------------------------------------------------------------------===//
129
130nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
131 return getValue()->begin();
132}
133
134nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
135 return getValue()->end();
136}
137
138//===----------------------------------------------------------------------===//
Ted Kremenek99b4f432008-07-18 15:54:51 +0000139// Useful predicates.
140//===----------------------------------------------------------------------===//
141
Zhongxing Xu097fc982008-10-17 05:57:07 +0000142bool SVal::isZeroConstant() const {
143 if (isa<loc::ConcreteInt>(*this))
144 return cast<loc::ConcreteInt>(*this).getValue() == 0;
145 else if (isa<nonloc::ConcreteInt>(*this))
146 return cast<nonloc::ConcreteInt>(*this).getValue() == 0;
Ted Kremenek99b4f432008-07-18 15:54:51 +0000147 else
148 return false;
149}
150
151
152//===----------------------------------------------------------------------===//
Zhongxing Xu097fc982008-10-17 05:57:07 +0000153// Transfer function dispatch for Non-Locs.
Ted Kremenek15cb0782008-02-06 22:50:25 +0000154//===----------------------------------------------------------------------===//
Ted Kremenek6e24a802008-02-01 06:36:40 +0000155
Zhongxing Xu097fc982008-10-17 05:57:07 +0000156SVal nonloc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
Ted Kremenek43a281c2008-07-18 15:59:33 +0000157 BinaryOperator::Opcode Op,
Zhongxing Xu097fc982008-10-17 05:57:07 +0000158 const nonloc::ConcreteInt& R) const {
Ted Kremenek07baa252008-02-21 18:02:17 +0000159
Ted Kremenek43a281c2008-07-18 15:59:33 +0000160 const llvm::APSInt* X =
161 BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenekc2d07202008-02-28 20:32:03 +0000162
163 if (X)
Zhongxing Xu097fc982008-10-17 05:57:07 +0000164 return nonloc::ConcreteInt(*X);
Ted Kremenekc2d07202008-02-28 20:32:03 +0000165 else
166 return UndefinedVal();
Ted Kremenek15cb0782008-02-06 22:50:25 +0000167}
168
Ted Kremenek15cb0782008-02-06 22:50:25 +0000169 // Bitwise-Complement.
170
Zhongxing Xu097fc982008-10-17 05:57:07 +0000171nonloc::ConcreteInt
172nonloc::ConcreteInt::EvalComplement(BasicValueFactory& BasicVals) const {
Ted Kremenek8ad19872008-03-07 20:13:31 +0000173 return BasicVals.getValue(~getValue());
Ted Kremenek15cb0782008-02-06 22:50:25 +0000174}
175
Ted Kremenek15cb0782008-02-06 22:50:25 +0000176 // Unary Minus.
Ted Kremenek72197902008-01-31 19:34:24 +0000177
Zhongxing Xu097fc982008-10-17 05:57:07 +0000178nonloc::ConcreteInt
179nonloc::ConcreteInt::EvalMinus(BasicValueFactory& BasicVals, UnaryOperator* U) const {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000180 assert (U->getType() == U->getSubExpr()->getType());
181 assert (U->getType()->isIntegerType());
Ted Kremenek8ad19872008-03-07 20:13:31 +0000182 return BasicVals.getValue(-getValue());
Ted Kremenek2cb46642008-02-04 16:58:30 +0000183}
184
Ted Kremenek72197902008-01-31 19:34:24 +0000185//===----------------------------------------------------------------------===//
Zhongxing Xu097fc982008-10-17 05:57:07 +0000186// Transfer function dispatch for Locs.
Ted Kremenek72197902008-01-31 19:34:24 +0000187//===----------------------------------------------------------------------===//
188
Ted Kremenekda3e12d2008-10-30 17:53:23 +0000189SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
190 BinaryOperator::Opcode Op,
191 const loc::ConcreteInt& R) const {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000192
193 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
194 (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
195
Ted Kremenek8ad19872008-03-07 20:13:31 +0000196 const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenekc2d07202008-02-28 20:32:03 +0000197
198 if (X)
Zhongxing Xu097fc982008-10-17 05:57:07 +0000199 return loc::ConcreteInt(*X);
Ted Kremenekc2d07202008-02-28 20:32:03 +0000200 else
201 return UndefinedVal();
Ted Kremenek15cb0782008-02-06 22:50:25 +0000202}
Ted Kremenek72197902008-01-31 19:34:24 +0000203
Ted Kremenek74556a12009-03-26 03:35:11 +0000204NonLoc Loc::EQ(SymbolManager& SymMgr, const Loc& R) const {
Ted Kremenek07baa252008-02-21 18:02:17 +0000205
Ted Kremenek72197902008-01-31 19:34:24 +0000206 switch (getSubKind()) {
207 default:
Zhongxing Xu097fc982008-10-17 05:57:07 +0000208 assert(false && "EQ not implemented for this Loc.");
Ted Kremenek07baa252008-02-21 18:02:17 +0000209 break;
Ted Kremenek15cb0782008-02-06 22:50:25 +0000210
Zhongxing Xu097fc982008-10-17 05:57:07 +0000211 case loc::ConcreteIntKind:
212 if (isa<loc::ConcreteInt>(R)) {
213 bool b = cast<loc::ConcreteInt>(this)->getValue() ==
214 cast<loc::ConcreteInt>(R).getValue();
Ted Kremenek15cb0782008-02-06 22:50:25 +0000215
Ted Kremenek74556a12009-03-26 03:35:11 +0000216 return NonLoc::MakeIntTruthVal(SymMgr.getBasicVals(), b);
Ted Kremenek81eacb22008-02-05 23:08:41 +0000217 }
Zhongxing Xu097fc982008-10-17 05:57:07 +0000218 else if (isa<loc::SymbolVal>(R)) {
Ted Kremenek74556a12009-03-26 03:35:11 +0000219 const SymIntExpr *SE =
220 SymMgr.getSymIntExpr(cast<loc::SymbolVal>(R).getSymbol(),
Ted Kremenek07baa252008-02-21 18:02:17 +0000221 BinaryOperator::EQ,
Ted Kremenek74556a12009-03-26 03:35:11 +0000222 cast<loc::ConcreteInt>(this)->getValue(),
223 SymMgr.getContext().IntTy);
Ted Kremenek81eacb22008-02-05 23:08:41 +0000224
Ted Kremenek74556a12009-03-26 03:35:11 +0000225 return nonloc::SymExprVal(SE);
Ted Kremenek81eacb22008-02-05 23:08:41 +0000226 }
227
228 break;
229
Zhongxing Xu097fc982008-10-17 05:57:07 +0000230 case loc::SymbolValKind: {
231 if (isa<loc::ConcreteInt>(R)) {
Ted Kremenek74556a12009-03-26 03:35:11 +0000232 const SymIntExpr *SE =
233 SymMgr.getSymIntExpr(cast<loc::SymbolVal>(this)->getSymbol(),
Ted Kremenek07baa252008-02-21 18:02:17 +0000234 BinaryOperator::EQ,
Ted Kremenek74556a12009-03-26 03:35:11 +0000235 cast<loc::ConcreteInt>(R).getValue(),
236 SymMgr.getContext().IntTy);
Ted Kremenek15cb0782008-02-06 22:50:25 +0000237
Ted Kremenek74556a12009-03-26 03:35:11 +0000238 return nonloc::SymExprVal(SE);
Ted Kremenek15cb0782008-02-06 22:50:25 +0000239 }
Ted Kremenek74556a12009-03-26 03:35:11 +0000240
241 assert (!isa<loc::SymbolVal>(R) && "FIXME: Implement unification.");
Ted Kremenek15cb0782008-02-06 22:50:25 +0000242 break;
Ted Kremenek81eacb22008-02-05 23:08:41 +0000243 }
Ted Kremenek81eacb22008-02-05 23:08:41 +0000244
Zhongxing Xu097fc982008-10-17 05:57:07 +0000245 case loc::MemRegionKind:
246 if (isa<loc::MemRegionVal>(R)) {
247 bool b = cast<loc::MemRegionVal>(*this) == cast<loc::MemRegionVal>(R);
Ted Kremenek74556a12009-03-26 03:35:11 +0000248 return NonLoc::MakeIntTruthVal(SymMgr.getBasicVals(), b);
Ted Kremenek81eacb22008-02-05 23:08:41 +0000249 }
250
251 break;
Ted Kremenek72197902008-01-31 19:34:24 +0000252 }
Ted Kremenek81eacb22008-02-05 23:08:41 +0000253
Ted Kremenek74556a12009-03-26 03:35:11 +0000254 return NonLoc::MakeIntTruthVal(SymMgr.getBasicVals(), false);
Ted Kremenek72197902008-01-31 19:34:24 +0000255}
256
Ted Kremenek74556a12009-03-26 03:35:11 +0000257NonLoc Loc::NE(SymbolManager& SymMgr, const Loc& R) const {
Ted Kremenek72197902008-01-31 19:34:24 +0000258 switch (getSubKind()) {
259 default:
Zhongxing Xu097fc982008-10-17 05:57:07 +0000260 assert(false && "NE not implemented for this Loc.");
Ted Kremenek07baa252008-02-21 18:02:17 +0000261 break;
Ted Kremenek72197902008-01-31 19:34:24 +0000262
Zhongxing Xu097fc982008-10-17 05:57:07 +0000263 case loc::ConcreteIntKind:
264 if (isa<loc::ConcreteInt>(R)) {
265 bool b = cast<loc::ConcreteInt>(this)->getValue() !=
266 cast<loc::ConcreteInt>(R).getValue();
Ted Kremenek81eacb22008-02-05 23:08:41 +0000267
Ted Kremenek74556a12009-03-26 03:35:11 +0000268 return NonLoc::MakeIntTruthVal(SymMgr.getBasicVals(), b);
Ted Kremenek81eacb22008-02-05 23:08:41 +0000269 }
Zhongxing Xu097fc982008-10-17 05:57:07 +0000270 else if (isa<loc::SymbolVal>(R)) {
Ted Kremenek74556a12009-03-26 03:35:11 +0000271 const SymIntExpr *SE =
272 SymMgr.getSymIntExpr(cast<loc::SymbolVal>(R).getSymbol(),
273 BinaryOperator::NE,
274 cast<loc::ConcreteInt>(this)->getValue(),
275 SymMgr.getContext().IntTy);
276 return nonloc::SymExprVal(SE);
Ted Kremenek81eacb22008-02-05 23:08:41 +0000277 }
Ted Kremenek81eacb22008-02-05 23:08:41 +0000278 break;
Ted Kremenek6e24a802008-02-01 06:36:40 +0000279
Zhongxing Xu097fc982008-10-17 05:57:07 +0000280 case loc::SymbolValKind: {
281 if (isa<loc::ConcreteInt>(R)) {
Ted Kremenek74556a12009-03-26 03:35:11 +0000282 const SymIntExpr *SE =
283 SymMgr.getSymIntExpr(cast<loc::SymbolVal>(this)->getSymbol(),
284 BinaryOperator::NE,
285 cast<loc::ConcreteInt>(R).getValue(),
286 SymMgr.getContext().IntTy);
Ted Kremenek81eacb22008-02-05 23:08:41 +0000287
Ted Kremenek74556a12009-03-26 03:35:11 +0000288 return nonloc::SymExprVal(SE);
Ted Kremenek81eacb22008-02-05 23:08:41 +0000289 }
290
Zhongxing Xu097fc982008-10-17 05:57:07 +0000291 assert (!isa<loc::SymbolVal>(R) && "FIXME: Implement sym !=.");
Ted Kremenek81eacb22008-02-05 23:08:41 +0000292 break;
293 }
294
Zhongxing Xu097fc982008-10-17 05:57:07 +0000295 case loc::MemRegionKind:
296 if (isa<loc::MemRegionVal>(R)) {
297 bool b = cast<loc::MemRegionVal>(*this)==cast<loc::MemRegionVal>(R);
Ted Kremenek74556a12009-03-26 03:35:11 +0000298 return NonLoc::MakeIntTruthVal(SymMgr.getBasicVals(), b);
Ted Kremenek07baa252008-02-21 18:02:17 +0000299 }
Ted Kremenek81eacb22008-02-05 23:08:41 +0000300
Ted Kremenek07baa252008-02-21 18:02:17 +0000301 break;
Ted Kremenek72197902008-01-31 19:34:24 +0000302 }
Ted Kremenek81eacb22008-02-05 23:08:41 +0000303
Ted Kremenek74556a12009-03-26 03:35:11 +0000304 return NonLoc::MakeIntTruthVal(SymMgr.getBasicVals(), true);
Ted Kremenek72197902008-01-31 19:34:24 +0000305}
306
Ted Kremenek72197902008-01-31 19:34:24 +0000307//===----------------------------------------------------------------------===//
Zhongxing Xu097fc982008-10-17 05:57:07 +0000308// Utility methods for constructing Non-Locs.
Ted Kremenek72197902008-01-31 19:34:24 +0000309//===----------------------------------------------------------------------===//
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000310
311NonLoc NonLoc::MakeVal(SymbolRef sym) {
312 return nonloc::SymbolVal(sym);
313}
314
Ted Kremenek74556a12009-03-26 03:35:11 +0000315NonLoc NonLoc::MakeVal(SymbolManager& SymMgr, const SymExpr *lhs,
316 BinaryOperator::Opcode op, const APSInt& v, QualType T) {
Zhongxing Xu02bb0342009-03-25 05:58:37 +0000317 // The Environment ensures we always get a persistent APSInt in
318 // BasicValueFactory, so we don't need to get the APSInt from
319 // BasicValueFactory again.
Ted Kremenek74556a12009-03-26 03:35:11 +0000320 assert(!Loc::IsLocType(T));
321 return nonloc::SymExprVal(SymMgr.getSymIntExpr(lhs, op, v, T));
Zhongxing Xu02bb0342009-03-25 05:58:37 +0000322}
323
Ted Kremenek74556a12009-03-26 03:35:11 +0000324NonLoc NonLoc::MakeVal(SymbolManager& SymMgr, const SymExpr *lhs,
325 BinaryOperator::Opcode op, const SymExpr *rhs,
326QualType T) {
Zhongxing Xu02bb0342009-03-25 05:58:37 +0000327 assert(SymMgr.getType(lhs) == SymMgr.getType(rhs));
Ted Kremenek74556a12009-03-26 03:35:11 +0000328 assert(!Loc::IsLocType(T));
329 return nonloc::SymExprVal(SymMgr.getSymSymExpr(lhs, op, rhs, T));
Zhongxing Xu02bb0342009-03-25 05:58:37 +0000330}
331
Ted Kremeneka25cb052009-01-30 00:08:43 +0000332NonLoc NonLoc::MakeIntVal(BasicValueFactory& BasicVals, uint64_t X,
333 bool isUnsigned) {
334 return nonloc::ConcreteInt(BasicVals.getIntValue(X, isUnsigned));
Zhongxing Xu83cae2c2008-11-24 02:18:56 +0000335}
Ted Kremenek72197902008-01-31 19:34:24 +0000336
Zhongxing Xu0df42642008-11-24 09:38:21 +0000337NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, uint64_t X,
338 unsigned BitWidth, bool isUnsigned) {
339 return nonloc::ConcreteInt(BasicVals.getValue(X, BitWidth, isUnsigned));
340}
341
Zhongxing Xu097fc982008-10-17 05:57:07 +0000342NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, uint64_t X, QualType T) {
343 return nonloc::ConcreteInt(BasicVals.getValue(X, T));
Ted Kremenek72197902008-01-31 19:34:24 +0000344}
345
Zhongxing Xu097fc982008-10-17 05:57:07 +0000346NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, IntegerLiteral* I) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000347
Zhongxing Xu097fc982008-10-17 05:57:07 +0000348 return nonloc::ConcreteInt(BasicVals.getValue(APSInt(I->getValue(),
Ted Kremenek07baa252008-02-21 18:02:17 +0000349 I->getType()->isUnsignedIntegerType())));
Ted Kremenek72197902008-01-31 19:34:24 +0000350}
351
Zhongxing Xud52b8cf2008-11-22 13:21:46 +0000352NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, const llvm::APInt& I,
353 bool isUnsigned) {
354 return nonloc::ConcreteInt(BasicVals.getValue(I, isUnsigned));
355}
356
Zhongxing Xu0df42642008-11-24 09:38:21 +0000357NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, const llvm::APSInt& I) {
358 return nonloc::ConcreteInt(BasicVals.getValue(I));
359}
360
Zhongxing Xu097fc982008-10-17 05:57:07 +0000361NonLoc NonLoc::MakeIntTruthVal(BasicValueFactory& BasicVals, bool b) {
362 return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
Ted Kremenek15cb0782008-02-06 22:50:25 +0000363}
364
Ted Kremenek5e7a4b82008-10-30 17:44:46 +0000365NonLoc NonLoc::MakeCompoundVal(QualType T, llvm::ImmutableList<SVal> Vals,
Zhongxing Xu277884f2008-10-30 04:58:00 +0000366 BasicValueFactory& BasicVals) {
Ted Kremenek5e7a4b82008-10-30 17:44:46 +0000367 return nonloc::CompoundVal(BasicVals.getCompoundValData(T, Vals));
Zhongxing Xu277884f2008-10-30 04:58:00 +0000368}
369
Ted Kremenek65865a02009-01-22 18:23:34 +0000370SVal SVal::GetRValueSymbolVal(SymbolManager& SymMgr, const MemRegion* R) {
371 SymbolRef sym = SymMgr.getRegionRValueSymbol(R);
372
Ted Kremenek3bd1e482009-03-18 22:10:22 +0000373 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
374 QualType T = TR->getRValueType(SymMgr.getContext());
375
376 if (Loc::IsLocType(T))
Ted Kremenek65865a02009-01-22 18:23:34 +0000377 return Loc::MakeVal(sym);
Ted Kremenek72197902008-01-31 19:34:24 +0000378
Ted Kremenek3bd1e482009-03-18 22:10:22 +0000379 // Only handle integers for now.
380 if (T->isIntegerType())
381 return NonLoc::MakeVal(sym);
382 }
383
384 return UnknownVal();
Zhongxing Xuaede15a2008-11-19 11:03:17 +0000385}
386
Ted Kremenek607415e2009-03-20 20:10:45 +0000387SVal SVal::GetConjuredSymbolVal(SymbolManager &SymMgr, const Expr* E,
388 unsigned Count) {
389
390 QualType T = E->getType();
391
392 if (Loc::IsLocType(T)) {
393 SymbolRef Sym = SymMgr.getConjuredSymbol(E, Count);
394 return loc::SymbolVal(Sym);
395 }
396 else if (T->isIntegerType() && T->isScalarType()) {
397 SymbolRef Sym = SymMgr.getConjuredSymbol(E, Count);
398 return nonloc::SymbolVal(Sym);
399 }
400
401 return UnknownVal();
402}
403
Ted Kremenek5e7a4b82008-10-30 17:44:46 +0000404nonloc::LocAsInteger nonloc::LocAsInteger::Make(BasicValueFactory& Vals, Loc V,
405 unsigned Bits) {
406 return LocAsInteger(Vals.getPersistentSValWithData(V, Bits));
407}
408
Ted Kremenekad884682008-02-12 21:37:56 +0000409//===----------------------------------------------------------------------===//
Zhongxing Xu097fc982008-10-17 05:57:07 +0000410// Utility methods for constructing Locs.
Ted Kremenekad884682008-02-12 21:37:56 +0000411//===----------------------------------------------------------------------===//
412
Zhongxing Xub50a09c2008-12-09 10:51:19 +0000413Loc Loc::MakeVal(const MemRegion* R) { return loc::MemRegionVal(R); }
414
Zhongxing Xu097fc982008-10-17 05:57:07 +0000415Loc Loc::MakeVal(AddrLabelExpr* E) { return loc::GotoLabel(E->getLabel()); }
Ted Kremenek15cb0782008-02-06 22:50:25 +0000416
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000417Loc Loc::MakeVal(SymbolRef sym) { return loc::SymbolVal(sym); }
418
Ted Kremenek72197902008-01-31 19:34:24 +0000419//===----------------------------------------------------------------------===//
420// Pretty-Printing.
421//===----------------------------------------------------------------------===//
422
Daniel Dunbar24c169d2009-03-10 18:00:19 +0000423void SVal::printStdErr() const { print(llvm::errs()); }
Ted Kremenekad884682008-02-12 21:37:56 +0000424
Zhongxing Xu097fc982008-10-17 05:57:07 +0000425void SVal::print(std::ostream& Out) const {
Ted Kremenekc1c2bbf2008-10-30 18:35:10 +0000426 llvm::raw_os_ostream out(Out);
427 print(out);
Ted Kremenek72197902008-01-31 19:34:24 +0000428}
429
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000430void SVal::print(llvm::raw_ostream& Out) const {
431
432 switch (getBaseKind()) {
433
434 case UnknownKind:
435 Out << "Invalid"; break;
436
437 case NonLocKind:
438 cast<NonLoc>(this)->print(Out); break;
439
440 case LocKind:
441 cast<Loc>(this)->print(Out); break;
442
443 case UndefinedKind:
444 Out << "Undefined"; break;
445
446 default:
447 assert (false && "Invalid SVal.");
448 }
449}
450
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000451void NonLoc::print(llvm::raw_ostream& Out) const {
452
453 switch (getSubKind()) {
454
455 case nonloc::ConcreteIntKind:
456 Out << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
457
458 if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
459 Out << 'U';
460
461 break;
462
463 case nonloc::SymbolValKind:
464 Out << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
465 break;
466
Ted Kremenek74556a12009-03-26 03:35:11 +0000467 case nonloc::SymExprValKind: {
468 const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
469 const SymExpr *SE = C.getSymbolicExpression();
470 Out << SE;
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000471 break;
472 }
473
474 case nonloc::LocAsIntegerKind: {
475 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
476 C.getLoc().print(Out);
477 Out << " [as " << C.getNumBits() << " bit integer]";
478 break;
479 }
480
Ted Kremenekb667c9f2008-10-30 18:01:28 +0000481 case nonloc::CompoundValKind: {
482 const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
Ted Kremenekc1c2bbf2008-10-30 18:35:10 +0000483 Out << " {";
484 bool first = true;
485 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
486 if (first) { Out << ' '; first = false; }
487 else Out << ", ";
Ted Kremenekb667c9f2008-10-30 18:01:28 +0000488 (*I).print(Out);
Ted Kremenekc1c2bbf2008-10-30 18:35:10 +0000489 }
Ted Kremenekb667c9f2008-10-30 18:01:28 +0000490 Out << " }";
491 break;
492 }
493
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000494 default:
495 assert (false && "Pretty-printed not implemented for this NonLoc.");
496 break;
497 }
498}
499
500void Loc::print(llvm::raw_ostream& Out) const {
501
502 switch (getSubKind()) {
503
504 case loc::ConcreteIntKind:
505 Out << cast<loc::ConcreteInt>(this)->getValue().getZExtValue()
506 << " (Loc)";
507 break;
508
509 case loc::SymbolValKind:
510 Out << '$' << cast<loc::SymbolVal>(this)->getSymbol();
511 break;
512
513 case loc::GotoLabelKind:
514 Out << "&&"
515 << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
516 break;
517
518 case loc::MemRegionKind:
519 Out << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
520 break;
521
522 case loc::FuncValKind:
523 Out << "function "
524 << cast<loc::FuncVal>(this)->getDecl()->getIdentifier()->getName();
525 break;
526
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000527 default:
528 assert (false && "Pretty-printing not implemented for this Loc.");
529 break;
530 }
531}