[Bug c/103887] New: -fsanitize=shift affects constness of an expression
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103887 Bug ID: 103887 Summary: -fsanitize=shift affects constness of an expression Product: gcc Version: 11.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: m...@mk-sys.cz Target Milestone: --- This simplified testcase --- #include #define ASSERT_ON_COMPILE_SELECTOR_SIZE(e)\ _Static_assert(((__builtin_constant_p(e) && ((e) >> 16) == 0)) || \ (sizeof(e) <= 2), "problem") int f(uint16_t tr) { ASSERT_ON_COMPILE_SELECTOR_SIZE(tr); return 0; } --- builds cleanly with "-O2" but fails with "-O2 -fsanitize=shift": --- mike@lion:/tmp/gcc> gcc-11 -Wall -Wextra -O2 -c foo.c mike@lion:/tmp/gcc> gcc-11 -Wall -Wextra -O2 -fsanitize=shift -c foo.c foo.c: In function ‘f’: foo.c:4:72: error: expression in static assertion is not constant 4 | _Static_assert(((__builtin_constant_p(e) && ((e) >> 16) == 0)) || \ | ^~~~ 5 |(sizeof(e) <= 2), "problem") | foo.c:9:9: note: in expansion of macro ‘ASSERT_ON_COMPILE_SELECTOR_SIZE’ 9 | ASSERT_ON_COMPILE_SELECTOR_SIZE(tr); | ^~~ --- I was able to reproduce the same with various gcc version from gcc7 to gcc12.
[Bug c/103887] -fsanitize=shift affects constness of an expression
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103887 --- Comment #2 from Michal Kubecek --- (In reply to Andrew Pinski from comment #1) > The problem is rather __builtin_constant_p not resolving to a constant. This > is a dup of bug 79482. There is a difference: your modified testcase fails to compile regardless of -fsanitize=shift while mine fails only with this option and succeeds without it. If __builtin_constant_p(tr) not being handled as constant were the problem, the result should not depend on presence of "-fsanitize=shift".
[Bug c/103887] -fsanitize=shift affects constness of an expression
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103887 --- Comment #4 from Michal Kubecek --- It's probably even more complicated as #define ASSERT_ON_COMPILE_SELECTOR_SIZE(expr) \ _Static_assert( \ __builtin_choose_expr(__builtin_constant_p(expr), \ ((expr) >> 16) == 0, \ sizeof(expr) <= 2), \ "problem") works as expected (tested with "uint16_t tr", "uint32_t tr", 6 and 7) even if the documentation says first argument of __builtin_choose_expr() has to be a constant expression. As this is nicer than the original code, I guess I'll use it and hope it is not just a happy coincidence that it works.