[PATCH] D134008: Add Cleanup class.

Justin Lebar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 15 20:05:38 PDT 2022


jlebar created this revision.
jlebar added a reviewer: tra.
Herald added a subscriber: mgorny.
Herald added a project: All.
jlebar requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Like absl::Cleanup, llvm::Cleanup runs an arbitrary function right
before the scope containing the Cleanup exits.

You can use this to do some work at the end of a function, right before
it returns.

This is used in a later patch.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134008

Files:
  llvm/include/llvm/ADT/Cleanup.h
  llvm/unittests/ADT/CMakeLists.txt
  llvm/unittests/ADT/CleanupTest.cpp


Index: llvm/unittests/ADT/CleanupTest.cpp
===================================================================
--- /dev/null
+++ llvm/unittests/ADT/CleanupTest.cpp
@@ -0,0 +1,25 @@
+//===- llvm/unittest/Support/CleanupTest.cpp - Cleanup tests ---===//
+//
+// 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 "llvm/ADT/Cleanup.h"
+#include "gtest/gtest.h"
+#include <cstdlib>
+
+using namespace llvm;
+
+namespace {
+
+TEST(CleanupTest, Simple) {
+  bool ran = false;
+  {
+    Cleanup c = [&] { ran = true; };
+  }
+  EXPECT_TRUE(ran);
+}
+
+} // anonymous namespace
Index: llvm/unittests/ADT/CMakeLists.txt
===================================================================
--- llvm/unittests/ADT/CMakeLists.txt
+++ llvm/unittests/ADT/CMakeLists.txt
@@ -15,6 +15,7 @@
   BitVectorTest.cpp
   BreadthFirstIteratorTest.cpp
   BumpPtrListTest.cpp
+  CleanupTest.cpp
   CoalescingBitVectorTest.cpp
   CombinationGeneratorTest.cpp
   DAGDeltaAlgorithmTest.cpp
Index: llvm/include/llvm/ADT/Cleanup.h
===================================================================
--- /dev/null
+++ llvm/include/llvm/ADT/Cleanup.h
@@ -0,0 +1,44 @@
+//===- Cleanup.h - Run a lambda when a fn exits -----------------*- 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+///  Cleanup is an RAII type whose destructor runs an arbitrary function or
+///  lambda.  You can use this to do some work at the end of a function, right
+///  before it returns.
+///
+///  Prior art: absl::Cleanup.
+///
+///  Example::
+///
+///    Cleanup cleanup = [&] { close(fd); };
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ADT_CLEANUP_H
+#define LLVM_ADT_CLEANUP_H
+
+#include <algorithm>
+namespace llvm {
+
+template <typename FnT> class Cleanup {
+public:
+  Cleanup(FnT Fn) : Fn(std::move(Fn)) {}
+  ~Cleanup() { Fn(); }
+
+private:
+  static_assert(std::is_same_v<decltype(std::declval<FnT>()()), void>,
+                "Callback function must return void.");
+
+  FnT Fn;
+};
+
+template <typename FnT> Cleanup(FnT Fn) -> Cleanup<FnT>;
+
+} // end namespace llvm
+
+#endif // LLVM_ADT_CLEANUP_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134008.460616.patch
Type: text/x-patch
Size: 2645 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220916/b83141d0/attachment.bin>


More information about the llvm-commits mailing list