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