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