Skip to content

Commit

Permalink
[clangd] Propagate data in diagnostics
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D98505
  • Loading branch information
kadircet committed Apr 13, 2021
1 parent bce3ac4 commit b5b2c81
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions clang-tools-extra/clangd/Diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions clang-tools-extra/clangd/Diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 7 additions & 1 deletion clang-tools-extra/clangd/Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,14 +600,20 @@ 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);
}

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) &&
Expand Down
7 changes: 7 additions & 0 deletions clang-tools-extra/clangd/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 &);

Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/refactor/Tweak.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit b5b2c81

Please sign in to comment.