1 //===- BasicValueFactory.cpp - Basic values for Path Sens analysis --------===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
16 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
20 #include "llvm/ADT/APSInt.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/ImmutableList.h"
23 #include "llvm/ADT/STLExtras.h"
28 using namespace clang;
31 void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T,
32 llvm::ImmutableList<SVal> L) {
34 ID.AddPointer(L.getInternalPointer());
37 void LazyCompoundValData::Profile(llvm::FoldingSetNodeID& ID,
38 const StoreRef &store,
39 const TypedValueRegion *region) {
40 ID.AddPointer(store.getStore());
41 ID.AddPointer(region);
44 void PointerToMemberData::Profile(
45 llvm::FoldingSetNodeID& ID, const DeclaratorDecl *D,
46 llvm::ImmutableList<const CXXBaseSpecifier *> L) {
48 ID.AddPointer(L.getInternalPointer());
51 using SValData = std::pair<SVal, uintptr_t>;
52 using SValPair = std::pair<SVal, SVal>;
56 template<> struct FoldingSetTrait<SValData> {
57 static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) {
59 ID.AddPointer( (void*) X.second);
63 template<> struct FoldingSetTrait<SValPair> {
64 static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) {
72 using PersistentSValsTy =
73 llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData>>;
75 using PersistentSValPairsTy =
76 llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair>>;
78 BasicValueFactory::~BasicValueFactory() {
79 // Note that the dstor for the contents of APSIntSet will never be called,
80 // so we iterate over the set and invoke the dstor for each APSInt. This
81 // frees an aux. memory allocated to represent very large constants.
82 for (const auto &I : APSIntSet)
83 I.getValue().~APSInt();
85 delete (PersistentSValsTy*) PersistentSVals;
86 delete (PersistentSValPairsTy*) PersistentSValPairs;
89 const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) {
90 llvm::FoldingSetNodeID ID;
93 using FoldNodeTy = llvm::FoldingSetNodeWrapper<llvm::APSInt>;
96 FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
99 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
100 new (P) FoldNodeTy(X);
101 APSIntSet.InsertNode(P, InsertPos);
107 const llvm::APSInt& BasicValueFactory::getValue(const llvm::APInt& X,
109 llvm::APSInt V(X, isUnsigned);
113 const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth,
115 llvm::APSInt V(BitWidth, isUnsigned);
120 const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) {
121 return getValue(getAPSIntType(T).getValue(X));
124 const CompoundValData*
125 BasicValueFactory::getCompoundValData(QualType T,
126 llvm::ImmutableList<SVal> Vals) {
127 llvm::FoldingSetNodeID ID;
128 CompoundValData::Profile(ID, T, Vals);
131 CompoundValData* D = CompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
134 D = (CompoundValData*) BPAlloc.Allocate<CompoundValData>();
135 new (D) CompoundValData(T, Vals);
136 CompoundValDataSet.InsertNode(D, InsertPos);
142 const LazyCompoundValData*
143 BasicValueFactory::getLazyCompoundValData(const StoreRef &store,
144 const TypedValueRegion *region) {
145 llvm::FoldingSetNodeID ID;
146 LazyCompoundValData::Profile(ID, store, region);
149 LazyCompoundValData *D =
150 LazyCompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
153 D = (LazyCompoundValData*) BPAlloc.Allocate<LazyCompoundValData>();
154 new (D) LazyCompoundValData(store, region);
155 LazyCompoundValDataSet.InsertNode(D, InsertPos);
161 const PointerToMemberData *BasicValueFactory::getPointerToMemberData(
162 const DeclaratorDecl *DD, llvm::ImmutableList<const CXXBaseSpecifier *> L) {
163 llvm::FoldingSetNodeID ID;
164 PointerToMemberData::Profile(ID, DD, L);
167 PointerToMemberData *D =
168 PointerToMemberDataSet.FindNodeOrInsertPos(ID, InsertPos);
171 D = (PointerToMemberData*) BPAlloc.Allocate<PointerToMemberData>();
172 new (D) PointerToMemberData(DD, L);
173 PointerToMemberDataSet.InsertNode(D, InsertPos);
179 const PointerToMemberData *BasicValueFactory::accumCXXBase(
180 llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
181 const nonloc::PointerToMember &PTM) {
182 nonloc::PointerToMember::PTMDataType PTMDT = PTM.getPTMData();
183 const DeclaratorDecl *DD = nullptr;
184 llvm::ImmutableList<const CXXBaseSpecifier *> PathList;
186 if (PTMDT.isNull() || PTMDT.is<const DeclaratorDecl *>()) {
187 if (PTMDT.is<const DeclaratorDecl *>())
188 DD = PTMDT.get<const DeclaratorDecl *>();
190 PathList = CXXBaseListFactory.getEmptyList();
191 } else { // const PointerToMemberData *
192 const PointerToMemberData *PTMD =
193 PTMDT.get<const PointerToMemberData *>();
194 DD = PTMD->getDeclaratorDecl();
196 PathList = PTMD->getCXXBaseList();
199 for (const auto &I : llvm::reverse(PathRange))
200 PathList = prependCXXBase(I, PathList);
201 return getPointerToMemberData(DD, PathList);
205 BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
206 const llvm::APSInt& V1, const llvm::APSInt& V2) {
209 llvm_unreachable("Invalid Opcode.");
212 return &getValue( V1 * V2 );
215 if (V2 == 0) // Avoid division by zero
217 return &getValue( V1 / V2 );
220 if (V2 == 0) // Avoid division by zero
222 return &getValue( V1 % V2 );
225 return &getValue( V1 + V2 );
228 return &getValue( V1 - V2 );
231 // FIXME: This logic should probably go higher up, where we can
232 // test these conditions symbolically.
234 if (V2.isSigned() && V2.isNegative())
237 uint64_t Amt = V2.getZExtValue();
239 if (Amt >= V1.getBitWidth())
242 if (!Ctx.getLangOpts().CPlusPlus2a) {
243 if (V1.isSigned() && V1.isNegative())
246 if (V1.isSigned() && Amt > V1.countLeadingZeros())
250 return &getValue( V1.operator<<( (unsigned) Amt ));
254 // FIXME: This logic should probably go higher up, where we can
255 // test these conditions symbolically.
257 if (V2.isSigned() && V2.isNegative())
260 uint64_t Amt = V2.getZExtValue();
262 if (Amt >= V1.getBitWidth())
265 return &getValue( V1.operator>>( (unsigned) Amt ));
269 return &getTruthValue( V1 < V2 );
272 return &getTruthValue( V1 > V2 );
275 return &getTruthValue( V1 <= V2 );
278 return &getTruthValue( V1 >= V2 );
281 return &getTruthValue( V1 == V2 );
284 return &getTruthValue( V1 != V2 );
286 // Note: LAnd, LOr, Comma are handled specially by higher-level logic.
289 return &getValue( V1 & V2 );
292 return &getValue( V1 | V2 );
295 return &getValue( V1 ^ V2 );
299 const std::pair<SVal, uintptr_t>&
300 BasicValueFactory::getPersistentSValWithData(const SVal& V, uintptr_t Data) {
301 // Lazily create the folding set.
302 if (!PersistentSVals) PersistentSVals = new PersistentSValsTy();
304 llvm::FoldingSetNodeID ID;
307 ID.AddPointer((void*) Data);
309 PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals);
311 using FoldNodeTy = llvm::FoldingSetNodeWrapper<SValData>;
313 FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
316 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
317 new (P) FoldNodeTy(std::make_pair(V, Data));
318 Map.InsertNode(P, InsertPos);
321 return P->getValue();
324 const std::pair<SVal, SVal>&
325 BasicValueFactory::getPersistentSValPair(const SVal& V1, const SVal& V2) {
326 // Lazily create the folding set.
327 if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy();
329 llvm::FoldingSetNodeID ID;
334 PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs);
336 using FoldNodeTy = llvm::FoldingSetNodeWrapper<SValPair>;
338 FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
341 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
342 new (P) FoldNodeTy(std::make_pair(V1, V2));
343 Map.InsertNode(P, InsertPos);
346 return P->getValue();
349 const SVal* BasicValueFactory::getPersistentSVal(SVal X) {
350 return &getPersistentSValWithData(X, 0).first;