public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Sandiford <richard.sandiford@arm.com>
To: David Aldrich <david.aldrich.ntml@gmail.com>
Cc: gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: gcc -Wconversion
Date: Tue, 06 Aug 2019 18:10:00 -0000	[thread overview]
Message-ID: <mpt8ss688s6.fsf@arm.com> (raw)
In-Reply-To: <CAJK_iejZfKNWfQviFTN5JOzg3pHGB7HMpyYST5RB-z5bsPEL3A@mail.gmail.com>	(David Aldrich's message of "Tue, 6 Aug 2019 16:55:35 +0100")

David Aldrich <david.aldrich.ntml@gmail.com> writes:
> Hi
>
> For our large 64-bit C++ project we use gcc compile options: -Wall -pedantic
>
> We also build the project with Visual C++ and get lots of instances of
> warning C4267, for example:
>
> 'initializing': conversion from 'size_t' to 'unsigned int', possible loss
> of data
>
> To get a similar warning with gcc it seems that I need -Wconversion.
> However, that warning option seems very strict. For example with this code:
>
> typedef struct
> {
>     uint8 x : 4,
>              y  : 4;
> } myHdr;
>
> unsigned X=1;
> myHdr hdr;
>
> hdr.x = static_cast<uint8_t>(X);
>
> I get warning:
>
> warning: conversion to ‘unsigned char:4’ from ‘uint8_t {aka unsigned char}’
> may alter its value [-Wconversion]

Yeah, it's a long-standing wart that the warning can't be disabled
for bitfields even when, like here, there's an explicit cast to the
underlying type (which is as close as an explicit cast can be).  See:

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

(warning: some of the initial responses were somewhat tetchy)

clang's -Wconversion doesn't warn for this case either, so it looks
like GCC is being stricter than both clang and Visual C++.

> I have two questions:
>
> 1) What static_cast would I use to fix the above example warning?

I don't think there is one, but you can use:

   hdr.x = X & 0xf;

to make the truncation explicit.  Neither GCC nor clang warn then.

I realise that might not be particularly desirable though.

> 2) Is -Wconversion recommended or is it too fussy in practice? Is there a
> better option?

-Wconversion is the right option to use.  Unfortunately there's
not yet any way of disabling or relaxing the warning for bitfields,
but the PR above is tracking that as a future enhancement.

Thanks,
Richard

  reply	other threads:[~2019-08-06 18:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-06 15:55 David Aldrich
2019-08-06 18:10 ` Richard Sandiford [this message]
2019-08-07 15:40   ` David Aldrich

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=mpt8ss688s6.fsf@arm.com \
    --to=richard.sandiford@arm.com \
    --cc=david.aldrich.ntml@gmail.com \
    --cc=gcc-help@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).