public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Lars-Peter Clausen <lars@metafoo.de>
To: Javier Martinez Canillas <javier@dowhile0.org>
Cc: Julia Lawall <Julia.Lawall@lip6.fr>,
	Michal Marek <mmarek@suse.cz>,
	linux-kernel@vger.kernel.org, cocci@systeme.lip6.fr
Subject: Re: [Cocci] [PATCH v2 1/1] scripts/coccinelle: use BIT macro if used
Date: Tue, 29 Apr 2014 18:18:40 +0200	[thread overview]
Message-ID: <535FD0E0.70809@metafoo.de> (raw)
In-Reply-To: <1398595800-6683-1-git-send-email-javier@dowhile0.org>

On 04/27/2014 12:50 PM, Javier Martinez Canillas wrote:
> Using the BIT() macro instead of manually shifting bits
> makes the code less error prone.
>
> If is more readable is a matter of taste so only replace
> if the file is already using this macro.
>
> Signed-off-by: Javier Martinez Canillas <javier@dowhile0.org>

I don't think this should be enabled by default. It will generate a ton of 
false positives, not everything that is 1 shifted by something is a 
single-bit field. E.g. imagine a device with multi-bit fields:

#define FOOBAR_A (0 << FOOBAR_OFFSET)
#define FOOBAR_B (1 << FOOBAR_OFFSET)
#define FOOBAR_C (2 << FOOBAR_OFFSET)
#define FOOBAR_D (3 << FOOBAR_OFFSET)

The script will now suggest to replace FOOBAR_B (1 << FOOBAR_OFFSET) with 
FOOBAR_B BIT(FOOBAR_OFFSET). Which is technically correct, but not semantically.

- Lars

> ---
>
> Changes since v1:
>   - Add a rule that checks if the file is already using this macro
>     as suggested by Julia Lawall
>
>   scripts/coccinelle/api/bit.cocci | 30 ++++++++++++++++++++++++++++++
>   1 file changed, 30 insertions(+)
>   create mode 100644 scripts/coccinelle/api/bit.cocci
>
> diff --git a/scripts/coccinelle/api/bit.cocci b/scripts/coccinelle/api/bit.cocci
> new file mode 100644
> index 0000000..a02cfd3
> --- /dev/null
> +++ b/scripts/coccinelle/api/bit.cocci
> @@ -0,0 +1,30 @@
> +// Use the BIT() macro if is already used
> +//
> +// Confidence: High
> +// Copyright (C) 2014 Javier Martinez Canillas.  GPLv2.
> +// URL: http://coccinelle.lip6.fr/
> +// Options: --include-headers
> +
> +@hasbitops@
> +@@
> +
> +#include <linux/bitops.h>
> +
> +@usesbit@
> +@@
> +
> +BIT(...)
> +
> +@depends on hasbitops && usesbit@
> +expression E;
> +@@
> +
> +- 1 << E
> ++ BIT(E)
> +
> +@depends on hasbitops && usesbit@
> +expression E;
> +@@
> +
> +- BIT((E))
> ++ BIT(E)
>


  reply	other threads:[~2014-04-29 16:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-27 10:50 [PATCH v2 1/1] scripts/coccinelle: use BIT macro if used Javier Martinez Canillas
2014-04-29 16:18 ` Lars-Peter Clausen [this message]
2014-04-30 12:05   ` [Cocci] " Javier Martinez Canillas

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=535FD0E0.70809@metafoo.de \
    --to=lars@metafoo.de \
    --cc=Julia.Lawall@lip6.fr \
    --cc=cocci@systeme.lip6.fr \
    --cc=javier@dowhile0.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mmarek@suse.cz \
    /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