clang  3.9.0
SVals.cpp
Go to the documentation of this file.
1 //= 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 //
10 // This file defines SVal, Loc, and NonLoc, classes that represent
11 // abstract r-values for use with path-sensitive value tracking.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "clang/AST/ExprObjC.h"
18 #include "llvm/Support/raw_ostream.h"
19 using namespace clang;
20 using namespace ento;
21 using llvm::APSInt;
22 
23 //===----------------------------------------------------------------------===//
24 // Symbol iteration within an SVal.
25 //===----------------------------------------------------------------------===//
26 
27 
28 //===----------------------------------------------------------------------===//
29 // Utility methods.
30 //===----------------------------------------------------------------------===//
31 
33  if (Optional<nonloc::SymbolVal> SV = getAs<nonloc::SymbolVal>()) {
34  SymbolRef sym = SV->getSymbol();
35  if (isa<SymbolConjured>(sym))
36  return true;
37  }
38 
39  if (Optional<loc::MemRegionVal> RV = getAs<loc::MemRegionVal>()) {
40  const MemRegion *R = RV->getRegion();
41  if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
42  SymbolRef sym = SR->getSymbol();
43  if (isa<SymbolConjured>(sym))
44  return true;
45  }
46  }
47 
48  return false;
49 }
50 
52  if (Optional<loc::MemRegionVal> X = getAs<loc::MemRegionVal>()) {
53  const MemRegion* R = X->getRegion();
54  if (const FunctionCodeRegion *CTR = R->getAs<FunctionCodeRegion>())
55  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CTR->getDecl()))
56  return FD;
57  }
58 
59  return nullptr;
60 }
61 
62 /// \brief If this SVal is a location (subclasses Loc) and wraps a symbol,
63 /// return that SymbolRef. Otherwise return 0.
64 ///
65 /// Implicit casts (ex: void* -> char*) can turn Symbolic region into Element
66 /// region. If that is the case, gets the underlining region.
67 /// When IncludeBaseRegions is set to true and the SubRegion is non-symbolic,
68 /// the first symbolic parent region is returned.
69 SymbolRef SVal::getAsLocSymbol(bool IncludeBaseRegions) const {
70  // FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
71  if (Optional<nonloc::LocAsInteger> X = getAs<nonloc::LocAsInteger>())
72  return X->getLoc().getAsLocSymbol();
73 
74  if (Optional<loc::MemRegionVal> X = getAs<loc::MemRegionVal>()) {
75  const MemRegion *R = X->getRegion();
76  if (const SymbolicRegion *SymR = IncludeBaseRegions ?
77  R->getSymbolicBase() :
78  dyn_cast<SymbolicRegion>(R->StripCasts()))
79  return SymR->getSymbol();
80  }
81  return nullptr;
82 }
83 
84 /// Get the symbol in the SVal or its base region.
86  Optional<loc::MemRegionVal> X = getAs<loc::MemRegionVal>();
87 
88  if (!X)
89  return nullptr;
90 
91  const MemRegion *R = X->getRegion();
92 
93  while (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
94  if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SR))
95  return SymR->getSymbol();
96  else
97  R = SR->getSuperRegion();
98  }
99 
100  return nullptr;
101 }
102 
103 // TODO: The next 3 functions have to be simplified.
104 
105 /// \brief If this SVal wraps a symbol return that SymbolRef.
106 /// Otherwise, return 0.
107 ///
108 /// Casts are ignored during lookup.
109 /// \param IncludeBaseRegions The boolean that controls whether the search
110 /// should continue to the base regions if the region is not symbolic.
111 SymbolRef SVal::getAsSymbol(bool IncludeBaseRegion) const {
112  // FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
113  if (Optional<nonloc::SymbolVal> X = getAs<nonloc::SymbolVal>())
114  return X->getSymbol();
115 
116  return getAsLocSymbol(IncludeBaseRegion);
117 }
118 
119 /// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
120 /// return that expression. Otherwise return NULL.
122  if (Optional<nonloc::SymbolVal> X = getAs<nonloc::SymbolVal>())
123  return X->getSymbol();
124 
125  return getAsSymbol();
126 }
127 
128 const SymExpr* SVal::getAsSymExpr() const {
129  const SymExpr* Sym = getAsSymbol();
130  if (!Sym)
131  Sym = getAsSymbolicExpression();
132  return Sym;
133 }
134 
135 const MemRegion *SVal::getAsRegion() const {
136  if (Optional<loc::MemRegionVal> X = getAs<loc::MemRegionVal>())
137  return X->getRegion();
138 
139  if (Optional<nonloc::LocAsInteger> X = getAs<nonloc::LocAsInteger>())
140  return X->getLoc().getAsRegion();
141 
142  return nullptr;
143 }
144 
145 const MemRegion *loc::MemRegionVal::stripCasts(bool StripBaseCasts) const {
146  const MemRegion *R = getRegion();
147  return R ? R->StripCasts(StripBaseCasts) : nullptr;
148 }
149 
151  return static_cast<const LazyCompoundValData*>(Data)->getStore();
152 }
153 
155  return static_cast<const LazyCompoundValData*>(Data)->getRegion();
156 }
157 
158 //===----------------------------------------------------------------------===//
159 // Other Iterators.
160 //===----------------------------------------------------------------------===//
161 
163  return getValue()->begin();
164 }
165 
167  return getValue()->end();
168 }
169 
170 //===----------------------------------------------------------------------===//
171 // Useful predicates.
172 //===----------------------------------------------------------------------===//
173 
174 bool SVal::isConstant() const {
175  return getAs<nonloc::ConcreteInt>() || getAs<loc::ConcreteInt>();
176 }
177 
178 bool SVal::isConstant(int I) const {
179  if (Optional<loc::ConcreteInt> LV = getAs<loc::ConcreteInt>())
180  return LV->getValue() == I;
181  if (Optional<nonloc::ConcreteInt> NV = getAs<nonloc::ConcreteInt>())
182  return NV->getValue() == I;
183  return false;
184 }
185 
186 bool SVal::isZeroConstant() const {
187  return isConstant(0);
188 }
189 
190 
191 //===----------------------------------------------------------------------===//
192 // Transfer function dispatch for Non-Locs.
193 //===----------------------------------------------------------------------===//
194 
197  const nonloc::ConcreteInt& R) const {
198  const llvm::APSInt* X =
199  svalBuilder.getBasicValueFactory().evalAPSInt(Op, getValue(), R.getValue());
200 
201  if (X)
202  return nonloc::ConcreteInt(*X);
203  else
204  return UndefinedVal();
205 }
206 
209  return svalBuilder.makeIntVal(~getValue());
210 }
211 
214  return svalBuilder.makeIntVal(-getValue());
215 }
216 
217 //===----------------------------------------------------------------------===//
218 // Transfer function dispatch for Locs.
219 //===----------------------------------------------------------------------===//
220 
223  const loc::ConcreteInt& R) const {
224 
225  assert(BinaryOperator::isComparisonOp(Op) || Op == BO_Sub);
226 
227  const llvm::APSInt *X = BasicVals.evalAPSInt(Op, getValue(), R.getValue());
228 
229  if (X)
230  return nonloc::ConcreteInt(*X);
231  else
232  return UndefinedVal();
233 }
234 
235 //===----------------------------------------------------------------------===//
236 // Pretty-Printing.
237 //===----------------------------------------------------------------------===//
238 
239 LLVM_DUMP_METHOD void SVal::dump() const { dumpToStream(llvm::errs()); }
240 
241 void SVal::dumpToStream(raw_ostream &os) const {
242  switch (getBaseKind()) {
243  case UnknownValKind:
244  os << "Unknown";
245  break;
246  case NonLocKind:
247  castAs<NonLoc>().dumpToStream(os);
248  break;
249  case LocKind:
250  castAs<Loc>().dumpToStream(os);
251  break;
252  case UndefinedValKind:
253  os << "Undefined";
254  break;
255  }
256 }
257 
258 void NonLoc::dumpToStream(raw_ostream &os) const {
259  switch (getSubKind()) {
260  case nonloc::ConcreteIntKind: {
261  const nonloc::ConcreteInt& C = castAs<nonloc::ConcreteInt>();
262  if (C.getValue().isUnsigned())
263  os << C.getValue().getZExtValue();
264  else
265  os << C.getValue().getSExtValue();
266  os << ' ' << (C.getValue().isUnsigned() ? 'U' : 'S')
267  << C.getValue().getBitWidth() << 'b';
268  break;
269  }
270  case nonloc::SymbolValKind: {
271  os << castAs<nonloc::SymbolVal>().getSymbol();
272  break;
273  }
274  case nonloc::LocAsIntegerKind: {
275  const nonloc::LocAsInteger& C = castAs<nonloc::LocAsInteger>();
276  os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
277  break;
278  }
279  case nonloc::CompoundValKind: {
280  const nonloc::CompoundVal& C = castAs<nonloc::CompoundVal>();
281  os << "compoundVal{";
282  bool first = true;
283  for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
284  if (first) {
285  os << ' '; first = false;
286  }
287  else
288  os << ", ";
289 
290  (*I).dumpToStream(os);
291  }
292  os << "}";
293  break;
294  }
295  case nonloc::LazyCompoundValKind: {
296  const nonloc::LazyCompoundVal &C = castAs<nonloc::LazyCompoundVal>();
297  os << "lazyCompoundVal{" << const_cast<void *>(C.getStore())
298  << ',' << C.getRegion()
299  << '}';
300  break;
301  }
302  default:
303  assert (false && "Pretty-printed not implemented for this NonLoc.");
304  break;
305  }
306 }
307 
308 void Loc::dumpToStream(raw_ostream &os) const {
309  switch (getSubKind()) {
310  case loc::ConcreteIntKind:
311  os << castAs<loc::ConcreteInt>().getValue().getZExtValue() << " (Loc)";
312  break;
313  case loc::GotoLabelKind:
314  os << "&&" << castAs<loc::GotoLabel>().getLabel()->getName();
315  break;
316  case loc::MemRegionValKind:
317  os << '&' << castAs<loc::MemRegionVal>().getRegion()->getString();
318  break;
319  default:
320  llvm_unreachable("Pretty-printing not implemented for this Loc.");
321  }
322 }
const SymExpr * getAsSymExpr() const
Definition: SVals.cpp:128
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1561
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:494
bool hasConjuredSymbol() const
hasConjuredSymbol - If this SVal wraps a conjured symbol, return true;
Definition: SVals.cpp:32
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
Definition: SValBuilder.h:237
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:79
const MemRegion * stripCasts(bool StripBaseCasts=true) const
Get the underlining region and strip casts.
Definition: SVals.cpp:145
const RegionTy * getAs() const
Definition: MemRegion.h:1106
void dumpToStream(raw_ostream &Out) const
Definition: SVals.cpp:258
SymbolRef getLocSymbolInBase() const
Get the symbol in the SVal or its base region.
Definition: SVals.cpp:85
Value representing integer constant.
Definition: SVals.h:341
bool isZeroConstant() const
Definition: SVals.cpp:186
Symbolic value.
Definition: SymExpr.h:29
bool isComparisonOp() const
Definition: Expr.h:2990
SymbolRef getAsLocSymbol(bool IncludeBaseRegions=false) const
If this SVal is a location and wraps a symbol, return that SymbolRef.
Definition: SVals.cpp:69
SVal evalBinOp(BasicValueFactory &BasicVals, BinaryOperator::Opcode Op, const ConcreteInt &R) const
Definition: SVals.cpp:221
const FunctionDecl * getAsFunctionDecl() const
getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a CodeTextRegion wrapping a FunctionDecl...
Definition: SVals.cpp:51
BinaryOperatorKind
const SymbolicRegion * getSymbolicBase() const
If this is a symbolic region, returns the region.
Definition: MemRegion.cpp:1177
unsigned getNumBits() const
Definition: SVals.h:393
bool isConstant() const
Definition: SVals.cpp:174
detail::InMemoryDirectory::const_iterator I
llvm::ImmutableList< SVal >::iterator iterator
Definition: SVals.h:422
const MemRegion * StripCasts(bool StripBaseCasts=true) const
Definition: MemRegion.cpp:1155
SymbolicRegion - A special, "non-concrete" region.
Definition: MemRegion.h:707
static SVal getValue(SVal val, SValBuilder &svalBuilder)
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
FunctionCodeRegion - A region that represents code texts of function.
Definition: MemRegion.h:541
SVal evalBinOp(SValBuilder &svalBuilder, BinaryOperator::Opcode Op, const ConcreteInt &R) const
Definition: SVals.cpp:195
void dumpToStream(raw_ostream &Out) const
Definition: SVals.cpp:308
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:46
const llvm::APSInt * evalAPSInt(BinaryOperator::Opcode Op, const llvm::APSInt &V1, const llvm::APSInt &V2)
ConcreteInt evalMinus(SValBuilder &svalBuilder) const
Definition: SVals.cpp:213
void dump() const
Definition: SVals.cpp:239
const llvm::APSInt & getValue() const
Definition: SVals.h:538
detail::InMemoryDirectory::const_iterator E
const MemRegion * getAsRegion() const
Definition: SVals.cpp:135
ConcreteInt evalComplement(SValBuilder &svalBuilder) const
Definition: SVals.cpp:208
BasicValueFactory & getBasicValueFactory()
Definition: SValBuilder.h:139
SubRegion - A region that subsets another larger region.
Definition: MemRegion.h:410
const TypedValueRegion * getRegion() const
Definition: SVals.cpp:154
void dumpToStream(raw_ostream &OS) const
Definition: SVals.cpp:241
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:12171
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
Definition: SVals.cpp:111
const SymExpr * getAsSymbolicExpression() const
getAsSymbolicExpression - If this Sval wraps a symbolic expression then return that expression...
Definition: SVals.cpp:121
iterator begin() const
Definition: SVals.cpp:162
const llvm::APSInt & getValue() const
Definition: SVals.h:345
const MemRegion * getRegion() const
Get the underlining region.
Definition: SVals.h:501
const void * getStore() const
Definition: SVals.cpp:150