From d4408fe17f33bcd664ec8f468abfd1094e84a7c1 Mon Sep 17 00:00:00 2001 From: Gousemoodhin Nadaf Date: Tue, 11 Aug 2020 16:48:24 +0200 Subject: [PATCH] [clang] Do not crash for unsupported fixed point to floating point conversion - Fixed point to floating point conversion is unimplemented. - If one of the operands has a floating type and the other operand has a fixed-point type, the function handleFloatConversion() is called because one of the operands has a floating type, but we do not handle fixed point type in this function (Implementation of fixed point to floating point conversion is missing), due to this compiler crashes. In order to avoid compiler crash, when one of the operands has a floating type and the other operand has a fixed-point type, return NULL. - FIXME: Implementation of fixed point to floating point conversion. - I am going to resolve FIXME in followup patches. - Add the test case. Reviewed By: ebevhan Differential Revision: https://reviews.llvm.org/D81904 --- clang/lib/Sema/SemaExpr.cpp | 5 +++++ clang/test/Frontend/fixed_point_errors.c | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 79340a9c7d7b..bea693f999ab 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -1125,6 +1125,11 @@ static QualType handleFloatConversion(Sema &S, ExprResult &LHS, bool LHSFloat = LHSType->isRealFloatingType(); bool RHSFloat = RHSType->isRealFloatingType(); + // FIXME: Implement floating to fixed point conversion.(Bug 46268) + // Reference N1169 4.1.4 (Type conversion, usual arithmetic conversions). + if ((LHSType->isFixedPointType() && RHSFloat) || + (LHSFloat && RHSType->isFixedPointType())) + return QualType(); // If we have two real floating types, convert the smaller operand // to the bigger result. if (LHSFloat && RHSFloat) { diff --git a/clang/test/Frontend/fixed_point_errors.c b/clang/test/Frontend/fixed_point_errors.c index 6c41bf6df163..5aaf59876dcb 100644 --- a/clang/test/Frontend/fixed_point_errors.c +++ b/clang/test/Frontend/fixed_point_errors.c @@ -286,3 +286,8 @@ short _Accum shl_sat = (_Sat short _Accum)200.0hk << 5; // Division by zero short _Accum div_zero = 4.5k / 0.0lr; // expected-error {{initializer element is not a compile-time constant}} + +void foo(void) { + _Accum x = 0.5k; + if (x == 0.5) {} // expected-error{{invalid operands to binary expression ('_Accum' and 'double')}} +}