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