Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc++] Add macros for try/catch and rethrow #112515

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

philnik777
Copy link
Contributor

This makes code using try/catch blocks significantly more readable, since we avoid a bunch of #if _LIBCPP_HAS_EXCEPTIONS.

Copy link

github-actions bot commented Oct 16, 2024

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff a2042521a0387d7d7b80b2987f4b21f5a50bc7bb cd8a8f667aff3fa310caf8b09e9cf866d3fde41f --extensions ,cpp,h -- libcxx/include/__config libcxx/include/__flat_map/flat_map.h libcxx/include/__hash_table libcxx/include/__iterator/counted_iterator.h libcxx/include/__memory/shared_ptr.h libcxx/include/__memory/uninitialized_algorithms.h libcxx/include/__ostream/basic_ostream.h libcxx/include/__ostream/print.h libcxx/include/__pstl/backends/libdispatch.h libcxx/include/__split_buffer libcxx/include/__vector/vector.h libcxx/include/__vector/vector_bool.h libcxx/include/deque libcxx/include/forward_list libcxx/include/fstream libcxx/include/future libcxx/include/iomanip libcxx/include/istream libcxx/include/list libcxx/include/sstream libcxx/include/string libcxx/include/syncstream libcxx/include/valarray libcxx/include/variant libcxx/src/filesystem/error.h libcxx/src/filesystem/format_string.h libcxx/src/locale.cpp
View the diff from clang-format here.
diff --git a/libcxx/include/__config b/libcxx/include/__config
index b318369f6a..f5373ba255 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -1233,7 +1233,7 @@ typedef __char32_t char32_t;
 
 #  if _LIBCPP_HAS_EXCEPTIONS
 #    define _LIBCPP_TRY try
-#    define _LIBCPP_CATCH(...) catch(__VA_ARGS__)
+#    define _LIBCPP_CATCH(...) catch (__VA_ARGS__)
 #    define _LIBCPP_RETHROW throw
 #  else
 #    define _LIBCPP_TRY if (true)
diff --git a/libcxx/include/__flat_map/flat_map.h b/libcxx/include/__flat_map/flat_map.h
index a511f38799..d5f66600be 100644
--- a/libcxx/include/__flat_map/flat_map.h
+++ b/libcxx/include/__flat_map/flat_map.h
@@ -1144,7 +1144,7 @@ private:
       } else {
         // In this case, we know the values are just like before we attempted emplacement,
         // and we also know that the keys have been emplaced successfully. Just roll back the keys.
-        _LIBCPP_TRY {
+      _LIBCPP_TRY {
           __containers_.keys.erase(__key_it);
         } _LIBCPP_CATCH(...) {
           // Now things are funky for real. We're failing to rollback the keys.
diff --git a/libcxx/include/__ostream/basic_ostream.h b/libcxx/include/__ostream/basic_ostream.h
index d19869621d..e9431f6ca1 100644
--- a/libcxx/include/__ostream/basic_ostream.h
+++ b/libcxx/include/__ostream/basic_ostream.h
@@ -584,7 +584,8 @@ basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::write(const char
       if (this->rdbuf()->sputn(__s, __n) != __n)
         this->setstate(ios_base::badbit);
     }
-  } _LIBCPP_CATCH(...) {
+  }
+  _LIBCPP_CATCH(...) {
     this->__set_badbit_and_consider_rethrow();
   }
   return *this;
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index 19c8964b95..29562d4ff3 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -1274,7 +1274,7 @@ vector<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __position, _Inpu
       reserve(__recommend(size() + __v.size()));
       __p        = this->__begin_ + __old_p;
       __old_last = this->__begin_ + __old_size;
-    } _LIBCPP_CATCH (...) {
+    } _LIBCPP_CATCH(...) {
       erase(__make_iter(__old_last), end());
       _LIBCPP_RETHROW;
     }
diff --git a/libcxx/include/future b/libcxx/include/future
index 67060941d6..f94a2d8298 100644
--- a/libcxx/include/future
+++ b/libcxx/include/future
@@ -867,7 +867,7 @@ void __async_assoc_state<void, _Fp>::__execute() {
   _LIBCPP_TRY {
     __func_();
     this->set_value();
-  } _LIBCPP_CATCH (...) {
+  } _LIBCPP_CATCH(...) {
     this->set_exception(current_exception());
   }
 }
@@ -1737,7 +1737,7 @@ void packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) {
   _LIBCPP_TRY {
     __f_(std::forward<_ArgTypes>(__args)...);
     __p_.set_value();
-  } _LIBCPP_CATCH (...) {
+  } _LIBCPP_CATCH(...) {
     __p_.set_exception(current_exception());
   }
 }
diff --git a/libcxx/include/istream b/libcxx/include/istream
index 130182c0ee..bd20bea188 100644
--- a/libcxx/include/istream
+++ b/libcxx/include/istream
@@ -833,7 +833,7 @@ typename basic_istream<_CharT, _Traits>::int_type basic_istream<_CharT, _Traits>
       __r = this->rdbuf()->sgetc();
       if (traits_type::eq_int_type(__r, traits_type::eof()))
         __state |= ios_base::eofbit;
-    } _LIBCPP_CATCH (...) {
+    } _LIBCPP_CATCH(...) {
       __state |= ios_base::badbit;
       this->__setstate_nothrow(__state);
       if (this->exceptions() & ios_base::badbit) {
diff --git a/libcxx/include/string b/libcxx/include/string
index 7604a51ca4..3efc69edba 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -2486,7 +2486,7 @@ basic_string<_CharT, _Traits, _Allocator>::__init_with_size(_InputIterator __fir
   _LIBCPP_TRY {
     auto __end = __copy_non_overlapping_range(__first, __last, std::__to_address(__p));
     traits_type::assign(*__end, value_type());
-  } _LIBCPP_CATCH (...) {
+  } _LIBCPP_CATCH(...) {
     if (__is_long())
       __alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_cap());
     _LIBCPP_RETHROW;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant