public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scripts: Add device_attr_<rw|ro|wr>.pl scripts to convert DEVICE_ATTR uses
@ 2017-12-22 20:02 Joe Perches
  2017-12-23 14:19 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 5+ messages in thread
From: Joe Perches @ 2017-12-22 20:02 UTC (permalink / raw)
  To: Greg Kroah-Hartman, linux-kernel; +Cc: Julia Lawall

There are many uses of the DEVICE_ATTR(var, perms, show, store)
declaration macro that could use one of the convenience macros
DEVICE_ATTR_RW, DEVICE_ATTR_RO, or DEVICE_ATTR_WO.

These scripts automate those conversions.

Many declarations of functions used to show or store the values
do not use the <var>_show and <var>_store form.  These scripts
also check to see if the normal form is unused in the file, that
the currently named functions are static and only used as the
function definition and the macro declaration and convert the
unusual function name to the more normal style.

Use these scripts with a list of files to be converted.

e.g.: git ls-files -- "*.c" | xargs perl -i scripts/device_attr_rw.pl

Here are the current uses of DEVICE_ATTR macros in next-20171222

$ git grep -w -P "DEVICE_ATTR(_RW|_RO|_WO|)" -- "*.c" | wc -l
3393
$ git grep -w -P "DEVICE_ATTR" -- "*.c" | wc -l
2052
$ git grep -w -P "DEVICE_ATTR_RW" -- "*.c" | wc -l
456
$ git grep -w -P "DEVICE_ATTR_RO" -- "*.c" | wc -l
821
$ git grep -w -P "DEVICE_ATTR_WO" -- "*.c" | wc -l
64

If these scripts on run on that tag, there are 1490 conversions done

$ git grep -w --name-only DEVICE_ATTR -- "*.c" | \
  xargs perl -i scripts/device_attr_rw.pl
$ git grep -w --name-only DEVICE_ATTR -- "*.c" | \
  xargs perl -i scripts/device_attr_ro.pl
$ git grep -w --name-only DEVICE_ATTR -- "*.c" | \
  xargs perl -i scripts/device_attr_wo.pl

$ git grep -w -P "DEVICE_ATTR(_RW|_RO|_WO|)" -- "*.c" | wc -l
3393
$ git grep -w -P "DEVICE_ATTR" -- "*.c" | wc -l
562
$ git grep -w -P "DEVICE_ATTR_RW" -- "*.c" | wc -l
924
$ git grep -w -P "DEVICE_ATTR_RO" -- "*.c" | wc -l
1727
$ git grep -w -P "DEVICE_ATTR_WO" -- "*.c" | wc -l
180

Signed-off-by: Joe Perches <joe@perches.com>

1727
ith '#' will be ignored, and an empty message aborts the commit.
---
 scripts/device_attr_ro.pl | 57 ++++++++++++++++++++++++++++++++++++++++
 scripts/device_attr_rw.pl | 66 +++++++++++++++++++++++++++++++++++++++++++++++
 scripts/device_attr_wo.pl | 57 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 180 insertions(+)
 create mode 100755 scripts/device_attr_ro.pl
 create mode 100755 scripts/device_attr_rw.pl
 create mode 100755 scripts/device_attr_wo.pl

diff --git a/scripts/device_attr_ro.pl b/scripts/device_attr_ro.pl
new file mode 100755
index 000000000000..3d26ed2087d1
--- /dev/null
+++ b/scripts/device_attr_ro.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/env perl
+# SPDX-License-Identifier: GPL-2.0
+# (c) 2017 Joe Perches <joe@perches.com>
+
+# script to convert DEVICE_ATTR uses with permissions of 0444 to DEV_ATTR_RO
+
+my $perms_to_match = qr{
+	  S_IRUGO
+	| 0444
+}x;
+
+my $args_to_match = qr{
+	(\w+)\s*,\s*
+	\(?\s*($perms_to_match)\s*\)?\s*,\s*
+	(\w+)\s*,\s*
+	NULL
+}x;
+
+local $/;
+
+while (<>) {
+    my $file = $_;
+    my $file_copy = $file;
+
+    # strip comments from file_copy (regex from perlfaq)
+    $file_copy =~ s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//([^\\]|[^\n][\n]?)*?\n|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $3 ? $3 : ""#gse;
+
+    # for each declaration of DEVICE_ATTR
+    while ($file =~ m/\bDEVICE_ATTR\s*\(\s*$args_to_match\s*\)/g) {
+	my $var = $1;
+	my $perms = $2;
+	my $show = $3;
+
+	# find the count of uses of the various function names
+	my $count_show = () = ($file_copy =~ /\b$show\b/g);
+	my $count_var_show = () = ($file_copy =~ /\b${var}_show\b/g);
+	my $show_declared = () = ($file_copy =~ /\bstatic\s+ssize_t\s+(?:__ref\s+|__used\s+)?$show\b/g);
+
+	# if the show function is locally declared
+	# rename it to the normal name
+	if (($count_show == 2) &&
+	    ($count_var_show == 0) &&
+	    ($show_declared == 1)) {
+	    $file =~ s/\b$show\b/${var}_show/g;
+	}
+	my $matched = qr{
+			    ${var}\s*,\s*
+			    \(?\s*$perms_to_match\s*\)?\s*,\s*
+			    ${var}_show\s*,\s*
+			    NULL
+		    }x;
+
+	# Rename this use
+	$file =~ s/\bDEVICE_ATTR\s*\(\s*$matched\s*\)/DEVICE_ATTR_RO(${var})/;
+    }
+    print $file;
+}
diff --git a/scripts/device_attr_rw.pl b/scripts/device_attr_rw.pl
new file mode 100755
index 000000000000..a420ee9f39c7
--- /dev/null
+++ b/scripts/device_attr_rw.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/env perl
+# SPDX-License-Identifier: GPL-2.0
+# (c) 2017 Joe Perches <joe@perches.com>
+
+# script to convert DEVICE_ATTR uses with permissions of 0644 to DEV_ATTR_RW
+
+my $perms_to_match = qr{
+	  S_IRUGO\s*\|\s*S_IWUSR
+	| S_IWUSR\s*\|\s*S_IRUGO
+	| 0644
+}x;
+
+my $args_to_match = qr{
+	(\w+)\s*,\s*
+	\(?\s*($perms_to_match)\s*\)?\s*,\s*
+	(\w+)\s*,\s*
+	(\w+)
+}x;
+
+local $/;
+
+while (<>) {
+    my $file = $_;
+    my $file_copy = $file;
+
+    # strip comments from file_copy (regex from perlfaq)
+    $file_copy =~ s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//([^\\]|[^\n][\n]?)*?\n|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $3 ? $3 : ""#gse;
+
+    # for each declaration of DEVICE_ATTR
+    while ($file =~ m/\bDEVICE_ATTR\s*\(\s*$args_to_match\s*\)/g) {
+	my $var = $1;
+	my $perms = $2;
+	my $show = $3;
+	my $store = $4;
+
+	# find the count of uses of the various function names
+	my $count_show = () = ($file_copy =~ /\b$show\b/g);
+	my $count_store = () = ($file_copy =~ /\b$store\b/g);
+	my $count_var_show = () = ($file_copy =~ /\b${var}_show\b/g);
+	my $count_var_store = () = ($file_copy =~ /\b${var}_store\b/g);
+	my $show_declared = () = ($file_copy =~ /\bstatic\s+ssize_t\s+(?:__ref\s+|__used\s+)?$show\b/g);
+	my $store_declared = () = ($file_copy =~ /\bstatic\s+ssize_t\s+(?:__ref\s+|__used\s+)?$store\b/g);
+
+	# if the show/store functions are locally declared
+	# rename them to the normal names
+	if (($count_show == 2) &&
+	    ($count_store == 2) &&
+	    ($count_var_show == 0) &&
+	    ($count_var_store == 0) &&
+	    ($show_declared == 1) &&
+	    ($store_declared == 1)) {
+	    $file =~ s/\b$show\b/${var}_show/g;
+	    $file =~ s/\b$store\b/${var}_store/g;
+	}
+	my $matched = qr{
+			    ${var}\s*,\s*
+			    \(?\s*$perms_to_match\s*\)?\s*,\s*
+			    ${var}_show\s*,\s*
+			    ${var}_store
+		    }x;
+
+	# Rename this use
+	$file =~ s/\bDEVICE_ATTR\s*\(\s*$matched\s*\)/DEVICE_ATTR_RW(${var})/;
+    }
+    print $file;
+}
diff --git a/scripts/device_attr_wo.pl b/scripts/device_attr_wo.pl
new file mode 100755
index 000000000000..b6d51c96b941
--- /dev/null
+++ b/scripts/device_attr_wo.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/env perl
+# SPDX-License-Identifier: GPL-2.0
+# (c) 2017 Joe Perches <joe@perches.com>
+
+# script to convert DEVICE_ATTR uses with permissions of 0644 to DEV_ATTR_RW
+
+my $perms_to_match = qr{
+	  S_IWUSR
+	| 0200
+}x;
+
+my $args_to_match = qr{
+	(\w+)\s*,\s*
+	\(?\s*($perms_to_match)\s*\)?\s*,\s*
+	NULL\s*,\s*
+	(\w+)
+}x;
+
+local $/;
+
+while (<>) {
+    my $file = $_;
+    my $file_copy = $file;
+
+    # strip comments from file_copy (regex from perlfaq)
+    $file_copy =~ s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//([^\\]|[^\n][\n]?)*?\n|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $3 ? $3 : ""#gse;
+
+    # for each declaration of DEVICE_ATTR
+    while ($file =~ m/\bDEVICE_ATTR\s*\(\s*$args_to_match\s*\)/g) {
+	my $var = $1;
+	my $perms = $2;
+	my $store = $3;
+
+	# find the count of uses of the various function names
+	my $count_store = () = ($file_copy =~ /\b$store\b/g);
+	my $count_var_store = () = ($file_copy =~ /\b${var}_store\b/g);
+	my $store_declared = () = ($file_copy =~ /\bstatic\s+ssize_t\s+(?:__ref\s+|__used\s+)?$store\b/g);
+
+	# if the store function is locally declared
+	# rename it to the normal name
+	if (($count_store == 2) &&
+	    ($count_var_store == 0) &&
+	    ($store_declared == 1)) {
+	    $file =~ s/\b$store\b/${var}_store/g;
+	}
+	my $matched = qr{
+			    ${var}\s*,\s*
+			    \(?\s*$perms_to_match\s*\)?\s*,\s*
+			    NULL\s*,\s*
+			    ${var}_store
+		    }x;
+
+	# Rename this use
+	$file =~ s/\bDEVICE_ATTR\s*\(\s*$matched\s*\)/DEVICE_ATTR_WO(${var})/;
+    }
+    print $file;
+}
-- 
2.15.0

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

* Re: [PATCH] scripts: Add device_attr_<rw|ro|wr>.pl scripts to convert DEVICE_ATTR uses
  2017-12-22 20:02 [PATCH] scripts: Add device_attr_<rw|ro|wr>.pl scripts to convert DEVICE_ATTR uses Joe Perches
@ 2017-12-23 14:19 ` Greg Kroah-Hartman
  2017-12-23 14:24   ` Joe Perches
  2018-01-02 15:34   ` Joe Perches
  0 siblings, 2 replies; 5+ messages in thread
From: Greg Kroah-Hartman @ 2017-12-23 14:19 UTC (permalink / raw)
  To: Joe Perches; +Cc: linux-kernel, Julia Lawall

On Fri, Dec 22, 2017 at 12:02:47PM -0800, Joe Perches wrote:
> There are many uses of the DEVICE_ATTR(var, perms, show, store)
> declaration macro that could use one of the convenience macros
> DEVICE_ATTR_RW, DEVICE_ATTR_RO, or DEVICE_ATTR_WO.
> 
> These scripts automate those conversions.
> 
> Many declarations of functions used to show or store the values
> do not use the <var>_show and <var>_store form.  These scripts
> also check to see if the normal form is unused in the file, that
> the currently named functions are static and only used as the
> function definition and the macro declaration and convert the
> unusual function name to the more normal style.
> 
> Use these scripts with a list of files to be converted.
> 
> e.g.: git ls-files -- "*.c" | xargs perl -i scripts/device_attr_rw.pl
> 
> Here are the current uses of DEVICE_ATTR macros in next-20171222
> 
> $ git grep -w -P "DEVICE_ATTR(_RW|_RO|_WO|)" -- "*.c" | wc -l
> 3393
> $ git grep -w -P "DEVICE_ATTR" -- "*.c" | wc -l
> 2052
> $ git grep -w -P "DEVICE_ATTR_RW" -- "*.c" | wc -l
> 456
> $ git grep -w -P "DEVICE_ATTR_RO" -- "*.c" | wc -l
> 821
> $ git grep -w -P "DEVICE_ATTR_WO" -- "*.c" | wc -l
> 64
> 
> If these scripts on run on that tag, there are 1490 conversions done
> 
> $ git grep -w --name-only DEVICE_ATTR -- "*.c" | \
>   xargs perl -i scripts/device_attr_rw.pl
> $ git grep -w --name-only DEVICE_ATTR -- "*.c" | \
>   xargs perl -i scripts/device_attr_ro.pl
> $ git grep -w --name-only DEVICE_ATTR -- "*.c" | \
>   xargs perl -i scripts/device_attr_wo.pl
> 
> $ git grep -w -P "DEVICE_ATTR(_RW|_RO|_WO|)" -- "*.c" | wc -l
> 3393
> $ git grep -w -P "DEVICE_ATTR" -- "*.c" | wc -l
> 562
> $ git grep -w -P "DEVICE_ATTR_RW" -- "*.c" | wc -l
> 924
> $ git grep -w -P "DEVICE_ATTR_RO" -- "*.c" | wc -l
> 1727
> $ git grep -w -P "DEVICE_ATTR_WO" -- "*.c" | wc -l
> 180
> 
> Signed-off-by: Joe Perches <joe@perches.com>
> 
> 1727
> ith '#' will be ignored, and an empty message aborts the commit.
> ---
>  scripts/device_attr_ro.pl | 57 ++++++++++++++++++++++++++++++++++++++++
>  scripts/device_attr_rw.pl | 66 +++++++++++++++++++++++++++++++++++++++++++++++
>  scripts/device_attr_wo.pl | 57 ++++++++++++++++++++++++++++++++++++++++

We should just fix up all in-kernel users, no need to put the scripts in
the kernel source tree, right?

thanks,

greg k-h

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

* Re: [PATCH] scripts: Add device_attr_<rw|ro|wr>.pl scripts to convert DEVICE_ATTR uses
  2017-12-23 14:19 ` Greg Kroah-Hartman
@ 2017-12-23 14:24   ` Joe Perches
  2018-01-02 15:34   ` Joe Perches
  1 sibling, 0 replies; 5+ messages in thread
From: Joe Perches @ 2017-12-23 14:24 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: linux-kernel, Julia Lawall

On Sat, 2017-12-23 at 15:19 +0100, Greg Kroah-Hartman wrote:
> On Fri, Dec 22, 2017 at 12:02:47PM -0800, Joe Perches wrote:
> > There are many uses of the DEVICE_ATTR(var, perms, show, store)
> > declaration macro that could use one of the convenience macros
> > DEVICE_ATTR_RW, DEVICE_ATTR_RO, or DEVICE_ATTR_WO.
> > 
> > These scripts automate those conversions.
[]
> We should just fix up all in-kernel users, no need to put the scripts in
> the kernel source tree, right?

There's really no need for these scripts in the kernel-tree
just as there's no real need for any coccinelle script once
any conversion is fully done.

Keeping the scripts around can help find more opportunities
for conversion as new code is added.

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

* Re: [PATCH] scripts: Add device_attr_<rw|ro|wr>.pl scripts to convert DEVICE_ATTR uses
  2017-12-23 14:19 ` Greg Kroah-Hartman
  2017-12-23 14:24   ` Joe Perches
@ 2018-01-02 15:34   ` Joe Perches
  2018-01-02 15:47     ` Greg Kroah-Hartman
  1 sibling, 1 reply; 5+ messages in thread
From: Joe Perches @ 2018-01-02 15:34 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: linux-kernel, Julia Lawall

On Sat, 2017-12-23 at 15:19 +0100, Greg Kroah-Hartman wrote:
> On Fri, Dec 22, 2017 at 12:02:47PM -0800, Joe Perches wrote:
> > There are many uses of the DEVICE_ATTR(var, perms, show, store)
> > declaration macro that could use one of the convenience macros
> > DEVICE_ATTR_RW, DEVICE_ATTR_RO, or DEVICE_ATTR_WO.
> > 
> > These scripts automate those conversions.

Hey Greg.  Are you going to use these scripts?

> > Many declarations of functions used to show or store the values
> > do not use the <var>_show and <var>_store form.  These scripts
> > also check to see if the normal form is unused in the file, that
> > the currently named functions are static and only used as the
> > function definition and the macro declaration and convert the
> > unusual function name to the more normal style.
> > 
> > Use these scripts with a list of files to be converted.
> > 
> > e.g.: git ls-files -- "*.c" | xargs perl -i scripts/device_attr_rw.pl
> > 
> > Here are the current uses of DEVICE_ATTR macros in next-20171222
> > 
> > $ git grep -w -P "DEVICE_ATTR(_RW|_RO|_WO|)" -- "*.c" | wc -l
> > 3393
> > $ git grep -w -P "DEVICE_ATTR" -- "*.c" | wc -l
> > 2052
> > $ git grep -w -P "DEVICE_ATTR_RW" -- "*.c" | wc -l
> > 456
> > $ git grep -w -P "DEVICE_ATTR_RO" -- "*.c" | wc -l
> > 821
> > $ git grep -w -P "DEVICE_ATTR_WO" -- "*.c" | wc -l
> > 64
> > 
> > If these scripts on run on that tag, there are 1490 conversions done
> > 
> > $ git grep -w --name-only DEVICE_ATTR -- "*.c" | \
> >   xargs perl -i scripts/device_attr_rw.pl
> > $ git grep -w --name-only DEVICE_ATTR -- "*.c" | \
> >   xargs perl -i scripts/device_attr_ro.pl
> > $ git grep -w --name-only DEVICE_ATTR -- "*.c" | \
> >   xargs perl -i scripts/device_attr_wo.pl
> > 
> > $ git grep -w -P "DEVICE_ATTR(_RW|_RO|_WO|)" -- "*.c" | wc -l
> > 3393
> > $ git grep -w -P "DEVICE_ATTR" -- "*.c" | wc -l
> > 562
> > $ git grep -w -P "DEVICE_ATTR_RW" -- "*.c" | wc -l
> > 924
> > $ git grep -w -P "DEVICE_ATTR_RO" -- "*.c" | wc -l
> > 1727
> > $ git grep -w -P "DEVICE_ATTR_WO" -- "*.c" | wc -l
> > 180
> > 
> > Signed-off-by: Joe Perches <joe@perches.com>
> > 
> > 1727
> > ith '#' will be ignored, and an empty message aborts the commit.
> > ---
> >  scripts/device_attr_ro.pl | 57 ++++++++++++++++++++++++++++++++++++++++
> >  scripts/device_attr_rw.pl | 66 +++++++++++++++++++++++++++++++++++++++++++++++
> >  scripts/device_attr_wo.pl | 57 ++++++++++++++++++++++++++++++++++++++++
> 
> We should just fix up all in-kernel users, no need to put the scripts in
> the kernel source tree, right?
> 
> thanks,
> 
> greg k-h

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

* Re: [PATCH] scripts: Add device_attr_<rw|ro|wr>.pl scripts to convert DEVICE_ATTR uses
  2018-01-02 15:34   ` Joe Perches
@ 2018-01-02 15:47     ` Greg Kroah-Hartman
  0 siblings, 0 replies; 5+ messages in thread
From: Greg Kroah-Hartman @ 2018-01-02 15:47 UTC (permalink / raw)
  To: Joe Perches; +Cc: linux-kernel, Julia Lawall

On Tue, Jan 02, 2018 at 07:34:30AM -0800, Joe Perches wrote:
> On Sat, 2017-12-23 at 15:19 +0100, Greg Kroah-Hartman wrote:
> > On Fri, Dec 22, 2017 at 12:02:47PM -0800, Joe Perches wrote:
> > > There are many uses of the DEVICE_ATTR(var, perms, show, store)
> > > declaration macro that could use one of the convenience macros
> > > DEVICE_ATTR_RW, DEVICE_ATTR_RO, or DEVICE_ATTR_WO.
> > > 
> > > These scripts automate those conversions.
> 
> Hey Greg.  Are you going to use these scripts?

Yes, when I get a chance, kind of busy for the next few weeks with other
stuff though :(

greg k-h

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

end of thread, other threads:[~2018-01-02 15:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-22 20:02 [PATCH] scripts: Add device_attr_<rw|ro|wr>.pl scripts to convert DEVICE_ATTR uses Joe Perches
2017-12-23 14:19 ` Greg Kroah-Hartman
2017-12-23 14:24   ` Joe Perches
2018-01-02 15:34   ` Joe Perches
2018-01-02 15:47     ` Greg Kroah-Hartman

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