blob: bba0bde12ab7c6346e402a8ad13a1ce99f2bc8f1 [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 Kremenek90e14812008-02-14 23:25:54 +000025// Symbol Iteration.
26//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +000027
Zhongxing Xu1c96b242008-10-17 05:57:07 +000028SVal::symbol_iterator SVal::symbol_begin() const {
Ted Kremenek718c4f72008-04-29 22:17:41 +000029 // FIXME: This is a rat's nest. Cleanup.
30
Zhongxing Xu1c96b242008-10-17 05:57:07 +000031 if (isa<loc::SymbolVal>(this))
Ted Kremenek0d958e72008-10-27 23:39:39 +000032 return symbol_iterator(SymbolID((uintptr_t)Data));
Zhongxing Xu1c96b242008-10-17 05:57:07 +000033 else if (isa<nonloc::SymbolVal>(this))
Ted Kremenek0d958e72008-10-27 23:39:39 +000034 return symbol_iterator(SymbolID((uintptr_t)Data));
Zhongxing Xu1c96b242008-10-17 05:57:07 +000035 else if (isa<nonloc::SymIntConstraintVal>(this)) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000036 const SymIntConstraint& C =
Ted Kremenek0d958e72008-10-27 23:39:39 +000037 cast<nonloc::SymIntConstraintVal>(this)->getConstraint();
38 return symbol_iterator(C.getSymbol());
Ted Kremenek90e14812008-02-14 23:25:54 +000039 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +000040 else if (isa<nonloc::LocAsInteger>(this)) {
41 const nonloc::LocAsInteger& V = cast<nonloc::LocAsInteger>(*this);
42 return V.getPersistentLoc().symbol_begin();
Ted Kremenek0fe33bc2008-04-22 21:10:18 +000043 }
Ted Kremenek0d958e72008-10-27 23:39:39 +000044 else if (isa<loc::MemRegionVal>(this)) {
45 const MemRegion* R = cast<loc::MemRegionVal>(this)->getRegion();
46 if (const SymbolicRegion* S = dyn_cast<SymbolicRegion>(R))
47 return symbol_iterator(S->getSymbol());
48 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000049
Ted Kremenek0d958e72008-10-27 23:39:39 +000050 return symbol_iterator();
Ted Kremenek90e14812008-02-14 23:25:54 +000051}
52
Zhongxing Xu1c96b242008-10-17 05:57:07 +000053SVal::symbol_iterator SVal::symbol_end() const {
Ted Kremenek0d958e72008-10-27 23:39:39 +000054 return symbol_iterator();
Ted Kremenek90e14812008-02-14 23:25:54 +000055}
Ted Kremeneka6e4d212008-02-01 06:36:40 +000056
Ted Kremenekcf78b6a2008-02-06 22:50:25 +000057//===----------------------------------------------------------------------===//
Ted Kremeneka6fac4e2008-10-30 18:01:28 +000058// Other Iterators.
59//===----------------------------------------------------------------------===//
60
61nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
62 return getValue()->begin();
63}
64
65nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
66 return getValue()->end();
67}
68
69//===----------------------------------------------------------------------===//
Ted Kremenek40fc5c72008-07-18 15:54:51 +000070// Useful predicates.
71//===----------------------------------------------------------------------===//
72
Zhongxing Xu1c96b242008-10-17 05:57:07 +000073bool SVal::isZeroConstant() const {
74 if (isa<loc::ConcreteInt>(*this))
75 return cast<loc::ConcreteInt>(*this).getValue() == 0;
76 else if (isa<nonloc::ConcreteInt>(*this))
77 return cast<nonloc::ConcreteInt>(*this).getValue() == 0;
Ted Kremenek40fc5c72008-07-18 15:54:51 +000078 else
79 return false;
80}
81
82
83//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +000084// Transfer function dispatch for Non-Locs.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +000085//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +000086
Zhongxing Xu1c96b242008-10-17 05:57:07 +000087SVal nonloc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
Ted Kremenek75b0a1c2008-07-18 15:59:33 +000088 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +000089 const nonloc::ConcreteInt& R) const {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000090
Ted Kremenek75b0a1c2008-07-18 15:59:33 +000091 const llvm::APSInt* X =
92 BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +000093
94 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +000095 return nonloc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +000096 else
97 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +000098}
99
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000100 // Bitwise-Complement.
101
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000102nonloc::ConcreteInt
103nonloc::ConcreteInt::EvalComplement(BasicValueFactory& BasicVals) const {
Ted Kremenek240f1f02008-03-07 20:13:31 +0000104 return BasicVals.getValue(~getValue());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000105}
106
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000107 // Unary Minus.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000108
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000109nonloc::ConcreteInt
110nonloc::ConcreteInt::EvalMinus(BasicValueFactory& BasicVals, UnaryOperator* U) const {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000111 assert (U->getType() == U->getSubExpr()->getType());
112 assert (U->getType()->isIntegerType());
Ted Kremenek240f1f02008-03-07 20:13:31 +0000113 return BasicVals.getValue(-getValue());
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000114}
115
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000116//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000117// Transfer function dispatch for Locs.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000118//===----------------------------------------------------------------------===//
119
Ted Kremenekccaad9d2008-10-30 17:53:23 +0000120SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
121 BinaryOperator::Opcode Op,
122 const loc::ConcreteInt& R) const {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000123
124 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
125 (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
126
Ted Kremenek240f1f02008-03-07 20:13:31 +0000127 const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000128
129 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000130 return loc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000131 else
132 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000133}
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000134
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000135NonLoc Loc::EQ(BasicValueFactory& BasicVals, const Loc& R) const {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000136
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000137 switch (getSubKind()) {
138 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000139 assert(false && "EQ not implemented for this Loc.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000140 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000141
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000142 case loc::ConcreteIntKind:
143 if (isa<loc::ConcreteInt>(R)) {
144 bool b = cast<loc::ConcreteInt>(this)->getValue() ==
145 cast<loc::ConcreteInt>(R).getValue();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000146
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000147 return NonLoc::MakeIntTruthVal(BasicVals, b);
Ted Kremenek0806acf2008-02-05 23:08:41 +0000148 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000149 else if (isa<loc::SymbolVal>(R)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000150
Ted Kremenek0806acf2008-02-05 23:08:41 +0000151 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000152 BasicVals.getConstraint(cast<loc::SymbolVal>(R).getSymbol(),
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000153 BinaryOperator::EQ,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000154 cast<loc::ConcreteInt>(this)->getValue());
Ted Kremenek0806acf2008-02-05 23:08:41 +0000155
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000156 return nonloc::SymIntConstraintVal(C);
Ted Kremenek0806acf2008-02-05 23:08:41 +0000157 }
158
159 break;
160
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000161 case loc::SymbolValKind: {
162 if (isa<loc::ConcreteInt>(R)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000163
164 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000165 BasicVals.getConstraint(cast<loc::SymbolVal>(this)->getSymbol(),
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000166 BinaryOperator::EQ,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000167 cast<loc::ConcreteInt>(R).getValue());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000168
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000169 return nonloc::SymIntConstraintVal(C);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000170 }
Ted Kremenek0806acf2008-02-05 23:08:41 +0000171
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000172 assert (!isa<loc::SymbolVal>(R) && "FIXME: Implement unification.");
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000173
174 break;
Ted Kremenek0806acf2008-02-05 23:08:41 +0000175 }
Ted Kremenek0806acf2008-02-05 23:08:41 +0000176
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000177 case loc::MemRegionKind:
178 if (isa<loc::MemRegionVal>(R)) {
179 bool b = cast<loc::MemRegionVal>(*this) == cast<loc::MemRegionVal>(R);
180 return NonLoc::MakeIntTruthVal(BasicVals, b);
Ted Kremenek0806acf2008-02-05 23:08:41 +0000181 }
182
183 break;
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000184 }
Ted Kremenek0806acf2008-02-05 23:08:41 +0000185
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000186 return NonLoc::MakeIntTruthVal(BasicVals, false);
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000187}
188
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000189NonLoc Loc::NE(BasicValueFactory& BasicVals, const Loc& R) const {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000190 switch (getSubKind()) {
191 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000192 assert(false && "NE not implemented for this Loc.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000193 break;
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000194
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000195 case loc::ConcreteIntKind:
196 if (isa<loc::ConcreteInt>(R)) {
197 bool b = cast<loc::ConcreteInt>(this)->getValue() !=
198 cast<loc::ConcreteInt>(R).getValue();
Ted Kremenek0806acf2008-02-05 23:08:41 +0000199
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000200 return NonLoc::MakeIntTruthVal(BasicVals, b);
Ted Kremenek0806acf2008-02-05 23:08:41 +0000201 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000202 else if (isa<loc::SymbolVal>(R)) {
Ted Kremenek0806acf2008-02-05 23:08:41 +0000203
204 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000205 BasicVals.getConstraint(cast<loc::SymbolVal>(R).getSymbol(),
Ted Kremenek0806acf2008-02-05 23:08:41 +0000206 BinaryOperator::NE,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000207 cast<loc::ConcreteInt>(this)->getValue());
Ted Kremenek0806acf2008-02-05 23:08:41 +0000208
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000209 return nonloc::SymIntConstraintVal(C);
Ted Kremenek0806acf2008-02-05 23:08:41 +0000210 }
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000211
Ted Kremenek0806acf2008-02-05 23:08:41 +0000212 break;
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000213
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000214 case loc::SymbolValKind: {
215 if (isa<loc::ConcreteInt>(R)) {
Ted Kremenek0806acf2008-02-05 23:08:41 +0000216
217 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000218 BasicVals.getConstraint(cast<loc::SymbolVal>(this)->getSymbol(),
Ted Kremenek0806acf2008-02-05 23:08:41 +0000219 BinaryOperator::NE,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000220 cast<loc::ConcreteInt>(R).getValue());
Ted Kremenek0806acf2008-02-05 23:08:41 +0000221
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000222 return nonloc::SymIntConstraintVal(C);
Ted Kremenek0806acf2008-02-05 23:08:41 +0000223 }
224
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000225 assert (!isa<loc::SymbolVal>(R) && "FIXME: Implement sym !=.");
Ted Kremenek0806acf2008-02-05 23:08:41 +0000226
227 break;
228 }
229
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000230 case loc::MemRegionKind:
231 if (isa<loc::MemRegionVal>(R)) {
232 bool b = cast<loc::MemRegionVal>(*this)==cast<loc::MemRegionVal>(R);
233 return NonLoc::MakeIntTruthVal(BasicVals, b);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000234 }
Ted Kremenek0806acf2008-02-05 23:08:41 +0000235
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000236 break;
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000237 }
Ted Kremenek0806acf2008-02-05 23:08:41 +0000238
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000239 return NonLoc::MakeIntTruthVal(BasicVals, true);
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000240}
241
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000242//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000243// Utility methods for constructing Non-Locs.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000244//===----------------------------------------------------------------------===//
245
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000246NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, uint64_t X, QualType T) {
247 return nonloc::ConcreteInt(BasicVals.getValue(X, T));
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000248}
249
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000250NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, IntegerLiteral* I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000251
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000252 return nonloc::ConcreteInt(BasicVals.getValue(APSInt(I->getValue(),
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000253 I->getType()->isUnsignedIntegerType())));
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000254}
255
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000256NonLoc NonLoc::MakeIntTruthVal(BasicValueFactory& BasicVals, bool b) {
257 return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000258}
259
Ted Kremenek632e8b82008-10-30 17:44:46 +0000260NonLoc NonLoc::MakeCompoundVal(QualType T, llvm::ImmutableList<SVal> Vals,
Zhongxing Xu6764b722008-10-30 04:58:00 +0000261 BasicValueFactory& BasicVals) {
Ted Kremenek632e8b82008-10-30 17:44:46 +0000262 return nonloc::CompoundVal(BasicVals.getCompoundValData(T, Vals));
Zhongxing Xu6764b722008-10-30 04:58:00 +0000263}
264
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000265SVal SVal::GetSymbolValue(SymbolManager& SymMgr, VarDecl* D) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000266
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000267 QualType T = D->getType();
268
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000269 if (Loc::IsLocType(T))
270 return loc::SymbolVal(SymMgr.getSymbol(D));
Ted Kremenekc1ff3cd2008-04-30 22:48:21 +0000271
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000272 return nonloc::SymbolVal(SymMgr.getSymbol(D));
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000273}
274
Ted Kremenek632e8b82008-10-30 17:44:46 +0000275nonloc::LocAsInteger nonloc::LocAsInteger::Make(BasicValueFactory& Vals, Loc V,
276 unsigned Bits) {
277 return LocAsInteger(Vals.getPersistentSValWithData(V, Bits));
278}
279
Ted Kremenek2a502572008-02-12 21:37:56 +0000280//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000281// Utility methods for constructing Locs.
Ted Kremenek2a502572008-02-12 21:37:56 +0000282//===----------------------------------------------------------------------===//
283
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000284Loc Loc::MakeVal(AddrLabelExpr* E) { return loc::GotoLabel(E->getLabel()); }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000285
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000286//===----------------------------------------------------------------------===//
287// Pretty-Printing.
288//===----------------------------------------------------------------------===//
289
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000290void SVal::printStdErr() const { print(*llvm::cerr.stream()); }
Ted Kremenek2a502572008-02-12 21:37:56 +0000291
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000292void SVal::print(std::ostream& Out) const {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000293
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000294 switch (getBaseKind()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000295
Ted Kremenek53c641a2008-02-08 03:02:48 +0000296 case UnknownKind:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000297 Out << "Invalid"; break;
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000298
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000299 case NonLocKind:
300 cast<NonLoc>(this)->print(Out); break;
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000301
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000302 case LocKind:
303 cast<Loc>(this)->print(Out); break;
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000304
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000305 case UndefinedKind:
306 Out << "Undefined"; break;
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000307
308 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000309 assert (false && "Invalid SVal.");
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000310 }
311}
312
Ted Kremenek0806acf2008-02-05 23:08:41 +0000313static void printOpcode(std::ostream& Out, BinaryOperator::Opcode Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000314
315 switch (Op) {
316 case BinaryOperator::Mul: Out << '*' ; break;
317 case BinaryOperator::Div: Out << '/' ; break;
318 case BinaryOperator::Rem: Out << '%' ; break;
319 case BinaryOperator::Add: Out << '+' ; break;
320 case BinaryOperator::Sub: Out << '-' ; break;
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000321 case BinaryOperator::Shl: Out << "<<" ; break;
322 case BinaryOperator::Shr: Out << ">>" ; break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000323 case BinaryOperator::LT: Out << "<" ; break;
324 case BinaryOperator::GT: Out << '>' ; break;
325 case BinaryOperator::LE: Out << "<=" ; break;
326 case BinaryOperator::GE: Out << ">=" ; break;
327 case BinaryOperator::EQ: Out << "==" ; break;
328 case BinaryOperator::NE: Out << "!=" ; break;
329 case BinaryOperator::And: Out << '&' ; break;
330 case BinaryOperator::Xor: Out << '^' ; break;
331 case BinaryOperator::Or: Out << '|' ; break;
332
Ted Kremenek0806acf2008-02-05 23:08:41 +0000333 default: assert(false && "Not yet implemented.");
334 }
335}
336
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000337void NonLoc::print(std::ostream& Out) const {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000338
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000339 switch (getSubKind()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000340
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000341 case nonloc::ConcreteIntKind:
342 Out << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000343
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000344 if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000345 Out << 'U';
346
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000347 break;
348
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000349 case nonloc::SymbolValKind:
350 Out << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000351 break;
Ted Kremenek0806acf2008-02-05 23:08:41 +0000352
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000353 case nonloc::SymIntConstraintValKind: {
354 const nonloc::SymIntConstraintVal& C =
355 *cast<nonloc::SymIntConstraintVal>(this);
Ted Kremenek0806acf2008-02-05 23:08:41 +0000356
357 Out << '$' << C.getConstraint().getSymbol() << ' ';
358 printOpcode(Out, C.getConstraint().getOpcode());
Chris Lattner405674c2008-08-23 22:23:37 +0000359 Out << ' ' << C.getConstraint().getInt().getZExtValue();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000360
361 if (C.getConstraint().getInt().isUnsigned())
362 Out << 'U';
363
Ted Kremenek0806acf2008-02-05 23:08:41 +0000364 break;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +0000365 }
366
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000367 case nonloc::LocAsIntegerKind: {
368 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
369 C.getLoc().print(Out);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +0000370 Out << " [as " << C.getNumBits() << " bit integer]";
371 break;
372 }
Ted Kremenek632e8b82008-10-30 17:44:46 +0000373
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000374 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000375 assert (false && "Pretty-printed not implemented for this NonLoc.");
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000376 break;
377 }
378}
379
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000380void Loc::print(std::ostream& Out) const {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000381
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000382 switch (getSubKind()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000383
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000384 case loc::ConcreteIntKind:
385 Out << cast<loc::ConcreteInt>(this)->getValue().getZExtValue()
386 << " (Loc)";
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000387 break;
388
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000389 case loc::SymbolValKind:
390 Out << '$' << cast<loc::SymbolVal>(this)->getSymbol();
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000391 break;
Ted Kremenek2a502572008-02-12 21:37:56 +0000392
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000393 case loc::GotoLabelKind:
Ted Kremenek2a502572008-02-12 21:37:56 +0000394 Out << "&&"
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000395 << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
Ted Kremenek2a502572008-02-12 21:37:56 +0000396 break;
Ted Kremenek08b66252008-02-06 04:31:33 +0000397
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000398 case loc::MemRegionKind:
399 Out << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
Ted Kremenekde434242008-02-19 01:44:53 +0000400 break;
401
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000402 case loc::FuncValKind:
Ted Kremenekde434242008-02-19 01:44:53 +0000403 Out << "function "
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000404 << cast<loc::FuncVal>(this)->getDecl()->getIdentifier()->getName();
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000405 break;
406
407 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000408 assert (false && "Pretty-printing not implemented for this Loc.");
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000409 break;
410 }
411}
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000412
413//===----------------------------------------------------------------------===//
414// Pretty-Printing with llvm::raw_ostream.
415//===----------------------------------------------------------------------===//
416
417void SVal::print(llvm::raw_ostream& Out) const {
418
419 switch (getBaseKind()) {
420
421 case UnknownKind:
422 Out << "Invalid"; break;
423
424 case NonLocKind:
425 cast<NonLoc>(this)->print(Out); break;
426
427 case LocKind:
428 cast<Loc>(this)->print(Out); break;
429
430 case UndefinedKind:
431 Out << "Undefined"; break;
432
433 default:
434 assert (false && "Invalid SVal.");
435 }
436}
437
438static void printOpcode(llvm::raw_ostream& Out, BinaryOperator::Opcode Op) {
439
440 switch (Op) {
441 case BinaryOperator::Mul: Out << '*' ; break;
442 case BinaryOperator::Div: Out << '/' ; break;
443 case BinaryOperator::Rem: Out << '%' ; break;
444 case BinaryOperator::Add: Out << '+' ; break;
445 case BinaryOperator::Sub: Out << '-' ; break;
446 case BinaryOperator::Shl: Out << "<<" ; break;
447 case BinaryOperator::Shr: Out << ">>" ; break;
448 case BinaryOperator::LT: Out << "<" ; break;
449 case BinaryOperator::GT: Out << '>' ; break;
450 case BinaryOperator::LE: Out << "<=" ; break;
451 case BinaryOperator::GE: Out << ">=" ; break;
452 case BinaryOperator::EQ: Out << "==" ; break;
453 case BinaryOperator::NE: Out << "!=" ; break;
454 case BinaryOperator::And: Out << '&' ; break;
455 case BinaryOperator::Xor: Out << '^' ; break;
456 case BinaryOperator::Or: Out << '|' ; break;
457
458 default: assert(false && "Not yet implemented.");
459 }
460}
461
462void NonLoc::print(llvm::raw_ostream& Out) const {
463
464 switch (getSubKind()) {
465
466 case nonloc::ConcreteIntKind:
467 Out << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
468
469 if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
470 Out << 'U';
471
472 break;
473
474 case nonloc::SymbolValKind:
475 Out << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
476 break;
477
478 case nonloc::SymIntConstraintValKind: {
479 const nonloc::SymIntConstraintVal& C =
480 *cast<nonloc::SymIntConstraintVal>(this);
481
482 Out << '$' << C.getConstraint().getSymbol() << ' ';
483 printOpcode(Out, C.getConstraint().getOpcode());
484 Out << ' ' << C.getConstraint().getInt().getZExtValue();
485
486 if (C.getConstraint().getInt().isUnsigned())
487 Out << 'U';
488
489 break;
490 }
491
492 case nonloc::LocAsIntegerKind: {
493 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
494 C.getLoc().print(Out);
495 Out << " [as " << C.getNumBits() << " bit integer]";
496 break;
497 }
498
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000499 case nonloc::CompoundValKind: {
500 const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
501 Out << " { ";
502 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I)
503 (*I).print(Out);
504 Out << " }";
505 break;
506 }
507
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000508 default:
509 assert (false && "Pretty-printed not implemented for this NonLoc.");
510 break;
511 }
512}
513
514void Loc::print(llvm::raw_ostream& Out) const {
515
516 switch (getSubKind()) {
517
518 case loc::ConcreteIntKind:
519 Out << cast<loc::ConcreteInt>(this)->getValue().getZExtValue()
520 << " (Loc)";
521 break;
522
523 case loc::SymbolValKind:
524 Out << '$' << cast<loc::SymbolVal>(this)->getSymbol();
525 break;
526
527 case loc::GotoLabelKind:
528 Out << "&&"
529 << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
530 break;
531
532 case loc::MemRegionKind:
533 Out << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
534 break;
535
536 case loc::FuncValKind:
537 Out << "function "
538 << cast<loc::FuncVal>(this)->getDecl()->getIdentifier()->getName();
539 break;
540
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000541 default:
542 assert (false && "Pretty-printing not implemented for this Loc.");
543 break;
544 }
545}