Files
clang-p2996/mlir/lib/Tools/mlir-lsp-server/MlirLspServerMain.cpp
River Riddle 751c14fc42 [mlir][mlir-lsp] Add a new C++ LSP server for MLIR named mlir-lsp-server
This commits adds a basic LSP server for MLIR that supports resolving references and definitions. Several components of the setup are simplified to keep the size of this commit down, and will be built out in later commits. A followup commit will add a vscode language client that communicates with this server, paving the way for better IDE experience when interfacing with MLIR files.

The structure of this tool is similar to mlir-opt and mlir-translate, i.e. the implementation is structured as a library that users can call into to implement entry points that contain the dialects/passes that they are interested in.

Note: This commit contains several files, namely those in `mlir-lsp-server/lsp`, that have been copied from the LSP code in clangd and adapted for use in MLIR. This copying was decided as the best initial path forward (discussed offline by several stake holders in MLIR and clangd) given the different needs of our MLIR server, and the one for clangd. If a strong desire/need for unification arises in the future, the existence of these files in mlir-lsp-server can be reconsidered.

Differential Revision: https://reviews.llvm.org/D100439
2021-04-21 14:44:37 -07:00

76 lines
2.7 KiB
C++

//===- MlirLspServerMain.cpp - MLIR Language Server main ------------------===//
//
// 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/mlir-lsp-server/MlirLspServerMain.h"
#include "LSPServer.h"
#include "MLIRServer.h"
#include "lsp/Logging.h"
#include "lsp/Transport.h"
#include "mlir/IR/Dialect.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Program.h"
using namespace mlir;
using namespace mlir::lsp;
LogicalResult mlir::MlirLspServerMain(int argc, char **argv,
DialectRegistry &registry) {
llvm::cl::opt<JSONStreamStyle> inputStyle{
"input-style",
llvm::cl::desc("Input JSON stream encoding"),
llvm::cl::values(clEnumValN(JSONStreamStyle::Standard, "standard",
"usual LSP protocol"),
clEnumValN(JSONStreamStyle::Delimited, "delimited",
"messages delimited by `// -----` lines, "
"with // comment support")),
llvm::cl::init(JSONStreamStyle::Standard),
llvm::cl::Hidden,
};
llvm::cl::opt<bool> litTest{
"lit-test",
llvm::cl::desc(
"Abbreviation for -input-style=delimited -pretty -log=verbose. "
"Intended to simplify lit tests"),
llvm::cl::init(false),
};
llvm::cl::opt<Logger::Level> logLevel{
"log",
llvm::cl::desc("Verbosity of log messages written to stderr"),
llvm::cl::values(
clEnumValN(Logger::Level::Error, "error", "Error messages only"),
clEnumValN(Logger::Level::Info, "info",
"High level execution tracing"),
clEnumValN(Logger::Level::Debug, "verbose", "Low level details")),
llvm::cl::init(Logger::Level::Info),
};
llvm::cl::opt<bool> prettyPrint{
"pretty",
llvm::cl::desc("Pretty-print JSON output"),
llvm::cl::init(false),
};
llvm::cl::ParseCommandLineOptions(argc, argv, "MLIR LSP Language Server");
if (litTest) {
inputStyle = JSONStreamStyle::Delimited;
logLevel = Logger::Level::Debug;
prettyPrint = true;
}
// Configure the logger.
Logger::setLogLevel(logLevel);
// Configure the transport used for communication.
llvm::sys::ChangeStdinToBinary();
JSONTransport transport(stdin, llvm::outs(), inputStyle, prettyPrint);
// Configure the servers and start the main language server.
MLIRServer server(registry);
LSPServer lspServer(server, transport);
return lspServer.run();
}