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