clang 10.0.0git

BasicValueFactory.h
Go to the documentation of this file.
1 //==- BasicValueFactory.h - Basic values for Path Sens analysis --*- C++ -*-==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines BasicValueFactory, a class that manages the lifetime
10 // of APSInt objects and symbolic constraints used by ExprEngine
11 // and related classes.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
16 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
17
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/Type.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
25 #include "llvm/ADT/APSInt.h"
26 #include "llvm/ADT/FoldingSet.h"
27 #include "llvm/ADT/ImmutableList.h"
28 #include "llvm/ADT/iterator_range.h"
29 #include "llvm/Support/Allocator.h"
30 #include <cassert>
31 #include <cstdint>
32 #include <utility>
33
34 namespace clang {
35
36 class CXXBaseSpecifier;
37 class DeclaratorDecl;
38
39 namespace ento {
40
41 class CompoundValData : public llvm::FoldingSetNode {
42 QualType T;
43 llvm::ImmutableList<SVal> L;
44
45 public:
46 CompoundValData(QualType t, llvm::ImmutableList<SVal> l) : T(t), L(l) {
47 assert(NonLoc::isCompoundType(t));
48 }
49
50 using iterator = llvm::ImmutableList<SVal>::iterator;
51
52 iterator begin() const { return L.begin(); }
53 iterator end() const { return L.end(); }
54
55 static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
56 llvm::ImmutableList<SVal> L);
57
58 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); }
59 };
60
61 class LazyCompoundValData : public llvm::FoldingSetNode {
62 StoreRef store;
63 const TypedValueRegion *region;
64
65 public:
66 LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r)
67 : store(st), region(r) {
68 assert(NonLoc::isCompoundType(r->getValueType()));
69 }
70
71 const void *getStore() const { return store.getStore(); }
72 const TypedValueRegion *getRegion() const { return region; }
73
74 static void Profile(llvm::FoldingSetNodeID& ID,
75 const StoreRef &store,
76 const TypedValueRegion *region);
77
78 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); }
79 };
80
81 class PointerToMemberData : public llvm::FoldingSetNode {
82 const DeclaratorDecl *D;
83 llvm::ImmutableList<const CXXBaseSpecifier *> L;
84
85 public:
86 PointerToMemberData(const DeclaratorDecl *D,
87 llvm::ImmutableList<const CXXBaseSpecifier *> L)
88 : D(D), L(L) {}
89
90 using iterator = llvm::ImmutableList<const CXXBaseSpecifier *>::iterator;
91
92 iterator begin() const { return L.begin(); }
93 iterator end() const { return L.end(); }
94
95 static void Profile(llvm::FoldingSetNodeID& ID, const DeclaratorDecl *D,
96 llvm::ImmutableList<const CXXBaseSpecifier *> L);
97
98 void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, D, L); }
99 const DeclaratorDecl *getDeclaratorDecl() const {return D;}
100
101 llvm::ImmutableList<const CXXBaseSpecifier *> getCXXBaseList() const {
102 return L;
103 }
104 };
105
106 class BasicValueFactory {
107 using APSIntSetTy =
108 llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt>>;
109
110 ASTContext &Ctx;
111 llvm::BumpPtrAllocator& BPAlloc;
112
113 APSIntSetTy APSIntSet;
114 void *PersistentSVals = nullptr;
115 void *PersistentSValPairs = nullptr;
116
117 llvm::ImmutableList<SVal>::Factory SValListFactory;
118 llvm::ImmutableList<const CXXBaseSpecifier *>::Factory CXXBaseListFactory;
119 llvm::FoldingSet<CompoundValData> CompoundValDataSet;
120 llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet;
121 llvm::FoldingSet<PointerToMemberData> PointerToMemberDataSet;
122
123 // This is private because external clients should use the factory
124 // method that takes a QualType.
125 const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
126
127 public:
128 BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
129 : Ctx(ctx), BPAlloc(Alloc), SValListFactory(Alloc),
130 CXXBaseListFactory(Alloc) {}
131
132 ~BasicValueFactory();
133
134 ASTContext &getContext() const { return Ctx; }
135
136 const llvm::APSInt& getValue(const llvm::APSInt& X);
137 const llvm::APSInt& getValue(const llvm::APInt& X, bool isUnsigned);
138 const llvm::APSInt& getValue(uint64_t X, QualType T);
139
140 /// Returns the type of the APSInt used to store values of the given QualType.
141 APSIntType getAPSIntType(QualType T) const {
142 assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
143 return APSIntType(Ctx.getIntWidth(T),
144 !T->isSignedIntegerOrEnumerationType());
145 }
146
147 /// Convert - Create a new persistent APSInt with the same value as 'From'
148 /// but with the bitwidth and signedness of 'To'.
149 const llvm::APSInt &Convert(const llvm::APSInt& To,
150 const llvm::APSInt& From) {
151 APSIntType TargetType(To);
152 if (TargetType == APSIntType(From))
153 return From;
154
155 return getValue(TargetType.convert(From));
156 }
157
158 const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
159 APSIntType TargetType = getAPSIntType(T);
160 if (TargetType == APSIntType(From))
161 return From;
162
163 return getValue(TargetType.convert(From));
164 }
165
166 const llvm::APSInt &getIntValue(uint64_t X, bool isUnsigned) {
167 QualType T = isUnsigned ? Ctx.UnsignedIntTy : Ctx.IntTy;
168 return getValue(X, T);
169 }
170
171 const llvm::APSInt &getMaxValue(const llvm::APSInt &v) {
172 return getValue(APSIntType(v).getMaxValue());
173 }
174
175 const llvm::APSInt &getMinValue(const llvm::APSInt &v) {
176 return getValue(APSIntType(v).getMinValue());
177 }
178
179 const llvm::APSInt &getMaxValue(QualType T) {
180 return getValue(getAPSIntType(T).getMaxValue());
181 }
182
183 const llvm::APSInt &getMinValue(QualType T) {
184 return getValue(getAPSIntType(T).getMinValue());
185 }
186
187 const llvm::APSInt &Add1(const llvm::APSInt &V) {
188 llvm::APSInt X = V;
189 ++X;
190 return getValue(X);
191 }
192
193 const llvm::APSInt &Sub1(const llvm::APSInt &V) {
194 llvm::APSInt X = V;
195 --X;
196 return getValue(X);
197 }
198
199 const llvm::APSInt &getZeroWithTypeSize(QualType T) {
200 assert(T->isScalarType());
201 return getValue(0, Ctx.getTypeSize(T), true);
202 }
203
204 const llvm::APSInt &getZeroWithPtrWidth(bool isUnsigned = true) {
205 return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
206 }
207
208 const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) {
209 return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
210 }
211
212 const llvm::APSInt &getTruthValue(bool b, QualType T) {
213 return getValue(b ? 1 : 0, Ctx.getIntWidth(T),
214 T->isUnsignedIntegerOrEnumerationType());
215 }
216
217 const llvm::APSInt &getTruthValue(bool b) {
218 return getTruthValue(b, Ctx.getLogicalOperationType());
219 }
220
221 const CompoundValData *getCompoundValData(QualType T,
222 llvm::ImmutableList<SVal> Vals);
223
224 const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store,
225 const TypedValueRegion *region);
226
227 const PointerToMemberData *getPointerToMemberData(
228 const DeclaratorDecl *DD,
229 llvm::ImmutableList<const CXXBaseSpecifier *> L);
230
231 llvm::ImmutableList<SVal> getEmptySValList() {
232 return SValListFactory.getEmptyList();
233 }
234
235 llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) {
236 return SValListFactory.add(X, L);
237 }
238
239 llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() {
240 return CXXBaseListFactory.getEmptyList();
241 }
242
243 llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase(
244 const CXXBaseSpecifier *CBS,
245 llvm::ImmutableList<const CXXBaseSpecifier *> L) {
246 return CXXBaseListFactory.add(CBS, L);
247 }
248
249 const PointerToMemberData *accumCXXBase(
250 llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
251 const nonloc::PointerToMember &PTM);
252
253 const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op,
254 const llvm::APSInt& V1,
255 const llvm::APSInt& V2);
256
257 const std::pair<SVal, uintptr_t>&
258 getPersistentSValWithData(const SVal& V, uintptr_t Data);
259
260 const std::pair<SVal, SVal>&
261 getPersistentSValPair(const SVal& V1, const SVal& V2);
262
263 const SVal* getPersistentSVal(SVal X);
264 };
265
266 } // namespace ento
267
268 } // namespace clang
269
270 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H
StoreRef.h
clang::Builtin::ID
ID
Definition: Builtins.h:46
ASTContext.h
Defines the clang::ASTContext interface.
ento
clang::ento::BasicValueFactory::Convert
const llvm::APSInt & Convert(QualType T, const llvm::APSInt &From)
Definition: BasicValueFactory.h:158
SVals.h
clang::ento::TypedValueRegion
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:530
clang::ASTContext::VoidPtrTy
CanQualType VoidPtrTy
Definition: ASTContext.h:1044
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:654
clang::ento::LazyCompoundValData
Definition: BasicValueFactory.h:61
clang::Type::isUnsignedIntegerOrEnumerationType
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
Definition: Type.cpp:1984
clang::ento::StoreRef::getStore
Store getStore() const
Definition: StoreRef.h:46
clang::ento::PointerToMemberData::getCXXBaseList
llvm::ImmutableList< const CXXBaseSpecifier * > getCXXBaseList() const
Definition: BasicValueFactory.h:101
clang::ento::BasicValueFactory::getMaxValue
const llvm::APSInt & getMaxValue(QualType T)
Definition: BasicValueFactory.h:179
clang::ento::LazyCompoundValData::getStore
const void * getStore() const
Definition: BasicValueFactory.h:71
Type.h
C Language Family Type Representation.
clang::ento::BasicValueFactory::getTruthValue
const llvm::APSInt & getTruthValue(bool b)
Definition: BasicValueFactory.h:217
clang::ento::TypedValueRegion::getValueType
virtual QualType getValueType() const =0
clang::ento::CompoundValData::begin
iterator begin() const
Definition: BasicValueFactory.h:52
clang::ento::BasicValueFactory::getZeroWithTypeSize
const llvm::APSInt & getZeroWithTypeSize(QualType T)
Definition: BasicValueFactory.h:199
clang::ASTContext::getLogicalOperationType
QualType getLogicalOperationType() const
The result type of logical operations, &#39;<&#39;, &#39;>&#39;, &#39;!=&#39;, etc.
Definition: ASTContext.h:1810
MemRegion.h
clang::ento::LazyCompoundValData::LazyCompoundValData
LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r)
Definition: BasicValueFactory.h:66
clang::ento::BasicValueFactory::Add1
const llvm::APSInt & Add1(const llvm::APSInt &V)
Definition: BasicValueFactory.h:187
clang::ento::BasicValueFactory::getTruthValue
const llvm::APSInt & getTruthValue(bool b, QualType T)
Definition: BasicValueFactory.h:212
clang::ento::BasicValueFactory
Definition: BasicValueFactory.h:106
clang::ento::PointerToMemberData::getDeclaratorDecl
const DeclaratorDecl * getDeclaratorDecl() const
Definition: BasicValueFactory.h:99
clang::ento::CompoundValData
Definition: BasicValueFactory.h:41
clang::ento::BasicValueFactory::getMinValue
const llvm::APSInt & getMinValue(QualType T)
Definition: BasicValueFactory.h:183
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
clang::ento::nonloc::PointerToMember
Value representing pointer-to-member.
Definition: SVals.h:524
APSIntType.h
clang::ento::NonLoc::isCompoundType
static bool isCompoundType(QualType T)
Definition: SVals.h:307
clang::Type::isIntegralOrEnumerationType
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:6881
clang::ento::BasicValueFactory::Convert
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...
Definition: BasicValueFactory.h:149
clang::ento::PointerToMemberData
Definition: BasicValueFactory.h:81
clang::ento::BasicValueFactory::prependSVal
llvm::ImmutableList< SVal > prependSVal(SVal X, llvm::ImmutableList< SVal > L)
Definition: BasicValueFactory.h:235
clang::ento::PointerToMemberData::Profile
void Profile(llvm::FoldingSetNodeID &ID)
Definition: BasicValueFactory.h:98
clang::ento::Loc::isLocType
static bool isLocType(QualType T)
Definition: SVals.h:329
clang::BinaryOperatorKind
BinaryOperatorKind
Definition: OperationKinds.h:25
clang::ento::PointerToMemberData::begin
iterator begin() const
Definition: BasicValueFactory.h:92
clang::ento::APSIntType
A record of the "type" of an APSInt, used for conversions.
Definition: APSIntType.h:19
clang::Type::isScalarType
bool isScalarType() const
Definition: Type.h:6866
clang::ento::BasicValueFactory::getIntValue
const llvm::APSInt & getIntValue(uint64_t X, bool isUnsigned)
Definition: BasicValueFactory.h:166
clang::DeclaratorDecl
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:671
clang::ento::CompoundValData::Profile
void Profile(llvm::FoldingSetNodeID &ID)
Definition: BasicValueFactory.h:58
clang::ento::BasicValueFactory::getIntWithPtrWidth
const llvm::APSInt & getIntWithPtrWidth(uint64_t X, bool isUnsigned)
Definition: BasicValueFactory.h:208
V
#define V(N, I)
Definition: ASTContext.h:2941
uintptr_t
__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-base.h:62
clang::ento::BasicValueFactory::getContext
ASTContext & getContext() const
Definition: BasicValueFactory.h:134
clang::ento::StoreRef
Definition: StoreRef.h:29
getValue
static SVal getValue(SVal val, SValBuilder &svalBuilder)
Definition: ArrayBoundCheckerV2.cpp:277
clang::ento::CompoundValData::end
iterator end() const
Definition: BasicValueFactory.h:53
clang::ento::LazyCompoundValData::getRegion
const TypedValueRegion * getRegion() const
Definition: BasicValueFactory.h:72
v
do v
Definition: arm_acle.h:64
clang::ento::BasicValueFactory::getEmptyCXXBaseList
llvm::ImmutableList< const CXXBaseSpecifier * > getEmptyCXXBaseList()
Definition: BasicValueFactory.h:239
APSInt
llvm::APSInt APSInt
Definition: ByteCodeEmitter.cpp:18
clang::ento::BasicValueFactory::prependCXXBase
llvm::ImmutableList< const CXXBaseSpecifier * > prependCXXBase(const CXXBaseSpecifier *CBS, llvm::ImmutableList< const CXXBaseSpecifier *> L)
Definition: BasicValueFactory.h:243
clang::ento::SVal
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:75
clang::Type::isSignedIntegerOrEnumerationType
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
Definition: Type.cpp:1944
clang::ento::BasicValueFactory::Sub1
const llvm::APSInt & Sub1(const llvm::APSInt &V)
Definition: BasicValueFactory.h:193
clang::ento::BasicValueFactory::getZeroWithPtrWidth
const llvm::APSInt & getZeroWithPtrWidth(bool isUnsigned=true)
Definition: BasicValueFactory.h:204
clang::interp::APInt
llvm::APInt APInt
Definition: Integral.h:27
clang::ento::APSIntType::convert
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:48
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::ento::CompoundValData::Profile
static void Profile(llvm::FoldingSetNodeID &ID, QualType T, llvm::ImmutableList< SVal > L)
Definition: BasicValueFactory.cpp:31
clang::ento::BasicValueFactory::getMinValue
const llvm::APSInt & getMinValue(const llvm::APSInt &v)
Definition: BasicValueFactory.h:175
clang::ASTContext::getIntWidth
unsigned getIntWidth(QualType T) const
Definition: ASTContext.cpp:9465
clang::ento::BasicValueFactory::getAPSIntType
APSIntType getAPSIntType(QualType T) const
Returns the type of the APSInt used to store values of the given QualType.
Definition: BasicValueFactory.h:141
clang::ento::LazyCompoundValData::Profile
void Profile(llvm::FoldingSetNodeID &ID)
Definition: BasicValueFactory.h:78
clang::CXXBaseSpecifier
Represents a base class of a C++ class.
Definition: DeclCXX.h:145
clang::ASTContext::getTypeSize
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2104
clang::ento::BasicValueFactory::BasicValueFactory
BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
Definition: BasicValueFactory.h:128
X
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:14780
clang::ento::PointerToMemberData::PointerToMemberData
PointerToMemberData(const DeclaratorDecl *D, llvm::ImmutableList< const CXXBaseSpecifier *> L)
Definition: BasicValueFactory.h:86
Expr.h
iterator
clang::ento::BasicValueFactory::getMaxValue
const llvm::APSInt & getMaxValue(const llvm::APSInt &v)
Definition: BasicValueFactory.h:171
clang::ASTContext::IntTy
CanQualType IntTy
Definition: ASTContext.h:1025
clang::ento::CompoundValData::CompoundValData
CompoundValData(QualType t, llvm::ImmutableList< SVal > l)
Definition: BasicValueFactory.h:46
clang::ento::BasicValueFactory::getEmptySValList
llvm::ImmutableList< SVal > getEmptySValList()
Definition: BasicValueFactory.h:231
clang::ento::PointerToMemberData::end
iterator end() const
Definition: BasicValueFactory.h:93
clang::ASTContext::UnsignedIntTy
CanQualType UnsignedIntTy
Definition: ASTContext.h:1026