Fix tizen coding convention
[apps/native/sample/sample-core-components.git] / tool / checkpatch_tizen.pl
1 #!/usr/bin/perl -w
2 # (c) 2001, Dave Jones. (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5 # (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6 # Licensed under the terms of the GNU GPL License version 2
7 #
8 # modified for coding style check for SLP (Samsung Linux Platform)
9 # Software Engineering Lab. DMC Research Center, Samsung Electronics
10
11 # modified for coding style check for TIZEN
12 # Software Platform Team. S/W Center, Samsung Electronics
13
14 use strict;
15
16 my $P = $0;
17 $P =~ s@.*/@@g;
18
19 my $V = '0.32-tizen0709';
20
21 use Getopt::Long qw(:config no_auto_abbrev);
22
23 my $quiet = 0;
24 my $tree = 0;
25 my $chk_signoff = 0;
26 my $chk_patch = 0;
27 my $tst_only;
28 my $emacs = 0;
29 my $terse = 1;
30 my $file = 1;
31 my $check = 0;
32 my $summary = 1;
33 my $mailback = 0;
34 my $summary_file = 0;
35 my $root;
36 my %debug;
37 my $help = 0;
38 my $detail = 0;
39
40 sub help {
41         my ($exitcode) = @_;
42
43         print << "EOM";
44 Usage: $P [OPTION]... [FILE]...
45 Version: $V
46
47 Options:
48   -q, --quiet                quiet
49   --no-tree                  run without a kernel tree
50   --no-signoff               do not check for 'Signed-off-by' line
51   --patch                    treat FILE as patchfile 
52   --emacs                    emacs compile window format
53   --terse                    one line per report (default)
54   --detail                   detail report
55   -f, --file                 treat FILE as regular source file (default)
56   --subjective, --strict     enable more subjective tests
57   --root=PATH                PATH to the kernel tree root
58   --no-summary               suppress the per-file summary (default)
59   --mailback                 only produce a report in case of warnings/errors
60   --summary-file             include the filename in summary
61   --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
62                              'values', 'possible', 'type', and 'attr' (default
63                              is all off)
64   --test-only=WORD           report only warnings/errors containing WORD
65                              literally
66   -h, --help, --version      display this help and exit
67
68 When FILE is - read standard input.
69 EOM
70
71         exit($exitcode);
72 }
73
74 GetOptions(
75         'q|quiet+'      => \$quiet,
76         'tree!'         => \$tree,
77         'signoff!'      => \$chk_signoff,
78         'patch!'        => \$chk_patch,
79         'emacs!'        => \$emacs,
80         'terse!'        => \$terse,
81         'detail!'       => \$detail,
82         'f|file!'       => \$file,
83         'subjective!'   => \$check,
84         'strict!'       => \$check,
85         'root=s'        => \$root,
86         'summary!'      => \$summary,
87         'mailback!'     => \$mailback,
88         'summary-file!' => \$summary_file,
89
90         'debug=s'       => \%debug,
91         'test-only=s'   => \$tst_only,
92         'h|help'        => \$help,
93         'version'       => \$help
94 ) or help(1);
95
96 help(0) if ($help);
97
98 my $exit = 0;
99
100 if ($#ARGV < 0) {
101         print "$P: no input files\n";
102         exit(1);
103 }
104
105 my $dbg_values = 0;
106 my $dbg_possible = 0;
107 my $dbg_type = 0;
108 my $dbg_attr = 0;
109 for my $key (keys %debug) {
110         ## no critic
111         eval "\${dbg_$key} = '$debug{$key}';";
112         die "$@" if ($@);
113 }
114
115 my $rpt_cleaners = 0;
116
117 if ($detail) {
118         $terse = 0;
119 }
120
121 if ($terse) {
122         $emacs = 1;
123         $quiet++;
124 }
125
126 if ($tree) {
127         if (defined $root) {
128                 if (!top_of_kernel_tree($root)) {
129                         die "$P: $root: --root does not point at a valid tree\n";
130                 }
131         } else {
132                 if (top_of_kernel_tree('.')) {
133                         $root = '.';
134                 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
135                                                 top_of_kernel_tree($1)) {
136                         $root = $1;
137                 }
138         }
139
140         if (!defined $root) {
141                 print "Must be run from the top-level dir. of a kernel tree\n";
142                 exit(2);
143         }
144 }
145
146 my $emitted_corrupt = 0;
147
148 our $Ident      = qr{
149                         [A-Za-z_][A-Za-z\d_]*
150                         (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
151                 }x;
152 our $Storage    = qr{extern|static|asmlinkage};
153 our $Sparse     = qr{
154                         __user|
155                         __kernel|
156                         __force|
157                         __iomem|
158                         __must_check|
159                         __init_refok|
160                         __kprobes|
161                         __ref
162                 }x;
163
164 # Notes to $Attribute:
165 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
166 our $Attribute  = qr{
167                         const|
168                         __percpu|
169                         __nocast|
170                         __safe|
171                         __bitwise__|
172                         __packed__|
173                         __packed2__|
174                         __naked|
175                         __maybe_unused|
176                         __always_unused|
177                         __noreturn|
178                         __used|
179                         __cold|
180                         __noclone|
181                         __deprecated|
182                         __read_mostly|
183                         __kprobes|
184                         __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
185                         ____cacheline_aligned|
186                         ____cacheline_aligned_in_smp|
187                         ____cacheline_internodealigned_in_smp|
188                         __weak
189                   }x;
190 our $Modifier;
191 our $Inline     = qr{inline|__always_inline|noinline};
192 our $Member     = qr{->$Ident|\.$Ident|\[[^]]*\]};
193 our $Lval       = qr{$Ident(?:$Member)*};
194
195 our $Constant   = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
196 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
197 our $Compare    = qr{<=|>=|==|!=|<|>};
198 our $Operators  = qr{
199                         <=|>=|==|!=|
200                         =>|->|<<|>>|<|>|!|~|
201                         &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
202                   }x;
203
204 our $NonptrType;
205 our $Type;
206 our $Declare;
207
208 our $UTF8       = qr {
209         [\x09\x0A\x0D\x20-\x7E]              # ASCII
210         | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
211         |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
212         | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
213         |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
214         |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
215         | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
216         |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
217 }x;
218
219 our $typeTypedefs = qr{(?x:
220         (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
221         atomic_t
222 )};
223
224 our $logFunctions = qr{(?x:
225         printk|
226         pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
227         (dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
228         WARN|
229         panic
230 )};
231
232 our @typeList = (
233         qr{void},
234         qr{(?:unsigned\s+)?char},
235         qr{(?:unsigned\s+)?short},
236         qr{(?:unsigned\s+)?int},
237         qr{(?:unsigned\s+)?long},
238         qr{(?:unsigned\s+)?long\s+int},
239         qr{(?:unsigned\s+)?long\s+long},
240         qr{(?:unsigned\s+)?long\s+long\s+int},
241         qr{unsigned},
242         qr{float},
243         qr{double},
244         qr{bool},
245         qr{struct\s+$Ident},
246         qr{union\s+$Ident},
247         qr{enum\s+$Ident},
248         qr{${Ident}_t},
249         qr{${Ident}_handler},
250         qr{${Ident}_handler_fn},
251 );
252 our @modifierList = (
253         qr{fastcall},
254 );
255
256 our $allowed_asm_includes = qr{(?x:
257         irq|
258         memory
259 )};
260 # memory.h: ARM has a custom one
261
262 sub build_types {
263         my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
264         my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
265         $Modifier       = qr{(?:$Attribute|$Sparse|$mods)};
266         $NonptrType     = qr{
267                         (?:$Modifier\s+|const\s+)*
268                         (?:
269                                 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
270                                 (?:$typeTypedefs\b)|
271                                 (?:${all}\b)
272                         )
273                         (?:\s+$Modifier|\s+const)*
274                   }x;
275         $Type   = qr{
276                         $NonptrType
277                         (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
278                         (?:\s+$Inline|\s+$Modifier)*
279                   }x;
280         $Declare        = qr{(?:$Storage\s+)?$Type};
281 }
282 build_types();
283
284 $chk_signoff = 0 if ($file);
285
286 my @dep_includes = ();
287 my @dep_functions = ();
288 my $removal = "Documentation/feature-removal-schedule.txt";
289 if ($tree && -f "$root/$removal") {
290         open(my $REMOVE, '<', "$root/$removal") ||
291                                 die "$P: $removal: open failed - $!\n";
292         while (<$REMOVE>) {
293                 if (/^Check:\s+(.*\S)/) {
294                         for my $entry (split(/[, ]+/, $1)) {
295                                 if ($entry =~ m@include/(.*)@) {
296                                         push(@dep_includes, $1);
297
298                                 } elsif ($entry !~ m@/@) {
299                                         push(@dep_functions, $entry);
300                                 }
301                         }
302                 }
303         }
304         close($REMOVE);
305 }
306
307 my @rawlines = ();
308 my @lines = ();
309 my $vname;
310 for my $filename (@ARGV) {
311         my $FILE;
312         if ($file) {
313                 open($FILE, '-|', "diff -u /dev/null $filename") ||
314                         die "$P: $filename: diff failed - $!\n";
315         } elsif ($filename eq '-') {
316                 open($FILE, '<&STDIN');
317         } else {
318                 open($FILE, '<', "$filename") ||
319                         die "$P: $filename: open failed - $!\n";
320         }
321         if ($filename eq '-') {
322                 $vname = 'Your patch';
323         } else {
324                 $vname = $filename;
325         }
326         while (<$FILE>) {
327                 chomp;
328                 push(@rawlines, $_);
329         }
330         close($FILE);
331         if (!process($filename)) {
332                 $exit = 1;
333         }
334         @rawlines = ();
335         @lines = ();
336 }
337
338 exit($exit);
339
340 sub top_of_kernel_tree {
341         my ($root) = @_;
342
343         my @tree_check = (
344                 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
345                 "README", "Documentation", "arch", "include", "drivers",
346                 "fs", "init", "ipc", "kernel", "lib", "scripts",
347         );
348
349         foreach my $check (@tree_check) {
350                 if (! -e $root . '/' . $check) {
351                         return 0;
352                 }
353         }
354         return 1;
355 }
356
357 sub expand_tabs {
358         my ($str) = @_;
359
360         my $res = '';
361         my $n = 0;
362         for my $c (split(//, $str)) {
363                 if ($c eq "\t") {
364                         $res .= ' ';
365                         $n++;
366                         for (; ($n % 8) != 0; $n++) {
367                                 $res .= ' ';
368                         }
369                         next;
370                 }
371                 $res .= $c;
372                 $n++;
373         }
374
375         return $res;
376 }
377 sub copy_spacing {
378         (my $res = shift) =~ tr/\t/ /c;
379         return $res;
380 }
381
382 sub line_stats {
383         my ($line) = @_;
384
385         # Drop the diff line leader and expand tabs
386         $line =~ s/^.//;
387         $line = expand_tabs($line);
388
389         # Pick the indent from the front of the line.
390         my ($white) = ($line =~ /^(\s*)/);
391
392         return (length($line), length($white));
393 }
394
395 my $sanitise_quote = '';
396
397 sub sanitise_line_reset {
398         my ($in_comment) = @_;
399
400         if ($in_comment) {
401                 $sanitise_quote = '*/';
402         } else {
403                 $sanitise_quote = '';
404         }
405 }
406 sub sanitise_line {
407         my ($line) = @_;
408
409         my $res = '';
410         my $l = '';
411
412         my $qlen = 0;
413         my $off = 0;
414         my $c;
415
416         # Always copy over the diff marker.
417         $res = substr($line, 0, 1);
418
419         for ($off = 1; $off < length($line); $off++) {
420                 $c = substr($line, $off, 1);
421
422                 # Comments we are wacking completly including the begin
423                 # and end, all to $;.
424                 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
425                         $sanitise_quote = '*/';
426
427                         substr($res, $off, 2, "$;$;");
428                         $off++;
429                         next;
430                 }
431                 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
432                         $sanitise_quote = '';
433                         substr($res, $off, 2, "$;$;");
434                         $off++;
435                         next;
436                 }
437                 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
438                         $sanitise_quote = '//';
439
440                         substr($res, $off, 2, $sanitise_quote);
441                         $off++;
442                         next;
443                 }
444
445                 # A \ in a string means ignore the next character.
446                 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
447                     $c eq "\\") {
448                         substr($res, $off, 2, 'XX');
449                         $off++;
450                         next;
451                 }
452                 # Regular quotes.
453                 if ($c eq "'" || $c eq '"') {
454                         if ($sanitise_quote eq '') {
455                                 $sanitise_quote = $c;
456
457                                 substr($res, $off, 1, $c);
458                                 next;
459                         } elsif ($sanitise_quote eq $c) {
460                                 $sanitise_quote = '';
461                         }
462                 }
463
464                 #print "c<$c> SQ<$sanitise_quote>\n";
465                 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
466                         substr($res, $off, 1, $;);
467                 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
468                         substr($res, $off, 1, $;);
469                 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
470                         substr($res, $off, 1, 'X');
471                 } else {
472                         substr($res, $off, 1, $c);
473                 }
474         }
475
476         if ($sanitise_quote eq '//') {
477                 $sanitise_quote = '';
478         }
479
480         # The pathname on a #include may be surrounded by '<' and '>'.
481         if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
482                 my $clean = 'X' x length($1);
483                 $res =~ s@\<.*\>@<$clean>@;
484
485         # The whole of a #error is a string.
486         } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
487                 my $clean = 'X' x length($1);
488                 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
489         }
490
491         return $res;
492 }
493
494 sub ctx_statement_block {
495         my ($linenr, $remain, $off) = @_;
496         my $line = $linenr - 1;
497         my $blk = '';
498         my $soff = $off;
499         my $coff = $off - 1;
500         my $coff_set = 0;
501
502         my $loff = 0;
503
504         my $type = '';
505         my $level = 0;
506         my @stack = ();
507         my $p;
508         my $c;
509         my $len = 0;
510
511         my $remainder;
512         while (1) {
513                 @stack = (['', 0]) if ($#stack == -1);
514
515                 #warn "CSB: blk<$blk> remain<$remain>\n";
516                 # If we are about to drop off the end, pull in more
517                 # context.
518                 if ($off >= $len) {
519                         for (; $remain > 0; $line++) {
520                                 last if (!defined $lines[$line]);
521                                 next if ($lines[$line] =~ /^-/);
522                                 $remain--;
523                                 $loff = $len;
524                                 $blk .= $lines[$line] . "\n";
525                                 $len = length($blk);
526                                 $line++;
527                                 last;
528                         }
529                         # Bail if there is no further context.
530                         #warn "CSB: blk<$blk> off<$off> len<$len>\n";
531                         if ($off >= $len) {
532                                 last;
533                         }
534                 }
535                 $p = $c;
536                 $c = substr($blk, $off, 1);
537                 $remainder = substr($blk, $off);
538
539                 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
540
541                 # Handle nested #if/#else.
542                 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
543                         push(@stack, [ $type, $level ]);
544                 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
545                         ($type, $level) = @{$stack[$#stack - 1]};
546                 } elsif ($remainder =~ /^#\s*endif\b/) {
547                         ($type, $level) = @{pop(@stack)};
548                 }
549
550                 # Statement ends at the ';' or a close '}' at the
551                 # outermost level.
552                 if ($level == 0 && $c eq ';') {
553                         last;
554                 }
555
556                 # An else is really a conditional as long as its not else if
557                 if ($level == 0 && $coff_set == 0 &&
558                                 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
559                                 $remainder =~ /^(else)(?:\s|{)/ &&
560                                 $remainder !~ /^else\s+if\b/) {
561                         $coff = $off + length($1) - 1;
562                         $coff_set = 1;
563                         #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
564                         #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
565                 }
566
567                 if (($type eq '' || $type eq '(') && $c eq '(') {
568                         $level++;
569                         $type = '(';
570                 }
571                 if ($type eq '(' && $c eq ')') {
572                         $level--;
573                         $type = ($level != 0)? '(' : '';
574
575                         if ($level == 0 && $coff < $soff) {
576                                 $coff = $off;
577                                 $coff_set = 1;
578                                 #warn "CSB: mark coff<$coff>\n";
579                         }
580                 }
581                 if (($type eq '' || $type eq '{') && $c eq '{') {
582                         $level++;
583                         $type = '{';
584                 }
585                 if ($type eq '{' && $c eq '}') {
586                         $level--;
587                         $type = ($level != 0)? '{' : '';
588
589                         if ($level == 0) {
590                                 if (substr($blk, $off + 1, 1) eq ';') {
591                                         $off++;
592                                 }
593                                 last;
594                         }
595                 }
596                 $off++;
597         }
598         # We are truly at the end, so shuffle to the next line.
599         if ($off == $len) {
600                 $loff = $len + 1;
601                 $line++;
602                 $remain--;
603         }
604
605         my $statement = substr($blk, $soff, $off - $soff + 1);
606         my $condition = substr($blk, $soff, $coff - $soff + 1);
607
608         #warn "STATEMENT<$statement>\n";
609         #warn "CONDITION<$condition>\n";
610
611         #print "coff<$coff> soff<$off> loff<$loff>\n";
612
613         return ($statement, $condition,
614                         $line, $remain + 1, $off - $loff + 1, $level);
615 }
616
617 sub statement_lines {
618         my ($stmt) = @_;
619
620         # Strip the diff line prefixes and rip blank lines at start and end.
621         $stmt =~ s/(^|\n)./$1/g;
622         $stmt =~ s/^\s*//;
623         $stmt =~ s/\s*$//;
624
625         my @stmt_lines = ($stmt =~ /\n/g);
626
627         return $#stmt_lines + 2;
628 }
629
630 sub statement_rawlines {
631         my ($stmt) = @_;
632
633         my @stmt_lines = ($stmt =~ /\n/g);
634
635         return $#stmt_lines + 2;
636 }
637
638 sub statement_block_size {
639         my ($stmt) = @_;
640
641         $stmt =~ s/(^|\n)./$1/g;
642         $stmt =~ s/^\s*{//;
643         $stmt =~ s/}\s*$//;
644         $stmt =~ s/^\s*//;
645         $stmt =~ s/\s*$//;
646
647         my @stmt_lines = ($stmt =~ /\n/g);
648         my @stmt_statements = ($stmt =~ /;/g);
649
650         my $stmt_lines = $#stmt_lines + 2;
651         my $stmt_statements = $#stmt_statements + 1;
652
653         if ($stmt_lines > $stmt_statements) {
654                 return $stmt_lines;
655         } else {
656                 return $stmt_statements;
657         }
658 }
659
660 sub ctx_statement_full {
661         my ($linenr, $remain, $off) = @_;
662         my ($statement, $condition, $level);
663
664         my (@chunks);
665
666         # Grab the first conditional/block pair.
667         ($statement, $condition, $linenr, $remain, $off, $level) =
668                                 ctx_statement_block($linenr, $remain, $off);
669         #print "F: c<$condition> s<$statement> remain<$remain>\n";
670         push(@chunks, [ $condition, $statement ]);
671         if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
672                 return ($level, $linenr, @chunks);
673         }
674
675         # Pull in the following conditional/block pairs and see if they
676         # could continue the statement.
677         for (;;) {
678                 ($statement, $condition, $linenr, $remain, $off, $level) =
679                                 ctx_statement_block($linenr, $remain, $off);
680                 #print "C: c<$condition> s<$statement> remain<$remain>\n";
681                 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
682                 #print "C: push\n";
683                 push(@chunks, [ $condition, $statement ]);
684         }
685
686         return ($level, $linenr, @chunks);
687 }
688
689 sub ctx_block_get {
690         my ($linenr, $remain, $outer, $open, $close, $off) = @_;
691         my $line;
692         my $start = $linenr - 1;
693         my $blk = '';
694         my @o;
695         my @c;
696         my @res = ();
697
698         my $level = 0;
699         my @stack = ($level);
700         for ($line = $start; $remain > 0; $line++) {
701                 next if ($rawlines[$line] =~ /^-/);
702                 $remain--;
703
704                 $blk .= $rawlines[$line];
705
706                 # Handle nested #if/#else.
707                 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
708                         push(@stack, $level);
709                 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
710                         $level = $stack[$#stack - 1];
711                 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
712                         $level = pop(@stack);
713                 }
714
715                 foreach my $c (split(//, $lines[$line])) {
716                         ##print "C<$c>L<$level><$open$close>O<$off>\n";
717                         if ($off > 0) {
718                                 $off--;
719                                 next;
720                         }
721
722                         if ($c eq $close && $level > 0) {
723                                 $level--;
724                                 last if ($level == 0);
725                         } elsif ($c eq $open) {
726                                 $level++;
727                         }
728                 }
729
730                 if (!$outer || $level <= 1) {
731                         push(@res, $rawlines[$line]);
732                 }
733
734                 last if ($level == 0);
735         }
736
737         return ($level, @res);
738 }
739 sub ctx_block_outer {
740         my ($linenr, $remain) = @_;
741
742         my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
743         return @r;
744 }
745 sub ctx_block {
746         my ($linenr, $remain) = @_;
747
748         my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
749         return @r;
750 }
751 sub ctx_statement {
752         my ($linenr, $remain, $off) = @_;
753
754         my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
755         return @r;
756 }
757 sub ctx_block_level {
758         my ($linenr, $remain) = @_;
759
760         return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
761 }
762 sub ctx_statement_level {
763         my ($linenr, $remain, $off) = @_;
764
765         return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
766 }
767
768 sub ctx_locate_comment {
769         my ($first_line, $end_line) = @_;
770
771         # Catch a comment on the end of the line itself.
772         my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
773         return $current_comment if (defined $current_comment);
774
775         # Look through the context and try and figure out if there is a
776         # comment.
777         my $in_comment = 0;
778         $current_comment = '';
779         for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
780                 my $line = $rawlines[$linenr - 1];
781                 #warn "           $line\n";
782                 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
783                         $in_comment = 1;
784                 }
785                 if ($line =~ m@/\*@) {
786                         $in_comment = 1;
787                 }
788                 if (!$in_comment && $current_comment ne '') {
789                         $current_comment = '';
790                 }
791                 $current_comment .= $line . "\n" if ($in_comment);
792                 if ($line =~ m@\*/@) {
793                         $in_comment = 0;
794                 }
795         }
796
797         chomp($current_comment);
798         return($current_comment);
799 }
800 sub ctx_has_comment {
801         my ($first_line, $end_line) = @_;
802         my $cmt = ctx_locate_comment($first_line, $end_line);
803
804         ##print "LINE: $rawlines[$end_line - 1 ]\n";
805         ##print "CMMT: $cmt\n";
806
807         return ($cmt ne '');
808 }
809
810 sub raw_line {
811         my ($linenr, $cnt) = @_;
812
813         my $offset = $linenr - 1;
814         $cnt++;
815
816         my $line;
817         while ($cnt) {
818                 $line = $rawlines[$offset++];
819                 next if (defined($line) && $line =~ /^-/);
820                 $cnt--;
821         }
822
823         return $line;
824 }
825
826 sub cat_vet {
827         my ($vet) = @_;
828         my ($res, $coded);
829
830         $res = '';
831         while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
832                 $res .= $1;
833                 if ($2 ne '') {
834                         $coded = sprintf("^%c", unpack('C', $2) + 64);
835                         $res .= $coded;
836                 }
837         }
838         $res =~ s/$/\$/;
839
840         return $res;
841 }
842
843 my $av_preprocessor = 0;
844 my $av_pending;
845 my @av_paren_type;
846 my $av_pend_colon;
847
848 sub annotate_reset {
849         $av_preprocessor = 0;
850         $av_pending = '_';
851         @av_paren_type = ('E');
852         $av_pend_colon = 'O';
853 }
854
855 sub annotate_values {
856         my ($stream, $type) = @_;
857
858         my $res;
859         my $var = '_' x length($stream);
860         my $cur = $stream;
861
862         print "$stream\n" if ($dbg_values > 1);
863
864         while (length($cur)) {
865                 @av_paren_type = ('E') if ($#av_paren_type < 0);
866                 print " <" . join('', @av_paren_type) .
867                                 "> <$type> <$av_pending>" if ($dbg_values > 1);
868                 if ($cur =~ /^(\s+)/o) {
869                         print "WS($1)\n" if ($dbg_values > 1);
870                         if ($1 =~ /\n/ && $av_preprocessor) {
871                                 $type = pop(@av_paren_type);
872                                 $av_preprocessor = 0;
873                         }
874
875                 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/) {
876                         print "CAST($1)\n" if ($dbg_values > 1);
877                         push(@av_paren_type, $type);
878                         $type = 'C';
879
880                 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
881                         print "DECLARE($1)\n" if ($dbg_values > 1);
882                         $type = 'T';
883
884                 } elsif ($cur =~ /^($Modifier)\s*/) {
885                         print "MODIFIER($1)\n" if ($dbg_values > 1);
886                         $type = 'T';
887
888                 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
889                         print "DEFINE($1,$2)\n" if ($dbg_values > 1);
890                         $av_preprocessor = 1;
891                         push(@av_paren_type, $type);
892                         if ($2 ne '') {
893                                 $av_pending = 'N';
894                         }
895                         $type = 'E';
896
897                 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
898                         print "UNDEF($1)\n" if ($dbg_values > 1);
899                         $av_preprocessor = 1;
900                         push(@av_paren_type, $type);
901
902                 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
903                         print "PRE_START($1)\n" if ($dbg_values > 1);
904                         $av_preprocessor = 1;
905
906                         push(@av_paren_type, $type);
907                         push(@av_paren_type, $type);
908                         $type = 'E';
909
910                 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
911                         print "PRE_RESTART($1)\n" if ($dbg_values > 1);
912                         $av_preprocessor = 1;
913
914                         push(@av_paren_type, $av_paren_type[$#av_paren_type]);
915
916                         $type = 'E';
917
918                 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
919                         print "PRE_END($1)\n" if ($dbg_values > 1);
920
921                         $av_preprocessor = 1;
922
923                         # Assume all arms of the conditional end as this
924                         # one does, and continue as if the #endif was not here.
925                         pop(@av_paren_type);
926                         push(@av_paren_type, $type);
927                         $type = 'E';
928
929                 } elsif ($cur =~ /^(\\\n)/o) {
930                         print "PRECONT($1)\n" if ($dbg_values > 1);
931
932                 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
933                         print "ATTR($1)\n" if ($dbg_values > 1);
934                         $av_pending = $type;
935                         $type = 'N';
936
937                 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
938                         print "SIZEOF($1)\n" if ($dbg_values > 1);
939                         if (defined $2) {
940                                 $av_pending = 'V';
941                         }
942                         $type = 'N';
943
944                 } elsif ($cur =~ /^(if|while|for)\b/o) {
945                         print "COND($1)\n" if ($dbg_values > 1);
946                         $av_pending = 'E';
947                         $type = 'N';
948
949                 } elsif ($cur =~/^(case)/o) {
950                         print "CASE($1)\n" if ($dbg_values > 1);
951                         $av_pend_colon = 'C';
952                         $type = 'N';
953
954                 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
955                         print "KEYWORD($1)\n" if ($dbg_values > 1);
956                         $type = 'N';
957
958                 } elsif ($cur =~ /^(\()/o) {
959                         print "PAREN('$1')\n" if ($dbg_values > 1);
960                         push(@av_paren_type, $av_pending);
961                         $av_pending = '_';
962                         $type = 'N';
963
964                 } elsif ($cur =~ /^(\))/o) {
965                         my $new_type = pop(@av_paren_type);
966                         if ($new_type ne '_') {
967                                 $type = $new_type;
968                                 print "PAREN('$1') -> $type\n"
969                                                         if ($dbg_values > 1);
970                         } else {
971                                 print "PAREN('$1')\n" if ($dbg_values > 1);
972                         }
973
974                 } elsif ($cur =~ /^($Ident)\s*\(/o) {
975                         print "FUNC($1)\n" if ($dbg_values > 1);
976                         $type = 'V';
977                         $av_pending = 'V';
978
979                 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
980                         if (defined $2 && $type eq 'C' || $type eq 'T') {
981                                 $av_pend_colon = 'B';
982                         } elsif ($type eq 'E') {
983                                 $av_pend_colon = 'L';
984                         }
985                         print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
986                         $type = 'V';
987
988                 } elsif ($cur =~ /^($Ident|$Constant)/o) {
989                         print "IDENT($1)\n" if ($dbg_values > 1);
990                         $type = 'V';
991
992                 } elsif ($cur =~ /^($Assignment)/o) {
993                         print "ASSIGN($1)\n" if ($dbg_values > 1);
994                         $type = 'N';
995
996                 } elsif ($cur =~/^(;|{|})/) {
997                         print "END($1)\n" if ($dbg_values > 1);
998                         $type = 'E';
999                         $av_pend_colon = 'O';
1000
1001                 } elsif ($cur =~/^(,)/) {
1002                         print "COMMA($1)\n" if ($dbg_values > 1);
1003                         $type = 'C';
1004
1005                 } elsif ($cur =~ /^(\?)/o) {
1006                         print "QUESTION($1)\n" if ($dbg_values > 1);
1007                         $type = 'N';
1008
1009                 } elsif ($cur =~ /^(:)/o) {
1010                         print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1011
1012                         substr($var, length($res), 1, $av_pend_colon);
1013                         if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1014                                 $type = 'E';
1015                         } else {
1016                                 $type = 'N';
1017                         }
1018                         $av_pend_colon = 'O';
1019
1020                 } elsif ($cur =~ /^(\[)/o) {
1021                         print "CLOSE($1)\n" if ($dbg_values > 1);
1022                         $type = 'N';
1023
1024                 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1025                         my $variant;
1026
1027                         print "OPV($1)\n" if ($dbg_values > 1);
1028                         if ($type eq 'V') {
1029                                 $variant = 'B';
1030                         } else {
1031                                 $variant = 'U';
1032                         }
1033
1034                         substr($var, length($res), 1, $variant);
1035                         $type = 'N';
1036
1037                 } elsif ($cur =~ /^($Operators)/o) {
1038                         print "OP($1)\n" if ($dbg_values > 1);
1039                         if ($1 ne '++' && $1 ne '--') {
1040                                 $type = 'N';
1041                         }
1042
1043                 } elsif ($cur =~ /(^.)/o) {
1044                         print "C($1)\n" if ($dbg_values > 1);
1045                 }
1046                 if (defined $1) {
1047                         $cur = substr($cur, length($1));
1048                         $res .= $type x length($1);
1049                 }
1050         }
1051
1052         return ($res, $var);
1053 }
1054
1055 sub possible {
1056         my ($possible, $line) = @_;
1057         my $notPermitted = qr{(?:
1058                 ^(?:
1059                         $Modifier|
1060                         $Storage|
1061                         $Type|
1062                         DEFINE_\S+
1063                 )$|
1064                 ^(?:
1065                         goto|
1066                         return|
1067                         case|
1068                         else|
1069                         asm|__asm__|
1070                         do
1071                 )(?:\s|$)|
1072                 ^(?:typedef|struct|enum)\b
1073             )}x;
1074         warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1075         if ($possible !~ $notPermitted) {
1076                 # Check for modifiers.
1077                 $possible =~ s/\s*$Storage\s*//g;
1078                 $possible =~ s/\s*$Sparse\s*//g;
1079                 if ($possible =~ /^\s*$/) {
1080
1081                 } elsif ($possible =~ /\s/) {
1082                         $possible =~ s/\s*$Type\s*//g;
1083                         for my $modifier (split(' ', $possible)) {
1084                                 if ($modifier !~ $notPermitted) {
1085                                         warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1086                                         push(@modifierList, $modifier);
1087                                 }
1088                         }
1089
1090                 } else {
1091                         warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1092                         push(@typeList, $possible);
1093                 }
1094                 build_types();
1095         } else {
1096                 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1097         }
1098 }
1099
1100 my $prefix = '';
1101
1102 sub report {
1103         if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1104                 return 0;
1105         }
1106         my $line = $prefix . $_[0];
1107
1108         $line = (split('\n', $line))[0] . "\n" if ($terse);
1109
1110         push(our @report, $line);
1111
1112         return 1;
1113 }
1114 sub report_dump {
1115         our @report;
1116 }
1117 sub ERROR {
1118         if (report("ERROR: $_[0]\n")) {
1119                 our $clean = 0;
1120                 our $cnt_error++;
1121         }
1122 }
1123 sub WARN {
1124         if (report("WARNING: $_[0]\n")) {
1125                 our $clean = 0;
1126                 our $cnt_warn++;
1127         }
1128 }
1129 sub CHK {
1130         if ($check && report("CHECK: $_[0]\n")) {
1131                 our $clean = 0;
1132                 our $cnt_chk++;
1133         }
1134 }
1135
1136 sub check_absolute_file {
1137         my ($absolute, $herecurr) = @_;
1138         my $file = $absolute;
1139
1140         ##print "absolute<$absolute>\n";
1141
1142         # See if any suffix of this path is a path within the tree.
1143         while ($file =~ s@^[^/]*/@@) {
1144                 if (-f "$root/$file") {
1145                         ##print "file<$file>\n";
1146                         last;
1147                 }
1148         }
1149         if (! -f _)  {
1150                 return 0;
1151         }
1152
1153         # It is, so see if the prefix is acceptable.
1154         my $prefix = $absolute;
1155         substr($prefix, -length($file)) = '';
1156
1157         ##print "prefix<$prefix>\n";
1158 #       if ($prefix ne ".../") {
1159 #               WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1160 #       }
1161 }
1162
1163 sub process {
1164         my $filename = shift;
1165
1166         my $linenr=0;
1167         my $prevline="";
1168         my $prevrawline="";
1169         my $stashline="";
1170         my $stashrawline="";
1171
1172         my $length;
1173         my $indent;
1174         my $previndent=0;
1175         my $stashindent=0;
1176
1177         our $clean = 1;
1178         my $signoff = 0;
1179         my $is_patch = 0;
1180
1181         our @report = ();
1182         our $cnt_lines = 0;
1183         our $cnt_error = 0;
1184         our $cnt_warn = 0;
1185         our $cnt_chk = 0;
1186
1187         # Trace the real file/line as we go.
1188         my $realfile = '';
1189         my $realline = 0;
1190         my $realcnt = 0;
1191         my $here = '';
1192         my $in_comment = 0;
1193         my $comment_edge = 0;
1194         my $first_line = 0;
1195         my $p1_prefix = '';
1196
1197         my $prev_values = 'E';
1198
1199         # suppression flags
1200         my %suppress_ifbraces;
1201         my %suppress_whiletrailers;
1202         my %suppress_export;
1203
1204         # Pre-scan the patch sanitizing the lines.
1205         # Pre-scan the patch looking for any __setup documentation.
1206         #
1207         my @setup_docs = ();
1208         my $setup_docs = 0;
1209
1210         sanitise_line_reset();
1211         my $line;
1212         foreach my $rawline (@rawlines) {
1213                 $linenr++;
1214                 $line = $rawline;
1215
1216                 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1217                         $setup_docs = 0;
1218                         if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1219                                 $setup_docs = 1;
1220                         }
1221                         #next;
1222                 }
1223                 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1224                         $realline=$1-1;
1225                         if (defined $2) {
1226                                 $realcnt=$3+1;
1227                         } else {
1228                                 $realcnt=1+1;
1229                         }
1230                         $in_comment = 0;
1231
1232                         # Guestimate if this is a continuing comment.  Run
1233                         # the context looking for a comment "edge".  If this
1234                         # edge is a close comment then we must be in a comment
1235                         # at context start.
1236                         my $edge;
1237                         my $cnt = $realcnt;
1238                         for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1239                                 next if (defined $rawlines[$ln - 1] &&
1240                                          $rawlines[$ln - 1] =~ /^-/);
1241                                 $cnt--;
1242                                 #print "RAW<$rawlines[$ln - 1]>\n";
1243                                 last if (!defined $rawlines[$ln - 1]);
1244                                 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1245                                     $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1246                                         ($edge) = $1;
1247                                         last;
1248                                 }
1249                         }
1250                         if (defined $edge && $edge eq '*/') {
1251                                 $in_comment = 1;
1252                         }
1253
1254                         # Guestimate if this is a continuing comment.  If this
1255                         # is the start of a diff block and this line starts
1256                         # ' *' then it is very likely a comment.
1257                         if (!defined $edge &&
1258                             $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1259                         {
1260                                 $in_comment = 1;
1261                         }
1262
1263                         ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1264                         sanitise_line_reset($in_comment);
1265
1266                 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1267                         # Standardise the strings and chars within the input to
1268                         # simplify matching -- only bother with positive lines.
1269                         $line = sanitise_line($rawline);
1270                 }
1271                 push(@lines, $line);
1272
1273                 if ($realcnt > 1) {
1274                         $realcnt-- if ($line =~ /^(?:\+| |$)/);
1275                 } else {
1276                         $realcnt = 0;
1277                 }
1278
1279                 #print "==>$rawline\n";
1280                 #print "-->$line\n";
1281
1282                 if ($setup_docs && $line =~ /^\+/) {
1283                         push(@setup_docs, $line);
1284                 }
1285         }
1286
1287         $prefix = '';
1288
1289         $realcnt = 0;
1290         $linenr = 0;
1291         foreach my $line (@lines) {
1292                 $linenr++;
1293
1294                 my $rawline = $rawlines[$linenr - 1];
1295
1296 #extract the line range in the file after the patch is applied
1297                 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1298                         $is_patch = 1;
1299                         $first_line = $linenr + 1;
1300                         $realline=$1-1;
1301                         if (defined $2) {
1302                                 $realcnt=$3+1;
1303                         } else {
1304                                 $realcnt=1+1;
1305                         }
1306                         annotate_reset();
1307                         $prev_values = 'E';
1308
1309                         %suppress_ifbraces = ();
1310                         %suppress_whiletrailers = ();
1311                         %suppress_export = ();
1312                         next;
1313
1314 # track the line number as we move through the hunk, note that
1315 # new versions of GNU diff omit the leading space on completely
1316 # blank context lines so we need to count that too.
1317                 } elsif ($line =~ /^( |\+|$)/) {
1318                         $realline++;
1319                         $realcnt-- if ($realcnt != 0);
1320
1321                         # Measure the line length and indent.
1322                         ($length, $indent) = line_stats($rawline);
1323
1324                         # Track the previous line.
1325                         ($prevline, $stashline) = ($stashline, $line);
1326                         ($previndent, $stashindent) = ($stashindent, $indent);
1327                         ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1328
1329                         #warn "line<$line>\n";
1330
1331                 } elsif ($realcnt == 1) {
1332                         $realcnt--;
1333                 }
1334
1335                 my $hunk_line = ($realcnt != 0);
1336
1337 #make up the handle for any error we report on this line
1338                 $prefix = "$filename:$realline: " if ($emacs && $file);
1339                 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1340
1341                 $here = "#$linenr: " if (!$file);
1342                 $here = "#$realline: " if ($file);
1343
1344                 # extract the filename as it passes
1345                 if ($line =~ /^diff --git.*?(\S+)$/) {
1346                         $realfile = $1;
1347                         $realfile =~ s@^([^/]*)/@@;
1348
1349                 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1350                         $realfile = $1;
1351                         $realfile =~ s@^([^/]*)/@@;
1352
1353                         $p1_prefix = $1;
1354 #                       if (!$file && $tree && $p1_prefix ne '' &&
1355 #                           -e "$root/$p1_prefix") {
1356 #                               WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1357 #                       }
1358 #
1359 #                       if ($realfile =~ m@^include/asm/@) {
1360 #                               ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1361 #                       }
1362                         next;
1363                 }
1364
1365                 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1366
1367                 my $hereline = "$here\n$rawline\n";
1368                 my $herecurr = "$here\n$rawline\n";
1369                 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1370
1371                 $cnt_lines++ if ($realcnt != 0);
1372
1373 # Check for incorrect file permissions
1374 #               if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1375 #                       my $permhere = $here . "FILE: $realfile\n";
1376 #                       if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1377 #                               ERROR("do not set execute permissions for source files\n" . $permhere);
1378 #                       }
1379 #               }
1380
1381 #check the patch for a signoff:
1382 #               if ($line =~ /^\s*signed-off-by:/i) {
1383 #                       # This is a signoff, if ugly, so do not double report.
1384 #                       $signoff++;
1385 #                       if (!($line =~ /^\s*Signed-off-by:/)) {
1386 #                               WARN("Signed-off-by: is the preferred form\n" .
1387 #                                       $herecurr);
1388 #                       }
1389 #                       if ($line =~ /^\s*signed-off-by:\S/i) {
1390 #                               WARN("space required after Signed-off-by:\n" .
1391 #                                       $herecurr);
1392 #                       }
1393 #               }
1394
1395 # Check for wrappage within a valid hunk of the file
1396 #               if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1397 #                       ERROR("patch seems to be corrupt (line wrapped?)\n" .
1398 #                               $herecurr) if (!$emitted_corrupt++);
1399 #               }
1400
1401 # Check for absolute kernel paths.
1402                 if ($tree) {
1403                         while ($line =~ m{(?:^|\s)(/\S*)}g) {
1404                                 my $file = $1;
1405
1406                                 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1407                                     check_absolute_file($1, $herecurr)) {
1408                                         #
1409                                 } else {
1410                                         check_absolute_file($file, $herecurr);
1411                                 }
1412                         }
1413                 }
1414
1415 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1416 #               if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1417 #                   $rawline !~ m/^$UTF8*$/) {
1418 #                       my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1419 #
1420 #                       my $blank = copy_spacing($rawline);
1421 #                       my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1422 #                       my $hereptr = "$hereline$ptr\n";
1423 #
1424 #                       ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1425 #               }
1426
1427 # ignore non-hunk lines and lines being removed
1428                 next if (!$hunk_line || $line =~ /^-/);
1429
1430 #trailing whitespace
1431                 if ($line =~ /^\+.*\015/) {
1432 #                       my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1433 #                       WARN("[LNE_R_TWS]DOS line endings\n" . $herevet);
1434 #
1435                 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1436                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1437                         WARN("[LNE_R_TWS]trailing whitespace\n" . $herevet);
1438                         $rpt_cleaners = 1;
1439                 }
1440
1441 # check for Kconfig help text having a real description
1442 # Only applies when adding the entry originally, after that we do not have
1443 # sufficient context to determine whether it is indeed long enough.
1444 #               if ($realfile =~ /Kconfig/ &&
1445 #                   $line =~ /\+\s*(?:---)?help(?:---)?$/) {
1446 #                       my $length = 0;
1447 #                       my $cnt = $realcnt;
1448 #                       my $ln = $linenr + 1;
1449 #                       my $f;
1450 #                       my $is_end = 0;
1451 #                       while ($cnt > 0 && defined $lines[$ln - 1]) {
1452 #                               $f = $lines[$ln - 1];
1453 #                               $cnt-- if ($lines[$ln - 1] !~ /^-/);
1454 #                               $is_end = $lines[$ln - 1] =~ /^\+/;
1455 #                               $ln++;
1456 #
1457 #                               next if ($f =~ /^-/);
1458 #                               $f =~ s/^.//;
1459 #                               $f =~ s/#.*//;
1460 #                               $f =~ s/^\s+//;
1461 #                               next if ($f =~ /^$/);
1462 #                               if ($f =~ /^\s*config\s/) {
1463 #                                       $is_end = 1;
1464 #                                       last;
1465 #                               }
1466 #                               $length++;
1467 #                       }
1468 #                       WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1469 #                       #print "is_end<$is_end> length<$length>\n";
1470 #               }
1471
1472 # check we are in a valid source file if not then ignore this hunk
1473                 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1474
1475 #80 column limit
1476 #               if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1477 #                   $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1478 #                   !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ ||
1479 #                   $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1480 #                   $length > 80)
1481 #               {
1482 #                       WARN("[SMT_M02_L80] line over 80 characters\n" . $herecurr);
1483 #               }
1484
1485 # check for spaces before a quoted newline
1486 #               if ($rawline =~ /^.*\".*\s\\n/) {
1487 #                       WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1488 #               }
1489
1490 # check for adding lines without a newline.
1491                 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1492                         WARN("[LNE_R_EOF] adding a line without newline at end of file\n" . $herecurr);
1493                 }
1494
1495 # Blackfin: use hi/lo macros
1496 #               if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1497 #                       if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1498 #                               my $herevet = "$here\n" . cat_vet($line) . "\n";
1499 #                               ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1500 #                       }
1501 #                       if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1502 #                               my $herevet = "$here\n" . cat_vet($line) . "\n";
1503 #                               ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1504 #                       }
1505 #               }
1506
1507 # check we are in a valid source file C or perl if not then ignore this hunk
1508                 next if ($realfile !~ /\.(h|c|pl)$/);
1509
1510 # at the beginning of a line any tabs must come first and anything
1511 # more than 8 must use tabs.
1512                 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1513                     $rawline =~ /^\+\s*        \s*/) {
1514                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1515                         WARN("[IDT_R_TAB] code indent should use tabs where possible\n" . $herevet);
1516                         $rpt_cleaners = 1;
1517                 }
1518
1519 # check for space before tabs.
1520                 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1521                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1522                         WARN("[IDT_R_TAB] please, no space before tabs\n" . $herevet);
1523                 }
1524
1525 # check for spaces at the beginning of a line.
1526 # Exceptions:
1527 #  1) within comments
1528 #  2) indented preprocessor commands
1529 #  3) hanging labels
1530                 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/)  {
1531                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1532                         WARN("[IDT_R_TAB] please, no spaces at the start of a line\n" . $herevet);
1533                 }
1534
1535 # check we are in a valid C source file if not then ignore this hunk
1536                 next if ($realfile !~ /\.(h|c)$/);
1537
1538 # check for RCS/CVS revision markers
1539 #               if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1540 #                       WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1541 #               }
1542
1543 # Blackfin: don't use __builtin_bfin_[cs]sync
1544 #               if ($line =~ /__builtin_bfin_csync/) {
1545 #                       my $herevet = "$here\n" . cat_vet($line) . "\n";
1546 #                       ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1547 #               }
1548 #               if ($line =~ /__builtin_bfin_ssync/) {
1549 #                       my $herevet = "$here\n" . cat_vet($line) . "\n";
1550 #                       ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1551 #               }
1552
1553 # Check for potential 'bare' types
1554                 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1555                     $realline_next);
1556                 if ($realcnt && $line =~ /.\s*\S/) {
1557                         ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1558                                 ctx_statement_block($linenr, $realcnt, 0);
1559                         $stat =~ s/\n./\n /g;
1560                         $cond =~ s/\n./\n /g;
1561
1562                         # Find the real next line.
1563                         $realline_next = $line_nr_next;
1564                         if (defined $realline_next &&
1565                             (!defined $lines[$realline_next - 1] ||
1566                              substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1567                                 $realline_next++;
1568                         }
1569
1570                         my $s = $stat;
1571                         $s =~ s/{.*$//s;
1572
1573                         # Ignore goto labels.
1574                         if ($s =~ /$Ident:\*$/s) {
1575
1576                         # Ignore functions being called
1577                         } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1578
1579                         } elsif ($s =~ /^.\s*else\b/s) {
1580
1581                         # declarations always start with types
1582                         } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
1583                                 my $type = $1;
1584                                 $type =~ s/\s+/ /g;
1585                                 possible($type, "A:" . $s);
1586
1587                         # definitions in global scope can only start with types
1588                         } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1589                                 possible($1, "B:" . $s);
1590                         }
1591
1592                         # any (foo ... *) is a pointer cast, and foo is a type
1593                         while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1594                                 possible($1, "C:" . $s);
1595                         }
1596
1597                         # Check for any sort of function declaration.
1598                         # int foo(something bar, other baz);
1599                         # void (*store_gdt)(x86_descr_ptr *);
1600                         if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1601                                 my ($name_len) = length($1);
1602
1603                                 my $ctx = $s;
1604                                 substr($ctx, 0, $name_len + 1, '');
1605                                 $ctx =~ s/\)[^\)]*$//;
1606
1607                                 for my $arg (split(/\s*,\s*/, $ctx)) {
1608                                         if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1609
1610                                                 possible($1, "D:" . $s);
1611                                         }
1612                                 }
1613                         }
1614
1615                 }
1616
1617 #
1618 # Checks which may be anchored in the context.
1619 #
1620
1621 # Check for switch () and associated case and default
1622 # statements should be at the same indent.
1623                 if ($line=~/\bswitch\s*\(.*\)/) {
1624                         my $err = '';
1625                         my $sep = '';
1626                         my @ctx = ctx_block_outer($linenr, $realcnt);
1627                         shift(@ctx);
1628                         for my $ctx (@ctx) {
1629                                 my ($clen, $cindent) = line_stats($ctx);
1630                                 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1631                                                         $indent != $cindent) {
1632                                         $err .= "$sep$ctx\n";
1633                                         $sep = '';
1634                                 } else {
1635                                         $sep = "[...]\n";
1636                                 }
1637                         }
1638                         if ($err ne '') {
1639                                 WARN("[IDT_R_SCH] switch and case should be at the same indent\n$hereline$err");
1640                         }
1641                 }
1642
1643 # if/while/etc brace do not go on next line, unless defining a do while loop,
1644 # or if that brace on the next line is for something else
1645                 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1646                         my $pre_ctx = "$1$2";
1647
1648                         my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1649                         my $ctx_cnt = $realcnt - $#ctx - 1;
1650                         my $ctx = join("\n", @ctx);
1651
1652                         my $ctx_ln = $linenr;
1653                         my $ctx_skip = $realcnt;
1654
1655                         while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1656                                         defined $lines[$ctx_ln - 1] &&
1657                                         $lines[$ctx_ln - 1] =~ /^-/)) {
1658                                 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1659                                 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1660                                 $ctx_ln++;
1661                         }
1662
1663                         #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1664                         #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1665
1666                         if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1667                                 ERROR("[BRC_M_SMT] that open brace { should be on the previous line\n" .
1668                                         "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1669                         }
1670 #                       if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1671 #                           $ctx =~ /\)\s*\;\s*$/ &&
1672 #                           defined $lines[$ctx_ln - 1])
1673 #                       {
1674 #                               my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1675 #                               if ($nindent > $indent) {
1676 #                                       WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1677 #                                               "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1678 #                               }
1679 #                       }
1680                 }
1681
1682 # Check relative indent for conditionals and blocks.
1683 #               if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1684 #                       my ($s, $c) = ($stat, $cond);
1685 #
1686 #                       substr($s, 0, length($c), '');
1687 #
1688 #                       # Make sure we remove the line prefixes as we have
1689 #                       # none on the first line, and are going to readd them
1690 #                       # where necessary.
1691 #                       $s =~ s/\n./\n/gs;
1692 #
1693 #                       # Find out how long the conditional actually is.
1694 #                       my @newlines = ($c =~ /\n/gs);
1695 #                       my $cond_lines = 1 + $#newlines;
1696 #
1697 #                       # We want to check the first line inside the block
1698 #                       # starting at the end of the conditional, so remove:
1699 #                       #  1) any blank line termination
1700 #                       #  2) any opening brace { on end of the line
1701 #                       #  3) any do (...) {
1702 #                       my $continuation = 0;
1703 #                       my $check = 0;
1704 #                       $s =~ s/^.*\bdo\b//;
1705 #                       $s =~ s/^\s*{//;
1706 #                       if ($s =~ s/^\s*\\//) {
1707 #                               $continuation = 1;
1708 #                       }
1709 #                       if ($s =~ s/^\s*?\n//) {
1710 #                               $check = 1;
1711 #                               $cond_lines++;
1712 #                       }
1713 #
1714 #                       # Also ignore a loop construct at the end of a
1715 #                       # preprocessor statement.
1716 #                       if (($prevline =~ /^.\s*#\s*define\s/ ||
1717 #                           $prevline =~ /\\\s*$/) && $continuation == 0) {
1718 #                               $check = 0;
1719 #                       }
1720 #
1721 #                       my $cond_ptr = -1;
1722 #                       $continuation = 0;
1723 #                       while ($cond_ptr != $cond_lines) {
1724 #                               $cond_ptr = $cond_lines;
1725 #
1726 #                               # If we see an #else/#elif then the code
1727 #                               # is not linear.
1728 #                               if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1729 #                                       $check = 0;
1730 #                               }
1731 #
1732 #                               # Ignore:
1733 #                               #  1) blank lines, they should be at 0,
1734 #                               #  2) preprocessor lines, and
1735 #                               #  3) labels.
1736 #                               if ($continuation ||
1737 #                                   $s =~ /^\s*?\n/ ||
1738 #                                   $s =~ /^\s*#\s*?/ ||
1739 #                                   $s =~ /^\s*$Ident\s*:/) {
1740 #                                       $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1741 #                                       if ($s =~ s/^.*?\n//) {
1742 #                                               $cond_lines++;
1743 #                                       }
1744 #                               }
1745 #                       }
1746 #
1747 #                       my (undef, $sindent) = line_stats("+" . $s);
1748 #                       my $stat_real = raw_line($linenr, $cond_lines);
1749 #
1750 #                       # Check if either of these lines are modified, else
1751 #                       # this is not this patch's fault.
1752 #                       if (!defined($stat_real) ||
1753 #                           $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1754 #                               $check = 0;
1755 #                       }
1756 #                       if (defined($stat_real) && $cond_lines > 1) {
1757 #                               $stat_real = "[...]\n$stat_real";
1758 #                       }
1759 #
1760 #                       #print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
1761 #
1762 #                       if ($check && (($sindent % 8) != 0 ||
1763 #                           ($sindent <= $indent && $s ne ''))) {
1764 #                               WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1765 #                       }
1766 #               }
1767
1768                 # Track the 'values' across context and added lines.
1769                 my $opline = $line; $opline =~ s/^./ /;
1770                 my ($curr_values, $curr_vars) =
1771                                 annotate_values($opline . "\n", $prev_values);
1772                 $curr_values = $prev_values . $curr_values;
1773                 if ($dbg_values) {
1774                         my $outline = $opline; $outline =~ s/\t/ /g;
1775                         print "$linenr > .$outline\n";
1776                         print "$linenr > $curr_values\n";
1777                         print "$linenr >  $curr_vars\n";
1778                 }
1779                 $prev_values = substr($curr_values, -1);
1780
1781 #ignore lines not being added
1782                 if ($line=~/^[^\+]/) {next;}
1783
1784 # TEST: allow direct testing of the type matcher.
1785 #               if ($dbg_type) {
1786 #                       if ($line =~ /^.\s*$Declare\s*$/) {
1787 #                               ERROR("TEST: is type\n" . $herecurr);
1788 #                       } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1789 #                               ERROR("TEST: is not type ($1 is)\n". $herecurr);
1790 #                       }
1791 #                       next;
1792 #               }
1793 # TEST: allow direct testing of the attribute matcher.
1794 #               if ($dbg_attr) {
1795 #                       if ($line =~ /^.\s*$Modifier\s*$/) {
1796 #                               ERROR("TEST: is attr\n" . $herecurr);
1797 #                       } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1798 #                               ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1799 #                       }
1800 #                       next;
1801 #               }
1802
1803 # check for initialisation to aggregates open brace on the next line
1804                 if ($line =~ /^.\s*{/ &&
1805                     $prevline =~ /(?:^|[^=])=\s*$/) {
1806                         ERROR("[BRC_M_SMT] that open brace { should be on the previous line\n" . $hereprev);
1807                 }
1808
1809 #
1810 # Checks which are anchored on the added line.
1811 #
1812
1813 # check for malformed paths in #include statements (uses RAW line)
1814 #               if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1815 #                       my $path = $1;
1816 #                       if ($path =~ m{//}) {
1817 #                               ERROR("malformed #include filename\n" .
1818 #                                       $herecurr);
1819 #                       }
1820 #               }
1821
1822 # no C99 // comments
1823                 if ($line =~ m{//}) {
1824                         WARN("[CMT_M_C89] do not use C99 // comments\n" . $herecurr);
1825                 }
1826                 # Remove C99 comments.
1827                 $line =~ s@//.*@@;
1828                 $opline =~ s@//.*@@;
1829
1830 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1831 # the whole statement.
1832 #print "APW <$lines[$realline_next - 1]>\n";
1833                 if (defined $realline_next &&
1834                     exists $lines[$realline_next - 1] &&
1835                     !defined $suppress_export{$realline_next} &&
1836                     ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1837                      $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1838                         # Handle definitions which produce identifiers with
1839                         # a prefix:
1840                         #   XXX(foo);
1841                         #   EXPORT_SYMBOL(something_foo);
1842                         my $name = $1;
1843                         if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1844                             $name =~ /^${Ident}_$2/) {
1845 #print "FOO C name<$name>\n";
1846                                 $suppress_export{$realline_next} = 1;
1847
1848                         } elsif ($stat !~ /(?:
1849                                 \n.}\s*$|
1850                                 ^.DEFINE_$Ident\(\Q$name\E\)|
1851                                 ^.DECLARE_$Ident\(\Q$name\E\)|
1852                                 ^.LIST_HEAD\(\Q$name\E\)|
1853                                 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1854                                 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
1855                             )/x) {
1856 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1857                                 $suppress_export{$realline_next} = 2;
1858                         } else {
1859                                 $suppress_export{$realline_next} = 1;
1860                         }
1861                 }
1862                 if (!defined $suppress_export{$linenr} &&
1863                     $prevline =~ /^.\s*$/ &&
1864                     ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1865                      $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1866 #print "FOO B <$lines[$linenr - 1]>\n";
1867                         $suppress_export{$linenr} = 2;
1868                 }
1869 #               if (defined $suppress_export{$linenr} &&
1870 #                   $suppress_export{$linenr} == 2) {
1871 #                       WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1872 #               }
1873
1874 # check for global initialisers.
1875 #               if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1876 #                       ERROR("do not initialise globals to 0 or NULL\n" .
1877 #                               $herecurr);
1878 #               }
1879 # check for static initialisers.
1880 #               if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1881 #                       ERROR("do not initialise statics to 0 or NULL\n" .
1882 #                               $herecurr);
1883 #               }
1884
1885 # check for static const char * arrays.
1886 #               if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1887 #                       WARN("static const char * array should probably be static const char * const\n" .
1888 #                               $herecurr);
1889 #               }
1890
1891 # check for static char foo[] = "bar" declarations.
1892 #               if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
1893 #                       WARN("static char array declaration should probably be static const char\n" .
1894 #                               $herecurr);
1895 #               }
1896
1897 # check for declarations of struct pci_device_id
1898 #               if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
1899 #                       WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
1900 #               }
1901
1902 # check for new typedefs, only function parameters and sparse annotations
1903 # make sense.
1904 #               if ($line =~ /\btypedef\s/ &&
1905 #                   $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1906 #                   $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1907 #                   $line !~ /\b$typeTypedefs\b/ &&
1908 #                   $line !~ /\b__bitwise(?:__|)\b/) {
1909 #                       WARN("do not add new typedefs\n" . $herecurr);
1910 #               }
1911
1912 # * goes on variable not on type
1913                 # (char*[ const])
1914                 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1915                         my ($from, $to) = ($1, $1);
1916
1917                         # Should start with a space.
1918                         $to =~ s/^(\S)/ $1/;
1919                         # Should not end with a space.
1920                         $to =~ s/\s+$//;
1921                         # '*'s should not have spaces between.
1922                         while ($to =~ s/\*\s+\*/\*\*/) {
1923                         }
1924
1925                         #print "from<$from> to<$to>\n";
1926                         if ($from ne $to) {
1927 #                               ERROR("[SPC_M_OPR] \"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
1928                         }
1929                 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1930                         my ($from, $to, $ident) = ($1, $1, $2);
1931
1932                         # Should start with a space.
1933                         $to =~ s/^(\S)/ $1/;
1934                         # Should not end with a space.
1935                         $to =~ s/\s+$//;
1936                         # '*'s should not have spaces between.
1937                         while ($to =~ s/\*\s+\*/\*\*/) {
1938                         }
1939                         # Modifiers should have spaces.
1940                         $to =~ s/(\b$Modifier$)/$1 /;
1941
1942                         #print "from<$from> to<$to> ident<$ident>\n";
1943                         if ($from ne $to && $ident !~ /^$Modifier$/) {
1944 #                               ERROR("[SPC_M_OPR] \"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
1945                         }
1946                 }
1947
1948 # # no BUG() or BUG_ON()
1949 #               if ($line =~ /\b(BUG|BUG_ON)\b/) {
1950 #                       print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1951 #                       print "$herecurr";
1952 #                       $clean = 0;
1953 #               }
1954
1955 #               if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1956 #                       WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1957 #               }
1958
1959 # printk should use KERN_* levels.  Note that follow on printk's on the
1960 # same line do not need a level, so we use the current block context
1961 # to try and find and validate the current printk.  In summary the current
1962 # printk includes all preceeding printk's which have no newline on the end.
1963 # we assume the first bad printk is the one to report.
1964 #               if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1965 #                       my $ok = 0;
1966 #                       for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1967 #                               #print "CHECK<$lines[$ln - 1]\n";
1968 #                               # we have a preceeding printk if it ends
1969 #                               # with "\n" ignore it, else it is to blame
1970 #                               if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1971 #                                       if ($rawlines[$ln - 1] !~ m{\\n"}) {
1972 #                                               $ok = 1;
1973 #                                       }
1974 #                                       last;
1975 #                               }
1976 #                       }
1977 #                       if ($ok == 0) {
1978 #                               WARN("printk() should include KERN_ facility level\n" . $herecurr);
1979 #                       }
1980 #               }
1981
1982 # function brace can't be on same line, except for #defines of do while,
1983 # or if closed on same line
1984                 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1985                     !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1986                         ERROR("[BRC_M_FTN] open brace '{' following function declarations go on the next line\n" . $herecurr);
1987                 }
1988
1989 # open braces for enum, union and struct go on the same line.
1990                 if ($line =~ /^.\s*{/ &&
1991                     $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1992                         ERROR("[BRC_M_EUS] open brace '{' following $1 go on the same line\n" . $hereprev);
1993                 }
1994
1995 # missing space after union, struct or enum definition
1996                 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1997                     ERROR("[SPC_M_KWD] missing space after $1 definition\n" . $herecurr);
1998                 }
1999
2000 # check for spacing round square brackets; allowed:
2001 #  1. with a type on the left -- int [] a;
2002 #  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2003 #  3. inside a curly brace -- = { [0...10] = 5 }
2004                 while ($line =~ /(.*?\s)\[/g) {
2005                         my ($where, $prefix) = ($-[1], $1);
2006                         if ($prefix !~ /$Type\s+$/ &&
2007                             ($where != 0 || $prefix !~ /^.\s+$/) &&
2008                             $prefix !~ /{\s+$/) {
2009                                 ERROR("[SPC_M_SEP] space prohibited before open square bracket '['\n" . $herecurr);
2010                         }
2011                 }
2012
2013 # check for spaces between functions and their parentheses.
2014                 while ($line =~ /($Ident)\s+\(/g) {
2015                         my $name = $1;
2016                         my $ctx_before = substr($line, 0, $-[1]);
2017                         my $ctx = "$ctx_before$name";
2018
2019                         # Ignore those directives where spaces _are_ permitted.
2020                         if ($name =~ /^(?:
2021                                 if|for|while|switch|return|case|
2022                                 volatile|__volatile__|
2023                                 __attribute__|format|__extension__|
2024                                 asm|__asm__)$/x)
2025                         {
2026
2027                         # cpp #define statements have non-optional spaces, ie
2028                         # if there is a space between the name and the open
2029                         # parenthesis it is simply not a parameter group.
2030                         } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2031
2032                         # cpp #elif statement condition may start with a (
2033                         } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2034
2035                         # If this whole things ends with a type its most
2036                         # likely a typedef for a function.
2037                         } elsif ($ctx =~ /$Type$/) {
2038
2039                         } else {
2040                                 ERROR("[SPC_M_SEP] space prohibited between function name and open parenthesis '('\n" . $herecurr);
2041                         }
2042                 }
2043 # Check operator spacing.
2044                 if (!($line=~/\#\s*include/)) {
2045                         my $ops = qr{
2046                                 <<=|>>=|<=|>=|==|!=|
2047                                 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2048                                 =>|->|<<|>>|<|>|=|!|~|
2049                                 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2050                                 \?|:
2051                         }x;
2052                         my @elements = split(/($ops|;)/, $opline);
2053                         my $off = 0;
2054
2055                         my $blank = copy_spacing($opline);
2056
2057                         for (my $n = 0; $n < $#elements; $n += 2) {
2058                                 $off += length($elements[$n]);
2059
2060                                 # Pick up the preceeding and succeeding characters.
2061                                 my $ca = substr($opline, 0, $off);
2062                                 my $cc = '';
2063                                 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2064                                         $cc = substr($opline, $off + length($elements[$n + 1]));
2065                                 }
2066                                 my $cb = "$ca$;$cc";
2067
2068                                 my $a = '';
2069                                 $a = 'V' if ($elements[$n] ne '');
2070                                 $a = 'W' if ($elements[$n] =~ /\s$/);
2071                                 $a = 'C' if ($elements[$n] =~ /$;$/);
2072                                 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2073                                 $a = 'O' if ($elements[$n] eq '');
2074                                 $a = 'E' if ($ca =~ /^\s*$/);
2075
2076                                 my $op = $elements[$n + 1];
2077
2078                                 my $c = '';
2079                                 if (defined $elements[$n + 2]) {
2080                                         $c = 'V' if ($elements[$n + 2] ne '');
2081                                         $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2082                                         $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2083                                         $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2084                                         $c = 'O' if ($elements[$n + 2] eq '');
2085                                         $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2086                                 } else {
2087                                         $c = 'E';
2088                                 }
2089
2090                                 my $ctx = "${a}x${c}";
2091
2092                                 my $at = "(ctx:$ctx)";
2093
2094                                 my $ptr = substr($blank, 0, $off) . "^";
2095                                 my $hereptr = "$hereline$ptr\n";
2096
2097                                 # Pull out the value of this operator.
2098                                 my $op_type = substr($curr_values, $off + 1, 1);
2099
2100                                 # Get the full operator variant.
2101                                 my $opv = $op . substr($curr_vars, $off, 1);
2102
2103                                 # Ignore operators passed as parameters.
2104                                 if ($op_type ne 'V' &&
2105                                     $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2106
2107 #                               # Ignore comments
2108 #                               } elsif ($op =~ /^$;+$/) {
2109
2110                                 # ; should have either the end of line or a space or \ after it
2111                                 } elsif ($op eq ';') {
2112                                         if ($ctx !~ /.x[WEBC]/ &&
2113                                             $cc !~ /^\\/ && $cc !~ /^;/) {
2114                                                 ERROR("[SPC_M_OPR] space required after that '$op' $at\n" . $hereptr);
2115                                         }
2116
2117                                 # // is a comment
2118                                 } elsif ($op eq '//') {
2119
2120                                 # No spaces for:
2121                                 #   ->
2122                                 #   :   when part of a bitfield
2123                                 } elsif ($op eq '->' || $opv eq ':B') {
2124                                         if ($ctx =~ /Wx.|.xW/) {
2125                                                 ERROR("[SPC_M_OPR] spaces prohibited around that '$op' $at\n" . $hereptr);
2126                                         }
2127
2128                                 # , must have a space on the right.
2129                                 } elsif ($op eq ',') {
2130                                         if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2131                                                 ERROR("[SPC_M_OPR] space required after that '$op' $at\n" . $hereptr);
2132                                         }
2133
2134                                 # '*' as part of a type definition -- reported already.
2135                                 } elsif ($opv eq '*_') {
2136                                         #warn "'*' is part of type\n";
2137
2138                                 # unary operators should have a space before and
2139                                 # none after.  May be left adjacent to another
2140                                 # unary operator, or a cast
2141                                 } elsif ($op eq '!' || $op eq '~' ||
2142                                          $opv eq '*U' || $opv eq '-U' ||
2143                                          $opv eq '&U' || $opv eq '&&U') {
2144                                         if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2145                                                 ERROR("[SPC_M_OPR] space required before that '$op' $at\n" . $hereptr);
2146                                         }
2147                                         if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2148                                                 # A unary '*' may be const
2149
2150                                         } elsif ($ctx =~ /.xW/) {
2151 # added because Bug
2152                                                                 if ($ctx =~ /WxW/) {
2153 #                                                     WARN("[SPC_M_OPR] space prohibited after that '$op' $at\n" . $hereptr);
2154                                               }
2155                                               else{
2156                                                 ERROR("[SPC_M_OPR] space prohibited after that '$op' $at\n" . $hereptr);}
2157                                               }
2158
2159                                 # unary ++ and unary -- are allowed no space on one side.
2160                                 } elsif ($op eq '++' or $op eq '--') {
2161                                         if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2162                                                 ERROR("[SPC_M_OPR] space required one side of that '$op' $at\n" . $hereptr);
2163                                         }
2164                                         if ($ctx =~ /Wx[BE]/ ||
2165                                             ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2166                                                 ERROR("[SPC_M_OPR] space prohibited before that '$op' $at\n" . $hereptr);
2167                                         }
2168                                         if ($ctx =~ /ExW/) {
2169                                                 ERROR("[SPC_M_OPR] space prohibited after that '$op' $at\n" . $hereptr);
2170                                         }
2171
2172
2173                                 # << and >> may either have or not have spaces both sides
2174                                 } elsif ($op eq '<<' or $op eq '>>' or
2175                                          $op eq '&' or $op eq '^' or $op eq '|' or
2176                                          $op eq '+' or $op eq '-' or
2177                                          $op eq '*' or $op eq '/' or
2178                                          $op eq '%')
2179                                 {
2180                                         if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2181                                                 # added because Bug
2182                                                                 if ($ctx =~ /WxV/) {
2183 #                                                     WARN("[SPC_M_OPR] need consistent spacing around '$op' $at\n" .$hereptr);
2184                                               }
2185                                               elsif($ctx =~ /WxO/){
2186 #                                               WARN("[SPC_M_OPR] need consistent spacing around '$op' $at\n" .$hereptr);
2187                                               }
2188                                               elsif($ctx =~ /WxB/){
2189 #                                               WARN("[SPC_M_OPR] need consistent spacing around '$op' $at\n" .$hereptr);
2190                                               }
2191                                               else {
2192                                                                         ERROR("[SPC_M_OPR] need consistent spacing around '$op' $at\n" . $hereptr);
2193                                                 }
2194                                         }
2195
2196                                 # A colon needs no spaces before when it is
2197                                 # terminating a case value or a label.
2198                                 } elsif ($opv eq ':C' || $opv eq ':L') {
2199                                         if ($ctx =~ /Wx./) {
2200                                                 ERROR("[SPC_M_OPR] space prohibited before that '$op' $at\n" . $hereptr);
2201                                         }
2202
2203                                 # All the others need spaces both sides.
2204                                 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2205                                         my $ok = 0;
2206
2207                                         # Ignore email addresses <foo@bar>
2208                                         if (($op eq '<' &&
2209                                              $cc =~ /^\S+\@\S+>/) ||
2210                                             ($op eq '>' &&
2211                                              $ca =~ /<\S+\@\S+$/))
2212                                         {
2213                                                 $ok = 1;
2214                                         }
2215
2216                                         # Ignore ?:
2217                                         if (($opv eq ':O' && $ca =~ /\?$/) ||
2218                                             ($op eq '?' && $cc =~ /^:/)) {
2219                                                 $ok = 1;
2220                                         }
2221
2222                                         if ($ok == 0) {
2223                                                 ERROR("[SPC_M_OPR] spaces required around that '$op' $at\n" . $hereptr);
2224                                         }
2225                                 }
2226                                 $off += length($elements[$n + 1]);
2227                         }
2228                 }
2229
2230 # check for multiple assignments
2231 #               if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2232 #                       CHK("multiple assignments should be avoided\n" . $herecurr);
2233 #               }
2234
2235 ## # check for multiple declarations, allowing for a function declaration
2236 ## # continuation.
2237 ##              if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2238 ##                  $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2239 ##
2240 ##                      # Remove any bracketed sections to ensure we do not
2241 ##                      # falsly report the parameters of functions.
2242 ##                      my $ln = $line;
2243 ##                      while ($ln =~ s/\([^\(\)]*\)//g) {
2244 ##                      }
2245 ##                      if ($ln =~ /,/) {
2246 ##                              WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2247 ##                      }
2248 ##              }
2249
2250 #need space before brace following if, while, etc
2251                 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2252                     $line =~ /do{/) {
2253                         ERROR("[SPC_M_KWD] space required before the open brace '{'\n" . $herecurr);
2254                 }
2255
2256 # closing brace should have a space following it when it has anything
2257 # on the line
2258                 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2259                         ERROR("[SPC_M_SEP] space required after that close brace '}'\n" . $herecurr);
2260                 }
2261
2262 # check spacing on square brackets
2263                 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2264                         ERROR("[SPC_M_SEP] space prohibited after that open square bracket '['\n" . $herecurr);
2265                 }
2266                 if ($line =~ /\s\]/) {
2267                         ERROR("[SPC_M_SEP] space prohibited before that close square bracket ']'\n" . $herecurr);
2268                 }
2269
2270 # check spacing on parentheses
2271                 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2272                     $line !~ /for\s*\(\s+;/) {
2273                         ERROR("[SPC_M_SEP] space prohibited after that open parenthesis '('\n" . $herecurr);
2274                 }
2275                 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2276                     $line !~ /for\s*\(.*;\s+\)/ &&
2277                     $line !~ /:\s+\)/) {
2278                         ERROR("[SPC_M_SEP] space prohibited before that close parenthesis ')'\n" . $herecurr);
2279                 }
2280
2281 #goto labels aren't indented, allow a single space however
2282                 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2283                    !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2284                         WARN("[IDT_R_LBL] labels should not be indented\n" . $herecurr);
2285                 }
2286
2287 # Return is not a function.
2288                 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2289                         my $spacing = $1;
2290                         my $value = $2;
2291
2292                         # Flatten any parentheses
2293                         $value =~ s/\(/ \(/g;
2294                         $value =~ s/\)/\) /g;
2295                         while ($value =~ s/\[[^\{\}]*\]/1/ ||
2296                                $value !~ /(?:$Ident|-?$Constant)\s*
2297                                              $Compare\s*
2298                                              (?:$Ident|-?$Constant)/x &&
2299                                $value =~ s/\([^\(\)]*\)/1/) {
2300                         }
2301 #print "value<$value>\n";
2302                         if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2303 #                               ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2304
2305                         } elsif ($spacing !~ /\s+/) {
2306                                 ERROR("[SPC_M_KWD] space required before the open parenthesis '('\n" . $herecurr);
2307                         }
2308                 }
2309 # Return of what appears to be an errno should normally be -'ve
2310 #               if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2311 #                       my $name = $1;
2312 #                       if ($name ne 'EOF' && $name ne 'ERROR') {
2313 #                               WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2314 #                       }
2315 #               }
2316
2317 # Need a space before open parenthesis after if, while etc
2318                 if ($line=~/\b(if|while|for|switch)\(/) {
2319                         ERROR("[SPC_M_KWD] space required before the open parenthesis '('\n" . $herecurr);
2320                 }
2321
2322 # Check for illegal assignment in if conditional -- and check for trailing
2323 # statements after the conditional.
2324                 if ($line =~ /do\s*(?!{)/) {
2325                         my ($stat_next) = ctx_statement_block($line_nr_next,
2326                                                 $remain_next, $off_next);
2327                         $stat_next =~ s/\n./\n /g;
2328                         ##print "stat<$stat> stat_next<$stat_next>\n";
2329
2330                         if ($stat_next =~ /^\s*while\b/) {
2331                                 # If the statement carries leading newlines,
2332                                 # then count those as offsets.
2333                                 my ($whitespace) =
2334                                         ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2335                                 my $offset =
2336                                         statement_rawlines($whitespace) - 1;
2337
2338                                 $suppress_whiletrailers{$line_nr_next +
2339                                                                 $offset} = 1;
2340                         }
2341                 }
2342                 if (!defined $suppress_whiletrailers{$linenr} &&
2343                     $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2344                         my ($s, $c) = ($stat, $cond);
2345
2346 #                       if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2347 #                               ERROR("do not use assignment in if condition\n" . $herecurr);
2348 #                       }
2349
2350                         # Find out what is on the end of the line after the
2351                         # conditional.
2352 #                       substr($s, 0, length($c), '');
2353 #                       $s =~ s/\n.*//g;
2354 #                       $s =~ s/$;//g;  # Remove any comments
2355 #                       if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2356 #                           $c !~ /}\s*while\s*/)
2357 #                       {
2358 #                               # Find out how long the conditional actually is.
2359 #                               my @newlines = ($c =~ /\n/gs);
2360 #                               my $cond_lines = 1 + $#newlines;
2361 #                               my $stat_real = '';
2362 #
2363 #                               $stat_real = raw_line($linenr, $cond_lines)
2364 #                                                       . "\n" if ($cond_lines);
2365 #                               if (defined($stat_real) && $cond_lines > 1) {
2366 #                                       $stat_real = "[...]\n$stat_real";
2367 #                               }
2368 #
2369 #                               ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2370 #                       }
2371                 }
2372
2373 # Check for bitwise tests written as boolean
2374 #               if ($line =~ /
2375 #                       (?:
2376 #                               (?:\[|\(|\&\&|\|\|)
2377 #                               \s*0[xX][0-9]+\s*
2378 #                               (?:\&\&|\|\|)
2379 #                       |
2380 #                               (?:\&\&|\|\|)
2381 #                               \s*0[xX][0-9]+\s*
2382 #                               (?:\&\&|\|\||\)|\])
2383 #                       )/x)
2384 #               {
2385 #                       WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2386 #               }
2387
2388 # if and else should not have general statements after it
2389 #               if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2390 #                       my $s = $1;
2391 #                       $s =~ s/$;//g;  # Remove any comments
2392 #                       if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2393 #                               ERROR("trailing statements should be on next line\n" . $herecurr);
2394 #                       }
2395 #               }
2396 # if should not continue a brace
2397 #               if ($line =~ /}\s*if\b/) {
2398 #                       ERROR("trailing statements should be on next line\n" .
2399 #                               $herecurr);
2400 #               }
2401 # case and default should not have general statements after them
2402 #               if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2403 #                   $line !~ /\G(?:
2404 #                       (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2405 #                       \s*return\s+
2406 #                   )/xg)
2407 #               {
2408 #                       ERROR("trailing statements should be on next line\n" . $herecurr);
2409 #               }
2410
2411                 # Check for }<nl>else {, these must be at the same
2412                 # indent level to be relevant to each other.
2413                 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2414                                                 $previndent == $indent) {
2415                         ERROR("[BRC_M_SMT] else should follow close brace '}'\n" . $hereprev);
2416                 }
2417
2418                 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2419                                                 $previndent == $indent) {
2420                         my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2421
2422                         # Find out what is on the end of the line after the
2423                         # conditional.
2424                         substr($s, 0, length($c), '');
2425                         $s =~ s/\n.*//g;
2426
2427                         if ($s =~ /^\s*;/) {
2428                                 ERROR("[BRC_M_SMT] while should follow close brace '}'\n" . $hereprev);
2429                         }
2430                 }
2431
2432 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2433 #               if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2434 #                   print "No studly caps, use _\n";
2435 #                   print "$herecurr";
2436 #                   $clean = 0;
2437 #               }
2438
2439 #no spaces allowed after \ in define
2440 #               if ($line=~/\#\s*define.*\\\s$/) {
2441 #                       WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2442 #               }
2443
2444 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2445 #               if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2446 #                       my $file = "$1.h";
2447 #                       my $checkfile = "include/linux/$file";
2448 #                       if (-f "$root/$checkfile" &&
2449 #                           $realfile ne $checkfile &&
2450 #                           $1 !~ /$allowed_asm_includes/)
2451 #                       {
2452 #                               if ($realfile =~ m{^arch/}) {
2453 #                                       CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2454 #                               } else {
2455 #                                       WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2456 #                               }
2457 #                       }
2458 #               }
2459
2460 # multi-statement macros should be enclosed in a do while loop, grab the
2461 # first statement and ensure its the whole macro if its not enclosed
2462 # in a known good container
2463                 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2464                     $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2465                         my $ln = $linenr;
2466                         my $cnt = $realcnt;
2467                         my ($off, $dstat, $dcond, $rest);
2468                         my $ctx = '';
2469
2470                         my $args = defined($1);
2471
2472                         # Find the end of the macro and limit our statement
2473                         # search to that.
2474                         while ($cnt > 0 && defined $lines[$ln - 1] &&
2475                                 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2476                         {
2477                                 $ctx .= $rawlines[$ln - 1] . "\n";
2478                                 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2479                                 $ln++;
2480                         }
2481                         $ctx .= $rawlines[$ln - 1];
2482
2483                         ($dstat, $dcond, $ln, $cnt, $off) =
2484                                 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2485                         #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2486                         #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2487
2488                         # Extract the remainder of the define (if any) and
2489                         # rip off surrounding spaces, and trailing \'s.
2490                         $rest = '';
2491                         while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2492                                 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2493                                 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2494                                         $rest .= substr($lines[$ln - 1], $off) . "\n";
2495                                         $cnt--;
2496                                 }
2497                                 $ln++;
2498                                 $off = 0;
2499                         }
2500                         $rest =~ s/\\\n.//g;
2501                         $rest =~ s/^\s*//s;
2502                         $rest =~ s/\s*$//s;
2503
2504                         # Clean up the original statement.
2505                         if ($args) {
2506                                 substr($dstat, 0, length($dcond), '');
2507                         } else {
2508                                 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2509                         }
2510                         $dstat =~ s/$;//g;
2511                         $dstat =~ s/\\\n.//g;
2512                         $dstat =~ s/^\s*//s;
2513                         $dstat =~ s/\s*$//s;
2514
2515                         # Flatten any parentheses and braces
2516                         while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2517                                $dstat =~ s/\{[^\{\}]*\}/1/ ||
2518                                $dstat =~ s/\[[^\{\}]*\]/1/)
2519                         {
2520                         }
2521
2522                         my $exceptions = qr{
2523                                 $Declare|
2524                                 module_param_named|
2525                                 MODULE_PARAM_DESC|
2526                                 DECLARE_PER_CPU|
2527                                 DEFINE_PER_CPU|
2528                                 __typeof__\(|
2529                                 union|
2530                                 struct|
2531                                 \.$Ident\s*=\s*|
2532                                 ^\"|\"$
2533                         }x;
2534                         #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2535                         if ($rest ne '' && $rest ne ',') {
2536                                 if ($rest !~ /while\s*\(/ &&
2537                                     $dstat !~ /$exceptions/)
2538                                 {
2539 #                                       ERROR("[DEC_MDW_R] Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2540                                 }
2541
2542                         } elsif ($ctx !~ /;/) {
2543                                 if ($dstat ne '' &&
2544                                     $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2545                                     $dstat !~ /$exceptions/ &&
2546                                     $dstat !~ /^\.$Ident\s*=/ &&
2547                                     $dstat =~ /$Operators/)
2548                                 {
2549 #                                       ERROR("[DEC_MPA_R] Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2550                                 }
2551                         }
2552                 }
2553
2554 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2555 # all assignments may have only one of the following with an assignment:
2556 #       .
2557 #       ALIGN(...)
2558 #       VMLINUX_SYMBOL(...)
2559 #               if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2560 #                       WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2561 #               }
2562
2563 # check for redundant bracing round if etc
2564                 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2565                         my ($level, $endln, @chunks) =
2566                                 ctx_statement_full($linenr, $realcnt, 1);
2567                         #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2568                         #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2569                         if ($#chunks > 0 && $level == 0) {
2570                                 my $allowed = 0;
2571                                 my $seen = 0;
2572                                 my $herectx = $here . "\n";
2573                                 my $ln = $linenr - 1;
2574                                 for my $chunk (@chunks) {
2575                                         my ($cond, $block) = @{$chunk};
2576
2577                                         # If the condition carries leading newlines, then count those as offsets.
2578                                         my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2579                                         my $offset = statement_rawlines($whitespace) - 1;
2580
2581                                         #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2582
2583                                         # We have looked at and allowed this specific line.
2584                                         $suppress_ifbraces{$ln + $offset} = 1;
2585
2586                                         $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2587                                         $ln += statement_rawlines($block) - 1;
2588
2589                                         substr($block, 0, length($cond), '');
2590
2591                                         $seen++ if ($block =~ /^\s*{/);
2592
2593                                         #print "cond<$cond> block<$block> allowed<$allowed>\n";
2594                                         if (statement_lines($cond) > 1) {
2595                                                 #print "APW: ALLOWED: cond<$cond>\n";
2596                                                 $allowed = 1;
2597                                         }
2598                                         if ($block =~/\b(?:if|for|while)\b/) {
2599                                                 #print "APW: ALLOWED: block<$block>\n";
2600                                                 $allowed = 1;
2601                                         }
2602                                         if (statement_block_size($block) > 1) {
2603                                                 #print "APW: ALLOWED: lines block<$block>\n";
2604                                                 $allowed = 1;
2605                                         }
2606                                 }
2607                                 if ($seen && !$allowed) {
2608                                         WARN("[BRC_R_SST] braces {} are not necessary for any arm of this statement\n" . $herectx);
2609                                 }
2610                         }
2611                 }
2612                 if (!defined $suppress_ifbraces{$linenr - 1} &&
2613                                         $line =~ /\b(if|while|for|else)\b/) {
2614                         my $allowed = 0;
2615
2616                         # Check the pre-context.
2617                         if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2618                                 #print "APW: ALLOWED: pre<$1>\n";
2619                                 $allowed = 1;
2620                         }
2621
2622                         my ($level, $endln, @chunks) =
2623                                 ctx_statement_full($linenr, $realcnt, $-[0]);
2624
2625                         # Check the condition.
2626                         my ($cond, $block) = @{$chunks[0]};
2627                         #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2628                         if (defined $cond) {
2629                                 substr($block, 0, length($cond), '');
2630                         }
2631                         if (statement_lines($cond) > 1) {
2632                                 #print "APW: ALLOWED: cond<$cond>\n";
2633                                 $allowed = 1;
2634                         }
2635                         if ($block =~/\b(?:if|for|while)\b/) {
2636                                 #print "APW: ALLOWED: block<$block>\n";
2637                                 $allowed = 1;
2638                         }
2639                         if (statement_block_size($block) > 1) {
2640                                 #print "APW: ALLOWED: lines block<$block>\n";
2641                                 $allowed = 1;
2642                         }
2643                         # Check the post-context.
2644                         if (defined $chunks[1]) {
2645                                 my ($cond, $block) = @{$chunks[1]};
2646                                 if (defined $cond) {
2647                                         substr($block, 0, length($cond), '');
2648                                 }
2649                                 if ($block =~ /^\s*\{/) {
2650                                         #print "APW: ALLOWED: chunk-1 block<$block>\n";
2651                                         $allowed = 1;
2652                                 }
2653                         }
2654                         if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2655                                 my $herectx = $here . "\n";;
2656                                 my $cnt = statement_rawlines($block);
2657
2658                                 for (my $n = 0; $n < $cnt; $n++) {
2659                                         $herectx .= raw_line($linenr, $n) . "\n";;
2660                                 }
2661
2662                                 WARN("[BRC_R_SST] braces {} are not necessary for single statement blocks\n" . $herectx);
2663                         }
2664                 }
2665
2666 # don't include deprecated include files (uses RAW line)
2667 #               for my $inc (@dep_includes) {
2668 #                       if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2669 #                               ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2670 #                       }
2671 #               }
2672
2673 # don't use deprecated functions
2674 #               for my $func (@dep_functions) {
2675 #                       if ($line =~ /\b$func\b/) {
2676 #                               ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2677 #                       }
2678 #               }
2679
2680 # no volatiles please
2681 #               my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2682 #               if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2683 #                       WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2684 #               }
2685
2686 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2687 #               if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2688 #                       ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2689 #               }
2690
2691 # warn about #if 0
2692 #               if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2693 #                       CHK("if this code is redundant consider removing it\n" .
2694 #                               $herecurr);
2695 #               }
2696
2697 # check for needless kfree() checks
2698 #               if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2699 #                       my $expr = $1;
2700 #                       if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2701 #                               WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2702 #                       }
2703 #               }
2704 # check for needless usb_free_urb() checks
2705 #               if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2706 #                       my $expr = $1;
2707 #                       if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2708 #                               WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2709 #                       }
2710 #               }
2711
2712 # prefer usleep_range over udelay
2713 #               if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2714 #                       # ignore udelay's < 10, however
2715 #                       if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2716 #                               CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2717 #                       }
2718 #               }
2719
2720 # warn about unexpectedly long msleep's
2721 #               if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2722 #                       if ($1 < 20) {
2723 #                               WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2724 #                       }
2725 #               }
2726
2727 # warn about #ifdefs in C files
2728 #               if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2729 #                       print "#ifdef in C files should be avoided\n";
2730 #                       print "$herecurr";
2731 #                       $clean = 0;
2732 #               }
2733
2734 # warn about spacing in #ifdefs
2735 #               if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2736 #                       ERROR("exactly one space required after that #$1\n" . $herecurr);
2737 #               }
2738
2739 # check for spinlock_t definitions without a comment.
2740 #               if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2741 #                   $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2742 #                       my $which = $1;
2743 #                       if (!ctx_has_comment($first_line, $linenr)) {
2744 #                               CHK("$1 definition without comment\n" . $herecurr);
2745 #                       }
2746 #               }
2747 # check for memory barriers without a comment.
2748 #               if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2749 #                       if (!ctx_has_comment($first_line, $linenr)) {
2750 #                               CHK("memory barrier without comment\n" . $herecurr);
2751 #                       }
2752 #               }
2753 # check of hardware specific defines
2754 #               if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2755 #                       CHK("architecture specific defines should be avoided\n" .  $herecurr);
2756 #               }
2757
2758 # Check that the storage class is at the beginning of a declaration
2759                 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2760                         WARN("[DEC_R_INL] storage class should be at the beginning of the declaration\n" . $herecurr)
2761                 }
2762
2763 # check the location of the inline attribute, that it is between
2764 # storage class and type.
2765                 if ($line =~ /\b$Type\s+$Inline\b/ ||
2766                     $line =~ /\b$Inline\s+$Storage\b/) {
2767                         WARN("[DEC_R_INL] inline keyword should sit between storage class and type\n" . $herecurr);
2768                 }
2769
2770 # Check for __inline__ and __inline, prefer inline
2771 #               if ($line =~ /\b(__inline__|__inline)\b/) {
2772 #                       WARN("plain inline is preferred over $1\n" . $herecurr);
2773 #               }
2774
2775 # check for sizeof(&)
2776 #               if ($line =~ /\bsizeof\s*\(\s*\&/) {
2777 #                       WARN("sizeof(& should be avoided\n" . $herecurr);
2778 #               }
2779
2780 # check for new externs in .c files.
2781 #               if ($realfile =~ /\.c$/ && defined $stat &&
2782 #                   $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2783 #               {
2784 #                       my $function_name = $1;
2785 #                       my $paren_space = $2;
2786 #
2787 #                       my $s = $stat;
2788 #                       if (defined $cond) {
2789 #                               substr($s, 0, length($cond), '');
2790 #                       }
2791 #                       if ($s =~ /^\s*;/ &&
2792 #                           $function_name ne 'uninitialized_var')
2793 #                       {
2794 #                               WARN("externs should be avoided in .c files\n" .  $herecurr);
2795 #                       }
2796 #
2797 #                       if ($paren_space =~ /\n/) {
2798 #                               WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2799 #                       }
2800 #
2801 #               } elsif ($realfile =~ /\.c$/ && defined $stat &&
2802 #                   $stat =~ /^.\s*extern\s+/)
2803 #               {
2804 #                       WARN("externs should be avoided in .c files\n" .  $herecurr);
2805 #               }
2806
2807 # checks for new __setup's
2808 #               if ($rawline =~ /\b__setup\("([^"]*)"/) {
2809 #                       my $name = $1;
2810 #
2811 #                       if (!grep(/$name/, @setup_docs)) {
2812 #                               CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2813 #                       }
2814 #               }
2815
2816 # check for pointless casting of kmalloc return
2817 #               if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2818 #                       WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2819 #               }
2820
2821 # check for gcc specific __FUNCTION__
2822 #               if ($line =~ /__FUNCTION__/) {
2823 #                       WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
2824 #               }
2825
2826 # check for semaphores initialized locked
2827 #               if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
2828 #                       WARN("consider using a completion\n" . $herecurr);
2829 #
2830 #               }
2831 # recommend strict_strto* over simple_strto*
2832 #               if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2833 #                       WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2834 #               }
2835 # check for __initcall(), use device_initcall() explicitly please
2836 #               if ($line =~ /^.\s*__initcall\s*\(/) {
2837 #                       WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2838 #               }
2839 # check for various ops structs, ensure they are const.
2840 #               my $struct_ops = qr{acpi_dock_ops|
2841 #                               address_space_operations|
2842 #                               backlight_ops|
2843 #                               block_device_operations|
2844 #                               dentry_operations|
2845 #                               dev_pm_ops|
2846 #                               dma_map_ops|
2847 #                               extent_io_ops|
2848 #                               file_lock_operations|
2849 #                               file_operations|
2850 #                               hv_ops|
2851 #                               ide_dma_ops|
2852 #                               intel_dvo_dev_ops|
2853 #                               item_operations|
2854 #                               iwl_ops|
2855 #                               kgdb_arch|
2856 #                               kgdb_io|
2857 #                               kset_uevent_ops|
2858 #                               lock_manager_operations|
2859 #                               microcode_ops|
2860 #                               mtrr_ops|
2861 #                               neigh_ops|
2862 #                               nlmsvc_binding|
2863 #                               pci_raw_ops|
2864 #                               pipe_buf_operations|
2865 #                               platform_hibernation_ops|
2866 #                               platform_suspend_ops|
2867 #                               proto_ops|
2868 #                               rpc_pipe_ops|
2869 #                               seq_operations|
2870 #                               snd_ac97_build_ops|
2871 #                               soc_pcmcia_socket_ops|
2872 #                               stacktrace_ops|
2873 #                               sysfs_ops|
2874 #                               tty_operations|
2875 #                               usb_mon_operations|
2876 #                               wd_ops}x;
2877 #               if ($line !~ /\bconst\b/ &&
2878 #                   $line =~ /\bstruct\s+($struct_ops)\b/) {
2879 #                       WARN("struct $1 should normally be const\n" .
2880 #                               $herecurr);
2881 #               }
2882
2883 # use of NR_CPUS is usually wrong
2884 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2885 #               if ($line =~ /\bNR_CPUS\b/ &&
2886 #                   $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2887 #                   $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2888 #                   $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2889 #                   $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2890 #                   $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2891 #               {
2892 #                       WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2893 #               }
2894
2895 # check for %L{u,d,i} in strings
2896 #               my $string;
2897 #               while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2898 #                       $string = substr($rawline, $-[1], $+[1] - $-[1]);
2899 #                       $string =~ s/%%/__/g;
2900 #                       if ($string =~ /(?<!%)%L[udi]/) {
2901 #                               WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2902 #                               last;
2903 #                       }
2904 #               }
2905
2906 # whine mightly about in_atomic
2907 #               if ($line =~ /\bin_atomic\s*\(/) {
2908 #                       if ($realfile =~ m@^drivers/@) {
2909 #                               ERROR("do not use in_atomic in drivers\n" . $herecurr);
2910 #                       } elsif ($realfile !~ m@^kernel/@) {
2911 #                               WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2912 #                       }
2913 #               }
2914
2915 # check for lockdep_set_novalidate_class
2916 #               if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2917 #                   $line =~ /__lockdep_no_validate__\s*\)/ ) {
2918 #                       if ($realfile !~ m@^kernel/lockdep@ &&
2919 #                           $realfile !~ m@^include/linux/lockdep@ &&
2920 #                           $realfile !~ m@^drivers/base/core@) {
2921 #                               ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2922 #                       }
2923 #               }
2924         }
2925
2926         # If we have no input at all, then there is nothing to report on
2927         # so just keep quiet.
2928         if ($#rawlines == -1) {
2929                 exit(0);
2930         }
2931
2932         # In mailback mode only produce a report in the negative, for
2933         # things that appear to be patches.
2934         if ($mailback && ($clean == 1 || !$is_patch)) {
2935                 exit(0);
2936         }
2937
2938         # This is not a patch, and we are are in 'no-patch' mode so
2939         # just keep quiet.
2940         if (!$chk_patch && !$is_patch) {
2941                 exit(0);
2942         }
2943
2944 #       if (!$is_patch) {
2945 #               ERROR("Does not appear to be a unified-diff format patch\n");
2946 #       }
2947 #       if ($is_patch && $chk_signoff && $signoff == 0) {
2948 #               ERROR("Missing Signed-off-by: line(s)\n");
2949 #       }
2950
2951         print report_dump();
2952         if ($summary && !($clean == 1 && $quiet == 1)) {
2953                 print "$filename " if ($summary_file);
2954                 print "total: $cnt_error errors, $cnt_warn warnings, " .
2955                         (($check)? "$cnt_chk checks, " : "") .
2956                         "$cnt_lines lines checked\n";
2957                 print "\n" if ($quiet == 0);
2958         }
2959         else {
2960                 print "$filename " if ($summary_file);
2961                 print "total: $cnt_error errors, $cnt_warn warnings, " .
2962                         (($check)? "$cnt_chk checks, " : "") .
2963                         "$cnt_lines lines checked\n";
2964                 print "\n" if ($quiet == 0);
2965         }
2966
2967         if ($quiet == 0) {
2968                 # If there were whitespace errors which cleanpatch can fix
2969                 # then suggest that.
2970                 if ($rpt_cleaners) {
2971                         print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2972                         print "      scripts/cleanfile\n\n";
2973                 }
2974         }
2975
2976         if ($clean == 1 && $quiet == 0) {
2977                 print "$vname has no obvious style problems and is ready for submission.\n"
2978         }
2979         if ($clean == 0 && $quiet == 0) {
2980                 print "$vname has style problems, please review.  If any of these errors\n";
2981                 print "are false positives report them to the maintainer, see\n";
2982                 print "CHECKPATCH in MAINTAINERS.\n";
2983         }
2984
2985         return $clean;
2986 }