clang  5.0.0
BasicValueFactory.cpp
Go to the documentation of this file.
1 //=== BasicValueFactory.cpp - Basic values for Path Sens analysis --*- C++ -*-//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "clang/AST/ASTContext.h"
19 
20 using namespace clang;
21 using namespace ento;
22 
23 void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T,
25  T.Profile(ID);
26  ID.AddPointer(L.getInternalPointer());
27 }
28 
29 void LazyCompoundValData::Profile(llvm::FoldingSetNodeID& ID,
30  const StoreRef &store,
31  const TypedValueRegion *region) {
32  ID.AddPointer(store.getStore());
33  ID.AddPointer(region);
34 }
35 
37  llvm::FoldingSetNodeID& ID, const DeclaratorDecl *D,
39  ID.AddPointer(D);
40  ID.AddPointer(L.getInternalPointer());
41 }
42 
43 typedef std::pair<SVal, uintptr_t> SValData;
44 typedef std::pair<SVal, SVal> SValPair;
45 
46 namespace llvm {
47 template<> struct FoldingSetTrait<SValData> {
48  static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) {
49  X.first.Profile(ID);
50  ID.AddPointer( (void*) X.second);
51  }
52 };
53 
54 template<> struct FoldingSetTrait<SValPair> {
55  static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) {
56  X.first.Profile(ID);
57  X.second.Profile(ID);
58  }
59 };
60 }
61 
62 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData> >
64 
65 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair> >
67 
69  // Note that the dstor for the contents of APSIntSet will never be called,
70  // so we iterate over the set and invoke the dstor for each APSInt. This
71  // frees an aux. memory allocated to represent very large constants.
72  for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I)
73  I->getValue().~APSInt();
74 
75  delete (PersistentSValsTy*) PersistentSVals;
76  delete (PersistentSValPairsTy*) PersistentSValPairs;
77 }
78 
79 const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) {
80  llvm::FoldingSetNodeID ID;
81  void *InsertPos;
82  typedef llvm::FoldingSetNodeWrapper<llvm::APSInt> FoldNodeTy;
83 
84  X.Profile(ID);
85  FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
86 
87  if (!P) {
88  P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
89  new (P) FoldNodeTy(X);
90  APSIntSet.InsertNode(P, InsertPos);
91  }
92 
93  return *P;
94 }
95 
96 const llvm::APSInt& BasicValueFactory::getValue(const llvm::APInt& X,
97  bool isUnsigned) {
98  llvm::APSInt V(X, isUnsigned);
99  return getValue(V);
100 }
101 
102 const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth,
103  bool isUnsigned) {
104  llvm::APSInt V(BitWidth, isUnsigned);
105  V = X;
106  return getValue(V);
107 }
108 
109 const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) {
110 
111  return getValue(getAPSIntType(T).getValue(X));
112 }
113 
114 const CompoundValData*
117 
118  llvm::FoldingSetNodeID ID;
119  CompoundValData::Profile(ID, T, Vals);
120  void *InsertPos;
121 
122  CompoundValData* D = CompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
123 
124  if (!D) {
125  D = (CompoundValData*) BPAlloc.Allocate<CompoundValData>();
126  new (D) CompoundValData(T, Vals);
127  CompoundValDataSet.InsertNode(D, InsertPos);
128  }
129 
130  return D;
131 }
132 
133 const LazyCompoundValData*
135  const TypedValueRegion *region) {
136  llvm::FoldingSetNodeID ID;
137  LazyCompoundValData::Profile(ID, store, region);
138  void *InsertPos;
139 
141  LazyCompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
142 
143  if (!D) {
144  D = (LazyCompoundValData*) BPAlloc.Allocate<LazyCompoundValData>();
145  new (D) LazyCompoundValData(store, region);
146  LazyCompoundValDataSet.InsertNode(D, InsertPos);
147  }
148 
149  return D;
150 }
151 
154  llvm::FoldingSetNodeID ID;
155  PointerToMemberData::Profile(ID, DD, L);
156  void *InsertPos;
157 
159  PointerToMemberDataSet.FindNodeOrInsertPos(ID, InsertPos);
160 
161  if (!D) {
162  D = (PointerToMemberData*) BPAlloc.Allocate<PointerToMemberData>();
163  new (D) PointerToMemberData(DD, L);
164  PointerToMemberDataSet.InsertNode(D, InsertPos);
165  }
166 
167  return D;
168 }
169 
171  llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
172  const nonloc::PointerToMember &PTM) {
174  const DeclaratorDecl *DD = nullptr;
176 
177  if (PTMDT.isNull() || PTMDT.is<const DeclaratorDecl *>()) {
178  if (PTMDT.is<const DeclaratorDecl *>())
179  DD = PTMDT.get<const DeclaratorDecl *>();
180 
181  PathList = CXXBaseListFactory.getEmptyList();
182  } else { // const PointerToMemberData *
183  const PointerToMemberData *PTMD =
184  PTMDT.get<const PointerToMemberData *>();
185  DD = PTMD->getDeclaratorDecl();
186 
187  PathList = PTMD->getCXXBaseList();
188  }
189 
190  for (const auto &I : llvm::reverse(PathRange))
191  PathList = prependCXXBase(I, PathList);
192  return getPointerToMemberData(DD, PathList);
193 }
194 
195 const llvm::APSInt*
197  const llvm::APSInt& V1, const llvm::APSInt& V2) {
198 
199  switch (Op) {
200  default:
201  assert (false && "Invalid Opcode.");
202 
203  case BO_Mul:
204  return &getValue( V1 * V2 );
205 
206  case BO_Div:
207  if (V2 == 0) // Avoid division by zero
208  return nullptr;
209  return &getValue( V1 / V2 );
210 
211  case BO_Rem:
212  if (V2 == 0) // Avoid division by zero
213  return nullptr;
214  return &getValue( V1 % V2 );
215 
216  case BO_Add:
217  return &getValue( V1 + V2 );
218 
219  case BO_Sub:
220  return &getValue( V1 - V2 );
221 
222  case BO_Shl: {
223 
224  // FIXME: This logic should probably go higher up, where we can
225  // test these conditions symbolically.
226 
227  // FIXME: Expand these checks to include all undefined behavior.
228 
229  if (V2.isSigned() && V2.isNegative())
230  return nullptr;
231 
232  uint64_t Amt = V2.getZExtValue();
233 
234  if (Amt >= V1.getBitWidth())
235  return nullptr;
236 
237  return &getValue( V1.operator<<( (unsigned) Amt ));
238  }
239 
240  case BO_Shr: {
241 
242  // FIXME: This logic should probably go higher up, where we can
243  // test these conditions symbolically.
244 
245  // FIXME: Expand these checks to include all undefined behavior.
246 
247  if (V2.isSigned() && V2.isNegative())
248  return nullptr;
249 
250  uint64_t Amt = V2.getZExtValue();
251 
252  if (Amt >= V1.getBitWidth())
253  return nullptr;
254 
255  return &getValue( V1.operator>>( (unsigned) Amt ));
256  }
257 
258  case BO_LT:
259  return &getTruthValue( V1 < V2 );
260 
261  case BO_GT:
262  return &getTruthValue( V1 > V2 );
263 
264  case BO_LE:
265  return &getTruthValue( V1 <= V2 );
266 
267  case BO_GE:
268  return &getTruthValue( V1 >= V2 );
269 
270  case BO_EQ:
271  return &getTruthValue( V1 == V2 );
272 
273  case BO_NE:
274  return &getTruthValue( V1 != V2 );
275 
276  // Note: LAnd, LOr, Comma are handled specially by higher-level logic.
277 
278  case BO_And:
279  return &getValue( V1 & V2 );
280 
281  case BO_Or:
282  return &getValue( V1 | V2 );
283 
284  case BO_Xor:
285  return &getValue( V1 ^ V2 );
286  }
287 }
288 
289 
290 const std::pair<SVal, uintptr_t>&
292 
293  // Lazily create the folding set.
294  if (!PersistentSVals) PersistentSVals = new PersistentSValsTy();
295 
296  llvm::FoldingSetNodeID ID;
297  void *InsertPos;
298  V.Profile(ID);
299  ID.AddPointer((void*) Data);
300 
301  PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals);
302 
303  typedef llvm::FoldingSetNodeWrapper<SValData> FoldNodeTy;
304  FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
305 
306  if (!P) {
307  P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
308  new (P) FoldNodeTy(std::make_pair(V, Data));
309  Map.InsertNode(P, InsertPos);
310  }
311 
312  return P->getValue();
313 }
314 
315 const std::pair<SVal, SVal>&
317 
318  // Lazily create the folding set.
319  if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy();
320 
321  llvm::FoldingSetNodeID ID;
322  void *InsertPos;
323  V1.Profile(ID);
324  V2.Profile(ID);
325 
326  PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs);
327 
328  typedef llvm::FoldingSetNodeWrapper<SValPair> FoldNodeTy;
329  FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
330 
331  if (!P) {
332  P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
333  new (P) FoldNodeTy(std::make_pair(V1, V2));
334  Map.InsertNode(P, InsertPos);
335  }
336 
337  return P->getValue();
338 }
339 
341  return &getPersistentSValWithData(X, 0).first;
342 }
Defines the clang::ASTContext interface.
void Profile(llvm::FoldingSetNodeID &ID) const
Definition: SVals.h:115
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:511
A (possibly-)qualified type.
Definition: Type.h:616
Store getStore() const
Definition: StoreRef.h:46
const CompoundValData * getCompoundValData(QualType T, llvm::ImmutableList< SVal > Vals)
const std::pair< SVal, SVal > & getPersistentSValPair(const SVal &V1, const SVal &V2)
StringRef P
const llvm::APSInt & getTruthValue(bool b, QualType T)
llvm::ImmutableList< const CXXBaseSpecifier * > getCXXBaseList() const
static void Profile(llvm::FoldingSetNodeID &ID, const DeclaratorDecl *D, llvm::ImmutableList< const CXXBaseSpecifier * > L)
const clang::ento::PointerToMemberData * accumCXXBase(llvm::iterator_range< CastExpr::path_const_iterator > PathRange, const nonloc::PointerToMember &PTM)
Value representing pointer-to-member.
Definition: SVals.h:486
llvm::ImmutableList< const CXXBaseSpecifier * > prependCXXBase(const CXXBaseSpecifier *CBS, llvm::ImmutableList< const CXXBaseSpecifier * > L)
BinaryOperatorKind
const SVal * getPersistentSVal(SVal X)
detail::InMemoryDirectory::const_iterator I
std::pair< SVal, SVal > SValPair
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:636
llvm::PointerUnion< const DeclaratorDecl *, const PointerToMemberData * > PTMDataType
Definition: SVals.h:491
unsigned Map[FirstTargetAddressSpace]
The type of a lookup table which maps from language-specific address spaces to target-specific ones...
Definition: AddressSpaces.h:53
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c.h:82
static void Profile(llvm::FoldingSetNodeID &ID, const StoreRef &store, const TypedValueRegion *region)
const std::string ID
llvm::FoldingSet< llvm::FoldingSetNodeWrapper< SValPair > > PersistentSValPairsTy
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:63
const llvm::APSInt * evalAPSInt(BinaryOperator::Opcode Op, const llvm::APSInt &V1, const llvm::APSInt &V2)
std::pair< SVal, uintptr_t > SValData
const PointerToMemberData * getPointerToMemberData(const DeclaratorDecl *DD, llvm::ImmutableList< const CXXBaseSpecifier * > L)
const PTMDataType getPTMData() const
Definition: SVals.h:492
llvm::FoldingSet< llvm::FoldingSetNodeWrapper< SValData > > PersistentSValsTy
static void Profile(llvm::FoldingSetNodeID &ID, QualType T, llvm::ImmutableList< SVal > L)
const DeclaratorDecl * getDeclaratorDecl() const
detail::InMemoryDirectory::const_iterator E
static void Profile(const SValPair &X, llvm::FoldingSetNodeID &ID)
const std::pair< SVal, uintptr_t > & getPersistentSValWithData(const SVal &V, uintptr_t Data)
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:13074
const LazyCompoundValData * getLazyCompoundValData(const StoreRef &store, const TypedValueRegion *region)
static void Profile(const SValData &X, llvm::FoldingSetNodeID &ID)
void Profile(llvm::FoldingSetNodeID &ID) const
Definition: Type.h:1007
APSIntType getAPSIntType(QualType T) const
Returns the type of the APSInt used to store values of the given QualType.