[libc-commits] [libc] [libc] Add compile tests for each public header (PR #122527)

Roland McGrath via libc-commits libc-commits at lists.llvm.org
Fri Jan 10 14:11:28 PST 2025


https://github.com/frobtech updated https://github.com/llvm/llvm-project/pull/122527

>From cc2061ad48dc1dc90696abfde41fbba94c59332d Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Fri, 10 Jan 2025 13:12:21 -0800
Subject: [PATCH 1/5] [libc] Add compile tests for each public header

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.
---
 libc/cmake/modules/LLVMLibCHeaderRules.cmake |  2 +
 libc/test/include/CMakeLists.txt             | 68 +++++++++++++++++++-
 2 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/libc/cmake/modules/LLVMLibCHeaderRules.cmake b/libc/cmake/modules/LLVMLibCHeaderRules.cmake
index 288e4dade0b472..ea8b76e0f62351 100644
--- a/libc/cmake/modules/LLVMLibCHeaderRules.cmake
+++ b/libc/cmake/modules/LLVMLibCHeaderRules.cmake
@@ -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}"
   )
@@ -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}"
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index ba21a69a31a3b4..ddb69ebc0c166e 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -422,7 +422,7 @@ add_libc_test(
     -Werror
   DEPENDS
     libc.include.llvm-libc-macros.math_function_macros
-)  
+)
 
 add_libc_test(
   isfinite_c_test
@@ -483,3 +483,69 @@ 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
+      UNIT_TEST_ONLY
+      SUITE
+	libc_include_tests
+      SRCS
+	${source_file}
+      COMPILE_OPTIONS
+	-Werror
+	-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()

>From 91da76ce4d46f558194888181e323bebb6668912 Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Fri, 10 Jan 2025 13:27:22 -0800
Subject: [PATCH 2/5] add mising file

---
 libc/test/include/header-test-template.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 libc/test/include/header-test-template.c

diff --git a/libc/test/include/header-test-template.c b/libc/test/include/header-test-template.c
new file mode 100644
index 00000000000000..c3921b14036929
--- /dev/null
+++ b/libc/test/include/header-test-template.c
@@ -0,0 +1,15 @@
+/*===-- 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.
+ * SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <@HEADER_NAME@>
+
+#ifdef __cplusplus
+extern "C"
+#endif
+int main(void) {
+  return 0;
+}

>From 56d789a148c7a86c29bdaf604f5f41a967328ab6 Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Fri, 10 Jan 2025 13:31:23 -0800
Subject: [PATCH 3/5] Use HERMETIC_TEST_ONLY, not UNIT_TEST_ONLY

---
 libc/test/include/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index ddb69ebc0c166e..65730454035a77 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -495,7 +495,7 @@ function(add_header_test target_name source_file deps std_mode)
     add_libc_test(
       ${target_name}
       C_TEST
-      UNIT_TEST_ONLY
+      HERMETIC_TEST_ONLY
       SUITE
 	libc_include_tests
       SRCS

>From 76b24bbc4072ba26fef7155bfff0583a7a55278b Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Fri, 10 Jan 2025 13:34:08 -0800
Subject: [PATCH 4/5] Reformat template

---
 libc/test/include/header-test-template.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/libc/test/include/header-test-template.c b/libc/test/include/header-test-template.c
index c3921b14036929..bbaf43f9603d6a 100644
--- a/libc/test/include/header-test-template.c
+++ b/libc/test/include/header-test-template.c
@@ -8,8 +8,7 @@
 #include <@HEADER_NAME@>
 
 #ifdef __cplusplus
-extern "C"
+extern "C" int main() { return 0; }
+#else
+int main(void) { return 0; }
 #endif
-int main(void) {
-  return 0;
-}

>From 1370e513d464b285ae464edfebfa653f8398e1ba Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Fri, 10 Jan 2025 14:10:32 -0800
Subject: [PATCH 5/5] Use a single main declaration.

---
 libc/test/include/header-test-template.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/libc/test/include/header-test-template.c b/libc/test/include/header-test-template.c
index bbaf43f9603d6a..84f3f00b9df895 100644
--- a/libc/test/include/header-test-template.c
+++ b/libc/test/include/header-test-template.c
@@ -7,8 +7,8 @@
 
 #include <@HEADER_NAME@>
 
-#ifdef __cplusplus
-extern "C" int main() { return 0; }
-#else
-int main(void) { return 0; }
-#endif
+int main(int argc, char **argv) {
+  (void)argc;
+  (void)argv;
+  return 0;
+}



More information about the libc-commits mailing list