diff options
author | PiJoules <6019989+PiJoules@users.noreply.github.com> | 2024-02-06 11:29:30 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-06 11:29:30 -0800 |
commit | 5b780c8c6c558ec283a9eec485a4f172df0f9fe1 (patch) | |
tree | b851c6db9c307a34cdb4ed1eb0c9628b4c24ec0d | |
parent | ce00fdc91cb7a466054c3ffd8788ae2f9f5100f3 (diff) | |
download | llvm-5b780c8c6c558ec283a9eec485a4f172df0f9fe1.zip llvm-5b780c8c6c558ec283a9eec485a4f172df0f9fe1.tar.gz llvm-5b780c8c6c558ec283a9eec485a4f172df0f9fe1.tar.bz2 |
Diagnose invalid fixed point conversion (#80763)
-rw-r--r-- | clang/include/clang/AST/Type.h | 7 | ||||
-rw-r--r-- | clang/lib/Sema/SemaOverload.cpp | 5 | ||||
-rw-r--r-- | clang/test/Frontend/fixed_point_errors.cpp | 4 |
3 files changed, 15 insertions, 1 deletions
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h index d6a55f3..1942b0e 100644 --- a/clang/include/clang/AST/Type.h +++ b/clang/include/clang/AST/Type.h @@ -2613,6 +2613,9 @@ public: /// Return true if this is a fixed point or integer type. bool isFixedPointOrIntegerType() const; + /// Return true if this can be converted to (or from) a fixed point type. + bool isConvertibleToFixedPointType() const; + /// Return true if this is a saturated fixed point type according to /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned. bool isSaturatedFixedPointType() const; @@ -7493,6 +7496,10 @@ inline bool Type::isFixedPointOrIntegerType() const { return isFixedPointType() || isIntegerType(); } +inline bool Type::isConvertibleToFixedPointType() const { + return isRealFloatingType() || isFixedPointOrIntegerType(); +} + inline bool Type::isSaturatedFixedPointType() const { if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { return BT->getKind() >= BuiltinType::SatShortAccum && diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 6a04d68..c46f633 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -2177,7 +2177,10 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, From->isIntegerConstantExpr(S.getASTContext())) { SCS.Second = ICK_Compatible_Conversion; FromType = ToType; - } else if (ToType->isFixedPointType() || FromType->isFixedPointType()) { + } else if ((ToType->isFixedPointType() && + FromType->isConvertibleToFixedPointType()) || + (FromType->isFixedPointType() && + ToType->isConvertibleToFixedPointType())) { SCS.Second = ICK_Fixed_Point_Conversion; FromType = ToType; } else { diff --git a/clang/test/Frontend/fixed_point_errors.cpp b/clang/test/Frontend/fixed_point_errors.cpp index 4097cd7..ef064bc 100644 --- a/clang/test/Frontend/fixed_point_errors.cpp +++ b/clang/test/Frontend/fixed_point_errors.cpp @@ -14,3 +14,7 @@ int fract_int = 10r; // expected-error{{invalid suffix 'r' on integer consta float accum_flt = 0.0k; // expected-error{{invalid suffix 'k' on floating constant}} float fract_flt = 0.0r; // expected-error{{invalid suffix 'r' on floating constant}} #endif + +#ifndef WITHOUT_FIXED_POINT +const char *c = 10.0k; // expected-error{{cannot initialize a variable of type 'const char *' with an rvalue of type '_Accum'}} +#endif |