GNU bug report logs - #75964
Switching the Emacs build to -Wswitch-enum in src/

Previous Next

Package: emacs;

Reported by: Pip Cet <pipcet <at> protonmail.com>

Date: Fri, 31 Jan 2025 09:41:02 UTC

Severity: wishlist

Full log


Message #8 received at 75964 <at> debbugs.gnu.org (full text, mbox):

From: Pip Cet <pipcet <at> protonmail.com>
To: 75964 <at> debbugs.gnu.org, Paul Eggert <eggert <at> cs.ucla.edu>,
 Stefan Kangas <stefankangas <at> gmail.com>
Subject: Re: Switching the Emacs build to -Wswitch-enum in src/
Date: Fri, 31 Jan 2025 11:05:12 +0000
Pip Cet <pipcet <at> protonmail.com> writes:

> Some preliminary patches to follow once this has a bug number.

Here's a first small patch series.  Some initial remarks

1. I built a fresh GCC from scratch, so I'd be able to get the latest
warnings.  Unfortunately, GCC now thinks:

  static const char hexchar[16] = "0123456789ABCDEF";

is unsafe and the [16] should be [17] or omitted.  So while I changed
that in my build, I'm not including that in the patches.

This GCC version is also patched to "infer" a switch type: if you do

int x:n

switch (x)
{
case PVEC_NORMAL:
  return;
default:
  emacs_abort ();
}

this GCC version will warn about the other PVEC types, which should
appear explicitly in the enum, because PVEC_NORMAL is part of an
enumeration, even though x is an integer.

Onwards to the patches:

First. it enables -Wswitch-enum if the
compiler supports it:

From 474b0b2aa3deb1a674dd7b112bcdbbe596475ad1 Mon Sep 17 00:00:00 2001
From: Pip Cet <pipcet <at> protonmail.com>
Subject: [PATCH 1/5] Enable -Wswitch-enum (bug#75964)

* configure.ac: Enable warning -Wswitch-enum if available.
---
 configure.ac | 1 +
 1 file changed, 1 insertion(+)

diff --git a/configure.ac b/configure.ac
index ba39074c83a..caa12347738 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1855,6 +1855,7 @@ AC_DEFUN
     [nw="$nw -Wsuggest-attribute=noreturn"])
 
   gl_MANYWARN_ALL_GCC([ws])
+  ws="$ws -Wswitch-enum"
   gl_MANYWARN_COMPLEMENT([ws], [$ws], [$nw])
   for w in $ws; do
     gl_WARN_ADD([$w])
-- 
2.48.1

Then, it adds a PACIFYING_CFLAGS variable set on a per-file basis to
src/Makefile.in, so we can list those files which we don't want to
compile with this new warning flag yet:

From bb884c228ee27c97682007119a21ec202c3799ee Mon Sep 17 00:00:00 2001
From: Pip Cet <pipcet <at> protonmail.com>
Subject: [PATCH] Disable -Wswitch-enum in lib-src (bug#75964)

* lib-src/Makefile.in (PACIFYING_CFLAGS, unfixed_Wswitch_obj): New
variables.  Use it.
---
 lib-src/Makefile.in | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/lib-src/Makefile.in b/lib-src/Makefile.in
index 439d9a1a52d..b2b596cfac4 100644
--- a/lib-src/Makefile.in
+++ b/lib-src/Makefile.in
@@ -274,12 +274,27 @@ BASE_CFLAGS =
 	      -I. -I../src -I../lib \
 	      -I${srcdir} -I${srcdir}/../src -I${srcdir}/../lib
 
-ALL_CFLAGS = ${BASE_CFLAGS} ${PROFILING_CFLAGS} ${LDFLAGS} ${CPPFLAGS} ${CFLAGS}
+ALL_CFLAGS = ${BASE_CFLAGS} ${PROFILING_CFLAGS} ${LDFLAGS} ${CPPFLAGS} ${CFLAGS} ${PACIFYING_CFLAGS}
 CPP_CFLAGS = ${BASE_CFLAGS} ${PROFILING_CFLAGS} ${CPPFLAGS} ${CFLAGS}
 
 ALL_CXXFLAGS = $(filter-out ${NON_CXX_CFLAGS},${BASE_CFLAGS}) \
   ${PROFILING_CFLAGS} ${LDFLAGS} ${CPPFLAGS} ${CFLAGS} ${CXXFLAGS} ${HAIKU_CFLAGS}
 
+unfixed_Wswitch_obj += ctags${EXEEXT}
+unfixed_Wswitch_obj += etags${EXEEXT}
+unfixed_Wswitch_obj += ebrowse${EXEEXT}
+unfixed_Wswitch_obj += ctags${EXEEXT}
+unfixed_Wswitch_obj += asset-directory-tool${EXEEXT}
+unfixed_Wswitch_obj += ebrowse${EXEEXT}
+unfixed_Wswitch_obj += make-docfile${EXEEXT}
+unfixed_Wswitch_obj += make-fingerprint${EXEEXT}
+unfixed_Wswitch_obj += movemail${EXEEXT}
+unfixed_Wswitch_obj += emacsclient${EXEEXT}
+unfixed_Wswitch_obj += emacsclientw${EXEEXT}
+unfixed_Wswitch_obj += hexl${EXEEXT}
+unfixed_Wswitch_obj += update-game-score${EXEEXT}
+
+$(unfixed_Wswitch_obj): PACIFYING_CFLAGS += -Wno-switch-enum -Wno-switch
 # Configuration files for .o files to depend on.
 config_h = ../src/config.h $(srcdir)/../src/conf_post.h
 
-- 
2.48.1

From ccf6b7f0690bcc11d0cba55997cd427fd7d18804 Mon Sep 17 00:00:00 2001
From: Pip Cet <pipcet <at> protonmail.com>
Subject: [PATCH 3/5] Per-file flags to disable -Wswitch-enum in src
 (bug#75964)

* src/Makefile.in (unfixed_wenum_obj): New variable.  Make it include
all known objects. (.c.o): Use it.
---
 src/Makefile.in | 177 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 176 insertions(+), 1 deletion(-)

diff --git a/src/Makefile.in b/src/Makefile.in
index d987124d29d..cb75ffc8482 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -455,7 +455,7 @@ ALL_CXX_CFLAGS =
 
 .SUFFIXES: .c .m .cc
 .c.o:
-	$(AM_V_CC)$(CC) -c $(CPPFLAGS) $(ALL_CFLAGS) $(PROFILING_CFLAGS) $<
+	$(AM_V_CC)$(CC) -c $(CPPFLAGS) $(ALL_CFLAGS) $(PROFILING_CFLAGS) $(PACIFYING_CFLAGS) $<
 .m.o:
 	$(AM_V_CC)$(CC) -c $(CPPFLAGS) $(ALL_OBJC_CFLAGS) $(PROFILING_CFLAGS) $<
 .cc.o:
@@ -484,6 +484,181 @@ base_obj =
 	$(MSDOS_OBJ) $(MSDOS_X_OBJ) $(NS_OBJ) $(CYGWIN_OBJ) $(FONT_OBJ)        \
 	$(W32_OBJ) $(WINDOW_SYSTEM_OBJ) $(XGSELOBJ)			       \
 	$(HAIKU_OBJ) $(PGTK_OBJ) $(ANDROID_OBJ)
+
+unfixed_Wswitch_obj += alloc.o
+unfixed_Wswitch_obj += android-emacs.o
+unfixed_Wswitch_obj += android.o
+unfixed_Wswitch_obj += androidfns.o
+unfixed_Wswitch_obj += androidfont.o
+unfixed_Wswitch_obj += androidmenu.o
+unfixed_Wswitch_obj += androidselect.o
+unfixed_Wswitch_obj += androidterm.o
+unfixed_Wswitch_obj += androidvfs.o
+unfixed_Wswitch_obj += atimer.o
+unfixed_Wswitch_obj += bidi.o
+unfixed_Wswitch_obj += bignum.o
+unfixed_Wswitch_obj += buffer.o
+unfixed_Wswitch_obj += bytecode.o
+unfixed_Wswitch_obj += callint.o
+unfixed_Wswitch_obj += callproc.o
+unfixed_Wswitch_obj += casefiddle.o
+unfixed_Wswitch_obj += casetab.o
+unfixed_Wswitch_obj += category.o
+unfixed_Wswitch_obj += ccl.o
+unfixed_Wswitch_obj += character.o
+unfixed_Wswitch_obj += charset.o
+unfixed_Wswitch_obj += chartab.o
+unfixed_Wswitch_obj += cm.o
+unfixed_Wswitch_obj += cmds.o
+unfixed_Wswitch_obj += coding.o
+unfixed_Wswitch_obj += comp.o
+unfixed_Wswitch_obj += composite.o
+unfixed_Wswitch_obj += cygw32.o
+unfixed_Wswitch_obj += data.o
+unfixed_Wswitch_obj += dbusbind.o
+unfixed_Wswitch_obj += decompress.o
+unfixed_Wswitch_obj += dired.o
+unfixed_Wswitch_obj += dispnew.o
+unfixed_Wswitch_obj += doc.o
+unfixed_Wswitch_obj += doprnt.o
+unfixed_Wswitch_obj += dosfns.o
+unfixed_Wswitch_obj += dynlib.o
+unfixed_Wswitch_obj += editfns.o
+unfixed_Wswitch_obj += emacs-module.o
+unfixed_Wswitch_obj += emacs.o
+unfixed_Wswitch_obj += emacsgtkfixed.o
+unfixed_Wswitch_obj += eval.o
+unfixed_Wswitch_obj += fileio.o
+unfixed_Wswitch_obj += filelock.o
+unfixed_Wswitch_obj += firstfile.o
+unfixed_Wswitch_obj += floatfns.o
+unfixed_Wswitch_obj += fns.o
+unfixed_Wswitch_obj += font.o
+unfixed_Wswitch_obj += fontset.o
+unfixed_Wswitch_obj += frame.o
+unfixed_Wswitch_obj += fringe.o
+unfixed_Wswitch_obj += ftcrfont.o
+unfixed_Wswitch_obj += ftfont.o
+unfixed_Wswitch_obj += gfilenotify.o
+unfixed_Wswitch_obj += gmalloc.o
+unfixed_Wswitch_obj += gnutls.o
+unfixed_Wswitch_obj += gtkutil.o
+unfixed_Wswitch_obj += haiku.o
+unfixed_Wswitch_obj += haiku_io.o
+unfixed_Wswitch_obj += haikufns.o
+unfixed_Wswitch_obj += haikufont.o
+unfixed_Wswitch_obj += haikuimage.o
+unfixed_Wswitch_obj += haikumenu.o
+unfixed_Wswitch_obj += haikuselect.o
+unfixed_Wswitch_obj += haikuterm.o
+unfixed_Wswitch_obj += hbfont.o
+unfixed_Wswitch_obj += image.o
+unfixed_Wswitch_obj += indent.o
+unfixed_Wswitch_obj += inotify.o
+unfixed_Wswitch_obj += insdel.o
+unfixed_Wswitch_obj += intervals.o
+unfixed_Wswitch_obj += itree.o
+unfixed_Wswitch_obj += json.o
+unfixed_Wswitch_obj += keyboard.o
+unfixed_Wswitch_obj += keymap.o
+unfixed_Wswitch_obj += kqueue.o
+unfixed_Wswitch_obj += lastfile.o
+unfixed_Wswitch_obj += lcms.o
+unfixed_Wswitch_obj += lread.o
+unfixed_Wswitch_obj += macfont.o
+unfixed_Wswitch_obj += macros.o
+unfixed_Wswitch_obj += marker.o
+unfixed_Wswitch_obj += menu.o
+unfixed_Wswitch_obj += minibuf.o
+unfixed_Wswitch_obj += msdos.o
+unfixed_Wswitch_obj += nsfns.o
+unfixed_Wswitch_obj += nsfont.o
+unfixed_Wswitch_obj += nsimage.o
+unfixed_Wswitch_obj += nsmenu.o
+unfixed_Wswitch_obj += nsselect.o
+unfixed_Wswitch_obj += nsterm.o
+unfixed_Wswitch_obj += nsxwidget.o
+unfixed_Wswitch_obj += pdumper.o
+unfixed_Wswitch_obj += pgtkfns.o
+unfixed_Wswitch_obj += pgtkim.o
+unfixed_Wswitch_obj += pgtkmenu.o
+unfixed_Wswitch_obj += pgtkselect.o
+unfixed_Wswitch_obj += pgtkterm.o
+unfixed_Wswitch_obj += print.o
+unfixed_Wswitch_obj += process.o
+unfixed_Wswitch_obj += profiler.o
+unfixed_Wswitch_obj += ralloc.o
+unfixed_Wswitch_obj += regex-emacs.o
+unfixed_Wswitch_obj += region-cache.o
+unfixed_Wswitch_obj += scroll.o
+unfixed_Wswitch_obj += search.o
+unfixed_Wswitch_obj += sfnt.o
+unfixed_Wswitch_obj += sfntfont-android.o
+unfixed_Wswitch_obj += sfntfont.o
+unfixed_Wswitch_obj += sheap.o
+unfixed_Wswitch_obj += sort.o
+unfixed_Wswitch_obj += sound.o
+unfixed_Wswitch_obj += sqlite.o
+unfixed_Wswitch_obj += syntax.o
+unfixed_Wswitch_obj += sysdep.o
+unfixed_Wswitch_obj += systhread.o
+unfixed_Wswitch_obj += term.o
+unfixed_Wswitch_obj += termcap.o
+unfixed_Wswitch_obj += terminal.o
+unfixed_Wswitch_obj += terminfo.o
+unfixed_Wswitch_obj += textconv.o
+unfixed_Wswitch_obj += textprop.o
+unfixed_Wswitch_obj += thread.o
+unfixed_Wswitch_obj += timefns.o
+unfixed_Wswitch_obj += tparam.o
+unfixed_Wswitch_obj += treesit.o
+unfixed_Wswitch_obj += undo.o
+unfixed_Wswitch_obj += unexaix.o
+unfixed_Wswitch_obj += unexcoff.o
+unfixed_Wswitch_obj += unexcw.o
+unfixed_Wswitch_obj += unexelf.o
+unfixed_Wswitch_obj += unexhp9k800.o
+unfixed_Wswitch_obj += unexmacosx.o
+unfixed_Wswitch_obj += unexsol.o
+unfixed_Wswitch_obj += unexw32.o
+unfixed_Wswitch_obj += vm-limit.o
+unfixed_Wswitch_obj += w16select.o
+unfixed_Wswitch_obj += w32.o
+unfixed_Wswitch_obj += w32console.o
+unfixed_Wswitch_obj += w32cygwinx.o
+unfixed_Wswitch_obj += w32dwrite.o
+unfixed_Wswitch_obj += w32fns.o
+unfixed_Wswitch_obj += w32font.o
+unfixed_Wswitch_obj += w32heap.o
+unfixed_Wswitch_obj += w32image.o
+unfixed_Wswitch_obj += w32inevt.o
+unfixed_Wswitch_obj += w32menu.o
+unfixed_Wswitch_obj += w32notify.o
+unfixed_Wswitch_obj += w32proc.o
+unfixed_Wswitch_obj += w32reg.o
+unfixed_Wswitch_obj += w32select.o
+unfixed_Wswitch_obj += w32term.o
+unfixed_Wswitch_obj += w32uniscribe.o
+unfixed_Wswitch_obj += w32xfns.o
+unfixed_Wswitch_obj += widget.o
+unfixed_Wswitch_obj += window.o
+unfixed_Wswitch_obj += xdisp.o
+unfixed_Wswitch_obj += xfaces.o
+unfixed_Wswitch_obj += xfns.o
+unfixed_Wswitch_obj += xfont.o
+unfixed_Wswitch_obj += xftfont.o
+unfixed_Wswitch_obj += xgselect.o
+unfixed_Wswitch_obj += xmenu.o
+unfixed_Wswitch_obj += xml.o
+unfixed_Wswitch_obj += xrdb.o
+unfixed_Wswitch_obj += xselect.o
+unfixed_Wswitch_obj += xsettings.o
+unfixed_Wswitch_obj += xsmfns.o
+unfixed_Wswitch_obj += xterm.o
+unfixed_Wswitch_obj += xwidget.o
+
+$(unfixed_Wswitch_obj): PACIFYING_CFLAGS += -Wno-switch-enum -Wno-switch
+
 doc_obj = $(base_obj) $(NS_OBJC_OBJ)
 obj = $(doc_obj) $(HAIKU_CXX_OBJ)
 
-- 
2.48.1

Since that listed *all* files, let's remove those which do not actually
contain any switch statements:

From 8ed603be95fdfe911cd567b88a7a1992695f4dc7 Mon Sep 17 00:00:00 2001
From: Pip Cet <pipcet <at> protonmail.com>
Subject: [PATCH 4/5] C files without switch statements are safe for
 -Wswitch-enum (bug#75964)

* src/Makefile.in (unfixed_Wswitch_obj): Remove files which do not
contain any switches.
---
 src/Makefile.in | 61 -------------------------------------------------
 1 file changed, 61 deletions(-)

diff --git a/src/Makefile.in b/src/Makefile.in
index cb75ffc8482..8f54006e276 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -486,88 +486,57 @@ base_obj =
 	$(HAIKU_OBJ) $(PGTK_OBJ) $(ANDROID_OBJ)
 
 unfixed_Wswitch_obj += alloc.o
-unfixed_Wswitch_obj += android-emacs.o
 unfixed_Wswitch_obj += android.o
 unfixed_Wswitch_obj += androidfns.o
-unfixed_Wswitch_obj += androidfont.o
-unfixed_Wswitch_obj += androidmenu.o
-unfixed_Wswitch_obj += androidselect.o
 unfixed_Wswitch_obj += androidterm.o
 unfixed_Wswitch_obj += androidvfs.o
 unfixed_Wswitch_obj += atimer.o
 unfixed_Wswitch_obj += bidi.o
-unfixed_Wswitch_obj += bignum.o
 unfixed_Wswitch_obj += buffer.o
 unfixed_Wswitch_obj += bytecode.o
 unfixed_Wswitch_obj += callint.o
-unfixed_Wswitch_obj += callproc.o
-unfixed_Wswitch_obj += casefiddle.o
-unfixed_Wswitch_obj += casetab.o
-unfixed_Wswitch_obj += category.o
 unfixed_Wswitch_obj += ccl.o
-unfixed_Wswitch_obj += character.o
-unfixed_Wswitch_obj += charset.o
-unfixed_Wswitch_obj += chartab.o
 unfixed_Wswitch_obj += cm.o
-unfixed_Wswitch_obj += cmds.o
 unfixed_Wswitch_obj += coding.o
 unfixed_Wswitch_obj += comp.o
-unfixed_Wswitch_obj += composite.o
-unfixed_Wswitch_obj += cygw32.o
 unfixed_Wswitch_obj += data.o
 unfixed_Wswitch_obj += dbusbind.o
-unfixed_Wswitch_obj += decompress.o
 unfixed_Wswitch_obj += dired.o
 unfixed_Wswitch_obj += dispnew.o
-unfixed_Wswitch_obj += doc.o
 unfixed_Wswitch_obj += doprnt.o
 unfixed_Wswitch_obj += dosfns.o
-unfixed_Wswitch_obj += dynlib.o
 unfixed_Wswitch_obj += editfns.o
 unfixed_Wswitch_obj += emacs-module.o
 unfixed_Wswitch_obj += emacs.o
-unfixed_Wswitch_obj += emacsgtkfixed.o
 unfixed_Wswitch_obj += eval.o
 unfixed_Wswitch_obj += fileio.o
 unfixed_Wswitch_obj += filelock.o
-unfixed_Wswitch_obj += firstfile.o
-unfixed_Wswitch_obj += floatfns.o
 unfixed_Wswitch_obj += fns.o
 unfixed_Wswitch_obj += font.o
 unfixed_Wswitch_obj += fontset.o
 unfixed_Wswitch_obj += frame.o
 unfixed_Wswitch_obj += fringe.o
-unfixed_Wswitch_obj += ftcrfont.o
 unfixed_Wswitch_obj += ftfont.o
 unfixed_Wswitch_obj += gfilenotify.o
 unfixed_Wswitch_obj += gmalloc.o
 unfixed_Wswitch_obj += gnutls.o
 unfixed_Wswitch_obj += gtkutil.o
-unfixed_Wswitch_obj += haiku.o
 unfixed_Wswitch_obj += haiku_io.o
 unfixed_Wswitch_obj += haikufns.o
 unfixed_Wswitch_obj += haikufont.o
-unfixed_Wswitch_obj += haikuimage.o
-unfixed_Wswitch_obj += haikumenu.o
 unfixed_Wswitch_obj += haikuselect.o
 unfixed_Wswitch_obj += haikuterm.o
 unfixed_Wswitch_obj += hbfont.o
 unfixed_Wswitch_obj += image.o
 unfixed_Wswitch_obj += indent.o
-unfixed_Wswitch_obj += inotify.o
 unfixed_Wswitch_obj += insdel.o
-unfixed_Wswitch_obj += intervals.o
 unfixed_Wswitch_obj += itree.o
 unfixed_Wswitch_obj += json.o
 unfixed_Wswitch_obj += keyboard.o
 unfixed_Wswitch_obj += keymap.o
-unfixed_Wswitch_obj += kqueue.o
-unfixed_Wswitch_obj += lastfile.o
-unfixed_Wswitch_obj += lcms.o
 unfixed_Wswitch_obj += lread.o
 unfixed_Wswitch_obj += macfont.o
 unfixed_Wswitch_obj += macros.o
-unfixed_Wswitch_obj += marker.o
 unfixed_Wswitch_obj += menu.o
 unfixed_Wswitch_obj += minibuf.o
 unfixed_Wswitch_obj += msdos.o
@@ -580,80 +549,50 @@ unfixed_Wswitch_obj +=
 unfixed_Wswitch_obj += nsxwidget.o
 unfixed_Wswitch_obj += pdumper.o
 unfixed_Wswitch_obj += pgtkfns.o
-unfixed_Wswitch_obj += pgtkim.o
-unfixed_Wswitch_obj += pgtkmenu.o
-unfixed_Wswitch_obj += pgtkselect.o
 unfixed_Wswitch_obj += pgtkterm.o
 unfixed_Wswitch_obj += print.o
 unfixed_Wswitch_obj += process.o
 unfixed_Wswitch_obj += profiler.o
-unfixed_Wswitch_obj += ralloc.o
 unfixed_Wswitch_obj += regex-emacs.o
-unfixed_Wswitch_obj += region-cache.o
-unfixed_Wswitch_obj += scroll.o
 unfixed_Wswitch_obj += search.o
 unfixed_Wswitch_obj += sfnt.o
-unfixed_Wswitch_obj += sfntfont-android.o
 unfixed_Wswitch_obj += sfntfont.o
-unfixed_Wswitch_obj += sheap.o
-unfixed_Wswitch_obj += sort.o
 unfixed_Wswitch_obj += sound.o
 unfixed_Wswitch_obj += sqlite.o
 unfixed_Wswitch_obj += syntax.o
 unfixed_Wswitch_obj += sysdep.o
-unfixed_Wswitch_obj += systhread.o
 unfixed_Wswitch_obj += term.o
-unfixed_Wswitch_obj += termcap.o
 unfixed_Wswitch_obj += terminal.o
-unfixed_Wswitch_obj += terminfo.o
 unfixed_Wswitch_obj += textconv.o
 unfixed_Wswitch_obj += textprop.o
 unfixed_Wswitch_obj += thread.o
 unfixed_Wswitch_obj += timefns.o
 unfixed_Wswitch_obj += tparam.o
 unfixed_Wswitch_obj += treesit.o
-unfixed_Wswitch_obj += undo.o
 unfixed_Wswitch_obj += unexaix.o
-unfixed_Wswitch_obj += unexcoff.o
-unfixed_Wswitch_obj += unexcw.o
 unfixed_Wswitch_obj += unexelf.o
-unfixed_Wswitch_obj += unexhp9k800.o
 unfixed_Wswitch_obj += unexmacosx.o
-unfixed_Wswitch_obj += unexsol.o
-unfixed_Wswitch_obj += unexw32.o
-unfixed_Wswitch_obj += vm-limit.o
 unfixed_Wswitch_obj += w16select.o
 unfixed_Wswitch_obj += w32.o
-unfixed_Wswitch_obj += w32console.o
-unfixed_Wswitch_obj += w32cygwinx.o
-unfixed_Wswitch_obj += w32dwrite.o
 unfixed_Wswitch_obj += w32fns.o
 unfixed_Wswitch_obj += w32font.o
 unfixed_Wswitch_obj += w32heap.o
 unfixed_Wswitch_obj += w32image.o
 unfixed_Wswitch_obj += w32inevt.o
 unfixed_Wswitch_obj += w32menu.o
-unfixed_Wswitch_obj += w32notify.o
 unfixed_Wswitch_obj += w32proc.o
-unfixed_Wswitch_obj += w32reg.o
 unfixed_Wswitch_obj += w32select.o
 unfixed_Wswitch_obj += w32term.o
-unfixed_Wswitch_obj += w32uniscribe.o
 unfixed_Wswitch_obj += w32xfns.o
-unfixed_Wswitch_obj += widget.o
 unfixed_Wswitch_obj += window.o
 unfixed_Wswitch_obj += xdisp.o
 unfixed_Wswitch_obj += xfaces.o
 unfixed_Wswitch_obj += xfns.o
-unfixed_Wswitch_obj += xfont.o
-unfixed_Wswitch_obj += xftfont.o
 unfixed_Wswitch_obj += xgselect.o
 unfixed_Wswitch_obj += xmenu.o
-unfixed_Wswitch_obj += xml.o
 unfixed_Wswitch_obj += xrdb.o
 unfixed_Wswitch_obj += xselect.o
 unfixed_Wswitch_obj += xsettings.o
-unfixed_Wswitch_obj += xsmfns.o
 unfixed_Wswitch_obj += xterm.o
 unfixed_Wswitch_obj += xwidget.o
 
-- 
2.48.1

Finally, convert the first file (alloc.c) to use exhaustive switches in
a way that makes it safe for -Wswitch-enum:

From 0de72741ed4bb5ef1b299390e4a031135b042f65 Mon Sep 17 00:00:00 2001
From: Pip Cet <pipcet <at> protonmail.com>
Subject: [PATCH 5/5] Make alloc.c safe for -Wswitch-enum (#75996)

---
 src/Makefile.in |  1 -
 src/alloc.c     | 51 +++++++++++++++++++++++++++++++++++--------------
 2 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/src/Makefile.in b/src/Makefile.in
index 8f54006e276..7388284fef2 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -485,7 +485,6 @@ base_obj =
 	$(W32_OBJ) $(WINDOW_SYSTEM_OBJ) $(XGSELOBJ)			       \
 	$(HAIKU_OBJ) $(PGTK_OBJ) $(ANDROID_OBJ)
 
-unfixed_Wswitch_obj += alloc.o
 unfixed_Wswitch_obj += android.o
 unfixed_Wswitch_obj += androidfns.o
 unfixed_Wswitch_obj += androidterm.o
diff --git a/src/alloc.c b/src/alloc.c
index b13c3e49224..e44e7223f90 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -3405,7 +3405,6 @@ cleanup_vector (struct Lisp_Vector *vector)
       break;
     /* Keep the switch exhaustive.  */
     case PVEC_NORMAL_VECTOR:
-    case PVEC_FREE:
     case PVEC_SYMBOL_WITH_POS:
     case PVEC_MISC_PTR:
     case PVEC_PROCESS:
@@ -3425,6 +3424,8 @@ cleanup_vector (struct Lisp_Vector *vector)
     case PVEC_SUB_CHAR_TABLE:
     case PVEC_RECORD:
       break;
+    case PVEC_FREE: eassume (0);
+    default: eassume (0);
     }
 }
 
@@ -5192,8 +5193,7 @@ mark_maybe_pointer (void *p, bool symbol_only)
 	  }
 	  break;
 
-	default:
-	  emacs_abort ();
+	default: eassume (0);
 	}
 
       mark_object (obj);
@@ -5573,8 +5573,7 @@ valid_lisp_object_p (Lisp_Object obj)
     case MEM_TYPE_VECTOR_BLOCK:
       return live_small_vector_p (m, p);
 
-    default:
-      break;
+    default: eassume (0);
     }
 
   return 0;
@@ -7274,10 +7273,31 @@ #define CHECK_ALLOCATED_AND_LIVE_SYMBOL()		((void) 0)
 #endif
 		break;
 
-	      case PVEC_FREE:
-		emacs_abort ();
-
-	      default:
+	      case PVEC_NORMAL_VECTOR:
+	      case PVEC_BIGNUM:
+	      case PVEC_MARKER:
+	      case PVEC_FINALIZER:
+	      case PVEC_SYMBOL_WITH_POS:
+	      case PVEC_MISC_PTR:
+	      case PVEC_USER_PTR:
+	      case PVEC_PROCESS:
+	      case PVEC_TERMINAL:
+	      case PVEC_WINDOW_CONFIGURATION:
+	      case PVEC_OTHER:
+	      case PVEC_XWIDGET:
+	      case PVEC_XWIDGET_VIEW:
+	      case PVEC_THREAD:
+	      case PVEC_MUTEX:
+	      case PVEC_CONDVAR:
+	      case PVEC_MODULE_FUNCTION:
+	      case PVEC_NATIVE_COMP_UNIT:
+	      case PVEC_TS_PARSER:
+	      case PVEC_TS_NODE:
+	      case PVEC_TS_COMPILED_QUERY:
+	      case PVEC_SQLITE:
+	      case PVEC_CLOSURE:
+	      case PVEC_RECORD:
+	      case PVEC_FONT:
 		{
 		  /* A regular vector or pseudovector needing no special
 		     treatment.  */
@@ -7288,6 +7308,9 @@ #define CHECK_ALLOCATED_AND_LIVE_SYMBOL()		((void) 0)
 		  mark_stack_push_values (ptr->contents, size);
 		}
 		break;
+
+	      case PVEC_FREE: eassume (0);
+	      default: eassume (0);
 	      }
 	  }
 	  break;
@@ -7337,7 +7360,7 @@ #define CHECK_ALLOCATED_AND_LIVE_SYMBOL()		((void) 0)
 		   reachable from font_style_table which is also
 		   staticpro'd.  */
 		break;
-	      default: emacs_abort ();
+	      default: eassume (0);
 	      }
 	    if (!PURE_P (XSTRING (ptr->u.s.name)))
 	      set_string_marked (XSTRING (ptr->u.s.name));
@@ -7391,8 +7414,8 @@ #define CHECK_ALLOCATED_AND_LIVE_SYMBOL()		((void) 0)
 	case Lisp_Int1:
 	  break;
 
-	default:
-	  emacs_abort ();
+	case Lisp_Type_Unused0: eassume (0);
+	default: eassume (0);
 	}
     }
 
@@ -7471,8 +7494,8 @@ survives_gc_p (Lisp_Object obj)
         pdumper_object_p (XFLOAT (obj));
       break;
 
-    default:
-      emacs_abort ();
+    case Lisp_Type_Unused0: eassume (0);
+    default: eassume (0);
     }
 
   return survives_p || PURE_P (XPNTR (obj));
-- 
2.48.1


I think this is a good place to stop for comments.  I've converted many
more files in my local tree, but let's agree on what we want to change
before having to do that work over and over again.

Note that alloc.c was a simple case because the enums are all internal
to Emacs and unlikely to change drastically without people touching the
code anyway.  image.c is very different in that regard.

Note that some additional work will be required to ensure we don't pass
-Wno-switch-enum or -Wno-switch to clang, assuming at least some version
of clang don't support these options.

I'll do pdumper.c next.

Thanks!

Pip





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.