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/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
21 #include "clang/AST/ASTContext.h"
22 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/ADT/APSInt.h"
24 #include "llvm/ADT/ImmutableList.h"
32 class CompoundValData : public llvm::FoldingSetNode {
34 llvm::ImmutableList<SVal> L;
37 CompoundValData(QualType t, llvm::ImmutableList<SVal> l)
40 typedef llvm::ImmutableList<SVal>::iterator iterator;
41 iterator begin() const { return L.begin(); }
42 iterator end() const { return L.end(); }
44 static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
45 llvm::ImmutableList<SVal> L);
47 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); }
50 class LazyCompoundValData : public llvm::FoldingSetNode {
52 const TypedValueRegion *region;
54 LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r)
55 : store(st), region(r) {}
57 const void *getStore() const { return store.getStore(); }
58 const TypedValueRegion *getRegion() const { return region; }
60 static void Profile(llvm::FoldingSetNodeID& ID,
61 const StoreRef &store,
62 const TypedValueRegion *region);
64 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); }
67 class BasicValueFactory {
68 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt> >
72 llvm::BumpPtrAllocator& BPAlloc;
74 APSIntSetTy APSIntSet;
75 void * PersistentSVals;
76 void * PersistentSValPairs;
78 llvm::ImmutableList<SVal>::Factory SValListFactory;
79 llvm::FoldingSet<CompoundValData> CompoundValDataSet;
80 llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet;
83 BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator& Alloc)
84 : Ctx(ctx), BPAlloc(Alloc), PersistentSVals(0), PersistentSValPairs(0),
85 SValListFactory(Alloc) {}
89 ASTContext &getContext() const { return Ctx; }
91 const llvm::APSInt& getValue(const llvm::APSInt& X);
92 const llvm::APSInt& getValue(const llvm::APInt& X, bool isUnsigned);
93 const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
94 const llvm::APSInt& getValue(uint64_t X, QualType T);
96 /// Convert - Create a new persistent APSInt with the same value as 'From'
97 /// but with the bitwidth and signedness of 'To'.
98 const llvm::APSInt &Convert(const llvm::APSInt& To,
99 const llvm::APSInt& From) {
101 if (To.isUnsigned() == From.isUnsigned() &&
102 To.getBitWidth() == From.getBitWidth())
105 return getValue(From.getSExtValue(), To.getBitWidth(), To.isUnsigned());
108 const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
109 assert(T->isIntegerType() || Loc::isLocType(T));
110 unsigned bitwidth = Ctx.getTypeSize(T);
112 = T->isUnsignedIntegerOrEnumerationType() || Loc::isLocType(T);
114 if (isUnsigned == From.isUnsigned() && bitwidth == From.getBitWidth())
117 return getValue(From.getSExtValue(), bitwidth, isUnsigned);
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(llvm::APSInt::getMaxValue(v.getBitWidth(), v.isUnsigned()));
129 inline const llvm::APSInt& getMinValue(const llvm::APSInt &v) {
130 return getValue(llvm::APSInt::getMinValue(v.getBitWidth(), v.isUnsigned()));
133 inline const llvm::APSInt& getMaxValue(QualType T) {
134 assert(T->isIntegerType() || Loc::isLocType(T));
136 = T->isUnsignedIntegerOrEnumerationType() || Loc::isLocType(T);
137 return getValue(llvm::APSInt::getMaxValue(Ctx.getTypeSize(T), isUnsigned));
140 inline const llvm::APSInt& getMinValue(QualType T) {
141 assert(T->isIntegerType() || Loc::isLocType(T));
143 = T->isUnsignedIntegerOrEnumerationType() || Loc::isLocType(T);
144 return getValue(llvm::APSInt::getMinValue(Ctx.getTypeSize(T), isUnsigned));
147 inline const llvm::APSInt& Add1(const llvm::APSInt& V) {
153 inline const llvm::APSInt& Sub1(const llvm::APSInt& V) {
159 inline const llvm::APSInt& getZeroWithPtrWidth(bool isUnsigned = true) {
160 return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
163 inline const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) {
164 return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
167 inline const llvm::APSInt& getTruthValue(bool b, QualType T) {
168 return getValue(b ? 1 : 0, Ctx.getTypeSize(T), false);
171 inline const llvm::APSInt& getTruthValue(bool b) {
172 return getTruthValue(b, Ctx.getLogicalOperationType());
175 const CompoundValData *getCompoundValData(QualType T,
176 llvm::ImmutableList<SVal> Vals);
178 const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store,
179 const TypedValueRegion *region);
181 llvm::ImmutableList<SVal> getEmptySValList() {
182 return SValListFactory.getEmptyList();
185 llvm::ImmutableList<SVal> consVals(SVal X, llvm::ImmutableList<SVal> L) {
186 return SValListFactory.add(X, L);
189 const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op,
190 const llvm::APSInt& V1,
191 const llvm::APSInt& V2);
193 const std::pair<SVal, uintptr_t>&
194 getPersistentSValWithData(const SVal& V, uintptr_t Data);
196 const std::pair<SVal, SVal>&
197 getPersistentSValPair(const SVal& V1, const SVal& V2);
199 const SVal* getPersistentSVal(SVal X);
202 } // end GR namespace
204 } // end clang namespace