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