Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / scripts / checkpatch.pl
blob416706b3a0931907a08605cbd3016997f2d257fb
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
7 use strict;
9 my $P = $0;
10 $P =~ s@.*/@@g;
12 <<<<<<< HEAD:scripts/checkpatch.pl
13 my $V = '0.14';
14 =======
15 my $V = '0.15';
16 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
18 use Getopt::Long qw(:config no_auto_abbrev);
20 my $quiet = 0;
21 my $tree = 1;
22 my $chk_signoff = 1;
23 my $chk_patch = 1;
24 my $tst_type = 0;
25 my $emacs = 0;
26 my $terse = 0;
27 my $file = 0;
28 my $check = 0;
29 my $summary = 1;
30 my $mailback = 0;
31 my $summary_file = 0;
32 my $root;
33 my %debug;
34 GetOptions(
35 'q|quiet+' => \$quiet,
36 'tree!' => \$tree,
37 'signoff!' => \$chk_signoff,
38 'patch!' => \$chk_patch,
39 'emacs!' => \$emacs,
40 'terse!' => \$terse,
41 'file!' => \$file,
42 'subjective!' => \$check,
43 'strict!' => \$check,
44 'root=s' => \$root,
45 'summary!' => \$summary,
46 'mailback!' => \$mailback,
47 'summary-file!' => \$summary_file,
49 'debug=s' => \%debug,
50 'test-type!' => \$tst_type,
51 ) or exit;
53 my $exit = 0;
55 if ($#ARGV < 0) {
56 print "usage: $P [options] patchfile\n";
57 print "version: $V\n";
58 print "options: -q => quiet\n";
59 print " --no-tree => run without a kernel tree\n";
60 print " --terse => one line per report\n";
61 print " --emacs => emacs compile window format\n";
62 print " --file => check a source file\n";
63 print " --strict => enable more subjective tests\n";
64 print " --root => path to the kernel tree root\n";
65 print " --no-summary => suppress the per-file summary\n";
66 print " --summary-file => include the filename in summary\n";
67 exit(1);
70 my $dbg_values = 0;
71 my $dbg_possible = 0;
72 for my $key (keys %debug) {
73 eval "\${dbg_$key} = '$debug{$key}';"
76 if ($terse) {
77 $emacs = 1;
78 $quiet++;
81 if ($tree) {
82 if (defined $root) {
83 if (!top_of_kernel_tree($root)) {
84 die "$P: $root: --root does not point at a valid tree\n";
86 } else {
87 if (top_of_kernel_tree('.')) {
88 $root = '.';
89 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
90 top_of_kernel_tree($1)) {
91 $root = $1;
95 if (!defined $root) {
96 print "Must be run from the top-level dir. of a kernel tree\n";
97 exit(2);
101 my $emitted_corrupt = 0;
103 our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
104 our $Storage = qr{extern|static|asmlinkage};
105 our $Sparse = qr{
106 __user|
107 __kernel|
108 __force|
109 __iomem|
110 __must_check|
111 __init_refok|
112 <<<<<<< HEAD:scripts/checkpatch.pl
113 __kprobes|
114 fastcall
115 =======
116 __kprobes
117 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
119 our $Attribute = qr{
120 const|
121 __read_mostly|
122 __kprobes|
123 __(?:mem|cpu|dev|)(?:initdata|init)
125 our $Inline = qr{inline|__always_inline|noinline};
126 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
127 our $Lval = qr{$Ident(?:$Member)*};
129 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
130 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
131 our $Operators = qr{
132 <=|>=|==|!=|
133 =>|->|<<|>>|<|>|!|~|
134 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
137 our $NonptrType;
138 our $Type;
139 our $Declare;
141 our @typeList = (
142 qr{void},
143 qr{char},
144 qr{short},
145 qr{int},
146 qr{long},
147 qr{unsigned},
148 qr{float},
149 qr{double},
150 qr{bool},
151 qr{long\s+int},
152 qr{long\s+long},
153 qr{long\s+long\s+int},
154 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
155 qr{struct\s+$Ident},
156 qr{union\s+$Ident},
157 qr{enum\s+$Ident},
158 qr{${Ident}_t},
159 qr{${Ident}_handler},
160 qr{${Ident}_handler_fn},
163 sub build_types {
164 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
165 $NonptrType = qr{
167 (?:const\s+)?
168 (?:unsigned\s+)?
169 <<<<<<< HEAD:scripts/checkpatch.pl
170 $all
171 =======
173 $all|
174 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
176 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
177 (?:\s+$Sparse|\s+const)*
180 $Type = qr{
181 \b$NonptrType\b
182 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
183 (?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
185 $Declare = qr{(?:$Storage\s+)?$Type};
187 build_types();
189 $chk_signoff = 0 if ($file);
191 my @dep_includes = ();
192 my @dep_functions = ();
193 my $removal = "Documentation/feature-removal-schedule.txt";
194 if ($tree && -f "$root/$removal") {
195 open(REMOVE, "<$root/$removal") ||
196 die "$P: $removal: open failed - $!\n";
197 while (<REMOVE>) {
198 if (/^Check:\s+(.*\S)/) {
199 for my $entry (split(/[, ]+/, $1)) {
200 if ($entry =~ m@include/(.*)@) {
201 push(@dep_includes, $1);
203 } elsif ($entry !~ m@/@) {
204 push(@dep_functions, $entry);
211 my @rawlines = ();
212 my @lines = ();
213 my $vname;
214 for my $filename (@ARGV) {
215 if ($file) {
216 open(FILE, "diff -u /dev/null $filename|") ||
217 die "$P: $filename: diff failed - $!\n";
218 } else {
219 open(FILE, "<$filename") ||
220 die "$P: $filename: open failed - $!\n";
222 if ($filename eq '-') {
223 $vname = 'Your patch';
224 } else {
225 $vname = $filename;
227 while (<FILE>) {
228 chomp;
229 push(@rawlines, $_);
231 close(FILE);
232 if (!process($filename)) {
233 $exit = 1;
235 @rawlines = ();
236 @lines = ();
239 exit($exit);
241 sub top_of_kernel_tree {
242 my ($root) = @_;
244 my @tree_check = (
245 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
246 "README", "Documentation", "arch", "include", "drivers",
247 "fs", "init", "ipc", "kernel", "lib", "scripts",
250 foreach my $check (@tree_check) {
251 if (! -e $root . '/' . $check) {
252 return 0;
255 return 1;
258 sub expand_tabs {
259 my ($str) = @_;
261 my $res = '';
262 my $n = 0;
263 for my $c (split(//, $str)) {
264 if ($c eq "\t") {
265 $res .= ' ';
266 $n++;
267 for (; ($n % 8) != 0; $n++) {
268 $res .= ' ';
270 next;
272 $res .= $c;
273 $n++;
276 return $res;
278 sub copy_spacing {
279 my ($str) = @_;
281 my $res = '';
282 for my $c (split(//, $str)) {
283 if ($c eq "\t") {
284 $res .= $c;
285 } else {
286 $res .= ' ';
290 return $res;
293 sub line_stats {
294 my ($line) = @_;
296 # Drop the diff line leader and expand tabs
297 $line =~ s/^.//;
298 $line = expand_tabs($line);
300 # Pick the indent from the front of the line.
301 my ($white) = ($line =~ /^(\s*)/);
303 return (length($line), length($white));
306 sub sanitise_line {
307 my ($line) = @_;
309 my $res = '';
310 my $l = '';
312 my $quote = '';
313 my $qlen = 0;
315 foreach my $c (split(//, $line)) {
316 # The second backslash of a pair is not a "quote".
317 if ($l eq "\\" && $c eq "\\") {
318 $c = 'X';
320 if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
321 if ($quote eq '') {
322 $quote = $c;
323 $res .= $c;
324 $l = $c;
325 $qlen = 0;
326 next;
327 } elsif ($quote eq $c) {
328 $quote = '';
331 if ($quote eq "'" && $qlen > 1) {
332 $quote = '';
334 if ($quote && $c ne "\t") {
335 $res .= "X";
336 $qlen++;
337 } else {
338 $res .= $c;
341 $l = $c;
344 # Clear out the comments.
345 while ($res =~ m@(/\*.*?\*/)@g) {
346 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
348 if ($res =~ m@(/\*.*)@) {
349 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
351 if ($res =~ m@^.(.*\*/)@) {
352 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
355 # The pathname on a #include may be surrounded by '<' and '>'.
356 if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
357 my $clean = 'X' x length($1);
358 $res =~ s@\<.*\>@<$clean>@;
360 # The whole of a #error is a string.
361 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
362 my $clean = 'X' x length($1);
363 $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
366 return $res;
369 sub ctx_statement_block {
370 my ($linenr, $remain, $off) = @_;
371 my $line = $linenr - 1;
372 my $blk = '';
373 my $soff = $off;
374 my $coff = $off - 1;
376 my $loff = 0;
378 my $type = '';
379 my $level = 0;
380 <<<<<<< HEAD:scripts/checkpatch.pl
381 =======
382 my $p;
383 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
384 my $c;
385 my $len = 0;
387 my $remainder;
388 while (1) {
389 #warn "CSB: blk<$blk>\n";
390 # If we are about to drop off the end, pull in more
391 # context.
392 if ($off >= $len) {
393 for (; $remain > 0; $line++) {
394 next if ($lines[$line] =~ /^-/);
395 $remain--;
396 $loff = $len;
397 $blk .= $lines[$line] . "\n";
398 $len = length($blk);
399 $line++;
400 last;
402 # Bail if there is no further context.
403 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
404 if ($off >= $len) {
405 last;
408 <<<<<<< HEAD:scripts/checkpatch.pl
409 =======
410 $p = $c;
411 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
412 $c = substr($blk, $off, 1);
413 $remainder = substr($blk, $off);
415 #warn "CSB: c<$c> type<$type> level<$level>\n";
416 # Statement ends at the ';' or a close '}' at the
417 # outermost level.
418 if ($level == 0 && $c eq ';') {
419 last;
422 # An else is really a conditional as long as its not else if
423 <<<<<<< HEAD:scripts/checkpatch.pl
424 if ($level == 0 && $remainder =~ /(\s+else)(?:\s|{)/ &&
425 $remainder !~ /\s+else\s+if\b/) {
426 =======
427 if ($level == 0 && (!defined($p) || $p =~ /(?:\s|\})/) &&
428 $remainder =~ /(else)(?:\s|{)/ &&
429 $remainder !~ /else\s+if\b/) {
430 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
431 $coff = $off + length($1);
434 if (($type eq '' || $type eq '(') && $c eq '(') {
435 $level++;
436 $type = '(';
438 if ($type eq '(' && $c eq ')') {
439 $level--;
440 $type = ($level != 0)? '(' : '';
442 if ($level == 0 && $coff < $soff) {
443 $coff = $off;
446 if (($type eq '' || $type eq '{') && $c eq '{') {
447 $level++;
448 $type = '{';
450 if ($type eq '{' && $c eq '}') {
451 $level--;
452 $type = ($level != 0)? '{' : '';
454 if ($level == 0) {
455 last;
458 $off++;
460 if ($off == $len) {
461 $line++;
462 $remain--;
465 my $statement = substr($blk, $soff, $off - $soff + 1);
466 my $condition = substr($blk, $soff, $coff - $soff + 1);
468 #warn "STATEMENT<$statement>\n";
469 #warn "CONDITION<$condition>\n";
471 #print "off<$off> loff<$loff>\n";
473 return ($statement, $condition,
474 $line, $remain + 1, $off - $loff + 1, $level);
477 <<<<<<< HEAD:scripts/checkpatch.pl
478 =======
479 sub statement_lines {
480 my ($stmt) = @_;
482 # Strip the diff line prefixes and rip blank lines at start and end.
483 $stmt =~ s/(^|\n)./$1/g;
484 $stmt =~ s/^\s*//;
485 $stmt =~ s/\s*$//;
487 my @stmt_lines = ($stmt =~ /\n/g);
489 return $#stmt_lines + 2;
492 sub statement_rawlines {
493 my ($stmt) = @_;
495 my @stmt_lines = ($stmt =~ /\n/g);
497 return $#stmt_lines + 2;
500 sub statement_block_size {
501 my ($stmt) = @_;
503 $stmt =~ s/(^|\n)./$1/g;
504 $stmt =~ s/^\s*{//;
505 $stmt =~ s/}\s*$//;
506 $stmt =~ s/^\s*//;
507 $stmt =~ s/\s*$//;
509 my @stmt_lines = ($stmt =~ /\n/g);
510 my @stmt_statements = ($stmt =~ /;/g);
512 my $stmt_lines = $#stmt_lines + 2;
513 my $stmt_statements = $#stmt_statements + 1;
515 if ($stmt_lines > $stmt_statements) {
516 return $stmt_lines;
517 } else {
518 return $stmt_statements;
522 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
523 sub ctx_statement_full {
524 my ($linenr, $remain, $off) = @_;
525 my ($statement, $condition, $level);
527 my (@chunks);
529 <<<<<<< HEAD:scripts/checkpatch.pl
530 =======
531 # Grab the first conditional/block pair.
532 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
533 ($statement, $condition, $linenr, $remain, $off, $level) =
534 ctx_statement_block($linenr, $remain, $off);
535 #print "F: c<$condition> s<$statement>\n";
536 <<<<<<< HEAD:scripts/checkpatch.pl
537 =======
538 push(@chunks, [ $condition, $statement ]);
539 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
540 return ($level, $linenr, @chunks);
543 # Pull in the following conditional/block pairs and see if they
544 # could continue the statement.
545 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
546 for (;;) {
547 <<<<<<< HEAD:scripts/checkpatch.pl
548 push(@chunks, [ $condition, $statement ]);
549 last if (!($remain > 0 && $condition =~ /^.\s*(?:if|else|do)/));
550 =======
551 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
552 ($statement, $condition, $linenr, $remain, $off, $level) =
553 ctx_statement_block($linenr, $remain, $off);
554 <<<<<<< HEAD:scripts/checkpatch.pl
555 #print "C: c<$condition> s<$statement>\n";
556 =======
557 #print "C: c<$condition> s<$statement> remain<$remain>\n";
558 last if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:else|do)\b/s));
559 #print "C: push\n";
560 push(@chunks, [ $condition, $statement ]);
561 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
564 return ($level, $linenr, @chunks);
567 sub ctx_block_get {
568 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
569 my $line;
570 my $start = $linenr - 1;
571 my $blk = '';
572 my @o;
573 my @c;
574 my @res = ();
576 my $level = 0;
577 for ($line = $start; $remain > 0; $line++) {
578 next if ($rawlines[$line] =~ /^-/);
579 $remain--;
581 $blk .= $rawlines[$line];
582 foreach my $c (split(//, $rawlines[$line])) {
583 ##print "C<$c>L<$level><$open$close>O<$off>\n";
584 if ($off > 0) {
585 $off--;
586 next;
589 if ($c eq $close && $level > 0) {
590 $level--;
591 last if ($level == 0);
592 } elsif ($c eq $open) {
593 $level++;
597 if (!$outer || $level <= 1) {
598 push(@res, $rawlines[$line]);
601 last if ($level == 0);
604 return ($level, @res);
606 sub ctx_block_outer {
607 my ($linenr, $remain) = @_;
609 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
610 return @r;
612 sub ctx_block {
613 my ($linenr, $remain) = @_;
615 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
616 return @r;
618 sub ctx_statement {
619 my ($linenr, $remain, $off) = @_;
621 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
622 return @r;
624 sub ctx_block_level {
625 my ($linenr, $remain) = @_;
627 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
629 sub ctx_statement_level {
630 my ($linenr, $remain, $off) = @_;
632 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
635 sub ctx_locate_comment {
636 my ($first_line, $end_line) = @_;
638 # Catch a comment on the end of the line itself.
639 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
640 return $current_comment if (defined $current_comment);
642 # Look through the context and try and figure out if there is a
643 # comment.
644 my $in_comment = 0;
645 $current_comment = '';
646 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
647 my $line = $rawlines[$linenr - 1];
648 #warn " $line\n";
649 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
650 $in_comment = 1;
652 if ($line =~ m@/\*@) {
653 $in_comment = 1;
655 if (!$in_comment && $current_comment ne '') {
656 $current_comment = '';
658 $current_comment .= $line . "\n" if ($in_comment);
659 if ($line =~ m@\*/@) {
660 $in_comment = 0;
664 chomp($current_comment);
665 return($current_comment);
667 sub ctx_has_comment {
668 my ($first_line, $end_line) = @_;
669 my $cmt = ctx_locate_comment($first_line, $end_line);
671 ##print "LINE: $rawlines[$end_line - 1 ]\n";
672 ##print "CMMT: $cmt\n";
674 return ($cmt ne '');
677 sub cat_vet {
678 my ($vet) = @_;
679 my ($res, $coded);
681 $res = '';
682 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
683 $res .= $1;
684 if ($2 ne '') {
685 $coded = sprintf("^%c", unpack('C', $2) + 64);
686 $res .= $coded;
689 $res =~ s/$/\$/;
691 return $res;
694 my $av_preprocessor = 0;
695 <<<<<<< HEAD:scripts/checkpatch.pl
696 my $av_paren = 0;
697 =======
698 my $av_pending;
699 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
700 my @av_paren_type;
702 sub annotate_reset {
703 $av_preprocessor = 0;
704 <<<<<<< HEAD:scripts/checkpatch.pl
705 $av_paren = 0;
706 @av_paren_type = ();
707 =======
708 $av_pending = '_';
709 @av_paren_type = ('E');
710 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
713 sub annotate_values {
714 my ($stream, $type) = @_;
716 my $res;
717 my $cur = $stream;
719 print "$stream\n" if ($dbg_values > 1);
721 while (length($cur)) {
722 <<<<<<< HEAD:scripts/checkpatch.pl
723 print " <$type> " if ($dbg_values > 1);
724 =======
725 print " <" . join('', @av_paren_type) .
726 "> <$type> " if ($dbg_values > 1);
727 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
728 if ($cur =~ /^(\s+)/o) {
729 print "WS($1)\n" if ($dbg_values > 1);
730 if ($1 =~ /\n/ && $av_preprocessor) {
731 <<<<<<< HEAD:scripts/checkpatch.pl
732 =======
733 $type = pop(@av_paren_type);
734 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
735 $av_preprocessor = 0;
736 <<<<<<< HEAD:scripts/checkpatch.pl
737 $type = 'N';
738 =======
739 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
742 } elsif ($cur =~ /^($Type)/) {
743 print "DECLARE($1)\n" if ($dbg_values > 1);
744 $type = 'T';
746 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
747 print "DEFINE($1)\n" if ($dbg_values > 1);
748 $av_preprocessor = 1;
749 <<<<<<< HEAD:scripts/checkpatch.pl
750 $av_paren_type[$av_paren] = 'N';
751 =======
752 $av_pending = 'N';
753 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
755 <<<<<<< HEAD:scripts/checkpatch.pl
756 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|elif|endif))/o) {
757 print "PRE($1)\n" if ($dbg_values > 1);
758 =======
759 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
760 print "PRE_START($1)\n" if ($dbg_values > 1);
761 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
762 $av_preprocessor = 1;
763 <<<<<<< HEAD:scripts/checkpatch.pl
764 =======
766 push(@av_paren_type, $type);
767 push(@av_paren_type, $type);
768 $type = 'N';
770 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
771 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
772 $av_preprocessor = 1;
774 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
776 $type = 'N';
778 } elsif ($cur =~ /^(#\s*(?:endif))/o) {
779 print "PRE_END($1)\n" if ($dbg_values > 1);
781 $av_preprocessor = 1;
783 # Assume all arms of the conditional end as this
784 # one does, and continue as if the #endif was not here.
785 pop(@av_paren_type);
786 push(@av_paren_type, $type);
787 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
788 $type = 'N';
790 } elsif ($cur =~ /^(\\\n)/o) {
791 print "PRECONT($1)\n" if ($dbg_values > 1);
793 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
794 print "SIZEOF($1)\n" if ($dbg_values > 1);
795 if (defined $2) {
796 <<<<<<< HEAD:scripts/checkpatch.pl
797 $av_paren_type[$av_paren] = 'V';
798 =======
799 $av_pending = 'V';
800 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
802 $type = 'N';
804 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
805 print "COND($1)\n" if ($dbg_values > 1);
806 <<<<<<< HEAD:scripts/checkpatch.pl
807 $av_paren_type[$av_paren] = 'N';
808 =======
809 $av_pending = 'N';
810 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
811 $type = 'N';
813 } elsif ($cur =~/^(return|case|else)/o) {
814 print "KEYWORD($1)\n" if ($dbg_values > 1);
815 $type = 'N';
817 } elsif ($cur =~ /^(\()/o) {
818 print "PAREN('$1')\n" if ($dbg_values > 1);
819 <<<<<<< HEAD:scripts/checkpatch.pl
820 $av_paren++;
821 =======
822 push(@av_paren_type, $av_pending);
823 $av_pending = '_';
824 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
825 $type = 'N';
827 } elsif ($cur =~ /^(\))/o) {
828 <<<<<<< HEAD:scripts/checkpatch.pl
829 $av_paren-- if ($av_paren > 0);
830 if (defined $av_paren_type[$av_paren]) {
831 $type = $av_paren_type[$av_paren];
832 undef $av_paren_type[$av_paren];
833 =======
834 my $new_type = pop(@av_paren_type);
835 if ($new_type ne '_') {
836 $type = $new_type;
837 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
838 print "PAREN('$1') -> $type\n"
839 if ($dbg_values > 1);
840 } else {
841 print "PAREN('$1')\n" if ($dbg_values > 1);
844 } elsif ($cur =~ /^($Ident)\(/o) {
845 print "FUNC($1)\n" if ($dbg_values > 1);
846 <<<<<<< HEAD:scripts/checkpatch.pl
847 $av_paren_type[$av_paren] = 'V';
848 =======
849 $av_pending = 'V';
850 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
852 } elsif ($cur =~ /^($Ident|$Constant)/o) {
853 print "IDENT($1)\n" if ($dbg_values > 1);
854 $type = 'V';
856 } elsif ($cur =~ /^($Assignment)/o) {
857 print "ASSIGN($1)\n" if ($dbg_values > 1);
858 $type = 'N';
860 <<<<<<< HEAD:scripts/checkpatch.pl
861 } elsif ($cur =~/^(;)/) {
862 =======
863 } elsif ($cur =~/^(;|{|})/) {
864 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
865 print "END($1)\n" if ($dbg_values > 1);
866 $type = 'E';
868 <<<<<<< HEAD:scripts/checkpatch.pl
869 } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) {
870 =======
871 } elsif ($cur =~ /^(;|\?|:|\[)/o) {
872 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
873 print "CLOSE($1)\n" if ($dbg_values > 1);
874 $type = 'N';
876 } elsif ($cur =~ /^($Operators)/o) {
877 print "OP($1)\n" if ($dbg_values > 1);
878 if ($1 ne '++' && $1 ne '--') {
879 $type = 'N';
882 } elsif ($cur =~ /(^.)/o) {
883 print "C($1)\n" if ($dbg_values > 1);
885 if (defined $1) {
886 $cur = substr($cur, length($1));
887 $res .= $type x length($1);
891 return $res;
894 sub possible {
895 my ($possible, $line) = @_;
897 #print "CHECK<$possible>\n";
898 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
899 $possible ne 'goto' && $possible ne 'return' &&
900 $possible ne 'struct' && $possible ne 'enum' &&
901 $possible ne 'case' && $possible ne 'else' &&
902 $possible ne 'typedef') {
903 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
904 push(@typeList, $possible);
905 build_types();
909 my $prefix = '';
911 sub report {
912 my $line = $prefix . $_[0];
914 $line = (split('\n', $line))[0] . "\n" if ($terse);
916 push(our @report, $line);
918 sub report_dump {
919 our @report;
921 sub ERROR {
922 report("ERROR: $_[0]\n");
923 our $clean = 0;
924 our $cnt_error++;
926 sub WARN {
927 report("WARNING: $_[0]\n");
928 our $clean = 0;
929 our $cnt_warn++;
931 sub CHK {
932 if ($check) {
933 report("CHECK: $_[0]\n");
934 our $clean = 0;
935 our $cnt_chk++;
939 sub process {
940 my $filename = shift;
942 my $linenr=0;
943 my $prevline="";
944 my $prevrawline="";
945 my $stashline="";
946 my $stashrawline="";
948 my $length;
949 my $indent;
950 my $previndent=0;
951 my $stashindent=0;
953 our $clean = 1;
954 my $signoff = 0;
955 my $is_patch = 0;
957 our @report = ();
958 our $cnt_lines = 0;
959 our $cnt_error = 0;
960 our $cnt_warn = 0;
961 our $cnt_chk = 0;
963 # Trace the real file/line as we go.
964 my $realfile = '';
965 my $realline = 0;
966 my $realcnt = 0;
967 my $here = '';
968 my $in_comment = 0;
969 my $comment_edge = 0;
970 my $first_line = 0;
972 my $prev_values = 'E';
974 # suppression flags
975 my $suppress_ifbraces = 0;
977 # Pre-scan the patch sanitizing the lines.
978 # Pre-scan the patch looking for any __setup documentation.
980 my @setup_docs = ();
981 my $setup_docs = 0;
982 my $line;
983 foreach my $rawline (@rawlines) {
984 # Standardise the strings and chars within the input to
985 # simplify matching.
986 $line = sanitise_line($rawline);
987 push(@lines, $line);
989 ##print "==>$rawline\n";
990 ##print "-->$line\n";
992 if ($line=~/^\+\+\+\s+(\S+)/) {
993 $setup_docs = 0;
994 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
995 $setup_docs = 1;
997 next;
1000 if ($setup_docs && $line =~ /^\+/) {
1001 push(@setup_docs, $line);
1005 $prefix = '';
1007 foreach my $line (@lines) {
1008 $linenr++;
1010 my $rawline = $rawlines[$linenr - 1];
1012 #extract the filename as it passes
1013 if ($line=~/^\+\+\+\s+(\S+)/) {
1014 $realfile=$1;
1015 $realfile =~ s@^[^/]*/@@;
1016 $in_comment = 0;
1017 next;
1019 #extract the line range in the file after the patch is applied
1020 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1021 $is_patch = 1;
1022 $first_line = $linenr + 1;
1023 $in_comment = 0;
1024 $realline=$1-1;
1025 if (defined $2) {
1026 $realcnt=$3+1;
1027 } else {
1028 $realcnt=1+1;
1030 annotate_reset();
1031 $prev_values = 'E';
1033 $suppress_ifbraces = $linenr - 1;
1034 next;
1037 # track the line number as we move through the hunk, note that
1038 # new versions of GNU diff omit the leading space on completely
1039 # blank context lines so we need to count that too.
1040 if ($line =~ /^( |\+|$)/) {
1041 $realline++;
1042 $realcnt-- if ($realcnt != 0);
1044 # Guestimate if this is a continuing comment. Run
1045 # the context looking for a comment "edge". If this
1046 # edge is a close comment then we must be in a comment
1047 # at context start.
1048 if ($linenr == $first_line) {
1049 my $edge;
1050 for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) {
1051 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
1052 last if (defined $edge);
1054 if (defined $edge && $edge eq '*/') {
1055 $in_comment = 1;
1059 # Guestimate if this is a continuing comment. If this
1060 # is the start of a diff block and this line starts
1061 # ' *' then it is very likely a comment.
1062 if ($linenr == $first_line and $rawline =~ m@^.\s* \*(?:\s|$)@) {
1063 $in_comment = 1;
1066 # Find the last comment edge on _this_ line.
1067 $comment_edge = 0;
1068 while (($rawline =~ m@(/\*|\*/)@g)) {
1069 if ($1 eq '/*') {
1070 $in_comment = 1;
1071 } else {
1072 $in_comment = 0;
1074 $comment_edge = 1;
1077 # Measure the line length and indent.
1078 ($length, $indent) = line_stats($rawline);
1080 # Track the previous line.
1081 ($prevline, $stashline) = ($stashline, $line);
1082 ($previndent, $stashindent) = ($stashindent, $indent);
1083 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1085 #warn "ic<$in_comment> ce<$comment_edge> line<$line>\n";
1087 } elsif ($realcnt == 1) {
1088 $realcnt--;
1091 #make up the handle for any error we report on this line
1092 $here = "#$linenr: " if (!$file);
1093 $here = "#$realline: " if ($file);
1094 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1096 my $hereline = "$here\n$rawline\n";
1097 my $herecurr = "$here\n$rawline\n";
1098 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1100 $prefix = "$filename:$realline: " if ($emacs && $file);
1101 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1102 $cnt_lines++ if ($realcnt != 0);
1104 #check the patch for a signoff:
1105 if ($line =~ /^\s*signed-off-by:/i) {
1106 # This is a signoff, if ugly, so do not double report.
1107 $signoff++;
1108 if (!($line =~ /^\s*Signed-off-by:/)) {
1109 WARN("Signed-off-by: is the preferred form\n" .
1110 $herecurr);
1112 if ($line =~ /^\s*signed-off-by:\S/i) {
1113 WARN("need space after Signed-off-by:\n" .
1114 $herecurr);
1118 # Check for wrappage within a valid hunk of the file
1119 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1120 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1121 $herecurr) if (!$emitted_corrupt++);
1124 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1125 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1126 !($rawline =~ m/^(
1127 [\x09\x0A\x0D\x20-\x7E] # ASCII
1128 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1129 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1130 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1131 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1132 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1133 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1134 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1135 )*$/x )) {
1136 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr);
1139 #ignore lines being removed
1140 if ($line=~/^-/) {next;}
1142 # check we are in a valid source file if not then ignore this hunk
1143 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1145 #trailing whitespace
1146 if ($line =~ /^\+.*\015/) {
1147 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1148 ERROR("DOS line endings\n" . $herevet);
1150 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1151 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1152 ERROR("trailing whitespace\n" . $herevet);
1154 #80 column limit
1155 if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
1156 WARN("line over 80 characters\n" . $herecurr);
1159 # check for adding lines without a newline.
1160 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1161 WARN("adding a line without newline at end of file\n" . $herecurr);
1164 # check we are in a valid source file *.[hc] if not then ignore this hunk
1165 next if ($realfile !~ /\.[hc]$/);
1167 # at the beginning of a line any tabs must come first and anything
1168 # more than 8 must use tabs.
1169 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1170 $rawline =~ /^\+\s* \s*/) {
1171 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1172 ERROR("use tabs not spaces\n" . $herevet);
1175 # check for RCS/CVS revision markers
1176 <<<<<<< HEAD:scripts/checkpatch.pl
1177 if ($rawline =~ /\$(Revision|Log|Id)(?:\$|)/) {
1178 =======
1179 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1180 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1181 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1184 # The rest of our checks refer specifically to C style
1185 # only apply those _outside_ comments. Only skip
1186 # lines in the middle of comments.
1187 next if (!$comment_edge && $in_comment);
1189 # Check for potential 'bare' types
1190 if ($realcnt) {
1191 <<<<<<< HEAD:scripts/checkpatch.pl
1192 =======
1193 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1194 $s =~ s/\n./ /g;
1195 $s =~ s/{.*$//;
1197 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1198 # Ignore goto labels.
1199 <<<<<<< HEAD:scripts/checkpatch.pl
1200 if ($line =~ /$Ident:\*$/) {
1201 =======
1202 if ($s =~ /$Ident:\*$/) {
1203 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1205 # Ignore functions being called
1206 <<<<<<< HEAD:scripts/checkpatch.pl
1207 } elsif ($line =~ /^.\s*$Ident\s*\(/) {
1208 =======
1209 } elsif ($s =~ /^.\s*$Ident\s*\(/) {
1210 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1212 # definitions in global scope can only start with types
1213 <<<<<<< HEAD:scripts/checkpatch.pl
1214 } elsif ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
1215 possible($1, $line);
1216 =======
1217 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
1218 possible($1, $s);
1219 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1221 # declarations always start with types
1222 <<<<<<< HEAD:scripts/checkpatch.pl
1223 } elsif ($prev_values eq 'E' && $line =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
1224 possible($1);
1225 =======
1226 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
1227 possible($1, $s);
1228 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1231 # any (foo ... *) is a pointer cast, and foo is a type
1232 <<<<<<< HEAD:scripts/checkpatch.pl
1233 while ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
1234 possible($1, $line);
1235 =======
1236 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
1237 possible($1, $s);
1238 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1241 # Check for any sort of function declaration.
1242 # int foo(something bar, other baz);
1243 # void (*store_gdt)(x86_descr_ptr *);
1244 <<<<<<< HEAD:scripts/checkpatch.pl
1245 if ($prev_values eq 'E' && $line =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
1246 =======
1247 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
1248 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1249 my ($name_len) = length($1);
1250 <<<<<<< HEAD:scripts/checkpatch.pl
1251 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len);
1252 my $ctx = join("\n", @ctx);
1253 =======
1254 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1256 <<<<<<< HEAD:scripts/checkpatch.pl
1257 $ctx =~ s/\n.//;
1258 =======
1259 my $ctx = $s;
1260 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1261 substr($ctx, 0, $name_len + 1) = '';
1262 $ctx =~ s/\)[^\)]*$//;
1263 <<<<<<< HEAD:scripts/checkpatch.pl
1264 =======
1266 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1267 for my $arg (split(/\s*,\s*/, $ctx)) {
1268 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1270 <<<<<<< HEAD:scripts/checkpatch.pl
1271 possible($1, $line);
1272 =======
1273 possible($1, $s);
1274 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1282 # Checks which may be anchored in the context.
1285 # Check for switch () and associated case and default
1286 # statements should be at the same indent.
1287 if ($line=~/\bswitch\s*\(.*\)/) {
1288 my $err = '';
1289 my $sep = '';
1290 my @ctx = ctx_block_outer($linenr, $realcnt);
1291 shift(@ctx);
1292 for my $ctx (@ctx) {
1293 my ($clen, $cindent) = line_stats($ctx);
1294 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1295 $indent != $cindent) {
1296 $err .= "$sep$ctx\n";
1297 $sep = '';
1298 } else {
1299 $sep = "[...]\n";
1302 if ($err ne '') {
1303 ERROR("switch and case should be at the same indent\n$hereline$err");
1307 # if/while/etc brace do not go on next line, unless defining a do while loop,
1308 # or if that brace on the next line is for something else
1309 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1310 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1311 my $ctx_ln = $linenr + $#ctx + 1;
1312 my $ctx_cnt = $realcnt - $#ctx - 1;
1313 my $ctx = join("\n", @ctx);
1315 # Skip over any removed lines in the context following statement.
1316 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
1317 $ctx_ln++;
1318 $ctx_cnt--;
1320 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
1322 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1323 ERROR("That open brace { should be on the previous line\n" .
1324 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1326 if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) {
1327 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1328 if ($nindent > $indent) {
1329 WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" .
1330 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1335 # Track the 'values' across context and added lines.
1336 my $opline = $line; $opline =~ s/^./ /;
1337 my $curr_values = annotate_values($opline . "\n", $prev_values);
1338 $curr_values = $prev_values . $curr_values;
1339 if ($dbg_values) {
1340 my $outline = $opline; $outline =~ s/\t/ /g;
1341 <<<<<<< HEAD:scripts/checkpatch.pl
1342 warn "--> .$outline\n";
1343 warn "--> $curr_values\n";
1344 =======
1345 print "$linenr > .$outline\n";
1346 print "$linenr > $curr_values\n";
1347 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1349 $prev_values = substr($curr_values, -1);
1351 #ignore lines not being added
1352 if ($line=~/^[^\+]/) {next;}
1354 # TEST: allow direct testing of the type matcher.
1355 if ($tst_type && $line =~ /^.$Declare$/) {
1356 ERROR("TEST: is type $Declare\n" . $herecurr);
1357 next;
1360 # check for initialisation to aggregates open brace on the next line
1361 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1362 $line =~ /^.\s*{/) {
1363 ERROR("That open brace { should be on the previous line\n" . $hereprev);
1367 # Checks which are anchored on the added line.
1370 # check for malformed paths in #include statements (uses RAW line)
1371 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1372 my $path = $1;
1373 if ($path =~ m{//}) {
1374 ERROR("malformed #include filename\n" .
1375 $herecurr);
1379 # no C99 // comments
1380 if ($line =~ m{//}) {
1381 ERROR("do not use C99 // comments\n" . $herecurr);
1383 # Remove C99 comments.
1384 $line =~ s@//.*@@;
1385 $opline =~ s@//.*@@;
1387 #EXPORT_SYMBOL should immediately follow its function closing }.
1388 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1389 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1390 my $name = $1;
1391 if (($prevline !~ /^}/) &&
1392 ($prevline !~ /^\+}/) &&
1393 ($prevline !~ /^ }/) &&
1394 <<<<<<< HEAD:scripts/checkpatch.pl
1395 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) {
1396 =======
1397 ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1398 ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1399 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1400 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1401 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1405 # check for external initialisers.
1406 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
1407 ERROR("do not initialise externals to 0 or NULL\n" .
1408 $herecurr);
1410 # check for static initialisers.
1411 if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
1412 ERROR("do not initialise statics to 0 or NULL\n" .
1413 $herecurr);
1416 # check for new typedefs, only function parameters and sparse annotations
1417 # make sense.
1418 if ($line =~ /\btypedef\s/ &&
1419 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1420 $line !~ /\b__bitwise(?:__|)\b/) {
1421 WARN("do not add new typedefs\n" . $herecurr);
1424 # * goes on variable not on type
1425 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1426 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1427 $herecurr);
1429 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1430 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1431 $herecurr);
1433 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1434 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1435 $herecurr);
1437 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1438 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1439 $herecurr);
1442 # # no BUG() or BUG_ON()
1443 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1444 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1445 # print "$herecurr";
1446 # $clean = 0;
1449 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1450 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1453 # printk should use KERN_* levels. Note that follow on printk's on the
1454 # same line do not need a level, so we use the current block context
1455 # to try and find and validate the current printk. In summary the current
1456 # printk includes all preceeding printk's which have no newline on the end.
1457 # we assume the first bad printk is the one to report.
1458 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1459 my $ok = 0;
1460 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1461 #print "CHECK<$lines[$ln - 1]\n";
1462 # we have a preceeding printk if it ends
1463 # with "\n" ignore it, else it is to blame
1464 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1465 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1466 $ok = 1;
1468 last;
1471 if ($ok == 0) {
1472 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1476 # function brace can't be on same line, except for #defines of do while,
1477 # or if closed on same line
1478 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
1479 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
1480 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1483 # open braces for enum, union and struct go on the same line.
1484 if ($line =~ /^.\s*{/ &&
1485 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1486 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1489 # check for spaces between functions and their parentheses.
1490 while ($line =~ /($Ident)\s+\(/g) {
1491 my $name = $1;
1492 my $ctx = substr($line, 0, $-[1]);
1494 # Ignore those directives where spaces _are_ permitted.
1495 if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case|__asm__)$/) {
1497 # cpp #define statements have non-optional spaces, ie
1498 # if there is a space between the name and the open
1499 # parenthesis it is simply not a parameter group.
1500 } elsif ($ctx =~ /^.\#\s*define\s*$/) {
1502 # If this whole things ends with a type its most
1503 # likely a typedef for a function.
1504 } elsif ("$ctx$name" =~ /$Type$/) {
1506 } else {
1507 WARN("no space between function name and open parenthesis '('\n" . $herecurr);
1510 # Check operator spacing.
1511 if (!($line=~/\#\s*include/)) {
1512 my $ops = qr{
1513 <<=|>>=|<=|>=|==|!=|
1514 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1515 =>|->|<<|>>|<|>|=|!|~|
1516 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1518 <<<<<<< HEAD:scripts/checkpatch.pl
1519 my @elements = split(/($;+|$ops|;)/, $opline);
1520 =======
1521 my @elements = split(/($ops|;)/, $opline);
1522 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1523 my $off = 0;
1525 my $blank = copy_spacing($opline);
1527 for (my $n = 0; $n < $#elements; $n += 2) {
1528 $off += length($elements[$n]);
1530 my $a = '';
1531 $a = 'V' if ($elements[$n] ne '');
1532 $a = 'W' if ($elements[$n] =~ /\s$/);
1533 <<<<<<< HEAD:scripts/checkpatch.pl
1534 =======
1535 $a = 'C' if ($elements[$n] =~ /$;$/);
1536 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1537 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1538 $a = 'O' if ($elements[$n] eq '');
1539 $a = 'E' if ($elements[$n] eq '' && $n == 0);
1541 my $op = $elements[$n + 1];
1543 my $c = '';
1544 if (defined $elements[$n + 2]) {
1545 $c = 'V' if ($elements[$n + 2] ne '');
1546 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1547 <<<<<<< HEAD:scripts/checkpatch.pl
1548 =======
1549 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1550 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1551 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1552 $c = 'O' if ($elements[$n + 2] eq '');
1553 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
1554 } else {
1555 $c = 'E';
1558 # Pick up the preceeding and succeeding characters.
1559 my $ca = substr($opline, 0, $off);
1560 my $cc = '';
1561 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1562 $cc = substr($opline, $off + length($elements[$n + 1]));
1564 my $cb = "$ca$;$cc";
1566 my $ctx = "${a}x${c}";
1568 my $at = "(ctx:$ctx)";
1570 my $ptr = substr($blank, 0, $off) . "^";
1571 my $hereptr = "$hereline$ptr\n";
1573 # Classify operators into binary, unary, or
1574 # definitions (* only) where they have more
1575 # than one mode.
1576 my $op_type = substr($curr_values, $off + 1, 1);
1577 my $op_left = substr($curr_values, $off, 1);
1578 my $is_unary;
1579 if ($op_type eq 'T') {
1580 $is_unary = 2;
1581 } elsif ($op_left eq 'V') {
1582 $is_unary = 0;
1583 } else {
1584 $is_unary = 1;
1586 #if ($op eq '-' || $op eq '&' || $op eq '*') {
1587 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
1590 # Ignore operators passed as parameters.
1591 if ($op_type ne 'V' &&
1592 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1594 <<<<<<< HEAD:scripts/checkpatch.pl
1595 # Ignore comments
1596 } elsif ($op =~ /^$;+$/) {
1597 =======
1598 # # Ignore comments
1599 # } elsif ($op =~ /^$;+$/) {
1600 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1602 # ; should have either the end of line or a space or \ after it
1603 } elsif ($op eq ';') {
1604 <<<<<<< HEAD:scripts/checkpatch.pl
1605 if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
1606 $cc !~ /^;/) {
1607 =======
1608 if ($ctx !~ /.x[WEBC]/ &&
1609 $cc !~ /^\\/ && $cc !~ /^;/) {
1610 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1611 ERROR("need space after that '$op' $at\n" . $hereptr);
1614 # // is a comment
1615 } elsif ($op eq '//') {
1617 # -> should have no spaces
1618 } elsif ($op eq '->') {
1619 if ($ctx =~ /Wx.|.xW/) {
1620 ERROR("no spaces around that '$op' $at\n" . $hereptr);
1623 # , must have a space on the right.
1624 } elsif ($op eq ',') {
1625 <<<<<<< HEAD:scripts/checkpatch.pl
1626 if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
1627 =======
1628 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1629 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1630 ERROR("need space after that '$op' $at\n" . $hereptr);
1633 # '*' as part of a type definition -- reported already.
1634 } elsif ($op eq '*' && $is_unary == 2) {
1635 #warn "'*' is part of type\n";
1637 # unary operators should have a space before and
1638 # none after. May be left adjacent to another
1639 # unary operator, or a cast
1640 } elsif ($op eq '!' || $op eq '~' ||
1641 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1642 <<<<<<< HEAD:scripts/checkpatch.pl
1643 if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1644 =======
1645 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1646 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1647 ERROR("need space before that '$op' $at\n" . $hereptr);
1649 if ($ctx =~ /.xW/) {
1650 ERROR("no space after that '$op' $at\n" . $hereptr);
1653 # unary ++ and unary -- are allowed no space on one side.
1654 } elsif ($op eq '++' or $op eq '--') {
1655 <<<<<<< HEAD:scripts/checkpatch.pl
1656 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
1657 =======
1658 if ($ctx !~ /[WOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1659 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1660 ERROR("need space one side of that '$op' $at\n" . $hereptr);
1662 if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1663 ERROR("no space before that '$op' $at\n" . $hereptr);
1666 # << and >> may either have or not have spaces both sides
1667 } elsif ($op eq '<<' or $op eq '>>' or
1668 $op eq '&' or $op eq '^' or $op eq '|' or
1669 $op eq '+' or $op eq '-' or
1670 $op eq '*' or $op eq '/' or
1671 $op eq '%')
1673 <<<<<<< HEAD:scripts/checkpatch.pl
1674 if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) {
1675 =======
1676 if ($ctx !~ /VxV|WxW|VxE|WxE|VxO|Cx.|.xC/) {
1677 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1678 ERROR("need consistent spacing around '$op' $at\n" .
1679 $hereptr);
1682 # All the others need spaces both sides.
1683 <<<<<<< HEAD:scripts/checkpatch.pl
1684 } elsif ($ctx !~ /[EW]x[WE]/) {
1685 =======
1686 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1687 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1688 # Ignore email addresses <foo@bar>
1689 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1690 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1691 ERROR("need spaces around that '$op' $at\n" . $hereptr);
1694 $off += length($elements[$n + 1]);
1698 # check for multiple assignments
1699 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
1700 CHK("multiple assignments should be avoided\n" . $herecurr);
1703 ## # check for multiple declarations, allowing for a function declaration
1704 ## # continuation.
1705 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1706 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1708 ## # Remove any bracketed sections to ensure we do not
1709 ## # falsly report the parameters of functions.
1710 ## my $ln = $line;
1711 ## while ($ln =~ s/\([^\(\)]*\)//g) {
1712 ## }
1713 ## if ($ln =~ /,/) {
1714 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1715 ## }
1716 ## }
1718 #need space before brace following if, while, etc
1719 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1720 $line =~ /do{/) {
1721 ERROR("need a space before the open brace '{'\n" . $herecurr);
1724 # closing brace should have a space following it when it has anything
1725 # on the line
1726 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1727 ERROR("need a space after that close brace '}'\n" . $herecurr);
1730 # check spacing on square brackets
1731 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1732 ERROR("no space after that open square bracket '['\n" . $herecurr);
1734 if ($line =~ /\s\]/) {
1735 ERROR("no space before that close square bracket ']'\n" . $herecurr);
1738 # check spacing on paretheses
1739 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1740 $line !~ /for\s*\(\s+;/) {
1741 ERROR("no space after that open parenthesis '('\n" . $herecurr);
1743 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1744 $line !~ /for\s*\(.*;\s+\)/) {
1745 ERROR("no space before that close parenthesis ')'\n" . $herecurr);
1748 #goto labels aren't indented, allow a single space however
1749 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
1750 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1751 WARN("labels should not be indented\n" . $herecurr);
1754 # Need a space before open parenthesis after if, while etc
1755 if ($line=~/\b(if|while|for|switch)\(/) {
1756 ERROR("need a space before the open parenthesis '('\n" . $herecurr);
1759 # Check for illegal assignment in if conditional.
1760 if ($line =~ /\bif\s*\(/) {
1761 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1763 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1764 ERROR("do not use assignment in if condition\n" . $herecurr);
1767 # Find out what is on the end of the line after the
1768 # conditional.
1769 substr($s, 0, length($c)) = '';
1770 $s =~ s/\n.*//g;
1771 $s =~ s/$;//g; # Remove any comments
1772 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/) {
1773 ERROR("trailing statements should be on next line\n" . $herecurr);
1777 # Check for bitwise tests written as boolean
1778 if ($line =~ /
1780 (?:\[|\(|\&\&|\|\|)
1781 \s*0[xX][0-9]+\s*
1782 (?:\&\&|\|\|)
1784 (?:\&\&|\|\|)
1785 \s*0[xX][0-9]+\s*
1786 (?:\&\&|\|\||\)|\])
1787 )/x)
1789 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
1792 # if and else should not have general statements after it
1793 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
1794 my $s = $1;
1795 $s =~ s/$;//g; # Remove any comments
1796 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
1797 ERROR("trailing statements should be on next line\n" . $herecurr);
1801 # Check for }<nl>else {, these must be at the same
1802 # indent level to be relevant to each other.
1803 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1804 $previndent == $indent) {
1805 ERROR("else should follow close brace '}'\n" . $hereprev);
1808 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1809 $previndent == $indent) {
1810 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1812 # Find out what is on the end of the line after the
1813 # conditional.
1814 substr($s, 0, length($c)) = '';
1815 $s =~ s/\n.*//g;
1817 if ($s =~ /^\s*;/) {
1818 ERROR("while should follow close brace '}'\n" . $hereprev);
1822 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
1823 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1824 # print "No studly caps, use _\n";
1825 # print "$herecurr";
1826 # $clean = 0;
1829 #no spaces allowed after \ in define
1830 if ($line=~/\#define.*\\\s$/) {
1831 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
1834 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1835 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
1836 my $checkfile = "$root/include/linux/$1.h";
1837 if (-f $checkfile && $1 ne 'irq.h') {
1838 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1839 $herecurr);
1843 # multi-statement macros should be enclosed in a do while loop, grab the
1844 # first statement and ensure its the whole macro if its not enclosed
1845 <<<<<<< HEAD:scripts/checkpatch.pl
1846 # in a known goot container
1847 =======
1848 # in a known good container
1849 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1850 if ($prevline =~ /\#define.*\\/ &&
1851 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1852 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1853 $line !~ /^.\s*$Declare\s/) {
1854 # Grab the first statement, if that is the entire macro
1855 # its ok. This may start either on the #define line
1856 # or the one below.
1857 my $ln = $linenr;
1858 my $cnt = $realcnt;
1859 my $off = 0;
1861 # If the macro starts on the define line start
1862 # grabbing the statement after the identifier
1863 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1864 ##print "1<$1> 2<$2>\n";
1865 if (defined $2 && $2 ne '') {
1866 $off = length($1);
1867 $ln--;
1868 $cnt++;
1869 while ($lines[$ln - 1] =~ /^-/) {
1870 $ln--;
1871 $cnt++;
1874 my @ctx = ctx_statement($ln, $cnt, $off);
1875 my $ctx_ln = $ln + $#ctx + 1;
1876 my $ctx = join("\n", @ctx);
1878 # Pull in any empty extension lines.
1879 while ($ctx =~ /\\$/ &&
1880 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1881 $ctx .= $lines[$ctx_ln - 1];
1882 $ctx_ln++;
1885 if ($ctx =~ /\\$/) {
1886 if ($ctx =~ /;/) {
1887 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
1888 } else {
1889 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
1894 # check for redundant bracing round if etc
1895 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1896 my ($level, $endln, @chunks) =
1897 <<<<<<< HEAD:scripts/checkpatch.pl
1898 ctx_statement_full($linenr, $realcnt, 0);
1899 =======
1900 ctx_statement_full($linenr, $realcnt, 1);
1901 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1902 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
1903 <<<<<<< HEAD:scripts/checkpatch.pl
1904 if ($#chunks > 1 && $level == 0) {
1905 =======
1906 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1907 if ($#chunks > 0 && $level == 0) {
1908 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1909 my $allowed = 0;
1910 my $seen = 0;
1911 <<<<<<< HEAD:scripts/checkpatch.pl
1912 =======
1913 my $herectx = $here . "\n";;
1914 my $ln = $linenr - 1;
1915 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1916 for my $chunk (@chunks) {
1917 my ($cond, $block) = @{$chunk};
1919 <<<<<<< HEAD:scripts/checkpatch.pl
1920 =======
1921 $herectx .= "$rawlines[$ln]\n[...]\n";
1922 $ln += statement_rawlines($block) - 1;
1924 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1925 substr($block, 0, length($cond)) = '';
1927 $seen++ if ($block =~ /^\s*{/);
1929 <<<<<<< HEAD:scripts/checkpatch.pl
1930 $block =~ s/(^|\n)./$1/g;
1931 $block =~ s/^\s*{//;
1932 $block =~ s/}\s*$//;
1933 $block =~ s/^\s*//;
1934 $block =~ s/\s*$//;
1936 my @lines = ($block =~ /\n/g);
1937 my @statements = ($block =~ /;/g);
1939 #print "cond<$cond> block<$block> lines<" . scalar(@lines) . "> statements<" . scalar(@statements) . "> seen<$seen> allowed<$allowed>\n";
1940 if (scalar(@lines) != 0) {
1941 =======
1942 #print "cond<$cond> block<$block> allowed<$allowed>\n";
1943 if (statement_lines($cond) > 1) {
1944 #print "APW: ALLOWED: cond<$cond>\n";
1945 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1946 $allowed = 1;
1948 if ($block =~/\b(?:if|for|while)\b/) {
1949 <<<<<<< HEAD:scripts/checkpatch.pl
1950 =======
1951 #print "APW: ALLOWED: block<$block>\n";
1952 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1953 $allowed = 1;
1955 <<<<<<< HEAD:scripts/checkpatch.pl
1956 if (scalar(@statements) > 1) {
1957 =======
1958 if (statement_block_size($block) > 1) {
1959 #print "APW: ALLOWED: lines block<$block>\n";
1960 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1961 $allowed = 1;
1964 if ($seen && !$allowed) {
1965 <<<<<<< HEAD:scripts/checkpatch.pl
1966 WARN("braces {} are not necessary for any arm of this statement\n" . $herecurr);
1967 $suppress_ifbraces = $endln;
1968 =======
1969 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
1970 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1972 <<<<<<< HEAD:scripts/checkpatch.pl
1973 =======
1974 # Either way we have looked over this whole
1975 # statement and said what needs to be said.
1976 $suppress_ifbraces = $endln;
1977 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
1980 if ($linenr > $suppress_ifbraces &&
1981 $line =~ /\b(if|while|for|else)\b/) {
1982 <<<<<<< HEAD:scripts/checkpatch.pl
1983 # Locate the end of the opening statement.
1984 my @control = ctx_statement($linenr, $realcnt, 0);
1985 my $nr = $linenr + (scalar(@control) - 1);
1986 my $cnt = $realcnt - (scalar(@control) - 1);
1988 my $off = $realcnt - $cnt;
1989 #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n";
1991 # If this is is a braced statement group check it
1992 if ($lines[$nr - 1] =~ /{\s*$/) {
1993 my ($lvl, @block) = ctx_block_level($nr, $cnt);
1995 my $stmt = join("\n", @block);
1996 # Drop the diff line leader.
1997 $stmt =~ s/\n./\n/g;
1998 # Drop the code outside the block.
1999 $stmt =~ s/(^[^{]*){\s*//;
2000 my $before = $1;
2001 $stmt =~ s/\s*}([^}]*$)//;
2002 my $after = $1;
2004 #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n";
2005 #print "before<$before> stmt<$stmt> after<$after>\n\n";
2007 # Count the newlines, if there is only one
2008 # then the block should not have {}'s.
2009 my @lines = ($stmt =~ /\n/g);
2010 my @statements = ($stmt =~ /;/g);
2011 #print "lines<" . scalar(@lines) . ">\n";
2012 #print "statements<" . scalar(@statements) . ">\n";
2013 if ($lvl == 0 && scalar(@lines) == 0 &&
2014 scalar(@statements) < 2 &&
2015 $stmt !~ /{/ && $stmt !~ /\bif\b/ &&
2016 $before !~ /}/ && $after !~ /{/) {
2017 my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n";
2018 shift(@block);
2019 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2020 =======
2021 my ($level, $endln, @chunks) =
2022 ctx_statement_full($linenr, $realcnt, $-[0]);
2024 my $allowed = 0;
2026 # Check the pre-context.
2027 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2028 #print "APW: ALLOWED: pre<$1>\n";
2029 $allowed = 1;
2031 # Check the condition.
2032 my ($cond, $block) = @{$chunks[0]};
2033 if (defined $cond) {
2034 substr($block, 0, length($cond)) = '';
2036 if (statement_lines($cond) > 1) {
2037 #print "APW: ALLOWED: cond<$cond>\n";
2038 $allowed = 1;
2040 if ($block =~/\b(?:if|for|while)\b/) {
2041 #print "APW: ALLOWED: block<$block>\n";
2042 $allowed = 1;
2044 if (statement_block_size($block) > 1) {
2045 #print "APW: ALLOWED: lines block<$block>\n";
2046 $allowed = 1;
2048 # Check the post-context.
2049 if (defined $chunks[1]) {
2050 my ($cond, $block) = @{$chunks[1]};
2051 if (defined $cond) {
2052 substr($block, 0, length($cond)) = '';
2054 if ($block =~ /^\s*\{/) {
2055 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2056 $allowed = 1;
2059 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2060 my $herectx = $here . "\n";;
2061 my $end = $linenr + statement_rawlines($block) - 1;
2063 for (my $ln = $linenr - 1; $ln < $end; $ln++) {
2064 $herectx .= $rawlines[$ln] . "\n";;
2065 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
2067 <<<<<<< HEAD:scripts/checkpatch.pl
2068 =======
2070 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2071 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
2075 # don't include deprecated include files (uses RAW line)
2076 for my $inc (@dep_includes) {
2077 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
2078 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2082 # don't use deprecated functions
2083 for my $func (@dep_functions) {
2084 if ($line =~ /\b$func\b/) {
2085 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2089 # no volatiles please
2090 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2091 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2092 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2095 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2096 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2097 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2100 # warn about #if 0
2101 if ($line =~ /^.#\s*if\s+0\b/) {
2102 CHK("if this code is redundant consider removing it\n" .
2103 $herecurr);
2106 # check for needless kfree() checks
2107 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2108 my $expr = $1;
2109 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2110 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
2114 # warn about #ifdefs in C files
2115 # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2116 # print "#ifdef in C files should be avoided\n";
2117 # print "$herecurr";
2118 # $clean = 0;
2121 # warn about spacing in #ifdefs
2122 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
2123 ERROR("exactly one space required after that #$1\n" . $herecurr);
2126 # check for spinlock_t definitions without a comment.
2127 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
2128 my $which = $1;
2129 if (!ctx_has_comment($first_line, $linenr)) {
2130 CHK("$1 definition without comment\n" . $herecurr);
2133 # check for memory barriers without a comment.
2134 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2135 if (!ctx_has_comment($first_line, $linenr)) {
2136 CHK("memory barrier without comment\n" . $herecurr);
2139 # check of hardware specific defines
2140 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2141 CHK("architecture specific defines should be avoided\n" . $herecurr);
2144 # check the location of the inline attribute, that it is between
2145 # storage class and type.
2146 if ($line =~ /\b$Type\s+$Inline\b/ ||
2147 $line =~ /\b$Inline\s+$Storage\b/) {
2148 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2151 # Check for __inline__ and __inline, prefer inline
2152 if ($line =~ /\b(__inline__|__inline)\b/) {
2153 WARN("plain inline is preferred over $1\n" . $herecurr);
2156 # check for new externs in .c files.
2157 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
2158 WARN("externs should be avoided in .c files\n" . $herecurr);
2161 # checks for new __setup's
2162 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2163 my $name = $1;
2165 if (!grep(/$name/, @setup_docs)) {
2166 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2170 # check for pointless casting of kmalloc return
2171 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2172 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2175 # check for gcc specific __FUNCTION__
2176 if ($line =~ /__FUNCTION__/) {
2177 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2181 # If we have no input at all, then there is nothing to report on
2182 # so just keep quiet.
2183 if ($#rawlines == -1) {
2184 exit(0);
2187 # In mailback mode only produce a report in the negative, for
2188 # things that appear to be patches.
2189 if ($mailback && ($clean == 1 || !$is_patch)) {
2190 exit(0);
2193 # This is not a patch, and we are are in 'no-patch' mode so
2194 # just keep quiet.
2195 if (!$chk_patch && !$is_patch) {
2196 exit(0);
2199 if (!$is_patch) {
2200 ERROR("Does not appear to be a unified-diff format patch\n");
2202 if ($is_patch && $chk_signoff && $signoff == 0) {
2203 ERROR("Missing Signed-off-by: line(s)\n");
2206 print report_dump();
2207 if ($summary && !($clean == 1 && $quiet == 1)) {
2208 print "$filename " if ($summary_file);
2209 print "total: $cnt_error errors, $cnt_warn warnings, " .
2210 (($check)? "$cnt_chk checks, " : "") .
2211 "$cnt_lines lines checked\n";
2212 print "\n" if ($quiet == 0);
2215 if ($clean == 1 && $quiet == 0) {
2216 print "$vname has no obvious style problems and is ready for submission.\n"
2218 if ($clean == 0 && $quiet == 0) {
2219 print "$vname has style problems, please review. If any of these errors\n";
2220 print "are false positives report them to the maintainer, see\n";
2221 print "CHECKPATCH in MAINTAINERS.\n";
2223 <<<<<<< HEAD:scripts/checkpatch.pl
2224 print <<EOL if ($file == 1 && $quiet == 0);
2226 WARNING: Using --file mode. Please do not send patches to linux-kernel
2227 that change whole existing files if you did not significantly change most
2228 of the the file for other reasons anyways or just wrote the file newly
2229 from scratch. Pure code style patches have a significant cost in a
2230 quickly changing code base like Linux because they cause rejects
2231 with other changes.
2233 =======
2234 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/checkpatch.pl
2236 return $clean;