From 1529b5221389c75371d0f3f181957a77a158a53c Mon Sep 17 00:00:00 2001 From: lemon Date: Mon, 9 Mar 2026 10:24:39 +0100 Subject: c: relax constexpr constraints, fix alignof - Allow short-circuiting of constant logical expressions where the unevaluated operand is not a constant expression (`1 || 0/0`) - Allow constant integer expressions that evaluate to zero to be used as null pointer constants (GNU extension). + According to the standard, `int *x = 5*0;` should be rejected. But compilers evaluate `5*0 -> 0` and allow it as if a null pointer literal. --- test/17-misc.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'test') diff --git a/test/17-misc.c b/test/17-misc.c index ecff06c..521b948 100644 --- a/test/17-misc.c +++ b/test/17-misc.c @@ -13,6 +13,19 @@ int fn1(uvlong p_9) { return q; } +/* Excerpt from linux/kernel.h */ +/* + * This returns a constant expression while determining if an argument is + * a constant expression, most importantly without evaluating the argument. + * Glory to Martin Uecker + */ +#define __is_constexpr(x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) + +_Static_assert(__is_constexpr(5 &&(1<<3) || 0/0), ""); +_Static_assert(!__is_constexpr(5/0), ""); +_Static_assert(!__is_constexpr(fn1(0)), ""); + extern int printf(const char *, ...); int main() { printf("%d\n", fn1(-77ull)); -- cgit v1.2.3