clang  8.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"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/Type.h"
26 #include "llvm/ADT/APSInt.h"
27 #include "llvm/ADT/FoldingSet.h"
28 #include "llvm/ADT/ImmutableList.h"
29 #include "llvm/ADT/iterator_range.h"
30 #include "llvm/Support/Allocator.h"
31 #include <cassert>
32 #include <cstdint>
33 #include <utility>
34 
35 namespace clang {
36 
37 class CXXBaseSpecifier;
38 class DeclaratorDecl;
39 
40 namespace ento {
41 
42 class CompoundValData : public llvm::FoldingSetNode {
43  QualType T;
44  llvm::ImmutableList<SVal> L;
45 
46 public:
47  CompoundValData(QualType t, llvm::ImmutableList<SVal> l) : T(t), L(l) {
48  assert(NonLoc::isCompoundType(t));
49  }
50 
51  using iterator = llvm::ImmutableList<SVal>::iterator;
52 
53  iterator begin() const { return L.begin(); }
54  iterator end() const { return L.end(); }
55 
56  static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
57  llvm::ImmutableList<SVal> L);
58 
59  void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); }
60 };
61 
62 class LazyCompoundValData : public llvm::FoldingSetNode {
63  StoreRef store;
64  const TypedValueRegion *region;
65 
66 public:
68  : store(st), region(r) {
70  }
71 
72  const void *getStore() const { return store.getStore(); }
73  const TypedValueRegion *getRegion() const { return region; }
74 
75  static void Profile(llvm::FoldingSetNodeID& ID,
76  const StoreRef &store,
77  const TypedValueRegion *region);
78 
79  void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); }
80 };
81 
82 class PointerToMemberData : public llvm::FoldingSetNode {
83  const DeclaratorDecl *D;
84  llvm::ImmutableList<const CXXBaseSpecifier *> L;
85 
86 public:
88  llvm::ImmutableList<const CXXBaseSpecifier *> L)
89  : D(D), L(L) {}
90 
91  using iterator = llvm::ImmutableList<const CXXBaseSpecifier *>::iterator;
92 
93  iterator begin() const { return L.begin(); }
94  iterator end() const { return L.end(); }
95 
96  static void Profile(llvm::FoldingSetNodeID& ID, const DeclaratorDecl *D,
97  llvm::ImmutableList<const CXXBaseSpecifier *> L);
98 
99  void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, D, L); }
100  const DeclaratorDecl *getDeclaratorDecl() const {return D;}
101 
102  llvm::ImmutableList<const CXXBaseSpecifier *> getCXXBaseList() const {
103  return L;
104  }
105 };
106 
108  using APSIntSetTy =
109  llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt>>;
110 
111  ASTContext &Ctx;
112  llvm::BumpPtrAllocator& BPAlloc;
113 
114  APSIntSetTy APSIntSet;
115  void *PersistentSVals = nullptr;
116  void *PersistentSValPairs = nullptr;
117 
118  llvm::ImmutableList<SVal>::Factory SValListFactory;
119  llvm::ImmutableList<const CXXBaseSpecifier *>::Factory CXXBaseListFactory;
120  llvm::FoldingSet<CompoundValData> CompoundValDataSet;
121  llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet;
122  llvm::FoldingSet<PointerToMemberData> PointerToMemberDataSet;
123 
124  // This is private because external clients should use the factory
125  // method that takes a QualType.
126  const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
127 
128 public:
129  BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
130  : Ctx(ctx), BPAlloc(Alloc), SValListFactory(Alloc),
131  CXXBaseListFactory(Alloc) {}
132 
134 
135  ASTContext &getContext() const { return Ctx; }
136 
137  const llvm::APSInt& getValue(const llvm::APSInt& X);
138  const llvm::APSInt& getValue(const llvm::APInt& X, bool isUnsigned);
139  const llvm::APSInt& getValue(uint64_t X, QualType T);
140 
141  /// Returns the type of the APSInt used to store values of the given QualType.
143  assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
144  return APSIntType(Ctx.getIntWidth(T),
146  }
147 
148  /// Convert - Create a new persistent APSInt with the same value as 'From'
149  /// but with the bitwidth and signedness of 'To'.
150  const llvm::APSInt &Convert(const llvm::APSInt& To,
151  const llvm::APSInt& From) {
152  APSIntType TargetType(To);
153  if (TargetType == APSIntType(From))
154  return From;
155 
156  return getValue(TargetType.convert(From));
157  }
158 
159  const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
160  APSIntType TargetType = getAPSIntType(T);
161  if (TargetType == APSIntType(From))
162  return From;
163 
164  return getValue(TargetType.convert(From));
165  }
166 
167  const llvm::APSInt &getIntValue(uint64_t X, bool isUnsigned) {
168  QualType T = isUnsigned ? Ctx.UnsignedIntTy : Ctx.IntTy;
169  return getValue(X, T);
170  }
171 
172  const llvm::APSInt &getMaxValue(const llvm::APSInt &v) {
173  return getValue(APSIntType(v).getMaxValue());
174  }
175 
176  const llvm::APSInt &getMinValue(const llvm::APSInt &v) {
177  return getValue(APSIntType(v).getMinValue());
178  }
179 
180  const llvm::APSInt &getMaxValue(QualType T) {
181  return getValue(getAPSIntType(T).getMaxValue());
182  }
183 
184  const llvm::APSInt &getMinValue(QualType T) {
185  return getValue(getAPSIntType(T).getMinValue());
186  }
187 
188  const llvm::APSInt &Add1(const llvm::APSInt &V) {
189  llvm::APSInt X = V;
190  ++X;
191  return getValue(X);
192  }
193 
194  const llvm::APSInt &Sub1(const llvm::APSInt &V) {
195  llvm::APSInt X = V;
196  --X;
197  return getValue(X);
198  }
199 
200  const llvm::APSInt &getZeroWithTypeSize(QualType T) {
201  assert(T->isScalarType());
202  return getValue(0, Ctx.getTypeSize(T), true);
203  }
204 
205  const llvm::APSInt &getZeroWithPtrWidth(bool isUnsigned = true) {
206  return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
207  }
208 
209  const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) {
210  return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
211  }
212 
213  const llvm::APSInt &getTruthValue(bool b, QualType T) {
214  return getValue(b ? 1 : 0, Ctx.getIntWidth(T),
216  }
217 
218  const llvm::APSInt &getTruthValue(bool b) {
219  return getTruthValue(b, Ctx.getLogicalOperationType());
220  }
221 
222  const CompoundValData *getCompoundValData(QualType T,
223  llvm::ImmutableList<SVal> Vals);
224 
225  const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store,
226  const TypedValueRegion *region);
227 
228  const PointerToMemberData *getPointerToMemberData(
229  const DeclaratorDecl *DD,
230  llvm::ImmutableList<const CXXBaseSpecifier *> L);
231 
232  llvm::ImmutableList<SVal> getEmptySValList() {
233  return SValListFactory.getEmptyList();
234  }
235 
236  llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) {
237  return SValListFactory.add(X, L);
238  }
239 
240  llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() {
241  return CXXBaseListFactory.getEmptyList();
242  }
243 
244  llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase(
245  const CXXBaseSpecifier *CBS,
246  llvm::ImmutableList<const CXXBaseSpecifier *> L) {
247  return CXXBaseListFactory.add(CBS, L);
248  }
249 
250  const PointerToMemberData *accumCXXBase(
251  llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
252  const nonloc::PointerToMember &PTM);
253 
254  const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op,
255  const llvm::APSInt& V1,
256  const llvm::APSInt& V2);
257 
258  const std::pair<SVal, uintptr_t>&
259  getPersistentSValWithData(const SVal& V, uintptr_t Data);
260 
261  const std::pair<SVal, SVal>&
262  getPersistentSValPair(const SVal& V1, const SVal& V2);
263 
264  const SVal* getPersistentSVal(SVal X);
265 };
266 
267 } // namespace ento
268 
269 } // namespace clang
270 
271 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
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:529
CanQualType VoidPtrTy
Definition: ASTContext.h:1043
A (possibly-)qualified type.
Definition: Type.h:642
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
Definition: Type.cpp:1900
Store getStore() const
Definition: StoreRef.h:47
llvm::ImmutableList< const CXXBaseSpecifier * > getCXXBaseList() const
const llvm::APSInt & getMaxValue(QualType T)
C Language Family Type Representation.
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:1784
LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r)
const llvm::APSInt & Add1(const llvm::APSInt &V)
const llvm::APSInt & getTruthValue(bool b, QualType T)
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:154
Value representing pointer-to-member.
Definition: SVals.h:522
static bool isCompoundType(QualType T)
Definition: SVals.h:305
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:6630
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:327
BinaryOperatorKind
A record of the "type" of an APSInt, used for conversions.
Definition: APSIntType.h:20
bool isScalarType() const
Definition: Type.h:6615
const llvm::APSInt & getIntValue(uint64_t X, bool isUnsigned)
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:688
void Profile(llvm::FoldingSetNodeID &ID)
llvm::ImmutableList< SVal >::iterator iterator
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:90
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:76
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
Definition: Type.cpp:1860
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)
llvm::ImmutableList< const CXXBaseSpecifier * >::iterator iterator
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:192
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2069
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:13956
PointerToMemberData(const DeclaratorDecl *D, llvm::ImmutableList< const CXXBaseSpecifier *> L)
const llvm::APSInt & getMaxValue(const llvm::APSInt &v)
CanQualType IntTy
Definition: ASTContext.h:1024
CompoundValData(QualType t, llvm::ImmutableList< SVal > l)
llvm::ImmutableList< SVal > getEmptySValList()
CanQualType UnsignedIntTy
Definition: ASTContext.h:1025