]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
typeck.c (build_binary_op): Issue warning if either operand of a comparison operator...
authorRoger Sayle <roger@eyesopen.com>
Tue, 6 Dec 2005 14:05:58 +0000 (14:05 +0000)
committerRoger Sayle <sayle@gcc.gnu.org>
Tue, 6 Dec 2005 14:05:58 +0000 (14:05 +0000)
* typeck.c (build_binary_op): Issue warning if either operand of a
comparison operator is a string literal, except for testing equality
or inequality against NULL.

* g++.dg/warn/Wstring-literal-comparison-1.C: New test case.
* g++.dg/warn/Wstring-literal-comparison-2.C: Likewise.
* g++.dg/warn/Wstring-literal-comparison-3.C: Likewise.
* g++.dg/warn/Wstring-literal-comparison-4.C: Likewise.

From-SVN: r108120

gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wstring-literal-comparison-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wstring-literal-comparison-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wstring-literal-comparison-3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wstring-literal-comparison-4.C [new file with mode: 0644]

index 82b99cb3b501ed8130df690ddb474efdf5fd5c8d..d133d8b144aedc359b4759c592f60a85008ec603 100644 (file)
@@ -1,3 +1,9 @@
+2005-12-06  Roger Sayle  <roger@eyesopen.com>
+
+       * typeck.c (build_binary_op): Issue warning if either operand of a
+       comparison operator is a string literal, except for testing equality
+       or inequality against NULL.
+
 2005-12-06  Roger Sayle  <roger@eyesopen.com>
 
        PR c++/25263
index 0ee630ab0d64a3868a58ca90c64bfe0876b51585..0cae93807107e1bbf884ea410db976951e7f664b 100644 (file)
@@ -3089,6 +3089,10 @@ build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
     case NE_EXPR:
       if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
        warning (0, "comparing floating point with == or != is unsafe");
+      if ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
+         || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0)))
+       warning (OPT_Wstring_literal_comparison,
+                "comparison with string literal");
 
       build_type = boolean_type_node;
       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
@@ -3194,6 +3198,11 @@ build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
     case GE_EXPR:
     case LT_EXPR:
     case GT_EXPR:
+      if (TREE_CODE (orig_op0) == STRING_CST
+         || TREE_CODE (orig_op1) == STRING_CST)
+       warning (OPT_Wstring_literal_comparison,
+                "comparison with string literal");
+
       build_type = boolean_type_node;
       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
index 6c16c7d430bb9ee7add79c1b386f37068f5fe83e..35f45eacce1e11f0816389999bc180e1ee226aec 100644 (file)
@@ -1,3 +1,10 @@
+2005-12-06  Roger Sayle  <roger@eyesopen.com>
+
+       * g++.dg/warn/Wstring-literal-comparison-1.C: New test case.
+       * g++.dg/warn/Wstring-literal-comparison-2.C: Likewise.
+       * g++.dg/warn/Wstring-literal-comparison-3.C: Likewise.
+       * g++.dg/warn/Wstring-literal-comparison-4.C: Likewise.
+
 2005-12-06  Roger Sayle  <roger@eyesopen.com>
 
        PR c++/25263
diff --git a/gcc/testsuite/g++.dg/warn/Wstring-literal-comparison-1.C b/gcc/testsuite/g++.dg/warn/Wstring-literal-comparison-1.C
new file mode 100644 (file)
index 0000000..c5dea46
--- /dev/null
@@ -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/g++.dg/warn/Wstring-literal-comparison-2.C b/gcc/testsuite/g++.dg/warn/Wstring-literal-comparison-2.C
new file mode 100644 (file)
index 0000000..3eb91ee
--- /dev/null
@@ -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/g++.dg/warn/Wstring-literal-comparison-3.C b/gcc/testsuite/g++.dg/warn/Wstring-literal-comparison-3.C
new file mode 100644 (file)
index 0000000..f700a51
--- /dev/null
@@ -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/g++.dg/warn/Wstring-literal-comparison-4.C b/gcc/testsuite/g++.dg/warn/Wstring-literal-comparison-4.C
new file mode 100644 (file)
index 0000000..27f25f3
--- /dev/null
@@ -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";
+}
+