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