1 //===--- TextDiagnostics.cpp - Text Diagnostics for Paths -------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the TextDiagnostics object.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Analysis/PathDiagnostic.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "clang/Basic/Version.h"
16 #include "clang/CrossTU/CrossTranslationUnit.h"
17 #include "clang/Frontend/ASTUnit.h"
18 #include "clang/Lex/Preprocessor.h"
19 #include "clang/Rewrite/Core/Rewriter.h"
20 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
21 #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
22 #include "clang/Tooling/Core/Replacement.h"
23 #include "clang/Tooling/Tooling.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/Support/Casting.h"
28 using namespace clang;
30 using namespace tooling;
33 /// Emitsd minimal diagnostics (report message + notes) for the 'none' output
34 /// type to the standard error, or to to compliment many others. Emits detailed
35 /// diagnostics in textual format for the 'text' output type.
36 class TextDiagnostics : public PathDiagnosticConsumer {
37 PathDiagnosticConsumerOptions DiagOpts;
38 DiagnosticsEngine &DiagEng;
39 const LangOptions &LO;
40 bool ShouldDisplayPathNotes;
43 TextDiagnostics(PathDiagnosticConsumerOptions DiagOpts,
44 DiagnosticsEngine &DiagEng, const LangOptions &LO,
45 bool ShouldDisplayPathNotes)
46 : DiagOpts(std::move(DiagOpts)), DiagEng(DiagEng), LO(LO),
47 ShouldDisplayPathNotes(ShouldDisplayPathNotes) {}
48 ~TextDiagnostics() override {}
50 StringRef getName() const override { return "TextDiagnostics"; }
52 bool supportsLogicalOpControlFlow() const override { return true; }
53 bool supportsCrossFileDiagnostics() const override { return true; }
55 PathGenerationScheme getGenerationScheme() const override {
56 return ShouldDisplayPathNotes ? Minimal : None;
59 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
60 FilesMade *filesMade) override {
62 DiagOpts.ShouldDisplayWarningsAsErrors
63 ? DiagEng.getCustomDiagID(DiagnosticsEngine::Error, "%0")
64 : DiagEng.getCustomDiagID(DiagnosticsEngine::Warning, "%0");
65 unsigned NoteID = DiagEng.getCustomDiagID(DiagnosticsEngine::Note, "%0");
66 SourceManager &SM = DiagEng.getSourceManager();
69 auto reportPiece = [&](unsigned ID, FullSourceLoc Loc, StringRef String,
70 ArrayRef<SourceRange> Ranges,
71 ArrayRef<FixItHint> Fixits) {
72 if (!DiagOpts.ShouldApplyFixIts) {
73 DiagEng.Report(Loc, ID) << String << Ranges << Fixits;
77 DiagEng.Report(Loc, ID) << String << Ranges;
78 for (const FixItHint &Hint : Fixits) {
79 Replacement Repl(SM, Hint.RemoveRange, Hint.CodeToInsert);
81 if (llvm::Error Err = Repls.add(Repl)) {
82 llvm::errs() << "Error applying replacement " << Repl.toString()
83 << ": " << Err << "\n";
88 for (std::vector<const PathDiagnostic *>::iterator I = Diags.begin(),
91 const PathDiagnostic *PD = *I;
92 std::string WarningMsg = (DiagOpts.ShouldDisplayDiagnosticName
93 ? " [" + PD->getCheckerName() + "]"
97 reportPiece(WarnID, PD->getLocation().asLocation(),
98 (PD->getShortDescription() + WarningMsg).str(),
99 PD->path.back()->getRanges(), PD->path.back()->getFixits());
101 // First, add extra notes, even if paths should not be included.
102 for (const auto &Piece : PD->path) {
103 if (!isa<PathDiagnosticNotePiece>(Piece.get()))
106 reportPiece(NoteID, Piece->getLocation().asLocation(),
107 Piece->getString(), Piece->getRanges(),
111 if (!ShouldDisplayPathNotes)
114 // Then, add the path notes if necessary.
115 PathPieces FlatPath = PD->path.flatten(/*ShouldFlattenMacros=*/true);
116 for (const auto &Piece : FlatPath) {
117 if (isa<PathDiagnosticNotePiece>(Piece.get()))
120 reportPiece(NoteID, Piece->getLocation().asLocation(),
121 Piece->getString(), Piece->getRanges(),
129 Rewriter Rewrite(SM, LO);
130 if (!applyAllReplacements(Repls, Rewrite)) {
131 llvm::errs() << "An error occured during applying fix-it.\n";
134 Rewrite.overwriteChangedFiles();
137 } // end anonymous namespace
139 void ento::createTextPathDiagnosticConsumer(
140 PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
141 const std::string &Prefix, const clang::Preprocessor &PP,
142 const cross_tu::CrossTranslationUnitContext &CTU) {
143 C.emplace_back(new TextDiagnostics(std::move(DiagOpts), PP.getDiagnostics(),
145 /*ShouldDisplayPathNotes=*/true));
148 void ento::createTextMinimalPathDiagnosticConsumer(
149 PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
150 const std::string &Prefix, const clang::Preprocessor &PP,
151 const cross_tu::CrossTranslationUnitContext &CTU) {
152 C.emplace_back(new TextDiagnostics(std::move(DiagOpts), PP.getDiagnostics(),
154 /*ShouldDisplayPathNotes=*/false));