aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2025-03-29 16:39:10 +0900
committerJunio C Hamano <gitster@pobox.com>2025-03-29 16:39:10 +0900
commitf76fe4ab067b9d6e530edf8205b5672ebad27bfc (patch)
tree7d8e5a823e75ee64c35b839fceedd9cbeb5074c5
parentb9b404fa1cdb43a54962188d883fafb5d2f9912d (diff)
parent16f5d967e2267ea37cd29d7aee21994921b6b98c (diff)
downloadgit-f76fe4ab067b9d6e530edf8205b5672ebad27bfc.tar.gz
Merge branch 'jk/use-wunreachable-code-for-devs'
Enable -Wunreachable-code for developer builds. * jk/use-wunreachable-code-for-devs: config.mak.dev: enable -Wunreachable-code git-compat-util: add NOT_CONSTANT macro and use it in atfork_prepare() run-command: use errno to check for sigfillset() error
-rw-r--r--Makefile1
-rw-r--r--compiler-tricks/not-constant.c2
-rw-r--r--config.mak.dev1
-rw-r--r--git-compat-util.h9
-rw-r--r--meson.build2
-rw-r--r--run-command.c8
6 files changed, 22 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index efd8215c04..ac32d2d0bd 100644
--- a/Makefile
+++ b/Makefile
@@ -995,6 +995,7 @@ LIB_OBJS += common-init.o
LIB_OBJS += compat/nonblock.o
LIB_OBJS += compat/obstack.o
LIB_OBJS += compat/terminal.o
+LIB_OBJS += compiler-tricks/not-constant.o
LIB_OBJS += config.o
LIB_OBJS += connect.o
LIB_OBJS += connected.o
diff --git a/compiler-tricks/not-constant.c b/compiler-tricks/not-constant.c
new file mode 100644
index 0000000000..1da3ffc2f5
--- /dev/null
+++ b/compiler-tricks/not-constant.c
@@ -0,0 +1,2 @@
+#include <git-compat-util.h>
+int false_but_the_compiler_does_not_know_it_;
diff --git a/config.mak.dev b/config.mak.dev
index 0fd8cc4d35..95b7bc46ae 100644
--- a/config.mak.dev
+++ b/config.mak.dev
@@ -39,6 +39,7 @@ DEVELOPER_CFLAGS += -Wunused
DEVELOPER_CFLAGS += -Wvla
DEVELOPER_CFLAGS += -Wwrite-strings
DEVELOPER_CFLAGS += -fno-common
+DEVELOPER_CFLAGS += -Wunreachable-code
ifneq ($(filter clang4,$(COMPILER_FEATURES)),)
DEVELOPER_CFLAGS += -Wtautological-constant-out-of-range-compare
diff --git a/git-compat-util.h b/git-compat-util.h
index e123288e8f..cf733b38ac 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -1583,4 +1583,13 @@ static inline void *container_of_or_null_offset(void *ptr, size_t offset)
((uintptr_t)&(ptr)->member - (uintptr_t)(ptr))
#endif /* !__GNUC__ */
+/*
+ * Prevent an overly clever compiler from optimizing an expression
+ * out, triggering a false positive when building with the
+ * -Wunreachable-code option. false_but_the_compiler_does_not_know_it_
+ * is defined in a compilation unit separate from where the macro is
+ * used, initialized to 0, and never modified.
+ */
+#define NOT_CONSTANT(expr) ((expr) || false_but_the_compiler_does_not_know_it_)
+extern int false_but_the_compiler_does_not_know_it_;
#endif
diff --git a/meson.build b/meson.build
index 0902e12189..33d92c066c 100644
--- a/meson.build
+++ b/meson.build
@@ -264,6 +264,7 @@ libgit_sources = [
'compat/nonblock.c',
'compat/obstack.c',
'compat/terminal.c',
+ 'compiler-tricks/not-constant.c',
'config.c',
'connect.c',
'connected.c',
@@ -719,6 +720,7 @@ if get_option('warning_level') in ['2','3', 'everything'] and compiler.get_argum
'-Woverflow',
'-Wpointer-arith',
'-Wstrict-prototypes',
+ '-Wunreachable-code',
'-Wunused',
'-Wvla',
'-Wwrite-strings',
diff --git a/run-command.c b/run-command.c
index 402138b8b5..8833b23367 100644
--- a/run-command.c
+++ b/run-command.c
@@ -515,7 +515,13 @@ static void atfork_prepare(struct atfork_state *as)
{
sigset_t all;
- if (sigfillset(&all))
+ /*
+ * POSIX says sigfillset() can fail, but an overly clever
+ * compiler can see through the header files and decide
+ * it cannot fail on a particular platform it is compiling for,
+ * triggering -Wunreachable-code false positive.
+ */
+ if (NOT_CONSTANT(sigfillset(&all)))
die_errno("sigfillset");
#ifdef NO_PTHREADS
if (sigprocmask(SIG_SETMASK, &all, &as->old))