netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Andy Whitcroft <apw@shadowen.org>
Cc: "Allan, Bruce W" <bruce.w.allan@intel.com>,
	David Miller <davem@davemloft.net>,
	"andrei.emeltchenko.news@gmail.com"
	<andrei.emeltchenko.news@gmail.com>,
	"linville@tuxdriver.com" <linville@tuxdriver.com>,
	"linux-wireless@vger.kernel.org" <linux-wireless@vger.kernel.org>,
	"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: [PATCH v2] checkpatch: Add some --strict coding style checks
Date: Fri, 24 Feb 2012 10:37:55 -0800	[thread overview]
Message-ID: <1330108675.18464.21.camel@joe2Laptop> (raw)
In-Reply-To: <804857E1F29AAC47BF68C404FC60A1842A1D36@ORSMSX102.amr.corp.intel.com>

Argument alignment across multiple lines should
match the open parenthesis.

Logical continuations should be at the end of
the previous line, not the start of a new line.

These are not required by CodingStyle so make the
tests active only when using --strict.

Improved_by_examples_from: "Bruce W. Allen" <bruce.w.allan@intel.com>
Signed-off-by: Joe Perches <joe@perches.com>

---

V2: Scan the entire line for balanced parentheses,
    use the position of the last non-balanced open parenthesis.
    Allow all space indentation too, checkpatch will complain
    in a different message about that if it's too long.

 scripts/checkpatch.pl |   65 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 89d24b3..7a0514b 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -330,10 +330,11 @@ sub build_types {
 }
 build_types();
 
-our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
+our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
+our $lval_parens = qr/(\((?:[^\(\)]+|(-1))*\))/;
 
 our $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
-our $LvalOrFunc	= qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
+our $LvalOrFunc	= qr{($Lval)\s*($lval_parens{0,1})\s*};
 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
 
 sub deparenthesize {
@@ -1330,6 +1331,36 @@ sub check_absolute_file {
 	}
 }
 
+sub pos_last_openparen {
+	my ($line) = @_;
+
+	my $pos = 0;
+
+	my $opens = $line =~ tr/\(/\(/;
+	my $closes = $line =~ tr/\)/\)/;
+
+	my $last_openparen = 0;
+
+	if (($opens == 0) || ($closes >= $opens)) {
+		return -1;
+	}
+
+	my $len = length($line);
+
+	for ($pos = 0; $pos < $len; $pos++) {
+		my $string = substr($line, $pos);
+		if ($string =~ /^($FuncArg|$balanced_parens)/) {
+			$pos += length($1);
+		}
+
+		if (substr($line, $pos, 1) eq '(') {
+			$last_openparen = $pos;
+		}
+	}
+
+	return $last_openparen + 1;
+}
+
 sub process {
 	my $filename = shift;
 
@@ -1783,6 +1814,36 @@ 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*$/) {
+			$prevline =~ /^\+(\t*)(.*)$/;
+			my $oldindent = $1;
+			my $rest = $2;
+
+			my $pos = pos_last_openparen($rest);
+			if ($pos >= 0) {
+				$line =~ /^\+([ \t]*)/;
+				my $newindent = $1;
+
+				my $goodtabindent = $oldindent .
+					"\t" x ($pos / 8) .
+					" "  x ($pos % 8);
+				my $goodspaceindent = $oldindent . " "  x $pos;
+
+				if ($newindent ne $goodtabindent &&
+				    $newindent ne $goodspaceindent) {
+					CHK("PARENTHESIS_ALIGNMENT",
+					    "Alignment should match open parenthesis\n" . $hereprev);
+				}
+			}
+		}
+
 # check for spaces at the beginning of a line.
 # Exceptions:
 #  1) within comments

  reply	other threads:[~2012-02-24 18:37 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-20 20:37 pull request: wireless 2012-02-20 John W. Linville
     [not found] ` <20120220203740.GD6740-2XuSBdqkA4R54TAoqtyWWQ@public.gmane.org>
2012-02-21  0:23   ` David Miller
     [not found]     ` <20120220.192324.498901675497866305.davem-fT/PcQaiUtIeIZ0/mPfg9Q@public.gmane.org>
2012-02-21 15:14       ` John W. Linville
2012-02-21 19:44         ` David Miller
2012-02-21 20:38           ` Andrei Emeltchenko
2012-02-21 20:40             ` David Miller
     [not found]               ` <20120221.154053.2103818562080068513.davem-fT/PcQaiUtIeIZ0/mPfg9Q@public.gmane.org>
2012-02-21 20:59                 ` [PATCH] checkpatch: Add some --strict coding style checks Joe Perches
2012-02-21 21:09                   ` Andrew Morton
2012-02-21 21:11                     ` Joe Perches
2012-02-21 22:09                   ` Allan, Bruce W
2012-02-21 23:16                     ` Joe Perches
2012-02-22  1:36                     ` Joe Perches
2012-02-22  1:56                       ` Allan, Bruce W
2012-02-22  1:58                         ` Joe Perches
2012-02-22  2:17                           ` Allan, Bruce W
     [not found]                             ` <804857E1F29AAC47BF68C404FC60A18429B98A-P5GAC/sN6hlcIJlls4ac1rfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2012-02-23  3:43                               ` Joe Perches
2012-02-24 18:22                                 ` Allan, Bruce W
2012-02-24 18:37                                   ` Joe Perches [this message]
2012-02-29 14:38                                     ` [PATCH v2] " Kalle Valo
     [not found]                     ` <804857E1F29AAC47BF68C404FC60A18429B3EE-P5GAC/sN6hlcIJlls4ac1rfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2012-02-22  9:35                       ` [PATCH] " David Laight
2012-02-22  9:46                         ` Johannes Berg
2012-02-22 10:07                 ` pull request: wireless 2012-02-20 Zefir Kurtisi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1330108675.18464.21.camel@joe2Laptop \
    --to=joe@perches.com \
    --cc=akpm@linux-foundation.org \
    --cc=andrei.emeltchenko.news@gmail.com \
    --cc=apw@shadowen.org \
    --cc=bruce.w.allan@intel.com \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=linville@tuxdriver.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).