From 8c631e339b8e5b7b9721d9e761d92b9e5d397d4b Mon Sep 17 00:00:00 2001 From: hurchalla Date: Mon, 11 Mar 2024 13:11:11 -0700 Subject: [PATCH] fix new compiler warnings in gcc13 and clang18 --- build_tests.sh | 21 +++++++++++++------ .../factoring/detail/is_prime_miller_rabin.h | 1 + test/detail_tests/test_factorize_wheel210.cpp | 14 +++++++++++++ .../test_is_prime_miller_rabin.cpp | 9 ++++++++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/build_tests.sh b/build_tests.sh index be1b370..af8145d 100755 --- a/build_tests.sh +++ b/build_tests.sh @@ -10,7 +10,7 @@ # This is a working convenience script for invoking the testing builds and then # running the tests. # The syntax is -# ./build_tests [-c] [-r] [-a] [-u] [-t] [-m] +# ./build_tests [-c] [-r] [-a] [-u] [-t] [-m] [-l] # # -c allows you to select the compiler, rather than using the default. # -r specifies to run all tests after the build. Without -r, no tests will run. @@ -25,6 +25,7 @@ # repository. Without -t, only tests for this repository will be compiled. # -m allows you to choose between Release, Debug, and Profile(Release with # debug symbols) build configurations, rather than using the default. +# -l allows you to choose between either libstdc++ or libc++ when using clang. # # Currently it supports clang, gcc, and icc but you'll need to customize the # section under "#Compiler commands" to match the compilers on your system. The @@ -167,12 +168,12 @@ -while getopts ":m:c:h-:raut" opt; do +while getopts ":m:l:c:h-:raut" opt; do case $opt in h) ;& -) - echo "Usage: build_tests [-c] [-r] [-a] [-u] [-t] [-m]" >&2 + echo "Usage: build_tests [-c] [-r] [-a] [-u] [-t] [-m] [-l]" >&2 exit 1 ;; c) @@ -181,6 +182,9 @@ while getopts ":m:c:h-:raut" opt; do m) mode=$OPTARG ;; + l) + library=$OPTARG + ;; r) run_tests=true ;; @@ -212,6 +216,10 @@ if [ -z "$mode" ]; then mode=Debug fi +if [ -n "$library" ]; then + cpp_stdlib="-stdlib=$library" +fi + # Compiler commands if [ "${compiler,,}" = "gcc" ] || [ "${compiler,,}" = "g++" ]; then @@ -506,7 +514,7 @@ if [ "${mode,,}" = "release" ]; then -DBENCH_HURCHALLA_FACTORING=ON \ $test_all_hurchalla_libs \ -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_CXX_FLAGS="$cpp_standard \ + -DCMAKE_CXX_FLAGS="$cpp_standard $cpp_stdlib \ $use_inline_asm $use_all_inline_asm \ $gcc_static_analysis" "${clang_static_analysis[@]}" \ $cmake_cpp_compiler $cmake_c_compiler @@ -522,7 +530,8 @@ elif [ "${mode,,}" = "debug" ]; then $test_all_hurchalla_libs \ -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_EXE_LINKER_FLAGS="$clang_ubsan_link_flags" \ - -DCMAKE_CXX_FLAGS="$cpp_standard $clang_ubsan $gcc_ubsan \ + -DCMAKE_CXX_FLAGS="$cpp_standard $cpp_stdlib \ + $clang_ubsan $gcc_ubsan \ $use_inline_asm $use_all_inline_asm \ $gcc_static_analysis" "${clang_static_analysis[@]}" \ $cmake_cpp_compiler $cmake_c_compiler @@ -538,7 +547,7 @@ elif [ "${mode,,}" = "profile" ]; then $test_all_hurchalla_libs \ -DCMAKE_BUILD_TYPE=RelWithDebInfo \ -DCMAKE_EXE_LINKER_FLAGS="-ldl" \ - -DCMAKE_CXX_FLAGS="$cpp_standard \ + -DCMAKE_CXX_FLAGS="$cpp_standard $cpp_stdlib \ $use_inline_asm $use_all_inline_asm" \ $cmake_cpp_compiler $cmake_c_compiler exit_on_failure diff --git a/include/hurchalla/factoring/detail/is_prime_miller_rabin.h b/include/hurchalla/factoring/detail/is_prime_miller_rabin.h index bd08388..7532249 100644 --- a/include/hurchalla/factoring/detail/is_prime_miller_rabin.h +++ b/include/hurchalla/factoring/detail/is_prime_miller_rabin.h @@ -65,6 +65,7 @@ #endif #ifdef __clang__ # pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wunknown-warning-option" # pragma GCC diagnostic ignored "-Wbitwise-instead-of-logical" #endif diff --git a/test/detail_tests/test_factorize_wheel210.cpp b/test/detail_tests/test_factorize_wheel210.cpp index 1f8f2c7..9e91f06 100644 --- a/test/detail_tests/test_factorize_wheel210.cpp +++ b/test/detail_tests/test_factorize_wheel210.cpp @@ -112,10 +112,24 @@ void test_factor_wheel210(T x, const std::vector& answer) FactorArrayAdapter faa(arr); factorize_wheel210::call(std::back_inserter(faa), x); auto num_factors = faa.size(); + EXPECT_TRUE(num_factors <= arr.size()); EXPECT_TRUE(num_factors == answer.size()); + +#if !defined(__GNUC__) || defined(__clang__) + // gcc10 appears to have a compiler bug, claiming that when we are called by + // TEST(...basic_tests_8), the line below for std::sort accesses the array + // out of bounds. It's a compile-time warning/error with gcc10. I can find + // no problem in the code, and the run-time test above for + // (num_factors <= arr.size()) doesn't fail in any compiler. + // --- Therefore we provide an alternate #else for gcc --- std::sort(arr.begin(), arr.begin()+num_factors); EXPECT_TRUE(std::equal(arr.begin(), arr.begin()+num_factors, answer.begin())); +#else + std::vector tmp(arr.begin(), arr.begin()+num_factors); + std::sort(tmp.begin(), tmp.end()); + EXPECT_TRUE(tmp == answer); +#endif } diff --git a/test/detail_tests/test_is_prime_miller_rabin.cpp b/test/detail_tests/test_is_prime_miller_rabin.cpp index 70d919e..64ff061 100644 --- a/test/detail_tests/test_is_prime_miller_rabin.cpp +++ b/test/detail_tests/test_is_prime_miller_rabin.cpp @@ -78,6 +78,11 @@ TEST(HurchallaFactoringIsPrimeMillerRabin, exhaustive_uint16_t) { } #endif + +#ifdef __GNUC__ +# pragma GCC diagnostic push +# pragma GCC diagnostic warning "-Wstrict-overflow=2" +#endif TEST(HurchallaFactoringIsPrimeMillerRabin, basic_test1) { using T = std::uint32_t; T modulus = 127; @@ -90,6 +95,10 @@ TEST(HurchallaFactoringIsPrimeMillerRabin, basic_test1) { EXPECT_TRUE(is_prime_miller_rabin::call(mFR)); EXPECT_TRUE(is_prime_miller_rabin::call(mQR)); } +#ifdef __GNUC__ +# pragma GCC diagnostic pop +#endif + TEST(HurchallaFactoringIsPrimeMillerRabin, basic_test2) { using T = std::uint32_t;