Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions common/common-nut_version.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,7 @@ const char *describe_NUT_VERSION_once(void)

memset(buf, 0, sizeof(buf));

#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#endif
#include "nut-pragmas-unreachable-code.h"
/* NOTE: Some compilers deduce that macro-based decisions about
* NUT_VERSION_IS_RELEASE make one of codepaths unreachable in
* a particular build. So we pragmatically handwave this away.
Expand Down Expand Up @@ -186,12 +177,7 @@ const char *describe_NUT_VERSION_once(void)
printed = UPS_VERSION;
}

#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic pop
#endif
#include "nut-pragmas-unreachable-code-end.h"

return printed;
}
Expand Down
36 changes: 4 additions & 32 deletions common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,16 +586,7 @@ uint64_t upsnotify_extend_timeout_usec_default = 600 * 1000000,

pid_t get_max_pid_t(void)
{
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#endif
#include "nut-pragmas-unreachable-code.h"
if (sizeof(pid_t) == sizeof(short)) return (pid_t)SHRT_MAX;
if (sizeof(pid_t) == sizeof(int)) return (pid_t)INT_MAX;
if (sizeof(pid_t) == sizeof(long)) return (pid_t)LONG_MAX;
Expand All @@ -605,12 +596,7 @@ pid_t get_max_pid_t(void)
# endif
#endif
abort();
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic pop
#endif
#include "nut-pragmas-unreachable-code-end.h"
}

void check_perms(const char *fn)
Expand Down Expand Up @@ -2881,16 +2867,7 @@ char *xbasename_no_ext(const char *file)
#endif

/* Some compilers detect that conditions are not changing at run-time: */
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#endif
#include "nut-pragmas-unreachable-code.h"

if (!exeext) {
#ifdef EXEEXT
Expand Down Expand Up @@ -2927,12 +2904,7 @@ char *xbasename_no_ext(const char *file)
if (!bn) {
bn = xstrdup(cs);
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic pop
#endif
#include "nut-pragmas-unreachable-code-end.h"

return bn;
}
Expand Down
7 changes: 7 additions & 0 deletions docs/developers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,13 @@ end a quiesced block of source code, e.g.:
}
----

The `nut-pragmas-unreachable-code.h` and
`nut-pragmas-unreachable-code-end.h` pair suppresses `-Wunreachable-code`
using the existing aggregate GCC capability guard and Clang pragmas.
These private headers can be included repeatedly. Keep the pair
at the original diagnostic boundaries; regions with different guards or
additional warnings need their own pragmas.


Switch case fall-through
~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
18 changes: 2 additions & 16 deletions drivers/bcmxcp_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +170,10 @@ ssize_t get_answer(unsigned char *data, unsigned char command)
gettimeofday(&start_time, NULL);
memset(&buf, 0x0, PW_CMD_BUFSIZE);

#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#endif
#include "nut-pragmas-unreachable-code.h"
/* Stay ahead of possible redefinitions... */
assert (XCP_USB_TIMEOUT < INT_MAX);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic pop
#endif
#include "nut-pragmas-unreachable-code-end.h"

while ( (!endblock) && ((XCP_USB_TIMEOUT - elapsed_time) > 0) ) {

Expand Down
18 changes: 2 additions & 16 deletions drivers/bestfcom.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,28 +258,14 @@ void upsdrv_updateinfo(void)

if (! fc.valid) {
upsdebugx(1, "upsupdate run before ups_ident() read ups config");
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#endif
#include "nut-pragmas-unreachable-code.h"
/* NOTE: This assert() always fails because of "0":
* error: will never be executed [-Werror,-Wunreachable-code]
* ((0) ? (void) (0) : __assert_fail ("0", "bestfcom.c", 254, __PRETTY_FUNCTION__));
* ^
*/
assert(0);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic pop
#endif
#include "nut-pragmas-unreachable-code-end.h"
}

if (execute("f\r", fstring, sizeof(fstring)) >= 80) {
Expand Down
18 changes: 2 additions & 16 deletions drivers/bestuferrups.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,28 +233,14 @@ void upsdrv_updateinfo(void)
if (! fc.valid) {
fprintf(stderr,
"upsupdate run before ups_ident() read ups config\n");
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#endif
#include "nut-pragmas-unreachable-code.h"
/* NOTE: This assert() always fails because of "0":
* error: will never be executed [-Werror,-Wunreachable-code]
* ((0) ? (void) (0) : __assert_fail ("0", "bestuferrups.c", 138, __PRETTY_FUNCTION__));
* ^
*/
assert(0);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic pop
#endif
#include "nut-pragmas-unreachable-code-end.h"
}

if (execute("f\r", fstring, sizeof(fstring)) > 0) {
Expand Down
18 changes: 2 additions & 16 deletions drivers/genericups.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,24 +394,10 @@ void upsdrv_shutdown(void)
sdtime);

if (sdtime > 0) {
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#endif
#include "nut-pragmas-unreachable-code.h"
/* Different platforms, different sizes, none fits all... */
if (sizeof(long) > sizeof(unsigned int) && sdtime < (long)UINT_MAX) {
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic pop
#endif
#include "nut-pragmas-unreachable-code-end.h"
sleep((unsigned int)sdtime);
} else {
sleep(UINT_MAX);
Expand Down
18 changes: 2 additions & 16 deletions drivers/nut-libfreeipmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,16 +528,7 @@ static int libfreeipmi_get_board_info (const void *areabuf,

/* Without a standard TIME_MAX, signedness may suffer;
* but we can at least check the number should fit */
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#endif
#include "nut-pragmas-unreachable-code.h"
/* Stay ahead of possible redefinitions... */
if (sizeof(mfg_date_time) > sizeof(timetmp))
{
Expand All @@ -550,12 +541,7 @@ static int libfreeipmi_get_board_info (const void *areabuf,
* should expose that so we look for a fix - so do not just blindly
* move the closing pragmas to end of method ;)
*/
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic pop
#endif
#include "nut-pragmas-unreachable-code-end.h"

timetmp = (time_t)mfg_date_time;
localtime_r (&timetmp, &mfg_date_time_tm);
Expand Down
2 changes: 2 additions & 0 deletions include/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ dist_noinst_HEADERS = \
nut_bool.h nut_float.h nut_stdint.h nut_platform.h \
nut-pragmas-covered-switch-default.h \
nut-pragmas-covered-switch-default-end.h \
nut-pragmas-unreachable-code.h \
nut-pragmas-unreachable-code-end.h \
strcasestr-static.h wincompat.h

# Optionally deliverable as part of NUT public API:
Expand Down
28 changes: 28 additions & 0 deletions include/nut-pragmas-unreachable-code-end.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* nut-pragmas-unreachable-code-end.h - restore diagnostic warnings

Copyright (C) 2020-2026 Jim Klimov <jimklimov+nut@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

/* This file is intentionally repeat-includable so one translation unit can
* bracket several unreachable code regions. */

#ifdef __clang__
# pragma clang diagnostic pop
#endif
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
# pragma GCC diagnostic pop
#endif
32 changes: 32 additions & 0 deletions include/nut-pragmas-unreachable-code.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* nut-pragmas-unreachable-code.h - quiesce unreachable code warnings

Copyright (C) 2020-2026 Jim Klimov <jimklimov+nut@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

/* This file is intentionally repeat-includable so one translation unit can
* bracket several unreachable code regions. */

#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
# pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
# pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunreachable-code"
#endif
18 changes: 2 additions & 16 deletions server/netget.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,7 @@ static void get_type(nut_ctype_t *client, const char *upsname, const char *var)

static void get_var_server(nut_ctype_t *client, const char *upsname, const char *var)
{
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#endif
#include "nut-pragmas-unreachable-code.h"
int pkgurlHasNutOrg = PACKAGE_URL ? (strstr(PACKAGE_URL, "networkupstools.org") != NULL) : 0;

if (!strcasecmp(var, "server.info")) {
Expand All @@ -212,12 +203,7 @@ static void get_var_server(nut_ctype_t *client, const char *upsname, const char
);
return;
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic pop
#endif
#include "nut-pragmas-unreachable-code-end.h"

if (!strcasecmp(var, "server.version")) {
sendback(client, "VAR %s server.version \"%s\"\n",
Expand Down
18 changes: 2 additions & 16 deletions server/netmisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,7 @@ void net_ver(nut_ctype_t *client, size_t numarg, const char **arg)
return;
}

#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#endif
#include "nut-pragmas-unreachable-code.h"
if (PACKAGE_URL && strstr(PACKAGE_URL, "networkupstools.org")) {
pkgurlHasNutOrg = 1;
} else {
Expand All @@ -65,12 +56,7 @@ void net_ver(nut_ctype_t *client, size_t numarg, const char **arg)
(PACKAGE_URL && !pkgurlHasNutOrg) ? " or " : "",
pkgurlHasNutOrg ? "" : "https://www.networkupstools.org/"
);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic pop
#endif
#include "nut-pragmas-unreachable-code-end.h"
}

void net_netver(nut_ctype_t *client, size_t numarg, const char **arg)
Expand Down
Loading
Loading