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