xref: /linux-6.15/scripts/checkpatch.pl (revision de7d4f0e)
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*de7d4f0eSAndy Whitcroftmy $V = '0.07';
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
249*de7d4f0eSAndy Whitcroftsub ERROR {
250*de7d4f0eSAndy Whitcroft	print "ERROR: $_[0]\n";
251*de7d4f0eSAndy Whitcroft	our $clean = 0;
252*de7d4f0eSAndy Whitcroft}
253*de7d4f0eSAndy Whitcroftsub WARN {
254*de7d4f0eSAndy Whitcroft	print "WARNING: $_[0]\n";
255*de7d4f0eSAndy Whitcroft	our $clean = 0;
256*de7d4f0eSAndy Whitcroft}
257*de7d4f0eSAndy Whitcroftsub CHK {
258*de7d4f0eSAndy Whitcroft	print "CHECK: $_[0]\n";
259*de7d4f0eSAndy Whitcroft	our $clean = 0;
260*de7d4f0eSAndy Whitcroft}
261*de7d4f0eSAndy Whitcroft
2620a920b5bSAndy Whitcroftsub process {
2630a920b5bSAndy Whitcroft	my $filename = shift;
2640a920b5bSAndy Whitcroft	my @lines = @_;
2650a920b5bSAndy Whitcroft
2660a920b5bSAndy Whitcroft	my $linenr=0;
2670a920b5bSAndy Whitcroft	my $prevline="";
2680a920b5bSAndy Whitcroft	my $stashline="";
2690a920b5bSAndy Whitcroft
2704a0df2efSAndy Whitcroft	my $length;
2710a920b5bSAndy Whitcroft	my $indent;
2720a920b5bSAndy Whitcroft	my $previndent=0;
2730a920b5bSAndy Whitcroft	my $stashindent=0;
2740a920b5bSAndy Whitcroft
275*de7d4f0eSAndy Whitcroft	our $clean = 1;
2760a920b5bSAndy Whitcroft	my $signoff = 0;
2770a920b5bSAndy Whitcroft	my $is_patch = 0;
2780a920b5bSAndy Whitcroft
2790a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
2800a920b5bSAndy Whitcroft	my $realfile = '';
2810a920b5bSAndy Whitcroft	my $realline = 0;
2820a920b5bSAndy Whitcroft	my $realcnt = 0;
2830a920b5bSAndy Whitcroft	my $here = '';
2840a920b5bSAndy Whitcroft	my $in_comment = 0;
2850a920b5bSAndy Whitcroft	my $first_line = 0;
2860a920b5bSAndy Whitcroft
287d8aaf121SAndy Whitcroft	my $Ident	= qr{[A-Za-z\d_]+};
288d8aaf121SAndy Whitcroft	my $Storage	= qr{extern|static};
289d8aaf121SAndy Whitcroft	my $Sparse	= qr{__user|__kernel|__force|__iomem};
290d8aaf121SAndy Whitcroft	my $NonptrType	= qr{
291d8aaf121SAndy Whitcroft				\b
292d8aaf121SAndy Whitcroft				(?:const\s+)?
293d8aaf121SAndy Whitcroft				(?:unsigned\s+)?
294d8aaf121SAndy Whitcroft				(?:
295d8aaf121SAndy Whitcroft					void|
296d8aaf121SAndy Whitcroft					char|
297d8aaf121SAndy Whitcroft					short|
298d8aaf121SAndy Whitcroft					int|
299d8aaf121SAndy Whitcroft					long|
300d8aaf121SAndy Whitcroft					unsigned|
301d8aaf121SAndy Whitcroft					float|
302d8aaf121SAndy Whitcroft					double|
303d8aaf121SAndy Whitcroft					long\s+int|
304d8aaf121SAndy Whitcroft					long\s+long|
305d8aaf121SAndy Whitcroft					long\s+long\s+int|
306*de7d4f0eSAndy Whitcroft					u8|u16|u32|u64|
307*de7d4f0eSAndy Whitcroft					s8|s16|s32|s64|
308d8aaf121SAndy Whitcroft					struct\s+$Ident|
309d8aaf121SAndy Whitcroft					union\s+$Ident|
310*de7d4f0eSAndy Whitcroft					enum\s+$Ident|
311d8aaf121SAndy Whitcroft					${Ident}_t
312d8aaf121SAndy Whitcroft				)
313d8aaf121SAndy Whitcroft				(?:\s+$Sparse)*
314d8aaf121SAndy Whitcroft				\b
315d8aaf121SAndy Whitcroft			  }x;
316d8aaf121SAndy Whitcroft	my $Type	= qr{
317d8aaf121SAndy Whitcroft				\b$NonptrType\b
318d8aaf121SAndy Whitcroft				(?:\s*\*+\s*const|\s*\*+)?
319d8aaf121SAndy Whitcroft			  }x;
320d8aaf121SAndy Whitcroft	my $Declare	= qr{(?:$Storage\s+)?$Type};
321d8aaf121SAndy Whitcroft	my $Attribute	= qr{__read_mostly|__init|__initdata};
322653d4876SAndy Whitcroft
323*de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
324*de7d4f0eSAndy Whitcroft	my @setup_docs = ();
325*de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
326*de7d4f0eSAndy Whitcroft	foreach my $line (@lines) {
327*de7d4f0eSAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
328*de7d4f0eSAndy Whitcroft			$setup_docs = 0;
329*de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
330*de7d4f0eSAndy Whitcroft				$setup_docs = 1;
331*de7d4f0eSAndy Whitcroft			}
332*de7d4f0eSAndy Whitcroft			next;
333*de7d4f0eSAndy Whitcroft		}
334*de7d4f0eSAndy Whitcroft
335*de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
336*de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
337*de7d4f0eSAndy Whitcroft		}
338*de7d4f0eSAndy Whitcroft	}
339*de7d4f0eSAndy Whitcroft
3400a920b5bSAndy Whitcroft	foreach my $line (@lines) {
3410a920b5bSAndy Whitcroft		$linenr++;
3420a920b5bSAndy Whitcroft
343653d4876SAndy Whitcroft		my $rawline = $line;
344653d4876SAndy Whitcroft
3450a920b5bSAndy Whitcroft#extract the filename as it passes
3460a920b5bSAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
3470a920b5bSAndy Whitcroft			$realfile=$1;
34800df344fSAndy Whitcroft			$realfile =~ s@^[^/]*/@@;
3490a920b5bSAndy Whitcroft			$in_comment = 0;
3500a920b5bSAndy Whitcroft			next;
3510a920b5bSAndy Whitcroft		}
3520a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
3530a920b5bSAndy Whitcroft		if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
3540a920b5bSAndy Whitcroft			$is_patch = 1;
3554a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
3560a920b5bSAndy Whitcroft			$in_comment = 0;
3570a920b5bSAndy Whitcroft			$realline=$1-1;
3580a920b5bSAndy Whitcroft			if (defined $2) {
3590a920b5bSAndy Whitcroft				$realcnt=$3+1;
3600a920b5bSAndy Whitcroft			} else {
3610a920b5bSAndy Whitcroft				$realcnt=1+1;
3620a920b5bSAndy Whitcroft			}
3630a920b5bSAndy Whitcroft			next;
3640a920b5bSAndy Whitcroft		}
3650a920b5bSAndy Whitcroft
3664a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
3674a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
3684a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
3694a0df2efSAndy Whitcroft		if ($line =~ /^( |\+|$)/) {
3700a920b5bSAndy Whitcroft			$realline++;
371d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
3720a920b5bSAndy Whitcroft
3730a920b5bSAndy Whitcroft			# track any sort of multi-line comment.  Obviously if
3740a920b5bSAndy Whitcroft			# the added text or context do not include the whole
3750a920b5bSAndy Whitcroft			# comment we will not see it. Such is life.
3760a920b5bSAndy Whitcroft			#
3770a920b5bSAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
3780a920b5bSAndy Whitcroft			# is the start of a diff block and this line starts
3790a920b5bSAndy Whitcroft			# ' *' then it is very likely a comment.
3804a0df2efSAndy Whitcroft			if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
3810a920b5bSAndy Whitcroft				$in_comment = 1;
3820a920b5bSAndy Whitcroft			}
3830a920b5bSAndy Whitcroft			if ($line =~ m@/\*@) {
3840a920b5bSAndy Whitcroft				$in_comment = 1;
3850a920b5bSAndy Whitcroft			}
3860a920b5bSAndy Whitcroft			if ($line =~ m@\*/@) {
3870a920b5bSAndy Whitcroft				$in_comment = 0;
3880a920b5bSAndy Whitcroft			}
3890a920b5bSAndy Whitcroft
3904a0df2efSAndy Whitcroft			# Measure the line length and indent.
3914a0df2efSAndy Whitcroft			($length, $indent) = line_stats($line);
3920a920b5bSAndy Whitcroft
3930a920b5bSAndy Whitcroft			# Track the previous line.
3940a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
3950a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
396d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
397d8aaf121SAndy Whitcroft			$realcnt--;
3980a920b5bSAndy Whitcroft		}
3990a920b5bSAndy Whitcroft
4000a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
401389834b6SRandy Dunlap		$here = "#$linenr: ";
402389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
4030a920b5bSAndy Whitcroft
40400df344fSAndy Whitcroft		my $hereline = "$here\n$line\n";
405*de7d4f0eSAndy Whitcroft		my $herecurr = "$here\n$line\n";
406*de7d4f0eSAndy Whitcroft		my $hereprev = "$here\n$prevline\n$line\n";
4070a920b5bSAndy Whitcroft
4080a920b5bSAndy Whitcroft#check the patch for a signoff:
409d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
4104a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
4114a0df2efSAndy Whitcroft			$signoff++;
4120a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
413*de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
414*de7d4f0eSAndy Whitcroft					$herecurr);
4150a920b5bSAndy Whitcroft			}
4160a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
417*de7d4f0eSAndy Whitcroft				WARN("need space after Signed-off-by:\n" .
418*de7d4f0eSAndy Whitcroft					$herecurr);
4190a920b5bSAndy Whitcroft			}
4200a920b5bSAndy Whitcroft		}
4210a920b5bSAndy Whitcroft
42200df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
42300df344fSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
424*de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
425*de7d4f0eSAndy Whitcroft				$herecurr);
426*de7d4f0eSAndy Whitcroft		}
427*de7d4f0eSAndy Whitcroft
428*de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
429*de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
430*de7d4f0eSAndy Whitcroft		     !($line =~ m/^(
431*de7d4f0eSAndy Whitcroft				[\x09\x0A\x0D\x20-\x7E]              # ASCII
432*de7d4f0eSAndy Whitcroft				| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
433*de7d4f0eSAndy Whitcroft				|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
434*de7d4f0eSAndy Whitcroft				| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
435*de7d4f0eSAndy Whitcroft				|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
436*de7d4f0eSAndy Whitcroft				|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
437*de7d4f0eSAndy Whitcroft				| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
438*de7d4f0eSAndy Whitcroft				|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
439*de7d4f0eSAndy Whitcroft				)*$/x )) {
440*de7d4f0eSAndy Whitcroft			ERROR("Invalid UTF-8\n" . $herecurr);
44100df344fSAndy Whitcroft		}
4420a920b5bSAndy Whitcroft
44300df344fSAndy Whitcroft#ignore lines being removed
44400df344fSAndy Whitcroft		if ($line=~/^-/) {next;}
44500df344fSAndy Whitcroft
44600df344fSAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
44700df344fSAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
4480a920b5bSAndy Whitcroft
4490a920b5bSAndy Whitcroft#trailing whitespace
450d8aaf121SAndy Whitcroft		if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
451*de7d4f0eSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($line) . "\n";
452*de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
4530a920b5bSAndy Whitcroft		}
4540a920b5bSAndy Whitcroft#80 column limit
45500df344fSAndy Whitcroft		if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
456*de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
4570a920b5bSAndy Whitcroft		}
4580a920b5bSAndy Whitcroft
4590a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk
4600a920b5bSAndy Whitcroft		next if ($realfile !~ /\.[hc]$/);
4610a920b5bSAndy Whitcroft
4620a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
4630a920b5bSAndy Whitcroft# more than 8 must use tabs.
4640a920b5bSAndy Whitcroft		if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s*        \s*/) {
465*de7d4f0eSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($line) . "\n";
466*de7d4f0eSAndy Whitcroft			ERROR("use tabs not spaces\n" . $herevet);
4670a920b5bSAndy Whitcroft		}
4680a920b5bSAndy Whitcroft
4690a920b5bSAndy Whitcroft		#
4700a920b5bSAndy Whitcroft		# The rest of our checks refer specifically to C style
4710a920b5bSAndy Whitcroft		# only apply those _outside_ comments.
4720a920b5bSAndy Whitcroft		#
4730a920b5bSAndy Whitcroft		next if ($in_comment);
4740a920b5bSAndy Whitcroft
4750a920b5bSAndy Whitcroft# Remove comments from the line before processing.
4760a920b5bSAndy Whitcroft		$line =~ s@/\*.*\*/@@g;
4770a920b5bSAndy Whitcroft		$line =~ s@/\*.*@@;
4780a920b5bSAndy Whitcroft		$line =~ s@.*\*/@@;
47900df344fSAndy Whitcroft
480653d4876SAndy Whitcroft# Standardise the strings and chars within the input to simplify matching.
481653d4876SAndy Whitcroft		$line = sanitise_line($line);
482653d4876SAndy Whitcroft
48300df344fSAndy Whitcroft#
48400df344fSAndy Whitcroft# Checks which may be anchored in the context.
48500df344fSAndy Whitcroft#
48600df344fSAndy Whitcroft
48700df344fSAndy Whitcroft# Check for switch () and associated case and default
48800df344fSAndy Whitcroft# statements should be at the same indent.
48900df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
49000df344fSAndy Whitcroft			my $err = '';
49100df344fSAndy Whitcroft			my $sep = '';
49200df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
49300df344fSAndy Whitcroft			shift(@ctx);
49400df344fSAndy Whitcroft			for my $ctx (@ctx) {
49500df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
49600df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
49700df344fSAndy Whitcroft							$indent != $cindent) {
49800df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
49900df344fSAndy Whitcroft					$sep = '';
50000df344fSAndy Whitcroft				} else {
50100df344fSAndy Whitcroft					$sep = "[...]\n";
50200df344fSAndy Whitcroft				}
50300df344fSAndy Whitcroft			}
50400df344fSAndy Whitcroft			if ($err ne '') {
505*de7d4f0eSAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline\n$err\n");
506*de7d4f0eSAndy Whitcroft			}
507*de7d4f0eSAndy Whitcroft		}
508*de7d4f0eSAndy Whitcroft
509*de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
510*de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
511*de7d4f0eSAndy Whitcroft		if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
512*de7d4f0eSAndy Whitcroft			my @ctx = ctx_statement($linenr, $realcnt);
513*de7d4f0eSAndy Whitcroft			my $ctx_ln = $linenr + $#ctx + 1;
514*de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
515*de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
516*de7d4f0eSAndy Whitcroft
517*de7d4f0eSAndy Whitcroft			while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
518*de7d4f0eSAndy Whitcroft				$ctx_ln++;
519*de7d4f0eSAndy Whitcroft				$ctx_cnt--;
520*de7d4f0eSAndy Whitcroft			}
521*de7d4f0eSAndy Whitcroft			##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
522*de7d4f0eSAndy Whitcroft
523*de7d4f0eSAndy Whitcroft			if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
524*de7d4f0eSAndy Whitcroft				ERROR("That { should be on the previous line\n" .
525*de7d4f0eSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]");
52600df344fSAndy Whitcroft			}
52700df344fSAndy Whitcroft		}
52800df344fSAndy Whitcroft
52900df344fSAndy Whitcroft#ignore lines not being added
53000df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
53100df344fSAndy Whitcroft
532653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
533653d4876SAndy Whitcroft		if ($tst_type && $line =~ /^.$Declare$/) {
534*de7d4f0eSAndy Whitcroft			ERROR("TEST: is type $Declare\n" . $herecurr);
535653d4876SAndy Whitcroft			next;
536653d4876SAndy Whitcroft		}
537653d4876SAndy Whitcroft
53800df344fSAndy Whitcroft#
53900df344fSAndy Whitcroft# Checks which are anchored on the added line.
54000df344fSAndy Whitcroft#
54100df344fSAndy Whitcroft
542653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
543653d4876SAndy Whitcroft		if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
544653d4876SAndy Whitcroft			my $path = $1;
545653d4876SAndy Whitcroft			if ($path =~ m{//}) {
546*de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
547*de7d4f0eSAndy Whitcroft					$herecurr);
548653d4876SAndy Whitcroft			}
549653d4876SAndy Whitcroft			# Sanitise this special form of string.
550653d4876SAndy Whitcroft			$path = 'X' x length($path);
551653d4876SAndy Whitcroft			$line =~ s{\<.*\>}{<$path>};
552653d4876SAndy Whitcroft		}
553653d4876SAndy Whitcroft
55400df344fSAndy Whitcroft# no C99 // comments
55500df344fSAndy Whitcroft		if ($line =~ m{//}) {
556*de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
55700df344fSAndy Whitcroft		}
55800df344fSAndy Whitcroft		# Remove C99 comments.
5590a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
5600a920b5bSAndy Whitcroft
5610a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
562653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
563653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
564653d4876SAndy Whitcroft			my $name = $1;
5650a920b5bSAndy Whitcroft			if (($prevline !~ /^}/) &&
5660a920b5bSAndy Whitcroft			   ($prevline !~ /^\+}/) &&
567653d4876SAndy Whitcroft			   ($prevline !~ /^ }/) &&
568653d4876SAndy Whitcroft			   ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) {
569*de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
5700a920b5bSAndy Whitcroft			}
5710a920b5bSAndy Whitcroft		}
5720a920b5bSAndy Whitcroft
5730a920b5bSAndy Whitcroft# check for static initialisers.
5740a920b5bSAndy Whitcroft		if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
575*de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
576*de7d4f0eSAndy Whitcroft				$herecurr);
5770a920b5bSAndy Whitcroft		}
5780a920b5bSAndy Whitcroft
579653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
580653d4876SAndy Whitcroft# make sense.
581653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
582*de7d4f0eSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+\(\s*\*$Ident\s*\)\s*\(/ &&
583653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
584*de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
5850a920b5bSAndy Whitcroft		}
5860a920b5bSAndy Whitcroft
5870a920b5bSAndy Whitcroft# * goes on variable not on type
588d8aaf121SAndy Whitcroft		if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
589*de7d4f0eSAndy Whitcroft			ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
590*de7d4f0eSAndy Whitcroft				$herecurr);
591d8aaf121SAndy Whitcroft
592d8aaf121SAndy Whitcroft		} elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
593*de7d4f0eSAndy Whitcroft			ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
594*de7d4f0eSAndy Whitcroft				$herecurr);
595d8aaf121SAndy Whitcroft
596d8aaf121SAndy Whitcroft		} elsif ($line =~ m{$NonptrType(\*+)(?:\s+const)?\s+[A-Za-z\d_]+}) {
597*de7d4f0eSAndy Whitcroft			ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
598*de7d4f0eSAndy Whitcroft				$herecurr);
599d8aaf121SAndy Whitcroft
600d8aaf121SAndy Whitcroft		} elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+const)\s+[A-Za-z\d_]+}) {
601*de7d4f0eSAndy Whitcroft			ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
602*de7d4f0eSAndy Whitcroft				$herecurr);
6030a920b5bSAndy Whitcroft		}
6040a920b5bSAndy Whitcroft
6050a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
6060a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
6070a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
6080a920b5bSAndy Whitcroft# 			print "$herecurr";
6090a920b5bSAndy Whitcroft# 			$clean = 0;
6100a920b5bSAndy Whitcroft# 		}
6110a920b5bSAndy Whitcroft
61200df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
61300df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
61400df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
61500df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
61600df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
6170a920b5bSAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)/) {
61800df344fSAndy Whitcroft			my $ok = 0;
61900df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
62000df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
62100df344fSAndy Whitcroft				# we have a preceeding printk if it ends
62200df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
62300df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
62400df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
62500df344fSAndy Whitcroft						$ok = 1;
62600df344fSAndy Whitcroft					}
62700df344fSAndy Whitcroft					last;
62800df344fSAndy Whitcroft				}
62900df344fSAndy Whitcroft			}
63000df344fSAndy Whitcroft			if ($ok == 0) {
631*de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
6320a920b5bSAndy Whitcroft			}
63300df344fSAndy Whitcroft		}
6340a920b5bSAndy Whitcroft
635653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
636653d4876SAndy Whitcroft# or if closed on same line
637d8aaf121SAndy Whitcroft		if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
6380a920b5bSAndy Whitcroft		    !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
639*de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
6400a920b5bSAndy Whitcroft		}
641653d4876SAndy Whitcroft
642653d4876SAndy Whitcroft# Check operator spacing.
6434a0df2efSAndy Whitcroft		# Note we expand the line with the leading + as the real
6444a0df2efSAndy Whitcroft		# line will be displayed with the leading + and the tabs
6454a0df2efSAndy Whitcroft		# will therefore also expand that way.
6460a920b5bSAndy Whitcroft		my $opline = $line;
6474a0df2efSAndy Whitcroft		$opline = expand_tabs($opline);
6480a920b5bSAndy Whitcroft		$opline =~ s/^./ /;
6490a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
6500a920b5bSAndy Whitcroft			my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
65100df344fSAndy Whitcroft			my $off = 0;
6520a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
6534a0df2efSAndy Whitcroft				$off += length($elements[$n]);
6544a0df2efSAndy Whitcroft
6554a0df2efSAndy Whitcroft				my $a = '';
6564a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
6574a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
6584a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
6594a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
6604a0df2efSAndy Whitcroft				$a = 'E' if ($elements[$n] eq '' && $n == 0);
6614a0df2efSAndy Whitcroft
6620a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
6634a0df2efSAndy Whitcroft
6644a0df2efSAndy Whitcroft				my $c = '';
6650a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
6664a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
6674a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
6684a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
6694a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
6704a0df2efSAndy Whitcroft				} else {
6714a0df2efSAndy Whitcroft					$c = 'E';
6720a920b5bSAndy Whitcroft				}
6730a920b5bSAndy Whitcroft
67400df344fSAndy Whitcroft				# Pick up the preceeding and succeeding characters.
675*de7d4f0eSAndy Whitcroft				my $ca = substr($opline, 0, $off);
67600df344fSAndy Whitcroft				my $cc = '';
677653d4876SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
678d8aaf121SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
67900df344fSAndy Whitcroft				}
680*de7d4f0eSAndy Whitcroft				my $cb = "$ca$;$cc";
68100df344fSAndy Whitcroft
6824a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
6834a0df2efSAndy Whitcroft
6844a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
6854a0df2efSAndy Whitcroft
6864a0df2efSAndy Whitcroft				my $ptr = (" " x $off) . "^";
687*de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
6880a920b5bSAndy Whitcroft
6890a920b5bSAndy Whitcroft				##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
6900a920b5bSAndy Whitcroft
691d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
692d8aaf121SAndy Whitcroft				if ($op eq ';') {
693*de7d4f0eSAndy Whitcroft					if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
694*de7d4f0eSAndy Whitcroft					    $cc !~ /^;/) {
695*de7d4f0eSAndy Whitcroft						ERROR("need space after that '$op' $at\n" . $hereptr);
696d8aaf121SAndy Whitcroft					}
697d8aaf121SAndy Whitcroft
698d8aaf121SAndy Whitcroft				# // is a comment
699d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
7000a920b5bSAndy Whitcroft
7010a920b5bSAndy Whitcroft				# -> should have no spaces
7020a920b5bSAndy Whitcroft				} elsif ($op eq '->') {
7034a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
704*de7d4f0eSAndy Whitcroft						ERROR("no spaces around that '$op' $at\n" . $hereptr);
7050a920b5bSAndy Whitcroft					}
7060a920b5bSAndy Whitcroft
7070a920b5bSAndy Whitcroft				# , must have a space on the right.
7080a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
709d8aaf121SAndy Whitcroft					if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
710*de7d4f0eSAndy Whitcroft						ERROR("need space after that '$op' $at\n" . $hereptr);
7110a920b5bSAndy Whitcroft					}
7120a920b5bSAndy Whitcroft
7130a920b5bSAndy Whitcroft				# unary ! and unary ~ are allowed no space on the right
7140a920b5bSAndy Whitcroft				} elsif ($op eq '!' or $op eq '~') {
7154a0df2efSAndy Whitcroft					if ($ctx !~ /[WOEB]x./) {
716*de7d4f0eSAndy Whitcroft						ERROR("need space before that '$op' $at\n" . $hereptr);
7170a920b5bSAndy Whitcroft					}
7184a0df2efSAndy Whitcroft					if ($ctx =~ /.xW/) {
719*de7d4f0eSAndy Whitcroft						ERROR("no space after that '$op' $at\n" . $hereptr);
7200a920b5bSAndy Whitcroft					}
7210a920b5bSAndy Whitcroft
7220a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
7230a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
724d8aaf121SAndy Whitcroft					if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
725*de7d4f0eSAndy Whitcroft						ERROR("need space one side of that '$op' $at\n" . $hereptr);
7260a920b5bSAndy Whitcroft					}
727d8aaf121SAndy Whitcroft					if ($ctx =~ /Wx./ && $cc =~ /^;/) {
728*de7d4f0eSAndy Whitcroft						ERROR("no space before that '$op' $at\n" . $hereptr);
729653d4876SAndy Whitcroft					}
7300a920b5bSAndy Whitcroft
7310a920b5bSAndy Whitcroft				# & is both unary and binary
7320a920b5bSAndy Whitcroft				# unary:
7330a920b5bSAndy Whitcroft				# 	a &b
7340a920b5bSAndy Whitcroft				# binary (consistent spacing):
7350a920b5bSAndy Whitcroft				#	a&b		OK
7360a920b5bSAndy Whitcroft				#	a & b		OK
7370a920b5bSAndy Whitcroft				#
7380a920b5bSAndy Whitcroft				# boiling down to: if there is a space on the right then there
7390a920b5bSAndy Whitcroft				# should be one on the left.
7400a920b5bSAndy Whitcroft				#
7410a920b5bSAndy Whitcroft				# - is the same
7420a920b5bSAndy Whitcroft				#
7434a0df2efSAndy Whitcroft				} elsif ($op eq '&' or $op eq '-') {
74400df344fSAndy Whitcroft					if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
745*de7d4f0eSAndy Whitcroft						ERROR("need space before that '$op' $at\n" . $hereptr);
7464a0df2efSAndy Whitcroft					}
7474a0df2efSAndy Whitcroft
74800df344fSAndy Whitcroft				# * is the same as & only adding:
74900df344fSAndy Whitcroft				# type:
75000df344fSAndy Whitcroft				# 	(foo *)
75100df344fSAndy Whitcroft				#	(foo **)
75200df344fSAndy Whitcroft				#
7534a0df2efSAndy Whitcroft				} elsif ($op eq '*') {
754*de7d4f0eSAndy Whitcroft					if ($ca !~ /$Type$/ && $cb !~ /(\*$;|$;\*)/ &&
755*de7d4f0eSAndy Whitcroft					    $ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
756*de7d4f0eSAndy Whitcroft						ERROR("need space before that '$op' $at\n" . $hereptr);
7570a920b5bSAndy Whitcroft					}
7580a920b5bSAndy Whitcroft
7590a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
7600a920b5bSAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
7610a920b5bSAndy Whitcroft					 $op eq '^' or $op eq '|')
7620a920b5bSAndy Whitcroft				{
7634a0df2efSAndy Whitcroft					if ($ctx !~ /VxV|WxW|VxE|WxE/) {
764*de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
765*de7d4f0eSAndy Whitcroft							$hereptr);
7660a920b5bSAndy Whitcroft					}
7670a920b5bSAndy Whitcroft
7680a920b5bSAndy Whitcroft				# All the others need spaces both sides.
7694a0df2efSAndy Whitcroft				} elsif ($ctx !~ /[EW]x[WE]/) {
770*de7d4f0eSAndy Whitcroft					ERROR("need spaces around that '$op' $at\n" . $hereptr);
7710a920b5bSAndy Whitcroft				}
7724a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
7730a920b5bSAndy Whitcroft			}
7740a920b5bSAndy Whitcroft		}
7750a920b5bSAndy Whitcroft
7760a920b5bSAndy Whitcroft#need space before brace following if, while, etc
777*de7d4f0eSAndy Whitcroft		if ($line =~ /\(.*\){/ || $line =~ /do{/) {
778*de7d4f0eSAndy Whitcroft			ERROR("need a space before the open brace '{'\n" . $herecurr);
779*de7d4f0eSAndy Whitcroft		}
780*de7d4f0eSAndy Whitcroft
781*de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
782*de7d4f0eSAndy Whitcroft# on the line
783*de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
784*de7d4f0eSAndy Whitcroft			ERROR("need a space after that close brace '}'\n" . $herecurr);
7850a920b5bSAndy Whitcroft		}
7860a920b5bSAndy Whitcroft
7870a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
7884a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
7890a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
790*de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
7910a920b5bSAndy Whitcroft		}
7920a920b5bSAndy Whitcroft
7930a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
7944a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
795*de7d4f0eSAndy Whitcroft			ERROR("need a space before the open parenthesis '('\n" . $herecurr);
7960a920b5bSAndy Whitcroft		}
7970a920b5bSAndy Whitcroft
7980a920b5bSAndy Whitcroft# Check for illegal assignment in if conditional.
799653d4876SAndy Whitcroft		if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
80000df344fSAndy Whitcroft			#next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
801*de7d4f0eSAndy Whitcroft			ERROR("do not use assignment in if condition\n" . $herecurr);
8020a920b5bSAndy Whitcroft		}
8030a920b5bSAndy Whitcroft
8040a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
8050a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
8060a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
8070a920b5bSAndy Whitcroft						$previndent == $indent) {
808*de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
8090a920b5bSAndy Whitcroft		}
8100a920b5bSAndy Whitcroft
8110a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
8120a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
8130a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
8140a920b5bSAndy Whitcroft#		    print "$herecurr";
8150a920b5bSAndy Whitcroft#		    $clean = 0;
8160a920b5bSAndy Whitcroft#		}
8170a920b5bSAndy Whitcroft
8180a920b5bSAndy Whitcroft#no spaces allowed after \ in define
8190a920b5bSAndy Whitcroft		if ($line=~/\#define.*\\\s$/) {
820*de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
8210a920b5bSAndy Whitcroft		}
8220a920b5bSAndy Whitcroft
823653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
824653d4876SAndy Whitcroft		if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
8250a920b5bSAndy Whitcroft			my $checkfile = "include/linux/$1.h";
8260a920b5bSAndy Whitcroft			if (-f $checkfile) {
827*de7d4f0eSAndy Whitcroft				CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
828*de7d4f0eSAndy Whitcroft					$herecurr);
8290a920b5bSAndy Whitcroft			}
8300a920b5bSAndy Whitcroft		}
8310a920b5bSAndy Whitcroft
832d8aaf121SAndy Whitcroft# if and else should not have general statements after it
833d8aaf121SAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
834*de7d4f0eSAndy Whitcroft		    $1 !~ /^\s*(?:\sif|{|\\|$)/) {
835*de7d4f0eSAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
836d8aaf121SAndy Whitcroft		}
837d8aaf121SAndy Whitcroft
838653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
839653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
840653d4876SAndy Whitcroft# in a known goot container
841653d4876SAndy Whitcroft		if (($prevline=~/\#define.*\\/) and
842653d4876SAndy Whitcroft		   !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and
843653d4876SAndy Whitcroft		   !($line=~/do.*{/) and !($line=~/\(\{/) and
844653d4876SAndy Whitcroft		   !($line=~/^.\s*$Declare\s/)) {
845653d4876SAndy Whitcroft			# Grab the first statement, if that is the entire macro
846653d4876SAndy Whitcroft			# its ok.  This may start either on the #define line
847653d4876SAndy Whitcroft			# or the one below.
848d8aaf121SAndy Whitcroft			my $ln = $linenr;
849d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
850653d4876SAndy Whitcroft
851d8aaf121SAndy Whitcroft			# If the macro starts on the define line start there.
852d8aaf121SAndy Whitcroft			if ($prevline !~ m{^.#\s*define\s*$Ident(?:\([^\)]*\))?\s*\\\s*$}) {
853d8aaf121SAndy Whitcroft				$ln--;
854d8aaf121SAndy Whitcroft				$cnt++;
855d8aaf121SAndy Whitcroft			}
856*de7d4f0eSAndy Whitcroft			my @ctx = ctx_statement($ln, $cnt);
857*de7d4f0eSAndy Whitcroft			my $ctx_ln = $ln + $#ctx + 1;
858*de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
859*de7d4f0eSAndy Whitcroft
860*de7d4f0eSAndy Whitcroft			# Pull in any empty extension lines.
861*de7d4f0eSAndy Whitcroft			while ($ctx =~ /\\$/ &&
862*de7d4f0eSAndy Whitcroft			       $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
863*de7d4f0eSAndy Whitcroft				$ctx .= $lines[$ctx_ln - 1];
864*de7d4f0eSAndy Whitcroft				$ctx_ln++;
865*de7d4f0eSAndy Whitcroft			}
866d8aaf121SAndy Whitcroft
867d8aaf121SAndy Whitcroft			if ($ctx =~ /\\$/) {
868d8aaf121SAndy Whitcroft				if ($ctx =~ /;/) {
869*de7d4f0eSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
870d8aaf121SAndy Whitcroft				} else {
871*de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
872d8aaf121SAndy Whitcroft				}
8730a920b5bSAndy Whitcroft			}
874653d4876SAndy Whitcroft		}
8750a920b5bSAndy Whitcroft
876653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
8774a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
878653d4876SAndy Whitcroft			if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
879*de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
8800a920b5bSAndy Whitcroft			}
8810a920b5bSAndy Whitcroft		}
8820a920b5bSAndy Whitcroft
8834a0df2efSAndy Whitcroft# don't use deprecated functions
8844a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
88500df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
886*de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
8874a0df2efSAndy Whitcroft			}
8884a0df2efSAndy Whitcroft		}
8894a0df2efSAndy Whitcroft
8904a0df2efSAndy Whitcroft# no volatiles please
89100df344fSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
892*de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
8934a0df2efSAndy Whitcroft		}
8944a0df2efSAndy Whitcroft
89500df344fSAndy Whitcroft# warn about #if 0
89600df344fSAndy Whitcroft		if ($line =~ /^.#\s*if\s+0\b/) {
897*de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
898*de7d4f0eSAndy Whitcroft				$herecurr);
8994a0df2efSAndy Whitcroft		}
9004a0df2efSAndy Whitcroft
90100df344fSAndy Whitcroft# warn about #ifdefs in C files
90200df344fSAndy Whitcroft#		if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
90300df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
90400df344fSAndy Whitcroft#			print "$herecurr";
90500df344fSAndy Whitcroft#			$clean = 0;
90600df344fSAndy Whitcroft#		}
90700df344fSAndy Whitcroft
9084a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
9094a0df2efSAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
9104a0df2efSAndy Whitcroft			my $which = $1;
9114a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
912*de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
9134a0df2efSAndy Whitcroft			}
9144a0df2efSAndy Whitcroft		}
9154a0df2efSAndy Whitcroft# check for memory barriers without a comment.
9164a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
9174a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
918*de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
9194a0df2efSAndy Whitcroft			}
9204a0df2efSAndy Whitcroft		}
9214a0df2efSAndy Whitcroft# check of hardware specific defines
9224a0df2efSAndy Whitcroft		if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
923*de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
9240a920b5bSAndy Whitcroft		}
925653d4876SAndy Whitcroft
926*de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
927*de7d4f0eSAndy Whitcroft# storage class and type.
928653d4876SAndy Whitcroft		if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ ||
929653d4876SAndy Whitcroft		    $line =~ /\b(?:inline|always_inline)\s+$Storage/) {
930*de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
931*de7d4f0eSAndy Whitcroft		}
932*de7d4f0eSAndy Whitcroft
933*de7d4f0eSAndy Whitcroft# check for new externs in .c files.
934*de7d4f0eSAndy Whitcroft		if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
935*de7d4f0eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
936*de7d4f0eSAndy Whitcroft		}
937*de7d4f0eSAndy Whitcroft
938*de7d4f0eSAndy Whitcroft# checks for new __setup's
939*de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
940*de7d4f0eSAndy Whitcroft			my $name = $1;
941*de7d4f0eSAndy Whitcroft
942*de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
943*de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
944*de7d4f0eSAndy Whitcroft			}
945653d4876SAndy Whitcroft		}
9460a920b5bSAndy Whitcroft	}
9470a920b5bSAndy Whitcroft
9480a920b5bSAndy Whitcroft	if ($chk_patch && !$is_patch) {
949*de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
9500a920b5bSAndy Whitcroft	}
9510a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
952*de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
9530a920b5bSAndy Whitcroft	}
9540a920b5bSAndy Whitcroft
9550a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
9560a920b5bSAndy Whitcroft		print "Your patch has no obvious style problems and is ready for submission.\n"
9570a920b5bSAndy Whitcroft	}
9580a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
9590a920b5bSAndy Whitcroft		print "Your patch has style problems, please review.  If any of these errors\n";
9600a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
9610a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
9620a920b5bSAndy Whitcroft	}
9630a920b5bSAndy Whitcroft	return $clean;
9640a920b5bSAndy Whitcroft}
965