Skip to content

Commit

Permalink
[libc] Add compile tests for each public header (llvm#122527)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
frobtech authored and DKLoehr committed Jan 17, 2025
1 parent 142cdbf commit 1828793
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
2 changes: 2 additions & 0 deletions libc/cmake/modules/LLVMLibCHeaderRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function(add_header target_name)
set_target_properties(
${fq_target_name}
PROPERTIES
HEADER_NAME ${dest_leaf_filename}
HEADER_FILE_PATH ${dest_file}
DEPS "${fq_deps_list}"
)
Expand Down Expand Up @@ -164,6 +165,7 @@ function(add_gen_header target_name)
set_target_properties(
${fq_target_name}
PROPERTIES
HEADER_NAME ${ADD_GEN_HDR_GEN_HDR}
HEADER_FILE_PATH ${out_file}
DECLS_FILE_PATH "${decl_out_file}"
DEPS "${fq_deps_list}"
Expand Down
69 changes: 68 additions & 1 deletion libc/test/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ add_libc_test(
-Werror
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
)
)

add_libc_test(
isfinite_c_test
Expand Down Expand Up @@ -483,3 +483,70 @@ add_libc_test(
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
)

# Test `#include <...>` of each header in each available language mode.
# This is gated on -DLLVM_LIBC_BUILD_HEADER_TESTS=ON until all the bugs
# in headers are fixed so the tests all compile.
set(TEST_STDC_VERSIONS 89;99;11;17;23)
set(TEST_STDCXX_VERSIONS 03;11;14;17;20;23;26)

function(add_header_test target_name source_file deps std_mode)
if(LLVM_LIBC_BUILD_HEADER_TESTS)
add_libc_test(
${target_name}
C_TEST
HERMETIC_TEST_ONLY
SUITE
libc_include_tests
SRCS
${source_file}
COMPILE_OPTIONS
-Werror
-Wsystem-headers
-Wall
-Wextra
-std=${std_mode}
DEPENDS
${deps}
)
endif()
endfunction()

foreach(target ${TARGET_PUBLIC_HEADERS})
string(REPLACE "libc.include." "" header ${target})
get_target_property(HEADER_NAME ${target} HEADER_NAME)

set(test_stdc_file "${CMAKE_CURRENT_BINARY_DIR}/${header}_test.c")
configure_file(header-test-template.c ${test_stdc_file} @ONLY)
foreach(stdc_version ${TEST_STDC_VERSIONS})
add_header_test(
"${header}_c${stdc_version}_test"
${test_stdc_file}
${target}
"c${stdc_version}"
)
add_header_test(
"${header}_gnu${stdc_version}_test"
${test_stdc_file}
${target}
"gnu${stdc_version}"
)
endforeach()

set(test_stdcxx_file "${CMAKE_CURRENT_BINARY_DIR}/${header}_test.cpp")
configure_file(header-test-template.c ${test_stdcxx_file} @ONLY)
foreach(stdcxx_version ${TEST_STDCXX_VERSIONS})
add_header_test(
"${header}_cpp${stdcxx_version}_test"
${test_stdcxx_file}
${target}
"c++${stdcxx_version}"
)
add_header_test(
"${header}_gnucpp${stdcxx_version}_test"
${test_stdcxx_file}
${target}
"gnu++${stdcxx_version}"
)
endforeach()
endforeach()
14 changes: 14 additions & 0 deletions libc/test/include/header-test-template.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*===-- Test for <@HEADER_NAME@> ----------------------------------------===
*
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
* See https://llvm.org/LICENSE.txt for license information.
* SPDXList-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#include <@HEADER_NAME@>

int main(int argc, char **argv) {
(void)argc;
(void)argv;
return 0;
}

0 comments on commit 1828793

Please sign in to comment.