Skip to content

Commit f76fe4a

Browse files
committed
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
2 parents b9b404f + 16f5d96 commit f76fe4a

File tree

6 files changed

+22
-1
lines changed

6 files changed

+22
-1
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,7 @@ LIB_OBJS += common-init.o
995995
LIB_OBJS += compat/nonblock.o
996996
LIB_OBJS += compat/obstack.o
997997
LIB_OBJS += compat/terminal.o
998+
LIB_OBJS += compiler-tricks/not-constant.o
998999
LIB_OBJS += config.o
9991000
LIB_OBJS += connect.o
10001001
LIB_OBJS += connected.o

compiler-tricks/not-constant.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include <git-compat-util.h>
2+
int false_but_the_compiler_does_not_know_it_;

config.mak.dev

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ DEVELOPER_CFLAGS += -Wunused
3939
DEVELOPER_CFLAGS += -Wvla
4040
DEVELOPER_CFLAGS += -Wwrite-strings
4141
DEVELOPER_CFLAGS += -fno-common
42+
DEVELOPER_CFLAGS += -Wunreachable-code
4243

4344
ifneq ($(filter clang4,$(COMPILER_FEATURES)),)
4445
DEVELOPER_CFLAGS += -Wtautological-constant-out-of-range-compare

git-compat-util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,4 +1583,13 @@ static inline void *container_of_or_null_offset(void *ptr, size_t offset)
15831583
((uintptr_t)&(ptr)->member - (uintptr_t)(ptr))
15841584
#endif /* !__GNUC__ */
15851585

1586+
/*
1587+
* Prevent an overly clever compiler from optimizing an expression
1588+
* out, triggering a false positive when building with the
1589+
* -Wunreachable-code option. false_but_the_compiler_does_not_know_it_
1590+
* is defined in a compilation unit separate from where the macro is
1591+
* used, initialized to 0, and never modified.
1592+
*/
1593+
#define NOT_CONSTANT(expr) ((expr) || false_but_the_compiler_does_not_know_it_)
1594+
extern int false_but_the_compiler_does_not_know_it_;
15861595
#endif

meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ libgit_sources = [
264264
'compat/nonblock.c',
265265
'compat/obstack.c',
266266
'compat/terminal.c',
267+
'compiler-tricks/not-constant.c',
267268
'config.c',
268269
'connect.c',
269270
'connected.c',
@@ -719,6 +720,7 @@ if get_option('warning_level') in ['2','3', 'everything'] and compiler.get_argum
719720
'-Woverflow',
720721
'-Wpointer-arith',
721722
'-Wstrict-prototypes',
723+
'-Wunreachable-code',
722724
'-Wunused',
723725
'-Wvla',
724726
'-Wwrite-strings',

run-command.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,13 @@ static void atfork_prepare(struct atfork_state *as)
515515
{
516516
sigset_t all;
517517

518-
if (sigfillset(&all))
518+
/*
519+
* POSIX says sigfillset() can fail, but an overly clever
520+
* compiler can see through the header files and decide
521+
* it cannot fail on a particular platform it is compiling for,
522+
* triggering -Wunreachable-code false positive.
523+
*/
524+
if (NOT_CONSTANT(sigfillset(&all)))
519525
die_errno("sigfillset");
520526
#ifdef NO_PTHREADS
521527
if (sigprocmask(SIG_SETMASK, &all, &as->old))

0 commit comments

Comments
 (0)