xref: /llvm-project/mlir/lib/Tools/mlir-lsp-server/MlirLspServerMain.cpp (revision 305d718539659fb5fccd9b6e0771bbabc6c3c821)
1 //===- MlirLspServerMain.cpp - MLIR Language Server main ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Tools/mlir-lsp-server/MlirLspServerMain.h"
10 #include "LSPServer.h"
11 #include "MLIRServer.h"
12 #include "mlir/IR/Dialect.h"
13 #include "mlir/Tools/lsp-server-support/Logging.h"
14 #include "mlir/Tools/lsp-server-support/Transport.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/Program.h"
17 
18 using namespace mlir;
19 using namespace mlir::lsp;
20 
MlirLspServerMain(int argc,char ** argv,DialectRegistry & registry)21 LogicalResult mlir::MlirLspServerMain(int argc, char **argv,
22                                       DialectRegistry &registry) {
23   llvm::cl::opt<JSONStreamStyle> inputStyle{
24       "input-style",
25       llvm::cl::desc("Input JSON stream encoding"),
26       llvm::cl::values(clEnumValN(JSONStreamStyle::Standard, "standard",
27                                   "usual LSP protocol"),
28                        clEnumValN(JSONStreamStyle::Delimited, "delimited",
29                                   "messages delimited by `// -----` lines, "
30                                   "with // comment support")),
31       llvm::cl::init(JSONStreamStyle::Standard),
32       llvm::cl::Hidden,
33   };
34   llvm::cl::opt<bool> litTest{
35       "lit-test",
36       llvm::cl::desc(
37           "Abbreviation for -input-style=delimited -pretty -log=verbose. "
38           "Intended to simplify lit tests"),
39       llvm::cl::init(false),
40   };
41   llvm::cl::opt<Logger::Level> logLevel{
42       "log",
43       llvm::cl::desc("Verbosity of log messages written to stderr"),
44       llvm::cl::values(
45           clEnumValN(Logger::Level::Error, "error", "Error messages only"),
46           clEnumValN(Logger::Level::Info, "info",
47                      "High level execution tracing"),
48           clEnumValN(Logger::Level::Debug, "verbose", "Low level details")),
49       llvm::cl::init(Logger::Level::Info),
50   };
51   llvm::cl::opt<bool> prettyPrint{
52       "pretty",
53       llvm::cl::desc("Pretty-print JSON output"),
54       llvm::cl::init(false),
55   };
56   llvm::cl::ParseCommandLineOptions(argc, argv, "MLIR LSP Language Server");
57 
58   if (litTest) {
59     inputStyle = JSONStreamStyle::Delimited;
60     logLevel = Logger::Level::Debug;
61     prettyPrint = true;
62   }
63 
64   // Configure the logger.
65   Logger::setLogLevel(logLevel);
66 
67   // Configure the transport used for communication.
68   llvm::sys::ChangeStdinToBinary();
69   JSONTransport transport(stdin, llvm::outs(), inputStyle, prettyPrint);
70 
71   // Register the additionally supported URI schemes for the MLIR server.
72   URIForFile::registerSupportedScheme("mlir.bytecode-mlir");
73 
74   // Configure the servers and start the main language server.
75   MLIRServer server(registry);
76   return runMlirLSPServer(server, transport);
77 }
78