Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h
Line
Count
Source (jump to first uncovered line)
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
38
namespace ento {
39
40
class CompoundValData : public llvm::FoldingSetNode {
41
  QualType T;
42
  llvm::ImmutableList<SVal> L;
43
44
public:
45
991
  CompoundValData(QualType t, llvm::ImmutableList<SVal> l) : T(t), L(l) {
46
991
    assert(NonLoc::isCompoundType(t));
47
991
  }
48
49
  using iterator = llvm::ImmutableList<SVal>::iterator;
50
51
3.46k
  iterator begin() const { return L.begin(); }
52
3.45k
  iterator end() const { return L.end(); }
53
54
126
  QualType getType() const { return T; }
55
56
  static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
57
                      llvm::ImmutableList<SVal> L);
58
59
248
  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:
67
  LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r)
68
20.2k
      : store(st), region(r) {
69
20.2k
    assert(NonLoc::isCompoundType(r->getValueType()));
70
20.2k
  }
71
72
81.2k
  const void *getStore() const { return store.getStore(); }
73
104k
  const TypedValueRegion *getRegion() const { return region; }
74
75
  static void Profile(llvm::FoldingSetNodeID& ID,
76
                      const StoreRef &store,
77
                      const TypedValueRegion *region);
78
79
42.0k
  void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); }
80
};
81
82
class PointerToMemberData : public llvm::FoldingSetNode {
83
  const NamedDecl *D;
84
  llvm::ImmutableList<const CXXBaseSpecifier *> L;
85
86
public:
87
  PointerToMemberData(const NamedDecl *D,
88
                      llvm::ImmutableList<const CXXBaseSpecifier *> L)
89
23
      : D(D), L(L) {}
90
91
  using iterator = llvm::ImmutableList<const CXXBaseSpecifier *>::iterator;
92
93
10
  iterator begin() const { return L.begin(); }
94
10
  iterator end() const { return L.end(); }
95
96
  static void Profile(llvm::FoldingSetNodeID &ID, const NamedDecl *D,
97
                      llvm::ImmutableList<const CXXBaseSpecifier *> L);
98
99
11
  void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, D, L); }
100
64
  const NamedDecl *getDeclaratorDecl() const { return D; }
101
102
18
  llvm::ImmutableList<const CXXBaseSpecifier *> getCXXBaseList() const {
103
18
    return L;
104
18
  }
105
};
106
107
class BasicValueFactory {
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
14.8k
        CXXBaseListFactory(Alloc) {}
132
133
  ~BasicValueFactory();
134
135
53
  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.
142
929k
  APSIntType getAPSIntType(QualType T) const {
143
    // For the purposes of the analysis and constraints, we treat atomics
144
    // as their underlying types.
145
929k
    if (const AtomicType *AT = T->getAs<AtomicType>()) {
146
7
      T = AT->getValueType();
147
7
    }
148
149
929k
    assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
150
0
    return APSIntType(Ctx.getIntWidth(T),
151
929k
                      !T->isSignedIntegerOrEnumerationType());
152
929k
  }
153
154
  /// Convert - Create a new persistent APSInt with the same value as 'From'
155
  ///  but with the bitwidth and signedness of 'To'.
156
  const llvm::APSInt &Convert(const llvm::APSInt& To,
157
0
                              const llvm::APSInt& From) {
158
0
    APSIntType TargetType(To);
159
0
    if (TargetType == APSIntType(From))
160
0
      return From;
161
0
162
0
    return getValue(TargetType.convert(From));
163
0
  }
164
165
28.5k
  const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
166
28.5k
    APSIntType TargetType = getAPSIntType(T);
167
28.5k
    return Convert(TargetType, From);
168
28.5k
  }
169
170
177k
  const llvm::APSInt &Convert(APSIntType TargetType, const llvm::APSInt &From) {
171
177k
    if (TargetType == APSIntType(From))
172
104k
      return From;
173
174
73.5k
    return getValue(TargetType.convert(From));
175
177k
  }
176
177
917
  const llvm::APSInt &getIntValue(uint64_t X, bool isUnsigned) {
178
917
    QualType T = isUnsigned ? 
Ctx.UnsignedIntTy604
:
Ctx.IntTy313
;
179
917
    return getValue(X, T);
180
917
  }
181
182
29.9k
  const llvm::APSInt &getMaxValue(const llvm::APSInt &v) {
183
29.9k
    return getValue(APSIntType(v).getMaxValue());
184
29.9k
  }
185
186
29.9k
  const llvm::APSInt &getMinValue(const llvm::APSInt &v) {
187
29.9k
    return getValue(APSIntType(v).getMinValue());
188
29.9k
  }
189
190
204k
  const llvm::APSInt &getMaxValue(QualType T) {
191
204k
    return getMaxValue(getAPSIntType(T));
192
204k
  }
193
194
206k
  const llvm::APSInt &getMinValue(QualType T) {
195
206k
    return getMinValue(getAPSIntType(T));
196
206k
  }
197
198
204k
  const llvm::APSInt &getMaxValue(APSIntType T) {
199
204k
    return getValue(T.getMaxValue());
200
204k
  }
201
202
206k
  const llvm::APSInt &getMinValue(APSIntType T) {
203
206k
    return getValue(T.getMinValue());
204
206k
  }
205
206
0
  const llvm::APSInt &Add1(const llvm::APSInt &V) {
207
0
    llvm::APSInt X = V;
208
0
    ++X;
209
0
    return getValue(X);
210
0
  }
211
212
0
  const llvm::APSInt &Sub1(const llvm::APSInt &V) {
213
0
    llvm::APSInt X = V;
214
0
    --X;
215
0
    return getValue(X);
216
0
  }
217
218
86.9k
  const llvm::APSInt &getZeroWithTypeSize(QualType T) {
219
86.9k
    assert(T->isScalarType());
220
0
    return getValue(0, Ctx.getTypeSize(T), true);
221
86.9k
  }
222
223
8.68k
  const llvm::APSInt &getZeroWithPtrWidth(bool isUnsigned = true) {
224
8.68k
    return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
225
8.68k
  }
226
227
151
  const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) {
228
151
    return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
229
151
  }
230
231
580k
  const llvm::APSInt &getTruthValue(bool b, QualType T) {
232
580k
    return getValue(b ? 
1564k
:
015.9k
, Ctx.getIntWidth(T),
233
580k
                    T->isUnsignedIntegerOrEnumerationType());
234
580k
  }
235
236
16.9k
  const llvm::APSInt &getTruthValue(bool b) {
237
16.9k
    return getTruthValue(b, Ctx.getLogicalOperationType());
238
16.9k
  }
239
240
  const CompoundValData *getCompoundValData(QualType T,
241
                                            llvm::ImmutableList<SVal> Vals);
242
243
  const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store,
244
                                            const TypedValueRegion *region);
245
246
  const PointerToMemberData *
247
  getPointerToMemberData(const NamedDecl *ND,
248
                         llvm::ImmutableList<const CXXBaseSpecifier *> L);
249
250
1.22k
  llvm::ImmutableList<SVal> getEmptySValList() {
251
1.22k
    return SValListFactory.getEmptyList();
252
1.22k
  }
253
254
2.32k
  llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) {
255
2.32k
    return SValListFactory.add(X, L);
256
2.32k
  }
257
258
0
  llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() {
259
0
    return CXXBaseListFactory.getEmptyList();
260
0
  }
261
262
  llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase(
263
      const CXXBaseSpecifier *CBS,
264
31
      llvm::ImmutableList<const CXXBaseSpecifier *> L) {
265
31
    return CXXBaseListFactory.add(CBS, L);
266
31
  }
267
268
  const PointerToMemberData *
269
  accumCXXBase(llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
270
               const nonloc::PointerToMember &PTM, const clang::CastKind &kind);
271
272
  const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op,
273
                                     const llvm::APSInt& V1,
274
                                     const llvm::APSInt& V2);
275
276
  const std::pair<SVal, uintptr_t>&
277
  getPersistentSValWithData(const SVal& V, uintptr_t Data);
278
279
  const std::pair<SVal, SVal>&
280
  getPersistentSValPair(const SVal& V1, const SVal& V2);
281
282
  const SVal* getPersistentSVal(SVal X);
283
};
284
285
} // namespace ento
286
287
} // namespace clang
288
289
#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H