From ca2d229e6b31fc007204eed806b2b4af405c02fd Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Mon, 27 May 2024 13:39:01 -0400 Subject: [PATCH] Test for compiling without warnings against public headers Under compilers with WERRORFLAG, MakeMakefile.try_compile treats warnings as errors, so we can use append_cflags to test whether the public header files emit warnings with certain flags turned on. [Regression test for feature #20507] --- ext/-test-/public_header_warnings/extconf.rb | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 ext/-test-/public_header_warnings/extconf.rb diff --git a/ext/-test-/public_header_warnings/extconf.rb b/ext/-test-/public_header_warnings/extconf.rb new file mode 100644 index 0000000000..f6a8a51f63 --- /dev/null +++ b/ext/-test-/public_header_warnings/extconf.rb @@ -0,0 +1,23 @@ +# +# Under compilers with WERRORFLAG, MakeMakefile.try_compile treats warnings as errors, so we can +# use append_cflags to test whether the public header files emit warnings with certain flags turned +# on. +# +def check_append_cflags(flag, msg = nil) + msg ||= "flag #{flag} is not acceptable" + !$CFLAGS.include?(flag) or raise("flag #{flag} already present in $CFLAGS") + append_cflags(flag) + $CFLAGS.include?(flag) or raise(msg) +end + +if %w[gcc clang].include?(RbConfig::CONFIG['CC']) + config_string("WERRORFLAG") or raise("expected WERRORFLAG to be defined") + + # should be acceptable on all modern C compilers + check_append_cflags("-D_TEST_OK", "baseline compiler warning test failed") + + # Feature #20507: Allow compilation of C extensions with -Wconversion + check_append_cflags("-Wconversion", "-Wconversion raising warnings in public headers") +end + +create_makefile("-test-/public_header_warnings")