[libc++] Granularize <exception> includes

Reviewed By: ldionne, #libc

Spies: mikhail.ramalho, smeenai, libcxx-commits

Differential Revision: https://reviews.llvm.org/D146097

GitOrigin-RevId: c9d36bd80760db14f14b33789e6cbc6cb8c64830
diff --git a/include/__expected/bad_expected_access.h b/include/__expected/bad_expected_access.h
index 361eab4..e7a568c 100644
--- a/include/__expected/bad_expected_access.h
+++ b/include/__expected/bad_expected_access.h
@@ -10,10 +10,9 @@
 #define _LIBCPP___EXPECTED_BAD_EXPECTED_ACCESS_H
 
 #include <__config>
+#include <__exception/exception.h>
 #include <__utility/move.h>
 
-#include <exception>
-
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
 #endif
diff --git a/include/__format/buffer.h b/include/__format/buffer.h
index c8e1192..0f669ba 100644
--- a/include/__format/buffer.h
+++ b/include/__format/buffer.h
@@ -27,6 +27,7 @@
 #include <__iterator/incrementable_traits.h>
 #include <__iterator/iterator_traits.h>
 #include <__iterator/wrap_iter.h>
+#include <__memory/addressof.h>
 #include <__type_traits/add_pointer.h>
 #include <__utility/move.h>
 #include <cstddef>
diff --git a/include/__functional/function.h b/include/__functional/function.h
index 92e0d24..3c6998c 100644
--- a/include/__functional/function.h
+++ b/include/__functional/function.h
@@ -12,6 +12,7 @@
 
 #include <__assert>
 #include <__config>
+#include <__exception/exception.h>
 #include <__functional/binary_function.h>
 #include <__functional/invoke.h>
 #include <__functional/unary_function.h>
@@ -32,7 +33,6 @@
 #include <__utility/piecewise_construct.h>
 #include <__utility/swap.h>
 #include <__verbose_abort>
-#include <exception>
 #include <new>
 #include <tuple>
 #include <typeinfo>
diff --git a/include/__ranges/join_view.h b/include/__ranges/join_view.h
index cba16c8..83145ff 100644
--- a/include/__ranges/join_view.h
+++ b/include/__ranges/join_view.h
@@ -22,6 +22,7 @@
 #include <__iterator/iterator_traits.h>
 #include <__iterator/iterator_with_data.h>
 #include <__iterator/segmented_iterator.h>
+#include <__memory/addressof.h>
 #include <__ranges/access.h>
 #include <__ranges/all.h>
 #include <__ranges/concepts.h>
diff --git a/include/functional b/include/functional
index 26c736f..489b70d 100644
--- a/include/functional
+++ b/include/functional
@@ -543,7 +543,6 @@
 #include <__functional/unary_negate.h>
 #include <__functional/unwrap_ref.h>
 #include <__utility/forward.h>
-#include <exception>
 #include <memory> // TODO: find out why removing this breaks the modules build
 #include <typeinfo>
 #include <version>
@@ -555,6 +554,7 @@
 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
 #  include <atomic>
 #  include <concepts>
+#  include <exception>
 #  include <tuple>
 #  include <type_traits>
 #  include <utility>
diff --git a/include/future b/include/future
index dee9d0a..cc7d1f9 100644
--- a/include/future
+++ b/include/future
@@ -366,6 +366,7 @@
 #include <__chrono/duration.h>
 #include <__chrono/time_point.h>
 #include <__config>
+#include <__exception/exception_ptr.h>
 #include <__memory/allocator_arg_t.h>
 #include <__memory/allocator_destructor.h>
 #include <__memory/shared_ptr.h>
@@ -377,8 +378,6 @@
 #include <__utility/auto_cast.h>
 #include <__utility/forward.h>
 #include <__utility/move.h>
-#include <__verbose_abort>
-#include <exception>
 #include <mutex>
 #include <new>
 #include <system_error>
@@ -2461,6 +2460,7 @@
 
 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
 #  include <atomic>
+#  include <exception>
 #endif
 
 #endif // _LIBCPP_FUTURE
diff --git a/include/map b/include/map
index c190e3f..98ee961 100644
--- a/include/map
+++ b/include/map
@@ -549,6 +549,7 @@
 #include <__iterator/erase_if_container.h>
 #include <__iterator/iterator_traits.h>
 #include <__iterator/reverse_iterator.h>
+#include <__memory/addressof.h>
 #include <__memory/allocator.h>
 #include <__memory_resource/polymorphic_allocator.h>
 #include <__node_handle>
diff --git a/include/new b/include/new
index 871ace0..af396cd 100644
--- a/include/new
+++ b/include/new
@@ -89,13 +89,14 @@
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__availability>
 #include <__config>
+#include <__exception/exception.h>
+#include <__type_traits/alignment_of.h>
 #include <__type_traits/is_function.h>
 #include <__type_traits/is_same.h>
 #include <__type_traits/remove_cv.h>
 #include <__verbose_abort>
 #include <cstddef>
 #include <cstdlib>
-#include <exception>
 #include <version>
 
 #if defined(_LIBCPP_ABI_VCRUNTIME)
@@ -365,6 +366,7 @@
 _LIBCPP_END_NAMESPACE_STD
 
 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
+#  include <exception>
 #  include <type_traits>
 #endif
 
diff --git a/include/ostream b/include/ostream
index 60f111c..dbb54ac 100644
--- a/include/ostream
+++ b/include/ostream
@@ -165,6 +165,7 @@
 
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__config>
+#include <__exception/operations.h>
 #include <__memory/shared_ptr.h>
 #include <__memory/unique_ptr.h>
 #include <__type_traits/conjunction.h>
diff --git a/include/stdexcept b/include/stdexcept
index ef47409..abdef87 100644
--- a/include/stdexcept
+++ b/include/stdexcept
@@ -43,9 +43,8 @@
 
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__config>
-#include <__verbose_abort>
+#include <__exception/exception.h>
 #include <cstdlib>
-#include <exception>
 #include <iosfwd>  // for string forward decl
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -300,4 +299,8 @@
 
 _LIBCPP_END_NAMESPACE_STD
 
+#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
+#  include <exception>
+#endif
+
 #endif // _LIBCPP_STDEXCEPT
diff --git a/include/thread b/include/thread
index db8a1e8..13e722c 100644
--- a/include/thread
+++ b/include/thread
@@ -85,7 +85,9 @@
 
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__config>
+#include <__exception/terminate.h>
 #include <__functional/hash.h>
+#include <__memory/addressof.h>
 #include <__memory/unique_ptr.h>
 #include <__mutex_base>
 #include <__thread/poll_with_backoff.h>
diff --git a/include/typeinfo b/include/typeinfo
index ca35150..1553be1 100644
--- a/include/typeinfo
+++ b/include/typeinfo
@@ -59,12 +59,12 @@
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__availability>
 #include <__config>
+#include <__exception/exception.h>
 #include <__type_traits/is_constant_evaluated.h>
 #include <__verbose_abort>
 #include <cstddef>
 #include <cstdint>
 #include <cstdlib>
-#include <exception>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -415,6 +415,7 @@
 _LIBCPP_END_NAMESPACE_STD
 
 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
+#  include <exception>
 #  include <type_traits>
 #endif
 
diff --git a/include/variant b/include/variant
index a88662e..425fead 100644
--- a/include/variant
+++ b/include/variant
@@ -210,6 +210,7 @@
 #include <__compare/compare_three_way_result.h>
 #include <__compare/three_way_comparable.h>
 #include <__config>
+#include <__exception/exception.h>
 #include <__functional/hash.h>
 #include <__functional/invoke.h>
 #include <__functional/operations.h>
@@ -239,7 +240,6 @@
 #include <__utility/swap.h>
 #include <__variant/monostate.h>
 #include <__verbose_abort>
-#include <exception>
 #include <initializer_list>
 #include <limits>
 #include <new>
@@ -1834,6 +1834,7 @@
 _LIBCPP_POP_MACROS
 
 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
+#  include <exception>
 #  include <type_traits>
 #  include <typeinfo>
 #  include <utility>
diff --git a/test/libcxx/transitive_includes/cxx03.csv b/test/libcxx/transitive_includes/cxx03.csv
index a5b1c91..92a84f5 100644
--- a/test/libcxx/transitive_includes/cxx03.csv
+++ b/test/libcxx/transitive_includes/cxx03.csv
@@ -213,7 +213,6 @@
 execution version
 expected cstddef
 expected cstdlib
-expected exception
 expected initializer_list
 expected new
 expected version
diff --git a/test/libcxx/transitive_includes/cxx11.csv b/test/libcxx/transitive_includes/cxx11.csv
index 2f36b61..833e8ac 100644
--- a/test/libcxx/transitive_includes/cxx11.csv
+++ b/test/libcxx/transitive_includes/cxx11.csv
@@ -213,7 +213,6 @@
 execution version
 expected cstddef
 expected cstdlib
-expected exception
 expected initializer_list
 expected new
 expected version
diff --git a/test/libcxx/transitive_includes/cxx14.csv b/test/libcxx/transitive_includes/cxx14.csv
index cc5075f..c3c0ae6 100644
--- a/test/libcxx/transitive_includes/cxx14.csv
+++ b/test/libcxx/transitive_includes/cxx14.csv
@@ -213,7 +213,6 @@
 execution version
 expected cstddef
 expected cstdlib
-expected exception
 expected initializer_list
 expected new
 expected version
diff --git a/test/libcxx/transitive_includes/cxx17.csv b/test/libcxx/transitive_includes/cxx17.csv
index cc5075f..c3c0ae6 100644
--- a/test/libcxx/transitive_includes/cxx17.csv
+++ b/test/libcxx/transitive_includes/cxx17.csv
@@ -213,7 +213,6 @@
 execution version
 expected cstddef
 expected cstdlib
-expected exception
 expected initializer_list
 expected new
 expected version
diff --git a/test/libcxx/transitive_includes/cxx20.csv b/test/libcxx/transitive_includes/cxx20.csv
index 4a892ae5..a5be0f1 100644
--- a/test/libcxx/transitive_includes/cxx20.csv
+++ b/test/libcxx/transitive_includes/cxx20.csv
@@ -220,7 +220,6 @@
 execution version
 expected cstddef
 expected cstdlib
-expected exception
 expected initializer_list
 expected new
 expected version
diff --git a/test/libcxx/transitive_includes/cxx2b.csv b/test/libcxx/transitive_includes/cxx2b.csv
index beb4acc..8bed29f 100644
--- a/test/libcxx/transitive_includes/cxx2b.csv
+++ b/test/libcxx/transitive_includes/cxx2b.csv
@@ -151,7 +151,6 @@
 execution version
 expected cstddef
 expected cstdlib
-expected exception
 expected initializer_list
 expected new
 expected version
@@ -277,7 +276,6 @@
 functional cstdint
 functional cstdlib
 functional cstring
-functional exception
 functional initializer_list
 functional iosfwd
 functional limits
@@ -293,7 +291,6 @@
 future cstdint
 future cstdlib
 future cstring
-future exception
 future initializer_list
 future iosfwd
 future limits
@@ -420,7 +417,6 @@
 mutex version
 new cstddef
 new cstdlib
-new exception
 new version
 numbers version
 numeric cmath
@@ -548,7 +544,6 @@
 stack initializer_list
 stack version
 stdexcept cstdlib
-stdexcept exception
 stdexcept iosfwd
 streambuf cstdint
 streambuf ios
@@ -618,7 +613,6 @@
 typeinfo cstddef
 typeinfo cstdint
 typeinfo cstdlib
-typeinfo exception
 unordered_map cmath
 unordered_map compare
 unordered_map cstddef
@@ -663,7 +657,6 @@
 variant cstddef
 variant cstdint
 variant cstring
-variant exception
 variant initializer_list
 variant limits
 variant new
diff --git a/test/std/thread/thread.condition/thread.condition.condvarany/wait_terminates.sh.cpp b/test/std/thread/thread.condition/thread.condition.condvarany/wait_terminates.sh.cpp
index fd61634..4e20862 100644
--- a/test/std/thread/thread.condition/thread.condition.condvarany/wait_terminates.sh.cpp
+++ b/test/std/thread/thread.condition/thread.condition.condvarany/wait_terminates.sh.cpp
@@ -55,13 +55,14 @@
 //     that terminate has been called)
 
 
-#include <condition_variable>
 #include <atomic>
-#include <thread>
-#include <chrono>
-#include <string>
-#include <cstdlib>
 #include <cassert>
+#include <chrono>
+#include <condition_variable>
+#include <cstdlib>
+#include <exception>
+#include <string>
+#include <thread>
 
 #include "make_test_thread.h"
 
diff --git a/test/std/thread/thread.threads/thread.thread.class/thread.thread.destr/dtor.pass.cpp b/test/std/thread/thread.threads/thread.thread.class/thread.thread.destr/dtor.pass.cpp
index 7766505..1d959e9 100644
--- a/test/std/thread/thread.threads/thread.thread.class/thread.thread.destr/dtor.pass.cpp
+++ b/test/std/thread/thread.threads/thread.thread.class/thread.thread.destr/dtor.pass.cpp
@@ -15,10 +15,11 @@
 
 // ~thread();
 
-#include <thread>
-#include <new>
-#include <cstdlib>
 #include <cassert>
+#include <cstdlib>
+#include <exception>
+#include <new>
+#include <thread>
 
 #include "make_test_thread.h"
 #include "test_macros.h"