From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 42121 invoked by alias); 6 Aug 2019 18:10:22 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 42112 invoked by uid 89); 6 Aug 2019 18:10:22 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-3.6 required=5.0 tests=AWL,BAYES_00,KAM_SHORT,SPF_PASS autolearn=ham version=3.3.1 spammy=aldrich, U*david.aldrich.ntml, Aldrich, davidaldrichntmlgmailcom X-HELO: foss.arm.com Received: from foss.arm.com (HELO foss.arm.com) (217.140.110.172) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 06 Aug 2019 18:10:21 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9EF7928; Tue, 6 Aug 2019 11:10:19 -0700 (PDT) Received: from localhost (e121540-lin.manchester.arm.com [10.32.99.62]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 29E983F575; Tue, 6 Aug 2019 11:10:19 -0700 (PDT) From: Richard Sandiford To: David Aldrich Mail-Followup-To: David Aldrich ,gcc-help , richard.sandiford@arm.com Cc: gcc-help Subject: Re: gcc -Wconversion References: Date: Tue, 06 Aug 2019 18:10:00 -0000 In-Reply-To: (David Aldrich's message of "Tue, 6 Aug 2019 16:55:35 +0100") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes X-SW-Source: 2019-08/txt/msg00023.txt.bz2 David Aldrich writes: > Hi > > For our large 64-bit C++ project we use gcc compile options: -Wall -pedan= tic > > 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 cod= e: > > typedef struct > { > uint8 x : 4, > y : 4; > } myHdr; > > unsigned X=3D1; > myHdr hdr; > > hdr.x =3D static_cast(X); > > I get warning: > > warning: conversion to =E2=80=98unsigned char:4=E2=80=99 from =E2=80=98ui= nt8_t {aka unsigned char}=E2=80=99 > 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=3D39170 (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 =3D 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