llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Marcel Jacobse (mjacobse)

<details>
<summary>Changes</summary>

Resolves https://github.com/llvm/llvm-project/issues/33409.

The information `IsListInit` is already passed to function 
`CheckImplicitConversion` for another use-case which makes adding a condition 
for the double-promotion case simple.

Also adds tests, both for the changed list-initialization case as well as for 
normal explicit casts which already would have passed before this PR. These 
negative tests are added directly next to the positive tests in 
`warn-double-promotion.c` or for the C++-specific cases in a new .cpp version 
of that file.

---
Full diff: https://github.com/llvm/llvm-project/pull/159992.diff


4 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+1) 
- (modified) clang/lib/Sema/SemaChecking.cpp (+5) 
- (modified) clang/test/Sema/warn-double-promotion.c (+27) 
- (added) clang/test/Sema/warn-double-promotion.cpp (+128) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 46d56bb3f07f5..37d5e09c3f4e8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -417,6 +417,7 @@ Bug Fixes to C++ Support
   ``__builtin_addressof``, and related issues with builtin arguments. 
(#GH154034)
 - Fix an assertion failure when taking the address on a non-type template 
parameter argument of
   object type. (#GH151531)
+- Suppress ``-Wdouble-promotion`` when explicitly asked for with C++ list 
initialization (#GH33409).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 00f40cfa910d2..d5b2cde0d1c09 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -12382,6 +12382,11 @@ void Sema::CheckImplicitConversion(Expr *E, QualType 
T, SourceLocation CC,
       }
       // ... or possibly if we're increasing rank, too
       else if (Order < 0) {
+        // Don't warn if we are in a C++ list initialization expression, as
+        // that means the promotion was asked for explicitly.
+        if (IsListInit)
+          return;
+
         if (SourceMgr.isInSystemMacro(CC))
           return;
 
diff --git a/clang/test/Sema/warn-double-promotion.c 
b/clang/test/Sema/warn-double-promotion.c
index 5742a4fb3cbd4..ac9e9499bc2b7 100644
--- a/clang/test/Sema/warn-double-promotion.c
+++ b/clang/test/Sema/warn-double-promotion.c
@@ -24,10 +24,25 @@ long double ReturnLongDoubleFromDouble(double d) {
   return d;  //expected-warning{{implicit conversion increases floating-point 
precision: 'double' to 'long double'}}
 }
 
+double ReturnDoubleFromFloatWithExplicitCast(float f) {
+  return (double)f;
+}
+
+long double ReturnLongDoubleFromFloatWithExplicitCast(float f) {
+  return (long double)f;
+}
+
+long double ReturnLongDoubleFromDoubleWithExplicitCast(double d) {
+  return (long double)d;
+}
+
 void Assignment(float f, double d, long double ld) {
   d = f;  //expected-warning{{implicit conversion increases floating-point 
precision: 'float' to 'double'}}
   ld = f; //expected-warning{{implicit conversion increases floating-point 
precision: 'float' to 'long double'}}
   ld = d; //expected-warning{{implicit conversion increases floating-point 
precision: 'double' to 'long double'}}
+  d = (double)f;
+  ld = (long double)f;
+  ld = (long double)d;
   f = d;
   f = ld;
   d = ld;
@@ -40,6 +55,9 @@ void ArgumentPassing(float f, double d) {
   DoubleParameter(f); // expected-warning{{implicit conversion increases 
floating-point precision: 'float' to 'double'}}
   LongDoubleParameter(f); // expected-warning{{implicit conversion increases 
floating-point precision: 'float' to 'long double'}}
   LongDoubleParameter(d); // expected-warning{{implicit conversion increases 
floating-point precision: 'double' to 'long double'}}
+  DoubleParameter((double)f);
+  LongDoubleParameter((long double)f);
+  LongDoubleParameter((long double)d);
 }
 
 void BinaryOperator(float f, double d, long double ld) {
@@ -49,12 +67,21 @@ void BinaryOperator(float f, double d, long double ld) {
   f = ld * f; // expected-warning{{implicit conversion increases 
floating-point precision: 'float' to 'long double'}}
   d = d * ld; // expected-warning{{implicit conversion increases 
floating-point precision: 'double' to 'long double'}}
   d = ld * d; // expected-warning{{implicit conversion increases 
floating-point precision: 'double' to 'long double'}}
+  f = (double)f * d;
+  f = d * (double)f;
+  f = (long double)f * ld;
+  f = ld * (long double)f;
+  d = (long double)d * ld;
+  d = ld * (long double)d;
 }
 
 void MultiplicationAssignment(float f, double d, long double ld) {
   d *= f; // expected-warning{{implicit conversion increases floating-point 
precision: 'float' to 'double'}}
   ld *= f; // expected-warning{{implicit conversion increases floating-point 
precision: 'float' to 'long double'}}
   ld *= d; // expected-warning{{implicit conversion increases floating-point 
precision: 'double' to 'long double'}}
+  d *= (double)f;
+  ld *= (long double)f;
+  ld *= (long double)d;
 
   // FIXME: These cases should produce warnings as above.
   f *= d;
diff --git a/clang/test/Sema/warn-double-promotion.cpp 
b/clang/test/Sema/warn-double-promotion.cpp
new file mode 100644
index 0000000000000..677f59a219521
--- /dev/null
+++ b/clang/test/Sema/warn-double-promotion.cpp
@@ -0,0 +1,128 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only %s 
-Wdouble-promotion
+
+float ReturnFloatFromDouble(double d) {
+  return d;
+}
+
+float ReturnFloatFromLongDouble(long double ld) {
+  return ld;
+}
+
+double ReturnDoubleFromLongDouble(long double ld) {
+  return ld;
+}
+
+double ReturnDoubleFromFloat(float f) {
+  return f;  //expected-warning{{implicit conversion increases floating-point 
precision: 'float' to 'double'}}
+}
+
+long double ReturnLongDoubleFromFloat(float f) {
+  return f;  //expected-warning{{implicit conversion increases floating-point 
precision: 'float' to 'long double'}}
+}
+
+long double ReturnLongDoubleFromDouble(double d) {
+  return d;  //expected-warning{{implicit conversion increases floating-point 
precision: 'double' to 'long double'}}
+}
+
+double ReturnDoubleFromFloatWithExplicitCast(float f) {
+  return static_cast<double>(f);
+}
+
+long double ReturnLongDoubleFromFloatWithExplicitCast(float f) {
+  return static_cast<long double>(f);
+}
+
+long double ReturnLongDoubleFromDoubleWithExplicitCast(double d) {
+  return static_cast<long double>(d);
+}
+
+double ReturnDoubleFromFloatWithExplicitListInitialization(float f) {
+  return double{f};
+}
+
+long double ReturnLongDoubleFromFloatWithExplicitListInitialization(float f) {
+  return (long double){f};
+}
+
+long double ReturnLongDoubleFromDoubleWithExplicitListInitialization(double d) 
{
+  return (long double){d};
+}
+
+void Assignment(float f, double d, long double ld) {
+  d = f;  //expected-warning{{implicit conversion increases floating-point 
precision: 'float' to 'double'}}
+  ld = f; //expected-warning{{implicit conversion increases floating-point 
precision: 'float' to 'long double'}}
+  ld = d; //expected-warning{{implicit conversion increases floating-point 
precision: 'double' to 'long double'}}
+  d = static_cast<double>(f);
+  ld = static_cast<long double>(f);
+  ld = static_cast<long double>(d);
+  d = double{f};
+  ld = (long double){f};
+  ld = (long double){d};
+  f = d;
+  f = ld;
+  d = ld;
+}
+
+extern void DoubleParameter(double);
+extern void LongDoubleParameter(long double);
+
+void ArgumentPassing(float f, double d) {
+  DoubleParameter(f); // expected-warning{{implicit conversion increases 
floating-point precision: 'float' to 'double'}}
+  LongDoubleParameter(f); // expected-warning{{implicit conversion increases 
floating-point precision: 'float' to 'long double'}}
+  LongDoubleParameter(d); // expected-warning{{implicit conversion increases 
floating-point precision: 'double' to 'long double'}}
+  DoubleParameter(static_cast<double>(f));
+  LongDoubleParameter(static_cast<long double>(f));
+  LongDoubleParameter(static_cast<long double>(d));
+  DoubleParameter(double{f});
+  LongDoubleParameter((long double){f});
+  LongDoubleParameter((long double){d});
+}
+
+void BinaryOperator(float f, double d, long double ld) {
+  f = f * d; // expected-warning{{implicit conversion increases floating-point 
precision: 'float' to 'double'}}
+  f = d * f; // expected-warning{{implicit conversion increases floating-point 
precision: 'float' to 'double'}}
+  f = f * ld; // expected-warning{{implicit conversion increases 
floating-point precision: 'float' to 'long double'}}
+  f = ld * f; // expected-warning{{implicit conversion increases 
floating-point precision: 'float' to 'long double'}}
+  d = d * ld; // expected-warning{{implicit conversion increases 
floating-point precision: 'double' to 'long double'}}
+  d = ld * d; // expected-warning{{implicit conversion increases 
floating-point precision: 'double' to 'long double'}}
+  f = static_cast<double>(f) * d;
+  f = d * static_cast<double>(f);
+  f = static_cast<long double>(f) * ld;
+  f = ld * static_cast<long double>(f);
+  d = static_cast<long double>(d) * ld;
+  d = ld * static_cast<long double>(d);
+  f = double{f} * d;
+  f = d * double{f};
+  f = (long double){f} * ld;
+  f = ld * (long double){f};
+  d = (long double){d} * ld;
+  d = ld * (long double){d};
+}
+
+void MultiplicationAssignment(float f, double d, long double ld) {
+  d *= f; // expected-warning{{implicit conversion increases floating-point 
precision: 'float' to 'double'}}
+  ld *= f; // expected-warning{{implicit conversion increases floating-point 
precision: 'float' to 'long double'}}
+  ld *= d; // expected-warning{{implicit conversion increases floating-point 
precision: 'double' to 'long double'}}
+  d *= static_cast<double>(f);
+  ld *= static_cast<long double>(f);
+  ld *= static_cast<long double>(d);
+  d *= double{f};
+  ld *= (long double){f};
+  ld *= (long double){d};
+
+  // FIXME: These cases should produce warnings as above.
+  f *= d;
+  f *= ld;
+  d *= ld;
+}
+
+// FIXME: As with a binary operator, the operands to the conditional operator 
are
+// converted to a common type and should produce a warning.
+void ConditionalOperator(float f, double d, long double ld, int i) {
+  f = i ? f : d;
+  f = i ? d : f;
+  f = i ? f : ld;
+  f = i ? ld : f;
+  d = i ? d : ld;
+  d = i ? ld : d;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/159992
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to