checkpatch: look for common misspellings
[firefly-linux-kernel-4.4.55.git] / 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 use strict;
9 use POSIX;
10
11 my $P = $0;
12 $P =~ s@(.*)/@@g;
13 my $D = $1;
14
15 my $V = '0.32';
16
17 use Getopt::Long qw(:config no_auto_abbrev);
18
19 my $quiet = 0;
20 my $tree = 1;
21 my $chk_signoff = 1;
22 my $chk_patch = 1;
23 my $tst_only;
24 my $emacs = 0;
25 my $terse = 0;
26 my $file = 0;
27 my $check = 0;
28 my $check_orig = 0;
29 my $summary = 1;
30 my $mailback = 0;
31 my $summary_file = 0;
32 my $show_types = 0;
33 my $fix = 0;
34 my $fix_inplace = 0;
35 my $root;
36 my %debug;
37 my %camelcase = ();
38 my %use_type = ();
39 my @use = ();
40 my %ignore_type = ();
41 my @ignore = ();
42 my $help = 0;
43 my $configuration_file = ".checkpatch.conf";
44 my $max_line_length = 80;
45 my $ignore_perl_version = 0;
46 my $minimum_perl_version = 5.10.0;
47 my $min_conf_desc_length = 4;
48 my $spelling_file = "$D/spelling.txt";
49
50 sub help {
51         my ($exitcode) = @_;
52
53         print << "EOM";
54 Usage: $P [OPTION]... [FILE]...
55 Version: $V
56
57 Options:
58   -q, --quiet                quiet
59   --no-tree                  run without a kernel tree
60   --no-signoff               do not check for 'Signed-off-by' line
61   --patch                    treat FILE as patchfile (default)
62   --emacs                    emacs compile window format
63   --terse                    one line per report
64   -f, --file                 treat FILE as regular source file
65   --subjective, --strict     enable more subjective tests
66   --types TYPE(,TYPE2...)    show only these comma separated message types
67   --ignore TYPE(,TYPE2...)   ignore various comma separated message types
68   --max-line-length=n        set the maximum line length, if exceeded, warn
69   --min-conf-desc-length=n   set the min description length, if shorter, warn
70   --show-types               show the message "types" in the output
71   --root=PATH                PATH to the kernel tree root
72   --no-summary               suppress the per-file summary
73   --mailback                 only produce a report in case of warnings/errors
74   --summary-file             include the filename in summary
75   --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
76                              'values', 'possible', 'type', and 'attr' (default
77                              is all off)
78   --test-only=WORD           report only warnings/errors containing WORD
79                              literally
80   --fix                      EXPERIMENTAL - may create horrible results
81                              If correctable single-line errors exist, create
82                              "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
83                              with potential errors corrected to the preferred
84                              checkpatch style
85   --fix-inplace              EXPERIMENTAL - may create horrible results
86                              Is the same as --fix, but overwrites the input
87                              file.  It's your fault if there's no backup or git
88   --ignore-perl-version      override checking of perl version.  expect
89                              runtime errors.
90   -h, --help, --version      display this help and exit
91
92 When FILE is - read standard input.
93 EOM
94
95         exit($exitcode);
96 }
97
98 my $conf = which_conf($configuration_file);
99 if (-f $conf) {
100         my @conf_args;
101         open(my $conffile, '<', "$conf")
102             or warn "$P: Can't find a readable $configuration_file file $!\n";
103
104         while (<$conffile>) {
105                 my $line = $_;
106
107                 $line =~ s/\s*\n?$//g;
108                 $line =~ s/^\s*//g;
109                 $line =~ s/\s+/ /g;
110
111                 next if ($line =~ m/^\s*#/);
112                 next if ($line =~ m/^\s*$/);
113
114                 my @words = split(" ", $line);
115                 foreach my $word (@words) {
116                         last if ($word =~ m/^#/);
117                         push (@conf_args, $word);
118                 }
119         }
120         close($conffile);
121         unshift(@ARGV, @conf_args) if @conf_args;
122 }
123
124 GetOptions(
125         'q|quiet+'      => \$quiet,
126         'tree!'         => \$tree,
127         'signoff!'      => \$chk_signoff,
128         'patch!'        => \$chk_patch,
129         'emacs!'        => \$emacs,
130         'terse!'        => \$terse,
131         'f|file!'       => \$file,
132         'subjective!'   => \$check,
133         'strict!'       => \$check,
134         'ignore=s'      => \@ignore,
135         'types=s'       => \@use,
136         'show-types!'   => \$show_types,
137         'max-line-length=i' => \$max_line_length,
138         'min-conf-desc-length=i' => \$min_conf_desc_length,
139         'root=s'        => \$root,
140         'summary!'      => \$summary,
141         'mailback!'     => \$mailback,
142         'summary-file!' => \$summary_file,
143         'fix!'          => \$fix,
144         'fix-inplace!'  => \$fix_inplace,
145         'ignore-perl-version!' => \$ignore_perl_version,
146         'debug=s'       => \%debug,
147         'test-only=s'   => \$tst_only,
148         'h|help'        => \$help,
149         'version'       => \$help
150 ) or help(1);
151
152 help(0) if ($help);
153
154 $fix = 1 if ($fix_inplace);
155 $check_orig = $check;
156
157 my $exit = 0;
158
159 if ($^V && $^V lt $minimum_perl_version) {
160         printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
161         if (!$ignore_perl_version) {
162                 exit(1);
163         }
164 }
165
166 if ($#ARGV < 0) {
167         print "$P: no input files\n";
168         exit(1);
169 }
170
171 sub hash_save_array_words {
172         my ($hashRef, $arrayRef) = @_;
173
174         my @array = split(/,/, join(',', @$arrayRef));
175         foreach my $word (@array) {
176                 $word =~ s/\s*\n?$//g;
177                 $word =~ s/^\s*//g;
178                 $word =~ s/\s+/ /g;
179                 $word =~ tr/[a-z]/[A-Z]/;
180
181                 next if ($word =~ m/^\s*#/);
182                 next if ($word =~ m/^\s*$/);
183
184                 $hashRef->{$word}++;
185         }
186 }
187
188 sub hash_show_words {
189         my ($hashRef, $prefix) = @_;
190
191         if ($quiet == 0 && keys %$hashRef) {
192                 print "NOTE: $prefix message types:";
193                 foreach my $word (sort keys %$hashRef) {
194                         print " $word";
195                 }
196                 print "\n\n";
197         }
198 }
199
200 hash_save_array_words(\%ignore_type, \@ignore);
201 hash_save_array_words(\%use_type, \@use);
202
203 my $dbg_values = 0;
204 my $dbg_possible = 0;
205 my $dbg_type = 0;
206 my $dbg_attr = 0;
207 for my $key (keys %debug) {
208         ## no critic
209         eval "\${dbg_$key} = '$debug{$key}';";
210         die "$@" if ($@);
211 }
212
213 my $rpt_cleaners = 0;
214
215 if ($terse) {
216         $emacs = 1;
217         $quiet++;
218 }
219
220 if ($tree) {
221         if (defined $root) {
222                 if (!top_of_kernel_tree($root)) {
223                         die "$P: $root: --root does not point at a valid tree\n";
224                 }
225         } else {
226                 if (top_of_kernel_tree('.')) {
227                         $root = '.';
228                 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
229                                                 top_of_kernel_tree($1)) {
230                         $root = $1;
231                 }
232         }
233
234         if (!defined $root) {
235                 print "Must be run from the top-level dir. of a kernel tree\n";
236                 exit(2);
237         }
238 }
239
240 my $emitted_corrupt = 0;
241
242 our $Ident      = qr{
243                         [A-Za-z_][A-Za-z\d_]*
244                         (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
245                 }x;
246 our $Storage    = qr{extern|static|asmlinkage};
247 our $Sparse     = qr{
248                         __user|
249                         __kernel|
250                         __force|
251                         __iomem|
252                         __must_check|
253                         __init_refok|
254                         __kprobes|
255                         __ref|
256                         __rcu
257                 }x;
258 our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
259 our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
260 our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
261 our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
262 our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
263
264 # Notes to $Attribute:
265 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
266 our $Attribute  = qr{
267                         const|
268                         __percpu|
269                         __nocast|
270                         __safe|
271                         __bitwise__|
272                         __packed__|
273                         __packed2__|
274                         __naked|
275                         __maybe_unused|
276                         __always_unused|
277                         __noreturn|
278                         __used|
279                         __cold|
280                         __noclone|
281                         __deprecated|
282                         __read_mostly|
283                         __kprobes|
284                         $InitAttribute|
285                         ____cacheline_aligned|
286                         ____cacheline_aligned_in_smp|
287                         ____cacheline_internodealigned_in_smp|
288                         __weak
289                   }x;
290 our $Modifier;
291 our $Inline     = qr{inline|__always_inline|noinline|__inline|__inline__};
292 our $Member     = qr{->$Ident|\.$Ident|\[[^]]*\]};
293 our $Lval       = qr{$Ident(?:$Member)*};
294
295 our $Int_type   = qr{(?i)llu|ull|ll|lu|ul|l|u};
296 our $Binary     = qr{(?i)0b[01]+$Int_type?};
297 our $Hex        = qr{(?i)0x[0-9a-f]+$Int_type?};
298 our $Int        = qr{[0-9]+$Int_type?};
299 our $Octal      = qr{0[0-7]+$Int_type?};
300 our $Float_hex  = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
301 our $Float_dec  = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
302 our $Float_int  = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
303 our $Float      = qr{$Float_hex|$Float_dec|$Float_int};
304 our $Constant   = qr{$Float|$Binary|$Octal|$Hex|$Int};
305 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
306 our $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
307 our $Arithmetic = qr{\+|-|\*|\/|%};
308 our $Operators  = qr{
309                         <=|>=|==|!=|
310                         =>|->|<<|>>|<|>|!|~|
311                         &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
312                   }x;
313
314 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
315
316 our $NonptrType;
317 our $NonptrTypeMisordered;
318 our $NonptrTypeWithAttr;
319 our $Type;
320 our $TypeMisordered;
321 our $Declare;
322 our $DeclareMisordered;
323
324 our $NON_ASCII_UTF8     = qr{
325         [\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
326         |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
327         | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
328         |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
329         |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
330         | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
331         |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
332 }x;
333
334 our $UTF8       = qr{
335         [\x09\x0A\x0D\x20-\x7E]              # ASCII
336         | $NON_ASCII_UTF8
337 }x;
338
339 our $typeTypedefs = qr{(?x:
340         (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
341         atomic_t
342 )};
343
344 our $logFunctions = qr{(?x:
345         printk(?:_ratelimited|_once|)|
346         (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
347         WARN(?:_RATELIMIT|_ONCE|)|
348         panic|
349         MODULE_[A-Z_]+|
350         seq_vprintf|seq_printf|seq_puts
351 )};
352
353 our $signature_tags = qr{(?xi:
354         Signed-off-by:|
355         Acked-by:|
356         Tested-by:|
357         Reviewed-by:|
358         Reported-by:|
359         Suggested-by:|
360         To:|
361         Cc:
362 )};
363
364 our @typeListMisordered = (
365         qr{char\s+(?:un)?signed},
366         qr{int\s+(?:(?:un)?signed\s+)?short\s},
367         qr{int\s+short(?:\s+(?:un)?signed)},
368         qr{short\s+int(?:\s+(?:un)?signed)},
369         qr{(?:un)?signed\s+int\s+short},
370         qr{short\s+(?:un)?signed},
371         qr{long\s+int\s+(?:un)?signed},
372         qr{int\s+long\s+(?:un)?signed},
373         qr{long\s+(?:un)?signed\s+int},
374         qr{int\s+(?:un)?signed\s+long},
375         qr{int\s+(?:un)?signed},
376         qr{int\s+long\s+long\s+(?:un)?signed},
377         qr{long\s+long\s+int\s+(?:un)?signed},
378         qr{long\s+long\s+(?:un)?signed\s+int},
379         qr{long\s+long\s+(?:un)?signed},
380         qr{long\s+(?:un)?signed},
381 );
382
383 our @typeList = (
384         qr{void},
385         qr{(?:(?:un)?signed\s+)?char},
386         qr{(?:(?:un)?signed\s+)?short\s+int},
387         qr{(?:(?:un)?signed\s+)?short},
388         qr{(?:(?:un)?signed\s+)?int},
389         qr{(?:(?:un)?signed\s+)?long\s+int},
390         qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
391         qr{(?:(?:un)?signed\s+)?long\s+long},
392         qr{(?:(?:un)?signed\s+)?long},
393         qr{(?:un)?signed},
394         qr{float},
395         qr{double},
396         qr{bool},
397         qr{struct\s+$Ident},
398         qr{union\s+$Ident},
399         qr{enum\s+$Ident},
400         qr{${Ident}_t},
401         qr{${Ident}_handler},
402         qr{${Ident}_handler_fn},
403         @typeListMisordered,
404 );
405 our @typeListWithAttr = (
406         @typeList,
407         qr{struct\s+$InitAttribute\s+$Ident},
408         qr{union\s+$InitAttribute\s+$Ident},
409 );
410
411 our @modifierList = (
412         qr{fastcall},
413 );
414
415 our @mode_permission_funcs = (
416         ["module_param", 3],
417         ["module_param_(?:array|named|string)", 4],
418         ["module_param_array_named", 5],
419         ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
420         ["proc_create(?:_data|)", 2],
421         ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
422 );
423
424 #Create a search pattern for all these functions to speed up a loop below
425 our $mode_perms_search = "";
426 foreach my $entry (@mode_permission_funcs) {
427         $mode_perms_search .= '|' if ($mode_perms_search ne "");
428         $mode_perms_search .= $entry->[0];
429 }
430
431 our $allowed_asm_includes = qr{(?x:
432         irq|
433         memory|
434         time|
435         reboot
436 )};
437 # memory.h: ARM has a custom one
438
439 # Load common spelling mistakes and build regular expression list.
440 my $misspellings;
441 my @spelling_list;
442 my %spelling_fix;
443 open(my $spelling, '<', $spelling_file)
444     or die "$P: Can't open $spelling_file for reading: $!\n";
445 while (<$spelling>) {
446         my $line = $_;
447
448         $line =~ s/\s*\n?$//g;
449         $line =~ s/^\s*//g;
450
451         next if ($line =~ m/^\s*#/);
452         next if ($line =~ m/^\s*$/);
453
454         my ($suspect, $fix) = split(/\|\|/, $line);
455
456         push(@spelling_list, $suspect);
457         $spelling_fix{$suspect} = $fix;
458 }
459 close($spelling);
460 $misspellings = join("|", @spelling_list);
461
462 sub build_types {
463         my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
464         my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
465         my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
466         my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
467         $Modifier       = qr{(?:$Attribute|$Sparse|$mods)};
468         $NonptrType     = qr{
469                         (?:$Modifier\s+|const\s+)*
470                         (?:
471                                 (?:typeof|__typeof__)\s*\([^\)]*\)|
472                                 (?:$typeTypedefs\b)|
473                                 (?:${all}\b)
474                         )
475                         (?:\s+$Modifier|\s+const)*
476                   }x;
477         $NonptrTypeMisordered   = qr{
478                         (?:$Modifier\s+|const\s+)*
479                         (?:
480                                 (?:${Misordered}\b)
481                         )
482                         (?:\s+$Modifier|\s+const)*
483                   }x;
484         $NonptrTypeWithAttr     = qr{
485                         (?:$Modifier\s+|const\s+)*
486                         (?:
487                                 (?:typeof|__typeof__)\s*\([^\)]*\)|
488                                 (?:$typeTypedefs\b)|
489                                 (?:${allWithAttr}\b)
490                         )
491                         (?:\s+$Modifier|\s+const)*
492                   }x;
493         $Type   = qr{
494                         $NonptrType
495                         (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
496                         (?:\s+$Inline|\s+$Modifier)*
497                   }x;
498         $TypeMisordered = qr{
499                         $NonptrTypeMisordered
500                         (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
501                         (?:\s+$Inline|\s+$Modifier)*
502                   }x;
503         $Declare        = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
504         $DeclareMisordered      = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
505 }
506 build_types();
507
508 our $Typecast   = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
509
510 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
511 # requires at least perl version v5.10.0
512 # Any use must be runtime checked with $^V
513
514 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
515 our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
516 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
517
518 our $declaration_macros = qr{(?x:
519         (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
520         (?:$Storage\s+)?LIST_HEAD\s*\(|
521         (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
522 )};
523
524 sub deparenthesize {
525         my ($string) = @_;
526         return "" if (!defined($string));
527
528         while ($string =~ /^\s*\(.*\)\s*$/) {
529                 $string =~ s@^\s*\(\s*@@;
530                 $string =~ s@\s*\)\s*$@@;
531         }
532
533         $string =~ s@\s+@ @g;
534
535         return $string;
536 }
537
538 sub seed_camelcase_file {
539         my ($file) = @_;
540
541         return if (!(-f $file));
542
543         local $/;
544
545         open(my $include_file, '<', "$file")
546             or warn "$P: Can't read '$file' $!\n";
547         my $text = <$include_file>;
548         close($include_file);
549
550         my @lines = split('\n', $text);
551
552         foreach my $line (@lines) {
553                 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
554                 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
555                         $camelcase{$1} = 1;
556                 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
557                         $camelcase{$1} = 1;
558                 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
559                         $camelcase{$1} = 1;
560                 }
561         }
562 }
563
564 my $camelcase_seeded = 0;
565 sub seed_camelcase_includes {
566         return if ($camelcase_seeded);
567
568         my $files;
569         my $camelcase_cache = "";
570         my @include_files = ();
571
572         $camelcase_seeded = 1;
573
574         if (-e ".git") {
575                 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
576                 chomp $git_last_include_commit;
577                 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
578         } else {
579                 my $last_mod_date = 0;
580                 $files = `find $root/include -name "*.h"`;
581                 @include_files = split('\n', $files);
582                 foreach my $file (@include_files) {
583                         my $date = POSIX::strftime("%Y%m%d%H%M",
584                                                    localtime((stat $file)[9]));
585                         $last_mod_date = $date if ($last_mod_date < $date);
586                 }
587                 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
588         }
589
590         if ($camelcase_cache ne "" && -f $camelcase_cache) {
591                 open(my $camelcase_file, '<', "$camelcase_cache")
592                     or warn "$P: Can't read '$camelcase_cache' $!\n";
593                 while (<$camelcase_file>) {
594                         chomp;
595                         $camelcase{$_} = 1;
596                 }
597                 close($camelcase_file);
598
599                 return;
600         }
601
602         if (-e ".git") {
603                 $files = `git ls-files "include/*.h"`;
604                 @include_files = split('\n', $files);
605         }
606
607         foreach my $file (@include_files) {
608                 seed_camelcase_file($file);
609         }
610
611         if ($camelcase_cache ne "") {
612                 unlink glob ".checkpatch-camelcase.*";
613                 open(my $camelcase_file, '>', "$camelcase_cache")
614                     or warn "$P: Can't write '$camelcase_cache' $!\n";
615                 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
616                         print $camelcase_file ("$_\n");
617                 }
618                 close($camelcase_file);
619         }
620 }
621
622 sub git_commit_info {
623         my ($commit, $id, $desc) = @_;
624
625         return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
626
627         my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
628         $output =~ s/^\s*//gm;
629         my @lines = split("\n", $output);
630
631         if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
632 # Maybe one day convert this block of bash into something that returns
633 # all matching commit ids, but it's very slow...
634 #
635 #               echo "checking commits $1..."
636 #               git rev-list --remotes | grep -i "^$1" |
637 #               while read line ; do
638 #                   git log --format='%H %s' -1 $line |
639 #                   echo "commit $(cut -c 1-12,41-)"
640 #               done
641         } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
642         } else {
643                 $id = substr($lines[0], 0, 12);
644                 $desc = substr($lines[0], 41);
645         }
646
647         return ($id, $desc);
648 }
649
650 $chk_signoff = 0 if ($file);
651
652 my @rawlines = ();
653 my @lines = ();
654 my @fixed = ();
655 my @fixed_inserted = ();
656 my @fixed_deleted = ();
657 my $fixlinenr = -1;
658
659 my $vname;
660 for my $filename (@ARGV) {
661         my $FILE;
662         if ($file) {
663                 open($FILE, '-|', "diff -u /dev/null $filename") ||
664                         die "$P: $filename: diff failed - $!\n";
665         } elsif ($filename eq '-') {
666                 open($FILE, '<&STDIN');
667         } else {
668                 open($FILE, '<', "$filename") ||
669                         die "$P: $filename: open failed - $!\n";
670         }
671         if ($filename eq '-') {
672                 $vname = 'Your patch';
673         } else {
674                 $vname = $filename;
675         }
676         while (<$FILE>) {
677                 chomp;
678                 push(@rawlines, $_);
679         }
680         close($FILE);
681         if (!process($filename)) {
682                 $exit = 1;
683         }
684         @rawlines = ();
685         @lines = ();
686         @fixed = ();
687         @fixed_inserted = ();
688         @fixed_deleted = ();
689         $fixlinenr = -1;
690 }
691
692 exit($exit);
693
694 sub top_of_kernel_tree {
695         my ($root) = @_;
696
697         my @tree_check = (
698                 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
699                 "README", "Documentation", "arch", "include", "drivers",
700                 "fs", "init", "ipc", "kernel", "lib", "scripts",
701         );
702
703         foreach my $check (@tree_check) {
704                 if (! -e $root . '/' . $check) {
705                         return 0;
706                 }
707         }
708         return 1;
709 }
710
711 sub parse_email {
712         my ($formatted_email) = @_;
713
714         my $name = "";
715         my $address = "";
716         my $comment = "";
717
718         if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
719                 $name = $1;
720                 $address = $2;
721                 $comment = $3 if defined $3;
722         } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
723                 $address = $1;
724                 $comment = $2 if defined $2;
725         } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
726                 $address = $1;
727                 $comment = $2 if defined $2;
728                 $formatted_email =~ s/$address.*$//;
729                 $name = $formatted_email;
730                 $name = trim($name);
731                 $name =~ s/^\"|\"$//g;
732                 # If there's a name left after stripping spaces and
733                 # leading quotes, and the address doesn't have both
734                 # leading and trailing angle brackets, the address
735                 # is invalid. ie:
736                 #   "joe smith joe@smith.com" bad
737                 #   "joe smith <joe@smith.com" bad
738                 if ($name ne "" && $address !~ /^<[^>]+>$/) {
739                         $name = "";
740                         $address = "";
741                         $comment = "";
742                 }
743         }
744
745         $name = trim($name);
746         $name =~ s/^\"|\"$//g;
747         $address = trim($address);
748         $address =~ s/^\<|\>$//g;
749
750         if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
751                 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
752                 $name = "\"$name\"";
753         }
754
755         return ($name, $address, $comment);
756 }
757
758 sub format_email {
759         my ($name, $address) = @_;
760
761         my $formatted_email;
762
763         $name = trim($name);
764         $name =~ s/^\"|\"$//g;
765         $address = trim($address);
766
767         if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
768                 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
769                 $name = "\"$name\"";
770         }
771
772         if ("$name" eq "") {
773                 $formatted_email = "$address";
774         } else {
775                 $formatted_email = "$name <$address>";
776         }
777
778         return $formatted_email;
779 }
780
781 sub which {
782         my ($bin) = @_;
783
784         foreach my $path (split(/:/, $ENV{PATH})) {
785                 if (-e "$path/$bin") {
786                         return "$path/$bin";
787                 }
788         }
789
790         return "";
791 }
792
793 sub which_conf {
794         my ($conf) = @_;
795
796         foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
797                 if (-e "$path/$conf") {
798                         return "$path/$conf";
799                 }
800         }
801
802         return "";
803 }
804
805 sub expand_tabs {
806         my ($str) = @_;
807
808         my $res = '';
809         my $n = 0;
810         for my $c (split(//, $str)) {
811                 if ($c eq "\t") {
812                         $res .= ' ';
813                         $n++;
814                         for (; ($n % 8) != 0; $n++) {
815                                 $res .= ' ';
816                         }
817                         next;
818                 }
819                 $res .= $c;
820                 $n++;
821         }
822
823         return $res;
824 }
825 sub copy_spacing {
826         (my $res = shift) =~ tr/\t/ /c;
827         return $res;
828 }
829
830 sub line_stats {
831         my ($line) = @_;
832
833         # Drop the diff line leader and expand tabs
834         $line =~ s/^.//;
835         $line = expand_tabs($line);
836
837         # Pick the indent from the front of the line.
838         my ($white) = ($line =~ /^(\s*)/);
839
840         return (length($line), length($white));
841 }
842
843 my $sanitise_quote = '';
844
845 sub sanitise_line_reset {
846         my ($in_comment) = @_;
847
848         if ($in_comment) {
849                 $sanitise_quote = '*/';
850         } else {
851                 $sanitise_quote = '';
852         }
853 }
854 sub sanitise_line {
855         my ($line) = @_;
856
857         my $res = '';
858         my $l = '';
859
860         my $qlen = 0;
861         my $off = 0;
862         my $c;
863
864         # Always copy over the diff marker.
865         $res = substr($line, 0, 1);
866
867         for ($off = 1; $off < length($line); $off++) {
868                 $c = substr($line, $off, 1);
869
870                 # Comments we are wacking completly including the begin
871                 # and end, all to $;.
872                 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
873                         $sanitise_quote = '*/';
874
875                         substr($res, $off, 2, "$;$;");
876                         $off++;
877                         next;
878                 }
879                 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
880                         $sanitise_quote = '';
881                         substr($res, $off, 2, "$;$;");
882                         $off++;
883                         next;
884                 }
885                 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
886                         $sanitise_quote = '//';
887
888                         substr($res, $off, 2, $sanitise_quote);
889                         $off++;
890                         next;
891                 }
892
893                 # A \ in a string means ignore the next character.
894                 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
895                     $c eq "\\") {
896                         substr($res, $off, 2, 'XX');
897                         $off++;
898                         next;
899                 }
900                 # Regular quotes.
901                 if ($c eq "'" || $c eq '"') {
902                         if ($sanitise_quote eq '') {
903                                 $sanitise_quote = $c;
904
905                                 substr($res, $off, 1, $c);
906                                 next;
907                         } elsif ($sanitise_quote eq $c) {
908                                 $sanitise_quote = '';
909                         }
910                 }
911
912                 #print "c<$c> SQ<$sanitise_quote>\n";
913                 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
914                         substr($res, $off, 1, $;);
915                 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
916                         substr($res, $off, 1, $;);
917                 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
918                         substr($res, $off, 1, 'X');
919                 } else {
920                         substr($res, $off, 1, $c);
921                 }
922         }
923
924         if ($sanitise_quote eq '//') {
925                 $sanitise_quote = '';
926         }
927
928         # The pathname on a #include may be surrounded by '<' and '>'.
929         if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
930                 my $clean = 'X' x length($1);
931                 $res =~ s@\<.*\>@<$clean>@;
932
933         # The whole of a #error is a string.
934         } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
935                 my $clean = 'X' x length($1);
936                 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
937         }
938
939         return $res;
940 }
941
942 sub get_quoted_string {
943         my ($line, $rawline) = @_;
944
945         return "" if ($line !~ m/(\"[X]+\")/g);
946         return substr($rawline, $-[0], $+[0] - $-[0]);
947 }
948
949 sub ctx_statement_block {
950         my ($linenr, $remain, $off) = @_;
951         my $line = $linenr - 1;
952         my $blk = '';
953         my $soff = $off;
954         my $coff = $off - 1;
955         my $coff_set = 0;
956
957         my $loff = 0;
958
959         my $type = '';
960         my $level = 0;
961         my @stack = ();
962         my $p;
963         my $c;
964         my $len = 0;
965
966         my $remainder;
967         while (1) {
968                 @stack = (['', 0]) if ($#stack == -1);
969
970                 #warn "CSB: blk<$blk> remain<$remain>\n";
971                 # If we are about to drop off the end, pull in more
972                 # context.
973                 if ($off >= $len) {
974                         for (; $remain > 0; $line++) {
975                                 last if (!defined $lines[$line]);
976                                 next if ($lines[$line] =~ /^-/);
977                                 $remain--;
978                                 $loff = $len;
979                                 $blk .= $lines[$line] . "\n";
980                                 $len = length($blk);
981                                 $line++;
982                                 last;
983                         }
984                         # Bail if there is no further context.
985                         #warn "CSB: blk<$blk> off<$off> len<$len>\n";
986                         if ($off >= $len) {
987                                 last;
988                         }
989                         if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
990                                 $level++;
991                                 $type = '#';
992                         }
993                 }
994                 $p = $c;
995                 $c = substr($blk, $off, 1);
996                 $remainder = substr($blk, $off);
997
998                 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
999
1000                 # Handle nested #if/#else.
1001                 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1002                         push(@stack, [ $type, $level ]);
1003                 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1004                         ($type, $level) = @{$stack[$#stack - 1]};
1005                 } elsif ($remainder =~ /^#\s*endif\b/) {
1006                         ($type, $level) = @{pop(@stack)};
1007                 }
1008
1009                 # Statement ends at the ';' or a close '}' at the
1010                 # outermost level.
1011                 if ($level == 0 && $c eq ';') {
1012                         last;
1013                 }
1014
1015                 # An else is really a conditional as long as its not else if
1016                 if ($level == 0 && $coff_set == 0 &&
1017                                 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1018                                 $remainder =~ /^(else)(?:\s|{)/ &&
1019                                 $remainder !~ /^else\s+if\b/) {
1020                         $coff = $off + length($1) - 1;
1021                         $coff_set = 1;
1022                         #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1023                         #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1024                 }
1025
1026                 if (($type eq '' || $type eq '(') && $c eq '(') {
1027                         $level++;
1028                         $type = '(';
1029                 }
1030                 if ($type eq '(' && $c eq ')') {
1031                         $level--;
1032                         $type = ($level != 0)? '(' : '';
1033
1034                         if ($level == 0 && $coff < $soff) {
1035                                 $coff = $off;
1036                                 $coff_set = 1;
1037                                 #warn "CSB: mark coff<$coff>\n";
1038                         }
1039                 }
1040                 if (($type eq '' || $type eq '{') && $c eq '{') {
1041                         $level++;
1042                         $type = '{';
1043                 }
1044                 if ($type eq '{' && $c eq '}') {
1045                         $level--;
1046                         $type = ($level != 0)? '{' : '';
1047
1048                         if ($level == 0) {
1049                                 if (substr($blk, $off + 1, 1) eq ';') {
1050                                         $off++;
1051                                 }
1052                                 last;
1053                         }
1054                 }
1055                 # Preprocessor commands end at the newline unless escaped.
1056                 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1057                         $level--;
1058                         $type = '';
1059                         $off++;
1060                         last;
1061                 }
1062                 $off++;
1063         }
1064         # We are truly at the end, so shuffle to the next line.
1065         if ($off == $len) {
1066                 $loff = $len + 1;
1067                 $line++;
1068                 $remain--;
1069         }
1070
1071         my $statement = substr($blk, $soff, $off - $soff + 1);
1072         my $condition = substr($blk, $soff, $coff - $soff + 1);
1073
1074         #warn "STATEMENT<$statement>\n";
1075         #warn "CONDITION<$condition>\n";
1076
1077         #print "coff<$coff> soff<$off> loff<$loff>\n";
1078
1079         return ($statement, $condition,
1080                         $line, $remain + 1, $off - $loff + 1, $level);
1081 }
1082
1083 sub statement_lines {
1084         my ($stmt) = @_;
1085
1086         # Strip the diff line prefixes and rip blank lines at start and end.
1087         $stmt =~ s/(^|\n)./$1/g;
1088         $stmt =~ s/^\s*//;
1089         $stmt =~ s/\s*$//;
1090
1091         my @stmt_lines = ($stmt =~ /\n/g);
1092
1093         return $#stmt_lines + 2;
1094 }
1095
1096 sub statement_rawlines {
1097         my ($stmt) = @_;
1098
1099         my @stmt_lines = ($stmt =~ /\n/g);
1100
1101         return $#stmt_lines + 2;
1102 }
1103
1104 sub statement_block_size {
1105         my ($stmt) = @_;
1106
1107         $stmt =~ s/(^|\n)./$1/g;
1108         $stmt =~ s/^\s*{//;
1109         $stmt =~ s/}\s*$//;
1110         $stmt =~ s/^\s*//;
1111         $stmt =~ s/\s*$//;
1112
1113         my @stmt_lines = ($stmt =~ /\n/g);
1114         my @stmt_statements = ($stmt =~ /;/g);
1115
1116         my $stmt_lines = $#stmt_lines + 2;
1117         my $stmt_statements = $#stmt_statements + 1;
1118
1119         if ($stmt_lines > $stmt_statements) {
1120                 return $stmt_lines;
1121         } else {
1122                 return $stmt_statements;
1123         }
1124 }
1125
1126 sub ctx_statement_full {
1127         my ($linenr, $remain, $off) = @_;
1128         my ($statement, $condition, $level);
1129
1130         my (@chunks);
1131
1132         # Grab the first conditional/block pair.
1133         ($statement, $condition, $linenr, $remain, $off, $level) =
1134                                 ctx_statement_block($linenr, $remain, $off);
1135         #print "F: c<$condition> s<$statement> remain<$remain>\n";
1136         push(@chunks, [ $condition, $statement ]);
1137         if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1138                 return ($level, $linenr, @chunks);
1139         }
1140
1141         # Pull in the following conditional/block pairs and see if they
1142         # could continue the statement.
1143         for (;;) {
1144                 ($statement, $condition, $linenr, $remain, $off, $level) =
1145                                 ctx_statement_block($linenr, $remain, $off);
1146                 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1147                 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1148                 #print "C: push\n";
1149                 push(@chunks, [ $condition, $statement ]);
1150         }
1151
1152         return ($level, $linenr, @chunks);
1153 }
1154
1155 sub ctx_block_get {
1156         my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1157         my $line;
1158         my $start = $linenr - 1;
1159         my $blk = '';
1160         my @o;
1161         my @c;
1162         my @res = ();
1163
1164         my $level = 0;
1165         my @stack = ($level);
1166         for ($line = $start; $remain > 0; $line++) {
1167                 next if ($rawlines[$line] =~ /^-/);
1168                 $remain--;
1169
1170                 $blk .= $rawlines[$line];
1171
1172                 # Handle nested #if/#else.
1173                 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1174                         push(@stack, $level);
1175                 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1176                         $level = $stack[$#stack - 1];
1177                 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1178                         $level = pop(@stack);
1179                 }
1180
1181                 foreach my $c (split(//, $lines[$line])) {
1182                         ##print "C<$c>L<$level><$open$close>O<$off>\n";
1183                         if ($off > 0) {
1184                                 $off--;
1185                                 next;
1186                         }
1187
1188                         if ($c eq $close && $level > 0) {
1189                                 $level--;
1190                                 last if ($level == 0);
1191                         } elsif ($c eq $open) {
1192                                 $level++;
1193                         }
1194                 }
1195
1196                 if (!$outer || $level <= 1) {
1197                         push(@res, $rawlines[$line]);
1198                 }
1199
1200                 last if ($level == 0);
1201         }
1202
1203         return ($level, @res);
1204 }
1205 sub ctx_block_outer {
1206         my ($linenr, $remain) = @_;
1207
1208         my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1209         return @r;
1210 }
1211 sub ctx_block {
1212         my ($linenr, $remain) = @_;
1213
1214         my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1215         return @r;
1216 }
1217 sub ctx_statement {
1218         my ($linenr, $remain, $off) = @_;
1219
1220         my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1221         return @r;
1222 }
1223 sub ctx_block_level {
1224         my ($linenr, $remain) = @_;
1225
1226         return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1227 }
1228 sub ctx_statement_level {
1229         my ($linenr, $remain, $off) = @_;
1230
1231         return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1232 }
1233
1234 sub ctx_locate_comment {
1235         my ($first_line, $end_line) = @_;
1236
1237         # Catch a comment on the end of the line itself.
1238         my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1239         return $current_comment if (defined $current_comment);
1240
1241         # Look through the context and try and figure out if there is a
1242         # comment.
1243         my $in_comment = 0;
1244         $current_comment = '';
1245         for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1246                 my $line = $rawlines[$linenr - 1];
1247                 #warn "           $line\n";
1248                 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1249                         $in_comment = 1;
1250                 }
1251                 if ($line =~ m@/\*@) {
1252                         $in_comment = 1;
1253                 }
1254                 if (!$in_comment && $current_comment ne '') {
1255                         $current_comment = '';
1256                 }
1257                 $current_comment .= $line . "\n" if ($in_comment);
1258                 if ($line =~ m@\*/@) {
1259                         $in_comment = 0;
1260                 }
1261         }
1262
1263         chomp($current_comment);
1264         return($current_comment);
1265 }
1266 sub ctx_has_comment {
1267         my ($first_line, $end_line) = @_;
1268         my $cmt = ctx_locate_comment($first_line, $end_line);
1269
1270         ##print "LINE: $rawlines[$end_line - 1 ]\n";
1271         ##print "CMMT: $cmt\n";
1272
1273         return ($cmt ne '');
1274 }
1275
1276 sub raw_line {
1277         my ($linenr, $cnt) = @_;
1278
1279         my $offset = $linenr - 1;
1280         $cnt++;
1281
1282         my $line;
1283         while ($cnt) {
1284                 $line = $rawlines[$offset++];
1285                 next if (defined($line) && $line =~ /^-/);
1286                 $cnt--;
1287         }
1288
1289         return $line;
1290 }
1291
1292 sub cat_vet {
1293         my ($vet) = @_;
1294         my ($res, $coded);
1295
1296         $res = '';
1297         while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1298                 $res .= $1;
1299                 if ($2 ne '') {
1300                         $coded = sprintf("^%c", unpack('C', $2) + 64);
1301                         $res .= $coded;
1302                 }
1303         }
1304         $res =~ s/$/\$/;
1305
1306         return $res;
1307 }
1308
1309 my $av_preprocessor = 0;
1310 my $av_pending;
1311 my @av_paren_type;
1312 my $av_pend_colon;
1313
1314 sub annotate_reset {
1315         $av_preprocessor = 0;
1316         $av_pending = '_';
1317         @av_paren_type = ('E');
1318         $av_pend_colon = 'O';
1319 }
1320
1321 sub annotate_values {
1322         my ($stream, $type) = @_;
1323
1324         my $res;
1325         my $var = '_' x length($stream);
1326         my $cur = $stream;
1327
1328         print "$stream\n" if ($dbg_values > 1);
1329
1330         while (length($cur)) {
1331                 @av_paren_type = ('E') if ($#av_paren_type < 0);
1332                 print " <" . join('', @av_paren_type) .
1333                                 "> <$type> <$av_pending>" if ($dbg_values > 1);
1334                 if ($cur =~ /^(\s+)/o) {
1335                         print "WS($1)\n" if ($dbg_values > 1);
1336                         if ($1 =~ /\n/ && $av_preprocessor) {
1337                                 $type = pop(@av_paren_type);
1338                                 $av_preprocessor = 0;
1339                         }
1340
1341                 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1342                         print "CAST($1)\n" if ($dbg_values > 1);
1343                         push(@av_paren_type, $type);
1344                         $type = 'c';
1345
1346                 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1347                         print "DECLARE($1)\n" if ($dbg_values > 1);
1348                         $type = 'T';
1349
1350                 } elsif ($cur =~ /^($Modifier)\s*/) {
1351                         print "MODIFIER($1)\n" if ($dbg_values > 1);
1352                         $type = 'T';
1353
1354                 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1355                         print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1356                         $av_preprocessor = 1;
1357                         push(@av_paren_type, $type);
1358                         if ($2 ne '') {
1359                                 $av_pending = 'N';
1360                         }
1361                         $type = 'E';
1362
1363                 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1364                         print "UNDEF($1)\n" if ($dbg_values > 1);
1365                         $av_preprocessor = 1;
1366                         push(@av_paren_type, $type);
1367
1368                 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1369                         print "PRE_START($1)\n" if ($dbg_values > 1);
1370                         $av_preprocessor = 1;
1371
1372                         push(@av_paren_type, $type);
1373                         push(@av_paren_type, $type);
1374                         $type = 'E';
1375
1376                 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1377                         print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1378                         $av_preprocessor = 1;
1379
1380                         push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1381
1382                         $type = 'E';
1383
1384                 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1385                         print "PRE_END($1)\n" if ($dbg_values > 1);
1386
1387                         $av_preprocessor = 1;
1388
1389                         # Assume all arms of the conditional end as this
1390                         # one does, and continue as if the #endif was not here.
1391                         pop(@av_paren_type);
1392                         push(@av_paren_type, $type);
1393                         $type = 'E';
1394
1395                 } elsif ($cur =~ /^(\\\n)/o) {
1396                         print "PRECONT($1)\n" if ($dbg_values > 1);
1397
1398                 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1399                         print "ATTR($1)\n" if ($dbg_values > 1);
1400                         $av_pending = $type;
1401                         $type = 'N';
1402
1403                 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1404                         print "SIZEOF($1)\n" if ($dbg_values > 1);
1405                         if (defined $2) {
1406                                 $av_pending = 'V';
1407                         }
1408                         $type = 'N';
1409
1410                 } elsif ($cur =~ /^(if|while|for)\b/o) {
1411                         print "COND($1)\n" if ($dbg_values > 1);
1412                         $av_pending = 'E';
1413                         $type = 'N';
1414
1415                 } elsif ($cur =~/^(case)/o) {
1416                         print "CASE($1)\n" if ($dbg_values > 1);
1417                         $av_pend_colon = 'C';
1418                         $type = 'N';
1419
1420                 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1421                         print "KEYWORD($1)\n" if ($dbg_values > 1);
1422                         $type = 'N';
1423
1424                 } elsif ($cur =~ /^(\()/o) {
1425                         print "PAREN('$1')\n" if ($dbg_values > 1);
1426                         push(@av_paren_type, $av_pending);
1427                         $av_pending = '_';
1428                         $type = 'N';
1429
1430                 } elsif ($cur =~ /^(\))/o) {
1431                         my $new_type = pop(@av_paren_type);
1432                         if ($new_type ne '_') {
1433                                 $type = $new_type;
1434                                 print "PAREN('$1') -> $type\n"
1435                                                         if ($dbg_values > 1);
1436                         } else {
1437                                 print "PAREN('$1')\n" if ($dbg_values > 1);
1438                         }
1439
1440                 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1441                         print "FUNC($1)\n" if ($dbg_values > 1);
1442                         $type = 'V';
1443                         $av_pending = 'V';
1444
1445                 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1446                         if (defined $2 && $type eq 'C' || $type eq 'T') {
1447                                 $av_pend_colon = 'B';
1448                         } elsif ($type eq 'E') {
1449                                 $av_pend_colon = 'L';
1450                         }
1451                         print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1452                         $type = 'V';
1453
1454                 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1455                         print "IDENT($1)\n" if ($dbg_values > 1);
1456                         $type = 'V';
1457
1458                 } elsif ($cur =~ /^($Assignment)/o) {
1459                         print "ASSIGN($1)\n" if ($dbg_values > 1);
1460                         $type = 'N';
1461
1462                 } elsif ($cur =~/^(;|{|})/) {
1463                         print "END($1)\n" if ($dbg_values > 1);
1464                         $type = 'E';
1465                         $av_pend_colon = 'O';
1466
1467                 } elsif ($cur =~/^(,)/) {
1468                         print "COMMA($1)\n" if ($dbg_values > 1);
1469                         $type = 'C';
1470
1471                 } elsif ($cur =~ /^(\?)/o) {
1472                         print "QUESTION($1)\n" if ($dbg_values > 1);
1473                         $type = 'N';
1474
1475                 } elsif ($cur =~ /^(:)/o) {
1476                         print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1477
1478                         substr($var, length($res), 1, $av_pend_colon);
1479                         if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1480                                 $type = 'E';
1481                         } else {
1482                                 $type = 'N';
1483                         }
1484                         $av_pend_colon = 'O';
1485
1486                 } elsif ($cur =~ /^(\[)/o) {
1487                         print "CLOSE($1)\n" if ($dbg_values > 1);
1488                         $type = 'N';
1489
1490                 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1491                         my $variant;
1492
1493                         print "OPV($1)\n" if ($dbg_values > 1);
1494                         if ($type eq 'V') {
1495                                 $variant = 'B';
1496                         } else {
1497                                 $variant = 'U';
1498                         }
1499
1500                         substr($var, length($res), 1, $variant);
1501                         $type = 'N';
1502
1503                 } elsif ($cur =~ /^($Operators)/o) {
1504                         print "OP($1)\n" if ($dbg_values > 1);
1505                         if ($1 ne '++' && $1 ne '--') {
1506                                 $type = 'N';
1507                         }
1508
1509                 } elsif ($cur =~ /(^.)/o) {
1510                         print "C($1)\n" if ($dbg_values > 1);
1511                 }
1512                 if (defined $1) {
1513                         $cur = substr($cur, length($1));
1514                         $res .= $type x length($1);
1515                 }
1516         }
1517
1518         return ($res, $var);
1519 }
1520
1521 sub possible {
1522         my ($possible, $line) = @_;
1523         my $notPermitted = qr{(?:
1524                 ^(?:
1525                         $Modifier|
1526                         $Storage|
1527                         $Type|
1528                         DEFINE_\S+
1529                 )$|
1530                 ^(?:
1531                         goto|
1532                         return|
1533                         case|
1534                         else|
1535                         asm|__asm__|
1536                         do|
1537                         \#|
1538                         \#\#|
1539                 )(?:\s|$)|
1540                 ^(?:typedef|struct|enum)\b
1541             )}x;
1542         warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1543         if ($possible !~ $notPermitted) {
1544                 # Check for modifiers.
1545                 $possible =~ s/\s*$Storage\s*//g;
1546                 $possible =~ s/\s*$Sparse\s*//g;
1547                 if ($possible =~ /^\s*$/) {
1548
1549                 } elsif ($possible =~ /\s/) {
1550                         $possible =~ s/\s*$Type\s*//g;
1551                         for my $modifier (split(' ', $possible)) {
1552                                 if ($modifier !~ $notPermitted) {
1553                                         warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1554                                         push(@modifierList, $modifier);
1555                                 }
1556                         }
1557
1558                 } else {
1559                         warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1560                         push(@typeList, $possible);
1561                 }
1562                 build_types();
1563         } else {
1564                 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1565         }
1566 }
1567
1568 my $prefix = '';
1569
1570 sub show_type {
1571         my ($type) = @_;
1572
1573         return defined $use_type{$type} if (scalar keys %use_type > 0);
1574
1575         return !defined $ignore_type{$type};
1576 }
1577
1578 sub report {
1579         my ($level, $type, $msg) = @_;
1580
1581         if (!show_type($type) ||
1582             (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1583                 return 0;
1584         }
1585         my $line;
1586         if ($show_types) {
1587                 $line = "$prefix$level:$type: $msg\n";
1588         } else {
1589                 $line = "$prefix$level: $msg\n";
1590         }
1591         $line = (split('\n', $line))[0] . "\n" if ($terse);
1592
1593         push(our @report, $line);
1594
1595         return 1;
1596 }
1597
1598 sub report_dump {
1599         our @report;
1600 }
1601
1602 sub fixup_current_range {
1603         my ($lineRef, $offset, $length) = @_;
1604
1605         if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1606                 my $o = $1;
1607                 my $l = $2;
1608                 my $no = $o + $offset;
1609                 my $nl = $l + $length;
1610                 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1611         }
1612 }
1613
1614 sub fix_inserted_deleted_lines {
1615         my ($linesRef, $insertedRef, $deletedRef) = @_;
1616
1617         my $range_last_linenr = 0;
1618         my $delta_offset = 0;
1619
1620         my $old_linenr = 0;
1621         my $new_linenr = 0;
1622
1623         my $next_insert = 0;
1624         my $next_delete = 0;
1625
1626         my @lines = ();
1627
1628         my $inserted = @{$insertedRef}[$next_insert++];
1629         my $deleted = @{$deletedRef}[$next_delete++];
1630
1631         foreach my $old_line (@{$linesRef}) {
1632                 my $save_line = 1;
1633                 my $line = $old_line;   #don't modify the array
1634                 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) {     #new filename
1635                         $delta_offset = 0;
1636                 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {    #new hunk
1637                         $range_last_linenr = $new_linenr;
1638                         fixup_current_range(\$line, $delta_offset, 0);
1639                 }
1640
1641                 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1642                         $deleted = @{$deletedRef}[$next_delete++];
1643                         $save_line = 0;
1644                         fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1645                 }
1646
1647                 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1648                         push(@lines, ${$inserted}{'LINE'});
1649                         $inserted = @{$insertedRef}[$next_insert++];
1650                         $new_linenr++;
1651                         fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1652                 }
1653
1654                 if ($save_line) {
1655                         push(@lines, $line);
1656                         $new_linenr++;
1657                 }
1658
1659                 $old_linenr++;
1660         }
1661
1662         return @lines;
1663 }
1664
1665 sub fix_insert_line {
1666         my ($linenr, $line) = @_;
1667
1668         my $inserted = {
1669                 LINENR => $linenr,
1670                 LINE => $line,
1671         };
1672         push(@fixed_inserted, $inserted);
1673 }
1674
1675 sub fix_delete_line {
1676         my ($linenr, $line) = @_;
1677
1678         my $deleted = {
1679                 LINENR => $linenr,
1680                 LINE => $line,
1681         };
1682
1683         push(@fixed_deleted, $deleted);
1684 }
1685
1686 sub ERROR {
1687         my ($type, $msg) = @_;
1688
1689         if (report("ERROR", $type, $msg)) {
1690                 our $clean = 0;
1691                 our $cnt_error++;
1692                 return 1;
1693         }
1694         return 0;
1695 }
1696 sub WARN {
1697         my ($type, $msg) = @_;
1698
1699         if (report("WARNING", $type, $msg)) {
1700                 our $clean = 0;
1701                 our $cnt_warn++;
1702                 return 1;
1703         }
1704         return 0;
1705 }
1706 sub CHK {
1707         my ($type, $msg) = @_;
1708
1709         if ($check && report("CHECK", $type, $msg)) {
1710                 our $clean = 0;
1711                 our $cnt_chk++;
1712                 return 1;
1713         }
1714         return 0;
1715 }
1716
1717 sub check_absolute_file {
1718         my ($absolute, $herecurr) = @_;
1719         my $file = $absolute;
1720
1721         ##print "absolute<$absolute>\n";
1722
1723         # See if any suffix of this path is a path within the tree.
1724         while ($file =~ s@^[^/]*/@@) {
1725                 if (-f "$root/$file") {
1726                         ##print "file<$file>\n";
1727                         last;
1728                 }
1729         }
1730         if (! -f _)  {
1731                 return 0;
1732         }
1733
1734         # It is, so see if the prefix is acceptable.
1735         my $prefix = $absolute;
1736         substr($prefix, -length($file)) = '';
1737
1738         ##print "prefix<$prefix>\n";
1739         if ($prefix ne ".../") {
1740                 WARN("USE_RELATIVE_PATH",
1741                      "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1742         }
1743 }
1744
1745 sub trim {
1746         my ($string) = @_;
1747
1748         $string =~ s/^\s+|\s+$//g;
1749
1750         return $string;
1751 }
1752
1753 sub ltrim {
1754         my ($string) = @_;
1755
1756         $string =~ s/^\s+//;
1757
1758         return $string;
1759 }
1760
1761 sub rtrim {
1762         my ($string) = @_;
1763
1764         $string =~ s/\s+$//;
1765
1766         return $string;
1767 }
1768
1769 sub string_find_replace {
1770         my ($string, $find, $replace) = @_;
1771
1772         $string =~ s/$find/$replace/g;
1773
1774         return $string;
1775 }
1776
1777 sub tabify {
1778         my ($leading) = @_;
1779
1780         my $source_indent = 8;
1781         my $max_spaces_before_tab = $source_indent - 1;
1782         my $spaces_to_tab = " " x $source_indent;
1783
1784         #convert leading spaces to tabs
1785         1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1786         #Remove spaces before a tab
1787         1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1788
1789         return "$leading";
1790 }
1791
1792 sub pos_last_openparen {
1793         my ($line) = @_;
1794
1795         my $pos = 0;
1796
1797         my $opens = $line =~ tr/\(/\(/;
1798         my $closes = $line =~ tr/\)/\)/;
1799
1800         my $last_openparen = 0;
1801
1802         if (($opens == 0) || ($closes >= $opens)) {
1803                 return -1;
1804         }
1805
1806         my $len = length($line);
1807
1808         for ($pos = 0; $pos < $len; $pos++) {
1809                 my $string = substr($line, $pos);
1810                 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1811                         $pos += length($1) - 1;
1812                 } elsif (substr($line, $pos, 1) eq '(') {
1813                         $last_openparen = $pos;
1814                 } elsif (index($string, '(') == -1) {
1815                         last;
1816                 }
1817         }
1818
1819         return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1820 }
1821
1822 sub process {
1823         my $filename = shift;
1824
1825         my $linenr=0;
1826         my $prevline="";
1827         my $prevrawline="";
1828         my $stashline="";
1829         my $stashrawline="";
1830
1831         my $length;
1832         my $indent;
1833         my $previndent=0;
1834         my $stashindent=0;
1835
1836         our $clean = 1;
1837         my $signoff = 0;
1838         my $is_patch = 0;
1839
1840         my $in_header_lines = $file ? 0 : 1;
1841         my $in_commit_log = 0;          #Scanning lines before patch
1842         my $reported_maintainer_file = 0;
1843         my $non_utf8_charset = 0;
1844
1845         my $last_blank_line = 0;
1846
1847         our @report = ();
1848         our $cnt_lines = 0;
1849         our $cnt_error = 0;
1850         our $cnt_warn = 0;
1851         our $cnt_chk = 0;
1852
1853         # Trace the real file/line as we go.
1854         my $realfile = '';
1855         my $realline = 0;
1856         my $realcnt = 0;
1857         my $here = '';
1858         my $in_comment = 0;
1859         my $comment_edge = 0;
1860         my $first_line = 0;
1861         my $p1_prefix = '';
1862
1863         my $prev_values = 'E';
1864
1865         # suppression flags
1866         my %suppress_ifbraces;
1867         my %suppress_whiletrailers;
1868         my %suppress_export;
1869         my $suppress_statement = 0;
1870
1871         my %signatures = ();
1872
1873         # Pre-scan the patch sanitizing the lines.
1874         # Pre-scan the patch looking for any __setup documentation.
1875         #
1876         my @setup_docs = ();
1877         my $setup_docs = 0;
1878
1879         my $camelcase_file_seeded = 0;
1880
1881         sanitise_line_reset();
1882         my $line;
1883         foreach my $rawline (@rawlines) {
1884                 $linenr++;
1885                 $line = $rawline;
1886
1887                 push(@fixed, $rawline) if ($fix);
1888
1889                 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1890                         $setup_docs = 0;
1891                         if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1892                                 $setup_docs = 1;
1893                         }
1894                         #next;
1895                 }
1896                 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1897                         $realline=$1-1;
1898                         if (defined $2) {
1899                                 $realcnt=$3+1;
1900                         } else {
1901                                 $realcnt=1+1;
1902                         }
1903                         $in_comment = 0;
1904
1905                         # Guestimate if this is a continuing comment.  Run
1906                         # the context looking for a comment "edge".  If this
1907                         # edge is a close comment then we must be in a comment
1908                         # at context start.
1909                         my $edge;
1910                         my $cnt = $realcnt;
1911                         for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1912                                 next if (defined $rawlines[$ln - 1] &&
1913                                          $rawlines[$ln - 1] =~ /^-/);
1914                                 $cnt--;
1915                                 #print "RAW<$rawlines[$ln - 1]>\n";
1916                                 last if (!defined $rawlines[$ln - 1]);
1917                                 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1918                                     $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1919                                         ($edge) = $1;
1920                                         last;
1921                                 }
1922                         }
1923                         if (defined $edge && $edge eq '*/') {
1924                                 $in_comment = 1;
1925                         }
1926
1927                         # Guestimate if this is a continuing comment.  If this
1928                         # is the start of a diff block and this line starts
1929                         # ' *' then it is very likely a comment.
1930                         if (!defined $edge &&
1931                             $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1932                         {
1933                                 $in_comment = 1;
1934                         }
1935
1936                         ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1937                         sanitise_line_reset($in_comment);
1938
1939                 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1940                         # Standardise the strings and chars within the input to
1941                         # simplify matching -- only bother with positive lines.
1942                         $line = sanitise_line($rawline);
1943                 }
1944                 push(@lines, $line);
1945
1946                 if ($realcnt > 1) {
1947                         $realcnt-- if ($line =~ /^(?:\+| |$)/);
1948                 } else {
1949                         $realcnt = 0;
1950                 }
1951
1952                 #print "==>$rawline\n";
1953                 #print "-->$line\n";
1954
1955                 if ($setup_docs && $line =~ /^\+/) {
1956                         push(@setup_docs, $line);
1957                 }
1958         }
1959
1960         $prefix = '';
1961
1962         $realcnt = 0;
1963         $linenr = 0;
1964         $fixlinenr = -1;
1965         foreach my $line (@lines) {
1966                 $linenr++;
1967                 $fixlinenr++;
1968                 my $sline = $line;      #copy of $line
1969                 $sline =~ s/$;/ /g;     #with comments as spaces
1970
1971                 my $rawline = $rawlines[$linenr - 1];
1972
1973 #extract the line range in the file after the patch is applied
1974                 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1975                         $is_patch = 1;
1976                         $first_line = $linenr + 1;
1977                         $realline=$1-1;
1978                         if (defined $2) {
1979                                 $realcnt=$3+1;
1980                         } else {
1981                                 $realcnt=1+1;
1982                         }
1983                         annotate_reset();
1984                         $prev_values = 'E';
1985
1986                         %suppress_ifbraces = ();
1987                         %suppress_whiletrailers = ();
1988                         %suppress_export = ();
1989                         $suppress_statement = 0;
1990                         next;
1991
1992 # track the line number as we move through the hunk, note that
1993 # new versions of GNU diff omit the leading space on completely
1994 # blank context lines so we need to count that too.
1995                 } elsif ($line =~ /^( |\+|$)/) {
1996                         $realline++;
1997                         $realcnt-- if ($realcnt != 0);
1998
1999                         # Measure the line length and indent.
2000                         ($length, $indent) = line_stats($rawline);
2001
2002                         # Track the previous line.
2003                         ($prevline, $stashline) = ($stashline, $line);
2004                         ($previndent, $stashindent) = ($stashindent, $indent);
2005                         ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2006
2007                         #warn "line<$line>\n";
2008
2009                 } elsif ($realcnt == 1) {
2010                         $realcnt--;
2011                 }
2012
2013                 my $hunk_line = ($realcnt != 0);
2014
2015 #make up the handle for any error we report on this line
2016                 $prefix = "$filename:$realline: " if ($emacs && $file);
2017                 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2018
2019                 $here = "#$linenr: " if (!$file);
2020                 $here = "#$realline: " if ($file);
2021
2022                 my $found_file = 0;
2023                 # extract the filename as it passes
2024                 if ($line =~ /^diff --git.*?(\S+)$/) {
2025                         $realfile = $1;
2026                         $realfile =~ s@^([^/]*)/@@ if (!$file);
2027                         $in_commit_log = 0;
2028                         $found_file = 1;
2029                 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2030                         $realfile = $1;
2031                         $realfile =~ s@^([^/]*)/@@ if (!$file);
2032                         $in_commit_log = 0;
2033
2034                         $p1_prefix = $1;
2035                         if (!$file && $tree && $p1_prefix ne '' &&
2036                             -e "$root/$p1_prefix") {
2037                                 WARN("PATCH_PREFIX",
2038                                      "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2039                         }
2040
2041                         if ($realfile =~ m@^include/asm/@) {
2042                                 ERROR("MODIFIED_INCLUDE_ASM",
2043                                       "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2044                         }
2045                         $found_file = 1;
2046                 }
2047
2048                 if ($found_file) {
2049                         if ($realfile =~ m@^(drivers/net/|net/)@) {
2050                                 $check = 1;
2051                         } else {
2052                                 $check = $check_orig;
2053                         }
2054                         next;
2055                 }
2056
2057                 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2058
2059                 my $hereline = "$here\n$rawline\n";
2060                 my $herecurr = "$here\n$rawline\n";
2061                 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2062
2063                 $cnt_lines++ if ($realcnt != 0);
2064
2065 # Check for incorrect file permissions
2066                 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2067                         my $permhere = $here . "FILE: $realfile\n";
2068                         if ($realfile !~ m@scripts/@ &&
2069                             $realfile !~ /\.(py|pl|awk|sh)$/) {
2070                                 ERROR("EXECUTE_PERMISSIONS",
2071                                       "do not set execute permissions for source files\n" . $permhere);
2072                         }
2073                 }
2074
2075 # Check the patch for a signoff:
2076                 if ($line =~ /^\s*signed-off-by:/i) {
2077                         $signoff++;
2078                         $in_commit_log = 0;
2079                 }
2080
2081 # Check signature styles
2082                 if (!$in_header_lines &&
2083                     $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2084                         my $space_before = $1;
2085                         my $sign_off = $2;
2086                         my $space_after = $3;
2087                         my $email = $4;
2088                         my $ucfirst_sign_off = ucfirst(lc($sign_off));
2089
2090                         if ($sign_off !~ /$signature_tags/) {
2091                                 WARN("BAD_SIGN_OFF",
2092                                      "Non-standard signature: $sign_off\n" . $herecurr);
2093                         }
2094                         if (defined $space_before && $space_before ne "") {
2095                                 if (WARN("BAD_SIGN_OFF",
2096                                          "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2097                                     $fix) {
2098                                         $fixed[$fixlinenr] =
2099                                             "$ucfirst_sign_off $email";
2100                                 }
2101                         }
2102                         if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2103                                 if (WARN("BAD_SIGN_OFF",
2104                                          "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2105                                     $fix) {
2106                                         $fixed[$fixlinenr] =
2107                                             "$ucfirst_sign_off $email";
2108                                 }
2109
2110                         }
2111                         if (!defined $space_after || $space_after ne " ") {
2112                                 if (WARN("BAD_SIGN_OFF",
2113                                          "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2114                                     $fix) {
2115                                         $fixed[$fixlinenr] =
2116                                             "$ucfirst_sign_off $email";
2117                                 }
2118                         }
2119
2120                         my ($email_name, $email_address, $comment) = parse_email($email);
2121                         my $suggested_email = format_email(($email_name, $email_address));
2122                         if ($suggested_email eq "") {
2123                                 ERROR("BAD_SIGN_OFF",
2124                                       "Unrecognized email address: '$email'\n" . $herecurr);
2125                         } else {
2126                                 my $dequoted = $suggested_email;
2127                                 $dequoted =~ s/^"//;
2128                                 $dequoted =~ s/" </ </;
2129                                 # Don't force email to have quotes
2130                                 # Allow just an angle bracketed address
2131                                 if ("$dequoted$comment" ne $email &&
2132                                     "<$email_address>$comment" ne $email &&
2133                                     "$suggested_email$comment" ne $email) {
2134                                         WARN("BAD_SIGN_OFF",
2135                                              "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2136                                 }
2137                         }
2138
2139 # Check for duplicate signatures
2140                         my $sig_nospace = $line;
2141                         $sig_nospace =~ s/\s//g;
2142                         $sig_nospace = lc($sig_nospace);
2143                         if (defined $signatures{$sig_nospace}) {
2144                                 WARN("BAD_SIGN_OFF",
2145                                      "Duplicate signature\n" . $herecurr);
2146                         } else {
2147                                 $signatures{$sig_nospace} = 1;
2148                         }
2149                 }
2150
2151 # Check for old stable address
2152                 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2153                         ERROR("STABLE_ADDRESS",
2154                               "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2155                 }
2156
2157 # Check for unwanted Gerrit info
2158                 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2159                         ERROR("GERRIT_CHANGE_ID",
2160                               "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2161                 }
2162
2163 # Check for improperly formed commit descriptions
2164                 if ($in_commit_log &&
2165                     $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
2166                     !($line =~ /\b[Cc]ommit [0-9a-f]{12,40} \("/ ||
2167                       ($line =~ /\b[Cc]ommit [0-9a-f]{12,40}\s*$/ &&
2168                        defined $rawlines[$linenr] &&
2169                        $rawlines[$linenr] =~ /^\s*\("/))) {
2170                         $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2171                         my $init_char = $1;
2172                         my $orig_commit = lc($2);
2173                         my $id = '01234567890ab';
2174                         my $desc = 'commit description';
2175                         ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2176                         ERROR("GIT_COMMIT_ID",
2177                               "Please use 12 or more chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2178                 }
2179
2180 # Check for added, moved or deleted files
2181                 if (!$reported_maintainer_file && !$in_commit_log &&
2182                     ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2183                      $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2184                      ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2185                       (defined($1) || defined($2))))) {
2186                         $reported_maintainer_file = 1;
2187                         WARN("FILE_PATH_CHANGES",
2188                              "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2189                 }
2190
2191 # Check for wrappage within a valid hunk of the file
2192                 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2193                         ERROR("CORRUPTED_PATCH",
2194                               "patch seems to be corrupt (line wrapped?)\n" .
2195                                 $herecurr) if (!$emitted_corrupt++);
2196                 }
2197
2198 # Check for absolute kernel paths.
2199                 if ($tree) {
2200                         while ($line =~ m{(?:^|\s)(/\S*)}g) {
2201                                 my $file = $1;
2202
2203                                 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2204                                     check_absolute_file($1, $herecurr)) {
2205                                         #
2206                                 } else {
2207                                         check_absolute_file($file, $herecurr);
2208                                 }
2209                         }
2210                 }
2211
2212 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2213                 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2214                     $rawline !~ m/^$UTF8*$/) {
2215                         my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2216
2217                         my $blank = copy_spacing($rawline);
2218                         my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2219                         my $hereptr = "$hereline$ptr\n";
2220
2221                         CHK("INVALID_UTF8",
2222                             "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2223                 }
2224
2225 # Check if it's the start of a commit log
2226 # (not a header line and we haven't seen the patch filename)
2227                 if ($in_header_lines && $realfile =~ /^$/ &&
2228                     !($rawline =~ /^\s+\S/ ||
2229                       $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
2230                         $in_header_lines = 0;
2231                         $in_commit_log = 1;
2232                 }
2233
2234 # Check if there is UTF-8 in a commit log when a mail header has explicitly
2235 # declined it, i.e defined some charset where it is missing.
2236                 if ($in_header_lines &&
2237                     $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2238                     $1 !~ /utf-8/i) {
2239                         $non_utf8_charset = 1;
2240                 }
2241
2242                 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2243                     $rawline =~ /$NON_ASCII_UTF8/) {
2244                         WARN("UTF8_BEFORE_PATCH",
2245                             "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2246                 }
2247
2248 # Check for various typo / spelling mistakes
2249                 if ($in_commit_log || $line =~ /^\+/) {
2250                         while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
2251                                 my $typo = $1;
2252                                 my $typo_fix = $spelling_fix{lc($typo)};
2253                                 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2254                                 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2255                                 my $msg_type = \&WARN;
2256                                 $msg_type = \&CHK if ($file);
2257                                 if (&{$msg_type}("TYPO_SPELLING",
2258                                                  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2259                                     $fix) {
2260                                         $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2261                                 }
2262                         }
2263                 }
2264
2265 # ignore non-hunk lines and lines being removed
2266                 next if (!$hunk_line || $line =~ /^-/);
2267
2268 #trailing whitespace
2269                 if ($line =~ /^\+.*\015/) {
2270                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2271                         if (ERROR("DOS_LINE_ENDINGS",
2272                                   "DOS line endings\n" . $herevet) &&
2273                             $fix) {
2274                                 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2275                         }
2276                 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2277                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2278                         if (ERROR("TRAILING_WHITESPACE",
2279                                   "trailing whitespace\n" . $herevet) &&
2280                             $fix) {
2281                                 $fixed[$fixlinenr] =~ s/\s+$//;
2282                         }
2283
2284                         $rpt_cleaners = 1;
2285                 }
2286
2287 # Check for FSF mailing addresses.
2288                 if ($rawline =~ /\bwrite to the Free/i ||
2289                     $rawline =~ /\b59\s+Temple\s+Pl/i ||
2290                     $rawline =~ /\b51\s+Franklin\s+St/i) {
2291                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2292                         my $msg_type = \&ERROR;
2293                         $msg_type = \&CHK if ($file);
2294                         &{$msg_type}("FSF_MAILING_ADDRESS",
2295                                      "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
2296                 }
2297
2298 # check for Kconfig help text having a real description
2299 # Only applies when adding the entry originally, after that we do not have
2300 # sufficient context to determine whether it is indeed long enough.
2301                 if ($realfile =~ /Kconfig/ &&
2302                     $line =~ /^\+\s*config\s+/) {
2303                         my $length = 0;
2304                         my $cnt = $realcnt;
2305                         my $ln = $linenr + 1;
2306                         my $f;
2307                         my $is_start = 0;
2308                         my $is_end = 0;
2309                         for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
2310                                 $f = $lines[$ln - 1];
2311                                 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2312                                 $is_end = $lines[$ln - 1] =~ /^\+/;
2313
2314                                 next if ($f =~ /^-/);
2315                                 last if (!$file && $f =~ /^\@\@/);
2316
2317                                 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2318                                         $is_start = 1;
2319                                 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2320                                         $length = -1;
2321                                 }
2322
2323                                 $f =~ s/^.//;
2324                                 $f =~ s/#.*//;
2325                                 $f =~ s/^\s+//;
2326                                 next if ($f =~ /^$/);
2327                                 if ($f =~ /^\s*config\s/) {
2328                                         $is_end = 1;
2329                                         last;
2330                                 }
2331                                 $length++;
2332                         }
2333                         if ($is_start && $is_end && $length < $min_conf_desc_length) {
2334                                 WARN("CONFIG_DESCRIPTION",
2335                                      "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2336                         }
2337                         #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2338                 }
2339
2340 # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2341                 if ($realfile =~ /Kconfig/ &&
2342                     $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2343                         WARN("CONFIG_EXPERIMENTAL",
2344                              "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2345                 }
2346
2347                 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2348                     ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2349                         my $flag = $1;
2350                         my $replacement = {
2351                                 'EXTRA_AFLAGS' =>   'asflags-y',
2352                                 'EXTRA_CFLAGS' =>   'ccflags-y',
2353                                 'EXTRA_CPPFLAGS' => 'cppflags-y',
2354                                 'EXTRA_LDFLAGS' =>  'ldflags-y',
2355                         };
2356
2357                         WARN("DEPRECATED_VARIABLE",
2358                              "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2359                 }
2360
2361 # check for DT compatible documentation
2362                 if (defined $root &&
2363                         (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2364                          ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2365
2366                         my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2367
2368                         my $dt_path = $root . "/Documentation/devicetree/bindings/";
2369                         my $vp_file = $dt_path . "vendor-prefixes.txt";
2370
2371                         foreach my $compat (@compats) {
2372                                 my $compat2 = $compat;
2373                                 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2374                                 my $compat3 = $compat;
2375                                 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2376                                 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2377                                 if ( $? >> 8 ) {
2378                                         WARN("UNDOCUMENTED_DT_STRING",
2379                                              "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2380                                 }
2381
2382                                 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2383                                 my $vendor = $1;
2384                                 `grep -Eq "^$vendor\\b" $vp_file`;
2385                                 if ( $? >> 8 ) {
2386                                         WARN("UNDOCUMENTED_DT_STRING",
2387                                              "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2388                                 }
2389                         }
2390                 }
2391
2392 # check we are in a valid source file if not then ignore this hunk
2393                 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
2394
2395 #line length limit
2396                 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2397                     $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
2398                     !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
2399                     $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
2400                     $length > $max_line_length)
2401                 {
2402                         WARN("LONG_LINE",
2403                              "line over $max_line_length characters\n" . $herecurr);
2404                 }
2405
2406 # Check for user-visible strings broken across lines, which breaks the ability
2407 # to grep for the string.  Make exceptions when the previous string ends in a
2408 # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
2409 # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2410                 if ($line =~ /^\+\s*"/ &&
2411                     $prevline =~ /"\s*$/ &&
2412                     $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2413                         WARN("SPLIT_STRING",
2414                              "quoted string split across lines\n" . $hereprev);
2415                 }
2416
2417 # check for missing a space in a string concatination
2418                 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
2419                         WARN('MISSING_SPACE',
2420                              "break quoted strings at a space character\n" . $hereprev);
2421                 }
2422
2423 # check for spaces before a quoted newline
2424                 if ($rawline =~ /^.*\".*\s\\n/) {
2425                         if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2426                                  "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2427                             $fix) {
2428                                 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2429                         }
2430
2431                 }
2432
2433 # check for adding lines without a newline.
2434                 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2435                         WARN("MISSING_EOF_NEWLINE",
2436                              "adding a line without newline at end of file\n" . $herecurr);
2437                 }
2438
2439 # Blackfin: use hi/lo macros
2440                 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2441                         if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2442                                 my $herevet = "$here\n" . cat_vet($line) . "\n";
2443                                 ERROR("LO_MACRO",
2444                                       "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2445                         }
2446                         if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2447                                 my $herevet = "$here\n" . cat_vet($line) . "\n";
2448                                 ERROR("HI_MACRO",
2449                                       "use the HI() macro, not (... >> 16)\n" . $herevet);
2450                         }
2451                 }
2452
2453 # check we are in a valid source file C or perl if not then ignore this hunk
2454                 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
2455
2456 # at the beginning of a line any tabs must come first and anything
2457 # more than 8 must use tabs.
2458                 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2459                     $rawline =~ /^\+\s*        \s*/) {
2460                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2461                         $rpt_cleaners = 1;
2462                         if (ERROR("CODE_INDENT",
2463                                   "code indent should use tabs where possible\n" . $herevet) &&
2464                             $fix) {
2465                                 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2466                         }
2467                 }
2468
2469 # check for space before tabs.
2470                 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2471                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2472                         if (WARN("SPACE_BEFORE_TAB",
2473                                 "please, no space before tabs\n" . $herevet) &&
2474                             $fix) {
2475                                 while ($fixed[$fixlinenr] =~
2476                                            s/(^\+.*) {8,8}\t/$1\t\t/) {}
2477                                 while ($fixed[$fixlinenr] =~
2478                                            s/(^\+.*) +\t/$1\t/) {}
2479                         }
2480                 }
2481
2482 # check for && or || at the start of a line
2483                 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2484                         CHK("LOGICAL_CONTINUATIONS",
2485                             "Logical continuations should be on the previous line\n" . $hereprev);
2486                 }
2487
2488 # check multi-line statement indentation matches previous line
2489                 if ($^V && $^V ge 5.10.0 &&
2490                     $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2491                         $prevline =~ /^\+(\t*)(.*)$/;
2492                         my $oldindent = $1;
2493                         my $rest = $2;
2494
2495                         my $pos = pos_last_openparen($rest);
2496                         if ($pos >= 0) {
2497                                 $line =~ /^(\+| )([ \t]*)/;
2498                                 my $newindent = $2;
2499
2500                                 my $goodtabindent = $oldindent .
2501                                         "\t" x ($pos / 8) .
2502                                         " "  x ($pos % 8);
2503                                 my $goodspaceindent = $oldindent . " "  x $pos;
2504
2505                                 if ($newindent ne $goodtabindent &&
2506                                     $newindent ne $goodspaceindent) {
2507
2508                                         if (CHK("PARENTHESIS_ALIGNMENT",
2509                                                 "Alignment should match open parenthesis\n" . $hereprev) &&
2510                                             $fix && $line =~ /^\+/) {
2511                                                 $fixed[$fixlinenr] =~
2512                                                     s/^\+[ \t]*/\+$goodtabindent/;
2513                                         }
2514                                 }
2515                         }
2516                 }
2517
2518                 if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|{)/) {
2519                         if (CHK("SPACING",
2520                                 "No space is necessary after a cast\n" . $herecurr) &&
2521                             $fix) {
2522                                 $fixed[$fixlinenr] =~
2523                                     s/(\(\s*$Type\s*\))[ \t]+/$1/;
2524                         }
2525                 }
2526
2527                 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2528                     $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2529                     $rawline =~ /^\+[ \t]*\*/ &&
2530                     $realline > 2) {
2531                         WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2532                              "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2533                 }
2534
2535                 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2536                     $prevrawline =~ /^\+[ \t]*\/\*/ &&          #starting /*
2537                     $prevrawline !~ /\*\/[ \t]*$/ &&            #no trailing */
2538                     $rawline =~ /^\+/ &&                        #line is new
2539                     $rawline !~ /^\+[ \t]*\*/) {                #no leading *
2540                         WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2541                              "networking block comments start with * on subsequent lines\n" . $hereprev);
2542                 }
2543
2544                 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2545                     $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&       #trailing */
2546                     $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&      #inline /*...*/
2547                     $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&       #trailing **/
2548                     $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {    #non blank */
2549                         WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2550                              "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2551                 }
2552
2553 # check for missing blank lines after struct/union declarations
2554 # with exceptions for various attributes and macros
2555                 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2556                     $line =~ /^\+/ &&
2557                     !($line =~ /^\+\s*$/ ||
2558                       $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2559                       $line =~ /^\+\s*MODULE_/i ||
2560                       $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2561                       $line =~ /^\+[a-z_]*init/ ||
2562                       $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2563                       $line =~ /^\+\s*DECLARE/ ||
2564                       $line =~ /^\+\s*__setup/)) {
2565                         if (CHK("LINE_SPACING",
2566                                 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2567                             $fix) {
2568                                 fix_insert_line($fixlinenr, "\+");
2569                         }
2570                 }
2571
2572 # check for multiple consecutive blank lines
2573                 if ($prevline =~ /^[\+ ]\s*$/ &&
2574                     $line =~ /^\+\s*$/ &&
2575                     $last_blank_line != ($linenr - 1)) {
2576                         if (CHK("LINE_SPACING",
2577                                 "Please don't use multiple blank lines\n" . $hereprev) &&
2578                             $fix) {
2579                                 fix_delete_line($fixlinenr, $rawline);
2580                         }
2581
2582                         $last_blank_line = $linenr;
2583                 }
2584
2585 # check for missing blank lines after declarations
2586                 if ($sline =~ /^\+\s+\S/ &&                     #Not at char 1
2587                         # actual declarations
2588                     ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2589                         # function pointer declarations
2590                      $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2591                         # foo bar; where foo is some local typedef or #define
2592                      $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2593                         # known declaration macros
2594                      $prevline =~ /^\+\s+$declaration_macros/) &&
2595                         # for "else if" which can look like "$Ident $Ident"
2596                     !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2597                         # other possible extensions of declaration lines
2598                       $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2599                         # not starting a section or a macro "\" extended line
2600                       $prevline =~ /(?:\{\s*|\\)$/) &&
2601                         # looks like a declaration
2602                     !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2603                         # function pointer declarations
2604                       $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2605                         # foo bar; where foo is some local typedef or #define
2606                       $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2607                         # known declaration macros
2608                       $sline =~ /^\+\s+$declaration_macros/ ||
2609                         # start of struct or union or enum
2610                       $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
2611                         # start or end of block or continuation of declaration
2612                       $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2613                         # bitfield continuation
2614                       $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2615                         # other possible extensions of declaration lines
2616                       $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2617                         # indentation of previous and current line are the same
2618                     (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2619                         if (WARN("LINE_SPACING",
2620                                  "Missing a blank line after declarations\n" . $hereprev) &&
2621                             $fix) {
2622                                 fix_insert_line($fixlinenr, "\+");
2623                         }
2624                 }
2625
2626 # check for spaces at the beginning of a line.
2627 # Exceptions:
2628 #  1) within comments
2629 #  2) indented preprocessor commands
2630 #  3) hanging labels
2631                 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
2632                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2633                         if (WARN("LEADING_SPACE",
2634                                  "please, no spaces at the start of a line\n" . $herevet) &&
2635                             $fix) {
2636                                 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2637                         }
2638                 }
2639
2640 # check we are in a valid C source file if not then ignore this hunk
2641                 next if ($realfile !~ /\.(h|c)$/);
2642
2643 # check indentation of any line with a bare else
2644 # if the previous line is a break or return and is indented 1 tab more...
2645                 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2646                         my $tabs = length($1) + 1;
2647                         if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2648                                 WARN("UNNECESSARY_ELSE",
2649                                      "else is not generally useful after a break or return\n" . $hereprev);
2650                         }
2651                 }
2652
2653 # check indentation of a line with a break;
2654 # if the previous line is a goto or return and is indented the same # of tabs
2655                 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2656                         my $tabs = $1;
2657                         if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2658                                 WARN("UNNECESSARY_BREAK",
2659                                      "break is not useful after a goto or return\n" . $hereprev);
2660                         }
2661                 }
2662
2663 # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2664                 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2665                         WARN("CONFIG_EXPERIMENTAL",
2666                              "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2667                 }
2668
2669 # check for RCS/CVS revision markers
2670                 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2671                         WARN("CVS_KEYWORD",
2672                              "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2673                 }
2674
2675 # Blackfin: don't use __builtin_bfin_[cs]sync
2676                 if ($line =~ /__builtin_bfin_csync/) {
2677                         my $herevet = "$here\n" . cat_vet($line) . "\n";
2678                         ERROR("CSYNC",
2679                               "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
2680                 }
2681                 if ($line =~ /__builtin_bfin_ssync/) {
2682                         my $herevet = "$here\n" . cat_vet($line) . "\n";
2683                         ERROR("SSYNC",
2684                               "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
2685                 }
2686
2687 # check for old HOTPLUG __dev<foo> section markings
2688                 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2689                         WARN("HOTPLUG_SECTION",
2690                              "Using $1 is unnecessary\n" . $herecurr);
2691                 }
2692
2693 # Check for potential 'bare' types
2694                 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2695                     $realline_next);
2696 #print "LINE<$line>\n";
2697                 if ($linenr >= $suppress_statement &&
2698                     $realcnt && $sline =~ /.\s*\S/) {
2699                         ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2700                                 ctx_statement_block($linenr, $realcnt, 0);
2701                         $stat =~ s/\n./\n /g;
2702                         $cond =~ s/\n./\n /g;
2703
2704 #print "linenr<$linenr> <$stat>\n";
2705                         # If this statement has no statement boundaries within
2706                         # it there is no point in retrying a statement scan
2707                         # until we hit end of it.
2708                         my $frag = $stat; $frag =~ s/;+\s*$//;
2709                         if ($frag !~ /(?:{|;)/) {
2710 #print "skip<$line_nr_next>\n";
2711                                 $suppress_statement = $line_nr_next;
2712                         }
2713
2714                         # Find the real next line.
2715                         $realline_next = $line_nr_next;
2716                         if (defined $realline_next &&
2717                             (!defined $lines[$realline_next - 1] ||
2718                              substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2719                                 $realline_next++;
2720                         }
2721
2722                         my $s = $stat;
2723                         $s =~ s/{.*$//s;
2724
2725                         # Ignore goto labels.
2726                         if ($s =~ /$Ident:\*$/s) {
2727
2728                         # Ignore functions being called
2729                         } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2730
2731                         } elsif ($s =~ /^.\s*else\b/s) {
2732
2733                         # declarations always start with types
2734                         } 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) {
2735                                 my $type = $1;
2736                                 $type =~ s/\s+/ /g;
2737                                 possible($type, "A:" . $s);
2738
2739                         # definitions in global scope can only start with types
2740                         } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2741                                 possible($1, "B:" . $s);
2742                         }
2743
2744                         # any (foo ... *) is a pointer cast, and foo is a type
2745                         while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2746                                 possible($1, "C:" . $s);
2747                         }
2748
2749                         # Check for any sort of function declaration.
2750                         # int foo(something bar, other baz);
2751                         # void (*store_gdt)(x86_descr_ptr *);
2752                         if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
2753                                 my ($name_len) = length($1);
2754
2755                                 my $ctx = $s;
2756                                 substr($ctx, 0, $name_len + 1, '');
2757                                 $ctx =~ s/\)[^\)]*$//;
2758
2759                                 for my $arg (split(/\s*,\s*/, $ctx)) {
2760                                         if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
2761
2762                                                 possible($1, "D:" . $s);
2763                                         }
2764                                 }
2765                         }
2766
2767                 }
2768
2769 #
2770 # Checks which may be anchored in the context.
2771 #
2772
2773 # Check for switch () and associated case and default
2774 # statements should be at the same indent.
2775                 if ($line=~/\bswitch\s*\(.*\)/) {
2776                         my $err = '';
2777                         my $sep = '';
2778                         my @ctx = ctx_block_outer($linenr, $realcnt);
2779                         shift(@ctx);
2780                         for my $ctx (@ctx) {
2781                                 my ($clen, $cindent) = line_stats($ctx);
2782                                 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2783                                                         $indent != $cindent) {
2784                                         $err .= "$sep$ctx\n";
2785                                         $sep = '';
2786                                 } else {
2787                                         $sep = "[...]\n";
2788                                 }
2789                         }
2790                         if ($err ne '') {
2791                                 ERROR("SWITCH_CASE_INDENT_LEVEL",
2792                                       "switch and case should be at the same indent\n$hereline$err");
2793                         }
2794                 }
2795
2796 # if/while/etc brace do not go on next line, unless defining a do while loop,
2797 # or if that brace on the next line is for something else
2798                 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2799                         my $pre_ctx = "$1$2";
2800
2801                         my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
2802
2803                         if ($line =~ /^\+\t{6,}/) {
2804                                 WARN("DEEP_INDENTATION",
2805                                      "Too many leading tabs - consider code refactoring\n" . $herecurr);
2806                         }
2807
2808                         my $ctx_cnt = $realcnt - $#ctx - 1;
2809                         my $ctx = join("\n", @ctx);
2810
2811                         my $ctx_ln = $linenr;
2812                         my $ctx_skip = $realcnt;
2813
2814                         while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2815                                         defined $lines[$ctx_ln - 1] &&
2816                                         $lines[$ctx_ln - 1] =~ /^-/)) {
2817                                 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2818                                 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2819                                 $ctx_ln++;
2820                         }
2821
2822                         #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2823                         #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2824
2825                         if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2826                                 ERROR("OPEN_BRACE",
2827                                       "that open brace { should be on the previous line\n" .
2828                                         "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2829                         }
2830                         if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2831                             $ctx =~ /\)\s*\;\s*$/ &&
2832                             defined $lines[$ctx_ln - 1])
2833                         {
2834                                 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2835                                 if ($nindent > $indent) {
2836                                         WARN("TRAILING_SEMICOLON",
2837                                              "trailing semicolon indicates no statements, indent implies otherwise\n" .
2838                                                 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2839                                 }
2840                         }
2841                 }
2842
2843 # Check relative indent for conditionals and blocks.
2844                 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2845                         ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2846                                 ctx_statement_block($linenr, $realcnt, 0)
2847                                         if (!defined $stat);
2848                         my ($s, $c) = ($stat, $cond);
2849
2850                         substr($s, 0, length($c), '');
2851
2852                         # Make sure we remove the line prefixes as we have
2853                         # none on the first line, and are going to readd them
2854                         # where necessary.
2855                         $s =~ s/\n./\n/gs;
2856
2857                         # Find out how long the conditional actually is.
2858                         my @newlines = ($c =~ /\n/gs);
2859                         my $cond_lines = 1 + $#newlines;
2860
2861                         # We want to check the first line inside the block
2862                         # starting at the end of the conditional, so remove:
2863                         #  1) any blank line termination
2864                         #  2) any opening brace { on end of the line
2865                         #  3) any do (...) {
2866                         my $continuation = 0;
2867                         my $check = 0;
2868                         $s =~ s/^.*\bdo\b//;
2869                         $s =~ s/^\s*{//;
2870                         if ($s =~ s/^\s*\\//) {
2871                                 $continuation = 1;
2872                         }
2873                         if ($s =~ s/^\s*?\n//) {
2874                                 $check = 1;
2875                                 $cond_lines++;
2876                         }
2877
2878                         # Also ignore a loop construct at the end of a
2879                         # preprocessor statement.
2880                         if (($prevline =~ /^.\s*#\s*define\s/ ||
2881                             $prevline =~ /\\\s*$/) && $continuation == 0) {
2882                                 $check = 0;
2883                         }
2884
2885                         my $cond_ptr = -1;
2886                         $continuation = 0;
2887                         while ($cond_ptr != $cond_lines) {
2888                                 $cond_ptr = $cond_lines;
2889
2890                                 # If we see an #else/#elif then the code
2891                                 # is not linear.
2892                                 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2893                                         $check = 0;
2894                                 }
2895
2896                                 # Ignore:
2897                                 #  1) blank lines, they should be at 0,
2898                                 #  2) preprocessor lines, and
2899                                 #  3) labels.
2900                                 if ($continuation ||
2901                                     $s =~ /^\s*?\n/ ||
2902                                     $s =~ /^\s*#\s*?/ ||
2903                                     $s =~ /^\s*$Ident\s*:/) {
2904                                         $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
2905                                         if ($s =~ s/^.*?\n//) {
2906                                                 $cond_lines++;
2907                                         }
2908                                 }
2909                         }
2910
2911                         my (undef, $sindent) = line_stats("+" . $s);
2912                         my $stat_real = raw_line($linenr, $cond_lines);
2913
2914                         # Check if either of these lines are modified, else
2915                         # this is not this patch's fault.
2916                         if (!defined($stat_real) ||
2917                             $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2918                                 $check = 0;
2919                         }
2920                         if (defined($stat_real) && $cond_lines > 1) {
2921                                 $stat_real = "[...]\n$stat_real";
2922                         }
2923
2924                         #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";
2925
2926                         if ($check && (($sindent % 8) != 0 ||
2927                             ($sindent <= $indent && $s ne ''))) {
2928                                 WARN("SUSPECT_CODE_INDENT",
2929                                      "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2930                         }
2931                 }
2932
2933                 # Track the 'values' across context and added lines.
2934                 my $opline = $line; $opline =~ s/^./ /;
2935                 my ($curr_values, $curr_vars) =
2936                                 annotate_values($opline . "\n", $prev_values);
2937                 $curr_values = $prev_values . $curr_values;
2938                 if ($dbg_values) {
2939                         my $outline = $opline; $outline =~ s/\t/ /g;
2940                         print "$linenr > .$outline\n";
2941                         print "$linenr > $curr_values\n";
2942                         print "$linenr >  $curr_vars\n";
2943                 }
2944                 $prev_values = substr($curr_values, -1);
2945
2946 #ignore lines not being added
2947                 next if ($line =~ /^[^\+]/);
2948
2949 # TEST: allow direct testing of the type matcher.
2950                 if ($dbg_type) {
2951                         if ($line =~ /^.\s*$Declare\s*$/) {
2952                                 ERROR("TEST_TYPE",
2953                                       "TEST: is type\n" . $herecurr);
2954                         } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2955                                 ERROR("TEST_NOT_TYPE",
2956                                       "TEST: is not type ($1 is)\n". $herecurr);
2957                         }
2958                         next;
2959                 }
2960 # TEST: allow direct testing of the attribute matcher.
2961                 if ($dbg_attr) {
2962                         if ($line =~ /^.\s*$Modifier\s*$/) {
2963                                 ERROR("TEST_ATTR",
2964                                       "TEST: is attr\n" . $herecurr);
2965                         } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2966                                 ERROR("TEST_NOT_ATTR",
2967                                       "TEST: is not attr ($1 is)\n". $herecurr);
2968                         }
2969                         next;
2970                 }
2971
2972 # check for initialisation to aggregates open brace on the next line
2973                 if ($line =~ /^.\s*{/ &&
2974                     $prevline =~ /(?:^|[^=])=\s*$/) {
2975                         if (ERROR("OPEN_BRACE",
2976                                   "that open brace { should be on the previous line\n" . $hereprev) &&
2977                             $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2978                                 fix_delete_line($fixlinenr - 1, $prevrawline);
2979                                 fix_delete_line($fixlinenr, $rawline);
2980                                 my $fixedline = $prevrawline;
2981                                 $fixedline =~ s/\s*=\s*$/ = {/;
2982                                 fix_insert_line($fixlinenr, $fixedline);
2983                                 $fixedline = $line;
2984                                 $fixedline =~ s/^(.\s*){\s*/$1/;
2985                                 fix_insert_line($fixlinenr, $fixedline);
2986                         }
2987                 }
2988
2989 #
2990 # Checks which are anchored on the added line.
2991 #
2992
2993 # check for malformed paths in #include statements (uses RAW line)
2994                 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2995                         my $path = $1;
2996                         if ($path =~ m{//}) {
2997                                 ERROR("MALFORMED_INCLUDE",
2998                                       "malformed #include filename\n" . $herecurr);
2999                         }
3000                         if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3001                                 ERROR("UAPI_INCLUDE",
3002                                       "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3003                         }
3004                 }
3005
3006 # no C99 // comments
3007                 if ($line =~ m{//}) {
3008                         if (ERROR("C99_COMMENTS",
3009                                   "do not use C99 // comments\n" . $herecurr) &&
3010                             $fix) {
3011                                 my $line = $fixed[$fixlinenr];
3012                                 if ($line =~ /\/\/(.*)$/) {
3013                                         my $comment = trim($1);
3014                                         $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
3015                                 }
3016                         }
3017                 }
3018                 # Remove C99 comments.
3019                 $line =~ s@//.*@@;
3020                 $opline =~ s@//.*@@;
3021
3022 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3023 # the whole statement.
3024 #print "APW <$lines[$realline_next - 1]>\n";
3025                 if (defined $realline_next &&
3026                     exists $lines[$realline_next - 1] &&
3027                     !defined $suppress_export{$realline_next} &&
3028                     ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3029                      $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3030                         # Handle definitions which produce identifiers with
3031                         # a prefix:
3032                         #   XXX(foo);
3033                         #   EXPORT_SYMBOL(something_foo);
3034                         my $name = $1;
3035                         if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
3036                             $name =~ /^${Ident}_$2/) {
3037 #print "FOO C name<$name>\n";
3038                                 $suppress_export{$realline_next} = 1;
3039
3040                         } elsif ($stat !~ /(?:
3041                                 \n.}\s*$|
3042                                 ^.DEFINE_$Ident\(\Q$name\E\)|
3043                                 ^.DECLARE_$Ident\(\Q$name\E\)|
3044                                 ^.LIST_HEAD\(\Q$name\E\)|
3045                                 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3046                                 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
3047                             )/x) {
3048 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3049                                 $suppress_export{$realline_next} = 2;
3050                         } else {
3051                                 $suppress_export{$realline_next} = 1;
3052                         }
3053                 }
3054                 if (!defined $suppress_export{$linenr} &&
3055                     $prevline =~ /^.\s*$/ &&
3056                     ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3057                      $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3058 #print "FOO B <$lines[$linenr - 1]>\n";
3059                         $suppress_export{$linenr} = 2;
3060                 }
3061                 if (defined $suppress_export{$linenr} &&
3062                     $suppress_export{$linenr} == 2) {
3063                         WARN("EXPORT_SYMBOL",
3064                              "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
3065                 }
3066
3067 # check for global initialisers.
3068                 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3069                         if (ERROR("GLOBAL_INITIALISERS",
3070                                   "do not initialise globals to 0 or NULL\n" .
3071                                       $herecurr) &&
3072                             $fix) {
3073                                 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3074                         }
3075                 }
3076 # check for static initialisers.
3077                 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3078                         if (ERROR("INITIALISED_STATIC",
3079                                   "do not initialise statics to 0 or NULL\n" .
3080                                       $herecurr) &&
3081                             $fix) {
3082                                 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3083                         }
3084                 }
3085
3086 # check for misordered declarations of char/short/int/long with signed/unsigned
3087                 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3088                         my $tmp = trim($1);
3089                         WARN("MISORDERED_TYPE",
3090                              "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3091                 }
3092
3093 # check for static const char * arrays.
3094                 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3095                         WARN("STATIC_CONST_CHAR_ARRAY",
3096                              "static const char * array should probably be static const char * const\n" .
3097                                 $herecurr);
3098                }
3099
3100 # check for static char foo[] = "bar" declarations.
3101                 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3102                         WARN("STATIC_CONST_CHAR_ARRAY",
3103                              "static char array declaration should probably be static const char\n" .
3104                                 $herecurr);
3105                }
3106
3107 # check for non-global char *foo[] = {"bar", ...} declarations.
3108                 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3109                         WARN("STATIC_CONST_CHAR_ARRAY",
3110                              "char * array declaration might be better as static const\n" .
3111                                 $herecurr);
3112                }
3113
3114 # check for function declarations without arguments like "int foo()"
3115                 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3116                         if (ERROR("FUNCTION_WITHOUT_ARGS",
3117                                   "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3118                             $fix) {
3119                                 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3120                         }
3121                 }
3122
3123 # check for uses of DEFINE_PCI_DEVICE_TABLE
3124                 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3125                         if (WARN("DEFINE_PCI_DEVICE_TABLE",
3126                                  "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3127                             $fix) {
3128                                 $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
3129                         }
3130                 }
3131
3132 # check for new typedefs, only function parameters and sparse annotations
3133 # make sense.
3134                 if ($line =~ /\btypedef\s/ &&
3135                     $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3136                     $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3137                     $line !~ /\b$typeTypedefs\b/ &&
3138                     $line !~ /\b__bitwise(?:__|)\b/) {
3139                         WARN("NEW_TYPEDEFS",
3140                              "do not add new typedefs\n" . $herecurr);
3141                 }
3142
3143 # * goes on variable not on type
3144                 # (char*[ const])
3145                 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3146                         #print "AA<$1>\n";
3147                         my ($ident, $from, $to) = ($1, $2, $2);
3148
3149                         # Should start with a space.
3150                         $to =~ s/^(\S)/ $1/;
3151                         # Should not end with a space.
3152                         $to =~ s/\s+$//;
3153                         # '*'s should not have spaces between.
3154                         while ($to =~ s/\*\s+\*/\*\*/) {
3155                         }
3156
3157 ##                      print "1: from<$from> to<$to> ident<$ident>\n";
3158                         if ($from ne $to) {
3159                                 if (ERROR("POINTER_LOCATION",
3160                                           "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
3161                                     $fix) {
3162                                         my $sub_from = $ident;
3163                                         my $sub_to = $ident;
3164                                         $sub_to =~ s/\Q$from\E/$to/;
3165                                         $fixed[$fixlinenr] =~
3166                                             s@\Q$sub_from\E@$sub_to@;
3167                                 }
3168                         }
3169                 }
3170                 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3171                         #print "BB<$1>\n";
3172                         my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3173
3174                         # Should start with a space.
3175                         $to =~ s/^(\S)/ $1/;
3176                         # Should not end with a space.
3177                         $to =~ s/\s+$//;
3178                         # '*'s should not have spaces between.
3179                         while ($to =~ s/\*\s+\*/\*\*/) {
3180                         }
3181                         # Modifiers should have spaces.
3182                         $to =~ s/(\b$Modifier$)/$1 /;
3183
3184 ##                      print "2: from<$from> to<$to> ident<$ident>\n";
3185                         if ($from ne $to && $ident !~ /^$Modifier$/) {
3186                                 if (ERROR("POINTER_LOCATION",
3187                                           "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
3188                                     $fix) {
3189
3190                                         my $sub_from = $match;
3191                                         my $sub_to = $match;
3192                                         $sub_to =~ s/\Q$from\E/$to/;
3193                                         $fixed[$fixlinenr] =~
3194                                             s@\Q$sub_from\E@$sub_to@;
3195                                 }
3196                         }
3197                 }
3198
3199 # # no BUG() or BUG_ON()
3200 #               if ($line =~ /\b(BUG|BUG_ON)\b/) {
3201 #                       print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3202 #                       print "$herecurr";
3203 #                       $clean = 0;
3204 #               }
3205
3206                 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3207                         WARN("LINUX_VERSION_CODE",
3208                              "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
3209                 }
3210
3211 # check for uses of printk_ratelimit
3212                 if ($line =~ /\bprintk_ratelimit\s*\(/) {
3213                         WARN("PRINTK_RATELIMITED",
3214 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
3215                 }
3216
3217 # printk should use KERN_* levels.  Note that follow on printk's on the
3218 # same line do not need a level, so we use the current block context
3219 # to try and find and validate the current printk.  In summary the current
3220 # printk includes all preceding printk's which have no newline on the end.
3221 # we assume the first bad printk is the one to report.
3222                 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
3223                         my $ok = 0;
3224                         for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3225                                 #print "CHECK<$lines[$ln - 1]\n";
3226                                 # we have a preceding printk if it ends
3227                                 # with "\n" ignore it, else it is to blame
3228                                 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3229                                         if ($rawlines[$ln - 1] !~ m{\\n"}) {
3230                                                 $ok = 1;
3231                                         }
3232                                         last;
3233                                 }
3234                         }
3235                         if ($ok == 0) {
3236                                 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3237                                      "printk() should include KERN_ facility level\n" . $herecurr);
3238                         }
3239                 }
3240
3241                 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3242                         my $orig = $1;
3243                         my $level = lc($orig);
3244                         $level = "warn" if ($level eq "warning");
3245                         my $level2 = $level;
3246                         $level2 = "dbg" if ($level eq "debug");
3247                         WARN("PREFER_PR_LEVEL",
3248                              "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3249                 }
3250
3251                 if ($line =~ /\bpr_warning\s*\(/) {
3252                         if (WARN("PREFER_PR_LEVEL",
3253                                  "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3254                             $fix) {
3255                                 $fixed[$fixlinenr] =~
3256                                     s/\bpr_warning\b/pr_warn/;
3257                         }
3258                 }
3259
3260                 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3261                         my $orig = $1;
3262                         my $level = lc($orig);
3263                         $level = "warn" if ($level eq "warning");
3264                         $level = "dbg" if ($level eq "debug");
3265                         WARN("PREFER_DEV_LEVEL",
3266                              "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3267                 }
3268
3269 # function brace can't be on same line, except for #defines of do while,
3270 # or if closed on same line
3271                 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3272                     !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
3273                         if (ERROR("OPEN_BRACE",
3274                                   "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3275                             $fix) {
3276                                 fix_delete_line($fixlinenr, $rawline);
3277                                 my $fixed_line = $rawline;
3278                                 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3279                                 my $line1 = $1;
3280                                 my $line2 = $2;
3281                                 fix_insert_line($fixlinenr, ltrim($line1));
3282                                 fix_insert_line($fixlinenr, "\+{");
3283                                 if ($line2 !~ /^\s*$/) {
3284                                         fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3285                                 }
3286                         }
3287                 }
3288
3289 # open braces for enum, union and struct go on the same line.
3290                 if ($line =~ /^.\s*{/ &&
3291                     $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
3292                         if (ERROR("OPEN_BRACE",
3293                                   "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3294                             $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3295                                 fix_delete_line($fixlinenr - 1, $prevrawline);
3296                                 fix_delete_line($fixlinenr, $rawline);
3297                                 my $fixedline = rtrim($prevrawline) . " {";
3298                                 fix_insert_line($fixlinenr, $fixedline);
3299                                 $fixedline = $rawline;
3300                                 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3301                                 if ($fixedline !~ /^\+\s*$/) {
3302                                         fix_insert_line($fixlinenr, $fixedline);
3303                                 }
3304                         }
3305                 }
3306
3307 # missing space after union, struct or enum definition
3308                 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3309                         if (WARN("SPACING",
3310                                  "missing space after $1 definition\n" . $herecurr) &&
3311                             $fix) {
3312                                 $fixed[$fixlinenr] =~
3313                                     s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3314                         }
3315                 }
3316
3317 # Function pointer declarations
3318 # check spacing between type, funcptr, and args
3319 # canonical declaration is "type (*funcptr)(args...)"
3320                 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
3321                         my $declare = $1;
3322                         my $pre_pointer_space = $2;
3323                         my $post_pointer_space = $3;
3324                         my $funcname = $4;
3325                         my $post_funcname_space = $5;
3326                         my $pre_args_space = $6;
3327
3328 # the $Declare variable will capture all spaces after the type
3329 # so check it for a missing trailing missing space but pointer return types
3330 # don't need a space so don't warn for those.
3331                         my $post_declare_space = "";
3332                         if ($declare =~ /(\s+)$/) {
3333                                 $post_declare_space = $1;
3334                                 $declare = rtrim($declare);
3335                         }
3336                         if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
3337                                 WARN("SPACING",
3338                                      "missing space after return type\n" . $herecurr);
3339                                 $post_declare_space = " ";
3340                         }
3341
3342 # unnecessary space "type  (*funcptr)(args...)"
3343 # This test is not currently implemented because these declarations are
3344 # equivalent to
3345 #       int  foo(int bar, ...)
3346 # and this is form shouldn't/doesn't generate a checkpatch warning.
3347 #
3348 #                       elsif ($declare =~ /\s{2,}$/) {
3349 #                               WARN("SPACING",
3350 #                                    "Multiple spaces after return type\n" . $herecurr);
3351 #                       }
3352
3353 # unnecessary space "type ( *funcptr)(args...)"
3354                         if (defined $pre_pointer_space &&
3355                             $pre_pointer_space =~ /^\s/) {
3356                                 WARN("SPACING",
3357                                      "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3358                         }
3359
3360 # unnecessary space "type (* funcptr)(args...)"
3361                         if (defined $post_pointer_space &&
3362                             $post_pointer_space =~ /^\s/) {
3363                                 WARN("SPACING",
3364                                      "Unnecessary space before function pointer name\n" . $herecurr);
3365                         }
3366
3367 # unnecessary space "type (*funcptr )(args...)"
3368                         if (defined $post_funcname_space &&
3369                             $post_funcname_space =~ /^\s/) {
3370                                 WARN("SPACING",
3371                                      "Unnecessary space after function pointer name\n" . $herecurr);
3372                         }
3373
3374 # unnecessary space "type (*funcptr) (args...)"
3375                         if (defined $pre_args_space &&
3376                             $pre_args_space =~ /^\s/) {
3377                                 WARN("SPACING",
3378                                      "Unnecessary space before function pointer arguments\n" . $herecurr);
3379                         }
3380
3381                         if (show_type("SPACING") && $fix) {
3382                                 $fixed[$fixlinenr] =~
3383                                     s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
3384                         }
3385                 }
3386
3387 # check for spacing round square brackets; allowed:
3388 #  1. with a type on the left -- int [] a;
3389 #  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3390 #  3. inside a curly brace -- = { [0...10] = 5 }
3391                 while ($line =~ /(.*?\s)\[/g) {
3392                         my ($where, $prefix) = ($-[1], $1);
3393                         if ($prefix !~ /$Type\s+$/ &&
3394                             ($where != 0 || $prefix !~ /^.\s+$/) &&
3395                             $prefix !~ /[{,]\s+$/) {
3396                                 if (ERROR("BRACKET_SPACE",
3397                                           "space prohibited before open square bracket '['\n" . $herecurr) &&
3398                                     $fix) {
3399                                     $fixed[$fixlinenr] =~
3400                                         s/^(\+.*?)\s+\[/$1\[/;
3401                                 }
3402                         }
3403                 }
3404
3405 # check for spaces between functions and their parentheses.
3406                 while ($line =~ /($Ident)\s+\(/g) {
3407                         my $name = $1;
3408                         my $ctx_before = substr($line, 0, $-[1]);
3409                         my $ctx = "$ctx_before$name";
3410
3411                         # Ignore those directives where spaces _are_ permitted.
3412                         if ($name =~ /^(?:
3413                                 if|for|while|switch|return|case|
3414                                 volatile|__volatile__|
3415                                 __attribute__|format|__extension__|
3416                                 asm|__asm__)$/x)
3417                         {
3418                         # cpp #define statements have non-optional spaces, ie
3419                         # if there is a space between the name and the open
3420                         # parenthesis it is simply not a parameter group.
3421                         } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3422
3423                         # cpp #elif statement condition may start with a (
3424                         } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3425
3426                         # If this whole things ends with a type its most
3427                         # likely a typedef for a function.
3428                         } elsif ($ctx =~ /$Type$/) {
3429
3430                         } else {
3431                                 if (WARN("SPACING",
3432                                          "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3433                                              $fix) {
3434                                         $fixed[$fixlinenr] =~
3435                                             s/\b$name\s+\(/$name\(/;
3436                                 }
3437                         }
3438                 }
3439
3440 # Check operator spacing.
3441                 if (!($line=~/\#\s*include/)) {
3442                         my $fixed_line = "";
3443                         my $line_fixed = 0;
3444
3445                         my $ops = qr{
3446                                 <<=|>>=|<=|>=|==|!=|
3447                                 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3448                                 =>|->|<<|>>|<|>|=|!|~|
3449                                 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
3450                                 \?:|\?|:
3451                         }x;
3452                         my @elements = split(/($ops|;)/, $opline);
3453
3454 ##                      print("element count: <" . $#elements . ">\n");
3455 ##                      foreach my $el (@elements) {
3456 ##                              print("el: <$el>\n");
3457 ##                      }
3458
3459                         my @fix_elements = ();
3460                         my $off = 0;
3461
3462                         foreach my $el (@elements) {
3463                                 push(@fix_elements, substr($rawline, $off, length($el)));
3464                                 $off += length($el);
3465                         }
3466
3467                         $off = 0;
3468
3469                         my $blank = copy_spacing($opline);
3470                         my $last_after = -1;
3471
3472                         for (my $n = 0; $n < $#elements; $n += 2) {
3473
3474                                 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3475
3476 ##                              print("n: <$n> good: <$good>\n");
3477
3478                                 $off += length($elements[$n]);
3479
3480                                 # Pick up the preceding and succeeding characters.
3481                                 my $ca = substr($opline, 0, $off);
3482                                 my $cc = '';
3483                                 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3484                                         $cc = substr($opline, $off + length($elements[$n + 1]));
3485                                 }
3486                                 my $cb = "$ca$;$cc";
3487
3488                                 my $a = '';
3489                                 $a = 'V' if ($elements[$n] ne '');
3490                                 $a = 'W' if ($elements[$n] =~ /\s$/);
3491                                 $a = 'C' if ($elements[$n] =~ /$;$/);
3492                                 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3493                                 $a = 'O' if ($elements[$n] eq '');
3494                                 $a = 'E' if ($ca =~ /^\s*$/);
3495
3496                                 my $op = $elements[$n + 1];
3497
3498                                 my $c = '';
3499                                 if (defined $elements[$n + 2]) {
3500                                         $c = 'V' if ($elements[$n + 2] ne '');
3501                                         $c = 'W' if ($elements[$n + 2] =~ /^\s/);
3502                                         $c = 'C' if ($elements[$n + 2] =~ /^$;/);
3503                                         $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3504                                         $c = 'O' if ($elements[$n + 2] eq '');
3505                                         $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
3506                                 } else {
3507                                         $c = 'E';
3508                                 }
3509
3510                                 my $ctx = "${a}x${c}";
3511
3512                                 my $at = "(ctx:$ctx)";
3513
3514                                 my $ptr = substr($blank, 0, $off) . "^";
3515                                 my $hereptr = "$hereline$ptr\n";
3516
3517                                 # Pull out the value of this operator.
3518                                 my $op_type = substr($curr_values, $off + 1, 1);
3519
3520                                 # Get the full operator variant.
3521                                 my $opv = $op . substr($curr_vars, $off, 1);
3522
3523                                 # Ignore operators passed as parameters.
3524                                 if ($op_type ne 'V' &&
3525                                     $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3526
3527 #                               # Ignore comments
3528 #                               } elsif ($op =~ /^$;+$/) {
3529
3530                                 # ; should have either the end of line or a space or \ after it
3531                                 } elsif ($op eq ';') {
3532                                         if ($ctx !~ /.x[WEBC]/ &&
3533                                             $cc !~ /^\\/ && $cc !~ /^;/) {
3534                                                 if (ERROR("SPACING",
3535                                                           "space required after that '$op' $at\n" . $hereptr)) {
3536                                                         $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3537                                                         $line_fixed = 1;
3538                                                 }
3539                                         }
3540
3541                                 # // is a comment
3542                                 } elsif ($op eq '//') {
3543
3544                                 #   :   when part of a bitfield
3545                                 } elsif ($opv eq ':B') {
3546                                         # skip the bitfield test for now
3547
3548                                 # No spaces for:
3549                                 #   ->
3550                                 } elsif ($op eq '->') {
3551                                         if ($ctx =~ /Wx.|.xW/) {
3552                                                 if (ERROR("SPACING",
3553                                                           "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3554                                                         $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3555                                                         if (defined $fix_elements[$n + 2]) {
3556                                                                 $fix_elements[$n + 2] =~ s/^\s+//;
3557                                                         }
3558                                                         $line_fixed = 1;
3559                                                 }
3560                                         }
3561
3562                                 # , must have a space on the right.
3563                                 } elsif ($op eq ',') {
3564                                         if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
3565                                                 if (ERROR("SPACING",
3566                                                           "space required after that '$op' $at\n" . $hereptr)) {
3567                                                         $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3568                                                         $line_fixed = 1;
3569                                                         $last_after = $n;
3570                                                 }
3571                                         }
3572
3573                                 # '*' as part of a type definition -- reported already.
3574                                 } elsif ($opv eq '*_') {
3575                                         #warn "'*' is part of type\n";
3576
3577                                 # unary operators should have a space before and
3578                                 # none after.  May be left adjacent to another
3579                                 # unary operator, or a cast
3580                                 } elsif ($op eq '!' || $op eq '~' ||
3581                                          $opv eq '*U' || $opv eq '-U' ||
3582                                          $opv eq '&U' || $opv eq '&&U') {
3583                                         if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
3584                                                 if (ERROR("SPACING",
3585                                                           "space required before that '$op' $at\n" . $hereptr)) {
3586                                                         if ($n != $last_after + 2) {
3587                                                                 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3588                                                                 $line_fixed = 1;
3589                                                         }
3590                                                 }
3591                                         }
3592                                         if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3593                                                 # A unary '*' may be const
3594
3595                                         } elsif ($ctx =~ /.xW/) {
3596                                                 if (ERROR("SPACING",
3597                                                           "space prohibited after that '$op' $at\n" . $hereptr)) {
3598                                                         $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
3599                                                         if (defined $fix_elements[$n + 2]) {
3600                                                                 $fix_elements[$n + 2] =~ s/^\s+//;
3601                                                         }
3602                                                         $line_fixed = 1;
3603                                                 }
3604                                         }
3605
3606                                 # unary ++ and unary -- are allowed no space on one side.
3607                                 } elsif ($op eq '++' or $op eq '--') {
3608                                         if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
3609                                                 if (ERROR("SPACING",
3610                                                           "space required one side of that '$op' $at\n" . $hereptr)) {
3611                                                         $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3612                                                         $line_fixed = 1;
3613                                                 }
3614                                         }
3615                                         if ($ctx =~ /Wx[BE]/ ||
3616                                             ($ctx =~ /Wx./ && $cc =~ /^;/)) {
3617                                                 if (ERROR("SPACING",
3618                                                           "space prohibited before that '$op' $at\n" . $hereptr)) {
3619                                                         $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3620                                                         $line_fixed = 1;
3621                                                 }
3622                                         }
3623                                         if ($ctx =~ /ExW/) {
3624                                                 if (ERROR("SPACING",
3625                                                           "space prohibited after that '$op' $at\n" . $hereptr)) {
3626                                                         $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3627                                                         if (defined $fix_elements[$n + 2]) {
3628                                                                 $fix_elements[$n + 2] =~ s/^\s+//;
3629                                                         }
3630                                                         $line_fixed = 1;
3631                                                 }
3632                                         }
3633
3634                                 # << and >> may either have or not have spaces both sides
3635                                 } elsif ($op eq '<<' or $op eq '>>' or
3636                                          $op eq '&' or $op eq '^' or $op eq '|' or
3637                                          $op eq '+' or $op eq '-' or
3638                                          $op eq '*' or $op eq '/' or
3639                                          $op eq '%')
3640                                 {
3641                                         if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
3642                                                 if (ERROR("SPACING",
3643                                                           "need consistent spacing around '$op' $at\n" . $hereptr)) {
3644                                                         $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3645                                                         if (defined $fix_elements[$n + 2]) {
3646                                                                 $fix_elements[$n + 2] =~ s/^\s+//;
3647                                                         }
3648                                                         $line_fixed = 1;
3649                                                 }
3650                                         }
3651
3652                                 # A colon needs no spaces before when it is
3653                                 # terminating a case value or a label.
3654                                 } elsif ($opv eq ':C' || $opv eq ':L') {
3655                                         if ($ctx =~ /Wx./) {
3656                                                 if (ERROR("SPACING",
3657                                                           "space prohibited before that '$op' $at\n" . $hereptr)) {
3658                                                         $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3659                                                         $line_fixed = 1;
3660                                                 }
3661                                         }
3662
3663                                 # All the others need spaces both sides.
3664                                 } elsif ($ctx !~ /[EWC]x[CWE]/) {
3665                                         my $ok = 0;
3666
3667                                         # Ignore email addresses <foo@bar>
3668                                         if (($op eq '<' &&
3669                                              $cc =~ /^\S+\@\S+>/) ||
3670                                             ($op eq '>' &&
3671                                              $ca =~ /<\S+\@\S+$/))
3672                                         {
3673                                                 $ok = 1;
3674                                         }
3675
3676                                         # messages are ERROR, but ?: are CHK
3677                                         if ($ok == 0) {
3678                                                 my $msg_type = \&ERROR;
3679                                                 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3680
3681                                                 if (&{$msg_type}("SPACING",
3682                                                                  "spaces required around that '$op' $at\n" . $hereptr)) {
3683                                                         $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3684                                                         if (defined $fix_elements[$n + 2]) {
3685                                                                 $fix_elements[$n + 2] =~ s/^\s+//;
3686                                                         }
3687                                                         $line_fixed = 1;
3688                                                 }
3689                                         }
3690                                 }
3691                                 $off += length($elements[$n + 1]);
3692
3693 ##                              print("n: <$n> GOOD: <$good>\n");
3694
3695                                 $fixed_line = $fixed_line . $good;
3696                         }
3697
3698                         if (($#elements % 2) == 0) {
3699                                 $fixed_line = $fixed_line . $fix_elements[$#elements];
3700                         }
3701
3702                         if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3703                                 $fixed[$fixlinenr] = $fixed_line;
3704                         }
3705
3706
3707                 }
3708
3709 # check for whitespace before a non-naked semicolon
3710                 if ($line =~ /^\+.*\S\s+;\s*$/) {
3711                         if (WARN("SPACING",
3712                                  "space prohibited before semicolon\n" . $herecurr) &&
3713                             $fix) {
3714                                 1 while $fixed[$fixlinenr] =~
3715                                     s/^(\+.*\S)\s+;/$1;/;
3716                         }
3717                 }
3718
3719 # check for multiple assignments
3720                 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3721                         CHK("MULTIPLE_ASSIGNMENTS",
3722                             "multiple assignments should be avoided\n" . $herecurr);
3723                 }
3724
3725 ## # check for multiple declarations, allowing for a function declaration
3726 ## # continuation.
3727 ##              if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3728 ##                  $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3729 ##
3730 ##                      # Remove any bracketed sections to ensure we do not
3731 ##                      # falsly report the parameters of functions.
3732 ##                      my $ln = $line;
3733 ##                      while ($ln =~ s/\([^\(\)]*\)//g) {
3734 ##                      }
3735 ##                      if ($ln =~ /,/) {
3736 ##                              WARN("MULTIPLE_DECLARATION",
3737 ##                                   "declaring multiple variables together should be avoided\n" . $herecurr);
3738 ##                      }
3739 ##              }
3740
3741 #need space before brace following if, while, etc
3742                 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3743                     $line =~ /do{/) {
3744                         if (ERROR("SPACING",
3745                                   "space required before the open brace '{'\n" . $herecurr) &&
3746                             $fix) {
3747                                 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
3748                         }
3749                 }
3750
3751 ## # check for blank lines before declarations
3752 ##              if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3753 ##                  $prevrawline =~ /^.\s*$/) {
3754 ##                      WARN("SPACING",
3755 ##                           "No blank lines before declarations\n" . $hereprev);
3756 ##              }
3757 ##
3758
3759 # closing brace should have a space following it when it has anything
3760 # on the line
3761                 if ($line =~ /}(?!(?:,|;|\)))\S/) {
3762                         if (ERROR("SPACING",
3763                                   "space required after that close brace '}'\n" . $herecurr) &&
3764                             $fix) {
3765                                 $fixed[$fixlinenr] =~
3766                                     s/}((?!(?:,|;|\)))\S)/} $1/;
3767                         }
3768                 }
3769
3770 # check spacing on square brackets
3771                 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
3772                         if (ERROR("SPACING",
3773                                   "space prohibited after that open square bracket '['\n" . $herecurr) &&
3774                             $fix) {
3775                                 $fixed[$fixlinenr] =~
3776                                     s/\[\s+/\[/;
3777                         }
3778                 }
3779                 if ($line =~ /\s\]/) {
3780                         if (ERROR("SPACING",
3781                                   "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3782                             $fix) {
3783                                 $fixed[$fixlinenr] =~
3784                                     s/\s+\]/\]/;
3785                         }
3786                 }
3787
3788 # check spacing on parentheses
3789                 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3790                     $line !~ /for\s*\(\s+;/) {
3791                         if (ERROR("SPACING",
3792                                   "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3793                             $fix) {
3794                                 $fixed[$fixlinenr] =~
3795                                     s/\(\s+/\(/;
3796                         }
3797                 }
3798                 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3799                     $line !~ /for\s*\(.*;\s+\)/ &&
3800                     $line !~ /:\s+\)/) {
3801                         if (ERROR("SPACING",
3802                                   "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3803                             $fix) {
3804                                 $fixed[$fixlinenr] =~
3805                                     s/\s+\)/\)/;
3806                         }
3807                 }
3808
3809 # check unnecessary parentheses around addressof/dereference single $Lvals
3810 # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3811
3812                 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3813                         CHK("UNNECESSARY_PARENTHESES",
3814                             "Unnecessary parentheses around $1\n" . $herecurr);
3815                     }
3816
3817 #goto labels aren't indented, allow a single space however
3818                 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
3819                    !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
3820                         if (WARN("INDENTED_LABEL",
3821                                  "labels should not be indented\n" . $herecurr) &&
3822                             $fix) {
3823                                 $fixed[$fixlinenr] =~
3824                                     s/^(.)\s+/$1/;
3825                         }
3826                 }
3827
3828 # return is not a function
3829                 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3830                         my $spacing = $1;
3831                         if ($^V && $^V ge 5.10.0 &&
3832                             $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3833                                 my $value = $1;
3834                                 $value = deparenthesize($value);
3835                                 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3836                                         ERROR("RETURN_PARENTHESES",
3837                                               "return is not a function, parentheses are not required\n" . $herecurr);
3838                                 }
3839                         } elsif ($spacing !~ /\s+/) {
3840                                 ERROR("SPACING",
3841                                       "space required before the open parenthesis '('\n" . $herecurr);
3842                         }
3843                 }
3844
3845 # unnecessary return in a void function
3846 # at end-of-function, with the previous line a single leading tab, then return;
3847 # and the line before that not a goto label target like "out:"
3848                 if ($sline =~ /^[ \+]}\s*$/ &&
3849                     $prevline =~ /^\+\treturn\s*;\s*$/ &&
3850                     $linenr >= 3 &&
3851                     $lines[$linenr - 3] =~ /^[ +]/ &&
3852                     $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
3853                         WARN("RETURN_VOID",
3854                              "void function return statements are not generally useful\n" . $hereprev);
3855                }
3856
3857 # if statements using unnecessary parentheses - ie: if ((foo == bar))
3858                 if ($^V && $^V ge 5.10.0 &&
3859                     $line =~ /\bif\s*((?:\(\s*){2,})/) {
3860                         my $openparens = $1;
3861                         my $count = $openparens =~ tr@\(@\(@;
3862                         my $msg = "";
3863                         if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3864                                 my $comp = $4;  #Not $1 because of $LvalOrFunc
3865                                 $msg = " - maybe == should be = ?" if ($comp eq "==");
3866                                 WARN("UNNECESSARY_PARENTHESES",
3867                                      "Unnecessary parentheses$msg\n" . $herecurr);
3868                         }
3869                 }
3870
3871 # Return of what appears to be an errno should normally be -'ve
3872                 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3873                         my $name = $1;
3874                         if ($name ne 'EOF' && $name ne 'ERROR') {
3875                                 WARN("USE_NEGATIVE_ERRNO",
3876                                      "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
3877                         }
3878                 }
3879
3880 # Need a space before open parenthesis after if, while etc
3881                 if ($line =~ /\b(if|while|for|switch)\(/) {
3882                         if (ERROR("SPACING",
3883                                   "space required before the open parenthesis '('\n" . $herecurr) &&
3884                             $fix) {
3885                                 $fixed[$fixlinenr] =~
3886                                     s/\b(if|while|for|switch)\(/$1 \(/;
3887                         }
3888                 }
3889
3890 # Check for illegal assignment in if conditional -- and check for trailing
3891 # statements after the conditional.
3892                 if ($line =~ /do\s*(?!{)/) {
3893                         ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3894                                 ctx_statement_block($linenr, $realcnt, 0)
3895                                         if (!defined $stat);
3896                         my ($stat_next) = ctx_statement_block($line_nr_next,
3897                                                 $remain_next, $off_next);
3898                         $stat_next =~ s/\n./\n /g;
3899                         ##print "stat<$stat> stat_next<$stat_next>\n";
3900
3901                         if ($stat_next =~ /^\s*while\b/) {
3902                                 # If the statement carries leading newlines,
3903                                 # then count those as offsets.
3904                                 my ($whitespace) =
3905                                         ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3906                                 my $offset =
3907                                         statement_rawlines($whitespace) - 1;
3908
3909                                 $suppress_whiletrailers{$line_nr_next +
3910                                                                 $offset} = 1;
3911                         }
3912                 }
3913                 if (!defined $suppress_whiletrailers{$linenr} &&
3914                     defined($stat) && defined($cond) &&
3915                     $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3916                         my ($s, $c) = ($stat, $cond);
3917
3918                         if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3919                                 ERROR("ASSIGN_IN_IF",
3920                                       "do not use assignment in if condition\n" . $herecurr);
3921                         }
3922
3923                         # Find out what is on the end of the line after the
3924                         # conditional.
3925                         substr($s, 0, length($c), '');
3926                         $s =~ s/\n.*//g;
3927                         $s =~ s/$;//g;  # Remove any comments
3928                         if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3929                             $c !~ /}\s*while\s*/)
3930                         {
3931                                 # Find out how long the conditional actually is.
3932                                 my @newlines = ($c =~ /\n/gs);
3933                                 my $cond_lines = 1 + $#newlines;
3934                                 my $stat_real = '';
3935
3936                                 $stat_real = raw_line($linenr, $cond_lines)
3937                                                         . "\n" if ($cond_lines);
3938                                 if (defined($stat_real) && $cond_lines > 1) {
3939                                         $stat_real = "[...]\n$stat_real";
3940                                 }
3941
3942                                 ERROR("TRAILING_STATEMENTS",
3943                                       "trailing statements should be on next line\n" . $herecurr . $stat_real);
3944                         }
3945                 }
3946
3947 # Check for bitwise tests written as boolean
3948                 if ($line =~ /
3949                         (?:
3950                                 (?:\[|\(|\&\&|\|\|)
3951                                 \s*0[xX][0-9]+\s*
3952                                 (?:\&\&|\|\|)
3953                         |
3954                                 (?:\&\&|\|\|)
3955                                 \s*0[xX][0-9]+\s*
3956                                 (?:\&\&|\|\||\)|\])
3957                         )/x)
3958                 {
3959                         WARN("HEXADECIMAL_BOOLEAN_TEST",
3960                              "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
3961                 }
3962
3963 # if and else should not have general statements after it
3964                 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3965                         my $s = $1;
3966                         $s =~ s/$;//g;  # Remove any comments
3967                         if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3968                                 ERROR("TRAILING_STATEMENTS",
3969                                       "trailing statements should be on next line\n" . $herecurr);
3970                         }
3971                 }
3972 # if should not continue a brace
3973                 if ($line =~ /}\s*if\b/) {
3974                         ERROR("TRAILING_STATEMENTS",
3975                               "trailing statements should be on next line (or did you mean 'else if'?)\n" .
3976                                 $herecurr);
3977                 }
3978 # case and default should not have general statements after them
3979                 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3980                     $line !~ /\G(?:
3981                         (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3982                         \s*return\s+
3983                     )/xg)
3984                 {
3985                         ERROR("TRAILING_STATEMENTS",
3986                               "trailing statements should be on next line\n" . $herecurr);
3987                 }
3988
3989                 # Check for }<nl>else {, these must be at the same
3990                 # indent level to be relevant to each other.
3991                 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
3992                     $previndent == $indent) {
3993                         if (ERROR("ELSE_AFTER_BRACE",
3994                                   "else should follow close brace '}'\n" . $hereprev) &&
3995                             $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3996                                 fix_delete_line($fixlinenr - 1, $prevrawline);
3997                                 fix_delete_line($fixlinenr, $rawline);
3998                                 my $fixedline = $prevrawline;
3999                                 $fixedline =~ s/}\s*$//;
4000                                 if ($fixedline !~ /^\+\s*$/) {
4001                                         fix_insert_line($fixlinenr, $fixedline);
4002                                 }
4003                                 $fixedline = $rawline;
4004                                 $fixedline =~ s/^(.\s*)else/$1} else/;
4005                                 fix_insert_line($fixlinenr, $fixedline);
4006                         }
4007                 }
4008
4009                 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4010                     $previndent == $indent) {
4011                         my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4012
4013                         # Find out what is on the end of the line after the
4014                         # conditional.
4015                         substr($s, 0, length($c), '');
4016                         $s =~ s/\n.*//g;
4017
4018                         if ($s =~ /^\s*;/) {
4019                                 if (ERROR("WHILE_AFTER_BRACE",
4020                                           "while should follow close brace '}'\n" . $hereprev) &&
4021                                     $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4022                                         fix_delete_line($fixlinenr - 1, $prevrawline);
4023                                         fix_delete_line($fixlinenr, $rawline);
4024                                         my $fixedline = $prevrawline;
4025                                         my $trailing = $rawline;
4026                                         $trailing =~ s/^\+//;
4027                                         $trailing = trim($trailing);
4028                                         $fixedline =~ s/}\s*$/} $trailing/;
4029                                         fix_insert_line($fixlinenr, $fixedline);
4030                                 }
4031                         }
4032                 }
4033
4034 #Specific variable tests
4035                 while ($line =~ m{($Constant|$Lval)}g) {
4036                         my $var = $1;
4037
4038 #gcc binary extension
4039                         if ($var =~ /^$Binary$/) {
4040                                 if (WARN("GCC_BINARY_CONSTANT",
4041                                          "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4042                                     $fix) {
4043                                         my $hexval = sprintf("0x%x", oct($var));
4044                                         $fixed[$fixlinenr] =~
4045                                             s/\b$var\b/$hexval/;
4046                                 }
4047                         }
4048
4049 #CamelCase
4050                         if ($var !~ /^$Constant$/ &&
4051                             $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
4052 #Ignore Page<foo> variants
4053                             $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
4054 #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4055                             $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
4056                                 while ($var =~ m{($Ident)}g) {
4057                                         my $word = $1;
4058                                         next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4059                                         if ($check) {
4060                                                 seed_camelcase_includes();
4061                                                 if (!$file && !$camelcase_file_seeded) {
4062                                                         seed_camelcase_file($realfile);
4063                                                         $camelcase_file_seeded = 1;
4064                                                 }
4065                                         }
4066                                         if (!defined $camelcase{$word}) {
4067                                                 $camelcase{$word} = 1;
4068                                                 CHK("CAMELCASE",
4069                                                     "Avoid CamelCase: <$word>\n" . $herecurr);
4070                                         }
4071                                 }
4072                         }
4073                 }
4074
4075 #no spaces allowed after \ in define
4076                 if ($line =~ /\#\s*define.*\\\s+$/) {
4077                         if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4078                                  "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4079                             $fix) {
4080                                 $fixed[$fixlinenr] =~ s/\s+$//;
4081                         }
4082                 }
4083
4084 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4085                 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4086                         my $file = "$1.h";
4087                         my $checkfile = "include/linux/$file";
4088                         if (-f "$root/$checkfile" &&
4089                             $realfile ne $checkfile &&
4090                             $1 !~ /$allowed_asm_includes/)
4091                         {
4092                                 if ($realfile =~ m{^arch/}) {
4093                                         CHK("ARCH_INCLUDE_LINUX",
4094                                             "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4095                                 } else {
4096                                         WARN("INCLUDE_LINUX",
4097                                              "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4098                                 }
4099                         }
4100                 }
4101
4102 # multi-statement macros should be enclosed in a do while loop, grab the
4103 # first statement and ensure its the whole macro if its not enclosed
4104 # in a known good container
4105                 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4106                     $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4107                         my $ln = $linenr;
4108                         my $cnt = $realcnt;
4109                         my ($off, $dstat, $dcond, $rest);
4110                         my $ctx = '';
4111                         my $has_flow_statement = 0;
4112                         my $has_arg_concat = 0;
4113                         ($dstat, $dcond, $ln, $cnt, $off) =
4114                                 ctx_statement_block($linenr, $realcnt, 0);
4115                         $ctx = $dstat;
4116                         #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4117                         #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4118
4119                         $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4120                         $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4121
4122                         $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4123                         $dstat =~ s/$;//g;
4124                         $dstat =~ s/\\\n.//g;
4125                         $dstat =~ s/^\s*//s;
4126                         $dstat =~ s/\s*$//s;
4127
4128                         # Flatten any parentheses and braces
4129                         while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4130                                $dstat =~ s/\{[^\{\}]*\}/1/ ||
4131                                $dstat =~ s/\[[^\[\]]*\]/1/)
4132                         {
4133                         }
4134
4135                         # Flatten any obvious string concatentation.
4136                         while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4137                                $dstat =~ s/$Ident\s*("X*")/$1/)
4138                         {
4139                         }
4140
4141                         my $exceptions = qr{
4142                                 $Declare|
4143                                 module_param_named|
4144                                 MODULE_PARM_DESC|
4145                                 DECLARE_PER_CPU|
4146                                 DEFINE_PER_CPU|
4147                                 __typeof__\(|
4148                                 union|
4149                                 struct|
4150                                 \.$Ident\s*=\s*|
4151                                 ^\"|\"$
4152                         }x;
4153                         #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4154                         if ($dstat ne '' &&
4155                             $dstat !~ /^(?:$Ident|-?$Constant),$/ &&                    # 10, // foo(),
4156                             $dstat !~ /^(?:$Ident|-?$Constant);$/ &&                    # foo();
4157                             $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&          # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4158                             $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&                  # character constants
4159                             $dstat !~ /$exceptions/ &&
4160                             $dstat !~ /^\.$Ident\s*=/ &&                                # .foo =
4161                             $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&          # stringification #foo
4162                             $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&       # do {...} while (...); // do {...} while (...)
4163                             $dstat !~ /^for\s*$Constant$/ &&                            # for (...)
4164                             $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&   # for (...) bar()
4165                             $dstat !~ /^do\s*{/ &&                                      # do {...
4166                             $dstat !~ /^\({/ &&                                         # ({...
4167                             $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4168                         {
4169                                 $ctx =~ s/\n*$//;
4170                                 my $herectx = $here . "\n";
4171                                 my $cnt = statement_rawlines($ctx);
4172
4173                                 for (my $n = 0; $n < $cnt; $n++) {
4174                                         $herectx .= raw_line($linenr, $n) . "\n";
4175                                 }
4176
4177                                 if ($dstat =~ /;/) {
4178                                         ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4179                                               "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4180                                 } else {
4181                                         ERROR("COMPLEX_MACRO",
4182                                               "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4183                                 }
4184                         }
4185
4186 # check for macros with flow control, but without ## concatenation
4187 # ## concatenation is commonly a macro that defines a function so ignore those
4188                         if ($has_flow_statement && !$has_arg_concat) {
4189                                 my $herectx = $here . "\n";
4190                                 my $cnt = statement_rawlines($ctx);
4191
4192                                 for (my $n = 0; $n < $cnt; $n++) {
4193                                         $herectx .= raw_line($linenr, $n) . "\n";
4194                                 }
4195                                 WARN("MACRO_WITH_FLOW_CONTROL",
4196                                      "Macros with flow control statements should be avoided\n" . "$herectx");
4197                         }
4198
4199 # check for line continuations outside of #defines, preprocessor #, and asm
4200
4201                 } else {
4202                         if ($prevline !~ /^..*\\$/ &&
4203                             $line !~ /^\+\s*\#.*\\$/ &&         # preprocessor
4204                             $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&   # asm
4205                             $line =~ /^\+.*\\$/) {
4206                                 WARN("LINE_CONTINUATIONS",
4207                                      "Avoid unnecessary line continuations\n" . $herecurr);
4208                         }
4209                 }
4210
4211 # do {} while (0) macro tests:
4212 # single-statement macros do not need to be enclosed in do while (0) loop,
4213 # macro should not end with a semicolon
4214                 if ($^V && $^V ge 5.10.0 &&
4215                     $realfile !~ m@/vmlinux.lds.h$@ &&
4216                     $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4217                         my $ln = $linenr;
4218                         my $cnt = $realcnt;
4219                         my ($off, $dstat, $dcond, $rest);
4220                         my $ctx = '';
4221                         ($dstat, $dcond, $ln, $cnt, $off) =
4222                                 ctx_statement_block($linenr, $realcnt, 0);
4223                         $ctx = $dstat;
4224
4225                         $dstat =~ s/\\\n.//g;
4226
4227                         if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4228                                 my $stmts = $2;
4229                                 my $semis = $3;
4230
4231                                 $ctx =~ s/\n*$//;
4232                                 my $cnt = statement_rawlines($ctx);
4233                                 my $herectx = $here . "\n";
4234
4235                                 for (my $n = 0; $n < $cnt; $n++) {
4236                                         $herectx .= raw_line($linenr, $n) . "\n";
4237                                 }
4238
4239                                 if (($stmts =~ tr/;/;/) == 1 &&
4240                                     $stmts !~ /^\s*(if|while|for|switch)\b/) {
4241                                         WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4242                                              "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4243                                 }
4244                                 if (defined $semis && $semis ne "") {
4245                                         WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4246                                              "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4247                                 }
4248                         } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4249                                 $ctx =~ s/\n*$//;
4250                                 my $cnt = statement_rawlines($ctx);
4251                                 my $herectx = $here . "\n";
4252
4253                                 for (my $n = 0; $n < $cnt; $n++) {
4254                                         $herectx .= raw_line($linenr, $n) . "\n";
4255                                 }
4256
4257                                 WARN("TRAILING_SEMICOLON",
4258                                      "macros should not use a trailing semicolon\n" . "$herectx");
4259                         }
4260                 }
4261
4262 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4263 # all assignments may have only one of the following with an assignment:
4264 #       .
4265 #       ALIGN(...)
4266 #       VMLINUX_SYMBOL(...)
4267                 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4268                         WARN("MISSING_VMLINUX_SYMBOL",
4269                              "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4270                 }
4271
4272 # check for redundant bracing round if etc
4273                 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4274                         my ($level, $endln, @chunks) =
4275                                 ctx_statement_full($linenr, $realcnt, 1);
4276                         #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4277                         #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4278                         if ($#chunks > 0 && $level == 0) {
4279                                 my @allowed = ();
4280                                 my $allow = 0;
4281                                 my $seen = 0;
4282                                 my $herectx = $here . "\n";
4283                                 my $ln = $linenr - 1;
4284                                 for my $chunk (@chunks) {
4285                                         my ($cond, $block) = @{$chunk};
4286
4287                                         # If the condition carries leading newlines, then count those as offsets.
4288                                         my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4289                                         my $offset = statement_rawlines($whitespace) - 1;
4290
4291                                         $allowed[$allow] = 0;
4292                                         #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4293
4294                                         # We have looked at and allowed this specific line.
4295                                         $suppress_ifbraces{$ln + $offset} = 1;
4296
4297                                         $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4298                                         $ln += statement_rawlines($block) - 1;
4299
4300                                         substr($block, 0, length($cond), '');
4301
4302                                         $seen++ if ($block =~ /^\s*{/);
4303
4304                                         #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4305                                         if (statement_lines($cond) > 1) {
4306                                                 #print "APW: ALLOWED: cond<$cond>\n";
4307                                                 $allowed[$allow] = 1;
4308                                         }
4309                                         if ($block =~/\b(?:if|for|while)\b/) {
4310                                                 #print "APW: ALLOWED: block<$block>\n";
4311                                                 $allowed[$allow] = 1;
4312                                         }
4313                                         if (statement_block_size($block) > 1) {
4314                                                 #print "APW: ALLOWED: lines block<$block>\n";
4315                                                 $allowed[$allow] = 1;
4316                                         }
4317                                         $allow++;
4318                                 }
4319                                 if ($seen) {
4320                                         my $sum_allowed = 0;
4321                                         foreach (@allowed) {
4322                                                 $sum_allowed += $_;
4323                                         }
4324                                         if ($sum_allowed == 0) {
4325                                                 WARN("BRACES",
4326                                                      "braces {} are not necessary for any arm of this statement\n" . $herectx);
4327                                         } elsif ($sum_allowed != $allow &&
4328                                                  $seen != $allow) {
4329                                                 CHK("BRACES",
4330                                                     "braces {} should be used on all arms of this statement\n" . $herectx);
4331                                         }
4332                                 }
4333                         }
4334                 }
4335                 if (!defined $suppress_ifbraces{$linenr - 1} &&
4336                                         $line =~ /\b(if|while|for|else)\b/) {
4337                         my $allowed = 0;
4338
4339                         # Check the pre-context.
4340                         if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4341                                 #print "APW: ALLOWED: pre<$1>\n";
4342                                 $allowed = 1;
4343                         }
4344
4345                         my ($level, $endln, @chunks) =
4346                                 ctx_statement_full($linenr, $realcnt, $-[0]);
4347
4348                         # Check the condition.
4349                         my ($cond, $block) = @{$chunks[0]};
4350                         #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4351                         if (defined $cond) {
4352                                 substr($block, 0, length($cond), '');
4353                         }
4354                         if (statement_lines($cond) > 1) {
4355                                 #print "APW: ALLOWED: cond<$cond>\n";
4356                                 $allowed = 1;
4357                         }
4358                         if ($block =~/\b(?:if|for|while)\b/) {
4359                                 #print "APW: ALLOWED: block<$block>\n";
4360                                 $allowed = 1;
4361                         }
4362                         if (statement_block_size($block) > 1) {
4363                                 #print "APW: ALLOWED: lines block<$block>\n";
4364                                 $allowed = 1;
4365                         }
4366                         # Check the post-context.
4367                         if (defined $chunks[1]) {
4368                                 my ($cond, $block) = @{$chunks[1]};
4369                                 if (defined $cond) {
4370                                         substr($block, 0, length($cond), '');
4371                                 }
4372                                 if ($block =~ /^\s*\{/) {
4373                                         #print "APW: ALLOWED: chunk-1 block<$block>\n";
4374                                         $allowed = 1;
4375                                 }
4376                         }
4377                         if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
4378                                 my $herectx = $here . "\n";
4379                                 my $cnt = statement_rawlines($block);
4380
4381                                 for (my $n = 0; $n < $cnt; $n++) {
4382                                         $herectx .= raw_line($linenr, $n) . "\n";
4383                                 }
4384
4385                                 WARN("BRACES",
4386                                      "braces {} are not necessary for single statement blocks\n" . $herectx);
4387                         }
4388                 }
4389
4390 # check for unnecessary blank lines around braces
4391                 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4392                         CHK("BRACES",
4393                             "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4394                 }
4395                 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4396                         CHK("BRACES",
4397                             "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4398                 }
4399
4400 # no volatiles please
4401                 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4402                 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4403                         WARN("VOLATILE",
4404                              "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4405                 }
4406
4407 # concatenated string without spaces between elements
4408                 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4409                         CHK("CONCATENATED_STRING",
4410                             "Concatenated strings should use spaces between elements\n" . $herecurr);
4411                 }
4412
4413 # warn about #if 0
4414                 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4415                         CHK("REDUNDANT_CODE",
4416                             "if this code is redundant consider removing it\n" .
4417                                 $herecurr);
4418                 }
4419
4420 # check for needless "if (<foo>) fn(<foo>)" uses
4421                 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4422                         my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4423                         if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4424                                 WARN('NEEDLESS_IF',
4425                                      "$1(NULL) is safe this check is probably not required\n" . $hereprev);
4426                         }
4427                 }
4428
4429 # check for unnecessary "Out of Memory" messages
4430                 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4431                     $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4432                     (defined $1 || defined $3) &&
4433                     $linenr > 3) {
4434                         my $testval = $2;
4435                         my $testline = $lines[$linenr - 3];
4436
4437                         my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4438 #                       print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4439
4440                         if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4441                                 WARN("OOM_MESSAGE",
4442                                      "Possible unnecessary 'out of memory' message\n" . $hereprev);
4443                         }
4444                 }
4445
4446 # check for bad placement of section $InitAttribute (e.g.: __initdata)
4447                 if ($line =~ /(\b$InitAttribute\b)/) {
4448                         my $attr = $1;
4449                         if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4450                                 my $ptr = $1;
4451                                 my $var = $2;
4452                                 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4453                                       ERROR("MISPLACED_INIT",
4454                                             "$attr should be placed after $var\n" . $herecurr)) ||
4455                                      ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4456                                       WARN("MISPLACED_INIT",
4457                                            "$attr should be placed after $var\n" . $herecurr))) &&
4458                                     $fix) {
4459                                         $fixed[$fixlinenr] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
4460                                 }
4461                         }
4462                 }
4463
4464 # check for $InitAttributeData (ie: __initdata) with const
4465                 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4466                         my $attr = $1;
4467                         $attr =~ /($InitAttributePrefix)(.*)/;
4468                         my $attr_prefix = $1;
4469                         my $attr_type = $2;
4470                         if (ERROR("INIT_ATTRIBUTE",
4471                                   "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4472                             $fix) {
4473                                 $fixed[$fixlinenr] =~
4474                                     s/$InitAttributeData/${attr_prefix}initconst/;
4475                         }
4476                 }
4477
4478 # check for $InitAttributeConst (ie: __initconst) without const
4479                 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4480                         my $attr = $1;
4481                         if (ERROR("INIT_ATTRIBUTE",
4482                                   "Use of $attr requires a separate use of const\n" . $herecurr) &&
4483                             $fix) {
4484                                 my $lead = $fixed[$fixlinenr] =~
4485                                     /(^\+\s*(?:static\s+))/;
4486                                 $lead = rtrim($1);
4487                                 $lead = "$lead " if ($lead !~ /^\+$/);
4488                                 $lead = "${lead}const ";
4489                                 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4490                         }
4491                 }
4492
4493 # don't use __constant_<foo> functions outside of include/uapi/
4494                 if ($realfile !~ m@^include/uapi/@ &&
4495                     $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4496                         my $constant_func = $1;
4497                         my $func = $constant_func;
4498                         $func =~ s/^__constant_//;
4499                         if (WARN("CONSTANT_CONVERSION",
4500                                  "$constant_func should be $func\n" . $herecurr) &&
4501                             $fix) {
4502                                 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4503                         }
4504                 }
4505
4506 # prefer usleep_range over udelay
4507                 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
4508                         my $delay = $1;
4509                         # ignore udelay's < 10, however
4510                         if (! ($delay < 10) ) {
4511                                 CHK("USLEEP_RANGE",
4512                                     "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4513                         }
4514                         if ($delay > 2000) {
4515                                 WARN("LONG_UDELAY",
4516                                      "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
4517                         }
4518                 }
4519
4520 # warn about unexpectedly long msleep's
4521                 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4522                         if ($1 < 20) {
4523                                 WARN("MSLEEP",
4524                                      "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4525                         }
4526                 }
4527
4528 # check for comparisons of jiffies
4529                 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4530                         WARN("JIFFIES_COMPARISON",
4531                              "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4532                 }
4533
4534 # check for comparisons of get_jiffies_64()
4535                 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4536                         WARN("JIFFIES_COMPARISON",
4537                              "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4538                 }
4539
4540 # warn about #ifdefs in C files
4541 #               if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
4542 #                       print "#ifdef in C files should be avoided\n";
4543 #                       print "$herecurr";
4544 #                       $clean = 0;
4545 #               }
4546
4547 # warn about spacing in #ifdefs
4548                 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
4549                         if (ERROR("SPACING",
4550                                   "exactly one space required after that #$1\n" . $herecurr) &&
4551                             $fix) {
4552                                 $fixed[$fixlinenr] =~
4553                                     s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4554                         }
4555
4556                 }
4557
4558 # check for spinlock_t definitions without a comment.
4559                 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4560                     $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
4561                         my $which = $1;
4562                         if (!ctx_has_comment($first_line, $linenr)) {
4563                                 CHK("UNCOMMENTED_DEFINITION",
4564                                     "$1 definition without comment\n" . $herecurr);
4565                         }
4566                 }
4567 # check for memory barriers without a comment.
4568                 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4569                         if (!ctx_has_comment($first_line, $linenr)) {
4570                                 WARN("MEMORY_BARRIER",
4571                                      "memory barrier without comment\n" . $herecurr);
4572                         }
4573                 }
4574 # check of hardware specific defines
4575                 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4576                         CHK("ARCH_DEFINES",
4577                             "architecture specific defines should be avoided\n" .  $herecurr);
4578                 }
4579
4580 # Check that the storage class is at the beginning of a declaration
4581                 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4582                         WARN("STORAGE_CLASS",
4583                              "storage class should be at the beginning of the declaration\n" . $herecurr)
4584                 }
4585
4586 # check the location of the inline attribute, that it is between
4587 # storage class and type.
4588                 if ($line =~ /\b$Type\s+$Inline\b/ ||
4589                     $line =~ /\b$Inline\s+$Storage\b/) {
4590                         ERROR("INLINE_LOCATION",
4591                               "inline keyword should sit between storage class and type\n" . $herecurr);
4592                 }
4593
4594 # Check for __inline__ and __inline, prefer inline
4595                 if ($realfile !~ m@\binclude/uapi/@ &&
4596                     $line =~ /\b(__inline__|__inline)\b/) {
4597                         if (WARN("INLINE",
4598                                  "plain inline is preferred over $1\n" . $herecurr) &&
4599                             $fix) {
4600                                 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4601
4602                         }
4603                 }
4604
4605 # Check for __attribute__ packed, prefer __packed
4606                 if ($realfile !~ m@\binclude/uapi/@ &&
4607                     $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4608                         WARN("PREFER_PACKED",
4609                              "__packed is preferred over __attribute__((packed))\n" . $herecurr);
4610                 }
4611
4612 # Check for __attribute__ aligned, prefer __aligned
4613                 if ($realfile !~ m@\binclude/uapi/@ &&
4614                     $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4615                         WARN("PREFER_ALIGNED",
4616                              "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
4617                 }
4618
4619 # Check for __attribute__ format(printf, prefer __printf
4620                 if ($realfile !~ m@\binclude/uapi/@ &&
4621                     $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4622                         if (WARN("PREFER_PRINTF",
4623                                  "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4624                             $fix) {
4625                                 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4626
4627                         }
4628                 }
4629
4630 # Check for __attribute__ format(scanf, prefer __scanf
4631                 if ($realfile !~ m@\binclude/uapi/@ &&
4632                     $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4633                         if (WARN("PREFER_SCANF",
4634                                  "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4635                             $fix) {
4636                                 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4637                         }
4638                 }
4639
4640 # check for sizeof(&)
4641                 if ($line =~ /\bsizeof\s*\(\s*\&/) {
4642                         WARN("SIZEOF_ADDRESS",
4643                              "sizeof(& should be avoided\n" . $herecurr);
4644                 }
4645
4646 # check for sizeof without parenthesis
4647                 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4648                         if (WARN("SIZEOF_PARENTHESIS",
4649                                  "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4650                             $fix) {
4651                                 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4652                         }
4653                 }
4654
4655 # check for line continuations in quoted strings with odd counts of "
4656                 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4657                         WARN("LINE_CONTINUATIONS",
4658                              "Avoid line continuations in quoted strings\n" . $herecurr);
4659                 }
4660
4661 # check for struct spinlock declarations
4662                 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4663                         WARN("USE_SPINLOCK_T",
4664                              "struct spinlock should be spinlock_t\n" . $herecurr);
4665                 }
4666
4667 # check for seq_printf uses that could be seq_puts
4668                 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4669                         my $fmt = get_quoted_string($line, $rawline);
4670                         if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4671                                 if (WARN("PREFER_SEQ_PUTS",
4672                                          "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4673                                     $fix) {
4674                                         $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4675                                 }
4676                         }
4677                 }
4678
4679 # Check for misused memsets
4680                 if ($^V && $^V ge 5.10.0 &&
4681                     defined $stat &&
4682                     $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4683
4684                         my $ms_addr = $2;
4685                         my $ms_val = $7;
4686                         my $ms_size = $12;
4687
4688                         if ($ms_size =~ /^(0x|)0$/i) {
4689                                 ERROR("MEMSET",
4690                                       "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4691                         } elsif ($ms_size =~ /^(0x|)1$/i) {
4692                                 WARN("MEMSET",
4693                                      "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4694                         }
4695                 }
4696
4697 # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4698                 if ($^V && $^V ge 5.10.0 &&
4699                     $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4700                         if (WARN("PREFER_ETHER_ADDR_COPY",
4701                                  "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4702                             $fix) {
4703                                 $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
4704                         }
4705                 }
4706
4707 # typecasts on min/max could be min_t/max_t
4708                 if ($^V && $^V ge 5.10.0 &&
4709                     defined $stat &&
4710                     $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4711                         if (defined $2 || defined $7) {
4712                                 my $call = $1;
4713                                 my $cast1 = deparenthesize($2);
4714                                 my $arg1 = $3;
4715                                 my $cast2 = deparenthesize($7);
4716                                 my $arg2 = $8;
4717                                 my $cast;
4718
4719                                 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4720                                         $cast = "$cast1 or $cast2";
4721                                 } elsif ($cast1 ne "") {
4722                                         $cast = $cast1;
4723                                 } else {
4724                                         $cast = $cast2;
4725                                 }
4726                                 WARN("MINMAX",
4727                                      "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4728                         }
4729                 }
4730
4731 # check usleep_range arguments
4732                 if ($^V && $^V ge 5.10.0 &&
4733                     defined $stat &&
4734                     $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4735                         my $min = $1;
4736                         my $max = $7;
4737                         if ($min eq $max) {
4738                                 WARN("USLEEP_RANGE",
4739                                      "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4740                         } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4741                                  $min > $max) {
4742                                 WARN("USLEEP_RANGE",
4743                                      "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4744                         }
4745                 }
4746
4747 # check for naked sscanf
4748                 if ($^V && $^V ge 5.10.0 &&
4749                     defined $stat &&
4750                     $line =~ /\bsscanf\b/ &&
4751                     ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4752                      $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4753                      $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4754                         my $lc = $stat =~ tr@\n@@;
4755                         $lc = $lc + $linenr;
4756                         my $stat_real = raw_line($linenr, 0);
4757                         for (my $count = $linenr + 1; $count <= $lc; $count++) {
4758                                 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4759                         }
4760                         WARN("NAKED_SSCANF",
4761                              "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4762                 }
4763
4764 # check for simple sscanf that should be kstrto<foo>
4765                 if ($^V && $^V ge 5.10.0 &&
4766                     defined $stat &&
4767                     $line =~ /\bsscanf\b/) {
4768                         my $lc = $stat =~ tr@\n@@;
4769                         $lc = $lc + $linenr;
4770                         my $stat_real = raw_line($linenr, 0);
4771                         for (my $count = $linenr + 1; $count <= $lc; $count++) {
4772                                 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4773                         }
4774                         if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4775                                 my $format = $6;
4776                                 my $count = $format =~ tr@%@%@;
4777                                 if ($count == 1 &&
4778                                     $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4779                                         WARN("SSCANF_TO_KSTRTO",
4780                                              "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4781                                 }
4782                         }
4783                 }
4784
4785 # check for new externs in .h files.
4786                 if ($realfile =~ /\.h$/ &&
4787                     $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4788                         if (CHK("AVOID_EXTERNS",
4789                                 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
4790                             $fix) {
4791                                 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
4792                         }
4793                 }
4794
4795 # check for new externs in .c files.
4796                 if ($realfile =~ /\.c$/ && defined $stat &&
4797                     $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4798                 {
4799                         my $function_name = $1;
4800                         my $paren_space = $2;
4801
4802                         my $s = $stat;
4803                         if (defined $cond) {
4804                                 substr($s, 0, length($cond), '');
4805                         }
4806                         if ($s =~ /^\s*;/ &&
4807                             $function_name ne 'uninitialized_var')
4808                         {
4809                                 WARN("AVOID_EXTERNS",
4810                                      "externs should be avoided in .c files\n" .  $herecurr);
4811                         }
4812
4813                         if ($paren_space =~ /\n/) {
4814                                 WARN("FUNCTION_ARGUMENTS",
4815                                      "arguments for function declarations should follow identifier\n" . $herecurr);
4816                         }
4817
4818                 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4819                     $stat =~ /^.\s*extern\s+/)
4820                 {
4821                         WARN("AVOID_EXTERNS",
4822                              "externs should be avoided in .c files\n" .  $herecurr);
4823                 }
4824
4825 # checks for new __setup's
4826                 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4827                         my $name = $1;
4828
4829                         if (!grep(/$name/, @setup_docs)) {
4830                                 CHK("UNDOCUMENTED_SETUP",
4831                                     "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4832                         }
4833                 }
4834
4835 # check for pointless casting of kmalloc return
4836                 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4837                         WARN("UNNECESSARY_CASTS",
4838                              "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
4839                 }
4840
4841 # alloc style
4842 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4843                 if ($^V && $^V ge 5.10.0 &&
4844                     $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4845                         CHK("ALLOC_SIZEOF_STRUCT",
4846                             "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4847                 }
4848
4849 # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
4850                 if ($^V && $^V ge 5.10.0 &&
4851                     $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
4852                         my $oldfunc = $3;
4853                         my $a1 = $4;
4854                         my $a2 = $10;
4855                         my $newfunc = "kmalloc_array";
4856                         $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
4857                         my $r1 = $a1;
4858                         my $r2 = $a2;
4859                         if ($a1 =~ /^sizeof\s*\S/) {
4860                                 $r1 = $a2;
4861                                 $r2 = $a1;
4862                         }
4863                         if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4864                             !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
4865                                 if (WARN("ALLOC_WITH_MULTIPLY",
4866                                          "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4867                                     $fix) {
4868                                         $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
4869
4870                                 }
4871                         }
4872                 }
4873
4874 # check for krealloc arg reuse
4875                 if ($^V && $^V ge 5.10.0 &&
4876                     $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4877                         WARN("KREALLOC_ARG_REUSE",
4878                              "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4879                 }
4880
4881 # check for alloc argument mismatch
4882                 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4883                         WARN("ALLOC_ARRAY_ARGS",
4884                              "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4885                 }
4886
4887 # check for multiple semicolons
4888                 if ($line =~ /;\s*;\s*$/) {
4889                         if (WARN("ONE_SEMICOLON",
4890                                  "Statements terminations use 1 semicolon\n" . $herecurr) &&
4891                             $fix) {
4892                                 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
4893                         }
4894                 }
4895
4896 # check for case / default statements not preceded by break/fallthrough/switch
4897                 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4898                         my $has_break = 0;
4899                         my $has_statement = 0;
4900                         my $count = 0;
4901                         my $prevline = $linenr;
4902                         while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
4903                                 $prevline--;
4904                                 my $rline = $rawlines[$prevline - 1];
4905                                 my $fline = $lines[$prevline - 1];
4906                                 last if ($fline =~ /^\@\@/);
4907                                 next if ($fline =~ /^\-/);
4908                                 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4909                                 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4910                                 next if ($fline =~ /^.[\s$;]*$/);
4911                                 $has_statement = 1;
4912                                 $count++;
4913                                 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4914                         }
4915                         if (!$has_break && $has_statement) {
4916                                 WARN("MISSING_BREAK",
4917                                      "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4918                         }
4919                 }
4920
4921 # check for switch/default statements without a break;
4922                 if ($^V && $^V ge 5.10.0 &&
4923                     defined $stat &&
4924                     $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4925                         my $ctx = '';
4926                         my $herectx = $here . "\n";
4927                         my $cnt = statement_rawlines($stat);
4928                         for (my $n = 0; $n < $cnt; $n++) {
4929                                 $herectx .= raw_line($linenr, $n) . "\n";
4930                         }
4931                         WARN("DEFAULT_NO_BREAK",
4932                              "switch default: should use break\n" . $herectx);
4933                 }
4934
4935 # check for gcc specific __FUNCTION__
4936                 if ($line =~ /\b__FUNCTION__\b/) {
4937                         if (WARN("USE_FUNC",
4938                                  "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
4939                             $fix) {
4940                                 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
4941                         }
4942                 }
4943
4944 # check for use of yield()
4945                 if ($line =~ /\byield\s*\(\s*\)/) {
4946                         WARN("YIELD",
4947                              "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
4948                 }
4949
4950 # check for comparisons against true and false
4951                 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4952                         my $lead = $1;
4953                         my $arg = $2;
4954                         my $test = $3;
4955                         my $otype = $4;
4956                         my $trail = $5;
4957                         my $op = "!";
4958
4959                         ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4960
4961                         my $type = lc($otype);
4962                         if ($type =~ /^(?:true|false)$/) {
4963                                 if (("$test" eq "==" && "$type" eq "true") ||
4964                                     ("$test" eq "!=" && "$type" eq "false")) {
4965                                         $op = "";
4966                                 }
4967
4968                                 CHK("BOOL_COMPARISON",
4969                                     "Using comparison to $otype is error prone\n" . $herecurr);
4970
4971 ## maybe suggesting a correct construct would better
4972 ##                                  "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4973
4974                         }
4975                 }
4976
4977 # check for semaphores initialized locked
4978                 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4979                         WARN("CONSIDER_COMPLETION",
4980                              "consider using a completion\n" . $herecurr);
4981                 }
4982
4983 # recommend kstrto* over simple_strto* and strict_strto*
4984                 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4985                         WARN("CONSIDER_KSTRTO",
4986                              "$1 is obsolete, use k$3 instead\n" . $herecurr);
4987                 }
4988
4989 # check for __initcall(), use device_initcall() explicitly or more appropriate function please
4990                 if ($line =~ /^.\s*__initcall\s*\(/) {
4991                         WARN("USE_DEVICE_INITCALL",
4992                              "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
4993                 }
4994
4995 # check for various ops structs, ensure they are const.
4996                 my $struct_ops = qr{acpi_dock_ops|
4997                                 address_space_operations|
4998                                 backlight_ops|
4999                                 block_device_operations|
5000                                 dentry_operations|
5001                                 dev_pm_ops|
5002                                 dma_map_ops|
5003                                 extent_io_ops|
5004                                 file_lock_operations|
5005                                 file_operations|
5006                                 hv_ops|
5007                                 ide_dma_ops|
5008                                 intel_dvo_dev_ops|
5009                                 item_operations|
5010                                 iwl_ops|
5011                                 kgdb_arch|
5012                                 kgdb_io|
5013                                 kset_uevent_ops|
5014                                 lock_manager_operations|
5015                                 microcode_ops|
5016                                 mtrr_ops|
5017                                 neigh_ops|
5018                                 nlmsvc_binding|
5019                                 pci_raw_ops|
5020                                 pipe_buf_operations|
5021                                 platform_hibernation_ops|
5022                                 platform_suspend_ops|
5023                                 proto_ops|
5024                                 rpc_pipe_ops|
5025                                 seq_operations|
5026                                 snd_ac97_build_ops|
5027                                 soc_pcmcia_socket_ops|
5028                                 stacktrace_ops|
5029                                 sysfs_ops|
5030                                 tty_operations|
5031                                 usb_mon_operations|
5032                                 wd_ops}x;
5033                 if ($line !~ /\bconst\b/ &&
5034                     $line =~ /\bstruct\s+($struct_ops)\b/) {
5035                         WARN("CONST_STRUCT",
5036                              "struct $1 should normally be const\n" .
5037                                 $herecurr);
5038                 }
5039
5040 # use of NR_CPUS is usually wrong
5041 # ignore definitions of NR_CPUS and usage to define arrays as likely right
5042                 if ($line =~ /\bNR_CPUS\b/ &&
5043                     $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5044                     $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5045                     $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5046                     $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5047                     $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5048                 {
5049                         WARN("NR_CPUS",
5050                              "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5051                 }
5052
5053 # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5054                 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5055                         ERROR("DEFINE_ARCH_HAS",
5056                               "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5057                 }
5058
5059 # check for %L{u,d,i} in strings
5060                 my $string;
5061                 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5062                         $string = substr($rawline, $-[1], $+[1] - $-[1]);
5063                         $string =~ s/%%/__/g;
5064                         if ($string =~ /(?<!%)%L[udi]/) {
5065                                 WARN("PRINTF_L",
5066                                      "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5067                                 last;
5068                         }
5069                 }
5070
5071 # whine mightly about in_atomic
5072                 if ($line =~ /\bin_atomic\s*\(/) {
5073                         if ($realfile =~ m@^drivers/@) {
5074                                 ERROR("IN_ATOMIC",
5075                                       "do not use in_atomic in drivers\n" . $herecurr);
5076                         } elsif ($realfile !~ m@^kernel/@) {
5077                                 WARN("IN_ATOMIC",
5078                                      "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5079                         }
5080                 }
5081
5082 # check for lockdep_set_novalidate_class
5083                 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5084                     $line =~ /__lockdep_no_validate__\s*\)/ ) {
5085                         if ($realfile !~ m@^kernel/lockdep@ &&
5086                             $realfile !~ m@^include/linux/lockdep@ &&
5087                             $realfile !~ m@^drivers/base/core@) {
5088                                 ERROR("LOCKDEP",
5089                                       "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
5090                         }
5091                 }
5092
5093                 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5094                     $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
5095                         WARN("EXPORTED_WORLD_WRITABLE",
5096                              "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
5097                 }
5098
5099 # Mode permission misuses where it seems decimal should be octal
5100 # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5101                 if ($^V && $^V ge 5.10.0 &&
5102                     $line =~ /$mode_perms_search/) {
5103                         foreach my $entry (@mode_permission_funcs) {
5104                                 my $func = $entry->[0];
5105                                 my $arg_pos = $entry->[1];
5106
5107                                 my $skip_args = "";
5108                                 if ($arg_pos > 1) {
5109                                         $arg_pos--;
5110                                         $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5111                                 }
5112                                 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5113                                 if ($line =~ /$test/) {
5114                                         my $val = $1;
5115                                         $val = $6 if ($skip_args ne "");
5116
5117                                         if ($val !~ /^0$/ &&
5118                                             (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5119                                              length($val) ne 4)) {
5120                                                 ERROR("NON_OCTAL_PERMISSIONS",
5121                                                       "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5122                                         }
5123                                 }
5124                         }
5125                 }
5126         }
5127
5128         # If we have no input at all, then there is nothing to report on
5129         # so just keep quiet.
5130         if ($#rawlines == -1) {
5131                 exit(0);
5132         }
5133
5134         # In mailback mode only produce a report in the negative, for
5135         # things that appear to be patches.
5136         if ($mailback && ($clean == 1 || !$is_patch)) {
5137                 exit(0);
5138         }
5139
5140         # This is not a patch, and we are are in 'no-patch' mode so
5141         # just keep quiet.
5142         if (!$chk_patch && !$is_patch) {
5143                 exit(0);
5144         }
5145
5146         if (!$is_patch) {
5147                 ERROR("NOT_UNIFIED_DIFF",
5148                       "Does not appear to be a unified-diff format patch\n");
5149         }
5150         if ($is_patch && $chk_signoff && $signoff == 0) {
5151                 ERROR("MISSING_SIGN_OFF",
5152                       "Missing Signed-off-by: line(s)\n");
5153         }
5154
5155         print report_dump();
5156         if ($summary && !($clean == 1 && $quiet == 1)) {
5157                 print "$filename " if ($summary_file);
5158                 print "total: $cnt_error errors, $cnt_warn warnings, " .
5159                         (($check)? "$cnt_chk checks, " : "") .
5160                         "$cnt_lines lines checked\n";
5161                 print "\n" if ($quiet == 0);
5162         }
5163
5164         if ($quiet == 0) {
5165
5166                 if ($^V lt 5.10.0) {
5167                         print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5168                         print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5169                 }
5170
5171                 # If there were whitespace errors which cleanpatch can fix
5172                 # then suggest that.
5173                 if ($rpt_cleaners) {
5174                         print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5175                         print "      scripts/cleanfile\n\n";
5176                         $rpt_cleaners = 0;
5177                 }
5178         }
5179
5180         hash_show_words(\%use_type, "Used");
5181         hash_show_words(\%ignore_type, "Ignored");
5182
5183         if ($clean == 0 && $fix &&
5184             ("@rawlines" ne "@fixed" ||
5185              $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
5186                 my $newfile = $filename;
5187                 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
5188                 my $linecount = 0;
5189                 my $f;
5190
5191                 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5192
5193                 open($f, '>', $newfile)
5194                     or die "$P: Can't open $newfile for write\n";
5195                 foreach my $fixed_line (@fixed) {
5196                         $linecount++;
5197                         if ($file) {
5198                                 if ($linecount > 3) {
5199                                         $fixed_line =~ s/^\+//;
5200                                         print $f $fixed_line . "\n";
5201                                 }
5202                         } else {
5203                                 print $f $fixed_line . "\n";
5204                         }
5205                 }
5206                 close($f);
5207
5208                 if (!$quiet) {
5209                         print << "EOM";
5210 Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5211
5212 Do _NOT_ trust the results written to this file.
5213 Do _NOT_ submit these changes without inspecting them for correctness.
5214
5215 This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5216 No warranties, expressed or implied...
5217
5218 EOM
5219                 }
5220         }
5221
5222         if ($clean == 1 && $quiet == 0) {
5223                 print "$vname has no obvious style problems and is ready for submission.\n"
5224         }
5225         if ($clean == 0 && $quiet == 0) {
5226                 print << "EOM";
5227 $vname has style problems, please review.
5228
5229 If any of these errors are false positives, please report
5230 them to the maintainer, see CHECKPATCH in MAINTAINERS.
5231 EOM
5232         }
5233
5234         return $clean;
5235 }