c-family: Implement -Warray-compare [PR97573]
authorMarek Polacek <polacek@redhat.com>
Wed, 29 Sep 2021 15:45:24 +0000 (11:45 -0400)
committerMarek Polacek <polacek@redhat.com>
Mon, 4 Oct 2021 16:33:57 +0000 (12:33 -0400)
commit2dda00b734888d3b53ac91160083b5c6cd5ca5c8
tree979400d2829948a66cb67287d39474967cadb837
parentd362b91fa655fb02a4214e28eb33b8b60a0e1ace
c-family: Implement -Warray-compare [PR97573]

This patch addresses one of my leftovers from GCC 11.  C++20 introduced
[depr.array.comp]: "Equality and relational comparisons between two operands
of array type are deprecated." so this patch adds -Warray-compare.  Since the
code in question is dubious (the comparison doesn't actually compare the array
elements), I've added this warning for C too, and enabled it in all C++ modes.

PR c++/97573

gcc/c-family/ChangeLog:

* c-common.h (do_warn_array_compare): Declare.
* c-warn.c (do_warn_array_compare): New.
* c.opt (Warray-compare): New option.

gcc/c/ChangeLog:

* c-typeck.c (parser_build_binary_op): Call do_warn_array_compare.

gcc/cp/ChangeLog:

* typeck.c (cp_build_binary_op): Call do_warn_array_compare.

gcc/ChangeLog:

* doc/invoke.texi: Document -Warray-compare.

gcc/testsuite/ChangeLog:

* c-c++-common/Warray-compare-1.c: New test.
* c-c++-common/Warray-compare-2.c: New test.
gcc/c-family/c-common.h
gcc/c-family/c-warn.c
gcc/c-family/c.opt
gcc/c/c-typeck.c
gcc/cp/typeck.c
gcc/doc/invoke.texi
gcc/testsuite/c-c++-common/Warray-compare-1.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/Warray-compare-2.c [new file with mode: 0644]