-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang] Fix -Wdouble-promotion in C++ list-initialization #159992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Sirraide
merged 13 commits into
llvm:main
from
mjacobse:fix_explicit_brace_init_double_promotion_warning
Sep 24, 2025
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4f63f70
[clang] Fix -Wdouble-promotion in C++ list-init
mjacobse 7a8192b
[clang] Test -Wdouble-promotion with explicit cast
mjacobse 2e32a97
Use long double alias to avoid compound literals
mjacobse 16a3ddd
Avoid duplication of non-C++ specific tests
mjacobse 77def97
Add tests for function style casts
mjacobse 900976c
Avoid unintended suppression of -Wdouble-promotion
mjacobse 2cdee44
Add C++-specific init tests for -Wdouble-promotion
mjacobse 63cc407
Fix formatting
mjacobse 665c723
Inline silencing of -Wdouble-promotion
mjacobse 0039d85
Add -Wdouble-promotion tests with extra parens
mjacobse 090aa2a
Add more specific C++ -Wdouble-promotion
mjacobse 377991b
Fix grammar in comment
mjacobse 7d5a915
Add -Wdouble-promotion test with extra braces
mjacobse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only %s -Wdouble-promotion | ||
|
|
||
| using LongDouble = 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 LongDouble{f}; | ||
| } | ||
|
|
||
| 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 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); | ||
| 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); | ||
|
|
||
| void ArgumentPassing(float f, double d) { | ||
| DoubleParameter(static_cast<double>(f)); | ||
| LongDoubleParameter(static_cast<long double>(f)); | ||
| LongDoubleParameter(static_cast<long 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) { | ||
| 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 = LongDouble{f} * 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) { | ||
| 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); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.