blob: edba36565167b38bffbe2e1f0a09df60b7deb3e9 [file] [log] [blame]
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001#!/usr/bin/env perl
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002# SPDX-License-Identifier: GPL-2.0
3#
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004# (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>
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02008# (c) 2010-2018 Joe Perches <joe@perches.com>
Joe Hershbergerba2beb52011-10-18 10:06:59 +00009
10use strict;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020011use warnings;
Tom Rinia50f8de2014-02-27 08:27:28 -050012use POSIX;
Dan Murphy463f6882017-01-31 14:15:53 -060013use File::Basename;
14use Cwd 'abs_path';
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020015use Term::ANSIColor qw(:constants);
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +020016use Encode qw(decode encode);
Joe Hershbergerba2beb52011-10-18 10:06:59 +000017
18my $P = $0;
Dan Murphy463f6882017-01-31 14:15:53 -060019my $D = dirname(abs_path($P));
Joe Hershbergerba2beb52011-10-18 10:06:59 +000020
21my $V = '0.32';
22
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
29my $tst_only;
30my $emacs = 0;
31my $terse = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020032my $showfile = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +000033my $file = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020034my $git = 0;
35my %git_commits = ();
Joe Hershbergerba2beb52011-10-18 10:06:59 +000036my $check = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020037my $check_orig = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +000038my $summary = 1;
39my $mailback = 0;
40my $summary_file = 0;
41my $show_types = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020042my $list_types = 0;
Tom Rinia50f8de2014-02-27 08:27:28 -050043my $fix = 0;
44my $fix_inplace = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +000045my $root;
46my %debug;
Tom Rinia50f8de2014-02-27 08:27:28 -050047my %camelcase = ();
48my %use_type = ();
49my @use = ();
Joe Hershbergerba2beb52011-10-18 10:06:59 +000050my %ignore_type = ();
51my @ignore = ();
52my $help = 0;
53my $configuration_file = ".checkpatch.conf";
Simon Glassa33d9822020-05-22 16:32:35 -060054my $max_line_length = 100;
Tom Rinia50f8de2014-02-27 08:27:28 -050055my $ignore_perl_version = 0;
56my $minimum_perl_version = 5.10.0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020057my $min_conf_desc_length = 4;
Dan Murphy463f6882017-01-31 14:15:53 -060058my $spelling_file = "$D/spelling.txt";
59my $codespell = 0;
60my $codespellfile = "/usr/share/codespell/dictionary.txt";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020061my $conststructsfile = "$D/const_structs.checkpatch";
62my $typedefsfile = "";
Simon Glassc8f4d092020-05-22 16:32:36 -060063my $u_boot = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020064my $color = "auto";
Simon Glassa33d9822020-05-22 16:32:35 -060065my $allow_c99_comments = 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +000066
67sub help {
68 my ($exitcode) = @_;
69
70 print << "EOM";
71Usage: $P [OPTION]... [FILE]...
72Version: $V
73
74Options:
75 -q, --quiet quiet
76 --no-tree run without a kernel tree
77 --no-signoff do not check for 'Signed-off-by' line
78 --patch treat FILE as patchfile (default)
79 --emacs emacs compile window format
80 --terse one line per report
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020081 --showfile emit diffed file position, not input file position
82 -g, --git treat FILE as a single commit or git revision range
83 single git commit with:
84 <rev>
85 <rev>^
86 <rev>~n
87 multiple git commits with:
88 <rev1>..<rev2>
89 <rev1>...<rev2>
90 <rev>-<count>
91 git merges are ignored
Joe Hershbergerba2beb52011-10-18 10:06:59 +000092 -f, --file treat FILE as regular source file
93 --subjective, --strict enable more subjective tests
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020094 --list-types list the possible message types
Tom Rinia50f8de2014-02-27 08:27:28 -050095 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Hershbergerba2beb52011-10-18 10:06:59 +000096 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020097 --show-types show the specific message type in the output
Simon Glassa33d9822020-05-22 16:32:35 -060098 --max-line-length=n set the maximum line length, (default $max_line_length)
99 if exceeded, warn on patches
100 requires --strict for use with --file
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200101 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000102 --root=PATH PATH to the kernel tree root
103 --no-summary suppress the per-file summary
104 --mailback only produce a report in case of warnings/errors
105 --summary-file include the filename in summary
106 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
107 'values', 'possible', 'type', and 'attr' (default
108 is all off)
109 --test-only=WORD report only warnings/errors containing WORD
110 literally
Tom Rinia50f8de2014-02-27 08:27:28 -0500111 --fix EXPERIMENTAL - may create horrible results
112 If correctable single-line errors exist, create
113 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
114 with potential errors corrected to the preferred
115 checkpatch style
116 --fix-inplace EXPERIMENTAL - may create horrible results
117 Is the same as --fix, but overwrites the input
118 file. It's your fault if there's no backup or git
119 --ignore-perl-version override checking of perl version. expect
120 runtime errors.
Dan Murphy463f6882017-01-31 14:15:53 -0600121 --codespell Use the codespell dictionary for spelling/typos
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200122 (default:/usr/share/codespell/dictionary.txt)
Dan Murphy463f6882017-01-31 14:15:53 -0600123 --codespellfile Use this codespell dictionary
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200124 --typedefsfile Read additional types from this file
125 --color[=WHEN] Use colors 'always', 'never', or only when output
126 is a terminal ('auto'). Default is 'auto'.
Simon Glassc8f4d092020-05-22 16:32:36 -0600127 --u-boot Run additional checks for U-Boot
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000128 -h, --help, --version display this help and exit
129
130When FILE is - read standard input.
131EOM
132
133 exit($exitcode);
134}
135
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200136sub uniq {
137 my %seen;
138 return grep { !$seen{$_}++ } @_;
139}
140
141sub list_types {
142 my ($exitcode) = @_;
143
144 my $count = 0;
145
146 local $/ = undef;
147
148 open(my $script, '<', abs_path($P)) or
149 die "$P: Can't read '$P' $!\n";
150
151 my $text = <$script>;
152 close($script);
153
154 my @types = ();
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +0200155 # Also catch when type or level is passed through a variable
156 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200157 push (@types, $_);
158 }
159 @types = sort(uniq(@types));
160 print("#\tMessage type\n\n");
161 foreach my $type (@types) {
162 print(++$count . "\t" . $type . "\n");
163 }
164
165 exit($exitcode);
166}
167
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000168my $conf = which_conf($configuration_file);
169if (-f $conf) {
170 my @conf_args;
171 open(my $conffile, '<', "$conf")
172 or warn "$P: Can't find a readable $configuration_file file $!\n";
173
174 while (<$conffile>) {
175 my $line = $_;
176
177 $line =~ s/\s*\n?$//g;
178 $line =~ s/^\s*//g;
179 $line =~ s/\s+/ /g;
180
181 next if ($line =~ m/^\s*#/);
182 next if ($line =~ m/^\s*$/);
183
184 my @words = split(" ", $line);
185 foreach my $word (@words) {
186 last if ($word =~ m/^#/);
187 push (@conf_args, $word);
188 }
189 }
190 close($conffile);
191 unshift(@ARGV, @conf_args) if @conf_args;
192}
193
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200194# Perl's Getopt::Long allows options to take optional arguments after a space.
195# Prevent --color by itself from consuming other arguments
196foreach (@ARGV) {
197 if ($_ eq "--color" || $_ eq "-color") {
198 $_ = "--color=$color";
199 }
200}
201
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000202GetOptions(
203 'q|quiet+' => \$quiet,
204 'tree!' => \$tree,
205 'signoff!' => \$chk_signoff,
206 'patch!' => \$chk_patch,
207 'emacs!' => \$emacs,
208 'terse!' => \$terse,
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200209 'showfile!' => \$showfile,
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000210 'f|file!' => \$file,
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200211 'g|git!' => \$git,
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000212 'subjective!' => \$check,
213 'strict!' => \$check,
214 'ignore=s' => \@ignore,
Tom Rinia50f8de2014-02-27 08:27:28 -0500215 'types=s' => \@use,
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000216 'show-types!' => \$show_types,
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200217 'list-types!' => \$list_types,
Kim Phillips9b7b8372013-02-28 12:53:52 +0000218 'max-line-length=i' => \$max_line_length,
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200219 'min-conf-desc-length=i' => \$min_conf_desc_length,
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000220 'root=s' => \$root,
221 'summary!' => \$summary,
222 'mailback!' => \$mailback,
223 'summary-file!' => \$summary_file,
Tom Rinia50f8de2014-02-27 08:27:28 -0500224 'fix!' => \$fix,
225 'fix-inplace!' => \$fix_inplace,
226 'ignore-perl-version!' => \$ignore_perl_version,
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000227 'debug=s' => \%debug,
228 'test-only=s' => \$tst_only,
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200229 'codespell!' => \$codespell,
230 'codespellfile=s' => \$codespellfile,
231 'typedefsfile=s' => \$typedefsfile,
Simon Glassc8f4d092020-05-22 16:32:36 -0600232 'u-boot' => \$u_boot,
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200233 'color=s' => \$color,
234 'no-color' => \$color, #keep old behaviors of -nocolor
235 'nocolor' => \$color, #keep old behaviors of -nocolor
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000236 'h|help' => \$help,
237 'version' => \$help
238) or help(1);
239
240help(0) if ($help);
241
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200242list_types(0) if ($list_types);
243
Tom Rinia50f8de2014-02-27 08:27:28 -0500244$fix = 1 if ($fix_inplace);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200245$check_orig = $check;
Tom Rinia50f8de2014-02-27 08:27:28 -0500246
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000247my $exit = 0;
248
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +0200249my $perl_version_ok = 1;
Tom Rinia50f8de2014-02-27 08:27:28 -0500250if ($^V && $^V lt $minimum_perl_version) {
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +0200251 $perl_version_ok = 0;
Tom Rinia50f8de2014-02-27 08:27:28 -0500252 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +0200253 exit(1) if (!$ignore_perl_version);
Tom Rinia50f8de2014-02-27 08:27:28 -0500254}
255
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200256#if no filenames are given, push '-' to read patch from stdin
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000257if ($#ARGV < 0) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200258 push(@ARGV, '-');
259}
260
261if ($color =~ /^[01]$/) {
262 $color = !$color;
263} elsif ($color =~ /^always$/i) {
264 $color = 1;
265} elsif ($color =~ /^never$/i) {
266 $color = 0;
267} elsif ($color =~ /^auto$/i) {
268 $color = (-t STDOUT);
269} else {
270 die "Invalid color mode: $color\n";
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000271}
272
Tom Rinia50f8de2014-02-27 08:27:28 -0500273sub hash_save_array_words {
274 my ($hashRef, $arrayRef) = @_;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000275
Tom Rinia50f8de2014-02-27 08:27:28 -0500276 my @array = split(/,/, join(',', @$arrayRef));
277 foreach my $word (@array) {
278 $word =~ s/\s*\n?$//g;
279 $word =~ s/^\s*//g;
280 $word =~ s/\s+/ /g;
281 $word =~ tr/[a-z]/[A-Z]/;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000282
Tom Rinia50f8de2014-02-27 08:27:28 -0500283 next if ($word =~ m/^\s*#/);
284 next if ($word =~ m/^\s*$/);
285
286 $hashRef->{$word}++;
287 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000288}
289
Tom Rinia50f8de2014-02-27 08:27:28 -0500290sub hash_show_words {
291 my ($hashRef, $prefix) = @_;
292
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200293 if (keys %$hashRef) {
294 print "\nNOTE: $prefix message types:";
Tom Rinia50f8de2014-02-27 08:27:28 -0500295 foreach my $word (sort keys %$hashRef) {
296 print " $word";
297 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200298 print "\n";
Tom Rinia50f8de2014-02-27 08:27:28 -0500299 }
300}
301
302hash_save_array_words(\%ignore_type, \@ignore);
303hash_save_array_words(\%use_type, \@use);
304
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000305my $dbg_values = 0;
306my $dbg_possible = 0;
307my $dbg_type = 0;
308my $dbg_attr = 0;
309for my $key (keys %debug) {
310 ## no critic
311 eval "\${dbg_$key} = '$debug{$key}';";
312 die "$@" if ($@);
313}
314
315my $rpt_cleaners = 0;
316
317if ($terse) {
318 $emacs = 1;
319 $quiet++;
320}
321
322if ($tree) {
323 if (defined $root) {
324 if (!top_of_kernel_tree($root)) {
325 die "$P: $root: --root does not point at a valid tree\n";
326 }
327 } else {
328 if (top_of_kernel_tree('.')) {
329 $root = '.';
330 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
331 top_of_kernel_tree($1)) {
332 $root = $1;
333 }
334 }
335
336 if (!defined $root) {
337 print "Must be run from the top-level dir. of a kernel tree\n";
338 exit(2);
339 }
340}
341
342my $emitted_corrupt = 0;
343
344our $Ident = qr{
345 [A-Za-z_][A-Za-z\d_]*
346 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
347 }x;
348our $Storage = qr{extern|static|asmlinkage};
349our $Sparse = qr{
350 __user|
351 __kernel|
352 __force|
353 __iomem|
354 __must_check|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000355 __kprobes|
356 __ref|
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +0200357 __refconst|
358 __refdata|
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200359 __rcu|
360 __private
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000361 }x;
Tom Rinia50f8de2014-02-27 08:27:28 -0500362our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
363our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
364our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
365our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
366our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000367
368# Notes to $Attribute:
369# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
370our $Attribute = qr{
371 const|
372 __percpu|
373 __nocast|
374 __safe|
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200375 __bitwise|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000376 __packed__|
377 __packed2__|
378 __naked|
379 __maybe_unused|
380 __always_unused|
381 __noreturn|
382 __used|
383 __cold|
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200384 __pure|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000385 __noclone|
386 __deprecated|
387 __read_mostly|
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +0200388 __ro_after_init|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000389 __kprobes|
Tom Rinia50f8de2014-02-27 08:27:28 -0500390 $InitAttribute|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000391 ____cacheline_aligned|
392 ____cacheline_aligned_in_smp|
393 ____cacheline_internodealigned_in_smp|
394 __weak
395 }x;
396our $Modifier;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200397our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000398our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
399our $Lval = qr{$Ident(?:$Member)*};
400
Tom Rinia50f8de2014-02-27 08:27:28 -0500401our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
402our $Binary = qr{(?i)0b[01]+$Int_type?};
403our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
404our $Int = qr{[0-9]+$Int_type?};
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200405our $Octal = qr{0[0-7]+$Int_type?};
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +0200406our $String = qr{"[X\t]*"};
Kim Phillips9b7b8372013-02-28 12:53:52 +0000407our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
408our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
409our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
410our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200411our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Kim Phillips9b7b8372013-02-28 12:53:52 +0000412our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200413our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Tom Rinia50f8de2014-02-27 08:27:28 -0500414our $Arithmetic = qr{\+|-|\*|\/|%};
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000415our $Operators = qr{
416 <=|>=|==|!=|
417 =>|->|<<|>>|<|>|!|~|
Tom Rinia50f8de2014-02-27 08:27:28 -0500418 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000419 }x;
420
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200421our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
422
423our $BasicType;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000424our $NonptrType;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200425our $NonptrTypeMisordered;
Tom Rinia50f8de2014-02-27 08:27:28 -0500426our $NonptrTypeWithAttr;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000427our $Type;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200428our $TypeMisordered;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000429our $Declare;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200430our $DeclareMisordered;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000431
Kim Phillips9b7b8372013-02-28 12:53:52 +0000432our $NON_ASCII_UTF8 = qr{
433 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000434 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
435 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
436 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
437 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
438 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
439 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
440}x;
441
Kim Phillips9b7b8372013-02-28 12:53:52 +0000442our $UTF8 = qr{
443 [\x09\x0A\x0D\x20-\x7E] # ASCII
444 | $NON_ASCII_UTF8
445}x;
446
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200447our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
448our $typeOtherOSTypedefs = qr{(?x:
449 u_(?:char|short|int|long) | # bsd
450 u(?:nchar|short|int|long) # sysv
451)};
452our $typeKernelTypedefs = qr{(?x:
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000453 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
454 atomic_t
455)};
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200456our $typeTypedefs = qr{(?x:
457 $typeC99Typedefs\b|
458 $typeOtherOSTypedefs\b|
459 $typeKernelTypedefs\b
460)};
461
462our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000463
464our $logFunctions = qr{(?x:
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200465 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Tom Rinia50f8de2014-02-27 08:27:28 -0500466 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +0200467 TP_printk|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000468 WARN(?:_RATELIMIT|_ONCE|)|
469 panic|
James Byrnea6859792019-11-21 14:32:46 +0000470 debug|
471 printf|
Tom Rinia50f8de2014-02-27 08:27:28 -0500472 MODULE_[A-Z_]+|
473 seq_vprintf|seq_printf|seq_puts
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000474)};
475
476our $signature_tags = qr{(?xi:
477 Signed-off-by:|
478 Acked-by:|
479 Tested-by:|
480 Reviewed-by:|
481 Reported-by:|
Tom Rinia50f8de2014-02-27 08:27:28 -0500482 Suggested-by:|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000483 To:|
484 Cc:
485)};
486
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200487our @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
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000506our @typeList = (
507 qr{void},
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200508 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},
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000517 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},
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200526 @typeListMisordered,
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000527);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200528
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 = ();
Tom Rinia50f8de2014-02-27 08:27:28 -0500551our @typeListWithAttr = (
552 @typeList,
553 qr{struct\s+$InitAttribute\s+$Ident},
554 qr{union\s+$InitAttribute\s+$Ident},
555);
556
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000557our @modifierList = (
558 qr{fastcall},
559);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200560our @modifierListFile = ();
561
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],
568 ["(?: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],
573);
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}
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +0200581$mode_perms_search = "(?:${mode_perms_search})";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200582
583our $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
591our %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);
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000609
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200610#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}
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +0200616our $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}
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200647
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000648our $allowed_asm_includes = qr{(?x:
649 irq|
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200650 memory|
651 time|
652 reboot
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000653)};
654# memory.h: ARM has a custom one
655
Dan Murphy463f6882017-01-31 14:15:53 -0600656# Load common spelling mistakes and build regular expression list.
657my $misspellings;
658my %spelling_fix;
659
660if (open(my $spelling, '<', $spelling_file)) {
661 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
672 $spelling_fix{$suspect} = $fix;
673 }
674 close($spelling);
675} else {
676 warn "No typos will be found - file '$spelling_file': $!\n";
677}
678
679if ($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
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200705sub read_words {
706 my ($wordsRef, $file) = @_;
707
708 if (open(my $words, '<', $file)) {
709 while (<$words>) {
710 my $line = $_;
711
712 $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;
724 }
725 close($file);
726 return 1;
727 }
728
729 return 0;
730}
731
732my $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 "");
Dan Murphy463f6882017-01-31 14:15:53 -0600742
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000743sub build_types {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200744 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
745 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
746 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Tom Rinia50f8de2014-02-27 08:27:28 -0500747 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000748 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200749 $BasicType = qr{
750 (?:$typeTypedefs\b)|
751 (?:${all}\b)
752 }x;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000753 $NonptrType = qr{
754 (?:$Modifier\s+|const\s+)*
755 (?:
Kim Phillips9b7b8372013-02-28 12:53:52 +0000756 (?:typeof|__typeof__)\s*\([^\)]*\)|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000757 (?:$typeTypedefs\b)|
758 (?:${all}\b)
759 )
760 (?:\s+$Modifier|\s+const)*
761 }x;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200762 $NonptrTypeMisordered = qr{
763 (?:$Modifier\s+|const\s+)*
764 (?:
765 (?:${Misordered}\b)
766 )
767 (?:\s+$Modifier|\s+const)*
768 }x;
Tom Rinia50f8de2014-02-27 08:27:28 -0500769 $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;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000778 $Type = qr{
779 $NonptrType
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200780 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000781 (?:\s+$Inline|\s+$Modifier)*
782 }x;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200783 $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};
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000790}
791build_types();
792
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000793our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Kim Phillips9b7b8372013-02-28 12:53:52 +0000794
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))*\))/;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200800our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
801our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
802
803our $declaration_macros = qr{(?x:
804 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
805 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +0200806 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
807 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200808)};
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000809
810sub deparenthesize {
811 my ($string) = @_;
812 return "" if (!defined($string));
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200813
814 while ($string =~ /^\s*\(.*\)\s*$/) {
815 $string =~ s@^\s*\(\s*@@;
816 $string =~ s@\s*\)\s*$@@;
817 }
818
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000819 $string =~ s@\s+@ @g;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200820
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000821 return $string;
822}
823
Tom Rinia50f8de2014-02-27 08:27:28 -0500824sub 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
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200850sub 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
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +0200860sub is_SPDX_License_valid {
861 my ($license) = @_;
862
863 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
864
865 my $root_path = abs_path($root);
866 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
867 return 0 if ($status ne "");
868 return 1;
869}
870
Tom Rinia50f8de2014-02-27 08:27:28 -0500871my $camelcase_seeded = 0;
872sub seed_camelcase_includes {
873 return if ($camelcase_seeded);
874
875 my $files;
876 my $camelcase_cache = "";
877 my @include_files = ();
878
879 $camelcase_seeded = 1;
880
881 if (-e ".git") {
Simon Glassa33d9822020-05-22 16:32:35 -0600882 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
Tom Rinia50f8de2014-02-27 08:27:28 -0500883 chomp $git_last_include_commit;
884 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
885 } else {
886 my $last_mod_date = 0;
887 $files = `find $root/include -name "*.h"`;
888 @include_files = split('\n', $files);
889 foreach my $file (@include_files) {
890 my $date = POSIX::strftime("%Y%m%d%H%M",
891 localtime((stat $file)[9]));
892 $last_mod_date = $date if ($last_mod_date < $date);
893 }
894 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
895 }
896
897 if ($camelcase_cache ne "" && -f $camelcase_cache) {
898 open(my $camelcase_file, '<', "$camelcase_cache")
899 or warn "$P: Can't read '$camelcase_cache' $!\n";
900 while (<$camelcase_file>) {
901 chomp;
902 $camelcase{$_} = 1;
903 }
904 close($camelcase_file);
905
906 return;
907 }
908
909 if (-e ".git") {
Simon Glassa33d9822020-05-22 16:32:35 -0600910 $files = `git ls-files "include/*.h"`;
Tom Rinia50f8de2014-02-27 08:27:28 -0500911 @include_files = split('\n', $files);
912 }
913
914 foreach my $file (@include_files) {
915 seed_camelcase_file($file);
916 }
917
918 if ($camelcase_cache ne "") {
919 unlink glob ".checkpatch-camelcase.*";
920 open(my $camelcase_file, '>', "$camelcase_cache")
921 or warn "$P: Can't write '$camelcase_cache' $!\n";
922 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
923 print $camelcase_file ("$_\n");
924 }
925 close($camelcase_file);
926 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200927}
928
929sub git_commit_info {
930 my ($commit, $id, $desc) = @_;
931
932 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
933
Simon Glassa33d9822020-05-22 16:32:35 -0600934 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200935 $output =~ s/^\s*//gm;
936 my @lines = split("\n", $output);
937
938 return ($id, $desc) if ($#lines < 0);
939
Simon Glassa33d9822020-05-22 16:32:35 -0600940 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200941# Maybe one day convert this block of bash into something that returns
942# all matching commit ids, but it's very slow...
943#
944# echo "checking commits $1..."
945# git rev-list --remotes | grep -i "^$1" |
946# while read line ; do
947# git log --format='%H %s' -1 $line |
948# echo "commit $(cut -c 1-12,41-)"
949# done
950 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
951 $id = undef;
952 } else {
953 $id = substr($lines[0], 0, 12);
954 $desc = substr($lines[0], 41);
955 }
956
957 return ($id, $desc);
Tom Rinia50f8de2014-02-27 08:27:28 -0500958}
959
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000960$chk_signoff = 0 if ($file);
961
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000962my @rawlines = ();
963my @lines = ();
Tom Rinia50f8de2014-02-27 08:27:28 -0500964my @fixed = ();
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200965my @fixed_inserted = ();
966my @fixed_deleted = ();
Dan Murphy463f6882017-01-31 14:15:53 -0600967my $fixlinenr = -1;
968
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200969# If input is git commits, extract all commits from the commit expressions.
970# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
971die "$P: No git repository found\n" if ($git && !-e ".git");
972
973if ($git) {
974 my @commits = ();
975 foreach my $commit_expr (@ARGV) {
976 my $git_range;
977 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
978 $git_range = "-$2 $1";
979 } elsif ($commit_expr =~ m/\.\./) {
980 $git_range = "$commit_expr";
981 } else {
982 $git_range = "-1 $commit_expr";
983 }
Simon Glassa33d9822020-05-22 16:32:35 -0600984 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200985 foreach my $line (split(/\n/, $lines)) {
986 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
987 next if (!defined($1) || !defined($2));
988 my $sha1 = $1;
989 my $subject = $2;
990 unshift(@commits, $sha1);
991 $git_commits{$sha1} = $subject;
992 }
993 }
994 die "$P: no git commits after extraction!\n" if (@commits == 0);
995 @ARGV = @commits;
996}
997
998my $vname;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000999for my $filename (@ARGV) {
1000 my $FILE;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001001 if ($git) {
1002 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1003 die "$P: $filename: git format-patch failed - $!\n";
1004 } elsif ($file) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001005 open($FILE, '-|', "diff -u /dev/null $filename") ||
1006 die "$P: $filename: diff failed - $!\n";
1007 } elsif ($filename eq '-') {
1008 open($FILE, '<&STDIN');
1009 } else {
1010 open($FILE, '<', "$filename") ||
1011 die "$P: $filename: open failed - $!\n";
1012 }
1013 if ($filename eq '-') {
1014 $vname = 'Your patch';
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001015 } elsif ($git) {
1016 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001017 } else {
1018 $vname = $filename;
1019 }
1020 while (<$FILE>) {
1021 chomp;
1022 push(@rawlines, $_);
1023 }
1024 close($FILE);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001025
1026 if ($#ARGV > 0 && $quiet == 0) {
1027 print '-' x length($vname) . "\n";
1028 print "$vname\n";
1029 print '-' x length($vname) . "\n";
1030 }
1031
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001032 if (!process($filename)) {
1033 $exit = 1;
1034 }
1035 @rawlines = ();
1036 @lines = ();
Tom Rinia50f8de2014-02-27 08:27:28 -05001037 @fixed = ();
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001038 @fixed_inserted = ();
1039 @fixed_deleted = ();
1040 $fixlinenr = -1;
1041 @modifierListFile = ();
1042 @typeListFile = ();
1043 build_types();
1044}
1045
1046if (!$quiet) {
1047 hash_show_words(\%use_type, "Used");
1048 hash_show_words(\%ignore_type, "Ignored");
1049
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02001050 if (!$perl_version_ok) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001051 print << "EOM"
1052
1053NOTE: perl $^V is not modern enough to detect all possible issues.
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02001054 An upgrade to at least perl $minimum_perl_version is suggested.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001055EOM
1056 }
1057 if ($exit) {
1058 print << "EOM"
1059
1060NOTE: If any of the errors are false positives, please report
1061 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1062EOM
1063 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001064}
1065
1066exit($exit);
1067
1068sub top_of_kernel_tree {
1069 my ($root) = @_;
1070
1071 my @tree_check = (
Tom Rinia50f8de2014-02-27 08:27:28 -05001072 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001073 "README", "Documentation", "arch", "include", "drivers",
1074 "fs", "init", "ipc", "kernel", "lib", "scripts",
1075 );
1076
1077 foreach my $check (@tree_check) {
1078 if (! -e $root . '/' . $check) {
1079 return 0;
1080 }
1081 }
1082 return 1;
Kim Phillips9b7b8372013-02-28 12:53:52 +00001083}
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001084
1085sub parse_email {
1086 my ($formatted_email) = @_;
1087
1088 my $name = "";
1089 my $address = "";
1090 my $comment = "";
1091
1092 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1093 $name = $1;
1094 $address = $2;
1095 $comment = $3 if defined $3;
1096 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1097 $address = $1;
1098 $comment = $2 if defined $2;
1099 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1100 $address = $1;
1101 $comment = $2 if defined $2;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02001102 $formatted_email =~ s/\Q$address\E.*$//;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001103 $name = $formatted_email;
Tom Rinia50f8de2014-02-27 08:27:28 -05001104 $name = trim($name);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001105 $name =~ s/^\"|\"$//g;
1106 # If there's a name left after stripping spaces and
1107 # leading quotes, and the address doesn't have both
1108 # leading and trailing angle brackets, the address
1109 # is invalid. ie:
1110 # "joe smith joe@smith.com" bad
1111 # "joe smith <joe@smith.com" bad
1112 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1113 $name = "";
1114 $address = "";
1115 $comment = "";
1116 }
1117 }
1118
Tom Rinia50f8de2014-02-27 08:27:28 -05001119 $name = trim($name);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001120 $name =~ s/^\"|\"$//g;
Tom Rinia50f8de2014-02-27 08:27:28 -05001121 $address = trim($address);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001122 $address =~ s/^\<|\>$//g;
1123
1124 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1125 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1126 $name = "\"$name\"";
1127 }
1128
1129 return ($name, $address, $comment);
1130}
1131
1132sub format_email {
1133 my ($name, $address) = @_;
1134
1135 my $formatted_email;
1136
Tom Rinia50f8de2014-02-27 08:27:28 -05001137 $name = trim($name);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001138 $name =~ s/^\"|\"$//g;
Tom Rinia50f8de2014-02-27 08:27:28 -05001139 $address = trim($address);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001140
1141 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1142 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1143 $name = "\"$name\"";
1144 }
1145
1146 if ("$name" eq "") {
1147 $formatted_email = "$address";
1148 } else {
1149 $formatted_email = "$name <$address>";
1150 }
1151
1152 return $formatted_email;
1153}
1154
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001155sub which {
1156 my ($bin) = @_;
1157
1158 foreach my $path (split(/:/, $ENV{PATH})) {
1159 if (-e "$path/$bin") {
1160 return "$path/$bin";
1161 }
1162 }
1163
1164 return "";
1165}
1166
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001167sub which_conf {
1168 my ($conf) = @_;
1169
1170 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1171 if (-e "$path/$conf") {
1172 return "$path/$conf";
1173 }
1174 }
1175
1176 return "";
1177}
1178
1179sub expand_tabs {
1180 my ($str) = @_;
1181
1182 my $res = '';
1183 my $n = 0;
1184 for my $c (split(//, $str)) {
1185 if ($c eq "\t") {
1186 $res .= ' ';
1187 $n++;
1188 for (; ($n % 8) != 0; $n++) {
1189 $res .= ' ';
1190 }
1191 next;
1192 }
1193 $res .= $c;
1194 $n++;
1195 }
1196
1197 return $res;
1198}
1199sub copy_spacing {
1200 (my $res = shift) =~ tr/\t/ /c;
1201 return $res;
1202}
1203
1204sub line_stats {
1205 my ($line) = @_;
1206
1207 # Drop the diff line leader and expand tabs
1208 $line =~ s/^.//;
1209 $line = expand_tabs($line);
1210
1211 # Pick the indent from the front of the line.
1212 my ($white) = ($line =~ /^(\s*)/);
1213
1214 return (length($line), length($white));
1215}
1216
1217my $sanitise_quote = '';
1218
1219sub sanitise_line_reset {
1220 my ($in_comment) = @_;
1221
1222 if ($in_comment) {
1223 $sanitise_quote = '*/';
1224 } else {
1225 $sanitise_quote = '';
1226 }
1227}
1228sub sanitise_line {
1229 my ($line) = @_;
1230
1231 my $res = '';
1232 my $l = '';
1233
1234 my $qlen = 0;
1235 my $off = 0;
1236 my $c;
1237
1238 # Always copy over the diff marker.
1239 $res = substr($line, 0, 1);
1240
1241 for ($off = 1; $off < length($line); $off++) {
1242 $c = substr($line, $off, 1);
1243
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02001244 # Comments we are whacking completely including the begin
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001245 # and end, all to $;.
1246 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1247 $sanitise_quote = '*/';
1248
1249 substr($res, $off, 2, "$;$;");
1250 $off++;
1251 next;
1252 }
1253 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1254 $sanitise_quote = '';
1255 substr($res, $off, 2, "$;$;");
1256 $off++;
1257 next;
1258 }
1259 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1260 $sanitise_quote = '//';
1261
1262 substr($res, $off, 2, $sanitise_quote);
1263 $off++;
1264 next;
1265 }
1266
1267 # A \ in a string means ignore the next character.
1268 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1269 $c eq "\\") {
1270 substr($res, $off, 2, 'XX');
1271 $off++;
1272 next;
1273 }
1274 # Regular quotes.
1275 if ($c eq "'" || $c eq '"') {
1276 if ($sanitise_quote eq '') {
1277 $sanitise_quote = $c;
1278
1279 substr($res, $off, 1, $c);
1280 next;
1281 } elsif ($sanitise_quote eq $c) {
1282 $sanitise_quote = '';
1283 }
1284 }
1285
1286 #print "c<$c> SQ<$sanitise_quote>\n";
1287 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1288 substr($res, $off, 1, $;);
1289 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1290 substr($res, $off, 1, $;);
1291 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1292 substr($res, $off, 1, 'X');
1293 } else {
1294 substr($res, $off, 1, $c);
1295 }
1296 }
1297
1298 if ($sanitise_quote eq '//') {
1299 $sanitise_quote = '';
1300 }
1301
1302 # The pathname on a #include may be surrounded by '<' and '>'.
1303 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1304 my $clean = 'X' x length($1);
1305 $res =~ s@\<.*\>@<$clean>@;
1306
1307 # The whole of a #error is a string.
1308 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1309 my $clean = 'X' x length($1);
1310 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1311 }
1312
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001313 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1314 my $match = $1;
1315 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1316 }
1317
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001318 return $res;
1319}
1320
Tom Rinia50f8de2014-02-27 08:27:28 -05001321sub get_quoted_string {
1322 my ($line, $rawline) = @_;
1323
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02001324 return "" if (!defined($line) || !defined($rawline));
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001325 return "" if ($line !~ m/($String)/g);
Tom Rinia50f8de2014-02-27 08:27:28 -05001326 return substr($rawline, $-[0], $+[0] - $-[0]);
1327}
1328
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001329sub ctx_statement_block {
1330 my ($linenr, $remain, $off) = @_;
1331 my $line = $linenr - 1;
1332 my $blk = '';
1333 my $soff = $off;
1334 my $coff = $off - 1;
1335 my $coff_set = 0;
1336
1337 my $loff = 0;
1338
1339 my $type = '';
1340 my $level = 0;
1341 my @stack = ();
1342 my $p;
1343 my $c;
1344 my $len = 0;
1345
1346 my $remainder;
1347 while (1) {
1348 @stack = (['', 0]) if ($#stack == -1);
1349
1350 #warn "CSB: blk<$blk> remain<$remain>\n";
1351 # If we are about to drop off the end, pull in more
1352 # context.
1353 if ($off >= $len) {
1354 for (; $remain > 0; $line++) {
1355 last if (!defined $lines[$line]);
1356 next if ($lines[$line] =~ /^-/);
1357 $remain--;
1358 $loff = $len;
1359 $blk .= $lines[$line] . "\n";
1360 $len = length($blk);
1361 $line++;
1362 last;
1363 }
1364 # Bail if there is no further context.
1365 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1366 if ($off >= $len) {
1367 last;
1368 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00001369 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1370 $level++;
1371 $type = '#';
1372 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001373 }
1374 $p = $c;
1375 $c = substr($blk, $off, 1);
1376 $remainder = substr($blk, $off);
1377
1378 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1379
1380 # Handle nested #if/#else.
1381 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1382 push(@stack, [ $type, $level ]);
1383 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1384 ($type, $level) = @{$stack[$#stack - 1]};
1385 } elsif ($remainder =~ /^#\s*endif\b/) {
1386 ($type, $level) = @{pop(@stack)};
1387 }
1388
1389 # Statement ends at the ';' or a close '}' at the
1390 # outermost level.
1391 if ($level == 0 && $c eq ';') {
1392 last;
1393 }
1394
1395 # An else is really a conditional as long as its not else if
1396 if ($level == 0 && $coff_set == 0 &&
1397 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1398 $remainder =~ /^(else)(?:\s|{)/ &&
1399 $remainder !~ /^else\s+if\b/) {
1400 $coff = $off + length($1) - 1;
1401 $coff_set = 1;
1402 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1403 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1404 }
1405
1406 if (($type eq '' || $type eq '(') && $c eq '(') {
1407 $level++;
1408 $type = '(';
1409 }
1410 if ($type eq '(' && $c eq ')') {
1411 $level--;
1412 $type = ($level != 0)? '(' : '';
1413
1414 if ($level == 0 && $coff < $soff) {
1415 $coff = $off;
1416 $coff_set = 1;
1417 #warn "CSB: mark coff<$coff>\n";
1418 }
1419 }
1420 if (($type eq '' || $type eq '{') && $c eq '{') {
1421 $level++;
1422 $type = '{';
1423 }
1424 if ($type eq '{' && $c eq '}') {
1425 $level--;
1426 $type = ($level != 0)? '{' : '';
1427
1428 if ($level == 0) {
1429 if (substr($blk, $off + 1, 1) eq ';') {
1430 $off++;
1431 }
1432 last;
1433 }
1434 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00001435 # Preprocessor commands end at the newline unless escaped.
1436 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1437 $level--;
1438 $type = '';
1439 $off++;
1440 last;
1441 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001442 $off++;
1443 }
1444 # We are truly at the end, so shuffle to the next line.
1445 if ($off == $len) {
1446 $loff = $len + 1;
1447 $line++;
1448 $remain--;
1449 }
1450
1451 my $statement = substr($blk, $soff, $off - $soff + 1);
1452 my $condition = substr($blk, $soff, $coff - $soff + 1);
1453
1454 #warn "STATEMENT<$statement>\n";
1455 #warn "CONDITION<$condition>\n";
1456
1457 #print "coff<$coff> soff<$off> loff<$loff>\n";
1458
1459 return ($statement, $condition,
1460 $line, $remain + 1, $off - $loff + 1, $level);
1461}
1462
1463sub statement_lines {
1464 my ($stmt) = @_;
1465
1466 # Strip the diff line prefixes and rip blank lines at start and end.
1467 $stmt =~ s/(^|\n)./$1/g;
1468 $stmt =~ s/^\s*//;
1469 $stmt =~ s/\s*$//;
1470
1471 my @stmt_lines = ($stmt =~ /\n/g);
1472
1473 return $#stmt_lines + 2;
1474}
1475
1476sub statement_rawlines {
1477 my ($stmt) = @_;
1478
1479 my @stmt_lines = ($stmt =~ /\n/g);
1480
1481 return $#stmt_lines + 2;
1482}
1483
1484sub statement_block_size {
1485 my ($stmt) = @_;
1486
1487 $stmt =~ s/(^|\n)./$1/g;
1488 $stmt =~ s/^\s*{//;
1489 $stmt =~ s/}\s*$//;
1490 $stmt =~ s/^\s*//;
1491 $stmt =~ s/\s*$//;
1492
1493 my @stmt_lines = ($stmt =~ /\n/g);
1494 my @stmt_statements = ($stmt =~ /;/g);
1495
1496 my $stmt_lines = $#stmt_lines + 2;
1497 my $stmt_statements = $#stmt_statements + 1;
1498
1499 if ($stmt_lines > $stmt_statements) {
1500 return $stmt_lines;
1501 } else {
1502 return $stmt_statements;
1503 }
1504}
1505
1506sub ctx_statement_full {
1507 my ($linenr, $remain, $off) = @_;
1508 my ($statement, $condition, $level);
1509
1510 my (@chunks);
1511
1512 # Grab the first conditional/block pair.
1513 ($statement, $condition, $linenr, $remain, $off, $level) =
1514 ctx_statement_block($linenr, $remain, $off);
1515 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1516 push(@chunks, [ $condition, $statement ]);
1517 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1518 return ($level, $linenr, @chunks);
1519 }
1520
1521 # Pull in the following conditional/block pairs and see if they
1522 # could continue the statement.
1523 for (;;) {
1524 ($statement, $condition, $linenr, $remain, $off, $level) =
1525 ctx_statement_block($linenr, $remain, $off);
1526 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1527 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1528 #print "C: push\n";
1529 push(@chunks, [ $condition, $statement ]);
1530 }
1531
1532 return ($level, $linenr, @chunks);
1533}
1534
1535sub ctx_block_get {
1536 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1537 my $line;
1538 my $start = $linenr - 1;
1539 my $blk = '';
1540 my @o;
1541 my @c;
1542 my @res = ();
1543
1544 my $level = 0;
1545 my @stack = ($level);
1546 for ($line = $start; $remain > 0; $line++) {
1547 next if ($rawlines[$line] =~ /^-/);
1548 $remain--;
1549
1550 $blk .= $rawlines[$line];
1551
1552 # Handle nested #if/#else.
1553 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1554 push(@stack, $level);
1555 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1556 $level = $stack[$#stack - 1];
1557 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1558 $level = pop(@stack);
1559 }
1560
1561 foreach my $c (split(//, $lines[$line])) {
1562 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1563 if ($off > 0) {
1564 $off--;
1565 next;
1566 }
1567
1568 if ($c eq $close && $level > 0) {
1569 $level--;
1570 last if ($level == 0);
1571 } elsif ($c eq $open) {
1572 $level++;
1573 }
1574 }
1575
1576 if (!$outer || $level <= 1) {
1577 push(@res, $rawlines[$line]);
1578 }
1579
1580 last if ($level == 0);
1581 }
1582
1583 return ($level, @res);
1584}
1585sub ctx_block_outer {
1586 my ($linenr, $remain) = @_;
1587
1588 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1589 return @r;
1590}
1591sub ctx_block {
1592 my ($linenr, $remain) = @_;
1593
1594 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1595 return @r;
1596}
1597sub ctx_statement {
1598 my ($linenr, $remain, $off) = @_;
1599
1600 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1601 return @r;
1602}
1603sub ctx_block_level {
1604 my ($linenr, $remain) = @_;
1605
1606 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1607}
1608sub ctx_statement_level {
1609 my ($linenr, $remain, $off) = @_;
1610
1611 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1612}
1613
1614sub ctx_locate_comment {
1615 my ($first_line, $end_line) = @_;
1616
1617 # Catch a comment on the end of the line itself.
1618 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1619 return $current_comment if (defined $current_comment);
1620
1621 # Look through the context and try and figure out if there is a
1622 # comment.
1623 my $in_comment = 0;
1624 $current_comment = '';
1625 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1626 my $line = $rawlines[$linenr - 1];
1627 #warn " $line\n";
1628 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1629 $in_comment = 1;
1630 }
1631 if ($line =~ m@/\*@) {
1632 $in_comment = 1;
1633 }
1634 if (!$in_comment && $current_comment ne '') {
1635 $current_comment = '';
1636 }
1637 $current_comment .= $line . "\n" if ($in_comment);
1638 if ($line =~ m@\*/@) {
1639 $in_comment = 0;
1640 }
1641 }
1642
1643 chomp($current_comment);
1644 return($current_comment);
1645}
1646sub ctx_has_comment {
1647 my ($first_line, $end_line) = @_;
1648 my $cmt = ctx_locate_comment($first_line, $end_line);
1649
1650 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1651 ##print "CMMT: $cmt\n";
1652
1653 return ($cmt ne '');
1654}
1655
1656sub raw_line {
1657 my ($linenr, $cnt) = @_;
1658
1659 my $offset = $linenr - 1;
1660 $cnt++;
1661
1662 my $line;
1663 while ($cnt) {
1664 $line = $rawlines[$offset++];
1665 next if (defined($line) && $line =~ /^-/);
1666 $cnt--;
1667 }
1668
1669 return $line;
1670}
1671
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02001672sub get_stat_real {
1673 my ($linenr, $lc) = @_;
1674
1675 my $stat_real = raw_line($linenr, 0);
1676 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1677 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1678 }
1679
1680 return $stat_real;
1681}
1682
1683sub get_stat_here {
1684 my ($linenr, $cnt, $here) = @_;
1685
1686 my $herectx = $here . "\n";
1687 for (my $n = 0; $n < $cnt; $n++) {
1688 $herectx .= raw_line($linenr, $n) . "\n";
1689 }
1690
1691 return $herectx;
1692}
1693
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001694sub cat_vet {
1695 my ($vet) = @_;
1696 my ($res, $coded);
1697
1698 $res = '';
1699 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1700 $res .= $1;
1701 if ($2 ne '') {
1702 $coded = sprintf("^%c", unpack('C', $2) + 64);
1703 $res .= $coded;
1704 }
1705 }
1706 $res =~ s/$/\$/;
1707
1708 return $res;
1709}
1710
1711my $av_preprocessor = 0;
1712my $av_pending;
1713my @av_paren_type;
1714my $av_pend_colon;
1715
1716sub annotate_reset {
1717 $av_preprocessor = 0;
1718 $av_pending = '_';
1719 @av_paren_type = ('E');
1720 $av_pend_colon = 'O';
1721}
1722
1723sub annotate_values {
1724 my ($stream, $type) = @_;
1725
1726 my $res;
1727 my $var = '_' x length($stream);
1728 my $cur = $stream;
1729
1730 print "$stream\n" if ($dbg_values > 1);
1731
1732 while (length($cur)) {
1733 @av_paren_type = ('E') if ($#av_paren_type < 0);
1734 print " <" . join('', @av_paren_type) .
1735 "> <$type> <$av_pending>" if ($dbg_values > 1);
1736 if ($cur =~ /^(\s+)/o) {
1737 print "WS($1)\n" if ($dbg_values > 1);
1738 if ($1 =~ /\n/ && $av_preprocessor) {
1739 $type = pop(@av_paren_type);
1740 $av_preprocessor = 0;
1741 }
1742
1743 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1744 print "CAST($1)\n" if ($dbg_values > 1);
1745 push(@av_paren_type, $type);
Kim Phillips9b7b8372013-02-28 12:53:52 +00001746 $type = 'c';
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001747
1748 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1749 print "DECLARE($1)\n" if ($dbg_values > 1);
1750 $type = 'T';
1751
1752 } elsif ($cur =~ /^($Modifier)\s*/) {
1753 print "MODIFIER($1)\n" if ($dbg_values > 1);
1754 $type = 'T';
1755
1756 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1757 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1758 $av_preprocessor = 1;
1759 push(@av_paren_type, $type);
1760 if ($2 ne '') {
1761 $av_pending = 'N';
1762 }
1763 $type = 'E';
1764
1765 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1766 print "UNDEF($1)\n" if ($dbg_values > 1);
1767 $av_preprocessor = 1;
1768 push(@av_paren_type, $type);
1769
1770 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1771 print "PRE_START($1)\n" if ($dbg_values > 1);
1772 $av_preprocessor = 1;
1773
1774 push(@av_paren_type, $type);
1775 push(@av_paren_type, $type);
1776 $type = 'E';
1777
1778 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1779 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1780 $av_preprocessor = 1;
1781
1782 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1783
1784 $type = 'E';
1785
1786 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1787 print "PRE_END($1)\n" if ($dbg_values > 1);
1788
1789 $av_preprocessor = 1;
1790
1791 # Assume all arms of the conditional end as this
1792 # one does, and continue as if the #endif was not here.
1793 pop(@av_paren_type);
1794 push(@av_paren_type, $type);
1795 $type = 'E';
1796
1797 } elsif ($cur =~ /^(\\\n)/o) {
1798 print "PRECONT($1)\n" if ($dbg_values > 1);
1799
1800 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1801 print "ATTR($1)\n" if ($dbg_values > 1);
1802 $av_pending = $type;
1803 $type = 'N';
1804
1805 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1806 print "SIZEOF($1)\n" if ($dbg_values > 1);
1807 if (defined $2) {
1808 $av_pending = 'V';
1809 }
1810 $type = 'N';
1811
1812 } elsif ($cur =~ /^(if|while|for)\b/o) {
1813 print "COND($1)\n" if ($dbg_values > 1);
1814 $av_pending = 'E';
1815 $type = 'N';
1816
1817 } elsif ($cur =~/^(case)/o) {
1818 print "CASE($1)\n" if ($dbg_values > 1);
1819 $av_pend_colon = 'C';
1820 $type = 'N';
1821
1822 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1823 print "KEYWORD($1)\n" if ($dbg_values > 1);
1824 $type = 'N';
1825
1826 } elsif ($cur =~ /^(\()/o) {
1827 print "PAREN('$1')\n" if ($dbg_values > 1);
1828 push(@av_paren_type, $av_pending);
1829 $av_pending = '_';
1830 $type = 'N';
1831
1832 } elsif ($cur =~ /^(\))/o) {
1833 my $new_type = pop(@av_paren_type);
1834 if ($new_type ne '_') {
1835 $type = $new_type;
1836 print "PAREN('$1') -> $type\n"
1837 if ($dbg_values > 1);
1838 } else {
1839 print "PAREN('$1')\n" if ($dbg_values > 1);
1840 }
1841
1842 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1843 print "FUNC($1)\n" if ($dbg_values > 1);
1844 $type = 'V';
1845 $av_pending = 'V';
1846
1847 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1848 if (defined $2 && $type eq 'C' || $type eq 'T') {
1849 $av_pend_colon = 'B';
1850 } elsif ($type eq 'E') {
1851 $av_pend_colon = 'L';
1852 }
1853 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1854 $type = 'V';
1855
1856 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1857 print "IDENT($1)\n" if ($dbg_values > 1);
1858 $type = 'V';
1859
1860 } elsif ($cur =~ /^($Assignment)/o) {
1861 print "ASSIGN($1)\n" if ($dbg_values > 1);
1862 $type = 'N';
1863
1864 } elsif ($cur =~/^(;|{|})/) {
1865 print "END($1)\n" if ($dbg_values > 1);
1866 $type = 'E';
1867 $av_pend_colon = 'O';
1868
1869 } elsif ($cur =~/^(,)/) {
1870 print "COMMA($1)\n" if ($dbg_values > 1);
1871 $type = 'C';
1872
1873 } elsif ($cur =~ /^(\?)/o) {
1874 print "QUESTION($1)\n" if ($dbg_values > 1);
1875 $type = 'N';
1876
1877 } elsif ($cur =~ /^(:)/o) {
1878 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1879
1880 substr($var, length($res), 1, $av_pend_colon);
1881 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1882 $type = 'E';
1883 } else {
1884 $type = 'N';
1885 }
1886 $av_pend_colon = 'O';
1887
1888 } elsif ($cur =~ /^(\[)/o) {
1889 print "CLOSE($1)\n" if ($dbg_values > 1);
1890 $type = 'N';
1891
1892 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1893 my $variant;
1894
1895 print "OPV($1)\n" if ($dbg_values > 1);
1896 if ($type eq 'V') {
1897 $variant = 'B';
1898 } else {
1899 $variant = 'U';
1900 }
1901
1902 substr($var, length($res), 1, $variant);
1903 $type = 'N';
1904
1905 } elsif ($cur =~ /^($Operators)/o) {
1906 print "OP($1)\n" if ($dbg_values > 1);
1907 if ($1 ne '++' && $1 ne '--') {
1908 $type = 'N';
1909 }
1910
1911 } elsif ($cur =~ /(^.)/o) {
1912 print "C($1)\n" if ($dbg_values > 1);
1913 }
1914 if (defined $1) {
1915 $cur = substr($cur, length($1));
1916 $res .= $type x length($1);
1917 }
1918 }
1919
1920 return ($res, $var);
1921}
1922
1923sub possible {
1924 my ($possible, $line) = @_;
1925 my $notPermitted = qr{(?:
1926 ^(?:
1927 $Modifier|
1928 $Storage|
1929 $Type|
1930 DEFINE_\S+
1931 )$|
1932 ^(?:
1933 goto|
1934 return|
1935 case|
1936 else|
1937 asm|__asm__|
Kim Phillips9b7b8372013-02-28 12:53:52 +00001938 do|
1939 \#|
1940 \#\#|
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001941 )(?:\s|$)|
1942 ^(?:typedef|struct|enum)\b
1943 )}x;
1944 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1945 if ($possible !~ $notPermitted) {
1946 # Check for modifiers.
1947 $possible =~ s/\s*$Storage\s*//g;
1948 $possible =~ s/\s*$Sparse\s*//g;
1949 if ($possible =~ /^\s*$/) {
1950
1951 } elsif ($possible =~ /\s/) {
1952 $possible =~ s/\s*$Type\s*//g;
1953 for my $modifier (split(' ', $possible)) {
1954 if ($modifier !~ $notPermitted) {
1955 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001956 push(@modifierListFile, $modifier);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001957 }
1958 }
1959
1960 } else {
1961 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001962 push(@typeListFile, $possible);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001963 }
1964 build_types();
1965 } else {
1966 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1967 }
1968}
1969
1970my $prefix = '';
1971
1972sub show_type {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001973 my ($type) = @_;
1974
1975 $type =~ tr/[a-z]/[A-Z]/;
Tom Rinia50f8de2014-02-27 08:27:28 -05001976
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001977 return defined $use_type{$type} if (scalar keys %use_type > 0);
1978
1979 return !defined $ignore_type{$type};
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001980}
1981
1982sub report {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001983 my ($level, $type, $msg) = @_;
1984
1985 if (!show_type($type) ||
1986 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001987 return 0;
1988 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001989 my $output = '';
1990 if ($color) {
1991 if ($level eq 'ERROR') {
1992 $output .= RED;
1993 } elsif ($level eq 'WARNING') {
1994 $output .= YELLOW;
1995 } else {
1996 $output .= GREEN;
1997 }
1998 }
1999 $output .= $prefix . $level . ':';
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002000 if ($show_types) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002001 $output .= BLUE if ($color);
2002 $output .= "$type:";
2003 }
2004 $output .= RESET if ($color);
2005 $output .= ' ' . $msg . "\n";
2006
2007 if ($showfile) {
2008 my @lines = split("\n", $output, -1);
2009 splice(@lines, 1, 1);
2010 $output = join("\n", @lines);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002011 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002012 $output = (split('\n', $output))[0] . "\n" if ($terse);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002013
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002014 push(our @report, $output);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002015
2016 return 1;
2017}
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002018
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002019sub report_dump {
2020 our @report;
2021}
2022
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002023sub fixup_current_range {
2024 my ($lineRef, $offset, $length) = @_;
2025
2026 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2027 my $o = $1;
2028 my $l = $2;
2029 my $no = $o + $offset;
2030 my $nl = $l + $length;
2031 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2032 }
2033}
2034
2035sub fix_inserted_deleted_lines {
2036 my ($linesRef, $insertedRef, $deletedRef) = @_;
2037
2038 my $range_last_linenr = 0;
2039 my $delta_offset = 0;
2040
2041 my $old_linenr = 0;
2042 my $new_linenr = 0;
2043
2044 my $next_insert = 0;
2045 my $next_delete = 0;
2046
2047 my @lines = ();
2048
2049 my $inserted = @{$insertedRef}[$next_insert++];
2050 my $deleted = @{$deletedRef}[$next_delete++];
2051
2052 foreach my $old_line (@{$linesRef}) {
2053 my $save_line = 1;
2054 my $line = $old_line; #don't modify the array
2055 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
2056 $delta_offset = 0;
2057 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2058 $range_last_linenr = $new_linenr;
2059 fixup_current_range(\$line, $delta_offset, 0);
2060 }
2061
2062 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2063 $deleted = @{$deletedRef}[$next_delete++];
2064 $save_line = 0;
2065 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2066 }
2067
2068 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2069 push(@lines, ${$inserted}{'LINE'});
2070 $inserted = @{$insertedRef}[$next_insert++];
2071 $new_linenr++;
2072 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2073 }
2074
2075 if ($save_line) {
2076 push(@lines, $line);
2077 $new_linenr++;
2078 }
2079
2080 $old_linenr++;
2081 }
2082
2083 return @lines;
2084}
2085
2086sub fix_insert_line {
2087 my ($linenr, $line) = @_;
2088
2089 my $inserted = {
2090 LINENR => $linenr,
2091 LINE => $line,
2092 };
2093 push(@fixed_inserted, $inserted);
2094}
2095
2096sub fix_delete_line {
2097 my ($linenr, $line) = @_;
2098
2099 my $deleted = {
2100 LINENR => $linenr,
2101 LINE => $line,
2102 };
2103
2104 push(@fixed_deleted, $deleted);
2105}
2106
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002107sub ERROR {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002108 my ($type, $msg) = @_;
2109
2110 if (report("ERROR", $type, $msg)) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002111 our $clean = 0;
2112 our $cnt_error++;
Tom Rinia50f8de2014-02-27 08:27:28 -05002113 return 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002114 }
Tom Rinia50f8de2014-02-27 08:27:28 -05002115 return 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002116}
2117sub WARN {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002118 my ($type, $msg) = @_;
2119
2120 if (report("WARNING", $type, $msg)) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002121 our $clean = 0;
2122 our $cnt_warn++;
Tom Rinia50f8de2014-02-27 08:27:28 -05002123 return 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002124 }
Tom Rinia50f8de2014-02-27 08:27:28 -05002125 return 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002126}
2127sub CHK {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002128 my ($type, $msg) = @_;
2129
2130 if ($check && report("CHECK", $type, $msg)) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002131 our $clean = 0;
2132 our $cnt_chk++;
Tom Rinia50f8de2014-02-27 08:27:28 -05002133 return 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002134 }
Tom Rinia50f8de2014-02-27 08:27:28 -05002135 return 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002136}
2137
2138sub check_absolute_file {
2139 my ($absolute, $herecurr) = @_;
2140 my $file = $absolute;
2141
2142 ##print "absolute<$absolute>\n";
2143
2144 # See if any suffix of this path is a path within the tree.
2145 while ($file =~ s@^[^/]*/@@) {
2146 if (-f "$root/$file") {
2147 ##print "file<$file>\n";
2148 last;
2149 }
2150 }
2151 if (! -f _) {
2152 return 0;
2153 }
2154
2155 # It is, so see if the prefix is acceptable.
2156 my $prefix = $absolute;
2157 substr($prefix, -length($file)) = '';
2158
2159 ##print "prefix<$prefix>\n";
2160 if ($prefix ne ".../") {
2161 WARN("USE_RELATIVE_PATH",
2162 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2163 }
2164}
2165
Tom Rinia50f8de2014-02-27 08:27:28 -05002166sub trim {
2167 my ($string) = @_;
2168
2169 $string =~ s/^\s+|\s+$//g;
2170
2171 return $string;
2172}
2173
2174sub ltrim {
2175 my ($string) = @_;
2176
2177 $string =~ s/^\s+//;
2178
2179 return $string;
2180}
2181
2182sub rtrim {
2183 my ($string) = @_;
2184
2185 $string =~ s/\s+$//;
2186
2187 return $string;
2188}
2189
2190sub string_find_replace {
2191 my ($string, $find, $replace) = @_;
2192
2193 $string =~ s/$find/$replace/g;
2194
2195 return $string;
2196}
2197
2198sub tabify {
2199 my ($leading) = @_;
2200
2201 my $source_indent = 8;
2202 my $max_spaces_before_tab = $source_indent - 1;
2203 my $spaces_to_tab = " " x $source_indent;
2204
2205 #convert leading spaces to tabs
2206 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2207 #Remove spaces before a tab
2208 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2209
2210 return "$leading";
2211}
2212
Kim Phillips9b7b8372013-02-28 12:53:52 +00002213sub pos_last_openparen {
2214 my ($line) = @_;
2215
2216 my $pos = 0;
2217
2218 my $opens = $line =~ tr/\(/\(/;
2219 my $closes = $line =~ tr/\)/\)/;
2220
2221 my $last_openparen = 0;
2222
2223 if (($opens == 0) || ($closes >= $opens)) {
2224 return -1;
2225 }
2226
2227 my $len = length($line);
2228
2229 for ($pos = 0; $pos < $len; $pos++) {
2230 my $string = substr($line, $pos);
2231 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2232 $pos += length($1) - 1;
2233 } elsif (substr($line, $pos, 1) eq '(') {
2234 $last_openparen = $pos;
2235 } elsif (index($string, '(') == -1) {
2236 last;
2237 }
2238 }
2239
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002240 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Kim Phillips9b7b8372013-02-28 12:53:52 +00002241}
2242
Simon Glassc8f4d092020-05-22 16:32:36 -06002243# Checks specific to U-Boot
2244sub u_boot_line {
2245 my ($realfile, $line, $herecurr) = @_;
Simon Glass7d919f52020-05-22 16:32:37 -06002246
2247 # ask for a test if a new uclass ID is added
2248 if ($realfile =~ /uclass-id.h/ && $line =~ /^\+/) {
2249 WARN("NEW_UCLASS",
2250 "Possible new uclass - make sure to add a sandbox driver, plus a test in test/dm/<name>.c\n" . $herecurr);
2251 }
Simon Glass3ee01322020-05-22 16:32:38 -06002252
2253 # try to get people to use the livetree API
2254 if ($line =~ /^\+.*fdtdec_/) {
2255 WARN("LIVETREE",
2256 "Use the livetree API (dev_read_...)\n" . $herecurr);
2257 }
Simon Glass35bf3b02020-05-22 16:32:39 -06002258
2259 # add tests for new commands
2260 if ($line =~ /^\+.*do_($Ident)\(struct cmd_tbl.*/) {
2261 WARN("CMD_TEST",
2262 "Possible new command - make sure you add a test\n" . $herecurr);
2263 }
Simon Glass567240d2020-05-22 16:32:40 -06002264
2265 # use if instead of #if
2266 if ($line =~ /^\+#if.*CONFIG.*/) {
2267 WARN("PREFER_IF",
2268 "Use 'if (IS_ENABLED(CONFIG...))' instead of '#if or #ifdef' where possible\n" . $herecurr);
2269 }
Tom Rinid7b4ab52020-05-26 14:29:02 -04002270
2271 # use defconfig to manage CONFIG_CMD options
2272 if ($line =~ /\+\s*#\s*(define|undef)\s+(CONFIG_CMD\w*)\b/) {
2273 ERROR("DEFINE_CONFIG_CMD",
2274 "All commands are managed by Kconfig\n" . $herecurr);
2275 }
Simon Glassc8f4d092020-05-22 16:32:36 -06002276}
2277
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002278sub process {
2279 my $filename = shift;
2280
2281 my $linenr=0;
2282 my $prevline="";
2283 my $prevrawline="";
2284 my $stashline="";
2285 my $stashrawline="";
2286
2287 my $length;
2288 my $indent;
2289 my $previndent=0;
2290 my $stashindent=0;
2291
2292 our $clean = 1;
2293 my $signoff = 0;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002294 my $author = '';
2295 my $authorsignoff = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002296 my $is_patch = 0;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002297 my $is_binding_patch = -1;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002298 my $in_header_lines = $file ? 0 : 1;
Kim Phillips9b7b8372013-02-28 12:53:52 +00002299 my $in_commit_log = 0; #Scanning lines before patch
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002300 my $has_commit_log = 0; #Encountered lines before patch
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002301 my $commit_log_lines = 0; #Number of commit log lines
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002302 my $commit_log_possible_stack_dump = 0;
2303 my $commit_log_long_line = 0;
2304 my $commit_log_has_diff = 0;
2305 my $reported_maintainer_file = 0;
Kim Phillips9b7b8372013-02-28 12:53:52 +00002306 my $non_utf8_charset = 0;
2307
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002308 my $last_blank_line = 0;
2309 my $last_coalesced_string_linenr = -1;
2310
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002311 our @report = ();
2312 our $cnt_lines = 0;
2313 our $cnt_error = 0;
2314 our $cnt_warn = 0;
2315 our $cnt_chk = 0;
2316
2317 # Trace the real file/line as we go.
2318 my $realfile = '';
2319 my $realline = 0;
2320 my $realcnt = 0;
2321 my $here = '';
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002322 my $context_function; #undef'd unless there's a known function
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002323 my $in_comment = 0;
2324 my $comment_edge = 0;
2325 my $first_line = 0;
2326 my $p1_prefix = '';
2327
2328 my $prev_values = 'E';
2329
2330 # suppression flags
2331 my %suppress_ifbraces;
2332 my %suppress_whiletrailers;
2333 my %suppress_export;
Kim Phillips9b7b8372013-02-28 12:53:52 +00002334 my $suppress_statement = 0;
2335
Tom Rinia50f8de2014-02-27 08:27:28 -05002336 my %signatures = ();
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002337
2338 # Pre-scan the patch sanitizing the lines.
2339 # Pre-scan the patch looking for any __setup documentation.
2340 #
2341 my @setup_docs = ();
2342 my $setup_docs = 0;
2343
Tom Rinia50f8de2014-02-27 08:27:28 -05002344 my $camelcase_file_seeded = 0;
2345
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02002346 my $checklicenseline = 1;
2347
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002348 sanitise_line_reset();
2349 my $line;
2350 foreach my $rawline (@rawlines) {
2351 $linenr++;
2352 $line = $rawline;
2353
Tom Rinia50f8de2014-02-27 08:27:28 -05002354 push(@fixed, $rawline) if ($fix);
2355
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002356 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2357 $setup_docs = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002358 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002359 $setup_docs = 1;
2360 }
2361 #next;
2362 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002363 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002364 $realline=$1-1;
2365 if (defined $2) {
2366 $realcnt=$3+1;
2367 } else {
2368 $realcnt=1+1;
2369 }
2370 $in_comment = 0;
2371
2372 # Guestimate if this is a continuing comment. Run
2373 # the context looking for a comment "edge". If this
2374 # edge is a close comment then we must be in a comment
2375 # at context start.
2376 my $edge;
2377 my $cnt = $realcnt;
2378 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2379 next if (defined $rawlines[$ln - 1] &&
2380 $rawlines[$ln - 1] =~ /^-/);
2381 $cnt--;
2382 #print "RAW<$rawlines[$ln - 1]>\n";
2383 last if (!defined $rawlines[$ln - 1]);
2384 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2385 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2386 ($edge) = $1;
2387 last;
2388 }
2389 }
2390 if (defined $edge && $edge eq '*/') {
2391 $in_comment = 1;
2392 }
2393
2394 # Guestimate if this is a continuing comment. If this
2395 # is the start of a diff block and this line starts
2396 # ' *' then it is very likely a comment.
2397 if (!defined $edge &&
2398 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2399 {
2400 $in_comment = 1;
2401 }
2402
2403 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2404 sanitise_line_reset($in_comment);
2405
2406 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2407 # Standardise the strings and chars within the input to
2408 # simplify matching -- only bother with positive lines.
2409 $line = sanitise_line($rawline);
2410 }
2411 push(@lines, $line);
2412
2413 if ($realcnt > 1) {
2414 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2415 } else {
2416 $realcnt = 0;
2417 }
2418
2419 #print "==>$rawline\n";
2420 #print "-->$line\n";
2421
2422 if ($setup_docs && $line =~ /^\+/) {
2423 push(@setup_docs, $line);
2424 }
2425 }
2426
2427 $prefix = '';
2428
2429 $realcnt = 0;
2430 $linenr = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002431 $fixlinenr = -1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002432 foreach my $line (@lines) {
2433 $linenr++;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002434 $fixlinenr++;
Tom Rinia50f8de2014-02-27 08:27:28 -05002435 my $sline = $line; #copy of $line
2436 $sline =~ s/$;/ /g; #with comments as spaces
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002437
2438 my $rawline = $rawlines[$linenr - 1];
2439
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002440# check if it's a mode change, rename or start of a patch
2441 if (!$in_commit_log &&
2442 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2443 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2444 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2445 $is_patch = 1;
2446 }
2447
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002448#extract the line range in the file after the patch is applied
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002449 if (!$in_commit_log &&
2450 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2451 my $context = $4;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002452 $is_patch = 1;
2453 $first_line = $linenr + 1;
2454 $realline=$1-1;
2455 if (defined $2) {
2456 $realcnt=$3+1;
2457 } else {
2458 $realcnt=1+1;
2459 }
2460 annotate_reset();
2461 $prev_values = 'E';
2462
2463 %suppress_ifbraces = ();
2464 %suppress_whiletrailers = ();
2465 %suppress_export = ();
Kim Phillips9b7b8372013-02-28 12:53:52 +00002466 $suppress_statement = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002467 if ($context =~ /\b(\w+)\s*\(/) {
2468 $context_function = $1;
2469 } else {
2470 undef $context_function;
2471 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002472 next;
2473
2474# track the line number as we move through the hunk, note that
2475# new versions of GNU diff omit the leading space on completely
2476# blank context lines so we need to count that too.
2477 } elsif ($line =~ /^( |\+|$)/) {
2478 $realline++;
2479 $realcnt-- if ($realcnt != 0);
2480
2481 # Measure the line length and indent.
2482 ($length, $indent) = line_stats($rawline);
2483
2484 # Track the previous line.
2485 ($prevline, $stashline) = ($stashline, $line);
2486 ($previndent, $stashindent) = ($stashindent, $indent);
2487 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2488
2489 #warn "line<$line>\n";
2490
2491 } elsif ($realcnt == 1) {
2492 $realcnt--;
2493 }
2494
2495 my $hunk_line = ($realcnt != 0);
2496
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002497 $here = "#$linenr: " if (!$file);
2498 $here = "#$realline: " if ($file);
2499
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002500 my $found_file = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002501 # extract the filename as it passes
2502 if ($line =~ /^diff --git.*?(\S+)$/) {
2503 $realfile = $1;
Tom Rinia50f8de2014-02-27 08:27:28 -05002504 $realfile =~ s@^([^/]*)/@@ if (!$file);
Kim Phillips9b7b8372013-02-28 12:53:52 +00002505 $in_commit_log = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002506 $found_file = 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002507 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2508 $realfile = $1;
Tom Rinia50f8de2014-02-27 08:27:28 -05002509 $realfile =~ s@^([^/]*)/@@ if (!$file);
Kim Phillips9b7b8372013-02-28 12:53:52 +00002510 $in_commit_log = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002511
2512 $p1_prefix = $1;
2513 if (!$file && $tree && $p1_prefix ne '' &&
2514 -e "$root/$p1_prefix") {
2515 WARN("PATCH_PREFIX",
2516 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2517 }
2518
2519 if ($realfile =~ m@^include/asm/@) {
2520 ERROR("MODIFIED_INCLUDE_ASM",
2521 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2522 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002523 $found_file = 1;
2524 }
2525
2526#make up the handle for any error we report on this line
2527 if ($showfile) {
2528 $prefix = "$realfile:$realline: "
2529 } elsif ($emacs) {
2530 if ($file) {
2531 $prefix = "$filename:$realline: ";
2532 } else {
2533 $prefix = "$filename:$linenr: ";
2534 }
2535 }
2536
2537 if ($found_file) {
2538 if (is_maintained_obsolete($realfile)) {
2539 WARN("OBSOLETE",
2540 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2541 }
2542 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2543 $check = 1;
2544 } else {
2545 $check = $check_orig;
2546 }
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02002547 $checklicenseline = 1;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002548
2549 if ($realfile !~ /^MAINTAINERS/) {
2550 my $last_binding_patch = $is_binding_patch;
2551
2552 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2553
2554 if (($last_binding_patch != -1) &&
2555 ($last_binding_patch ^ $is_binding_patch)) {
2556 WARN("DT_SPLIT_BINDING_PATCH",
2557 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2558 }
2559 }
2560
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002561 next;
2562 }
2563
2564 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2565
2566 my $hereline = "$here\n$rawline\n";
2567 my $herecurr = "$here\n$rawline\n";
2568 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2569
2570 $cnt_lines++ if ($realcnt != 0);
2571
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002572# Verify the existence of a commit log if appropriate
2573# 2 is used because a $signature is counted in $commit_log_lines
2574 if ($in_commit_log) {
2575 if ($line !~ /^\s*$/) {
2576 $commit_log_lines++; #could be a $signature
2577 }
2578 } elsif ($has_commit_log && $commit_log_lines < 2) {
2579 WARN("COMMIT_MESSAGE",
2580 "Missing commit description - Add an appropriate one\n");
2581 $commit_log_lines = 2; #warn only once
2582 }
2583
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002584# Check if the commit log has what seems like a diff which can confuse patch
2585 if ($in_commit_log && !$commit_log_has_diff &&
2586 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2587 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2588 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2589 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2590 ERROR("DIFF_IN_COMMIT_MSG",
2591 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2592 $commit_log_has_diff = 1;
2593 }
2594
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002595# Check for incorrect file permissions
2596 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2597 my $permhere = $here . "FILE: $realfile\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05002598 if ($realfile !~ m@scripts/@ &&
2599 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002600 ERROR("EXECUTE_PERMISSIONS",
2601 "do not set execute permissions for source files\n" . $permhere);
2602 }
2603 }
2604
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002605# Check the patch for a From:
2606 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2607 $author = $1;
2608 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2609 $author =~ s/"//g;
2610 }
2611
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002612# Check the patch for a signoff:
2613 if ($line =~ /^\s*signed-off-by:/i) {
2614 $signoff++;
Kim Phillips9b7b8372013-02-28 12:53:52 +00002615 $in_commit_log = 0;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002616 if ($author ne '') {
2617 my $l = $line;
2618 $l =~ s/"//g;
2619 if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2620 $authorsignoff = 1;
2621 }
2622 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002623 }
2624
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002625# Check if MAINTAINERS is being updated. If so, there's probably no need to
2626# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2627 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2628 $reported_maintainer_file = 1;
2629 }
2630
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002631# Check signature styles
Kim Phillips9b7b8372013-02-28 12:53:52 +00002632 if (!$in_header_lines &&
2633 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002634 my $space_before = $1;
2635 my $sign_off = $2;
2636 my $space_after = $3;
2637 my $email = $4;
2638 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2639
Kim Phillips9b7b8372013-02-28 12:53:52 +00002640 if ($sign_off !~ /$signature_tags/) {
2641 WARN("BAD_SIGN_OFF",
2642 "Non-standard signature: $sign_off\n" . $herecurr);
2643 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002644 if (defined $space_before && $space_before ne "") {
Tom Rinia50f8de2014-02-27 08:27:28 -05002645 if (WARN("BAD_SIGN_OFF",
2646 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2647 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002648 $fixed[$fixlinenr] =
Tom Rinia50f8de2014-02-27 08:27:28 -05002649 "$ucfirst_sign_off $email";
2650 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002651 }
2652 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Tom Rinia50f8de2014-02-27 08:27:28 -05002653 if (WARN("BAD_SIGN_OFF",
2654 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2655 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002656 $fixed[$fixlinenr] =
Tom Rinia50f8de2014-02-27 08:27:28 -05002657 "$ucfirst_sign_off $email";
2658 }
2659
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002660 }
2661 if (!defined $space_after || $space_after ne " ") {
Tom Rinia50f8de2014-02-27 08:27:28 -05002662 if (WARN("BAD_SIGN_OFF",
2663 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2664 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002665 $fixed[$fixlinenr] =
Tom Rinia50f8de2014-02-27 08:27:28 -05002666 "$ucfirst_sign_off $email";
2667 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002668 }
2669
2670 my ($email_name, $email_address, $comment) = parse_email($email);
2671 my $suggested_email = format_email(($email_name, $email_address));
2672 if ($suggested_email eq "") {
2673 ERROR("BAD_SIGN_OFF",
2674 "Unrecognized email address: '$email'\n" . $herecurr);
2675 } else {
2676 my $dequoted = $suggested_email;
2677 $dequoted =~ s/^"//;
2678 $dequoted =~ s/" </ </;
2679 # Don't force email to have quotes
2680 # Allow just an angle bracketed address
2681 if ("$dequoted$comment" ne $email &&
2682 "<$email_address>$comment" ne $email &&
2683 "$suggested_email$comment" ne $email) {
2684 WARN("BAD_SIGN_OFF",
2685 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2686 }
2687 }
Tom Rinia50f8de2014-02-27 08:27:28 -05002688
2689# Check for duplicate signatures
2690 my $sig_nospace = $line;
2691 $sig_nospace =~ s/\s//g;
2692 $sig_nospace = lc($sig_nospace);
2693 if (defined $signatures{$sig_nospace}) {
2694 WARN("BAD_SIGN_OFF",
2695 "Duplicate signature\n" . $herecurr);
2696 } else {
2697 $signatures{$sig_nospace} = 1;
2698 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002699 }
2700
2701# Check email subject for common tools that don't need to be mentioned
2702 if ($in_header_lines &&
2703 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2704 WARN("EMAIL_SUBJECT",
2705 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2706 }
2707
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002708# Check for unwanted Gerrit info
2709 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2710 ERROR("GERRIT_CHANGE_ID",
2711 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2712 }
2713
2714# Check if the commit log is in a possible stack dump
2715 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2716 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2717 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2718 # timestamp
Simon Glassa33d9822020-05-22 16:32:35 -06002719 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2720 # stack dump address
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002721 $commit_log_possible_stack_dump = 1;
2722 }
2723
2724# Check for line lengths > 75 in commit log, warn once
2725 if ($in_commit_log && !$commit_log_long_line &&
2726 length($line) > 75 &&
2727 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2728 # file delta changes
2729 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2730 # filename then :
2731 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2732 # A Fixes: or Link: line
2733 $commit_log_possible_stack_dump)) {
2734 WARN("COMMIT_LOG_LONG_LINE",
2735 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2736 $commit_log_long_line = 1;
2737 }
2738
2739# Reset possible stack dump if a blank line is found
2740 if ($in_commit_log && $commit_log_possible_stack_dump &&
2741 $line =~ /^\s*$/) {
2742 $commit_log_possible_stack_dump = 0;
2743 }
2744
2745# Check for git id commit length and improperly formed commit descriptions
2746 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2747 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2748 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2749 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2750 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2751 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2752 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2753 my $init_char = "c";
2754 my $orig_commit = "";
2755 my $short = 1;
2756 my $long = 0;
2757 my $case = 1;
2758 my $space = 1;
2759 my $hasdesc = 0;
2760 my $hasparens = 0;
2761 my $id = '0123456789ab';
2762 my $orig_desc = "commit description";
2763 my $description = "";
2764
2765 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2766 $init_char = $1;
2767 $orig_commit = lc($2);
2768 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2769 $orig_commit = lc($1);
2770 }
2771
2772 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2773 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2774 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2775 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2776 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2777 $orig_desc = $1;
2778 $hasparens = 1;
2779 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2780 defined $rawlines[$linenr] &&
2781 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2782 $orig_desc = $1;
2783 $hasparens = 1;
2784 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2785 defined $rawlines[$linenr] &&
2786 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2787 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2788 $orig_desc = $1;
2789 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2790 $orig_desc .= " " . $1;
2791 $hasparens = 1;
2792 }
2793
2794 ($id, $description) = git_commit_info($orig_commit,
2795 $id, $orig_desc);
2796
2797 if (defined($id) &&
2798 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2799 ERROR("GIT_COMMIT_ID",
2800 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2801 }
2802 }
2803
2804# Check for added, moved or deleted files
2805 if (!$reported_maintainer_file && !$in_commit_log &&
2806 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2807 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2808 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2809 (defined($1) || defined($2))))) {
2810 $is_patch = 1;
2811 $reported_maintainer_file = 1;
2812 WARN("FILE_PATH_CHANGES",
2813 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002814 }
2815
2816# Check for wrappage within a valid hunk of the file
2817 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2818 ERROR("CORRUPTED_PATCH",
2819 "patch seems to be corrupt (line wrapped?)\n" .
2820 $herecurr) if (!$emitted_corrupt++);
2821 }
2822
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002823# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2824 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2825 $rawline !~ m/^$UTF8*$/) {
2826 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2827
2828 my $blank = copy_spacing($rawline);
2829 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2830 my $hereptr = "$hereline$ptr\n";
2831
2832 CHK("INVALID_UTF8",
2833 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2834 }
2835
Kim Phillips9b7b8372013-02-28 12:53:52 +00002836# Check if it's the start of a commit log
2837# (not a header line and we haven't seen the patch filename)
2838 if ($in_header_lines && $realfile =~ /^$/ &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002839 !($rawline =~ /^\s+(?:\S|$)/ ||
2840 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00002841 $in_header_lines = 0;
2842 $in_commit_log = 1;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002843 $has_commit_log = 1;
Kim Phillips9b7b8372013-02-28 12:53:52 +00002844 }
2845
2846# Check if there is UTF-8 in a commit log when a mail header has explicitly
2847# declined it, i.e defined some charset where it is missing.
2848 if ($in_header_lines &&
2849 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2850 $1 !~ /utf-8/i) {
2851 $non_utf8_charset = 1;
2852 }
2853
2854 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2855 $rawline =~ /$NON_ASCII_UTF8/) {
2856 WARN("UTF8_BEFORE_PATCH",
2857 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2858 }
2859
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002860# Check for absolute kernel paths in commit message
2861 if ($tree && $in_commit_log) {
2862 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2863 my $file = $1;
2864
2865 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2866 check_absolute_file($1, $herecurr)) {
2867 #
2868 } else {
2869 check_absolute_file($file, $herecurr);
2870 }
2871 }
2872 }
2873
Dan Murphy463f6882017-01-31 14:15:53 -06002874# Check for various typo / spelling mistakes
2875 if (defined($misspellings) &&
2876 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2877 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
2878 my $typo = $1;
2879 my $typo_fix = $spelling_fix{lc($typo)};
2880 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2881 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02002882 my $msg_level = \&WARN;
2883 $msg_level = \&CHK if ($file);
2884 if (&{$msg_level}("TYPO_SPELLING",
2885 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Dan Murphy463f6882017-01-31 14:15:53 -06002886 $fix) {
2887 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2888 }
2889 }
2890 }
2891
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002892# ignore non-hunk lines and lines being removed
2893 next if (!$hunk_line || $line =~ /^-/);
2894
2895#trailing whitespace
2896 if ($line =~ /^\+.*\015/) {
2897 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05002898 if (ERROR("DOS_LINE_ENDINGS",
2899 "DOS line endings\n" . $herevet) &&
2900 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002901 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Tom Rinia50f8de2014-02-27 08:27:28 -05002902 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002903 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2904 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05002905 if (ERROR("TRAILING_WHITESPACE",
2906 "trailing whitespace\n" . $herevet) &&
2907 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002908 $fixed[$fixlinenr] =~ s/\s+$//;
Tom Rinia50f8de2014-02-27 08:27:28 -05002909 }
2910
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002911 $rpt_cleaners = 1;
2912 }
2913
Tom Rinia50f8de2014-02-27 08:27:28 -05002914# Check for FSF mailing addresses.
2915 if ($rawline =~ /\bwrite to the Free/i ||
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002916 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Tom Rinia50f8de2014-02-27 08:27:28 -05002917 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2918 $rawline =~ /\b51\s+Franklin\s+St/i) {
2919 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02002920 my $msg_level = \&ERROR;
2921 $msg_level = \&CHK if ($file);
2922 &{$msg_level}("FSF_MAILING_ADDRESS",
2923 "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)
Tom Rinia50f8de2014-02-27 08:27:28 -05002924 }
2925
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002926# check for Kconfig help text having a real description
2927# Only applies when adding the entry originally, after that we do not have
2928# sufficient context to determine whether it is indeed long enough.
2929 if ($realfile =~ /Kconfig/ &&
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02002930 # 'choice' is usually the last thing on the line (though
2931 # Kconfig supports named choices), so use a word boundary
2932 # (\b) rather than a whitespace character (\s)
2933 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002934 my $length = 0;
2935 my $cnt = $realcnt;
2936 my $ln = $linenr + 1;
2937 my $f;
Kim Phillips9b7b8372013-02-28 12:53:52 +00002938 my $is_start = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002939 my $is_end = 0;
Kim Phillips9b7b8372013-02-28 12:53:52 +00002940 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002941 $f = $lines[$ln - 1];
2942 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2943 $is_end = $lines[$ln - 1] =~ /^\+/;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002944
2945 next if ($f =~ /^-/);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002946 last if (!$file && $f =~ /^\@\@/);
Kim Phillips9b7b8372013-02-28 12:53:52 +00002947
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02002948 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00002949 $is_start = 1;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02002950 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
2951 if ($lines[$ln - 1] =~ "---help---") {
2952 WARN("CONFIG_DESCRIPTION",
2953 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
2954 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00002955 $length = -1;
2956 }
2957
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002958 $f =~ s/^.//;
2959 $f =~ s/#.*//;
2960 $f =~ s/^\s+//;
2961 next if ($f =~ /^$/);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02002962
2963 # This only checks context lines in the patch
2964 # and so hopefully shouldn't trigger false
2965 # positives, even though some of these are
2966 # common words in help texts
2967 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2968 if|endif|menu|endmenu|source)\b/x) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002969 $is_end = 1;
2970 last;
2971 }
2972 $length++;
2973 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002974 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2975 WARN("CONFIG_DESCRIPTION",
2976 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2977 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00002978 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2979 }
2980
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002981# check for MAINTAINERS entries that don't have the right form
2982 if ($realfile =~ /^MAINTAINERS$/ &&
2983 $rawline =~ /^\+[A-Z]:/ &&
2984 $rawline !~ /^\+[A-Z]:\t\S/) {
2985 if (WARN("MAINTAINERS_STYLE",
2986 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2987 $fix) {
2988 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2989 }
2990 }
2991
2992# discourage the use of boolean for type definition attributes of Kconfig options
Kim Phillips9b7b8372013-02-28 12:53:52 +00002993 if ($realfile =~ /Kconfig/ &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002994 $line =~ /^\+\s*\bboolean\b/) {
2995 WARN("CONFIG_TYPE_BOOLEAN",
2996 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002997 }
2998
Kim Phillips9b7b8372013-02-28 12:53:52 +00002999 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3000 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3001 my $flag = $1;
3002 my $replacement = {
3003 'EXTRA_AFLAGS' => 'asflags-y',
3004 'EXTRA_CFLAGS' => 'ccflags-y',
3005 'EXTRA_CPPFLAGS' => 'cppflags-y',
3006 'EXTRA_LDFLAGS' => 'ldflags-y',
3007 };
3008
3009 WARN("DEPRECATED_VARIABLE",
3010 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3011 }
3012
Tom Rinia50f8de2014-02-27 08:27:28 -05003013# check for DT compatible documentation
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003014 if (defined $root &&
3015 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3016 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3017
Tom Rinia50f8de2014-02-27 08:27:28 -05003018 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3019
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003020 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Simon Glassa33d9822020-05-22 16:32:35 -06003021 my $vp_file = $dt_path . "vendor-prefixes.txt";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003022
Tom Rinia50f8de2014-02-27 08:27:28 -05003023 foreach my $compat (@compats) {
3024 my $compat2 = $compat;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003025 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3026 my $compat3 = $compat;
3027 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3028 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Tom Rinia50f8de2014-02-27 08:27:28 -05003029 if ( $? >> 8 ) {
3030 WARN("UNDOCUMENTED_DT_STRING",
3031 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3032 }
3033
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003034 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3035 my $vendor = $1;
Simon Glassa33d9822020-05-22 16:32:35 -06003036 `grep -Eq "^$vendor\\b" $vp_file`;
Tom Rinia50f8de2014-02-27 08:27:28 -05003037 if ( $? >> 8 ) {
3038 WARN("UNDOCUMENTED_DT_STRING",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003039 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Tom Rinia50f8de2014-02-27 08:27:28 -05003040 }
3041 }
3042 }
3043
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003044# check for using SPDX license tag at beginning of files
3045 if ($realline == $checklicenseline) {
3046 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3047 $checklicenseline = 2;
3048 } elsif ($rawline =~ /^\+/) {
3049 my $comment = "";
3050 if ($realfile =~ /\.(h|s|S)$/) {
3051 $comment = '/*';
3052 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3053 $comment = '//';
3054 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
3055 $comment = '#';
3056 } elsif ($realfile =~ /\.rst$/) {
3057 $comment = '..';
3058 }
3059
3060 if ($comment !~ /^$/ &&
Simon Glassa33d9822020-05-22 16:32:35 -06003061 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
3062 WARN("SPDX_LICENSE_TAG",
3063 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02003064 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Simon Glassa33d9822020-05-22 16:32:35 -06003065 my $spdx_license = $1;
3066 if (!is_SPDX_License_valid($spdx_license)) {
3067 WARN("SPDX_LICENSE_TAG",
3068 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3069 }
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003070 }
3071 }
3072 }
3073
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003074# check we are in a valid source file if not then ignore this hunk
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003075 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003076
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003077# line length limit (with some exclusions)
3078#
3079# There are a few types of lines that may extend beyond $max_line_length:
3080# logging functions like pr_info that end in a string
3081# lines with a single string
3082# #defines that are a single string
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003083# lines with an RFC3986 like URL
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003084#
3085# There are 3 different line length message types:
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003086# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003087# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3088# LONG_LINE all other lines longer than $max_line_length
3089#
3090# if LONG_LINE is ignored, the other 2 types are also ignored
3091#
Kim Phillips9b7b8372013-02-28 12:53:52 +00003092
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003093 if ($line =~ /^\+/ && $length > $max_line_length) {
3094 my $msg_type = "LONG_LINE";
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003095
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003096 # Check the allowed long line types first
3097
3098 # logging functions that end in a string that starts
3099 # before $max_line_length
3100 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3101 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3102 $msg_type = "";
3103
3104 # lines with only strings (w/ possible termination)
3105 # #defines with only strings
3106 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3107 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3108 $msg_type = "";
3109
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003110 # More special cases
3111 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3112 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3113 $msg_type = "";
3114
3115 # URL ($rawline is used in case the URL is in a comment)
3116 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003117 $msg_type = "";
3118
3119 # Otherwise set the alternate message types
3120
3121 # a comment starts before $max_line_length
3122 } elsif ($line =~ /($;[\s$;]*)$/ &&
3123 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3124 $msg_type = "LONG_LINE_COMMENT"
3125
3126 # a quoted string starts before $max_line_length
3127 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3128 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3129 $msg_type = "LONG_LINE_STRING"
Tom Rinia50f8de2014-02-27 08:27:28 -05003130 }
3131
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003132 if ($msg_type ne "" &&
3133 (show_type("LONG_LINE") || show_type($msg_type))) {
Simon Glassa33d9822020-05-22 16:32:35 -06003134 my $msg_level = \&WARN;
3135 $msg_level = \&CHK if ($file);
3136 &{$msg_level}($msg_type,
3137 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003138 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003139 }
3140
3141# check for adding lines without a newline.
3142 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3143 WARN("MISSING_EOF_NEWLINE",
3144 "adding a line without newline at end of file\n" . $herecurr);
3145 }
3146
Simon Glassc8f4d092020-05-22 16:32:36 -06003147 if ($u_boot) {
3148 u_boot_line($realfile, $line, $herecurr);
3149 }
3150
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003151# check we are in a valid source file C or perl if not then ignore this hunk
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003152 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003153
3154# at the beginning of a line any tabs must come first and anything
3155# more than 8 must use tabs.
3156 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3157 $rawline =~ /^\+\s* \s*/) {
3158 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003159 $rpt_cleaners = 1;
Tom Rinia50f8de2014-02-27 08:27:28 -05003160 if (ERROR("CODE_INDENT",
3161 "code indent should use tabs where possible\n" . $herevet) &&
3162 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003163 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Tom Rinia50f8de2014-02-27 08:27:28 -05003164 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003165 }
3166
3167# check for space before tabs.
3168 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3169 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05003170 if (WARN("SPACE_BEFORE_TAB",
3171 "please, no space before tabs\n" . $herevet) &&
3172 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003173 while ($fixed[$fixlinenr] =~
Joe Perchesdc22ede2014-09-23 12:41:02 +02003174 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003175 while ($fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05003176 s/(^\+.*) +\t/$1\t/) {}
3177 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003178 }
3179
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02003180# check for assignments on the start of a line
3181 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3182 CHK("ASSIGNMENT_CONTINUATIONS",
3183 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3184 }
3185
Kim Phillips9b7b8372013-02-28 12:53:52 +00003186# check for && or || at the start of a line
3187 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3188 CHK("LOGICAL_CONTINUATIONS",
3189 "Logical continuations should be on the previous line\n" . $hereprev);
3190 }
3191
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003192# check indentation starts on a tab stop
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02003193 if ($perl_version_ok &&
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003194 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003195 my $indent = length($1);
3196 if ($indent % 8) {
3197 if (WARN("TABSTOP",
3198 "Statements should start on a tabstop\n" . $herecurr) &&
3199 $fix) {
3200 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3201 }
3202 }
3203 }
3204
Kim Phillips9b7b8372013-02-28 12:53:52 +00003205# check multi-line statement indentation matches previous line
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02003206 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003207 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00003208 $prevline =~ /^\+(\t*)(.*)$/;
3209 my $oldindent = $1;
3210 my $rest = $2;
3211
3212 my $pos = pos_last_openparen($rest);
3213 if ($pos >= 0) {
3214 $line =~ /^(\+| )([ \t]*)/;
3215 my $newindent = $2;
3216
3217 my $goodtabindent = $oldindent .
3218 "\t" x ($pos / 8) .
3219 " " x ($pos % 8);
3220 my $goodspaceindent = $oldindent . " " x $pos;
3221
3222 if ($newindent ne $goodtabindent &&
3223 $newindent ne $goodspaceindent) {
Tom Rinia50f8de2014-02-27 08:27:28 -05003224
3225 if (CHK("PARENTHESIS_ALIGNMENT",
3226 "Alignment should match open parenthesis\n" . $hereprev) &&
3227 $fix && $line =~ /^\+/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003228 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05003229 s/^\+[ \t]*/\+$goodtabindent/;
3230 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00003231 }
3232 }
3233 }
3234
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003235# check for space after cast like "(int) foo" or "(struct foo) bar"
3236# avoid checking a few false positives:
3237# "sizeof(<type>)" or "__alignof__(<type>)"
3238# function pointer declarations like "(*foo)(int) = bar;"
3239# structure definitions like "(struct foo) { 0 };"
3240# multiline macros that define functions
3241# known attributes or the __attribute__ keyword
3242 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3243 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05003244 if (CHK("SPACING",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003245 "No space is necessary after a cast\n" . $herecurr) &&
Tom Rinia50f8de2014-02-27 08:27:28 -05003246 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003247 $fixed[$fixlinenr] =~
3248 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Tom Rinia50f8de2014-02-27 08:27:28 -05003249 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00003250 }
3251
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003252# Block comment styles
3253# Networking with an initial /*
Kim Phillips9b7b8372013-02-28 12:53:52 +00003254 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Tom Rinia50f8de2014-02-27 08:27:28 -05003255 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003256 $rawline =~ /^\+[ \t]*\*/ &&
3257 $realline > 2) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00003258 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3259 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3260 }
3261
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003262# Block comments use * on subsequent lines
3263 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3264 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Tom Rinia50f8de2014-02-27 08:27:28 -05003265 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3266 $rawline =~ /^\+/ && #line is new
3267 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003268 WARN("BLOCK_COMMENT_STYLE",
3269 "Block comments use * on subsequent lines\n" . $hereprev);
Tom Rinia50f8de2014-02-27 08:27:28 -05003270 }
3271
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003272# Block comments use */ on trailing lines
3273 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Kim Phillips9b7b8372013-02-28 12:53:52 +00003274 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3275 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3276 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003277 WARN("BLOCK_COMMENT_STYLE",
3278 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Kim Phillips9b7b8372013-02-28 12:53:52 +00003279 }
3280
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003281# Block comment * alignment
3282 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3283 $line =~ /^\+[ \t]*$;/ && #leading comment
3284 $rawline =~ /^\+[ \t]*\*/ && #leading *
3285 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
3286 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3287 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3288 my $oldindent;
3289 $prevrawline =~ m@^\+([ \t]*/?)\*@;
3290 if (defined($1)) {
3291 $oldindent = expand_tabs($1);
3292 } else {
3293 $prevrawline =~ m@^\+(.*/?)\*@;
3294 $oldindent = expand_tabs($1);
3295 }
3296 $rawline =~ m@^\+([ \t]*)\*@;
3297 my $newindent = $1;
3298 $newindent = expand_tabs($newindent);
3299 if (length($oldindent) ne length($newindent)) {
3300 WARN("BLOCK_COMMENT_STYLE",
3301 "Block comments should align the * on each line\n" . $hereprev);
3302 }
3303 }
3304
3305# check for missing blank lines after struct/union declarations
3306# with exceptions for various attributes and macros
3307 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3308 $line =~ /^\+/ &&
3309 !($line =~ /^\+\s*$/ ||
3310 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3311 $line =~ /^\+\s*MODULE_/i ||
3312 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3313 $line =~ /^\+[a-z_]*init/ ||
3314 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3315 $line =~ /^\+\s*DECLARE/ ||
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003316 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003317 $line =~ /^\+\s*__setup/)) {
3318 if (CHK("LINE_SPACING",
3319 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3320 $fix) {
3321 fix_insert_line($fixlinenr, "\+");
3322 }
3323 }
3324
3325# check for multiple consecutive blank lines
3326 if ($prevline =~ /^[\+ ]\s*$/ &&
3327 $line =~ /^\+\s*$/ &&
3328 $last_blank_line != ($linenr - 1)) {
3329 if (CHK("LINE_SPACING",
3330 "Please don't use multiple blank lines\n" . $hereprev) &&
3331 $fix) {
3332 fix_delete_line($fixlinenr, $rawline);
3333 }
3334
3335 $last_blank_line = $linenr;
3336 }
3337
3338# check for missing blank lines after declarations
3339 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3340 # actual declarations
3341 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3342 # function pointer declarations
3343 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3344 # foo bar; where foo is some local typedef or #define
3345 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3346 # known declaration macros
3347 $prevline =~ /^\+\s+$declaration_macros/) &&
3348 # for "else if" which can look like "$Ident $Ident"
3349 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3350 # other possible extensions of declaration lines
3351 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3352 # not starting a section or a macro "\" extended line
3353 $prevline =~ /(?:\{\s*|\\)$/) &&
3354 # looks like a declaration
3355 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3356 # function pointer declarations
3357 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3358 # foo bar; where foo is some local typedef or #define
3359 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3360 # known declaration macros
3361 $sline =~ /^\+\s+$declaration_macros/ ||
3362 # start of struct or union or enum
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02003363 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003364 # start or end of block or continuation of declaration
3365 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3366 # bitfield continuation
3367 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3368 # other possible extensions of declaration lines
3369 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3370 # indentation of previous and current line are the same
3371 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3372 if (WARN("LINE_SPACING",
3373 "Missing a blank line after declarations\n" . $hereprev) &&
3374 $fix) {
3375 fix_insert_line($fixlinenr, "\+");
3376 }
3377 }
3378
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003379# check for spaces at the beginning of a line.
3380# Exceptions:
3381# 1) within comments
3382# 2) indented preprocessor commands
3383# 3) hanging labels
Tom Rinia50f8de2014-02-27 08:27:28 -05003384 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003385 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05003386 if (WARN("LEADING_SPACE",
3387 "please, no spaces at the start of a line\n" . $herevet) &&
3388 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003389 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Tom Rinia50f8de2014-02-27 08:27:28 -05003390 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003391 }
3392
3393# check we are in a valid C source file if not then ignore this hunk
3394 next if ($realfile !~ /\.(h|c)$/);
3395
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003396# check for unusual line ending [ or (
3397 if ($line =~ /^\+.*([\[\(])\s*$/) {
3398 CHK("OPEN_ENDED_LINE",
3399 "Lines should not end with a '$1'\n" . $herecurr);
3400 }
3401
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003402# check if this appears to be the start function declaration, save the name
3403 if ($sline =~ /^\+\{\s*$/ &&
3404 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3405 $context_function = $1;
3406 }
3407
3408# check if this appears to be the end of function declaration
3409 if ($sline =~ /^\+\}\s*$/) {
3410 undef $context_function;
3411 }
3412
3413# check indentation of any line with a bare else
3414# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3415# if the previous line is a break or return and is indented 1 tab more...
3416 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3417 my $tabs = length($1) + 1;
3418 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3419 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3420 defined $lines[$linenr] &&
3421 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3422 WARN("UNNECESSARY_ELSE",
3423 "else is not generally useful after a break or return\n" . $hereprev);
3424 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00003425 }
3426
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003427# check indentation of a line with a break;
3428# if the previous line is a goto or return and is indented the same # of tabs
3429 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3430 my $tabs = $1;
3431 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3432 WARN("UNNECESSARY_BREAK",
3433 "break is not useful after a goto or return\n" . $hereprev);
3434 }
3435 }
3436
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003437# check for RCS/CVS revision markers
3438 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3439 WARN("CVS_KEYWORD",
3440 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3441 }
3442
Kim Phillips9b7b8372013-02-28 12:53:52 +00003443# check for old HOTPLUG __dev<foo> section markings
3444 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3445 WARN("HOTPLUG_SECTION",
3446 "Using $1 is unnecessary\n" . $herecurr);
3447 }
3448
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003449# Check for potential 'bare' types
3450 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3451 $realline_next);
Kim Phillips9b7b8372013-02-28 12:53:52 +00003452#print "LINE<$line>\n";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003453 if ($linenr > $suppress_statement &&
Tom Rinia50f8de2014-02-27 08:27:28 -05003454 $realcnt && $sline =~ /.\s*\S/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003455 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3456 ctx_statement_block($linenr, $realcnt, 0);
3457 $stat =~ s/\n./\n /g;
3458 $cond =~ s/\n./\n /g;
3459
Kim Phillips9b7b8372013-02-28 12:53:52 +00003460#print "linenr<$linenr> <$stat>\n";
3461 # If this statement has no statement boundaries within
3462 # it there is no point in retrying a statement scan
3463 # until we hit end of it.
3464 my $frag = $stat; $frag =~ s/;+\s*$//;
3465 if ($frag !~ /(?:{|;)/) {
3466#print "skip<$line_nr_next>\n";
3467 $suppress_statement = $line_nr_next;
3468 }
3469
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003470 # Find the real next line.
3471 $realline_next = $line_nr_next;
3472 if (defined $realline_next &&
3473 (!defined $lines[$realline_next - 1] ||
3474 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3475 $realline_next++;
3476 }
3477
3478 my $s = $stat;
3479 $s =~ s/{.*$//s;
3480
3481 # Ignore goto labels.
3482 if ($s =~ /$Ident:\*$/s) {
3483
3484 # Ignore functions being called
3485 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3486
3487 } elsif ($s =~ /^.\s*else\b/s) {
3488
3489 # declarations always start with types
3490 } 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) {
3491 my $type = $1;
3492 $type =~ s/\s+/ /g;
3493 possible($type, "A:" . $s);
3494
3495 # definitions in global scope can only start with types
3496 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3497 possible($1, "B:" . $s);
3498 }
3499
3500 # any (foo ... *) is a pointer cast, and foo is a type
3501 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3502 possible($1, "C:" . $s);
3503 }
3504
3505 # Check for any sort of function declaration.
3506 # int foo(something bar, other baz);
3507 # void (*store_gdt)(x86_descr_ptr *);
3508 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3509 my ($name_len) = length($1);
3510
3511 my $ctx = $s;
3512 substr($ctx, 0, $name_len + 1, '');
3513 $ctx =~ s/\)[^\)]*$//;
3514
3515 for my $arg (split(/\s*,\s*/, $ctx)) {
3516 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3517
3518 possible($1, "D:" . $s);
3519 }
3520 }
3521 }
3522
3523 }
3524
3525#
3526# Checks which may be anchored in the context.
3527#
3528
3529# Check for switch () and associated case and default
3530# statements should be at the same indent.
3531 if ($line=~/\bswitch\s*\(.*\)/) {
3532 my $err = '';
3533 my $sep = '';
3534 my @ctx = ctx_block_outer($linenr, $realcnt);
3535 shift(@ctx);
3536 for my $ctx (@ctx) {
3537 my ($clen, $cindent) = line_stats($ctx);
3538 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3539 $indent != $cindent) {
3540 $err .= "$sep$ctx\n";
3541 $sep = '';
3542 } else {
3543 $sep = "[...]\n";
3544 }
3545 }
3546 if ($err ne '') {
3547 ERROR("SWITCH_CASE_INDENT_LEVEL",
3548 "switch and case should be at the same indent\n$hereline$err");
3549 }
3550 }
3551
3552# if/while/etc brace do not go on next line, unless defining a do while loop,
3553# or if that brace on the next line is for something else
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003554 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003555 my $pre_ctx = "$1$2";
3556
3557 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Kim Phillips9b7b8372013-02-28 12:53:52 +00003558
3559 if ($line =~ /^\+\t{6,}/) {
3560 WARN("DEEP_INDENTATION",
3561 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3562 }
3563
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003564 my $ctx_cnt = $realcnt - $#ctx - 1;
3565 my $ctx = join("\n", @ctx);
3566
3567 my $ctx_ln = $linenr;
3568 my $ctx_skip = $realcnt;
3569
3570 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3571 defined $lines[$ctx_ln - 1] &&
3572 $lines[$ctx_ln - 1] =~ /^-/)) {
3573 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3574 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3575 $ctx_ln++;
3576 }
3577
3578 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3579 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3580
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003581 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003582 ERROR("OPEN_BRACE",
3583 "that open brace { should be on the previous line\n" .
3584 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3585 }
3586 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3587 $ctx =~ /\)\s*\;\s*$/ &&
3588 defined $lines[$ctx_ln - 1])
3589 {
3590 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3591 if ($nindent > $indent) {
3592 WARN("TRAILING_SEMICOLON",
3593 "trailing semicolon indicates no statements, indent implies otherwise\n" .
3594 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3595 }
3596 }
3597 }
3598
3599# Check relative indent for conditionals and blocks.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003600 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00003601 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3602 ctx_statement_block($linenr, $realcnt, 0)
3603 if (!defined $stat);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003604 my ($s, $c) = ($stat, $cond);
3605
3606 substr($s, 0, length($c), '');
3607
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003608 # remove inline comments
3609 $s =~ s/$;/ /g;
3610 $c =~ s/$;/ /g;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003611
3612 # Find out how long the conditional actually is.
3613 my @newlines = ($c =~ /\n/gs);
3614 my $cond_lines = 1 + $#newlines;
3615
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003616 # Make sure we remove the line prefixes as we have
3617 # none on the first line, and are going to readd them
3618 # where necessary.
3619 $s =~ s/\n./\n/gs;
3620 while ($s =~ /\n\s+\\\n/) {
3621 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3622 }
3623
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003624 # We want to check the first line inside the block
3625 # starting at the end of the conditional, so remove:
3626 # 1) any blank line termination
3627 # 2) any opening brace { on end of the line
3628 # 3) any do (...) {
3629 my $continuation = 0;
3630 my $check = 0;
3631 $s =~ s/^.*\bdo\b//;
3632 $s =~ s/^\s*{//;
3633 if ($s =~ s/^\s*\\//) {
3634 $continuation = 1;
3635 }
3636 if ($s =~ s/^\s*?\n//) {
3637 $check = 1;
3638 $cond_lines++;
3639 }
3640
3641 # Also ignore a loop construct at the end of a
3642 # preprocessor statement.
3643 if (($prevline =~ /^.\s*#\s*define\s/ ||
3644 $prevline =~ /\\\s*$/) && $continuation == 0) {
3645 $check = 0;
3646 }
3647
3648 my $cond_ptr = -1;
3649 $continuation = 0;
3650 while ($cond_ptr != $cond_lines) {
3651 $cond_ptr = $cond_lines;
3652
3653 # If we see an #else/#elif then the code
3654 # is not linear.
3655 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3656 $check = 0;
3657 }
3658
3659 # Ignore:
3660 # 1) blank lines, they should be at 0,
3661 # 2) preprocessor lines, and
3662 # 3) labels.
3663 if ($continuation ||
3664 $s =~ /^\s*?\n/ ||
3665 $s =~ /^\s*#\s*?/ ||
3666 $s =~ /^\s*$Ident\s*:/) {
3667 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
3668 if ($s =~ s/^.*?\n//) {
3669 $cond_lines++;
3670 }
3671 }
3672 }
3673
3674 my (undef, $sindent) = line_stats("+" . $s);
3675 my $stat_real = raw_line($linenr, $cond_lines);
3676
3677 # Check if either of these lines are modified, else
3678 # this is not this patch's fault.
3679 if (!defined($stat_real) ||
3680 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3681 $check = 0;
3682 }
3683 if (defined($stat_real) && $cond_lines > 1) {
3684 $stat_real = "[...]\n$stat_real";
3685 }
3686
3687 #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";
3688
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003689 if ($check && $s ne '' &&
3690 (($sindent % 8) != 0 ||
3691 ($sindent < $indent) ||
3692 ($sindent == $indent &&
3693 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
3694 ($sindent > $indent + 8))) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003695 WARN("SUSPECT_CODE_INDENT",
3696 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
3697 }
3698 }
3699
3700 # Track the 'values' across context and added lines.
3701 my $opline = $line; $opline =~ s/^./ /;
3702 my ($curr_values, $curr_vars) =
3703 annotate_values($opline . "\n", $prev_values);
3704 $curr_values = $prev_values . $curr_values;
3705 if ($dbg_values) {
3706 my $outline = $opline; $outline =~ s/\t/ /g;
3707 print "$linenr > .$outline\n";
3708 print "$linenr > $curr_values\n";
3709 print "$linenr > $curr_vars\n";
3710 }
3711 $prev_values = substr($curr_values, -1);
3712
3713#ignore lines not being added
Tom Rinia50f8de2014-02-27 08:27:28 -05003714 next if ($line =~ /^[^\+]/);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003715
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003716# check for dereferences that span multiple lines
3717 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3718 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3719 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3720 my $ref = $1;
3721 $line =~ /^.\s*($Lval)/;
3722 $ref .= $1;
3723 $ref =~ s/\s//g;
3724 WARN("MULTILINE_DEREFERENCE",
3725 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3726 }
3727
3728# check for declarations of signed or unsigned without int
3729 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3730 my $type = $1;
3731 my $var = $2;
3732 $var = "" if (!defined $var);
3733 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3734 my $sign = $1;
3735 my $pointer = $2;
3736
3737 $pointer = "" if (!defined $pointer);
3738
3739 if (WARN("UNSPECIFIED_INT",
3740 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3741 $fix) {
3742 my $decl = trim($sign) . " int ";
3743 my $comp_pointer = $pointer;
3744 $comp_pointer =~ s/\s//g;
3745 $decl .= $comp_pointer;
3746 $decl = rtrim($decl) if ($var eq "");
3747 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3748 }
3749 }
3750 }
3751
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003752# TEST: allow direct testing of the type matcher.
3753 if ($dbg_type) {
3754 if ($line =~ /^.\s*$Declare\s*$/) {
3755 ERROR("TEST_TYPE",
3756 "TEST: is type\n" . $herecurr);
3757 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3758 ERROR("TEST_NOT_TYPE",
3759 "TEST: is not type ($1 is)\n". $herecurr);
3760 }
3761 next;
3762 }
3763# TEST: allow direct testing of the attribute matcher.
3764 if ($dbg_attr) {
3765 if ($line =~ /^.\s*$Modifier\s*$/) {
3766 ERROR("TEST_ATTR",
3767 "TEST: is attr\n" . $herecurr);
3768 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3769 ERROR("TEST_NOT_ATTR",
3770 "TEST: is not attr ($1 is)\n". $herecurr);
3771 }
3772 next;
3773 }
3774
3775# check for initialisation to aggregates open brace on the next line
3776 if ($line =~ /^.\s*{/ &&
3777 $prevline =~ /(?:^|[^=])=\s*$/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003778 if (ERROR("OPEN_BRACE",
3779 "that open brace { should be on the previous line\n" . $hereprev) &&
3780 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3781 fix_delete_line($fixlinenr - 1, $prevrawline);
3782 fix_delete_line($fixlinenr, $rawline);
3783 my $fixedline = $prevrawline;
3784 $fixedline =~ s/\s*=\s*$/ = {/;
3785 fix_insert_line($fixlinenr, $fixedline);
3786 $fixedline = $line;
3787 $fixedline =~ s/^(.\s*)\{\s*/$1/;
3788 fix_insert_line($fixlinenr, $fixedline);
3789 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003790 }
3791
3792#
3793# Checks which are anchored on the added line.
3794#
3795
3796# check for malformed paths in #include statements (uses RAW line)
3797 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3798 my $path = $1;
3799 if ($path =~ m{//}) {
3800 ERROR("MALFORMED_INCLUDE",
Kim Phillips9b7b8372013-02-28 12:53:52 +00003801 "malformed #include filename\n" . $herecurr);
3802 }
3803 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3804 ERROR("UAPI_INCLUDE",
3805 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003806 }
3807 }
3808
3809# no C99 // comments
3810 if ($line =~ m{//}) {
Tom Rinia50f8de2014-02-27 08:27:28 -05003811 if (ERROR("C99_COMMENTS",
3812 "do not use C99 // comments\n" . $herecurr) &&
3813 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003814 my $line = $fixed[$fixlinenr];
Tom Rinia50f8de2014-02-27 08:27:28 -05003815 if ($line =~ /\/\/(.*)$/) {
3816 my $comment = trim($1);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003817 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Tom Rinia50f8de2014-02-27 08:27:28 -05003818 }
3819 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003820 }
3821 # Remove C99 comments.
3822 $line =~ s@//.*@@;
3823 $opline =~ s@//.*@@;
3824
3825# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3826# the whole statement.
3827#print "APW <$lines[$realline_next - 1]>\n";
3828 if (defined $realline_next &&
3829 exists $lines[$realline_next - 1] &&
3830 !defined $suppress_export{$realline_next} &&
3831 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3832 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3833 # Handle definitions which produce identifiers with
3834 # a prefix:
3835 # XXX(foo);
3836 # EXPORT_SYMBOL(something_foo);
3837 my $name = $1;
Kim Phillips9b7b8372013-02-28 12:53:52 +00003838 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003839 $name =~ /^${Ident}_$2/) {
3840#print "FOO C name<$name>\n";
3841 $suppress_export{$realline_next} = 1;
3842
3843 } elsif ($stat !~ /(?:
3844 \n.}\s*$|
3845 ^.DEFINE_$Ident\(\Q$name\E\)|
3846 ^.DECLARE_$Ident\(\Q$name\E\)|
3847 ^.LIST_HEAD\(\Q$name\E\)|
3848 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3849 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
3850 )/x) {
3851#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3852 $suppress_export{$realline_next} = 2;
3853 } else {
3854 $suppress_export{$realline_next} = 1;
3855 }
3856 }
3857 if (!defined $suppress_export{$linenr} &&
3858 $prevline =~ /^.\s*$/ &&
3859 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3860 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3861#print "FOO B <$lines[$linenr - 1]>\n";
3862 $suppress_export{$linenr} = 2;
3863 }
3864 if (defined $suppress_export{$linenr} &&
3865 $suppress_export{$linenr} == 2) {
3866 WARN("EXPORT_SYMBOL",
3867 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
3868 }
3869
3870# check for global initialisers.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003871 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05003872 if (ERROR("GLOBAL_INITIALISERS",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003873 "do not initialise globals to $1\n" . $herecurr) &&
Tom Rinia50f8de2014-02-27 08:27:28 -05003874 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003875 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Tom Rinia50f8de2014-02-27 08:27:28 -05003876 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003877 }
3878# check for static initialisers.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003879 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05003880 if (ERROR("INITIALISED_STATIC",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003881 "do not initialise statics to $1\n" .
Tom Rinia50f8de2014-02-27 08:27:28 -05003882 $herecurr) &&
3883 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003884 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Tom Rinia50f8de2014-02-27 08:27:28 -05003885 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003886 }
3887
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003888# check for misordered declarations of char/short/int/long with signed/unsigned
3889 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3890 my $tmp = trim($1);
3891 WARN("MISORDERED_TYPE",
3892 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3893 }
3894
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02003895# check for unnecessary <signed> int declarations of short/long/long long
3896 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3897 my $type = trim($1);
3898 next if ($type !~ /\bint\b/);
3899 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3900 my $new_type = $type;
3901 $new_type =~ s/\b\s*int\s*\b/ /;
3902 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3903 $new_type =~ s/^const\s+//;
3904 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3905 $new_type = "const $new_type" if ($type =~ /^const\b/);
3906 $new_type =~ s/\s+/ /g;
3907 $new_type = trim($new_type);
3908 if (WARN("UNNECESSARY_INT",
3909 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3910 $fix) {
3911 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3912 }
3913 }
3914
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003915# check for static const char * arrays.
3916 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3917 WARN("STATIC_CONST_CHAR_ARRAY",
3918 "static const char * array should probably be static const char * const\n" .
3919 $herecurr);
Simon Glassa33d9822020-05-22 16:32:35 -06003920 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003921
3922# check for static char foo[] = "bar" declarations.
3923 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3924 WARN("STATIC_CONST_CHAR_ARRAY",
3925 "static char array declaration should probably be static const char\n" .
3926 $herecurr);
Simon Glassa33d9822020-05-22 16:32:35 -06003927 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003928
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003929# check for const <foo> const where <foo> is not a pointer or array type
3930 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3931 my $found = $1;
3932 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3933 WARN("CONST_CONST",
3934 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3935 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3936 WARN("CONST_CONST",
3937 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3938 }
3939 }
3940
3941# check for non-global char *foo[] = {"bar", ...} declarations.
3942 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3943 WARN("STATIC_CONST_CHAR_ARRAY",
3944 "char * array declaration might be better as static const\n" .
3945 $herecurr);
3946 }
3947
3948# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3949 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3950 my $array = $1;
3951 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3952 my $array_div = $1;
3953 if (WARN("ARRAY_SIZE",
3954 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3955 $fix) {
3956 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3957 }
3958 }
3959 }
3960
Tom Rinia50f8de2014-02-27 08:27:28 -05003961# check for function declarations without arguments like "int foo()"
3962 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3963 if (ERROR("FUNCTION_WITHOUT_ARGS",
3964 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3965 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003966 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Tom Rinia50f8de2014-02-27 08:27:28 -05003967 }
3968 }
3969
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003970# check for new typedefs, only function parameters and sparse annotations
3971# make sense.
3972 if ($line =~ /\btypedef\s/ &&
3973 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3974 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3975 $line !~ /\b$typeTypedefs\b/ &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003976 $line !~ /\b__bitwise\b/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003977 WARN("NEW_TYPEDEFS",
3978 "do not add new typedefs\n" . $herecurr);
3979 }
3980
3981# * goes on variable not on type
3982 # (char*[ const])
Kim Phillips9b7b8372013-02-28 12:53:52 +00003983 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3984 #print "AA<$1>\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05003985 my ($ident, $from, $to) = ($1, $2, $2);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003986
3987 # Should start with a space.
3988 $to =~ s/^(\S)/ $1/;
3989 # Should not end with a space.
3990 $to =~ s/\s+$//;
3991 # '*'s should not have spaces between.
3992 while ($to =~ s/\*\s+\*/\*\*/) {
3993 }
3994
Tom Rinia50f8de2014-02-27 08:27:28 -05003995## print "1: from<$from> to<$to> ident<$ident>\n";
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003996 if ($from ne $to) {
Tom Rinia50f8de2014-02-27 08:27:28 -05003997 if (ERROR("POINTER_LOCATION",
3998 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3999 $fix) {
4000 my $sub_from = $ident;
4001 my $sub_to = $ident;
4002 $sub_to =~ s/\Q$from\E/$to/;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004003 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004004 s@\Q$sub_from\E@$sub_to@;
4005 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004006 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00004007 }
4008 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4009 #print "BB<$1>\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05004010 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004011
4012 # Should start with a space.
4013 $to =~ s/^(\S)/ $1/;
4014 # Should not end with a space.
4015 $to =~ s/\s+$//;
4016 # '*'s should not have spaces between.
4017 while ($to =~ s/\*\s+\*/\*\*/) {
4018 }
4019 # Modifiers should have spaces.
4020 $to =~ s/(\b$Modifier$)/$1 /;
4021
Tom Rinia50f8de2014-02-27 08:27:28 -05004022## print "2: from<$from> to<$to> ident<$ident>\n";
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004023 if ($from ne $to && $ident !~ /^$Modifier$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004024 if (ERROR("POINTER_LOCATION",
4025 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4026 $fix) {
4027
4028 my $sub_from = $match;
4029 my $sub_to = $match;
4030 $sub_to =~ s/\Q$from\E/$to/;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004031 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004032 s@\Q$sub_from\E@$sub_to@;
4033 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004034 }
4035 }
4036
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004037# avoid BUG() or BUG_ON()
4038 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004039 my $msg_level = \&WARN;
4040 $msg_level = \&CHK if ($file);
4041 &{$msg_level}("AVOID_BUG",
4042 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004043 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004044
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004045# avoid LINUX_VERSION_CODE
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004046 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4047 WARN("LINUX_VERSION_CODE",
4048 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
4049 }
4050
4051# check for uses of printk_ratelimit
4052 if ($line =~ /\bprintk_ratelimit\s*\(/) {
4053 WARN("PRINTK_RATELIMITED",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004054 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004055 }
4056
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004057# printk should use KERN_* levels
4058 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4059 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4060 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004061 }
4062
Kim Phillips9b7b8372013-02-28 12:53:52 +00004063 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4064 my $orig = $1;
4065 my $level = lc($orig);
4066 $level = "warn" if ($level eq "warning");
4067 my $level2 = $level;
4068 $level2 = "dbg" if ($level eq "debug");
4069 WARN("PREFER_PR_LEVEL",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004070 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Kim Phillips9b7b8372013-02-28 12:53:52 +00004071 }
4072
4073 if ($line =~ /\bpr_warning\s*\(/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004074 if (WARN("PREFER_PR_LEVEL",
4075 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4076 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004077 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004078 s/\bpr_warning\b/pr_warn/;
4079 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00004080 }
4081
4082 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4083 my $orig = $1;
4084 my $level = lc($orig);
4085 $level = "warn" if ($level eq "warning");
4086 $level = "dbg" if ($level eq "debug");
4087 WARN("PREFER_DEV_LEVEL",
4088 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4089 }
4090
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004091# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4092# number of false positives, but assembly files are not checked, so at
4093# least the arch entry code will not trigger this warning.
4094 if ($line =~ /\bENOSYS\b/) {
4095 WARN("ENOSYS",
4096 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4097 }
4098
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004099# function brace can't be on same line, except for #defines of do while,
4100# or if closed on same line
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004101 if ($perl_version_ok &&
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004102 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4103 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4104 $sline !~ /}/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004105 if (ERROR("OPEN_BRACE",
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004106 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004107 $fix) {
4108 fix_delete_line($fixlinenr, $rawline);
4109 my $fixed_line = $rawline;
4110 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4111 my $line1 = $1;
4112 my $line2 = $2;
4113 fix_insert_line($fixlinenr, ltrim($line1));
4114 fix_insert_line($fixlinenr, "\+{");
4115 if ($line2 !~ /^\s*$/) {
4116 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4117 }
4118 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004119 }
4120
4121# open braces for enum, union and struct go on the same line.
4122 if ($line =~ /^.\s*{/ &&
4123 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004124 if (ERROR("OPEN_BRACE",
4125 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4126 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4127 fix_delete_line($fixlinenr - 1, $prevrawline);
4128 fix_delete_line($fixlinenr, $rawline);
4129 my $fixedline = rtrim($prevrawline) . " {";
4130 fix_insert_line($fixlinenr, $fixedline);
4131 $fixedline = $rawline;
4132 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
4133 if ($fixedline !~ /^\+\s*$/) {
4134 fix_insert_line($fixlinenr, $fixedline);
4135 }
4136 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004137 }
4138
4139# missing space after union, struct or enum definition
Tom Rinia50f8de2014-02-27 08:27:28 -05004140 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4141 if (WARN("SPACING",
4142 "missing space after $1 definition\n" . $herecurr) &&
4143 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004144 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004145 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4146 }
4147 }
4148
4149# Function pointer declarations
4150# check spacing between type, funcptr, and args
4151# canonical declaration is "type (*funcptr)(args...)"
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004152 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004153 my $declare = $1;
4154 my $pre_pointer_space = $2;
4155 my $post_pointer_space = $3;
4156 my $funcname = $4;
4157 my $post_funcname_space = $5;
4158 my $pre_args_space = $6;
4159
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004160# the $Declare variable will capture all spaces after the type
4161# so check it for a missing trailing missing space but pointer return types
4162# don't need a space so don't warn for those.
4163 my $post_declare_space = "";
4164 if ($declare =~ /(\s+)$/) {
4165 $post_declare_space = $1;
4166 $declare = rtrim($declare);
4167 }
4168 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004169 WARN("SPACING",
4170 "missing space after return type\n" . $herecurr);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004171 $post_declare_space = " ";
Tom Rinia50f8de2014-02-27 08:27:28 -05004172 }
4173
4174# unnecessary space "type (*funcptr)(args...)"
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004175# This test is not currently implemented because these declarations are
4176# equivalent to
4177# int foo(int bar, ...)
4178# and this is form shouldn't/doesn't generate a checkpatch warning.
4179#
4180# elsif ($declare =~ /\s{2,}$/) {
4181# WARN("SPACING",
4182# "Multiple spaces after return type\n" . $herecurr);
4183# }
Tom Rinia50f8de2014-02-27 08:27:28 -05004184
4185# unnecessary space "type ( *funcptr)(args...)"
4186 if (defined $pre_pointer_space &&
4187 $pre_pointer_space =~ /^\s/) {
4188 WARN("SPACING",
4189 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4190 }
4191
4192# unnecessary space "type (* funcptr)(args...)"
4193 if (defined $post_pointer_space &&
4194 $post_pointer_space =~ /^\s/) {
4195 WARN("SPACING",
4196 "Unnecessary space before function pointer name\n" . $herecurr);
4197 }
4198
4199# unnecessary space "type (*funcptr )(args...)"
4200 if (defined $post_funcname_space &&
4201 $post_funcname_space =~ /^\s/) {
4202 WARN("SPACING",
4203 "Unnecessary space after function pointer name\n" . $herecurr);
4204 }
4205
4206# unnecessary space "type (*funcptr) (args...)"
4207 if (defined $pre_args_space &&
4208 $pre_args_space =~ /^\s/) {
4209 WARN("SPACING",
4210 "Unnecessary space before function pointer arguments\n" . $herecurr);
4211 }
4212
4213 if (show_type("SPACING") && $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004214 $fixed[$fixlinenr] =~
4215 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Tom Rinia50f8de2014-02-27 08:27:28 -05004216 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004217 }
4218
4219# check for spacing round square brackets; allowed:
4220# 1. with a type on the left -- int [] a;
4221# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4222# 3. inside a curly brace -- = { [0...10] = 5 }
4223 while ($line =~ /(.*?\s)\[/g) {
4224 my ($where, $prefix) = ($-[1], $1);
4225 if ($prefix !~ /$Type\s+$/ &&
4226 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004227 $prefix !~ /[{,:]\s+$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004228 if (ERROR("BRACKET_SPACE",
4229 "space prohibited before open square bracket '['\n" . $herecurr) &&
4230 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004231 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004232 s/^(\+.*?)\s+\[/$1\[/;
4233 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004234 }
4235 }
4236
4237# check for spaces between functions and their parentheses.
4238 while ($line =~ /($Ident)\s+\(/g) {
4239 my $name = $1;
4240 my $ctx_before = substr($line, 0, $-[1]);
4241 my $ctx = "$ctx_before$name";
4242
4243 # Ignore those directives where spaces _are_ permitted.
4244 if ($name =~ /^(?:
4245 if|for|while|switch|return|case|
4246 volatile|__volatile__|
4247 __attribute__|format|__extension__|
4248 asm|__asm__)$/x)
4249 {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004250 # cpp #define statements have non-optional spaces, ie
4251 # if there is a space between the name and the open
4252 # parenthesis it is simply not a parameter group.
4253 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4254
4255 # cpp #elif statement condition may start with a (
4256 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4257
4258 # If this whole things ends with a type its most
4259 # likely a typedef for a function.
4260 } elsif ($ctx =~ /$Type$/) {
4261
4262 } else {
Tom Rinia50f8de2014-02-27 08:27:28 -05004263 if (WARN("SPACING",
4264 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4265 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004266 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004267 s/\b$name\s+\(/$name\(/;
4268 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004269 }
4270 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00004271
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004272# Check operator spacing.
4273 if (!($line=~/\#\s*include/)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004274 my $fixed_line = "";
4275 my $line_fixed = 0;
4276
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004277 my $ops = qr{
4278 <<=|>>=|<=|>=|==|!=|
4279 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4280 =>|->|<<|>>|<|>|=|!|~|
4281 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Tom Rinia50f8de2014-02-27 08:27:28 -05004282 \?:|\?|:
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004283 }x;
4284 my @elements = split(/($ops|;)/, $opline);
Tom Rinia50f8de2014-02-27 08:27:28 -05004285
4286## print("element count: <" . $#elements . ">\n");
4287## foreach my $el (@elements) {
4288## print("el: <$el>\n");
4289## }
4290
4291 my @fix_elements = ();
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004292 my $off = 0;
4293
Tom Rinia50f8de2014-02-27 08:27:28 -05004294 foreach my $el (@elements) {
4295 push(@fix_elements, substr($rawline, $off, length($el)));
4296 $off += length($el);
4297 }
4298
4299 $off = 0;
4300
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004301 my $blank = copy_spacing($opline);
Tom Rinia50f8de2014-02-27 08:27:28 -05004302 my $last_after = -1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004303
4304 for (my $n = 0; $n < $#elements; $n += 2) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004305
4306 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4307
4308## print("n: <$n> good: <$good>\n");
4309
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004310 $off += length($elements[$n]);
4311
4312 # Pick up the preceding and succeeding characters.
4313 my $ca = substr($opline, 0, $off);
4314 my $cc = '';
4315 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4316 $cc = substr($opline, $off + length($elements[$n + 1]));
4317 }
4318 my $cb = "$ca$;$cc";
4319
4320 my $a = '';
4321 $a = 'V' if ($elements[$n] ne '');
4322 $a = 'W' if ($elements[$n] =~ /\s$/);
4323 $a = 'C' if ($elements[$n] =~ /$;$/);
4324 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4325 $a = 'O' if ($elements[$n] eq '');
4326 $a = 'E' if ($ca =~ /^\s*$/);
4327
4328 my $op = $elements[$n + 1];
4329
4330 my $c = '';
4331 if (defined $elements[$n + 2]) {
4332 $c = 'V' if ($elements[$n + 2] ne '');
4333 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
4334 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4335 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4336 $c = 'O' if ($elements[$n + 2] eq '');
4337 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4338 } else {
4339 $c = 'E';
4340 }
4341
4342 my $ctx = "${a}x${c}";
4343
4344 my $at = "(ctx:$ctx)";
4345
4346 my $ptr = substr($blank, 0, $off) . "^";
4347 my $hereptr = "$hereline$ptr\n";
4348
4349 # Pull out the value of this operator.
4350 my $op_type = substr($curr_values, $off + 1, 1);
4351
4352 # Get the full operator variant.
4353 my $opv = $op . substr($curr_vars, $off, 1);
4354
4355 # Ignore operators passed as parameters.
4356 if ($op_type ne 'V' &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004357 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004358
4359# # Ignore comments
4360# } elsif ($op =~ /^$;+$/) {
4361
4362 # ; should have either the end of line or a space or \ after it
4363 } elsif ($op eq ';') {
4364 if ($ctx !~ /.x[WEBC]/ &&
4365 $cc !~ /^\\/ && $cc !~ /^;/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004366 if (ERROR("SPACING",
4367 "space required after that '$op' $at\n" . $hereptr)) {
4368 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4369 $line_fixed = 1;
4370 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004371 }
4372
4373 # // is a comment
4374 } elsif ($op eq '//') {
4375
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004376 # : when part of a bitfield
4377 } elsif ($opv eq ':B') {
4378 # skip the bitfield test for now
4379
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004380 # No spaces for:
4381 # ->
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004382 } elsif ($op eq '->') {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004383 if ($ctx =~ /Wx.|.xW/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004384 if (ERROR("SPACING",
4385 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4386 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4387 if (defined $fix_elements[$n + 2]) {
4388 $fix_elements[$n + 2] =~ s/^\s+//;
4389 }
4390 $line_fixed = 1;
4391 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004392 }
4393
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004394 # , must not have a space before and must have a space on the right.
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004395 } elsif ($op eq ',') {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004396 my $rtrim_before = 0;
4397 my $space_after = 0;
4398 if ($ctx =~ /Wx./) {
4399 if (ERROR("SPACING",
4400 "space prohibited before that '$op' $at\n" . $hereptr)) {
4401 $line_fixed = 1;
4402 $rtrim_before = 1;
4403 }
4404 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004405 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004406 if (ERROR("SPACING",
4407 "space required after that '$op' $at\n" . $hereptr)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004408 $line_fixed = 1;
4409 $last_after = $n;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004410 $space_after = 1;
Tom Rinia50f8de2014-02-27 08:27:28 -05004411 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004412 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004413 if ($rtrim_before || $space_after) {
4414 if ($rtrim_before) {
4415 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4416 } else {
4417 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4418 }
4419 if ($space_after) {
4420 $good .= " ";
4421 }
4422 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004423
4424 # '*' as part of a type definition -- reported already.
4425 } elsif ($opv eq '*_') {
4426 #warn "'*' is part of type\n";
4427
4428 # unary operators should have a space before and
4429 # none after. May be left adjacent to another
4430 # unary operator, or a cast
4431 } elsif ($op eq '!' || $op eq '~' ||
4432 $opv eq '*U' || $opv eq '-U' ||
4433 $opv eq '&U' || $opv eq '&&U') {
4434 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004435 if (ERROR("SPACING",
4436 "space required before that '$op' $at\n" . $hereptr)) {
4437 if ($n != $last_after + 2) {
4438 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4439 $line_fixed = 1;
4440 }
4441 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004442 }
4443 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4444 # A unary '*' may be const
4445
4446 } elsif ($ctx =~ /.xW/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004447 if (ERROR("SPACING",
4448 "space prohibited after that '$op' $at\n" . $hereptr)) {
4449 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
4450 if (defined $fix_elements[$n + 2]) {
4451 $fix_elements[$n + 2] =~ s/^\s+//;
4452 }
4453 $line_fixed = 1;
4454 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004455 }
4456
4457 # unary ++ and unary -- are allowed no space on one side.
4458 } elsif ($op eq '++' or $op eq '--') {
4459 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004460 if (ERROR("SPACING",
4461 "space required one side of that '$op' $at\n" . $hereptr)) {
4462 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4463 $line_fixed = 1;
4464 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004465 }
4466 if ($ctx =~ /Wx[BE]/ ||
4467 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004468 if (ERROR("SPACING",
4469 "space prohibited before that '$op' $at\n" . $hereptr)) {
4470 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4471 $line_fixed = 1;
4472 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004473 }
4474 if ($ctx =~ /ExW/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004475 if (ERROR("SPACING",
4476 "space prohibited after that '$op' $at\n" . $hereptr)) {
4477 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4478 if (defined $fix_elements[$n + 2]) {
4479 $fix_elements[$n + 2] =~ s/^\s+//;
4480 }
4481 $line_fixed = 1;
4482 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004483 }
4484
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004485 # << and >> may either have or not have spaces both sides
4486 } elsif ($op eq '<<' or $op eq '>>' or
4487 $op eq '&' or $op eq '^' or $op eq '|' or
4488 $op eq '+' or $op eq '-' or
4489 $op eq '*' or $op eq '/' or
4490 $op eq '%')
4491 {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004492 if ($check) {
4493 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4494 if (CHK("SPACING",
4495 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4496 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4497 $fix_elements[$n + 2] =~ s/^\s+//;
4498 $line_fixed = 1;
4499 }
4500 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4501 if (CHK("SPACING",
4502 "space preferred before that '$op' $at\n" . $hereptr)) {
4503 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4504 $line_fixed = 1;
4505 }
4506 }
4507 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004508 if (ERROR("SPACING",
4509 "need consistent spacing around '$op' $at\n" . $hereptr)) {
4510 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4511 if (defined $fix_elements[$n + 2]) {
4512 $fix_elements[$n + 2] =~ s/^\s+//;
4513 }
4514 $line_fixed = 1;
4515 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004516 }
4517
4518 # A colon needs no spaces before when it is
4519 # terminating a case value or a label.
4520 } elsif ($opv eq ':C' || $opv eq ':L') {
4521 if ($ctx =~ /Wx./) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004522 if (ERROR("SPACING",
4523 "space prohibited before that '$op' $at\n" . $hereptr)) {
4524 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4525 $line_fixed = 1;
4526 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004527 }
4528
4529 # All the others need spaces both sides.
4530 } elsif ($ctx !~ /[EWC]x[CWE]/) {
4531 my $ok = 0;
4532
4533 # Ignore email addresses <foo@bar>
4534 if (($op eq '<' &&
4535 $cc =~ /^\S+\@\S+>/) ||
4536 ($op eq '>' &&
4537 $ca =~ /<\S+\@\S+$/))
4538 {
Tom Rinia50f8de2014-02-27 08:27:28 -05004539 $ok = 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004540 }
4541
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004542 # for asm volatile statements
4543 # ignore a colon with another
4544 # colon immediately before or after
4545 if (($op eq ':') &&
4546 ($ca =~ /:$/ || $cc =~ /^:/)) {
4547 $ok = 1;
4548 }
4549
Tom Rinia50f8de2014-02-27 08:27:28 -05004550 # messages are ERROR, but ?: are CHK
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004551 if ($ok == 0) {
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004552 my $msg_level = \&ERROR;
4553 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Tom Rinia50f8de2014-02-27 08:27:28 -05004554
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004555 if (&{$msg_level}("SPACING",
4556 "spaces required around that '$op' $at\n" . $hereptr)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004557 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4558 if (defined $fix_elements[$n + 2]) {
4559 $fix_elements[$n + 2] =~ s/^\s+//;
4560 }
4561 $line_fixed = 1;
4562 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004563 }
4564 }
4565 $off += length($elements[$n + 1]);
Tom Rinia50f8de2014-02-27 08:27:28 -05004566
4567## print("n: <$n> GOOD: <$good>\n");
4568
4569 $fixed_line = $fixed_line . $good;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004570 }
Tom Rinia50f8de2014-02-27 08:27:28 -05004571
4572 if (($#elements % 2) == 0) {
4573 $fixed_line = $fixed_line . $fix_elements[$#elements];
4574 }
4575
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004576 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4577 $fixed[$fixlinenr] = $fixed_line;
Tom Rinia50f8de2014-02-27 08:27:28 -05004578 }
4579
4580
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004581 }
4582
Tom Rinia50f8de2014-02-27 08:27:28 -05004583# check for whitespace before a non-naked semicolon
4584 if ($line =~ /^\+.*\S\s+;\s*$/) {
4585 if (WARN("SPACING",
4586 "space prohibited before semicolon\n" . $herecurr) &&
4587 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004588 1 while $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004589 s/^(\+.*\S)\s+;/$1;/;
4590 }
4591 }
4592
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004593# check for multiple assignments
4594 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4595 CHK("MULTIPLE_ASSIGNMENTS",
4596 "multiple assignments should be avoided\n" . $herecurr);
4597 }
4598
4599## # check for multiple declarations, allowing for a function declaration
4600## # continuation.
4601## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4602## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4603##
4604## # Remove any bracketed sections to ensure we do not
4605## # falsly report the parameters of functions.
4606## my $ln = $line;
4607## while ($ln =~ s/\([^\(\)]*\)//g) {
4608## }
4609## if ($ln =~ /,/) {
4610## WARN("MULTIPLE_DECLARATION",
4611## "declaring multiple variables together should be avoided\n" . $herecurr);
4612## }
4613## }
4614
4615#need space before brace following if, while, etc
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004616 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004617 $line =~ /\b(?:else|do)\{/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004618 if (ERROR("SPACING",
4619 "space required before the open brace '{'\n" . $herecurr) &&
4620 $fix) {
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004621 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Tom Rinia50f8de2014-02-27 08:27:28 -05004622 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004623 }
4624
Tom Rinia50f8de2014-02-27 08:27:28 -05004625## # check for blank lines before declarations
4626## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4627## $prevrawline =~ /^.\s*$/) {
4628## WARN("SPACING",
4629## "No blank lines before declarations\n" . $hereprev);
4630## }
4631##
4632
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004633# closing brace should have a space following it when it has anything
4634# on the line
Simon Glassa33d9822020-05-22 16:32:35 -06004635 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004636 if (ERROR("SPACING",
4637 "space required after that close brace '}'\n" . $herecurr) &&
4638 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004639 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004640 s/}((?!(?:,|;|\)))\S)/} $1/;
4641 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004642 }
4643
4644# check spacing on square brackets
4645 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004646 if (ERROR("SPACING",
4647 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4648 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004649 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004650 s/\[\s+/\[/;
4651 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004652 }
4653 if ($line =~ /\s\]/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004654 if (ERROR("SPACING",
4655 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4656 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004657 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004658 s/\s+\]/\]/;
4659 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004660 }
4661
4662# check spacing on parentheses
4663 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4664 $line !~ /for\s*\(\s+;/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004665 if (ERROR("SPACING",
4666 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4667 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004668 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004669 s/\(\s+/\(/;
4670 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004671 }
4672 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4673 $line !~ /for\s*\(.*;\s+\)/ &&
4674 $line !~ /:\s+\)/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004675 if (ERROR("SPACING",
4676 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4677 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004678 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004679 s/\s+\)/\)/;
4680 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004681 }
4682
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004683# check unnecessary parentheses around addressof/dereference single $Lvals
4684# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4685
4686 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4687 my $var = $1;
4688 if (CHK("UNNECESSARY_PARENTHESES",
4689 "Unnecessary parentheses around $var\n" . $herecurr) &&
4690 $fix) {
4691 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4692 }
4693 }
4694
4695# check for unnecessary parentheses around function pointer uses
4696# ie: (foo->bar)(); should be foo->bar();
4697# but not "if (foo->bar) (" to avoid some false positives
4698 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4699 my $var = $2;
4700 if (CHK("UNNECESSARY_PARENTHESES",
4701 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4702 $fix) {
4703 my $var2 = deparenthesize($var);
4704 $var2 =~ s/\s//g;
4705 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4706 }
4707 }
4708
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004709# check for unnecessary parentheses around comparisons in if uses
4710# when !drivers/staging or command-line uses --strict
4711 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004712 $perl_version_ok && defined($stat) &&
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004713 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4714 my $if_stat = $1;
4715 my $test = substr($2, 1, -1);
4716 my $herectx;
4717 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4718 my $match = $1;
4719 # avoid parentheses around potential macro args
4720 next if ($match =~ /^\s*\w+\s*$/);
4721 if (!defined($herectx)) {
4722 $herectx = $here . "\n";
4723 my $cnt = statement_rawlines($if_stat);
4724 for (my $n = 0; $n < $cnt; $n++) {
4725 my $rl = raw_line($linenr, $n);
4726 $herectx .= $rl . "\n";
4727 last if $rl =~ /^[ \+].*\{/;
4728 }
4729 }
4730 CHK("UNNECESSARY_PARENTHESES",
4731 "Unnecessary parentheses around '$match'\n" . $herectx);
4732 }
4733 }
4734
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004735#goto labels aren't indented, allow a single space however
4736 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
4737 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004738 if (WARN("INDENTED_LABEL",
4739 "labels should not be indented\n" . $herecurr) &&
4740 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004741 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004742 s/^(.)\s+/$1/;
4743 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004744 }
4745
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004746# return is not a function
Tom Rinia50f8de2014-02-27 08:27:28 -05004747 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004748 my $spacing = $1;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004749 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004750 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4751 my $value = $1;
4752 $value = deparenthesize($value);
4753 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4754 ERROR("RETURN_PARENTHESES",
4755 "return is not a function, parentheses are not required\n" . $herecurr);
4756 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004757 } elsif ($spacing !~ /\s+/) {
4758 ERROR("SPACING",
4759 "space required before the open parenthesis '('\n" . $herecurr);
4760 }
4761 }
Tom Rinia50f8de2014-02-27 08:27:28 -05004762
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004763# unnecessary return in a void function
4764# at end-of-function, with the previous line a single leading tab, then return;
4765# and the line before that not a goto label target like "out:"
4766 if ($sline =~ /^[ \+]}\s*$/ &&
4767 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4768 $linenr >= 3 &&
4769 $lines[$linenr - 3] =~ /^[ +]/ &&
4770 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
4771 WARN("RETURN_VOID",
4772 "void function return statements are not generally useful\n" . $hereprev);
4773 }
4774
Tom Rinia50f8de2014-02-27 08:27:28 -05004775# if statements using unnecessary parentheses - ie: if ((foo == bar))
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004776 if ($perl_version_ok &&
Tom Rinia50f8de2014-02-27 08:27:28 -05004777 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4778 my $openparens = $1;
4779 my $count = $openparens =~ tr@\(@\(@;
4780 my $msg = "";
4781 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4782 my $comp = $4; #Not $1 because of $LvalOrFunc
4783 $msg = " - maybe == should be = ?" if ($comp eq "==");
4784 WARN("UNNECESSARY_PARENTHESES",
4785 "Unnecessary parentheses$msg\n" . $herecurr);
4786 }
4787 }
4788
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004789# comparisons with a constant or upper case identifier on the left
4790# avoid cases like "foo + BAR < baz"
4791# only fix matches surrounded by parentheses to avoid incorrect
4792# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004793 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004794 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4795 my $lead = $1;
4796 my $const = $2;
4797 my $comp = $3;
4798 my $to = $4;
4799 my $newcomp = $comp;
4800 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4801 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4802 WARN("CONSTANT_COMPARISON",
4803 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4804 $fix) {
4805 if ($comp eq "<") {
4806 $newcomp = ">";
4807 } elsif ($comp eq "<=") {
4808 $newcomp = ">=";
4809 } elsif ($comp eq ">") {
4810 $newcomp = "<";
4811 } elsif ($comp eq ">=") {
4812 $newcomp = "<=";
4813 }
4814 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4815 }
4816 }
4817
4818# Return of what appears to be an errno should normally be negative
4819 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004820 my $name = $1;
4821 if ($name ne 'EOF' && $name ne 'ERROR') {
4822 WARN("USE_NEGATIVE_ERRNO",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004823 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004824 }
4825 }
4826
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004827# Need a space before open parenthesis after if, while etc
Tom Rinia50f8de2014-02-27 08:27:28 -05004828 if ($line =~ /\b(if|while|for|switch)\(/) {
4829 if (ERROR("SPACING",
4830 "space required before the open parenthesis '('\n" . $herecurr) &&
4831 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004832 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004833 s/\b(if|while|for|switch)\(/$1 \(/;
4834 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004835 }
4836
4837# Check for illegal assignment in if conditional -- and check for trailing
4838# statements after the conditional.
4839 if ($line =~ /do\s*(?!{)/) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00004840 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4841 ctx_statement_block($linenr, $realcnt, 0)
4842 if (!defined $stat);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004843 my ($stat_next) = ctx_statement_block($line_nr_next,
4844 $remain_next, $off_next);
4845 $stat_next =~ s/\n./\n /g;
4846 ##print "stat<$stat> stat_next<$stat_next>\n";
4847
4848 if ($stat_next =~ /^\s*while\b/) {
4849 # If the statement carries leading newlines,
4850 # then count those as offsets.
4851 my ($whitespace) =
4852 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4853 my $offset =
4854 statement_rawlines($whitespace) - 1;
4855
4856 $suppress_whiletrailers{$line_nr_next +
4857 $offset} = 1;
4858 }
4859 }
4860 if (!defined $suppress_whiletrailers{$linenr} &&
Tom Rinia50f8de2014-02-27 08:27:28 -05004861 defined($stat) && defined($cond) &&
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004862 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4863 my ($s, $c) = ($stat, $cond);
4864
4865 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4866 ERROR("ASSIGN_IN_IF",
4867 "do not use assignment in if condition\n" . $herecurr);
4868 }
4869
4870 # Find out what is on the end of the line after the
4871 # conditional.
4872 substr($s, 0, length($c), '');
4873 $s =~ s/\n.*//g;
4874 $s =~ s/$;//g; # Remove any comments
4875 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4876 $c !~ /}\s*while\s*/)
4877 {
4878 # Find out how long the conditional actually is.
4879 my @newlines = ($c =~ /\n/gs);
4880 my $cond_lines = 1 + $#newlines;
4881 my $stat_real = '';
4882
4883 $stat_real = raw_line($linenr, $cond_lines)
4884 . "\n" if ($cond_lines);
4885 if (defined($stat_real) && $cond_lines > 1) {
4886 $stat_real = "[...]\n$stat_real";
4887 }
4888
4889 ERROR("TRAILING_STATEMENTS",
4890 "trailing statements should be on next line\n" . $herecurr . $stat_real);
4891 }
4892 }
4893
4894# Check for bitwise tests written as boolean
4895 if ($line =~ /
4896 (?:
4897 (?:\[|\(|\&\&|\|\|)
4898 \s*0[xX][0-9]+\s*
4899 (?:\&\&|\|\|)
4900 |
4901 (?:\&\&|\|\|)
4902 \s*0[xX][0-9]+\s*
4903 (?:\&\&|\|\||\)|\])
4904 )/x)
4905 {
4906 WARN("HEXADECIMAL_BOOLEAN_TEST",
4907 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
4908 }
4909
4910# if and else should not have general statements after it
4911 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4912 my $s = $1;
4913 $s =~ s/$;//g; # Remove any comments
4914 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4915 ERROR("TRAILING_STATEMENTS",
4916 "trailing statements should be on next line\n" . $herecurr);
4917 }
4918 }
4919# if should not continue a brace
4920 if ($line =~ /}\s*if\b/) {
4921 ERROR("TRAILING_STATEMENTS",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004922 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004923 $herecurr);
4924 }
4925# case and default should not have general statements after them
4926 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4927 $line !~ /\G(?:
4928 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4929 \s*return\s+
4930 )/xg)
4931 {
4932 ERROR("TRAILING_STATEMENTS",
4933 "trailing statements should be on next line\n" . $herecurr);
4934 }
4935
4936 # Check for }<nl>else {, these must be at the same
4937 # indent level to be relevant to each other.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004938 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4939 $previndent == $indent) {
4940 if (ERROR("ELSE_AFTER_BRACE",
4941 "else should follow close brace '}'\n" . $hereprev) &&
4942 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4943 fix_delete_line($fixlinenr - 1, $prevrawline);
4944 fix_delete_line($fixlinenr, $rawline);
4945 my $fixedline = $prevrawline;
4946 $fixedline =~ s/}\s*$//;
4947 if ($fixedline !~ /^\+\s*$/) {
4948 fix_insert_line($fixlinenr, $fixedline);
4949 }
4950 $fixedline = $rawline;
4951 $fixedline =~ s/^(.\s*)else/$1} else/;
4952 fix_insert_line($fixlinenr, $fixedline);
4953 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004954 }
4955
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004956 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4957 $previndent == $indent) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004958 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4959
4960 # Find out what is on the end of the line after the
4961 # conditional.
4962 substr($s, 0, length($c), '');
4963 $s =~ s/\n.*//g;
4964
4965 if ($s =~ /^\s*;/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004966 if (ERROR("WHILE_AFTER_BRACE",
4967 "while should follow close brace '}'\n" . $hereprev) &&
4968 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4969 fix_delete_line($fixlinenr - 1, $prevrawline);
4970 fix_delete_line($fixlinenr, $rawline);
4971 my $fixedline = $prevrawline;
4972 my $trailing = $rawline;
4973 $trailing =~ s/^\+//;
4974 $trailing = trim($trailing);
4975 $fixedline =~ s/}\s*$/} $trailing/;
4976 fix_insert_line($fixlinenr, $fixedline);
4977 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004978 }
4979 }
4980
Tom Rinia50f8de2014-02-27 08:27:28 -05004981#Specific variable tests
Kim Phillips9b7b8372013-02-28 12:53:52 +00004982 while ($line =~ m{($Constant|$Lval)}g) {
4983 my $var = $1;
Tom Rinia50f8de2014-02-27 08:27:28 -05004984
Simon Glassa33d9822020-05-22 16:32:35 -06004985#gcc binary extension
4986 if ($var =~ /^$Binary$/) {
4987 if (WARN("GCC_BINARY_CONSTANT",
4988 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4989 $fix) {
4990 my $hexval = sprintf("0x%x", oct($var));
4991 $fixed[$fixlinenr] =~
4992 s/\b$var\b/$hexval/;
4993 }
4994 }
4995
Tom Rinia50f8de2014-02-27 08:27:28 -05004996#CamelCase
4997 if ($var !~ /^$Constant$/ &&
4998 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
4999#Ignore Page<foo> variants
5000 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5001#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005002 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
5003#Ignore some three character SI units explicitly, like MiB and KHz
5004 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05005005 while ($var =~ m{($Ident)}g) {
5006 my $word = $1;
5007 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5008 if ($check) {
5009 seed_camelcase_includes();
5010 if (!$file && !$camelcase_file_seeded) {
5011 seed_camelcase_file($realfile);
5012 $camelcase_file_seeded = 1;
5013 }
5014 }
5015 if (!defined $camelcase{$word}) {
5016 $camelcase{$word} = 1;
5017 CHK("CAMELCASE",
5018 "Avoid CamelCase: <$word>\n" . $herecurr);
5019 }
5020 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00005021 }
5022 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005023
5024#no spaces allowed after \ in define
Tom Rinia50f8de2014-02-27 08:27:28 -05005025 if ($line =~ /\#\s*define.*\\\s+$/) {
5026 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5027 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5028 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005029 $fixed[$fixlinenr] =~ s/\s+$//;
Tom Rinia50f8de2014-02-27 08:27:28 -05005030 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005031 }
5032
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005033# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5034# itself <asm/foo.h> (uses RAW line)
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005035 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5036 my $file = "$1.h";
5037 my $checkfile = "include/linux/$file";
5038 if (-f "$root/$checkfile" &&
5039 $realfile ne $checkfile &&
5040 $1 !~ /$allowed_asm_includes/)
5041 {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005042 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5043 if ($asminclude > 0) {
5044 if ($realfile =~ m{^arch/}) {
5045 CHK("ARCH_INCLUDE_LINUX",
5046 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5047 } else {
5048 WARN("INCLUDE_LINUX",
5049 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5050 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005051 }
5052 }
5053 }
5054
5055# multi-statement macros should be enclosed in a do while loop, grab the
5056# first statement and ensure its the whole macro if its not enclosed
5057# in a known good container
5058 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5059 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5060 my $ln = $linenr;
5061 my $cnt = $realcnt;
5062 my ($off, $dstat, $dcond, $rest);
5063 my $ctx = '';
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005064 my $has_flow_statement = 0;
5065 my $has_arg_concat = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005066 ($dstat, $dcond, $ln, $cnt, $off) =
Kim Phillips9b7b8372013-02-28 12:53:52 +00005067 ctx_statement_block($linenr, $realcnt, 0);
5068 $ctx = $dstat;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005069 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5070 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5071
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005072 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
5073 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
5074
5075 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5076 my $define_args = $1;
5077 my $define_stmt = $dstat;
5078 my @def_args = ();
5079
5080 if (defined $define_args && $define_args ne "") {
5081 $define_args = substr($define_args, 1, length($define_args) - 2);
5082 $define_args =~ s/\s*//g;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005083 $define_args =~ s/\\\+?//g;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005084 @def_args = split(",", $define_args);
5085 }
5086
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005087 $dstat =~ s/$;//g;
5088 $dstat =~ s/\\\n.//g;
5089 $dstat =~ s/^\s*//s;
5090 $dstat =~ s/\s*$//s;
5091
5092 # Flatten any parentheses and braces
5093 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5094 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005095 $dstat =~ s/.\[[^\[\]]*\]/1/)
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005096 {
5097 }
5098
Kim Phillips9b7b8372013-02-28 12:53:52 +00005099 # Flatten any obvious string concatentation.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005100 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5101 $dstat =~ s/$Ident\s*($String)/$1/)
Kim Phillips9b7b8372013-02-28 12:53:52 +00005102 {
5103 }
5104
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005105 # Make asm volatile uses seem like a generic function
5106 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5107
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005108 my $exceptions = qr{
5109 $Declare|
5110 module_param_named|
Kim Phillips9b7b8372013-02-28 12:53:52 +00005111 MODULE_PARM_DESC|
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005112 DECLARE_PER_CPU|
5113 DEFINE_PER_CPU|
5114 __typeof__\(|
5115 union|
5116 struct|
5117 \.$Ident\s*=\s*|
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005118 ^\"|\"$|
5119 ^\[
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005120 }x;
5121 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005122
5123 $ctx =~ s/\n*$//;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005124 my $stmt_cnt = statement_rawlines($ctx);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005125 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005126
Kim Phillips9b7b8372013-02-28 12:53:52 +00005127 if ($dstat ne '' &&
5128 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5129 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Tom Rinia50f8de2014-02-27 08:27:28 -05005130 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005131 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Kim Phillips9b7b8372013-02-28 12:53:52 +00005132 $dstat !~ /$exceptions/ &&
5133 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Tom Rinia50f8de2014-02-27 08:27:28 -05005134 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Kim Phillips9b7b8372013-02-28 12:53:52 +00005135 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
5136 $dstat !~ /^for\s*$Constant$/ && # for (...)
5137 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5138 $dstat !~ /^do\s*{/ && # do {...
Heiko Schochere65be982016-01-04 09:17:19 +01005139 $dstat !~ /^\(\{/ && # ({...
Tom Rinia50f8de2014-02-27 08:27:28 -05005140 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Kim Phillips9b7b8372013-02-28 12:53:52 +00005141 {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005142 if ($dstat =~ /^\s*if\b/) {
5143 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5144 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5145 } elsif ($dstat =~ /;/) {
5146 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5147 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5148 } else {
5149 ERROR("COMPLEX_MACRO",
5150 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5151 }
5152
5153 }
5154
5155 # Make $define_stmt single line, comment-free, etc
5156 my @stmt_array = split('\n', $define_stmt);
5157 my $first = 1;
5158 $define_stmt = "";
5159 foreach my $l (@stmt_array) {
5160 $l =~ s/\\$//;
5161 if ($first) {
5162 $define_stmt = $l;
5163 $first = 0;
5164 } elsif ($l =~ /^[\+ ]/) {
5165 $define_stmt .= substr($l, 1);
5166 }
5167 }
5168 $define_stmt =~ s/$;//g;
5169 $define_stmt =~ s/\s+/ /g;
5170 $define_stmt = trim($define_stmt);
5171
5172# check if any macro arguments are reused (ignore '...' and 'type')
5173 foreach my $arg (@def_args) {
5174 next if ($arg =~ /\.\.\./);
5175 next if ($arg =~ /^type$/i);
5176 my $tmp_stmt = $define_stmt;
Simon Glassa33d9822020-05-22 16:32:35 -06005177 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005178 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5179 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005180 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005181 if ($use_cnt > 1) {
5182 CHK("MACRO_ARG_REUSE",
5183 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5184 }
5185# check if any macro arguments may have other precedence issues
5186 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5187 ((defined($1) && $1 ne ',') ||
5188 (defined($2) && $2 ne ','))) {
5189 CHK("MACRO_ARG_PRECEDENCE",
5190 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
5191 }
5192 }
5193
5194# check for macros with flow control, but without ## concatenation
5195# ## concatenation is commonly a macro that defines a function so ignore those
5196 if ($has_flow_statement && !$has_arg_concat) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00005197 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005198 my $herectx = get_stat_here($linenr, $cnt, $here);
Kim Phillips9b7b8372013-02-28 12:53:52 +00005199
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005200 WARN("MACRO_WITH_FLOW_CONTROL",
5201 "Macros with flow control statements should be avoided\n" . "$herectx");
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005202 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00005203
5204# check for line continuations outside of #defines, preprocessor #, and asm
5205
5206 } else {
5207 if ($prevline !~ /^..*\\$/ &&
5208 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5209 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
5210 $line =~ /^\+.*\\$/) {
5211 WARN("LINE_CONTINUATIONS",
5212 "Avoid unnecessary line continuations\n" . $herecurr);
5213 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005214 }
5215
Kim Phillips9b7b8372013-02-28 12:53:52 +00005216# do {} while (0) macro tests:
5217# single-statement macros do not need to be enclosed in do while (0) loop,
5218# macro should not end with a semicolon
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005219 if ($perl_version_ok &&
Kim Phillips9b7b8372013-02-28 12:53:52 +00005220 $realfile !~ m@/vmlinux.lds.h$@ &&
5221 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5222 my $ln = $linenr;
5223 my $cnt = $realcnt;
5224 my ($off, $dstat, $dcond, $rest);
5225 my $ctx = '';
5226 ($dstat, $dcond, $ln, $cnt, $off) =
5227 ctx_statement_block($linenr, $realcnt, 0);
5228 $ctx = $dstat;
5229
5230 $dstat =~ s/\\\n.//g;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005231 $dstat =~ s/$;/ /g;
Kim Phillips9b7b8372013-02-28 12:53:52 +00005232
5233 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5234 my $stmts = $2;
5235 my $semis = $3;
5236
5237 $ctx =~ s/\n*$//;
5238 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005239 my $herectx = get_stat_here($linenr, $cnt, $here);
Kim Phillips9b7b8372013-02-28 12:53:52 +00005240
5241 if (($stmts =~ tr/;/;/) == 1 &&
5242 $stmts !~ /^\s*(if|while|for|switch)\b/) {
5243 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5244 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5245 }
5246 if (defined $semis && $semis ne "") {
5247 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5248 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5249 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005250 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5251 $ctx =~ s/\n*$//;
5252 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005253 my $herectx = get_stat_here($linenr, $cnt, $here);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005254
5255 WARN("TRAILING_SEMICOLON",
5256 "macros should not use a trailing semicolon\n" . "$herectx");
Kim Phillips9b7b8372013-02-28 12:53:52 +00005257 }
5258 }
5259
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005260# check for redundant bracing round if etc
5261 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5262 my ($level, $endln, @chunks) =
5263 ctx_statement_full($linenr, $realcnt, 1);
5264 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5265 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5266 if ($#chunks > 0 && $level == 0) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00005267 my @allowed = ();
5268 my $allow = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005269 my $seen = 0;
5270 my $herectx = $here . "\n";
5271 my $ln = $linenr - 1;
5272 for my $chunk (@chunks) {
5273 my ($cond, $block) = @{$chunk};
5274
5275 # If the condition carries leading newlines, then count those as offsets.
5276 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5277 my $offset = statement_rawlines($whitespace) - 1;
5278
Kim Phillips9b7b8372013-02-28 12:53:52 +00005279 $allowed[$allow] = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005280 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5281
5282 # We have looked at and allowed this specific line.
5283 $suppress_ifbraces{$ln + $offset} = 1;
5284
5285 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5286 $ln += statement_rawlines($block) - 1;
5287
5288 substr($block, 0, length($cond), '');
5289
5290 $seen++ if ($block =~ /^\s*{/);
5291
Kim Phillips9b7b8372013-02-28 12:53:52 +00005292 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005293 if (statement_lines($cond) > 1) {
5294 #print "APW: ALLOWED: cond<$cond>\n";
Kim Phillips9b7b8372013-02-28 12:53:52 +00005295 $allowed[$allow] = 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005296 }
5297 if ($block =~/\b(?:if|for|while)\b/) {
5298 #print "APW: ALLOWED: block<$block>\n";
Kim Phillips9b7b8372013-02-28 12:53:52 +00005299 $allowed[$allow] = 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005300 }
5301 if (statement_block_size($block) > 1) {
5302 #print "APW: ALLOWED: lines block<$block>\n";
Kim Phillips9b7b8372013-02-28 12:53:52 +00005303 $allowed[$allow] = 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005304 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00005305 $allow++;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005306 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00005307 if ($seen) {
5308 my $sum_allowed = 0;
5309 foreach (@allowed) {
5310 $sum_allowed += $_;
5311 }
5312 if ($sum_allowed == 0) {
5313 WARN("BRACES",
5314 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5315 } elsif ($sum_allowed != $allow &&
5316 $seen != $allow) {
5317 CHK("BRACES",
5318 "braces {} should be used on all arms of this statement\n" . $herectx);
5319 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005320 }
5321 }
5322 }
5323 if (!defined $suppress_ifbraces{$linenr - 1} &&
5324 $line =~ /\b(if|while|for|else)\b/) {
5325 my $allowed = 0;
5326
5327 # Check the pre-context.
5328 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5329 #print "APW: ALLOWED: pre<$1>\n";
5330 $allowed = 1;
5331 }
5332
5333 my ($level, $endln, @chunks) =
5334 ctx_statement_full($linenr, $realcnt, $-[0]);
5335
5336 # Check the condition.
5337 my ($cond, $block) = @{$chunks[0]};
5338 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5339 if (defined $cond) {
5340 substr($block, 0, length($cond), '');
5341 }
5342 if (statement_lines($cond) > 1) {
5343 #print "APW: ALLOWED: cond<$cond>\n";
5344 $allowed = 1;
5345 }
5346 if ($block =~/\b(?:if|for|while)\b/) {
5347 #print "APW: ALLOWED: block<$block>\n";
5348 $allowed = 1;
5349 }
5350 if (statement_block_size($block) > 1) {
5351 #print "APW: ALLOWED: lines block<$block>\n";
5352 $allowed = 1;
5353 }
5354 # Check the post-context.
5355 if (defined $chunks[1]) {
5356 my ($cond, $block) = @{$chunks[1]};
5357 if (defined $cond) {
5358 substr($block, 0, length($cond), '');
5359 }
5360 if ($block =~ /^\s*\{/) {
5361 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5362 $allowed = 1;
5363 }
5364 }
5365 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005366 my $cnt = statement_rawlines($block);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005367 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005368
5369 WARN("BRACES",
5370 "braces {} are not necessary for single statement blocks\n" . $herectx);
5371 }
5372 }
5373
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005374# check for single line unbalanced braces
5375 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5376 $sline =~ /^.\s*else\s*\{\s*$/) {
5377 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5378 }
5379
Kim Phillips9b7b8372013-02-28 12:53:52 +00005380# check for unnecessary blank lines around braces
Tom Rinia50f8de2014-02-27 08:27:28 -05005381 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005382 if (CHK("BRACES",
5383 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5384 $fix && $prevrawline =~ /^\+/) {
5385 fix_delete_line($fixlinenr - 1, $prevrawline);
5386 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005387 }
Tom Rinia50f8de2014-02-27 08:27:28 -05005388 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005389 if (CHK("BRACES",
5390 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5391 $fix) {
5392 fix_delete_line($fixlinenr, $rawline);
5393 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005394 }
5395
5396# no volatiles please
5397 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5398 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5399 WARN("VOLATILE",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005400 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
5401 }
5402
5403# Check for user-visible strings broken across lines, which breaks the ability
5404# to grep for the string. Make exceptions when the previous string ends in a
5405# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5406# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
5407 if ($line =~ /^\+\s*$String/ &&
5408 $prevline =~ /"\s*$/ &&
5409 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5410 if (WARN("SPLIT_STRING",
5411 "quoted string split across lines\n" . $hereprev) &&
5412 $fix &&
5413 $prevrawline =~ /^\+.*"\s*$/ &&
5414 $last_coalesced_string_linenr != $linenr - 1) {
5415 my $extracted_string = get_quoted_string($line, $rawline);
5416 my $comma_close = "";
5417 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5418 $comma_close = $1;
5419 }
5420
5421 fix_delete_line($fixlinenr - 1, $prevrawline);
5422 fix_delete_line($fixlinenr, $rawline);
5423 my $fixedline = $prevrawline;
5424 $fixedline =~ s/"\s*$//;
5425 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5426 fix_insert_line($fixlinenr - 1, $fixedline);
5427 $fixedline = $rawline;
5428 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5429 if ($fixedline !~ /\+\s*$/) {
5430 fix_insert_line($fixlinenr, $fixedline);
5431 }
5432 $last_coalesced_string_linenr = $linenr;
5433 }
5434 }
5435
5436# check for missing a space in a string concatenation
5437 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5438 WARN('MISSING_SPACE',
5439 "break quoted strings at a space character\n" . $hereprev);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005440 }
5441
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005442# check for an embedded function name in a string when the function is known
5443# This does not work very well for -f --file checking as it depends on patch
5444# context providing the function name or a single line form for in-file
5445# function declarations
5446 if ($line =~ /^\+.*$String/ &&
5447 defined($context_function) &&
5448 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5449 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
5450 WARN("EMBEDDED_FUNCTION_NAME",
5451 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
5452 }
5453
5454# check for spaces before a quoted newline
5455 if ($rawline =~ /^.*\".*\s\\n/) {
5456 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5457 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5458 $fix) {
5459 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5460 }
5461
5462 }
5463
5464# concatenated string without spaces between elements
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005465 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5466 if (CHK("CONCATENATED_STRING",
5467 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5468 $fix) {
5469 while ($line =~ /($String)/g) {
5470 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5471 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5472 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5473 }
5474 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005475 }
5476
5477# uncoalesced string fragments
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005478 if ($line =~ /$String\s*"/) {
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005479 if (WARN("STRING_FRAGMENTS",
5480 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5481 $fix) {
5482 while ($line =~ /($String)(?=\s*")/g) {
5483 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5484 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5485 }
5486 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005487 }
5488
5489# check for non-standard and hex prefixed decimal printf formats
5490 my $show_L = 1; #don't show the same defect twice
5491 my $show_Z = 1;
5492 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5493 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
5494 $string =~ s/%%/__/g;
5495 # check for %L
5496 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
5497 WARN("PRINTF_L",
5498 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5499 $show_L = 0;
5500 }
5501 # check for %Z
5502 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5503 WARN("PRINTF_Z",
5504 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5505 $show_Z = 0;
5506 }
5507 # check for 0x<decimal>
5508 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5509 ERROR("PRINTF_0XDECIMAL",
5510 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5511 }
5512 }
5513
5514# check for line continuations in quoted strings with odd counts of "
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005515 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005516 WARN("LINE_CONTINUATIONS",
5517 "Avoid line continuations in quoted strings\n" . $herecurr);
5518 }
5519
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005520# warn about #if 0
5521 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005522 WARN("IF_0",
5523 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5524 }
5525
5526# warn about #if 1
5527 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5528 WARN("IF_1",
5529 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005530 }
5531
Kim Phillips9b7b8372013-02-28 12:53:52 +00005532# check for needless "if (<foo>) fn(<foo>)" uses
5533 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005534 my $tested = quotemeta($1);
5535 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5536 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5537 my $func = $1;
5538 if (WARN('NEEDLESS_IF',
5539 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5540 $fix) {
5541 my $do_fix = 1;
5542 my $leading_tabs = "";
5543 my $new_leading_tabs = "";
5544 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5545 $leading_tabs = $1;
5546 } else {
5547 $do_fix = 0;
5548 }
5549 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5550 $new_leading_tabs = $1;
5551 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5552 $do_fix = 0;
5553 }
5554 } else {
5555 $do_fix = 0;
5556 }
5557 if ($do_fix) {
5558 fix_delete_line($fixlinenr - 1, $prevrawline);
5559 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5560 }
5561 }
5562 }
5563 }
5564
5565# check for unnecessary "Out of Memory" messages
5566 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5567 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5568 (defined $1 || defined $3) &&
5569 $linenr > 3) {
5570 my $testval = $2;
5571 my $testline = $lines[$linenr - 3];
5572
5573 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5574# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5575
Simon Glassa33d9822020-05-22 16:32:35 -06005576 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)/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005577 WARN("OOM_MESSAGE",
5578 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5579 }
5580 }
5581
5582# check for logging functions with KERN_<LEVEL>
5583 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5584 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5585 my $level = $1;
5586 if (WARN("UNNECESSARY_KERN_LEVEL",
5587 "Possible unnecessary $level\n" . $herecurr) &&
5588 $fix) {
5589 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5590 }
5591 }
5592
5593# check for logging continuations
5594 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5595 WARN("LOGGING_CONTINUATION",
5596 "Avoid logging continuation uses where feasible\n" . $herecurr);
5597 }
5598
5599# check for mask then right shift without a parentheses
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005600 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005601 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5602 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5603 WARN("MASK_THEN_SHIFT",
5604 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5605 }
5606
5607# check for pointer comparisons to NULL
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005608 if ($perl_version_ok) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005609 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5610 my $val = $1;
5611 my $equal = "!";
5612 $equal = "" if ($4 eq "!=");
5613 if (CHK("COMPARISON_TO_NULL",
5614 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5615 $fix) {
5616 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5617 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005618 }
5619 }
5620
Tom Rinia50f8de2014-02-27 08:27:28 -05005621# check for bad placement of section $InitAttribute (e.g.: __initdata)
5622 if ($line =~ /(\b$InitAttribute\b)/) {
5623 my $attr = $1;
5624 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5625 my $ptr = $1;
5626 my $var = $2;
5627 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5628 ERROR("MISPLACED_INIT",
5629 "$attr should be placed after $var\n" . $herecurr)) ||
5630 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5631 WARN("MISPLACED_INIT",
5632 "$attr should be placed after $var\n" . $herecurr))) &&
5633 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005634 $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;
Tom Rinia50f8de2014-02-27 08:27:28 -05005635 }
5636 }
5637 }
5638
5639# check for $InitAttributeData (ie: __initdata) with const
5640 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5641 my $attr = $1;
5642 $attr =~ /($InitAttributePrefix)(.*)/;
5643 my $attr_prefix = $1;
5644 my $attr_type = $2;
5645 if (ERROR("INIT_ATTRIBUTE",
5646 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5647 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005648 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05005649 s/$InitAttributeData/${attr_prefix}initconst/;
5650 }
5651 }
5652
5653# check for $InitAttributeConst (ie: __initconst) without const
5654 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5655 my $attr = $1;
5656 if (ERROR("INIT_ATTRIBUTE",
5657 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5658 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005659 my $lead = $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05005660 /(^\+\s*(?:static\s+))/;
5661 $lead = rtrim($1);
5662 $lead = "$lead " if ($lead !~ /^\+$/);
5663 $lead = "${lead}const ";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005664 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5665 }
5666 }
5667
5668# check for __read_mostly with const non-pointer (should just be const)
5669 if ($line =~ /\b__read_mostly\b/ &&
5670 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5671 if (ERROR("CONST_READ_MOSTLY",
5672 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5673 $fix) {
5674 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5675 }
5676 }
5677
5678# don't use __constant_<foo> functions outside of include/uapi/
5679 if ($realfile !~ m@^include/uapi/@ &&
5680 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5681 my $constant_func = $1;
5682 my $func = $constant_func;
5683 $func =~ s/^__constant_//;
5684 if (WARN("CONSTANT_CONVERSION",
5685 "$constant_func should be $func\n" . $herecurr) &&
5686 $fix) {
5687 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Tom Rinia50f8de2014-02-27 08:27:28 -05005688 }
5689 }
5690
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005691# prefer usleep_range over udelay
Kim Phillips9b7b8372013-02-28 12:53:52 +00005692 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005693 my $delay = $1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005694 # ignore udelay's < 10, however
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005695 if (! ($delay < 10) ) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005696 CHK("USLEEP_RANGE",
Simon Glassa33d9822020-05-22 16:32:35 -06005697 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005698 }
5699 if ($delay > 2000) {
5700 WARN("LONG_UDELAY",
5701 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005702 }
5703 }
5704
5705# warn about unexpectedly long msleep's
5706 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5707 if ($1 < 20) {
5708 WARN("MSLEEP",
Simon Glassa33d9822020-05-22 16:32:35 -06005709 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005710 }
5711 }
5712
Tom Rinia50f8de2014-02-27 08:27:28 -05005713# check for comparisons of jiffies
5714 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5715 WARN("JIFFIES_COMPARISON",
5716 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5717 }
5718
5719# check for comparisons of get_jiffies_64()
5720 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5721 WARN("JIFFIES_COMPARISON",
5722 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5723 }
5724
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005725# warn about #ifdefs in C files
5726# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
5727# print "#ifdef in C files should be avoided\n";
5728# print "$herecurr";
5729# $clean = 0;
5730# }
5731
5732# warn about spacing in #ifdefs
5733 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05005734 if (ERROR("SPACING",
5735 "exactly one space required after that #$1\n" . $herecurr) &&
5736 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005737 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05005738 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5739 }
5740
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005741 }
5742
5743# check for spinlock_t definitions without a comment.
5744 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5745 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
5746 my $which = $1;
5747 if (!ctx_has_comment($first_line, $linenr)) {
5748 CHK("UNCOMMENTED_DEFINITION",
5749 "$1 definition without comment\n" . $herecurr);
5750 }
5751 }
5752# check for memory barriers without a comment.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005753
5754 my $barriers = qr{
5755 mb|
5756 rmb|
5757 wmb|
5758 read_barrier_depends
5759 }x;
5760 my $barrier_stems = qr{
5761 mb__before_atomic|
5762 mb__after_atomic|
5763 store_release|
5764 load_acquire|
5765 store_mb|
5766 (?:$barriers)
5767 }x;
5768 my $all_barriers = qr{
5769 (?:$barriers)|
5770 smp_(?:$barrier_stems)|
5771 virt_(?:$barrier_stems)
5772 }x;
5773
5774 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005775 if (!ctx_has_comment($first_line, $linenr)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05005776 WARN("MEMORY_BARRIER",
5777 "memory barrier without comment\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005778 }
5779 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005780
5781 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5782
5783 if ($realfile !~ m@^include/asm-generic/@ &&
5784 $realfile !~ m@/barrier\.h$@ &&
5785 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5786 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5787 WARN("MEMORY_BARRIER",
5788 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5789 }
5790
5791# check for waitqueue_active without a comment.
5792 if ($line =~ /\bwaitqueue_active\s*\(/) {
5793 if (!ctx_has_comment($first_line, $linenr)) {
5794 WARN("WAITQUEUE_ACTIVE",
5795 "waitqueue_active without comment\n" . $herecurr);
5796 }
5797 }
5798
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005799# check for smp_read_barrier_depends and read_barrier_depends
5800 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5801 WARN("READ_BARRIER_DEPENDS",
5802 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5803 }
5804
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005805# check of hardware specific defines
5806 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5807 CHK("ARCH_DEFINES",
5808 "architecture specific defines should be avoided\n" . $herecurr);
5809 }
5810
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005811# check that the storage class is not after a type
5812 if ($line =~ /\b($Type)\s+($Storage)\b/) {
5813 WARN("STORAGE_CLASS",
5814 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5815 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005816# Check that the storage class is at the beginning of a declaration
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005817 if ($line =~ /\b$Storage\b/ &&
5818 $line !~ /^.\s*$Storage/ &&
5819 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5820 $1 !~ /[\,\)]\s*$/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005821 WARN("STORAGE_CLASS",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005822 "storage class should be at the beginning of the declaration\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005823 }
5824
5825# check the location of the inline attribute, that it is between
5826# storage class and type.
5827 if ($line =~ /\b$Type\s+$Inline\b/ ||
5828 $line =~ /\b$Inline\s+$Storage\b/) {
5829 ERROR("INLINE_LOCATION",
5830 "inline keyword should sit between storage class and type\n" . $herecurr);
5831 }
5832
5833# Check for __inline__ and __inline, prefer inline
Tom Rinia50f8de2014-02-27 08:27:28 -05005834 if ($realfile !~ m@\binclude/uapi/@ &&
5835 $line =~ /\b(__inline__|__inline)\b/) {
5836 if (WARN("INLINE",
5837 "plain inline is preferred over $1\n" . $herecurr) &&
5838 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005839 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Tom Rinia50f8de2014-02-27 08:27:28 -05005840
5841 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005842 }
5843
5844# Check for __attribute__ packed, prefer __packed
Tom Rinia50f8de2014-02-27 08:27:28 -05005845 if ($realfile !~ m@\binclude/uapi/@ &&
5846 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005847 WARN("PREFER_PACKED",
5848 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
5849 }
5850
5851# Check for __attribute__ aligned, prefer __aligned
Tom Rinia50f8de2014-02-27 08:27:28 -05005852 if ($realfile !~ m@\binclude/uapi/@ &&
5853 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005854 WARN("PREFER_ALIGNED",
5855 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
5856 }
5857
Kim Phillips9b7b8372013-02-28 12:53:52 +00005858# Check for __attribute__ format(printf, prefer __printf
Tom Rinia50f8de2014-02-27 08:27:28 -05005859 if ($realfile !~ m@\binclude/uapi/@ &&
5860 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5861 if (WARN("PREFER_PRINTF",
5862 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5863 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005864 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Tom Rinia50f8de2014-02-27 08:27:28 -05005865
5866 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00005867 }
5868
5869# Check for __attribute__ format(scanf, prefer __scanf
Tom Rinia50f8de2014-02-27 08:27:28 -05005870 if ($realfile !~ m@\binclude/uapi/@ &&
5871 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5872 if (WARN("PREFER_SCANF",
5873 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5874 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005875 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5876 }
5877 }
5878
5879# Check for __attribute__ weak, or __weak declarations (may have link issues)
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005880 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005881 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5882 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5883 $line =~ /\b__weak\b/)) {
5884 ERROR("WEAK_DECLARATION",
5885 "Using weak declarations can have unintended link defects\n" . $herecurr);
5886 }
5887
5888# check for c99 types like uint8_t used outside of uapi/ and tools/
5889 if ($realfile !~ m@\binclude/uapi/@ &&
5890 $realfile !~ m@\btools/@ &&
5891 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5892 my $type = $1;
5893 if ($type =~ /\b($typeC99Typedefs)\b/) {
5894 $type = $1;
5895 my $kernel_type = 'u';
5896 $kernel_type = 's' if ($type =~ /^_*[si]/);
5897 $type =~ /(\d+)/;
5898 $kernel_type .= $1;
5899 if (CHK("PREFER_KERNEL_TYPES",
5900 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5901 $fix) {
5902 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5903 }
5904 }
5905 }
5906
5907# check for cast of C90 native int or longer types constants
5908 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5909 my $cast = $1;
5910 my $const = $2;
5911 if (WARN("TYPECAST_INT_CONSTANT",
5912 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5913 $fix) {
5914 my $suffix = "";
5915 my $newconst = $const;
5916 $newconst =~ s/${Int_type}$//;
5917 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5918 if ($cast =~ /\blong\s+long\b/) {
5919 $suffix .= 'LL';
5920 } elsif ($cast =~ /\blong\b/) {
5921 $suffix .= 'L';
5922 }
5923 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
Tom Rinia50f8de2014-02-27 08:27:28 -05005924 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00005925 }
5926
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005927# check for sizeof(&)
5928 if ($line =~ /\bsizeof\s*\(\s*\&/) {
5929 WARN("SIZEOF_ADDRESS",
5930 "sizeof(& should be avoided\n" . $herecurr);
5931 }
5932
Kim Phillips9b7b8372013-02-28 12:53:52 +00005933# check for sizeof without parenthesis
5934 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05005935 if (WARN("SIZEOF_PARENTHESIS",
5936 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5937 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005938 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Tom Rinia50f8de2014-02-27 08:27:28 -05005939 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00005940 }
5941
Kim Phillips9b7b8372013-02-28 12:53:52 +00005942# check for struct spinlock declarations
5943 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5944 WARN("USE_SPINLOCK_T",
5945 "struct spinlock should be spinlock_t\n" . $herecurr);
5946 }
5947
Tom Rinia50f8de2014-02-27 08:27:28 -05005948# check for seq_printf uses that could be seq_puts
5949 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5950 my $fmt = get_quoted_string($line, $rawline);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005951 $fmt =~ s/%%//g;
5952 if ($fmt !~ /%/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05005953 if (WARN("PREFER_SEQ_PUTS",
5954 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5955 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005956 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5957 }
5958 }
5959 }
5960
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005961# check for vsprintf extension %p<foo> misuses
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005962 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005963 defined $stat &&
5964 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5965 $1 !~ /^_*volatile_*$/) {
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005966 my $stat_real;
5967
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005968 my $lc = $stat =~ tr@\n@@;
5969 $lc = $lc + $linenr;
5970 for (my $count = $linenr; $count <= $lc; $count++) {
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005971 my $specifier;
5972 my $extension;
5973 my $bad_specifier = "";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005974 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5975 $fmt =~ s/%%//g;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005976
5977 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5978 $specifier = $1;
5979 $extension = $2;
Simon Glassa33d9822020-05-22 16:32:35 -06005980 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005981 $bad_specifier = $specifier;
5982 last;
5983 }
5984 if ($extension eq "x" && !defined($stat_real)) {
5985 if (!defined($stat_real)) {
5986 $stat_real = get_stat_real($linenr, $lc);
5987 }
5988 WARN("VSPRINTF_SPECIFIER_PX",
5989 "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");
5990 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005991 }
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005992 if ($bad_specifier ne "") {
5993 my $stat_real = get_stat_real($linenr, $lc);
5994 my $ext_type = "Invalid";
5995 my $use = "";
5996 if ($bad_specifier =~ /p[Ff]/) {
5997 $ext_type = "Deprecated";
5998 $use = " - use %pS instead";
5999 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6000 }
6001
6002 WARN("VSPRINTF_POINTER_EXTENSION",
6003 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
Tom Rinia50f8de2014-02-27 08:27:28 -05006004 }
6005 }
6006 }
6007
Kim Phillips9b7b8372013-02-28 12:53:52 +00006008# Check for misused memsets
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006009 if ($perl_version_ok &&
Kim Phillips9b7b8372013-02-28 12:53:52 +00006010 defined $stat &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006011 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00006012
6013 my $ms_addr = $2;
6014 my $ms_val = $7;
6015 my $ms_size = $12;
6016
6017 if ($ms_size =~ /^(0x|)0$/i) {
6018 ERROR("MEMSET",
6019 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6020 } elsif ($ms_size =~ /^(0x|)1$/i) {
6021 WARN("MEMSET",
6022 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6023 }
6024 }
6025
Tom Rinia50f8de2014-02-27 08:27:28 -05006026# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006027# if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006028# defined $stat &&
6029# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6030# if (WARN("PREFER_ETHER_ADDR_COPY",
6031# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6032# $fix) {
6033# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6034# }
6035# }
6036
6037# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006038# if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006039# defined $stat &&
6040# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6041# WARN("PREFER_ETHER_ADDR_EQUAL",
6042# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6043# }
6044
6045# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6046# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006047# if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006048# defined $stat &&
6049# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6050#
6051# my $ms_val = $7;
6052#
6053# if ($ms_val =~ /^(?:0x|)0+$/i) {
6054# if (WARN("PREFER_ETH_ZERO_ADDR",
6055# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6056# $fix) {
6057# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6058# }
6059# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6060# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6061# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6062# $fix) {
6063# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6064# }
6065# }
6066# }
Tom Rinia50f8de2014-02-27 08:27:28 -05006067
Kim Phillips9b7b8372013-02-28 12:53:52 +00006068# typecasts on min/max could be min_t/max_t
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006069 if ($perl_version_ok &&
Kim Phillips9b7b8372013-02-28 12:53:52 +00006070 defined $stat &&
6071 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6072 if (defined $2 || defined $7) {
6073 my $call = $1;
6074 my $cast1 = deparenthesize($2);
6075 my $arg1 = $3;
6076 my $cast2 = deparenthesize($7);
6077 my $arg2 = $8;
6078 my $cast;
6079
6080 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6081 $cast = "$cast1 or $cast2";
6082 } elsif ($cast1 ne "") {
6083 $cast = $cast1;
6084 } else {
6085 $cast = $cast2;
6086 }
6087 WARN("MINMAX",
6088 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6089 }
6090 }
6091
6092# check usleep_range arguments
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006093 if ($perl_version_ok &&
Kim Phillips9b7b8372013-02-28 12:53:52 +00006094 defined $stat &&
6095 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6096 my $min = $1;
6097 my $max = $7;
6098 if ($min eq $max) {
6099 WARN("USLEEP_RANGE",
Simon Glassa33d9822020-05-22 16:32:35 -06006100 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
Kim Phillips9b7b8372013-02-28 12:53:52 +00006101 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6102 $min > $max) {
6103 WARN("USLEEP_RANGE",
Simon Glassa33d9822020-05-22 16:32:35 -06006104 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
Kim Phillips9b7b8372013-02-28 12:53:52 +00006105 }
6106 }
6107
Tom Rinia50f8de2014-02-27 08:27:28 -05006108# check for naked sscanf
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006109 if ($perl_version_ok &&
Tom Rinia50f8de2014-02-27 08:27:28 -05006110 defined $stat &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006111 $line =~ /\bsscanf\b/ &&
Tom Rinia50f8de2014-02-27 08:27:28 -05006112 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6113 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6114 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6115 my $lc = $stat =~ tr@\n@@;
6116 $lc = $lc + $linenr;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006117 my $stat_real = get_stat_real($linenr, $lc);
Tom Rinia50f8de2014-02-27 08:27:28 -05006118 WARN("NAKED_SSCANF",
6119 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6120 }
6121
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006122# check for simple sscanf that should be kstrto<foo>
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006123 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006124 defined $stat &&
6125 $line =~ /\bsscanf\b/) {
6126 my $lc = $stat =~ tr@\n@@;
6127 $lc = $lc + $linenr;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006128 my $stat_real = get_stat_real($linenr, $lc);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006129 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6130 my $format = $6;
6131 my $count = $format =~ tr@%@%@;
6132 if ($count == 1 &&
6133 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6134 WARN("SSCANF_TO_KSTRTO",
6135 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6136 }
6137 }
6138 }
6139
Tom Rinia50f8de2014-02-27 08:27:28 -05006140# check for new externs in .h files.
6141 if ($realfile =~ /\.h$/ &&
6142 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6143 if (CHK("AVOID_EXTERNS",
6144 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
6145 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006146 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Tom Rinia50f8de2014-02-27 08:27:28 -05006147 }
6148 }
6149
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006150# check for new externs in .c files.
6151 if ($realfile =~ /\.c$/ && defined $stat &&
6152 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6153 {
6154 my $function_name = $1;
6155 my $paren_space = $2;
6156
6157 my $s = $stat;
6158 if (defined $cond) {
6159 substr($s, 0, length($cond), '');
6160 }
6161 if ($s =~ /^\s*;/ &&
6162 $function_name ne 'uninitialized_var')
6163 {
6164 WARN("AVOID_EXTERNS",
6165 "externs should be avoided in .c files\n" . $herecurr);
6166 }
6167
6168 if ($paren_space =~ /\n/) {
6169 WARN("FUNCTION_ARGUMENTS",
6170 "arguments for function declarations should follow identifier\n" . $herecurr);
6171 }
6172
6173 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6174 $stat =~ /^.\s*extern\s+/)
6175 {
6176 WARN("AVOID_EXTERNS",
6177 "externs should be avoided in .c files\n" . $herecurr);
6178 }
6179
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006180# check for function declarations that have arguments without identifier names
6181 if (defined $stat &&
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006182 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006183 $1 ne "void") {
6184 my $args = trim($1);
6185 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6186 my $arg = trim($1);
6187 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6188 WARN("FUNCTION_ARGUMENTS",
6189 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6190 }
6191 }
6192 }
6193
6194# check for function definitions
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006195 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006196 defined $stat &&
6197 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6198 $context_function = $1;
6199
6200# check for multiline function definition with misplaced open brace
6201 my $ok = 0;
6202 my $cnt = statement_rawlines($stat);
6203 my $herectx = $here . "\n";
6204 for (my $n = 0; $n < $cnt; $n++) {
6205 my $rl = raw_line($linenr, $n);
6206 $herectx .= $rl . "\n";
6207 $ok = 1 if ($rl =~ /^[ \+]\{/);
6208 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6209 last if $rl =~ /^[ \+].*\{/;
6210 }
6211 if (!$ok) {
6212 ERROR("OPEN_BRACE",
6213 "open brace '{' following function definitions go on the next line\n" . $herectx);
6214 }
6215 }
6216
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006217# checks for new __setup's
6218 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6219 my $name = $1;
6220
6221 if (!grep(/$name/, @setup_docs)) {
6222 CHK("UNDOCUMENTED_SETUP",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006223 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006224 }
6225 }
6226
Simon Glassa33d9822020-05-22 16:32:35 -06006227# check for pointless casting of kmalloc return
6228 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006229 WARN("UNNECESSARY_CASTS",
6230 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
6231 }
6232
Tom Rinia50f8de2014-02-27 08:27:28 -05006233# alloc style
6234# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006235 if ($perl_version_ok &&
Simon Glassa33d9822020-05-22 16:32:35 -06006236 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05006237 CHK("ALLOC_SIZEOF_STRUCT",
6238 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6239 }
6240
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006241# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006242 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006243 defined $stat &&
6244 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
6245 my $oldfunc = $3;
6246 my $a1 = $4;
6247 my $a2 = $10;
6248 my $newfunc = "kmalloc_array";
6249 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
6250 my $r1 = $a1;
6251 my $r2 = $a2;
6252 if ($a1 =~ /^sizeof\s*\S/) {
6253 $r1 = $a2;
6254 $r2 = $a1;
6255 }
6256 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6257 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006258 my $cnt = statement_rawlines($stat);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006259 my $herectx = get_stat_here($linenr, $cnt, $here);
6260
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006261 if (WARN("ALLOC_WITH_MULTIPLY",
6262 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6263 $cnt == 1 &&
6264 $fix) {
6265 $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;
6266 }
6267 }
6268 }
6269
Tom Rinia50f8de2014-02-27 08:27:28 -05006270# check for krealloc arg reuse
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006271 if ($perl_version_ok &&
6272 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6273 $1 eq $3) {
Tom Rinia50f8de2014-02-27 08:27:28 -05006274 WARN("KREALLOC_ARG_REUSE",
6275 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6276 }
6277
Kim Phillips9b7b8372013-02-28 12:53:52 +00006278# check for alloc argument mismatch
6279 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6280 WARN("ALLOC_ARRAY_ARGS",
6281 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6282 }
6283
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006284# check for multiple semicolons
6285 if ($line =~ /;\s*;\s*$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05006286 if (WARN("ONE_SEMICOLON",
6287 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6288 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006289 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6290 }
6291 }
6292
6293# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6294 if ($realfile !~ m@^include/uapi/@ &&
6295 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
6296 my $ull = "";
6297 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6298 if (CHK("BIT_MACRO",
6299 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6300 $fix) {
6301 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
Tom Rinia50f8de2014-02-27 08:27:28 -05006302 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006303 }
6304
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006305# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6306 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6307 my $config = $1;
6308 if (WARN("PREFER_IS_ENABLED",
6309 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6310 $fix) {
6311 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6312 }
6313 }
6314
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -04006315# check for case / default statements not preceded by break/fallthrough/switch
Tom Rinia50f8de2014-02-27 08:27:28 -05006316 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6317 my $has_break = 0;
6318 my $has_statement = 0;
6319 my $count = 0;
6320 my $prevline = $linenr;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006321 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Tom Rinia50f8de2014-02-27 08:27:28 -05006322 $prevline--;
6323 my $rline = $rawlines[$prevline - 1];
6324 my $fline = $lines[$prevline - 1];
6325 last if ($fline =~ /^\@\@/);
6326 next if ($fline =~ /^\-/);
6327 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6328 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6329 next if ($fline =~ /^.[\s$;]*$/);
6330 $has_statement = 1;
6331 $count++;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006332 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
Tom Rinia50f8de2014-02-27 08:27:28 -05006333 }
6334 if (!$has_break && $has_statement) {
6335 WARN("MISSING_BREAK",
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -04006336 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Tom Rinia50f8de2014-02-27 08:27:28 -05006337 }
6338 }
6339
Kim Phillips9b7b8372013-02-28 12:53:52 +00006340# check for switch/default statements without a break;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006341 if ($perl_version_ok &&
Kim Phillips9b7b8372013-02-28 12:53:52 +00006342 defined $stat &&
6343 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00006344 my $cnt = statement_rawlines($stat);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006345 my $herectx = get_stat_here($linenr, $cnt, $here);
6346
Kim Phillips9b7b8372013-02-28 12:53:52 +00006347 WARN("DEFAULT_NO_BREAK",
6348 "switch default: should use break\n" . $herectx);
Eric Nelson9c7c24f2012-05-02 20:32:18 -07006349 }
6350
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006351# check for gcc specific __FUNCTION__
Tom Rinia50f8de2014-02-27 08:27:28 -05006352 if ($line =~ /\b__FUNCTION__\b/) {
6353 if (WARN("USE_FUNC",
6354 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6355 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006356 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Tom Rinia50f8de2014-02-27 08:27:28 -05006357 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006358 }
6359
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006360# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6361 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6362 ERROR("DATE_TIME",
6363 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6364 }
6365
Kim Phillips9b7b8372013-02-28 12:53:52 +00006366# check for use of yield()
6367 if ($line =~ /\byield\s*\(\s*\)/) {
6368 WARN("YIELD",
6369 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6370 }
6371
Tom Rinia50f8de2014-02-27 08:27:28 -05006372# check for comparisons against true and false
6373 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6374 my $lead = $1;
6375 my $arg = $2;
6376 my $test = $3;
6377 my $otype = $4;
6378 my $trail = $5;
6379 my $op = "!";
6380
6381 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6382
6383 my $type = lc($otype);
6384 if ($type =~ /^(?:true|false)$/) {
6385 if (("$test" eq "==" && "$type" eq "true") ||
6386 ("$test" eq "!=" && "$type" eq "false")) {
6387 $op = "";
6388 }
6389
6390 CHK("BOOL_COMPARISON",
6391 "Using comparison to $otype is error prone\n" . $herecurr);
6392
6393## maybe suggesting a correct construct would better
6394## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6395
6396 }
6397 }
6398
Simon Glassa33d9822020-05-22 16:32:35 -06006399# check for bool bitfields
6400 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6401 WARN("BOOL_BITFIELD",
6402 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6403 }
6404
6405# check for bool use in .h files
6406 if ($realfile =~ /\.h$/ &&
6407 $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6408 CHK("BOOL_MEMBER",
6409 "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6410 }
6411
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006412# check for semaphores initialized locked
6413 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6414 WARN("CONSIDER_COMPLETION",
6415 "consider using a completion\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006416 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00006417
6418# recommend kstrto* over simple_strto* and strict_strto*
6419 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006420 WARN("CONSIDER_KSTRTO",
Kim Phillips9b7b8372013-02-28 12:53:52 +00006421 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006422 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00006423
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006424# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006425 if ($line =~ /^.\s*__initcall\s*\(/) {
6426 WARN("USE_DEVICE_INITCALL",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006427 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006428 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00006429
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006430# check for various structs that are normally const (ops, kgdb, device_tree)
6431# and avoid what seem like struct definitions 'struct foo {'
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006432 if ($line !~ /\bconst\b/ &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006433 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006434 WARN("CONST_STRUCT",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006435 "struct $1 should normally be const\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006436 }
6437
6438# use of NR_CPUS is usually wrong
6439# ignore definitions of NR_CPUS and usage to define arrays as likely right
6440 if ($line =~ /\bNR_CPUS\b/ &&
6441 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6442 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6443 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6444 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6445 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6446 {
6447 WARN("NR_CPUS",
6448 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6449 }
6450
Tom Rinia50f8de2014-02-27 08:27:28 -05006451# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6452 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6453 ERROR("DEFINE_ARCH_HAS",
6454 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6455 }
6456
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006457# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006458 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006459 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6460 WARN("LIKELY_MISUSE",
6461 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006462 }
6463
6464# whine mightly about in_atomic
6465 if ($line =~ /\bin_atomic\s*\(/) {
6466 if ($realfile =~ m@^drivers/@) {
6467 ERROR("IN_ATOMIC",
6468 "do not use in_atomic in drivers\n" . $herecurr);
6469 } elsif ($realfile !~ m@^kernel/@) {
6470 WARN("IN_ATOMIC",
6471 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6472 }
6473 }
6474
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006475# check for mutex_trylock_recursive usage
6476 if ($line =~ /mutex_trylock_recursive/) {
6477 ERROR("LOCKING",
6478 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6479 }
6480
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006481# check for lockdep_set_novalidate_class
6482 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6483 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6484 if ($realfile !~ m@^kernel/lockdep@ &&
6485 $realfile !~ m@^include/linux/lockdep@ &&
6486 $realfile !~ m@^drivers/base/core@) {
6487 ERROR("LOCKDEP",
6488 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
6489 }
6490 }
6491
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006492 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6493 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006494 WARN("EXPORTED_WORLD_WRITABLE",
6495 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
6496 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006497
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006498# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6499# and whether or not function naming is typical and if
6500# DEVICE_ATTR permissions uses are unusual too
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006501 if ($perl_version_ok &&
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006502 defined $stat &&
6503 $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*\)/) {
6504 my $var = $1;
6505 my $perms = $2;
6506 my $show = $3;
6507 my $store = $4;
6508 my $octal_perms = perms_to_octal($perms);
6509 if ($show =~ /^${var}_show$/ &&
6510 $store =~ /^${var}_store$/ &&
6511 $octal_perms eq "0644") {
6512 if (WARN("DEVICE_ATTR_RW",
6513 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6514 $fix) {
6515 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6516 }
6517 } elsif ($show =~ /^${var}_show$/ &&
6518 $store =~ /^NULL$/ &&
6519 $octal_perms eq "0444") {
6520 if (WARN("DEVICE_ATTR_RO",
6521 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6522 $fix) {
6523 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6524 }
6525 } elsif ($show =~ /^NULL$/ &&
6526 $store =~ /^${var}_store$/ &&
6527 $octal_perms eq "0200") {
6528 if (WARN("DEVICE_ATTR_WO",
6529 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6530 $fix) {
6531 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6532 }
6533 } elsif ($octal_perms eq "0644" ||
6534 $octal_perms eq "0444" ||
6535 $octal_perms eq "0200") {
6536 my $newshow = "$show";
6537 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6538 my $newstore = $store;
6539 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6540 my $rename = "";
6541 if ($show ne $newshow) {
6542 $rename .= " '$show' to '$newshow'";
6543 }
6544 if ($store ne $newstore) {
6545 $rename .= " '$store' to '$newstore'";
6546 }
6547 WARN("DEVICE_ATTR_FUNCTIONS",
6548 "Consider renaming function(s)$rename\n" . $herecurr);
6549 } else {
6550 WARN("DEVICE_ATTR_PERMS",
6551 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6552 }
6553 }
6554
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006555# Mode permission misuses where it seems decimal should be octal
6556# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006557# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6558# specific definition of not visible in sysfs.
6559# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6560# use the default permissions
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006561 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006562 defined $stat &&
6563 $line =~ /$mode_perms_search/) {
6564 foreach my $entry (@mode_permission_funcs) {
6565 my $func = $entry->[0];
6566 my $arg_pos = $entry->[1];
6567
6568 my $lc = $stat =~ tr@\n@@;
6569 $lc = $lc + $linenr;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006570 my $stat_real = get_stat_real($linenr, $lc);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006571
6572 my $skip_args = "";
6573 if ($arg_pos > 1) {
6574 $arg_pos--;
6575 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6576 }
6577 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6578 if ($stat =~ /$test/) {
6579 my $val = $1;
6580 $val = $6 if ($skip_args ne "");
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006581 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6582 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6583 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006584 ERROR("NON_OCTAL_PERMISSIONS",
6585 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6586 }
6587 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6588 ERROR("EXPORTED_WORLD_WRITABLE",
6589 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
6590 }
6591 }
6592 }
6593 }
6594
6595# check for uses of S_<PERMS> that could be octal for readability
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006596 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
6597 my $oval = $1;
6598 my $octal = perms_to_octal($oval);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006599 if (WARN("SYMBOLIC_PERMS",
6600 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6601 $fix) {
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006602 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006603 }
6604 }
6605
6606# validate content of MODULE_LICENSE against list from include/linux/module.h
6607 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6608 my $extracted_string = get_quoted_string($line, $rawline);
6609 my $valid_licenses = qr{
6610 GPL|
6611 GPL\ v2|
6612 GPL\ and\ additional\ rights|
6613 Dual\ BSD/GPL|
6614 Dual\ MIT/GPL|
6615 Dual\ MPL/GPL|
6616 Proprietary
6617 }x;
6618 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6619 WARN("MODULE_LICENSE",
6620 "unknown module license " . $extracted_string . "\n" . $herecurr);
6621 }
6622 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006623 }
6624
6625 # If we have no input at all, then there is nothing to report on
6626 # so just keep quiet.
6627 if ($#rawlines == -1) {
6628 exit(0);
6629 }
6630
6631 # In mailback mode only produce a report in the negative, for
6632 # things that appear to be patches.
6633 if ($mailback && ($clean == 1 || !$is_patch)) {
6634 exit(0);
6635 }
6636
6637 # This is not a patch, and we are are in 'no-patch' mode so
6638 # just keep quiet.
6639 if (!$chk_patch && !$is_patch) {
6640 exit(0);
6641 }
6642
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006643 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006644 ERROR("NOT_UNIFIED_DIFF",
6645 "Does not appear to be a unified-diff format patch\n");
6646 }
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006647 if ($is_patch && $has_commit_log && $chk_signoff) {
6648 if ($signoff == 0) {
6649 ERROR("MISSING_SIGN_OFF",
6650 "Missing Signed-off-by: line(s)\n");
6651 } elsif (!$authorsignoff) {
6652 WARN("NO_AUTHOR_SIGN_OFF",
6653 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6654 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006655 }
6656
6657 print report_dump();
6658 if ($summary && !($clean == 1 && $quiet == 1)) {
6659 print "$filename " if ($summary_file);
6660 print "total: $cnt_error errors, $cnt_warn warnings, " .
6661 (($check)? "$cnt_chk checks, " : "") .
6662 "$cnt_lines lines checked\n";
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006663 }
6664
6665 if ($quiet == 0) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006666 # If there were any defects found and not already fixing them
6667 if (!$clean and !$fix) {
6668 print << "EOM"
Kim Phillips9b7b8372013-02-28 12:53:52 +00006669
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006670NOTE: For some of the reported defects, checkpatch may be able to
6671 mechanically convert to the typical style using --fix or --fix-inplace.
6672EOM
Kim Phillips9b7b8372013-02-28 12:53:52 +00006673 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006674 # If there were whitespace errors which cleanpatch can fix
6675 # then suggest that.
6676 if ($rpt_cleaners) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006677 $rpt_cleaners = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006678 print << "EOM"
6679
6680NOTE: Whitespace errors detected.
6681 You may wish to use scripts/cleanpatch or scripts/cleanfile
6682EOM
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006683 }
6684 }
6685
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006686 if ($clean == 0 && $fix &&
6687 ("@rawlines" ne "@fixed" ||
6688 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05006689 my $newfile = $filename;
6690 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
6691 my $linecount = 0;
6692 my $f;
6693
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006694 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6695
Tom Rinia50f8de2014-02-27 08:27:28 -05006696 open($f, '>', $newfile)
6697 or die "$P: Can't open $newfile for write\n";
6698 foreach my $fixed_line (@fixed) {
6699 $linecount++;
6700 if ($file) {
6701 if ($linecount > 3) {
6702 $fixed_line =~ s/^\+//;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006703 print $f $fixed_line . "\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05006704 }
6705 } else {
6706 print $f $fixed_line . "\n";
6707 }
6708 }
6709 close($f);
6710
6711 if (!$quiet) {
6712 print << "EOM";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006713
Tom Rinia50f8de2014-02-27 08:27:28 -05006714Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6715
6716Do _NOT_ trust the results written to this file.
6717Do _NOT_ submit these changes without inspecting them for correctness.
6718
6719This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6720No warranties, expressed or implied...
Tom Rinia50f8de2014-02-27 08:27:28 -05006721EOM
6722 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006723 }
6724
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006725 if ($quiet == 0) {
6726 print "\n";
6727 if ($clean == 1) {
6728 print "$vname has no obvious style problems and is ready for submission.\n";
6729 } else {
6730 print "$vname has style problems, please review.\n";
6731 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006732 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006733 return $clean;
6734}