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