This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C++ PATCH] Warn about if ("abc" < "xyz")...


This is the promised C++ version of my fix for enhancement request
PR c/7776.  The documentation and command line option bits were
already applied to mainline as part of the C patch.

The following patch has been tested on i686-pc-linux-gnu with a
full "make bootstrap", and regression tested with a top-level
"make -k check" with no new failures.

Ok for mainline?


2005-12-05  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.

	* 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.


Index: typeck.c
===================================================================
*** typeck.c	(revision 108018)
--- typeck.c	(working copy)
*************** build_binary_op (enum tree_code code, tr
*** 3089,3094 ****
--- 3089,3098 ----
      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
*************** build_binary_op (enum tree_code code, tr
*** 3194,3199 ****
--- 3198,3208 ----
      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))


/* 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";
}


/* 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";
}


/* 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";
}


/* 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";
}


Roger
--
Roger Sayle,                         E-mail: roger@eyesopen.com
OpenEye Scientific Software,         WWW: http://www.eyesopen.com/
Suite 1107, 3600 Cerrillos Road,     Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507.         Fax: (+1) 505-473-0833


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]