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, DEFERRABLE, 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 instantiation 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 
toDiag(const llvm::SMDiagnostic & D,Diag::DiagSource Source)380 Diag toDiag(const llvm::SMDiagnostic &D, Diag::DiagSource Source) {
381   Diag Result;
382   Result.Message = D.getMessage().str();
383   switch (D.getKind()) {
384   case llvm::SourceMgr::DK_Error:
385     Result.Severity = DiagnosticsEngine::Error;
386     break;
387   case llvm::SourceMgr::DK_Warning:
388     Result.Severity = DiagnosticsEngine::Warning;
389     break;
390   default:
391     break;
392   }
393   Result.Source = Source;
394   Result.AbsFile = D.getFilename().str();
395   Result.InsideMainFile = D.getSourceMgr()->FindBufferContainingLoc(
396                               D.getLoc()) == D.getSourceMgr()->getMainFileID();
397   if (D.getRanges().empty())
398     Result.Range = {{D.getLineNo() - 1, D.getColumnNo()},
399                     {D.getLineNo() - 1, D.getColumnNo()}};
400   else
401     Result.Range = {{D.getLineNo() - 1, (int)D.getRanges().front().first},
402                     {D.getLineNo() - 1, (int)D.getRanges().front().second}};
403   return Result;
404 }
405 
toLSPDiags(const Diag & D,const URIForFile & File,const ClangdDiagnosticOptions & Opts,llvm::function_ref<void (clangd::Diagnostic,llvm::ArrayRef<Fix>)> OutFn)406 void toLSPDiags(
407     const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts,
408     llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn) {
409   clangd::Diagnostic Main;
410   Main.severity = getSeverity(D.Severity);
411 
412   // Main diagnostic should always refer to a range inside main file. If a
413   // diagnostic made it so for, it means either itself or one of its notes is
414   // inside main file.
415   if (D.InsideMainFile) {
416     Main.range = D.Range;
417   } else {
418     auto It =
419         llvm::find_if(D.Notes, [](const Note &N) { return N.InsideMainFile; });
420     assert(It != D.Notes.end() &&
421            "neither the main diagnostic nor notes are inside main file");
422     Main.range = It->Range;
423   }
424 
425   Main.code = D.Name;
426   switch (D.Source) {
427   case Diag::Clang:
428     Main.source = "clang";
429     break;
430   case Diag::ClangTidy:
431     Main.source = "clang-tidy";
432     break;
433   case Diag::ClangdConfig:
434     Main.source = "clangd-config";
435     break;
436   case Diag::Unknown:
437     break;
438   }
439   if (Opts.EmbedFixesInDiagnostics) {
440     Main.codeActions.emplace();
441     for (const auto &Fix : D.Fixes)
442       Main.codeActions->push_back(toCodeAction(Fix, File));
443     if (Main.codeActions->size() == 1)
444       Main.codeActions->front().isPreferred = true;
445   }
446   if (Opts.SendDiagnosticCategory && !D.Category.empty())
447     Main.category = D.Category;
448 
449   Main.message = mainMessage(D, Opts);
450   if (Opts.EmitRelatedLocations) {
451     Main.relatedInformation.emplace();
452     for (auto &Note : D.Notes) {
453       if (!Note.AbsFile) {
454         vlog("Dropping note from unknown file: {0}", Note);
455         continue;
456       }
457       DiagnosticRelatedInformation RelInfo;
458       RelInfo.location.range = Note.Range;
459       RelInfo.location.uri =
460           URIForFile::canonicalize(*Note.AbsFile, File.file());
461       RelInfo.message = noteMessage(D, Note, Opts);
462       Main.relatedInformation->push_back(std::move(RelInfo));
463     }
464   }
465   OutFn(std::move(Main), D.Fixes);
466 
467   // If we didn't emit the notes as relatedLocations, emit separate diagnostics
468   // so the user can find the locations easily.
469   if (!Opts.EmitRelatedLocations)
470     for (auto &Note : D.Notes) {
471       if (!Note.InsideMainFile)
472         continue;
473       clangd::Diagnostic Res;
474       Res.severity = getSeverity(Note.Severity);
475       Res.range = Note.Range;
476       Res.message = noteMessage(D, Note, Opts);
477       OutFn(std::move(Res), llvm::ArrayRef<Fix>());
478     }
479 }
480 
getSeverity(DiagnosticsEngine::Level L)481 int getSeverity(DiagnosticsEngine::Level L) {
482   switch (L) {
483   case DiagnosticsEngine::Remark:
484     return 4;
485   case DiagnosticsEngine::Note:
486     return 3;
487   case DiagnosticsEngine::Warning:
488     return 2;
489   case DiagnosticsEngine::Fatal:
490   case DiagnosticsEngine::Error:
491     return 1;
492   case DiagnosticsEngine::Ignored:
493     return 0;
494   }
495   llvm_unreachable("Unknown diagnostic level!");
496 }
497 
take(const clang::tidy::ClangTidyContext * Tidy)498 std::vector<Diag> StoreDiags::take(const clang::tidy::ClangTidyContext *Tidy) {
499   // Do not forget to emit a pending diagnostic if there is one.
500   flushLastDiag();
501 
502   // Fill in name/source now that we have all the context needed to map them.
503   for (auto &Diag : Output) {
504     if (const char *ClangDiag = getDiagnosticCode(Diag.ID)) {
505       // Warnings controlled by -Wfoo are better recognized by that name.
506       StringRef Warning = DiagnosticIDs::getWarningOptionForDiag(Diag.ID);
507       if (!Warning.empty()) {
508         Diag.Name = ("-W" + Warning).str();
509       } else {
510         StringRef Name(ClangDiag);
511         // Almost always an error, with a name like err_enum_class_reference.
512         // Drop the err_ prefix for brevity.
513         Name.consume_front("err_");
514         Diag.Name = std::string(Name);
515       }
516       Diag.Source = Diag::Clang;
517       continue;
518     }
519     if (Tidy != nullptr) {
520       std::string TidyDiag = Tidy->getCheckName(Diag.ID);
521       if (!TidyDiag.empty()) {
522         Diag.Name = std::move(TidyDiag);
523         Diag.Source = Diag::ClangTidy;
524         // clang-tidy bakes the name into diagnostic messages. Strip it out.
525         // It would be much nicer to make clang-tidy not do this.
526         auto CleanMessage = [&](std::string &Msg) {
527           StringRef Rest(Msg);
528           if (Rest.consume_back("]") && Rest.consume_back(Diag.Name) &&
529               Rest.consume_back(" ["))
530             Msg.resize(Rest.size());
531         };
532         CleanMessage(Diag.Message);
533         for (auto &Note : Diag.Notes)
534           CleanMessage(Note.Message);
535         for (auto &Fix : Diag.Fixes)
536           CleanMessage(Fix.Message);
537         continue;
538       }
539     }
540   }
541   // Deduplicate clang-tidy diagnostics -- some clang-tidy checks may emit
542   // duplicated messages due to various reasons (e.g. the check doesn't handle
543   // template instantiations well; clang-tidy alias checks).
544   std::set<std::pair<Range, std::string>> SeenDiags;
545   llvm::erase_if(Output, [&](const Diag& D) {
546     return !SeenDiags.emplace(D.Range, D.Message).second;
547   });
548   return std::move(Output);
549 }
550 
BeginSourceFile(const LangOptions & Opts,const Preprocessor * PP)551 void StoreDiags::BeginSourceFile(const LangOptions &Opts,
552                                  const Preprocessor *PP) {
553   LangOpts = Opts;
554   if (PP) {
555     OrigSrcMgr = &PP->getSourceManager();
556   }
557 }
558 
EndSourceFile()559 void StoreDiags::EndSourceFile() {
560   flushLastDiag();
561   LangOpts = None;
562   OrigSrcMgr = nullptr;
563 }
564 
565 /// Sanitizes a piece for presenting it in a synthesized fix message. Ensures
566 /// the result is not too large and does not contain newlines.
writeCodeToFixMessage(llvm::raw_ostream & OS,llvm::StringRef Code)567 static void writeCodeToFixMessage(llvm::raw_ostream &OS, llvm::StringRef Code) {
568   constexpr unsigned MaxLen = 50;
569 
570   // Only show the first line if there are many.
571   llvm::StringRef R = Code.split('\n').first;
572   // Shorten the message if it's too long.
573   R = R.take_front(MaxLen);
574 
575   OS << R;
576   if (R.size() != Code.size())
577     OS << "…";
578 }
579 
580 /// Fills \p D with all information, except the location-related bits.
581 /// Also note that ID and Name are not part of clangd::DiagBase and should be
582 /// set elsewhere.
fillNonLocationData(DiagnosticsEngine::Level DiagLevel,const clang::Diagnostic & Info,clangd::DiagBase & D)583 static void fillNonLocationData(DiagnosticsEngine::Level DiagLevel,
584                                 const clang::Diagnostic &Info,
585                                 clangd::DiagBase &D) {
586   llvm::SmallString<64> Message;
587   Info.FormatDiagnostic(Message);
588 
589   D.Message = std::string(Message.str());
590   D.Severity = DiagLevel;
591   D.Category = DiagnosticIDs::getCategoryNameFromID(
592                    DiagnosticIDs::getCategoryNumberForDiag(Info.getID()))
593                    .str();
594 }
595 
HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,const clang::Diagnostic & Info)596 void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
597                                   const clang::Diagnostic &Info) {
598   // If the diagnostic was generated for a different SourceManager, skip it.
599   // This happens when a module is imported and needs to be implicitly built.
600   // The compilation of that module will use the same StoreDiags, but different
601   // SourceManager.
602   if (OrigSrcMgr && Info.hasSourceManager() &&
603       OrigSrcMgr != &Info.getSourceManager()) {
604     IgnoreDiagnostics::log(DiagLevel, Info);
605     return;
606   }
607 
608   DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
609   bool OriginallyError =
610       Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError(
611           Info.getID());
612 
613   if (Info.getLocation().isInvalid()) {
614     // Handle diagnostics coming from command-line arguments. The source manager
615     // is *not* available at this point, so we cannot use it.
616     if (!OriginallyError) {
617       IgnoreDiagnostics::log(DiagLevel, Info);
618       return; // non-errors add too much noise, do not show them.
619     }
620 
621     flushLastDiag();
622 
623     LastDiag = Diag();
624     LastDiagLoc.reset();
625     LastDiagOriginallyError = OriginallyError;
626     LastDiag->ID = Info.getID();
627     fillNonLocationData(DiagLevel, Info, *LastDiag);
628     LastDiag->InsideMainFile = true;
629     // Put it at the start of the main file, for a lack of a better place.
630     LastDiag->Range.start = Position{0, 0};
631     LastDiag->Range.end = Position{0, 0};
632     return;
633   }
634 
635   if (!LangOpts || !Info.hasSourceManager()) {
636     IgnoreDiagnostics::log(DiagLevel, Info);
637     return;
638   }
639 
640   bool InsideMainFile = isInsideMainFile(Info);
641   SourceManager &SM = Info.getSourceManager();
642 
643   auto FillDiagBase = [&](DiagBase &D) {
644     fillNonLocationData(DiagLevel, Info, D);
645 
646     D.InsideMainFile = InsideMainFile;
647     D.Range = diagnosticRange(Info, *LangOpts);
648     D.File = std::string(SM.getFilename(Info.getLocation()));
649     D.AbsFile = getCanonicalPath(
650         SM.getFileEntryForID(SM.getFileID(Info.getLocation())), SM);
651     D.ID = Info.getID();
652     return D;
653   };
654 
655   auto AddFix = [&](bool SyntheticMessage) -> bool {
656     assert(!Info.getFixItHints().empty() &&
657            "diagnostic does not have attached fix-its");
658     if (!InsideMainFile)
659       return false;
660 
661     // Copy as we may modify the ranges.
662     auto FixIts = Info.getFixItHints().vec();
663     llvm::SmallVector<TextEdit, 1> Edits;
664     for (auto &FixIt : FixIts) {
665       // Allow fixits within a single macro-arg expansion to be applied.
666       // This can be incorrect if the argument is expanded multiple times in
667       // different contexts. Hopefully this is rare!
668       if (FixIt.RemoveRange.getBegin().isMacroID() &&
669           FixIt.RemoveRange.getEnd().isMacroID() &&
670           SM.getFileID(FixIt.RemoveRange.getBegin()) ==
671               SM.getFileID(FixIt.RemoveRange.getEnd())) {
672         FixIt.RemoveRange = CharSourceRange(
673             {SM.getTopMacroCallerLoc(FixIt.RemoveRange.getBegin()),
674              SM.getTopMacroCallerLoc(FixIt.RemoveRange.getEnd())},
675             FixIt.RemoveRange.isTokenRange());
676       }
677       // Otherwise, follow clang's behavior: no fixits in macros.
678       if (FixIt.RemoveRange.getBegin().isMacroID() ||
679           FixIt.RemoveRange.getEnd().isMacroID())
680         return false;
681       if (!isInsideMainFile(FixIt.RemoveRange.getBegin(), SM))
682         return false;
683       Edits.push_back(toTextEdit(FixIt, SM, *LangOpts));
684     }
685 
686     llvm::SmallString<64> Message;
687     // If requested and possible, create a message like "change 'foo' to 'bar'".
688     if (SyntheticMessage && FixIts.size() == 1) {
689       const auto &FixIt = FixIts.front();
690       bool Invalid = false;
691       llvm::StringRef Remove =
692           Lexer::getSourceText(FixIt.RemoveRange, SM, *LangOpts, &Invalid);
693       llvm::StringRef Insert = FixIt.CodeToInsert;
694       if (!Invalid) {
695         llvm::raw_svector_ostream M(Message);
696         if (!Remove.empty() && !Insert.empty()) {
697           M << "change '";
698           writeCodeToFixMessage(M, Remove);
699           M << "' to '";
700           writeCodeToFixMessage(M, Insert);
701           M << "'";
702         } else if (!Remove.empty()) {
703           M << "remove '";
704           writeCodeToFixMessage(M, Remove);
705           M << "'";
706         } else if (!Insert.empty()) {
707           M << "insert '";
708           writeCodeToFixMessage(M, Insert);
709           M << "'";
710         }
711         // Don't allow source code to inject newlines into diagnostics.
712         std::replace(Message.begin(), Message.end(), '\n', ' ');
713       }
714     }
715     if (Message.empty()) // either !SyntheticMessage, or we failed to make one.
716       Info.FormatDiagnostic(Message);
717     LastDiag->Fixes.push_back(
718         Fix{std::string(Message.str()), std::move(Edits)});
719     return true;
720   };
721 
722   if (!isNote(DiagLevel)) {
723     // Handle the new main diagnostic.
724     flushLastDiag();
725 
726     if (Adjuster) {
727       DiagLevel = Adjuster(DiagLevel, Info);
728       if (DiagLevel == DiagnosticsEngine::Ignored) {
729         LastPrimaryDiagnosticWasSuppressed = true;
730         return;
731       }
732     }
733     LastPrimaryDiagnosticWasSuppressed = false;
734 
735     LastDiag = Diag();
736     FillDiagBase(*LastDiag);
737     LastDiagLoc.emplace(Info.getLocation(), Info.getSourceManager());
738     LastDiagOriginallyError = OriginallyError;
739 
740     if (!Info.getFixItHints().empty())
741       AddFix(true /* try to invent a message instead of repeating the diag */);
742     if (Fixer) {
743       auto ExtraFixes = Fixer(DiagLevel, Info);
744       LastDiag->Fixes.insert(LastDiag->Fixes.end(), ExtraFixes.begin(),
745                              ExtraFixes.end());
746     }
747   } else {
748     // Handle a note to an existing diagnostic.
749 
750     // If a diagnostic was suppressed due to the suppression filter,
751     // also suppress notes associated with it.
752     if (LastPrimaryDiagnosticWasSuppressed) {
753       return;
754     }
755 
756     if (!LastDiag) {
757       assert(false && "Adding a note without main diagnostic");
758       IgnoreDiagnostics::log(DiagLevel, Info);
759       return;
760     }
761 
762     if (!Info.getFixItHints().empty()) {
763       // A clang note with fix-it is not a separate diagnostic in clangd. We
764       // attach it as a Fix to the main diagnostic instead.
765       if (!AddFix(false /* use the note as the message */))
766         IgnoreDiagnostics::log(DiagLevel, Info);
767     } else {
768       // A clang note without fix-its corresponds to clangd::Note.
769       Note N;
770       FillDiagBase(N);
771 
772       LastDiag->Notes.push_back(std::move(N));
773     }
774   }
775 }
776 
flushLastDiag()777 void StoreDiags::flushLastDiag() {
778   if (!LastDiag)
779     return;
780   auto Finish = llvm::make_scope_exit([&, NDiags(Output.size())] {
781     if (Output.size() == NDiags) // No new diag emitted.
782       vlog("Dropped diagnostic: {0}: {1}", LastDiag->File, LastDiag->Message);
783     LastDiag.reset();
784   });
785 
786   if (isExcluded(*LastDiag))
787     return;
788   // Move errors that occur from headers into main file.
789   if (!LastDiag->InsideMainFile && LastDiagLoc && LastDiagOriginallyError) {
790     if (tryMoveToMainFile(*LastDiag, *LastDiagLoc)) {
791       // Suppress multiple errors from the same inclusion.
792       if (!IncludedErrorLocations
793                .insert({LastDiag->Range.start.line,
794                         LastDiag->Range.start.character})
795                .second)
796         return;
797     }
798   }
799   if (!mentionsMainFile(*LastDiag))
800     return;
801   Output.push_back(std::move(*LastDiag));
802 }
803 
isBuiltinDiagnosticSuppressed(unsigned ID,const llvm::StringSet<> & Suppress)804 bool isBuiltinDiagnosticSuppressed(unsigned ID,
805                                    const llvm::StringSet<> &Suppress) {
806   if (const char *CodePtr = getDiagnosticCode(ID)) {
807     if (Suppress.contains(normalizeSuppressedCode(CodePtr)))
808       return true;
809   }
810   StringRef Warning = DiagnosticIDs::getWarningOptionForDiag(ID);
811   if (!Warning.empty() && Suppress.contains(Warning))
812     return true;
813   return false;
814 }
815 
normalizeSuppressedCode(llvm::StringRef Code)816 llvm::StringRef normalizeSuppressedCode(llvm::StringRef Code) {
817   Code.consume_front("err_");
818   Code.consume_front("-W");
819   return Code;
820 }
821 
822 } // namespace clangd
823 } // namespace clang
824