public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/40412]  New: -Wunreachable-code warns for strcmp with constant argument
@ 2009-06-11  8:08 s dot contini at oltrelinux dot com
  2009-06-11  8:09 ` [Bug c/40412] " s dot contini at oltrelinux dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: s dot contini at oltrelinux dot com @ 2009-06-11  8:08 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3701 bytes --]

Compiling the following code with gcc 4.4.0 20090506 on x86 with 'gcc
-fdiagnostics-show-option  -fshow-column  -O2 -Wunreachable-code  strcmp.c -o
strcmp':

-------8<-----------8<-------
#include <string.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
        if (argc != 3)
                return 1;

        printf("%d\n", strcmp(argv[1], "foo"));
        printf("%d\n", strcmp(argv[1], argv[2]));

        return 0;
}
-------8<-----------8<-------

produces the warning:

-------8<-----------8<-------
strcmp.c: In function ‘main’:
strcmp.c:9:17: warning: will never be executed [-Wunreachable-code]
-------8<-----------8<-------

using --save-temps you can see that the warning is about some generated code:

-------8<-----------8<-------
strcmp.c: In function ‘main’:
strcmp.c:9:2193: warning: will never be executed [-Wunreachable-code]
-------8<-----------8<-------

here the line of the .i file:

-------8<-----------8<-------
 printf("%d\n", __extension__ ({ size_t __s1_len, __s2_len;
(__builtin_constant_p (argv[1]) && __builtin_constant_p ("foo") && (__s1_len =
strlen (argv[1]), __s2_len = strlen ("foo"), (!((size_t)(const void
*)((argv[1]) + 1) - (size_t)(const void *)(argv[1]) == 1) || __s1_len >= 4) &&
(!((size_t)(const void *)(("foo") + 1) - (size_t)(const void *)("foo") == 1) ||
__s2_len >= 4)) ? __builtin_strcmp (argv[1], "foo") : (__builtin_constant_p
(argv[1]) && ((size_t)(const void *)((argv[1]) + 1) - (size_t)(const void
*)(argv[1]) == 1) && (__s1_len = strlen (argv[1]), __s1_len < 4) ?
(__builtin_constant_p ("foo") && ((size_t)(const void *)(("foo") + 1) -
(size_t)(const void *)("foo") == 1) ? __builtin_strcmp (argv[1], "foo") :
(__extension__ ({ __const unsigned char *__s2 = (__const unsigned char *)
(__const char *) ("foo"); register int __result = (((__const unsigned char *)
(__const char *) (argv[1]))[0] - __s2[0]); if (__s1_len > 0 && __result == 0) {
__result = (((__const unsigned char *) (__const char *) (argv[1]))[1] -
__s2[1]); if (__s1_len > 1 && __result == 0) { __result = (((__const unsigned
char *) (__const char *) (argv[1]))[2] - __s2[2]); if (__s1_len > 2 && __result
== 0) __result = (((__const unsigned char *) (__const char *) (argv[1]))[3] -
__s2[3]); } } __result; }))) : (__builtin_constant_p ("foo") && ((size_t)(const
void *)(("foo") + 1) - (size_t)(const void *)("foo") == 1) && (__s2_len =
strlen ("foo"), __s2_len < 4) ? (__builtin_constant_p (argv[1]) &&
((size_t)(const void *)((argv[1]) + 1) - (size_t)(const void *)(argv[1]) == 1)
? __builtin_strcmp (argv[1], "foo") : (__extension__ ({ __const unsigned char
*__s1 = (__const unsigned char *) (__const char *) (argv[1]); register int
__result = __s1[0] - ((__const unsigned char *) (__const char *) ("foo"))[0];
if (__s2_len > 0 && __result == 0) { __result = (__s1[1] - ((__const unsigned
char *) (__const char *) ("foo"))[1]); if (__s2_len > 1 && __result == 0) {
__result = (__s1[2] - ((__const unsigned char *) (__const char *) ("foo"))[2]);
if (__s2_len > 2 && __result == 0) __result = (__s1[3] - ((__const unsigned
char *) (__const char *) ("foo"))[3]); } } __result; }))) : __builtin_strcmp
(argv[1], "foo")))); }));
-------8<-----------8<-------

there aren't any problem with the second call to strcmp without constant
parameters.


-- 
           Summary: -Wunreachable-code warns for strcmp with constant
                    argument
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: s dot contini at oltrelinux dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40412


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c/40412] -Wunreachable-code warns for strcmp with constant argument
  2009-06-11  8:08 [Bug c/40412] New: -Wunreachable-code warns for strcmp with constant argument s dot contini at oltrelinux dot com
@ 2009-06-11  8:09 ` s dot contini at oltrelinux dot com
  2009-06-11  8:10 ` s dot contini at oltrelinux dot com
  2009-06-11  9:17 ` [Bug middle-end/40412] " pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: s dot contini at oltrelinux dot com @ 2009-06-11  8:09 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from s dot contini at oltrelinux dot com  2009-06-11 08:09 -------
Created an attachment (id=17981)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=17981&action=view)
test case


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40412


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c/40412] -Wunreachable-code warns for strcmp with constant argument
  2009-06-11  8:08 [Bug c/40412] New: -Wunreachable-code warns for strcmp with constant argument s dot contini at oltrelinux dot com
  2009-06-11  8:09 ` [Bug c/40412] " s dot contini at oltrelinux dot com
@ 2009-06-11  8:10 ` s dot contini at oltrelinux dot com
  2009-06-11  9:17 ` [Bug middle-end/40412] " pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: s dot contini at oltrelinux dot com @ 2009-06-11  8:10 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from s dot contini at oltrelinux dot com  2009-06-11 08:10 -------
Created an attachment (id=17982)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=17982&action=view)
preprocessed test case source


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40412


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug middle-end/40412] -Wunreachable-code warns for strcmp with constant argument
  2009-06-11  8:08 [Bug c/40412] New: -Wunreachable-code warns for strcmp with constant argument s dot contini at oltrelinux dot com
  2009-06-11  8:09 ` [Bug c/40412] " s dot contini at oltrelinux dot com
  2009-06-11  8:10 ` s dot contini at oltrelinux dot com
@ 2009-06-11  9:17 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2009-06-11  9:17 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2009-06-11 09:16 -------
This is why -Wunreachable-code is not useful really.  There is nothing that GCC
can do really in this case.  The reason why the non constant form does not warn
is because the __builtin_constant_p is folded into 0 early so there is no dead
code.  With the __builtin_constant_p folding to 1, the __s1_len/__s2_len is not
constant propagated until later which causes GCC to remove an unreachable basic
block.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
          Component|c                           |middle-end
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40412


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-06-11  9:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-06-11  8:08 [Bug c/40412] New: -Wunreachable-code warns for strcmp with constant argument s dot contini at oltrelinux dot com
2009-06-11  8:09 ` [Bug c/40412] " s dot contini at oltrelinux dot com
2009-06-11  8:10 ` s dot contini at oltrelinux dot com
2009-06-11  9:17 ` [Bug middle-end/40412] " pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).