[clang] [clang] Fix -Wdouble-promotion in C++ list-initialization (PR #159992)
Marcel Jacobse via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 24 11:10:19 PDT 2025
https://github.com/mjacobse updated https://github.com/llvm/llvm-project/pull/159992
>From 4f63f703b5bb56d2def94b99cd05212652605e42 Mon Sep 17 00:00:00 2001
From: mjacobse <44684927+mjacobse at users.noreply.github.com>
Date: Sun, 21 Sep 2025 18:06:20 +0200
Subject: [PATCH 01/13] [clang] Fix -Wdouble-promotion in C++ list-init
A C++ list-initialization explicitly asks for the promotion to happen,
much like an explicit static_cast, so it should not be warned about.
Fixes #33409
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaChecking.cpp | 5 +++++
2 files changed, 6 insertions(+)
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;
>From 7a8192b294436ceea93f4ce38371e85d8b331dd3 Mon Sep 17 00:00:00 2001
From: mjacobse <44684927+mjacobse at users.noreply.github.com>
Date: Sun, 21 Sep 2025 18:13:22 +0200
Subject: [PATCH 02/13] [clang] Test -Wdouble-promotion with explicit cast
Add tests to ensure that -Wdouble-promotion does not warn when promotion
is asked for explicitly by an explicit cast or C++ list initialization.
For the latter this creates a .cpp version of warn-double-promotion.c.
Test case for #33409
---
clang/test/Sema/warn-double-promotion.c | 27 +++++
clang/test/Sema/warn-double-promotion.cpp | 128 ++++++++++++++++++++++
2 files changed, 155 insertions(+)
create mode 100644 clang/test/Sema/warn-double-promotion.cpp
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;
+}
>From 2e32a977bea9b9ae07db7f49cec87976e61ac8c8 Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Mon, 22 Sep 2025 16:30:20 +0200
Subject: [PATCH 03/13] Use long double alias to avoid compound literals
With (long double){f} we were getting the compound literal construction
from C99 instead of the intended C++ function-style cast with
list-initialization syntax.
---
clang/test/Sema/warn-double-promotion.cpp | 26 ++++++++++++-----------
1 file changed, 14 insertions(+), 12 deletions(-)
diff --git a/clang/test/Sema/warn-double-promotion.cpp b/clang/test/Sema/warn-double-promotion.cpp
index 677f59a219521..5f4e327ab9d06 100644
--- a/clang/test/Sema/warn-double-promotion.cpp
+++ b/clang/test/Sema/warn-double-promotion.cpp
@@ -1,5 +1,7 @@
// RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only %s -Wdouble-promotion
+using LongDouble = long double;
+
float ReturnFloatFromDouble(double d) {
return d;
}
@@ -41,11 +43,11 @@ double ReturnDoubleFromFloatWithExplicitListInitialization(float f) {
}
long double ReturnLongDoubleFromFloatWithExplicitListInitialization(float f) {
- return (long double){f};
+ return LongDouble{f};
}
long double ReturnLongDoubleFromDoubleWithExplicitListInitialization(double d) {
- return (long double){d};
+ return LongDouble{d};
}
void Assignment(float f, double d, long double ld) {
@@ -56,8 +58,8 @@ void Assignment(float f, double d, long double ld) {
ld = static_cast<long double>(f);
ld = static_cast<long double>(d);
d = double{f};
- ld = (long double){f};
- ld = (long double){d};
+ ld = LongDouble{f};
+ ld = LongDouble{d};
f = d;
f = ld;
d = ld;
@@ -74,8 +76,8 @@ void ArgumentPassing(float f, double d) {
LongDoubleParameter(static_cast<long double>(f));
LongDoubleParameter(static_cast<long double>(d));
DoubleParameter(double{f});
- LongDoubleParameter((long double){f});
- LongDoubleParameter((long double){d});
+ LongDoubleParameter(LongDouble{f});
+ LongDoubleParameter(LongDouble{d});
}
void BinaryOperator(float f, double d, long double ld) {
@@ -93,10 +95,10 @@ void BinaryOperator(float f, double d, long double 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};
+ f = LongDouble{f} * ld;
+ f = ld * LongDouble{f};
+ d = LongDouble{d} * ld;
+ d = ld * LongDouble{d};
}
void MultiplicationAssignment(float f, double d, long double ld) {
@@ -107,8 +109,8 @@ void MultiplicationAssignment(float f, double d, long double ld) {
ld *= static_cast<long double>(f);
ld *= static_cast<long double>(d);
d *= double{f};
- ld *= (long double){f};
- ld *= (long double){d};
+ ld *= LongDouble{f};
+ ld *= LongDouble{d};
// FIXME: These cases should produce warnings as above.
f *= d;
>From 16a3ddd9b1c0d5e60b8f99ec2581b177f5534b38 Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Mon, 22 Sep 2025 16:36:19 +0200
Subject: [PATCH 04/13] Avoid duplication of non-C++ specific tests
Run warn-double-promotion.c in both C and C++ mode and only add
those C++ tests with warn-double-promotion.cpp that are not
valid C.
---
clang/test/Sema/warn-double-promotion.c | 1 +
clang/test/Sema/warn-double-promotion.cpp | 59 +----------------------
2 files changed, 2 insertions(+), 58 deletions(-)
diff --git a/clang/test/Sema/warn-double-promotion.c b/clang/test/Sema/warn-double-promotion.c
index ac9e9499bc2b7..0e56e1c93ce5d 100644
--- a/clang/test/Sema/warn-double-promotion.c
+++ b/clang/test/Sema/warn-double-promotion.c
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only %s -Wdouble-promotion
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only -x c++ %s -Wdouble-promotion
float ReturnFloatFromDouble(double d) {
return d;
diff --git a/clang/test/Sema/warn-double-promotion.cpp b/clang/test/Sema/warn-double-promotion.cpp
index 5f4e327ab9d06..e618b79264408 100644
--- a/clang/test/Sema/warn-double-promotion.cpp
+++ b/clang/test/Sema/warn-double-promotion.cpp
@@ -1,31 +1,8 @@
// RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only %s -Wdouble-promotion
+// expected-no-diagnostics
using LongDouble = long double;
-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);
}
@@ -51,27 +28,18 @@ long double ReturnLongDoubleFromDoubleWithExplicitListInitialization(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 = LongDouble{f};
ld = LongDouble{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));
@@ -81,12 +49,6 @@ void ArgumentPassing(float f, 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;
@@ -102,29 +64,10 @@ void BinaryOperator(float f, double d, long double ld) {
}
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 *= LongDouble{f};
ld *= LongDouble{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;
}
>From 77def9792947a6c0c188da3d1a6ab9a1b752a708 Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Mon, 22 Sep 2025 16:45:14 +0200
Subject: [PATCH 05/13] Add tests for function style casts
---
clang/test/Sema/warn-double-promotion.cpp | 27 +++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/clang/test/Sema/warn-double-promotion.cpp b/clang/test/Sema/warn-double-promotion.cpp
index e618b79264408..cfdbabbcdadc1 100644
--- a/clang/test/Sema/warn-double-promotion.cpp
+++ b/clang/test/Sema/warn-double-promotion.cpp
@@ -27,6 +27,18 @@ long double ReturnLongDoubleFromDoubleWithExplicitListInitialization(double d) {
return LongDouble{d};
}
+double ReturnDoubleFromFloatWithFunctionStyleCast(float f) {
+ return double(f);
+}
+
+long double ReturnLongDoubleFromFloatWithFunctionStyleCast(float f) {
+ return LongDouble(f);
+}
+
+long double ReturnLongDoubleFromDoubleWithFunctionStyleCast(double d) {
+ return LongDouble(d);
+}
+
void Assignment(float f, double d, long double ld) {
d = static_cast<double>(f);
ld = static_cast<long double>(f);
@@ -34,6 +46,9 @@ void Assignment(float f, double d, long double ld) {
d = double{f};
ld = LongDouble{f};
ld = LongDouble{d};
+ d = double(f);
+ ld = LongDouble(f);
+ ld = LongDouble(d);
}
extern void DoubleParameter(double);
@@ -46,6 +61,9 @@ void ArgumentPassing(float f, double d) {
DoubleParameter(double{f});
LongDoubleParameter(LongDouble{f});
LongDoubleParameter(LongDouble{d});
+ DoubleParameter(double(f));
+ LongDoubleParameter(LongDouble(f));
+ LongDoubleParameter(LongDouble(d));
}
void BinaryOperator(float f, double d, long double ld) {
@@ -61,6 +79,12 @@ void BinaryOperator(float f, double d, long double ld) {
f = ld * LongDouble{f};
d = LongDouble{d} * ld;
d = ld * LongDouble{d};
+ f = double(f) * d;
+ f = d * double(f);
+ f = LongDouble(f) * ld;
+ f = ld * LongDouble(f);
+ d = LongDouble(d) * ld;
+ d = ld * LongDouble(d);
}
void MultiplicationAssignment(float f, double d, long double ld) {
@@ -70,4 +94,7 @@ void MultiplicationAssignment(float f, double d, long double ld) {
d *= double{f};
ld *= LongDouble{f};
ld *= LongDouble{d};
+ d *= double(f);
+ ld *= LongDouble(f);
+ ld *= LongDouble(d);
}
>From 900976cb9fb8a57158d64e4a8d8271ff3f9d7d73 Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Mon, 22 Sep 2025 16:55:12 +0200
Subject: [PATCH 06/13] Avoid unintended suppression of -Wdouble-promotion
This reverts commit 4f63f703b5bb56d2def94b99cd05212652605e42
which was the previous attempt to suppress -Wdouble-promotion when
explicitly asked for with C++ list initialization. Just checking if
we are somehow in a list initialization was way too loose and
suppressed many cases that should not have been.
The new fix works with the observation that the case of explicit
C++ list initialization in this case turns into a
CXXFunctionalCastExpr with InitListExpr as direct child. While the
CXXFunctionalCastExpr is ignored when checking for implicit
conversions, the InitListExpr is not, which causes the
-Wdouble-promotion warning. To fix this, treat the InitListExpr as
part of the CXXFunctionalCastExpr and skip it too.
---
clang/lib/Sema/SemaChecking.cpp | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index d5b2cde0d1c09..f25496915a5da 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -12382,11 +12382,6 @@ 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;
@@ -12802,6 +12797,22 @@ static void CheckCommaOperand(
S.CheckImplicitConversion(E, T, CC);
}
+static Expr* IgnoreExplicitCastForImplicitConversionCheck(ExplicitCastExpr *E) {
+ // In the special case of C++ function-style cast with braces,
+ // CXXFunctionalCastExpr has InitListExpr as direct child with a single
+ // initializer. It basically belongs to the cast itself, so for the purposes
+ // of checking for implicit conversions to warn about it should be skipped
+ // too.
+ if (auto *FCE = dyn_cast<CXXFunctionalCastExpr>(E)) {
+ if (auto *IFCE = dyn_cast<InitListExpr>(FCE->getSubExpr())) {
+ if (IFCE->getNumInits() == 1) {
+ return IFCE->getInit(0);
+ }
+ }
+ }
+ return E->getSubExpr();
+}
+
/// Data recursive variant of AnalyzeImplicitConversions. Subexpressions
/// that should be visited are added to WorkList.
static void AnalyzeImplicitConversions(
@@ -12914,7 +12925,7 @@ static void AnalyzeImplicitConversions(
// Skip past explicit casts.
if (auto *CE = dyn_cast<ExplicitCastExpr>(E)) {
- E = CE->getSubExpr()->IgnoreParenImpCasts();
+ E = IgnoreExplicitCastForImplicitConversionCheck(CE)->IgnoreParenImpCasts();
if (!CE->getType()->isVoidType() && E->getType()->isAtomicType())
S.Diag(E->getBeginLoc(), diag::warn_atomic_implicit_seq_cst);
WorkList.push_back({E, CC, IsListInit});
>From 2cdee44f2d9cf26b28138c9d64038b770853e62d Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Mon, 22 Sep 2025 17:18:39 +0200
Subject: [PATCH 07/13] Add C++-specific init tests for -Wdouble-promotion
---
clang/test/Sema/warn-double-promotion.cpp | 47 ++++++++++++++++++++++-
1 file changed, 46 insertions(+), 1 deletion(-)
diff --git a/clang/test/Sema/warn-double-promotion.cpp b/clang/test/Sema/warn-double-promotion.cpp
index cfdbabbcdadc1..12ac7e6fc7fe5 100644
--- a/clang/test/Sema/warn-double-promotion.cpp
+++ b/clang/test/Sema/warn-double-promotion.cpp
@@ -1,5 +1,4 @@
// RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only %s -Wdouble-promotion
-// expected-no-diagnostics
using LongDouble = long double;
@@ -39,6 +38,52 @@ long double ReturnLongDoubleFromDoubleWithFunctionStyleCast(double d) {
return LongDouble(d);
}
+void InitializationWithParens(float f, double d) {
+ {
+ double d(f); // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+ long double ld0(f); // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+ long double ld1(d); // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+ }
+ {
+ double d(static_cast<double>(f));
+ long double ld0(static_cast<long double>(f));
+ long double ld1(static_cast<long double>(d));
+ }
+ {
+ double d(double{f});
+ long double ld0(LongDouble{f});
+ long double ld1(LongDouble{d});
+ }
+ {
+ double d((double(f)));
+ long double ld0((LongDouble(f)));
+ long double ld1((LongDouble(d)));
+ }
+}
+
+void InitializationWithBraces(float f, double d) {
+ {
+ double d{f}; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+ long double ld0{f}; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+ long double ld1{d}; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+ }
+ {
+ double d{static_cast<double>(f)};
+ long double ld0{static_cast<long double>(f)};
+ long double ld1{static_cast<long double>(d)};
+ }
+ {
+ double d{double{f}};
+ long double ld0{LongDouble{f}};
+ long double ld1{LongDouble{d}};
+ }
+ {
+ double d{double(f)};
+ long double ld0{LongDouble(f)};
+ long double ld1{LongDouble(d)};
+ }
+}
+
void Assignment(float f, double d, long double ld) {
d = static_cast<double>(f);
ld = static_cast<long double>(f);
>From 63cc4073596eecf2bd57ac881fef86ee4ee376f9 Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Mon, 22 Sep 2025 21:05:40 +0200
Subject: [PATCH 08/13] Fix formatting
---
clang/lib/Sema/SemaChecking.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index f25496915a5da..68dcd7ef40380 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -12797,7 +12797,7 @@ static void CheckCommaOperand(
S.CheckImplicitConversion(E, T, CC);
}
-static Expr* IgnoreExplicitCastForImplicitConversionCheck(ExplicitCastExpr *E) {
+static Expr *IgnoreExplicitCastForImplicitConversionCheck(ExplicitCastExpr *E) {
// In the special case of C++ function-style cast with braces,
// CXXFunctionalCastExpr has InitListExpr as direct child with a single
// initializer. It basically belongs to the cast itself, so for the purposes
>From 665c723747837e0283794333e44daa053327e47d Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Tue, 23 Sep 2025 20:16:38 +0200
Subject: [PATCH 09/13] Inline silencing of -Wdouble-promotion
To make it more clear what's going on
---
clang/lib/Sema/SemaChecking.cpp | 30 +++++++++++++-----------------
1 file changed, 13 insertions(+), 17 deletions(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 68dcd7ef40380..6f487535b50b4 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -12797,22 +12797,6 @@ static void CheckCommaOperand(
S.CheckImplicitConversion(E, T, CC);
}
-static Expr *IgnoreExplicitCastForImplicitConversionCheck(ExplicitCastExpr *E) {
- // In the special case of C++ function-style cast with braces,
- // CXXFunctionalCastExpr has InitListExpr as direct child with a single
- // initializer. It basically belongs to the cast itself, so for the purposes
- // of checking for implicit conversions to warn about it should be skipped
- // too.
- if (auto *FCE = dyn_cast<CXXFunctionalCastExpr>(E)) {
- if (auto *IFCE = dyn_cast<InitListExpr>(FCE->getSubExpr())) {
- if (IFCE->getNumInits() == 1) {
- return IFCE->getInit(0);
- }
- }
- }
- return E->getSubExpr();
-}
-
/// Data recursive variant of AnalyzeImplicitConversions. Subexpressions
/// that should be visited are added to WorkList.
static void AnalyzeImplicitConversions(
@@ -12925,7 +12909,19 @@ static void AnalyzeImplicitConversions(
// Skip past explicit casts.
if (auto *CE = dyn_cast<ExplicitCastExpr>(E)) {
- E = IgnoreExplicitCastForImplicitConversionCheck(CE)->IgnoreParenImpCasts();
+ E = CE->getSubExpr();
+ // In the special case of C++ function-style cast with braces,
+ // CXXFunctionalCastExpr has InitListExpr as direct child with a single
+ // initializer. This InitListExpr basically belongs to the cast itself, so
+ // we skip it too. Specifically this is needed to silence -Wdouble-promotion
+ if (isa<CXXFunctionalCastExpr>(CE)) {
+ if (auto *InitListE = dyn_cast<InitListExpr>(E)) {
+ if (InitListE->getNumInits() == 1) {
+ E = InitListE->getInit(0);
+ }
+ }
+ }
+ E = E->IgnoreParenImpCasts();
if (!CE->getType()->isVoidType() && E->getType()->isAtomicType())
S.Diag(E->getBeginLoc(), diag::warn_atomic_implicit_seq_cst);
WorkList.push_back({E, CC, IsListInit});
>From 0039d85f54cbe54aef2ca1d85c56d849effd5495 Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Tue, 23 Sep 2025 21:25:36 +0200
Subject: [PATCH 10/13] Add -Wdouble-promotion tests with extra parens
---
clang/test/Sema/warn-double-promotion.c | 9 +++++++++
clang/test/Sema/warn-double-promotion.cpp | 12 ++++++++++++
2 files changed, 21 insertions(+)
diff --git a/clang/test/Sema/warn-double-promotion.c b/clang/test/Sema/warn-double-promotion.c
index 0e56e1c93ce5d..7b06658bf4cdf 100644
--- a/clang/test/Sema/warn-double-promotion.c
+++ b/clang/test/Sema/warn-double-promotion.c
@@ -49,6 +49,15 @@ void Assignment(float f, double d, long double ld) {
d = ld;
}
+void AssignmentWithExtraParens(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);
+}
+
extern void DoubleParameter(double);
extern void LongDoubleParameter(long double);
diff --git a/clang/test/Sema/warn-double-promotion.cpp b/clang/test/Sema/warn-double-promotion.cpp
index 12ac7e6fc7fe5..1ca6a816ed8b7 100644
--- a/clang/test/Sema/warn-double-promotion.cpp
+++ b/clang/test/Sema/warn-double-promotion.cpp
@@ -96,6 +96,18 @@ void Assignment(float f, double d, long double ld) {
ld = LongDouble(d);
}
+void AssignmentWithExtraParens(float f, double d, long double ld) {
+ d = static_cast<double>((f));
+ ld = static_cast<long double>((f));
+ ld = static_cast<long double>((d));
+ d = double{(f)};
+ ld = LongDouble{(f)};
+ ld = LongDouble{(d)};
+ d = double((f));
+ ld = LongDouble((f));
+ ld = LongDouble((d));
+}
+
extern void DoubleParameter(double);
extern void LongDoubleParameter(long double);
>From 090aa2a8d7446eb47db7892b5300634c65c63bf2 Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Tue, 23 Sep 2025 21:33:09 +0200
Subject: [PATCH 11/13] Add more specific C++ -Wdouble-promotion
Includes tests with classes and templates
---
clang/test/Sema/warn-double-promotion.cpp | 93 +++++++++++++++++++++++
1 file changed, 93 insertions(+)
diff --git a/clang/test/Sema/warn-double-promotion.cpp b/clang/test/Sema/warn-double-promotion.cpp
index 1ca6a816ed8b7..7da273dc886f6 100644
--- a/clang/test/Sema/warn-double-promotion.cpp
+++ b/clang/test/Sema/warn-double-promotion.cpp
@@ -155,3 +155,96 @@ void MultiplicationAssignment(float f, double d, long double ld) {
ld *= LongDouble(f);
ld *= LongDouble(d);
}
+
+struct ConstructWithDouble {
+ ConstructWithDouble(double);
+};
+
+struct ConstructWithLongDouble {
+ ConstructWithLongDouble(long double);
+};
+
+void Construct(float f, double d) {
+ ConstructWithDouble{f}; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+ ConstructWithLongDouble{f}; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+ ConstructWithLongDouble{d}; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+ ConstructWithDouble{static_cast<double>(f)};
+ ConstructWithLongDouble{static_cast<long double>(f)};
+ ConstructWithLongDouble{static_cast<long double>(d)};
+ ConstructWithDouble{double{f}};
+ ConstructWithLongDouble{LongDouble{f}};
+ ConstructWithLongDouble{LongDouble{d}};
+ ConstructWithDouble{double(f)};
+ ConstructWithLongDouble{LongDouble(f)};
+ ConstructWithLongDouble{LongDouble(d)};
+}
+
+template <class T> T ReturnTFromFloat(float f) {
+ return f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}} \
+ // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+}
+
+template <class T> T ReturnTFromDouble(double d) {
+ return d; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+}
+
+template <class T> T ReturnTFromFloatWithStaticCast(float f) {
+ return static_cast<T>(f);
+}
+
+template <class T> T ReturnTFromDoubleWithStaticCast(double d) {
+ return static_cast<T>(d);
+}
+
+template <class T> T ReturnTFromFloatWithExplicitListInitialization(float f) {
+ return T{f};
+}
+
+template <class T> T ReturnTFromDoubleWithExplicitListInitialization(double d) {
+ return T{d};
+}
+
+template <class T> T ReturnTFromFloatWithFunctionStyleCast(float f) {
+ return T(f);
+}
+
+template <class T> T ReturnTFromDoubleWithFunctionStyleCast(double d) {
+ return T(d);
+}
+
+void TestTemplate(float f, double d) {
+ ReturnTFromFloat<double>(f); // expected-note{{in instantiation of function template specialization 'ReturnTFromFloat<double>' requested here}}
+ ReturnTFromFloat<long double>(f); // expected-note{{in instantiation of function template specialization 'ReturnTFromFloat<long double>' requested here}}
+ ReturnTFromDouble<long double>(d); // expected-note{{in instantiation of function template specialization 'ReturnTFromDouble<long double>' requested here}}
+ ReturnTFromFloatWithStaticCast<double>(f);
+ ReturnTFromFloatWithStaticCast<long double>(f);
+ ReturnTFromDoubleWithStaticCast<long double>(d);
+ ReturnTFromFloatWithExplicitListInitialization<double>(f);
+ ReturnTFromFloatWithExplicitListInitialization<long double>(f);
+ ReturnTFromDoubleWithExplicitListInitialization<long double>(d);
+ ReturnTFromFloatWithFunctionStyleCast<double>(f);
+ ReturnTFromFloatWithFunctionStyleCast<long double>(f);
+ ReturnTFromDoubleWithFunctionStyleCast<long double>(d);
+}
+
+struct MemberInitializerListParens {
+ double m_d;
+ long double m_ld0;
+ long double m_ld1;
+ MemberInitializerListParens(float f, double d):
+ m_d(f), // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+ m_ld0(f), // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+ m_ld1(d) // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+ {}
+};
+
+struct MemberInitializerListBraces {
+ double m_d;
+ long double m_ld0;
+ long double m_ld1;
+ MemberInitializerListBraces(float f, double d):
+ m_d{f}, // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+ m_ld0{f}, // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+ m_ld1{d} // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+ {}
+};
>From 377991bfa0f04aaa65c45c6683ca348d6a0683cb Mon Sep 17 00:00:00 2001
From: Marcel Jacobse <mjacobse at uni-bremen.de>
Date: Wed, 24 Sep 2025 20:07:30 +0200
Subject: [PATCH 12/13] Fix grammar in comment
Co-authored-by: Sirraide <aeternalmail at gmail.com>
---
clang/lib/Sema/SemaChecking.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 6f487535b50b4..594ef644f8828 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -12910,8 +12910,8 @@ static void AnalyzeImplicitConversions(
// Skip past explicit casts.
if (auto *CE = dyn_cast<ExplicitCastExpr>(E)) {
E = CE->getSubExpr();
- // In the special case of C++ function-style cast with braces,
- // CXXFunctionalCastExpr has InitListExpr as direct child with a single
+ // In the special case of a C++ function-style cast with braces,
+ // CXXFunctionalCastExpr has an InitListExpr as direct child with a single
// initializer. This InitListExpr basically belongs to the cast itself, so
// we skip it too. Specifically this is needed to silence -Wdouble-promotion
if (isa<CXXFunctionalCastExpr>(CE)) {
>From 7d5a915f683cd0704296ed3a39fafc7808471eaa Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Wed, 24 Sep 2025 20:09:00 +0200
Subject: [PATCH 13/13] Add -Wdouble-promotion test with extra braces
---
clang/test/Sema/warn-double-promotion.cpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/clang/test/Sema/warn-double-promotion.cpp b/clang/test/Sema/warn-double-promotion.cpp
index 7da273dc886f6..886911244fbd7 100644
--- a/clang/test/Sema/warn-double-promotion.cpp
+++ b/clang/test/Sema/warn-double-promotion.cpp
@@ -108,6 +108,12 @@ void AssignmentWithExtraParens(float f, double d, long double ld) {
ld = LongDouble((d));
}
+void AssignmentWithExtraBraces(float f, double d, long double ld) {
+ d = double{{f}}; // expected-warning{{too many braces around scalar initializer}}
+ ld = LongDouble{{f}}; // expected-warning{{too many braces around scalar initializer}}
+ ld = LongDouble{{d}}; // expected-warning{{too many braces around scalar initializer}}
+}
+
extern void DoubleParameter(double);
extern void LongDoubleParameter(long double);
More information about the cfe-commits
mailing list