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