google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
34.71k stars 10.13k forks source link

[Bug]: A recent change cause a new warning on ubuntu2204 using Clang. #4206

Open fengga opened 1 year ago

fengga commented 1 year ago

Describe the issue

A recent change https://github.com/google/googletest/commit/9c332145b71c36a5bad9688312c79184f98601ff will generate this warning: /home/vsts/work/1/s/deps/googletest/googletest/include/gtest/gtest-printers.h:532:9: error: implicit conversion from 'int32_t' (aka 'int') to 'float' may lose precision [-Werror,-Wimplicit-int-float-conversion] if (static_cast(val * mulfor6 + 0.5) / mulfor6 == val) return 6; ^~~~~~~~~ ~

Building on ubuntu 2204 with Clang will have this issue (Building with GCC will succeed).

Steps to reproduce the problem

Just build with Clang on ubuntu 2204.

What version of GoogleTest are you using?

v1.13.0

What operating system and version are you using?

ubuntu 2204

What compiler and version are you using?

-- The C compiler identification is Clang 14.0.0 -- The CXX compiler identification is Clang 14.0.0

What build system are you using?

CMake version: 3.26.0

Additional context

No response

JochenHiller commented 1 year ago

I stumbled also over that issue:

.../build/_deps/googletest-src/googletest/include/gtest/gtest-printers.h:532:9: warning: implicit conversion from 'int32_t' (aka 'int') to 'float' may lose precision [-Wimplicit-int-float-conversion]
    if (static_cast<int32_t>(val * mulfor6 + 0.5) / mulfor6 == val) return 6;
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~
.../build/_deps/googletest-src/googletest/include/gtest/gtest-printers.h:551:17: note: in instantiation of function template specialization 'testing::internal::AppropriateResolution<float>' requested here
  os->precision(AppropriateResolution(f));
                ^
.../build/_deps/googletest-src/googletest/include/gtest/gtest-printers.h:544:9: warning: implicit conversion from 'int32_t' (aka 'int') to 'float' may lose precision [-Wimplicit-int-float-conversion]
    if (static_cast<int32_t>(val / divfor6 + 0.5) * divfor6 == val) return 6;
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~

I am using:

This warning started for me when updating from Clang 14 to 16.0.4.

Is there a way to disable these warnings using CMake (as a workaround)? I am using CMake integration Googletest, and did not find a way to disable the warning for this build step target_link_libraries(myTarget_test myLib GTest::gtest_main).

samuelpmish commented 1 year ago

I just ran into this problem when upgrading to clang 16 too. At the time of writing this the 1.13.0 release does still have the implicit conversion problem, but the main branch has already fixed it. So, either patching those two lines of code to explicitly (rather than implicitly) convert to FloatType fixes it:

https://github.com/google/googletest/commit/3044657e7afa759ce875a8161cd4bff0fd2e22bc

as does updating the version of googletest being used (from the googletest cmake quick-start guide):

FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)