]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r302069, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / StaticAnalyzer / Core / PathSensitive / BasicValueFactory.h
1 //=== BasicValueFactory.h - Basic values for Path Sens analysis --*- C++ -*---//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
17 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
18
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"
24
25 namespace clang {
26 namespace ento {
27
28 class CompoundValData : public llvm::FoldingSetNode {
29   QualType T;
30   llvm::ImmutableList<SVal> L;
31
32 public:
33   CompoundValData(QualType t, llvm::ImmutableList<SVal> l) : T(t), L(l) {
34     assert(NonLoc::isCompoundType(t));
35   }
36
37   typedef llvm::ImmutableList<SVal>::iterator iterator;
38   iterator begin() const { return L.begin(); }
39   iterator end() const { return L.end(); }
40
41   static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
42                       llvm::ImmutableList<SVal> L);
43
44   void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); }
45 };
46
47 class LazyCompoundValData : public llvm::FoldingSetNode {
48   StoreRef store;
49   const TypedValueRegion *region;
50 public:
51   LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r)
52       : store(st), region(r) {
53     assert(NonLoc::isCompoundType(r->getValueType()));
54   }
55
56   const void *getStore() const { return store.getStore(); }
57   const TypedValueRegion *getRegion() const { return region; }
58
59   static void Profile(llvm::FoldingSetNodeID& ID,
60                       const StoreRef &store,
61                       const TypedValueRegion *region);
62
63   void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); }
64 };
65
66 class PointerToMemberData: public llvm::FoldingSetNode {
67   const DeclaratorDecl *D;
68   llvm::ImmutableList<const CXXBaseSpecifier *> L;
69
70 public:
71   PointerToMemberData(const DeclaratorDecl *D,
72                       llvm::ImmutableList<const CXXBaseSpecifier *> L)
73     : D(D), L(L) {}
74
75   typedef llvm::ImmutableList<const CXXBaseSpecifier *>::iterator iterator;
76   iterator begin() const { return L.begin(); }
77   iterator end() const { return L.end(); }
78
79   static void Profile(llvm::FoldingSetNodeID& ID, const DeclaratorDecl *D,
80                       llvm::ImmutableList<const CXXBaseSpecifier *> L);
81
82   void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, D, L); }
83   const DeclaratorDecl *getDeclaratorDecl() const {return D;}
84   llvm::ImmutableList<const CXXBaseSpecifier *> getCXXBaseList() const {
85     return L;
86   }
87 };
88
89 class BasicValueFactory {
90   typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt> >
91           APSIntSetTy;
92
93   ASTContext &Ctx;
94   llvm::BumpPtrAllocator& BPAlloc;
95
96   APSIntSetTy   APSIntSet;
97   void *        PersistentSVals;
98   void *        PersistentSValPairs;
99
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;
105
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);
109
110 public:
111   BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
112     : Ctx(ctx), BPAlloc(Alloc), PersistentSVals(nullptr),
113       PersistentSValPairs(nullptr), SValListFactory(Alloc),
114       CXXBaseListFactory(Alloc) {}
115
116   ~BasicValueFactory();
117
118   ASTContext &getContext() const { return Ctx; }
119
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);
123
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());
129   }
130
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))
137       return From;
138
139     return getValue(TargetType.convert(From));
140   }
141   
142   const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
143     APSIntType TargetType = getAPSIntType(T);
144     if (TargetType == APSIntType(From))
145       return From;
146     
147     return getValue(TargetType.convert(From));
148   }
149
150   const llvm::APSInt& getIntValue(uint64_t X, bool isUnsigned) {
151     QualType T = isUnsigned ? Ctx.UnsignedIntTy : Ctx.IntTy;
152     return getValue(X, T);
153   }
154
155   inline const llvm::APSInt& getMaxValue(const llvm::APSInt &v) {
156     return getValue(APSIntType(v).getMaxValue());
157   }
158
159   inline const llvm::APSInt& getMinValue(const llvm::APSInt &v) {
160     return getValue(APSIntType(v).getMinValue());
161   }
162
163   inline const llvm::APSInt& getMaxValue(QualType T) {
164     return getValue(getAPSIntType(T).getMaxValue());
165   }
166
167   inline const llvm::APSInt& getMinValue(QualType T) {
168     return getValue(getAPSIntType(T).getMinValue());
169   }
170
171   inline const llvm::APSInt& Add1(const llvm::APSInt& V) {
172     llvm::APSInt X = V;
173     ++X;
174     return getValue(X);
175   }
176
177   inline const llvm::APSInt& Sub1(const llvm::APSInt& V) {
178     llvm::APSInt X = V;
179     --X;
180     return getValue(X);
181   }
182
183   inline const llvm::APSInt& getZeroWithPtrWidth(bool isUnsigned = true) {
184     return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
185   }
186
187   inline const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) {
188     return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
189   }
190
191   inline const llvm::APSInt& getTruthValue(bool b, QualType T) {
192     return getValue(b ? 1 : 0, Ctx.getTypeSize(T), false);
193   }
194
195   inline const llvm::APSInt& getTruthValue(bool b) {
196     return getTruthValue(b, Ctx.getLogicalOperationType());
197   }
198
199   const CompoundValData *getCompoundValData(QualType T,
200                                             llvm::ImmutableList<SVal> Vals);
201
202   const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store,
203                                             const TypedValueRegion *region);
204
205   const PointerToMemberData *getPointerToMemberData(
206       const DeclaratorDecl *DD,
207       llvm::ImmutableList<const CXXBaseSpecifier *> L);
208
209   llvm::ImmutableList<SVal> getEmptySValList() {
210     return SValListFactory.getEmptyList();
211   }
212
213   llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) {
214     return SValListFactory.add(X, L);
215   }
216
217   llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() {
218     return CXXBaseListFactory.getEmptyList();
219   }
220
221   llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase(
222       const CXXBaseSpecifier *CBS,
223       llvm::ImmutableList<const CXXBaseSpecifier *> L) {
224     return CXXBaseListFactory.add(CBS, L);
225   }
226
227   const clang::ento::PointerToMemberData *accumCXXBase(
228       llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
229       const nonloc::PointerToMember &PTM);
230
231   const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op,
232                                      const llvm::APSInt& V1,
233                                      const llvm::APSInt& V2);
234
235   const std::pair<SVal, uintptr_t>&
236   getPersistentSValWithData(const SVal& V, uintptr_t Data);
237
238   const std::pair<SVal, SVal>&
239   getPersistentSValPair(const SVal& V1, const SVal& V2);
240
241   const SVal* getPersistentSVal(SVal X);
242 };
243
244 } // end GR namespace
245
246 } // end clang namespace
247
248 #endif