diff mbox

checkpatch: Add some --strict coding style checks

Message ID 1329874581.5143.22.camel@joe2Laptop
State Not Applicable, archived
Delegated to: David Miller
Headers show

Commit Message

Joe Perches Feb. 22, 2012, 1:36 a.m. UTC
On Tue, 2012-02-21 at 22:09 +0000, Allan, Bruce W wrote:
> This appears to falsely complain about parenthesis alignment in
> conditional statements with multiple opening parentheses.

Hey Allan.

Can you try this one please?
---
 scripts/checkpatch.pl |   58 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 58 insertions(+), 0 deletions(-)



--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Allan, Bruce W Feb. 22, 2012, 1:56 a.m. UTC | #1
> -----Original Message-----

> From: Joe Perches [mailto:joe@perches.com]

> Sent: Tuesday, February 21, 2012 5:36 PM

> To: Allan, Bruce W

> Cc: David Miller; Andy Whitcroft; Andrew Morton;

> andrei.emeltchenko.news@gmail.com; linville@tuxdriver.com; linux-

> wireless@vger.kernel.org; netdev@vger.kernel.org; linux-

> kernel@vger.kernel.org

> Subject: RE: [PATCH] checkpatch: Add some --strict coding style checks

> 

> On Tue, 2012-02-21 at 22:09 +0000, Allan, Bruce W wrote:

> > This appears to falsely complain about parenthesis alignment in

> > conditional statements with multiple opening parentheses.

> 

> Hey Allan.

> 

> Can you try this one please?

> ---

>  scripts/checkpatch.pl |   58

> +++++++++++++++++++++++++++++++++++++++++++++++++

>  1 files changed, 58 insertions(+), 0 deletions(-)

> 

> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl

> index a3b9782..e95419e 100755

> --- a/scripts/checkpatch.pl

> +++ b/scripts/checkpatch.pl

> @@ -1330,6 +1330,35 @@ sub check_absolute_file {

>  	}

>  }

> 

> +sub pos_last_openparen {

> +

> +	my ($args) = @_;

> +

> +	my $pos = 0;

> +	my $len = length($args);

> +

> +	my $opens = $args =~ tr/\(/\(/;

> +	my $closes = $args =~ tr/\)/\)/;

> +

> +	my $last_openparen = 0;

> +

> +	if (($opens == 0) || ($closes >= $opens)) {

> +		return 0;

> +	}

> +

> +	for ($pos = 0; $pos < $len; $pos++) {

> +		if (substr($args, $pos) =~ /^($FuncArg)/) {

> +			$pos += length($1);

> +		}

> +

> +		if (substr($args, $pos, 1) eq '(') {

> +			$last_openparen = $pos;

> +		}

> +	}

> +

> +	return $last_openparen + 1;

> +}

> +

>  sub process {

>  	my $filename = shift;

> 

> @@ -1783,6 +1812,35 @@ sub process {

>  			     "please, no space before tabs\n" . $herevet);

>  		}

> 

> +# check for && or || at the start of a line

> +		if ($rawline =~ /^\+\s*(&&|\|\|)/) {

> +			CHK("LOGICAL_CONTINUATIONS",

> +			    "Logical continuations should be on the previous

> line\n" . $hereprev);

> +		}

> +

> +# check multi-line statement indentation matches previous line

> +		if ($prevline =~ /^\+(\t*)(if

> \(|$Ident\().*(\&\&|\|\||,)\s*$/ && $rawline =~ /^\+([ \t]*)/) {

> +			$prevline =~ /^\+(\t*)(if

> \(|$Ident\()(.*)(\&\&|\|\||,)\s*$/;

> +			my $oldindent = $1;

> +			my $if_or_func = $2;

> +			my $args = $3;

> +

> +			my $pos = pos_last_openparen($args);

> +

> +			my $len = length($if_or_func) + $pos;

> +			$rawline =~ /^\+([ \t]*)/;

> +			my $newindent = $1;

> +

> +			my $goodindent = $oldindent .

> +					 "\t" x ($len / 8) .

> +					 " "  x ($len % 8);

> +

> +			if ($newindent ne $goodindent) {

> +				CHK("PARENTHESIS_ALIGNMENT",

> +				    "Alignment should match open

> parenthesis\n" . $hereprev);

> +			}

> +		}

> +

>  # check for spaces at the beginning of a line.

>  # Exceptions:

>  #  1) within comments

> 


It's better, but there are still instances of false hits AFAICT.

Bruce.
Joe Perches Feb. 22, 2012, 1:58 a.m. UTC | #2
On Wed, 2012-02-22 at 01:56 +0000, Allan, Bruce W wrote:
> > On Tue, 2012-02-21 at 22:09 +0000, Allan, Bruce W wrote:
> > > This appears to falsely complain about parenthesis alignment in
> > > conditional statements with multiple opening parentheses.
> > Can you try this one please?
[]
> It's better, but there are still instances of false hits AFAICT.

Do you have any examples?
The only one I noticed was spaces for alignment instead of tabs.
I think that's not a false hit myself.


--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index a3b9782..e95419e 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1330,6 +1330,35 @@  sub check_absolute_file {
 	}
 }
 
+sub pos_last_openparen {
+
+	my ($args) = @_;
+
+	my $pos = 0;
+	my $len = length($args);
+
+	my $opens = $args =~ tr/\(/\(/;
+	my $closes = $args =~ tr/\)/\)/;
+
+	my $last_openparen = 0;
+
+	if (($opens == 0) || ($closes >= $opens)) {
+		return 0;
+	}
+
+	for ($pos = 0; $pos < $len; $pos++) {
+		if (substr($args, $pos) =~ /^($FuncArg)/) {
+			$pos += length($1);
+		}
+
+		if (substr($args, $pos, 1) eq '(') {
+			$last_openparen = $pos;
+		}
+	}
+
+	return $last_openparen + 1;
+}
+
 sub process {
 	my $filename = shift;
 
@@ -1783,6 +1812,35 @@  sub process {
 			     "please, no space before tabs\n" . $herevet);
 		}
 
+# check for && or || at the start of a line
+		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
+			CHK("LOGICAL_CONTINUATIONS",
+			    "Logical continuations should be on the previous line\n" . $hereprev);
+		}
+
+# check multi-line statement indentation matches previous line
+		if ($prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/ && $rawline =~ /^\+([ \t]*)/) {
+			$prevline =~ /^\+(\t*)(if \(|$Ident\()(.*)(\&\&|\|\||,)\s*$/;
+			my $oldindent = $1;
+			my $if_or_func = $2;
+			my $args = $3;
+
+			my $pos = pos_last_openparen($args);
+
+			my $len = length($if_or_func) + $pos;
+			$rawline =~ /^\+([ \t]*)/;
+			my $newindent = $1;
+
+			my $goodindent = $oldindent .
+					 "\t" x ($len / 8) .
+					 " "  x ($len % 8);
+
+			if ($newindent ne $goodindent) {
+				CHK("PARENTHESIS_ALIGNMENT",
+				    "Alignment should match open parenthesis\n" . $hereprev);
+			}
+		}
+
 # check for spaces at the beginning of a line.
 # Exceptions:
 #  1) within comments