From patchwork Mon Jul 9 16:25:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Vetter X-Patchwork-Id: 10515199 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 76D8C6032C for ; Mon, 9 Jul 2018 16:25:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 61F1128BD7 for ; Mon, 9 Jul 2018 16:25:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5661E28CA4; Mon, 9 Jul 2018 16:25:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3BFA628BD7 for ; Mon, 9 Jul 2018 16:25:20 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9E8916E52C; Mon, 9 Jul 2018 16:25:17 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-ed1-x542.google.com (mail-ed1-x542.google.com [IPv6:2a00:1450:4864:20::542]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1632B6E52C for ; Mon, 9 Jul 2018 16:25:17 +0000 (UTC) Received: by mail-ed1-x542.google.com with SMTP id x5-v6so10719828edr.0 for ; Mon, 09 Jul 2018 09:25:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=+fbqYqPvkeNQn/GeP/TLNQC6r5DJ3br/mflYTWDsAbw=; b=pjNh3tis5eSpnxHeor3grtaffcTMjhBu0niy7JH5e0C5MZkAgHQr5MJOKEQ0nsYhMI hj/9jC7uStNcXTK+5rVSf2FRgTacLL/s943/Sg4BYziPEPywkg0/mW7c4RW4e7y7I21t bIO4idgOCnwmCl0x/uL8axvzVhgGTzReGmwW6Z/k2JoCPYh877AiGPV24lvlaSaoEfBq bffjBORrncrKCFcz4j33F1Wrcj7NJ2d/6Vtd7TJnxQHkWc97VUHJckLTelBdcOjoKZsh E3nihONBBKAFG4n9PBkdhXAxlHq//wYNGIzVrvP7A+YHYtSfW7rlkgTpMczHNyezv7rM 5KlQ== X-Gm-Message-State: APt69E1RFjipdkD1u8GLp1tt0bliTQ+QfYLVgEtPmW7LrAopryzov0ah P8Q1UyU45ZjnJlEQnzYp4jVGEg== X-Google-Smtp-Source: AAOMgpdu3uIJO5A3ZMH//9TI76An6wwZXlcam+EGPtvg8eA+oLA3POOMoOCgKYoK9Rq9uixHrxOFTw== X-Received: by 2002:a50:a4f1:: with SMTP id x46-v6mr22349768edb.247.1531153515752; Mon, 09 Jul 2018 09:25:15 -0700 (PDT) Received: from phenom.ffwll.local ([2a02:168:5628:0:496f:7dc5:66d7:a057]) by smtp.gmail.com with ESMTPSA id z3-v6sm7578905edh.56.2018.07.09.09.25.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Jul 2018 09:25:14 -0700 (PDT) From: Daniel Vetter To: LKML Subject: [PATCH] kernel.h: Add for_each_if() Date: Mon, 9 Jul 2018 18:25:09 +0200 Message-Id: <20180709162509.29343-1-daniel.vetter@ffwll.ch> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180709083650.23549-1-daniel.vetter@ffwll.ch> References: <20180709083650.23549-1-daniel.vetter@ffwll.ch> X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kees Cook , Greg Kroah-Hartman , David Airlie , Daniel Vetter , Intel Graphics Development , Randy Dunlap , NeilBrown , DRI Development , Yisheng Xie , Peter Zijlstra , Wei Wang , Daniel Vetter , Andrew Morton , Andy Shevchenko , Ingo Molnar , Andrei Vagin MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP To avoid compilers complainig about ambigious else blocks when putting an if condition into a for_each macro one needs to invert the condition and add a dummy else. We have a nice little convenience macro for that in drm headers, let's move it out. Subsequent patches will roll it out to other places. The issue the compilers complain about are nested if with an else block and no {} to disambiguate which if the else belongs to. The C standard is clear, but in practice people forget: if (foo) if (bar) /* something */ else /* something else The same can happen in a for_each macro when it also contains an if condition at the end, except the compiler message is now really confusing since there's only 1 if: for_each_something() if (bar) /* something */ else /* something else The for_each_if() macro, by inverting the condition and adding an else, avoids the compiler warning. Motivated by a discussion with Andy and Yisheng, who want to add another for_each_macro which would benefit from for_each_if() instead of hand-rolling it. Cc: Gustavo Padovan Cc: Maarten Lankhorst Cc: Sean Paul Cc: David Airlie Cc: Andrew Morton Cc: Kees Cook Cc: Ingo Molnar Cc: Greg Kroah-Hartman Cc: NeilBrown Cc: Wei Wang Cc: Stefan Agner Cc: Andrei Vagin Cc: Randy Dunlap Cc: Andy Shevchenko Cc: Yisheng Xie Cc: Peter Zijlstra Reviewed-by: Andy Shevchenko Signed-off-by: Daniel Vetter --- v2: Explain a bit better what this is good for, after the discussion with Peter Z. --- include/drm/drmP.h | 3 --- include/linux/kernel.h | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/drm/drmP.h b/include/drm/drmP.h index f7a19c2a7a80..05350424a4d3 100644 --- a/include/drm/drmP.h +++ b/include/drm/drmP.h @@ -110,7 +110,4 @@ static inline bool drm_can_sleep(void) return true; } -/* helper for handling conditionals in various for_each macros */ -#define for_each_if(condition) if (!(condition)) {} else - #endif diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 941dc0a5a877..4cb95ab9a5bc 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -71,6 +71,9 @@ */ #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) +/* helper for handling conditionals in various for_each macros */ +#define for_each_if(condition) if (!(condition)) {} else + #define u64_to_user_ptr(x) ( \ { \ typecheck(u64, x); \