GNU bug report logs -
#75964
Switching the Emacs build to -Wswitch-enum in src/
Previous Next
Full log
View this message in rfc822 format
> Date: Fri, 31 Jan 2025 09:39:45 +0000
> From: Pip Cet via "Bug reports for GNU Emacs,
> the Swiss army knife of text editors" <bug-gnu-emacs <at> gnu.org>
>
> I'm proposing to enable -Wswitch-enum as a warning option when compiling
> Emacs C sources in src/, and to modify those source files to make good
> use of it.
>
> GCC's -Wswitch-enum will warn about a switch statement of the form
>
> enum ABC { A, B, C };
>
> enum ABC x = ...;
>
> switch (x)
> {
> case A:
> case B:
> return 1;
> default:
> return 0;
> }
>
> The reason is that the "default" branch covers both case C and the case
> that the value of x isn't A, B, or, C. C allows this latter case and
> requires compilers to support it, and some Emacs code relies on
> non-enumerated values to be valid.
>
> Instead, with -Wswitch-enum, one should write:
>
> enum ABC { A, B, C };
>
> enum ABC x = ...;
>
> switch (x)
> {
> case A:
> case B:
> return 1;
> case C:
> return 0;
> default: eassume (false);
> }
>
> assuming x is known never to have a non-enumerated value (this is almost
> always the case in Emacs).
What should one do if the enumeration is large and the code wants to
treat all but the few values the same? This is a legitimate use case,
isn't it? Having to spell out all of the values is quite tedious, and
eassume will not do what we want in this case.
This bug report was last modified 127 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.