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 "../lsp-server-support/Logging.h" 11 #include "../lsp-server-support/Transport.h" 12 #include "LSPServer.h" 13 #include "MLIRServer.h" 14 #include "mlir/IR/Dialect.h" 15 #include "llvm/Support/CommandLine.h" 16 #include "llvm/Support/Program.h" 17 18 using namespace mlir; 19 using namespace mlir::lsp; 20 21 LogicalResult mlir::MlirLspServerMain(int argc, char **argv, 22 DialectRegistry ®istry) { 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 // Configure the servers and start the main language server. 72 MLIRServer server(registry); 73 LSPServer lspServer(server, transport); 74 return lspServer.run(); 75 } 76