Merge branch 'libertas-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/linvil...
[pandora-kernel.git] / scripts / checkpatch.pl
1 #!/usr/bin/perl -w
2 # (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
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
7 use strict;
8
9 my $P = $0;
10 $P =~ s@.*/@@g;
11
12 my $V = '0.04';
13
14 use Getopt::Long qw(:config no_auto_abbrev);
15
16 my $quiet = 0;
17 my $tree = 1;
18 my $chk_signoff = 1;
19 my $chk_patch = 1;
20 GetOptions(
21         'q|quiet'       => \$quiet,
22         'tree!'         => \$tree,
23         'signoff!'      => \$chk_signoff,
24         'patch!'        => \$chk_patch,
25 ) or exit;
26
27 my $exit = 0;
28
29 if ($#ARGV < 0) {
30         print "usage: $P [options] patchfile\n";
31         print "version: $V\n";
32         print "options: -q           => quiet\n";
33         print "         --no-tree    => run without a kernel tree\n";
34         exit(1);
35 }
36
37 if ($tree && !top_of_kernel_tree()) {
38         print "Must be run from the top-level dir. of a kernel tree\n";
39         exit(2);
40 }
41
42 my @dep_includes = ();
43 my @dep_functions = ();
44 my $removal = 'Documentation/feature-removal-schedule.txt';
45 if ($tree && -f $removal) {
46         open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n";
47         while (<REMOVE>) {
48                 if (/^Files:\s+(.*\S)/) {
49                         for my $file (split(/[, ]+/, $1)) {
50                                 if ($file =~ m@include/(.*)@) {
51                                         push(@dep_includes, $1);
52                                 }
53                         }
54
55                 } elsif (/^Funcs:\s+(.*\S)/) {
56                         for my $func (split(/[, ]+/, $1)) {
57                                 push(@dep_functions, $func);
58                         }
59                 }
60         }
61 }
62
63 my @rawlines = ();
64 while (<>) {
65         chomp;
66         push(@rawlines, $_);
67         if (eof(ARGV)) {
68                 if (!process($ARGV, @rawlines)) {
69                         $exit = 1;
70                 }
71                 @rawlines = ();
72         }
73 }
74
75 exit($exit);
76
77 sub 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
88 sub 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
109 sub 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
122 sub 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
153 sub ctx_block_get {
154         my ($linenr, $remain, $outer) = @_;
155         my $line;
156         my $start = $linenr - 1;
157         my $blk = '';
158         my @o;
159         my @c;
160         my @res = ();
161
162         for ($line = $start; $remain > 0; $line++) {
163                 next if ($rawlines[$line] =~ /^-/);
164                 $remain--;
165
166                 $blk .= $rawlines[$line];
167
168                 @o = ($blk =~ /\{/g);
169                 @c = ($blk =~ /\}/g);
170
171                 if (!$outer || (scalar(@o) - scalar(@c)) == 1) {
172                         push(@res, $rawlines[$line]);
173                 }
174
175                 last if (scalar(@o) == scalar(@c));
176         }
177
178         return @res;
179 }
180 sub ctx_block_outer {
181         my ($linenr, $remain) = @_;
182
183         return ctx_block_get($linenr, $remain, 1);
184 }
185 sub ctx_block {
186         my ($linenr, $remain) = @_;
187
188         return ctx_block_get($linenr, $remain, 0);
189 }
190
191 sub ctx_locate_comment {
192         my ($first_line, $end_line) = @_;
193
194         # Catch a comment on the end of the line itself.
195         my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
196         return $current_comment if (defined $current_comment);
197
198         # Look through the context and try and figure out if there is a
199         # comment.
200         my $in_comment = 0;
201         $current_comment = '';
202         for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
203                 my $line = $rawlines[$linenr - 1];
204                 #warn "           $line\n";
205                 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
206                         $in_comment = 1;
207                 }
208                 if ($line =~ m@/\*@) {
209                         $in_comment = 1;
210                 }
211                 if (!$in_comment && $current_comment ne '') {
212                         $current_comment = '';
213                 }
214                 $current_comment .= $line . "\n" if ($in_comment);
215                 if ($line =~ m@\*/@) {
216                         $in_comment = 0;
217                 }
218         }
219
220         chomp($current_comment);
221         return($current_comment);
222 }
223 sub ctx_has_comment {
224         my ($first_line, $end_line) = @_;
225         my $cmt = ctx_locate_comment($first_line, $end_line);
226
227         ##print "LINE: $rawlines[$end_line - 1 ]\n";
228         ##print "CMMT: $cmt\n";
229
230         return ($cmt ne '');
231 }
232
233 sub cat_vet {
234         my ($vet) = @_;
235
236         $vet =~ s/\t/^I/;
237         $vet =~ s/$/\$/;
238
239         return $vet;
240 }
241
242 sub process {
243         my $filename = shift;
244         my @lines = @_;
245
246         my $linenr=0;
247         my $prevline="";
248         my $stashline="";
249
250         my $length;
251         my $indent;
252         my $previndent=0;
253         my $stashindent=0;
254
255         my $clean = 1;
256         my $signoff = 0;
257         my $is_patch = 0;
258
259         # Trace the real file/line as we go.
260         my $realfile = '';
261         my $realline = 0;
262         my $realcnt = 0;
263         my $here = '';
264         my $in_comment = 0;
265         my $first_line = 0;
266
267         foreach my $line (@lines) {
268                 $linenr++;
269
270 #extract the filename as it passes
271                 if ($line=~/^\+\+\+\s+(\S+)/) {
272                         $realfile=$1;
273                         $realfile =~ s@^[^/]*/@@;
274                         $in_comment = 0;
275                         next;
276                 }
277 #extract the line range in the file after the patch is applied
278                 if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
279                         $is_patch = 1;
280                         $first_line = $linenr + 1;
281                         $in_comment = 0;
282                         $realline=$1-1;
283                         if (defined $2) {
284                                 $realcnt=$3+1;
285                         } else {
286                                 $realcnt=1+1;
287                         }
288                         next;
289                 }
290
291 # track the line number as we move through the hunk, note that
292 # new versions of GNU diff omit the leading space on completely
293 # blank context lines so we need to count that too.
294                 if ($line =~ /^( |\+|$)/) {
295                         $realline++;
296
297                         # track any sort of multi-line comment.  Obviously if
298                         # the added text or context do not include the whole
299                         # comment we will not see it. Such is life.
300                         #
301                         # Guestimate if this is a continuing comment.  If this
302                         # is the start of a diff block and this line starts
303                         # ' *' then it is very likely a comment.
304                         if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
305                                 $in_comment = 1;
306                         }
307                         if ($line =~ m@/\*@) {
308                                 $in_comment = 1;
309                         }
310                         if ($line =~ m@\*/@) {
311                                 $in_comment = 0;
312                         }
313
314                         # Measure the line length and indent.
315                         ($length, $indent) = line_stats($line);
316
317                         # Track the previous line.
318                         ($prevline, $stashline) = ($stashline, $line);
319                         ($previndent, $stashindent) = ($stashindent, $indent);
320                 }
321                 $realcnt-- if ($realcnt != 0);
322
323 #make up the handle for any error we report on this line
324                 $here = "#$linenr: ";
325                 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
326
327                 my $hereline = "$here\n$line\n";
328                 my $herecurr = "$here\n$line\n\n";
329                 my $hereprev = "$here\n$prevline\n$line\n\n";
330
331 #check the patch for a signoff:
332                 if ($line =~ /^\s*Signed-off-by:\s/) {
333                         $signoff++;
334
335                 } elsif ($line =~ /^\s*signed-off-by:/i) {
336                         # This is a signoff, if ugly, so do not double report.
337                         $signoff++;
338                         if (!($line =~ /^\s*Signed-off-by:/)) {
339                                 print "use Signed-off-by:\n";
340                                 print "$herecurr";
341                                 $clean = 0;
342                         }
343                         if ($line =~ /^\s*signed-off-by:\S/i) {
344                                 print "need space after Signed-off-by:\n";
345                                 print "$herecurr";
346                                 $clean = 0;
347                         }
348                 }
349
350 # Check for wrappage within a valid hunk of the file
351                 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
352                         print "patch seems to be corrupt (line wrapped?) [$realcnt]\n";
353                         print "$herecurr";
354                         $clean = 0;
355                 }
356
357 #ignore lines being removed
358                 if ($line=~/^-/) {next;}
359
360 # check we are in a valid source file if not then ignore this hunk
361                 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
362
363 #trailing whitespace
364                 if ($line=~/\+.*\S\s+$/) {
365                         my $herevet = "$here\n" . cat_vet($line) . "\n\n";
366                         print "trailing whitespace\n";
367                         print "$herevet";
368                         $clean = 0;
369                 }
370 #80 column limit
371                 if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
372                         print "line over 80 characters\n";
373                         print "$herecurr";
374                         $clean = 0;
375                 }
376
377 # check we are in a valid source file *.[hc] if not then ignore this hunk
378                 next if ($realfile !~ /\.[hc]$/);
379
380 # at the beginning of a line any tabs must come first and anything
381 # more than 8 must use tabs.
382                 if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s*        \s*/) {
383                         my $herevet = "$here\n" . cat_vet($line) . "\n\n";
384                         print "use tabs not spaces\n";
385                         print "$herevet";
386                         $clean = 0;
387                 }
388
389                 #
390                 # The rest of our checks refer specifically to C style
391                 # only apply those _outside_ comments.
392                 #
393                 next if ($in_comment);
394
395                 # Remove comments from the line before processing.
396                 $line =~ s@/\*.*\*/@@g;
397                 $line =~ s@/\*.*@@;
398                 $line =~ s@.*\*/@@;
399
400                 #
401                 # Checks which may be anchored in the context.
402                 #
403
404                 # Check for switch () and associated case and default
405                 # statements should be at the same indent.
406                 if ($line=~/\bswitch\s*\(.*\)/) {
407                         my $err = '';
408                         my $sep = '';
409                         my @ctx = ctx_block_outer($linenr, $realcnt);
410                         shift(@ctx);
411                         for my $ctx (@ctx) {
412                                 my ($clen, $cindent) = line_stats($ctx);
413                                 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
414                                                         $indent != $cindent) {
415                                         $err .= "$sep$ctx\n";
416                                         $sep = '';
417                                 } else {
418                                         $sep = "[...]\n";
419                                 }
420                         }
421                         if ($err ne '') {
422                                 print "switch and case should be at the same indent\n";
423                                 print "$here\n$line\n$err\n";
424                                 $clean = 0;
425                         }
426                 }
427
428 #ignore lines not being added
429                 if ($line=~/^[^\+]/) {next;}
430
431                 #
432                 # Checks which are anchored on the added line.
433                 #
434
435 # no C99 // comments
436                 if ($line =~ m{//}) {
437                         print "do not use C99 // comments\n";
438                         print "$herecurr";
439                         $clean = 0;
440                 }
441                 # Remove C99 comments.
442                 $line =~ s@//.*@@;
443
444                 # Standardise the strings and chars within the input
445                 # to simplify matching.
446                 $line = sanitise_line($line);
447
448 #EXPORT_SYMBOL should immediately follow its function closing }.
449                 if (($line =~ /EXPORT_SYMBOL.*\(.*\)/) ||
450                     ($line =~ /EXPORT_UNUSED_SYMBOL.*\(.*\)/)) {
451                         if (($prevline !~ /^}/) &&
452                            ($prevline !~ /^\+}/) &&
453                            ($prevline !~ /^ }/)) {
454                                 print "EXPORT_SYMBOL(func); should immediately follow its function\n";
455                                 print "$herecurr";
456                                 $clean = 0;
457                         }
458                 }
459
460                 # check for static initialisers.
461                 if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
462                         print "do not initialise statics to 0 or NULL\n";
463                         print "$herecurr";
464                         $clean = 0;
465                 }
466
467                 # check for new typedefs.
468                 if ($line=~/\s*typedef\s/) {
469                         print "do not add new typedefs\n";
470                         print "$herecurr";
471                         $clean = 0;
472                 }
473
474 # * goes on variable not on type
475                 my $type = '(?:char|short|int|long|unsigned|float|double|' .
476                            'struct\s+[A-Za-z\d_]+|' .
477                            'union\s+[A-Za-z\d_]+)';
478
479                 if ($line =~ m{[A-Za-z\d_]+(\*+) [A-Za-z\d_]+}) {
480                         print "\"foo$1 bar\" should be \"foo $1bar\"\n";
481                         print "$herecurr";
482                         $clean = 0;
483                 }
484                 if ($line =~ m{$type (\*) [A-Za-z\d_]+} ||
485                     $line =~ m{[A-Za-z\d_]+ (\*\*+) [A-Za-z\d_]+}) {
486                         print "\"foo $1 bar\" should be \"foo $1bar\"\n";
487                         print "$herecurr";
488                         $clean = 0;
489                 }
490                 if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_](\*+)\)}) {
491                         print "\"(foo$1)\" should be \"(foo $1)\"\n";
492                         print "$herecurr";
493                         $clean = 0;
494                 }
495                 if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_]\s+(\*+)\s+\)}) {
496                         print "\"(foo $1 )\" should be \"(foo $1)\"\n";
497                         print "$herecurr";
498                         $clean = 0;
499                 }
500
501 # # no BUG() or BUG_ON()
502 #               if ($line =~ /\b(BUG|BUG_ON)\b/) {
503 #                       print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
504 #                       print "$herecurr";
505 #                       $clean = 0;
506 #               }
507
508 # printk should use KERN_* levels.  Note that follow on printk's on the
509 # same line do not need a level, so we use the current block context
510 # to try and find and validate the current printk.  In summary the current
511 # printk includes all preceeding printk's which have no newline on the end.
512 # we assume the first bad printk is the one to report.
513                 if ($line =~ /\bprintk\((?!KERN_)/) {
514                         my $ok = 0;
515                         for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
516                                 #print "CHECK<$lines[$ln - 1]\n";
517                                 # we have a preceeding printk if it ends
518                                 # with "\n" ignore it, else it is to blame
519                                 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
520                                         if ($rawlines[$ln - 1] !~ m{\\n"}) {
521                                                 $ok = 1;
522                                         }
523                                         last;
524                                 }
525                         }
526                         if ($ok == 0) {
527                                 print "printk() should include KERN_ facility level\n";
528                                 print "$herecurr";
529                                 $clean = 0;
530                         }
531                 }
532
533 #function brace can't be on same line, except for #defines of do while, or if closed on same line
534                 if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and
535                     !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
536                         print "braces following function declarations go on the next line\n";
537                         print "$herecurr";
538                         $clean = 0;
539                 }
540                 # Note we expand the line with the leading + as the real
541                 # line will be displayed with the leading + and the tabs
542                 # will therefore also expand that way.
543                 my $opline = $line;
544                 $opline = expand_tabs($opline);
545                 $opline =~ s/^./ /;
546                 if (!($line=~/\#\s*include/)) {
547                         # Check operator spacing.
548                         my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
549                         my $off = 0;
550                         for (my $n = 0; $n < $#elements; $n += 2) {
551                                 $off += length($elements[$n]);
552
553                                 my $a = '';
554                                 $a = 'V' if ($elements[$n] ne '');
555                                 $a = 'W' if ($elements[$n] =~ /\s$/);
556                                 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
557                                 $a = 'O' if ($elements[$n] eq '');
558                                 $a = 'E' if ($elements[$n] eq '' && $n == 0);
559
560                                 my $op = $elements[$n + 1];
561
562                                 my $c = '';
563                                 if (defined $elements[$n + 2]) {
564                                         $c = 'V' if ($elements[$n + 2] ne '');
565                                         $c = 'W' if ($elements[$n + 2] =~ /^\s/);
566                                         $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
567                                         $c = 'O' if ($elements[$n + 2] eq '');
568                                 } else {
569                                         $c = 'E';
570                                 }
571
572                                 # Pick up the preceeding and succeeding characters.
573                                 my $ca = substr($opline, $off - 1, 1);
574                                 my $cc = '';
575                                 if (length($opline) > ($off + length($elements[$n]))) {
576                                         $cc = substr($opline, $off + 1 + length($elements[$n]), 1);
577                                 }
578
579                                 my $ctx = "${a}x${c}";
580
581                                 my $at = "(ctx:$ctx)";
582
583                                 my $ptr = (" " x $off) . "^";
584                                 my $hereptr = "$hereline$ptr\n\n";
585
586                                 ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
587
588                                 # We need ; as an operator.  // is a comment.
589                                 if ($op eq ';' or $op eq '//') {
590
591                                 # -> should have no spaces
592                                 } elsif ($op eq '->') {
593                                         if ($ctx =~ /Wx.|.xW/) {
594                                                 print "no spaces around that '$op' $at\n";
595                                                 print "$hereptr";
596                                                 $clean = 0;
597                                         }
598
599                                 # , must have a space on the right.
600                                 } elsif ($op eq ',') {
601                                         if ($ctx !~ /.xW|.xE/) {
602                                                 print "need space after that '$op' $at\n";
603                                                 print "$hereptr";
604                                                 $clean = 0;
605                                         }
606
607                                 # unary ! and unary ~ are allowed no space on the right
608                                 } elsif ($op eq '!' or $op eq '~') {
609                                         if ($ctx !~ /[WOEB]x./) {
610                                                 print "need space before that '$op' $at\n";
611                                                 print "$hereptr";
612                                                 $clean = 0;
613                                         }
614                                         if ($ctx =~ /.xW/) {
615                                                 print "no space after that '$op' $at\n";
616                                                 print "$hereptr";
617                                                 $clean = 0;
618                                         }
619
620                                 # unary ++ and unary -- are allowed no space on one side.
621                                 } elsif ($op eq '++' or $op eq '--') {
622                                         if ($ctx !~ /[WOB]x[^W]|[^W]x[WOB]/) {
623                                                 print "need space one side of that '$op' $at\n";
624                                                 print "$hereptr";
625                                                 $clean = 0;
626                                         }
627
628                                 # & is both unary and binary
629                                 # unary:
630                                 #       a &b
631                                 # binary (consistent spacing):
632                                 #       a&b             OK
633                                 #       a & b           OK
634                                 #
635                                 # boiling down to: if there is a space on the right then there
636                                 # should be one on the left.
637                                 #
638                                 # - is the same
639                                 #
640                                 } elsif ($op eq '&' or $op eq '-') {
641                                         if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
642                                                 print "need space before that '$op' $at\n";
643                                                 print "$hereptr";
644                                                 $clean = 0;
645                                         }
646
647                                 # * is the same as & only adding:
648                                 # type:
649                                 #       (foo *)
650                                 #       (foo **)
651                                 #
652                                 } elsif ($op eq '*') {
653                                         if ($ca eq '*') {
654                                                 if ($cc =~ /\s/) {
655                                                         print "no space after that '$op' $at\n";
656                                                         print "$hereptr";
657                                                         $clean = 0;
658                                                 }
659                                         } elsif ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB/) {
660                                                 print "need space before that '$op' $at\n";
661                                                 print "$hereptr";
662                                                 $clean = 0;
663                                         }
664
665                                 # << and >> may either have or not have spaces both sides
666                                 } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
667                                          $op eq '^' or $op eq '|')
668                                 {
669                                         if ($ctx !~ /VxV|WxW|VxE|WxE/) {
670                                                 print "need consistent spacing around '$op' $at\n";
671                                                 print "$hereptr";
672                                                 $clean = 0;
673                                         }
674
675                                 # All the others need spaces both sides.
676                                 } elsif ($ctx !~ /[EW]x[WE]/) {
677                                         print "need spaces around that '$op' $at\n";
678                                         print "$hereptr";
679                                         $clean = 0;
680                                 }
681                                 $off += length($elements[$n + 1]);
682                         }
683                 }
684
685 #need space before brace following if, while, etc
686                 if ($line=~/\(.*\){/) {
687                         print "need a space before the brace\n";
688                         print "$herecurr";
689                         $clean = 0;
690                 }
691
692 #goto labels aren't indented, allow a single space however
693                 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
694                    !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
695                         print "labels should not be indented\n";
696                         print "$herecurr";
697                         $clean = 0;
698                 }
699
700 # Need a space before open parenthesis after if, while etc
701                 if ($line=~/\b(if|while|for|switch)\(/) {
702                         print "need a space before the open parenthesis\n";
703                         print "$herecurr";
704                         $clean = 0;
705                 }
706
707 # Check for illegal assignment in if conditional.
708                 if ($line=~/\b(if|while)\s*\(.*[^<>!=]=[^=].*\)/) {
709                         #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
710                         print "do not use assignment in condition\n";
711                         print "$herecurr";
712                         $clean = 0;
713                 }
714
715                 # Check for }<nl>else {, these must be at the same
716                 # indent level to be relevant to each other.
717                 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
718                                                 $previndent == $indent) {
719                         print "else should follow close brace\n";
720                         print "$hereprev";
721                         $clean = 0;
722                 }
723
724 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
725 #               if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
726 #                   print "No studly caps, use _\n";
727 #                   print "$herecurr";
728 #                   $clean = 0;
729 #               }
730
731 #no spaces allowed after \ in define
732                 if ($line=~/\#define.*\\\s$/) {
733                         print("Whitepspace after \\ makes next lines useless\n");
734                         print "$herecurr";
735                         $clean = 0;
736                 }
737
738 #warn if <asm/foo.h> is #included and <linux/foo.h> is available.
739                 if ($tree && $line =~ qr|\s*\#\s*include\s*\<asm\/(.*)\.h\>|) {
740                         my $checkfile = "include/linux/$1.h";
741                         if (-f $checkfile) {
742                                 print "Use #include <linux/$1.h> instead of <asm/$1.h>\n";
743                                 print $herecurr;
744                                 $clean = 0;
745                         }
746                 }
747
748 #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
749                 if ($prevline=~/\b(if|while|for|switch)\s*\(/) {
750                         my @opened = $prevline=~/\(/g;
751                         my @closed = $prevline=~/\)/g;
752                         my $nr_line = $linenr;
753                         my $remaining = $realcnt - 1;
754                         my $next_line = $line;
755                         my $extra_lines = 0;
756                         my $display_segment = $prevline;
757
758                         while ($remaining > 0 && scalar @opened > scalar @closed) {
759                                 $prevline .= $next_line;
760                                 $display_segment .= "\n" . $next_line;
761                                 $next_line = $lines[$nr_line];
762                                 $nr_line++;
763                                 $remaining--;
764
765                                 @opened = $prevline=~/\(/g;
766                                 @closed = $prevline=~/\)/g;
767                         }
768
769                         if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and
770                            !($next_line=~/\b(if|while|for)/) and !($next_line=~/\#define.*do.*while/)) {
771                                 print "That { should be on the previous line\n";
772                                 print "$here\n$display_segment\n$next_line\n\n";
773                                 $clean = 0;
774                         }
775                 }
776
777 #multiline macros should be enclosed in a do while loop
778                 if (($prevline=~/\#define.*\\/) and !($prevline=~/do\s+{/) and
779                    !($prevline=~/\(\{/) and ($line=~/;\s*\\/) and
780                    !($line=~/do.*{/) and !($line=~/\(\{/)) {
781                         print "Macros with multiple statements should be enclosed in a do - while loop\n";
782                         print "$hereprev";
783                         $clean = 0;
784                 }
785
786 # don't include deprecated include files
787                 for my $inc (@dep_includes) {
788                         if ($line =~ m@\#\s*include\s*\<$inc>@) {
789                                 print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n";
790                                 print "$herecurr";
791                                 $clean = 0;
792                         }
793                 }
794
795 # don't use deprecated functions
796                 for my $func (@dep_functions) {
797                         if ($line =~ /\b$func\b/) {
798                                 print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n";
799                                 print "$herecurr";
800                                 $clean = 0;
801                         }
802                 }
803
804 # no volatiles please
805                 if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
806                         print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n";
807                         print "$herecurr";
808                         $clean = 0;
809                 }
810
811 # warn about #if 0
812                 if ($line =~ /^.#\s*if\s+0\b/) {
813                         print "#if 0 -- if this code redundant remove it\n";
814                         print "$herecurr";
815                         $clean = 0;
816                 }
817
818 # warn about #ifdefs in C files
819 #               if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
820 #                       print "#ifdef in C files should be avoided\n";
821 #                       print "$herecurr";
822 #                       $clean = 0;
823 #               }
824
825 # check for spinlock_t definitions without a comment.
826                 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
827                         my $which = $1;
828                         if (!ctx_has_comment($first_line, $linenr)) {
829                                 print "$1 definition without comment\n";
830                                 print "$herecurr";
831                                 $clean = 0;
832                         }
833                 }
834 # check for memory barriers without a comment.
835                 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
836                         if (!ctx_has_comment($first_line, $linenr)) {
837                                 print "memory barrier without comment\n";
838                                 print "$herecurr";
839                                 $clean = 0;
840                         }
841                 }
842 # check of hardware specific defines
843                 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
844                         print "architecture specific defines should be avoided\n";
845                         print "$herecurr";
846                         $clean = 0;
847                 }
848         }
849
850         if ($chk_patch && !$is_patch) {
851                 $clean = 0;
852                 print "Does not appear to be a unified-diff format patch\n";
853         }
854         if ($is_patch && $chk_signoff && $signoff == 0) {
855                 $clean = 0;
856                 print "Missing Signed-off-by: line(s)\n";
857         }
858
859         if ($clean == 1 && $quiet == 0) {
860                 print "Your patch has no obvious style problems and is ready for submission.\n"
861         }
862         if ($clean == 0 && $quiet == 0) {
863                 print "Your patch has style problems, please review.  If any of these errors\n";
864                 print "are false positives report them to the maintainer, see\n";
865                 print "CHECKPATCH in MAINTAINERS.\n";
866         }
867         return $clean;
868 }