[Lldb-commits] [lldb] r371474 - [Utility] Replace `lldb_private::CleanUp` by `llvm::scope_exit`

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Mon Sep 9 17:20:50 PDT 2019


Author: jdevlieghere
Date: Mon Sep  9 17:20:50 2019
New Revision: 371474

URL: http://llvm.org/viewvc/llvm-project?rev=371474&view=rev
Log:
[Utility] Replace `lldb_private::CleanUp` by `llvm::scope_exit`

This removes the CleanUp class and replaces its usages with llvm's
ScopeExit, which has similar semantics.

Differential revision: https://reviews.llvm.org/D67378

Removed:
    lldb/trunk/include/lldb/Utility/CleanUp.h
    lldb/trunk/unittests/Utility/CleanUpTest.cpp
Modified:
    lldb/trunk/source/Host/macosx/objcxx/Host.mm
    lldb/trunk/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
    lldb/trunk/source/Symbol/LocateSymbolFileMacOSX.cpp
    lldb/trunk/tools/lldb-test/lldb-test.cpp
    lldb/trunk/unittests/Utility/CMakeLists.txt

Removed: lldb/trunk/include/lldb/Utility/CleanUp.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/CleanUp.h?rev=371473&view=auto
==============================================================================
--- lldb/trunk/include/lldb/Utility/CleanUp.h (original)
+++ lldb/trunk/include/lldb/Utility/CleanUp.h (removed)
@@ -1,42 +0,0 @@
-//===-- CleanUp.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 liblldb_CleanUp_h_
-#define liblldb_CleanUp_h_
-
-#include "lldb/lldb-public.h"
-#include <functional>
-
-namespace lldb_private {
-
-/// Run a cleanup function on scope exit unless it's explicitly disabled.
-class CleanUp {
-  std::function<void()> Clean;
-
-public:
-  /// Register a cleanup function which applies \p Func to a list of arguments.
-  /// Use caution with arguments which are references: they will be copied.
-  template <typename F, typename... Args>
-  CleanUp(F &&Func, Args &&... args)
-      : Clean(std::bind(std::forward<F>(Func), std::forward<Args>(args)...)) {}
-
-  ~CleanUp() {
-    if (Clean)
-      Clean();
-  }
-
-  /// Disable the cleanup.
-  void disable() { Clean = nullptr; }
-
-  // Prevent cleanups from being run more than once.
-  DISALLOW_COPY_AND_ASSIGN(CleanUp);
-};
-
-} // namespace lldb_private
-
-#endif // #ifndef liblldb_CleanUp_h_

Modified: lldb/trunk/source/Host/macosx/objcxx/Host.mm
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/macosx/objcxx/Host.mm?rev=371474&r1=371473&r2=371474&view=diff
==============================================================================
--- lldb/trunk/source/Host/macosx/objcxx/Host.mm (original)
+++ lldb/trunk/source/Host/macosx/objcxx/Host.mm Mon Sep  9 17:20:50 2019
@@ -59,7 +59,6 @@
 #include "lldb/Host/ProcessLaunchInfo.h"
 #include "lldb/Host/ThreadLauncher.h"
 #include "lldb/Utility/ArchSpec.h"
-#include "lldb/Utility/CleanUp.h"
 #include "lldb/Utility/DataBufferHeap.h"
 #include "lldb/Utility/DataExtractor.h"
 #include "lldb/Utility/Endian.h"
@@ -71,8 +70,9 @@
 #include "lldb/Utility/StructuredData.h"
 #include "lldb/lldb-defines.h"
 
-#include "llvm/Support/FileSystem.h"
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/Support/Errno.h"
+#include "llvm/Support/FileSystem.h"
 
 #include "../cfcpp/CFCBundle.h"
 #include "../cfcpp/CFCMutableArray.h"
@@ -1092,7 +1092,8 @@ static Status LaunchProcessPosixSpawn(co
   }
 
   // Make sure we clean up the posix spawn attributes before exiting this scope.
-  CleanUp cleanup_attr(posix_spawnattr_destroy, &attr);
+  auto cleanup_attr =
+      llvm::make_scope_exit([&]() { posix_spawnattr_destroy(&attr); });
 
   sigset_t no_signals;
   sigset_t all_signals;
@@ -1195,7 +1196,8 @@ static Status LaunchProcessPosixSpawn(co
     }
 
     // Make sure we clean up the posix file actions before exiting this scope.
-    CleanUp cleanup_fileact(posix_spawn_file_actions_destroy, &file_actions);
+    auto cleanup_fileact = llvm::make_scope_exit(
+        [&]() { posix_spawn_file_actions_destroy(&file_actions); });
 
     for (size_t i = 0; i < num_file_actions; ++i) {
       const FileAction *launch_file_action =

Modified: lldb/trunk/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp?rev=371474&r1=371473&r2=371474&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp Mon Sep  9 17:20:50 2019
@@ -27,11 +27,11 @@
 #include "lldb/Target/ExecutionContext.h"
 #include "lldb/Target/Process.h"
 #include "lldb/Target/Thread.h"
-#include "lldb/Utility/CleanUp.h"
 #include "lldb/Utility/DataBufferHeap.h"
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
 #include "lldb/Utility/StreamString.h"
+#include "llvm/ADT/ScopeExit.h"
 
 using namespace lldb;
 using namespace lldb_private;
@@ -798,12 +798,13 @@ uint32_t PlatformPOSIX::DoLoadImage(lldb
                                     "for path: %s", utility_error.AsCString());
     return LLDB_INVALID_IMAGE_TOKEN;
   }
-  
+
   // Make sure we deallocate the input string memory:
-  CleanUp path_cleanup([process, path_addr] { 
-      process->DeallocateMemory(path_addr); 
+  auto path_cleanup = llvm::make_scope_exit([process, path_addr] {
+    // Deallocate the buffer.
+    process->DeallocateMemory(path_addr);
   });
-  
+
   process->WriteMemory(path_addr, path.c_str(), path_len, utility_error);
   if (utility_error.Fail()) {
     error.SetErrorStringWithFormat("dlopen error: could not write path string:"
@@ -824,21 +825,24 @@ uint32_t PlatformPOSIX::DoLoadImage(lldb
   }
   
   // Make sure we deallocate the result structure memory
-  CleanUp return_cleanup([process, return_addr] {
-      process->DeallocateMemory(return_addr);
+  auto return_cleanup = llvm::make_scope_exit([process, return_addr] {
+    // Deallocate the buffer
+    process->DeallocateMemory(return_addr);
   });
-  
+
   // This will be the address of the storage for paths, if we are using them,
   // or nullptr to signal we aren't.
   lldb::addr_t path_array_addr = 0x0;
-  llvm::Optional<CleanUp> path_array_cleanup;
+  llvm::Optional<llvm::detail::scope_exit<std::function<void()>>>
+      path_array_cleanup;
 
   // This is the address to a buffer large enough to hold the largest path
   // conjoined with the library name we're passing in.  This is a convenience 
   // to avoid having to call malloc in the dlopen function.
   lldb::addr_t buffer_addr = 0x0;
-  llvm::Optional<CleanUp> buffer_cleanup;
-  
+  llvm::Optional<llvm::detail::scope_exit<std::function<void()>>>
+      buffer_cleanup;
+
   // Set the values into our args and write them to the target:
   if (paths != nullptr) {
     // First insert the paths into the target.  This is expected to be a 
@@ -871,8 +875,9 @@ uint32_t PlatformPOSIX::DoLoadImage(lldb
     }
     
     // Make sure we deallocate the paths array.
-    path_array_cleanup.emplace([process, path_array_addr] { 
-        process->DeallocateMemory(path_array_addr); 
+    path_array_cleanup.emplace([process, path_array_addr]() {
+      // Deallocate the path array.
+      process->DeallocateMemory(path_array_addr);
     });
 
     process->WriteMemory(path_array_addr, path_array.data(), 
@@ -898,8 +903,9 @@ uint32_t PlatformPOSIX::DoLoadImage(lldb
     }
   
     // Make sure we deallocate the buffer memory:
-    buffer_cleanup.emplace([process, buffer_addr] { 
-        process->DeallocateMemory(buffer_addr); 
+    buffer_cleanup.emplace([process, buffer_addr]() {
+      // Deallocate the buffer.
+      process->DeallocateMemory(buffer_addr);
     });
   }
     
@@ -924,10 +930,11 @@ uint32_t PlatformPOSIX::DoLoadImage(lldb
   // Make sure we clean up the args structure.  We can't reuse it because the
   // Platform lives longer than the process and the Platforms don't get a
   // signal to clean up cached data when a process goes away.
-  CleanUp args_cleanup([do_dlopen_function, &exe_ctx, func_args_addr] {
-    do_dlopen_function->DeallocateFunctionResults(exe_ctx, func_args_addr);
-  });
-  
+  auto args_cleanup =
+      llvm::make_scope_exit([do_dlopen_function, &exe_ctx, func_args_addr] {
+        do_dlopen_function->DeallocateFunctionResults(exe_ctx, func_args_addr);
+      });
+
   // Now run the caller:
   EvaluateExpressionOptions options;
   options.SetExecutionPolicy(eExecutionPolicyAlways);

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp?rev=371474&r1=371473&r2=371474&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp Mon Sep  9 17:20:50 2019
@@ -63,7 +63,6 @@
 #include "lldb/Target/TargetList.h"
 #include "lldb/Target/ThreadPlanCallFunction.h"
 #include "lldb/Utility/Args.h"
-#include "lldb/Utility/CleanUp.h"
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Reproducer.h"
 #include "lldb/Utility/State.h"
@@ -81,6 +80,7 @@
 #include "lldb/Host/Host.h"
 #include "lldb/Utility/StringExtractorGDBRemote.h"
 
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/Threading.h"
 #include "llvm/Support/raw_ostream.h"
@@ -3519,8 +3519,8 @@ Status ProcessGDBRemote::LaunchAndConnec
 
     int our_socket = sockets[0];
     int gdb_socket = sockets[1];
-    CleanUp cleanup_our(close, our_socket);
-    CleanUp cleanup_gdb(close, gdb_socket);
+    auto cleanup_our = llvm::make_scope_exit([&]() { close(our_socket); });
+    auto cleanup_gdb = llvm::make_scope_exit([&]() { close(gdb_socket); });
 
     // Don't let any child processes inherit our communication socket
     SetCloexecFlag(our_socket);
@@ -3540,7 +3540,7 @@ Status ProcessGDBRemote::LaunchAndConnec
 #ifdef USE_SOCKETPAIR_FOR_LOCAL_CONNECTION
       // Our process spawned correctly, we can now set our connection to use
       // our end of the socket pair
-      cleanup_our.disable();
+      cleanup_our.release();
       m_gdb_comm.SetConnection(new ConnectionFileDescriptor(our_socket, true));
 #endif
       StartAsyncThread();

Modified: lldb/trunk/source/Symbol/LocateSymbolFileMacOSX.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/LocateSymbolFileMacOSX.cpp?rev=371474&r1=371473&r2=371474&view=diff
==============================================================================
--- lldb/trunk/source/Symbol/LocateSymbolFileMacOSX.cpp (original)
+++ lldb/trunk/source/Symbol/LocateSymbolFileMacOSX.cpp Mon Sep  9 17:20:50 2019
@@ -23,7 +23,6 @@
 #include "lldb/Host/Host.h"
 #include "lldb/Symbol/ObjectFile.h"
 #include "lldb/Utility/ArchSpec.h"
-#include "lldb/Utility/CleanUp.h"
 #include "lldb/Utility/DataBuffer.h"
 #include "lldb/Utility/DataExtractor.h"
 #include "lldb/Utility/Endian.h"
@@ -33,6 +32,7 @@
 #include "lldb/Utility/UUID.h"
 #include "mach/machine.h"
 
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/Support/FileSystem.h"
 
 using namespace lldb;
@@ -264,7 +264,7 @@ FileSpec Symbols::FindSymbolFileInBundle
     return {};
 
   // Make sure we close the directory before exiting this scope.
-  CleanUp cleanup_dir(closedir, dirp);
+  auto cleanup_dir = llvm::make_scope_exit([&]() { closedir(dirp); });
 
   FileSpec dsym_fspec;
   dsym_fspec.GetDirectory().SetCString(path);

Modified: lldb/trunk/tools/lldb-test/lldb-test.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-test/lldb-test.cpp?rev=371474&r1=371473&r2=371474&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-test/lldb-test.cpp (original)
+++ lldb/trunk/tools/lldb-test/lldb-test.cpp Mon Sep  9 17:20:50 2019
@@ -29,12 +29,12 @@
 #include "lldb/Target/Language.h"
 #include "lldb/Target/Process.h"
 #include "lldb/Target/Target.h"
-#include "lldb/Utility/CleanUp.h"
 #include "lldb/Utility/DataExtractor.h"
 #include "lldb/Utility/State.h"
 #include "lldb/Utility/StreamString.h"
 
 #include "llvm/ADT/IntervalMap.h"
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ManagedStatic.h"
@@ -1035,7 +1035,8 @@ int main(int argc, const char *argv[]) {
     return 1;
   }
 
-  CleanUp TerminateDebugger([&] { DebuggerLifetime.Terminate(); });
+  auto TerminateDebugger =
+      llvm::make_scope_exit([&] { DebuggerLifetime.Terminate(); });
 
   auto Dbg = lldb_private::Debugger::CreateInstance();
   ModuleList::GetGlobalModuleListProperties().SetEnableExternalLookup(false);

Modified: lldb/trunk/unittests/Utility/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/CMakeLists.txt?rev=371474&r1=371473&r2=371474&view=diff
==============================================================================
--- lldb/trunk/unittests/Utility/CMakeLists.txt (original)
+++ lldb/trunk/unittests/Utility/CMakeLists.txt Mon Sep  9 17:20:50 2019
@@ -4,7 +4,6 @@ add_lldb_unittest(UtilityTests
   OptionsWithRawTest.cpp
   ArchSpecTest.cpp
   BroadcasterTest.cpp
-  CleanUpTest.cpp
   ConstStringTest.cpp
   CompletionRequestTest.cpp
   DataExtractorTest.cpp

Removed: lldb/trunk/unittests/Utility/CleanUpTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/CleanUpTest.cpp?rev=371473&view=auto
==============================================================================
--- lldb/trunk/unittests/Utility/CleanUpTest.cpp (original)
+++ lldb/trunk/unittests/Utility/CleanUpTest.cpp (removed)
@@ -1,46 +0,0 @@
-//===-- CleanUpTest.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 "lldb/Utility/CleanUp.h"
-#include "gtest/gtest.h"
-
-using namespace lldb_private;
-
-TEST(CleanUpTest, no_args) {
-  bool f = false;
-  {
-    CleanUp cleanup([&] { f = true; });
-  }
-  ASSERT_TRUE(f);
-}
-
-TEST(CleanUpTest, multiple_args) {
-  bool f1 = false;
-  bool f2 = false;
-  bool f3 = false;
-  {
-    CleanUp cleanup(
-        [](bool arg1, bool *arg2, bool &arg3) {
-          ASSERT_FALSE(arg1);
-          *arg2 = true;
-          arg3 = true;
-        },
-        f1, &f2, f3);
-  }
-  ASSERT_TRUE(f2);
-  ASSERT_FALSE(f3);
-}
-
-TEST(CleanUpTest, disable) {
-  bool f = false;
-  {
-    CleanUp cleanup([&] { f = true; });
-    cleanup.disable();
-  }
-  ASSERT_FALSE(f);
-}




More information about the lldb-commits mailing list