From patchwork Thu Mar 16 21:04:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Pfaff X-Patchwork-Id: 739988 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vkgw66Tzqz9ryQ for ; Fri, 17 Mar 2017 08:04:54 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 38B5DC19; Thu, 16 Mar 2017 21:04:53 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 847C4C0C for ; Thu, 16 Mar 2017 21:04:52 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id DA1C9127 for ; Thu, 16 Mar 2017 21:04:51 +0000 (UTC) Received: from mfilter11-d.gandi.net (mfilter11-d.gandi.net [217.70.178.131]) by relay4-d.mail.gandi.net (Postfix) with ESMTP id A5F211720AF; Thu, 16 Mar 2017 22:04:50 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mfilter11-d.gandi.net Received: from relay4-d.mail.gandi.net ([IPv6:::ffff:217.70.183.196]) by mfilter11-d.gandi.net (mfilter11-d.gandi.net [::ffff:10.0.15.180]) (amavisd-new, port 10024) with ESMTP id kDxMtceGODGD; Thu, 16 Mar 2017 22:04:49 +0100 (CET) X-Originating-IP: 208.91.2.3 Received: from sigabrt.benpfaff.org (unknown [208.91.2.3]) (Authenticated sender: blp@ovn.org) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id 3CC9D17209B; Thu, 16 Mar 2017 22:04:47 +0100 (CET) From: Ben Pfaff To: dev@openvswitch.org Date: Thu, 16 Mar 2017 14:04:41 -0700 Message-Id: <20170316210441.7138-1-blp@ovn.org> X-Mailer: git-send-email 2.10.2 X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Ben Pfaff Subject: [ovs-dev] [PATCH] compiler: Use C11 build assertions with new enough GCC or Clang. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org Until now, the BUILD_ASSERT and BUILD_ASSERT_DECL macros have used OVS's home-grown build assertion strategy. This commit switches them to using C11 build assertions with compilers that support them. The semantics are the same, but C11 build assertions yield clearer error messages when they fail. This commit also reorders the definitions a bit to make it easier to follow. Signed-off-by: Ben Pfaff Acked-by: Jarno Rajahalme --- include/openvswitch/compiler.h | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/include/openvswitch/compiler.h b/include/openvswitch/compiler.h index 6e779f38fd16..0dc8636add33 100644 --- a/include/openvswitch/compiler.h +++ b/include/openvswitch/compiler.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Nicira, Inc. + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016, 2017 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -236,26 +236,28 @@ #define OVS_PREFETCH_WRITE(addr) #endif -/* Build assertions. */ +/* Build assertions. + * + * Use BUILD_ASSERT_DECL as a declaration or a statement, or BUILD_ASSERT as + * part of an expression. */ #ifdef __CHECKER__ #define BUILD_ASSERT(EXPR) ((void) 0) #define BUILD_ASSERT_DECL(EXPR) extern int (*build_assert(void))[1] -#elif !defined(__cplusplus) -/* Build-time assertion building block. */ +#elif defined(__cplusplus) +#include +#define BUILD_ASSERT BOOST_STATIC_ASSERT +#define BUILD_ASSERT_DECL BOOST_STATIC_ASSERT +#elif (__GNUC__ * 256 + __GNUC_MINOR__ >= 0x403 \ + || __has_extension(c_static_assert)) +#define BUILD_ASSERT_DECL(EXPR) _Static_assert(EXPR, #EXPR) +#define BUILD_ASSERT(EXPR) (void) ({ _Static_assert(EXPR, #EXPR); }) +#else #define BUILD_ASSERT__(EXPR) \ sizeof(struct { unsigned int build_assert_failed : (EXPR) ? 1 : -1; }) - -/* Build-time assertion for use in a statement context. */ #define BUILD_ASSERT(EXPR) (void) BUILD_ASSERT__(EXPR) - -/* Build-time assertion for use in a declaration context. */ #define BUILD_ASSERT_DECL(EXPR) \ extern int (*build_assert(void))[BUILD_ASSERT__(EXPR)] -#else /* __cplusplus */ -#include -#define BUILD_ASSERT BOOST_STATIC_ASSERT -#define BUILD_ASSERT_DECL BOOST_STATIC_ASSERT -#endif /* __cplusplus */ +#endif #ifdef __GNUC__ #define BUILD_ASSERT_GCCONLY(EXPR) BUILD_ASSERT(EXPR)