aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2024-06-17 19:24:05 +0200
committerJakub Jelinek <jakub@redhat.com>2024-06-17 19:25:43 +0200
commit922648759b034c356e7d5c1ae530bdb6f3d00c62 (patch)
tree21869b7e70f8f71841d1234c44496b45460f8cb3
parent5be6d9d2a9854c05f3c019deb9fe95eca7248140 (diff)
downloadgcc-922648759b034c356e7d5c1ae530bdb6f3d00c62.zip
gcc-922648759b034c356e7d5c1ae530bdb6f3d00c62.tar.gz
gcc-922648759b034c356e7d5c1ae530bdb6f3d00c62.tar.bz2
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)
-rw-r--r--gcc/c-family/c-warn.cc13
-rw-r--r--gcc/testsuite/c-c++-common/Warray-compare-3.c13
2 files changed, 22 insertions, 4 deletions
diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc
index bff87be..8fc0d8a 100644
--- a/gcc/c-family/c-warn.cc
+++ b/gcc/c-family/c-warn.cc
@@ -3830,11 +3830,16 @@ do_warn_array_compare (location_t location, tree_code code, tree op0, tree op1)
/* C doesn't allow +arr. */
if (c_dialect_cxx ())
inform (location, "use unary %<+%> which decays operands to pointers "
- "or %<&%D[0] %s &%D[0]%> to compare the addresses",
- op0, op_symbol_code (code), op1);
+ "or %<&%s%E%s[0] %s &%s%E%s[0]%> to compare the addresses",
+ DECL_P (op0) ? "" : "(", op0, DECL_P (op0) ? "" : ")",
+ op_symbol_code (code),
+ DECL_P (op1) ? "" : "(", op1, DECL_P (op1) ? "" : ")");
else
- inform (location, "use %<&%D[0] %s &%D[0]%> to compare the addresses",
- op0, op_symbol_code (code), op1);
+ inform (location,
+ "use %<&%s%E%s[0] %s &%s%E%s[0]%> to compare the addresses",
+ DECL_P (op0) ? "" : "(", op0, DECL_P (op0) ? "" : ")",
+ op_symbol_code (code),
+ DECL_P (op1) ? "" : "(", op1, DECL_P (op1) ? "" : ")");
}
}
diff --git a/gcc/testsuite/c-c++-common/Warray-compare-3.c b/gcc/testsuite/c-c++-common/Warray-compare-3.c
new file mode 100644
index 0000000..4725aa2
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Warray-compare-3.c
@@ -0,0 +1,13 @@
+/* PR c/115290 */
+/* { dg-do compile } */
+/* { dg-options "-Warray-compare" } */
+
+int a[32][32], b[32][32];
+
+int
+foo (int x, int y)
+{
+ return (x ? a : b) == (y ? a : b); /* { dg-warning "comparison between two arrays" } */
+/* { dg-message "use '&\\\(\[^\n\r]*\\\)\\\[0\\\] == &\\\(\[^\n\r]*\\\)\\\[0\\\]' to compare the addresses" "" { target c } .-1 } */
+/* { dg-message "use unary '\\\+' which decays operands to pointers or '&\\\(\[^\n\r]*\\\)\\\[0\\\] == &\\\(\[^\n\r]*\\\)\\\[0\\\]' to compare the addresses" "" { target c++ } .-2 } */
+}