clang-tools  8.0.0
Diagnostics.cpp
Go to the documentation of this file.
1 //===--- Diagnostics.cpp -----------------------------------------*- C++-*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Diagnostics.h"
11 #include "Compiler.h"
12 #include "Logger.h"
13 #include "SourceCode.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "clang/Lex/Lexer.h"
16 #include "llvm/Support/Capacity.h"
17 #include "llvm/Support/Path.h"
18 #include <algorithm>
19 
20 namespace clang {
21 namespace clangd {
22 
23 namespace {
24 
25 bool mentionsMainFile(const Diag &D) {
26  if (D.InsideMainFile)
27  return true;
28  // Fixes are always in the main file.
29  if (!D.Fixes.empty())
30  return true;
31  for (auto &N : D.Notes) {
32  if (N.InsideMainFile)
33  return true;
34  }
35  return false;
36 }
37 
38 // Checks whether a location is within a half-open range.
39 // Note that clang also uses closed source ranges, which this can't handle!
40 bool locationInRange(SourceLocation L, CharSourceRange R,
41  const SourceManager &M) {
42  assert(R.isCharRange());
43  if (!R.isValid() || M.getFileID(R.getBegin()) != M.getFileID(R.getEnd()) ||
44  M.getFileID(R.getBegin()) != M.getFileID(L))
45  return false;
46  return L != R.getEnd() && M.isPointWithin(L, R.getBegin(), R.getEnd());
47 }
48 
49 // Clang diags have a location (shown as ^) and 0 or more ranges (~~~~).
50 // LSP needs a single range.
51 Range diagnosticRange(const clang::Diagnostic &D, const LangOptions &L) {
52  auto &M = D.getSourceManager();
53  auto Loc = M.getFileLoc(D.getLocation());
54  for (const auto &CR : D.getRanges()) {
55  auto R = Lexer::makeFileCharRange(CR, M, L);
56  if (locationInRange(Loc, R, M))
57  return halfOpenToRange(M, R);
58  }
59  llvm::Optional<Range> FallbackRange;
60  // The range may be given as a fixit hint instead.
61  for (const auto &F : D.getFixItHints()) {
62  auto R = Lexer::makeFileCharRange(F.RemoveRange, M, L);
63  if (locationInRange(Loc, R, M))
64  return halfOpenToRange(M, R);
65  // If there's a fixit that performs insertion, it has zero-width. Therefore
66  // it can't contain the location of the diag, but it might be possible that
67  // this should be reported as range. For example missing semicolon.
68  if (R.getBegin() == R.getEnd() && Loc == R.getBegin())
69  FallbackRange = halfOpenToRange(M, R);
70  }
71  if (FallbackRange)
72  return *FallbackRange;
73  // If no suitable range is found, just use the token at the location.
74  auto R = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(Loc), M, L);
75  if (!R.isValid()) // Fall back to location only, let the editor deal with it.
76  R = CharSourceRange::getCharRange(Loc);
77  return halfOpenToRange(M, R);
78 }
79 
80 bool isInsideMainFile(const SourceLocation Loc, const SourceManager &M) {
81  return Loc.isValid() && M.isWrittenInMainFile(M.getFileLoc(Loc));
82 }
83 
84 bool isInsideMainFile(const clang::Diagnostic &D) {
85  if (!D.hasSourceManager())
86  return false;
87 
88  return isInsideMainFile(D.getLocation(), D.getSourceManager());
89 }
90 
91 bool isNote(DiagnosticsEngine::Level L) {
92  return L == DiagnosticsEngine::Note || L == DiagnosticsEngine::Remark;
93 }
94 
95 llvm::StringRef diagLeveltoString(DiagnosticsEngine::Level Lvl) {
96  switch (Lvl) {
97  case DiagnosticsEngine::Ignored:
98  return "ignored";
99  case DiagnosticsEngine::Note:
100  return "note";
101  case DiagnosticsEngine::Remark:
102  return "remark";
103  case DiagnosticsEngine::Warning:
104  return "warning";
105  case DiagnosticsEngine::Error:
106  return "error";
107  case DiagnosticsEngine::Fatal:
108  return "fatal error";
109  }
110  llvm_unreachable("unhandled DiagnosticsEngine::Level");
111 }
112 
113 /// Prints a single diagnostic in a clang-like manner, the output includes
114 /// location, severity and error message. An example of the output message is:
115 ///
116 /// main.cpp:12:23: error: undeclared identifier
117 ///
118 /// For main file we only print the basename and for all other files we print
119 /// the filename on a separate line to provide a slightly more readable output
120 /// in the editors:
121 ///
122 /// dir1/dir2/dir3/../../dir4/header.h:12:23
123 /// error: undeclared identifier
124 void printDiag(llvm::raw_string_ostream &OS, const DiagBase &D) {
125  if (D.InsideMainFile) {
126  // Paths to main files are often taken from compile_command.json, where they
127  // are typically absolute. To reduce noise we print only basename for them,
128  // it should not be confusing and saves space.
129  OS << llvm::sys::path::filename(D.File) << ":";
130  } else {
131  OS << D.File << ":";
132  }
133  // Note +1 to line and character. clangd::Range is zero-based, but when
134  // printing for users we want one-based indexes.
135  auto Pos = D.Range.start;
136  OS << (Pos.line + 1) << ":" << (Pos.character + 1) << ":";
137  // The non-main-file paths are often too long, putting them on a separate
138  // line improves readability.
139  if (D.InsideMainFile)
140  OS << " ";
141  else
142  OS << "\n";
143  OS << diagLeveltoString(D.Severity) << ": " << D.Message;
144 }
145 
146 /// Capitalizes the first word in the diagnostic's message.
147 std::string capitalize(std::string Message) {
148  if (!Message.empty())
149  Message[0] = llvm::toUpper(Message[0]);
150  return Message;
151 }
152 
153 /// Returns a message sent to LSP for the main diagnostic in \p D.
154 /// The message includes all the notes with their corresponding locations.
155 /// However, notes with fix-its are excluded as those usually only contain a
156 /// fix-it message and just add noise if included in the message for diagnostic.
157 /// Example output:
158 ///
159 /// no matching function for call to 'foo'
160 ///
161 /// main.cpp:3:5: note: candidate function not viable: requires 2 arguments
162 ///
163 /// dir1/dir2/dir3/../../dir4/header.h:12:23
164 /// note: candidate function not viable: requires 3 arguments
165 std::string mainMessage(const Diag &D) {
166  std::string Result;
167  llvm::raw_string_ostream OS(Result);
168  OS << D.Message;
169  for (auto &Note : D.Notes) {
170  OS << "\n\n";
171  printDiag(OS, Note);
172  }
173  OS.flush();
174  return capitalize(std::move(Result));
175 }
176 
177 /// Returns a message sent to LSP for the note of the main diagnostic.
178 /// The message includes the main diagnostic to provide the necessary context
179 /// for the user to understand the note.
180 std::string noteMessage(const Diag &Main, const DiagBase &Note) {
181  std::string Result;
182  llvm::raw_string_ostream OS(Result);
183  OS << Note.Message;
184  OS << "\n\n";
185  printDiag(OS, Main);
186  OS.flush();
187  return capitalize(std::move(Result));
188 }
189 } // namespace
190 
191 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DiagBase &D) {
192  OS << "[";
193  if (!D.InsideMainFile)
194  OS << D.File << ":";
195  OS << D.Range.start << "-" << D.Range.end << "] ";
196 
197  return OS << D.Message;
198 }
199 
200 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Fix &F) {
201  OS << F.Message << " {";
202  const char *Sep = "";
203  for (const auto &Edit : F.Edits) {
204  OS << Sep << Edit;
205  Sep = ", ";
206  }
207  return OS << "}";
208 }
209 
210 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diag &D) {
211  OS << static_cast<const DiagBase &>(D);
212  if (!D.Notes.empty()) {
213  OS << ", notes: {";
214  const char *Sep = "";
215  for (auto &Note : D.Notes) {
216  OS << Sep << Note;
217  Sep = ", ";
218  }
219  OS << "}";
220  }
221  if (!D.Fixes.empty()) {
222  OS << ", fixes: {";
223  const char *Sep = "";
224  for (auto &Fix : D.Fixes) {
225  OS << Sep << Fix;
226  Sep = ", ";
227  }
228  }
229  return OS;
230 }
231 
232 CodeAction toCodeAction(const Fix &F, const URIForFile &File) {
234  Action.title = F.Message;
236  Action.edit.emplace();
237  Action.edit->changes.emplace();
238  (*Action.edit->changes)[File.uri()] = {F.Edits.begin(), F.Edits.end()};
239  return Action;
240 }
241 
243  const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts,
244  llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn) {
245  auto FillBasicFields = [](const DiagBase &D) -> clangd::Diagnostic {
246  clangd::Diagnostic Res;
247  Res.range = D.Range;
248  Res.severity = getSeverity(D.Severity);
249  return Res;
250  };
251 
252  {
253  clangd::Diagnostic Main = FillBasicFields(D);
254  Main.message = mainMessage(D);
255  if (Opts.EmbedFixesInDiagnostics) {
256  Main.codeActions.emplace();
257  for (const auto &Fix : D.Fixes)
258  Main.codeActions->push_back(toCodeAction(Fix, File));
259  }
260  if (Opts.SendDiagnosticCategory && !D.Category.empty())
261  Main.category = D.Category;
262 
263  OutFn(std::move(Main), D.Fixes);
264  }
265 
266  for (auto &Note : D.Notes) {
267  if (!Note.InsideMainFile)
268  continue;
269  clangd::Diagnostic Res = FillBasicFields(Note);
270  Res.message = noteMessage(D, Note);
271  OutFn(std::move(Res), llvm::ArrayRef<Fix>());
272  }
273 }
274 
275 int getSeverity(DiagnosticsEngine::Level L) {
276  switch (L) {
277  case DiagnosticsEngine::Remark:
278  return 4;
279  case DiagnosticsEngine::Note:
280  return 3;
281  case DiagnosticsEngine::Warning:
282  return 2;
283  case DiagnosticsEngine::Fatal:
284  case DiagnosticsEngine::Error:
285  return 1;
286  case DiagnosticsEngine::Ignored:
287  return 0;
288  }
289  llvm_unreachable("Unknown diagnostic level!");
290 }
291 
292 std::vector<Diag> StoreDiags::take() { return std::move(Output); }
293 
294 void StoreDiags::BeginSourceFile(const LangOptions &Opts,
295  const Preprocessor *) {
296  LangOpts = Opts;
297 }
298 
300  flushLastDiag();
301  LangOpts = None;
302 }
303 
304 void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
305  const clang::Diagnostic &Info) {
306  DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
307 
308  if (!LangOpts || !Info.hasSourceManager()) {
309  IgnoreDiagnostics::log(DiagLevel, Info);
310  return;
311  }
312 
313  bool InsideMainFile = isInsideMainFile(Info);
314 
315  auto FillDiagBase = [&](DiagBase &D) {
316  D.Range = diagnosticRange(Info, *LangOpts);
317  llvm::SmallString<64> Message;
318  Info.FormatDiagnostic(Message);
319  D.Message = Message.str();
320  D.InsideMainFile = InsideMainFile;
321  D.File = Info.getSourceManager().getFilename(Info.getLocation());
322  D.Severity = DiagLevel;
323  D.Category = DiagnosticIDs::getCategoryNameFromID(
324  DiagnosticIDs::getCategoryNumberForDiag(Info.getID()))
325  .str();
326  return D;
327  };
328 
329  auto AddFix = [&](bool SyntheticMessage) -> bool {
330  assert(!Info.getFixItHints().empty() &&
331  "diagnostic does not have attached fix-its");
332  if (!InsideMainFile)
333  return false;
334 
335  llvm::SmallVector<TextEdit, 1> Edits;
336  for (auto &FixIt : Info.getFixItHints()) {
337  if (!isInsideMainFile(FixIt.RemoveRange.getBegin(),
338  Info.getSourceManager()))
339  return false;
340  Edits.push_back(toTextEdit(FixIt, Info.getSourceManager(), *LangOpts));
341  }
342 
343  llvm::SmallString<64> Message;
344  // If requested and possible, create a message like "change 'foo' to 'bar'".
345  if (SyntheticMessage && Info.getNumFixItHints() == 1) {
346  const auto &FixIt = Info.getFixItHint(0);
347  bool Invalid = false;
348  llvm::StringRef Remove = Lexer::getSourceText(
349  FixIt.RemoveRange, Info.getSourceManager(), *LangOpts, &Invalid);
350  llvm::StringRef Insert = FixIt.CodeToInsert;
351  if (!Invalid) {
352  llvm::raw_svector_ostream M(Message);
353  if (!Remove.empty() && !Insert.empty())
354  M << "change '" << Remove << "' to '" << Insert << "'";
355  else if (!Remove.empty())
356  M << "remove '" << Remove << "'";
357  else if (!Insert.empty())
358  M << "insert '" << Insert << "'";
359  // Don't allow source code to inject newlines into diagnostics.
360  std::replace(Message.begin(), Message.end(), '\n', ' ');
361  }
362  }
363  if (Message.empty()) // either !SytheticMessage, or we failed to make one.
364  Info.FormatDiagnostic(Message);
365  LastDiag->Fixes.push_back(Fix{Message.str(), std::move(Edits)});
366  return true;
367  };
368 
369  if (!isNote(DiagLevel)) {
370  // Handle the new main diagnostic.
371  flushLastDiag();
372 
373  LastDiag = Diag();
374  FillDiagBase(*LastDiag);
375 
376  if (!Info.getFixItHints().empty())
377  AddFix(true /* try to invent a message instead of repeating the diag */);
378  } else {
379  // Handle a note to an existing diagnostic.
380  if (!LastDiag) {
381  assert(false && "Adding a note without main diagnostic");
382  IgnoreDiagnostics::log(DiagLevel, Info);
383  return;
384  }
385 
386  if (!Info.getFixItHints().empty()) {
387  // A clang note with fix-it is not a separate diagnostic in clangd. We
388  // attach it as a Fix to the main diagnostic instead.
389  if (!AddFix(false /* use the note as the message */))
390  IgnoreDiagnostics::log(DiagLevel, Info);
391  } else {
392  // A clang note without fix-its corresponds to clangd::Note.
393  Note N;
394  FillDiagBase(N);
395 
396  LastDiag->Notes.push_back(std::move(N));
397  }
398  }
399 }
400 
401 void StoreDiags::flushLastDiag() {
402  if (!LastDiag)
403  return;
404  if (mentionsMainFile(*LastDiag))
405  Output.push_back(std::move(*LastDiag));
406  else
407  log("Dropped diagnostic outside main file: {0}: {1}", LastDiag->File,
408  LastDiag->Message);
409  LastDiag.reset();
410 }
411 
412 } // namespace clangd
413 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
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:156
std::vector< Diag > take()
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:29
Contains basic information about a diagnostic.
Definition: Diagnostics.h:39
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:590
llvm::Optional< std::string > kind
The kind of the code action.
Definition: Protocol.h:671
std::string title
A short, human-readable, title for this code action.
Definition: Protocol.h:667
A code action represents a change that can be performed in code, e.g.
Definition: Protocol.h:665
llvm::Optional< WorkspaceEdit > edit
The workspace edit this code action performs.
Definition: Protocol.h:678
Documents should not be synced at all.
void BeginSourceFile(const LangOptions &Opts, const Preprocessor *) override
A top-level diagnostic that may have Notes and Fixes.
Definition: Diagnostics.h:67
std::string uri() const
Definition: Protocol.h:94
std::string Message
Message for the fix-it.
Definition: Diagnostics.h:56
std::vector< Fix > Fixes
Alternative fixes for this diagnostic, one should be chosen.
Definition: Diagnostics.h:71
llvm::SmallVector< TextEdit, 1 > Edits
TextEdits from clang&#39;s fix-its. Must be non-empty.
Definition: Diagnostics.h:58
TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M, const LangOptions &L)
Definition: SourceCode.cpp:226
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
void log(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:63
llvm::Optional< std::string > category
The diagnostic&#39;s category.
Definition: Protocol.h:585
clangd::Range Range
Definition: Diagnostics.h:44
DiagnosticsEngine::Level Severity
Definition: Diagnostics.h:45
const Decl * D
Definition: XRefs.cpp:79
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Represents a single fix-it that editor can apply to fix the error.
Definition: Diagnostics.h:54
Position Pos
FunctionInfo Info
int line
Line position in a document (zero-based).
Definition: Protocol.h:127
int character
Character offset on a line in a document (zero-based).
Definition: Protocol.h:132
std::vector< Note > Notes
Elaborate on the problem, usually pointing to a related piece of code.
Definition: Diagnostics.h:69
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::string message
The diagnostic&#39;s code.
Definition: Protocol.h:579
CharSourceRange Range
SourceRange for the file name.
static const llvm::StringLiteral QUICKFIX_KIND
Definition: Protocol.h:672
int severity
The diagnostic&#39;s severity.
Definition: Protocol.h:567
bool SendDiagnosticCategory
If true, Clangd uses an LSP extension to send the diagnostic&#39;s category to the client.
Definition: Diagnostics.h:35
void EndSourceFile() override
llvm::Optional< FixItHint > FixIt
Range range
The range at which the message applies.
Definition: Protocol.h:563
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:159
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
Definition: SourceCode.cpp:145