[clang-tools-extra] b5b2c81 - [clangd] Propagate data in diagnostics
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 13 08:50:33 PDT 2021
Author: Kadir Cetinkaya
Date: 2021-04-13T17:45:09+02:00
New Revision: b5b2c81055cfd0cc6a2a1d1ff2c8017d422586dc
URL: https://github.com/llvm/llvm-project/commit/b5b2c81055cfd0cc6a2a1d1ff2c8017d422586dc
DIFF: https://github.com/llvm/llvm-project/commit/b5b2c81055cfd0cc6a2a1d1ff2c8017d422586dc.diff
LOG: [clangd] Propagate data in diagnostics
Differential Revision: https://reviews.llvm.org/D98505
Added:
Modified:
clang-tools-extra/clangd/Diagnostics.cpp
clang-tools-extra/clangd/Diagnostics.h
clang-tools-extra/clangd/Protocol.cpp
clang-tools-extra/clangd/Protocol.h
clang-tools-extra/clangd/refactor/Tweak.h
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/Diagnostics.cpp b/clang-tools-extra/clangd/Diagnostics.cpp
index 49574dc3b2acc..453b5a644d096 100644
--- a/clang-tools-extra/clangd/Diagnostics.cpp
+++ b/clang-tools-extra/clangd/Diagnostics.cpp
@@ -476,6 +476,10 @@ void toLSPDiags(
Res.message = noteMessage(D, Note, Opts);
OutFn(std::move(Res), llvm::ArrayRef<Fix>());
}
+
+ // 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});
}
int getSeverity(DiagnosticsEngine::Level L) {
diff --git a/clang-tools-extra/clangd/Diagnostics.h b/clang-tools-extra/clangd/Diagnostics.h
index ea55132a9ca89..9c2235dce9a9a 100644
--- a/clang-tools-extra/clangd/Diagnostics.h
+++ b/clang-tools-extra/clangd/Diagnostics.h
@@ -20,12 +20,14 @@
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringSet.h"
+#include "llvm/Support/JSON.h"
#include "llvm/Support/SourceMgr.h"
#include <cassert>
#include <functional>
#include <memory>
#include <string>
#include <utility>
+#include <vector>
namespace clang {
namespace tidy {
@@ -70,6 +72,10 @@ struct DiagBase {
// diags from the main file.
bool InsideMainFile = false;
unsigned ID; // e.g. member of clang::diag, or clang-tidy assigned ID.
+ // Feature modules can make use of this field to propagate data from a
+ // diagnostic to a CodeAction request. Each module should only append to the
+ // list.
+ llvm::json::Object OpaqueData;
};
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DiagBase &D);
diff --git a/clang-tools-extra/clangd/Protocol.cpp b/clang-tools-extra/clangd/Protocol.cpp
index de2f34c6bd846..3a94b2965a6d7 100644
--- a/clang-tools-extra/clangd/Protocol.cpp
+++ b/clang-tools-extra/clangd/Protocol.cpp
@@ -600,6 +600,8 @@ llvm::json::Value toJSON(const Diagnostic &D) {
Diag["source"] = D.source;
if (D.relatedInformation)
Diag["relatedInformation"] = *D.relatedInformation;
+ if (!D.data.empty())
+ Diag["data"] = llvm::json::Object(D.data);
// FIXME: workaround for older gcc/clang
return std::move(Diag);
}
@@ -607,7 +609,11 @@ llvm::json::Value toJSON(const Diagnostic &D) {
bool fromJSON(const llvm::json::Value &Params, Diagnostic &R,
llvm::json::Path P) {
llvm::json::ObjectMapper O(Params, P);
- return O && O.map("range", R.range) && O.map("message", R.message) &&
+ if (!O)
+ return false;
+ if (auto *Data = Params.getAsObject()->getObject("data"))
+ R.data = *Data;
+ return O.map("range", R.range) && O.map("message", R.message) &&
mapOptOrNull(Params, "severity", R.severity, P) &&
mapOptOrNull(Params, "category", R.category, P) &&
mapOptOrNull(Params, "code", R.code, P) &&
diff --git a/clang-tools-extra/clangd/Protocol.h b/clang-tools-extra/clangd/Protocol.h
index 8e90f1f478314..79c0e0ea15a74 100644
--- a/clang-tools-extra/clangd/Protocol.h
+++ b/clang-tools-extra/clangd/Protocol.h
@@ -844,6 +844,13 @@ struct Diagnostic {
/// Only with capability textDocument.publishDiagnostics.codeActionsInline.
/// (These actions can also be obtained using textDocument/codeAction).
llvm::Optional<std::vector<CodeAction>> codeActions;
+
+ /// A data entry field that is preserved between a
+ /// `textDocument/publishDiagnostics` notification
+ /// and`textDocument/codeAction` request.
+ /// Mutating users should associate their data with a unique key they can use
+ /// to retrieve later on.
+ llvm::json::Object data;
};
llvm::json::Value toJSON(const Diagnostic &);
diff --git a/clang-tools-extra/clangd/refactor/Tweak.h b/clang-tools-extra/clangd/refactor/Tweak.h
index cc8d12ba2cb85..b381bdb2b799b 100644
--- a/clang-tools-extra/clangd/refactor/Tweak.h
+++ b/clang-tools-extra/clangd/refactor/Tweak.h
@@ -26,6 +26,7 @@
#include "index/Index.h"
#include "support/Path.h"
#include "clang/Tooling/Core/Replacement.h"
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
More information about the cfe-commits
mailing list