blob: f1c31c54c08aa25aa104c7d679e3e4ceccf224e4 [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
Julius Werner218906d2021-03-25 21:12:49 -07001200 # Drop the diff line leader
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001201 $line =~ s/^.//;
Julius Werner218906d2021-03-25 21:12:49 -07001202
1203 # Treat labels like whitespace when counting indentation
1204 $line =~ s/^( ?$Ident:)/" " x length($1)/e;
1205
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001206 $line = expand_tabs($line);
1207
1208 # Pick the indent from the front of the line.
1209 my ($white) = ($line =~ /^(\s*)/);
1210
1211 return (length($line), length($white));
1212}
1213
1214my $sanitise_quote = '';
1215
1216sub sanitise_line_reset {
1217 my ($in_comment) = @_;
1218
1219 if ($in_comment) {
1220 $sanitise_quote = '*/';
1221 } else {
1222 $sanitise_quote = '';
1223 }
1224}
1225sub sanitise_line {
1226 my ($line) = @_;
1227
1228 my $res = '';
1229 my $l = '';
1230
1231 my $qlen = 0;
1232 my $off = 0;
1233 my $c;
1234
1235 # Always copy over the diff marker.
1236 $res = substr($line, 0, 1);
1237
1238 for ($off = 1; $off < length($line); $off++) {
1239 $c = substr($line, $off, 1);
1240
Martin Roth60915b32018-08-10 21:04:05 -06001241 # Comments we are whacking completely including the begin
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001242 # and end, all to $;.
1243 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1244 $sanitise_quote = '*/';
1245
1246 substr($res, $off, 2, "$;$;");
1247 $off++;
1248 next;
1249 }
1250 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1251 $sanitise_quote = '';
1252 substr($res, $off, 2, "$;$;");
1253 $off++;
1254 next;
1255 }
1256 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1257 $sanitise_quote = '//';
1258
1259 substr($res, $off, 2, $sanitise_quote);
1260 $off++;
1261 next;
1262 }
1263
1264 # A \ in a string means ignore the next character.
1265 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1266 $c eq "\\") {
1267 substr($res, $off, 2, 'XX');
1268 $off++;
1269 next;
1270 }
1271 # Regular quotes.
1272 if ($c eq "'" || $c eq '"') {
1273 if ($sanitise_quote eq '') {
1274 $sanitise_quote = $c;
1275
1276 substr($res, $off, 1, $c);
1277 next;
1278 } elsif ($sanitise_quote eq $c) {
1279 $sanitise_quote = '';
1280 }
1281 }
1282
1283 #print "c<$c> SQ<$sanitise_quote>\n";
1284 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1285 substr($res, $off, 1, $;);
1286 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1287 substr($res, $off, 1, $;);
1288 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1289 substr($res, $off, 1, 'X');
1290 } else {
1291 substr($res, $off, 1, $c);
1292 }
1293 }
1294
1295 if ($sanitise_quote eq '//') {
1296 $sanitise_quote = '';
1297 }
1298
1299 # The pathname on a #include may be surrounded by '<' and '>'.
1300 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1301 my $clean = 'X' x length($1);
1302 $res =~ s@\<.*\>@<$clean>@;
1303
1304 # The whole of a #error is a string.
1305 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1306 my $clean = 'X' x length($1);
1307 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1308 }
1309
Martin Rothedd591d2017-03-14 10:16:29 -06001310 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1311 my $match = $1;
1312 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1313 }
1314
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001315 return $res;
1316}
1317
1318sub get_quoted_string {
1319 my ($line, $rawline) = @_;
1320
Martin Roth60915b32018-08-10 21:04:05 -06001321 return "" if (!defined($line) || !defined($rawline));
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001322 return "" if ($line !~ m/($String)/g);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001323 return substr($rawline, $-[0], $+[0] - $-[0]);
1324}
1325
1326sub ctx_statement_block {
1327 my ($linenr, $remain, $off) = @_;
1328 my $line = $linenr - 1;
1329 my $blk = '';
1330 my $soff = $off;
1331 my $coff = $off - 1;
1332 my $coff_set = 0;
1333
1334 my $loff = 0;
1335
1336 my $type = '';
1337 my $level = 0;
Julius Werner218906d2021-03-25 21:12:49 -07001338 my @stack = (['', $level]);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001339 my $p;
1340 my $c;
1341 my $len = 0;
1342
1343 my $remainder;
1344 while (1) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001345 #warn "CSB: blk<$blk> remain<$remain>\n";
1346 # If we are about to drop off the end, pull in more
1347 # context.
1348 if ($off >= $len) {
1349 for (; $remain > 0; $line++) {
1350 last if (!defined $lines[$line]);
1351 next if ($lines[$line] =~ /^-/);
1352 $remain--;
1353 $loff = $len;
1354 $blk .= $lines[$line] . "\n";
1355 $len = length($blk);
1356 $line++;
1357 last;
1358 }
1359 # Bail if there is no further context.
1360 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1361 if ($off >= $len) {
1362 last;
1363 }
1364 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1365 $level++;
1366 $type = '#';
1367 }
1368 }
1369 $p = $c;
1370 $c = substr($blk, $off, 1);
1371 $remainder = substr($blk, $off);
1372
1373 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1374
1375 # Handle nested #if/#else.
1376 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1377 push(@stack, [ $type, $level ]);
Julius Werner218906d2021-03-25 21:12:49 -07001378 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/ && $#stack > 0) {
1379 ($type, $level) = @{$stack[$#stack]};
1380 } elsif ($remainder =~ /^#\s*endif\b/ && $#stack > 0) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001381 ($type, $level) = @{pop(@stack)};
1382 }
1383
1384 # Statement ends at the ';' or a close '}' at the
1385 # outermost level.
1386 if ($level == 0 && $c eq ';') {
1387 last;
1388 }
1389
1390 # An else is really a conditional as long as its not else if
1391 if ($level == 0 && $coff_set == 0 &&
1392 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1393 $remainder =~ /^(else)(?:\s|{)/ &&
1394 $remainder !~ /^else\s+if\b/) {
1395 $coff = $off + length($1) - 1;
1396 $coff_set = 1;
1397 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1398 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1399 }
1400
1401 if (($type eq '' || $type eq '(') && $c eq '(') {
1402 $level++;
1403 $type = '(';
1404 }
1405 if ($type eq '(' && $c eq ')') {
1406 $level--;
1407 $type = ($level != 0)? '(' : '';
1408
1409 if ($level == 0 && $coff < $soff) {
1410 $coff = $off;
1411 $coff_set = 1;
1412 #warn "CSB: mark coff<$coff>\n";
1413 }
1414 }
1415 if (($type eq '' || $type eq '{') && $c eq '{') {
1416 $level++;
1417 $type = '{';
1418 }
1419 if ($type eq '{' && $c eq '}') {
1420 $level--;
1421 $type = ($level != 0)? '{' : '';
1422
1423 if ($level == 0) {
1424 if (substr($blk, $off + 1, 1) eq ';') {
1425 $off++;
1426 }
1427 last;
1428 }
1429 }
1430 # Preprocessor commands end at the newline unless escaped.
1431 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1432 $level--;
1433 $type = '';
1434 $off++;
1435 last;
1436 }
1437 $off++;
1438 }
1439 # We are truly at the end, so shuffle to the next line.
1440 if ($off == $len) {
1441 $loff = $len + 1;
1442 $line++;
1443 $remain--;
1444 }
1445
1446 my $statement = substr($blk, $soff, $off - $soff + 1);
1447 my $condition = substr($blk, $soff, $coff - $soff + 1);
1448
1449 #warn "STATEMENT<$statement>\n";
1450 #warn "CONDITION<$condition>\n";
1451
1452 #print "coff<$coff> soff<$off> loff<$loff>\n";
1453
1454 return ($statement, $condition,
1455 $line, $remain + 1, $off - $loff + 1, $level);
1456}
1457
1458sub statement_lines {
1459 my ($stmt) = @_;
1460
1461 # Strip the diff line prefixes and rip blank lines at start and end.
1462 $stmt =~ s/(^|\n)./$1/g;
1463 $stmt =~ s/^\s*//;
1464 $stmt =~ s/\s*$//;
1465
1466 my @stmt_lines = ($stmt =~ /\n/g);
1467
1468 return $#stmt_lines + 2;
1469}
1470
1471sub statement_rawlines {
1472 my ($stmt) = @_;
1473
1474 my @stmt_lines = ($stmt =~ /\n/g);
1475
1476 return $#stmt_lines + 2;
1477}
1478
1479sub statement_block_size {
1480 my ($stmt) = @_;
1481
1482 $stmt =~ s/(^|\n)./$1/g;
1483 $stmt =~ s/^\s*{//;
1484 $stmt =~ s/}\s*$//;
1485 $stmt =~ s/^\s*//;
1486 $stmt =~ s/\s*$//;
1487
1488 my @stmt_lines = ($stmt =~ /\n/g);
1489 my @stmt_statements = ($stmt =~ /;/g);
1490
1491 my $stmt_lines = $#stmt_lines + 2;
1492 my $stmt_statements = $#stmt_statements + 1;
1493
1494 if ($stmt_lines > $stmt_statements) {
1495 return $stmt_lines;
1496 } else {
1497 return $stmt_statements;
1498 }
1499}
1500
1501sub ctx_statement_full {
1502 my ($linenr, $remain, $off) = @_;
1503 my ($statement, $condition, $level);
1504
1505 my (@chunks);
1506
1507 # Grab the first conditional/block pair.
1508 ($statement, $condition, $linenr, $remain, $off, $level) =
1509 ctx_statement_block($linenr, $remain, $off);
1510 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1511 push(@chunks, [ $condition, $statement ]);
1512 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1513 return ($level, $linenr, @chunks);
1514 }
1515
1516 # Pull in the following conditional/block pairs and see if they
1517 # could continue the statement.
1518 for (;;) {
1519 ($statement, $condition, $linenr, $remain, $off, $level) =
1520 ctx_statement_block($linenr, $remain, $off);
1521 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1522 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1523 #print "C: push\n";
1524 push(@chunks, [ $condition, $statement ]);
1525 }
1526
1527 return ($level, $linenr, @chunks);
1528}
1529
1530sub ctx_block_get {
1531 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1532 my $line;
1533 my $start = $linenr - 1;
1534 my $blk = '';
1535 my @o;
1536 my @c;
1537 my @res = ();
1538
1539 my $level = 0;
1540 my @stack = ($level);
1541 for ($line = $start; $remain > 0; $line++) {
1542 next if ($rawlines[$line] =~ /^-/);
1543 $remain--;
1544
1545 $blk .= $rawlines[$line];
1546
1547 # Handle nested #if/#else.
1548 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1549 push(@stack, $level);
Julius Werner218906d2021-03-25 21:12:49 -07001550 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/ && $#stack > 0) {
1551 $level = $stack[$#stack];
1552 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/ && $#stack > 0) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001553 $level = pop(@stack);
1554 }
1555
1556 foreach my $c (split(//, $lines[$line])) {
1557 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1558 if ($off > 0) {
1559 $off--;
1560 next;
1561 }
1562
1563 if ($c eq $close && $level > 0) {
1564 $level--;
1565 last if ($level == 0);
1566 } elsif ($c eq $open) {
1567 $level++;
1568 }
1569 }
1570
1571 if (!$outer || $level <= 1) {
1572 push(@res, $rawlines[$line]);
1573 }
1574
1575 last if ($level == 0);
1576 }
1577
1578 return ($level, @res);
1579}
1580sub ctx_block_outer {
1581 my ($linenr, $remain) = @_;
1582
1583 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1584 return @r;
1585}
1586sub ctx_block {
1587 my ($linenr, $remain) = @_;
1588
1589 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1590 return @r;
1591}
1592sub ctx_statement {
1593 my ($linenr, $remain, $off) = @_;
1594
1595 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1596 return @r;
1597}
1598sub ctx_block_level {
1599 my ($linenr, $remain) = @_;
1600
1601 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1602}
1603sub ctx_statement_level {
1604 my ($linenr, $remain, $off) = @_;
1605
1606 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1607}
1608
1609sub ctx_locate_comment {
1610 my ($first_line, $end_line) = @_;
1611
1612 # Catch a comment on the end of the line itself.
1613 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1614 return $current_comment if (defined $current_comment);
1615
1616 # Look through the context and try and figure out if there is a
1617 # comment.
1618 my $in_comment = 0;
1619 $current_comment = '';
1620 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1621 my $line = $rawlines[$linenr - 1];
1622 #warn " $line\n";
1623 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1624 $in_comment = 1;
1625 }
1626 if ($line =~ m@/\*@) {
1627 $in_comment = 1;
1628 }
1629 if (!$in_comment && $current_comment ne '') {
1630 $current_comment = '';
1631 }
1632 $current_comment .= $line . "\n" if ($in_comment);
1633 if ($line =~ m@\*/@) {
1634 $in_comment = 0;
1635 }
1636 }
1637
1638 chomp($current_comment);
1639 return($current_comment);
1640}
1641sub ctx_has_comment {
1642 my ($first_line, $end_line) = @_;
1643 my $cmt = ctx_locate_comment($first_line, $end_line);
1644
1645 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1646 ##print "CMMT: $cmt\n";
1647
1648 return ($cmt ne '');
1649}
1650
1651sub raw_line {
1652 my ($linenr, $cnt) = @_;
1653
1654 my $offset = $linenr - 1;
1655 $cnt++;
1656
1657 my $line;
1658 while ($cnt) {
1659 $line = $rawlines[$offset++];
1660 next if (defined($line) && $line =~ /^-/);
1661 $cnt--;
1662 }
1663
Martin Roth96a48f12016-08-29 15:30:23 -06001664 # coreboot: This probably shouldn't happen, but it does.
1665 # Return a defined value so we don't get an error.
1666 if (defined $line) {
1667 return $line;
1668 } else {
1669 return "";
1670 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001671}
1672
Martin Roth60915b32018-08-10 21:04:05 -06001673sub get_stat_real {
1674 my ($linenr, $lc) = @_;
1675
1676 my $stat_real = raw_line($linenr, 0);
1677 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1678 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1679 }
1680
1681 return $stat_real;
1682}
1683
1684sub get_stat_here {
1685 my ($linenr, $cnt, $here) = @_;
1686
1687 my $herectx = $here . "\n";
1688 for (my $n = 0; $n < $cnt; $n++) {
1689 $herectx .= raw_line($linenr, $n) . "\n";
1690 }
1691
1692 return $herectx;
1693}
1694
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001695sub cat_vet {
1696 my ($vet) = @_;
1697 my ($res, $coded);
1698
1699 $res = '';
1700 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1701 $res .= $1;
1702 if ($2 ne '') {
1703 $coded = sprintf("^%c", unpack('C', $2) + 64);
1704 $res .= $coded;
1705 }
1706 }
1707 $res =~ s/$/\$/;
1708
1709 return $res;
1710}
1711
1712my $av_preprocessor = 0;
1713my $av_pending;
1714my @av_paren_type;
1715my $av_pend_colon;
1716
1717sub annotate_reset {
1718 $av_preprocessor = 0;
1719 $av_pending = '_';
1720 @av_paren_type = ('E');
1721 $av_pend_colon = 'O';
1722}
1723
1724sub annotate_values {
1725 my ($stream, $type) = @_;
1726
1727 my $res;
1728 my $var = '_' x length($stream);
1729 my $cur = $stream;
1730
1731 print "$stream\n" if ($dbg_values > 1);
1732
1733 while (length($cur)) {
1734 @av_paren_type = ('E') if ($#av_paren_type < 0);
1735 print " <" . join('', @av_paren_type) .
1736 "> <$type> <$av_pending>" if ($dbg_values > 1);
1737 if ($cur =~ /^(\s+)/o) {
1738 print "WS($1)\n" if ($dbg_values > 1);
1739 if ($1 =~ /\n/ && $av_preprocessor) {
1740 $type = pop(@av_paren_type);
1741 $av_preprocessor = 0;
1742 }
1743
1744 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1745 print "CAST($1)\n" if ($dbg_values > 1);
1746 push(@av_paren_type, $type);
1747 $type = 'c';
1748
1749 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1750 print "DECLARE($1)\n" if ($dbg_values > 1);
1751 $type = 'T';
1752
1753 } elsif ($cur =~ /^($Modifier)\s*/) {
1754 print "MODIFIER($1)\n" if ($dbg_values > 1);
1755 $type = 'T';
1756
1757 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1758 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1759 $av_preprocessor = 1;
1760 push(@av_paren_type, $type);
1761 if ($2 ne '') {
1762 $av_pending = 'N';
1763 }
1764 $type = 'E';
1765
1766 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1767 print "UNDEF($1)\n" if ($dbg_values > 1);
1768 $av_preprocessor = 1;
1769 push(@av_paren_type, $type);
1770
1771 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1772 print "PRE_START($1)\n" if ($dbg_values > 1);
1773 $av_preprocessor = 1;
1774
1775 push(@av_paren_type, $type);
1776 push(@av_paren_type, $type);
1777 $type = 'E';
1778
1779 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1780 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1781 $av_preprocessor = 1;
1782
1783 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1784
1785 $type = 'E';
1786
1787 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1788 print "PRE_END($1)\n" if ($dbg_values > 1);
1789
1790 $av_preprocessor = 1;
1791
1792 # Assume all arms of the conditional end as this
1793 # one does, and continue as if the #endif was not here.
1794 pop(@av_paren_type);
1795 push(@av_paren_type, $type);
1796 $type = 'E';
1797
1798 } elsif ($cur =~ /^(\\\n)/o) {
1799 print "PRECONT($1)\n" if ($dbg_values > 1);
1800
1801 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1802 print "ATTR($1)\n" if ($dbg_values > 1);
1803 $av_pending = $type;
1804 $type = 'N';
1805
1806 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1807 print "SIZEOF($1)\n" if ($dbg_values > 1);
1808 if (defined $2) {
1809 $av_pending = 'V';
1810 }
1811 $type = 'N';
1812
1813 } elsif ($cur =~ /^(if|while|for)\b/o) {
1814 print "COND($1)\n" if ($dbg_values > 1);
1815 $av_pending = 'E';
1816 $type = 'N';
1817
1818 } elsif ($cur =~/^(case)/o) {
1819 print "CASE($1)\n" if ($dbg_values > 1);
1820 $av_pend_colon = 'C';
1821 $type = 'N';
1822
1823 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1824 print "KEYWORD($1)\n" if ($dbg_values > 1);
1825 $type = 'N';
1826
1827 } elsif ($cur =~ /^(\()/o) {
1828 print "PAREN('$1')\n" if ($dbg_values > 1);
1829 push(@av_paren_type, $av_pending);
1830 $av_pending = '_';
1831 $type = 'N';
1832
1833 } elsif ($cur =~ /^(\))/o) {
1834 my $new_type = pop(@av_paren_type);
1835 if ($new_type ne '_') {
1836 $type = $new_type;
1837 print "PAREN('$1') -> $type\n"
1838 if ($dbg_values > 1);
1839 } else {
1840 print "PAREN('$1')\n" if ($dbg_values > 1);
1841 }
1842
1843 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1844 print "FUNC($1)\n" if ($dbg_values > 1);
1845 $type = 'V';
1846 $av_pending = 'V';
1847
1848 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1849 if (defined $2 && $type eq 'C' || $type eq 'T') {
1850 $av_pend_colon = 'B';
1851 } elsif ($type eq 'E') {
1852 $av_pend_colon = 'L';
1853 }
1854 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1855 $type = 'V';
1856
1857 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1858 print "IDENT($1)\n" if ($dbg_values > 1);
1859 $type = 'V';
1860
1861 } elsif ($cur =~ /^($Assignment)/o) {
1862 print "ASSIGN($1)\n" if ($dbg_values > 1);
1863 $type = 'N';
1864
1865 } elsif ($cur =~/^(;|{|})/) {
1866 print "END($1)\n" if ($dbg_values > 1);
1867 $type = 'E';
1868 $av_pend_colon = 'O';
1869
1870 } elsif ($cur =~/^(,)/) {
1871 print "COMMA($1)\n" if ($dbg_values > 1);
1872 $type = 'C';
1873
1874 } elsif ($cur =~ /^(\?)/o) {
1875 print "QUESTION($1)\n" if ($dbg_values > 1);
1876 $type = 'N';
1877
1878 } elsif ($cur =~ /^(:)/o) {
1879 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1880
1881 substr($var, length($res), 1, $av_pend_colon);
1882 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1883 $type = 'E';
1884 } else {
1885 $type = 'N';
1886 }
1887 $av_pend_colon = 'O';
1888
1889 } elsif ($cur =~ /^(\[)/o) {
1890 print "CLOSE($1)\n" if ($dbg_values > 1);
1891 $type = 'N';
1892
1893 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1894 my $variant;
1895
1896 print "OPV($1)\n" if ($dbg_values > 1);
1897 if ($type eq 'V') {
1898 $variant = 'B';
1899 } else {
1900 $variant = 'U';
1901 }
1902
1903 substr($var, length($res), 1, $variant);
1904 $type = 'N';
1905
1906 } elsif ($cur =~ /^($Operators)/o) {
1907 print "OP($1)\n" if ($dbg_values > 1);
1908 if ($1 ne '++' && $1 ne '--') {
1909 $type = 'N';
1910 }
1911
1912 } elsif ($cur =~ /(^.)/o) {
1913 print "C($1)\n" if ($dbg_values > 1);
1914 }
1915 if (defined $1) {
1916 $cur = substr($cur, length($1));
1917 $res .= $type x length($1);
1918 }
1919 }
1920
1921 return ($res, $var);
1922}
1923
1924sub possible {
1925 my ($possible, $line) = @_;
1926 my $notPermitted = qr{(?:
1927 ^(?:
1928 $Modifier|
1929 $Storage|
1930 $Type|
1931 DEFINE_\S+
1932 )$|
1933 ^(?:
1934 goto|
1935 return|
1936 case|
1937 else|
1938 asm|__asm__|
1939 do|
1940 \#|
1941 \#\#|
1942 )(?:\s|$)|
1943 ^(?:typedef|struct|enum)\b
1944 )}x;
1945 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1946 if ($possible !~ $notPermitted) {
1947 # Check for modifiers.
1948 $possible =~ s/\s*$Storage\s*//g;
1949 $possible =~ s/\s*$Sparse\s*//g;
1950 if ($possible =~ /^\s*$/) {
1951
1952 } elsif ($possible =~ /\s/) {
1953 $possible =~ s/\s*$Type\s*//g;
1954 for my $modifier (split(' ', $possible)) {
1955 if ($modifier !~ $notPermitted) {
1956 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001957 push(@modifierListFile, $modifier);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001958 }
1959 }
1960
1961 } else {
1962 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001963 push(@typeListFile, $possible);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001964 }
1965 build_types();
1966 } else {
1967 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1968 }
1969}
1970
1971my $prefix = '';
1972
1973sub show_type {
1974 my ($type) = @_;
1975
Martin Rothedd591d2017-03-14 10:16:29 -06001976 $type =~ tr/[a-z]/[A-Z]/;
1977
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001978 return defined $use_type{$type} if (scalar keys %use_type > 0);
1979
1980 return !defined $ignore_type{$type};
1981}
1982
1983sub report {
1984 my ($level, $type, $msg) = @_;
1985
1986 if (!show_type($type) ||
1987 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1988 return 0;
1989 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001990 my $output = '';
Martin Roth387dec82017-09-17 19:20:46 -06001991 if ($color) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001992 if ($level eq 'ERROR') {
1993 $output .= RED;
1994 } elsif ($level eq 'WARNING') {
1995 $output .= YELLOW;
1996 } else {
1997 $output .= GREEN;
1998 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001999 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002000 $output .= $prefix . $level . ':';
2001 if ($show_types) {
Martin Roth387dec82017-09-17 19:20:46 -06002002 $output .= BLUE if ($color);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002003 $output .= "$type:";
2004 }
Martin Roth387dec82017-09-17 19:20:46 -06002005 $output .= RESET if ($color);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002006 $output .= ' ' . $msg . "\n";
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002007
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002008 if ($showfile) {
2009 my @lines = split("\n", $output, -1);
2010 splice(@lines, 1, 1);
2011 $output = join("\n", @lines);
2012 }
2013 $output = (split('\n', $output))[0] . "\n" if ($terse);
2014
2015 push(our @report, $output);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002016
2017 return 1;
2018}
2019
2020sub report_dump {
2021 our @report;
2022}
2023
2024sub fixup_current_range {
2025 my ($lineRef, $offset, $length) = @_;
2026
2027 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2028 my $o = $1;
2029 my $l = $2;
2030 my $no = $o + $offset;
2031 my $nl = $l + $length;
2032 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2033 }
2034}
2035
2036sub fix_inserted_deleted_lines {
2037 my ($linesRef, $insertedRef, $deletedRef) = @_;
2038
2039 my $range_last_linenr = 0;
2040 my $delta_offset = 0;
2041
2042 my $old_linenr = 0;
2043 my $new_linenr = 0;
2044
2045 my $next_insert = 0;
2046 my $next_delete = 0;
2047
2048 my @lines = ();
2049
2050 my $inserted = @{$insertedRef}[$next_insert++];
2051 my $deleted = @{$deletedRef}[$next_delete++];
2052
2053 foreach my $old_line (@{$linesRef}) {
2054 my $save_line = 1;
2055 my $line = $old_line; #don't modify the array
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002056 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002057 $delta_offset = 0;
2058 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2059 $range_last_linenr = $new_linenr;
2060 fixup_current_range(\$line, $delta_offset, 0);
2061 }
2062
2063 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2064 $deleted = @{$deletedRef}[$next_delete++];
2065 $save_line = 0;
2066 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2067 }
2068
2069 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2070 push(@lines, ${$inserted}{'LINE'});
2071 $inserted = @{$insertedRef}[$next_insert++];
2072 $new_linenr++;
2073 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2074 }
2075
2076 if ($save_line) {
2077 push(@lines, $line);
2078 $new_linenr++;
2079 }
2080
2081 $old_linenr++;
2082 }
2083
2084 return @lines;
2085}
2086
2087sub fix_insert_line {
2088 my ($linenr, $line) = @_;
2089
2090 my $inserted = {
2091 LINENR => $linenr,
2092 LINE => $line,
2093 };
2094 push(@fixed_inserted, $inserted);
2095}
2096
2097sub fix_delete_line {
2098 my ($linenr, $line) = @_;
2099
2100 my $deleted = {
2101 LINENR => $linenr,
2102 LINE => $line,
2103 };
2104
2105 push(@fixed_deleted, $deleted);
2106}
2107
2108sub ERROR {
2109 my ($type, $msg) = @_;
2110
2111 if (report("ERROR", $type, $msg)) {
2112 our $clean = 0;
2113 our $cnt_error++;
2114 return 1;
2115 }
2116 return 0;
2117}
2118sub WARN {
2119 my ($type, $msg) = @_;
2120
2121 if (report("WARNING", $type, $msg)) {
2122 our $clean = 0;
2123 our $cnt_warn++;
2124 return 1;
2125 }
2126 return 0;
2127}
2128sub CHK {
2129 my ($type, $msg) = @_;
2130
2131 if ($check && report("CHECK", $type, $msg)) {
2132 our $clean = 0;
2133 our $cnt_chk++;
2134 return 1;
2135 }
2136 return 0;
2137}
2138
2139sub check_absolute_file {
2140 my ($absolute, $herecurr) = @_;
2141 my $file = $absolute;
2142
2143 ##print "absolute<$absolute>\n";
2144
2145 # See if any suffix of this path is a path within the tree.
2146 while ($file =~ s@^[^/]*/@@) {
2147 if (-f "$root/$file") {
2148 ##print "file<$file>\n";
2149 last;
2150 }
2151 }
2152 if (! -f _) {
2153 return 0;
2154 }
2155
2156 # It is, so see if the prefix is acceptable.
2157 my $prefix = $absolute;
2158 substr($prefix, -length($file)) = '';
2159
2160 ##print "prefix<$prefix>\n";
2161 if ($prefix ne ".../") {
2162 WARN("USE_RELATIVE_PATH",
2163 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2164 }
2165}
2166
2167sub trim {
2168 my ($string) = @_;
2169
2170 $string =~ s/^\s+|\s+$//g;
2171
2172 return $string;
2173}
2174
2175sub ltrim {
2176 my ($string) = @_;
2177
2178 $string =~ s/^\s+//;
2179
2180 return $string;
2181}
2182
2183sub rtrim {
2184 my ($string) = @_;
2185
2186 $string =~ s/\s+$//;
2187
2188 return $string;
2189}
2190
2191sub string_find_replace {
2192 my ($string, $find, $replace) = @_;
2193
2194 $string =~ s/$find/$replace/g;
2195
2196 return $string;
2197}
2198
2199sub tabify {
2200 my ($leading) = @_;
2201
2202 my $source_indent = 8;
2203 my $max_spaces_before_tab = $source_indent - 1;
2204 my $spaces_to_tab = " " x $source_indent;
2205
2206 #convert leading spaces to tabs
2207 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2208 #Remove spaces before a tab
2209 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2210
2211 return "$leading";
2212}
2213
2214sub pos_last_openparen {
2215 my ($line) = @_;
2216
2217 my $pos = 0;
2218
2219 my $opens = $line =~ tr/\(/\(/;
2220 my $closes = $line =~ tr/\)/\)/;
2221
2222 my $last_openparen = 0;
2223
2224 if (($opens == 0) || ($closes >= $opens)) {
2225 return -1;
2226 }
2227
2228 my $len = length($line);
2229
2230 for ($pos = 0; $pos < $len; $pos++) {
2231 my $string = substr($line, $pos);
2232 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2233 $pos += length($1) - 1;
2234 } elsif (substr($line, $pos, 1) eq '(') {
2235 $last_openparen = $pos;
2236 } elsif (index($string, '(') == -1) {
2237 last;
2238 }
2239 }
2240
2241 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2242}
2243
2244sub process {
2245 my $filename = shift;
2246
2247 my $linenr=0;
2248 my $prevline="";
2249 my $prevrawline="";
2250 my $stashline="";
2251 my $stashrawline="";
2252
2253 my $length;
2254 my $indent;
2255 my $previndent=0;
2256 my $stashindent=0;
2257
2258 our $clean = 1;
2259 my $signoff = 0;
2260 my $is_patch = 0;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002261 my $in_header_lines = $file ? 0 : 1;
2262 my $in_commit_log = 0; #Scanning lines before patch
Martin Rothedd591d2017-03-14 10:16:29 -06002263 my $has_commit_log = 0; #Encountered lines before patch
2264 my $commit_log_possible_stack_dump = 0;
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002265 my $commit_log_long_line = 0;
2266 my $commit_log_has_diff = 0;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002267 my $reported_maintainer_file = 0;
2268 my $non_utf8_charset = 0;
2269
2270 my $last_blank_line = 0;
2271 my $last_coalesced_string_linenr = -1;
2272
2273 our @report = ();
2274 our $cnt_lines = 0;
2275 our $cnt_error = 0;
2276 our $cnt_warn = 0;
2277 our $cnt_chk = 0;
2278
2279 # Trace the real file/line as we go.
2280 my $realfile = '';
2281 my $realline = 0;
2282 my $realcnt = 0;
2283 my $here = '';
Martin Rothedd591d2017-03-14 10:16:29 -06002284 my $context_function; #undef'd unless there's a known function
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002285 my $in_comment = 0;
2286 my $comment_edge = 0;
2287 my $first_line = 0;
2288 my $p1_prefix = '';
2289
2290 my $prev_values = 'E';
2291
2292 # suppression flags
2293 my %suppress_ifbraces;
2294 my %suppress_whiletrailers;
2295 my %suppress_export;
2296 my $suppress_statement = 0;
2297
2298 my %signatures = ();
2299
2300 # Pre-scan the patch sanitizing the lines.
2301 # Pre-scan the patch looking for any __setup documentation.
2302 #
2303 my @setup_docs = ();
2304 my $setup_docs = 0;
2305
2306 my $camelcase_file_seeded = 0;
2307
Martin Roth60915b32018-08-10 21:04:05 -06002308 my $checklicenseline = 1;
2309
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002310 sanitise_line_reset();
2311 my $line;
2312 foreach my $rawline (@rawlines) {
2313 $linenr++;
2314 $line = $rawline;
2315
2316 push(@fixed, $rawline) if ($fix);
2317
2318 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2319 $setup_docs = 0;
Martin Rothedd591d2017-03-14 10:16:29 -06002320 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002321 $setup_docs = 1;
2322 }
2323 #next;
2324 }
Martin Roth387dec82017-09-17 19:20:46 -06002325 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002326 $realline=$1-1;
2327 if (defined $2) {
2328 $realcnt=$3+1;
2329 } else {
2330 $realcnt=1+1;
2331 }
2332 $in_comment = 0;
2333
2334 # Guestimate if this is a continuing comment. Run
2335 # the context looking for a comment "edge". If this
2336 # edge is a close comment then we must be in a comment
2337 # at context start.
2338 my $edge;
2339 my $cnt = $realcnt;
2340 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2341 next if (defined $rawlines[$ln - 1] &&
2342 $rawlines[$ln - 1] =~ /^-/);
2343 $cnt--;
2344 #print "RAW<$rawlines[$ln - 1]>\n";
2345 last if (!defined $rawlines[$ln - 1]);
2346 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2347 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2348 ($edge) = $1;
2349 last;
2350 }
2351 }
2352 if (defined $edge && $edge eq '*/') {
2353 $in_comment = 1;
2354 }
2355
2356 # Guestimate if this is a continuing comment. If this
2357 # is the start of a diff block and this line starts
2358 # ' *' then it is very likely a comment.
2359 if (!defined $edge &&
2360 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2361 {
2362 $in_comment = 1;
2363 }
2364
2365 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2366 sanitise_line_reset($in_comment);
2367
2368 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2369 # Standardise the strings and chars within the input to
2370 # simplify matching -- only bother with positive lines.
2371 $line = sanitise_line($rawline);
2372 }
2373 push(@lines, $line);
2374
2375 if ($realcnt > 1) {
2376 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2377 } else {
2378 $realcnt = 0;
2379 }
2380
2381 #print "==>$rawline\n";
2382 #print "-->$line\n";
2383
2384 if ($setup_docs && $line =~ /^\+/) {
2385 push(@setup_docs, $line);
2386 }
2387 }
2388
2389 $prefix = '';
2390
2391 $realcnt = 0;
2392 $linenr = 0;
2393 $fixlinenr = -1;
2394 foreach my $line (@lines) {
2395 $linenr++;
2396 $fixlinenr++;
2397 my $sline = $line; #copy of $line
2398 $sline =~ s/$;/ /g; #with comments as spaces
2399
2400 my $rawline = $rawlines[$linenr - 1];
2401
Martin Roth60915b32018-08-10 21:04:05 -06002402# check if it's a mode change, rename or start of a patch
2403 if (!$in_commit_log &&
2404 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2405 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2406 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2407 $is_patch = 1;
2408 }
2409
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002410#extract the line range in the file after the patch is applied
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002411 if (!$in_commit_log &&
Martin Roth387dec82017-09-17 19:20:46 -06002412 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2413 my $context = $4;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002414 $is_patch = 1;
2415 $first_line = $linenr + 1;
2416 $realline=$1-1;
2417 if (defined $2) {
2418 $realcnt=$3+1;
2419 } else {
2420 $realcnt=1+1;
2421 }
2422 annotate_reset();
2423 $prev_values = 'E';
2424
2425 %suppress_ifbraces = ();
2426 %suppress_whiletrailers = ();
2427 %suppress_export = ();
2428 $suppress_statement = 0;
Martin Roth387dec82017-09-17 19:20:46 -06002429 if ($context =~ /\b(\w+)\s*\(/) {
2430 $context_function = $1;
2431 } else {
2432 undef $context_function;
2433 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002434 next;
2435
2436# track the line number as we move through the hunk, note that
2437# new versions of GNU diff omit the leading space on completely
2438# blank context lines so we need to count that too.
2439 } elsif ($line =~ /^( |\+|$)/) {
2440 $realline++;
2441 $realcnt-- if ($realcnt != 0);
2442
2443 # Measure the line length and indent.
2444 ($length, $indent) = line_stats($rawline);
2445
2446 # Track the previous line.
2447 ($prevline, $stashline) = ($stashline, $line);
2448 ($previndent, $stashindent) = ($stashindent, $indent);
2449 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2450
2451 #warn "line<$line>\n";
2452
2453 } elsif ($realcnt == 1) {
2454 $realcnt--;
2455 }
2456
2457 my $hunk_line = ($realcnt != 0);
2458
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002459 $here = "#$linenr: " if (!$file);
2460 $here = "#$realline: " if ($file);
2461
2462 my $found_file = 0;
2463 # extract the filename as it passes
2464 if ($line =~ /^diff --git.*?(\S+)$/) {
2465 $realfile = $1;
2466 $realfile =~ s@^([^/]*)/@@ if (!$file);
2467 $in_commit_log = 0;
2468 $found_file = 1;
2469 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2470 $realfile = $1;
2471 $realfile =~ s@^([^/]*)/@@ if (!$file);
2472 $in_commit_log = 0;
2473
2474 $p1_prefix = $1;
2475 if (!$file && $tree && $p1_prefix ne '' &&
2476 -e "$root/$p1_prefix") {
2477 WARN("PATCH_PREFIX",
2478 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2479 }
2480
2481 if ($realfile =~ m@^include/asm/@) {
2482 ERROR("MODIFIED_INCLUDE_ASM",
2483 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2484 }
2485 $found_file = 1;
2486 }
2487
Martin Rothedd591d2017-03-14 10:16:29 -06002488 # coreboot
Martin Rotha3cac872017-03-04 18:17:35 -07002489 my $skipme = 0;
2490 foreach (@exclude) {
2491 if ($realfile =~ m@^(?:$_/)@) {
2492 $skipme = 1;
2493 }
2494 }
2495 if ($skipme) {
2496 next;
2497 }
2498
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002499#make up the handle for any error we report on this line
2500 if ($showfile) {
2501 $prefix = "$realfile:$realline: "
2502 } elsif ($emacs) {
2503 if ($file) {
2504 $prefix = "$filename:$realline: ";
2505 } else {
2506 $prefix = "$filename:$linenr: ";
2507 }
2508 }
2509
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002510 if ($found_file) {
Martin Rothedd591d2017-03-14 10:16:29 -06002511 if (is_maintained_obsolete($realfile)) {
2512 WARN("OBSOLETE",
2513 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2514 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002515 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002516 $check = 1;
2517 } else {
2518 $check = $check_orig;
2519 }
Martin Roth60915b32018-08-10 21:04:05 -06002520 $checklicenseline = 1;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002521 next;
2522 }
2523
2524 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2525
2526 my $hereline = "$here\n$rawline\n";
2527 my $herecurr = "$here\n$rawline\n";
2528 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2529
2530 $cnt_lines++ if ($realcnt != 0);
2531
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002532# Check if the commit log has what seems like a diff which can confuse patch
2533 if ($in_commit_log && !$commit_log_has_diff &&
2534 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2535 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2536 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2537 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2538 ERROR("DIFF_IN_COMMIT_MSG",
2539 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2540 $commit_log_has_diff = 1;
2541 }
2542
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002543# Check for incorrect file permissions
2544 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2545 my $permhere = $here . "FILE: $realfile\n";
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002546 if ($realfile !~ m@scripts/@ &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002547 $realfile !~ /\.(py|pl|awk|sh)$/) {
2548 ERROR("EXECUTE_PERMISSIONS",
2549 "do not set execute permissions for source files\n" . $permhere);
2550 }
2551 }
2552
2553# Check the patch for a signoff:
2554 if ($line =~ /^\s*signed-off-by:/i) {
2555 $signoff++;
2556 $in_commit_log = 0;
2557 }
2558
2559# Check if MAINTAINERS is being updated. If so, there's probably no need to
2560# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2561 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2562 $reported_maintainer_file = 1;
2563 }
2564
2565# Check signature styles
2566 if (!$in_header_lines &&
2567 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2568 my $space_before = $1;
2569 my $sign_off = $2;
2570 my $space_after = $3;
2571 my $email = $4;
2572 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2573
2574 if ($sign_off !~ /$signature_tags/) {
2575 WARN("BAD_SIGN_OFF",
2576 "Non-standard signature: $sign_off\n" . $herecurr);
2577 }
2578 if (defined $space_before && $space_before ne "") {
2579 if (WARN("BAD_SIGN_OFF",
2580 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2581 $fix) {
2582 $fixed[$fixlinenr] =
2583 "$ucfirst_sign_off $email";
2584 }
2585 }
2586 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2587 if (WARN("BAD_SIGN_OFF",
2588 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2589 $fix) {
2590 $fixed[$fixlinenr] =
2591 "$ucfirst_sign_off $email";
2592 }
2593
2594 }
2595 if (!defined $space_after || $space_after ne " ") {
2596 if (WARN("BAD_SIGN_OFF",
2597 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2598 $fix) {
2599 $fixed[$fixlinenr] =
2600 "$ucfirst_sign_off $email";
2601 }
2602 }
2603
2604 my ($email_name, $email_address, $comment) = parse_email($email);
2605 my $suggested_email = format_email(($email_name, $email_address));
2606 if ($suggested_email eq "") {
2607 ERROR("BAD_SIGN_OFF",
2608 "Unrecognized email address: '$email'\n" . $herecurr);
2609 } else {
2610 my $dequoted = $suggested_email;
2611 $dequoted =~ s/^"//;
2612 $dequoted =~ s/" </ </;
2613 # Don't force email to have quotes
2614 # Allow just an angle bracketed address
2615 if ("$dequoted$comment" ne $email &&
2616 "<$email_address>$comment" ne $email &&
2617 "$suggested_email$comment" ne $email) {
2618 WARN("BAD_SIGN_OFF",
2619 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2620 }
2621 }
2622
2623# Check for duplicate signatures
2624 my $sig_nospace = $line;
2625 $sig_nospace =~ s/\s//g;
2626 $sig_nospace = lc($sig_nospace);
2627 if (defined $signatures{$sig_nospace}) {
2628 WARN("BAD_SIGN_OFF",
2629 "Duplicate signature\n" . $herecurr);
2630 } else {
2631 $signatures{$sig_nospace} = 1;
2632 }
2633 }
2634
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002635# Check email subject for common tools that don't need to be mentioned
2636 if ($in_header_lines &&
2637 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2638 WARN("EMAIL_SUBJECT",
2639 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2640 }
2641
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002642# Check for unwanted Gerrit info
2643 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2644 ERROR("GERRIT_CHANGE_ID",
2645 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2646 }
2647
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002648# Check if the commit log is in a possible stack dump
2649 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2650 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2651 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2652 # timestamp
2653 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2654 # stack dump address
2655 $commit_log_possible_stack_dump = 1;
2656 }
2657
2658# Check for line lengths > 75 in commit log, warn once
2659 if ($in_commit_log && !$commit_log_long_line &&
2660 length($line) > 75 &&
2661 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2662 # file delta changes
2663 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2664 # filename then :
2665 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2666 # A Fixes: or Link: line
2667 $commit_log_possible_stack_dump)) {
2668 WARN("COMMIT_LOG_LONG_LINE",
2669 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2670 $commit_log_long_line = 1;
2671 }
2672
2673# Reset possible stack dump if a blank line is found
2674 if ($in_commit_log && $commit_log_possible_stack_dump &&
2675 $line =~ /^\s*$/) {
2676 $commit_log_possible_stack_dump = 0;
2677 }
2678
2679# Check for git id commit length and improperly formed commit descriptions
2680 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Martin Rothedd591d2017-03-14 10:16:29 -06002681 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Martin Roth387dec82017-09-17 19:20:46 -06002682 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002683 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Martin Rothedd591d2017-03-14 10:16:29 -06002684 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002685 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2686 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2687 my $init_char = "c";
2688 my $orig_commit = "";
2689 my $short = 1;
2690 my $long = 0;
2691 my $case = 1;
2692 my $space = 1;
2693 my $hasdesc = 0;
2694 my $hasparens = 0;
2695 my $id = '0123456789ab';
2696 my $orig_desc = "commit description";
2697 my $description = "";
2698
2699 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2700 $init_char = $1;
2701 $orig_commit = lc($2);
2702 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2703 $orig_commit = lc($1);
2704 }
2705
2706 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2707 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2708 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2709 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2710 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2711 $orig_desc = $1;
2712 $hasparens = 1;
2713 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2714 defined $rawlines[$linenr] &&
2715 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2716 $orig_desc = $1;
2717 $hasparens = 1;
2718 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2719 defined $rawlines[$linenr] &&
2720 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2721 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2722 $orig_desc = $1;
2723 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2724 $orig_desc .= " " . $1;
2725 $hasparens = 1;
2726 }
2727
2728 ($id, $description) = git_commit_info($orig_commit,
2729 $id, $orig_desc);
2730
Martin Roth387dec82017-09-17 19:20:46 -06002731 if (defined($id) &&
2732 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002733 ERROR("GIT_COMMIT_ID",
2734 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2735 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002736 }
2737
2738# Check for added, moved or deleted files
2739 if (!$reported_maintainer_file && !$in_commit_log &&
2740 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2741 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2742 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2743 (defined($1) || defined($2))))) {
Martin Rothedd591d2017-03-14 10:16:29 -06002744 $is_patch = 1;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002745 $reported_maintainer_file = 1;
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002746 WARN("FILE_PATH_CHANGES",
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002747 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2748 }
2749
2750# Check for wrappage within a valid hunk of the file
2751 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2752 ERROR("CORRUPTED_PATCH",
2753 "patch seems to be corrupt (line wrapped?)\n" .
2754 $herecurr) if (!$emitted_corrupt++);
2755 }
2756
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002757# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2758 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2759 $rawline !~ m/^$UTF8*$/) {
2760 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2761
2762 my $blank = copy_spacing($rawline);
2763 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2764 my $hereptr = "$hereline$ptr\n";
2765
2766 CHK("INVALID_UTF8",
2767 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2768 }
2769
2770# Check if it's the start of a commit log
2771# (not a header line and we haven't seen the patch filename)
2772 if ($in_header_lines && $realfile =~ /^$/ &&
Martin Roth387dec82017-09-17 19:20:46 -06002773 !($rawline =~ /^\s+(?:\S|$)/ ||
2774 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002775 $in_header_lines = 0;
2776 $in_commit_log = 1;
Martin Rothedd591d2017-03-14 10:16:29 -06002777 $has_commit_log = 1;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002778 }
2779
2780# Check if there is UTF-8 in a commit log when a mail header has explicitly
2781# declined it, i.e defined some charset where it is missing.
2782 if ($in_header_lines &&
2783 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2784 $1 !~ /utf-8/i) {
2785 $non_utf8_charset = 1;
2786 }
2787
2788 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2789 $rawline =~ /$NON_ASCII_UTF8/) {
2790 WARN("UTF8_BEFORE_PATCH",
2791 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2792 }
2793
Martin Rothedd591d2017-03-14 10:16:29 -06002794# Check for absolute kernel paths in commit message
2795 if ($tree && $in_commit_log) {
2796 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2797 my $file = $1;
2798
2799 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2800 check_absolute_file($1, $herecurr)) {
2801 #
2802 } else {
2803 check_absolute_file($file, $herecurr);
2804 }
2805 }
2806 }
2807
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002808# Check for various typo / spelling mistakes
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002809 if (defined($misspellings) &&
2810 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2811 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002812 my $typo = $1;
2813 my $typo_fix = $spelling_fix{lc($typo)};
2814 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2815 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Martin Roth387dec82017-09-17 19:20:46 -06002816 my $msg_level = \&WARN;
2817 $msg_level = \&CHK if ($file);
2818 if (&{$msg_level}("TYPO_SPELLING",
2819 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002820 $fix) {
2821 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2822 }
2823 }
2824 }
2825
2826# ignore non-hunk lines and lines being removed
2827 next if (!$hunk_line || $line =~ /^-/);
2828
2829#trailing whitespace
2830 if ($line =~ /^\+.*\015/) {
2831 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2832 if (ERROR("DOS_LINE_ENDINGS",
2833 "DOS line endings\n" . $herevet) &&
2834 $fix) {
2835 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2836 }
2837 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2838 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2839 if (ERROR("TRAILING_WHITESPACE",
2840 "trailing whitespace\n" . $herevet) &&
2841 $fix) {
2842 $fixed[$fixlinenr] =~ s/\s+$//;
2843 }
2844
2845 $rpt_cleaners = 1;
2846 }
2847
2848# Check for FSF mailing addresses.
2849 if ($rawline =~ /\bwrite to the Free/i ||
Martin Rothedd591d2017-03-14 10:16:29 -06002850 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002851 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2852 $rawline =~ /\b51\s+Franklin\s+St/i) {
2853 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Martin Roth387dec82017-09-17 19:20:46 -06002854 my $msg_level = \&ERROR;
2855 $msg_level = \&CHK if ($file);
2856 &{$msg_level}("FSF_MAILING_ADDRESS",
2857 "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 +01002858 }
2859
2860# check for Kconfig help text having a real description
2861# Only applies when adding the entry originally, after that we do not have
2862# sufficient context to determine whether it is indeed long enough.
2863 if ($realfile =~ /Kconfig/ &&
Martin Roth60915b32018-08-10 21:04:05 -06002864 # 'choice' is usually the last thing on the line (though
2865 # Kconfig supports named choices), so use a word boundary
2866 # (\b) rather than a whitespace character (\s)
2867 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002868 my $length = 0;
2869 my $cnt = $realcnt;
2870 my $ln = $linenr + 1;
2871 my $f;
2872 my $is_start = 0;
2873 my $is_end = 0;
2874 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
2875 $f = $lines[$ln - 1];
2876 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2877 $is_end = $lines[$ln - 1] =~ /^\+/;
2878
2879 next if ($f =~ /^-/);
2880 last if (!$file && $f =~ /^\@\@/);
2881
Martin Roth60915b32018-08-10 21:04:05 -06002882 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002883 $is_start = 1;
Martin Roth60915b32018-08-10 21:04:05 -06002884 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
2885 if ($lines[$ln - 1] =~ "---help---") {
2886 WARN("CONFIG_DESCRIPTION",
2887 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
2888 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002889 $length = -1;
2890 }
2891
2892 $f =~ s/^.//;
2893 $f =~ s/#.*//;
2894 $f =~ s/^\s+//;
2895 next if ($f =~ /^$/);
Martin Roth60915b32018-08-10 21:04:05 -06002896
2897 # This only checks context lines in the patch
2898 # and so hopefully shouldn't trigger false
2899 # positives, even though some of these are
2900 # common words in help texts
2901 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2902 if|endif|menu|endmenu|source)\b/x) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002903 $is_end = 1;
2904 last;
2905 }
2906 $length++;
2907 }
2908 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2909 WARN("CONFIG_DESCRIPTION",
2910 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2911 }
2912 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2913 }
2914
Martin Roth387dec82017-09-17 19:20:46 -06002915# check for MAINTAINERS entries that don't have the right form
2916 if ($realfile =~ /^MAINTAINERS$/ &&
2917 $rawline =~ /^\+[A-Z]:/ &&
2918 $rawline !~ /^\+[A-Z]:\t\S/) {
2919 if (WARN("MAINTAINERS_STYLE",
2920 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2921 $fix) {
2922 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2923 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002924 }
2925
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002926# discourage the use of boolean for type definition attributes of Kconfig options
2927 if ($realfile =~ /Kconfig/ &&
2928 $line =~ /^\+\s*\bboolean\b/) {
2929 WARN("CONFIG_TYPE_BOOLEAN",
2930 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2931 }
2932
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002933 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2934 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2935 my $flag = $1;
2936 my $replacement = {
2937 'EXTRA_AFLAGS' => 'asflags-y',
2938 'EXTRA_CFLAGS' => 'ccflags-y',
2939 'EXTRA_CPPFLAGS' => 'cppflags-y',
2940 'EXTRA_LDFLAGS' => 'ldflags-y',
2941 };
2942
2943 WARN("DEPRECATED_VARIABLE",
2944 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2945 }
2946
2947# check for DT compatible documentation
2948 if (defined $root &&
2949 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2950 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2951
2952 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2953
2954 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2955 my $vp_file = $dt_path . "vendor-prefixes.txt";
2956
2957 foreach my $compat (@compats) {
2958 my $compat2 = $compat;
2959 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2960 my $compat3 = $compat;
2961 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2962 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2963 if ( $? >> 8 ) {
2964 WARN("UNDOCUMENTED_DT_STRING",
2965 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2966 }
2967
2968 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2969 my $vendor = $1;
2970 `grep -Eq "^$vendor\\b" $vp_file`;
2971 if ( $? >> 8 ) {
2972 WARN("UNDOCUMENTED_DT_STRING",
2973 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2974 }
2975 }
2976 }
2977
Martin Roth60915b32018-08-10 21:04:05 -06002978# check for using SPDX license tag at beginning of files
2979 if ($realline == $checklicenseline) {
2980 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
2981 $checklicenseline = 2;
2982 } elsif ($rawline =~ /^\+/) {
2983 my $comment = "";
2984 if ($realfile =~ /\.(h|s|S)$/) {
2985 $comment = '/*';
2986 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
2987 $comment = '//';
2988 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
2989 $comment = '#';
2990 } elsif ($realfile =~ /\.rst$/) {
2991 $comment = '..';
2992 }
2993
2994 if ($comment !~ /^$/ &&
2995 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
2996 WARN("SPDX_LICENSE_TAG",
2997 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
2998 }
2999 }
3000 }
3001
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003002# check we are in a valid source file if not then ignore this hunk
Martin Rothedd591d2017-03-14 10:16:29 -06003003 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003004
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003005# line length limit (with some exclusions)
3006#
3007# There are a few types of lines that may extend beyond $max_line_length:
3008# logging functions like pr_info that end in a string
3009# lines with a single string
3010# #defines that are a single string
Martin Roth60915b32018-08-10 21:04:05 -06003011# lines with an RFC3986 like URL
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003012#
3013# There are 3 different line length message types:
Martin Roth387dec82017-09-17 19:20:46 -06003014# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003015# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3016# LONG_LINE all other lines longer than $max_line_length
3017#
3018# if LONG_LINE is ignored, the other 2 types are also ignored
3019#
3020
3021 if ($line =~ /^\+/ && $length > $max_line_length) {
3022 my $msg_type = "LONG_LINE";
3023
3024 # Check the allowed long line types first
3025
3026 # logging functions that end in a string that starts
3027 # before $max_line_length
3028 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3029 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3030 $msg_type = "";
3031
3032 # lines with only strings (w/ possible termination)
3033 # #defines with only strings
3034 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3035 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3036 $msg_type = "";
3037
Martin Roth60915b32018-08-10 21:04:05 -06003038 # More special cases
3039 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3040 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3041 $msg_type = "";
3042
3043 # URL ($rawline is used in case the URL is in a comment)
3044 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
Martin Rothedd591d2017-03-14 10:16:29 -06003045 $msg_type = "";
3046
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003047 # Otherwise set the alternate message types
3048
3049 # a comment starts before $max_line_length
3050 } elsif ($line =~ /($;[\s$;]*)$/ &&
3051 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3052 $msg_type = "LONG_LINE_COMMENT"
3053
3054 # a quoted string starts before $max_line_length
3055 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3056 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3057 $msg_type = "LONG_LINE_STRING"
3058 }
3059
3060 if ($msg_type ne "" &&
3061 (show_type("LONG_LINE") || show_type($msg_type))) {
3062 WARN($msg_type,
3063 "line over $max_line_length characters\n" . $herecurr);
3064 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003065 }
3066
3067# check for adding lines without a newline.
3068 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3069 WARN("MISSING_EOF_NEWLINE",
3070 "adding a line without newline at end of file\n" . $herecurr);
3071 }
3072
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003073# check we are in a valid source file C or perl if not then ignore this hunk
3074 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
3075
3076# at the beginning of a line any tabs must come first and anything
3077# more than 8 must use tabs.
3078 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3079 $rawline =~ /^\+\s* \s*/) {
3080 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3081 $rpt_cleaners = 1;
3082 if (ERROR("CODE_INDENT",
3083 "code indent should use tabs where possible\n" . $herevet) &&
3084 $fix) {
3085 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3086 }
3087 }
3088
3089# check for space before tabs.
3090 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3091 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3092 if (WARN("SPACE_BEFORE_TAB",
3093 "please, no space before tabs\n" . $herevet) &&
3094 $fix) {
3095 while ($fixed[$fixlinenr] =~
3096 s/(^\+.*) {8,8}\t/$1\t\t/) {}
3097 while ($fixed[$fixlinenr] =~
3098 s/(^\+.*) +\t/$1\t/) {}
3099 }
3100 }
3101
Martin Roth60915b32018-08-10 21:04:05 -06003102# check for assignments on the start of a line
3103 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3104 CHK("ASSIGNMENT_CONTINUATIONS",
3105 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3106 }
3107
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003108# check for && or || at the start of a line
3109 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3110 CHK("LOGICAL_CONTINUATIONS",
3111 "Logical continuations should be on the previous line\n" . $hereprev);
3112 }
3113
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003114# check indentation starts on a tab stop
3115 if ($^V && $^V ge 5.10.0 &&
Martin Roth60915b32018-08-10 21:04:05 -06003116 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003117 my $indent = length($1);
3118 if ($indent % 8) {
3119 if (WARN("TABSTOP",
3120 "Statements should start on a tabstop\n" . $herecurr) &&
3121 $fix) {
3122 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3123 }
3124 }
3125 }
3126
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003127# check multi-line statement indentation matches previous line
3128 if ($^V && $^V ge 5.10.0 &&
Martin Roth387dec82017-09-17 19:20:46 -06003129 $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 +01003130 $prevline =~ /^\+(\t*)(.*)$/;
3131 my $oldindent = $1;
3132 my $rest = $2;
3133
3134 my $pos = pos_last_openparen($rest);
3135 if ($pos >= 0) {
3136 $line =~ /^(\+| )([ \t]*)/;
3137 my $newindent = $2;
3138
3139 my $goodtabindent = $oldindent .
3140 "\t" x ($pos / 8) .
3141 " " x ($pos % 8);
3142 my $goodspaceindent = $oldindent . " " x $pos;
3143
3144 if ($newindent ne $goodtabindent &&
3145 $newindent ne $goodspaceindent) {
3146
3147 if (CHK("PARENTHESIS_ALIGNMENT",
3148 "Alignment should match open parenthesis\n" . $hereprev) &&
3149 $fix && $line =~ /^\+/) {
3150 $fixed[$fixlinenr] =~
3151 s/^\+[ \t]*/\+$goodtabindent/;
3152 }
3153 }
3154 }
3155 }
3156
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003157# check for space after cast like "(int) foo" or "(struct foo) bar"
3158# avoid checking a few false positives:
3159# "sizeof(<type>)" or "__alignof__(<type>)"
3160# function pointer declarations like "(*foo)(int) = bar;"
3161# structure definitions like "(struct foo) { 0 };"
3162# multiline macros that define functions
3163# known attributes or the __attribute__ keyword
3164 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3165 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003166 if (CHK("SPACING",
3167 "No space is necessary after a cast\n" . $herecurr) &&
3168 $fix) {
3169 $fixed[$fixlinenr] =~
3170 s/(\(\s*$Type\s*\))[ \t]+/$1/;
3171 }
3172 }
3173
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003174# Block comment styles
3175# Networking with an initial /*
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003176 if ($realfile =~ m@^(drivers/net/|net/)@ &&
3177 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
3178 $rawline =~ /^\+[ \t]*\*/ &&
3179 $realline > 2) {
3180 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3181 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3182 }
3183
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003184# Block comments use * on subsequent lines
3185 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3186 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003187 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3188 $rawline =~ /^\+/ && #line is new
3189 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003190 WARN("BLOCK_COMMENT_STYLE",
3191 "Block comments use * on subsequent lines\n" . $hereprev);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003192 }
3193
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003194# Block comments use */ on trailing lines
3195 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003196 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3197 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3198 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003199 WARN("BLOCK_COMMENT_STYLE",
3200 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003201 }
3202
Martin Rothedd591d2017-03-14 10:16:29 -06003203# Block comment * alignment
3204 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3205 $line =~ /^\+[ \t]*$;/ && #leading comment
3206 $rawline =~ /^\+[ \t]*\*/ && #leading *
3207 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
3208 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3209 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3210 my $oldindent;
3211 $prevrawline =~ m@^\+([ \t]*/?)\*@;
3212 if (defined($1)) {
3213 $oldindent = expand_tabs($1);
3214 } else {
3215 $prevrawline =~ m@^\+(.*/?)\*@;
3216 $oldindent = expand_tabs($1);
3217 }
3218 $rawline =~ m@^\+([ \t]*)\*@;
3219 my $newindent = $1;
3220 $newindent = expand_tabs($newindent);
3221 if (length($oldindent) ne length($newindent)) {
3222 WARN("BLOCK_COMMENT_STYLE",
3223 "Block comments should align the * on each line\n" . $hereprev);
3224 }
3225 }
3226
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003227# check for missing blank lines after struct/union declarations
3228# with exceptions for various attributes and macros
3229 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3230 $line =~ /^\+/ &&
3231 !($line =~ /^\+\s*$/ ||
3232 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3233 $line =~ /^\+\s*MODULE_/i ||
3234 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3235 $line =~ /^\+[a-z_]*init/ ||
3236 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3237 $line =~ /^\+\s*DECLARE/ ||
Martin Roth60915b32018-08-10 21:04:05 -06003238 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003239 $line =~ /^\+\s*__setup/)) {
3240 if (CHK("LINE_SPACING",
3241 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3242 $fix) {
3243 fix_insert_line($fixlinenr, "\+");
3244 }
3245 }
3246
3247# check for multiple consecutive blank lines
3248 if ($prevline =~ /^[\+ ]\s*$/ &&
3249 $line =~ /^\+\s*$/ &&
3250 $last_blank_line != ($linenr - 1)) {
3251 if (CHK("LINE_SPACING",
3252 "Please don't use multiple blank lines\n" . $hereprev) &&
3253 $fix) {
3254 fix_delete_line($fixlinenr, $rawline);
3255 }
3256
3257 $last_blank_line = $linenr;
3258 }
3259
3260# check for missing blank lines after declarations
3261 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3262 # actual declarations
3263 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3264 # function pointer declarations
3265 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3266 # foo bar; where foo is some local typedef or #define
3267 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3268 # known declaration macros
3269 $prevline =~ /^\+\s+$declaration_macros/) &&
3270 # for "else if" which can look like "$Ident $Ident"
3271 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3272 # other possible extensions of declaration lines
3273 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3274 # not starting a section or a macro "\" extended line
3275 $prevline =~ /(?:\{\s*|\\)$/) &&
3276 # looks like a declaration
3277 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3278 # function pointer declarations
3279 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3280 # foo bar; where foo is some local typedef or #define
3281 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3282 # known declaration macros
3283 $sline =~ /^\+\s+$declaration_macros/ ||
3284 # start of struct or union or enum
3285 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
3286 # start or end of block or continuation of declaration
3287 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3288 # bitfield continuation
3289 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3290 # other possible extensions of declaration lines
3291 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3292 # indentation of previous and current line are the same
3293 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3294 if (WARN("LINE_SPACING",
3295 "Missing a blank line after declarations\n" . $hereprev) &&
3296 $fix) {
3297 fix_insert_line($fixlinenr, "\+");
3298 }
3299 }
3300
3301# check for spaces at the beginning of a line.
3302# Exceptions:
3303# 1) within comments
3304# 2) indented preprocessor commands
3305# 3) hanging labels
3306 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
3307 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3308 if (WARN("LEADING_SPACE",
3309 "please, no spaces at the start of a line\n" . $herevet) &&
3310 $fix) {
3311 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3312 }
3313 }
3314
3315# check we are in a valid C source file if not then ignore this hunk
3316 next if ($realfile !~ /\.(h|c)$/);
3317
Martin Roth60915b32018-08-10 21:04:05 -06003318# check for unusual line ending [ or (
3319 if ($line =~ /^\+.*([\[\(])\s*$/) {
3320 CHK("OPEN_ENDED_LINE",
3321 "Lines should not end with a '$1'\n" . $herecurr);
3322 }
3323
Martin Roth387dec82017-09-17 19:20:46 -06003324# check if this appears to be the start function declaration, save the name
3325 if ($sline =~ /^\+\{\s*$/ &&
3326 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3327 $context_function = $1;
3328 }
3329
3330# check if this appears to be the end of function declaration
3331 if ($sline =~ /^\+\}\s*$/) {
3332 undef $context_function;
3333 }
3334
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003335# check indentation of any line with a bare else
3336# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3337# if the previous line is a break or return and is indented 1 tab more...
3338 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3339 my $tabs = length($1) + 1;
3340 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3341 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3342 defined $lines[$linenr] &&
3343 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3344 WARN("UNNECESSARY_ELSE",
3345 "else is not generally useful after a break or return\n" . $hereprev);
3346 }
3347 }
3348
3349# check indentation of a line with a break;
3350# if the previous line is a goto or return and is indented the same # of tabs
3351 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3352 my $tabs = $1;
3353 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3354 WARN("UNNECESSARY_BREAK",
3355 "break is not useful after a goto or return\n" . $hereprev);
3356 }
3357 }
3358
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003359# check for RCS/CVS revision markers
3360 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3361 WARN("CVS_KEYWORD",
3362 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3363 }
3364
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003365# check for old HOTPLUG __dev<foo> section markings
3366 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3367 WARN("HOTPLUG_SECTION",
3368 "Using $1 is unnecessary\n" . $herecurr);
3369 }
3370
3371# Check for potential 'bare' types
3372 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3373 $realline_next);
3374#print "LINE<$line>\n";
Martin Roth387dec82017-09-17 19:20:46 -06003375 if ($linenr > $suppress_statement &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003376 $realcnt && $sline =~ /.\s*\S/) {
3377 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3378 ctx_statement_block($linenr, $realcnt, 0);
3379 $stat =~ s/\n./\n /g;
3380 $cond =~ s/\n./\n /g;
3381
3382#print "linenr<$linenr> <$stat>\n";
3383 # If this statement has no statement boundaries within
3384 # it there is no point in retrying a statement scan
3385 # until we hit end of it.
3386 my $frag = $stat; $frag =~ s/;+\s*$//;
3387 if ($frag !~ /(?:{|;)/) {
3388#print "skip<$line_nr_next>\n";
3389 $suppress_statement = $line_nr_next;
3390 }
3391
3392 # Find the real next line.
3393 $realline_next = $line_nr_next;
3394 if (defined $realline_next &&
3395 (!defined $lines[$realline_next - 1] ||
3396 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3397 $realline_next++;
3398 }
3399
3400 my $s = $stat;
3401 $s =~ s/{.*$//s;
3402
3403 # Ignore goto labels.
3404 if ($s =~ /$Ident:\*$/s) {
3405
3406 # Ignore functions being called
3407 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3408
3409 } elsif ($s =~ /^.\s*else\b/s) {
3410
3411 # declarations always start with types
3412 } 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) {
3413 my $type = $1;
3414 $type =~ s/\s+/ /g;
3415 possible($type, "A:" . $s);
3416
3417 # definitions in global scope can only start with types
3418 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3419 possible($1, "B:" . $s);
3420 }
3421
3422 # any (foo ... *) is a pointer cast, and foo is a type
3423 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3424 possible($1, "C:" . $s);
3425 }
3426
3427 # Check for any sort of function declaration.
3428 # int foo(something bar, other baz);
3429 # void (*store_gdt)(x86_descr_ptr *);
3430 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3431 my ($name_len) = length($1);
3432
3433 my $ctx = $s;
3434 substr($ctx, 0, $name_len + 1, '');
3435 $ctx =~ s/\)[^\)]*$//;
3436
3437 for my $arg (split(/\s*,\s*/, $ctx)) {
3438 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3439
3440 possible($1, "D:" . $s);
3441 }
3442 }
3443 }
3444
3445 }
3446
3447#
3448# Checks which may be anchored in the context.
3449#
3450
3451# Check for switch () and associated case and default
3452# statements should be at the same indent.
3453 if ($line=~/\bswitch\s*\(.*\)/) {
3454 my $err = '';
3455 my $sep = '';
3456 my @ctx = ctx_block_outer($linenr, $realcnt);
3457 shift(@ctx);
3458 for my $ctx (@ctx) {
3459 my ($clen, $cindent) = line_stats($ctx);
3460 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3461 $indent != $cindent) {
3462 $err .= "$sep$ctx\n";
3463 $sep = '';
3464 } else {
3465 $sep = "[...]\n";
3466 }
3467 }
3468 if ($err ne '') {
3469 ERROR("SWITCH_CASE_INDENT_LEVEL",
3470 "switch and case should be at the same indent\n$hereline$err");
3471 }
3472 }
3473
3474# if/while/etc brace do not go on next line, unless defining a do while loop,
3475# or if that brace on the next line is for something else
3476 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3477 my $pre_ctx = "$1$2";
3478
3479 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
3480
3481 if ($line =~ /^\+\t{6,}/) {
3482 WARN("DEEP_INDENTATION",
3483 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3484 }
3485
3486 my $ctx_cnt = $realcnt - $#ctx - 1;
3487 my $ctx = join("\n", @ctx);
3488
3489 my $ctx_ln = $linenr;
3490 my $ctx_skip = $realcnt;
3491
3492 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3493 defined $lines[$ctx_ln - 1] &&
3494 $lines[$ctx_ln - 1] =~ /^-/)) {
3495 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3496 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3497 $ctx_ln++;
3498 }
3499
3500 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3501 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3502
3503 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3504 ERROR("OPEN_BRACE",
3505 "that open brace { should be on the previous line\n" .
3506 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3507 }
3508 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3509 $ctx =~ /\)\s*\;\s*$/ &&
3510 defined $lines[$ctx_ln - 1])
3511 {
3512 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3513 if ($nindent > $indent) {
3514 WARN("TRAILING_SEMICOLON",
3515 "trailing semicolon indicates no statements, indent implies otherwise\n" .
3516 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3517 }
3518 }
3519 }
3520
3521# Check relative indent for conditionals and blocks.
Martin Roth387dec82017-09-17 19:20:46 -06003522 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 +01003523 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3524 ctx_statement_block($linenr, $realcnt, 0)
3525 if (!defined $stat);
3526 my ($s, $c) = ($stat, $cond);
3527
3528 substr($s, 0, length($c), '');
3529
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003530 # remove inline comments
3531 $s =~ s/$;/ /g;
3532 $c =~ s/$;/ /g;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003533
3534 # Find out how long the conditional actually is.
3535 my @newlines = ($c =~ /\n/gs);
3536 my $cond_lines = 1 + $#newlines;
3537
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003538 # Make sure we remove the line prefixes as we have
3539 # none on the first line, and are going to readd them
3540 # where necessary.
3541 $s =~ s/\n./\n/gs;
3542 while ($s =~ /\n\s+\\\n/) {
3543 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3544 }
3545
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003546 # We want to check the first line inside the block
3547 # starting at the end of the conditional, so remove:
3548 # 1) any blank line termination
3549 # 2) any opening brace { on end of the line
3550 # 3) any do (...) {
3551 my $continuation = 0;
3552 my $check = 0;
3553 $s =~ s/^.*\bdo\b//;
3554 $s =~ s/^\s*{//;
3555 if ($s =~ s/^\s*\\//) {
3556 $continuation = 1;
3557 }
3558 if ($s =~ s/^\s*?\n//) {
3559 $check = 1;
3560 $cond_lines++;
3561 }
3562
3563 # Also ignore a loop construct at the end of a
3564 # preprocessor statement.
3565 if (($prevline =~ /^.\s*#\s*define\s/ ||
3566 $prevline =~ /\\\s*$/) && $continuation == 0) {
3567 $check = 0;
3568 }
3569
3570 my $cond_ptr = -1;
3571 $continuation = 0;
3572 while ($cond_ptr != $cond_lines) {
3573 $cond_ptr = $cond_lines;
3574
3575 # If we see an #else/#elif then the code
3576 # is not linear.
3577 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3578 $check = 0;
3579 }
3580
3581 # Ignore:
3582 # 1) blank lines, they should be at 0,
3583 # 2) preprocessor lines, and
3584 # 3) labels.
3585 if ($continuation ||
3586 $s =~ /^\s*?\n/ ||
3587 $s =~ /^\s*#\s*?/ ||
3588 $s =~ /^\s*$Ident\s*:/) {
3589 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
3590 if ($s =~ s/^.*?\n//) {
3591 $cond_lines++;
3592 }
3593 }
3594 }
3595
3596 my (undef, $sindent) = line_stats("+" . $s);
3597 my $stat_real = raw_line($linenr, $cond_lines);
3598
3599 # Check if either of these lines are modified, else
3600 # this is not this patch's fault.
3601 if (!defined($stat_real) ||
3602 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3603 $check = 0;
3604 }
3605 if (defined($stat_real) && $cond_lines > 1) {
3606 $stat_real = "[...]\n$stat_real";
3607 }
3608
3609 #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";
3610
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003611 if ($check && $s ne '' &&
3612 (($sindent % 8) != 0 ||
3613 ($sindent < $indent) ||
Martin Roth387dec82017-09-17 19:20:46 -06003614 ($sindent == $indent &&
3615 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003616 ($sindent > $indent + 8))) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003617 WARN("SUSPECT_CODE_INDENT",
3618 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
3619 }
Julius Werner218906d2021-03-25 21:12:49 -07003620
3621# Also check if the next statement after the previous condition has the same indent
3622 my ($stat_next, undef, $line_nr_next_next) =
3623 ctx_statement_block($line_nr_next, $remain_next, $off_next);
3624 my $s_next = $stat_next;
3625
3626 # Remove line prefixes
3627 $s_next =~ s/\n./\n/g;
3628
3629 # Remove any comments
3630 $s_next =~ s/$;//g;
3631
3632 # Remove any leading labels
3633 $s_next =~ s/\n( ?$Ident:)/"\n" . " " x length($1)/eg;
3634
3635 # Skip this check for in case next statement starts with 'else'
3636 if ($s_next !~ /\belse\b/) {
3637
3638 # Remove while that belongs to a do {} while
3639 if ($stat =~ /\bdo\b/) {
3640 $s_next =~ s/^.*\bwhile\b\s*($balanced_parens)\s*?//;
3641 }
3642
3643 # Remove blank lines
3644 $s_next =~ s/\s*\\?\n//g;
3645
3646 # Get the real next lines
3647 my $next_nof_lines = $line_nr_next_next - $line_nr_next;
3648 my $stat_next_real = raw_line($line_nr_next, $next_nof_lines);
3649 if (!defined($stat_next_real)) {
3650 $stat_next_real = "";
3651 } elsif ($next_nof_lines > 1) {
3652 $stat_next_real = "[...]\n$stat_next_real";
3653 }
3654 my (undef, $nindent) = line_stats('+' . $s_next);
3655
3656 #print "stat_next<$stat_next> stat<$stat> indent<$indent> nindent<$nindent> s_next<$s_next> stat_next_real<$stat_next_real>\n";
3657
3658 if ($nindent > $indent) {
3659 WARN("SUSPICIOUS_CODE_INDENT",
3660 "suspicious code indentation after conditional statements\n" .
3661 $herecurr . "$stat_real\n$stat_next_real\n");
3662 }
3663 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003664 }
3665
3666 # Track the 'values' across context and added lines.
3667 my $opline = $line; $opline =~ s/^./ /;
3668 my ($curr_values, $curr_vars) =
3669 annotate_values($opline . "\n", $prev_values);
3670 $curr_values = $prev_values . $curr_values;
3671 if ($dbg_values) {
3672 my $outline = $opline; $outline =~ s/\t/ /g;
3673 print "$linenr > .$outline\n";
3674 print "$linenr > $curr_values\n";
3675 print "$linenr > $curr_vars\n";
3676 }
3677 $prev_values = substr($curr_values, -1);
3678
3679#ignore lines not being added
3680 next if ($line =~ /^[^\+]/);
3681
Martin Rothedd591d2017-03-14 10:16:29 -06003682# check for dereferences that span multiple lines
3683 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3684 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3685 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3686 my $ref = $1;
3687 $line =~ /^.\s*($Lval)/;
3688 $ref .= $1;
3689 $ref =~ s/\s//g;
3690 WARN("MULTILINE_DEREFERENCE",
3691 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3692 }
3693
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003694# check for declarations of signed or unsigned without int
Martin Rothedd591d2017-03-14 10:16:29 -06003695 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 -07003696 my $type = $1;
3697 my $var = $2;
3698 $var = "" if (!defined $var);
3699 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3700 my $sign = $1;
3701 my $pointer = $2;
3702
3703 $pointer = "" if (!defined $pointer);
3704
3705 if (WARN("UNSPECIFIED_INT",
3706 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3707 $fix) {
3708 my $decl = trim($sign) . " int ";
3709 my $comp_pointer = $pointer;
3710 $comp_pointer =~ s/\s//g;
3711 $decl .= $comp_pointer;
3712 $decl = rtrim($decl) if ($var eq "");
3713 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3714 }
3715 }
3716 }
3717
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003718# TEST: allow direct testing of the type matcher.
3719 if ($dbg_type) {
3720 if ($line =~ /^.\s*$Declare\s*$/) {
3721 ERROR("TEST_TYPE",
3722 "TEST: is type\n" . $herecurr);
3723 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3724 ERROR("TEST_NOT_TYPE",
3725 "TEST: is not type ($1 is)\n". $herecurr);
3726 }
3727 next;
3728 }
3729# TEST: allow direct testing of the attribute matcher.
3730 if ($dbg_attr) {
3731 if ($line =~ /^.\s*$Modifier\s*$/) {
3732 ERROR("TEST_ATTR",
3733 "TEST: is attr\n" . $herecurr);
3734 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3735 ERROR("TEST_NOT_ATTR",
3736 "TEST: is not attr ($1 is)\n". $herecurr);
3737 }
3738 next;
3739 }
3740
3741# check for initialisation to aggregates open brace on the next line
3742 if ($line =~ /^.\s*{/ &&
3743 $prevline =~ /(?:^|[^=])=\s*$/) {
3744 if (ERROR("OPEN_BRACE",
3745 "that open brace { should be on the previous line\n" . $hereprev) &&
3746 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3747 fix_delete_line($fixlinenr - 1, $prevrawline);
3748 fix_delete_line($fixlinenr, $rawline);
3749 my $fixedline = $prevrawline;
3750 $fixedline =~ s/\s*=\s*$/ = {/;
3751 fix_insert_line($fixlinenr, $fixedline);
3752 $fixedline = $line;
Martin Roth387dec82017-09-17 19:20:46 -06003753 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003754 fix_insert_line($fixlinenr, $fixedline);
3755 }
3756 }
3757
3758#
3759# Checks which are anchored on the added line.
3760#
3761
3762# check for malformed paths in #include statements (uses RAW line)
3763 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3764 my $path = $1;
3765 if ($path =~ m{//}) {
3766 ERROR("MALFORMED_INCLUDE",
3767 "malformed #include filename\n" . $herecurr);
3768 }
3769 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3770 ERROR("UAPI_INCLUDE",
3771 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3772 }
3773 }
3774
3775# no C99 // comments
3776 if ($line =~ m{//}) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003777 if (ERROR("C99_COMMENTS",
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003778 "do not use C99 // comments\n" . $herecurr) &&
3779 $fix) {
3780 my $line = $fixed[$fixlinenr];
3781 if ($line =~ /\/\/(.*)$/) {
3782 my $comment = trim($1);
3783 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
3784 }
3785 }
3786 }
3787 # Remove C99 comments.
3788 $line =~ s@//.*@@;
3789 $opline =~ s@//.*@@;
3790
3791# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3792# the whole statement.
3793#print "APW <$lines[$realline_next - 1]>\n";
3794 if (defined $realline_next &&
3795 exists $lines[$realline_next - 1] &&
3796 !defined $suppress_export{$realline_next} &&
3797 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3798 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3799 # Handle definitions which produce identifiers with
3800 # a prefix:
3801 # XXX(foo);
3802 # EXPORT_SYMBOL(something_foo);
3803 my $name = $1;
3804 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
3805 $name =~ /^${Ident}_$2/) {
3806#print "FOO C name<$name>\n";
3807 $suppress_export{$realline_next} = 1;
3808
3809 } elsif ($stat !~ /(?:
3810 \n.}\s*$|
3811 ^.DEFINE_$Ident\(\Q$name\E\)|
3812 ^.DECLARE_$Ident\(\Q$name\E\)|
3813 ^.LIST_HEAD\(\Q$name\E\)|
3814 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3815 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
3816 )/x) {
3817#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3818 $suppress_export{$realline_next} = 2;
3819 } else {
3820 $suppress_export{$realline_next} = 1;
3821 }
3822 }
3823 if (!defined $suppress_export{$linenr} &&
3824 $prevline =~ /^.\s*$/ &&
3825 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3826 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3827#print "FOO B <$lines[$linenr - 1]>\n";
3828 $suppress_export{$linenr} = 2;
3829 }
3830 if (defined $suppress_export{$linenr} &&
3831 $suppress_export{$linenr} == 2) {
3832 WARN("EXPORT_SYMBOL",
3833 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
3834 }
3835
3836# check for global initialisers.
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003837 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003838 if (ERROR("GLOBAL_INITIALISERS",
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003839 "do not initialise globals to $1\n" . $herecurr) &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003840 $fix) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003841 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003842 }
3843 }
3844# check for static initialisers.
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003845 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003846 if (ERROR("INITIALISED_STATIC",
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003847 "do not initialise statics to $1\n" .
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003848 $herecurr) &&
3849 $fix) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003850 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003851 }
3852 }
3853
3854# check for misordered declarations of char/short/int/long with signed/unsigned
3855 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3856 my $tmp = trim($1);
3857 WARN("MISORDERED_TYPE",
3858 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3859 }
3860
3861# check for static const char * arrays.
3862 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3863 WARN("STATIC_CONST_CHAR_ARRAY",
3864 "static const char * array should probably be static const char * const\n" .
3865 $herecurr);
3866 }
3867
3868# check for static char foo[] = "bar" declarations.
3869 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3870 WARN("STATIC_CONST_CHAR_ARRAY",
3871 "static char array declaration should probably be static const char\n" .
3872 $herecurr);
3873 }
3874
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003875# check for const <foo> const where <foo> is not a pointer or array type
3876 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3877 my $found = $1;
3878 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3879 WARN("CONST_CONST",
3880 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3881 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3882 WARN("CONST_CONST",
3883 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3884 }
3885 }
3886
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003887# check for non-global char *foo[] = {"bar", ...} declarations.
3888 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3889 WARN("STATIC_CONST_CHAR_ARRAY",
3890 "char * array declaration might be better as static const\n" .
3891 $herecurr);
3892 }
3893
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003894# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3895 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3896 my $array = $1;
3897 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3898 my $array_div = $1;
3899 if (WARN("ARRAY_SIZE",
3900 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3901 $fix) {
3902 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3903 }
3904 }
3905 }
3906
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003907# check for function declarations without arguments like "int foo()"
3908 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3909 if (ERROR("FUNCTION_WITHOUT_ARGS",
3910 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3911 $fix) {
3912 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3913 }
3914 }
3915
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003916# check for new typedefs, only function parameters and sparse annotations
3917# make sense.
3918 if ($line =~ /\btypedef\s/ &&
3919 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3920 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3921 $line !~ /\b$typeTypedefs\b/ &&
Martin Rothedd591d2017-03-14 10:16:29 -06003922 $line !~ /\b__bitwise\b/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003923 WARN("NEW_TYPEDEFS",
3924 "do not add new typedefs\n" . $herecurr);
3925 }
3926
3927# * goes on variable not on type
3928 # (char*[ const])
3929 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3930 #print "AA<$1>\n";
3931 my ($ident, $from, $to) = ($1, $2, $2);
3932
3933 # Should start with a space.
3934 $to =~ s/^(\S)/ $1/;
3935 # Should not end with a space.
3936 $to =~ s/\s+$//;
3937 # '*'s should not have spaces between.
3938 while ($to =~ s/\*\s+\*/\*\*/) {
3939 }
3940
3941## print "1: from<$from> to<$to> ident<$ident>\n";
3942 if ($from ne $to) {
3943 if (ERROR("POINTER_LOCATION",
3944 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3945 $fix) {
3946 my $sub_from = $ident;
3947 my $sub_to = $ident;
3948 $sub_to =~ s/\Q$from\E/$to/;
3949 $fixed[$fixlinenr] =~
3950 s@\Q$sub_from\E@$sub_to@;
3951 }
3952 }
3953 }
3954 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3955 #print "BB<$1>\n";
3956 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3957
3958 # Should start with a space.
3959 $to =~ s/^(\S)/ $1/;
3960 # Should not end with a space.
3961 $to =~ s/\s+$//;
3962 # '*'s should not have spaces between.
3963 while ($to =~ s/\*\s+\*/\*\*/) {
3964 }
3965 # Modifiers should have spaces.
3966 $to =~ s/(\b$Modifier$)/$1 /;
3967
3968## print "2: from<$from> to<$to> ident<$ident>\n";
3969 if ($from ne $to && $ident !~ /^$Modifier$/) {
3970 if (ERROR("POINTER_LOCATION",
3971 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3972 $fix) {
3973
3974 my $sub_from = $match;
3975 my $sub_to = $match;
3976 $sub_to =~ s/\Q$from\E/$to/;
3977 $fixed[$fixlinenr] =~
3978 s@\Q$sub_from\E@$sub_to@;
3979 }
3980 }
3981 }
3982
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003983# avoid BUG() or BUG_ON()
3984 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Martin Roth387dec82017-09-17 19:20:46 -06003985 my $msg_level = \&WARN;
3986 $msg_level = \&CHK if ($file);
3987 &{$msg_level}("AVOID_BUG",
3988 "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 -07003989 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003990
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003991# avoid LINUX_VERSION_CODE
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003992 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3993 WARN("LINUX_VERSION_CODE",
3994 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
3995 }
3996
3997# check for uses of printk_ratelimit
3998 if ($line =~ /\bprintk_ratelimit\s*\(/) {
3999 WARN("PRINTK_RATELIMITED",
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004000 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004001 }
4002
Martin Roth60915b32018-08-10 21:04:05 -06004003# printk should use KERN_* levels
4004 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4005 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4006 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004007 }
4008
4009 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4010 my $orig = $1;
4011 my $level = lc($orig);
4012 $level = "warn" if ($level eq "warning");
4013 my $level2 = $level;
4014 $level2 = "dbg" if ($level eq "debug");
4015 WARN("PREFER_PR_LEVEL",
4016 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
4017 }
4018
4019 if ($line =~ /\bpr_warning\s*\(/) {
4020 if (WARN("PREFER_PR_LEVEL",
4021 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4022 $fix) {
4023 $fixed[$fixlinenr] =~
4024 s/\bpr_warning\b/pr_warn/;
4025 }
4026 }
4027
4028 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4029 my $orig = $1;
4030 my $level = lc($orig);
4031 $level = "warn" if ($level eq "warning");
4032 $level = "dbg" if ($level eq "debug");
4033 WARN("PREFER_DEV_LEVEL",
4034 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4035 }
4036
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004037# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4038# number of false positives, but assembly files are not checked, so at
4039# least the arch entry code will not trigger this warning.
4040 if ($line =~ /\bENOSYS\b/) {
4041 WARN("ENOSYS",
4042 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4043 }
4044
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004045# function brace can't be on same line, except for #defines of do while,
4046# or if closed on same line
Martin Roth60915b32018-08-10 21:04:05 -06004047 if ($^V && $^V ge 5.10.0 &&
4048 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4049 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4050 $sline !~ /}/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004051 if (ERROR("OPEN_BRACE",
Martin Roth60915b32018-08-10 21:04:05 -06004052 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004053 $fix) {
4054 fix_delete_line($fixlinenr, $rawline);
4055 my $fixed_line = $rawline;
4056 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4057 my $line1 = $1;
4058 my $line2 = $2;
4059 fix_insert_line($fixlinenr, ltrim($line1));
4060 fix_insert_line($fixlinenr, "\+{");
4061 if ($line2 !~ /^\s*$/) {
4062 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4063 }
4064 }
4065 }
4066
4067# open braces for enum, union and struct go on the same line.
4068 if ($line =~ /^.\s*{/ &&
4069 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
4070 if (ERROR("OPEN_BRACE",
4071 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4072 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4073 fix_delete_line($fixlinenr - 1, $prevrawline);
4074 fix_delete_line($fixlinenr, $rawline);
4075 my $fixedline = rtrim($prevrawline) . " {";
4076 fix_insert_line($fixlinenr, $fixedline);
4077 $fixedline = $rawline;
Martin Roth387dec82017-09-17 19:20:46 -06004078 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004079 if ($fixedline !~ /^\+\s*$/) {
4080 fix_insert_line($fixlinenr, $fixedline);
4081 }
4082 }
4083 }
4084
4085# missing space after union, struct or enum definition
4086 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4087 if (WARN("SPACING",
4088 "missing space after $1 definition\n" . $herecurr) &&
4089 $fix) {
4090 $fixed[$fixlinenr] =~
4091 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4092 }
4093 }
4094
4095# Function pointer declarations
4096# check spacing between type, funcptr, and args
4097# canonical declaration is "type (*funcptr)(args...)"
4098 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
4099 my $declare = $1;
4100 my $pre_pointer_space = $2;
4101 my $post_pointer_space = $3;
4102 my $funcname = $4;
4103 my $post_funcname_space = $5;
4104 my $pre_args_space = $6;
4105
4106# the $Declare variable will capture all spaces after the type
4107# so check it for a missing trailing missing space but pointer return types
4108# don't need a space so don't warn for those.
4109 my $post_declare_space = "";
4110 if ($declare =~ /(\s+)$/) {
4111 $post_declare_space = $1;
4112 $declare = rtrim($declare);
4113 }
4114 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
4115 WARN("SPACING",
4116 "missing space after return type\n" . $herecurr);
4117 $post_declare_space = " ";
4118 }
4119
4120# unnecessary space "type (*funcptr)(args...)"
4121# This test is not currently implemented because these declarations are
4122# equivalent to
4123# int foo(int bar, ...)
4124# and this is form shouldn't/doesn't generate a checkpatch warning.
4125#
4126# elsif ($declare =~ /\s{2,}$/) {
4127# WARN("SPACING",
4128# "Multiple spaces after return type\n" . $herecurr);
4129# }
4130
4131# unnecessary space "type ( *funcptr)(args...)"
4132 if (defined $pre_pointer_space &&
4133 $pre_pointer_space =~ /^\s/) {
4134 WARN("SPACING",
4135 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4136 }
4137
4138# unnecessary space "type (* funcptr)(args...)"
4139 if (defined $post_pointer_space &&
4140 $post_pointer_space =~ /^\s/) {
4141 WARN("SPACING",
4142 "Unnecessary space before function pointer name\n" . $herecurr);
4143 }
4144
4145# unnecessary space "type (*funcptr )(args...)"
4146 if (defined $post_funcname_space &&
4147 $post_funcname_space =~ /^\s/) {
4148 WARN("SPACING",
4149 "Unnecessary space after function pointer name\n" . $herecurr);
4150 }
4151
4152# unnecessary space "type (*funcptr) (args...)"
4153 if (defined $pre_args_space &&
4154 $pre_args_space =~ /^\s/) {
4155 WARN("SPACING",
4156 "Unnecessary space before function pointer arguments\n" . $herecurr);
4157 }
4158
4159 if (show_type("SPACING") && $fix) {
4160 $fixed[$fixlinenr] =~
4161 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
4162 }
4163 }
4164
4165# check for spacing round square brackets; allowed:
4166# 1. with a type on the left -- int [] a;
4167# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4168# 3. inside a curly brace -- = { [0...10] = 5 }
Martin Rothedd591d2017-03-14 10:16:29 -06004169# 4. in an extended asm instruction -- : [r0]"r"(r0) (coreboot)
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004170 while ($line =~ /(.*?\s)\[/g) {
4171 my ($where, $prefix) = ($-[1], $1);
4172 if ($prefix !~ /$Type\s+$/ &&
4173 ($where != 0 || $prefix !~ /^.\s+$/) &&
Martin Rothedd591d2017-03-14 10:16:29 -06004174 $prefix !~ /[{,:]\s+$/) { #coreboot
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004175 if (ERROR("BRACKET_SPACE",
4176 "space prohibited before open square bracket '['\n" . $herecurr) &&
4177 $fix) {
4178 $fixed[$fixlinenr] =~
4179 s/^(\+.*?)\s+\[/$1\[/;
4180 }
4181 }
4182 }
4183
4184# check for spaces between functions and their parentheses.
4185 while ($line =~ /($Ident)\s+\(/g) {
4186 my $name = $1;
4187 my $ctx_before = substr($line, 0, $-[1]);
4188 my $ctx = "$ctx_before$name";
4189
4190 # Ignore those directives where spaces _are_ permitted.
4191 if ($name =~ /^(?:
4192 if|for|while|switch|return|case|
4193 volatile|__volatile__|
4194 __attribute__|format|__extension__|
4195 asm|__asm__)$/x)
4196 {
4197 # cpp #define statements have non-optional spaces, ie
4198 # if there is a space between the name and the open
4199 # parenthesis it is simply not a parameter group.
4200 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4201
4202 # cpp #elif statement condition may start with a (
4203 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4204
4205 # If this whole things ends with a type its most
4206 # likely a typedef for a function.
4207 } elsif ($ctx =~ /$Type$/) {
4208
4209 } else {
4210 if (WARN("SPACING",
4211 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4212 $fix) {
4213 $fixed[$fixlinenr] =~
4214 s/\b$name\s+\(/$name\(/;
4215 }
4216 }
4217 }
4218
4219# Check operator spacing.
4220 if (!($line=~/\#\s*include/)) {
4221 my $fixed_line = "";
4222 my $line_fixed = 0;
4223
4224 my $ops = qr{
4225 <<=|>>=|<=|>=|==|!=|
4226 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4227 =>|->|<<|>>|<|>|=|!|~|
4228 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
4229 \?:|\?|:
4230 }x;
4231 my @elements = split(/($ops|;)/, $opline);
4232
4233## print("element count: <" . $#elements . ">\n");
4234## foreach my $el (@elements) {
4235## print("el: <$el>\n");
4236## }
4237
4238 my @fix_elements = ();
4239 my $off = 0;
4240
4241 foreach my $el (@elements) {
4242 push(@fix_elements, substr($rawline, $off, length($el)));
4243 $off += length($el);
4244 }
4245
4246 $off = 0;
4247
4248 my $blank = copy_spacing($opline);
4249 my $last_after = -1;
4250
4251 for (my $n = 0; $n < $#elements; $n += 2) {
4252
4253 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4254
4255## print("n: <$n> good: <$good>\n");
4256
4257 $off += length($elements[$n]);
4258
4259 # Pick up the preceding and succeeding characters.
4260 my $ca = substr($opline, 0, $off);
4261 my $cc = '';
4262 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4263 $cc = substr($opline, $off + length($elements[$n + 1]));
4264 }
4265 my $cb = "$ca$;$cc";
4266
4267 my $a = '';
4268 $a = 'V' if ($elements[$n] ne '');
4269 $a = 'W' if ($elements[$n] =~ /\s$/);
4270 $a = 'C' if ($elements[$n] =~ /$;$/);
4271 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4272 $a = 'O' if ($elements[$n] eq '');
4273 $a = 'E' if ($ca =~ /^\s*$/);
4274
4275 my $op = $elements[$n + 1];
4276
4277 my $c = '';
4278 if (defined $elements[$n + 2]) {
4279 $c = 'V' if ($elements[$n + 2] ne '');
4280 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
4281 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4282 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4283 $c = 'O' if ($elements[$n + 2] eq '');
4284 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4285 } else {
4286 $c = 'E';
4287 }
4288
4289 my $ctx = "${a}x${c}";
4290
4291 my $at = "(ctx:$ctx)";
4292
4293 my $ptr = substr($blank, 0, $off) . "^";
4294 my $hereptr = "$hereline$ptr\n";
4295
4296 # Pull out the value of this operator.
4297 my $op_type = substr($curr_values, $off + 1, 1);
4298
4299 # Get the full operator variant.
4300 my $opv = $op . substr($curr_vars, $off, 1);
4301
4302 # Ignore operators passed as parameters.
4303 if ($op_type ne 'V' &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004304 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004305
4306# # Ignore comments
4307# } elsif ($op =~ /^$;+$/) {
4308
4309 # ; should have either the end of line or a space or \ after it
4310 } elsif ($op eq ';') {
4311 if ($ctx !~ /.x[WEBC]/ &&
4312 $cc !~ /^\\/ && $cc !~ /^;/) {
4313 if (ERROR("SPACING",
4314 "space required after that '$op' $at\n" . $hereptr)) {
4315 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4316 $line_fixed = 1;
4317 }
4318 }
4319
4320 # // is a comment
4321 } elsif ($op eq '//') {
4322
4323 # : when part of a bitfield
4324 } elsif ($opv eq ':B') {
4325 # skip the bitfield test for now
4326
4327 # No spaces for:
4328 # ->
4329 } elsif ($op eq '->') {
4330 if ($ctx =~ /Wx.|.xW/) {
4331 if (ERROR("SPACING",
4332 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4333 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4334 if (defined $fix_elements[$n + 2]) {
4335 $fix_elements[$n + 2] =~ s/^\s+//;
4336 }
4337 $line_fixed = 1;
4338 }
4339 }
4340
4341 # , must not have a space before and must have a space on the right.
4342 } elsif ($op eq ',') {
4343 my $rtrim_before = 0;
4344 my $space_after = 0;
4345 if ($ctx =~ /Wx./) {
4346 if (ERROR("SPACING",
4347 "space prohibited before that '$op' $at\n" . $hereptr)) {
4348 $line_fixed = 1;
4349 $rtrim_before = 1;
4350 }
4351 }
4352 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
4353 if (ERROR("SPACING",
4354 "space required after that '$op' $at\n" . $hereptr)) {
4355 $line_fixed = 1;
4356 $last_after = $n;
4357 $space_after = 1;
4358 }
4359 }
4360 if ($rtrim_before || $space_after) {
4361 if ($rtrim_before) {
4362 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4363 } else {
4364 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4365 }
4366 if ($space_after) {
4367 $good .= " ";
4368 }
4369 }
4370
4371 # '*' as part of a type definition -- reported already.
4372 } elsif ($opv eq '*_') {
4373 #warn "'*' is part of type\n";
4374
4375 # unary operators should have a space before and
4376 # none after. May be left adjacent to another
4377 # unary operator, or a cast
4378 } elsif ($op eq '!' || $op eq '~' ||
4379 $opv eq '*U' || $opv eq '-U' ||
4380 $opv eq '&U' || $opv eq '&&U') {
4381 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
4382 if (ERROR("SPACING",
4383 "space required before that '$op' $at\n" . $hereptr)) {
4384 if ($n != $last_after + 2) {
4385 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4386 $line_fixed = 1;
4387 }
4388 }
4389 }
4390 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4391 # A unary '*' may be const
4392
4393 } elsif ($ctx =~ /.xW/) {
4394 if (ERROR("SPACING",
4395 "space prohibited after that '$op' $at\n" . $hereptr)) {
4396 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
4397 if (defined $fix_elements[$n + 2]) {
4398 $fix_elements[$n + 2] =~ s/^\s+//;
4399 }
4400 $line_fixed = 1;
4401 }
4402 }
4403
4404 # unary ++ and unary -- are allowed no space on one side.
4405 } elsif ($op eq '++' or $op eq '--') {
4406 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
4407 if (ERROR("SPACING",
4408 "space required one side of that '$op' $at\n" . $hereptr)) {
4409 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4410 $line_fixed = 1;
4411 }
4412 }
4413 if ($ctx =~ /Wx[BE]/ ||
4414 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
4415 if (ERROR("SPACING",
4416 "space prohibited before that '$op' $at\n" . $hereptr)) {
4417 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4418 $line_fixed = 1;
4419 }
4420 }
4421 if ($ctx =~ /ExW/) {
4422 if (ERROR("SPACING",
4423 "space prohibited after that '$op' $at\n" . $hereptr)) {
4424 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4425 if (defined $fix_elements[$n + 2]) {
4426 $fix_elements[$n + 2] =~ s/^\s+//;
4427 }
4428 $line_fixed = 1;
4429 }
4430 }
4431
4432 # << and >> may either have or not have spaces both sides
4433 } elsif ($op eq '<<' or $op eq '>>' or
4434 $op eq '&' or $op eq '^' or $op eq '|' or
4435 $op eq '+' or $op eq '-' or
4436 $op eq '*' or $op eq '/' or
4437 $op eq '%')
4438 {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004439 if ($check) {
4440 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4441 if (CHK("SPACING",
4442 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4443 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4444 $fix_elements[$n + 2] =~ s/^\s+//;
4445 $line_fixed = 1;
4446 }
4447 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4448 if (CHK("SPACING",
4449 "space preferred before that '$op' $at\n" . $hereptr)) {
4450 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4451 $line_fixed = 1;
4452 }
4453 }
4454 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004455 if (ERROR("SPACING",
4456 "need consistent spacing around '$op' $at\n" . $hereptr)) {
4457 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4458 if (defined $fix_elements[$n + 2]) {
4459 $fix_elements[$n + 2] =~ s/^\s+//;
4460 }
4461 $line_fixed = 1;
4462 }
4463 }
4464
4465 # A colon needs no spaces before when it is
4466 # terminating a case value or a label.
4467 } elsif ($opv eq ':C' || $opv eq ':L') {
4468 if ($ctx =~ /Wx./) {
4469 if (ERROR("SPACING",
4470 "space prohibited before that '$op' $at\n" . $hereptr)) {
4471 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4472 $line_fixed = 1;
4473 }
4474 }
4475
4476 # All the others need spaces both sides.
4477 } elsif ($ctx !~ /[EWC]x[CWE]/) {
4478 my $ok = 0;
4479
4480 # Ignore email addresses <foo@bar>
4481 if (($op eq '<' &&
4482 $cc =~ /^\S+\@\S+>/) ||
4483 ($op eq '>' &&
4484 $ca =~ /<\S+\@\S+$/))
4485 {
4486 $ok = 1;
4487 }
4488
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004489 # for asm volatile statements
4490 # ignore a colon with another
4491 # colon immediately before or after
4492 if (($op eq ':') &&
4493 ($ca =~ /:$/ || $cc =~ /^:/)) {
4494 $ok = 1;
4495 }
4496
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004497 # messages are ERROR, but ?: are CHK
4498 if ($ok == 0) {
Martin Roth387dec82017-09-17 19:20:46 -06004499 my $msg_level = \&ERROR;
4500 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004501
Martin Roth387dec82017-09-17 19:20:46 -06004502 if (&{$msg_level}("SPACING",
4503 "spaces required around that '$op' $at\n" . $hereptr)) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004504 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4505 if (defined $fix_elements[$n + 2]) {
4506 $fix_elements[$n + 2] =~ s/^\s+//;
4507 }
4508 $line_fixed = 1;
4509 }
4510 }
4511 }
4512 $off += length($elements[$n + 1]);
4513
4514## print("n: <$n> GOOD: <$good>\n");
4515
4516 $fixed_line = $fixed_line . $good;
4517 }
4518
4519 if (($#elements % 2) == 0) {
4520 $fixed_line = $fixed_line . $fix_elements[$#elements];
4521 }
4522
4523 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4524 $fixed[$fixlinenr] = $fixed_line;
4525 }
4526
4527
4528 }
4529
4530# check for whitespace before a non-naked semicolon
4531 if ($line =~ /^\+.*\S\s+;\s*$/) {
4532 if (WARN("SPACING",
4533 "space prohibited before semicolon\n" . $herecurr) &&
4534 $fix) {
4535 1 while $fixed[$fixlinenr] =~
4536 s/^(\+.*\S)\s+;/$1;/;
4537 }
4538 }
4539
4540# check for multiple assignments
4541 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4542 CHK("MULTIPLE_ASSIGNMENTS",
4543 "multiple assignments should be avoided\n" . $herecurr);
4544 }
4545
4546## # check for multiple declarations, allowing for a function declaration
4547## # continuation.
4548## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4549## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4550##
4551## # Remove any bracketed sections to ensure we do not
4552## # falsly report the parameters of functions.
4553## my $ln = $line;
4554## while ($ln =~ s/\([^\(\)]*\)//g) {
4555## }
4556## if ($ln =~ /,/) {
4557## WARN("MULTIPLE_DECLARATION",
4558## "declaring multiple variables together should be avoided\n" . $herecurr);
4559## }
4560## }
4561
4562#need space before brace following if, while, etc
Alexander Couzensebef00f2016-04-11 00:52:01 +02004563 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
4564 $line =~ /do\{/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004565 if (ERROR("SPACING",
4566 "space required before the open brace '{'\n" . $herecurr) &&
4567 $fix) {
Martin Roth387dec82017-09-17 19:20:46 -06004568 #coreboot - Open braces must be escaped in regex
4569 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 \{/;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004570 }
4571 }
4572
4573## # check for blank lines before declarations
4574## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4575## $prevrawline =~ /^.\s*$/) {
4576## WARN("SPACING",
4577## "No blank lines before declarations\n" . $hereprev);
4578## }
4579##
4580
4581# closing brace should have a space following it when it has anything
4582# on the line
4583 if ($line =~ /}(?!(?:,|;|\)))\S/) {
4584 if (ERROR("SPACING",
4585 "space required after that close brace '}'\n" . $herecurr) &&
4586 $fix) {
4587 $fixed[$fixlinenr] =~
4588 s/}((?!(?:,|;|\)))\S)/} $1/;
4589 }
4590 }
4591
4592# check spacing on square brackets
4593 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
4594 if (ERROR("SPACING",
4595 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4596 $fix) {
4597 $fixed[$fixlinenr] =~
4598 s/\[\s+/\[/;
4599 }
4600 }
4601 if ($line =~ /\s\]/) {
4602 if (ERROR("SPACING",
4603 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4604 $fix) {
4605 $fixed[$fixlinenr] =~
4606 s/\s+\]/\]/;
4607 }
4608 }
4609
4610# check spacing on parentheses
4611 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4612 $line !~ /for\s*\(\s+;/) {
4613 if (ERROR("SPACING",
4614 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4615 $fix) {
4616 $fixed[$fixlinenr] =~
4617 s/\(\s+/\(/;
4618 }
4619 }
4620 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4621 $line !~ /for\s*\(.*;\s+\)/ &&
4622 $line !~ /:\s+\)/) {
4623 if (ERROR("SPACING",
4624 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4625 $fix) {
4626 $fixed[$fixlinenr] =~
4627 s/\s+\)/\)/;
4628 }
4629 }
4630
4631# check unnecessary parentheses around addressof/dereference single $Lvals
4632# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4633
4634 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4635 my $var = $1;
4636 if (CHK("UNNECESSARY_PARENTHESES",
4637 "Unnecessary parentheses around $var\n" . $herecurr) &&
4638 $fix) {
4639 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4640 }
4641 }
4642
4643# check for unnecessary parentheses around function pointer uses
4644# ie: (foo->bar)(); should be foo->bar();
4645# but not "if (foo->bar) (" to avoid some false positives
4646 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4647 my $var = $2;
4648 if (CHK("UNNECESSARY_PARENTHESES",
4649 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4650 $fix) {
4651 my $var2 = deparenthesize($var);
4652 $var2 =~ s/\s//g;
4653 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4654 }
4655 }
4656
Martin Roth387dec82017-09-17 19:20:46 -06004657# check for unnecessary parentheses around comparisons in if uses
Martin Roth60915b32018-08-10 21:04:05 -06004658# when !drivers/staging or command-line uses --strict
4659 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
4660 $^V && $^V ge 5.10.0 && defined($stat) &&
Martin Roth387dec82017-09-17 19:20:46 -06004661 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4662 my $if_stat = $1;
4663 my $test = substr($2, 1, -1);
4664 my $herectx;
4665 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4666 my $match = $1;
4667 # avoid parentheses around potential macro args
4668 next if ($match =~ /^\s*\w+\s*$/);
4669 if (!defined($herectx)) {
4670 $herectx = $here . "\n";
4671 my $cnt = statement_rawlines($if_stat);
4672 for (my $n = 0; $n < $cnt; $n++) {
4673 my $rl = raw_line($linenr, $n);
4674 $herectx .= $rl . "\n";
4675 last if $rl =~ /^[ \+].*\{/;
4676 }
4677 }
4678 CHK("UNNECESSARY_PARENTHESES",
4679 "Unnecessary parentheses around '$match'\n" . $herectx);
4680 }
4681 }
4682
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004683#goto labels aren't indented, allow a single space however
4684 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
4685 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
4686 if (WARN("INDENTED_LABEL",
4687 "labels should not be indented\n" . $herecurr) &&
4688 $fix) {
4689 $fixed[$fixlinenr] =~
4690 s/^(.)\s+/$1/;
4691 }
4692 }
4693
4694# return is not a function
4695 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4696 my $spacing = $1;
4697 if ($^V && $^V ge 5.10.0 &&
4698 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4699 my $value = $1;
4700 $value = deparenthesize($value);
4701 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4702 ERROR("RETURN_PARENTHESES",
4703 "return is not a function, parentheses are not required\n" . $herecurr);
4704 }
4705 } elsif ($spacing !~ /\s+/) {
4706 ERROR("SPACING",
4707 "space required before the open parenthesis '('\n" . $herecurr);
4708 }
4709 }
4710
4711# unnecessary return in a void function
4712# at end-of-function, with the previous line a single leading tab, then return;
4713# and the line before that not a goto label target like "out:"
4714 if ($sline =~ /^[ \+]}\s*$/ &&
4715 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4716 $linenr >= 3 &&
4717 $lines[$linenr - 3] =~ /^[ +]/ &&
4718 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
4719 WARN("RETURN_VOID",
4720 "void function return statements are not generally useful\n" . $hereprev);
4721 }
4722
4723# if statements using unnecessary parentheses - ie: if ((foo == bar))
4724 if ($^V && $^V ge 5.10.0 &&
4725 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4726 my $openparens = $1;
4727 my $count = $openparens =~ tr@\(@\(@;
4728 my $msg = "";
4729 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4730 my $comp = $4; #Not $1 because of $LvalOrFunc
4731 $msg = " - maybe == should be = ?" if ($comp eq "==");
4732 WARN("UNNECESSARY_PARENTHESES",
4733 "Unnecessary parentheses$msg\n" . $herecurr);
4734 }
4735 }
4736
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004737# comparisons with a constant or upper case identifier on the left
4738# avoid cases like "foo + BAR < baz"
4739# only fix matches surrounded by parentheses to avoid incorrect
4740# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4741 if ($^V && $^V ge 5.10.0 &&
4742 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4743 my $lead = $1;
4744 my $const = $2;
4745 my $comp = $3;
4746 my $to = $4;
4747 my $newcomp = $comp;
4748 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4749 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4750 WARN("CONSTANT_COMPARISON",
4751 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4752 $fix) {
4753 if ($comp eq "<") {
4754 $newcomp = ">";
4755 } elsif ($comp eq "<=") {
4756 $newcomp = ">=";
4757 } elsif ($comp eq ">") {
4758 $newcomp = "<";
4759 } elsif ($comp eq ">=") {
4760 $newcomp = "<=";
4761 }
4762 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4763 }
4764 }
4765
4766# Return of what appears to be an errno should normally be negative
4767 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004768 my $name = $1;
4769 if ($name ne 'EOF' && $name ne 'ERROR') {
4770 WARN("USE_NEGATIVE_ERRNO",
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004771 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004772 }
4773 }
4774
4775# Need a space before open parenthesis after if, while etc
4776 if ($line =~ /\b(if|while|for|switch)\(/) {
4777 if (ERROR("SPACING",
4778 "space required before the open parenthesis '('\n" . $herecurr) &&
4779 $fix) {
4780 $fixed[$fixlinenr] =~
4781 s/\b(if|while|for|switch)\(/$1 \(/;
4782 }
4783 }
4784
4785# Check for illegal assignment in if conditional -- and check for trailing
4786# statements after the conditional.
4787 if ($line =~ /do\s*(?!{)/) {
4788 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4789 ctx_statement_block($linenr, $realcnt, 0)
4790 if (!defined $stat);
4791 my ($stat_next) = ctx_statement_block($line_nr_next,
4792 $remain_next, $off_next);
4793 $stat_next =~ s/\n./\n /g;
4794 ##print "stat<$stat> stat_next<$stat_next>\n";
4795
4796 if ($stat_next =~ /^\s*while\b/) {
4797 # If the statement carries leading newlines,
4798 # then count those as offsets.
4799 my ($whitespace) =
4800 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4801 my $offset =
4802 statement_rawlines($whitespace) - 1;
4803
4804 $suppress_whiletrailers{$line_nr_next +
4805 $offset} = 1;
4806 }
4807 }
4808 if (!defined $suppress_whiletrailers{$linenr} &&
4809 defined($stat) && defined($cond) &&
4810 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4811 my ($s, $c) = ($stat, $cond);
4812
4813 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4814 ERROR("ASSIGN_IN_IF",
4815 "do not use assignment in if condition\n" . $herecurr);
4816 }
4817
4818 # Find out what is on the end of the line after the
4819 # conditional.
4820 substr($s, 0, length($c), '');
4821 $s =~ s/\n.*//g;
4822 $s =~ s/$;//g; # Remove any comments
4823 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4824 $c !~ /}\s*while\s*/)
4825 {
4826 # Find out how long the conditional actually is.
4827 my @newlines = ($c =~ /\n/gs);
4828 my $cond_lines = 1 + $#newlines;
4829 my $stat_real = '';
4830
4831 $stat_real = raw_line($linenr, $cond_lines)
4832 . "\n" if ($cond_lines);
4833 if (defined($stat_real) && $cond_lines > 1) {
4834 $stat_real = "[...]\n$stat_real";
4835 }
4836
4837 ERROR("TRAILING_STATEMENTS",
4838 "trailing statements should be on next line\n" . $herecurr . $stat_real);
4839 }
4840 }
4841
4842# Check for bitwise tests written as boolean
4843 if ($line =~ /
4844 (?:
4845 (?:\[|\(|\&\&|\|\|)
4846 \s*0[xX][0-9]+\s*
4847 (?:\&\&|\|\|)
4848 |
4849 (?:\&\&|\|\|)
4850 \s*0[xX][0-9]+\s*
4851 (?:\&\&|\|\||\)|\])
4852 )/x)
4853 {
4854 WARN("HEXADECIMAL_BOOLEAN_TEST",
4855 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
4856 }
4857
4858# if and else should not have general statements after it
4859 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4860 my $s = $1;
4861 $s =~ s/$;//g; # Remove any comments
4862 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4863 ERROR("TRAILING_STATEMENTS",
4864 "trailing statements should be on next line\n" . $herecurr);
4865 }
4866 }
4867# if should not continue a brace
4868 if ($line =~ /}\s*if\b/) {
4869 ERROR("TRAILING_STATEMENTS",
4870 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
4871 $herecurr);
4872 }
4873# case and default should not have general statements after them
4874 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4875 $line !~ /\G(?:
4876 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4877 \s*return\s+
4878 )/xg)
4879 {
4880 ERROR("TRAILING_STATEMENTS",
4881 "trailing statements should be on next line\n" . $herecurr);
4882 }
4883
4884 # Check for }<nl>else {, these must be at the same
4885 # indent level to be relevant to each other.
4886 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4887 $previndent == $indent) {
4888 if (ERROR("ELSE_AFTER_BRACE",
4889 "else should follow close brace '}'\n" . $hereprev) &&
4890 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4891 fix_delete_line($fixlinenr - 1, $prevrawline);
4892 fix_delete_line($fixlinenr, $rawline);
4893 my $fixedline = $prevrawline;
4894 $fixedline =~ s/}\s*$//;
4895 if ($fixedline !~ /^\+\s*$/) {
4896 fix_insert_line($fixlinenr, $fixedline);
4897 }
4898 $fixedline = $rawline;
4899 $fixedline =~ s/^(.\s*)else/$1} else/;
4900 fix_insert_line($fixlinenr, $fixedline);
4901 }
4902 }
4903
4904 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4905 $previndent == $indent) {
4906 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4907
4908 # Find out what is on the end of the line after the
4909 # conditional.
4910 substr($s, 0, length($c), '');
4911 $s =~ s/\n.*//g;
4912
4913 if ($s =~ /^\s*;/) {
4914 if (ERROR("WHILE_AFTER_BRACE",
4915 "while should follow close brace '}'\n" . $hereprev) &&
4916 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4917 fix_delete_line($fixlinenr - 1, $prevrawline);
4918 fix_delete_line($fixlinenr, $rawline);
4919 my $fixedline = $prevrawline;
4920 my $trailing = $rawline;
4921 $trailing =~ s/^\+//;
4922 $trailing = trim($trailing);
4923 $fixedline =~ s/}\s*$/} $trailing/;
4924 fix_insert_line($fixlinenr, $fixedline);
4925 }
4926 }
4927 }
4928
4929#Specific variable tests
4930 while ($line =~ m{($Constant|$Lval)}g) {
4931 my $var = $1;
4932
4933#gcc binary extension
4934 if ($var =~ /^$Binary$/) {
4935 if (WARN("GCC_BINARY_CONSTANT",
4936 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4937 $fix) {
4938 my $hexval = sprintf("0x%x", oct($var));
4939 $fixed[$fixlinenr] =~
4940 s/\b$var\b/$hexval/;
4941 }
4942 }
4943
4944#CamelCase
4945 if ($var !~ /^$Constant$/ &&
4946 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
4947#Ignore Page<foo> variants
4948 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
4949#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4950 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4951#Ignore some three character SI units explicitly, like MiB and KHz
4952 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
4953 while ($var =~ m{($Ident)}g) {
4954 my $word = $1;
4955 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4956 if ($check) {
4957 seed_camelcase_includes();
4958 if (!$file && !$camelcase_file_seeded) {
4959 seed_camelcase_file($realfile);
4960 $camelcase_file_seeded = 1;
4961 }
4962 }
4963 if (!defined $camelcase{$word}) {
4964 $camelcase{$word} = 1;
4965 CHK("CAMELCASE",
4966 "Avoid CamelCase: <$word>\n" . $herecurr);
4967 }
4968 }
4969 }
4970 }
4971
4972#no spaces allowed after \ in define
4973 if ($line =~ /\#\s*define.*\\\s+$/) {
4974 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4975 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4976 $fix) {
4977 $fixed[$fixlinenr] =~ s/\s+$//;
4978 }
4979 }
4980
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004981# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4982# itself <asm/foo.h> (uses RAW line)
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004983 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4984 my $file = "$1.h";
4985 my $checkfile = "include/linux/$file";
4986 if (-f "$root/$checkfile" &&
4987 $realfile ne $checkfile &&
4988 $1 !~ /$allowed_asm_includes/)
4989 {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004990 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4991 if ($asminclude > 0) {
4992 if ($realfile =~ m{^arch/}) {
4993 CHK("ARCH_INCLUDE_LINUX",
4994 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4995 } else {
4996 WARN("INCLUDE_LINUX",
4997 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4998 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004999 }
5000 }
5001 }
5002
5003# multi-statement macros should be enclosed in a do while loop, grab the
5004# first statement and ensure its the whole macro if its not enclosed
5005# in a known good container
5006 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5007 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5008 my $ln = $linenr;
5009 my $cnt = $realcnt;
5010 my ($off, $dstat, $dcond, $rest);
5011 my $ctx = '';
5012 my $has_flow_statement = 0;
5013 my $has_arg_concat = 0;
5014 ($dstat, $dcond, $ln, $cnt, $off) =
5015 ctx_statement_block($linenr, $realcnt, 0);
5016 $ctx = $dstat;
5017 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5018 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5019
5020 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005021 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005022
Martin Rothedd591d2017-03-14 10:16:29 -06005023 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5024 my $define_args = $1;
5025 my $define_stmt = $dstat;
5026 my @def_args = ();
5027
5028 if (defined $define_args && $define_args ne "") {
5029 $define_args = substr($define_args, 1, length($define_args) - 2);
5030 $define_args =~ s/\s*//g;
5031 @def_args = split(",", $define_args);
5032 }
5033
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005034 $dstat =~ s/$;//g;
5035 $dstat =~ s/\\\n.//g;
5036 $dstat =~ s/^\s*//s;
5037 $dstat =~ s/\s*$//s;
5038
5039 # Flatten any parentheses and braces
5040 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5041 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005042 $dstat =~ s/.\[[^\[\]]*\]/1/)
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005043 {
5044 }
5045
5046 # Flatten any obvious string concatentation.
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005047 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5048 $dstat =~ s/$Ident\s*($String)/$1/)
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005049 {
5050 }
5051
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005052 # Make asm volatile uses seem like a generic function
5053 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5054
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005055 my $exceptions = qr{
5056 $Declare|
5057 module_param_named|
5058 MODULE_PARM_DESC|
5059 DECLARE_PER_CPU|
5060 DEFINE_PER_CPU|
5061 __typeof__\(|
5062 union|
5063 struct|
5064 \.$Ident\s*=\s*|
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005065 ^\"|\"$|
5066 ^\[
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005067 }x;
5068 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Martin Rothedd591d2017-03-14 10:16:29 -06005069
5070 $ctx =~ s/\n*$//;
Martin Rothedd591d2017-03-14 10:16:29 -06005071 my $stmt_cnt = statement_rawlines($ctx);
Martin Roth60915b32018-08-10 21:04:05 -06005072 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Martin Rothedd591d2017-03-14 10:16:29 -06005073
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005074 if ($dstat ne '' &&
5075 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5076 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
5077 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5078 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
5079 $dstat !~ /$exceptions/ &&
5080 $dstat !~ /^\.$Ident\s*=/ && # .foo =
5081 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
5082 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
5083 $dstat !~ /^for\s*$Constant$/ && # for (...)
5084 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
Martin Rothedd591d2017-03-14 10:16:29 -06005085 $dstat !~ /^do\s*{/ && # do {...
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005086 $dstat !~ /^\(\{/ && # ({...
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005087 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5088 {
Martin Roth387dec82017-09-17 19:20:46 -06005089 if ($dstat =~ /^\s*if\b/) {
5090 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5091 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5092 } elsif ($dstat =~ /;/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005093 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5094 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5095 } else {
5096 ERROR("COMPLEX_MACRO",
5097 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5098 }
Martin Rothedd591d2017-03-14 10:16:29 -06005099
5100 }
5101
5102 # Make $define_stmt single line, comment-free, etc
5103 my @stmt_array = split('\n', $define_stmt);
5104 my $first = 1;
5105 $define_stmt = "";
5106 foreach my $l (@stmt_array) {
5107 $l =~ s/\\$//;
5108 if ($first) {
5109 $define_stmt = $l;
5110 $first = 0;
5111 } elsif ($l =~ /^[\+ ]/) {
5112 $define_stmt .= substr($l, 1);
5113 }
5114 }
5115 $define_stmt =~ s/$;//g;
5116 $define_stmt =~ s/\s+/ /g;
5117 $define_stmt = trim($define_stmt);
5118
5119# check if any macro arguments are reused (ignore '...' and 'type')
5120 foreach my $arg (@def_args) {
5121 next if ($arg =~ /\.\.\./);
5122 next if ($arg =~ /^type$/i);
Martin Roth387dec82017-09-17 19:20:46 -06005123 my $tmp_stmt = $define_stmt;
5124 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5125 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5126 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Martin Roth60915b32018-08-10 21:04:05 -06005127 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Martin Rothedd591d2017-03-14 10:16:29 -06005128 if ($use_cnt > 1) {
5129 CHK("MACRO_ARG_REUSE",
5130 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5131 }
5132# check if any macro arguments may have other precedence issues
Martin Roth387dec82017-09-17 19:20:46 -06005133 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Martin Rothedd591d2017-03-14 10:16:29 -06005134 ((defined($1) && $1 ne ',') ||
5135 (defined($2) && $2 ne ','))) {
5136 CHK("MACRO_ARG_PRECEDENCE",
5137 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
5138 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005139 }
5140
5141# check for macros with flow control, but without ## concatenation
5142# ## concatenation is commonly a macro that defines a function so ignore those
5143 if ($has_flow_statement && !$has_arg_concat) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005144 my $cnt = statement_rawlines($ctx);
Martin Roth60915b32018-08-10 21:04:05 -06005145 my $herectx = get_stat_here($linenr, $cnt, $here);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005146
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005147 WARN("MACRO_WITH_FLOW_CONTROL",
5148 "Macros with flow control statements should be avoided\n" . "$herectx");
5149 }
5150
5151# check for line continuations outside of #defines, preprocessor #, and asm
5152
5153 } else {
5154 if ($prevline !~ /^..*\\$/ &&
5155 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5156 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
5157 $line =~ /^\+.*\\$/) {
5158 WARN("LINE_CONTINUATIONS",
5159 "Avoid unnecessary line continuations\n" . $herecurr);
5160 }
5161 }
5162
5163# do {} while (0) macro tests:
5164# single-statement macros do not need to be enclosed in do while (0) loop,
5165# macro should not end with a semicolon
5166 if ($^V && $^V ge 5.10.0 &&
5167 $realfile !~ m@/vmlinux.lds.h$@ &&
5168 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5169 my $ln = $linenr;
5170 my $cnt = $realcnt;
5171 my ($off, $dstat, $dcond, $rest);
5172 my $ctx = '';
5173 ($dstat, $dcond, $ln, $cnt, $off) =
5174 ctx_statement_block($linenr, $realcnt, 0);
5175 $ctx = $dstat;
5176
5177 $dstat =~ s/\\\n.//g;
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005178 $dstat =~ s/$;/ /g;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005179
5180 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5181 my $stmts = $2;
5182 my $semis = $3;
5183
5184 $ctx =~ s/\n*$//;
5185 my $cnt = statement_rawlines($ctx);
Martin Roth60915b32018-08-10 21:04:05 -06005186 my $herectx = get_stat_here($linenr, $cnt, $here);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005187
5188 if (($stmts =~ tr/;/;/) == 1 &&
5189 $stmts !~ /^\s*(if|while|for|switch)\b/) {
5190 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5191 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5192 }
5193 if (defined $semis && $semis ne "") {
5194 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5195 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5196 }
5197 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5198 $ctx =~ s/\n*$//;
5199 my $cnt = statement_rawlines($ctx);
Martin Roth60915b32018-08-10 21:04:05 -06005200 my $herectx = get_stat_here($linenr, $cnt, $here);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005201
5202 WARN("TRAILING_SEMICOLON",
5203 "macros should not use a trailing semicolon\n" . "$herectx");
5204 }
5205 }
5206
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005207# check for redundant bracing round if etc
5208 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5209 my ($level, $endln, @chunks) =
5210 ctx_statement_full($linenr, $realcnt, 1);
5211 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5212 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5213 if ($#chunks > 0 && $level == 0) {
5214 my @allowed = ();
5215 my $allow = 0;
5216 my $seen = 0;
5217 my $herectx = $here . "\n";
5218 my $ln = $linenr - 1;
5219 for my $chunk (@chunks) {
5220 my ($cond, $block) = @{$chunk};
5221
5222 # If the condition carries leading newlines, then count those as offsets.
5223 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5224 my $offset = statement_rawlines($whitespace) - 1;
5225
5226 $allowed[$allow] = 0;
5227 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5228
5229 # We have looked at and allowed this specific line.
5230 $suppress_ifbraces{$ln + $offset} = 1;
5231
5232 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5233 $ln += statement_rawlines($block) - 1;
5234
5235 substr($block, 0, length($cond), '');
5236
5237 $seen++ if ($block =~ /^\s*{/);
5238
5239 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5240 if (statement_lines($cond) > 1) {
5241 #print "APW: ALLOWED: cond<$cond>\n";
5242 $allowed[$allow] = 1;
5243 }
5244 if ($block =~/\b(?:if|for|while)\b/) {
5245 #print "APW: ALLOWED: block<$block>\n";
5246 $allowed[$allow] = 1;
5247 }
5248 if (statement_block_size($block) > 1) {
5249 #print "APW: ALLOWED: lines block<$block>\n";
5250 $allowed[$allow] = 1;
5251 }
5252 $allow++;
5253 }
5254 if ($seen) {
5255 my $sum_allowed = 0;
5256 foreach (@allowed) {
5257 $sum_allowed += $_;
5258 }
5259 if ($sum_allowed == 0) {
5260 WARN("BRACES",
5261 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5262 } elsif ($sum_allowed != $allow &&
5263 $seen != $allow) {
5264 CHK("BRACES",
5265 "braces {} should be used on all arms of this statement\n" . $herectx);
5266 }
5267 }
5268 }
5269 }
5270 if (!defined $suppress_ifbraces{$linenr - 1} &&
5271 $line =~ /\b(if|while|for|else)\b/) {
5272 my $allowed = 0;
5273
5274 # Check the pre-context.
5275 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5276 #print "APW: ALLOWED: pre<$1>\n";
5277 $allowed = 1;
5278 }
5279
5280 my ($level, $endln, @chunks) =
5281 ctx_statement_full($linenr, $realcnt, $-[0]);
5282
5283 # Check the condition.
5284 my ($cond, $block) = @{$chunks[0]};
5285 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5286 if (defined $cond) {
5287 substr($block, 0, length($cond), '');
5288 }
5289 if (statement_lines($cond) > 1) {
5290 #print "APW: ALLOWED: cond<$cond>\n";
5291 $allowed = 1;
5292 }
5293 if ($block =~/\b(?:if|for|while)\b/) {
5294 #print "APW: ALLOWED: block<$block>\n";
5295 $allowed = 1;
5296 }
5297 if (statement_block_size($block) > 1) {
5298 #print "APW: ALLOWED: lines block<$block>\n";
5299 $allowed = 1;
5300 }
5301 # Check the post-context.
5302 if (defined $chunks[1]) {
5303 my ($cond, $block) = @{$chunks[1]};
5304 if (defined $cond) {
5305 substr($block, 0, length($cond), '');
5306 }
5307 if ($block =~ /^\s*\{/) {
5308 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5309 $allowed = 1;
5310 }
5311 }
5312 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005313 my $cnt = statement_rawlines($block);
Martin Roth60915b32018-08-10 21:04:05 -06005314 my $herectx = get_stat_here($linenr, $cnt, $here);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005315
5316 WARN("BRACES",
5317 "braces {} are not necessary for single statement blocks\n" . $herectx);
5318 }
5319 }
5320
Martin Rothedd591d2017-03-14 10:16:29 -06005321# check for single line unbalanced braces
5322 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5323 $sline =~ /^.\s*else\s*\{\s*$/) {
5324 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5325 }
5326
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005327# check for unnecessary blank lines around braces
5328 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005329 if (CHK("BRACES",
5330 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5331 $fix && $prevrawline =~ /^\+/) {
5332 fix_delete_line($fixlinenr - 1, $prevrawline);
5333 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005334 }
5335 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005336 if (CHK("BRACES",
5337 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5338 $fix) {
5339 fix_delete_line($fixlinenr, $rawline);
5340 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005341 }
5342
5343# no volatiles please
5344 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5345 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5346 WARN("VOLATILE",
Martin Rothedd591d2017-03-14 10:16:29 -06005347 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005348 }
5349
5350# Check for user-visible strings broken across lines, which breaks the ability
5351# to grep for the string. Make exceptions when the previous string ends in a
5352# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5353# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005354 if ($line =~ /^\+\s*$String/ &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005355 $prevline =~ /"\s*$/ &&
5356 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5357 if (WARN("SPLIT_STRING",
5358 "quoted string split across lines\n" . $hereprev) &&
5359 $fix &&
5360 $prevrawline =~ /^\+.*"\s*$/ &&
5361 $last_coalesced_string_linenr != $linenr - 1) {
5362 my $extracted_string = get_quoted_string($line, $rawline);
5363 my $comma_close = "";
5364 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5365 $comma_close = $1;
5366 }
5367
5368 fix_delete_line($fixlinenr - 1, $prevrawline);
5369 fix_delete_line($fixlinenr, $rawline);
5370 my $fixedline = $prevrawline;
5371 $fixedline =~ s/"\s*$//;
5372 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5373 fix_insert_line($fixlinenr - 1, $fixedline);
5374 $fixedline = $rawline;
5375 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5376 if ($fixedline !~ /\+\s*$/) {
5377 fix_insert_line($fixlinenr, $fixedline);
5378 }
5379 $last_coalesced_string_linenr = $linenr;
5380 }
5381 }
5382
5383# check for missing a space in a string concatenation
5384 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5385 WARN('MISSING_SPACE',
5386 "break quoted strings at a space character\n" . $hereprev);
5387 }
5388
Martin Roth387dec82017-09-17 19:20:46 -06005389# check for an embedded function name in a string when the function is known
5390# This does not work very well for -f --file checking as it depends on patch
5391# context providing the function name or a single line form for in-file
5392# function declarations
Martin Rothedd591d2017-03-14 10:16:29 -06005393 if ($line =~ /^\+.*$String/ &&
5394 defined($context_function) &&
Martin Roth387dec82017-09-17 19:20:46 -06005395 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5396 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Martin Rothedd591d2017-03-14 10:16:29 -06005397 WARN("EMBEDDED_FUNCTION_NAME",
Martin Roth387dec82017-09-17 19:20:46 -06005398 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Martin Rothedd591d2017-03-14 10:16:29 -06005399 }
5400
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005401# check for spaces before a quoted newline
5402 if ($rawline =~ /^.*\".*\s\\n/) {
5403 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5404 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5405 $fix) {
5406 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5407 }
5408
5409 }
5410
5411# concatenated string without spaces between elements
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005412 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005413 CHK("CONCATENATED_STRING",
5414 "Concatenated strings should use spaces between elements\n" . $herecurr);
5415 }
5416
5417# uncoalesced string fragments
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005418 if ($line =~ /$String\s*"/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005419 WARN("STRING_FRAGMENTS",
5420 "Consecutive strings are generally better as a single string\n" . $herecurr);
5421 }
5422
Martin Rothedd591d2017-03-14 10:16:29 -06005423# check for non-standard and hex prefixed decimal printf formats
5424 my $show_L = 1; #don't show the same defect twice
5425 my $show_Z = 1;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005426 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Martin Rothedd591d2017-03-14 10:16:29 -06005427 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005428 $string =~ s/%%/__/g;
Martin Rothedd591d2017-03-14 10:16:29 -06005429 # check for %L
5430 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005431 WARN("PRINTF_L",
Martin Rothedd591d2017-03-14 10:16:29 -06005432 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5433 $show_L = 0;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005434 }
Martin Rothedd591d2017-03-14 10:16:29 -06005435 # check for %Z
5436 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5437 WARN("PRINTF_Z",
5438 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5439 $show_Z = 0;
5440 }
5441 # check for 0x<decimal>
5442 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5443 ERROR("PRINTF_0XDECIMAL",
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005444 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5445 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005446 }
5447
5448# check for line continuations in quoted strings with odd counts of "
Martin Roth60915b32018-08-10 21:04:05 -06005449 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005450 WARN("LINE_CONTINUATIONS",
5451 "Avoid line continuations in quoted strings\n" . $herecurr);
5452 }
5453
5454# warn about #if 0
5455 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
5456 CHK("REDUNDANT_CODE",
5457 "if this code is redundant consider removing it\n" .
5458 $herecurr);
5459 }
5460
5461# check for needless "if (<foo>) fn(<foo>)" uses
5462 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005463 my $tested = quotemeta($1);
5464 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5465 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5466 my $func = $1;
5467 if (WARN('NEEDLESS_IF',
5468 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5469 $fix) {
5470 my $do_fix = 1;
5471 my $leading_tabs = "";
5472 my $new_leading_tabs = "";
5473 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5474 $leading_tabs = $1;
5475 } else {
5476 $do_fix = 0;
5477 }
5478 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5479 $new_leading_tabs = $1;
5480 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5481 $do_fix = 0;
5482 }
5483 } else {
5484 $do_fix = 0;
5485 }
5486 if ($do_fix) {
5487 fix_delete_line($fixlinenr - 1, $prevrawline);
5488 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5489 }
5490 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005491 }
5492 }
5493
5494# check for unnecessary "Out of Memory" messages
5495 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5496 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5497 (defined $1 || defined $3) &&
5498 $linenr > 3) {
5499 my $testval = $2;
5500 my $testline = $lines[$linenr - 3];
5501
5502 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5503# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5504
Martin Roth387dec82017-09-17 19:20:46 -06005505 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 +01005506 WARN("OOM_MESSAGE",
5507 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5508 }
5509 }
5510
5511# check for logging functions with KERN_<LEVEL>
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005512 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005513 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5514 my $level = $1;
5515 if (WARN("UNNECESSARY_KERN_LEVEL",
5516 "Possible unnecessary $level\n" . $herecurr) &&
5517 $fix) {
5518 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5519 }
5520 }
5521
Martin Rothedd591d2017-03-14 10:16:29 -06005522# check for logging continuations
5523 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5524 WARN("LOGGING_CONTINUATION",
5525 "Avoid logging continuation uses where feasible\n" . $herecurr);
5526 }
5527
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005528# check for mask then right shift without a parentheses
5529 if ($^V && $^V ge 5.10.0 &&
5530 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5531 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5532 WARN("MASK_THEN_SHIFT",
5533 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5534 }
5535
5536# check for pointer comparisons to NULL
5537 if ($^V && $^V ge 5.10.0) {
5538 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5539 my $val = $1;
5540 my $equal = "!";
5541 $equal = "" if ($4 eq "!=");
5542 if (CHK("COMPARISON_TO_NULL",
5543 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5544 $fix) {
5545 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5546 }
5547 }
5548 }
5549
5550# check for bad placement of section $InitAttribute (e.g.: __initdata)
5551 if ($line =~ /(\b$InitAttribute\b)/) {
5552 my $attr = $1;
5553 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5554 my $ptr = $1;
5555 my $var = $2;
5556 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5557 ERROR("MISPLACED_INIT",
5558 "$attr should be placed after $var\n" . $herecurr)) ||
5559 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5560 WARN("MISPLACED_INIT",
5561 "$attr should be placed after $var\n" . $herecurr))) &&
5562 $fix) {
5563 $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;
5564 }
5565 }
5566 }
5567
5568# check for $InitAttributeData (ie: __initdata) with const
5569 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5570 my $attr = $1;
5571 $attr =~ /($InitAttributePrefix)(.*)/;
5572 my $attr_prefix = $1;
5573 my $attr_type = $2;
5574 if (ERROR("INIT_ATTRIBUTE",
5575 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5576 $fix) {
5577 $fixed[$fixlinenr] =~
5578 s/$InitAttributeData/${attr_prefix}initconst/;
5579 }
5580 }
5581
5582# check for $InitAttributeConst (ie: __initconst) without const
5583 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5584 my $attr = $1;
5585 if (ERROR("INIT_ATTRIBUTE",
5586 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5587 $fix) {
5588 my $lead = $fixed[$fixlinenr] =~
5589 /(^\+\s*(?:static\s+))/;
5590 $lead = rtrim($1);
5591 $lead = "$lead " if ($lead !~ /^\+$/);
5592 $lead = "${lead}const ";
5593 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5594 }
5595 }
5596
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005597# check for __read_mostly with const non-pointer (should just be const)
5598 if ($line =~ /\b__read_mostly\b/ &&
5599 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5600 if (ERROR("CONST_READ_MOSTLY",
5601 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5602 $fix) {
5603 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5604 }
5605 }
5606
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005607# don't use __constant_<foo> functions outside of include/uapi/
5608 if ($realfile !~ m@^include/uapi/@ &&
5609 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5610 my $constant_func = $1;
5611 my $func = $constant_func;
5612 $func =~ s/^__constant_//;
5613 if (WARN("CONSTANT_CONVERSION",
5614 "$constant_func should be $func\n" . $herecurr) &&
5615 $fix) {
5616 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5617 }
5618 }
5619
5620# prefer usleep_range over udelay
5621 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
5622 my $delay = $1;
5623 # ignore udelay's < 10, however
5624 if (! ($delay < 10) ) {
5625 CHK("USLEEP_RANGE",
5626 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5627 }
5628 if ($delay > 2000) {
5629 WARN("LONG_UDELAY",
5630 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
5631 }
5632 }
5633
5634# warn about unexpectedly long msleep's
5635 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5636 if ($1 < 20) {
5637 WARN("MSLEEP",
5638 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5639 }
5640 }
5641
5642# check for comparisons of jiffies
5643 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5644 WARN("JIFFIES_COMPARISON",
5645 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5646 }
5647
5648# check for comparisons of get_jiffies_64()
5649 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5650 WARN("JIFFIES_COMPARISON",
5651 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5652 }
5653
5654# warn about #ifdefs in C files
5655# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
5656# print "#ifdef in C files should be avoided\n";
5657# print "$herecurr";
5658# $clean = 0;
5659# }
5660
5661# warn about spacing in #ifdefs
5662 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
5663 if (ERROR("SPACING",
5664 "exactly one space required after that #$1\n" . $herecurr) &&
5665 $fix) {
5666 $fixed[$fixlinenr] =~
5667 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5668 }
5669
5670 }
5671
5672# check for spinlock_t definitions without a comment.
5673 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5674 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
5675 my $which = $1;
5676 if (!ctx_has_comment($first_line, $linenr)) {
5677 CHK("UNCOMMENTED_DEFINITION",
5678 "$1 definition without comment\n" . $herecurr);
5679 }
5680 }
5681# check for memory barriers without a comment.
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005682
5683 my $barriers = qr{
5684 mb|
5685 rmb|
5686 wmb|
5687 read_barrier_depends
5688 }x;
5689 my $barrier_stems = qr{
5690 mb__before_atomic|
5691 mb__after_atomic|
5692 store_release|
5693 load_acquire|
5694 store_mb|
5695 (?:$barriers)
5696 }x;
5697 my $all_barriers = qr{
5698 (?:$barriers)|
5699 smp_(?:$barrier_stems)|
5700 virt_(?:$barrier_stems)
5701 }x;
5702
5703 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005704 if (!ctx_has_comment($first_line, $linenr)) {
5705 WARN("MEMORY_BARRIER",
5706 "memory barrier without comment\n" . $herecurr);
5707 }
5708 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005709
5710 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5711
5712 if ($realfile !~ m@^include/asm-generic/@ &&
5713 $realfile !~ m@/barrier\.h$@ &&
5714 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5715 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5716 WARN("MEMORY_BARRIER",
5717 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5718 }
5719
5720# check for waitqueue_active without a comment.
5721 if ($line =~ /\bwaitqueue_active\s*\(/) {
5722 if (!ctx_has_comment($first_line, $linenr)) {
5723 WARN("WAITQUEUE_ACTIVE",
5724 "waitqueue_active without comment\n" . $herecurr);
5725 }
5726 }
5727
Martin Roth60915b32018-08-10 21:04:05 -06005728# check for smp_read_barrier_depends and read_barrier_depends
5729 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5730 WARN("READ_BARRIER_DEPENDS",
5731 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5732 }
5733
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005734# check of hardware specific defines
5735 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5736 CHK("ARCH_DEFINES",
5737 "architecture specific defines should be avoided\n" . $herecurr);
5738 }
5739
Martin Roth387dec82017-09-17 19:20:46 -06005740# check that the storage class is not after a type
5741 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005742 WARN("STORAGE_CLASS",
Martin Roth387dec82017-09-17 19:20:46 -06005743 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5744 }
5745# Check that the storage class is at the beginning of a declaration
5746 if ($line =~ /\b$Storage\b/ &&
5747 $line !~ /^.\s*$Storage/ &&
5748 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5749 $1 !~ /[\,\)]\s*$/) {
5750 WARN("STORAGE_CLASS",
5751 "storage class should be at the beginning of the declaration\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005752 }
5753
5754# check the location of the inline attribute, that it is between
5755# storage class and type.
5756 if ($line =~ /\b$Type\s+$Inline\b/ ||
5757 $line =~ /\b$Inline\s+$Storage\b/) {
5758 ERROR("INLINE_LOCATION",
5759 "inline keyword should sit between storage class and type\n" . $herecurr);
5760 }
5761
5762# Check for __inline__ and __inline, prefer inline
5763 if ($realfile !~ m@\binclude/uapi/@ &&
5764 $line =~ /\b(__inline__|__inline)\b/) {
5765 if (WARN("INLINE",
5766 "plain inline is preferred over $1\n" . $herecurr) &&
5767 $fix) {
5768 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5769
5770 }
5771 }
5772
5773# Check for __attribute__ packed, prefer __packed
5774 if ($realfile !~ m@\binclude/uapi/@ &&
5775 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5776 WARN("PREFER_PACKED",
5777 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
5778 }
5779
5780# Check for __attribute__ aligned, prefer __aligned
5781 if ($realfile !~ m@\binclude/uapi/@ &&
5782 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5783 WARN("PREFER_ALIGNED",
5784 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
5785 }
5786
5787# Check for __attribute__ format(printf, prefer __printf
5788 if ($realfile !~ m@\binclude/uapi/@ &&
5789 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5790 if (WARN("PREFER_PRINTF",
5791 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5792 $fix) {
5793 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5794
5795 }
5796 }
5797
5798# Check for __attribute__ format(scanf, prefer __scanf
5799 if ($realfile !~ m@\binclude/uapi/@ &&
5800 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5801 if (WARN("PREFER_SCANF",
5802 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5803 $fix) {
5804 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5805 }
5806 }
5807
5808# Check for __attribute__ weak, or __weak declarations (may have link issues)
5809 if ($^V && $^V ge 5.10.0 &&
5810 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5811 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5812 $line =~ /\b__weak\b/)) {
5813 ERROR("WEAK_DECLARATION",
5814 "Using weak declarations can have unintended link defects\n" . $herecurr);
5815 }
5816
Martin Rothedd591d2017-03-14 10:16:29 -06005817# check for c99 types like uint8_t used outside of uapi/ and tools/
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005818 if ($realfile !~ m@\binclude/uapi/@ &&
Martin Rothedd591d2017-03-14 10:16:29 -06005819 $realfile !~ m@\btools/@ &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005820 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5821 my $type = $1;
5822 if ($type =~ /\b($typeC99Typedefs)\b/) {
5823 $type = $1;
5824 my $kernel_type = 'u';
5825 $kernel_type = 's' if ($type =~ /^_*[si]/);
5826 $type =~ /(\d+)/;
5827 $kernel_type .= $1;
5828 if (CHK("PREFER_KERNEL_TYPES",
5829 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5830 $fix) {
5831 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5832 }
5833 }
5834 }
5835
5836# check for cast of C90 native int or longer types constants
5837 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5838 my $cast = $1;
5839 my $const = $2;
5840 if (WARN("TYPECAST_INT_CONSTANT",
5841 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5842 $fix) {
5843 my $suffix = "";
5844 my $newconst = $const;
5845 $newconst =~ s/${Int_type}$//;
5846 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5847 if ($cast =~ /\blong\s+long\b/) {
5848 $suffix .= 'LL';
5849 } elsif ($cast =~ /\blong\b/) {
5850 $suffix .= 'L';
5851 }
5852 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5853 }
5854 }
5855
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005856# check for sizeof(&)
5857 if ($line =~ /\bsizeof\s*\(\s*\&/) {
5858 WARN("SIZEOF_ADDRESS",
5859 "sizeof(& should be avoided\n" . $herecurr);
5860 }
5861
5862# check for sizeof without parenthesis
5863 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5864 if (WARN("SIZEOF_PARENTHESIS",
5865 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5866 $fix) {
5867 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5868 }
5869 }
5870
5871# check for struct spinlock declarations
5872 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5873 WARN("USE_SPINLOCK_T",
5874 "struct spinlock should be spinlock_t\n" . $herecurr);
5875 }
5876
5877# check for seq_printf uses that could be seq_puts
5878 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5879 my $fmt = get_quoted_string($line, $rawline);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005880 $fmt =~ s/%%//g;
5881 if ($fmt !~ /%/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005882 if (WARN("PREFER_SEQ_PUTS",
5883 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5884 $fix) {
5885 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5886 }
5887 }
5888 }
5889
Martin Roth60915b32018-08-10 21:04:05 -06005890# check for vsprintf extension %p<foo> misuses
Martin Roth387dec82017-09-17 19:20:46 -06005891 if ($^V && $^V ge 5.10.0 &&
5892 defined $stat &&
5893 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5894 $1 !~ /^_*volatile_*$/) {
Martin Roth60915b32018-08-10 21:04:05 -06005895 my $stat_real;
5896
Martin Roth387dec82017-09-17 19:20:46 -06005897 my $lc = $stat =~ tr@\n@@;
5898 $lc = $lc + $linenr;
5899 for (my $count = $linenr; $count <= $lc; $count++) {
Martin Roth60915b32018-08-10 21:04:05 -06005900 my $specifier;
5901 my $extension;
5902 my $bad_specifier = "";
Martin Roth387dec82017-09-17 19:20:46 -06005903 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5904 $fmt =~ s/%%//g;
Martin Roth60915b32018-08-10 21:04:05 -06005905
5906 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5907 $specifier = $1;
5908 $extension = $2;
5909 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
5910 $bad_specifier = $specifier;
5911 last;
5912 }
5913 if ($extension eq "x" && !defined($stat_real)) {
5914 if (!defined($stat_real)) {
5915 $stat_real = get_stat_real($linenr, $lc);
5916 }
5917 WARN("VSPRINTF_SPECIFIER_PX",
5918 "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");
5919 }
Martin Roth387dec82017-09-17 19:20:46 -06005920 }
Martin Roth60915b32018-08-10 21:04:05 -06005921 if ($bad_specifier ne "") {
5922 my $stat_real = get_stat_real($linenr, $lc);
5923 my $ext_type = "Invalid";
5924 my $use = "";
5925 if ($bad_specifier =~ /p[Ff]/) {
5926 $ext_type = "Deprecated";
5927 $use = " - use %pS instead";
5928 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
5929 }
5930
5931 WARN("VSPRINTF_POINTER_EXTENSION",
5932 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
Martin Roth387dec82017-09-17 19:20:46 -06005933 }
Martin Roth387dec82017-09-17 19:20:46 -06005934 }
5935 }
5936
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005937# Check for misused memsets
5938 if ($^V && $^V ge 5.10.0 &&
5939 defined $stat &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005940 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005941
5942 my $ms_addr = $2;
5943 my $ms_val = $7;
5944 my $ms_size = $12;
5945
5946 if ($ms_size =~ /^(0x|)0$/i) {
5947 ERROR("MEMSET",
5948 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5949 } elsif ($ms_size =~ /^(0x|)1$/i) {
5950 WARN("MEMSET",
5951 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5952 }
5953 }
5954
5955# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Martin Rothedd591d2017-03-14 10:16:29 -06005956# if ($^V && $^V ge 5.10.0 &&
5957# defined $stat &&
5958# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5959# if (WARN("PREFER_ETHER_ADDR_COPY",
5960# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5961# $fix) {
5962# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5963# }
5964# }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005965
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005966# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Martin Rothedd591d2017-03-14 10:16:29 -06005967# if ($^V && $^V ge 5.10.0 &&
5968# defined $stat &&
5969# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5970# WARN("PREFER_ETHER_ADDR_EQUAL",
5971# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5972# }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005973
5974# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5975# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Martin Rothedd591d2017-03-14 10:16:29 -06005976# if ($^V && $^V ge 5.10.0 &&
5977# defined $stat &&
5978# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5979#
5980# my $ms_val = $7;
5981#
5982# if ($ms_val =~ /^(?:0x|)0+$/i) {
5983# if (WARN("PREFER_ETH_ZERO_ADDR",
5984# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5985# $fix) {
5986# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5987# }
5988# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5989# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5990# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5991# $fix) {
5992# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5993# }
5994# }
5995# }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005996
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005997# typecasts on min/max could be min_t/max_t
5998 if ($^V && $^V ge 5.10.0 &&
5999 defined $stat &&
6000 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6001 if (defined $2 || defined $7) {
6002 my $call = $1;
6003 my $cast1 = deparenthesize($2);
6004 my $arg1 = $3;
6005 my $cast2 = deparenthesize($7);
6006 my $arg2 = $8;
6007 my $cast;
6008
6009 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6010 $cast = "$cast1 or $cast2";
6011 } elsif ($cast1 ne "") {
6012 $cast = $cast1;
6013 } else {
6014 $cast = $cast2;
6015 }
6016 WARN("MINMAX",
6017 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6018 }
6019 }
6020
6021# check usleep_range arguments
6022 if ($^V && $^V ge 5.10.0 &&
6023 defined $stat &&
6024 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6025 my $min = $1;
6026 my $max = $7;
6027 if ($min eq $max) {
6028 WARN("USLEEP_RANGE",
6029 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6030 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6031 $min > $max) {
6032 WARN("USLEEP_RANGE",
6033 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6034 }
6035 }
6036
6037# check for naked sscanf
6038 if ($^V && $^V ge 5.10.0 &&
6039 defined $stat &&
6040 $line =~ /\bsscanf\b/ &&
6041 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6042 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6043 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6044 my $lc = $stat =~ tr@\n@@;
6045 $lc = $lc + $linenr;
Martin Roth60915b32018-08-10 21:04:05 -06006046 my $stat_real = get_stat_real($linenr, $lc);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006047 WARN("NAKED_SSCANF",
6048 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6049 }
6050
6051# check for simple sscanf that should be kstrto<foo>
6052 if ($^V && $^V ge 5.10.0 &&
6053 defined $stat &&
6054 $line =~ /\bsscanf\b/) {
6055 my $lc = $stat =~ tr@\n@@;
6056 $lc = $lc + $linenr;
Martin Roth60915b32018-08-10 21:04:05 -06006057 my $stat_real = get_stat_real($linenr, $lc);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006058 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6059 my $format = $6;
6060 my $count = $format =~ tr@%@%@;
6061 if ($count == 1 &&
6062 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6063 WARN("SSCANF_TO_KSTRTO",
6064 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6065 }
6066 }
6067 }
6068
6069# check for new externs in .h files.
6070 if ($realfile =~ /\.h$/ &&
6071 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6072 if (CHK("AVOID_EXTERNS",
6073 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
6074 $fix) {
6075 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
6076 }
6077 }
6078
6079# check for new externs in .c files.
6080 if ($realfile =~ /\.c$/ && defined $stat &&
6081 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6082 {
6083 my $function_name = $1;
6084 my $paren_space = $2;
6085
6086 my $s = $stat;
6087 if (defined $cond) {
6088 substr($s, 0, length($cond), '');
6089 }
6090 if ($s =~ /^\s*;/ &&
6091 $function_name ne 'uninitialized_var')
6092 {
6093 WARN("AVOID_EXTERNS",
6094 "externs should be avoided in .c files\n" . $herecurr);
6095 }
6096
6097 if ($paren_space =~ /\n/) {
6098 WARN("FUNCTION_ARGUMENTS",
6099 "arguments for function declarations should follow identifier\n" . $herecurr);
6100 }
6101
6102 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6103 $stat =~ /^.\s*extern\s+/)
6104 {
6105 WARN("AVOID_EXTERNS",
6106 "externs should be avoided in .c files\n" . $herecurr);
6107 }
6108
Martin Roth387dec82017-09-17 19:20:46 -06006109# check for function declarations that have arguments without identifier names
6110 if (defined $stat &&
Martin Roth60915b32018-08-10 21:04:05 -06006111 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Martin Rothedd591d2017-03-14 10:16:29 -06006112 $1 ne "void") {
6113 my $args = trim($1);
6114 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6115 my $arg = trim($1);
6116 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6117 WARN("FUNCTION_ARGUMENTS",
6118 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6119 }
6120 }
6121 }
6122
Martin Roth387dec82017-09-17 19:20:46 -06006123# check for function definitions
6124 if ($^V && $^V ge 5.10.0 &&
6125 defined $stat &&
6126 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6127 $context_function = $1;
6128
6129# check for multiline function definition with misplaced open brace
6130 my $ok = 0;
6131 my $cnt = statement_rawlines($stat);
6132 my $herectx = $here . "\n";
6133 for (my $n = 0; $n < $cnt; $n++) {
6134 my $rl = raw_line($linenr, $n);
6135 $herectx .= $rl . "\n";
6136 $ok = 1 if ($rl =~ /^[ \+]\{/);
6137 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6138 last if $rl =~ /^[ \+].*\{/;
6139 }
6140 if (!$ok) {
6141 ERROR("OPEN_BRACE",
6142 "open brace '{' following function definitions go on the next line\n" . $herectx);
6143 }
6144 }
6145
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006146# checks for new __setup's
6147 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6148 my $name = $1;
6149
6150 if (!grep(/$name/, @setup_docs)) {
6151 CHK("UNDOCUMENTED_SETUP",
Martin Rothedd591d2017-03-14 10:16:29 -06006152 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006153 }
6154 }
6155
6156# check for pointless casting of kmalloc return
6157 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
6158 WARN("UNNECESSARY_CASTS",
6159 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
6160 }
6161
6162# alloc style
6163# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6164 if ($^V && $^V ge 5.10.0 &&
6165 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6166 CHK("ALLOC_SIZEOF_STRUCT",
6167 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6168 }
6169
6170# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
6171 if ($^V && $^V ge 5.10.0 &&
Martin Roth387dec82017-09-17 19:20:46 -06006172 defined $stat &&
6173 $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 +01006174 my $oldfunc = $3;
6175 my $a1 = $4;
6176 my $a2 = $10;
6177 my $newfunc = "kmalloc_array";
6178 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
6179 my $r1 = $a1;
6180 my $r2 = $a2;
6181 if ($a1 =~ /^sizeof\s*\S/) {
6182 $r1 = $a2;
6183 $r2 = $a1;
6184 }
6185 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6186 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Martin Roth387dec82017-09-17 19:20:46 -06006187 my $cnt = statement_rawlines($stat);
Martin Roth60915b32018-08-10 21:04:05 -06006188 my $herectx = get_stat_here($linenr, $cnt, $here);
6189
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006190 if (WARN("ALLOC_WITH_MULTIPLY",
Martin Roth387dec82017-09-17 19:20:46 -06006191 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6192 $cnt == 1 &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006193 $fix) {
6194 $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 +01006195 }
6196 }
6197 }
6198
6199# check for krealloc arg reuse
6200 if ($^V && $^V ge 5.10.0 &&
6201 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6202 WARN("KREALLOC_ARG_REUSE",
6203 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6204 }
6205
6206# check for alloc argument mismatch
6207 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6208 WARN("ALLOC_ARRAY_ARGS",
6209 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6210 }
6211
6212# check for multiple semicolons
6213 if ($line =~ /;\s*;\s*$/) {
6214 if (WARN("ONE_SEMICOLON",
6215 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6216 $fix) {
6217 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6218 }
6219 }
6220
Martin Rothedd591d2017-03-14 10:16:29 -06006221# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6222 if ($realfile !~ m@^include/uapi/@ &&
6223 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006224 my $ull = "";
6225 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6226 if (CHK("BIT_MACRO",
6227 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6228 $fix) {
6229 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6230 }
6231 }
6232
6233# check for case / default statements not preceded by break/fallthrough/switch
6234 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6235 my $has_break = 0;
6236 my $has_statement = 0;
6237 my $count = 0;
6238 my $prevline = $linenr;
6239 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6240 $prevline--;
6241 my $rline = $rawlines[$prevline - 1];
6242 my $fline = $lines[$prevline - 1];
6243 last if ($fline =~ /^\@\@/);
6244 next if ($fline =~ /^\-/);
6245 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6246 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6247 next if ($fline =~ /^.[\s$;]*$/);
6248 $has_statement = 1;
6249 $count++;
Martin Roth60915b32018-08-10 21:04:05 -06006250 $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 +01006251 }
6252 if (!$has_break && $has_statement) {
6253 WARN("MISSING_BREAK",
Martin Rothedd591d2017-03-14 10:16:29 -06006254 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006255 }
6256 }
6257
6258# check for switch/default statements without a break;
6259 if ($^V && $^V ge 5.10.0 &&
6260 defined $stat &&
6261 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006262 my $cnt = statement_rawlines($stat);
Martin Roth60915b32018-08-10 21:04:05 -06006263 my $herectx = get_stat_here($linenr, $cnt, $here);
6264
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006265 WARN("DEFAULT_NO_BREAK",
6266 "switch default: should use break\n" . $herectx);
6267 }
6268
6269# check for gcc specific __FUNCTION__
6270 if ($line =~ /\b__FUNCTION__\b/) {
6271 if (WARN("USE_FUNC",
6272 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6273 $fix) {
6274 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6275 }
6276 }
6277
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006278# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6279 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6280 ERROR("DATE_TIME",
6281 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6282 }
6283
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006284# check for use of yield()
6285 if ($line =~ /\byield\s*\(\s*\)/) {
6286 WARN("YIELD",
6287 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6288 }
6289
6290# check for comparisons against true and false
6291 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6292 my $lead = $1;
6293 my $arg = $2;
6294 my $test = $3;
6295 my $otype = $4;
6296 my $trail = $5;
6297 my $op = "!";
6298
6299 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6300
6301 my $type = lc($otype);
6302 if ($type =~ /^(?:true|false)$/) {
6303 if (("$test" eq "==" && "$type" eq "true") ||
6304 ("$test" eq "!=" && "$type" eq "false")) {
6305 $op = "";
6306 }
6307
6308 CHK("BOOL_COMPARISON",
6309 "Using comparison to $otype is error prone\n" . $herecurr);
6310
6311## maybe suggesting a correct construct would better
6312## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6313
6314 }
6315 }
6316
Martin Roth60915b32018-08-10 21:04:05 -06006317# check for bool bitfields
6318 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6319 WARN("BOOL_BITFIELD",
6320 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6321 }
6322
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006323# check for semaphores initialized locked
6324 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6325 WARN("CONSIDER_COMPLETION",
6326 "consider using a completion\n" . $herecurr);
6327 }
6328
6329# recommend kstrto* over simple_strto* and strict_strto*
6330 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6331 WARN("CONSIDER_KSTRTO",
6332 "$1 is obsolete, use k$3 instead\n" . $herecurr);
6333 }
6334
6335# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6336 if ($line =~ /^.\s*__initcall\s*\(/) {
6337 WARN("USE_DEVICE_INITCALL",
6338 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6339 }
6340
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006341# check for various structs that are normally const (ops, kgdb, device_tree)
Martin Roth387dec82017-09-17 19:20:46 -06006342# and avoid what seem like struct definitions 'struct foo {'
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006343 if ($line !~ /\bconst\b/ &&
Martin Roth387dec82017-09-17 19:20:46 -06006344 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006345 WARN("CONST_STRUCT",
Martin Roth387dec82017-09-17 19:20:46 -06006346 "struct $1 should normally be const\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006347 }
6348
6349# use of NR_CPUS is usually wrong
6350# ignore definitions of NR_CPUS and usage to define arrays as likely right
6351 if ($line =~ /\bNR_CPUS\b/ &&
6352 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6353 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6354 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6355 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6356 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6357 {
6358 WARN("NR_CPUS",
6359 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6360 }
6361
6362# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6363 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6364 ERROR("DEFINE_ARCH_HAS",
6365 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6366 }
6367
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006368# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6369 if ($^V && $^V ge 5.10.0 &&
6370 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6371 WARN("LIKELY_MISUSE",
6372 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6373 }
6374
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006375# whine mightly about in_atomic
6376 if ($line =~ /\bin_atomic\s*\(/) {
6377 if ($realfile =~ m@^drivers/@) {
6378 ERROR("IN_ATOMIC",
6379 "do not use in_atomic in drivers\n" . $herecurr);
6380 } elsif ($realfile !~ m@^kernel/@) {
6381 WARN("IN_ATOMIC",
6382 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6383 }
6384 }
6385
Martin Rothedd591d2017-03-14 10:16:29 -06006386# check for mutex_trylock_recursive usage
6387 if ($line =~ /mutex_trylock_recursive/) {
6388 ERROR("LOCKING",
6389 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6390 }
6391
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006392# check for lockdep_set_novalidate_class
6393 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6394 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6395 if ($realfile !~ m@^kernel/lockdep@ &&
6396 $realfile !~ m@^include/linux/lockdep@ &&
6397 $realfile !~ m@^drivers/base/core@) {
6398 ERROR("LOCKDEP",
6399 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
6400 }
6401 }
6402
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006403 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6404 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006405 WARN("EXPORTED_WORLD_WRITABLE",
6406 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
6407 }
6408
Martin Roth60915b32018-08-10 21:04:05 -06006409# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6410# and whether or not function naming is typical and if
6411# DEVICE_ATTR permissions uses are unusual too
6412 if ($^V && $^V ge 5.10.0 &&
6413 defined $stat &&
6414 $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*\)/) {
6415 my $var = $1;
6416 my $perms = $2;
6417 my $show = $3;
6418 my $store = $4;
6419 my $octal_perms = perms_to_octal($perms);
6420 if ($show =~ /^${var}_show$/ &&
6421 $store =~ /^${var}_store$/ &&
6422 $octal_perms eq "0644") {
6423 if (WARN("DEVICE_ATTR_RW",
6424 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6425 $fix) {
6426 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6427 }
6428 } elsif ($show =~ /^${var}_show$/ &&
6429 $store =~ /^NULL$/ &&
6430 $octal_perms eq "0444") {
6431 if (WARN("DEVICE_ATTR_RO",
6432 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6433 $fix) {
6434 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6435 }
6436 } elsif ($show =~ /^NULL$/ &&
6437 $store =~ /^${var}_store$/ &&
6438 $octal_perms eq "0200") {
6439 if (WARN("DEVICE_ATTR_WO",
6440 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6441 $fix) {
6442 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6443 }
6444 } elsif ($octal_perms eq "0644" ||
6445 $octal_perms eq "0444" ||
6446 $octal_perms eq "0200") {
6447 my $newshow = "$show";
6448 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6449 my $newstore = $store;
6450 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6451 my $rename = "";
6452 if ($show ne $newshow) {
6453 $rename .= " '$show' to '$newshow'";
6454 }
6455 if ($store ne $newstore) {
6456 $rename .= " '$store' to '$newstore'";
6457 }
6458 WARN("DEVICE_ATTR_FUNCTIONS",
6459 "Consider renaming function(s)$rename\n" . $herecurr);
6460 } else {
6461 WARN("DEVICE_ATTR_PERMS",
6462 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6463 }
6464 }
6465
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006466# Mode permission misuses where it seems decimal should be octal
6467# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Martin Roth60915b32018-08-10 21:04:05 -06006468# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6469# specific definition of not visible in sysfs.
6470# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6471# use the default permissions
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006472 if ($^V && $^V ge 5.10.0 &&
Martin Rothedd591d2017-03-14 10:16:29 -06006473 defined $stat &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006474 $line =~ /$mode_perms_search/) {
6475 foreach my $entry (@mode_permission_funcs) {
6476 my $func = $entry->[0];
6477 my $arg_pos = $entry->[1];
6478
Martin Rothedd591d2017-03-14 10:16:29 -06006479 my $lc = $stat =~ tr@\n@@;
6480 $lc = $lc + $linenr;
Martin Roth60915b32018-08-10 21:04:05 -06006481 my $stat_real = get_stat_real($linenr, $lc);
Martin Rothedd591d2017-03-14 10:16:29 -06006482
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006483 my $skip_args = "";
6484 if ($arg_pos > 1) {
6485 $arg_pos--;
6486 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6487 }
Martin Rothedd591d2017-03-14 10:16:29 -06006488 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6489 if ($stat =~ /$test/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006490 my $val = $1;
6491 $val = $6 if ($skip_args ne "");
Martin Roth60915b32018-08-10 21:04:05 -06006492 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6493 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6494 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006495 ERROR("NON_OCTAL_PERMISSIONS",
Martin Rothedd591d2017-03-14 10:16:29 -06006496 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6497 }
6498 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006499 ERROR("EXPORTED_WORLD_WRITABLE",
Martin Rothedd591d2017-03-14 10:16:29 -06006500 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006501 }
6502 }
6503 }
6504 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006505
Martin Rothedd591d2017-03-14 10:16:29 -06006506# check for uses of S_<PERMS> that could be octal for readability
Martin Roth60915b32018-08-10 21:04:05 -06006507 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
6508 my $oval = $1;
6509 my $octal = perms_to_octal($oval);
Martin Rothedd591d2017-03-14 10:16:29 -06006510 if (WARN("SYMBOLIC_PERMS",
6511 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6512 $fix) {
Martin Roth60915b32018-08-10 21:04:05 -06006513 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Martin Rothedd591d2017-03-14 10:16:29 -06006514 }
6515 }
6516
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006517# validate content of MODULE_LICENSE against list from include/linux/module.h
6518 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6519 my $extracted_string = get_quoted_string($line, $rawline);
6520 my $valid_licenses = qr{
6521 GPL|
6522 GPL\ v2|
6523 GPL\ and\ additional\ rights|
6524 Dual\ BSD/GPL|
6525 Dual\ MIT/GPL|
6526 Dual\ MPL/GPL|
6527 Proprietary
6528 }x;
6529 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6530 WARN("MODULE_LICENSE",
6531 "unknown module license " . $extracted_string . "\n" . $herecurr);
6532 }
6533 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006534 }
6535
6536 # If we have no input at all, then there is nothing to report on
6537 # so just keep quiet.
6538 if ($#rawlines == -1) {
6539 exit(0);
6540 }
6541
6542 # In mailback mode only produce a report in the negative, for
6543 # things that appear to be patches.
6544 if ($mailback && ($clean == 1 || !$is_patch)) {
6545 exit(0);
6546 }
6547
6548 # This is not a patch, and we are are in 'no-patch' mode so
6549 # just keep quiet.
6550 if (!$chk_patch && !$is_patch) {
6551 exit(0);
6552 }
6553
Martin Roth60915b32018-08-10 21:04:05 -06006554 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006555 ERROR("NOT_UNIFIED_DIFF",
6556 "Does not appear to be a unified-diff format patch\n");
6557 }
Martin Rothedd591d2017-03-14 10:16:29 -06006558 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006559 ERROR("MISSING_SIGN_OFF",
6560 "Missing Signed-off-by: line(s)\n");
6561 }
6562
6563 print report_dump();
6564 if ($summary && !($clean == 1 && $quiet == 1)) {
6565 print "$filename " if ($summary_file);
6566 print "total: $cnt_error errors, $cnt_warn warnings, " .
6567 (($check)? "$cnt_chk checks, " : "") .
6568 "$cnt_lines lines checked\n";
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006569 }
6570
6571 if ($quiet == 0) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006572 # If there were any defects found and not already fixing them
6573 if (!$clean and !$fix) {
6574 print << "EOM"
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006575
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006576NOTE: For some of the reported defects, checkpatch may be able to
6577 mechanically convert to the typical style using --fix or --fix-inplace.
6578EOM
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006579 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006580 # If there were whitespace errors which cleanpatch can fix
6581 # then suggest that.
6582 if ($rpt_cleaners) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006583 $rpt_cleaners = 0;
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006584 print << "EOM"
6585
6586NOTE: Whitespace errors detected.
6587 You may wish to use scripts/cleanpatch or scripts/cleanfile
6588EOM
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006589 }
6590 }
6591
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006592 if ($clean == 0 && $fix &&
6593 ("@rawlines" ne "@fixed" ||
6594 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
6595 my $newfile = $filename;
6596 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
6597 my $linecount = 0;
6598 my $f;
6599
6600 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6601
6602 open($f, '>', $newfile)
6603 or die "$P: Can't open $newfile for write\n";
6604 foreach my $fixed_line (@fixed) {
6605 $linecount++;
6606 if ($file) {
6607 if ($linecount > 3) {
6608 $fixed_line =~ s/^\+//;
6609 print $f $fixed_line . "\n";
6610 }
6611 } else {
6612 print $f $fixed_line . "\n";
6613 }
6614 }
6615 close($f);
6616
6617 if (!$quiet) {
6618 print << "EOM";
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006619
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006620Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6621
6622Do _NOT_ trust the results written to this file.
6623Do _NOT_ submit these changes without inspecting them for correctness.
6624
6625This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6626No warranties, expressed or implied...
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006627EOM
6628 }
6629 }
6630
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006631 if ($quiet == 0) {
6632 print "\n";
6633 if ($clean == 1) {
6634 print "$vname has no obvious style problems and is ready for submission.\n";
6635 } else {
6636 print "$vname has style problems, please review.\n";
6637 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006638 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006639 return $clean;
6640}