public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/87983] Feature: Add a warning when case labels from a different enum than the one in switch(EXPR) are used
       [not found] <bug-87983-4@http.gcc.gnu.org/bugzilla/>
@ 2022-01-03 23:18 ` pinskia at gcc dot gnu.org
  2022-01-03 23:19 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-03 23:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87983

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

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

* [Bug c/87983] Feature: Add a warning when case labels from a different enum than the one in switch(EXPR) are used
       [not found] <bug-87983-4@http.gcc.gnu.org/bugzilla/>
  2022-01-03 23:18 ` [Bug c/87983] Feature: Add a warning when case labels from a different enum than the one in switch(EXPR) are used pinskia at gcc dot gnu.org
@ 2022-01-03 23:19 ` pinskia at gcc dot gnu.org
  2022-01-20  4:10 ` egallager at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-03 23:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87983

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-01-03

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

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

* [Bug c/87983] Feature: Add a warning when case labels from a different enum than the one in switch(EXPR) are used
       [not found] <bug-87983-4@http.gcc.gnu.org/bugzilla/>
  2022-01-03 23:18 ` [Bug c/87983] Feature: Add a warning when case labels from a different enum than the one in switch(EXPR) are used pinskia at gcc dot gnu.org
  2022-01-03 23:19 ` pinskia at gcc dot gnu.org
@ 2022-01-20  4:10 ` egallager at gcc dot gnu.org
  2022-01-20 10:40 ` avarab at gmail dot com
  2022-04-10 22:03 ` egallager at gcc dot gnu.org
  4 siblings, 0 replies; 5+ messages in thread
From: egallager at gcc dot gnu.org @ 2022-01-20  4:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87983

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |egallager at gcc dot gnu.org

--- Comment #3 from Eric Gallager <egallager at gcc dot gnu.org> ---
Is the expectation that this would come from -Wswitch, -Wswitch-enum,
-Wenum-compare, -Wenum-conversion, or some new flag?

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

* [Bug c/87983] Feature: Add a warning when case labels from a different enum than the one in switch(EXPR) are used
       [not found] <bug-87983-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2022-01-20  4:10 ` egallager at gcc dot gnu.org
@ 2022-01-20 10:40 ` avarab at gmail dot com
  2022-04-10 22:03 ` egallager at gcc dot gnu.org
  4 siblings, 0 replies; 5+ messages in thread
From: avarab at gmail dot com @ 2022-01-20 10:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87983

--- Comment #4 from Ævar Arnfjörð Bjarmason <avarab at gmail dot com> ---
(In reply to Eric Gallager from comment #3)
> Is the expectation that this would come from -Wswitch, -Wswitch-enum,
> -Wenum-compare, -Wenum-conversion, or some new flag?

I think a new flag would be best. The clang compiler has a C++-only
-Wenum-compare-switch flag which will warn about this.

Here's a better and updated testcase showing how it works:

```
#include <stdio.h>

enum enum_x { A, B };
enum enum_y { C, D };

int main(void)
{   
    enum enum_y y = C;
    switch (y) {
      case A: /* Should warn: switch() on C instead of A */
        puts("A");
        break;
      case D:
        puts("B");
        break;
    }
}
```

And a one-liner showing how gcc, g++, clang and clang++ handle it:

```
$ for cc in gcc g++ clang clang++; do echo $cc: && $cc -Wswitch -Wswitch-enum
-Wenum-compare -o test test.c; ./test; done
gcc:
A
g++:
A
clang:
A
clang++:
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is
deprecated [-Wdeprecated]
test.c:10:12: warning: comparison of different enumeration types in switch
statement ('enum enum_y' and 'enum_x') [-Wenum-compare-switch]
      case A: /* Should warn: switch() on C instead of A */
           ^
1 warning generated.
A
```

(This is with a local GCC 10.* and LLVM 13.*, on a Debian box). Documentation
for the Clang warning:
https://clang.llvm.org/docs/DiagnosticsReference.html#wenum-compare-switch

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

* [Bug c/87983] Feature: Add a warning when case labels from a different enum than the one in switch(EXPR) are used
       [not found] <bug-87983-4@http.gcc.gnu.org/bugzilla/>
                   ` (3 preceding siblings ...)
  2022-01-20 10:40 ` avarab at gmail dot com
@ 2022-04-10 22:03 ` egallager at gcc dot gnu.org
  4 siblings, 0 replies; 5+ messages in thread
From: egallager at gcc dot gnu.org @ 2022-04-10 22:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87983

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|NEW                         |RESOLVED

--- Comment #5 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Ævar Arnfjörð Bjarmason from comment #4)
> (In reply to Eric Gallager from comment #3)
> > Is the expectation that this would come from -Wswitch, -Wswitch-enum,
> > -Wenum-compare, -Wenum-conversion, or some new flag?
> 
> I think a new flag would be best. The clang compiler has a C++-only
> -Wenum-compare-switch flag which will warn about this.
> 
> Here's a better and updated testcase showing how it works:
> 
> ```
> #include <stdio.h>
> 
> enum enum_x { A, B };
> enum enum_y { C, D };
> 
> int main(void)
> {   
>     enum enum_y y = C;
>     switch (y) {
>       case A: /* Should warn: switch() on C instead of A */
>         puts("A");
>         break;
>       case D:
>         puts("B");
>         break;
>     }
> }
> ```
> 
> And a one-liner showing how gcc, g++, clang and clang++ handle it:
> 
> ```
> $ for cc in gcc g++ clang clang++; do echo $cc: && $cc -Wswitch
> -Wswitch-enum -Wenum-compare -o test test.c; ./test; done
> gcc:
> A
> g++:
> A
> clang:
> A
> clang++:
> clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior
> is deprecated [-Wdeprecated]
> test.c:10:12: warning: comparison of different enumeration types in switch
> statement ('enum enum_y' and 'enum_x') [-Wenum-compare-switch]
>       case A: /* Should warn: switch() on C instead of A */
>            ^
> 1 warning generated.
> A
> ```
> 
> (This is with a local GCC 10.* and LLVM 13.*, on a Debian box).
> Documentation for the Clang warning:
> https://clang.llvm.org/docs/DiagnosticsReference.html#wenum-compare-switch

ok so in that case, this is bug 87404

*** This bug has been marked as a duplicate of bug 87404 ***

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

end of thread, other threads:[~2022-04-10 22:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-87983-4@http.gcc.gnu.org/bugzilla/>
2022-01-03 23:18 ` [Bug c/87983] Feature: Add a warning when case labels from a different enum than the one in switch(EXPR) are used pinskia at gcc dot gnu.org
2022-01-03 23:19 ` pinskia at gcc dot gnu.org
2022-01-20  4:10 ` egallager at gcc dot gnu.org
2022-01-20 10:40 ` avarab at gmail dot com
2022-04-10 22:03 ` egallager at gcc dot gnu.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).