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