Coverage Report

Created: 2021-09-21 08:58

/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
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
909
  CompoundValData(QualType t, llvm::ImmutableList<SVal> l) : T(t), L(l) {
47
909
    assert(NonLoc::isCompoundType(t));
48
909
  }
49
50
  using iterator = llvm::ImmutableList<SVal>::iterator;
51
52
3.25k
  iterator begin() const { return L.begin(); }
53
3.25k
  iterator end() const { return L.end(); }
54
55
6
  QualType getType() const { return T; }
56
57
  static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
58
                      llvm::ImmutableList<SVal> L);
59
60
251
  void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); }
61
};
62
63
class LazyCompoundValData : public llvm::FoldingSetNode {
64
  StoreRef store;
65
  const TypedValueRegion *region;
66
67
public:
68
  LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r)
69
20.0k
      : store(st), region(r) {
70
20.0k
    assert(NonLoc::isCompoundType(r->getValueType()));
71
20.0k
  }
72
73
81.9k
  const void *getStore() const { return store.getStore(); }
74
104k
  const TypedValueRegion *getRegion() const { return region; }
75
76
  static void Profile(llvm::FoldingSetNodeID& ID,
77
                      const StoreRef &store,
78
                      const TypedValueRegion *region);
79
80
42.7k
  void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); }
81
};
82
83
class PointerToMemberData : public llvm::FoldingSetNode {
84
  const NamedDecl *D;
85
  llvm::ImmutableList<const CXXBaseSpecifier *> L;
86
87
public:
88
  PointerToMemberData(const NamedDecl *D,
89
                      llvm::ImmutableList<const CXXBaseSpecifier *> L)
90
23
      : D(D), L(L) {}
91
92
  using iterator = llvm::ImmutableList<const CXXBaseSpecifier *>::iterator;
93
94
10
  iterator begin() const { return L.begin(); }
95
10
  iterator end() const { return L.end(); }
96
97
  static void Profile(llvm::FoldingSetNodeID &ID, const NamedDecl *D,
98
                      llvm::ImmutableList<const CXXBaseSpecifier *> L);
99
100
11
  void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, D, L); }
101
64
  const NamedDecl *getDeclaratorDecl() const { return D; }
102
103
18
  llvm::ImmutableList<const CXXBaseSpecifier *> getCXXBaseList() const {
104
18
    return L;
105
18
  }
106
};
107
108
class BasicValueFactory {
109
  using APSIntSetTy =
110
      llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt>>;
111
112
  ASTContext &Ctx;
113
  llvm::BumpPtrAllocator& BPAlloc;
114
115
  APSIntSetTy APSIntSet;
116
  void *PersistentSVals = nullptr;
117
  void *PersistentSValPairs = nullptr;
118
119
  llvm::ImmutableList<SVal>::Factory SValListFactory;
120
  llvm::ImmutableList<const CXXBaseSpecifier *>::Factory CXXBaseListFactory;
121
  llvm::FoldingSet<CompoundValData>  CompoundValDataSet;
122
  llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet;
123
  llvm::FoldingSet<PointerToMemberData> PointerToMemberDataSet;
124
125
  // This is private because external clients should use the factory
126
  // method that takes a QualType.
127
  const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
128
129
public:
130
  BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator &Alloc)
131
      : Ctx(ctx), BPAlloc(Alloc), SValListFactory(Alloc),
132
14.4k
        CXXBaseListFactory(Alloc) {}
133
134
  ~BasicValueFactory();
135
136
53
  ASTContext &getContext() const { return Ctx; }
137
138
  const llvm::APSInt& getValue(const llvm::APSInt& X);
139
  const llvm::APSInt& getValue(const llvm::APInt& X, bool isUnsigned);
140
  const llvm::APSInt& getValue(uint64_t X, QualType T);
141
142
  /// Returns the type of the APSInt used to store values of the given QualType.
143
923k
  APSIntType getAPSIntType(QualType T) const {
144
    // For the purposes of the analysis and constraints, we treat atomics
145
    // as their underlying types.
146
923k
    if (const AtomicType *AT = T->getAs<AtomicType>()) {
147
7
      T = AT->getValueType();
148
7
    }
149
150
923k
    assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
151
0
    return APSIntType(Ctx.getIntWidth(T),
152
923k
                      !T->isSignedIntegerOrEnumerationType());
153
923k
  }
154
155
  /// Convert - Create a new persistent APSInt with the same value as 'From'
156
  ///  but with the bitwidth and signedness of 'To'.
157
  const llvm::APSInt &Convert(const llvm::APSInt& To,
158
0
                              const llvm::APSInt& From) {
159
0
    APSIntType TargetType(To);
160
0
    if (TargetType == APSIntType(From))
161
0
      return From;
162
0
163
0
    return getValue(TargetType.convert(From));
164
0
  }
165
166
28.6k
  const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
167
28.6k
    APSIntType TargetType = getAPSIntType(T);
168
28.6k
    return Convert(TargetType, From);
169
28.6k
  }
170
171
177k
  const llvm::APSInt &Convert(APSIntType TargetType, const llvm::APSInt &From) {
172
177k
    if (TargetType == APSIntType(From))
173
104k
      return From;
174
175
73.5k
    return getValue(TargetType.convert(From));
176
177k
  }
177
178
717
  const llvm::APSInt &getIntValue(uint64_t X, bool isUnsigned) {
179
717
    QualType T = isUnsigned ? 
Ctx.UnsignedIntTy604
:
Ctx.IntTy113
;
180
717
    return getValue(X, T);
181
717
  }
182
183
29.8k
  const llvm::APSInt &getMaxValue(const llvm::APSInt &v) {
184
29.8k
    return getValue(APSIntType(v).getMaxValue());
185
29.8k
  }
186
187
29.8k
  const llvm::APSInt &getMinValue(const llvm::APSInt &v) {
188
29.8k
    return getValue(APSIntType(v).getMinValue());
189
29.8k
  }
190
191
202k
  const llvm::APSInt &getMaxValue(QualType T) {
192
202k
    return getMaxValue(getAPSIntType(T));
193
202k
  }
194
195
204k
  const llvm::APSInt &getMinValue(QualType T) {
196
204k
    return getMinValue(getAPSIntType(T));
197
204k
  }
198
199
202k
  const llvm::APSInt &getMaxValue(APSIntType T) {
200
202k
    return getValue(T.getMaxValue());
201
202k
  }
202
203
204k
  const llvm::APSInt &getMinValue(APSIntType T) {
204
204k
    return getValue(T.getMinValue());
205
204k
  }
206
207
0
  const llvm::APSInt &Add1(const llvm::APSInt &V) {
208
0
    llvm::APSInt X = V;
209
0
    ++X;
210
0
    return getValue(X);
211
0
  }
212
213
0
  const llvm::APSInt &Sub1(const llvm::APSInt &V) {
214
0
    llvm::APSInt X = V;
215
0
    --X;
216
0
    return getValue(X);
217
0
  }
218
219
86.5k
  const llvm::APSInt &getZeroWithTypeSize(QualType T) {
220
86.5k
    assert(T->isScalarType());
221
0
    return getValue(0, Ctx.getTypeSize(T), true);
222
86.5k
  }
223
224
8.60k
  const llvm::APSInt &getZeroWithPtrWidth(bool isUnsigned = true) {
225
8.60k
    return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
226
8.60k
  }
227
228
151
  const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) {
229
151
    return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
230
151
  }
231
232
572k
  const llvm::APSInt &getTruthValue(bool b, QualType T) {
233
572k
    return getValue(b ? 
1556k
:
015.7k
, Ctx.getIntWidth(T),
234
572k
                    T->isUnsignedIntegerOrEnumerationType());
235
572k
  }
236
237
17.0k
  const llvm::APSInt &getTruthValue(bool b) {
238
17.0k
    return getTruthValue(b, Ctx.getLogicalOperationType());
239
17.0k
  }
240
241
  const CompoundValData *getCompoundValData(QualType T,
242
                                            llvm::ImmutableList<SVal> Vals);
243
244
  const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store,
245
                                            const TypedValueRegion *region);
246
247
  const PointerToMemberData *
248
  getPointerToMemberData(const NamedDecl *ND,
249
                         llvm::ImmutableList<const CXXBaseSpecifier *> L);
250
251
1.14k
  llvm::ImmutableList<SVal> getEmptySValList() {
252
1.14k
    return SValListFactory.getEmptyList();
253
1.14k
  }
254
255
2.17k
  llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) {
256
2.17k
    return SValListFactory.add(X, L);
257
2.17k
  }
258
259
0
  llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() {
260
0
    return CXXBaseListFactory.getEmptyList();
261
0
  }
262
263
  llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase(
264
      const CXXBaseSpecifier *CBS,
265
31
      llvm::ImmutableList<const CXXBaseSpecifier *> L) {
266
31
    return CXXBaseListFactory.add(CBS, L);
267
31
  }
268
269
  const PointerToMemberData *
270
  accumCXXBase(llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
271
               const nonloc::PointerToMember &PTM, const clang::CastKind &kind);
272
273
  const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op,
274
                                     const llvm::APSInt& V1,
275
                                     const llvm::APSInt& V2);
276
277
  const std::pair<SVal, uintptr_t>&
278
  getPersistentSValWithData(const SVal& V, uintptr_t Data);
279
280
  const std::pair<SVal, SVal>&
281
  getPersistentSValPair(const SVal& V1, const SVal& V2);
282
283
  const SVal* getPersistentSVal(SVal X);
284
};
285
286
} // namespace ento
287
288
} // namespace clang
289
290
#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_BASICVALUEFACTORY_H