[tz] [PROPOSED 5/5] Use C23 [[fallthrough]] if available

Paul Eggert eggert at cs.ucla.edu
Mon Nov 21 18:36:43 UTC 2022


Instead of informal /*fallthrough*/ comments, use C23
[[fallthrough]] if available.
* Makefile (GCC_DEBUG_FLAGS): Add -Wimplicit-fallthrough=5.
* private.h (ATTRIBUTE_FALLTHROUGH): New macro, now used whenever
switch branches fall through.
---
 Makefile  |  2 +-
 private.h | 13 +++++++++++++
 zdump.c   |  2 +-
 zic.c     |  8 ++++----
 4 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/Makefile b/Makefile
index b2c6ff6f..afb9d538 100644
--- a/Makefile
+++ b/Makefile
@@ -287,7 +287,7 @@ GCC_DEBUG_FLAGS = -DGCC_LINT -g3 -O3 -fno-common \
   -Wdeclaration-after-statement -Wdouble-promotion \
   -Wduplicated-branches -Wduplicated-cond \
   -Wformat=2 -Wformat-overflow=2 -Wformat-signedness -Wformat-truncation \
-  -Winit-self -Wlogical-op \
+  -Wimplicit-fallthrough=5 -Winit-self -Wlogical-op \
   -Wmissing-declarations -Wmissing-prototypes -Wnested-externs \
   -Wnull-dereference \
   -Wold-style-definition -Woverlength-strings -Wpointer-arith \
diff --git a/private.h b/private.h
index 4d667aa0..7a73eff7 100644
--- a/private.h
+++ b/private.h
@@ -440,6 +440,19 @@ typedef unsigned long uintmax_t;
 # define HAVE_HAS_C_ATTRIBUTE false
 #endif
 
+#if HAVE_HAS_C_ATTRIBUTE
+# if __has_c_attribute(fallthrough)
+#  define ATTRIBUTE_FALLTHROUGH [[fallthrough]]
+# endif
+#endif
+#ifndef ATTRIBUTE_FALLTHROUGH
+# if 7 <= __GNUC__
+#  define ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
+# else
+#  define ATTRIBUTE_FALLTHROUGH ((void) 0)
+# endif
+#endif
+
 #if HAVE_HAS_C_ATTRIBUTE
 # if __has_c_attribute(maybe_unused)
 #  define ATTRIBUTE_MAYBE_UNUSED [[maybe_unused]]
diff --git a/zdump.c b/zdump.c
index 47141655..7acb3e2d 100644
--- a/zdump.c
+++ b/zdump.c
@@ -503,7 +503,7 @@ main(int argc, char *argv[])
 	  case -1:
 	    if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
 	      goto arg_processing_done;
-	    /* Fall through.  */
+	    ATTRIBUTE_FALLTHROUGH;
 	  default:
 	    usage(stderr, EXIT_FAILURE);
 	  }
diff --git a/zic.c b/zic.c
index 4af0ca27..892414af 100644
--- a/zic.c
+++ b/zic.c
@@ -1750,15 +1750,15 @@ gethms(char const *string, char const *errstring)
 	  default: ok = false; break;
 	  case 8:
 	    ok = '0' <= xr && xr <= '9';
-	    /* fallthrough */
+	    ATTRIBUTE_FALLTHROUGH;
 	  case 7:
 	    ok &= ssx == '.';
 	    if (ok && noise)
 	      warning(_("fractional seconds rejected by"
 			" pre-2018 versions of zic"));
-	    /* fallthrough */
-	  case 5: ok &= mmx == ':'; /* fallthrough */
-	  case 3: ok &= hhx == ':'; /* fallthrough */
+	    ATTRIBUTE_FALLTHROUGH;
+	  case 5: ok &= mmx == ':'; ATTRIBUTE_FALLTHROUGH;
+	  case 3: ok &= hhx == ':'; ATTRIBUTE_FALLTHROUGH;
 	  case 1: break;
 	}
 	if (!ok) {
-- 
2.37.2



More information about the tz mailing list