xref: /linux-6.15/scripts/checkpatch.pl (revision 4a0df2ef)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
20a920b5bSAndy Whitcroft# (c) 2001, Dave Jones. <[email protected]> (the file handling bit)
30a920b5bSAndy Whitcroft# (c) 2005, Joel Scohpp <[email protected]> (the ugly bit)
40a920b5bSAndy Whitcroft# (c) 2007, Andy Whitcroft <[email protected]> (new conditions, test suite, etc)
50a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
60a920b5bSAndy Whitcroft
70a920b5bSAndy Whitcroftuse strict;
80a920b5bSAndy Whitcroft
90a920b5bSAndy Whitcroftmy $P = $0;
100a920b5bSAndy Whitcroft
11*4a0df2efSAndy Whitcroftmy $V = '0.03';
120a920b5bSAndy Whitcroft
130a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
140a920b5bSAndy Whitcroft
150a920b5bSAndy Whitcroftmy $quiet = 0;
160a920b5bSAndy Whitcroftmy $tree = 1;
170a920b5bSAndy Whitcroftmy $chk_signoff = 1;
180a920b5bSAndy Whitcroftmy $chk_patch = 1;
190a920b5bSAndy WhitcroftGetOptions(
200a920b5bSAndy Whitcroft	'q|quiet'	=> \$quiet,
210a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
220a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
230a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
240a920b5bSAndy Whitcroft) or exit;
250a920b5bSAndy Whitcroft
260a920b5bSAndy Whitcroftmy $exit = 0;
270a920b5bSAndy Whitcroft
280a920b5bSAndy Whitcroftif ($#ARGV < 0) {
290a920b5bSAndy Whitcroft	print "usage: patchstylecheckemail.pl [options] patchfile\n";
300a920b5bSAndy Whitcroft	print "version: $V\n";
310a920b5bSAndy Whitcroft	print "options: -q           => quiet\n";
320a920b5bSAndy Whitcroft	print "         --no-tree    => run without a kernel tree\n";
330a920b5bSAndy Whitcroft	exit(1);
340a920b5bSAndy Whitcroft}
350a920b5bSAndy Whitcroft
360a920b5bSAndy Whitcroftif ($tree && !top_of_kernel_tree()) {
370a920b5bSAndy Whitcroft	print "Must be run from the top-level dir. of a kernel tree\n";
380a920b5bSAndy Whitcroft	exit(2);
390a920b5bSAndy Whitcroft}
400a920b5bSAndy Whitcroft
41*4a0df2efSAndy Whitcroftmy @dep_includes = ();
42*4a0df2efSAndy Whitcroftmy @dep_functions = ();
430a920b5bSAndy Whitcroftmy $removal = 'Documentation/feature-removal-schedule.txt';
440a920b5bSAndy Whitcroftif ($tree && -f $removal) {
450a920b5bSAndy Whitcroft	open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n";
460a920b5bSAndy Whitcroft	while (<REMOVE>) {
470a920b5bSAndy Whitcroft		if (/^Files:\s+(.*\S)/) {
480a920b5bSAndy Whitcroft			for my $file (split(/[, ]+/, $1)) {
490a920b5bSAndy Whitcroft				if ($file =~ m@include/(.*)@) {
50*4a0df2efSAndy Whitcroft					push(@dep_includes, $1);
510a920b5bSAndy Whitcroft				}
520a920b5bSAndy Whitcroft			}
53*4a0df2efSAndy Whitcroft
54*4a0df2efSAndy Whitcroft		} elsif (/^Funcs:\s+(.*\S)/) {
55*4a0df2efSAndy Whitcroft			for my $func (split(/[, ]+/, $1)) {
56*4a0df2efSAndy Whitcroft				push(@dep_functions, $func);
57*4a0df2efSAndy Whitcroft			}
580a920b5bSAndy Whitcroft		}
590a920b5bSAndy Whitcroft	}
600a920b5bSAndy Whitcroft}
610a920b5bSAndy Whitcroft
620a920b5bSAndy Whitcroftmy @lines = ();
630a920b5bSAndy Whitcroftwhile (<>) {
640a920b5bSAndy Whitcroft	chomp;
650a920b5bSAndy Whitcroft	push(@lines, $_);
660a920b5bSAndy Whitcroft	if (eof(ARGV)) {
670a920b5bSAndy Whitcroft		if (!process($ARGV, @lines)) {
680a920b5bSAndy Whitcroft			$exit = 1;
690a920b5bSAndy Whitcroft		}
700a920b5bSAndy Whitcroft		@lines = ();
710a920b5bSAndy Whitcroft	}
720a920b5bSAndy Whitcroft}
730a920b5bSAndy Whitcroft
740a920b5bSAndy Whitcroftexit($exit);
750a920b5bSAndy Whitcroft
760a920b5bSAndy Whitcroftsub top_of_kernel_tree {
770a920b5bSAndy Whitcroft	if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") &&
780a920b5bSAndy Whitcroft	    (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") &&
790a920b5bSAndy Whitcroft	    (-d "Documentation") && (-d "arch") && (-d "include") &&
800a920b5bSAndy Whitcroft	    (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") &&
810a920b5bSAndy Whitcroft	    (-d "kernel") && (-d "lib") && (-d "scripts")) {
820a920b5bSAndy Whitcroft		return 1;
830a920b5bSAndy Whitcroft	}
840a920b5bSAndy Whitcroft	return 0;
850a920b5bSAndy Whitcroft}
860a920b5bSAndy Whitcroft
870a920b5bSAndy Whitcroftsub expand_tabs {
880a920b5bSAndy Whitcroft	my ($str) = @_;
890a920b5bSAndy Whitcroft
900a920b5bSAndy Whitcroft	my $res = '';
910a920b5bSAndy Whitcroft	my $n = 0;
920a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
930a920b5bSAndy Whitcroft		if ($c eq "\t") {
940a920b5bSAndy Whitcroft			$res .= ' ';
950a920b5bSAndy Whitcroft			$n++;
960a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
970a920b5bSAndy Whitcroft				$res .= ' ';
980a920b5bSAndy Whitcroft			}
990a920b5bSAndy Whitcroft			next;
1000a920b5bSAndy Whitcroft		}
1010a920b5bSAndy Whitcroft		$res .= $c;
1020a920b5bSAndy Whitcroft		$n++;
1030a920b5bSAndy Whitcroft	}
1040a920b5bSAndy Whitcroft
1050a920b5bSAndy Whitcroft	return $res;
1060a920b5bSAndy Whitcroft}
1070a920b5bSAndy Whitcroft
108*4a0df2efSAndy Whitcroftsub line_stats {
109*4a0df2efSAndy Whitcroft	my ($line) = @_;
110*4a0df2efSAndy Whitcroft
111*4a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
112*4a0df2efSAndy Whitcroft	$line =~ s/^.//;
113*4a0df2efSAndy Whitcroft	$line = expand_tabs($line);
114*4a0df2efSAndy Whitcroft
115*4a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
116*4a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
117*4a0df2efSAndy Whitcroft
118*4a0df2efSAndy Whitcroft	return (length($line), length($white));
119*4a0df2efSAndy Whitcroft}
120*4a0df2efSAndy Whitcroft
121*4a0df2efSAndy Whitcroftsub ctx_block_get {
122*4a0df2efSAndy Whitcroft	my ($linenr, $remain, $outer) = @_;
123*4a0df2efSAndy Whitcroft	my $line;
124*4a0df2efSAndy Whitcroft	my $start = $linenr - 1;
125*4a0df2efSAndy Whitcroft	my $end = $linenr - 1 + $remain;
126*4a0df2efSAndy Whitcroft	my $blk = '';
127*4a0df2efSAndy Whitcroft	my @o;
128*4a0df2efSAndy Whitcroft	my @c;
129*4a0df2efSAndy Whitcroft	my @res = ();
130*4a0df2efSAndy Whitcroft
131*4a0df2efSAndy Whitcroft	for ($line = $start; $line < $end; $line++) {
132*4a0df2efSAndy Whitcroft		$blk .= $lines[$line];
133*4a0df2efSAndy Whitcroft
134*4a0df2efSAndy Whitcroft		@o = ($blk =~ /\{/g);
135*4a0df2efSAndy Whitcroft		@c = ($blk =~ /\}/g);
136*4a0df2efSAndy Whitcroft
137*4a0df2efSAndy Whitcroft		if (!$outer || (scalar(@o) - scalar(@c)) == 1) {
138*4a0df2efSAndy Whitcroft			push(@res, $lines[$line]);
139*4a0df2efSAndy Whitcroft		}
140*4a0df2efSAndy Whitcroft
141*4a0df2efSAndy Whitcroft		last if (scalar(@o) == scalar(@c));
142*4a0df2efSAndy Whitcroft	}
143*4a0df2efSAndy Whitcroft
144*4a0df2efSAndy Whitcroft	return @res;
145*4a0df2efSAndy Whitcroft}
146*4a0df2efSAndy Whitcroftsub ctx_block_outer {
147*4a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
148*4a0df2efSAndy Whitcroft
149*4a0df2efSAndy Whitcroft	return ctx_block_get($linenr, $remain, 1);
150*4a0df2efSAndy Whitcroft}
151*4a0df2efSAndy Whitcroftsub ctx_block {
152*4a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
153*4a0df2efSAndy Whitcroft
154*4a0df2efSAndy Whitcroft	return ctx_block_get($linenr, $remain, 0);
155*4a0df2efSAndy Whitcroft}
156*4a0df2efSAndy Whitcroft
157*4a0df2efSAndy Whitcroftsub ctx_locate_comment {
158*4a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
159*4a0df2efSAndy Whitcroft
160*4a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
161*4a0df2efSAndy Whitcroft	my ($current_comment) = ($lines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
162*4a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
163*4a0df2efSAndy Whitcroft
164*4a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
165*4a0df2efSAndy Whitcroft	# comment.
166*4a0df2efSAndy Whitcroft	my $in_comment = 0;
167*4a0df2efSAndy Whitcroft	$current_comment = '';
168*4a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
169*4a0df2efSAndy Whitcroft		my $line = $lines[$linenr - 1];
170*4a0df2efSAndy Whitcroft		##warn "           $line\n";
171*4a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
172*4a0df2efSAndy Whitcroft			$in_comment = 1;
173*4a0df2efSAndy Whitcroft		}
174*4a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
175*4a0df2efSAndy Whitcroft			$in_comment = 1;
176*4a0df2efSAndy Whitcroft		}
177*4a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
178*4a0df2efSAndy Whitcroft			$current_comment = '';
179*4a0df2efSAndy Whitcroft		}
180*4a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
181*4a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
182*4a0df2efSAndy Whitcroft			$in_comment = 0;
183*4a0df2efSAndy Whitcroft		}
184*4a0df2efSAndy Whitcroft	}
185*4a0df2efSAndy Whitcroft
186*4a0df2efSAndy Whitcroft	chomp($current_comment);
187*4a0df2efSAndy Whitcroft	return($current_comment);
188*4a0df2efSAndy Whitcroft}
189*4a0df2efSAndy Whitcroftsub ctx_has_comment {
190*4a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
191*4a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
192*4a0df2efSAndy Whitcroft
193*4a0df2efSAndy Whitcroft	##print "LINE: $lines[$end_line - 1 ]\n";
194*4a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
195*4a0df2efSAndy Whitcroft
196*4a0df2efSAndy Whitcroft	return ($cmt ne '');
197*4a0df2efSAndy Whitcroft}
198*4a0df2efSAndy Whitcroft
1990a920b5bSAndy Whitcroftsub cat_vet {
2000a920b5bSAndy Whitcroft	my ($vet) = @_;
2010a920b5bSAndy Whitcroft
2020a920b5bSAndy Whitcroft	$vet =~ s/\t/^I/;
2030a920b5bSAndy Whitcroft	$vet =~ s/$/\$/;
2040a920b5bSAndy Whitcroft
2050a920b5bSAndy Whitcroft	return $vet;
2060a920b5bSAndy Whitcroft}
2070a920b5bSAndy Whitcroft
208*4a0df2efSAndy Whitcroftsub has_non_quoted {
209*4a0df2efSAndy Whitcroft	return ($_[0] =~ m{$_[1]} and $_[0] !~ m{\".*$_[1].*\"});
210*4a0df2efSAndy Whitcroft}
211*4a0df2efSAndy Whitcroft
2120a920b5bSAndy Whitcroftsub process {
2130a920b5bSAndy Whitcroft	my $filename = shift;
2140a920b5bSAndy Whitcroft	my @lines = @_;
2150a920b5bSAndy Whitcroft
2160a920b5bSAndy Whitcroft	my $linenr=0;
2170a920b5bSAndy Whitcroft	my $prevline="";
2180a920b5bSAndy Whitcroft	my $stashline="";
2190a920b5bSAndy Whitcroft
220*4a0df2efSAndy Whitcroft	my $length;
2210a920b5bSAndy Whitcroft	my $indent;
2220a920b5bSAndy Whitcroft	my $previndent=0;
2230a920b5bSAndy Whitcroft	my $stashindent=0;
2240a920b5bSAndy Whitcroft
2250a920b5bSAndy Whitcroft	my $clean = 1;
2260a920b5bSAndy Whitcroft	my $signoff = 0;
2270a920b5bSAndy Whitcroft	my $is_patch = 0;
2280a920b5bSAndy Whitcroft
2290a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
2300a920b5bSAndy Whitcroft	my $realfile = '';
2310a920b5bSAndy Whitcroft	my $realline = 0;
2320a920b5bSAndy Whitcroft	my $realcnt = 0;
2330a920b5bSAndy Whitcroft	my $here = '';
2340a920b5bSAndy Whitcroft	my $in_comment = 0;
2350a920b5bSAndy Whitcroft	my $first_line = 0;
2360a920b5bSAndy Whitcroft
2370a920b5bSAndy Whitcroft	foreach my $line (@lines) {
2380a920b5bSAndy Whitcroft		$linenr++;
2390a920b5bSAndy Whitcroft
2400a920b5bSAndy Whitcroft#extract the filename as it passes
2410a920b5bSAndy Whitcroft		if ($line=~/^\+\+\+\s+(\S+)/) {
2420a920b5bSAndy Whitcroft			$realfile=$1;
2430a920b5bSAndy Whitcroft			$in_comment = 0;
2440a920b5bSAndy Whitcroft			next;
2450a920b5bSAndy Whitcroft		}
2460a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2470a920b5bSAndy Whitcroft		if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
2480a920b5bSAndy Whitcroft			$is_patch = 1;
249*4a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
2500a920b5bSAndy Whitcroft			$in_comment = 0;
2510a920b5bSAndy Whitcroft			$realline=$1-1;
2520a920b5bSAndy Whitcroft			if (defined $2) {
2530a920b5bSAndy Whitcroft				$realcnt=$3+1;
2540a920b5bSAndy Whitcroft			} else {
2550a920b5bSAndy Whitcroft				$realcnt=1+1;
2560a920b5bSAndy Whitcroft			}
2570a920b5bSAndy Whitcroft			next;
2580a920b5bSAndy Whitcroft		}
2590a920b5bSAndy Whitcroft
260*4a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
261*4a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
262*4a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
263*4a0df2efSAndy Whitcroft		if ($line =~ /^( |\+|$)/) {
2640a920b5bSAndy Whitcroft			$realline++;
2650a920b5bSAndy Whitcroft			$realcnt-- if ($realcnt != 0);
2660a920b5bSAndy Whitcroft
2670a920b5bSAndy Whitcroft			# track any sort of multi-line comment.  Obviously if
2680a920b5bSAndy Whitcroft			# the added text or context do not include the whole
2690a920b5bSAndy Whitcroft			# comment we will not see it. Such is life.
2700a920b5bSAndy Whitcroft			#
2710a920b5bSAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2720a920b5bSAndy Whitcroft			# is the start of a diff block and this line starts
2730a920b5bSAndy Whitcroft			# ' *' then it is very likely a comment.
274*4a0df2efSAndy Whitcroft			if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
2750a920b5bSAndy Whitcroft				$in_comment = 1;
2760a920b5bSAndy Whitcroft			}
2770a920b5bSAndy Whitcroft			if ($line =~ m@/\*@) {
2780a920b5bSAndy Whitcroft				$in_comment = 1;
2790a920b5bSAndy Whitcroft			}
2800a920b5bSAndy Whitcroft			if ($line =~ m@\*/@) {
2810a920b5bSAndy Whitcroft				$in_comment = 0;
2820a920b5bSAndy Whitcroft			}
2830a920b5bSAndy Whitcroft
284*4a0df2efSAndy Whitcroft			# Measure the line length and indent.
285*4a0df2efSAndy Whitcroft			($length, $indent) = line_stats($line);
2860a920b5bSAndy Whitcroft
2870a920b5bSAndy Whitcroft			# Track the previous line.
2880a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
2890a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2900a920b5bSAndy Whitcroft		}
2910a920b5bSAndy Whitcroft
2920a920b5bSAndy Whitcroft#make up the handle for any error we report on this line
2930a920b5bSAndy Whitcroft		$here = "PATCH: $ARGV:$linenr:";
2940a920b5bSAndy Whitcroft		$here .= "\nFILE: $realfile:$realline:" if ($realcnt != 0);
2950a920b5bSAndy Whitcroft
2960a920b5bSAndy Whitcroft		my $herecurr = "$here\n$line\n\n";
2970a920b5bSAndy Whitcroft		my $hereprev = "$here\n$prevline\n$line\n\n";
2980a920b5bSAndy Whitcroft
2990a920b5bSAndy Whitcroft#check the patch for a signoff:
3000a920b5bSAndy Whitcroft		if ($line =~ /^\s*Signed-off-by:\s/) {
3010a920b5bSAndy Whitcroft			$signoff++;
3020a920b5bSAndy Whitcroft
3030a920b5bSAndy Whitcroft		} elsif ($line =~ /^\s*signed-off-by:/i) {
304*4a0df2efSAndy Whitcroft			# This is a signoff, if ugly, so do not double report.
305*4a0df2efSAndy Whitcroft			$signoff++;
3060a920b5bSAndy Whitcroft			if (!($line =~ /^\s*Signed-off-by:/)) {
3070a920b5bSAndy Whitcroft				print "use Signed-off-by:\n";
3080a920b5bSAndy Whitcroft				print "$herecurr";
3090a920b5bSAndy Whitcroft				$clean = 0;
3100a920b5bSAndy Whitcroft			}
3110a920b5bSAndy Whitcroft			if ($line =~ /^\s*signed-off-by:\S/i) {
3120a920b5bSAndy Whitcroft				print "need space after Signed-off-by:\n";
3130a920b5bSAndy Whitcroft				print "$herecurr";
3140a920b5bSAndy Whitcroft				$clean = 0;
3150a920b5bSAndy Whitcroft			}
3160a920b5bSAndy Whitcroft		}
3170a920b5bSAndy Whitcroft
3180a920b5bSAndy Whitcroft#ignore lines not being added
3190a920b5bSAndy Whitcroft		if ($line=~/^[^\+]/) {next;}
3200a920b5bSAndy Whitcroft
3210a920b5bSAndy Whitcroft# check we are in a valid source file *.[hcsS] if not then ignore this hunk
3220a920b5bSAndy Whitcroft		next if ($realfile !~ /\.[hcsS]$/);
3230a920b5bSAndy Whitcroft
3240a920b5bSAndy Whitcroft#trailing whitespace
3250a920b5bSAndy Whitcroft		if ($line=~/\S\s+$/) {
3260a920b5bSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($line) . "\n\n";
3270a920b5bSAndy Whitcroft			print "trailing whitespace\n";
3280a920b5bSAndy Whitcroft			print "$herevet";
3290a920b5bSAndy Whitcroft			$clean = 0;
3300a920b5bSAndy Whitcroft		}
3310a920b5bSAndy Whitcroft#80 column limit
332*4a0df2efSAndy Whitcroft		if (!($prevline=~/\/\*\*/) && $length > 80) {
3330a920b5bSAndy Whitcroft			print "line over 80 characters\n";
3340a920b5bSAndy Whitcroft			print "$herecurr";
3350a920b5bSAndy Whitcroft			$clean = 0;
3360a920b5bSAndy Whitcroft		}
3370a920b5bSAndy Whitcroft
3380a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk
3390a920b5bSAndy Whitcroft		next if ($realfile !~ /\.[hc]$/);
3400a920b5bSAndy Whitcroft
3410a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
3420a920b5bSAndy Whitcroft# more than 8 must use tabs.
3430a920b5bSAndy Whitcroft		if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s*        \s*/) {
3440a920b5bSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($line) . "\n\n";
3450a920b5bSAndy Whitcroft			print "use tabs not spaces\n";
3460a920b5bSAndy Whitcroft			print "$herevet";
3470a920b5bSAndy Whitcroft			$clean = 0;
3480a920b5bSAndy Whitcroft		}
3490a920b5bSAndy Whitcroft
3500a920b5bSAndy Whitcroft		#
3510a920b5bSAndy Whitcroft		# The rest of our checks refer specifically to C style
3520a920b5bSAndy Whitcroft		# only apply those _outside_ comments.
3530a920b5bSAndy Whitcroft		#
3540a920b5bSAndy Whitcroft		next if ($in_comment);
3550a920b5bSAndy Whitcroft
3560a920b5bSAndy Whitcroft# no C99 // comments
357*4a0df2efSAndy Whitcroft		if (has_non_quoted($line, '//')) {
3580a920b5bSAndy Whitcroft			print "do not use C99 // comments\n";
3590a920b5bSAndy Whitcroft			print "$herecurr";
3600a920b5bSAndy Whitcroft			$clean = 0;
3610a920b5bSAndy Whitcroft		}
3620a920b5bSAndy Whitcroft
3630a920b5bSAndy Whitcroft		# Remove comments from the line before processing.
3640a920b5bSAndy Whitcroft		$line =~ s@/\*.*\*/@@g;
3650a920b5bSAndy Whitcroft		$line =~ s@/\*.*@@;
3660a920b5bSAndy Whitcroft		$line =~ s@.*\*/@@;
3670a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
3680a920b5bSAndy Whitcroft
3690a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }.
3700a920b5bSAndy Whitcroft		if (($line =~ /EXPORT_SYMBOL.*\(.*\)/) ||
3710a920b5bSAndy Whitcroft		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\(.*\)/)) {
3720a920b5bSAndy Whitcroft			if (($prevline !~ /^}/) &&
3730a920b5bSAndy Whitcroft			   ($prevline !~ /^\+}/) &&
3740a920b5bSAndy Whitcroft			   ($prevline !~ /^ }/)) {
3750a920b5bSAndy Whitcroft				print "EXPORT_SYMBOL(func); should immediately follow its function\n";
3760a920b5bSAndy Whitcroft				print "$herecurr";
3770a920b5bSAndy Whitcroft				$clean = 0;
3780a920b5bSAndy Whitcroft			}
3790a920b5bSAndy Whitcroft		}
3800a920b5bSAndy Whitcroft
3810a920b5bSAndy Whitcroft		# check for static initialisers.
3820a920b5bSAndy Whitcroft		if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
3830a920b5bSAndy Whitcroft			print "do not initialise statics to 0 or NULL\n";
3840a920b5bSAndy Whitcroft			print "$herecurr";
3850a920b5bSAndy Whitcroft			$clean = 0;
3860a920b5bSAndy Whitcroft		}
3870a920b5bSAndy Whitcroft
3880a920b5bSAndy Whitcroft		# check for new typedefs.
3890a920b5bSAndy Whitcroft		if ($line=~/\s*typedef\s/) {
3900a920b5bSAndy Whitcroft			print "do not add new typedefs\n";
3910a920b5bSAndy Whitcroft			print "$herecurr";
3920a920b5bSAndy Whitcroft			$clean = 0;
3930a920b5bSAndy Whitcroft		}
3940a920b5bSAndy Whitcroft
3950a920b5bSAndy Whitcroft# * goes on variable not on type
3960a920b5bSAndy Whitcroft		if ($line=~/[A-Za-z\d_]+\* [A-Za-z\d_]+/) {
3970a920b5bSAndy Whitcroft			print "\"foo* bar\" should be \"foo *bar\"\n";
3980a920b5bSAndy Whitcroft			print "$herecurr";
3990a920b5bSAndy Whitcroft			$clean = 0;
4000a920b5bSAndy Whitcroft		}
4010a920b5bSAndy Whitcroft
4020a920b5bSAndy Whitcroft# # no BUG() or BUG_ON()
4030a920b5bSAndy Whitcroft# 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
4040a920b5bSAndy Whitcroft# 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
4050a920b5bSAndy Whitcroft# 			print "$herecurr";
4060a920b5bSAndy Whitcroft# 			$clean = 0;
4070a920b5bSAndy Whitcroft# 		}
4080a920b5bSAndy Whitcroft
4090a920b5bSAndy Whitcroft# printk should use KERN_* levels
4100a920b5bSAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)/) {
4110a920b5bSAndy Whitcroft			print "printk() should include KERN_ facility level\n";
4120a920b5bSAndy Whitcroft			print "$herecurr";
4130a920b5bSAndy Whitcroft			$clean = 0;
4140a920b5bSAndy Whitcroft		}
4150a920b5bSAndy Whitcroft
4160a920b5bSAndy Whitcroft#function brace can't be on same line, except for #defines of do while, or if closed on same line
4170a920b5bSAndy Whitcroft		if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and
4180a920b5bSAndy Whitcroft		    !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
4190a920b5bSAndy Whitcroft			print "braces following function declarations go on the next line\n";
4200a920b5bSAndy Whitcroft			print "$herecurr";
4210a920b5bSAndy Whitcroft			$clean = 0;
4220a920b5bSAndy Whitcroft		}
423*4a0df2efSAndy Whitcroft		# Note we expand the line with the leading + as the real
424*4a0df2efSAndy Whitcroft		# line will be displayed with the leading + and the tabs
425*4a0df2efSAndy Whitcroft		# will therefore also expand that way.
4260a920b5bSAndy Whitcroft		my $opline = $line;
427*4a0df2efSAndy Whitcroft		$opline = expand_tabs($opline);
4280a920b5bSAndy Whitcroft		$opline =~ s/^.//;
4290a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
4300a920b5bSAndy Whitcroft			# Check operator spacing.
4310a920b5bSAndy Whitcroft			my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
432*4a0df2efSAndy Whitcroft			my $off = 1;
4330a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
434*4a0df2efSAndy Whitcroft				$off += length($elements[$n]);
435*4a0df2efSAndy Whitcroft
436*4a0df2efSAndy Whitcroft				my $a = '';
437*4a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
438*4a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
439*4a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
440*4a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
441*4a0df2efSAndy Whitcroft				$a = 'E' if ($elements[$n] eq '' && $n == 0);
442*4a0df2efSAndy Whitcroft
4430a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
444*4a0df2efSAndy Whitcroft
445*4a0df2efSAndy Whitcroft				my $c = '';
4460a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
447*4a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
448*4a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
449*4a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
450*4a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
451*4a0df2efSAndy Whitcroft				} else {
452*4a0df2efSAndy Whitcroft					$c = 'E';
4530a920b5bSAndy Whitcroft				}
4540a920b5bSAndy Whitcroft
455*4a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
456*4a0df2efSAndy Whitcroft
457*4a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
458*4a0df2efSAndy Whitcroft
459*4a0df2efSAndy Whitcroft				my $ptr = (" " x $off) . "^";
460*4a0df2efSAndy Whitcroft				my $hereptr = "$here\n$line\n$ptr\n\n";
4610a920b5bSAndy Whitcroft
4620a920b5bSAndy Whitcroft				##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
4630a920b5bSAndy Whitcroft				# Skip things apparently in quotes.
4640a920b5bSAndy Whitcroft				next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
4650a920b5bSAndy Whitcroft
4660a920b5bSAndy Whitcroft				# We need ; as an operator.  // is a comment.
4670a920b5bSAndy Whitcroft				if ($op eq ';' or $op eq '//') {
4680a920b5bSAndy Whitcroft
4690a920b5bSAndy Whitcroft				# -> should have no spaces
4700a920b5bSAndy Whitcroft				} elsif ($op eq '->') {
471*4a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
4720a920b5bSAndy Whitcroft						print "no spaces around that '$op' $at\n";
473*4a0df2efSAndy Whitcroft						print "$hereptr";
4740a920b5bSAndy Whitcroft						$clean = 0;
4750a920b5bSAndy Whitcroft					}
4760a920b5bSAndy Whitcroft
4770a920b5bSAndy Whitcroft				# , must have a space on the right.
4780a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
479*4a0df2efSAndy Whitcroft					if ($ctx !~ /.xW|.xE/) {
4800a920b5bSAndy Whitcroft						print "need space after that '$op' $at\n";
481*4a0df2efSAndy Whitcroft						print "$hereptr";
4820a920b5bSAndy Whitcroft						$clean = 0;
4830a920b5bSAndy Whitcroft					}
4840a920b5bSAndy Whitcroft
4850a920b5bSAndy Whitcroft				# unary ! and unary ~ are allowed no space on the right
4860a920b5bSAndy Whitcroft				} elsif ($op eq '!' or $op eq '~') {
487*4a0df2efSAndy Whitcroft					if ($ctx !~ /[WOEB]x./) {
4880a920b5bSAndy Whitcroft						print "need space before that '$op' $at\n";
489*4a0df2efSAndy Whitcroft						print "$hereptr";
4900a920b5bSAndy Whitcroft						$clean = 0;
4910a920b5bSAndy Whitcroft					}
492*4a0df2efSAndy Whitcroft					if ($ctx =~ /.xW/) {
4930a920b5bSAndy Whitcroft						print "no space after that '$op' $at\n";
494*4a0df2efSAndy Whitcroft						print "$hereptr";
4950a920b5bSAndy Whitcroft						$clean = 0;
4960a920b5bSAndy Whitcroft					}
4970a920b5bSAndy Whitcroft
4980a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
4990a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
500*4a0df2efSAndy Whitcroft					if ($ctx !~ /[WOB]x[^W]|[^W]x[WOB]/) {
5010a920b5bSAndy Whitcroft						print "need space one side of that '$op' $at\n";
502*4a0df2efSAndy Whitcroft						print "$hereptr";
5030a920b5bSAndy Whitcroft						$clean = 0;
5040a920b5bSAndy Whitcroft					}
5050a920b5bSAndy Whitcroft
5060a920b5bSAndy Whitcroft				# & is both unary and binary
5070a920b5bSAndy Whitcroft				# unary:
5080a920b5bSAndy Whitcroft				# 	a &b
5090a920b5bSAndy Whitcroft				# binary (consistent spacing):
5100a920b5bSAndy Whitcroft				#	a&b		OK
5110a920b5bSAndy Whitcroft				#	a & b		OK
5120a920b5bSAndy Whitcroft				#
5130a920b5bSAndy Whitcroft				# boiling down to: if there is a space on the right then there
5140a920b5bSAndy Whitcroft				# should be one on the left.
5150a920b5bSAndy Whitcroft				#
5160a920b5bSAndy Whitcroft				# - is the same
5170a920b5bSAndy Whitcroft				#
5180a920b5bSAndy Whitcroft				# * is the same only adding:
5190a920b5bSAndy Whitcroft				# type:
5200a920b5bSAndy Whitcroft				# 	(foo *)
5210a920b5bSAndy Whitcroft				#	(foo **)
5220a920b5bSAndy Whitcroft				#
523*4a0df2efSAndy Whitcroft				} elsif ($op eq '&' or $op eq '-') {
524*4a0df2efSAndy Whitcroft					if ($ctx !~ /VxV|[EWB]x[WE]|[EWB]x[VO]/) {
5250a920b5bSAndy Whitcroft						print "need space before that '$op' $at\n";
526*4a0df2efSAndy Whitcroft						print "$hereptr";
527*4a0df2efSAndy Whitcroft						$clean = 0;
528*4a0df2efSAndy Whitcroft					}
529*4a0df2efSAndy Whitcroft
530*4a0df2efSAndy Whitcroft				} elsif ($op eq '*') {
531*4a0df2efSAndy Whitcroft					if ($ctx !~ /VxV|[EWB]x[WE]|[EWB]x[VO]|[EWO]x[OBV]/) {
532*4a0df2efSAndy Whitcroft						print "need space before that '$op' $at\n";
533*4a0df2efSAndy Whitcroft						print "$hereptr";
5340a920b5bSAndy Whitcroft						$clean = 0;
5350a920b5bSAndy Whitcroft					}
5360a920b5bSAndy Whitcroft
5370a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
5380a920b5bSAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
5390a920b5bSAndy Whitcroft					 $op eq '^' or $op eq '|')
5400a920b5bSAndy Whitcroft				{
541*4a0df2efSAndy Whitcroft					if ($ctx !~ /VxV|WxW|VxE|WxE/) {
5420a920b5bSAndy Whitcroft						print "need consistent spacing around '$op' $at\n";
543*4a0df2efSAndy Whitcroft						print "$hereptr";
5440a920b5bSAndy Whitcroft						$clean = 0;
5450a920b5bSAndy Whitcroft					}
5460a920b5bSAndy Whitcroft
5470a920b5bSAndy Whitcroft				# All the others need spaces both sides.
548*4a0df2efSAndy Whitcroft				} elsif ($ctx !~ /[EW]x[WE]/) {
5490a920b5bSAndy Whitcroft					print "need spaces around that '$op' $at\n";
550*4a0df2efSAndy Whitcroft					print "$hereptr";
5510a920b5bSAndy Whitcroft					$clean = 0;
5520a920b5bSAndy Whitcroft				}
553*4a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
5540a920b5bSAndy Whitcroft			}
5550a920b5bSAndy Whitcroft		}
5560a920b5bSAndy Whitcroft
5570a920b5bSAndy Whitcroft#need space before brace following if, while, etc
5580a920b5bSAndy Whitcroft		if ($line=~/\(.*\){/) {
5590a920b5bSAndy Whitcroft			print "need a space before the brace\n";
5600a920b5bSAndy Whitcroft			print "$herecurr";
5610a920b5bSAndy Whitcroft			$clean = 0;
5620a920b5bSAndy Whitcroft		}
5630a920b5bSAndy Whitcroft
5640a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
565*4a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
5660a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
5670a920b5bSAndy Whitcroft			print "labels should not be indented\n";
5680a920b5bSAndy Whitcroft			print "$herecurr";
5690a920b5bSAndy Whitcroft			$clean = 0;
5700a920b5bSAndy Whitcroft		}
5710a920b5bSAndy Whitcroft
5720a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
573*4a0df2efSAndy Whitcroft		if ($line=~/\b(if|while|for|switch)\(/) {
5740a920b5bSAndy Whitcroft			print "need a space before the open parenthesis\n";
5750a920b5bSAndy Whitcroft			print "$herecurr";
5760a920b5bSAndy Whitcroft			$clean = 0;
5770a920b5bSAndy Whitcroft		}
5780a920b5bSAndy Whitcroft
5790a920b5bSAndy Whitcroft# Check for illegal assignment in if conditional.
580*4a0df2efSAndy Whitcroft		if ($line=~/\b(if|while)\s*\(.*[^<>!=]=[^=].*\)/) {
581*4a0df2efSAndy Whitcroft			print "do not use assignment in condition\n";
5820a920b5bSAndy Whitcroft			print "$herecurr";
5830a920b5bSAndy Whitcroft			$clean = 0;
5840a920b5bSAndy Whitcroft		}
5850a920b5bSAndy Whitcroft
5860a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
5870a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
5880a920b5bSAndy Whitcroft		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
5890a920b5bSAndy Whitcroft						$previndent == $indent) {
5900a920b5bSAndy Whitcroft			print "else should follow close brace\n";
5910a920b5bSAndy Whitcroft			print "$hereprev";
5920a920b5bSAndy Whitcroft			$clean = 0;
5930a920b5bSAndy Whitcroft		}
5940a920b5bSAndy Whitcroft
595*4a0df2efSAndy Whitcroft		# Check for switch () and associated case and default
596*4a0df2efSAndy Whitcroft		# statements should be at the same indent.
597*4a0df2efSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
598*4a0df2efSAndy Whitcroft			my $err = '';
599*4a0df2efSAndy Whitcroft			my $sep = '';
600*4a0df2efSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
601*4a0df2efSAndy Whitcroft			shift(@ctx);
602*4a0df2efSAndy Whitcroft			for my $ctx (@ctx) {
603*4a0df2efSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
604*4a0df2efSAndy Whitcroft				if ($ctx =~ /\s*(case\s+|default:)/ &&
605*4a0df2efSAndy Whitcroft							$indent != $cindent) {
606*4a0df2efSAndy Whitcroft					$err .= "$sep$ctx\n";
607*4a0df2efSAndy Whitcroft					$sep = '';
608*4a0df2efSAndy Whitcroft				} else {
609*4a0df2efSAndy Whitcroft					$sep = "[...]\n";
610*4a0df2efSAndy Whitcroft				}
611*4a0df2efSAndy Whitcroft			}
612*4a0df2efSAndy Whitcroft			if ($err ne '') {
6130a920b5bSAndy Whitcroft				print "switch and case should be at the same indent\n";
614*4a0df2efSAndy Whitcroft				print "$here\n$line\n$err\n";
6150a920b5bSAndy Whitcroft				$clean = 0;
6160a920b5bSAndy Whitcroft			}
617*4a0df2efSAndy Whitcroft		}
6180a920b5bSAndy Whitcroft
6190a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new
6200a920b5bSAndy Whitcroft#		if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
6210a920b5bSAndy Whitcroft#		    print "No studly caps, use _\n";
6220a920b5bSAndy Whitcroft#		    print "$herecurr";
6230a920b5bSAndy Whitcroft#		    $clean = 0;
6240a920b5bSAndy Whitcroft#		}
6250a920b5bSAndy Whitcroft
6260a920b5bSAndy Whitcroft#no spaces allowed after \ in define
6270a920b5bSAndy Whitcroft		if ($line=~/\#define.*\\\s$/) {
6280a920b5bSAndy Whitcroft			print("Whitepspace after \\ makes next lines useless\n");
6290a920b5bSAndy Whitcroft			print "$herecurr";
6300a920b5bSAndy Whitcroft			$clean = 0;
6310a920b5bSAndy Whitcroft		}
6320a920b5bSAndy Whitcroft
6330a920b5bSAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available.
6340a920b5bSAndy Whitcroft		if ($tree && $line =~ qr|\s*\#\s*include\s*\<asm\/(.*)\.h\>|) {
6350a920b5bSAndy Whitcroft			my $checkfile = "include/linux/$1.h";
6360a920b5bSAndy Whitcroft			if (-f $checkfile) {
6370a920b5bSAndy Whitcroft				print "Use #include <linux/$1.h> instead of <asm/$1.h>\n";
6380a920b5bSAndy Whitcroft				print $herecurr;
6390a920b5bSAndy Whitcroft				$clean = 0;
6400a920b5bSAndy Whitcroft			}
6410a920b5bSAndy Whitcroft		}
6420a920b5bSAndy Whitcroft
6430a920b5bSAndy Whitcroft#if/while/etc brace do not go on next line, unless #defining a do while loop, or if that brace on the next line is for something else
644*4a0df2efSAndy Whitcroft		if ($prevline=~/\b(if|while|for|switch)\s*\(/) {
6450a920b5bSAndy Whitcroft			my @opened = $prevline=~/\(/g;
6460a920b5bSAndy Whitcroft			my @closed = $prevline=~/\)/g;
6470a920b5bSAndy Whitcroft			my $nr_line = $linenr;
6480a920b5bSAndy Whitcroft			my $remaining = $realcnt;
6490a920b5bSAndy Whitcroft			my $next_line = $line;
6500a920b5bSAndy Whitcroft			my $extra_lines = 0;
6510a920b5bSAndy Whitcroft			my $display_segment = $prevline;
6520a920b5bSAndy Whitcroft
653*4a0df2efSAndy Whitcroft			while ($remaining > 1 && scalar @opened > scalar @closed) {
6540a920b5bSAndy Whitcroft				$prevline .= $next_line;
6550a920b5bSAndy Whitcroft				$display_segment .= "\n" . $next_line;
6560a920b5bSAndy Whitcroft				$next_line = $lines[$nr_line];
6570a920b5bSAndy Whitcroft				$nr_line++;
6580a920b5bSAndy Whitcroft				$remaining--;
6590a920b5bSAndy Whitcroft
6600a920b5bSAndy Whitcroft				@opened = $prevline=~/\(/g;
6610a920b5bSAndy Whitcroft				@closed = $prevline=~/\)/g;
6620a920b5bSAndy Whitcroft			}
6630a920b5bSAndy Whitcroft
664*4a0df2efSAndy Whitcroft			if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and
665*4a0df2efSAndy Whitcroft			   !($next_line=~/\b(if|while|for)/) and !($next_line=~/\#define.*do.*while/)) {
6660a920b5bSAndy Whitcroft				print "That { should be on the previous line\n";
667*4a0df2efSAndy Whitcroft				print "$here\n$display_segment\n$next_line\n\n";
6680a920b5bSAndy Whitcroft				$clean = 0;
6690a920b5bSAndy Whitcroft			}
6700a920b5bSAndy Whitcroft		}
6710a920b5bSAndy Whitcroft
6720a920b5bSAndy Whitcroft#multiline macros should be enclosed in a do while loop
6730a920b5bSAndy Whitcroft		if (($prevline=~/\#define.*\\/) and !($prevline=~/do\s+{/) and
6740a920b5bSAndy Whitcroft		   !($prevline=~/\(\{/) and ($line=~/;\s*\\/) and
6750a920b5bSAndy Whitcroft		   !($line=~/do.*{/) and !($line=~/\(\{/)) {
6760a920b5bSAndy Whitcroft			print "Macros with multiple statements should be enclosed in a do - while loop\n";
6770a920b5bSAndy Whitcroft			print "$hereprev";
6780a920b5bSAndy Whitcroft			$clean = 0;
6790a920b5bSAndy Whitcroft		}
6800a920b5bSAndy Whitcroft
6810a920b5bSAndy Whitcroft# don't include deprecated include files
682*4a0df2efSAndy Whitcroft		for my $inc (@dep_includes) {
6830a920b5bSAndy Whitcroft			if ($line =~ m@\#\s*include\s*\<$inc>@) {
6840a920b5bSAndy Whitcroft				print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n";
6850a920b5bSAndy Whitcroft				print "$herecurr";
6860a920b5bSAndy Whitcroft				$clean = 0;
6870a920b5bSAndy Whitcroft			}
6880a920b5bSAndy Whitcroft		}
6890a920b5bSAndy Whitcroft
690*4a0df2efSAndy Whitcroft# don't use deprecated functions
691*4a0df2efSAndy Whitcroft		for my $func (@dep_functions) {
692*4a0df2efSAndy Whitcroft			if (has_non_quoted($line, '\b' . $func . '\b')) {
693*4a0df2efSAndy Whitcroft				print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n";
694*4a0df2efSAndy Whitcroft				print "$herecurr";
695*4a0df2efSAndy Whitcroft				$clean = 0;
696*4a0df2efSAndy Whitcroft			}
697*4a0df2efSAndy Whitcroft		}
698*4a0df2efSAndy Whitcroft
699*4a0df2efSAndy Whitcroft# no volatiles please
700*4a0df2efSAndy Whitcroft 		if (has_non_quoted($line, '\bvolatile\b')) {
701*4a0df2efSAndy Whitcroft			print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n";
702*4a0df2efSAndy Whitcroft			print "$herecurr";
703*4a0df2efSAndy Whitcroft			$clean = 0;
704*4a0df2efSAndy Whitcroft		}
705*4a0df2efSAndy Whitcroft
706*4a0df2efSAndy Whitcroft# warn about #ifdefs in C files
707*4a0df2efSAndy Whitcroft		if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
708*4a0df2efSAndy Whitcroft			print "#ifdef in C files should be avoided\n";
709*4a0df2efSAndy Whitcroft			print "$herecurr";
710*4a0df2efSAndy Whitcroft			$clean = 0;
711*4a0df2efSAndy Whitcroft		}
712*4a0df2efSAndy Whitcroft
713*4a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
714*4a0df2efSAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
715*4a0df2efSAndy Whitcroft			my $which = $1;
716*4a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
717*4a0df2efSAndy Whitcroft				print "$1 definition without comment\n";
718*4a0df2efSAndy Whitcroft				print "$herecurr";
719*4a0df2efSAndy Whitcroft				$clean = 0;
720*4a0df2efSAndy Whitcroft			}
721*4a0df2efSAndy Whitcroft		}
722*4a0df2efSAndy Whitcroft# check for memory barriers without a comment.
723*4a0df2efSAndy Whitcroft		if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
724*4a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
725*4a0df2efSAndy Whitcroft				print "memory barrier without comment\n";
726*4a0df2efSAndy Whitcroft				print "$herecurr";
727*4a0df2efSAndy Whitcroft				$clean = 0;
728*4a0df2efSAndy Whitcroft			}
729*4a0df2efSAndy Whitcroft		}
730*4a0df2efSAndy Whitcroft# check of hardware specific defines
731*4a0df2efSAndy Whitcroft		if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
732*4a0df2efSAndy Whitcroft			print "architecture specific defines should be avoided\n";
7330a920b5bSAndy Whitcroft			print "$herecurr";
7340a920b5bSAndy Whitcroft			$clean = 0;
7350a920b5bSAndy Whitcroft		}
7360a920b5bSAndy Whitcroft	}
7370a920b5bSAndy Whitcroft
7380a920b5bSAndy Whitcroft	if ($chk_patch && !$is_patch) {
7390a920b5bSAndy Whitcroft		$clean = 0;
7400a920b5bSAndy Whitcroft		print "Does not appear to be a unified-diff format patch\n";
7410a920b5bSAndy Whitcroft	}
7420a920b5bSAndy Whitcroft	if ($is_patch && $chk_signoff && $signoff == 0) {
7430a920b5bSAndy Whitcroft		$clean = 0;
7440a920b5bSAndy Whitcroft		print "Missing Signed-off-by: line(s)\n";
7450a920b5bSAndy Whitcroft	}
7460a920b5bSAndy Whitcroft
7470a920b5bSAndy Whitcroft	if ($clean == 1 && $quiet == 0) {
7480a920b5bSAndy Whitcroft		print "Your patch has no obvious style problems and is ready for submission.\n"
7490a920b5bSAndy Whitcroft	}
7500a920b5bSAndy Whitcroft	if ($clean == 0 && $quiet == 0) {
7510a920b5bSAndy Whitcroft		print "Your patch has style problems, please review.  If any of these errors\n";
7520a920b5bSAndy Whitcroft		print "are false positives report them to the maintainer, see\n";
7530a920b5bSAndy Whitcroft		print "CHECKPATCH in MAINTAINERS.\n";
7540a920b5bSAndy Whitcroft	}
7550a920b5bSAndy Whitcroft	return $clean;
7560a920b5bSAndy Whitcroft}
757