6 def get_libcxx_paths():
7 script_path = os.path.dirname(os.path.abspath(__file__))
8 script_name = os.path.basename(__file__)
9 assert os.path.exists(script_path)
11 src_root = script_path
12 for _ in xrange(0, 5):
13 src_root = os.path.dirname(src_root)
14 include_path = os.path.join(src_root, 'include')
15 assert os.path.exists(include_path)
16 docs_path = os.path.join(src_root, 'docs')
17 assert os.path.exists(docs_path)
18 return script_path, script_name, src_root, include_path, docs_path
21 script_path, script_name, source_root, include_path, docs_path = get_libcxx_paths()
24 h_path = os.path.join(include_path, h)
25 return os.path.exists(h_path)
27 def add_version_header(tc):
28 tc["headers"].append("version")
31 feature_test_macros = sorted([ add_version_header(x) for x in [
33 {"name": "__cpp_lib_integer_sequence",
37 "headers": ["utility"],
39 {"name": "__cpp_lib_exchange_function",
43 "headers": ["utility"],
45 {"name": "__cpp_lib_tuples_by_type",
49 "headers": ["utility", "tuple"],
51 {"name": "__cpp_lib_tuple_element_t",
57 {"name": "__cpp_lib_make_unique",
61 "headers": ["memory"],
63 {"name": "__cpp_lib_transparent_operators",
68 "headers": ["functional"],
70 {"name": "__cpp_lib_integral_constant_callable",
74 "headers": ["type_traits"],
76 {"name": "__cpp_lib_transformation_trait_aliases",
80 "headers": ["type_traits"]
82 {"name": "__cpp_lib_result_of_sfinae",
86 "headers": ["functional", "type_traits"]
88 {"name": "__cpp_lib_is_final",
92 "headers": ["type_traits"]
94 {"name": "__cpp_lib_is_null_pointer",
98 "headers": ["type_traits"]
100 {"name": "__cpp_lib_chrono_udls",
104 "headers": ["chrono"]
106 {"name": "__cpp_lib_string_udls",
110 "headers": ["string"]
112 {"name": "__cpp_lib_generic_associative_lookup",
116 "headers": ["map", "set"]
118 {"name": "__cpp_lib_null_iterators",
122 "headers": ["iterator"]
124 {"name": "__cpp_lib_make_reverse_iterator",
128 "headers": ["iterator"]
130 {"name": "__cpp_lib_robust_nonmodifying_seq_ops",
134 "headers": ["algorithm"]
136 {"name": "__cpp_lib_complex_udls",
140 "headers": ["complex"]
142 {"name": "__cpp_lib_quoted_string_io",
146 "headers": ["iomanip"]
148 {"name": "__cpp_lib_shared_timed_mutex",
152 "headers": ["shared_mutex"],
153 "depends": "!defined(_LIBCPP_HAS_NO_THREADS)",
154 "internal_depends": "!defined(_LIBCPP_HAS_NO_THREADS)",
157 {"name": "__cpp_lib_atomic_is_always_lock_free",
161 "headers": ["atomic"],
162 "depends": "!defined(_LIBCPP_HAS_NO_THREADS)",
163 "internal_depends": "!defined(_LIBCPP_HAS_NO_THREADS)",
165 {"name": "__cpp_lib_filesystem",
169 "headers": ["filesystem"]
171 {"name": "__cpp_lib_invoke",
175 "headers": ["functional"]
177 {"name": "__cpp_lib_void_t",
181 "headers": ["type_traits"]
183 {"name": "__cpp_lib_node_extract",
187 "headers": ["map", "set", "unordered_map", "unordered_set"]
189 {"name": "__cpp_lib_byte",
193 "headers": ["cstddef"],
195 {"name": "__cpp_lib_hardware_interference_size",
201 {"name": "__cpp_lib_launder",
207 {"name": "__cpp_lib_uncaught_exceptions",
211 "headers": ["exception"],
213 {"name": "__cpp_lib_as_const",
217 "headers": ["utility"],
219 {"name": "__cpp_lib_make_from_tuple",
223 "headers": ["tuple"],
225 {"name": "__cpp_lib_apply",
229 "headers": ["tuple"],
231 {"name": "__cpp_lib_optional",
235 "headers": ["optional"],
237 {"name": "__cpp_lib_variant",
241 "headers": ["variant"],
243 {"name": "__cpp_lib_any",
249 {"name": "__cpp_lib_addressof_constexpr",
253 "headers": ["memory"],
254 "depends": "TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700",
255 "internal_depends": "!defined(_LIBCPP_HAS_NO_BUILTIN_ADDRESSOF)",
257 {"name": "__cpp_lib_raw_memory_algorithms",
261 "headers": ["memory"],
263 {"name": "__cpp_lib_enable_shared_from_this",
267 "headers": ["memory"],
269 {"name": "__cpp_lib_shared_ptr_weak_type",
273 "headers": ["memory"],
275 {"name": "__cpp_lib_shared_ptr_arrays",
279 "headers": ["memory"],
280 "unimplemented": True,
282 {"name": "__cpp_lib_memory_resource",
286 "headers": ["memory_resource"],
287 "unimplemented": True,
289 {"name": "__cpp_lib_boyer_moore_searcher",
293 "headers": ["functional"],
294 "unimplemented": True,
296 {"name": "__cpp_lib_not_fn",
300 "headers": ["functional"],
302 {"name": "__cpp_lib_bool_constant",
306 "headers": ["type_traits"],
308 {"name": "__cpp_lib_type_trait_variable_templates",
312 "headers": ["type_traits"],
314 {"name": "__cpp_lib_logical_traits",
318 "headers": ["type_traits"],
320 {"name": "__cpp_lib_is_swappable",
324 "headers": ["type_traits"],
326 {"name": "__cpp_lib_is_invocable",
330 "headers": ["type_traits"],
332 {"name": "__cpp_lib_has_unique_object_representations",
336 "headers": ["type_traits"],
337 "depends": "TEST_HAS_BUILTIN_IDENTIFIER(__has_unique_object_representations) || TEST_GCC_VER >= 700",
338 "internal_depends": "defined(_LIBCPP_HAS_UNIQUE_OBJECT_REPRESENTATIONS)",
340 {"name": "__cpp_lib_is_aggregate",
344 "headers": ["type_traits"],
345 "depends": "TEST_HAS_BUILTIN_IDENTIFIER(__is_aggregate) || TEST_GCC_VER_NEW >= 7001",
346 "internal_depends": "!defined(_LIBCPP_HAS_NO_IS_AGGREGATE)",
348 {"name": "__cpp_lib_chrono",
352 "headers": ["chrono"],
354 {"name": "__cpp_lib_execution",
358 "headers": ["execution"],
359 "unimplemented": True
361 {"name": "__cpp_lib_parallel_algorithm",
365 "headers": ["algorithm", "numeric"],
366 "unimplemented": True,
368 {"name": "__cpp_lib_to_chars",
372 "headers": ["utility"],
373 "unimplemented": True,
375 {"name": "__cpp_lib_string_view",
379 "headers": ["string", "string_view"],
381 {"name": "__cpp_lib_allocator_traits_is_always_equal",
385 "headers": ["memory", "scoped_allocator", "string", "deque", "forward_list", "list", "vector", "map", "set", "unordered_map", "unordered_set"],
387 {"name": "__cpp_lib_incomplete_container_elements",
391 "headers": ["forward_list", "list", "vector"],
393 {"name": "__cpp_lib_map_try_emplace",
399 {"name": "__cpp_lib_unordered_map_try_emplace",
403 "headers": ["unordered_map"],
405 {"name": "__cpp_lib_array_constexpr",
409 "headers": ["iterator", "array"],
411 {"name": "__cpp_lib_nonmember_container_access",
415 "headers": ["iterator", "array", "deque", "forward_list", "list", "map", "regex",
416 "set", "string", "unordered_map", "unordered_set", "vector"],
418 {"name": "__cpp_lib_sample",
422 "headers": ["algorithm"],
424 {"name": "__cpp_lib_clamp",
428 "headers": ["algorithm"],
430 {"name": "__cpp_lib_gcd_lcm",
434 "headers": ["numeric"],
436 {"name": "__cpp_lib_hypot",
440 "headers": ["cmath"],
442 {"name": "__cpp_lib_math_special_functions",
446 "headers": ["cmath"],
447 "unimplemented": True,
449 {"name": "__cpp_lib_shared_mutex",
453 "headers": ["shared_mutex"],
454 "depends": "!defined(_LIBCPP_HAS_NO_THREADS)",
455 "internal_depends": "!defined(_LIBCPP_HAS_NO_THREADS)",
457 {"name": "__cpp_lib_scoped_lock",
461 "headers": ["mutex"],
464 {"name": "__cpp_lib_char8_t",
468 "headers": ["atomic", "filesystem", "istream", "limits", "locale", "ostream",
469 "string", "string_view"],
470 "depends": "defined(__cpp_char8_t)",
471 "internal_depends": "!defined(_LIBCPP_NO_HAS_CHAR8_T)",
473 {"name": "__cpp_lib_erase_if",
477 "headers": ["string", "deque", "forward_list", "list", "vector", "map",
478 "set", "unordered_map", "unordered_set"]
480 {"name": "__cpp_lib_destroying_delete",
485 "unimplemented": True,
487 {"name": "__cpp_lib_three_way_comparison",
491 "headers": ["compare"],
492 "unimplemented": True,
494 {"name": "__cpp_lib_concepts",
498 "headers": ["concepts"],
499 "unimplemented": True,
501 {"name": "__cpp_lib_constexpr_swap_algorithms",
505 "headers": ["algorithm"],
506 "unimplemented": True,
508 {"name": "__cpp_lib_constexpr_misc",
512 "headers": ["array", "functional", "iterator", "string_view", "tuple", "utility"],
513 "unimplemented": True,
515 {"name": "__cpp_lib_bind_front",
519 "headers": ["functional"],
520 "unimplemented": True,
522 {"name": "__cpp_lib_is_constant_evaluated",
526 "headers": ["type_traits"],
527 "unimplemented": True,
529 {"name": "__cpp_lib_list_remove_return_type",
533 "headers": ["forward_list", "list"],
534 "unimplemented": True,
536 {"name": "__cpp_lib_generic_unordered_lookup",
540 "headers": ["unordered_map", "unordered_set"],
541 "unimplemented": True,
543 {"name": "__cpp_lib_ranges",
547 "headers": ["algorithm", "functional", "iterator", "memory", "ranges"],
548 "unimplemented": True,
550 {"name": "__cpp_lib_bit_cast",
555 "unimplemented": True,
557 {"name": "__cpp_lib_atomic_ref",
561 "headers": ["atomic"],
562 "unimplemented": True,
563 "depends": "!defined(_LIBCPP_HAS_NO_THREADS)",
564 "internal_depends": "!defined(_LIBCPP_HAS_NO_THREADS)",
566 ]], key=lambda tc: tc["name"])
568 def get_std_dialects():
569 std_dialects = ['c++14', 'c++17', 'c++2a']
570 return list(std_dialects)
572 def get_first_std(d):
573 for s in get_std_dialects():
579 rev_dialects = get_std_dialects()
580 rev_dialects.reverse()
581 for s in rev_dialects:
586 def get_std_before(d, std):
587 std_dialects = get_std_dialects()
588 candidates = std_dialects[0:std_dialects.index(std)]
590 for cand in candidates:
595 def get_value_before(d, std):
596 new_std = get_std_before(d, std)
601 def get_for_std(d, std):
602 # This catches the C++11 case for which there should be no defined feature
604 std_dialects = get_std_dialects()
605 if std not in std_dialects:
607 # Find the value for the newest C++ dialect between C++14 and std
608 std_list = list(std_dialects[0:std_dialects.index(std)+1])
617 Functions to produce the <version> header
620 def produce_macros_definition_for_std(std):
623 for tc in feature_test_macros:
624 if std not in tc["values"]:
627 if 'depends' in tc.keys():
628 assert 'internal_depends' in tc.keys()
629 result += "# if %s\n" % tc["internal_depends"]
631 if get_value_before(tc["values"], std) is not None:
632 assert 'depends' not in tc.keys()
633 result += "# undef %s\n" % tc["name"]
634 line = "#%sdefine %s" % ((" " * inner_indent), tc["name"])
635 line += " " * (indent - len(line))
636 line += "%sL" % tc["values"][std]
637 if 'unimplemented' in tc.keys():
641 if 'depends' in tc.keys():
642 result += "# endif\n"
646 """Yield successive n-sized chunks from l."""
647 for i in range(0, len(l), n):
650 def produce_version_synopsis():
652 header_indent = 56 + len("20XXYYL ")
654 def indent_to(s, val):
657 s += " " * (val - len(s))
659 line = indent_to("Macro name", indent) + "Value"
660 line = indent_to(line, header_indent) + "Headers"
661 result += line + "\n"
662 for tc in feature_test_macros:
663 prev_defined_std = get_last_std(tc["values"])
664 line = "{name: <{indent}}{value}L ".format(name=tc['name'], indent=indent,
665 value=tc["values"][prev_defined_std])
666 headers = list(tc["headers"])
667 headers.remove("version")
668 for chunk in chunks(headers, 3):
669 line = indent_to(line, header_indent)
670 chunk = ['<%s>' % header for header in chunk]
671 line += ' '.join(chunk)
676 prev_defined_std = get_std_before(tc["values"], prev_defined_std)
677 if prev_defined_std is None:
679 result += "%s%sL // %s\n" % (indent_to("", indent), tc["values"][prev_defined_std],
680 prev_defined_std.replace("c++", "C++"))
684 def produce_version_header():
685 template="""// -*- C++ -*-
686 //===--------------------------- version ----------------------------------===//
688 // The LLVM Compiler Infrastructure
690 // This file is dual licensed under the MIT and the University of Illinois Open
691 // Source Licenses. See LICENSE.TXT for details.
693 //===----------------------------------------------------------------------===//
695 #ifndef _LIBCPP_VERSIONH
696 #define _LIBCPP_VERSIONH
707 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
708 #pragma GCC system_header
711 #if _LIBCPP_STD_VER > 11
715 #if _LIBCPP_STD_VER > 14
719 #if _LIBCPP_STD_VER > 17
723 #endif // _LIBCPP_VERSIONH
725 return template.format(
726 synopsis=produce_version_synopsis().strip(),
727 cxx14_macros=produce_macros_definition_for_std('c++14').strip(),
728 cxx17_macros=produce_macros_definition_for_std('c++17').strip(),
729 cxx2a_macros=produce_macros_definition_for_std('c++2a').strip())
732 Functions to produce test files
738 # error "{name} should not be defined before {std_first}"
745 # error "{name} should be defined in {std}"
747 # if {name} != {value}
748 # error "{name} should have the value {value} in {std}"
752 # error "{name} should not be defined when {depends} is not defined!"
758 # if !defined(_LIBCPP_VERSION)
760 # error "{name} should be defined in {std}"
762 # if {name} != {value}
763 # error "{name} should have the value {value} in {std}"
765 # else // _LIBCPP_VERSION
767 # error "{name} should not be defined because it is unimplemented in libc++!"
774 # error "{name} should be defined in {std}"
776 # if {name} != {value}
777 # error "{name} should have the value {value} in {std}"
782 def generate_std_test(test_list, std):
785 val = get_for_std(tc["values"], std)
789 result += test_types["undefined"].format(name=tc["name"], std_first=get_first_std(tc["values"]))
790 elif 'unimplemented' in tc.keys():
791 result += test_types["unimplemented"].format(name=tc["name"], value=val, std=std)
792 elif "depends" in tc.keys():
793 result += test_types["depends"].format(name=tc["name"], value=val, std=std, depends=tc["depends"])
795 result += test_types["defined"].format(name=tc["name"], value=val, std=std)
798 def generate_synopsis(test_list):
799 max_name_len = max([len(tc["name"]) for tc in test_list])
800 indent = max_name_len + 8
801 def mk_line(prefix, suffix):
802 return "{prefix: <{max_len}}{suffix}\n".format(prefix=prefix, suffix=suffix,
805 result += mk_line("/* Constant", "Value")
807 prefix = " %s" % tc["name"]
808 for std in [s for s in get_std_dialects() if s in tc["values"].keys()]:
809 result += mk_line(prefix, "%sL [%s]" % (tc["values"][std], std.replace("c++", "C++")))
814 def is_threading_header_unsafe_to_include(h):
815 # NOTE: "<mutex>" does not blow up when included without threads.
816 return h in ['atomic', 'shared_mutex']
819 headers = set([h for tc in feature_test_macros for h in tc["headers"]])
821 test_list = [tc for tc in feature_test_macros if h in tc["headers"]]
822 if not has_header(h):
824 assert 'unimplemented' in tc.keys()
827 if is_threading_header_unsafe_to_include(h):
828 test_tags += '\n// UNSUPPORTED: libcpp-has-no-threads\n'
830 """//===----------------------------------------------------------------------===//
832 // The LLVM Compiler Infrastructure
834 // This file is dual licensed under the MIT and the University of Illinois Open
835 // Source Licenses. See LICENSE.TXT for details.
837 //===----------------------------------------------------------------------===//
839 // WARNING: This test was generated by {script_name}
840 // and should not be edited manually.
844 // Test the feature test macros defined by <{header}>
849 #include "test_macros.h"
851 #if TEST_STD_VER < 14
855 #elif TEST_STD_VER == 14
859 #elif TEST_STD_VER == 17
863 #elif TEST_STD_VER > 17
867 #endif // TEST_STD_VER > 17
870 """.format(script_name=script_name,
873 synopsis=generate_synopsis(test_list),
874 cxx11_tests=generate_std_test(test_list, 'c++11').strip(),
875 cxx14_tests=generate_std_test(test_list, 'c++14').strip(),
876 cxx17_tests=generate_std_test(test_list, 'c++17').strip(),
877 cxx2a_tests=generate_std_test(test_list, 'c++2a').strip())
878 test_name = "{header}.version.pass.cpp".format(header=h)
879 out_path = os.path.join(script_path, test_name)
880 with open(out_path, 'w') as f:
884 Produce documentation for the feature test macros
887 def make_widths(grid):
889 for i in range(0, len(grid[0])):
890 cell_width = 2 + max(reduce(lambda x,y: x+y, [[len(row[i])] for row in grid], []))
891 widths += [cell_width]
894 def create_table(grid, indent):
895 indent_str = ' '*indent
896 col_widths = make_widths(grid)
897 num_cols = len(grid[0])
898 result = indent_str + add_divider(col_widths, 2)
900 for row_i in xrange(0, len(grid)):
902 result = result + indent_str + ' '.join([pad_cell(row[i], col_widths[i]) for i in range(0, len(row))]) + '\n'
903 is_cxx_header = row[0].startswith('**')
904 if row_i == len(grid) - 1:
906 result = result + indent_str + add_divider(col_widths, 1 if is_cxx_header else header_flag)
910 def add_divider(widths, header_flag):
912 return ' '.join(['='*w for w in widths]) + '\n'
914 return '-'.join(['-'*w for w in widths]) + '\n'
916 return ' '.join(['-'*w for w in widths]) + '\n'
918 def pad_cell(s, length, left_align=True):
919 padding = ((length - len(s)) * ' ')
923 def get_status_table():
924 table = [["Macro Name", "Value"]]
925 for std in get_std_dialects():
926 table += [["**" + std.replace("c++", "C++ ") + "**", ""]]
927 for tc in feature_test_macros:
928 if std not in tc["values"].keys():
930 value = "``%sL``" % tc["values"][std]
931 if 'unimplemented' in tc.keys():
932 value = '*unimplemented*'
933 table += [["``%s``" % tc["name"], value]]
937 doc_str = """.. _FeatureTestMacroTable:
939 ==========================
940 Feature Test Macro Support
941 ==========================
949 This file documents the feature test macros currently supported by libc++.
956 .. table:: Current Status
957 :name: feature-status-table
962 """.format(status_tables=create_table(get_status_table(), 4))
964 table_doc_path = os.path.join(docs_path, 'FeatureTestMacroTable.rst')
965 with open(table_doc_path, 'w') as f:
969 with tempfile.NamedTemporaryFile(mode='w', prefix='version.', delete=False) as tmp_file:
970 print("producing new <version> header as %s" % tmp_file.name)
971 tmp_file.write(produce_version_header())
976 if __name__ == '__main__':