1 //=== BasicValueFactory.cpp - Basic values for Path Sens analysis --*- C++ -*-//
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/Store.h"
19 using namespace clang;
22 void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T,
23 llvm::ImmutableList<SVal> L) {
25 ID.AddPointer(L.getInternalPointer());
28 void LazyCompoundValData::Profile(llvm::FoldingSetNodeID& ID,
29 const StoreRef &store,
30 const TypedValueRegion *region) {
31 ID.AddPointer(store.getStore());
32 ID.AddPointer(region);
35 typedef std::pair<SVal, uintptr_t> SValData;
36 typedef std::pair<SVal, SVal> SValPair;
39 template<> struct FoldingSetTrait<SValData> {
40 static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) {
42 ID.AddPointer( (void*) X.second);
46 template<> struct FoldingSetTrait<SValPair> {
47 static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) {
54 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData> >
57 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair> >
58 PersistentSValPairsTy;
60 BasicValueFactory::~BasicValueFactory() {
61 // Note that the dstor for the contents of APSIntSet will never be called,
62 // so we iterate over the set and invoke the dstor for each APSInt. This
63 // frees an aux. memory allocated to represent very large constants.
64 for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I)
65 I->getValue().~APSInt();
67 delete (PersistentSValsTy*) PersistentSVals;
68 delete (PersistentSValPairsTy*) PersistentSValPairs;
71 const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) {
72 llvm::FoldingSetNodeID ID;
74 typedef llvm::FoldingSetNodeWrapper<llvm::APSInt> FoldNodeTy;
77 FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
80 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
81 new (P) FoldNodeTy(X);
82 APSIntSet.InsertNode(P, InsertPos);
88 const llvm::APSInt& BasicValueFactory::getValue(const llvm::APInt& X,
90 llvm::APSInt V(X, isUnsigned);
94 const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth,
96 llvm::APSInt V(BitWidth, isUnsigned);
101 const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) {
103 unsigned bits = Ctx.getTypeSize(T);
105 T->isUnsignedIntegerOrEnumerationType() || Loc::isLocType(T));
110 const CompoundValData*
111 BasicValueFactory::getCompoundValData(QualType T,
112 llvm::ImmutableList<SVal> Vals) {
114 llvm::FoldingSetNodeID ID;
115 CompoundValData::Profile(ID, T, Vals);
118 CompoundValData* D = CompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
121 D = (CompoundValData*) BPAlloc.Allocate<CompoundValData>();
122 new (D) CompoundValData(T, Vals);
123 CompoundValDataSet.InsertNode(D, InsertPos);
129 const LazyCompoundValData*
130 BasicValueFactory::getLazyCompoundValData(const StoreRef &store,
131 const TypedValueRegion *region) {
132 llvm::FoldingSetNodeID ID;
133 LazyCompoundValData::Profile(ID, store, region);
136 LazyCompoundValData *D =
137 LazyCompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
140 D = (LazyCompoundValData*) BPAlloc.Allocate<LazyCompoundValData>();
141 new (D) LazyCompoundValData(store, region);
142 LazyCompoundValDataSet.InsertNode(D, InsertPos);
149 BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
150 const llvm::APSInt& V1, const llvm::APSInt& V2) {
154 assert (false && "Invalid Opcode.");
157 return &getValue( V1 * V2 );
160 return &getValue( V1 / V2 );
163 return &getValue( V1 % V2 );
166 return &getValue( V1 + V2 );
169 return &getValue( V1 - V2 );
173 // FIXME: This logic should probably go higher up, where we can
174 // test these conditions symbolically.
176 // FIXME: Expand these checks to include all undefined behavior.
178 if (V2.isSigned() && V2.isNegative())
181 uint64_t Amt = V2.getZExtValue();
183 if (Amt > V1.getBitWidth())
186 return &getValue( V1.operator<<( (unsigned) Amt ));
191 // FIXME: This logic should probably go higher up, where we can
192 // test these conditions symbolically.
194 // FIXME: Expand these checks to include all undefined behavior.
196 if (V2.isSigned() && V2.isNegative())
199 uint64_t Amt = V2.getZExtValue();
201 if (Amt > V1.getBitWidth())
204 return &getValue( V1.operator>>( (unsigned) Amt ));
208 return &getTruthValue( V1 < V2 );
211 return &getTruthValue( V1 > V2 );
214 return &getTruthValue( V1 <= V2 );
217 return &getTruthValue( V1 >= V2 );
220 return &getTruthValue( V1 == V2 );
223 return &getTruthValue( V1 != V2 );
225 // Note: LAnd, LOr, Comma are handled specially by higher-level logic.
228 return &getValue( V1 & V2 );
231 return &getValue( V1 | V2 );
234 return &getValue( V1 ^ V2 );
239 const std::pair<SVal, uintptr_t>&
240 BasicValueFactory::getPersistentSValWithData(const SVal& V, uintptr_t Data) {
242 // Lazily create the folding set.
243 if (!PersistentSVals) PersistentSVals = new PersistentSValsTy();
245 llvm::FoldingSetNodeID ID;
248 ID.AddPointer((void*) Data);
250 PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals);
252 typedef llvm::FoldingSetNodeWrapper<SValData> FoldNodeTy;
253 FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
256 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
257 new (P) FoldNodeTy(std::make_pair(V, Data));
258 Map.InsertNode(P, InsertPos);
261 return P->getValue();
264 const std::pair<SVal, SVal>&
265 BasicValueFactory::getPersistentSValPair(const SVal& V1, const SVal& V2) {
267 // Lazily create the folding set.
268 if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy();
270 llvm::FoldingSetNodeID ID;
275 PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs);
277 typedef llvm::FoldingSetNodeWrapper<SValPair> FoldNodeTy;
278 FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
281 P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
282 new (P) FoldNodeTy(std::make_pair(V1, V2));
283 Map.InsertNode(P, InsertPos);
286 return P->getValue();
289 const SVal* BasicValueFactory::getPersistentSVal(SVal X) {
290 return &getPersistentSValWithData(X, 0).first;