Coverage Report

Created: 2023-09-21 18:56

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Core/TextDiagnostics.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- TextDiagnostics.cpp - Text Diagnostics for Paths -------*- 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 TextDiagnostics object.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Analysis/MacroExpansionContext.h"
14
#include "clang/Analysis/PathDiagnostic.h"
15
#include "clang/Basic/SourceManager.h"
16
#include "clang/Basic/Version.h"
17
#include "clang/CrossTU/CrossTranslationUnit.h"
18
#include "clang/Frontend/ASTUnit.h"
19
#include "clang/Lex/Preprocessor.h"
20
#include "clang/Rewrite/Core/Rewriter.h"
21
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
22
#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
23
#include "clang/Tooling/Core/Replacement.h"
24
#include "clang/Tooling/Tooling.h"
25
#include "llvm/ADT/SmallPtrSet.h"
26
#include "llvm/ADT/SmallVector.h"
27
#include "llvm/Support/Casting.h"
28
29
using namespace clang;
30
using namespace ento;
31
using namespace tooling;
32
33
namespace {
34
/// Emits minimal diagnostics (report message + notes) for the 'none' output
35
/// type to the standard error, or to complement many others. Emits detailed
36
/// diagnostics in textual format for the 'text' output type.
37
class TextDiagnostics : public PathDiagnosticConsumer {
38
  PathDiagnosticConsumerOptions DiagOpts;
39
  DiagnosticsEngine &DiagEng;
40
  const LangOptions &LO;
41
  bool ShouldDisplayPathNotes;
42
43
public:
44
  TextDiagnostics(PathDiagnosticConsumerOptions DiagOpts,
45
                  DiagnosticsEngine &DiagEng, const LangOptions &LO,
46
                  bool ShouldDisplayPathNotes)
47
1.80k
      : DiagOpts(std::move(DiagOpts)), DiagEng(DiagEng), LO(LO),
48
1.80k
        ShouldDisplayPathNotes(ShouldDisplayPathNotes) {}
49
1.80k
  ~TextDiagnostics() override {}
50
51
0
  StringRef getName() const override { return "TextDiagnostics"; }
52
53
86
  bool supportsLogicalOpControlFlow() const override { return true; }
54
21.7k
  bool supportsCrossFileDiagnostics() const override { return true; }
55
56
178k
  PathGenerationScheme getGenerationScheme() const override {
57
178k
    return ShouldDisplayPathNotes ? 
Minimal159k
:
None19.1k
;
58
178k
  }
59
60
  void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
61
1.80k
                            FilesMade *filesMade) override {
62
1.80k
    unsigned WarnID =
63
1.80k
        DiagOpts.ShouldDisplayWarningsAsErrors
64
1.80k
            ? 
DiagEng.getCustomDiagID(DiagnosticsEngine::Error, "%0")1
65
1.80k
            : 
DiagEng.getCustomDiagID(DiagnosticsEngine::Warning, "%0")1.79k
;
66
1.80k
    unsigned NoteID = DiagEng.getCustomDiagID(DiagnosticsEngine::Note, "%0");
67
1.80k
    SourceManager &SM = DiagEng.getSourceManager();
68
69
1.80k
    Replacements Repls;
70
1.80k
    auto reportPiece = [&](unsigned ID, FullSourceLoc Loc, StringRef String,
71
1.80k
                           ArrayRef<SourceRange> Ranges,
72
27.4k
                           ArrayRef<FixItHint> Fixits) {
73
27.4k
      if (!DiagOpts.ShouldApplyFixIts) {
74
27.4k
        DiagEng.Report(Loc, ID) << String << Ranges << Fixits;
75
27.4k
        return;
76
27.4k
      }
77
78
61
      DiagEng.Report(Loc, ID) << String << Ranges;
79
61
      for (const FixItHint &Hint : Fixits) {
80
11
        Replacement Repl(SM, Hint.RemoveRange, Hint.CodeToInsert);
81
82
11
        if (llvm::Error Err = Repls.add(Repl)) {
83
0
          llvm::errs() << "Error applying replacement " << Repl.toString()
84
0
                       << ": " << Err << "\n";
85
0
        }
86
11
      }
87
61
    };
88
89
21.6k
    for (const PathDiagnostic *PD : Diags) {
90
21.6k
      std::string WarningMsg = (DiagOpts.ShouldDisplayDiagnosticName
91
21.6k
                                    ? 
" [" + PD->getCheckerName() + "]"19.4k
92
21.6k
                                    : 
""2.20k
)
93
21.6k
                                   .str();
94
95
21.6k
      reportPiece(WarnID, PD->getLocation().asLocation(),
96
21.6k
                  (PD->getShortDescription() + WarningMsg).str(),
97
21.6k
                  PD->path.back()->getRanges(), PD->path.back()->getFixits());
98
99
      // First, add extra notes, even if paths should not be included.
100
25.2k
      for (const auto &Piece : PD->path) {
101
25.2k
        if (!isa<PathDiagnosticNotePiece>(Piece.get()))
102
25.1k
          continue;
103
104
185
        reportPiece(NoteID, Piece->getLocation().asLocation(),
105
185
                    Piece->getString(), Piece->getRanges(),
106
185
                    Piece->getFixits());
107
185
      }
108
109
21.6k
      if (!ShouldDisplayPathNotes)
110
20.1k
        continue;
111
112
      // Then, add the path notes if necessary.
113
1.56k
      PathPieces FlatPath = PD->path.flatten(/*ShouldFlattenMacros=*/true);
114
5.64k
      for (const auto &Piece : FlatPath) {
115
5.64k
        if (isa<PathDiagnosticNotePiece>(Piece.get()))
116
11
          continue;
117
118
5.63k
        reportPiece(NoteID, Piece->getLocation().asLocation(),
119
5.63k
                    Piece->getString(), Piece->getRanges(),
120
5.63k
                    Piece->getFixits());
121
5.63k
      }
122
1.56k
    }
123
124
1.80k
    if (Repls.empty())
125
1.79k
      return;
126
127
3
    Rewriter Rewrite(SM, LO);
128
3
    if (!applyAllReplacements(Repls, Rewrite)) {
129
0
      llvm::errs() << "An error occurred during applying fix-it.\n";
130
0
    }
131
132
3
    Rewrite.overwriteChangedFiles();
133
3
  }
134
};
135
} // end anonymous namespace
136
137
void ento::createTextPathDiagnosticConsumer(
138
    PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
139
    const std::string &Prefix, const Preprocessor &PP,
140
    const cross_tu::CrossTranslationUnitContext &CTU,
141
165
    const MacroExpansionContext &MacroExpansions) {
142
165
  C.emplace_back(new TextDiagnostics(std::move(DiagOpts), PP.getDiagnostics(),
143
165
                                     PP.getLangOpts(),
144
165
                                     /*ShouldDisplayPathNotes=*/true));
145
165
}
146
147
void ento::createTextMinimalPathDiagnosticConsumer(
148
    PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
149
    const std::string &Prefix, const Preprocessor &PP,
150
    const cross_tu::CrossTranslationUnitContext &CTU,
151
1.63k
    const MacroExpansionContext &MacroExpansions) {
152
1.63k
  C.emplace_back(new TextDiagnostics(std::move(DiagOpts), PP.getDiagnostics(),
153
1.63k
                                     PP.getLangOpts(),
154
1.63k
                                     /*ShouldDisplayPathNotes=*/false));
155
1.63k
}