150
|
1 //===--- Diagnostics.cpp -----------------------------------------*- 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 #include "Diagnostics.h"
|
|
10 #include "../clang-tidy/ClangTidyDiagnosticConsumer.h"
|
|
11 #include "Compiler.h"
|
|
12 #include "Protocol.h"
|
|
13 #include "SourceCode.h"
|
173
|
14 #include "support/Logger.h"
|
150
|
15 #include "clang/Basic/AllDiagnostics.h"
|
|
16 #include "clang/Basic/Diagnostic.h"
|
|
17 #include "clang/Basic/DiagnosticIDs.h"
|
|
18 #include "clang/Basic/FileManager.h"
|
|
19 #include "clang/Basic/SourceLocation.h"
|
|
20 #include "clang/Basic/SourceManager.h"
|
|
21 #include "clang/Lex/Lexer.h"
|
|
22 #include "clang/Lex/Token.h"
|
|
23 #include "llvm/ADT/ArrayRef.h"
|
|
24 #include "llvm/ADT/DenseSet.h"
|
|
25 #include "llvm/ADT/Optional.h"
|
|
26 #include "llvm/ADT/STLExtras.h"
|
|
27 #include "llvm/ADT/SmallString.h"
|
|
28 #include "llvm/ADT/StringRef.h"
|
|
29 #include "llvm/ADT/Twine.h"
|
|
30 #include "llvm/Support/Capacity.h"
|
|
31 #include "llvm/Support/Path.h"
|
|
32 #include "llvm/Support/ScopedPrinter.h"
|
|
33 #include "llvm/Support/Signals.h"
|
|
34 #include "llvm/Support/raw_ostream.h"
|
|
35 #include <algorithm>
|
|
36 #include <cstddef>
|
|
37
|
|
38 namespace clang {
|
|
39 namespace clangd {
|
|
40 namespace {
|
|
41
|
|
42 const char *getDiagnosticCode(unsigned ID) {
|
|
43 switch (ID) {
|
|
44 #define DIAG(ENUM, CLASS, DEFAULT_MAPPING, DESC, GROPU, SFINAE, NOWERROR, \
|
|
45 SHOWINSYSHEADER, CATEGORY) \
|
|
46 case clang::diag::ENUM: \
|
|
47 return #ENUM;
|
|
48 #include "clang/Basic/DiagnosticASTKinds.inc"
|
|
49 #include "clang/Basic/DiagnosticAnalysisKinds.inc"
|
|
50 #include "clang/Basic/DiagnosticCommentKinds.inc"
|
|
51 #include "clang/Basic/DiagnosticCommonKinds.inc"
|
|
52 #include "clang/Basic/DiagnosticDriverKinds.inc"
|
|
53 #include "clang/Basic/DiagnosticFrontendKinds.inc"
|
|
54 #include "clang/Basic/DiagnosticLexKinds.inc"
|
|
55 #include "clang/Basic/DiagnosticParseKinds.inc"
|
|
56 #include "clang/Basic/DiagnosticRefactoringKinds.inc"
|
|
57 #include "clang/Basic/DiagnosticSemaKinds.inc"
|
|
58 #include "clang/Basic/DiagnosticSerializationKinds.inc"
|
|
59 #undef DIAG
|
|
60 default:
|
|
61 return nullptr;
|
|
62 }
|
|
63 }
|
|
64
|
|
65 bool mentionsMainFile(const Diag &D) {
|
|
66 if (D.InsideMainFile)
|
|
67 return true;
|
|
68 // Fixes are always in the main file.
|
|
69 if (!D.Fixes.empty())
|
|
70 return true;
|
|
71 for (auto &N : D.Notes) {
|
|
72 if (N.InsideMainFile)
|
|
73 return true;
|
|
74 }
|
|
75 return false;
|
|
76 }
|
|
77
|
|
78 bool isBlacklisted(const Diag &D) {
|
|
79 // clang will always fail parsing MS ASM, we don't link in desc + asm parser.
|
|
80 if (D.ID == clang::diag::err_msasm_unable_to_create_target ||
|
|
81 D.ID == clang::diag::err_msasm_unsupported_arch)
|
|
82 return true;
|
|
83 return false;
|
|
84 }
|
|
85
|
|
86 // Checks whether a location is within a half-open range.
|
|
87 // Note that clang also uses closed source ranges, which this can't handle!
|
|
88 bool locationInRange(SourceLocation L, CharSourceRange R,
|
|
89 const SourceManager &M) {
|
|
90 assert(R.isCharRange());
|
|
91 if (!R.isValid() || M.getFileID(R.getBegin()) != M.getFileID(R.getEnd()) ||
|
|
92 M.getFileID(R.getBegin()) != M.getFileID(L))
|
|
93 return false;
|
|
94 return L != R.getEnd() && M.isPointWithin(L, R.getBegin(), R.getEnd());
|
|
95 }
|
|
96
|
|
97 // Clang diags have a location (shown as ^) and 0 or more ranges (~~~~).
|
|
98 // LSP needs a single range.
|
|
99 Range diagnosticRange(const clang::Diagnostic &D, const LangOptions &L) {
|
|
100 auto &M = D.getSourceManager();
|
|
101 auto Loc = M.getFileLoc(D.getLocation());
|
|
102 for (const auto &CR : D.getRanges()) {
|
|
103 auto R = Lexer::makeFileCharRange(CR, M, L);
|
|
104 if (locationInRange(Loc, R, M))
|
|
105 return halfOpenToRange(M, R);
|
|
106 }
|
|
107 // The range may be given as a fixit hint instead.
|
|
108 for (const auto &F : D.getFixItHints()) {
|
|
109 auto R = Lexer::makeFileCharRange(F.RemoveRange, M, L);
|
|
110 if (locationInRange(Loc, R, M))
|
|
111 return halfOpenToRange(M, R);
|
|
112 }
|
|
113 // If the token at the location is not a comment, we use the token.
|
|
114 // If we can't get the token at the location, fall back to using the location
|
|
115 auto R = CharSourceRange::getCharRange(Loc);
|
|
116 Token Tok;
|
|
117 if (!Lexer::getRawToken(Loc, Tok, M, L, true) && Tok.isNot(tok::comment)) {
|
|
118 R = CharSourceRange::getTokenRange(Tok.getLocation(), Tok.getEndLoc());
|
|
119 }
|
|
120 return halfOpenToRange(M, R);
|
|
121 }
|
|
122
|
|
123 // Returns whether the \p D is modified.
|
|
124 bool adjustDiagFromHeader(Diag &D, const clang::Diagnostic &Info,
|
|
125 const LangOptions &LangOpts) {
|
|
126 // We only report diagnostics with at least error severity from headers.
|
173
|
127 // Use default severity to avoid noise with -Werror.
|
|
128 if (!Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError(
|
|
129 Info.getID()))
|
150
|
130 return false;
|
|
131
|
|
132 const SourceManager &SM = Info.getSourceManager();
|
|
133 const SourceLocation &DiagLoc = SM.getExpansionLoc(Info.getLocation());
|
|
134 SourceLocation IncludeInMainFile;
|
|
135 auto GetIncludeLoc = [&SM](SourceLocation SLoc) {
|
|
136 return SM.getIncludeLoc(SM.getFileID(SLoc));
|
|
137 };
|
|
138 for (auto IncludeLocation = GetIncludeLoc(DiagLoc); IncludeLocation.isValid();
|
|
139 IncludeLocation = GetIncludeLoc(IncludeLocation)) {
|
|
140 if (clangd::isInsideMainFile(IncludeLocation, SM)) {
|
|
141 IncludeInMainFile = IncludeLocation;
|
|
142 break;
|
|
143 }
|
|
144 }
|
|
145 if (IncludeInMainFile.isInvalid())
|
|
146 return false;
|
|
147
|
|
148 // Update diag to point at include inside main file.
|
|
149 D.File = SM.getFileEntryForID(SM.getMainFileID())->getName().str();
|
|
150 D.Range.start = sourceLocToPosition(SM, IncludeInMainFile);
|
|
151 D.Range.end = sourceLocToPosition(
|
|
152 SM, Lexer::getLocForEndOfToken(IncludeInMainFile, 0, SM, LangOpts));
|
|
153 D.InsideMainFile = true;
|
|
154
|
|
155 // Add a note that will point to real diagnostic.
|
|
156 const auto *FE = SM.getFileEntryForID(SM.getFileID(DiagLoc));
|
|
157 D.Notes.emplace_back();
|
|
158 Note &N = D.Notes.back();
|
|
159 N.AbsFile = std::string(FE->tryGetRealPathName());
|
|
160 N.File = std::string(FE->getName());
|
|
161 N.Message = "error occurred here";
|
|
162 N.Range = diagnosticRange(Info, LangOpts);
|
|
163
|
|
164 // Update message to mention original file.
|
|
165 D.Message = llvm::Twine("in included file: ", D.Message).str();
|
|
166 return true;
|
|
167 }
|
|
168
|
|
169 bool isInsideMainFile(const clang::Diagnostic &D) {
|
|
170 if (!D.hasSourceManager())
|
|
171 return false;
|
|
172
|
|
173 return clangd::isInsideMainFile(D.getLocation(), D.getSourceManager());
|
|
174 }
|
|
175
|
|
176 bool isNote(DiagnosticsEngine::Level L) {
|
|
177 return L == DiagnosticsEngine::Note || L == DiagnosticsEngine::Remark;
|
|
178 }
|
|
179
|
|
180 llvm::StringRef diagLeveltoString(DiagnosticsEngine::Level Lvl) {
|
|
181 switch (Lvl) {
|
|
182 case DiagnosticsEngine::Ignored:
|
|
183 return "ignored";
|
|
184 case DiagnosticsEngine::Note:
|
|
185 return "note";
|
|
186 case DiagnosticsEngine::Remark:
|
|
187 return "remark";
|
|
188 case DiagnosticsEngine::Warning:
|
|
189 return "warning";
|
|
190 case DiagnosticsEngine::Error:
|
|
191 return "error";
|
|
192 case DiagnosticsEngine::Fatal:
|
|
193 return "fatal error";
|
|
194 }
|
|
195 llvm_unreachable("unhandled DiagnosticsEngine::Level");
|
|
196 }
|
|
197
|
|
198 /// Prints a single diagnostic in a clang-like manner, the output includes
|
|
199 /// location, severity and error message. An example of the output message is:
|
|
200 ///
|
|
201 /// main.cpp:12:23: error: undeclared identifier
|
|
202 ///
|
|
203 /// For main file we only print the basename and for all other files we print
|
|
204 /// the filename on a separate line to provide a slightly more readable output
|
|
205 /// in the editors:
|
|
206 ///
|
|
207 /// dir1/dir2/dir3/../../dir4/header.h:12:23
|
|
208 /// error: undeclared identifier
|
|
209 void printDiag(llvm::raw_string_ostream &OS, const DiagBase &D) {
|
|
210 if (D.InsideMainFile) {
|
|
211 // Paths to main files are often taken from compile_command.json, where they
|
|
212 // are typically absolute. To reduce noise we print only basename for them,
|
|
213 // it should not be confusing and saves space.
|
|
214 OS << llvm::sys::path::filename(D.File) << ":";
|
|
215 } else {
|
|
216 OS << D.File << ":";
|
|
217 }
|
|
218 // Note +1 to line and character. clangd::Range is zero-based, but when
|
|
219 // printing for users we want one-based indexes.
|
|
220 auto Pos = D.Range.start;
|
|
221 OS << (Pos.line + 1) << ":" << (Pos.character + 1) << ":";
|
|
222 // The non-main-file paths are often too long, putting them on a separate
|
|
223 // line improves readability.
|
|
224 if (D.InsideMainFile)
|
|
225 OS << " ";
|
|
226 else
|
|
227 OS << "\n";
|
|
228 OS << diagLeveltoString(D.Severity) << ": " << D.Message;
|
|
229 }
|
|
230
|
|
231 /// Capitalizes the first word in the diagnostic's message.
|
|
232 std::string capitalize(std::string Message) {
|
|
233 if (!Message.empty())
|
|
234 Message[0] = llvm::toUpper(Message[0]);
|
|
235 return Message;
|
|
236 }
|
|
237
|
|
238 /// Returns a message sent to LSP for the main diagnostic in \p D.
|
|
239 /// This message may include notes, if they're not emitted in some other way.
|
|
240 /// Example output:
|
|
241 ///
|
|
242 /// no matching function for call to 'foo'
|
|
243 ///
|
|
244 /// main.cpp:3:5: note: candidate function not viable: requires 2 arguments
|
|
245 ///
|
|
246 /// dir1/dir2/dir3/../../dir4/header.h:12:23
|
|
247 /// note: candidate function not viable: requires 3 arguments
|
|
248 std::string mainMessage(const Diag &D, const ClangdDiagnosticOptions &Opts) {
|
|
249 std::string Result;
|
|
250 llvm::raw_string_ostream OS(Result);
|
|
251 OS << D.Message;
|
|
252 if (Opts.DisplayFixesCount && !D.Fixes.empty())
|
|
253 OS << " (" << (D.Fixes.size() > 1 ? "fixes" : "fix") << " available)";
|
|
254 // If notes aren't emitted as structured info, add them to the message.
|
|
255 if (!Opts.EmitRelatedLocations)
|
|
256 for (auto &Note : D.Notes) {
|
|
257 OS << "\n\n";
|
|
258 printDiag(OS, Note);
|
|
259 }
|
|
260 OS.flush();
|
|
261 return capitalize(std::move(Result));
|
|
262 }
|
|
263
|
|
264 /// Returns a message sent to LSP for the note of the main diagnostic.
|
|
265 std::string noteMessage(const Diag &Main, const DiagBase &Note,
|
|
266 const ClangdDiagnosticOptions &Opts) {
|
|
267 std::string Result;
|
|
268 llvm::raw_string_ostream OS(Result);
|
|
269 OS << Note.Message;
|
|
270 // If the client doesn't support structured links between the note and the
|
|
271 // original diagnostic, then emit the main diagnostic to give context.
|
|
272 if (!Opts.EmitRelatedLocations) {
|
|
273 OS << "\n\n";
|
|
274 printDiag(OS, Main);
|
|
275 }
|
|
276 OS.flush();
|
|
277 return capitalize(std::move(Result));
|
|
278 }
|
|
279 } // namespace
|
|
280
|
|
281 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DiagBase &D) {
|
|
282 OS << "[";
|
|
283 if (!D.InsideMainFile)
|
|
284 OS << D.File << ":";
|
|
285 OS << D.Range.start << "-" << D.Range.end << "] ";
|
|
286
|
|
287 return OS << D.Message;
|
|
288 }
|
|
289
|
|
290 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Fix &F) {
|
|
291 OS << F.Message << " {";
|
|
292 const char *Sep = "";
|
|
293 for (const auto &Edit : F.Edits) {
|
|
294 OS << Sep << Edit;
|
|
295 Sep = ", ";
|
|
296 }
|
|
297 return OS << "}";
|
|
298 }
|
|
299
|
|
300 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diag &D) {
|
|
301 OS << static_cast<const DiagBase &>(D);
|
|
302 if (!D.Notes.empty()) {
|
|
303 OS << ", notes: {";
|
|
304 const char *Sep = "";
|
|
305 for (auto &Note : D.Notes) {
|
|
306 OS << Sep << Note;
|
|
307 Sep = ", ";
|
|
308 }
|
|
309 OS << "}";
|
|
310 }
|
|
311 if (!D.Fixes.empty()) {
|
|
312 OS << ", fixes: {";
|
|
313 const char *Sep = "";
|
|
314 for (auto &Fix : D.Fixes) {
|
|
315 OS << Sep << Fix;
|
|
316 Sep = ", ";
|
|
317 }
|
|
318 }
|
|
319 return OS;
|
|
320 }
|
|
321
|
|
322 CodeAction toCodeAction(const Fix &F, const URIForFile &File) {
|
|
323 CodeAction Action;
|
|
324 Action.title = F.Message;
|
|
325 Action.kind = std::string(CodeAction::QUICKFIX_KIND);
|
|
326 Action.edit.emplace();
|
|
327 Action.edit->changes.emplace();
|
|
328 (*Action.edit->changes)[File.uri()] = {F.Edits.begin(), F.Edits.end()};
|
|
329 return Action;
|
|
330 }
|
|
331
|
|
332 void toLSPDiags(
|
|
333 const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts,
|
|
334 llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn) {
|
|
335 clangd::Diagnostic Main;
|
|
336 Main.severity = getSeverity(D.Severity);
|
|
337
|
|
338 // Main diagnostic should always refer to a range inside main file. If a
|
|
339 // diagnostic made it so for, it means either itself or one of its notes is
|
|
340 // inside main file.
|
|
341 if (D.InsideMainFile) {
|
|
342 Main.range = D.Range;
|
|
343 } else {
|
|
344 auto It =
|
|
345 llvm::find_if(D.Notes, [](const Note &N) { return N.InsideMainFile; });
|
|
346 assert(It != D.Notes.end() &&
|
|
347 "neither the main diagnostic nor notes are inside main file");
|
|
348 Main.range = It->Range;
|
|
349 }
|
|
350
|
|
351 Main.code = D.Name;
|
|
352 switch (D.Source) {
|
|
353 case Diag::Clang:
|
|
354 Main.source = "clang";
|
|
355 break;
|
|
356 case Diag::ClangTidy:
|
|
357 Main.source = "clang-tidy";
|
|
358 break;
|
|
359 case Diag::Unknown:
|
|
360 break;
|
|
361 }
|
|
362 if (Opts.EmbedFixesInDiagnostics) {
|
|
363 Main.codeActions.emplace();
|
|
364 for (const auto &Fix : D.Fixes)
|
|
365 Main.codeActions->push_back(toCodeAction(Fix, File));
|
|
366 }
|
|
367 if (Opts.SendDiagnosticCategory && !D.Category.empty())
|
|
368 Main.category = D.Category;
|
|
369
|
|
370 Main.message = mainMessage(D, Opts);
|
|
371 if (Opts.EmitRelatedLocations) {
|
|
372 Main.relatedInformation.emplace();
|
|
373 for (auto &Note : D.Notes) {
|
|
374 if (!Note.AbsFile) {
|
|
375 vlog("Dropping note from unknown file: {0}", Note);
|
|
376 continue;
|
|
377 }
|
|
378 DiagnosticRelatedInformation RelInfo;
|
|
379 RelInfo.location.range = Note.Range;
|
|
380 RelInfo.location.uri =
|
|
381 URIForFile::canonicalize(*Note.AbsFile, File.file());
|
|
382 RelInfo.message = noteMessage(D, Note, Opts);
|
|
383 Main.relatedInformation->push_back(std::move(RelInfo));
|
|
384 }
|
|
385 }
|
|
386 OutFn(std::move(Main), D.Fixes);
|
|
387
|
|
388 // If we didn't emit the notes as relatedLocations, emit separate diagnostics
|
|
389 // so the user can find the locations easily.
|
|
390 if (!Opts.EmitRelatedLocations)
|
|
391 for (auto &Note : D.Notes) {
|
|
392 if (!Note.InsideMainFile)
|
|
393 continue;
|
|
394 clangd::Diagnostic Res;
|
|
395 Res.severity = getSeverity(Note.Severity);
|
|
396 Res.range = Note.Range;
|
|
397 Res.message = noteMessage(D, Note, Opts);
|
|
398 OutFn(std::move(Res), llvm::ArrayRef<Fix>());
|
|
399 }
|
|
400 }
|
|
401
|
|
402 int getSeverity(DiagnosticsEngine::Level L) {
|
|
403 switch (L) {
|
|
404 case DiagnosticsEngine::Remark:
|
|
405 return 4;
|
|
406 case DiagnosticsEngine::Note:
|
|
407 return 3;
|
|
408 case DiagnosticsEngine::Warning:
|
|
409 return 2;
|
|
410 case DiagnosticsEngine::Fatal:
|
|
411 case DiagnosticsEngine::Error:
|
|
412 return 1;
|
|
413 case DiagnosticsEngine::Ignored:
|
|
414 return 0;
|
|
415 }
|
|
416 llvm_unreachable("Unknown diagnostic level!");
|
|
417 }
|
|
418
|
|
419 std::vector<Diag> StoreDiags::take(const clang::tidy::ClangTidyContext *Tidy) {
|
|
420 // Do not forget to emit a pending diagnostic if there is one.
|
|
421 flushLastDiag();
|
|
422
|
|
423 // Fill in name/source now that we have all the context needed to map them.
|
|
424 for (auto &Diag : Output) {
|
|
425 if (const char *ClangDiag = getDiagnosticCode(Diag.ID)) {
|
|
426 // Warnings controlled by -Wfoo are better recognized by that name.
|
|
427 StringRef Warning = DiagnosticIDs::getWarningOptionForDiag(Diag.ID);
|
|
428 if (!Warning.empty()) {
|
|
429 Diag.Name = ("-W" + Warning).str();
|
|
430 } else {
|
|
431 StringRef Name(ClangDiag);
|
|
432 // Almost always an error, with a name like err_enum_class_reference.
|
|
433 // Drop the err_ prefix for brevity.
|
|
434 Name.consume_front("err_");
|
|
435 Diag.Name = std::string(Name);
|
|
436 }
|
|
437 Diag.Source = Diag::Clang;
|
|
438 continue;
|
|
439 }
|
|
440 if (Tidy != nullptr) {
|
|
441 std::string TidyDiag = Tidy->getCheckName(Diag.ID);
|
|
442 if (!TidyDiag.empty()) {
|
|
443 Diag.Name = std::move(TidyDiag);
|
|
444 Diag.Source = Diag::ClangTidy;
|
|
445 // clang-tidy bakes the name into diagnostic messages. Strip it out.
|
|
446 // It would be much nicer to make clang-tidy not do this.
|
|
447 auto CleanMessage = [&](std::string &Msg) {
|
|
448 StringRef Rest(Msg);
|
|
449 if (Rest.consume_back("]") && Rest.consume_back(Diag.Name) &&
|
|
450 Rest.consume_back(" ["))
|
|
451 Msg.resize(Rest.size());
|
|
452 };
|
|
453 CleanMessage(Diag.Message);
|
|
454 for (auto &Note : Diag.Notes)
|
|
455 CleanMessage(Note.Message);
|
|
456 for (auto &Fix : Diag.Fixes)
|
|
457 CleanMessage(Fix.Message);
|
|
458 continue;
|
|
459 }
|
|
460 }
|
|
461 }
|
|
462 // Deduplicate clang-tidy diagnostics -- some clang-tidy checks may emit
|
|
463 // duplicated messages due to various reasons (e.g. the check doesn't handle
|
|
464 // template instantiations well; clang-tidy alias checks).
|
|
465 std::set<std::pair<Range, std::string>> SeenDiags;
|
|
466 llvm::erase_if(Output, [&](const Diag& D) {
|
|
467 return !SeenDiags.emplace(D.Range, D.Message).second;
|
|
468 });
|
|
469 return std::move(Output);
|
|
470 }
|
|
471
|
|
472 void StoreDiags::BeginSourceFile(const LangOptions &Opts,
|
|
473 const Preprocessor *) {
|
|
474 LangOpts = Opts;
|
|
475 }
|
|
476
|
|
477 void StoreDiags::EndSourceFile() {
|
|
478 LangOpts = None;
|
|
479 }
|
|
480
|
|
481 /// Sanitizes a piece for presenting it in a synthesized fix message. Ensures
|
|
482 /// the result is not too large and does not contain newlines.
|
|
483 static void writeCodeToFixMessage(llvm::raw_ostream &OS, llvm::StringRef Code) {
|
|
484 constexpr unsigned MaxLen = 50;
|
|
485
|
|
486 // Only show the first line if there are many.
|
|
487 llvm::StringRef R = Code.split('\n').first;
|
|
488 // Shorten the message if it's too long.
|
|
489 R = R.take_front(MaxLen);
|
|
490
|
|
491 OS << R;
|
|
492 if (R.size() != Code.size())
|
|
493 OS << "…";
|
|
494 }
|
|
495
|
|
496 /// Fills \p D with all information, except the location-related bits.
|
|
497 /// Also note that ID and Name are not part of clangd::DiagBase and should be
|
|
498 /// set elsewhere.
|
|
499 static void fillNonLocationData(DiagnosticsEngine::Level DiagLevel,
|
|
500 const clang::Diagnostic &Info,
|
|
501 clangd::DiagBase &D) {
|
|
502 llvm::SmallString<64> Message;
|
|
503 Info.FormatDiagnostic(Message);
|
|
504
|
|
505 D.Message = std::string(Message.str());
|
|
506 D.Severity = DiagLevel;
|
|
507 D.Category = DiagnosticIDs::getCategoryNameFromID(
|
|
508 DiagnosticIDs::getCategoryNumberForDiag(Info.getID()))
|
|
509 .str();
|
|
510 }
|
|
511
|
|
512 void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
|
513 const clang::Diagnostic &Info) {
|
|
514 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
|
|
515
|
|
516 if (Info.getLocation().isInvalid()) {
|
|
517 // Handle diagnostics coming from command-line arguments. The source manager
|
|
518 // is *not* available at this point, so we cannot use it.
|
173
|
519 if (!Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError(
|
|
520 Info.getID())) {
|
150
|
521 IgnoreDiagnostics::log(DiagLevel, Info);
|
|
522 return; // non-errors add too much noise, do not show them.
|
|
523 }
|
|
524
|
|
525 flushLastDiag();
|
|
526
|
|
527 LastDiag = Diag();
|
|
528 LastDiag->ID = Info.getID();
|
|
529 fillNonLocationData(DiagLevel, Info, *LastDiag);
|
|
530 LastDiag->InsideMainFile = true;
|
|
531 // Put it at the start of the main file, for a lack of a better place.
|
|
532 LastDiag->Range.start = Position{0, 0};
|
|
533 LastDiag->Range.end = Position{0, 0};
|
|
534 return;
|
|
535 }
|
|
536
|
|
537 if (!LangOpts || !Info.hasSourceManager()) {
|
|
538 IgnoreDiagnostics::log(DiagLevel, Info);
|
|
539 return;
|
|
540 }
|
|
541
|
|
542 bool InsideMainFile = isInsideMainFile(Info);
|
|
543 SourceManager &SM = Info.getSourceManager();
|
|
544
|
|
545 auto FillDiagBase = [&](DiagBase &D) {
|
|
546 fillNonLocationData(DiagLevel, Info, D);
|
|
547
|
|
548 D.InsideMainFile = InsideMainFile;
|
|
549 D.Range = diagnosticRange(Info, *LangOpts);
|
|
550 D.File = std::string(SM.getFilename(Info.getLocation()));
|
|
551 D.AbsFile = getCanonicalPath(
|
|
552 SM.getFileEntryForID(SM.getFileID(Info.getLocation())), SM);
|
|
553 return D;
|
|
554 };
|
|
555
|
|
556 auto AddFix = [&](bool SyntheticMessage) -> bool {
|
|
557 assert(!Info.getFixItHints().empty() &&
|
|
558 "diagnostic does not have attached fix-its");
|
|
559 if (!InsideMainFile)
|
|
560 return false;
|
|
561
|
173
|
562 // Copy as we may modify the ranges.
|
|
563 auto FixIts = Info.getFixItHints().vec();
|
150
|
564 llvm::SmallVector<TextEdit, 1> Edits;
|
173
|
565 for (auto &FixIt : FixIts) {
|
|
566 // Allow fixits within a single macro-arg expansion to be applied.
|
|
567 // This can be incorrect if the argument is expanded multiple times in
|
|
568 // different contexts. Hopefully this is rare!
|
|
569 if (FixIt.RemoveRange.getBegin().isMacroID() &&
|
|
570 FixIt.RemoveRange.getEnd().isMacroID() &&
|
|
571 SM.getFileID(FixIt.RemoveRange.getBegin()) ==
|
|
572 SM.getFileID(FixIt.RemoveRange.getEnd())) {
|
|
573 FixIt.RemoveRange = CharSourceRange(
|
|
574 {SM.getTopMacroCallerLoc(FixIt.RemoveRange.getBegin()),
|
|
575 SM.getTopMacroCallerLoc(FixIt.RemoveRange.getEnd())},
|
|
576 FixIt.RemoveRange.isTokenRange());
|
|
577 }
|
|
578 // Otherwise, follow clang's behavior: no fixits in macros.
|
150
|
579 if (FixIt.RemoveRange.getBegin().isMacroID() ||
|
|
580 FixIt.RemoveRange.getEnd().isMacroID())
|
|
581 return false;
|
|
582 if (!isInsideMainFile(FixIt.RemoveRange.getBegin(), SM))
|
|
583 return false;
|
|
584 Edits.push_back(toTextEdit(FixIt, SM, *LangOpts));
|
|
585 }
|
|
586
|
|
587 llvm::SmallString<64> Message;
|
|
588 // If requested and possible, create a message like "change 'foo' to 'bar'".
|
173
|
589 if (SyntheticMessage && FixIts.size() == 1) {
|
|
590 const auto &FixIt = FixIts.front();
|
150
|
591 bool Invalid = false;
|
|
592 llvm::StringRef Remove =
|
|
593 Lexer::getSourceText(FixIt.RemoveRange, SM, *LangOpts, &Invalid);
|
|
594 llvm::StringRef Insert = FixIt.CodeToInsert;
|
|
595 if (!Invalid) {
|
|
596 llvm::raw_svector_ostream M(Message);
|
|
597 if (!Remove.empty() && !Insert.empty()) {
|
|
598 M << "change '";
|
|
599 writeCodeToFixMessage(M, Remove);
|
|
600 M << "' to '";
|
|
601 writeCodeToFixMessage(M, Insert);
|
|
602 M << "'";
|
|
603 } else if (!Remove.empty()) {
|
|
604 M << "remove '";
|
|
605 writeCodeToFixMessage(M, Remove);
|
|
606 M << "'";
|
|
607 } else if (!Insert.empty()) {
|
|
608 M << "insert '";
|
|
609 writeCodeToFixMessage(M, Insert);
|
|
610 M << "'";
|
|
611 }
|
|
612 // Don't allow source code to inject newlines into diagnostics.
|
|
613 std::replace(Message.begin(), Message.end(), '\n', ' ');
|
|
614 }
|
|
615 }
|
173
|
616 if (Message.empty()) // either !SyntheticMessage, or we failed to make one.
|
150
|
617 Info.FormatDiagnostic(Message);
|
|
618 LastDiag->Fixes.push_back(
|
|
619 Fix{std::string(Message.str()), std::move(Edits)});
|
|
620 return true;
|
|
621 };
|
|
622
|
|
623 if (!isNote(DiagLevel)) {
|
|
624 // Handle the new main diagnostic.
|
|
625 flushLastDiag();
|
|
626
|
|
627 if (Adjuster) {
|
|
628 DiagLevel = Adjuster(DiagLevel, Info);
|
|
629 if (DiagLevel == DiagnosticsEngine::Ignored) {
|
|
630 LastPrimaryDiagnosticWasSuppressed = true;
|
|
631 return;
|
|
632 }
|
|
633 }
|
|
634 LastPrimaryDiagnosticWasSuppressed = false;
|
|
635
|
|
636 LastDiag = Diag();
|
|
637 LastDiag->ID = Info.getID();
|
|
638 FillDiagBase(*LastDiag);
|
|
639 if (!InsideMainFile)
|
|
640 LastDiagWasAdjusted = adjustDiagFromHeader(*LastDiag, Info, *LangOpts);
|
|
641
|
|
642 if (!Info.getFixItHints().empty())
|
|
643 AddFix(true /* try to invent a message instead of repeating the diag */);
|
|
644 if (Fixer) {
|
|
645 auto ExtraFixes = Fixer(DiagLevel, Info);
|
|
646 LastDiag->Fixes.insert(LastDiag->Fixes.end(), ExtraFixes.begin(),
|
|
647 ExtraFixes.end());
|
|
648 }
|
|
649 } else {
|
|
650 // Handle a note to an existing diagnostic.
|
|
651
|
|
652 // If a diagnostic was suppressed due to the suppression filter,
|
|
653 // also suppress notes associated with it.
|
|
654 if (LastPrimaryDiagnosticWasSuppressed) {
|
|
655 return;
|
|
656 }
|
|
657
|
|
658 if (!LastDiag) {
|
|
659 assert(false && "Adding a note without main diagnostic");
|
|
660 IgnoreDiagnostics::log(DiagLevel, Info);
|
|
661 return;
|
|
662 }
|
|
663
|
|
664 if (!Info.getFixItHints().empty()) {
|
|
665 // A clang note with fix-it is not a separate diagnostic in clangd. We
|
|
666 // attach it as a Fix to the main diagnostic instead.
|
|
667 if (!AddFix(false /* use the note as the message */))
|
|
668 IgnoreDiagnostics::log(DiagLevel, Info);
|
|
669 } else {
|
|
670 // A clang note without fix-its corresponds to clangd::Note.
|
|
671 Note N;
|
|
672 FillDiagBase(N);
|
|
673
|
|
674 LastDiag->Notes.push_back(std::move(N));
|
|
675 }
|
|
676 }
|
|
677 }
|
|
678
|
|
679 void StoreDiags::flushLastDiag() {
|
|
680 if (!LastDiag)
|
|
681 return;
|
|
682 if (!isBlacklisted(*LastDiag) && mentionsMainFile(*LastDiag) &&
|
|
683 (!LastDiagWasAdjusted ||
|
|
684 // Only report the first diagnostic coming from each particular header.
|
|
685 IncludeLinesWithErrors.insert(LastDiag->Range.start.line).second)) {
|
|
686 Output.push_back(std::move(*LastDiag));
|
|
687 } else {
|
|
688 vlog("Dropped diagnostic: {0}: {1}", LastDiag->File, LastDiag->Message);
|
|
689 }
|
|
690 LastDiag.reset();
|
|
691 LastDiagWasAdjusted = false;
|
|
692 }
|
|
693
|
|
694 } // namespace clangd
|
|
695 } // namespace clang
|