diff options
author | sayle <sayle@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-12-04 19:56:47 +0000 |
---|---|---|
committer | sayle <sayle@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-12-04 19:56:47 +0000 |
commit | 19f0596c4a5e1a38ec4e960f475f89dab547665d (patch) | |
tree | 0c868f77e7b841472e04bdb16256459255a05c04 /gcc/c-typeck.c | |
parent | 4a72b87684934abf9883bb0a90f0f3cb7329b659 (diff) | |
download | gcc-19f0596c4a5e1a38ec4e960f475f89dab547665d.tar.gz |
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.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@108018 138bc75d-0d04-0410-961f-82ee72b054a4
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 8b8eb56ea44..062de7cded0 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); |