[PATCH] D96244: [clangd][RFC] Introudce Plugins

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 7 23:08:53 PST 2021


kadircet created this revision.
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.

Plugins can be used to augment clangd's behaviours in various features.
Initially it only supports generating extra code actions for diagnostics.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96244

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


Index: clang-tools-extra/clangd/Plugin.h
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/Plugin.h
@@ -0,0 +1,38 @@
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PLUGIN_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PLUGIN_H
+#include "Diagnostics.h"
+#include "support/Path.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/None.h"
+#include "llvm/ADT/Optional.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Registry.h"
+#include <vector>
+
+namespace clang {
+namespace clangd {
+
+/// Plugins enable augmenting clangd's behaviour in various places.
+class Plugin {
+public:
+  virtual ~Plugin() = default;
+
+  /// Identifier for the plugin, should be unique.
+  virtual llvm::StringLiteral id() = 0;
+
+  /// Returns an action that can be invoked to fix the diagnostic.
+  virtual llvm::Optional<CodeAction>
+  actOnDiagnostic(DiagnosticsEngine::Level L, const clang::Diagnostic &Diag) {
+    return llvm::None;
+  }
+
+  /// Convenience helper to collect fixes from all the plugins.
+  static std::vector<CodeAction> collectFixes(DiagnosticsEngine::Level L,
+                                              const clang::Diagnostic &Diag);
+};
+using PluginRegistry = llvm::Registry<Plugin>;
+
+} // namespace clangd
+} // namespace clang
+#endif
Index: clang-tools-extra/clangd/Plugin.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/Plugin.cpp
@@ -0,0 +1,24 @@
+#include "Plugin.h"
+#include "Diagnostics.h"
+#include "Protocol.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/Support/Registry.h"
+#include <utility>
+#include <vector>
+
+namespace clang {
+namespace clangd {
+
+std::vector<CodeAction> Plugin::collectFixes(DiagnosticsEngine::Level L,
+                                             const clang::Diagnostic &Diag) {
+  std::vector<CodeAction> Actions;
+  for (const auto &Entry : PluginRegistry::entries()) {
+    if (auto Action = Entry.instantiate()->actOnDiagnostic(L, Diag))
+      Actions.emplace_back(std::move(*Action));
+  }
+  return Actions;
+}
+} // namespace clangd
+} // namespace clang
+
+LLVM_INSTANTIATE_REGISTRY(clang::clangd::PluginRegistry);
Index: clang-tools-extra/clangd/CMakeLists.txt
===================================================================
--- clang-tools-extra/clangd/CMakeLists.txt
+++ clang-tools-extra/clangd/CMakeLists.txt
@@ -75,6 +75,7 @@
   IncludeFixer.cpp
   JSONTransport.cpp
   PathMapping.cpp
+  Plugin.cpp
   Protocol.cpp
   Quality.cpp
   ParsedAST.cpp


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96244.322034.patch
Type: text/x-patch
Size: 2602 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210208/4734d757/attachment.bin>


More information about the cfe-commits mailing list