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