[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [XEN PATCH v3] xen/include: avoid using a compiler extension for BUILD_BUG_ON_ZERO.


  • To: Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Thu, 22 Jun 2023 14:41:33 +0200
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=0s2bwkJebguGX04knwEod3khKYd/2r92OdeVJeu1nwM=; b=GXP2xkiVV8xp5oBLTnOgmi7Iiw2CdQy8zOoBf9jci8LlpxJ7snECxgtOUJRkOe66BGio8reA9S+pkdTok19WMdokgu4YFtYmwiBpM/FAZsd0/7EDYBwVE8JjQ+GmVtTaMi18g/sXe2sn6E2eYK114p1GJvW6PiS8K/9KwaJky56Qh5564PaiBllARFxSBi4TikMIlZoIVweCwgtqUiqHtv7ff+m9Unki8tuWqmqD6bRimxsipHK39PdEmjB0Htrto3aXXL0D032NJHHdJDjjBAzuc4dYPyapZUb6/D+p8Tltynszb2DMRtW6hStAWk8VO0pruY+QA9fUFIFC5Bf64A==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aY9sOxatgPSCWIaf1DLJCFWn9pkDy9gHhOVCcvYxWbjbK0uEuPpOUADfL5u6DBQKdGRlcvCQL+joF9xrVcbDLV1lqCT/U8FolKr3NbdvMU7Al1hmooZZ0QJax7rbLrk46gFLydAZwFpebM7sONXZzyjmrhcA3Kfhj+q57guEKetnvia2sE9jcccSFUQ8beAmtJm6C1dSXJiaLGXGLw5uUFD8+eKtLXw9eHfzjV57c8axPwnVqf4AwwvFT8koWpOeVPeCZVJDQn7i2B1qpBCsZrarxqI6QfLx2lVfLgrFiqWshN5WjnCfdU3/p+LTunc60PJ87aznIIjhi3BtDBpASw==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com;
  • Cc: sstabellini@xxxxxxxxxx, michal.orzel@xxxxxxx, xenia.ragiadakou@xxxxxxx, ayan.kumar.halder@xxxxxxx, consulting@xxxxxxxxxxx, andrew.cooper3@xxxxxxxxxx, roger.pau@xxxxxxxxxx, bertrand.marquis@xxxxxxx, julien@xxxxxxx, George Dunlap <george.dunlap@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • Delivery-date: Thu, 22 Jun 2023 12:41:56 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 22.06.2023 11:34, Nicola Vetrini wrote:
> 
> 
> On 22/06/23 10:56, Jan Beulich wrote:
>> On 22.06.2023 10:24, Nicola Vetrini wrote:
>>> --- a/xen/include/xen/lib.h
>>> +++ b/xen/include/xen/lib.h
>>> @@ -51,9 +51,10 @@
>>>      e.g. in a structure initializer (or where-ever else comma expressions
>>>      aren't permitted). */
>>>   #define BUILD_BUG_ON_ZERO(cond) \
>>> -    sizeof(struct { _Static_assert(!(cond), "!(" #cond ")"); })
>>> +    (sizeof(struct { char c; _Static_assert(!(cond), "!(" #cond ")"); }) & 
>>> 0U)
>>>   #else
>>> -#define BUILD_BUG_ON_ZERO(cond) sizeof(struct { int:-!!(cond); })
>>> +#define BUILD_BUG_ON_ZERO(cond) \
>>> +    (sizeof(struct { unsigned u : (cond) ? -1 : 1; }) & 0U)
>>
>> Given your remark on named bitfields not being allowed to be zero length
>> (which hopefully holds universally, i.e. isn't subject to compiler
>> extensions), how about
>>
>> #define BUILD_BUG_ON_ZERO(cond) (sizeof(struct { int _:!(cond); }) & 0)
> 
> Well, the reason for the change is to drop any assumption on 
> compiler-specific quirks that may arise (it would be a surprise if this 
> wasn't the case, though)

And the reason for my suggestion was that it's shorter, and typically I
view "shorter" as also "easier to read/follow".

>> ? (Implicitly, as said before, I question the value of the U suffixes here.
>> Even better might be to make sure the expression is explicitly not of
>> unsigned type, to avoid surprises if someone used ~BUILD_BUG_ON_ZERO()
>> somewhere.)
> 
> The documentation for the macro definition states that the expression 
> must have value 0 and type size_t when cond is false. If I understand 
> correctly what you're saying here, then this is not allowed by the 
> documentation.

Well, it's the comment that says this, yes. Question is whether the
comment mandates the behavior or merely describes the current
implementation. Imo it's the latter ... But anyway, switching the
result type is independent of the present goal.

Jan



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.