Coverage Report

Created: 2019-03-22 08:08

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/StaticAnalyzer/Core/BugReporter/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
class ExplodedNode;
56
class SymExpr;
57
58
using SymbolRef = const SymExpr *;
59
60
//===----------------------------------------------------------------------===//
61
// High-level interface for handlers of path-sensitive diagnostics.
62
//===----------------------------------------------------------------------===//
63
64
class PathDiagnostic;
65
66
class PathDiagnosticConsumer {
67
public:
68
  class PDFileEntry : public llvm::FoldingSetNode {
69
  public:
70
79
    PDFileEntry(llvm::FoldingSetNodeID &NodeID) : NodeID(NodeID) {}
71
72
    using ConsumerFiles = std::vector<std::pair<StringRef, StringRef>>;
73
74
    /// A vector of <consumer,file> pairs.
75
    ConsumerFiles files;
76
77
    /// A precomputed hash tag used for uniquing PDFileEntry objects.
78
    const llvm::FoldingSetNodeID NodeID;
79
80
    /// Used for profiling in the FoldingSet.
81
13
    void Profile(llvm::FoldingSetNodeID &ID) { ID = NodeID; }
82
  };
83
84
  class FilesMade {
85
    llvm::BumpPtrAllocator Alloc;
86
    llvm::FoldingSet<PDFileEntry> Set;
87
88
  public:
89
    ~FilesMade();
90
91
590
    bool empty() const { return Set.empty(); }
92
93
    void addDiagnostic(const PathDiagnostic &PD,
94
                       StringRef ConsumerName,
95
                       StringRef fileName);
96
97
    PDFileEntry::ConsumerFiles *getFiles(const PathDiagnostic &PD);
98
  };
99
100
private:
101
  virtual void anchor();
102
103
public:
104
937
  PathDiagnosticConsumer() = default;
105
  virtual ~PathDiagnosticConsumer();
106
107
  void FlushDiagnostics(FilesMade *FilesMade);
108
109
  virtual void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
110
                                    FilesMade *filesMade) = 0;
111
112
  virtual StringRef getName() const = 0;
113
114
  void HandlePathDiagnostic(std::unique_ptr<PathDiagnostic> D);
115
116
  enum PathGenerationScheme {
117
    /// Only runs visitors, no output generated.
118
    None,
119
120
    /// Used for HTML, SARIF, and text output.
121
    Minimal,
122
123
    /// Used for plist output, used for "arrows" generation.
124
    Extensive,
125
  };
126
127
83
  virtual PathGenerationScheme getGenerationScheme() const { return Minimal; }
128
0
  virtual bool supportsLogicalOpControlFlow() const { return false; }
129
130
  /// Return true if the PathDiagnosticConsumer supports individual
131
  /// PathDiagnostics that span multiple files.
132
1
  virtual bool supportsCrossFileDiagnostics() const { return false; }
133
134
protected:
135
  bool flushed = false;
136
  llvm::FoldingSet<PathDiagnostic> Diags;
137
};
138
139
//===----------------------------------------------------------------------===//
140
// Path-sensitive diagnostics.
141
//===----------------------------------------------------------------------===//
142
143
0
class PathDiagnosticRange : public SourceRange {
Unexecuted instantiation: clang::ento::PathDiagnosticRange::operator=(clang::ento::PathDiagnosticRange&&)
Unexecuted instantiation: clang::ento::PathDiagnosticRange::operator=(clang::ento::PathDiagnosticRange const&)
144
public:
145
  bool isPoint = false;
146
147
  PathDiagnosticRange(SourceRange R, bool isP = false)
148
81.8k
      : SourceRange(R), isPoint(isP) {}
149
45.2k
  PathDiagnosticRange() = default;
150
};
151
152
using LocationOrAnalysisDeclContext =
153
    llvm::PointerUnion<const LocationContext *, AnalysisDeclContext *>;
154
155
0
class PathDiagnosticLocation {
Unexecuted instantiation: clang::ento::PathDiagnosticLocation::operator=(clang::ento::PathDiagnosticLocation const&)
Unexecuted instantiation: clang::ento::PathDiagnosticLocation::operator=(clang::ento::PathDiagnosticLocation&&)
156
private:
157
  enum Kind { RangeK, SingleLocK, StmtK, DeclK } K = SingleLocK;
158
159
  const Stmt *S = nullptr;
160
  const Decl *D = nullptr;
161
  const SourceManager *SM = nullptr;
162
  FullSourceLoc Loc;
163
  PathDiagnosticRange Range;
164
165
  PathDiagnosticLocation(SourceLocation L, const SourceManager &sm, Kind kind)
166
12.3k
      : K(kind), SM(&sm), Loc(genLocation(L)), Range(genRange()) {}
167
168
  FullSourceLoc genLocation(
169
      SourceLocation L = SourceLocation(),
170
      LocationOrAnalysisDeclContext LAC = (AnalysisDeclContext *)nullptr) const;
171
172
  PathDiagnosticRange genRange(
173
      LocationOrAnalysisDeclContext LAC = (AnalysisDeclContext *)nullptr) const;
174
175
public:
176
  /// Create an invalid location.
177
45.2k
  PathDiagnosticLocation() = default;
178
179
  /// Create a location corresponding to the given statement.
180
  PathDiagnosticLocation(const Stmt *s, const SourceManager &sm,
181
                         LocationOrAnalysisDeclContext lac)
182
      : K(s->getBeginLoc().isValid() ? StmtK : SingleLocK),
183
        S(K == StmtK ? s : nullptr), SM(&sm),
184
69.0k
        Loc(genLocation(SourceLocation(), lac)), Range(genRange(lac)) {
185
69.0k
    assert(K == SingleLocK || S);
186
69.0k
    assert(K == SingleLocK || Loc.isValid());
187
69.0k
    assert(K == SingleLocK || Range.isValid());
188
69.0k
  }
189
190
  /// Create a location corresponding to the given declaration.
191
  PathDiagnosticLocation(const Decl *d, const SourceManager &sm)
192
302
      : K(DeclK), D(d), SM(&sm), Loc(genLocation()), Range(genRange()) {
193
302
    assert(D);
194
302
    assert(Loc.isValid());
195
302
    assert(Range.isValid());
196
302
  }
197
198
  /// Create a location at an explicit offset in the source.
199
  ///
200
  /// This should only be used if there are no more appropriate constructors.
201
  PathDiagnosticLocation(SourceLocation loc, const SourceManager &sm)
202
119
      : SM(&sm), Loc(loc, sm), Range(genRange()) {
203
119
    assert(Loc.isValid());
204
119
    assert(Range.isValid());
205
119
  }
206
207
  /// Create a location corresponding to the given declaration.
208
  static PathDiagnosticLocation create(const Decl *D,
209
284
                                       const SourceManager &SM) {
210
284
    return PathDiagnosticLocation(D, SM);
211
284
  }
212
213
  /// Create a location for the beginning of the declaration.
214
  static PathDiagnosticLocation createBegin(const Decl *D,
215
                                            const SourceManager &SM);
216
217
  /// Create a location for the beginning of the declaration.
218
  /// The third argument is ignored, useful for generic treatment
219
  /// of statements and declarations.
220
  static PathDiagnosticLocation
221
  createBegin(const Decl *D, const SourceManager &SM,
222
0
              const LocationOrAnalysisDeclContext LAC) {
223
0
    return createBegin(D, SM);
224
0
  }
225
226
  /// Create a location for the beginning of the statement.
227
  static PathDiagnosticLocation createBegin(const Stmt *S,
228
                                            const SourceManager &SM,
229
                                            const LocationOrAnalysisDeclContext LAC);
230
231
  /// Create a location for the end of the statement.
232
  ///
233
  /// If the statement is a CompoundStatement, the location will point to the
234
  /// closing brace instead of following it.
235
  static PathDiagnosticLocation createEnd(const Stmt *S,
236
                                          const SourceManager &SM,
237
                                       const LocationOrAnalysisDeclContext LAC);
238
239
  /// Create the location for the operator of the binary expression.
240
  /// Assumes the statement has a valid location.
241
  static PathDiagnosticLocation createOperatorLoc(const BinaryOperator *BO,
242
                                                  const SourceManager &SM);
243
  static PathDiagnosticLocation createConditionalColonLoc(
244
                                                  const ConditionalOperator *CO,
245
                                                  const SourceManager &SM);
246
247
  /// For member expressions, return the location of the '.' or '->'.
248
  /// Assumes the statement has a valid location.
249
  static PathDiagnosticLocation createMemberLoc(const MemberExpr *ME,
250
                                                const SourceManager &SM);
251
252
  /// Create a location for the beginning of the compound statement.
253
  /// Assumes the statement has a valid location.
254
  static PathDiagnosticLocation createBeginBrace(const CompoundStmt *CS,
255
                                                 const SourceManager &SM);
256
257
  /// Create a location for the end of the compound statement.
258
  /// Assumes the statement has a valid location.
259
  static PathDiagnosticLocation createEndBrace(const CompoundStmt *CS,
260
                                               const SourceManager &SM);
261
262
  /// Create a location for the beginning of the enclosing declaration body.
263
  /// Defaults to the beginning of the first statement in the declaration body.
264
  static PathDiagnosticLocation createDeclBegin(const LocationContext *LC,
265
                                                const SourceManager &SM);
266
267
  /// Constructs a location for the end of the enclosing declaration body.
268
  /// Defaults to the end of brace.
269
  static PathDiagnosticLocation createDeclEnd(const LocationContext *LC,
270
                                                   const SourceManager &SM);
271
272
  /// Create a location corresponding to the given valid ExplodedNode.
273
  static PathDiagnosticLocation create(const ProgramPoint &P,
274
                                       const SourceManager &SMng);
275
276
  /// Create a location corresponding to the next valid ExplodedNode as end
277
  /// of path location.
278
  static PathDiagnosticLocation createEndOfPath(const ExplodedNode* N,
279
                                                const SourceManager &SM);
280
281
  /// Convert the given location into a single kind location.
282
  static PathDiagnosticLocation createSingleLocation(
283
                                             const PathDiagnosticLocation &PDL);
284
285
2.81k
  bool operator==(const PathDiagnosticLocation &X) const {
286
2.81k
    return K == X.K && 
Loc == X.Loc2.66k
&&
Range == X.Range1.50k
;
287
2.81k
  }
288
289
1.35k
  bool operator!=(const PathDiagnosticLocation &X) const {
290
1.35k
    return !(*this == X);
291
1.35k
  }
292
293
190k
  bool isValid() const {
294
190k
    return SM != nullptr;
295
190k
  }
296
297
226k
  FullSourceLoc asLocation() const {
298
226k
    return Loc;
299
226k
  }
300
301
20.7k
  PathDiagnosticRange asRange() const {
302
20.7k
    return Range;
303
20.7k
  }
304
305
229k
  const Stmt *asStmt() const { assert(isValid()); return S; }
306
81.7k
  const Stmt *getStmtOrNull() const {
307
81.7k
    if (!isValid())
308
0
      return nullptr;
309
81.7k
    return asStmt();
310
81.7k
  }
311
312
0
  const Decl *asDecl() const { assert(isValid()); return D; }
313
314
19.4k
  bool hasRange() const { return K == StmtK || 
K == RangeK3.83k
||
K == DeclK3.16k
; }
315
316
115
  void invalidate() {
317
115
    *this = PathDiagnosticLocation();
318
115
  }
319
320
  void flatten();
321
322
566
  const SourceManager& getManager() const { assert(isValid()); return *SM; }
323
324
  void Profile(llvm::FoldingSetNodeID &ID) const;
325
326
  void dump() const;
327
328
  /// Given an exploded node, retrieve the statement that should be used
329
  /// for the diagnostic location.
330
  static const Stmt *getStmt(const ExplodedNode *N);
331
332
  /// Retrieve the statement corresponding to the successor node.
333
  static const Stmt *getNextStmt(const ExplodedNode *N);
334
};
335
336
class PathDiagnosticLocationPair {
337
private:
338
  PathDiagnosticLocation Start, End;
339
340
public:
341
  PathDiagnosticLocationPair(const PathDiagnosticLocation &start,
342
                             const PathDiagnosticLocation &end)
343
12.7k
      : Start(start), End(end) {}
344
345
78.9k
  const PathDiagnosticLocation &getStart() const { return Start; }
346
56.5k
  const PathDiagnosticLocation &getEnd() const { return End; }
347
348
889
  void setStart(const PathDiagnosticLocation &L) { Start = L; }
349
8.78k
  void setEnd(const PathDiagnosticLocation &L) { End = L; }
350
351
2.52k
  void flatten() {
352
2.52k
    Start.flatten();
353
2.52k
    End.flatten();
354
2.52k
  }
355
356
0
  void Profile(llvm::FoldingSetNodeID &ID) const {
357
0
    Start.Profile(ID);
358
0
    End.Profile(ID);
359
0
  }
360
};
361
362
//===----------------------------------------------------------------------===//
363
// Path "pieces" for path-sensitive diagnostics.
364
//===----------------------------------------------------------------------===//
365
366
class PathDiagnosticPiece: public llvm::FoldingSetNode {
367
public:
368
  enum Kind { ControlFlow, Event, Macro, Call, Note };
369
  enum DisplayHint { Above, Below };
370
371
private:
372
  const std::string str;
373
  const Kind kind;
374
  const DisplayHint Hint;
375
376
  /// In the containing bug report, this piece is the last piece from
377
  /// the main source file.
378
  bool LastInMainSourceFile = false;
379
380
  /// A constant string that can be used to tag the PathDiagnosticPiece,
381
  /// typically with the identification of the creator.  The actual pointer
382
  /// value is meant to be an identifier; the string itself is useful for
383
  /// debugging.
384
  StringRef Tag;
385
386
  std::vector<SourceRange> ranges;
387
388
protected:
389
  PathDiagnosticPiece(StringRef s, Kind k, DisplayHint hint = Below);
390
  PathDiagnosticPiece(Kind k, DisplayHint hint = Below);
391
392
public:
393
  PathDiagnosticPiece() = delete;
394
  PathDiagnosticPiece(const PathDiagnosticPiece &) = delete;
395
  PathDiagnosticPiece &operator=(const PathDiagnosticPiece &) = delete;
396
  virtual ~PathDiagnosticPiece();
397
398
8.28k
  StringRef getString() const { return str; }
399
400
  /// Tag this PathDiagnosticPiece with the given C-string.
401
3.45k
  void setTag(const char *tag) { Tag = tag; }
402
403
  /// Return the opaque tag (if any) on the PathDiagnosticPiece.
404
245
  const void *getTag() const { return Tag.data(); }
405
406
  /// Return the string representation of the tag.  This is useful
407
  /// for debugging.
408
9
  StringRef getTagStr() const { return Tag; }
409
410
  /// getDisplayHint - Return a hint indicating where the diagnostic should
411
  ///  be displayed by the PathDiagnosticConsumer.
412
2.01k
  DisplayHint getDisplayHint() const { return Hint; }
413
414
  virtual PathDiagnosticLocation getLocation() const = 0;
415
  virtual void flattenLocations() = 0;
416
417
241k
  Kind getKind() const { return kind; }
418
419
27.3k
  void addRange(SourceRange R) {
420
27.3k
    if (!R.isValid())
421
0
      return;
422
27.3k
    ranges.push_back(R);
423
27.3k
  }
424
425
0
  void addRange(SourceLocation B, SourceLocation E) {
426
0
    if (!B.isValid() || !E.isValid())
427
0
      return;
428
0
    ranges.push_back(SourceRange(B,E));
429
0
  }
430
431
  /// Return the SourceRanges associated with this PathDiagnosticPiece.
432
24.5k
  ArrayRef<SourceRange> getRanges() const { return ranges; }
433
434
  virtual void Profile(llvm::FoldingSetNodeID &ID) const;
435
436
2
  void setAsLastInMainSourceFile() {
437
2
    LastInMainSourceFile = true;
438
2
  }
439
440
141
  bool isLastInMainSourceFile() const {
441
141
    return LastInMainSourceFile;
442
141
  }
443
444
  virtual void dump() const = 0;
445
};
446
447
class PathPieces : public std::list<std::shared_ptr<PathDiagnosticPiece>> {
448
  void flattenTo(PathPieces &Primary, PathPieces &Current,
449
                 bool ShouldFlattenMacros) const;
450
451
public:
452
12.2k
  PathPieces flatten(bool ShouldFlattenMacros) const {
453
12.2k
    PathPieces Result;
454
12.2k
    flattenTo(Result, Result, ShouldFlattenMacros);
455
12.2k
    return Result;
456
12.2k
  }
457
458
  void dump() const;
459
};
460
461
class PathDiagnosticSpotPiece : public PathDiagnosticPiece {
462
private:
463
  PathDiagnosticLocation Pos;
464
465
public:
466
  PathDiagnosticSpotPiece(const PathDiagnosticLocation &pos,
467
                          StringRef s,
468
                          PathDiagnosticPiece::Kind k,
469
                          bool addPosRange = true)
470
20.7k
      : PathDiagnosticPiece(s, k), Pos(pos) {
471
20.7k
    assert(Pos.isValid() && Pos.asLocation().isValid() &&
472
20.7k
           "PathDiagnosticSpotPiece's must have a valid location.");
473
20.7k
    if (addPosRange && 
Pos.hasRange()19.4k
)
addRange(Pos.asRange())16.5k
;
474
20.7k
  }
475
476
43.5k
  PathDiagnosticLocation getLocation() const override { return Pos; }
477
13.6k
  void flattenLocations() override { Pos.flatten(); }
478
479
  void Profile(llvm::FoldingSetNodeID &ID) const override;
480
481
0
  static bool classof(const PathDiagnosticPiece *P) {
482
0
    return P->getKind() == Event || P->getKind() == Macro ||
483
0
           P->getKind() == Note;
484
0
  }
485
};
486
487
/// Interface for classes constructing Stack hints.
488
///
489
/// If a PathDiagnosticEvent occurs in a different frame than the final
490
/// diagnostic the hints can be used to summarize the effect of the call.
491
class StackHintGenerator {
492
public:
493
  virtual ~StackHintGenerator() = 0;
494
495
  /// Construct the Diagnostic message for the given ExplodedNode.
496
  virtual std::string getMessage(const ExplodedNode *N) = 0;
497
};
498
499
/// Constructs a Stack hint for the given symbol.
500
///
501
/// The class knows how to construct the stack hint message based on
502
/// traversing the CallExpr associated with the call and checking if the given
503
/// symbol is returned or is one of the arguments.
504
/// The hint can be customized by redefining 'getMessageForX()' methods.
505
class StackHintGeneratorForSymbol : public StackHintGenerator {
506
private:
507
  SymbolRef Sym;
508
  std::string Msg;
509
510
public:
511
681
  StackHintGeneratorForSymbol(SymbolRef S, StringRef M) : Sym(S), Msg(M) {}
512
681
  ~StackHintGeneratorForSymbol() override = default;
513
514
  /// Search the call expression for the symbol Sym and dispatch the
515
  /// 'getMessageForX()' methods to construct a specific message.
516
  std::string getMessage(const ExplodedNode *N) override;
517
518
  /// Produces the message of the following form:
519
  ///   'Msg via Nth parameter'
520
  virtual std::string getMessageForArg(const Expr *ArgE, unsigned ArgIndex);
521
522
7
  virtual std::string getMessageForReturn(const CallExpr *CallExpr) {
523
7
    return Msg;
524
7
  }
525
526
6
  virtual std::string getMessageForSymbolNotFound() {
527
6
    return Msg;
528
6
  }
529
};
530
531
class PathDiagnosticEventPiece : public PathDiagnosticSpotPiece {
532
  Optional<bool> IsPrunable;
533
534
  /// If the event occurs in a different frame than the final diagnostic,
535
  /// supply a message that will be used to construct an extra hint on the
536
  /// returns from all the calls on the stack from this event to the final
537
  /// diagnostic.
538
  std::unique_ptr<StackHintGenerator> CallStackHint;
539
540
public:
541
  PathDiagnosticEventPiece(const PathDiagnosticLocation &pos,
542
                           StringRef s, bool addPosRange = true,
543
                           StackHintGenerator *stackHint = nullptr)
544
      : PathDiagnosticSpotPiece(pos, s, Event, addPosRange),
545
20.6k
        CallStackHint(stackHint) {}
546
  ~PathDiagnosticEventPiece() override;
547
548
  /// Mark the diagnostic piece as being potentially prunable.  This
549
  /// flag may have been previously set, at which point it will not
550
  /// be reset unless one specifies to do so.
551
3.57k
  void setPrunable(bool isPrunable, bool override = false) {
552
3.57k
    if (IsPrunable.hasValue() && 
!override274
)
553
274
     return;
554
3.29k
    IsPrunable = isPrunable;
555
3.29k
  }
556
557
  /// Return true if the diagnostic piece is prunable.
558
3.45k
  bool isPrunable() const {
559
3.45k
    return IsPrunable.hasValue() ? 
IsPrunable.getValue()533
:
false2.92k
;
560
3.45k
  }
561
562
1.97k
  bool hasCallStackHint() { return (bool)CallStackHint; }
563
564
  /// Produce the hint for the given node. The node contains
565
  /// information about the call for which the diagnostic can be generated.
566
20
  std::string getCallStackMessage(const ExplodedNode *N) {
567
20
    if (CallStackHint)
568
20
      return CallStackHint->getMessage(N);
569
0
    return {};
570
0
  }
571
572
  void dump() const override;
573
574
15.8k
  static bool classof(const PathDiagnosticPiece *P) {
575
15.8k
    return P->getKind() == Event;
576
15.8k
  }
577
};
578
579
class PathDiagnosticCallPiece : public PathDiagnosticPiece {
580
  const Decl *Caller;
581
  const Decl *Callee = nullptr;
582
583
  // Flag signifying that this diagnostic has only call enter and no matching
584
  // call exit.
585
  bool NoExit;
586
587
  // Flag signifying that the callee function is an Objective-C autosynthesized
588
  // property getter or setter.
589
  bool IsCalleeAnAutosynthesizedPropertyAccessor = false;
590
591
  // The custom string, which should appear after the call Return Diagnostic.
592
  // TODO: Should we allow multiple diagnostics?
593
  std::string CallStackMessage;
594
595
  PathDiagnosticCallPiece(const Decl *callerD,
596
                          const PathDiagnosticLocation &callReturnPos)
597
      : PathDiagnosticPiece(Call), Caller(callerD), NoExit(false),
598
4.55k
        callReturn(callReturnPos) {}
599
  PathDiagnosticCallPiece(PathPieces &oldPath, const Decl *caller)
600
      : PathDiagnosticPiece(Call), Caller(caller), NoExit(true),
601
190
        path(oldPath) {}
602
603
public:
604
  PathDiagnosticLocation callEnter;
605
  PathDiagnosticLocation callEnterWithin;
606
  PathDiagnosticLocation callReturn;
607
  PathPieces path;
608
609
  ~PathDiagnosticCallPiece() override;
610
611
80
  const Decl *getCaller() const { return Caller; }
612
613
388
  const Decl *getCallee() const { return Callee; }
614
  void setCallee(const CallEnter &CE, const SourceManager &SM);
615
616
20
  bool hasCallStackMessage() { return !CallStackMessage.empty(); }
617
18
  void setCallStackMessage(StringRef st) { CallStackMessage = st; }
618
619
5.77k
  PathDiagnosticLocation getLocation() const override { return callEnter; }
620
621
  std::shared_ptr<PathDiagnosticEventPiece> getCallEnterEvent() const;
622
  std::shared_ptr<PathDiagnosticEventPiece>
623
  getCallEnterWithinCallerEvent() const;
624
  std::shared_ptr<PathDiagnosticEventPiece> getCallExitEvent() const;
625
626
386
  void flattenLocations() override {
627
386
    callEnter.flatten();
628
386
    callReturn.flatten();
629
386
    for (const auto &I : path)
630
837
      I->flattenLocations();
631
386
  }
632
633
  static std::shared_ptr<PathDiagnosticCallPiece>
634
  construct(const CallExitEnd &CE,
635
            const SourceManager &SM);
636
637
  static PathDiagnosticCallPiece *construct(PathPieces &pieces,
638
                                            const Decl *caller);
639
640
  void dump() const override;
641
642
  void Profile(llvm::FoldingSetNodeID &ID) const override;
643
644
63.8k
  static bool classof(const PathDiagnosticPiece *P) {
645
63.8k
    return P->getKind() == Call;
646
63.8k
  }
647
};
648
649
class PathDiagnosticControlFlowPiece : public PathDiagnosticPiece {
650
  std::vector<PathDiagnosticLocationPair> LPairs;
651
652
public:
653
  PathDiagnosticControlFlowPiece(const PathDiagnosticLocation &startPos,
654
                                 const PathDiagnosticLocation &endPos,
655
                                 StringRef s)
656
460
      : PathDiagnosticPiece(s, ControlFlow) {
657
460
    LPairs.push_back(PathDiagnosticLocationPair(startPos, endPos));
658
460
  }
659
660
  PathDiagnosticControlFlowPiece(const PathDiagnosticLocation &startPos,
661
                                 const PathDiagnosticLocation &endPos)
662
12.3k
      : PathDiagnosticPiece(ControlFlow) {
663
12.3k
    LPairs.push_back(PathDiagnosticLocationPair(startPos, endPos));
664
12.3k
  }
665
666
  ~PathDiagnosticControlFlowPiece() override;
667
668
76.8k
  PathDiagnosticLocation getStartLocation() const {
669
76.8k
    assert(!LPairs.empty() &&
670
76.8k
           "PathDiagnosticControlFlowPiece needs at least one location.");
671
76.8k
    return LPairs[0].getStart();
672
76.8k
  }
673
674
54.4k
  PathDiagnosticLocation getEndLocation() const {
675
54.4k
    assert(!LPairs.empty() &&
676
54.4k
           "PathDiagnosticControlFlowPiece needs at least one location.");
677
54.4k
    return LPairs[0].getEnd();
678
54.4k
  }
679
680
889
  void setStartLocation(const PathDiagnosticLocation &L) {
681
889
    LPairs[0].setStart(L);
682
889
  }
683
684
8.78k
  void setEndLocation(const PathDiagnosticLocation &L) {
685
8.78k
    LPairs[0].setEnd(L);
686
8.78k
  }
687
688
0
  void push_back(const PathDiagnosticLocationPair &X) { LPairs.push_back(X); }
689
690
30.8k
  PathDiagnosticLocation getLocation() const override {
691
30.8k
    return getStartLocation();
692
30.8k
  }
693
694
  using iterator = std::vector<PathDiagnosticLocationPair>::iterator;
695
696
2.52k
  iterator begin() { return LPairs.begin(); }
697
2.52k
  iterator end() { return LPairs.end(); }
698
699
2.52k
  void flattenLocations() override {
700
2.52k
    for (auto &I : *this)
701
2.52k
      I.flatten();
702
2.52k
  }
703
704
  using const_iterator =
705
      std::vector<PathDiagnosticLocationPair>::const_iterator;
706
707
2.08k
  const_iterator begin() const { return LPairs.begin(); }
708
2.08k
  const_iterator end() const { return LPairs.end(); }
709
710
64.3k
  static bool classof(const PathDiagnosticPiece *P) {
711
64.3k
    return P->getKind() == ControlFlow;
712
64.3k
  }
713
714
  void dump() const override;
715
716
  void Profile(llvm::FoldingSetNodeID &ID) const override;
717
};
718
719
class PathDiagnosticMacroPiece : public PathDiagnosticSpotPiece {
720
public:
721
  PathDiagnosticMacroPiece(const PathDiagnosticLocation &pos)
722
31
      : PathDiagnosticSpotPiece(pos, "", Macro) {}
723
  ~PathDiagnosticMacroPiece() override;
724
725
  PathPieces subPieces;
726
727
  bool containsEvent() const;
728
729
31
  void flattenLocations() override {
730
31
    PathDiagnosticSpotPiece::flattenLocations();
731
31
    for (const auto &I : subPieces)
732
68
      I->flattenLocations();
733
31
  }
734
735
29.0k
  static bool classof(const PathDiagnosticPiece *P) {
736
29.0k
    return P->getKind() == Macro;
737
29.0k
  }
738
739
  void dump() const override;
740
741
  void Profile(llvm::FoldingSetNodeID &ID) const override;
742
};
743
744
class PathDiagnosticNotePiece: public PathDiagnosticSpotPiece {
745
public:
746
  PathDiagnosticNotePiece(const PathDiagnosticLocation &Pos, StringRef S,
747
                               bool AddPosRange = true)
748
164
      : PathDiagnosticSpotPiece(Pos, S, Note, AddPosRange) {}
749
  ~PathDiagnosticNotePiece() override;
750
751
15.0k
  static bool classof(const PathDiagnosticPiece *P) {
752
15.0k
    return P->getKind() == Note;
753
15.0k
  }
754
755
  void dump() const override;
756
757
  void Profile(llvm::FoldingSetNodeID &ID) const override;
758
};
759
760
/// File IDs mapped to sets of line numbers.
761
using FilesToLineNumsMap = std::map<FileID, std::set<unsigned>>;
762
763
/// PathDiagnostic - PathDiagnostic objects represent a single path-sensitive
764
///  diagnostic.  It represents an ordered-collection of PathDiagnosticPieces,
765
///  each which represent the pieces of the path.
766
class PathDiagnostic : public llvm::FoldingSetNode {
767
  std::string CheckName;
768
  const Decl *DeclWithIssue;
769
  std::string BugType;
770
  std::string VerboseDesc;
771
  std::string ShortDesc;
772
  std::string Category;
773
  std::deque<std::string> OtherDesc;
774
775
  /// Loc The location of the path diagnostic report.
776
  PathDiagnosticLocation Loc;
777
778
  PathPieces pathImpl;
779
  SmallVector<PathPieces *, 3> pathStack;
780
781
  /// Important bug uniqueing location.
782
  /// The location info is useful to differentiate between bugs.
783
  PathDiagnosticLocation UniqueingLoc;
784
  const Decl *UniqueingDecl;
785
786
  /// Lines executed in the path.
787
  std::unique_ptr<FilesToLineNumsMap> ExecutedLines;
788
789
public:
790
  PathDiagnostic() = delete;
791
  PathDiagnostic(StringRef CheckName, const Decl *DeclWithIssue,
792
                 StringRef bugtype, StringRef verboseDesc, StringRef shortDesc,
793
                 StringRef category, PathDiagnosticLocation LocationToUnique,
794
                 const Decl *DeclToUnique,
795
                 std::unique_ptr<FilesToLineNumsMap> ExecutedLines);
796
  ~PathDiagnostic();
797
798
  const PathPieces &path;
799
800
  /// Return the path currently used by builders for constructing the
801
  /// PathDiagnostic.
802
70.5k
  PathPieces &getActivePath() {
803
70.5k
    if (pathStack.empty())
804
60.5k
      return pathImpl;
805
10.0k
    return *pathStack.back();
806
10.0k
  }
807
808
  /// Return a mutable version of 'path'.
809
20.1k
  PathPieces &getMutablePieces() {
810
20.1k
    return pathImpl;
811
20.1k
  }
812
813
  /// Return the unrolled size of the path.
814
  unsigned full_size();
815
816
4.55k
  void pushActivePath(PathPieces *p) { pathStack.push_back(p); }
817
4.74k
  void popActivePath() { if (!pathStack.empty()) 
pathStack.pop_back()4.55k
; }
818
819
4.74k
  bool isWithinCall() const { return !pathStack.empty(); }
820
821
11.4k
  void setEndOfPath(std::shared_ptr<PathDiagnosticPiece> EndPiece) {
822
11.4k
    assert(!Loc.isValid() && "End location already set!");
823
11.4k
    Loc = EndPiece->getLocation();
824
11.4k
    assert(Loc.isValid() && "Invalid location for end-of-path piece");
825
11.4k
    getActivePath().push_back(std::move(EndPiece));
826
11.4k
  }
827
828
2
  void appendToDesc(StringRef S) {
829
2
    if (!ShortDesc.empty())
830
0
      ShortDesc += S;
831
2
    VerboseDesc += S;
832
2
  }
833
834
  /// If the last piece of the report point to the header file, resets
835
  /// the location of the report to be the last location in the main source
836
  /// file.
837
  void resetDiagnosticLocationToMainFile();
838
839
516
  StringRef getVerboseDescription() const { return VerboseDesc; }
840
841
11.2k
  StringRef getShortDescription() const {
842
11.2k
    return ShortDesc.empty() ? 
VerboseDesc11.2k
:
ShortDesc48
;
843
11.2k
  }
844
845
1.27k
  StringRef getCheckName() const { return CheckName; }
846
1.63k
  StringRef getBugType() const { return BugType; }
847
852
  StringRef getCategory() const { return Category; }
848
849
  /// Return the semantic context where an issue occurred.  If the
850
  /// issue occurs along a path, this represents the "central" area
851
  /// where the bug manifests.
852
1.35k
  const Decl *getDeclWithIssue() const { return DeclWithIssue; }
853
854
  using meta_iterator = std::deque<std::string>::const_iterator;
855
856
94
  meta_iterator meta_begin() const { return OtherDesc.begin(); }
857
94
  meta_iterator meta_end() const { return OtherDesc.end(); }
858
0
  void addMeta(StringRef s) { OtherDesc.push_back(s); }
859
860
672
  const FilesToLineNumsMap &getExecutedLines() const {
861
672
    return *ExecutedLines;
862
672
  }
863
864
11.4k
  FilesToLineNumsMap &getExecutedLines() {
865
11.4k
    return *ExecutedLines;
866
11.4k
  }
867
868
172k
  PathDiagnosticLocation getLocation() const {
869
172k
    return Loc;
870
172k
  }
871
872
  /// Get the location on which the report should be uniqued.
873
672
  PathDiagnosticLocation getUniqueingLoc() const {
874
672
    return UniqueingLoc;
875
672
  }
876
877
  /// Get the declaration containing the uniqueing location.
878
191
  const Decl *getUniqueingDecl() const {
879
191
    return UniqueingDecl;
880
191
  }
881
882
11.4k
  void flattenLocations() {
883
11.4k
    Loc.flatten();
884
11.4k
    for (const auto &I : pathImpl)
885
15.6k
      I->flattenLocations();
886
11.4k
  }
887
888
  /// Profiles the diagnostic, independent of the path it references.
889
  ///
890
  /// This can be used to merge diagnostics that refer to the same issue
891
  /// along different paths.
892
  void Profile(llvm::FoldingSetNodeID &ID) const;
893
894
  /// Profiles the diagnostic, including its path.
895
  ///
896
  /// Two diagnostics with the same issue along different paths will generate
897
  /// different profiles.
898
  void FullProfile(llvm::FoldingSetNodeID &ID) const;
899
};
900
901
} // namespace ento
902
903
} // namespace clang
904
905
#endif // LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_PATHDIAGNOSTIC_H