aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2024-01-23 07:02:01 -0800
committerCopybara-Service <copybara-worker@google.com>2024-01-23 07:02:53 -0800
commit563ebf1769af08d448765c31064260e1a16fcced (patch)
tree3eca704c424bc48ce56d8bedc81a8a5716034752
parentbd30c39d61a803def75d0b756f7cff3591655f91 (diff)
downloadgoogletest-563ebf1769af08d448765c31064260e1a16fcced.tar.gz
Fix double-promotion warnings in AppropriateResolution()
When -Wdouble-promotion is enabled, the templatized function AppropriateResolution fails to compile since its float instantiation promotes floats to doubles when doing arithmetic and comparisons. Add static casts to resolve these errors. PiperOrigin-RevId: 600776333 Change-Id: Ia530b4bbca6ddce27caf0a817196d87efef711cb
-rw-r--r--googletest/include/gtest/gtest-printers.h49
1 files changed, 28 insertions, 21 deletions
diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h
index 0007a2a9..b2822bcd 100644
--- a/googletest/include/gtest/gtest-printers.h
+++ b/googletest/include/gtest/gtest-printers.h
@@ -124,7 +124,7 @@
#if GTEST_INTERNAL_HAS_STD_SPAN
#include <span> // NOLINT
-#endif // GTEST_INTERNAL_HAS_STD_SPAN
+#endif // GTEST_INTERNAL_HAS_STD_SPAN
namespace testing {
@@ -241,8 +241,8 @@ struct StreamPrinter {
// ADL (possibly involving implicit conversions).
// (Use SFINAE via return type, because it seems GCC < 12 doesn't handle name
// lookup properly when we do it in the template parameter list.)
- static auto PrintValue(const T& value, ::std::ostream* os)
- -> decltype((void)(*os << value)) {
+ static auto PrintValue(const T& value,
+ ::std::ostream* os) -> decltype((void)(*os << value)) {
// Call streaming operator found by ADL, possibly with implicit conversions
// of the arguments.
*os << value;
@@ -558,43 +558,50 @@ int AppropriateResolution(FloatType val) {
#endif
if (val < 1000000) {
FloatType mulfor6 = 1e10;
- if (val >= 100000.0) { // 100,000 to 999,999
+ // Without these static casts, the template instantiation for float would
+ // fail to compile when -Wdouble-promotion is enabled, as the arithmetic and
+ // comparison logic would promote floats to doubles.
+ if (val >= static_cast<FloatType>(100000.0)) { // 100,000 to 999,999
mulfor6 = 1.0;
- } else if (val >= 10000.0) {
+ } else if (val >= static_cast<FloatType>(10000.0)) {
mulfor6 = 1e1;
- } else if (val >= 1000.0) {
+ } else if (val >= static_cast<FloatType>(1000.0)) {
mulfor6 = 1e2;
- } else if (val >= 100.0) {
+ } else if (val >= static_cast<FloatType>(100.0)) {
mulfor6 = 1e3;
- } else if (val >= 10.0) {
+ } else if (val >= static_cast<FloatType>(10.0)) {
mulfor6 = 1e4;
- } else if (val >= 1.0) {
+ } else if (val >= static_cast<FloatType>(1.0)) {
mulfor6 = 1e5;
- } else if (val >= 0.1) {
+ } else if (val >= static_cast<FloatType>(0.1)) {
mulfor6 = 1e6;
- } else if (val >= 0.01) {
+ } else if (val >= static_cast<FloatType>(0.01)) {
mulfor6 = 1e7;
- } else if (val >= 0.001) {
+ } else if (val >= static_cast<FloatType>(0.001)) {
mulfor6 = 1e8;
- } else if (val >= 0.0001) {
+ } else if (val >= static_cast<FloatType>(0.0001)) {
mulfor6 = 1e9;
}
- if (static_cast<FloatType>(static_cast<int32_t>(val * mulfor6 + 0.5)) /
+ if (static_cast<FloatType>(static_cast<int32_t>(
+ val * mulfor6 + (static_cast<FloatType>(0.5)))) /
mulfor6 ==
val)
return 6;
- } else if (val < 1e10) {
- FloatType divfor6 = 1.0;
- if (val >= 1e9) { // 1,000,000,000 to 9,999,999,999
+ } else if (val < static_cast<FloatType>(1e10)) {
+ FloatType divfor6 = static_cast<FloatType>(1.0);
+ if (val >= static_cast<FloatType>(1e9)) { // 1,000,000,000 to 9,999,999,999
divfor6 = 10000;
- } else if (val >= 1e8) { // 100,000,000 to 999,999,999
+ } else if (val >=
+ static_cast<FloatType>(1e8)) { // 100,000,000 to 999,999,999
divfor6 = 1000;
- } else if (val >= 1e7) { // 10,000,000 to 99,999,999
+ } else if (val >=
+ static_cast<FloatType>(1e7)) { // 10,000,000 to 99,999,999
divfor6 = 100;
- } else if (val >= 1e6) { // 1,000,000 to 9,999,999
+ } else if (val >= static_cast<FloatType>(1e6)) { // 1,000,000 to 9,999,999
divfor6 = 10;
}
- if (static_cast<FloatType>(static_cast<int32_t>(val / divfor6 + 0.5)) *
+ if (static_cast<FloatType>(static_cast<int32_t>(
+ val / divfor6 + (static_cast<FloatType>(0.5)))) *
divfor6 ==
val)
return 6;