[Lldb-commits] [PATCH] D143548: [lldb] Add the ability to remove diagnostic callbacks

Jonas Devlieghere via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Feb 9 13:20:58 PST 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG26e9f23ada4f: [lldb] Add the ability to remove diagnostic callbacks (authored by JDevlieghere).
Herald added a project: LLDB.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143548/new/

https://reviews.llvm.org/D143548

Files:
  lldb/include/lldb/Utility/Diagnostics.h
  lldb/source/Utility/Diagnostics.cpp


Index: lldb/source/Utility/Diagnostics.cpp
===================================================================
--- lldb/source/Utility/Diagnostics.cpp
+++ lldb/source/Utility/Diagnostics.cpp
@@ -43,9 +43,19 @@
 
 Diagnostics::~Diagnostics() {}
 
-void Diagnostics::AddCallback(Callback callback) {
+Diagnostics::CallbackID Diagnostics::AddCallback(Callback callback) {
   std::lock_guard<std::mutex> guard(m_callbacks_mutex);
-  m_callbacks.push_back(callback);
+  CallbackID id = m_callback_id++;
+  m_callbacks.emplace_back(id, callback);
+  return id;
+}
+
+void Diagnostics::RemoveCallback(CallbackID id) {
+  std::lock_guard<std::mutex> guard(m_callbacks_mutex);
+  m_callbacks.erase(
+      std::remove_if(m_callbacks.begin(), m_callbacks.end(),
+                     [id](const CallbackEntry &e) { return e.id == id; }),
+      m_callbacks.end());
 }
 
 bool Diagnostics::Dump(raw_ostream &stream) {
@@ -84,8 +94,8 @@
   if (Error err = DumpDiangosticsLog(dir))
     return err;
 
-  for (Callback c : m_callbacks) {
-    if (Error err = c(dir))
+  for (CallbackEntry e : m_callbacks) {
+    if (Error err = e.callback(dir))
       return err;
   }
 
Index: lldb/include/lldb/Utility/Diagnostics.h
===================================================================
--- lldb/include/lldb/Utility/Diagnostics.h
+++ lldb/include/lldb/Utility/Diagnostics.h
@@ -42,8 +42,10 @@
   void Report(llvm::StringRef message);
 
   using Callback = std::function<llvm::Error(const FileSpec &)>;
+  using CallbackID = uint64_t;
 
-  void AddCallback(Callback callback);
+  CallbackID AddCallback(Callback callback);
+  void RemoveCallback(CallbackID id);
 
   static Diagnostics &Instance();
 
@@ -61,7 +63,21 @@
 
   RotatingLogHandler m_log_handler;
 
-  llvm::SmallVector<Callback, 4> m_callbacks;
+  struct CallbackEntry {
+    CallbackEntry(CallbackID id, Callback callback)
+        : id(id), callback(std::move(callback)) {}
+    CallbackID id;
+    Callback callback;
+  };
+
+  /// Monotonically increasing callback identifier. Unique per Diagnostic
+  /// instance.
+  CallbackID m_callback_id;
+
+  /// List of callback entries.
+  llvm::SmallVector<CallbackEntry, 4> m_callbacks;
+
+  /// Mutex to protect callback list and callback identifier.
   std::mutex m_callbacks_mutex;
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143548.496224.patch
Type: text/x-patch
Size: 2289 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20230209/a25fb07e/attachment.bin>


More information about the lldb-commits mailing list