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