Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Analysis/PathDiagnostic.h
Line
Count
Source (jump to first uncovered line)
1
//===- PathDiagnostic.h - Path-Specific Diagnostic Handling -----*- 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 the PathDiagnostic-related interfaces.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_PATHDIAGNOSTIC_H
14
#define LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_PATHDIAGNOSTIC_H
15
16
#include "clang/AST/Stmt.h"
17
#include "clang/Analysis/AnalysisDeclContext.h"
18
#include "clang/Basic/LLVM.h"
19
#include "clang/Basic/SourceLocation.h"
20
#include "llvm/ADT/ArrayRef.h"
21
#include "llvm/ADT/FoldingSet.h"
22
#include "llvm/ADT/Optional.h"
23
#include "llvm/ADT/PointerUnion.h"
24
#include "llvm/ADT/SmallVector.h"
25
#include "llvm/ADT/StringRef.h"
26
#include "llvm/Support/Allocator.h"
27
#include <cassert>
28
#include <deque>
29
#include <iterator>
30
#include <list>
31
#include <map>
32
#include <memory>
33
#include <set>
34
#include <string>
35
#include <utility>
36
#include <vector>
37
38
namespace clang {
39
40
class AnalysisDeclContext;
41
class BinaryOperator;
42
class CallEnter;
43
class CallExitEnd;
44
class CallExpr;
45
class ConditionalOperator;
46
class Decl;
47
class Expr;
48
class LocationContext;
49
class MemberExpr;
50
class ProgramPoint;
51
class SourceManager;
52
53
namespace ento {
54
55
//===----------------------------------------------------------------------===//
56
// High-level interface for handlers of path-sensitive diagnostics.
57
//===----------------------------------------------------------------------===//
58
59
class PathDiagnostic;
60
61
/// These options tweak the behavior of path diangostic consumers.
62
/// Most of these options are currently supported by very few consumers.
63
struct PathDiagnosticConsumerOptions {
64
  /// Run-line of the tool that produced the diagnostic.
65
  /// It can be included with the diagnostic for debugging purposes.
66
  std::string ToolInvocation;
67
68
  /// Whether to include additional information about macro expansions
69
  /// with the diagnostics, because otherwise they can be hard to obtain
70
  /// without re-compiling the program under analysis.
71
  bool ShouldDisplayMacroExpansions = false;
72
73
  /// Whether to include LLVM statistics of the process in the diagnostic.
74
  /// Useful for profiling the tool on large real-world codebases.
75
  bool ShouldSerializeStats = false;
76
77
  /// If the consumer intends to produce multiple output files, should it
78
  /// use randomly generated file names for these files (with the tiny risk of
79
  /// having random collisions) or deterministic human-readable file names
80
  /// (with a larger risk of deterministic collisions or invalid characters
81
  /// in the file name). We should not really give this choice to the users
82
  /// because deterministic mode is always superior when done right, but
83
  /// for some consumers this mode is experimental and needs to be
84
  /// off by default.
85
  bool ShouldWriteStableReportFilename = false;
86
87
  /// Whether the consumer should treat consumed diagnostics as hard errors.
88
  /// Useful for breaking your build when issues are found.
89
  bool ShouldDisplayWarningsAsErrors = false;
90
91
  /// Whether the consumer should attempt to rewrite the source file
92
  /// with fix-it hints attached to the diagnostics it consumes.
93
  bool ShouldApplyFixIts = false;
94
95
  /// Whether the consumer should present the name of the entity that emitted
96
  /// the diagnostic (eg., a checker) so that the user knew how to disable it.
97
  bool ShouldDisplayDiagnosticName = false;
98
};
99
100
class PathDiagnosticConsumer {
101
public:
102
  class PDFileEntry : public llvm::FoldingSetNode {
103
  public:
104
95
    PDFileEntry(llvm::FoldingSetNodeID &NodeID) : NodeID(NodeID) {}
105
106
    using ConsumerFiles = std::vector<std::pair<StringRef, StringRef>>;
107
108
    /// A vector of <consumer,file> pairs.
109
    ConsumerFiles files;
110
111
    /// A precomputed hash tag used for uniquing PDFileEntry objects.
112
    const llvm::FoldingSetNodeID NodeID;
113
114
    /// Used for profiling in the FoldingSet.
115
10
    void Profile(llvm::FoldingSetNodeID &ID) { ID = NodeID; }
116
  };
117
118
  class FilesMade {
119
    llvm::BumpPtrAllocator Alloc;
120
    llvm::FoldingSet<PDFileEntry> Set;
121
122
  public:
123
    ~FilesMade();
124
125
630
    bool empty() const { return Set.empty(); }
126
127
    void addDiagnostic(const PathDiagnostic &PD,
128
                       StringRef ConsumerName,
129
                       StringRef fileName);
130
131
    PDFileEntry::ConsumerFiles *getFiles(const PathDiagnostic &PD);
132
  };
133
134
private:
135
  virtual void anchor();
136
137
public:
138
1.53k
  PathDiagnosticConsumer() = default;
139
  virtual ~PathDiagnosticConsumer();
140
141
  void FlushDiagnostics(FilesMade *FilesMade);
142
143
  virtual void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
144
                                    FilesMade *filesMade) = 0;
145
146
  virtual StringRef getName() const = 0;
147
148
  void HandlePathDiagnostic(std::unique_ptr<PathDiagnostic> D);
149
150
  enum PathGenerationScheme {
151
    /// Only runs visitors, no output generated.
152
    None,
153
154
    /// Used for SARIF and text output.
155
    Minimal,
156
157
    /// Used for plist output, used for "arrows" generation.
158
    Extensive,
159
160
    /// Used for HTML, shows both "arrows" and control notes.
161
    Everything
162
  };
163
164
137
  virtual PathGenerationScheme getGenerationScheme() const { return Minimal; }
165
166
15.1k
  bool shouldGenerateDiagnostics() const {
167
15.1k
    return getGenerationScheme() != None;
168
15.1k
  }
169
170
157k
  bool shouldAddPathEdges() const { return getGenerationScheme() >= Extensive; }
171
17.4k
  bool shouldAddControlNotes() const {
172
17.4k
    return getGenerationScheme() == Minimal ||
173
17.4k
           
getGenerationScheme() == Everything2.23k
;
174
17.4k
  }
175
176
0
  virtual bool supportsLogicalOpControlFlow() const { return false; }
177
178
  /// Return true if the PathDiagnosticConsumer supports individual
179
  /// PathDiagnostics that span multiple files.
180
17
  virtual bool supportsCrossFileDiagnostics() const { return false; }
181
182
protected:
183
  bool flushed = false;
184
  llvm::FoldingSet<PathDiagnostic> Diags;
185
};
186
187
//===----------------------------------------------------------------------===//
188
// Path-sensitive diagnostics.
189
//===----------------------------------------------------------------------===//
190
191
class PathDiagnosticRange : public SourceRange {
192
public:
193
  bool isPoint = false;
194
195
  PathDiagnosticRange(SourceRange R, bool isP = false)
196
117k
      : SourceRange(R), isPoint(isP) {}
197
65.1k
  PathDiagnosticRange() = default;
198
};
199
200
using LocationOrAnalysisDeclContext =
201
    llvm::PointerUnion<const LocationContext *, AnalysisDeclContext *>;
202
203
class PathDiagnosticLocation {
204
private:
205
  enum Kind { RangeK, SingleLocK, StmtK, DeclK } K = SingleLocK;
206
207
  const Stmt *S = nullptr;
208
  const Decl *D = nullptr;
209
  const SourceManager *SM = nullptr;
210
  FullSourceLoc Loc;
211
  PathDiagnosticRange Range;
212
213
  PathDiagnosticLocation(SourceLocation L, const SourceManager &sm, Kind kind)
214
16.7k
      : K(kind), SM(&sm), Loc(genLocation(L)), Range(genRange()) {}
215
216
  FullSourceLoc genLocation(
217
      SourceLocation L = SourceLocation(),
218
      LocationOrAnalysisDeclContext LAC = (AnalysisDeclContext *)nullptr) const;
219
220
  PathDiagnosticRange genRange(
221
      LocationOrAnalysisDeclContext LAC = (AnalysisDeclContext *)nullptr) const;
222
223
public:
224
  /// Create an invalid location.
225
65.1k
  PathDiagnosticLocation() = default;
226
227
  /// Create a location corresponding to the given statement.
228
  PathDiagnosticLocation(const Stmt *s, const SourceManager &sm,
229
                         LocationOrAnalysisDeclContext lac)
230
      : K(s->getBeginLoc().isValid() ? StmtK : SingleLocK),
231
        S(K == StmtK ? s : nullptr), SM(&sm),
232
99.9k
        Loc(genLocation(SourceLocation(), lac)), Range(genRange(lac)) {
233
99.9k
    assert(K == SingleLocK || S);
234
0
    assert(K == SingleLocK || Loc.isValid());
235
0
    assert(K == SingleLocK || Range.isValid());
236
99.9k
  }
237
238
  /// Create a location corresponding to the given declaration.
239
  PathDiagnosticLocation(const Decl *d, const SourceManager &sm)
240
310
      : K(DeclK), D(d), SM(&sm), Loc(genLocation()), Range(genRange()) {
241
310
    assert(D);
242
0
    assert(Loc.isValid());
243
0
    assert(Range.isValid());
244
310
  }
245
246
  /// Create a location at an explicit offset in the source.
247
  ///
248
  /// This should only be used if there are no more appropriate constructors.
249
  PathDiagnosticLocation(SourceLocation loc, const SourceManager &sm)
250
298
      : SM(&sm), Loc(loc, sm), Range(genRange()) {
251
298
    assert(Loc.isValid());
252
0
    assert(Range.isValid());
253
298
  }
254
255
  /// Create a location corresponding to the given declaration.
256
  static PathDiagnosticLocation create(const Decl *D,
257
284
                                       const SourceManager &SM) {
258
284
    return PathDiagnosticLocation(D, SM);
259
284
  }
260
261
  /// Create a location for the beginning of the declaration.
262
  static PathDiagnosticLocation createBegin(const Decl *D,
263
                                            const SourceManager &SM);
264
265
  /// Create a location for the beginning of the declaration.
266
  /// The third argument is ignored, useful for generic treatment
267
  /// of statements and declarations.
268
  static PathDiagnosticLocation
269
  createBegin(const Decl *D, const SourceManager &SM,
270
0
              const LocationOrAnalysisDeclContext LAC) {
271
0
    return createBegin(D, SM);
272
0
  }
273
274
  /// Create a location for the beginning of the statement.
275
  static PathDiagnosticLocation createBegin(const Stmt *S,
276
                                            const SourceManager &SM,
277
                                            const LocationOrAnalysisDeclContext LAC);
278
279
  /// Create a location for the end of the statement.
280
  ///
281
  /// If the statement is a CompoundStatement, the location will point to the
282
  /// closing brace instead of following it.
283
  static PathDiagnosticLocation createEnd(const Stmt *S,
284
                                          const SourceManager &SM,
285
                                       const LocationOrAnalysisDeclContext LAC);
286
287
  /// Create the location for the operator of the binary expression.
288
  /// Assumes the statement has a valid location.
289
  static PathDiagnosticLocation createOperatorLoc(const BinaryOperator *BO,
290
                                                  const SourceManager &SM);
291
  static PathDiagnosticLocation createConditionalColonLoc(
292
                                                  const ConditionalOperator *CO,
293
                                                  const SourceManager &SM);
294
295
  /// For member expressions, return the location of the '.' or '->'.
296
  /// Assumes the statement has a valid location.
297
  static PathDiagnosticLocation createMemberLoc(const MemberExpr *ME,
298
                                                const SourceManager &SM);
299
300
  /// Create a location for the beginning of the compound statement.
301
  /// Assumes the statement has a valid location.
302
  static PathDiagnosticLocation createBeginBrace(const CompoundStmt *CS,
303
                                                 const SourceManager &SM);
304
305
  /// Create a location for the end of the compound statement.
306
  /// Assumes the statement has a valid location.
307
  static PathDiagnosticLocation createEndBrace(const CompoundStmt *CS,
308
                                               const SourceManager &SM);
309
310
  /// Create a location for the beginning of the enclosing declaration body.
311
  /// Defaults to the beginning of the first statement in the declaration body.
312
  static PathDiagnosticLocation createDeclBegin(const LocationContext *LC,
313
                                                const SourceManager &SM);
314
315
  /// Constructs a location for the end of the enclosing declaration body.
316
  /// Defaults to the end of brace.
317
  static PathDiagnosticLocation createDeclEnd(const LocationContext *LC,
318
                                                   const SourceManager &SM);
319
320
  /// Create a location corresponding to the given valid ProgramPoint.
321
  static PathDiagnosticLocation create(const ProgramPoint &P,
322
                                       const SourceManager &SMng);
323
324
  /// Convert the given location into a single kind location.
325
  static PathDiagnosticLocation createSingleLocation(
326
                                             const PathDiagnosticLocation &PDL);
327
328
  /// Construct a source location that corresponds to either the beginning
329
  /// or the end of the given statement, or a nearby valid source location
330
  /// if the statement does not have a valid source location of its own.
331
  static SourceLocation
332
  getValidSourceLocation(const Stmt *S, LocationOrAnalysisDeclContext LAC,
333
                         bool UseEndOfStatement = false);
334
335
3.75k
  bool operator==(const PathDiagnosticLocation &X) const {
336
3.75k
    return K == X.K && 
Loc == X.Loc3.55k
&&
Range == X.Range1.87k
;
337
3.75k
  }
338
339
1.95k
  bool operator!=(const PathDiagnosticLocation &X) const {
340
1.95k
    return !(*this == X);
341
1.95k
  }
342
343
823k
  bool isValid() const {
344
823k
    return SM != nullptr;
345
823k
  }
346
347
459k
  FullSourceLoc asLocation() const {
348
459k
    return Loc;
349
459k
  }
350
351
31.0k
  PathDiagnosticRange asRange() const {
352
31.0k
    return Range;
353
31.0k
  }
354
355
292k
  const Stmt *asStmt() const { assert(isValid()); return S; }
356
98.3k
  const Stmt *getStmtOrNull() const {
357
98.3k
    if (!isValid())
358
0
      return nullptr;
359
98.3k
    return asStmt();
360
98.3k
  }
361
362
0
  const Decl *asDecl() const { assert(isValid()); return D; }
363
364
29.3k
  bool hasRange() const { return K == StmtK || 
K == RangeK4.49k
||
K == DeclK3.67k
; }
365
366
31.3k
  bool hasValidLocation() const { return asLocation().isValid(); }
367
368
256
  void invalidate() {
369
256
    *this = PathDiagnosticLocation();
370
256
  }
371
372
  void flatten();
373
374
630
  const SourceManager& getManager() const { assert(isValid()); return *SM; }
375
376
  void Profile(llvm::FoldingSetNodeID &ID) const;
377
378
  void dump() const;
379
};
380
381
class PathDiagnosticLocationPair {
382
private:
383
  PathDiagnosticLocation Start, End;
384
385
public:
386
  PathDiagnosticLocationPair(const PathDiagnosticLocation &start,
387
                             const PathDiagnosticLocation &end)
388
15.8k
      : Start(start), End(end) {}
389
390
96.2k
  const PathDiagnosticLocation &getStart() const { return Start; }
391
68.1k
  const PathDiagnosticLocation &getEnd() const { return End; }
392
393
1.03k
  void setStart(const PathDiagnosticLocation &L) { Start = L; }
394
10.4k
  void setEnd(const PathDiagnosticLocation &L) { End = L; }
395
396
3.38k
  void flatten() {
397
3.38k
    Start.flatten();
398
3.38k
    End.flatten();
399
3.38k
  }
400
401
0
  void Profile(llvm::FoldingSetNodeID &ID) const {
402
0
    Start.Profile(ID);
403
0
    End.Profile(ID);
404
0
  }
405
};
406
407
//===----------------------------------------------------------------------===//
408
// Path "pieces" for path-sensitive diagnostics.
409
//===----------------------------------------------------------------------===//
410
411
class PathDiagnosticPiece: public llvm::FoldingSetNode {
412
public:
413
  enum Kind { ControlFlow, Event, Macro, Call, Note, PopUp };
414
  enum DisplayHint { Above, Below };
415
416
private:
417
  const std::string str;
418
  const Kind kind;
419
  const DisplayHint Hint;
420
421
  /// In the containing bug report, this piece is the last piece from
422
  /// the main source file.
423
  bool LastInMainSourceFile = false;
424
425
  /// A constant string that can be used to tag the PathDiagnosticPiece,
426
  /// typically with the identification of the creator.  The actual pointer
427
  /// value is meant to be an identifier; the string itself is useful for
428
  /// debugging.
429
  StringRef Tag;
430
431
  std::vector<SourceRange> ranges;
432
  std::vector<FixItHint> fixits;
433
434
protected:
435
  PathDiagnosticPiece(StringRef s, Kind k, DisplayHint hint = Below);
436
  PathDiagnosticPiece(Kind k, DisplayHint hint = Below);
437
438
public:
439
  PathDiagnosticPiece() = delete;
440
  PathDiagnosticPiece(const PathDiagnosticPiece &) = delete;
441
  PathDiagnosticPiece &operator=(const PathDiagnosticPiece &) = delete;
442
  virtual ~PathDiagnosticPiece();
443
444
12.3k
  StringRef getString() const { return str; }
445
446
  /// Tag this PathDiagnosticPiece with the given C-string.
447
7.41k
  void setTag(const char *tag) { Tag = tag; }
448
449
  /// Return the opaque tag (if any) on the PathDiagnosticPiece.
450
311
  const void *getTag() const { return Tag.data(); }
451
452
  /// Return the string representation of the tag.  This is useful
453
  /// for debugging.
454
14
  StringRef getTagStr() const { return Tag; }
455
456
  /// getDisplayHint - Return a hint indicating where the diagnostic should
457
  ///  be displayed by the PathDiagnosticConsumer.
458
3.13k
  DisplayHint getDisplayHint() const { return Hint; }
459
460
  virtual PathDiagnosticLocation getLocation() const = 0;
461
  virtual void flattenLocations() = 0;
462
463
414k
  Kind getKind() const { return kind; }
464
465
41.7k
  void addRange(SourceRange R) {
466
41.7k
    if (!R.isValid())
467
6
      return;
468
41.7k
    ranges.push_back(R);
469
41.7k
  }
470
471
0
  void addRange(SourceLocation B, SourceLocation E) {
472
0
    if (!B.isValid() || !E.isValid())
473
0
      return;
474
0
    ranges.push_back(SourceRange(B,E));
475
0
  }
476
477
13
  void addFixit(FixItHint F) {
478
13
    fixits.push_back(F);
479
13
  }
480
481
  /// Return the SourceRanges associated with this PathDiagnosticPiece.
482
33.4k
  ArrayRef<SourceRange> getRanges() const { return ranges; }
483
484
  /// Return the fix-it hints associated with this PathDiagnosticPiece.
485
24.6k
  ArrayRef<FixItHint> getFixits() const { return fixits; }
486
487
  virtual void Profile(llvm::FoldingSetNodeID &ID) const;
488
489
2
  void setAsLastInMainSourceFile() {
490
2
    LastInMainSourceFile = true;
491
2
  }
492
493
152
  bool isLastInMainSourceFile() const {
494
152
    return LastInMainSourceFile;
495
152
  }
496
497
  virtual void dump() const = 0;
498
};
499
500
using PathDiagnosticPieceRef = std::shared_ptr<PathDiagnosticPiece>;
501
502
class PathPieces : public std::list<PathDiagnosticPieceRef> {
503
  void flattenTo(PathPieces &Primary, PathPieces &Current,
504
                 bool ShouldFlattenMacros) const;
505
506
public:
507
17.8k
  PathPieces flatten(bool ShouldFlattenMacros) const {
508
17.8k
    PathPieces Result;
509
17.8k
    flattenTo(Result, Result, ShouldFlattenMacros);
510
17.8k
    return Result;
511
17.8k
  }
512
513
  void dump() const;
514
};
515
516
class PathDiagnosticSpotPiece : public PathDiagnosticPiece {
517
private:
518
  PathDiagnosticLocation Pos;
519
520
public:
521
  PathDiagnosticSpotPiece(const PathDiagnosticLocation &pos,
522
                          StringRef s,
523
                          PathDiagnosticPiece::Kind k,
524
                          bool addPosRange = true)
525
31.2k
      : PathDiagnosticPiece(s, k), Pos(pos) {
526
31.2k
    assert(Pos.isValid() && Pos.hasValidLocation() &&
527
31.2k
           "PathDiagnosticSpotPiece's must have a valid location.");
528
31.2k
    if (addPosRange && 
Pos.hasRange()29.3k
)
addRange(Pos.asRange())26.0k
;
529
31.2k
  }
530
531
61.5k
  PathDiagnosticLocation getLocation() const override { return Pos; }
532
19.7k
  void flattenLocations() override { Pos.flatten(); }
533
534
  void Profile(llvm::FoldingSetNodeID &ID) const override;
535
536
0
  static bool classof(const PathDiagnosticPiece *P) {
537
0
    return P->getKind() == Event || P->getKind() == Macro ||
538
0
           P->getKind() == Note || P->getKind() == PopUp;
539
0
  }
540
};
541
542
class PathDiagnosticEventPiece : public PathDiagnosticSpotPiece {
543
  Optional<bool> IsPrunable;
544
545
public:
546
  PathDiagnosticEventPiece(const PathDiagnosticLocation &pos,
547
                           StringRef s, bool addPosRange = true)
548
28.7k
      : PathDiagnosticSpotPiece(pos, s, Event, addPosRange) {}
549
  ~PathDiagnosticEventPiece() override;
550
551
  /// Mark the diagnostic piece as being potentially prunable.  This
552
  /// flag may have been previously set, at which point it will not
553
  /// be reset unless one specifies to do so.
554
5.82k
  void setPrunable(bool isPrunable, bool override = false) {
555
5.82k
    if (IsPrunable.hasValue() && 
!override441
)
556
441
     return;
557
5.38k
    IsPrunable = isPrunable;
558
5.38k
  }
559
560
  /// Return true if the diagnostic piece is prunable.
561
5.02k
  bool isPrunable() const {
562
5.02k
    return IsPrunable.hasValue() ? 
IsPrunable.getValue()1.03k
:
false3.99k
;
563
5.02k
  }
564
565
  void dump() const override;
566
567
37.9k
  static bool classof(const PathDiagnosticPiece *P) {
568
37.9k
    return P->getKind() == Event;
569
37.9k
  }
570
};
571
572
class PathDiagnosticCallPiece : public PathDiagnosticPiece {
573
  const Decl *Caller;
574
  const Decl *Callee = nullptr;
575
576
  // Flag signifying that this diagnostic has only call enter and no matching
577
  // call exit.
578
  bool NoExit;
579
580
  // Flag signifying that the callee function is an Objective-C autosynthesized
581
  // property getter or setter.
582
  bool IsCalleeAnAutosynthesizedPropertyAccessor = false;
583
584
  // The custom string, which should appear after the call Return Diagnostic.
585
  // TODO: Should we allow multiple diagnostics?
586
  std::string CallStackMessage;
587
588
  PathDiagnosticCallPiece(const Decl *callerD,
589
                          const PathDiagnosticLocation &callReturnPos)
590
      : PathDiagnosticPiece(Call), Caller(callerD), NoExit(false),
591
6.87k
        callReturn(callReturnPos) {}
592
  PathDiagnosticCallPiece(PathPieces &oldPath, const Decl *caller)
593
      : PathDiagnosticPiece(Call), Caller(caller), NoExit(true),
594
168
        path(oldPath) {}
595
596
public:
597
  PathDiagnosticLocation callEnter;
598
  PathDiagnosticLocation callEnterWithin;
599
  PathDiagnosticLocation callReturn;
600
  PathPieces path;
601
602
  ~PathDiagnosticCallPiece() override;
603
604
68
  const Decl *getCaller() const { return Caller; }
605
606
441
  const Decl *getCallee() const { return Callee; }
607
  void setCallee(const CallEnter &CE, const SourceManager &SM);
608
609
21
  bool hasCallStackMessage() { return !CallStackMessage.empty(); }
610
19
  void setCallStackMessage(StringRef st) { CallStackMessage = std::string(st); }
611
612
8.20k
  PathDiagnosticLocation getLocation() const override { return callEnter; }
613
614
  std::shared_ptr<PathDiagnosticEventPiece> getCallEnterEvent() const;
615
  std::shared_ptr<PathDiagnosticEventPiece>
616
  getCallEnterWithinCallerEvent() const;
617
  std::shared_ptr<PathDiagnosticEventPiece> getCallExitEvent() const;
618
619
439
  void flattenLocations() override {
620
439
    callEnter.flatten();
621
439
    callReturn.flatten();
622
439
    for (const auto &I : path)
623
1.05k
      I->flattenLocations();
624
439
  }
625
626
  static std::shared_ptr<PathDiagnosticCallPiece>
627
  construct(const CallExitEnd &CE,
628
            const SourceManager &SM);
629
630
  static PathDiagnosticCallPiece *construct(PathPieces &pieces,
631
                                            const Decl *caller);
632
633
  void dump() const override;
634
635
  void Profile(llvm::FoldingSetNodeID &ID) const override;
636
637
96.7k
  static bool classof(const PathDiagnosticPiece *P) {
638
96.7k
    return P->getKind() == Call;
639
96.7k
  }
640
};
641
642
class PathDiagnosticControlFlowPiece : public PathDiagnosticPiece {
643
  std::vector<PathDiagnosticLocationPair> LPairs;
644
645
public:
646
  PathDiagnosticControlFlowPiece(const PathDiagnosticLocation &startPos,
647
                                 const PathDiagnosticLocation &endPos,
648
                                 StringRef s)
649
906
      : PathDiagnosticPiece(s, ControlFlow) {
650
906
    LPairs.push_back(PathDiagnosticLocationPair(startPos, endPos));
651
906
  }
652
653
  PathDiagnosticControlFlowPiece(const PathDiagnosticLocation &startPos,
654
                                 const PathDiagnosticLocation &endPos)
655
14.9k
      : PathDiagnosticPiece(ControlFlow) {
656
14.9k
    LPairs.push_back(PathDiagnosticLocationPair(startPos, endPos));
657
14.9k
  }
658
659
  ~PathDiagnosticControlFlowPiece() override;
660
661
93.6k
  PathDiagnosticLocation getStartLocation() const {
662
93.6k
    assert(!LPairs.empty() &&
663
93.6k
           "PathDiagnosticControlFlowPiece needs at least one location.");
664
0
    return LPairs[0].getStart();
665
93.6k
  }
666
667
65.6k
  PathDiagnosticLocation getEndLocation() const {
668
65.6k
    assert(!LPairs.empty() &&
669
65.6k
           "PathDiagnosticControlFlowPiece needs at least one location.");
670
0
    return LPairs[0].getEnd();
671
65.6k
  }
672
673
1.03k
  void setStartLocation(const PathDiagnosticLocation &L) {
674
1.03k
    LPairs[0].setStart(L);
675
1.03k
  }
676
677
10.4k
  void setEndLocation(const PathDiagnosticLocation &L) {
678
10.4k
    LPairs[0].setEnd(L);
679
10.4k
  }
680
681
0
  void push_back(const PathDiagnosticLocationPair &X) { LPairs.push_back(X); }
682
683
38.1k
  PathDiagnosticLocation getLocation() const override {
684
38.1k
    return getStartLocation();
685
38.1k
  }
686
687
  using iterator = std::vector<PathDiagnosticLocationPair>::iterator;
688
689
3.38k
  iterator begin() { return LPairs.begin(); }
690
3.38k
  iterator end() { return LPairs.end(); }
691
692
3.38k
  void flattenLocations() override {
693
3.38k
    for (auto &I : *this)
694
3.38k
      I.flatten();
695
3.38k
  }
696
697
  using const_iterator =
698
      std::vector<PathDiagnosticLocationPair>::const_iterator;
699
700
2.50k
  const_iterator begin() const { return LPairs.begin(); }
701
2.50k
  const_iterator end() const { return LPairs.end(); }
702
703
138k
  static bool classof(const PathDiagnosticPiece *P) {
704
138k
    return P->getKind() == ControlFlow;
705
138k
  }
706
707
  void dump() const override;
708
709
  void Profile(llvm::FoldingSetNodeID &ID) const override;
710
};
711
712
class PathDiagnosticMacroPiece : public PathDiagnosticSpotPiece {
713
public:
714
  PathDiagnosticMacroPiece(const PathDiagnosticLocation &pos)
715
46
      : PathDiagnosticSpotPiece(pos, "", Macro) {}
716
  ~PathDiagnosticMacroPiece() override;
717
718
  PathPieces subPieces;
719
720
46
  void flattenLocations() override {
721
46
    PathDiagnosticSpotPiece::flattenLocations();
722
46
    for (const auto &I : subPieces)
723
94
      I->flattenLocations();
724
46
  }
725
726
36.7k
  static bool classof(const PathDiagnosticPiece *P) {
727
36.7k
    return P->getKind() == Macro;
728
36.7k
  }
729
730
  void dump() const override;
731
732
  void Profile(llvm::FoldingSetNodeID &ID) const override;
733
};
734
735
class PathDiagnosticNotePiece: public PathDiagnosticSpotPiece {
736
public:
737
  PathDiagnosticNotePiece(const PathDiagnosticLocation &Pos, StringRef S,
738
                          bool AddPosRange = true)
739
227
      : PathDiagnosticSpotPiece(Pos, S, Note, AddPosRange) {}
740
  ~PathDiagnosticNotePiece() override;
741
742
24.9k
  static bool classof(const PathDiagnosticPiece *P) {
743
24.9k
    return P->getKind() == Note;
744
24.9k
  }
745
746
  void dump() const override;
747
748
  void Profile(llvm::FoldingSetNodeID &ID) const override;
749
};
750
751
class PathDiagnosticPopUpPiece: public PathDiagnosticSpotPiece {
752
public:
753
  PathDiagnosticPopUpPiece(const PathDiagnosticLocation &Pos, StringRef S,
754
                           bool AddPosRange = true)
755
2.30k
      : PathDiagnosticSpotPiece(Pos, S, PopUp, AddPosRange) {}
756
  ~PathDiagnosticPopUpPiece() override;
757
758
1.79k
  static bool classof(const PathDiagnosticPiece *P) {
759
1.79k
    return P->getKind() == PopUp;
760
1.79k
  }
761
762
  void dump() const override;
763
764
  void Profile(llvm::FoldingSetNodeID &ID) const override;
765
};
766
767
/// File IDs mapped to sets of line numbers.
768
using FilesToLineNumsMap = std::map<FileID, std::set<unsigned>>;
769
770
/// PathDiagnostic - PathDiagnostic objects represent a single path-sensitive
771
///  diagnostic.  It represents an ordered-collection of PathDiagnosticPieces,
772
///  each which represent the pieces of the path.
773
class PathDiagnostic : public llvm::FoldingSetNode {
774
  std::string CheckerName;
775
  const Decl *DeclWithIssue;
776
  std::string BugType;
777
  std::string VerboseDesc;
778
  std::string ShortDesc;
779
  std::string Category;
780
  std::deque<std::string> OtherDesc;
781
782
  /// Loc The location of the path diagnostic report.
783
  PathDiagnosticLocation Loc;
784
785
  PathPieces pathImpl;
786
  SmallVector<PathPieces *, 3> pathStack;
787
788
  /// Important bug uniqueing location.
789
  /// The location info is useful to differentiate between bugs.
790
  PathDiagnosticLocation UniqueingLoc;
791
  const Decl *UniqueingDecl;
792
793
  /// Lines executed in the path.
794
  std::unique_ptr<FilesToLineNumsMap> ExecutedLines;
795
796
public:
797
  PathDiagnostic() = delete;
798
  PathDiagnostic(StringRef CheckerName, const Decl *DeclWithIssue,
799
                 StringRef bugtype, StringRef verboseDesc, StringRef shortDesc,
800
                 StringRef category, PathDiagnosticLocation LocationToUnique,
801
                 const Decl *DeclToUnique,
802
                 std::unique_ptr<FilesToLineNumsMap> ExecutedLines);
803
  ~PathDiagnostic();
804
805
  const PathPieces &path;
806
807
  /// Return the path currently used by builders for constructing the
808
  /// PathDiagnostic.
809
287k
  PathPieces &getActivePath() {
810
287k
    if (pathStack.empty())
811
146k
      return pathImpl;
812
141k
    return *pathStack.back();
813
287k
  }
814
815
  /// Return a mutable version of 'path'.
816
28.7k
  PathPieces &getMutablePieces() {
817
28.7k
    return pathImpl;
818
28.7k
  }
819
820
  /// Return the unrolled size of the path.
821
  unsigned full_size();
822
823
6.87k
  void pushActivePath(PathPieces *p) { pathStack.push_back(p); }
824
7.04k
  void popActivePath() { if (!pathStack.empty()) 
pathStack.pop_back()6.87k
; }
825
826
7.04k
  bool isWithinCall() const { return !pathStack.empty(); }
827
828
16.4k
  void setEndOfPath(PathDiagnosticPieceRef EndPiece) {
829
16.4k
    assert(!Loc.isValid() && "End location already set!");
830
0
    Loc = EndPiece->getLocation();
831
16.4k
    assert(Loc.isValid() && "Invalid location for end-of-path piece");
832
0
    getActivePath().push_back(std::move(EndPiece));
833
16.4k
  }
834
835
2
  void appendToDesc(StringRef S) {
836
2
    if (!ShortDesc.empty())
837
2
      ShortDesc += S;
838
2
    VerboseDesc += S;
839
2
  }
840
841
1.73k
  StringRef getVerboseDescription() const { return VerboseDesc; }
842
843
16.3k
  StringRef getShortDescription() const {
844
16.3k
    return ShortDesc.empty() ? 
VerboseDesc1.84k
:
ShortDesc14.4k
;
845
16.3k
  }
846
847
14.8k
  StringRef getCheckerName() const { return CheckerName; }
848
2.23k
  StringRef getBugType() const { return BugType; }
849
1.50k
  StringRef getCategory() const { return Category; }
850
851
  using meta_iterator = std::deque<std::string>::const_iterator;
852
853
120
  meta_iterator meta_begin() const { return OtherDesc.begin(); }
854
120
  meta_iterator meta_end() const { return OtherDesc.end(); }
855
0
  void addMeta(StringRef s) { OtherDesc.push_back(std::string(s)); }
856
857
728
  const FilesToLineNumsMap &getExecutedLines() const {
858
728
    return *ExecutedLines;
859
728
  }
860
861
16.4k
  FilesToLineNumsMap &getExecutedLines() {
862
16.4k
    return *ExecutedLines;
863
16.4k
  }
864
865
  /// Return the semantic context where an issue occurred.  If the
866
  /// issue occurs along a path, this represents the "central" area
867
  /// where the bug manifests.
868
1.47k
  const Decl *getDeclWithIssue() const { return DeclWithIssue; }
869
870
2
  void setDeclWithIssue(const Decl *D) {
871
2
    DeclWithIssue = D;
872
2
  }
873
874
353k
  PathDiagnosticLocation getLocation() const {
875
353k
    return Loc;
876
353k
  }
877
878
2
  void setLocation(PathDiagnosticLocation NewLoc) {
879
2
    Loc = NewLoc;
880
2
  }
881
882
  /// Get the location on which the report should be uniqued.
883
58.7k
  PathDiagnosticLocation getUniqueingLoc() const {
884
58.7k
    return UniqueingLoc;
885
58.7k
  }
886
887
  /// Get the declaration containing the uniqueing location.
888
209
  const Decl *getUniqueingDecl() const {
889
209
    return UniqueingDecl;
890
209
  }
891
892
16.4k
  void flattenLocations() {
893
16.4k
    Loc.flatten();
894
16.4k
    for (const auto &I : pathImpl)
895
22.4k
      I->flattenLocations();
896
16.4k
  }
897
898
  /// Profiles the diagnostic, independent of the path it references.
899
  ///
900
  /// This can be used to merge diagnostics that refer to the same issue
901
  /// along different paths.
902
  void Profile(llvm::FoldingSetNodeID &ID) const;
903
904
  /// Profiles the diagnostic, including its path.
905
  ///
906
  /// Two diagnostics with the same issue along different paths will generate
907
  /// different profiles.
908
  void FullProfile(llvm::FoldingSetNodeID &ID) const;
909
};
910
911
} // namespace ento
912
} // namespace clang
913
914
#endif // LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_PATHDIAGNOSTIC_H