public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-8874] c: Fix boolean conversion of floating constant as integer constant expression [PR113776]
@ 2024-02-08 1:35 Joseph Myers
0 siblings, 0 replies; only message in thread
From: Joseph Myers @ 2024-02-08 1:35 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:bfd72bb44eca83b0db2b0bab895f27a8a44247a2
commit r14-8874-gbfd72bb44eca83b0db2b0bab895f27a8a44247a2
Author: Joseph Myers <josmyers@redhat.com>
Date: Thu Feb 8 01:34:09 2024 +0000
c: Fix boolean conversion of floating constant as integer constant expression [PR113776]
My fix for bug 111059 and bug 111911 caused a conversion of a floating
constant to boolean to wrongly no longer be considered an integer
constant expression, because logic to insert a NOP_EXPR in
c_objc_common_truthvalue_conversion for an argument not an integer
constant expression itself now took place after rather than before the
conversion to bool. In the specific case of casting a floating
constant to bool, the result is an integer constant expression even
though the argument isn't (build_c_cast deals with ensuring that casts
to integer type of anything of floating type more complicated than a
single floating constant don't get wrongly treated as integer constant
expressions even if they fold to constants), so fix the logic in
c_objc_common_truthvalue_conversion to handle that special case.
Bootstrapped with no regressions for x86_64-pc-linux-gnu.
PR c/113776
gcc/c
* c-typeck.cc (c_objc_common_truthvalue_conversion): Return an
integer constant expression for boolean conversion of floating
constant.
gcc/testsuite/
* gcc.dg/pr113776-1.c, gcc.dg/pr113776-2.c, gcc.dg/pr113776-3.c,
gcc.dg/pr113776-4.c: New tests.
Diff:
---
gcc/c/c-typeck.cc | 12 +++++++++++-
gcc/testsuite/gcc.dg/pr113776-1.c | 5 +++++
gcc/testsuite/gcc.dg/pr113776-2.c | 4 ++++
gcc/testsuite/gcc.dg/pr113776-3.c | 7 +++++++
gcc/testsuite/gcc.dg/pr113776-4.c | 6 ++++++
5 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 3b519c48ae0a..ddeab1e2a8a1 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -13572,7 +13572,17 @@ c_objc_common_truthvalue_conversion (location_t location, tree expr, tree type)
break;
}
- int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
+ /* Conversion of a floating constant to boolean goes through here
+ and yields an integer constant expression. Otherwise, the result
+ is only an integer constant expression if the argument is. */
+ int_const = ((TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr))
+ || ((TREE_CODE (expr) == REAL_CST
+ || TREE_CODE (expr) == COMPLEX_CST)
+ && (TREE_CODE (type) == BOOLEAN_TYPE
+ || (TREE_CODE (type) == ENUMERAL_TYPE
+ && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
+ && (TREE_CODE (ENUM_UNDERLYING_TYPE (type))
+ == BOOLEAN_TYPE)))));
int_operands = EXPR_INT_CONST_OPERANDS (expr);
if (int_operands && TREE_CODE (expr) != INTEGER_CST)
{
diff --git a/gcc/testsuite/gcc.dg/pr113776-1.c b/gcc/testsuite/gcc.dg/pr113776-1.c
new file mode 100644
index 000000000000..36190fbc3fec
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr113776-1.c
@@ -0,0 +1,5 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic" } */
+
+char d[(_Bool)0.5 == 1 ? 1 : -1];
+char f[(_Bool)0.0 == 0 ? 1 : -1];
diff --git a/gcc/testsuite/gcc.dg/pr113776-2.c b/gcc/testsuite/gcc.dg/pr113776-2.c
new file mode 100644
index 000000000000..9e88210892a4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr113776-2.c
@@ -0,0 +1,4 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic" } */
+
+enum e { A = (_Bool) 0.0, B = (_Bool) 0.5, C = (_Bool) 1.0 };
diff --git a/gcc/testsuite/gcc.dg/pr113776-3.c b/gcc/testsuite/gcc.dg/pr113776-3.c
new file mode 100644
index 000000000000..c615994a89f6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr113776-3.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c23 -pedantic" } */
+
+enum ebool : bool { BF, BT };
+
+char d[(enum ebool)0.5 == 1 ? 1 : -1];
+char f[(enum ebool)0.0 == 0 ? 1 : -1];
diff --git a/gcc/testsuite/gcc.dg/pr113776-4.c b/gcc/testsuite/gcc.dg/pr113776-4.c
new file mode 100644
index 000000000000..1b57557746e1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr113776-4.c
@@ -0,0 +1,6 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c23 -pedantic" } */
+
+enum ebool : bool { BF, BT };
+
+enum e { A = (enum ebool) 0.0, B = (enum ebool) 0.5, C = (enum ebool) 1.0 };
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2024-02-08 1:35 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-08 1:35 [gcc r14-8874] c: Fix boolean conversion of floating constant as integer constant expression [PR113776] Joseph Myers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).