xref: /linux-6.15/scripts/checkpatch.pl (revision 22f2a2ef)
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*22f2a2efSAndy Whitcroftmy $V = '0.09';
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>) {
50f0a594c1SAndy Whitcroft		if (/^Check:\s+(.*\S)/) {
51f0a594c1SAndy Whitcroft			for my $entry (split(/[, ]+/, $1)) {
52f0a594c1SAndy Whitcroft				if ($entry =~ m@include/(.*)@) {
534a0df2efSAndy Whitcroft					push(@dep_includes, $1);
544a0df2efSAndy Whitcroft
55f0a594c1SAndy Whitcroft				} elsif ($entry !~ m@/@) {
56f0a594c1SAndy Whitcroft					push(@dep_functions, $entry);
57f0a594c1SAndy Whitcroft				}
584a0df2efSAndy Whitcroft			}
590a920b5bSAndy Whitcroft		}
600a920b5bSAndy Whitcroft	}
610a920b5bSAndy Whitcroft}
620a920b5bSAndy Whitcroft
6300df344fSAndy Whitcroftmy @rawlines = ();
640a920b5bSAndy Whitcroftwhile (<>) {
650a920b5bSAndy Whitcroft	chomp;
6600df344fSAndy Whitcroft	push(@rawlines, $_);
670a920b5bSAndy Whitcroft	if (eof(ARGV)) {
6800df344fSAndy Whitcroft		if (!process($ARGV, @rawlines)) {
690a920b5bSAndy Whitcroft			$exit = 1;
700a920b5bSAndy Whitcroft		}
7100df344fSAndy Whitcroft		@rawlines = ();
720a920b5bSAndy Whitcroft	}
730a920b5bSAndy Whitcroft}
740a920b5bSAndy Whitcroft
750a920b5bSAndy Whitcroftexit($exit);
760a920b5bSAndy Whitcroft
770a920b5bSAndy Whitcroftsub top_of_kernel_tree {
780a920b5bSAndy Whitcroft	if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") &&
790a920b5bSAndy Whitcroft	    (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") &&
800a920b5bSAndy Whitcroft	    (-d "Documentation") && (-d "arch") && (-d "include") &&
810a920b5bSAndy Whitcroft	    (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") &&
820a920b5bSAndy Whitcroft	    (-d "kernel") && (-d "lib") && (-d "scripts")) {
830a920b5bSAndy Whitcroft		return 1;
840a920b5bSAndy Whitcroft	}
850a920b5bSAndy Whitcroft	return 0;
860a920b5bSAndy Whitcroft}
870a920b5bSAndy Whitcroft
880a920b5bSAndy Whitcroftsub expand_tabs {
890a920b5bSAndy Whitcroft	my ($str) = @_;
900a920b5bSAndy Whitcroft
910a920b5bSAndy Whitcroft	my $res = '';
920a920b5bSAndy Whitcroft	my $n = 0;
930a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
940a920b5bSAndy Whitcroft		if ($c eq "\t") {
950a920b5bSAndy Whitcroft			$res .= ' ';
960a920b5bSAndy Whitcroft			$n++;
970a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
980a920b5bSAndy Whitcroft				$res .= ' ';
990a920b5bSAndy Whitcroft			}
1000a920b5bSAndy Whitcroft			next;
1010a920b5bSAndy Whitcroft		}
1020a920b5bSAndy Whitcroft		$res .= $c;
1030a920b5bSAndy Whitcroft		$n++;
1040a920b5bSAndy Whitcroft	}
1050a920b5bSAndy Whitcroft
1060a920b5bSAndy Whitcroft	return $res;
1070a920b5bSAndy Whitcroft}
1080a920b5bSAndy Whitcroft
1094a0df2efSAndy Whitcroftsub line_stats {
1104a0df2efSAndy Whitcroft	my ($line) = @_;
1114a0df2efSAndy Whitcroft
1124a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
1134a0df2efSAndy Whitcroft	$line =~ s/^.//;
1144a0df2efSAndy Whitcroft	$line = expand_tabs($line);
1154a0df2efSAndy Whitcroft
1164a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
1174a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
1184a0df2efSAndy Whitcroft
1194a0df2efSAndy Whitcroft	return (length($line), length($white));
1204a0df2efSAndy Whitcroft}
1214a0df2efSAndy Whitcroft
12200df344fSAndy Whitcroftsub sanitise_line {
12300df344fSAndy Whitcroft	my ($line) = @_;
12400df344fSAndy Whitcroft
12500df344fSAndy Whitcroft	my $res = '';
12600df344fSAndy Whitcroft	my $l = '';
12700df344fSAndy Whitcroft
12800df344fSAndy Whitcroft	my $quote = '';
12900df344fSAndy Whitcroft
13000df344fSAndy Whitcroft	foreach my $c (split(//, $line)) {
13100df344fSAndy Whitcroft		if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
13200df344fSAndy Whitcroft			if ($quote eq '') {
13300df344fSAndy Whitcroft				$quote = $c;
13400df344fSAndy Whitcroft				$res .= $c;
13500df344fSAndy Whitcroft				$l = $c;
13600df344fSAndy Whitcroft				next;
13700df344fSAndy Whitcroft			} elsif ($quote eq $c) {
13800df344fSAndy Whitcroft				$quote = '';
13900df344fSAndy Whitcroft			}
14000df344fSAndy Whitcroft		}
14100df344fSAndy Whitcroft		if ($quote && $c ne "\t") {
14200df344fSAndy Whitcroft			$res .= "X";
14300df344fSAndy Whitcroft		} else {
14400df344fSAndy Whitcroft			$res .= $c;
14500df344fSAndy Whitcroft		}
14600df344fSAndy Whitcroft
14700df344fSAndy Whitcroft		$l = $c;
14800df344fSAndy Whitcroft	}
14900df344fSAndy Whitcroft
15000df344fSAndy Whitcroft	return $res;
15100df344fSAndy Whitcroft}
15200df344fSAndy Whitcroft
1534a0df2efSAndy Whitcroftsub ctx_block_get {
154f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1554a0df2efSAndy Whitcroft	my $line;
1564a0df2efSAndy Whitcroft	my $start = $linenr - 1;
1574a0df2efSAndy Whitcroft	my $blk = '';
1584a0df2efSAndy Whitcroft	my @o;
1594a0df2efSAndy Whitcroft	my @c;
1604a0df2efSAndy Whitcroft	my @res = ();
1614a0df2efSAndy Whitcroft
162f0a594c1SAndy Whitcroft	my $level = 0;
16300df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
16400df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
16500df344fSAndy Whitcroft		$remain--;
16600df344fSAndy Whitcroft
16700df344fSAndy Whitcroft		$blk .= $rawlines[$line];
168f0a594c1SAndy Whitcroft		foreach my $c (split(//, $rawlines[$line])) {
169f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
170f0a594c1SAndy Whitcroft			if ($off > 0) {
171f0a594c1SAndy Whitcroft				$off--;
172f0a594c1SAndy Whitcroft				next;
173f0a594c1SAndy Whitcroft			}
1744a0df2efSAndy Whitcroft
175f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
176f0a594c1SAndy Whitcroft				$level--;
177f0a594c1SAndy Whitcroft				last if ($level == 0);
178f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
179f0a594c1SAndy Whitcroft				$level++;
180f0a594c1SAndy Whitcroft			}
181f0a594c1SAndy Whitcroft		}
1824a0df2efSAndy Whitcroft
183f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
18400df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
1854a0df2efSAndy Whitcroft		}
1864a0df2efSAndy Whitcroft
187f0a594c1SAndy Whitcroft		last if ($level == 0);
1884a0df2efSAndy Whitcroft	}
1894a0df2efSAndy Whitcroft
190f0a594c1SAndy Whitcroft	return ($level, @res);
1914a0df2efSAndy Whitcroft}
1924a0df2efSAndy Whitcroftsub ctx_block_outer {
1934a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
1944a0df2efSAndy Whitcroft
195f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
196f0a594c1SAndy Whitcroft	return @r;
1974a0df2efSAndy Whitcroft}
1984a0df2efSAndy Whitcroftsub ctx_block {
1994a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
2004a0df2efSAndy Whitcroft
201f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
202f0a594c1SAndy Whitcroft	return @r;
203653d4876SAndy Whitcroft}
204653d4876SAndy Whitcroftsub ctx_statement {
205f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
206f0a594c1SAndy Whitcroft
207f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
208f0a594c1SAndy Whitcroft	return @r;
209f0a594c1SAndy Whitcroft}
210f0a594c1SAndy Whitcroftsub ctx_block_level {
211653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
212653d4876SAndy Whitcroft
213f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
2144a0df2efSAndy Whitcroft}
2154a0df2efSAndy Whitcroft
2164a0df2efSAndy Whitcroftsub ctx_locate_comment {
2174a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
2184a0df2efSAndy Whitcroft
2194a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
22000df344fSAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
2214a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
2224a0df2efSAndy Whitcroft
2234a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
2244a0df2efSAndy Whitcroft	# comment.
2254a0df2efSAndy Whitcroft	my $in_comment = 0;
2264a0df2efSAndy Whitcroft	$current_comment = '';
2274a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
22800df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
22900df344fSAndy Whitcroft		#warn "           $line\n";
2304a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
2314a0df2efSAndy Whitcroft			$in_comment = 1;
2324a0df2efSAndy Whitcroft		}
2334a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
2344a0df2efSAndy Whitcroft			$in_comment = 1;
2354a0df2efSAndy Whitcroft		}
2364a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
2374a0df2efSAndy Whitcroft			$current_comment = '';
2384a0df2efSAndy Whitcroft		}
2394a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
2404a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
2414a0df2efSAndy Whitcroft			$in_comment = 0;
2424a0df2efSAndy Whitcroft		}
2434a0df2efSAndy Whitcroft	}
2444a0df2efSAndy Whitcroft
2454a0df2efSAndy Whitcroft	chomp($current_comment);
2464a0df2efSAndy Whitcroft	return($current_comment);
2474a0df2efSAndy Whitcroft}
2484a0df2efSAndy Whitcroftsub ctx_has_comment {
2494a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
2504a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
2514a0df2efSAndy Whitcroft
25200df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
2534a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
2544a0df2efSAndy Whitcroft
2554a0df2efSAndy Whitcroft	return ($cmt ne '');
2564a0df2efSAndy Whitcroft}
2574a0df2efSAndy Whitcroft
2580a920b5bSAndy Whitcroftsub cat_vet {
2590a920b5bSAndy Whitcroft	my ($vet) = @_;
2600a920b5bSAndy Whitcroft
2610a920b5bSAndy Whitcroft	$vet =~ s/\t/^I/;
2620a920b5bSAndy Whitcroft	$vet =~ s/$/\$/;
2630a920b5bSAndy Whitcroft
2640a920b5bSAndy Whitcroft	return $vet;
2650a920b5bSAndy Whitcroft}
2660a920b5bSAndy Whitcroft
267f0a594c1SAndy Whitcroftmy @report = ();
268f0a594c1SAndy Whitcroftsub report {
269f0a594c1SAndy Whitcroft	push(@report, $_[0]);
270f0a594c1SAndy Whitcroft}
271f0a594c1SAndy Whitcroftsub report_dump {
272f0a594c1SAndy Whitcroft	@report;
273f0a594c1SAndy Whitcroft}
274de7d4f0eSAndy Whitcroftsub ERROR {
275f0a594c1SAndy Whitcroft	report("ERROR: $_[0]\n");
276de7d4f0eSAndy Whitcroft	our $clean = 0;
277de7d4f0eSAndy Whitcroft}
278de7d4f0eSAndy Whitcroftsub WARN {
279f0a594c1SAndy Whitcroft	report("WARNING: $_[0]\n");
280de7d4f0eSAndy Whitcroft	our $clean = 0;
281de7d4f0eSAndy Whitcroft}
282de7d4f0eSAndy Whitcroftsub CHK {
283f0a594c1SAndy Whitcroft	report("CHECK: $_[0]\n");
284de7d4f0eSAndy Whitcroft	our $clean = 0;
285de7d4f0eSAndy Whitcroft}
286de7d4f0eSAndy Whitcroft
2870a920b5bSAndy Whitcroftsub process {
2880a920b5bSAndy Whitcroft	my $filename = shift;
2890a920b5bSAndy Whitcroft	my @lines = @_;
2900a920b5bSAndy Whitcroft
2910a920b5bSAndy Whitcroft	my $linenr=0;
2920a920b5bSAndy Whitcroft	my $prevline="";
2930a920b5bSAndy Whitcroft	my $stashline="";
2940a920b5bSAndy Whitcroft
2954a0df2efSAndy Whitcroft	my $length;
2960a920b5bSAndy Whitcroft	my $indent;
2970a920b5bSAndy Whitcroft	my $previndent=0;
2980a920b5bSAndy Whitcroft	my $stashindent=0;
2990a920b5bSAndy Whitcroft
300de7d4f0eSAndy Whitcroft	our $clean = 1;
3010a920b5bSAndy Whitcroft	my $signoff = 0;
3020a920b5bSAndy Whitcroft	my $is_patch = 0;
3030a920b5bSAndy Whitcroft
3040a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
3050a920b5bSAndy Whitcroft	my $realfile = '';
3060a920b5bSAndy Whitcroft	my $realline = 0;
3070a920b5bSAndy Whitcroft	my $realcnt = 0;
3080a920b5bSAndy Whitcroft	my $here = '';
3090a920b5bSAndy Whitcroft	my $in_comment = 0;
3100a920b5bSAndy Whitcroft	my $first_line = 0;
3110a920b5bSAndy Whitcroft
312d8aaf121SAndy Whitcroft	my $Ident	= qr{[A-Za-z\d_]+};
313d8aaf121SAndy Whitcroft	my $Storage	= qr{extern|static};
314*22f2a2efSAndy Whitcroft	my $Sparse	= qr{__user|__kernel|__force|__iomem|__must_check|__init_refok};
315d8aaf121SAndy Whitcroft	my $NonptrType	= qr{
316d8aaf121SAndy Whitcroft				\b
317d8aaf121SAndy Whitcroft				(?:const\s+)?
318d8aaf121SAndy Whitcroft				(?:unsigned\s+)?
319d8aaf121SAndy Whitcroft				(?:
320d8aaf121SAndy Whitcroft					void|
321d8aaf121SAndy Whitcroft					char|
322d8aaf121SAndy Whitcroft					short|
323d8aaf121SAndy Whitcroft					int|
324d8aaf121SAndy Whitcroft					long|
325d8aaf121SAndy Whitcroft					unsigned|
326d8aaf121SAndy Whitcroft					float|
327d8aaf121SAndy Whitcroft					double|
328*22f2a2efSAndy Whitcroft					bool|
329d8aaf121SAndy Whitcroft					long\s+int|
330d8aaf121SAndy Whitcroft					long\s+long|
331d8aaf121SAndy Whitcroft					long\s+long\s+int|
332de7d4f0eSAndy Whitcroft					u8|u16|u32|u64|
333de7d4f0eSAndy Whitcroft					s8|s16|s32|s64|
334d8aaf121SAndy Whitcroft					struct\s+$Ident|
335d8aaf121SAndy Whitcroft					union\s+$Ident|
336de7d4f0eSAndy Whitcroft					enum\s+$Ident|
337d8aaf121SAndy Whitcroft					${Ident}_t
338d8aaf121SAndy Whitcroft				)
339d8aaf121SAndy Whitcroft				(?:\s+$Sparse)*
340d8aaf121SAndy Whitcroft				\b
341d8aaf121SAndy Whitcroft			  }x;
342d8aaf121SAndy Whitcroft	my $Type	= qr{
343d8aaf121SAndy Whitcroft				\b$NonptrType\b
344*22f2a2efSAndy Whitcroft				(?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
345*22f2a2efSAndy Whitcroft				(?:\s+$Sparse)*
346d8aaf121SAndy Whitcroft			  }x;
347d8aaf121SAndy Whitcroft	my $Declare	= qr{(?:$Storage\s+)?$Type};
348f0a594c1SAndy Whitcroft	my $Attribute	= qr{const|__read_mostly|__init|__initdata|__meminit};
349f0a594c1SAndy Whitcroft
350f0a594c1SAndy Whitcroft	my $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
351f0a594c1SAndy Whitcroft	my $Lval	= qr{$Ident(?:$Member)*};
352653d4876SAndy Whitcroft
353de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
354de7d4f0eSAndy Whitcroft	my @setup_docs = ();
355de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
356de7d4f0eSAndy Whitcroft	foreach my $line (@lines) {
357de7d4f0eSAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
358de7d4f0eSAndy Whitcroft			$setup_docs = 0;
359de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
360de7d4f0eSAndy Whitcroft				$setup_docs = 1;
361de7d4f0eSAndy Whitcroft			}
362de7d4f0eSAndy Whitcroft			next;
363de7d4f0eSAndy Whitcroft		}
364de7d4f0eSAndy Whitcroft
365de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
366de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
367de7d4f0eSAndy Whitcroft		}
368de7d4f0eSAndy Whitcroft	}
369de7d4f0eSAndy Whitcroft
3700a920b5bSAndy Whitcroft	foreach my $line (@lines) {
3710a920b5bSAndy Whitcroft		$linenr++;
3720a920b5bSAndy Whitcroft
373653d4876SAndy Whitcroft		my $rawline = $line;
374653d4876SAndy Whitcroft
3750a920b5bSAndy Whitcroft#extract the filename as it passes
3760a920b5bSAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
3770a920b5bSAndy Whitcroft			$realfile=$1;
37800df344fSAndy Whitcroft			$realfile =~ s@^[^/]*/@@;
3790a920b5bSAndy Whitcroft			$in_comment = 0;
3800a920b5bSAndy Whitcroft			next;
3810a920b5bSAndy Whitcroft		}
3820a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
3830a920b5bSAndy Whitcroft		if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
3840a920b5bSAndy Whitcroft			$is_patch = 1;
3854a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
3860a920b5bSAndy Whitcroft			$in_comment = 0;
3870a920b5bSAndy Whitcroft			$realline=$1-1;
3880a920b5bSAndy Whitcroft			if (defined $2) {
3890a920b5bSAndy Whitcroft				$realcnt=$3+1;
3900a920b5bSAndy Whitcroft			} else {
3910a920b5bSAndy Whitcroft				$realcnt=1+1;
3920a920b5bSAndy Whitcroft			}
3930a920b5bSAndy Whitcroft			next;
3940a920b5bSAndy Whitcroft		}
3950a920b5bSAndy Whitcroft
3964a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
3974a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
3984a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
3994a0df2efSAndy Whitcroft		if ($line =~ /^( |\+|$)/) {
4000a920b5bSAndy Whitcroft			$realline++;
401d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
4020a920b5bSAndy Whitcroft
4030a920b5bSAndy Whitcroft			# track any sort of multi-line comment.  Obviously if
4040a920b5bSAndy Whitcroft			# the added text or context do not include the whole
4050a920b5bSAndy Whitcroft			# comment we will not see it. Such is life.
4060a920b5bSAndy Whitcroft			#
4070a920b5bSAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
4080a920b5bSAndy Whitcroft			# is the start of a diff block and this line starts
4090a920b5bSAndy Whitcroft			# ' *' then it is very likely a comment.
4104a0df2efSAndy Whitcroft			if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
4110a920b5bSAndy Whitcroft				$in_comment = 1;
4120a920b5bSAndy Whitcroft			}
4130a920b5bSAndy Whitcroft			if ($line =~ m@/\*@) {
4140a920b5bSAndy Whitcroft				$in_comment = 1;
4150a920b5bSAndy Whitcroft			}
4160a920b5bSAndy Whitcroft			if ($line =~ m@\*/@) {
4170a920b5bSAndy Whitcroft				$in_comment = 0;
4180a920b5bSAndy Whitcroft			}
4190a920b5bSAndy Whitcroft
4204a0df2efSAndy Whitcroft			# Measure the line length and indent.
4214a0df2efSAndy Whitcroft			($length, $indent) = line_stats($line);
4220a920b5bSAndy Whitcroft
4230a920b5bSAndy Whitcroft			# Track the previous line.
4240a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
4250a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
426d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
427d8aaf121SAndy Whitcroft			$realcnt--;
4280a920b5bSAndy Whitcroft		}
4290a920b5bSAndy Whitcroft
4300a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
431389834b6SRandy Dunlap		$here = "#$linenr: ";
432389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
4330a920b5bSAndy Whitcroft
43400df344fSAndy Whitcroft		my $hereline = "$here\n$line\n";
435de7d4f0eSAndy Whitcroft		my $herecurr = "$here\n$line\n";
436de7d4f0eSAndy Whitcroft		my $hereprev = "$here\n$prevline\n$line\n";
4370a920b5bSAndy Whitcroft
4380a920b5bSAndy Whitcroft#check the patch for a signoff:
439d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
4404a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
4414a0df2efSAndy Whitcroft			$signoff++;
4420a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
443de7d4f0eSAndy Whitcroft				WARN("Signed-off-by: is the preferred form\n" .
444de7d4f0eSAndy Whitcroft					$herecurr);
4450a920b5bSAndy Whitcroft			}
4460a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
447de7d4f0eSAndy Whitcroft				WARN("need space after Signed-off-by:\n" .
448de7d4f0eSAndy Whitcroft					$herecurr);
4490a920b5bSAndy Whitcroft			}
4500a920b5bSAndy Whitcroft		}
4510a920b5bSAndy Whitcroft
45200df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
45300df344fSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
454de7d4f0eSAndy Whitcroft			ERROR("patch seems to be corrupt (line wrapped?)\n" .
455de7d4f0eSAndy Whitcroft				$herecurr);
456de7d4f0eSAndy Whitcroft		}
457de7d4f0eSAndy Whitcroft
458de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
459de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
460de7d4f0eSAndy Whitcroft		     !($line =~ m/^(
461de7d4f0eSAndy Whitcroft				[\x09\x0A\x0D\x20-\x7E]              # ASCII
462de7d4f0eSAndy Whitcroft				| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
463de7d4f0eSAndy Whitcroft				|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
464de7d4f0eSAndy Whitcroft				| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
465de7d4f0eSAndy Whitcroft				|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
466de7d4f0eSAndy Whitcroft				|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
467de7d4f0eSAndy Whitcroft				| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
468de7d4f0eSAndy Whitcroft				|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
469de7d4f0eSAndy Whitcroft				)*$/x )) {
470de7d4f0eSAndy Whitcroft			ERROR("Invalid UTF-8\n" . $herecurr);
47100df344fSAndy Whitcroft		}
4720a920b5bSAndy Whitcroft
47300df344fSAndy Whitcroft#ignore lines being removed
47400df344fSAndy Whitcroft		if ($line=~/^-/) {next;}
47500df344fSAndy Whitcroft
47600df344fSAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
47700df344fSAndy Whitcroft		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
4780a920b5bSAndy Whitcroft
4790a920b5bSAndy Whitcroft#trailing whitespace
480d8aaf121SAndy Whitcroft		if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
481de7d4f0eSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($line) . "\n";
482de7d4f0eSAndy Whitcroft			ERROR("trailing whitespace\n" . $herevet);
4830a920b5bSAndy Whitcroft		}
4840a920b5bSAndy Whitcroft#80 column limit
48500df344fSAndy Whitcroft		if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
486de7d4f0eSAndy Whitcroft			WARN("line over 80 characters\n" . $herecurr);
4870a920b5bSAndy Whitcroft		}
4880a920b5bSAndy Whitcroft
4890a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk
4900a920b5bSAndy Whitcroft		next if ($realfile !~ /\.[hc]$/);
4910a920b5bSAndy Whitcroft
4920a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
4930a920b5bSAndy Whitcroft# more than 8 must use tabs.
4940a920b5bSAndy Whitcroft		if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s*        \s*/) {
495de7d4f0eSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($line) . "\n";
496de7d4f0eSAndy Whitcroft			ERROR("use tabs not spaces\n" . $herevet);
4970a920b5bSAndy Whitcroft		}
4980a920b5bSAndy Whitcroft
4990a920b5bSAndy Whitcroft# Remove comments from the line before processing.
500*22f2a2efSAndy Whitcroft		my $comment_edge = ($line =~ s@/\*.*\*/@@g) +
501*22f2a2efSAndy Whitcroft				   ($line =~ s@/\*.*@@) +
502*22f2a2efSAndy Whitcroft				   ($line =~ s@^(.).*\*/@$1@);
503*22f2a2efSAndy Whitcroft
504*22f2a2efSAndy Whitcroft# The rest of our checks refer specifically to C style
505*22f2a2efSAndy Whitcroft# only apply those _outside_ comments.  Only skip
506*22f2a2efSAndy Whitcroft# lines in the middle of comments.
507*22f2a2efSAndy Whitcroft		next if (!$comment_edge && $in_comment);
50800df344fSAndy Whitcroft
509653d4876SAndy Whitcroft# Standardise the strings and chars within the input to simplify matching.
510653d4876SAndy Whitcroft		$line = sanitise_line($line);
511653d4876SAndy Whitcroft
51200df344fSAndy Whitcroft#
51300df344fSAndy Whitcroft# Checks which may be anchored in the context.
51400df344fSAndy Whitcroft#
51500df344fSAndy Whitcroft
51600df344fSAndy Whitcroft# Check for switch () and associated case and default
51700df344fSAndy Whitcroft# statements should be at the same indent.
51800df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
51900df344fSAndy Whitcroft			my $err = '';
52000df344fSAndy Whitcroft			my $sep = '';
52100df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
52200df344fSAndy Whitcroft			shift(@ctx);
52300df344fSAndy Whitcroft			for my $ctx (@ctx) {
52400df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
52500df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
52600df344fSAndy Whitcroft							$indent != $cindent) {
52700df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
52800df344fSAndy Whitcroft					$sep = '';
52900df344fSAndy Whitcroft				} else {
53000df344fSAndy Whitcroft					$sep = "[...]\n";
53100df344fSAndy Whitcroft				}
53200df344fSAndy Whitcroft			}
53300df344fSAndy Whitcroft			if ($err ne '') {
534de7d4f0eSAndy Whitcroft				ERROR("switch and case should be at the same indent\n$hereline\n$err\n");
535de7d4f0eSAndy Whitcroft			}
536de7d4f0eSAndy Whitcroft		}
537de7d4f0eSAndy Whitcroft
538de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
539de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
540de7d4f0eSAndy Whitcroft		if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
541f0a594c1SAndy Whitcroft			my @ctx = ctx_statement($linenr, $realcnt, 0);
542de7d4f0eSAndy Whitcroft			my $ctx_ln = $linenr + $#ctx + 1;
543de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
544de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
545de7d4f0eSAndy Whitcroft
546de7d4f0eSAndy Whitcroft			while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
547de7d4f0eSAndy Whitcroft				$ctx_ln++;
548de7d4f0eSAndy Whitcroft				$ctx_cnt--;
549de7d4f0eSAndy Whitcroft			}
550de7d4f0eSAndy Whitcroft			##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
551de7d4f0eSAndy Whitcroft
552de7d4f0eSAndy Whitcroft			if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
553f0a594c1SAndy Whitcroft				ERROR("That open brace { should be on the previous line\n" .
554de7d4f0eSAndy Whitcroft					"$here\n$ctx\n$lines[$ctx_ln - 1]");
55500df344fSAndy Whitcroft			}
55600df344fSAndy Whitcroft		}
55700df344fSAndy Whitcroft
55800df344fSAndy Whitcroft#ignore lines not being added
55900df344fSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
56000df344fSAndy Whitcroft
561653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
562653d4876SAndy Whitcroft		if ($tst_type && $line =~ /^.$Declare$/) {
563de7d4f0eSAndy Whitcroft			ERROR("TEST: is type $Declare\n" . $herecurr);
564653d4876SAndy Whitcroft			next;
565653d4876SAndy Whitcroft		}
566653d4876SAndy Whitcroft
567f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
568f0a594c1SAndy Whitcroft		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
569f0a594c1SAndy Whitcroft		    $line =~ /^.\s*{/) {
570f0a594c1SAndy Whitcroft			ERROR("That open brace { should be on the previous line\n" . $hereprev);
571f0a594c1SAndy Whitcroft		}
572f0a594c1SAndy Whitcroft
57300df344fSAndy Whitcroft#
57400df344fSAndy Whitcroft# Checks which are anchored on the added line.
57500df344fSAndy Whitcroft#
57600df344fSAndy Whitcroft
577653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
578653d4876SAndy Whitcroft		if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
579653d4876SAndy Whitcroft			my $path = $1;
580653d4876SAndy Whitcroft			if ($path =~ m{//}) {
581de7d4f0eSAndy Whitcroft				ERROR("malformed #include filename\n" .
582de7d4f0eSAndy Whitcroft					$herecurr);
583653d4876SAndy Whitcroft			}
584653d4876SAndy Whitcroft			# Sanitise this special form of string.
585653d4876SAndy Whitcroft			$path = 'X' x length($path);
586653d4876SAndy Whitcroft			$line =~ s{\<.*\>}{<$path>};
587653d4876SAndy Whitcroft		}
588653d4876SAndy Whitcroft
58900df344fSAndy Whitcroft# no C99 // comments
59000df344fSAndy Whitcroft		if ($line =~ m{//}) {
591de7d4f0eSAndy Whitcroft			ERROR("do not use C99 // comments\n" . $herecurr);
59200df344fSAndy Whitcroft		}
59300df344fSAndy Whitcroft		# Remove C99 comments.
5940a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
5950a920b5bSAndy Whitcroft
5960a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
597653d4876SAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
598653d4876SAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
599653d4876SAndy Whitcroft			my $name = $1;
6000a920b5bSAndy Whitcroft			if (($prevline !~ /^}/) &&
6010a920b5bSAndy Whitcroft			   ($prevline !~ /^\+}/) &&
602653d4876SAndy Whitcroft			   ($prevline !~ /^ }/) &&
603*22f2a2efSAndy Whitcroft			   ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) {
604de7d4f0eSAndy Whitcroft				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
6050a920b5bSAndy Whitcroft			}
6060a920b5bSAndy Whitcroft		}
6070a920b5bSAndy Whitcroft
608f0a594c1SAndy Whitcroft# check for external initialisers.
609f0a594c1SAndy Whitcroft		if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
610f0a594c1SAndy Whitcroft			ERROR("do not initialise externals to 0 or NULL\n" .
611f0a594c1SAndy Whitcroft				$herecurr);
612f0a594c1SAndy Whitcroft		}
6130a920b5bSAndy Whitcroft# check for static initialisers.
614f0a594c1SAndy Whitcroft		if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
615de7d4f0eSAndy Whitcroft			ERROR("do not initialise statics to 0 or NULL\n" .
616de7d4f0eSAndy Whitcroft				$herecurr);
6170a920b5bSAndy Whitcroft		}
6180a920b5bSAndy Whitcroft
619653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
620653d4876SAndy Whitcroft# make sense.
621653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
622de7d4f0eSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+\(\s*\*$Ident\s*\)\s*\(/ &&
623653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
624de7d4f0eSAndy Whitcroft			WARN("do not add new typedefs\n" . $herecurr);
6250a920b5bSAndy Whitcroft		}
6260a920b5bSAndy Whitcroft
6270a920b5bSAndy Whitcroft# * goes on variable not on type
628d8aaf121SAndy Whitcroft		if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
629de7d4f0eSAndy Whitcroft			ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
630de7d4f0eSAndy Whitcroft				$herecurr);
631d8aaf121SAndy Whitcroft
632d8aaf121SAndy Whitcroft		} elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
633de7d4f0eSAndy Whitcroft			ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
634de7d4f0eSAndy Whitcroft				$herecurr);
635d8aaf121SAndy Whitcroft
636f0a594c1SAndy Whitcroft		} elsif ($line =~ m{$NonptrType(\*+)(?:\s+$Attribute)?\s+[A-Za-z\d_]+}) {
637de7d4f0eSAndy Whitcroft			ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
638de7d4f0eSAndy Whitcroft				$herecurr);
639d8aaf121SAndy Whitcroft
640f0a594c1SAndy Whitcroft		} elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+$Attribute)\s+[A-Za-z\d_]+}) {
641de7d4f0eSAndy Whitcroft			ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
642de7d4f0eSAndy Whitcroft				$herecurr);
6430a920b5bSAndy Whitcroft		}
6440a920b5bSAndy Whitcroft
6450a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
6460a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
6470a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
6480a920b5bSAndy Whitcroft# 			print "$herecurr";
6490a920b5bSAndy Whitcroft# 			$clean = 0;
6500a920b5bSAndy Whitcroft# 		}
6510a920b5bSAndy Whitcroft
65200df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
65300df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
65400df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
65500df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end.
65600df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
657f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
65800df344fSAndy Whitcroft			my $ok = 0;
65900df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
66000df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
66100df344fSAndy Whitcroft				# we have a preceeding printk if it ends
66200df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
66300df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
66400df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
66500df344fSAndy Whitcroft						$ok = 1;
66600df344fSAndy Whitcroft					}
66700df344fSAndy Whitcroft					last;
66800df344fSAndy Whitcroft				}
66900df344fSAndy Whitcroft			}
67000df344fSAndy Whitcroft			if ($ok == 0) {
671de7d4f0eSAndy Whitcroft				WARN("printk() should include KERN_ facility level\n" . $herecurr);
6720a920b5bSAndy Whitcroft			}
67300df344fSAndy Whitcroft		}
6740a920b5bSAndy Whitcroft
675653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
676653d4876SAndy Whitcroft# or if closed on same line
677d8aaf121SAndy Whitcroft		if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
6780a920b5bSAndy Whitcroft		    !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
679de7d4f0eSAndy Whitcroft			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
6800a920b5bSAndy Whitcroft		}
681653d4876SAndy Whitcroft
682f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
683f0a594c1SAndy Whitcroft		if ($line =~ /($Ident)\s+\(/ &&
684*22f2a2efSAndy Whitcroft		    $1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright)$/ &&
685f0a594c1SAndy Whitcroft		    $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) {
686*22f2a2efSAndy Whitcroft			WARN("no space between function name and open parenthesis '('\n" . $herecurr);
687f0a594c1SAndy Whitcroft		}
688653d4876SAndy Whitcroft# Check operator spacing.
6894a0df2efSAndy Whitcroft		# Note we expand the line with the leading + as the real
6904a0df2efSAndy Whitcroft		# line will be displayed with the leading + and the tabs
6914a0df2efSAndy Whitcroft		# will therefore also expand that way.
6920a920b5bSAndy Whitcroft		my $opline = $line;
6934a0df2efSAndy Whitcroft		$opline = expand_tabs($opline);
6940a920b5bSAndy Whitcroft		$opline =~ s/^./ /;
6950a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
696f0a594c1SAndy Whitcroft			my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|=>|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
69700df344fSAndy Whitcroft			my $off = 0;
6980a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
6994a0df2efSAndy Whitcroft				$off += length($elements[$n]);
7004a0df2efSAndy Whitcroft
7014a0df2efSAndy Whitcroft				my $a = '';
7024a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
7034a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
7044a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
7054a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
7064a0df2efSAndy Whitcroft				$a = 'E' if ($elements[$n] eq '' && $n == 0);
7074a0df2efSAndy Whitcroft
7080a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
7094a0df2efSAndy Whitcroft
7104a0df2efSAndy Whitcroft				my $c = '';
7110a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
7124a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
7134a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
7144a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
7154a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
716*22f2a2efSAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
7174a0df2efSAndy Whitcroft				} else {
7184a0df2efSAndy Whitcroft					$c = 'E';
7190a920b5bSAndy Whitcroft				}
7200a920b5bSAndy Whitcroft
72100df344fSAndy Whitcroft				# Pick up the preceeding and succeeding characters.
722de7d4f0eSAndy Whitcroft				my $ca = substr($opline, 0, $off);
72300df344fSAndy Whitcroft				my $cc = '';
724653d4876SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
725d8aaf121SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
72600df344fSAndy Whitcroft				}
727de7d4f0eSAndy Whitcroft				my $cb = "$ca$;$cc";
72800df344fSAndy Whitcroft
7294a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
7304a0df2efSAndy Whitcroft
7314a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
7324a0df2efSAndy Whitcroft
7334a0df2efSAndy Whitcroft				my $ptr = (" " x $off) . "^";
734de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
7350a920b5bSAndy Whitcroft
7360a920b5bSAndy Whitcroft				##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
7370a920b5bSAndy Whitcroft
738d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
739d8aaf121SAndy Whitcroft				if ($op eq ';') {
740de7d4f0eSAndy Whitcroft					if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
741de7d4f0eSAndy Whitcroft					    $cc !~ /^;/) {
742de7d4f0eSAndy Whitcroft						ERROR("need space after that '$op' $at\n" . $hereptr);
743d8aaf121SAndy Whitcroft					}
744d8aaf121SAndy Whitcroft
745d8aaf121SAndy Whitcroft				# // is a comment
746d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
7470a920b5bSAndy Whitcroft
7480a920b5bSAndy Whitcroft				# -> should have no spaces
7490a920b5bSAndy Whitcroft				} elsif ($op eq '->') {
7504a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
751de7d4f0eSAndy Whitcroft						ERROR("no spaces around that '$op' $at\n" . $hereptr);
7520a920b5bSAndy Whitcroft					}
7530a920b5bSAndy Whitcroft
7540a920b5bSAndy Whitcroft				# , must have a space on the right.
7550a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
756d8aaf121SAndy Whitcroft					if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
757de7d4f0eSAndy Whitcroft						ERROR("need space after that '$op' $at\n" . $hereptr);
7580a920b5bSAndy Whitcroft					}
7590a920b5bSAndy Whitcroft
7600a920b5bSAndy Whitcroft				# unary ! and unary ~ are allowed no space on the right
7610a920b5bSAndy Whitcroft				} elsif ($op eq '!' or $op eq '~') {
7624a0df2efSAndy Whitcroft					if ($ctx !~ /[WOEB]x./) {
763de7d4f0eSAndy Whitcroft						ERROR("need space before that '$op' $at\n" . $hereptr);
7640a920b5bSAndy Whitcroft					}
7654a0df2efSAndy Whitcroft					if ($ctx =~ /.xW/) {
766de7d4f0eSAndy Whitcroft						ERROR("no space after that '$op' $at\n" . $hereptr);
7670a920b5bSAndy Whitcroft					}
7680a920b5bSAndy Whitcroft
7690a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
7700a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
771d8aaf121SAndy Whitcroft					if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
772de7d4f0eSAndy Whitcroft						ERROR("need space one side of that '$op' $at\n" . $hereptr);
7730a920b5bSAndy Whitcroft					}
774d8aaf121SAndy Whitcroft					if ($ctx =~ /Wx./ && $cc =~ /^;/) {
775de7d4f0eSAndy Whitcroft						ERROR("no space before that '$op' $at\n" . $hereptr);
776653d4876SAndy Whitcroft					}
7770a920b5bSAndy Whitcroft
7780a920b5bSAndy Whitcroft				# & is both unary and binary
7790a920b5bSAndy Whitcroft				# unary:
7800a920b5bSAndy Whitcroft				# 	a &b
7810a920b5bSAndy Whitcroft				# binary (consistent spacing):
7820a920b5bSAndy Whitcroft				#	a&b		OK
7830a920b5bSAndy Whitcroft				#	a & b		OK
7840a920b5bSAndy Whitcroft				#
7850a920b5bSAndy Whitcroft				# boiling down to: if there is a space on the right then there
7860a920b5bSAndy Whitcroft				# should be one on the left.
7870a920b5bSAndy Whitcroft				#
7880a920b5bSAndy Whitcroft				# - is the same
7890a920b5bSAndy Whitcroft				#
7904a0df2efSAndy Whitcroft				} elsif ($op eq '&' or $op eq '-') {
79100df344fSAndy Whitcroft					if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
792de7d4f0eSAndy Whitcroft						ERROR("need space before that '$op' $at\n" . $hereptr);
7934a0df2efSAndy Whitcroft					}
7944a0df2efSAndy Whitcroft
79500df344fSAndy Whitcroft				# * is the same as & only adding:
79600df344fSAndy Whitcroft				# type:
79700df344fSAndy Whitcroft				# 	(foo *)
79800df344fSAndy Whitcroft				#	(foo **)
79900df344fSAndy Whitcroft				#
8004a0df2efSAndy Whitcroft				} elsif ($op eq '*') {
801de7d4f0eSAndy Whitcroft					if ($ca !~ /$Type$/ && $cb !~ /(\*$;|$;\*)/ &&
802de7d4f0eSAndy Whitcroft					    $ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
803de7d4f0eSAndy Whitcroft						ERROR("need space before that '$op' $at\n" . $hereptr);
8040a920b5bSAndy Whitcroft					}
8050a920b5bSAndy Whitcroft
8060a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
8070a920b5bSAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
8080a920b5bSAndy Whitcroft					 $op eq '^' or $op eq '|')
8090a920b5bSAndy Whitcroft				{
8104a0df2efSAndy Whitcroft					if ($ctx !~ /VxV|WxW|VxE|WxE/) {
811de7d4f0eSAndy Whitcroft						ERROR("need consistent spacing around '$op' $at\n" .
812de7d4f0eSAndy Whitcroft							$hereptr);
8130a920b5bSAndy Whitcroft					}
8140a920b5bSAndy Whitcroft
8150a920b5bSAndy Whitcroft				# All the others need spaces both sides.
8164a0df2efSAndy Whitcroft				} elsif ($ctx !~ /[EW]x[WE]/) {
817*22f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
818*22f2a2efSAndy Whitcroft					if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
819*22f2a2efSAndy Whitcroft					    !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
820de7d4f0eSAndy Whitcroft						ERROR("need spaces around that '$op' $at\n" . $hereptr);
8210a920b5bSAndy Whitcroft					}
822*22f2a2efSAndy Whitcroft				}
8234a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
8240a920b5bSAndy Whitcroft			}
8250a920b5bSAndy Whitcroft		}
8260a920b5bSAndy Whitcroft
827f0a594c1SAndy Whitcroft# check for multiple assignments
828f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
829f0a594c1SAndy Whitcroft			WARN("multiple assignments should be avoided\n" . $herecurr);
830f0a594c1SAndy Whitcroft		}
831f0a594c1SAndy Whitcroft
832*22f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
833*22f2a2efSAndy Whitcroft## # continuation.
834*22f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
835*22f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
836*22f2a2efSAndy Whitcroft##
837*22f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
838*22f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
839*22f2a2efSAndy Whitcroft## 			my $ln = $line;
840*22f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
841*22f2a2efSAndy Whitcroft## 			}
842*22f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
843*22f2a2efSAndy Whitcroft## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
844*22f2a2efSAndy Whitcroft## 			}
845*22f2a2efSAndy Whitcroft## 		}
846f0a594c1SAndy Whitcroft
8470a920b5bSAndy Whitcroft#need space before brace following if, while, etc
848*22f2a2efSAndy Whitcroft		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
849*22f2a2efSAndy Whitcroft		    $line =~ /do{/) {
850de7d4f0eSAndy Whitcroft			ERROR("need a space before the open brace '{'\n" . $herecurr);
851de7d4f0eSAndy Whitcroft		}
852de7d4f0eSAndy Whitcroft
853de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
854de7d4f0eSAndy Whitcroft# on the line
855de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
856de7d4f0eSAndy Whitcroft			ERROR("need a space after that close brace '}'\n" . $herecurr);
8570a920b5bSAndy Whitcroft		}
8580a920b5bSAndy Whitcroft
859*22f2a2efSAndy Whitcroft# check spacing on square brackets
860*22f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
861*22f2a2efSAndy Whitcroft			ERROR("no space after that open square bracket '['\n" . $herecurr);
862*22f2a2efSAndy Whitcroft		}
863*22f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
864*22f2a2efSAndy Whitcroft			ERROR("no space before that close square bracket ']'\n" . $herecurr);
865*22f2a2efSAndy Whitcroft		}
866*22f2a2efSAndy Whitcroft
867*22f2a2efSAndy Whitcroft# check spacing on paretheses
868*22f2a2efSAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*$/) {
869*22f2a2efSAndy Whitcroft			ERROR("no space after that open parenthesis '('\n" . $herecurr);
870*22f2a2efSAndy Whitcroft		}
871*22f2a2efSAndy Whitcroft		if ($line =~ /\s\)/) {
872*22f2a2efSAndy Whitcroft			ERROR("no space before that close parenthesis ')'\n" . $herecurr);
873*22f2a2efSAndy Whitcroft		}
874*22f2a2efSAndy Whitcroft
8750a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
8764a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
8770a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
878de7d4f0eSAndy Whitcroft			WARN("labels should not be indented\n" . $herecurr);
8790a920b5bSAndy Whitcroft		}
8800a920b5bSAndy Whitcroft
8810a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
8824a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
883de7d4f0eSAndy Whitcroft			ERROR("need a space before the open parenthesis '('\n" . $herecurr);
8840a920b5bSAndy Whitcroft		}
8850a920b5bSAndy Whitcroft
8860a920b5bSAndy Whitcroft# Check for illegal assignment in if conditional.
887653d4876SAndy Whitcroft		if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
88800df344fSAndy Whitcroft			#next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
889de7d4f0eSAndy Whitcroft			ERROR("do not use assignment in if condition\n" . $herecurr);
8900a920b5bSAndy Whitcroft		}
8910a920b5bSAndy Whitcroft
8920a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
8930a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
8940a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
8950a920b5bSAndy Whitcroft						$previndent == $indent) {
896de7d4f0eSAndy Whitcroft			ERROR("else should follow close brace '}'\n" . $hereprev);
8970a920b5bSAndy Whitcroft		}
8980a920b5bSAndy Whitcroft
8990a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
9000a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
9010a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
9020a920b5bSAndy Whitcroft#		    print "$herecurr";
9030a920b5bSAndy Whitcroft#		    $clean = 0;
9040a920b5bSAndy Whitcroft#		}
9050a920b5bSAndy Whitcroft
9060a920b5bSAndy Whitcroft#no spaces allowed after \ in define
9070a920b5bSAndy Whitcroft		if ($line=~/\#define.*\\\s$/) {
908de7d4f0eSAndy Whitcroft			WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
9090a920b5bSAndy Whitcroft		}
9100a920b5bSAndy Whitcroft
911653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
912653d4876SAndy Whitcroft		if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
9130a920b5bSAndy Whitcroft			my $checkfile = "include/linux/$1.h";
9140a920b5bSAndy Whitcroft			if (-f $checkfile) {
915de7d4f0eSAndy Whitcroft				CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
916de7d4f0eSAndy Whitcroft					$herecurr);
9170a920b5bSAndy Whitcroft			}
9180a920b5bSAndy Whitcroft		}
9190a920b5bSAndy Whitcroft
920d8aaf121SAndy Whitcroft# if and else should not have general statements after it
921d8aaf121SAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
922de7d4f0eSAndy Whitcroft		    $1 !~ /^\s*(?:\sif|{|\\|$)/) {
923de7d4f0eSAndy Whitcroft			ERROR("trailing statements should be on next line\n" . $herecurr);
924d8aaf121SAndy Whitcroft		}
925d8aaf121SAndy Whitcroft
926653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
927653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
928653d4876SAndy Whitcroft# in a known goot container
929653d4876SAndy Whitcroft		if (($prevline=~/\#define.*\\/) and
930653d4876SAndy Whitcroft		   !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and
931653d4876SAndy Whitcroft		   !($line=~/do.*{/) and !($line=~/\(\{/) and
932653d4876SAndy Whitcroft		   !($line=~/^.\s*$Declare\s/)) {
933653d4876SAndy Whitcroft			# Grab the first statement, if that is the entire macro
934653d4876SAndy Whitcroft			# its ok.  This may start either on the #define line
935653d4876SAndy Whitcroft			# or the one below.
936d8aaf121SAndy Whitcroft			my $ln = $linenr;
937d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
938f0a594c1SAndy Whitcroft			my $off = 0;
939653d4876SAndy Whitcroft
940f0a594c1SAndy Whitcroft			# If the macro starts on the define line start
941f0a594c1SAndy Whitcroft			# grabbing the statement after the identifier
942f0a594c1SAndy Whitcroft			$prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
943f0a594c1SAndy Whitcroft			##print "1<$1> 2<$2>\n";
944*22f2a2efSAndy Whitcroft			if (defined $2 && $2 ne '') {
945f0a594c1SAndy Whitcroft				$off = length($1);
946d8aaf121SAndy Whitcroft				$ln--;
947d8aaf121SAndy Whitcroft				$cnt++;
948d8aaf121SAndy Whitcroft			}
949f0a594c1SAndy Whitcroft			my @ctx = ctx_statement($ln, $cnt, $off);
950de7d4f0eSAndy Whitcroft			my $ctx_ln = $ln + $#ctx + 1;
951de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
952de7d4f0eSAndy Whitcroft
953de7d4f0eSAndy Whitcroft			# Pull in any empty extension lines.
954de7d4f0eSAndy Whitcroft			while ($ctx =~ /\\$/ &&
955de7d4f0eSAndy Whitcroft			       $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
956de7d4f0eSAndy Whitcroft				$ctx .= $lines[$ctx_ln - 1];
957de7d4f0eSAndy Whitcroft				$ctx_ln++;
958de7d4f0eSAndy Whitcroft			}
959d8aaf121SAndy Whitcroft
960d8aaf121SAndy Whitcroft			if ($ctx =~ /\\$/) {
961d8aaf121SAndy Whitcroft				if ($ctx =~ /;/) {
962de7d4f0eSAndy Whitcroft					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
963d8aaf121SAndy Whitcroft				} else {
964de7d4f0eSAndy Whitcroft					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
965d8aaf121SAndy Whitcroft				}
9660a920b5bSAndy Whitcroft			}
967653d4876SAndy Whitcroft		}
9680a920b5bSAndy Whitcroft
969f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
970f0a594c1SAndy Whitcroft		if ($line =~ /\b(if|while|for|else)\b/) {
971f0a594c1SAndy Whitcroft			# Locate the end of the opening statement.
972f0a594c1SAndy Whitcroft			my @control = ctx_statement($linenr, $realcnt, 0);
973f0a594c1SAndy Whitcroft			my $nr = $linenr + (scalar(@control) - 1);
974f0a594c1SAndy Whitcroft			my $cnt = $realcnt - (scalar(@control) - 1);
975f0a594c1SAndy Whitcroft
976f0a594c1SAndy Whitcroft			my $off = $realcnt - $cnt;
977f0a594c1SAndy Whitcroft			#print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n";
978f0a594c1SAndy Whitcroft
979f0a594c1SAndy Whitcroft			# If this is is a braced statement group check it
980f0a594c1SAndy Whitcroft			if ($lines[$nr - 1] =~ /{\s*$/) {
981f0a594c1SAndy Whitcroft				my ($lvl, @block) = ctx_block_level($nr, $cnt);
982f0a594c1SAndy Whitcroft
983f0a594c1SAndy Whitcroft				my $stmt = join(' ', @block);
984*22f2a2efSAndy Whitcroft				$stmt =~ s/(^[^{]*){//;
985*22f2a2efSAndy Whitcroft				my $before = $1;
986*22f2a2efSAndy Whitcroft				$stmt =~ s/}([^}]*$)//;
987*22f2a2efSAndy Whitcroft				my $after = $1;
988f0a594c1SAndy Whitcroft
989f0a594c1SAndy Whitcroft				#print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n";
990f0a594c1SAndy Whitcroft				#print "stmt<$stmt>\n\n";
991f0a594c1SAndy Whitcroft
992f0a594c1SAndy Whitcroft				# Count the ;'s if there is fewer than two
993f0a594c1SAndy Whitcroft				# then there can only be one statement,
994f0a594c1SAndy Whitcroft				# if there is a brace inside we cannot
995f0a594c1SAndy Whitcroft				# trivially detect if its one statement.
996f0a594c1SAndy Whitcroft				# Also nested if's often require braces to
997f0a594c1SAndy Whitcroft				# disambiguate the else binding so shhh there.
998f0a594c1SAndy Whitcroft				my @semi = ($stmt =~ /;/g);
999*22f2a2efSAndy Whitcroft				push(@semi, "/**/") if ($stmt =~ m@/\*@);
1000f0a594c1SAndy Whitcroft				##print "semi<" . scalar(@semi) . ">\n";
1001f0a594c1SAndy Whitcroft				if ($lvl == 0 && scalar(@semi) < 2 &&
1002*22f2a2efSAndy Whitcroft				    $stmt !~ /{/ && $stmt !~ /\bif\b/ &&
1003*22f2a2efSAndy Whitcroft				    $before !~ /}/ && $after !~ /{/) {
1004f0a594c1SAndy Whitcroft				    	my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n";
1005f0a594c1SAndy Whitcroft				    	shift(@block);
1006*22f2a2efSAndy Whitcroft					WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
1007f0a594c1SAndy Whitcroft				}
1008f0a594c1SAndy Whitcroft			}
1009f0a594c1SAndy Whitcroft		}
1010f0a594c1SAndy Whitcroft
1011653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line)
10124a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
1013653d4876SAndy Whitcroft			if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
1014de7d4f0eSAndy Whitcroft				ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
10150a920b5bSAndy Whitcroft			}
10160a920b5bSAndy Whitcroft		}
10170a920b5bSAndy Whitcroft
10184a0df2efSAndy Whitcroft# don't use deprecated functions
10194a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
102000df344fSAndy Whitcroft			if ($line =~ /\b$func\b/) {
1021de7d4f0eSAndy Whitcroft				ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
10224a0df2efSAndy Whitcroft			}
10234a0df2efSAndy Whitcroft		}
10244a0df2efSAndy Whitcroft
10254a0df2efSAndy Whitcroft# no volatiles please
102600df344fSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
1027de7d4f0eSAndy Whitcroft			WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
10284a0df2efSAndy Whitcroft		}
10294a0df2efSAndy Whitcroft
103000df344fSAndy Whitcroft# warn about #if 0
103100df344fSAndy Whitcroft		if ($line =~ /^.#\s*if\s+0\b/) {
1032de7d4f0eSAndy Whitcroft			CHK("if this code is redundant consider removing it\n" .
1033de7d4f0eSAndy Whitcroft				$herecurr);
10344a0df2efSAndy Whitcroft		}
10354a0df2efSAndy Whitcroft
1036f0a594c1SAndy Whitcroft# check for needless kfree() checks
1037f0a594c1SAndy Whitcroft		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1038f0a594c1SAndy Whitcroft			my $expr = $1;
1039f0a594c1SAndy Whitcroft			if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1040f0a594c1SAndy Whitcroft				WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1041f0a594c1SAndy Whitcroft			}
1042f0a594c1SAndy Whitcroft		}
1043f0a594c1SAndy Whitcroft
104400df344fSAndy Whitcroft# warn about #ifdefs in C files
104500df344fSAndy Whitcroft#		if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
104600df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
104700df344fSAndy Whitcroft#			print "$herecurr";
104800df344fSAndy Whitcroft#			$clean = 0;
104900df344fSAndy Whitcroft#		}
105000df344fSAndy Whitcroft
1051*22f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
1052*22f2a2efSAndy Whitcroft		if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1053*22f2a2efSAndy Whitcroft			ERROR("exactly one space required after that #$1\n" . $herecurr);
1054*22f2a2efSAndy Whitcroft		}
1055*22f2a2efSAndy Whitcroft
10564a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
10574a0df2efSAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
10584a0df2efSAndy Whitcroft			my $which = $1;
10594a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
1060de7d4f0eSAndy Whitcroft				CHK("$1 definition without comment\n" . $herecurr);
10614a0df2efSAndy Whitcroft			}
10624a0df2efSAndy Whitcroft		}
10634a0df2efSAndy Whitcroft# check for memory barriers without a comment.
10644a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
10654a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
1066de7d4f0eSAndy Whitcroft				CHK("memory barrier without comment\n" . $herecurr);
10674a0df2efSAndy Whitcroft			}
10684a0df2efSAndy Whitcroft		}
10694a0df2efSAndy Whitcroft# check of hardware specific defines
1070*22f2a2efSAndy Whitcroft		if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1071de7d4f0eSAndy Whitcroft			CHK("architecture specific defines should be avoided\n" .  $herecurr);
10720a920b5bSAndy Whitcroft		}
1073653d4876SAndy Whitcroft
1074de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
1075de7d4f0eSAndy Whitcroft# storage class and type.
1076*22f2a2efSAndy Whitcroft		if ($line =~ /$Type\s+(?:inline|__always_inline|noinline)\b/ ||
1077*22f2a2efSAndy Whitcroft		    $line =~ /\b(?:inline|__always_inline|noinline)\s+$Storage/) {
1078de7d4f0eSAndy Whitcroft			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1079de7d4f0eSAndy Whitcroft		}
1080de7d4f0eSAndy Whitcroft
1081de7d4f0eSAndy Whitcroft# check for new externs in .c files.
1082de7d4f0eSAndy Whitcroft		if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1083de7d4f0eSAndy Whitcroft			WARN("externs should be avoided in .c files\n" .  $herecurr);
1084de7d4f0eSAndy Whitcroft		}
1085de7d4f0eSAndy Whitcroft
1086de7d4f0eSAndy Whitcroft# checks for new __setup's
1087de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
1088de7d4f0eSAndy Whitcroft			my $name = $1;
1089de7d4f0eSAndy Whitcroft
1090de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
1091de7d4f0eSAndy Whitcroft				CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1092de7d4f0eSAndy Whitcroft			}
1093653d4876SAndy Whitcroft		}
10940a920b5bSAndy Whitcroft	}
10950a920b5bSAndy Whitcroft
10960a920b5bSAndy Whitcroft	if ($chk_patch && !$is_patch) {
1097de7d4f0eSAndy Whitcroft		ERROR("Does not appear to be a unified-diff format patch\n");
10980a920b5bSAndy Whitcroft	}
10990a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
1100de7d4f0eSAndy Whitcroft		ERROR("Missing Signed-off-by: line(s)\n");
11010a920b5bSAndy Whitcroft	}
11020a920b5bSAndy Whitcroft
1103f0a594c1SAndy Whitcroft	if ($clean == 0 && ($chk_patch || $is_patch)) {
1104f0a594c1SAndy Whitcroft		print report_dump();
1105f0a594c1SAndy Whitcroft	}
11060a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
11070a920b5bSAndy Whitcroft		print "Your patch has no obvious style problems and is ready for submission.\n"
11080a920b5bSAndy Whitcroft	}
11090a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
11100a920b5bSAndy Whitcroft		print "Your patch has style problems, please review.  If any of these errors\n";
11110a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
11120a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
11130a920b5bSAndy Whitcroft	}
11140a920b5bSAndy Whitcroft	return $clean;
11150a920b5bSAndy Whitcroft}
1116