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