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