[clang-tools-extra] [llvm] [clang] [clang] Ensure fixed point conversions work in C++ (PR #68344)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 15 15:48:23 PST 2023
https://github.com/PiJoules updated https://github.com/llvm/llvm-project/pull/68344
>From 1eb1638a12c619febf1fe9830f34e51a56d4c20e Mon Sep 17 00:00:00 2001
From: Leonard Chan <leonardchan at google.com>
Date: Thu, 5 Oct 2023 19:19:47 +0000
Subject: [PATCH] [clang] Ensure fixed point conversions work in C++
---
clang/include/clang/Sema/Overload.h | 3 ++
clang/lib/Sema/SemaDecl.cpp | 3 ++
clang/lib/Sema/SemaExprCXX.cpp | 30 +++++++++++++++++++
clang/lib/Sema/SemaOverload.cpp | 10 +++++--
clang/test/Frontend/fixed_point_conversions.c | 14 +++++++--
5 files changed, 56 insertions(+), 4 deletions(-)
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 6ac02df193976a9..52cbd5c7593d9ce 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 1153049496d129f..e15d8e01f934bd6 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -4478,6 +4478,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 ce78994e6553814..f5cc8a6ee6e3b9f 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -156,7 +156,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_C_Conversion_Extension,
};
static_assert(std::size(Rank) == (int)ICK_Num_Conversion_Kinds);
return Rank[(int)Kind];
@@ -195,7 +196,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];
@@ -2192,6 +2194,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;
@@ -5950,6 +5955,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