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 : DiagOpts(std::move(DiagOpts)), DiagEng(DiagEng), LO(LO), 48 ShouldDisplayPathNotes(ShouldDisplayPathNotes) {} 49 ~TextDiagnostics() override {} 50 51 StringRef getName() const override { return "TextDiagnostics"; } 52 53 bool supportsLogicalOpControlFlow() const override { return true; } 54 bool supportsCrossFileDiagnostics() const override { return true; } 55 56 PathGenerationScheme getGenerationScheme() const override { 57 return ShouldDisplayPathNotes ? Minimal : None; 58 } 59 60 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags, 61 FilesMade *filesMade) override { 62 unsigned WarnID = 63 DiagOpts.ShouldDisplayWarningsAsErrors 64 ? DiagEng.getCustomDiagID(DiagnosticsEngine::Error, "%0") 65 : DiagEng.getCustomDiagID(DiagnosticsEngine::Warning, "%0"); 66 unsigned NoteID = DiagEng.getCustomDiagID(DiagnosticsEngine::Note, "%0"); 67 SourceManager &SM = DiagEng.getSourceManager(); 68 69 Replacements Repls; 70 auto reportPiece = [&](unsigned ID, FullSourceLoc Loc, StringRef String, 71 ArrayRef<SourceRange> Ranges, 72 ArrayRef<FixItHint> Fixits) { 73 if (!DiagOpts.ShouldApplyFixIts) { 74 DiagEng.Report(Loc, ID) << String << Ranges << Fixits; 75 return; 76 } 77 78 DiagEng.Report(Loc, ID) << String << Ranges; 79 for (const FixItHint &Hint : Fixits) { 80 Replacement Repl(SM, Hint.RemoveRange, Hint.CodeToInsert); 81 82 if (llvm::Error Err = Repls.add(Repl)) { 83 llvm::errs() << "Error applying replacement " << Repl.toString() 84 << ": " << llvm::toString(std::move(Err)) << "\n"; 85 } 86 } 87 }; 88 89 for (const PathDiagnostic *PD : Diags) { 90 std::string WarningMsg = (DiagOpts.ShouldDisplayDiagnosticName 91 ? " [" + PD->getCheckerName() + "]" 92 : "") 93 .str(); 94 reportPiece(WarnID, PD->getLocation().asLocation(), 95 (PD->getShortDescription() + WarningMsg).str(), 96 PD->path.back()->getRanges(), PD->path.back()->getFixits()); 97 98 // First, add extra notes, even if paths should not be included. 99 for (const auto &Piece : PD->path) { 100 if (!isa<PathDiagnosticNotePiece>(Piece.get())) 101 continue; 102 103 reportPiece(NoteID, Piece->getLocation().asLocation(), 104 Piece->getString(), Piece->getRanges(), 105 Piece->getFixits()); 106 } 107 108 if (!ShouldDisplayPathNotes) 109 continue; 110 111 // Then, add the path notes if necessary. 112 PathPieces FlatPath = PD->path.flatten(/*ShouldFlattenMacros=*/true); 113 for (const auto &Piece : FlatPath) { 114 if (isa<PathDiagnosticNotePiece>(Piece.get())) 115 continue; 116 117 reportPiece(NoteID, Piece->getLocation().asLocation(), 118 Piece->getString(), Piece->getRanges(), 119 Piece->getFixits()); 120 } 121 } 122 123 if (Repls.empty()) 124 return; 125 126 Rewriter Rewrite(SM, LO); 127 if (!applyAllReplacements(Repls, Rewrite)) { 128 llvm::errs() << "An error occurred during applying fix-it.\n"; 129 } 130 131 Rewrite.overwriteChangedFiles(); 132 } 133 }; 134 } // end anonymous namespace 135 136 void ento::createTextPathDiagnosticConsumer( 137 PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C, 138 const std::string &Prefix, const Preprocessor &PP, 139 const cross_tu::CrossTranslationUnitContext &CTU, 140 const MacroExpansionContext &MacroExpansions) { 141 C.emplace_back(new TextDiagnostics(std::move(DiagOpts), PP.getDiagnostics(), 142 PP.getLangOpts(), 143 /*ShouldDisplayPathNotes=*/true)); 144 } 145 146 void ento::createTextMinimalPathDiagnosticConsumer( 147 PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C, 148 const std::string &Prefix, const Preprocessor &PP, 149 const cross_tu::CrossTranslationUnitContext &CTU, 150 const MacroExpansionContext &MacroExpansions) { 151 C.emplace_back(new TextDiagnostics(std::move(DiagOpts), PP.getDiagnostics(), 152 PP.getLangOpts(), 153 /*ShouldDisplayPathNotes=*/false)); 154 } 155