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_GR_BASICVALUEFACTORY_H
17 #define LLVM_CLANG_GR_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 BasicValueFactory {
63 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt> >
67 llvm::BumpPtrAllocator& BPAlloc;
69 APSIntSetTy APSIntSet;
70 void * PersistentSVals;
71 void * PersistentSValPairs;
73 llvm::ImmutableList<SVal>::Factory SValListFactory;
74 llvm::FoldingSet<CompoundValData> CompoundValDataSet;
75 llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet;
77 // This is private because external clients should use the factory
78 // method that takes a QualType.
79 const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
82 BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator& Alloc)
83 : Ctx(ctx), BPAlloc(Alloc), PersistentSVals(0), PersistentSValPairs(0),
84 SValListFactory(Alloc) {}
88 ASTContext &getContext() const { return Ctx; }
90 const llvm::APSInt& getValue(const llvm::APSInt& X);
91 const llvm::APSInt& getValue(const llvm::APInt& X, bool isUnsigned);
92 const llvm::APSInt& getValue(uint64_t X, QualType T);
94 /// Returns the type of the APSInt used to store values of the given QualType.
95 APSIntType getAPSIntType(QualType T) const {
96 assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
97 return APSIntType(Ctx.getTypeSize(T),
98 !T->isSignedIntegerOrEnumerationType());
101 /// Convert - Create a new persistent APSInt with the same value as 'From'
102 /// but with the bitwidth and signedness of 'To'.
103 const llvm::APSInt &Convert(const llvm::APSInt& To,
104 const llvm::APSInt& From) {
105 APSIntType TargetType(To);
106 if (TargetType == APSIntType(From))
109 return getValue(TargetType.convert(From));
112 const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
113 APSIntType TargetType = getAPSIntType(T);
114 if (TargetType == APSIntType(From))
117 return getValue(TargetType.convert(From));
120 const llvm::APSInt& getIntValue(uint64_t X, bool isUnsigned) {
121 QualType T = isUnsigned ? Ctx.UnsignedIntTy : Ctx.IntTy;
122 return getValue(X, T);
125 inline const llvm::APSInt& getMaxValue(const llvm::APSInt &v) {
126 return getValue(APSIntType(v).getMaxValue());
129 inline const llvm::APSInt& getMinValue(const llvm::APSInt &v) {
130 return getValue(APSIntType(v).getMinValue());
133 inline const llvm::APSInt& getMaxValue(QualType T) {
134 return getValue(getAPSIntType(T).getMaxValue());
137 inline const llvm::APSInt& getMinValue(QualType T) {
138 return getValue(getAPSIntType(T).getMinValue());
141 inline const llvm::APSInt& Add1(const llvm::APSInt& V) {
147 inline const llvm::APSInt& Sub1(const llvm::APSInt& V) {
153 inline const llvm::APSInt& getZeroWithPtrWidth(bool isUnsigned = true) {
154 return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
157 inline const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) {
158 return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
161 inline const llvm::APSInt& getTruthValue(bool b, QualType T) {
162 return getValue(b ? 1 : 0, Ctx.getTypeSize(T), false);
165 inline const llvm::APSInt& getTruthValue(bool b) {
166 return getTruthValue(b, Ctx.getLogicalOperationType());
169 const CompoundValData *getCompoundValData(QualType T,
170 llvm::ImmutableList<SVal> Vals);
172 const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store,
173 const TypedValueRegion *region);
175 llvm::ImmutableList<SVal> getEmptySValList() {
176 return SValListFactory.getEmptyList();
179 llvm::ImmutableList<SVal> consVals(SVal X, llvm::ImmutableList<SVal> L) {
180 return SValListFactory.add(X, L);
183 const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op,
184 const llvm::APSInt& V1,
185 const llvm::APSInt& V2);
187 const std::pair<SVal, uintptr_t>&
188 getPersistentSValWithData(const SVal& V, uintptr_t Data);
190 const std::pair<SVal, SVal>&
191 getPersistentSValPair(const SVal& V1, const SVal& V2);
193 const SVal* getPersistentSVal(SVal X);
196 } // end GR namespace
198 } // end clang namespace