clang 20.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
19#include "clang/AST/Expr.h"
20#include "clang/AST/Type.h"
26#include "llvm/ADT/APSInt.h"
27#include "llvm/ADT/FoldingSet.h"
28#include "llvm/ADT/ImmutableList.h"
29#include "llvm/ADT/iterator_range.h"
30#include "llvm/Support/Allocator.h"
31#include <cassert>
32#include <cstdint>
33#include <utility>
34
35namespace clang {
36
37class CXXBaseSpecifier;
38
39namespace ento {
40
41class CompoundValData : public llvm::FoldingSetNode {
42 QualType T;
43 llvm::ImmutableList<SVal> L;
44
45public:
46 CompoundValData(QualType t, llvm::ImmutableList<SVal> l) : T(t), L(l) {
47 assert(NonLoc::isCompoundType(t));
48 }
49
50 using iterator = llvm::ImmutableList<SVal>::iterator;
51
52 iterator begin() const { return L.begin(); }
53 iterator end() const { return L.end(); }
54
55 QualType getType() const { return T; }
56
57 static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
58 llvm::ImmutableList<SVal> L);
59
60 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); }
61};
62
63class LazyCompoundValData : public llvm::FoldingSetNode {
64 StoreRef store;
65 const TypedValueRegion *region;
66
67public:
69 : store(st), region(r) {
70 assert(r);
72 }
73
74 /// It might return null.
75 const void *getStore() const { return store.getStore(); }
76
77 LLVM_ATTRIBUTE_RETURNS_NONNULL
78 const TypedValueRegion *getRegion() const { return region; }
79
80 static void Profile(llvm::FoldingSetNodeID& ID,
81 const StoreRef &store,
82 const TypedValueRegion *region);
83
84 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); }
85};
86
87class PointerToMemberData : public llvm::FoldingSetNode {
88 const NamedDecl *D;
89 llvm::ImmutableList<const CXXBaseSpecifier *> L;
90
91public:
93 llvm::ImmutableList<const CXXBaseSpecifier *> L)
94 : D(D), L(L) {}
95
96 using iterator = llvm::ImmutableList<const CXXBaseSpecifier *>::iterator;
97
98 iterator begin() const { return L.begin(); }
99 iterator end() const { return L.end(); }
100
101 static void Profile(llvm::FoldingSetNodeID &ID, const NamedDecl *D,
102 llvm::ImmutableList<const CXXBaseSpecifier *> L);
103
104 void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, D, L); }
105
106 /// It might return null.
107 const NamedDecl *getDeclaratorDecl() const { return D; }
108
109 llvm::ImmutableList<const CXXBaseSpecifier *> getCXXBaseList() const {
110 return L;
111 }
112};
113
115 using APSIntSetTy =
116 llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt>>;
117
118 ASTContext &Ctx;
119 llvm::BumpPtrAllocator& BPAlloc;
120
121 APSIntSetTy APSIntSet;
122 void *PersistentSVals = nullptr;
123 void *PersistentSValPairs = nullptr;
124
125 llvm::ImmutableList<SVal>::Factory SValListFactory;
126 llvm::ImmutableList<const CXXBaseSpecifier *>::Factory CXXBaseListFactory;
127 llvm::FoldingSet<CompoundValData> CompoundValDataSet;
128 llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet;
129 llvm::FoldingSet<PointerToMemberData> PointerToMemberDataSet;
130
131 // This is private because external clients should use the factory
132 // method that takes a QualType.
133 APSIntPtr getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
134
135public:
136 BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
137 : Ctx(ctx), BPAlloc(Alloc), SValListFactory(Alloc),
138 CXXBaseListFactory(Alloc) {}
139
141
142 ASTContext &getContext() const { return Ctx; }
143
144 APSIntPtr getValue(const llvm::APSInt &X);
145 APSIntPtr getValue(const llvm::APInt &X, bool isUnsigned);
146 APSIntPtr getValue(uint64_t X, QualType T);
147
148 /// Returns the type of the APSInt used to store values of the given QualType.
150 // For the purposes of the analysis and constraints, we treat atomics
151 // as their underlying types.
152 if (const AtomicType *AT = T->getAs<AtomicType>()) {
153 T = AT->getValueType();
154 }
155
157 return APSIntType(Ctx.getIntWidth(T),
159 } else {
160 // implicitly handle case of T->isFixedPointType()
162 }
163
164 llvm_unreachable("Unsupported type in getAPSIntType!");
165 }
166
167 /// Convert - Create a new persistent APSInt with the same value as 'From'
168 /// but with the bitwidth and signedness of 'To'.
169 APSIntPtr Convert(const llvm::APSInt &To, const llvm::APSInt &From) {
170 APSIntType TargetType(To);
171 if (TargetType == APSIntType(From))
172 return getValue(From);
173
174 return getValue(TargetType.convert(From));
175 }
176
177 APSIntPtr Convert(QualType T, const llvm::APSInt &From) {
178 APSIntType TargetType = getAPSIntType(T);
179 return Convert(TargetType, From);
180 }
181
182 APSIntPtr Convert(APSIntType TargetType, const llvm::APSInt &From) {
183 if (TargetType == APSIntType(From))
184 return getValue(From);
185
186 return getValue(TargetType.convert(From));
187 }
188
191 return getValue(X, T);
192 }
193
194 APSIntPtr getMaxValue(const llvm::APSInt &v) {
195 return getValue(APSIntType(v).getMaxValue());
196 }
197
198 APSIntPtr getMinValue(const llvm::APSInt &v) {
199 return getValue(APSIntType(v).getMinValue());
200 }
201
203
205
206 APSIntPtr getMaxValue(APSIntType T) { return getValue(T.getMaxValue()); }
207
208 APSIntPtr getMinValue(APSIntType T) { return getValue(T.getMinValue()); }
209
210 APSIntPtr Add1(const llvm::APSInt &V) {
211 llvm::APSInt X = V;
212 ++X;
213 return getValue(X);
214 }
215
216 APSIntPtr Sub1(const llvm::APSInt &V) {
217 llvm::APSInt X = V;
218 --X;
219 return getValue(X);
220 }
221
223 assert(T->isScalarType());
224 return getValue(0, Ctx.getTypeSize(T), true);
225 }
226
228 return getValue(b ? 1 : 0, Ctx.getIntWidth(T),
230 }
231
234 }
235
237 llvm::ImmutableList<SVal> Vals);
238
240 const TypedValueRegion *region);
241
242 const PointerToMemberData *
244 llvm::ImmutableList<const CXXBaseSpecifier *> L);
245
246 llvm::ImmutableList<SVal> getEmptySValList() {
247 return SValListFactory.getEmptyList();
248 }
249
250 llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) {
251 return SValListFactory.add(X, L);
252 }
253
254 llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() {
255 return CXXBaseListFactory.getEmptyList();
256 }
257
258 llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase(
259 const CXXBaseSpecifier *CBS,
260 llvm::ImmutableList<const CXXBaseSpecifier *> L) {
261 return CXXBaseListFactory.add(CBS, L);
262 }
263
264 const PointerToMemberData *
265 accumCXXBase(llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
266 const nonloc::PointerToMember &PTM, const clang::CastKind &kind);
267
268 std::optional<APSIntPtr> evalAPSInt(BinaryOperator::Opcode Op,
269 const llvm::APSInt &V1,
270 const llvm::APSInt &V2);
271
272 const std::pair<SVal, uintptr_t>&
274
275 const std::pair<SVal, SVal>&
276 getPersistentSValPair(const SVal& V1, const SVal& V2);
277
279};
280
281} // namespace ento
282
283} // namespace clang
284
285#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
Defines the clang::ASTContext interface.
#define V(N, I)
Definition: ASTContext.h:3443
static char ID
Definition: Arena.cpp:183
static bool isUnsigned(SValBuilder &SVB, NonLoc Value)
const Decl * D
#define X(type, name)
Definition: Value.h:144
C Language Family Type Representation.
__device__ __2f16 b
do v
Definition: arm_acle.h:91
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
unsigned getIntWidth(QualType T) const
CanQualType IntTy
Definition: ASTContext.h:1169
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2482
CanQualType UnsignedIntTy
Definition: ASTContext.h:1170
QualType getLogicalOperationType() const
The result type of logical operations, '<', '>', '!=', etc.
Definition: ASTContext.h:2132
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
This represents a decl that may have a name.
Definition: Decl.h:253
A (possibly-)qualified type.
Definition: Type.h:929
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
Definition: Type.cpp:2201
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
Definition: Type.cpp:2251
bool isScalarType() const
Definition: Type.h:8609
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:8625
bool isUnsignedFixedPointType() const
Return true if this is a fixed point type that is unsigned according to ISO/IEC JTC1 SC22 WG14 N1169.
Definition: Type.h:8605
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8731
A safe wrapper around APSInt objects allocated and owned by BasicValueFactory.
Definition: APSIntPtr.h:19
A record of the "type" of an APSInt, used for conversions.
Definition: APSIntType.h:19
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
APSIntPtr getMaxValue(const llvm::APSInt &v)
const CompoundValData * getCompoundValData(QualType T, llvm::ImmutableList< SVal > Vals)
const std::pair< SVal, SVal > & getPersistentSValPair(const SVal &V1, const SVal &V2)
APSIntPtr getZeroWithTypeSize(QualType T)
APSIntPtr Add1(const llvm::APSInt &V)
APSIntPtr getMinValue(QualType T)
APSIntPtr getMaxValue(QualType T)
APSIntPtr getIntValue(uint64_t X, bool isUnsigned)
const SVal * getPersistentSVal(SVal X)
BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
APSIntPtr getMaxValue(APSIntType T)
APSIntPtr getMinValue(const llvm::APSInt &v)
const std::pair< SVal, uintptr_t > & getPersistentSValWithData(const SVal &V, uintptr_t Data)
APSIntType getAPSIntType(QualType T) const
Returns the type of the APSInt used to store values of the given QualType.
std::optional< APSIntPtr > evalAPSInt(BinaryOperator::Opcode Op, const llvm::APSInt &V1, const llvm::APSInt &V2)
APSIntPtr getTruthValue(bool b, QualType T)
const PointerToMemberData * getPointerToMemberData(const NamedDecl *ND, llvm::ImmutableList< const CXXBaseSpecifier * > L)
APSIntPtr Convert(QualType T, const llvm::APSInt &From)
llvm::ImmutableList< const CXXBaseSpecifier * > prependCXXBase(const CXXBaseSpecifier *CBS, llvm::ImmutableList< const CXXBaseSpecifier * > L)
llvm::ImmutableList< const CXXBaseSpecifier * > getEmptyCXXBaseList()
APSIntPtr Convert(APSIntType TargetType, const llvm::APSInt &From)
APSIntPtr 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...
const LazyCompoundValData * getLazyCompoundValData(const StoreRef &store, const TypedValueRegion *region)
const PointerToMemberData * accumCXXBase(llvm::iterator_range< CastExpr::path_const_iterator > PathRange, const nonloc::PointerToMember &PTM, const clang::CastKind &kind)
APSIntPtr Sub1(const llvm::APSInt &V)
APSIntPtr getMinValue(APSIntType T)
llvm::ImmutableList< SVal > getEmptySValList()
llvm::ImmutableList< SVal > prependSVal(SVal X, llvm::ImmutableList< SVal > L)
static void Profile(llvm::FoldingSetNodeID &ID, QualType T, llvm::ImmutableList< SVal > L)
llvm::ImmutableList< SVal >::iterator iterator
void Profile(llvm::FoldingSetNodeID &ID)
CompoundValData(QualType t, llvm::ImmutableList< SVal > l)
static void Profile(llvm::FoldingSetNodeID &ID, const StoreRef &store, const TypedValueRegion *region)
LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r)
const void * getStore() const
It might return null.
void Profile(llvm::FoldingSetNodeID &ID)
LLVM_ATTRIBUTE_RETURNS_NONNULL const TypedValueRegion * getRegion() const
static bool isLocType(QualType T)
Definition: SVals.h:262
static bool isCompoundType(QualType T)
Definition: SVals.h:245
llvm::ImmutableList< const CXXBaseSpecifier * > getCXXBaseList() const
void Profile(llvm::FoldingSetNodeID &ID)
llvm::ImmutableList< const CXXBaseSpecifier * >::iterator iterator
const NamedDecl * getDeclaratorDecl() const
It might return null.
static void Profile(llvm::FoldingSetNodeID &ID, const NamedDecl *D, llvm::ImmutableList< const CXXBaseSpecifier * > L)
PointerToMemberData(const NamedDecl *D, llvm::ImmutableList< const CXXBaseSpecifier * > L)
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:56
Store getStore() const
Definition: StoreRef.h:46
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:535
virtual QualType getValueType() const =0
Value representing pointer-to-member.
Definition: SVals.h:434
The JSON file list parser is used to communicate input to InstallAPI.
BinaryOperatorKind
CastKind
CastKind - The kind of operation required for a conversion.
const FunctionProtoType * T
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...