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 | |
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
-rw-r--r-- | gcc/ChangeLog | 11 | ||||
-rw-r--r-- | gcc/c-opts.c | 1 | ||||
-rw-r--r-- | gcc/c-typeck.c | 14 | ||||
-rw-r--r-- | gcc/common.opt | 4 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 8 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/Wstring-literal-comparison-1.c | 29 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/Wstring-literal-comparison-2.c | 29 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/Wstring-literal-comparison-3.c | 29 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/Wstring-literal-comparison-4.c | 29 |
9 files changed, 154 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index afe22a0..d8fc531 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,14 @@ +2005-12-04 Roger Sayle <roger@eyesopen.com> + + 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. + 2005-12-03 Joseph S. Myers <joseph@codesourcery.com> * c-common.c (c_sizeof_or_alignof_type): Use fold_convert instead diff --git a/gcc/c-opts.c b/gcc/c-opts.c index 62db668..86f2244 100644 --- a/gcc/c-opts.c +++ b/gcc/c-opts.c @@ -386,6 +386,7 @@ c_common_handle_option (size_t scode, const char *arg, int value) warn_sign_compare = value; warn_switch = value; warn_strict_aliasing = value; + warn_string_literal_comparison = value; /* Only warn about unknown pragmas that are not in system headers. */ 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); diff --git a/gcc/common.opt b/gcc/common.opt index 6ac77fd..e49838a 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -125,6 +125,10 @@ Wstrict-aliasing= Common Joined UInteger Warn about code which might break strict aliasing rules +Wstring-literal-comparison +Common Var(warn_string_literal_comparison) +Warn about comparisons to constant string literals + Wswitch Common Var(warn_switch) Warn about enumerated switches, with no default, missing a case diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 257c74a..f08c252 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2005-12-04 Roger Sayle <roger@eyesopen.com> + + PR c/7776 + * 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. + 2005-12-03 Joseph S. Myers <joseph@codesourcery.com> * gcc.dg/cast-pretty-print-1.c: New test. diff --git a/gcc/testsuite/gcc.dg/Wstring-literal-comparison-1.c b/gcc/testsuite/gcc.dg/Wstring-literal-comparison-1.c new file mode 100644 index 0000000..c5dea46 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wstring-literal-comparison-1.c @@ -0,0 +1,29 @@ +/* PR c/7776 */ +/* { dg-do compile } */ +/* { dg-options "-Wstring-literal-comparison" } */ + +int test1(char *ptr) +{ + return ptr == "foo"; /* { dg-warning "comparison with string" } */ +} + +int test2() +{ + return "foo" != (const char*)0; +} + +int test3() +{ + return "foo" == (const char*)0; +} + +int test4() +{ + return (const char*)0 != "foo"; +} + +int test5() +{ + return (const char*)0 == "foo"; +} + diff --git a/gcc/testsuite/gcc.dg/Wstring-literal-comparison-2.c b/gcc/testsuite/gcc.dg/Wstring-literal-comparison-2.c new file mode 100644 index 0000000..3eb91ee --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wstring-literal-comparison-2.c @@ -0,0 +1,29 @@ +/* PR c/7776 */ +/* { dg-do compile } */ +/* { dg-options "-Wall" } */ + +int test1(char *ptr) +{ + return ptr == "foo"; /* { dg-warning "comparison with string" } */ +} + +int test2() +{ + return "foo" != (const char*)0; +} + +int test3() +{ + return "foo" == (const char*)0; +} + +int test4() +{ + return (const char*)0 != "foo"; +} + +int test5() +{ + return (const char*)0 == "foo"; +} + diff --git a/gcc/testsuite/gcc.dg/Wstring-literal-comparison-3.c b/gcc/testsuite/gcc.dg/Wstring-literal-comparison-3.c new file mode 100644 index 0000000..f700a51 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wstring-literal-comparison-3.c @@ -0,0 +1,29 @@ +/* PR c/7776 */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +int test1(char *ptr) +{ + return ptr == "foo"; +} + +int test2() +{ + return "foo" != (const char*)0; +} + +int test3() +{ + return "foo" == (const char*)0; +} + +int test4() +{ + return (const char*)0 != "foo"; +} + +int test5() +{ + return (const char*)0 == "foo"; +} + diff --git a/gcc/testsuite/gcc.dg/Wstring-literal-comparison-4.c b/gcc/testsuite/gcc.dg/Wstring-literal-comparison-4.c new file mode 100644 index 0000000..27f25f3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wstring-literal-comparison-4.c @@ -0,0 +1,29 @@ +/* PR c/7776 */ +/* { dg-do compile } */ +/* { dg-options "-Wall -Wno-string-literal-comparison" } */ + +int test1(char *ptr) +{ + return ptr == "foo"; +} + +int test2() +{ + return "foo" != (const char*)0; +} + +int test3() +{ + return "foo" == (const char*)0; +} + +int test4() +{ + return (const char*)0 != "foo"; +} + +int test5() +{ + return (const char*)0 == "foo"; +} + |