Coverage Report

Created: 2018-07-19 20:53

/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
758
  CompoundValData(QualType t, llvm::ImmutableList<SVal> l) : T(t), L(l) {
48
758
    assert(NonLoc::isCompoundType(t));
49
758
  }
50
51
  using iterator = llvm::ImmutableList<SVal>::iterator;
52
53
2.39k
  iterator begin() const { return L.begin(); }
54
2.38k
  iterator end() const { return L.end(); }
55
56
  static void Profile(llvm::FoldingSetNodeID& ID, QualType T,
57
                      llvm::ImmutableList<SVal> L);
58
59
119
  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
6.73k
      : store(st), region(r) {
69
6.73k
    assert(NonLoc::isCompoundType(r->getValueType()));
70
6.73k
  }
71
72
50.8k
  const void *getStore() const { return store.getStore(); }
73
52.3k
  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.0k
  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
5
  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.37k
        CXXBaseListFactory(Alloc) {}
132
133
  ~BasicValueFactory();
134
135
20
  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
667k
  APSIntType getAPSIntType(QualType T) const {
143
667k
    assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
144
667k
    return APSIntType(Ctx.getIntWidth(T),
145
667k
                      !T->isSignedIntegerOrEnumerationType());
146
667k
  }
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.4k
  const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) {
160
28.4k
    APSIntType TargetType = getAPSIntType(T);
161
28.4k
    if (TargetType == APSIntType(From))
162
25.8k
      return From;
163
2.60k
    
164
2.60k
    return getValue(TargetType.convert(From));
165
2.60k
  }
166
167
425
  const llvm::APSInt &getIntValue(uint64_t X, bool isUnsigned) {
168
425
    QualType T = isUnsigned ? 
Ctx.UnsignedIntTy394
:
Ctx.IntTy31
;
169
425
    return getValue(X, T);
170
425
  }
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.1k
  const llvm::APSInt &getMinValue(const llvm::APSInt &v) {
177
28.1k
    return getValue(APSIntType(v).getMinValue());
178
28.1k
  }
179
180
80.9k
  const llvm::APSInt &getMaxValue(QualType T) {
181
80.9k
    return getValue(getAPSIntType(T).getMaxValue());
182
80.9k
  }
183
184
118k
  const llvm::APSInt &getMinValue(QualType T) {
185
118k
    return getValue(getAPSIntType(T).getMinValue());
186
118k
  }
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
201
  const llvm::APSInt &getZeroWithTypeSize(QualType T) {
201
201
    assert(T->isScalarType());
202
201
    return getValue(0, Ctx.getTypeSize(T), true);
203
201
  }
204
205
4.10k
  const llvm::APSInt &getZeroWithPtrWidth(bool isUnsigned = true) {
206
4.10k
    return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned);
207
4.10k
  }
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
387k
  const llvm::APSInt &getTruthValue(bool b, QualType T) {
214
387k
    return getValue(b ? 
1376k
:
011.7k
, Ctx.getIntWidth(T), true);
215
387k
  }
216
217
11.9k
  const llvm::APSInt &getTruthValue(bool b) {
218
11.9k
    return getTruthValue(b, Ctx.getLogicalOperationType());
219
11.9k
  }
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
868
  llvm::ImmutableList<SVal> getEmptySValList() {
232
868
    return SValListFactory.getEmptyList();
233
868
  }
234
235
1.90k
  llvm::ImmutableList<SVal> prependSVal(SVal X, llvm::ImmutableList<SVal> L) {
236
1.90k
    return SValListFactory.add(X, L);
237
1.90k
  }
238
239
  llvm::ImmutableList<const CXXBaseSpecifier *> getEmptyCXXBaseList() {
240
    return CXXBaseListFactory.getEmptyList();
241
  }
242
243
  llvm::ImmutableList<const CXXBaseSpecifier *> prependCXXBase(
244
      const CXXBaseSpecifier *CBS,
245
28
      llvm::ImmutableList<const CXXBaseSpecifier *> L) {
246
28
    return CXXBaseListFactory.add(CBS, L);
247
28
  }
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