From 37146431d23a926b560b449acd847bda8d7a7bb3 Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Tue, 14 Aug 2018 22:21:40 +0000 Subject: [PATCH] [clangd] add an extension field to LSP to transfer the diagnostic's category This patch adds a 'category' extension field to the LSP diagnostic that's sent by Clangd. This extension is always on by default. Differential Revision: https://reviews.llvm.org/D50571 llvm-svn: 339738 --- clang-tools-extra/clangd/ClangdLSPServer.cpp | 2 ++ clang-tools-extra/clangd/Diagnostics.cpp | 4 ++++ clang-tools-extra/clangd/Diagnostics.h | 1 + clang-tools-extra/clangd/Protocol.h | 6 ++++++ .../test/clangd/compile-commands-path-in-initialize.test | 2 ++ clang-tools-extra/test/clangd/compile-commands-path.test | 3 +++ clang-tools-extra/test/clangd/diagnostics.test | 1 + .../test/clangd/did-change-configuration-params.test | 1 + clang-tools-extra/test/clangd/execute-command.test | 1 + clang-tools-extra/test/clangd/extra-flags.test | 2 ++ clang-tools-extra/test/clangd/fixits.test | 1 + 11 files changed, 24 insertions(+) diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp index f71e36183a02..85e14e884bac 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -506,6 +506,8 @@ void ClangdLSPServer::onDiagnosticsReady(PathRef File, } LSPDiag["clangd_fixes"] = std::move(ClangdFixes); } + if (!Diag.category.empty()) + LSPDiag["category"] = Diag.category; DiagnosticsJSON.push_back(std::move(LSPDiag)); auto &FixItsForDiagnostic = LocalFixIts[Diag]; diff --git a/clang-tools-extra/clangd/Diagnostics.cpp b/clang-tools-extra/clangd/Diagnostics.cpp index c053bd22266f..893854d1d9f9 100644 --- a/clang-tools-extra/clangd/Diagnostics.cpp +++ b/clang-tools-extra/clangd/Diagnostics.cpp @@ -226,6 +226,7 @@ void toLSPDiags( clangd::Diagnostic Res; Res.range = D.Range; Res.severity = getSeverity(D.Severity); + Res.category = D.Category; return Res; }; @@ -292,6 +293,9 @@ void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, D.InsideMainFile = InsideMainFile; D.File = Info.getSourceManager().getFilename(Info.getLocation()); D.Severity = DiagLevel; + D.Category = DiagnosticIDs::getCategoryNameFromID( + DiagnosticIDs::getCategoryNumberForDiag(Info.getID())) + .str(); return D; }; diff --git a/clang-tools-extra/clangd/Diagnostics.h b/clang-tools-extra/clangd/Diagnostics.h index 0370bd36d234..89612c907739 100644 --- a/clang-tools-extra/clangd/Diagnostics.h +++ b/clang-tools-extra/clangd/Diagnostics.h @@ -37,6 +37,7 @@ struct DiagBase { std::string File; clangd::Range Range; DiagnosticsEngine::Level Severity = DiagnosticsEngine::Note; + std::string Category; // Since File is only descriptive, we store a separate flag to distinguish // diags from the main file. bool InsideMainFile = false; diff --git a/clang-tools-extra/clangd/Protocol.h b/clang-tools-extra/clangd/Protocol.h index f533c97f4873..1de5ced82980 100644 --- a/clang-tools-extra/clangd/Protocol.h +++ b/clang-tools-extra/clangd/Protocol.h @@ -544,6 +544,12 @@ struct Diagnostic { /// The diagnostic's message. std::string message; + + /// The diagnostic's category. Can be omitted. + /// An LSP extension that's used to send the name of the category over to the + /// client. The category typically describes the compilation stage during + /// which the issue was produced, e.g. "Semantic Issue" or "Parse Issue". + std::string category; }; /// A LSP-specific comparator used to find diagnostic in a container like diff --git a/clang-tools-extra/test/clangd/compile-commands-path-in-initialize.test b/clang-tools-extra/test/clangd/compile-commands-path-in-initialize.test index b34c59525bca..17b4333aee11 100644 --- a/clang-tools-extra/test/clangd/compile-commands-path-in-initialize.test +++ b/clang-tools-extra/test/clangd/compile-commands-path-in-initialize.test @@ -23,6 +23,7 @@ # CHECK-NEXT: "params": { # CHECK-NEXT: "diagnostics": [ # CHECK-NEXT: { +# CHECK-NEXT: "category": "#pragma message Directive", # CHECK-NEXT: "message": "MACRO is one", --- {"jsonrpc":"2.0","id":0,"method":"workspace/didChangeConfiguration","params":{"settings":{"compilationDatabasePath":"INPUT_DIR/build-2"}}} @@ -30,6 +31,7 @@ # CHECK-NEXT: "params": { # CHECK-NEXT: "diagnostics": [ # CHECK-NEXT: { +# CHECK-NEXT: "category": "#pragma message Directive", # CHECK-NEXT: "message": "MACRO is two", --- {"jsonrpc":"2.0","id":10000,"method":"shutdown"} diff --git a/clang-tools-extra/test/clangd/compile-commands-path.test b/clang-tools-extra/test/clangd/compile-commands-path.test index f25d002f91e8..693101ccd1c5 100644 --- a/clang-tools-extra/test/clangd/compile-commands-path.test +++ b/clang-tools-extra/test/clangd/compile-commands-path.test @@ -23,6 +23,7 @@ # CHECK-NEXT: "params": { # CHECK-NEXT: "diagnostics": [ # CHECK-NEXT: { +# CHECK-NEXT: "category": "#pragma message Directive", # CHECK-NEXT: "message": "MACRO is not defined", --- {"jsonrpc":"2.0","id":0,"method":"workspace/didChangeConfiguration","params":{"settings":{"compilationDatabasePath":"INPUT_DIR/build-1"}}} @@ -30,6 +31,7 @@ # CHECK-NEXT: "params": { # CHECK-NEXT: "diagnostics": [ # CHECK-NEXT: { +# CHECK-NEXT: "category": "#pragma message Directive", # CHECK-NEXT: "message": "MACRO is one", --- {"jsonrpc":"2.0","id":0,"method":"workspace/didChangeConfiguration","params":{"settings":{"compilationDatabasePath":"INPUT_DIR/build-2"}}} @@ -37,6 +39,7 @@ # CHECK-NEXT: "params": { # CHECK-NEXT: "diagnostics": [ # CHECK-NEXT: { +# CHECK-NEXT: "category": "#pragma message Directive", # CHECK-NEXT: "message": "MACRO is two", --- {"jsonrpc":"2.0","id":10000,"method":"shutdown"} diff --git a/clang-tools-extra/test/clangd/diagnostics.test b/clang-tools-extra/test/clangd/diagnostics.test index a191c0822a99..8a9feab3a39c 100644 --- a/clang-tools-extra/test/clangd/diagnostics.test +++ b/clang-tools-extra/test/clangd/diagnostics.test @@ -6,6 +6,7 @@ # CHECK-NEXT: "params": { # CHECK-NEXT: "diagnostics": [ # CHECK-NEXT: { +# CHECK-NEXT: "category": "Semantic Issue", # CHECK-NEXT: "message": "Return type of 'main' is not 'int'", # CHECK-NEXT: "range": { # CHECK-NEXT: "end": { diff --git a/clang-tools-extra/test/clangd/did-change-configuration-params.test b/clang-tools-extra/test/clangd/did-change-configuration-params.test index 51b4a8747415..d1caf71480af 100644 --- a/clang-tools-extra/test/clangd/did-change-configuration-params.test +++ b/clang-tools-extra/test/clangd/did-change-configuration-params.test @@ -24,6 +24,7 @@ # CHECK-NEXT: "params": { # CHECK-NEXT: "diagnostics": [ # CHECK-NEXT: { +# CHECK-NEXT: "category": "Semantic Issue", # CHECK-NEXT: "message": "Variable 'i' is uninitialized when used here", # CHECK-NEXT: "range": { # CHECK-NEXT: "end": { diff --git a/clang-tools-extra/test/clangd/execute-command.test b/clang-tools-extra/test/clangd/execute-command.test index 492006fdfab5..b907f0a9b5ab 100644 --- a/clang-tools-extra/test/clangd/execute-command.test +++ b/clang-tools-extra/test/clangd/execute-command.test @@ -6,6 +6,7 @@ # CHECK-NEXT: "params": { # CHECK-NEXT: "diagnostics": [ # CHECK-NEXT: { +# CHECK-NEXT: "category": "Semantic Issue", # CHECK-NEXT: "message": "Using the result of an assignment as a condition without parentheses", # CHECK-NEXT: "range": { # CHECK-NEXT: "end": { diff --git a/clang-tools-extra/test/clangd/extra-flags.test b/clang-tools-extra/test/clangd/extra-flags.test index f2460eb1241f..07b64cd34e9e 100644 --- a/clang-tools-extra/test/clangd/extra-flags.test +++ b/clang-tools-extra/test/clangd/extra-flags.test @@ -6,6 +6,7 @@ # CHECK-NEXT: "params": { # CHECK-NEXT: "diagnostics": [ # CHECK-NEXT: { +# CHECK-NEXT: "category": "Semantic Issue", # CHECK-NEXT: "message": "Variable 'i' is uninitialized when used here", # CHECK-NEXT: "range": { # CHECK-NEXT: "end": { @@ -28,6 +29,7 @@ # CHECK-NEXT: "params": { # CHECK-NEXT: "diagnostics": [ # CHECK-NEXT: { +# CHECK-NEXT: "category": "Semantic Issue", # CHECK-NEXT: "message": "Variable 'i' is uninitialized when used here", # CHECK-NEXT: "range": { # CHECK-NEXT: "end": { diff --git a/clang-tools-extra/test/clangd/fixits.test b/clang-tools-extra/test/clangd/fixits.test index ce74d1c8b377..f99a5a9ba7ac 100644 --- a/clang-tools-extra/test/clangd/fixits.test +++ b/clang-tools-extra/test/clangd/fixits.test @@ -6,6 +6,7 @@ # CHECK-NEXT: "params": { # CHECK-NEXT: "diagnostics": [ # CHECK-NEXT: { +# CHECK-NEXT: "category": "Semantic Issue", # CHECK-NEXT: "message": "Using the result of an assignment as a condition without parentheses", # CHECK-NEXT: "range": { # CHECK-NEXT: "end": {