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