Skip to contentSkip to conversation

update checkpatch to version 0.26

PATCH In review Andy Whitcroft 2 participants 15 messages Dec 7, 2008 – Dec 9, 2008 Checkpatch

lore
Share & tools
lore
#0 lore raw

cover update checkpatch to version 0.26

This update brings a couple of new checks, and a number of fixes. Of note:

- allows parentheses on return of comparisons
- tracks #ifdef sections better
- fixes miss reports of variables containing static

It also updates the maintainers entry to track my new email address and to drop inactive maintainers.

Complete changelog below.

-apw

Andy Whitcroft (11):
checkpatch: update MAINTAINERS entry
checkpatch: update copyrights
checkpatch: allow parentheses on return for comparisons
checkpatch: loosen spacing on typedef function checks
checkpatch: fix continuation detection when handling spacing on operators
checkpatch: track #ifdef/#else/#endif when tracking blocks
checkpatch: do not report nr_static as a static declaration
checkpatch: ensure we actually detect if assignments split across lines
checkpatch: struct file_operations should normally be const
checkpatch: fix the perlcritic errors
checkpatch: version: 0.26

Mike Frysinger (1):
checkpatch: try to catch missing VMLINUX_SYMBOL() in vmlinux.lds.h

Wolfram Sang (1):
checkpatch: Add warning for p0-patches

 scripts/checkpatch.pl |   84 +++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 67 insertions(+), 17 deletions(-)
#1 lore raw

PATCH 1/13 checkpatch: update MAINTAINERS entry

Update my email address to my new work address. Also, as per our recent email conversation remove Randy and Joel from the maintainers list. Finally add LKML so that emails are recorded somewhere.

Cc Randy Dunlap <[email protected]>
Cc Joel Schopp <[email protected]>
---
 MAINTAINERS |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
MAINTAINERS +2 -5
index 618c1ef..0d587ff 100644
@@ -1091,11 +1091,8 @@ S:	Maintained
 
 CHECKPATCH
 P:	Andy Whitcroft
-M:	[email protected]
-P:	Randy Dunlap
-M:	[email protected]
-P:	Joel Schopp
-M:	[email protected]
+M:	[email protected]
+L:	[email protected]
 S:	Supported
 
 CISCO 10G ETHERNET DRIVER
Signature · 1 line
1.6.0.4.911.gc990
#2 lore raw

PATCH 2/13 checkpatch: update copyrights

In reply to Andy Whitcroft

From: Andy Whitcroft <[email protected]>

---
 scripts/checkpatch.pl |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
scripts/checkpatch.pl +2 -1
index 770fa41..f015698 100755
@@ -1,7 +1,8 @@
 #!/usr/bin/perl -w
 # (c) 2001, Dave Jones. <[email protected]> (the file handling bit)
 # (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
-# (c) 2007, Andy Whitcroft <[email protected]> (new conditions, test suite, etc)
+# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
+# (c) 2008, Andy Whitcroft <[email protected]>
 # Licensed under the terms of the GNU GPL License version 2
 
 use strict;
Signature · 1 line
1.6.0.4.911.gc990
#3 lore raw

PATCH 3/13 checkpatch: Add warning for p0-patches

In reply to Andy Whitcroft

From: Wolfram Sang <[email protected]>

Some people work internally with -p0-patches which has the danger that one forgets to convert them to -p1 before mainlining. Bitten myself and seen p0-patches in mailing lists occasionally, this patch adds a warning to checkpatch.pl in case a patch is -p0. If you really want, you can fool this check to generate false positives, this is why it just spits a warning. Making the check 100% proof is trickier than it looks, so let's start with a version which catches the cases of real use.

[[email protected]: update message language, handle null prefix, add tests]
---
 scripts/checkpatch.pl |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
scripts/checkpatch.pl +7 -1
index f015698..b953c76 100755
@@ -1057,6 +1057,7 @@ sub process {
 	my $in_comment = 0;
 	my $comment_edge = 0;
 	my $first_line = 0;
+	my $p1_prefix = '';
 
 	my $prev_values = 'E';
 
@@ -1205,7 +1206,12 @@ sub process {
 		# extract the filename as it passes
 		if ($line=~/^\+\+\+\s+(\S+)/) {
 			$realfile = $1;
-			$realfile =~ s@^[^/]*/@@;
+			$realfile =~ s@^([^/]*)/@@;
+
+			$p1_prefix = $1;
+			if ($tree && $p1_prefix ne '' && -e "$root/$p1_prefix") {
+				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
+			}
 
 			if ($realfile =~ m@^include/asm/@) {
 				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
Signature · 1 line
1.6.0.4.911.gc990
#4 lore raw

PATCH 4/13 checkpatch: allow parentheses on return for comparisons

In reply to Andy Whitcroft

From: Andy Whitcroft <[email protected]>

It seems to be a common idiom to include braces on conditionals in all contexts including return. Allow this exception to the return is not a function checks. Reported by Kay Sievers.

---
 scripts/checkpatch.pl |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +3 -2
index b953c76..5c7fd1a 100755
@@ -127,6 +127,7 @@ our $Lval	= qr{$Ident(?:$Member)*};
 
 our $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
 our $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
+our $Compare    = qr{<=|>=|==|!=|<|>};
 our $Operators	= qr{
 			<=|>=|==|!=|
 			=>|->|<<|>>|<|>|!|~|
@@ -1983,9 +1984,9 @@ sub process {
 			my $spacing = $1;
 			my $value = $2;
 
-			# Flatten any parentheses and braces
+			# Flatten any parentheses
 			$value =~ s/\)\(/\) \(/g;
-			while ($value =~ s/\([^\(\)]*\)/1/) {
+			while ($value !~ /(?:$Ident|-?$Constant)\s*$Compare\s*(?:$Ident|-?$Constant)/ && $value =~ s/\([^\(\)]*\)/1/) {
 			}
 
 			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
Signature · 1 line
1.6.0.4.911.gc990
#5 lore raw

PATCH 5/13 checkpatch: try to catch missing VMLINUX_SYMBOL() in vmlinux.lds.h

In reply to Andy Whitcroft

From: Mike Frysinger <[email protected]>

Seems like every other release we have someone who updates vmlinux.lds.h and adds C-visible symbols without VMLINUX_SYMBOL() around them. So start checking the file and reject assignments which have plain symbols on either side.

[[email protected]: soften the check, add tests]
---
 scripts/checkpatch.pl |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)
scripts/checkpatch.pl +9 -0
index 5c7fd1a..705a043 100755
@@ -2224,6 +2224,15 @@ sub process {
 			}
 		}
 
+# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
+# all assignments may have only one of the following with an assignment:
+#	.
+#	ALIGN(...)
+#	VMLINUX_SYMBOL(...)
+		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
+			WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
+		}
+
 # check for redundant bracing round if etc
 		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
 			my ($level, $endln, @chunks) =
Signature · 1 line
1.6.0.4.911.gc990
#6 lore raw

PATCH 6/13 checkpatch: loosen spacing on typedef function checks

In reply to Andy Whitcroft

From: Andy Whitcroft <[email protected]>

Loosen spacing checks to correctly detect this valid use of a typedef:

	typedef struct rcu_data *(*get_data_func)(int);
---
 scripts/checkpatch.pl |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
scripts/checkpatch.pl +1 -1
index 705a043..d80b55a 100755
@@ -1618,7 +1618,7 @@ sub process {
 # check for new typedefs, only function parameters and sparse annotations
 # make sense.
 		if ($line =~ /\btypedef\s/ &&
-		    $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
+		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
 		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
 		    $line !~ /\b$typeTypedefs\b/ &&
 		    $line !~ /\b__bitwise(?:__|)\b/) {
Signature · 1 line
1.6.0.4.911.gc990
#7 lore raw

PATCH 7/13 checkpatch: fix continuation detection when handling spacing on operators

In reply to Andy Whitcroft

From: Andy Whitcroft <[email protected]>

We are miss categorising a continuation fragment following an operator which may lead to us thinking that there is a space after it when there is not. Fix this up.

---
 scripts/checkpatch.pl |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
scripts/checkpatch.pl +1 -1
index d80b55a..67b0c9f 100755
@@ -1793,7 +1793,7 @@ sub process {
 					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
 					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
 					$c = 'O' if ($elements[$n + 2] eq '');
-					$c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
+					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
 				} else {
 					$c = 'E';
 				}
Signature · 1 line
1.6.0.4.911.gc990
#8 lore raw

PATCH 8/13 checkpatch: track #ifdef/#else/#endif when tracking blocks

In reply to Andy Whitcroft

From: Andy Whitcroft <[email protected]>

When picking up a complete statement or block for analysis we cannot simply track open/close/etc parenthesis we must take into account preprocessor section boundaries.

---
 scripts/checkpatch.pl |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)
scripts/checkpatch.pl +22 -0
index 67b0c9f..906624c 100755
@@ -405,6 +405,7 @@ sub ctx_statement_block {
 
 	my $type = '';
 	my $level = 0;
+	my @stack = ([$type, $level]);
 	my $p;
 	my $c;
 	my $len = 0;
@@ -436,6 +437,16 @@ sub ctx_statement_block {
 		$remainder = substr($blk, $off);
 
 		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
+
+		# Handle nested #if/#else.
+		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
+			push(@stack, [ $type, $level ]);
+		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
+			($type, $level) = @{$stack[$#stack - 1]};
+		} elsif ($remainder =~ /^#\s*endif\b/) {
+			($type, $level) = @{pop(@stack)};
+		}
+
 		# Statement ends at the ';' or a close '}' at the
 		# outermost level.
 		if ($level == 0 && $c eq ';') {
@@ -582,11 +593,22 @@ sub ctx_block_get {
 	my @res = ();
 
 	my $level = 0;
+	my @stack = ($level);
 	for ($line = $start; $remain > 0; $line++) {
 		next if ($rawlines[$line] =~ /^-/);
 		$remain--;
 
 		$blk .= $rawlines[$line];
+
+		# Handle nested #if/#else.
+		if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
+			push(@stack, $level);
+		} elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
+			$level = $stack[$#stack - 1];
+		} elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
+			$level = pop(@stack);
+		}
+
 		foreach my $c (split(//, $rawlines[$line])) {
 			##print "C<$c>L<$level><$open$close>O<$off>\n";
 			if ($off > 0) {
Signature · 1 line
1.6.0.4.911.gc990
#9 lore raw

PATCH 9/13 checkpatch: do not report nr_static as a static declaration

In reply to Andy Whitcroft

From: Andy Whitcroft <[email protected]>

Ensure we do not report identifiers containing the word static as static declarations. For example this should not be reported as an unecessary assignement of 0:

	long nr_static = 0;
---
 scripts/checkpatch.pl |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
scripts/checkpatch.pl +1 -1
index 906624c..a521d49 100755
@@ -1632,7 +1632,7 @@ sub process {
 				$herecurr);
 		}
 # check for static initialisers.
-		if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
+		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
 			ERROR("do not initialise statics to 0 or NULL\n" .
 				$herecurr);
 		}
Signature · 1 line
1.6.0.4.911.gc990
#10 lore raw

PATCH 10/13 checkpatch: ensure we actually detect if assignments split across lines

In reply to Andy Whitcroft

From: Andy Whitcroft <[email protected]>

When checking for assignments within if conditionals we check the whole of the condition, but the match is performed using a line constrained regular expression. This means we can miss split conditionals or those on the second line. Allow the check to span lines.

---
 scripts/checkpatch.pl |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
scripts/checkpatch.pl +1 -1
index a521d49..c39ce0b 100755
@@ -2048,7 +2048,7 @@ sub process {
 		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
 			my ($s, $c) = ($stat, $cond);
 
-			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
+			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
 				ERROR("do not use assignment in if condition\n" . $herecurr);
 			}
 
Signature · 1 line
1.6.0.4.911.gc990
#11 lore raw

PATCH 11/13 checkpatch: struct file_operations should normally be const

In reply to Andy Whitcroft

From: Andy Whitcroft <[email protected]>

In the general use case struct file_operations should be a const object. Check for and warn where it is not. As suggested by Steven and Ingo.

Cc Steven Rostedt <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 scripts/checkpatch.pl |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
scripts/checkpatch.pl +5 -0
index c39ce0b..94371f6 100755
@@ -2509,6 +2509,11 @@ sub process {
 		if ($line =~ /^.\s*__initcall\s*\(/) {
 			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
 		}
+# check for struct file_operations, ensure they are const.
+		if ($line =~ /\bstruct\s+file_operations\b/ &&
+		    $line !~ /\bconst\b/) {
+			WARN("struct file_operations should normally be const\n" . $herecurr);
+		}
 
 # use of NR_CPUS is usually wrong
 # ignore definitions of NR_CPUS and usage to define arrays as likely right
Signature · 1 line
1.6.0.4.911.gc990
Acked-by on v1 11/13
#12 lore raw

Re: [PATCH 11/13] checkpatch: struct file_operations should normally be const

Quoted 4 lines · Andy Whitcroft

From: Andy Whitcroft <[email protected]>

In the general use case struct file_operations should be a const object. Check for and warn where it is not. As suggested by Steven and Ingo.

Andy,

Thanks for doing this!

-- Steve

Quoted 33 lines
Cc Steven Rostedt <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 scripts/checkpatch.pl |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
scripts/checkpatch.pl +5 -0
index c39ce0b..94371f6 100755
@@ -2509,6 +2509,11 @@ sub process {
 		if ($line =~ /^.\s*__initcall\s*\(/) {
 			WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
 		}
+# check for struct file_operations, ensure they are const.
+		if ($line =~ /\bstruct\s+file_operations\b/ &&
+		    $line !~ /\bconst\b/) {
+			WARN("struct file_operations should normally be const\n" . $herecurr);
+		}
 
 # use of NR_CPUS is usually wrong
 # ignore definitions of NR_CPUS and usage to define arrays as likely right
Signature · 7 lines
1.6.0.4.911.gc990

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
#13 lore raw

PATCH 12/13 checkpatch: fix the perlcritic errors

In reply to Andy Whitcroft

From: Andy Whitcroft <[email protected]>

Clean up checkpatch using perlcritic.

---
 scripts/checkpatch.pl |   20 +++++++++++++-------
 1 files changed, 13 insertions(+), 7 deletions(-)
scripts/checkpatch.pl +13 -7
index 94371f6..bd6ac90 100755
@@ -69,7 +69,9 @@ my $dbg_possible = 0;
 my $dbg_type = 0;
 my $dbg_attr = 0;
 for my $key (keys %debug) {
-	eval "\${dbg_$key} = '$debug{$key}';"
+	## no critic
+	eval "\${dbg_$key} = '$debug{$key}';";
+	die "$@" if ($@);
 }
 
 if ($terse) {
@@ -206,9 +208,9 @@ my @dep_includes = ();
 my @dep_functions = ();
 my $removal = "Documentation/feature-removal-schedule.txt";
 if ($tree && -f "$root/$removal") {
-	open(REMOVE, "<$root/$removal") ||
+	open(my $REMOVE, '<', "$root/$removal") ||
 				die "$P: $removal: open failed - $!\n";
-	while (<REMOVE>) {
+	while (<$REMOVE>) {
 		if (/^Check:\s+(.*\S)/) {
 			for my $entry (split(/[, ]+/, $1)) {
 				if ($entry =~ m@include/(.*)@) {
@@ -220,17 +222,21 @@ if ($tree && -f "$root/$removal") {
 			}
 		}
 	}
+	close($REMOVE);
 }
 
 my @rawlines = ();
 my @lines = ();
 my $vname;
 for my $filename (@ARGV) {
+	my $FILE;
 	if ($file) {
-		open(FILE, "diff -u /dev/null $filename|") ||
+		open($FILE, '-|', "diff -u /dev/null $filename") ||
 			die "$P: $filename: diff failed - $!\n";
+	} elsif ($filename eq '-') {
+		open($FILE, '<&STDIN');
 	} else {
-		open(FILE, "<$filename") ||
+		open($FILE, '<', "$filename") ||
 			die "$P: $filename: open failed - $!\n";
 	}
 	if ($filename eq '-') {
@@ -238,11 +244,11 @@ for my $filename (@ARGV) {
 	} else {
 		$vname = $filename;
 	}
-	while (<FILE>) {
+	while (<$FILE>) {
 		chomp;
 		push(@rawlines, $_);
 	}
-	close(FILE);
+	close($FILE);
 	if (!process($filename)) {
 		$exit = 1;
 	}
Signature · 1 line
1.6.0.4.911.gc990
#14 lore raw

PATCH 13/13 checkpatch: version: 0.26

In reply to Andy Whitcroft

From: Andy Whitcroft <[email protected]>

---
 scripts/checkpatch.pl |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
scripts/checkpatch.pl +1 -1
index bd6ac90..7bed4ed 100755
@@ -10,7 +10,7 @@ use strict;
 my $P = $0;
 $P =~ s@.*/@@g;
 
-my $V = '0.25';
+my $V = '0.26';
 
 use Getopt::Long qw(:config no_auto_abbrev);
 
Signature · 1 line
1.6.0.4.911.gc990