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