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