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