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++] Fix missing and incorrect push/pop macros #79204

Merged
merged 4 commits into from
Jan 25, 2024

Conversation

ldionne
Copy link
Member

@ldionne ldionne commented Jan 23, 2024

We recently noticed that the unwrap_iter.h file was pushing macros, but it was pushing them again instead of popping them at the end of the file. This led to libc++ basically swallowing any custom definition of these macros in user code:

#define min HELLO
#include <algorithm>
// min is not HELLO anymore, it's not defined

While investigating this issue, I noticed that our push/pop pragmas were actually entirely wrong too. Indeed, instead of pushing macros like move, we'd push move(int, int) in the pragma, which is not a valid macro name. As a result, we would not actually push macros like move -- instead we'd simply undefine them. This led to the following code not working:

#define move HELLO
#include <algorithm>
// move is not HELLO anymore

Fixing the pragma push/pop incantations led to a cascade of issues because we use identifiers like move in a large number of places, and all of these headers would now need to do the push/pop dance.

This patch fixes all these issues. First, it adds a check that we don't swallow important names like min, max, move or refresh as explained above. This is done by augmenting the existing system_reserved_names.gen.py test to also check that the macros are what we expect after including each header.

Second, it fixes the push/pop pragmas to work properly and adds missing pragmas to all the files I could detect a failure in via the newly added test.

rdar://121365472

We recently noticed that the unwrap_iter.h file was pushing macros,
but it was pushing them again instead of popping them at the end of
the file. This led to libc++ basically swallowing any custom definition
of these macros in user code:

    #define min HELLO
    #include <algorithm>
    // min is not HELLO anymore, it's not defined

While investigating this issue, I noticed that our push/pop pragmas
were actually entirely wrong too. Indeed, instead of pushing macros
like `move`, we'd push `move(int, int)` in the pragma, which is not
a valid macro name. As a result, we would not actually push macros
like `move` -- instead we'd simply undefine them. This led to the
following code not working:

    #define move HELLO
    #include <algorithm>
    // move is not HELLO anymore

Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places,
and all of these headers would now need to do the push/pop dance.

This patch fixes all these issues. First, it adds a check that we
don't swallow important names like min, max, move or refresh as
explained above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros
are what we expect after including each header.

Second, it fixes the push/pop pragmas to work properly and adds
missing pragmas to all the files I could detect a failure in via
the newly added test.

rdar://121365472
@ldionne ldionne requested a review from a team as a code owner January 23, 2024 20:18
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jan 23, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 23, 2024

@llvm/pr-subscribers-libcxx

Author: Louis Dionne (ldionne)

Changes

We recently noticed that the unwrap_iter.h file was pushing macros, but it was pushing them again instead of popping them at the end of the file. This led to libc++ basically swallowing any custom definition of these macros in user code:

#define min HELLO
#include &lt;algorithm&gt;
// min is not HELLO anymore, it's not defined

While investigating this issue, I noticed that our push/pop pragmas were actually entirely wrong too. Indeed, instead of pushing macros like move, we'd push move(int, int) in the pragma, which is not a valid macro name. As a result, we would not actually push macros like move -- instead we'd simply undefine them. This led to the following code not working:

#define move HELLO
#include &lt;algorithm&gt;
// move is not HELLO anymore

Fixing the pragma push/pop incantations led to a cascade of issues because we use identifiers like move in a large number of places, and all of these headers would now need to do the push/pop dance.

This patch fixes all these issues. First, it adds a check that we don't swallow important names like min, max, move or refresh as explained above. This is done by augmenting the existing system_reserved_names.gen.py test to also check that the macros are what we expect after including each header.

Second, it fixes the push/pop pragmas to work properly and adds missing pragmas to all the files I could detect a failure in via the newly added test.

rdar://121365472


Patch is 110.11 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/79204.diff

184 Files Affected:

  • (modified) libcxx/include/__algorithm/copy_move_common.h (+5)
  • (modified) libcxx/include/__algorithm/equal.h (+5)
  • (modified) libcxx/include/__algorithm/equal_range.h (+5)
  • (modified) libcxx/include/__algorithm/fold.h (+5)
  • (modified) libcxx/include/__algorithm/in_found_result.h (+5)
  • (modified) libcxx/include/__algorithm/in_fun_result.h (+5)
  • (modified) libcxx/include/__algorithm/in_in_out_result.h (+5)
  • (modified) libcxx/include/__algorithm/in_in_result.h (+5)
  • (modified) libcxx/include/__algorithm/in_out_out_result.h (+5)
  • (modified) libcxx/include/__algorithm/includes.h (+5)
  • (modified) libcxx/include/__algorithm/next_permutation.h (+5)
  • (modified) libcxx/include/__algorithm/nth_element.h (+5)
  • (modified) libcxx/include/__algorithm/partial_sort.h (+5)
  • (modified) libcxx/include/__algorithm/partial_sort_copy.h (+5)
  • (modified) libcxx/include/__algorithm/partition.h (+5)
  • (modified) libcxx/include/__algorithm/prev_permutation.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_any_all_none_of.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_backends/cpu_backends/transform_reduce.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_copy.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_count.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_equal.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_fill.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_find.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_for_each.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_generate.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_is_partitioned.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_merge.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_move.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_replace.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_rotate_copy.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_sort.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_stable_sort.h (+5)
  • (modified) libcxx/include/__algorithm/pstl_transform.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_all_of.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_any_of.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_binary_search.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_clamp.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_contains.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_copy.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_copy_backward.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_copy_if.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_copy_n.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_count.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_count_if.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_ends_with.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_equal.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_equal_range.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_fill.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_fill_n.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_find.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_find_end.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_find_first_of.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_find_if.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_find_if_not.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_for_each.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_for_each_n.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_generate.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_generate_n.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_includes.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_inplace_merge.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_is_heap.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_is_heap_until.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_is_partitioned.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_is_permutation.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_is_sorted.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_is_sorted_until.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_iterator_concept.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_lexicographical_compare.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_lower_bound.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_make_heap.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_max_element.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_merge.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_min_element.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_minmax_element.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_mismatch.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_move.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_move_backward.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_next_permutation.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_none_of.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_nth_element.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_partial_sort.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_partial_sort_copy.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_partition.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_partition_copy.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_partition_point.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_pop_heap.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_prev_permutation.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_push_heap.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_remove.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_remove_copy.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_remove_copy_if.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_remove_if.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_replace.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_replace_copy.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_replace_copy_if.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_replace_if.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_reverse_copy.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_rotate.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_rotate_copy.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_sample.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_search_n.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_set_difference.h (+6)
  • (modified) libcxx/include/__algorithm/ranges_set_intersection.h (+6)
  • (modified) libcxx/include/__algorithm/ranges_set_symmetric_difference.h (+6)
  • (modified) libcxx/include/__algorithm/ranges_set_union.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_shuffle.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_sort.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_sort_heap.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_stable_partition.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_stable_sort.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_starts_with.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_swap_ranges.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_transform.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_unique.h (+5)
  • (modified) libcxx/include/__algorithm/ranges_unique_copy.h (+5)
  • (modified) libcxx/include/__algorithm/remove.h (+5)
  • (modified) libcxx/include/__algorithm/remove_if.h (+5)
  • (modified) libcxx/include/__algorithm/reverse.h (+5)
  • (modified) libcxx/include/__algorithm/rotate.h (+5)
  • (modified) libcxx/include/__algorithm/set_difference.h (+5)
  • (modified) libcxx/include/__algorithm/set_intersection.h (+5)
  • (modified) libcxx/include/__algorithm/set_symmetric_difference.h (+5)
  • (modified) libcxx/include/__algorithm/set_union.h (+5)
  • (modified) libcxx/include/__algorithm/shift_left.h (+5)
  • (modified) libcxx/include/__algorithm/shift_right.h (+5)
  • (modified) libcxx/include/__algorithm/sort.h (+5)
  • (modified) libcxx/include/__algorithm/sort_heap.h (+5)
  • (modified) libcxx/include/__algorithm/stable_partition.h (+5)
  • (modified) libcxx/include/__algorithm/stable_sort.h (+5)
  • (modified) libcxx/include/__algorithm/swap_ranges.h (+5)
  • (modified) libcxx/include/__algorithm/unique.h (+5)
  • (modified) libcxx/include/__algorithm/unique_copy.h (+5)
  • (modified) libcxx/include/__algorithm/unwrap_iter.h (+1-1)
  • (modified) libcxx/include/__algorithm/unwrap_range.h (+5)
  • (modified) libcxx/include/__config (+2-2)
  • (modified) libcxx/include/__filesystem/directory_iterator.h (+5)
  • (modified) libcxx/include/__filesystem/path.h (+5)
  • (modified) libcxx/include/__filesystem/recursive_directory_iterator.h (+5)
  • (modified) libcxx/include/__format/format_arg.h (+5)
  • (modified) libcxx/include/__format/format_context.h (+5)
  • (modified) libcxx/include/__format/format_functions.h (+5)
  • (modified) libcxx/include/__format/formatter_output.h (+5)
  • (modified) libcxx/include/__format/write_escaped.h (+5)
  • (modified) libcxx/include/__functional/function.h (+5)
  • (modified) libcxx/include/__iterator/cpp17_iterator_concepts.h (+5)
  • (modified) libcxx/include/__iterator/iterator_with_data.h (+5)
  • (modified) libcxx/include/__memory/ranges_uninitialized_algorithms.h (+5)
  • (modified) libcxx/include/__memory/shared_ptr.h (+5)
  • (modified) libcxx/include/__memory/uninitialized_algorithms.h (+5)
  • (modified) libcxx/include/__mutex/once_flag.h (+5)
  • (modified) libcxx/include/__numeric/pstl_reduce.h (+5)
  • (modified) libcxx/include/__numeric/pstl_transform_reduce.h (+5)
  • (modified) libcxx/include/__numeric/reduce.h (+5)
  • (modified) libcxx/include/__numeric/saturation_arithmetic.h (+5)
  • (modified) libcxx/include/__numeric/transform_reduce.h (+5)
  • (modified) libcxx/include/__ranges/counted.h (+5)
  • (modified) libcxx/include/__ranges/drop_while_view.h (+5)
  • (modified) libcxx/include/__ranges/elements_view.h (+5)
  • (modified) libcxx/include/__ranges/filter_view.h (+5)
  • (modified) libcxx/include/__ranges/iota_view.h (+5)
  • (modified) libcxx/include/__ranges/join_view.h (+5)
  • (modified) libcxx/include/__ranges/lazy_split_view.h (+5)
  • (modified) libcxx/include/__ranges/repeat_view.h (+5)
  • (modified) libcxx/include/__ranges/reverse_view.h (+5)
  • (modified) libcxx/include/__ranges/single_view.h (+5)
  • (modified) libcxx/include/__ranges/split_view.h (+5)
  • (modified) libcxx/include/__ranges/take_while_view.h (+5)
  • (modified) libcxx/include/__ranges/transform_view.h (+5)
  • (modified) libcxx/include/__thread/jthread.h (+5)
  • (modified) libcxx/include/__thread/thread.h (+5)
  • (modified) libcxx/include/array (+5)
  • (modified) libcxx/include/condition_variable (+5)
  • (modified) libcxx/include/experimental/iterator (+5)
  • (modified) libcxx/include/future (+5)
  • (modified) libcxx/include/ios (+5)
  • (modified) libcxx/include/map (+5)
  • (modified) libcxx/include/ostream (+5)
  • (modified) libcxx/include/queue (+5)
  • (modified) libcxx/include/set (+5)
  • (modified) libcxx/include/stack (+5)
  • (modified) libcxx/include/strstream (+5)
  • (modified) libcxx/include/unordered_map (+5)
  • (modified) libcxx/include/unordered_set (+5)
  • (modified) libcxx/test/libcxx/system_reserved_names.gen.py (+19-1)
diff --git a/libcxx/include/__algorithm/copy_move_common.h b/libcxx/include/__algorithm/copy_move_common.h
index b350507e32bae95..0fc7a5e3cee7009 100644
--- a/libcxx/include/__algorithm/copy_move_common.h
+++ b/libcxx/include/__algorithm/copy_move_common.h
@@ -31,6 +31,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 // Type traits.
@@ -132,4 +135,6 @@ __dispatch_copy_or_move(_InIter __first, _Sent __last, _OutIter __out_first) {
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_COPY_MOVE_COMMON_H
diff --git a/libcxx/include/__algorithm/equal.h b/libcxx/include/__algorithm/equal.h
index f03f010aa51ab3f..3c0e3060e39a99f 100644
--- a/libcxx/include/__algorithm/equal.h
+++ b/libcxx/include/__algorithm/equal.h
@@ -30,6 +30,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
@@ -162,4 +165,6 @@ equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_EQUAL_H
diff --git a/libcxx/include/__algorithm/equal_range.h b/libcxx/include/__algorithm/equal_range.h
index 7ce54965fff05fb..a94290431971c41 100644
--- a/libcxx/include/__algorithm/equal_range.h
+++ b/libcxx/include/__algorithm/equal_range.h
@@ -31,6 +31,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _AlgPolicy, class _Compare, class _Iter, class _Sent, class _Tp, class _Proj>
@@ -77,4 +80,6 @@ equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __valu
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_EQUAL_RANGE_H
diff --git a/libcxx/include/__algorithm/fold.h b/libcxx/include/__algorithm/fold.h
index 88e6814d5cf99dd..1a9d76b50d83c96 100644
--- a/libcxx/include/__algorithm/fold.h
+++ b/libcxx/include/__algorithm/fold.h
@@ -32,6 +32,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 23
@@ -122,4 +125,6 @@ inline constexpr auto fold_left = __fold_left();
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_FOLD_H
diff --git a/libcxx/include/__algorithm/in_found_result.h b/libcxx/include/__algorithm/in_found_result.h
index 88a0255d169831a..a67ae387974c0a8 100644
--- a/libcxx/include/__algorithm/in_found_result.h
+++ b/libcxx/include/__algorithm/in_found_result.h
@@ -18,6 +18,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if _LIBCPP_STD_VER >= 20
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -46,4 +49,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // _LIBCPP_STD_VER >= 20
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_IN_FOUND_RESULT_H
diff --git a/libcxx/include/__algorithm/in_fun_result.h b/libcxx/include/__algorithm/in_fun_result.h
index 6110c1cf86cd52b..a22069a9a8ddaae 100644
--- a/libcxx/include/__algorithm/in_fun_result.h
+++ b/libcxx/include/__algorithm/in_fun_result.h
@@ -18,6 +18,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 20
@@ -46,4 +49,6 @@ struct in_fun_result {
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_IN_FUN_RESULT_H
diff --git a/libcxx/include/__algorithm/in_in_out_result.h b/libcxx/include/__algorithm/in_in_out_result.h
index 95ce4f4fd5bd44a..ba0380b5c68147d 100644
--- a/libcxx/include/__algorithm/in_in_out_result.h
+++ b/libcxx/include/__algorithm/in_in_out_result.h
@@ -18,6 +18,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 20
@@ -51,4 +54,6 @@ struct in_in_out_result {
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_IN_IN_OUT_RESULT_H
diff --git a/libcxx/include/__algorithm/in_in_result.h b/libcxx/include/__algorithm/in_in_result.h
index d1d62dae7f6703a..994573fc70fd887 100644
--- a/libcxx/include/__algorithm/in_in_result.h
+++ b/libcxx/include/__algorithm/in_in_result.h
@@ -18,6 +18,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 20
@@ -48,4 +51,6 @@ struct in_in_result {
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_IN_IN_RESULT_H
diff --git a/libcxx/include/__algorithm/in_out_out_result.h b/libcxx/include/__algorithm/in_out_out_result.h
index 143642368750860..8ceb452841a419b 100644
--- a/libcxx/include/__algorithm/in_out_out_result.h
+++ b/libcxx/include/__algorithm/in_out_out_result.h
@@ -18,6 +18,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 20
@@ -49,4 +52,6 @@ struct in_out_out_result {
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_IN_OUT_OUT_RESULT_H
diff --git a/libcxx/include/__algorithm/includes.h b/libcxx/include/__algorithm/includes.h
index 531752e93175699..05d45365eb806ff 100644
--- a/libcxx/include/__algorithm/includes.h
+++ b/libcxx/include/__algorithm/includes.h
@@ -22,6 +22,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Comp, class _Proj1, class _Proj2>
@@ -71,4 +74,6 @@ includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __fi
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_INCLUDES_H
diff --git a/libcxx/include/__algorithm/next_permutation.h b/libcxx/include/__algorithm/next_permutation.h
index d66ea9b973453f6..011ee028cc2f522 100644
--- a/libcxx/include/__algorithm/next_permutation.h
+++ b/libcxx/include/__algorithm/next_permutation.h
@@ -22,6 +22,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _AlgPolicy, class _Compare, class _BidirectionalIterator, class _Sentinel>
@@ -67,4 +70,6 @@ next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_NEXT_PERMUTATION_H
diff --git a/libcxx/include/__algorithm/nth_element.h b/libcxx/include/__algorithm/nth_element.h
index 37ddfbdacf044d0..da748d7255aba6a 100644
--- a/libcxx/include/__algorithm/nth_element.h
+++ b/libcxx/include/__algorithm/nth_element.h
@@ -23,6 +23,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Compare, class _RandomAccessIterator>
@@ -253,4 +256,6 @@ nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomA
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_NTH_ELEMENT_H
diff --git a/libcxx/include/__algorithm/partial_sort.h b/libcxx/include/__algorithm/partial_sort.h
index 27511a124229bba..85a8fdc77aa228b 100644
--- a/libcxx/include/__algorithm/partial_sort.h
+++ b/libcxx/include/__algorithm/partial_sort.h
@@ -26,6 +26,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _AlgPolicy, class _Compare, class _RandomAccessIterator, class _Sentinel>
@@ -83,4 +86,6 @@ partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _Ran
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PARTIAL_SORT_H
diff --git a/libcxx/include/__algorithm/partial_sort_copy.h b/libcxx/include/__algorithm/partial_sort_copy.h
index e7d8df4de89f95e..ef7c9d34d949831 100644
--- a/libcxx/include/__algorithm/partial_sort_copy.h
+++ b/libcxx/include/__algorithm/partial_sort_copy.h
@@ -28,6 +28,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _AlgPolicy,
@@ -98,4 +101,6 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _RandomAccessIterator
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PARTIAL_SORT_COPY_H
diff --git a/libcxx/include/__algorithm/partition.h b/libcxx/include/__algorithm/partition.h
index e2ceb07bf19588b..824e49b9ec21495 100644
--- a/libcxx/include/__algorithm/partition.h
+++ b/libcxx/include/__algorithm/partition.h
@@ -19,6 +19,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Predicate, class _AlgPolicy, class _ForwardIterator, class _Sentinel>
@@ -82,4 +85,6 @@ partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PARTITION_H
diff --git a/libcxx/include/__algorithm/prev_permutation.h b/libcxx/include/__algorithm/prev_permutation.h
index 3e4bbb3fbb1670e..8d15b6806401d8d 100644
--- a/libcxx/include/__algorithm/prev_permutation.h
+++ b/libcxx/include/__algorithm/prev_permutation.h
@@ -22,6 +22,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _AlgPolicy, class _Compare, class _BidirectionalIterator, class _Sentinel>
@@ -67,4 +70,6 @@ prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PREV_PERMUTATION_H
diff --git a/libcxx/include/__algorithm/pstl_any_all_none_of.h b/libcxx/include/__algorithm/pstl_any_all_none_of.h
index d93fdba2224c9b1..4b1e0e61b542185 100644
--- a/libcxx/include/__algorithm/pstl_any_all_none_of.h
+++ b/libcxx/include/__algorithm/pstl_any_all_none_of.h
@@ -23,6 +23,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -144,4 +147,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_ANY_ALL_NONE_OF_H
diff --git a/libcxx/include/__algorithm/pstl_backends/cpu_backends/transform_reduce.h b/libcxx/include/__algorithm/pstl_backends/cpu_backends/transform_reduce.h
index ab2e3172b8b63bd..14a0d76741d4c55 100644
--- a/libcxx/include/__algorithm/pstl_backends/cpu_backends/transform_reduce.h
+++ b/libcxx/include/__algorithm/pstl_backends/cpu_backends/transform_reduce.h
@@ -25,6 +25,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -194,4 +197,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_TRANSFORM_REDUCE_H
diff --git a/libcxx/include/__algorithm/pstl_copy.h b/libcxx/include/__algorithm/pstl_copy.h
index 19f275a0d5d97fc..1069dcec0e117a6 100644
--- a/libcxx/include/__algorithm/pstl_copy.h
+++ b/libcxx/include/__algorithm/pstl_copy.h
@@ -28,6 +28,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -113,4 +116,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_COPY_H
diff --git a/libcxx/include/__algorithm/pstl_count.h b/libcxx/include/__algorithm/pstl_count.h
index 28806fca0637013..2781f6bfd3c9e06 100644
--- a/libcxx/include/__algorithm/pstl_count.h
+++ b/libcxx/include/__algorithm/pstl_count.h
@@ -29,6 +29,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -113,4 +116,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_COUNT_H
diff --git a/libcxx/include/__algorithm/pstl_equal.h b/libcxx/include/__algorithm/pstl_equal.h
index b343d2675980c9c..d235c0f4f419722 100644
--- a/libcxx/include/__algorithm/pstl_equal.h
+++ b/libcxx/include/__algorithm/pstl_equal.h
@@ -21,6 +21,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -167,4 +170,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_EQUAL_H
diff --git a/libcxx/include/__algorithm/pstl_fill.h b/libcxx/include/__algorithm/pstl_fill.h
index 3057dcc04f1ad74..488b49a0feec96c 100644
--- a/libcxx/include/__algorithm/pstl_fill.h
+++ b/libcxx/include/__algorithm/pstl_fill.h
@@ -26,6 +26,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -108,4 +111,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_FILL_H
diff --git a/libcxx/include/__algorithm/pstl_find.h b/libcxx/include/__algorithm/pstl_find.h
index adc05ea1a9e55a0..5b694db68aead40 100644
--- a/libcxx/include/__algorithm/pstl_find.h
+++ b/libcxx/include/__algorithm/pstl_find.h
@@ -25,6 +25,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -133,4 +136,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_FIND_H
diff --git a/libcxx/include/__algorithm/pstl_for_each.h b/libcxx/include/__algorithm/pstl_for_each.h
index 819a43d685abed5..bb7b5a61a6dc0d1 100644
--- a/libcxx/include/__algorithm/pstl_for_each.h
+++ b/libcxx/include/__algorithm/pstl_for_each.h
@@ -28,6 +28,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -100,4 +103,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_FOR_EACH_H
diff --git a/libcxx/include/__algorithm/pstl_generate.h b/libcxx/include/__algorithm/pstl_generate.h
index 56538392d5b5ddd..7133c6f4f4c621d 100644
--- a/libcxx/include/__algorithm/pstl_generate.h
+++ b/libcxx/include/__algorithm/pstl_generate.h
@@ -25,6 +25,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -106,4 +109,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_GENERATE_H
diff --git a/libcxx/include/__algorithm/pstl_is_partitioned.h b/libcxx/include/__algorithm/pstl_is_partitioned.h
index 39cf6369339db69..b65430212207275 100644
--- a/libcxx/include/__algorithm/pstl_is_partitioned.h
+++ b/libcxx/include/__algorithm/pstl_is_partitioned.h
@@ -24,6 +24,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -69,4 +72,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_IS_PARITTIONED
diff --git a/libcxx/include/__algorithm/pstl_merge.h b/libcxx/include/__algorithm/pstl_merge.h
index ed8014510863266..3d262db6bc0c15d 100644
--- a/libcxx/include/__algorithm/pstl_merge.h
+++ b/libcxx/include/__algorithm/pstl_merge.h
@@ -22,6 +22,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -84,4 +87,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_MERGE_H
diff --git a/libcxx/include/__algorithm/pstl_move.h b/libcxx/include/__algorithm/pstl_move.h
index 52baab57591e268..d8441f1a6c2e169 100644
--- a/libcxx/include/__algorithm/pstl_move.h
+++ b/libcxx/include/__algorithm/pstl_move.h
@@ -27,6 +27,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -76,4 +79,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_MOVE_H
diff --git a/libcxx/include/__algorithm/pstl_replace.h b/libcxx/include/__algorithm/pstl_replace.h
index 05dee3f6a4f30c6..b1caf3fd4ac0a1a 100644
--- a/libcxx/include/__algorithm/pstl_replace.h
+++ b/libcxx/include/__algorithm/pstl_replace.h
@@ -24,6 +24,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -239,4 +242,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_REPLACE_H
diff --git a/libcxx/include/__algorithm/pstl_rotate_copy.h b/libcxx/include/__algorithm/pstl_rotate_copy.h
index 33dc9a3635f7e83..346aab1d4a55c0e 100644
--- a/libcxx/include/__algorithm/pstl_rotate_copy.h
+++ b/libcxx/include/__algorithm/pstl_rotate_copy.h
@@ -19,6 +19,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -77,4 +80,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_ROTATE_COPY_H
diff --git a/libcxx/include/__algorithm/pstl_sort.h b/libcxx/include/__algorithm/pstl_sort.h
index 3e71e0aa5ae0a1c..a931f768111a23b 100644
--- a/libcxx/include/__algorithm/pstl_sort.h
+++ b/libcxx/include/__algorithm/pstl_sort.h
@@ -25,6 +25,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -74,4 +77,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_SORT_H
diff --git a/libcxx/include/__algorithm/pstl_stable_sort.h b/libcxx/include/__algorithm/pstl_stable_sort.h
index c9d375535fc4505..8ea0bb3f9a8d590 100644
--- a/libcxx/include/__algorithm/pstl_stable_sort.h
+++ b/libcxx/include/__algorithm/pstl_stable_sort.h
@@ -23,6 +23,9 @@
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -53,4 +56,6 @@ _LIBCPP_END_NAMESPACE_STD
 
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_PSTL_STABLE_SORT_H
diff --git a/libcxx/include/__algorithm/pstl_transform.h b/libcxx/include/__algorithm/pstl_transform.h
index aad59d1f30e6b93..f95...
[truncated]

Copy link

github-actions bot commented Jan 23, 2024

⚠️ Python code formatter, darker found issues in your code. ⚠️

You can test this locally with the following command:
darker --check --diff -r bb8a8770e203ba027d141cd1200e93809ea66c8f...0f8dbb6883af705d9fa5ae14c6d924666d6a587c libcxx/test/libcxx/system_reserved_names.gen.py
View the diff from darker here.
--- system_reserved_names.gen.py	2024-01-24 13:59:01.000000 +0000
+++ system_reserved_names.gen.py	2024-01-24 14:01:28.034771 +0000
@@ -15,11 +15,12 @@
 import sys
 sys.path.append(sys.argv[1])
 from libcxx.header_information import lit_header_restrictions, public_headers
 
 for header in public_headers:
-  print(f"""\
+    print(
+        f"""\
 //--- {header}.compile.pass.cpp
 {lit_header_restrictions.get(header, '')}
 
 #define SYSTEM_RESERVED_NAME This name should not be used in libc++
 
@@ -170,6 +171,7 @@
 static_assert(__builtin_strcmp(STRINGIFY(min), STRINGIFY(SYSTEM_RESERVED_NAME)) == 0, "");
 static_assert(__builtin_strcmp(STRINGIFY(max), STRINGIFY(SYSTEM_RESERVED_NAME)) == 0, "");
 static_assert(__builtin_strcmp(STRINGIFY(move), STRINGIFY(SYSTEM_RESERVED_NAME)) == 0, "");
 static_assert(__builtin_strcmp(STRINGIFY(erase), STRINGIFY(SYSTEM_RESERVED_NAME)) == 0, "");
 static_assert(__builtin_strcmp(STRINGIFY(refresh), STRINGIFY(SYSTEM_RESERVED_NAME)) == 0, "");
-""")
+"""
+    )

@ldionne ldionne added this to the LLVM 18.0.X Release milestone Jan 23, 2024
@ldionne
Copy link
Member Author

ldionne commented Jan 24, 2024

This should be ready to review again. @philnik777 I used your suggestion for __builtin_strcmp and it's a lot better.

I would like to cherry-pick this back to LLVM 18 once merged.

@ldionne ldionne merged commit 7b46225 into llvm:main Jan 25, 2024
42 of 44 checks passed
@ldionne ldionne deleted the review/fix-push-macro branch January 25, 2024 20:48
ldionne added a commit to ldionne/llvm-project that referenced this pull request Jan 25, 2024
We recently noticed that the unwrap_iter.h file was pushing macros, but
it was pushing them again instead of popping them at the end of the
file. This led to libc++ basically swallowing any custom definition of
these macros in user code:

    #define min HELLO
    #include <algorithm>
    // min is not HELLO anymore, it's not defined

While investigating this issue, I noticed that our push/pop pragmas were
actually entirely wrong too. Indeed, instead of pushing macros like
`move`, we'd push `move(int, int)` in the pragma, which is not a valid
macro name. As a result, we would not actually push macros like `move`
-- instead we'd simply undefine them. This led to the following code not
working:

    #define move HELLO
    #include <algorithm>
    // move is not HELLO anymore

Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places, and
all of these headers would now need to do the push/pop dance.

This patch fixes all these issues. First, it adds a check that we don't
swallow important names like min, max, move or refresh as explained
above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros are what
we expect after including each header.

Second, it fixes the push/pop pragmas to work properly and adds missing
pragmas to all the files I could detect a failure in via the newly added
test.

rdar://121365472
(cherry picked from commit 7b46225)
ldionne added a commit to ldionne/llvm-project that referenced this pull request Jan 25, 2024
We recently noticed that the unwrap_iter.h file was pushing macros, but
it was pushing them again instead of popping them at the end of the
file. This led to libc++ basically swallowing any custom definition of
these macros in user code:

    #define min HELLO
    #include <algorithm>
    // min is not HELLO anymore, it's not defined

While investigating this issue, I noticed that our push/pop pragmas were
actually entirely wrong too. Indeed, instead of pushing macros like
`move`, we'd push `move(int, int)` in the pragma, which is not a valid
macro name. As a result, we would not actually push macros like `move`
-- instead we'd simply undefine them. This led to the following code not
working:

    #define move HELLO
    #include <algorithm>
    // move is not HELLO anymore

Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places, and
all of these headers would now need to do the push/pop dance.

This patch fixes all these issues. First, it adds a check that we don't
swallow important names like min, max, move or refresh as explained
above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros are what
we expect after including each header.

Second, it fixes the push/pop pragmas to work properly and adds missing
pragmas to all the files I could detect a failure in via the newly added
test.

rdar://121365472
(cherry picked from commit 7b46225)
tstellar pushed a commit that referenced this pull request Feb 2, 2024
We recently noticed that the unwrap_iter.h file was pushing macros, but
it was pushing them again instead of popping them at the end of the
file. This led to libc++ basically swallowing any custom definition of
these macros in user code:

    #define min HELLO
    #include <algorithm>
    // min is not HELLO anymore, it's not defined

While investigating this issue, I noticed that our push/pop pragmas were
actually entirely wrong too. Indeed, instead of pushing macros like
`move`, we'd push `move(int, int)` in the pragma, which is not a valid
macro name. As a result, we would not actually push macros like `move`
-- instead we'd simply undefine them. This led to the following code not
working:

    #define move HELLO
    #include <algorithm>
    // move is not HELLO anymore

Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places, and
all of these headers would now need to do the push/pop dance.

This patch fixes all these issues. First, it adds a check that we don't
swallow important names like min, max, move or refresh as explained
above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros are what
we expect after including each header.

Second, it fixes the push/pop pragmas to work properly and adds missing
pragmas to all the files I could detect a failure in via the newly added
test.

rdar://121365472
(cherry picked from commit 7b46225)
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
…lvm#79497)

We recently noticed that the unwrap_iter.h file was pushing macros, but
it was pushing them again instead of popping them at the end of the
file. This led to libc++ basically swallowing any custom definition of
these macros in user code:

    #define min HELLO
    #include <algorithm>
    // min is not HELLO anymore, it's not defined

While investigating this issue, I noticed that our push/pop pragmas were
actually entirely wrong too. Indeed, instead of pushing macros like
`move`, we'd push `move(int, int)` in the pragma, which is not a valid
macro name. As a result, we would not actually push macros like `move`
-- instead we'd simply undefine them. This led to the following code not
working:

    #define move HELLO
    #include <algorithm>
    // move is not HELLO anymore

Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places, and
all of these headers would now need to do the push/pop dance.

This patch fixes all these issues. First, it adds a check that we don't
swallow important names like min, max, move or refresh as explained
above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros are what
we expect after including each header.

Second, it fixes the push/pop pragmas to work properly and adds missing
pragmas to all the files I could detect a failure in via the newly added
test.

rdar://121365472
(cherry picked from commit 7b46225)
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
…lvm#79497)

We recently noticed that the unwrap_iter.h file was pushing macros, but
it was pushing them again instead of popping them at the end of the
file. This led to libc++ basically swallowing any custom definition of
these macros in user code:

    #define min HELLO
    #include <algorithm>
    // min is not HELLO anymore, it's not defined

While investigating this issue, I noticed that our push/pop pragmas were
actually entirely wrong too. Indeed, instead of pushing macros like
`move`, we'd push `move(int, int)` in the pragma, which is not a valid
macro name. As a result, we would not actually push macros like `move`
-- instead we'd simply undefine them. This led to the following code not
working:

    #define move HELLO
    #include <algorithm>
    // move is not HELLO anymore

Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places, and
all of these headers would now need to do the push/pop dance.

This patch fixes all these issues. First, it adds a check that we don't
swallow important names like min, max, move or refresh as explained
above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros are what
we expect after including each header.

Second, it fixes the push/pop pragmas to work properly and adds missing
pragmas to all the files I could detect a failure in via the newly added
test.

rdar://121365472
(cherry picked from commit 7b46225)
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
…lvm#79497)

We recently noticed that the unwrap_iter.h file was pushing macros, but
it was pushing them again instead of popping them at the end of the
file. This led to libc++ basically swallowing any custom definition of
these macros in user code:

    #define min HELLO
    #include <algorithm>
    // min is not HELLO anymore, it's not defined

While investigating this issue, I noticed that our push/pop pragmas were
actually entirely wrong too. Indeed, instead of pushing macros like
`move`, we'd push `move(int, int)` in the pragma, which is not a valid
macro name. As a result, we would not actually push macros like `move`
-- instead we'd simply undefine them. This led to the following code not
working:

    #define move HELLO
    #include <algorithm>
    // move is not HELLO anymore

Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places, and
all of these headers would now need to do the push/pop dance.

This patch fixes all these issues. First, it adds a check that we don't
swallow important names like min, max, move or refresh as explained
above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros are what
we expect after including each header.

Second, it fixes the push/pop pragmas to work properly and adds missing
pragmas to all the files I could detect a failure in via the newly added
test.

rdar://121365472
(cherry picked from commit 7b46225)
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
…lvm#79497)

We recently noticed that the unwrap_iter.h file was pushing macros, but
it was pushing them again instead of popping them at the end of the
file. This led to libc++ basically swallowing any custom definition of
these macros in user code:

    #define min HELLO
    #include <algorithm>
    // min is not HELLO anymore, it's not defined

While investigating this issue, I noticed that our push/pop pragmas were
actually entirely wrong too. Indeed, instead of pushing macros like
`move`, we'd push `move(int, int)` in the pragma, which is not a valid
macro name. As a result, we would not actually push macros like `move`
-- instead we'd simply undefine them. This led to the following code not
working:

    #define move HELLO
    #include <algorithm>
    // move is not HELLO anymore

Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places, and
all of these headers would now need to do the push/pop dance.

This patch fixes all these issues. First, it adds a check that we don't
swallow important names like min, max, move or refresh as explained
above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros are what
we expect after including each header.

Second, it fixes the push/pop pragmas to work properly and adds missing
pragmas to all the files I could detect a failure in via the newly added
test.

rdar://121365472
(cherry picked from commit 7b46225)
@pointhex pointhex mentioned this pull request May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants