25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/Support/Casting.h"
29 using namespace clang;
31 using namespace tooling;
37 class TextDiagnostics :
public PathDiagnosticConsumer {
38 PathDiagnosticConsumerOptions DiagOpts;
41 bool ShouldDisplayPathNotes;
44 TextDiagnostics(PathDiagnosticConsumerOptions DiagOpts,
46 bool ShouldDisplayPathNotes)
47 : DiagOpts(
std::move(DiagOpts)), DiagEng(DiagEng), LO(LO),
48 ShouldDisplayPathNotes(ShouldDisplayPathNotes) {}
49 ~TextDiagnostics()
override {}
51 StringRef
getName()
const override {
return "TextDiagnostics"; }
53 bool supportsLogicalOpControlFlow()
const override {
return true; }
54 bool supportsCrossFileDiagnostics()
const override {
return true; }
56 PathGenerationScheme getGenerationScheme()
const override {
60 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
61 FilesMade *filesMade)
override {
63 DiagOpts.ShouldDisplayWarningsAsErrors
70 auto reportPiece = [&](
unsigned ID,
FullSourceLoc Loc, StringRef String,
73 if (!DiagOpts.ShouldApplyFixIts) {
74 DiagEng.
Report(Loc,
ID) << String << Ranges << Fixits;
78 DiagEng.
Report(Loc,
ID) << String << Ranges;
80 Replacement Repl(
SM, Hint.RemoveRange, Hint.CodeToInsert);
82 if (llvm::Error Err = Repls.add(Repl)) {
83 llvm::errs() <<
"Error applying replacement " << Repl.toString()
84 <<
": " << Err <<
"\n";
89 for (std::vector<const PathDiagnostic *>::iterator I = Diags.begin(),
92 const PathDiagnostic *PD = *I;
93 std::string WarningMsg = (DiagOpts.ShouldDisplayDiagnosticName
94 ?
" [" + PD->getCheckerName() +
"]"
98 reportPiece(WarnID, PD->getLocation().asLocation(),
99 (PD->getShortDescription() + WarningMsg).str(),
100 PD->path.back()->getRanges(), PD->path.back()->getFixits());
103 for (
const auto &Piece : PD->path) {
104 if (!isa<PathDiagnosticNotePiece>(Piece.get()))
107 reportPiece(NoteID, Piece->getLocation().asLocation(),
108 Piece->getString(), Piece->getRanges(),
112 if (!ShouldDisplayPathNotes)
116 PathPieces FlatPath = PD->path.flatten(
true);
117 for (
const auto &Piece : FlatPath) {
118 if (isa<PathDiagnosticNotePiece>(Piece.get()))
121 reportPiece(NoteID, Piece->getLocation().asLocation(),
122 Piece->getString(), Piece->getRanges(),
132 llvm::errs() <<
"An error occurred during applying fix-it.\n";
135 Rewrite.overwriteChangedFiles();
140 void ento::createTextPathDiagnosticConsumer(
145 C.emplace_back(
new TextDiagnostics(std::move(DiagOpts), PP.
getDiagnostics(),
150 void ento::createTextMinimalPathDiagnosticConsumer(
155 C.emplace_back(
new TextDiagnostics(std::move(DiagOpts), PP.
getDiagnostics(),