xref: /linux-6.15/scripts/checkpatch.pl (revision d8aaf121)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
20a920b5bSAndy Whitcroft# (c) 2001, Dave Jones. <[email protected]> (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
40a920b5bSAndy Whitcroft# (c) 2007, Andy Whitcroft <[email protected]> (new conditions, test suite, etc)
50a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
60a920b5bSAndy Whitcroft
70a920b5bSAndy Whitcroftuse strict;
80a920b5bSAndy Whitcroft
90a920b5bSAndy Whitcroftmy $P = $0;
1000df344fSAndy Whitcroft$P =~ s@.*/@@g;
110a920b5bSAndy Whitcroft
12*d8aaf121SAndy Whitcroftmy $V = '0.06';
130a920b5bSAndy Whitcroft
140a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
150a920b5bSAndy Whitcroft
160a920b5bSAndy Whitcroftmy $quiet = 0;
170a920b5bSAndy Whitcroftmy $tree = 1;
180a920b5bSAndy Whitcroftmy $chk_signoff = 1;
190a920b5bSAndy Whitcroftmy $chk_patch = 1;
20653d4876SAndy Whitcroftmy $tst_type = 0;
210a920b5bSAndy WhitcroftGetOptions(
220a920b5bSAndy Whitcroft	'q|quiet'	=> \$quiet,
230a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
240a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
250a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
26653d4876SAndy Whitcroft	'test-type!'	=> \$tst_type,
270a920b5bSAndy Whitcroft) or exit;
280a920b5bSAndy Whitcroft
290a920b5bSAndy Whitcroftmy $exit = 0;
300a920b5bSAndy Whitcroft
310a920b5bSAndy Whitcroftif ($#ARGV < 0) {
3200df344fSAndy Whitcroft	print "usage: $P [options] patchfile\n";
330a920b5bSAndy Whitcroft	print "version: $V\n";
340a920b5bSAndy Whitcroft	print "options: -q           => quiet\n";
350a920b5bSAndy Whitcroft	print "         --no-tree    => run without a kernel tree\n";
360a920b5bSAndy Whitcroft	exit(1);
370a920b5bSAndy Whitcroft}
380a920b5bSAndy Whitcroft
390a920b5bSAndy Whitcroftif ($tree && !top_of_kernel_tree()) {
400a920b5bSAndy Whitcroft	print "Must be run from the top-level dir. of a kernel tree\n";
410a920b5bSAndy Whitcroft	exit(2);
420a920b5bSAndy Whitcroft}
430a920b5bSAndy Whitcroft
444a0df2efSAndy Whitcroftmy @dep_includes = ();
454a0df2efSAndy Whitcroftmy @dep_functions = ();
460a920b5bSAndy Whitcroftmy $removal = 'Documentation/feature-removal-schedule.txt';
470a920b5bSAndy Whitcroftif ($tree && -f $removal) {
480a920b5bSAndy Whitcroft	open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n";
490a920b5bSAndy Whitcroft	while (<REMOVE>) {
500a920b5bSAndy Whitcroft		if (/^Files:\s+(.*\S)/) {
510a920b5bSAndy Whitcroft			for my $file (split(/[, ]+/, $1)) {
520a920b5bSAndy Whitcroft				if ($file =~ m@include/(.*)@) {
534a0df2efSAndy Whitcroft					push(@dep_includes, $1);
540a920b5bSAndy Whitcroft				}
550a920b5bSAndy Whitcroft			}
564a0df2efSAndy Whitcroft
574a0df2efSAndy Whitcroft		} elsif (/^Funcs:\s+(.*\S)/) {
584a0df2efSAndy Whitcroft			for my $func (split(/[, ]+/, $1)) {
594a0df2efSAndy Whitcroft				push(@dep_functions, $func);
604a0df2efSAndy Whitcroft			}
610a920b5bSAndy Whitcroft		}
620a920b5bSAndy Whitcroft	}
630a920b5bSAndy Whitcroft}
640a920b5bSAndy Whitcroft
6500df344fSAndy Whitcroftmy @rawlines = ();
660a920b5bSAndy Whitcroftwhile (<>) {
670a920b5bSAndy Whitcroft	chomp;
6800df344fSAndy Whitcroft	push(@rawlines, $_);
690a920b5bSAndy Whitcroft	if (eof(ARGV)) {
7000df344fSAndy Whitcroft		if (!process($ARGV, @rawlines)) {
710a920b5bSAndy Whitcroft			$exit = 1;
720a920b5bSAndy Whitcroft		}
7300df344fSAndy Whitcroft		@rawlines = ();
740a920b5bSAndy Whitcroft	}
750a920b5bSAndy Whitcroft}
760a920b5bSAndy Whitcroft
770a920b5bSAndy Whitcroftexit($exit);
780a920b5bSAndy Whitcroft
790a920b5bSAndy Whitcroftsub top_of_kernel_tree {
800a920b5bSAndy Whitcroft	if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") &&
810a920b5bSAndy Whitcroft	    (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") &&
820a920b5bSAndy Whitcroft	    (-d "Documentation") && (-d "arch") && (-d "include") &&
830a920b5bSAndy Whitcroft	    (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") &&
840a920b5bSAndy Whitcroft	    (-d "kernel") && (-d "lib") && (-d "scripts")) {
850a920b5bSAndy Whitcroft		return 1;
860a920b5bSAndy Whitcroft	}
870a920b5bSAndy Whitcroft	return 0;
880a920b5bSAndy Whitcroft}
890a920b5bSAndy Whitcroft
900a920b5bSAndy Whitcroftsub expand_tabs {
910a920b5bSAndy Whitcroft	my ($str) = @_;
920a920b5bSAndy Whitcroft
930a920b5bSAndy Whitcroft	my $res = '';
940a920b5bSAndy Whitcroft	my $n = 0;
950a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
960a920b5bSAndy Whitcroft		if ($c eq "\t") {
970a920b5bSAndy Whitcroft			$res .= ' ';
980a920b5bSAndy Whitcroft			$n++;
990a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
1000a920b5bSAndy Whitcroft				$res .= ' ';
1010a920b5bSAndy Whitcroft			}
1020a920b5bSAndy Whitcroft			next;
1030a920b5bSAndy Whitcroft		}
1040a920b5bSAndy Whitcroft		$res .= $c;
1050a920b5bSAndy Whitcroft		$n++;
1060a920b5bSAndy Whitcroft	}
1070a920b5bSAndy Whitcroft
1080a920b5bSAndy Whitcroft	return $res;
1090a920b5bSAndy Whitcroft}
1100a920b5bSAndy Whitcroft
1114a0df2efSAndy Whitcroftsub line_stats {
1124a0df2efSAndy Whitcroft	my ($line) = @_;
1134a0df2efSAndy Whitcroft
1144a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
1154a0df2efSAndy Whitcroft	$line =~ s/^.//;
1164a0df2efSAndy Whitcroft	$line = expand_tabs($line);
1174a0df2efSAndy Whitcroft
1184a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
1194a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
1204a0df2efSAndy Whitcroft
1214a0df2efSAndy Whitcroft	return (length($line), length($white));
1224a0df2efSAndy Whitcroft}
1234a0df2efSAndy Whitcroft
12400df344fSAndy Whitcroftsub sanitise_line {
12500df344fSAndy Whitcroft	my ($line) = @_;
12600df344fSAndy Whitcroft
12700df344fSAndy Whitcroft	my $res = '';
12800df344fSAndy Whitcroft	my $l = '';
12900df344fSAndy Whitcroft
13000df344fSAndy Whitcroft	my $quote = '';
13100df344fSAndy Whitcroft
13200df344fSAndy Whitcroft	foreach my $c (split(//, $line)) {
13300df344fSAndy Whitcroft		if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
13400df344fSAndy Whitcroft			if ($quote eq '') {
13500df344fSAndy Whitcroft				$quote = $c;
13600df344fSAndy Whitcroft				$res .= $c;
13700df344fSAndy Whitcroft				$l = $c;
13800df344fSAndy Whitcroft				next;
13900df344fSAndy Whitcroft			} elsif ($quote eq $c) {
14000df344fSAndy Whitcroft				$quote = '';
14100df344fSAndy Whitcroft			}
14200df344fSAndy Whitcroft		}
14300df344fSAndy Whitcroft		if ($quote && $c ne "\t") {
14400df344fSAndy Whitcroft			$res .= "X";
14500df344fSAndy Whitcroft		} else {
14600df344fSAndy Whitcroft			$res .= $c;
14700df344fSAndy Whitcroft		}
14800df344fSAndy Whitcroft
14900df344fSAndy Whitcroft		$l = $c;
15000df344fSAndy Whitcroft	}
15100df344fSAndy Whitcroft
15200df344fSAndy Whitcroft	return $res;
15300df344fSAndy Whitcroft}
15400df344fSAndy Whitcroft
1554a0df2efSAndy Whitcroftsub ctx_block_get {
156653d4876SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close) = @_;
1574a0df2efSAndy Whitcroft	my $line;
1584a0df2efSAndy Whitcroft	my $start = $linenr - 1;
1594a0df2efSAndy Whitcroft	my $blk = '';
1604a0df2efSAndy Whitcroft	my @o;
1614a0df2efSAndy Whitcroft	my @c;
1624a0df2efSAndy Whitcroft	my @res = ();
1634a0df2efSAndy Whitcroft
16400df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
16500df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
16600df344fSAndy Whitcroft		$remain--;
16700df344fSAndy Whitcroft
16800df344fSAndy Whitcroft		$blk .= $rawlines[$line];
1694a0df2efSAndy Whitcroft
170653d4876SAndy Whitcroft		@o = ($blk =~ /$open/g);
171653d4876SAndy Whitcroft		@c = ($blk =~ /$close/g);
1724a0df2efSAndy Whitcroft
1734a0df2efSAndy Whitcroft		if (!$outer || (scalar(@o) - scalar(@c)) == 1) {
17400df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
1754a0df2efSAndy Whitcroft		}
1764a0df2efSAndy Whitcroft
1774a0df2efSAndy Whitcroft		last if (scalar(@o) == scalar(@c));
1784a0df2efSAndy Whitcroft	}
1794a0df2efSAndy Whitcroft
1804a0df2efSAndy Whitcroft	return @res;
1814a0df2efSAndy Whitcroft}
1824a0df2efSAndy Whitcroftsub ctx_block_outer {
1834a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
1844a0df2efSAndy Whitcroft
185653d4876SAndy Whitcroft	return ctx_block_get($linenr, $remain, 1, '\{', '\}');
1864a0df2efSAndy Whitcroft}
1874a0df2efSAndy Whitcroftsub ctx_block {
1884a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
1894a0df2efSAndy Whitcroft
190653d4876SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '\{', '\}');
191653d4876SAndy Whitcroft}
192653d4876SAndy Whitcroftsub ctx_statement {
193653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
194653d4876SAndy Whitcroft
195653d4876SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '\(', '\)');
1964a0df2efSAndy Whitcroft}
1974a0df2efSAndy Whitcroft
1984a0df2efSAndy Whitcroftsub ctx_locate_comment {
1994a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
2004a0df2efSAndy Whitcroft
2014a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
20200df344fSAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
2034a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
2044a0df2efSAndy Whitcroft
2054a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
2064a0df2efSAndy Whitcroft	# comment.
2074a0df2efSAndy Whitcroft	my $in_comment = 0;
2084a0df2efSAndy Whitcroft	$current_comment = '';
2094a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
21000df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
21100df344fSAndy Whitcroft		#warn "           $line\n";
2124a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
2134a0df2efSAndy Whitcroft			$in_comment = 1;
2144a0df2efSAndy Whitcroft		}
2154a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
2164a0df2efSAndy Whitcroft			$in_comment = 1;
2174a0df2efSAndy Whitcroft		}
2184a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
2194a0df2efSAndy Whitcroft			$current_comment = '';
2204a0df2efSAndy Whitcroft		}
2214a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
2224a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
2234a0df2efSAndy Whitcroft			$in_comment = 0;
2244a0df2efSAndy Whitcroft		}
2254a0df2efSAndy Whitcroft	}
2264a0df2efSAndy Whitcroft
2274a0df2efSAndy Whitcroft	chomp($current_comment);
2284a0df2efSAndy Whitcroft	return($current_comment);
2294a0df2efSAndy Whitcroft}
2304a0df2efSAndy Whitcroftsub ctx_has_comment {
2314a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
2324a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
2334a0df2efSAndy Whitcroft
23400df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
2354a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
2364a0df2efSAndy Whitcroft
2374a0df2efSAndy Whitcroft	return ($cmt ne '');
2384a0df2efSAndy Whitcroft}
2394a0df2efSAndy Whitcroft
2400a920b5bSAndy Whitcroftsub cat_vet {
2410a920b5bSAndy Whitcroft	my ($vet) = @_;
2420a920b5bSAndy Whitcroft
2430a920b5bSAndy Whitcroft	$vet =~ s/\t/^I/;
2440a920b5bSAndy Whitcroft	$vet =~ s/$/\$/;
2450a920b5bSAndy Whitcroft
2460a920b5bSAndy Whitcroft	return $vet;
2470a920b5bSAndy Whitcroft}
2480a920b5bSAndy Whitcroft
2490a920b5bSAndy Whitcroftsub process {
2500a920b5bSAndy Whitcroft	my $filename = shift;
2510a920b5bSAndy Whitcroft	my @lines = @_;
2520a920b5bSAndy Whitcroft
2530a920b5bSAndy Whitcroft	my $linenr=0;
2540a920b5bSAndy Whitcroft	my $prevline="";
2550a920b5bSAndy Whitcroft	my $stashline="";
2560a920b5bSAndy Whitcroft
2574a0df2efSAndy Whitcroft	my $length;
2580a920b5bSAndy Whitcroft	my $indent;
2590a920b5bSAndy Whitcroft	my $previndent=0;
2600a920b5bSAndy Whitcroft	my $stashindent=0;
2610a920b5bSAndy Whitcroft
2620a920b5bSAndy Whitcroft	my $clean = 1;
2630a920b5bSAndy Whitcroft	my $signoff = 0;
2640a920b5bSAndy Whitcroft	my $is_patch = 0;
2650a920b5bSAndy Whitcroft
2660a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
2670a920b5bSAndy Whitcroft	my $realfile = '';
2680a920b5bSAndy Whitcroft	my $realline = 0;
2690a920b5bSAndy Whitcroft	my $realcnt = 0;
2700a920b5bSAndy Whitcroft	my $here = '';
2710a920b5bSAndy Whitcroft	my $in_comment = 0;
2720a920b5bSAndy Whitcroft	my $first_line = 0;
2730a920b5bSAndy Whitcroft
274*d8aaf121SAndy Whitcroft	my $Ident	= qr{[A-Za-z\d_]+};
275*d8aaf121SAndy Whitcroft	my $Storage	= qr{extern|static};
276*d8aaf121SAndy Whitcroft	my $Sparse	= qr{__user|__kernel|__force|__iomem};
277*d8aaf121SAndy Whitcroft	my $NonptrType	= qr{
278*d8aaf121SAndy Whitcroft				\b
279*d8aaf121SAndy Whitcroft				(?:const\s+)?
280*d8aaf121SAndy Whitcroft				(?:unsigned\s+)?
281*d8aaf121SAndy Whitcroft				(?:
282*d8aaf121SAndy Whitcroft					void|
283*d8aaf121SAndy Whitcroft					char|
284*d8aaf121SAndy Whitcroft					short|
285*d8aaf121SAndy Whitcroft					int|
286*d8aaf121SAndy Whitcroft					long|
287*d8aaf121SAndy Whitcroft					unsigned|
288*d8aaf121SAndy Whitcroft					float|
289*d8aaf121SAndy Whitcroft					double|
290*d8aaf121SAndy Whitcroft					long\s+int|
291*d8aaf121SAndy Whitcroft					long\s+long|
292*d8aaf121SAndy Whitcroft					long\s+long\s+int|
293*d8aaf121SAndy Whitcroft					struct\s+$Ident|
294*d8aaf121SAndy Whitcroft					union\s+$Ident|
295*d8aaf121SAndy Whitcroft					${Ident}_t
296*d8aaf121SAndy Whitcroft				)
297*d8aaf121SAndy Whitcroft				(?:\s+$Sparse)*
298*d8aaf121SAndy Whitcroft				\b
299*d8aaf121SAndy Whitcroft			  }x;
300*d8aaf121SAndy Whitcroft	my $Type	= qr{
301*d8aaf121SAndy Whitcroft				\b$NonptrType\b
302*d8aaf121SAndy Whitcroft				(?:\s*\*+\s*const|\s*\*+)?
303*d8aaf121SAndy Whitcroft			  }x;
304*d8aaf121SAndy Whitcroft	my $Declare	= qr{(?:$Storage\s+)?$Type};
305*d8aaf121SAndy Whitcroft	my $Attribute	= qr{__read_mostly|__init|__initdata};
306653d4876SAndy Whitcroft
3070a920b5bSAndy Whitcroft	foreach my $line (@lines) {
3080a920b5bSAndy Whitcroft		$linenr++;
3090a920b5bSAndy Whitcroft
310653d4876SAndy Whitcroft		my $rawline = $line;
311653d4876SAndy Whitcroft
3120a920b5bSAndy Whitcroft#extract the filename as it passes
3130a920b5bSAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
3140a920b5bSAndy Whitcroft			$realfile=$1;
31500df344fSAndy Whitcroft			$realfile =~ s@^[^/]*/@@;
3160a920b5bSAndy Whitcroft			$in_comment = 0;
3170a920b5bSAndy Whitcroft			next;
3180a920b5bSAndy Whitcroft		}
3190a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
3200a920b5bSAndy Whitcroft		if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
3210a920b5bSAndy Whitcroft			$is_patch = 1;
3224a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
3230a920b5bSAndy Whitcroft			$in_comment = 0;
3240a920b5bSAndy Whitcroft			$realline=$1-1;
3250a920b5bSAndy Whitcroft			if (defined $2) {
3260a920b5bSAndy Whitcroft				$realcnt=$3+1;
3270a920b5bSAndy Whitcroft			} else {
3280a920b5bSAndy Whitcroft				$realcnt=1+1;
3290a920b5bSAndy Whitcroft			}
3300a920b5bSAndy Whitcroft			next;
3310a920b5bSAndy Whitcroft		}
3320a920b5bSAndy Whitcroft
3334a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
3344a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
3354a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
3364a0df2efSAndy Whitcroft		if ($line =~ /^( |\+|$)/) {
3370a920b5bSAndy Whitcroft			$realline++;
338*d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
3390a920b5bSAndy Whitcroft
3400a920b5bSAndy Whitcroft			# track any sort of multi-line comment.  Obviously if
3410a920b5bSAndy Whitcroft			# the added text or context do not include the whole
3420a920b5bSAndy Whitcroft			# comment we will not see it. Such is life.
3430a920b5bSAndy Whitcroft			#
3440a920b5bSAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
3450a920b5bSAndy Whitcroft			# is the start of a diff block and this line starts
3460a920b5bSAndy Whitcroft			# ' *' then it is very likely a comment.
3474a0df2efSAndy Whitcroft			if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
3480a920b5bSAndy Whitcroft				$in_comment = 1;
3490a920b5bSAndy Whitcroft			}
3500a920b5bSAndy Whitcroft			if ($line =~ m@/\*@) {
3510a920b5bSAndy Whitcroft				$in_comment = 1;
3520a920b5bSAndy Whitcroft			}
3530a920b5bSAndy Whitcroft			if ($line =~ m@\*/@) {
3540a920b5bSAndy Whitcroft				$in_comment = 0;
3550a920b5bSAndy Whitcroft			}
3560a920b5bSAndy Whitcroft
3574a0df2efSAndy Whitcroft			# Measure the line length and indent.
3584a0df2efSAndy Whitcroft			($length, $indent) = line_stats($line);
3590a920b5bSAndy Whitcroft
3600a920b5bSAndy Whitcroft			# Track the previous line.
3610a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
3620a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
363*d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
364*d8aaf121SAndy Whitcroft			$realcnt--;
3650a920b5bSAndy Whitcroft		}
3660a920b5bSAndy Whitcroft
3670a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
368389834b6SRandy Dunlap		$here = "#$linenr: ";
369389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
3700a920b5bSAndy Whitcroft
37100df344fSAndy Whitcroft		my $hereline = "$here\n$line\n";
3720a920b5bSAndy Whitcroft		my $herecurr = "$here\n$line\n\n";
3730a920b5bSAndy Whitcroft		my $hereprev = "$here\n$prevline\n$line\n\n";
3740a920b5bSAndy Whitcroft
3750a920b5bSAndy Whitcroft#check the patch for a signoff:
376*d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
3774a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
3784a0df2efSAndy Whitcroft			$signoff++;
3790a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
380*d8aaf121SAndy Whitcroft				print "Signed-off-by: is the preferred form\n";
3810a920b5bSAndy Whitcroft				print "$herecurr";
3820a920b5bSAndy Whitcroft				$clean = 0;
3830a920b5bSAndy Whitcroft			}
3840a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
3850a920b5bSAndy Whitcroft				print "need space after Signed-off-by:\n";
3860a920b5bSAndy Whitcroft				print "$herecurr";
3870a920b5bSAndy Whitcroft				$clean = 0;
3880a920b5bSAndy Whitcroft			}
3890a920b5bSAndy Whitcroft		}
3900a920b5bSAndy Whitcroft
39100df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
39200df344fSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
39300df344fSAndy Whitcroft			print "patch seems to be corrupt (line wrapped?) [$realcnt]\n";
39400df344fSAndy Whitcroft			print "$herecurr";
39500df344fSAndy Whitcroft			$clean = 0;
39600df344fSAndy Whitcroft		}
3970a920b5bSAndy Whitcroft
39800df344fSAndy Whitcroft#ignore lines being removed
39900df344fSAndy Whitcroft		if ($line=~/^-/) {next;}
40000df344fSAndy Whitcroft
40100df344fSAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
40200df344fSAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
4030a920b5bSAndy Whitcroft
4040a920b5bSAndy Whitcroft#trailing whitespace
405*d8aaf121SAndy Whitcroft		if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
4060a920b5bSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($line) . "\n\n";
4070a920b5bSAndy Whitcroft			print "trailing whitespace\n";
4080a920b5bSAndy Whitcroft			print "$herevet";
4090a920b5bSAndy Whitcroft			$clean = 0;
4100a920b5bSAndy Whitcroft		}
4110a920b5bSAndy Whitcroft#80 column limit
41200df344fSAndy Whitcroft		if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
4130a920b5bSAndy Whitcroft			print "line over 80 characters\n";
4140a920b5bSAndy Whitcroft			print "$herecurr";
4150a920b5bSAndy Whitcroft			$clean = 0;
4160a920b5bSAndy Whitcroft		}
4170a920b5bSAndy Whitcroft
4180a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk
4190a920b5bSAndy Whitcroft		next if ($realfile !~ /\.[hc]$/);
4200a920b5bSAndy Whitcroft
4210a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
4220a920b5bSAndy Whitcroft# more than 8 must use tabs.
4230a920b5bSAndy Whitcroft		if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s*        \s*/) {
4240a920b5bSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($line) . "\n\n";
4250a920b5bSAndy Whitcroft			print "use tabs not spaces\n";
4260a920b5bSAndy Whitcroft			print "$herevet";
4270a920b5bSAndy Whitcroft			$clean = 0;
4280a920b5bSAndy Whitcroft		}
4290a920b5bSAndy Whitcroft
4300a920b5bSAndy Whitcroft		#
4310a920b5bSAndy Whitcroft		# The rest of our checks refer specifically to C style
4320a920b5bSAndy Whitcroft		# only apply those _outside_ comments.
4330a920b5bSAndy Whitcroft		#
4340a920b5bSAndy Whitcroft		next if ($in_comment);
4350a920b5bSAndy Whitcroft
4360a920b5bSAndy Whitcroft# Remove comments from the line before processing.
4370a920b5bSAndy Whitcroft		$line =~ s@/\*.*\*/@@g;
4380a920b5bSAndy Whitcroft		$line =~ s@/\*.*@@;
4390a920b5bSAndy Whitcroft		$line =~ s@.*\*/@@;
44000df344fSAndy Whitcroft
441653d4876SAndy Whitcroft# Standardise the strings and chars within the input to simplify matching.
442653d4876SAndy Whitcroft		$line = sanitise_line($line);
443653d4876SAndy Whitcroft
44400df344fSAndy Whitcroft#
44500df344fSAndy Whitcroft# Checks which may be anchored in the context.
44600df344fSAndy Whitcroft#
44700df344fSAndy Whitcroft
44800df344fSAndy Whitcroft# Check for switch () and associated case and default
44900df344fSAndy Whitcroft# statements should be at the same indent.
45000df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
45100df344fSAndy Whitcroft			my $err = '';
45200df344fSAndy Whitcroft			my $sep = '';
45300df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
45400df344fSAndy Whitcroft			shift(@ctx);
45500df344fSAndy Whitcroft			for my $ctx (@ctx) {
45600df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
45700df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
45800df344fSAndy Whitcroft							$indent != $cindent) {
45900df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
46000df344fSAndy Whitcroft					$sep = '';
46100df344fSAndy Whitcroft				} else {
46200df344fSAndy Whitcroft					$sep = "[...]\n";
46300df344fSAndy Whitcroft				}
46400df344fSAndy Whitcroft			}
46500df344fSAndy Whitcroft			if ($err ne '') {
46600df344fSAndy Whitcroft				print "switch and case should be at the same indent\n";
46700df344fSAndy Whitcroft				print "$here\n$line\n$err\n";
46800df344fSAndy Whitcroft				$clean = 0;
46900df344fSAndy Whitcroft			}
47000df344fSAndy Whitcroft		}
47100df344fSAndy Whitcroft
47200df344fSAndy Whitcroft#ignore lines not being added
47300df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
47400df344fSAndy Whitcroft
475653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
476653d4876SAndy Whitcroft		if ($tst_type && $line =~ /^.$Declare$/) {
477653d4876SAndy Whitcroft			print "TEST: is type $Declare\n";
478653d4876SAndy Whitcroft			print "$herecurr";
479653d4876SAndy Whitcroft			$clean = 0;
480653d4876SAndy Whitcroft			next;
481653d4876SAndy Whitcroft		}
482653d4876SAndy Whitcroft
48300df344fSAndy Whitcroft#
48400df344fSAndy Whitcroft# Checks which are anchored on the added line.
48500df344fSAndy Whitcroft#
48600df344fSAndy Whitcroft
487653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
488653d4876SAndy Whitcroft		if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
489653d4876SAndy Whitcroft			my $path = $1;
490653d4876SAndy Whitcroft			if ($path =~ m{//}) {
491653d4876SAndy Whitcroft				print "malformed #include filename\n";
492653d4876SAndy Whitcroft				print "$herecurr";
493653d4876SAndy Whitcroft				$clean = 0;
494653d4876SAndy Whitcroft			}
495653d4876SAndy Whitcroft			# Sanitise this special form of string.
496653d4876SAndy Whitcroft			$path = 'X' x length($path);
497653d4876SAndy Whitcroft			$line =~ s{\<.*\>}{<$path>};
498653d4876SAndy Whitcroft		}
499653d4876SAndy Whitcroft
50000df344fSAndy Whitcroft# no C99 // comments
50100df344fSAndy Whitcroft		if ($line =~ m{//}) {
50200df344fSAndy Whitcroft			print "do not use C99 // comments\n";
50300df344fSAndy Whitcroft			print "$herecurr";
50400df344fSAndy Whitcroft			$clean = 0;
50500df344fSAndy Whitcroft		}
50600df344fSAndy Whitcroft		# Remove C99 comments.
5070a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
5080a920b5bSAndy Whitcroft
5090a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
510653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
511653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
512653d4876SAndy Whitcroft			my $name = $1;
5130a920b5bSAndy Whitcroft			if (($prevline !~ /^}/) &&
5140a920b5bSAndy Whitcroft			   ($prevline !~ /^\+}/) &&
515653d4876SAndy Whitcroft			   ($prevline !~ /^ }/) &&
516653d4876SAndy Whitcroft			   ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) {
517653d4876SAndy Whitcroft				print "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n";
5180a920b5bSAndy Whitcroft				print "$herecurr";
5190a920b5bSAndy Whitcroft				$clean = 0;
5200a920b5bSAndy Whitcroft			}
5210a920b5bSAndy Whitcroft		}
5220a920b5bSAndy Whitcroft
5230a920b5bSAndy Whitcroft# check for static initialisers.
5240a920b5bSAndy Whitcroft		if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
5250a920b5bSAndy Whitcroft			print "do not initialise statics to 0 or NULL\n";
5260a920b5bSAndy Whitcroft			print "$herecurr";
5270a920b5bSAndy Whitcroft			$clean = 0;
5280a920b5bSAndy Whitcroft		}
5290a920b5bSAndy Whitcroft
530653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
531653d4876SAndy Whitcroft# make sense.
532653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
533653d4876SAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+\(\s*$Ident\s*\)\s*\(/ &&
534653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
5350a920b5bSAndy Whitcroft			print "do not add new typedefs\n";
5360a920b5bSAndy Whitcroft			print "$herecurr";
5370a920b5bSAndy Whitcroft			$clean = 0;
5380a920b5bSAndy Whitcroft		}
5390a920b5bSAndy Whitcroft
5400a920b5bSAndy Whitcroft# * goes on variable not on type
541*d8aaf121SAndy Whitcroft		if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
54200df344fSAndy Whitcroft			print "\"(foo$1)\" should be \"(foo $1)\"\n";
54300df344fSAndy Whitcroft			print "$herecurr";
54400df344fSAndy Whitcroft			$clean = 0;
545*d8aaf121SAndy Whitcroft
546*d8aaf121SAndy Whitcroft		} elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
54700df344fSAndy Whitcroft			print "\"(foo $1 )\" should be \"(foo $1)\"\n";
5480a920b5bSAndy Whitcroft			print "$herecurr";
5490a920b5bSAndy Whitcroft			$clean = 0;
550*d8aaf121SAndy Whitcroft
551*d8aaf121SAndy Whitcroft		} elsif ($line =~ m{$NonptrType(\*+)(?:\s+const)?\s+[A-Za-z\d_]+}) {
552*d8aaf121SAndy Whitcroft			print "\"foo$1 bar\" should be \"foo $1bar\"\n";
553*d8aaf121SAndy Whitcroft			print "$herecurr";
554*d8aaf121SAndy Whitcroft			$clean = 0;
555*d8aaf121SAndy Whitcroft
556*d8aaf121SAndy Whitcroft		} elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+const)\s+[A-Za-z\d_]+}) {
557*d8aaf121SAndy Whitcroft			print "\"foo $1 bar\" should be \"foo $1bar\"\n";
558*d8aaf121SAndy Whitcroft			print "$herecurr";
559*d8aaf121SAndy Whitcroft			$clean = 0;
5600a920b5bSAndy Whitcroft		}
5610a920b5bSAndy Whitcroft
5620a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
5630a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
5640a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
5650a920b5bSAndy Whitcroft# 			print "$herecurr";
5660a920b5bSAndy Whitcroft# 			$clean = 0;
5670a920b5bSAndy Whitcroft# 		}
5680a920b5bSAndy Whitcroft
56900df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
57000df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
57100df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
57200df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
57300df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
5740a920b5bSAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)/) {
57500df344fSAndy Whitcroft			my $ok = 0;
57600df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
57700df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
57800df344fSAndy Whitcroft				# we have a preceeding printk if it ends
57900df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
58000df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
58100df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
58200df344fSAndy Whitcroft						$ok = 1;
58300df344fSAndy Whitcroft					}
58400df344fSAndy Whitcroft					last;
58500df344fSAndy Whitcroft				}
58600df344fSAndy Whitcroft			}
58700df344fSAndy Whitcroft			if ($ok == 0) {
5880a920b5bSAndy Whitcroft				print "printk() should include KERN_ facility level\n";
5890a920b5bSAndy Whitcroft				print "$herecurr";
5900a920b5bSAndy Whitcroft				$clean = 0;
5910a920b5bSAndy Whitcroft			}
59200df344fSAndy Whitcroft		}
5930a920b5bSAndy Whitcroft
594653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
595653d4876SAndy Whitcroft# or if closed on same line
596*d8aaf121SAndy Whitcroft		if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
5970a920b5bSAndy Whitcroft		    !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
5980a920b5bSAndy Whitcroft			print "braces following function declarations go on the next line\n";
5990a920b5bSAndy Whitcroft			print "$herecurr";
6000a920b5bSAndy Whitcroft			$clean = 0;
6010a920b5bSAndy Whitcroft		}
602653d4876SAndy Whitcroft
603653d4876SAndy Whitcroft# Check operator spacing.
6044a0df2efSAndy Whitcroft		# Note we expand the line with the leading + as the real
6054a0df2efSAndy Whitcroft		# line will be displayed with the leading + and the tabs
6064a0df2efSAndy Whitcroft		# will therefore also expand that way.
6070a920b5bSAndy Whitcroft		my $opline = $line;
6084a0df2efSAndy Whitcroft		$opline = expand_tabs($opline);
6090a920b5bSAndy Whitcroft		$opline =~ s/^./ /;
6100a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
6110a920b5bSAndy Whitcroft			my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
61200df344fSAndy Whitcroft			my $off = 0;
6130a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
6144a0df2efSAndy Whitcroft				$off += length($elements[$n]);
6154a0df2efSAndy Whitcroft
6164a0df2efSAndy Whitcroft				my $a = '';
6174a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
6184a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
6194a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
6204a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
6214a0df2efSAndy Whitcroft				$a = 'E' if ($elements[$n] eq '' && $n == 0);
6224a0df2efSAndy Whitcroft
6230a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
6244a0df2efSAndy Whitcroft
6254a0df2efSAndy Whitcroft				my $c = '';
6260a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
6274a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
6284a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
6294a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
6304a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
6314a0df2efSAndy Whitcroft				} else {
6324a0df2efSAndy Whitcroft					$c = 'E';
6330a920b5bSAndy Whitcroft				}
6340a920b5bSAndy Whitcroft
63500df344fSAndy Whitcroft				# Pick up the preceeding and succeeding characters.
63600df344fSAndy Whitcroft				my $ca = substr($opline, $off - 1, 1);
63700df344fSAndy Whitcroft				my $cc = '';
638653d4876SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
639*d8aaf121SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
64000df344fSAndy Whitcroft				}
64100df344fSAndy Whitcroft
6424a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
6434a0df2efSAndy Whitcroft
6444a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
6454a0df2efSAndy Whitcroft
6464a0df2efSAndy Whitcroft				my $ptr = (" " x $off) . "^";
64700df344fSAndy Whitcroft				my $hereptr = "$hereline$ptr\n\n";
6480a920b5bSAndy Whitcroft
6490a920b5bSAndy Whitcroft				##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
6500a920b5bSAndy Whitcroft
651*d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
652*d8aaf121SAndy Whitcroft				if ($op eq ';') {
653*d8aaf121SAndy Whitcroft					if ($ctx !~ /.x[WE]/ && $cc !~ /^\\/) {
654*d8aaf121SAndy Whitcroft						print "need space after that '$op' $at\n";
655*d8aaf121SAndy Whitcroft						print "$hereptr";
656*d8aaf121SAndy Whitcroft						$clean = 0;
657*d8aaf121SAndy Whitcroft					}
658*d8aaf121SAndy Whitcroft
659*d8aaf121SAndy Whitcroft				# // is a comment
660*d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
6610a920b5bSAndy Whitcroft
6620a920b5bSAndy Whitcroft				# -> should have no spaces
6630a920b5bSAndy Whitcroft				} elsif ($op eq '->') {
6644a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
6650a920b5bSAndy Whitcroft						print "no spaces around that '$op' $at\n";
6664a0df2efSAndy Whitcroft						print "$hereptr";
6670a920b5bSAndy Whitcroft						$clean = 0;
6680a920b5bSAndy Whitcroft					}
6690a920b5bSAndy Whitcroft
6700a920b5bSAndy Whitcroft				# , must have a space on the right.
6710a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
672*d8aaf121SAndy Whitcroft					if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
6730a920b5bSAndy Whitcroft						print "need space after that '$op' $at\n";
6744a0df2efSAndy Whitcroft						print "$hereptr";
6750a920b5bSAndy Whitcroft						$clean = 0;
6760a920b5bSAndy Whitcroft					}
6770a920b5bSAndy Whitcroft
6780a920b5bSAndy Whitcroft				# unary ! and unary ~ are allowed no space on the right
6790a920b5bSAndy Whitcroft				} elsif ($op eq '!' or $op eq '~') {
6804a0df2efSAndy Whitcroft					if ($ctx !~ /[WOEB]x./) {
6810a920b5bSAndy Whitcroft						print "need space before that '$op' $at\n";
6824a0df2efSAndy Whitcroft						print "$hereptr";
6830a920b5bSAndy Whitcroft						$clean = 0;
6840a920b5bSAndy Whitcroft					}
6854a0df2efSAndy Whitcroft					if ($ctx =~ /.xW/) {
6860a920b5bSAndy Whitcroft						print "no space after that '$op' $at\n";
6874a0df2efSAndy Whitcroft						print "$hereptr";
6880a920b5bSAndy Whitcroft						$clean = 0;
6890a920b5bSAndy Whitcroft					}
6900a920b5bSAndy Whitcroft
6910a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
6920a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
693*d8aaf121SAndy Whitcroft					if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
6940a920b5bSAndy Whitcroft						print "need space one side of that '$op' $at\n";
6954a0df2efSAndy Whitcroft						print "$hereptr";
6960a920b5bSAndy Whitcroft						$clean = 0;
6970a920b5bSAndy Whitcroft					}
698*d8aaf121SAndy Whitcroft					if ($ctx =~ /Wx./ && $cc =~ /^;/) {
699653d4876SAndy Whitcroft						print "no space before that '$op' $at\n";
700653d4876SAndy Whitcroft						print "$hereptr";
701653d4876SAndy Whitcroft						$clean = 0;
702653d4876SAndy Whitcroft					}
7030a920b5bSAndy Whitcroft
7040a920b5bSAndy Whitcroft				# & is both unary and binary
7050a920b5bSAndy Whitcroft				# unary:
7060a920b5bSAndy Whitcroft				# 	a &b
7070a920b5bSAndy Whitcroft				# binary (consistent spacing):
7080a920b5bSAndy Whitcroft				#	a&b		OK
7090a920b5bSAndy Whitcroft				#	a & b		OK
7100a920b5bSAndy Whitcroft				#
7110a920b5bSAndy Whitcroft				# boiling down to: if there is a space on the right then there
7120a920b5bSAndy Whitcroft				# should be one on the left.
7130a920b5bSAndy Whitcroft				#
7140a920b5bSAndy Whitcroft				# - is the same
7150a920b5bSAndy Whitcroft				#
7164a0df2efSAndy Whitcroft				} elsif ($op eq '&' or $op eq '-') {
71700df344fSAndy Whitcroft					if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
7180a920b5bSAndy Whitcroft						print "need space before that '$op' $at\n";
7194a0df2efSAndy Whitcroft						print "$hereptr";
7204a0df2efSAndy Whitcroft						$clean = 0;
7214a0df2efSAndy Whitcroft					}
7224a0df2efSAndy Whitcroft
72300df344fSAndy Whitcroft				# * is the same as & only adding:
72400df344fSAndy Whitcroft				# type:
72500df344fSAndy Whitcroft				# 	(foo *)
72600df344fSAndy Whitcroft				#	(foo **)
72700df344fSAndy Whitcroft				#
7284a0df2efSAndy Whitcroft				} elsif ($op eq '*') {
72900df344fSAndy Whitcroft					if ($ca eq '*') {
730*d8aaf121SAndy Whitcroft						if ($cc =~ /^\s(?!\s*const)/) {
73100df344fSAndy Whitcroft							print "no space after that '$op' $at\n";
73200df344fSAndy Whitcroft							print "$hereptr";
73300df344fSAndy Whitcroft							$clean = 0;
73400df344fSAndy Whitcroft						}
735653d4876SAndy Whitcroft					} elsif ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
7364a0df2efSAndy Whitcroft						print "need space before that '$op' $at\n";
7374a0df2efSAndy Whitcroft						print "$hereptr";
7380a920b5bSAndy Whitcroft						$clean = 0;
7390a920b5bSAndy Whitcroft					}
7400a920b5bSAndy Whitcroft
7410a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
7420a920b5bSAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
7430a920b5bSAndy Whitcroft					 $op eq '^' or $op eq '|')
7440a920b5bSAndy Whitcroft				{
7454a0df2efSAndy Whitcroft					if ($ctx !~ /VxV|WxW|VxE|WxE/) {
7460a920b5bSAndy Whitcroft						print "need consistent spacing around '$op' $at\n";
7474a0df2efSAndy Whitcroft						print "$hereptr";
7480a920b5bSAndy Whitcroft						$clean = 0;
7490a920b5bSAndy Whitcroft					}
7500a920b5bSAndy Whitcroft
7510a920b5bSAndy Whitcroft				# All the others need spaces both sides.
7524a0df2efSAndy Whitcroft				} elsif ($ctx !~ /[EW]x[WE]/) {
7530a920b5bSAndy Whitcroft					print "need spaces around that '$op' $at\n";
7544a0df2efSAndy Whitcroft					print "$hereptr";
7550a920b5bSAndy Whitcroft					$clean = 0;
7560a920b5bSAndy Whitcroft				}
7574a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
7580a920b5bSAndy Whitcroft			}
7590a920b5bSAndy Whitcroft		}
7600a920b5bSAndy Whitcroft
7610a920b5bSAndy Whitcroft#need space before brace following if, while, etc
7620a920b5bSAndy Whitcroft		if ($line=~/\(.*\){/) {
7630a920b5bSAndy Whitcroft			print "need a space before the brace\n";
7640a920b5bSAndy Whitcroft			print "$herecurr";
7650a920b5bSAndy Whitcroft			$clean = 0;
7660a920b5bSAndy Whitcroft		}
7670a920b5bSAndy Whitcroft
7680a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
7694a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
7700a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
7710a920b5bSAndy Whitcroft			print "labels should not be indented\n";
7720a920b5bSAndy Whitcroft			print "$herecurr";
7730a920b5bSAndy Whitcroft			$clean = 0;
7740a920b5bSAndy Whitcroft		}
7750a920b5bSAndy Whitcroft
7760a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
7774a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
7780a920b5bSAndy Whitcroft			print "need a space before the open parenthesis\n";
7790a920b5bSAndy Whitcroft			print "$herecurr";
7800a920b5bSAndy Whitcroft			$clean = 0;
7810a920b5bSAndy Whitcroft		}
7820a920b5bSAndy Whitcroft
7830a920b5bSAndy Whitcroft# Check for illegal assignment in if conditional.
784653d4876SAndy Whitcroft		if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
78500df344fSAndy Whitcroft			#next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
786653d4876SAndy Whitcroft			print "do not use assignment in if condition\n";
7870a920b5bSAndy Whitcroft			print "$herecurr";
7880a920b5bSAndy Whitcroft			$clean = 0;
7890a920b5bSAndy Whitcroft		}
7900a920b5bSAndy Whitcroft
7910a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
7920a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
7930a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
7940a920b5bSAndy Whitcroft						$previndent == $indent) {
7950a920b5bSAndy Whitcroft			print "else should follow close brace\n";
7960a920b5bSAndy Whitcroft			print "$hereprev";
7970a920b5bSAndy Whitcroft			$clean = 0;
7980a920b5bSAndy Whitcroft		}
7990a920b5bSAndy Whitcroft
8000a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
8010a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
8020a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
8030a920b5bSAndy Whitcroft#		    print "$herecurr";
8040a920b5bSAndy Whitcroft#		    $clean = 0;
8050a920b5bSAndy Whitcroft#		}
8060a920b5bSAndy Whitcroft
8070a920b5bSAndy Whitcroft#no spaces allowed after \ in define
8080a920b5bSAndy Whitcroft		if ($line=~/\#define.*\\\s$/) {
8090a920b5bSAndy Whitcroft			print("Whitepspace after \\ makes next lines useless\n");
8100a920b5bSAndy Whitcroft			print "$herecurr";
8110a920b5bSAndy Whitcroft			$clean = 0;
8120a920b5bSAndy Whitcroft		}
8130a920b5bSAndy Whitcroft
814653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
815653d4876SAndy Whitcroft		if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
8160a920b5bSAndy Whitcroft			my $checkfile = "include/linux/$1.h";
8170a920b5bSAndy Whitcroft			if (-f $checkfile) {
8180a920b5bSAndy Whitcroft				print "Use #include <linux/$1.h> instead of <asm/$1.h>\n";
8190a920b5bSAndy Whitcroft				print $herecurr;
8200a920b5bSAndy Whitcroft				$clean = 0;
8210a920b5bSAndy Whitcroft			}
8220a920b5bSAndy Whitcroft		}
8230a920b5bSAndy Whitcroft
824653d4876SAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
825653d4876SAndy Whitcroft# or if that brace on the next line is for something else
826*d8aaf121SAndy Whitcroft		if ($prevline=~/\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/) {
8270a920b5bSAndy Whitcroft			my @opened = $prevline=~/\(/g;
8280a920b5bSAndy Whitcroft			my @closed = $prevline=~/\)/g;
8290a920b5bSAndy Whitcroft			my $nr_line = $linenr;
83000df344fSAndy Whitcroft			my $remaining = $realcnt - 1;
8310a920b5bSAndy Whitcroft			my $next_line = $line;
8320a920b5bSAndy Whitcroft			my $extra_lines = 0;
8330a920b5bSAndy Whitcroft			my $display_segment = $prevline;
8340a920b5bSAndy Whitcroft
83500df344fSAndy Whitcroft			while ($remaining > 0 && scalar @opened > scalar @closed) {
8360a920b5bSAndy Whitcroft				$prevline .= $next_line;
8370a920b5bSAndy Whitcroft				$display_segment .= "\n" . $next_line;
8380a920b5bSAndy Whitcroft				$next_line = $lines[$nr_line];
8390a920b5bSAndy Whitcroft				$nr_line++;
8400a920b5bSAndy Whitcroft				$remaining--;
8410a920b5bSAndy Whitcroft
8420a920b5bSAndy Whitcroft				@opened = $prevline=~/\(/g;
8430a920b5bSAndy Whitcroft				@closed = $prevline=~/\)/g;
8440a920b5bSAndy Whitcroft			}
8450a920b5bSAndy Whitcroft
846*d8aaf121SAndy Whitcroft			if (($prevline=~/\b(?:(if|while|for|switch)\s*\(.*\)|do|else)\s*$/) and ($next_line=~/{/) and
847*d8aaf121SAndy Whitcroft			   !($next_line=~/\b(?:if|while|for|switch|do|else)\b/) and !($next_line=~/\#define.*do.*while/)) {
8480a920b5bSAndy Whitcroft				print "That { should be on the previous line\n";
8494a0df2efSAndy Whitcroft				print "$here\n$display_segment\n$next_line\n\n";
8500a920b5bSAndy Whitcroft				$clean = 0;
8510a920b5bSAndy Whitcroft			}
8520a920b5bSAndy Whitcroft		}
8530a920b5bSAndy Whitcroft
854*d8aaf121SAndy Whitcroft# if and else should not have general statements after it
855*d8aaf121SAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
856*d8aaf121SAndy Whitcroft		    $1 !~ /^\s*(?:\sif|{|$)/) {
857*d8aaf121SAndy Whitcroft			print "trailing statements should be on next line\n";
858*d8aaf121SAndy Whitcroft			print "$herecurr";
859*d8aaf121SAndy Whitcroft			$clean = 0;
860*d8aaf121SAndy Whitcroft		}
861*d8aaf121SAndy Whitcroft
862653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
863653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
864653d4876SAndy Whitcroft# in a known goot container
865653d4876SAndy Whitcroft		if (($prevline=~/\#define.*\\/) and
866653d4876SAndy Whitcroft		   !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and
867653d4876SAndy Whitcroft		   !($line=~/do.*{/) and !($line=~/\(\{/) and
868653d4876SAndy Whitcroft		   !($line=~/^.\s*$Declare\s/)) {
869653d4876SAndy Whitcroft			# Grab the first statement, if that is the entire macro
870653d4876SAndy Whitcroft			# its ok.  This may start either on the #define line
871653d4876SAndy Whitcroft			# or the one below.
872*d8aaf121SAndy Whitcroft			my $ln = $linenr;
873*d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
874653d4876SAndy Whitcroft
875*d8aaf121SAndy Whitcroft			# If the macro starts on the define line start there.
876*d8aaf121SAndy Whitcroft			if ($prevline !~ m{^.#\s*define\s*$Ident(?:\([^\)]*\))?\s*\\\s*$}) {
877*d8aaf121SAndy Whitcroft				$ln--;
878*d8aaf121SAndy Whitcroft				$cnt++;
879*d8aaf121SAndy Whitcroft			}
880*d8aaf121SAndy Whitcroft			my $ctx = join('', ctx_statement($ln, $cnt));
881*d8aaf121SAndy Whitcroft
882*d8aaf121SAndy Whitcroft			if ($ctx =~ /\\$/) {
883*d8aaf121SAndy Whitcroft				if ($ctx =~ /;/) {
8840a920b5bSAndy Whitcroft					print "Macros with multiple statements should be enclosed in a do - while loop\n";
885*d8aaf121SAndy Whitcroft				} else {
886*d8aaf121SAndy Whitcroft					print "Macros with complex values should be enclosed in parenthesis\n";
887*d8aaf121SAndy Whitcroft				}
8880a920b5bSAndy Whitcroft				print "$hereprev";
8890a920b5bSAndy Whitcroft				$clean = 0;
8900a920b5bSAndy Whitcroft			}
891653d4876SAndy Whitcroft		}
8920a920b5bSAndy Whitcroft
893653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
8944a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
895653d4876SAndy Whitcroft			if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
8960a920b5bSAndy Whitcroft				print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n";
8970a920b5bSAndy Whitcroft				print "$herecurr";
8980a920b5bSAndy Whitcroft				$clean = 0;
8990a920b5bSAndy Whitcroft			}
9000a920b5bSAndy Whitcroft		}
9010a920b5bSAndy Whitcroft
9024a0df2efSAndy Whitcroft# don't use deprecated functions
9034a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
90400df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
9054a0df2efSAndy Whitcroft				print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n";
9064a0df2efSAndy Whitcroft				print "$herecurr";
9074a0df2efSAndy Whitcroft				$clean = 0;
9084a0df2efSAndy Whitcroft			}
9094a0df2efSAndy Whitcroft		}
9104a0df2efSAndy Whitcroft
9114a0df2efSAndy Whitcroft# no volatiles please
91200df344fSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
9134a0df2efSAndy Whitcroft			print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n";
9144a0df2efSAndy Whitcroft			print "$herecurr";
9154a0df2efSAndy Whitcroft			$clean = 0;
9164a0df2efSAndy Whitcroft		}
9174a0df2efSAndy Whitcroft
91800df344fSAndy Whitcroft# warn about #if 0
91900df344fSAndy Whitcroft		if ($line =~ /^.#\s*if\s+0\b/) {
92000df344fSAndy Whitcroft			print "#if 0 -- if this code redundant remove it\n";
9214a0df2efSAndy Whitcroft			print "$herecurr";
9224a0df2efSAndy Whitcroft			$clean = 0;
9234a0df2efSAndy Whitcroft		}
9244a0df2efSAndy Whitcroft
92500df344fSAndy Whitcroft# warn about #ifdefs in C files
92600df344fSAndy Whitcroft#		if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
92700df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
92800df344fSAndy Whitcroft#			print "$herecurr";
92900df344fSAndy Whitcroft#			$clean = 0;
93000df344fSAndy Whitcroft#		}
93100df344fSAndy Whitcroft
9324a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
9334a0df2efSAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
9344a0df2efSAndy Whitcroft			my $which = $1;
9354a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
9364a0df2efSAndy Whitcroft				print "$1 definition without comment\n";
9374a0df2efSAndy Whitcroft				print "$herecurr";
9384a0df2efSAndy Whitcroft				$clean = 0;
9394a0df2efSAndy Whitcroft			}
9404a0df2efSAndy Whitcroft		}
9414a0df2efSAndy Whitcroft# check for memory barriers without a comment.
9424a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
9434a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
9444a0df2efSAndy Whitcroft				print "memory barrier without comment\n";
9454a0df2efSAndy Whitcroft				print "$herecurr";
9464a0df2efSAndy Whitcroft				$clean = 0;
9474a0df2efSAndy Whitcroft			}
9484a0df2efSAndy Whitcroft		}
9494a0df2efSAndy Whitcroft# check of hardware specific defines
9504a0df2efSAndy Whitcroft		if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
9514a0df2efSAndy Whitcroft			print "architecture specific defines should be avoided\n";
9520a920b5bSAndy Whitcroft			print "$herecurr";
9530a920b5bSAndy Whitcroft			$clean = 0;
9540a920b5bSAndy Whitcroft		}
955653d4876SAndy Whitcroft
956653d4876SAndy Whitcroft		if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ ||
957653d4876SAndy Whitcroft		    $line =~ /\b(?:inline|always_inline)\s+$Storage/) {
958653d4876SAndy Whitcroft			print "inline keyword should sit between storage class and type\n";
959653d4876SAndy Whitcroft			print "$herecurr";
960653d4876SAndy Whitcroft			$clean = 0;
961653d4876SAndy Whitcroft		}
9620a920b5bSAndy Whitcroft	}
9630a920b5bSAndy Whitcroft
9640a920b5bSAndy Whitcroft	if ($chk_patch && !$is_patch) {
9650a920b5bSAndy Whitcroft		$clean = 0;
9660a920b5bSAndy Whitcroft		print "Does not appear to be a unified-diff format patch\n";
9670a920b5bSAndy Whitcroft	}
9680a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
9690a920b5bSAndy Whitcroft		$clean = 0;
9700a920b5bSAndy Whitcroft		print "Missing Signed-off-by: line(s)\n";
9710a920b5bSAndy Whitcroft	}
9720a920b5bSAndy Whitcroft
9730a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
9740a920b5bSAndy Whitcroft		print "Your patch has no obvious style problems and is ready for submission.\n"
9750a920b5bSAndy Whitcroft	}
9760a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
9770a920b5bSAndy Whitcroft		print "Your patch has style problems, please review.  If any of these errors\n";
9780a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
9790a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
9800a920b5bSAndy Whitcroft	}
9810a920b5bSAndy Whitcroft	return $clean;
9820a920b5bSAndy Whitcroft}
983