eb534d48140e2384bb61278f6a7e7f907f2b1d02
[muen/linux.git] / scripts / checkpatch.pl
1 #!/usr/bin/env perl
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 warnings;
10 use POSIX;
11 use File::Basename;
12 use Cwd 'abs_path';
13 use Term::ANSIColor qw(:constants);
14
15 my $P = $0;
16 my $D = dirname(abs_path($P));
17
18 my $V = '0.32';
19
20 use Getopt::Long qw(:config no_auto_abbrev);
21
22 my $quiet = 0;
23 my $tree = 1;
24 my $chk_signoff = 1;
25 my $chk_patch = 1;
26 my $tst_only;
27 my $emacs = 0;
28 my $terse = 0;
29 my $showfile = 0;
30 my $file = 0;
31 my $git = 0;
32 my %git_commits = ();
33 my $check = 0;
34 my $check_orig = 0;
35 my $summary = 1;
36 my $mailback = 0;
37 my $summary_file = 0;
38 my $show_types = 0;
39 my $list_types = 0;
40 my $fix = 0;
41 my $fix_inplace = 0;
42 my $root;
43 my %debug;
44 my %camelcase = ();
45 my %use_type = ();
46 my @use = ();
47 my %ignore_type = ();
48 my @ignore = ();
49 my $help = 0;
50 my $configuration_file = ".checkpatch.conf";
51 my $max_line_length = 80;
52 my $ignore_perl_version = 0;
53 my $minimum_perl_version = 5.10.0;
54 my $min_conf_desc_length = 4;
55 my $spelling_file = "$D/spelling.txt";
56 my $codespell = 0;
57 my $codespellfile = "/usr/share/codespell/dictionary.txt";
58 my $conststructsfile = "$D/const_structs.checkpatch";
59 my $typedefsfile = "";
60 my $color = "auto";
61 my $allow_c99_comments = 1;
62
63 sub help {
64         my ($exitcode) = @_;
65
66         print << "EOM";
67 Usage: $P [OPTION]... [FILE]...
68 Version: $V
69
70 Options:
71   -q, --quiet                quiet
72   --no-tree                  run without a kernel tree
73   --no-signoff               do not check for 'Signed-off-by' line
74   --patch                    treat FILE as patchfile (default)
75   --emacs                    emacs compile window format
76   --terse                    one line per report
77   --showfile                 emit diffed file position, not input file position
78   -g, --git                  treat FILE as a single commit or git revision range
79                              single git commit with:
80                                <rev>
81                                <rev>^
82                                <rev>~n
83                              multiple git commits with:
84                                <rev1>..<rev2>
85                                <rev1>...<rev2>
86                                <rev>-<count>
87                              git merges are ignored
88   -f, --file                 treat FILE as regular source file
89   --subjective, --strict     enable more subjective tests
90   --list-types               list the possible message types
91   --types TYPE(,TYPE2...)    show only these comma separated message types
92   --ignore TYPE(,TYPE2...)   ignore various comma separated message types
93   --show-types               show the specific message type in the output
94   --max-line-length=n        set the maximum line length, if exceeded, warn
95   --min-conf-desc-length=n   set the min description length, if shorter, warn
96   --root=PATH                PATH to the kernel tree root
97   --no-summary               suppress the per-file summary
98   --mailback                 only produce a report in case of warnings/errors
99   --summary-file             include the filename in summary
100   --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
101                              'values', 'possible', 'type', and 'attr' (default
102                              is all off)
103   --test-only=WORD           report only warnings/errors containing WORD
104                              literally
105   --fix                      EXPERIMENTAL - may create horrible results
106                              If correctable single-line errors exist, create
107                              "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
108                              with potential errors corrected to the preferred
109                              checkpatch style
110   --fix-inplace              EXPERIMENTAL - may create horrible results
111                              Is the same as --fix, but overwrites the input
112                              file.  It's your fault if there's no backup or git
113   --ignore-perl-version      override checking of perl version.  expect
114                              runtime errors.
115   --codespell                Use the codespell dictionary for spelling/typos
116                              (default:/usr/share/codespell/dictionary.txt)
117   --codespellfile            Use this codespell dictionary
118   --typedefsfile             Read additional types from this file
119   --color[=WHEN]             Use colors 'always', 'never', or only when output
120                              is a terminal ('auto'). Default is 'auto'.
121   -h, --help, --version      display this help and exit
122
123 When FILE is - read standard input.
124 EOM
125
126         exit($exitcode);
127 }
128
129 sub uniq {
130         my %seen;
131         return grep { !$seen{$_}++ } @_;
132 }
133
134 sub list_types {
135         my ($exitcode) = @_;
136
137         my $count = 0;
138
139         local $/ = undef;
140
141         open(my $script, '<', abs_path($P)) or
142             die "$P: Can't read '$P' $!\n";
143
144         my $text = <$script>;
145         close($script);
146
147         my @types = ();
148         # Also catch when type or level is passed through a variable
149         for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
150                 push (@types, $_);
151         }
152         @types = sort(uniq(@types));
153         print("#\tMessage type\n\n");
154         foreach my $type (@types) {
155                 print(++$count . "\t" . $type . "\n");
156         }
157
158         exit($exitcode);
159 }
160
161 my $conf = which_conf($configuration_file);
162 if (-f $conf) {
163         my @conf_args;
164         open(my $conffile, '<', "$conf")
165             or warn "$P: Can't find a readable $configuration_file file $!\n";
166
167         while (<$conffile>) {
168                 my $line = $_;
169
170                 $line =~ s/\s*\n?$//g;
171                 $line =~ s/^\s*//g;
172                 $line =~ s/\s+/ /g;
173
174                 next if ($line =~ m/^\s*#/);
175                 next if ($line =~ m/^\s*$/);
176
177                 my @words = split(" ", $line);
178                 foreach my $word (@words) {
179                         last if ($word =~ m/^#/);
180                         push (@conf_args, $word);
181                 }
182         }
183         close($conffile);
184         unshift(@ARGV, @conf_args) if @conf_args;
185 }
186
187 # Perl's Getopt::Long allows options to take optional arguments after a space.
188 # Prevent --color by itself from consuming other arguments
189 foreach (@ARGV) {
190         if ($_ eq "--color" || $_ eq "-color") {
191                 $_ = "--color=$color";
192         }
193 }
194
195 GetOptions(
196         'q|quiet+'      => \$quiet,
197         'tree!'         => \$tree,
198         'signoff!'      => \$chk_signoff,
199         'patch!'        => \$chk_patch,
200         'emacs!'        => \$emacs,
201         'terse!'        => \$terse,
202         'showfile!'     => \$showfile,
203         'f|file!'       => \$file,
204         'g|git!'        => \$git,
205         'subjective!'   => \$check,
206         'strict!'       => \$check,
207         'ignore=s'      => \@ignore,
208         'types=s'       => \@use,
209         'show-types!'   => \$show_types,
210         'list-types!'   => \$list_types,
211         'max-line-length=i' => \$max_line_length,
212         'min-conf-desc-length=i' => \$min_conf_desc_length,
213         'root=s'        => \$root,
214         'summary!'      => \$summary,
215         'mailback!'     => \$mailback,
216         'summary-file!' => \$summary_file,
217         'fix!'          => \$fix,
218         'fix-inplace!'  => \$fix_inplace,
219         'ignore-perl-version!' => \$ignore_perl_version,
220         'debug=s'       => \%debug,
221         'test-only=s'   => \$tst_only,
222         'codespell!'    => \$codespell,
223         'codespellfile=s'       => \$codespellfile,
224         'typedefsfile=s'        => \$typedefsfile,
225         'color=s'       => \$color,
226         'no-color'      => \$color,     #keep old behaviors of -nocolor
227         'nocolor'       => \$color,     #keep old behaviors of -nocolor
228         'h|help'        => \$help,
229         'version'       => \$help
230 ) or help(1);
231
232 help(0) if ($help);
233
234 list_types(0) if ($list_types);
235
236 $fix = 1 if ($fix_inplace);
237 $check_orig = $check;
238
239 my $exit = 0;
240
241 if ($^V && $^V lt $minimum_perl_version) {
242         printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
243         if (!$ignore_perl_version) {
244                 exit(1);
245         }
246 }
247
248 #if no filenames are given, push '-' to read patch from stdin
249 if ($#ARGV < 0) {
250         push(@ARGV, '-');
251 }
252
253 if ($color =~ /^[01]$/) {
254         $color = !$color;
255 } elsif ($color =~ /^always$/i) {
256         $color = 1;
257 } elsif ($color =~ /^never$/i) {
258         $color = 0;
259 } elsif ($color =~ /^auto$/i) {
260         $color = (-t STDOUT);
261 } else {
262         die "Invalid color mode: $color\n";
263 }
264
265 sub hash_save_array_words {
266         my ($hashRef, $arrayRef) = @_;
267
268         my @array = split(/,/, join(',', @$arrayRef));
269         foreach my $word (@array) {
270                 $word =~ s/\s*\n?$//g;
271                 $word =~ s/^\s*//g;
272                 $word =~ s/\s+/ /g;
273                 $word =~ tr/[a-z]/[A-Z]/;
274
275                 next if ($word =~ m/^\s*#/);
276                 next if ($word =~ m/^\s*$/);
277
278                 $hashRef->{$word}++;
279         }
280 }
281
282 sub hash_show_words {
283         my ($hashRef, $prefix) = @_;
284
285         if (keys %$hashRef) {
286                 print "\nNOTE: $prefix message types:";
287                 foreach my $word (sort keys %$hashRef) {
288                         print " $word";
289                 }
290                 print "\n";
291         }
292 }
293
294 hash_save_array_words(\%ignore_type, \@ignore);
295 hash_save_array_words(\%use_type, \@use);
296
297 my $dbg_values = 0;
298 my $dbg_possible = 0;
299 my $dbg_type = 0;
300 my $dbg_attr = 0;
301 for my $key (keys %debug) {
302         ## no critic
303         eval "\${dbg_$key} = '$debug{$key}';";
304         die "$@" if ($@);
305 }
306
307 my $rpt_cleaners = 0;
308
309 if ($terse) {
310         $emacs = 1;
311         $quiet++;
312 }
313
314 if ($tree) {
315         if (defined $root) {
316                 if (!top_of_kernel_tree($root)) {
317                         die "$P: $root: --root does not point at a valid tree\n";
318                 }
319         } else {
320                 if (top_of_kernel_tree('.')) {
321                         $root = '.';
322                 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
323                                                 top_of_kernel_tree($1)) {
324                         $root = $1;
325                 }
326         }
327
328         if (!defined $root) {
329                 print "Must be run from the top-level dir. of a kernel tree\n";
330                 exit(2);
331         }
332 }
333
334 my $emitted_corrupt = 0;
335
336 our $Ident      = qr{
337                         [A-Za-z_][A-Za-z\d_]*
338                         (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
339                 }x;
340 our $Storage    = qr{extern|static|asmlinkage};
341 our $Sparse     = qr{
342                         __user|
343                         __kernel|
344                         __force|
345                         __iomem|
346                         __must_check|
347                         __init_refok|
348                         __kprobes|
349                         __ref|
350                         __rcu|
351                         __private
352                 }x;
353 our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
354 our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
355 our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
356 our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
357 our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
358
359 # Notes to $Attribute:
360 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
361 our $Attribute  = qr{
362                         const|
363                         __percpu|
364                         __nocast|
365                         __safe|
366                         __bitwise|
367                         __packed__|
368                         __packed2__|
369                         __naked|
370                         __maybe_unused|
371                         __always_unused|
372                         __noreturn|
373                         __used|
374                         __cold|
375                         __pure|
376                         __noclone|
377                         __deprecated|
378                         __read_mostly|
379                         __kprobes|
380                         $InitAttribute|
381                         ____cacheline_aligned|
382                         ____cacheline_aligned_in_smp|
383                         ____cacheline_internodealigned_in_smp|
384                         __weak
385                   }x;
386 our $Modifier;
387 our $Inline     = qr{inline|__always_inline|noinline|__inline|__inline__};
388 our $Member     = qr{->$Ident|\.$Ident|\[[^]]*\]};
389 our $Lval       = qr{$Ident(?:$Member)*};
390
391 our $Int_type   = qr{(?i)llu|ull|ll|lu|ul|l|u};
392 our $Binary     = qr{(?i)0b[01]+$Int_type?};
393 our $Hex        = qr{(?i)0x[0-9a-f]+$Int_type?};
394 our $Int        = qr{[0-9]+$Int_type?};
395 our $Octal      = qr{0[0-7]+$Int_type?};
396 our $String     = qr{"[X\t]*"};
397 our $Float_hex  = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
398 our $Float_dec  = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
399 our $Float_int  = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
400 our $Float      = qr{$Float_hex|$Float_dec|$Float_int};
401 our $Constant   = qr{$Float|$Binary|$Octal|$Hex|$Int};
402 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
403 our $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
404 our $Arithmetic = qr{\+|-|\*|\/|%};
405 our $Operators  = qr{
406                         <=|>=|==|!=|
407                         =>|->|<<|>>|<|>|!|~|
408                         &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
409                   }x;
410
411 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
412
413 our $BasicType;
414 our $NonptrType;
415 our $NonptrTypeMisordered;
416 our $NonptrTypeWithAttr;
417 our $Type;
418 our $TypeMisordered;
419 our $Declare;
420 our $DeclareMisordered;
421
422 our $NON_ASCII_UTF8     = qr{
423         [\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
424         |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
425         | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
426         |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
427         |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
428         | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
429         |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
430 }x;
431
432 our $UTF8       = qr{
433         [\x09\x0A\x0D\x20-\x7E]              # ASCII
434         | $NON_ASCII_UTF8
435 }x;
436
437 our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
438 our $typeOtherOSTypedefs = qr{(?x:
439         u_(?:char|short|int|long) |          # bsd
440         u(?:nchar|short|int|long)            # sysv
441 )};
442 our $typeKernelTypedefs = qr{(?x:
443         (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
444         atomic_t
445 )};
446 our $typeTypedefs = qr{(?x:
447         $typeC99Typedefs\b|
448         $typeOtherOSTypedefs\b|
449         $typeKernelTypedefs\b
450 )};
451
452 our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
453
454 our $logFunctions = qr{(?x:
455         printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
456         (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
457         TP_printk|
458         WARN(?:_RATELIMIT|_ONCE|)|
459         panic|
460         MODULE_[A-Z_]+|
461         seq_vprintf|seq_printf|seq_puts
462 )};
463
464 our $signature_tags = qr{(?xi:
465         Signed-off-by:|
466         Acked-by:|
467         Tested-by:|
468         Reviewed-by:|
469         Reported-by:|
470         Suggested-by:|
471         To:|
472         Cc:
473 )};
474
475 our @typeListMisordered = (
476         qr{char\s+(?:un)?signed},
477         qr{int\s+(?:(?:un)?signed\s+)?short\s},
478         qr{int\s+short(?:\s+(?:un)?signed)},
479         qr{short\s+int(?:\s+(?:un)?signed)},
480         qr{(?:un)?signed\s+int\s+short},
481         qr{short\s+(?:un)?signed},
482         qr{long\s+int\s+(?:un)?signed},
483         qr{int\s+long\s+(?:un)?signed},
484         qr{long\s+(?:un)?signed\s+int},
485         qr{int\s+(?:un)?signed\s+long},
486         qr{int\s+(?:un)?signed},
487         qr{int\s+long\s+long\s+(?:un)?signed},
488         qr{long\s+long\s+int\s+(?:un)?signed},
489         qr{long\s+long\s+(?:un)?signed\s+int},
490         qr{long\s+long\s+(?:un)?signed},
491         qr{long\s+(?:un)?signed},
492 );
493
494 our @typeList = (
495         qr{void},
496         qr{(?:(?:un)?signed\s+)?char},
497         qr{(?:(?:un)?signed\s+)?short\s+int},
498         qr{(?:(?:un)?signed\s+)?short},
499         qr{(?:(?:un)?signed\s+)?int},
500         qr{(?:(?:un)?signed\s+)?long\s+int},
501         qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
502         qr{(?:(?:un)?signed\s+)?long\s+long},
503         qr{(?:(?:un)?signed\s+)?long},
504         qr{(?:un)?signed},
505         qr{float},
506         qr{double},
507         qr{bool},
508         qr{struct\s+$Ident},
509         qr{union\s+$Ident},
510         qr{enum\s+$Ident},
511         qr{${Ident}_t},
512         qr{${Ident}_handler},
513         qr{${Ident}_handler_fn},
514         @typeListMisordered,
515 );
516
517 our $C90_int_types = qr{(?x:
518         long\s+long\s+int\s+(?:un)?signed|
519         long\s+long\s+(?:un)?signed\s+int|
520         long\s+long\s+(?:un)?signed|
521         (?:(?:un)?signed\s+)?long\s+long\s+int|
522         (?:(?:un)?signed\s+)?long\s+long|
523         int\s+long\s+long\s+(?:un)?signed|
524         int\s+(?:(?:un)?signed\s+)?long\s+long|
525
526         long\s+int\s+(?:un)?signed|
527         long\s+(?:un)?signed\s+int|
528         long\s+(?:un)?signed|
529         (?:(?:un)?signed\s+)?long\s+int|
530         (?:(?:un)?signed\s+)?long|
531         int\s+long\s+(?:un)?signed|
532         int\s+(?:(?:un)?signed\s+)?long|
533
534         int\s+(?:un)?signed|
535         (?:(?:un)?signed\s+)?int
536 )};
537
538 our @typeListFile = ();
539 our @typeListWithAttr = (
540         @typeList,
541         qr{struct\s+$InitAttribute\s+$Ident},
542         qr{union\s+$InitAttribute\s+$Ident},
543 );
544
545 our @modifierList = (
546         qr{fastcall},
547 );
548 our @modifierListFile = ();
549
550 our @mode_permission_funcs = (
551         ["module_param", 3],
552         ["module_param_(?:array|named|string)", 4],
553         ["module_param_array_named", 5],
554         ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
555         ["proc_create(?:_data|)", 2],
556         ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
557         ["IIO_DEV_ATTR_[A-Z_]+", 1],
558         ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
559         ["SENSOR_TEMPLATE(?:_2|)", 3],
560         ["__ATTR", 2],
561 );
562
563 #Create a search pattern for all these functions to speed up a loop below
564 our $mode_perms_search = "";
565 foreach my $entry (@mode_permission_funcs) {
566         $mode_perms_search .= '|' if ($mode_perms_search ne "");
567         $mode_perms_search .= $entry->[0];
568 }
569 $mode_perms_search = "(?:${mode_perms_search})";
570
571 our $mode_perms_world_writable = qr{
572         S_IWUGO         |
573         S_IWOTH         |
574         S_IRWXUGO       |
575         S_IALLUGO       |
576         0[0-7][0-7][2367]
577 }x;
578
579 our %mode_permission_string_types = (
580         "S_IRWXU" => 0700,
581         "S_IRUSR" => 0400,
582         "S_IWUSR" => 0200,
583         "S_IXUSR" => 0100,
584         "S_IRWXG" => 0070,
585         "S_IRGRP" => 0040,
586         "S_IWGRP" => 0020,
587         "S_IXGRP" => 0010,
588         "S_IRWXO" => 0007,
589         "S_IROTH" => 0004,
590         "S_IWOTH" => 0002,
591         "S_IXOTH" => 0001,
592         "S_IRWXUGO" => 0777,
593         "S_IRUGO" => 0444,
594         "S_IWUGO" => 0222,
595         "S_IXUGO" => 0111,
596 );
597
598 #Create a search pattern for all these strings to speed up a loop below
599 our $mode_perms_string_search = "";
600 foreach my $entry (keys %mode_permission_string_types) {
601         $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
602         $mode_perms_string_search .= $entry;
603 }
604 our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
605 our $multi_mode_perms_string_search = qr{
606         ${single_mode_perms_string_search}
607         (?:\s*\|\s*${single_mode_perms_string_search})*
608 }x;
609
610 sub perms_to_octal {
611         my ($string) = @_;
612
613         return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
614
615         my $val = "";
616         my $oval = "";
617         my $to = 0;
618         my $curpos = 0;
619         my $lastpos = 0;
620         while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
621                 $curpos = pos($string);
622                 my $match = $2;
623                 my $omatch = $1;
624                 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
625                 $lastpos = $curpos;
626                 $to |= $mode_permission_string_types{$match};
627                 $val .= '\s*\|\s*' if ($val ne "");
628                 $val .= $match;
629                 $oval .= $omatch;
630         }
631         $oval =~ s/^\s*\|\s*//;
632         $oval =~ s/\s*\|\s*$//;
633         return sprintf("%04o", $to);
634 }
635
636 our $allowed_asm_includes = qr{(?x:
637         irq|
638         memory|
639         time|
640         reboot
641 )};
642 # memory.h: ARM has a custom one
643
644 # Load common spelling mistakes and build regular expression list.
645 my $misspellings;
646 my %spelling_fix;
647
648 if (open(my $spelling, '<', $spelling_file)) {
649         while (<$spelling>) {
650                 my $line = $_;
651
652                 $line =~ s/\s*\n?$//g;
653                 $line =~ s/^\s*//g;
654
655                 next if ($line =~ m/^\s*#/);
656                 next if ($line =~ m/^\s*$/);
657
658                 my ($suspect, $fix) = split(/\|\|/, $line);
659
660                 $spelling_fix{$suspect} = $fix;
661         }
662         close($spelling);
663 } else {
664         warn "No typos will be found - file '$spelling_file': $!\n";
665 }
666
667 if ($codespell) {
668         if (open(my $spelling, '<', $codespellfile)) {
669                 while (<$spelling>) {
670                         my $line = $_;
671
672                         $line =~ s/\s*\n?$//g;
673                         $line =~ s/^\s*//g;
674
675                         next if ($line =~ m/^\s*#/);
676                         next if ($line =~ m/^\s*$/);
677                         next if ($line =~ m/, disabled/i);
678
679                         $line =~ s/,.*$//;
680
681                         my ($suspect, $fix) = split(/->/, $line);
682
683                         $spelling_fix{$suspect} = $fix;
684                 }
685                 close($spelling);
686         } else {
687                 warn "No codespell typos will be found - file '$codespellfile': $!\n";
688         }
689 }
690
691 $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
692
693 sub read_words {
694         my ($wordsRef, $file) = @_;
695
696         if (open(my $words, '<', $file)) {
697                 while (<$words>) {
698                         my $line = $_;
699
700                         $line =~ s/\s*\n?$//g;
701                         $line =~ s/^\s*//g;
702
703                         next if ($line =~ m/^\s*#/);
704                         next if ($line =~ m/^\s*$/);
705                         if ($line =~ /\s/) {
706                                 print("$file: '$line' invalid - ignored\n");
707                                 next;
708                         }
709
710                         $$wordsRef .= '|' if ($$wordsRef ne "");
711                         $$wordsRef .= $line;
712                 }
713                 close($file);
714                 return 1;
715         }
716
717         return 0;
718 }
719
720 my $const_structs = "";
721 read_words(\$const_structs, $conststructsfile)
722     or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
723
724 my $typeOtherTypedefs = "";
725 if (length($typedefsfile)) {
726         read_words(\$typeOtherTypedefs, $typedefsfile)
727             or warn "No additional types will be considered - file '$typedefsfile': $!\n";
728 }
729 $typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
730
731 sub build_types {
732         my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
733         my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
734         my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
735         my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
736         $Modifier       = qr{(?:$Attribute|$Sparse|$mods)};
737         $BasicType      = qr{
738                                 (?:$typeTypedefs\b)|
739                                 (?:${all}\b)
740                 }x;
741         $NonptrType     = qr{
742                         (?:$Modifier\s+|const\s+)*
743                         (?:
744                                 (?:typeof|__typeof__)\s*\([^\)]*\)|
745                                 (?:$typeTypedefs\b)|
746                                 (?:${all}\b)
747                         )
748                         (?:\s+$Modifier|\s+const)*
749                   }x;
750         $NonptrTypeMisordered   = qr{
751                         (?:$Modifier\s+|const\s+)*
752                         (?:
753                                 (?:${Misordered}\b)
754                         )
755                         (?:\s+$Modifier|\s+const)*
756                   }x;
757         $NonptrTypeWithAttr     = qr{
758                         (?:$Modifier\s+|const\s+)*
759                         (?:
760                                 (?:typeof|__typeof__)\s*\([^\)]*\)|
761                                 (?:$typeTypedefs\b)|
762                                 (?:${allWithAttr}\b)
763                         )
764                         (?:\s+$Modifier|\s+const)*
765                   }x;
766         $Type   = qr{
767                         $NonptrType
768                         (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
769                         (?:\s+$Inline|\s+$Modifier)*
770                   }x;
771         $TypeMisordered = qr{
772                         $NonptrTypeMisordered
773                         (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
774                         (?:\s+$Inline|\s+$Modifier)*
775                   }x;
776         $Declare        = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
777         $DeclareMisordered      = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
778 }
779 build_types();
780
781 our $Typecast   = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
782
783 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
784 # requires at least perl version v5.10.0
785 # Any use must be runtime checked with $^V
786
787 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
788 our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
789 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
790
791 our $declaration_macros = qr{(?x:
792         (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
793         (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
794         (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
795         (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
796 )};
797
798 sub deparenthesize {
799         my ($string) = @_;
800         return "" if (!defined($string));
801
802         while ($string =~ /^\s*\(.*\)\s*$/) {
803                 $string =~ s@^\s*\(\s*@@;
804                 $string =~ s@\s*\)\s*$@@;
805         }
806
807         $string =~ s@\s+@ @g;
808
809         return $string;
810 }
811
812 sub seed_camelcase_file {
813         my ($file) = @_;
814
815         return if (!(-f $file));
816
817         local $/;
818
819         open(my $include_file, '<', "$file")
820             or warn "$P: Can't read '$file' $!\n";
821         my $text = <$include_file>;
822         close($include_file);
823
824         my @lines = split('\n', $text);
825
826         foreach my $line (@lines) {
827                 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
828                 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
829                         $camelcase{$1} = 1;
830                 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
831                         $camelcase{$1} = 1;
832                 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
833                         $camelcase{$1} = 1;
834                 }
835         }
836 }
837
838 sub is_maintained_obsolete {
839         my ($filename) = @_;
840
841         return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
842
843         my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
844
845         return $status =~ /obsolete/i;
846 }
847
848 my $camelcase_seeded = 0;
849 sub seed_camelcase_includes {
850         return if ($camelcase_seeded);
851
852         my $files;
853         my $camelcase_cache = "";
854         my @include_files = ();
855
856         $camelcase_seeded = 1;
857
858         if (-e ".git") {
859                 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
860                 chomp $git_last_include_commit;
861                 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
862         } else {
863                 my $last_mod_date = 0;
864                 $files = `find $root/include -name "*.h"`;
865                 @include_files = split('\n', $files);
866                 foreach my $file (@include_files) {
867                         my $date = POSIX::strftime("%Y%m%d%H%M",
868                                                    localtime((stat $file)[9]));
869                         $last_mod_date = $date if ($last_mod_date < $date);
870                 }
871                 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
872         }
873
874         if ($camelcase_cache ne "" && -f $camelcase_cache) {
875                 open(my $camelcase_file, '<', "$camelcase_cache")
876                     or warn "$P: Can't read '$camelcase_cache' $!\n";
877                 while (<$camelcase_file>) {
878                         chomp;
879                         $camelcase{$_} = 1;
880                 }
881                 close($camelcase_file);
882
883                 return;
884         }
885
886         if (-e ".git") {
887                 $files = `git ls-files "include/*.h"`;
888                 @include_files = split('\n', $files);
889         }
890
891         foreach my $file (@include_files) {
892                 seed_camelcase_file($file);
893         }
894
895         if ($camelcase_cache ne "") {
896                 unlink glob ".checkpatch-camelcase.*";
897                 open(my $camelcase_file, '>', "$camelcase_cache")
898                     or warn "$P: Can't write '$camelcase_cache' $!\n";
899                 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
900                         print $camelcase_file ("$_\n");
901                 }
902                 close($camelcase_file);
903         }
904 }
905
906 sub git_commit_info {
907         my ($commit, $id, $desc) = @_;
908
909         return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
910
911         my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
912         $output =~ s/^\s*//gm;
913         my @lines = split("\n", $output);
914
915         return ($id, $desc) if ($#lines < 0);
916
917         if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
918 # Maybe one day convert this block of bash into something that returns
919 # all matching commit ids, but it's very slow...
920 #
921 #               echo "checking commits $1..."
922 #               git rev-list --remotes | grep -i "^$1" |
923 #               while read line ; do
924 #                   git log --format='%H %s' -1 $line |
925 #                   echo "commit $(cut -c 1-12,41-)"
926 #               done
927         } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
928                 $id = undef;
929         } else {
930                 $id = substr($lines[0], 0, 12);
931                 $desc = substr($lines[0], 41);
932         }
933
934         return ($id, $desc);
935 }
936
937 $chk_signoff = 0 if ($file);
938
939 my @rawlines = ();
940 my @lines = ();
941 my @fixed = ();
942 my @fixed_inserted = ();
943 my @fixed_deleted = ();
944 my $fixlinenr = -1;
945
946 # If input is git commits, extract all commits from the commit expressions.
947 # For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
948 die "$P: No git repository found\n" if ($git && !-e ".git");
949
950 if ($git) {
951         my @commits = ();
952         foreach my $commit_expr (@ARGV) {
953                 my $git_range;
954                 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
955                         $git_range = "-$2 $1";
956                 } elsif ($commit_expr =~ m/\.\./) {
957                         $git_range = "$commit_expr";
958                 } else {
959                         $git_range = "-1 $commit_expr";
960                 }
961                 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
962                 foreach my $line (split(/\n/, $lines)) {
963                         $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
964                         next if (!defined($1) || !defined($2));
965                         my $sha1 = $1;
966                         my $subject = $2;
967                         unshift(@commits, $sha1);
968                         $git_commits{$sha1} = $subject;
969                 }
970         }
971         die "$P: no git commits after extraction!\n" if (@commits == 0);
972         @ARGV = @commits;
973 }
974
975 my $vname;
976 for my $filename (@ARGV) {
977         my $FILE;
978         if ($git) {
979                 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
980                         die "$P: $filename: git format-patch failed - $!\n";
981         } elsif ($file) {
982                 open($FILE, '-|', "diff -u /dev/null $filename") ||
983                         die "$P: $filename: diff failed - $!\n";
984         } elsif ($filename eq '-') {
985                 open($FILE, '<&STDIN');
986         } else {
987                 open($FILE, '<', "$filename") ||
988                         die "$P: $filename: open failed - $!\n";
989         }
990         if ($filename eq '-') {
991                 $vname = 'Your patch';
992         } elsif ($git) {
993                 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
994         } else {
995                 $vname = $filename;
996         }
997         while (<$FILE>) {
998                 chomp;
999                 push(@rawlines, $_);
1000         }
1001         close($FILE);
1002
1003         if ($#ARGV > 0 && $quiet == 0) {
1004                 print '-' x length($vname) . "\n";
1005                 print "$vname\n";
1006                 print '-' x length($vname) . "\n";
1007         }
1008
1009         if (!process($filename)) {
1010                 $exit = 1;
1011         }
1012         @rawlines = ();
1013         @lines = ();
1014         @fixed = ();
1015         @fixed_inserted = ();
1016         @fixed_deleted = ();
1017         $fixlinenr = -1;
1018         @modifierListFile = ();
1019         @typeListFile = ();
1020         build_types();
1021 }
1022
1023 if (!$quiet) {
1024         hash_show_words(\%use_type, "Used");
1025         hash_show_words(\%ignore_type, "Ignored");
1026
1027         if ($^V lt 5.10.0) {
1028                 print << "EOM"
1029
1030 NOTE: perl $^V is not modern enough to detect all possible issues.
1031       An upgrade to at least perl v5.10.0 is suggested.
1032 EOM
1033         }
1034         if ($exit) {
1035                 print << "EOM"
1036
1037 NOTE: If any of the errors are false positives, please report
1038       them to the maintainer, see CHECKPATCH in MAINTAINERS.
1039 EOM
1040         }
1041 }
1042
1043 exit($exit);
1044
1045 sub top_of_kernel_tree {
1046         my ($root) = @_;
1047
1048         my @tree_check = (
1049                 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1050                 "README", "Documentation", "arch", "include", "drivers",
1051                 "fs", "init", "ipc", "kernel", "lib", "scripts",
1052         );
1053
1054         foreach my $check (@tree_check) {
1055                 if (! -e $root . '/' . $check) {
1056                         return 0;
1057                 }
1058         }
1059         return 1;
1060 }
1061
1062 sub parse_email {
1063         my ($formatted_email) = @_;
1064
1065         my $name = "";
1066         my $address = "";
1067         my $comment = "";
1068
1069         if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1070                 $name = $1;
1071                 $address = $2;
1072                 $comment = $3 if defined $3;
1073         } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1074                 $address = $1;
1075                 $comment = $2 if defined $2;
1076         } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1077                 $address = $1;
1078                 $comment = $2 if defined $2;
1079                 $formatted_email =~ s/\Q$address\E.*$//;
1080                 $name = $formatted_email;
1081                 $name = trim($name);
1082                 $name =~ s/^\"|\"$//g;
1083                 # If there's a name left after stripping spaces and
1084                 # leading quotes, and the address doesn't have both
1085                 # leading and trailing angle brackets, the address
1086                 # is invalid. ie:
1087                 #   "joe smith joe@smith.com" bad
1088                 #   "joe smith <joe@smith.com" bad
1089                 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1090                         $name = "";
1091                         $address = "";
1092                         $comment = "";
1093                 }
1094         }
1095
1096         $name = trim($name);
1097         $name =~ s/^\"|\"$//g;
1098         $address = trim($address);
1099         $address =~ s/^\<|\>$//g;
1100
1101         if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1102                 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1103                 $name = "\"$name\"";
1104         }
1105
1106         return ($name, $address, $comment);
1107 }
1108
1109 sub format_email {
1110         my ($name, $address) = @_;
1111
1112         my $formatted_email;
1113
1114         $name = trim($name);
1115         $name =~ s/^\"|\"$//g;
1116         $address = trim($address);
1117
1118         if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1119                 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1120                 $name = "\"$name\"";
1121         }
1122
1123         if ("$name" eq "") {
1124                 $formatted_email = "$address";
1125         } else {
1126                 $formatted_email = "$name <$address>";
1127         }
1128
1129         return $formatted_email;
1130 }
1131
1132 sub which {
1133         my ($bin) = @_;
1134
1135         foreach my $path (split(/:/, $ENV{PATH})) {
1136                 if (-e "$path/$bin") {
1137                         return "$path/$bin";
1138                 }
1139         }
1140
1141         return "";
1142 }
1143
1144 sub which_conf {
1145         my ($conf) = @_;
1146
1147         foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1148                 if (-e "$path/$conf") {
1149                         return "$path/$conf";
1150                 }
1151         }
1152
1153         return "";
1154 }
1155
1156 sub expand_tabs {
1157         my ($str) = @_;
1158
1159         my $res = '';
1160         my $n = 0;
1161         for my $c (split(//, $str)) {
1162                 if ($c eq "\t") {
1163                         $res .= ' ';
1164                         $n++;
1165                         for (; ($n % 8) != 0; $n++) {
1166                                 $res .= ' ';
1167                         }
1168                         next;
1169                 }
1170                 $res .= $c;
1171                 $n++;
1172         }
1173
1174         return $res;
1175 }
1176 sub copy_spacing {
1177         (my $res = shift) =~ tr/\t/ /c;
1178         return $res;
1179 }
1180
1181 sub line_stats {
1182         my ($line) = @_;
1183
1184         # Drop the diff line leader and expand tabs
1185         $line =~ s/^.//;
1186         $line = expand_tabs($line);
1187
1188         # Pick the indent from the front of the line.
1189         my ($white) = ($line =~ /^(\s*)/);
1190
1191         return (length($line), length($white));
1192 }
1193
1194 my $sanitise_quote = '';
1195
1196 sub sanitise_line_reset {
1197         my ($in_comment) = @_;
1198
1199         if ($in_comment) {
1200                 $sanitise_quote = '*/';
1201         } else {
1202                 $sanitise_quote = '';
1203         }
1204 }
1205 sub sanitise_line {
1206         my ($line) = @_;
1207
1208         my $res = '';
1209         my $l = '';
1210
1211         my $qlen = 0;
1212         my $off = 0;
1213         my $c;
1214
1215         # Always copy over the diff marker.
1216         $res = substr($line, 0, 1);
1217
1218         for ($off = 1; $off < length($line); $off++) {
1219                 $c = substr($line, $off, 1);
1220
1221                 # Comments we are whacking completely including the begin
1222                 # and end, all to $;.
1223                 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1224                         $sanitise_quote = '*/';
1225
1226                         substr($res, $off, 2, "$;$;");
1227                         $off++;
1228                         next;
1229                 }
1230                 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1231                         $sanitise_quote = '';
1232                         substr($res, $off, 2, "$;$;");
1233                         $off++;
1234                         next;
1235                 }
1236                 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1237                         $sanitise_quote = '//';
1238
1239                         substr($res, $off, 2, $sanitise_quote);
1240                         $off++;
1241                         next;
1242                 }
1243
1244                 # A \ in a string means ignore the next character.
1245                 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1246                     $c eq "\\") {
1247                         substr($res, $off, 2, 'XX');
1248                         $off++;
1249                         next;
1250                 }
1251                 # Regular quotes.
1252                 if ($c eq "'" || $c eq '"') {
1253                         if ($sanitise_quote eq '') {
1254                                 $sanitise_quote = $c;
1255
1256                                 substr($res, $off, 1, $c);
1257                                 next;
1258                         } elsif ($sanitise_quote eq $c) {
1259                                 $sanitise_quote = '';
1260                         }
1261                 }
1262
1263                 #print "c<$c> SQ<$sanitise_quote>\n";
1264                 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1265                         substr($res, $off, 1, $;);
1266                 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1267                         substr($res, $off, 1, $;);
1268                 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1269                         substr($res, $off, 1, 'X');
1270                 } else {
1271                         substr($res, $off, 1, $c);
1272                 }
1273         }
1274
1275         if ($sanitise_quote eq '//') {
1276                 $sanitise_quote = '';
1277         }
1278
1279         # The pathname on a #include may be surrounded by '<' and '>'.
1280         if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1281                 my $clean = 'X' x length($1);
1282                 $res =~ s@\<.*\>@<$clean>@;
1283
1284         # The whole of a #error is a string.
1285         } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1286                 my $clean = 'X' x length($1);
1287                 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1288         }
1289
1290         if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1291                 my $match = $1;
1292                 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1293         }
1294
1295         return $res;
1296 }
1297
1298 sub get_quoted_string {
1299         my ($line, $rawline) = @_;
1300
1301         return "" if (!defined($line) || !defined($rawline));
1302         return "" if ($line !~ m/($String)/g);
1303         return substr($rawline, $-[0], $+[0] - $-[0]);
1304 }
1305
1306 sub ctx_statement_block {
1307         my ($linenr, $remain, $off) = @_;
1308         my $line = $linenr - 1;
1309         my $blk = '';
1310         my $soff = $off;
1311         my $coff = $off - 1;
1312         my $coff_set = 0;
1313
1314         my $loff = 0;
1315
1316         my $type = '';
1317         my $level = 0;
1318         my @stack = ();
1319         my $p;
1320         my $c;
1321         my $len = 0;
1322
1323         my $remainder;
1324         while (1) {
1325                 @stack = (['', 0]) if ($#stack == -1);
1326
1327                 #warn "CSB: blk<$blk> remain<$remain>\n";
1328                 # If we are about to drop off the end, pull in more
1329                 # context.
1330                 if ($off >= $len) {
1331                         for (; $remain > 0; $line++) {
1332                                 last if (!defined $lines[$line]);
1333                                 next if ($lines[$line] =~ /^-/);
1334                                 $remain--;
1335                                 $loff = $len;
1336                                 $blk .= $lines[$line] . "\n";
1337                                 $len = length($blk);
1338                                 $line++;
1339                                 last;
1340                         }
1341                         # Bail if there is no further context.
1342                         #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1343                         if ($off >= $len) {
1344                                 last;
1345                         }
1346                         if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1347                                 $level++;
1348                                 $type = '#';
1349                         }
1350                 }
1351                 $p = $c;
1352                 $c = substr($blk, $off, 1);
1353                 $remainder = substr($blk, $off);
1354
1355                 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1356
1357                 # Handle nested #if/#else.
1358                 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1359                         push(@stack, [ $type, $level ]);
1360                 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1361                         ($type, $level) = @{$stack[$#stack - 1]};
1362                 } elsif ($remainder =~ /^#\s*endif\b/) {
1363                         ($type, $level) = @{pop(@stack)};
1364                 }
1365
1366                 # Statement ends at the ';' or a close '}' at the
1367                 # outermost level.
1368                 if ($level == 0 && $c eq ';') {
1369                         last;
1370                 }
1371
1372                 # An else is really a conditional as long as its not else if
1373                 if ($level == 0 && $coff_set == 0 &&
1374                                 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1375                                 $remainder =~ /^(else)(?:\s|{)/ &&
1376                                 $remainder !~ /^else\s+if\b/) {
1377                         $coff = $off + length($1) - 1;
1378                         $coff_set = 1;
1379                         #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1380                         #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1381                 }
1382
1383                 if (($type eq '' || $type eq '(') && $c eq '(') {
1384                         $level++;
1385                         $type = '(';
1386                 }
1387                 if ($type eq '(' && $c eq ')') {
1388                         $level--;
1389                         $type = ($level != 0)? '(' : '';
1390
1391                         if ($level == 0 && $coff < $soff) {
1392                                 $coff = $off;
1393                                 $coff_set = 1;
1394                                 #warn "CSB: mark coff<$coff>\n";
1395                         }
1396                 }
1397                 if (($type eq '' || $type eq '{') && $c eq '{') {
1398                         $level++;
1399                         $type = '{';
1400                 }
1401                 if ($type eq '{' && $c eq '}') {
1402                         $level--;
1403                         $type = ($level != 0)? '{' : '';
1404
1405                         if ($level == 0) {
1406                                 if (substr($blk, $off + 1, 1) eq ';') {
1407                                         $off++;
1408                                 }
1409                                 last;
1410                         }
1411                 }
1412                 # Preprocessor commands end at the newline unless escaped.
1413                 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1414                         $level--;
1415                         $type = '';
1416                         $off++;
1417                         last;
1418                 }
1419                 $off++;
1420         }
1421         # We are truly at the end, so shuffle to the next line.
1422         if ($off == $len) {
1423                 $loff = $len + 1;
1424                 $line++;
1425                 $remain--;
1426         }
1427
1428         my $statement = substr($blk, $soff, $off - $soff + 1);
1429         my $condition = substr($blk, $soff, $coff - $soff + 1);
1430
1431         #warn "STATEMENT<$statement>\n";
1432         #warn "CONDITION<$condition>\n";
1433
1434         #print "coff<$coff> soff<$off> loff<$loff>\n";
1435
1436         return ($statement, $condition,
1437                         $line, $remain + 1, $off - $loff + 1, $level);
1438 }
1439
1440 sub statement_lines {
1441         my ($stmt) = @_;
1442
1443         # Strip the diff line prefixes and rip blank lines at start and end.
1444         $stmt =~ s/(^|\n)./$1/g;
1445         $stmt =~ s/^\s*//;
1446         $stmt =~ s/\s*$//;
1447
1448         my @stmt_lines = ($stmt =~ /\n/g);
1449
1450         return $#stmt_lines + 2;
1451 }
1452
1453 sub statement_rawlines {
1454         my ($stmt) = @_;
1455
1456         my @stmt_lines = ($stmt =~ /\n/g);
1457
1458         return $#stmt_lines + 2;
1459 }
1460
1461 sub statement_block_size {
1462         my ($stmt) = @_;
1463
1464         $stmt =~ s/(^|\n)./$1/g;
1465         $stmt =~ s/^\s*{//;
1466         $stmt =~ s/}\s*$//;
1467         $stmt =~ s/^\s*//;
1468         $stmt =~ s/\s*$//;
1469
1470         my @stmt_lines = ($stmt =~ /\n/g);
1471         my @stmt_statements = ($stmt =~ /;/g);
1472
1473         my $stmt_lines = $#stmt_lines + 2;
1474         my $stmt_statements = $#stmt_statements + 1;
1475
1476         if ($stmt_lines > $stmt_statements) {
1477                 return $stmt_lines;
1478         } else {
1479                 return $stmt_statements;
1480         }
1481 }
1482
1483 sub ctx_statement_full {
1484         my ($linenr, $remain, $off) = @_;
1485         my ($statement, $condition, $level);
1486
1487         my (@chunks);
1488
1489         # Grab the first conditional/block pair.
1490         ($statement, $condition, $linenr, $remain, $off, $level) =
1491                                 ctx_statement_block($linenr, $remain, $off);
1492         #print "F: c<$condition> s<$statement> remain<$remain>\n";
1493         push(@chunks, [ $condition, $statement ]);
1494         if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1495                 return ($level, $linenr, @chunks);
1496         }
1497
1498         # Pull in the following conditional/block pairs and see if they
1499         # could continue the statement.
1500         for (;;) {
1501                 ($statement, $condition, $linenr, $remain, $off, $level) =
1502                                 ctx_statement_block($linenr, $remain, $off);
1503                 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1504                 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1505                 #print "C: push\n";
1506                 push(@chunks, [ $condition, $statement ]);
1507         }
1508
1509         return ($level, $linenr, @chunks);
1510 }
1511
1512 sub ctx_block_get {
1513         my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1514         my $line;
1515         my $start = $linenr - 1;
1516         my $blk = '';
1517         my @o;
1518         my @c;
1519         my @res = ();
1520
1521         my $level = 0;
1522         my @stack = ($level);
1523         for ($line = $start; $remain > 0; $line++) {
1524                 next if ($rawlines[$line] =~ /^-/);
1525                 $remain--;
1526
1527                 $blk .= $rawlines[$line];
1528
1529                 # Handle nested #if/#else.
1530                 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1531                         push(@stack, $level);
1532                 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1533                         $level = $stack[$#stack - 1];
1534                 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1535                         $level = pop(@stack);
1536                 }
1537
1538                 foreach my $c (split(//, $lines[$line])) {
1539                         ##print "C<$c>L<$level><$open$close>O<$off>\n";
1540                         if ($off > 0) {
1541                                 $off--;
1542                                 next;
1543                         }
1544
1545                         if ($c eq $close && $level > 0) {
1546                                 $level--;
1547                                 last if ($level == 0);
1548                         } elsif ($c eq $open) {
1549                                 $level++;
1550                         }
1551                 }
1552
1553                 if (!$outer || $level <= 1) {
1554                         push(@res, $rawlines[$line]);
1555                 }
1556
1557                 last if ($level == 0);
1558         }
1559
1560         return ($level, @res);
1561 }
1562 sub ctx_block_outer {
1563         my ($linenr, $remain) = @_;
1564
1565         my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1566         return @r;
1567 }
1568 sub ctx_block {
1569         my ($linenr, $remain) = @_;
1570
1571         my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1572         return @r;
1573 }
1574 sub ctx_statement {
1575         my ($linenr, $remain, $off) = @_;
1576
1577         my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1578         return @r;
1579 }
1580 sub ctx_block_level {
1581         my ($linenr, $remain) = @_;
1582
1583         return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1584 }
1585 sub ctx_statement_level {
1586         my ($linenr, $remain, $off) = @_;
1587
1588         return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1589 }
1590
1591 sub ctx_locate_comment {
1592         my ($first_line, $end_line) = @_;
1593
1594         # Catch a comment on the end of the line itself.
1595         my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1596         return $current_comment if (defined $current_comment);
1597
1598         # Look through the context and try and figure out if there is a
1599         # comment.
1600         my $in_comment = 0;
1601         $current_comment = '';
1602         for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1603                 my $line = $rawlines[$linenr - 1];
1604                 #warn "           $line\n";
1605                 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1606                         $in_comment = 1;
1607                 }
1608                 if ($line =~ m@/\*@) {
1609                         $in_comment = 1;
1610                 }
1611                 if (!$in_comment && $current_comment ne '') {
1612                         $current_comment = '';
1613                 }
1614                 $current_comment .= $line . "\n" if ($in_comment);
1615                 if ($line =~ m@\*/@) {
1616                         $in_comment = 0;
1617                 }
1618         }
1619
1620         chomp($current_comment);
1621         return($current_comment);
1622 }
1623 sub ctx_has_comment {
1624         my ($first_line, $end_line) = @_;
1625         my $cmt = ctx_locate_comment($first_line, $end_line);
1626
1627         ##print "LINE: $rawlines[$end_line - 1 ]\n";
1628         ##print "CMMT: $cmt\n";
1629
1630         return ($cmt ne '');
1631 }
1632
1633 sub raw_line {
1634         my ($linenr, $cnt) = @_;
1635
1636         my $offset = $linenr - 1;
1637         $cnt++;
1638
1639         my $line;
1640         while ($cnt) {
1641                 $line = $rawlines[$offset++];
1642                 next if (defined($line) && $line =~ /^-/);
1643                 $cnt--;
1644         }
1645
1646         return $line;
1647 }
1648
1649 sub get_stat_real {
1650         my ($linenr, $lc) = @_;
1651
1652         my $stat_real = raw_line($linenr, 0);
1653         for (my $count = $linenr + 1; $count <= $lc; $count++) {
1654                 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1655         }
1656
1657         return $stat_real;
1658 }
1659
1660 sub get_stat_here {
1661         my ($linenr, $cnt, $here) = @_;
1662
1663         my $herectx = $here . "\n";
1664         for (my $n = 0; $n < $cnt; $n++) {
1665                 $herectx .= raw_line($linenr, $n) . "\n";
1666         }
1667
1668         return $herectx;
1669 }
1670
1671 sub cat_vet {
1672         my ($vet) = @_;
1673         my ($res, $coded);
1674
1675         $res = '';
1676         while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1677                 $res .= $1;
1678                 if ($2 ne '') {
1679                         $coded = sprintf("^%c", unpack('C', $2) + 64);
1680                         $res .= $coded;
1681                 }
1682         }
1683         $res =~ s/$/\$/;
1684
1685         return $res;
1686 }
1687
1688 my $av_preprocessor = 0;
1689 my $av_pending;
1690 my @av_paren_type;
1691 my $av_pend_colon;
1692
1693 sub annotate_reset {
1694         $av_preprocessor = 0;
1695         $av_pending = '_';
1696         @av_paren_type = ('E');
1697         $av_pend_colon = 'O';
1698 }
1699
1700 sub annotate_values {
1701         my ($stream, $type) = @_;
1702
1703         my $res;
1704         my $var = '_' x length($stream);
1705         my $cur = $stream;
1706
1707         print "$stream\n" if ($dbg_values > 1);
1708
1709         while (length($cur)) {
1710                 @av_paren_type = ('E') if ($#av_paren_type < 0);
1711                 print " <" . join('', @av_paren_type) .
1712                                 "> <$type> <$av_pending>" if ($dbg_values > 1);
1713                 if ($cur =~ /^(\s+)/o) {
1714                         print "WS($1)\n" if ($dbg_values > 1);
1715                         if ($1 =~ /\n/ && $av_preprocessor) {
1716                                 $type = pop(@av_paren_type);
1717                                 $av_preprocessor = 0;
1718                         }
1719
1720                 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1721                         print "CAST($1)\n" if ($dbg_values > 1);
1722                         push(@av_paren_type, $type);
1723                         $type = 'c';
1724
1725                 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1726                         print "DECLARE($1)\n" if ($dbg_values > 1);
1727                         $type = 'T';
1728
1729                 } elsif ($cur =~ /^($Modifier)\s*/) {
1730                         print "MODIFIER($1)\n" if ($dbg_values > 1);
1731                         $type = 'T';
1732
1733                 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1734                         print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1735                         $av_preprocessor = 1;
1736                         push(@av_paren_type, $type);
1737                         if ($2 ne '') {
1738                                 $av_pending = 'N';
1739                         }
1740                         $type = 'E';
1741
1742                 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1743                         print "UNDEF($1)\n" if ($dbg_values > 1);
1744                         $av_preprocessor = 1;
1745                         push(@av_paren_type, $type);
1746
1747                 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1748                         print "PRE_START($1)\n" if ($dbg_values > 1);
1749                         $av_preprocessor = 1;
1750
1751                         push(@av_paren_type, $type);
1752                         push(@av_paren_type, $type);
1753                         $type = 'E';
1754
1755                 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1756                         print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1757                         $av_preprocessor = 1;
1758
1759                         push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1760
1761                         $type = 'E';
1762
1763                 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1764                         print "PRE_END($1)\n" if ($dbg_values > 1);
1765
1766                         $av_preprocessor = 1;
1767
1768                         # Assume all arms of the conditional end as this
1769                         # one does, and continue as if the #endif was not here.
1770                         pop(@av_paren_type);
1771                         push(@av_paren_type, $type);
1772                         $type = 'E';
1773
1774                 } elsif ($cur =~ /^(\\\n)/o) {
1775                         print "PRECONT($1)\n" if ($dbg_values > 1);
1776
1777                 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1778                         print "ATTR($1)\n" if ($dbg_values > 1);
1779                         $av_pending = $type;
1780                         $type = 'N';
1781
1782                 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1783                         print "SIZEOF($1)\n" if ($dbg_values > 1);
1784                         if (defined $2) {
1785                                 $av_pending = 'V';
1786                         }
1787                         $type = 'N';
1788
1789                 } elsif ($cur =~ /^(if|while|for)\b/o) {
1790                         print "COND($1)\n" if ($dbg_values > 1);
1791                         $av_pending = 'E';
1792                         $type = 'N';
1793
1794                 } elsif ($cur =~/^(case)/o) {
1795                         print "CASE($1)\n" if ($dbg_values > 1);
1796                         $av_pend_colon = 'C';
1797                         $type = 'N';
1798
1799                 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1800                         print "KEYWORD($1)\n" if ($dbg_values > 1);
1801                         $type = 'N';
1802
1803                 } elsif ($cur =~ /^(\()/o) {
1804                         print "PAREN('$1')\n" if ($dbg_values > 1);
1805                         push(@av_paren_type, $av_pending);
1806                         $av_pending = '_';
1807                         $type = 'N';
1808
1809                 } elsif ($cur =~ /^(\))/o) {
1810                         my $new_type = pop(@av_paren_type);
1811                         if ($new_type ne '_') {
1812                                 $type = $new_type;
1813                                 print "PAREN('$1') -> $type\n"
1814                                                         if ($dbg_values > 1);
1815                         } else {
1816                                 print "PAREN('$1')\n" if ($dbg_values > 1);
1817                         }
1818
1819                 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1820                         print "FUNC($1)\n" if ($dbg_values > 1);
1821                         $type = 'V';
1822                         $av_pending = 'V';
1823
1824                 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1825                         if (defined $2 && $type eq 'C' || $type eq 'T') {
1826                                 $av_pend_colon = 'B';
1827                         } elsif ($type eq 'E') {
1828                                 $av_pend_colon = 'L';
1829                         }
1830                         print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1831                         $type = 'V';
1832
1833                 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1834                         print "IDENT($1)\n" if ($dbg_values > 1);
1835                         $type = 'V';
1836
1837                 } elsif ($cur =~ /^($Assignment)/o) {
1838                         print "ASSIGN($1)\n" if ($dbg_values > 1);
1839                         $type = 'N';
1840
1841                 } elsif ($cur =~/^(;|{|})/) {
1842                         print "END($1)\n" if ($dbg_values > 1);
1843                         $type = 'E';
1844                         $av_pend_colon = 'O';
1845
1846                 } elsif ($cur =~/^(,)/) {
1847                         print "COMMA($1)\n" if ($dbg_values > 1);
1848                         $type = 'C';
1849
1850                 } elsif ($cur =~ /^(\?)/o) {
1851                         print "QUESTION($1)\n" if ($dbg_values > 1);
1852                         $type = 'N';
1853
1854                 } elsif ($cur =~ /^(:)/o) {
1855                         print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1856
1857                         substr($var, length($res), 1, $av_pend_colon);
1858                         if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1859                                 $type = 'E';
1860                         } else {
1861                                 $type = 'N';
1862                         }
1863                         $av_pend_colon = 'O';
1864
1865                 } elsif ($cur =~ /^(\[)/o) {
1866                         print "CLOSE($1)\n" if ($dbg_values > 1);
1867                         $type = 'N';
1868
1869                 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1870                         my $variant;
1871
1872                         print "OPV($1)\n" if ($dbg_values > 1);
1873                         if ($type eq 'V') {
1874                                 $variant = 'B';
1875                         } else {
1876                                 $variant = 'U';
1877                         }
1878
1879                         substr($var, length($res), 1, $variant);
1880                         $type = 'N';
1881
1882                 } elsif ($cur =~ /^($Operators)/o) {
1883                         print "OP($1)\n" if ($dbg_values > 1);
1884                         if ($1 ne '++' && $1 ne '--') {
1885                                 $type = 'N';
1886                         }
1887
1888                 } elsif ($cur =~ /(^.)/o) {
1889                         print "C($1)\n" if ($dbg_values > 1);
1890                 }
1891                 if (defined $1) {
1892                         $cur = substr($cur, length($1));
1893                         $res .= $type x length($1);
1894                 }
1895         }
1896
1897         return ($res, $var);
1898 }
1899
1900 sub possible {
1901         my ($possible, $line) = @_;
1902         my $notPermitted = qr{(?:
1903                 ^(?:
1904                         $Modifier|
1905                         $Storage|
1906                         $Type|
1907                         DEFINE_\S+
1908                 )$|
1909                 ^(?:
1910                         goto|
1911                         return|
1912                         case|
1913                         else|
1914                         asm|__asm__|
1915                         do|
1916                         \#|
1917                         \#\#|
1918                 )(?:\s|$)|
1919                 ^(?:typedef|struct|enum)\b
1920             )}x;
1921         warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1922         if ($possible !~ $notPermitted) {
1923                 # Check for modifiers.
1924                 $possible =~ s/\s*$Storage\s*//g;
1925                 $possible =~ s/\s*$Sparse\s*//g;
1926                 if ($possible =~ /^\s*$/) {
1927
1928                 } elsif ($possible =~ /\s/) {
1929                         $possible =~ s/\s*$Type\s*//g;
1930                         for my $modifier (split(' ', $possible)) {
1931                                 if ($modifier !~ $notPermitted) {
1932                                         warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1933                                         push(@modifierListFile, $modifier);
1934                                 }
1935                         }
1936
1937                 } else {
1938                         warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1939                         push(@typeListFile, $possible);
1940                 }
1941                 build_types();
1942         } else {
1943                 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1944         }
1945 }
1946
1947 my $prefix = '';
1948
1949 sub show_type {
1950         my ($type) = @_;
1951
1952         $type =~ tr/[a-z]/[A-Z]/;
1953
1954         return defined $use_type{$type} if (scalar keys %use_type > 0);
1955
1956         return !defined $ignore_type{$type};
1957 }
1958
1959 sub report {
1960         my ($level, $type, $msg) = @_;
1961
1962         if (!show_type($type) ||
1963             (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1964                 return 0;
1965         }
1966         my $output = '';
1967         if ($color) {
1968                 if ($level eq 'ERROR') {
1969                         $output .= RED;
1970                 } elsif ($level eq 'WARNING') {
1971                         $output .= YELLOW;
1972                 } else {
1973                         $output .= GREEN;
1974                 }
1975         }
1976         $output .= $prefix . $level . ':';
1977         if ($show_types) {
1978                 $output .= BLUE if ($color);
1979                 $output .= "$type:";
1980         }
1981         $output .= RESET if ($color);
1982         $output .= ' ' . $msg . "\n";
1983
1984         if ($showfile) {
1985                 my @lines = split("\n", $output, -1);
1986                 splice(@lines, 1, 1);
1987                 $output = join("\n", @lines);
1988         }
1989         $output = (split('\n', $output))[0] . "\n" if ($terse);
1990
1991         push(our @report, $output);
1992
1993         return 1;
1994 }
1995
1996 sub report_dump {
1997         our @report;
1998 }
1999
2000 sub fixup_current_range {
2001         my ($lineRef, $offset, $length) = @_;
2002
2003         if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2004                 my $o = $1;
2005                 my $l = $2;
2006                 my $no = $o + $offset;
2007                 my $nl = $l + $length;
2008                 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2009         }
2010 }
2011
2012 sub fix_inserted_deleted_lines {
2013         my ($linesRef, $insertedRef, $deletedRef) = @_;
2014
2015         my $range_last_linenr = 0;
2016         my $delta_offset = 0;
2017
2018         my $old_linenr = 0;
2019         my $new_linenr = 0;
2020
2021         my $next_insert = 0;
2022         my $next_delete = 0;
2023
2024         my @lines = ();
2025
2026         my $inserted = @{$insertedRef}[$next_insert++];
2027         my $deleted = @{$deletedRef}[$next_delete++];
2028
2029         foreach my $old_line (@{$linesRef}) {
2030                 my $save_line = 1;
2031                 my $line = $old_line;   #don't modify the array
2032                 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {      #new filename
2033                         $delta_offset = 0;
2034                 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {    #new hunk
2035                         $range_last_linenr = $new_linenr;
2036                         fixup_current_range(\$line, $delta_offset, 0);
2037                 }
2038
2039                 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2040                         $deleted = @{$deletedRef}[$next_delete++];
2041                         $save_line = 0;
2042                         fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2043                 }
2044
2045                 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2046                         push(@lines, ${$inserted}{'LINE'});
2047                         $inserted = @{$insertedRef}[$next_insert++];
2048                         $new_linenr++;
2049                         fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2050                 }
2051
2052                 if ($save_line) {
2053                         push(@lines, $line);
2054                         $new_linenr++;
2055                 }
2056
2057                 $old_linenr++;
2058         }
2059
2060         return @lines;
2061 }
2062
2063 sub fix_insert_line {
2064         my ($linenr, $line) = @_;
2065
2066         my $inserted = {
2067                 LINENR => $linenr,
2068                 LINE => $line,
2069         };
2070         push(@fixed_inserted, $inserted);
2071 }
2072
2073 sub fix_delete_line {
2074         my ($linenr, $line) = @_;
2075
2076         my $deleted = {
2077                 LINENR => $linenr,
2078                 LINE => $line,
2079         };
2080
2081         push(@fixed_deleted, $deleted);
2082 }
2083
2084 sub ERROR {
2085         my ($type, $msg) = @_;
2086
2087         if (report("ERROR", $type, $msg)) {
2088                 our $clean = 0;
2089                 our $cnt_error++;
2090                 return 1;
2091         }
2092         return 0;
2093 }
2094 sub WARN {
2095         my ($type, $msg) = @_;
2096
2097         if (report("WARNING", $type, $msg)) {
2098                 our $clean = 0;
2099                 our $cnt_warn++;
2100                 return 1;
2101         }
2102         return 0;
2103 }
2104 sub CHK {
2105         my ($type, $msg) = @_;
2106
2107         if ($check && report("CHECK", $type, $msg)) {
2108                 our $clean = 0;
2109                 our $cnt_chk++;
2110                 return 1;
2111         }
2112         return 0;
2113 }
2114
2115 sub check_absolute_file {
2116         my ($absolute, $herecurr) = @_;
2117         my $file = $absolute;
2118
2119         ##print "absolute<$absolute>\n";
2120
2121         # See if any suffix of this path is a path within the tree.
2122         while ($file =~ s@^[^/]*/@@) {
2123                 if (-f "$root/$file") {
2124                         ##print "file<$file>\n";
2125                         last;
2126                 }
2127         }
2128         if (! -f _)  {
2129                 return 0;
2130         }
2131
2132         # It is, so see if the prefix is acceptable.
2133         my $prefix = $absolute;
2134         substr($prefix, -length($file)) = '';
2135
2136         ##print "prefix<$prefix>\n";
2137         if ($prefix ne ".../") {
2138                 WARN("USE_RELATIVE_PATH",
2139                      "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2140         }
2141 }
2142
2143 sub trim {
2144         my ($string) = @_;
2145
2146         $string =~ s/^\s+|\s+$//g;
2147
2148         return $string;
2149 }
2150
2151 sub ltrim {
2152         my ($string) = @_;
2153
2154         $string =~ s/^\s+//;
2155
2156         return $string;
2157 }
2158
2159 sub rtrim {
2160         my ($string) = @_;
2161
2162         $string =~ s/\s+$//;
2163
2164         return $string;
2165 }
2166
2167 sub string_find_replace {
2168         my ($string, $find, $replace) = @_;
2169
2170         $string =~ s/$find/$replace/g;
2171
2172         return $string;
2173 }
2174
2175 sub tabify {
2176         my ($leading) = @_;
2177
2178         my $source_indent = 8;
2179         my $max_spaces_before_tab = $source_indent - 1;
2180         my $spaces_to_tab = " " x $source_indent;
2181
2182         #convert leading spaces to tabs
2183         1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2184         #Remove spaces before a tab
2185         1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2186
2187         return "$leading";
2188 }
2189
2190 sub pos_last_openparen {
2191         my ($line) = @_;
2192
2193         my $pos = 0;
2194
2195         my $opens = $line =~ tr/\(/\(/;
2196         my $closes = $line =~ tr/\)/\)/;
2197
2198         my $last_openparen = 0;
2199
2200         if (($opens == 0) || ($closes >= $opens)) {
2201                 return -1;
2202         }
2203
2204         my $len = length($line);
2205
2206         for ($pos = 0; $pos < $len; $pos++) {
2207                 my $string = substr($line, $pos);
2208                 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2209                         $pos += length($1) - 1;
2210                 } elsif (substr($line, $pos, 1) eq '(') {
2211                         $last_openparen = $pos;
2212                 } elsif (index($string, '(') == -1) {
2213                         last;
2214                 }
2215         }
2216
2217         return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2218 }
2219
2220 sub process {
2221         my $filename = shift;
2222
2223         my $linenr=0;
2224         my $prevline="";
2225         my $prevrawline="";
2226         my $stashline="";
2227         my $stashrawline="";
2228
2229         my $length;
2230         my $indent;
2231         my $previndent=0;
2232         my $stashindent=0;
2233
2234         our $clean = 1;
2235         my $signoff = 0;
2236         my $is_patch = 0;
2237         my $in_header_lines = $file ? 0 : 1;
2238         my $in_commit_log = 0;          #Scanning lines before patch
2239         my $has_commit_log = 0;         #Encountered lines before patch
2240         my $commit_log_possible_stack_dump = 0;
2241         my $commit_log_long_line = 0;
2242         my $commit_log_has_diff = 0;
2243         my $reported_maintainer_file = 0;
2244         my $non_utf8_charset = 0;
2245
2246         my $last_blank_line = 0;
2247         my $last_coalesced_string_linenr = -1;
2248
2249         our @report = ();
2250         our $cnt_lines = 0;
2251         our $cnt_error = 0;
2252         our $cnt_warn = 0;
2253         our $cnt_chk = 0;
2254
2255         # Trace the real file/line as we go.
2256         my $realfile = '';
2257         my $realline = 0;
2258         my $realcnt = 0;
2259         my $here = '';
2260         my $context_function;           #undef'd unless there's a known function
2261         my $in_comment = 0;
2262         my $comment_edge = 0;
2263         my $first_line = 0;
2264         my $p1_prefix = '';
2265
2266         my $prev_values = 'E';
2267
2268         # suppression flags
2269         my %suppress_ifbraces;
2270         my %suppress_whiletrailers;
2271         my %suppress_export;
2272         my $suppress_statement = 0;
2273
2274         my %signatures = ();
2275
2276         # Pre-scan the patch sanitizing the lines.
2277         # Pre-scan the patch looking for any __setup documentation.
2278         #
2279         my @setup_docs = ();
2280         my $setup_docs = 0;
2281
2282         my $camelcase_file_seeded = 0;
2283
2284         my $checklicenseline = 1;
2285
2286         sanitise_line_reset();
2287         my $line;
2288         foreach my $rawline (@rawlines) {
2289                 $linenr++;
2290                 $line = $rawline;
2291
2292                 push(@fixed, $rawline) if ($fix);
2293
2294                 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2295                         $setup_docs = 0;
2296                         if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2297                                 $setup_docs = 1;
2298                         }
2299                         #next;
2300                 }
2301                 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2302                         $realline=$1-1;
2303                         if (defined $2) {
2304                                 $realcnt=$3+1;
2305                         } else {
2306                                 $realcnt=1+1;
2307                         }
2308                         $in_comment = 0;
2309
2310                         # Guestimate if this is a continuing comment.  Run
2311                         # the context looking for a comment "edge".  If this
2312                         # edge is a close comment then we must be in a comment
2313                         # at context start.
2314                         my $edge;
2315                         my $cnt = $realcnt;
2316                         for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2317                                 next if (defined $rawlines[$ln - 1] &&
2318                                          $rawlines[$ln - 1] =~ /^-/);
2319                                 $cnt--;
2320                                 #print "RAW<$rawlines[$ln - 1]>\n";
2321                                 last if (!defined $rawlines[$ln - 1]);
2322                                 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2323                                     $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2324                                         ($edge) = $1;
2325                                         last;
2326                                 }
2327                         }
2328                         if (defined $edge && $edge eq '*/') {
2329                                 $in_comment = 1;
2330                         }
2331
2332                         # Guestimate if this is a continuing comment.  If this
2333                         # is the start of a diff block and this line starts
2334                         # ' *' then it is very likely a comment.
2335                         if (!defined $edge &&
2336                             $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2337                         {
2338                                 $in_comment = 1;
2339                         }
2340
2341                         ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2342                         sanitise_line_reset($in_comment);
2343
2344                 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2345                         # Standardise the strings and chars within the input to
2346                         # simplify matching -- only bother with positive lines.
2347                         $line = sanitise_line($rawline);
2348                 }
2349                 push(@lines, $line);
2350
2351                 if ($realcnt > 1) {
2352                         $realcnt-- if ($line =~ /^(?:\+| |$)/);
2353                 } else {
2354                         $realcnt = 0;
2355                 }
2356
2357                 #print "==>$rawline\n";
2358                 #print "-->$line\n";
2359
2360                 if ($setup_docs && $line =~ /^\+/) {
2361                         push(@setup_docs, $line);
2362                 }
2363         }
2364
2365         $prefix = '';
2366
2367         $realcnt = 0;
2368         $linenr = 0;
2369         $fixlinenr = -1;
2370         foreach my $line (@lines) {
2371                 $linenr++;
2372                 $fixlinenr++;
2373                 my $sline = $line;      #copy of $line
2374                 $sline =~ s/$;/ /g;     #with comments as spaces
2375
2376                 my $rawline = $rawlines[$linenr - 1];
2377
2378 #extract the line range in the file after the patch is applied
2379                 if (!$in_commit_log &&
2380                     $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2381                         my $context = $4;
2382                         $is_patch = 1;
2383                         $first_line = $linenr + 1;
2384                         $realline=$1-1;
2385                         if (defined $2) {
2386                                 $realcnt=$3+1;
2387                         } else {
2388                                 $realcnt=1+1;
2389                         }
2390                         annotate_reset();
2391                         $prev_values = 'E';
2392
2393                         %suppress_ifbraces = ();
2394                         %suppress_whiletrailers = ();
2395                         %suppress_export = ();
2396                         $suppress_statement = 0;
2397                         if ($context =~ /\b(\w+)\s*\(/) {
2398                                 $context_function = $1;
2399                         } else {
2400                                 undef $context_function;
2401                         }
2402                         next;
2403
2404 # track the line number as we move through the hunk, note that
2405 # new versions of GNU diff omit the leading space on completely
2406 # blank context lines so we need to count that too.
2407                 } elsif ($line =~ /^( |\+|$)/) {
2408                         $realline++;
2409                         $realcnt-- if ($realcnt != 0);
2410
2411                         # Measure the line length and indent.
2412                         ($length, $indent) = line_stats($rawline);
2413
2414                         # Track the previous line.
2415                         ($prevline, $stashline) = ($stashline, $line);
2416                         ($previndent, $stashindent) = ($stashindent, $indent);
2417                         ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2418
2419                         #warn "line<$line>\n";
2420
2421                 } elsif ($realcnt == 1) {
2422                         $realcnt--;
2423                 }
2424
2425                 my $hunk_line = ($realcnt != 0);
2426
2427                 $here = "#$linenr: " if (!$file);
2428                 $here = "#$realline: " if ($file);
2429
2430                 my $found_file = 0;
2431                 # extract the filename as it passes
2432                 if ($line =~ /^diff --git.*?(\S+)$/) {
2433                         $realfile = $1;
2434                         $realfile =~ s@^([^/]*)/@@ if (!$file);
2435                         $in_commit_log = 0;
2436                         $found_file = 1;
2437                 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2438                         $realfile = $1;
2439                         $realfile =~ s@^([^/]*)/@@ if (!$file);
2440                         $in_commit_log = 0;
2441
2442                         $p1_prefix = $1;
2443                         if (!$file && $tree && $p1_prefix ne '' &&
2444                             -e "$root/$p1_prefix") {
2445                                 WARN("PATCH_PREFIX",
2446                                      "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2447                         }
2448
2449                         if ($realfile =~ m@^include/asm/@) {
2450                                 ERROR("MODIFIED_INCLUDE_ASM",
2451                                       "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2452                         }
2453                         $found_file = 1;
2454                 }
2455
2456 #make up the handle for any error we report on this line
2457                 if ($showfile) {
2458                         $prefix = "$realfile:$realline: "
2459                 } elsif ($emacs) {
2460                         if ($file) {
2461                                 $prefix = "$filename:$realline: ";
2462                         } else {
2463                                 $prefix = "$filename:$linenr: ";
2464                         }
2465                 }
2466
2467                 if ($found_file) {
2468                         if (is_maintained_obsolete($realfile)) {
2469                                 WARN("OBSOLETE",
2470                                      "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
2471                         }
2472                         if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2473                                 $check = 1;
2474                         } else {
2475                                 $check = $check_orig;
2476                         }
2477                         $checklicenseline = 1;
2478                         next;
2479                 }
2480
2481                 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2482
2483                 my $hereline = "$here\n$rawline\n";
2484                 my $herecurr = "$here\n$rawline\n";
2485                 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2486
2487                 $cnt_lines++ if ($realcnt != 0);
2488
2489 # Check if the commit log has what seems like a diff which can confuse patch
2490                 if ($in_commit_log && !$commit_log_has_diff &&
2491                     (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2492                       $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2493                      $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2494                      $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2495                         ERROR("DIFF_IN_COMMIT_MSG",
2496                               "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2497                         $commit_log_has_diff = 1;
2498                 }
2499
2500 # Check for incorrect file permissions
2501                 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2502                         my $permhere = $here . "FILE: $realfile\n";
2503                         if ($realfile !~ m@scripts/@ &&
2504                             $realfile !~ /\.(py|pl|awk|sh)$/) {
2505                                 ERROR("EXECUTE_PERMISSIONS",
2506                                       "do not set execute permissions for source files\n" . $permhere);
2507                         }
2508                 }
2509
2510 # Check the patch for a signoff:
2511                 if ($line =~ /^\s*signed-off-by:/i) {
2512                         $signoff++;
2513                         $in_commit_log = 0;
2514                 }
2515
2516 # Check if MAINTAINERS is being updated.  If so, there's probably no need to
2517 # emit the "does MAINTAINERS need updating?" message on file add/move/delete
2518                 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2519                         $reported_maintainer_file = 1;
2520                 }
2521
2522 # Check signature styles
2523                 if (!$in_header_lines &&
2524                     $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2525                         my $space_before = $1;
2526                         my $sign_off = $2;
2527                         my $space_after = $3;
2528                         my $email = $4;
2529                         my $ucfirst_sign_off = ucfirst(lc($sign_off));
2530
2531                         if ($sign_off !~ /$signature_tags/) {
2532                                 WARN("BAD_SIGN_OFF",
2533                                      "Non-standard signature: $sign_off\n" . $herecurr);
2534                         }
2535                         if (defined $space_before && $space_before ne "") {
2536                                 if (WARN("BAD_SIGN_OFF",
2537                                          "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2538                                     $fix) {
2539                                         $fixed[$fixlinenr] =
2540                                             "$ucfirst_sign_off $email";
2541                                 }
2542                         }
2543                         if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2544                                 if (WARN("BAD_SIGN_OFF",
2545                                          "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2546                                     $fix) {
2547                                         $fixed[$fixlinenr] =
2548                                             "$ucfirst_sign_off $email";
2549                                 }
2550
2551                         }
2552                         if (!defined $space_after || $space_after ne " ") {
2553                                 if (WARN("BAD_SIGN_OFF",
2554                                          "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2555                                     $fix) {
2556                                         $fixed[$fixlinenr] =
2557                                             "$ucfirst_sign_off $email";
2558                                 }
2559                         }
2560
2561                         my ($email_name, $email_address, $comment) = parse_email($email);
2562                         my $suggested_email = format_email(($email_name, $email_address));
2563                         if ($suggested_email eq "") {
2564                                 ERROR("BAD_SIGN_OFF",
2565                                       "Unrecognized email address: '$email'\n" . $herecurr);
2566                         } else {
2567                                 my $dequoted = $suggested_email;
2568                                 $dequoted =~ s/^"//;
2569                                 $dequoted =~ s/" </ </;
2570                                 # Don't force email to have quotes
2571                                 # Allow just an angle bracketed address
2572                                 if ("$dequoted$comment" ne $email &&
2573                                     "<$email_address>$comment" ne $email &&
2574                                     "$suggested_email$comment" ne $email) {
2575                                         WARN("BAD_SIGN_OFF",
2576                                              "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2577                                 }
2578                         }
2579
2580 # Check for duplicate signatures
2581                         my $sig_nospace = $line;
2582                         $sig_nospace =~ s/\s//g;
2583                         $sig_nospace = lc($sig_nospace);
2584                         if (defined $signatures{$sig_nospace}) {
2585                                 WARN("BAD_SIGN_OFF",
2586                                      "Duplicate signature\n" . $herecurr);
2587                         } else {
2588                                 $signatures{$sig_nospace} = 1;
2589                         }
2590                 }
2591
2592 # Check email subject for common tools that don't need to be mentioned
2593                 if ($in_header_lines &&
2594                     $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2595                         WARN("EMAIL_SUBJECT",
2596                              "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2597                 }
2598
2599 # Check for old stable address
2600                 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2601                         ERROR("STABLE_ADDRESS",
2602                               "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2603                 }
2604
2605 # Check for unwanted Gerrit info
2606                 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2607                         ERROR("GERRIT_CHANGE_ID",
2608                               "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2609                 }
2610
2611 # Check if the commit log is in a possible stack dump
2612                 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2613                     ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2614                      $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2615                                         # timestamp
2616                      $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2617                                         # stack dump address
2618                         $commit_log_possible_stack_dump = 1;
2619                 }
2620
2621 # Check for line lengths > 75 in commit log, warn once
2622                 if ($in_commit_log && !$commit_log_long_line &&
2623                     length($line) > 75 &&
2624                     !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2625                                         # file delta changes
2626                       $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2627                                         # filename then :
2628                       $line =~ /^\s*(?:Fixes:|Link:)/i ||
2629                                         # A Fixes: or Link: line
2630                       $commit_log_possible_stack_dump)) {
2631                         WARN("COMMIT_LOG_LONG_LINE",
2632                              "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2633                         $commit_log_long_line = 1;
2634                 }
2635
2636 # Reset possible stack dump if a blank line is found
2637                 if ($in_commit_log && $commit_log_possible_stack_dump &&
2638                     $line =~ /^\s*$/) {
2639                         $commit_log_possible_stack_dump = 0;
2640                 }
2641
2642 # Check for git id commit length and improperly formed commit descriptions
2643                 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2644                     $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2645                     $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2646                     ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2647                      ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2648                       $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2649                       $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2650                         my $init_char = "c";
2651                         my $orig_commit = "";
2652                         my $short = 1;
2653                         my $long = 0;
2654                         my $case = 1;
2655                         my $space = 1;
2656                         my $hasdesc = 0;
2657                         my $hasparens = 0;
2658                         my $id = '0123456789ab';
2659                         my $orig_desc = "commit description";
2660                         my $description = "";
2661
2662                         if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2663                                 $init_char = $1;
2664                                 $orig_commit = lc($2);
2665                         } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2666                                 $orig_commit = lc($1);
2667                         }
2668
2669                         $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2670                         $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2671                         $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2672                         $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2673                         if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2674                                 $orig_desc = $1;
2675                                 $hasparens = 1;
2676                         } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2677                                  defined $rawlines[$linenr] &&
2678                                  $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2679                                 $orig_desc = $1;
2680                                 $hasparens = 1;
2681                         } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2682                                  defined $rawlines[$linenr] &&
2683                                  $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2684                                 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2685                                 $orig_desc = $1;
2686                                 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2687                                 $orig_desc .= " " . $1;
2688                                 $hasparens = 1;
2689                         }
2690
2691                         ($id, $description) = git_commit_info($orig_commit,
2692                                                               $id, $orig_desc);
2693
2694                         if (defined($id) &&
2695                            ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2696                                 ERROR("GIT_COMMIT_ID",
2697                                       "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2698                         }
2699                 }
2700
2701 # Check for added, moved or deleted files
2702                 if (!$reported_maintainer_file && !$in_commit_log &&
2703                     ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2704                      $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2705                      ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2706                       (defined($1) || defined($2))))) {
2707                         $is_patch = 1;
2708                         $reported_maintainer_file = 1;
2709                         WARN("FILE_PATH_CHANGES",
2710                              "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2711                 }
2712
2713 # Check for wrappage within a valid hunk of the file
2714                 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2715                         ERROR("CORRUPTED_PATCH",
2716                               "patch seems to be corrupt (line wrapped?)\n" .
2717                                 $herecurr) if (!$emitted_corrupt++);
2718                 }
2719
2720 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2721                 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2722                     $rawline !~ m/^$UTF8*$/) {
2723                         my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2724
2725                         my $blank = copy_spacing($rawline);
2726                         my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2727                         my $hereptr = "$hereline$ptr\n";
2728
2729                         CHK("INVALID_UTF8",
2730                             "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2731                 }
2732
2733 # Check if it's the start of a commit log
2734 # (not a header line and we haven't seen the patch filename)
2735                 if ($in_header_lines && $realfile =~ /^$/ &&
2736                     !($rawline =~ /^\s+(?:\S|$)/ ||
2737                       $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
2738                         $in_header_lines = 0;
2739                         $in_commit_log = 1;
2740                         $has_commit_log = 1;
2741                 }
2742
2743 # Check if there is UTF-8 in a commit log when a mail header has explicitly
2744 # declined it, i.e defined some charset where it is missing.
2745                 if ($in_header_lines &&
2746                     $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2747                     $1 !~ /utf-8/i) {
2748                         $non_utf8_charset = 1;
2749                 }
2750
2751                 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2752                     $rawline =~ /$NON_ASCII_UTF8/) {
2753                         WARN("UTF8_BEFORE_PATCH",
2754                             "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2755                 }
2756
2757 # Check for absolute kernel paths in commit message
2758                 if ($tree && $in_commit_log) {
2759                         while ($line =~ m{(?:^|\s)(/\S*)}g) {
2760                                 my $file = $1;
2761
2762                                 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2763                                     check_absolute_file($1, $herecurr)) {
2764                                         #
2765                                 } else {
2766                                         check_absolute_file($file, $herecurr);
2767                                 }
2768                         }
2769                 }
2770
2771 # Check for various typo / spelling mistakes
2772                 if (defined($misspellings) &&
2773                     ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2774                         while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
2775                                 my $typo = $1;
2776                                 my $typo_fix = $spelling_fix{lc($typo)};
2777                                 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2778                                 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2779                                 my $msg_level = \&WARN;
2780                                 $msg_level = \&CHK if ($file);
2781                                 if (&{$msg_level}("TYPO_SPELLING",
2782                                                   "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2783                                     $fix) {
2784                                         $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2785                                 }
2786                         }
2787                 }
2788
2789 # ignore non-hunk lines and lines being removed
2790                 next if (!$hunk_line || $line =~ /^-/);
2791
2792 #trailing whitespace
2793                 if ($line =~ /^\+.*\015/) {
2794                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2795                         if (ERROR("DOS_LINE_ENDINGS",
2796                                   "DOS line endings\n" . $herevet) &&
2797                             $fix) {
2798                                 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2799                         }
2800                 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2801                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2802                         if (ERROR("TRAILING_WHITESPACE",
2803                                   "trailing whitespace\n" . $herevet) &&
2804                             $fix) {
2805                                 $fixed[$fixlinenr] =~ s/\s+$//;
2806                         }
2807
2808                         $rpt_cleaners = 1;
2809                 }
2810
2811 # Check for FSF mailing addresses.
2812                 if ($rawline =~ /\bwrite to the Free/i ||
2813                     $rawline =~ /\b675\s+Mass\s+Ave/i ||
2814                     $rawline =~ /\b59\s+Temple\s+Pl/i ||
2815                     $rawline =~ /\b51\s+Franklin\s+St/i) {
2816                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2817                         my $msg_level = \&ERROR;
2818                         $msg_level = \&CHK if ($file);
2819                         &{$msg_level}("FSF_MAILING_ADDRESS",
2820                                       "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)
2821                 }
2822
2823 # check for Kconfig help text having a real description
2824 # Only applies when adding the entry originally, after that we do not have
2825 # sufficient context to determine whether it is indeed long enough.
2826                 if ($realfile =~ /Kconfig/ &&
2827                     # 'choice' is usually the last thing on the line (though
2828                     # Kconfig supports named choices), so use a word boundary
2829                     # (\b) rather than a whitespace character (\s)
2830                     $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
2831                         my $length = 0;
2832                         my $cnt = $realcnt;
2833                         my $ln = $linenr + 1;
2834                         my $f;
2835                         my $is_start = 0;
2836                         my $is_end = 0;
2837                         for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
2838                                 $f = $lines[$ln - 1];
2839                                 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2840                                 $is_end = $lines[$ln - 1] =~ /^\+/;
2841
2842                                 next if ($f =~ /^-/);
2843                                 last if (!$file && $f =~ /^\@\@/);
2844
2845                                 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
2846                                         $is_start = 1;
2847                                 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
2848                                         if ($lines[$ln - 1] =~ "---help---") {
2849                                                 WARN("CONFIG_DESCRIPTION",
2850                                                      "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
2851                                         }
2852                                         $length = -1;
2853                                 }
2854
2855                                 $f =~ s/^.//;
2856                                 $f =~ s/#.*//;
2857                                 $f =~ s/^\s+//;
2858                                 next if ($f =~ /^$/);
2859
2860                                 # This only checks context lines in the patch
2861                                 # and so hopefully shouldn't trigger false
2862                                 # positives, even though some of these are
2863                                 # common words in help texts
2864                                 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2865                                                   if|endif|menu|endmenu|source)\b/x) {
2866                                         $is_end = 1;
2867                                         last;
2868                                 }
2869                                 $length++;
2870                         }
2871                         if ($is_start && $is_end && $length < $min_conf_desc_length) {
2872                                 WARN("CONFIG_DESCRIPTION",
2873                                      "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2874                         }
2875                         #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2876                 }
2877
2878 # check for MAINTAINERS entries that don't have the right form
2879                 if ($realfile =~ /^MAINTAINERS$/ &&
2880                     $rawline =~ /^\+[A-Z]:/ &&
2881                     $rawline !~ /^\+[A-Z]:\t\S/) {
2882                         if (WARN("MAINTAINERS_STYLE",
2883                                  "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2884                             $fix) {
2885                                 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2886                         }
2887                 }
2888
2889 # discourage the use of boolean for type definition attributes of Kconfig options
2890                 if ($realfile =~ /Kconfig/ &&
2891                     $line =~ /^\+\s*\bboolean\b/) {
2892                         WARN("CONFIG_TYPE_BOOLEAN",
2893                              "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2894                 }
2895
2896                 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2897                     ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2898                         my $flag = $1;
2899                         my $replacement = {
2900                                 'EXTRA_AFLAGS' =>   'asflags-y',
2901                                 'EXTRA_CFLAGS' =>   'ccflags-y',
2902                                 'EXTRA_CPPFLAGS' => 'cppflags-y',
2903                                 'EXTRA_LDFLAGS' =>  'ldflags-y',
2904                         };
2905
2906                         WARN("DEPRECATED_VARIABLE",
2907                              "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2908                 }
2909
2910 # check for DT compatible documentation
2911                 if (defined $root &&
2912                         (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2913                          ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2914
2915                         my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2916
2917                         my $dt_path = $root . "/Documentation/devicetree/bindings/";
2918                         my $vp_file = $dt_path . "vendor-prefixes.txt";
2919
2920                         foreach my $compat (@compats) {
2921                                 my $compat2 = $compat;
2922                                 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2923                                 my $compat3 = $compat;
2924                                 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2925                                 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2926                                 if ( $? >> 8 ) {
2927                                         WARN("UNDOCUMENTED_DT_STRING",
2928                                              "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2929                                 }
2930
2931                                 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2932                                 my $vendor = $1;
2933                                 `grep -Eq "^$vendor\\b" $vp_file`;
2934                                 if ( $? >> 8 ) {
2935                                         WARN("UNDOCUMENTED_DT_STRING",
2936                                              "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2937                                 }
2938                         }
2939                 }
2940
2941 # check for using SPDX license tag at beginning of files
2942                 if ($realline == $checklicenseline) {
2943                         if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
2944                                 $checklicenseline = 2;
2945                         } elsif ($rawline =~ /^\+/) {
2946                                 my $comment = "";
2947                                 if ($realfile =~ /\.(h|s|S)$/) {
2948                                         $comment = '/*';
2949                                 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
2950                                         $comment = '//';
2951                                 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
2952                                         $comment = '#';
2953                                 } elsif ($realfile =~ /\.rst$/) {
2954                                         $comment = '..';
2955                                 }
2956
2957                                 if ($comment !~ /^$/ &&
2958                                     $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
2959                                         WARN("SPDX_LICENSE_TAG",
2960                                              "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
2961                                 }
2962                         }
2963                 }
2964
2965 # check we are in a valid source file if not then ignore this hunk
2966                 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
2967
2968 # line length limit (with some exclusions)
2969 #
2970 # There are a few types of lines that may extend beyond $max_line_length:
2971 #       logging functions like pr_info that end in a string
2972 #       lines with a single string
2973 #       #defines that are a single string
2974 #       lines with an RFC3986 like URL
2975 #
2976 # There are 3 different line length message types:
2977 # LONG_LINE_COMMENT     a comment starts before but extends beyond $max_line_length
2978 # LONG_LINE_STRING      a string starts before but extends beyond $max_line_length
2979 # LONG_LINE             all other lines longer than $max_line_length
2980 #
2981 # if LONG_LINE is ignored, the other 2 types are also ignored
2982 #
2983
2984                 if ($line =~ /^\+/ && $length > $max_line_length) {
2985                         my $msg_type = "LONG_LINE";
2986
2987                         # Check the allowed long line types first
2988
2989                         # logging functions that end in a string that starts
2990                         # before $max_line_length
2991                         if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2992                             length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2993                                 $msg_type = "";
2994
2995                         # lines with only strings (w/ possible termination)
2996                         # #defines with only strings
2997                         } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2998                                  $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2999                                 $msg_type = "";
3000
3001                         # More special cases
3002                         } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3003                                  $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3004                                 $msg_type = "";
3005
3006                         # URL ($rawline is used in case the URL is in a comment)
3007                         } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3008                                 $msg_type = "";
3009
3010                         # Otherwise set the alternate message types
3011
3012                         # a comment starts before $max_line_length
3013                         } elsif ($line =~ /($;[\s$;]*)$/ &&
3014                                  length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3015                                 $msg_type = "LONG_LINE_COMMENT"
3016
3017                         # a quoted string starts before $max_line_length
3018                         } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3019                                  length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3020                                 $msg_type = "LONG_LINE_STRING"
3021                         }
3022
3023                         if ($msg_type ne "" &&
3024                             (show_type("LONG_LINE") || show_type($msg_type))) {
3025                                 WARN($msg_type,
3026                                      "line over $max_line_length characters\n" . $herecurr);
3027                         }
3028                 }
3029
3030 # check for adding lines without a newline.
3031                 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3032                         WARN("MISSING_EOF_NEWLINE",
3033                              "adding a line without newline at end of file\n" . $herecurr);
3034                 }
3035
3036 # check we are in a valid source file C or perl if not then ignore this hunk
3037                 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
3038
3039 # at the beginning of a line any tabs must come first and anything
3040 # more than 8 must use tabs.
3041                 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3042                     $rawline =~ /^\+\s*        \s*/) {
3043                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3044                         $rpt_cleaners = 1;
3045                         if (ERROR("CODE_INDENT",
3046                                   "code indent should use tabs where possible\n" . $herevet) &&
3047                             $fix) {
3048                                 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3049                         }
3050                 }
3051
3052 # check for space before tabs.
3053                 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3054                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3055                         if (WARN("SPACE_BEFORE_TAB",
3056                                 "please, no space before tabs\n" . $herevet) &&
3057                             $fix) {
3058                                 while ($fixed[$fixlinenr] =~
3059                                            s/(^\+.*) {8,8}\t/$1\t\t/) {}
3060                                 while ($fixed[$fixlinenr] =~
3061                                            s/(^\+.*) +\t/$1\t/) {}
3062                         }
3063                 }
3064
3065 # check for assignments on the start of a line
3066                 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3067                         CHK("ASSIGNMENT_CONTINUATIONS",
3068                             "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3069                 }
3070
3071 # check for && or || at the start of a line
3072                 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3073                         CHK("LOGICAL_CONTINUATIONS",
3074                             "Logical continuations should be on the previous line\n" . $hereprev);
3075                 }
3076
3077 # check indentation starts on a tab stop
3078                 if ($^V && $^V ge 5.10.0 &&
3079                     $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3080                         my $indent = length($1);
3081                         if ($indent % 8) {
3082                                 if (WARN("TABSTOP",
3083                                          "Statements should start on a tabstop\n" . $herecurr) &&
3084                                     $fix) {
3085                                         $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3086                                 }
3087                         }
3088                 }
3089
3090 # check multi-line statement indentation matches previous line
3091                 if ($^V && $^V ge 5.10.0 &&
3092                     $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3093                         $prevline =~ /^\+(\t*)(.*)$/;
3094                         my $oldindent = $1;
3095                         my $rest = $2;
3096
3097                         my $pos = pos_last_openparen($rest);
3098                         if ($pos >= 0) {
3099                                 $line =~ /^(\+| )([ \t]*)/;
3100                                 my $newindent = $2;
3101
3102                                 my $goodtabindent = $oldindent .
3103                                         "\t" x ($pos / 8) .
3104                                         " "  x ($pos % 8);
3105                                 my $goodspaceindent = $oldindent . " "  x $pos;
3106
3107                                 if ($newindent ne $goodtabindent &&
3108                                     $newindent ne $goodspaceindent) {
3109
3110                                         if (CHK("PARENTHESIS_ALIGNMENT",
3111                                                 "Alignment should match open parenthesis\n" . $hereprev) &&
3112                                             $fix && $line =~ /^\+/) {
3113                                                 $fixed[$fixlinenr] =~
3114                                                     s/^\+[ \t]*/\+$goodtabindent/;
3115                                         }
3116                                 }
3117                         }
3118                 }
3119
3120 # check for space after cast like "(int) foo" or "(struct foo) bar"
3121 # avoid checking a few false positives:
3122 #   "sizeof(<type>)" or "__alignof__(<type>)"
3123 #   function pointer declarations like "(*foo)(int) = bar;"
3124 #   structure definitions like "(struct foo) { 0 };"
3125 #   multiline macros that define functions
3126 #   known attributes or the __attribute__ keyword
3127                 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3128                     (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
3129                         if (CHK("SPACING",
3130                                 "No space is necessary after a cast\n" . $herecurr) &&
3131                             $fix) {
3132                                 $fixed[$fixlinenr] =~
3133                                     s/(\(\s*$Type\s*\))[ \t]+/$1/;
3134                         }
3135                 }
3136
3137 # Block comment styles
3138 # Networking with an initial /*
3139                 if ($realfile =~ m@^(drivers/net/|net/)@ &&
3140                     $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
3141                     $rawline =~ /^\+[ \t]*\*/ &&
3142                     $realline > 2) {
3143                         WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3144                              "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3145                 }
3146
3147 # Block comments use * on subsequent lines
3148                 if ($prevline =~ /$;[ \t]*$/ &&                 #ends in comment
3149                     $prevrawline =~ /^\+.*?\/\*/ &&             #starting /*
3150                     $prevrawline !~ /\*\/[ \t]*$/ &&            #no trailing */
3151                     $rawline =~ /^\+/ &&                        #line is new
3152                     $rawline !~ /^\+[ \t]*\*/) {                #no leading *
3153                         WARN("BLOCK_COMMENT_STYLE",
3154                              "Block comments use * on subsequent lines\n" . $hereprev);
3155                 }
3156
3157 # Block comments use */ on trailing lines
3158                 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&       #trailing */
3159                     $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&      #inline /*...*/
3160                     $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&       #trailing **/
3161                     $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {    #non blank */
3162                         WARN("BLOCK_COMMENT_STYLE",
3163                              "Block comments use a trailing */ on a separate line\n" . $herecurr);
3164                 }
3165
3166 # Block comment * alignment
3167                 if ($prevline =~ /$;[ \t]*$/ &&                 #ends in comment
3168                     $line =~ /^\+[ \t]*$;/ &&                   #leading comment
3169                     $rawline =~ /^\+[ \t]*\*/ &&                #leading *
3170                     (($prevrawline =~ /^\+.*?\/\*/ &&           #leading /*
3171                       $prevrawline !~ /\*\/[ \t]*$/) ||         #no trailing */
3172                      $prevrawline =~ /^\+[ \t]*\*/)) {          #leading *
3173                         my $oldindent;
3174                         $prevrawline =~ m@^\+([ \t]*/?)\*@;
3175                         if (defined($1)) {
3176                                 $oldindent = expand_tabs($1);
3177                         } else {
3178                                 $prevrawline =~ m@^\+(.*/?)\*@;
3179                                 $oldindent = expand_tabs($1);
3180                         }
3181                         $rawline =~ m@^\+([ \t]*)\*@;
3182                         my $newindent = $1;
3183                         $newindent = expand_tabs($newindent);
3184                         if (length($oldindent) ne length($newindent)) {
3185                                 WARN("BLOCK_COMMENT_STYLE",
3186                                      "Block comments should align the * on each line\n" . $hereprev);
3187                         }
3188                 }
3189
3190 # check for missing blank lines after struct/union declarations
3191 # with exceptions for various attributes and macros
3192                 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3193                     $line =~ /^\+/ &&
3194                     !($line =~ /^\+\s*$/ ||
3195                       $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3196                       $line =~ /^\+\s*MODULE_/i ||
3197                       $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3198                       $line =~ /^\+[a-z_]*init/ ||
3199                       $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3200                       $line =~ /^\+\s*DECLARE/ ||
3201                       $line =~ /^\+\s*builtin_[\w_]*driver/ ||
3202                       $line =~ /^\+\s*__setup/)) {
3203                         if (CHK("LINE_SPACING",
3204                                 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3205                             $fix) {
3206                                 fix_insert_line($fixlinenr, "\+");
3207                         }
3208                 }
3209
3210 # check for multiple consecutive blank lines
3211                 if ($prevline =~ /^[\+ ]\s*$/ &&
3212                     $line =~ /^\+\s*$/ &&
3213                     $last_blank_line != ($linenr - 1)) {
3214                         if (CHK("LINE_SPACING",
3215                                 "Please don't use multiple blank lines\n" . $hereprev) &&
3216                             $fix) {
3217                                 fix_delete_line($fixlinenr, $rawline);
3218                         }
3219
3220                         $last_blank_line = $linenr;
3221                 }
3222
3223 # check for missing blank lines after declarations
3224                 if ($sline =~ /^\+\s+\S/ &&                     #Not at char 1
3225                         # actual declarations
3226                     ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3227                         # function pointer declarations
3228                      $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3229                         # foo bar; where foo is some local typedef or #define
3230                      $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3231                         # known declaration macros
3232                      $prevline =~ /^\+\s+$declaration_macros/) &&
3233                         # for "else if" which can look like "$Ident $Ident"
3234                     !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3235                         # other possible extensions of declaration lines
3236                       $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3237                         # not starting a section or a macro "\" extended line
3238                       $prevline =~ /(?:\{\s*|\\)$/) &&
3239                         # looks like a declaration
3240                     !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3241                         # function pointer declarations
3242                       $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3243                         # foo bar; where foo is some local typedef or #define
3244                       $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3245                         # known declaration macros
3246                       $sline =~ /^\+\s+$declaration_macros/ ||
3247                         # start of struct or union or enum
3248                       $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
3249                         # start or end of block or continuation of declaration
3250                       $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3251                         # bitfield continuation
3252                       $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3253                         # other possible extensions of declaration lines
3254                       $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3255                         # indentation of previous and current line are the same
3256                     (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3257                         if (WARN("LINE_SPACING",
3258                                  "Missing a blank line after declarations\n" . $hereprev) &&
3259                             $fix) {
3260                                 fix_insert_line($fixlinenr, "\+");
3261                         }
3262                 }
3263
3264 # check for spaces at the beginning of a line.
3265 # Exceptions:
3266 #  1) within comments
3267 #  2) indented preprocessor commands
3268 #  3) hanging labels
3269                 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
3270                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3271                         if (WARN("LEADING_SPACE",
3272                                  "please, no spaces at the start of a line\n" . $herevet) &&
3273                             $fix) {
3274                                 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3275                         }
3276                 }
3277
3278 # check we are in a valid C source file if not then ignore this hunk
3279                 next if ($realfile !~ /\.(h|c)$/);
3280
3281 # check for unusual line ending [ or (
3282                 if ($line =~ /^\+.*([\[\(])\s*$/) {
3283                         CHK("OPEN_ENDED_LINE",
3284                             "Lines should not end with a '$1'\n" . $herecurr);
3285                 }
3286
3287 # check if this appears to be the start function declaration, save the name
3288                 if ($sline =~ /^\+\{\s*$/ &&
3289                     $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3290                         $context_function = $1;
3291                 }
3292
3293 # check if this appears to be the end of function declaration
3294                 if ($sline =~ /^\+\}\s*$/) {
3295                         undef $context_function;
3296                 }
3297
3298 # check indentation of any line with a bare else
3299 # (but not if it is a multiple line "if (foo) return bar; else return baz;")
3300 # if the previous line is a break or return and is indented 1 tab more...
3301                 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3302                         my $tabs = length($1) + 1;
3303                         if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3304                             ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3305                              defined $lines[$linenr] &&
3306                              $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3307                                 WARN("UNNECESSARY_ELSE",
3308                                      "else is not generally useful after a break or return\n" . $hereprev);
3309                         }
3310                 }
3311
3312 # check indentation of a line with a break;
3313 # if the previous line is a goto or return and is indented the same # of tabs
3314                 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3315                         my $tabs = $1;
3316                         if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3317                                 WARN("UNNECESSARY_BREAK",
3318                                      "break is not useful after a goto or return\n" . $hereprev);
3319                         }
3320                 }
3321
3322 # check for RCS/CVS revision markers
3323                 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3324                         WARN("CVS_KEYWORD",
3325                              "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3326                 }
3327
3328 # check for old HOTPLUG __dev<foo> section markings
3329                 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3330                         WARN("HOTPLUG_SECTION",
3331                              "Using $1 is unnecessary\n" . $herecurr);
3332                 }
3333
3334 # Check for potential 'bare' types
3335                 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3336                     $realline_next);
3337 #print "LINE<$line>\n";
3338                 if ($linenr > $suppress_statement &&
3339                     $realcnt && $sline =~ /.\s*\S/) {
3340                         ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3341                                 ctx_statement_block($linenr, $realcnt, 0);
3342                         $stat =~ s/\n./\n /g;
3343                         $cond =~ s/\n./\n /g;
3344
3345 #print "linenr<$linenr> <$stat>\n";
3346                         # If this statement has no statement boundaries within
3347                         # it there is no point in retrying a statement scan
3348                         # until we hit end of it.
3349                         my $frag = $stat; $frag =~ s/;+\s*$//;
3350                         if ($frag !~ /(?:{|;)/) {
3351 #print "skip<$line_nr_next>\n";
3352                                 $suppress_statement = $line_nr_next;
3353                         }
3354
3355                         # Find the real next line.
3356                         $realline_next = $line_nr_next;
3357                         if (defined $realline_next &&
3358                             (!defined $lines[$realline_next - 1] ||
3359                              substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3360                                 $realline_next++;
3361                         }
3362
3363                         my $s = $stat;
3364                         $s =~ s/{.*$//s;
3365
3366                         # Ignore goto labels.
3367                         if ($s =~ /$Ident:\*$/s) {
3368
3369                         # Ignore functions being called
3370                         } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3371
3372                         } elsif ($s =~ /^.\s*else\b/s) {
3373
3374                         # declarations always start with types
3375                         } 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) {
3376                                 my $type = $1;
3377                                 $type =~ s/\s+/ /g;
3378                                 possible($type, "A:" . $s);
3379
3380                         # definitions in global scope can only start with types
3381                         } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3382                                 possible($1, "B:" . $s);
3383                         }
3384
3385                         # any (foo ... *) is a pointer cast, and foo is a type
3386                         while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3387                                 possible($1, "C:" . $s);
3388                         }
3389
3390                         # Check for any sort of function declaration.
3391                         # int foo(something bar, other baz);
3392                         # void (*store_gdt)(x86_descr_ptr *);
3393                         if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3394                                 my ($name_len) = length($1);
3395
3396                                 my $ctx = $s;
3397                                 substr($ctx, 0, $name_len + 1, '');
3398                                 $ctx =~ s/\)[^\)]*$//;
3399
3400                                 for my $arg (split(/\s*,\s*/, $ctx)) {
3401                                         if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3402
3403                                                 possible($1, "D:" . $s);
3404                                         }
3405                                 }
3406                         }
3407
3408                 }
3409
3410 #
3411 # Checks which may be anchored in the context.
3412 #
3413
3414 # Check for switch () and associated case and default
3415 # statements should be at the same indent.
3416                 if ($line=~/\bswitch\s*\(.*\)/) {
3417                         my $err = '';
3418                         my $sep = '';
3419                         my @ctx = ctx_block_outer($linenr, $realcnt);
3420                         shift(@ctx);
3421                         for my $ctx (@ctx) {
3422                                 my ($clen, $cindent) = line_stats($ctx);
3423                                 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3424                                                         $indent != $cindent) {
3425                                         $err .= "$sep$ctx\n";
3426                                         $sep = '';
3427                                 } else {
3428                                         $sep = "[...]\n";
3429                                 }
3430                         }
3431                         if ($err ne '') {
3432                                 ERROR("SWITCH_CASE_INDENT_LEVEL",
3433                                       "switch and case should be at the same indent\n$hereline$err");
3434                         }
3435                 }
3436
3437 # if/while/etc brace do not go on next line, unless defining a do while loop,
3438 # or if that brace on the next line is for something else
3439                 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3440                         my $pre_ctx = "$1$2";
3441
3442                         my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
3443
3444                         if ($line =~ /^\+\t{6,}/) {
3445                                 WARN("DEEP_INDENTATION",
3446                                      "Too many leading tabs - consider code refactoring\n" . $herecurr);
3447                         }
3448
3449                         my $ctx_cnt = $realcnt - $#ctx - 1;
3450                         my $ctx = join("\n", @ctx);
3451
3452                         my $ctx_ln = $linenr;
3453                         my $ctx_skip = $realcnt;
3454
3455                         while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3456                                         defined $lines[$ctx_ln - 1] &&
3457                                         $lines[$ctx_ln - 1] =~ /^-/)) {
3458                                 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3459                                 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3460                                 $ctx_ln++;
3461                         }
3462
3463                         #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3464                         #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3465
3466                         if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3467                                 ERROR("OPEN_BRACE",
3468                                       "that open brace { should be on the previous line\n" .
3469                                         "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3470                         }
3471                         if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3472                             $ctx =~ /\)\s*\;\s*$/ &&
3473                             defined $lines[$ctx_ln - 1])
3474                         {
3475                                 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3476                                 if ($nindent > $indent) {
3477                                         WARN("TRAILING_SEMICOLON",
3478                                              "trailing semicolon indicates no statements, indent implies otherwise\n" .
3479                                                 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3480                                 }
3481                         }
3482                 }
3483
3484 # Check relative indent for conditionals and blocks.
3485                 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
3486                         ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3487                                 ctx_statement_block($linenr, $realcnt, 0)
3488                                         if (!defined $stat);
3489                         my ($s, $c) = ($stat, $cond);
3490
3491                         substr($s, 0, length($c), '');
3492
3493                         # remove inline comments
3494                         $s =~ s/$;/ /g;
3495                         $c =~ s/$;/ /g;
3496
3497                         # Find out how long the conditional actually is.
3498                         my @newlines = ($c =~ /\n/gs);
3499                         my $cond_lines = 1 + $#newlines;
3500
3501                         # Make sure we remove the line prefixes as we have
3502                         # none on the first line, and are going to readd them
3503                         # where necessary.
3504                         $s =~ s/\n./\n/gs;
3505                         while ($s =~ /\n\s+\\\n/) {
3506                                 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3507                         }
3508
3509                         # We want to check the first line inside the block
3510                         # starting at the end of the conditional, so remove:
3511                         #  1) any blank line termination
3512                         #  2) any opening brace { on end of the line
3513                         #  3) any do (...) {
3514                         my $continuation = 0;
3515                         my $check = 0;
3516                         $s =~ s/^.*\bdo\b//;
3517                         $s =~ s/^\s*{//;
3518                         if ($s =~ s/^\s*\\//) {
3519                                 $continuation = 1;
3520                         }
3521                         if ($s =~ s/^\s*?\n//) {
3522                                 $check = 1;
3523                                 $cond_lines++;
3524                         }
3525
3526                         # Also ignore a loop construct at the end of a
3527                         # preprocessor statement.
3528                         if (($prevline =~ /^.\s*#\s*define\s/ ||
3529                             $prevline =~ /\\\s*$/) && $continuation == 0) {
3530                                 $check = 0;
3531                         }
3532
3533                         my $cond_ptr = -1;
3534                         $continuation = 0;
3535                         while ($cond_ptr != $cond_lines) {
3536                                 $cond_ptr = $cond_lines;
3537
3538                                 # If we see an #else/#elif then the code
3539                                 # is not linear.
3540                                 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3541                                         $check = 0;
3542                                 }
3543
3544                                 # Ignore:
3545                                 #  1) blank lines, they should be at 0,
3546                                 #  2) preprocessor lines, and
3547                                 #  3) labels.
3548                                 if ($continuation ||
3549                                     $s =~ /^\s*?\n/ ||
3550                                     $s =~ /^\s*#\s*?/ ||
3551                                     $s =~ /^\s*$Ident\s*:/) {
3552                                         $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
3553                                         if ($s =~ s/^.*?\n//) {
3554                                                 $cond_lines++;
3555                                         }
3556                                 }
3557                         }
3558
3559                         my (undef, $sindent) = line_stats("+" . $s);
3560                         my $stat_real = raw_line($linenr, $cond_lines);
3561
3562                         # Check if either of these lines are modified, else
3563                         # this is not this patch's fault.
3564                         if (!defined($stat_real) ||
3565                             $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3566                                 $check = 0;
3567                         }
3568                         if (defined($stat_real) && $cond_lines > 1) {
3569                                 $stat_real = "[...]\n$stat_real";
3570                         }
3571
3572                         #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";
3573
3574                         if ($check && $s ne '' &&
3575                             (($sindent % 8) != 0 ||
3576                              ($sindent < $indent) ||
3577                              ($sindent == $indent &&
3578                               ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
3579                              ($sindent > $indent + 8))) {
3580                                 WARN("SUSPECT_CODE_INDENT",
3581                                      "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
3582                         }
3583                 }
3584
3585                 # Track the 'values' across context and added lines.
3586                 my $opline = $line; $opline =~ s/^./ /;
3587                 my ($curr_values, $curr_vars) =
3588                                 annotate_values($opline . "\n", $prev_values);
3589                 $curr_values = $prev_values . $curr_values;
3590                 if ($dbg_values) {
3591 &