blob: dae7d30dca0f1886b450311ee0e3c959fc3d40da [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
2# (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004# (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc)
5# Licensed under the terms of the GNU GPL License version 2
6
7use strict;
8
9my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070010$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070011
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -070012my $V = '0.09';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
14use Getopt::Long qw(:config no_auto_abbrev);
15
16my $quiet = 0;
17my $tree = 1;
18my $chk_signoff = 1;
19my $chk_patch = 1;
Andy Whitcroft653d4872007-06-23 17:16:34 -070020my $tst_type = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070021GetOptions(
22 'q|quiet' => \$quiet,
23 'tree!' => \$tree,
24 'signoff!' => \$chk_signoff,
25 'patch!' => \$chk_patch,
Andy Whitcroft653d4872007-06-23 17:16:34 -070026 'test-type!' => \$tst_type,
Andy Whitcroft0a920b52007-06-01 00:46:48 -070027) or exit;
28
29my $exit = 0;
30
31if ($#ARGV < 0) {
Andy Whitcroft00df3442007-06-08 13:47:06 -070032 print "usage: $P [options] patchfile\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -070033 print "version: $V\n";
34 print "options: -q => quiet\n";
35 print " --no-tree => run without a kernel tree\n";
36 exit(1);
37}
38
39if ($tree && !top_of_kernel_tree()) {
40 print "Must be run from the top-level dir. of a kernel tree\n";
41 exit(2);
42}
43
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -070044my @dep_includes = ();
45my @dep_functions = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -070046my $removal = 'Documentation/feature-removal-schedule.txt';
47if ($tree && -f $removal) {
48 open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n";
49 while (<REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -070050 if (/^Check:\s+(.*\S)/) {
51 for my $entry (split(/[, ]+/, $1)) {
52 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -070053 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -070054
Andy Whitcroftf0a594c2007-07-19 01:48:34 -070055 } elsif ($entry !~ m@/@) {
56 push(@dep_functions, $entry);
57 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -070058 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -070059 }
60 }
61}
62
Andy Whitcroft00df3442007-06-08 13:47:06 -070063my @rawlines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -070064while (<>) {
65 chomp;
Andy Whitcroft00df3442007-06-08 13:47:06 -070066 push(@rawlines, $_);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070067 if (eof(ARGV)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -070068 if (!process($ARGV, @rawlines)) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -070069 $exit = 1;
70 }
Andy Whitcroft00df3442007-06-08 13:47:06 -070071 @rawlines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -070072 }
73}
74
75exit($exit);
76
77sub top_of_kernel_tree {
78 if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") &&
79 (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") &&
80 (-d "Documentation") && (-d "arch") && (-d "include") &&
81 (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") &&
82 (-d "kernel") && (-d "lib") && (-d "scripts")) {
83 return 1;
84 }
85 return 0;
86}
87
88sub expand_tabs {
89 my ($str) = @_;
90
91 my $res = '';
92 my $n = 0;
93 for my $c (split(//, $str)) {
94 if ($c eq "\t") {
95 $res .= ' ';
96 $n++;
97 for (; ($n % 8) != 0; $n++) {
98 $res .= ' ';
99 }
100 next;
101 }
102 $res .= $c;
103 $n++;
104 }
105
106 return $res;
107}
108
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700109sub line_stats {
110 my ($line) = @_;
111
112 # Drop the diff line leader and expand tabs
113 $line =~ s/^.//;
114 $line = expand_tabs($line);
115
116 # Pick the indent from the front of the line.
117 my ($white) = ($line =~ /^(\s*)/);
118
119 return (length($line), length($white));
120}
121
Andy Whitcroft00df3442007-06-08 13:47:06 -0700122sub sanitise_line {
123 my ($line) = @_;
124
125 my $res = '';
126 my $l = '';
127
128 my $quote = '';
129
130 foreach my $c (split(//, $line)) {
131 if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
132 if ($quote eq '') {
133 $quote = $c;
134 $res .= $c;
135 $l = $c;
136 next;
137 } elsif ($quote eq $c) {
138 $quote = '';
139 }
140 }
141 if ($quote && $c ne "\t") {
142 $res .= "X";
143 } else {
144 $res .= $c;
145 }
146
147 $l = $c;
148 }
149
150 return $res;
151}
152
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700153sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700154 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700155 my $line;
156 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700157 my $blk = '';
158 my @o;
159 my @c;
160 my @res = ();
161
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700162 my $level = 0;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700163 for ($line = $start; $remain > 0; $line++) {
164 next if ($rawlines[$line] =~ /^-/);
165 $remain--;
166
167 $blk .= $rawlines[$line];
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700168 foreach my $c (split(//, $rawlines[$line])) {
169 ##print "C<$c>L<$level><$open$close>O<$off>\n";
170 if ($off > 0) {
171 $off--;
172 next;
173 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700174
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700175 if ($c eq $close && $level > 0) {
176 $level--;
177 last if ($level == 0);
178 } elsif ($c eq $open) {
179 $level++;
180 }
181 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700182
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700183 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700184 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700185 }
186
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700187 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700188 }
189
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700190 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700191}
192sub ctx_block_outer {
193 my ($linenr, $remain) = @_;
194
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700195 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
196 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700197}
198sub ctx_block {
199 my ($linenr, $remain) = @_;
200
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700201 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
202 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700203}
204sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700205 my ($linenr, $remain, $off) = @_;
206
207 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
208 return @r;
209}
210sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700211 my ($linenr, $remain) = @_;
212
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700213 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700214}
215
216sub ctx_locate_comment {
217 my ($first_line, $end_line) = @_;
218
219 # Catch a comment on the end of the line itself.
Andy Whitcroft00df3442007-06-08 13:47:06 -0700220 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700221 return $current_comment if (defined $current_comment);
222
223 # Look through the context and try and figure out if there is a
224 # comment.
225 my $in_comment = 0;
226 $current_comment = '';
227 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700228 my $line = $rawlines[$linenr - 1];
229 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700230 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
231 $in_comment = 1;
232 }
233 if ($line =~ m@/\*@) {
234 $in_comment = 1;
235 }
236 if (!$in_comment && $current_comment ne '') {
237 $current_comment = '';
238 }
239 $current_comment .= $line . "\n" if ($in_comment);
240 if ($line =~ m@\*/@) {
241 $in_comment = 0;
242 }
243 }
244
245 chomp($current_comment);
246 return($current_comment);
247}
248sub ctx_has_comment {
249 my ($first_line, $end_line) = @_;
250 my $cmt = ctx_locate_comment($first_line, $end_line);
251
Andy Whitcroft00df3442007-06-08 13:47:06 -0700252 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700253 ##print "CMMT: $cmt\n";
254
255 return ($cmt ne '');
256}
257
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700258sub cat_vet {
259 my ($vet) = @_;
260
261 $vet =~ s/\t/^I/;
262 $vet =~ s/$/\$/;
263
264 return $vet;
265}
266
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700267my @report = ();
268sub report {
269 push(@report, $_[0]);
270}
271sub report_dump {
272 @report;
273}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700274sub ERROR {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700275 report("ERROR: $_[0]\n");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700276 our $clean = 0;
277}
278sub WARN {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700279 report("WARNING: $_[0]\n");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700280 our $clean = 0;
281}
282sub CHK {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700283 report("CHECK: $_[0]\n");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700284 our $clean = 0;
285}
286
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700287sub process {
288 my $filename = shift;
289 my @lines = @_;
290
291 my $linenr=0;
292 my $prevline="";
293 my $stashline="";
294
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700295 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700296 my $indent;
297 my $previndent=0;
298 my $stashindent=0;
299
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700300 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700301 my $signoff = 0;
302 my $is_patch = 0;
303
304 # Trace the real file/line as we go.
305 my $realfile = '';
306 my $realline = 0;
307 my $realcnt = 0;
308 my $here = '';
309 my $in_comment = 0;
310 my $first_line = 0;
311
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700312 my $Ident = qr{[A-Za-z\d_]+};
313 my $Storage = qr{extern|static};
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700314 my $Sparse = qr{__user|__kernel|__force|__iomem|__must_check|__init_refok};
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700315 my $NonptrType = qr{
316 \b
317 (?:const\s+)?
318 (?:unsigned\s+)?
319 (?:
320 void|
321 char|
322 short|
323 int|
324 long|
325 unsigned|
326 float|
327 double|
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700328 bool|
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700329 long\s+int|
330 long\s+long|
331 long\s+long\s+int|
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700332 u8|u16|u32|u64|
333 s8|s16|s32|s64|
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700334 struct\s+$Ident|
335 union\s+$Ident|
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700336 enum\s+$Ident|
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700337 ${Ident}_t
338 )
339 (?:\s+$Sparse)*
340 \b
341 }x;
342 my $Type = qr{
343 \b$NonptrType\b
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700344 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
345 (?:\s+$Sparse)*
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700346 }x;
347 my $Declare = qr{(?:$Storage\s+)?$Type};
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700348 my $Attribute = qr{const|__read_mostly|__init|__initdata|__meminit};
349
350 my $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
351 my $Lval = qr{$Ident(?:$Member)*};
Andy Whitcroft653d4872007-06-23 17:16:34 -0700352
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700353 # Pre-scan the patch looking for any __setup documentation.
354 my @setup_docs = ();
355 my $setup_docs = 0;
356 foreach my $line (@lines) {
357 if ($line=~/^\+\+\+\s+(\S+)/) {
358 $setup_docs = 0;
359 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
360 $setup_docs = 1;
361 }
362 next;
363 }
364
365 if ($setup_docs && $line =~ /^\+/) {
366 push(@setup_docs, $line);
367 }
368 }
369
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700370 foreach my $line (@lines) {
371 $linenr++;
372
Andy Whitcroft653d4872007-06-23 17:16:34 -0700373 my $rawline = $line;
374
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700375#extract the filename as it passes
376 if ($line=~/^\+\+\+\s+(\S+)/) {
377 $realfile=$1;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700378 $realfile =~ s@^[^/]*/@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700379 $in_comment = 0;
380 next;
381 }
382#extract the line range in the file after the patch is applied
383 if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
384 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700385 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700386 $in_comment = 0;
387 $realline=$1-1;
388 if (defined $2) {
389 $realcnt=$3+1;
390 } else {
391 $realcnt=1+1;
392 }
393 next;
394 }
395
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700396# track the line number as we move through the hunk, note that
397# new versions of GNU diff omit the leading space on completely
398# blank context lines so we need to count that too.
399 if ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700400 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700401 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700402
403 # track any sort of multi-line comment. Obviously if
404 # the added text or context do not include the whole
405 # comment we will not see it. Such is life.
406 #
407 # Guestimate if this is a continuing comment. If this
408 # is the start of a diff block and this line starts
409 # ' *' then it is very likely a comment.
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700410 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700411 $in_comment = 1;
412 }
413 if ($line =~ m@/\*@) {
414 $in_comment = 1;
415 }
416 if ($line =~ m@\*/@) {
417 $in_comment = 0;
418 }
419
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700420 # Measure the line length and indent.
421 ($length, $indent) = line_stats($line);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700422
423 # Track the previous line.
424 ($prevline, $stashline) = ($stashline, $line);
425 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700426 } elsif ($realcnt == 1) {
427 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700428 }
429
430#make up the handle for any error we report on this line
Randy Dunlap389834b2007-06-08 13:47:03 -0700431 $here = "#$linenr: ";
432 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700433
Andy Whitcroft00df3442007-06-08 13:47:06 -0700434 my $hereline = "$here\n$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700435 my $herecurr = "$here\n$line\n";
436 my $hereprev = "$here\n$prevline\n$line\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700437
438#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700439 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700440 # This is a signoff, if ugly, so do not double report.
441 $signoff++;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700442 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700443 WARN("Signed-off-by: is the preferred form\n" .
444 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700445 }
446 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700447 WARN("need space after Signed-off-by:\n" .
448 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700449 }
450 }
451
Andy Whitcroft00df3442007-06-08 13:47:06 -0700452# Check for wrappage within a valid hunk of the file
453 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700454 ERROR("patch seems to be corrupt (line wrapped?)\n" .
455 $herecurr);
456 }
457
458# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
459 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
460 !($line =~ m/^(
461 [\x09\x0A\x0D\x20-\x7E] # ASCII
462 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
463 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
464 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
465 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
466 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
467 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
468 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
469 )*$/x )) {
470 ERROR("Invalid UTF-8\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700471 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700472
Andy Whitcroft00df3442007-06-08 13:47:06 -0700473#ignore lines being removed
474 if ($line=~/^-/) {next;}
475
476# check we are in a valid source file if not then ignore this hunk
477 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700478
479#trailing whitespace
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700480 if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700481 my $herevet = "$here\n" . cat_vet($line) . "\n";
482 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700483 }
484#80 column limit
Andy Whitcroft00df3442007-06-08 13:47:06 -0700485 if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700486 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700487 }
488
489# check we are in a valid source file *.[hc] if not then ignore this hunk
490 next if ($realfile !~ /\.[hc]$/);
491
492# at the beginning of a line any tabs must come first and anything
493# more than 8 must use tabs.
494 if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700495 my $herevet = "$here\n" . cat_vet($line) . "\n";
496 ERROR("use tabs not spaces\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700497 }
498
Andy Whitcroft653d4872007-06-23 17:16:34 -0700499# Remove comments from the line before processing.
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700500 my $comment_edge = ($line =~ s@/\*.*\*/@@g) +
501 ($line =~ s@/\*.*@@) +
502 ($line =~ s@^(.).*\*/@$1@);
503
504# The rest of our checks refer specifically to C style
505# only apply those _outside_ comments. Only skip
506# lines in the middle of comments.
507 next if (!$comment_edge && $in_comment);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700508
Andy Whitcroft653d4872007-06-23 17:16:34 -0700509# Standardise the strings and chars within the input to simplify matching.
510 $line = sanitise_line($line);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700511
Andy Whitcroft653d4872007-06-23 17:16:34 -0700512#
513# Checks which may be anchored in the context.
514#
515
516# Check for switch () and associated case and default
517# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -0700518 if ($line=~/\bswitch\s*\(.*\)/) {
519 my $err = '';
520 my $sep = '';
521 my @ctx = ctx_block_outer($linenr, $realcnt);
522 shift(@ctx);
523 for my $ctx (@ctx) {
524 my ($clen, $cindent) = line_stats($ctx);
525 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
526 $indent != $cindent) {
527 $err .= "$sep$ctx\n";
528 $sep = '';
529 } else {
530 $sep = "[...]\n";
531 }
532 }
533 if ($err ne '') {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700534 ERROR("switch and case should be at the same indent\n$hereline\n$err\n");
535 }
536 }
537
538# if/while/etc brace do not go on next line, unless defining a do while loop,
539# or if that brace on the next line is for something else
540 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700541 my @ctx = ctx_statement($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700542 my $ctx_ln = $linenr + $#ctx + 1;
543 my $ctx_cnt = $realcnt - $#ctx - 1;
544 my $ctx = join("\n", @ctx);
545
546 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
547 $ctx_ln++;
548 $ctx_cnt--;
549 }
550 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
551
552 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700553 ERROR("That open brace { should be on the previous line\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700554 "$here\n$ctx\n$lines[$ctx_ln - 1]");
Andy Whitcroft00df3442007-06-08 13:47:06 -0700555 }
556 }
557
558#ignore lines not being added
559 if ($line=~/^[^\+]/) {next;}
560
Andy Whitcroft653d4872007-06-23 17:16:34 -0700561# TEST: allow direct testing of the type matcher.
562 if ($tst_type && $line =~ /^.$Declare$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700563 ERROR("TEST: is type $Declare\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -0700564 next;
565 }
566
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700567# check for initialisation to aggregates open brace on the next line
568 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
569 $line =~ /^.\s*{/) {
570 ERROR("That open brace { should be on the previous line\n" . $hereprev);
571 }
572
Andy Whitcroft653d4872007-06-23 17:16:34 -0700573#
574# Checks which are anchored on the added line.
575#
576
577# check for malformed paths in #include statements (uses RAW line)
578 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
579 my $path = $1;
580 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700581 ERROR("malformed #include filename\n" .
582 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -0700583 }
584 # Sanitise this special form of string.
585 $path = 'X' x length($path);
586 $line =~ s{\<.*\>}{<$path>};
587 }
Andy Whitcroft00df3442007-06-08 13:47:06 -0700588
589# no C99 // comments
590 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700591 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700592 }
593 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700594 $line =~ s@//.*@@;
595
596#EXPORT_SYMBOL should immediately follow its function closing }.
Andy Whitcroft653d4872007-06-23 17:16:34 -0700597 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
598 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
599 my $name = $1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700600 if (($prevline !~ /^}/) &&
601 ($prevline !~ /^\+}/) &&
Andy Whitcroft653d4872007-06-23 17:16:34 -0700602 ($prevline !~ /^ }/) &&
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700603 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700604 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700605 }
606 }
607
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700608# check for external initialisers.
609 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
610 ERROR("do not initialise externals to 0 or NULL\n" .
611 $herecurr);
612 }
Andy Whitcroft653d4872007-06-23 17:16:34 -0700613# check for static initialisers.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700614 if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700615 ERROR("do not initialise statics to 0 or NULL\n" .
616 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700617 }
618
Andy Whitcroft653d4872007-06-23 17:16:34 -0700619# check for new typedefs, only function parameters and sparse annotations
620# make sense.
621 if ($line =~ /\btypedef\s/ &&
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700622 $line !~ /\btypedef\s+$Type\s+\(\s*\*$Ident\s*\)\s*\(/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -0700623 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700624 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700625 }
626
627# * goes on variable not on type
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700628 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700629 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
630 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700631
632 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700633 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
634 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700635
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700636 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+$Attribute)?\s+[A-Za-z\d_]+}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700637 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
638 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700639
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700640 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+$Attribute)\s+[A-Za-z\d_]+}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700641 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
642 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700643 }
644
645# # no BUG() or BUG_ON()
646# if ($line =~ /\b(BUG|BUG_ON)\b/) {
647# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
648# print "$herecurr";
649# $clean = 0;
650# }
651
Andy Whitcroft00df3442007-06-08 13:47:06 -0700652# printk should use KERN_* levels. Note that follow on printk's on the
653# same line do not need a level, so we use the current block context
654# to try and find and validate the current printk. In summary the current
655# printk includes all preceeding printk's which have no newline on the end.
656# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700657 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700658 my $ok = 0;
659 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
660 #print "CHECK<$lines[$ln - 1]\n";
661 # we have a preceeding printk if it ends
662 # with "\n" ignore it, else it is to blame
663 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
664 if ($rawlines[$ln - 1] !~ m{\\n"}) {
665 $ok = 1;
666 }
667 last;
668 }
669 }
670 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700671 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700672 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700673 }
674
Andy Whitcroft653d4872007-06-23 17:16:34 -0700675# function brace can't be on same line, except for #defines of do while,
676# or if closed on same line
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700677 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700678 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700679 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700680 }
Andy Whitcroft653d4872007-06-23 17:16:34 -0700681
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700682# check for spaces between functions and their parentheses.
683 if ($line =~ /($Ident)\s+\(/ &&
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700684 $1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright)$/ &&
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700685 $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700686 WARN("no space between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700687 }
Andy Whitcroft653d4872007-06-23 17:16:34 -0700688# Check operator spacing.
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700689 # Note we expand the line with the leading + as the real
690 # line will be displayed with the leading + and the tabs
691 # will therefore also expand that way.
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700692 my $opline = $line;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700693 $opline = expand_tabs($opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700694 $opline =~ s/^./ /;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700695 if (!($line=~/\#\s*include/)) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700696 my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|=>|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700697 my $off = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700698 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700699 $off += length($elements[$n]);
700
701 my $a = '';
702 $a = 'V' if ($elements[$n] ne '');
703 $a = 'W' if ($elements[$n] =~ /\s$/);
704 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
705 $a = 'O' if ($elements[$n] eq '');
706 $a = 'E' if ($elements[$n] eq '' && $n == 0);
707
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700708 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700709
710 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700711 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700712 $c = 'V' if ($elements[$n + 2] ne '');
713 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
714 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
715 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700716 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700717 } else {
718 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700719 }
720
Andy Whitcroft00df3442007-06-08 13:47:06 -0700721 # Pick up the preceeding and succeeding characters.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700722 my $ca = substr($opline, 0, $off);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700723 my $cc = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -0700724 if (length($opline) >= ($off + length($elements[$n + 1]))) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700725 $cc = substr($opline, $off + length($elements[$n + 1]));
Andy Whitcroft00df3442007-06-08 13:47:06 -0700726 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700727 my $cb = "$ca$;$cc";
Andy Whitcroft00df3442007-06-08 13:47:06 -0700728
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700729 my $ctx = "${a}x${c}";
730
731 my $at = "(ctx:$ctx)";
732
733 my $ptr = (" " x $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700734 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700735
736 ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700737
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700738 # ; should have either the end of line or a space or \ after it
739 if ($op eq ';') {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700740 if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
741 $cc !~ /^;/) {
742 ERROR("need space after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700743 }
744
745 # // is a comment
746 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700747
748 # -> should have no spaces
749 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700750 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700751 ERROR("no spaces around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700752 }
753
754 # , must have a space on the right.
755 } elsif ($op eq ',') {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700756 if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700757 ERROR("need space after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700758 }
759
760 # unary ! and unary ~ are allowed no space on the right
761 } elsif ($op eq '!' or $op eq '~') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700762 if ($ctx !~ /[WOEB]x./) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700763 ERROR("need space before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700764 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700765 if ($ctx =~ /.xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700766 ERROR("no space after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700767 }
768
769 # unary ++ and unary -- are allowed no space on one side.
770 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700771 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700772 ERROR("need space one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700773 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700774 if ($ctx =~ /Wx./ && $cc =~ /^;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700775 ERROR("no space before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -0700776 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700777
778 # & is both unary and binary
779 # unary:
780 # a &b
781 # binary (consistent spacing):
782 # a&b OK
783 # a & b OK
784 #
785 # boiling down to: if there is a space on the right then there
786 # should be one on the left.
787 #
788 # - is the same
789 #
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700790 } elsif ($op eq '&' or $op eq '-') {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700791 if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700792 ERROR("need space before that '$op' $at\n" . $hereptr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700793 }
794
Andy Whitcroft00df3442007-06-08 13:47:06 -0700795 # * is the same as & only adding:
796 # type:
797 # (foo *)
798 # (foo **)
799 #
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700800 } elsif ($op eq '*') {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700801 if ($ca !~ /$Type$/ && $cb !~ /(\*$;|$;\*)/ &&
802 $ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
803 ERROR("need space before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700804 }
805
806 # << and >> may either have or not have spaces both sides
807 } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
808 $op eq '^' or $op eq '|')
809 {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700810 if ($ctx !~ /VxV|WxW|VxE|WxE/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700811 ERROR("need consistent spacing around '$op' $at\n" .
812 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700813 }
814
815 # All the others need spaces both sides.
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700816 } elsif ($ctx !~ /[EW]x[WE]/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700817 # Ignore email addresses <foo@bar>
818 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
819 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
820 ERROR("need spaces around that '$op' $at\n" . $hereptr);
821 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700822 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700823 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700824 }
825 }
826
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700827# check for multiple assignments
828 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
829 WARN("multiple assignments should be avoided\n" . $herecurr);
830 }
831
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700832## # check for multiple declarations, allowing for a function declaration
833## # continuation.
834## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
835## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
836##
837## # Remove any bracketed sections to ensure we do not
838## # falsly report the parameters of functions.
839## my $ln = $line;
840## while ($ln =~ s/\([^\(\)]*\)//g) {
841## }
842## if ($ln =~ /,/) {
843## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
844## }
845## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700846
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700847#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700848 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
849 $line =~ /do{/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700850 ERROR("need a space before the open brace '{'\n" . $herecurr);
851 }
852
853# closing brace should have a space following it when it has anything
854# on the line
855 if ($line =~ /}(?!(?:,|;|\)))\S/) {
856 ERROR("need a space after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700857 }
858
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700859# check spacing on square brackets
860 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
861 ERROR("no space after that open square bracket '['\n" . $herecurr);
862 }
863 if ($line =~ /\s\]/) {
864 ERROR("no space before that close square bracket ']'\n" . $herecurr);
865 }
866
867# check spacing on paretheses
868 if ($line =~ /\(\s/ && $line !~ /\(\s*$/) {
869 ERROR("no space after that open parenthesis '('\n" . $herecurr);
870 }
871 if ($line =~ /\s\)/) {
872 ERROR("no space before that close parenthesis ')'\n" . $herecurr);
873 }
874
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700875#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700876 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700877 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700878 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700879 }
880
881# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700882 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700883 ERROR("need a space before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700884 }
885
886# Check for illegal assignment in if conditional.
Andy Whitcroft653d4872007-06-23 17:16:34 -0700887 if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700888 #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700889 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700890 }
891
892 # Check for }<nl>else {, these must be at the same
893 # indent level to be relevant to each other.
894 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
895 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700896 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700897 }
898
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700899#studly caps, commented out until figure out how to distinguish between use of existing and adding new
900# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
901# print "No studly caps, use _\n";
902# print "$herecurr";
903# $clean = 0;
904# }
905
906#no spaces allowed after \ in define
907 if ($line=~/\#define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700908 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700909 }
910
Andy Whitcroft653d4872007-06-23 17:16:34 -0700911#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
912 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700913 my $checkfile = "include/linux/$1.h";
914 if (-f $checkfile) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700915 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
916 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700917 }
918 }
919
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700920# if and else should not have general statements after it
921 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700922 $1 !~ /^\s*(?:\sif|{|\\|$)/) {
923 ERROR("trailing statements should be on next line\n" . $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700924 }
925
Andy Whitcroft653d4872007-06-23 17:16:34 -0700926# multi-statement macros should be enclosed in a do while loop, grab the
927# first statement and ensure its the whole macro if its not enclosed
928# in a known goot container
929 if (($prevline=~/\#define.*\\/) and
930 !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and
931 !($line=~/do.*{/) and !($line=~/\(\{/) and
932 !($line=~/^.\s*$Declare\s/)) {
933 # Grab the first statement, if that is the entire macro
934 # its ok. This may start either on the #define line
935 # or the one below.
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700936 my $ln = $linenr;
937 my $cnt = $realcnt;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700938 my $off = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700939
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700940 # If the macro starts on the define line start
941 # grabbing the statement after the identifier
942 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
943 ##print "1<$1> 2<$2>\n";
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700944 if (defined $2 && $2 ne '') {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700945 $off = length($1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700946 $ln--;
947 $cnt++;
948 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700949 my @ctx = ctx_statement($ln, $cnt, $off);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700950 my $ctx_ln = $ln + $#ctx + 1;
951 my $ctx = join("\n", @ctx);
952
953 # Pull in any empty extension lines.
954 while ($ctx =~ /\\$/ &&
955 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
956 $ctx .= $lines[$ctx_ln - 1];
957 $ctx_ln++;
958 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700959
960 if ($ctx =~ /\\$/) {
961 if ($ctx =~ /;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700962 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700963 } else {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700964 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700965 }
Andy Whitcroft653d4872007-06-23 17:16:34 -0700966 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700967 }
968
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700969# check for redundant bracing round if etc
970 if ($line =~ /\b(if|while|for|else)\b/) {
971 # Locate the end of the opening statement.
972 my @control = ctx_statement($linenr, $realcnt, 0);
973 my $nr = $linenr + (scalar(@control) - 1);
974 my $cnt = $realcnt - (scalar(@control) - 1);
975
976 my $off = $realcnt - $cnt;
977 #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n";
978
979 # If this is is a braced statement group check it
980 if ($lines[$nr - 1] =~ /{\s*$/) {
981 my ($lvl, @block) = ctx_block_level($nr, $cnt);
982
983 my $stmt = join(' ', @block);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700984 $stmt =~ s/(^[^{]*){//;
985 my $before = $1;
986 $stmt =~ s/}([^}]*$)//;
987 my $after = $1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700988
989 #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n";
990 #print "stmt<$stmt>\n\n";
991
992 # Count the ;'s if there is fewer than two
993 # then there can only be one statement,
994 # if there is a brace inside we cannot
995 # trivially detect if its one statement.
996 # Also nested if's often require braces to
997 # disambiguate the else binding so shhh there.
998 my @semi = ($stmt =~ /;/g);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700999 push(@semi, "/**/") if ($stmt =~ m@/\*@);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001000 ##print "semi<" . scalar(@semi) . ">\n";
1001 if ($lvl == 0 && scalar(@semi) < 2 &&
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001002 $stmt !~ /{/ && $stmt !~ /\bif\b/ &&
1003 $before !~ /}/ && $after !~ /{/) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001004 my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n";
1005 shift(@block);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001006 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001007 }
1008 }
1009 }
1010
Andy Whitcroft653d4872007-06-23 17:16:34 -07001011# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001012 for my $inc (@dep_includes) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001013 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001014 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001015 }
1016 }
1017
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001018# don't use deprecated functions
1019 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001020 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001021 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001022 }
1023 }
1024
1025# no volatiles please
Andy Whitcroft00df3442007-06-08 13:47:06 -07001026 if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001027 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001028 }
1029
Andy Whitcroft00df3442007-06-08 13:47:06 -07001030# warn about #if 0
1031 if ($line =~ /^.#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001032 CHK("if this code is redundant consider removing it\n" .
1033 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001034 }
1035
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001036# check for needless kfree() checks
1037 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1038 my $expr = $1;
1039 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1040 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1041 }
1042 }
1043
Andy Whitcroft00df3442007-06-08 13:47:06 -07001044# warn about #ifdefs in C files
1045# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1046# print "#ifdef in C files should be avoided\n";
1047# print "$herecurr";
1048# $clean = 0;
1049# }
1050
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001051# warn about spacing in #ifdefs
1052 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1053 ERROR("exactly one space required after that #$1\n" . $herecurr);
1054 }
1055
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001056# check for spinlock_t definitions without a comment.
1057 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
1058 my $which = $1;
1059 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001060 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001061 }
1062 }
1063# check for memory barriers without a comment.
1064 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1065 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001066 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001067 }
1068 }
1069# check of hardware specific defines
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001070 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001071 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001072 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001073
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001074# check the location of the inline attribute, that it is between
1075# storage class and type.
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001076 if ($line =~ /$Type\s+(?:inline|__always_inline|noinline)\b/ ||
1077 $line =~ /\b(?:inline|__always_inline|noinline)\s+$Storage/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001078 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1079 }
1080
1081# check for new externs in .c files.
1082 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1083 WARN("externs should be avoided in .c files\n" . $herecurr);
1084 }
1085
1086# checks for new __setup's
1087 if ($rawline =~ /\b__setup\("([^"]*)"/) {
1088 my $name = $1;
1089
1090 if (!grep(/$name/, @setup_docs)) {
1091 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1092 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001093 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001094 }
1095
1096 if ($chk_patch && !$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001097 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001098 }
1099 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001100 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001101 }
1102
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001103 if ($clean == 0 && ($chk_patch || $is_patch)) {
1104 print report_dump();
1105 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001106 if ($clean == 1 && $quiet == 0) {
1107 print "Your patch has no obvious style problems and is ready for submission.\n"
1108 }
1109 if ($clean == 0 && $quiet == 0) {
1110 print "Your patch has style problems, please review. If any of these errors\n";
1111 print "are false positives report them to the maintainer, see\n";
1112 print "CHECKPATCH in MAINTAINERS.\n";
1113 }
1114 return $clean;
1115}