1 //==- BasicValueFactory.h - Basic values for Path Sens analysis --*- C++ -*-==//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
16 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/Type.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.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"
36 class CXXBaseSpecifier;
41 class CompoundValData : public llvm::FoldingSetNode {
43 llvm::ImmutableList<SVal> L;
46 CompoundValData(QualType t, llvm::ImmutableList<SVal> l) : T(t), L(l) {
47 assert(NonLoc::isCompoundType(t));
50 using iterator = llvm::ImmutableList<SVal>::iterator;
52 iterator begin() const { return L.begin(); }
53 iterator end() const { return L.end(); }
55 static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
56 llvm::ImmutableList<SVal> L);
58 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); }
61 class LazyCompoundValData : public llvm::FoldingSetNode {
63 const TypedValueRegion *region;
66 LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r)
67 : store(st), region(r) {
68 assert(NonLoc::isCompoundType(r->getValueType()));
71 const void *getStore() const { return store.getStore(); }
72 const TypedValueRegion *getRegion() const { return region; }
74 static void Profile(llvm::FoldingSetNodeID& ID,
75 const StoreRef &store,
76 const TypedValueRegion *region);
78 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); }
81 class PointerToMemberData : public llvm::FoldingSetNode {
82 const DeclaratorDecl *D;
83 llvm::ImmutableList<const CXXBaseSpecifier *> L;
86 PointerToMemberData(const DeclaratorDecl *D,
87 llvm::ImmutableList<const CXXBaseSpecifier *> L)
90 using iterator = llvm::ImmutableList<const CXXBaseSpecifier *>::iterator;
92 iterator begin() const { return L.begin(); }
93 iterator end() const { return L.end(); }
95 static void Profile(llvm::FoldingSetNodeID& ID, const DeclaratorDecl *D,
96 llvm::ImmutableList<const CXXBaseSpecifier *> L);
98 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, D, L); }
99 const DeclaratorDecl *getDeclaratorDecl() const {return D;}
101 llvm::ImmutableList<const CXXBaseSpecifier *> getCXXBaseList() const {
106 class BasicValueFactory {
108 llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt>>;
111 llvm::BumpPtrAllocator& BPAlloc;
113 APSIntSetTy APSIntSet;
114 void *PersistentSVals = nullptr;
115 void *PersistentSValPairs = nullptr;
117 llvm::ImmutableList<SVal>::Factory SValListFactory;
118 llvm::ImmutableList<const CXXBaseSpecifier *>::Factory CXXBaseListFactory;
119 llvm::FoldingSet<CompoundValData> CompoundValDataSet;
120 llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet;
121 llvm::FoldingSet<PointerToMemberData> PointerToMemberDataSet;
123 // This is private because external clients should use the factory
124 // method that takes a QualType.
125 const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
128 BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
129 : Ctx(ctx), BPAlloc(Alloc), SValListFactory(Alloc),
130 CXXBaseListFactory(Alloc) {}
132 ~BasicValueFactory();
134 ASTContext &getContext() const { return Ctx; }
136 const llvm::APSInt& getValue(const llvm::APSInt& X);
137 const llvm::APSInt& getValue(const llvm::APInt& X, bool isUnsigned);
138 const llvm::APSInt& getValue(uint64_t X, QualType T);
140 /// Returns the type of the APSInt used to store values of the given QualType.
141 APSIntType getAPSIntType(QualType T) const {
142 assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
143 return APSIntType(Ctx.getIntWidth(T),
144 !T->isSignedIntegerOrEnumerationType());
147 /// Convert - Create a new persistent APSInt with the same value as 'From'
148 /// but with the bitwidth and signedness of 'To'.
149 const llvm::APSInt &Convert(const llvm::APSInt& To,
150 const llvm::APSInt& From) {
151 APSIntType TargetType(To);
152 if (TargetType == APSIntType(From))
155 return getValue(TargetType.convert(From));
158 const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
159 APSIntType TargetType = getAPSIntType(T);
160 if (TargetType == APSIntType(From))
163 return getValue(TargetType.convert(From));
166 const llvm::APSInt &getIntValue(uint64_t X, bool isUnsigned) {
167 QualType T = isUnsigned ? Ctx.UnsignedIntTy : Ctx.IntTy;
168 return getValue(X, T);
171 const llvm::APSInt &getMaxValue(const llvm::APSInt &v) {
172 return getValue(APSIntType(v).getMaxValue());
175 const llvm::APSInt &getMinValue(const llvm::APSInt &v) {
176 return getValue(APSIntType(v).getMinValue());
179 const llvm::APSInt &getMaxValue(QualType T) {
180 return getValue(getAPSIntType(T).getMaxValue());
183 const llvm::APSInt &getMinValue(QualType T) {
184 return getValue(getAPSIntType(T).getMinValue());
187 const llvm::APSInt &Add1(const llvm::APSInt &V) {
193 const llvm::APSInt &Sub1(const llvm::APSInt &V) {
199 const llvm::APSInt &getZeroWithTypeSize(QualType T) {
200 assert(T->isScalarType());
201 return getValue(0, Ctx.getTypeSize(T), true);
204 const llvm::APSInt &getZeroWithPtrWidth(bool isUnsigned = true) {
205 return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
208 const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) {
209 return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
212 const llvm::APSInt &getTruthValue(bool b, QualType T) {
213 return getValue(b ? 1 : 0, Ctx.getIntWidth(T),
214 T->isUnsignedIntegerOrEnumerationType());
217 const llvm::APSInt &getTruthValue(bool b) {
218 return getTruthValue(b, Ctx.getLogicalOperationType());
221 const CompoundValData *getCompoundValData(QualType T,
222 llvm::ImmutableList<SVal> Vals);
224 const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store,
225 const TypedValueRegion *region);
227 const PointerToMemberData *getPointerToMemberData(
228 const DeclaratorDecl *DD,
229 llvm::ImmutableList<const CXXBaseSpecifier *> L);
231 llvm::ImmutableList<SVal> getEmptySValList() {
232 return SValListFactory.getEmptyList();
235 llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) {
236 return SValListFactory.add(X, L);
239 llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() {
240 return CXXBaseListFactory.getEmptyList();
243 llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase(
244 const CXXBaseSpecifier *CBS,
245 llvm::ImmutableList<const CXXBaseSpecifier *> L) {
246 return CXXBaseListFactory.add(CBS, L);
249 const PointerToMemberData *accumCXXBase(
250 llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
251 const nonloc::PointerToMember &PTM);
253 const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op,
254 const llvm::APSInt& V1,
255 const llvm::APSInt& V2);
257 const std::pair<SVal, uintptr_t>&
258 getPersistentSValWithData(const SVal& V, uintptr_t Data);
260 const std::pair<SVal, SVal>&
261 getPersistentSValPair(const SVal& V1, const SVal& V2);
263 const SVal* getPersistentSVal(SVal X);
270 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H