clang  6.0.0svn
BasicValueFactory.h
Go to the documentation of this file.
1 //=== BasicValueFactory.h - 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 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
17 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
18 
19 #include "clang/AST/ASTContext.h"
24 
25 namespace clang {
26 namespace ento {
27 
28 class CompoundValData : public llvm::FoldingSetNode {
29  QualType T;
31 
32 public:
34  assert(NonLoc::isCompoundType(t));
35  }
36 
38  iterator begin() const { return L.begin(); }
39  iterator end() const { return L.end(); }
40 
41  static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
43 
44  void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); }
45 };
46 
47 class LazyCompoundValData : public llvm::FoldingSetNode {
48  StoreRef store;
49  const TypedValueRegion *region;
50 public:
52  : store(st), region(r) {
54  }
55 
56  const void *getStore() const { return store.getStore(); }
57  const TypedValueRegion *getRegion() const { return region; }
58 
59  static void Profile(llvm::FoldingSetNodeID& ID,
60  const StoreRef &store,
61  const TypedValueRegion *region);
62 
63  void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); }
64 };
65 
66 class PointerToMemberData: public llvm::FoldingSetNode {
67  const DeclaratorDecl *D;
69 
70 public:
73  : D(D), L(L) {}
74 
76  iterator begin() const { return L.begin(); }
77  iterator end() const { return L.end(); }
78 
79  static void Profile(llvm::FoldingSetNodeID& ID, const DeclaratorDecl *D,
81 
82  void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, D, L); }
83  const DeclaratorDecl *getDeclaratorDecl() const {return D;}
85  return L;
86  }
87 };
88 
90  typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt> >
91  APSIntSetTy;
92 
93  ASTContext &Ctx;
94  llvm::BumpPtrAllocator& BPAlloc;
95 
96  APSIntSetTy APSIntSet;
97  void * PersistentSVals;
98  void * PersistentSValPairs;
99 
100  llvm::ImmutableList<SVal>::Factory SValListFactory;
102  llvm::FoldingSet<CompoundValData> CompoundValDataSet;
103  llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet;
104  llvm::FoldingSet<PointerToMemberData> PointerToMemberDataSet;
105 
106  // This is private because external clients should use the factory
107  // method that takes a QualType.
108  const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
109 
110 public:
111  BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
112  : Ctx(ctx), BPAlloc(Alloc), PersistentSVals(nullptr),
113  PersistentSValPairs(nullptr), SValListFactory(Alloc),
114  CXXBaseListFactory(Alloc) {}
115 
117 
118  ASTContext &getContext() const { return Ctx; }
119 
120  const llvm::APSInt& getValue(const llvm::APSInt& X);
121  const llvm::APSInt& getValue(const llvm::APInt& X, bool isUnsigned);
122  const llvm::APSInt& getValue(uint64_t X, QualType T);
123 
124  /// Returns the type of the APSInt used to store values of the given QualType.
126  assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
127  return APSIntType(Ctx.getIntWidth(T),
129  }
130 
131  /// Convert - Create a new persistent APSInt with the same value as 'From'
132  /// but with the bitwidth and signedness of 'To'.
133  const llvm::APSInt &Convert(const llvm::APSInt& To,
134  const llvm::APSInt& From) {
135  APSIntType TargetType(To);
136  if (TargetType == APSIntType(From))
137  return From;
138 
139  return getValue(TargetType.convert(From));
140  }
141 
142  const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
143  APSIntType TargetType = getAPSIntType(T);
144  if (TargetType == APSIntType(From))
145  return From;
146 
147  return getValue(TargetType.convert(From));
148  }
149 
150  const llvm::APSInt& getIntValue(uint64_t X, bool isUnsigned) {
151  QualType T = isUnsigned ? Ctx.UnsignedIntTy : Ctx.IntTy;
152  return getValue(X, T);
153  }
154 
155  inline const llvm::APSInt& getMaxValue(const llvm::APSInt &v) {
156  return getValue(APSIntType(v).getMaxValue());
157  }
158 
159  inline const llvm::APSInt& getMinValue(const llvm::APSInt &v) {
160  return getValue(APSIntType(v).getMinValue());
161  }
162 
163  inline const llvm::APSInt& getMaxValue(QualType T) {
164  return getValue(getAPSIntType(T).getMaxValue());
165  }
166 
167  inline const llvm::APSInt& getMinValue(QualType T) {
168  return getValue(getAPSIntType(T).getMinValue());
169  }
170 
171  inline const llvm::APSInt& Add1(const llvm::APSInt& V) {
172  llvm::APSInt X = V;
173  ++X;
174  return getValue(X);
175  }
176 
177  inline const llvm::APSInt& Sub1(const llvm::APSInt& V) {
178  llvm::APSInt X = V;
179  --X;
180  return getValue(X);
181  }
182 
183  inline const llvm::APSInt& getZeroWithTypeSize(QualType T) {
184  assert(T->isScalarType());
185  return getValue(0, Ctx.getTypeSize(T), true);
186  }
187 
188  inline const llvm::APSInt& getZeroWithPtrWidth(bool isUnsigned = true) {
189  return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
190  }
191 
192  inline const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) {
193  return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
194  }
195 
196  inline const llvm::APSInt& getTruthValue(bool b, QualType T) {
197  return getValue(b ? 1 : 0, Ctx.getTypeSize(T), false);
198  }
199 
200  inline const llvm::APSInt& getTruthValue(bool b) {
201  return getTruthValue(b, Ctx.getLogicalOperationType());
202  }
203 
204  const CompoundValData *getCompoundValData(QualType T,
206 
207  const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store,
208  const TypedValueRegion *region);
209 
210  const PointerToMemberData *getPointerToMemberData(
211  const DeclaratorDecl *DD,
213 
215  return SValListFactory.getEmptyList();
216  }
217 
219  return SValListFactory.add(X, L);
220  }
221 
223  return CXXBaseListFactory.getEmptyList();
224  }
225 
227  const CXXBaseSpecifier *CBS,
229  return CXXBaseListFactory.add(CBS, L);
230  }
231 
232  const clang::ento::PointerToMemberData *accumCXXBase(
233  llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
234  const nonloc::PointerToMember &PTM);
235 
236  const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op,
237  const llvm::APSInt& V1,
238  const llvm::APSInt& V2);
239 
240  const std::pair<SVal, uintptr_t>&
241  getPersistentSValWithData(const SVal& V, uintptr_t Data);
242 
243  const std::pair<SVal, SVal>&
244  getPersistentSValPair(const SVal& V1, const SVal& V2);
245 
246  const SVal* getPersistentSVal(SVal X);
247 };
248 
249 } // end GR namespace
250 
251 } // end clang namespace
252 
253 #endif
Defines the clang::ASTContext interface.
const llvm::APSInt & Convert(QualType T, const llvm::APSInt &From)
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:511
CanQualType VoidPtrTy
Definition: ASTContext.h:981
A (possibly-)qualified type.
Definition: Type.h:614
Store getStore() const
Definition: StoreRef.h:46
llvm::ImmutableList< const CXXBaseSpecifier * > getCXXBaseList() const
const llvm::APSInt & getMaxValue(QualType T)
const llvm::APSInt & getTruthValue(bool b)
virtual QualType getValueType() const =0
const llvm::APSInt & getZeroWithTypeSize(QualType T)
QualType getLogicalOperationType() const
The result type of logical operations, &#39;<&#39;, &#39;>&#39;, &#39;!=&#39;, etc.
Definition: ASTContext.h:1697
LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r)
const llvm::APSInt & Add1(const llvm::APSInt &V)
const llvm::APSInt & getTruthValue(bool b, QualType T)
llvm::ImmutableList< const CXXBaseSpecifier * >::iterator iterator
const DeclaratorDecl * getDeclaratorDecl() const
const llvm::APSInt & getMinValue(QualType T)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:128
Value representing pointer-to-member.
Definition: SVals.h:486
static bool isCompoundType(QualType T)
Definition: SVals.h:286
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:6013
const llvm::APSInt & Convert(const llvm::APSInt &To, const llvm::APSInt &From)
Convert - Create a new persistent APSInt with the same value as &#39;From&#39; but with the bitwidth and sign...
llvm::ImmutableList< SVal > prependSVal(SVal X, llvm::ImmutableList< SVal > L)
void Profile(llvm::FoldingSetNodeID &ID)
static bool isLocType(QualType T)
Definition: SVals.h:307
BinaryOperatorKind
llvm::ImmutableList< SVal >::iterator iterator
A record of the "type" of an APSInt, used for conversions.
Definition: APSIntType.h:20
bool isScalarType() const
Definition: Type.h:5998
const llvm::APSInt & getIntValue(uint64_t X, bool isUnsigned)
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:649
void Profile(llvm::FoldingSetNodeID &ID)
const FunctionProtoType * T
const llvm::APSInt & getIntWithPtrWidth(uint64_t X, bool isUnsigned)
static SVal getValue(SVal val, SValBuilder &svalBuilder)
const TypedValueRegion * getRegion() const
do v
Definition: arm_acle.h:78
__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
llvm::ImmutableList< const CXXBaseSpecifier * > getEmptyCXXBaseList()
llvm::ImmutableList< const CXXBaseSpecifier * > prependCXXBase(const CXXBaseSpecifier *CBS, 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:63
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
Definition: Type.cpp:1781
const llvm::APSInt & Sub1(const llvm::APSInt &V)
const llvm::APSInt & getZeroWithPtrWidth(bool isUnsigned=true)
llvm::APSInt convert(const llvm::APSInt &Value) const LLVM_READONLY
Convert and return a new APSInt with the given value, but this type&#39;s bit width and signedness...
Definition: APSIntType.h:49
Dataflow Directional Tag Classes.
static void Profile(llvm::FoldingSetNodeID &ID, QualType T, llvm::ImmutableList< SVal > L)
const llvm::APSInt & getMinValue(const llvm::APSInt &v)
unsigned getIntWidth(QualType T) const
APSIntType getAPSIntType(QualType T) const
Returns the type of the APSInt used to store values of the given QualType.
void Profile(llvm::FoldingSetNodeID &ID)
Represents a base class of a C++ class.
Definition: DeclCXX.h:157
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:1961
BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:13161
PointerToMemberData(const DeclaratorDecl *D, llvm::ImmutableList< const CXXBaseSpecifier *> L)
const llvm::APSInt & getMaxValue(const llvm::APSInt &v)
CanQualType IntTy
Definition: ASTContext.h:973
CompoundValData(QualType t, llvm::ImmutableList< SVal > l)
llvm::ImmutableList< SVal > getEmptySValList()
CanQualType UnsignedIntTy
Definition: ASTContext.h:974