Bug 35973 - Incorrect warning: will never be executed
Summary: Incorrect warning: will never be executed
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-04-18 16:05 UTC by Tony Finch
Modified: 2008-12-28 06:13 UTC (History)
2 users (show)

See Also:
Host: i386-undermydesk-freebsd
Target: i386-undermydesk-freebsd
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tony Finch 2008-04-18 16:05:30 UTC
This bug exists in various forms in multiple versions of gcc:

$ cat gccbug.c

struct sel {
        const char  *name;
        unsigned int enabled;
};

int selog_on(struct sel *);

#define selog_on(sel) \
        (sel.enabled == 0 \
                ? selog_on(&sel) \
                : sel.enabled != 42)

void dostuff(int *);

void gccbug1(void) {
        struct sel sel = { "test", 0 };
        if(selog_on(sel)) {
                dostuff(0);
        }
}

void gccbug2(void) {
        struct sel sel = { "test", 0 };
        if(selog_on(sel)) {
                int i;
                dostuff(&i);
        }
}


$ gcc -O -Wunreachable-code -c gccbug.c
gccbug.c: In function `gccbug2':
gccbug.c:25: warning: will never be executed
$ gcc --version
gcc --version
gcc (GCC) 3.4.4 [FreeBSD] 20050518

$ gcc -O -Wunreachable-code -c gccbug.c
gccbug.c: In function ‘gccbug2’:
gccbug.c:24: warning: will never be executed
gccbug.c: In function ‘gccbug1’:
gccbug.c:17: warning: will never be executed
$ gcc -O -Werror -Wunreachable-code -c gccbug.c
cc1: warnings being treated as errors
gccbug.c: In function ‘gccbug2’:
gccbug.c:24: warning: will never be executed
$ gcc --version
gcc (GCC) 4.1.2 20070115 (prerelease) (SUSE Linux)

$ gcc -O -Wunreachable-code -c gccbug.c
gccbug.c: In function 'gccbug2':
gccbug.c:24: warning: will never be executed
gccbug.c: In function 'gccbug1':
gccbug.c:17: warning: will never be executed
$ gcc -O -Werror -Wunreachable-code -c gccbug.c
cc1: warnings being treated as errors
gccbug.c: In function 'gccbug2':
gccbug.c:24: warning: will never be executed
$ gcc --version
gcc (GCC) 4.2.1 20070719  [FreeBSD]
Comment 1 Andrew Pinski 2008-05-05 05:38:57 UTC
The warning looks fine for me, it is saying selog_on(&sel) will never be executed.
Comment 2 Andrew Pinski 2008-05-05 05:40:55 UTC
Sorry I mean:
  [t.c : 24] D.1191_5 = sel.enabled;
  [t.c : 24] iftmp.1_6 = D.1191_5 != 42;

As far as I can tell this warning is correct.
Comment 3 Tony Finch 2008-05-05 14:52:17 UTC
The code is executed when the function selog_on() returns true. The warning is also inconsistent, which also points to an analysis bug.
Comment 4 Andrew Pinski 2008-12-28 06:13:03 UTC
This warning depends on optimizations so what you do expect with the inconsistency.  So in one case, we decide that i can alias sel which is obviously not true but what do you expect from aliasing.