[PATCH] scripts: Add device_attr_<rw|ro|wr>.pl scripts to convert DEVICE_ATTR uses
From: Joe Perches
Date: Fri Dec 22 2017 - 15:03:00 EST
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@xxxxxxxxxxx>
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@xxxxxxxxxxx>
+
+# 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@xxxxxxxxxxx>
+
+# 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@xxxxxxxxxxx>
+
+# 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