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"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
28 class CompoundValData : public llvm::FoldingSetNode {
30 llvm::ImmutableList<SVal> L;
33 CompoundValData(QualType t, llvm::ImmutableList<SVal> l) : T(t), L(l) {
34 assert(NonLoc::isCompoundType(t));
37 typedef llvm::ImmutableList<SVal>::iterator iterator;
38 iterator begin() const { return L.begin(); }
39 iterator end() const { return L.end(); }
41 static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
42 llvm::ImmutableList<SVal> L);
44 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); }
47 class LazyCompoundValData : public llvm::FoldingSetNode {
49 const TypedValueRegion *region;
51 LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r)
52 : store(st), region(r) {
53 assert(NonLoc::isCompoundType(r->getValueType()));
56 const void *getStore() const { return store.getStore(); }
57 const TypedValueRegion *getRegion() const { return region; }
59 static void Profile(llvm::FoldingSetNodeID& ID,
60 const StoreRef &store,
61 const TypedValueRegion *region);
63 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); }
66 class PointerToMemberData: public llvm::FoldingSetNode {
67 const DeclaratorDecl *D;
68 llvm::ImmutableList<const CXXBaseSpecifier *> L;
71 PointerToMemberData(const DeclaratorDecl *D,
72 llvm::ImmutableList<const CXXBaseSpecifier *> L)
75 typedef llvm::ImmutableList<const CXXBaseSpecifier *>::iterator iterator;
76 iterator begin() const { return L.begin(); }
77 iterator end() const { return L.end(); }
79 static void Profile(llvm::FoldingSetNodeID& ID, const DeclaratorDecl *D,
80 llvm::ImmutableList<const CXXBaseSpecifier *> L);
82 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, D, L); }
83 const DeclaratorDecl *getDeclaratorDecl() const {return D;}
84 llvm::ImmutableList<const CXXBaseSpecifier *> getCXXBaseList() const {
89 class BasicValueFactory {
90 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt> >
94 llvm::BumpPtrAllocator& BPAlloc;
96 APSIntSetTy APSIntSet;
97 void * PersistentSVals;
98 void * PersistentSValPairs;
100 llvm::ImmutableList<SVal>::Factory SValListFactory;
101 llvm::ImmutableList<const CXXBaseSpecifier*>::Factory CXXBaseListFactory;
102 llvm::FoldingSet<CompoundValData> CompoundValDataSet;
103 llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet;
104 llvm::FoldingSet<PointerToMemberData> PointerToMemberDataSet;
106 // This is private because external clients should use the factory
107 // method that takes a QualType.
108 const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
111 BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
112 : Ctx(ctx), BPAlloc(Alloc), PersistentSVals(nullptr),
113 PersistentSValPairs(nullptr), SValListFactory(Alloc),
114 CXXBaseListFactory(Alloc) {}
116 ~BasicValueFactory();
118 ASTContext &getContext() const { return Ctx; }
120 const llvm::APSInt& getValue(const llvm::APSInt& X);
121 const llvm::APSInt& getValue(const llvm::APInt& X, bool isUnsigned);
122 const llvm::APSInt& getValue(uint64_t X, QualType T);
124 /// Returns the type of the APSInt used to store values of the given QualType.
125 APSIntType getAPSIntType(QualType T) const {
126 assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
127 return APSIntType(Ctx.getTypeSize(T),
128 !T->isSignedIntegerOrEnumerationType());
131 /// Convert - Create a new persistent APSInt with the same value as 'From'
132 /// but with the bitwidth and signedness of 'To'.
133 const llvm::APSInt &Convert(const llvm::APSInt& To,
134 const llvm::APSInt& From) {
135 APSIntType TargetType(To);
136 if (TargetType == APSIntType(From))
139 return getValue(TargetType.convert(From));
142 const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
143 APSIntType TargetType = getAPSIntType(T);
144 if (TargetType == APSIntType(From))
147 return getValue(TargetType.convert(From));
150 const llvm::APSInt& getIntValue(uint64_t X, bool isUnsigned) {
151 QualType T = isUnsigned ? Ctx.UnsignedIntTy : Ctx.IntTy;
152 return getValue(X, T);
155 inline const llvm::APSInt& getMaxValue(const llvm::APSInt &v) {
156 return getValue(APSIntType(v).getMaxValue());
159 inline const llvm::APSInt& getMinValue(const llvm::APSInt &v) {
160 return getValue(APSIntType(v).getMinValue());
163 inline const llvm::APSInt& getMaxValue(QualType T) {
164 return getValue(getAPSIntType(T).getMaxValue());
167 inline const llvm::APSInt& getMinValue(QualType T) {
168 return getValue(getAPSIntType(T).getMinValue());
171 inline const llvm::APSInt& Add1(const llvm::APSInt& V) {
177 inline const llvm::APSInt& Sub1(const llvm::APSInt& V) {
183 inline const llvm::APSInt& getZeroWithPtrWidth(bool isUnsigned = true) {
184 return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
187 inline const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) {
188 return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
191 inline const llvm::APSInt& getTruthValue(bool b, QualType T) {
192 return getValue(b ? 1 : 0, Ctx.getTypeSize(T), false);
195 inline const llvm::APSInt& getTruthValue(bool b) {
196 return getTruthValue(b, Ctx.getLogicalOperationType());
199 const CompoundValData *getCompoundValData(QualType T,
200 llvm::ImmutableList<SVal> Vals);
202 const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store,
203 const TypedValueRegion *region);
205 const PointerToMemberData *getPointerToMemberData(
206 const DeclaratorDecl *DD,
207 llvm::ImmutableList<const CXXBaseSpecifier *> L);
209 llvm::ImmutableList<SVal> getEmptySValList() {
210 return SValListFactory.getEmptyList();
213 llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) {
214 return SValListFactory.add(X, L);
217 llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() {
218 return CXXBaseListFactory.getEmptyList();
221 llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase(
222 const CXXBaseSpecifier *CBS,
223 llvm::ImmutableList<const CXXBaseSpecifier *> L) {
224 return CXXBaseListFactory.add(CBS, L);
227 const clang::ento::PointerToMemberData *accumCXXBase(
228 llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
229 const nonloc::PointerToMember &PTM);
231 const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op,
232 const llvm::APSInt& V1,
233 const llvm::APSInt& V2);
235 const std::pair<SVal, uintptr_t>&
236 getPersistentSValWithData(const SVal& V, uintptr_t Data);
238 const std::pair<SVal, SVal>&
239 getPersistentSValPair(const SVal& V1, const SVal& V2);
241 const SVal* getPersistentSVal(SVal X);
244 } // end GR namespace
246 } // end clang namespace