1 //===- BasicValueFactory.cpp - Basic values for Path Sens analysis --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
21 #include "llvm/ADT/APSInt.h"
22 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/ADT/ImmutableList.h"
24 #include "llvm/ADT/STLExtras.h"
29 using namespace clang;
32 void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T,
33 llvm::ImmutableList<SVal> L) {
35 ID.AddPointer(L.getInternalPointer());
38 void LazyCompoundValData::Profile(llvm::FoldingSetNodeID& ID,
39 const StoreRef &store,
40 const TypedValueRegion *region) {
41 ID.AddPointer(store.getStore());
42 ID.AddPointer(region);
45 void PointerToMemberData::Profile(
46 llvm::FoldingSetNodeID& ID, const DeclaratorDecl *D,
47 llvm::ImmutableList<const CXXBaseSpecifier *> L) {
49 ID.AddPointer(L.getInternalPointer());
52 using SValData = std::pair<SVal, uintptr_t>;
53 using SValPair = std::pair<SVal, SVal>;
57 template<> struct FoldingSetTrait<SValData> {
58 static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) {
60 ID.AddPointer( (void*) X.second);
64 template<> struct FoldingSetTrait<SValPair> {
65 static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) {
73 using PersistentSValsTy =
74 llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData>>;
76 using PersistentSValPairsTy =
77 llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair>>;
79 BasicValueFactory::~BasicValueFactory() {
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();
86 delete (PersistentSValsTy*) PersistentSVals;
87 delete (PersistentSValPairsTy*) PersistentSValPairs;
90 const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) {
91 llvm::FoldingSetNodeID ID;
94 using FoldNodeTy = llvm::FoldingSetNodeWrapper<llvm::APSInt>;
97 FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
100 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
101 new (P) FoldNodeTy(X);
102 APSIntSet.InsertNode(P, InsertPos);
108 const llvm::APSInt& BasicValueFactory::getValue(const llvm::APInt& X,
110 llvm::APSInt V(X, isUnsigned);
114 const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth,
116 llvm::APSInt V(BitWidth, isUnsigned);
121 const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) {
122 return getValue(getAPSIntType(T).getValue(X));
125 const CompoundValData*
126 BasicValueFactory::getCompoundValData(QualType T,
127 llvm::ImmutableList<SVal> Vals) {
128 llvm::FoldingSetNodeID ID;
129 CompoundValData::Profile(ID, T, Vals);
132 CompoundValData* D = CompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
135 D = (CompoundValData*) BPAlloc.Allocate<CompoundValData>();
136 new (D) CompoundValData(T, Vals);
137 CompoundValDataSet.InsertNode(D, InsertPos);
143 const LazyCompoundValData*
144 BasicValueFactory::getLazyCompoundValData(const StoreRef &store,
145 const TypedValueRegion *region) {
146 llvm::FoldingSetNodeID ID;
147 LazyCompoundValData::Profile(ID, store, region);
150 LazyCompoundValData *D =
151 LazyCompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
154 D = (LazyCompoundValData*) BPAlloc.Allocate<LazyCompoundValData>();
155 new (D) LazyCompoundValData(store, region);
156 LazyCompoundValDataSet.InsertNode(D, InsertPos);
162 const PointerToMemberData *BasicValueFactory::getPointerToMemberData(
163 const DeclaratorDecl *DD, llvm::ImmutableList<const CXXBaseSpecifier *> L) {
164 llvm::FoldingSetNodeID ID;
165 PointerToMemberData::Profile(ID, DD, L);
168 PointerToMemberData *D =
169 PointerToMemberDataSet.FindNodeOrInsertPos(ID, InsertPos);
172 D = (PointerToMemberData*) BPAlloc.Allocate<PointerToMemberData>();
173 new (D) PointerToMemberData(DD, L);
174 PointerToMemberDataSet.InsertNode(D, InsertPos);
180 const PointerToMemberData *BasicValueFactory::accumCXXBase(
181 llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
182 const nonloc::PointerToMember &PTM) {
183 nonloc::PointerToMember::PTMDataType PTMDT = PTM.getPTMData();
184 const DeclaratorDecl *DD = nullptr;
185 llvm::ImmutableList<const CXXBaseSpecifier *> PathList;
187 if (PTMDT.isNull() || PTMDT.is<const DeclaratorDecl *>()) {
188 if (PTMDT.is<const DeclaratorDecl *>())
189 DD = PTMDT.get<const DeclaratorDecl *>();
191 PathList = CXXBaseListFactory.getEmptyList();
192 } else { // const PointerToMemberData *
193 const PointerToMemberData *PTMD =
194 PTMDT.get<const PointerToMemberData *>();
195 DD = PTMD->getDeclaratorDecl();
197 PathList = PTMD->getCXXBaseList();
200 for (const auto &I : llvm::reverse(PathRange))
201 PathList = prependCXXBase(I, PathList);
202 return getPointerToMemberData(DD, PathList);
206 BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
207 const llvm::APSInt& V1, const llvm::APSInt& V2) {
210 assert(false && "Invalid Opcode.");
213 return &getValue( V1 * V2 );
216 if (V2 == 0) // Avoid division by zero
218 return &getValue( V1 / V2 );
221 if (V2 == 0) // Avoid division by zero
223 return &getValue( V1 % V2 );
226 return &getValue( V1 + V2 );
229 return &getValue( V1 - V2 );
232 // FIXME: This logic should probably go higher up, where we can
233 // test these conditions symbolically.
235 if (V1.isSigned() && V1.isNegative())
238 if (V2.isSigned() && V2.isNegative())
241 uint64_t Amt = V2.getZExtValue();
243 if (Amt >= V1.getBitWidth())
246 if (V1.isSigned() && Amt > V1.countLeadingZeros())
249 return &getValue( V1.operator<<( (unsigned) Amt ));
253 // FIXME: This logic should probably go higher up, where we can
254 // test these conditions symbolically.
256 if (V2.isSigned() && V2.isNegative())
259 uint64_t Amt = V2.getZExtValue();
261 if (Amt >= V1.getBitWidth())
264 return &getValue( V1.operator>>( (unsigned) Amt ));
268 return &getTruthValue( V1 < V2 );
271 return &getTruthValue( V1 > V2 );
274 return &getTruthValue( V1 <= V2 );
277 return &getTruthValue( V1 >= V2 );
280 return &getTruthValue( V1 == V2 );
283 return &getTruthValue( V1 != V2 );
285 // Note: LAnd, LOr, Comma are handled specially by higher-level logic.
288 return &getValue( V1 & V2 );
291 return &getValue( V1 | V2 );
294 return &getValue( V1 ^ V2 );
298 const std::pair<SVal, uintptr_t>&
299 BasicValueFactory::getPersistentSValWithData(const SVal& V, uintptr_t Data) {
300 // Lazily create the folding set.
301 if (!PersistentSVals) PersistentSVals = new PersistentSValsTy();
303 llvm::FoldingSetNodeID ID;
306 ID.AddPointer((void*) Data);
308 PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals);
310 using FoldNodeTy = llvm::FoldingSetNodeWrapper<SValData>;
312 FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
315 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
316 new (P) FoldNodeTy(std::make_pair(V, Data));
317 Map.InsertNode(P, InsertPos);
320 return P->getValue();
323 const std::pair<SVal, SVal>&
324 BasicValueFactory::getPersistentSValPair(const SVal& V1, const SVal& V2) {
325 // Lazily create the folding set.
326 if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy();
328 llvm::FoldingSetNodeID ID;
333 PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs);
335 using FoldNodeTy = llvm::FoldingSetNodeWrapper<SValPair>;
337 FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
340 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
341 new (P) FoldNodeTy(std::make_pair(V1, V2));
342 Map.InsertNode(P, InsertPos);
345 return P->getValue();
348 const SVal* BasicValueFactory::getPersistentSVal(SVal X) {
349 return &getPersistentSValWithData(X, 0).first;