1#!/usr/bin/env python
2
3import os
4from builtins import range
5from functools import reduce
6
7def get_libcxx_paths():
8  utils_path = os.path.dirname(os.path.abspath(__file__))
9  script_name = os.path.basename(__file__)
10  assert os.path.exists(utils_path)
11  src_root = os.path.dirname(utils_path)
12  include_path = os.path.join(src_root, 'include')
13  assert os.path.exists(include_path)
14  docs_path = os.path.join(src_root, 'docs')
15  assert os.path.exists(docs_path)
16  macro_test_path = os.path.join(src_root, 'test', 'std', 'language.support',
17                            'support.limits', 'support.limits.general')
18  assert os.path.exists(macro_test_path)
19  assert os.path.exists(os.path.join(macro_test_path, 'version.version.pass.cpp'))
20  return script_name, src_root, include_path, docs_path, macro_test_path
21
22script_name, source_root, include_path, docs_path, macro_test_path = get_libcxx_paths()
23
24def has_header(h):
25  h_path = os.path.join(include_path, h)
26  return os.path.exists(h_path)
27
28def add_version_header(tc):
29  tc["headers"].append("version")
30  return tc
31
32# ================  ============================================================
33# Field             Description
34# ================  ============================================================
35# name              The name of the feature-test macro.
36# values            A dict whose keys are C++ versions and whose values are the
37#                   value of the feature-test macro for that C++ version.
38#                   (TODO: This isn't a very clean model for feature-test
39#                   macros affected by multiple papers.)
40# headers           An array with the headers that should provide the
41#                   feature-test macro.
42# test_suite_guard  An optional string field. When this field is provided,
43#                   `libcxx_guard` must also be provided. This field is used
44#                   only to generate the unit tests for the feature-test macros.
45#                   It can't depend on macros defined in <__config> because the
46#                   `test/std/` parts of the test suite are intended to be
47#                   portable to any C++ standard library implementation, not
48#                   just libc++. It may depend on
49#                    * macros defined by the compiler itself, or
50#                    * macros generated by CMake.
51#                   In some cases we add
52#                   `&& !defined(_LIBCPP_AVAILABILITY_DISABLE_FTM_...)`
53#                   in order to make libc++ pass the tests on OSX; see D94983.
54# libcxx_guard      An optional string field. When this field is provided,
55#                   `test_suite_guard` must also be provided. This field is used
56#                   only to guard the feature-test macro in <version>. It may
57#                   be the same as `test_suite_guard`, or it may depend on
58#                   macros defined in <__config>.
59# unimplemented     An optional Boolean field with the value `True`. This field
60#                   is only used when a feature isn't fully implemented. Once
61#                   you've fully implemented the feature, you should remove
62#                   this field.
63# ================  ============================================================
64feature_test_macros = [ add_version_header(x) for x in [
65  {
66    "name": "__cpp_lib_addressof_constexpr",
67    "values": { "c++17": 201603 },
68    "headers": ["memory"],
69    "test_suite_guard": "TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700",
70    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_BUILTIN_ADDRESSOF)",
71  }, {
72    "name": "__cpp_lib_allocator_traits_is_always_equal",
73    "values": { "c++17": 201411 },
74    "headers": ["deque", "forward_list", "list", "map", "memory", "scoped_allocator", "set", "string", "unordered_map", "unordered_set", "vector"],
75  }, {
76    "name": "__cpp_lib_any",
77    "values": { "c++17": 201606 },
78    "headers": ["any"],
79  }, {
80    "name": "__cpp_lib_apply",
81    "values": { "c++17": 201603 },
82    "headers": ["tuple"],
83  }, {
84    "name": "__cpp_lib_array_constexpr",
85    "values": { "c++17": 201603, "c++20": 201811 },
86    "headers": ["array", "iterator"],
87  }, {
88    "name": "__cpp_lib_as_const",
89    "values": { "c++17": 201510 },
90    "headers": ["utility"],
91  }, {
92    "name": "__cpp_lib_assume_aligned",
93    "values": { "c++20": 201811 },
94    "headers": ["memory"],
95    "unimplemented": True,
96  }, {
97    "name": "__cpp_lib_atomic_flag_test",
98    "values": { "c++20": 201907 },
99    "headers": ["atomic"],
100    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
101    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
102  }, {
103    "name": "__cpp_lib_atomic_float",
104    "values": { "c++20": 201711 },
105    "headers": ["atomic"],
106    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
107    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
108    "unimplemented": True,
109  }, {
110    "name": "__cpp_lib_atomic_is_always_lock_free",
111    "values": { "c++17": 201603 },
112    "headers": ["atomic"],
113    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
114    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
115  }, {
116    "name": "__cpp_lib_atomic_lock_free_type_aliases",
117    "values": { "c++20": 201907 },
118    "headers": ["atomic"],
119    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
120    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
121  }, {
122    "name": "__cpp_lib_atomic_ref",
123    "values": { "c++20": 201806 },
124    "headers": ["atomic"],
125    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
126    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
127    "unimplemented": True,
128  }, {
129    "name": "__cpp_lib_atomic_shared_ptr",
130    "values": { "c++20": 201711 },
131    "headers": ["atomic"],
132    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
133    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
134    "unimplemented": True,
135  }, {
136    "name": "__cpp_lib_atomic_value_initialization",
137    "values": { "c++20": 201911 },
138    "headers": ["atomic", "memory"],
139    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
140    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
141  }, {
142    "name": "__cpp_lib_atomic_wait",
143    "values": { "c++20": 201907 },
144    "headers": ["atomic"],
145    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_atomic_wait)",
146    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_atomic_wait)",
147  }, {
148    "name": "__cpp_lib_barrier",
149    "values": { "c++20": 201907 },
150    "headers": ["barrier"],
151    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_barrier)",
152    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_barrier)",
153  }, {
154    "name": "__cpp_lib_bind_front",
155    "values": { "c++20": 201907 },
156    "headers": ["functional"],
157  }, {
158    "name": "__cpp_lib_bit_cast",
159    "values": { "c++20": 201806 },
160    "headers": ["bit"],
161    "unimplemented": True,
162  }, {
163    "name": "__cpp_lib_bitops",
164    "values": { "c++20": 201907 },
165    "headers": ["bit"],
166    "unimplemented": True,
167  }, {
168    "name": "__cpp_lib_bool_constant",
169    "values": { "c++17": 201505 },
170    "headers": ["type_traits"],
171  }, {
172    "name": "__cpp_lib_bounded_array_traits",
173    "values": { "c++20": 201902 },
174    "headers": ["type_traits"],
175  }, {
176    "name": "__cpp_lib_boyer_moore_searcher",
177    "values": { "c++17": 201603 },
178    "headers": ["functional"],
179    "unimplemented": True,
180  }, {
181    "name": "__cpp_lib_byte",
182    "values": { "c++17": 201603 },
183    "headers": ["cstddef"],
184  }, {
185    "name": "__cpp_lib_char8_t",
186    "values": { "c++20": 201811 },
187    "headers": ["atomic", "filesystem", "istream", "limits", "locale", "ostream", "string", "string_view"],
188    "test_suite_guard": "defined(__cpp_char8_t)",
189    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_CHAR8_T)",
190  }, {
191    "name": "__cpp_lib_chrono",
192    "values": { "c++17": 201611 },
193    "headers": ["chrono"],
194  }, {
195    "name": "__cpp_lib_chrono_udls",
196    "values": { "c++14": 201304 },
197    "headers": ["chrono"],
198  }, {
199    "name": "__cpp_lib_clamp",
200    "values": { "c++17": 201603 },
201    "headers": ["algorithm"],
202  }, {
203    "name": "__cpp_lib_complex_udls",
204    "values": { "c++14": 201309 },
205    "headers": ["complex"],
206  }, {
207    "name": "__cpp_lib_concepts",
208    "values": { "c++20": 202002 },
209    "headers": ["concepts"],
210  }, {
211    "name": "__cpp_lib_constexpr_algorithms",
212    "values": { "c++20": 201806 },
213    "headers": ["algorithm"],
214  }, {
215    "name": "__cpp_lib_constexpr_complex",
216    "values": { "c++20": 201711 },
217    "headers": ["complex"],
218    "unimplemented": True,
219  }, {
220    "name": "__cpp_lib_constexpr_dynamic_alloc",
221    "values": { "c++20": 201907 },
222    "headers": ["memory"],
223  }, {
224    "name": "__cpp_lib_constexpr_functional",
225    "values": { "c++20": 201907 },
226    "headers": ["functional"],
227  }, {
228    "name": "__cpp_lib_constexpr_iterator",
229    "values": { "c++20": 201811 },
230    "headers": ["iterator"],
231  }, {
232    "name": "__cpp_lib_constexpr_memory",
233    "values": { "c++20": 201811 },
234    "headers": ["memory"],
235  }, {
236    "name": "__cpp_lib_constexpr_numeric",
237    "values": { "c++20": 201911 },
238    "headers": ["numeric"],
239  }, {
240    "name": "__cpp_lib_constexpr_string",
241    "values": { "c++20": 201811 },  # because P1032R1 is implemented; but should become 201907 after P0980R1
242    "headers": ["string"],
243  }, {
244    "name": "__cpp_lib_constexpr_string_view",
245    "values": { "c++20": 201811 },
246    "headers": ["string_view"],
247  }, {
248    "name": "__cpp_lib_constexpr_tuple",
249    "values": { "c++20": 201811 },
250    "headers": ["tuple"],
251  }, {
252    "name": "__cpp_lib_constexpr_utility",
253    "values": { "c++20": 201811 },
254    "headers": ["utility"],
255  }, {
256    "name": "__cpp_lib_constexpr_vector",
257    "values": { "c++20": 201907 },
258    "headers": ["vector"],
259    "unimplemented": True,
260  }, {
261    "name": "__cpp_lib_coroutine",
262    "values": { "c++20": 201902 },
263    "headers": ["coroutine"],
264    "unimplemented": True,
265  }, {
266    "name": "__cpp_lib_destroying_delete",
267    "values": { "c++20": 201806 },
268    "headers": ["new"],
269    "test_suite_guard": "TEST_STD_VER > 17 && defined(__cpp_impl_destroying_delete) && __cpp_impl_destroying_delete >= 201806L",
270    "libcxx_guard": "_LIBCPP_STD_VER > 17 && defined(__cpp_impl_destroying_delete) && __cpp_impl_destroying_delete >= 201806L",
271  }, {
272    "name": "__cpp_lib_enable_shared_from_this",
273    "values": { "c++17": 201603 },
274    "headers": ["memory"],
275  }, {
276    "name": "__cpp_lib_endian",
277    "values": { "c++20": 201907 },
278    "headers": ["bit"],
279  }, {
280    "name": "__cpp_lib_erase_if",
281    "values": { "c++20": 202002 },
282    "headers": ["deque", "forward_list", "list", "map", "set", "string", "unordered_map", "unordered_set", "vector"],
283  }, {
284    "name": "__cpp_lib_exchange_function",
285    "values": { "c++14": 201304 },
286    "headers": ["utility"],
287  }, {
288    "name": "__cpp_lib_execution",
289    "values": { "c++17": 201603, "c++20": 201902 },
290    "headers": ["execution"],
291    "unimplemented": True,
292  }, {
293    "name": "__cpp_lib_filesystem",
294    "values": { "c++17": 201703 },
295    "headers": ["filesystem"],
296    "test_suite_guard": "!defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_filesystem)",
297    "libcxx_guard": "!defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_filesystem)"
298  }, {
299    "name": "__cpp_lib_format",
300    "values": { "c++20": 201907 },
301    "headers": ["format"],
302    "test_suite_guard": "!defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_format)",
303    "libcxx_guard": "!defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_format)",
304    "unimplemented": True,
305  }, {
306    "name": "__cpp_lib_gcd_lcm",
307    "values": { "c++17": 201606 },
308    "headers": ["numeric"],
309  }, {
310    "name": "__cpp_lib_generic_associative_lookup",
311    "values": { "c++14": 201304 },
312    "headers": ["map", "set"],
313  }, {
314    "name": "__cpp_lib_generic_unordered_lookup",
315    "values": { "c++20": 201811 },
316    "headers": ["unordered_map", "unordered_set"],
317  }, {
318    "name": "__cpp_lib_hardware_interference_size",
319    "values": { "c++17": 201703 },
320    "headers": ["new"],
321    "unimplemented": True,
322  }, {
323    "name": "__cpp_lib_has_unique_object_representations",
324    "values": { "c++17": 201606 },
325    "headers": ["type_traits"],
326    "test_suite_guard": "TEST_HAS_BUILTIN_IDENTIFIER(__has_unique_object_representations) || TEST_GCC_VER >= 700",
327    "libcxx_guard": "defined(_LIBCPP_HAS_UNIQUE_OBJECT_REPRESENTATIONS)",
328  }, {
329    "name": "__cpp_lib_hypot",
330    "values": { "c++17": 201603 },
331    "headers": ["cmath"],
332  }, {
333    "name": "__cpp_lib_incomplete_container_elements",
334    "values": { "c++17": 201505 },
335    "headers": ["forward_list", "list", "vector"],
336  }, {
337    "name": "__cpp_lib_int_pow2",
338    "values": { "c++20": 202002 },
339    "headers": ["bit"],
340  }, {
341    "name": "__cpp_lib_integer_comparison_functions",
342    "values": { "c++20": 202002 },
343    "headers": ["utility"],
344    "test_suite_guard": "defined(__cpp_concepts) && __cpp_concepts >= 201907L",
345    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_CONCEPTS)",
346  }, {
347    "name": "__cpp_lib_integer_sequence",
348    "values": { "c++14": 201304 },
349    "headers": ["utility"],
350  }, {
351    "name": "__cpp_lib_integral_constant_callable",
352    "values": { "c++14": 201304 },
353    "headers": ["type_traits"],
354  }, {
355    "name": "__cpp_lib_interpolate",
356    "values": { "c++20": 201902 },
357    "headers": ["cmath", "numeric"],
358  }, {
359    "name": "__cpp_lib_invoke",
360    "values": { "c++17": 201411 },
361    "headers": ["functional"],
362  }, {
363    "name": "__cpp_lib_is_aggregate",
364    "values": { "c++17": 201703 },
365    "headers": ["type_traits"],
366    "test_suite_guard": "TEST_HAS_BUILTIN_IDENTIFIER(__is_aggregate) || TEST_GCC_VER_NEW >= 7001",
367    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_IS_AGGREGATE)",
368  }, {
369    "name": "__cpp_lib_is_constant_evaluated",
370    "values": { "c++20": 201811 },
371    "headers": ["type_traits"],
372    "test_suite_guard": "TEST_HAS_BUILTIN(__builtin_is_constant_evaluated) || TEST_GCC_VER >= 900",
373    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_BUILTIN_IS_CONSTANT_EVALUATED)",
374  }, {
375    "name": "__cpp_lib_is_final",
376    "values": { "c++14": 201402 },
377    "headers": ["type_traits"],
378  }, {
379    "name": "__cpp_lib_is_invocable",
380    "values": { "c++17": 201703 },
381    "headers": ["type_traits"],
382  }, {
383    "name": "__cpp_lib_is_layout_compatible",
384    "values": { "c++20": 201907 },
385    "headers": ["type_traits"],
386    "unimplemented": True,
387  }, {
388    "name": "__cpp_lib_is_nothrow_convertible",
389    "values": { "c++20": 201806 },
390    "headers": ["type_traits"],
391  }, {
392    "name": "__cpp_lib_is_null_pointer",
393    "values": { "c++14": 201309 },
394    "headers": ["type_traits"],
395  }, {
396    "name": "__cpp_lib_is_pointer_interconvertible",
397    "values": { "c++20": 201907 },
398    "headers": ["type_traits"],
399    "unimplemented": True,
400  }, {
401    "name": "__cpp_lib_is_scoped_enum",
402    "values": { "c++2b": 202011 },
403    "headers": ["type_traits"],
404  }, {
405    "name": "__cpp_lib_is_swappable",
406    "values": { "c++17": 201603 },
407    "headers": ["type_traits"],
408  }, {
409    "name": "__cpp_lib_jthread",
410    "values": { "c++20": 201911 },
411    "headers": ["stop_token", "thread"],
412    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
413    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS)",
414    "unimplemented": True,
415  }, {
416    "name": "__cpp_lib_latch",
417    "values": { "c++20": 201907 },
418    "headers": ["latch"],
419    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_latch)",
420    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_latch)",
421  }, {
422    "name": "__cpp_lib_launder",
423    "values": { "c++17": 201606 },
424    "headers": ["new"],
425  }, {
426    "name": "__cpp_lib_list_remove_return_type",
427    "values": { "c++20": 201806 },
428    "headers": ["forward_list", "list"],
429  }, {
430    "name": "__cpp_lib_logical_traits",
431    "values": { "c++17": 201510 },
432    "headers": ["type_traits"],
433  }, {
434    "name": "__cpp_lib_make_from_tuple",
435    "values": { "c++17": 201606 },
436    "headers": ["tuple"],
437  }, {
438    "name": "__cpp_lib_make_reverse_iterator",
439    "values": { "c++14": 201402 },
440    "headers": ["iterator"],
441  }, {
442    "name": "__cpp_lib_make_unique",
443    "values": { "c++14": 201304 },
444    "headers": ["memory"],
445  }, {
446    "name": "__cpp_lib_map_try_emplace",
447    "values": { "c++17": 201411 },
448    "headers": ["map"],
449  }, {
450    "name": "__cpp_lib_math_constants",
451    "values": { "c++20": 201907 },
452    "headers": ["numbers"],
453    "test_suite_guard": "defined(__cpp_concepts) && __cpp_concepts >= 201907L",
454    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_CONCEPTS)",
455  }, {
456    "name": "__cpp_lib_math_special_functions",
457    "values": { "c++17": 201603 },
458    "headers": ["cmath"],
459    "unimplemented": True,
460  }, {
461    "name": "__cpp_lib_memory_resource",
462    "values": { "c++17": 201603 },
463    "headers": ["memory_resource"],
464    "unimplemented": True,
465  }, {
466    "name": "__cpp_lib_node_extract",
467    "values": { "c++17": 201606 },
468    "headers": ["map", "set", "unordered_map", "unordered_set"],
469  }, {
470    "name": "__cpp_lib_nonmember_container_access",
471    "values": { "c++17": 201411 },
472    "headers": ["array", "deque", "forward_list", "iterator", "list", "map", "regex", "set", "string", "unordered_map", "unordered_set", "vector"],
473  }, {
474    "name": "__cpp_lib_not_fn",
475    "values": { "c++17": 201603 },
476    "headers": ["functional"],
477  }, {
478    "name": "__cpp_lib_null_iterators",
479    "values": { "c++14": 201304 },
480    "headers": ["iterator"],
481  }, {
482    "name": "__cpp_lib_optional",
483    "values": { "c++17": 201606 },
484    "headers": ["optional"],
485  }, {
486    "name": "__cpp_lib_parallel_algorithm",
487    "values": { "c++17": 201603 },
488    "headers": ["algorithm", "numeric"],
489    "unimplemented": True,
490  }, {
491    "name": "__cpp_lib_polymorphic_allocator",
492    "values": { "c++20": 201902 },
493    "headers": ["memory"],
494    "unimplemented": True,
495  }, {
496    "name": "__cpp_lib_quoted_string_io",
497    "values": { "c++14": 201304 },
498    "headers": ["iomanip"],
499  }, {
500    "name": "__cpp_lib_ranges",
501    "values": { "c++20": 201811 },
502    "headers": ["algorithm", "functional", "iterator", "memory", "ranges"],
503    "unimplemented": True,
504  }, {
505    "name": "__cpp_lib_raw_memory_algorithms",
506    "values": { "c++17": 201606 },
507    "headers": ["memory"],
508  }, {
509    "name": "__cpp_lib_remove_cvref",
510    "values": { "c++20": 201711 },
511    "headers": ["type_traits"],
512  }, {
513    "name": "__cpp_lib_result_of_sfinae",
514    "values": { "c++14": 201210 },
515    "headers": ["functional", "type_traits"],
516  }, {
517    "name": "__cpp_lib_robust_nonmodifying_seq_ops",
518    "values": { "c++14": 201304 },
519    "headers": ["algorithm"],
520  }, {
521    "name": "__cpp_lib_sample",
522    "values": { "c++17": 201603 },
523    "headers": ["algorithm"],
524  }, {
525    "name": "__cpp_lib_scoped_lock",
526    "values": { "c++17": 201703 },
527    "headers": ["mutex"],
528  }, {
529    "name": "__cpp_lib_semaphore",
530    "values": { "c++20": 201907 },
531    "headers": ["semaphore"],
532    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_semaphore)",
533    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_semaphore)",
534  }, {
535    "name": "__cpp_lib_shared_mutex",
536    "values": { "c++17": 201505 },
537    "headers": ["shared_mutex"],
538    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_mutex)",
539    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_mutex)",
540  }, {
541    "name": "__cpp_lib_shared_ptr_arrays",
542    "values": { "c++17": 201611 },
543    "headers": ["memory"],
544  }, {
545    "name": "__cpp_lib_shared_ptr_weak_type",
546    "values": { "c++17": 201606 },
547    "headers": ["memory"],
548  }, {
549    "name": "__cpp_lib_shared_timed_mutex",
550    "values": { "c++14": 201402 },
551    "headers": ["shared_mutex"],
552    "test_suite_guard": "!defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_timed_mutex)",
553    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_timed_mutex)",
554  }, {
555    "name": "__cpp_lib_shift",
556    "values": { "c++20": 201806 },
557    "headers": ["algorithm"],
558  }, {
559    "name": "__cpp_lib_smart_ptr_for_overwrite",
560    "values": { "c++20": 202002 },
561    "headers": ["memory"],
562    "unimplemented": True,
563  }, {
564    "name": "__cpp_lib_source_location",
565    "values": { "c++20": 201907 },
566    "headers": ["source_location"],
567    "unimplemented": True,
568  }, {
569    "name": "__cpp_lib_span",
570    "values": { "c++20": 202002 },
571    "headers": ["span"],
572  }, {
573    "name": "__cpp_lib_ssize",
574    "values": { "c++20": 201902 },
575    "headers": ["iterator"],
576  }, {
577    "name": "__cpp_lib_stacktrace",
578    "values": { "c++2b": 202011 },
579    "headers": ["stacktrace"],
580    "unimplemented": True,
581  }, {
582    "name": "__cpp_lib_starts_ends_with",
583    "values": { "c++20": 201711 },
584    "headers": ["string", "string_view"],
585  }, {
586    "name": "__cpp_lib_stdatomic_h",
587    "values": { "c++2b": 202011 },
588    "headers": ["stdatomic.h"],
589    "unimplemented": True,
590  }, {
591    "name": "__cpp_lib_string_contains",
592    "values": { "c++2b": 202011 },
593    "headers": ["string", "string_view"],
594  }, {
595    "name": "__cpp_lib_string_udls",
596    "values": { "c++14": 201304 },
597    "headers": ["string"],
598  }, {
599    "name": "__cpp_lib_string_view",
600    "values": { "c++17": 201606, "c++20": 201803 },
601    "headers": ["string", "string_view"],
602  }, {
603    "name": "__cpp_lib_syncbuf",
604    "values": { "c++20": 201803 },
605    "headers": ["syncstream"],
606    "unimplemented": True,
607  }, {
608    "name": "__cpp_lib_three_way_comparison",
609    "values": { "c++20": 201907 },
610    "headers": ["compare"],
611    "unimplemented": True,
612  }, {
613    "name": "__cpp_lib_to_address",
614    "values": { "c++20": 201711 },
615    "headers": ["memory"],
616  }, {
617    "name": "__cpp_lib_to_array",
618    "values": { "c++20": 201907 },
619    "headers": ["array"],
620  }, {
621    "name": "__cpp_lib_to_chars",
622    "values": { "c++17": 201611 },
623    "headers": ["utility"],
624    "unimplemented": True,
625  }, {
626    "name": "__cpp_lib_to_underlying",
627    "values": { "c++2b": 202102 },
628    "headers": ["utility"],
629  }, {
630    "name": "__cpp_lib_transformation_trait_aliases",
631    "values": { "c++14": 201304 },
632    "headers": ["type_traits"],
633  }, {
634    "name": "__cpp_lib_transparent_operators",
635    "values": { "c++14": 201210, "c++17": 201510 },
636    "headers": ["functional", "memory"],
637  }, {
638    "name": "__cpp_lib_tuple_element_t",
639    "values": { "c++14": 201402 },
640    "headers": ["tuple"],
641  }, {
642    "name": "__cpp_lib_tuples_by_type",
643    "values": { "c++14": 201304 },
644    "headers": ["tuple", "utility"],
645  }, {
646    "name": "__cpp_lib_type_trait_variable_templates",
647    "values": { "c++17": 201510 },
648    "headers": ["type_traits"],
649  }, {
650    "name": "__cpp_lib_uncaught_exceptions",
651    "values": { "c++17": 201411 },
652    "headers": ["exception"],
653  }, {
654    "name": "__cpp_lib_unordered_map_try_emplace",
655    "values": { "c++17": 201411 },
656    "headers": ["unordered_map"],
657  }, {
658    "name": "__cpp_lib_unwrap_ref",
659    "values": { "c++20": 201811 },
660    "headers": ["functional"],
661  }, {
662    "name": "__cpp_lib_variant",
663    "values": { "c++17": 202102 },
664    "headers": ["variant"],
665  }, {
666    "name": "__cpp_lib_void_t",
667    "values": { "c++17": 201411 },
668    "headers": ["type_traits"],
669  }
670]]
671
672assert feature_test_macros == sorted(feature_test_macros, key=lambda tc: tc["name"])
673assert all(tc["headers"] == sorted(tc["headers"]) for tc in feature_test_macros)
674assert all(("libcxx_guard" in tc) == ("test_suite_guard" in tc) for tc in feature_test_macros)
675assert all(all(key in ["name", "values", "headers", "libcxx_guard", "test_suite_guard", "unimplemented"] for key in tc.keys()) for tc in feature_test_macros)
676
677# Map from each header to the Lit annotations that should be used for
678# tests that include that header.
679#
680# For example, when threads are not supported, any feature-test-macro test
681# that includes <thread> should be marked as UNSUPPORTED, because including
682# <thread> is a hard error in that case.
683lit_markup = {
684  "atomic": ["UNSUPPORTED: libcpp-has-no-threads"],
685  "barrier": ["UNSUPPORTED: libcpp-has-no-threads"],
686  "filesystem": ["UNSUPPORTED: libcpp-has-no-filesystem-library"],
687  "iomanip": ["UNSUPPORTED: libcpp-has-no-localization"],
688  "istream": ["UNSUPPORTED: libcpp-has-no-localization"],
689  "latch": ["UNSUPPORTED: libcpp-has-no-threads"],
690  "locale": ["UNSUPPORTED: libcpp-has-no-localization"],
691  "ostream": ["UNSUPPORTED: libcpp-has-no-localization"],
692  "regex": ["UNSUPPORTED: libcpp-has-no-localization"],
693  "semaphore": ["UNSUPPORTED: libcpp-has-no-threads"],
694  "shared_mutex": ["UNSUPPORTED: libcpp-has-no-threads"],
695  "thread": ["UNSUPPORTED: libcpp-has-no-threads"],
696}
697
698def get_std_dialects():
699  std_dialects = ['c++14', 'c++17', 'c++20', 'c++2b']
700  return list(std_dialects)
701
702def get_first_std(d):
703    for s in get_std_dialects():
704        if s in d.keys():
705            return s
706    return None
707
708def get_last_std(d):
709  rev_dialects = get_std_dialects()
710  rev_dialects.reverse()
711  for s in rev_dialects:
712    if s in d.keys():
713      return s
714  return None
715
716def get_std_before(d, std):
717  std_dialects = get_std_dialects()
718  candidates = std_dialects[0:std_dialects.index(std)]
719  candidates.reverse()
720  for cand in candidates:
721    if cand in d.keys():
722      return cand
723  return None
724
725def get_value_before(d, std):
726  new_std = get_std_before(d, std)
727  if new_std is None:
728    return None
729  return d[new_std]
730
731def get_for_std(d, std):
732  # This catches the C++11 case for which there should be no defined feature
733  # test macros.
734  std_dialects = get_std_dialects()
735  if std not in std_dialects:
736    return None
737  # Find the value for the newest C++ dialect between C++14 and std
738  std_list = list(std_dialects[0:std_dialects.index(std)+1])
739  std_list.reverse()
740  for s in std_list:
741    if s in d.keys():
742      return d[s]
743  return None
744
745def get_std_number(std):
746    return std.replace('c++', '')
747
748"""
749  Functions to produce the <version> header
750"""
751
752def produce_macros_definition_for_std(std):
753  result = ""
754  indent = 55
755  for tc in feature_test_macros:
756    if std not in tc["values"]:
757      continue
758    inner_indent = 1
759    if 'test_suite_guard' in tc.keys():
760      result += "# if %s\n" % tc["libcxx_guard"]
761      inner_indent += 2
762    if get_value_before(tc["values"], std) is not None:
763      assert 'test_suite_guard' not in tc.keys()
764      result += "# undef  %s\n" % tc["name"]
765    line = "#%sdefine %s" % ((" " * inner_indent), tc["name"])
766    line += " " * (indent - len(line))
767    line += " %sL" % tc["values"][std]
768    if 'unimplemented' in tc.keys():
769      line = "// " + line
770    result += line
771    result += "\n"
772    if 'test_suite_guard' in tc.keys():
773      result += "# endif\n"
774  return result.strip()
775
776def produce_macros_definitions():
777  macro_definition_template = """#if _LIBCPP_STD_VER > {previous_std_number}
778{macro_definition}
779#endif"""
780
781  macros_definitions = []
782  previous_std_number = '11'
783  for std in get_std_dialects():
784    macros_definitions.append(
785      macro_definition_template.format(previous_std_number=previous_std_number,
786                                       macro_definition=produce_macros_definition_for_std(std)))
787    previous_std_number = get_std_number(std)
788
789  return '\n\n'.join(macros_definitions)
790
791def chunks(l, n):
792  """Yield successive n-sized chunks from l."""
793  for i in range(0, len(l), n):
794    yield l[i:i + n]
795
796def produce_version_synopsis():
797  indent = 56
798  header_indent = 56 + len("20XXYYL ")
799  result = ""
800  def indent_to(s, val):
801    if len(s) >= val:
802      return s
803    s += " " * (val - len(s))
804    return s
805  line = indent_to("Macro name", indent) + "Value"
806  line = indent_to(line, header_indent) + "Headers"
807  result += line + "\n"
808  for tc in feature_test_macros:
809    prev_defined_std = get_last_std(tc["values"])
810    line = "{name: <{indent}}{value}L ".format(name=tc['name'], indent=indent,
811                                               value=tc["values"][prev_defined_std])
812    headers = list(tc["headers"])
813    headers.remove("version")
814    for chunk in chunks(headers, 3):
815      line = indent_to(line, header_indent)
816      chunk = ['<%s>' % header for header in chunk]
817      line += ' '.join(chunk)
818      result += line
819      result += "\n"
820      line = ""
821    while True:
822      prev_defined_std = get_std_before(tc["values"], prev_defined_std)
823      if prev_defined_std is None:
824        break
825      result += "%s%sL // %s\n" % (indent_to("", indent), tc["values"][prev_defined_std],
826                                prev_defined_std.replace("c++", "C++"))
827  return result
828
829
830def produce_version_header():
831  template="""// -*- C++ -*-
832//===--------------------------- version ----------------------------------===//
833//
834// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
835// See https://llvm.org/LICENSE.txt for license information.
836// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
837//
838//===----------------------------------------------------------------------===//
839
840#ifndef _LIBCPP_VERSIONH
841#define _LIBCPP_VERSIONH
842
843/*
844  version synopsis
845
846{synopsis}
847
848*/
849
850#include <__config>
851
852#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
853#pragma GCC system_header
854#endif
855
856// clang-format off
857
858{cxx_macros}
859
860// clang-format on
861
862#endif // _LIBCPP_VERSIONH
863"""
864
865  version_str = template.format(
866      synopsis=produce_version_synopsis().strip(),
867      cxx_macros=produce_macros_definitions())
868  version_header_path = os.path.join(include_path, 'version')
869  with open(version_header_path, 'w', newline='\n') as f:
870    f.write(version_str)
871
872
873"""
874    Functions to produce test files
875"""
876
877test_types = {
878  "undefined": """
879# ifdef {name}
880#   error "{name} should not be defined before {std_first}"
881# endif
882""",
883
884  "test_suite_guard": """
885# if {test_suite_guard}
886#   ifndef {name}
887#     error "{name} should be defined in {std}"
888#   endif
889#   if {name} != {value}
890#     error "{name} should have the value {value} in {std}"
891#   endif
892# else
893#   ifdef {name}
894#     error "{name} should not be defined when {test_suite_guard} is not defined!"
895#   endif
896# endif
897""",
898
899  "unimplemented": """
900# if !defined(_LIBCPP_VERSION)
901#   ifndef {name}
902#     error "{name} should be defined in {std}"
903#   endif
904#   if {name} != {value}
905#     error "{name} should have the value {value} in {std}"
906#   endif
907# else // _LIBCPP_VERSION
908#   ifdef {name}
909#     error "{name} should not be defined because it is unimplemented in libc++!"
910#   endif
911# endif
912""",
913
914  "defined": """
915# ifndef {name}
916#   error "{name} should be defined in {std}"
917# endif
918# if {name} != {value}
919#   error "{name} should have the value {value} in {std}"
920# endif
921"""
922}
923
924def generate_std_test(test_list, std):
925  result = ""
926  for tc in test_list:
927    val = get_for_std(tc["values"], std)
928    if val is not None:
929      val = "%sL" % val
930    if val is None:
931      result += test_types["undefined"].format(name=tc["name"], std_first=get_first_std(tc["values"]))
932    elif 'unimplemented' in tc.keys():
933      result += test_types["unimplemented"].format(name=tc["name"], value=val, std=std)
934    elif "test_suite_guard" in tc.keys():
935      result += test_types["test_suite_guard"].format(name=tc["name"], value=val, std=std, test_suite_guard=tc["test_suite_guard"])
936    else:
937      result +=  test_types["defined"].format(name=tc["name"], value=val, std=std)
938  return result.strip()
939
940def generate_std_tests(test_list):
941  std_tests_template = """#if TEST_STD_VER < {first_std_number}
942
943{pre_std_test}
944
945{other_std_tests}
946
947#elif TEST_STD_VER > {penultimate_std_number}
948
949{last_std_test}
950
951#endif // TEST_STD_VER > {penultimate_std_number}"""
952
953  std_dialects = get_std_dialects()
954  assert not get_std_number(std_dialects[-1]).isnumeric()
955
956  other_std_tests = []
957  for std in std_dialects[:-1]:
958    other_std_tests.append('#elif TEST_STD_VER == ' + get_std_number(std))
959    other_std_tests.append(generate_std_test(test_list, std))
960
961  std_tests = std_tests_template.format(first_std_number=get_std_number(std_dialects[0]),
962                                        pre_std_test=generate_std_test(test_list, 'c++11'),
963                                        other_std_tests='\n\n'.join(other_std_tests),
964                                        penultimate_std_number=get_std_number(std_dialects[-2]),
965                                        last_std_test=generate_std_test(test_list, std_dialects[-1]))
966
967  return std_tests
968
969def generate_synopsis(test_list):
970    max_name_len = max([len(tc["name"]) for tc in test_list])
971    indent = max_name_len + 8
972    def mk_line(prefix, suffix):
973        return "{prefix: <{max_len}}{suffix}\n".format(prefix=prefix, suffix=suffix,
974        max_len=indent)
975    result = ""
976    result += mk_line("/*  Constant", "Value")
977    for tc in test_list:
978        prefix = "    %s" % tc["name"]
979        for std in [s for s in get_std_dialects() if s in tc["values"].keys()]:
980            result += mk_line(prefix, "%sL [%s]" % (tc["values"][std], std.replace("c++", "C++")))
981            prefix = ""
982    result += "*/"
983    return result
984
985def produce_tests():
986  headers = set([h for tc in feature_test_macros for h in tc["headers"]])
987  for h in headers:
988    test_list = [tc for tc in feature_test_macros if h in tc["headers"]]
989    if not has_header(h):
990      for tc in test_list:
991        assert 'unimplemented' in tc.keys()
992      continue
993    markup = '\n'.join('// ' + tag for tag in lit_markup.get(h, []))
994    test_body = \
995"""//===----------------------------------------------------------------------===//
996//
997// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
998// See https://llvm.org/LICENSE.txt for license information.
999// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
1000//
1001//===----------------------------------------------------------------------===//
1002//
1003// WARNING: This test was generated by {script_name}
1004// and should not be edited manually.
1005//
1006// clang-format off
1007{markup}
1008// <{header}>
1009
1010// Test the feature test macros defined by <{header}>
1011
1012{synopsis}
1013
1014#include <{header}>
1015#include "test_macros.h"
1016
1017{cxx_tests}
1018
1019int main(int, char**) {{ return 0; }}
1020""".format(script_name=script_name,
1021           header=h,
1022           markup=('\n{}\n'.format(markup) if markup else ''),
1023           synopsis=generate_synopsis(test_list),
1024           cxx_tests=generate_std_tests(test_list))
1025    test_name = "{header}.version.pass.cpp".format(header=h)
1026    out_path = os.path.join(macro_test_path, test_name)
1027    with open(out_path, 'w', newline='\n') as f:
1028      f.write(test_body)
1029
1030"""
1031    Produce documentation for the feature test macros
1032"""
1033
1034def make_widths(grid):
1035  widths = []
1036  for i in range(0, len(grid[0])):
1037    cell_width = 2 + max(reduce(lambda x,y: x+y, [[len(row[i])] for row in grid], []))
1038    widths += [cell_width]
1039  return widths
1040
1041def create_table(grid, indent):
1042  indent_str = ' '*indent
1043  col_widths = make_widths(grid)
1044  result = [indent_str + add_divider(col_widths, 2)]
1045  header_flag = 2
1046  for row_i in range(0, len(grid)):
1047    row = grid[row_i]
1048    line = indent_str + ' '.join([pad_cell(row[i], col_widths[i]) for i in range(0, len(row))])
1049    result.append(line.rstrip())
1050    is_cxx_header = row[0].startswith('**')
1051    if row_i == len(grid) - 1:
1052      header_flag = 2
1053    separator = indent_str + add_divider(col_widths, 1 if is_cxx_header else header_flag)
1054    result.append(separator.rstrip())
1055    header_flag = 0
1056  return '\n'.join(result)
1057
1058def add_divider(widths, header_flag):
1059  if header_flag == 2:
1060    return ' '.join(['='*w for w in widths])
1061  if header_flag == 1:
1062    return '-'.join(['-'*w for w in widths])
1063  else:
1064    return ' '.join(['-'*w for w in widths])
1065
1066def pad_cell(s, length, left_align=True):
1067  padding = ((length - len(s)) * ' ')
1068  return s + padding
1069
1070
1071def get_status_table():
1072  table = [["Macro Name", "Value"]]
1073  for std in get_std_dialects():
1074    table += [["**" + std.replace("c++", "C++ ") + "**", ""]]
1075    for tc in feature_test_macros:
1076      if std not in tc["values"].keys():
1077        continue
1078      value = "``%sL``" % tc["values"][std]
1079      if 'unimplemented' in tc.keys():
1080        value = '*unimplemented*'
1081      table += [["``%s``" % tc["name"], value]]
1082  return table
1083
1084def produce_docs():
1085  doc_str = """.. _FeatureTestMacroTable:
1086
1087==========================
1088Feature Test Macro Support
1089==========================
1090
1091.. contents::
1092   :local:
1093
1094Overview
1095========
1096
1097This file documents the feature test macros currently supported by libc++.
1098
1099.. _feature-status:
1100
1101Status
1102======
1103
1104.. table:: Current Status
1105     :name: feature-status-table
1106     :widths: auto
1107
1108{status_tables}
1109
1110""".format(status_tables=create_table(get_status_table(), 4))
1111
1112  table_doc_path = os.path.join(docs_path, 'FeatureTestMacroTable.rst')
1113  with open(table_doc_path, 'w', newline='\n') as f:
1114    f.write(doc_str)
1115
1116def main():
1117  produce_version_header()
1118  produce_tests()
1119  produce_docs()
1120
1121
1122if __name__ == '__main__':
1123  main()
1124