clang 24.0.0git
BasicValueFactory.cpp
Go to the documentation of this file.
1//===- BasicValueFactory.cpp - Basic values for Path Sens analysis --------===//
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
20#include "llvm/ADT/APSInt.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/ImmutableList.h"
23#include "llvm/ADT/STLExtras.h"
24#include "llvm/ADT/SmallPtrSet.h"
25#include <cassert>
26#include <cstdint>
27#include <utility>
28
29using namespace clang;
30using namespace ento;
31
32void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T,
33 llvm::ImmutableList<SVal> L) {
34 T.Profile(ID);
35 ID.AddPointer(L.getInternalPointer());
36}
37
38void LazyCompoundValData::Profile(llvm::FoldingSetNodeID& ID,
39 const StoreRef &store,
40 const TypedValueRegion *region) {
41 ID.AddPointer(store.getStore());
42 ID.AddPointer(region);
43}
44
46 llvm::FoldingSetNodeID &ID, const NamedDecl *D,
47 llvm::ImmutableList<const CXXBaseSpecifier *> L) {
48 ID.AddPointer(D);
49 ID.AddPointer(L.getInternalPointer());
50}
51
52using SValData = std::pair<SVal, uintptr_t>;
53using SValPair = std::pair<SVal, SVal>;
54
55namespace llvm {
56
57template<> struct FoldingSetTrait<SValData> {
58 static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) {
59 X.first.Profile(ID);
60 ID.AddPointer( (void*) X.second);
61 }
62};
63
64template<> struct FoldingSetTrait<SValPair> {
65 static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) {
66 X.first.Profile(ID);
67 X.second.Profile(ID);
68 }
69};
70
71} // namespace llvm
72
74 llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData>>;
75
77 llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair>>;
78
80 // Note that the dstor for the contents of APSIntSet will never be called,
81 // so we iterate over the set and invoke the dstor for each APSInt. This
82 // frees an aux. memory allocated to represent very large constants.
83 for (const auto &I : APSIntSet)
84 I.getValue().~APSInt();
85
86 delete (PersistentSValsTy*) PersistentSVals;
87 delete (PersistentSValPairsTy*) PersistentSValPairs;
88}
89
90APSIntPtr BasicValueFactory::getValue(const llvm::APSInt &X) {
91 llvm::FoldingSetNodeID ID;
92 llvm::FoldingSetInsertToken InsertToken;
93
94 using FoldNodeTy = llvm::FoldingSetNodeWrapper<llvm::APSInt>;
95
96 X.Profile(ID);
97 FoldNodeTy *P = APSIntSet.lookup(ID, InsertToken);
98
99 if (!P) {
100 P = new (BPAlloc) FoldNodeTy(X);
101 APSIntSet.insert(P, InsertToken);
102 }
103
104 // We own the APSInt object. It's safe here.
105 return APSIntPtr::unsafeConstructor(&P->getValue());
106}
107
108APSIntPtr BasicValueFactory::getValue(const llvm::APInt &X, bool isUnsigned) {
109 llvm::APSInt V(X, isUnsigned);
110 return getValue(V);
111}
112
113APSIntPtr BasicValueFactory::getValue(uint64_t X, unsigned BitWidth,
114 bool isUnsigned) {
115 llvm::APSInt V(BitWidth, isUnsigned);
116 V = X;
117 return getValue(V);
118}
119
120APSIntPtr BasicValueFactory::getValue(uint64_t X, QualType T) {
121 return getValue(getAPSIntType(T).getValue(X));
122}
123
124const CompoundValData*
126 llvm::ImmutableList<SVal> Vals) {
127 llvm::FoldingSetNodeID ID;
128 CompoundValData::Profile(ID, T, Vals);
129 llvm::FoldingSetInsertToken InsertToken;
130
131 CompoundValData *D = CompoundValDataSet.lookup(ID, InsertToken);
132
133 if (!D) {
134 D = new (BPAlloc) CompoundValData(T, Vals);
135 CompoundValDataSet.insert(D, InsertToken);
136 }
137
138 return D;
139}
140
143 const TypedValueRegion *region) {
144 llvm::FoldingSetNodeID ID;
145 LazyCompoundValData::Profile(ID, store, region);
146 llvm::FoldingSetInsertToken InsertToken;
147
148 LazyCompoundValData *D = LazyCompoundValDataSet.lookup(ID, InsertToken);
149
150 if (!D) {
151 D = new (BPAlloc) LazyCompoundValData(store, region);
152 LazyCompoundValDataSet.insert(D, InsertToken);
153 }
154
155 return D;
156}
157
159 const NamedDecl *ND, llvm::ImmutableList<const CXXBaseSpecifier *> L) {
160 llvm::FoldingSetNodeID ID;
162 llvm::FoldingSetInsertToken InsertToken;
163
164 PointerToMemberData *D = PointerToMemberDataSet.lookup(ID, InsertToken);
165
166 if (!D) {
167 D = new (BPAlloc) PointerToMemberData(ND, L);
168 PointerToMemberDataSet.insert(D, InsertToken);
169 }
170
171 return D;
172}
173
174[[maybe_unused]] static bool hasNoRepeatedElements(
175 llvm::ImmutableList<const CXXBaseSpecifier *> BaseSpecList) {
177 for (const CXXBaseSpecifier *BaseSpec : BaseSpecList) {
178 QualType BaseType = BaseSpec->getType();
179 // Check whether inserted
180 if (!BaseSpecSeen.insert(BaseType).second)
181 return false;
182 }
183 return true;
184}
185
187 llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
188 const nonloc::PointerToMember &PTM, const CastKind &kind) {
189 assert((kind == CK_DerivedToBaseMemberPointer ||
190 kind == CK_BaseToDerivedMemberPointer ||
191 kind == CK_ReinterpretMemberPointer) &&
192 "accumCXXBase called with wrong CastKind");
194 const NamedDecl *ND = nullptr;
195 llvm::ImmutableList<const CXXBaseSpecifier *> BaseSpecList;
196
197 if (PTMDT.isNull() || isa<const NamedDecl *>(PTMDT)) {
198 if (const auto *NDP = dyn_cast_if_present<const NamedDecl *>(PTMDT))
199 ND = NDP;
200
201 BaseSpecList = CXXBaseListFactory.getEmptyList();
202 } else {
203 const auto *PTMD = cast<const PointerToMemberData *>(PTMDT);
204 ND = PTMD->getDeclaratorDecl();
205
206 BaseSpecList = PTMD->getCXXBaseList();
207 }
208
209 assert(hasNoRepeatedElements(BaseSpecList) &&
210 "CXXBaseSpecifier list of PointerToMemberData must not have repeated "
211 "elements");
212
213 if (kind == CK_DerivedToBaseMemberPointer) {
214 // Here we pop off matching CXXBaseSpecifiers from BaseSpecList.
215 // Because, CK_DerivedToBaseMemberPointer comes from a static_cast and
216 // serves to remove a matching implicit cast. Note that static_cast's that
217 // are no-ops do not count since they produce an empty PathRange, a nice
218 // thing about Clang AST.
219
220 // Now we know that there are no repetitions in BaseSpecList.
221 // So, popping the first element from it corresponding to each element in
222 // PathRange is equivalent to only including elements that are in
223 // BaseSpecList but not it PathRange
224 auto ReducedBaseSpecList = CXXBaseListFactory.getEmptyList();
225 for (const CXXBaseSpecifier *BaseSpec : BaseSpecList) {
226 auto IsSameAsBaseSpec = [&BaseSpec](const CXXBaseSpecifier *I) -> bool {
227 return BaseSpec->getType() == I->getType();
228 };
229 if (llvm::none_of(PathRange, IsSameAsBaseSpec))
230 ReducedBaseSpecList =
231 CXXBaseListFactory.add(BaseSpec, ReducedBaseSpecList);
232 }
233
234 return getPointerToMemberData(ND, ReducedBaseSpecList);
235 }
236 // FIXME: Reinterpret casts on member-pointers are not handled properly by
237 // this code
238 for (const CXXBaseSpecifier *I : llvm::reverse(PathRange))
239 BaseSpecList = prependCXXBase(I, BaseSpecList);
240 return getPointerToMemberData(ND, BaseSpecList);
241}
242
244 const llvm::APSInt &V1) {
245 switch (Op) {
246 default:
247 assert(false && "Invalid Opcode.");
248 return std::nullopt;
249
250 case UO_Minus:
251 return getValue(-V1);
252
253 case UO_Not:
254 return getValue(~V1);
255 }
256}
257
258std::optional<APSIntPtr>
260 const llvm::APSInt &V2) {
261 switch (Op) {
262 default:
263 assert(false && "Invalid Opcode.");
264 return std::nullopt;
265
266 case BO_Mul:
267 return getValue(V1 * V2);
268
269 case BO_Div:
270 if (V2 == 0) // Avoid division by zero
271 return std::nullopt;
272 return getValue(V1 / V2);
273
274 case BO_Rem:
275 if (V2 == 0) // Avoid division by zero
276 return std::nullopt;
277 return getValue(V1 % V2);
278
279 case BO_Add:
280 return getValue(V1 + V2);
281
282 case BO_Sub:
283 return getValue(V1 - V2);
284
285 case BO_Shl: {
286 // FIXME: This logic should probably go higher up, where we can
287 // test these conditions symbolically.
288
289 if (V2.isNegative() || V2.getBitWidth() > 64)
290 return std::nullopt;
291
292 uint64_t Amt = V2.getZExtValue();
293
294 if (Amt >= V1.getBitWidth())
295 return std::nullopt;
296
297 return getValue(V1.operator<<((unsigned)Amt));
298 }
299
300 case BO_Shr: {
301 // FIXME: This logic should probably go higher up, where we can
302 // test these conditions symbolically.
303
304 if (V2.isNegative() || V2.getBitWidth() > 64)
305 return std::nullopt;
306
307 uint64_t Amt = V2.getZExtValue();
308
309 if (Amt >= V1.getBitWidth())
310 return std::nullopt;
311
312 return getValue(V1.operator>>((unsigned)Amt));
313 }
314
315 case BO_LT:
316 return getTruthValue(V1 < V2);
317
318 case BO_GT:
319 return getTruthValue(V1 > V2);
320
321 case BO_LE:
322 return getTruthValue(V1 <= V2);
323
324 case BO_GE:
325 return getTruthValue(V1 >= V2);
326
327 case BO_EQ:
328 return getTruthValue(V1 == V2);
329
330 case BO_NE:
331 return getTruthValue(V1 != V2);
332
333 // Note: LAnd, LOr, Comma are handled specially by higher-level logic.
334
335 case BO_And:
336 return getValue(V1 & V2);
337
338 case BO_Or:
339 return getValue(V1 | V2);
340
341 case BO_Xor:
342 return getValue(V1 ^ V2);
343 }
344}
345
346const std::pair<SVal, uintptr_t>&
348 // Lazily create the folding set.
349 if (!PersistentSVals) PersistentSVals = new PersistentSValsTy();
350
351 llvm::FoldingSetNodeID ID;
352 llvm::FoldingSetInsertToken InsertToken;
353 V.Profile(ID);
354 ID.AddPointer((void*) Data);
355
356 PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals);
357
358 using FoldNodeTy = llvm::FoldingSetNodeWrapper<SValData>;
359
360 FoldNodeTy *P = Map.lookup(ID, InsertToken);
361
362 if (!P) {
363 P = new (BPAlloc) FoldNodeTy(std::make_pair(V, Data));
364 Map.insert(P, InsertToken);
365 }
366
367 return P->getValue();
368}
369
370const std::pair<SVal, SVal>&
372 // Lazily create the folding set.
373 if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy();
374
375 llvm::FoldingSetNodeID ID;
376 llvm::FoldingSetInsertToken InsertToken;
377 V1.Profile(ID);
378 V2.Profile(ID);
379
380 PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs);
381
382 using FoldNodeTy = llvm::FoldingSetNodeWrapper<SValPair>;
383
384 FoldNodeTy *P = Map.lookup(ID, InsertToken);
385
386 if (!P) {
387 P = new (BPAlloc) FoldNodeTy(std::make_pair(V1, V2));
388 Map.insert(P, InsertToken);
389 }
390
391 return P->getValue();
392}
393
#define V(N, I)
std::pair< SVal, SVal > SValPair
static bool hasNoRepeatedElements(llvm::ImmutableList< const CXXBaseSpecifier * > BaseSpecList)
std::pair< SVal, uintptr_t > SValData
llvm::FoldingSet< llvm::FoldingSetNodeWrapper< SValData > > PersistentSValsTy
llvm::FoldingSet< llvm::FoldingSetNodeWrapper< SValPair > > PersistentSValPairsTy
static bool isUnsigned(SValBuilder &SVB, NonLoc Value)
#define X(type, name)
Definition Value.h:97
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
BinaryOperatorKind Opcode
Definition Expr.h:4087
Represents a base class of a C++ class.
Definition DeclCXX.h:146
This represents a decl that may have a name.
Definition Decl.h:275
A (possibly-)qualified type.
Definition TypeBase.h:938
UnaryOperatorKind Opcode
Definition Expr.h:2302
A safe wrapper around APSInt objects allocated and owned by BasicValueFactory.
Definition APSIntPtr.h:19
static APSIntPtr unsafeConstructor(const APSInt *Ptr)
You should not use this API.
Definition APSIntPtr.h:31
const CompoundValData * getCompoundValData(QualType T, llvm::ImmutableList< SVal > Vals)
const std::pair< SVal, SVal > & getPersistentSValPair(const SVal &V1, const SVal &V2)
const std::pair< SVal, uintptr_t > & getPersistentSValWithData(const SVal &V, uintptr_t Data)
APSIntType getAPSIntType(QualType T) const
Returns the type of the APSInt used to store values of the given QualType.
APSIntPtr getTruthValue(bool b, QualType T)
const PointerToMemberData * getPointerToMemberData(const NamedDecl *ND, llvm::ImmutableList< const CXXBaseSpecifier * > L)
llvm::ImmutableList< const CXXBaseSpecifier * > prependCXXBase(const CXXBaseSpecifier *CBS, llvm::ImmutableList< const CXXBaseSpecifier * > L)
std::optional< APSIntPtr > evalAPSInt(UnaryOperator::Opcode Op, const llvm::APSInt &V1)
const LazyCompoundValData * getLazyCompoundValData(const StoreRef &store, const TypedValueRegion *region)
const PointerToMemberData * accumCXXBase(llvm::iterator_range< CastExpr::path_const_iterator > PathRange, const nonloc::PointerToMember &PTM, const clang::CastKind &kind)
static void Profile(llvm::FoldingSetNodeID &ID, QualType T, llvm::ImmutableList< SVal > L)
static void Profile(llvm::FoldingSetNodeID &ID, const StoreRef &store, const TypedValueRegion *region)
static void Profile(llvm::FoldingSetNodeID &ID, const NamedDecl *D, llvm::ImmutableList< const CXXBaseSpecifier * > L)
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:57
void Profile(llvm::FoldingSetNodeID &ID) const
Definition SVals.h:98
TypedValueRegion - An abstract class representing regions having a typed value.
Definition MemRegion.h:569
Value representing pointer-to-member.
Definition SVals.h:440
llvm::PointerUnion< const NamedDecl *, const PointerToMemberData * > PTMDataType
Definition SVals.h:444
const PTMDataType getPTMData() const
Definition SVals.h:447
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
const FunctionProtoType * T
CastKind
CastKind - The kind of operation required for a conversion.
U cast(CodeGen::Address addr)
Definition Address.h:327
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
static void Profile(const SValData &X, llvm::FoldingSetNodeID &ID)
static void Profile(const SValPair &X, llvm::FoldingSetNodeID &ID)