From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id AF23C3858D33; Fri, 20 Jan 2023 21:59:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AF23C3858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674251998; bh=6bPigz4Z6mWgo66QG56ovZsIEF+riGMex1bb+nt6/W4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Jlvkkv1yuju7B1tuMO6ebQWfvy9I7GsS1J23RrqoRm3m7j1cp0H0zMg6qbEmeGNVY 1RxU0VeIAuah57rNKD4wEXgn1isWHOGFTgRKzfzcUdwY6LVifunyjvSTj/lKCLBHuG 5gALcL8roAHjvQ58+FCAsf+zD28HEPENouYZcYlk= From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/108483] gcc warns about suspicious constructs for unevaluted ?: operand Date: Fri, 20 Jan 2023 21:59:58 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 10.2.1 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: enhancement X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D108483 Jakub Jelinek changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jakub at gcc dot gnu.org --- Comment #5 from Jakub Jelinek --- If you don't want to see the warning at all when using the macro, workaroun= d is easy, just use (0+sizeof(x)/sizeof(*(x)) and be done with it. If you want a warning if you use it say on int * type and no warning but 0 result if you use it on cv void * (note, the original one only works with void * and not with const void * etc.), you could use e= .g. following (verified both with gcc and clang), of course if you don't care about const void * etc., you can simplify it a little bit. //#define ARRAY_SIZE_MAYBENULL(x) ( __builtin_types_compatible_p(typeof(x), void*) ? 0 : (sizeof(x)/sizeof(*(x))) ) //#define ARRAY_SIZE_MAYBENULL(x) _Generic((x), void*: 0, const void*: 0, volatile void*: 0, const volatile void*: 0, default: (0+sizeof(x))/sizeof(*(x))) //#define ARRAY_SIZE_MAYBENULL(x) _Generic((x), void*: 0, const void*: 0, volatile void*: 0, const volatile void*: 0, default: sizeof(x)/sizeof(*(x))) #define ARRAY_SIZE_MAYBENULL(x) _Generic((x), void*: 0, const void*: 0, volatile void*: 0, const volatile void*: 0, default: \ sizeof(x)/sizeof(_Generic((x), void*: (x), const void*: (x), volatile voi= d*: (x), const volatile void*: (x), default:*(x)))) void foo (int *p) { int a[10]; p[0] =3D ARRAY_SIZE_MAYBENULL (a); p[1] =3D ARRAY_SIZE_MAYBENULL ((void *) 0); p[2] =3D ARRAY_SIZE_MAYBENULL ((const void *) 0); p[3] =3D ARRAY_SIZE_MAYBENULL ((volatile void *) 0); p[4] =3D ARRAY_SIZE_MAYBENULL ((void *restrict) 0); p[5] =3D ARRAY_SIZE_MAYBENULL ((const volatile void *) 0); p[6] =3D ARRAY_SIZE_MAYBENULL (p); }=