Skip to content

[mlir-lsp] Add DiagnosticTag from LSP spec #91396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions mlir/include/mlir/Tools/lsp-server-support/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,16 @@ enum class DiagnosticSeverity {
Hint = 4
};

enum class DiagnosticTag {
Unnecessary = 1,
Deprecated = 2,
};

/// Add support for JSON serialization.
llvm::json::Value toJSON(DiagnosticTag tag);
bool fromJSON(const llvm::json::Value &value, DiagnosticTag &result,
llvm::json::Path path);

struct Diagnostic {
/// The source range where the message applies.
Range range;
Expand All @@ -696,6 +706,9 @@ struct Diagnostic {
/// a scope collide all definitions can be marked via this property.
std::optional<std::vector<DiagnosticRelatedInformation>> relatedInformation;

/// Additional metadata about the diagnostic.
std::vector<DiagnosticTag> tags;

/// 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
Expand Down
19 changes: 18 additions & 1 deletion mlir/lib/Tools/lsp-server-support/Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,20 @@ llvm::json::Value mlir::lsp::toJSON(const DiagnosticRelatedInformation &info) {
// Diagnostic
//===----------------------------------------------------------------------===//

llvm::json::Value mlir::lsp::toJSON(DiagnosticTag tag) {
return static_cast<int>(tag);
}

bool mlir::lsp::fromJSON(const llvm::json::Value &value, DiagnosticTag &result,
llvm::json::Path path) {
if (std::optional<int64_t> i = value.getAsInteger()) {
result = (DiagnosticTag)*i;
return true;
}

return false;
}

llvm::json::Value mlir::lsp::toJSON(const Diagnostic &diag) {
llvm::json::Object result{
{"range", diag.range},
Expand All @@ -658,6 +672,8 @@ llvm::json::Value mlir::lsp::toJSON(const Diagnostic &diag) {
result["source"] = diag.source;
if (diag.relatedInformation)
result["relatedInformation"] = *diag.relatedInformation;
if (!diag.tags.empty())
result["tags"] = diag.tags;
return std::move(result);
}

Expand All @@ -675,7 +691,8 @@ bool mlir::lsp::fromJSON(const llvm::json::Value &value, Diagnostic &result,
mapOptOrNull(value, "category", result.category, path) &&
mapOptOrNull(value, "source", result.source, path) &&
mapOptOrNull(value, "relatedInformation", result.relatedInformation,
path);
path) &&
mapOptOrNull(value, "tags", result.tags, path);
}

//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 0 deletions mlir/unittests/Tools/lsp-server-support/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_mlir_unittest(MLIRLspServerSupportTests
Protocol.cpp
Transport.cpp
)
target_link_libraries(MLIRLspServerSupportTests
Expand Down
51 changes: 51 additions & 0 deletions mlir/unittests/Tools/lsp-server-support/Protocol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===- Protocol.cpp - LSP JSON protocol unit tests ------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "mlir/Tools/lsp-server-support/Protocol.h"

#include "gtest/gtest.h"

using namespace mlir;
using namespace mlir::lsp;
using namespace testing;

namespace {

TEST(ProtocolTest, DiagnosticTagPresent) {
Diagnostic diagnostic;
diagnostic.tags.push_back(DiagnosticTag::Unnecessary);

llvm::json::Value json = toJSON(diagnostic);
const llvm::json::Object *o = json.getAsObject();
const llvm::json::Array *v = o->get("tags")->getAsArray();
EXPECT_EQ(*v, llvm::json::Array{1});

Diagnostic parsed;
llvm::json::Path::Root root = llvm::json::Path::Root();
bool success = fromJSON(json, parsed, llvm::json::Path(root));
EXPECT_TRUE(success);
ASSERT_EQ(parsed.tags.size(), (size_t)1);
EXPECT_EQ(parsed.tags.at(0), DiagnosticTag::Unnecessary);
}

TEST(ProtocolTest, DiagnosticTagNotPresent) {
Diagnostic diagnostic;

llvm::json::Value json = toJSON(diagnostic);
const llvm::json::Object *o = json.getAsObject();
const llvm::json::Value *v = o->get("tags");
EXPECT_EQ(v, nullptr);

Diagnostic parsed;
llvm::json::Path::Root root = llvm::json::Path::Root();
bool success = fromJSON(json, parsed, llvm::json::Path(root));
EXPECT_TRUE(success);
EXPECT_TRUE(parsed.tags.empty());
}

} // namespace