public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Tom Rini <trini@konsulko.com>
To: u-boot@lists.denx.de
Subject: [PATCH 6/6] checkpatch.pl: Request if() instead #ifdef
Date: Mon, 15 Jun 2020 21:21:34 -0400	[thread overview]
Message-ID: <20200616012134.GR24893@bill-the-cat> (raw)
In-Reply-To: <20200616003430.GA15478@laputa>

On Tue, Jun 16, 2020 at 09:34:30AM +0900, AKASHI Takahiro wrote:
> On Mon, Jun 15, 2020 at 10:34:56AM -0400, Tom Rini wrote:
> > On Sun, Jun 14, 2020 at 09:58:48PM -0600, Simon Glass wrote:
> > > Hi Akashi,
> > > 
> > > On Sun, 14 Jun 2020 at 20:59, AKASHI Takahiro
> > > <takahiro.akashi@linaro.org> wrote:
> > > >
> > > > On Thu, Jun 04, 2020 at 07:39:35PM -0400, Tom Rini wrote:
> > > > > On Fri, May 22, 2020 at 04:32:40PM -0600, Simon Glass wrote:
> > > > >
> > > > > > There is a lot of use of #ifdefs in U-Boot. In an effort reduce this,
> > > > > > suggest using the compile-time construct.
> > > > > >
> > > > > > Signed-off-by: Simon Glass <sjg@chromium.org>
> > > > >
> > > > > Applied to u-boot/master, thanks!
> > > >
> > > > This check is simple, but IMHO, too simple.
> > > > It will generate false-positive, or pointless, warnings
> > > > for some common use cases. Say,
> > > >
> > > > In an include header,
> > > > #ifdef CONFIG_xxx
> > > > extern int foo_data;
> > > > int foo(void);
> > > > #endif
> > > 
> > > We should try to avoid this in header files. But I sent a patch
> > > earlier today to turn off the check for header files and device tree.
> > 
> > Right, in a header that's a bad idea unless it's:
> 
> I'm not sure that it is a so bad idea; I think that it will
> detect some configuration error immediately rather than
> at the link time.

We prefer link time failures as -Werror is not the default in a regular
build.

> > ...
> > #else
> > static inline foo(void) {}
> > #endif
> 
> Well, in this case, a corresponding C file often has a definition like:
> #ifdef CONFIG_xxx
> int foo(void) {
>     ...
> }
> #endif

Right, and that's fine.  But headers do not get guards around functions
unless it's else-inline-to-nop-it-out.

> > > > Or in a C file (foo_common.c),
> > > > #ifdef CONFIG_xxx_a
> > > > int foo_a(void)
> > > > ...
> > > > #endif
> > > > #ifdef CONFIG_xxx_b
> > > > int foo_b(void)
> > > > ...
> > > > #endif
> > > >
> > > 
> > > Perhaps the if() could be inside those functions in some cases?
> > 
> > Yeah, that's less clearly an example of something bad.
> 
> Again, I'm not sure that it is a bad idea. Such a use can be
> seen quite often in library code where there are many configurable
> options.
> The only way to avoid such a style of coding is that we would
> put each function into a separate C file even if it can be
> very small. It also requires more (common/helper) functions, which are
> essentially local to that library, to be declared as global.
> 
> Is this what you want?

It comes down to what the code reads best as, yes.  A checkpatch error
isn't a fatal you must fix it error.  But you must be able to explain
why it's wrong.  And I think we're getting away from the main point
here.  Generally, #ifdef CONFIG_FOO .... #endif, in a function is ugly
and we can do better.  It also means better code analysis as I believe
some tools will still evaluate if (0) { ... } but will not evaluate #if
0 ... #endif.

> > > > Or,
> > > >
> > > > struct baa baa_list[] = {
> > > > #ifdef CONFIG_xxx
> > > >         data_xxx,
> > > > #endif
> > > 
> > > I'm not sure how to handle this one.
> > 
> > Rasmus' series to add CONFIG_IS_ENABLED(SYM, true, false) stuff would be
> > handy here.
> 
> Ah, I didn't notice that. We can now have the code like:
> struct baa baa_list[] = {
>     ...
>     CONFIG_IS_ENABLED(xxx, (data_xxx,))
>     ...
> }
> 
> ## I think the comma after 'data_xxx' is required, isn't it?
> 
> But what is the merit?
> 
> And, data_xxx itself has to be declared anyway like:
> #ifdef CONFIG_xxx
> struct baa data_xxx = {
>     ...
> };
> #endif

We _could_ have that yes, he's posted an RFC I need to reply to
directly.  As you would probably also need a __maybe_unused on the
struct itself.  Is that better?

> > > > ...
> > > >
> > > > They are harmless and can be ignored, but also annoying.
> > > > Can you sophisticate this check?
> > > 
> > > Yes I agree we should avoid false negatives. It is better not to have
> > > a check than have one that is unreliable.
> > > 
> > > >
> > > > In addition, if I want to stick to this rule, there can co-exist
> > > > an "old" style and "new" style of code in a single file.
> > > > (particularly tons of examples in UEFI subsystem)
> > > >
> > > > How should we deal with this?
> > > 
> > > Convert it?
> > 
> > Yes, code should be cleaned up and converted to using if (...) when
> > possible.  That we have new code that doesn't make use of this is
> > because we didn't have tooling warning about when it wasn't used.
> 
> So if we want to add a new commit that complies with this rule while
> the file to which it will be applied has an old style of code,
> do you *require* that this existing file should be converted first
> in any case?

I honestly don't know.  Is it a problem to look over the code and make
use of if (IS_ENABLED(...)) { ... } when it would make the code read
better and get better analysis?

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200615/4232080d/attachment.sig>

  reply	other threads:[~2020-06-16  1:21 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-22 22:32 [PATCH 0/6] checkpatch.pl: Add features to help improve U-Boot code Simon Glass
2020-05-22 22:32 ` [PATCH 1/6] checkpatch.pl: Update to v5.7-rc6 Simon Glass
2020-06-04 23:39   ` Tom Rini
2020-05-22 22:32 ` [PATCH 2/6] checkpatch.pl: Add a U-Boot option Simon Glass
2020-06-04 23:39   ` Tom Rini
2020-05-22 22:32 ` [PATCH 3/6] checkpatch.pl: Add a check for tests needed for uclasses Simon Glass
2020-06-04 23:39   ` Tom Rini
2020-05-22 22:32 ` [PATCH 4/6] checkpatch.pl: Warn if the flattree API is used Simon Glass
2020-06-04 23:39   ` Tom Rini
2020-05-22 22:32 ` [PATCH 5/6] checkpatch.pl: Request a test when a new command is added Simon Glass
2020-06-04 23:39   ` Tom Rini
2020-05-22 22:32 ` [PATCH 6/6] checkpatch.pl: Request if() instead #ifdef Simon Glass
2020-06-04 23:39   ` Tom Rini
2020-06-15  2:58     ` AKASHI Takahiro
2020-06-15  3:58       ` Simon Glass
2020-06-15 14:34         ` Tom Rini
2020-06-16  0:34           ` AKASHI Takahiro
2020-06-16  1:21             ` Tom Rini [this message]
2020-06-16  2:15             ` Simon Glass
2020-05-24 18:23 ` [PATCH 0/6] checkpatch.pl: Add features to help improve U-Boot code Tom Rini
2020-05-26 18:29 ` [PATCHv2] checkpatch.pl: Add check for defining CONFIG_CMD_xxx via config files Tom Rini
2020-06-04 23:39   ` Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200616012134.GR24893@bill-the-cat \
    --to=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox