public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] checkpatch: Add --strict preference for #defines using BIT(foo)
       [not found]   ` <20141107.151607.480474516800359791.davem@davemloft.net>
@ 2014-11-07 21:15     ` Joe Perches
  2014-11-09  9:50       ` Jiri Pirko
  2014-11-10 23:36       ` Andrew Morton
  0 siblings, 2 replies; 5+ messages in thread
From: Joe Perches @ 2014-11-07 21:15 UTC (permalink / raw)
  To: Andrew Morton; +Cc: David Miller, jiri, netdev, LKML

Using BIT(foo) and BIT_ULL(bar) is more common now.
Suggest using these macros over #defines with 1<<value.

Add a --fix option too.

Signed-off-by: Joe Perches <joe@perches.com>
---
 scripts/checkpatch.pl | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 893cbd5..b5dc3f4 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4973,6 +4973,17 @@ sub process {
 			}
 		}
 
+# check for #defines like: 1 << <digit> that could be BIT(digit)
+		if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
+			my $ull = "";
+			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
+			if (CHK("BIT_MACRO",
+				"Prefer using the BIT$ull macro\n" . $herecurr) &&
+			    $fix) {
+				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
+			}
+		}
+
 # check for case / default statements not preceded by break/fallthrough/switch
 		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
 			my $has_break = 0;



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] checkpatch: Add --strict preference for #defines using BIT(foo)
  2014-11-07 21:15     ` [PATCH] checkpatch: Add --strict preference for #defines using BIT(foo) Joe Perches
@ 2014-11-09  9:50       ` Jiri Pirko
  2014-11-09 14:22         ` Joe Perches
  2014-11-10 23:36       ` Andrew Morton
  1 sibling, 1 reply; 5+ messages in thread
From: Jiri Pirko @ 2014-11-09  9:50 UTC (permalink / raw)
  To: Joe Perches; +Cc: Andrew Morton, David Miller, netdev, LKML

Fri, Nov 07, 2014 at 10:15:39PM CET, joe@perches.com wrote:
>Using BIT(foo) and BIT_ULL(bar) is more common now.
>Suggest using these macros over #defines with 1<<value.

Joe, regarding the other Dave's comment, the multiline one, that is also
not covered by checkpatch. Would please you take care of that as well?

Thanks.

>
>Add a --fix option too.
>
>Signed-off-by: Joe Perches <joe@perches.com>
>---
> scripts/checkpatch.pl | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
>diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
>index 893cbd5..b5dc3f4 100755
>--- a/scripts/checkpatch.pl
>+++ b/scripts/checkpatch.pl
>@@ -4973,6 +4973,17 @@ sub process {
> 			}
> 		}
> 
>+# check for #defines like: 1 << <digit> that could be BIT(digit)
>+		if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
>+			my $ull = "";
>+			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
>+			if (CHK("BIT_MACRO",
>+				"Prefer using the BIT$ull macro\n" . $herecurr) &&
>+			    $fix) {
>+				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
>+			}
>+		}
>+
> # check for case / default statements not preceded by break/fallthrough/switch
> 		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
> 			my $has_break = 0;
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] checkpatch: Add --strict preference for #defines using BIT(foo)
  2014-11-09  9:50       ` Jiri Pirko
@ 2014-11-09 14:22         ` Joe Perches
  0 siblings, 0 replies; 5+ messages in thread
From: Joe Perches @ 2014-11-09 14:22 UTC (permalink / raw)
  To: Jiri Pirko; +Cc: Andrew Morton, David Miller, netdev, LKML

On Sun, 2014-11-09 at 10:50 +0100, Jiri Pirko wrote:
> Joe, regarding the other Dave's comment, the multiline one, that is also
> not covered by checkpatch. Would please you take care of that as well?

No, as far as I know, it's not feasible given
insertion/deletion, but you are welcome to try.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] checkpatch: Add --strict preference for #defines using BIT(foo)
  2014-11-07 21:15     ` [PATCH] checkpatch: Add --strict preference for #defines using BIT(foo) Joe Perches
  2014-11-09  9:50       ` Jiri Pirko
@ 2014-11-10 23:36       ` Andrew Morton
  2014-11-10 23:53         ` Joe Perches
  1 sibling, 1 reply; 5+ messages in thread
From: Andrew Morton @ 2014-11-10 23:36 UTC (permalink / raw)
  To: Joe Perches; +Cc: David Miller, jiri, netdev, LKML

On Fri, 07 Nov 2014 13:15:39 -0800 Joe Perches <joe@perches.com> wrote:

> Using BIT(foo) and BIT_ULL(bar) is more common now.
> Suggest using these macros over #defines with 1<<value.

urgh.  I'm counting eightish implementations of BIT(), an unknown
number of which are actually being used.  Many use 1<<n, some use
1UL<<N, another uses 1ULL<<n.  I'm a bit reluctant to recommend that
anyone should use BIT() until it has has some vigorous scrubbing :(

Is it actually an improvement?  If I see

#define X	(1U << 7)

then I know exactly what it does.  Whereas when I see

#define X	BIT(7)

I know neither the size or the signedness of X so I have to go look it
up.


I have no strong feelings either way, but I'm wondering what might have
inspired this change?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] checkpatch: Add --strict preference for #defines using BIT(foo)
  2014-11-10 23:36       ` Andrew Morton
@ 2014-11-10 23:53         ` Joe Perches
  0 siblings, 0 replies; 5+ messages in thread
From: Joe Perches @ 2014-11-10 23:53 UTC (permalink / raw)
  To: Andrew Morton; +Cc: David Miller, jiri, netdev, LKML

On Mon, 2014-11-10 at 15:36 -0800, Andrew Morton wrote:
> On Fri, 07 Nov 2014 13:15:39 -0800 Joe Perches <joe@perches.com> wrote:
> 
> > Using BIT(foo) and BIT_ULL(bar) is more common now.
> > Suggest using these macros over #defines with 1<<value.
> 
> urgh.  I'm counting eightish implementations of BIT(), an unknown
> number of which are actually being used.  Many use 1<<n, some use
> 1UL<<N, another uses 1ULL<<n.  I'm a bit reluctant to recommend that
> anyone should use BIT() until it has has some vigorous scrubbing :(
>
> Is it actually an improvement?  If I see
> 
> #define X	(1U << 7)
> 
> then I know exactly what it does.  Whereas when I see
> 
> #define X	BIT(7)
> 
> I know neither the size or the signedness of X so I have to go look it
> up.

I'm not sure the signedness or type of X matters much
as the non-64bit comparisons are done by promotion to
at least int or unsigned int anyway.

The BIT macro makes sure a single bit is set.

The 'good' one is in bitops.h.  It also has #define BIT_ULL

include/linux/bitops.h:#define BIT(nr)                  (1UL << (nr))
include/linux/bitops.h-#define BIT_ULL(nr)              (1ULL << (nr))

The ones in tools/ are independent and should not be changed.
Excluding tools/, the others should probably be removed

$ git grep -E "define\s+BIT\b" 
arch/arm/mach-davinci/sleep.S:#define BIT(nr)                   (1 << (nr))
drivers/staging/rtl8188eu/include/rtl8188e_spec.h:#define BIT(x)                (1 << (x))
drivers/staging/rtl8188eu/include/wifi.h:#define BIT(x) (1 << (x))
drivers/staging/rtl8192e/rtl8192e/rtl_core.h:#define BIT(_i)                            (1<<(_i))
drivers/staging/rtl8712/osdep_service.h:        #define BIT(x)  (1 << (x))
drivers/staging/rtl8712/wifi.h:#define BIT(x)   (1 << (x))

> I have no strong feelings either way, but I'm wondering what might have
> inspired this change?

David Miller commented on a netdev patch where 1<<foo was
being used in a #define and suggested using BIT.

http://article.gmane.org/gmane.linux.network/337535/match=bit

Using BIT _is_ more common in recent patches too.



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-11-10 23:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <1415265610-9338-1-git-send-email-jiri@resnulli.us>
     [not found] ` <1415265610-9338-10-git-send-email-jiri@resnulli.us>
     [not found]   ` <20141107.151607.480474516800359791.davem@davemloft.net>
2014-11-07 21:15     ` [PATCH] checkpatch: Add --strict preference for #defines using BIT(foo) Joe Perches
2014-11-09  9:50       ` Jiri Pirko
2014-11-09 14:22         ` Joe Perches
2014-11-10 23:36       ` Andrew Morton
2014-11-10 23:53         ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox