Skip to content

Commit 5e4b41c

Browse files
authored
[libc] Add compile tests for each public header (#122527)
This adds a test that consists of compiling `#include <...>`, pretty much alone, for each public header file in each different language mode (`-std=...` compiler switch) with -Werror and many warnings enabled. There are several headers that have bugs when used alone, and many more headers that have bugs in certain language modes. So for now, compiling the new tests is gated on the cmake switch -DLLVM_LIBC_BUILD_HEADER_TESTS=ON. When all the bugs are fixed, the switch will be removed so future regressions don't land.
1 parent 2c7829e commit 5e4b41c

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

libc/cmake/modules/LLVMLibCHeaderRules.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ function(add_header target_name)
6666
set_target_properties(
6767
${fq_target_name}
6868
PROPERTIES
69+
HEADER_NAME ${dest_leaf_filename}
6970
HEADER_FILE_PATH ${dest_file}
7071
DEPS "${fq_deps_list}"
7172
)
@@ -164,6 +165,7 @@ function(add_gen_header target_name)
164165
set_target_properties(
165166
${fq_target_name}
166167
PROPERTIES
168+
HEADER_NAME ${ADD_GEN_HDR_GEN_HDR}
167169
HEADER_FILE_PATH ${out_file}
168170
DECLS_FILE_PATH "${decl_out_file}"
169171
DEPS "${fq_deps_list}"

libc/test/include/CMakeLists.txt

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ add_libc_test(
422422
-Werror
423423
DEPENDS
424424
libc.include.llvm-libc-macros.math_function_macros
425-
)
425+
)
426426

427427
add_libc_test(
428428
isfinite_c_test
@@ -483,3 +483,70 @@ add_libc_test(
483483
DEPENDS
484484
libc.include.llvm-libc-macros.math_function_macros
485485
)
486+
487+
# Test `#include <...>` of each header in each available language mode.
488+
# This is gated on -DLLVM_LIBC_BUILD_HEADER_TESTS=ON until all the bugs
489+
# in headers are fixed so the tests all compile.
490+
set(TEST_STDC_VERSIONS 89;99;11;17;23)
491+
set(TEST_STDCXX_VERSIONS 03;11;14;17;20;23;26)
492+
493+
function(add_header_test target_name source_file deps std_mode)
494+
if(LLVM_LIBC_BUILD_HEADER_TESTS)
495+
add_libc_test(
496+
${target_name}
497+
C_TEST
498+
HERMETIC_TEST_ONLY
499+
SUITE
500+
libc_include_tests
501+
SRCS
502+
${source_file}
503+
COMPILE_OPTIONS
504+
-Werror
505+
-Wsystem-headers
506+
-Wall
507+
-Wextra
508+
-std=${std_mode}
509+
DEPENDS
510+
${deps}
511+
)
512+
endif()
513+
endfunction()
514+
515+
foreach(target ${TARGET_PUBLIC_HEADERS})
516+
string(REPLACE "libc.include." "" header ${target})
517+
get_target_property(HEADER_NAME ${target} HEADER_NAME)
518+
519+
set(test_stdc_file "${CMAKE_CURRENT_BINARY_DIR}/${header}_test.c")
520+
configure_file(header-test-template.c ${test_stdc_file} @ONLY)
521+
foreach(stdc_version ${TEST_STDC_VERSIONS})
522+
add_header_test(
523+
"${header}_c${stdc_version}_test"
524+
${test_stdc_file}
525+
${target}
526+
"c${stdc_version}"
527+
)
528+
add_header_test(
529+
"${header}_gnu${stdc_version}_test"
530+
${test_stdc_file}
531+
${target}
532+
"gnu${stdc_version}"
533+
)
534+
endforeach()
535+
536+
set(test_stdcxx_file "${CMAKE_CURRENT_BINARY_DIR}/${header}_test.cpp")
537+
configure_file(header-test-template.c ${test_stdcxx_file} @ONLY)
538+
foreach(stdcxx_version ${TEST_STDCXX_VERSIONS})
539+
add_header_test(
540+
"${header}_cpp${stdcxx_version}_test"
541+
${test_stdcxx_file}
542+
${target}
543+
"c++${stdcxx_version}"
544+
)
545+
add_header_test(
546+
"${header}_gnucpp${stdcxx_version}_test"
547+
${test_stdcxx_file}
548+
${target}
549+
"gnu++${stdcxx_version}"
550+
)
551+
endforeach()
552+
endforeach()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*===-- Test for <@HEADER_NAME@> ----------------------------------------===
2+
*
3+
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
* See https://llvm.org/LICENSE.txt for license information.
5+
* SPDXList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#include <@HEADER_NAME@>
9+
10+
int main(int argc, char **argv) {
11+
(void)argc;
12+
(void)argv;
13+
return 0;
14+
}

0 commit comments

Comments
 (0)