[tz] [PROPOSED 3/7] Use C23 'unreachable' if available

Paul Eggert eggert at cs.ucla.edu
Thu Oct 20 19:44:02 UTC 2022


* private.h: Include stddef.h here, for 'unreachable'.
(unreachable): Rename from UNREACHABLE.  All uses changed.
This is now the C23 macro if available, a substitute otherwise.
However, if DEBUG is defined, it is still 'abort'.
* zic.c: Do not include stddef.h here, private.h does that now.
---
 NEWS        |  2 +-
 localtime.c |  2 +-
 private.h   | 22 +++++++++++++---------
 zic.c       |  7 +++----
 4 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/NEWS b/NEWS
index 2532691..a5b680f 100644
--- a/NEWS
+++ b/NEWS
@@ -38,7 +38,7 @@ Unreleased, experimental changes
     400-year Gregorian cycle.  (Problem reported by Bradley White.)
 
     Take advantage of the following C23 features if available:
-    bool/true/false keywords and __has_include.
+    bool/true/false keywords, __has_include, unreachable.
 
 
 Release 2022e - 2022-10-11 11:13:02 -0700
diff --git a/localtime.c b/localtime.c
index 0ad28dc..c556531 100644
--- a/localtime.c
+++ b/localtime.c
@@ -1098,7 +1098,7 @@ transtime(const int year, register const struct rule *const rulep,
 			value += mon_lengths[leapyear][i] * SECSPERDAY;
 		break;
 
-	default: UNREACHABLE();
+	default: unreachable();
 	}
 
 	/*
diff --git a/private.h b/private.h
index 6510c52..5624abc 100644
--- a/private.h
+++ b/private.h
@@ -161,6 +161,7 @@
 #undef tzalloc
 #undef tzfree
 
+#include <stddef.h>
 #include <sys/types.h>	/* for time_t */
 #include <string.h>
 #include <limits.h>	/* for CHAR_BIT et al. */
@@ -711,16 +712,19 @@ time_t time2posix_z(timezone_t, time_t) ATTRIBUTE_PURE;
 #endif
 
 #ifdef DEBUG
-# define UNREACHABLE() abort()
-#elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
-# define UNREACHABLE() __builtin_unreachable()
-#elif defined __has_builtin
-# if __has_builtin(__builtin_unreachable)
-#  define UNREACHABLE() __builtin_unreachable()
+# undef unreachable
+# define unreachable() abort()
+#elif !defined unreachable
+# ifdef __has_builtin
+#  if __has_builtin(__builtin_unreachable)
+#   define unreachable() __builtin_unreachable()
+#  endif
+# elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
+#  define unreachable() __builtin_unreachable()
+# endif
+# ifndef unreachable
+#  define unreachable() ((void) 0)
 # endif
-#endif
-#ifndef UNREACHABLE
-# define UNREACHABLE() ((void) 0)
 #endif
 
 /*
diff --git a/zic.c b/zic.c
index ff04793..38a65eb 100644
--- a/zic.c
+++ b/zic.c
@@ -20,7 +20,6 @@
 #include <locale.h>
 #include <signal.h>
 #include <stdarg.h>
-#include <stddef.h>
 #include <stdio.h>
 
 typedef int_fast64_t	zic_t;
@@ -1457,7 +1456,7 @@ infile(const char *name)
 					inexpires(fields, nfields);
 					wantcont = false;
 					break;
-				default: UNREACHABLE();
+				default: unreachable();
 			}
 		}
 	}
@@ -1903,7 +1902,7 @@ rulesub(struct rule *rp, const char *loyearp, const char *hiyearp,
 		case YR_MAXIMUM:
 			rp->r_loyear = ZIC_MAX;
 			break;
-		default: UNREACHABLE();
+		default: unreachable();
 	} else if (sscanf(cp, "%"SCNdZIC"%c", &rp->r_loyear, &xs) != 1) {
 		error(_("invalid starting year"));
 		return false;
@@ -1921,7 +1920,7 @@ rulesub(struct rule *rp, const char *loyearp, const char *hiyearp,
 		case YR_ONLY:
 			rp->r_hiyear = rp->r_loyear;
 			break;
-		default: UNREACHABLE();
+		default: unreachable();
 	} else if (sscanf(cp, "%"SCNdZIC"%c", &rp->r_hiyear, &xs) != 1) {
 		error(_("invalid ending year"));
 		return false;
-- 
2.37.3



More information about the tz mailing list