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