Bug 230189

Summary: gcc shouldn't warn when testing two distinct string literals for equality
Product: [openSUSE] openSUSE 10.3 Reporter: Michal Marek <mmarek>
Component: DevelopmentAssignee: Richard Biener <rguenther>
Status: RESOLVED INVALID QA Contact: E-mail List <qa-bugs>
Severity: Minor    
Priority: P5 - None    
Version: unspecified   
Target Milestone: ---   
Hardware: Other   
OS: Other   
Whiteboard:
Found By: Other Services Priority:
Business Priority: Blocker: ---
Marketing QA Status: --- IT Deployment: ---
Attachments: _untested_ patch for C

Description Michal Marek 2006-12-21 10:56:16 UTC
$ cat cmp.c 
int f1(void)
{
        return "string1" == "string2";
}

int f2(void)
{
        return "string3" != "string4";
}
$ gcc -c -Wall cmp.c 
cmp.c: In function ‘f1’:
cmp.c:3: warning: comparison with string literal
cmp.c: In function ‘f2’:
cmp.c:8: warning: comparison with string literal
$

Not that code like this would fall into the category "good programming practice", but the expressions will work nevertheless.
Comment 1 Michal Marek 2006-12-21 12:18:08 UTC
Created attachment 110675 [details]
_untested_ patch for C
Comment 2 Michal Marek 2006-12-28 12:45:58 UTC
(In reply to comment #1)
> Created an attachment (id=110675) [edit]
> _untested_ patch for C

Doesn't work :-/
Comment 3 Richard Biener 2007-01-14 17:34:13 UTC
It is not guaranteed that "string1" == "string1" (they are not required to be merged).  So your program is not portable.
Comment 4 Michal Marek 2007-01-14 19:14:16 UTC
In the example above, there are four _different_ strings, which will live at four different addresses.

Of course if fixing this is too difficult, then feel free to close this again. Code like this isn't very common...
Comment 5 Richard Biener 2007-01-15 13:10:31 UTC
The point I wanted to make is that comparison of string literals is pointless, so
the warning is ok.  I guess you want to avoid the warning if we can prove the
result will be the same regardless of whether string literals are merged?  If so,
please file this upstream as an enhancement request or with fate.

Thanks,
Richard.