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