/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h
Line | Count | Source (jump to first uncovered line) |
1 | | //==- BasicValueFactory.h - Basic values for Path Sens analysis --*- C++ -*-==// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file defines BasicValueFactory, a class that manages the lifetime |
10 | | // of APSInt objects and symbolic constraints used by ExprEngine |
11 | | // and related classes. |
12 | | // |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
15 | | #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H |
16 | | #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H |
17 | | |
18 | | #include "clang/AST/ASTContext.h" |
19 | | #include "clang/AST/Expr.h" |
20 | | #include "clang/AST/Type.h" |
21 | | #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h" |
22 | | #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" |
23 | | #include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h" |
24 | | #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" |
25 | | #include "llvm/ADT/APSInt.h" |
26 | | #include "llvm/ADT/FoldingSet.h" |
27 | | #include "llvm/ADT/ImmutableList.h" |
28 | | #include "llvm/ADT/iterator_range.h" |
29 | | #include "llvm/Support/Allocator.h" |
30 | | #include <cassert> |
31 | | #include <cstdint> |
32 | | #include <utility> |
33 | | |
34 | | namespace clang { |
35 | | |
36 | | class CXXBaseSpecifier; |
37 | | class DeclaratorDecl; |
38 | | |
39 | | namespace ento { |
40 | | |
41 | | class CompoundValData : public llvm::FoldingSetNode { |
42 | | QualType T; |
43 | | llvm::ImmutableList<SVal> L; |
44 | | |
45 | | public: |
46 | 885 | CompoundValData(QualType t, llvm::ImmutableList<SVal> l) : T(t), L(l) { |
47 | 885 | assert(NonLoc::isCompoundType(t)); |
48 | 885 | } |
49 | | |
50 | | using iterator = llvm::ImmutableList<SVal>::iterator; |
51 | | |
52 | 3.18k | iterator begin() const { return L.begin(); } |
53 | 3.17k | iterator end() const { return L.end(); } |
54 | | |
55 | | static void Profile(llvm::FoldingSetNodeID& ID, QualType T, |
56 | | llvm::ImmutableList<SVal> L); |
57 | | |
58 | 239 | void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); } |
59 | | }; |
60 | | |
61 | | class LazyCompoundValData : public llvm::FoldingSetNode { |
62 | | StoreRef store; |
63 | | const TypedValueRegion *region; |
64 | | |
65 | | public: |
66 | | LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r) |
67 | 19.8k | : store(st), region(r) { |
68 | 19.8k | assert(NonLoc::isCompoundType(r->getValueType())); |
69 | 19.8k | } |
70 | | |
71 | 79.0k | const void *getStore() const { return store.getStore(); } |
72 | 101k | const TypedValueRegion *getRegion() const { return region; } |
73 | | |
74 | | static void Profile(llvm::FoldingSetNodeID& ID, |
75 | | const StoreRef &store, |
76 | | const TypedValueRegion *region); |
77 | | |
78 | 39.3k | void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); } |
79 | | }; |
80 | | |
81 | | class PointerToMemberData : public llvm::FoldingSetNode { |
82 | | const NamedDecl *D; |
83 | | llvm::ImmutableList<const CXXBaseSpecifier *> L; |
84 | | |
85 | | public: |
86 | | PointerToMemberData(const NamedDecl *D, |
87 | | llvm::ImmutableList<const CXXBaseSpecifier *> L) |
88 | 20 | : D(D), L(L) {} |
89 | | |
90 | | using iterator = llvm::ImmutableList<const CXXBaseSpecifier *>::iterator; |
91 | | |
92 | 7 | iterator begin() const { return L.begin(); } |
93 | 7 | iterator end() const { return L.end(); } |
94 | | |
95 | | static void Profile(llvm::FoldingSetNodeID &ID, const NamedDecl *D, |
96 | | llvm::ImmutableList<const CXXBaseSpecifier *> L); |
97 | | |
98 | 7 | void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, D, L); } |
99 | 55 | const NamedDecl *getDeclaratorDecl() const { return D; } |
100 | | |
101 | 12 | llvm::ImmutableList<const CXXBaseSpecifier *> getCXXBaseList() const { |
102 | 12 | return L; |
103 | 12 | } |
104 | | }; |
105 | | |
106 | | class BasicValueFactory { |
107 | | using APSIntSetTy = |
108 | | llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt>>; |
109 | | |
110 | | ASTContext &Ctx; |
111 | | llvm::BumpPtrAllocator& BPAlloc; |
112 | | |
113 | | APSIntSetTy APSIntSet; |
114 | | void *PersistentSVals = nullptr; |
115 | | void *PersistentSValPairs = nullptr; |
116 | | |
117 | | llvm::ImmutableList<SVal>::Factory SValListFactory; |
118 | | llvm::ImmutableList<const CXXBaseSpecifier *>::Factory CXXBaseListFactory; |
119 | | llvm::FoldingSet<CompoundValData> CompoundValDataSet; |
120 | | llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet; |
121 | | llvm::FoldingSet<PointerToMemberData> PointerToMemberDataSet; |
122 | | |
123 | | // This is private because external clients should use the factory |
124 | | // method that takes a QualType. |
125 | | const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned); |
126 | | |
127 | | public: |
128 | | BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc) |
129 | | : Ctx(ctx), BPAlloc(Alloc), SValListFactory(Alloc), |
130 | 13.7k | CXXBaseListFactory(Alloc) {} |
131 | | |
132 | | ~BasicValueFactory(); |
133 | | |
134 | 90 | ASTContext &getContext() const { return Ctx; } |
135 | | |
136 | | const llvm::APSInt& getValue(const llvm::APSInt& X); |
137 | | const llvm::APSInt& getValue(const llvm::APInt& X, bool isUnsigned); |
138 | | const llvm::APSInt& getValue(uint64_t X, QualType T); |
139 | | |
140 | | /// Returns the type of the APSInt used to store values of the given QualType. |
141 | 855k | APSIntType getAPSIntType(QualType T) const { |
142 | 855k | assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T)); |
143 | 855k | return APSIntType(Ctx.getIntWidth(T), |
144 | 855k | !T->isSignedIntegerOrEnumerationType()); |
145 | 855k | } |
146 | | |
147 | | /// Convert - Create a new persistent APSInt with the same value as 'From' |
148 | | /// but with the bitwidth and signedness of 'To'. |
149 | | const llvm::APSInt &Convert(const llvm::APSInt& To, |
150 | 0 | const llvm::APSInt& From) { |
151 | 0 | APSIntType TargetType(To); |
152 | 0 | if (TargetType == APSIntType(From)) |
153 | 0 | return From; |
154 | 0 |
|
155 | 0 | return getValue(TargetType.convert(From)); |
156 | 0 | } |
157 | | |
158 | 28.3k | const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) { |
159 | 28.3k | APSIntType TargetType = getAPSIntType(T); |
160 | 28.3k | return Convert(TargetType, From); |
161 | 28.3k | } |
162 | | |
163 | 174k | const llvm::APSInt &Convert(APSIntType TargetType, const llvm::APSInt &From) { |
164 | 174k | if (TargetType == APSIntType(From)) |
165 | 100k | return From; |
166 | | |
167 | 73.9k | return getValue(TargetType.convert(From)); |
168 | 73.9k | } |
169 | | |
170 | 454 | const llvm::APSInt &getIntValue(uint64_t X, bool isUnsigned) { |
171 | 354 | QualType T = isUnsigned ? Ctx.UnsignedIntTy : Ctx.IntTy100 ; |
172 | 454 | return getValue(X, T); |
173 | 454 | } |
174 | | |
175 | 30.4k | const llvm::APSInt &getMaxValue(const llvm::APSInt &v) { |
176 | 30.4k | return getValue(APSIntType(v).getMaxValue()); |
177 | 30.4k | } |
178 | | |
179 | 30.4k | const llvm::APSInt &getMinValue(const llvm::APSInt &v) { |
180 | 30.4k | return getValue(APSIntType(v).getMinValue()); |
181 | 30.4k | } |
182 | | |
183 | 114k | const llvm::APSInt &getMaxValue(QualType T) { |
184 | 114k | return getMaxValue(getAPSIntType(T)); |
185 | 114k | } |
186 | | |
187 | 115k | const llvm::APSInt &getMinValue(QualType T) { |
188 | 115k | return getMinValue(getAPSIntType(T)); |
189 | 115k | } |
190 | | |
191 | 114k | const llvm::APSInt &getMaxValue(APSIntType T) { |
192 | 114k | return getValue(T.getMaxValue()); |
193 | 114k | } |
194 | | |
195 | 115k | const llvm::APSInt &getMinValue(APSIntType T) { |
196 | 115k | return getValue(T.getMinValue()); |
197 | 115k | } |
198 | | |
199 | 0 | const llvm::APSInt &Add1(const llvm::APSInt &V) { |
200 | 0 | llvm::APSInt X = V; |
201 | 0 | ++X; |
202 | 0 | return getValue(X); |
203 | 0 | } |
204 | | |
205 | 0 | const llvm::APSInt &Sub1(const llvm::APSInt &V) { |
206 | 0 | llvm::APSInt X = V; |
207 | 0 | --X; |
208 | 0 | return getValue(X); |
209 | 0 | } |
210 | | |
211 | 281 | const llvm::APSInt &getZeroWithTypeSize(QualType T) { |
212 | 281 | assert(T->isScalarType()); |
213 | 281 | return getValue(0, Ctx.getTypeSize(T), true); |
214 | 281 | } |
215 | | |
216 | 7.90k | const llvm::APSInt &getZeroWithPtrWidth(bool isUnsigned = true) { |
217 | 7.90k | return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned); |
218 | 7.90k | } |
219 | | |
220 | 150 | const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) { |
221 | 150 | return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned); |
222 | 150 | } |
223 | | |
224 | 543k | const llvm::APSInt &getTruthValue(bool b, QualType T) { |
225 | 528k | return getValue(b ? 1 : 014.8k , Ctx.getIntWidth(T), |
226 | 543k | T->isUnsignedIntegerOrEnumerationType()); |
227 | 543k | } |
228 | | |
229 | 15.1k | const llvm::APSInt &getTruthValue(bool b) { |
230 | 15.1k | return getTruthValue(b, Ctx.getLogicalOperationType()); |
231 | 15.1k | } |
232 | | |
233 | | const CompoundValData *getCompoundValData(QualType T, |
234 | | llvm::ImmutableList<SVal> Vals); |
235 | | |
236 | | const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store, |
237 | | const TypedValueRegion *region); |
238 | | |
239 | | const PointerToMemberData * |
240 | | getPointerToMemberData(const NamedDecl *ND, |
241 | | llvm::ImmutableList<const CXXBaseSpecifier *> L); |
242 | | |
243 | 1.11k | llvm::ImmutableList<SVal> getEmptySValList() { |
244 | 1.11k | return SValListFactory.getEmptyList(); |
245 | 1.11k | } |
246 | | |
247 | 2.13k | llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) { |
248 | 2.13k | return SValListFactory.add(X, L); |
249 | 2.13k | } |
250 | | |
251 | 0 | llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() { |
252 | 0 | return CXXBaseListFactory.getEmptyList(); |
253 | 0 | } |
254 | | |
255 | | llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase( |
256 | | const CXXBaseSpecifier *CBS, |
257 | 28 | llvm::ImmutableList<const CXXBaseSpecifier *> L) { |
258 | 28 | return CXXBaseListFactory.add(CBS, L); |
259 | 28 | } |
260 | | |
261 | | const PointerToMemberData *accumCXXBase( |
262 | | llvm::iterator_range<CastExpr::path_const_iterator> PathRange, |
263 | | const nonloc::PointerToMember &PTM); |
264 | | |
265 | | const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op, |
266 | | const llvm::APSInt& V1, |
267 | | const llvm::APSInt& V2); |
268 | | |
269 | | const std::pair<SVal, uintptr_t>& |
270 | | getPersistentSValWithData(const SVal& V, uintptr_t Data); |
271 | | |
272 | | const std::pair<SVal, SVal>& |
273 | | getPersistentSValPair(const SVal& V1, const SVal& V2); |
274 | | |
275 | | const SVal* getPersistentSVal(SVal X); |
276 | | }; |
277 | | |
278 | | } // namespace ento |
279 | | |
280 | | } // namespace clang |
281 | | |
282 | | #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H |