This is a second PR on this patch (first #155572), that fixes the linking problem for `flang-aarch64-dylib` test. The SupportLSP library was made a component library. --- This PR moves the generic Language Server Protocol (LSP) server support code that was copied from clangd into MLIR, into the LLVM tree so it can be reused by multiple subprojects. Centralizing the generic LSP support in LLVM lowers the barrier to building new LSP servers across the LLVM ecosystem and avoids each subproject maintaining its own copy. The code originated in clangd and was copied into MLIR for its LSP server. MLIR had this code seperate to be reused by all of their LSP server. This PR relocates the MLIR copy into LLVM as a shared component into LLVM/Support. If this is not a suitable place, please suggest a better one. A follow up to this move could be deduplication with the original clangd implementation and converge on a single shared LSP support library used by clangd, MLIR, and future servers. What changes mlir/include/mlir/Tools/lsp-server-support/{Logging, Protocol, Transport}.h moved to llvm/include/llvm/Support/LSP mlir/lib/Tools/lsp-server-support/{Logging, Protocol, Transport}.cpp moved to llvm/lib/Support/LSP and their namespace was changed from mlir to llvm I ran clang-tidy --fix and clang-format on the whole moved files (last two commits), as they are basically new files and should hold up to the code style used by LLVM. MLIR LSP servers where updated to include these files from their new location and account for the namespace change. This PR is made as part of the LLVM IR LSP project ([RFC](https://discourse.llvm.org/t/rfc-ir-visualization-with-vs-code-extension-using-an-lsp-server/87773))
72 lines
2.4 KiB
C++
72 lines
2.4 KiB
C++
//===--- Protocol.cpp - Language Server Protocol Implementation -----------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file contains the serialization code for the PDLL specific LSP structs.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "Protocol.h"
|
|
#include "mlir/Support/LLVM.h"
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
#include "llvm/Support/JSON.h"
|
|
|
|
using namespace mlir;
|
|
using namespace mlir::lsp;
|
|
|
|
// Helper that doesn't treat `null` and absent fields as failures.
|
|
template <typename T>
|
|
static bool mapOptOrNull(const llvm::json::Value ¶ms,
|
|
llvm::StringLiteral prop, T &out,
|
|
llvm::json::Path path) {
|
|
const llvm::json::Object *o = params.getAsObject();
|
|
assert(o);
|
|
|
|
// Field is missing or null.
|
|
auto *v = o->get(prop);
|
|
if (!v || v->getAsNull())
|
|
return true;
|
|
return fromJSON(*v, out, path.field(prop));
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// PDLLViewOutputParams
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
bool mlir::lsp::fromJSON(const llvm::json::Value &value,
|
|
PDLLViewOutputKind &result, llvm::json::Path path) {
|
|
if (std::optional<StringRef> str = value.getAsString()) {
|
|
if (*str == "ast") {
|
|
result = PDLLViewOutputKind::AST;
|
|
return true;
|
|
}
|
|
if (*str == "mlir") {
|
|
result = PDLLViewOutputKind::MLIR;
|
|
return true;
|
|
}
|
|
if (*str == "cpp") {
|
|
result = PDLLViewOutputKind::CPP;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool mlir::lsp::fromJSON(const llvm::json::Value &value,
|
|
PDLLViewOutputParams &result, llvm::json::Path path) {
|
|
llvm::json::ObjectMapper o(value, path);
|
|
return o && o.map("uri", result.uri) && o.map("kind", result.kind);
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// PDLLViewOutputResult
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
llvm::json::Value mlir::lsp::toJSON(const PDLLViewOutputResult &value) {
|
|
return llvm::json::Object{{"output", value.output}};
|
|
}
|