]> git.ipfire.org Git - thirdparty/gcc.git/commit
c-family: Fix -Warray-compare warning ICE [PR115290]
authorJakub Jelinek <jakub@redhat.com>
Mon, 17 Jun 2024 17:24:05 +0000 (19:24 +0200)
committerJakub Jelinek <jakub@redhat.com>
Thu, 20 Jun 2024 08:45:56 +0000 (10:45 +0200)
commit8f612e6559b39569747894ec0f8b4694b96492a7
tree8ea9c502ffbe0cec48e17cc0c48e179bbd21baa6
parent98794d9bd4907cea20bd5b7bb2ca2ba8fdc57830
c-family: Fix -Warray-compare warning ICE [PR115290]

The warning code uses %D to print the ARRAY_REF first operands.
That works in the most common case where those operands are decls, but
as can be seen on the following testcase, they can be other expressions
with array type.
Just changing %D to %E isn't enough, because then the diagnostics can
suggest something like
note: use '&(x) != 0 ? (int (*)[32])&a : (int (*)[32])&b[0] == &(y) != 0 ? (int (*)[32])&a : (int (*)[32])&b[0]' to compare the addresses
which is a bad suggestion, the %E printing doesn't know that the
warning code will want to add & before it and [0] after it.
So, the following patch adds ()s around the operand as well, but does
that only for non-decls, for decls keeps it as &arr[0] like before.

2024-06-17  Jakub Jelinek  <jakub@redhat.com>

PR c/115290
* c-warn.cc (do_warn_array_compare): Use %E rather than %D for
printing op0 and op1; if those operands aren't decls, also print
parens around them.

* c-c++-common/Warray-compare-3.c: New test.

(cherry picked from commit b63c7d92012f92e0517190cf263d29bbef8a06bf)
gcc/c-family/c-warn.cc
gcc/testsuite/c-c++-common/Warray-compare-3.c [new file with mode: 0644]