Whamcloud - gitweb
LU-6245 libcfs: cleanup up libcfs hash code for upstream
[fs/lustre-release.git] / contrib / scripts / checkpatch.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 # Based on linux/scripts/checkpatch.pl from 2.6.38 but uses Lustre
9 # specific deprecated symbol/functions/include lists rather than
10 # Documentation/feature-removal-schedule.txt.
11
12 use strict;
13
14 my $P = $0;
15 $P =~ s@.*/@@g;
16
17 my $V = '0.32';
18
19 use Getopt::Long qw(:config no_auto_abbrev);
20
21 my $quiet = 0;
22 my $tree = 0;
23 my $chk_signoff = 0;
24 my $chk_patch = 1;
25 my $tst_only;
26 my $emacs = 0;
27 my $terse = 0;
28 my $file = 0;
29 my $check = 0;
30 my $summary = 1;
31 my $mailback = 0;
32 my $summary_file = 0;
33 my $show_types = 0;
34 my $root;
35 my %debug;
36 my %ignore_type = ();
37 my @ignore = ();
38 my $help = 0;
39 my $configuration_file = ".checkpatch.conf";
40
41 sub help {
42         my ($exitcode) = @_;
43
44         print << "EOM";
45 Usage: $P [OPTION]... [FILE]...
46 Version: $V
47
48 Options:
49   -q, --quiet                quiet
50   --no-tree                  run without a kernel tree
51   --no-signoff               do not check for 'Signed-off-by' line
52   --patch                    treat FILE as patchfile (default)
53   --emacs                    emacs compile window format
54   --terse                    one line per report
55   -f, --file                 treat FILE as regular source file
56   --subjective, --strict     enable more subjective tests
57   --ignore TYPE(,TYPE2...)   ignore various comma separated message types
58   --show-types               show the message "types" in the output
59   --root=PATH                PATH to the kernel tree root
60   --no-summary               suppress the per-file summary
61   --mailback                 only produce a report in case of warnings/errors
62   --summary-file             include the filename in summary
63   --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
64                              'values', 'possible', 'type', and 'attr' (default
65                              is all off)
66   --test-only=WORD           report only warnings/errors containing WORD
67                              literally
68   -h, --help, --version      display this help and exit
69
70 When FILE is - read standard input.
71 EOM
72
73         exit($exitcode);
74 }
75
76 my $conf = which_conf($configuration_file);
77 if (-f $conf) {
78         my @conf_args;
79         open(my $conffile, '<', "$conf")
80             or warn "$P: Can't find a readable $configuration_file file $!\n";
81
82         while (<$conffile>) {
83                 my $line = $_;
84
85                 $line =~ s/\s*\n?$//g;
86                 $line =~ s/^\s*//g;
87                 $line =~ s/\s+/ /g;
88
89                 next if ($line =~ m/^\s*#/);
90                 next if ($line =~ m/^\s*$/);
91
92                 my @words = split(" ", $line);
93                 foreach my $word (@words) {
94                         last if ($word =~ m/^#/);
95                         push (@conf_args, $word);
96                 }
97         }
98         close($conffile);
99         unshift(@ARGV, @conf_args) if @conf_args;
100 }
101
102 GetOptions(
103         'q|quiet+'      => \$quiet,
104         'tree!'         => \$tree,
105         'signoff!'      => \$chk_signoff,
106         'patch!'        => \$chk_patch,
107         'emacs!'        => \$emacs,
108         'terse!'        => \$terse,
109         'f|file!'       => \$file,
110         'subjective!'   => \$check,
111         'strict!'       => \$check,
112         'ignore=s'      => \@ignore,
113         'show-types!'   => \$show_types,
114         'root=s'        => \$root,
115         'summary!'      => \$summary,
116         'mailback!'     => \$mailback,
117         'summary-file!' => \$summary_file,
118
119         'debug=s'       => \%debug,
120         'test-only=s'   => \$tst_only,
121         'h|help'        => \$help,
122         'version'       => \$help
123 ) or help(1);
124
125 help(0) if ($help);
126
127 my $exit = 0;
128
129 if ($#ARGV < 0) {
130         print "$P: no input files\n";
131         exit(1);
132 }
133
134 @ignore = split(/,/, join(',',@ignore));
135 foreach my $word (@ignore) {
136         $word =~ s/\s*\n?$//g;
137         $word =~ s/^\s*//g;
138         $word =~ s/\s+/ /g;
139         $word =~ tr/[a-z]/[A-Z]/;
140
141         next if ($word =~ m/^\s*#/);
142         next if ($word =~ m/^\s*$/);
143
144         $ignore_type{$word}++;
145 }
146
147 my $dbg_values = 0;
148 my $dbg_possible = 0;
149 my $dbg_type = 0;
150 my $dbg_attr = 0;
151 for my $key (keys %debug) {
152         ## no critic
153         eval "\${dbg_$key} = '$debug{$key}';";
154         die "$@" if ($@);
155 }
156
157 my $rpt_cleaners = 0;
158
159 if ($terse) {
160         $emacs = 1;
161         $quiet++;
162 }
163
164 if ($tree) {
165         if (defined $root) {
166                 if (!top_of_kernel_tree($root)) {
167                         die "$P: $root: --root does not point at a valid tree\n";
168                 }
169         } else {
170                 if (top_of_kernel_tree('.')) {
171                         $root = '.';
172                 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
173                                                 top_of_kernel_tree($1)) {
174                         $root = $1;
175                 }
176         }
177
178         if (!defined $root) {
179                 print "Must be run from the top-level dir. of a kernel tree\n";
180                 exit(2);
181         }
182 }
183
184 my $emitted_corrupt = 0;
185
186 our $Ident      = qr{
187                         [A-Za-z_][A-Za-z\d_]*
188                         (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
189                 }x;
190 our $Storage    = qr{extern|static|asmlinkage};
191 our $Sparse     = qr{
192                         __user|
193                         __kernel|
194                         __force|
195                         __iomem|
196                         __must_check|
197                         __init_refok|
198                         __kprobes|
199                         __ref|
200                         __rcu
201                 }x;
202
203 # Notes to $Attribute:
204 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
205 our $Attribute  = qr{
206                         const|
207                         __percpu|
208                         __nocast|
209                         __safe|
210                         __bitwise__|
211                         __packed__|
212                         __packed2__|
213                         __naked|
214                         __maybe_unused|
215                         __always_unused|
216                         __noreturn|
217                         __used|
218                         __cold|
219                         __noclone|
220                         __deprecated|
221                         __read_mostly|
222                         __kprobes|
223                         __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
224                         ____cacheline_aligned|
225                         ____cacheline_aligned_in_smp|
226                         ____cacheline_internodealigned_in_smp|
227                         __weak
228                   }x;
229 our $Modifier;
230 our $Inline     = qr{inline|__always_inline|noinline};
231 our $Member     = qr{->$Ident|\.$Ident|\[[^]]*\]};
232 our $Lval       = qr{$Ident(?:$Member)*};
233
234 our $Constant   = qr{(?i:(?:[0-9]+|0x[0-9a-f]+)[ul]*)};
235 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
236 our $Compare    = qr{<=|>=|==|!=|<|>};
237 our $Operators  = qr{
238                         <=|>=|==|!=|
239                         =>|->|<<|>>|<|>|!|~|
240                         &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
241                   }x;
242
243 our $NonptrType;
244 our $Type;
245 our $Declare;
246
247 our $NON_ASCII_UTF8     = qr{
248         [\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
249         |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
250         | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
251         |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
252         |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
253         | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
254         |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
255 }x;
256
257 our $UTF8       = qr{
258         [\x09\x0A\x0D\x20-\x7E]              # ASCII
259         | $NON_ASCII_UTF8
260 }x;
261
262 our $typeTypedefs = qr{(?x:
263         (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
264         atomic_t
265 )};
266
267 our $logFunctions = qr{(?x:
268         printk(?:_ratelimited|_once|)|
269         [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
270         WARN(?:_RATELIMIT|_ONCE|)|
271         panic|
272         MODULE_[A-Z_]+
273 )};
274
275 our $signature_tags = qr{(?xi:
276         Signed-off-by:|
277         Acked-by:|
278         Tested-by:|
279         Reviewed-by:|
280         Reported-by:|
281         To:|
282         Cc:
283 )};
284
285 our @typeList = (
286         qr{void},
287         qr{(?:unsigned\s+)?char},
288         qr{(?:unsigned\s+)?short},
289         qr{(?:unsigned\s+)?int},
290         qr{(?:unsigned\s+)?long},
291         qr{(?:unsigned\s+)?long\s+int},
292         qr{(?:unsigned\s+)?long\s+long},
293         qr{(?:unsigned\s+)?long\s+long\s+int},
294         qr{unsigned},
295         qr{float},
296         qr{double},
297         qr{bool},
298         qr{struct\s+$Ident},
299         qr{union\s+$Ident},
300         qr{enum\s+$Ident},
301         qr{${Ident}_t},
302         qr{${Ident}_handler},
303         qr{${Ident}_handler_fn},
304 );
305 our @modifierList = (
306         qr{fastcall},
307 );
308
309 our $allowed_asm_includes = qr{(?x:
310         irq|
311         memory
312 )};
313 # memory.h: ARM has a custom one
314
315 sub build_types {
316         my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
317         my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
318         $Modifier       = qr{(?:$Attribute|$Sparse|$mods)};
319         $NonptrType     = qr{
320                         (?:$Modifier\s+|const\s+)*
321                         (?:
322                                 (?:typeof|__typeof__)\s*\([^\)]*\)|
323                                 (?:$typeTypedefs\b)|
324                                 (?:${all}\b)
325                         )
326                         (?:\s+$Modifier|\s+const)*
327                   }x;
328         $Type   = qr{
329                         $NonptrType
330                         (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
331                         (?:\s+$Inline|\s+$Modifier)*
332                   }x;
333         $Declare        = qr{(?:$Storage\s+)?$Type};
334 }
335 build_types();
336
337 our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
338
339 our $Typecast   = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
340 our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
341 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
342
343 sub deparenthesize {
344         my ($string) = @_;
345         return "" if (!defined($string));
346         $string =~ s@^\s*\(\s*@@g;
347         $string =~ s@\s*\)\s*$@@g;
348         $string =~ s@\s+@ @g;
349         return $string;
350 }
351
352 $chk_signoff = 0 if ($file);
353
354 my %dep_includes = (
355         'asm/types.h',                  'linux/types.h',
356         'asm/uaccess.h',                'linux/uaccess.h',
357 );
358
359 my %dep_functions = (
360         'CFS_ATOMIC_INIT',              'ATOMIC_INIT',
361         'cfs_atomic_add',               'atomic_add',
362         'cfs_atomic_add_return',        'atomic_add_return',
363         'cfs_atomic_add_unless',        'atomic_add_unless',
364         'cfs_atomic_cmpxchg',           'atomic_cmpxchg',
365         'cfs_atomic_dec',               'atomic_dec',
366         'cfs_atomic_dec_and_lock',      'atomic_dec_and_lock',
367         'cfs_atomic_dec_and_test',      'atomic_dec_and_test',
368         'cfs_atomic_dec_return',        'atomic_dec_return',
369         'cfs_atomic_inc',               'atomic_inc',
370         'cfs_atomic_inc_and_test',      'atomic_inc_and_test',
371         'cfs_atomic_inc_not_zero',      'atomic_inc_not_zero',
372         'cfs_atomic_inc_return',        'atomic_inc_return',
373         'cfs_atomic_read',              'atomic_read',
374         'cfs_atomic_set',               'atomic_set',
375         'cfs_atomic_sub',               'atomic_sub',
376         'cfs_atomic_sub_and_test',      'atomic_sub_and_test',
377         'cfs_atomic_sub_return',        'atomic_sub_return',
378         'cfs_atomic_t',                 'atomic_t',
379
380         'CFS_HLIST_HEAD',               'HLIST_HEAD',
381         'CFS_HLIST_HEAD_INIT',          'HLIST_HEAD_INIT',
382         'CFS_INIT_HLIST_HEAD',          'INIT_HLIST_HEAD',
383         'CFS_INIT_HLIST_NODE',          'INIT_HLIST_NODE',
384         'cfs_hlist_add_after',          'hlist_add_after',
385         'cfs_hlist_add_before',         'hlist_add_before',
386         'cfs_hlist_add_head',           'hlist_add_head',
387         'cfs_hlist_del',                'hlist_del',
388         'cfs_hlist_del_init',           'hlist_del_init',
389         'cfs_hlist_empty',              'hlist_empty',
390         'cfs_hlist_entry',              'hlist_entry',
391         'cfs_hlist_for_each',           'hlist_for_each',
392         'cfs_hlist_for_each_safe',      'hlist_for_each_safe',
393         'cfs_hlist_head_t',             'struct hlist_head',
394         'cfs_hlist_node_t',             'struct hlist_node',
395         'cfs_hlist_unhashed',           'hlist_unhashed',
396
397         'cfs_inode_t',                  'struct inode',
398
399         'CFS_INIT_LIST_HEAD',           'INIT_LIST_HEAD',
400         'CFS_LIST_HEAD',                'struct list_head foo = LIST_HEAD_INIT(foo);',
401         'CFS_LIST_HEAD_INIT',           'LIST_HEAD_INIT',
402         'cfs_list_add',                 'list_add',
403         'cfs_list_add_tail',            'list_add_tail',
404         'cfs_list_del',                 'list_del',
405         'cfs_list_del_init',            'list_del_init',
406         'cfs_list_empty',               'list_empty',
407         'cfs_list_empty_careful',       'list_empty_careful',
408         'cfs_list_entry',               'list_entry',
409         'cfs_list_for_each',            'list_for_each',
410         'cfs_list_for_each_entry',      'list_for_each_entry',
411         'cfs_list_for_each_entry_continue',
412                                         'list_for_each_entry_continue',
413         'cfs_list_for_each_entry_reverse',
414                                         'list_for_each_entry_reverse',
415         'cfs_list_for_each_entry_safe',
416                                         'list_for_each_entry_safe',
417         'cfs_list_for_each_entry_safe_from',
418                                         'list_for_each_entry_safe_from',
419         'cfs_list_for_each_entry_safe_reverse',
420                                         'list_for_each_entry_safe_reverse',
421         'cfs_list_for_each_entry_safe_typed',
422                                         'list_for_each_entry_safe_typed',
423         'cfs_list_for_each_entry_typed',
424                                         'list_for_each_entry_typed',
425         'cfs_list_for_each_prev',       'list_for_each_prev',
426         'cfs_list_for_each_safe',       'list_for_each_safe',
427         'cfs_list_move',                'list_move',
428         'cfs_list_move_tail',           'list_move_tail',
429         'cfs_list_splice',              'list_splice',
430         'cfs_list_splice_init',         'list_splice_init',
431         'cfs_list_splice_tail',         'list_splice_tail',
432         'cfs_list_t',                   'struct list_head',
433
434         'CFS_PAGE_MASK',                'PAGE_CACHE_MASK or PAGE_MASK',
435         'CFS_PAGE_SIZE',                'PAGE_CACHE_SIZE or PAGE_SIZE',
436
437         'cfs_proc_dir_entry_t',         'struct proc_dir_entry',
438
439         'cfs_rcu_head_t',               'struct rcu_head',
440
441         'cfs_hash_lock_t',              'union cfs_hash_lock',
442         'cfs_hash_bucket_t',            'struct cfs_hash_bucket',
443         'cfs_hash_bd_t',                'struct cfs_hash_bd',
444         'cfs_hash_t',                   'struct cfs_hash',
445         'cfs_hash_lock_ops_t',          'struct cfs_hash_lock_ops',
446         'cfs_hash_hlist_ops_t',         'struct cfs_hash_hlist_ops',
447         'cfs_hash_ops_t',               'struct cfs_hash_ops',
448         'cfs_hash_head_t',              'struct cfs_hash_head',
449         'cfs_hash_head_dep_t',          'struct cfs_hash_head_dep',
450         'cfs_hash_dhead_t',             'struct cfs_hash_dhead',
451         'cfs_hash_dhead_dep_t',         'struct cfs_hash_dhead_dep',
452         'cfs_hash_lookup_intent_t',     'enum cfs_hash_lookup_intent',
453         'cfs_hash_cond_arg_t',          'struct cfs_hash_cond_arg',
454
455         'LPROCFS',                      'CONFIG_PROC_FS',
456         'alloca',                       'malloc',
457         'mktemp',                       'mkstemp',
458         'sprintf',                      'snprintf',
459         'strcpy',                       'strncpy',
460         'strcat',                       'strncat',
461         'tempnam',                      'mkstemp',
462 );
463
464 my @rawlines = ();
465 my @lines = ();
466 my $vname;
467 for my $filename (@ARGV) {
468         my $FILE;
469         if ($file) {
470                 open($FILE, '-|', "diff -u /dev/null $filename") ||
471                         die "$P: $filename: diff failed - $!\n";
472         } elsif ($filename eq '-') {
473                 open($FILE, '<&STDIN');
474         } else {
475                 open($FILE, '<', "$filename") ||
476                         die "$P: $filename: open failed - $!\n";
477         }
478         if ($filename eq '-') {
479                 $vname = 'Your patch';
480         } else {
481                 $vname = $filename;
482         }
483         while (<$FILE>) {
484                 chomp;
485                 push(@rawlines, $_);
486         }
487         close($FILE);
488         if (!process($filename)) {
489                 $exit = 1;
490         }
491         @rawlines = ();
492         @lines = ();
493 }
494
495 exit($exit);
496
497 sub top_of_kernel_tree {
498         my ($root) = @_;
499
500         my @tree_check = (
501                 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
502                 "README", "Documentation", "arch", "include", "drivers",
503                 "fs", "init", "ipc", "kernel", "lib", "scripts",
504         );
505
506         foreach my $check (@tree_check) {
507                 if (! -e $root . '/' . $check) {
508                         return 0;
509                 }
510         }
511         return 1;
512     }
513
514 sub parse_email {
515         my ($formatted_email) = @_;
516
517         my $name = "";
518         my $address = "";
519         my $comment = "";
520
521         if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
522                 $name = $1;
523                 $address = $2;
524                 $comment = $3 if defined $3;
525         } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
526                 $address = $1;
527                 $comment = $2 if defined $2;
528         } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
529                 $address = $1;
530                 $comment = $2 if defined $2;
531                 $formatted_email =~ s/$address.*$//;
532                 $name = $formatted_email;
533                 $name =~ s/^\s+|\s+$//g;
534                 $name =~ s/^\"|\"$//g;
535                 # If there's a name left after stripping spaces and
536                 # leading quotes, and the address doesn't have both
537                 # leading and trailing angle brackets, the address
538                 # is invalid. ie:
539                 #   "joe smith joe@smith.com" bad
540                 #   "joe smith <joe@smith.com" bad
541                 if ($name ne "" && $address !~ /^<[^>]+>$/) {
542                         $name = "";
543                         $address = "";
544                         $comment = "";
545                 }
546         }
547
548         $name =~ s/^\s+|\s+$//g;
549         $name =~ s/^\"|\"$//g;
550         $address =~ s/^\s+|\s+$//g;
551         $address =~ s/^\<|\>$//g;
552
553         if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
554                 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
555                 $name = "\"$name\"";
556         }
557
558         return ($name, $address, $comment);
559 }
560
561 sub format_email {
562         my ($name, $address) = @_;
563
564         my $formatted_email;
565
566         $name =~ s/^\s+|\s+$//g;
567         $name =~ s/^\"|\"$//g;
568         $address =~ s/^\s+|\s+$//g;
569
570         if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
571                 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
572                 $name = "\"$name\"";
573         }
574
575         if ("$name" eq "") {
576                 $formatted_email = "$address";
577         } else {
578                 $formatted_email = "$name <$address>";
579         }
580
581         return $formatted_email;
582 }
583
584 sub which_conf {
585         my ($conf) = @_;
586
587         foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
588                 if (-e "$path/$conf") {
589                         return "$path/$conf";
590                 }
591         }
592
593         return "";
594 }
595
596 sub expand_tabs {
597         my ($str) = @_;
598
599         my $res = '';
600         my $n = 0;
601         for my $c (split(//, $str)) {
602                 if ($c eq "\t") {
603                         $res .= ' ';
604                         $n++;
605                         for (; ($n % 8) != 0; $n++) {
606                                 $res .= ' ';
607                         }
608                         next;
609                 }
610                 $res .= $c;
611                 $n++;
612         }
613
614         return $res;
615 }
616 sub copy_spacing {
617         (my $res = shift) =~ tr/\t/ /c;
618         return $res;
619 }
620
621 sub line_stats {
622         my ($line) = @_;
623
624         # Drop the diff line leader and expand tabs
625         $line =~ s/^.//;
626         $line = expand_tabs($line);
627
628         # Pick the indent from the front of the line.
629         my ($white) = ($line =~ /^(\s*)/);
630
631         return (length($line), length($white));
632 }
633
634 my $sanitise_quote = '';
635
636 sub sanitise_line_reset {
637         my ($in_comment) = @_;
638
639         if ($in_comment) {
640                 $sanitise_quote = '*/';
641         } else {
642                 $sanitise_quote = '';
643         }
644 }
645 sub sanitise_line {
646         my ($line) = @_;
647
648         my $res = '';
649         my $l = '';
650
651         my $qlen = 0;
652         my $off = 0;
653         my $c;
654
655         # Always copy over the diff marker.
656         $res = substr($line, 0, 1);
657
658         for ($off = 1; $off < length($line); $off++) {
659                 $c = substr($line, $off, 1);
660
661                 # Comments we are wacking completly including the begin
662                 # and end, all to $;.
663                 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
664                         $sanitise_quote = '*/';
665
666                         substr($res, $off, 2, "$;$;");
667                         $off++;
668                         next;
669                 }
670                 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
671                         $sanitise_quote = '';
672                         substr($res, $off, 2, "$;$;");
673                         $off++;
674                         next;
675                 }
676                 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
677                         $sanitise_quote = '//';
678
679                         substr($res, $off, 2, $sanitise_quote);
680                         $off++;
681                         next;
682                 }
683
684                 # A \ in a string means ignore the next character.
685                 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
686                     $c eq "\\") {
687                         substr($res, $off, 2, 'XX');
688                         $off++;
689                         next;
690                 }
691                 # Regular quotes.
692                 if ($c eq "'" || $c eq '"') {
693                         if ($sanitise_quote eq '') {
694                                 $sanitise_quote = $c;
695
696                                 substr($res, $off, 1, $c);
697                                 next;
698                         } elsif ($sanitise_quote eq $c) {
699                                 $sanitise_quote = '';
700                         }
701                 }
702
703                 #print "c<$c> SQ<$sanitise_quote>\n";
704                 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
705                         substr($res, $off, 1, $;);
706                 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
707                         substr($res, $off, 1, $;);
708                 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
709                         substr($res, $off, 1, 'X');
710                 } else {
711                         substr($res, $off, 1, $c);
712                 }
713         }
714
715         if ($sanitise_quote eq '//') {
716                 $sanitise_quote = '';
717         }
718
719         # The pathname on a #include may be surrounded by '<' and '>'.
720         if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
721                 my $clean = 'X' x length($1);
722                 $res =~ s@\<.*\>@<$clean>@;
723
724         # The whole of a #error is a string.
725         } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
726                 my $clean = 'X' x length($1);
727                 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
728         }
729
730         return $res;
731 }
732
733 sub ctx_statement_block {
734         my ($linenr, $remain, $off) = @_;
735         my $line = $linenr - 1;
736         my $blk = '';
737         my $soff = $off;
738         my $coff = $off - 1;
739         my $coff_set = 0;
740
741         my $loff = 0;
742
743         my $type = '';
744         my $level = 0;
745         my @stack = ();
746         my $p;
747         my $c;
748         my $len = 0;
749
750         my $remainder;
751         while (1) {
752                 @stack = (['', 0]) if ($#stack == -1);
753
754                 #warn "CSB: blk<$blk> remain<$remain>\n";
755                 # If we are about to drop off the end, pull in more
756                 # context.
757                 if ($off >= $len) {
758                         for (; $remain > 0; $line++) {
759                                 last if (!defined $lines[$line]);
760                                 next if ($lines[$line] =~ /^-/);
761                                 $remain--;
762                                 $loff = $len;
763                                 $blk .= $lines[$line] . "\n";
764                                 $len = length($blk);
765                                 $line++;
766                                 last;
767                         }
768                         # Bail if there is no further context.
769                         #warn "CSB: blk<$blk> off<$off> len<$len>\n";
770                         if ($off >= $len) {
771                                 last;
772                         }
773                         if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
774                                 $level++;
775                                 $type = '#';
776                         }
777                 }
778                 $p = $c;
779                 $c = substr($blk, $off, 1);
780                 $remainder = substr($blk, $off);
781
782                 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
783
784                 # Handle nested #if/#else.
785                 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
786                         push(@stack, [ $type, $level ]);
787                 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
788                         ($type, $level) = @{$stack[$#stack - 1]};
789                 } elsif ($remainder =~ /^#\s*endif\b/) {
790                         ($type, $level) = @{pop(@stack)};
791                 }
792
793                 # Statement ends at the ';' or a close '}' at the
794                 # outermost level.
795                 if ($level == 0 && $c eq ';') {
796                         last;
797                 }
798
799                 # An else is really a conditional as long as its not else if
800                 if ($level == 0 && $coff_set == 0 &&
801                                 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
802                                 $remainder =~ /^(else)(?:\s|{)/ &&
803                                 $remainder !~ /^else\s+if\b/) {
804                         $coff = $off + length($1) - 1;
805                         $coff_set = 1;
806                         #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
807                         #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
808                 }
809
810                 if (($type eq '' || $type eq '(') && $c eq '(') {
811                         $level++;
812                         $type = '(';
813                 }
814                 if ($type eq '(' && $c eq ')') {
815                         $level--;
816                         $type = ($level != 0)? '(' : '';
817
818                         if ($level == 0 && $coff < $soff) {
819                                 $coff = $off;
820                                 $coff_set = 1;
821                                 #warn "CSB: mark coff<$coff>\n";
822                         }
823                 }
824                 if (($type eq '' || $type eq '{') && $c eq '{') {
825                         $level++;
826                         $type = '{';
827                 }
828                 if ($type eq '{' && $c eq '}') {
829                         $level--;
830                         $type = ($level != 0)? '{' : '';
831
832                         if ($level == 0) {
833                                 if (substr($blk, $off + 1, 1) eq ';') {
834                                         $off++;
835                                 }
836                                 last;
837                         }
838                 }
839                 # Preprocessor commands end at the newline unless escaped.
840                 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
841                         $level--;
842                         $type = '';
843                         $off++;
844                         last;
845                 }
846                 $off++;
847         }
848         # We are truly at the end, so shuffle to the next line.
849         if ($off == $len) {
850                 $loff = $len + 1;
851                 $line++;
852                 $remain--;
853         }
854
855         my $statement = substr($blk, $soff, $off - $soff + 1);
856         my $condition = substr($blk, $soff, $coff - $soff + 1);
857
858         #warn "STATEMENT<$statement>\n";
859         #warn "CONDITION<$condition>\n";
860
861         #print "coff<$coff> soff<$off> loff<$loff>\n";
862
863         return ($statement, $condition,
864                         $line, $remain + 1, $off - $loff + 1, $level);
865 }
866
867 sub statement_lines {
868         my ($stmt) = @_;
869
870         # Strip the diff line prefixes and rip blank lines at start and end.
871         $stmt =~ s/(^|\n)./$1/g;
872         $stmt =~ s/^\s*//;
873         $stmt =~ s/\s*$//;
874
875         my @stmt_lines = ($stmt =~ /\n/g);
876
877         return $#stmt_lines + 2;
878 }
879
880 sub statement_rawlines {
881         my ($stmt) = @_;
882
883         my @stmt_lines = ($stmt =~ /\n/g);
884
885         return $#stmt_lines + 2;
886 }
887
888 sub statement_block_size {
889         my ($stmt) = @_;
890
891         $stmt =~ s/(^|\n)./$1/g;
892         $stmt =~ s/^\s*{//;
893         $stmt =~ s/}\s*$//;
894         $stmt =~ s/^\s*//;
895         $stmt =~ s/\s*$//;
896
897         my @stmt_lines = ($stmt =~ /\n/g);
898         my @stmt_statements = ($stmt =~ /;/g);
899
900         my $stmt_lines = $#stmt_lines + 2;
901         my $stmt_statements = $#stmt_statements + 1;
902
903         if ($stmt_lines > $stmt_statements) {
904                 return $stmt_lines;
905         } else {
906                 return $stmt_statements;
907         }
908 }
909
910 sub ctx_statement_full {
911         my ($linenr, $remain, $off) = @_;
912         my ($statement, $condition, $level);
913
914         my (@chunks);
915
916         # Grab the first conditional/block pair.
917         ($statement, $condition, $linenr, $remain, $off, $level) =
918                                 ctx_statement_block($linenr, $remain, $off);
919         #print "F: c<$condition> s<$statement> remain<$remain>\n";
920         push(@chunks, [ $condition, $statement ]);
921         if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
922                 return ($level, $linenr, @chunks);
923         }
924
925         # Pull in the following conditional/block pairs and see if they
926         # could continue the statement.
927         for (;;) {
928                 ($statement, $condition, $linenr, $remain, $off, $level) =
929                                 ctx_statement_block($linenr, $remain, $off);
930                 #print "C: c<$condition> s<$statement> remain<$remain>\n";
931                 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
932                 #print "C: push\n";
933                 push(@chunks, [ $condition, $statement ]);
934         }
935
936         return ($level, $linenr, @chunks);
937 }
938
939 sub ctx_block_get {
940         my ($linenr, $remain, $outer, $open, $close, $off) = @_;
941         my $line;
942         my $start = $linenr - 1;
943         my $blk = '';
944         my @o;
945         my @c;
946         my @res = ();
947
948         my $level = 0;
949         my @stack = ($level);
950         for ($line = $start; $remain > 0; $line++) {
951                 next if ($rawlines[$line] =~ /^-/);
952                 $remain--;
953
954                 $blk .= $rawlines[$line];
955
956                 # Handle nested #if/#else.
957                 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
958                         push(@stack, $level);
959                 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
960                         $level = $stack[$#stack - 1];
961                 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
962                         $level = pop(@stack);
963                 }
964
965                 foreach my $c (split(//, $lines[$line])) {
966                         ##print "C<$c>L<$level><$open$close>O<$off>\n";
967                         if ($off > 0) {
968                                 $off--;
969                                 next;
970                         }
971
972                         if ($c eq $close && $level > 0) {
973                                 $level--;
974                                 last if ($level == 0);
975                         } elsif ($c eq $open) {
976                                 $level++;
977                         }
978                 }
979
980                 if (!$outer || $level <= 1) {
981                         push(@res, $rawlines[$line]);
982                 }
983
984                 last if ($level == 0);
985         }
986
987         return ($level, @res);
988 }
989 sub ctx_block_outer {
990         my ($linenr, $remain) = @_;
991
992         my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
993         return @r;
994 }
995 sub ctx_block {
996         my ($linenr, $remain) = @_;
997
998         my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
999         return @r;
1000 }
1001 sub ctx_statement {
1002         my ($linenr, $remain, $off) = @_;
1003
1004         my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1005         return @r;
1006 }
1007 sub ctx_block_level {
1008         my ($linenr, $remain) = @_;
1009
1010         return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1011 }
1012 sub ctx_statement_level {
1013         my ($linenr, $remain, $off) = @_;
1014
1015         return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1016 }
1017
1018 sub ctx_locate_comment {
1019         my ($first_line, $end_line) = @_;
1020
1021         # Catch a comment on the end of the line itself.
1022         my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1023         return $current_comment if (defined $current_comment);
1024
1025         # Look through the context and try and figure out if there is a
1026         # comment.
1027         my $in_comment = 0;
1028         $current_comment = '';
1029         for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1030                 my $line = $rawlines[$linenr - 1];
1031                 #warn "           $line\n";
1032                 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1033                         $in_comment = 1;
1034                 }
1035                 if ($line =~ m@/\*@) {
1036                         $in_comment = 1;
1037                 }
1038                 if (!$in_comment && $current_comment ne '') {
1039                         $current_comment = '';
1040                 }
1041                 $current_comment .= $line . "\n" if ($in_comment);
1042                 if ($line =~ m@\*/@) {
1043                         $in_comment = 0;
1044                 }
1045         }
1046
1047         chomp($current_comment);
1048         return($current_comment);
1049 }
1050 sub ctx_has_comment {
1051         my ($first_line, $end_line) = @_;
1052         my $cmt = ctx_locate_comment($first_line, $end_line);
1053
1054         ##print "LINE: $rawlines[$end_line - 1 ]\n";
1055         ##print "CMMT: $cmt\n";
1056
1057         return ($cmt ne '');
1058 }
1059
1060 sub raw_line {
1061         my ($linenr, $cnt) = @_;
1062
1063         my $offset = $linenr - 1;
1064         $cnt++;
1065
1066         my $line;
1067         while ($cnt) {
1068                 $line = $rawlines[$offset++];
1069                 next if (defined($line) && $line =~ /^-/);
1070                 $cnt--;
1071         }
1072
1073         return $line;
1074 }
1075
1076 sub cat_vet {
1077         my ($vet) = @_;
1078         my ($res, $coded);
1079
1080         $res = '';
1081         while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1082                 $res .= $1;
1083                 if ($2 ne '') {
1084                         $coded = sprintf("^%c", unpack('C', $2) + 64);
1085                         $res .= $coded;
1086                 }
1087         }
1088         $res =~ s/$/\$/;
1089
1090         return $res;
1091 }
1092
1093 my $av_preprocessor = 0;
1094 my $av_pending;
1095 my @av_paren_type;
1096 my $av_pend_colon;
1097
1098 sub annotate_reset {
1099         $av_preprocessor = 0;
1100         $av_pending = '_';
1101         @av_paren_type = ('E');
1102         $av_pend_colon = 'O';
1103 }
1104
1105 sub annotate_values {
1106         my ($stream, $type) = @_;
1107
1108         my $res;
1109         my $var = '_' x length($stream);
1110         my $cur = $stream;
1111
1112         print "$stream\n" if ($dbg_values > 1);
1113
1114         while (length($cur)) {
1115                 @av_paren_type = ('E') if ($#av_paren_type < 0);
1116                 print " <" . join('', @av_paren_type) .
1117                                 "> <$type> <$av_pending>" if ($dbg_values > 1);
1118                 if ($cur =~ /^(\s+)/o) {
1119                         print "WS($1)\n" if ($dbg_values > 1);
1120                         if ($1 =~ /\n/ && $av_preprocessor) {
1121                                 $type = pop(@av_paren_type);
1122                                 $av_preprocessor = 0;
1123                         }
1124
1125                 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1126                         print "CAST($1)\n" if ($dbg_values > 1);
1127                         push(@av_paren_type, $type);
1128                         $type = 'c';
1129
1130                 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1131                         print "DECLARE($1)\n" if ($dbg_values > 1);
1132                         $type = 'T';
1133
1134                 } elsif ($cur =~ /^($Modifier)\s*/) {
1135                         print "MODIFIER($1)\n" if ($dbg_values > 1);
1136                         $type = 'T';
1137
1138                 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1139                         print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1140                         $av_preprocessor = 1;
1141                         push(@av_paren_type, $type);
1142                         if ($2 ne '') {
1143                                 $av_pending = 'N';
1144                         }
1145                         $type = 'E';
1146
1147                 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1148                         print "UNDEF($1)\n" if ($dbg_values > 1);
1149                         $av_preprocessor = 1;
1150                         push(@av_paren_type, $type);
1151
1152                 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1153                         print "PRE_START($1)\n" if ($dbg_values > 1);
1154                         $av_preprocessor = 1;
1155
1156                         push(@av_paren_type, $type);
1157                         push(@av_paren_type, $type);
1158                         $type = 'E';
1159
1160                 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1161                         print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1162                         $av_preprocessor = 1;
1163
1164                         push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1165
1166                         $type = 'E';
1167
1168                 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1169                         print "PRE_END($1)\n" if ($dbg_values > 1);
1170
1171                         $av_preprocessor = 1;
1172
1173                         # Assume all arms of the conditional end as this
1174                         # one does, and continue as if the #endif was not here.
1175                         pop(@av_paren_type);
1176                         push(@av_paren_type, $type);
1177                         $type = 'E';
1178
1179                 } elsif ($cur =~ /^(\\\n)/o) {
1180                         print "PRECONT($1)\n" if ($dbg_values > 1);
1181
1182                 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1183                         print "ATTR($1)\n" if ($dbg_values > 1);
1184                         $av_pending = $type;
1185                         $type = 'N';
1186
1187                 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1188                         print "SIZEOF($1)\n" if ($dbg_values > 1);
1189                         if (defined $2) {
1190                                 $av_pending = 'V';
1191                         }
1192                         $type = 'N';
1193
1194                 } elsif ($cur =~ /^(if|while|for)\b/o) {
1195                         print "COND($1)\n" if ($dbg_values > 1);
1196                         $av_pending = 'E';
1197                         $type = 'N';
1198
1199                 } elsif ($cur =~/^(case)/o) {
1200                         print "CASE($1)\n" if ($dbg_values > 1);
1201                         $av_pend_colon = 'C';
1202                         $type = 'N';
1203
1204                 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1205                         print "KEYWORD($1)\n" if ($dbg_values > 1);
1206                         $type = 'N';
1207
1208                 } elsif ($cur =~ /^(\()/o) {
1209                         print "PAREN('$1')\n" if ($dbg_values > 1);
1210                         push(@av_paren_type, $av_pending);
1211                         $av_pending = '_';
1212                         $type = 'N';
1213
1214                 } elsif ($cur =~ /^(\))/o) {
1215                         my $new_type = pop(@av_paren_type);
1216                         if ($new_type ne '_') {
1217                                 $type = $new_type;
1218                                 print "PAREN('$1') -> $type\n"
1219                                                         if ($dbg_values > 1);
1220                         } else {
1221                                 print "PAREN('$1')\n" if ($dbg_values > 1);
1222                         }
1223
1224                 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1225                         print "FUNC($1)\n" if ($dbg_values > 1);
1226                         $type = 'V';
1227                         $av_pending = 'V';
1228
1229                 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1230                         if (defined $2 && $type eq 'C' || $type eq 'T') {
1231                                 $av_pend_colon = 'B';
1232                         } elsif ($type eq 'E') {
1233                                 $av_pend_colon = 'L';
1234                         }
1235                         print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1236                         $type = 'V';
1237
1238                 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1239                         print "IDENT($1)\n" if ($dbg_values > 1);
1240                         $type = 'V';
1241
1242                 } elsif ($cur =~ /^($Assignment)/o) {
1243                         print "ASSIGN($1)\n" if ($dbg_values > 1);
1244                         $type = 'N';
1245
1246                 } elsif ($cur =~/^(;|{|})/) {
1247                         print "END($1)\n" if ($dbg_values > 1);
1248                         $type = 'E';
1249                         $av_pend_colon = 'O';
1250
1251                 } elsif ($cur =~/^(,)/) {
1252                         print "COMMA($1)\n" if ($dbg_values > 1);
1253                         $type = 'C';
1254
1255                 } elsif ($cur =~ /^(\?)/o) {
1256                         print "QUESTION($1)\n" if ($dbg_values > 1);
1257                         $type = 'N';
1258
1259                 } elsif ($cur =~ /^(:)/o) {
1260                         print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1261
1262                         substr($var, length($res), 1, $av_pend_colon);
1263                         if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1264                                 $type = 'E';
1265                         } else {
1266                                 $type = 'N';
1267                         }
1268                         $av_pend_colon = 'O';
1269
1270                 } elsif ($cur =~ /^(\[)/o) {
1271                         print "CLOSE($1)\n" if ($dbg_values > 1);
1272                         $type = 'N';
1273
1274                 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1275                         my $variant;
1276
1277                         print "OPV($1)\n" if ($dbg_values > 1);
1278                         if ($type eq 'V') {
1279                                 $variant = 'B';
1280                         } else {
1281                                 $variant = 'U';
1282                         }
1283
1284                         substr($var, length($res), 1, $variant);
1285                         $type = 'N';
1286
1287                 } elsif ($cur =~ /^($Operators)/o) {
1288                         print "OP($1)\n" if ($dbg_values > 1);
1289                         if ($1 ne '++' && $1 ne '--') {
1290                                 $type = 'N';
1291                         }
1292
1293                 } elsif ($cur =~ /(^.)/o) {
1294                         print "C($1)\n" if ($dbg_values > 1);
1295                 }
1296                 if (defined $1) {
1297                         $cur = substr($cur, length($1));
1298                         $res .= $type x length($1);
1299                 }
1300         }
1301
1302         return ($res, $var);
1303 }
1304
1305 sub possible {
1306         my ($possible, $line) = @_;
1307         my $notPermitted = qr{(?:
1308                 ^(?:
1309                         $Modifier|
1310                         $Storage|
1311                         $Type|
1312                         DEFINE_\S+
1313                 )$|
1314                 ^(?:
1315                         goto|
1316                         return|
1317                         case|
1318                         else|
1319                         asm|__asm__|
1320                         do|
1321                         \#|
1322                         \#\#|
1323                 )(?:\s|$)|
1324                 ^(?:typedef|struct|enum)\b
1325             )}x;
1326         warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1327         if ($possible !~ $notPermitted) {
1328                 # Check for modifiers.
1329                 $possible =~ s/\s*$Storage\s*//g;
1330                 $possible =~ s/\s*$Sparse\s*//g;
1331                 if ($possible =~ /^\s*$/) {
1332
1333                 } elsif ($possible =~ /\s/) {
1334                         $possible =~ s/\s*$Type\s*//g;
1335                         for my $modifier (split(' ', $possible)) {
1336                                 if ($modifier !~ $notPermitted) {
1337                                         warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1338                                         push(@modifierList, $modifier);
1339                                 }
1340                         }
1341
1342                 } else {
1343                         warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1344                         push(@typeList, $possible);
1345                 }
1346                 build_types();
1347         } else {
1348                 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1349         }
1350 }
1351
1352 my $prefix = '';
1353
1354 sub show_type {
1355        return !defined $ignore_type{$_[0]};
1356 }
1357
1358 sub report {
1359         if (!show_type($_[1]) ||
1360             (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1361                 return 0;
1362         }
1363         my $line;
1364         if ($show_types) {
1365                 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1366         } else {
1367                 $line = "$prefix$_[0]: $_[2]\n";
1368         }
1369         $line = (split('\n', $line))[0] . "\n" if ($terse);
1370
1371         push(our @report, $line);
1372
1373         return 1;
1374 }
1375 sub report_dump {
1376         our @report;
1377 }
1378
1379 sub ERROR {
1380         if (report("ERROR", $_[0], $_[1])) {
1381                 our $clean = 0;
1382                 our $cnt_error++;
1383         }
1384 }
1385 sub WARN {
1386         if (report("WARNING", $_[0], $_[1])) {
1387                 our $clean = 0;
1388                 our $cnt_warn++;
1389         }
1390 }
1391 sub CHK {
1392         if ($check && report("CHECK", $_[0], $_[1])) {
1393                 our $clean = 0;
1394                 our $cnt_chk++;
1395         }
1396 }
1397
1398 sub check_absolute_file {
1399         my ($absolute, $herecurr) = @_;
1400         my $file = $absolute;
1401
1402         ##print "absolute<$absolute>\n";
1403
1404         # See if any suffix of this path is a path within the tree.
1405         while ($file =~ s@^[^/]*/@@) {
1406                 if (-f "$root/$file") {
1407                         ##print "file<$file>\n";
1408                         last;
1409                 }
1410         }
1411         if (! -f _)  {
1412                 return 0;
1413         }
1414
1415         # It is, so see if the prefix is acceptable.
1416         my $prefix = $absolute;
1417         substr($prefix, -length($file)) = '';
1418
1419         ##print "prefix<$prefix>\n";
1420         if ($prefix ne ".../") {
1421                 WARN("USE_RELATIVE_PATH",
1422                      "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1423         }
1424 }
1425
1426 sub process {
1427         my $filename = shift;
1428
1429         my $linenr=0;
1430         my $prevline="";
1431         my $prevrawline="";
1432         my $stashline="";
1433         my $stashrawline="";
1434
1435         my $length;
1436         my $indent;
1437         my $previndent=0;
1438         my $stashindent=0;
1439
1440         our $clean = 1;
1441         my $signoff = 0;
1442         my $is_patch = 0;
1443
1444         my $in_header_lines = 1;
1445         my $in_commit_log = 0;          #Scanning lines before patch
1446
1447         our @report = ();
1448         our $cnt_lines = 0;
1449         our $cnt_error = 0;
1450         our $cnt_warn = 0;
1451         our $cnt_chk = 0;
1452
1453         # Trace the real file/line as we go.
1454         my $realfile = '';
1455         my $realline = 0;
1456         my $realcnt = 0;
1457         my $here = '';
1458         my $in_comment = 0;
1459         my $comment_edge = 0;
1460         my $first_line = 0;
1461         my $p1_prefix = '';
1462
1463         my $prev_values = 'E';
1464
1465         # suppression flags
1466         my %suppress_ifbraces;
1467         my %suppress_whiletrailers;
1468         my %suppress_export;
1469         my $suppress_statement = 0;
1470
1471         # Pre-scan the patch sanitizing the lines.
1472         # Pre-scan the patch looking for any __setup documentation.
1473         #
1474         my @setup_docs = ();
1475         my $setup_docs = 0;
1476
1477         sanitise_line_reset();
1478         my $line;
1479         foreach my $rawline (@rawlines) {
1480                 $linenr++;
1481                 $line = $rawline;
1482
1483                 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1484                         $setup_docs = 0;
1485                         if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1486                                 $setup_docs = 1;
1487                         }
1488                         #next;
1489                 }
1490                 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1491                         $realline=$1-1;
1492                         if (defined $2) {
1493                                 $realcnt=$3+1;
1494                         } else {
1495                                 $realcnt=1+1;
1496                         }
1497                         $in_comment = 0;
1498
1499                         # Guestimate if this is a continuing comment.  Run
1500                         # the context looking for a comment "edge".  If this
1501                         # edge is a close comment then we must be in a comment
1502                         # at context start.
1503                         my $edge;
1504                         my $cnt = $realcnt;
1505                         for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1506                                 next if (defined $rawlines[$ln - 1] &&
1507                                          $rawlines[$ln - 1] =~ /^-/);
1508                                 $cnt--;
1509                                 #print "RAW<$rawlines[$ln - 1]>\n";
1510                                 last if (!defined $rawlines[$ln - 1]);
1511                                 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1512                                     $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1513                                         ($edge) = $1;
1514                                         last;
1515                                 }
1516                         }
1517                         if (defined $edge && $edge eq '*/') {
1518                                 $in_comment = 1;
1519                         }
1520
1521                         # Guestimate if this is a continuing comment.  If this
1522                         # is the start of a diff block and this line starts
1523                         # ' *' then it is very likely a comment.
1524                         if (!defined $edge &&
1525                             $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1526                         {
1527                                 $in_comment = 1;
1528                         }
1529
1530                         ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1531                         sanitise_line_reset($in_comment);
1532
1533                 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1534                         # Standardise the strings and chars within the input to
1535                         # simplify matching -- only bother with positive lines.
1536                         $line = sanitise_line($rawline);
1537                 }
1538                 push(@lines, $line);
1539
1540                 if ($realcnt > 1) {
1541                         $realcnt-- if ($line =~ /^(?:\+| |$)/);
1542                 } else {
1543                         $realcnt = 0;
1544                 }
1545
1546                 #print "==>$rawline\n";
1547                 #print "-->$line\n";
1548
1549                 if ($setup_docs && $line =~ /^\+/) {
1550                         push(@setup_docs, $line);
1551                 }
1552         }
1553
1554         $prefix = '';
1555
1556         $realcnt = 0;
1557         $linenr = 0;
1558         foreach my $line (@lines) {
1559                 $linenr++;
1560
1561                 my $rawline = $rawlines[$linenr - 1];
1562
1563 #extract the line range in the file after the patch is applied
1564                 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1565                         $is_patch = 1;
1566                         $first_line = $linenr + 1;
1567                         $realline=$1-1;
1568                         if (defined $2) {
1569                                 $realcnt=$3+1;
1570                         } else {
1571                                 $realcnt=1+1;
1572                         }
1573                         annotate_reset();
1574                         $prev_values = 'E';
1575
1576                         %suppress_ifbraces = ();
1577                         %suppress_whiletrailers = ();
1578                         %suppress_export = ();
1579                         $suppress_statement = 0;
1580                         next;
1581
1582 # track the line number as we move through the hunk, note that
1583 # new versions of GNU diff omit the leading space on completely
1584 # blank context lines so we need to count that too.
1585                 } elsif ($line =~ /^( |\+|$)/) {
1586                         $realline++;
1587                         $realcnt-- if ($realcnt != 0);
1588
1589                         # Measure the line length and indent.
1590                         ($length, $indent) = line_stats($rawline);
1591
1592                         # Track the previous line.
1593                         ($prevline, $stashline) = ($stashline, $line);
1594                         ($previndent, $stashindent) = ($stashindent, $indent);
1595                         ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1596
1597                         #warn "line<$line>\n";
1598
1599                 } elsif ($realcnt == 1) {
1600                         $realcnt--;
1601                 }
1602
1603                 my $hunk_line = ($realcnt != 0);
1604
1605 #make up the handle for any error we report on this line
1606                 $prefix = "$filename:$realline: " if ($emacs && $file);
1607                 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1608
1609                 $here = "#$linenr: " if (!$file);
1610                 $here = "#$realline: " if ($file);
1611
1612                 # extract the filename as it passes
1613                 if ($line =~ /^diff --git.*?(\S+)$/) {
1614                         $realfile = $1;
1615                         $realfile =~ s@^([^/]*)/@@;
1616                         $in_commit_log = 0;
1617                 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1618                         $realfile = $1;
1619                         $realfile =~ s@^([^/]*)/@@;
1620                         $in_commit_log = 0;
1621
1622                         $p1_prefix = $1;
1623                         if (!$file && $tree && $p1_prefix ne '' &&
1624                             -e "$root/$p1_prefix") {
1625                                 WARN("PATCH_PREFIX",
1626                                      "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1627                         }
1628
1629                         if ($realfile =~ m@^include/asm/@) {
1630                                 ERROR("MODIFIED_INCLUDE_ASM",
1631                                       "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1632                         }
1633                         next;
1634                 }
1635
1636                 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1637
1638                 my $hereline = "$here\n$rawline\n";
1639                 my $herecurr = "$here\n$rawline\n";
1640                 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1641
1642                 $cnt_lines++ if ($realcnt != 0);
1643
1644 # Check for incorrect file permissions
1645                 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1646                         my $permhere = $here . "FILE: $realfile\n";
1647                         if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1648                                 ERROR("EXECUTE_PERMISSIONS",
1649                                       "do not set execute permissions for source files\n" . $permhere);
1650                         }
1651                 }
1652
1653 # Check the patch for a signoff:
1654                 if ($line =~ /^\s*signed-off-by:/i) {
1655                         $signoff++;
1656                         $in_commit_log = 0;
1657                 }
1658
1659 # Check signature styles
1660                 if (!$in_header_lines &&
1661                     $line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
1662                         my $space_before = $1;
1663                         my $sign_off = $2;
1664                         my $space_after = $3;
1665                         my $email = $4;
1666                         my $ucfirst_sign_off = ucfirst(lc($sign_off));
1667
1668                         if (defined $space_before && $space_before ne "") {
1669                                 WARN("BAD_SIGN_OFF",
1670                                      "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
1671                         }
1672                         if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1673                                 WARN("BAD_SIGN_OFF",
1674                                      "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
1675                         }
1676                         if (!defined $space_after || $space_after ne " ") {
1677                                 WARN("BAD_SIGN_OFF",
1678                                      "Use a single space after $ucfirst_sign_off\n" . $herecurr);
1679                         }
1680
1681                         my ($email_name, $email_address, $comment) = parse_email($email);
1682                         my $suggested_email = format_email(($email_name, $email_address));
1683                         if ($suggested_email eq "") {
1684                                 ERROR("BAD_SIGN_OFF",
1685                                       "Unrecognized email address: '$email'\n" . $herecurr);
1686                         } else {
1687                                 my $dequoted = $suggested_email;
1688                                 $dequoted =~ s/^"//;
1689                                 $dequoted =~ s/" </ </;
1690                                 # Don't force email to have quotes
1691                                 # Allow just an angle bracketed address
1692                                 if ("$dequoted$comment" ne $email &&
1693                                     "<$email_address>$comment" ne $email &&
1694                                     "$suggested_email$comment" ne $email) {
1695                                         WARN("BAD_SIGN_OFF",
1696                                              "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1697                                 }
1698                         }
1699                 }
1700
1701 # Check for wrappage within a valid hunk of the file
1702                 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1703                         ERROR("CORRUPTED_PATCH",
1704                               "patch seems to be corrupt (line wrapped?)\n" .
1705                                 $herecurr) if (!$emitted_corrupt++);
1706                 }
1707
1708 # Check for absolute kernel paths.
1709                 if ($tree) {
1710                         while ($line =~ m{(?:^|\s)(/\S*)}g) {
1711                                 my $file = $1;
1712
1713                                 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1714                                     check_absolute_file($1, $herecurr)) {
1715                                         #
1716                                 } else {
1717                                         check_absolute_file($file, $herecurr);
1718                                 }
1719                         }
1720                 }
1721
1722 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1723                 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1724                     $rawline !~ m/^$UTF8*$/) {
1725                         my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1726
1727                         my $blank = copy_spacing($rawline);
1728                         my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1729                         my $hereptr = "$hereline$ptr\n";
1730
1731                         CHK("INVALID_UTF8",
1732                             "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1733                 }
1734
1735 # Check if it's the start of a commit log
1736 # (not a header line and we haven't seen the patch filename)
1737                 if ($in_header_lines && $realfile =~ /^$/ &&
1738                     $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
1739                         $in_header_lines = 0;
1740                         $in_commit_log = 1;
1741                 }
1742
1743 # Still not yet in a patch, check for any UTF-8
1744                 if ($in_commit_log && $realfile =~ /^$/ &&
1745                     $rawline =~ /$NON_ASCII_UTF8/) {
1746                         CHK("UTF8_BEFORE_PATCH",
1747                             "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1748                 }
1749
1750 # ignore non-hunk lines and lines being removed
1751                 next if (!$hunk_line || $line =~ /^-/);
1752
1753 #trailing whitespace
1754                 if ($line =~ /^\+.*\015/) {
1755                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1756                         ERROR("DOS_LINE_ENDINGS",
1757                               "DOS line endings\n" . $herevet);
1758
1759                 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1760                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1761                         ERROR("TRAILING_WHITESPACE",
1762                               "trailing whitespace\n" . $herevet);
1763                         $rpt_cleaners = 1;
1764                 }
1765
1766 # check for Kconfig help text having a real description
1767 # Only applies when adding the entry originally, after that we do not have
1768 # sufficient context to determine whether it is indeed long enough.
1769                 if ($realfile =~ /Kconfig/ &&
1770                     $line =~ /.\s*config\s+/) {
1771                         my $length = 0;
1772                         my $cnt = $realcnt;
1773                         my $ln = $linenr + 1;
1774                         my $f;
1775                         my $is_start = 0;
1776                         my $is_end = 0;
1777                         for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
1778                                 $f = $lines[$ln - 1];
1779                                 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1780                                 $is_end = $lines[$ln - 1] =~ /^\+/;
1781
1782                                 next if ($f =~ /^-/);
1783
1784                                 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1785                                         $is_start = 1;
1786                                 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1787                                         $length = -1;
1788                                 }
1789
1790                                 $f =~ s/^.//;
1791                                 $f =~ s/#.*//;
1792                                 $f =~ s/^\s+//;
1793                                 next if ($f =~ /^$/);
1794                                 if ($f =~ /^\s*config\s/) {
1795                                         $is_end = 1;
1796                                         last;
1797                                 }
1798                                 $length++;
1799                         }
1800                         WARN("CONFIG_DESCRIPTION",
1801                              "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1802                         #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
1803                 }
1804
1805                 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1806                     ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1807                         my $flag = $1;
1808                         my $replacement = {
1809                                 'EXTRA_AFLAGS' =>   'asflags-y',
1810                                 'EXTRA_CFLAGS' =>   'ccflags-y',
1811                                 'EXTRA_CPPFLAGS' => 'cppflags-y',
1812                                 'EXTRA_LDFLAGS' =>  'ldflags-y',
1813                         };
1814
1815                         WARN("DEPRECATED_VARIABLE",
1816                              "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1817                 }
1818
1819 # check we are in a valid source file if not then ignore this hunk
1820                 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1821
1822 #80 column limit
1823                 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1824                     $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1825                     !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1826                     $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1827                     $length > 80)
1828                 {
1829                         WARN("LONG_LINE",
1830                              "line over 80 characters\n" . $herecurr);
1831                 }
1832
1833 # check for spaces before a quoted newline
1834                 if ($rawline =~ /^.*\".*\s\\n/) {
1835                         WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1836                              "unnecessary whitespace before a quoted newline\n" . $herecurr);
1837                 }
1838
1839 # check for adding lines without a newline.
1840                 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1841                         WARN("MISSING_EOF_NEWLINE",
1842                              "adding a line without newline at end of file\n" . $herecurr);
1843                 }
1844
1845 # Blackfin: use hi/lo macros
1846                 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1847                         if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1848                                 my $herevet = "$here\n" . cat_vet($line) . "\n";
1849                                 ERROR("LO_MACRO",
1850                                       "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1851                         }
1852                         if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1853                                 my $herevet = "$here\n" . cat_vet($line) . "\n";
1854                                 ERROR("HI_MACRO",
1855                                       "use the HI() macro, not (... >> 16)\n" . $herevet);
1856                         }
1857                 }
1858
1859 # check we are in a valid source file C, perl or bash script
1860 # if not then ignore this hunk
1861                 next if ($realfile !~ /\.(h|c|pl|sh)$/);
1862
1863 # at the beginning of a line any tabs must come first and anything
1864 # more than 8 must use tabs.
1865                 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1866                     $rawline =~ /^\+\s*        \s*/) {
1867                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1868                         ERROR("CODE_INDENT",
1869                               "code indent should use tabs where possible\n" . $herevet);
1870                         $rpt_cleaners = 1;
1871                 }
1872
1873 # check for space before tabs.
1874                 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1875                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1876                         WARN("SPACE_BEFORE_TAB",
1877                              "please, no space before tabs\n" . $herevet);
1878                 }
1879
1880 # check for spaces at the beginning of a line.
1881 # Exceptions:
1882 #  1) within comments
1883 #  2) indented preprocessor commands
1884 #  3) hanging labels
1885                 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/)  {
1886                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1887                         WARN("LEADING_SPACE",
1888                              "please, no spaces at the start of a line\n" . $herevet);
1889                 }
1890
1891 # check we are in a valid C source file if not then ignore this hunk
1892                 next if ($realfile !~ /\.(h|c)$/);
1893
1894 # check for RCS/CVS revision markers
1895                 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1896                         WARN("CVS_KEYWORD",
1897                              "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1898                 }
1899
1900 # Blackfin: don't use __builtin_bfin_[cs]sync
1901                 if ($line =~ /__builtin_bfin_csync/) {
1902                         my $herevet = "$here\n" . cat_vet($line) . "\n";
1903                         ERROR("CSYNC",
1904                               "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1905                 }
1906                 if ($line =~ /__builtin_bfin_ssync/) {
1907                         my $herevet = "$here\n" . cat_vet($line) . "\n";
1908                         ERROR("SSYNC",
1909                               "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1910                 }
1911
1912 # Check for potential 'bare' types
1913                 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1914                     $realline_next);
1915 #print "LINE<$line>\n";
1916                 if ($linenr >= $suppress_statement &&
1917                     $realcnt && $line =~ /.\s*\S/) {
1918                         ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1919                                 ctx_statement_block($linenr, $realcnt, 0);
1920                         $stat =~ s/\n./\n /g;
1921                         $cond =~ s/\n./\n /g;
1922
1923 #print "linenr<$linenr> <$stat>\n";
1924                         # If this statement has no statement boundaries within
1925                         # it there is no point in retrying a statement scan
1926                         # until we hit end of it.
1927                         my $frag = $stat; $frag =~ s/;+\s*$//;
1928                         if ($frag !~ /(?:{|;)/) {
1929 #print "skip<$line_nr_next>\n";
1930                                 $suppress_statement = $line_nr_next;
1931                         }
1932
1933                         # Find the real next line.
1934                         $realline_next = $line_nr_next;
1935                         if (defined $realline_next &&
1936                             (!defined $lines[$realline_next - 1] ||
1937                              substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1938                                 $realline_next++;
1939                         }
1940
1941                         my $s = $stat;
1942                         $s =~ s/{.*$//s;
1943
1944                         # Ignore goto labels.
1945                         if ($s =~ /$Ident:\*$/s) {
1946
1947                         # Ignore functions being called
1948                         } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1949
1950                         } elsif ($s =~ /^.\s*else\b/s) {
1951
1952                         # declarations always start with types
1953                         } 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) {
1954                                 my $type = $1;
1955                                 $type =~ s/\s+/ /g;
1956                                 possible($type, "A:" . $s);
1957
1958                         # definitions in global scope can only start with types
1959                         } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1960                                 possible($1, "B:" . $s);
1961                         }
1962
1963                         # any (foo ... *) is a pointer cast, and foo is a type
1964                         while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1965                                 possible($1, "C:" . $s);
1966                         }
1967
1968                         # Check for any sort of function declaration.
1969                         # int foo(something bar, other baz);
1970                         # void (*store_gdt)(x86_descr_ptr *);
1971                         if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1972                                 my ($name_len) = length($1);
1973
1974                                 my $ctx = $s;
1975                                 substr($ctx, 0, $name_len + 1, '');
1976                                 $ctx =~ s/\)[^\)]*$//;
1977
1978                                 for my $arg (split(/\s*,\s*/, $ctx)) {
1979                                         if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1980
1981                                                 possible($1, "D:" . $s);
1982                                         }
1983                                 }
1984                         }
1985
1986                 }
1987
1988 #
1989 # Checks which may be anchored in the context.
1990 #
1991
1992 # Check for switch () and associated case and default
1993 # statements should be at the same indent.
1994                 if ($line=~/\bswitch\s*\(.*\)/) {
1995                         my $err = '';
1996                         my $sep = '';
1997                         my @ctx = ctx_block_outer($linenr, $realcnt);
1998                         shift(@ctx);
1999                         for my $ctx (@ctx) {
2000                                 my ($clen, $cindent) = line_stats($ctx);
2001                                 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2002                                                         $indent != $cindent) {
2003                                         $err .= "$sep$ctx\n";
2004                                         $sep = '';
2005                                 } else {
2006                                         $sep = "[...]\n";
2007                                 }
2008                         }
2009                         if ($err ne '') {
2010                                 ERROR("SWITCH_CASE_INDENT_LEVEL",
2011                                       "switch and case should be at the same indent\n$hereline$err");
2012                         }
2013                 }
2014
2015 # if/while/etc brace do not go on next line, unless defining a do while loop,
2016 # or if that brace on the next line is for something else
2017                 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2018                         my $pre_ctx = "$1$2";
2019
2020                         my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
2021
2022                         if ($line =~ /^\+\t{6,}/) {
2023                                 WARN("DEEP_INDENTATION",
2024                                      "Too many leading tabs - consider code refactoring\n" . $herecurr);
2025                         }
2026
2027                         my $ctx_cnt = $realcnt - $#ctx - 1;
2028                         my $ctx = join("\n", @ctx);
2029
2030                         my $ctx_ln = $linenr;
2031                         my $ctx_skip = $realcnt;
2032
2033                         while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2034                                         defined $lines[$ctx_ln - 1] &&
2035                                         $lines[$ctx_ln - 1] =~ /^-/)) {
2036                                 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2037                                 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2038                                 $ctx_ln++;
2039                         }
2040
2041                         #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2042                         #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2043
2044                         if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2045                                 ERROR("OPEN_BRACE",
2046                                       "that open brace { should be on the previous line\n" .
2047                                         "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2048                         }
2049                         if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2050                             $ctx =~ /\)\s*\;\s*$/ &&
2051                             defined $lines[$ctx_ln - 1])
2052                         {
2053                                 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2054                                 if ($nindent > $indent) {
2055                                         WARN("TRAILING_SEMICOLON",
2056                                              "trailing semicolon indicates no statements, indent implies otherwise\n" .
2057                                                 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2058                                 }
2059                         }
2060                 }
2061
2062 # Check relative indent for conditionals and blocks.
2063                 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2064                         ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2065                                 ctx_statement_block($linenr, $realcnt, 0)
2066                                         if (!defined $stat);
2067                         my ($s, $c) = ($stat, $cond);
2068
2069                         substr($s, 0, length($c), '');
2070
2071                         # Make sure we remove the line prefixes as we have
2072                         # none on the first line, and are going to readd them
2073                         # where necessary.
2074                         $s =~ s/\n./\n/gs;
2075
2076                         # Find out how long the conditional actually is.
2077                         my @newlines = ($c =~ /\n/gs);
2078                         my $cond_lines = 1 + $#newlines;
2079
2080                         # We want to check the first line inside the block
2081                         # starting at the end of the conditional, so remove:
2082                         #  1) any blank line termination
2083                         #  2) any opening brace { on end of the line
2084                         #  3) any do (...) {
2085                         my $continuation = 0;
2086                         my $check = 0;
2087                         $s =~ s/^.*\bdo\b//;
2088                         $s =~ s/^\s*{//;
2089                         if ($s =~ s/^\s*\\//) {
2090                                 $continuation = 1;
2091                         }
2092                         if ($s =~ s/^\s*?\n//) {
2093                                 $check = 1;
2094                                 $cond_lines++;
2095                         }
2096
2097                         # Also ignore a loop construct at the end of a
2098                         # preprocessor statement.
2099                         if (($prevline =~ /^.\s*#\s*define\s/ ||
2100                             $prevline =~ /\\\s*$/) && $continuation == 0) {
2101                                 $check = 0;
2102                         }
2103
2104                         my $cond_ptr = -1;
2105                         $continuation = 0;
2106                         while ($cond_ptr != $cond_lines) {
2107                                 $cond_ptr = $cond_lines;
2108
2109                                 # If we see an #else/#elif then the code
2110                                 # is not linear.
2111                                 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2112                                         $check = 0;
2113                                 }
2114
2115                                 # Ignore:
2116                                 #  1) blank lines, they should be at 0,
2117                                 #  2) preprocessor lines, and
2118                                 #  3) labels.
2119                                 if ($continuation ||
2120                                     $s =~ /^\s*?\n/ ||
2121                                     $s =~ /^\s*#\s*?/ ||
2122                                     $s =~ /^\s*$Ident\s*:/) {
2123                                         $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
2124                                         if ($s =~ s/^.*?\n//) {
2125                                                 $cond_lines++;
2126                                         }
2127                                 }
2128                         }
2129
2130                         my (undef, $sindent) = line_stats("+" . $s);
2131                         my $stat_real = raw_line($linenr, $cond_lines);
2132
2133                         # Check if either of these lines are modified, else
2134                         # this is not this patch's fault.
2135                         if (!defined($stat_real) ||
2136                             $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2137                                 $check = 0;
2138                         }
2139                         if (defined($stat_real) && $cond_lines > 1) {
2140                                 $stat_real = "[...]\n$stat_real";
2141                         }
2142
2143                         #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";
2144
2145                         if ($check && (($sindent % 8) != 0 ||
2146                             ($sindent <= $indent && $s ne ''))) {
2147                                 WARN("SUSPECT_CODE_INDENT",
2148                                      "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2149                         }
2150                 }
2151
2152                 # Track the 'values' across context and added lines.
2153                 my $opline = $line; $opline =~ s/^./ /;
2154                 my ($curr_values, $curr_vars) =
2155                                 annotate_values($opline . "\n", $prev_values);
2156                 $curr_values = $prev_values . $curr_values;
2157                 if ($dbg_values) {
2158                         my $outline = $opline; $outline =~ s/\t/ /g;
2159                         print "$linenr > .$outline\n";
2160                         print "$linenr > $curr_values\n";
2161                         print "$linenr >  $curr_vars\n";
2162                 }
2163                 $prev_values = substr($curr_values, -1);
2164
2165 #ignore lines not being added
2166                 if ($line=~/^[^\+]/) {next;}
2167
2168 # TEST: allow direct testing of the type matcher.
2169                 if ($dbg_type) {
2170                         if ($line =~ /^.\s*$Declare\s*$/) {
2171                                 ERROR("TEST_TYPE",
2172                                       "TEST: is type\n" . $herecurr);
2173                         } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2174                                 ERROR("TEST_NOT_TYPE",
2175                                       "TEST: is not type ($1 is)\n". $herecurr);
2176                         }
2177                         next;
2178                 }
2179 # TEST: allow direct testing of the attribute matcher.
2180                 if ($dbg_attr) {
2181                         if ($line =~ /^.\s*$Modifier\s*$/) {
2182                                 ERROR("TEST_ATTR",
2183                                       "TEST: is attr\n" . $herecurr);
2184                         } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2185                                 ERROR("TEST_NOT_ATTR",
2186                                       "TEST: is not attr ($1 is)\n". $herecurr);
2187                         }
2188                         next;
2189                 }
2190
2191 # check for initialisation to aggregates open brace on the next line
2192                 if ($line =~ /^.\s*{/ &&
2193                     $prevline =~ /(?:^|[^=])=\s*$/) {
2194                         ERROR("OPEN_BRACE",
2195                               "that open brace { should be on the previous line\n" . $hereprev);
2196                 }
2197
2198 #
2199 # Checks which are anchored on the added line.
2200 #
2201
2202 # check for malformed paths in #include statements (uses RAW line)
2203                 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2204                         my $path = $1;
2205                         if ($path =~ m{//}) {
2206                                 ERROR("MALFORMED_INCLUDE",
2207                                       "malformed #include filename\n" .
2208                                         $herecurr);
2209                         }
2210                 }
2211
2212 # no C99 // comments
2213                 if ($line =~ m{//}) {
2214                         ERROR("C99_COMMENTS",
2215                               "do not use C99 // comments\n" . $herecurr);
2216                 }
2217                 # Remove C99 comments.
2218                 $line =~ s@//.*@@;
2219                 $opline =~ s@//.*@@;
2220
2221 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2222 # the whole statement.
2223 #print "APW <$lines[$realline_next - 1]>\n";
2224                 if (defined $realline_next &&
2225                     exists $lines[$realline_next - 1] &&
2226                     !defined $suppress_export{$realline_next} &&
2227                     ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2228                      $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2229                         # Handle definitions which produce identifiers with
2230                         # a prefix:
2231                         #   XXX(foo);
2232                         #   EXPORT_SYMBOL(something_foo);
2233                         my $name = $1;
2234                         if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
2235                             $name =~ /^${Ident}_$2/) {
2236 #print "FOO C name<$name>\n";
2237                                 $suppress_export{$realline_next} = 1;
2238
2239                         } elsif ($stat !~ /(?:
2240                                 \n.}\s*$|
2241                                 ^.DEFINE_$Ident\(\Q$name\E\)|
2242                                 ^.DECLARE_$Ident\(\Q$name\E\)|
2243                                 ^.LIST_HEAD\(\Q$name\E\)|
2244                                 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2245                                 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
2246                             )/x) {
2247 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2248                                 $suppress_export{$realline_next} = 2;
2249                         } else {
2250                                 $suppress_export{$realline_next} = 1;
2251                         }
2252                 }
2253                 if (!defined $suppress_export{$linenr} &&
2254                     $prevline =~ /^.\s*$/ &&
2255                     ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2256                      $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2257 #print "FOO B <$lines[$linenr - 1]>\n";
2258                         $suppress_export{$linenr} = 2;
2259                 }
2260                 if (defined $suppress_export{$linenr} &&
2261                     $suppress_export{$linenr} == 2) {
2262                         WARN("EXPORT_SYMBOL",
2263                              "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2264                 }
2265
2266 # check for global initialisers.
2267                 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2268                         ERROR("GLOBAL_INITIALISERS",
2269                               "do not initialise globals to 0 or NULL\n" .
2270                                 $herecurr);
2271                 }
2272 # check for static initialisers.
2273                 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2274                         ERROR("INITIALISED_STATIC",
2275                               "do not initialise statics to 0 or NULL\n" .
2276                                 $herecurr);
2277                 }
2278
2279 # check for static const char * arrays.
2280                 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2281                         WARN("STATIC_CONST_CHAR_ARRAY",
2282                              "static const char * array should probably be static const char * const\n" .
2283                                 $herecurr);
2284                }
2285
2286 # check for static char foo[] = "bar" declarations.
2287                 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2288                         WARN("STATIC_CONST_CHAR_ARRAY",
2289                              "static char array declaration should probably be static const char\n" .
2290                                 $herecurr);
2291                }
2292
2293 # check for declarations of struct pci_device_id
2294                 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2295                         WARN("DEFINE_PCI_DEVICE_TABLE",
2296                              "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2297                 }
2298
2299 # check for new typedefs, only function parameters and sparse annotations
2300 # make sense.
2301                 if ($line =~ /\btypedef\s/ &&
2302                     $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2303                     $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
2304                     $line !~ /\b$typeTypedefs\b/ &&
2305                     $line !~ /\b__bitwise(?:__|)\b/) {
2306                         WARN("NEW_TYPEDEFS",
2307                              "do not add new typedefs\n" . $herecurr);
2308                 }
2309
2310 # * goes on variable not on type
2311                 # (char*[ const])
2312                 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2313                         #print "AA<$1>\n";
2314                         my ($from, $to) = ($2, $2);
2315
2316                         # Should start with a space.
2317                         $to =~ s/^(\S)/ $1/;
2318                         # Should not end with a space.
2319                         $to =~ s/\s+$//;
2320                         # '*'s should not have spaces between.
2321                         while ($to =~ s/\*\s+\*/\*\*/) {
2322                         }
2323
2324                         #print "from<$from> to<$to>\n";
2325                         if ($from ne $to) {
2326                                 ERROR("POINTER_LOCATION",
2327                                       "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr);
2328                         }
2329                 }
2330                 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2331                         #print "BB<$1>\n";
2332                         my ($from, $to, $ident) = ($2, $2, $3);
2333
2334                         # Should start with a space.
2335                         $to =~ s/^(\S)/ $1/;
2336                         # Should not end with a space.
2337                         $to =~ s/\s+$//;
2338                         # '*'s should not have spaces between.
2339                         while ($to =~ s/\*\s+\*/\*\*/) {
2340                         }
2341                         # Modifiers should have spaces.
2342                         $to =~ s/(\b$Modifier$)/$1 /;
2343
2344                         #print "from<$from> to<$to> ident<$ident>\n";
2345                         if ($from ne $to && $ident !~ /^$Modifier$/) {
2346                                 ERROR("POINTER_LOCATION",
2347                                       "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr);
2348                         }
2349                 }
2350
2351 # # no BUG() or BUG_ON()
2352 #               if ($line =~ /\b(BUG|BUG_ON)\b/) {
2353 #                       print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2354 #                       print "$herecurr";
2355 #                       $clean = 0;
2356 #               }
2357
2358                 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2359                         WARN("LINUX_VERSION_CODE",
2360                              "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
2361                 }
2362
2363 # check for uses of printk_ratelimit
2364                 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2365                         WARN("PRINTK_RATELIMITED",
2366 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2367                 }
2368
2369 # printk should use KERN_* levels.  Note that follow on printk's on the
2370 # same line do not need a level, so we use the current block context
2371 # to try and find and validate the current printk.  In summary the current
2372 # printk includes all preceding printk's which have no newline on the end.
2373 # we assume the first bad printk is the one to report.
2374                 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
2375                         my $ok = 0;
2376                         for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2377                                 #print "CHECK<$lines[$ln - 1]\n";
2378                                 # we have a preceding printk if it ends
2379                                 # with "\n" ignore it, else it is to blame
2380                                 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2381                                         if ($rawlines[$ln - 1] !~ m{\\n"}) {
2382                                                 $ok = 1;
2383                                         }
2384                                         last;
2385                                 }
2386                         }
2387                         if ($ok == 0) {
2388                                 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2389                                      "printk() should include KERN_ facility level\n" . $herecurr);
2390                         }
2391                 }
2392
2393 # function brace can't be on same line, except for #defines of do while,
2394 # or if closed on same line
2395                 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2396                     !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2397                         ERROR("OPEN_BRACE",
2398                               "open brace '{' following function declarations go on the next line\n" . $herecurr);
2399                 }
2400
2401 # open braces for enum, union and struct go on the same line.
2402                 if ($line =~ /^.\s*{/ &&
2403                     $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2404                         ERROR("OPEN_BRACE",
2405                               "open brace '{' following $1 go on the same line\n" . $hereprev);
2406                 }
2407
2408 # missing space after union, struct or enum definition
2409                 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2410                     WARN("SPACING",
2411                          "missing space after $1 definition\n" . $herecurr);
2412                 }
2413
2414 # check for spacing round square brackets; allowed:
2415 #  1. with a type on the left -- int [] a;
2416 #  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2417 #  3. inside a curly brace -- = { [0...10] = 5 }
2418                 while ($line =~ /(.*?\s)\[/g) {
2419                         my ($where, $prefix) = ($-[1], $1);
2420                         if ($prefix !~ /$Type\s+$/ &&
2421                             ($where != 0 || $prefix !~ /^.\s+$/) &&
2422                             $prefix !~ /{\s+$/) {
2423                                 ERROR("BRACKET_SPACE",
2424                                       "space prohibited before open square bracket '['\n" . $herecurr);
2425                         }
2426                 }
2427
2428 # check for spaces between functions and their parentheses.
2429                 while ($line =~ /($Ident)\s+\(/g) {
2430                         my $name = $1;
2431                         my $ctx_before = substr($line, 0, $-[1]);
2432                         my $ctx = "$ctx_before$name";
2433
2434                         # Ignore those directives where spaces _are_ permitted.
2435                         if ($name =~ /^(?:
2436                                 if|for|while|switch|return|case|
2437                                 volatile|__volatile__|
2438                                 __attribute__|format|__extension__|
2439                                 asm|__asm__)$/x)
2440                         {
2441
2442                         # cpp #define statements have non-optional spaces, ie
2443                         # if there is a space between the name and the open
2444                         # parenthesis it is simply not a parameter group.
2445                         } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2446
2447                         # cpp #elif statement condition may start with a (
2448                         } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2449
2450                         # If this whole things ends with a type its most
2451                         # likely a typedef for a function.
2452                         } elsif ($ctx =~ /$Type$/) {
2453
2454                         } else {
2455                                 WARN("SPACING",
2456                                      "space prohibited between function name and open parenthesis '('\n" . $herecurr);
2457                         }
2458                 }
2459 # Check operator spacing.
2460                 if (!($line=~/\#\s*include/)) {
2461                         my $ops = qr{
2462                                 <<=|>>=|<=|>=|==|!=|
2463                                 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2464                                 =>|->|<<|>>|<|>|=|!|~|
2465                                 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2466                                 \?|:
2467                         }x;
2468                         my @elements = split(/($ops|;)/, $opline);
2469                         my $off = 0;
2470
2471                         my $blank = copy_spacing($opline);
2472
2473                         for (my $n = 0; $n < $#elements; $n += 2) {
2474                                 $off += length($elements[$n]);
2475
2476                                 # Pick up the preceding and succeeding characters.
2477                                 my $ca = substr($opline, 0, $off);
2478                                 my $cc = '';
2479                                 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2480                                         $cc = substr($opline, $off + length($elements[$n + 1]));
2481                                 }
2482                                 my $cb = "$ca$;$cc";
2483
2484                                 my $a = '';
2485                                 $a = 'V' if ($elements[$n] ne '');
2486                                 $a = 'W' if ($elements[$n] =~ /\s$/);
2487                                 $a = 'C' if ($elements[$n] =~ /$;$/);
2488                                 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2489                                 $a = 'O' if ($elements[$n] eq '');
2490                                 $a = 'E' if ($ca =~ /^\s*$/);
2491
2492                                 my $op = $elements[$n + 1];
2493
2494                                 my $c = '';
2495                                 if (defined $elements[$n + 2]) {
2496                                         $c = 'V' if ($elements[$n + 2] ne '');
2497                                         $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2498                                         $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2499                                         $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2500                                         $c = 'O' if ($elements[$n + 2] eq '');
2501                                         $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2502                                 } else {
2503                                         $c = 'E';
2504                                 }
2505
2506                                 my $ctx = "${a}x${c}";
2507
2508                                 my $at = "(ctx:$ctx)";
2509
2510                                 my $ptr = substr($blank, 0, $off) . "^";
2511                                 my $hereptr = "$hereline$ptr\n";
2512
2513                                 # Pull out the value of this operator.
2514                                 my $op_type = substr($curr_values, $off + 1, 1);
2515
2516                                 # Get the full operator variant.
2517                                 my $opv = $op . substr($curr_vars, $off, 1);
2518
2519                                 # Ignore operators passed as parameters.
2520                                 if ($op_type ne 'V' &&
2521                                     $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2522
2523 #                               # Ignore comments
2524 #                               } elsif ($op =~ /^$;+$/) {
2525
2526                                 # ; should have either the end of line or a space or \ after it
2527                                 } elsif ($op eq ';') {
2528                                         if ($ctx !~ /.x[WEBC]/ &&
2529                                             $cc !~ /^\\/ && $cc !~ /^;/) {
2530                                                 ERROR("SPACING",
2531                                                       "space required after that '$op' $at\n" . $hereptr);
2532                                         }
2533
2534                                 # // is a comment
2535                                 } elsif ($op eq '//') {
2536
2537                                 # No spaces for:
2538                                 #   ->
2539                                 #   :   when part of a bitfield
2540                                 } elsif ($op eq '->' || $opv eq ':B') {
2541                                         if ($ctx =~ /Wx.|.xW/) {
2542                                                 ERROR("SPACING",
2543                                                       "spaces prohibited around that '$op' $at\n" . $hereptr);
2544                                         }
2545
2546                                 # , must have a space on the right.
2547                                 } elsif ($op eq ',') {
2548                                         if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2549                                                 ERROR("SPACING",
2550                                                       "space required after that '$op' $at\n" . $hereptr);
2551                                         }
2552
2553                                 # '*' as part of a type definition -- reported already.
2554                                 } elsif ($opv eq '*_') {
2555                                         #warn "'*' is part of type\n";
2556
2557                                 # unary operators should have a space before and
2558                                 # none after.  May be left adjacent to another
2559                                 # unary operator, or a cast
2560                                 } elsif ($op eq '!' || $op eq '~' ||
2561                                          $opv eq '*U' || $opv eq '-U' ||
2562                                          $opv eq '&U' || $opv eq '&&U') {
2563                                         if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2564                                                 ERROR("SPACING",
2565                                                       "space required before that '$op' $at\n" . $hereptr);
2566                                         }
2567                                         if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2568                                                 # A unary '*' may be const
2569
2570                                         } elsif ($ctx =~ /.xW/) {
2571                                                 ERROR("SPACING",
2572                                                       "space prohibited after that '$op' $at\n" . $hereptr);
2573                                         }
2574
2575                                 # unary ++ and unary -- are allowed no space on one side.
2576                                 } elsif ($op eq '++' or $op eq '--') {
2577                                         if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2578                                                 ERROR("SPACING",
2579                                                       "space required one side of that '$op' $at\n" . $hereptr);
2580                                         }
2581                                         if ($ctx =~ /Wx[BE]/ ||
2582                                             ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2583                                                 ERROR("SPACING",
2584                                                       "space prohibited before that '$op' $at\n" . $hereptr);
2585                                         }
2586                                         if ($ctx =~ /ExW/) {
2587                                                 ERROR("SPACING",
2588                                                       "space prohibited after that '$op' $at\n" . $hereptr);
2589                                         }
2590
2591
2592                                 # << and >> may either have or not have spaces both sides
2593                                 } elsif ($op eq '<<' or $op eq '>>' or
2594                                          $op eq '&' or $op eq '^' or $op eq '|' or
2595                                          $op eq '+' or $op eq '-' or
2596                                          $op eq '*' or $op eq '/' or
2597                                          $op eq '%')
2598                                 {
2599                                         if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2600                                                 ERROR("SPACING",
2601                                                       "need consistent spacing around '$op' $at\n" .
2602                                                         $hereptr);
2603                                         }
2604
2605                                 # A colon needs no spaces before when it is
2606                                 # terminating a case value or a label.
2607                                 } elsif ($opv eq ':C' || $opv eq ':L') {
2608                                         if ($ctx =~ /Wx./) {
2609                                                 ERROR("SPACING",
2610                                                       "space prohibited before that '$op' $at\n" . $hereptr);
2611                                         }
2612
2613                                 # All the others need spaces both sides.
2614                                 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2615                                         my $ok = 0;
2616
2617                                         # Ignore email addresses <foo@bar>
2618                                         if (($op eq '<' &&
2619                                              $cc =~ /^\S+\@\S+>/) ||
2620                                             ($op eq '>' &&
2621                                              $ca =~ /<\S+\@\S+$/))
2622                                         {
2623                                                 $ok = 1;
2624                                         }
2625
2626                                         # Ignore ?:
2627                                         if (($opv eq ':O' && $ca =~ /\?$/) ||
2628                                             ($op eq '?' && $cc =~ /^:/)) {
2629                                                 $ok = 1;
2630                                         }
2631
2632                                         if ($ok == 0) {
2633                                                 ERROR("SPACING",
2634                                                       "spaces required around that '$op' $at\n" . $hereptr);
2635                                         }
2636                                 }
2637                                 $off += length($elements[$n + 1]);
2638                         }
2639                 }
2640
2641 # check for multiple assignments
2642                 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2643                         CHK("MULTIPLE_ASSIGNMENTS",
2644                             "multiple assignments should be avoided\n" . $herecurr);
2645                 }
2646
2647 ## # check for multiple declarations, allowing for a function declaration
2648 ## # continuation.
2649 ##              if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2650 ##                  $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2651 ##
2652 ##                      # Remove any bracketed sections to ensure we do not
2653 ##                      # falsly report the parameters of functions.
2654 ##                      my $ln = $line;
2655 ##                      while ($ln =~ s/\([^\(\)]*\)//g) {
2656 ##                      }
2657 ##                      if ($ln =~ /,/) {
2658 ##                              WARN("MULTIPLE_DECLARATION",
2659 ##                                   "declaring multiple variables together should be avoided\n" . $herecurr);
2660 ##                      }
2661 ##              }
2662
2663 #need space before brace following if, while, etc
2664                 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2665                     $line =~ /do{/) {
2666                         ERROR("SPACING",
2667                               "space required before the open brace '{'\n" . $herecurr);
2668                 }
2669
2670 # closing brace should have a space following it when it has anything
2671 # on the line
2672                 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2673                         ERROR("SPACING",
2674                               "space required after that close brace '}'\n" . $herecurr);
2675                 }
2676
2677 # check spacing on square brackets
2678                 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2679                         ERROR("SPACING",
2680                               "space prohibited after that open square bracket '['\n" . $herecurr);
2681                 }
2682                 if ($line =~ /\s\]/) {
2683                         ERROR("SPACING",
2684                               "space prohibited before that close square bracket ']'\n" . $herecurr);
2685                 }
2686
2687 # check spacing on parentheses
2688                 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2689                     $line !~ /for\s*\(\s+;/) {
2690                         ERROR("SPACING",
2691                               "space prohibited after that open parenthesis '('\n" . $herecurr);
2692                 }
2693                 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2694                     $line !~ /for\s*\(.*;\s+\)/ &&
2695                     $line !~ /:\s+\)/) {
2696                         ERROR("SPACING",
2697                               "space prohibited before that close parenthesis ')'\n" . $herecurr);
2698                 }
2699
2700 #goto labels aren't indented, allow a single space however
2701                 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2702                    !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2703                         WARN("INDENTED_LABEL",
2704                              "labels should not be indented\n" . $herecurr);
2705                 }
2706
2707 # Return is not a function.
2708                 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2709                         my $spacing = $1;
2710                         my $value = $2;
2711
2712                         # Flatten any parentheses
2713                         $value =~ s/\(/ \(/g;
2714                         $value =~ s/\)/\) /g;
2715                         while ($value =~ s/\[[^\[\]]*\]/1/ ||
2716                                $value !~ /(?:$Ident|-?$Constant)\s*
2717                                              $Compare\s*
2718                                              (?:$Ident|-?$Constant)/x &&
2719                                $value =~ s/\([^\(\)]*\)/1/) {
2720                         }
2721 #print "value<$value>\n";
2722                         if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2723                                 ERROR("RETURN_PARENTHESES",
2724                                       "return is not a function, parentheses are not required\n" . $herecurr);
2725
2726                         } elsif ($spacing !~ /\s+/) {
2727                                 ERROR("SPACING",
2728                                       "space required before the open parenthesis '('\n" . $herecurr);
2729                         }
2730                 }
2731 # Return of what appears to be an errno should normally be -'ve
2732                 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2733                         my $name = $1;
2734                         if ($name ne 'EOF' && $name ne 'ERROR') {
2735                                 WARN("USE_NEGATIVE_ERRNO",
2736                                      "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2737                         }
2738                 }
2739
2740 # Need a space before open parenthesis after if, while etc
2741                 if ($line=~/\b(if|while|for|switch)\(/) {
2742                         ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
2743                 }
2744
2745 # Check for illegal assignment in if conditional -- and check for trailing
2746 # statements after the conditional.
2747                 if ($line =~ /do\s*(?!{)/) {
2748                         ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2749                                 ctx_statement_block($linenr, $realcnt, 0)
2750                                         if (!defined $stat);
2751                         my ($stat_next) = ctx_statement_block($line_nr_next,
2752                                                 $remain_next, $off_next);
2753                         $stat_next =~ s/\n./\n /g;
2754                         ##print "stat<$stat> stat_next<$stat_next>\n";
2755
2756                         if ($stat_next =~ /^\s*while\b/) {
2757                                 # If the statement carries leading newlines,
2758                                 # then count those as offsets.
2759                                 my ($whitespace) =
2760                                         ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2761                                 my $offset =
2762                                         statement_rawlines($whitespace) - 1;
2763
2764                                 $suppress_whiletrailers{$line_nr_next +
2765                                                                 $offset} = 1;
2766                         }
2767                 }
2768                 if (!defined $suppress_whiletrailers{$linenr} &&
2769                     $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2770                         my ($s, $c) = ($stat, $cond);
2771
2772                         if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2773                                 ERROR("ASSIGN_IN_IF",
2774                                       "do not use assignment in if condition\n" . $herecurr);
2775                         }
2776
2777                         # Find out what is on the end of the line after the
2778                         # conditional.
2779                         substr($s, 0, length($c), '');
2780                         $s =~ s/\n.*//g;
2781                         $s =~ s/$;//g;  # Remove any comments
2782                         if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2783                             $c !~ /}\s*while\s*/)
2784                         {
2785                                 # Find out how long the conditional actually is.
2786                                 my @newlines = ($c =~ /\n/gs);
2787                                 my $cond_lines = 1 + $#newlines;
2788                                 my $stat_real = '';
2789
2790                                 $stat_real = raw_line($linenr, $cond_lines)
2791                                                         . "\n" if ($cond_lines);
2792                                 if (defined($stat_real) && $cond_lines > 1) {
2793                                         $stat_real = "[...]\n$stat_real";
2794                                 }
2795
2796                                 ERROR("TRAILING_STATEMENTS",
2797                                       "trailing statements should be on next line\n" . $herecurr . $stat_real);
2798                         }
2799                 }
2800
2801 # Check for bitwise tests written as boolean
2802                 if ($line =~ /
2803                         (?:
2804                                 (?:\[|\(|\&\&|\|\|)
2805                                 \s*0[xX][0-9]+\s*
2806                                 (?:\&\&|\|\|)
2807                         |
2808                                 (?:\&\&|\|\|)
2809                                 \s*0[xX][0-9]+\s*
2810                                 (?:\&\&|\|\||\)|\])
2811                         )/x)
2812                 {
2813                         WARN("HEXADECIMAL_BOOLEAN_TEST",
2814                              "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2815                 }
2816
2817 # if and else should not have general statements after it
2818                 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2819                         my $s = $1;
2820                         $s =~ s/$;//g;  # Remove any comments
2821                         if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2822                                 ERROR("TRAILING_STATEMENTS",
2823                                       "trailing statements should be on next line\n" . $herecurr);
2824                         }
2825                 }
2826 # if should not continue a brace
2827                 if ($line =~ /}\s*if\b/) {
2828                         ERROR("TRAILING_STATEMENTS",
2829                               "trailing statements should be on next line\n" .
2830                                 $herecurr);
2831                 }
2832 # case and default should not have general statements after them
2833                 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2834                     $line !~ /\G(?:
2835                         (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2836                         \s*return\s+
2837                     )/xg)
2838                 {
2839                         ERROR("TRAILING_STATEMENTS",
2840                               "trailing statements should be on next line\n" . $herecurr);
2841                 }
2842
2843                 # Check for }<nl>else {, these must be at the same
2844                 # indent level to be relevant to each other.
2845                 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2846                                                 $previndent == $indent) {
2847                         ERROR("ELSE_AFTER_BRACE",
2848                               "else should follow close brace '}'\n" . $hereprev);
2849                 }
2850
2851                 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2852                                                 $previndent == $indent) {
2853                         my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2854
2855                         # Find out what is on the end of the line after the
2856                         # conditional.
2857                         substr($s, 0, length($c), '');
2858                         $s =~ s/\n.*//g;
2859
2860                         if ($s =~ /^\s*;/) {
2861                                 ERROR("WHILE_AFTER_BRACE",
2862                                       "while should follow close brace '}'\n" . $hereprev);
2863                         }
2864                 }
2865
2866 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2867 #               if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2868 #                   print "No studly caps, use _\n";
2869 #                   print "$herecurr";
2870 #                   $clean = 0;
2871 #               }
2872
2873 #no spaces allowed after \ in define
2874                 if ($line=~/\#\s*define.*\\\s$/) {
2875                         WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2876                              "Whitepspace after \\ makes next lines useless\n" . $herecurr);
2877                 }
2878
2879 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2880                 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2881                         my $file = "$1.h";
2882                         my $checkfile = "include/linux/$file";
2883                         if (-f "$root/$checkfile" &&
2884                             $realfile ne $checkfile &&
2885                             $1 !~ /$allowed_asm_includes/)
2886                         {
2887                                 if ($realfile =~ m{^arch/}) {
2888                                         CHK("ARCH_INCLUDE_LINUX",
2889                                             "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2890                                 } else {
2891                                         WARN("INCLUDE_LINUX",
2892                                              "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2893                                 }
2894                         }
2895                 }
2896
2897 # multi-statement macros should be enclosed in a do while loop, grab the
2898 # first statement and ensure its the whole macro if its not enclosed
2899 # in a known good container
2900                 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2901                     $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2902                         my $ln = $linenr;
2903                         my $cnt = $realcnt;
2904                         my ($off, $dstat, $dcond, $rest);
2905                         my $ctx = '';
2906                         ($dstat, $dcond, $ln, $cnt, $off) =
2907                                 ctx_statement_block($linenr, $realcnt, 0);
2908                         $ctx = $dstat;
2909                         #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2910                         #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2911
2912                         $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
2913                         $dstat =~ s/$;//g;
2914                         $dstat =~ s/\\\n.//g;
2915                         $dstat =~ s/^\s*//s;
2916                         $dstat =~ s/\s*$//s;
2917
2918                         # Flatten any parentheses and braces
2919                         while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2920                                $dstat =~ s/\{[^\{\}]*\}/1/ ||
2921                                $dstat =~ s/\[[^\[\]]*\]/1/)
2922                         {
2923                         }
2924
2925                         my $exceptions = qr{
2926                                 $Declare|
2927                                 module_param_named|
2928                                 MODULE_PARAM_DESC|
2929                                 DECLARE_PER_CPU|
2930                                 DEFINE_PER_CPU|
2931                                 __typeof__\(|
2932                                 union|
2933                                 struct|
2934                                 \.$Ident\s*=\s*|
2935                                 ^\"|\"$
2936                         }x;
2937                         #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2938                         if ($dstat ne '' &&
2939                             $dstat !~ /^(?:$Ident|-?$Constant),$/ &&                    # 10, // foo(),
2940                             $dstat !~ /^(?:$Ident|-?$Constant);$/ &&                    # foo();
2941                             $dstat !~ /^(?:$Ident|-?$Constant)$/ &&                     # 10 // foo()
2942                             $dstat !~ /$exceptions/ &&
2943                             $dstat !~ /^\.$Ident\s*=/ &&                                # .foo =
2944                             $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&       # do {...} while (...); // do {...} while (...)
2945                             $dstat !~ /^for\s*$Constant$/ &&                            # for (...)
2946                             $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&   # for (...) bar()
2947                             $dstat !~ /^do\s*{/ &&                                      # do {...
2948                             $dstat !~ /^\({/)                                           # ({...
2949                         {
2950                                 $ctx =~ s/\n*$//;
2951                                 my $herectx = $here . "\n";
2952                                 my $cnt = statement_rawlines($ctx);
2953
2954                                 for (my $n = 0; $n < $cnt; $n++) {
2955                                         $herectx .= raw_line($linenr, $n) . "\n";
2956                                 }
2957
2958                                 if ($dstat =~ /;/) {
2959                                         ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
2960                                               "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
2961                                 } else {
2962                                         ERROR("COMPLEX_MACRO",
2963                                               "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
2964                                 }
2965                         }
2966                 }
2967
2968 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2969 # all assignments may have only one of the following with an assignment:
2970 #       .
2971 #       ALIGN(...)
2972 #       VMLINUX_SYMBOL(...)
2973                 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2974                         WARN("MISSING_VMLINUX_SYMBOL",
2975                              "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2976                 }
2977
2978 # check for redundant bracing round if etc
2979                 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2980                         my ($level, $endln, @chunks) =
2981                                 ctx_statement_full($linenr, $realcnt, 1);
2982                         #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2983                         #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2984                         if ($#chunks > 0 && $level == 0) {
2985                                 my $allowed = 0;
2986                                 my $seen = 0;
2987                                 my $herectx = $here . "\n";
2988                                 my $ln = $linenr - 1;
2989                                 for my $chunk (@chunks) {
2990                                         my ($cond, $block) = @{$chunk};
2991
2992                                         # If the condition carries leading newlines, then count those as offsets.
2993                                         my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2994                                         my $offset = statement_rawlines($whitespace) - 1;
2995
2996                                         #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2997
2998                                         # We have looked at and allowed this specific line.
2999                                         $suppress_ifbraces{$ln + $offset} = 1;
3000
3001                                         $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3002                                         $ln += statement_rawlines($block) - 1;
3003
3004                                         substr($block, 0, length($cond), '');
3005
3006                                         $seen++ if ($block =~ /^\s*{/);
3007
3008                                         #print "cond<$cond> block<$block> allowed<$allowed>\n";
3009                                         if (statement_lines($cond) > 1) {
3010                                                 #print "APW: ALLOWED: cond<$cond>\n";
3011                                                 $allowed = 1;
3012                                         }
3013                                         if ($block =~/\b(?:if|for|while)\b/) {
3014                                                 #print "APW: ALLOWED: block<$block>\n";
3015                                                 $allowed = 1;
3016                                         }
3017                                         if (statement_block_size($block) > 1) {
3018                                                 #print "APW: ALLOWED: lines block<$block>\n";
3019                                                 $allowed = 1;
3020                                         }
3021                                 }
3022                                 if ($seen && !$allowed) {
3023                                         WARN("BRACES",
3024                                              "braces {} are not necessary for any arm of this statement\n" . $herectx);
3025                                 }
3026                         }
3027                 }
3028                 if (!defined $suppress_ifbraces{$linenr - 1} &&
3029                                         $line =~ /\b(if|while|for|else)\b/) {
3030                         my $allowed = 0;
3031
3032                         # Check the pre-context.
3033                         if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3034                                 #print "APW: ALLOWED: pre<$1>\n";
3035                                 $allowed = 1;
3036                         }
3037
3038                         my ($level, $endln, @chunks) =
3039                                 ctx_statement_full($linenr, $realcnt, $-[0]);
3040
3041                         # Check the condition.
3042                         my ($cond, $block) = @{$chunks[0]};
3043                         #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3044                         if (defined $cond) {
3045                                 substr($block, 0, length($cond), '');
3046                         }
3047                         if (statement_lines($cond) > 1) {
3048                                 #print "APW: ALLOWED: cond<$cond>\n";
3049                                 $allowed = 1;
3050                         }
3051                         if ($block =~/\b(?:if|for|while)\b/) {
3052                                 #print "APW: ALLOWED: block<$block>\n";
3053                                 $allowed = 1;
3054                         }
3055                         if (statement_block_size($block) > 1) {
3056                                 #print "APW: ALLOWED: lines block<$block>\n";
3057                                 $allowed = 1;
3058                         }
3059                         # Check the post-context.
3060                         if (defined $chunks[1]) {
3061                                 my ($cond, $block) = @{$chunks[1]};
3062                                 if (defined $cond) {
3063                                         substr($block, 0, length($cond), '');
3064                                 }
3065                                 if ($block =~ /^\s*\{/) {
3066                                         #print "APW: ALLOWED: chunk-1 block<$block>\n";
3067                                         $allowed = 1;
3068                                 }
3069                         }
3070                         if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
3071                                 my $herectx = $here . "\n";
3072                                 my $cnt = statement_rawlines($block);
3073
3074                                 for (my $n = 0; $n < $cnt; $n++) {
3075                                         $herectx .= raw_line($linenr, $n) . "\n";
3076                                 }
3077
3078                                 WARN("BRACES",
3079                                      "braces {} are not necessary for single statement blocks\n" . $herectx);
3080                         }
3081                 }
3082
3083 # don't include deprecated include files (uses RAW line)
3084                 for my $inc (keys %dep_includes) {
3085                         if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
3086                                 ERROR("DEPRECATED_INCLUDE",
3087                                       "Don't use <$inc>, include " .
3088                                       "$dep_includes{$inc} instead\n" .
3089                                       $herecurr);
3090                         }
3091                 }
3092
3093 # don't use deprecated functions
3094                 for my $func (keys %dep_functions) {
3095                         if ($line =~ /\b$func\b/) {
3096                                 ERROR("DEPRECATED_FUNCTION",
3097                                       "$func is deprecated, " .
3098                                       "use $dep_functions{$func} instead\n" .
3099                                       $herecurr);
3100                         }
3101                 }
3102
3103 # try to replace assertions with error handling
3104                 if ($line =~ /\bLASSERTF?\s*\(/) {
3105                     WARN("LASSERT",
3106                          "try to replace assertions with error handling\n" .
3107                          $herecurr);
3108                 }
3109
3110 # avoid new console messages
3111                 if ($line =~ /\bLCONSOLE[A-Z_]*\s*\(/) {
3112                     WARN("LCONSOLE",
3113                          "avoid adding new console messages\n" .
3114                          $herecurr);
3115                 }
3116
3117 # minimize new CERROR messages
3118                 if ($line =~ /\bC(EMERG|ERROR|NETERR|WARN)\s*\(/) {
3119                     WARN("LCONSOLE",
3120                          "think hard before adding new CERROR messages\n" .
3121                          $herecurr);
3122                 }
3123
3124 # no volatiles please
3125                 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3126                 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3127                         WARN("VOLATILE",
3128                              "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
3129                 }
3130
3131 # warn about #if 0
3132                 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3133                         CHK("REDUNDANT_CODE",
3134                             "if this code is redundant consider removing it\n" .
3135                                 $herecurr);
3136                 }
3137
3138 # check for needless kfree() checks
3139                 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3140                         my $expr = $1;
3141                         if ($line =~ /\bkfree\(\Q$expr\E\);/) {
3142                                 WARN("NEEDLESS_KFREE",
3143                                      "kfree(NULL) is safe this check is probably not required\n" . $hereprev);
3144                         }
3145                 }
3146 # check for needless usb_free_urb() checks
3147                 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3148                         my $expr = $1;
3149                         if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
3150                                 WARN("NEEDLESS_USB_FREE_URB",
3151                                      "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
3152                         }
3153                 }
3154
3155 # prefer usleep_range over udelay
3156                 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
3157                         # ignore udelay's < 10, however
3158                         if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
3159                                 CHK("USLEEP_RANGE",
3160                                     "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
3161                         }
3162                 }
3163
3164 # warn about unexpectedly long msleep's
3165                 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3166                         if ($1 < 20) {
3167                                 WARN("MSLEEP",
3168                                      "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
3169                         }
3170                 }
3171
3172 # warn about #ifdefs in C files
3173 #               if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3174 #                       print "#ifdef in C files should be avoided\n";
3175 #                       print "$herecurr";
3176 #                       $clean = 0;
3177 #               }
3178
3179 # warn about spacing in #ifdefs
3180                 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3181                         ERROR("SPACING",
3182                               "exactly one space required after that #$1\n" . $herecurr);
3183                 }
3184
3185 # check for spinlock_t definitions without a comment.
3186                 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3187                     $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
3188                         my $which = $1;
3189                         if (!ctx_has_comment($first_line, $linenr)) {
3190                                 CHK("UNCOMMENTED_DEFINITION",
3191                                     "$1 definition without comment\n" . $herecurr);
3192                         }
3193                 }
3194 # check for memory barriers without a comment.
3195                 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3196                         if (!ctx_has_comment($first_line, $linenr)) {
3197                                 CHK("MEMORY_BARRIER",
3198                                     "memory barrier without comment\n" . $herecurr);
3199                         }
3200                 }
3201 # check of hardware specific defines
3202                 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3203                         CHK("ARCH_DEFINES",
3204                             "architecture specific defines should be avoided\n" .  $herecurr);
3205                 }
3206
3207 # Check that the storage class is at the beginning of a declaration
3208                 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3209                         WARN("STORAGE_CLASS",
3210                              "storage class should be at the beginning of the declaration\n" . $herecurr)
3211                 }
3212
3213 # check the location of the inline attribute, that it is between
3214 # storage class and type.
3215                 if ($line =~ /\b$Type\s+$Inline\b/ ||
3216                     $line =~ /\b$Inline\s+$Storage\b/) {
3217                         ERROR("INLINE_LOCATION",
3218                               "inline keyword should sit between storage class and type\n" . $herecurr);
3219                 }
3220
3221 # Check for __inline__ and __inline, prefer inline
3222                 if ($line =~ /\b(__inline__|__inline)\b/) {
3223                         WARN("INLINE",
3224                              "plain inline is preferred over $1\n" . $herecurr);
3225                 }
3226
3227 # Check for __attribute__ packed, prefer __packed
3228                 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3229                         WARN("PREFER_PACKED",
3230                              "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3231                 }
3232
3233 # Check for __attribute__ aligned, prefer __aligned
3234                 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3235                         WARN("PREFER_ALIGNED",
3236                              "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3237                 }
3238
3239 # Check for __attribute__ format(printf, prefer __printf
3240                 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3241                         WARN("PREFER_PRINTF",
3242                              "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3243                 }
3244
3245 # check for sizeof(&)
3246                 if ($line =~ /\bsizeof\s*\(\s*\&/) {
3247                         WARN("SIZEOF_ADDRESS",
3248                              "sizeof(& should be avoided\n" . $herecurr);
3249                 }
3250
3251 # check for line continuations in quoted strings with odd counts of "
3252                 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3253                         WARN("LINE_CONTINUATIONS",
3254                              "Avoid line continuations in quoted strings\n" . $herecurr);
3255                 }
3256
3257 # Check for misused memsets
3258                 if (defined $stat &&
3259                     $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3260
3261                         my $ms_addr = $2;
3262                         my $ms_val = $8;
3263                         my $ms_size = $14;
3264
3265                         if ($ms_size =~ /^(0x|)0$/i) {
3266                                 ERROR("MEMSET",
3267                                       "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3268                         } elsif ($ms_size =~ /^(0x|)1$/i) {
3269                                 WARN("MEMSET",
3270                                      "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3271                         }
3272                 }
3273
3274 # typecasts on min/max could be min_t/max_t
3275                 if (defined $stat &&
3276                     $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3277                         if (defined $2 || defined $8) {
3278                                 my $call = $1;
3279                                 my $cast1 = deparenthesize($2);
3280                                 my $arg1 = $3;
3281                                 my $cast2 = deparenthesize($8);
3282                                 my $arg2 = $9;
3283                                 my $cast;
3284
3285                                 if ($cast1 ne "" && $cast2 ne "") {
3286                                         $cast = "$cast1 or $cast2";
3287                                 } elsif ($cast1 ne "") {
3288                                         $cast = $cast1;
3289                                 } else {
3290                                         $cast = $cast2;
3291                                 }
3292                                 WARN("MINMAX",
3293                                      "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3294                         }
3295                 }
3296
3297 # check for new externs in .c files.
3298                 if ($realfile =~ /\.c$/ && defined $stat &&
3299                     $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3300                 {
3301                         my $function_name = $1;
3302                         my $paren_space = $2;
3303
3304                         my $s = $stat;
3305                         if (defined $cond) {
3306                                 substr($s, 0, length($cond), '');
3307                         }
3308                         if ($s =~ /^\s*;/ &&
3309                             $function_name ne 'uninitialized_var')
3310                         {
3311                                 WARN("AVOID_EXTERNS",
3312                                      "externs should be avoided in .c files\n" .  $herecurr);
3313                         }
3314
3315                         if ($paren_space =~ /\n/) {
3316                                 WARN("FUNCTION_ARGUMENTS",
3317                                      "arguments for function declarations should follow identifier\n" . $herecurr);
3318                         }
3319
3320                 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3321                     $stat =~ /^.\s*extern\s+/)
3322                 {
3323                         WARN("AVOID_EXTERNS",
3324                              "externs should be avoided in .c files\n" .  $herecurr);
3325                 }
3326
3327 # checks for new __setup's
3328                 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3329                         my $name = $1;
3330
3331                         if (!grep(/$name/, @setup_docs)) {
3332                                 CHK("UNDOCUMENTED_SETUP",
3333                                     "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3334                         }
3335                 }
3336
3337 # check for pointless casting of kmalloc return
3338                 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3339                         WARN("UNNECESSARY_CASTS",
3340                              "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
3341                 }
3342
3343 # check for multiple semicolons
3344                 if ($line =~ /;\s*;\s*$/) {
3345                     WARN("ONE_SEMICOLON",
3346                          "Statements terminations use 1 semicolon\n" . $herecurr);
3347                 }
3348
3349 # check for gcc specific __FUNCTION__
3350                 if ($line =~ /__FUNCTION__/) {
3351                         WARN("USE_FUNC",
3352                              "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
3353                 }
3354
3355 # check for semaphores initialized locked
3356                 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3357                         WARN("CONSIDER_COMPLETION",
3358                              "consider using a completion\n" . $herecurr);
3359
3360                 }
3361 # recommend kstrto* over simple_strto* and strict_strto*
3362                 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3363                         WARN("CONSIDER_KSTRTO",
3364                              "$1 is obsolete, use k$3 instead\n" . $herecurr);
3365                 }
3366 # check for __initcall(), use device_initcall() explicitly please
3367                 if ($line =~ /^.\s*__initcall\s*\(/) {
3368                         WARN("USE_DEVICE_INITCALL",
3369                              "please use device_initcall() instead of __initcall()\n" . $herecurr);
3370                 }
3371 # check for various ops structs, ensure they are const.
3372                 my $struct_ops = qr{acpi_dock_ops|
3373                                 address_space_operations|
3374                                 backlight_ops|
3375                                 block_device_operations|
3376                                 dentry_operations|
3377                                 dev_pm_ops|
3378                                 dma_map_ops|
3379                                 extent_io_ops|
3380                                 file_lock_operations|
3381                                 file_operations|
3382                                 hv_ops|
3383                                 ide_dma_ops|
3384                                 intel_dvo_dev_ops|
3385                                 item_operations|
3386                                 iwl_ops|
3387                                 kgdb_arch|
3388                                 kgdb_io|
3389                                 kset_uevent_ops|
3390                                 lock_manager_operations|
3391                                 microcode_ops|
3392                                 mtrr_ops|
3393                                 neigh_ops|
3394                                 nlmsvc_binding|
3395                                 pci_raw_ops|
3396                                 pipe_buf_operations|
3397                                 platform_hibernation_ops|
3398                                 platform_suspend_ops|
3399                                 proto_ops|
3400                                 rpc_pipe_ops|
3401                                 seq_operations|
3402                                 snd_ac97_build_ops|
3403                                 soc_pcmcia_socket_ops|
3404                                 stacktrace_ops|
3405                                 sysfs_ops|
3406                                 tty_operations|
3407                                 usb_mon_operations|
3408                                 wd_ops}x;
3409                 if ($line !~ /\bconst\b/ &&
3410                     $line =~ /\bstruct\s+($struct_ops)\b/) {
3411                         WARN("CONST_STRUCT",
3412                              "struct $1 should normally be const\n" .
3413                                 $herecurr);
3414                 }
3415
3416 # use of NR_CPUS is usually wrong
3417 # ignore definitions of NR_CPUS and usage to define arrays as likely right
3418                 if ($line =~ /\bNR_CPUS\b/ &&
3419                     $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3420                     $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3421                     $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3422                     $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3423                     $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3424                 {
3425                         WARN("NR_CPUS",
3426                              "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3427                 }
3428
3429 # check for %L{u,d,i} in strings
3430                 my $string;
3431                 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3432                         $string = substr($rawline, $-[1], $+[1] - $-[1]);
3433                         $string =~ s/%%/__/g;
3434                         if ($string =~ /(?<!%)%L[udi]/) {
3435                                 WARN("PRINTF_L",
3436                                      "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3437                                 last;
3438                         }
3439                 }
3440
3441 # whine mightly about in_atomic
3442                 if ($line =~ /\bin_atomic\s*\(/) {
3443                         if ($realfile =~ m@^drivers/@) {
3444                                 ERROR("IN_ATOMIC",
3445                                       "do not use in_atomic in drivers\n" . $herecurr);
3446                         } elsif ($realfile !~ m@^kernel/@) {
3447                                 WARN("IN_ATOMIC",
3448                                      "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3449                         }
3450                 }
3451
3452 # check for lockdep_set_novalidate_class
3453                 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3454                     $line =~ /__lockdep_no_validate__\s*\)/ ) {
3455                         if ($realfile !~ m@^kernel/lockdep@ &&
3456                             $realfile !~ m@^include/linux/lockdep@ &&
3457                             $realfile !~ m@^drivers/base/core@) {
3458                                 ERROR("LOCKDEP",
3459                                       "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3460                         }
3461                 }
3462
3463                 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3464                     $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3465                         WARN("EXPORTED_WORLD_WRITABLE",
3466                              "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3467                 }
3468
3469                 if ($rawline =~ /version 3/) {
3470                         WARN("GPLV3",
3471                              "using GPLv3 is usually wrong\n" . $herecurr);
3472                 }
3473         }
3474
3475         # If we have no input at all, then there is nothing to report on
3476         # so just keep quiet.
3477         if ($#rawlines == -1) {
3478                 exit(0);
3479         }
3480
3481         # In mailback mode only produce a report in the negative, for
3482         # things that appear to be patches.
3483         if ($mailback && ($clean == 1 || !$is_patch)) {
3484                 exit(0);
3485         }
3486
3487         # This is not a patch, and we are are in 'no-patch' mode so
3488         # just keep quiet.
3489         if (!$chk_patch && !$is_patch) {
3490                 exit(0);
3491         }
3492
3493         if (!$is_patch) {
3494                 ERROR("NOT_UNIFIED_DIFF",
3495                       "Does not appear to be a unified-diff format patch\n");
3496         }
3497         if ($is_patch && $chk_signoff && $signoff == 0) {
3498                 ERROR("MISSING_SIGN_OFF",
3499                       "Missing Signed-off-by: line(s)\n");
3500         }
3501
3502         print report_dump();
3503         if ($summary && !($clean == 1 && $quiet == 1)) {
3504                 print "$filename " if ($summary_file);
3505                 print "total: $cnt_error errors, $cnt_warn warnings, " .
3506                         (($check)? "$cnt_chk checks, " : "") .
3507                         "$cnt_lines lines checked\n";
3508                 print "\n" if ($quiet == 0);
3509         }
3510
3511         if ($quiet == 0) {
3512                 # If there were whitespace errors which cleanpatch can fix
3513                 # then suggest that.
3514                 if ($rpt_cleaners) {
3515                         print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3516                         print "      scripts/cleanfile\n\n";
3517                         $rpt_cleaners = 0;
3518                 }
3519         }
3520
3521         if (keys %ignore_type) {
3522             print "NOTE: Ignored message types:";
3523             foreach my $ignore (sort keys %ignore_type) {
3524                 print " $ignore";
3525             }
3526             print "\n";
3527             print "\n" if ($quiet == 0);
3528         }
3529
3530         if ($clean == 1 && $quiet == 0) {
3531                 print "$vname has no obvious style problems and is ready for submission.\n"
3532         }
3533         if ($clean == 0 && $quiet == 0) {
3534                 print << "EOM";
3535 $vname has style problems, please review.
3536
3537 If any of these errors are false positives, please report
3538 them to the maintainer, see CHECKPATCH in MAINTAINERS.
3539 EOM
3540         }
3541
3542         return $clean;
3543 }