[clang] b2d62c9 - [clang] Ensure fixed point conversions work in C++ (#68344)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 16 13:11:19 PST 2023
Author: PiJoules
Date: 2023-11-16T13:11:15-08:00
New Revision: b2d62c9a58433e2a2ca8d2c9cd6b0b612dca2e76
URL: https://github.com/llvm/llvm-project/commit/b2d62c9a58433e2a2ca8d2c9cd6b0b612dca2e76
DIFF: https://github.com/llvm/llvm-project/commit/b2d62c9a58433e2a2ca8d2c9cd6b0b612dca2e76.diff
LOG: [clang] Ensure fixed point conversions work in C++ (#68344)
Added:
Modified:
clang/include/clang/Sema/Overload.h
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaExprCXX.cpp
clang/lib/Sema/SemaOverload.cpp
clang/test/Frontend/fixed_point_conversions.c
Removed:
################################################################################
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index a97968dc7b20967..333309a3686a1f9 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -192,6 +192,9 @@ class Sema;
/// C-only conversion between pointers with incompatible types
ICK_Incompatible_Pointer_Conversion,
+ /// Fixed point type conversions according to N1169.
+ ICK_Fixed_Point_Conversion,
+
/// The number of conversion kinds
ICK_Num_Conversion_Kinds,
};
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index a6cd0bb9ea2a829..da8e2db36aaabb8 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -148,6 +148,9 @@ bool Sema::isSimpleTypeSpecifier(tok::TokenKind Kind) const {
case tok::kw___ibm128:
case tok::kw_wchar_t:
case tok::kw_bool:
+ case tok::kw__Accum:
+ case tok::kw__Fract:
+ case tok::kw__Sat:
#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
#include "clang/Basic/TransformTypeTraits.def"
case tok::kw___auto_type:
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 023411c7edc946b..081b568762ae228 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -4506,6 +4506,36 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
.get();
break;
+ case ICK_Fixed_Point_Conversion:
+ assert((FromType->isFixedPointType() || ToType->isFixedPointType()) &&
+ "Attempting implicit fixed point conversion without a fixed "
+ "point operand");
+ if (FromType->isFloatingType())
+ From = ImpCastExprToType(From, ToType, CK_FloatingToFixedPoint,
+ VK_PRValue,
+ /*BasePath=*/nullptr, CCK).get();
+ else if (ToType->isFloatingType())
+ From = ImpCastExprToType(From, ToType, CK_FixedPointToFloating,
+ VK_PRValue,
+ /*BasePath=*/nullptr, CCK).get();
+ else if (FromType->isIntegralType(Context))
+ From = ImpCastExprToType(From, ToType, CK_IntegralToFixedPoint,
+ VK_PRValue,
+ /*BasePath=*/nullptr, CCK).get();
+ else if (ToType->isIntegralType(Context))
+ From = ImpCastExprToType(From, ToType, CK_FixedPointToIntegral,
+ VK_PRValue,
+ /*BasePath=*/nullptr, CCK).get();
+ else if (ToType->isBooleanType())
+ From = ImpCastExprToType(From, ToType, CK_FixedPointToBoolean,
+ VK_PRValue,
+ /*BasePath=*/nullptr, CCK).get();
+ else
+ From = ImpCastExprToType(From, ToType, CK_FixedPointCast,
+ VK_PRValue,
+ /*BasePath=*/nullptr, CCK).get();
+ break;
+
case ICK_Compatible_Conversion:
From = ImpCastExprToType(From, ToType, CK_NoOp, From->getValueKind(),
/*BasePath=*/nullptr, CCK).get();
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 858654e35cbb6bd..9800d7f1c9cfee9 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -158,7 +158,8 @@ ImplicitConversionRank clang::GetConversionRank(ImplicitConversionKind Kind) {
// it was omitted by the patch that added
// ICK_Zero_Queue_Conversion
ICR_C_Conversion,
- ICR_C_Conversion_Extension
+ ICR_C_Conversion_Extension,
+ ICR_Conversion,
};
static_assert(std::size(Rank) == (int)ICK_Num_Conversion_Kinds);
return Rank[(int)Kind];
@@ -197,7 +198,8 @@ static const char* GetImplicitConversionName(ImplicitConversionKind Kind) {
"OpenCL Zero Event Conversion",
"OpenCL Zero Queue Conversion",
"C specific type conversion",
- "Incompatible pointer conversion"
+ "Incompatible pointer conversion",
+ "Fixed point conversion",
};
static_assert(std::size(Name) == (int)ICK_Num_Conversion_Kinds);
return Name[Kind];
@@ -2189,6 +2191,9 @@ 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()) {
+ SCS.Second = ICK_Fixed_Point_Conversion;
+ FromType = ToType;
} else {
// No second conversion required.
SCS.Second = ICK_Identity;
@@ -5947,6 +5952,7 @@ static bool CheckConvertedConstantConversions(Sema &S,
case ICK_Zero_Event_Conversion:
case ICK_C_Only_Conversion:
case ICK_Incompatible_Pointer_Conversion:
+ case ICK_Fixed_Point_Conversion:
return false;
case ICK_Lvalue_To_Rvalue:
diff --git a/clang/test/Frontend/fixed_point_conversions.c b/clang/test/Frontend/fixed_point_conversions.c
index ebd1d7e521df43b..efa3f1b347246e3 100644
--- a/clang/test/Frontend/fixed_point_conversions.c
+++ b/clang/test/Frontend/fixed_point_conversions.c
@@ -1,6 +1,12 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
-// RUN: %clang_cc1 -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,SIGNED
-// RUN: %clang_cc1 -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - -fpadding-on-unsigned-fixed-point | FileCheck %s --check-prefixes=CHECK,UNSIGNED
+// RUN: %clang_cc1 -x c -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,SIGNED
+// RUN: %clang_cc1 -x c -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - -fpadding-on-unsigned-fixed-point | FileCheck %s --check-prefixes=CHECK,UNSIGNED
+// RUN: %clang_cc1 -x c++ -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,SIGNED
+// RUN: %clang_cc1 -x c++ -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - -fpadding-on-unsigned-fixed-point | FileCheck %s --check-prefixes=CHECK,UNSIGNED
+
+#ifdef __cplusplus
+extern "C" {
+#endif
short _Accum sa;
_Accum a, a2;
@@ -994,3 +1000,7 @@ void float_sat5(void) {
void float_sat6(void) {
sat_uf = fl;
}
+
+#ifdef __cplusplus
+}
+#endif
More information about the cfe-commits
mailing list