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