Coverage Report

Created: 2019-03-22 08:08

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SVals.h
Line
Count
Source (jump to first uncovered line)
1
//===- SVals.h - Abstract Values for Static Analysis ------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file defines SVal, Loc, and NonLoc, classes that represent
10
//  abstract r-values for use with path-sensitive value tracking.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALS_H
15
#define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALS_H
16
17
#include "clang/AST/Expr.h"
18
#include "clang/AST/Type.h"
19
#include "clang/Basic/LLVM.h"
20
#include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
21
#include "llvm/ADT/FoldingSet.h"
22
#include "llvm/ADT/ImmutableList.h"
23
#include "llvm/ADT/None.h"
24
#include "llvm/ADT/Optional.h"
25
#include "llvm/ADT/PointerUnion.h"
26
#include "llvm/Support/Casting.h"
27
#include <cassert>
28
#include <cstdint>
29
#include <utility>
30
31
//==------------------------------------------------------------------------==//
32
//  Base SVal types.
33
//==------------------------------------------------------------------------==//
34
35
namespace clang {
36
37
class CXXBaseSpecifier;
38
class DeclaratorDecl;
39
class FunctionDecl;
40
class LabelDecl;
41
42
namespace ento {
43
44
class BasicValueFactory;
45
class CompoundValData;
46
class LazyCompoundValData;
47
class MemRegion;
48
class PointerToMemberData;
49
class SValBuilder;
50
class TypedValueRegion;
51
52
namespace nonloc {
53
54
/// Sub-kinds for NonLoc values.
55
enum Kind {
56
#define NONLOC_SVAL(Id, Parent) Id ## Kind,
57
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.def"
58
};
59
60
} // namespace nonloc
61
62
namespace loc {
63
64
/// Sub-kinds for Loc values.
65
enum Kind {
66
#define LOC_SVAL(Id, Parent) Id ## Kind,
67
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.def"
68
};
69
70
} // namespace loc
71
72
/// SVal - This represents a symbolic expression, which can be either
73
///  an L-value or an R-value.
74
///
75
0
class SVal {
76
public:
77
  enum BaseKind {
78
    // The enumerators must be representable using 2 bits.
79
#define BASIC_SVAL(Id, Parent) Id ## Kind,
80
#define ABSTRACT_SVAL_WITH_KIND(Id, Parent) Id ## Kind,
81
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.def"
82
  };
83
  enum { BaseBits = 2, BaseMask = 0x3 };
84
85
protected:
86
  const void *Data = nullptr;
87
88
  /// The lowest 2 bits are a BaseKind (0 -- 3).
89
  ///  The higher bits are an unsigned "kind" value.
90
  unsigned Kind = 0;
91
92
  explicit SVal(const void *d, bool isLoc, unsigned ValKind)
93
1.93M
      : Data(d), Kind((isLoc ? LocKind : NonLocKind) | (ValKind << BaseBits)) {}
94
95
329k
  explicit SVal(BaseKind k, const void *D = nullptr) : Data(D), Kind(k) {}
96
97
public:
98
122k
  explicit SVal() = default;
99
100
  /// Convert to the specified SVal type, asserting that this SVal is of
101
  /// the desired type.
102
  template<typename T>
103
4.50M
  T castAs() const {
104
4.50M
    assert(T::isKind(*this));
105
4.50M
    return *static_cast<const T *>(this);
106
4.50M
  }
clang::ento::DefinedOrUnknownSVal clang::ento::SVal::castAs<clang::ento::DefinedOrUnknownSVal>() const
Line
Count
Source
103
305k
  T castAs() const {
104
305k
    assert(T::isKind(*this));
105
305k
    return *static_cast<const T *>(this);
106
305k
  }
clang::ento::Loc clang::ento::SVal::castAs<clang::ento::Loc>() const
Line
Count
Source
103
450k
  T castAs() const {
104
450k
    assert(T::isKind(*this));
105
450k
    return *static_cast<const T *>(this);
106
450k
  }
clang::ento::DefinedSVal clang::ento::SVal::castAs<clang::ento::DefinedSVal>() const
Line
Count
Source
103
863k
  T castAs() const {
104
863k
    assert(T::isKind(*this));
105
863k
    return *static_cast<const T *>(this);
106
863k
  }
clang::ento::NonLoc clang::ento::SVal::castAs<clang::ento::NonLoc>() const
Line
Count
Source
103
1.02M
  T castAs() const {
104
1.02M
    assert(T::isKind(*this));
105
1.02M
    return *static_cast<const T *>(this);
106
1.02M
  }
clang::ento::UndefinedVal clang::ento::SVal::castAs<clang::ento::UndefinedVal>() const
Line
Count
Source
103
1
  T castAs() const {
104
1
    assert(T::isKind(*this));
105
1
    return *static_cast<const T *>(this);
106
1
  }
clang::ento::UnknownVal clang::ento::SVal::castAs<clang::ento::UnknownVal>() const
Line
Count
Source
103
14
  T castAs() const {
104
14
    assert(T::isKind(*this));
105
14
    return *static_cast<const T *>(this);
106
14
  }
clang::ento::loc::ConcreteInt clang::ento::SVal::castAs<clang::ento::loc::ConcreteInt>() const
Line
Count
Source
103
113
  T castAs() const {
104
113
    assert(T::isKind(*this));
105
113
    return *static_cast<const T *>(this);
106
113
  }
Unexecuted instantiation: clang::ento::loc::GotoLabel clang::ento::SVal::castAs<clang::ento::loc::GotoLabel>() const
clang::ento::loc::MemRegionVal clang::ento::SVal::castAs<clang::ento::loc::MemRegionVal>() const
Line
Count
Source
103
1.00M
  T castAs() const {
104
1.00M
    assert(T::isKind(*this));
105
1.00M
    return *static_cast<const T *>(this);
106
1.00M
  }
clang::ento::nonloc::CompoundVal clang::ento::SVal::castAs<clang::ento::nonloc::CompoundVal>() const
Line
Count
Source
103
1.90k
  T castAs() const {
104
1.90k
    assert(T::isKind(*this));
105
1.90k
    return *static_cast<const T *>(this);
106
1.90k
  }
clang::ento::nonloc::ConcreteInt clang::ento::SVal::castAs<clang::ento::nonloc::ConcreteInt>() const
Line
Count
Source
103
576k
  T castAs() const {
104
576k
    assert(T::isKind(*this));
105
576k
    return *static_cast<const T *>(this);
106
576k
  }
clang::ento::nonloc::LazyCompoundVal clang::ento::SVal::castAs<clang::ento::nonloc::LazyCompoundVal>() const
Line
Count
Source
103
2
  T castAs() const {
104
2
    assert(T::isKind(*this));
105
2
    return *static_cast<const T *>(this);
106
2
  }
clang::ento::nonloc::LocAsInteger clang::ento::SVal::castAs<clang::ento::nonloc::LocAsInteger>() const
Line
Count
Source
103
68
  T castAs() const {
104
68
    assert(T::isKind(*this));
105
68
    return *static_cast<const T *>(this);
106
68
  }
clang::ento::nonloc::SymbolVal clang::ento::SVal::castAs<clang::ento::nonloc::SymbolVal>() const
Line
Count
Source
103
278k
  T castAs() const {
104
278k
    assert(T::isKind(*this));
105
278k
    return *static_cast<const T *>(this);
106
278k
  }
clang::ento::nonloc::PointerToMember clang::ento::SVal::castAs<clang::ento::nonloc::PointerToMember>() const
Line
Count
Source
103
20
  T castAs() const {
104
20
    assert(T::isKind(*this));
105
20
    return *static_cast<const T *>(this);
106
20
  }
Unexecuted instantiation: clang::ento::KnownSVal clang::ento::SVal::castAs<clang::ento::KnownSVal>() const
107
108
  /// Convert to the specified SVal type, returning None if this SVal is
109
  /// not of the desired type.
110
  template<typename T>
111
22.9M
  Optional<T> getAs() const {
112
22.9M
    if (!T::isKind(*this))
113
15.4M
      return None;
114
7.53M
    return *static_cast<const T *>(this);
115
7.53M
  }
llvm::Optional<clang::ento::UndefinedVal> clang::ento::SVal::getAs<clang::ento::UndefinedVal>() const
Line
Count
Source
111
1.31k
  Optional<T> getAs() const {
112
1.31k
    if (!T::isKind(*this))
113
315
      return None;
114
997
    return *static_cast<const T *>(this);
115
997
  }
llvm::Optional<clang::ento::NonLoc> clang::ento::SVal::getAs<clang::ento::NonLoc>() const
Line
Count
Source
111
80.5k
  Optional<T> getAs() const {
112
80.5k
    if (!T::isKind(*this))
113
49.0k
      return None;
114
31.4k
    return *static_cast<const T *>(this);
115
31.4k
  }
llvm::Optional<clang::ento::nonloc::ConcreteInt> clang::ento::SVal::getAs<clang::ento::nonloc::ConcreteInt>() const
Line
Count
Source
111
1.06M
  Optional<T> getAs() const {
112
1.06M
    if (!T::isKind(*this))
113
794k
      return None;
114
268k
    return *static_cast<const T *>(this);
115
268k
  }
llvm::Optional<clang::ento::nonloc::SymbolVal> clang::ento::SVal::getAs<clang::ento::nonloc::SymbolVal>() const
Line
Count
Source
111
4.55M
  Optional<T> getAs() const {
112
4.55M
    if (!T::isKind(*this))
113
2.96M
      return None;
114
1.58M
    return *static_cast<const T *>(this);
115
1.58M
  }
llvm::Optional<clang::ento::Loc> clang::ento::SVal::getAs<clang::ento::Loc>() const
Line
Count
Source
111
1.67M
  Optional<T> getAs() const {
112
1.67M
    if (!T::isKind(*this))
113
378k
      return None;
114
1.29M
    return *static_cast<const T *>(this);
115
1.29M
  }
llvm::Optional<clang::ento::loc::MemRegionVal> clang::ento::SVal::getAs<clang::ento::loc::MemRegionVal>() const
Line
Count
Source
111
7.22M
  Optional<T> getAs() const {
112
7.22M
    if (!T::isKind(*this))
113
3.01M
      return None;
114
4.21M
    return *static_cast<const T *>(this);
115
4.21M
  }
llvm::Optional<clang::ento::DefinedSVal> clang::ento::SVal::getAs<clang::ento::DefinedSVal>() const
Line
Count
Source
111
24.1k
  Optional<T> getAs() const {
112
24.1k
    if (!T::isKind(*this))
113
386
      return None;
114
23.7k
    return *static_cast<const T *>(this);
115
23.7k
  }
llvm::Optional<clang::ento::loc::ConcreteInt> clang::ento::SVal::getAs<clang::ento::loc::ConcreteInt>() const
Line
Count
Source
111
1.50M
  Optional<T> getAs() const {
112
1.50M
    if (!T::isKind(*this))
113
1.48M
      return None;
114
14.0k
    return *static_cast<const T *>(this);
115
14.0k
  }
llvm::Optional<clang::ento::DefinedOrUnknownSVal> clang::ento::SVal::getAs<clang::ento::DefinedOrUnknownSVal>() const
Line
Count
Source
111
3.53k
  Optional<T> getAs() const {
112
3.53k
    if (!T::isKind(*this))
113
1
      return None;
114
3.53k
    return *static_cast<const T *>(this);
115
3.53k
  }
llvm::Optional<clang::ento::loc::GotoLabel> clang::ento::SVal::getAs<clang::ento::loc::GotoLabel>() const
Line
Count
Source
111
9.75k
  Optional<T> getAs() const {
112
9.75k
    if (!T::isKind(*this))
113
9.74k
      return None;
114
14
    return *static_cast<const T *>(this);
115
14
  }
llvm::Optional<clang::ento::nonloc::LazyCompoundVal> clang::ento::SVal::getAs<clang::ento::nonloc::LazyCompoundVal>() const
Line
Count
Source
111
2.23M
  Optional<T> getAs() const {
112
2.23M
    if (!T::isKind(*this))
113
2.15M
      return None;
114
82.1k
    return *static_cast<const T *>(this);
115
82.1k
  }
llvm::Optional<clang::ento::nonloc::CompoundVal> clang::ento::SVal::getAs<clang::ento::nonloc::CompoundVal>() const
Line
Count
Source
111
297k
  Optional<T> getAs() const {
112
297k
    if (!T::isKind(*this))
113
295k
      return None;
114
2.05k
    return *static_cast<const T *>(this);
115
2.05k
  }
llvm::Optional<clang::ento::nonloc::LocAsInteger> clang::ento::SVal::getAs<clang::ento::nonloc::LocAsInteger>() const
Line
Count
Source
111
4.13M
  Optional<T> getAs() const {
112
4.13M
    if (!T::isKind(*this))
113
4.13M
      return None;
114
913
    return *static_cast<const T *>(this);
115
913
  }
llvm::Optional<clang::ento::KnownSVal> clang::ento::SVal::getAs<clang::ento::KnownSVal>() const
Line
Count
Source
111
1.46k
  Optional<T> getAs() const {
112
1.46k
    if (!T::isKind(*this))
113
1
      return None;
114
1.46k
    return *static_cast<const T *>(this);
115
1.46k
  }
llvm::Optional<clang::ento::nonloc::PointerToMember> clang::ento::SVal::getAs<clang::ento::nonloc::PointerToMember>() const
Line
Count
Source
111
168k
  Optional<T> getAs() const {
112
168k
    if (!T::isKind(*this))
113
167k
      return None;
114
247
    return *static_cast<const T *>(this);
115
247
  }
116
117
20.1M
  unsigned getRawKind() const { return Kind; }
118
23.0M
  BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
119
14.9M
  unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; }
120
121
  // This method is required for using SVal in a FoldingSetNode.  It
122
  // extracts a unique signature for this SVal object.
123
4.32M
  void Profile(llvm::FoldingSetNodeID &ID) const {
124
4.32M
    ID.AddInteger((unsigned) getRawKind());
125
4.32M
    ID.AddPointer(Data);
126
4.32M
  }
127
128
5.71M
  bool operator==(const SVal &R) const {
129
5.71M
    return getRawKind() == R.getRawKind() && 
Data == R.Data5.62M
;
130
5.71M
  }
131
132
138k
  bool operator!=(const SVal &R) const {
133
138k
    return !(*this == R);
134
138k
  }
135
136
1.55M
  bool isUnknown() const {
137
1.55M
    return getRawKind() == UnknownValKind;
138
1.55M
  }
139
140
868k
  bool isUndef() const {
141
868k
    return getRawKind() == UndefinedValKind;
142
868k
  }
143
144
1.72M
  bool isUnknownOrUndef() const {
145
1.72M
    return getRawKind() <= UnknownValKind;
146
1.72M
  }
147
148
286k
  bool isValid() const {
149
286k
    return getRawKind() > UnknownValKind;
150
286k
  }
151
152
  bool isConstant() const;
153
154
  bool isConstant(int I) const;
155
156
  bool isZeroConstant() const;
157
158
  /// hasConjuredSymbol - If this SVal wraps a conjured symbol, return true;
159
  bool hasConjuredSymbol() const;
160
161
  /// getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a
162
  /// CodeTextRegion wrapping a FunctionDecl, return that FunctionDecl.
163
  /// Otherwise return 0.
164
  const FunctionDecl *getAsFunctionDecl() const;
165
166
  /// If this SVal is a location and wraps a symbol, return that
167
  ///  SymbolRef. Otherwise return 0.
168
  ///
169
  /// Casts are ignored during lookup.
170
  /// \param IncludeBaseRegions The boolean that controls whether the search
171
  /// should continue to the base regions if the region is not symbolic.
172
  SymbolRef getAsLocSymbol(bool IncludeBaseRegions = false) const;
173
174
  /// Get the symbol in the SVal or its base region.
175
  SymbolRef getLocSymbolInBase() const;
176
177
  /// If this SVal wraps a symbol return that SymbolRef.
178
  /// Otherwise, return 0.
179
  ///
180
  /// Casts are ignored during lookup.
181
  /// \param IncludeBaseRegions The boolean that controls whether the search
182
  /// should continue to the base regions if the region is not symbolic.
183
  SymbolRef getAsSymbol(bool IncludeBaseRegions = false) const;
184
185
  /// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
186
  ///  return that expression.  Otherwise return NULL.
187
  const SymExpr *getAsSymbolicExpression() const;
188
189
  const SymExpr *getAsSymExpr() const;
190
191
  const MemRegion *getAsRegion() const;
192
193
  void dumpToStream(raw_ostream &OS) const;
194
  void dump() const;
195
196
1.52M
  SymExpr::symbol_iterator symbol_begin() const {
197
1.52M
    const SymExpr *SE = getAsSymbol(/*IncludeBaseRegions=*/true);
198
1.52M
    if (SE)
199
806k
      return SE->symbol_begin();
200
722k
    else
201
722k
      return SymExpr::symbol_iterator();
202
1.52M
  }
203
204
1.52M
  SymExpr::symbol_iterator symbol_end() const {
205
1.52M
    return SymExpr::symbol_end();
206
1.52M
  }
207
};
208
209
2
inline raw_ostream &operator<<(raw_ostream &os, clang::ento::SVal V) {
210
2
  V.dumpToStream(os);
211
2
  return os;
212
2
}
213
214
class UndefinedVal : public SVal {
215
public:
216
50.2k
  UndefinedVal() : SVal(UndefinedValKind) {}
217
218
private:
219
  friend class SVal;
220
221
1.31k
  static bool isKind(const SVal& V) {
222
1.31k
    return V.getBaseKind() == UndefinedValKind;
223
1.31k
  }
224
};
225
226
class DefinedOrUnknownSVal : public SVal {
227
public:
228
  // We want calling these methods to be a compiler error since they are
229
  // tautologically false.
230
  bool isUndef() const = delete;
231
  bool isValid() const = delete;
232
233
protected:
234
  DefinedOrUnknownSVal() = default;
235
  explicit DefinedOrUnknownSVal(const void *d, bool isLoc, unsigned ValKind)
236
1.93M
      : SVal(d, isLoc, ValKind) {}
237
279k
  explicit DefinedOrUnknownSVal(BaseKind k, void *D = nullptr) : SVal(k, D) {}
238
239
private:
240
  friend class SVal;
241
242
3.53k
  static bool isKind(const SVal& V) {
243
3.53k
    return !V.isUndef();
244
3.53k
  }
245
};
246
247
class UnknownVal : public DefinedOrUnknownSVal {
248
public:
249
279k
  explicit UnknownVal() : DefinedOrUnknownSVal(UnknownValKind) {}
250
251
private:
252
  friend class SVal;
253
254
0
  static bool isKind(const SVal &V) {
255
0
    return V.getBaseKind() == UnknownValKind;
256
0
  }
257
};
258
259
class DefinedSVal : public DefinedOrUnknownSVal {
260
public:
261
  // We want calling these methods to be a compiler error since they are
262
  // tautologically true/false.
263
  bool isUnknown() const = delete;
264
  bool isUnknownOrUndef() const = delete;
265
  bool isValid() const = delete;
266
267
protected:
268
  DefinedSVal() = default;
269
  explicit DefinedSVal(const void *d, bool isLoc, unsigned ValKind)
270
1.93M
      : DefinedOrUnknownSVal(d, isLoc, ValKind) {}
271
272
private:
273
  friend class SVal;
274
275
24.1k
  static bool isKind(const SVal& V) {
276
24.1k
    return !V.isUnknownOrUndef();
277
24.1k
  }
278
};
279
280
/// Represents an SVal that is guaranteed to not be UnknownVal.
281
class KnownSVal : public SVal {
282
  friend class SVal;
283
284
  KnownSVal() = default;
285
286
1.46k
  static bool isKind(const SVal &V) {
287
1.46k
    return !V.isUnknown();
288
1.46k
  }
289
290
public:
291
  KnownSVal(const DefinedSVal &V) : SVal(V) {}
292
6
  KnownSVal(const UndefinedVal &V) : SVal(V) {}
293
};
294
295
class NonLoc : public DefinedSVal {
296
protected:
297
  NonLoc() = default;
298
  explicit NonLoc(unsigned SubKind, const void *d)
299
1.23M
      : DefinedSVal(d, false, SubKind) {}
300
301
public:
302
  void dumpToStream(raw_ostream &Out) const;
303
304
0
  static bool isCompoundType(QualType T) {
305
0
    return T->isArrayType() || T->isRecordType() ||
306
0
           T->isComplexType() || T->isVectorType();
307
0
  }
308
309
private:
310
  friend class SVal;
311
312
80.5k
  static bool isKind(const SVal& V) {
313
80.5k
    return V.getBaseKind() == NonLocKind;
314
80.5k
  }
315
};
316
317
class Loc : public DefinedSVal {
318
protected:
319
  Loc() = default;
320
  explicit Loc(unsigned SubKind, const void *D)
321
700k
      : DefinedSVal(const_cast<void *>(D), true, SubKind) {}
322
323
public:
324
  void dumpToStream(raw_ostream &Out) const;
325
326
2.09M
  static bool isLocType(QualType T) {
327
2.09M
    return T->isAnyPointerType() || 
T->isBlockPointerType()1.28M
||
328
2.09M
           
T->isReferenceType()1.28M
||
T->isNullPtrType()1.26M
;
329
2.09M
  }
330
331
private:
332
  friend class SVal;
333
334
1.67M
  static bool isKind(const SVal& V) {
335
1.67M
    return V.getBaseKind() == LocKind;
336
1.67M
  }
337
};
338
339
//==------------------------------------------------------------------------==//
340
//  Subclasses of NonLoc.
341
//==------------------------------------------------------------------------==//
342
343
namespace nonloc {
344
345
/// Represents symbolic expression that isn't a location.
346
class SymbolVal : public NonLoc {
347
public:
348
  SymbolVal() = delete;
349
553k
  SymbolVal(SymbolRef sym) : NonLoc(SymbolValKind, sym) {
350
553k
    assert(sym);
351
553k
    assert(!Loc::isLocType(sym->getType()));
352
553k
  }
353
354
2.02M
  SymbolRef getSymbol() const {
355
2.02M
    return (const SymExpr *) Data;
356
2.02M
  }
357
358
166k
  bool isExpression() const {
359
166k
    return !isa<SymbolData>(getSymbol());
360
166k
  }
361
362
private:
363
  friend class SVal;
364
365
4.55M
  static bool isKind(const SVal& V) {
366
4.55M
    return V.getBaseKind() == NonLocKind &&
367
4.55M
           
V.getSubKind() == SymbolValKind3.19M
;
368
4.55M
  }
369
370
0
  static bool isKind(const NonLoc& V) {
371
0
    return V.getSubKind() == SymbolValKind;
372
0
  }
373
};
374
375
/// Value representing integer constant.
376
class ConcreteInt : public NonLoc {
377
public:
378
637k
  explicit ConcreteInt(const llvm::APSInt& V) : NonLoc(ConcreteIntKind, &V) {}
379
380
685k
  const llvm::APSInt& getValue() const {
381
685k
    return *static_cast<const llvm::APSInt *>(Data);
382
685k
  }
383
384
  // Transfer functions for binary/unary operations on ConcreteInts.
385
  SVal evalBinOp(SValBuilder &svalBuilder, BinaryOperator::Opcode Op,
386
                 const ConcreteInt& R) const;
387
388
  ConcreteInt evalComplement(SValBuilder &svalBuilder) const;
389
390
  ConcreteInt evalMinus(SValBuilder &svalBuilder) const;
391
392
private:
393
  friend class SVal;
394
395
  ConcreteInt() = default;
396
397
1.06M
  static bool isKind(const SVal& V) {
398
1.06M
    return V.getBaseKind() == NonLocKind &&
399
1.06M
           
V.getSubKind() == ConcreteIntKind542k
;
400
1.06M
  }
401
402
0
  static bool isKind(const NonLoc& V) {
403
0
    return V.getSubKind() == ConcreteIntKind;
404
0
  }
405
};
406
407
class LocAsInteger : public NonLoc {
408
  friend class ento::SValBuilder;
409
410
  explicit LocAsInteger(const std::pair<SVal, uintptr_t> &data)
411
113
      : NonLoc(LocAsIntegerKind, &data) {
412
113
    // We do not need to represent loc::ConcreteInt as LocAsInteger,
413
113
    // as it'd collapse into a nonloc::ConcreteInt instead.
414
113
    assert(data.first.getBaseKind() == LocKind &&
415
113
           (data.first.getSubKind() == loc::MemRegionValKind ||
416
113
            data.first.getSubKind() == loc::GotoLabelKind));
417
113
  }
418
419
public:
420
873
  Loc getLoc() const {
421
873
    const std::pair<SVal, uintptr_t> *D =
422
873
      static_cast<const std::pair<SVal, uintptr_t> *>(Data);
423
873
    return D->first.castAs<Loc>();
424
873
  }
425
426
0
  Loc getPersistentLoc() const {
427
0
    const std::pair<SVal, uintptr_t> *D =
428
0
      static_cast<const std::pair<SVal, uintptr_t> *>(Data);
429
0
    const SVal& V = D->first;
430
0
    return V.castAs<Loc>();
431
0
  }
432
433
102
  unsigned getNumBits() const {
434
102
    const std::pair<SVal, uintptr_t> *D =
435
102
      static_cast<const std::pair<SVal, uintptr_t> *>(Data);
436
102
    return D->second;
437
102
  }
438
439
private:
440
  friend class SVal;
441
442
  LocAsInteger() = default;
443
444
4.13M
  static bool isKind(const SVal& V) {
445
4.13M
    return V.getBaseKind() == NonLocKind &&
446
4.13M
           
V.getSubKind() == LocAsIntegerKind2.57M
;
447
4.13M
  }
448
449
0
  static bool isKind(const NonLoc& V) {
450
0
    return V.getSubKind() == LocAsIntegerKind;
451
0
  }
452
};
453
454
class CompoundVal : public NonLoc {
455
  friend class ento::SValBuilder;
456
457
1.05k
  explicit CompoundVal(const CompoundValData* D) : NonLoc(CompoundValKind, D) {}
458
459
public:
460
6.06k
  const CompoundValData* getValue() const {
461
6.06k
    return static_cast<const CompoundValData *>(Data);
462
6.06k
  }
463
464
  using iterator = llvm::ImmutableList<SVal>::iterator;
465
466
  iterator begin() const;
467
  iterator end() const;
468
469
private:
470
  friend class SVal;
471
472
  CompoundVal() = default;
473
474
297k
  static bool isKind(const SVal& V) {
475
297k
    return V.getBaseKind() == NonLocKind && 
V.getSubKind() == CompoundValKind216k
;
476
297k
  }
477
478
0
  static bool isKind(const NonLoc& V) {
479
0
    return V.getSubKind() == CompoundValKind;
480
0
  }
481
};
482
483
class LazyCompoundVal : public NonLoc {
484
  friend class ento::SValBuilder;
485
486
  explicit LazyCompoundVal(const LazyCompoundValData *D)
487
38.9k
      : NonLoc(LazyCompoundValKind, D) {}
488
489
public:
490
54.2k
  const LazyCompoundValData *getCVData() const {
491
54.2k
    return static_cast<const LazyCompoundValData *>(Data);
492
54.2k
  }
493
494
  const void *getStore() const;
495
  const TypedValueRegion *getRegion() const;
496
497
private:
498
  friend class SVal;
499
500
  LazyCompoundVal() = default;
501
502
2.23M
  static bool isKind(const SVal& V) {
503
2.23M
    return V.getBaseKind() == NonLocKind &&
504
2.23M
           
V.getSubKind() == LazyCompoundValKind1.64M
;
505
2.23M
  }
506
507
0
  static bool isKind(const NonLoc& V) {
508
0
    return V.getSubKind() == LazyCompoundValKind;
509
0
  }
510
};
511
512
/// Value representing pointer-to-member.
513
///
514
/// This value is qualified as NonLoc because neither loading nor storing
515
/// operations are applied to it. Instead, the analyzer uses the L-value coming
516
/// from pointer-to-member applied to an object.
517
/// This SVal is represented by a DeclaratorDecl which can be a member function
518
/// pointer or a member data pointer and a list of CXXBaseSpecifiers. This list
519
/// is required to accumulate the pointer-to-member cast history to figure out
520
/// the correct subobject field.
521
class PointerToMember : public NonLoc {
522
  friend class ento::SValBuilder;
523
524
public:
525
  using PTMDataType =
526
      llvm::PointerUnion<const DeclaratorDecl *, const PointerToMemberData *>;
527
528
266
  const PTMDataType getPTMData() const {
529
266
    return PTMDataType::getFromOpaqueValue(const_cast<void *>(Data));
530
266
  }
531
532
  bool isNullMemberPointer() const;
533
534
  const DeclaratorDecl *getDecl() const;
535
536
  template<typename AdjustedDecl>
537
21
  const AdjustedDecl *getDeclAs() const {
538
21
    return dyn_cast_or_null<AdjustedDecl>(getDecl());
539
21
  }
540
541
  using iterator = llvm::ImmutableList<const CXXBaseSpecifier *>::iterator;
542
543
  iterator begin() const;
544
  iterator end() const;
545
546
private:
547
  friend class SVal;
548
549
  PointerToMember() = default;
550
  explicit PointerToMember(const PTMDataType D)
551
65
      : NonLoc(PointerToMemberKind, D.getOpaqueValue()) {}
552
553
168k
  static bool isKind(const SVal& V) {
554
168k
    return V.getBaseKind() == NonLocKind &&
555
168k
           
V.getSubKind() == PointerToMemberKind148k
;
556
168k
  }
557
558
0
  static bool isKind(const NonLoc& V) {
559
0
    return V.getSubKind() == PointerToMemberKind;
560
0
  }
561
};
562
563
} // namespace nonloc
564
565
//==------------------------------------------------------------------------==//
566
//  Subclasses of Loc.
567
//==------------------------------------------------------------------------==//
568
569
namespace loc {
570
571
class GotoLabel : public Loc {
572
public:
573
62
  explicit GotoLabel(const LabelDecl *Label) : Loc(GotoLabelKind, Label) {
574
62
    assert(Label);
575
62
  }
576
577
12
  const LabelDecl *getLabel() const {
578
12
    return static_cast<const LabelDecl *>(Data);
579
12
  }
580
581
private:
582
  friend class SVal;
583
584
  GotoLabel() = default;
585
586
9.75k
  static bool isKind(const SVal& V) {
587
9.75k
    return V.getBaseKind() == LocKind && 
V.getSubKind() == GotoLabelKind9.74k
;
588
9.75k
  }
589
590
0
  static bool isKind(const Loc& V) {
591
0
    return V.getSubKind() == GotoLabelKind;
592
0
  }
593
};
594
595
class MemRegionVal : public Loc {
596
public:
597
691k
  explicit MemRegionVal(const MemRegion* r) : Loc(MemRegionValKind, r) {
598
691k
    assert(r);
599
691k
  }
600
601
  /// Get the underlining region.
602
4.75M
  const MemRegion *getRegion() const {
603
4.75M
    return static_cast<const MemRegion *>(Data);
604
4.75M
  }
605
606
  /// Get the underlining region and strip casts.
607
  const MemRegion* stripCasts(bool StripBaseCasts = true) const;
608
609
  template <typename REGION>
610
10.4k
  const REGION* getRegionAs() const {
611
10.4k
    return dyn_cast<REGION>(getRegion());
612
10.4k
  }
clang::ento::FieldRegion const* clang::ento::loc::MemRegionVal::getRegionAs<clang::ento::FieldRegion>() const
Line
Count
Source
610
642
  const REGION* getRegionAs() const {
611
642
    return dyn_cast<REGION>(getRegion());
612
642
  }
clang::ento::TypedValueRegion const* clang::ento::loc::MemRegionVal::getRegionAs<clang::ento::TypedValueRegion>() const
Line
Count
Source
610
80
  const REGION* getRegionAs() const {
611
80
    return dyn_cast<REGION>(getRegion());
612
80
  }
clang::ento::SubRegion const* clang::ento::loc::MemRegionVal::getRegionAs<clang::ento::SubRegion>() const
Line
Count
Source
610
9.72k
  const REGION* getRegionAs() const {
611
9.72k
    return dyn_cast<REGION>(getRegion());
612
9.72k
  }
613
614
0
  bool operator==(const MemRegionVal &R) const {
615
0
    return getRegion() == R.getRegion();
616
0
  }
617
618
0
  bool operator!=(const MemRegionVal &R) const {
619
0
    return getRegion() != R.getRegion();
620
0
  }
621
622
private:
623
  friend class SVal;
624
625
  MemRegionVal() = default;
626
627
7.22M
  static bool isKind(const SVal& V) {
628
7.22M
    return V.getBaseKind() == LocKind &&
629
7.22M
           
V.getSubKind() == MemRegionValKind4.29M
;
630
7.22M
  }
631
632
0
  static bool isKind(const Loc& V) {
633
0
    return V.getSubKind() == MemRegionValKind;
634
0
  }
635
};
636
637
class ConcreteInt : public Loc {
638
public:
639
8.67k
  explicit ConcreteInt(const llvm::APSInt& V) : Loc(ConcreteIntKind, &V) {}
640
641
8.89k
  const llvm::APSInt &getValue() const {
642
8.89k
    return *static_cast<const llvm::APSInt *>(Data);
643
8.89k
  }
644
645
  // Transfer functions for binary/unary operations on ConcreteInts.
646
  SVal evalBinOp(BasicValueFactory& BasicVals, BinaryOperator::Opcode Op,
647
                 const ConcreteInt& R) const;
648
649
private:
650
  friend class SVal;
651
652
  ConcreteInt() = default;
653
654
1.50M
  static bool isKind(const SVal& V) {
655
1.50M
    return V.getBaseKind() == LocKind &&
656
1.50M
           
V.getSubKind() == ConcreteIntKind1.10M
;
657
1.50M
  }
658
659
0
  static bool isKind(const Loc& V) {
660
0
    return V.getSubKind() == ConcreteIntKind;
661
0
  }
662
};
663
664
} // namespace loc
665
666
} // namespace ento
667
668
} // namespace clang
669
670
#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALS_H