| 1 |
//===--- Diagnostics.cpp -----------------------------------------*- C++-*-===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "Diagnostics.h" |
| 10 |
#include "../clang-tidy/ClangTidyDiagnosticConsumer.h" |
| 11 |
#include "Compiler.h" |
| 12 |
#include "Protocol.h" |
| 13 |
#include "SourceCode.h" |
| 14 |
#include "support/Logger.h" |
| 15 |
#include "clang/Basic/AllDiagnostics.h" // IWYU pragma: keep |
| 16 |
#include "clang/Basic/Diagnostic.h" |
| 17 |
#include "clang/Basic/DiagnosticIDs.h" |
| 18 |
#include "clang/Basic/SourceLocation.h" |
| 19 |
#include "clang/Basic/SourceManager.h" |
| 20 |
#include "clang/Lex/Lexer.h" |
| 21 |
#include "clang/Lex/Token.h" |
| 22 |
#include "llvm/ADT/ArrayRef.h" |
| 23 |
#include "llvm/ADT/DenseSet.h" |
| 24 |
#include "llvm/ADT/STLExtras.h" |
| 25 |
#include "llvm/ADT/ScopeExit.h" |
| 26 |
#include "llvm/ADT/SmallString.h" |
| 27 |
#include "llvm/ADT/SmallVector.h" |
| 28 |
#include "llvm/ADT/StringRef.h" |
| 29 |
#include "llvm/ADT/Twine.h" |
| 30 |
#include "llvm/Support/Path.h" |
| 31 |
#include "llvm/Support/raw_ostream.h" |
| 32 |
#include <algorithm> |
| 33 |
#include <cassert> |
| 34 |
#include <cstddef> |
| 35 |
#include <optional> |
| 36 |
#include <vector> |
| 37 |
|
| 38 |
namespace clang { |
| 39 |
namespace clangd { |
| 40 |
namespace { |
| 41 |
|
| 42 |
const char *getDiagnosticCode(unsigned ID) { |
| 43 |
switch (ID) { |
| 44 |
#define DIAG(ENUM, CLASS, DEFAULT_MAPPING, DESC, GROPU, SFINAE, NOWERROR, \ |
| 45 |
SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY) \ |
| 46 |
case clang::diag::ENUM: \ |
| 47 |
return #ENUM; |
| 48 |
#include "clang/Basic/DiagnosticASTKinds.inc" |
| 49 |
#include "clang/Basic/DiagnosticAnalysisKinds.inc" |
| 50 |
#include "clang/Basic/DiagnosticCommentKinds.inc" |
| 51 |
#include "clang/Basic/DiagnosticCommonKinds.inc" |
| 52 |
#include "clang/Basic/DiagnosticDriverKinds.inc" |
| 53 |
#include "clang/Basic/DiagnosticFrontendKinds.inc" |
| 54 |
#include "clang/Basic/DiagnosticLexKinds.inc" |
| 55 |
#include "clang/Basic/DiagnosticParseKinds.inc" |
| 56 |
#include "clang/Basic/DiagnosticRefactoringKinds.inc" |
| 57 |
#include "clang/Basic/DiagnosticSemaKinds.inc" |
| 58 |
#include "clang/Basic/DiagnosticSerializationKinds.inc" |
| 59 |
#undef DIAG |
| 60 |
default: |
| 61 |
return nullptr; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
bool mentionsMainFile(const Diag &D) { |
| 66 |
if (D.InsideMainFile) |
| 67 |
return true; |
| 68 |
// Fixes are always in the main file. |
| 69 |
if (!D.Fixes.empty()) |
| 70 |
return true; |
| 71 |
for (auto &N : D.Notes) { |
| 72 |
if (N.InsideMainFile) |
| 73 |
return true; |
| 74 |
} |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
bool isExcluded(unsigned DiagID) { |
| 79 |
// clang will always fail parsing MS ASM, we don't link in desc + asm parser. |
| 80 |
if (DiagID == clang::diag::err_msasm_unable_to_create_target || |
| 81 |
DiagID == clang::diag::err_msasm_unsupported_arch) |
| 82 |
return true; |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
// Checks whether a location is within a half-open range. |
| 87 |
// Note that clang also uses closed source ranges, which this can't handle! |
| 88 |
bool locationInRange(SourceLocation L, CharSourceRange R, |
| 89 |
const SourceManager &M) { |
| 90 |
assert(R.isCharRange()); |
| 91 |
if (!R.isValid() || M.getFileID(R.getBegin()) != M.getFileID(R.getEnd()) || |
| 92 |
M.getFileID(R.getBegin()) != M.getFileID(L)) |
| 93 |
return false; |
| 94 |
return L != R.getEnd() && M.isPointWithin(L, R.getBegin(), R.getEnd()); |
| 95 |
} |
| 96 |
|
| 97 |
// Clang diags have a location (shown as ^) and 0 or more ranges (~~~~). |
| 98 |
// LSP needs a single range. |
| 99 |
Range diagnosticRange(const clang::Diagnostic &D, const LangOptions &L) { |
| 100 |
auto &M = D.getSourceManager(); |
| 101 |
auto Loc = M.getFileLoc(D.getLocation()); |
| 102 |
for (const auto &CR : D.getRanges()) { |
| 103 |
auto R = Lexer::makeFileCharRange(CR, M, L); |
| 104 |
if (locationInRange(Loc, R, M)) |
| 105 |
return halfOpenToRange(M, R); |
| 106 |
} |
| 107 |
// The range may be given as a fixit hint instead. |
| 108 |
for (const auto &F : D.getFixItHints()) { |
| 109 |
auto R = Lexer::makeFileCharRange(F.RemoveRange, M, L); |
| 110 |
if (locationInRange(Loc, R, M)) |
| 111 |
return halfOpenToRange(M, R); |
| 112 |
} |
| 113 |
// If the token at the location is not a comment, we use the token. |
| 114 |
// If we can't get the token at the location, fall back to using the location |
| 115 |
auto R = CharSourceRange::getCharRange(Loc); |
| 116 |
Token Tok; |
| 117 |
if (!Lexer::getRawToken(Loc, Tok, M, L, true) && Tok.isNot(tok::comment)) { |
| 118 |
R = CharSourceRange::getTokenRange(Tok.getLocation(), Tok.getEndLoc()); |
| 119 |
} |
| 120 |
return halfOpenToRange(M, R); |
| 121 |
} |
| 122 |
|
| 123 |
// Try to find a location in the main-file to report the diagnostic D. |
| 124 |
// Returns a description like "in included file", or nullptr on failure. |
| 125 |
const char *getMainFileRange(const Diag &D, const SourceManager &SM, |
| 126 |
SourceLocation DiagLoc, Range &R) { |
| 127 |
// Look for a note in the main file indicating template instantiation. |
| 128 |
for (const auto &N : D.Notes) { |
| 129 |
if (N.InsideMainFile) { |
| 130 |
switch (N.ID) { |
| 131 |
case diag::note_template_class_instantiation_was_here: |
| 132 |
case diag::note_template_class_explicit_specialization_was_here: |
| 133 |
case diag::note_template_class_instantiation_here: |
| 134 |
case diag::note_template_member_class_here: |
| 135 |
case diag::note_template_member_function_here: |
| 136 |
case diag::note_function_template_spec_here: |
| 137 |
case diag::note_template_static_data_member_def_here: |
| 138 |
case diag::note_template_variable_def_here: |
| 139 |
case diag::note_template_enum_def_here: |
| 140 |
case diag::note_template_nsdmi_here: |
| 141 |
case diag::note_template_type_alias_instantiation_here: |
| 142 |
case diag::note_template_exception_spec_instantiation_here: |
| 143 |
case diag::note_template_requirement_instantiation_here: |
| 144 |
case diag::note_evaluating_exception_spec_here: |
| 145 |
case diag::note_default_arg_instantiation_here: |
| 146 |
case diag::note_default_function_arg_instantiation_here: |
| 147 |
case diag::note_explicit_template_arg_substitution_here: |
| 148 |
case diag::note_function_template_deduction_instantiation_here: |
| 149 |
case diag::note_deduced_template_arg_substitution_here: |
| 150 |
case diag::note_prior_template_arg_substitution: |
| 151 |
case diag::note_template_default_arg_checking: |
| 152 |
case diag::note_concept_specialization_here: |
| 153 |
case diag::note_nested_requirement_here: |
| 154 |
case diag::note_checking_constraints_for_template_id_here: |
| 155 |
case diag::note_checking_constraints_for_var_spec_id_here: |
| 156 |
case diag::note_checking_constraints_for_class_spec_id_here: |
| 157 |
case diag::note_checking_constraints_for_function_here: |
| 158 |
case diag::note_constraint_substitution_here: |
| 159 |
case diag::note_constraint_normalization_here: |
| 160 |
case diag::note_parameter_mapping_substitution_here: |
| 161 |
R = N.Range; |
| 162 |
return "in template"; |
| 163 |
default: |
| 164 |
break; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
// Look for where the file with the error was #included. |
| 169 |
auto GetIncludeLoc = [&SM](SourceLocation SLoc) { |
| 170 |
return SM.getIncludeLoc(SM.getFileID(SLoc)); |
| 171 |
}; |
| 172 |
for (auto IncludeLocation = GetIncludeLoc(SM.getExpansionLoc(DiagLoc)); |
| 173 |
IncludeLocation.isValid(); |
| 174 |
IncludeLocation = GetIncludeLoc(IncludeLocation)) { |
| 175 |
if (clangd::isInsideMainFile(IncludeLocation, SM)) { |
| 176 |
R.start = sourceLocToPosition(SM, IncludeLocation); |
| 177 |
R.end = sourceLocToPosition( |
| 178 |
SM, |
| 179 |
Lexer::getLocForEndOfToken(IncludeLocation, 0, SM, LangOptions())); |
| 180 |
return "in included file"; |
| 181 |
} |
| 182 |
} |
| 183 |
return nullptr; |
| 184 |
} |
| 185 |
|
| 186 |
// Place the diagnostic the main file, rather than the header, if possible: |
| 187 |
// - for errors in included files, use the #include location |
| 188 |
// - for errors in template instantiation, use the instantiation location |
| 189 |
// In both cases, add the original header location as a note. |
| 190 |
bool tryMoveToMainFile(Diag &D, FullSourceLoc DiagLoc) { |
| 191 |
const SourceManager &SM = DiagLoc.getManager(); |
| 192 |
DiagLoc = DiagLoc.getExpansionLoc(); |
| 193 |
Range R; |
| 194 |
const char *Prefix = getMainFileRange(D, SM, DiagLoc, R); |
| 195 |
if (!Prefix) |
| 196 |
return false; |
| 197 |
|
| 198 |
// Add a note that will point to real diagnostic. |
| 199 |
auto FE = *SM.getFileEntryRefForID(SM.getFileID(DiagLoc)); |
| 200 |
D.Notes.emplace(D.Notes.begin()); |
| 201 |
Note &N = D.Notes.front(); |
| 202 |
N.AbsFile = std::string(FE.getFileEntry().tryGetRealPathName()); |
| 203 |
N.File = std::string(FE.getName()); |
| 204 |
N.Message = "error occurred here"; |
| 205 |
N.Range = D.Range; |
| 206 |
|
| 207 |
// Update diag to point at include inside main file. |
| 208 |
D.File = SM.getFileEntryRefForID(SM.getMainFileID())->getName().str(); |
| 209 |
D.Range = std::move(R); |
| 210 |
D.InsideMainFile = true; |
| 211 |
// Update message to mention original file. |
| 212 |
D.Message = llvm::formatv("{0}: {1}", Prefix, D.Message); |
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
bool isInsideMainFile(const clang::Diagnostic &D) { |
| 217 |
if (!D.hasSourceManager()) |
| 218 |
return false; |
| 219 |
|
| 220 |
return clangd::isInsideMainFile(D.getLocation(), D.getSourceManager()); |
| 221 |
} |
| 222 |
|
| 223 |
bool isNote(DiagnosticsEngine::Level L) { |
| 224 |
return L == DiagnosticsEngine::Note || L == DiagnosticsEngine::Remark; |
| 225 |
} |
| 226 |
|
| 227 |
llvm::StringRef diagLeveltoString(DiagnosticsEngine::Level Lvl) { |
| 228 |
switch (Lvl) { |
| 229 |
case DiagnosticsEngine::Ignored: |
| 230 |
return "ignored"; |
| 231 |
case DiagnosticsEngine::Note: |
| 232 |
return "note"; |
| 233 |
case DiagnosticsEngine::Remark: |
| 234 |
return "remark"; |
| 235 |
case DiagnosticsEngine::Warning: |
| 236 |
return "warning"; |
| 237 |
case DiagnosticsEngine::Error: |
| 238 |
return "error"; |
| 239 |
case DiagnosticsEngine::Fatal: |
| 240 |
return "fatal error"; |
| 241 |
} |
| 242 |
llvm_unreachable("unhandled DiagnosticsEngine::Level"); |
| 243 |
} |
| 244 |
|
| 245 |
/// Prints a single diagnostic in a clang-like manner, the output includes |
| 246 |
/// location, severity and error message. An example of the output message is: |
| 247 |
/// |
| 248 |
/// main.cpp:12:23: error: undeclared identifier |
| 249 |
/// |
| 250 |
/// For main file we only print the basename and for all other files we print |
| 251 |
/// the filename on a separate line to provide a slightly more readable output |
| 252 |
/// in the editors: |
| 253 |
/// |
| 254 |
/// dir1/dir2/dir3/../../dir4/header.h:12:23 |
| 255 |
/// error: undeclared identifier |
| 256 |
void printDiag(llvm::raw_string_ostream &OS, const DiagBase &D) { |
| 257 |
if (D.InsideMainFile) { |
| 258 |
// Paths to main files are often taken from compile_command.json, where they |
| 259 |
// are typically absolute. To reduce noise we print only basename for them, |
| 260 |
// it should not be confusing and saves space. |
| 261 |
OS << llvm::sys::path::filename(D.File) << ":"; |
| 262 |
} else { |
| 263 |
OS << D.File << ":"; |
| 264 |
} |
| 265 |
// Note +1 to line and character. clangd::Range is zero-based, but when |
| 266 |
// printing for users we want one-based indexes. |
| 267 |
auto Pos = D.Range.start; |
| 268 |
OS << (Pos.line + 1) << ":" << (Pos.character + 1) << ":"; |
| 269 |
// The non-main-file paths are often too long, putting them on a separate |
| 270 |
// line improves readability. |
| 271 |
if (D.InsideMainFile) |
| 272 |
OS << " "; |
| 273 |
else |
| 274 |
OS << "\n"; |
| 275 |
OS << diagLeveltoString(D.Severity) << ": " << D.Message; |
| 276 |
} |
| 277 |
|
| 278 |
/// Capitalizes the first word in the diagnostic's message. |
| 279 |
std::string capitalize(std::string Message) { |
| 280 |
if (!Message.empty()) |
| 281 |
Message[0] = llvm::toUpper(Message[0]); |
| 282 |
return Message; |
| 283 |
} |
| 284 |
|
| 285 |
/// Returns a message sent to LSP for the main diagnostic in \p D. |
| 286 |
/// This message may include notes, if they're not emitted in some other way. |
| 287 |
/// Example output: |
| 288 |
/// |
| 289 |
/// no matching function for call to 'foo' |
| 290 |
/// |
| 291 |
/// main.cpp:3:5: note: candidate function not viable: requires 2 arguments |
| 292 |
/// |
| 293 |
/// dir1/dir2/dir3/../../dir4/header.h:12:23 |
| 294 |
/// note: candidate function not viable: requires 3 arguments |
| 295 |
std::string mainMessage(const Diag &D, const ClangdDiagnosticOptions &Opts) { |
| 296 |
std::string Result; |
| 297 |
llvm::raw_string_ostream OS(Result); |
| 298 |
OS << D.Message; |
| 299 |
if (Opts.DisplayFixesCount && !D.Fixes.empty()) |
| 300 |
OS << " (" << (D.Fixes.size() > 1 ? "fixes" : "fix") << " available)"; |
| 301 |
// If notes aren't emitted as structured info, add them to the message. |
| 302 |
if (!Opts.EmitRelatedLocations) |
| 303 |
for (auto &Note : D.Notes) { |
| 304 |
OS << "\n\n"; |
| 305 |
printDiag(OS, Note); |
| 306 |
} |
| 307 |
OS.flush(); |
| 308 |
return capitalize(std::move(Result)); |
| 309 |
} |
| 310 |
|
| 311 |
/// Returns a message sent to LSP for the note of the main diagnostic. |
| 312 |
std::string noteMessage(const Diag &Main, const DiagBase &Note, |
| 313 |
const ClangdDiagnosticOptions &Opts) { |
| 314 |
std::string Result; |
| 315 |
llvm::raw_string_ostream OS(Result); |
| 316 |
OS << Note.Message; |
| 317 |
// If the client doesn't support structured links between the note and the |
| 318 |
// original diagnostic, then emit the main diagnostic to give context. |
| 319 |
if (!Opts.EmitRelatedLocations) { |
| 320 |
OS << "\n\n"; |
| 321 |
printDiag(OS, Main); |
| 322 |
} |
| 323 |
OS.flush(); |
| 324 |
return capitalize(std::move(Result)); |
| 325 |
} |
| 326 |
|
| 327 |
void setTags(clangd::Diag &D) { |
| 328 |
static const auto *DeprecatedDiags = new llvm::DenseSet<unsigned>{ |
| 329 |
diag::warn_access_decl_deprecated, |
| 330 |
diag::warn_atl_uuid_deprecated, |
| 331 |
diag::warn_deprecated, |
| 332 |
diag::warn_deprecated_altivec_src_compat, |
| 333 |
diag::warn_deprecated_comma_subscript, |
| 334 |
diag::warn_deprecated_copy, |
| 335 |
diag::warn_deprecated_copy_with_dtor, |
| 336 |
diag::warn_deprecated_copy_with_user_provided_copy, |
| 337 |
diag::warn_deprecated_copy_with_user_provided_dtor, |
| 338 |
diag::warn_deprecated_def, |
| 339 |
diag::warn_deprecated_increment_decrement_volatile, |
| 340 |
diag::warn_deprecated_message, |
| 341 |
diag::warn_deprecated_redundant_constexpr_static_def, |
| 342 |
diag::warn_deprecated_register, |
| 343 |
diag::warn_deprecated_simple_assign_volatile, |
| 344 |
diag::warn_deprecated_string_literal_conversion, |
| 345 |
diag::warn_deprecated_this_capture, |
| 346 |
diag::warn_deprecated_volatile_param, |
| 347 |
diag::warn_deprecated_volatile_return, |
| 348 |
diag::warn_deprecated_volatile_structured_binding, |
| 349 |
diag::warn_opencl_attr_deprecated_ignored, |
| 350 |
diag::warn_property_method_deprecated, |
| 351 |
diag::warn_vector_mode_deprecated, |
| 352 |
}; |
| 353 |
static const auto *UnusedDiags = new llvm::DenseSet<unsigned>{ |
| 354 |
diag::warn_opencl_attr_deprecated_ignored, |
| 355 |
diag::warn_pragma_attribute_unused, |
| 356 |
diag::warn_unused_but_set_parameter, |
| 357 |
diag::warn_unused_but_set_variable, |
| 358 |
diag::warn_unused_comparison, |
| 359 |
diag::warn_unused_const_variable, |
| 360 |
diag::warn_unused_exception_param, |
| 361 |
diag::warn_unused_function, |
| 362 |
diag::warn_unused_label, |
| 363 |
diag::warn_unused_lambda_capture, |
| 364 |
diag::warn_unused_local_typedef, |
| 365 |
diag::warn_unused_member_function, |
| 366 |
diag::warn_unused_parameter, |
| 367 |
diag::warn_unused_private_field, |
| 368 |
diag::warn_unused_property_backing_ivar, |
| 369 |
diag::warn_unused_template, |
| 370 |
diag::warn_unused_variable, |
| 371 |
}; |
| 372 |
if (DeprecatedDiags->contains(D.ID)) { |
| 373 |
D.Tags.push_back(DiagnosticTag::Deprecated); |
| 374 |
} else if (UnusedDiags->contains(D.ID)) { |
| 375 |
D.Tags.push_back(DiagnosticTag::Unnecessary); |
| 376 |
} |
| 377 |
if (D.Source == Diag::ClangTidy) { |
| 378 |
if (llvm::StringRef(D.Name).starts_with("misc-unused-")) |
| 379 |
D.Tags.push_back(DiagnosticTag::Unnecessary); |
| 380 |
if (llvm::StringRef(D.Name).starts_with("modernize-")) |
| 381 |
D.Tags.push_back(DiagnosticTag::Deprecated); |
| 382 |
} |
| 383 |
} |
| 384 |
} // namespace |
| 385 |
|
| 386 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DiagBase &D) { |
| 387 |
OS << "["; |
| 388 |
if (!D.InsideMainFile) |
| 389 |
OS << D.File << ":"; |
| 390 |
OS << D.Range.start << "-" << D.Range.end << "] "; |
| 391 |
|
| 392 |
return OS << D.Message; |
| 393 |
} |
| 394 |
|
| 395 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Fix &F) { |
| 396 |
OS << F.Message << " {"; |
| 397 |
const char *Sep = ""; |
| 398 |
for (const auto &Edit : F.Edits) { |
| 399 |
OS << Sep << Edit; |
| 400 |
Sep = ", "; |
| 401 |
} |
| 402 |
return OS << "}"; |
| 403 |
} |
| 404 |
|
| 405 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diag &D) { |
| 406 |
OS << static_cast<const DiagBase &>(D); |
| 407 |
if (!D.Notes.empty()) { |
| 408 |
OS << ", notes: {"; |
| 409 |
const char *Sep = ""; |
| 410 |
for (auto &Note : D.Notes) { |
| 411 |
OS << Sep << Note; |
| 412 |
Sep = ", "; |
| 413 |
} |
| 414 |
OS << "}"; |
| 415 |
} |
| 416 |
if (!D.Fixes.empty()) { |
| 417 |
OS << ", fixes: {"; |
| 418 |
const char *Sep = ""; |
| 419 |
for (auto &Fix : D.Fixes) { |
| 420 |
OS << Sep << Fix; |
| 421 |
Sep = ", "; |
| 422 |
} |
| 423 |
OS << "}"; |
| 424 |
} |
| 425 |
return OS; |
| 426 |
} |
| 427 |
|
| 428 |
CodeAction toCodeAction(const Fix &F, const URIForFile &File) { |
| 429 |
CodeAction Action; |
| 430 |
Action.title = F.Message; |
| 431 |
Action.kind = std::string(CodeAction::QUICKFIX_KIND); |
| 432 |
Action.edit.emplace(); |
| 433 |
Action.edit->changes[File.uri()] = {F.Edits.begin(), F.Edits.end()}; |
| 434 |
return Action; |
| 435 |
} |
| 436 |
|
| 437 |
Diag toDiag(const llvm::SMDiagnostic &D, Diag::DiagSource Source) { |
| 438 |
Diag Result; |
| 439 |
Result.Message = D.getMessage().str(); |
| 440 |
switch (D.getKind()) { |
| 441 |
case llvm::SourceMgr::DK_Error: |
| 442 |
Result.Severity = DiagnosticsEngine::Error; |
| 443 |
break; |
| 444 |
case llvm::SourceMgr::DK_Warning: |
| 445 |
Result.Severity = DiagnosticsEngine::Warning; |
| 446 |
break; |
| 447 |
default: |
| 448 |
break; |
| 449 |
} |
| 450 |
Result.Source = Source; |
| 451 |
Result.AbsFile = D.getFilename().str(); |
| 452 |
Result.InsideMainFile = D.getSourceMgr()->FindBufferContainingLoc( |
| 453 |
D.getLoc()) == D.getSourceMgr()->getMainFileID(); |
| 454 |
if (D.getRanges().empty()) |
| 455 |
Result.Range = {{D.getLineNo() - 1, D.getColumnNo()}, |
| 456 |
{D.getLineNo() - 1, D.getColumnNo()}}; |
| 457 |
else |
| 458 |
Result.Range = {{D.getLineNo() - 1, (int)D.getRanges().front().first}, |
| 459 |
{D.getLineNo() - 1, (int)D.getRanges().front().second}}; |
| 460 |
return Result; |
| 461 |
} |
| 462 |
|
| 463 |
void toLSPDiags( |
| 464 |
const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts, |
| 465 |
llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn) { |
| 466 |
clangd::Diagnostic Main; |
| 467 |
Main.severity = getSeverity(D.Severity); |
| 468 |
|
| 469 |
// Main diagnostic should always refer to a range inside main file. If a |
| 470 |
// diagnostic made it so for, it means either itself or one of its notes is |
| 471 |
// inside main file. It's also possible that there's a fix in the main file, |
| 472 |
// but we preserve fixes iff primary diagnostic is in the main file. |
| 473 |
if (D.InsideMainFile) { |
| 474 |
Main.range = D.Range; |
| 475 |
} else { |
| 476 |
auto It = |
| 477 |
llvm::find_if(D.Notes, [](const Note &N) { return N.InsideMainFile; }); |
| 478 |
assert(It != D.Notes.end() && |
| 479 |
"neither the main diagnostic nor notes are inside main file"); |
| 480 |
Main.range = It->Range; |
| 481 |
} |
| 482 |
|
| 483 |
Main.code = D.Name; |
| 484 |
if (auto URI = getDiagnosticDocURI(D.Source, D.ID, D.Name)) { |
| 485 |
Main.codeDescription.emplace(); |
| 486 |
Main.codeDescription->href = std::move(*URI); |
| 487 |
} |
| 488 |
switch (D.Source) { |
| 489 |
case Diag::Clang: |
| 490 |
Main.source = "clang"; |
| 491 |
break; |
| 492 |
case Diag::ClangTidy: |
| 493 |
Main.source = "clang-tidy"; |
| 494 |
break; |
| 495 |
case Diag::Clangd: |
| 496 |
Main.source = "clangd"; |
| 497 |
break; |
| 498 |
case Diag::ClangdConfig: |
| 499 |
Main.source = "clangd-config"; |
| 500 |
break; |
| 501 |
case Diag::Unknown: |
| 502 |
break; |
| 503 |
} |
| 504 |
if (Opts.EmbedFixesInDiagnostics) { |
| 505 |
Main.codeActions.emplace(); |
| 506 |
for (const auto &Fix : D.Fixes) |
| 507 |
Main.codeActions->push_back(toCodeAction(Fix, File)); |
| 508 |
if (Main.codeActions->size() == 1) |
| 509 |
Main.codeActions->front().isPreferred = true; |
| 510 |
} |
| 511 |
if (Opts.SendDiagnosticCategory && !D.Category.empty()) |
| 512 |
Main.category = D.Category; |
| 513 |
|
| 514 |
Main.message = mainMessage(D, Opts); |
| 515 |
if (Opts.EmitRelatedLocations) { |
| 516 |
Main.relatedInformation.emplace(); |
| 517 |
for (auto &Note : D.Notes) { |
| 518 |
if (!Note.AbsFile) { |
| 519 |
vlog("Dropping note from unknown file: {0}", Note); |
| 520 |
continue; |
| 521 |
} |
| 522 |
DiagnosticRelatedInformation RelInfo; |
| 523 |
RelInfo.location.range = Note.Range; |
| 524 |
RelInfo.location.uri = |
| 525 |
URIForFile::canonicalize(*Note.AbsFile, File.file()); |
| 526 |
RelInfo.message = noteMessage(D, Note, Opts); |
| 527 |
Main.relatedInformation->push_back(std::move(RelInfo)); |
| 528 |
} |
| 529 |
} |
| 530 |
Main.tags = D.Tags; |
| 531 |
OutFn(std::move(Main), D.Fixes); |
| 532 |
|
| 533 |
// If we didn't emit the notes as relatedLocations, emit separate diagnostics |
| 534 |
// so the user can find the locations easily. |
| 535 |
if (!Opts.EmitRelatedLocations) |
| 536 |
for (auto &Note : D.Notes) { |
| 537 |
if (!Note.InsideMainFile) |
| 538 |
continue; |
| 539 |
clangd::Diagnostic Res; |
| 540 |
Res.severity = getSeverity(Note.Severity); |
| 541 |
Res.range = Note.Range; |
| 542 |
Res.message = noteMessage(D, Note, Opts); |
| 543 |
OutFn(std::move(Res), llvm::ArrayRef<Fix>()); |
| 544 |
} |
| 545 |
|
| 546 |
// FIXME: Get rid of the copies here by taking in a mutable clangd::Diag. |
| 547 |
for (auto &Entry : D.OpaqueData) |
| 548 |
Main.data.insert({Entry.first, Entry.second}); |
| 549 |
} |
| 550 |
|
| 551 |
int getSeverity(DiagnosticsEngine::Level L) { |
| 552 |
switch (L) { |
| 553 |
case DiagnosticsEngine::Remark: |
| 554 |
return 4; |
| 555 |
case DiagnosticsEngine::Note: |
| 556 |
return 3; |
| 557 |
case DiagnosticsEngine::Warning: |
| 558 |
return 2; |
| 559 |
case DiagnosticsEngine::Fatal: |
| 560 |
case DiagnosticsEngine::Error: |
| 561 |
return 1; |
| 562 |
case DiagnosticsEngine::Ignored: |
| 563 |
return 0; |
| 564 |
} |
| 565 |
llvm_unreachable("Unknown diagnostic level!"); |
| 566 |
} |
| 567 |
|
| 568 |
std::vector<Diag> StoreDiags::take(const clang::tidy::ClangTidyContext *Tidy) { |
| 569 |
// Do not forget to emit a pending diagnostic if there is one. |
| 570 |
flushLastDiag(); |
| 571 |
|
| 572 |
// Fill in name/source now that we have all the context needed to map them. |
| 573 |
for (auto &Diag : Output) { |
| 574 |
if (const char *ClangDiag = getDiagnosticCode(Diag.ID)) { |
| 575 |
// Warnings controlled by -Wfoo are better recognized by that name. |
| 576 |
StringRef Warning = DiagnosticIDs::getWarningOptionForDiag(Diag.ID); |
| 577 |
if (!Warning.empty()) { |
| 578 |
Diag.Name = ("-W" + Warning).str(); |
| 579 |
} else { |
| 580 |
StringRef Name(ClangDiag); |
| 581 |
// Almost always an error, with a name like err_enum_class_reference. |
| 582 |
// Drop the err_ prefix for brevity. |
| 583 |
Name.consume_front("err_"); |
| 584 |
Diag.Name = std::string(Name); |
| 585 |
} |
| 586 |
Diag.Source = Diag::Clang; |
| 587 |
} else if (Tidy != nullptr) { |
| 588 |
std::string TidyDiag = Tidy->getCheckName(Diag.ID); |
| 589 |
if (!TidyDiag.empty()) { |
| 590 |
Diag.Name = std::move(TidyDiag); |
| 591 |
Diag.Source = Diag::ClangTidy; |
| 592 |
// clang-tidy bakes the name into diagnostic messages. Strip it out. |
| 593 |
// It would be much nicer to make clang-tidy not do this. |
| 594 |
auto CleanMessage = [&](std::string &Msg) { |
| 595 |
StringRef Rest(Msg); |
| 596 |
if (Rest.consume_back("]") && Rest.consume_back(Diag.Name) && |
| 597 |
Rest.consume_back(" [")) |
| 598 |
Msg.resize(Rest.size()); |
| 599 |
}; |
| 600 |
CleanMessage(Diag.Message); |
| 601 |
for (auto &Note : Diag.Notes) |
| 602 |
CleanMessage(Note.Message); |
| 603 |
for (auto &Fix : Diag.Fixes) |
| 604 |
CleanMessage(Fix.Message); |
| 605 |
} |
| 606 |
} |
| 607 |
setTags(Diag); |
| 608 |
} |
| 609 |
// Deduplicate clang-tidy diagnostics -- some clang-tidy checks may emit |
| 610 |
// duplicated messages due to various reasons (e.g. the check doesn't handle |
| 611 |
// template instantiations well; clang-tidy alias checks). |
| 612 |
std::set<std::pair<Range, std::string>> SeenDiags; |
| 613 |
llvm::erase_if(Output, [&](const Diag &D) { |
| 614 |
return !SeenDiags.emplace(D.Range, D.Message).second; |
| 615 |
}); |
| 616 |
return std::move(Output); |
| 617 |
} |
| 618 |
|
| 619 |
void StoreDiags::BeginSourceFile(const LangOptions &Opts, |
| 620 |
const Preprocessor *PP) { |
| 621 |
LangOpts = Opts; |
| 622 |
if (PP) { |
| 623 |
OrigSrcMgr = &PP->getSourceManager(); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
void StoreDiags::EndSourceFile() { |
| 628 |
flushLastDiag(); |
| 629 |
LangOpts = std::nullopt; |
| 630 |
OrigSrcMgr = nullptr; |
| 631 |
} |
| 632 |
|
| 633 |
/// Sanitizes a piece for presenting it in a synthesized fix message. Ensures |
| 634 |
/// the result is not too large and does not contain newlines. |
| 635 |
static void writeCodeToFixMessage(llvm::raw_ostream &OS, llvm::StringRef Code) { |
| 636 |
constexpr unsigned MaxLen = 50; |
| 637 |
if (Code == "\n") { |
| 638 |
OS << "\\n"; |
| 639 |
return; |
| 640 |
} |
| 641 |
// Only show the first line if there are many. |
| 642 |
llvm::StringRef R = Code.split('\n').first; |
| 643 |
// Shorten the message if it's too long. |
| 644 |
R = R.take_front(MaxLen); |
| 645 |
|
| 646 |
OS << R; |
| 647 |
if (R.size() != Code.size()) |
| 648 |
OS << "…"; |
| 649 |
} |
| 650 |
|
| 651 |
/// Fills \p D with all information, except the location-related bits. |
| 652 |
/// Also note that ID and Name are not part of clangd::DiagBase and should be |
| 653 |
/// set elsewhere. |
| 654 |
static void fillNonLocationData(DiagnosticsEngine::Level DiagLevel, |
| 655 |
const clang::Diagnostic &Info, |
| 656 |
clangd::DiagBase &D) { |
| 657 |
llvm::SmallString<64> Message; |
| 658 |
Info.FormatDiagnostic(Message); |
| 659 |
|
| 660 |
D.Message = std::string(Message.str()); |
| 661 |
D.Severity = DiagLevel; |
| 662 |
D.Category = DiagnosticIDs::getCategoryNameFromID( |
| 663 |
DiagnosticIDs::getCategoryNumberForDiag(Info.getID())) |
| 664 |
.str(); |
| 665 |
} |
| 666 |
|
| 667 |
void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 668 |
const clang::Diagnostic &Info) { |
| 669 |
// If the diagnostic was generated for a different SourceManager, skip it. |
| 670 |
// This happens when a module is imported and needs to be implicitly built. |
| 671 |
// The compilation of that module will use the same StoreDiags, but different |
| 672 |
// SourceManager. |
| 673 |
if (OrigSrcMgr && Info.hasSourceManager() && |
| 674 |
OrigSrcMgr != &Info.getSourceManager()) { |
| 675 |
IgnoreDiagnostics::log(DiagLevel, Info); |
| 676 |
return; |
| 677 |
} |
| 678 |
|
| 679 |
DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info); |
| 680 |
bool OriginallyError = |
| 681 |
Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError( |
| 682 |
Info.getID()); |
| 683 |
|
| 684 |
if (Info.getLocation().isInvalid()) { |
| 685 |
// Handle diagnostics coming from command-line arguments. The source manager |
| 686 |
// is *not* available at this point, so we cannot use it. |
| 687 |
if (!OriginallyError) { |
| 688 |
IgnoreDiagnostics::log(DiagLevel, Info); |
| 689 |
return; // non-errors add too much noise, do not show them. |
| 690 |
} |
| 691 |
|
| 692 |
flushLastDiag(); |
| 693 |
|
| 694 |
LastDiag = Diag(); |
| 695 |
LastDiagLoc.reset(); |
| 696 |
LastDiagOriginallyError = OriginallyError; |
| 697 |
LastDiag->ID = Info.getID(); |
| 698 |
fillNonLocationData(DiagLevel, Info, *LastDiag); |
| 699 |
LastDiag->InsideMainFile = true; |
| 700 |
// Put it at the start of the main file, for a lack of a better place. |
| 701 |
LastDiag->Range.start = Position{0, 0}; |
| 702 |
LastDiag->Range.end = Position{0, 0}; |
| 703 |
return; |
| 704 |
} |
| 705 |
|
| 706 |
if (!LangOpts || !Info.hasSourceManager()) { |
| 707 |
IgnoreDiagnostics::log(DiagLevel, Info); |
| 708 |
return; |
| 709 |
} |
| 710 |
|
| 711 |
SourceManager &SM = Info.getSourceManager(); |
| 712 |
|
| 713 |
auto FillDiagBase = [&](DiagBase &D) { |
| 714 |
fillNonLocationData(DiagLevel, Info, D); |
| 715 |
|
| 716 |
D.InsideMainFile = isInsideMainFile(Info); |
| 717 |
D.Range = diagnosticRange(Info, *LangOpts); |
| 718 |
D.File = std::string(SM.getFilename(Info.getLocation())); |
| 719 |
D.AbsFile = getCanonicalPath( |
| 720 |
SM.getFileEntryForID(SM.getFileID(Info.getLocation())), SM); |
| 721 |
D.ID = Info.getID(); |
| 722 |
return D; |
| 723 |
}; |
| 724 |
|
| 725 |
auto AddFix = [&](bool SyntheticMessage) -> bool { |
| 726 |
assert(!Info.getFixItHints().empty() && |
| 727 |
"diagnostic does not have attached fix-its"); |
| 728 |
// No point in generating fixes, if the diagnostic is for a different file. |
| 729 |
if (!LastDiag->InsideMainFile) |
| 730 |
return false; |
| 731 |
// Copy as we may modify the ranges. |
| 732 |
auto FixIts = Info.getFixItHints().vec(); |
| 733 |
llvm::SmallVector<TextEdit, 1> Edits; |
| 734 |
for (auto &FixIt : FixIts) { |
| 735 |
// Allow fixits within a single macro-arg expansion to be applied. |
| 736 |
// This can be incorrect if the argument is expanded multiple times in |
| 737 |
// different contexts. Hopefully this is rare! |
| 738 |
if (FixIt.RemoveRange.getBegin().isMacroID() && |
| 739 |
FixIt.RemoveRange.getEnd().isMacroID() && |
| 740 |
SM.getFileID(FixIt.RemoveRange.getBegin()) == |
| 741 |
SM.getFileID(FixIt.RemoveRange.getEnd())) { |
| 742 |
FixIt.RemoveRange = CharSourceRange( |
| 743 |
{SM.getTopMacroCallerLoc(FixIt.RemoveRange.getBegin()), |
| 744 |
SM.getTopMacroCallerLoc(FixIt.RemoveRange.getEnd())}, |
| 745 |
FixIt.RemoveRange.isTokenRange()); |
| 746 |
} |
| 747 |
// Otherwise, follow clang's behavior: no fixits in macros. |
| 748 |
if (FixIt.RemoveRange.getBegin().isMacroID() || |
| 749 |
FixIt.RemoveRange.getEnd().isMacroID()) |
| 750 |
return false; |
| 751 |
if (!isInsideMainFile(FixIt.RemoveRange.getBegin(), SM)) |
| 752 |
return false; |
| 753 |
Edits.push_back(toTextEdit(FixIt, SM, *LangOpts)); |
| 754 |
} |
| 755 |
|
| 756 |
llvm::SmallString<64> Message; |
| 757 |
// If requested and possible, create a message like "change 'foo' to 'bar'". |
| 758 |
if (SyntheticMessage && FixIts.size() == 1) { |
| 759 |
const auto &FixIt = FixIts.front(); |
| 760 |
bool Invalid = false; |
| 761 |
llvm::StringRef Remove = |
| 762 |
Lexer::getSourceText(FixIt.RemoveRange, SM, *LangOpts, &Invalid); |
| 763 |
llvm::StringRef Insert = FixIt.CodeToInsert; |
| 764 |
if (!Invalid) { |
| 765 |
llvm::raw_svector_ostream M(Message); |
| 766 |
if (!Remove.empty() && !Insert.empty()) { |
| 767 |
M << "change '"; |
| 768 |
writeCodeToFixMessage(M, Remove); |
| 769 |
M << "' to '"; |
| 770 |
writeCodeToFixMessage(M, Insert); |
| 771 |
M << "'"; |
| 772 |
} else if (!Remove.empty()) { |
| 773 |
M << "remove '"; |
| 774 |
writeCodeToFixMessage(M, Remove); |
| 775 |
M << "'"; |
| 776 |
} else if (!Insert.empty()) { |
| 777 |
M << "insert '"; |
| 778 |
writeCodeToFixMessage(M, Insert); |
| 779 |
M << "'"; |
| 780 |
} |
| 781 |
// Don't allow source code to inject newlines into diagnostics. |
| 782 |
std::replace(Message.begin(), Message.end(), '\n', ' '); |
| 783 |
} |
| 784 |
} |
| 785 |
if (Message.empty()) // either !SyntheticMessage, or we failed to make one. |
| 786 |
Info.FormatDiagnostic(Message); |
| 787 |
LastDiag->Fixes.push_back( |
| 788 |
Fix{std::string(Message.str()), std::move(Edits)}); |
| 789 |
return true; |
| 790 |
}; |
| 791 |
|
| 792 |
if (!isNote(DiagLevel)) { |
| 793 |
// Handle the new main diagnostic. |
| 794 |
flushLastDiag(); |
| 795 |
|
| 796 |
LastDiag = Diag(); |
| 797 |
// FIXME: Merge with feature modules. |
| 798 |
if (Adjuster) |
| 799 |
DiagLevel = Adjuster(DiagLevel, Info); |
| 800 |
|
| 801 |
FillDiagBase(*LastDiag); |
| 802 |
if (isExcluded(LastDiag->ID)) |
| 803 |
LastDiag->Severity = DiagnosticsEngine::Ignored; |
| 804 |
if (DiagCB) |
| 805 |
DiagCB(Info, *LastDiag); |
| 806 |
// Don't bother filling in the rest if diag is going to be dropped. |
| 807 |
if (LastDiag->Severity == DiagnosticsEngine::Ignored) |
| 808 |
return; |
| 809 |
|
| 810 |
LastDiagLoc.emplace(Info.getLocation(), Info.getSourceManager()); |
| 811 |
LastDiagOriginallyError = OriginallyError; |
| 812 |
if (!Info.getFixItHints().empty()) |
| 813 |
AddFix(true /* try to invent a message instead of repeating the diag */); |
| 814 |
if (Fixer) { |
| 815 |
auto ExtraFixes = Fixer(LastDiag->Severity, Info); |
| 816 |
LastDiag->Fixes.insert(LastDiag->Fixes.end(), ExtraFixes.begin(), |
| 817 |
ExtraFixes.end()); |
| 818 |
} |
| 819 |
} else { |
| 820 |
// Handle a note to an existing diagnostic. |
| 821 |
if (!LastDiag) { |
| 822 |
assert(false && "Adding a note without main diagnostic"); |
| 823 |
IgnoreDiagnostics::log(DiagLevel, Info); |
| 824 |
return; |
| 825 |
} |
| 826 |
|
| 827 |
// If a diagnostic was suppressed due to the suppression filter, |
| 828 |
// also suppress notes associated with it. |
| 829 |
if (LastDiag->Severity == DiagnosticsEngine::Ignored) |
| 830 |
return; |
| 831 |
|
| 832 |
// Give include-fixer a chance to replace a note with a fix. |
| 833 |
if (Fixer) { |
| 834 |
auto ReplacementFixes = Fixer(LastDiag->Severity, Info); |
| 835 |
if (!ReplacementFixes.empty()) { |
| 836 |
assert(Info.getNumFixItHints() == 0 && |
| 837 |
"Include-fixer replaced a note with clang fix-its attached!"); |
| 838 |
LastDiag->Fixes.insert(LastDiag->Fixes.end(), ReplacementFixes.begin(), |
| 839 |
ReplacementFixes.end()); |
| 840 |
return; |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
if (!Info.getFixItHints().empty()) { |
| 845 |
// A clang note with fix-it is not a separate diagnostic in clangd. We |
| 846 |
// attach it as a Fix to the main diagnostic instead. |
| 847 |
if (!AddFix(false /* use the note as the message */)) |
| 848 |
IgnoreDiagnostics::log(DiagLevel, Info); |
| 849 |
} else { |
| 850 |
// A clang note without fix-its corresponds to clangd::Note. |
| 851 |
Note N; |
| 852 |
FillDiagBase(N); |
| 853 |
|
| 854 |
LastDiag->Notes.push_back(std::move(N)); |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
void StoreDiags::flushLastDiag() { |
| 860 |
if (!LastDiag) |
| 861 |
return; |
| 862 |
auto Finish = llvm::make_scope_exit([&, NDiags(Output.size())] { |
| 863 |
if (Output.size() == NDiags) // No new diag emitted. |
| 864 |
vlog("Dropped diagnostic: {0}: {1}", LastDiag->File, LastDiag->Message); |
| 865 |
LastDiag.reset(); |
| 866 |
}); |
| 867 |
|
| 868 |
if (LastDiag->Severity == DiagnosticsEngine::Ignored) |
| 869 |
return; |
| 870 |
// Move errors that occur from headers into main file. |
| 871 |
if (!LastDiag->InsideMainFile && LastDiagLoc && LastDiagOriginallyError) { |
| 872 |
if (tryMoveToMainFile(*LastDiag, *LastDiagLoc)) { |
| 873 |
// Suppress multiple errors from the same inclusion. |
| 874 |
if (!IncludedErrorLocations |
| 875 |
.insert({LastDiag->Range.start.line, |
| 876 |
LastDiag->Range.start.character}) |
| 877 |
.second) |
| 878 |
return; |
| 879 |
} |
| 880 |
} |
| 881 |
if (!mentionsMainFile(*LastDiag)) |
| 882 |
return; |
| 883 |
Output.push_back(std::move(*LastDiag)); |
| 884 |
} |
| 885 |
|
| 886 |
bool isBuiltinDiagnosticSuppressed(unsigned ID, |
| 887 |
const llvm::StringSet<> &Suppress, |
| 888 |
const LangOptions &LangOpts) { |
| 889 |
// Don't complain about header-only stuff in mainfiles if it's a header. |
| 890 |
// FIXME: would be cleaner to suppress in clang, once we decide whether the |
| 891 |
// behavior should be to silently-ignore or respect the pragma. |
| 892 |
if (ID == diag::pp_pragma_sysheader_in_main_file && LangOpts.IsHeaderFile) |
| 893 |
return true; |
| 894 |
|
| 895 |
if (const char *CodePtr = getDiagnosticCode(ID)) { |
| 896 |
if (Suppress.contains(normalizeSuppressedCode(CodePtr))) |
| 897 |
return true; |
| 898 |
} |
| 899 |
StringRef Warning = DiagnosticIDs::getWarningOptionForDiag(ID); |
| 900 |
if (!Warning.empty() && Suppress.contains(Warning)) |
| 901 |
return true; |
| 902 |
return false; |
| 903 |
} |
| 904 |
|
| 905 |
llvm::StringRef normalizeSuppressedCode(llvm::StringRef Code) { |
| 906 |
Code.consume_front("err_"); |
| 907 |
Code.consume_front("-W"); |
| 908 |
return Code; |
| 909 |
} |
| 910 |
|
| 911 |
std::optional<std::string> getDiagnosticDocURI(Diag::DiagSource Source, |
| 912 |
unsigned ID, |
| 913 |
llvm::StringRef Name) { |
| 914 |
switch (Source) { |
| 915 |
case Diag::Unknown: |
| 916 |
break; |
| 917 |
case Diag::Clang: |
| 918 |
// There is a page listing many warning flags, but it provides too little |
| 919 |
// information to be worth linking. |
| 920 |
// https://clang.llvm.org/docs/DiagnosticsReference.html |
| 921 |
break; |
| 922 |
case Diag::ClangTidy: { |
| 923 |
StringRef Module, Check; |
| 924 |
// This won't correctly get the module for clang-analyzer checks, but as we |
| 925 |
// don't link in the analyzer that shouldn't be an issue. |
| 926 |
// This would also need updating if anyone decides to create a module with a |
| 927 |
// '-' in the name. |
| 928 |
std::tie(Module, Check) = Name.split('-'); |
| 929 |
if (Module.empty() || Check.empty()) |
| 930 |
return std::nullopt; |
| 931 |
return ("https://clang.llvm.org/extra/clang-tidy/checks/" + Module + "/" + |
| 932 |
Check + ".html") |
| 933 |
.str(); |
| 934 |
} |
| 935 |
case Diag::Clangd: |
| 936 |
if (Name == "unused-includes") |
| 937 |
return {"https://clangd.llvm.org/guides/include-cleaner"}; |
| 938 |
break; |
| 939 |
case Diag::ClangdConfig: |
| 940 |
// FIXME: we should link to https://clangd.llvm.org/config |
| 941 |
// However we have no diagnostic codes, which the link should describe! |
| 942 |
break; |
| 943 |
} |
| 944 |
return std::nullopt; |
| 945 |
} |
| 946 |
|
| 947 |
} // namespace clangd |
| 948 |
} // namespace clang |