Re: [PATCH 4/4] scripts/cvt_style.pl: Tool to reformat sources in various ways

2019-10-10 Thread Joe Perches
On Thu, 2019-10-10 at 13:39 -0700, Kees Cook wrote:
> On Sat, Oct 05, 2019 at 11:35:42PM -0700, Joe Perches wrote:
[]
> > I think clang-format could not do this sort of conversion.
> > Nor could coccinelle or checkpatch.
> > 
> > Anyway, it's not really necessary for this particular patch
> > to be applied, but it's a convenient way to show the script
> > has the capability to do fallthrough comment conversions.
> > 
> > I think it does conversions fairly reasonably but likely
> > some files could not compile without adding an #include
> > like:
> > 
> > #include 
> 
> I think this is a nice tool to add -- at the very least it serves as
> infrastructure for future similar conversions. And small cleanups can be
> generated from it for people looking to clean up subsystems, etc.

Another similar tool that used checkpatch and also compile
tested and created git commits was reformat_with_checkpatch

https://lore.kernel.org/lkml/1405128087.6751.12.camel@joe-AO725/




Re: [PATCH 4/4] scripts/cvt_style.pl: Tool to reformat sources in various ways

2019-10-10 Thread Kees Cook
On Sat, Oct 05, 2019 at 11:35:42PM -0700, Joe Perches wrote:
> On Sat, 2019-10-05 at 19:31 +0200, Miguel Ojeda wrote:
> > Hi Joe,
> 
> Hello.
> 
> > On Sat, Oct 5, 2019 at 6:47 PM Joe Perches  wrote:
> []
> > > As for the commit itself: while I am sure this tool is very useful
> > (and certainly you put a *lot* of effort into this tool), I don't see
> > how it is related to the fallthrough remapping (at least the
> > non-fallthrough parts).
> 
> It's not particularly related.
> 
> It's a 10 year old script that I just extended because it's
> convenient for me.
> 
> I think I first posted it in 2011, but I started it as a
> complement to checkpatch in 2010.
> 
> https://lwn.net/Articles/380161/
> 
> Doing the regexes for the fallthrough conversions took me
> a couple hours.
> 
> > Also, we should consider whether we want more tools like this now or
> > simply put the efforts into moving to clang-format.
> 
> I think clang-format could not do this sort of conversion.
> Nor could coccinelle or checkpatch.
> 
> Anyway, it's not really necessary for this particular patch
> to be applied, but it's a convenient way to show the script
> has the capability to do fallthrough comment conversions.
> 
> I think it does conversions fairly reasonably but likely
> some files could not compile without adding an #include
> like:
> 
> #include 

I think this is a nice tool to add -- at the very least it serves as
infrastructure for future similar conversions. And small cleanups can be
generated from it for people looking to clean up subsystems, etc.

-- 
Kees Cook


Re: [PATCH 4/4] scripts/cvt_style.pl: Tool to reformat sources in various ways

2019-10-09 Thread Nick Desaulniers
On Sat, Oct 5, 2019 at 9:47 AM Joe Perches  wrote:
>
> Trivial tool to reformat source code in various ways.
>
> This is an old tool that was recently updated to convert /* fallthrough */
> style comments to the new pseudo-keyword fallthrough;
>
> Typical command line use is:
> $ perl scripts/cvt_style --convert=fallthrough 

It would be cool to include the treewide onliner from your cover sheet
in this commit message, as I find myself flipping between that and
this, otherwise the recommended onliner will be lost to LKML (instead
of being lost to git log). Or in the usage comment at the top of the
script.

>
> Available conversions:
> all
> printk_to_pr_level
> printk_KERN_DEBUG_to_pr_debug
> dev_printk_to_dev_level
> dev_printk_KERN_DEBUG_to_dev_dbg
> sdev_printk_to_sdev_level
> sdev_printk_KERN_DEBUG_to_sdev_dbg
> coalesce_formats
> cuddle_open_brace
> cuddle_else

I think some of these could use examples of what they do. I can't read
perl (as we've previously established :P) and I'm not sure what it
means to cuddle open braces or elses, though they do sound nice.

> deparenthesize_returns
> space_after_KERN_level
> space_after_if_while_for_switch
> space_after_for_semicolons
> space_after_comma
> space_before_pointer
> space_around_trigraph
> leading_spaces_to_tabs
> coalesce_semicolons
> remove_trailing_whitespace
> remove_whitespace_before_quoted_newline
> remove_whitespace_before_trailing_semicolon
> remove_whitespace_before_bracket
> remove_parenthesis_whitespace
> remove_single_statement_braces
> remove_whitespace_after_cast
> hoist_assigns_from_if
> convert_c99_comments
> remove_private_data_casts
> remove_static_initializations_to_0
> remove_true_false_comparisons
> remove_NULL_comparisons
> remove_trailing_if_semicolons

To Miguel's comment about clang-format, it looks like you can do:

Use -style="{key: value, ...}" to set specific
  parameters, e.g.:
-style="{BasedOnStyle: llvm, IndentWidth: 8}"

via: https://clang.llvm.org/docs/ClangFormat.html
which might make some nice one liners for some of these.


> network_comments
> remove_switchforwhileif_semicolons
> detab_else_return
> remove_while_while
> fallthrough
> Additional conversions which may not work well:
> (enable individually or with --convert=all --broken)
> move_labels_to_column_1
> space_around_logical_tests
> space_around_assign
> space_around_arithmetic
> CamelCase_to_camel_case

s/camel_case/snake_case/

I'll give the script a run later this week and report back if I can
find any errors in the resulting build, as in the previous patch
series. Thanks for the work on this.
-- 
Thanks,
~Nick Desaulniers


Re: [PATCH 4/4] scripts/cvt_style.pl: Tool to reformat sources in various ways

2019-10-06 Thread Joe Perches
On Sat, 2019-10-05 at 19:31 +0200, Miguel Ojeda wrote:
> Hi Joe,

Hello.

> On Sat, Oct 5, 2019 at 6:47 PM Joe Perches  wrote:
[]
> > As for the commit itself: while I am sure this tool is very useful
> (and certainly you put a *lot* of effort into this tool), I don't see
> how it is related to the fallthrough remapping (at least the
> non-fallthrough parts).

It's not particularly related.

It's a 10 year old script that I just extended because it's
convenient for me.

I think I first posted it in 2011, but I started it as a
complement to checkpatch in 2010.

https://lwn.net/Articles/380161/

Doing the regexes for the fallthrough conversions took me
a couple hours.

> Also, we should consider whether we want more tools like this now or
> simply put the efforts into moving to clang-format.

I think clang-format could not do this sort of conversion.
Nor could coccinelle or checkpatch.

Anyway, it's not really necessary for this particular patch
to be applied, but it's a convenient way to show the script
has the capability to do fallthrough comment conversions.

I think it does conversions fairly reasonably but likely
some files could not compile without adding an #include
like:

#include 




Re: [PATCH 4/4] scripts/cvt_style.pl: Tool to reformat sources in various ways

2019-10-05 Thread Miguel Ojeda
Hi Joe,

On Sat, Oct 5, 2019 at 6:47 PM Joe Perches  wrote:
>
> diff --git a/scripts/cvt_style.pl b/scripts/cvt_style.pl
> new file mode 100755
> index ..fcbda0b1c67a
> --- /dev/null
> +++ b/scripts/cvt_style.pl
> @@ -0,0 +1,808 @@
> +#!/usr/bin/perl -w

Nit: #!/usr/bin/env perl instead?

> +
> +# Change some style elements of a source file
> +# An imperfect source code formatter.
> +# Might make trivial patches a bit easier.
> +#
> +# usage: perl scripts/cvt_kernel_style.pl 
> +#
> +# Licensed under the terms of the GNU GPL License version 2

Nit: use # SPDX-License-Identifier: GPL-2.0-only instead

As for the commit itself: while I am sure this tool is very useful
(and certainly you put a *lot* of effort into this tool), I don't see
how it is related to the fallthrough remapping (at least the
non-fallthrough parts).

Also, we should consider whether we want more tools like this now or
simply put the efforts into moving to clang-format.

Cheers,
Miguel


[PATCH 4/4] scripts/cvt_style.pl: Tool to reformat sources in various ways

2019-10-05 Thread Joe Perches
Trivial tool to reformat source code in various ways.

This is an old tool that was recently updated to convert /* fallthrough */
style comments to the new pseudo-keyword fallthrough;

Typical command line use is:
$ perl scripts/cvt_style --convert=fallthrough 

Available conversions:
all
printk_to_pr_level
printk_KERN_DEBUG_to_pr_debug
dev_printk_to_dev_level
dev_printk_KERN_DEBUG_to_dev_dbg
sdev_printk_to_sdev_level
sdev_printk_KERN_DEBUG_to_sdev_dbg
coalesce_formats
cuddle_open_brace
cuddle_else
deparenthesize_returns
space_after_KERN_level
space_after_if_while_for_switch
space_after_for_semicolons
space_after_comma
space_before_pointer
space_around_trigraph
leading_spaces_to_tabs
coalesce_semicolons
remove_trailing_whitespace
remove_whitespace_before_quoted_newline
remove_whitespace_before_trailing_semicolon
remove_whitespace_before_bracket
remove_parenthesis_whitespace
remove_single_statement_braces
remove_whitespace_after_cast
hoist_assigns_from_if
convert_c99_comments
remove_private_data_casts
remove_static_initializations_to_0
remove_true_false_comparisons
remove_NULL_comparisons
remove_trailing_if_semicolons
network_comments
remove_switchforwhileif_semicolons
detab_else_return
remove_while_while
fallthrough
Additional conversions which may not work well:
(enable individually or with --convert=all --broken)
move_labels_to_column_1
space_around_logical_tests
space_around_assign
space_around_arithmetic
CamelCase_to_camel_case

Use --convert=(comma separated list)
   ie: --convert=printk_to_pr_level,coalesce_formats

Output file:
  --overwrite => write the changes to the source file
  --suffix => suffix to append to new file (default: .style)

Signed-off-by: Joe Perches 
---
 scripts/cvt_style.pl | 808 +++
 1 file changed, 808 insertions(+)
 create mode 100755 scripts/cvt_style.pl

diff --git a/scripts/cvt_style.pl b/scripts/cvt_style.pl
new file mode 100755
index ..fcbda0b1c67a
--- /dev/null
+++ b/scripts/cvt_style.pl
@@ -0,0 +1,808 @@
+#!/usr/bin/perl -w
+
+# Change some style elements of a source file
+# An imperfect source code formatter.
+# Might make trivial patches a bit easier.
+#
+# usage: perl scripts/cvt_kernel_style.pl 
+#
+# Licensed under the terms of the GNU GPL License version 2
+
+use strict;
+use Getopt::Long qw(:config no_auto_abbrev);
+
+my $P = $0;
+my $V = '0.4';
+
+my $source_indent = 8;
+my $quiet = 0;
+my $stats = 1;
+my $overwrite = 0;
+my $modified = 0;
+my $suffix = ".style";
+my $convert_options = "";
+my $broken = 0;
+
+my @std_options = (
+"all",
+"printk_to_pr_level",
+"printk_KERN_DEBUG_to_pr_debug",
+"dev_printk_to_dev_level",
+"dev_printk_KERN_DEBUG_to_dev_dbg",
+"sdev_printk_to_sdev_level",
+"sdev_printk_KERN_DEBUG_to_sdev_dbg",
+"coalesce_formats",
+"cuddle_open_brace",
+"cuddle_else",
+"deparenthesize_returns",
+"space_after_KERN_level",
+"space_after_if_while_for_switch",
+"space_after_for_semicolons",
+"space_after_comma",
+"space_before_pointer",
+"space_around_trigraph",
+"leading_spaces_to_tabs",
+"coalesce_semicolons",
+"remove_trailing_whitespace",
+"remove_whitespace_before_quoted_newline",
+"remove_whitespace_before_trailing_semicolon",
+"remove_whitespace_before_bracket",
+"remove_parenthesis_whitespace",
+"remove_single_statement_braces",
+"remove_whitespace_after_cast",
+"hoist_assigns_from_if",
+"convert_c99_comments",
+"remove_private_data_casts",
+"remove_static_initializations_to_0",
+"remove_true_false_comparisons",
+"remove_NULL_comparisons",
+"remove_trailing_if_semicolons",
+"network_comments",
+"remove_switchforwhileif_semicolons",
+"detab_else_return",
+"remove_while_while",
+"fallthrough",
+);
+
+my @other_options = (
+"move_labels_to_column_1",
+"space_around_logical_tests",
+"space_around_assign",
+"space_around_arithmetic",
+"CamelCase_to_camel_case"
+);
+
+my $version = 0;
+my $help = 0;
+
+my $logFunctions = qr{(?x:
+   printk|
+   
([a-z0-9_]+)_(debug|dbg|vdbg|devel|info|warn|warning|err|notice|alert|crit|emerg|cont)|
+   WARN|
+   panic
+)};
+
+my $match_balanced_parentheses = qr/(\((?:[^\(\)]++|(?-1))*\))/;
+my $match_balanced_braces  = qr/(\{(?:[^\{\}]++|(?-1))*\})/;
+my $do_cvt;
+
+my %hash;
+
+sub set_all_options {
+my ($enabled) = @_;
+
+foreach my $opt (@std_options) {
+   $hash{$opt} = $enabled;
+}
+
+if ($broken > 0 || $enabled == -1) {
+   foreach my $opt (@other_options) {
+   $hash{$opt} =