blob: 1cd52ad3475877c5d506145e26151d0b93f0cf5c [file] [log] [blame]
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00001//===--- Diagnostics.cpp -----------------------------------------*- C++-*-===//
Ilya Biryukov71028b82018-03-12 15:28:22 +00002//
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//
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00008//===----------------------------------------------------------------------===//
Ilya Biryukov71028b82018-03-12 15:28:22 +00009
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
20namespace clang {
21namespace clangd {
22
23namespace {
24
25bool 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!
40bool 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.
51Range diagnosticRange(const clang::Diagnostic &D, const LangOptions &L) {
52 auto &M = D.getSourceManager();
53 auto Loc = M.getFileLoc(D.getLocation());
Ilya Biryukov71028b82018-03-12 15:28:22 +000054 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 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000059 llvm::Optional<Range> FallbackRange;
Ilya Biryukov71028b82018-03-12 15:28:22 +000060 // 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);
Kadir Cetinkaya2e3f1f12018-09-27 12:12:42 +000065 // 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.
Kadir Cetinkaya84bf6072018-10-09 08:41:12 +000068 if (R.getBegin() == R.getEnd() && Loc == R.getBegin())
Kadir Cetinkaya2e3f1f12018-09-27 12:12:42 +000069 FallbackRange = halfOpenToRange(M, R);
Ilya Biryukov71028b82018-03-12 15:28:22 +000070 }
Kadir Cetinkaya2e3f1f12018-09-27 12:12:42 +000071 if (FallbackRange)
72 return *FallbackRange;
Ilya Biryukov71028b82018-03-12 15:28:22 +000073 // 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
Ilya Biryukov71028b82018-03-12 15:28:22 +000080bool isInsideMainFile(const SourceLocation Loc, const SourceManager &M) {
Ilya Biryukove8ccb822018-11-26 17:05:13 +000081 return Loc.isValid() && M.isWrittenInMainFile(M.getFileLoc(Loc));
Ilya Biryukov71028b82018-03-12 15:28:22 +000082}
83
84bool isInsideMainFile(const clang::Diagnostic &D) {
85 if (!D.hasSourceManager())
86 return false;
87
88 return isInsideMainFile(D.getLocation(), D.getSourceManager());
89}
90
91bool isNote(DiagnosticsEngine::Level L) {
92 return L == DiagnosticsEngine::Note || L == DiagnosticsEngine::Remark;
93}
94
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000095llvm::StringRef diagLeveltoString(DiagnosticsEngine::Level Lvl) {
Ilya Biryukov71028b82018-03-12 15:28:22 +000096 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
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000124void printDiag(llvm::raw_string_ostream &OS, const DiagBase &D) {
Ilya Biryukov71028b82018-03-12 15:28:22 +0000125 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.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000129 OS << llvm::sys::path::filename(D.File) << ":";
Ilya Biryukov71028b82018-03-12 15:28:22 +0000130 } 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
Alex Lorenzb411cf32018-08-03 20:43:28 +0000146/// Capitalizes the first word in the diagnostic's message.
147std::string capitalize(std::string Message) {
148 if (!Message.empty())
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000149 Message[0] = llvm::toUpper(Message[0]);
Alex Lorenzb411cf32018-08-03 20:43:28 +0000150 return Message;
151}
152
Ilya Biryukov71028b82018-03-12 15:28:22 +0000153/// 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
165std::string mainMessage(const Diag &D) {
166 std::string Result;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000167 llvm::raw_string_ostream OS(Result);
Ilya Biryukov71028b82018-03-12 15:28:22 +0000168 OS << D.Message;
169 for (auto &Note : D.Notes) {
170 OS << "\n\n";
171 printDiag(OS, Note);
172 }
173 OS.flush();
Alex Lorenzb411cf32018-08-03 20:43:28 +0000174 return capitalize(std::move(Result));
Ilya Biryukov71028b82018-03-12 15:28:22 +0000175}
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.
180std::string noteMessage(const Diag &Main, const DiagBase &Note) {
181 std::string Result;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000182 llvm::raw_string_ostream OS(Result);
Ilya Biryukov71028b82018-03-12 15:28:22 +0000183 OS << Note.Message;
184 OS << "\n\n";
185 printDiag(OS, Main);
186 OS.flush();
Alex Lorenzb411cf32018-08-03 20:43:28 +0000187 return capitalize(std::move(Result));
Ilya Biryukov71028b82018-03-12 15:28:22 +0000188}
189} // namespace
190
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000191llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DiagBase &D) {
Sam McCall991e3162018-11-20 10:58:48 +0000192 OS << "[";
Ilya Biryukov71028b82018-03-12 15:28:22 +0000193 if (!D.InsideMainFile)
Sam McCall991e3162018-11-20 10:58:48 +0000194 OS << D.File << ":";
195 OS << D.Range.start << "-" << D.Range.end << "] ";
196
Ilya Biryukov71028b82018-03-12 15:28:22 +0000197 return OS << D.Message;
198}
199
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000200llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Fix &F) {
Ilya Biryukov71028b82018-03-12 15:28:22 +0000201 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
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000210llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diag &D) {
Ilya Biryukov71028b82018-03-12 15:28:22 +0000211 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
Sam McCall16e70702018-10-24 07:59:38 +0000232CodeAction toCodeAction(const Fix &F, const URIForFile &File) {
233 CodeAction Action;
234 Action.title = F.Message;
235 Action.kind = CodeAction::QUICKFIX_KIND;
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
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000242void toLSPDiags(
243 const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts,
244 llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn) {
Ilya Biryukov71028b82018-03-12 15:28:22 +0000245 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);
Sam McCall16e70702018-10-24 07:59:38 +0000255 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
Ilya Biryukov71028b82018-03-12 15:28:22 +0000263 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);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000271 OutFn(std::move(Res), llvm::ArrayRef<Fix>());
Ilya Biryukov71028b82018-03-12 15:28:22 +0000272 }
273}
274
275int 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
292std::vector<Diag> StoreDiags::take() { return std::move(Output); }
293
294void StoreDiags::BeginSourceFile(const LangOptions &Opts,
295 const Preprocessor *) {
296 LangOpts = Opts;
297}
298
299void StoreDiags::EndSourceFile() {
300 flushLastDiag();
Sam McCallc008af62018-10-20 15:30:37 +0000301 LangOpts = None;
Ilya Biryukov71028b82018-03-12 15:28:22 +0000302}
303
304void 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);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000317 llvm::SmallString<64> Message;
Ilya Biryukov71028b82018-03-12 15:28:22 +0000318 Info.FormatDiagnostic(Message);
319 D.Message = Message.str();
320 D.InsideMainFile = InsideMainFile;
321 D.File = Info.getSourceManager().getFilename(Info.getLocation());
322 D.Severity = DiagLevel;
Alex Lorenz37146432018-08-14 22:21:40 +0000323 D.Category = DiagnosticIDs::getCategoryNameFromID(
324 DiagnosticIDs::getCategoryNumberForDiag(Info.getID()))
325 .str();
Ilya Biryukov71028b82018-03-12 15:28:22 +0000326 return D;
327 };
328
Sam McCallba3c02e2018-04-03 17:35:57 +0000329 auto AddFix = [&](bool SyntheticMessage) -> bool {
Ilya Biryukov71028b82018-03-12 15:28:22 +0000330 assert(!Info.getFixItHints().empty() &&
331 "diagnostic does not have attached fix-its");
332 if (!InsideMainFile)
333 return false;
334
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000335 llvm::SmallVector<TextEdit, 1> Edits;
Ilya Biryukov71028b82018-03-12 15:28:22 +0000336 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
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000343 llvm::SmallString<64> Message;
Sam McCallba3c02e2018-04-03 17:35:57 +0000344 // 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;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000348 llvm::StringRef Remove = Lexer::getSourceText(
Sam McCallba3c02e2018-04-03 17:35:57 +0000349 FixIt.RemoveRange, Info.getSourceManager(), *LangOpts, &Invalid);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000350 llvm::StringRef Insert = FixIt.CodeToInsert;
Sam McCallba3c02e2018-04-03 17:35:57 +0000351 if (!Invalid) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000352 llvm::raw_svector_ostream M(Message);
Sam McCallba3c02e2018-04-03 17:35:57 +0000353 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);
Ilya Biryukov71028b82018-03-12 15:28:22 +0000365 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())
Sam McCallba3c02e2018-04-03 17:35:57 +0000377 AddFix(true /* try to invent a message instead of repeating the diag */);
Ilya Biryukov71028b82018-03-12 15:28:22 +0000378 } 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.
Sam McCallba3c02e2018-04-03 17:35:57 +0000389 if (!AddFix(false /* use the note as the message */))
Ilya Biryukov71028b82018-03-12 15:28:22 +0000390 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
401void StoreDiags::flushLastDiag() {
402 if (!LastDiag)
403 return;
404 if (mentionsMainFile(*LastDiag))
405 Output.push_back(std::move(*LastDiag));
406 else
Sam McCallbed58852018-07-11 10:35:11 +0000407 log("Dropped diagnostic outside main file: {0}: {1}", LastDiag->File,
Ilya Biryukov71028b82018-03-12 15:28:22 +0000408 LastDiag->Message);
409 LastDiag.reset();
410}
411
412} // namespace clangd
413} // namespace clang