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