xref: /llvm-project/mlir/lib/Tools/tblgen-lsp-server/TableGenLspServerMain.cpp (revision 305d718539659fb5fccd9b6e0771bbabc6c3c821)
1 //===- TableGenLspServerMain.cpp - TableGen 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/tblgen-lsp-server/TableGenLspServerMain.h"
10 #include "LSPServer.h"
11 #include "TableGenServer.h"
12 #include "mlir/Tools/lsp-server-support/Logging.h"
13 #include "mlir/Tools/lsp-server-support/Transport.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/Program.h"
16 
17 using namespace mlir;
18 using namespace mlir::lsp;
19 
TableGenLspServerMain(int argc,char ** argv)20 LogicalResult mlir::TableGenLspServerMain(int argc, char **argv) {
21   llvm::cl::opt<JSONStreamStyle> inputStyle{
22       "input-style",
23       llvm::cl::desc("Input JSON stream encoding"),
24       llvm::cl::values(clEnumValN(JSONStreamStyle::Standard, "standard",
25                                   "usual LSP protocol"),
26                        clEnumValN(JSONStreamStyle::Delimited, "delimited",
27                                   "messages delimited by `// -----` lines, "
28                                   "with // comment support")),
29       llvm::cl::init(JSONStreamStyle::Standard),
30       llvm::cl::Hidden,
31   };
32   llvm::cl::opt<bool> litTest{
33       "lit-test",
34       llvm::cl::desc(
35           "Abbreviation for -input-style=delimited -pretty -log=verbose. "
36           "Intended to simplify lit tests"),
37       llvm::cl::init(false),
38   };
39   llvm::cl::opt<Logger::Level> logLevel{
40       "log",
41       llvm::cl::desc("Verbosity of log messages written to stderr"),
42       llvm::cl::values(
43           clEnumValN(Logger::Level::Error, "error", "Error messages only"),
44           clEnumValN(Logger::Level::Info, "info",
45                      "High level execution tracing"),
46           clEnumValN(Logger::Level::Debug, "verbose", "Low level details")),
47       llvm::cl::init(Logger::Level::Info),
48   };
49   llvm::cl::opt<bool> prettyPrint{
50       "pretty",
51       llvm::cl::desc("Pretty-print JSON output"),
52       llvm::cl::init(false),
53   };
54   llvm::cl::list<std::string> extraIncludeDirs(
55       "tablegen-extra-dir", llvm::cl::desc("Extra directory of include files"),
56       llvm::cl::value_desc("directory"), llvm::cl::Prefix);
57   llvm::cl::list<std::string> compilationDatabases(
58       "tablegen-compilation-database",
59       llvm::cl::desc("Compilation YAML databases containing additional "
60                      "compilation information for .td files"));
61 
62   llvm::cl::ParseCommandLineOptions(argc, argv, "TableGen LSP Language Server");
63 
64   if (litTest) {
65     inputStyle = JSONStreamStyle::Delimited;
66     logLevel = Logger::Level::Debug;
67     prettyPrint = true;
68   }
69 
70   // Configure the logger.
71   Logger::setLogLevel(logLevel);
72 
73   // Configure the transport used for communication.
74   llvm::sys::ChangeStdinToBinary();
75   JSONTransport transport(stdin, llvm::outs(), inputStyle, prettyPrint);
76 
77   // Configure the servers and start the main language server.
78   TableGenServer::Options options(compilationDatabases, extraIncludeDirs);
79   TableGenServer server(options);
80   return runTableGenLSPServer(server, transport);
81 }
82