diff options
author | Marek Polacek <polacek@redhat.com> | 2021-09-29 11:45:24 -0400 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2021-10-04 12:33:57 -0400 |
commit | 2dda00b734888d3b53ac91160083b5c6cd5ca5c8 (patch) | |
tree | 979400d2829948a66cb67287d39474967cadb837 /gcc/c | |
parent | d362b91fa655fb02a4214e28eb33b8b60a0e1ace (diff) | |
download | gcc-2dda00b734888d3b53ac91160083b5c6cd5ca5c8.zip gcc-2dda00b734888d3b53ac91160083b5c6cd5ca5c8.tar.gz gcc-2dda00b734888d3b53ac91160083b5c6cd5ca5c8.tar.bz2 |
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.
Diffstat (limited to 'gcc/c')
-rw-r--r-- | gcc/c/c-typeck.c | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 33963d7..f9eb0e5 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -3940,7 +3940,14 @@ parser_build_binary_op (location_t location, enum tree_code code, else if (TREE_CODE_CLASS (code) == tcc_comparison && (code1 == STRING_CST || code2 == STRING_CST)) warning_at (location, OPT_Waddress, - "comparison with string literal results in unspecified behavior"); + "comparison with string literal results in unspecified " + "behavior"); + + if (warn_array_compare + && TREE_CODE_CLASS (code) == tcc_comparison + && TREE_CODE (type1) == ARRAY_TYPE + && TREE_CODE (type2) == ARRAY_TYPE) + do_warn_array_compare (location, code, arg1.value, arg2.value); if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg1.value) |