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