diff options
author | Roger Sayle <roger@eyesopen.com> | 2005-12-04 19:56:47 +0000 |
---|---|---|
committer | Roger Sayle <sayle@gcc.gnu.org> | 2005-12-04 19:56:47 +0000 |
commit | e994a705a9d8447c17486c13d741c2692c475f67 (patch) | |
tree | 0c868f77e7b841472e04bdb16256459255a05c04 /gcc/c-typeck.c | |
parent | 93af36c5c18bae3a294d98e46300ea3a867ecf56 (diff) | |
download | gcc-e994a705a9d8447c17486c13d741c2692c475f67.zip gcc-e994a705a9d8447c17486c13d741c2692c475f67.tar.gz gcc-e994a705a9d8447c17486c13d741c2692c475f67.tar.bz2 |
re PR c/7776 (const char* p = "foo"; if (p == "foo") ... is compiled without warning!)
PR c/7776
* common.opt (Wstring-literal-comparison): New command line option.
* c-opts.c (c_common_handle_option): Set it with -Wall.
* c-typeck.c (parser_build_binary_op): Issue warning if either
operand of a comparison operator is a string literal, except for
testing equality or inequality against NULL.
* doc/invoke.texi: Document new -Wstring-literal-comparison option.
* gcc.dg/Wstring-literal-comparison-1.c: New test case.
* gcc.dg/Wstring-literal-comparison-2.c: Likewise.
* gcc.dg/Wstring-literal-comparison-3.c: Likewise.
* gcc.dg/Wstring-literal-comparison-4.c: Likewise.
From-SVN: r108018
Diffstat (limited to 'gcc/c-typeck.c')
-rw-r--r-- | gcc/c-typeck.c | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 8b8eb56..062de7c 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -2552,6 +2552,20 @@ parser_build_binary_op (enum tree_code code, struct c_expr arg1, } + /* Warn about comparisons against string literals, with the exception + of testing for equality or inequality of a string literal with NULL. */ + if (code == EQ_EXPR || code == NE_EXPR) + { + if ((code1 == STRING_CST && !integer_zerop (arg2.value)) + || (code2 == STRING_CST && !integer_zerop (arg1.value))) + warning (OPT_Wstring_literal_comparison, + "comparison with string literal"); + } + else if (TREE_CODE_CLASS (code) == tcc_comparison + && (code1 == STRING_CST || code2 == STRING_CST)) + warning (OPT_Wstring_literal_comparison, + "comparison with string literal"); + unsigned_conversion_warning (result.value, arg1.value); unsigned_conversion_warning (result.value, arg2.value); overflow_warning (result.value); |