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