[PROPOSED] Use C23 [[maybe_unused]] if available
* Makefile (GCC_DEBUG_FLAGS): Omit -Wno-unused-parameters, as the code is now safe for gcc -Wunused-parameters. * private.h (ATTRIBUTE_MAYBE_UNUSED): New macro. * localtime.c (gmtsub): * strftime.c (strftime_l) [HAVE_STRFTIME_L]: * zdump.c (gmtoff): Use it to mark possibly-unused parameters. * zdump.c (hunt): * zic.c (getleapdatetime): Omit unused arg. All callers changed. --- Makefile | 2 +- localtime.c | 4 ++-- private.h | 13 +++++++++++++ strftime.c | 2 +- zdump.c | 13 +++++++------ zic.c | 6 +++--- 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 92d78cb4..b2c6ff6f 100644 --- a/Makefile +++ b/Makefile @@ -300,7 +300,7 @@ GCC_DEBUG_FLAGS = -DGCC_LINT -g3 -O3 -fno-common \ -Wtrampolines -Wundef -Wuninitialized -Wunused-macros -Wuse-after-free=3 \ -Wvariadic-macros -Wvla -Wwrite-strings \ -Wno-address -Wno-format-nonliteral -Wno-sign-compare \ - -Wno-type-limits -Wno-unused-parameter + -Wno-type-limits # # If your system has a "GMT offset" field in its "struct tm"s # (or if you decide to add such a field in your system's "time.h" file), diff --git a/localtime.c b/localtime.c index 7faa5d32..4dd9d259 100644 --- a/localtime.c +++ b/localtime.c @@ -1662,8 +1662,8 @@ localtime_r(const time_t *timep, struct tm *tmp) */ static struct tm * -gmtsub(struct state const *sp, time_t const *timep, int_fast32_t offset, - struct tm *tmp) +gmtsub(ATTRIBUTE_MAYBE_UNUSED struct state const *sp, time_t const *timep, + int_fast32_t offset, struct tm *tmp) { register struct tm * result; diff --git a/private.h b/private.h index 4d40582e..fee18cb2 100644 --- a/private.h +++ b/private.h @@ -433,6 +433,19 @@ typedef unsigned long uintmax_t; # define ATTRIBUTE_FORMAT(spec) /* empty */ #endif +#ifdef __has_c_attribute +# if __has_c_attribute(__maybe_unused__) +# define ATTRIBUTE_MAYBE_UNUSED [[__maybe_unused__]] +# endif +#endif +#ifndef ATTRIBUTE_MAYBE_UNUSED +# if 2 < __GNUC__ + (7 <= __GNUC_MINOR__) +# define ATTRIBUTE_MAYBE_UNUSED __attribute__ ((__unused__)) +# else +# define ATTRIBUTE_MAYBE_UNUSED /* empty */ +# endif +#endif + #if !defined _Noreturn && __STDC_VERSION__ < 201112 # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__) # define _Noreturn __attribute__((__noreturn__)) diff --git a/strftime.c b/strftime.c index 9ea40fec..b23b6101 100644 --- a/strftime.c +++ b/strftime.c @@ -117,7 +117,7 @@ static char * _yconv(int, int, bool, bool, char *, char const *); #if HAVE_STRFTIME_L size_t strftime_l(char *s, size_t maxsize, char const *format, struct tm const *t, - locale_t locale) + ATTRIBUTE_MAYBE_UNUSED locale_t locale) { /* Just call strftime, as only the C locale is supported. */ return strftime(s, maxsize, format, t); diff --git a/zdump.c b/zdump.c index 89bcd25c..de1e1116 100644 --- a/zdump.c +++ b/zdump.c @@ -91,7 +91,7 @@ static bool errout; static char const *abbr(struct tm const *); static intmax_t delta(struct tm *, struct tm *) ATTRIBUTE_PURE; static void dumptime(struct tm const *); -static time_t hunt(timezone_t, char *, time_t, time_t, bool); +static time_t hunt(timezone_t, time_t, time_t, bool); static void show(timezone_t, char *, time_t, bool); static void showextrema(timezone_t, char *, time_t, struct tm *, time_t); static void showtrans(char const *, struct tm const *, time_t, char const *, @@ -627,7 +627,7 @@ main(int argc, char *argv[]) || (ab && (delta(&newtm, &tm) != newt - t || newtm.tm_isdst != tm.tm_isdst || strcmp(abbr(&newtm), ab) != 0))) { - newt = hunt(tz, argv[i], t, newt, false); + newt = hunt(tz, t, newt, false); newtmp = localtime_rz(tz, &newt, &newtm); newtm_ok = newtmp != NULL; if (iflag) @@ -707,7 +707,7 @@ yeartot(intmax_t y) return t; } -/* Search for a discontinuity in timezone TZ with name NAME, in the +/* Search for a discontinuity in timezone TZ, in the timestamps ranging from LOT through HIT. LOT and HIT disagree about some aspect of timezone. If ONLY_OK, search only for definedness changes, i.e., localtime succeeds on one side of the @@ -715,7 +715,7 @@ yeartot(intmax_t y) before the transition from LOT's settings. */ static time_t -hunt(timezone_t tz, char *name, time_t lot, time_t hit, bool only_ok) +hunt(timezone_t tz, time_t lot, time_t hit, bool only_ok) { static char * loab; static ptrdiff_t loabsize; @@ -807,7 +807,8 @@ adjusted_yday(struct tm const *a, struct tm const *b) my_gmtime_r and use its result instead of B. Otherwise, B is the possibly nonnull result of an earlier call to my_gmtime_r. */ static long -gmtoff(struct tm const *a, time_t *t, struct tm const *b) +gmtoff(struct tm const *a, ATTRIBUTE_MAYBE_UNUSED time_t *t, + ATTRIBUTE_MAYBE_UNUSED struct tm const *b) { #ifdef TM_GMTOFF return a->TM_GMTOFF; @@ -878,7 +879,7 @@ static void showextrema(timezone_t tz, char *zone, time_t lo, struct tm *lotmp, time_t hi) { struct tm localtm[2], gmtm[2]; - time_t t, boundary = hunt(tz, zone, lo, hi, true); + time_t t, boundary = hunt(tz, lo, hi, true); bool old = false; hi = (SECSPERDAY < hi - boundary ? boundary + SECSPERDAY diff --git a/zic.c b/zic.c index 8f8088ec..7a7a5094 100644 --- a/zic.c +++ b/zic.c @@ -1970,7 +1970,7 @@ inzsub(char **fields, int nfields, bool iscont) } static zic_t -getleapdatetime(char **fields, int nfields, bool expire_line) +getleapdatetime(char **fields, bool expire_line) { register const char * cp; register const struct lookup * lp; @@ -2048,7 +2048,7 @@ inleap(char **fields, int nfields) if (nfields != LEAP_FIELDS) error(_("wrong number of fields on Leap line")); else { - zic_t t = getleapdatetime(fields, nfields, false); + zic_t t = getleapdatetime(fields, false); if (0 <= t) { struct lookup const *lp = byword(fields[LP_ROLL], leap_types); if (!lp) @@ -2076,7 +2076,7 @@ inexpires(char **fields, int nfields) else if (0 <= leapexpires) error(_("multiple Expires lines")); else - leapexpires = getleapdatetime(fields, nfields, true); + leapexpires = getleapdatetime(fields, true); } static void -- 2.37.2
There's a minor inconsistency here with the Subject header, which says [[maybe_unused]], and the patch, which says [[__maybe_unused__]]. I only mention it because I was curious whether the underscores were best practice. (I've seen these C23 attributes before, but I don't yet understand how they work.) Google results seem to mostly not use the underscores. -- Russ Allbery (eagle@eyrie.org) <https://www.eyrie.org/~eagle/>
On 2022-11-20 12:47, Russ Allbery wrote:
I only mention it because I was curious whether the underscores were best practice.
I put the underscores in due to long habit with public include files. But they're not needed in private.h, so I installed the attached.
(I've seen these C23 attributes before, but I don't yet understand how they work.)
These standardize some of the attributes that have long been supported by GCC, but using a nicer syntax because the C standardizers weren't constrained to use a syntax that can be #defined to empty.
participants (2)
-
Paul Eggert -
Russ Allbery