[llvm] [llvm-lsp] LSP server for LLVM IR (PR #161969)

Michal Vlasák via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 17 09:26:16 PST 2025


================
@@ -0,0 +1,213 @@
+//===-- llvm-lsp-server.cpp -------------------------------------*- C++ -*-===//
+//
+// 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 "llvm/IR/BasicBlock.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/JSON.h"
+#include "llvm/Support/Program.h"
+
+#include "IRDocument.h"
+#include "llvm-lsp-server.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
+#include <string>
+
+using namespace llvm;
+
+static cl::OptionCategory LlvmLspServerCategory("llvm-lsp-server options");
+static cl::opt<std::string> LogFilePath("log-file",
+                                        cl::desc("Path to log file"),
+                                        cl::init("/tmp/llvm-lsp-server.log"),
+                                        cl::cat(LlvmLspServerCategory));
+
+static lsp::Position llvmFileLocToLspPosition(const FileLoc &Pos) {
+  return lsp::Position(Pos.Line, Pos.Col);
+}
+
+static lsp::Range llvmFileLocRangeToLspRange(const FileLocRange &Range) {
+  return lsp::Range(llvmFileLocToLspPosition(Range.Start),
+                    llvmFileLocToLspPosition(Range.End));
+}
+
+llvm::Error LspServer::run() {
+  registerMessageHandlers();
+  return Transport.run(MessageHandler);
+}
+
+void LspServer::sendInfo(const std::string &Message) {
+  ShowMessageSender(lsp::ShowMessageParams(lsp::MessageType::Info, Message));
+}
+
+void LspServer::sendError(const std::string &Message) {
+  ShowMessageSender(lsp::ShowMessageParams(lsp::MessageType::Error, Message));
+}
+
+void LspServer::handleRequestInitialize(
+    const lsp::InitializeParams &Params,
+    lsp::Callback<llvm::json::Value> Reply) {
+  lsp::Logger::info("Received Initialize Message!");
+  sendInfo("Hello! Welcome to LLVM IR Language Server!");
----------------
vlasakm wrote:

FYI this actually waits for the user to press Return, so I don't think we need to use it for "hello" message.

https://github.com/llvm/llvm-project/pull/161969


More information about the llvm-commits mailing list