clang  15.0.0git
BasicValueFactory.h
Go to the documentation of this file.
1 //==- BasicValueFactory.h - Basic values for Path Sens 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 BasicValueFactory, a class that manages the lifetime
10 // of APSInt objects and symbolic constraints used by ExprEngine
11 // and related classes.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
16 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
17 
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/Type.h"
25 #include "llvm/ADT/APSInt.h"
26 #include "llvm/ADT/FoldingSet.h"
27 #include "llvm/ADT/ImmutableList.h"
28 #include "llvm/ADT/iterator_range.h"
29 #include "llvm/Support/Allocator.h"
30 #include <cassert>
31 #include <cstdint>
32 #include <utility>
33 
34 namespace clang {
35 
36 class CXXBaseSpecifier;
37 
38 namespace ento {
39 
40 class CompoundValData : public llvm::FoldingSetNode {
41  QualType T;
42  llvm::ImmutableList<SVal> L;
43 
44 public:
45  CompoundValData(QualType t, llvm::ImmutableList<SVal> l) : T(t), L(l) {
46  assert(NonLoc::isCompoundType(t));
47  }
48 
49  using iterator = llvm::ImmutableList<SVal>::iterator;
50 
51  iterator begin() const { return L.begin(); }
52  iterator end() const { return L.end(); }
53 
54  QualType getType() const { return T; }
55 
56  static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
57  llvm::ImmutableList<SVal> L);
58 
59  void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); }
60 };
61 
62 class LazyCompoundValData : public llvm::FoldingSetNode {
63  StoreRef store;
64  const TypedValueRegion *region;
65 
66 public:
68  : store(st), region(r) {
70  }
71 
72  const void *getStore() const { return store.getStore(); }
73  const TypedValueRegion *getRegion() const { return region; }
74 
75  static void Profile(llvm::FoldingSetNodeID& ID,
76  const StoreRef &store,
77  const TypedValueRegion *region);
78 
79  void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); }
80 };
81 
82 class PointerToMemberData : public llvm::FoldingSetNode {
83  const NamedDecl *D;
84  llvm::ImmutableList<const CXXBaseSpecifier *> L;
85 
86 public:
88  llvm::ImmutableList<const CXXBaseSpecifier *> L)
89  : D(D), L(L) {}
90 
91  using iterator = llvm::ImmutableList<const CXXBaseSpecifier *>::iterator;
92 
93  iterator begin() const { return L.begin(); }
94  iterator end() const { return L.end(); }
95 
96  static void Profile(llvm::FoldingSetNodeID &ID, const NamedDecl *D,
97  llvm::ImmutableList<const CXXBaseSpecifier *> L);
98 
99  void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, D, L); }
100  const NamedDecl *getDeclaratorDecl() const { return D; }
101 
102  llvm::ImmutableList<const CXXBaseSpecifier *> getCXXBaseList() const {
103  return L;
104  }
105 };
106 
108  using APSIntSetTy =
109  llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt>>;
110 
111  ASTContext &Ctx;
112  llvm::BumpPtrAllocator& BPAlloc;
113 
114  APSIntSetTy APSIntSet;
115  void *PersistentSVals = nullptr;
116  void *PersistentSValPairs = nullptr;
117 
118  llvm::ImmutableList<SVal>::Factory SValListFactory;
119  llvm::ImmutableList<const CXXBaseSpecifier *>::Factory CXXBaseListFactory;
120  llvm::FoldingSet<CompoundValData> CompoundValDataSet;
121  llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet;
122  llvm::FoldingSet<PointerToMemberData> PointerToMemberDataSet;
123 
124  // This is private because external clients should use the factory
125  // method that takes a QualType.
126  const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
127 
128 public:
129  BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
130  : Ctx(ctx), BPAlloc(Alloc), SValListFactory(Alloc),
131  CXXBaseListFactory(Alloc) {}
132 
134 
135  ASTContext &getContext() const { return Ctx; }
136 
137  const llvm::APSInt& getValue(const llvm::APSInt& X);
138  const llvm::APSInt& getValue(const llvm::APInt& X, bool isUnsigned);
139  const llvm::APSInt& getValue(uint64_t X, QualType T);
140 
141  /// Returns the type of the APSInt used to store values of the given QualType.
143  // For the purposes of the analysis and constraints, we treat atomics
144  // as their underlying types.
145  if (const AtomicType *AT = T->getAs<AtomicType>()) {
146  T = AT->getValueType();
147  }
148 
149  assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
150  return APSIntType(Ctx.getIntWidth(T),
152  }
153 
154  /// Convert - Create a new persistent APSInt with the same value as 'From'
155  /// but with the bitwidth and signedness of 'To'.
157  const llvm::APSInt& From) {
158  APSIntType TargetType(To);
159  if (TargetType == APSIntType(From))
160  return From;
161 
162  return getValue(TargetType.convert(From));
163  }
164 
165  const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
166  APSIntType TargetType = getAPSIntType(T);
167  return Convert(TargetType, From);
168  }
169 
170  const llvm::APSInt &Convert(APSIntType TargetType, const llvm::APSInt &From) {
171  if (TargetType == APSIntType(From))
172  return From;
173 
174  return getValue(TargetType.convert(From));
175  }
176 
177  const llvm::APSInt &getIntValue(uint64_t X, bool isUnsigned) {
178  QualType T = isUnsigned ? Ctx.UnsignedIntTy : Ctx.IntTy;
179  return getValue(X, T);
180  }
181 
183  return getValue(APSIntType(v).getMaxValue());
184  }
185 
187  return getValue(APSIntType(v).getMinValue());
188  }
189 
191  return getMaxValue(getAPSIntType(T));
192  }
193 
195  return getMinValue(getAPSIntType(T));
196  }
197 
199  return getValue(T.getMaxValue());
200  }
201 
203  return getValue(T.getMinValue());
204  }
205 
206  const llvm::APSInt &Add1(const llvm::APSInt &V) {
207  llvm::APSInt X = V;
208  ++X;
209  return getValue(X);
210  }
211 
212  const llvm::APSInt &Sub1(const llvm::APSInt &V) {
213  llvm::APSInt X = V;
214  --X;
215  return getValue(X);
216  }
217 
219  assert(T->isScalarType());
220  return getValue(0, Ctx.getTypeSize(T), true);
221  }
222 
224  return getValue(b ? 1 : 0, Ctx.getIntWidth(T),
226  }
227 
228  const llvm::APSInt &getTruthValue(bool b) {
229  return getTruthValue(b, Ctx.getLogicalOperationType());
230  }
231 
233  llvm::ImmutableList<SVal> Vals);
234 
236  const TypedValueRegion *region);
237 
238  const PointerToMemberData *
240  llvm::ImmutableList<const CXXBaseSpecifier *> L);
241 
242  llvm::ImmutableList<SVal> getEmptySValList() {
243  return SValListFactory.getEmptyList();
244  }
245 
246  llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) {
247  return SValListFactory.add(X, L);
248  }
249 
250  llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() {
251  return CXXBaseListFactory.getEmptyList();
252  }
253 
254  llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase(
255  const CXXBaseSpecifier *CBS,
256  llvm::ImmutableList<const CXXBaseSpecifier *> L) {
257  return CXXBaseListFactory.add(CBS, L);
258  }
259 
260  const PointerToMemberData *
261  accumCXXBase(llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
262  const nonloc::PointerToMember &PTM, const clang::CastKind &kind);
263 
265  const llvm::APSInt& V1,
266  const llvm::APSInt& V2);
267 
268  const std::pair<SVal, uintptr_t>&
270 
271  const std::pair<SVal, SVal>&
272  getPersistentSValPair(const SVal& V1, const SVal& V2);
273 
274  const SVal* getPersistentSVal(SVal X);
275 };
276 
277 } // namespace ento
278 
279 } // namespace clang
280 
281 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
clang::ento::BasicValueFactory::getPersistentSValWithData
const std::pair< SVal, uintptr_t > & getPersistentSValWithData(const SVal &V, uintptr_t Data)
Definition: BasicValueFactory.cpp:345
clang::ento::BasicValueFactory::getTruthValue
const llvm::APSInt & getTruthValue(bool b)
Definition: BasicValueFactory.h:228
clang::ento::BasicValueFactory::~BasicValueFactory
~BasicValueFactory()
Definition: BasicValueFactory.cpp:79
clang::ento::Loc::isLocType
static bool isLocType(QualType T)
Definition: SVals.h:335
clang::ento::BasicValueFactory::getEmptySValList
llvm::ImmutableList< SVal > getEmptySValList()
Definition: BasicValueFactory.h:242
clang::interp::APInt
llvm::APInt APInt
Definition: Integral.h:27
clang::ento::BasicValueFactory
Definition: BasicValueFactory.h:107
clang::ento::LazyCompoundValData
Definition: BasicValueFactory.h:62
clang::ento::BasicValueFactory::prependCXXBase
llvm::ImmutableList< const CXXBaseSpecifier * > prependCXXBase(const CXXBaseSpecifier *CBS, llvm::ImmutableList< const CXXBaseSpecifier * > L)
Definition: BasicValueFactory.h:254
clang::ento::CompoundValData
Definition: BasicValueFactory.h:40
SVals.h
clang::ento::BasicValueFactory::Add1
const llvm::APSInt & Add1(const llvm::APSInt &V)
Definition: BasicValueFactory.h:206
clang::ento::BasicValueFactory::getMaxValue
const llvm::APSInt & getMaxValue(const llvm::APSInt &v)
Definition: BasicValueFactory.h:182
clang::ASTContext::getIntWidth
unsigned getIntWidth(QualType T) const
Definition: ASTContext.cpp:10684
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:247
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:675
clang::ento::PointerToMemberData::begin
iterator begin() const
Definition: BasicValueFactory.h:93
clang::Type::isScalarType
bool isScalarType() const
Definition: Type.h:7124
clang::ento::BasicValueFactory::Convert
const llvm::APSInt & Convert(APSIntType TargetType, const llvm::APSInt &From)
Definition: BasicValueFactory.h:170
clang::ento::BasicValueFactory::getMinValue
const llvm::APSInt & getMinValue(APSIntType T)
Definition: BasicValueFactory.h:202
clang::ento::CompoundValData::Profile
void Profile(llvm::FoldingSetNodeID &ID)
Definition: BasicValueFactory.h:59
clang::ento::BasicValueFactory::getPersistentSVal
const SVal * getPersistentSVal(SVal X)
Definition: BasicValueFactory.cpp:394
clang::ento::PointerToMemberData::getDeclaratorDecl
const NamedDecl * getDeclaratorDecl() const
Definition: BasicValueFactory.h:100
clang::tooling::X
static ToolExecutorPluginRegistry::Add< AllTUsToolExecutorPlugin > X("all-TUs", "Runs FrontendActions on all TUs in the compilation database. " "Tool results are stored in memory.")
clang::ento::BasicValueFactory::getCompoundValData
const CompoundValData * getCompoundValData(QualType T, llvm::ImmutableList< SVal > Vals)
Definition: BasicValueFactory.cpp:126
clang::ento::PointerToMemberData::Profile
static void Profile(llvm::FoldingSetNodeID &ID, const NamedDecl *D, llvm::ImmutableList< const CXXBaseSpecifier * > L)
Definition: BasicValueFactory.cpp:45
clang::ento::BasicValueFactory::getPointerToMemberData
const PointerToMemberData * getPointerToMemberData(const NamedDecl *ND, llvm::ImmutableList< const CXXBaseSpecifier * > L)
Definition: BasicValueFactory.cpp:162
b
__device__ __2f16 b
Definition: __clang_hip_libdevice_declares.h:314
clang::ento::APSIntType::getMaxValue
llvm::APSInt getMaxValue() const LLVM_READONLY
Returns the maximum value for this type.
Definition: APSIntType.h:65
clang::ento::BasicValueFactory::accumCXXBase
const PointerToMemberData * accumCXXBase(llvm::iterator_range< CastExpr::path_const_iterator > PathRange, const nonloc::PointerToMember &PTM, const clang::CastKind &kind)
Definition: BasicValueFactory.cpp:192
clang::ento::BasicValueFactory::getMinValue
const llvm::APSInt & getMinValue(QualType T)
Definition: BasicValueFactory.h:194
APSInt
llvm::APSInt APSInt
Definition: ByteCodeEmitter.cpp:19
clang::ento::LazyCompoundValData::LazyCompoundValData
LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r)
Definition: BasicValueFactory.h:67
V
#define V(N, I)
Definition: ASTContext.h:3176
clang::ento::StoreRef::getStore
Store getStore() const
Definition: StoreRef.h:46
clang::ento::NonLoc::isCompoundType
static bool isCompoundType(QualType T)
Definition: SVals.h:313
clang::Type::isSignedIntegerOrEnumerationType
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
Definition: Type.cpp:2039
clang::ento::BasicValueFactory::Convert
const llvm::APSInt & Convert(const llvm::APSInt &To, const llvm::APSInt &From)
Convert - Create a new persistent APSInt with the same value as 'From' but with the bitwidth and sign...
Definition: BasicValueFactory.h:156
clang::ento::PointerToMemberData::PointerToMemberData
PointerToMemberData(const NamedDecl *D, llvm::ImmutableList< const CXXBaseSpecifier * > L)
Definition: BasicValueFactory.h:87
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:208
clang::ento::StoreRef
Definition: StoreRef.h:29
clang::Type::getAs
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7243
clang::ento::nonloc::PointerToMember
Value representing pointer-to-member.
Definition: SVals.h:524
clang::ASTContext::getTypeSize
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2285
uintptr_t
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c-base.h:139
Type.h
Expr.h
clang::ento::BasicValueFactory::prependSVal
llvm::ImmutableList< SVal > prependSVal(SVal X, llvm::ImmutableList< SVal > L)
Definition: BasicValueFactory.h:246
ASTContext.h
clang::Type::isUnsignedIntegerOrEnumerationType
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
Definition: Type.cpp:2089
clang::ento::BasicValueFactory::getIntValue
const llvm::APSInt & getIntValue(uint64_t X, bool isUnsigned)
Definition: BasicValueFactory.h:177
clang::ento::CompoundValData::Profile
static void Profile(llvm::FoldingSetNodeID &ID, QualType T, llvm::ImmutableList< SVal > L)
Definition: BasicValueFactory.cpp:32
clang::ento::APSIntType
A record of the "type" of an APSInt, used for conversions.
Definition: APSIntType.h:19
clang::ento::LazyCompoundValData::getRegion
const TypedValueRegion * getRegion() const
Definition: BasicValueFactory.h:73
clang::ento::LazyCompoundValData::Profile
static void Profile(llvm::FoldingSetNodeID &ID, const StoreRef &store, const TypedValueRegion *region)
Definition: BasicValueFactory.cpp:38
clang::ento::BasicValueFactory::getContext
ASTContext & getContext() const
Definition: BasicValueFactory.h:135
clang::ento::APSIntType::getMinValue
llvm::APSInt getMinValue() const LLVM_READONLY
Returns the minimum value for this type.
Definition: APSIntType.h:60
clang::ento::BasicValueFactory::getMaxValue
const llvm::APSInt & getMaxValue(APSIntType T)
Definition: BasicValueFactory.h:198
clang::ASTContext::getLogicalOperationType
QualType getLogicalOperationType() const
The result type of logical operations, '<', '>', '!=', etc.
Definition: ASTContext.h:1969
clang::ento::PointerToMemberData::iterator
llvm::ImmutableList< const CXXBaseSpecifier * >::iterator iterator
Definition: BasicValueFactory.h:91
clang::ASTContext::UnsignedIntTy
CanQualType UnsignedIntTy
Definition: ASTContext.h:1106
clang::ento::LazyCompoundValData::getStore
const void * getStore() const
Definition: BasicValueFactory.h:72
clang::ento::CompoundValData::getType
QualType getType() const
Definition: BasicValueFactory.h:54
clang::ASTContext::IntTy
CanQualType IntTy
Definition: ASTContext.h:1105
clang::ento::BasicValueFactory::getEmptyCXXBaseList
llvm::ImmutableList< const CXXBaseSpecifier * > getEmptyCXXBaseList()
Definition: BasicValueFactory.h:250
clang::ento::BasicValueFactory::BasicValueFactory
BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
Definition: BasicValueFactory.h:129
clang::ento::BasicValueFactory::getLazyCompoundValData
const LazyCompoundValData * getLazyCompoundValData(const StoreRef &store, const TypedValueRegion *region)
Definition: BasicValueFactory.cpp:144
APSIntType.h
clang::BinaryOperatorKind
BinaryOperatorKind
Definition: OperationKinds.h:25
clang::ento::BasicValueFactory::getMinValue
const llvm::APSInt & getMinValue(const llvm::APSInt &v)
Definition: BasicValueFactory.h:186
clang::ento::TypedValueRegion::getValueType
virtual QualType getValueType() const =0
clang::Builtin::ID
ID
Definition: Builtins.h:51
clang
Definition: CalledOnceCheck.h:17
clang::ento::CompoundValData::begin
iterator begin() const
Definition: BasicValueFactory.h:51
clang::ento::PointerToMemberData::getCXXBaseList
llvm::ImmutableList< const CXXBaseSpecifier * > getCXXBaseList() const
Definition: BasicValueFactory.h:102
clang::ento::BasicValueFactory::Sub1
const llvm::APSInt & Sub1(const llvm::APSInt &V)
Definition: BasicValueFactory.h:212
clang::ento::CompoundValData::CompoundValData
CompoundValData(QualType t, llvm::ImmutableList< SVal > l)
Definition: BasicValueFactory.h:45
clang::ento::PointerToMemberData::end
iterator end() const
Definition: BasicValueFactory.h:94
MemRegion.h
clang::CXXBaseSpecifier
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
clang::ento::BasicValueFactory::evalAPSInt
const llvm::APSInt * evalAPSInt(BinaryOperator::Opcode Op, const llvm::APSInt &V1, const llvm::APSInt &V2)
Definition: BasicValueFactory.cpp:250
clang::ento::APSIntType::convert
llvm::APSInt convert(const llvm::APSInt &Value) const LLVM_READONLY
Convert and return a new APSInt with the given value, but this type's bit width and signedness.
Definition: APSIntType.h:48
clang::ento::LazyCompoundValData::Profile
void Profile(llvm::FoldingSetNodeID &ID)
Definition: BasicValueFactory.h:79
clang::ento::BasicValueFactory::Convert
const llvm::APSInt & Convert(QualType T, const llvm::APSInt &From)
Definition: BasicValueFactory.h:165
clang::ento::PointerToMemberData
Definition: BasicValueFactory.h:82
clang::ento::SVal
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:74
v
do v
Definition: arm_acle.h:76
clang::ento::TypedValueRegion
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:522
clang::AtomicType
Definition: Type.h:6318
clang::ento::BasicValueFactory::getMaxValue
const llvm::APSInt & getMaxValue(QualType T)
Definition: BasicValueFactory.h:190
clang::CastKind
CastKind
CastKind - The kind of operation required for a conversion.
Definition: OperationKinds.h:20
clang::ento::CompoundValData::iterator
llvm::ImmutableList< SVal >::iterator iterator
Definition: BasicValueFactory.h:49
StoreRef.h
clang::diag::kind
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:62
clang::ento::CompoundValData::end
iterator end() const
Definition: BasicValueFactory.h:52
clang::ento::BasicValueFactory::getAPSIntType
APSIntType getAPSIntType(QualType T) const
Returns the type of the APSInt used to store values of the given QualType.
Definition: BasicValueFactory.h:142
clang::ento::BasicValueFactory::getZeroWithTypeSize
const llvm::APSInt & getZeroWithTypeSize(QualType T)
Definition: BasicValueFactory.h:218
clang::Type::isIntegralOrEnumerationType
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:7140
clang::ento::BasicValueFactory::getTruthValue
const llvm::APSInt & getTruthValue(bool b, QualType T)
Definition: BasicValueFactory.h:223
clang::ento::PointerToMemberData::Profile
void Profile(llvm::FoldingSetNodeID &ID)
Definition: BasicValueFactory.h:99
clang::ento::BasicValueFactory::getPersistentSValPair
const std::pair< SVal, SVal > & getPersistentSValPair(const SVal &V1, const SVal &V2)
Definition: BasicValueFactory.cpp:370