Skip to content

Commit 7a8192b

Browse files
committed
[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
1 parent 4f63f70 commit 7a8192b

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

clang/test/Sema/warn-double-promotion.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,25 @@ long double ReturnLongDoubleFromDouble(double d) {
2424
return d; //expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
2525
}
2626

27+
double ReturnDoubleFromFloatWithExplicitCast(float f) {
28+
return (double)f;
29+
}
30+
31+
long double ReturnLongDoubleFromFloatWithExplicitCast(float f) {
32+
return (long double)f;
33+
}
34+
35+
long double ReturnLongDoubleFromDoubleWithExplicitCast(double d) {
36+
return (long double)d;
37+
}
38+
2739
void Assignment(float f, double d, long double ld) {
2840
d = f; //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
2941
ld = f; //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
3042
ld = d; //expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
43+
d = (double)f;
44+
ld = (long double)f;
45+
ld = (long double)d;
3146
f = d;
3247
f = ld;
3348
d = ld;
@@ -40,6 +55,9 @@ void ArgumentPassing(float f, double d) {
4055
DoubleParameter(f); // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
4156
LongDoubleParameter(f); // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
4257
LongDoubleParameter(d); // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
58+
DoubleParameter((double)f);
59+
LongDoubleParameter((long double)f);
60+
LongDoubleParameter((long double)d);
4361
}
4462

4563
void BinaryOperator(float f, double d, long double ld) {
@@ -49,12 +67,21 @@ void BinaryOperator(float f, double d, long double ld) {
4967
f = ld * f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
5068
d = d * ld; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
5169
d = ld * d; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
70+
f = (double)f * d;
71+
f = d * (double)f;
72+
f = (long double)f * ld;
73+
f = ld * (long double)f;
74+
d = (long double)d * ld;
75+
d = ld * (long double)d;
5276
}
5377

5478
void MultiplicationAssignment(float f, double d, long double ld) {
5579
d *= f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
5680
ld *= f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
5781
ld *= d; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
82+
d *= (double)f;
83+
ld *= (long double)f;
84+
ld *= (long double)d;
5885

5986
// FIXME: These cases should produce warnings as above.
6087
f *= d;
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only %s -Wdouble-promotion
2+
3+
float ReturnFloatFromDouble(double d) {
4+
return d;
5+
}
6+
7+
float ReturnFloatFromLongDouble(long double ld) {
8+
return ld;
9+
}
10+
11+
double ReturnDoubleFromLongDouble(long double ld) {
12+
return ld;
13+
}
14+
15+
double ReturnDoubleFromFloat(float f) {
16+
return f; //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
17+
}
18+
19+
long double ReturnLongDoubleFromFloat(float f) {
20+
return f; //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
21+
}
22+
23+
long double ReturnLongDoubleFromDouble(double d) {
24+
return d; //expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
25+
}
26+
27+
double ReturnDoubleFromFloatWithExplicitCast(float f) {
28+
return static_cast<double>(f);
29+
}
30+
31+
long double ReturnLongDoubleFromFloatWithExplicitCast(float f) {
32+
return static_cast<long double>(f);
33+
}
34+
35+
long double ReturnLongDoubleFromDoubleWithExplicitCast(double d) {
36+
return static_cast<long double>(d);
37+
}
38+
39+
double ReturnDoubleFromFloatWithExplicitListInitialization(float f) {
40+
return double{f};
41+
}
42+
43+
long double ReturnLongDoubleFromFloatWithExplicitListInitialization(float f) {
44+
return (long double){f};
45+
}
46+
47+
long double ReturnLongDoubleFromDoubleWithExplicitListInitialization(double d) {
48+
return (long double){d};
49+
}
50+
51+
void Assignment(float f, double d, long double ld) {
52+
d = f; //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
53+
ld = f; //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
54+
ld = d; //expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
55+
d = static_cast<double>(f);
56+
ld = static_cast<long double>(f);
57+
ld = static_cast<long double>(d);
58+
d = double{f};
59+
ld = (long double){f};
60+
ld = (long double){d};
61+
f = d;
62+
f = ld;
63+
d = ld;
64+
}
65+
66+
extern void DoubleParameter(double);
67+
extern void LongDoubleParameter(long double);
68+
69+
void ArgumentPassing(float f, double d) {
70+
DoubleParameter(f); // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
71+
LongDoubleParameter(f); // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
72+
LongDoubleParameter(d); // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
73+
DoubleParameter(static_cast<double>(f));
74+
LongDoubleParameter(static_cast<long double>(f));
75+
LongDoubleParameter(static_cast<long double>(d));
76+
DoubleParameter(double{f});
77+
LongDoubleParameter((long double){f});
78+
LongDoubleParameter((long double){d});
79+
}
80+
81+
void BinaryOperator(float f, double d, long double ld) {
82+
f = f * d; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
83+
f = d * f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
84+
f = f * ld; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
85+
f = ld * f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
86+
d = d * ld; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
87+
d = ld * d; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
88+
f = static_cast<double>(f) * d;
89+
f = d * static_cast<double>(f);
90+
f = static_cast<long double>(f) * ld;
91+
f = ld * static_cast<long double>(f);
92+
d = static_cast<long double>(d) * ld;
93+
d = ld * static_cast<long double>(d);
94+
f = double{f} * d;
95+
f = d * double{f};
96+
f = (long double){f} * ld;
97+
f = ld * (long double){f};
98+
d = (long double){d} * ld;
99+
d = ld * (long double){d};
100+
}
101+
102+
void MultiplicationAssignment(float f, double d, long double ld) {
103+
d *= f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
104+
ld *= f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
105+
ld *= d; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
106+
d *= static_cast<double>(f);
107+
ld *= static_cast<long double>(f);
108+
ld *= static_cast<long double>(d);
109+
d *= double{f};
110+
ld *= (long double){f};
111+
ld *= (long double){d};
112+
113+
// FIXME: These cases should produce warnings as above.
114+
f *= d;
115+
f *= ld;
116+
d *= ld;
117+
}
118+
119+
// FIXME: As with a binary operator, the operands to the conditional operator are
120+
// converted to a common type and should produce a warning.
121+
void ConditionalOperator(float f, double d, long double ld, int i) {
122+
f = i ? f : d;
123+
f = i ? d : f;
124+
f = i ? f : ld;
125+
f = i ? ld : f;
126+
d = i ? d : ld;
127+
d = i ? ld : d;
128+
}

0 commit comments

Comments
 (0)