[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

llvm-svn: 371474
This commit is contained in:
Jonas Devlieghere 2019-09-10 00:20:50 +00:00
parent 87d47cb7c4
commit e0ea8d87eb
8 changed files with 41 additions and 120 deletions

View File

@ -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_

View File

@ -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(const char *exe_path,
}
// 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(const char *exe_path,
}
// 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 =

View File

@ -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_private::Process *process,
"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_private::Process *process,
}
// 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_private::Process *process,
}
// 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_private::Process *process,
}
// 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_private::Process *process,
// 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);

View File

@ -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::LaunchAndConnectToDebugserver(
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::LaunchAndConnectToDebugserver(
#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();

View File

@ -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(const FileSpec &dsym_bundle_fspec,
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);

View File

@ -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);

View File

@ -4,7 +4,6 @@ add_lldb_unittest(UtilityTests
OptionsWithRawTest.cpp
ArchSpecTest.cpp
BroadcasterTest.cpp
CleanUpTest.cpp
ConstStringTest.cpp
CompletionRequestTest.cpp
DataExtractorTest.cpp

View File

@ -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);
}