Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h
Line
Count
Source (jump to first uncovered line)
1
//===- BugReporter.h - Generate PathDiagnostics -----------------*- 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 BugReporter, a utility class for generating
10
//  PathDiagnostics for analyses based on ProgramState.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_BUGREPORTER_H
15
#define LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_BUGREPORTER_H
16
17
#include "clang/Basic/LLVM.h"
18
#include "clang/Basic/SourceLocation.h"
19
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
20
#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
21
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
22
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
23
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
24
#include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
25
#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
26
#include "llvm/ADT/ArrayRef.h"
27
#include "llvm/ADT/DenseSet.h"
28
#include "llvm/ADT/FoldingSet.h"
29
#include "llvm/ADT/ImmutableSet.h"
30
#include "llvm/ADT/None.h"
31
#include "llvm/ADT/SmallSet.h"
32
#include "llvm/ADT/SmallVector.h"
33
#include "llvm/ADT/StringMap.h"
34
#include "llvm/ADT/StringRef.h"
35
#include "llvm/ADT/ilist.h"
36
#include "llvm/ADT/ilist_node.h"
37
#include "llvm/ADT/iterator_range.h"
38
#include <cassert>
39
#include <memory>
40
#include <string>
41
#include <utility>
42
#include <vector>
43
44
namespace clang {
45
46
class AnalyzerOptions;
47
class ASTContext;
48
class Decl;
49
class DiagnosticsEngine;
50
class LocationContext;
51
class SourceManager;
52
class Stmt;
53
54
namespace ento {
55
56
class BugType;
57
class CheckerBase;
58
class ExplodedGraph;
59
class ExplodedNode;
60
class ExprEngine;
61
class MemRegion;
62
class SValBuilder;
63
64
//===----------------------------------------------------------------------===//
65
// Interface for individual bug reports.
66
//===----------------------------------------------------------------------===//
67
68
/// A mapping from diagnostic consumers to the diagnostics they should
69
/// consume.
70
using DiagnosticForConsumerMapTy =
71
    llvm::DenseMap<PathDiagnosticConsumer *, std::unique_ptr<PathDiagnostic>>;
72
73
/// This class provides an interface through which checkers can create
74
/// individual bug reports.
75
class BugReport : public llvm::ilist_node<BugReport> {
76
public:
77
  class NodeResolver {
78
    virtual void anchor();
79
80
  public:
81
20.5k
    virtual ~NodeResolver() = default;
82
83
    virtual const ExplodedNode*
84
            getOriginalNode(const ExplodedNode *N) = 0;
85
  };
86
87
  using ranges_iterator = const SourceRange *;
88
  using VisitorList = SmallVector<std::unique_ptr<BugReporterVisitor>, 8>;
89
  using visitor_iterator = VisitorList::iterator;
90
  using ExtraTextList = SmallVector<StringRef, 2>;
91
  using NoteList = SmallVector<std::shared_ptr<PathDiagnosticNotePiece>, 4>;
92
93
protected:
94
  friend class BugReportEquivClass;
95
  friend class BugReporter;
96
97
  const BugType& BT;
98
  const Decl *DeclWithIssue = nullptr;
99
  std::string ShortDescription;
100
  std::string Description;
101
  PathDiagnosticLocation Location;
102
  PathDiagnosticLocation UniqueingLocation;
103
  const Decl *UniqueingDecl;
104
105
  const ExplodedNode *ErrorNode = nullptr;
106
  SmallVector<SourceRange, 4> Ranges;
107
  ExtraTextList ExtraText;
108
  NoteList Notes;
109
110
  using Symbols = llvm::DenseSet<SymbolRef>;
111
  using Regions = llvm::DenseSet<const MemRegion *>;
112
113
  /// A (stack of) a set of symbols that are registered with this
114
  /// report as being "interesting", and thus used to help decide which
115
  /// diagnostics to include when constructing the final path diagnostic.
116
  /// The stack is largely used by BugReporter when generating PathDiagnostics
117
  /// for multiple PathDiagnosticConsumers.
118
  SmallVector<Symbols *, 2> interestingSymbols;
119
120
  /// A (stack of) set of regions that are registered with this report as being
121
  /// "interesting", and thus used to help decide which diagnostics
122
  /// to include when constructing the final path diagnostic.
123
  /// The stack is largely used by BugReporter when generating PathDiagnostics
124
  /// for multiple PathDiagnosticConsumers.
125
  SmallVector<Regions *, 2> interestingRegions;
126
127
  /// A set of location contexts that correspoind to call sites which should be
128
  /// considered "interesting".
129
  llvm::SmallSet<const LocationContext *, 2> InterestingLocationContexts;
130
131
  /// A set of custom visitors which generate "event" diagnostics at
132
  /// interesting points in the path.
133
  VisitorList Callbacks;
134
135
  /// Used for ensuring the visitors are only added once.
136
  llvm::FoldingSet<BugReporterVisitor> CallbacksSet;
137
138
  /// When set, this flag disables all callstack pruning from a diagnostic
139
  /// path.  This is useful for some reports that want maximum fidelty
140
  /// when reporting an issue.
141
  bool DoNotPrunePath = false;
142
143
  /// Used to track unique reasons why a bug report might be invalid.
144
  ///
145
  /// \sa markInvalid
146
  /// \sa removeInvalidation
147
  using InvalidationRecord = std::pair<const void *, const void *>;
148
149
  /// If non-empty, this bug report is likely a false positive and should not be
150
  /// shown to the user.
151
  ///
152
  /// \sa markInvalid
153
  /// \sa removeInvalidation
154
  llvm::SmallSet<InvalidationRecord, 4> Invalidations;
155
156
  /// Conditions we're already tracking.
157
  llvm::SmallSet<const ExplodedNode *, 4> TrackedConditions;
158
159
private:
160
  // Used internally by BugReporter.
161
  Symbols &getInterestingSymbols();
162
  Regions &getInterestingRegions();
163
164
  void lazyInitializeInterestingSets();
165
  void pushInterestingSymbolsAndRegions();
166
  void popInterestingSymbolsAndRegions();
167
168
public:
169
  BugReport(const BugType& bt, StringRef desc, const ExplodedNode *errornode)
170
9.89k
      : BT(bt), Description(desc), ErrorNode(errornode) {}
171
172
  BugReport(const BugType& bt, StringRef shortDesc, StringRef desc,
173
            const ExplodedNode *errornode)
174
      : BT(bt), ShortDescription(shortDesc), Description(desc),
175
32
        ErrorNode(errornode) {}
176
177
  BugReport(const BugType &bt, StringRef desc, PathDiagnosticLocation l)
178
1.20k
      : BT(bt), Description(desc), Location(l) {}
179
180
  /// Create a BugReport with a custom uniqueing location.
181
  ///
182
  /// The reports that have the same report location, description, bug type, and
183
  /// ranges are uniqued - only one of the equivalent reports will be presented
184
  /// to the user. This method allows to rest the location which should be used
185
  /// for uniquing reports. For example, memory leaks checker, could set this to
186
  /// the allocation site, rather then the location where the bug is reported.
187
  BugReport(BugType& bt, StringRef desc, const ExplodedNode *errornode,
188
            PathDiagnosticLocation LocationToUnique, const Decl *DeclToUnique)
189
      : BT(bt), Description(desc), UniqueingLocation(LocationToUnique),
190
664
        UniqueingDecl(DeclToUnique), ErrorNode(errornode) {}
191
192
  virtual ~BugReport();
193
194
82.8k
  const BugType& getBugType() const { return BT; }
195
  //BugType& getBugType() { return BT; }
196
197
  /// True when the report has an execution path associated with it.
198
  ///
199
  /// A report is said to be path-sensitive if it was thrown against a
200
  /// particular exploded node in the path-sensitive analysis graph.
201
  /// Path-sensitive reports have their intermediate path diagnostics
202
  /// auto-generated, perhaps with the help of checker-defined visitors,
203
  /// and may contain extra notes.
204
  /// Path-insensitive reports consist only of a single warning message
205
  /// in a specific location, and perhaps extra notes.
206
  /// Path-sensitive checkers are allowed to throw path-insensitive reports.
207
11.2k
  bool isPathSensitive() const { return ErrorNode != nullptr; }
208
209
79.1k
  const ExplodedNode *getErrorNode() const { return ErrorNode; }
210
211
23.4k
  StringRef getDescription() const { return Description; }
212
213
11.6k
  StringRef getShortDescription(bool UseFallback = true) const {
214
11.6k
    if (ShortDescription.empty() && 
UseFallback11.6k
)
215
0
      return Description;
216
11.6k
    return ShortDescription;
217
11.6k
  }
218
219
  /// Indicates whether or not any path pruning should take place
220
  /// when generating a PathDiagnostic from this BugReport.
221
1.50k
  bool shouldPrunePath() const { return !DoNotPrunePath; }
222
223
  /// Disable all path pruning when generating a PathDiagnostic.
224
6
  void disablePathPruning() { DoNotPrunePath = true; }
225
226
  void markInteresting(SymbolRef sym);
227
  void markInteresting(const MemRegion *R);
228
  void markInteresting(SVal V);
229
  void markInteresting(const LocationContext *LC);
230
231
  bool isInteresting(SymbolRef sym);
232
  bool isInteresting(const MemRegion *R);
233
  bool isInteresting(SVal V);
234
  bool isInteresting(const LocationContext *LC);
235
236
  /// Returns whether or not this report should be considered valid.
237
  ///
238
  /// Invalid reports are those that have been classified as likely false
239
  /// positives after the fact.
240
1.03M
  bool isValid() const {
241
1.03M
    return Invalidations.empty();
242
1.03M
  }
243
244
  /// Marks the current report as invalid, meaning that it is probably a false
245
  /// positive and should not be reported to the user.
246
  ///
247
  /// The \p Tag and \p Data arguments are intended to be opaque identifiers for
248
  /// this particular invalidation, where \p Tag represents the visitor
249
  /// responsible for invalidation, and \p Data represents the reason this
250
  /// visitor decided to invalidate the bug report.
251
  ///
252
  /// \sa removeInvalidation
253
282
  void markInvalid(const void *Tag, const void *Data) {
254
282
    Invalidations.insert(std::make_pair(Tag, Data));
255
282
  }
256
257
  /// Return the canonical declaration, be it a method or class, where
258
  /// this issue semantically occurred.
259
  const Decl *getDeclWithIssue() const;
260
261
  /// Specifically set the Decl where an issue occurred.  This isn't necessary
262
  /// for BugReports that cover a path as it will be automatically inferred.
263
1.19k
  void setDeclWithIssue(const Decl *declWithIssue) {
264
1.19k
    DeclWithIssue = declWithIssue;
265
1.19k
  }
266
267
  /// Add new item to the list of additional notes that need to be attached to
268
  /// this path-insensitive report. If you want to add extra notes to a
269
  /// path-sensitive report, you need to use a BugReporterVisitor because it
270
  /// allows you to specify where exactly in the auto-generated path diagnostic
271
  /// the extra note should appear.
272
  void addNote(StringRef Msg, const PathDiagnosticLocation &Pos,
273
166
               ArrayRef<SourceRange> Ranges) {
274
166
    auto P = std::make_shared<PathDiagnosticNotePiece>(Pos, Msg);
275
166
276
166
    for (const auto &R : Ranges)
277
19
      P->addRange(R);
278
166
279
166
    Notes.push_back(std::move(P));
280
166
  }
281
282
  // FIXME: Instead of making an override, we could have default-initialized
283
  // Ranges with {}, however it crashes the MSVC 2013 compiler.
284
147
  void addNote(StringRef Msg, const PathDiagnosticLocation &Pos) {
285
147
    std::vector<SourceRange> Ranges;
286
147
    addNote(Msg, Pos, Ranges);
287
147
  }
288
289
23.3k
  virtual const NoteList &getNotes() {
290
23.3k
    return Notes;
291
23.3k
  }
292
293
  /// This allows for addition of meta data to the diagnostic.
294
  ///
295
  /// Currently, only the HTMLDiagnosticClient knows how to display it.
296
0
  void addExtraText(StringRef S) {
297
0
    ExtraText.push_back(S);
298
0
  }
299
300
11.6k
  virtual const ExtraTextList &getExtraText() {
301
11.6k
    return ExtraText;
302
11.6k
  }
303
304
  /// Return the "definitive" location of the reported bug.
305
  ///
306
  ///  While a bug can span an entire path, usually there is a specific
307
  ///  location that can be used to identify where the key issue occurred.
308
  ///  This location is used by clients rendering diagnostics.
309
  virtual PathDiagnosticLocation getLocation(const SourceManager &SM) const;
310
311
  /// Get the location on which the report should be uniqued.
312
24.3k
  PathDiagnosticLocation getUniqueingLocation() const {
313
24.3k
    return UniqueingLocation;
314
24.3k
  }
315
316
  /// Get the declaration containing the uniqueing location.
317
11.6k
  const Decl *getUniqueingDecl() const {
318
11.6k
    return UniqueingDecl;
319
11.6k
  }
320
321
  const Stmt *getStmt() const;
322
323
  /// Add a range to a bug report.
324
  ///
325
  /// Ranges are used to highlight regions of interest in the source code.
326
  /// They should be at the same source code line as the BugReport location.
327
  /// By default, the source range of the statement corresponding to the error
328
  /// node will be used; add a single invalid range to specify absence of
329
  /// ranges.
330
10.4k
  void addRange(SourceRange R) {
331
10.4k
    assert((R.isValid() || Ranges.empty()) && "Invalid range can only be used "
332
10.4k
                           "to specify that the report does not have a range.");
333
10.4k
    Ranges.push_back(R);
334
10.4k
  }
335
336
  /// Get the SourceRanges associated with the report.
337
  virtual llvm::iterator_range<ranges_iterator> getRanges();
338
339
  /// Add custom or predefined bug report visitors to this report.
340
  ///
341
  /// The visitors should be used when the default trace is not sufficient.
342
  /// For example, they allow constructing a more elaborate trace.
343
  /// \sa registerConditionVisitor(), registerTrackNullOrUndefValue(),
344
  /// registerFindLastStore(), registerNilReceiverVisitor(), and
345
  /// registerVarDeclsLastStore().
346
  void addVisitor(std::unique_ptr<BugReporterVisitor> visitor);
347
348
  /// Remove all visitors attached to this bug report.
349
  void clearVisitors();
350
351
  /// Iterators through the custom diagnostic visitors.
352
1.00M
  visitor_iterator visitor_begin() { return Callbacks.begin(); }
353
1.00M
  visitor_iterator visitor_end() { return Callbacks.end(); }
354
355
  /// Notes that the condition of the CFGBlock associated with \p Cond is
356
  /// being tracked.
357
  /// \returns false if the condition is already being tracked.
358
30
  bool addTrackedCondition(const ExplodedNode *Cond) {
359
30
    return TrackedConditions.insert(Cond).second;
360
30
  }
361
362
  /// Profile to identify equivalent bug reports for error report coalescing.
363
  /// Reports are uniqued to ensure that we do not emit multiple diagnostics
364
  /// for each bug.
365
  virtual void Profile(llvm::FoldingSetNodeID& hash) const;
366
};
367
368
//===----------------------------------------------------------------------===//
369
// BugTypes (collections of related reports).
370
//===----------------------------------------------------------------------===//
371
372
class BugReportEquivClass : public llvm::FoldingSetNode {
373
  friend class BugReporter;
374
375
  /// List of *owned* BugReport objects.
376
  llvm::ilist<BugReport> Reports;
377
378
11.7k
  void AddReport(std::unique_ptr<BugReport> R) {
379
11.7k
    Reports.push_back(R.release());
380
11.7k
  }
381
382
public:
383
11.2k
  BugReportEquivClass(std::unique_ptr<BugReport> R) { AddReport(std::move(R)); }
384
  ~BugReportEquivClass();
385
386
855
  void Profile(llvm::FoldingSetNodeID& ID) const {
387
855
    assert(!Reports.empty());
388
855
    Reports.front().Profile(ID);
389
855
  }
390
391
  using iterator = llvm::ilist<BugReport>::iterator;
392
  using const_iterator = llvm::ilist<BugReport>::const_iterator;
393
394
21.6k
  iterator begin() { return Reports.begin(); }
395
21.6k
  iterator end() { return Reports.end(); }
396
397
0
  const_iterator begin() const { return Reports.begin(); }
398
0
  const_iterator end() const { return Reports.end(); }
399
};
400
401
//===----------------------------------------------------------------------===//
402
// BugReporter and friends.
403
//===----------------------------------------------------------------------===//
404
405
class BugReporterData {
406
public:
407
  virtual ~BugReporterData();
408
409
  virtual DiagnosticsEngine& getDiagnostic() = 0;
410
  virtual ArrayRef<PathDiagnosticConsumer*> getPathDiagnosticConsumers() = 0;
411
  virtual ASTContext &getASTContext() = 0;
412
  virtual SourceManager &getSourceManager() = 0;
413
  virtual AnalyzerOptions &getAnalyzerOptions() = 0;
414
};
415
416
/// BugReporter is a utility class for generating PathDiagnostics for analysis.
417
/// It collects the BugReports and BugTypes and knows how to generate
418
/// and flush the corresponding diagnostics.
419
///
420
/// The base class is used for generating path-insensitive
421
class BugReporter {
422
public:
423
  enum Kind { BaseBRKind, GRBugReporterKind };
424
425
private:
426
  using BugTypesTy = llvm::ImmutableSet<BugType *>;
427
428
  BugTypesTy::Factory F;
429
  BugTypesTy BugTypes;
430
431
  const Kind kind;
432
  BugReporterData& D;
433
434
  /// Generate and flush the diagnostics for the given bug report.
435
  void FlushReport(BugReportEquivClass& EQ);
436
437
  /// Generate the diagnostics for the given bug report.
438
  std::unique_ptr<DiagnosticForConsumerMapTy>
439
  generateDiagnosticForConsumerMap(BugReport *exampleReport,
440
                                   ArrayRef<PathDiagnosticConsumer *> consumers,
441
                                   ArrayRef<BugReport *> bugReports);
442
443
  /// The set of bug reports tracked by the BugReporter.
444
  llvm::FoldingSet<BugReportEquivClass> EQClasses;
445
446
  /// A vector of BugReports for tracking the allocated pointers and cleanup.
447
  std::vector<BugReportEquivClass *> EQClassesVector;
448
449
protected:
450
  BugReporter(BugReporterData& d, Kind k)
451
10.8k
      : BugTypes(F.getEmptySet()), kind(k), D(d) {}
452
453
public:
454
  BugReporter(BugReporterData& d)
455
27.7k
      : BugTypes(F.getEmptySet()), kind(BaseBRKind), D(d) {}
456
  virtual ~BugReporter();
457
458
  /// Generate and flush diagnostics for all bug reports.
459
  void FlushReports();
460
461
0
  Kind getKind() const { return kind; }
462
463
0
  DiagnosticsEngine& getDiagnostic() {
464
0
    return D.getDiagnostic();
465
0
  }
466
467
11.2k
  ArrayRef<PathDiagnosticConsumer*> getPathDiagnosticConsumers() {
468
11.2k
    return D.getPathDiagnosticConsumers();
469
11.2k
  }
470
471
  /// Iterator over the set of BugTypes tracked by the BugReporter.
472
  using iterator = BugTypesTy::iterator;
473
0
  iterator begin() { return BugTypes.begin(); }
474
0
  iterator end() { return BugTypes.end(); }
475
476
  /// Iterator over the set of BugReports tracked by the BugReporter.
477
  using EQClasses_iterator = llvm::FoldingSet<BugReportEquivClass>::iterator;
478
0
  EQClasses_iterator EQClasses_begin() { return EQClasses.begin(); }
479
0
  EQClasses_iterator EQClasses_end() { return EQClasses.end(); }
480
481
2.44k
  ASTContext &getContext() { return D.getASTContext(); }
482
483
291k
  SourceManager &getSourceManager() { return D.getSourceManager(); }
484
485
52.0k
  AnalyzerOptions &getAnalyzerOptions() { return D.getAnalyzerOptions(); }
486
487
  virtual std::unique_ptr<DiagnosticForConsumerMapTy>
488
  generatePathDiagnostics(ArrayRef<PathDiagnosticConsumer *> consumers,
489
0
                          ArrayRef<BugReport *> &bugReports) {
490
0
    return {};
491
0
  }
492
493
  void Register(const BugType *BT);
494
495
  /// Add the given report to the set of reports tracked by BugReporter.
496
  ///
497
  /// The reports are usually generated by the checkers. Further, they are
498
  /// folded based on the profile value, which is done to coalesce similar
499
  /// reports.
500
  void emitReport(std::unique_ptr<BugReport> R);
501
502
  void EmitBasicReport(const Decl *DeclWithIssue, const CheckerBase *Checker,
503
                       StringRef BugName, StringRef BugCategory,
504
                       StringRef BugStr, PathDiagnosticLocation Loc,
505
                       ArrayRef<SourceRange> Ranges = None);
506
507
  void EmitBasicReport(const Decl *DeclWithIssue, CheckName CheckName,
508
                       StringRef BugName, StringRef BugCategory,
509
                       StringRef BugStr, PathDiagnosticLocation Loc,
510
                       ArrayRef<SourceRange> Ranges = None);
511
512
private:
513
  llvm::StringMap<BugType *> StrBugTypes;
514
515
  /// Returns a BugType that is associated with the given name and
516
  /// category.
517
  BugType *getBugTypeForName(CheckName CheckName, StringRef name,
518
                             StringRef category);
519
};
520
521
/// GRBugReporter is used for generating path-sensitive reports.
522
class GRBugReporter : public BugReporter {
523
  ExprEngine& Eng;
524
525
public:
526
  GRBugReporter(BugReporterData& d, ExprEngine& eng)
527
10.8k
      : BugReporter(d, GRBugReporterKind), Eng(eng) {}
528
529
  ~GRBugReporter() override;
530
531
  /// getGraph - Get the exploded graph created by the analysis engine
532
  ///  for the analyzed method or function.
533
  ExplodedGraph &getGraph();
534
535
  /// getStateManager - Return the state manager used by the analysis
536
  ///  engine.
537
  ProgramStateManager &getStateManager();
538
539
  /// \p bugReports A set of bug reports within a *single* equivalence class
540
  ///
541
  /// \return A mapping from consumers to the corresponding diagnostics.
542
  /// Iterates through the bug reports within a single equivalence class,
543
  /// stops at a first non-invalidated report.
544
  std::unique_ptr<DiagnosticForConsumerMapTy>
545
  generatePathDiagnostics(ArrayRef<PathDiagnosticConsumer *> consumers,
546
                          ArrayRef<BugReport *> &bugReports) override;
547
548
  /// classof - Used by isa<>, cast<>, and dyn_cast<>.
549
0
  static bool classof(const BugReporter* R) {
550
0
    return R->getKind() == GRBugReporterKind;
551
0
  }
552
};
553
554
555
class NodeMapClosure : public BugReport::NodeResolver {
556
  InterExplodedGraphMap &M;
557
558
public:
559
20.5k
  NodeMapClosure(InterExplodedGraphMap &m) : M(m) {}
560
561
0
  const ExplodedNode *getOriginalNode(const ExplodedNode *N) override {
562
0
    return M.lookup(N);
563
0
  }
564
};
565
566
class BugReporterContext {
567
  GRBugReporter &BR;
568
  NodeMapClosure NMC;
569
570
  virtual void anchor();
571
572
public:
573
  BugReporterContext(GRBugReporter &br, InterExplodedGraphMap &Backmap)
574
20.5k
      : BR(br), NMC(Backmap) {}
575
576
20.5k
  virtual ~BugReporterContext() = default;
577
578
10.4k
  GRBugReporter& getBugReporter() { return BR; }
579
580
0
  ExplodedGraph &getGraph() { return BR.getGraph(); }
581
582
31.6k
  ProgramStateManager& getStateManager() {
583
31.6k
    return BR.getStateManager();
584
31.6k
  }
585
586
0
  SValBuilder &getSValBuilder() {
587
0
    return getStateManager().getSValBuilder();
588
0
  }
589
590
275
  ASTContext &getASTContext() {
591
275
    return BR.getContext();
592
275
  }
593
594
265k
  SourceManager& getSourceManager() {
595
265k
    return BR.getSourceManager();
596
265k
  }
597
598
9.98k
  AnalyzerOptions &getAnalyzerOptions() {
599
9.98k
    return BR.getAnalyzerOptions();
600
9.98k
  }
601
602
0
  NodeMapClosure& getNodeResolver() { return NMC; }
603
};
604
605
606
/// The tag upon which the TagVisitor reacts. Add these in order to display
607
/// additional PathDiagnosticEventPieces along the path.
608
class NoteTag : public ProgramPointTag {
609
public:
610
  using Callback =
611
      std::function<std::string(BugReporterContext &, BugReport &)>;
612
613
private:
614
  static int Kind;
615
616
  const Callback Cb;
617
  const bool IsPrunable;
618
619
  NoteTag(Callback &&Cb, bool IsPrunable)
620
150
      : ProgramPointTag(&Kind), Cb(std::move(Cb)), IsPrunable(IsPrunable) {}
621
622
public:
623
218k
  static bool classof(const ProgramPointTag *T) {
624
218k
    return T->getTagKind() == &Kind;
625
218k
  }
626
627
  Optional<std::string> generateMessage(BugReporterContext &BRC,
628
156
                                        BugReport &R) const {
629
156
    std::string Msg = Cb(BRC, R);
630
156
    if (Msg.empty())
631
1
      return None;
632
155
633
155
    return std::move(Msg);
634
155
  }
635
636
0
  StringRef getTagDescription() const override {
637
0
    // TODO: Remember a few examples of generated messages
638
0
    // and display them in the ExplodedGraph dump by
639
0
    // returning them from this function.
640
0
    return "Note Tag";
641
0
  }
642
643
155
  bool isPrunable() const { return IsPrunable; }
644
645
  // Manage memory for NoteTag objects.
646
  class Factory {
647
    std::vector<std::unique_ptr<NoteTag>> Tags;
648
649
  public:
650
150
    const NoteTag *makeNoteTag(Callback &&Cb, bool IsPrunable = false) {
651
150
      // We cannot use make_unique because we cannot access the private
652
150
      // constructor from inside it.
653
150
      std::unique_ptr<NoteTag> T(new NoteTag(std::move(Cb), IsPrunable));
654
150
      Tags.push_back(std::move(T));
655
150
      return Tags.back().get();
656
150
    }
657
  };
658
659
  friend class TagVisitor;
660
};
661
662
} // namespace ento
663
664
} // namespace clang
665
666
#endif // LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_BUGREPORTER_H