Bug 20911 - Erroneous unreachable code warning
Summary: Erroneous unreachable code warning
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 3.3.3
: P2 minor
Target Milestone: 4.0.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2005-04-08 22:27 UTC by Rici Lake
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host:
Target:
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 Rici Lake 2005-04-08 22:27:58 UTC
#include <stdlib.h>
#include <stdio.h>

void print_joined (const char *first, const char *delim, const char *last, const char *def,
                   char **list) {
  switch (*list == NULL) {
    for (; *list; ++list) {
      printf("%s", delim);
      if (0) {
  case 0:
        printf("%s", first);
      }
      printf("%s", *list);
    }
    printf("%s", last);
    break;
  default:
    printf("%s", def);
    break;
  }
}


int main (int argc, char **argv) {
  print_joined("(", ", ", ")", "nil", argv + 1);
  puts("");
  return 0;
}

---------------

OK, it's an outrageous bit of code. But it's legal and it works. However, gcc 3.3.3 reports

join.c: In function `print_joined':
join.c:8: warning: unreachable code at beginning of switch statement

with -O{1,2}

and 

join.c: In function `main':
join.c:8: warning: unreachable code at beginning of switch statement
join.c: In function `print_joined':
join.c:8: warning: unreachable code at beginning of switch statement

with -O{3,4,5}

The code is clearly reachable; running the program reaches it:

rlake@freeb:~/src$ ./join
nil
rlake@freeb:~/src$ ./join a
(a)
rlake@freeb:~/src$ ./join a b
(a, b)
rlake@freeb:~/src$ ./join a b c
(a, b, c)
Comment 1 Andrew Pinski 2005-04-08 22:32:06 UTC
Fixed in 4.0.0 and above.