Coverage Report

Created: 2018-07-21 08:31

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
Line
Count
Source (jump to first uncovered line)
1
//===- SymbolManager.h - Management of Symbolic Values ----------*- 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 SymbolManager, a class that manages symbolic values
11
//  created for use by ExprEngine and related classes.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMBOLMANAGER_H
16
#define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMBOLMANAGER_H
17
18
#include "clang/AST/Expr.h"
19
#include "clang/AST/Type.h"
20
#include "clang/Analysis/AnalysisDeclContext.h"
21
#include "clang/Basic/LLVM.h"
22
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
23
#include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
24
#include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
25
#include "llvm/ADT/DenseMap.h"
26
#include "llvm/ADT/DenseSet.h"
27
#include "llvm/ADT/FoldingSet.h"
28
#include "llvm/Support/Allocator.h"
29
#include <cassert>
30
31
namespace clang {
32
33
class ASTContext;
34
class Stmt;
35
36
namespace ento {
37
38
class BasicValueFactory;
39
class StoreManager;
40
41
///A symbol representing the value stored at a MemRegion.
42
class SymbolRegionValue : public SymbolData {
43
  const TypedValueRegion *R;
44
45
public:
46
  SymbolRegionValue(SymbolID sym, const TypedValueRegion *r)
47
7.19k
      : SymbolData(SymbolRegionValueKind, sym), R(r) {
48
7.19k
    assert(r);
49
7.19k
    assert(isValidTypeForSymbol(r->getValueType()));
50
7.19k
  }
51
52
1.66M
  const TypedValueRegion* getRegion() const { return R; }
53
54
145k
  static void Profile(llvm::FoldingSetNodeID& profile, const TypedValueRegion* R) {
55
145k
    profile.AddInteger((unsigned) SymbolRegionValueKind);
56
145k
    profile.AddPointer(R);
57
145k
  }
58
59
75.9k
  void Profile(llvm::FoldingSetNodeID& profile) override {
60
75.9k
    Profile(profile, R);
61
75.9k
  }
62
63
  void dumpToStream(raw_ostream &os) const override;
64
1.05k
  const MemRegion *getOriginRegion() const override { return getRegion(); }
65
66
  QualType getType() const override;
67
68
  // Implement isa<T> support.
69
22.6k
  static bool classof(const SymExpr *SE) {
70
22.6k
    return SE->getKind() == SymbolRegionValueKind;
71
22.6k
  }
72
};
73
74
/// A symbol representing the result of an expression in the case when we do
75
/// not know anything about what the expression is.
76
class SymbolConjured : public SymbolData {
77
  const Stmt *S;
78
  QualType T;
79
  unsigned Count;
80
  const LocationContext *LCtx;
81
  const void *SymbolTag;
82
83
public:
84
  SymbolConjured(SymbolID sym, const Stmt *s, const LocationContext *lctx,
85
                 QualType t, unsigned count, const void *symbolTag)
86
      : SymbolData(SymbolConjuredKind, sym), S(s), T(t), Count(count),
87
36.1k
        LCtx(lctx), SymbolTag(symbolTag) {
88
36.1k
    // FIXME: 's' might be a nullptr if we're conducting invalidation
89
36.1k
    // that was caused by a destructor call on a temporary object,
90
36.1k
    // which has no statement associated with it.
91
36.1k
    // Due to this, we might be creating the same invalidation symbol for
92
36.1k
    // two different invalidation passes (for two different temporaries).
93
36.1k
    assert(lctx);
94
36.1k
    assert(isValidTypeForSymbol(t));
95
36.1k
  }
96
97
53
  const Stmt *getStmt() const { return S; }
98
0
  unsigned getCount() const { return Count; }
99
0
  const void *getTag() const { return SymbolTag; }
100
101
  QualType getType() const override;
102
103
  void dumpToStream(raw_ostream &os) const override;
104
105
  static void Profile(llvm::FoldingSetNodeID& profile, const Stmt *S,
106
                      QualType T, unsigned Count, const LocationContext *LCtx,
107
162k
                      const void *SymbolTag) {
108
162k
    profile.AddInteger((unsigned) SymbolConjuredKind);
109
162k
    profile.AddPointer(S);
110
162k
    profile.AddPointer(LCtx);
111
162k
    profile.Add(T);
112
162k
    profile.AddInteger(Count);
113
162k
    profile.AddPointer(SymbolTag);
114
162k
  }
115
116
79.3k
  void Profile(llvm::FoldingSetNodeID& profile) override {
117
79.3k
    Profile(profile, S, T, Count, LCtx, SymbolTag);
118
79.3k
  }
119
120
  // Implement isa<T> support.
121
204
  static bool classof(const SymExpr *SE) {
122
204
    return SE->getKind() == SymbolConjuredKind;
123
204
  }
124
};
125
126
/// A symbol representing the value of a MemRegion whose parent region has
127
/// symbolic value.
128
class SymbolDerived : public SymbolData {
129
  SymbolRef parentSymbol;
130
  const TypedValueRegion *R;
131
132
public:
133
  SymbolDerived(SymbolID sym, SymbolRef parent, const TypedValueRegion *r)
134
1.08k
      : SymbolData(SymbolDerivedKind, sym), parentSymbol(parent), R(r) {
135
1.08k
    assert(parent);
136
1.08k
    assert(r);
137
1.08k
    assert(isValidTypeForSymbol(r->getValueType()));
138
1.08k
  }
139
140
303k
  SymbolRef getParentSymbol() const { return parentSymbol; }
141
225
  const TypedValueRegion *getRegion() const { return R; }
142
143
  QualType getType() const override;
144
145
  void dumpToStream(raw_ostream &os) const override;
146
109
  const MemRegion *getOriginRegion() const override { return getRegion(); }
147
148
  static void Profile(llvm::FoldingSetNodeID& profile, SymbolRef parent,
149
24.7k
                      const TypedValueRegion *r) {
150
24.7k
    profile.AddInteger((unsigned) SymbolDerivedKind);
151
24.7k
    profile.AddPointer(r);
152
24.7k
    profile.AddPointer(parent);
153
24.7k
  }
154
155
13.7k
  void Profile(llvm::FoldingSetNodeID& profile) override {
156
13.7k
    Profile(profile, parentSymbol, R);
157
13.7k
  }
158
159
  // Implement isa<T> support.
160
22.7k
  static bool classof(const SymExpr *SE) {
161
22.7k
    return SE->getKind() == SymbolDerivedKind;
162
22.7k
  }
163
};
164
165
/// SymbolExtent - Represents the extent (size in bytes) of a bounded region.
166
///  Clients should not ask the SymbolManager for a region's extent. Always use
167
///  SubRegion::getExtent instead -- the value returned may not be a symbol.
168
class SymbolExtent : public SymbolData {
169
  const SubRegion *R;
170
  
171
public:
172
  SymbolExtent(SymbolID sym, const SubRegion *r)
173
1.51k
      : SymbolData(SymbolExtentKind, sym), R(r) {
174
1.51k
    assert(r);
175
1.51k
  }
176
177
16.2k
  const SubRegion *getRegion() const { return R; }
178
179
  QualType getType() const override;
180
181
  void dumpToStream(raw_ostream &os) const override;
182
183
2.90k
  static void Profile(llvm::FoldingSetNodeID& profile, const SubRegion *R) {
184
2.90k
    profile.AddInteger((unsigned) SymbolExtentKind);
185
2.90k
    profile.AddPointer(R);
186
2.90k
  }
187
188
861
  void Profile(llvm::FoldingSetNodeID& profile) override {
189
861
    Profile(profile, R);
190
861
  }
191
192
  // Implement isa<T> support.
193
0
  static bool classof(const SymExpr *SE) {
194
0
    return SE->getKind() == SymbolExtentKind;
195
0
  }
196
};
197
198
/// SymbolMetadata - Represents path-dependent metadata about a specific region.
199
///  Metadata symbols remain live as long as they are marked as in use before
200
///  dead-symbol sweeping AND their associated regions are still alive.
201
///  Intended for use by checkers.
202
class SymbolMetadata : public SymbolData {
203
  const MemRegion* R;
204
  const Stmt *S;
205
  QualType T;
206
  const LocationContext *LCtx;
207
  unsigned Count;
208
  const void *Tag;
209
210
public:
211
  SymbolMetadata(SymbolID sym, const MemRegion* r, const Stmt *s, QualType t,
212
                 const LocationContext *LCtx, unsigned count, const void *tag)
213
      : SymbolData(SymbolMetadataKind, sym), R(r), S(s), T(t), LCtx(LCtx),
214
367
        Count(count), Tag(tag) {
215
367
      assert(r);
216
367
      assert(s);
217
367
      assert(isValidTypeForSymbol(t));
218
367
      assert(LCtx);
219
367
      assert(tag);
220
367
    }
221
222
2.32k
  const MemRegion *getRegion() const { return R; }
223
0
  const Stmt *getStmt() const { return S; }
224
0
  const LocationContext *getLocationContext() const { return LCtx; }
225
0
  unsigned getCount() const { return Count; }
226
0
  const void *getTag() const { return Tag; }
227
228
  QualType getType() const override;
229
230
  void dumpToStream(raw_ostream &os) const override;
231
232
  static void Profile(llvm::FoldingSetNodeID& profile, const MemRegion *R,
233
                      const Stmt *S, QualType T, const LocationContext *LCtx,
234
461
                      unsigned Count, const void *Tag) {
235
461
    profile.AddInteger((unsigned) SymbolMetadataKind);
236
461
    profile.AddPointer(R);
237
461
    profile.AddPointer(S);
238
461
    profile.Add(T);
239
461
    profile.AddPointer(LCtx);
240
461
    profile.AddInteger(Count);
241
461
    profile.AddPointer(Tag);
242
461
  }
243
244
70
  void Profile(llvm::FoldingSetNodeID& profile) override {
245
70
    Profile(profile, R, S, T, LCtx, Count, Tag);
246
70
  }
247
248
  // Implement isa<T> support.
249
5.77k
  static bool classof(const SymExpr *SE) {
250
5.77k
    return SE->getKind() == SymbolMetadataKind;
251
5.77k
  }
252
};
253
254
/// Represents a cast expression.
255
class SymbolCast : public SymExpr {
256
  const SymExpr *Operand;
257
258
  /// Type of the operand.
259
  QualType FromTy;
260
261
  /// The type of the result.
262
  QualType ToTy;
263
264
public:
265
  SymbolCast(const SymExpr *In, QualType From, QualType To)
266
25
      : SymExpr(SymbolCastKind), Operand(In), FromTy(From), ToTy(To) {
267
25
    assert(In);
268
25
    assert(isValidTypeForSymbol(From));
269
25
    // FIXME: GenericTaintChecker creates symbols of void type.
270
25
    // Otherwise, 'To' should also be a valid type.
271
25
  }
272
273
0
  unsigned computeComplexity() const override {
274
0
    if (Complexity == 0)
275
0
      Complexity = 1 + Operand->computeComplexity();
276
0
    return Complexity;
277
0
  }
278
279
94
  QualType getType() const override { return ToTy; }
280
281
543
  const SymExpr *getOperand() const { return Operand; }
282
283
  void dumpToStream(raw_ostream &os) const override;
284
285
  static void Profile(llvm::FoldingSetNodeID& ID,
286
35
                      const SymExpr *In, QualType From, QualType To) {
287
35
    ID.AddInteger((unsigned) SymbolCastKind);
288
35
    ID.AddPointer(In);
289
35
    ID.Add(From);
290
35
    ID.Add(To);
291
35
  }
292
293
5
  void Profile(llvm::FoldingSetNodeID& ID) override {
294
5
    Profile(ID, Operand, FromTy, ToTy);
295
5
  }
296
297
  // Implement isa<T> support.
298
22.4k
  static bool classof(const SymExpr *SE) {
299
22.4k
    return SE->getKind() == SymbolCastKind;
300
22.4k
  }
301
};
302
303
/// Represents a symbolic expression involving a binary operator 
304
class BinarySymExpr : public SymExpr {
305
  BinaryOperator::Opcode Op;
306
  QualType T;
307
308
protected:
309
  BinarySymExpr(Kind k, BinaryOperator::Opcode op, QualType t)
310
16.2k
      : SymExpr(k), Op(op), T(t) {
311
16.2k
    assert(classof(this));
312
16.2k
    assert(isValidTypeForSymbol(t));
313
16.2k
  }
314
315
public:
316
  // FIXME: We probably need to make this out-of-line to avoid redundant
317
  // generation of virtual functions.
318
409k
  QualType getType() const override { return T; }
319
320
632k
  BinaryOperator::Opcode getOpcode() const { return Op; }
321
322
  // Implement isa<T> support.
323
134k
  static bool classof(const SymExpr *SE) {
324
134k
    Kind k = SE->getKind();
325
134k
    return k >= BEGIN_BINARYSYMEXPRS && k <= END_BINARYSYMEXPRS;
326
134k
  }
327
};
328
329
/// Represents a symbolic expression like 'x' + 3.
330
class SymIntExpr : public BinarySymExpr {
331
  const SymExpr *LHS;
332
  const llvm::APSInt& RHS;
333
334
public:
335
  SymIntExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
336
             const llvm::APSInt &rhs, QualType t)
337
14.8k
      : BinarySymExpr(SymIntExprKind, op, t), LHS(lhs), RHS(rhs) {
338
14.8k
    assert(lhs);
339
14.8k
  }
340
341
  void dumpToStream(raw_ostream &os) const override;
342
343
2.04M
  const SymExpr *getLHS() const { return LHS; }
344
190k
  const llvm::APSInt &getRHS() const { return RHS; }
345
346
2.20k
  unsigned computeComplexity() const override {
347
2.20k
    if (Complexity == 0)
348
269
      Complexity = 1 + LHS->computeComplexity();
349
2.20k
    return Complexity;
350
2.20k
  }
351
352
  static void Profile(llvm::FoldingSetNodeID& ID, const SymExpr *lhs,
353
                      BinaryOperator::Opcode op, const llvm::APSInt& rhs,
354
328k
                      QualType t) {
355
328k
    ID.AddInteger((unsigned) SymIntExprKind);
356
328k
    ID.AddPointer(lhs);
357
328k
    ID.AddInteger(op);
358
328k
    ID.AddPointer(&rhs);
359
328k
    ID.Add(t);
360
328k
  }
361
362
186k
  void Profile(llvm::FoldingSetNodeID& ID) override {
363
186k
    Profile(ID, LHS, getOpcode(), RHS, getType());
364
186k
  }
365
366
  // Implement isa<T> support.
367
579k
  static bool classof(const SymExpr *SE) {
368
579k
    return SE->getKind() == SymIntExprKind;
369
579k
  }
370
};
371
372
/// Represents a symbolic expression like 3 - 'x'.
373
class IntSymExpr : public BinarySymExpr {
374
  const llvm::APSInt& LHS;
375
  const SymExpr *RHS;
376
377
public:
378
  IntSymExpr(const llvm::APSInt &lhs, BinaryOperator::Opcode op,
379
             const SymExpr *rhs, QualType t)
380
119
      : BinarySymExpr(IntSymExprKind, op, t), LHS(lhs), RHS(rhs) {
381
119
    assert(rhs);
382
119
  }
383
384
  void dumpToStream(raw_ostream &os) const override;
385
386
4.40k
  const SymExpr *getRHS() const { return RHS; }
387
0
  const llvm::APSInt &getLHS() const { return LHS; }
388
389
20
  unsigned computeComplexity() const override {
390
20
    if (Complexity == 0)
391
12
      Complexity = 1 + RHS->computeComplexity();
392
20
    return Complexity;
393
20
  }
394
395
  static void Profile(llvm::FoldingSetNodeID& ID, const llvm::APSInt& lhs,
396
                      BinaryOperator::Opcode op, const SymExpr *rhs,
397
212
                      QualType t) {
398
212
    ID.AddInteger((unsigned) IntSymExprKind);
399
212
    ID.AddPointer(&lhs);
400
212
    ID.AddInteger(op);
401
212
    ID.AddPointer(rhs);
402
212
    ID.Add(t);
403
212
  }
404
405
76
  void Profile(llvm::FoldingSetNodeID& ID) override {
406
76
    Profile(ID, LHS, getOpcode(), RHS, getType());
407
76
  }
408
409
  // Implement isa<T> support.
410
0
  static bool classof(const SymExpr *SE) {
411
0
    return SE->getKind() == IntSymExprKind;
412
0
  }
413
};
414
415
/// Represents a symbolic expression like 'x' + 'y'.
416
class SymSymExpr : public BinarySymExpr {
417
  const SymExpr *LHS;
418
  const SymExpr *RHS;
419
420
public:
421
  SymSymExpr(const SymExpr *lhs, BinaryOperator::Opcode op, const SymExpr *rhs,
422
             QualType t)
423
1.19k
      : BinarySymExpr(SymSymExprKind, op, t), LHS(lhs), RHS(rhs) {
424
1.19k
    assert(lhs);
425
1.19k
    assert(rhs);
426
1.19k
  }
427
428
2.50M
  const SymExpr *getLHS() const { return LHS; }
429
1.87M
  const SymExpr *getRHS() const { return RHS; }
430
431
  void dumpToStream(raw_ostream &os) const override;
432
433
6.18k
  unsigned computeComplexity() const override {
434
6.18k
    if (Complexity == 0)
435
188
      Complexity = RHS->computeComplexity() + LHS->computeComplexity();
436
6.18k
    return Complexity;
437
6.18k
  }
438
439
  static void Profile(llvm::FoldingSetNodeID& ID, const SymExpr *lhs,
440
32.3k
                    BinaryOperator::Opcode op, const SymExpr *rhs, QualType t) {
441
32.3k
    ID.AddInteger((unsigned) SymSymExprKind);
442
32.3k
    ID.AddPointer(lhs);
443
32.3k
    ID.AddInteger(op);
444
32.3k
    ID.AddPointer(rhs);
445
32.3k
    ID.Add(t);
446
32.3k
  }
447
448
21.5k
  void Profile(llvm::FoldingSetNodeID& ID) override {
449
21.5k
    Profile(ID, LHS, getOpcode(), RHS, getType());
450
21.5k
  }
451
452
  // Implement isa<T> support.
453
84.8k
  static bool classof(const SymExpr *SE) {
454
84.8k
    return SE->getKind() == SymSymExprKind;
455
84.8k
  }
456
};
457
458
class SymbolManager {
459
  using DataSetTy = llvm::FoldingSet<SymExpr>;
460
  using SymbolDependTy = llvm::DenseMap<SymbolRef, SymbolRefSmallVectorTy *>;
461
462
  DataSetTy DataSet;
463
464
  /// Stores the extra dependencies between symbols: the data should be kept
465
  /// alive as long as the key is live.
466
  SymbolDependTy SymbolDependencies;
467
468
  unsigned SymbolCounter = 0;
469
  llvm::BumpPtrAllocator& BPAlloc;
470
  BasicValueFactory &BV;
471
  ASTContext &Ctx;
472
473
public:
474
  SymbolManager(ASTContext &ctx, BasicValueFactory &bv,
475
                llvm::BumpPtrAllocator& bpalloc)
476
9.37k
      : SymbolDependencies(16), BPAlloc(bpalloc), BV(bv), Ctx(ctx) {}
477
  ~SymbolManager();
478
479
  static bool canSymbolicate(QualType T);
480
481
  /// Make a unique symbol for MemRegion R according to its kind.
482
  const SymbolRegionValue* getRegionValueSymbol(const TypedValueRegion* R);
483
484
  const SymbolConjured* conjureSymbol(const Stmt *E,
485
                                      const LocationContext *LCtx,
486
                                      QualType T,
487
                                      unsigned VisitCount,
488
                                      const void *SymbolTag = nullptr);
489
490
  const SymbolConjured* conjureSymbol(const Expr *E,
491
                                      const LocationContext *LCtx,
492
                                      unsigned VisitCount,
493
0
                                      const void *SymbolTag = nullptr) {
494
0
    return conjureSymbol(E, LCtx, E->getType(), VisitCount, SymbolTag);
495
0
  }
496
497
  const SymbolDerived *getDerivedSymbol(SymbolRef parentSymbol,
498
                                        const TypedValueRegion *R);
499
500
  const SymbolExtent *getExtentSymbol(const SubRegion *R);
501
502
  /// Creates a metadata symbol associated with a specific region.
503
  ///
504
  /// VisitCount can be used to differentiate regions corresponding to
505
  /// different loop iterations, thus, making the symbol path-dependent.
506
  const SymbolMetadata *getMetadataSymbol(const MemRegion *R, const Stmt *S,
507
                                          QualType T,
508
                                          const LocationContext *LCtx,
509
                                          unsigned VisitCount,
510
                                          const void *SymbolTag = nullptr);
511
512
  const SymbolCast* getCastSymbol(const SymExpr *Operand,
513
                                  QualType From, QualType To);
514
515
  const SymIntExpr *getSymIntExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
516
                                  const llvm::APSInt& rhs, QualType t);
517
518
  const SymIntExpr *getSymIntExpr(const SymExpr &lhs, BinaryOperator::Opcode op,
519
0
                                  const llvm::APSInt& rhs, QualType t) {
520
0
    return getSymIntExpr(&lhs, op, rhs, t);
521
0
  }
522
523
  const IntSymExpr *getIntSymExpr(const llvm::APSInt& lhs,
524
                                  BinaryOperator::Opcode op,
525
                                  const SymExpr *rhs, QualType t);
526
527
  const SymSymExpr *getSymSymExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
528
                                  const SymExpr *rhs, QualType t);
529
530
26
  QualType getType(const SymExpr *SE) const {
531
26
    return SE->getType();
532
26
  }
533
534
  /// Add artificial symbol dependency.
535
  ///
536
  /// The dependent symbol should stay alive as long as the primary is alive.
537
  void addSymbolDependency(const SymbolRef Primary, const SymbolRef Dependent);
538
539
  const SymbolRefSmallVectorTy *getDependentSymbols(const SymbolRef Primary);
540
541
567
  ASTContext &getContext() { return Ctx; }
542
26
  BasicValueFactory &getBasicVals() { return BV; }
543
};
544
545
/// A class responsible for cleaning up unused symbols.
546
class SymbolReaper {
547
  enum SymbolStatus {
548
    NotProcessed,
549
    HaveMarkedDependents
550
  };
551
552
  using SymbolSetTy = llvm::DenseSet<SymbolRef>;
553
  using SymbolMapTy = llvm::DenseMap<SymbolRef, SymbolStatus>;
554
  using RegionSetTy = llvm::DenseSet<const MemRegion *>;
555
556
  SymbolMapTy TheLiving;
557
  SymbolSetTy MetadataInUse;
558
  SymbolSetTy TheDead;
559
560
  RegionSetTy RegionRoots;
561
  
562
  const StackFrameContext *LCtx;
563
  const Stmt *Loc;
564
  SymbolManager& SymMgr;
565
  StoreRef reapedStore;
566
  llvm::DenseMap<const MemRegion *, unsigned> includedRegionCache;
567
568
public:
569
  /// Construct a reaper object, which removes everything which is not
570
  /// live before we execute statement s in the given location context.
571
  ///
572
  /// If the statement is NULL, everything is this and parent contexts is
573
  /// considered live.
574
  /// If the stack frame context is NULL, everything on stack is considered
575
  /// dead.
576
  SymbolReaper(const StackFrameContext *Ctx, const Stmt *s,
577
               SymbolManager &symmgr, StoreManager &storeMgr)
578
252k
      : LCtx(Ctx), Loc(s), SymMgr(symmgr), reapedStore(nullptr, storeMgr) {}
579
580
  const LocationContext *getLocationContext() const { return LCtx; }
581
582
  bool isLive(SymbolRef sym);
583
  bool isLiveRegion(const MemRegion *region);
584
  bool isLive(const Stmt *ExprVal, const LocationContext *LCtx) const;
585
  bool isLive(const VarRegion *VR, bool includeStoreBindings = false) const;
586
587
  /// Unconditionally marks a symbol as live.
588
  ///
589
  /// This should never be
590
  /// used by checkers, only by the state infrastructure such as the store and
591
  /// environment. Checkers should instead use metadata symbols and markInUse.
592
  void markLive(SymbolRef sym);
593
594
  /// Marks a symbol as important to a checker.
595
  ///
596
  /// For metadata symbols,
597
  /// this will keep the symbol alive as long as its associated region is also
598
  /// live. For other symbols, this has no effect; checkers are not permitted
599
  /// to influence the life of other symbols. This should be used before any
600
  /// symbol marking has occurred, i.e. in the MarkLiveSymbols callback.
601
  void markInUse(SymbolRef sym);
602
603
  /// If a symbol is known to be live, marks the symbol as live.
604
  ///
605
  ///  Otherwise, if the symbol cannot be proven live, it is marked as dead.
606
  ///  Returns true if the symbol is dead, false if live.
607
  bool maybeDead(SymbolRef sym);
608
609
  using dead_iterator = SymbolSetTy::const_iterator;
610
611
2.39k
  dead_iterator dead_begin() const { return TheDead.begin(); }
612
2.39k
  dead_iterator dead_end() const { return TheDead.end(); }
613
614
321k
  bool hasDeadSymbols() const {
615
321k
    return !TheDead.empty();
616
321k
  }
617
  
618
  using region_iterator = RegionSetTy::const_iterator;
619
620
252k
  region_iterator region_begin() const { return RegionRoots.begin(); }
621
252k
  region_iterator region_end() const { return RegionRoots.end(); }
622
623
  /// Returns whether or not a symbol has been confirmed dead.
624
  ///
625
  /// This should only be called once all marking of dead symbols has completed.
626
  /// (For checkers, this means only in the evalDeadSymbols callback.)
627
3.50k
  bool isDead(SymbolRef sym) const {
628
3.50k
    return TheDead.count(sym);
629
3.50k
  }
630
  
631
  void markLive(const MemRegion *region);
632
  void markElementIndicesLive(const MemRegion *region);
633
  
634
  /// Set to the value of the symbolic store after
635
  /// StoreManager::removeDeadBindings has been called.
636
252k
  void setReapedStore(StoreRef st) { reapedStore = st; }
637
638
private:
639
  /// Mark the symbols dependent on the input symbol as live.
640
  void markDependentsLive(SymbolRef sym);
641
};
642
643
class SymbolVisitor {
644
protected:
645
  ~SymbolVisitor() = default;
646
647
public:
648
256k
  SymbolVisitor() = default;
649
  SymbolVisitor(const SymbolVisitor &) = default;
650
  SymbolVisitor(SymbolVisitor &&) {}
651
652
  /// A visitor method invoked by ProgramStateManager::scanReachableSymbols.
653
  ///
654
  /// The method returns \c true if symbols should continue be scanned and \c
655
  /// false otherwise.
656
  virtual bool VisitSymbol(SymbolRef sym) = 0;
657
842
  virtual bool VisitMemRegion(const MemRegion *region) { return true; }
658
};
659
660
} // namespace ento
661
662
} // namespace clang
663
664
#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMBOLMANAGER_H