clang-tools 23.0.0git
Diagnostics.cpp
Go to the documentation of this file.
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"
11#include "Compiler.h"
12#include "Protocol.h"
13#include "SourceCode.h"
14#include "support/Logger.h"
15#include "clang/Basic/AllDiagnostics.h" // IWYU pragma: keep
16#include "clang/Basic/Diagnostic.h"
17#include "clang/Basic/DiagnosticIDs.h"
18#include "clang/Basic/LLVM.h"
19#include "clang/Basic/SourceLocation.h"
20#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/TokenKinds.h"
22#include "clang/Lex/Lexer.h"
23#include "clang/Lex/Token.h"
24#include "llvm/ADT/ArrayRef.h"
25#include "llvm/ADT/DenseSet.h"
26#include "llvm/ADT/STLExtras.h"
27#include "llvm/ADT/STLFunctionalExtras.h"
28#include "llvm/ADT/ScopeExit.h"
29#include "llvm/ADT/SmallString.h"
30#include "llvm/ADT/SmallVector.h"
31#include "llvm/ADT/StringExtras.h"
32#include "llvm/ADT/StringRef.h"
33#include "llvm/ADT/StringSet.h"
34#include "llvm/ADT/Twine.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/FormatVariadic.h"
37#include "llvm/Support/Path.h"
38#include "llvm/Support/SourceMgr.h"
39#include "llvm/Support/raw_ostream.h"
40#include <algorithm>
41#include <cassert>
42#include <optional>
43#include <set>
44#include <string>
45#include <tuple>
46#include <utility>
47#include <vector>
48
49namespace clang {
50namespace clangd {
51namespace {
52
53const char *getDiagnosticCode(unsigned ID) {
54 switch (ID) {
55#define DIAG(ENUM, CLASS, DEFAULT_MAPPING, DESC, GROPU, SFINAE, NOWERROR, \
56 SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY, STABLE_ID, \
57 LEGACY_STABLE_IDS) \
58 case clang::diag::ENUM: \
59 return #ENUM;
60#include "clang/Basic/DiagnosticASTKinds.inc"
61#include "clang/Basic/DiagnosticAnalysisKinds.inc"
62#include "clang/Basic/DiagnosticCommentKinds.inc"
63#include "clang/Basic/DiagnosticCommonKinds.inc"
64#include "clang/Basic/DiagnosticDriverKinds.inc"
65#include "clang/Basic/DiagnosticFrontendKinds.inc"
66#include "clang/Basic/DiagnosticLexKinds.inc"
67#include "clang/Basic/DiagnosticParseKinds.inc"
68#include "clang/Basic/DiagnosticRefactoringKinds.inc"
69#include "clang/Basic/DiagnosticSemaKinds.inc"
70#include "clang/Basic/DiagnosticSerializationKinds.inc"
71#undef DIAG
72 default:
73 return nullptr;
74 }
75}
76
77bool mentionsMainFile(const Diag &D) {
78 if (D.InsideMainFile)
79 return true;
80 // Fixes are always in the main file.
81 if (!D.Fixes.empty())
82 return true;
83 for (auto &N : D.Notes) {
84 if (N.InsideMainFile)
85 return true;
86 }
87 return false;
88}
89
90bool isExcluded(unsigned DiagID) {
91 // clang will always fail parsing MS ASM, we don't link in desc + asm parser.
92 if (DiagID == clang::diag::err_msasm_unable_to_create_target ||
93 DiagID == clang::diag::err_msasm_unsupported_arch)
94 return true;
95 return false;
96}
97
98// Checks whether a location is within a half-open range.
99// Note that clang also uses closed source ranges, which this can't handle!
100bool locationInRange(SourceLocation L, CharSourceRange R,
101 const SourceManager &M) {
102 assert(R.isCharRange());
103 if (!R.isValid() || M.getFileID(R.getBegin()) != M.getFileID(R.getEnd()) ||
104 M.getFileID(R.getBegin()) != M.getFileID(L))
105 return false;
106 return L != R.getEnd() && M.isPointWithin(L, R.getBegin(), R.getEnd());
107}
108
109// Clang diags have a location (shown as ^) and 0 or more ranges (~~~~).
110// LSP needs a single range.
111std::optional<Range> diagnosticRange(const clang::Diagnostic &D,
112 const LangOptions &L) {
113 auto &M = D.getSourceManager();
114 auto PatchedRange = [&M](CharSourceRange &R) {
115 R.setBegin(translatePreamblePatchLocation(R.getBegin(), M));
116 R.setEnd(translatePreamblePatchLocation(R.getEnd(), M));
117 return R;
118 };
119 auto Loc = M.getFileLoc(D.getLocation());
120 for (const auto &CR : D.getRanges()) {
121 auto R = Lexer::makeFileCharRange(CR, M, L);
122 if (locationInRange(Loc, R, M))
123 return halfOpenToRange(M, PatchedRange(R));
124 }
125 // The range may be given as a fixit hint instead.
126 for (const auto &F : D.getFixItHints()) {
127 auto R = Lexer::makeFileCharRange(F.RemoveRange, M, L);
128 if (locationInRange(Loc, R, M))
129 return halfOpenToRange(M, PatchedRange(R));
130 }
131 // Source locations from stale preambles might become OOB.
132 // FIXME: These diagnostics might point to wrong locations even when they're
133 // not OOB.
134 auto [FID, Offset] = M.getDecomposedLoc(Loc);
135 if (Offset > M.getBufferData(FID).size())
136 return std::nullopt;
137 // If the token at the location is not a comment, we use the token.
138 // If we can't get the token at the location, fall back to using the location
139 auto R = CharSourceRange::getCharRange(Loc);
140 Token Tok;
141 if (!Lexer::getRawToken(Loc, Tok, M, L, true) && Tok.isNot(tok::comment))
142 R = CharSourceRange::getTokenRange(Tok.getLocation(), Tok.getEndLoc());
143 return halfOpenToRange(M, PatchedRange(R));
144}
145
146// Try to find a location in the main-file to report the diagnostic D.
147// Returns a description like "in included file", or nullptr on failure.
148const char *getMainFileRange(const Diag &D, const SourceManager &SM,
149 SourceLocation DiagLoc, Range &R) {
150 // Look for a note in the main file indicating template instantiation.
151 for (const auto &N : D.Notes) {
152 if (N.InsideMainFile) {
153 switch (N.ID) {
154 case diag::note_template_class_instantiation_was_here:
155 case diag::note_template_class_explicit_specialization_was_here:
156 case diag::note_template_class_instantiation_here:
157 case diag::note_template_member_class_here:
158 case diag::note_template_member_function_here:
159 case diag::note_function_template_spec_here:
160 case diag::note_template_static_data_member_def_here:
161 case diag::note_template_variable_def_here:
162 case diag::note_template_enum_def_here:
163 case diag::note_template_nsdmi_here:
164 case diag::note_template_type_alias_instantiation_here:
165 case diag::note_template_exception_spec_instantiation_here:
166 case diag::note_template_requirement_instantiation_here:
167 case diag::note_evaluating_exception_spec_here:
168 case diag::note_default_arg_instantiation_here:
169 case diag::note_default_function_arg_instantiation_here:
170 case diag::note_explicit_template_arg_substitution_here:
171 case diag::note_function_template_deduction_instantiation_here:
172 case diag::note_deduced_template_arg_substitution_here:
173 case diag::note_prior_template_arg_substitution:
174 case diag::note_template_default_arg_checking:
175 case diag::note_concept_specialization_here:
176 case diag::note_nested_requirement_here:
177 case diag::note_checking_constraints_for_template_id_here:
178 case diag::note_checking_constraints_for_var_spec_id_here:
179 case diag::note_checking_constraints_for_class_spec_id_here:
180 case diag::note_checking_constraints_for_function_here:
181 case diag::note_constraint_substitution_here:
182 case diag::note_constraint_normalization_here:
183 case diag::note_parameter_mapping_substitution_here:
184 R = N.Range;
185 return "in template";
186 default:
187 break;
188 }
189 }
190 }
191 // Look for where the file with the error was #included.
192 auto GetIncludeLoc = [&SM](SourceLocation SLoc) {
193 return SM.getIncludeLoc(SM.getFileID(SLoc));
194 };
195 for (auto IncludeLocation = GetIncludeLoc(SM.getExpansionLoc(DiagLoc));
196 IncludeLocation.isValid();
197 IncludeLocation = GetIncludeLoc(IncludeLocation)) {
198 if (clangd::isInsideMainFile(IncludeLocation, SM)) {
199 R.start = sourceLocToPosition(SM, IncludeLocation);
200 R.end = sourceLocToPosition(
201 SM,
202 Lexer::getLocForEndOfToken(IncludeLocation, 0, SM, LangOptions()));
203 return "in included file";
204 }
205 }
206 return nullptr;
207}
208
209// Place the diagnostic the main file, rather than the header, if possible:
210// - for errors in included files, use the #include location
211// - for errors in template instantiation, use the instantiation location
212// In both cases, add the original header location as a note.
213bool tryMoveToMainFile(Diag &D, FullSourceLoc DiagLoc) {
214 const SourceManager &SM = DiagLoc.getManager();
215 DiagLoc = DiagLoc.getExpansionLoc();
216 Range R;
217 const char *Prefix = getMainFileRange(D, SM, DiagLoc, R);
218 if (!Prefix)
219 return false;
220
221 // Add a note that will point to real diagnostic.
222 auto FE = *SM.getFileEntryRefForID(SM.getFileID(DiagLoc));
223 D.Notes.emplace(D.Notes.begin());
224 Note &N = D.Notes.front();
225 N.AbsFile = std::string(FE.getFileEntry().tryGetRealPathName());
226 N.File = std::string(FE.getName());
227 N.Message = "error occurred here";
228 N.Range = D.Range;
229
230 // Update diag to point at include inside main file.
231 D.File = SM.getFileEntryRefForID(SM.getMainFileID())->getName().str();
232 D.Range = std::move(R);
233 D.InsideMainFile = true;
234 // Update message to mention original file.
235 D.Message = llvm::formatv("{0}: {1}", Prefix, D.Message);
236 return true;
237}
238
239bool isNote(DiagnosticsEngine::Level L) {
240 return L == DiagnosticsEngine::Note || L == DiagnosticsEngine::Remark;
241}
242
243llvm::StringRef diagLeveltoString(DiagnosticsEngine::Level Lvl) {
244 switch (Lvl) {
245 case DiagnosticsEngine::Ignored:
246 return "ignored";
247 case DiagnosticsEngine::Note:
248 return "note";
249 case DiagnosticsEngine::Remark:
250 return "remark";
251 case DiagnosticsEngine::Warning:
252 return "warning";
253 case DiagnosticsEngine::Error:
254 return "error";
255 case DiagnosticsEngine::Fatal:
256 return "fatal error";
257 }
258 llvm_unreachable("unhandled DiagnosticsEngine::Level");
259}
260
261/// Prints a single diagnostic in a clang-like manner, the output includes
262/// location, severity and error message. An example of the output message is:
263///
264/// main.cpp:12:23: error: undeclared identifier
265///
266/// For main file we only print the basename and for all other files we print
267/// the filename on a separate line to provide a slightly more readable output
268/// in the editors:
269///
270/// dir1/dir2/dir3/../../dir4/header.h:12:23
271/// error: undeclared identifier
272void printDiag(llvm::raw_string_ostream &OS, const DiagBase &D) {
273 if (D.InsideMainFile) {
274 // Paths to main files are often taken from compile_command.json, where they
275 // are typically absolute. To reduce noise we print only basename for them,
276 // it should not be confusing and saves space.
277 OS << llvm::sys::path::filename(D.File) << ":";
278 } else {
279 OS << D.File << ":";
280 }
281 // Note +1 to line and character. clangd::Range is zero-based, but when
282 // printing for users we want one-based indexes.
283 auto Pos = D.Range.start;
284 OS << (Pos.line + 1) << ":" << (Pos.character + 1) << ":";
285 // The non-main-file paths are often too long, putting them on a separate
286 // line improves readability.
287 if (D.InsideMainFile)
288 OS << " ";
289 else
290 OS << "\n";
291 OS << diagLeveltoString(D.Severity) << ": " << D.Message;
292}
293
294/// Capitalizes the first word in the diagnostic's message.
295std::string capitalize(std::string Message) {
296 if (!Message.empty())
297 Message[0] = llvm::toUpper(Message[0]);
298 return Message;
299}
300
301/// Returns a message sent to LSP for the main diagnostic in \p D.
302/// This message may include notes, if they're not emitted in some other way.
303/// Example output:
304///
305/// no matching function for call to 'foo'
306///
307/// main.cpp:3:5: note: candidate function not viable: requires 2 arguments
308///
309/// dir1/dir2/dir3/../../dir4/header.h:12:23
310/// note: candidate function not viable: requires 3 arguments
311std::string mainMessage(const Diag &D, const ClangdDiagnosticOptions &Opts) {
312 std::string Result;
313 llvm::raw_string_ostream OS(Result);
314 OS << D.Message;
315 if (Opts.DisplayFixesCount && !D.Fixes.empty())
316 OS << " (" << (D.Fixes.size() > 1 ? "fixes" : "fix") << " available)";
317 // If notes aren't emitted as structured info, add them to the message.
318 if (!Opts.EmitRelatedLocations)
319 for (auto &Note : D.Notes) {
320 OS << "\n\n";
321 printDiag(OS, Note);
322 }
323 return capitalize(std::move(Result));
324}
325
326/// Returns a message sent to LSP for the note of the main diagnostic.
327std::string noteMessage(const Diag &Main, const DiagBase &Note,
328 const ClangdDiagnosticOptions &Opts) {
329 std::string Result;
330 llvm::raw_string_ostream OS(Result);
331 OS << Note.Message;
332 // If the client doesn't support structured links between the note and the
333 // original diagnostic, then emit the main diagnostic to give context.
334 if (!Opts.EmitRelatedLocations) {
335 OS << "\n\n";
336 printDiag(OS, Main);
337 }
338 return capitalize(std::move(Result));
339}
340
341void setTags(clangd::Diag &D) {
342 static const auto *DeprecatedDiags = new llvm::DenseSet<unsigned>{
343 diag::warn_access_decl_deprecated,
344 diag::warn_atl_uuid_deprecated,
345 diag::warn_deprecated,
346 diag::warn_deprecated_altivec_src_compat,
347 diag::warn_deprecated_comma_subscript,
348 diag::warn_deprecated_copy,
349 diag::warn_deprecated_copy_with_dtor,
350 diag::warn_deprecated_copy_with_user_provided_copy,
351 diag::warn_deprecated_copy_with_user_provided_dtor,
352 diag::warn_deprecated_def,
353 diag::warn_deprecated_increment_decrement_volatile,
354 diag::warn_deprecated_message,
355 diag::warn_deprecated_redundant_constexpr_static_def,
356 diag::warn_deprecated_register,
357 diag::warn_deprecated_simple_assign_volatile,
358 diag::warn_deprecated_string_literal_conversion,
359 diag::warn_deprecated_this_capture,
360 diag::warn_deprecated_volatile_param,
361 diag::warn_deprecated_volatile_return,
362 diag::warn_deprecated_volatile_structured_binding,
363 diag::warn_opencl_attr_deprecated_ignored,
364 diag::warn_property_method_deprecated,
365 diag::warn_vector_mode_deprecated,
366 };
367 static const auto *UnusedDiags = new llvm::DenseSet<unsigned>{
368 diag::warn_opencl_attr_deprecated_ignored,
369 diag::warn_pragma_attribute_unused,
370 diag::warn_unused_but_set_parameter,
371 diag::warn_unused_but_set_variable,
372 diag::warn_unused_comparison,
373 diag::warn_unused_const_variable,
374 diag::warn_unused_exception_param,
375 diag::warn_unused_function,
376 diag::warn_unused_label,
377 diag::warn_unused_lambda_capture,
378 diag::warn_unused_local_typedef,
379 diag::warn_unused_member_function,
380 diag::warn_unused_parameter,
381 diag::warn_unused_private_field,
382 diag::warn_unused_property_backing_ivar,
383 diag::warn_unused_template,
384 diag::warn_unused_variable,
385 };
386 if (DeprecatedDiags->contains(D.ID)) {
387 D.Tags.push_back(DiagnosticTag::Deprecated);
388 } else if (UnusedDiags->contains(D.ID)) {
389 D.Tags.push_back(DiagnosticTag::Unnecessary);
390 }
391 if (D.Source == Diag::ClangTidy) {
392 if (llvm::StringRef(D.Name).starts_with("misc-unused-"))
393 D.Tags.push_back(DiagnosticTag::Unnecessary);
394 if (llvm::StringRef(D.Name).starts_with("modernize-"))
395 D.Tags.push_back(DiagnosticTag::Deprecated);
396 }
397}
398} // namespace
399
400llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DiagBase &D) {
401 OS << "[";
402 if (!D.InsideMainFile)
403 OS << D.File << ":";
404 OS << D.Range.start << "-" << D.Range.end << "] ";
405
406 return OS << D.Message;
407}
408
409llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Fix &F) {
410 OS << F.Message << " {";
411 const char *Sep = "";
412 for (const auto &Edit : F.Edits) {
413 OS << Sep << Edit;
414 Sep = ", ";
415 }
416 return OS << "}";
417}
418
419llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diag &D) {
420 OS << static_cast<const DiagBase &>(D);
421 if (!D.Notes.empty()) {
422 OS << ", notes: {";
423 const char *Sep = "";
424 for (auto &Note : D.Notes) {
425 OS << Sep << Note;
426 Sep = ", ";
427 }
428 OS << "}";
429 }
430 if (!D.Fixes.empty()) {
431 OS << ", fixes: {";
432 const char *Sep = "";
433 for (auto &Fix : D.Fixes) {
434 OS << Sep << Fix;
435 Sep = ", ";
436 }
437 OS << "}";
438 }
439 return OS;
440}
441
442Diag toDiag(const llvm::SMDiagnostic &D, Diag::DiagSource Source) {
443 Diag Result;
444 Result.Message = D.getMessage().str();
445 switch (D.getKind()) {
446 case llvm::SourceMgr::DK_Error:
447 Result.Severity = DiagnosticsEngine::Error;
448 break;
449 case llvm::SourceMgr::DK_Warning:
450 Result.Severity = DiagnosticsEngine::Warning;
451 break;
452 default:
453 break;
454 }
455 Result.Source = Source;
456 Result.AbsFile = D.getFilename().str();
457 Result.InsideMainFile = D.getSourceMgr()->FindBufferContainingLoc(
458 D.getLoc()) == D.getSourceMgr()->getMainFileID();
459 if (D.getRanges().empty())
460 Result.Range = {{D.getLineNo() - 1, D.getColumnNo()},
461 {D.getLineNo() - 1, D.getColumnNo()}};
462 else
463 Result.Range = {{D.getLineNo() - 1, (int)D.getRanges().front().first},
464 {D.getLineNo() - 1, (int)D.getRanges().front().second}};
465 return Result;
466}
467
469 const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts,
470 llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn) {
472 Main.severity = getSeverity(D.Severity);
473 // We downgrade severity for certain noisy warnings, like deprecated
474 // declartions. These already have visible decorations inside the editor and
475 // most users find the extra clutter in the UI (gutter, minimap, diagnostics
476 // views) overwhelming.
477 if (D.Severity == DiagnosticsEngine::Warning) {
478 if (llvm::is_contained(D.Tags, DiagnosticTag::Deprecated))
479 Main.severity = getSeverity(DiagnosticsEngine::Remark);
480 }
481
482 // Main diagnostic should always refer to a range inside main file. If a
483 // diagnostic made it so for, it means either itself or one of its notes is
484 // inside main file. It's also possible that there's a fix in the main file,
485 // but we preserve fixes iff primary diagnostic is in the main file.
486 if (D.InsideMainFile) {
487 Main.range = D.Range;
488 } else {
489 auto It =
490 llvm::find_if(D.Notes, [](const Note &N) { return N.InsideMainFile; });
491 assert(It != D.Notes.end() &&
492 "neither the main diagnostic nor notes are inside main file");
493 Main.range = It->Range;
494 }
495
496 Main.code = D.Name;
497 if (auto URI = getDiagnosticDocURI(D.Source, D.ID, D.Name)) {
498 Main.codeDescription.emplace();
499 Main.codeDescription->href = std::move(*URI);
500 }
501 switch (D.Source) {
502 case Diag::Clang:
503 Main.source = "clang";
504 break;
505 case Diag::ClangTidy:
506 Main.source = "clang-tidy";
507 break;
508 case Diag::Clangd:
509 Main.source = "clangd";
510 break;
512 Main.source = "clangd-config";
513 break;
514 case Diag::Unknown:
515 break;
516 }
517 if (Opts.SendDiagnosticCategory && !D.Category.empty())
518 Main.category = D.Category;
519
520 Main.message = mainMessage(D, Opts);
521 if (Opts.EmitRelatedLocations) {
522 Main.relatedInformation.emplace();
523 for (auto &Note : D.Notes) {
524 if (!Note.AbsFile) {
525 vlog("Dropping note from unknown file: {0}", Note);
526 continue;
527 }
529 RelInfo.location.range = Note.Range;
530 RelInfo.location.uri =
532 RelInfo.message = noteMessage(D, Note, Opts);
533 Main.relatedInformation->push_back(std::move(RelInfo));
534 }
535 }
536 Main.tags = D.Tags;
537 // FIXME: Get rid of the copies here by taking in a mutable clangd::Diag.
538 for (auto &Entry : D.OpaqueData)
539 Main.data.insert({Entry.first, Entry.second});
540 OutFn(std::move(Main), D.Fixes);
541
542 // If we didn't emit the notes as relatedLocations, emit separate diagnostics
543 // so the user can find the locations easily.
544 if (!Opts.EmitRelatedLocations)
545 for (auto &Note : D.Notes) {
546 if (!Note.InsideMainFile)
547 continue;
550 Res.range = Note.Range;
551 Res.message = noteMessage(D, Note, Opts);
552 OutFn(std::move(Res), llvm::ArrayRef<Fix>());
553 }
554}
555
556int getSeverity(DiagnosticsEngine::Level L) {
557 switch (L) {
558 case DiagnosticsEngine::Remark:
559 return 4;
560 case DiagnosticsEngine::Note:
561 return 3;
562 case DiagnosticsEngine::Warning:
563 return 2;
564 case DiagnosticsEngine::Fatal:
565 case DiagnosticsEngine::Error:
566 return 1;
567 case DiagnosticsEngine::Ignored:
568 return 0;
569 }
570 llvm_unreachable("Unknown diagnostic level!");
571}
572
573std::vector<Diag> StoreDiags::take(const clang::tidy::ClangTidyContext *Tidy) {
574 // Do not forget to emit a pending diagnostic if there is one.
575 flushLastDiag();
576
577 // Fill in name/source now that we have all the context needed to map them.
578 for (auto &Diag : Output) {
579 if (const char *ClangDiag = getDiagnosticCode(Diag.ID)) {
580 // Warnings controlled by -Wfoo are better recognized by that name.
581 StringRef Warning = [&] {
582 if (OrigSrcMgr) {
583 return OrigSrcMgr->getDiagnostics()
584 .getDiagnosticIDs()
585 ->getWarningOptionForDiag(Diag.ID);
586 }
587 if (!DiagnosticIDs::IsCustomDiag(Diag.ID))
588 return DiagnosticIDs{}.getWarningOptionForDiag(Diag.ID);
589 return StringRef{};
590 }();
591
592 if (!Warning.empty()) {
593 Diag.Name = ("-W" + Warning).str();
594 } else {
595 StringRef Name(ClangDiag);
596 // Almost always an error, with a name like err_enum_class_reference.
597 // Drop the err_ prefix for brevity.
598 Name.consume_front("err_");
599 Diag.Name = std::string(Name);
600 }
602 } else if (Tidy != nullptr) {
603 std::string TidyDiag = Tidy->getCheckName(Diag.ID);
604 if (!TidyDiag.empty()) {
605 Diag.Name = std::move(TidyDiag);
607 // clang-tidy bakes the name into diagnostic messages. Strip it out.
608 // It would be much nicer to make clang-tidy not do this.
609 auto CleanMessage = [&](std::string &Msg) {
610 StringRef Rest(Msg);
611 if (Rest.consume_back("]") && Rest.consume_back(Diag.Name) &&
612 Rest.consume_back(" ["))
613 Msg.resize(Rest.size());
614 };
615 CleanMessage(Diag.Message);
616 for (auto &Note : Diag.Notes)
617 CleanMessage(Note.Message);
618 for (auto &Fix : Diag.Fixes)
619 CleanMessage(Fix.Message);
620 }
621 }
622 setTags(Diag);
623 }
624 // Deduplicate clang-tidy diagnostics -- some clang-tidy checks may emit
625 // duplicated messages due to various reasons (e.g. the check doesn't handle
626 // template instantiations well; clang-tidy alias checks).
627 std::set<std::pair<Range, std::string>> SeenDiags;
628 llvm::erase_if(Output, [&](const Diag &D) {
629 return !SeenDiags.emplace(D.Range, D.Message).second;
630 });
631 return std::move(Output);
632}
633
634void StoreDiags::BeginSourceFile(const LangOptions &Opts,
635 const Preprocessor *PP) {
636 LangOpts = Opts;
637 if (PP) {
638 OrigSrcMgr = &PP->getSourceManager();
639 }
640}
641
643 flushLastDiag();
644 LangOpts = std::nullopt;
645 OrigSrcMgr = nullptr;
646}
647
648/// Sanitizes a piece for presenting it in a synthesized fix message. Ensures
649/// the result is not too large and does not contain newlines.
650static void writeCodeToFixMessage(llvm::raw_ostream &OS, llvm::StringRef Code) {
651 constexpr unsigned MaxLen = 50;
652 if (Code == "\n") {
653 OS << "\\n";
654 return;
655 }
656 // Only show the first line if there are many.
657 llvm::StringRef R = Code.split('\n').first;
658 // Shorten the message if it's too long.
659 R = R.take_front(MaxLen);
660
661 OS << R;
662 if (R.size() != Code.size())
663 OS << "…";
664}
665
666/// Fills \p D with all information, except the location-related bits.
667/// Also note that ID and Name are not part of clangd::DiagBase and should be
668/// set elsewhere.
669static void fillNonLocationData(DiagnosticsEngine::Level DiagLevel,
670 const clang::Diagnostic &Info,
671 clangd::DiagBase &D) {
672 llvm::SmallString<64> Message;
673 Info.FormatDiagnostic(Message);
674
675 D.Message = std::string(Message);
676 D.Severity = DiagLevel;
677 D.Category = DiagnosticIDs::getCategoryNameFromID(
678 DiagnosticIDs::getCategoryNumberForDiag(Info.getID()))
679 .str();
680}
681
682void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
683 const clang::Diagnostic &Info) {
684 // If the diagnostic was generated for a different SourceManager, skip it.
685 // This happens when a module is imported and needs to be implicitly built.
686 // The compilation of that module will use the same StoreDiags, but different
687 // SourceManager.
688 if (OrigSrcMgr && Info.hasSourceManager() &&
689 OrigSrcMgr != &Info.getSourceManager()) {
690 IgnoreDiagnostics::log(DiagLevel, Info);
691 return;
692 }
693
694 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
695 bool OriginallyError =
696 Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError(
697 Info.getID());
698
699 if (Info.getLocation().isInvalid()) {
700 // Handle diagnostics coming from command-line arguments. The source manager
701 // is *not* available at this point, so we cannot use it.
702 if (!OriginallyError) {
703 IgnoreDiagnostics::log(DiagLevel, Info);
704 return; // non-errors add too much noise, do not show them.
705 }
706
707 flushLastDiag();
708
709 LastDiag = Diag();
710 LastDiagLoc.reset();
711 LastDiagOriginallyError = OriginallyError;
712 LastDiag->ID = Info.getID();
713 fillNonLocationData(DiagLevel, Info, *LastDiag);
714 LastDiag->InsideMainFile = true;
715 // Put it at the start of the main file, for a lack of a better place.
716 LastDiag->Range.start = Position{0, 0};
717 LastDiag->Range.end = Position{0, 0};
718 return;
719 }
720
721 if (!LangOpts || !Info.hasSourceManager()) {
722 IgnoreDiagnostics::log(DiagLevel, Info);
723 return;
724 }
725
726 SourceManager &SM = Info.getSourceManager();
727
728 auto FillDiagBase = [&](DiagBase &D) {
729 fillNonLocationData(DiagLevel, Info, D);
730
731 SourceLocation PatchLoc =
732 translatePreamblePatchLocation(Info.getLocation(), SM);
733 D.InsideMainFile = isInsideMainFile(PatchLoc, SM);
734 if (auto DRange = diagnosticRange(Info, *LangOpts))
735 D.Range = *DRange;
736 else
737 D.Severity = DiagnosticsEngine::Ignored;
738 auto FID = SM.getFileID(Info.getLocation());
739 if (const auto FE = SM.getFileEntryRefForID(FID)) {
740 D.File = FE->getName().str();
741 D.AbsFile = getCanonicalPath(*FE, SM.getFileManager());
742 }
743 D.ID = Info.getID();
744 return D;
745 };
746
747 auto AddFix = [&](bool SyntheticMessage) -> bool {
748 assert(!Info.getFixItHints().empty() &&
749 "diagnostic does not have attached fix-its");
750 // No point in generating fixes, if the diagnostic is for a different file.
751 if (!LastDiag->InsideMainFile)
752 return false;
753 // Copy as we may modify the ranges.
754 auto FixIts = Info.getFixItHints().vec();
755 llvm::SmallVector<TextEdit, 1> Edits;
756 for (auto &FixIt : FixIts) {
757 // Allow fixits within a single macro-arg expansion to be applied.
758 // This can be incorrect if the argument is expanded multiple times in
759 // different contexts. Hopefully this is rare!
760 if (FixIt.RemoveRange.getBegin().isMacroID() &&
761 FixIt.RemoveRange.getEnd().isMacroID() &&
762 SM.getFileID(FixIt.RemoveRange.getBegin()) ==
763 SM.getFileID(FixIt.RemoveRange.getEnd())) {
764 FixIt.RemoveRange = CharSourceRange(
765 {SM.getTopMacroCallerLoc(FixIt.RemoveRange.getBegin()),
766 SM.getTopMacroCallerLoc(FixIt.RemoveRange.getEnd())},
767 FixIt.RemoveRange.isTokenRange());
768 }
769 // Otherwise, follow clang's behavior: no fixits in macros.
770 if (FixIt.RemoveRange.getBegin().isMacroID() ||
771 FixIt.RemoveRange.getEnd().isMacroID())
772 return false;
773 if (!isInsideMainFile(FixIt.RemoveRange.getBegin(), SM))
774 return false;
775 Edits.push_back(toTextEdit(FixIt, SM, *LangOpts));
776 }
777
778 llvm::SmallString<64> Message;
779 // If requested and possible, create a message like "change 'foo' to 'bar'".
780 if (SyntheticMessage && FixIts.size() == 1) {
781 const auto &FixIt = FixIts.front();
782 bool Invalid = false;
783 llvm::StringRef Remove =
784 Lexer::getSourceText(FixIt.RemoveRange, SM, *LangOpts, &Invalid);
785 llvm::StringRef Insert = FixIt.CodeToInsert;
786 if (!Invalid) {
787 llvm::raw_svector_ostream M(Message);
788 if (!Remove.empty() && !Insert.empty()) {
789 M << "change '";
790 writeCodeToFixMessage(M, Remove);
791 M << "' to '";
792 writeCodeToFixMessage(M, Insert);
793 M << "'";
794 } else if (!Remove.empty()) {
795 M << "remove '";
796 writeCodeToFixMessage(M, Remove);
797 M << "'";
798 } else if (!Insert.empty()) {
799 M << "insert '";
800 writeCodeToFixMessage(M, Insert);
801 M << "'";
802 }
803 // Don't allow source code to inject newlines into diagnostics.
804 llvm::replace(Message, '\n', ' ');
805 }
806 }
807 if (Message.empty()) // either !SyntheticMessage, or we failed to make one.
808 Info.FormatDiagnostic(Message);
809 LastDiag->Fixes.push_back(
810 Fix{std::string(Message), std::move(Edits), {}});
811 return true;
812 };
813
814 if (!isNote(DiagLevel)) {
815 // Handle the new main diagnostic.
816 flushLastDiag();
817
818 LastDiag = Diag();
819 // FIXME: Merge with feature modules.
820 if (Adjuster)
821 DiagLevel = Adjuster(DiagLevel, Info);
822
823 FillDiagBase(*LastDiag);
824 if (isExcluded(LastDiag->ID))
825 LastDiag->Severity = DiagnosticsEngine::Ignored;
826 if (DiagCB)
827 DiagCB(Info, *LastDiag);
828 // Don't bother filling in the rest if diag is going to be dropped.
829 if (LastDiag->Severity == DiagnosticsEngine::Ignored)
830 return;
831
832 LastDiagLoc.emplace(Info.getLocation(), Info.getSourceManager());
833 LastDiagOriginallyError = OriginallyError;
834 if (!Info.getFixItHints().empty())
835 AddFix(true /* try to invent a message instead of repeating the diag */);
836 if (Fixer) {
837 auto ExtraFixes = Fixer(LastDiag->Severity, Info);
838 LastDiag->Fixes.insert(LastDiag->Fixes.end(), ExtraFixes.begin(),
839 ExtraFixes.end());
840 }
841 } else {
842 // Handle a note to an existing diagnostic.
843 if (!LastDiag) {
844 assert(false && "Adding a note without main diagnostic");
845 IgnoreDiagnostics::log(DiagLevel, Info);
846 return;
847 }
848
849 // If a diagnostic was suppressed due to the suppression filter,
850 // also suppress notes associated with it.
851 if (LastDiag->Severity == DiagnosticsEngine::Ignored)
852 return;
853
854 // Give include-fixer a chance to replace a note with a fix.
855 if (Fixer) {
856 auto ReplacementFixes = Fixer(LastDiag->Severity, Info);
857 if (!ReplacementFixes.empty()) {
858 assert(Info.getNumFixItHints() == 0 &&
859 "Include-fixer replaced a note with clang fix-its attached!");
860 LastDiag->Fixes.insert(LastDiag->Fixes.end(), ReplacementFixes.begin(),
861 ReplacementFixes.end());
862 return;
863 }
864 }
865
866 if (!Info.getFixItHints().empty()) {
867 // A clang note with fix-it is not a separate diagnostic in clangd. We
868 // attach it as a Fix to the main diagnostic instead.
869 if (!AddFix(false /* use the note as the message */))
870 IgnoreDiagnostics::log(DiagLevel, Info);
871 } else {
872 // A clang note without fix-its corresponds to clangd::Note.
873 Note N;
874 FillDiagBase(N);
875
876 LastDiag->Notes.push_back(std::move(N));
877 }
878 }
879}
880
881void StoreDiags::flushLastDiag() {
882 if (!LastDiag)
883 return;
884 llvm::scope_exit Finish([&, NDiags(Output.size())] {
885 if (Output.size() == NDiags) // No new diag emitted.
886 vlog("Dropped diagnostic: {0}: {1}", LastDiag->File, LastDiag->Message);
887 LastDiag.reset();
888 });
889
890 if (LastDiag->Severity == DiagnosticsEngine::Ignored)
891 return;
892 // Move errors that occur from headers into main file.
893 if (!LastDiag->InsideMainFile && LastDiagLoc && LastDiagOriginallyError) {
894 if (tryMoveToMainFile(*LastDiag, *LastDiagLoc)) {
895 // Suppress multiple errors from the same inclusion.
896 if (!IncludedErrorLocations
897 .insert({LastDiag->Range.start.line,
898 LastDiag->Range.start.character})
899 .second)
900 return;
901 }
902 }
903 if (!mentionsMainFile(*LastDiag))
904 return;
905 Output.push_back(std::move(*LastDiag));
906}
907
908bool isDiagnosticSuppressed(const clang::Diagnostic &Diag,
909 const llvm::StringSet<> &Suppress,
910 const LangOptions &LangOpts) {
911 // Don't complain about header-only stuff in mainfiles if it's a header.
912 // FIXME: would be cleaner to suppress in clang, once we decide whether the
913 // behavior should be to silently-ignore or respect the pragma.
914 if (Diag.getID() == diag::pp_pragma_sysheader_in_main_file &&
915 LangOpts.IsHeaderFile)
916 return true;
917
918 if (const char *CodePtr = getDiagnosticCode(Diag.getID())) {
919 if (Suppress.contains(normalizeSuppressedCode(CodePtr)))
920 return true;
921 }
922 StringRef Warning =
923 Diag.getDiags()->getDiagnosticIDs()->getWarningOptionForDiag(
924 Diag.getID());
925 if (!Warning.empty() && Suppress.contains(Warning))
926 return true;
927 return false;
928}
929
930llvm::StringRef normalizeSuppressedCode(llvm::StringRef Code) {
931 Code.consume_front("err_");
932 Code.consume_front("-W");
933 return Code;
934}
935
936std::optional<std::string> getDiagnosticDocURI(Diag::DiagSource Source,
937 unsigned ID,
938 llvm::StringRef Name) {
939 switch (Source) {
940 case Diag::Unknown:
941 break;
942 case Diag::Clang:
943 // There is a page listing many warning flags, but it provides too little
944 // information to be worth linking.
945 // https://clang.llvm.org/docs/DiagnosticsReference.html
946 break;
947 case Diag::ClangTidy: {
948 StringRef Module, Check;
949 // This won't correctly get the module for clang-analyzer checks, but as we
950 // don't link in the analyzer that shouldn't be an issue.
951 // This would also need updating if anyone decides to create a module with a
952 // '-' in the name.
953 std::tie(Module, Check) = Name.split('-');
954 if (Module.empty() || Check.empty())
955 return std::nullopt;
956 return ("https://clang.llvm.org/extra/clang-tidy/checks/" + Module + "/" +
957 Check + ".html")
958 .str();
959 }
960 case Diag::Clangd:
961 if (Name == "unused-includes" || Name == "missing-includes")
962 return {"https://clangd.llvm.org/guides/include-cleaner"};
963 break;
965 // FIXME: we should link to https://clangd.llvm.org/config
966 // However we have no diagnostic codes, which the link should describe!
967 break;
968 }
969 return std::nullopt;
970}
971
972} // namespace clangd
973} // namespace clang
static void log(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
Definition Compiler.cpp:21
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) override
std::vector< Diag > take(const clang::tidy::ClangTidyContext *Tidy=nullptr)
void BeginSourceFile(const LangOptions &Opts, const Preprocessor *PP) override
void EndSourceFile() override
A URI describes the location of a source file.
Definition URI.h:28
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
std::string getCheckName(unsigned DiagnosticID) const
Returns the name of the clang-tidy check which produced this diagnostic ID.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
bool isDiagnosticSuppressed(const clang::Diagnostic &Diag, const llvm::StringSet<> &Suppress, const LangOptions &LangOpts)
Determine whether a (non-clang-tidy) diagnostic is suppressed by config.
static void writeCodeToFixMessage(llvm::raw_ostream &OS, llvm::StringRef Code)
Sanitizes a piece for presenting it in a synthesized fix message.
@ Warning
A warning message.
Definition Protocol.h:736
@ Info
An information message.
Definition Protocol.h:738
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M, const LangOptions &L)
static void fillNonLocationData(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info, clangd::DiagBase &D)
Fills D with all information, except the location-related bits.
void toLSPDiags(const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts, llvm::function_ref< void(clangd::Diagnostic, llvm::ArrayRef< Fix >)> OutFn)
Conversion to LSP diagnostics.
bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM)
Returns true iff Loc is inside the main file.
void vlog(const char *Fmt, Ts &&... Vals)
Definition Logger.h:72
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
SourceLocation translatePreamblePatchLocation(SourceLocation Loc, const SourceManager &SM)
Translates locations inside preamble patch to their main-file equivalent using presumed locations.
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
std::optional< std::string > getCanonicalPath(const FileEntryRef F, FileManager &FileMgr)
Get the canonical path of F.
llvm::StringRef normalizeSuppressedCode(llvm::StringRef Code)
Take a user-specified diagnostic code, and convert it to a normalized form stored in the config and c...
Diag toDiag(const llvm::SMDiagnostic &D, Diag::DiagSource Source)
int getSeverity(DiagnosticsEngine::Level L)
Convert from clang diagnostic level to LSP severity.
@ Deprecated
Deprecated or obsolete code.
Definition Protocol.h:919
@ Unnecessary
Unused or unnecessary code.
Definition Protocol.h:915
std::optional< std::string > getDiagnosticDocURI(Diag::DiagSource Source, unsigned ID, llvm::StringRef Name)
Returns a URI providing more information about a particular diagnostic.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
bool SendDiagnosticCategory
If true, Clangd uses an LSP extension to send the diagnostic's category to the client.
Definition Diagnostics.h:50
bool EmitRelatedLocations
If true, Clangd uses the relatedInformation field to include other locations (in particular attached ...
Definition Diagnostics.h:44
Contains basic information about a diagnostic.
Definition Diagnostics.h:58
DiagnosticsEngine::Level Severity
Definition Diagnostics.h:67
std::optional< std::string > AbsFile
Definition Diagnostics.h:64
A top-level diagnostic that may have Notes and Fixes.
Definition Diagnostics.h:98
std::vector< Fix > Fixes
Alternative fixes for this diagnostic, one should be chosen.
enum clang::clangd::Diag::DiagSource Source
std::vector< Note > Notes
Elaborate on the problem, usually pointing to a related piece of code.
Represents a related message and source code location for a diagnostic.
Definition Protocol.h:902
std::string message
The message of this related diagnostic information.
Definition Protocol.h:906
Location location
The location of this related diagnostic information.
Definition Protocol.h:904
std::optional< CodeDescription > codeDescription
An optional property to describe the error code.
Definition Protocol.h:943
llvm::json::Object data
A data entry field that is preserved between a textDocument/publishDiagnostics notification and textD...
Definition Protocol.h:975
std::optional< std::vector< DiagnosticRelatedInformation > > relatedInformation
An array of related diagnostic information, e.g.
Definition Protocol.h:957
std::string code
The diagnostic's code. Can be omitted.
Definition Protocol.h:940
Range range
The range at which the message applies.
Definition Protocol.h:933
std::string source
A human-readable string describing the source of this diagnostic, e.g.
Definition Protocol.h:947
std::string message
The diagnostic's message.
Definition Protocol.h:950
int severity
The diagnostic's severity.
Definition Protocol.h:937
std::optional< std::string > category
The diagnostic's category.
Definition Protocol.h:963
llvm::SmallVector< DiagnosticTag, 1 > tags
Additional metadata about the diagnostic.
Definition Protocol.h:953
A set of edits generated for a single file.
Definition SourceCode.h:189
Represents a single fix-it that editor can apply to fix the error.
Definition Diagnostics.h:81
std::string Message
Message for the fix-it.
Definition Diagnostics.h:83
llvm::SmallVector< TextEdit, 1 > Edits
TextEdits from clang's fix-its. Must be non-empty.
Definition Diagnostics.h:85
URIForFile uri
The text document's URI.
Definition Protocol.h:213
Represents a note for the diagnostic.
Definition Diagnostics.h:95
A single C++ or preprocessor token.
static URIForFile canonicalize(llvm::StringRef AbsPath, llvm::StringRef TUPath)
Canonicalizes AbsPath via URI.
Definition Protocol.cpp:46