/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 | | |
38 | | namespace ento { |
39 | | |
40 | | class CompoundValData : public llvm::FoldingSetNode { |
41 | | QualType T; |
42 | | llvm::ImmutableList<SVal> L; |
43 | | |
44 | | public: |
45 | 1.01k | CompoundValData(QualType t, llvm::ImmutableList<SVal> l) : T(t), L(l) { |
46 | 1.01k | assert(NonLoc::isCompoundType(t)); |
47 | 1.01k | } |
48 | | |
49 | | using iterator = llvm::ImmutableList<SVal>::iterator; |
50 | | |
51 | 3.53k | iterator begin() const { return L.begin(); } |
52 | 3.52k | iterator end() const { return L.end(); } |
53 | | |
54 | 126 | QualType getType() const { return T; } |
55 | | |
56 | | static void Profile(llvm::FoldingSetNodeID& ID, QualType T, |
57 | | llvm::ImmutableList<SVal> L); |
58 | | |
59 | 260 | void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); } |
60 | | }; |
61 | | |
62 | | class LazyCompoundValData : public llvm::FoldingSetNode { |
63 | | StoreRef store; |
64 | | const TypedValueRegion *region; |
65 | | |
66 | | public: |
67 | | LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r) |
68 | 20.3k | : store(st), region(r) { |
69 | 20.3k | assert(NonLoc::isCompoundType(r->getValueType())); |
70 | 20.3k | } |
71 | | |
72 | 81.3k | const void *getStore() const { return store.getStore(); } |
73 | 104k | const TypedValueRegion *getRegion() const { return region; } |
74 | | |
75 | | static void Profile(llvm::FoldingSetNodeID& ID, |
76 | | const StoreRef &store, |
77 | | const TypedValueRegion *region); |
78 | | |
79 | 43.0k | void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); } |
80 | | }; |
81 | | |
82 | | class PointerToMemberData : public llvm::FoldingSetNode { |
83 | | const NamedDecl *D; |
84 | | llvm::ImmutableList<const CXXBaseSpecifier *> L; |
85 | | |
86 | | public: |
87 | | PointerToMemberData(const NamedDecl *D, |
88 | | llvm::ImmutableList<const CXXBaseSpecifier *> L) |
89 | 23 | : D(D), L(L) {} |
90 | | |
91 | | using iterator = llvm::ImmutableList<const CXXBaseSpecifier *>::iterator; |
92 | | |
93 | 10 | iterator begin() const { return L.begin(); } |
94 | 10 | iterator end() const { return L.end(); } |
95 | | |
96 | | static void Profile(llvm::FoldingSetNodeID &ID, const NamedDecl *D, |
97 | | llvm::ImmutableList<const CXXBaseSpecifier *> L); |
98 | | |
99 | 9 | void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, D, L); } |
100 | 64 | const NamedDecl *getDeclaratorDecl() const { return D; } |
101 | | |
102 | 18 | llvm::ImmutableList<const CXXBaseSpecifier *> getCXXBaseList() const { |
103 | 18 | return L; |
104 | 18 | } |
105 | | }; |
106 | | |
107 | | class BasicValueFactory { |
108 | | using APSIntSetTy = |
109 | | llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt>>; |
110 | | |
111 | | ASTContext &Ctx; |
112 | | llvm::BumpPtrAllocator& BPAlloc; |
113 | | |
114 | | APSIntSetTy APSIntSet; |
115 | | void *PersistentSVals = nullptr; |
116 | | void *PersistentSValPairs = nullptr; |
117 | | |
118 | | llvm::ImmutableList<SVal>::Factory SValListFactory; |
119 | | llvm::ImmutableList<const CXXBaseSpecifier *>::Factory CXXBaseListFactory; |
120 | | llvm::FoldingSet<CompoundValData> CompoundValDataSet; |
121 | | llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet; |
122 | | llvm::FoldingSet<PointerToMemberData> PointerToMemberDataSet; |
123 | | |
124 | | // This is private because external clients should use the factory |
125 | | // method that takes a QualType. |
126 | | const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned); |
127 | | |
128 | | public: |
129 | | BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc) |
130 | | : Ctx(ctx), BPAlloc(Alloc), SValListFactory(Alloc), |
131 | 15.4k | CXXBaseListFactory(Alloc) {} |
132 | | |
133 | | ~BasicValueFactory(); |
134 | | |
135 | 55 | ASTContext &getContext() const { return Ctx; } |
136 | | |
137 | | const llvm::APSInt& getValue(const llvm::APSInt& X); |
138 | | const llvm::APSInt& getValue(const llvm::APInt& X, bool isUnsigned); |
139 | | const llvm::APSInt& getValue(uint64_t X, QualType T); |
140 | | |
141 | | /// Returns the type of the APSInt used to store values of the given QualType. |
142 | 1.07M | APSIntType getAPSIntType(QualType T) const { |
143 | | // For the purposes of the analysis and constraints, we treat atomics |
144 | | // as their underlying types. |
145 | 1.07M | if (const AtomicType *AT = T->getAs<AtomicType>()) { |
146 | 7 | T = AT->getValueType(); |
147 | 7 | } |
148 | | |
149 | 1.07M | assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T)); |
150 | 0 | return APSIntType(Ctx.getIntWidth(T), |
151 | 1.07M | !T->isSignedIntegerOrEnumerationType()); |
152 | 1.07M | } |
153 | | |
154 | | /// Convert - Create a new persistent APSInt with the same value as 'From' |
155 | | /// but with the bitwidth and signedness of 'To'. |
156 | | const llvm::APSInt &Convert(const llvm::APSInt& To, |
157 | 0 | const llvm::APSInt& From) { |
158 | 0 | APSIntType TargetType(To); |
159 | 0 | if (TargetType == APSIntType(From)) |
160 | 0 | return From; |
161 | 0 |
|
162 | 0 | return getValue(TargetType.convert(From)); |
163 | 0 | } |
164 | | |
165 | 28.4k | const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) { |
166 | 28.4k | APSIntType TargetType = getAPSIntType(T); |
167 | 28.4k | return Convert(TargetType, From); |
168 | 28.4k | } |
169 | | |
170 | 177k | const llvm::APSInt &Convert(APSIntType TargetType, const llvm::APSInt &From) { |
171 | 177k | if (TargetType == APSIntType(From)) |
172 | 103k | return From; |
173 | | |
174 | 73.7k | return getValue(TargetType.convert(From)); |
175 | 177k | } |
176 | | |
177 | 929 | const llvm::APSInt &getIntValue(uint64_t X, bool isUnsigned) { |
178 | 929 | QualType T = isUnsigned ? Ctx.UnsignedIntTy616 : Ctx.IntTy313 ; |
179 | 929 | return getValue(X, T); |
180 | 929 | } |
181 | | |
182 | 31.4k | const llvm::APSInt &getMaxValue(const llvm::APSInt &v) { |
183 | 31.4k | return getValue(APSIntType(v).getMaxValue()); |
184 | 31.4k | } |
185 | | |
186 | 31.4k | const llvm::APSInt &getMinValue(const llvm::APSInt &v) { |
187 | 31.4k | return getValue(APSIntType(v).getMinValue()); |
188 | 31.4k | } |
189 | | |
190 | 236k | const llvm::APSInt &getMaxValue(QualType T) { |
191 | 236k | return getMaxValue(getAPSIntType(T)); |
192 | 236k | } |
193 | | |
194 | 236k | const llvm::APSInt &getMinValue(QualType T) { |
195 | 236k | return getMinValue(getAPSIntType(T)); |
196 | 236k | } |
197 | | |
198 | 236k | const llvm::APSInt &getMaxValue(APSIntType T) { |
199 | 236k | return getValue(T.getMaxValue()); |
200 | 236k | } |
201 | | |
202 | 237k | const llvm::APSInt &getMinValue(APSIntType T) { |
203 | 237k | return getValue(T.getMinValue()); |
204 | 237k | } |
205 | | |
206 | 0 | const llvm::APSInt &Add1(const llvm::APSInt &V) { |
207 | 0 | llvm::APSInt X = V; |
208 | 0 | ++X; |
209 | 0 | return getValue(X); |
210 | 0 | } |
211 | | |
212 | 0 | const llvm::APSInt &Sub1(const llvm::APSInt &V) { |
213 | 0 | llvm::APSInt X = V; |
214 | 0 | --X; |
215 | 0 | return getValue(X); |
216 | 0 | } |
217 | | |
218 | 100k | const llvm::APSInt &getZeroWithTypeSize(QualType T) { |
219 | 100k | assert(T->isScalarType()); |
220 | 0 | return getValue(0, Ctx.getTypeSize(T), true); |
221 | 100k | } |
222 | | |
223 | 586k | const llvm::APSInt &getTruthValue(bool b, QualType T) { |
224 | 586k | return getValue(b ? 1568k : 018.6k , Ctx.getIntWidth(T), |
225 | 586k | T->isUnsignedIntegerOrEnumerationType()); |
226 | 586k | } |
227 | | |
228 | 16.9k | const llvm::APSInt &getTruthValue(bool b) { |
229 | 16.9k | return getTruthValue(b, Ctx.getLogicalOperationType()); |
230 | 16.9k | } |
231 | | |
232 | | const CompoundValData *getCompoundValData(QualType T, |
233 | | llvm::ImmutableList<SVal> Vals); |
234 | | |
235 | | const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store, |
236 | | const TypedValueRegion *region); |
237 | | |
238 | | const PointerToMemberData * |
239 | | getPointerToMemberData(const NamedDecl *ND, |
240 | | llvm::ImmutableList<const CXXBaseSpecifier *> L); |
241 | | |
242 | 1.24k | llvm::ImmutableList<SVal> getEmptySValList() { |
243 | 1.24k | return SValListFactory.getEmptyList(); |
244 | 1.24k | } |
245 | | |
246 | 2.35k | llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) { |
247 | 2.35k | return SValListFactory.add(X, L); |
248 | 2.35k | } |
249 | | |
250 | 0 | llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() { |
251 | 0 | return CXXBaseListFactory.getEmptyList(); |
252 | 0 | } |
253 | | |
254 | | llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase( |
255 | | const CXXBaseSpecifier *CBS, |
256 | 31 | llvm::ImmutableList<const CXXBaseSpecifier *> L) { |
257 | 31 | return CXXBaseListFactory.add(CBS, L); |
258 | 31 | } |
259 | | |
260 | | const PointerToMemberData * |
261 | | accumCXXBase(llvm::iterator_range<CastExpr::path_const_iterator> PathRange, |
262 | | const nonloc::PointerToMember &PTM, const clang::CastKind &kind); |
263 | | |
264 | | const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op, |
265 | | const llvm::APSInt& V1, |
266 | | const llvm::APSInt& V2); |
267 | | |
268 | | const std::pair<SVal, uintptr_t>& |
269 | | getPersistentSValWithData(const SVal& V, uintptr_t Data); |
270 | | |
271 | | const std::pair<SVal, SVal>& |
272 | | getPersistentSValPair(const SVal& V1, const SVal& V2); |
273 | | |
274 | | const SVal* getPersistentSVal(SVal X); |
275 | | }; |
276 | | |
277 | | } // namespace ento |
278 | | |
279 | | } // namespace clang |
280 | | |
281 | | #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H |