Skip to content

Commit 5b780c8

Browse files
authored
Diagnose invalid fixed point conversion (#80763)
1 parent ce00fdc commit 5b780c8

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

clang/include/clang/AST/Type.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,6 +2613,9 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
26132613
/// Return true if this is a fixed point or integer type.
26142614
bool isFixedPointOrIntegerType() const;
26152615

2616+
/// Return true if this can be converted to (or from) a fixed point type.
2617+
bool isConvertibleToFixedPointType() const;
2618+
26162619
/// Return true if this is a saturated fixed point type according to
26172620
/// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned.
26182621
bool isSaturatedFixedPointType() const;
@@ -7493,6 +7496,10 @@ inline bool Type::isFixedPointOrIntegerType() const {
74937496
return isFixedPointType() || isIntegerType();
74947497
}
74957498

7499+
inline bool Type::isConvertibleToFixedPointType() const {
7500+
return isRealFloatingType() || isFixedPointOrIntegerType();
7501+
}
7502+
74967503
inline bool Type::isSaturatedFixedPointType() const {
74977504
if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) {
74987505
return BT->getKind() >= BuiltinType::SatShortAccum &&

clang/lib/Sema/SemaOverload.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2177,7 +2177,10 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType,
21772177
From->isIntegerConstantExpr(S.getASTContext())) {
21782178
SCS.Second = ICK_Compatible_Conversion;
21792179
FromType = ToType;
2180-
} else if (ToType->isFixedPointType() || FromType->isFixedPointType()) {
2180+
} else if ((ToType->isFixedPointType() &&
2181+
FromType->isConvertibleToFixedPointType()) ||
2182+
(FromType->isFixedPointType() &&
2183+
ToType->isConvertibleToFixedPointType())) {
21812184
SCS.Second = ICK_Fixed_Point_Conversion;
21822185
FromType = ToType;
21832186
} else {

clang/test/Frontend/fixed_point_errors.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ int fract_int = 10r; // expected-error{{invalid suffix 'r' on integer consta
1414
float accum_flt = 0.0k; // expected-error{{invalid suffix 'k' on floating constant}}
1515
float fract_flt = 0.0r; // expected-error{{invalid suffix 'r' on floating constant}}
1616
#endif
17+
18+
#ifndef WITHOUT_FIXED_POINT
19+
const char *c = 10.0k; // expected-error{{cannot initialize a variable of type 'const char *' with an rvalue of type '_Accum'}}
20+
#endif

0 commit comments

Comments
 (0)