diff options
author | Joseph Myers <josmyers@redhat.com> | 2024-01-31 01:24:21 +0000 |
---|---|---|
committer | Joseph Myers <josmyers@redhat.com> | 2024-01-31 01:24:21 +0000 |
commit | 35de88e2ed0aa78f6e3306c8560cd6bb15ce0ffe (patch) | |
tree | 35cf4dbd2ab713b9fa4b694ec6f77aad921a70eb /gcc/testsuite/gcc.c-torture | |
parent | 18aabe7d203aa1276e6cbacfb3ffc8d8fcb14966 (diff) | |
download | gcc-35de88e2ed0aa78f6e3306c8560cd6bb15ce0ffe.zip gcc-35de88e2ed0aa78f6e3306c8560cd6bb15ce0ffe.tar.gz gcc-35de88e2ed0aa78f6e3306c8560cd6bb15ce0ffe.tar.bz2 |
c: Fix ICEs casting expressions with integer constant operands to bool [PR111059, PR111911]
C front-end bugs 111059 and 111911 both report ICEs with conversions
to boolean of expressions with integer constant operands that can
appear in an integer constant expression as long as they are not
evaluated (such as division by zero).
The issue is a nested C_MAYBE_CONST_EXPR, with the inner one generated
in build_binary_op to indicate that a subexpression has been fully
folded and should not be folded again, and the outer one in
build_c_cast to indicate that the expression has integer constant
operands. To avoid the inner one from build_binary_op,
c_objc_common_truthvalue_conversion should be given an argument
properly marked as having integer constant operands rather than that
information having been removed by the caller - but because c_convert
would then also wrap a C_MAYBE_CONST_EXPR with a NOP_EXPR converting
to boolean, it seems most convenient to have
c_objc_common_truthvalue_conversion produce the NE_EXPR directly in
the desired type (boolean in this case), before generating any
C_MAYBE_CONST_EXPR there, rather than it always producing a comparison
in integer_type_node and doing a conversion to boolean in the caller.
The same issue as in those PRs also applies for conversion to enums
with a boolean fixed underlying type; that case is also fixed and
tests added for it. Note that not all the tests added failed before
the patch (in particular, the issue was specific to casts and did not
apply for implicit conversions, but some tests of those are added as
well).
Bootstrapped with no regressions for x86_64-pc-linux-gnu.
PR c/111059
PR c/111911
gcc/c/
* c-tree.h (c_objc_common_truthvalue_conversion): Add third
argument.
* c-convert.cc (c_convert): For conversions to boolean, pass third
argument to c_objc_common_truthvalue_conversion rather than
converting here.
* c-typeck.cc (build_c_cast): Ensure arguments with integer
operands are marked as such for conversion to boolean.
(c_objc_common_truthvalue_conversion): Add third argument TYPE.
gcc/testsuite/
* gcc.c-torture/compile/pr111059-1.c,
gcc.c-torture/compile/pr111059-2.c,
gcc.c-torture/compile/pr111059-3.c,
gcc.c-torture/compile/pr111059-4.c,
gcc.c-torture/compile/pr111059-5.c,
gcc.c-torture/compile/pr111059-6.c,
gcc.c-torture/compile/pr111059-7.c,
gcc.c-torture/compile/pr111059-8.c,
gcc.c-torture/compile/pr111059-9.c,
gcc.c-torture/compile/pr111059-10.c,
gcc.c-torture/compile/pr111059-11.c,
gcc.c-torture/compile/pr111059-12.c,
gcc.c-torture/compile/pr111911-1.c,
gcc.c-torture/compile/pr111911-2.c: New tests.
Diffstat (limited to 'gcc/testsuite/gcc.c-torture')
14 files changed, 102 insertions, 0 deletions
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-1.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-1.c new file mode 100644 index 0000000..5788cb4 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-1.c @@ -0,0 +1,5 @@ +void +f () +{ + (_Bool) (1 << -1); +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-10.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-10.c new file mode 100644 index 0000000..fdd9a29 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-10.c @@ -0,0 +1,9 @@ +/* { dg-options "-std=gnu23 -Wno-div-by-zero" } */ + +enum e : bool { X }; + +enum e +f () +{ + return 0 / 0; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-11.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-11.c new file mode 100644 index 0000000..8126034 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-11.c @@ -0,0 +1,9 @@ +/* { dg-options "-std=gnu23 -Wno-overflow" } */ + +enum e : bool { X }; + +void +f () +{ + (enum e) (__INT_MAX__ + 1); +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-12.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-12.c new file mode 100644 index 0000000..d11fcf6 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-12.c @@ -0,0 +1,9 @@ +/* { dg-options "-std=gnu23 -Wno-overflow" } */ + +enum e : bool { X }; + +enum e +f () +{ + return __INT_MAX__ + 1; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-2.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-2.c new file mode 100644 index 0000000..785e291 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-2.c @@ -0,0 +1,5 @@ +void +f () +{ + (_Bool) (0 / 0); +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-3.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-3.c new file mode 100644 index 0000000..21099bc --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-3.c @@ -0,0 +1,5 @@ +_Bool +f () +{ + return 1 << -1; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-4.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-4.c new file mode 100644 index 0000000..95c68e5 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-4.c @@ -0,0 +1,5 @@ +_Bool +f () +{ + return 0 / 0; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-5.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-5.c new file mode 100644 index 0000000..1047355 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-5.c @@ -0,0 +1,5 @@ +void +f () +{ + (_Bool) (__INT_MAX__ + 1); +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-6.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-6.c new file mode 100644 index 0000000..213f79b --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-6.c @@ -0,0 +1,5 @@ +_Bool +f () +{ + return __INT_MAX__ + 1; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-7.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-7.c new file mode 100644 index 0000000..466f45f --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-7.c @@ -0,0 +1,9 @@ +/* { dg-options "-std=gnu23 -Wno-shift-count-negative" } */ + +enum e : bool { X }; + +void +f () +{ + (enum e) (1 << -1); +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-8.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-8.c new file mode 100644 index 0000000..67f3395 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-8.c @@ -0,0 +1,9 @@ +/* { dg-options "-std=gnu23 -Wno-div-by-zero" } */ + +enum e : bool { X }; + +void +f () +{ + (enum e) (0 / 0); +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-9.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-9.c new file mode 100644 index 0000000..f88096d --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-9.c @@ -0,0 +1,9 @@ +/* { dg-options "-std=gnu23 -Wno-shift-count-negative" } */ + +enum e : bool { X }; + +enum e +f () +{ + return 1 << -1; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111911-1.c b/gcc/testsuite/gcc.c-torture/compile/pr111911-1.c new file mode 100644 index 0000000..350f4cc --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111911-1.c @@ -0,0 +1,7 @@ +int +main (void) +{ + if (!(_Bool)(__INT_MAX__ + 1) / !(_Bool)(__INT_MAX__ + 1)) + ; + return 1; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111911-2.c b/gcc/testsuite/gcc.c-torture/compile/pr111911-2.c new file mode 100644 index 0000000..28b69c4 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111911-2.c @@ -0,0 +1,11 @@ +/* { dg-options "-std=gnu23 -Wno-overflow -Wno-div-by-zero" } */ + +enum e : bool { X }; + +int +main (void) +{ + if (!(enum e)(__INT_MAX__ + 1) / !(enum e)(__INT_MAX__ + 1)) + ; + return 1; +} |