xref: /openbmc/qemu/scripts/checkpatch.pl (revision bcad45de)
1#!/usr/bin/perl -w
2# (c) 2001, Dave Jones. (the file handling bit)
3# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
9
10my $P = $0;
11$P =~ s@.*/@@g;
12
13my $V = '0.31';
14
15use Getopt::Long qw(:config no_auto_abbrev);
16
17my $quiet = 0;
18my $tree = 1;
19my $chk_signoff = 1;
20my $chk_patch = 1;
21my $tst_only;
22my $emacs = 0;
23my $terse = 0;
24my $file = 0;
25my $no_warnings = 0;
26my $summary = 1;
27my $mailback = 0;
28my $summary_file = 0;
29my $root;
30my %debug;
31my $help = 0;
32
33sub help {
34	my ($exitcode) = @_;
35
36	print << "EOM";
37Usage: $P [OPTION]... [FILE]...
38Version: $V
39
40Options:
41  -q, --quiet                quiet
42  --no-tree                  run without a kernel tree
43  --no-signoff               do not check for 'Signed-off-by' line
44  --patch                    treat FILE as patchfile (default)
45  --emacs                    emacs compile window format
46  --terse                    one line per report
47  -f, --file                 treat FILE as regular source file
48  --strict                   fail if only warnings are found
49  --root=PATH                PATH to the kernel tree root
50  --no-summary               suppress the per-file summary
51  --mailback                 only produce a report in case of warnings/errors
52  --summary-file             include the filename in summary
53  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
54                             'values', 'possible', 'type', and 'attr' (default
55                             is all off)
56  --test-only=WORD           report only warnings/errors containing WORD
57                             literally
58  -h, --help, --version      display this help and exit
59
60When FILE is - read standard input.
61EOM
62
63	exit($exitcode);
64}
65
66GetOptions(
67	'q|quiet+'	=> \$quiet,
68	'tree!'		=> \$tree,
69	'signoff!'	=> \$chk_signoff,
70	'patch!'	=> \$chk_patch,
71	'emacs!'	=> \$emacs,
72	'terse!'	=> \$terse,
73	'f|file!'	=> \$file,
74	'strict!'	=> \$no_warnings,
75	'root=s'	=> \$root,
76	'summary!'	=> \$summary,
77	'mailback!'	=> \$mailback,
78	'summary-file!'	=> \$summary_file,
79
80	'debug=s'	=> \%debug,
81	'test-only=s'	=> \$tst_only,
82	'h|help'	=> \$help,
83	'version'	=> \$help
84) or help(1);
85
86help(0) if ($help);
87
88my $exit = 0;
89
90if ($#ARGV < 0) {
91	print "$P: no input files\n";
92	exit(1);
93}
94
95my $dbg_values = 0;
96my $dbg_possible = 0;
97my $dbg_type = 0;
98my $dbg_attr = 0;
99my $dbg_adv_dcs = 0;
100my $dbg_adv_checking = 0;
101my $dbg_adv_apw = 0;
102for my $key (keys %debug) {
103	## no critic
104	eval "\${dbg_$key} = '$debug{$key}';";
105	die "$@" if ($@);
106}
107
108my $rpt_cleaners = 0;
109
110if ($terse) {
111	$emacs = 1;
112	$quiet++;
113}
114
115if ($tree) {
116	if (defined $root) {
117		if (!top_of_kernel_tree($root)) {
118			die "$P: $root: --root does not point at a valid tree\n";
119		}
120	} else {
121		if (top_of_kernel_tree('.')) {
122			$root = '.';
123		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
124						top_of_kernel_tree($1)) {
125			$root = $1;
126		}
127	}
128
129	if (!defined $root) {
130		print "Must be run from the top-level dir. of a kernel tree\n";
131		exit(2);
132	}
133}
134
135my $emitted_corrupt = 0;
136
137our $Ident	= qr{
138			[A-Za-z_][A-Za-z\d_]*
139			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
140		}x;
141our $Storage	= qr{extern|static|asmlinkage};
142our $Sparse	= qr{
143			__force
144		}x;
145
146# Notes to $Attribute:
147our $Attribute	= qr{
148			const|
149			volatile|
150			QEMU_NORETURN|
151			QEMU_WARN_UNUSED_RESULT|
152			QEMU_SENTINEL|
153			QEMU_ARTIFICIAL|
154			QEMU_PACKED|
155			GCC_FMT_ATTR
156		  }x;
157our $Modifier;
158our $Inline	= qr{inline};
159our $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
160our $Lval	= qr{$Ident(?:$Member)*};
161
162our $Constant	= qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
163our $Assignment	= qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
164our $Compare    = qr{<=|>=|==|!=|<|>};
165our $Operators	= qr{
166			<=|>=|==|!=|
167			=>|->|<<|>>|<|>|!|~|
168			&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
169		  }x;
170
171our $NonptrType;
172our $Type;
173our $Declare;
174
175our $UTF8	= qr {
176	[\x09\x0A\x0D\x20-\x7E]              # ASCII
177	| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
178	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
179	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
180	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
181	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
182	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
183	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
184}x;
185
186# There are still some false positives, but this catches most
187# common cases.
188our $typeTypedefs = qr{(?x:
189        [A-Z][A-Z\d_]*[a-z][A-Za-z\d_]*     # camelcase
190        | [A-Z][A-Z\d_]*AIOCB               # all uppercase
191        | [A-Z][A-Z\d_]*CPU                 # all uppercase
192        | QEMUBH                            # all uppercase
193)};
194
195our @typeList = (
196	qr{void},
197	qr{(?:unsigned\s+)?char},
198	qr{(?:unsigned\s+)?short},
199	qr{(?:unsigned\s+)?int},
200	qr{(?:unsigned\s+)?long},
201	qr{(?:unsigned\s+)?long\s+int},
202	qr{(?:unsigned\s+)?long\s+long},
203	qr{(?:unsigned\s+)?long\s+long\s+int},
204	qr{unsigned},
205	qr{float},
206	qr{double},
207	qr{bool},
208	qr{struct\s+$Ident},
209	qr{union\s+$Ident},
210	qr{enum\s+$Ident},
211	qr{${Ident}_t},
212	qr{${Ident}_handler},
213	qr{${Ident}_handler_fn},
214	qr{target_(?:u)?long},
215);
216
217# This can be modified by sub possible.  Since it can be empty, be careful
218# about regexes that always match, because they can cause infinite loops.
219our @modifierList = (
220);
221
222sub build_types {
223	my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
224	if (@modifierList > 0) {
225		my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
226		$Modifier = qr{(?:$Attribute|$Sparse|$mods)};
227	} else {
228		$Modifier = qr{(?:$Attribute|$Sparse)};
229	}
230	$NonptrType	= qr{
231			(?:$Modifier\s+|const\s+)*
232			(?:
233				(?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
234				(?:$typeTypedefs\b)|
235				(?:${all}\b)
236			)
237			(?:\s+$Modifier|\s+const)*
238		  }x;
239	$Type	= qr{
240			$NonptrType
241			(?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
242			(?:\s+$Inline|\s+$Modifier)*
243		  }x;
244	$Declare	= qr{(?:$Storage\s+)?$Type};
245}
246build_types();
247
248$chk_signoff = 0 if ($file);
249
250my @rawlines = ();
251my @lines = ();
252my $vname;
253for my $filename (@ARGV) {
254	my $FILE;
255	if ($file) {
256		open($FILE, '-|', "diff -u /dev/null $filename") ||
257			die "$P: $filename: diff failed - $!\n";
258	} elsif ($filename eq '-') {
259		open($FILE, '<&STDIN');
260	} else {
261		open($FILE, '<', "$filename") ||
262			die "$P: $filename: open failed - $!\n";
263	}
264	if ($filename eq '-') {
265		$vname = 'Your patch';
266	} else {
267		$vname = $filename;
268	}
269	while (<$FILE>) {
270		chomp;
271		push(@rawlines, $_);
272	}
273	close($FILE);
274	if (!process($filename)) {
275		$exit = 1;
276	}
277	@rawlines = ();
278	@lines = ();
279}
280
281exit($exit);
282
283sub top_of_kernel_tree {
284	my ($root) = @_;
285
286	my @tree_check = (
287		"COPYING", "MAINTAINERS", "Makefile",
288		"README", "docs", "VERSION",
289		"vl.c"
290	);
291
292	foreach my $check (@tree_check) {
293		if (! -e $root . '/' . $check) {
294			return 0;
295		}
296	}
297	return 1;
298}
299
300sub expand_tabs {
301	my ($str) = @_;
302
303	my $res = '';
304	my $n = 0;
305	for my $c (split(//, $str)) {
306		if ($c eq "\t") {
307			$res .= ' ';
308			$n++;
309			for (; ($n % 8) != 0; $n++) {
310				$res .= ' ';
311			}
312			next;
313		}
314		$res .= $c;
315		$n++;
316	}
317
318	return $res;
319}
320sub copy_spacing {
321	(my $res = shift) =~ tr/\t/ /c;
322	return $res;
323}
324
325sub line_stats {
326	my ($line) = @_;
327
328	# Drop the diff line leader and expand tabs
329	$line =~ s/^.//;
330	$line = expand_tabs($line);
331
332	# Pick the indent from the front of the line.
333	my ($white) = ($line =~ /^(\s*)/);
334
335	return (length($line), length($white));
336}
337
338my $sanitise_quote = '';
339
340sub sanitise_line_reset {
341	my ($in_comment) = @_;
342
343	if ($in_comment) {
344		$sanitise_quote = '*/';
345	} else {
346		$sanitise_quote = '';
347	}
348}
349sub sanitise_line {
350	my ($line) = @_;
351
352	my $res = '';
353	my $l = '';
354
355	my $qlen = 0;
356	my $off = 0;
357	my $c;
358
359	# Always copy over the diff marker.
360	$res = substr($line, 0, 1);
361
362	for ($off = 1; $off < length($line); $off++) {
363		$c = substr($line, $off, 1);
364
365		# Comments we are wacking completely including the begin
366		# and end, all to $;.
367		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
368			$sanitise_quote = '*/';
369
370			substr($res, $off, 2, "$;$;");
371			$off++;
372			next;
373		}
374		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
375			$sanitise_quote = '';
376			substr($res, $off, 2, "$;$;");
377			$off++;
378			next;
379		}
380		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
381			$sanitise_quote = '//';
382
383			substr($res, $off, 2, $sanitise_quote);
384			$off++;
385			next;
386		}
387
388		# A \ in a string means ignore the next character.
389		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
390		    $c eq "\\") {
391			substr($res, $off, 2, 'XX');
392			$off++;
393			next;
394		}
395		# Regular quotes.
396		if ($c eq "'" || $c eq '"') {
397			if ($sanitise_quote eq '') {
398				$sanitise_quote = $c;
399
400				substr($res, $off, 1, $c);
401				next;
402			} elsif ($sanitise_quote eq $c) {
403				$sanitise_quote = '';
404			}
405		}
406
407		#print "c<$c> SQ<$sanitise_quote>\n";
408		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
409			substr($res, $off, 1, $;);
410		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
411			substr($res, $off, 1, $;);
412		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
413			substr($res, $off, 1, 'X');
414		} else {
415			substr($res, $off, 1, $c);
416		}
417	}
418
419	if ($sanitise_quote eq '//') {
420		$sanitise_quote = '';
421	}
422
423	# The pathname on a #include may be surrounded by '<' and '>'.
424	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
425		my $clean = 'X' x length($1);
426		$res =~ s@\<.*\>@<$clean>@;
427
428	# The whole of a #error is a string.
429	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
430		my $clean = 'X' x length($1);
431		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
432	}
433
434	return $res;
435}
436
437sub ctx_statement_block {
438	my ($linenr, $remain, $off) = @_;
439	my $line = $linenr - 1;
440	my $blk = '';
441	my $soff = $off;
442	my $coff = $off - 1;
443	my $coff_set = 0;
444
445	my $loff = 0;
446
447	my $type = '';
448	my $level = 0;
449	my @stack = ();
450	my $p;
451	my $c;
452	my $len = 0;
453
454	my $remainder;
455	while (1) {
456		@stack = (['', 0]) if ($#stack == -1);
457
458		#warn "CSB: blk<$blk> remain<$remain>\n";
459		# If we are about to drop off the end, pull in more
460		# context.
461		if ($off >= $len) {
462			for (; $remain > 0; $line++) {
463				last if (!defined $lines[$line]);
464				next if ($lines[$line] =~ /^-/);
465				$remain--;
466				$loff = $len;
467				$blk .= $lines[$line] . "\n";
468				$len = length($blk);
469				$line++;
470				last;
471			}
472			# Bail if there is no further context.
473			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
474			if ($off >= $len) {
475				last;
476			}
477		}
478		$p = $c;
479		$c = substr($blk, $off, 1);
480		$remainder = substr($blk, $off);
481
482		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
483
484		# Handle nested #if/#else.
485		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
486			push(@stack, [ $type, $level ]);
487		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
488			($type, $level) = @{$stack[$#stack - 1]};
489		} elsif ($remainder =~ /^#\s*endif\b/) {
490			($type, $level) = @{pop(@stack)};
491		}
492
493		# Statement ends at the ';' or a close '}' at the
494		# outermost level.
495		if ($level == 0 && $c eq ';') {
496			last;
497		}
498
499		# An else is really a conditional as long as its not else if
500		if ($level == 0 && $coff_set == 0 &&
501				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
502				$remainder =~ /^(else)(?:\s|{)/ &&
503				$remainder !~ /^else\s+if\b/) {
504			$coff = $off + length($1) - 1;
505			$coff_set = 1;
506			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
507			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
508		}
509
510		if (($type eq '' || $type eq '(') && $c eq '(') {
511			$level++;
512			$type = '(';
513		}
514		if ($type eq '(' && $c eq ')') {
515			$level--;
516			$type = ($level != 0)? '(' : '';
517
518			if ($level == 0 && $coff < $soff) {
519				$coff = $off;
520				$coff_set = 1;
521				#warn "CSB: mark coff<$coff>\n";
522			}
523		}
524		if (($type eq '' || $type eq '{') && $c eq '{') {
525			$level++;
526			$type = '{';
527		}
528		if ($type eq '{' && $c eq '}') {
529			$level--;
530			$type = ($level != 0)? '{' : '';
531
532			if ($level == 0) {
533				if (substr($blk, $off + 1, 1) eq ';') {
534					$off++;
535				}
536				last;
537			}
538		}
539		$off++;
540	}
541	# We are truly at the end, so shuffle to the next line.
542	if ($off == $len) {
543		$loff = $len + 1;
544		$line++;
545		$remain--;
546	}
547
548	my $statement = substr($blk, $soff, $off - $soff + 1);
549	my $condition = substr($blk, $soff, $coff - $soff + 1);
550
551	#warn "STATEMENT<$statement>\n";
552	#warn "CONDITION<$condition>\n";
553
554	#print "coff<$coff> soff<$off> loff<$loff>\n";
555
556	return ($statement, $condition,
557			$line, $remain + 1, $off - $loff + 1, $level);
558}
559
560sub statement_lines {
561	my ($stmt) = @_;
562
563	# Strip the diff line prefixes and rip blank lines at start and end.
564	$stmt =~ s/(^|\n)./$1/g;
565	$stmt =~ s/^\s*//;
566	$stmt =~ s/\s*$//;
567
568	my @stmt_lines = ($stmt =~ /\n/g);
569
570	return $#stmt_lines + 2;
571}
572
573sub statement_rawlines {
574	my ($stmt) = @_;
575
576	my @stmt_lines = ($stmt =~ /\n/g);
577
578	return $#stmt_lines + 2;
579}
580
581sub statement_block_size {
582	my ($stmt) = @_;
583
584	$stmt =~ s/(^|\n)./$1/g;
585	$stmt =~ s/^\s*\{//;
586	$stmt =~ s/}\s*$//;
587	$stmt =~ s/^\s*//;
588	$stmt =~ s/\s*$//;
589
590	my @stmt_lines = ($stmt =~ /\n/g);
591	my @stmt_statements = ($stmt =~ /;/g);
592
593	my $stmt_lines = $#stmt_lines + 2;
594	my $stmt_statements = $#stmt_statements + 1;
595
596	if ($stmt_lines > $stmt_statements) {
597		return $stmt_lines;
598	} else {
599		return $stmt_statements;
600	}
601}
602
603sub ctx_statement_full {
604	my ($linenr, $remain, $off) = @_;
605	my ($statement, $condition, $level);
606
607	my (@chunks);
608
609	# Grab the first conditional/block pair.
610	($statement, $condition, $linenr, $remain, $off, $level) =
611				ctx_statement_block($linenr, $remain, $off);
612	#print "F: c<$condition> s<$statement> remain<$remain>\n";
613	push(@chunks, [ $condition, $statement ]);
614	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
615		return ($level, $linenr, @chunks);
616	}
617
618	# Pull in the following conditional/block pairs and see if they
619	# could continue the statement.
620	for (;;) {
621		($statement, $condition, $linenr, $remain, $off, $level) =
622				ctx_statement_block($linenr, $remain, $off);
623		#print "C: c<$condition> s<$statement> remain<$remain>\n";
624		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
625		#print "C: push\n";
626		push(@chunks, [ $condition, $statement ]);
627	}
628
629	return ($level, $linenr, @chunks);
630}
631
632sub ctx_block_get {
633	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
634	my $line;
635	my $start = $linenr - 1;
636	my $blk = '';
637	my @o;
638	my @c;
639	my @res = ();
640
641	my $level = 0;
642	my @stack = ($level);
643	for ($line = $start; $remain > 0; $line++) {
644		next if ($rawlines[$line] =~ /^-/);
645		$remain--;
646
647		$blk .= $rawlines[$line];
648
649		# Handle nested #if/#else.
650		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
651			push(@stack, $level);
652		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
653			$level = $stack[$#stack - 1];
654		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
655			$level = pop(@stack);
656		}
657
658		foreach my $c (split(//, $lines[$line])) {
659			##print "C<$c>L<$level><$open$close>O<$off>\n";
660			if ($off > 0) {
661				$off--;
662				next;
663			}
664
665			if ($c eq $close && $level > 0) {
666				$level--;
667				last if ($level == 0);
668			} elsif ($c eq $open) {
669				$level++;
670			}
671		}
672
673		if (!$outer || $level <= 1) {
674			push(@res, $rawlines[$line]);
675		}
676
677		last if ($level == 0);
678	}
679
680	return ($level, @res);
681}
682sub ctx_block_outer {
683	my ($linenr, $remain) = @_;
684
685	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
686	return @r;
687}
688sub ctx_block {
689	my ($linenr, $remain) = @_;
690
691	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
692	return @r;
693}
694sub ctx_statement {
695	my ($linenr, $remain, $off) = @_;
696
697	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
698	return @r;
699}
700sub ctx_block_level {
701	my ($linenr, $remain) = @_;
702
703	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
704}
705sub ctx_statement_level {
706	my ($linenr, $remain, $off) = @_;
707
708	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
709}
710
711sub ctx_locate_comment {
712	my ($first_line, $end_line) = @_;
713
714	# Catch a comment on the end of the line itself.
715	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
716	return $current_comment if (defined $current_comment);
717
718	# Look through the context and try and figure out if there is a
719	# comment.
720	my $in_comment = 0;
721	$current_comment = '';
722	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
723		my $line = $rawlines[$linenr - 1];
724		#warn "           $line\n";
725		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
726			$in_comment = 1;
727		}
728		if ($line =~ m@/\*@) {
729			$in_comment = 1;
730		}
731		if (!$in_comment && $current_comment ne '') {
732			$current_comment = '';
733		}
734		$current_comment .= $line . "\n" if ($in_comment);
735		if ($line =~ m@\*/@) {
736			$in_comment = 0;
737		}
738	}
739
740	chomp($current_comment);
741	return($current_comment);
742}
743sub ctx_has_comment {
744	my ($first_line, $end_line) = @_;
745	my $cmt = ctx_locate_comment($first_line, $end_line);
746
747	##print "LINE: $rawlines[$end_line - 1 ]\n";
748	##print "CMMT: $cmt\n";
749
750	return ($cmt ne '');
751}
752
753sub raw_line {
754	my ($linenr, $cnt) = @_;
755
756	my $offset = $linenr - 1;
757	$cnt++;
758
759	my $line;
760	while ($cnt) {
761		$line = $rawlines[$offset++];
762		next if (defined($line) && $line =~ /^-/);
763		$cnt--;
764	}
765
766	return $line;
767}
768
769sub cat_vet {
770	my ($vet) = @_;
771	my ($res, $coded);
772
773	$res = '';
774	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
775		$res .= $1;
776		if ($2 ne '') {
777			$coded = sprintf("^%c", unpack('C', $2) + 64);
778			$res .= $coded;
779		}
780	}
781	$res =~ s/$/\$/;
782
783	return $res;
784}
785
786my $av_preprocessor = 0;
787my $av_pending;
788my @av_paren_type;
789my $av_pend_colon;
790
791sub annotate_reset {
792	$av_preprocessor = 0;
793	$av_pending = '_';
794	@av_paren_type = ('E');
795	$av_pend_colon = 'O';
796}
797
798sub annotate_values {
799	my ($stream, $type) = @_;
800
801	my $res;
802	my $var = '_' x length($stream);
803	my $cur = $stream;
804
805	print "$stream\n" if ($dbg_values > 1);
806
807	while (length($cur)) {
808		@av_paren_type = ('E') if ($#av_paren_type < 0);
809		print " <" . join('', @av_paren_type) .
810				"> <$type> <$av_pending>" if ($dbg_values > 1);
811		if ($cur =~ /^(\s+)/o) {
812			print "WS($1)\n" if ($dbg_values > 1);
813			if ($1 =~ /\n/ && $av_preprocessor) {
814				$type = pop(@av_paren_type);
815				$av_preprocessor = 0;
816			}
817
818		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
819			print "CAST($1)\n" if ($dbg_values > 1);
820			push(@av_paren_type, $type);
821			$type = 'C';
822
823		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
824			print "DECLARE($1)\n" if ($dbg_values > 1);
825			$type = 'T';
826
827		} elsif ($cur =~ /^($Modifier)\s*/) {
828			print "MODIFIER($1)\n" if ($dbg_values > 1);
829			$type = 'T';
830
831		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
832			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
833			$av_preprocessor = 1;
834			push(@av_paren_type, $type);
835			if ($2 ne '') {
836				$av_pending = 'N';
837			}
838			$type = 'E';
839
840		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
841			print "UNDEF($1)\n" if ($dbg_values > 1);
842			$av_preprocessor = 1;
843			push(@av_paren_type, $type);
844
845		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
846			print "PRE_START($1)\n" if ($dbg_values > 1);
847			$av_preprocessor = 1;
848
849			push(@av_paren_type, $type);
850			push(@av_paren_type, $type);
851			$type = 'E';
852
853		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
854			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
855			$av_preprocessor = 1;
856
857			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
858
859			$type = 'E';
860
861		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
862			print "PRE_END($1)\n" if ($dbg_values > 1);
863
864			$av_preprocessor = 1;
865
866			# Assume all arms of the conditional end as this
867			# one does, and continue as if the #endif was not here.
868			pop(@av_paren_type);
869			push(@av_paren_type, $type);
870			$type = 'E';
871
872		} elsif ($cur =~ /^(\\\n)/o) {
873			print "PRECONT($1)\n" if ($dbg_values > 1);
874
875		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
876			print "ATTR($1)\n" if ($dbg_values > 1);
877			$av_pending = $type;
878			$type = 'N';
879
880		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
881			print "SIZEOF($1)\n" if ($dbg_values > 1);
882			if (defined $2) {
883				$av_pending = 'V';
884			}
885			$type = 'N';
886
887		} elsif ($cur =~ /^(if|while|for)\b/o) {
888			print "COND($1)\n" if ($dbg_values > 1);
889			$av_pending = 'E';
890			$type = 'N';
891
892		} elsif ($cur =~/^(case)/o) {
893			print "CASE($1)\n" if ($dbg_values > 1);
894			$av_pend_colon = 'C';
895			$type = 'N';
896
897		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
898			print "KEYWORD($1)\n" if ($dbg_values > 1);
899			$type = 'N';
900
901		} elsif ($cur =~ /^(\()/o) {
902			print "PAREN('$1')\n" if ($dbg_values > 1);
903			push(@av_paren_type, $av_pending);
904			$av_pending = '_';
905			$type = 'N';
906
907		} elsif ($cur =~ /^(\))/o) {
908			my $new_type = pop(@av_paren_type);
909			if ($new_type ne '_') {
910				$type = $new_type;
911				print "PAREN('$1') -> $type\n"
912							if ($dbg_values > 1);
913			} else {
914				print "PAREN('$1')\n" if ($dbg_values > 1);
915			}
916
917		} elsif ($cur =~ /^($Ident)\s*\(/o) {
918			print "FUNC($1)\n" if ($dbg_values > 1);
919			$type = 'V';
920			$av_pending = 'V';
921
922		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
923			if (defined $2 && $type eq 'C' || $type eq 'T') {
924				$av_pend_colon = 'B';
925			} elsif ($type eq 'E') {
926				$av_pend_colon = 'L';
927			}
928			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
929			$type = 'V';
930
931		} elsif ($cur =~ /^($Ident|$Constant)/o) {
932			print "IDENT($1)\n" if ($dbg_values > 1);
933			$type = 'V';
934
935		} elsif ($cur =~ /^($Assignment)/o) {
936			print "ASSIGN($1)\n" if ($dbg_values > 1);
937			$type = 'N';
938
939		} elsif ($cur =~/^(;|{|})/) {
940			print "END($1)\n" if ($dbg_values > 1);
941			$type = 'E';
942			$av_pend_colon = 'O';
943
944		} elsif ($cur =~/^(,)/) {
945			print "COMMA($1)\n" if ($dbg_values > 1);
946			$type = 'C';
947
948		} elsif ($cur =~ /^(\?)/o) {
949			print "QUESTION($1)\n" if ($dbg_values > 1);
950			$type = 'N';
951
952		} elsif ($cur =~ /^(:)/o) {
953			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
954
955			substr($var, length($res), 1, $av_pend_colon);
956			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
957				$type = 'E';
958			} else {
959				$type = 'N';
960			}
961			$av_pend_colon = 'O';
962
963		} elsif ($cur =~ /^(\[)/o) {
964			print "CLOSE($1)\n" if ($dbg_values > 1);
965			$type = 'N';
966
967		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
968			my $variant;
969
970			print "OPV($1)\n" if ($dbg_values > 1);
971			if ($type eq 'V') {
972				$variant = 'B';
973			} else {
974				$variant = 'U';
975			}
976
977			substr($var, length($res), 1, $variant);
978			$type = 'N';
979
980		} elsif ($cur =~ /^($Operators)/o) {
981			print "OP($1)\n" if ($dbg_values > 1);
982			if ($1 ne '++' && $1 ne '--') {
983				$type = 'N';
984			}
985
986		} elsif ($cur =~ /(^.)/o) {
987			print "C($1)\n" if ($dbg_values > 1);
988		}
989		if (defined $1) {
990			$cur = substr($cur, length($1));
991			$res .= $type x length($1);
992		}
993	}
994
995	return ($res, $var);
996}
997
998sub possible {
999	my ($possible, $line) = @_;
1000	my $notPermitted = qr{(?:
1001		^(?:
1002			$Modifier|
1003			$Storage|
1004			$Type|
1005			DEFINE_\S+
1006		)$|
1007		^(?:
1008			goto|
1009			return|
1010			case|
1011			else|
1012			asm|__asm__|
1013			do|
1014			\#|
1015			\#\#
1016		)(?:\s|$)|
1017		^(?:typedef|struct|enum)\b
1018	    )}x;
1019	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1020	if ($possible !~ $notPermitted) {
1021		# Check for modifiers.
1022		$possible =~ s/\s*$Storage\s*//g;
1023		$possible =~ s/\s*$Sparse\s*//g;
1024		if ($possible =~ /^\s*$/) {
1025
1026		} elsif ($possible =~ /\s/) {
1027			$possible =~ s/\s*$Type\s*//g;
1028			for my $modifier (split(' ', $possible)) {
1029				if ($modifier !~ $notPermitted) {
1030					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1031					push(@modifierList, $modifier);
1032				}
1033			}
1034
1035		} else {
1036			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1037			push(@typeList, $possible);
1038		}
1039		build_types();
1040	} else {
1041		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1042	}
1043}
1044
1045my $prefix = '';
1046
1047sub report {
1048	if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1049		return 0;
1050	}
1051	my $line = $prefix . $_[0];
1052
1053	$line = (split('\n', $line))[0] . "\n" if ($terse);
1054
1055	push(our @report, $line);
1056
1057	return 1;
1058}
1059sub report_dump {
1060	our @report;
1061}
1062sub ERROR {
1063	if (report("ERROR: $_[0]\n")) {
1064		our $clean = 0;
1065		our $cnt_error++;
1066	}
1067}
1068sub WARN {
1069	if (report("WARNING: $_[0]\n")) {
1070		our $clean = 0;
1071		our $cnt_warn++;
1072	}
1073}
1074
1075sub process {
1076	my $filename = shift;
1077
1078	my $linenr=0;
1079	my $prevline="";
1080	my $prevrawline="";
1081	my $stashline="";
1082	my $stashrawline="";
1083
1084	my $length;
1085	my $indent;
1086	my $previndent=0;
1087	my $stashindent=0;
1088
1089	our $clean = 1;
1090	my $signoff = 0;
1091	my $is_patch = 0;
1092
1093	our @report = ();
1094	our $cnt_lines = 0;
1095	our $cnt_error = 0;
1096	our $cnt_warn = 0;
1097	our $cnt_chk = 0;
1098
1099	# Trace the real file/line as we go.
1100	my $realfile = '';
1101	my $realline = 0;
1102	my $realcnt = 0;
1103	my $here = '';
1104	my $in_comment = 0;
1105	my $comment_edge = 0;
1106	my $first_line = 0;
1107	my $p1_prefix = '';
1108
1109	my $prev_values = 'E';
1110
1111	# suppression flags
1112	my %suppress_ifbraces;
1113	my %suppress_whiletrailers;
1114	my %suppress_export;
1115
1116	# Pre-scan the patch sanitizing the lines.
1117
1118	sanitise_line_reset();
1119	my $line;
1120	foreach my $rawline (@rawlines) {
1121		$linenr++;
1122		$line = $rawline;
1123
1124		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1125			$realline=$1-1;
1126			if (defined $2) {
1127				$realcnt=$3+1;
1128			} else {
1129				$realcnt=1+1;
1130			}
1131			$in_comment = 0;
1132
1133			# Guestimate if this is a continuing comment.  Run
1134			# the context looking for a comment "edge".  If this
1135			# edge is a close comment then we must be in a comment
1136			# at context start.
1137			my $edge;
1138			my $cnt = $realcnt;
1139			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1140				next if (defined $rawlines[$ln - 1] &&
1141					 $rawlines[$ln - 1] =~ /^-/);
1142				$cnt--;
1143				#print "RAW<$rawlines[$ln - 1]>\n";
1144				last if (!defined $rawlines[$ln - 1]);
1145				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1146				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1147					($edge) = $1;
1148					last;
1149				}
1150			}
1151			if (defined $edge && $edge eq '*/') {
1152				$in_comment = 1;
1153			}
1154
1155			# Guestimate if this is a continuing comment.  If this
1156			# is the start of a diff block and this line starts
1157			# ' *' then it is very likely a comment.
1158			if (!defined $edge &&
1159			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1160			{
1161				$in_comment = 1;
1162			}
1163
1164			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1165			sanitise_line_reset($in_comment);
1166
1167		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1168			# Standardise the strings and chars within the input to
1169			# simplify matching -- only bother with positive lines.
1170			$line = sanitise_line($rawline);
1171		}
1172		push(@lines, $line);
1173
1174		if ($realcnt > 1) {
1175			$realcnt-- if ($line =~ /^(?:\+| |$)/);
1176		} else {
1177			$realcnt = 0;
1178		}
1179
1180		#print "==>$rawline\n";
1181		#print "-->$line\n";
1182	}
1183
1184	$prefix = '';
1185
1186	$realcnt = 0;
1187	$linenr = 0;
1188	foreach my $line (@lines) {
1189		$linenr++;
1190
1191		my $rawline = $rawlines[$linenr - 1];
1192
1193#extract the line range in the file after the patch is applied
1194		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1195			$is_patch = 1;
1196			$first_line = $linenr + 1;
1197			$realline=$1-1;
1198			if (defined $2) {
1199				$realcnt=$3+1;
1200			} else {
1201				$realcnt=1+1;
1202			}
1203			annotate_reset();
1204			$prev_values = 'E';
1205
1206			%suppress_ifbraces = ();
1207			%suppress_whiletrailers = ();
1208			%suppress_export = ();
1209			next;
1210
1211# track the line number as we move through the hunk, note that
1212# new versions of GNU diff omit the leading space on completely
1213# blank context lines so we need to count that too.
1214		} elsif ($line =~ /^( |\+|$)/) {
1215			$realline++;
1216			$realcnt-- if ($realcnt != 0);
1217
1218			# Measure the line length and indent.
1219			($length, $indent) = line_stats($rawline);
1220
1221			# Track the previous line.
1222			($prevline, $stashline) = ($stashline, $line);
1223			($previndent, $stashindent) = ($stashindent, $indent);
1224			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1225
1226			#warn "line<$line>\n";
1227
1228		} elsif ($realcnt == 1) {
1229			$realcnt--;
1230		}
1231
1232		my $hunk_line = ($realcnt != 0);
1233
1234#make up the handle for any error we report on this line
1235		$prefix = "$filename:$realline: " if ($emacs && $file);
1236		$prefix = "$filename:$linenr: " if ($emacs && !$file);
1237
1238		$here = "#$linenr: " if (!$file);
1239		$here = "#$realline: " if ($file);
1240
1241		# extract the filename as it passes
1242		if ($line =~ /^diff --git.*?(\S+)$/) {
1243			$realfile = $1;
1244			$realfile =~ s@^([^/]*)/@@;
1245
1246		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1247			$realfile = $1;
1248			$realfile =~ s@^([^/]*)/@@;
1249
1250			$p1_prefix = $1;
1251			if (!$file && $tree && $p1_prefix ne '' &&
1252			    -e "$root/$p1_prefix") {
1253				WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1254			}
1255
1256			next;
1257		}
1258
1259		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1260
1261		my $hereline = "$here\n$rawline\n";
1262		my $herecurr = "$here\n$rawline\n";
1263		my $hereprev = "$here\n$prevrawline\n$rawline\n";
1264
1265		$cnt_lines++ if ($realcnt != 0);
1266
1267# Check for incorrect file permissions
1268		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1269			my $permhere = $here . "FILE: $realfile\n";
1270			if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
1271				ERROR("do not set execute permissions for source files\n" . $permhere);
1272			}
1273		}
1274
1275# Accept git diff extended headers as valid patches
1276		if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) {
1277			$is_patch = 1;
1278		}
1279
1280#check the patch for a signoff:
1281		if ($line =~ /^\s*signed-off-by:/i) {
1282			# This is a signoff, if ugly, so do not double report.
1283			$signoff++;
1284			if (!($line =~ /^\s*Signed-off-by:/)) {
1285				ERROR("The correct form is \"Signed-off-by\"\n" .
1286					$herecurr);
1287			}
1288			if ($line =~ /^\s*signed-off-by:\S/i) {
1289				ERROR("space required after Signed-off-by:\n" .
1290					$herecurr);
1291			}
1292		}
1293
1294# Check for wrappage within a valid hunk of the file
1295		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1296			ERROR("patch seems to be corrupt (line wrapped?)\n" .
1297				$herecurr) if (!$emitted_corrupt++);
1298		}
1299
1300# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1301		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1302		    $rawline !~ m/^$UTF8*$/) {
1303			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1304
1305			my $blank = copy_spacing($rawline);
1306			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1307			my $hereptr = "$hereline$ptr\n";
1308
1309			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1310		}
1311
1312# ignore non-hunk lines and lines being removed
1313		next if (!$hunk_line || $line =~ /^-/);
1314
1315# ignore files that are being periodically imported from Linux
1316		next if ($realfile =~ /^(linux-headers|include\/standard-headers)\//);
1317
1318#trailing whitespace
1319		if ($line =~ /^\+.*\015/) {
1320			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1321			ERROR("DOS line endings\n" . $herevet);
1322
1323		} elsif ($realfile =~ /^docs\/.+\.txt/ ||
1324			 $realfile =~ /^docs\/.+\.md/) {
1325		    if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1326			# TODO: properly check we're in a code block
1327			#       (surrounding text is 4-column aligned)
1328			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1329			ERROR("code blocks in documentation should have " .
1330			      "empty lines with exactly 4 columns of " .
1331			      "whitespace\n" . $herevet);
1332		    }
1333		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1334			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1335			ERROR("trailing whitespace\n" . $herevet);
1336			$rpt_cleaners = 1;
1337		}
1338
1339# check we are in a valid source file if not then ignore this hunk
1340		next if ($realfile !~ /\.(h|c|cpp|s|S|pl|py|sh)$/);
1341
1342#90 column limit
1343		if ($line =~ /^\+/ &&
1344		    !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1345		    $length > 80)
1346		{
1347			if ($length > 90) {
1348				ERROR("line over 90 characters\n" . $herecurr);
1349			} else {
1350				WARN("line over 80 characters\n" . $herecurr);
1351			}
1352		}
1353
1354# check for spaces before a quoted newline
1355		if ($rawline =~ /^.*\".*\s\\n/) {
1356			ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr);
1357		}
1358
1359# check for adding lines without a newline.
1360		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1361			ERROR("adding a line without newline at end of file\n" . $herecurr);
1362		}
1363
1364# check for RCS/CVS revision markers
1365		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) {
1366			ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1367		}
1368
1369# tabs are only allowed in assembly source code, and in
1370# some scripts we imported from other projects.
1371		next if ($realfile =~ /\.(s|S)$/);
1372		next if ($realfile =~ /(checkpatch|get_maintainer|texi2pod)\.pl$/);
1373
1374		if ($rawline =~ /^\+.*\t/) {
1375			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1376			ERROR("code indent should never use tabs\n" . $herevet);
1377			$rpt_cleaners = 1;
1378		}
1379
1380# check we are in a valid C source file if not then ignore this hunk
1381		next if ($realfile !~ /\.(h|c|cpp)$/);
1382
1383# Check for potential 'bare' types
1384		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1385		    $realline_next);
1386		if ($realcnt && $line =~ /.\s*\S/) {
1387			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1388				ctx_statement_block($linenr, $realcnt, 0);
1389			$stat =~ s/\n./\n /g;
1390			$cond =~ s/\n./\n /g;
1391
1392			# Find the real next line.
1393			$realline_next = $line_nr_next;
1394			if (defined $realline_next &&
1395			    (!defined $lines[$realline_next - 1] ||
1396			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1397				$realline_next++;
1398			}
1399
1400			my $s = $stat;
1401			$s =~ s/{.*$//s;
1402
1403			# Ignore goto labels.
1404			if ($s =~ /$Ident:\*$/s) {
1405
1406			# Ignore functions being called
1407			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1408
1409			} elsif ($s =~ /^.\s*else\b/s) {
1410
1411			# declarations always start with types
1412			} elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
1413				my $type = $1;
1414				$type =~ s/\s+/ /g;
1415				possible($type, "A:" . $s);
1416
1417			# definitions in global scope can only start with types
1418			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1419				possible($1, "B:" . $s);
1420			}
1421
1422			# any (foo ... *) is a pointer cast, and foo is a type
1423			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1424				possible($1, "C:" . $s);
1425			}
1426
1427			# Check for any sort of function declaration.
1428			# int foo(something bar, other baz);
1429			# void (*store_gdt)(x86_descr_ptr *);
1430			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1431				my ($name_len) = length($1);
1432
1433				my $ctx = $s;
1434				substr($ctx, 0, $name_len + 1, '');
1435				$ctx =~ s/\)[^\)]*$//;
1436
1437				for my $arg (split(/\s*,\s*/, $ctx)) {
1438					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1439
1440						possible($1, "D:" . $s);
1441					}
1442				}
1443			}
1444
1445		}
1446
1447#
1448# Checks which may be anchored in the context.
1449#
1450
1451# Check for switch () and associated case and default
1452# statements should be at the same indent.
1453		if ($line=~/\bswitch\s*\(.*\)/) {
1454			my $err = '';
1455			my $sep = '';
1456			my @ctx = ctx_block_outer($linenr, $realcnt);
1457			shift(@ctx);
1458			for my $ctx (@ctx) {
1459				my ($clen, $cindent) = line_stats($ctx);
1460				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1461							$indent != $cindent) {
1462					$err .= "$sep$ctx\n";
1463					$sep = '';
1464				} else {
1465					$sep = "[...]\n";
1466				}
1467			}
1468			if ($err ne '') {
1469				ERROR("switch and case should be at the same indent\n$hereline$err");
1470			}
1471		}
1472
1473# if/while/etc brace do not go on next line, unless defining a do while loop,
1474# or if that brace on the next line is for something else
1475		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1476			my $pre_ctx = "$1$2";
1477
1478			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1479			my $ctx_cnt = $realcnt - $#ctx - 1;
1480			my $ctx = join("\n", @ctx);
1481
1482			my $ctx_ln = $linenr;
1483			my $ctx_skip = $realcnt;
1484
1485			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1486					defined $lines[$ctx_ln - 1] &&
1487					$lines[$ctx_ln - 1] =~ /^-/)) {
1488				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1489				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1490				$ctx_ln++;
1491			}
1492
1493			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1494			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1495
1496			# The length of the "previous line" is checked against 80 because it
1497			# includes the + at the beginning of the line (if the actual line has
1498			# 79 or 80 characters, it is no longer possible to add a space and an
1499			# opening brace there)
1500			if ($#ctx == 0 && $ctx !~ /{\s*/ &&
1501			    defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
1502			    defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
1503				ERROR("that open brace { should be on the previous line\n" .
1504					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1505			}
1506			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1507			    $ctx =~ /\)\s*\;\s*$/ &&
1508			    defined $lines[$ctx_ln - 1])
1509			{
1510				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1511				if ($nindent > $indent) {
1512					ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" .
1513						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1514				}
1515			}
1516		}
1517
1518# Check relative indent for conditionals and blocks.
1519		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1520			my ($s, $c) = ($stat, $cond);
1521
1522			substr($s, 0, length($c), '');
1523
1524			# Make sure we remove the line prefixes as we have
1525			# none on the first line, and are going to readd them
1526			# where necessary.
1527			$s =~ s/\n./\n/gs;
1528
1529			# Find out how long the conditional actually is.
1530			my @newlines = ($c =~ /\n/gs);
1531			my $cond_lines = 1 + $#newlines;
1532
1533			# We want to check the first line inside the block
1534			# starting at the end of the conditional, so remove:
1535			#  1) any blank line termination
1536			#  2) any opening brace { on end of the line
1537			#  3) any do (...) {
1538			my $continuation = 0;
1539			my $check = 0;
1540			$s =~ s/^.*\bdo\b//;
1541			$s =~ s/^\s*\{//;
1542			if ($s =~ s/^\s*\\//) {
1543				$continuation = 1;
1544			}
1545			if ($s =~ s/^\s*?\n//) {
1546				$check = 1;
1547				$cond_lines++;
1548			}
1549
1550			# Also ignore a loop construct at the end of a
1551			# preprocessor statement.
1552			if (($prevline =~ /^.\s*#\s*define\s/ ||
1553			    $prevline =~ /\\\s*$/) && $continuation == 0) {
1554				$check = 0;
1555			}
1556
1557			my $cond_ptr = -1;
1558			$continuation = 0;
1559			while ($cond_ptr != $cond_lines) {
1560				$cond_ptr = $cond_lines;
1561
1562				# If we see an #else/#elif then the code
1563				# is not linear.
1564				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1565					$check = 0;
1566				}
1567
1568				# Ignore:
1569				#  1) blank lines, they should be at 0,
1570				#  2) preprocessor lines, and
1571				#  3) labels.
1572				if ($continuation ||
1573				    $s =~ /^\s*?\n/ ||
1574				    $s =~ /^\s*#\s*?/ ||
1575				    $s =~ /^\s*$Ident\s*:/) {
1576					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1577					if ($s =~ s/^.*?\n//) {
1578						$cond_lines++;
1579					}
1580				}
1581			}
1582
1583			my (undef, $sindent) = line_stats("+" . $s);
1584			my $stat_real = raw_line($linenr, $cond_lines);
1585
1586			# Check if either of these lines are modified, else
1587			# this is not this patch's fault.
1588			if (!defined($stat_real) ||
1589			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1590				$check = 0;
1591			}
1592			if (defined($stat_real) && $cond_lines > 1) {
1593				$stat_real = "[...]\n$stat_real";
1594			}
1595
1596			#print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
1597
1598			if ($check && (($sindent % 4) != 0 ||
1599			    ($sindent <= $indent && $s ne ''))) {
1600				ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1601			}
1602		}
1603
1604		# Track the 'values' across context and added lines.
1605		my $opline = $line; $opline =~ s/^./ /;
1606		my ($curr_values, $curr_vars) =
1607				annotate_values($opline . "\n", $prev_values);
1608		$curr_values = $prev_values . $curr_values;
1609		if ($dbg_values) {
1610			my $outline = $opline; $outline =~ s/\t/ /g;
1611			print "$linenr > .$outline\n";
1612			print "$linenr > $curr_values\n";
1613			print "$linenr >  $curr_vars\n";
1614		}
1615		$prev_values = substr($curr_values, -1);
1616
1617#ignore lines not being added
1618		if ($line=~/^[^\+]/) {next;}
1619
1620# TEST: allow direct testing of the type matcher.
1621		if ($dbg_type) {
1622			if ($line =~ /^.\s*$Declare\s*$/) {
1623				ERROR("TEST: is type\n" . $herecurr);
1624			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1625				ERROR("TEST: is not type ($1 is)\n". $herecurr);
1626			}
1627			next;
1628		}
1629# TEST: allow direct testing of the attribute matcher.
1630		if ($dbg_attr) {
1631			if ($line =~ /^.\s*$Modifier\s*$/) {
1632				ERROR("TEST: is attr\n" . $herecurr);
1633			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1634				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1635			}
1636			next;
1637		}
1638
1639# check for initialisation to aggregates open brace on the next line
1640		if ($line =~ /^.\s*\{/ &&
1641		    $prevline =~ /(?:^|[^=])=\s*$/) {
1642			ERROR("that open brace { should be on the previous line\n" . $hereprev);
1643		}
1644
1645#
1646# Checks which are anchored on the added line.
1647#
1648
1649# check for malformed paths in #include statements (uses RAW line)
1650		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1651			my $path = $1;
1652			if ($path =~ m{//}) {
1653				ERROR("malformed #include filename\n" .
1654					$herecurr);
1655			}
1656		}
1657
1658# no C99 // comments
1659		if ($line =~ m{//}) {
1660			ERROR("do not use C99 // comments\n" . $herecurr);
1661		}
1662		# Remove C99 comments.
1663		$line =~ s@//.*@@;
1664		$opline =~ s@//.*@@;
1665
1666# check for global initialisers.
1667		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1668			ERROR("do not initialise globals to 0 or NULL\n" .
1669				$herecurr);
1670		}
1671# check for static initialisers.
1672		if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1673			ERROR("do not initialise statics to 0 or NULL\n" .
1674				$herecurr);
1675		}
1676
1677# * goes on variable not on type
1678		# (char*[ const])
1679		if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1680			my ($from, $to) = ($1, $1);
1681
1682			# Should start with a space.
1683			$to =~ s/^(\S)/ $1/;
1684			# Should not end with a space.
1685			$to =~ s/\s+$//;
1686			# '*'s should not have spaces between.
1687			while ($to =~ s/\*\s+\*/\*\*/) {
1688			}
1689
1690			#print "from<$from> to<$to>\n";
1691			if ($from ne $to) {
1692				ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
1693			}
1694		} elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1695			my ($from, $to, $ident) = ($1, $1, $2);
1696
1697			# Should start with a space.
1698			$to =~ s/^(\S)/ $1/;
1699			# Should not end with a space.
1700			$to =~ s/\s+$//;
1701			# '*'s should not have spaces between.
1702			while ($to =~ s/\*\s+\*/\*\*/) {
1703			}
1704			# Modifiers should have spaces.
1705			$to =~ s/(\b$Modifier$)/$1 /;
1706
1707			#print "from<$from> to<$to> ident<$ident>\n";
1708			if ($from ne $to && $ident !~ /^$Modifier$/) {
1709				ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
1710			}
1711		}
1712
1713# function brace can't be on same line, except for #defines of do while,
1714# or if closed on same line
1715		if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
1716		    !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
1717			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1718		}
1719
1720# open braces for enum, union and struct go on the same line.
1721		if ($line =~ /^.\s*\{/ &&
1722		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1723			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1724		}
1725
1726# missing space after union, struct or enum definition
1727		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1728		    ERROR("missing space after $1 definition\n" . $herecurr);
1729		}
1730
1731# check for spacing round square brackets; allowed:
1732#  1. with a type on the left -- int [] a;
1733#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1734#  3. inside a curly brace -- = { [0...10] = 5 }
1735#  4. after a comma -- [1] = 5, [2] = 6
1736#  5. in a macro definition -- #define abc(x) [x] = y
1737		while ($line =~ /(.*?\s)\[/g) {
1738			my ($where, $prefix) = ($-[1], $1);
1739			if ($prefix !~ /$Type\s+$/ &&
1740			    ($where != 0 || $prefix !~ /^.\s+$/) &&
1741			    $prefix !~ /{\s+$/ &&
1742			    $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
1743			    $prefix !~ /,\s+$/) {
1744				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1745			}
1746		}
1747
1748# check for spaces between functions and their parentheses.
1749		while ($line =~ /($Ident)\s+\(/g) {
1750			my $name = $1;
1751			my $ctx_before = substr($line, 0, $-[1]);
1752			my $ctx = "$ctx_before$name";
1753
1754			# Ignore those directives where spaces _are_ permitted.
1755			if ($name =~ /^(?:
1756				if|for|while|switch|return|case|
1757				volatile|__volatile__|
1758				__attribute__|format|__extension__|
1759				asm|__asm__)$/x)
1760			{
1761
1762			# Ignore 'catch (...)' in C++
1763			} elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
1764
1765			# cpp #define statements have non-optional spaces, ie
1766			# if there is a space between the name and the open
1767			# parenthesis it is simply not a parameter group.
1768			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1769
1770			# cpp #elif statement condition may start with a (
1771			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1772
1773			# If this whole things ends with a type its most
1774			# likely a typedef for a function.
1775			} elsif ($ctx =~ /$Type$/) {
1776
1777			} else {
1778				ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1779			}
1780		}
1781# Check operator spacing.
1782		if (!($line=~/\#\s*include/)) {
1783			my $ops = qr{
1784				<<=|>>=|<=|>=|==|!=|
1785				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1786				=>|->|<<|>>|<|>|=|!|~|
1787				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1788				\?|::|:
1789			}x;
1790			my @elements = split(/($ops|;)/, $opline);
1791			my $off = 0;
1792
1793			my $blank = copy_spacing($opline);
1794
1795			for (my $n = 0; $n < $#elements; $n += 2) {
1796				$off += length($elements[$n]);
1797
1798				# Pick up the preceding and succeeding characters.
1799				my $ca = substr($opline, 0, $off);
1800				my $cc = '';
1801				if (length($opline) >= ($off + length($elements[$n + 1]))) {
1802					$cc = substr($opline, $off + length($elements[$n + 1]));
1803				}
1804				my $cb = "$ca$;$cc";
1805
1806				my $a = '';
1807				$a = 'V' if ($elements[$n] ne '');
1808				$a = 'W' if ($elements[$n] =~ /\s$/);
1809				$a = 'C' if ($elements[$n] =~ /$;$/);
1810				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1811				$a = 'O' if ($elements[$n] eq '');
1812				$a = 'E' if ($ca =~ /^\s*$/);
1813
1814				my $op = $elements[$n + 1];
1815
1816				my $c = '';
1817				if (defined $elements[$n + 2]) {
1818					$c = 'V' if ($elements[$n + 2] ne '');
1819					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
1820					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
1821					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1822					$c = 'O' if ($elements[$n + 2] eq '');
1823					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
1824				} else {
1825					$c = 'E';
1826				}
1827
1828				my $ctx = "${a}x${c}";
1829
1830				my $at = "(ctx:$ctx)";
1831
1832				my $ptr = substr($blank, 0, $off) . "^";
1833				my $hereptr = "$hereline$ptr\n";
1834
1835				# Pull out the value of this operator.
1836				my $op_type = substr($curr_values, $off + 1, 1);
1837
1838				# Get the full operator variant.
1839				my $opv = $op . substr($curr_vars, $off, 1);
1840
1841				# Ignore operators passed as parameters.
1842				if ($op_type ne 'V' &&
1843				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1844
1845#				# Ignore comments
1846#				} elsif ($op =~ /^$;+$/) {
1847
1848				# ; should have either the end of line or a space or \ after it
1849				} elsif ($op eq ';') {
1850					if ($ctx !~ /.x[WEBC]/ &&
1851					    $cc !~ /^\\/ && $cc !~ /^;/) {
1852						ERROR("space required after that '$op' $at\n" . $hereptr);
1853					}
1854
1855				# // is a comment
1856				} elsif ($op eq '//') {
1857
1858				# Ignore : used in class declaration in C++
1859				} elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
1860						 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
1861
1862				# No spaces for:
1863				#   ->
1864				#   :   when part of a bitfield
1865				} elsif ($op eq '->' || $opv eq ':B') {
1866					if ($ctx =~ /Wx.|.xW/) {
1867						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
1868					}
1869
1870				# , must have a space on the right.
1871                                # not required when having a single },{ on one line
1872				} elsif ($op eq ',') {
1873					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
1874                                            ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
1875						ERROR("space required after that '$op' $at\n" . $hereptr);
1876					}
1877
1878				# '*' as part of a type definition -- reported already.
1879				} elsif ($opv eq '*_') {
1880					#warn "'*' is part of type\n";
1881
1882				# unary operators should have a space before and
1883				# none after.  May be left adjacent to another
1884				# unary operator, or a cast
1885				} elsif ($op eq '!' || $op eq '~' ||
1886					 $opv eq '*U' || $opv eq '-U' ||
1887					 $opv eq '&U' || $opv eq '&&U') {
1888					if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
1889						# '~' used as a name of Destructor
1890
1891					} elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1892						ERROR("space required before that '$op' $at\n" . $hereptr);
1893					}
1894					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
1895						# A unary '*' may be const
1896
1897					} elsif ($ctx =~ /.xW/) {
1898						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1899					}
1900
1901				# unary ++ and unary -- are allowed no space on one side.
1902				} elsif ($op eq '++' or $op eq '--') {
1903					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1904						ERROR("space required one side of that '$op' $at\n" . $hereptr);
1905					}
1906					if ($ctx =~ /Wx[BE]/ ||
1907					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1908						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1909					}
1910					if ($ctx =~ /ExW/) {
1911						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1912					}
1913
1914				# A colon needs no spaces before when it is
1915				# terminating a case value or a label.
1916				} elsif ($opv eq ':C' || $opv eq ':L') {
1917					if ($ctx =~ /Wx./) {
1918						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1919					}
1920
1921				# All the others need spaces both sides.
1922				} elsif ($ctx !~ /[EWC]x[CWE]/) {
1923					my $ok = 0;
1924
1925					if ($realfile =~ /\.cpp|\.h$/) {
1926						# Ignore template arguments <...> in C++
1927						if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
1928							$ok = 1;
1929						}
1930
1931						# Ignore :: in C++
1932						if ($op eq '::') {
1933							$ok = 1;
1934						}
1935					}
1936
1937					# Ignore email addresses <foo@bar>
1938					if (($op eq '<' &&
1939					     $cc =~ /^\S+\@\S+>/) ||
1940					    ($op eq '>' &&
1941					     $ca =~ /<\S+\@\S+$/))
1942					{
1943						$ok = 1;
1944					}
1945
1946					# Ignore ?:
1947					if (($opv eq ':O' && $ca =~ /\?$/) ||
1948					    ($op eq '?' && $cc =~ /^:/)) {
1949						$ok = 1;
1950					}
1951
1952					if ($ok == 0) {
1953						ERROR("spaces required around that '$op' $at\n" . $hereptr);
1954					}
1955				}
1956				$off += length($elements[$n + 1]);
1957			}
1958		}
1959
1960#need space before brace following if, while, etc
1961		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
1962		    $line =~ /do\{/) {
1963			ERROR("space required before the open brace '{'\n" . $herecurr);
1964		}
1965
1966# closing brace should have a space following it when it has anything
1967# on the line
1968		if ($line =~ /}(?!(?:,|;|\)))\S/) {
1969			ERROR("space required after that close brace '}'\n" . $herecurr);
1970		}
1971
1972# check spacing on square brackets
1973		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1974			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
1975		}
1976		if ($line =~ /\s\]/) {
1977			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
1978		}
1979
1980# check spacing on parentheses
1981		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1982		    $line !~ /for\s*\(\s+;/) {
1983			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
1984		}
1985		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1986		    $line !~ /for\s*\(.*;\s+\)/ &&
1987		    $line !~ /:\s+\)/) {
1988			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
1989		}
1990
1991# Return is not a function.
1992		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1993			my $spacing = $1;
1994			my $value = $2;
1995
1996			# Flatten any parentheses
1997			$value =~ s/\(/ \(/g;
1998			$value =~ s/\)/\) /g;
1999			while ($value =~ s/\[[^\{\}]*\]/1/ ||
2000			       $value !~ /(?:$Ident|-?$Constant)\s*
2001					     $Compare\s*
2002					     (?:$Ident|-?$Constant)/x &&
2003			       $value =~ s/\([^\(\)]*\)/1/) {
2004			}
2005#print "value<$value>\n";
2006			if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2007				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2008
2009			} elsif ($spacing !~ /\s+/) {
2010				ERROR("space required before the open parenthesis '('\n" . $herecurr);
2011			}
2012		}
2013# Return of what appears to be an errno should normally be -'ve
2014		if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2015			my $name = $1;
2016			if ($name ne 'EOF' && $name ne 'ERROR') {
2017				ERROR("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2018			}
2019		}
2020
2021# Need a space before open parenthesis after if, while etc
2022		if ($line=~/\b(if|while|for|switch)\(/) {
2023			ERROR("space required before the open parenthesis '('\n" . $herecurr);
2024		}
2025
2026# Check for illegal assignment in if conditional -- and check for trailing
2027# statements after the conditional.
2028		if ($line =~ /do\s*(?!{)/) {
2029			my ($stat_next) = ctx_statement_block($line_nr_next,
2030						$remain_next, $off_next);
2031			$stat_next =~ s/\n./\n /g;
2032			##print "stat<$stat> stat_next<$stat_next>\n";
2033
2034			if ($stat_next =~ /^\s*while\b/) {
2035				# If the statement carries leading newlines,
2036				# then count those as offsets.
2037				my ($whitespace) =
2038					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2039				my $offset =
2040					statement_rawlines($whitespace) - 1;
2041
2042				$suppress_whiletrailers{$line_nr_next +
2043								$offset} = 1;
2044			}
2045		}
2046		if (!defined $suppress_whiletrailers{$linenr} &&
2047		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2048			my ($s, $c) = ($stat, $cond);
2049
2050			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2051				ERROR("do not use assignment in if condition\n" . $herecurr);
2052			}
2053
2054			# Find out what is on the end of the line after the
2055			# conditional.
2056			substr($s, 0, length($c), '');
2057			$s =~ s/\n.*//g;
2058			$s =~ s/$;//g; 	# Remove any comments
2059			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2060			    $c !~ /}\s*while\s*/)
2061			{
2062				# Find out how long the conditional actually is.
2063				my @newlines = ($c =~ /\n/gs);
2064				my $cond_lines = 1 + $#newlines;
2065				my $stat_real = '';
2066
2067				$stat_real = raw_line($linenr, $cond_lines)
2068							. "\n" if ($cond_lines);
2069				if (defined($stat_real) && $cond_lines > 1) {
2070					$stat_real = "[...]\n$stat_real";
2071				}
2072
2073				ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2074			}
2075		}
2076
2077# Check for bitwise tests written as boolean
2078		if ($line =~ /
2079			(?:
2080				(?:\[|\(|\&\&|\|\|)
2081				\s*0[xX][0-9]+\s*
2082				(?:\&\&|\|\|)
2083			|
2084				(?:\&\&|\|\|)
2085				\s*0[xX][0-9]+\s*
2086				(?:\&\&|\|\||\)|\])
2087			)/x)
2088		{
2089			ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2090		}
2091
2092# if and else should not have general statements after it
2093		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2094			my $s = $1;
2095			$s =~ s/$;//g; 	# Remove any comments
2096			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2097				ERROR("trailing statements should be on next line\n" . $herecurr);
2098			}
2099		}
2100# if should not continue a brace
2101		if ($line =~ /}\s*if\b/) {
2102			ERROR("trailing statements should be on next line\n" .
2103				$herecurr);
2104		}
2105# case and default should not have general statements after them
2106		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2107		    $line !~ /\G(?:
2108			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2109			\s*return\s+
2110		    )/xg)
2111		{
2112			ERROR("trailing statements should be on next line\n" . $herecurr);
2113		}
2114
2115		# Check for }<nl>else {, these must be at the same
2116		# indent level to be relevant to each other.
2117		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2118						$previndent == $indent) {
2119			ERROR("else should follow close brace '}'\n" . $hereprev);
2120		}
2121
2122		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2123						$previndent == $indent) {
2124			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2125
2126			# Find out what is on the end of the line after the
2127			# conditional.
2128			substr($s, 0, length($c), '');
2129			$s =~ s/\n.*//g;
2130
2131			if ($s =~ /^\s*;/) {
2132				ERROR("while should follow close brace '}'\n" . $hereprev);
2133			}
2134		}
2135
2136#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2137#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2138#		    print "No studly caps, use _\n";
2139#		    print "$herecurr";
2140#		    $clean = 0;
2141#		}
2142
2143#no spaces allowed after \ in define
2144		if ($line=~/\#\s*define.*\\\s$/) {
2145			ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr);
2146		}
2147
2148# multi-statement macros should be enclosed in a do while loop, grab the
2149# first statement and ensure its the whole macro if its not enclosed
2150# in a known good container
2151		if ($realfile !~ m@/vmlinux.lds.h$@ &&
2152		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2153			my $ln = $linenr;
2154			my $cnt = $realcnt;
2155			my ($off, $dstat, $dcond, $rest);
2156			my $ctx = '';
2157
2158			my $args = defined($1);
2159
2160			# Find the end of the macro and limit our statement
2161			# search to that.
2162			while ($cnt > 0 && defined $lines[$ln - 1] &&
2163				$lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2164			{
2165				$ctx .= $rawlines[$ln - 1] . "\n";
2166				$cnt-- if ($lines[$ln - 1] !~ /^-/);
2167				$ln++;
2168			}
2169			$ctx .= $rawlines[$ln - 1];
2170
2171			($dstat, $dcond, $ln, $cnt, $off) =
2172				ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2173			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2174			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2175
2176			# Extract the remainder of the define (if any) and
2177			# rip off surrounding spaces, and trailing \'s.
2178			$rest = '';
2179			while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2180				#print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2181				if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2182					$rest .= substr($lines[$ln - 1], $off) . "\n";
2183					$cnt--;
2184				}
2185				$ln++;
2186				$off = 0;
2187			}
2188			$rest =~ s/\\\n.//g;
2189			$rest =~ s/^\s*//s;
2190			$rest =~ s/\s*$//s;
2191
2192			# Clean up the original statement.
2193			if ($args) {
2194				substr($dstat, 0, length($dcond), '');
2195			} else {
2196				$dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2197			}
2198			$dstat =~ s/$;//g;
2199			$dstat =~ s/\\\n.//g;
2200			$dstat =~ s/^\s*//s;
2201			$dstat =~ s/\s*$//s;
2202
2203			# Flatten any parentheses and braces
2204			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2205			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
2206			       $dstat =~ s/\[[^\{\}]*\]/1/)
2207			{
2208			}
2209
2210			my $exceptions = qr{
2211				$Declare|
2212				module_param_named|
2213				MODULE_PARAM_DESC|
2214				DECLARE_PER_CPU|
2215				DEFINE_PER_CPU|
2216				__typeof__\(|
2217				union|
2218				struct|
2219				\.$Ident\s*=\s*|
2220				^\"|\"$
2221			}x;
2222			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2223			if ($rest ne '' && $rest ne ',') {
2224				if ($rest !~ /while\s*\(/ &&
2225				    $dstat !~ /$exceptions/)
2226				{
2227					ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2228				}
2229
2230			} elsif ($ctx !~ /;/) {
2231				if ($dstat ne '' &&
2232				    $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2233				    $dstat !~ /$exceptions/ &&
2234				    $dstat !~ /^\.$Ident\s*=/ &&
2235				    $dstat =~ /$Operators/)
2236				{
2237					ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2238				}
2239			}
2240		}
2241
2242# check for missing bracing round if etc
2243		if ($line =~ /(^.*)\bif\b/ && $line !~ /\#\s*if/) {
2244			my ($level, $endln, @chunks) =
2245				ctx_statement_full($linenr, $realcnt, 1);
2246                        if ($dbg_adv_apw) {
2247                            print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2248                            print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2249                                if $#chunks >= 1;
2250                        }
2251			if ($#chunks >= 0 && $level == 0) {
2252				my $allowed = 0;
2253				my $seen = 0;
2254				my $herectx = $here . "\n";
2255				my $ln = $linenr - 1;
2256				for my $chunk (@chunks) {
2257					my ($cond, $block) = @{$chunk};
2258
2259					# If the condition carries leading newlines, then count those as offsets.
2260					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2261					my $offset = statement_rawlines($whitespace) - 1;
2262
2263					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2264
2265					# We have looked at and allowed this specific line.
2266					$suppress_ifbraces{$ln + $offset} = 1;
2267
2268					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2269					$ln += statement_rawlines($block) - 1;
2270
2271					substr($block, 0, length($cond), '');
2272
2273					my $spaced_block = $block;
2274					$spaced_block =~ s/\n\+/ /g;
2275
2276					$seen++ if ($spaced_block =~ /^\s*\{/);
2277
2278                                        print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2279                                            if $dbg_adv_apw;
2280					if (statement_lines($cond) > 1) {
2281                                            print "APW: ALLOWED: cond<$cond>\n"
2282                                                if $dbg_adv_apw;
2283                                            $allowed = 1;
2284					}
2285					if ($block =~/\b(?:if|for|while)\b/) {
2286                                            print "APW: ALLOWED: block<$block>\n"
2287                                                if $dbg_adv_apw;
2288                                            $allowed = 1;
2289					}
2290					if (statement_block_size($block) > 1) {
2291                                            print "APW: ALLOWED: lines block<$block>\n"
2292                                                if $dbg_adv_apw;
2293                                            $allowed = 1;
2294					}
2295				}
2296				if ($seen != ($#chunks + 1)) {
2297					ERROR("braces {} are necessary for all arms of this statement\n" . $herectx);
2298				}
2299			}
2300		}
2301		if (!defined $suppress_ifbraces{$linenr - 1} &&
2302					$line =~ /\b(if|while|for|else)\b/ &&
2303					$line !~ /\#\s*if/ &&
2304					$line !~ /\#\s*else/) {
2305			my $allowed = 0;
2306
2307                        # Check the pre-context.
2308                        if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2309                            my $pre = $1;
2310
2311                            if ($line !~ /else/) {
2312                                print "APW: ALLOWED: pre<$pre> line<$line>\n"
2313                                    if $dbg_adv_apw;
2314                                $allowed = 1;
2315                            }
2316                        }
2317
2318			my ($level, $endln, @chunks) =
2319				ctx_statement_full($linenr, $realcnt, $-[0]);
2320
2321			# Check the condition.
2322			my ($cond, $block) = @{$chunks[0]};
2323                        print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2324                            if $dbg_adv_checking;
2325			if (defined $cond) {
2326				substr($block, 0, length($cond), '');
2327			}
2328			if (statement_lines($cond) > 1) {
2329                            print "APW: ALLOWED: cond<$cond>\n"
2330                                if $dbg_adv_apw;
2331                            $allowed = 1;
2332			}
2333			if ($block =~/\b(?:if|for|while)\b/) {
2334                            print "APW: ALLOWED: block<$block>\n"
2335                                if $dbg_adv_apw;
2336                            $allowed = 1;
2337			}
2338			if (statement_block_size($block) > 1) {
2339                            print "APW: ALLOWED: lines block<$block>\n"
2340                                if $dbg_adv_apw;
2341                            $allowed = 1;
2342			}
2343			# Check the post-context.
2344			if (defined $chunks[1]) {
2345				my ($cond, $block) = @{$chunks[1]};
2346				if (defined $cond) {
2347					substr($block, 0, length($cond), '');
2348				}
2349				if ($block =~ /^\s*\{/) {
2350                                    print "APW: ALLOWED: chunk-1 block<$block>\n"
2351                                        if $dbg_adv_apw;
2352                                    $allowed = 1;
2353				}
2354			}
2355                        print "DCS: level=$level block<$block> allowed=$allowed\n"
2356                            if $dbg_adv_dcs;
2357			if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2358				my $herectx = $here . "\n";;
2359				my $cnt = statement_rawlines($block);
2360
2361				for (my $n = 0; $n < $cnt; $n++) {
2362					$herectx .= raw_line($linenr, $n) . "\n";;
2363				}
2364
2365				ERROR("braces {} are necessary even for single statement blocks\n" . $herectx);
2366			}
2367		}
2368
2369# no volatiles please
2370		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2371		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2372			ERROR("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2373		}
2374
2375# warn about #if 0
2376		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2377			ERROR("if this code is redundant consider removing it\n" .
2378				$herecurr);
2379		}
2380
2381# check for needless g_free() checks
2382		if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2383			my $expr = $1;
2384			if ($line =~ /\bg_free\(\Q$expr\E\);/) {
2385				ERROR("g_free(NULL) is safe this check is probably not required\n" . $hereprev);
2386			}
2387		}
2388
2389# warn about #ifdefs in C files
2390#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2391#			print "#ifdef in C files should be avoided\n";
2392#			print "$herecurr";
2393#			$clean = 0;
2394#		}
2395
2396# warn about spacing in #ifdefs
2397		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2398			ERROR("exactly one space required after that #$1\n" . $herecurr);
2399		}
2400# check for memory barriers without a comment.
2401		if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2402			if (!ctx_has_comment($first_line, $linenr)) {
2403				ERROR("memory barrier without comment\n" . $herecurr);
2404			}
2405		}
2406# check of hardware specific defines
2407# we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases
2408# where they might be necessary.
2409		if ($line =~ m@^.\s*\#\s*if.*\b__@) {
2410			WARN("architecture specific defines should be avoided\n" .  $herecurr);
2411		}
2412
2413# Check that the storage class is at the beginning of a declaration
2414		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2415			ERROR("storage class should be at the beginning of the declaration\n" . $herecurr)
2416		}
2417
2418# check the location of the inline attribute, that it is between
2419# storage class and type.
2420		if ($line =~ /\b$Type\s+$Inline\b/ ||
2421		    $line =~ /\b$Inline\s+$Storage\b/) {
2422			ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2423		}
2424
2425# check for sizeof(&)
2426		if ($line =~ /\bsizeof\s*\(\s*\&/) {
2427			ERROR("sizeof(& should be avoided\n" . $herecurr);
2428		}
2429
2430# check for new externs in .c files.
2431		if ($realfile =~ /\.c$/ && defined $stat &&
2432		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2433		{
2434			my $function_name = $1;
2435			my $paren_space = $2;
2436
2437			my $s = $stat;
2438			if (defined $cond) {
2439				substr($s, 0, length($cond), '');
2440			}
2441			if ($s =~ /^\s*;/ &&
2442			    $function_name ne 'uninitialized_var')
2443			{
2444				ERROR("externs should be avoided in .c files\n" .  $herecurr);
2445			}
2446
2447			if ($paren_space =~ /\n/) {
2448				ERROR("arguments for function declarations should follow identifier\n" . $herecurr);
2449			}
2450
2451		} elsif ($realfile =~ /\.c$/ && defined $stat &&
2452		    $stat =~ /^.\s*extern\s+/)
2453		{
2454			ERROR("externs should be avoided in .c files\n" .  $herecurr);
2455		}
2456
2457# check for pointless casting of g_malloc return
2458		if ($line =~ /\*\s*\)\s*g_(try)?(m|re)alloc(0?)(_n)?\b/) {
2459			if ($2 == 'm') {
2460				ERROR("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr);
2461			} else {
2462				ERROR("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr);
2463			}
2464		}
2465
2466# check for gcc specific __FUNCTION__
2467		if ($line =~ /__FUNCTION__/) {
2468			ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
2469		}
2470
2471# recommend qemu_strto* over strto* for numeric conversions
2472		if ($line =~ /\b(strto[^kd].*?)\s*\(/) {
2473			ERROR("consider using qemu_$1 in preference to $1\n" . $herecurr);
2474		}
2475# check for module_init(), use category-specific init macros explicitly please
2476		if ($line =~ /^module_init\s*\(/) {
2477			ERROR("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr);
2478		}
2479# check for various ops structs, ensure they are const.
2480		my $struct_ops = qr{AIOCBInfo|
2481				BdrvActionOps|
2482				BlockDevOps|
2483				BlockJobDriver|
2484				DisplayChangeListenerOps|
2485				GraphicHwOps|
2486				IDEDMAOps|
2487				KVMCapabilityInfo|
2488				MemoryRegionIOMMUOps|
2489				MemoryRegionOps|
2490				MemoryRegionPortio|
2491				QEMUFileOps|
2492				SCSIBusInfo|
2493				SCSIReqOps|
2494				Spice[A-Z][a-zA-Z0-9]*Interface|
2495				TPMDriverOps|
2496				USBDesc[A-Z][a-zA-Z0-9]*|
2497				VhostOps|
2498				VMStateDescription|
2499				VMStateInfo}x;
2500		if ($line !~ /\bconst\b/ &&
2501		    $line =~ /\b($struct_ops)\b/) {
2502			ERROR("struct $1 should normally be const\n" .
2503				$herecurr);
2504		}
2505
2506# check for %L{u,d,i} in strings
2507		my $string;
2508		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2509			$string = substr($rawline, $-[1], $+[1] - $-[1]);
2510			$string =~ s/%%/__/g;
2511			if ($string =~ /(?<!%)%L[udi]/) {
2512				ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2513				last;
2514			}
2515		}
2516
2517# QEMU specific tests
2518		if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
2519			ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr);
2520		}
2521
2522# Qemu error function tests
2523
2524	# Find newlines in error messages
2525	my $qemu_error_funcs = qr{error_setg|
2526				error_setg_errno|
2527				error_setg_win32|
2528				error_setg_file_open|
2529				error_set|
2530				error_prepend|
2531				error_reportf_err|
2532				error_vreport|
2533				error_report}x;
2534
2535	if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) {
2536		ERROR("Error messages should not contain newlines\n" . $herecurr);
2537	}
2538
2539	# Continue checking for error messages that contains newlines. This
2540	# check handles cases where string literals are spread over multiple lines.
2541	# Example:
2542	# error_report("Error msg line #1"
2543	#              "Error msg line #2\n");
2544	my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2545	my $continued_str_literal = qr{\+\s*\".*\"};
2546
2547	if ($rawline =~ /$quoted_newline_regex/) {
2548		# Backtrack to first line that does not contain only a quoted literal
2549		# and assume that it is the start of the statement.
2550		my $i = $linenr - 2;
2551
2552		while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2553			$i--;
2554		}
2555
2556		if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) {
2557			ERROR("Error messages should not contain newlines\n" . $herecurr);
2558		}
2559	}
2560
2561# check for non-portable libc calls that have portable alternatives in QEMU
2562		if ($line =~ /\bffs\(/) {
2563			ERROR("use ctz32() instead of ffs()\n" . $herecurr);
2564		}
2565		if ($line =~ /\bffsl\(/) {
2566			ERROR("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr);
2567		}
2568		if ($line =~ /\bffsll\(/) {
2569			ERROR("use ctz64() instead of ffsll()\n" . $herecurr);
2570		}
2571		if ($line =~ /\bbzero\(/) {
2572			ERROR("use memset() instead of bzero()\n" . $herecurr);
2573		}
2574	}
2575
2576	# If we have no input at all, then there is nothing to report on
2577	# so just keep quiet.
2578	if ($#rawlines == -1) {
2579		exit(0);
2580	}
2581
2582	# In mailback mode only produce a report in the negative, for
2583	# things that appear to be patches.
2584	if ($mailback && ($clean == 1 || !$is_patch)) {
2585		exit(0);
2586	}
2587
2588	# This is not a patch, and we are are in 'no-patch' mode so
2589	# just keep quiet.
2590	if (!$chk_patch && !$is_patch) {
2591		exit(0);
2592	}
2593
2594	if (!$is_patch) {
2595		ERROR("Does not appear to be a unified-diff format patch\n");
2596	}
2597	if ($is_patch && $chk_signoff && $signoff == 0) {
2598		ERROR("Missing Signed-off-by: line(s)\n");
2599	}
2600
2601	print report_dump();
2602	if ($summary && !($clean == 1 && $quiet == 1)) {
2603		print "$filename " if ($summary_file);
2604		print "total: $cnt_error errors, $cnt_warn warnings, " .
2605			"$cnt_lines lines checked\n";
2606		print "\n" if ($quiet == 0);
2607	}
2608
2609	if ($quiet == 0) {
2610		# If there were whitespace errors which cleanpatch can fix
2611		# then suggest that.
2612#		if ($rpt_cleaners) {
2613#			print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2614#			print "      scripts/cleanfile\n\n";
2615#		}
2616	}
2617
2618	if ($clean == 1 && $quiet == 0) {
2619		print "$vname has no obvious style problems and is ready for submission.\n"
2620	}
2621	if ($clean == 0 && $quiet == 0) {
2622		print "$vname has style problems, please review.  If any of these errors\n";
2623		print "are false positives report them to the maintainer, see\n";
2624		print "CHECKPATCH in MAINTAINERS.\n";
2625	}
2626
2627	return ($no_warnings ? $clean : $cnt_error == 0);
2628}
2629