kadircet created this revision.
kadircet added a reviewer: sammccall.
Herald added subscribers: usaxena95, arphaman, mgorny.
kadircet requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

Interface for BuildSystem integration to enable generation of build
file edits.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96124

Files:
  clang-tools-extra/clangd/BuildSystem.cpp
  clang-tools-extra/clangd/BuildSystem.h
  clang-tools-extra/clangd/CMakeLists.txt

Index: clang-tools-extra/clangd/CMakeLists.txt
===================================================================
--- clang-tools-extra/clangd/CMakeLists.txt
+++ clang-tools-extra/clangd/CMakeLists.txt
@@ -47,6 +47,7 @@
 add_clang_library(clangDaemon
   AST.cpp
   ASTSignals.cpp
+  BuildSystem.cpp
   ClangdLSPServer.cpp
   ClangdServer.cpp
   CodeComplete.cpp
Index: clang-tools-extra/clangd/BuildSystem.h
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/BuildSystem.h
@@ -0,0 +1,55 @@
+//===--- BuildSystem.h -------------------------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_BUILD_SYSTEM_H_
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_BUILD_SYSTEM_H_
+
+#include "Protocol.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Error.h"
+#include <memory>
+#include <mutex>
+#include <string>
+#include <vector>
+
+namespace clang {
+namespace clangd {
+
+class BuildSystem {
+public:
+  virtual ~BuildSystem() = default;
+
+  virtual llvm::Expected<std::vector<std::string>>
+  getOwningTarget(llvm::StringRef FilePath) const = 0;
+
+  virtual llvm::Expected<WorkspaceEdit>
+  addDependency(llvm::StringRef FromTarget, llvm::StringRef ToTarget) const = 0;
+};
+
+class BuildSystemPlugin {
+public:
+  virtual ~BuildSystemPlugin() = default;
+
+  virtual std::unique_ptr<BuildSystem>
+  createForDirectory(llvm::StringRef Dir) const = 0;
+};
+using BuildSystemRegistry = llvm::Registry<BuildSystemPlugin>;
+
+class BuildSystemProvider {
+public:
+  const BuildSystem *get(llvm::StringRef Path);
+
+private:
+  // Keyed by absolute paths.
+  // We can hand out pointers as they're stable and entries are never removed.
+  mutable llvm::StringMap<std::unique_ptr<BuildSystem>> DirCaches;
+  mutable std::mutex DirCachesMutex;
+};
+} // namespace clangd
+} // namespace clang
+#endif
Index: clang-tools-extra/clangd/BuildSystem.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/BuildSystem.cpp
@@ -0,0 +1,43 @@
+//===--- BuildSystem.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 "BuildSystem.h"
+#include "support/Path.h"
+#include "llvm/ADT/StringRef.h"
+#include <memory>
+#include <mutex>
+
+namespace clang {
+namespace clangd {
+
+const BuildSystem *BuildSystemProvider::get(llvm::StringRef Path) {
+  std::vector<PathRef> SearchDirs;
+  actOnAllParentDirectories(Path, [&](PathRef Dir) {
+    SearchDirs.push_back(Dir);
+    return false;
+  });
+  std::lock_guard<std::mutex> Lock(DirCachesMutex);
+  for (PathRef Dir : SearchDirs) {
+    auto Entry = DirCaches.try_emplace(Dir, nullptr);
+    auto &Value = Entry.first->getValue();
+    if (Entry.second) {
+      for (const auto &BSP : BuildSystemRegistry::entries()) {
+        Value = BSP.instantiate()->createForDirectory(Dir);
+        if (Value)
+          break;
+      }
+    }
+    if (Value)
+      return Value.get();
+  }
+  return nullptr;
+}
+
+} // namespace clangd
+} // namespace clang
+
+LLVM_INSTANTIATE_REGISTRY(clang::clangd::BuildSystemRegistry);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to