2018-08-15 00:03:32 +08:00
|
|
|
//===--- Diagnostics.cpp -----------------------------------------*- C++-*-===//
|
2018-03-12 23:28:22 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-03-12 23:28:22 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-03-12 23:28:22 +08:00
|
|
|
|
|
|
|
#include "Diagnostics.h"
|
2019-04-17 20:35:16 +08:00
|
|
|
#include "../clang-tidy/ClangTidyDiagnosticConsumer.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "Compiler.h"
|
2019-04-18 23:17:07 +08:00
|
|
|
#include "Protocol.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "SourceCode.h"
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Logger.h"
|
2019-04-17 20:35:16 +08:00
|
|
|
#include "clang/Basic/AllDiagnostics.h"
|
2019-04-29 18:25:44 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2019-04-17 20:35:16 +08:00
|
|
|
#include "clang/Basic/DiagnosticIDs.h"
|
2019-04-29 18:25:44 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
2019-06-13 20:31:36 +08:00
|
|
|
#include "clang/Lex/Token.h"
|
2019-04-29 18:25:44 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2019-07-30 18:26:51 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2020-01-09 23:54:22 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2020-06-09 21:46:35 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2021-07-27 20:25:29 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2019-04-29 18:25:44 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "llvm/Support/Capacity.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2019-04-29 18:25:44 +08:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
2019-05-24 18:26:23 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include <algorithm>
|
2019-05-24 18:26:23 +08:00
|
|
|
#include <cstddef>
|
2021-07-27 20:25:29 +08:00
|
|
|
#include <vector>
|
2018-03-12 23:28:22 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
2019-04-17 20:35:16 +08:00
|
|
|
const char *getDiagnosticCode(unsigned ID) {
|
|
|
|
switch (ID) {
|
|
|
|
#define DIAG(ENUM, CLASS, DEFAULT_MAPPING, DESC, GROPU, SFINAE, NOWERROR, \
|
2020-09-24 06:00:23 +08:00
|
|
|
SHOWINSYSHEADER, DEFERRABLE, CATEGORY) \
|
2019-04-17 20:35:16 +08:00
|
|
|
case clang::diag::ENUM: \
|
|
|
|
return #ENUM;
|
|
|
|
#include "clang/Basic/DiagnosticASTKinds.inc"
|
|
|
|
#include "clang/Basic/DiagnosticAnalysisKinds.inc"
|
|
|
|
#include "clang/Basic/DiagnosticCommentKinds.inc"
|
|
|
|
#include "clang/Basic/DiagnosticCommonKinds.inc"
|
|
|
|
#include "clang/Basic/DiagnosticDriverKinds.inc"
|
|
|
|
#include "clang/Basic/DiagnosticFrontendKinds.inc"
|
|
|
|
#include "clang/Basic/DiagnosticLexKinds.inc"
|
|
|
|
#include "clang/Basic/DiagnosticParseKinds.inc"
|
|
|
|
#include "clang/Basic/DiagnosticRefactoringKinds.inc"
|
|
|
|
#include "clang/Basic/DiagnosticSemaKinds.inc"
|
|
|
|
#include "clang/Basic/DiagnosticSerializationKinds.inc"
|
|
|
|
#undef DIAG
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
bool mentionsMainFile(const Diag &D) {
|
|
|
|
if (D.InsideMainFile)
|
|
|
|
return true;
|
|
|
|
// Fixes are always in the main file.
|
|
|
|
if (!D.Fixes.empty())
|
|
|
|
return true;
|
|
|
|
for (auto &N : D.Notes) {
|
|
|
|
if (N.InsideMainFile)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-31 14:24:06 +08:00
|
|
|
bool isExcluded(unsigned DiagID) {
|
2019-12-10 02:21:58 +08:00
|
|
|
// clang will always fail parsing MS ASM, we don't link in desc + asm parser.
|
2021-05-31 14:24:06 +08:00
|
|
|
if (DiagID == clang::diag::err_msasm_unable_to_create_target ||
|
|
|
|
DiagID == clang::diag::err_msasm_unsupported_arch)
|
2019-12-09 18:57:23 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
// Checks whether a location is within a half-open range.
|
|
|
|
// Note that clang also uses closed source ranges, which this can't handle!
|
|
|
|
bool locationInRange(SourceLocation L, CharSourceRange R,
|
|
|
|
const SourceManager &M) {
|
|
|
|
assert(R.isCharRange());
|
|
|
|
if (!R.isValid() || M.getFileID(R.getBegin()) != M.getFileID(R.getEnd()) ||
|
|
|
|
M.getFileID(R.getBegin()) != M.getFileID(L))
|
|
|
|
return false;
|
|
|
|
return L != R.getEnd() && M.isPointWithin(L, R.getBegin(), R.getEnd());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clang diags have a location (shown as ^) and 0 or more ranges (~~~~).
|
|
|
|
// LSP needs a single range.
|
|
|
|
Range diagnosticRange(const clang::Diagnostic &D, const LangOptions &L) {
|
|
|
|
auto &M = D.getSourceManager();
|
|
|
|
auto Loc = M.getFileLoc(D.getLocation());
|
|
|
|
for (const auto &CR : D.getRanges()) {
|
|
|
|
auto R = Lexer::makeFileCharRange(CR, M, L);
|
|
|
|
if (locationInRange(Loc, R, M))
|
|
|
|
return halfOpenToRange(M, R);
|
|
|
|
}
|
|
|
|
// The range may be given as a fixit hint instead.
|
|
|
|
for (const auto &F : D.getFixItHints()) {
|
|
|
|
auto R = Lexer::makeFileCharRange(F.RemoveRange, M, L);
|
|
|
|
if (locationInRange(Loc, R, M))
|
|
|
|
return halfOpenToRange(M, R);
|
|
|
|
}
|
2019-06-13 20:31:36 +08:00
|
|
|
// If the token at the location is not a comment, we use the token.
|
|
|
|
// If we can't get the token at the location, fall back to using the location
|
|
|
|
auto R = CharSourceRange::getCharRange(Loc);
|
|
|
|
Token Tok;
|
|
|
|
if (!Lexer::getRawToken(Loc, Tok, M, L, true) && Tok.isNot(tok::comment)) {
|
|
|
|
R = CharSourceRange::getTokenRange(Tok.getLocation(), Tok.getEndLoc());
|
|
|
|
}
|
2018-03-12 23:28:22 +08:00
|
|
|
return halfOpenToRange(M, R);
|
|
|
|
}
|
|
|
|
|
2020-06-09 21:46:35 +08:00
|
|
|
// Try to find a location in the main-file to report the diagnostic D.
|
|
|
|
// Returns a description like "in included file", or nullptr on failure.
|
|
|
|
const char *getMainFileRange(const Diag &D, const SourceManager &SM,
|
|
|
|
SourceLocation DiagLoc, Range &R) {
|
|
|
|
// Look for a note in the main file indicating template instantiation.
|
|
|
|
for (const auto &N : D.Notes) {
|
|
|
|
if (N.InsideMainFile) {
|
|
|
|
switch (N.ID) {
|
|
|
|
case diag::note_template_class_instantiation_was_here:
|
|
|
|
case diag::note_template_class_explicit_specialization_was_here:
|
|
|
|
case diag::note_template_class_instantiation_here:
|
|
|
|
case diag::note_template_member_class_here:
|
|
|
|
case diag::note_template_member_function_here:
|
|
|
|
case diag::note_function_template_spec_here:
|
|
|
|
case diag::note_template_static_data_member_def_here:
|
|
|
|
case diag::note_template_variable_def_here:
|
|
|
|
case diag::note_template_enum_def_here:
|
|
|
|
case diag::note_template_nsdmi_here:
|
|
|
|
case diag::note_template_type_alias_instantiation_here:
|
|
|
|
case diag::note_template_exception_spec_instantiation_here:
|
|
|
|
case diag::note_template_requirement_instantiation_here:
|
|
|
|
case diag::note_evaluating_exception_spec_here:
|
|
|
|
case diag::note_default_arg_instantiation_here:
|
|
|
|
case diag::note_default_function_arg_instantiation_here:
|
|
|
|
case diag::note_explicit_template_arg_substitution_here:
|
|
|
|
case diag::note_function_template_deduction_instantiation_here:
|
|
|
|
case diag::note_deduced_template_arg_substitution_here:
|
|
|
|
case diag::note_prior_template_arg_substitution:
|
|
|
|
case diag::note_template_default_arg_checking:
|
|
|
|
case diag::note_concept_specialization_here:
|
|
|
|
case diag::note_nested_requirement_here:
|
|
|
|
case diag::note_checking_constraints_for_template_id_here:
|
|
|
|
case diag::note_checking_constraints_for_var_spec_id_here:
|
|
|
|
case diag::note_checking_constraints_for_class_spec_id_here:
|
|
|
|
case diag::note_checking_constraints_for_function_here:
|
|
|
|
case diag::note_constraint_substitution_here:
|
|
|
|
case diag::note_constraint_normalization_here:
|
|
|
|
case diag::note_parameter_mapping_substitution_here:
|
|
|
|
R = N.Range;
|
|
|
|
return "in template";
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Look for where the file with the error was #included.
|
2019-04-29 18:25:44 +08:00
|
|
|
auto GetIncludeLoc = [&SM](SourceLocation SLoc) {
|
|
|
|
return SM.getIncludeLoc(SM.getFileID(SLoc));
|
|
|
|
};
|
2020-06-09 21:46:35 +08:00
|
|
|
for (auto IncludeLocation = GetIncludeLoc(SM.getExpansionLoc(DiagLoc));
|
|
|
|
IncludeLocation.isValid();
|
2019-08-12 17:35:04 +08:00
|
|
|
IncludeLocation = GetIncludeLoc(IncludeLocation)) {
|
|
|
|
if (clangd::isInsideMainFile(IncludeLocation, SM)) {
|
2020-06-09 21:46:35 +08:00
|
|
|
R.start = sourceLocToPosition(SM, IncludeLocation);
|
|
|
|
R.end = sourceLocToPosition(
|
|
|
|
SM,
|
|
|
|
Lexer::getLocForEndOfToken(IncludeLocation, 0, SM, LangOptions()));
|
|
|
|
return "in included file";
|
2019-08-12 17:35:04 +08:00
|
|
|
}
|
|
|
|
}
|
2020-06-09 21:46:35 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-04-29 18:25:44 +08:00
|
|
|
|
2020-06-09 21:46:35 +08:00
|
|
|
// Place the diagnostic the main file, rather than the header, if possible:
|
|
|
|
// - for errors in included files, use the #include location
|
2020-12-02 09:02:46 +08:00
|
|
|
// - for errors in template instantiation, use the instantiation location
|
2020-06-09 21:46:35 +08:00
|
|
|
// In both cases, add the original header location as a note.
|
|
|
|
bool tryMoveToMainFile(Diag &D, FullSourceLoc DiagLoc) {
|
|
|
|
const SourceManager &SM = DiagLoc.getManager();
|
|
|
|
DiagLoc = DiagLoc.getExpansionLoc();
|
|
|
|
Range R;
|
|
|
|
const char *Prefix = getMainFileRange(D, SM, DiagLoc, R);
|
|
|
|
if (!Prefix)
|
|
|
|
return false;
|
2020-06-09 21:36:29 +08:00
|
|
|
|
2019-04-29 18:25:44 +08:00
|
|
|
// Add a note that will point to real diagnostic.
|
|
|
|
const auto *FE = SM.getFileEntryForID(SM.getFileID(DiagLoc));
|
2020-06-09 21:46:35 +08:00
|
|
|
D.Notes.emplace(D.Notes.begin());
|
|
|
|
Note &N = D.Notes.front();
|
2020-01-29 03:23:46 +08:00
|
|
|
N.AbsFile = std::string(FE->tryGetRealPathName());
|
|
|
|
N.File = std::string(FE->getName());
|
2019-04-29 18:25:44 +08:00
|
|
|
N.Message = "error occurred here";
|
2020-06-09 21:46:35 +08:00
|
|
|
N.Range = D.Range;
|
2019-04-29 18:25:44 +08:00
|
|
|
|
2020-06-09 21:46:35 +08:00
|
|
|
// Update diag to point at include inside main file.
|
|
|
|
D.File = SM.getFileEntryForID(SM.getMainFileID())->getName().str();
|
|
|
|
D.Range = std::move(R);
|
|
|
|
D.InsideMainFile = true;
|
2019-04-29 18:25:44 +08:00
|
|
|
// Update message to mention original file.
|
2020-06-09 21:46:35 +08:00
|
|
|
D.Message = llvm::formatv("{0}: {1}", Prefix, D.Message);
|
2019-07-30 18:26:51 +08:00
|
|
|
return true;
|
2019-04-29 18:25:44 +08:00
|
|
|
}
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
bool isInsideMainFile(const clang::Diagnostic &D) {
|
|
|
|
if (!D.hasSourceManager())
|
|
|
|
return false;
|
|
|
|
|
2019-07-19 16:33:39 +08:00
|
|
|
return clangd::isInsideMainFile(D.getLocation(), D.getSourceManager());
|
2018-03-12 23:28:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isNote(DiagnosticsEngine::Level L) {
|
|
|
|
return L == DiagnosticsEngine::Note || L == DiagnosticsEngine::Remark;
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef diagLeveltoString(DiagnosticsEngine::Level Lvl) {
|
2018-03-12 23:28:22 +08:00
|
|
|
switch (Lvl) {
|
|
|
|
case DiagnosticsEngine::Ignored:
|
|
|
|
return "ignored";
|
|
|
|
case DiagnosticsEngine::Note:
|
|
|
|
return "note";
|
|
|
|
case DiagnosticsEngine::Remark:
|
|
|
|
return "remark";
|
|
|
|
case DiagnosticsEngine::Warning:
|
|
|
|
return "warning";
|
|
|
|
case DiagnosticsEngine::Error:
|
|
|
|
return "error";
|
|
|
|
case DiagnosticsEngine::Fatal:
|
|
|
|
return "fatal error";
|
|
|
|
}
|
|
|
|
llvm_unreachable("unhandled DiagnosticsEngine::Level");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prints a single diagnostic in a clang-like manner, the output includes
|
|
|
|
/// location, severity and error message. An example of the output message is:
|
|
|
|
///
|
|
|
|
/// main.cpp:12:23: error: undeclared identifier
|
|
|
|
///
|
|
|
|
/// For main file we only print the basename and for all other files we print
|
|
|
|
/// the filename on a separate line to provide a slightly more readable output
|
|
|
|
/// in the editors:
|
|
|
|
///
|
|
|
|
/// dir1/dir2/dir3/../../dir4/header.h:12:23
|
|
|
|
/// error: undeclared identifier
|
2019-01-07 23:45:19 +08:00
|
|
|
void printDiag(llvm::raw_string_ostream &OS, const DiagBase &D) {
|
2018-03-12 23:28:22 +08:00
|
|
|
if (D.InsideMainFile) {
|
|
|
|
// Paths to main files are often taken from compile_command.json, where they
|
|
|
|
// are typically absolute. To reduce noise we print only basename for them,
|
|
|
|
// it should not be confusing and saves space.
|
2019-01-07 23:45:19 +08:00
|
|
|
OS << llvm::sys::path::filename(D.File) << ":";
|
2018-03-12 23:28:22 +08:00
|
|
|
} else {
|
|
|
|
OS << D.File << ":";
|
|
|
|
}
|
|
|
|
// Note +1 to line and character. clangd::Range is zero-based, but when
|
|
|
|
// printing for users we want one-based indexes.
|
|
|
|
auto Pos = D.Range.start;
|
|
|
|
OS << (Pos.line + 1) << ":" << (Pos.character + 1) << ":";
|
|
|
|
// The non-main-file paths are often too long, putting them on a separate
|
|
|
|
// line improves readability.
|
|
|
|
if (D.InsideMainFile)
|
|
|
|
OS << " ";
|
|
|
|
else
|
|
|
|
OS << "\n";
|
|
|
|
OS << diagLeveltoString(D.Severity) << ": " << D.Message;
|
|
|
|
}
|
|
|
|
|
2018-08-04 04:43:28 +08:00
|
|
|
/// Capitalizes the first word in the diagnostic's message.
|
|
|
|
std::string capitalize(std::string Message) {
|
|
|
|
if (!Message.empty())
|
2019-01-07 23:45:19 +08:00
|
|
|
Message[0] = llvm::toUpper(Message[0]);
|
2018-08-04 04:43:28 +08:00
|
|
|
return Message;
|
|
|
|
}
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
/// Returns a message sent to LSP for the main diagnostic in \p D.
|
2019-12-16 17:33:56 +08:00
|
|
|
/// This message may include notes, if they're not emitted in some other way.
|
2018-03-12 23:28:22 +08:00
|
|
|
/// Example output:
|
|
|
|
///
|
|
|
|
/// no matching function for call to 'foo'
|
|
|
|
///
|
|
|
|
/// main.cpp:3:5: note: candidate function not viable: requires 2 arguments
|
|
|
|
///
|
|
|
|
/// dir1/dir2/dir3/../../dir4/header.h:12:23
|
|
|
|
/// note: candidate function not viable: requires 3 arguments
|
2019-04-18 23:17:07 +08:00
|
|
|
std::string mainMessage(const Diag &D, const ClangdDiagnosticOptions &Opts) {
|
2018-03-12 23:28:22 +08:00
|
|
|
std::string Result;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::raw_string_ostream OS(Result);
|
2018-03-12 23:28:22 +08:00
|
|
|
OS << D.Message;
|
2019-04-18 23:17:07 +08:00
|
|
|
if (Opts.DisplayFixesCount && !D.Fixes.empty())
|
2019-02-01 00:09:25 +08:00
|
|
|
OS << " (" << (D.Fixes.size() > 1 ? "fixes" : "fix") << " available)";
|
2019-04-18 23:17:07 +08:00
|
|
|
// If notes aren't emitted as structured info, add them to the message.
|
|
|
|
if (!Opts.EmitRelatedLocations)
|
|
|
|
for (auto &Note : D.Notes) {
|
|
|
|
OS << "\n\n";
|
|
|
|
printDiag(OS, Note);
|
|
|
|
}
|
2018-03-12 23:28:22 +08:00
|
|
|
OS.flush();
|
2018-08-04 04:43:28 +08:00
|
|
|
return capitalize(std::move(Result));
|
2018-03-12 23:28:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a message sent to LSP for the note of the main diagnostic.
|
2019-04-18 23:17:07 +08:00
|
|
|
std::string noteMessage(const Diag &Main, const DiagBase &Note,
|
|
|
|
const ClangdDiagnosticOptions &Opts) {
|
2018-03-12 23:28:22 +08:00
|
|
|
std::string Result;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::raw_string_ostream OS(Result);
|
2018-03-12 23:28:22 +08:00
|
|
|
OS << Note.Message;
|
2019-04-18 23:17:07 +08:00
|
|
|
// If the client doesn't support structured links between the note and the
|
|
|
|
// original diagnostic, then emit the main diagnostic to give context.
|
|
|
|
if (!Opts.EmitRelatedLocations) {
|
|
|
|
OS << "\n\n";
|
|
|
|
printDiag(OS, Main);
|
|
|
|
}
|
2018-03-12 23:28:22 +08:00
|
|
|
OS.flush();
|
2018-08-04 04:43:28 +08:00
|
|
|
return capitalize(std::move(Result));
|
2018-03-12 23:28:22 +08:00
|
|
|
}
|
2021-07-27 20:25:29 +08:00
|
|
|
|
|
|
|
void setTags(clangd::Diag &D) {
|
|
|
|
static const auto *DeprecatedDiags = new llvm::DenseSet<unsigned>{
|
|
|
|
diag::warn_access_decl_deprecated,
|
|
|
|
diag::warn_atl_uuid_deprecated,
|
|
|
|
diag::warn_deprecated,
|
|
|
|
diag::warn_deprecated_altivec_src_compat,
|
|
|
|
diag::warn_deprecated_comma_subscript,
|
|
|
|
diag::warn_deprecated_compound_assign_volatile,
|
|
|
|
diag::warn_deprecated_copy,
|
|
|
|
diag::warn_deprecated_copy_with_dtor,
|
|
|
|
diag::warn_deprecated_copy_with_user_provided_copy,
|
|
|
|
diag::warn_deprecated_copy_with_user_provided_dtor,
|
|
|
|
diag::warn_deprecated_def,
|
|
|
|
diag::warn_deprecated_increment_decrement_volatile,
|
|
|
|
diag::warn_deprecated_message,
|
|
|
|
diag::warn_deprecated_redundant_constexpr_static_def,
|
|
|
|
diag::warn_deprecated_register,
|
|
|
|
diag::warn_deprecated_simple_assign_volatile,
|
|
|
|
diag::warn_deprecated_string_literal_conversion,
|
|
|
|
diag::warn_deprecated_this_capture,
|
|
|
|
diag::warn_deprecated_volatile_param,
|
|
|
|
diag::warn_deprecated_volatile_return,
|
|
|
|
diag::warn_deprecated_volatile_structured_binding,
|
|
|
|
diag::warn_opencl_attr_deprecated_ignored,
|
|
|
|
diag::warn_property_method_deprecated,
|
|
|
|
diag::warn_vector_mode_deprecated,
|
|
|
|
};
|
|
|
|
static const auto *UnusedDiags = new llvm::DenseSet<unsigned>{
|
|
|
|
diag::warn_opencl_attr_deprecated_ignored,
|
|
|
|
diag::warn_pragma_attribute_unused,
|
|
|
|
diag::warn_unused_but_set_parameter,
|
|
|
|
diag::warn_unused_but_set_variable,
|
|
|
|
diag::warn_unused_comparison,
|
|
|
|
diag::warn_unused_const_variable,
|
|
|
|
diag::warn_unused_exception_param,
|
|
|
|
diag::warn_unused_function,
|
|
|
|
diag::warn_unused_label,
|
|
|
|
diag::warn_unused_lambda_capture,
|
|
|
|
diag::warn_unused_local_typedef,
|
|
|
|
diag::warn_unused_member_function,
|
|
|
|
diag::warn_unused_parameter,
|
|
|
|
diag::warn_unused_private_field,
|
|
|
|
diag::warn_unused_property_backing_ivar,
|
|
|
|
diag::warn_unused_template,
|
|
|
|
diag::warn_unused_variable,
|
|
|
|
};
|
|
|
|
if (DeprecatedDiags->contains(D.ID)) {
|
|
|
|
D.Tags.push_back(DiagnosticTag::Deprecated);
|
|
|
|
} else if (UnusedDiags->contains(D.ID)) {
|
|
|
|
D.Tags.push_back(DiagnosticTag::Unnecessary);
|
|
|
|
}
|
|
|
|
// FIXME: Set tags for tidy-based diagnostics too.
|
|
|
|
}
|
2018-03-12 23:28:22 +08:00
|
|
|
} // namespace
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DiagBase &D) {
|
2018-11-20 18:58:48 +08:00
|
|
|
OS << "[";
|
2018-03-12 23:28:22 +08:00
|
|
|
if (!D.InsideMainFile)
|
2018-11-20 18:58:48 +08:00
|
|
|
OS << D.File << ":";
|
|
|
|
OS << D.Range.start << "-" << D.Range.end << "] ";
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
return OS << D.Message;
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Fix &F) {
|
2018-03-12 23:28:22 +08:00
|
|
|
OS << F.Message << " {";
|
|
|
|
const char *Sep = "";
|
|
|
|
for (const auto &Edit : F.Edits) {
|
|
|
|
OS << Sep << Edit;
|
|
|
|
Sep = ", ";
|
|
|
|
}
|
|
|
|
return OS << "}";
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diag &D) {
|
2018-03-12 23:28:22 +08:00
|
|
|
OS << static_cast<const DiagBase &>(D);
|
|
|
|
if (!D.Notes.empty()) {
|
|
|
|
OS << ", notes: {";
|
|
|
|
const char *Sep = "";
|
|
|
|
for (auto &Note : D.Notes) {
|
|
|
|
OS << Sep << Note;
|
|
|
|
Sep = ", ";
|
|
|
|
}
|
|
|
|
OS << "}";
|
|
|
|
}
|
|
|
|
if (!D.Fixes.empty()) {
|
|
|
|
OS << ", fixes: {";
|
|
|
|
const char *Sep = "";
|
|
|
|
for (auto &Fix : D.Fixes) {
|
|
|
|
OS << Sep << Fix;
|
|
|
|
Sep = ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
[clangd] Embed fixes as CodeAction, instead of clangd_fixes. Clean up serialization.
Summary:
CodeAction provides us with a standard way of representing fixes inline, so
use it, replacing our existing ad-hoc extension.
After this, it's easy to serialize diagnostics using the structured
toJSON/Protocol.h mechanism rather than assembling JSON ad-hoc.
Reviewers: hokein, arphaman
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53391
llvm-svn: 345119
2018-10-24 15:59:38 +08:00
|
|
|
CodeAction toCodeAction(const Fix &F, const URIForFile &File) {
|
|
|
|
CodeAction Action;
|
|
|
|
Action.title = F.Message;
|
2020-01-29 03:23:46 +08:00
|
|
|
Action.kind = std::string(CodeAction::QUICKFIX_KIND);
|
[clangd] Embed fixes as CodeAction, instead of clangd_fixes. Clean up serialization.
Summary:
CodeAction provides us with a standard way of representing fixes inline, so
use it, replacing our existing ad-hoc extension.
After this, it's easy to serialize diagnostics using the structured
toJSON/Protocol.h mechanism rather than assembling JSON ad-hoc.
Reviewers: hokein, arphaman
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53391
llvm-svn: 345119
2018-10-24 15:59:38 +08:00
|
|
|
Action.edit.emplace();
|
2021-06-01 19:23:59 +08:00
|
|
|
Action.edit->changes[File.uri()] = {F.Edits.begin(), F.Edits.end()};
|
[clangd] Embed fixes as CodeAction, instead of clangd_fixes. Clean up serialization.
Summary:
CodeAction provides us with a standard way of representing fixes inline, so
use it, replacing our existing ad-hoc extension.
After this, it's easy to serialize diagnostics using the structured
toJSON/Protocol.h mechanism rather than assembling JSON ad-hoc.
Reviewers: hokein, arphaman
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53391
llvm-svn: 345119
2018-10-24 15:59:38 +08:00
|
|
|
return Action;
|
|
|
|
}
|
|
|
|
|
2020-12-05 07:39:21 +08:00
|
|
|
Diag toDiag(const llvm::SMDiagnostic &D, Diag::DiagSource Source) {
|
|
|
|
Diag Result;
|
|
|
|
Result.Message = D.getMessage().str();
|
|
|
|
switch (D.getKind()) {
|
|
|
|
case llvm::SourceMgr::DK_Error:
|
|
|
|
Result.Severity = DiagnosticsEngine::Error;
|
|
|
|
break;
|
|
|
|
case llvm::SourceMgr::DK_Warning:
|
|
|
|
Result.Severity = DiagnosticsEngine::Warning;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Result.Source = Source;
|
|
|
|
Result.AbsFile = D.getFilename().str();
|
|
|
|
Result.InsideMainFile = D.getSourceMgr()->FindBufferContainingLoc(
|
|
|
|
D.getLoc()) == D.getSourceMgr()->getMainFileID();
|
|
|
|
if (D.getRanges().empty())
|
|
|
|
Result.Range = {{D.getLineNo() - 1, D.getColumnNo()},
|
|
|
|
{D.getLineNo() - 1, D.getColumnNo()}};
|
|
|
|
else
|
|
|
|
Result.Range = {{D.getLineNo() - 1, (int)D.getRanges().front().first},
|
|
|
|
{D.getLineNo() - 1, (int)D.getRanges().front().second}};
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
void toLSPDiags(
|
|
|
|
const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts,
|
|
|
|
llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn) {
|
2020-01-09 23:54:22 +08:00
|
|
|
clangd::Diagnostic Main;
|
|
|
|
Main.severity = getSeverity(D.Severity);
|
|
|
|
|
|
|
|
// Main diagnostic should always refer to a range inside main file. If a
|
|
|
|
// diagnostic made it so for, it means either itself or one of its notes is
|
|
|
|
// inside main file.
|
|
|
|
if (D.InsideMainFile) {
|
|
|
|
Main.range = D.Range;
|
|
|
|
} else {
|
|
|
|
auto It =
|
|
|
|
llvm::find_if(D.Notes, [](const Note &N) { return N.InsideMainFile; });
|
|
|
|
assert(It != D.Notes.end() &&
|
|
|
|
"neither the main diagnostic nor notes are inside main file");
|
|
|
|
Main.range = It->Range;
|
|
|
|
}
|
2018-03-12 23:28:22 +08:00
|
|
|
|
2019-04-18 23:17:07 +08:00
|
|
|
Main.code = D.Name;
|
|
|
|
switch (D.Source) {
|
|
|
|
case Diag::Clang:
|
|
|
|
Main.source = "clang";
|
|
|
|
break;
|
|
|
|
case Diag::ClangTidy:
|
|
|
|
Main.source = "clang-tidy";
|
|
|
|
break;
|
2020-12-05 07:39:21 +08:00
|
|
|
case Diag::ClangdConfig:
|
|
|
|
Main.source = "clangd-config";
|
|
|
|
break;
|
2019-04-18 23:17:07 +08:00
|
|
|
case Diag::Unknown:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Opts.EmbedFixesInDiagnostics) {
|
|
|
|
Main.codeActions.emplace();
|
|
|
|
for (const auto &Fix : D.Fixes)
|
|
|
|
Main.codeActions->push_back(toCodeAction(Fix, File));
|
2020-09-29 22:28:50 +08:00
|
|
|
if (Main.codeActions->size() == 1)
|
|
|
|
Main.codeActions->front().isPreferred = true;
|
2018-03-12 23:28:22 +08:00
|
|
|
}
|
2019-04-18 23:17:07 +08:00
|
|
|
if (Opts.SendDiagnosticCategory && !D.Category.empty())
|
|
|
|
Main.category = D.Category;
|
2018-03-12 23:28:22 +08:00
|
|
|
|
2019-04-18 23:17:07 +08:00
|
|
|
Main.message = mainMessage(D, Opts);
|
|
|
|
if (Opts.EmitRelatedLocations) {
|
|
|
|
Main.relatedInformation.emplace();
|
|
|
|
for (auto &Note : D.Notes) {
|
|
|
|
if (!Note.AbsFile) {
|
|
|
|
vlog("Dropping note from unknown file: {0}", Note);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DiagnosticRelatedInformation RelInfo;
|
|
|
|
RelInfo.location.range = Note.Range;
|
|
|
|
RelInfo.location.uri =
|
|
|
|
URIForFile::canonicalize(*Note.AbsFile, File.file());
|
|
|
|
RelInfo.message = noteMessage(D, Note, Opts);
|
|
|
|
Main.relatedInformation->push_back(std::move(RelInfo));
|
|
|
|
}
|
2018-03-12 23:28:22 +08:00
|
|
|
}
|
2021-07-27 20:25:29 +08:00
|
|
|
Main.tags = D.Tags;
|
2019-04-18 23:17:07 +08:00
|
|
|
OutFn(std::move(Main), D.Fixes);
|
|
|
|
|
|
|
|
// If we didn't emit the notes as relatedLocations, emit separate diagnostics
|
|
|
|
// so the user can find the locations easily.
|
|
|
|
if (!Opts.EmitRelatedLocations)
|
|
|
|
for (auto &Note : D.Notes) {
|
|
|
|
if (!Note.InsideMainFile)
|
|
|
|
continue;
|
2020-01-09 23:54:22 +08:00
|
|
|
clangd::Diagnostic Res;
|
|
|
|
Res.severity = getSeverity(Note.Severity);
|
|
|
|
Res.range = Note.Range;
|
2019-04-18 23:17:07 +08:00
|
|
|
Res.message = noteMessage(D, Note, Opts);
|
|
|
|
OutFn(std::move(Res), llvm::ArrayRef<Fix>());
|
|
|
|
}
|
2021-03-12 23:10:24 +08:00
|
|
|
|
|
|
|
// FIXME: Get rid of the copies here by taking in a mutable clangd::Diag.
|
|
|
|
for (auto &Entry : D.OpaqueData)
|
|
|
|
Main.data.insert({Entry.first, Entry.second});
|
2018-03-12 23:28:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int getSeverity(DiagnosticsEngine::Level L) {
|
|
|
|
switch (L) {
|
|
|
|
case DiagnosticsEngine::Remark:
|
|
|
|
return 4;
|
|
|
|
case DiagnosticsEngine::Note:
|
|
|
|
return 3;
|
|
|
|
case DiagnosticsEngine::Warning:
|
|
|
|
return 2;
|
|
|
|
case DiagnosticsEngine::Fatal:
|
|
|
|
case DiagnosticsEngine::Error:
|
|
|
|
return 1;
|
|
|
|
case DiagnosticsEngine::Ignored:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown diagnostic level!");
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:35:16 +08:00
|
|
|
std::vector<Diag> StoreDiags::take(const clang::tidy::ClangTidyContext *Tidy) {
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
// Do not forget to emit a pending diagnostic if there is one.
|
|
|
|
flushLastDiag();
|
|
|
|
|
2019-04-17 20:35:16 +08:00
|
|
|
// Fill in name/source now that we have all the context needed to map them.
|
|
|
|
for (auto &Diag : Output) {
|
2021-07-27 20:25:29 +08:00
|
|
|
setTags(Diag);
|
2019-04-17 20:35:16 +08:00
|
|
|
if (const char *ClangDiag = getDiagnosticCode(Diag.ID)) {
|
2019-04-18 04:12:03 +08:00
|
|
|
// Warnings controlled by -Wfoo are better recognized by that name.
|
|
|
|
StringRef Warning = DiagnosticIDs::getWarningOptionForDiag(Diag.ID);
|
|
|
|
if (!Warning.empty()) {
|
|
|
|
Diag.Name = ("-W" + Warning).str();
|
|
|
|
} else {
|
|
|
|
StringRef Name(ClangDiag);
|
|
|
|
// Almost always an error, with a name like err_enum_class_reference.
|
|
|
|
// Drop the err_ prefix for brevity.
|
|
|
|
Name.consume_front("err_");
|
2020-01-29 03:23:46 +08:00
|
|
|
Diag.Name = std::string(Name);
|
2019-04-18 04:12:03 +08:00
|
|
|
}
|
2019-04-17 20:35:16 +08:00
|
|
|
Diag.Source = Diag::Clang;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Tidy != nullptr) {
|
|
|
|
std::string TidyDiag = Tidy->getCheckName(Diag.ID);
|
|
|
|
if (!TidyDiag.empty()) {
|
|
|
|
Diag.Name = std::move(TidyDiag);
|
|
|
|
Diag.Source = Diag::ClangTidy;
|
2019-04-18 04:15:08 +08:00
|
|
|
// clang-tidy bakes the name into diagnostic messages. Strip it out.
|
|
|
|
// It would be much nicer to make clang-tidy not do this.
|
|
|
|
auto CleanMessage = [&](std::string &Msg) {
|
|
|
|
StringRef Rest(Msg);
|
|
|
|
if (Rest.consume_back("]") && Rest.consume_back(Diag.Name) &&
|
|
|
|
Rest.consume_back(" ["))
|
|
|
|
Msg.resize(Rest.size());
|
|
|
|
};
|
|
|
|
CleanMessage(Diag.Message);
|
2019-04-29 18:25:44 +08:00
|
|
|
for (auto &Note : Diag.Notes)
|
2019-04-18 04:15:08 +08:00
|
|
|
CleanMessage(Note.Message);
|
2019-07-01 16:05:53 +08:00
|
|
|
for (auto &Fix : Diag.Fixes)
|
|
|
|
CleanMessage(Fix.Message);
|
2019-04-17 20:35:16 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-05 20:57:56 +08:00
|
|
|
// Deduplicate clang-tidy diagnostics -- some clang-tidy checks may emit
|
|
|
|
// duplicated messages due to various reasons (e.g. the check doesn't handle
|
|
|
|
// template instantiations well; clang-tidy alias checks).
|
|
|
|
std::set<std::pair<Range, std::string>> SeenDiags;
|
2021-07-27 20:25:29 +08:00
|
|
|
llvm::erase_if(Output, [&](const Diag &D) {
|
2019-07-05 20:57:56 +08:00
|
|
|
return !SeenDiags.emplace(D.Range, D.Message).second;
|
|
|
|
});
|
2019-04-17 20:35:16 +08:00
|
|
|
return std::move(Output);
|
|
|
|
}
|
2018-03-12 23:28:22 +08:00
|
|
|
|
|
|
|
void StoreDiags::BeginSourceFile(const LangOptions &Opts,
|
2020-08-12 00:48:22 +08:00
|
|
|
const Preprocessor *PP) {
|
2018-03-12 23:28:22 +08:00
|
|
|
LangOpts = Opts;
|
2020-08-12 00:48:22 +08:00
|
|
|
if (PP) {
|
|
|
|
OrigSrcMgr = &PP->getSourceManager();
|
|
|
|
}
|
2018-03-12 23:28:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StoreDiags::EndSourceFile() {
|
2020-06-09 21:46:35 +08:00
|
|
|
flushLastDiag();
|
2018-10-20 23:30:37 +08:00
|
|
|
LangOpts = None;
|
2020-08-12 00:48:22 +08:00
|
|
|
OrigSrcMgr = nullptr;
|
2018-03-12 23:28:22 +08:00
|
|
|
}
|
|
|
|
|
2019-05-24 18:26:23 +08:00
|
|
|
/// Sanitizes a piece for presenting it in a synthesized fix message. Ensures
|
|
|
|
/// the result is not too large and does not contain newlines.
|
|
|
|
static void writeCodeToFixMessage(llvm::raw_ostream &OS, llvm::StringRef Code) {
|
|
|
|
constexpr unsigned MaxLen = 50;
|
|
|
|
|
|
|
|
// Only show the first line if there are many.
|
|
|
|
llvm::StringRef R = Code.split('\n').first;
|
|
|
|
// Shorten the message if it's too long.
|
|
|
|
R = R.take_front(MaxLen);
|
|
|
|
|
|
|
|
OS << R;
|
|
|
|
if (R.size() != Code.size())
|
|
|
|
OS << "…";
|
|
|
|
}
|
|
|
|
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
/// Fills \p D with all information, except the location-related bits.
|
|
|
|
/// Also note that ID and Name are not part of clangd::DiagBase and should be
|
|
|
|
/// set elsewhere.
|
|
|
|
static void fillNonLocationData(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const clang::Diagnostic &Info,
|
|
|
|
clangd::DiagBase &D) {
|
|
|
|
llvm::SmallString<64> Message;
|
|
|
|
Info.FormatDiagnostic(Message);
|
|
|
|
|
2020-01-29 03:23:46 +08:00
|
|
|
D.Message = std::string(Message.str());
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
D.Severity = DiagLevel;
|
|
|
|
D.Category = DiagnosticIDs::getCategoryNameFromID(
|
|
|
|
DiagnosticIDs::getCategoryNumberForDiag(Info.getID()))
|
|
|
|
.str();
|
|
|
|
}
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const clang::Diagnostic &Info) {
|
2020-08-12 00:48:22 +08:00
|
|
|
// If the diagnostic was generated for a different SourceManager, skip it.
|
|
|
|
// This happens when a module is imported and needs to be implicitly built.
|
|
|
|
// The compilation of that module will use the same StoreDiags, but different
|
|
|
|
// SourceManager.
|
|
|
|
if (OrigSrcMgr && Info.hasSourceManager() &&
|
|
|
|
OrigSrcMgr != &Info.getSourceManager()) {
|
|
|
|
IgnoreDiagnostics::log(DiagLevel, Info);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
|
2020-06-09 21:46:35 +08:00
|
|
|
bool OriginallyError =
|
|
|
|
Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError(
|
|
|
|
Info.getID());
|
2018-03-12 23:28:22 +08:00
|
|
|
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
if (Info.getLocation().isInvalid()) {
|
|
|
|
// Handle diagnostics coming from command-line arguments. The source manager
|
|
|
|
// is *not* available at this point, so we cannot use it.
|
2020-06-09 21:46:35 +08:00
|
|
|
if (!OriginallyError) {
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
IgnoreDiagnostics::log(DiagLevel, Info);
|
|
|
|
return; // non-errors add too much noise, do not show them.
|
|
|
|
}
|
|
|
|
|
|
|
|
flushLastDiag();
|
|
|
|
|
|
|
|
LastDiag = Diag();
|
2020-06-09 21:46:35 +08:00
|
|
|
LastDiagLoc.reset();
|
|
|
|
LastDiagOriginallyError = OriginallyError;
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
LastDiag->ID = Info.getID();
|
|
|
|
fillNonLocationData(DiagLevel, Info, *LastDiag);
|
|
|
|
LastDiag->InsideMainFile = true;
|
|
|
|
// Put it at the start of the main file, for a lack of a better place.
|
|
|
|
LastDiag->Range.start = Position{0, 0};
|
|
|
|
LastDiag->Range.end = Position{0, 0};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
if (!LangOpts || !Info.hasSourceManager()) {
|
|
|
|
IgnoreDiagnostics::log(DiagLevel, Info);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InsideMainFile = isInsideMainFile(Info);
|
2019-07-30 18:26:51 +08:00
|
|
|
SourceManager &SM = Info.getSourceManager();
|
2018-03-12 23:28:22 +08:00
|
|
|
|
|
|
|
auto FillDiagBase = [&](DiagBase &D) {
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
fillNonLocationData(DiagLevel, Info, D);
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
D.InsideMainFile = InsideMainFile;
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
D.Range = diagnosticRange(Info, *LangOpts);
|
2020-01-29 03:23:46 +08:00
|
|
|
D.File = std::string(SM.getFilename(Info.getLocation()));
|
2019-04-18 23:17:07 +08:00
|
|
|
D.AbsFile = getCanonicalPath(
|
|
|
|
SM.getFileEntryForID(SM.getFileID(Info.getLocation())), SM);
|
2020-06-09 21:46:35 +08:00
|
|
|
D.ID = Info.getID();
|
2018-03-12 23:28:22 +08:00
|
|
|
return D;
|
|
|
|
};
|
|
|
|
|
2018-04-04 01:35:57 +08:00
|
|
|
auto AddFix = [&](bool SyntheticMessage) -> bool {
|
2018-03-12 23:28:22 +08:00
|
|
|
assert(!Info.getFixItHints().empty() &&
|
|
|
|
"diagnostic does not have attached fix-its");
|
|
|
|
if (!InsideMainFile)
|
|
|
|
return false;
|
|
|
|
|
2020-04-17 07:38:42 +08:00
|
|
|
// Copy as we may modify the ranges.
|
|
|
|
auto FixIts = Info.getFixItHints().vec();
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::SmallVector<TextEdit, 1> Edits;
|
2020-04-17 07:38:42 +08:00
|
|
|
for (auto &FixIt : FixIts) {
|
|
|
|
// Allow fixits within a single macro-arg expansion to be applied.
|
|
|
|
// This can be incorrect if the argument is expanded multiple times in
|
|
|
|
// different contexts. Hopefully this is rare!
|
|
|
|
if (FixIt.RemoveRange.getBegin().isMacroID() &&
|
|
|
|
FixIt.RemoveRange.getEnd().isMacroID() &&
|
|
|
|
SM.getFileID(FixIt.RemoveRange.getBegin()) ==
|
|
|
|
SM.getFileID(FixIt.RemoveRange.getEnd())) {
|
|
|
|
FixIt.RemoveRange = CharSourceRange(
|
|
|
|
{SM.getTopMacroCallerLoc(FixIt.RemoveRange.getBegin()),
|
|
|
|
SM.getTopMacroCallerLoc(FixIt.RemoveRange.getEnd())},
|
|
|
|
FixIt.RemoveRange.isTokenRange());
|
|
|
|
}
|
|
|
|
// Otherwise, follow clang's behavior: no fixits in macros.
|
2019-02-22 17:43:56 +08:00
|
|
|
if (FixIt.RemoveRange.getBegin().isMacroID() ||
|
|
|
|
FixIt.RemoveRange.getEnd().isMacroID())
|
|
|
|
return false;
|
2019-07-30 18:26:51 +08:00
|
|
|
if (!isInsideMainFile(FixIt.RemoveRange.getBegin(), SM))
|
2018-03-12 23:28:22 +08:00
|
|
|
return false;
|
2019-07-30 18:26:51 +08:00
|
|
|
Edits.push_back(toTextEdit(FixIt, SM, *LangOpts));
|
2018-03-12 23:28:22 +08:00
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::SmallString<64> Message;
|
2018-04-04 01:35:57 +08:00
|
|
|
// If requested and possible, create a message like "change 'foo' to 'bar'".
|
2020-04-17 07:38:42 +08:00
|
|
|
if (SyntheticMessage && FixIts.size() == 1) {
|
|
|
|
const auto &FixIt = FixIts.front();
|
2018-04-04 01:35:57 +08:00
|
|
|
bool Invalid = false;
|
2019-07-30 18:26:51 +08:00
|
|
|
llvm::StringRef Remove =
|
|
|
|
Lexer::getSourceText(FixIt.RemoveRange, SM, *LangOpts, &Invalid);
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef Insert = FixIt.CodeToInsert;
|
2018-04-04 01:35:57 +08:00
|
|
|
if (!Invalid) {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::raw_svector_ostream M(Message);
|
2019-05-24 18:26:23 +08:00
|
|
|
if (!Remove.empty() && !Insert.empty()) {
|
|
|
|
M << "change '";
|
|
|
|
writeCodeToFixMessage(M, Remove);
|
|
|
|
M << "' to '";
|
|
|
|
writeCodeToFixMessage(M, Insert);
|
|
|
|
M << "'";
|
|
|
|
} else if (!Remove.empty()) {
|
|
|
|
M << "remove '";
|
|
|
|
writeCodeToFixMessage(M, Remove);
|
|
|
|
M << "'";
|
|
|
|
} else if (!Insert.empty()) {
|
|
|
|
M << "insert '";
|
|
|
|
writeCodeToFixMessage(M, Insert);
|
|
|
|
M << "'";
|
|
|
|
}
|
2018-04-04 01:35:57 +08:00
|
|
|
// Don't allow source code to inject newlines into diagnostics.
|
|
|
|
std::replace(Message.begin(), Message.end(), '\n', ' ');
|
|
|
|
}
|
|
|
|
}
|
2020-04-05 14:28:11 +08:00
|
|
|
if (Message.empty()) // either !SyntheticMessage, or we failed to make one.
|
2018-04-04 01:35:57 +08:00
|
|
|
Info.FormatDiagnostic(Message);
|
2020-01-29 03:23:46 +08:00
|
|
|
LastDiag->Fixes.push_back(
|
|
|
|
Fix{std::string(Message.str()), std::move(Edits)});
|
2018-03-12 23:28:22 +08:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!isNote(DiagLevel)) {
|
|
|
|
// Handle the new main diagnostic.
|
|
|
|
flushLastDiag();
|
|
|
|
|
2021-05-31 14:24:06 +08:00
|
|
|
LastDiag = Diag();
|
|
|
|
// FIXME: Merge with feature modules.
|
|
|
|
if (Adjuster)
|
2019-05-19 12:19:14 +08:00
|
|
|
DiagLevel = Adjuster(DiagLevel, Info);
|
2019-05-19 12:06:52 +08:00
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
FillDiagBase(*LastDiag);
|
2021-05-31 14:24:06 +08:00
|
|
|
if (isExcluded(LastDiag->ID))
|
|
|
|
LastDiag->Severity = DiagnosticsEngine::Ignored;
|
|
|
|
if (DiagCB)
|
|
|
|
DiagCB(Info, *LastDiag);
|
|
|
|
// Don't bother filling in the rest if diag is going to be dropped.
|
|
|
|
if (LastDiag->Severity == DiagnosticsEngine::Ignored)
|
|
|
|
return;
|
|
|
|
|
2020-06-09 21:46:35 +08:00
|
|
|
LastDiagLoc.emplace(Info.getLocation(), Info.getSourceManager());
|
|
|
|
LastDiagOriginallyError = OriginallyError;
|
2018-03-12 23:28:22 +08:00
|
|
|
if (!Info.getFixItHints().empty())
|
2018-04-04 01:35:57 +08:00
|
|
|
AddFix(true /* try to invent a message instead of repeating the diag */);
|
2019-01-28 22:01:55 +08:00
|
|
|
if (Fixer) {
|
2021-05-31 14:24:06 +08:00
|
|
|
auto ExtraFixes = Fixer(LastDiag->Severity, Info);
|
2019-01-28 22:01:55 +08:00
|
|
|
LastDiag->Fixes.insert(LastDiag->Fixes.end(), ExtraFixes.begin(),
|
|
|
|
ExtraFixes.end());
|
|
|
|
}
|
2018-03-12 23:28:22 +08:00
|
|
|
} else {
|
|
|
|
// Handle a note to an existing diagnostic.
|
|
|
|
if (!LastDiag) {
|
|
|
|
assert(false && "Adding a note without main diagnostic");
|
|
|
|
IgnoreDiagnostics::log(DiagLevel, Info);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-31 14:24:06 +08:00
|
|
|
// If a diagnostic was suppressed due to the suppression filter,
|
|
|
|
// also suppress notes associated with it.
|
|
|
|
if (LastDiag->Severity == DiagnosticsEngine::Ignored)
|
|
|
|
return;
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
if (!Info.getFixItHints().empty()) {
|
|
|
|
// A clang note with fix-it is not a separate diagnostic in clangd. We
|
|
|
|
// attach it as a Fix to the main diagnostic instead.
|
2018-04-04 01:35:57 +08:00
|
|
|
if (!AddFix(false /* use the note as the message */))
|
2018-03-12 23:28:22 +08:00
|
|
|
IgnoreDiagnostics::log(DiagLevel, Info);
|
|
|
|
} else {
|
|
|
|
// A clang note without fix-its corresponds to clangd::Note.
|
|
|
|
Note N;
|
|
|
|
FillDiagBase(N);
|
|
|
|
|
|
|
|
LastDiag->Notes.push_back(std::move(N));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StoreDiags::flushLastDiag() {
|
|
|
|
if (!LastDiag)
|
|
|
|
return;
|
2020-06-09 21:46:35 +08:00
|
|
|
auto Finish = llvm::make_scope_exit([&, NDiags(Output.size())] {
|
|
|
|
if (Output.size() == NDiags) // No new diag emitted.
|
|
|
|
vlog("Dropped diagnostic: {0}: {1}", LastDiag->File, LastDiag->Message);
|
|
|
|
LastDiag.reset();
|
|
|
|
});
|
|
|
|
|
2021-05-31 14:24:06 +08:00
|
|
|
if (LastDiag->Severity == DiagnosticsEngine::Ignored)
|
2020-06-09 21:46:35 +08:00
|
|
|
return;
|
|
|
|
// Move errors that occur from headers into main file.
|
|
|
|
if (!LastDiag->InsideMainFile && LastDiagLoc && LastDiagOriginallyError) {
|
|
|
|
if (tryMoveToMainFile(*LastDiag, *LastDiagLoc)) {
|
|
|
|
// Suppress multiple errors from the same inclusion.
|
|
|
|
if (!IncludedErrorLocations
|
|
|
|
.insert({LastDiag->Range.start.line,
|
|
|
|
LastDiag->Range.start.character})
|
|
|
|
.second)
|
|
|
|
return;
|
|
|
|
}
|
2019-04-29 18:25:44 +08:00
|
|
|
}
|
2020-06-09 21:46:35 +08:00
|
|
|
if (!mentionsMainFile(*LastDiag))
|
|
|
|
return;
|
|
|
|
Output.push_back(std::move(*LastDiag));
|
2018-03-12 23:28:22 +08:00
|
|
|
}
|
|
|
|
|
2021-01-25 21:03:13 +08:00
|
|
|
bool isBuiltinDiagnosticSuppressed(unsigned ID,
|
|
|
|
const llvm::StringSet<> &Suppress) {
|
|
|
|
if (const char *CodePtr = getDiagnosticCode(ID)) {
|
|
|
|
if (Suppress.contains(normalizeSuppressedCode(CodePtr)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
StringRef Warning = DiagnosticIDs::getWarningOptionForDiag(ID);
|
|
|
|
if (!Warning.empty() && Suppress.contains(Warning))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::StringRef normalizeSuppressedCode(llvm::StringRef Code) {
|
|
|
|
Code.consume_front("err_");
|
|
|
|
Code.consume_front("-W");
|
|
|
|
return Code;
|
|
|
|
}
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|