summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorAmit Sharma <amisharma@nvidia.com>2016-04-19 01:43:45 -0400
committerSri Krishna Chowdary <schowdary@nvidia.com>2016-04-19 12:23:35 -0400
commita626fba62e0031de7d10c82ea12e803905d9e384 (patch)
treebbf784308b7f87575780bf1f9d1e70532a439df5 /scripts
parent2ac8c9729a5b7ca0b0bdc053e72d2b4658f8bed7 (diff)
checkpatch: add checkpatch.pl
Add checkpatch.pl and dependent spelling.txt file to newly added kernel repo's. We might need to update the added files incase there is any modification happens in mentioned files within kernel-next. Bug 200192360 Change-Id: Id6eb52070ea7d76b81e9bff8461f1251a0e2c0b1 Signed-off-by: Amit Sharma <amisharma@nvidia.com> Reviewed-on: http://git-master/r/1128609 Reviewed-by: Deepak Nibade <dnibade@nvidia.com> Reviewed-by: Sri Krishna Chowdary <schowdary@nvidia.com>
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/checkpatch.pl5253
-rw-r--r--scripts/spelling.txt1042
2 files changed, 6295 insertions, 0 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
new file mode 100755
index 00000000..a47ea497
--- /dev/null
+++ b/scripts/checkpatch.pl
@@ -0,0 +1,5253 @@
1#!/usr/bin/perl -w
2# (c) 2001, Dave Jones. (the file handling bit)
3# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
9use POSIX;
10
11my $P = $0;
12$P =~ s@(.*)/@@g;
13my $D = $1;
14
15my $V = '0.32';
16
17use Getopt::Long qw(:config no_auto_abbrev);
18
19my $quiet = 0;
20my $tree = 1;
21my $chk_signoff = 1;
22my $ignore_changeid = 0;
23my $chk_patch = 1;
24my $tst_only;
25my $emacs = 0;
26my $terse = 0;
27my $file = 0;
28my $check = 0;
29my $check_orig = 0;
30my $summary = 1;
31my $mailback = 0;
32my $summary_file = 0;
33my $show_types = 0;
34my $fix = 0;
35my $fix_inplace = 0;
36my $root;
37my %debug;
38my %camelcase = ();
39my %use_type = ();
40my @use = ();
41my %ignore_type = ();
42my @ignore = ();
43my $help = 0;
44my $configuration_file = ".checkpatch.conf";
45my $max_line_length = 80;
46my $ignore_perl_version = 0;
47my $minimum_perl_version = 5.10.0;
48my $min_conf_desc_length = 4;
49my $spelling_file = "$D/spelling.txt";
50
51sub help {
52 my ($exitcode) = @_;
53
54 print << "EOM";
55Usage: $P [OPTION]... [FILE]...
56Version: $V
57
58Options:
59 -q, --quiet quiet
60 --no-tree run without a kernel tree
61 --no-signoff do not check for 'Signed-off-by' line
62 --ignore-changeid ignore Gerrit Change-Id
63 --patch treat FILE as patchfile (default)
64 --emacs emacs compile window format
65 --terse one line per report
66 -f, --file treat FILE as regular source file
67 --subjective, --strict enable more subjective tests
68 --types TYPE(,TYPE2...) show only these comma separated message types
69 --ignore TYPE(,TYPE2...) ignore various comma separated message types
70 --max-line-length=n set the maximum line length, if exceeded, warn
71 --min-conf-desc-length=n set the min description length, if shorter, warn
72 --show-types show the message "types" in the output
73 --root=PATH PATH to the kernel tree root
74 --no-summary suppress the per-file summary
75 --mailback only produce a report in case of warnings/errors
76 --summary-file include the filename in summary
77 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
78 'values', 'possible', 'type', and 'attr' (default
79 is all off)
80 --test-only=WORD report only warnings/errors containing WORD
81 literally
82 --fix EXPERIMENTAL - may create horrible results
83 If correctable single-line errors exist, create
84 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
85 with potential errors corrected to the preferred
86 checkpatch style
87 --fix-inplace EXPERIMENTAL - may create horrible results
88 Is the same as --fix, but overwrites the input
89 file. It's your fault if there's no backup or git
90 --ignore-perl-version override checking of perl version. expect
91 runtime errors.
92 -h, --help, --version display this help and exit
93
94When FILE is - read standard input.
95EOM
96
97 exit($exitcode);
98}
99
100my $conf = which_conf($configuration_file);
101if (-f $conf) {
102 my @conf_args;
103 open(my $conffile, '<', "$conf")
104 or warn "$P: Can't find a readable $configuration_file file $!\n";
105
106 while (<$conffile>) {
107 my $line = $_;
108
109 $line =~ s/\s*\n?$//g;
110 $line =~ s/^\s*//g;
111 $line =~ s/\s+/ /g;
112
113 next if ($line =~ m/^\s*#/);
114 next if ($line =~ m/^\s*$/);
115
116 my @words = split(" ", $line);
117 foreach my $word (@words) {
118 last if ($word =~ m/^#/);
119 push (@conf_args, $word);
120 }
121 }
122 close($conffile);
123 unshift(@ARGV, @conf_args) if @conf_args;
124}
125
126GetOptions(
127 'q|quiet+' => \$quiet,
128 'tree!' => \$tree,
129 'signoff!' => \$chk_signoff,
130 'ignore-changeid!' => \$ignore_changeid,
131 'patch!' => \$chk_patch,
132 'emacs!' => \$emacs,
133 'terse!' => \$terse,
134 'f|file!' => \$file,
135 'subjective!' => \$check,
136 'strict!' => \$check,
137 'ignore=s' => \@ignore,
138 'types=s' => \@use,
139 'show-types!' => \$show_types,
140 'max-line-length=i' => \$max_line_length,
141 'min-conf-desc-length=i' => \$min_conf_desc_length,
142 'root=s' => \$root,
143 'summary!' => \$summary,
144 'mailback!' => \$mailback,
145 'summary-file!' => \$summary_file,
146 'fix!' => \$fix,
147 'fix-inplace!' => \$fix_inplace,
148 'ignore-perl-version!' => \$ignore_perl_version,
149 'debug=s' => \%debug,
150 'test-only=s' => \$tst_only,
151 'h|help' => \$help,
152 'version' => \$help
153) or help(1);
154
155help(0) if ($help);
156
157$fix = 1 if ($fix_inplace);
158$check_orig = $check;
159
160my $exit = 0;
161
162if ($^V && $^V lt $minimum_perl_version) {
163 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
164 if (!$ignore_perl_version) {
165 exit(1);
166 }
167}
168
169if ($#ARGV < 0) {
170 print "$P: no input files\n";
171 exit(1);
172}
173
174sub hash_save_array_words {
175 my ($hashRef, $arrayRef) = @_;
176
177 my @array = split(/,/, join(',', @$arrayRef));
178 foreach my $word (@array) {
179 $word =~ s/\s*\n?$//g;
180 $word =~ s/^\s*//g;
181 $word =~ s/\s+/ /g;
182 $word =~ tr/[a-z]/[A-Z]/;
183
184 next if ($word =~ m/^\s*#/);
185 next if ($word =~ m/^\s*$/);
186
187 $hashRef->{$word}++;
188 }
189}
190
191sub hash_show_words {
192 my ($hashRef, $prefix) = @_;
193
194 if ($quiet == 0 && keys %$hashRef) {
195 print "NOTE: $prefix message types:";
196 foreach my $word (sort keys %$hashRef) {
197 print " $word";
198 }
199 print "\n\n";
200 }
201}
202
203hash_save_array_words(\%ignore_type, \@ignore);
204hash_save_array_words(\%use_type, \@use);
205
206my $dbg_values = 0;
207my $dbg_possible = 0;
208my $dbg_type = 0;
209my $dbg_attr = 0;
210for my $key (keys %debug) {
211 ## no critic
212 eval "\${dbg_$key} = '$debug{$key}';";
213 die "$@" if ($@);
214}
215
216my $rpt_cleaners = 0;
217
218if ($terse) {
219 $emacs = 1;
220 $quiet++;
221}
222
223if ($tree) {
224 if (defined $root) {
225 if (!top_of_kernel_tree($root)) {
226 die "$P: $root: --root does not point at a valid tree\n";
227 }
228 } else {
229 if (top_of_kernel_tree('.')) {
230 $root = '.';
231 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
232 top_of_kernel_tree($1)) {
233 $root = $1;
234 }
235 }
236
237 if (!defined $root) {
238 print "Must be run from the top-level dir. of a kernel tree\n";
239 exit(2);
240 }
241}
242
243my $emitted_corrupt = 0;
244
245our $Ident = qr{
246 [A-Za-z_][A-Za-z\d_]*
247 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
248 }x;
249our $Storage = qr{extern|static|asmlinkage};
250our $Sparse = qr{
251 __user|
252 __kernel|
253 __force|
254 __iomem|
255 __must_check|
256 __init_refok|
257 __kprobes|
258 __ref|
259 __rcu
260 }x;
261our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
262our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
263our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
264our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
265our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
266
267# Notes to $Attribute:
268# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
269our $Attribute = qr{
270 const|
271 __percpu|
272 __nocast|
273 __safe|
274 __bitwise__|
275 __packed__|
276 __packed2__|
277 __naked|
278 __maybe_unused|
279 __always_unused|
280 __noreturn|
281 __used|
282 __cold|
283 __noclone|
284 __deprecated|
285 __read_mostly|
286 __kprobes|
287 $InitAttribute|
288 ____cacheline_aligned|
289 ____cacheline_aligned_in_smp|
290 ____cacheline_internodealigned_in_smp|
291 __weak
292 }x;
293our $Modifier;
294our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
295our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
296our $Lval = qr{$Ident(?:$Member)*};
297
298our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
299our $Binary = qr{(?i)0b[01]+$Int_type?};
300our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
301our $Int = qr{[0-9]+$Int_type?};
302our $Octal = qr{0[0-7]+$Int_type?};
303our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
304our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
305our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
306our $Float = qr{$Float_hex|$Float_dec|$Float_int};
307our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
308our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
309our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
310our $Arithmetic = qr{\+|-|\*|\/|%};
311our $Operators = qr{
312 <=|>=|==|!=|
313 =>|->|<<|>>|<|>|!|~|
314 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
315 }x;
316
317our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
318
319our $NonptrType;
320our $NonptrTypeMisordered;
321our $NonptrTypeWithAttr;
322our $Type;
323our $TypeMisordered;
324our $Declare;
325our $DeclareMisordered;
326
327our $NON_ASCII_UTF8 = qr{
328 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
329 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
330 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
331 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
332 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
333 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
334 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
335}x;
336
337our $UTF8 = qr{
338 [\x09\x0A\x0D\x20-\x7E] # ASCII
339 | $NON_ASCII_UTF8
340}x;
341
342our $typeTypedefs = qr{(?x:
343 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
344 atomic_t
345)};
346
347our $logFunctions = qr{(?x:
348 printk(?:_ratelimited|_once|)|
349 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
350 WARN(?:_RATELIMIT|_ONCE|)|
351 panic|
352 MODULE_[A-Z_]+|
353 seq_vprintf|seq_printf|seq_puts
354)};
355
356our $signature_tags = qr{(?xi:
357 Signed-off-by:|
358 Acked-by:|
359 Tested-by:|
360 Reviewed-by:|
361 Reported-by:|
362 Suggested-by:|
363 To:|
364 Cc:
365)};
366
367our @typeListMisordered = (
368 qr{char\s+(?:un)?signed},
369 qr{int\s+(?:(?:un)?signed\s+)?short\s},
370 qr{int\s+short(?:\s+(?:un)?signed)},
371 qr{short\s+int(?:\s+(?:un)?signed)},
372 qr{(?:un)?signed\s+int\s+short},
373 qr{short\s+(?:un)?signed},
374 qr{long\s+int\s+(?:un)?signed},
375 qr{int\s+long\s+(?:un)?signed},
376 qr{long\s+(?:un)?signed\s+int},
377 qr{int\s+(?:un)?signed\s+long},
378 qr{int\s+(?:un)?signed},
379 qr{int\s+long\s+long\s+(?:un)?signed},
380 qr{long\s+long\s+int\s+(?:un)?signed},
381 qr{long\s+long\s+(?:un)?signed\s+int},
382 qr{long\s+long\s+(?:un)?signed},
383 qr{long\s+(?:un)?signed},
384);
385
386our @typeList = (
387 qr{void},
388 qr{(?:(?:un)?signed\s+)?char},
389 qr{(?:(?:un)?signed\s+)?short\s+int},
390 qr{(?:(?:un)?signed\s+)?short},
391 qr{(?:(?:un)?signed\s+)?int},
392 qr{(?:(?:un)?signed\s+)?long\s+int},
393 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
394 qr{(?:(?:un)?signed\s+)?long\s+long},
395 qr{(?:(?:un)?signed\s+)?long},
396 qr{(?:un)?signed},
397 qr{float},
398 qr{double},
399 qr{bool},
400 qr{struct\s+$Ident},
401 qr{union\s+$Ident},
402 qr{enum\s+$Ident},
403 qr{${Ident}_t},
404 qr{${Ident}_handler},
405 qr{${Ident}_handler_fn},
406 @typeListMisordered,
407);
408our @typeListWithAttr = (
409 @typeList,
410 qr{struct\s+$InitAttribute\s+$Ident},
411 qr{union\s+$InitAttribute\s+$Ident},
412);
413
414our @modifierList = (
415 qr{fastcall},
416);
417
418our @mode_permission_funcs = (
419 ["module_param", 3],
420 ["module_param_(?:array|named|string)", 4],
421 ["module_param_array_named", 5],
422 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
423 ["proc_create(?:_data|)", 2],
424 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
425);
426
427#Create a search pattern for all these functions to speed up a loop below
428our $mode_perms_search = "";
429foreach my $entry (@mode_permission_funcs) {
430 $mode_perms_search .= '|' if ($mode_perms_search ne "");
431 $mode_perms_search .= $entry->[0];
432}
433
434our $allowed_asm_includes = qr{(?x:
435 irq|
436 memory|
437 time|
438 reboot
439)};
440# memory.h: ARM has a custom one
441
442# Load common spelling mistakes and build regular expression list.
443my $misspellings;
444my @spelling_list;
445my %spelling_fix;
446open(my $spelling, '<', $spelling_file)
447 or die "$P: Can't open $spelling_file for reading: $!\n";
448while (<$spelling>) {
449 my $line = $_;
450
451 $line =~ s/\s*\n?$//g;
452 $line =~ s/^\s*//g;
453
454 next if ($line =~ m/^\s*#/);
455 next if ($line =~ m/^\s*$/);
456
457 my ($suspect, $fix) = split(/\|\|/, $line);
458
459 push(@spelling_list, $suspect);
460 $spelling_fix{$suspect} = $fix;
461}
462close($spelling);
463$misspellings = join("|", @spelling_list);
464
465sub build_types {
466 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
467 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
468 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
469 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
470 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
471 $NonptrType = qr{
472 (?:$Modifier\s+|const\s+)*
473 (?:
474 (?:typeof|__typeof__)\s*\([^\)]*\)|
475 (?:$typeTypedefs\b)|
476 (?:${all}\b)
477 )
478 (?:\s+$Modifier|\s+const)*
479 }x;
480 $NonptrTypeMisordered = qr{
481 (?:$Modifier\s+|const\s+)*
482 (?:
483 (?:${Misordered}\b)
484 )
485 (?:\s+$Modifier|\s+const)*
486 }x;
487 $NonptrTypeWithAttr = qr{
488 (?:$Modifier\s+|const\s+)*
489 (?:
490 (?:typeof|__typeof__)\s*\([^\)]*\)|
491 (?:$typeTypedefs\b)|
492 (?:${allWithAttr}\b)
493 )
494 (?:\s+$Modifier|\s+const)*
495 }x;
496 $Type = qr{
497 $NonptrType
498 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
499 (?:\s+$Inline|\s+$Modifier)*
500 }x;
501 $TypeMisordered = qr{
502 $NonptrTypeMisordered
503 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
504 (?:\s+$Inline|\s+$Modifier)*
505 }x;
506 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
507 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
508}
509build_types();
510
511our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
512
513# Using $balanced_parens, $LvalOrFunc, or $FuncArg
514# requires at least perl version v5.10.0
515# Any use must be runtime checked with $^V
516
517our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
518our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
519our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
520
521our $declaration_macros = qr{(?x:
522 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
523 (?:$Storage\s+)?LIST_HEAD\s*\(|
524 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
525)};
526
527sub deparenthesize {
528 my ($string) = @_;
529 return "" if (!defined($string));
530
531 while ($string =~ /^\s*\(.*\)\s*$/) {
532 $string =~ s@^\s*\(\s*@@;
533 $string =~ s@\s*\)\s*$@@;
534 }
535
536 $string =~ s@\s+@ @g;
537
538 return $string;
539}
540
541sub seed_camelcase_file {
542 my ($file) = @_;
543
544 return if (!(-f $file));
545
546 local $/;
547
548 open(my $include_file, '<', "$file")
549 or warn "$P: Can't read '$file' $!\n";
550 my $text = <$include_file>;
551 close($include_file);
552
553 my @lines = split('\n', $text);
554
555 foreach my $line (@lines) {
556 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
557 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
558 $camelcase{$1} = 1;
559 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
560 $camelcase{$1} = 1;
561 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
562 $camelcase{$1} = 1;
563 }
564 }
565}
566
567my $camelcase_seeded = 0;
568sub seed_camelcase_includes {
569 return if ($camelcase_seeded);
570
571 my $files;
572 my $camelcase_cache = "";
573 my @include_files = ();
574
575 $camelcase_seeded = 1;
576
577 if (-e ".git") {
578 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
579 chomp $git_last_include_commit;
580 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
581 } else {
582 my $last_mod_date = 0;
583 $files = `find $root/include -name "*.h"`;
584 @include_files = split('\n', $files);
585 foreach my $file (@include_files) {
586 my $date = POSIX::strftime("%Y%m%d%H%M",
587 localtime((stat $file)[9]));
588 $last_mod_date = $date if ($last_mod_date < $date);
589 }
590 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
591 }
592
593 if ($camelcase_cache ne "" && -f $camelcase_cache) {
594 open(my $camelcase_file, '<', "$camelcase_cache")
595 or warn "$P: Can't read '$camelcase_cache' $!\n";
596 while (<$camelcase_file>) {
597 chomp;
598 $camelcase{$_} = 1;
599 }
600 close($camelcase_file);
601
602 return;
603 }
604
605 if (-e ".git") {
606 $files = `git ls-files "include/*.h"`;
607 @include_files = split('\n', $files);
608 }
609
610 foreach my $file (@include_files) {
611 seed_camelcase_file($file);
612 }
613
614 if ($camelcase_cache ne "") {
615 unlink glob ".checkpatch-camelcase.*";
616 open(my $camelcase_file, '>', "$camelcase_cache")
617 or warn "$P: Can't write '$camelcase_cache' $!\n";
618 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
619 print $camelcase_file ("$_\n");
620 }
621 close($camelcase_file);
622 }
623}
624
625sub git_commit_info {
626 my ($commit, $id, $desc) = @_;
627
628 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
629
630 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
631 $output =~ s/^\s*//gm;
632 my @lines = split("\n", $output);
633
634 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
635# Maybe one day convert this block of bash into something that returns
636# all matching commit ids, but it's very slow...
637#
638# echo "checking commits $1..."
639# git rev-list --remotes | grep -i "^$1" |
640# while read line ; do
641# git log --format='%H %s' -1 $line |
642# echo "commit $(cut -c 1-12,41-)"
643# done
644 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
645 } else {
646 $id = substr($lines[0], 0, 12);
647 $desc = substr($lines[0], 41);
648 }
649
650 return ($id, $desc);
651}
652
653$chk_signoff = 0 if ($file);
654
655my @rawlines = ();
656my @lines = ();
657my @fixed = ();
658my @fixed_inserted = ();
659my @fixed_deleted = ();
660my $fixlinenr = -1;
661
662my $vname;
663for my $filename (@ARGV) {
664 my $FILE;
665 if ($file) {
666 open($FILE, '-|', "diff -u /dev/null $filename") ||
667 die "$P: $filename: diff failed - $!\n";
668 } elsif ($filename eq '-') {
669 open($FILE, '<&STDIN');
670 } else {
671 open($FILE, '<', "$filename") ||
672 die "$P: $filename: open failed - $!\n";
673 }
674 if ($filename eq '-') {
675 $vname = 'Your patch';
676 } else {
677 $vname = $filename;
678 }
679 while (<$FILE>) {
680 chomp;
681 push(@rawlines, $_);
682 }
683 close($FILE);
684 if (!process($filename)) {
685 $exit = 1;
686 }
687 @rawlines = ();
688 @lines = ();
689 @fixed = ();
690 @fixed_inserted = ();
691 @fixed_deleted = ();
692 $fixlinenr = -1;
693}
694
695exit($exit);
696
697sub top_of_kernel_tree {
698 my ($root) = @_;
699
700 my @tree_check = (
701 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
702 "README", "Documentation", "arch", "include", "drivers",
703 "fs", "init", "ipc", "kernel", "lib", "scripts",
704 );
705
706 foreach my $check (@tree_check) {
707 if (! -e $root . '/' . $check) {
708 return 0;
709 }
710 }
711 return 1;
712}
713
714sub parse_email {
715 my ($formatted_email) = @_;
716
717 my $name = "";
718 my $address = "";
719 my $comment = "";
720
721 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
722 $name = $1;
723 $address = $2;
724 $comment = $3 if defined $3;
725 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
726 $address = $1;
727 $comment = $2 if defined $2;
728 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
729 $address = $1;
730 $comment = $2 if defined $2;
731 $formatted_email =~ s/$address.*$//;
732 $name = $formatted_email;
733 $name = trim($name);
734 $name =~ s/^\"|\"$//g;
735 # If there's a name left after stripping spaces and
736 # leading quotes, and the address doesn't have both
737 # leading and trailing angle brackets, the address
738 # is invalid. ie:
739 # "joe smith joe@smith.com" bad
740 # "joe smith <joe@smith.com" bad
741 if ($name ne "" && $address !~ /^<[^>]+>$/) {
742 $name = "";
743 $address = "";
744 $comment = "";
745 }
746 }
747
748 $name = trim($name);
749 $name =~ s/^\"|\"$//g;
750 $address = trim($address);
751 $address =~ s/^\<|\>$//g;
752
753 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
754 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
755 $name = "\"$name\"";
756 }
757
758 return ($name, $address, $comment);
759}
760
761sub format_email {
762 my ($name, $address) = @_;
763
764 my $formatted_email;
765
766 $name = trim($name);
767 $name =~ s/^\"|\"$//g;
768 $address = trim($address);
769
770 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
771 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
772 $name = "\"$name\"";
773 }
774
775 if ("$name" eq "") {
776 $formatted_email = "$address";
777 } else {
778 $formatted_email = "$name <$address>";
779 }
780
781 return $formatted_email;
782}
783
784sub which {
785 my ($bin) = @_;
786
787 foreach my $path (split(/:/, $ENV{PATH})) {
788 if (-e "$path/$bin") {
789 return "$path/$bin";
790 }
791 }
792
793 return "";
794}
795
796sub which_conf {
797 my ($conf) = @_;
798
799 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
800 if (-e "$path/$conf") {
801 return "$path/$conf";
802 }
803 }
804
805 return "";
806}
807
808sub expand_tabs {
809 my ($str) = @_;
810
811 my $res = '';
812 my $n = 0;
813 for my $c (split(//, $str)) {
814 if ($c eq "\t") {
815 $res .= ' ';
816 $n++;
817 for (; ($n % 8) != 0; $n++) {
818 $res .= ' ';
819 }
820 next;
821 }
822 $res .= $c;
823 $n++;
824 }
825
826 return $res;
827}
828sub copy_spacing {
829 (my $res = shift) =~ tr/\t/ /c;
830 return $res;
831}
832
833sub line_stats {
834 my ($line) = @_;
835
836 # Drop the diff line leader and expand tabs
837 $line =~ s/^.//;
838 $line = expand_tabs($line);
839
840 # Pick the indent from the front of the line.
841 my ($white) = ($line =~ /^(\s*)/);
842
843 return (length($line), length($white));
844}
845
846my $sanitise_quote = '';
847
848sub sanitise_line_reset {
849 my ($in_comment) = @_;
850
851 if ($in_comment) {
852 $sanitise_quote = '*/';
853 } else {
854 $sanitise_quote = '';
855 }
856}
857sub sanitise_line {
858 my ($line) = @_;
859
860 my $res = '';
861 my $l = '';
862
863 my $qlen = 0;
864 my $off = 0;
865 my $c;
866
867 # Always copy over the diff marker.
868 $res = substr($line, 0, 1);
869
870 for ($off = 1; $off < length($line); $off++) {
871 $c = substr($line, $off, 1);
872
873 # Comments we are wacking completly including the begin
874 # and end, all to $;.
875 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
876 $sanitise_quote = '*/';
877
878 substr($res, $off, 2, "$;$;");
879 $off++;
880 next;
881 }
882 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
883 $sanitise_quote = '';
884 substr($res, $off, 2, "$;$;");
885 $off++;
886 next;
887 }
888 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
889 $sanitise_quote = '//';
890
891 substr($res, $off, 2, $sanitise_quote);
892 $off++;
893 next;
894 }
895
896 # A \ in a string means ignore the next character.
897 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
898 $c eq "\\") {
899 substr($res, $off, 2, 'XX');
900 $off++;
901 next;
902 }
903 # Regular quotes.
904 if ($c eq "'" || $c eq '"') {
905 if ($sanitise_quote eq '') {
906 $sanitise_quote = $c;
907
908 substr($res, $off, 1, $c);
909 next;
910 } elsif ($sanitise_quote eq $c) {
911 $sanitise_quote = '';
912 }
913 }
914
915 #print "c<$c> SQ<$sanitise_quote>\n";
916 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
917 substr($res, $off, 1, $;);
918 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
919 substr($res, $off, 1, $;);
920 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
921 substr($res, $off, 1, 'X');
922 } else {
923 substr($res, $off, 1, $c);
924 }
925 }
926
927 if ($sanitise_quote eq '//') {
928 $sanitise_quote = '';
929 }
930
931 # The pathname on a #include may be surrounded by '<' and '>'.
932 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
933 my $clean = 'X' x length($1);
934 $res =~ s@\<.*\>@<$clean>@;
935
936 # The whole of a #error is a string.
937 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
938 my $clean = 'X' x length($1);
939 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
940 }
941
942 return $res;
943}
944
945sub get_quoted_string {
946 my ($line, $rawline) = @_;
947
948 return "" if ($line !~ m/(\"[X]+\")/g);
949 return substr($rawline, $-[0], $+[0] - $-[0]);
950}
951
952sub ctx_statement_block {
953 my ($linenr, $remain, $off) = @_;
954 my $line = $linenr - 1;
955 my $blk = '';
956 my $soff = $off;
957 my $coff = $off - 1;
958 my $coff_set = 0;
959
960 my $loff = 0;
961
962 my $type = '';
963 my $level = 0;
964 my @stack = ();
965 my $p;
966 my $c;
967 my $len = 0;
968
969 my $remainder;
970 while (1) {
971 @stack = (['', 0]) if ($#stack == -1);
972
973 #warn "CSB: blk<$blk> remain<$remain>\n";
974 # If we are about to drop off the end, pull in more
975 # context.
976 if ($off >= $len) {
977 for (; $remain > 0; $line++) {
978 last if (!defined $lines[$line]);
979 next if ($lines[$line] =~ /^-/);
980 $remain--;
981 $loff = $len;
982 $blk .= $lines[$line] . "\n";
983 $len = length($blk);
984 $line++;
985 last;
986 }
987 # Bail if there is no further context.
988 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
989 if ($off >= $len) {
990 last;
991 }
992 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
993 $level++;
994 $type = '#';
995 }
996 }
997 $p = $c;
998 $c = substr($blk, $off, 1);
999 $remainder = substr($blk, $off);
1000
1001 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1002
1003 # Handle nested #if/#else.
1004 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1005 push(@stack, [ $type, $level ]);
1006 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1007 ($type, $level) = @{$stack[$#stack - 1]};
1008 } elsif ($remainder =~ /^#\s*endif\b/) {
1009 ($type, $level) = @{pop(@stack)};
1010 }
1011
1012 # Statement ends at the ';' or a close '}' at the
1013 # outermost level.
1014 if ($level == 0 && $c eq ';') {
1015 last;
1016 }
1017
1018 # An else is really a conditional as long as its not else if
1019 if ($level == 0 && $coff_set == 0 &&
1020 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1021 $remainder =~ /^(else)(?:\s|{)/ &&
1022 $remainder !~ /^else\s+if\b/) {
1023 $coff = $off + length($1) - 1;
1024 $coff_set = 1;
1025 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1026 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1027 }
1028
1029 if (($type eq '' || $type eq '(') && $c eq '(') {
1030 $level++;
1031 $type = '(';
1032 }
1033 if ($type eq '(' && $c eq ')') {
1034 $level--;
1035 $type = ($level != 0)? '(' : '';
1036
1037 if ($level == 0 && $coff < $soff) {
1038 $coff = $off;
1039 $coff_set = 1;
1040 #warn "CSB: mark coff<$coff>\n";
1041 }
1042 }
1043 if (($type eq '' || $type eq '{') && $c eq '{') {
1044 $level++;
1045 $type = '{';
1046 }
1047 if ($type eq '{' && $c eq '}') {
1048 $level--;
1049 $type = ($level != 0)? '{' : '';
1050
1051 if ($level == 0) {
1052 if (substr($blk, $off + 1, 1) eq ';') {
1053 $off++;
1054 }
1055 last;
1056 }
1057 }
1058 # Preprocessor commands end at the newline unless escaped.
1059 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1060 $level--;
1061 $type = '';
1062 $off++;
1063 last;
1064 }
1065 $off++;
1066 }
1067 # We are truly at the end, so shuffle to the next line.
1068 if ($off == $len) {
1069 $loff = $len + 1;
1070 $line++;
1071 $remain--;
1072 }
1073
1074 my $statement = substr($blk, $soff, $off - $soff + 1);
1075 my $condition = substr($blk, $soff, $coff - $soff + 1);
1076
1077 #warn "STATEMENT<$statement>\n";
1078 #warn "CONDITION<$condition>\n";
1079
1080 #print "coff<$coff> soff<$off> loff<$loff>\n";
1081
1082 return ($statement, $condition,
1083 $line, $remain + 1, $off - $loff + 1, $level);
1084}
1085
1086sub statement_lines {
1087 my ($stmt) = @_;
1088
1089 # Strip the diff line prefixes and rip blank lines at start and end.
1090 $stmt =~ s/(^|\n)./$1/g;
1091 $stmt =~ s/^\s*//;
1092 $stmt =~ s/\s*$//;
1093
1094 my @stmt_lines = ($stmt =~ /\n/g);
1095
1096 return $#stmt_lines + 2;
1097}
1098
1099sub statement_rawlines {
1100 my ($stmt) = @_;
1101
1102 my @stmt_lines = ($stmt =~ /\n/g);
1103
1104 return $#stmt_lines + 2;
1105}
1106
1107sub statement_block_size {
1108 my ($stmt) = @_;
1109
1110 $stmt =~ s/(^|\n)./$1/g;
1111 $stmt =~ s/^\s*{//;
1112 $stmt =~ s/}\s*$//;
1113 $stmt =~ s/^\s*//;
1114 $stmt =~ s/\s*$//;
1115
1116 my @stmt_lines = ($stmt =~ /\n/g);
1117 my @stmt_statements = ($stmt =~ /;/g);
1118
1119 my $stmt_lines = $#stmt_lines + 2;
1120 my $stmt_statements = $#stmt_statements + 1;
1121
1122 if ($stmt_lines > $stmt_statements) {
1123 return $stmt_lines;
1124 } else {
1125 return $stmt_statements;
1126 }
1127}
1128
1129sub ctx_statement_full {
1130 my ($linenr, $remain, $off) = @_;
1131 my ($statement, $condition, $level);
1132
1133 my (@chunks);
1134
1135 # Grab the first conditional/block pair.
1136 ($statement, $condition, $linenr, $remain, $off, $level) =
1137 ctx_statement_block($linenr, $remain, $off);
1138 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1139 push(@chunks, [ $condition, $statement ]);
1140 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1141 return ($level, $linenr, @chunks);
1142 }
1143
1144 # Pull in the following conditional/block pairs and see if they
1145 # could continue the statement.
1146 for (;;) {
1147 ($statement, $condition, $linenr, $remain, $off, $level) =
1148 ctx_statement_block($linenr, $remain, $off);
1149 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1150 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1151 #print "C: push\n";
1152 push(@chunks, [ $condition, $statement ]);
1153 }
1154
1155 return ($level, $linenr, @chunks);
1156}
1157
1158sub ctx_block_get {
1159 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1160 my $line;
1161 my $start = $linenr - 1;
1162 my $blk = '';
1163 my @o;
1164 my @c;
1165 my @res = ();
1166
1167 my $level = 0;
1168 my @stack = ($level);
1169 for ($line = $start; $remain > 0; $line++) {
1170 next if ($rawlines[$line] =~ /^-/);
1171 $remain--;
1172
1173 $blk .= $rawlines[$line];
1174
1175 # Handle nested #if/#else.
1176 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1177 push(@stack, $level);
1178 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1179 $level = $stack[$#stack - 1];
1180 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1181 $level = pop(@stack);
1182 }
1183
1184 foreach my $c (split(//, $lines[$line])) {
1185 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1186 if ($off > 0) {
1187 $off--;
1188 next;
1189 }
1190
1191 if ($c eq $close && $level > 0) {
1192 $level--;
1193 last if ($level == 0);
1194 } elsif ($c eq $open) {
1195 $level++;
1196 }
1197 }
1198
1199 if (!$outer || $level <= 1) {
1200 push(@res, $rawlines[$line]);
1201 }
1202
1203 last if ($level == 0);
1204 }
1205
1206 return ($level, @res);
1207}
1208sub ctx_block_outer {
1209 my ($linenr, $remain) = @_;
1210
1211 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1212 return @r;
1213}
1214sub ctx_block {
1215 my ($linenr, $remain) = @_;
1216
1217 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1218 return @r;
1219}
1220sub ctx_statement {
1221 my ($linenr, $remain, $off) = @_;
1222
1223 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1224 return @r;
1225}
1226sub ctx_block_level {
1227 my ($linenr, $remain) = @_;
1228
1229 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1230}
1231sub ctx_statement_level {
1232 my ($linenr, $remain, $off) = @_;
1233
1234 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1235}
1236
1237sub ctx_locate_comment {
1238 my ($first_line, $end_line) = @_;
1239
1240 # Catch a comment on the end of the line itself.
1241 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1242 return $current_comment if (defined $current_comment);
1243
1244 # Look through the context and try and figure out if there is a
1245 # comment.
1246 my $in_comment = 0;
1247 $current_comment = '';
1248 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1249 my $line = $rawlines[$linenr - 1];
1250 #warn " $line\n";
1251 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1252 $in_comment = 1;
1253 }
1254 if ($line =~ m@/\*@) {
1255 $in_comment = 1;
1256 }
1257 if (!$in_comment && $current_comment ne '') {
1258 $current_comment = '';
1259 }
1260 $current_comment .= $line . "\n" if ($in_comment);
1261 if ($line =~ m@\*/@) {
1262 $in_comment = 0;
1263 }
1264 }
1265
1266 chomp($current_comment);
1267 return($current_comment);
1268}
1269sub ctx_has_comment {
1270 my ($first_line, $end_line) = @_;
1271 my $cmt = ctx_locate_comment($first_line, $end_line);
1272
1273 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1274 ##print "CMMT: $cmt\n";
1275
1276 return ($cmt ne '');
1277}
1278
1279sub raw_line {
1280 my ($linenr, $cnt) = @_;
1281
1282 my $offset = $linenr - 1;
1283 $cnt++;
1284
1285 my $line;
1286 while ($cnt) {
1287 $line = $rawlines[$offset++];
1288 next if (defined($line) && $line =~ /^-/);
1289 $cnt--;
1290 }
1291
1292 return $line;
1293}
1294
1295sub cat_vet {
1296 my ($vet) = @_;
1297 my ($res, $coded);
1298
1299 $res = '';
1300 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1301 $res .= $1;
1302 if ($2 ne '') {
1303 $coded = sprintf("^%c", unpack('C', $2) + 64);
1304 $res .= $coded;
1305 }
1306 }
1307 $res =~ s/$/\$/;
1308
1309 return $res;
1310}
1311
1312my $av_preprocessor = 0;
1313my $av_pending;
1314my @av_paren_type;
1315my $av_pend_colon;
1316
1317sub annotate_reset {
1318 $av_preprocessor = 0;
1319 $av_pending = '_';
1320 @av_paren_type = ('E');
1321 $av_pend_colon = 'O';
1322}
1323
1324sub annotate_values {
1325 my ($stream, $type) = @_;
1326
1327 my $res;
1328 my $var = '_' x length($stream);
1329 my $cur = $stream;
1330
1331 print "$stream\n" if ($dbg_values > 1);
1332
1333 while (length($cur)) {
1334 @av_paren_type = ('E') if ($#av_paren_type < 0);
1335 print " <" . join('', @av_paren_type) .
1336 "> <$type> <$av_pending>" if ($dbg_values > 1);
1337 if ($cur =~ /^(\s+)/o) {
1338 print "WS($1)\n" if ($dbg_values > 1);
1339 if ($1 =~ /\n/ && $av_preprocessor) {
1340 $type = pop(@av_paren_type);
1341 $av_preprocessor = 0;
1342 }
1343
1344 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1345 print "CAST($1)\n" if ($dbg_values > 1);
1346 push(@av_paren_type, $type);
1347 $type = 'c';
1348
1349 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1350 print "DECLARE($1)\n" if ($dbg_values > 1);
1351 $type = 'T';
1352
1353 } elsif ($cur =~ /^($Modifier)\s*/) {
1354 print "MODIFIER($1)\n" if ($dbg_values > 1);
1355 $type = 'T';
1356
1357 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1358 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1359 $av_preprocessor = 1;
1360 push(@av_paren_type, $type);
1361 if ($2 ne '') {
1362 $av_pending = 'N';
1363 }
1364 $type = 'E';
1365
1366 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1367 print "UNDEF($1)\n" if ($dbg_values > 1);
1368 $av_preprocessor = 1;
1369 push(@av_paren_type, $type);
1370
1371 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1372 print "PRE_START($1)\n" if ($dbg_values > 1);
1373 $av_preprocessor = 1;
1374
1375 push(@av_paren_type, $type);
1376 push(@av_paren_type, $type);
1377 $type = 'E';
1378
1379 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1380 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1381 $av_preprocessor = 1;
1382
1383 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1384
1385 $type = 'E';
1386
1387 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1388 print "PRE_END($1)\n" if ($dbg_values > 1);
1389
1390 $av_preprocessor = 1;
1391
1392 # Assume all arms of the conditional end as this
1393 # one does, and continue as if the #endif was not here.
1394 pop(@av_paren_type);
1395 push(@av_paren_type, $type);
1396 $type = 'E';
1397
1398 } elsif ($cur =~ /^(\\\n)/o) {
1399 print "PRECONT($1)\n" if ($dbg_values > 1);
1400
1401 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1402 print "ATTR($1)\n" if ($dbg_values > 1);
1403 $av_pending = $type;
1404 $type = 'N';
1405
1406 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1407 print "SIZEOF($1)\n" if ($dbg_values > 1);
1408 if (defined $2) {
1409 $av_pending = 'V';
1410 }
1411 $type = 'N';
1412
1413 } elsif ($cur =~ /^(if|while|for)\b/o) {
1414 print "COND($1)\n" if ($dbg_values > 1);
1415 $av_pending = 'E';
1416 $type = 'N';
1417
1418 } elsif ($cur =~/^(case)/o) {
1419 print "CASE($1)\n" if ($dbg_values > 1);
1420 $av_pend_colon = 'C';
1421 $type = 'N';
1422
1423 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1424 print "KEYWORD($1)\n" if ($dbg_values > 1);
1425 $type = 'N';
1426
1427 } elsif ($cur =~ /^(\()/o) {
1428 print "PAREN('$1')\n" if ($dbg_values > 1);
1429 push(@av_paren_type, $av_pending);
1430 $av_pending = '_';
1431 $type = 'N';
1432
1433 } elsif ($cur =~ /^(\))/o) {
1434 my $new_type = pop(@av_paren_type);
1435 if ($new_type ne '_') {
1436 $type = $new_type;
1437 print "PAREN('$1') -> $type\n"
1438 if ($dbg_values > 1);
1439 } else {
1440 print "PAREN('$1')\n" if ($dbg_values > 1);
1441 }
1442
1443 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1444 print "FUNC($1)\n" if ($dbg_values > 1);
1445 $type = 'V';
1446 $av_pending = 'V';
1447
1448 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1449 if (defined $2 && $type eq 'C' || $type eq 'T') {
1450 $av_pend_colon = 'B';
1451 } elsif ($type eq 'E') {
1452 $av_pend_colon = 'L';
1453 }
1454 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1455 $type = 'V';
1456
1457 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1458 print "IDENT($1)\n" if ($dbg_values > 1);
1459 $type = 'V';
1460
1461 } elsif ($cur =~ /^($Assignment)/o) {
1462 print "ASSIGN($1)\n" if ($dbg_values > 1);
1463 $type = 'N';
1464
1465 } elsif ($cur =~/^(;|{|})/) {
1466 print "END($1)\n" if ($dbg_values > 1);
1467 $type = 'E';
1468 $av_pend_colon = 'O';
1469
1470 } elsif ($cur =~/^(,)/) {
1471 print "COMMA($1)\n" if ($dbg_values > 1);
1472 $type = 'C';
1473
1474 } elsif ($cur =~ /^(\?)/o) {
1475 print "QUESTION($1)\n" if ($dbg_values > 1);
1476 $type = 'N';
1477
1478 } elsif ($cur =~ /^(:)/o) {
1479 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1480
1481 substr($var, length($res), 1, $av_pend_colon);
1482 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1483 $type = 'E';
1484 } else {
1485 $type = 'N';
1486 }
1487 $av_pend_colon = 'O';
1488
1489 } elsif ($cur =~ /^(\[)/o) {
1490 print "CLOSE($1)\n" if ($dbg_values > 1);
1491 $type = 'N';
1492
1493 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1494 my $variant;
1495
1496 print "OPV($1)\n" if ($dbg_values > 1);
1497 if ($type eq 'V') {
1498 $variant = 'B';
1499 } else {
1500 $variant = 'U';
1501 }
1502
1503 substr($var, length($res), 1, $variant);
1504 $type = 'N';
1505
1506 } elsif ($cur =~ /^($Operators)/o) {
1507 print "OP($1)\n" if ($dbg_values > 1);
1508 if ($1 ne '++' && $1 ne '--') {
1509 $type = 'N';
1510 }
1511
1512 } elsif ($cur =~ /(^.)/o) {
1513 print "C($1)\n" if ($dbg_values > 1);
1514 }
1515 if (defined $1) {
1516 $cur = substr($cur, length($1));
1517 $res .= $type x length($1);
1518 }
1519 }
1520
1521 return ($res, $var);
1522}
1523
1524sub possible {
1525 my ($possible, $line) = @_;
1526 my $notPermitted = qr{(?:
1527 ^(?:
1528 $Modifier|
1529 $Storage|
1530 $Type|
1531 DEFINE_\S+
1532 )$|
1533 ^(?:
1534 goto|
1535 return|
1536 case|
1537 else|
1538 asm|__asm__|
1539 do|
1540 \#|
1541 \#\#|
1542 )(?:\s|$)|
1543 ^(?:typedef|struct|enum)\b
1544 )}x;
1545 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1546 if ($possible !~ $notPermitted) {
1547 # Check for modifiers.
1548 $possible =~ s/\s*$Storage\s*//g;
1549 $possible =~ s/\s*$Sparse\s*//g;
1550 if ($possible =~ /^\s*$/) {
1551
1552 } elsif ($possible =~ /\s/) {
1553 $possible =~ s/\s*$Type\s*//g;
1554 for my $modifier (split(' ', $possible)) {
1555 if ($modifier !~ $notPermitted) {
1556 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1557 push(@modifierList, $modifier);
1558 }
1559 }
1560
1561 } else {
1562 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1563 push(@typeList, $possible);
1564 }
1565 build_types();
1566 } else {
1567 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1568 }
1569}
1570
1571my $prefix = '';
1572
1573sub show_type {
1574 my ($type) = @_;
1575
1576 return defined $use_type{$type} if (scalar keys %use_type > 0);
1577
1578 return !defined $ignore_type{$type};
1579}
1580
1581sub report {
1582 my ($level, $type, $msg) = @_;
1583
1584 if (!show_type($type) ||
1585 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1586 return 0;
1587 }
1588 my $line;
1589 if ($show_types) {
1590 $line = "$prefix$level:$type: $msg\n";
1591 } else {
1592 $line = "$prefix$level: $msg\n";
1593 }
1594 $line = (split('\n', $line))[0] . "\n" if ($terse);
1595
1596 push(our @report, $line);
1597
1598 return 1;
1599}
1600
1601sub report_dump {
1602 our @report;
1603}
1604
1605sub fixup_current_range {
1606 my ($lineRef, $offset, $length) = @_;
1607
1608 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1609 my $o = $1;
1610 my $l = $2;
1611 my $no = $o + $offset;
1612 my $nl = $l + $length;
1613 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1614 }
1615}
1616
1617sub fix_inserted_deleted_lines {
1618 my ($linesRef, $insertedRef, $deletedRef) = @_;
1619
1620 my $range_last_linenr = 0;
1621 my $delta_offset = 0;
1622
1623 my $old_linenr = 0;
1624 my $new_linenr = 0;
1625
1626 my $next_insert = 0;
1627 my $next_delete = 0;
1628
1629 my @lines = ();
1630
1631 my $inserted = @{$insertedRef}[$next_insert++];
1632 my $deleted = @{$deletedRef}[$next_delete++];
1633
1634 foreach my $old_line (@{$linesRef}) {
1635 my $save_line = 1;
1636 my $line = $old_line; #don't modify the array
1637 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1638 $delta_offset = 0;
1639 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1640 $range_last_linenr = $new_linenr;
1641 fixup_current_range(\$line, $delta_offset, 0);
1642 }
1643
1644 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1645 $deleted = @{$deletedRef}[$next_delete++];
1646 $save_line = 0;
1647 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1648 }
1649
1650 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1651 push(@lines, ${$inserted}{'LINE'});
1652 $inserted = @{$insertedRef}[$next_insert++];
1653 $new_linenr++;
1654 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1655 }
1656
1657 if ($save_line) {
1658 push(@lines, $line);
1659 $new_linenr++;
1660 }
1661
1662 $old_linenr++;
1663 }
1664
1665 return @lines;
1666}
1667
1668sub fix_insert_line {
1669 my ($linenr, $line) = @_;
1670
1671 my $inserted = {
1672 LINENR => $linenr,
1673 LINE => $line,
1674 };
1675 push(@fixed_inserted, $inserted);
1676}
1677
1678sub fix_delete_line {
1679 my ($linenr, $line) = @_;
1680
1681 my $deleted = {
1682 LINENR => $linenr,
1683 LINE => $line,
1684 };
1685
1686 push(@fixed_deleted, $deleted);
1687}
1688
1689sub ERROR {
1690 my ($type, $msg) = @_;
1691
1692 if (report("ERROR", $type, $msg)) {
1693 our $clean = 0;
1694 our $cnt_error++;
1695 return 1;
1696 }
1697 return 0;
1698}
1699sub WARN {
1700 my ($type, $msg) = @_;
1701
1702 if (report("WARNING", $type, $msg)) {
1703 our $clean = 0;
1704 our $cnt_warn++;
1705 return 1;
1706 }
1707 return 0;
1708}
1709sub CHK {
1710 my ($type, $msg) = @_;
1711
1712 if ($check && report("CHECK", $type, $msg)) {
1713 our $clean = 0;
1714 our $cnt_chk++;
1715 return 1;
1716 }
1717 return 0;
1718}
1719
1720sub check_absolute_file {
1721 my ($absolute, $herecurr) = @_;
1722 my $file = $absolute;
1723
1724 ##print "absolute<$absolute>\n";
1725
1726 # See if any suffix of this path is a path within the tree.
1727 while ($file =~ s@^[^/]*/@@) {
1728 if (-f "$root/$file") {
1729 ##print "file<$file>\n";
1730 last;
1731 }
1732 }
1733 if (! -f _) {
1734 return 0;
1735 }
1736
1737 # It is, so see if the prefix is acceptable.
1738 my $prefix = $absolute;
1739 substr($prefix, -length($file)) = '';
1740
1741 ##print "prefix<$prefix>\n";
1742 if ($prefix ne ".../") {
1743 WARN("USE_RELATIVE_PATH",
1744 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1745 }
1746}
1747
1748sub trim {
1749 my ($string) = @_;
1750
1751 $string =~ s/^\s+|\s+$//g;
1752
1753 return $string;
1754}
1755
1756sub ltrim {
1757 my ($string) = @_;
1758
1759 $string =~ s/^\s+//;
1760
1761 return $string;
1762}
1763
1764sub rtrim {
1765 my ($string) = @_;
1766
1767 $string =~ s/\s+$//;
1768
1769 return $string;
1770}
1771
1772sub string_find_replace {
1773 my ($string, $find, $replace) = @_;
1774
1775 $string =~ s/$find/$replace/g;
1776
1777 return $string;
1778}
1779
1780sub tabify {
1781 my ($leading) = @_;
1782
1783 my $source_indent = 8;
1784 my $max_spaces_before_tab = $source_indent - 1;
1785 my $spaces_to_tab = " " x $source_indent;
1786
1787 #convert leading spaces to tabs
1788 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1789 #Remove spaces before a tab
1790 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1791
1792 return "$leading";
1793}
1794
1795sub pos_last_openparen {
1796 my ($line) = @_;
1797
1798 my $pos = 0;
1799
1800 my $opens = $line =~ tr/\(/\(/;
1801 my $closes = $line =~ tr/\)/\)/;
1802
1803 my $last_openparen = 0;
1804
1805 if (($opens == 0) || ($closes >= $opens)) {
1806 return -1;
1807 }
1808
1809 my $len = length($line);
1810
1811 for ($pos = 0; $pos < $len; $pos++) {
1812 my $string = substr($line, $pos);
1813 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1814 $pos += length($1) - 1;
1815 } elsif (substr($line, $pos, 1) eq '(') {
1816 $last_openparen = $pos;
1817 } elsif (index($string, '(') == -1) {
1818 last;
1819 }
1820 }
1821
1822 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1823}
1824
1825sub process {
1826 my $filename = shift;
1827
1828 my $linenr=0;
1829 my $prevline="";
1830 my $prevrawline="";
1831 my $stashline="";
1832 my $stashrawline="";
1833
1834 my $length;
1835 my $indent;
1836 my $previndent=0;
1837 my $stashindent=0;
1838
1839 our $clean = 1;
1840 my $signoff = 0;
1841 my $is_patch = 0;
1842
1843 my $in_header_lines = $file ? 0 : 1;
1844 my $in_commit_log = 0; #Scanning lines before patch
1845 my $reported_maintainer_file = 0;
1846 my $non_utf8_charset = 0;
1847
1848 my $last_blank_line = 0;
1849
1850 our @report = ();
1851 our $cnt_lines = 0;
1852 our $cnt_error = 0;
1853 our $cnt_warn = 0;
1854 our $cnt_chk = 0;
1855
1856 # Trace the real file/line as we go.
1857 my $realfile = '';
1858 my $realline = 0;
1859 my $realcnt = 0;
1860 my $here = '';
1861 my $in_comment = 0;
1862 my $comment_edge = 0;
1863 my $first_line = 0;
1864 my $p1_prefix = '';
1865
1866 my $prev_values = 'E';
1867
1868 # suppression flags
1869 my %suppress_ifbraces;
1870 my %suppress_whiletrailers;
1871 my %suppress_export;
1872 my $suppress_statement = 0;
1873
1874 my %signatures = ();
1875
1876 # Pre-scan the patch sanitizing the lines.
1877 # Pre-scan the patch looking for any __setup documentation.
1878 #
1879 my @setup_docs = ();
1880 my $setup_docs = 0;
1881
1882 my $camelcase_file_seeded = 0;
1883
1884 sanitise_line_reset();
1885 my $line;
1886 foreach my $rawline (@rawlines) {
1887 $linenr++;
1888 $line = $rawline;
1889
1890 push(@fixed, $rawline) if ($fix);
1891
1892 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1893 $setup_docs = 0;
1894 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1895 $setup_docs = 1;
1896 }
1897 #next;
1898 }
1899 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1900 $realline=$1-1;
1901 if (defined $2) {
1902 $realcnt=$3+1;
1903 } else {
1904 $realcnt=1+1;
1905 }
1906 $in_comment = 0;
1907
1908 # Guestimate if this is a continuing comment. Run
1909 # the context looking for a comment "edge". If this
1910 # edge is a close comment then we must be in a comment
1911 # at context start.
1912 my $edge;
1913 my $cnt = $realcnt;
1914 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1915 next if (defined $rawlines[$ln - 1] &&
1916 $rawlines[$ln - 1] =~ /^-/);
1917 $cnt--;
1918 #print "RAW<$rawlines[$ln - 1]>\n";
1919 last if (!defined $rawlines[$ln - 1]);
1920 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1921 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1922 ($edge) = $1;
1923 last;
1924 }
1925 }
1926 if (defined $edge && $edge eq '*/') {
1927 $in_comment = 1;
1928 }
1929
1930 # Guestimate if this is a continuing comment. If this
1931 # is the start of a diff block and this line starts
1932 # ' *' then it is very likely a comment.
1933 if (!defined $edge &&
1934 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1935 {
1936 $in_comment = 1;
1937 }
1938
1939 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1940 sanitise_line_reset($in_comment);
1941
1942 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1943 # Standardise the strings and chars within the input to
1944 # simplify matching -- only bother with positive lines.
1945 $line = sanitise_line($rawline);
1946 }
1947 push(@lines, $line);
1948
1949 if ($realcnt > 1) {
1950 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1951 } else {
1952 $realcnt = 0;
1953 }
1954
1955 #print "==>$rawline\n";
1956 #print "-->$line\n";
1957
1958 if ($setup_docs && $line =~ /^\+/) {
1959 push(@setup_docs, $line);
1960 }
1961 }
1962
1963 $prefix = '';
1964
1965 $realcnt = 0;
1966 $linenr = 0;
1967 $fixlinenr = -1;
1968 foreach my $line (@lines) {
1969 $linenr++;
1970 $fixlinenr++;
1971 my $sline = $line; #copy of $line
1972 $sline =~ s/$;/ /g; #with comments as spaces
1973
1974 my $rawline = $rawlines[$linenr - 1];
1975
1976#extract the line range in the file after the patch is applied
1977 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1978 $is_patch = 1;
1979 $first_line = $linenr + 1;
1980 $realline=$1-1;
1981 if (defined $2) {
1982 $realcnt=$3+1;
1983 } else {
1984 $realcnt=1+1;
1985 }
1986 annotate_reset();
1987 $prev_values = 'E';
1988
1989 %suppress_ifbraces = ();
1990 %suppress_whiletrailers = ();
1991 %suppress_export = ();
1992 $suppress_statement = 0;
1993 next;
1994
1995# track the line number as we move through the hunk, note that
1996# new versions of GNU diff omit the leading space on completely
1997# blank context lines so we need to count that too.
1998 } elsif ($line =~ /^( |\+|$)/) {
1999 $realline++;
2000 $realcnt-- if ($realcnt != 0);
2001
2002 # Measure the line length and indent.
2003 ($length, $indent) = line_stats($rawline);
2004
2005 # Track the previous line.
2006 ($prevline, $stashline) = ($stashline, $line);
2007 ($previndent, $stashindent) = ($stashindent, $indent);
2008 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2009
2010 #warn "line<$line>\n";
2011
2012 } elsif ($realcnt == 1) {
2013 $realcnt--;
2014 }
2015
2016 my $hunk_line = ($realcnt != 0);
2017
2018#make up the handle for any error we report on this line
2019 $prefix = "$filename:$realline: " if ($emacs && $file);
2020 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2021
2022 $here = "#$linenr: " if (!$file);
2023 $here = "#$realline: " if ($file);
2024
2025 my $found_file = 0;
2026 # extract the filename as it passes
2027 if ($line =~ /^diff --git.*?(\S+)$/) {
2028 $realfile = $1;
2029 $realfile =~ s@^([^/]*)/@@ if (!$file);
2030 $in_commit_log = 0;
2031 $found_file = 1;
2032 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2033 $realfile = $1;
2034 $realfile =~ s@^([^/]*)/@@ if (!$file);
2035 $in_commit_log = 0;
2036
2037 $p1_prefix = $1;
2038 if (!$file && $tree && $p1_prefix ne '' &&
2039 -e "$root/$p1_prefix") {
2040 WARN("PATCH_PREFIX",
2041 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2042 }
2043
2044 if ($realfile =~ m@^include/asm/@) {
2045 ERROR("MODIFIED_INCLUDE_ASM",
2046 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2047 }
2048 $found_file = 1;
2049 }
2050
2051 if ($found_file) {
2052 if ($realfile =~ m@^(drivers/net/|net/)@) {
2053 $check = 1;
2054 } else {
2055 $check = $check_orig;
2056 }
2057 next;
2058 }
2059
2060 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2061
2062 my $hereline = "$here\n$rawline\n";
2063 my $herecurr = "$here\n$rawline\n";
2064 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2065
2066 $cnt_lines++ if ($realcnt != 0);
2067
2068# Check for incorrect file permissions
2069 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2070 my $permhere = $here . "FILE: $realfile\n";
2071 if ($realfile !~ m@scripts/@ &&
2072 $realfile !~ /\.(py|pl|awk|sh)$/) {
2073 ERROR("EXECUTE_PERMISSIONS",
2074 "do not set execute permissions for source files\n" . $permhere);
2075 }
2076 }
2077
2078# Check the patch for a signoff:
2079 if ($line =~ /^\s*signed-off-by:/i) {
2080 $signoff++;
2081 $in_commit_log = 0;
2082 }
2083
2084# Check signature styles
2085 if (!$in_header_lines &&
2086 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2087 my $space_before = $1;
2088 my $sign_off = $2;
2089 my $space_after = $3;
2090 my $email = $4;
2091 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2092
2093 if ($sign_off !~ /$signature_tags/) {
2094 WARN("BAD_SIGN_OFF",
2095 "Non-standard signature: $sign_off\n" . $herecurr);
2096 }
2097 if (defined $space_before && $space_before ne "") {
2098 if (WARN("BAD_SIGN_OFF",
2099 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2100 $fix) {
2101 $fixed[$fixlinenr] =
2102 "$ucfirst_sign_off $email";
2103 }
2104 }
2105 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2106 if (WARN("BAD_SIGN_OFF",
2107 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2108 $fix) {
2109 $fixed[$fixlinenr] =
2110 "$ucfirst_sign_off $email";
2111 }
2112
2113 }
2114 if (!defined $space_after || $space_after ne " ") {
2115 if (WARN("BAD_SIGN_OFF",
2116 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2117 $fix) {
2118 $fixed[$fixlinenr] =
2119 "$ucfirst_sign_off $email";
2120 }
2121 }
2122
2123 my ($email_name, $email_address, $comment) = parse_email($email);
2124 my $suggested_email = format_email(($email_name, $email_address));
2125 if ($suggested_email eq "") {
2126 ERROR("BAD_SIGN_OFF",
2127 "Unrecognized email address: '$email'\n" . $herecurr);
2128 } else {
2129 my $dequoted = $suggested_email;
2130 $dequoted =~ s/^"//;
2131 $dequoted =~ s/" </ </;
2132 # Don't force email to have quotes
2133 # Allow just an angle bracketed address
2134 if ("$dequoted$comment" ne $email &&
2135 "<$email_address>$comment" ne $email &&
2136 "$suggested_email$comment" ne $email) {
2137 WARN("BAD_SIGN_OFF",
2138 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2139 }
2140 }
2141
2142# Check for duplicate signatures
2143 my $sig_nospace = $line;
2144 $sig_nospace =~ s/\s//g;
2145 $sig_nospace = lc($sig_nospace);
2146 if (defined $signatures{$sig_nospace}) {
2147 WARN("BAD_SIGN_OFF",
2148 "Duplicate signature\n" . $herecurr);
2149 } else {
2150 $signatures{$sig_nospace} = 1;
2151 }
2152 }
2153
2154# Check for old stable address
2155 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2156 ERROR("STABLE_ADDRESS",
2157 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2158 }
2159
2160# Check for unwanted Gerrit info
2161 if ($in_commit_log && !$ignore_changeid && $line =~ /^\s*change-id:/i) {
2162 ERROR("GERRIT_CHANGE_ID",
2163 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2164 }
2165
2166# Check for improperly formed commit descriptions
2167 if ($in_commit_log &&
2168 $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
2169 !($line =~ /\b[Cc]ommit [0-9a-f]{12,40} \("/ ||
2170 ($line =~ /\b[Cc]ommit [0-9a-f]{12,40}\s*$/ &&
2171 defined $rawlines[$linenr] &&
2172 $rawlines[$linenr] =~ /^\s*\("/))) {
2173 $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2174 my $init_char = $1;
2175 my $orig_commit = lc($2);
2176 my $id = '01234567890ab';
2177 my $desc = 'commit description';
2178 ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2179 ERROR("GIT_COMMIT_ID",
2180 "Please use 12 or more chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2181 }
2182
2183# Check for added, moved or deleted files
2184 if (!$reported_maintainer_file && !$in_commit_log &&
2185 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2186 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2187 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2188 (defined($1) || defined($2))))) {
2189 $reported_maintainer_file = 1;
2190 WARN("FILE_PATH_CHANGES",
2191 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2192 }
2193
2194# Check for wrappage within a valid hunk of the file
2195 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2196 ERROR("CORRUPTED_PATCH",
2197 "patch seems to be corrupt (line wrapped?)\n" .
2198 $herecurr) if (!$emitted_corrupt++);
2199 }
2200
2201# Check for absolute kernel paths.
2202 if ($tree) {
2203 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2204 my $file = $1;
2205
2206 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2207 check_absolute_file($1, $herecurr)) {
2208 #
2209 } else {
2210 check_absolute_file($file, $herecurr);
2211 }
2212 }
2213 }
2214
2215# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2216 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2217 $rawline !~ m/^$UTF8*$/) {
2218 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2219
2220 my $blank = copy_spacing($rawline);
2221 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2222 my $hereptr = "$hereline$ptr\n";
2223
2224 CHK("INVALID_UTF8",
2225 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2226 }
2227
2228# Check if it's the start of a commit log
2229# (not a header line and we haven't seen the patch filename)
2230 if ($in_header_lines && $realfile =~ /^$/ &&
2231 !($rawline =~ /^\s+\S/ ||
2232 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
2233 $in_header_lines = 0;
2234 $in_commit_log = 1;
2235 }
2236
2237# Check if there is UTF-8 in a commit log when a mail header has explicitly
2238# declined it, i.e defined some charset where it is missing.
2239 if ($in_header_lines &&
2240 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2241 $1 !~ /utf-8/i) {
2242 $non_utf8_charset = 1;
2243 }
2244
2245 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2246 $rawline =~ /$NON_ASCII_UTF8/) {
2247 WARN("UTF8_BEFORE_PATCH",
2248 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2249 }
2250
2251# Check for various typo / spelling mistakes
2252 if ($in_commit_log || $line =~ /^\+/) {
2253 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
2254 my $typo = $1;
2255 my $typo_fix = $spelling_fix{lc($typo)};
2256 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2257 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2258 my $msg_type = \&WARN;
2259 $msg_type = \&CHK if ($file);
2260 if (&{$msg_type}("TYPO_SPELLING",
2261 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2262 $fix) {
2263 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2264 }
2265 }
2266 }
2267
2268# ignore non-hunk lines and lines being removed
2269 next if (!$hunk_line || $line =~ /^-/);
2270
2271#trailing whitespace
2272 if ($line =~ /^\+.*\015/) {
2273 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2274 if (ERROR("DOS_LINE_ENDINGS",
2275 "DOS line endings\n" . $herevet) &&
2276 $fix) {
2277 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2278 }
2279 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2280 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2281 if (ERROR("TRAILING_WHITESPACE",
2282 "trailing whitespace\n" . $herevet) &&
2283 $fix) {
2284 $fixed[$fixlinenr] =~ s/\s+$//;
2285 }
2286
2287 $rpt_cleaners = 1;
2288 }
2289
2290# Check for FSF mailing addresses.
2291 if ($rawline =~ /\bwrite to the Free/i ||
2292 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2293 $rawline =~ /\b51\s+Franklin\s+St/i) {
2294 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2295 my $msg_type = \&ERROR;
2296 $msg_type = \&CHK if ($file);
2297 &{$msg_type}("FSF_MAILING_ADDRESS",
2298 "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)
2299 }
2300
2301# check for Kconfig help text having a real description
2302# Only applies when adding the entry originally, after that we do not have
2303# sufficient context to determine whether it is indeed long enough.
2304 if ($realfile =~ /Kconfig/ &&
2305 $line =~ /^\+\s*config\s+/) {
2306 my $length = 0;
2307 my $cnt = $realcnt;
2308 my $ln = $linenr + 1;
2309 my $f;
2310 my $is_start = 0;
2311 my $is_end = 0;
2312 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
2313 $f = $lines[$ln - 1];
2314 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2315 $is_end = $lines[$ln - 1] =~ /^\+/;
2316
2317 next if ($f =~ /^-/);
2318 last if (!$file && $f =~ /^\@\@/);
2319
2320 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2321 $is_start = 1;
2322 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2323 $length = -1;
2324 }
2325
2326 $f =~ s/^.//;
2327 $f =~ s/#.*//;
2328 $f =~ s/^\s+//;
2329 next if ($f =~ /^$/);
2330 if ($f =~ /^\s*config\s/) {
2331 $is_end = 1;
2332 last;
2333 }
2334 $length++;
2335 }
2336 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2337 WARN("CONFIG_DESCRIPTION",
2338 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2339 }
2340 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2341 }
2342
2343# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2344 if ($realfile =~ /Kconfig/ &&
2345 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2346 WARN("CONFIG_EXPERIMENTAL",
2347 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2348 }
2349
2350 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2351 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2352 my $flag = $1;
2353 my $replacement = {
2354 'EXTRA_AFLAGS' => 'asflags-y',
2355 'EXTRA_CFLAGS' => 'ccflags-y',
2356 'EXTRA_CPPFLAGS' => 'cppflags-y',
2357 'EXTRA_LDFLAGS' => 'ldflags-y',
2358 };
2359
2360 WARN("DEPRECATED_VARIABLE",
2361 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2362 }
2363
2364# check for DT compatible documentation
2365 if (defined $root &&
2366 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2367 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2368
2369 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2370
2371 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2372 my $vp_file = $dt_path . "vendor-prefixes.txt";
2373
2374 foreach my $compat (@compats) {
2375 my $compat2 = $compat;
2376 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2377 my $compat3 = $compat;
2378 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2379 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2380 if ( $? >> 8 ) {
2381 WARN("UNDOCUMENTED_DT_STRING",
2382 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2383 }
2384
2385 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2386 my $vendor = $1;
2387 `grep -Eq "^$vendor\\b" $vp_file`;
2388 if ( $? >> 8 ) {
2389 WARN("UNDOCUMENTED_DT_STRING",
2390 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2391 }
2392 }
2393 }
2394
2395# check we are in a valid source file if not then ignore this hunk
2396 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
2397
2398#line length limit
2399 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2400 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
2401 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
2402 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
2403 $length > $max_line_length)
2404 {
2405 WARN("LONG_LINE",
2406 "line over $max_line_length characters\n" . $herecurr);
2407 }
2408
2409# Check for user-visible strings broken across lines, which breaks the ability
2410# to grep for the string. Make exceptions when the previous string ends in a
2411# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
2412# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2413 if ($line =~ /^\+\s*"/ &&
2414 $prevline =~ /"\s*$/ &&
2415 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2416 WARN("SPLIT_STRING",
2417 "quoted string split across lines\n" . $hereprev);
2418 }
2419
2420# check for missing a space in a string concatination
2421 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
2422 WARN('MISSING_SPACE',
2423 "break quoted strings at a space character\n" . $hereprev);
2424 }
2425
2426# check for spaces before a quoted newline
2427 if ($rawline =~ /^.*\".*\s\\n/) {
2428 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2429 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2430 $fix) {
2431 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2432 }
2433
2434 }
2435
2436# check for adding lines without a newline.
2437 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2438 WARN("MISSING_EOF_NEWLINE",
2439 "adding a line without newline at end of file\n" . $herecurr);
2440 }
2441
2442# Blackfin: use hi/lo macros
2443 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2444 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2445 my $herevet = "$here\n" . cat_vet($line) . "\n";
2446 ERROR("LO_MACRO",
2447 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2448 }
2449 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2450 my $herevet = "$here\n" . cat_vet($line) . "\n";
2451 ERROR("HI_MACRO",
2452 "use the HI() macro, not (... >> 16)\n" . $herevet);
2453 }
2454 }
2455
2456# check we are in a valid source file C or perl if not then ignore this hunk
2457 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
2458
2459# at the beginning of a line any tabs must come first and anything
2460# more than 8 must use tabs.
2461 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2462 $rawline =~ /^\+\s* \s*/) {
2463 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2464 $rpt_cleaners = 1;
2465 if (ERROR("CODE_INDENT",
2466 "code indent should use tabs where possible\n" . $herevet) &&
2467 $fix) {
2468 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2469 }
2470 }
2471
2472# check for space before tabs.
2473 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2474 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2475 if (WARN("SPACE_BEFORE_TAB",
2476 "please, no space before tabs\n" . $herevet) &&
2477 $fix) {
2478 while ($fixed[$fixlinenr] =~
2479 s/(^\+.*) {8,8}\t/$1\t\t/) {}
2480 while ($fixed[$fixlinenr] =~
2481 s/(^\+.*) +\t/$1\t/) {}
2482 }
2483 }
2484
2485# check for && or || at the start of a line
2486 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2487 CHK("LOGICAL_CONTINUATIONS",
2488 "Logical continuations should be on the previous line\n" . $hereprev);
2489 }
2490
2491# check multi-line statement indentation matches previous line
2492 if ($^V && $^V ge 5.10.0 &&
2493 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2494 $prevline =~ /^\+(\t*)(.*)$/;
2495 my $oldindent = $1;
2496 my $rest = $2;
2497
2498 my $pos = pos_last_openparen($rest);
2499 if ($pos >= 0) {
2500 $line =~ /^(\+| )([ \t]*)/;
2501 my $newindent = $2;
2502
2503 my $goodtabindent = $oldindent .
2504 "\t" x ($pos / 8) .
2505 " " x ($pos % 8);
2506 my $goodspaceindent = $oldindent . " " x $pos;
2507
2508 if ($newindent ne $goodtabindent &&
2509 $newindent ne $goodspaceindent) {
2510
2511 if (CHK("PARENTHESIS_ALIGNMENT",
2512 "Alignment should match open parenthesis\n" . $hereprev) &&
2513 $fix && $line =~ /^\+/) {
2514 $fixed[$fixlinenr] =~
2515 s/^\+[ \t]*/\+$goodtabindent/;
2516 }
2517 }
2518 }
2519 }
2520
2521 if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|{)/) {
2522 if (CHK("SPACING",
2523 "No space is necessary after a cast\n" . $herecurr) &&
2524 $fix) {
2525 $fixed[$fixlinenr] =~
2526 s/(\(\s*$Type\s*\))[ \t]+/$1/;
2527 }
2528 }
2529
2530 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2531 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2532 $rawline =~ /^\+[ \t]*\*/ &&
2533 $realline > 2) {
2534 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2535 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2536 }
2537
2538 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2539 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2540 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2541 $rawline =~ /^\+/ && #line is new
2542 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2543 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2544 "networking block comments start with * on subsequent lines\n" . $hereprev);
2545 }
2546
2547 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2548 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2549 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2550 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2551 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
2552 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2553 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2554 }
2555
2556# check for missing blank lines after struct/union declarations
2557# with exceptions for various attributes and macros
2558 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2559 $line =~ /^\+/ &&
2560 !($line =~ /^\+\s*$/ ||
2561 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2562 $line =~ /^\+\s*MODULE_/i ||
2563 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2564 $line =~ /^\+[a-z_]*init/ ||
2565 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2566 $line =~ /^\+\s*DECLARE/ ||
2567 $line =~ /^\+\s*__setup/)) {
2568 if (CHK("LINE_SPACING",
2569 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2570 $fix) {
2571 fix_insert_line($fixlinenr, "\+");
2572 }
2573 }
2574
2575# check for multiple consecutive blank lines
2576 if ($prevline =~ /^[\+ ]\s*$/ &&
2577 $line =~ /^\+\s*$/ &&
2578 $last_blank_line != ($linenr - 1)) {
2579 if (CHK("LINE_SPACING",
2580 "Please don't use multiple blank lines\n" . $hereprev) &&
2581 $fix) {
2582 fix_delete_line($fixlinenr, $rawline);
2583 }
2584
2585 $last_blank_line = $linenr;
2586 }
2587
2588# check for missing blank lines after declarations
2589 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2590 # actual declarations
2591 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2592 # function pointer declarations
2593 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2594 # foo bar; where foo is some local typedef or #define
2595 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2596 # known declaration macros
2597 $prevline =~ /^\+\s+$declaration_macros/) &&
2598 # for "else if" which can look like "$Ident $Ident"
2599 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2600 # other possible extensions of declaration lines
2601 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2602 # not starting a section or a macro "\" extended line
2603 $prevline =~ /(?:\{\s*|\\)$/) &&
2604 # looks like a declaration
2605 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2606 # function pointer declarations
2607 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2608 # foo bar; where foo is some local typedef or #define
2609 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2610 # known declaration macros
2611 $sline =~ /^\+\s+$declaration_macros/ ||
2612 # start of struct or union or enum
2613 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
2614 # start or end of block or continuation of declaration
2615 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2616 # bitfield continuation
2617 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2618 # other possible extensions of declaration lines
2619 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2620 # indentation of previous and current line are the same
2621 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2622 if (WARN("LINE_SPACING",
2623 "Missing a blank line after declarations\n" . $hereprev) &&
2624 $fix) {
2625 fix_insert_line($fixlinenr, "\+");
2626 }
2627 }
2628
2629# check for spaces at the beginning of a line.
2630# Exceptions:
2631# 1) within comments
2632# 2) indented preprocessor commands
2633# 3) hanging labels
2634 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
2635 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2636 if (WARN("LEADING_SPACE",
2637 "please, no spaces at the start of a line\n" . $herevet) &&
2638 $fix) {
2639 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2640 }
2641 }
2642
2643# check we are in a valid C source file if not then ignore this hunk
2644 next if ($realfile !~ /\.(h|c)$/);
2645
2646# check indentation of any line with a bare else
2647# (but not if it is a multiple line "if (foo) return bar; else return baz;")
2648# if the previous line is a break or return and is indented 1 tab more...
2649 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2650 my $tabs = length($1) + 1;
2651 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2652 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2653 defined $lines[$linenr] &&
2654 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2655 WARN("UNNECESSARY_ELSE",
2656 "else is not generally useful after a break or return\n" . $hereprev);
2657 }
2658 }
2659
2660# check indentation of a line with a break;
2661# if the previous line is a goto or return and is indented the same # of tabs
2662 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2663 my $tabs = $1;
2664 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2665 WARN("UNNECESSARY_BREAK",
2666 "break is not useful after a goto or return\n" . $hereprev);
2667 }
2668 }
2669
2670# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2671 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2672 WARN("CONFIG_EXPERIMENTAL",
2673 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2674 }
2675
2676# check for RCS/CVS revision markers
2677 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2678 WARN("CVS_KEYWORD",
2679 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2680 }
2681
2682# Blackfin: don't use __builtin_bfin_[cs]sync
2683 if ($line =~ /__builtin_bfin_csync/) {
2684 my $herevet = "$here\n" . cat_vet($line) . "\n";
2685 ERROR("CSYNC",
2686 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
2687 }
2688 if ($line =~ /__builtin_bfin_ssync/) {
2689 my $herevet = "$here\n" . cat_vet($line) . "\n";
2690 ERROR("SSYNC",
2691 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
2692 }
2693
2694# check for old HOTPLUG __dev<foo> section markings
2695 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2696 WARN("HOTPLUG_SECTION",
2697 "Using $1 is unnecessary\n" . $herecurr);
2698 }
2699
2700# Check for potential 'bare' types
2701 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2702 $realline_next);
2703#print "LINE<$line>\n";
2704 if ($linenr >= $suppress_statement &&
2705 $realcnt && $sline =~ /.\s*\S/) {
2706 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2707 ctx_statement_block($linenr, $realcnt, 0);
2708 $stat =~ s/\n./\n /g;
2709 $cond =~ s/\n./\n /g;
2710
2711#print "linenr<$linenr> <$stat>\n";
2712 # If this statement has no statement boundaries within
2713 # it there is no point in retrying a statement scan
2714 # until we hit end of it.
2715 my $frag = $stat; $frag =~ s/;+\s*$//;
2716 if ($frag !~ /(?:{|;)/) {
2717#print "skip<$line_nr_next>\n";
2718 $suppress_statement = $line_nr_next;
2719 }
2720
2721 # Find the real next line.
2722 $realline_next = $line_nr_next;
2723 if (defined $realline_next &&
2724 (!defined $lines[$realline_next - 1] ||
2725 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2726 $realline_next++;
2727 }
2728
2729 my $s = $stat;
2730 $s =~ s/{.*$//s;
2731
2732 # Ignore goto labels.
2733 if ($s =~ /$Ident:\*$/s) {
2734
2735 # Ignore functions being called
2736 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2737
2738 } elsif ($s =~ /^.\s*else\b/s) {
2739
2740 # declarations always start with types
2741 } 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) {
2742 my $type = $1;
2743 $type =~ s/\s+/ /g;
2744 possible($type, "A:" . $s);
2745
2746 # definitions in global scope can only start with types
2747 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2748 possible($1, "B:" . $s);
2749 }
2750
2751 # any (foo ... *) is a pointer cast, and foo is a type
2752 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2753 possible($1, "C:" . $s);
2754 }
2755
2756 # Check for any sort of function declaration.
2757 # int foo(something bar, other baz);
2758 # void (*store_gdt)(x86_descr_ptr *);
2759 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
2760 my ($name_len) = length($1);
2761
2762 my $ctx = $s;
2763 substr($ctx, 0, $name_len + 1, '');
2764 $ctx =~ s/\)[^\)]*$//;
2765
2766 for my $arg (split(/\s*,\s*/, $ctx)) {
2767 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
2768
2769 possible($1, "D:" . $s);
2770 }
2771 }
2772 }
2773
2774 }
2775
2776#
2777# Checks which may be anchored in the context.
2778#
2779
2780# Check for switch () and associated case and default
2781# statements should be at the same indent.
2782 if ($line=~/\bswitch\s*\(.*\)/) {
2783 my $err = '';
2784 my $sep = '';
2785 my @ctx = ctx_block_outer($linenr, $realcnt);
2786 shift(@ctx);
2787 for my $ctx (@ctx) {
2788 my ($clen, $cindent) = line_stats($ctx);
2789 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2790 $indent != $cindent) {
2791 $err .= "$sep$ctx\n";
2792 $sep = '';
2793 } else {
2794 $sep = "[...]\n";
2795 }
2796 }
2797 if ($err ne '') {
2798 ERROR("SWITCH_CASE_INDENT_LEVEL",
2799 "switch and case should be at the same indent\n$hereline$err");
2800 }
2801 }
2802
2803# if/while/etc brace do not go on next line, unless defining a do while loop,
2804# or if that brace on the next line is for something else
2805 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2806 my $pre_ctx = "$1$2";
2807
2808 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
2809
2810 if ($line =~ /^\+\t{6,}/) {
2811 WARN("DEEP_INDENTATION",
2812 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2813 }
2814
2815 my $ctx_cnt = $realcnt - $#ctx - 1;
2816 my $ctx = join("\n", @ctx);
2817
2818 my $ctx_ln = $linenr;
2819 my $ctx_skip = $realcnt;
2820
2821 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2822 defined $lines[$ctx_ln - 1] &&
2823 $lines[$ctx_ln - 1] =~ /^-/)) {
2824 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2825 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2826 $ctx_ln++;
2827 }
2828
2829 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2830 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2831
2832 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2833 ERROR("OPEN_BRACE",
2834 "that open brace { should be on the previous line\n" .
2835 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2836 }
2837 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2838 $ctx =~ /\)\s*\;\s*$/ &&
2839 defined $lines[$ctx_ln - 1])
2840 {
2841 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2842 if ($nindent > $indent) {
2843 WARN("TRAILING_SEMICOLON",
2844 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2845 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2846 }
2847 }
2848 }
2849
2850# Check relative indent for conditionals and blocks.
2851 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2852 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2853 ctx_statement_block($linenr, $realcnt, 0)
2854 if (!defined $stat);
2855 my ($s, $c) = ($stat, $cond);
2856
2857 substr($s, 0, length($c), '');
2858
2859 # Make sure we remove the line prefixes as we have
2860 # none on the first line, and are going to readd them
2861 # where necessary.
2862 $s =~ s/\n./\n/gs;
2863
2864 # Find out how long the conditional actually is.
2865 my @newlines = ($c =~ /\n/gs);
2866 my $cond_lines = 1 + $#newlines;
2867
2868 # We want to check the first line inside the block
2869 # starting at the end of the conditional, so remove:
2870 # 1) any blank line termination
2871 # 2) any opening brace { on end of the line
2872 # 3) any do (...) {
2873 my $continuation = 0;
2874 my $check = 0;
2875 $s =~ s/^.*\bdo\b//;
2876 $s =~ s/^\s*{//;
2877 if ($s =~ s/^\s*\\//) {
2878 $continuation = 1;
2879 }
2880 if ($s =~ s/^\s*?\n//) {
2881 $check = 1;
2882 $cond_lines++;
2883 }
2884
2885 # Also ignore a loop construct at the end of a
2886 # preprocessor statement.
2887 if (($prevline =~ /^.\s*#\s*define\s/ ||
2888 $prevline =~ /\\\s*$/) && $continuation == 0) {
2889 $check = 0;
2890 }
2891
2892 my $cond_ptr = -1;
2893 $continuation = 0;
2894 while ($cond_ptr != $cond_lines) {
2895 $cond_ptr = $cond_lines;
2896
2897 # If we see an #else/#elif then the code
2898 # is not linear.
2899 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2900 $check = 0;
2901 }
2902
2903 # Ignore:
2904 # 1) blank lines, they should be at 0,
2905 # 2) preprocessor lines, and
2906 # 3) labels.
2907 if ($continuation ||
2908 $s =~ /^\s*?\n/ ||
2909 $s =~ /^\s*#\s*?/ ||
2910 $s =~ /^\s*$Ident\s*:/) {
2911 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
2912 if ($s =~ s/^.*?\n//) {
2913 $cond_lines++;
2914 }
2915 }
2916 }
2917
2918 my (undef, $sindent) = line_stats("+" . $s);
2919 my $stat_real = raw_line($linenr, $cond_lines);
2920
2921 # Check if either of these lines are modified, else
2922 # this is not this patch's fault.
2923 if (!defined($stat_real) ||
2924 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2925 $check = 0;
2926 }
2927 if (defined($stat_real) && $cond_lines > 1) {
2928 $stat_real = "[...]\n$stat_real";
2929 }
2930
2931 #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";
2932
2933 if ($check && (($sindent % 8) != 0 ||
2934 ($sindent <= $indent && $s ne ''))) {
2935 WARN("SUSPECT_CODE_INDENT",
2936 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2937 }
2938 }
2939
2940 # Track the 'values' across context and added lines.
2941 my $opline = $line; $opline =~ s/^./ /;
2942 my ($curr_values, $curr_vars) =
2943 annotate_values($opline . "\n", $prev_values);
2944 $curr_values = $prev_values . $curr_values;
2945 if ($dbg_values) {
2946 my $outline = $opline; $outline =~ s/\t/ /g;
2947 print "$linenr > .$outline\n";
2948 print "$linenr > $curr_values\n";
2949 print "$linenr > $curr_vars\n";
2950 }
2951 $prev_values = substr($curr_values, -1);
2952
2953#ignore lines not being added
2954 next if ($line =~ /^[^\+]/);
2955
2956# TEST: allow direct testing of the type matcher.
2957 if ($dbg_type) {
2958 if ($line =~ /^.\s*$Declare\s*$/) {
2959 ERROR("TEST_TYPE",
2960 "TEST: is type\n" . $herecurr);
2961 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2962 ERROR("TEST_NOT_TYPE",
2963 "TEST: is not type ($1 is)\n". $herecurr);
2964 }
2965 next;
2966 }
2967# TEST: allow direct testing of the attribute matcher.
2968 if ($dbg_attr) {
2969 if ($line =~ /^.\s*$Modifier\s*$/) {
2970 ERROR("TEST_ATTR",
2971 "TEST: is attr\n" . $herecurr);
2972 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2973 ERROR("TEST_NOT_ATTR",
2974 "TEST: is not attr ($1 is)\n". $herecurr);
2975 }
2976 next;
2977 }
2978
2979# check for initialisation to aggregates open brace on the next line
2980 if ($line =~ /^.\s*{/ &&
2981 $prevline =~ /(?:^|[^=])=\s*$/) {
2982 if (ERROR("OPEN_BRACE",
2983 "that open brace { should be on the previous line\n" . $hereprev) &&
2984 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2985 fix_delete_line($fixlinenr - 1, $prevrawline);
2986 fix_delete_line($fixlinenr, $rawline);
2987 my $fixedline = $prevrawline;
2988 $fixedline =~ s/\s*=\s*$/ = {/;
2989 fix_insert_line($fixlinenr, $fixedline);
2990 $fixedline = $line;
2991 $fixedline =~ s/^(.\s*){\s*/$1/;
2992 fix_insert_line($fixlinenr, $fixedline);
2993 }
2994 }
2995
2996#
2997# Checks which are anchored on the added line.
2998#
2999
3000# check for malformed paths in #include statements (uses RAW line)
3001 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3002 my $path = $1;
3003 if ($path =~ m{//}) {
3004 ERROR("MALFORMED_INCLUDE",
3005 "malformed #include filename\n" . $herecurr);
3006 }
3007 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3008 ERROR("UAPI_INCLUDE",
3009 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3010 }
3011 }
3012
3013# no C99 // comments
3014 if ($line =~ m{//}) {
3015 if (ERROR("C99_COMMENTS",
3016 "do not use C99 // comments\n" . $herecurr) &&
3017 $fix) {
3018 my $line = $fixed[$fixlinenr];
3019 if ($line =~ /\/\/(.*)$/) {
3020 my $comment = trim($1);
3021 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
3022 }
3023 }
3024 }
3025 # Remove C99 comments.
3026 $line =~ s@//.*@@;
3027 $opline =~ s@//.*@@;
3028
3029# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3030# the whole statement.
3031#print "APW <$lines[$realline_next - 1]>\n";
3032 if (defined $realline_next &&
3033 exists $lines[$realline_next - 1] &&
3034 !defined $suppress_export{$realline_next} &&
3035 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3036 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3037 # Handle definitions which produce identifiers with
3038 # a prefix:
3039 # XXX(foo);
3040 # EXPORT_SYMBOL(something_foo);
3041 my $name = $1;
3042 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
3043 $name =~ /^${Ident}_$2/) {
3044#print "FOO C name<$name>\n";
3045 $suppress_export{$realline_next} = 1;
3046
3047 } elsif ($stat !~ /(?:
3048 \n.}\s*$|
3049 ^.DEFINE_$Ident\(\Q$name\E\)|
3050 ^.DECLARE_$Ident\(\Q$name\E\)|
3051 ^.LIST_HEAD\(\Q$name\E\)|
3052 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3053 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
3054 )/x) {
3055#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3056 $suppress_export{$realline_next} = 2;
3057 } else {
3058 $suppress_export{$realline_next} = 1;
3059 }
3060 }
3061 if (!defined $suppress_export{$linenr} &&
3062 $prevline =~ /^.\s*$/ &&
3063 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3064 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3065#print "FOO B <$lines[$linenr - 1]>\n";
3066 $suppress_export{$linenr} = 2;
3067 }
3068 if (defined $suppress_export{$linenr} &&
3069 $suppress_export{$linenr} == 2) {
3070 WARN("EXPORT_SYMBOL",
3071 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
3072 }
3073
3074# check for global initialisers.
3075 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3076 if (ERROR("GLOBAL_INITIALISERS",
3077 "do not initialise globals to 0 or NULL\n" .
3078 $herecurr) &&
3079 $fix) {
3080 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3081 }
3082 }
3083# check for static initialisers.
3084 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3085 if (ERROR("INITIALISED_STATIC",
3086 "do not initialise statics to 0 or NULL\n" .
3087 $herecurr) &&
3088 $fix) {
3089 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3090 }
3091 }
3092
3093# check for misordered declarations of char/short/int/long with signed/unsigned
3094 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3095 my $tmp = trim($1);
3096 WARN("MISORDERED_TYPE",
3097 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3098 }
3099
3100# check for static const char * arrays.
3101 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3102 WARN("STATIC_CONST_CHAR_ARRAY",
3103 "static const char * array should probably be static const char * const\n" .
3104 $herecurr);
3105 }
3106
3107# check for static char foo[] = "bar" declarations.
3108 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3109 WARN("STATIC_CONST_CHAR_ARRAY",
3110 "static char array declaration should probably be static const char\n" .
3111 $herecurr);
3112 }
3113
3114# check for non-global char *foo[] = {"bar", ...} declarations.
3115 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3116 WARN("STATIC_CONST_CHAR_ARRAY",
3117 "char * array declaration might be better as static const\n" .
3118 $herecurr);
3119 }
3120
3121# check for function declarations without arguments like "int foo()"
3122 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3123 if (ERROR("FUNCTION_WITHOUT_ARGS",
3124 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3125 $fix) {
3126 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3127 }
3128 }
3129
3130# check for uses of DEFINE_PCI_DEVICE_TABLE
3131 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3132 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3133 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3134 $fix) {
3135 $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
3136 }
3137 }
3138
3139# check for new typedefs, only function parameters and sparse annotations
3140# make sense.
3141 if ($line =~ /\btypedef\s/ &&
3142 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3143 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3144 $line !~ /\b$typeTypedefs\b/ &&
3145 $line !~ /\b__bitwise(?:__|)\b/) {
3146 WARN("NEW_TYPEDEFS",
3147 "do not add new typedefs\n" . $herecurr);
3148 }
3149
3150# * goes on variable not on type
3151 # (char*[ const])
3152 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3153 #print "AA<$1>\n";
3154 my ($ident, $from, $to) = ($1, $2, $2);
3155
3156 # Should start with a space.
3157 $to =~ s/^(\S)/ $1/;
3158 # Should not end with a space.
3159 $to =~ s/\s+$//;
3160 # '*'s should not have spaces between.
3161 while ($to =~ s/\*\s+\*/\*\*/) {
3162 }
3163
3164## print "1: from<$from> to<$to> ident<$ident>\n";
3165 if ($from ne $to) {
3166 if (ERROR("POINTER_LOCATION",
3167 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3168 $fix) {
3169 my $sub_from = $ident;
3170 my $sub_to = $ident;
3171 $sub_to =~ s/\Q$from\E/$to/;
3172 $fixed[$fixlinenr] =~
3173 s@\Q$sub_from\E@$sub_to@;
3174 }
3175 }
3176 }
3177 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3178 #print "BB<$1>\n";
3179 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3180
3181 # Should start with a space.
3182 $to =~ s/^(\S)/ $1/;
3183 # Should not end with a space.
3184 $to =~ s/\s+$//;
3185 # '*'s should not have spaces between.
3186 while ($to =~ s/\*\s+\*/\*\*/) {
3187 }
3188 # Modifiers should have spaces.
3189 $to =~ s/(\b$Modifier$)/$1 /;
3190
3191## print "2: from<$from> to<$to> ident<$ident>\n";
3192 if ($from ne $to && $ident !~ /^$Modifier$/) {
3193 if (ERROR("POINTER_LOCATION",
3194 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3195 $fix) {
3196
3197 my $sub_from = $match;
3198 my $sub_to = $match;
3199 $sub_to =~ s/\Q$from\E/$to/;
3200 $fixed[$fixlinenr] =~
3201 s@\Q$sub_from\E@$sub_to@;
3202 }
3203 }
3204 }
3205
3206# # no BUG() or BUG_ON()
3207# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3208# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3209# print "$herecurr";
3210# $clean = 0;
3211# }
3212
3213 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3214 WARN("LINUX_VERSION_CODE",
3215 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
3216 }
3217
3218# check for uses of printk_ratelimit
3219 if ($line =~ /\bprintk_ratelimit\s*\(/) {
3220 WARN("PRINTK_RATELIMITED",
3221"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
3222 }
3223
3224# printk should use KERN_* levels. Note that follow on printk's on the
3225# same line do not need a level, so we use the current block context
3226# to try and find and validate the current printk. In summary the current
3227# printk includes all preceding printk's which have no newline on the end.
3228# we assume the first bad printk is the one to report.
3229 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
3230 my $ok = 0;
3231 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3232 #print "CHECK<$lines[$ln - 1]\n";
3233 # we have a preceding printk if it ends
3234 # with "\n" ignore it, else it is to blame
3235 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3236 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3237 $ok = 1;
3238 }
3239 last;
3240 }
3241 }
3242 if ($ok == 0) {
3243 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3244 "printk() should include KERN_ facility level\n" . $herecurr);
3245 }
3246 }
3247
3248 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3249 my $orig = $1;
3250 my $level = lc($orig);
3251 $level = "warn" if ($level eq "warning");
3252 my $level2 = $level;
3253 $level2 = "dbg" if ($level eq "debug");
3254 WARN("PREFER_PR_LEVEL",
3255 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
3256 }
3257
3258 if ($line =~ /\bpr_warning\s*\(/) {
3259 if (WARN("PREFER_PR_LEVEL",
3260 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3261 $fix) {
3262 $fixed[$fixlinenr] =~
3263 s/\bpr_warning\b/pr_warn/;
3264 }
3265 }
3266
3267 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3268 my $orig = $1;
3269 my $level = lc($orig);
3270 $level = "warn" if ($level eq "warning");
3271 $level = "dbg" if ($level eq "debug");
3272 WARN("PREFER_DEV_LEVEL",
3273 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3274 }
3275
3276# function brace can't be on same line, except for #defines of do while,
3277# or if closed on same line
3278 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3279 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
3280 if (ERROR("OPEN_BRACE",
3281 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3282 $fix) {
3283 fix_delete_line($fixlinenr, $rawline);
3284 my $fixed_line = $rawline;
3285 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3286 my $line1 = $1;
3287 my $line2 = $2;
3288 fix_insert_line($fixlinenr, ltrim($line1));
3289 fix_insert_line($fixlinenr, "\+{");
3290 if ($line2 !~ /^\s*$/) {
3291 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3292 }
3293 }
3294 }
3295
3296# open braces for enum, union and struct go on the same line.
3297 if ($line =~ /^.\s*{/ &&
3298 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
3299 if (ERROR("OPEN_BRACE",
3300 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3301 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3302 fix_delete_line($fixlinenr - 1, $prevrawline);
3303 fix_delete_line($fixlinenr, $rawline);
3304 my $fixedline = rtrim($prevrawline) . " {";
3305 fix_insert_line($fixlinenr, $fixedline);
3306 $fixedline = $rawline;
3307 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3308 if ($fixedline !~ /^\+\s*$/) {
3309 fix_insert_line($fixlinenr, $fixedline);
3310 }
3311 }
3312 }
3313
3314# missing space after union, struct or enum definition
3315 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3316 if (WARN("SPACING",
3317 "missing space after $1 definition\n" . $herecurr) &&
3318 $fix) {
3319 $fixed[$fixlinenr] =~
3320 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3321 }
3322 }
3323
3324# Function pointer declarations
3325# check spacing between type, funcptr, and args
3326# canonical declaration is "type (*funcptr)(args...)"
3327 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
3328 my $declare = $1;
3329 my $pre_pointer_space = $2;
3330 my $post_pointer_space = $3;
3331 my $funcname = $4;
3332 my $post_funcname_space = $5;
3333 my $pre_args_space = $6;
3334
3335# the $Declare variable will capture all spaces after the type
3336# so check it for a missing trailing missing space but pointer return types
3337# don't need a space so don't warn for those.
3338 my $post_declare_space = "";
3339 if ($declare =~ /(\s+)$/) {
3340 $post_declare_space = $1;
3341 $declare = rtrim($declare);
3342 }
3343 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
3344 WARN("SPACING",
3345 "missing space after return type\n" . $herecurr);
3346 $post_declare_space = " ";
3347 }
3348
3349# unnecessary space "type (*funcptr)(args...)"
3350# This test is not currently implemented because these declarations are
3351# equivalent to
3352# int foo(int bar, ...)
3353# and this is form shouldn't/doesn't generate a checkpatch warning.
3354#
3355# elsif ($declare =~ /\s{2,}$/) {
3356# WARN("SPACING",
3357# "Multiple spaces after return type\n" . $herecurr);
3358# }
3359
3360# unnecessary space "type ( *funcptr)(args...)"
3361 if (defined $pre_pointer_space &&
3362 $pre_pointer_space =~ /^\s/) {
3363 WARN("SPACING",
3364 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3365 }
3366
3367# unnecessary space "type (* funcptr)(args...)"
3368 if (defined $post_pointer_space &&
3369 $post_pointer_space =~ /^\s/) {
3370 WARN("SPACING",
3371 "Unnecessary space before function pointer name\n" . $herecurr);
3372 }
3373
3374# unnecessary space "type (*funcptr )(args...)"
3375 if (defined $post_funcname_space &&
3376 $post_funcname_space =~ /^\s/) {
3377 WARN("SPACING",
3378 "Unnecessary space after function pointer name\n" . $herecurr);
3379 }
3380
3381# unnecessary space "type (*funcptr) (args...)"
3382 if (defined $pre_args_space &&
3383 $pre_args_space =~ /^\s/) {
3384 WARN("SPACING",
3385 "Unnecessary space before function pointer arguments\n" . $herecurr);
3386 }
3387
3388 if (show_type("SPACING") && $fix) {
3389 $fixed[$fixlinenr] =~
3390 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
3391 }
3392 }
3393
3394# check for spacing round square brackets; allowed:
3395# 1. with a type on the left -- int [] a;
3396# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3397# 3. inside a curly brace -- = { [0...10] = 5 }
3398 while ($line =~ /(.*?\s)\[/g) {
3399 my ($where, $prefix) = ($-[1], $1);
3400 if ($prefix !~ /$Type\s+$/ &&
3401 ($where != 0 || $prefix !~ /^.\s+$/) &&
3402 $prefix !~ /[{,]\s+$/) {
3403 if (ERROR("BRACKET_SPACE",
3404 "space prohibited before open square bracket '['\n" . $herecurr) &&
3405 $fix) {
3406 $fixed[$fixlinenr] =~
3407 s/^(\+.*?)\s+\[/$1\[/;
3408 }
3409 }
3410 }
3411
3412# check for spaces between functions and their parentheses.
3413 while ($line =~ /($Ident)\s+\(/g) {
3414 my $name = $1;
3415 my $ctx_before = substr($line, 0, $-[1]);
3416 my $ctx = "$ctx_before$name";
3417
3418 # Ignore those directives where spaces _are_ permitted.
3419 if ($name =~ /^(?:
3420 if|for|while|switch|return|case|
3421 volatile|__volatile__|
3422 __attribute__|format|__extension__|
3423 asm|__asm__)$/x)
3424 {
3425 # cpp #define statements have non-optional spaces, ie
3426 # if there is a space between the name and the open
3427 # parenthesis it is simply not a parameter group.
3428 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3429
3430 # cpp #elif statement condition may start with a (
3431 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3432
3433 # If this whole things ends with a type its most
3434 # likely a typedef for a function.
3435 } elsif ($ctx =~ /$Type$/) {
3436
3437 } else {
3438 if (WARN("SPACING",
3439 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3440 $fix) {
3441 $fixed[$fixlinenr] =~
3442 s/\b$name\s+\(/$name\(/;
3443 }
3444 }
3445 }
3446
3447# Check operator spacing.
3448 if (!($line=~/\#\s*include/)) {
3449 my $fixed_line = "";
3450 my $line_fixed = 0;
3451
3452 my $ops = qr{
3453 <<=|>>=|<=|>=|==|!=|
3454 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3455 =>|->|<<|>>|<|>|=|!|~|
3456 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
3457 \?:|\?|:
3458 }x;
3459 my @elements = split(/($ops|;)/, $opline);
3460
3461## print("element count: <" . $#elements . ">\n");
3462## foreach my $el (@elements) {
3463## print("el: <$el>\n");
3464## }
3465
3466 my @fix_elements = ();
3467 my $off = 0;
3468
3469 foreach my $el (@elements) {
3470 push(@fix_elements, substr($rawline, $off, length($el)));
3471 $off += length($el);
3472 }
3473
3474 $off = 0;
3475
3476 my $blank = copy_spacing($opline);
3477 my $last_after = -1;
3478
3479 for (my $n = 0; $n < $#elements; $n += 2) {
3480
3481 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3482
3483## print("n: <$n> good: <$good>\n");
3484
3485 $off += length($elements[$n]);
3486
3487 # Pick up the preceding and succeeding characters.
3488 my $ca = substr($opline, 0, $off);
3489 my $cc = '';
3490 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3491 $cc = substr($opline, $off + length($elements[$n + 1]));
3492 }
3493 my $cb = "$ca$;$cc";
3494
3495 my $a = '';
3496 $a = 'V' if ($elements[$n] ne '');
3497 $a = 'W' if ($elements[$n] =~ /\s$/);
3498 $a = 'C' if ($elements[$n] =~ /$;$/);
3499 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3500 $a = 'O' if ($elements[$n] eq '');
3501 $a = 'E' if ($ca =~ /^\s*$/);
3502
3503 my $op = $elements[$n + 1];
3504
3505 my $c = '';
3506 if (defined $elements[$n + 2]) {
3507 $c = 'V' if ($elements[$n + 2] ne '');
3508 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
3509 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
3510 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3511 $c = 'O' if ($elements[$n + 2] eq '');
3512 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
3513 } else {
3514 $c = 'E';
3515 }
3516
3517 my $ctx = "${a}x${c}";
3518
3519 my $at = "(ctx:$ctx)";
3520
3521 my $ptr = substr($blank, 0, $off) . "^";
3522 my $hereptr = "$hereline$ptr\n";
3523
3524 # Pull out the value of this operator.
3525 my $op_type = substr($curr_values, $off + 1, 1);
3526
3527 # Get the full operator variant.
3528 my $opv = $op . substr($curr_vars, $off, 1);
3529
3530 # Ignore operators passed as parameters.
3531 if ($op_type ne 'V' &&
3532 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3533
3534# # Ignore comments
3535# } elsif ($op =~ /^$;+$/) {
3536
3537 # ; should have either the end of line or a space or \ after it
3538 } elsif ($op eq ';') {
3539 if ($ctx !~ /.x[WEBC]/ &&
3540 $cc !~ /^\\/ && $cc !~ /^;/) {
3541 if (ERROR("SPACING",
3542 "space required after that '$op' $at\n" . $hereptr)) {
3543 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3544 $line_fixed = 1;
3545 }
3546 }
3547
3548 # // is a comment
3549 } elsif ($op eq '//') {
3550
3551 # : when part of a bitfield
3552 } elsif ($opv eq ':B') {
3553 # skip the bitfield test for now
3554
3555 # No spaces for:
3556 # ->
3557 } elsif ($op eq '->') {
3558 if ($ctx =~ /Wx.|.xW/) {
3559 if (ERROR("SPACING",
3560 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3561 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3562 if (defined $fix_elements[$n + 2]) {
3563 $fix_elements[$n + 2] =~ s/^\s+//;
3564 }
3565 $line_fixed = 1;
3566 }
3567 }
3568
3569 # , must have a space on the right.
3570 } elsif ($op eq ',') {
3571 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
3572 if (ERROR("SPACING",
3573 "space required after that '$op' $at\n" . $hereptr)) {
3574 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3575 $line_fixed = 1;
3576 $last_after = $n;
3577 }
3578 }
3579
3580 # '*' as part of a type definition -- reported already.
3581 } elsif ($opv eq '*_') {
3582 #warn "'*' is part of type\n";
3583
3584 # unary operators should have a space before and
3585 # none after. May be left adjacent to another
3586 # unary operator, or a cast
3587 } elsif ($op eq '!' || $op eq '~' ||
3588 $opv eq '*U' || $opv eq '-U' ||
3589 $opv eq '&U' || $opv eq '&&U') {
3590 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
3591 if (ERROR("SPACING",
3592 "space required before that '$op' $at\n" . $hereptr)) {
3593 if ($n != $last_after + 2) {
3594 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3595 $line_fixed = 1;
3596 }
3597 }
3598 }
3599 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3600 # A unary '*' may be const
3601
3602 } elsif ($ctx =~ /.xW/) {
3603 if (ERROR("SPACING",
3604 "space prohibited after that '$op' $at\n" . $hereptr)) {
3605 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
3606 if (defined $fix_elements[$n + 2]) {
3607 $fix_elements[$n + 2] =~ s/^\s+//;
3608 }
3609 $line_fixed = 1;
3610 }
3611 }
3612
3613 # unary ++ and unary -- are allowed no space on one side.
3614 } elsif ($op eq '++' or $op eq '--') {
3615 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
3616 if (ERROR("SPACING",
3617 "space required one side of that '$op' $at\n" . $hereptr)) {
3618 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3619 $line_fixed = 1;
3620 }
3621 }
3622 if ($ctx =~ /Wx[BE]/ ||
3623 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
3624 if (ERROR("SPACING",
3625 "space prohibited before that '$op' $at\n" . $hereptr)) {
3626 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3627 $line_fixed = 1;
3628 }
3629 }
3630 if ($ctx =~ /ExW/) {
3631 if (ERROR("SPACING",
3632 "space prohibited after that '$op' $at\n" . $hereptr)) {
3633 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3634 if (defined $fix_elements[$n + 2]) {
3635 $fix_elements[$n + 2] =~ s/^\s+//;
3636 }
3637 $line_fixed = 1;
3638 }
3639 }
3640
3641 # << and >> may either have or not have spaces both sides
3642 } elsif ($op eq '<<' or $op eq '>>' or
3643 $op eq '&' or $op eq '^' or $op eq '|' or
3644 $op eq '+' or $op eq '-' or
3645 $op eq '*' or $op eq '/' or
3646 $op eq '%')
3647 {
3648 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
3649 if (ERROR("SPACING",
3650 "need consistent spacing around '$op' $at\n" . $hereptr)) {
3651 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3652 if (defined $fix_elements[$n + 2]) {
3653 $fix_elements[$n + 2] =~ s/^\s+//;
3654 }
3655 $line_fixed = 1;
3656 }
3657 }
3658
3659 # A colon needs no spaces before when it is
3660 # terminating a case value or a label.
3661 } elsif ($opv eq ':C' || $opv eq ':L') {
3662 if ($ctx =~ /Wx./) {
3663 if (ERROR("SPACING",
3664 "space prohibited before that '$op' $at\n" . $hereptr)) {
3665 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3666 $line_fixed = 1;
3667 }
3668 }
3669
3670 # All the others need spaces both sides.
3671 } elsif ($ctx !~ /[EWC]x[CWE]/) {
3672 my $ok = 0;
3673
3674 # Ignore email addresses <foo@bar>
3675 if (($op eq '<' &&
3676 $cc =~ /^\S+\@\S+>/) ||
3677 ($op eq '>' &&
3678 $ca =~ /<\S+\@\S+$/))
3679 {
3680 $ok = 1;
3681 }
3682
3683 # messages are ERROR, but ?: are CHK
3684 if ($ok == 0) {
3685 my $msg_type = \&ERROR;
3686 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3687
3688 if (&{$msg_type}("SPACING",
3689 "spaces required around that '$op' $at\n" . $hereptr)) {
3690 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3691 if (defined $fix_elements[$n + 2]) {
3692 $fix_elements[$n + 2] =~ s/^\s+//;
3693 }
3694 $line_fixed = 1;
3695 }
3696 }
3697 }
3698 $off += length($elements[$n + 1]);
3699
3700## print("n: <$n> GOOD: <$good>\n");
3701
3702 $fixed_line = $fixed_line . $good;
3703 }
3704
3705 if (($#elements % 2) == 0) {
3706 $fixed_line = $fixed_line . $fix_elements[$#elements];
3707 }
3708
3709 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3710 $fixed[$fixlinenr] = $fixed_line;
3711 }
3712
3713
3714 }
3715
3716# check for whitespace before a non-naked semicolon
3717 if ($line =~ /^\+.*\S\s+;\s*$/) {
3718 if (WARN("SPACING",
3719 "space prohibited before semicolon\n" . $herecurr) &&
3720 $fix) {
3721 1 while $fixed[$fixlinenr] =~
3722 s/^(\+.*\S)\s+;/$1;/;
3723 }
3724 }
3725
3726# check for multiple assignments
3727 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3728 CHK("MULTIPLE_ASSIGNMENTS",
3729 "multiple assignments should be avoided\n" . $herecurr);
3730 }
3731
3732## # check for multiple declarations, allowing for a function declaration
3733## # continuation.
3734## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3735## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3736##
3737## # Remove any bracketed sections to ensure we do not
3738## # falsly report the parameters of functions.
3739## my $ln = $line;
3740## while ($ln =~ s/\([^\(\)]*\)//g) {
3741## }
3742## if ($ln =~ /,/) {
3743## WARN("MULTIPLE_DECLARATION",
3744## "declaring multiple variables together should be avoided\n" . $herecurr);
3745## }
3746## }
3747
3748#need space before brace following if, while, etc
3749 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3750 $line =~ /do{/) {
3751 if (ERROR("SPACING",
3752 "space required before the open brace '{'\n" . $herecurr) &&
3753 $fix) {
3754 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
3755 }
3756 }
3757
3758## # check for blank lines before declarations
3759## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3760## $prevrawline =~ /^.\s*$/) {
3761## WARN("SPACING",
3762## "No blank lines before declarations\n" . $hereprev);
3763## }
3764##
3765
3766# closing brace should have a space following it when it has anything
3767# on the line
3768 if ($line =~ /}(?!(?:,|;|\)))\S/) {
3769 if (ERROR("SPACING",
3770 "space required after that close brace '}'\n" . $herecurr) &&
3771 $fix) {
3772 $fixed[$fixlinenr] =~
3773 s/}((?!(?:,|;|\)))\S)/} $1/;
3774 }
3775 }
3776
3777# check spacing on square brackets
3778 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
3779 if (ERROR("SPACING",
3780 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3781 $fix) {
3782 $fixed[$fixlinenr] =~
3783 s/\[\s+/\[/;
3784 }
3785 }
3786 if ($line =~ /\s\]/) {
3787 if (ERROR("SPACING",
3788 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3789 $fix) {
3790 $fixed[$fixlinenr] =~
3791 s/\s+\]/\]/;
3792 }
3793 }
3794
3795# check spacing on parentheses
3796 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3797 $line !~ /for\s*\(\s+;/) {
3798 if (ERROR("SPACING",
3799 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3800 $fix) {
3801 $fixed[$fixlinenr] =~
3802 s/\(\s+/\(/;
3803 }
3804 }
3805 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3806 $line !~ /for\s*\(.*;\s+\)/ &&
3807 $line !~ /:\s+\)/) {
3808 if (ERROR("SPACING",
3809 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3810 $fix) {
3811 $fixed[$fixlinenr] =~
3812 s/\s+\)/\)/;
3813 }
3814 }
3815
3816# check unnecessary parentheses around addressof/dereference single $Lvals
3817# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3818
3819 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3820 CHK("UNNECESSARY_PARENTHESES",
3821 "Unnecessary parentheses around $1\n" . $herecurr);
3822 }
3823
3824#goto labels aren't indented, allow a single space however
3825 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
3826 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
3827 if (WARN("INDENTED_LABEL",
3828 "labels should not be indented\n" . $herecurr) &&
3829 $fix) {
3830 $fixed[$fixlinenr] =~
3831 s/^(.)\s+/$1/;
3832 }
3833 }
3834
3835# return is not a function
3836 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3837 my $spacing = $1;
3838 if ($^V && $^V ge 5.10.0 &&
3839 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3840 my $value = $1;
3841 $value = deparenthesize($value);
3842 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3843 ERROR("RETURN_PARENTHESES",
3844 "return is not a function, parentheses are not required\n" . $herecurr);
3845 }
3846 } elsif ($spacing !~ /\s+/) {
3847 ERROR("SPACING",
3848 "space required before the open parenthesis '('\n" . $herecurr);
3849 }
3850 }
3851
3852# unnecessary return in a void function
3853# at end-of-function, with the previous line a single leading tab, then return;
3854# and the line before that not a goto label target like "out:"
3855 if ($sline =~ /^[ \+]}\s*$/ &&
3856 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3857 $linenr >= 3 &&
3858 $lines[$linenr - 3] =~ /^[ +]/ &&
3859 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
3860 WARN("RETURN_VOID",
3861 "void function return statements are not generally useful\n" . $hereprev);
3862 }
3863
3864# if statements using unnecessary parentheses - ie: if ((foo == bar))
3865 if ($^V && $^V ge 5.10.0 &&
3866 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3867 my $openparens = $1;
3868 my $count = $openparens =~ tr@\(@\(@;
3869 my $msg = "";
3870 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3871 my $comp = $4; #Not $1 because of $LvalOrFunc
3872 $msg = " - maybe == should be = ?" if ($comp eq "==");
3873 WARN("UNNECESSARY_PARENTHESES",
3874 "Unnecessary parentheses$msg\n" . $herecurr);
3875 }
3876 }
3877
3878# Return of what appears to be an errno should normally be -'ve
3879 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3880 my $name = $1;
3881 if ($name ne 'EOF' && $name ne 'ERROR') {
3882 WARN("USE_NEGATIVE_ERRNO",
3883 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
3884 }
3885 }
3886
3887# Need a space before open parenthesis after if, while etc
3888 if ($line =~ /\b(if|while|for|switch)\(/) {
3889 if (ERROR("SPACING",
3890 "space required before the open parenthesis '('\n" . $herecurr) &&
3891 $fix) {
3892 $fixed[$fixlinenr] =~
3893 s/\b(if|while|for|switch)\(/$1 \(/;
3894 }
3895 }
3896
3897# Check for illegal assignment in if conditional -- and check for trailing
3898# statements after the conditional.
3899 if ($line =~ /do\s*(?!{)/) {
3900 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3901 ctx_statement_block($linenr, $realcnt, 0)
3902 if (!defined $stat);
3903 my ($stat_next) = ctx_statement_block($line_nr_next,
3904 $remain_next, $off_next);
3905 $stat_next =~ s/\n./\n /g;
3906 ##print "stat<$stat> stat_next<$stat_next>\n";
3907
3908 if ($stat_next =~ /^\s*while\b/) {
3909 # If the statement carries leading newlines,
3910 # then count those as offsets.
3911 my ($whitespace) =
3912 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3913 my $offset =
3914 statement_rawlines($whitespace) - 1;
3915
3916 $suppress_whiletrailers{$line_nr_next +
3917 $offset} = 1;
3918 }
3919 }
3920 if (!defined $suppress_whiletrailers{$linenr} &&
3921 defined($stat) && defined($cond) &&
3922 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3923 my ($s, $c) = ($stat, $cond);
3924
3925 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3926 ERROR("ASSIGN_IN_IF",
3927 "do not use assignment in if condition\n" . $herecurr);
3928 }
3929
3930 # Find out what is on the end of the line after the
3931 # conditional.
3932 substr($s, 0, length($c), '');
3933 $s =~ s/\n.*//g;
3934 $s =~ s/$;//g; # Remove any comments
3935 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3936 $c !~ /}\s*while\s*/)
3937 {
3938 # Find out how long the conditional actually is.
3939 my @newlines = ($c =~ /\n/gs);
3940 my $cond_lines = 1 + $#newlines;
3941 my $stat_real = '';
3942
3943 $stat_real = raw_line($linenr, $cond_lines)
3944 . "\n" if ($cond_lines);
3945 if (defined($stat_real) && $cond_lines > 1) {
3946 $stat_real = "[...]\n$stat_real";
3947 }
3948
3949 ERROR("TRAILING_STATEMENTS",
3950 "trailing statements should be on next line\n" . $herecurr . $stat_real);
3951 }
3952 }
3953
3954# Check for bitwise tests written as boolean
3955 if ($line =~ /
3956 (?:
3957 (?:\[|\(|\&\&|\|\|)
3958 \s*0[xX][0-9]+\s*
3959 (?:\&\&|\|\|)
3960 |
3961 (?:\&\&|\|\|)
3962 \s*0[xX][0-9]+\s*
3963 (?:\&\&|\|\||\)|\])
3964 )/x)
3965 {
3966 WARN("HEXADECIMAL_BOOLEAN_TEST",
3967 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
3968 }
3969
3970# if and else should not have general statements after it
3971 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3972 my $s = $1;
3973 $s =~ s/$;//g; # Remove any comments
3974 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3975 ERROR("TRAILING_STATEMENTS",
3976 "trailing statements should be on next line\n" . $herecurr);
3977 }
3978 }
3979# if should not continue a brace
3980 if ($line =~ /}\s*if\b/) {
3981 ERROR("TRAILING_STATEMENTS",
3982 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
3983 $herecurr);
3984 }
3985# case and default should not have general statements after them
3986 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3987 $line !~ /\G(?:
3988 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3989 \s*return\s+
3990 )/xg)
3991 {
3992 ERROR("TRAILING_STATEMENTS",
3993 "trailing statements should be on next line\n" . $herecurr);
3994 }
3995
3996 # Check for }<nl>else {, these must be at the same
3997 # indent level to be relevant to each other.
3998 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
3999 $previndent == $indent) {
4000 if (ERROR("ELSE_AFTER_BRACE",
4001 "else should follow close brace '}'\n" . $hereprev) &&
4002 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4003 fix_delete_line($fixlinenr - 1, $prevrawline);
4004 fix_delete_line($fixlinenr, $rawline);
4005 my $fixedline = $prevrawline;
4006 $fixedline =~ s/}\s*$//;
4007 if ($fixedline !~ /^\+\s*$/) {
4008 fix_insert_line($fixlinenr, $fixedline);
4009 }
4010 $fixedline = $rawline;
4011 $fixedline =~ s/^(.\s*)else/$1} else/;
4012 fix_insert_line($fixlinenr, $fixedline);
4013 }
4014 }
4015
4016 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4017 $previndent == $indent) {
4018 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4019
4020 # Find out what is on the end of the line after the
4021 # conditional.
4022 substr($s, 0, length($c), '');
4023 $s =~ s/\n.*//g;
4024
4025 if ($s =~ /^\s*;/) {
4026 if (ERROR("WHILE_AFTER_BRACE",
4027 "while should follow close brace '}'\n" . $hereprev) &&
4028 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4029 fix_delete_line($fixlinenr - 1, $prevrawline);
4030 fix_delete_line($fixlinenr, $rawline);
4031 my $fixedline = $prevrawline;
4032 my $trailing = $rawline;
4033 $trailing =~ s/^\+//;
4034 $trailing = trim($trailing);
4035 $fixedline =~ s/}\s*$/} $trailing/;
4036 fix_insert_line($fixlinenr, $fixedline);
4037 }
4038 }
4039 }
4040
4041#Specific variable tests
4042 while ($line =~ m{($Constant|$Lval)}g) {
4043 my $var = $1;
4044
4045#gcc binary extension
4046 if ($var =~ /^$Binary$/) {
4047 if (WARN("GCC_BINARY_CONSTANT",
4048 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4049 $fix) {
4050 my $hexval = sprintf("0x%x", oct($var));
4051 $fixed[$fixlinenr] =~
4052 s/\b$var\b/$hexval/;
4053 }
4054 }
4055
4056#CamelCase
4057 if ($var !~ /^$Constant$/ &&
4058 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
4059#Ignore Page<foo> variants
4060 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
4061#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4062 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
4063 while ($var =~ m{($Ident)}g) {
4064 my $word = $1;
4065 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4066 if ($check) {
4067 seed_camelcase_includes();
4068 if (!$file && !$camelcase_file_seeded) {
4069 seed_camelcase_file($realfile);
4070 $camelcase_file_seeded = 1;
4071 }
4072 }
4073 if (!defined $camelcase{$word}) {
4074 $camelcase{$word} = 1;
4075 CHK("CAMELCASE",
4076 "Avoid CamelCase: <$word>\n" . $herecurr);
4077 }
4078 }
4079 }
4080 }
4081
4082#no spaces allowed after \ in define
4083 if ($line =~ /\#\s*define.*\\\s+$/) {
4084 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4085 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4086 $fix) {
4087 $fixed[$fixlinenr] =~ s/\s+$//;
4088 }
4089 }
4090
4091#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4092 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4093 my $file = "$1.h";
4094 my $checkfile = "include/linux/$file";
4095 if (-f "$root/$checkfile" &&
4096 $realfile ne $checkfile &&
4097 $1 !~ /$allowed_asm_includes/)
4098 {
4099 if ($realfile =~ m{^arch/}) {
4100 CHK("ARCH_INCLUDE_LINUX",
4101 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4102 } else {
4103 WARN("INCLUDE_LINUX",
4104 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4105 }
4106 }
4107 }
4108
4109# multi-statement macros should be enclosed in a do while loop, grab the
4110# first statement and ensure its the whole macro if its not enclosed
4111# in a known good container
4112 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4113 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4114 my $ln = $linenr;
4115 my $cnt = $realcnt;
4116 my ($off, $dstat, $dcond, $rest);
4117 my $ctx = '';
4118 my $has_flow_statement = 0;
4119 my $has_arg_concat = 0;
4120 ($dstat, $dcond, $ln, $cnt, $off) =
4121 ctx_statement_block($linenr, $realcnt, 0);
4122 $ctx = $dstat;
4123 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4124 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4125
4126 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4127 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4128
4129 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4130 $dstat =~ s/$;//g;
4131 $dstat =~ s/\\\n.//g;
4132 $dstat =~ s/^\s*//s;
4133 $dstat =~ s/\s*$//s;
4134
4135 # Flatten any parentheses and braces
4136 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4137 $dstat =~ s/\{[^\{\}]*\}/1/ ||
4138 $dstat =~ s/\[[^\[\]]*\]/1/)
4139 {
4140 }
4141
4142 # Flatten any obvious string concatentation.
4143 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4144 $dstat =~ s/$Ident\s*("X*")/$1/)
4145 {
4146 }
4147
4148 my $exceptions = qr{
4149 $Declare|
4150 module_param_named|
4151 MODULE_PARM_DESC|
4152 DECLARE_PER_CPU|
4153 DEFINE_PER_CPU|
4154 __typeof__\(|
4155 union|
4156 struct|
4157 \.$Ident\s*=\s*|
4158 ^\"|\"$
4159 }x;
4160 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4161 if ($dstat ne '' &&
4162 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4163 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
4164 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4165 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
4166 $dstat !~ /$exceptions/ &&
4167 $dstat !~ /^\.$Ident\s*=/ && # .foo =
4168 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
4169 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
4170 $dstat !~ /^for\s*$Constant$/ && # for (...)
4171 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4172 $dstat !~ /^do\s*{/ && # do {...
4173 $dstat !~ /^\({/ && # ({...
4174 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4175 {
4176 $ctx =~ s/\n*$//;
4177 my $herectx = $here . "\n";
4178 my $cnt = statement_rawlines($ctx);
4179
4180 for (my $n = 0; $n < $cnt; $n++) {
4181 $herectx .= raw_line($linenr, $n) . "\n";
4182 }
4183
4184 if ($dstat =~ /;/) {
4185 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4186 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4187 } else {
4188 ERROR("COMPLEX_MACRO",
4189 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4190 }
4191 }
4192
4193# check for macros with flow control, but without ## concatenation
4194# ## concatenation is commonly a macro that defines a function so ignore those
4195 if ($has_flow_statement && !$has_arg_concat) {
4196 my $herectx = $here . "\n";
4197 my $cnt = statement_rawlines($ctx);
4198
4199 for (my $n = 0; $n < $cnt; $n++) {
4200 $herectx .= raw_line($linenr, $n) . "\n";
4201 }
4202 WARN("MACRO_WITH_FLOW_CONTROL",
4203 "Macros with flow control statements should be avoided\n" . "$herectx");
4204 }
4205
4206# check for line continuations outside of #defines, preprocessor #, and asm
4207
4208 } else {
4209 if ($prevline !~ /^..*\\$/ &&
4210 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4211 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
4212 $line =~ /^\+.*\\$/) {
4213 WARN("LINE_CONTINUATIONS",
4214 "Avoid unnecessary line continuations\n" . $herecurr);
4215 }
4216 }
4217
4218# do {} while (0) macro tests:
4219# single-statement macros do not need to be enclosed in do while (0) loop,
4220# macro should not end with a semicolon
4221 if ($^V && $^V ge 5.10.0 &&
4222 $realfile !~ m@/vmlinux.lds.h$@ &&
4223 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4224 my $ln = $linenr;
4225 my $cnt = $realcnt;
4226 my ($off, $dstat, $dcond, $rest);
4227 my $ctx = '';
4228 ($dstat, $dcond, $ln, $cnt, $off) =
4229 ctx_statement_block($linenr, $realcnt, 0);
4230 $ctx = $dstat;
4231
4232 $dstat =~ s/\\\n.//g;
4233
4234 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4235 my $stmts = $2;
4236 my $semis = $3;
4237
4238 $ctx =~ s/\n*$//;
4239 my $cnt = statement_rawlines($ctx);
4240 my $herectx = $here . "\n";
4241
4242 for (my $n = 0; $n < $cnt; $n++) {
4243 $herectx .= raw_line($linenr, $n) . "\n";
4244 }
4245
4246 if (($stmts =~ tr/;/;/) == 1 &&
4247 $stmts !~ /^\s*(if|while|for|switch)\b/) {
4248 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4249 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4250 }
4251 if (defined $semis && $semis ne "") {
4252 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4253 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4254 }
4255 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4256 $ctx =~ s/\n*$//;
4257 my $cnt = statement_rawlines($ctx);
4258 my $herectx = $here . "\n";
4259
4260 for (my $n = 0; $n < $cnt; $n++) {
4261 $herectx .= raw_line($linenr, $n) . "\n";
4262 }
4263
4264 WARN("TRAILING_SEMICOLON",
4265 "macros should not use a trailing semicolon\n" . "$herectx");
4266 }
4267 }
4268
4269# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4270# all assignments may have only one of the following with an assignment:
4271# .
4272# ALIGN(...)
4273# VMLINUX_SYMBOL(...)
4274 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4275 WARN("MISSING_VMLINUX_SYMBOL",
4276 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4277 }
4278
4279# check for redundant bracing round if etc
4280 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4281 my ($level, $endln, @chunks) =
4282 ctx_statement_full($linenr, $realcnt, 1);
4283 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4284 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4285 if ($#chunks > 0 && $level == 0) {
4286 my @allowed = ();
4287 my $allow = 0;
4288 my $seen = 0;
4289 my $herectx = $here . "\n";
4290 my $ln = $linenr - 1;
4291 for my $chunk (@chunks) {
4292 my ($cond, $block) = @{$chunk};
4293
4294 # If the condition carries leading newlines, then count those as offsets.
4295 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4296 my $offset = statement_rawlines($whitespace) - 1;
4297
4298 $allowed[$allow] = 0;
4299 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4300
4301 # We have looked at and allowed this specific line.
4302 $suppress_ifbraces{$ln + $offset} = 1;
4303
4304 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4305 $ln += statement_rawlines($block) - 1;
4306
4307 substr($block, 0, length($cond), '');
4308
4309 $seen++ if ($block =~ /^\s*{/);
4310
4311 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4312 if (statement_lines($cond) > 1) {
4313 #print "APW: ALLOWED: cond<$cond>\n";
4314 $allowed[$allow] = 1;
4315 }
4316 if ($block =~/\b(?:if|for|while)\b/) {
4317 #print "APW: ALLOWED: block<$block>\n";
4318 $allowed[$allow] = 1;
4319 }
4320 if (statement_block_size($block) > 1) {
4321 #print "APW: ALLOWED: lines block<$block>\n";
4322 $allowed[$allow] = 1;
4323 }
4324 $allow++;
4325 }
4326 if ($seen) {
4327 my $sum_allowed = 0;
4328 foreach (@allowed) {
4329 $sum_allowed += $_;
4330 }
4331 if ($sum_allowed == 0) {
4332 WARN("BRACES",
4333 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4334 } elsif ($sum_allowed != $allow &&
4335 $seen != $allow) {
4336 CHK("BRACES",
4337 "braces {} should be used on all arms of this statement\n" . $herectx);
4338 }
4339 }
4340 }
4341 }
4342 if (!defined $suppress_ifbraces{$linenr - 1} &&
4343 $line =~ /\b(if|while|for|else)\b/) {
4344 my $allowed = 0;
4345
4346 # Check the pre-context.
4347 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4348 #print "APW: ALLOWED: pre<$1>\n";
4349 $allowed = 1;
4350 }
4351
4352 my ($level, $endln, @chunks) =
4353 ctx_statement_full($linenr, $realcnt, $-[0]);
4354
4355 # Check the condition.
4356 my ($cond, $block) = @{$chunks[0]};
4357 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4358 if (defined $cond) {
4359 substr($block, 0, length($cond), '');
4360 }
4361 if (statement_lines($cond) > 1) {
4362 #print "APW: ALLOWED: cond<$cond>\n";
4363 $allowed = 1;
4364 }
4365 if ($block =~/\b(?:if|for|while)\b/) {
4366 #print "APW: ALLOWED: block<$block>\n";
4367 $allowed = 1;
4368 }
4369 if (statement_block_size($block) > 1) {
4370 #print "APW: ALLOWED: lines block<$block>\n";
4371 $allowed = 1;
4372 }
4373 # Check the post-context.
4374 if (defined $chunks[1]) {
4375 my ($cond, $block) = @{$chunks[1]};
4376 if (defined $cond) {
4377 substr($block, 0, length($cond), '');
4378 }
4379 if ($block =~ /^\s*\{/) {
4380 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4381 $allowed = 1;
4382 }
4383 }
4384 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
4385 my $herectx = $here . "\n";
4386 my $cnt = statement_rawlines($block);
4387
4388 for (my $n = 0; $n < $cnt; $n++) {
4389 $herectx .= raw_line($linenr, $n) . "\n";
4390 }
4391
4392 WARN("BRACES",
4393 "braces {} are not necessary for single statement blocks\n" . $herectx);
4394 }
4395 }
4396
4397# check for unnecessary blank lines around braces
4398 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4399 CHK("BRACES",
4400 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4401 }
4402 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4403 CHK("BRACES",
4404 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4405 }
4406
4407# no volatiles please
4408 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4409 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4410 WARN("VOLATILE",
4411 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4412 }
4413
4414# concatenated string without spaces between elements
4415 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4416 CHK("CONCATENATED_STRING",
4417 "Concatenated strings should use spaces between elements\n" . $herecurr);
4418 }
4419
4420# warn about #if 0
4421 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4422 CHK("REDUNDANT_CODE",
4423 "if this code is redundant consider removing it\n" .
4424 $herecurr);
4425 }
4426
4427# check for needless "if (<foo>) fn(<foo>)" uses
4428 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4429 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4430 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4431 WARN('NEEDLESS_IF',
4432 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
4433 }
4434 }
4435
4436# check for unnecessary "Out of Memory" messages
4437 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4438 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4439 (defined $1 || defined $3) &&
4440 $linenr > 3) {
4441 my $testval = $2;
4442 my $testline = $lines[$linenr - 3];
4443
4444 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4445# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4446
4447 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4448 WARN("OOM_MESSAGE",
4449 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4450 }
4451 }
4452
4453# check for logging functions with KERN_<LEVEL>
4454 if ($line !~ /printk\s*\(/ &&
4455 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4456 my $level = $1;
4457 if (WARN("UNNECESSARY_KERN_LEVEL",
4458 "Possible unnecessary $level\n" . $herecurr) &&
4459 $fix) {
4460 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4461 }
4462 }
4463
4464# check for bad placement of section $InitAttribute (e.g.: __initdata)
4465 if ($line =~ /(\b$InitAttribute\b)/) {
4466 my $attr = $1;
4467 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4468 my $ptr = $1;
4469 my $var = $2;
4470 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4471 ERROR("MISPLACED_INIT",
4472 "$attr should be placed after $var\n" . $herecurr)) ||
4473 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4474 WARN("MISPLACED_INIT",
4475 "$attr should be placed after $var\n" . $herecurr))) &&
4476 $fix) {
4477 $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;
4478 }
4479 }
4480 }
4481
4482# check for $InitAttributeData (ie: __initdata) with const
4483 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4484 my $attr = $1;
4485 $attr =~ /($InitAttributePrefix)(.*)/;
4486 my $attr_prefix = $1;
4487 my $attr_type = $2;
4488 if (ERROR("INIT_ATTRIBUTE",
4489 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4490 $fix) {
4491 $fixed[$fixlinenr] =~
4492 s/$InitAttributeData/${attr_prefix}initconst/;
4493 }
4494 }
4495
4496# check for $InitAttributeConst (ie: __initconst) without const
4497 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4498 my $attr = $1;
4499 if (ERROR("INIT_ATTRIBUTE",
4500 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4501 $fix) {
4502 my $lead = $fixed[$fixlinenr] =~
4503 /(^\+\s*(?:static\s+))/;
4504 $lead = rtrim($1);
4505 $lead = "$lead " if ($lead !~ /^\+$/);
4506 $lead = "${lead}const ";
4507 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4508 }
4509 }
4510
4511# don't use __constant_<foo> functions outside of include/uapi/
4512 if ($realfile !~ m@^include/uapi/@ &&
4513 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4514 my $constant_func = $1;
4515 my $func = $constant_func;
4516 $func =~ s/^__constant_//;
4517 if (WARN("CONSTANT_CONVERSION",
4518 "$constant_func should be $func\n" . $herecurr) &&
4519 $fix) {
4520 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4521 }
4522 }
4523
4524# prefer usleep_range over udelay
4525 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
4526 my $delay = $1;
4527 # ignore udelay's < 10, however
4528 if (! ($delay < 10) ) {
4529 CHK("USLEEP_RANGE",
4530 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4531 }
4532 if ($delay > 2000) {
4533 WARN("LONG_UDELAY",
4534 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
4535 }
4536 }
4537
4538# warn about unexpectedly long msleep's
4539 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4540 if ($1 < 20) {
4541 WARN("MSLEEP",
4542 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4543 }
4544 }
4545
4546# check for comparisons of jiffies
4547 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4548 WARN("JIFFIES_COMPARISON",
4549 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4550 }
4551
4552# check for comparisons of get_jiffies_64()
4553 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4554 WARN("JIFFIES_COMPARISON",
4555 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4556 }
4557
4558# warn about #ifdefs in C files
4559# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
4560# print "#ifdef in C files should be avoided\n";
4561# print "$herecurr";
4562# $clean = 0;
4563# }
4564
4565# warn about spacing in #ifdefs
4566 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
4567 if (ERROR("SPACING",
4568 "exactly one space required after that #$1\n" . $herecurr) &&
4569 $fix) {
4570 $fixed[$fixlinenr] =~
4571 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4572 }
4573
4574 }
4575
4576# check for spinlock_t definitions without a comment.
4577 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4578 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
4579 my $which = $1;
4580 if (!ctx_has_comment($first_line, $linenr)) {
4581 CHK("UNCOMMENTED_DEFINITION",
4582 "$1 definition without comment\n" . $herecurr);
4583 }
4584 }
4585# check for memory barriers without a comment.
4586 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4587 if (!ctx_has_comment($first_line, $linenr)) {
4588 WARN("MEMORY_BARRIER",
4589 "memory barrier without comment\n" . $herecurr);
4590 }
4591 }
4592# check of hardware specific defines
4593 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4594 CHK("ARCH_DEFINES",
4595 "architecture specific defines should be avoided\n" . $herecurr);
4596 }
4597
4598# Check that the storage class is at the beginning of a declaration
4599 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4600 WARN("STORAGE_CLASS",
4601 "storage class should be at the beginning of the declaration\n" . $herecurr)
4602 }
4603
4604# check the location of the inline attribute, that it is between
4605# storage class and type.
4606 if ($line =~ /\b$Type\s+$Inline\b/ ||
4607 $line =~ /\b$Inline\s+$Storage\b/) {
4608 ERROR("INLINE_LOCATION",
4609 "inline keyword should sit between storage class and type\n" . $herecurr);
4610 }
4611
4612# Check for __inline__ and __inline, prefer inline
4613 if ($realfile !~ m@\binclude/uapi/@ &&
4614 $line =~ /\b(__inline__|__inline)\b/) {
4615 if (WARN("INLINE",
4616 "plain inline is preferred over $1\n" . $herecurr) &&
4617 $fix) {
4618 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4619
4620 }
4621 }
4622
4623# Check for __attribute__ packed, prefer __packed
4624 if ($realfile !~ m@\binclude/uapi/@ &&
4625 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4626 WARN("PREFER_PACKED",
4627 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
4628 }
4629
4630# Check for __attribute__ aligned, prefer __aligned
4631 if ($realfile !~ m@\binclude/uapi/@ &&
4632 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4633 WARN("PREFER_ALIGNED",
4634 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
4635 }
4636
4637# Check for __attribute__ format(printf, prefer __printf
4638 if ($realfile !~ m@\binclude/uapi/@ &&
4639 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4640 if (WARN("PREFER_PRINTF",
4641 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4642 $fix) {
4643 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4644
4645 }
4646 }
4647
4648# Check for __attribute__ format(scanf, prefer __scanf
4649 if ($realfile !~ m@\binclude/uapi/@ &&
4650 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4651 if (WARN("PREFER_SCANF",
4652 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4653 $fix) {
4654 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4655 }
4656 }
4657
4658# check for sizeof(&)
4659 if ($line =~ /\bsizeof\s*\(\s*\&/) {
4660 WARN("SIZEOF_ADDRESS",
4661 "sizeof(& should be avoided\n" . $herecurr);
4662 }
4663
4664# check for sizeof without parenthesis
4665 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4666 if (WARN("SIZEOF_PARENTHESIS",
4667 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4668 $fix) {
4669 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4670 }
4671 }
4672
4673# check for line continuations in quoted strings with odd counts of "
4674 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4675 WARN("LINE_CONTINUATIONS",
4676 "Avoid line continuations in quoted strings\n" . $herecurr);
4677 }
4678
4679# check for struct spinlock declarations
4680 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4681 WARN("USE_SPINLOCK_T",
4682 "struct spinlock should be spinlock_t\n" . $herecurr);
4683 }
4684
4685# check for seq_printf uses that could be seq_puts
4686 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4687 my $fmt = get_quoted_string($line, $rawline);
4688 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4689 if (WARN("PREFER_SEQ_PUTS",
4690 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4691 $fix) {
4692 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4693 }
4694 }
4695 }
4696
4697# Check for misused memsets
4698 if ($^V && $^V ge 5.10.0 &&
4699 defined $stat &&
4700 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4701
4702 my $ms_addr = $2;
4703 my $ms_val = $7;
4704 my $ms_size = $12;
4705
4706 if ($ms_size =~ /^(0x|)0$/i) {
4707 ERROR("MEMSET",
4708 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4709 } elsif ($ms_size =~ /^(0x|)1$/i) {
4710 WARN("MEMSET",
4711 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4712 }
4713 }
4714
4715# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4716 if ($^V && $^V ge 5.10.0 &&
4717 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4718 if (WARN("PREFER_ETHER_ADDR_COPY",
4719 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4720 $fix) {
4721 $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
4722 }
4723 }
4724
4725# typecasts on min/max could be min_t/max_t
4726 if ($^V && $^V ge 5.10.0 &&
4727 defined $stat &&
4728 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4729 if (defined $2 || defined $7) {
4730 my $call = $1;
4731 my $cast1 = deparenthesize($2);
4732 my $arg1 = $3;
4733 my $cast2 = deparenthesize($7);
4734 my $arg2 = $8;
4735 my $cast;
4736
4737 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4738 $cast = "$cast1 or $cast2";
4739 } elsif ($cast1 ne "") {
4740 $cast = $cast1;
4741 } else {
4742 $cast = $cast2;
4743 }
4744 WARN("MINMAX",
4745 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4746 }
4747 }
4748
4749# check usleep_range arguments
4750 if ($^V && $^V ge 5.10.0 &&
4751 defined $stat &&
4752 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4753 my $min = $1;
4754 my $max = $7;
4755 if ($min eq $max) {
4756 WARN("USLEEP_RANGE",
4757 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4758 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4759 $min > $max) {
4760 WARN("USLEEP_RANGE",
4761 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4762 }
4763 }
4764
4765# check for naked sscanf
4766 if ($^V && $^V ge 5.10.0 &&
4767 defined $stat &&
4768 $line =~ /\bsscanf\b/ &&
4769 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4770 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4771 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4772 my $lc = $stat =~ tr@\n@@;
4773 $lc = $lc + $linenr;
4774 my $stat_real = raw_line($linenr, 0);
4775 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4776 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4777 }
4778 WARN("NAKED_SSCANF",
4779 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4780 }
4781
4782# check for simple sscanf that should be kstrto<foo>
4783 if ($^V && $^V ge 5.10.0 &&
4784 defined $stat &&
4785 $line =~ /\bsscanf\b/) {
4786 my $lc = $stat =~ tr@\n@@;
4787 $lc = $lc + $linenr;
4788 my $stat_real = raw_line($linenr, 0);
4789 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4790 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4791 }
4792 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4793 my $format = $6;
4794 my $count = $format =~ tr@%@%@;
4795 if ($count == 1 &&
4796 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4797 WARN("SSCANF_TO_KSTRTO",
4798 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4799 }
4800 }
4801 }
4802
4803# check for new externs in .h files.
4804 if ($realfile =~ /\.h$/ &&
4805 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4806 if (CHK("AVOID_EXTERNS",
4807 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
4808 $fix) {
4809 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
4810 }
4811 }
4812
4813# check for new externs in .c files.
4814 if ($realfile =~ /\.c$/ && defined $stat &&
4815 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4816 {
4817 my $function_name = $1;
4818 my $paren_space = $2;
4819
4820 my $s = $stat;
4821 if (defined $cond) {
4822 substr($s, 0, length($cond), '');
4823 }
4824 if ($s =~ /^\s*;/ &&
4825 $function_name ne 'uninitialized_var')
4826 {
4827 WARN("AVOID_EXTERNS",
4828 "externs should be avoided in .c files\n" . $herecurr);
4829 }
4830
4831 if ($paren_space =~ /\n/) {
4832 WARN("FUNCTION_ARGUMENTS",
4833 "arguments for function declarations should follow identifier\n" . $herecurr);
4834 }
4835
4836 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4837 $stat =~ /^.\s*extern\s+/)
4838 {
4839 WARN("AVOID_EXTERNS",
4840 "externs should be avoided in .c files\n" . $herecurr);
4841 }
4842
4843# checks for new __setup's
4844 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4845 my $name = $1;
4846
4847 if (!grep(/$name/, @setup_docs)) {
4848 CHK("UNDOCUMENTED_SETUP",
4849 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4850 }
4851 }
4852
4853# check for pointless casting of kmalloc return
4854 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4855 WARN("UNNECESSARY_CASTS",
4856 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
4857 }
4858
4859# alloc style
4860# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4861 if ($^V && $^V ge 5.10.0 &&
4862 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4863 CHK("ALLOC_SIZEOF_STRUCT",
4864 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4865 }
4866
4867# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
4868 if ($^V && $^V ge 5.10.0 &&
4869 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
4870 my $oldfunc = $3;
4871 my $a1 = $4;
4872 my $a2 = $10;
4873 my $newfunc = "kmalloc_array";
4874 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
4875 my $r1 = $a1;
4876 my $r2 = $a2;
4877 if ($a1 =~ /^sizeof\s*\S/) {
4878 $r1 = $a2;
4879 $r2 = $a1;
4880 }
4881 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4882 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
4883 if (WARN("ALLOC_WITH_MULTIPLY",
4884 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4885 $fix) {
4886 $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;
4887
4888 }
4889 }
4890 }
4891
4892# check for krealloc arg reuse
4893 if ($^V && $^V ge 5.10.0 &&
4894 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4895 WARN("KREALLOC_ARG_REUSE",
4896 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4897 }
4898
4899# check for alloc argument mismatch
4900 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4901 WARN("ALLOC_ARRAY_ARGS",
4902 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4903 }
4904
4905# check for multiple semicolons
4906 if ($line =~ /;\s*;\s*$/) {
4907 if (WARN("ONE_SEMICOLON",
4908 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4909 $fix) {
4910 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
4911 }
4912 }
4913
4914# check for case / default statements not preceded by break/fallthrough/switch
4915 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4916 my $has_break = 0;
4917 my $has_statement = 0;
4918 my $count = 0;
4919 my $prevline = $linenr;
4920 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
4921 $prevline--;
4922 my $rline = $rawlines[$prevline - 1];
4923 my $fline = $lines[$prevline - 1];
4924 last if ($fline =~ /^\@\@/);
4925 next if ($fline =~ /^\-/);
4926 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4927 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4928 next if ($fline =~ /^.[\s$;]*$/);
4929 $has_statement = 1;
4930 $count++;
4931 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4932 }
4933 if (!$has_break && $has_statement) {
4934 WARN("MISSING_BREAK",
4935 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4936 }
4937 }
4938
4939# check for switch/default statements without a break;
4940 if ($^V && $^V ge 5.10.0 &&
4941 defined $stat &&
4942 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4943 my $ctx = '';
4944 my $herectx = $here . "\n";
4945 my $cnt = statement_rawlines($stat);
4946 for (my $n = 0; $n < $cnt; $n++) {
4947 $herectx .= raw_line($linenr, $n) . "\n";
4948 }
4949 WARN("DEFAULT_NO_BREAK",
4950 "switch default: should use break\n" . $herectx);
4951 }
4952
4953# check for gcc specific __FUNCTION__
4954 if ($line =~ /\b__FUNCTION__\b/) {
4955 if (WARN("USE_FUNC",
4956 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4957 $fix) {
4958 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
4959 }
4960 }
4961
4962# check for use of yield()
4963 if ($line =~ /\byield\s*\(\s*\)/) {
4964 WARN("YIELD",
4965 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4966 }
4967
4968# check for comparisons against true and false
4969 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4970 my $lead = $1;
4971 my $arg = $2;
4972 my $test = $3;
4973 my $otype = $4;
4974 my $trail = $5;
4975 my $op = "!";
4976
4977 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4978
4979 my $type = lc($otype);
4980 if ($type =~ /^(?:true|false)$/) {
4981 if (("$test" eq "==" && "$type" eq "true") ||
4982 ("$test" eq "!=" && "$type" eq "false")) {
4983 $op = "";
4984 }
4985
4986 CHK("BOOL_COMPARISON",
4987 "Using comparison to $otype is error prone\n" . $herecurr);
4988
4989## maybe suggesting a correct construct would better
4990## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4991
4992 }
4993 }
4994
4995# check for semaphores initialized locked
4996 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4997 WARN("CONSIDER_COMPLETION",
4998 "consider using a completion\n" . $herecurr);
4999 }
5000
5001# recommend kstrto* over simple_strto* and strict_strto*
5002 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
5003 WARN("CONSIDER_KSTRTO",
5004 "$1 is obsolete, use k$3 instead\n" . $herecurr);
5005 }
5006
5007# check for __initcall(), use device_initcall() explicitly or more appropriate function please
5008 if ($line =~ /^.\s*__initcall\s*\(/) {
5009 WARN("USE_DEVICE_INITCALL",
5010 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
5011 }
5012
5013# check for various ops structs, ensure they are const.
5014 my $struct_ops = qr{acpi_dock_ops|
5015 address_space_operations|
5016 backlight_ops|
5017 block_device_operations|
5018 dentry_operations|
5019 dev_pm_ops|
5020 dma_map_ops|
5021 extent_io_ops|
5022 file_lock_operations|
5023 file_operations|
5024 hv_ops|
5025 ide_dma_ops|
5026 intel_dvo_dev_ops|
5027 item_operations|
5028 iwl_ops|
5029 kgdb_arch|
5030 kgdb_io|
5031 kset_uevent_ops|
5032 lock_manager_operations|
5033 microcode_ops|
5034 mtrr_ops|
5035 neigh_ops|
5036 nlmsvc_binding|
5037 pci_raw_ops|
5038 pipe_buf_operations|
5039 platform_hibernation_ops|
5040 platform_suspend_ops|
5041 proto_ops|
5042 rpc_pipe_ops|
5043 seq_operations|
5044 snd_ac97_build_ops|
5045 soc_pcmcia_socket_ops|
5046 stacktrace_ops|
5047 sysfs_ops|
5048 tty_operations|
5049 usb_mon_operations|
5050 wd_ops}x;
5051 if ($line !~ /\bconst\b/ &&
5052 $line =~ /\bstruct\s+($struct_ops)\b/) {
5053 WARN("CONST_STRUCT",
5054 "struct $1 should normally be const\n" .
5055 $herecurr);
5056 }
5057
5058# use of NR_CPUS is usually wrong
5059# ignore definitions of NR_CPUS and usage to define arrays as likely right
5060 if ($line =~ /\bNR_CPUS\b/ &&
5061 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5062 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5063 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5064 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5065 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5066 {
5067 WARN("NR_CPUS",
5068 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5069 }
5070
5071# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5072 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5073 ERROR("DEFINE_ARCH_HAS",
5074 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5075 }
5076
5077# check for %L{u,d,i} in strings
5078 my $string;
5079 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5080 $string = substr($rawline, $-[1], $+[1] - $-[1]);
5081 $string =~ s/%%/__/g;
5082 if ($string =~ /(?<!%)%L[udi]/) {
5083 WARN("PRINTF_L",
5084 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5085 last;
5086 }
5087 }
5088
5089# whine mightly about in_atomic
5090 if ($line =~ /\bin_atomic\s*\(/) {
5091 if ($realfile =~ m@^drivers/@) {
5092 ERROR("IN_ATOMIC",
5093 "do not use in_atomic in drivers\n" . $herecurr);
5094 } elsif ($realfile !~ m@^kernel/@) {
5095 WARN("IN_ATOMIC",
5096 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5097 }
5098 }
5099
5100# check for lockdep_set_novalidate_class
5101 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5102 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5103 if ($realfile !~ m@^kernel/lockdep@ &&
5104 $realfile !~ m@^include/linux/lockdep@ &&
5105 $realfile !~ m@^drivers/base/core@) {
5106 ERROR("LOCKDEP",
5107 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
5108 }
5109 }
5110
5111 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5112 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
5113 WARN("EXPORTED_WORLD_WRITABLE",
5114 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
5115 }
5116
5117# Mode permission misuses where it seems decimal should be octal
5118# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5119 if ($^V && $^V ge 5.10.0 &&
5120 $line =~ /$mode_perms_search/) {
5121 foreach my $entry (@mode_permission_funcs) {
5122 my $func = $entry->[0];
5123 my $arg_pos = $entry->[1];
5124
5125 my $skip_args = "";
5126 if ($arg_pos > 1) {
5127 $arg_pos--;
5128 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5129 }
5130 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5131 if ($line =~ /$test/) {
5132 my $val = $1;
5133 $val = $6 if ($skip_args ne "");
5134
5135 if ($val !~ /^0$/ &&
5136 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5137 length($val) ne 4)) {
5138 ERROR("NON_OCTAL_PERMISSIONS",
5139 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5140 }
5141 }
5142 }
5143 }
5144 }
5145
5146 # If we have no input at all, then there is nothing to report on
5147 # so just keep quiet.
5148 if ($#rawlines == -1) {
5149 exit(0);
5150 }
5151
5152 # In mailback mode only produce a report in the negative, for
5153 # things that appear to be patches.
5154 if ($mailback && ($clean == 1 || !$is_patch)) {
5155 exit(0);
5156 }
5157
5158 # This is not a patch, and we are are in 'no-patch' mode so
5159 # just keep quiet.
5160 if (!$chk_patch && !$is_patch) {
5161 exit(0);
5162 }
5163
5164 if (!$is_patch) {
5165 ERROR("NOT_UNIFIED_DIFF",
5166 "Does not appear to be a unified-diff format patch\n");
5167 }
5168 if ($is_patch && $chk_signoff && $signoff == 0) {
5169 ERROR("MISSING_SIGN_OFF",
5170 "Missing Signed-off-by: line(s)\n");
5171 }
5172
5173 print report_dump();
5174 if ($summary && !($clean == 1 && $quiet == 1)) {
5175 print "$filename " if ($summary_file);
5176 print "total: $cnt_error errors, $cnt_warn warnings, " .
5177 (($check)? "$cnt_chk checks, " : "") .
5178 "$cnt_lines lines checked\n";
5179 print "\n" if ($quiet == 0);
5180 }
5181
5182 if ($quiet == 0) {
5183
5184 if ($^V lt 5.10.0) {
5185 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5186 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5187 }
5188
5189 # If there were whitespace errors which cleanpatch can fix
5190 # then suggest that.
5191 if ($rpt_cleaners) {
5192 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5193 print " scripts/cleanfile\n\n";
5194 $rpt_cleaners = 0;
5195 }
5196 }
5197
5198 hash_show_words(\%use_type, "Used");
5199 hash_show_words(\%ignore_type, "Ignored");
5200
5201 if ($clean == 0 && $fix &&
5202 ("@rawlines" ne "@fixed" ||
5203 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
5204 my $newfile = $filename;
5205 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
5206 my $linecount = 0;
5207 my $f;
5208
5209 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5210
5211 open($f, '>', $newfile)
5212 or die "$P: Can't open $newfile for write\n";
5213 foreach my $fixed_line (@fixed) {
5214 $linecount++;
5215 if ($file) {
5216 if ($linecount > 3) {
5217 $fixed_line =~ s/^\+//;
5218 print $f $fixed_line . "\n";
5219 }
5220 } else {
5221 print $f $fixed_line . "\n";
5222 }
5223 }
5224 close($f);
5225
5226 if (!$quiet) {
5227 print << "EOM";
5228Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5229
5230Do _NOT_ trust the results written to this file.
5231Do _NOT_ submit these changes without inspecting them for correctness.
5232
5233This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5234No warranties, expressed or implied...
5235
5236EOM
5237 }
5238 }
5239
5240 if ($clean == 1 && $quiet == 0) {
5241 print "$vname has no obvious style problems and is ready for submission.\n"
5242 }
5243 if ($clean == 0 && $quiet == 0) {
5244 print << "EOM";
5245$vname has style problems, please review.
5246
5247If any of these errors are false positives, please report
5248them to the maintainer, see CHECKPATCH in MAINTAINERS.
5249EOM
5250 }
5251
5252 return $clean;
5253}
diff --git a/scripts/spelling.txt b/scripts/spelling.txt
new file mode 100644
index 00000000..fc7fd52b
--- /dev/null
+++ b/scripts/spelling.txt
@@ -0,0 +1,1042 @@
1# Originally from Debian's Lintian tool. Various false positives have been
2# removed, and various additions have been made as they've been discovered
3# in the kernel source.
4#
5# License: GPLv2
6#
7# The format of each line is:
8# mistake||correction
9#
10abandonning||abandoning
11abigious||ambiguous
12abitrate||arbitrate
13abov||above
14abreviated||abbreviated
15absense||absence
16absolut||absolute
17absoulte||absolute
18acccess||access
19acceleratoin||acceleration
20accelleration||acceleration
21accesing||accessing
22accesnt||accent
23accessable||accessible
24accesss||access
25accidentaly||accidentally
26accidentually||accidentally
27accoding||according
28accomodate||accommodate
29accomodates||accommodates
30accordign||according
31accoring||according
32accout||account
33accquire||acquire
34accquired||acquired
35acessable||accessible
36acess||access
37achitecture||architecture
38acient||ancient
39acitions||actions
40acitve||active
41acknowldegement||acknowldegement
42acknowledgement||acknowledgment
43ackowledge||acknowledge
44ackowledged||acknowledged
45acording||according
46activete||activate
47acumulating||accumulating
48adapater||adapter
49addional||additional
50additionaly||additionally
51addres||address
52addreses||addresses
53addresss||address
54aditional||additional
55aditionally||additionally
56aditionaly||additionally
57adminstrative||administrative
58adress||address
59adresses||addresses
60adviced||advised
61afecting||affecting
62agaist||against
63albumns||albums
64alegorical||allegorical
65algorith||algorithm
66algorithmical||algorithmically
67algoritm||algorithm
68algoritms||algorithms
69algorrithm||algorithm
70algorritm||algorithm
71allign||align
72allocatrd||allocated
73allocte||allocate
74allpication||application
75alocate||allocate
76alogirhtms||algorithms
77alogrithm||algorithm
78alot||a lot
79alow||allow
80alows||allows
81altough||although
82alue||value
83ambigious||ambiguous
84amoung||among
85amout||amount
86analysator||analyzer
87ang||and
88anniversery||anniversary
89annoucement||announcement
90anomolies||anomalies
91anomoly||anomaly
92anway||anyway
93aplication||application
94appearence||appearance
95applicaion||application
96appliction||application
97applictions||applications
98appplications||applications
99appropiate||appropriate
100appropriatly||appropriately
101approriate||appropriate
102approriately||appropriately
103aquainted||acquainted
104aquired||acquired
105arbitary||arbitrary
106architechture||architecture
107arguement||argument
108arguements||arguments
109aritmetic||arithmetic
110arne't||aren't
111arraival||arrival
112artifical||artificial
113artillary||artillery
114assiged||assigned
115assigment||assignment
116assigments||assignments
117assistent||assistant
118assocation||association
119associcated||associated
120assotiated||associated
121assum||assume
122assumtpion||assumption
123asuming||assuming
124asycronous||asynchronous
125asynchnous||asynchronous
126atomatically||automatically
127atomicly||atomically
128attachement||attachment
129attched||attached
130attemps||attempts
131attruibutes||attributes
132authentification||authentication
133automaticaly||automatically
134automaticly||automatically
135automatize||automate
136automatized||automated
137automatizes||automates
138autonymous||autonomous
139auxilliary||auxiliary
140avaiable||available
141avaible||available
142availabe||available
143availabled||available
144availablity||availability
145availale||available
146availavility||availability
147availble||available
148availiable||available
149avalable||available
150avaliable||available
151aysnc||async
152backgroud||background
153backword||backward
154backwords||backwards
155bahavior||behavior
156bakup||backup
157baloon||balloon
158baloons||balloons
159bandwith||bandwidth
160batery||battery
161beacuse||because
162becasue||because
163becomming||becoming
164becuase||because
165beeing||being
166befor||before
167begining||beginning
168beter||better
169betweeen||between
170bianries||binaries
171bitmast||bitmask
172boardcast||broadcast
173borad||board
174boundry||boundary
175brievely||briefly
176broadcat||broadcast
177cacluated||calculated
178caculation||calculation
179calender||calendar
180calle||called
181calucate||calculate
182calulate||calculate
183cancelation||cancellation
184capabilites||capabilities
185capabitilies||capabilities
186capatibilities||capabilities
187carefuly||carefully
188cariage||carriage
189catagory||category
190challange||challenge
191challanges||challenges
192chanell||channel
193changable||changeable
194channle||channel
195channnel||channel
196charachter||character
197charachters||characters
198charactor||character
199charater||character
200charaters||characters
201charcter||character
202checksuming||checksumming
203childern||children
204childs||children
205chiled||child
206chked||checked
207chnage||change
208chnages||changes
209chnnel||channel
210choosen||chosen
211chouse||chose
212circumvernt||circumvent
213claread||cleared
214clared||cleared
215closeing||closing
216clustred||clustered
217collapsable||collapsible
218colorfull||colorful
219comand||command
220comit||commit
221commerical||commercial
222comming||coming
223comminucation||communication
224commited||committed
225commiting||committing
226committ||commit
227commoditiy||commodity
228compability||compatibility
229compaibility||compatibility
230compatability||compatibility
231compatable||compatible
232compatibiliy||compatibility
233compatibilty||compatibility
234compilant||compliant
235compleatly||completely
236completly||completely
237complient||compliant
238componnents||components
239compres||compress
240compresion||compression
241comression||compression
242comunication||communication
243conbination||combination
244conditionaly||conditionally
245conected||connected
246configuratoin||configuration
247configuraton||configuration
248configuretion||configuration
249conider||consider
250conjuction||conjunction
251connectinos||connections
252connnection||connection
253connnections||connections
254consistancy||consistency
255consistant||consistent
256containes||contains
257containts||contains
258contaisn||contains
259contant||contact
260contence||contents
261continous||continuous
262continously||continuously
263continueing||continuing
264contraints||constraints
265controled||controlled
266controler||controller
267controll||control
268contruction||construction
269contry||country
270convertion||conversion
271convertor||converter
272convienient||convenient
273convinient||convenient
274corected||corrected
275correponding||corresponding
276correponds||corresponds
277correspoding||corresponding
278cotrol||control
279couter||counter
280coutner||counter
281cryptocraphic||cryptographic
282cunter||counter
283curently||currently
284dafault||default
285deafult||default
286deamon||daemon
287decompres||decompress
288decription||description
289defailt||default
290defferred||deferred
291definate||definite
292definately||definitely
293defintion||definition
294defualt||default
295defult||default
296deivce||device
297delared||declared
298delare||declare
299delares||declares
300delaring||declaring
301delemiter||delimiter
302dependancies||dependencies
303dependancy||dependency
304dependant||dependent
305depreacted||deprecated
306depreacte||deprecate
307desactivate||deactivate
308desciptors||descriptors
309descrition||description
310descritptor||descriptor
311desctiptor||descriptor
312desriptor||descriptor
313desriptors||descriptors
314destory||destroy
315destoryed||destroyed
316destorys||destroys
317destroied||destroyed
318detabase||database
319develope||develop
320developement||development
321developped||developed
322developpement||development
323developper||developer
324developpment||development
325deveolpment||development
326devided||divided
327deviece||device
328diable||disable
329dictionnary||dictionary
330diferent||different
331differrence||difference
332difinition||definition
333diplay||display
334direectly||directly
335disapear||disappear
336disapeared||disappeared
337disappared||disappeared
338disconnet||disconnect
339discontinous||discontinuous
340dispertion||dispersion
341dissapears||disappears
342distiction||distinction
343docuentation||documentation
344documantation||documentation
345documentaion||documentation
346documment||document
347dorp||drop
348dosen||doesn
349downlad||download
350downlads||downloads
351druing||during
352dynmaic||dynamic
353easilly||easily
354ecspecially||especially
355edditable||editable
356editting||editing
357efficently||efficiently
358ehther||ether
359eigth||eight
360eletronic||electronic
361enabledi||enabled
362enchanced||enhanced
363encorporating||incorporating
364encrupted||encrypted
365encrypiton||encryption
366endianess||endianness
367enhaced||enhanced
368enlightnment||enlightenment
369enocded||encoded
370enterily||entirely
371enviroiment||environment
372enviroment||environment
373environement||environment
374environent||environment
375eqivalent||equivalent
376equiped||equipped
377equivelant||equivalent
378equivilant||equivalent
379eror||error
380estbalishment||establishment
381etsablishment||establishment
382etsbalishment||establishment
383excecutable||executable
384exceded||exceeded
385excellant||excellent
386existance||existence
387existant||existent
388exixt||exist
389exlcude||exclude
390exlcusive||exclusive
391exmaple||example
392expecially||especially
393explicite||explicit
394explicitely||explicitly
395explict||explicit
396explictly||explicitly
397expresion||expression
398exprimental||experimental
399extened||extended
400extensability||extensibility
401extention||extension
402extracter||extractor
403faild||failed
404faill||fail
405failue||failure
406failuer||failure
407faireness||fairness
408faliure||failure
409familar||familiar
410fatser||faster
411feauture||feature
412feautures||features
413fetaure||feature
414fetaures||features
415fileystem||filesystem
416finanize||finalize
417findn||find
418finilizes||finalizes
419finsih||finish
420flusing||flushing
421folloing||following
422followign||following
423follwing||following
424forseeable||foreseeable
425forse||force
426fortan||fortran
427forwardig||forwarding
428framwork||framework
429frequncy||frequency
430frome||from
431fucntion||function
432fuction||function
433fuctions||functions
434funcion||function
435functionallity||functionality
436functionaly||functionally
437functionnality||functionality
438functonality||functionality
439funtion||function
440funtions||functions
441furthur||further
442futhermore||furthermore
443futrue||future
444gaurenteed||guaranteed
445generiously||generously
446genric||generic
447globel||global
448grabing||grabbing
449grahical||graphical
450grahpical||graphical
451grapic||graphic
452guage||gauge
453guarentee||guarantee
454halfs||halves
455hander||handler
456handfull||handful
457hanled||handled
458harware||hardware
459heirarchically||hierarchically
460helpfull||helpful
461hierachy||hierarchy
462hierarchie||hierarchy
463howver||however
464hsould||should
465hypter||hyper
466identidier||identifier
467imblance||imbalance
468immeadiately||immediately
469immedaite||immediate
470immediatelly||immediately
471immediatly||immediately
472immidiate||immediate
473impelentation||implementation
474impementated||implemented
475implemantation||implementation
476implemenation||implementation
477implementaiton||implementation
478implementated||implemented
479implemention||implementation
480implemetation||implementation
481implemntation||implementation
482implentation||implementation
483implmentation||implementation
484implmenting||implementing
485incomming||incoming
486incompatabilities||incompatibilities
487incompatable||incompatible
488inconsistant||inconsistent
489increas||increase
490incrment||increment
491indendation||indentation
492indended||intended
493independant||independent
494independantly||independently
495independed||independent
496indiate||indicate
497inexpect||inexpected
498infomation||information
499informatiom||information
500informations||information
501informtion||information
502infromation||information
503ingore||ignore
504inital||initial
505initalised||initialized
506initalise||initialize
507initalize||initialize
508initation||initiation
509initators||initiators
510initializiation||initialization
511initialzed||initialized
512initilization||initialization
513initilize||initialize
514inofficial||unofficial
515instal||install
516inteface||interface
517integreated||integrated
518integrety||integrity
519integrey||integrity
520intendet||intended
521intented||intended
522interanl||internal
523interchangable||interchangeable
524interferring||interfering
525interger||integer
526intermittant||intermittent
527internel||internal
528interoprability||interoperability
529interrface||interface
530interrrupt||interrupt
531interrup||interrupt
532interrups||interrupts
533interruptted||interrupted
534interupted||interrupted
535interupt||interrupt
536intial||initial
537intialized||initialized
538intialize||initialize
539intregral||integral
540intrrupt||interrupt
541intuative||intuitive
542invaid||invalid
543invalde||invald
544invalide||invalid
545invididual||individual
546invokation||invocation
547invokations||invocations
548irrelevent||irrelevant
549isssue||issue
550itslef||itself
551jave||java
552jeffies||jiffies
553juse||just
554jus||just
555kown||known
556langage||language
557langauage||language
558langauge||language
559langugage||language
560lauch||launch
561leightweight||lightweight
562lengh||length
563lenght||length
564lenth||length
565lesstiff||lesstif
566libaries||libraries
567libary||library
568librairies||libraries
569libraris||libraries
570licenceing||licencing
571loggging||logging
572loggin||login
573logile||logfile
574loosing||losing
575losted||lost
576machinary||machinery
577maintainance||maintenance
578maintainence||maintenance
579maintan||maintain
580makeing||making
581malplaced||misplaced
582malplace||misplace
583managable||manageable
584managment||management
585mangement||management
586manoeuvering||maneuvering
587mappping||mapping
588mathimatical||mathematical
589mathimatic||mathematic
590mathimatics||mathematics
591maxium||maximum
592mechamism||mechanism
593meetign||meeting
594ment||meant
595mergable||mergeable
596mesage||message
597messags||messages
598messgaes||messages
599messsage||message
600messsages||messages
601microprocesspr||microprocessor
602milliseonds||milliseconds
603minumum||minimum
604miscelleneous||miscellaneous
605misformed||malformed
606mispelled||misspelled
607mispelt||misspelt
608miximum||maximum
609mmnemonic||mnemonic
610mnay||many
611modeled||modelled
612modulues||modules
613monochorome||monochrome
614monochromo||monochrome
615monocrome||monochrome
616mopdule||module
617mroe||more
618mulitplied||multiplied
619multidimensionnal||multidimensional
620multple||multiple
621mumber||number
622muticast||multicast
623mutiple||multiple
624mutli||multi
625nams||names
626navagating||navigating
627nead||need
628neccecary||necessary
629neccesary||necessary
630neccessary||necessary
631necesary||necessary
632negaive||negative
633negoitation||negotiation
634negotation||negotiation
635nerver||never
636nescessary||necessary
637nessessary||necessary
638noticable||noticeable
639notications||notifications
640notifed||notified
641numebr||number
642numner||number
643obtaion||obtain
644occassionally||occasionally
645occationally||occasionally
646occurance||occurrence
647occurances||occurrences
648occured||occurred
649occurence||occurrence
650occure||occurred
651occuring||occurring
652offet||offset
653omitt||omit
654ommiting||omitting
655ommitted||omitted
656onself||oneself
657ony||only
658operatione||operation
659opertaions||operations
660optionnal||optional
661optmizations||optimizations
662orientatied||orientated
663orientied||oriented
664otherise||otherwise
665ouput||output
666overaall||overall
667overhread||overhead
668overlaping||overlapping
669overriden||overridden
670overun||overrun
671pacakge||package
672pachage||package
673packacge||package
674packege||package
675packge||package
676packtes||packets
677pakage||package
678pallette||palette
679paln||plan
680paramameters||parameters
681paramater||parameter
682parametes||parameters
683parametised||parametrised
684paramter||parameter
685paramters||parameters
686particuarly||particularly
687particularily||particularly
688pased||passed
689passin||passing
690pathes||paths
691pecularities||peculiarities
692peformance||performance
693peice||piece
694pendantic||pedantic
695peprocessor||preprocessor
696perfoming||performing
697permissons||permissions
698peroid||period
699persistance||persistence
700persistant||persistent
701platfrom||platform
702plattform||platform
703pleaes||please
704ploting||plotting
705plugable||pluggable
706poinnter||pointer
707poiter||pointer
708posible||possible
709positon||position
710possibilites||possibilities
711powerfull||powerful
712preceeded||preceded
713preceeding||preceding
714preceed||precede
715precendence||precedence
716precission||precision
717prefered||preferred
718prefferably||preferably
719premption||preemption
720prepaired||prepared
721pressre||pressure
722primative||primitive
723princliple||principle
724priorty||priority
725privilaged||privileged
726privilage||privilege
727priviledge||privilege
728priviledges||privileges
729probaly||probably
730procceed||proceed
731proccesors||processors
732procesed||processed
733proces||process
734processessing||processing
735processess||processes
736processpr||processor
737processsed||processed
738processsing||processing
739procteted||protected
740prodecure||procedure
741progams||programs
742progess||progress
743programers||programmers
744programm||program
745programms||programs
746progresss||progress
747promps||prompts
748pronnounced||pronounced
749prononciation||pronunciation
750pronouce||pronounce
751pronunce||pronounce
752propery||property
753propigate||propagate
754propigation||propagation
755propogate||propagate
756prosess||process
757protable||portable
758protcol||protocol
759protecion||protection
760protocoll||protocol
761psudo||pseudo
762psuedo||pseudo
763psychadelic||psychedelic
764pwoer||power
765quering||querying
766raoming||roaming
767reasearcher||researcher
768reasearchers||researchers
769reasearch||research
770recepient||recipient
771receving||receiving
772recieved||received
773recieve||receive
774reciever||receiver
775recieves||receives
776recogniced||recognised
777recognizeable||recognizable
778recommanded||recommended
779recyle||recycle
780redircet||redirect
781redirectrion||redirection
782refcounf||refcount
783refence||reference
784refered||referred
785referenace||reference
786refering||referring
787refernces||references
788refernnce||reference
789refrence||reference
790registerd||registered
791registeresd||registered
792registes||registers
793registraration||registration
794regster||register
795regualar||regular
796reguator||regulator
797regulamentations||regulations
798reigstration||registration
799releated||related
800relevent||relevant
801remoote||remote
802remore||remote
803removeable||removable
804repectively||respectively
805replacable||replaceable
806replacments||replacements
807replys||replies
808reponse||response
809representaion||representation
810reqeust||request
811requiere||require
812requirment||requirement
813requred||required
814requried||required
815requst||request
816reseting||resetting
817resizeable||resizable
818resouces||resources
819resoures||resources
820ressizes||resizes
821ressource||resource
822ressources||resources
823retransmited||retransmitted
824retreived||retrieved
825retreive||retrieve
826retrive||retrieve
827retuned||returned
828reuest||request
829reuqest||request
830reutnred||returned
831rmeoved||removed
832rmeove||remove
833rmeoves||removes
834rountine||routine
835routins||routines
836rquest||request
837runing||running
838runned||ran
839runnning||running
840runtine||runtime
841sacrifying||sacrificing
842safly||safely
843safty||safety
844savable||saveable
845scaned||scanned
846scaning||scanning
847scarch||search
848seach||search
849searchs||searches
850secquence||sequence
851secund||second
852segement||segment
853senarios||scenarios
854sentivite||sensitive
855separatly||separately
856sepcify||specify
857sepc||spec
858seperated||separated
859seperately||separately
860seperate||separate
861seperatly||separately
862seperator||separator
863sepperate||separate
864sequece||sequence
865sequencial||sequential
866serveral||several
867setts||sets
868settting||setting
869shotdown||shutdown
870shoud||should
871shoule||should
872shrinked||shrunk
873siginificantly||significantly
874signabl||signal
875similary||similarly
876similiar||similar
877simlar||similar
878simliar||similar
879simpified||simplified
880singaled||signaled
881singal||signal
882singed||signed
883sleeped||slept
884softwares||software
885speach||speech
886specfic||specific
887speciefied||specified
888specifc||specific
889specifed||specified
890specificatin||specification
891specificaton||specification
892specifing||specifying
893specifiying||specifying
894speficied||specified
895speicify||specify
896speling||spelling
897spinlcok||spinlock
898spinock||spinlock
899splitted||split
900spreaded||spread
901sructure||structure
902stablilization||stabilization
903staically||statically
904staion||station
905standardss||standards
906standartization||standardization
907standart||standard
908staticly||statically
909stoped||stopped
910stoppped||stopped
911straming||streaming
912struc||struct
913structres||structures
914stuct||struct
915sturcture||structure
916subdirectoires||subdirectories
917suble||subtle
918succesfully||successfully
919succesful||successful
920successfull||successful
921sucessfully||successfully
922sucess||success
923superflous||superfluous
924superseeded||superseded
925suplied||supplied
926suported||supported
927suport||support
928suppored||supported
929supportin||supporting
930suppoted||supported
931suppported||supported
932suppport||support
933supress||suppress
934surpresses||suppresses
935susbsystem||subsystem
936suspicously||suspiciously
937swaping||swapping
938switchs||switches
939symetric||symmetric
940synax||syntax
941synchonized||synchronized
942syncronize||synchronize
943syncronizing||synchronizing
944syncronus||synchronous
945syste||system
946sytem||system
947sythesis||synthesis
948taht||that
949targetted||targeted
950targetting||targeting
951teh||the
952temorary||temporary
953temproarily||temporarily
954thier||their
955threds||threads
956threshhold||threshold
957throught||through
958thses||these
959tiggered||triggered
960tipically||typically
961tmis||this
962torerable||tolerable
963tramsmitted||transmitted
964tramsmit||transmit
965tranfer||transfer
966transciever||transceiver
967transferd||transferrd
968transfered||transferred
969transfering||transferring
970transision||transition
971transmittd||transmitted
972transormed||transformed
973trasmission||transmission
974treshold||threshold
975trigerring||triggering
976trun||turn
977ture||true
978tyep||type
979udpate||update
980uesd||used
981unconditionaly||unconditionally
982underun||underrun
983unecessary||unnecessary
984unexecpted||unexpected
985unexpectd||unexpected
986unexpeted||unexpected
987unfortunatelly||unfortunately
988unifiy||unify
989unknonw||unknown
990unknow||unknown
991unkown||unknown
992unneedingly||unnecessarily
993unresgister||unregister
994unsinged||unsigned
995unstabel||unstable
996unsuccessfull||unsuccessful
997unsuported||unsupported
998untill||until
999unuseful||useless
1000upate||update
1001usefule||useful
1002usefull||useful
1003usege||usage
1004usera||users
1005usualy||usually
1006utilites||utilities
1007utillities||utilities
1008utilties||utilities
1009utiltity||utility
1010utitity||utility
1011utitlty||utility
1012vaid||valid
1013vaild||valid
1014valide||valid
1015variantions||variations
1016varient||variant
1017vaule||value
1018verbse||verbose
1019verisons||versions
1020verison||version
1021verson||version
1022vicefersa||vice-versa
1023virtal||virtual
1024virtaul||virtual
1025virtiual||virtual
1026visiters||visitors
1027vitual||virtual
1028wating||waiting
1029whataver||whatever
1030whenver||whenever
1031wheter||whether
1032whe||when
1033wierd||weird
1034wiil||will
1035wirte||write
1036withing||within
1037wnat||want
1038workarould||workaround
1039writeing||writing
1040writting||writing
1041zombe||zombie
1042zomebie||zombie