GNU bug report logs -
#75964
Switching the Emacs build to -Wswitch-enum in src/
Previous Next
Full log
View this message in rfc822 format
[Message part 1 (text/plain, inline)]
On 2025-02-02 03:48, Pip Cet wrote:
> I've also stayed away from lib-src for now.
Although you did look at lib-src (I see some changes there) you
understandably did not tackle lib-src/etags.c which has many enum
switches and gimmicks to pacify GCC. So I took a crack at etags.c and
came up with an alternative proposal consisting of two suggestions to
support the following goals:
* Cleanly compile with -Wswitch-enum to catch potential errors.
* A style that is easy to explain and understand.
* No changes to GCC needed for now.
* No new C macros in the Emacs source code.
* Source code that is shorter and easier to maintain.
Here's the proposal:
Suggestion 1: Don't treat all enum switch statements the same. Some enum
switch statements merely want to treat a couple of enum values
specially, with all other enum values being default. For these, it's
unlikely that forcing the programmer to list a case for every enum value
will catch many bugs; it's even possible that this would cause more
problems than it'll cure, and it's certainly an annoyance.
For these switch statements, use "switch (+E)" instead of "switch (E)".
This pacifies GCC and clearly signals to the reader that the switch's
cases are not intended to exhaust the enum. A "switch (E)" must list all
the enum values; a "switch (+E)" need not do so. A reasonable guideline
is that if a switch statement has more than three "case X: break;"s then
it may be a good idea to use "switch (+E)" instead of "switch (E)".
Suggestion 2: Omit "default: break;"s present only to tell GCC and/or
the reader that the switch is otherwise not exhaustive. "switch (+expr)"
already does this more concisely and more usefully.
Combine these two suggestions, and I made etags.c shorter (by 23 lines)
and easier to read once you know the style. See attached patch, which
compiles cleanly with -Wswitch-enum.
I assume similar results would apply elsewhere in Emacs.
[0001-Pacify-gcc-Wswitch-enum-in-etags.c.patch (text/x-patch, attachment)]
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.