Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp459757imm; Wed, 11 Jul 2018 05:39:25 -0700 (PDT) X-Google-Smtp-Source: AAOMgpe6MJae4hlhy22A9RRDBqkEJ8TEkqGipj44/1vofrIEkCTzgJKwlxQQrEYKZq71Ci+Qpd4F X-Received: by 2002:a17:902:6ac7:: with SMTP id i7-v6mr29343629plt.288.1531312765795; Wed, 11 Jul 2018 05:39:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531312765; cv=none; d=google.com; s=arc-20160816; b=eTuG2rsj0Enlh+2ZFb9i/t4/489L/vCHno1hXGwWUFVaX5t6I6K7soeqdnJ5mVg9Zm KWw4vFp1ll7h/KZloyWEKNgzAcyQQpNNOlUzHNt3t7ikPBoEbcSeR/RO8yGC8Fvwnx2S Ee38W8B6tAm5b9fw586ExZthmkE8yVbVRzktalqyppSrH5tnuHtZaMxibDifx/G7slYR E1Y3T7gs53rFqr+UpPsljBzobsplE6nZ6IWTXAwA5+F1v+D787BXC+tJeqNx7PpvXLrc +HBMb4PUj8KiK5ofYiYx7UtoWIb8QuREPnnlNZBTI+tjgClO9NTqXLtSyYFlpYaf8VuC lnxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=UMyl+7eMVhoJe82mLlfKA5i2hhnu2JlOHvhdvL0gSME=; b=kU/lxUIYrrN4+kj09W/mPwA7V0ggJBjFyVbPGKFkaQxzMChRVaYOzJzxSM73MZcn72 wz8NB8G8Jyl1PFaMEg0UQ1KP5cdGb3AW87hABEATu5q7Kzf6JlLjZyoZH0p2m3CeJWKE RWOdl4bwJhNqj8ysSZQqbQfuYJwNJhn/b/Ao4nAO6/RESSsZ+HNYkPOLmhBiJSkqwLPE wz68kjhfL1+zaZxuMWSPCzy3AH8cEkWQe90KkALbZ/BDHibRiNVadbgxIFvzfEqSLqYF g8SWA998uBKC4nOnpInMz72EcYsWbJzsLJTook/MTnZUwA6iU/GBQNTc6m6MseQFMniL djIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@ffwll.ch header.s=google header.b=d4Ml8RwN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t66-v6si21077399pfg.292.2018.07.11.05.39.10; Wed, 11 Jul 2018 05:39:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@ffwll.ch header.s=google header.b=d4Ml8RwN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732817AbeGKLzI (ORCPT + 99 others); Wed, 11 Jul 2018 07:55:08 -0400 Received: from mail-it0-f67.google.com ([209.85.214.67]:54999 "EHLO mail-it0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726818AbeGKLzI (ORCPT ); Wed, 11 Jul 2018 07:55:08 -0400 Received: by mail-it0-f67.google.com with SMTP id s7-v6so3169611itb.4 for ; Wed, 11 Jul 2018 04:51:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=UMyl+7eMVhoJe82mLlfKA5i2hhnu2JlOHvhdvL0gSME=; b=d4Ml8RwNma1cUq7qG0oUaxFxW4mRbdd0JRU8FaUr7FUhj+8DB/jO69fVna1CEd6ofM mvCVAU+tPVHmjOeIjGQeetg3lLB08Qu+uRjogIgXY0XZPDHa/ICkDLtAnb2MNNEUCqh9 JDJ3OtzfkIZ95J9Z2ZHpKF5p6CiqV+hiDF5KU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=UMyl+7eMVhoJe82mLlfKA5i2hhnu2JlOHvhdvL0gSME=; b=Gk1bBqXVHzEC7nWFMhcKxlVPrapAV+vxy/vDALB4SC2+105DyiYsGfQDbozXD4++Cp g7nkyI0GeqJrbG23IafFm1aHyJq7QESBhzlpV5SdZXTPH0/ZZ/imfMmDm70/KtIX4gKJ +qdmxE9bRMqmQGekHmVdVy3/shncj7p2lpQikptaoONPkonci3CQITCowj56BkbKCS+Y w5klo/O5Oxk3KrTB3wxI+dlxS5hBNUn9ElzQluEQZoSVWjcsA/OsBZqOT3iwK3AWzA6H a/KKDVYBpwultcoR8EUZxU7Tj99R+z2fIXaBidpshIpwh51Nin8CE6eZfaJWNn44gFJD yX3w== X-Gm-Message-State: APt69E3dIrD4KNFsdsLt5mQOH6jQhEerzT9vNLEMteUjpME+Q35RHBrH F/Qkz570iU9GditW2uOPs8B2ZrgmyRYXYxDUmZjgmA== X-Received: by 2002:a24:52:: with SMTP id 79-v6mr22984107ita.58.1531309869167; Wed, 11 Jul 2018 04:51:09 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a4f:e492:0:0:0:0:0 with HTTP; Wed, 11 Jul 2018 04:51:08 -0700 (PDT) X-Originating-IP: [212.51.149.109] In-Reply-To: <871scbwfd4.fsf@notabene.neil.brown.name> References: <20180709083650.23549-1-daniel.vetter@ffwll.ch> <20180709162509.29343-1-daniel.vetter@ffwll.ch> <20180709163001.8fb8148223a57bc46a13fbda@linux-foundation.org> <20180710075328.GG3008@phenom.ffwll.local> <871scbwfd4.fsf@notabene.neil.brown.name> From: Daniel Vetter Date: Wed, 11 Jul 2018 13:51:08 +0200 X-Google-Sender-Auth: 7Clwz2lAPTu5AbNIS497KBVxgvw Message-ID: Subject: Re: [PATCH] kernel.h: Add for_each_if() To: NeilBrown Cc: Andrew Morton , LKML , DRI Development , Intel Graphics Development , Gustavo Padovan , Maarten Lankhorst , Sean Paul , David Airlie , Kees Cook , Ingo Molnar , Greg Kroah-Hartman , Wei Wang , Stefan Agner , Andrei Vagin , Randy Dunlap , Andy Shevchenko , Yisheng Xie , Peter Zijlstra , Daniel Vetter Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jul 10, 2018 at 12:32 PM, NeilBrown wrote: > On Tue, Jul 10 2018, Daniel Vetter wrote: > >> On Mon, Jul 09, 2018 at 04:30:01PM -0700, Andrew Morton wrote: >>> On Mon, 9 Jul 2018 18:25:09 +0200 Daniel Vetter wrote: >>> >>> > 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 >>> >>> um, yeah, don't do that. Kernel coding style is very much to do >>> >>> if (foo) { >>> if (bar) >>> /* something */ >>> else >>> /* something else >>> } >>> >>> And if not doing that generates a warning then, well, do that. >>> >>> > 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. >>> >>> Ditto. >>> >>> > 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. >>> >>> Ditto. >>> >>> > v2: Explain a bit better what this is good for, after the discussion >>> > with Peter Z. >>> >>> Presumably the above was discussed in whatever-thread-that-was. >> >> So there's a bunch of open coded versions of this already in kernel >> headers (at least the ones I've found). Not counting the big pile of >> existing users in drm. They are all wrong and should be reverted to a >> plain if? That why there's a bunch more patches in this series. >> >> And yes I made it clear in the discussion that if you sprinkle enough {} >> there's no warning, should have probably captured this here. >> >> Aka a formal Nack-pls-keep-your-stuff-in-drm: would be appreciated so I >> can stop bothering with this. > > I think is it problematic to have macros like > > #define for_each_foo(...) for (......) if (....) > > because > for_each_foo(...) > if (x) ....; else ......; > > is handled badly. > So in that sense, your work seems like a good thing. > > However it isn't clear to me that you need a new macro. > The above macro could simply be changed to > > #define for_each_foo(...) for (......) if (!....);else > > Clearly people don't always think to do this, but would adding a macro > help people to think? > > If we were to have a macro, it isn't clear to me that for_each_if() is a > good name. > Every other macro I've seen that starts "for_each_" causes the body to > loop. This one doesn't. If someone doesn't know what for_each_if() > does and sees it in code, they are unlikely to jump to the right > conclusion. > I would suggest that "__if" would be a better choice. I think most > people would guess that means "like 'if', but a bit different", which is > fairly accurate. > > I think the only sure way to avoid bad macros being written is to teach > some static checker to warn about any macro with a dangling "if". > Possibly checkpatch.pl could do that (but I'm not volunteering). > > I do agree that it would be good to do something, and if people find > for_each_fi() to actually reduce the number of poorly written macros, > then I don't object to it. There's also the proposal of if_noelse() which I think fares a bit better than the __if(). But I still have the situation that a bunch of maintainers acked this and Andrew Morton defacto nacked it, which I guess means I'll keep the macro in drm? The common way to go about this seems to be to just push the patch series with the ack in some pull request to Linus and ignore the people who raised questions, but not really my thing. -Daniel -- Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch