blob: 71f09d2dfcda66b23a3f591f962e9ced6ea1d4f8 [file] [log] [blame]
Ted Kremeneka90ccfe2008-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 Xu1c96b242008-10-17 05:57:07 +000010// This file defines SVal, Loc, and NonLoc, classes that represent
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000011// abstract r-values for use with path-sensitive value tracking.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek9e240492008-10-04 05:50:14 +000015#include "clang/Analysis/PathSensitive/GRState.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000016#include "clang/Basic/IdentifierTable.h"
Ted Kremenekd70d0b02008-02-16 01:12:31 +000017#include "llvm/Support/Streams.h"
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000018
19using namespace clang;
20using llvm::dyn_cast;
21using llvm::cast;
22using llvm::APSInt;
23
24//===----------------------------------------------------------------------===//
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000025// Symbol iteration within an SVal.
Ted Kremenek90e14812008-02-14 23:25:54 +000026//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +000027
Ted Kremenek718c4f72008-04-29 22:17:41 +000028
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000029//===----------------------------------------------------------------------===//
30// Utility methods.
31//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +000032
Zhongxing Xu369f4472009-04-20 05:24:46 +000033const FunctionDecl* SVal::getAsFunctionDecl() const {
Zhongxing Xu369f4472009-04-20 05:24:46 +000034 if (const loc::MemRegionVal* X = dyn_cast<loc::MemRegionVal>(this)) {
35 const MemRegion* R = X->getRegion();
Ted Kremenek4abbea62009-04-21 17:37:26 +000036 if (const CodeTextRegion* CTR = R->getAs<CodeTextRegion>()) {
Zhongxing Xu369f4472009-04-20 05:24:46 +000037 if (CTR->isDeclared())
38 return CTR->getDecl();
39 }
40 }
41
42 return 0;
43}
44
Ted Kremenek94c96982009-03-03 22:06:47 +000045/// getAsLocSymbol - If this SVal is a location (subclasses Loc) and
Zhongxing Xu369f4472009-04-20 05:24:46 +000046/// wraps a symbol, return that SymbolRef. Otherwise return 0.
47// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
Ted Kremenek94c96982009-03-03 22:06:47 +000048SymbolRef SVal::getAsLocSymbol() const {
Ted Kremenek94c96982009-03-03 22:06:47 +000049 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this)) {
50 const MemRegion *R = X->getRegion();
51
52 while (R) {
53 // Blast through region views.
54 if (const TypedViewRegion *View = dyn_cast<TypedViewRegion>(R)) {
55 R = View->getSuperRegion();
56 continue;
57 }
58
59 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
60 return SymR->getSymbol();
61
62 break;
63 }
64 }
65
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000066 return 0;
Ted Kremenek94c96982009-03-03 22:06:47 +000067}
68
69/// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
Zhongxing Xu369f4472009-04-20 05:24:46 +000070/// Otherwise return 0.
71// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
Ted Kremenek94c96982009-03-03 22:06:47 +000072SymbolRef SVal::getAsSymbol() const {
73 if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
74 return X->getSymbol();
75
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000076 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
77 if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression()))
78 return Y;
79
Ted Kremenek94c96982009-03-03 22:06:47 +000080 return getAsLocSymbol();
81}
82
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000083/// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
84/// return that expression. Otherwise return NULL.
85const SymExpr *SVal::getAsSymbolicExpression() const {
86 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
87 return X->getSymbolicExpression();
88
89 return getAsSymbol();
90}
91
92bool SVal::symbol_iterator::operator==(const symbol_iterator &X) const {
93 return itr == X.itr;
94}
95
96bool SVal::symbol_iterator::operator!=(const symbol_iterator &X) const {
97 return itr != X.itr;
98}
99
100SVal::symbol_iterator::symbol_iterator(const SymExpr *SE) {
101 itr.push_back(SE);
102 while (!isa<SymbolData>(itr.back())) expand();
103}
104
105SVal::symbol_iterator& SVal::symbol_iterator::operator++() {
106 assert(!itr.empty() && "attempting to iterate on an 'end' iterator");
107 assert(isa<SymbolData>(itr.back()));
108 itr.pop_back();
109 if (!itr.empty())
110 while (!isa<SymbolData>(itr.back())) expand();
111 return *this;
112}
113
114SymbolRef SVal::symbol_iterator::operator*() {
115 assert(!itr.empty() && "attempting to dereference an 'end' iterator");
116 return cast<SymbolData>(itr.back());
117}
118
119void SVal::symbol_iterator::expand() {
120 const SymExpr *SE = itr.back();
121 itr.pop_back();
122
123 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
124 itr.push_back(SIE->getLHS());
125 return;
126 }
127 else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
128 itr.push_back(SSE->getLHS());
129 itr.push_back(SSE->getRHS());
130 return;
131 }
132
133 assert(false && "unhandled expansion case");
134}
135
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000136//===----------------------------------------------------------------------===//
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000137// Other Iterators.
138//===----------------------------------------------------------------------===//
139
140nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
141 return getValue()->begin();
142}
143
144nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
145 return getValue()->end();
146}
147
148//===----------------------------------------------------------------------===//
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000149// Useful predicates.
150//===----------------------------------------------------------------------===//
151
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000152bool SVal::isZeroConstant() const {
153 if (isa<loc::ConcreteInt>(*this))
154 return cast<loc::ConcreteInt>(*this).getValue() == 0;
155 else if (isa<nonloc::ConcreteInt>(*this))
156 return cast<nonloc::ConcreteInt>(*this).getValue() == 0;
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000157 else
158 return false;
159}
160
161
162//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000163// Transfer function dispatch for Non-Locs.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000164//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000165
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000166SVal nonloc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
Ted Kremenek75b0a1c2008-07-18 15:59:33 +0000167 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000168 const nonloc::ConcreteInt& R) const {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000169
Ted Kremenek75b0a1c2008-07-18 15:59:33 +0000170 const llvm::APSInt* X =
171 BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000172
173 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000174 return nonloc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000175 else
176 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000177}
178
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000179 // Bitwise-Complement.
180
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000181nonloc::ConcreteInt
182nonloc::ConcreteInt::EvalComplement(BasicValueFactory& BasicVals) const {
Ted Kremenek240f1f02008-03-07 20:13:31 +0000183 return BasicVals.getValue(~getValue());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000184}
185
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000186 // Unary Minus.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000187
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000188nonloc::ConcreteInt
189nonloc::ConcreteInt::EvalMinus(BasicValueFactory& BasicVals, UnaryOperator* U) const {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000190 assert (U->getType() == U->getSubExpr()->getType());
191 assert (U->getType()->isIntegerType());
Ted Kremenek240f1f02008-03-07 20:13:31 +0000192 return BasicVals.getValue(-getValue());
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000193}
194
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000195//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000196// Transfer function dispatch for Locs.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000197//===----------------------------------------------------------------------===//
198
Ted Kremenekccaad9d2008-10-30 17:53:23 +0000199SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
200 BinaryOperator::Opcode Op,
201 const loc::ConcreteInt& R) const {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000202
203 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
204 (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
205
Ted Kremenek240f1f02008-03-07 20:13:31 +0000206 const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000207
208 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000209 return loc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000210 else
211 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000212}
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000213
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000214//===----------------------------------------------------------------------===//
Ted Kremenekda9ae602009-04-08 18:51:08 +0000215// Utility methods for constructing SVals.
216//===----------------------------------------------------------------------===//
217
Ted Kremenek8e5fb282009-04-09 16:46:55 +0000218SVal ValueManager::makeZeroVal(QualType T) {
Ted Kremenekda9ae602009-04-08 18:51:08 +0000219 if (Loc::IsLocType(T))
220 return Loc::MakeNull(BasicVals);
221
222 if (T->isIntegerType())
223 return NonLoc::MakeVal(BasicVals, 0, T);
224
225 // FIXME: Handle floats.
226 // FIXME: Handle structs.
227 return UnknownVal();
228}
229
230//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000231// Utility methods for constructing Non-Locs.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000232//===----------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000233
Ted Kremenekfc3388d2009-04-10 18:11:44 +0000234NonLoc ValueManager::makeNonLoc(SymbolRef sym) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000235 return nonloc::SymbolVal(sym);
236}
237
Ted Kremenekfc3388d2009-04-10 18:11:44 +0000238NonLoc ValueManager::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
239 const APSInt& v, QualType T) {
Zhongxing Xua129eb92009-03-25 05:58:37 +0000240 // The Environment ensures we always get a persistent APSInt in
241 // BasicValueFactory, so we don't need to get the APSInt from
242 // BasicValueFactory again.
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000243 assert(!Loc::IsLocType(T));
244 return nonloc::SymExprVal(SymMgr.getSymIntExpr(lhs, op, v, T));
Zhongxing Xua129eb92009-03-25 05:58:37 +0000245}
246
Ted Kremenekfc3388d2009-04-10 18:11:44 +0000247NonLoc ValueManager::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
248 const SymExpr *rhs, QualType T) {
Zhongxing Xua129eb92009-03-25 05:58:37 +0000249 assert(SymMgr.getType(lhs) == SymMgr.getType(rhs));
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000250 assert(!Loc::IsLocType(T));
251 return nonloc::SymExprVal(SymMgr.getSymSymExpr(lhs, op, rhs, T));
Zhongxing Xua129eb92009-03-25 05:58:37 +0000252}
253
Ted Kremenek14553ab2009-01-30 00:08:43 +0000254NonLoc NonLoc::MakeIntVal(BasicValueFactory& BasicVals, uint64_t X,
255 bool isUnsigned) {
256 return nonloc::ConcreteInt(BasicVals.getIntValue(X, isUnsigned));
Zhongxing Xu6613d082008-11-24 02:18:56 +0000257}
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000258
Zhongxing Xu8b862732008-11-24 09:38:21 +0000259NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, uint64_t X,
260 unsigned BitWidth, bool isUnsigned) {
261 return nonloc::ConcreteInt(BasicVals.getValue(X, BitWidth, isUnsigned));
262}
263
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000264NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, uint64_t X, QualType T) {
265 return nonloc::ConcreteInt(BasicVals.getValue(X, T));
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000266}
267
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000268NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, IntegerLiteral* I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000269
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000270 return nonloc::ConcreteInt(BasicVals.getValue(APSInt(I->getValue(),
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000271 I->getType()->isUnsignedIntegerType())));
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000272}
273
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000274NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, const llvm::APInt& I,
275 bool isUnsigned) {
276 return nonloc::ConcreteInt(BasicVals.getValue(I, isUnsigned));
277}
278
Zhongxing Xu8b862732008-11-24 09:38:21 +0000279NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, const llvm::APSInt& I) {
280 return nonloc::ConcreteInt(BasicVals.getValue(I));
281}
282
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000283NonLoc NonLoc::MakeIntTruthVal(BasicValueFactory& BasicVals, bool b) {
284 return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000285}
286
Ted Kremenek1670e402009-04-11 00:11:10 +0000287NonLoc ValueManager::makeTruthVal(bool b, QualType T) {
288 return nonloc::ConcreteInt(BasicVals.getTruthValue(b, T));
289}
290
Ted Kremenek632e8b82008-10-30 17:44:46 +0000291NonLoc NonLoc::MakeCompoundVal(QualType T, llvm::ImmutableList<SVal> Vals,
Zhongxing Xu6764b722008-10-30 04:58:00 +0000292 BasicValueFactory& BasicVals) {
Ted Kremenek632e8b82008-10-30 17:44:46 +0000293 return nonloc::CompoundVal(BasicVals.getCompoundValData(T, Vals));
Zhongxing Xu6764b722008-10-30 04:58:00 +0000294}
295
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000296SVal ValueManager::getRValueSymbolVal(const MemRegion* R) {
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000297 SymbolRef sym = SymMgr.getRegionRValueSymbol(R);
298
Ted Kremenekec099f12009-03-18 22:10:22 +0000299 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
300 QualType T = TR->getRValueType(SymMgr.getContext());
Zhongxing Xuec13d922009-04-10 08:45:10 +0000301
302 // If T is of function pointer type, create a CodeTextRegion wrapping a
303 // symbol.
304 if (T->isFunctionPointerType()) {
305 return Loc::MakeVal(MemMgr.getCodeTextRegion(sym, T));
306 }
Ted Kremenekec099f12009-03-18 22:10:22 +0000307
308 if (Loc::IsLocType(T))
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000309 return Loc::MakeVal(MemMgr.getSymbolicRegion(sym));
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000310
Ted Kremenekec099f12009-03-18 22:10:22 +0000311 // Only handle integers for now.
Zhongxing Xu867418f2009-04-09 05:57:11 +0000312 if (T->isIntegerType() && T->isScalarType())
Ted Kremenekfc3388d2009-04-10 18:11:44 +0000313 return makeNonLoc(sym);
Ted Kremenekec099f12009-03-18 22:10:22 +0000314 }
315
316 return UnknownVal();
Zhongxing Xueabf7762008-11-19 11:03:17 +0000317}
318
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000319SVal ValueManager::getConjuredSymbolVal(const Expr* E, unsigned Count) {
Ted Kremenekbb9b2712009-03-20 20:10:45 +0000320 QualType T = E->getType();
Zhongxing Xu867418f2009-04-09 05:57:11 +0000321 SymbolRef sym = SymMgr.getConjuredSymbol(E, Count);
322
Zhongxing Xuec13d922009-04-10 08:45:10 +0000323 // If T is of function pointer type, create a CodeTextRegion wrapping a
324 // symbol.
325 if (T->isFunctionPointerType()) {
326 return Loc::MakeVal(MemMgr.getCodeTextRegion(sym, T));
327 }
328
Zhongxing Xu867418f2009-04-09 05:57:11 +0000329 if (Loc::IsLocType(T))
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000330 return Loc::MakeVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xu867418f2009-04-09 05:57:11 +0000331
332 if (T->isIntegerType() && T->isScalarType())
Ted Kremenekfc3388d2009-04-10 18:11:44 +0000333 return makeNonLoc(sym);
Ted Kremenekbb9b2712009-03-20 20:10:45 +0000334
335 return UnknownVal();
336}
337
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000338SVal ValueManager::getConjuredSymbolVal(const Expr* E, QualType T,
339 unsigned Count) {
340
Zhongxing Xufe1635b2009-04-09 06:30:17 +0000341 SymbolRef sym = SymMgr.getConjuredSymbol(E, T, Count);
342
Zhongxing Xuec13d922009-04-10 08:45:10 +0000343 // If T is of function pointer type, create a CodeTextRegion wrapping a
344 // symbol.
345 if (T->isFunctionPointerType()) {
346 return Loc::MakeVal(MemMgr.getCodeTextRegion(sym, T));
347 }
348
Zhongxing Xufe1635b2009-04-09 06:30:17 +0000349 if (Loc::IsLocType(T))
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000350 return Loc::MakeVal(MemMgr.getSymbolicRegion(sym));
Zhongxing Xufe1635b2009-04-09 06:30:17 +0000351
352 if (T->isIntegerType() && T->isScalarType())
Ted Kremenekfc3388d2009-04-10 18:11:44 +0000353 return makeNonLoc(sym);
Zhongxing Xufe1635b2009-04-09 06:30:17 +0000354
355 return UnknownVal();
356}
357
Zhongxing Xuec13d922009-04-10 08:45:10 +0000358SVal ValueManager::getFunctionPointer(const FunctionDecl* FD) {
359 CodeTextRegion* R
360 = MemMgr.getCodeTextRegion(FD, Context.getPointerType(FD->getType()));
Zhongxing Xu0a095fb2009-04-20 02:27:09 +0000361 return loc::MemRegionVal(R);
Zhongxing Xuec13d922009-04-10 08:45:10 +0000362}
363
Ted Kremenek632e8b82008-10-30 17:44:46 +0000364nonloc::LocAsInteger nonloc::LocAsInteger::Make(BasicValueFactory& Vals, Loc V,
365 unsigned Bits) {
366 return LocAsInteger(Vals.getPersistentSValWithData(V, Bits));
367}
368
Ted Kremenek2a502572008-02-12 21:37:56 +0000369//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000370// Utility methods for constructing Locs.
Ted Kremenek2a502572008-02-12 21:37:56 +0000371//===----------------------------------------------------------------------===//
372
Zhongxing Xu2fdf5552008-12-09 10:51:19 +0000373Loc Loc::MakeVal(const MemRegion* R) { return loc::MemRegionVal(R); }
374
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000375Loc Loc::MakeVal(AddrLabelExpr* E) { return loc::GotoLabel(E->getLabel()); }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000376
Ted Kremenekda9ae602009-04-08 18:51:08 +0000377Loc Loc::MakeNull(BasicValueFactory &BasicVals) {
378 return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
379}
380
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000381//===----------------------------------------------------------------------===//
382// Pretty-Printing.
383//===----------------------------------------------------------------------===//
384
Daniel Dunbar4a77edb2009-03-10 18:00:19 +0000385void SVal::printStdErr() const { print(llvm::errs()); }
Ted Kremenek2a502572008-02-12 21:37:56 +0000386
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000387void SVal::print(std::ostream& Out) const {
Ted Kremenekb8b41612008-10-30 18:35:10 +0000388 llvm::raw_os_ostream out(Out);
389 print(out);
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000390}
391
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000392void SVal::print(llvm::raw_ostream& Out) const {
393
394 switch (getBaseKind()) {
395
396 case UnknownKind:
397 Out << "Invalid"; break;
398
399 case NonLocKind:
400 cast<NonLoc>(this)->print(Out); break;
401
402 case LocKind:
403 cast<Loc>(this)->print(Out); break;
404
405 case UndefinedKind:
406 Out << "Undefined"; break;
407
408 default:
409 assert (false && "Invalid SVal.");
410 }
411}
412
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000413void NonLoc::print(llvm::raw_ostream& Out) const {
414
415 switch (getSubKind()) {
416
417 case nonloc::ConcreteIntKind:
418 Out << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
419
420 if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
421 Out << 'U';
422
423 break;
424
425 case nonloc::SymbolValKind:
426 Out << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
427 break;
428
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000429 case nonloc::SymExprValKind: {
430 const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
431 const SymExpr *SE = C.getSymbolicExpression();
432 Out << SE;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000433 break;
434 }
435
436 case nonloc::LocAsIntegerKind: {
437 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
438 C.getLoc().print(Out);
439 Out << " [as " << C.getNumBits() << " bit integer]";
440 break;
441 }
442
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000443 case nonloc::CompoundValKind: {
444 const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
Ted Kremenekb8b41612008-10-30 18:35:10 +0000445 Out << " {";
446 bool first = true;
447 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
448 if (first) { Out << ' '; first = false; }
449 else Out << ", ";
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000450 (*I).print(Out);
Ted Kremenekb8b41612008-10-30 18:35:10 +0000451 }
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000452 Out << " }";
453 break;
454 }
455
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000456 default:
457 assert (false && "Pretty-printed not implemented for this NonLoc.");
458 break;
459 }
460}
461
462void Loc::print(llvm::raw_ostream& Out) const {
463
464 switch (getSubKind()) {
465
466 case loc::ConcreteIntKind:
467 Out << cast<loc::ConcreteInt>(this)->getValue().getZExtValue()
468 << " (Loc)";
469 break;
470
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000471 case loc::GotoLabelKind:
472 Out << "&&"
473 << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
474 break;
475
476 case loc::MemRegionKind:
477 Out << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
478 break;
479
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000480 default:
481 assert (false && "Pretty-printing not implemented for this Loc.");
482 break;
483 }
484}