middle-end/3773: Comparision with string literal

ank@nixu.fi ank@nixu.fi
Mon Jul 23 03:57:00 GMT 2001


>Number:         3773
>Category:       middle-end
>Synopsis:       Comparision with string literal
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Mon Jul 23 03:56:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     ank@nixu.fi (Alexander Krotov)
>Release:        3.0
>Organization:
>Environment:
All
>Description:
GCC 3.0 does not warn about typical beginers misstake:
int f (char *p)
{
	if (p=="foo")
		return 1;
	return 2;
}
And does not actually recognize that condition is always false.
>How-To-Repeat:
Compile code in descrption.
>Fix:
Might be fixed in fold-const.c:
6153a6154,6164
>       if (TREE_CODE(a0)==ADDR_EXPR && TREE_CODE(TREE_OPERAND(a0,0))==STRING_CST 
>        || TREE_CODE(a1)==ADDR_EXPR && TREE_CODE(TREE_OPERAND(a1,0))==STRING_CST)
>       {
>         warning ("comparison is always %d due to comparision with string "
>                  "literal",
>                  code != EQ_EXPR);
>         return convert (type,
>                         (code != EQ_EXPR
>                          ? integer_one_node : integer_zero_node));
>       }
> 
Although this is not the best way to go, it would be better
to detect the case after constant propagation (in cse.c)
to recognize cases like
int f (char *p)
{
	char *x = "foo";
	if (p==x)
		return 1;
	return 2;
}
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list