1 Date: Tue, 14 Jun 2005 00:20:06 -0600 (MDT)
2 From: Roger Sayle <roger at eyesopen dot com>
3 Subject: [PATCH] PR 7776: Warn about if ("abc" < "xyz") ... (take 2)
5 The following patch is a revised version of my proposed solution to
6 PR middle-end/7776, which addresses Daniel Jacobowitz's concerns about
7 the potential utility of testing string literals for equality/inequality
10 See http://gcc.gnu.org/ml/gcc-patches/2005-06/msg00369.html (and the
11 following thread) for details and motivation.
13 The following patch has been tested on i686-pc-linux-gnu with a full
14 "make bootstrap", all default languages, and regression tested with a
15 top-level "make -k check" with no new failures.
19 2005-06-13 Roger Sayle <roger@eyesopen.com>
22 * common.opt (Wstring-literal-comparison): New command line option.
23 * c-opts.c (c_common_handle_option): Set it with -Wall.
24 * c-typeck.c (parser_build_binary_op): Issue warning if either
25 operand of a comparison operator is a string literal, except for
26 testing equality or inequality against NULL.
28 * gcc.dg/Wstring-literal-comparison-1.c: New test case.
29 * gcc.dg/Wstring-literal-comparison-2.c: Likewise.
30 * gcc.dg/Wstring-literal-comparison-3.c: Likewise.
31 * gcc.dg/Wstring-literal-comparison-4.c: Likewise.
34 *** a/gcc/common.opt 4 Jun 2005 17:07:55 -0000 1.73
35 --- b/gcc/common.opt 13 Jun 2005 16:59:28 -0000
36 *************** Wstrict-aliasing=
39 Common Joined UInteger
40 Warn about code which might break strict aliasing rules
42 + Wstring-literal-comparison
43 + Common Var(warn_string_literal_comparison)
44 + Warn about comparisons to constant string literals
47 Common Var(warn_switch)
48 Warn about enumerated switches, with no default, missing a case
49 *** a/gcc/c-opts.c 25 May 2005 03:58:55 -0000 1.146
50 --- b/gcc/c-opts.c 13 Jun 2005 16:59:29 -0000
51 *************** c_common_handle_option (size_t scode, co
54 warn_sign_compare = value;
56 warn_strict_aliasing = value;
57 + warn_string_literal_comparison = value;
59 /* Only warn about unknown pragmas that are not in system
61 *** a/gcc/c-typeck.c 11 Jun 2005 19:47:01 -0000 1.453
62 --- b/gcc/c-typeck.c 13 Jun 2005 16:59:31 -0000
63 *************** parser_build_binary_op (enum tree_code c
69 + /* Warn about comparisons against string literals, with the exception
70 + of testing for equality or inequality of a string literal with NULL. */
71 + if (code == EQ_EXPR || code == NE_EXPR)
73 + if ((TREE_CODE (arg1.value) == STRING_CST
74 + && !integer_zerop (arg2.value))
75 + || (TREE_CODE (arg2.value) == STRING_CST
76 + && !integer_zerop (arg1.value)))
77 + warning (OPT_Wstring_literal_comparison,
78 + "comparison with string literal");
80 + else if (TREE_CODE_CLASS (code) == tcc_comparison
81 + && (TREE_CODE (arg1.value) == STRING_CST
82 + || TREE_CODE (arg2.value) == STRING_CST))
83 + warning (OPT_Wstring_literal_comparison,
84 + "comparison with string literal");
86 unsigned_conversion_warning (result.value, arg1.value);
87 unsigned_conversion_warning (result.value, arg2.value);
88 overflow_warning (result.value);