Coverage Report

Created: 2018-09-23 03:40

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
//                     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"
22
#include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
23
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
24
#include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
25
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.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
768
  CompoundValData(QualType t, llvm::ImmutableList<SVal> l) : T(t), L(l) {
48
768
    assert(NonLoc::isCompoundType(t));
49
768
  }
50
51
  using iterator = llvm::ImmutableList<SVal>::iterator;
52
53
2.40k
  iterator begin() const { return L.begin(); }
54
2.39k
  iterator end() const { return L.end(); }
55
56
  static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
57
                      llvm::ImmutableList<SVal> L);
58
59
123
  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
8.54k
      : store(st), region(r) {
69
8.54k
    assert(NonLoc::isCompoundType(r->getValueType()));
70
8.54k
  }
71
72
53.7k
  const void *getStore() const { return store.getStore(); }
73
59.6k
  const TypedValueRegion *getRegion() const { return region; }
74
75
  static void Profile(llvm::FoldingSetNodeID& ID,
76
                      const StoreRef &store,
77
                      const TypedValueRegion *region);
78
79
33.8k
  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:
87
  PointerToMemberData(const DeclaratorDecl *D,
88
                      llvm::ImmutableList<const CXXBaseSpecifier *> L)
89
20
      : D(D), L(L) {}
90
91
  using iterator = llvm::ImmutableList<const CXXBaseSpecifier *>::iterator;
92
93
7
  iterator begin() const { return L.begin(); }
94
7
  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
4
  void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, D, L); }
100
49
  const DeclaratorDecl *getDeclaratorDecl() const {return D;}
101
102
12
  llvm::ImmutableList<const CXXBaseSpecifier *> getCXXBaseList() const {
103
12
    return L;
104
12
  }
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
9.75k
        CXXBaseListFactory(Alloc) {}
132
133
  ~BasicValueFactory();
134
135
25
  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
695k
  APSIntType getAPSIntType(QualType T) const {
143
695k
    assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
144
695k
    return APSIntType(Ctx.getIntWidth(T),
145
695k
                      !T->isSignedIntegerOrEnumerationType());
146
695k
  }
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
0
                              const llvm::APSInt& From) {
152
0
    APSIntType TargetType(To);
153
0
    if (TargetType == APSIntType(From))
154
0
      return From;
155
0
156
0
    return getValue(TargetType.convert(From));
157
0
  }
158
159
28.7k
  const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
160
28.7k
    APSIntType TargetType = getAPSIntType(T);
161
28.7k
    if (TargetType == APSIntType(From))
162
26.1k
      return From;
163
2.59k
164
2.59k
    return getValue(TargetType.convert(From));
165
2.59k
  }
166
167
438
  const llvm::APSInt &getIntValue(uint64_t X, bool isUnsigned) {
168
438
    QualType T = isUnsigned ? 
Ctx.UnsignedIntTy404
:
Ctx.IntTy34
;
169
438
    return getValue(X, T);
170
438
  }
171
172
28.1k
  const llvm::APSInt &getMaxValue(const llvm::APSInt &v) {
173
28.1k
    return getValue(APSIntType(v).getMaxValue());
174
28.1k
  }
175
176
28.2k
  const llvm::APSInt &getMinValue(const llvm::APSInt &v) {
177
28.2k
    return getValue(APSIntType(v).getMinValue());
178
28.2k
  }
179
180
83.8k
  const llvm::APSInt &getMaxValue(QualType T) {
181
83.8k
    return getValue(getAPSIntType(T).getMaxValue());
182
83.8k
  }
183
184
121k
  const llvm::APSInt &getMinValue(QualType T) {
185
121k
    return getValue(getAPSIntType(T).getMinValue());
186
121k
  }
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
206
  const llvm::APSInt &getZeroWithTypeSize(QualType T) {
201
206
    assert(T->isScalarType());
202
206
    return getValue(0, Ctx.getTypeSize(T), true);
203
206
  }
204
205
4.03k
  const llvm::APSInt &getZeroWithPtrWidth(bool isUnsigned = true) {
206
4.03k
    return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
207
4.03k
  }
208
209
94
  const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) {
210
94
    return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
211
94
  }
212
213
398k
  const llvm::APSInt &getTruthValue(bool b, QualType T) {
214
398k
    return getValue(b ? 
1386k
:
012.0k
, Ctx.getIntWidth(T),
215
398k
                    T->isUnsignedIntegerOrEnumerationType());
216
398k
  }
217
218
12.2k
  const llvm::APSInt &getTruthValue(bool b) {
219
12.2k
    return getTruthValue(b, Ctx.getLogicalOperationType());
220
12.2k
  }
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
878
  llvm::ImmutableList<SVal> getEmptySValList() {
233
878
    return SValListFactory.getEmptyList();
234
878
  }
235
236
1.93k
  llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) {
237
1.93k
    return SValListFactory.add(X, L);
238
1.93k
  }
239
240
  llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() {
241
    return CXXBaseListFactory.getEmptyList();
242
  }
243
244
  llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase(
245
      const CXXBaseSpecifier *CBS,
246
28
      llvm::ImmutableList<const CXXBaseSpecifier *> L) {
247
28
    return CXXBaseListFactory.add(CBS, L);
248
28
  }
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