1 //===--- Diagnostics.h -------------------------------------------*- 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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H 10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H 11 12 #include "Protocol.h" 13 #include "support/Path.h" 14 #include "clang/Basic/Diagnostic.h" 15 #include "clang/Basic/LangOptions.h" 16 #include "clang/Basic/SourceLocation.h" 17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/ADT/DenseSet.h" 19 #include "llvm/ADT/None.h" 20 #include "llvm/ADT/Optional.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/ADT/StringSet.h" 23 #include "llvm/Support/SourceMgr.h" 24 #include <cassert> 25 #include <string> 26 27 namespace clang { 28 namespace tidy { 29 class ClangTidyContext; 30 } // namespace tidy 31 namespace clangd { 32 33 struct ClangdDiagnosticOptions { 34 /// If true, Clangd uses an LSP extension to embed the fixes with the 35 /// diagnostics that are sent to the client. 36 bool EmbedFixesInDiagnostics = false; 37 38 /// If true, Clangd uses the relatedInformation field to include other 39 /// locations (in particular attached notes). 40 /// Otherwise, these are flattened into the diagnostic message. 41 bool EmitRelatedLocations = false; 42 43 /// If true, Clangd uses an LSP extension to send the diagnostic's 44 /// category to the client. The category typically describes the compilation 45 /// stage during which the issue was produced, e.g. "Semantic Issue" or "Parse 46 /// Issue". 47 bool SendDiagnosticCategory = false; 48 49 /// If true, Clangd will add a number of available fixes to the diagnostic's 50 /// message. 51 bool DisplayFixesCount = true; 52 }; 53 54 /// Contains basic information about a diagnostic. 55 struct DiagBase { 56 std::string Message; 57 // Intended to be used only in error messages. 58 // May be relative, absolute or even artificially constructed. 59 std::string File; 60 // Absolute path to containing file, if available. 61 llvm::Optional<std::string> AbsFile; 62 63 clangd::Range Range; 64 DiagnosticsEngine::Level Severity = DiagnosticsEngine::Note; 65 std::string Category; 66 // Since File is only descriptive, we store a separate flag to distinguish 67 // diags from the main file. 68 bool InsideMainFile = false; 69 unsigned ID; // e.g. member of clang::diag, or clang-tidy assigned ID. 70 }; 71 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DiagBase &D); 72 73 /// Represents a single fix-it that editor can apply to fix the error. 74 struct Fix { 75 /// Message for the fix-it. 76 std::string Message; 77 /// TextEdits from clang's fix-its. Must be non-empty. 78 llvm::SmallVector<TextEdit, 1> Edits; 79 }; 80 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Fix &F); 81 82 /// Represents a note for the diagnostic. Severity of notes can only be 'note' 83 /// or 'remark'. 84 struct Note : DiagBase {}; 85 86 /// A top-level diagnostic that may have Notes and Fixes. 87 struct Diag : DiagBase { 88 std::string Name; // if ID was recognized. 89 // The source of this diagnostic. 90 enum DiagSource { 91 Unknown, 92 Clang, 93 ClangTidy, 94 ClangdConfig, 95 } Source = Unknown; 96 /// Elaborate on the problem, usually pointing to a related piece of code. 97 std::vector<Note> Notes; 98 /// *Alternative* fixes for this diagnostic, one should be chosen. 99 std::vector<Fix> Fixes; 100 }; 101 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diag &D); 102 103 Diag toDiag(const llvm::SMDiagnostic &, Diag::DiagSource Source); 104 105 /// Conversion to LSP diagnostics. Formats the error message of each diagnostic 106 /// to include all its notes. Notes inside main file are also provided as 107 /// separate diagnostics with their corresponding fixits. Notes outside main 108 /// file do not have a corresponding LSP diagnostic, but can still be included 109 /// as part of their main diagnostic's message. 110 void toLSPDiags( 111 const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts, 112 llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn); 113 114 /// Convert from Fix to LSP CodeAction. 115 CodeAction toCodeAction(const Fix &D, const URIForFile &File); 116 117 /// Convert from clang diagnostic level to LSP severity. 118 int getSeverity(DiagnosticsEngine::Level L); 119 120 /// StoreDiags collects the diagnostics that can later be reported by 121 /// clangd. It groups all notes for a diagnostic into a single Diag 122 /// and filters out diagnostics that don't mention the main file (i.e. neither 123 /// the diag itself nor its notes are in the main file). 124 class StoreDiags : public DiagnosticConsumer { 125 public: 126 // The ClangTidyContext populates Source and Name for clang-tidy diagnostics. 127 std::vector<Diag> take(const clang::tidy::ClangTidyContext *Tidy = nullptr); 128 129 void BeginSourceFile(const LangOptions &Opts, 130 const Preprocessor *PP) override; 131 void EndSourceFile() override; 132 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, 133 const clang::Diagnostic &Info) override; 134 135 using DiagFixer = std::function<std::vector<Fix>(DiagnosticsEngine::Level, 136 const clang::Diagnostic &)>; 137 using LevelAdjuster = std::function<DiagnosticsEngine::Level( 138 DiagnosticsEngine::Level, const clang::Diagnostic &)>; 139 /// If set, possibly adds fixes for diagnostics using \p Fixer. contributeFixes(DiagFixer Fixer)140 void contributeFixes(DiagFixer Fixer) { this->Fixer = Fixer; } 141 /// If set, this allows the client of this class to adjust the level of 142 /// diagnostics, such as promoting warnings to errors, or ignoring 143 /// diagnostics. setLevelAdjuster(LevelAdjuster Adjuster)144 void setLevelAdjuster(LevelAdjuster Adjuster) { this->Adjuster = Adjuster; } 145 146 private: 147 void flushLastDiag(); 148 149 DiagFixer Fixer = nullptr; 150 LevelAdjuster Adjuster = nullptr; 151 std::vector<Diag> Output; 152 llvm::Optional<LangOptions> LangOpts; 153 llvm::Optional<Diag> LastDiag; 154 llvm::Optional<FullSourceLoc> LastDiagLoc; // Valid only when LastDiag is set. 155 bool LastDiagOriginallyError = false; // Valid only when LastDiag is set. 156 SourceManager *OrigSrcMgr = nullptr; 157 158 llvm::DenseSet<std::pair<unsigned, unsigned>> IncludedErrorLocations; 159 bool LastPrimaryDiagnosticWasSuppressed = false; 160 }; 161 162 } // namespace clangd 163 } // namespace clang 164 165 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H 166