[libcxx-commits] [libcxx] [libc++] Optimize `std::exception_ptr` (PR #162773)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Oct 9 21:40:23 PDT 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions ,cpp,h -- libcxx/include/__exception/exception_ptr.h libcxx/include/__exception/nested_exception.h libcxx/include/__exception/operations.h libcxx/include/exception libcxx/src/exception.cpp libcxx/src/new_handler.cpp libcxx/src/typeinfo.cpp libcxx/test/benchmarks/exception_ptr.bench.cpp
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index e442e198a..e7a1c7278 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -33,14 +33,14 @@
 // the functions to be emitted and included in the library. When users of libc++
 // compile their code, the __gnu_inline__ attribute will suppress generation of
 // these functions while making their definitions available for inlining.
-#  ifdef _LIBCPP_EMIT_CODE_FOR_EXCEPTION_PTR
-#    define _LIBCPP_EXPORTED_FROM_LIB_INLINEABLE _LIBCPP_EXPORTED_FROM_ABI
-#  else
-#    if !__has_cpp_attribute(gnu::gnu_inline)
-#      error "GNU inline attribute is not supported"
-#    endif
-#    define _LIBCPP_EXPORTED_FROM_LIB_INLINEABLE [[gnu::gnu_inline]] inline
+#ifdef _LIBCPP_EMIT_CODE_FOR_EXCEPTION_PTR
+#  define _LIBCPP_EXPORTED_FROM_LIB_INLINEABLE _LIBCPP_EXPORTED_FROM_ABI
+#else
+#  if !__has_cpp_attribute(gnu::gnu_inline)
+#    error "GNU inline attribute is not supported"
 #  endif
+#  define _LIBCPP_EXPORTED_FROM_LIB_INLINEABLE [[gnu::gnu_inline]] inline
+#endif
 
 _LIBCPP_DIAGNOSTIC_PUSH
 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wgnu-inline-cpp-without-extern")
@@ -119,8 +119,8 @@ public:
 
   friend _LIBCPP_HIDE_FROM_ABI void swap(exception_ptr& __x, exception_ptr& __y) {
     void* __tmp = __x.__ptr_;
-    __x.__ptr_ = __y.__ptr_;
-    __y.__ptr_ = __tmp;
+    __x.__ptr_  = __y.__ptr_;
+    __y.__ptr_  = __tmp;
   }
 
   friend _LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT;
@@ -136,7 +136,8 @@ _LIBCPP_EXPORTED_FROM_LIB_INLINEABLE exception_ptr exception_ptr::__from_native_
 }
 
 // Must be defined outside the class definition due to _LIBCPP_EXPORTED_FROM_LIB_INLINEABLE
-_LIBCPP_EXPORTED_FROM_LIB_INLINEABLE exception_ptr::exception_ptr(const exception_ptr& __other) noexcept : __ptr_(__other.__ptr_) {
+_LIBCPP_EXPORTED_FROM_LIB_INLINEABLE exception_ptr::exception_ptr(const exception_ptr& __other) noexcept
+    : __ptr_(__other.__ptr_) {
   __increment_refcount(__ptr_);
 }
 
@@ -156,7 +157,7 @@ _LIBCPP_EXPORTED_FROM_LIB_INLINEABLE exception_ptr& exception_ptr::operator=(con
 
 _LIBCPP_HIDE_FROM_ABI inline exception_ptr& exception_ptr::operator=(exception_ptr&& __other) noexcept {
   __decrement_refcount(__ptr_);
-  __ptr_ = __other.__ptr_;
+  __ptr_         = __other.__ptr_;
   __other.__ptr_ = nullptr;
   return *this;
 }
@@ -274,17 +275,17 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
 
 _LIBCPP_END_UNVERSIONED_NAMESPACE_STD
 
-#  if defined(_LIBCPP_CXX_ABI_NONE)
-#    include <__exception/exception_ptr_unimplemented.ipp>
-#  elif defined(_LIBCPP_CXX_ABI_LIBCXXABI) || defined(_LIBCPP_CXX_ABI_LIBCXXRT)
-#    include <__exception/exception_ptr_cxxabi.ipp>
-#  elif defined(_LIBCPP_CXX_ABI_LIBSTDCXX) || defined(_LIBCPP_CXX_ABI_LIBSUPCXX)
-#    include <__exception/exception_ptr_glibcxx.ipp>
-#  elif defined(_LIBCPP_CXX_ABI_VCRUNTIME)
-#    include <__exception/exception_ptr_msvc.ipp>
-#  else
-#    error "Unsupported C++ ABI library"
-#  endif
+#if defined(_LIBCPP_CXX_ABI_NONE)
+#  include <__exception/exception_ptr_unimplemented.ipp>
+#elif defined(_LIBCPP_CXX_ABI_LIBCXXABI) || defined(_LIBCPP_CXX_ABI_LIBCXXRT)
+#  include <__exception/exception_ptr_cxxabi.ipp>
+#elif defined(_LIBCPP_CXX_ABI_LIBSTDCXX) || defined(_LIBCPP_CXX_ABI_LIBSUPCXX)
+#  include <__exception/exception_ptr_glibcxx.ipp>
+#elif defined(_LIBCPP_CXX_ABI_VCRUNTIME)
+#  include <__exception/exception_ptr_msvc.ipp>
+#else
+#  error "Unsupported C++ ABI library"
+#endif
 
 _LIBCPP_DIAGNOSTIC_POP
 
diff --git a/libcxx/src/exception.cpp b/libcxx/src/exception.cpp
index 3ab6c1c2b..b0e4e7557 100644
--- a/libcxx/src/exception.cpp
+++ b/libcxx/src/exception.cpp
@@ -43,4 +43,4 @@ void nested_exception::rethrow_nested() const {
   rethrow_exception(__ptr_);
 }
 
-}
+} // namespace std
diff --git a/libcxx/test/benchmarks/exception_ptr.bench.cpp b/libcxx/test/benchmarks/exception_ptr.bench.cpp
index 6a4210bf6..b3fdf474d 100644
--- a/libcxx/test/benchmarks/exception_ptr.bench.cpp
+++ b/libcxx/test/benchmarks/exception_ptr.bench.cpp
@@ -18,15 +18,14 @@ void bm_make_exception_ptr(benchmark::State& state) {
 }
 BENCHMARK(bm_make_exception_ptr)->ThreadRange(1, 8);
 
-
 void bm_empty_exception_ptr(benchmark::State& state) {
   for (auto _ : state) {
     // All of the following operations are no-ops because
     // the exception_ptr is empty. Hence, the compiler should
     // be able to optimize them very aggressively.
     std::exception_ptr p1;
-    std::exception_ptr p2 (p1);
-    std::exception_ptr p3 (std::move(p2));
+    std::exception_ptr p2(p1);
+    std::exception_ptr p3(std::move(p2));
     p2 = std::move(p1);
     p1 = p2;
     swap(p1, p2);

``````````

</details>


https://github.com/llvm/llvm-project/pull/162773


More information about the libcxx-commits mailing list