]> bbs.cooldavid.org Git - net-next-2.6.git/blame - scripts/checkpatch.pl
Typo: fro -> from
[net-next-2.6.git] / scripts / checkpatch.pl
CommitLineData
0a920b5b
AW
1#!/usr/bin/perl -w
2# (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
00df344f 3# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
0a920b5b
AW
4# (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;
00df344f 10$P =~ s@.*/@@g;
0a920b5b 11
de7d4f0e 12my $V = '0.07';
0a920b5b
AW
13
14use Getopt::Long qw(:config no_auto_abbrev);
15
16my $quiet = 0;
17my $tree = 1;
18my $chk_signoff = 1;
19my $chk_patch = 1;
653d4876 20my $tst_type = 0;
0a920b5b
AW
21GetOptions(
22 'q|quiet' => \$quiet,
23 'tree!' => \$tree,
24 'signoff!' => \$chk_signoff,
25 'patch!' => \$chk_patch,
653d4876 26 'test-type!' => \$tst_type,
0a920b5b
AW
27) or exit;
28
29my $exit = 0;
30
31if ($#ARGV < 0) {
00df344f 32 print "usage: $P [options] patchfile\n";
0a920b5b
AW
33 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
4a0df2ef
AW
44my @dep_includes = ();
45my @dep_functions = ();
0a920b5b
AW
46my $removal = 'Documentation/feature-removal-schedule.txt';
47if ($tree && -f $removal) {
48 open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n";
49 while (<REMOVE>) {
50 if (/^Files:\s+(.*\S)/) {
51 for my $file (split(/[, ]+/, $1)) {
52 if ($file =~ m@include/(.*)@) {
4a0df2ef 53 push(@dep_includes, $1);
0a920b5b
AW
54 }
55 }
4a0df2ef
AW
56
57 } elsif (/^Funcs:\s+(.*\S)/) {
58 for my $func (split(/[, ]+/, $1)) {
59 push(@dep_functions, $func);
60 }
0a920b5b
AW
61 }
62 }
63}
64
00df344f 65my @rawlines = ();
0a920b5b
AW
66while (<>) {
67 chomp;
00df344f 68 push(@rawlines, $_);
0a920b5b 69 if (eof(ARGV)) {
00df344f 70 if (!process($ARGV, @rawlines)) {
0a920b5b
AW
71 $exit = 1;
72 }
00df344f 73 @rawlines = ();
0a920b5b
AW
74 }
75}
76
77exit($exit);
78
79sub top_of_kernel_tree {
80 if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") &&
81 (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") &&
82 (-d "Documentation") && (-d "arch") && (-d "include") &&
83 (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") &&
84 (-d "kernel") && (-d "lib") && (-d "scripts")) {
85 return 1;
86 }
87 return 0;
88}
89
90sub expand_tabs {
91 my ($str) = @_;
92
93 my $res = '';
94 my $n = 0;
95 for my $c (split(//, $str)) {
96 if ($c eq "\t") {
97 $res .= ' ';
98 $n++;
99 for (; ($n % 8) != 0; $n++) {
100 $res .= ' ';
101 }
102 next;
103 }
104 $res .= $c;
105 $n++;
106 }
107
108 return $res;
109}
110
4a0df2ef
AW
111sub line_stats {
112 my ($line) = @_;
113
114 # Drop the diff line leader and expand tabs
115 $line =~ s/^.//;
116 $line = expand_tabs($line);
117
118 # Pick the indent from the front of the line.
119 my ($white) = ($line =~ /^(\s*)/);
120
121 return (length($line), length($white));
122}
123
00df344f
AW
124sub sanitise_line {
125 my ($line) = @_;
126
127 my $res = '';
128 my $l = '';
129
130 my $quote = '';
131
132 foreach my $c (split(//, $line)) {
133 if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
134 if ($quote eq '') {
135 $quote = $c;
136 $res .= $c;
137 $l = $c;
138 next;
139 } elsif ($quote eq $c) {
140 $quote = '';
141 }
142 }
143 if ($quote && $c ne "\t") {
144 $res .= "X";
145 } else {
146 $res .= $c;
147 }
148
149 $l = $c;
150 }
151
152 return $res;
153}
154
4a0df2ef 155sub ctx_block_get {
653d4876 156 my ($linenr, $remain, $outer, $open, $close) = @_;
4a0df2ef
AW
157 my $line;
158 my $start = $linenr - 1;
4a0df2ef
AW
159 my $blk = '';
160 my @o;
161 my @c;
162 my @res = ();
163
00df344f
AW
164 for ($line = $start; $remain > 0; $line++) {
165 next if ($rawlines[$line] =~ /^-/);
166 $remain--;
167
168 $blk .= $rawlines[$line];
4a0df2ef 169
653d4876
AW
170 @o = ($blk =~ /$open/g);
171 @c = ($blk =~ /$close/g);
4a0df2ef
AW
172
173 if (!$outer || (scalar(@o) - scalar(@c)) == 1) {
00df344f 174 push(@res, $rawlines[$line]);
4a0df2ef
AW
175 }
176
177 last if (scalar(@o) == scalar(@c));
178 }
179
180 return @res;
181}
182sub ctx_block_outer {
183 my ($linenr, $remain) = @_;
184
653d4876 185 return ctx_block_get($linenr, $remain, 1, '\{', '\}');
4a0df2ef
AW
186}
187sub ctx_block {
188 my ($linenr, $remain) = @_;
189
653d4876
AW
190 return ctx_block_get($linenr, $remain, 0, '\{', '\}');
191}
192sub ctx_statement {
193 my ($linenr, $remain) = @_;
194
195 return ctx_block_get($linenr, $remain, 0, '\(', '\)');
4a0df2ef
AW
196}
197
198sub ctx_locate_comment {
199 my ($first_line, $end_line) = @_;
200
201 # Catch a comment on the end of the line itself.
00df344f 202 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
4a0df2ef
AW
203 return $current_comment if (defined $current_comment);
204
205 # Look through the context and try and figure out if there is a
206 # comment.
207 my $in_comment = 0;
208 $current_comment = '';
209 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
00df344f
AW
210 my $line = $rawlines[$linenr - 1];
211 #warn " $line\n";
4a0df2ef
AW
212 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
213 $in_comment = 1;
214 }
215 if ($line =~ m@/\*@) {
216 $in_comment = 1;
217 }
218 if (!$in_comment && $current_comment ne '') {
219 $current_comment = '';
220 }
221 $current_comment .= $line . "\n" if ($in_comment);
222 if ($line =~ m@\*/@) {
223 $in_comment = 0;
224 }
225 }
226
227 chomp($current_comment);
228 return($current_comment);
229}
230sub ctx_has_comment {
231 my ($first_line, $end_line) = @_;
232 my $cmt = ctx_locate_comment($first_line, $end_line);
233
00df344f 234 ##print "LINE: $rawlines[$end_line - 1 ]\n";
4a0df2ef
AW
235 ##print "CMMT: $cmt\n";
236
237 return ($cmt ne '');
238}
239
0a920b5b
AW
240sub cat_vet {
241 my ($vet) = @_;
242
243 $vet =~ s/\t/^I/;
244 $vet =~ s/$/\$/;
245
246 return $vet;
247}
248
de7d4f0e
AW
249sub ERROR {
250 print "ERROR: $_[0]\n";
251 our $clean = 0;
252}
253sub WARN {
254 print "WARNING: $_[0]\n";
255 our $clean = 0;
256}
257sub CHK {
258 print "CHECK: $_[0]\n";
259 our $clean = 0;
260}
261
0a920b5b
AW
262sub process {
263 my $filename = shift;
264 my @lines = @_;
265
266 my $linenr=0;
267 my $prevline="";
268 my $stashline="";
269
4a0df2ef 270 my $length;
0a920b5b
AW
271 my $indent;
272 my $previndent=0;
273 my $stashindent=0;
274
de7d4f0e 275 our $clean = 1;
0a920b5b
AW
276 my $signoff = 0;
277 my $is_patch = 0;
278
279 # Trace the real file/line as we go.
280 my $realfile = '';
281 my $realline = 0;
282 my $realcnt = 0;
283 my $here = '';
284 my $in_comment = 0;
285 my $first_line = 0;
286
d8aaf121
AW
287 my $Ident = qr{[A-Za-z\d_]+};
288 my $Storage = qr{extern|static};
289 my $Sparse = qr{__user|__kernel|__force|__iomem};
290 my $NonptrType = qr{
291 \b
292 (?:const\s+)?
293 (?:unsigned\s+)?
294 (?:
295 void|
296 char|
297 short|
298 int|
299 long|
300 unsigned|
301 float|
302 double|
303 long\s+int|
304 long\s+long|
305 long\s+long\s+int|
de7d4f0e
AW
306 u8|u16|u32|u64|
307 s8|s16|s32|s64|
d8aaf121
AW
308 struct\s+$Ident|
309 union\s+$Ident|
de7d4f0e 310 enum\s+$Ident|
d8aaf121
AW
311 ${Ident}_t
312 )
313 (?:\s+$Sparse)*
314 \b
315 }x;
316 my $Type = qr{
317 \b$NonptrType\b
318 (?:\s*\*+\s*const|\s*\*+)?
319 }x;
320 my $Declare = qr{(?:$Storage\s+)?$Type};
321 my $Attribute = qr{__read_mostly|__init|__initdata};
653d4876 322
de7d4f0e
AW
323 # Pre-scan the patch looking for any __setup documentation.
324 my @setup_docs = ();
325 my $setup_docs = 0;
326 foreach my $line (@lines) {
327 if ($line=~/^\+\+\+\s+(\S+)/) {
328 $setup_docs = 0;
329 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
330 $setup_docs = 1;
331 }
332 next;
333 }
334
335 if ($setup_docs && $line =~ /^\+/) {
336 push(@setup_docs, $line);
337 }
338 }
339
0a920b5b
AW
340 foreach my $line (@lines) {
341 $linenr++;
342
653d4876
AW
343 my $rawline = $line;
344
0a920b5b
AW
345#extract the filename as it passes
346 if ($line=~/^\+\+\+\s+(\S+)/) {
347 $realfile=$1;
00df344f 348 $realfile =~ s@^[^/]*/@@;
0a920b5b
AW
349 $in_comment = 0;
350 next;
351 }
352#extract the line range in the file after the patch is applied
353 if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
354 $is_patch = 1;
4a0df2ef 355 $first_line = $linenr + 1;
0a920b5b
AW
356 $in_comment = 0;
357 $realline=$1-1;
358 if (defined $2) {
359 $realcnt=$3+1;
360 } else {
361 $realcnt=1+1;
362 }
363 next;
364 }
365
4a0df2ef
AW
366# track the line number as we move through the hunk, note that
367# new versions of GNU diff omit the leading space on completely
368# blank context lines so we need to count that too.
369 if ($line =~ /^( |\+|$)/) {
0a920b5b 370 $realline++;
d8aaf121 371 $realcnt-- if ($realcnt != 0);
0a920b5b
AW
372
373 # track any sort of multi-line comment. Obviously if
374 # the added text or context do not include the whole
375 # comment we will not see it. Such is life.
376 #
377 # Guestimate if this is a continuing comment. If this
378 # is the start of a diff block and this line starts
379 # ' *' then it is very likely a comment.
4a0df2ef 380 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
0a920b5b
AW
381 $in_comment = 1;
382 }
383 if ($line =~ m@/\*@) {
384 $in_comment = 1;
385 }
386 if ($line =~ m@\*/@) {
387 $in_comment = 0;
388 }
389
4a0df2ef
AW
390 # Measure the line length and indent.
391 ($length, $indent) = line_stats($line);
0a920b5b
AW
392
393 # Track the previous line.
394 ($prevline, $stashline) = ($stashline, $line);
395 ($previndent, $stashindent) = ($stashindent, $indent);
d8aaf121
AW
396 } elsif ($realcnt == 1) {
397 $realcnt--;
0a920b5b
AW
398 }
399
400#make up the handle for any error we report on this line
389834b6
RD
401 $here = "#$linenr: ";
402 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
0a920b5b 403
00df344f 404 my $hereline = "$here\n$line\n";
de7d4f0e
AW
405 my $herecurr = "$here\n$line\n";
406 my $hereprev = "$here\n$prevline\n$line\n";
0a920b5b
AW
407
408#check the patch for a signoff:
d8aaf121 409 if ($line =~ /^\s*signed-off-by:/i) {
4a0df2ef
AW
410 # This is a signoff, if ugly, so do not double report.
411 $signoff++;
0a920b5b 412 if (!($line =~ /^\s*Signed-off-by:/)) {
de7d4f0e
AW
413 WARN("Signed-off-by: is the preferred form\n" .
414 $herecurr);
0a920b5b
AW
415 }
416 if ($line =~ /^\s*signed-off-by:\S/i) {
de7d4f0e
AW
417 WARN("need space after Signed-off-by:\n" .
418 $herecurr);
0a920b5b
AW
419 }
420 }
421
00df344f
AW
422# Check for wrappage within a valid hunk of the file
423 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
de7d4f0e
AW
424 ERROR("patch seems to be corrupt (line wrapped?)\n" .
425 $herecurr);
426 }
427
428# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
429 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
430 !($line =~ m/^(
431 [\x09\x0A\x0D\x20-\x7E] # ASCII
432 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
433 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
434 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
435 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
436 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
437 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
438 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
439 )*$/x )) {
440 ERROR("Invalid UTF-8\n" . $herecurr);
00df344f
AW
441 }
442
443#ignore lines being removed
444 if ($line=~/^-/) {next;}
0a920b5b 445
00df344f
AW
446# check we are in a valid source file if not then ignore this hunk
447 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
0a920b5b
AW
448
449#trailing whitespace
d8aaf121 450 if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
de7d4f0e
AW
451 my $herevet = "$here\n" . cat_vet($line) . "\n";
452 ERROR("trailing whitespace\n" . $herevet);
0a920b5b
AW
453 }
454#80 column limit
00df344f 455 if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
de7d4f0e 456 WARN("line over 80 characters\n" . $herecurr);
0a920b5b
AW
457 }
458
459# check we are in a valid source file *.[hc] if not then ignore this hunk
460 next if ($realfile !~ /\.[hc]$/);
461
462# at the beginning of a line any tabs must come first and anything
463# more than 8 must use tabs.
464 if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
de7d4f0e
AW
465 my $herevet = "$here\n" . cat_vet($line) . "\n";
466 ERROR("use tabs not spaces\n" . $herevet);
0a920b5b
AW
467 }
468
469 #
470 # The rest of our checks refer specifically to C style
471 # only apply those _outside_ comments.
472 #
473 next if ($in_comment);
474
653d4876 475# Remove comments from the line before processing.
00df344f
AW
476 $line =~ s@/\*.*\*/@@g;
477 $line =~ s@/\*.*@@;
478 $line =~ s@.*\*/@@;
479
653d4876
AW
480# Standardise the strings and chars within the input to simplify matching.
481 $line = sanitise_line($line);
482
483#
484# Checks which may be anchored in the context.
485#
00df344f 486
653d4876
AW
487# Check for switch () and associated case and default
488# statements should be at the same indent.
00df344f
AW
489 if ($line=~/\bswitch\s*\(.*\)/) {
490 my $err = '';
491 my $sep = '';
492 my @ctx = ctx_block_outer($linenr, $realcnt);
493 shift(@ctx);
494 for my $ctx (@ctx) {
495 my ($clen, $cindent) = line_stats($ctx);
496 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
497 $indent != $cindent) {
498 $err .= "$sep$ctx\n";
499 $sep = '';
500 } else {
501 $sep = "[...]\n";
502 }
503 }
504 if ($err ne '') {
de7d4f0e
AW
505 ERROR("switch and case should be at the same indent\n$hereline\n$err\n");
506 }
507 }
508
509# if/while/etc brace do not go on next line, unless defining a do while loop,
510# or if that brace on the next line is for something else
511 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
512 my @ctx = ctx_statement($linenr, $realcnt);
513 my $ctx_ln = $linenr + $#ctx + 1;
514 my $ctx_cnt = $realcnt - $#ctx - 1;
515 my $ctx = join("\n", @ctx);
516
517 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
518 $ctx_ln++;
519 $ctx_cnt--;
520 }
521 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
522
523 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
524 ERROR("That { should be on the previous line\n" .
525 "$here\n$ctx\n$lines[$ctx_ln - 1]");
00df344f
AW
526 }
527 }
528
529#ignore lines not being added
530 if ($line=~/^[^\+]/) {next;}
531
653d4876
AW
532# TEST: allow direct testing of the type matcher.
533 if ($tst_type && $line =~ /^.$Declare$/) {
de7d4f0e 534 ERROR("TEST: is type $Declare\n" . $herecurr);
653d4876
AW
535 next;
536 }
537
538#
539# Checks which are anchored on the added line.
540#
541
542# check for malformed paths in #include statements (uses RAW line)
543 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
544 my $path = $1;
545 if ($path =~ m{//}) {
de7d4f0e
AW
546 ERROR("malformed #include filename\n" .
547 $herecurr);
653d4876
AW
548 }
549 # Sanitise this special form of string.
550 $path = 'X' x length($path);
551 $line =~ s{\<.*\>}{<$path>};
552 }
00df344f 553
0a920b5b 554# no C99 // comments
00df344f 555 if ($line =~ m{//}) {
de7d4f0e 556 ERROR("do not use C99 // comments\n" . $herecurr);
0a920b5b 557 }
00df344f 558 # Remove C99 comments.
0a920b5b
AW
559 $line =~ s@//.*@@;
560
561#EXPORT_SYMBOL should immediately follow its function closing }.
653d4876
AW
562 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
563 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
564 my $name = $1;
0a920b5b
AW
565 if (($prevline !~ /^}/) &&
566 ($prevline !~ /^\+}/) &&
653d4876
AW
567 ($prevline !~ /^ }/) &&
568 ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) {
de7d4f0e 569 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
0a920b5b
AW
570 }
571 }
572
653d4876 573# check for static initialisers.
0a920b5b 574 if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
de7d4f0e
AW
575 ERROR("do not initialise statics to 0 or NULL\n" .
576 $herecurr);
0a920b5b
AW
577 }
578
653d4876
AW
579# check for new typedefs, only function parameters and sparse annotations
580# make sense.
581 if ($line =~ /\btypedef\s/ &&
de7d4f0e 582 $line !~ /\btypedef\s+$Type\s+\(\s*\*$Ident\s*\)\s*\(/ &&
653d4876 583 $line !~ /\b__bitwise(?:__|)\b/) {
de7d4f0e 584 WARN("do not add new typedefs\n" . $herecurr);
0a920b5b
AW
585 }
586
587# * goes on variable not on type
d8aaf121 588 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
de7d4f0e
AW
589 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
590 $herecurr);
d8aaf121
AW
591
592 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
de7d4f0e
AW
593 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
594 $herecurr);
d8aaf121
AW
595
596 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+const)?\s+[A-Za-z\d_]+}) {
de7d4f0e
AW
597 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
598 $herecurr);
d8aaf121
AW
599
600 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+const)\s+[A-Za-z\d_]+}) {
de7d4f0e
AW
601 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
602 $herecurr);
0a920b5b
AW
603 }
604
605# # no BUG() or BUG_ON()
606# if ($line =~ /\b(BUG|BUG_ON)\b/) {
607# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
608# print "$herecurr";
609# $clean = 0;
610# }
611
00df344f
AW
612# printk should use KERN_* levels. Note that follow on printk's on the
613# same line do not need a level, so we use the current block context
614# to try and find and validate the current printk. In summary the current
615# printk includes all preceeding printk's which have no newline on the end.
616# we assume the first bad printk is the one to report.
0a920b5b 617 if ($line =~ /\bprintk\((?!KERN_)/) {
00df344f
AW
618 my $ok = 0;
619 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
620 #print "CHECK<$lines[$ln - 1]\n";
621 # we have a preceeding printk if it ends
622 # with "\n" ignore it, else it is to blame
623 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
624 if ($rawlines[$ln - 1] !~ m{\\n"}) {
625 $ok = 1;
626 }
627 last;
628 }
629 }
630 if ($ok == 0) {
de7d4f0e 631 WARN("printk() should include KERN_ facility level\n" . $herecurr);
00df344f 632 }
0a920b5b
AW
633 }
634
653d4876
AW
635# function brace can't be on same line, except for #defines of do while,
636# or if closed on same line
d8aaf121 637 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
0a920b5b 638 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
de7d4f0e 639 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
0a920b5b 640 }
653d4876
AW
641
642# Check operator spacing.
4a0df2ef
AW
643 # Note we expand the line with the leading + as the real
644 # line will be displayed with the leading + and the tabs
645 # will therefore also expand that way.
0a920b5b 646 my $opline = $line;
4a0df2ef 647 $opline = expand_tabs($opline);
00df344f 648 $opline =~ s/^./ /;
0a920b5b 649 if (!($line=~/\#\s*include/)) {
0a920b5b 650 my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
00df344f 651 my $off = 0;
0a920b5b 652 for (my $n = 0; $n < $#elements; $n += 2) {
4a0df2ef
AW
653 $off += length($elements[$n]);
654
655 my $a = '';
656 $a = 'V' if ($elements[$n] ne '');
657 $a = 'W' if ($elements[$n] =~ /\s$/);
658 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
659 $a = 'O' if ($elements[$n] eq '');
660 $a = 'E' if ($elements[$n] eq '' && $n == 0);
661
0a920b5b 662 my $op = $elements[$n + 1];
4a0df2ef
AW
663
664 my $c = '';
0a920b5b 665 if (defined $elements[$n + 2]) {
4a0df2ef
AW
666 $c = 'V' if ($elements[$n + 2] ne '');
667 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
668 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
669 $c = 'O' if ($elements[$n + 2] eq '');
670 } else {
671 $c = 'E';
0a920b5b
AW
672 }
673
00df344f 674 # Pick up the preceeding and succeeding characters.
de7d4f0e 675 my $ca = substr($opline, 0, $off);
00df344f 676 my $cc = '';
653d4876 677 if (length($opline) >= ($off + length($elements[$n + 1]))) {
d8aaf121 678 $cc = substr($opline, $off + length($elements[$n + 1]));
00df344f 679 }
de7d4f0e 680 my $cb = "$ca$;$cc";
00df344f 681
4a0df2ef
AW
682 my $ctx = "${a}x${c}";
683
684 my $at = "(ctx:$ctx)";
685
686 my $ptr = (" " x $off) . "^";
de7d4f0e 687 my $hereptr = "$hereline$ptr\n";
0a920b5b
AW
688
689 ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
0a920b5b 690
d8aaf121
AW
691 # ; should have either the end of line or a space or \ after it
692 if ($op eq ';') {
de7d4f0e
AW
693 if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
694 $cc !~ /^;/) {
695 ERROR("need space after that '$op' $at\n" . $hereptr);
d8aaf121
AW
696 }
697
698 # // is a comment
699 } elsif ($op eq '//') {
0a920b5b
AW
700
701 # -> should have no spaces
702 } elsif ($op eq '->') {
4a0df2ef 703 if ($ctx =~ /Wx.|.xW/) {
de7d4f0e 704 ERROR("no spaces around that '$op' $at\n" . $hereptr);
0a920b5b
AW
705 }
706
707 # , must have a space on the right.
708 } elsif ($op eq ',') {
d8aaf121 709 if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
de7d4f0e 710 ERROR("need space after that '$op' $at\n" . $hereptr);
0a920b5b
AW
711 }
712
713 # unary ! and unary ~ are allowed no space on the right
714 } elsif ($op eq '!' or $op eq '~') {
4a0df2ef 715 if ($ctx !~ /[WOEB]x./) {
de7d4f0e 716 ERROR("need space before that '$op' $at\n" . $hereptr);
0a920b5b 717 }
4a0df2ef 718 if ($ctx =~ /.xW/) {
de7d4f0e 719 ERROR("no space after that '$op' $at\n" . $hereptr);
0a920b5b
AW
720 }
721
722 # unary ++ and unary -- are allowed no space on one side.
723 } elsif ($op eq '++' or $op eq '--') {
d8aaf121 724 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
de7d4f0e 725 ERROR("need space one side of that '$op' $at\n" . $hereptr);
0a920b5b 726 }
d8aaf121 727 if ($ctx =~ /Wx./ && $cc =~ /^;/) {
de7d4f0e 728 ERROR("no space before that '$op' $at\n" . $hereptr);
653d4876 729 }
0a920b5b
AW
730
731 # & is both unary and binary
732 # unary:
733 # a &b
734 # binary (consistent spacing):
735 # a&b OK
736 # a & b OK
737 #
738 # boiling down to: if there is a space on the right then there
739 # should be one on the left.
740 #
741 # - is the same
742 #
4a0df2ef 743 } elsif ($op eq '&' or $op eq '-') {
00df344f 744 if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
de7d4f0e 745 ERROR("need space before that '$op' $at\n" . $hereptr);
4a0df2ef
AW
746 }
747
00df344f
AW
748 # * is the same as & only adding:
749 # type:
750 # (foo *)
751 # (foo **)
752 #
4a0df2ef 753 } elsif ($op eq '*') {
de7d4f0e
AW
754 if ($ca !~ /$Type$/ && $cb !~ /(\*$;|$;\*)/ &&
755 $ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
756 ERROR("need space before that '$op' $at\n" . $hereptr);
0a920b5b
AW
757 }
758
759 # << and >> may either have or not have spaces both sides
760 } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
761 $op eq '^' or $op eq '|')
762 {
4a0df2ef 763 if ($ctx !~ /VxV|WxW|VxE|WxE/) {
de7d4f0e
AW
764 ERROR("need consistent spacing around '$op' $at\n" .
765 $hereptr);
0a920b5b
AW
766 }
767
768 # All the others need spaces both sides.
4a0df2ef 769 } elsif ($ctx !~ /[EW]x[WE]/) {
de7d4f0e 770 ERROR("need spaces around that '$op' $at\n" . $hereptr);
0a920b5b 771 }
4a0df2ef 772 $off += length($elements[$n + 1]);
0a920b5b
AW
773 }
774 }
775
776#need space before brace following if, while, etc
de7d4f0e
AW
777 if ($line =~ /\(.*\){/ || $line =~ /do{/) {
778 ERROR("need a space before the open brace '{'\n" . $herecurr);
779 }
780
781# closing brace should have a space following it when it has anything
782# on the line
783 if ($line =~ /}(?!(?:,|;|\)))\S/) {
784 ERROR("need a space after that close brace '}'\n" . $herecurr);
0a920b5b
AW
785 }
786
787#goto labels aren't indented, allow a single space however
4a0df2ef 788 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
0a920b5b 789 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
de7d4f0e 790 WARN("labels should not be indented\n" . $herecurr);
0a920b5b
AW
791 }
792
793# Need a space before open parenthesis after if, while etc
4a0df2ef 794 if ($line=~/\b(if|while|for|switch)\(/) {
de7d4f0e 795 ERROR("need a space before the open parenthesis '('\n" . $herecurr);
0a920b5b
AW
796 }
797
798# Check for illegal assignment in if conditional.
653d4876 799 if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
00df344f 800 #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
de7d4f0e 801 ERROR("do not use assignment in if condition\n" . $herecurr);
0a920b5b
AW
802 }
803
804 # Check for }<nl>else {, these must be at the same
805 # indent level to be relevant to each other.
806 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
807 $previndent == $indent) {
de7d4f0e 808 ERROR("else should follow close brace '}'\n" . $hereprev);
0a920b5b
AW
809 }
810
0a920b5b
AW
811#studly caps, commented out until figure out how to distinguish between use of existing and adding new
812# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
813# print "No studly caps, use _\n";
814# print "$herecurr";
815# $clean = 0;
816# }
817
818#no spaces allowed after \ in define
819 if ($line=~/\#define.*\\\s$/) {
de7d4f0e 820 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
0a920b5b
AW
821 }
822
653d4876
AW
823#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
824 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
0a920b5b
AW
825 my $checkfile = "include/linux/$1.h";
826 if (-f $checkfile) {
de7d4f0e
AW
827 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
828 $herecurr);
0a920b5b
AW
829 }
830 }
831
d8aaf121
AW
832# if and else should not have general statements after it
833 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
de7d4f0e
AW
834 $1 !~ /^\s*(?:\sif|{|\\|$)/) {
835 ERROR("trailing statements should be on next line\n" . $herecurr);
d8aaf121
AW
836 }
837
653d4876
AW
838# multi-statement macros should be enclosed in a do while loop, grab the
839# first statement and ensure its the whole macro if its not enclosed
840# in a known goot container
841 if (($prevline=~/\#define.*\\/) and
842 !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and
843 !($line=~/do.*{/) and !($line=~/\(\{/) and
844 !($line=~/^.\s*$Declare\s/)) {
845 # Grab the first statement, if that is the entire macro
846 # its ok. This may start either on the #define line
847 # or the one below.
d8aaf121
AW
848 my $ln = $linenr;
849 my $cnt = $realcnt;
653d4876 850
d8aaf121
AW
851 # If the macro starts on the define line start there.
852 if ($prevline !~ m{^.#\s*define\s*$Ident(?:\([^\)]*\))?\s*\\\s*$}) {
853 $ln--;
854 $cnt++;
855 }
de7d4f0e
AW
856 my @ctx = ctx_statement($ln, $cnt);
857 my $ctx_ln = $ln + $#ctx + 1;
858 my $ctx = join("\n", @ctx);
859
860 # Pull in any empty extension lines.
861 while ($ctx =~ /\\$/ &&
862 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
863 $ctx .= $lines[$ctx_ln - 1];
864 $ctx_ln++;
865 }
d8aaf121
AW
866
867 if ($ctx =~ /\\$/) {
868 if ($ctx =~ /;/) {
de7d4f0e 869 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
d8aaf121 870 } else {
de7d4f0e 871 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
d8aaf121 872 }
653d4876 873 }
0a920b5b
AW
874 }
875
653d4876 876# don't include deprecated include files (uses RAW line)
4a0df2ef 877 for my $inc (@dep_includes) {
653d4876 878 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
de7d4f0e 879 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
0a920b5b
AW
880 }
881 }
882
4a0df2ef
AW
883# don't use deprecated functions
884 for my $func (@dep_functions) {
00df344f 885 if ($line =~ /\b$func\b/) {
de7d4f0e 886 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
4a0df2ef
AW
887 }
888 }
889
890# no volatiles please
00df344f 891 if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
de7d4f0e 892 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4a0df2ef
AW
893 }
894
00df344f
AW
895# warn about #if 0
896 if ($line =~ /^.#\s*if\s+0\b/) {
de7d4f0e
AW
897 CHK("if this code is redundant consider removing it\n" .
898 $herecurr);
4a0df2ef
AW
899 }
900
00df344f
AW
901# warn about #ifdefs in C files
902# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
903# print "#ifdef in C files should be avoided\n";
904# print "$herecurr";
905# $clean = 0;
906# }
907
4a0df2ef
AW
908# check for spinlock_t definitions without a comment.
909 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
910 my $which = $1;
911 if (!ctx_has_comment($first_line, $linenr)) {
de7d4f0e 912 CHK("$1 definition without comment\n" . $herecurr);
4a0df2ef
AW
913 }
914 }
915# check for memory barriers without a comment.
916 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
917 if (!ctx_has_comment($first_line, $linenr)) {
de7d4f0e 918 CHK("memory barrier without comment\n" . $herecurr);
4a0df2ef
AW
919 }
920 }
921# check of hardware specific defines
922 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
de7d4f0e 923 CHK("architecture specific defines should be avoided\n" . $herecurr);
0a920b5b 924 }
653d4876 925
de7d4f0e
AW
926# check the location of the inline attribute, that it is between
927# storage class and type.
653d4876
AW
928 if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ ||
929 $line =~ /\b(?:inline|always_inline)\s+$Storage/) {
de7d4f0e
AW
930 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
931 }
932
933# check for new externs in .c files.
934 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
935 WARN("externs should be avoided in .c files\n" . $herecurr);
936 }
937
938# checks for new __setup's
939 if ($rawline =~ /\b__setup\("([^"]*)"/) {
940 my $name = $1;
941
942 if (!grep(/$name/, @setup_docs)) {
943 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
944 }
653d4876 945 }
0a920b5b
AW
946 }
947
948 if ($chk_patch && !$is_patch) {
de7d4f0e 949 ERROR("Does not appear to be a unified-diff format patch\n");
0a920b5b
AW
950 }
951 if ($is_patch && $chk_signoff && $signoff == 0) {
de7d4f0e 952 ERROR("Missing Signed-off-by: line(s)\n");
0a920b5b
AW
953 }
954
955 if ($clean == 1 && $quiet == 0) {
956 print "Your patch has no obvious style problems and is ready for submission.\n"
957 }
958 if ($clean == 0 && $quiet == 0) {
959 print "Your patch has style problems, please review. If any of these errors\n";
960 print "are false positives report them to the maintainer, see\n";
961 print "CHECKPATCH in MAINTAINERS.\n";
962 }
963 return $clean;
964}