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