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