Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3314492pxu; Tue, 8 Dec 2020 08:51:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJxX/71ro4ZAoj6Yxrw/UXESEec3JAshs6ZFFx0LeYolhFCxzaMQQQ6MF2Lu6v1bP6xdDnoY X-Received: by 2002:a17:907:6e6:: with SMTP id yh6mr24352831ejb.512.1607446301916; Tue, 08 Dec 2020 08:51:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607446301; cv=none; d=google.com; s=arc-20160816; b=BsAmT19qiAyLYMlY0tvs6PVgYf1Ki+91OOKJPSc1poU8h7bwbcm0sdn2cJl8WfW5jH YuQdFdeRogHDVgSlZSckx8Ht1ajxCUQqTnNjb0FQYofYrmuK2yQTQ5HnEAADMMPRzy9s 0GrUSS7T7xxsmkUVl+c270qvbeUVcHaZOrdQX5x9QMtE9ws3uepAB3CIXtfnB7vYLXUN QG9CSOWTUX7yRAnXQ/1D0azYIvxo8M7qwrI84CtNN29YiRBg7S6GMLa0JgPmjMxxObkm r/vEUObKX18uQ9c1yO2b46rl0bX+nN/DLnY3v+lh4KwIk+RpPx2wWHmaDtBweRkIUaYE c0iw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=ddJMPzp8uk8W77TpH/QtHPf5ZLig2Pj6FL/Wyp2ecto=; b=apqPfo4B8YNFdpGr15WpaOHp055T6o70sP0R+cM9t+ZefGWiZUT1WIUvATzyWcwyi7 M5+a3kfgMBlWEHpicRZpECWXXQhcR0Yqq7N++2JPTuaYA8nQFyCqMJAWjTmvI84dDapF 37JJ9iCG1fDf7sUlz5KjRUGclnndYQ2aw0zAJwF+7NIXc8lFHoFBLiNkw0dWXwn1A7sn QM4oaRznm0csey3hyHNAei0foTlZEyhHaCQ2zynTAMEugHSVgSAo7zFysdniQ42XcERc 6HySoSf1DO3IABj5ofDxE2qXDmE+mVl+8tU2rxozzGgGsXNOpvZ/uQ/ethap2MKy9PC0 qeAQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id oi22si6650175ejb.631.2020.12.08.08.51.17; Tue, 08 Dec 2020 08:51:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730400AbgLHQtb (ORCPT + 99 others); Tue, 8 Dec 2020 11:49:31 -0500 Received: from aposti.net ([89.234.176.197]:60800 "EHLO aposti.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726703AbgLHQta (ORCPT ); Tue, 8 Dec 2020 11:49:30 -0500 From: Paul Cercueil To: Linus Walleij Cc: Arnd Bergmann , od@zcrc.me, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Cercueil Subject: [PATCH 1/2] if_enabled.h: Add IF_ENABLED_OR_ELSE() and IF_ENABLED() macros Date: Tue, 8 Dec 2020 16:48:20 +0000 Message-Id: <20201208164821.2686082-1-paul@crapouillou.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Introduce a new header , that brings two new macros: IF_ENABLED_OR_ELSE() and IF_ENABLED(). IF_ENABLED_OR_ELSE(CONFIG_FOO, a, b) evaluates to (a) if CONFIG_FOO is set to 'y' or 'm', (b) otherwise. It is used internally to define the IF_ENABLED() macro. The (a) and (b) arguments must be of the same type. IF_ENABLED(CONFIG_FOO, ptr) evaluates to (ptr) if CONFIG_FOO is set to 'y' or 'm', NULL otherwise. The (ptr) argument must be a pointer. The IF_ENABLED() macro can be very useful to help GCC drop dead code. For instance, consider the following: #ifdef CONFIG_FOO_SUSPEND static int foo_suspend(struct device *dev) { ... } #endif static struct pm_ops foo_ops = { #ifdef CONFIG_FOO_SUSPEND .suspend = foo_suspend, #endif }; While this works, the foo_suspend() macro is compiled conditionally, only when CONFIG_FOO_SUSPEND is set. This is problematic, as there could be a build bug in this function, we wouldn't have a way to know unless the config option is set. An alternative is to declare foo_suspend() always, but mark it as maybe unused: static int __maybe_unused foo_suspend(struct device *dev) { ... } static struct pm_ops foo_ops = { #ifdef CONFIG_FOO_SUSPEND .suspend = foo_suspend, #endif }; Again, this works, but the __maybe_unused attribute is required to instruct the compiler that the function may not be referenced anywhere, and is safe to remove without making a fuss about it. This makes the programmer responsible for tagging the functions that can be garbage-collected. With this patch, it is now possible to write the following: static int foo_suspend(struct device *dev) { ... } static struct pm_ops foo_ops = { .suspend = IF_ENABLED(CONFIG_FOO_SUSPEND, foo_suspend), }; The foo_suspend() function will now be automatically dropped by the compiler, and it does not require any specific attribute. Signed-off-by: Paul Cercueil --- include/linux/if_enabled.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 include/linux/if_enabled.h diff --git a/include/linux/if_enabled.h b/include/linux/if_enabled.h new file mode 100644 index 000000000000..8189d1402340 --- /dev/null +++ b/include/linux/if_enabled.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __LINUX_IF_ENABLED_H +#define __LINUX_IF_ENABLED_H + +#include +#include +#include + +/* + * IF_ENABLED_OR_ELSE(CONFIG_FOO, a, b) evaluates to (a) if CONFIG_FOO is set + * to 'y' or 'm', (b) otherwise. + */ +#define IF_ENABLED_OR_ELSE(option, a, b) \ + (BUILD_BUG_ON_ZERO(__same_type((a), (b))) || IS_ENABLED(option) ? (a) : (b)) + +/* + * IF_ENABLED(CONFIG_FOO, ptr) evaluates to (ptr) if CONFIG_FOO is set to 'y' + * or 'm', NULL otherwise. + */ +#define IF_ENABLED(option, ptr) IF_ENABLED_OR_ELSE(option, ptr, NULL) + +#endif /* __LINUX_IF_ENABLED_H */ -- 2.29.2