public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/108483] gcc warns about suspicious constructs for unevaluted ?: operand
Date: Sat, 21 Jan 2023 12:17:43 +0000	[thread overview]
Message-ID: <bug-108483-4-mpXufrUS9M@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-108483-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108483

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
If it needs to be used in constant expression and you want warnings for the
non-void * cases with pointers, not arrays and errors for use of void *
pointers except for NULL, then maybe:
typedef __SIZE_TYPE__ size_t;
typedef __UINTPTR_TYPE__ uintptr_t;
#define NULL ((void *) 0)
#define ARRAY_SIZE_MAYBENULL(x) _Generic((x), void*: (size_t) (uintptr_t)
((char (*) [__builtin_constant_p (x) && (x) == 0 ? 1 : _Generic((x), void*: -1,
default: 1)]) 0), default: \
  sizeof(x)/sizeof(_Generic((x), void*: (x), default:*(x))))

int a[10];
int *q;
void *r;
int p[] = {
ARRAY_SIZE_MAYBENULL (a),
ARRAY_SIZE_MAYBENULL (NULL),
ARRAY_SIZE_MAYBENULL ((const void *) 0),
ARRAY_SIZE_MAYBENULL ((volatile void *) 0),
ARRAY_SIZE_MAYBENULL ((void *restrict) 0),
ARRAY_SIZE_MAYBENULL ((const volatile void *) 0),
ARRAY_SIZE_MAYBENULL (q),
ARRAY_SIZE_MAYBENULL (r),
ARRAY_SIZE_MAYBENULL ((void *) (uintptr_t) 0x1234abc)
};

This warns -Wsizeof-pointer-div on the const void *, volatile void *, const
volatile void * and q cases and errors on the last 2.
clang seems to also emit those 4 -Wsizeof-pointer-div warnings, but doesn't
emit any errors nor warnings on the last 2.

      parent reply	other threads:[~2023-01-21 12:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-20 18:27 [Bug c/108483] New: " gcc-bugzilla at mkarcher dot dialup.fu-berlin.de
2023-01-20 20:01 ` [Bug c/108483] " pinskia at gcc dot gnu.org
2023-01-20 20:03 ` segher at gcc dot gnu.org
2023-01-20 20:15 ` gcc-bugzilla at mkarcher dot dialup.fu-berlin.de
2023-01-20 21:05 ` segher at gcc dot gnu.org
2023-01-20 21:59 ` jakub at gcc dot gnu.org
2023-01-21 11:12 ` jakub at gcc dot gnu.org
2023-01-21 12:17 ` jakub at gcc dot gnu.org [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-108483-4-mpXufrUS9M@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).