1 //=== BasicValueFactory.h - Basic values for Path Sens analysis --*- C++ -*---//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines BasicValueFactory, a class that manages the lifetime
11 // of APSInt objects and symbolic constraints used by ExprEngine
12 // and related classes.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
17 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
19 #include "clang/AST/ASTContext.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
27 class CompoundValData : public llvm::FoldingSetNode {
29 llvm::ImmutableList<SVal> L;
32 CompoundValData(QualType t, llvm::ImmutableList<SVal> l)
35 typedef llvm::ImmutableList<SVal>::iterator iterator;
36 iterator begin() const { return L.begin(); }
37 iterator end() const { return L.end(); }
39 static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
40 llvm::ImmutableList<SVal> L);
42 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); }
45 class LazyCompoundValData : public llvm::FoldingSetNode {
47 const TypedValueRegion *region;
49 LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r)
50 : store(st), region(r) {}
52 const void *getStore() const { return store.getStore(); }
53 const TypedValueRegion *getRegion() const { return region; }
55 static void Profile(llvm::FoldingSetNodeID& ID,
56 const StoreRef &store,
57 const TypedValueRegion *region);
59 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); }
62 class PointerToMemberData: public llvm::FoldingSetNode {
63 const DeclaratorDecl *D;
64 llvm::ImmutableList<const CXXBaseSpecifier *> L;
67 PointerToMemberData(const DeclaratorDecl *D,
68 llvm::ImmutableList<const CXXBaseSpecifier *> L)
71 typedef llvm::ImmutableList<const CXXBaseSpecifier *>::iterator iterator;
72 iterator begin() const { return L.begin(); }
73 iterator end() const { return L.end(); }
75 static void Profile(llvm::FoldingSetNodeID& ID, const DeclaratorDecl *D,
76 llvm::ImmutableList<const CXXBaseSpecifier *> L);
78 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, D, L); }
79 const DeclaratorDecl *getDeclaratorDecl() const {return D;}
80 llvm::ImmutableList<const CXXBaseSpecifier *> getCXXBaseList() const {
85 class BasicValueFactory {
86 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt> >
90 llvm::BumpPtrAllocator& BPAlloc;
92 APSIntSetTy APSIntSet;
93 void * PersistentSVals;
94 void * PersistentSValPairs;
96 llvm::ImmutableList<SVal>::Factory SValListFactory;
97 llvm::ImmutableList<const CXXBaseSpecifier*>::Factory CXXBaseListFactory;
98 llvm::FoldingSet<CompoundValData> CompoundValDataSet;
99 llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet;
100 llvm::FoldingSet<PointerToMemberData> PointerToMemberDataSet;
102 // This is private because external clients should use the factory
103 // method that takes a QualType.
104 const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
107 BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
108 : Ctx(ctx), BPAlloc(Alloc), PersistentSVals(nullptr),
109 PersistentSValPairs(nullptr), SValListFactory(Alloc),
110 CXXBaseListFactory(Alloc) {}
112 ~BasicValueFactory();
114 ASTContext &getContext() const { return Ctx; }
116 const llvm::APSInt& getValue(const llvm::APSInt& X);
117 const llvm::APSInt& getValue(const llvm::APInt& X, bool isUnsigned);
118 const llvm::APSInt& getValue(uint64_t X, QualType T);
120 /// Returns the type of the APSInt used to store values of the given QualType.
121 APSIntType getAPSIntType(QualType T) const {
122 assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
123 return APSIntType(Ctx.getTypeSize(T),
124 !T->isSignedIntegerOrEnumerationType());
127 /// Convert - Create a new persistent APSInt with the same value as 'From'
128 /// but with the bitwidth and signedness of 'To'.
129 const llvm::APSInt &Convert(const llvm::APSInt& To,
130 const llvm::APSInt& From) {
131 APSIntType TargetType(To);
132 if (TargetType == APSIntType(From))
135 return getValue(TargetType.convert(From));
138 const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
139 APSIntType TargetType = getAPSIntType(T);
140 if (TargetType == APSIntType(From))
143 return getValue(TargetType.convert(From));
146 const llvm::APSInt& getIntValue(uint64_t X, bool isUnsigned) {
147 QualType T = isUnsigned ? Ctx.UnsignedIntTy : Ctx.IntTy;
148 return getValue(X, T);
151 inline const llvm::APSInt& getMaxValue(const llvm::APSInt &v) {
152 return getValue(APSIntType(v).getMaxValue());
155 inline const llvm::APSInt& getMinValue(const llvm::APSInt &v) {
156 return getValue(APSIntType(v).getMinValue());
159 inline const llvm::APSInt& getMaxValue(QualType T) {
160 return getValue(getAPSIntType(T).getMaxValue());
163 inline const llvm::APSInt& getMinValue(QualType T) {
164 return getValue(getAPSIntType(T).getMinValue());
167 inline const llvm::APSInt& Add1(const llvm::APSInt& V) {
173 inline const llvm::APSInt& Sub1(const llvm::APSInt& V) {
179 inline const llvm::APSInt& getZeroWithPtrWidth(bool isUnsigned = true) {
180 return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
183 inline const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) {
184 return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
187 inline const llvm::APSInt& getTruthValue(bool b, QualType T) {
188 return getValue(b ? 1 : 0, Ctx.getTypeSize(T), false);
191 inline const llvm::APSInt& getTruthValue(bool b) {
192 return getTruthValue(b, Ctx.getLogicalOperationType());
195 const CompoundValData *getCompoundValData(QualType T,
196 llvm::ImmutableList<SVal> Vals);
198 const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store,
199 const TypedValueRegion *region);
201 const PointerToMemberData *getPointerToMemberData(
202 const DeclaratorDecl *DD,
203 llvm::ImmutableList<const CXXBaseSpecifier *> L);
205 llvm::ImmutableList<SVal> getEmptySValList() {
206 return SValListFactory.getEmptyList();
209 llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) {
210 return SValListFactory.add(X, L);
213 llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() {
214 return CXXBaseListFactory.getEmptyList();
217 llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase(
218 const CXXBaseSpecifier *CBS,
219 llvm::ImmutableList<const CXXBaseSpecifier *> L) {
220 return CXXBaseListFactory.add(CBS, L);
223 const clang::ento::PointerToMemberData *accumCXXBase(
224 llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
225 const nonloc::PointerToMember &PTM);
227 const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op,
228 const llvm::APSInt& V1,
229 const llvm::APSInt& V2);
231 const std::pair<SVal, uintptr_t>&
232 getPersistentSValWithData(const SVal& V, uintptr_t Data);
234 const std::pair<SVal, SVal>&
235 getPersistentSValPair(const SVal& V1, const SVal& V2);
237 const SVal* getPersistentSVal(SVal X);
240 } // end GR namespace
242 } // end clang namespace