concurrencykit / ck

Concurrency primitives, safe memory reclamation mechanisms and non-blocking (including lock-free) data structures designed to aid in the research, design and implementation of high performance concurrent systems developed in C99+.
http://concurrencykit.org/
Other
2.34k stars 312 forks source link

Compilation fails with -Werror=strict-aliasing #200

Open ConiKost opened 1 year ago

ConiKost commented 1 year ago

Compilation fails with -Werror=strict-aliasing.

/usr/bin/x86_64-pc-linux-gnu-gcc -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_DEFAULT_SOURCE -std=gnu99 -pedantic -Wall -W -Wundef -Wendif-labels -Wshadow -Wpointer-arith -Wcast-align -Wcast-qual -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Winline -Wdisabled-optimization -fstrict-aliasing -O2 -pipe -Wno-parentheses -O2 -pipe -march=x86-64 -frecord-gcc-switches -fno-diagnostics-color -fmessage-length=0 -flto -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing -fPIC -I../../../include -DCORES=48 -m64 -D__x86_64__ -D_GNU_SOURCE -o ck_pr_bin ck_pr_bin.c
In file included from ../../common.h:31,
                 from ck_pr_store.c:27:
ck_pr_store.c: In function ‘rg_width’:
ck_pr_store.c:70:33: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wstrict-aliasing]]
   70 |                 ck_pr_store_##w((uint##w##_t *)(void *)&f, j);                  \
../../../include/ck_pr.h:182:25: note: in definition of macro ‘CK_PR_STORE_SAFE’
  182 |         ((void)sizeof(*(DST) = (VAL)), (DST)),                  \
      |                         ^~~
ck_pr_store.c:70:17: note: in expansion of macro ‘ck_pr_store_32’
   70 |                 ck_pr_store_##w((uint##w##_t *)(void *)&f, j);                  \
      |                 ^~~~~~~~~~~~
ck_pr_store.c:89:17: note: in expansion of macro ‘CK_PR_STORE_W’
   89 |                 CK_PR_STORE_W(64, 32);
      |                 ^~~~~~~~~~~~~
ck_pr_store.c:70:33: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wstrict-aliasing]]
   70 |                 ck_pr_store_##w((uint##w##_t *)(void *)&f, j);                  \
../../../include/ck_pr.h:182:25: note: in definition of macro ‘CK_PR_STORE_SAFE’
  182 |         ((void)sizeof(*(DST) = (VAL)), (DST)),                  \
      |                         ^~~
ck_pr_store.c:70:17: note: in expansion of macro ‘ck_pr_store_16’
   70 |                 ck_pr_store_##w((uint##w##_t *)(void *)&f, j);                  \
      |                 ^~~~~~~~~~~~
ck_pr_store.c:103:17: note: in expansion of macro ‘CK_PR_STORE_W’
  103 |                 CK_PR_STORE_W(32, 16);
      |                 ^~~~~~~~~~~~~
cc1: some warnings being treated as errors
michael-grunder commented 1 year ago

I can't replicate this. What version of GCC are you using? It compiles without warning for me using gcc-10, gcc-11, and gcc-13.

$ uname -a
Linux cthulhu 5.15.0-52-generic #58-Ubuntu SMP Thu Oct 13 08:03:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
ConiKost commented 1 year ago

Did you add -Werror=strict-aliasing to your CFLAGS? I am using GCC-13.

Linux Galactica 6.0.7-gentoo #1 SMP PREEMPT_DYNAMIC Sun Nov 6 14:26:32 CET 2022 x86_64 GNU/Linux

michael-grunder commented 1 year ago

Apologies, I can replicate it.

It didn't throw an error when I tried compiling the single file.

gcc -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_DEFAULT_SOURCE -std=gnu99 -pedantic -Wall -W -Wundef -Wendif-labels -Wshadow -Wpointer-arith -Wcast-align -Wcast-qual -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Winline -Wdisabled-optimization -fstrict-aliasing -O2 -pipe -Wno-parentheses -O2 -pipe -march=x86-64 -frecord-gcc-switches -fno-diagnostics-color -fmessage-length=0 -flto -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing -fPIC -I../../../include -DCORES=48 -m64 -D__x86_64__ -D_GNU_SOURCE -o ck_pr_bin ck_pr_bin.c

But doing:

CFLAGS="-Wstrict-aliasing -Werror=strict-aliasing" ./configure && make regressions

does reproduce the same error for me.

Edit: If it's helpful it's also possible to get the warning but still compile the code with -Wstrict-aliasing=3