Bug 17843 - Warning not given for unreachable code in a switch
Summary: Warning not given for unreachable code in a switch
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 3.4.0
: P2 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, patch
Depends on:
Blocks: 4.4pending 37515 4.7pending
  Show dependency treegraph
 
Reported: 2004-10-05 14:49 UTC by Dhruv Matani
Modified: 2014-10-11 11:20 UTC (History)
3 users (show)

See Also:
Host: i386-pc-linux-gnu
Target: i386-pc-linux-gnu
Build: i386-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2008-02-02 13:44:24


Attachments
Emit warning for surplus case labels in a switch stmt with a boolean condition (793 bytes, patch)
2008-02-02 13:43 UTC, Bernhard Reutner-Fischer
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dhruv Matani 2004-10-05 14:49:57 UTC
#include <stdio.h>

int main()
{
  int x = 0;
  scanf("%d", &x);

  bool b = x;

  switch (b)
    {
    case true:
      x = 23;
      break;

    case false:
      x = 45;
      break;

    default:
      x = 69;
    }
  return x;
}



The following code should give a warning to the effect of unreachable code in
the default: block, which it doesn't.

-Dhruv.
Comment 1 Andrew Pinski 2004-10-05 14:52:34 UTC
Use -Wunreachable-code.
Comment 2 Dhruv Matani 2004-10-05 14:58:29 UTC
(In reply to comment #1)
> Use -Wunreachable-code.

3.4 still doesn't show the warning. I had tested with -Wall.

Does mainline show the warning now?
Comment 3 Wolfgang Bangerth 2004-10-05 17:31:18 UTC
Andrew, would you mind checking a little more carefully? 
 
With mainline, I get this warning message: 
g/x> /home/bangerth/bin/gcc-4*-pre/bin/c++ -c x.cc -Wunreachable-code 
x.cc: In function `int main()': 
x.cc:24: warning: will never be executed 
 
This is indeed a progression, though line 24 is the closing brace of 
main and it is therefore not at all clear what will never be executed. 
So there are two bugs here: first that the warning message is on the 
wrong line, second that the message doesn't state what exactly is never 
executed (the message should probably read like "statement will never 
be executed" or similar). 
 
W. 
Comment 4 Wolfgang Bangerth 2004-10-05 17:31:38 UTC
. 
Comment 5 Andrew Pinski 2004-10-05 18:04:36 UTC
This is because switches are not optimizated as much as ifs or anything else is.  The warning which 
Wolfgang gives is a different bug which is already filed.

Note the tree which are generated is "(int) (x != 0)" so we are switching on that but the middle-end 
does not produce good RTL for that to optimize right.
Comment 6 Bernhard Reutner-Fischer 2008-02-02 13:43:30 UTC
Created attachment 15077 [details]
Emit warning for surplus case labels in a switch stmt with a boolean condition

The attached patchlet would warn about a superfluous default_label in a
switch_statement that uses a boolean as condition.
It does not burden VRP to emit this warning but checks in c-common.c instead.

gcc/ChangeLog
2008-02-02  Bernhard Fischer  <aldot>

        * c-common.c (record_surplus_node): New function.
        (c_do_switch_warnings): Warn for surplus default label in switch
        stmt.
Comment 7 Bernhard Reutner-Fischer 2008-02-02 13:44:24 UTC
Reconfirm for 4.3.0 / 4.4.x
Comment 8 Manuel López-Ibáñez 2008-08-13 10:49:23 UTC
@Bernhard

You need to properly submit the patch to gcc-patches (plus copyright assignment in place, testcases, changelog)?

See http://gcc.gnu.org/contribute.html

As for the patch itself. Notice that c_do_switch_warnings returns before your code

 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
    return;

If you make your warning conditional on Wunreachable-code, you need to add warn_unreachable_code to that test. Alternatively, make it conditional on -Wswitch and it will work. Then, you should use warning (OPT_Wswitch, ""). Also, you should pass to warning a explicit location (ideally, the one corresponding ot the default label or if that is not possible, then the one of the switch):

warning (OPT_Wswitch, "%Hwarning", &location);


Bonus points if the code for this and enums could be merged somehow!
Comment 9 Manuel López-Ibáñez 2014-10-11 11:20:52 UTC
This is fixed by -Wswitch-bool in GCC 5.0