blob: e88461fc7cb68c9de158e57aa1a541e115e69c61 [file] [log] [blame]
Martin Roth387dec82017-09-17 19:20:46 -06001#!/usr/bin/env perl
Martin Roth60915b32018-08-10 21:04:05 -06002# SPDX-License-Identifier: GPL-2.0
3#
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004# (c) 2001, Dave Jones. (the file handling bit)
5# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
6# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
7# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Martin Roth60915b32018-08-10 21:04:05 -06008# (c) 2010-2018 Joe Perches <joe@perches.com>
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01009
10use strict;
Martin Roth387dec82017-09-17 19:20:46 -060011use warnings;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +010012use POSIX;
13use File::Basename;
14use Cwd 'abs_path';
Stefan Reinauerc6080c62016-07-29 16:01:40 -070015use Term::ANSIColor qw(:constants);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +010016
17my $P = $0;
18my $D = dirname(abs_path($P));
19
20my $V = '0.32';
21
22use Getopt::Long qw(:config no_auto_abbrev);
23
24my $quiet = 0;
Elyes Haouas84ef4fb2022-04-07 09:40:15 +020025my $verbose = 0;
26my %verbose_messages = ();
27my %verbose_emitted = ();
Stefan Reinauer44d0fd92015-02-11 01:49:00 +010028my $tree = 1;
29my $chk_signoff = 1;
30my $chk_patch = 1;
31my $tst_only;
32my $emacs = 0;
33my $terse = 0;
Stefan Reinauerc6080c62016-07-29 16:01:40 -070034my $showfile = 0;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +010035my $file = 0;
Stefan Reinauerc6080c62016-07-29 16:01:40 -070036my $git = 0;
37my %git_commits = ();
Stefan Reinauer44d0fd92015-02-11 01:49:00 +010038my $check = 0;
39my $check_orig = 0;
40my $summary = 1;
41my $mailback = 0;
42my $summary_file = 0;
43my $show_types = 0;
Stefan Reinauerc6080c62016-07-29 16:01:40 -070044my $list_types = 0;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +010045my $fix = 0;
46my $fix_inplace = 0;
Martin Rothedd591d2017-03-14 10:16:29 -060047my $root = $P; #coreboot
Elyes HAOUAS18ef5202022-01-29 08:27:14 +010048my $gitroot = $ENV{'GIT_DIR'};
49$gitroot = ".git" if !defined($gitroot);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +010050my %debug;
51my %camelcase = ();
52my %use_type = ();
53my @use = ();
54my %ignore_type = ();
55my @ignore = ();
Martin Rothedd591d2017-03-14 10:16:29 -060056my @exclude = (); #coreboot
Stefan Reinauer44d0fd92015-02-11 01:49:00 +010057my $help = 0;
58my $configuration_file = ".checkpatch.conf";
Elyes Haouas0b1a03c2022-04-07 10:13:54 +020059my $max_line_length = 100;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +010060my $ignore_perl_version = 0;
61my $minimum_perl_version = 5.10.0;
62my $min_conf_desc_length = 4;
63my $spelling_file = "$D/spelling.txt";
Stefan Reinauerc6080c62016-07-29 16:01:40 -070064my $codespell = 0;
65my $codespellfile = "/usr/share/codespell/dictionary.txt";
Elyes Haouas84ef4fb2022-04-07 09:40:15 +020066my $user_codespellfile = "";
Martin Rothedd591d2017-03-14 10:16:29 -060067my $conststructsfile = "$D/const_structs.checkpatch";
Elyes Haouas46b07e52022-04-07 10:07:34 +020068my $docsfile = "";
69my $typedefsfile;
Martin Roth387dec82017-09-17 19:20:46 -060070my $color = "auto";
Elyes Haouas8f599602022-04-12 12:55:43 +020071my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
Elyes HAOUASf2a9c8d2022-01-29 08:31:06 +010072my $git_command ='git'; # coreboot
Elyes HAOUAS96771bf2022-01-29 07:28:45 +010073my $tabsize = 8;
Elyes Haouasf62a9892022-04-12 12:17:50 +020074my ${CONFIG_} = "CONFIG_";
Martin Roth1f3daea2017-08-30 13:53:58 -060075# For coreboot jenkins
76# If taint mode is enabled, Untaint the path - files must be in /bin, /usr/bin or /usr/local/bin
77if ( ${^TAINT} ) {
78 $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
79 delete @ENV{ 'IFS', 'CDPATH', 'ENV', 'BASH_ENV' };
80}
81
Stefan Reinauer44d0fd92015-02-11 01:49:00 +010082sub help {
83 my ($exitcode) = @_;
84
85 print << "EOM";
86Usage: $P [OPTION]... [FILE]...
87Version: $V
88
89Options:
90 -q, --quiet quiet
Elyes Haouas46b07e52022-04-07 10:07:34 +020091 -v, --verbose verbose mode
Stefan Reinauerc6080c62016-07-29 16:01:40 -070092 --no-tree run without a kernel tree
Stefan Reinauer44d0fd92015-02-11 01:49:00 +010093 --no-signoff do not check for 'Signed-off-by' line
94 --patch treat FILE as patchfile (default)
95 --emacs emacs compile window format
96 --terse one line per report
Stefan Reinauerc6080c62016-07-29 16:01:40 -070097 --showfile emit diffed file position, not input file position
98 -g, --git treat FILE as a single commit or git revision range
99 single git commit with:
100 <rev>
101 <rev>^
102 <rev>~n
103 multiple git commits with:
104 <rev1>..<rev2>
105 <rev1>...<rev2>
106 <rev>-<count>
107 git merges are ignored
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100108 -f, --file treat FILE as regular source file
109 --subjective, --strict enable more subjective tests
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700110 --list-types list the possible message types
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100111 --types TYPE(,TYPE2...) show only these comma separated message types
112 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Martin Rotha3cac872017-03-04 18:17:35 -0700113 --exclude DIR(,DIR22...) exclude directories
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700114 --show-types show the specific message type in the output
Elyes Haouas0b1a03c2022-04-07 10:13:54 +0200115 --max-line-length=n set the maximum line length, (default $max_line_length)
116 if exceeded, warn on patches
117 requires --strict for use with --file
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100118 --min-conf-desc-length=n set the min description length, if shorter, warn
Elyes Haouas41d43f52022-04-12 19:54:37 +0200119 --tab-size=n set the number of spaces for tab (default $tabsize)
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700120 --root=PATH PATH to the kernel tree root
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100121 --no-summary suppress the per-file summary
122 --mailback only produce a report in case of warnings/errors
123 --summary-file include the filename in summary
124 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
125 'values', 'possible', 'type', and 'attr' (default
126 is all off)
127 --test-only=WORD report only warnings/errors containing WORD
128 literally
129 --fix EXPERIMENTAL - may create horrible results
130 If correctable single-line errors exist, create
131 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
132 with potential errors corrected to the preferred
133 checkpatch style
134 --fix-inplace EXPERIMENTAL - may create horrible results
135 Is the same as --fix, but overwrites the input
136 file. It's your fault if there's no backup or git
137 --ignore-perl-version override checking of perl version. expect
138 runtime errors.
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700139 --codespell Use the codespell dictionary for spelling/typos
Elyes Haouas84ef4fb2022-04-07 09:40:15 +0200140 (default:$codespellfile)
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700141 --codespellfile Use this codespell dictionary
Martin Roth387dec82017-09-17 19:20:46 -0600142 --typedefsfile Read additional types from this file
143 --color[=WHEN] Use colors 'always', 'never', or only when output
144 is a terminal ('auto'). Default is 'auto'.
Elyes Haouasf62a9892022-04-12 12:17:50 +0200145 --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default
146 ${CONFIG_})
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100147 -h, --help, --version display this help and exit
148
149When FILE is - read standard input.
150EOM
151
152 exit($exitcode);
153}
154
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700155sub uniq {
156 my %seen;
157 return grep { !$seen{$_}++ } @_;
158}
159
160sub list_types {
161 my ($exitcode) = @_;
162
163 my $count = 0;
164
165 local $/ = undef;
166
167 open(my $script, '<', abs_path($P)) or
168 die "$P: Can't read '$P' $!\n";
169
170 my $text = <$script>;
171 close($script);
172
Elyes Haouas46b07e52022-04-07 10:07:34 +0200173 my %types = ();
Martin Roth387dec82017-09-17 19:20:46 -0600174 # Also catch when type or level is passed through a variable
Elyes Haouas46b07e52022-04-07 10:07:34 +0200175 while ($text =~ /(?:(\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
176 if (defined($1)) {
177 if (exists($types{$2})) {
178 $types{$2} .= ",$1" if ($types{$2} ne $1);
179 } else {
180 $types{$2} = $1;
181 }
182 } else {
183 $types{$2} = "UNDETERMINED";
184 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700185 }
Elyes Haouas46b07e52022-04-07 10:07:34 +0200186
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700187 print("#\tMessage type\n\n");
Elyes Haouas46b07e52022-04-07 10:07:34 +0200188 if ($color) {
189 print(" ( Color coding: ");
190 print(RED . "ERROR" . RESET);
191 print(" | ");
192 print(YELLOW . "WARNING" . RESET);
193 print(" | ");
194 print(GREEN . "CHECK" . RESET);
195 print(" | ");
196 print("Multiple levels / Undetermined");
197 print(" )\n\n");
198 }
199
200 foreach my $type (sort keys %types) {
201 my $orig_type = $type;
202 if ($color) {
203 my $level = $types{$type};
204 if ($level eq "ERROR") {
205 $type = RED . $type . RESET;
206 } elsif ($level eq "WARN") {
207 $type = YELLOW . $type . RESET;
208 } elsif ($level eq "CHK") {
209 $type = GREEN . $type . RESET;
210 }
211 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700212 print(++$count . "\t" . $type . "\n");
Elyes Haouas46b07e52022-04-07 10:07:34 +0200213 if ($verbose && exists($verbose_messages{$orig_type})) {
214 my $message = $verbose_messages{$orig_type};
215 $message =~ s/\n/\n\t/g;
216 print("\t" . $message . "\n\n");
217 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700218 }
219
220 exit($exitcode);
221}
222
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100223my $conf = which_conf($configuration_file);
224if (-f $conf) {
225 my @conf_args;
226 open(my $conffile, '<', "$conf")
227 or warn "$P: Can't find a readable $configuration_file file $!\n";
228
229 while (<$conffile>) {
230 my $line = $_;
231
232 $line =~ s/\s*\n?$//g;
233 $line =~ s/^\s*//g;
234 $line =~ s/\s+/ /g;
235
236 next if ($line =~ m/^\s*#/);
237 next if ($line =~ m/^\s*$/);
238
239 my @words = split(" ", $line);
240 foreach my $word (@words) {
241 last if ($word =~ m/^#/);
242 push (@conf_args, $word);
243 }
244 }
245 close($conffile);
246 unshift(@ARGV, @conf_args) if @conf_args;
247}
248
Elyes Haouas46b07e52022-04-07 10:07:34 +0200249sub load_docs {
250 open(my $docs, '<', "$docsfile")
251 or warn "$P: Can't read the documentation file $docsfile $!\n";
252
253 my $type = '';
254 my $desc = '';
255 my $in_desc = 0;
256
257 while (<$docs>) {
258 chomp;
259 my $line = $_;
260 $line =~ s/\s+$//;
261
262 if ($line =~ /^\s*\*\*(.+)\*\*$/) {
263 if ($desc ne '') {
264 $verbose_messages{$type} = trim($desc);
265 }
266 $type = $1;
267 $desc = '';
268 $in_desc = 1;
269 } elsif ($in_desc) {
270 if ($line =~ /^(?:\s{4,}|$)/) {
271 $line =~ s/^\s{4}//;
272 $desc .= $line;
273 $desc .= "\n";
274 } else {
275 $verbose_messages{$type} = trim($desc);
276 $type = '';
277 $desc = '';
278 $in_desc = 0;
279 }
280 }
281 }
282
283 if ($desc ne '') {
284 $verbose_messages{$type} = trim($desc);
285 }
286 close($docs);
287}
288
Martin Roth387dec82017-09-17 19:20:46 -0600289# Perl's Getopt::Long allows options to take optional arguments after a space.
290# Prevent --color by itself from consuming other arguments
291foreach (@ARGV) {
292 if ($_ eq "--color" || $_ eq "-color") {
293 $_ = "--color=$color";
294 }
295}
296
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100297GetOptions(
298 'q|quiet+' => \$quiet,
Elyes Haouas46b07e52022-04-07 10:07:34 +0200299 'v|verbose!' => \$verbose,
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100300 'tree!' => \$tree,
301 'signoff!' => \$chk_signoff,
302 'patch!' => \$chk_patch,
303 'emacs!' => \$emacs,
304 'terse!' => \$terse,
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700305 'showfile!' => \$showfile,
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100306 'f|file!' => \$file,
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700307 'g|git!' => \$git,
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100308 'subjective!' => \$check,
309 'strict!' => \$check,
310 'ignore=s' => \@ignore,
Martin Rothedd591d2017-03-14 10:16:29 -0600311 'exclude=s' => \@exclude, #coreboot
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100312 'types=s' => \@use,
313 'show-types!' => \$show_types,
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700314 'list-types!' => \$list_types,
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100315 'max-line-length=i' => \$max_line_length,
316 'min-conf-desc-length=i' => \$min_conf_desc_length,
Elyes Haouas41d43f52022-04-12 19:54:37 +0200317 'tab-size=i' => \$tabsize,
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100318 'root=s' => \$root,
319 'summary!' => \$summary,
320 'mailback!' => \$mailback,
321 'summary-file!' => \$summary_file,
322 'fix!' => \$fix,
323 'fix-inplace!' => \$fix_inplace,
324 'ignore-perl-version!' => \$ignore_perl_version,
325 'debug=s' => \%debug,
326 'test-only=s' => \$tst_only,
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700327 'codespell!' => \$codespell,
Elyes Haouas84ef4fb2022-04-07 09:40:15 +0200328 'codespellfile=s' => \$user_codespellfile,
Martin Roth387dec82017-09-17 19:20:46 -0600329 'typedefsfile=s' => \$typedefsfile,
330 'color=s' => \$color,
331 'no-color' => \$color, #keep old behaviors of -nocolor
332 'nocolor' => \$color, #keep old behaviors of -nocolor
Elyes Haouasf62a9892022-04-12 12:17:50 +0200333 'kconfig-prefix=s' => \${CONFIG_},
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100334 'h|help' => \$help,
335 'version' => \$help
Elyes Haouas84ef4fb2022-04-07 09:40:15 +0200336) or $help = 2;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100337
Elyes Haouas84ef4fb2022-04-07 09:40:15 +0200338if ($user_codespellfile) {
339 # Use the user provided codespell file unconditionally
340 $codespellfile = $user_codespellfile;
341} elsif (!(-f $codespellfile)) {
342 # If /usr/share/codespell/dictionary.txt is not present, try to find it
343 # under codespell's install directory: <codespell_root>/data/dictionary.txt
344 if (($codespell || $help) && which("python3") ne "") {
345 my $python_codespell_dict = << "EOF";
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100346
Elyes Haouas84ef4fb2022-04-07 09:40:15 +0200347import os.path as op
348import codespell_lib
349codespell_dir = op.dirname(codespell_lib.__file__)
350codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt')
351print(codespell_file, end='')
352EOF
353
354 my $codespell_dict = `python3 -c "$python_codespell_dict" 2> /dev/null`;
355 $codespellfile = $codespell_dict if (-f $codespell_dict);
356 }
357}
358
359# $help is 1 if either -h, --help or --version is passed as option - exitcode: 0
360# $help is 2 if invalid option is passed - exitcode: 1
361help($help - 1) if ($help);
362
363die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
364die "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse);
365
366if ($color =~ /^[01]$/) {
367 $color = !$color;
368} elsif ($color =~ /^always$/i) {
369 $color = 1;
370} elsif ($color =~ /^never$/i) {
371 $color = 0;
372} elsif ($color =~ /^auto$/i) {
373 $color = (-t STDOUT);
374} else {
375 die "$P: Invalid color mode: $color\n";
376}
377
378load_docs() if ($verbose);
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700379list_types(0) if ($list_types);
380
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100381$fix = 1 if ($fix_inplace);
382$check_orig = $check;
383
384my $exit = 0;
385
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +0100386my $perl_version_ok = 1;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100387if ($^V && $^V lt $minimum_perl_version) {
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +0100388 $perl_version_ok = 0;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100389 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +0100390 exit(1) if (!$ignore_perl_version);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100391}
392
Martin Rothedd591d2017-03-14 10:16:29 -0600393#if no filenames are given, push '-' to read patch from stdin
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100394if ($#ARGV < 0) {
Martin Rothedd591d2017-03-14 10:16:29 -0600395 push(@ARGV, '-');
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100396}
397
Elyes Haouas41d43f52022-04-12 19:54:37 +0200398# skip TAB size 1 to avoid additional checks on $tabsize - 1
399die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
400
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100401sub hash_save_array_words {
402 my ($hashRef, $arrayRef) = @_;
403
404 my @array = split(/,/, join(',', @$arrayRef));
405 foreach my $word (@array) {
406 $word =~ s/\s*\n?$//g;
407 $word =~ s/^\s*//g;
408 $word =~ s/\s+/ /g;
409 $word =~ tr/[a-z]/[A-Z]/;
410
411 next if ($word =~ m/^\s*#/);
412 next if ($word =~ m/^\s*$/);
413
414 $hashRef->{$word}++;
415 }
416}
417
418sub hash_show_words {
419 my ($hashRef, $prefix) = @_;
420
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700421 if (keys %$hashRef) {
422 print "\nNOTE: $prefix message types:";
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100423 foreach my $word (sort keys %$hashRef) {
424 print " $word";
425 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700426 print "\n";
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100427 }
428}
429
430hash_save_array_words(\%ignore_type, \@ignore);
431hash_save_array_words(\%use_type, \@use);
432
433my $dbg_values = 0;
434my $dbg_possible = 0;
435my $dbg_type = 0;
436my $dbg_attr = 0;
437for my $key (keys %debug) {
438 ## no critic
439 eval "\${dbg_$key} = '$debug{$key}';";
440 die "$@" if ($@);
441}
442
443my $rpt_cleaners = 0;
444
445if ($terse) {
446 $emacs = 1;
447 $quiet++;
448}
449
450if ($tree) {
451 if (defined $root) {
452 if (!top_of_kernel_tree($root)) {
453 die "$P: $root: --root does not point at a valid tree\n";
454 }
455 } else {
456 if (top_of_kernel_tree('.')) {
457 $root = '.';
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700458 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100459 top_of_kernel_tree($1)) {
460 $root = $1;
461 }
462 }
463
464 if (!defined $root) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700465 print "Must be run from the top-level dir. of a kernel tree\n";
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100466 exit(2);
467 }
468}
469
470my $emitted_corrupt = 0;
471
472our $Ident = qr{
473 [A-Za-z_][A-Za-z\d_]*
474 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
475 }x;
476our $Storage = qr{extern|static|asmlinkage};
477our $Sparse = qr{
478 __user|
479 __kernel|
480 __force|
481 __iomem|
482 __must_check|
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100483 __kprobes|
484 __ref|
Elyes Haouas26e0b942022-05-22 11:11:11 +0200485 __refconst|
486 __refdata|
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700487 __rcu|
488 __private
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100489 }x;
490our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
491our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
492our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
493our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
494our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
495
496# Notes to $Attribute:
497# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
498our $Attribute = qr{
499 const|
Elyes Haouas26e0b942022-05-22 11:11:11 +0200500 volatile|
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100501 __percpu|
502 __nocast|
503 __safe|
Martin Rothedd591d2017-03-14 10:16:29 -0600504 __bitwise|
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100505 __packed__|
506 __packed2__|
507 __naked|
508 __maybe_unused|
509 __always_unused|
510 __noreturn|
511 __used|
512 __cold|
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700513 __pure|
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100514 __noclone|
515 __deprecated|
516 __read_mostly|
Elyes Haouas26e0b942022-05-22 11:11:11 +0200517 __ro_after_init|
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100518 __kprobes|
519 $InitAttribute|
520 ____cacheline_aligned|
521 ____cacheline_aligned_in_smp|
522 ____cacheline_internodealigned_in_smp|
Elyes Haouas26e0b942022-05-22 11:11:11 +0200523 __weak|
524 __alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\)
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100525 }x;
526our $Modifier;
527our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
528our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
529our $Lval = qr{$Ident(?:$Member)*};
530
531our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
532our $Binary = qr{(?i)0b[01]+$Int_type?};
533our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
534our $Int = qr{[0-9]+$Int_type?};
535our $Octal = qr{0[0-7]+$Int_type?};
Elyes Haouas26e0b942022-05-22 11:11:11 +0200536our $String = qr{(?:\b[Lu])?"[X\t]*"};
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100537our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
538our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
539our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
540our $Float = qr{$Float_hex|$Float_dec|$Float_int};
541our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
542our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
543our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
544our $Arithmetic = qr{\+|-|\*|\/|%};
545our $Operators = qr{
546 <=|>=|==|!=|
547 =>|->|<<|>>|<|>|!|~|
548 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
549 }x;
550
551our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
552
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700553our $BasicType;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100554our $NonptrType;
555our $NonptrTypeMisordered;
556our $NonptrTypeWithAttr;
557our $Type;
558our $TypeMisordered;
559our $Declare;
560our $DeclareMisordered;
561
562our $NON_ASCII_UTF8 = qr{
563 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
564 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
565 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
566 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
567 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
568 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
569 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
570}x;
571
572our $UTF8 = qr{
573 [\x09\x0A\x0D\x20-\x7E] # ASCII
574 | $NON_ASCII_UTF8
575}x;
576
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700577our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
578our $typeOtherOSTypedefs = qr{(?x:
579 u_(?:char|short|int|long) | # bsd
580 u(?:nchar|short|int|long) # sysv
581)};
582our $typeKernelTypedefs = qr{(?x:
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100583 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
584 atomic_t
585)};
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700586our $typeTypedefs = qr{(?x:
587 $typeC99Typedefs\b|
588 $typeOtherOSTypedefs\b|
589 $typeKernelTypedefs\b
590)};
591
592our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100593
594our $logFunctions = qr{(?x:
Martin Rothedd591d2017-03-14 10:16:29 -0600595 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100596 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Martin Roth60915b32018-08-10 21:04:05 -0600597 TP_printk|
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100598 WARN(?:_RATELIMIT|_ONCE|)|
599 panic|
600 MODULE_[A-Z_]+|
601 seq_vprintf|seq_printf|seq_puts
602)};
603
Elyes Haouasa7b0d382022-05-23 18:33:15 +0200604our $allocFunctions = qr{(?x:
605 (?:(?:devm_)?
606 (?:kv|k|v)[czm]alloc(?:_array)?(?:_node)? |
607 kstrdup(?:_const)? |
608 kmemdup(?:_nul)?) |
609 (?:\w+)?alloc_skb(?:_ip_align)? |
610 # dev_alloc_skb/netdev_alloc_skb, et al
611 dma_alloc_coherent
612)};
613
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100614our $signature_tags = qr{(?xi:
615 Signed-off-by:|
616 Acked-by:|
617 Tested-by:|
618 Reviewed-by:|
619 Reported-by:|
620 Suggested-by:|
Michael Niewöhner8cfd3f82022-11-02 18:17:57 +0100621 Co-authored-by:|
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100622 To:|
623 Cc:
624)};
625
626our @typeListMisordered = (
627 qr{char\s+(?:un)?signed},
628 qr{int\s+(?:(?:un)?signed\s+)?short\s},
629 qr{int\s+short(?:\s+(?:un)?signed)},
630 qr{short\s+int(?:\s+(?:un)?signed)},
631 qr{(?:un)?signed\s+int\s+short},
632 qr{short\s+(?:un)?signed},
633 qr{long\s+int\s+(?:un)?signed},
634 qr{int\s+long\s+(?:un)?signed},
635 qr{long\s+(?:un)?signed\s+int},
636 qr{int\s+(?:un)?signed\s+long},
637 qr{int\s+(?:un)?signed},
638 qr{int\s+long\s+long\s+(?:un)?signed},
639 qr{long\s+long\s+int\s+(?:un)?signed},
640 qr{long\s+long\s+(?:un)?signed\s+int},
641 qr{long\s+long\s+(?:un)?signed},
642 qr{long\s+(?:un)?signed},
643);
644
645our @typeList = (
646 qr{void},
647 qr{(?:(?:un)?signed\s+)?char},
648 qr{(?:(?:un)?signed\s+)?short\s+int},
649 qr{(?:(?:un)?signed\s+)?short},
650 qr{(?:(?:un)?signed\s+)?int},
651 qr{(?:(?:un)?signed\s+)?long\s+int},
652 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
653 qr{(?:(?:un)?signed\s+)?long\s+long},
654 qr{(?:(?:un)?signed\s+)?long},
655 qr{(?:un)?signed},
656 qr{float},
657 qr{double},
658 qr{bool},
659 qr{struct\s+$Ident},
660 qr{union\s+$Ident},
661 qr{enum\s+$Ident},
662 qr{${Ident}_t},
663 qr{${Ident}_handler},
664 qr{${Ident}_handler_fn},
665 @typeListMisordered,
666);
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700667
668our $C90_int_types = qr{(?x:
669 long\s+long\s+int\s+(?:un)?signed|
670 long\s+long\s+(?:un)?signed\s+int|
671 long\s+long\s+(?:un)?signed|
672 (?:(?:un)?signed\s+)?long\s+long\s+int|
673 (?:(?:un)?signed\s+)?long\s+long|
674 int\s+long\s+long\s+(?:un)?signed|
675 int\s+(?:(?:un)?signed\s+)?long\s+long|
676
677 long\s+int\s+(?:un)?signed|
678 long\s+(?:un)?signed\s+int|
679 long\s+(?:un)?signed|
680 (?:(?:un)?signed\s+)?long\s+int|
681 (?:(?:un)?signed\s+)?long|
682 int\s+long\s+(?:un)?signed|
683 int\s+(?:(?:un)?signed\s+)?long|
684
685 int\s+(?:un)?signed|
686 (?:(?:un)?signed\s+)?int
687)};
688
689our @typeListFile = ();
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100690our @typeListWithAttr = (
691 @typeList,
692 qr{struct\s+$InitAttribute\s+$Ident},
693 qr{union\s+$InitAttribute\s+$Ident},
694);
695
696our @modifierList = (
697 qr{fastcall},
698);
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700699our @modifierListFile = ();
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100700
701our @mode_permission_funcs = (
702 ["module_param", 3],
703 ["module_param_(?:array|named|string)", 4],
704 ["module_param_array_named", 5],
705 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
706 ["proc_create(?:_data|)", 2],
Martin Rothedd591d2017-03-14 10:16:29 -0600707 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
708 ["IIO_DEV_ATTR_[A-Z_]+", 1],
709 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
710 ["SENSOR_TEMPLATE(?:_2|)", 3],
711 ["__ATTR", 2],
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100712);
713
Elyes Haouas84083a22022-04-12 12:28:32 +0200714my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
715
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100716#Create a search pattern for all these functions to speed up a loop below
717our $mode_perms_search = "";
718foreach my $entry (@mode_permission_funcs) {
719 $mode_perms_search .= '|' if ($mode_perms_search ne "");
720 $mode_perms_search .= $entry->[0];
721}
Martin Roth60915b32018-08-10 21:04:05 -0600722$mode_perms_search = "(?:${mode_perms_search})";
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100723
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700724our $mode_perms_world_writable = qr{
725 S_IWUGO |
726 S_IWOTH |
727 S_IRWXUGO |
728 S_IALLUGO |
729 0[0-7][0-7][2367]
730}x;
731
Martin Rothedd591d2017-03-14 10:16:29 -0600732our %mode_permission_string_types = (
733 "S_IRWXU" => 0700,
734 "S_IRUSR" => 0400,
735 "S_IWUSR" => 0200,
736 "S_IXUSR" => 0100,
737 "S_IRWXG" => 0070,
738 "S_IRGRP" => 0040,
739 "S_IWGRP" => 0020,
740 "S_IXGRP" => 0010,
741 "S_IRWXO" => 0007,
742 "S_IROTH" => 0004,
743 "S_IWOTH" => 0002,
744 "S_IXOTH" => 0001,
745 "S_IRWXUGO" => 0777,
746 "S_IRUGO" => 0444,
747 "S_IWUGO" => 0222,
748 "S_IXUGO" => 0111,
749);
750
751#Create a search pattern for all these strings to speed up a loop below
752our $mode_perms_string_search = "";
753foreach my $entry (keys %mode_permission_string_types) {
754 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
755 $mode_perms_string_search .= $entry;
756}
Martin Roth60915b32018-08-10 21:04:05 -0600757our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
758our $multi_mode_perms_string_search = qr{
759 ${single_mode_perms_string_search}
760 (?:\s*\|\s*${single_mode_perms_string_search})*
761}x;
762
763sub perms_to_octal {
764 my ($string) = @_;
765
766 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
767
768 my $val = "";
769 my $oval = "";
770 my $to = 0;
771 my $curpos = 0;
772 my $lastpos = 0;
773 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
774 $curpos = pos($string);
775 my $match = $2;
776 my $omatch = $1;
777 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
778 $lastpos = $curpos;
779 $to |= $mode_permission_string_types{$match};
780 $val .= '\s*\|\s*' if ($val ne "");
781 $val .= $match;
782 $oval .= $omatch;
783 }
784 $oval =~ s/^\s*\|\s*//;
785 $oval =~ s/\s*\|\s*$//;
786 return sprintf("%04o", $to);
787}
Martin Rothedd591d2017-03-14 10:16:29 -0600788
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100789our $allowed_asm_includes = qr{(?x:
790 irq|
791 memory|
792 time|
793 reboot
794)};
795# memory.h: ARM has a custom one
796
797# Load common spelling mistakes and build regular expression list.
798my $misspellings;
799my %spelling_fix;
800
801if (open(my $spelling, '<', $spelling_file)) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100802 while (<$spelling>) {
803 my $line = $_;
804
805 $line =~ s/\s*\n?$//g;
806 $line =~ s/^\s*//g;
807
808 next if ($line =~ m/^\s*#/);
809 next if ($line =~ m/^\s*$/);
810
811 my ($suspect, $fix) = split(/\|\|/, $line);
812
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100813 $spelling_fix{$suspect} = $fix;
814 }
815 close($spelling);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100816} else {
817 warn "No typos will be found - file '$spelling_file': $!\n";
818}
819
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700820if ($codespell) {
821 if (open(my $spelling, '<', $codespellfile)) {
822 while (<$spelling>) {
823 my $line = $_;
824
825 $line =~ s/\s*\n?$//g;
826 $line =~ s/^\s*//g;
827
828 next if ($line =~ m/^\s*#/);
829 next if ($line =~ m/^\s*$/);
830 next if ($line =~ m/, disabled/i);
831
832 $line =~ s/,.*$//;
833
834 my ($suspect, $fix) = split(/->/, $line);
835
836 $spelling_fix{$suspect} = $fix;
837 }
838 close($spelling);
839 } else {
840 warn "No codespell typos will be found - file '$codespellfile': $!\n";
841 }
842}
843
844$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
845
Martin Roth387dec82017-09-17 19:20:46 -0600846sub read_words {
847 my ($wordsRef, $file) = @_;
Martin Rothedd591d2017-03-14 10:16:29 -0600848
Martin Roth387dec82017-09-17 19:20:46 -0600849 if (open(my $words, '<', $file)) {
850 while (<$words>) {
851 my $line = $_;
Martin Rothedd591d2017-03-14 10:16:29 -0600852
Martin Roth387dec82017-09-17 19:20:46 -0600853 $line =~ s/\s*\n?$//g;
854 $line =~ s/^\s*//g;
855
856 next if ($line =~ m/^\s*#/);
857 next if ($line =~ m/^\s*$/);
858 if ($line =~ /\s/) {
859 print("$file: '$line' invalid - ignored\n");
860 next;
861 }
862
Elyes Haouas14c49e32022-05-08 10:11:40 +0200863 $$wordsRef .= '|' if (defined $$wordsRef);
Martin Roth387dec82017-09-17 19:20:46 -0600864 $$wordsRef .= $line;
Martin Rothedd591d2017-03-14 10:16:29 -0600865 }
Martin Roth387dec82017-09-17 19:20:46 -0600866 close($file);
867 return 1;
Martin Rothedd591d2017-03-14 10:16:29 -0600868 }
Martin Roth387dec82017-09-17 19:20:46 -0600869
870 return 0;
Martin Rothedd591d2017-03-14 10:16:29 -0600871}
872
Elyes Haouas43529c82022-04-12 13:19:50 +0200873my $const_structs;
874if (show_type("CONST_STRUCT")) {
875 read_words(\$const_structs, $conststructsfile)
876 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
877}
Martin Roth387dec82017-09-17 19:20:46 -0600878
Elyes Haouas26e0b942022-05-22 11:11:11 +0200879if (defined($typedefsfile)) {
880 my $typeOtherTypedefs;
Martin Roth387dec82017-09-17 19:20:46 -0600881 read_words(\$typeOtherTypedefs, $typedefsfile)
882 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
Elyes Haouas26e0b942022-05-22 11:11:11 +0200883 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
Martin Roth387dec82017-09-17 19:20:46 -0600884}
Martin Roth387dec82017-09-17 19:20:46 -0600885
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100886sub build_types {
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700887 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
888 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100889 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
890 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
891 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700892 $BasicType = qr{
893 (?:$typeTypedefs\b)|
894 (?:${all}\b)
895 }x;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100896 $NonptrType = qr{
897 (?:$Modifier\s+|const\s+)*
898 (?:
899 (?:typeof|__typeof__)\s*\([^\)]*\)|
900 (?:$typeTypedefs\b)|
901 (?:${all}\b)
902 )
903 (?:\s+$Modifier|\s+const)*
904 }x;
905 $NonptrTypeMisordered = qr{
906 (?:$Modifier\s+|const\s+)*
907 (?:
908 (?:${Misordered}\b)
909 )
910 (?:\s+$Modifier|\s+const)*
911 }x;
912 $NonptrTypeWithAttr = qr{
913 (?:$Modifier\s+|const\s+)*
914 (?:
915 (?:typeof|__typeof__)\s*\([^\)]*\)|
916 (?:$typeTypedefs\b)|
917 (?:${allWithAttr}\b)
918 )
919 (?:\s+$Modifier|\s+const)*
920 }x;
921 $Type = qr{
922 $NonptrType
Elyes Haouas26e0b942022-05-22 11:11:11 +0200923 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100924 (?:\s+$Inline|\s+$Modifier)*
925 }x;
926 $TypeMisordered = qr{
927 $NonptrTypeMisordered
Elyes Haouas26e0b942022-05-22 11:11:11 +0200928 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100929 (?:\s+$Inline|\s+$Modifier)*
930 }x;
931 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
932 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
933}
934build_types();
935
936our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
937
938# Using $balanced_parens, $LvalOrFunc, or $FuncArg
939# requires at least perl version v5.10.0
940# Any use must be runtime checked with $^V
941
942our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
943our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700944our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100945
946our $declaration_macros = qr{(?x:
Stefan Reinauerc6080c62016-07-29 16:01:40 -0700947 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Martin Roth387dec82017-09-17 19:20:46 -0600948 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Elyes Haouasd27b2e82022-09-05 11:50:48 +0200949 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(|
950 (?:$Storage\s+)?(?:XA_STATE|XA_STATE_ORDER)\s*\(
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100951)};
952
Elyes Haouas84083a22022-04-12 12:28:32 +0200953our %allow_repeated_words = (
954 add => '',
955 added => '',
956 bad => '',
957 be => '',
958);
959
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100960sub deparenthesize {
961 my ($string) = @_;
962 return "" if (!defined($string));
963
964 while ($string =~ /^\s*\(.*\)\s*$/) {
965 $string =~ s@^\s*\(\s*@@;
966 $string =~ s@\s*\)\s*$@@;
967 }
968
969 $string =~ s@\s+@ @g;
970
971 return $string;
972}
973
974sub seed_camelcase_file {
975 my ($file) = @_;
976
977 return if (!(-f $file));
978
979 local $/;
980
981 open(my $include_file, '<', "$file")
982 or warn "$P: Can't read '$file' $!\n";
983 my $text = <$include_file>;
984 close($include_file);
985
986 my @lines = split('\n', $text);
987
988 foreach my $line (@lines) {
989 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
990 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
991 $camelcase{$1} = 1;
992 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
993 $camelcase{$1} = 1;
994 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
995 $camelcase{$1} = 1;
996 }
997 }
998}
999
Martin Rothedd591d2017-03-14 10:16:29 -06001000sub is_maintained_obsolete {
1001 my ($filename) = @_;
1002
1003 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
1004
1005 my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
1006
1007 return $status =~ /obsolete/i;
1008}
1009
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001010my $camelcase_seeded = 0;
1011sub seed_camelcase_includes {
1012 return if ($camelcase_seeded);
1013
1014 my $files;
1015 my $camelcase_cache = "";
1016 my @include_files = ();
1017
1018 $camelcase_seeded = 1;
1019
Elyes HAOUAS18ef5202022-01-29 08:27:14 +01001020 if (-e "$gitroot") {
Elyes HAOUASf2a9c8d2022-01-29 08:31:06 +01001021 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001022 chomp $git_last_include_commit;
1023 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
1024 } else {
1025 my $last_mod_date = 0;
1026 $files = `find $root/include -name "*.h"`;
1027 @include_files = split('\n', $files);
1028 foreach my $file (@include_files) {
1029 my $date = POSIX::strftime("%Y%m%d%H%M",
1030 localtime((stat $file)[9]));
1031 $last_mod_date = $date if ($last_mod_date < $date);
1032 }
1033 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
1034 }
1035
1036 if ($camelcase_cache ne "" && -f $camelcase_cache) {
1037 open(my $camelcase_file, '<', "$camelcase_cache")
1038 or warn "$P: Can't read '$camelcase_cache' $!\n";
1039 while (<$camelcase_file>) {
1040 chomp;
1041 $camelcase{$_} = 1;
1042 }
1043 close($camelcase_file);
1044
1045 return;
1046 }
1047
Elyes HAOUAS18ef5202022-01-29 08:27:14 +01001048 if (-e "$gitroot") {
Elyes HAOUASf2a9c8d2022-01-29 08:31:06 +01001049 $files = `${git_command} ls-files "include/*.h"`;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001050 @include_files = split('\n', $files);
1051 }
1052
1053 foreach my $file (@include_files) {
1054 seed_camelcase_file($file);
1055 }
1056
1057 if ($camelcase_cache ne "") {
1058 unlink glob ".checkpatch-camelcase.*";
1059 open(my $camelcase_file, '>', "$camelcase_cache")
1060 or warn "$P: Can't write '$camelcase_cache' $!\n";
1061 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1062 print $camelcase_file ("$_\n");
1063 }
1064 close($camelcase_file);
1065 }
1066}
1067
1068sub git_commit_info {
1069 my ($commit, $id, $desc) = @_;
1070
Elyes HAOUAS18ef5202022-01-29 08:27:14 +01001071 return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001072
Elyes HAOUASf2a9c8d2022-01-29 08:31:06 +01001073 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001074 $output =~ s/^\s*//gm;
1075 my @lines = split("\n", $output);
1076
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001077 return ($id, $desc) if ($#lines < 0);
1078
Elyes Haouas26e0b942022-05-22 11:11:11 +02001079 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001080# Maybe one day convert this block of bash into something that returns
1081# all matching commit ids, but it's very slow...
1082#
1083# echo "checking commits $1..."
1084# git rev-list --remotes | grep -i "^$1" |
1085# while read line ; do
1086# git log --format='%H %s' -1 $line |
1087# echo "commit $(cut -c 1-12,41-)"
1088# done
Elyes Haouas26e0b942022-05-22 11:11:11 +02001089 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
1090 $lines[0] =~ /^fatal: bad object $commit/) {
Martin Roth387dec82017-09-17 19:20:46 -06001091 $id = undef;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001092 } else {
1093 $id = substr($lines[0], 0, 12);
1094 $desc = substr($lines[0], 41);
1095 }
1096
1097 return ($id, $desc);
1098}
1099
1100$chk_signoff = 0 if ($file);
1101
1102my @rawlines = ();
1103my @lines = ();
1104my @fixed = ();
1105my @fixed_inserted = ();
1106my @fixed_deleted = ();
1107my $fixlinenr = -1;
1108
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001109# If input is git commits, extract all commits from the commit expressions.
1110# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
Elyes HAOUAS18ef5202022-01-29 08:27:14 +01001111die "$P: No git repository found\n" if ($git && !-e "$gitroot");
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001112
1113if ($git) {
1114 my @commits = ();
1115 foreach my $commit_expr (@ARGV) {
1116 my $git_range;
1117 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1118 $git_range = "-$2 $1";
1119 } elsif ($commit_expr =~ m/\.\./) {
1120 $git_range = "$commit_expr";
1121 } else {
1122 $git_range = "-1 $commit_expr";
1123 }
Elyes HAOUASf2a9c8d2022-01-29 08:31:06 +01001124 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001125 foreach my $line (split(/\n/, $lines)) {
1126 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1127 next if (!defined($1) || !defined($2));
1128 my $sha1 = $1;
1129 my $subject = $2;
1130 unshift(@commits, $sha1);
1131 $git_commits{$sha1} = $subject;
1132 }
1133 }
1134 die "$P: no git commits after extraction!\n" if (@commits == 0);
1135 @ARGV = @commits;
1136}
1137
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001138my $vname;
Elyes Haouas8f599602022-04-12 12:55:43 +02001139$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Martin Roth387dec82017-09-17 19:20:46 -06001140for my $filename (@ARGV) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001141 my $FILE;
Martin Rotha9868b22018-01-27 17:31:42 -07001142
1143 # coreboot: Mark filename as untainted
1144 $filename =~ /^(.*)$/s or die; $filename = $1;
1145
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001146 if ($git) {
1147 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1148 die "$P: $filename: git format-patch failed - $!\n";
1149 } elsif ($file) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001150 open($FILE, '-|', "diff -u /dev/null $filename") ||
1151 die "$P: $filename: diff failed - $!\n";
1152 } elsif ($filename eq '-') {
1153 open($FILE, '<&STDIN');
1154 } else {
1155 open($FILE, '<', "$filename") ||
1156 die "$P: $filename: open failed - $!\n";
1157 }
1158 if ($filename eq '-') {
1159 $vname = 'Your patch';
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001160 } elsif ($git) {
1161 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001162 } else {
1163 $vname = $filename;
1164 }
1165 while (<$FILE>) {
1166 chomp;
1167 push(@rawlines, $_);
1168 }
1169 close($FILE);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001170
1171 if ($#ARGV > 0 && $quiet == 0) {
1172 print '-' x length($vname) . "\n";
1173 print "$vname\n";
1174 print '-' x length($vname) . "\n";
1175 }
1176
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001177 if (!process($filename)) {
1178 $exit = 1;
1179 }
1180 @rawlines = ();
1181 @lines = ();
1182 @fixed = ();
1183 @fixed_inserted = ();
1184 @fixed_deleted = ();
1185 $fixlinenr = -1;
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001186 @modifierListFile = ();
1187 @typeListFile = ();
1188 build_types();
1189}
1190
1191if (!$quiet) {
1192 hash_show_words(\%use_type, "Used");
1193 hash_show_words(\%ignore_type, "Ignored");
1194
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01001195 if (!$perl_version_ok) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001196 print << "EOM"
1197
1198NOTE: perl $^V is not modern enough to detect all possible issues.
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01001199 An upgrade to at least perl $minimum_perl_version is suggested.
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001200EOM
1201 }
1202 if ($exit) {
1203 print << "EOM"
1204
1205NOTE: If any of the errors are false positives, please report
1206 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1207EOM
1208 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001209}
1210
1211exit($exit);
1212
1213sub top_of_kernel_tree {
1214 my ($root) = @_;
1215
1216 my @tree_check = (
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001217 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1218 "README", "Documentation", "arch", "include", "drivers",
1219 "fs", "init", "ipc", "kernel", "lib", "scripts",
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001220 );
1221
1222 foreach my $check (@tree_check) {
1223 if (! -e $root . '/' . $check) {
1224 return 0;
1225 }
1226 }
1227 return 1;
1228}
1229
1230sub parse_email {
1231 my ($formatted_email) = @_;
1232
1233 my $name = "";
1234 my $address = "";
1235 my $comment = "";
1236
1237 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1238 $name = $1;
1239 $address = $2;
1240 $comment = $3 if defined $3;
1241 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1242 $address = $1;
1243 $comment = $2 if defined $2;
1244 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1245 $address = $1;
1246 $comment = $2 if defined $2;
Martin Roth60915b32018-08-10 21:04:05 -06001247 $formatted_email =~ s/\Q$address\E.*$//;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001248 $name = $formatted_email;
1249 $name = trim($name);
1250 $name =~ s/^\"|\"$//g;
1251 # If there's a name left after stripping spaces and
1252 # leading quotes, and the address doesn't have both
1253 # leading and trailing angle brackets, the address
1254 # is invalid. ie:
1255 # "joe smith joe@smith.com" bad
1256 # "joe smith <joe@smith.com" bad
1257 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1258 $name = "";
1259 $address = "";
1260 $comment = "";
1261 }
1262 }
1263
1264 $name = trim($name);
1265 $name =~ s/^\"|\"$//g;
1266 $address = trim($address);
1267 $address =~ s/^\<|\>$//g;
1268
1269 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1270 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1271 $name = "\"$name\"";
1272 }
1273
1274 return ($name, $address, $comment);
1275}
1276
1277sub format_email {
1278 my ($name, $address) = @_;
1279
1280 my $formatted_email;
1281
1282 $name = trim($name);
1283 $name =~ s/^\"|\"$//g;
1284 $address = trim($address);
1285
1286 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1287 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1288 $name = "\"$name\"";
1289 }
1290
1291 if ("$name" eq "") {
1292 $formatted_email = "$address";
1293 } else {
1294 $formatted_email = "$name <$address>";
1295 }
1296
1297 return $formatted_email;
1298}
1299
1300sub which {
1301 my ($bin) = @_;
1302
1303 foreach my $path (split(/:/, $ENV{PATH})) {
1304 if (-e "$path/$bin") {
1305 return "$path/$bin";
1306 }
1307 }
1308
1309 return "";
1310}
1311
1312sub which_conf {
1313 my ($conf) = @_;
1314
1315 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1316 if (-e "$path/$conf") {
1317 return "$path/$conf";
1318 }
1319 }
1320
1321 return "";
1322}
1323
1324sub expand_tabs {
1325 my ($str) = @_;
1326
1327 my $res = '';
1328 my $n = 0;
1329 for my $c (split(//, $str)) {
1330 if ($c eq "\t") {
1331 $res .= ' ';
1332 $n++;
Elyes HAOUAS96771bf2022-01-29 07:28:45 +01001333 for (; ($n % $tabsize) != 0; $n++) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001334 $res .= ' ';
1335 }
1336 next;
1337 }
1338 $res .= $c;
1339 $n++;
1340 }
1341
1342 return $res;
1343}
1344sub copy_spacing {
1345 (my $res = shift) =~ tr/\t/ /c;
1346 return $res;
1347}
1348
1349sub line_stats {
1350 my ($line) = @_;
1351
Elyes Haouas26e0b942022-05-22 11:11:11 +02001352 # Drop the diff line leader and expand tabs
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001353 $line =~ s/^.//;
Julius Werner218906d2021-03-25 21:12:49 -07001354
1355 # Treat labels like whitespace when counting indentation
1356 $line =~ s/^( ?$Ident:)/" " x length($1)/e;
1357
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001358 $line = expand_tabs($line);
1359
1360 # Pick the indent from the front of the line.
1361 my ($white) = ($line =~ /^(\s*)/);
1362
1363 return (length($line), length($white));
1364}
1365
1366my $sanitise_quote = '';
1367
1368sub sanitise_line_reset {
1369 my ($in_comment) = @_;
1370
1371 if ($in_comment) {
1372 $sanitise_quote = '*/';
1373 } else {
1374 $sanitise_quote = '';
1375 }
1376}
1377sub sanitise_line {
1378 my ($line) = @_;
1379
1380 my $res = '';
1381 my $l = '';
1382
1383 my $qlen = 0;
1384 my $off = 0;
1385 my $c;
1386
1387 # Always copy over the diff marker.
1388 $res = substr($line, 0, 1);
1389
1390 for ($off = 1; $off < length($line); $off++) {
1391 $c = substr($line, $off, 1);
1392
Martin Roth60915b32018-08-10 21:04:05 -06001393 # Comments we are whacking completely including the begin
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001394 # and end, all to $;.
1395 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1396 $sanitise_quote = '*/';
1397
1398 substr($res, $off, 2, "$;$;");
1399 $off++;
1400 next;
1401 }
1402 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1403 $sanitise_quote = '';
1404 substr($res, $off, 2, "$;$;");
1405 $off++;
1406 next;
1407 }
1408 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1409 $sanitise_quote = '//';
1410
1411 substr($res, $off, 2, $sanitise_quote);
1412 $off++;
1413 next;
1414 }
1415
1416 # A \ in a string means ignore the next character.
1417 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1418 $c eq "\\") {
1419 substr($res, $off, 2, 'XX');
1420 $off++;
1421 next;
1422 }
1423 # Regular quotes.
1424 if ($c eq "'" || $c eq '"') {
1425 if ($sanitise_quote eq '') {
1426 $sanitise_quote = $c;
1427
1428 substr($res, $off, 1, $c);
1429 next;
1430 } elsif ($sanitise_quote eq $c) {
1431 $sanitise_quote = '';
1432 }
1433 }
1434
1435 #print "c<$c> SQ<$sanitise_quote>\n";
1436 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1437 substr($res, $off, 1, $;);
1438 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1439 substr($res, $off, 1, $;);
1440 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1441 substr($res, $off, 1, 'X');
1442 } else {
1443 substr($res, $off, 1, $c);
1444 }
1445 }
1446
1447 if ($sanitise_quote eq '//') {
1448 $sanitise_quote = '';
1449 }
1450
1451 # The pathname on a #include may be surrounded by '<' and '>'.
1452 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1453 my $clean = 'X' x length($1);
1454 $res =~ s@\<.*\>@<$clean>@;
1455
1456 # The whole of a #error is a string.
1457 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1458 my $clean = 'X' x length($1);
1459 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1460 }
1461
Martin Rothedd591d2017-03-14 10:16:29 -06001462 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1463 my $match = $1;
1464 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1465 }
1466
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001467 return $res;
1468}
1469
1470sub get_quoted_string {
1471 my ($line, $rawline) = @_;
1472
Martin Roth60915b32018-08-10 21:04:05 -06001473 return "" if (!defined($line) || !defined($rawline));
Stefan Reinauerc6080c62016-07-29 16:01:40 -07001474 return "" if ($line !~ m/($String)/g);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001475 return substr($rawline, $-[0], $+[0] - $-[0]);
1476}
1477
1478sub ctx_statement_block {
1479 my ($linenr, $remain, $off) = @_;
1480 my $line = $linenr - 1;
1481 my $blk = '';
1482 my $soff = $off;
1483 my $coff = $off - 1;
1484 my $coff_set = 0;
1485
1486 my $loff = 0;
1487
1488 my $type = '';
1489 my $level = 0;
Julius Werner218906d2021-03-25 21:12:49 -07001490 my @stack = (['', $level]);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001491 my $p;
1492 my $c;
1493 my $len = 0;
1494
1495 my $remainder;
1496 while (1) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001497 #warn "CSB: blk<$blk> remain<$remain>\n";
1498 # If we are about to drop off the end, pull in more
1499 # context.
1500 if ($off >= $len) {
1501 for (; $remain > 0; $line++) {
1502 last if (!defined $lines[$line]);
1503 next if ($lines[$line] =~ /^-/);
1504 $remain--;
1505 $loff = $len;
1506 $blk .= $lines[$line] . "\n";
1507 $len = length($blk);
1508 $line++;
1509 last;
1510 }
1511 # Bail if there is no further context.
1512 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1513 if ($off >= $len) {
1514 last;
1515 }
1516 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1517 $level++;
1518 $type = '#';
1519 }
1520 }
1521 $p = $c;
1522 $c = substr($blk, $off, 1);
1523 $remainder = substr($blk, $off);
1524
1525 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1526
1527 # Handle nested #if/#else.
1528 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1529 push(@stack, [ $type, $level ]);
Julius Werner218906d2021-03-25 21:12:49 -07001530 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/ && $#stack > 0) {
1531 ($type, $level) = @{$stack[$#stack]};
1532 } elsif ($remainder =~ /^#\s*endif\b/ && $#stack > 0) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001533 ($type, $level) = @{pop(@stack)};
1534 }
1535
1536 # Statement ends at the ';' or a close '}' at the
1537 # outermost level.
1538 if ($level == 0 && $c eq ';') {
1539 last;
1540 }
1541
1542 # An else is really a conditional as long as its not else if
1543 if ($level == 0 && $coff_set == 0 &&
1544 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1545 $remainder =~ /^(else)(?:\s|{)/ &&
1546 $remainder !~ /^else\s+if\b/) {
1547 $coff = $off + length($1) - 1;
1548 $coff_set = 1;
1549 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1550 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1551 }
1552
1553 if (($type eq '' || $type eq '(') && $c eq '(') {
1554 $level++;
1555 $type = '(';
1556 }
1557 if ($type eq '(' && $c eq ')') {
1558 $level--;
1559 $type = ($level != 0)? '(' : '';
1560
1561 if ($level == 0 && $coff < $soff) {
1562 $coff = $off;
1563 $coff_set = 1;
1564 #warn "CSB: mark coff<$coff>\n";
1565 }
1566 }
1567 if (($type eq '' || $type eq '{') && $c eq '{') {
1568 $level++;
1569 $type = '{';
1570 }
1571 if ($type eq '{' && $c eq '}') {
1572 $level--;
1573 $type = ($level != 0)? '{' : '';
1574
1575 if ($level == 0) {
1576 if (substr($blk, $off + 1, 1) eq ';') {
1577 $off++;
1578 }
1579 last;
1580 }
1581 }
1582 # Preprocessor commands end at the newline unless escaped.
1583 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1584 $level--;
1585 $type = '';
1586 $off++;
1587 last;
1588 }
1589 $off++;
1590 }
1591 # We are truly at the end, so shuffle to the next line.
1592 if ($off == $len) {
1593 $loff = $len + 1;
1594 $line++;
1595 $remain--;
1596 }
1597
1598 my $statement = substr($blk, $soff, $off - $soff + 1);
1599 my $condition = substr($blk, $soff, $coff - $soff + 1);
1600
1601 #warn "STATEMENT<$statement>\n";
1602 #warn "CONDITION<$condition>\n";
1603
1604 #print "coff<$coff> soff<$off> loff<$loff>\n";
1605
1606 return ($statement, $condition,
1607 $line, $remain + 1, $off - $loff + 1, $level);
1608}
1609
1610sub statement_lines {
1611 my ($stmt) = @_;
1612
1613 # Strip the diff line prefixes and rip blank lines at start and end.
1614 $stmt =~ s/(^|\n)./$1/g;
1615 $stmt =~ s/^\s*//;
1616 $stmt =~ s/\s*$//;
1617
1618 my @stmt_lines = ($stmt =~ /\n/g);
1619
1620 return $#stmt_lines + 2;
1621}
1622
1623sub statement_rawlines {
1624 my ($stmt) = @_;
1625
1626 my @stmt_lines = ($stmt =~ /\n/g);
1627
1628 return $#stmt_lines + 2;
1629}
1630
1631sub statement_block_size {
1632 my ($stmt) = @_;
1633
1634 $stmt =~ s/(^|\n)./$1/g;
1635 $stmt =~ s/^\s*{//;
1636 $stmt =~ s/}\s*$//;
1637 $stmt =~ s/^\s*//;
1638 $stmt =~ s/\s*$//;
1639
1640 my @stmt_lines = ($stmt =~ /\n/g);
1641 my @stmt_statements = ($stmt =~ /;/g);
1642
1643 my $stmt_lines = $#stmt_lines + 2;
1644 my $stmt_statements = $#stmt_statements + 1;
1645
1646 if ($stmt_lines > $stmt_statements) {
1647 return $stmt_lines;
1648 } else {
1649 return $stmt_statements;
1650 }
1651}
1652
1653sub ctx_statement_full {
1654 my ($linenr, $remain, $off) = @_;
1655 my ($statement, $condition, $level);
1656
1657 my (@chunks);
1658
1659 # Grab the first conditional/block pair.
1660 ($statement, $condition, $linenr, $remain, $off, $level) =
1661 ctx_statement_block($linenr, $remain, $off);
1662 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1663 push(@chunks, [ $condition, $statement ]);
1664 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1665 return ($level, $linenr, @chunks);
1666 }
1667
1668 # Pull in the following conditional/block pairs and see if they
1669 # could continue the statement.
1670 for (;;) {
1671 ($statement, $condition, $linenr, $remain, $off, $level) =
1672 ctx_statement_block($linenr, $remain, $off);
1673 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1674 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1675 #print "C: push\n";
1676 push(@chunks, [ $condition, $statement ]);
1677 }
1678
1679 return ($level, $linenr, @chunks);
1680}
1681
1682sub ctx_block_get {
1683 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1684 my $line;
1685 my $start = $linenr - 1;
1686 my $blk = '';
1687 my @o;
1688 my @c;
1689 my @res = ();
1690
1691 my $level = 0;
1692 my @stack = ($level);
1693 for ($line = $start; $remain > 0; $line++) {
1694 next if ($rawlines[$line] =~ /^-/);
1695 $remain--;
1696
1697 $blk .= $rawlines[$line];
1698
1699 # Handle nested #if/#else.
1700 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1701 push(@stack, $level);
Julius Werner218906d2021-03-25 21:12:49 -07001702 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/ && $#stack > 0) {
1703 $level = $stack[$#stack];
1704 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/ && $#stack > 0) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001705 $level = pop(@stack);
1706 }
1707
1708 foreach my $c (split(//, $lines[$line])) {
1709 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1710 if ($off > 0) {
1711 $off--;
1712 next;
1713 }
1714
1715 if ($c eq $close && $level > 0) {
1716 $level--;
1717 last if ($level == 0);
1718 } elsif ($c eq $open) {
1719 $level++;
1720 }
1721 }
1722
1723 if (!$outer || $level <= 1) {
1724 push(@res, $rawlines[$line]);
1725 }
1726
1727 last if ($level == 0);
1728 }
1729
1730 return ($level, @res);
1731}
1732sub ctx_block_outer {
1733 my ($linenr, $remain) = @_;
1734
1735 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1736 return @r;
1737}
1738sub ctx_block {
1739 my ($linenr, $remain) = @_;
1740
1741 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1742 return @r;
1743}
1744sub ctx_statement {
1745 my ($linenr, $remain, $off) = @_;
1746
1747 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1748 return @r;
1749}
1750sub ctx_block_level {
1751 my ($linenr, $remain) = @_;
1752
1753 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1754}
1755sub ctx_statement_level {
1756 my ($linenr, $remain, $off) = @_;
1757
1758 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1759}
1760
1761sub ctx_locate_comment {
1762 my ($first_line, $end_line) = @_;
1763
Elyes Haouas26e0b942022-05-22 11:11:11 +02001764 # If c99 comment on the current line, or the line before or after
1765 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1766 return $current_comment if (defined $current_comment);
1767 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1768 return $current_comment if (defined $current_comment);
1769 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1770 return $current_comment if (defined $current_comment);
1771
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001772 # Catch a comment on the end of the line itself.
Elyes Haouas26e0b942022-05-22 11:11:11 +02001773 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001774 return $current_comment if (defined $current_comment);
1775
1776 # Look through the context and try and figure out if there is a
1777 # comment.
1778 my $in_comment = 0;
1779 $current_comment = '';
1780 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1781 my $line = $rawlines[$linenr - 1];
1782 #warn " $line\n";
1783 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1784 $in_comment = 1;
1785 }
1786 if ($line =~ m@/\*@) {
1787 $in_comment = 1;
1788 }
1789 if (!$in_comment && $current_comment ne '') {
1790 $current_comment = '';
1791 }
1792 $current_comment .= $line . "\n" if ($in_comment);
1793 if ($line =~ m@\*/@) {
1794 $in_comment = 0;
1795 }
1796 }
1797
1798 chomp($current_comment);
1799 return($current_comment);
1800}
1801sub ctx_has_comment {
1802 my ($first_line, $end_line) = @_;
1803 my $cmt = ctx_locate_comment($first_line, $end_line);
1804
1805 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1806 ##print "CMMT: $cmt\n";
1807
1808 return ($cmt ne '');
1809}
1810
1811sub raw_line {
1812 my ($linenr, $cnt) = @_;
1813
1814 my $offset = $linenr - 1;
1815 $cnt++;
1816
1817 my $line;
1818 while ($cnt) {
1819 $line = $rawlines[$offset++];
1820 next if (defined($line) && $line =~ /^-/);
1821 $cnt--;
1822 }
1823
Martin Roth96a48f12016-08-29 15:30:23 -06001824 # coreboot: This probably shouldn't happen, but it does.
1825 # Return a defined value so we don't get an error.
1826 if (defined $line) {
1827 return $line;
1828 } else {
1829 return "";
1830 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001831}
1832
Martin Roth60915b32018-08-10 21:04:05 -06001833sub get_stat_real {
1834 my ($linenr, $lc) = @_;
1835
1836 my $stat_real = raw_line($linenr, 0);
1837 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1838 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1839 }
1840
1841 return $stat_real;
1842}
1843
1844sub get_stat_here {
1845 my ($linenr, $cnt, $here) = @_;
1846
1847 my $herectx = $here . "\n";
1848 for (my $n = 0; $n < $cnt; $n++) {
1849 $herectx .= raw_line($linenr, $n) . "\n";
1850 }
1851
1852 return $herectx;
1853}
1854
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001855sub cat_vet {
1856 my ($vet) = @_;
1857 my ($res, $coded);
1858
1859 $res = '';
1860 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1861 $res .= $1;
1862 if ($2 ne '') {
1863 $coded = sprintf("^%c", unpack('C', $2) + 64);
1864 $res .= $coded;
1865 }
1866 }
1867 $res =~ s/$/\$/;
1868
1869 return $res;
1870}
1871
1872my $av_preprocessor = 0;
1873my $av_pending;
1874my @av_paren_type;
1875my $av_pend_colon;
1876
1877sub annotate_reset {
1878 $av_preprocessor = 0;
1879 $av_pending = '_';
1880 @av_paren_type = ('E');
1881 $av_pend_colon = 'O';
1882}
1883
1884sub annotate_values {
1885 my ($stream, $type) = @_;
1886
1887 my $res;
1888 my $var = '_' x length($stream);
1889 my $cur = $stream;
1890
1891 print "$stream\n" if ($dbg_values > 1);
1892
1893 while (length($cur)) {
1894 @av_paren_type = ('E') if ($#av_paren_type < 0);
1895 print " <" . join('', @av_paren_type) .
1896 "> <$type> <$av_pending>" if ($dbg_values > 1);
1897 if ($cur =~ /^(\s+)/o) {
1898 print "WS($1)\n" if ($dbg_values > 1);
1899 if ($1 =~ /\n/ && $av_preprocessor) {
1900 $type = pop(@av_paren_type);
1901 $av_preprocessor = 0;
1902 }
1903
1904 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1905 print "CAST($1)\n" if ($dbg_values > 1);
1906 push(@av_paren_type, $type);
1907 $type = 'c';
1908
1909 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1910 print "DECLARE($1)\n" if ($dbg_values > 1);
1911 $type = 'T';
1912
1913 } elsif ($cur =~ /^($Modifier)\s*/) {
1914 print "MODIFIER($1)\n" if ($dbg_values > 1);
1915 $type = 'T';
1916
1917 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1918 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1919 $av_preprocessor = 1;
1920 push(@av_paren_type, $type);
1921 if ($2 ne '') {
1922 $av_pending = 'N';
1923 }
1924 $type = 'E';
1925
1926 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1927 print "UNDEF($1)\n" if ($dbg_values > 1);
1928 $av_preprocessor = 1;
1929 push(@av_paren_type, $type);
1930
1931 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1932 print "PRE_START($1)\n" if ($dbg_values > 1);
1933 $av_preprocessor = 1;
1934
1935 push(@av_paren_type, $type);
1936 push(@av_paren_type, $type);
1937 $type = 'E';
1938
1939 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1940 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1941 $av_preprocessor = 1;
1942
1943 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1944
1945 $type = 'E';
1946
1947 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1948 print "PRE_END($1)\n" if ($dbg_values > 1);
1949
1950 $av_preprocessor = 1;
1951
1952 # Assume all arms of the conditional end as this
1953 # one does, and continue as if the #endif was not here.
1954 pop(@av_paren_type);
1955 push(@av_paren_type, $type);
1956 $type = 'E';
1957
1958 } elsif ($cur =~ /^(\\\n)/o) {
1959 print "PRECONT($1)\n" if ($dbg_values > 1);
1960
1961 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1962 print "ATTR($1)\n" if ($dbg_values > 1);
1963 $av_pending = $type;
1964 $type = 'N';
1965
1966 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1967 print "SIZEOF($1)\n" if ($dbg_values > 1);
1968 if (defined $2) {
1969 $av_pending = 'V';
1970 }
1971 $type = 'N';
1972
1973 } elsif ($cur =~ /^(if|while|for)\b/o) {
1974 print "COND($1)\n" if ($dbg_values > 1);
1975 $av_pending = 'E';
1976 $type = 'N';
1977
1978 } elsif ($cur =~/^(case)/o) {
1979 print "CASE($1)\n" if ($dbg_values > 1);
1980 $av_pend_colon = 'C';
1981 $type = 'N';
1982
1983 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1984 print "KEYWORD($1)\n" if ($dbg_values > 1);
1985 $type = 'N';
1986
1987 } elsif ($cur =~ /^(\()/o) {
1988 print "PAREN('$1')\n" if ($dbg_values > 1);
1989 push(@av_paren_type, $av_pending);
1990 $av_pending = '_';
1991 $type = 'N';
1992
1993 } elsif ($cur =~ /^(\))/o) {
1994 my $new_type = pop(@av_paren_type);
1995 if ($new_type ne '_') {
1996 $type = $new_type;
1997 print "PAREN('$1') -> $type\n"
1998 if ($dbg_values > 1);
1999 } else {
2000 print "PAREN('$1')\n" if ($dbg_values > 1);
2001 }
2002
2003 } elsif ($cur =~ /^($Ident)\s*\(/o) {
2004 print "FUNC($1)\n" if ($dbg_values > 1);
2005 $type = 'V';
2006 $av_pending = 'V';
2007
2008 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
2009 if (defined $2 && $type eq 'C' || $type eq 'T') {
2010 $av_pend_colon = 'B';
2011 } elsif ($type eq 'E') {
2012 $av_pend_colon = 'L';
2013 }
2014 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
2015 $type = 'V';
2016
2017 } elsif ($cur =~ /^($Ident|$Constant)/o) {
2018 print "IDENT($1)\n" if ($dbg_values > 1);
2019 $type = 'V';
2020
2021 } elsif ($cur =~ /^($Assignment)/o) {
2022 print "ASSIGN($1)\n" if ($dbg_values > 1);
2023 $type = 'N';
2024
2025 } elsif ($cur =~/^(;|{|})/) {
2026 print "END($1)\n" if ($dbg_values > 1);
2027 $type = 'E';
2028 $av_pend_colon = 'O';
2029
2030 } elsif ($cur =~/^(,)/) {
2031 print "COMMA($1)\n" if ($dbg_values > 1);
2032 $type = 'C';
2033
2034 } elsif ($cur =~ /^(\?)/o) {
2035 print "QUESTION($1)\n" if ($dbg_values > 1);
2036 $type = 'N';
2037
2038 } elsif ($cur =~ /^(:)/o) {
2039 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
2040
2041 substr($var, length($res), 1, $av_pend_colon);
2042 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
2043 $type = 'E';
2044 } else {
2045 $type = 'N';
2046 }
2047 $av_pend_colon = 'O';
2048
2049 } elsif ($cur =~ /^(\[)/o) {
2050 print "CLOSE($1)\n" if ($dbg_values > 1);
2051 $type = 'N';
2052
2053 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
2054 my $variant;
2055
2056 print "OPV($1)\n" if ($dbg_values > 1);
2057 if ($type eq 'V') {
2058 $variant = 'B';
2059 } else {
2060 $variant = 'U';
2061 }
2062
2063 substr($var, length($res), 1, $variant);
2064 $type = 'N';
2065
2066 } elsif ($cur =~ /^($Operators)/o) {
2067 print "OP($1)\n" if ($dbg_values > 1);
2068 if ($1 ne '++' && $1 ne '--') {
2069 $type = 'N';
2070 }
2071
2072 } elsif ($cur =~ /(^.)/o) {
2073 print "C($1)\n" if ($dbg_values > 1);
2074 }
2075 if (defined $1) {
2076 $cur = substr($cur, length($1));
2077 $res .= $type x length($1);
2078 }
2079 }
2080
2081 return ($res, $var);
2082}
2083
2084sub possible {
2085 my ($possible, $line) = @_;
2086 my $notPermitted = qr{(?:
2087 ^(?:
2088 $Modifier|
2089 $Storage|
2090 $Type|
2091 DEFINE_\S+
2092 )$|
2093 ^(?:
2094 goto|
2095 return|
2096 case|
2097 else|
2098 asm|__asm__|
2099 do|
2100 \#|
2101 \#\#|
2102 )(?:\s|$)|
2103 ^(?:typedef|struct|enum)\b
2104 )}x;
2105 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2106 if ($possible !~ $notPermitted) {
2107 # Check for modifiers.
2108 $possible =~ s/\s*$Storage\s*//g;
2109 $possible =~ s/\s*$Sparse\s*//g;
2110 if ($possible =~ /^\s*$/) {
2111
2112 } elsif ($possible =~ /\s/) {
2113 $possible =~ s/\s*$Type\s*//g;
2114 for my $modifier (split(' ', $possible)) {
2115 if ($modifier !~ $notPermitted) {
2116 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002117 push(@modifierListFile, $modifier);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002118 }
2119 }
2120
2121 } else {
2122 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002123 push(@typeListFile, $possible);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002124 }
2125 build_types();
2126 } else {
2127 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
2128 }
2129}
2130
2131my $prefix = '';
2132
2133sub show_type {
2134 my ($type) = @_;
2135
Martin Rothedd591d2017-03-14 10:16:29 -06002136 $type =~ tr/[a-z]/[A-Z]/;
2137
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002138 return defined $use_type{$type} if (scalar keys %use_type > 0);
2139
2140 return !defined $ignore_type{$type};
2141}
2142
2143sub report {
2144 my ($level, $type, $msg) = @_;
2145
2146 if (!show_type($type) ||
2147 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2148 return 0;
2149 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002150 my $output = '';
Martin Roth387dec82017-09-17 19:20:46 -06002151 if ($color) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002152 if ($level eq 'ERROR') {
2153 $output .= RED;
2154 } elsif ($level eq 'WARNING') {
2155 $output .= YELLOW;
2156 } else {
2157 $output .= GREEN;
2158 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002159 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002160 $output .= $prefix . $level . ':';
2161 if ($show_types) {
Martin Roth387dec82017-09-17 19:20:46 -06002162 $output .= BLUE if ($color);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002163 $output .= "$type:";
2164 }
Martin Roth387dec82017-09-17 19:20:46 -06002165 $output .= RESET if ($color);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002166 $output .= ' ' . $msg . "\n";
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002167
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002168 if ($showfile) {
2169 my @lines = split("\n", $output, -1);
2170 splice(@lines, 1, 1);
2171 $output = join("\n", @lines);
2172 }
Elyes Haouas46b07e52022-04-07 10:07:34 +02002173
2174 if ($terse) {
2175 $output = (split('\n', $output))[0] . "\n";
2176 }
2177
2178 if ($verbose && exists($verbose_messages{$type}) &&
2179 !exists($verbose_emitted{$type})) {
2180 $output .= $verbose_messages{$type} . "\n\n";
2181 $verbose_emitted{$type} = 1;
2182 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002183
2184 push(our @report, $output);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002185
2186 return 1;
2187}
2188
2189sub report_dump {
2190 our @report;
2191}
2192
2193sub fixup_current_range {
2194 my ($lineRef, $offset, $length) = @_;
2195
2196 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2197 my $o = $1;
2198 my $l = $2;
2199 my $no = $o + $offset;
2200 my $nl = $l + $length;
2201 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2202 }
2203}
2204
2205sub fix_inserted_deleted_lines {
2206 my ($linesRef, $insertedRef, $deletedRef) = @_;
2207
2208 my $range_last_linenr = 0;
2209 my $delta_offset = 0;
2210
2211 my $old_linenr = 0;
2212 my $new_linenr = 0;
2213
2214 my $next_insert = 0;
2215 my $next_delete = 0;
2216
2217 my @lines = ();
2218
2219 my $inserted = @{$insertedRef}[$next_insert++];
2220 my $deleted = @{$deletedRef}[$next_delete++];
2221
2222 foreach my $old_line (@{$linesRef}) {
2223 my $save_line = 1;
2224 my $line = $old_line; #don't modify the array
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002225 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002226 $delta_offset = 0;
2227 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2228 $range_last_linenr = $new_linenr;
2229 fixup_current_range(\$line, $delta_offset, 0);
2230 }
2231
2232 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2233 $deleted = @{$deletedRef}[$next_delete++];
2234 $save_line = 0;
2235 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2236 }
2237
2238 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2239 push(@lines, ${$inserted}{'LINE'});
2240 $inserted = @{$insertedRef}[$next_insert++];
2241 $new_linenr++;
2242 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2243 }
2244
2245 if ($save_line) {
2246 push(@lines, $line);
2247 $new_linenr++;
2248 }
2249
2250 $old_linenr++;
2251 }
2252
2253 return @lines;
2254}
2255
2256sub fix_insert_line {
2257 my ($linenr, $line) = @_;
2258
2259 my $inserted = {
2260 LINENR => $linenr,
2261 LINE => $line,
2262 };
2263 push(@fixed_inserted, $inserted);
2264}
2265
2266sub fix_delete_line {
2267 my ($linenr, $line) = @_;
2268
2269 my $deleted = {
2270 LINENR => $linenr,
2271 LINE => $line,
2272 };
2273
2274 push(@fixed_deleted, $deleted);
2275}
2276
2277sub ERROR {
2278 my ($type, $msg) = @_;
2279
2280 if (report("ERROR", $type, $msg)) {
2281 our $clean = 0;
2282 our $cnt_error++;
2283 return 1;
2284 }
2285 return 0;
2286}
2287sub WARN {
2288 my ($type, $msg) = @_;
2289
2290 if (report("WARNING", $type, $msg)) {
2291 our $clean = 0;
2292 our $cnt_warn++;
2293 return 1;
2294 }
2295 return 0;
2296}
2297sub CHK {
2298 my ($type, $msg) = @_;
2299
2300 if ($check && report("CHECK", $type, $msg)) {
2301 our $clean = 0;
2302 our $cnt_chk++;
2303 return 1;
2304 }
2305 return 0;
2306}
2307
2308sub check_absolute_file {
2309 my ($absolute, $herecurr) = @_;
2310 my $file = $absolute;
2311
2312 ##print "absolute<$absolute>\n";
2313
2314 # See if any suffix of this path is a path within the tree.
2315 while ($file =~ s@^[^/]*/@@) {
2316 if (-f "$root/$file") {
2317 ##print "file<$file>\n";
2318 last;
2319 }
2320 }
2321 if (! -f _) {
2322 return 0;
2323 }
2324
2325 # It is, so see if the prefix is acceptable.
2326 my $prefix = $absolute;
2327 substr($prefix, -length($file)) = '';
2328
2329 ##print "prefix<$prefix>\n";
2330 if ($prefix ne ".../") {
2331 WARN("USE_RELATIVE_PATH",
2332 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2333 }
2334}
2335
2336sub trim {
2337 my ($string) = @_;
2338
2339 $string =~ s/^\s+|\s+$//g;
2340
2341 return $string;
2342}
2343
2344sub ltrim {
2345 my ($string) = @_;
2346
2347 $string =~ s/^\s+//;
2348
2349 return $string;
2350}
2351
2352sub rtrim {
2353 my ($string) = @_;
2354
2355 $string =~ s/\s+$//;
2356
2357 return $string;
2358}
2359
2360sub string_find_replace {
2361 my ($string, $find, $replace) = @_;
2362
2363 $string =~ s/$find/$replace/g;
2364
2365 return $string;
2366}
2367
2368sub tabify {
2369 my ($leading) = @_;
2370
Elyes HAOUAS96771bf2022-01-29 07:28:45 +01002371 my $source_indent = $tabsize;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002372 my $max_spaces_before_tab = $source_indent - 1;
2373 my $spaces_to_tab = " " x $source_indent;
2374
2375 #convert leading spaces to tabs
2376 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2377 #Remove spaces before a tab
2378 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2379
2380 return "$leading";
2381}
2382
2383sub pos_last_openparen {
2384 my ($line) = @_;
2385
2386 my $pos = 0;
2387
2388 my $opens = $line =~ tr/\(/\(/;
2389 my $closes = $line =~ tr/\)/\)/;
2390
2391 my $last_openparen = 0;
2392
2393 if (($opens == 0) || ($closes >= $opens)) {
2394 return -1;
2395 }
2396
2397 my $len = length($line);
2398
2399 for ($pos = 0; $pos < $len; $pos++) {
2400 my $string = substr($line, $pos);
2401 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2402 $pos += length($1) - 1;
2403 } elsif (substr($line, $pos, 1) eq '(') {
2404 $last_openparen = $pos;
2405 } elsif (index($string, '(') == -1) {
2406 last;
2407 }
2408 }
2409
2410 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2411}
2412
Elyes Haouasf62a9892022-04-12 12:17:50 +02002413sub get_raw_comment {
2414 my ($line, $rawline) = @_;
2415 my $comment = '';
2416
2417 for my $i (0 .. (length($line) - 1)) {
2418 if (substr($line, $i, 1) eq "$;") {
2419 $comment .= substr($rawline, $i, 1);
2420 }
2421 }
2422
2423 return $comment;
2424}
2425
2426sub exclude_global_initialisers {
2427 my ($realfile) = @_;
2428
2429 # Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
2430 return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ ||
2431 $realfile =~ m@^samples/bpf/.*_kern\.c$@ ||
2432 $realfile =~ m@/bpf/.*\.bpf\.c$@;
2433}
2434
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002435sub process {
2436 my $filename = shift;
2437
2438 my $linenr=0;
2439 my $prevline="";
2440 my $prevrawline="";
2441 my $stashline="";
2442 my $stashrawline="";
2443
2444 my $length;
2445 my $indent;
2446 my $previndent=0;
2447 my $stashindent=0;
2448
2449 our $clean = 1;
2450 my $signoff = 0;
Elyes Haouas6e84c2c2022-07-13 17:35:43 +02002451 my $author = '';
2452 my $authorsignoff = 0;
2453 my $author_sob = '';
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002454 my $is_patch = 0;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002455 my $in_header_lines = $file ? 0 : 1;
2456 my $in_commit_log = 0; #Scanning lines before patch
Elyes Haouascb346842022-07-13 17:05:19 +02002457 my $has_patch_separator = 0; #Found a --- line
Martin Rothedd591d2017-03-14 10:16:29 -06002458 my $has_commit_log = 0; #Encountered lines before patch
Elyes Haouase83e0902022-07-13 17:40:30 +02002459 my $commit_log_lines = 0; #Number of commit log lines
Martin Rothedd591d2017-03-14 10:16:29 -06002460 my $commit_log_possible_stack_dump = 0;
Elyes Haouas26e0b942022-05-22 11:11:11 +02002461 my $commit_log_long_line = 0;
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002462 my $commit_log_has_diff = 0;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002463 my $reported_maintainer_file = 0;
2464 my $non_utf8_charset = 0;
2465
2466 my $last_blank_line = 0;
2467 my $last_coalesced_string_linenr = -1;
2468
2469 our @report = ();
2470 our $cnt_lines = 0;
2471 our $cnt_error = 0;
2472 our $cnt_warn = 0;
2473 our $cnt_chk = 0;
2474
2475 # Trace the real file/line as we go.
2476 my $realfile = '';
2477 my $realline = 0;
2478 my $realcnt = 0;
2479 my $here = '';
Martin Rothedd591d2017-03-14 10:16:29 -06002480 my $context_function; #undef'd unless there's a known function
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002481 my $in_comment = 0;
2482 my $comment_edge = 0;
2483 my $first_line = 0;
2484 my $p1_prefix = '';
2485
2486 my $prev_values = 'E';
2487
2488 # suppression flags
2489 my %suppress_ifbraces;
2490 my %suppress_whiletrailers;
2491 my %suppress_export;
2492 my $suppress_statement = 0;
2493
2494 my %signatures = ();
2495
2496 # Pre-scan the patch sanitizing the lines.
2497 # Pre-scan the patch looking for any __setup documentation.
2498 #
2499 my @setup_docs = ();
2500 my $setup_docs = 0;
2501
2502 my $camelcase_file_seeded = 0;
2503
Martin Roth60915b32018-08-10 21:04:05 -06002504 my $checklicenseline = 1;
2505
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002506 sanitise_line_reset();
2507 my $line;
2508 foreach my $rawline (@rawlines) {
2509 $linenr++;
2510 $line = $rawline;
2511
2512 push(@fixed, $rawline) if ($fix);
2513
2514 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2515 $setup_docs = 0;
Martin Rothedd591d2017-03-14 10:16:29 -06002516 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002517 $setup_docs = 1;
2518 }
2519 #next;
2520 }
Martin Roth387dec82017-09-17 19:20:46 -06002521 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002522 $realline=$1-1;
2523 if (defined $2) {
2524 $realcnt=$3+1;
2525 } else {
2526 $realcnt=1+1;
2527 }
2528 $in_comment = 0;
2529
2530 # Guestimate if this is a continuing comment. Run
2531 # the context looking for a comment "edge". If this
2532 # edge is a close comment then we must be in a comment
2533 # at context start.
2534 my $edge;
2535 my $cnt = $realcnt;
2536 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2537 next if (defined $rawlines[$ln - 1] &&
2538 $rawlines[$ln - 1] =~ /^-/);
2539 $cnt--;
2540 #print "RAW<$rawlines[$ln - 1]>\n";
2541 last if (!defined $rawlines[$ln - 1]);
2542 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2543 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2544 ($edge) = $1;
2545 last;
2546 }
2547 }
2548 if (defined $edge && $edge eq '*/') {
2549 $in_comment = 1;
2550 }
2551
2552 # Guestimate if this is a continuing comment. If this
2553 # is the start of a diff block and this line starts
2554 # ' *' then it is very likely a comment.
2555 if (!defined $edge &&
2556 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2557 {
2558 $in_comment = 1;
2559 }
2560
2561 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2562 sanitise_line_reset($in_comment);
2563
2564 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2565 # Standardise the strings and chars within the input to
2566 # simplify matching -- only bother with positive lines.
2567 $line = sanitise_line($rawline);
2568 }
2569 push(@lines, $line);
2570
2571 if ($realcnt > 1) {
2572 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2573 } else {
2574 $realcnt = 0;
2575 }
2576
2577 #print "==>$rawline\n";
2578 #print "-->$line\n";
2579
2580 if ($setup_docs && $line =~ /^\+/) {
2581 push(@setup_docs, $line);
2582 }
2583 }
2584
2585 $prefix = '';
2586
2587 $realcnt = 0;
2588 $linenr = 0;
2589 $fixlinenr = -1;
2590 foreach my $line (@lines) {
2591 $linenr++;
2592 $fixlinenr++;
2593 my $sline = $line; #copy of $line
2594 $sline =~ s/$;/ /g; #with comments as spaces
2595
2596 my $rawline = $rawlines[$linenr - 1];
Elyes Haouasf62a9892022-04-12 12:17:50 +02002597 my $raw_comment = get_raw_comment($line, $rawline);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002598
Martin Roth60915b32018-08-10 21:04:05 -06002599# check if it's a mode change, rename or start of a patch
2600 if (!$in_commit_log &&
2601 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2602 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2603 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2604 $is_patch = 1;
2605 }
2606
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002607#extract the line range in the file after the patch is applied
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002608 if (!$in_commit_log &&
Martin Roth387dec82017-09-17 19:20:46 -06002609 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2610 my $context = $4;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002611 $is_patch = 1;
2612 $first_line = $linenr + 1;
2613 $realline=$1-1;
2614 if (defined $2) {
2615 $realcnt=$3+1;
2616 } else {
2617 $realcnt=1+1;
2618 }
2619 annotate_reset();
2620 $prev_values = 'E';
2621
2622 %suppress_ifbraces = ();
2623 %suppress_whiletrailers = ();
2624 %suppress_export = ();
2625 $suppress_statement = 0;
Martin Roth387dec82017-09-17 19:20:46 -06002626 if ($context =~ /\b(\w+)\s*\(/) {
2627 $context_function = $1;
2628 } else {
2629 undef $context_function;
2630 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002631 next;
2632
2633# track the line number as we move through the hunk, note that
2634# new versions of GNU diff omit the leading space on completely
2635# blank context lines so we need to count that too.
2636 } elsif ($line =~ /^( |\+|$)/) {
2637 $realline++;
2638 $realcnt-- if ($realcnt != 0);
2639
2640 # Measure the line length and indent.
2641 ($length, $indent) = line_stats($rawline);
2642
2643 # Track the previous line.
2644 ($prevline, $stashline) = ($stashline, $line);
2645 ($previndent, $stashindent) = ($stashindent, $indent);
2646 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2647
2648 #warn "line<$line>\n";
2649
2650 } elsif ($realcnt == 1) {
2651 $realcnt--;
2652 }
2653
2654 my $hunk_line = ($realcnt != 0);
2655
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002656 $here = "#$linenr: " if (!$file);
2657 $here = "#$realline: " if ($file);
2658
2659 my $found_file = 0;
2660 # extract the filename as it passes
2661 if ($line =~ /^diff --git.*?(\S+)$/) {
2662 $realfile = $1;
2663 $realfile =~ s@^([^/]*)/@@ if (!$file);
2664 $in_commit_log = 0;
2665 $found_file = 1;
2666 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2667 $realfile = $1;
2668 $realfile =~ s@^([^/]*)/@@ if (!$file);
2669 $in_commit_log = 0;
2670
2671 $p1_prefix = $1;
2672 if (!$file && $tree && $p1_prefix ne '' &&
2673 -e "$root/$p1_prefix") {
2674 WARN("PATCH_PREFIX",
2675 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2676 }
2677
2678 if ($realfile =~ m@^include/asm/@) {
2679 ERROR("MODIFIED_INCLUDE_ASM",
2680 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2681 }
2682 $found_file = 1;
2683 }
2684
Martin Rothedd591d2017-03-14 10:16:29 -06002685 # coreboot
Martin Rotha3cac872017-03-04 18:17:35 -07002686 my $skipme = 0;
2687 foreach (@exclude) {
2688 if ($realfile =~ m@^(?:$_/)@) {
2689 $skipme = 1;
2690 }
2691 }
2692 if ($skipme) {
2693 next;
2694 }
2695
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002696#make up the handle for any error we report on this line
2697 if ($showfile) {
2698 $prefix = "$realfile:$realline: "
2699 } elsif ($emacs) {
2700 if ($file) {
2701 $prefix = "$filename:$realline: ";
2702 } else {
2703 $prefix = "$filename:$linenr: ";
2704 }
2705 }
2706
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002707 if ($found_file) {
Martin Rothedd591d2017-03-14 10:16:29 -06002708 if (is_maintained_obsolete($realfile)) {
2709 WARN("OBSOLETE",
2710 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2711 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002712 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002713 $check = 1;
2714 } else {
2715 $check = $check_orig;
2716 }
Martin Roth60915b32018-08-10 21:04:05 -06002717 $checklicenseline = 1;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002718 next;
2719 }
2720
2721 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2722
2723 my $hereline = "$here\n$rawline\n";
2724 my $herecurr = "$here\n$rawline\n";
2725 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2726
2727 $cnt_lines++ if ($realcnt != 0);
2728
Elyes Haouase83e0902022-07-13 17:40:30 +02002729# Verify the existence of a commit log if appropriate
2730# 2 is used because a $signature is counted in $commit_log_lines
2731 if ($in_commit_log) {
2732 if ($line !~ /^\s*$/) {
2733 $commit_log_lines++; #could be a $signature
2734 }
2735 } elsif ($has_commit_log && $commit_log_lines < 2) {
2736 WARN("COMMIT_MESSAGE",
2737 "Missing commit description - Add an appropriate one\n");
2738 $commit_log_lines = 2; #warn only once
2739 }
2740
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002741# Check if the commit log has what seems like a diff which can confuse patch
2742 if ($in_commit_log && !$commit_log_has_diff &&
Elyes Haouas26e0b942022-05-22 11:11:11 +02002743 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2744 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002745 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2746 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2747 ERROR("DIFF_IN_COMMIT_MSG",
2748 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2749 $commit_log_has_diff = 1;
2750 }
2751
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002752# Check for incorrect file permissions
2753 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2754 my $permhere = $here . "FILE: $realfile\n";
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002755 if ($realfile !~ m@scripts/@ &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002756 $realfile !~ /\.(py|pl|awk|sh)$/) {
2757 ERROR("EXECUTE_PERMISSIONS",
2758 "do not set execute permissions for source files\n" . $permhere);
2759 }
2760 }
2761
2762# Check the patch for a signoff:
Elyes Haouas6e84c2c2022-07-13 17:35:43 +02002763 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002764 $signoff++;
2765 $in_commit_log = 0;
Elyes Haouas6e84c2c2022-07-13 17:35:43 +02002766 if ($author ne '' && $authorsignoff != 1) {
2767 if (same_email_addresses($1, $author)) {
2768 $authorsignoff = 1;
2769 } else {
2770 my $ctx = $1;
2771 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
2772 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
2773
2774 if (lc $email_address eq lc $author_address && $email_name eq $author_name) {
2775 $author_sob = $ctx;
2776 $authorsignoff = 2;
2777 } elsif (lc $email_address eq lc $author_address) {
2778 $author_sob = $ctx;
2779 $authorsignoff = 3;
2780 } elsif ($email_name eq $author_name) {
2781 $author_sob = $ctx;
2782 $authorsignoff = 4;
2783
2784 my $address1 = $email_address;
2785 my $address2 = $author_address;
2786
2787 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
2788 $address1 = "$1$2";
2789 }
2790 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
2791 $address2 = "$1$2";
2792 }
2793 if ($address1 eq $address2) {
2794 $authorsignoff = 5;
2795 }
2796 }
2797 }
2798 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002799 }
2800
Elyes Haouascb346842022-07-13 17:05:19 +02002801# Check for patch separator
2802 if ($line =~ /^---$/) {
2803 $has_patch_separator = 1;
2804 $in_commit_log = 0;
2805 }
2806
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002807# Check if MAINTAINERS is being updated. If so, there's probably no need to
2808# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2809 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2810 $reported_maintainer_file = 1;
2811 }
2812
2813# Check signature styles
2814 if (!$in_header_lines &&
2815 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2816 my $space_before = $1;
2817 my $sign_off = $2;
2818 my $space_after = $3;
2819 my $email = $4;
2820 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2821
2822 if ($sign_off !~ /$signature_tags/) {
2823 WARN("BAD_SIGN_OFF",
2824 "Non-standard signature: $sign_off\n" . $herecurr);
2825 }
2826 if (defined $space_before && $space_before ne "") {
2827 if (WARN("BAD_SIGN_OFF",
2828 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2829 $fix) {
2830 $fixed[$fixlinenr] =
2831 "$ucfirst_sign_off $email";
2832 }
2833 }
2834 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2835 if (WARN("BAD_SIGN_OFF",
2836 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2837 $fix) {
2838 $fixed[$fixlinenr] =
2839 "$ucfirst_sign_off $email";
2840 }
2841
2842 }
2843 if (!defined $space_after || $space_after ne " ") {
2844 if (WARN("BAD_SIGN_OFF",
2845 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2846 $fix) {
2847 $fixed[$fixlinenr] =
2848 "$ucfirst_sign_off $email";
2849 }
2850 }
2851
2852 my ($email_name, $email_address, $comment) = parse_email($email);
2853 my $suggested_email = format_email(($email_name, $email_address));
2854 if ($suggested_email eq "") {
2855 ERROR("BAD_SIGN_OFF",
2856 "Unrecognized email address: '$email'\n" . $herecurr);
2857 } else {
2858 my $dequoted = $suggested_email;
2859 $dequoted =~ s/^"//;
2860 $dequoted =~ s/" </ </;
2861 # Don't force email to have quotes
2862 # Allow just an angle bracketed address
2863 if ("$dequoted$comment" ne $email &&
2864 "<$email_address>$comment" ne $email &&
2865 "$suggested_email$comment" ne $email) {
2866 WARN("BAD_SIGN_OFF",
2867 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2868 }
2869 }
2870
2871# Check for duplicate signatures
2872 my $sig_nospace = $line;
2873 $sig_nospace =~ s/\s//g;
2874 $sig_nospace = lc($sig_nospace);
2875 if (defined $signatures{$sig_nospace}) {
2876 WARN("BAD_SIGN_OFF",
2877 "Duplicate signature\n" . $herecurr);
2878 } else {
2879 $signatures{$sig_nospace} = 1;
2880 }
2881 }
2882
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002883# Check email subject for common tools that don't need to be mentioned
2884 if ($in_header_lines &&
2885 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2886 WARN("EMAIL_SUBJECT",
2887 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2888 }
2889
Elyes Haouascb346842022-07-13 17:05:19 +02002890# Check for Gerrit Change-Ids not in any patch context
2891 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
2892 if (ERROR("GERRIT_CHANGE_ID",
2893 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
2894 $fix) {
2895 fix_delete_line($fixlinenr, $rawline);
2896 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002897 }
2898
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002899# Check if the commit log is in a possible stack dump
2900 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2901 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2902 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2903 # timestamp
Elyes Haouas26e0b942022-05-22 11:11:11 +02002904 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2905 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2906 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2907 # stack dump address styles
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002908 $commit_log_possible_stack_dump = 1;
2909 }
2910
Martin Roth10d34b72022-03-22 16:24:44 -06002911# coreboot: The line length limit is 72
2912# Check for line lengths > 72 in commit log
2913 if ($in_commit_log && length($line) > 72 &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002914 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2915 # file delta changes
Elyes HAOUASac690492022-01-29 07:59:17 +01002916 $line =~ /^\s*(?:[\w\.\-\+]*\/)++[\w\.\-\+]+:/ ||
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002917 # filename then :
Elyes HAOUASac690492022-01-29 07:59:17 +01002918 $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
2919 # A Fixes: or Link: line or signature tag line
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002920 $commit_log_possible_stack_dump)) {
2921 WARN("COMMIT_LOG_LONG_LINE",
Paul Menzeldd1ee272021-12-09 12:49:35 +01002922 "Possible unwrapped commit description (prefer a maximum 72 chars per line)\n" . $herecurr);
Elyes Haouas26e0b942022-05-22 11:11:11 +02002923 $commit_log_long_line = 1;
Martin Roth10d34b72022-03-22 16:24:44 -06002924 }
2925
2926# coreboot: The line subject limit is 65
2927# Check for line lengths > 65 in commit subject
Martin Roth97e7eea2022-03-22 18:06:15 -06002928# coreboot: Check for a period at the end of the subject line
Martin Roth10d34b72022-03-22 16:24:44 -06002929 if ($in_header_lines &&
2930 $line =~ /^Subject: /) {
2931 $line = $line.$rawlines[$linenr];
2932 $line =~ s/^Subject: \[PATCH\] //;
Martin Roth341a53d2022-05-24 18:46:45 -06002933 if (length($line) - length("Subject: [PATCH] ") > 65) {
2934 WARN("COMMIT_LOG_LONG_LINE",
2935 "Possible long commit subject (prefer a maximum 65 characters)\n" . $herecurr);
Martin Roth10d34b72022-03-22 16:24:44 -06002936 }
Martin Roth97e7eea2022-03-22 18:06:15 -06002937 if ($line =~ /\.$/) {
2938 WARN("SUBJECT_HAS_FULL_STOP",
2939 "Subject line should not end with a period.\n" . $herecurr);
2940 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002941 }
2942
2943# Reset possible stack dump if a blank line is found
2944 if ($in_commit_log && $commit_log_possible_stack_dump &&
2945 $line =~ /^\s*$/) {
2946 $commit_log_possible_stack_dump = 0;
2947 }
2948
2949# Check for git id commit length and improperly formed commit descriptions
2950 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Martin Rothedd591d2017-03-14 10:16:29 -06002951 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Martin Roth387dec82017-09-17 19:20:46 -06002952 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002953 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Martin Rothedd591d2017-03-14 10:16:29 -06002954 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07002955 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2956 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2957 my $init_char = "c";
2958 my $orig_commit = "";
2959 my $short = 1;
2960 my $long = 0;
2961 my $case = 1;
2962 my $space = 1;
2963 my $hasdesc = 0;
2964 my $hasparens = 0;
2965 my $id = '0123456789ab';
2966 my $orig_desc = "commit description";
2967 my $description = "";
2968
2969 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2970 $init_char = $1;
2971 $orig_commit = lc($2);
2972 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2973 $orig_commit = lc($1);
2974 }
2975
2976 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2977 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2978 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2979 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2980 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2981 $orig_desc = $1;
2982 $hasparens = 1;
2983 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2984 defined $rawlines[$linenr] &&
2985 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2986 $orig_desc = $1;
2987 $hasparens = 1;
2988 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2989 defined $rawlines[$linenr] &&
2990 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2991 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2992 $orig_desc = $1;
2993 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2994 $orig_desc .= " " . $1;
2995 $hasparens = 1;
2996 }
2997
2998 ($id, $description) = git_commit_info($orig_commit,
2999 $id, $orig_desc);
3000
Martin Roth387dec82017-09-17 19:20:46 -06003001 if (defined($id) &&
3002 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003003 ERROR("GIT_COMMIT_ID",
3004 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
3005 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003006 }
3007
3008# Check for added, moved or deleted files
3009 if (!$reported_maintainer_file && !$in_commit_log &&
3010 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3011 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
3012 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
3013 (defined($1) || defined($2))))) {
Martin Rothedd591d2017-03-14 10:16:29 -06003014 $is_patch = 1;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003015 $reported_maintainer_file = 1;
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003016 WARN("FILE_PATH_CHANGES",
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003017 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3018 }
3019
3020# Check for wrappage within a valid hunk of the file
3021 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
3022 ERROR("CORRUPTED_PATCH",
3023 "patch seems to be corrupt (line wrapped?)\n" .
3024 $herecurr) if (!$emitted_corrupt++);
3025 }
3026
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003027# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3028 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
3029 $rawline !~ m/^$UTF8*$/) {
3030 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3031
3032 my $blank = copy_spacing($rawline);
3033 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3034 my $hereptr = "$hereline$ptr\n";
3035
3036 CHK("INVALID_UTF8",
3037 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
3038 }
3039
3040# Check if it's the start of a commit log
3041# (not a header line and we haven't seen the patch filename)
3042 if ($in_header_lines && $realfile =~ /^$/ &&
Martin Roth387dec82017-09-17 19:20:46 -06003043 !($rawline =~ /^\s+(?:\S|$)/ ||
3044 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003045 $in_header_lines = 0;
3046 $in_commit_log = 1;
Martin Rothedd591d2017-03-14 10:16:29 -06003047 $has_commit_log = 1;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003048 }
3049
3050# Check if there is UTF-8 in a commit log when a mail header has explicitly
3051# declined it, i.e defined some charset where it is missing.
3052 if ($in_header_lines &&
3053 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3054 $1 !~ /utf-8/i) {
3055 $non_utf8_charset = 1;
3056 }
3057
3058 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
3059 $rawline =~ /$NON_ASCII_UTF8/) {
3060 WARN("UTF8_BEFORE_PATCH",
3061 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3062 }
3063
Martin Rothedd591d2017-03-14 10:16:29 -06003064# Check for absolute kernel paths in commit message
3065 if ($tree && $in_commit_log) {
3066 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3067 my $file = $1;
3068
3069 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3070 check_absolute_file($1, $herecurr)) {
3071 #
3072 } else {
3073 check_absolute_file($file, $herecurr);
3074 }
3075 }
3076 }
3077
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003078# Check for various typo / spelling mistakes
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003079 if (defined($misspellings) &&
3080 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Elyes Haouas26e0b942022-05-22 11:11:11 +02003081 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003082 my $typo = $1;
Elyes Haouas26e0b942022-05-22 11:11:11 +02003083 my $blank = copy_spacing($rawline);
3084 my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
3085 my $hereptr = "$hereline$ptr\n";
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003086 my $typo_fix = $spelling_fix{lc($typo)};
3087 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3088 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Martin Roth387dec82017-09-17 19:20:46 -06003089 my $msg_level = \&WARN;
3090 $msg_level = \&CHK if ($file);
3091 if (&{$msg_level}("TYPO_SPELLING",
Elyes Haouas26e0b942022-05-22 11:11:11 +02003092 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003093 $fix) {
3094 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3095 }
3096 }
3097 }
Elyes Haouas84083a22022-04-12 12:28:32 +02003098# check for repeated words separated by a single space
3099# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3100 if (($rawline =~ /^\+/ || $in_commit_log) &&
3101 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
3102 pos($rawline) = 1 if (!$in_commit_log);
3103 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3104
3105 my $first = $1;
3106 my $second = $2;
3107 my $start_pos = $-[1];
3108 my $end_pos = $+[2];
3109 if ($first =~ /(?:struct|union|enum)/) {
3110 pos($rawline) += length($first) + length($second) + 1;
3111 next;
3112 }
3113
3114 next if (lc($first) ne lc($second));
3115 next if ($first eq 'long');
3116
3117 # check for character before and after the word matches
3118 my $start_char = '';
3119 my $end_char = '';
3120 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
3121 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3122
3123 next if ($start_char =~ /^\S$/);
3124 next if (index(" \t.,;?!", $end_char) == -1);
3125
3126 # avoid repeating hex occurrences like 'ff ff fe 09 ...'
3127 if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3128 next if (!exists($allow_repeated_words{lc($first)}));
3129 }
3130
3131 if (WARN("REPEATED_WORD",
3132 "Possible repeated word: '$first'\n" . $herecurr) &&
3133 $fix) {
3134 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3135 }
3136 }
3137
3138 # if it's a repeated word on consecutive lines in a comment block
3139 if ($prevline =~ /$;+\s*$/ &&
3140 $prevrawline =~ /($word_pattern)\s*$/) {
3141 my $last_word = $1;
3142 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3143 if (WARN("REPEATED_WORD",
3144 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3145 $fix) {
3146 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3147 }
3148 }
3149 }
3150 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003151
3152# ignore non-hunk lines and lines being removed
3153 next if (!$hunk_line || $line =~ /^-/);
3154
3155#trailing whitespace
3156 if ($line =~ /^\+.*\015/) {
3157 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3158 if (ERROR("DOS_LINE_ENDINGS",
3159 "DOS line endings\n" . $herevet) &&
3160 $fix) {
3161 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
3162 }
3163 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3164 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3165 if (ERROR("TRAILING_WHITESPACE",
3166 "trailing whitespace\n" . $herevet) &&
3167 $fix) {
3168 $fixed[$fixlinenr] =~ s/\s+$//;
3169 }
3170
3171 $rpt_cleaners = 1;
3172 }
3173
3174# Check for FSF mailing addresses.
3175 if ($rawline =~ /\bwrite to the Free/i ||
Martin Rothedd591d2017-03-14 10:16:29 -06003176 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003177 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3178 $rawline =~ /\b51\s+Franklin\s+St/i) {
3179 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Martin Roth387dec82017-09-17 19:20:46 -06003180 my $msg_level = \&ERROR;
3181 $msg_level = \&CHK if ($file);
3182 &{$msg_level}("FSF_MAILING_ADDRESS",
3183 "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003184 }
3185
3186# check for Kconfig help text having a real description
3187# Only applies when adding the entry originally, after that we do not have
3188# sufficient context to determine whether it is indeed long enough.
3189 if ($realfile =~ /Kconfig/ &&
Martin Roth60915b32018-08-10 21:04:05 -06003190 # 'choice' is usually the last thing on the line (though
3191 # Kconfig supports named choices), so use a word boundary
3192 # (\b) rather than a whitespace character (\s)
3193 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Elyes Haouasf62a9892022-04-12 12:17:50 +02003194 my $ln = $linenr;
3195 my $needs_help = 0;
3196 my $has_help = 0;
3197 my $help_length = 0;
3198 while (defined $lines[$ln]) {
3199 my $f = $lines[$ln++];
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003200
3201 next if ($f =~ /^-/);
Elyes Haouasf62a9892022-04-12 12:17:50 +02003202 last if ($f !~ /^[\+ ]/); # !patch context
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003203
Elyes Haouasf62a9892022-04-12 12:17:50 +02003204 if ($f =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3205 $needs_help = 1;
3206 next;
3207 }
3208 if ($f =~ /^\+\s*help\s*$/) {
3209 $has_help = 1;
3210 next;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003211 }
3212
Elyes Haouasf62a9892022-04-12 12:17:50 +02003213 $f =~ s/^.//; # strip patch context [+ ]
3214 $f =~ s/#.*//; # strip # directives
3215 $f =~ s/^\s+//; # strip leading blanks
3216 next if ($f =~ /^$/); # skip blank lines
Martin Roth60915b32018-08-10 21:04:05 -06003217
Elyes Haouasf62a9892022-04-12 12:17:50 +02003218 # At the end of this Kconfig block:
Martin Roth60915b32018-08-10 21:04:05 -06003219 # This only checks context lines in the patch
3220 # and so hopefully shouldn't trigger false
3221 # positives, even though some of these are
3222 # common words in help texts
Elyes Haouasf62a9892022-04-12 12:17:50 +02003223 if ($f =~ /^(?:config|menuconfig|choice|endchoice|
3224 if|endif|menu|endmenu|source)\b/x) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003225 last;
3226 }
Elyes Haouasf62a9892022-04-12 12:17:50 +02003227 $help_length++ if ($has_help);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003228 }
Elyes Haouasf62a9892022-04-12 12:17:50 +02003229 if ($needs_help &&
3230 $help_length < $min_conf_desc_length) {
3231 my $stat_real = get_stat_real($linenr, $ln - 1);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003232 WARN("CONFIG_DESCRIPTION",
Elyes Haouasf62a9892022-04-12 12:17:50 +02003233 "please write a help paragraph that fully describes the config symbol\n" . "$here\n$stat_real\n");
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003234 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003235 }
3236
Martin Roth387dec82017-09-17 19:20:46 -06003237# check for MAINTAINERS entries that don't have the right form
3238 if ($realfile =~ /^MAINTAINERS$/ &&
3239 $rawline =~ /^\+[A-Z]:/ &&
3240 $rawline !~ /^\+[A-Z]:\t\S/) {
3241 if (WARN("MAINTAINERS_STYLE",
3242 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3243 $fix) {
3244 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3245 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003246 }
3247
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003248# discourage the use of boolean for type definition attributes of Kconfig options
3249 if ($realfile =~ /Kconfig/ &&
3250 $line =~ /^\+\s*\bboolean\b/) {
3251 WARN("CONFIG_TYPE_BOOLEAN",
3252 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3253 }
3254
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003255 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3256 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3257 my $flag = $1;
3258 my $replacement = {
3259 'EXTRA_AFLAGS' => 'asflags-y',
3260 'EXTRA_CFLAGS' => 'ccflags-y',
3261 'EXTRA_CPPFLAGS' => 'cppflags-y',
3262 'EXTRA_LDFLAGS' => 'ldflags-y',
3263 };
3264
3265 WARN("DEPRECATED_VARIABLE",
3266 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3267 }
3268
3269# check for DT compatible documentation
3270 if (defined $root &&
3271 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3272 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3273
3274 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3275
3276 my $dt_path = $root . "/Documentation/devicetree/bindings/";
3277 my $vp_file = $dt_path . "vendor-prefixes.txt";
3278
3279 foreach my $compat (@compats) {
3280 my $compat2 = $compat;
3281 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3282 my $compat3 = $compat;
3283 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3284 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3285 if ( $? >> 8 ) {
3286 WARN("UNDOCUMENTED_DT_STRING",
3287 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3288 }
3289
3290 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3291 my $vendor = $1;
3292 `grep -Eq "^$vendor\\b" $vp_file`;
3293 if ( $? >> 8 ) {
3294 WARN("UNDOCUMENTED_DT_STRING",
3295 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3296 }
3297 }
3298 }
3299
Martin Roth60915b32018-08-10 21:04:05 -06003300# check for using SPDX license tag at beginning of files
3301 if ($realline == $checklicenseline) {
3302 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3303 $checklicenseline = 2;
3304 } elsif ($rawline =~ /^\+/) {
3305 my $comment = "";
3306 if ($realfile =~ /\.(h|s|S)$/) {
3307 $comment = '/*';
3308 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3309 $comment = '//';
3310 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
3311 $comment = '#';
3312 } elsif ($realfile =~ /\.rst$/) {
3313 $comment = '..';
3314 }
3315
3316 if ($comment !~ /^$/ &&
3317 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
3318 WARN("SPDX_LICENSE_TAG",
3319 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
3320 }
3321 }
3322 }
3323
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003324# check we are in a valid source file if not then ignore this hunk
Martin Rothedd591d2017-03-14 10:16:29 -06003325 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003326
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003327# line length limit (with some exclusions)
3328#
3329# There are a few types of lines that may extend beyond $max_line_length:
3330# logging functions like pr_info that end in a string
3331# lines with a single string
3332# #defines that are a single string
Martin Roth60915b32018-08-10 21:04:05 -06003333# lines with an RFC3986 like URL
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003334#
3335# There are 3 different line length message types:
Martin Roth387dec82017-09-17 19:20:46 -06003336# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003337# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3338# LONG_LINE all other lines longer than $max_line_length
3339#
3340# if LONG_LINE is ignored, the other 2 types are also ignored
3341#
3342
Michael Niewöhner8eb7b352022-10-25 23:29:18 +02003343 $max_line_length += 1; # consider leading + in patches
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003344 if ($line =~ /^\+/ && $length > $max_line_length) {
3345 my $msg_type = "LONG_LINE";
3346
3347 # Check the allowed long line types first
3348
3349 # logging functions that end in a string that starts
3350 # before $max_line_length
3351 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3352 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3353 $msg_type = "";
3354
3355 # lines with only strings (w/ possible termination)
3356 # #defines with only strings
3357 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3358 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3359 $msg_type = "";
3360
Martin Roth60915b32018-08-10 21:04:05 -06003361 # More special cases
3362 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3363 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3364 $msg_type = "";
3365
3366 # URL ($rawline is used in case the URL is in a comment)
3367 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
Martin Rothedd591d2017-03-14 10:16:29 -06003368 $msg_type = "";
3369
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003370 # Otherwise set the alternate message types
3371
3372 # a comment starts before $max_line_length
3373 } elsif ($line =~ /($;[\s$;]*)$/ &&
3374 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3375 $msg_type = "LONG_LINE_COMMENT"
3376
3377 # a quoted string starts before $max_line_length
3378 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3379 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3380 $msg_type = "LONG_LINE_STRING"
3381 }
3382
3383 if ($msg_type ne "" &&
3384 (show_type("LONG_LINE") || show_type($msg_type))) {
Elyes Haouas0b1a03c2022-04-07 10:13:54 +02003385 my $msg_level = \&WARN;
3386 $msg_level = \&CHK if ($file);
3387 &{$msg_level}($msg_type,
3388 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003389 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003390 }
3391
3392# check for adding lines without a newline.
3393 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Elyes Haouas86e4a3a2022-07-13 16:32:45 +02003394 if (WARN("MISSING_EOF_NEWLINE",
3395 "adding a line without newline at end of file\n" . $herecurr) &&
3396 $fix) {
3397 fix_delete_line($fixlinenr+1, "No newline at end of file");
3398 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003399 }
3400
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003401# check we are in a valid source file C or perl if not then ignore this hunk
3402 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
3403
3404# at the beginning of a line any tabs must come first and anything
Elyes HAOUAS96771bf2022-01-29 07:28:45 +01003405# more than $tabsize must use tabs.
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003406 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3407 $rawline =~ /^\+\s* \s*/) {
3408 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3409 $rpt_cleaners = 1;
3410 if (ERROR("CODE_INDENT",
3411 "code indent should use tabs where possible\n" . $herevet) &&
3412 $fix) {
3413 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3414 }
3415 }
3416
3417# check for space before tabs.
3418 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3419 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3420 if (WARN("SPACE_BEFORE_TAB",
3421 "please, no space before tabs\n" . $herevet) &&
3422 $fix) {
3423 while ($fixed[$fixlinenr] =~
Elyes HAOUAS96771bf2022-01-29 07:28:45 +01003424 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003425 while ($fixed[$fixlinenr] =~
3426 s/(^\+.*) +\t/$1\t/) {}
3427 }
3428 }
3429
Martin Roth60915b32018-08-10 21:04:05 -06003430# check for assignments on the start of a line
3431 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
Elyes Haouasc5ede532022-07-13 16:29:14 +02003432 my $operator = $1;
3433 if (CHK("ASSIGNMENT_CONTINUATIONS",
3434 "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3435 $fix && $prevrawline =~ /^\+/) {
3436 # add assignment operator to the previous line, remove from current line
3437 $fixed[$fixlinenr - 1] .= " $operator";
3438 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3439 }
Martin Roth60915b32018-08-10 21:04:05 -06003440 }
3441
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003442# check for && or || at the start of a line
3443 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
Elyes Haouasd92fcf42022-07-13 16:34:29 +02003444 my $operator = $1;
3445 if (CHK("LOGICAL_CONTINUATIONS",
3446 "Logical continuations should be on the previous line\n" . $hereprev) &&
3447 $fix && $prevrawline =~ /^\+/) {
3448 # insert logical operator at last non-comment, non-whitepsace char on previous line
3449 $prevline =~ /[\s$;]*$/;
3450 my $line_end = substr($prevrawline, $-[0]);
3451 $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
3452 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3453 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003454 }
3455
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003456# check indentation starts on a tab stop
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01003457 if ($perl_version_ok &&
Martin Roth60915b32018-08-10 21:04:05 -06003458 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003459 my $indent = length($1);
Elyes HAOUAS96771bf2022-01-29 07:28:45 +01003460 if ($indent % $tabsize) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003461 if (WARN("TABSTOP",
3462 "Statements should start on a tabstop\n" . $herecurr) &&
3463 $fix) {
Elyes HAOUAS96771bf2022-01-29 07:28:45 +01003464 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003465 }
3466 }
3467 }
3468
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003469# check multi-line statement indentation matches previous line
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01003470 if ($perl_version_ok &&
Martin Roth387dec82017-09-17 19:20:46 -06003471 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003472 $prevline =~ /^\+(\t*)(.*)$/;
3473 my $oldindent = $1;
3474 my $rest = $2;
3475
3476 my $pos = pos_last_openparen($rest);
3477 if ($pos >= 0) {
3478 $line =~ /^(\+| )([ \t]*)/;
3479 my $newindent = $2;
3480
3481 my $goodtabindent = $oldindent .
Elyes HAOUAS96771bf2022-01-29 07:28:45 +01003482 "\t" x ($pos / $tabsize) .
3483 " " x ($pos % $tabsize);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003484 my $goodspaceindent = $oldindent . " " x $pos;
3485
3486 if ($newindent ne $goodtabindent &&
3487 $newindent ne $goodspaceindent) {
3488
3489 if (CHK("PARENTHESIS_ALIGNMENT",
3490 "Alignment should match open parenthesis\n" . $hereprev) &&
3491 $fix && $line =~ /^\+/) {
3492 $fixed[$fixlinenr] =~
3493 s/^\+[ \t]*/\+$goodtabindent/;
3494 }
3495 }
3496 }
3497 }
3498
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003499# check for space after cast like "(int) foo" or "(struct foo) bar"
3500# avoid checking a few false positives:
3501# "sizeof(<type>)" or "__alignof__(<type>)"
3502# function pointer declarations like "(*foo)(int) = bar;"
3503# structure definitions like "(struct foo) { 0 };"
3504# multiline macros that define functions
3505# known attributes or the __attribute__ keyword
3506 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3507 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003508 if (CHK("SPACING",
3509 "No space is necessary after a cast\n" . $herecurr) &&
3510 $fix) {
3511 $fixed[$fixlinenr] =~
3512 s/(\(\s*$Type\s*\))[ \t]+/$1/;
3513 }
3514 }
3515
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003516# Block comment styles
3517# Networking with an initial /*
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003518 if ($realfile =~ m@^(drivers/net/|net/)@ &&
3519 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
3520 $rawline =~ /^\+[ \t]*\*/ &&
3521 $realline > 2) {
3522 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3523 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3524 }
3525
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003526# Block comments use * on subsequent lines
3527 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3528 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003529 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3530 $rawline =~ /^\+/ && #line is new
3531 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003532 WARN("BLOCK_COMMENT_STYLE",
3533 "Block comments use * on subsequent lines\n" . $hereprev);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003534 }
3535
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003536# Block comments use */ on trailing lines
3537 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003538 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3539 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3540 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003541 WARN("BLOCK_COMMENT_STYLE",
3542 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003543 }
3544
Martin Rothedd591d2017-03-14 10:16:29 -06003545# Block comment * alignment
3546 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3547 $line =~ /^\+[ \t]*$;/ && #leading comment
3548 $rawline =~ /^\+[ \t]*\*/ && #leading *
3549 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
3550 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3551 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3552 my $oldindent;
3553 $prevrawline =~ m@^\+([ \t]*/?)\*@;
3554 if (defined($1)) {
3555 $oldindent = expand_tabs($1);
3556 } else {
3557 $prevrawline =~ m@^\+(.*/?)\*@;
3558 $oldindent = expand_tabs($1);
3559 }
3560 $rawline =~ m@^\+([ \t]*)\*@;
3561 my $newindent = $1;
3562 $newindent = expand_tabs($newindent);
3563 if (length($oldindent) ne length($newindent)) {
3564 WARN("BLOCK_COMMENT_STYLE",
3565 "Block comments should align the * on each line\n" . $hereprev);
3566 }
3567 }
3568
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003569# check for missing blank lines after struct/union declarations
3570# with exceptions for various attributes and macros
3571 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3572 $line =~ /^\+/ &&
3573 !($line =~ /^\+\s*$/ ||
3574 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3575 $line =~ /^\+\s*MODULE_/i ||
3576 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3577 $line =~ /^\+[a-z_]*init/ ||
3578 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3579 $line =~ /^\+\s*DECLARE/ ||
Martin Roth60915b32018-08-10 21:04:05 -06003580 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003581 $line =~ /^\+\s*__setup/)) {
3582 if (CHK("LINE_SPACING",
3583 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3584 $fix) {
3585 fix_insert_line($fixlinenr, "\+");
3586 }
3587 }
3588
3589# check for multiple consecutive blank lines
3590 if ($prevline =~ /^[\+ ]\s*$/ &&
3591 $line =~ /^\+\s*$/ &&
3592 $last_blank_line != ($linenr - 1)) {
3593 if (CHK("LINE_SPACING",
3594 "Please don't use multiple blank lines\n" . $hereprev) &&
3595 $fix) {
3596 fix_delete_line($fixlinenr, $rawline);
3597 }
3598
3599 $last_blank_line = $linenr;
3600 }
3601
3602# check for missing blank lines after declarations
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003603# (declarations must have the same indentation and not be at the start of line)
3604 if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
3605 # use temporaries
3606 my $sl = $sline;
3607 my $pl = $prevline;
3608 # remove $Attribute/$Sparse uses to simplify comparisons
3609 $sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3610 $pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3611 if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003612 # function pointer declarations
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003613 $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003614 # foo bar; where foo is some local typedef or #define
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003615 $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003616 # known declaration macros
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003617 $pl =~ /^\+\s+$declaration_macros/) &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003618 # for "else if" which can look like "$Ident $Ident"
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003619 !($pl =~ /^\+\s+$c90_Keywords\b/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003620 # other possible extensions of declaration lines
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003621 $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003622 # not starting a section or a macro "\" extended line
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003623 $pl =~ /(?:\{\s*|\\)$/) &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003624 # looks like a declaration
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003625 !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003626 # function pointer declarations
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003627 $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003628 # foo bar; where foo is some local typedef or #define
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003629 $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003630 # known declaration macros
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003631 $sl =~ /^\+\s+$declaration_macros/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003632 # start of struct or union or enum
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003633 $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003634 # start or end of block or continuation of declaration
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003635 $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003636 # bitfield continuation
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003637 $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003638 # other possible extensions of declaration lines
Elyes HAOUAS249c4042022-01-29 08:11:50 +01003639 $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
3640 if (WARN("LINE_SPACING",
3641 "Missing a blank line after declarations\n" . $hereprev) &&
3642 $fix) {
3643 fix_insert_line($fixlinenr, "\+");
3644 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003645 }
3646 }
3647
3648# check for spaces at the beginning of a line.
3649# Exceptions:
3650# 1) within comments
3651# 2) indented preprocessor commands
3652# 3) hanging labels
3653 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
3654 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3655 if (WARN("LEADING_SPACE",
3656 "please, no spaces at the start of a line\n" . $herevet) &&
3657 $fix) {
3658 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3659 }
3660 }
3661
3662# check we are in a valid C source file if not then ignore this hunk
3663 next if ($realfile !~ /\.(h|c)$/);
3664
Martin Roth60915b32018-08-10 21:04:05 -06003665# check for unusual line ending [ or (
3666 if ($line =~ /^\+.*([\[\(])\s*$/) {
3667 CHK("OPEN_ENDED_LINE",
3668 "Lines should not end with a '$1'\n" . $herecurr);
3669 }
3670
Martin Roth387dec82017-09-17 19:20:46 -06003671# check if this appears to be the start function declaration, save the name
3672 if ($sline =~ /^\+\{\s*$/ &&
3673 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3674 $context_function = $1;
3675 }
3676
3677# check if this appears to be the end of function declaration
3678 if ($sline =~ /^\+\}\s*$/) {
3679 undef $context_function;
3680 }
3681
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003682# check indentation of any line with a bare else
3683# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3684# if the previous line is a break or return and is indented 1 tab more...
3685 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3686 my $tabs = length($1) + 1;
3687 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3688 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3689 defined $lines[$linenr] &&
3690 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3691 WARN("UNNECESSARY_ELSE",
3692 "else is not generally useful after a break or return\n" . $hereprev);
3693 }
3694 }
3695
3696# check indentation of a line with a break;
Elyes Haouas26e0b942022-05-22 11:11:11 +02003697# if the previous line is a goto, return or break
3698# and is indented the same # of tabs
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003699 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3700 my $tabs = $1;
Elyes Haouasa59a87c2022-07-13 16:36:25 +02003701 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
3702 if (WARN("UNNECESSARY_BREAK",
3703 "break is not useful after a $1\n" . $hereprev) &&
3704 $fix) {
3705 fix_delete_line($fixlinenr, $rawline);
3706 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003707 }
3708 }
3709
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003710# check for RCS/CVS revision markers
3711 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3712 WARN("CVS_KEYWORD",
3713 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3714 }
3715
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003716# check for old HOTPLUG __dev<foo> section markings
3717 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3718 WARN("HOTPLUG_SECTION",
3719 "Using $1 is unnecessary\n" . $herecurr);
3720 }
3721
3722# Check for potential 'bare' types
3723 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3724 $realline_next);
3725#print "LINE<$line>\n";
Martin Roth387dec82017-09-17 19:20:46 -06003726 if ($linenr > $suppress_statement &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003727 $realcnt && $sline =~ /.\s*\S/) {
3728 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3729 ctx_statement_block($linenr, $realcnt, 0);
3730 $stat =~ s/\n./\n /g;
3731 $cond =~ s/\n./\n /g;
3732
3733#print "linenr<$linenr> <$stat>\n";
3734 # If this statement has no statement boundaries within
3735 # it there is no point in retrying a statement scan
3736 # until we hit end of it.
3737 my $frag = $stat; $frag =~ s/;+\s*$//;
3738 if ($frag !~ /(?:{|;)/) {
3739#print "skip<$line_nr_next>\n";
3740 $suppress_statement = $line_nr_next;
3741 }
3742
3743 # Find the real next line.
3744 $realline_next = $line_nr_next;
3745 if (defined $realline_next &&
3746 (!defined $lines[$realline_next - 1] ||
3747 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3748 $realline_next++;
3749 }
3750
3751 my $s = $stat;
3752 $s =~ s/{.*$//s;
3753
3754 # Ignore goto labels.
3755 if ($s =~ /$Ident:\*$/s) {
3756
3757 # Ignore functions being called
3758 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3759
3760 } elsif ($s =~ /^.\s*else\b/s) {
3761
3762 # declarations always start with types
3763 } 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) {
3764 my $type = $1;
3765 $type =~ s/\s+/ /g;
3766 possible($type, "A:" . $s);
3767
3768 # definitions in global scope can only start with types
3769 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3770 possible($1, "B:" . $s);
3771 }
3772
3773 # any (foo ... *) is a pointer cast, and foo is a type
3774 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3775 possible($1, "C:" . $s);
3776 }
3777
3778 # Check for any sort of function declaration.
3779 # int foo(something bar, other baz);
3780 # void (*store_gdt)(x86_descr_ptr *);
3781 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3782 my ($name_len) = length($1);
3783
3784 my $ctx = $s;
3785 substr($ctx, 0, $name_len + 1, '');
3786 $ctx =~ s/\)[^\)]*$//;
3787
3788 for my $arg (split(/\s*,\s*/, $ctx)) {
3789 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3790
3791 possible($1, "D:" . $s);
3792 }
3793 }
3794 }
3795
3796 }
3797
3798#
3799# Checks which may be anchored in the context.
3800#
3801
3802# Check for switch () and associated case and default
3803# statements should be at the same indent.
3804 if ($line=~/\bswitch\s*\(.*\)/) {
3805 my $err = '';
3806 my $sep = '';
3807 my @ctx = ctx_block_outer($linenr, $realcnt);
3808 shift(@ctx);
3809 for my $ctx (@ctx) {
3810 my ($clen, $cindent) = line_stats($ctx);
3811 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3812 $indent != $cindent) {
3813 $err .= "$sep$ctx\n";
3814 $sep = '';
3815 } else {
3816 $sep = "[...]\n";
3817 }
3818 }
3819 if ($err ne '') {
3820 ERROR("SWITCH_CASE_INDENT_LEVEL",
3821 "switch and case should be at the same indent\n$hereline$err");
3822 }
3823 }
3824
3825# if/while/etc brace do not go on next line, unless defining a do while loop,
3826# or if that brace on the next line is for something else
3827 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3828 my $pre_ctx = "$1$2";
3829
3830 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
3831
3832 if ($line =~ /^\+\t{6,}/) {
3833 WARN("DEEP_INDENTATION",
3834 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3835 }
3836
3837 my $ctx_cnt = $realcnt - $#ctx - 1;
3838 my $ctx = join("\n", @ctx);
3839
3840 my $ctx_ln = $linenr;
3841 my $ctx_skip = $realcnt;
3842
3843 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3844 defined $lines[$ctx_ln - 1] &&
3845 $lines[$ctx_ln - 1] =~ /^-/)) {
3846 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3847 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3848 $ctx_ln++;
3849 }
3850
3851 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3852 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3853
3854 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3855 ERROR("OPEN_BRACE",
3856 "that open brace { should be on the previous line\n" .
3857 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3858 }
3859 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3860 $ctx =~ /\)\s*\;\s*$/ &&
3861 defined $lines[$ctx_ln - 1])
3862 {
3863 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3864 if ($nindent > $indent) {
3865 WARN("TRAILING_SEMICOLON",
3866 "trailing semicolon indicates no statements, indent implies otherwise\n" .
3867 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3868 }
3869 }
3870 }
3871
3872# Check relative indent for conditionals and blocks.
Martin Roth387dec82017-09-17 19:20:46 -06003873 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003874 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3875 ctx_statement_block($linenr, $realcnt, 0)
3876 if (!defined $stat);
3877 my ($s, $c) = ($stat, $cond);
3878
3879 substr($s, 0, length($c), '');
3880
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003881 # remove inline comments
3882 $s =~ s/$;/ /g;
3883 $c =~ s/$;/ /g;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003884
3885 # Find out how long the conditional actually is.
3886 my @newlines = ($c =~ /\n/gs);
3887 my $cond_lines = 1 + $#newlines;
3888
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003889 # Make sure we remove the line prefixes as we have
3890 # none on the first line, and are going to readd them
3891 # where necessary.
3892 $s =~ s/\n./\n/gs;
3893 while ($s =~ /\n\s+\\\n/) {
3894 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3895 }
3896
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003897 # We want to check the first line inside the block
3898 # starting at the end of the conditional, so remove:
3899 # 1) any blank line termination
3900 # 2) any opening brace { on end of the line
3901 # 3) any do (...) {
3902 my $continuation = 0;
3903 my $check = 0;
3904 $s =~ s/^.*\bdo\b//;
3905 $s =~ s/^\s*{//;
3906 if ($s =~ s/^\s*\\//) {
3907 $continuation = 1;
3908 }
3909 if ($s =~ s/^\s*?\n//) {
3910 $check = 1;
3911 $cond_lines++;
3912 }
3913
3914 # Also ignore a loop construct at the end of a
3915 # preprocessor statement.
3916 if (($prevline =~ /^.\s*#\s*define\s/ ||
3917 $prevline =~ /\\\s*$/) && $continuation == 0) {
3918 $check = 0;
3919 }
3920
3921 my $cond_ptr = -1;
3922 $continuation = 0;
3923 while ($cond_ptr != $cond_lines) {
3924 $cond_ptr = $cond_lines;
3925
3926 # If we see an #else/#elif then the code
3927 # is not linear.
3928 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3929 $check = 0;
3930 }
3931
3932 # Ignore:
3933 # 1) blank lines, they should be at 0,
3934 # 2) preprocessor lines, and
3935 # 3) labels.
3936 if ($continuation ||
3937 $s =~ /^\s*?\n/ ||
3938 $s =~ /^\s*#\s*?/ ||
3939 $s =~ /^\s*$Ident\s*:/) {
3940 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
3941 if ($s =~ s/^.*?\n//) {
3942 $cond_lines++;
3943 }
3944 }
3945 }
3946
3947 my (undef, $sindent) = line_stats("+" . $s);
3948 my $stat_real = raw_line($linenr, $cond_lines);
3949
3950 # Check if either of these lines are modified, else
3951 # this is not this patch's fault.
3952 if (!defined($stat_real) ||
3953 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3954 $check = 0;
3955 }
3956 if (defined($stat_real) && $cond_lines > 1) {
3957 $stat_real = "[...]\n$stat_real";
3958 }
3959
3960 #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";
3961
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003962 if ($check && $s ne '' &&
Elyes HAOUAS96771bf2022-01-29 07:28:45 +01003963 (($sindent % $tabsize) != 0 ||
Stefan Reinauerc6080c62016-07-29 16:01:40 -07003964 ($sindent < $indent) ||
Martin Roth387dec82017-09-17 19:20:46 -06003965 ($sindent == $indent &&
3966 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Elyes HAOUAS96771bf2022-01-29 07:28:45 +01003967 ($sindent > $indent + $tabsize))) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01003968 WARN("SUSPECT_CODE_INDENT",
3969 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
3970 }
Julius Werner218906d2021-03-25 21:12:49 -07003971
3972# Also check if the next statement after the previous condition has the same indent
3973 my ($stat_next, undef, $line_nr_next_next) =
3974 ctx_statement_block($line_nr_next, $remain_next, $off_next);
3975 my $s_next = $stat_next;
3976
3977 # Remove line prefixes
3978 $s_next =~ s/\n./\n/g;
3979
3980 # Remove any comments
3981 $s_next =~ s/$;//g;
3982
3983 # Remove any leading labels
3984 $s_next =~ s/\n( ?$Ident:)/"\n" . " " x length($1)/eg;
3985
3986 # Skip this check for in case next statement starts with 'else'
3987 if ($s_next !~ /\belse\b/) {
3988
3989 # Remove while that belongs to a do {} while
3990 if ($stat =~ /\bdo\b/) {
3991 $s_next =~ s/^.*\bwhile\b\s*($balanced_parens)\s*?//;
3992 }
3993
3994 # Remove blank lines
3995 $s_next =~ s/\s*\\?\n//g;
3996
3997 # Get the real next lines
3998 my $next_nof_lines = $line_nr_next_next - $line_nr_next;
3999 my $stat_next_real = raw_line($line_nr_next, $next_nof_lines);
4000 if (!defined($stat_next_real)) {
4001 $stat_next_real = "";
4002 } elsif ($next_nof_lines > 1) {
4003 $stat_next_real = "[...]\n$stat_next_real";
4004 }
4005 my (undef, $nindent) = line_stats('+' . $s_next);
4006
4007 #print "stat_next<$stat_next> stat<$stat> indent<$indent> nindent<$nindent> s_next<$s_next> stat_next_real<$stat_next_real>\n";
4008
4009 if ($nindent > $indent) {
4010 WARN("SUSPICIOUS_CODE_INDENT",
4011 "suspicious code indentation after conditional statements\n" .
4012 $herecurr . "$stat_real\n$stat_next_real\n");
4013 }
4014 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004015 }
4016
4017 # Track the 'values' across context and added lines.
4018 my $opline = $line; $opline =~ s/^./ /;
4019 my ($curr_values, $curr_vars) =
4020 annotate_values($opline . "\n", $prev_values);
4021 $curr_values = $prev_values . $curr_values;
4022 if ($dbg_values) {
4023 my $outline = $opline; $outline =~ s/\t/ /g;
4024 print "$linenr > .$outline\n";
4025 print "$linenr > $curr_values\n";
4026 print "$linenr > $curr_vars\n";
4027 }
4028 $prev_values = substr($curr_values, -1);
4029
4030#ignore lines not being added
4031 next if ($line =~ /^[^\+]/);
4032
Elyes Haouasf9a3554a2022-07-13 18:03:47 +02004033# check for self assignments used to avoid compiler warnings
4034# e.g.: int foo = foo, *bar = NULL;
4035# struct foo bar = *(&(bar));
4036 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
4037 my $var = $1;
4038 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4039 WARN("SELF_ASSIGNMENT",
4040 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4041 }
4042 }
4043
Martin Rothedd591d2017-03-14 10:16:29 -06004044# check for dereferences that span multiple lines
4045 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4046 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4047 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4048 my $ref = $1;
4049 $line =~ /^.\s*($Lval)/;
4050 $ref .= $1;
4051 $ref =~ s/\s//g;
4052 WARN("MULTILINE_DEREFERENCE",
4053 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4054 }
4055
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004056# check for declarations of signed or unsigned without int
Martin Rothedd591d2017-03-14 10:16:29 -06004057 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004058 my $type = $1;
4059 my $var = $2;
4060 $var = "" if (!defined $var);
4061 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
4062 my $sign = $1;
4063 my $pointer = $2;
4064
4065 $pointer = "" if (!defined $pointer);
4066
4067 if (WARN("UNSPECIFIED_INT",
4068 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4069 $fix) {
4070 my $decl = trim($sign) . " int ";
4071 my $comp_pointer = $pointer;
4072 $comp_pointer =~ s/\s//g;
4073 $decl .= $comp_pointer;
4074 $decl = rtrim($decl) if ($var eq "");
4075 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
4076 }
4077 }
4078 }
4079
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004080# TEST: allow direct testing of the type matcher.
4081 if ($dbg_type) {
4082 if ($line =~ /^.\s*$Declare\s*$/) {
4083 ERROR("TEST_TYPE",
4084 "TEST: is type\n" . $herecurr);
4085 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
4086 ERROR("TEST_NOT_TYPE",
4087 "TEST: is not type ($1 is)\n". $herecurr);
4088 }
4089 next;
4090 }
4091# TEST: allow direct testing of the attribute matcher.
4092 if ($dbg_attr) {
4093 if ($line =~ /^.\s*$Modifier\s*$/) {
4094 ERROR("TEST_ATTR",
4095 "TEST: is attr\n" . $herecurr);
4096 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
4097 ERROR("TEST_NOT_ATTR",
4098 "TEST: is not attr ($1 is)\n". $herecurr);
4099 }
4100 next;
4101 }
4102
4103# check for initialisation to aggregates open brace on the next line
4104 if ($line =~ /^.\s*{/ &&
4105 $prevline =~ /(?:^|[^=])=\s*$/) {
4106 if (ERROR("OPEN_BRACE",
4107 "that open brace { should be on the previous line\n" . $hereprev) &&
4108 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4109 fix_delete_line($fixlinenr - 1, $prevrawline);
4110 fix_delete_line($fixlinenr, $rawline);
4111 my $fixedline = $prevrawline;
4112 $fixedline =~ s/\s*=\s*$/ = {/;
4113 fix_insert_line($fixlinenr, $fixedline);
4114 $fixedline = $line;
Martin Roth387dec82017-09-17 19:20:46 -06004115 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004116 fix_insert_line($fixlinenr, $fixedline);
4117 }
4118 }
4119
4120#
4121# Checks which are anchored on the added line.
4122#
4123
4124# check for malformed paths in #include statements (uses RAW line)
4125 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4126 my $path = $1;
4127 if ($path =~ m{//}) {
4128 ERROR("MALFORMED_INCLUDE",
4129 "malformed #include filename\n" . $herecurr);
4130 }
4131 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4132 ERROR("UAPI_INCLUDE",
4133 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
4134 }
4135 }
4136
4137# no C99 // comments
4138 if ($line =~ m{//}) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004139 if (ERROR("C99_COMMENTS",
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004140 "do not use C99 // comments\n" . $herecurr) &&
4141 $fix) {
4142 my $line = $fixed[$fixlinenr];
4143 if ($line =~ /\/\/(.*)$/) {
4144 my $comment = trim($1);
4145 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
4146 }
4147 }
4148 }
4149 # Remove C99 comments.
4150 $line =~ s@//.*@@;
4151 $opline =~ s@//.*@@;
4152
4153# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4154# the whole statement.
4155#print "APW <$lines[$realline_next - 1]>\n";
4156 if (defined $realline_next &&
4157 exists $lines[$realline_next - 1] &&
4158 !defined $suppress_export{$realline_next} &&
4159 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4160 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4161 # Handle definitions which produce identifiers with
4162 # a prefix:
4163 # XXX(foo);
4164 # EXPORT_SYMBOL(something_foo);
4165 my $name = $1;
Elyes Haouas26e0b942022-05-22 11:11:11 +02004166 $name =~ s/^\s*($Ident).*/$1/;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004167 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
4168 $name =~ /^${Ident}_$2/) {
4169#print "FOO C name<$name>\n";
4170 $suppress_export{$realline_next} = 1;
4171
4172 } elsif ($stat !~ /(?:
4173 \n.}\s*$|
4174 ^.DEFINE_$Ident\(\Q$name\E\)|
4175 ^.DECLARE_$Ident\(\Q$name\E\)|
4176 ^.LIST_HEAD\(\Q$name\E\)|
4177 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4178 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
4179 )/x) {
4180#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4181 $suppress_export{$realline_next} = 2;
4182 } else {
4183 $suppress_export{$realline_next} = 1;
4184 }
4185 }
4186 if (!defined $suppress_export{$linenr} &&
4187 $prevline =~ /^.\s*$/ &&
4188 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4189 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4190#print "FOO B <$lines[$linenr - 1]>\n";
4191 $suppress_export{$linenr} = 2;
4192 }
4193 if (defined $suppress_export{$linenr} &&
4194 $suppress_export{$linenr} == 2) {
4195 WARN("EXPORT_SYMBOL",
4196 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
4197 }
4198
4199# check for global initialisers.
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004200 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004201 if (ERROR("GLOBAL_INITIALISERS",
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004202 "do not initialise globals to $1\n" . $herecurr) &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004203 $fix) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004204 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004205 }
4206 }
4207# check for static initialisers.
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004208 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004209 if (ERROR("INITIALISED_STATIC",
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004210 "do not initialise statics to $1\n" .
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004211 $herecurr) &&
4212 $fix) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004213 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004214 }
4215 }
4216
4217# check for misordered declarations of char/short/int/long with signed/unsigned
4218 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4219 my $tmp = trim($1);
4220 WARN("MISORDERED_TYPE",
4221 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4222 }
4223
4224# check for static const char * arrays.
4225 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4226 WARN("STATIC_CONST_CHAR_ARRAY",
4227 "static const char * array should probably be static const char * const\n" .
4228 $herecurr);
Elyes Haouas26e0b942022-05-22 11:11:11 +02004229 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004230
4231# check for static char foo[] = "bar" declarations.
4232 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4233 WARN("STATIC_CONST_CHAR_ARRAY",
4234 "static char array declaration should probably be static const char\n" .
4235 $herecurr);
Elyes HAOUAS919b0c72022-01-29 07:48:52 +01004236 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004237
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004238# check for const <foo> const where <foo> is not a pointer or array type
4239 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4240 my $found = $1;
4241 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4242 WARN("CONST_CONST",
4243 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4244 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4245 WARN("CONST_CONST",
4246 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4247 }
4248 }
4249
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004250# check for non-global char *foo[] = {"bar", ...} declarations.
4251 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4252 WARN("STATIC_CONST_CHAR_ARRAY",
4253 "char * array declaration might be better as static const\n" .
4254 $herecurr);
Elyes HAOUAS919b0c72022-01-29 07:48:52 +01004255 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004256
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004257# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4258 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4259 my $array = $1;
4260 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4261 my $array_div = $1;
4262 if (WARN("ARRAY_SIZE",
4263 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4264 $fix) {
4265 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4266 }
4267 }
4268 }
4269
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004270# check for function declarations without arguments like "int foo()"
Elyes Haouas26e0b942022-05-22 11:11:11 +02004271 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004272 if (ERROR("FUNCTION_WITHOUT_ARGS",
4273 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4274 $fix) {
4275 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4276 }
4277 }
4278
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004279# check for new typedefs, only function parameters and sparse annotations
4280# make sense.
4281 if ($line =~ /\btypedef\s/ &&
4282 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4283 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
4284 $line !~ /\b$typeTypedefs\b/ &&
Martin Rothedd591d2017-03-14 10:16:29 -06004285 $line !~ /\b__bitwise\b/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004286 WARN("NEW_TYPEDEFS",
4287 "do not add new typedefs\n" . $herecurr);
4288 }
4289
4290# * goes on variable not on type
4291 # (char*[ const])
4292 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4293 #print "AA<$1>\n";
4294 my ($ident, $from, $to) = ($1, $2, $2);
4295
4296 # Should start with a space.
4297 $to =~ s/^(\S)/ $1/;
4298 # Should not end with a space.
4299 $to =~ s/\s+$//;
4300 # '*'s should not have spaces between.
4301 while ($to =~ s/\*\s+\*/\*\*/) {
4302 }
4303
4304## print "1: from<$from> to<$to> ident<$ident>\n";
4305 if ($from ne $to) {
4306 if (ERROR("POINTER_LOCATION",
4307 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4308 $fix) {
4309 my $sub_from = $ident;
4310 my $sub_to = $ident;
4311 $sub_to =~ s/\Q$from\E/$to/;
4312 $fixed[$fixlinenr] =~
4313 s@\Q$sub_from\E@$sub_to@;
4314 }
4315 }
4316 }
4317 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4318 #print "BB<$1>\n";
4319 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4320
4321 # Should start with a space.
4322 $to =~ s/^(\S)/ $1/;
4323 # Should not end with a space.
4324 $to =~ s/\s+$//;
4325 # '*'s should not have spaces between.
4326 while ($to =~ s/\*\s+\*/\*\*/) {
4327 }
4328 # Modifiers should have spaces.
4329 $to =~ s/(\b$Modifier$)/$1 /;
4330
4331## print "2: from<$from> to<$to> ident<$ident>\n";
4332 if ($from ne $to && $ident !~ /^$Modifier$/) {
4333 if (ERROR("POINTER_LOCATION",
4334 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4335 $fix) {
4336
4337 my $sub_from = $match;
4338 my $sub_to = $match;
4339 $sub_to =~ s/\Q$from\E/$to/;
4340 $fixed[$fixlinenr] =~
4341 s@\Q$sub_from\E@$sub_to@;
4342 }
4343 }
4344 }
4345
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004346# avoid BUG() or BUG_ON()
4347 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Martin Roth387dec82017-09-17 19:20:46 -06004348 my $msg_level = \&WARN;
4349 $msg_level = \&CHK if ($file);
4350 &{$msg_level}("AVOID_BUG",
4351 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004352 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004353
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004354# avoid LINUX_VERSION_CODE
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004355 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4356 WARN("LINUX_VERSION_CODE",
4357 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
4358 }
4359
4360# check for uses of printk_ratelimit
4361 if ($line =~ /\bprintk_ratelimit\s*\(/) {
4362 WARN("PRINTK_RATELIMITED",
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004363 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004364 }
4365
Martin Roth60915b32018-08-10 21:04:05 -06004366# printk should use KERN_* levels
4367 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4368 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4369 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004370 }
4371
4372 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4373 my $orig = $1;
4374 my $level = lc($orig);
4375 $level = "warn" if ($level eq "warning");
4376 my $level2 = $level;
4377 $level2 = "dbg" if ($level eq "debug");
4378 WARN("PREFER_PR_LEVEL",
4379 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
4380 }
4381
4382 if ($line =~ /\bpr_warning\s*\(/) {
4383 if (WARN("PREFER_PR_LEVEL",
4384 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4385 $fix) {
4386 $fixed[$fixlinenr] =~
4387 s/\bpr_warning\b/pr_warn/;
4388 }
4389 }
4390
4391 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4392 my $orig = $1;
4393 my $level = lc($orig);
4394 $level = "warn" if ($level eq "warning");
4395 $level = "dbg" if ($level eq "debug");
4396 WARN("PREFER_DEV_LEVEL",
4397 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4398 }
4399
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004400# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4401# number of false positives, but assembly files are not checked, so at
4402# least the arch entry code will not trigger this warning.
4403 if ($line =~ /\bENOSYS\b/) {
4404 WARN("ENOSYS",
4405 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4406 }
4407
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004408# function brace can't be on same line, except for #defines of do while,
4409# or if closed on same line
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01004410 if ($perl_version_ok &&
Martin Roth60915b32018-08-10 21:04:05 -06004411 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4412 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4413 $sline !~ /}/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004414 if (ERROR("OPEN_BRACE",
Martin Roth60915b32018-08-10 21:04:05 -06004415 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004416 $fix) {
4417 fix_delete_line($fixlinenr, $rawline);
4418 my $fixed_line = $rawline;
Elyes Haouas26e0b942022-05-22 11:11:11 +02004419 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004420 my $line1 = $1;
4421 my $line2 = $2;
4422 fix_insert_line($fixlinenr, ltrim($line1));
4423 fix_insert_line($fixlinenr, "\+{");
4424 if ($line2 !~ /^\s*$/) {
4425 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4426 }
4427 }
4428 }
4429
4430# open braces for enum, union and struct go on the same line.
4431 if ($line =~ /^.\s*{/ &&
4432 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
4433 if (ERROR("OPEN_BRACE",
4434 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4435 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4436 fix_delete_line($fixlinenr - 1, $prevrawline);
4437 fix_delete_line($fixlinenr, $rawline);
4438 my $fixedline = rtrim($prevrawline) . " {";
4439 fix_insert_line($fixlinenr, $fixedline);
4440 $fixedline = $rawline;
Martin Roth387dec82017-09-17 19:20:46 -06004441 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004442 if ($fixedline !~ /^\+\s*$/) {
4443 fix_insert_line($fixlinenr, $fixedline);
4444 }
4445 }
4446 }
4447
4448# missing space after union, struct or enum definition
4449 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4450 if (WARN("SPACING",
4451 "missing space after $1 definition\n" . $herecurr) &&
4452 $fix) {
4453 $fixed[$fixlinenr] =~
4454 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4455 }
4456 }
4457
4458# Function pointer declarations
4459# check spacing between type, funcptr, and args
4460# canonical declaration is "type (*funcptr)(args...)"
4461 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
4462 my $declare = $1;
4463 my $pre_pointer_space = $2;
4464 my $post_pointer_space = $3;
4465 my $funcname = $4;
4466 my $post_funcname_space = $5;
4467 my $pre_args_space = $6;
4468
4469# the $Declare variable will capture all spaces after the type
4470# so check it for a missing trailing missing space but pointer return types
4471# don't need a space so don't warn for those.
4472 my $post_declare_space = "";
4473 if ($declare =~ /(\s+)$/) {
4474 $post_declare_space = $1;
4475 $declare = rtrim($declare);
4476 }
4477 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
4478 WARN("SPACING",
4479 "missing space after return type\n" . $herecurr);
4480 $post_declare_space = " ";
4481 }
4482
4483# unnecessary space "type (*funcptr)(args...)"
4484# This test is not currently implemented because these declarations are
4485# equivalent to
4486# int foo(int bar, ...)
4487# and this is form shouldn't/doesn't generate a checkpatch warning.
4488#
4489# elsif ($declare =~ /\s{2,}$/) {
4490# WARN("SPACING",
4491# "Multiple spaces after return type\n" . $herecurr);
4492# }
4493
4494# unnecessary space "type ( *funcptr)(args...)"
4495 if (defined $pre_pointer_space &&
4496 $pre_pointer_space =~ /^\s/) {
4497 WARN("SPACING",
4498 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4499 }
4500
4501# unnecessary space "type (* funcptr)(args...)"
4502 if (defined $post_pointer_space &&
4503 $post_pointer_space =~ /^\s/) {
4504 WARN("SPACING",
4505 "Unnecessary space before function pointer name\n" . $herecurr);
4506 }
4507
4508# unnecessary space "type (*funcptr )(args...)"
4509 if (defined $post_funcname_space &&
4510 $post_funcname_space =~ /^\s/) {
4511 WARN("SPACING",
4512 "Unnecessary space after function pointer name\n" . $herecurr);
4513 }
4514
4515# unnecessary space "type (*funcptr) (args...)"
4516 if (defined $pre_args_space &&
4517 $pre_args_space =~ /^\s/) {
4518 WARN("SPACING",
4519 "Unnecessary space before function pointer arguments\n" . $herecurr);
4520 }
4521
4522 if (show_type("SPACING") && $fix) {
4523 $fixed[$fixlinenr] =~
4524 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
4525 }
4526 }
4527
4528# check for spacing round square brackets; allowed:
4529# 1. with a type on the left -- int [] a;
4530# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4531# 3. inside a curly brace -- = { [0...10] = 5 }
Martin Rothedd591d2017-03-14 10:16:29 -06004532# 4. in an extended asm instruction -- : [r0]"r"(r0) (coreboot)
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004533 while ($line =~ /(.*?\s)\[/g) {
4534 my ($where, $prefix) = ($-[1], $1);
4535 if ($prefix !~ /$Type\s+$/ &&
4536 ($where != 0 || $prefix !~ /^.\s+$/) &&
Martin Rothedd591d2017-03-14 10:16:29 -06004537 $prefix !~ /[{,:]\s+$/) { #coreboot
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004538 if (ERROR("BRACKET_SPACE",
4539 "space prohibited before open square bracket '['\n" . $herecurr) &&
4540 $fix) {
4541 $fixed[$fixlinenr] =~
4542 s/^(\+.*?)\s+\[/$1\[/;
4543 }
4544 }
4545 }
4546
4547# check for spaces between functions and their parentheses.
4548 while ($line =~ /($Ident)\s+\(/g) {
4549 my $name = $1;
4550 my $ctx_before = substr($line, 0, $-[1]);
4551 my $ctx = "$ctx_before$name";
4552
4553 # Ignore those directives where spaces _are_ permitted.
4554 if ($name =~ /^(?:
4555 if|for|while|switch|return|case|
4556 volatile|__volatile__|
4557 __attribute__|format|__extension__|
4558 asm|__asm__)$/x)
4559 {
4560 # cpp #define statements have non-optional spaces, ie
4561 # if there is a space between the name and the open
4562 # parenthesis it is simply not a parameter group.
4563 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4564
4565 # cpp #elif statement condition may start with a (
4566 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4567
4568 # If this whole things ends with a type its most
4569 # likely a typedef for a function.
4570 } elsif ($ctx =~ /$Type$/) {
4571
4572 } else {
4573 if (WARN("SPACING",
4574 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4575 $fix) {
4576 $fixed[$fixlinenr] =~
4577 s/\b$name\s+\(/$name\(/;
4578 }
4579 }
4580 }
4581
4582# Check operator spacing.
4583 if (!($line=~/\#\s*include/)) {
4584 my $fixed_line = "";
4585 my $line_fixed = 0;
4586
4587 my $ops = qr{
4588 <<=|>>=|<=|>=|==|!=|
4589 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4590 =>|->|<<|>>|<|>|=|!|~|
4591 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
4592 \?:|\?|:
4593 }x;
4594 my @elements = split(/($ops|;)/, $opline);
4595
4596## print("element count: <" . $#elements . ">\n");
4597## foreach my $el (@elements) {
4598## print("el: <$el>\n");
4599## }
4600
4601 my @fix_elements = ();
4602 my $off = 0;
4603
4604 foreach my $el (@elements) {
4605 push(@fix_elements, substr($rawline, $off, length($el)));
4606 $off += length($el);
4607 }
4608
4609 $off = 0;
4610
4611 my $blank = copy_spacing($opline);
4612 my $last_after = -1;
4613
4614 for (my $n = 0; $n < $#elements; $n += 2) {
4615
4616 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4617
4618## print("n: <$n> good: <$good>\n");
4619
4620 $off += length($elements[$n]);
4621
4622 # Pick up the preceding and succeeding characters.
4623 my $ca = substr($opline, 0, $off);
4624 my $cc = '';
4625 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4626 $cc = substr($opline, $off + length($elements[$n + 1]));
4627 }
4628 my $cb = "$ca$;$cc";
4629
4630 my $a = '';
4631 $a = 'V' if ($elements[$n] ne '');
4632 $a = 'W' if ($elements[$n] =~ /\s$/);
4633 $a = 'C' if ($elements[$n] =~ /$;$/);
4634 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4635 $a = 'O' if ($elements[$n] eq '');
4636 $a = 'E' if ($ca =~ /^\s*$/);
4637
4638 my $op = $elements[$n + 1];
4639
4640 my $c = '';
4641 if (defined $elements[$n + 2]) {
4642 $c = 'V' if ($elements[$n + 2] ne '');
4643 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
4644 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4645 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4646 $c = 'O' if ($elements[$n + 2] eq '');
4647 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4648 } else {
4649 $c = 'E';
4650 }
4651
4652 my $ctx = "${a}x${c}";
4653
4654 my $at = "(ctx:$ctx)";
4655
4656 my $ptr = substr($blank, 0, $off) . "^";
4657 my $hereptr = "$hereline$ptr\n";
4658
4659 # Pull out the value of this operator.
4660 my $op_type = substr($curr_values, $off + 1, 1);
4661
4662 # Get the full operator variant.
4663 my $opv = $op . substr($curr_vars, $off, 1);
4664
4665 # Ignore operators passed as parameters.
4666 if ($op_type ne 'V' &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004667 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004668
4669# # Ignore comments
4670# } elsif ($op =~ /^$;+$/) {
4671
4672 # ; should have either the end of line or a space or \ after it
4673 } elsif ($op eq ';') {
4674 if ($ctx !~ /.x[WEBC]/ &&
4675 $cc !~ /^\\/ && $cc !~ /^;/) {
4676 if (ERROR("SPACING",
4677 "space required after that '$op' $at\n" . $hereptr)) {
4678 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4679 $line_fixed = 1;
4680 }
4681 }
4682
4683 # // is a comment
4684 } elsif ($op eq '//') {
4685
4686 # : when part of a bitfield
4687 } elsif ($opv eq ':B') {
4688 # skip the bitfield test for now
4689
4690 # No spaces for:
4691 # ->
4692 } elsif ($op eq '->') {
4693 if ($ctx =~ /Wx.|.xW/) {
4694 if (ERROR("SPACING",
4695 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4696 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4697 if (defined $fix_elements[$n + 2]) {
4698 $fix_elements[$n + 2] =~ s/^\s+//;
4699 }
4700 $line_fixed = 1;
4701 }
4702 }
4703
4704 # , must not have a space before and must have a space on the right.
4705 } elsif ($op eq ',') {
4706 my $rtrim_before = 0;
4707 my $space_after = 0;
4708 if ($ctx =~ /Wx./) {
4709 if (ERROR("SPACING",
4710 "space prohibited before that '$op' $at\n" . $hereptr)) {
4711 $line_fixed = 1;
4712 $rtrim_before = 1;
4713 }
4714 }
4715 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
4716 if (ERROR("SPACING",
4717 "space required after that '$op' $at\n" . $hereptr)) {
4718 $line_fixed = 1;
4719 $last_after = $n;
4720 $space_after = 1;
4721 }
4722 }
4723 if ($rtrim_before || $space_after) {
4724 if ($rtrim_before) {
4725 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4726 } else {
4727 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4728 }
4729 if ($space_after) {
4730 $good .= " ";
4731 }
4732 }
4733
4734 # '*' as part of a type definition -- reported already.
4735 } elsif ($opv eq '*_') {
4736 #warn "'*' is part of type\n";
4737
4738 # unary operators should have a space before and
4739 # none after. May be left adjacent to another
4740 # unary operator, or a cast
4741 } elsif ($op eq '!' || $op eq '~' ||
4742 $opv eq '*U' || $opv eq '-U' ||
4743 $opv eq '&U' || $opv eq '&&U') {
4744 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
4745 if (ERROR("SPACING",
4746 "space required before that '$op' $at\n" . $hereptr)) {
4747 if ($n != $last_after + 2) {
4748 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4749 $line_fixed = 1;
4750 }
4751 }
4752 }
4753 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4754 # A unary '*' may be const
4755
4756 } elsif ($ctx =~ /.xW/) {
4757 if (ERROR("SPACING",
4758 "space prohibited after that '$op' $at\n" . $hereptr)) {
4759 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
4760 if (defined $fix_elements[$n + 2]) {
4761 $fix_elements[$n + 2] =~ s/^\s+//;
4762 }
4763 $line_fixed = 1;
4764 }
4765 }
4766
4767 # unary ++ and unary -- are allowed no space on one side.
4768 } elsif ($op eq '++' or $op eq '--') {
4769 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
4770 if (ERROR("SPACING",
4771 "space required one side of that '$op' $at\n" . $hereptr)) {
4772 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4773 $line_fixed = 1;
4774 }
4775 }
4776 if ($ctx =~ /Wx[BE]/ ||
4777 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
4778 if (ERROR("SPACING",
4779 "space prohibited before that '$op' $at\n" . $hereptr)) {
4780 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4781 $line_fixed = 1;
4782 }
4783 }
4784 if ($ctx =~ /ExW/) {
4785 if (ERROR("SPACING",
4786 "space prohibited after that '$op' $at\n" . $hereptr)) {
4787 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4788 if (defined $fix_elements[$n + 2]) {
4789 $fix_elements[$n + 2] =~ s/^\s+//;
4790 }
4791 $line_fixed = 1;
4792 }
4793 }
4794
4795 # << and >> may either have or not have spaces both sides
4796 } elsif ($op eq '<<' or $op eq '>>' or
4797 $op eq '&' or $op eq '^' or $op eq '|' or
4798 $op eq '+' or $op eq '-' or
4799 $op eq '*' or $op eq '/' or
4800 $op eq '%')
4801 {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004802 if ($check) {
4803 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4804 if (CHK("SPACING",
4805 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4806 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4807 $fix_elements[$n + 2] =~ s/^\s+//;
4808 $line_fixed = 1;
4809 }
4810 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4811 if (CHK("SPACING",
4812 "space preferred before that '$op' $at\n" . $hereptr)) {
4813 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4814 $line_fixed = 1;
4815 }
4816 }
4817 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004818 if (ERROR("SPACING",
4819 "need consistent spacing around '$op' $at\n" . $hereptr)) {
4820 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4821 if (defined $fix_elements[$n + 2]) {
4822 $fix_elements[$n + 2] =~ s/^\s+//;
4823 }
4824 $line_fixed = 1;
4825 }
4826 }
4827
4828 # A colon needs no spaces before when it is
4829 # terminating a case value or a label.
4830 } elsif ($opv eq ':C' || $opv eq ':L') {
4831 if ($ctx =~ /Wx./) {
4832 if (ERROR("SPACING",
4833 "space prohibited before that '$op' $at\n" . $hereptr)) {
4834 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4835 $line_fixed = 1;
4836 }
4837 }
4838
4839 # All the others need spaces both sides.
4840 } elsif ($ctx !~ /[EWC]x[CWE]/) {
4841 my $ok = 0;
4842
4843 # Ignore email addresses <foo@bar>
4844 if (($op eq '<' &&
4845 $cc =~ /^\S+\@\S+>/) ||
4846 ($op eq '>' &&
4847 $ca =~ /<\S+\@\S+$/))
4848 {
Elyes HAOUAS919b0c72022-01-29 07:48:52 +01004849 $ok = 1;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004850 }
4851
Stefan Reinauerc6080c62016-07-29 16:01:40 -07004852 # for asm volatile statements
4853 # ignore a colon with another
4854 # colon immediately before or after
4855 if (($op eq ':') &&
4856 ($ca =~ /:$/ || $cc =~ /^:/)) {
4857 $ok = 1;
4858 }
4859
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004860 # messages are ERROR, but ?: are CHK
4861 if ($ok == 0) {
Martin Roth387dec82017-09-17 19:20:46 -06004862 my $msg_level = \&ERROR;
4863 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004864
Martin Roth387dec82017-09-17 19:20:46 -06004865 if (&{$msg_level}("SPACING",
4866 "spaces required around that '$op' $at\n" . $hereptr)) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004867 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4868 if (defined $fix_elements[$n + 2]) {
4869 $fix_elements[$n + 2] =~ s/^\s+//;
4870 }
4871 $line_fixed = 1;
4872 }
4873 }
4874 }
4875 $off += length($elements[$n + 1]);
4876
4877## print("n: <$n> GOOD: <$good>\n");
4878
4879 $fixed_line = $fixed_line . $good;
4880 }
4881
4882 if (($#elements % 2) == 0) {
4883 $fixed_line = $fixed_line . $fix_elements[$#elements];
4884 }
4885
4886 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4887 $fixed[$fixlinenr] = $fixed_line;
4888 }
4889
4890
4891 }
4892
4893# check for whitespace before a non-naked semicolon
4894 if ($line =~ /^\+.*\S\s+;\s*$/) {
4895 if (WARN("SPACING",
4896 "space prohibited before semicolon\n" . $herecurr) &&
4897 $fix) {
4898 1 while $fixed[$fixlinenr] =~
4899 s/^(\+.*\S)\s+;/$1;/;
4900 }
4901 }
4902
4903# check for multiple assignments
4904 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4905 CHK("MULTIPLE_ASSIGNMENTS",
4906 "multiple assignments should be avoided\n" . $herecurr);
4907 }
4908
4909## # check for multiple declarations, allowing for a function declaration
4910## # continuation.
4911## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4912## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4913##
4914## # Remove any bracketed sections to ensure we do not
Elyes HAOUAS919b0c72022-01-29 07:48:52 +01004915## # falsely report the parameters of functions.
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004916## my $ln = $line;
4917## while ($ln =~ s/\([^\(\)]*\)//g) {
4918## }
4919## if ($ln =~ /,/) {
4920## WARN("MULTIPLE_DECLARATION",
4921## "declaring multiple variables together should be avoided\n" . $herecurr);
4922## }
4923## }
4924
4925#need space before brace following if, while, etc
Alexander Couzensebef00f2016-04-11 00:52:01 +02004926 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Elyes Haouas26e0b942022-05-22 11:11:11 +02004927 $line =~ /\b(?:else|do)\{/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004928 if (ERROR("SPACING",
4929 "space required before the open brace '{'\n" . $herecurr) &&
4930 $fix) {
Martin Roth387dec82017-09-17 19:20:46 -06004931 #coreboot - Open braces must be escaped in regex
Elyes Haouas26e0b942022-05-22 11:11:11 +02004932 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 \{/;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004933 }
4934 }
4935
4936## # check for blank lines before declarations
4937## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4938## $prevrawline =~ /^.\s*$/) {
4939## WARN("SPACING",
4940## "No blank lines before declarations\n" . $hereprev);
4941## }
4942##
4943
4944# closing brace should have a space following it when it has anything
4945# on the line
Elyes Haouas26e0b942022-05-22 11:11:11 +02004946 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01004947 if (ERROR("SPACING",
4948 "space required after that close brace '}'\n" . $herecurr) &&
4949 $fix) {
4950 $fixed[$fixlinenr] =~
4951 s/}((?!(?:,|;|\)))\S)/} $1/;
4952 }
4953 }
4954
4955# check spacing on square brackets
4956 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
4957 if (ERROR("SPACING",
4958 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4959 $fix) {
4960 $fixed[$fixlinenr] =~
4961 s/\[\s+/\[/;
4962 }
4963 }
4964 if ($line =~ /\s\]/) {
4965 if (ERROR("SPACING",
4966 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4967 $fix) {
4968 $fixed[$fixlinenr] =~
4969 s/\s+\]/\]/;
4970 }
4971 }
4972
4973# check spacing on parentheses
4974 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4975 $line !~ /for\s*\(\s+;/) {
4976 if (ERROR("SPACING",
4977 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4978 $fix) {
4979 $fixed[$fixlinenr] =~
4980 s/\(\s+/\(/;
4981 }
4982 }
4983 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4984 $line !~ /for\s*\(.*;\s+\)/ &&
4985 $line !~ /:\s+\)/) {
4986 if (ERROR("SPACING",
4987 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4988 $fix) {
4989 $fixed[$fixlinenr] =~
4990 s/\s+\)/\)/;
4991 }
4992 }
4993
4994# check unnecessary parentheses around addressof/dereference single $Lvals
4995# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4996
4997 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4998 my $var = $1;
4999 if (CHK("UNNECESSARY_PARENTHESES",
5000 "Unnecessary parentheses around $var\n" . $herecurr) &&
5001 $fix) {
5002 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5003 }
5004 }
5005
5006# check for unnecessary parentheses around function pointer uses
5007# ie: (foo->bar)(); should be foo->bar();
5008# but not "if (foo->bar) (" to avoid some false positives
5009 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5010 my $var = $2;
5011 if (CHK("UNNECESSARY_PARENTHESES",
5012 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5013 $fix) {
5014 my $var2 = deparenthesize($var);
5015 $var2 =~ s/\s//g;
5016 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5017 }
5018 }
5019
Martin Roth387dec82017-09-17 19:20:46 -06005020# check for unnecessary parentheses around comparisons in if uses
Martin Roth60915b32018-08-10 21:04:05 -06005021# when !drivers/staging or command-line uses --strict
5022 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01005023 $perl_version_ok && defined($stat) &&
Martin Roth387dec82017-09-17 19:20:46 -06005024 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5025 my $if_stat = $1;
5026 my $test = substr($2, 1, -1);
5027 my $herectx;
5028 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5029 my $match = $1;
5030 # avoid parentheses around potential macro args
5031 next if ($match =~ /^\s*\w+\s*$/);
5032 if (!defined($herectx)) {
5033 $herectx = $here . "\n";
5034 my $cnt = statement_rawlines($if_stat);
5035 for (my $n = 0; $n < $cnt; $n++) {
5036 my $rl = raw_line($linenr, $n);
5037 $herectx .= $rl . "\n";
5038 last if $rl =~ /^[ \+].*\{/;
5039 }
5040 }
5041 CHK("UNNECESSARY_PARENTHESES",
5042 "Unnecessary parentheses around '$match'\n" . $herectx);
5043 }
5044 }
5045
Elyes Haouas26e0b942022-05-22 11:11:11 +02005046# check that goto labels aren't indented (allow a single space indentation)
5047# and ignore bitfield definitions like foo:1
5048# Strictly, labels can have whitespace after the identifier and before the :
5049# but this is not allowed here as many ?: uses would appear to be labels
5050 if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ &&
5051 $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ &&
5052 $sline !~ /^.\s+default:/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005053 if (WARN("INDENTED_LABEL",
5054 "labels should not be indented\n" . $herecurr) &&
5055 $fix) {
5056 $fixed[$fixlinenr] =~
5057 s/^(.)\s+/$1/;
5058 }
5059 }
5060
Elyes Haouas50eef652022-07-13 18:07:58 +02005061# check if a statement with a comma should be two statements like:
5062# foo = bar(), /* comma should be semicolon */
5063# bar = baz();
5064 if (defined($stat) &&
5065 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5066 my $cnt = statement_rawlines($stat);
5067 my $herectx = get_stat_here($linenr, $cnt, $here);
5068 WARN("SUSPECT_COMMA_SEMICOLON",
5069 "Possible comma where semicolon could be used\n" . $herectx);
5070 }
5071
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005072# return is not a function
5073 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
5074 my $spacing = $1;
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01005075 if ($perl_version_ok &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005076 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5077 my $value = $1;
5078 $value = deparenthesize($value);
5079 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5080 ERROR("RETURN_PARENTHESES",
5081 "return is not a function, parentheses are not required\n" . $herecurr);
5082 }
5083 } elsif ($spacing !~ /\s+/) {
5084 ERROR("SPACING",
5085 "space required before the open parenthesis '('\n" . $herecurr);
5086 }
5087 }
5088
5089# unnecessary return in a void function
5090# at end-of-function, with the previous line a single leading tab, then return;
5091# and the line before that not a goto label target like "out:"
5092 if ($sline =~ /^[ \+]}\s*$/ &&
5093 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5094 $linenr >= 3 &&
5095 $lines[$linenr - 3] =~ /^[ +]/ &&
5096 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
5097 WARN("RETURN_VOID",
5098 "void function return statements are not generally useful\n" . $hereprev);
Elyes HAOUAS919b0c72022-01-29 07:48:52 +01005099 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005100
5101# if statements using unnecessary parentheses - ie: if ((foo == bar))
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01005102 if ($perl_version_ok &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005103 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5104 my $openparens = $1;
5105 my $count = $openparens =~ tr@\(@\(@;
5106 my $msg = "";
5107 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5108 my $comp = $4; #Not $1 because of $LvalOrFunc
5109 $msg = " - maybe == should be = ?" if ($comp eq "==");
5110 WARN("UNNECESSARY_PARENTHESES",
5111 "Unnecessary parentheses$msg\n" . $herecurr);
5112 }
5113 }
5114
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005115# comparisons with a constant or upper case identifier on the left
5116# avoid cases like "foo + BAR < baz"
5117# only fix matches surrounded by parentheses to avoid incorrect
5118# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01005119 if ($perl_version_ok &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005120 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5121 my $lead = $1;
5122 my $const = $2;
5123 my $comp = $3;
5124 my $to = $4;
5125 my $newcomp = $comp;
5126 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5127 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5128 WARN("CONSTANT_COMPARISON",
5129 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5130 $fix) {
5131 if ($comp eq "<") {
5132 $newcomp = ">";
5133 } elsif ($comp eq "<=") {
5134 $newcomp = ">=";
5135 } elsif ($comp eq ">") {
5136 $newcomp = "<";
5137 } elsif ($comp eq ">=") {
5138 $newcomp = "<=";
5139 }
5140 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5141 }
5142 }
5143
5144# Return of what appears to be an errno should normally be negative
5145 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005146 my $name = $1;
5147 if ($name ne 'EOF' && $name ne 'ERROR') {
5148 WARN("USE_NEGATIVE_ERRNO",
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005149 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005150 }
5151 }
5152
5153# Need a space before open parenthesis after if, while etc
5154 if ($line =~ /\b(if|while|for|switch)\(/) {
5155 if (ERROR("SPACING",
5156 "space required before the open parenthesis '('\n" . $herecurr) &&
5157 $fix) {
5158 $fixed[$fixlinenr] =~
5159 s/\b(if|while|for|switch)\(/$1 \(/;
5160 }
5161 }
5162
5163# Check for illegal assignment in if conditional -- and check for trailing
5164# statements after the conditional.
5165 if ($line =~ /do\s*(?!{)/) {
5166 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5167 ctx_statement_block($linenr, $realcnt, 0)
5168 if (!defined $stat);
5169 my ($stat_next) = ctx_statement_block($line_nr_next,
5170 $remain_next, $off_next);
5171 $stat_next =~ s/\n./\n /g;
5172 ##print "stat<$stat> stat_next<$stat_next>\n";
5173
5174 if ($stat_next =~ /^\s*while\b/) {
5175 # If the statement carries leading newlines,
5176 # then count those as offsets.
5177 my ($whitespace) =
5178 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5179 my $offset =
5180 statement_rawlines($whitespace) - 1;
5181
5182 $suppress_whiletrailers{$line_nr_next +
5183 $offset} = 1;
5184 }
5185 }
5186 if (!defined $suppress_whiletrailers{$linenr} &&
5187 defined($stat) && defined($cond) &&
5188 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5189 my ($s, $c) = ($stat, $cond);
5190
5191 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
5192 ERROR("ASSIGN_IN_IF",
5193 "do not use assignment in if condition\n" . $herecurr);
5194 }
5195
5196 # Find out what is on the end of the line after the
5197 # conditional.
5198 substr($s, 0, length($c), '');
5199 $s =~ s/\n.*//g;
Elyes HAOUAS919b0c72022-01-29 07:48:52 +01005200 $s =~ s/$;//g; # Remove any comments
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005201 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5202 $c !~ /}\s*while\s*/)
5203 {
5204 # Find out how long the conditional actually is.
5205 my @newlines = ($c =~ /\n/gs);
5206 my $cond_lines = 1 + $#newlines;
5207 my $stat_real = '';
5208
5209 $stat_real = raw_line($linenr, $cond_lines)
5210 . "\n" if ($cond_lines);
5211 if (defined($stat_real) && $cond_lines > 1) {
5212 $stat_real = "[...]\n$stat_real";
5213 }
5214
5215 ERROR("TRAILING_STATEMENTS",
5216 "trailing statements should be on next line\n" . $herecurr . $stat_real);
5217 }
5218 }
5219
5220# Check for bitwise tests written as boolean
5221 if ($line =~ /
5222 (?:
5223 (?:\[|\(|\&\&|\|\|)
5224 \s*0[xX][0-9]+\s*
5225 (?:\&\&|\|\|)
5226 |
5227 (?:\&\&|\|\|)
5228 \s*0[xX][0-9]+\s*
5229 (?:\&\&|\|\||\)|\])
5230 )/x)
5231 {
5232 WARN("HEXADECIMAL_BOOLEAN_TEST",
5233 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
5234 }
5235
5236# if and else should not have general statements after it
5237 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5238 my $s = $1;
Elyes HAOUAS919b0c72022-01-29 07:48:52 +01005239 $s =~ s/$;//g; # Remove any comments
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005240 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5241 ERROR("TRAILING_STATEMENTS",
5242 "trailing statements should be on next line\n" . $herecurr);
5243 }
5244 }
5245# if should not continue a brace
5246 if ($line =~ /}\s*if\b/) {
5247 ERROR("TRAILING_STATEMENTS",
5248 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
5249 $herecurr);
5250 }
5251# case and default should not have general statements after them
5252 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5253 $line !~ /\G(?:
5254 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5255 \s*return\s+
5256 )/xg)
5257 {
5258 ERROR("TRAILING_STATEMENTS",
5259 "trailing statements should be on next line\n" . $herecurr);
5260 }
5261
5262 # Check for }<nl>else {, these must be at the same
5263 # indent level to be relevant to each other.
5264 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5265 $previndent == $indent) {
5266 if (ERROR("ELSE_AFTER_BRACE",
5267 "else should follow close brace '}'\n" . $hereprev) &&
5268 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5269 fix_delete_line($fixlinenr - 1, $prevrawline);
5270 fix_delete_line($fixlinenr, $rawline);
5271 my $fixedline = $prevrawline;
5272 $fixedline =~ s/}\s*$//;
5273 if ($fixedline !~ /^\+\s*$/) {
5274 fix_insert_line($fixlinenr, $fixedline);
5275 }
5276 $fixedline = $rawline;
5277 $fixedline =~ s/^(.\s*)else/$1} else/;
5278 fix_insert_line($fixlinenr, $fixedline);
5279 }
5280 }
5281
5282 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5283 $previndent == $indent) {
5284 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5285
5286 # Find out what is on the end of the line after the
5287 # conditional.
5288 substr($s, 0, length($c), '');
5289 $s =~ s/\n.*//g;
5290
5291 if ($s =~ /^\s*;/) {
5292 if (ERROR("WHILE_AFTER_BRACE",
5293 "while should follow close brace '}'\n" . $hereprev) &&
5294 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5295 fix_delete_line($fixlinenr - 1, $prevrawline);
5296 fix_delete_line($fixlinenr, $rawline);
5297 my $fixedline = $prevrawline;
5298 my $trailing = $rawline;
5299 $trailing =~ s/^\+//;
5300 $trailing = trim($trailing);
5301 $fixedline =~ s/}\s*$/} $trailing/;
5302 fix_insert_line($fixlinenr, $fixedline);
5303 }
5304 }
5305 }
5306
5307#Specific variable tests
5308 while ($line =~ m{($Constant|$Lval)}g) {
5309 my $var = $1;
5310
5311#gcc binary extension
5312 if ($var =~ /^$Binary$/) {
5313 if (WARN("GCC_BINARY_CONSTANT",
5314 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
5315 $fix) {
5316 my $hexval = sprintf("0x%x", oct($var));
5317 $fixed[$fixlinenr] =~
5318 s/\b$var\b/$hexval/;
5319 }
5320 }
5321
5322#CamelCase
5323 if ($var !~ /^$Constant$/ &&
5324 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
5325#Ignore Page<foo> variants
5326 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Elyes Haouas26e0b942022-05-22 11:11:11 +02005327#Ignore SI style variants like nS, mV and dB
5328#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5329 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005330#Ignore some three character SI units explicitly, like MiB and KHz
5331 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Elyes Haouas0d42db62022-09-05 11:24:00 +02005332 while ($var =~ m{\b($Ident)}g) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005333 my $word = $1;
5334 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5335 if ($check) {
5336 seed_camelcase_includes();
5337 if (!$file && !$camelcase_file_seeded) {
5338 seed_camelcase_file($realfile);
5339 $camelcase_file_seeded = 1;
5340 }
5341 }
5342 if (!defined $camelcase{$word}) {
5343 $camelcase{$word} = 1;
5344 CHK("CAMELCASE",
5345 "Avoid CamelCase: <$word>\n" . $herecurr);
5346 }
5347 }
5348 }
5349 }
5350
5351#no spaces allowed after \ in define
5352 if ($line =~ /\#\s*define.*\\\s+$/) {
5353 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5354 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5355 $fix) {
5356 $fixed[$fixlinenr] =~ s/\s+$//;
5357 }
5358 }
5359
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005360# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5361# itself <asm/foo.h> (uses RAW line)
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005362 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5363 my $file = "$1.h";
5364 my $checkfile = "include/linux/$file";
5365 if (-f "$root/$checkfile" &&
5366 $realfile ne $checkfile &&
5367 $1 !~ /$allowed_asm_includes/)
5368 {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005369 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5370 if ($asminclude > 0) {
5371 if ($realfile =~ m{^arch/}) {
5372 CHK("ARCH_INCLUDE_LINUX",
5373 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5374 } else {
5375 WARN("INCLUDE_LINUX",
5376 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5377 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005378 }
5379 }
5380 }
5381
5382# multi-statement macros should be enclosed in a do while loop, grab the
5383# first statement and ensure its the whole macro if its not enclosed
5384# in a known good container
5385 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5386 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5387 my $ln = $linenr;
5388 my $cnt = $realcnt;
5389 my ($off, $dstat, $dcond, $rest);
5390 my $ctx = '';
5391 my $has_flow_statement = 0;
5392 my $has_arg_concat = 0;
5393 ($dstat, $dcond, $ln, $cnt, $off) =
5394 ctx_statement_block($linenr, $realcnt, 0);
5395 $ctx = $dstat;
5396 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5397 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5398
5399 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005400 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005401
Martin Rothedd591d2017-03-14 10:16:29 -06005402 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5403 my $define_args = $1;
5404 my $define_stmt = $dstat;
5405 my @def_args = ();
5406
5407 if (defined $define_args && $define_args ne "") {
5408 $define_args = substr($define_args, 1, length($define_args) - 2);
5409 $define_args =~ s/\s*//g;
Elyes Haouas26e0b942022-05-22 11:11:11 +02005410 $define_args =~ s/\\\+?//g;
Martin Rothedd591d2017-03-14 10:16:29 -06005411 @def_args = split(",", $define_args);
5412 }
5413
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005414 $dstat =~ s/$;//g;
5415 $dstat =~ s/\\\n.//g;
5416 $dstat =~ s/^\s*//s;
5417 $dstat =~ s/\s*$//s;
5418
5419 # Flatten any parentheses and braces
Elyes Haouas26e0b942022-05-22 11:11:11 +02005420 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5421 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5422 $dstat =~ s/.\[[^\[\]]*\]/1u/)
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005423 {
5424 }
5425
Elyes HAOUAS919b0c72022-01-29 07:48:52 +01005426 # Flatten any obvious string concatenation.
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005427 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5428 $dstat =~ s/$Ident\s*($String)/$1/)
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005429 {
5430 }
5431
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005432 # Make asm volatile uses seem like a generic function
5433 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5434
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005435 my $exceptions = qr{
5436 $Declare|
5437 module_param_named|
5438 MODULE_PARM_DESC|
5439 DECLARE_PER_CPU|
5440 DEFINE_PER_CPU|
5441 __typeof__\(|
5442 union|
5443 struct|
5444 \.$Ident\s*=\s*|
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005445 ^\"|\"$|
5446 ^\[
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005447 }x;
5448 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Martin Rothedd591d2017-03-14 10:16:29 -06005449
5450 $ctx =~ s/\n*$//;
Martin Rothedd591d2017-03-14 10:16:29 -06005451 my $stmt_cnt = statement_rawlines($ctx);
Martin Roth60915b32018-08-10 21:04:05 -06005452 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Martin Rothedd591d2017-03-14 10:16:29 -06005453
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005454 if ($dstat ne '' &&
5455 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5456 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
5457 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5458 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
5459 $dstat !~ /$exceptions/ &&
5460 $dstat !~ /^\.$Ident\s*=/ && # .foo =
5461 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
5462 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Elyes Haouas26e0b942022-05-22 11:11:11 +02005463 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005464 $dstat !~ /^for\s*$Constant$/ && # for (...)
5465 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
Martin Rothedd591d2017-03-14 10:16:29 -06005466 $dstat !~ /^do\s*{/ && # do {...
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005467 $dstat !~ /^\(\{/ && # ({...
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005468 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5469 {
Martin Roth387dec82017-09-17 19:20:46 -06005470 if ($dstat =~ /^\s*if\b/) {
5471 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5472 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5473 } elsif ($dstat =~ /;/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005474 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5475 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5476 } else {
5477 ERROR("COMPLEX_MACRO",
5478 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5479 }
Martin Rothedd591d2017-03-14 10:16:29 -06005480
5481 }
5482
5483 # Make $define_stmt single line, comment-free, etc
5484 my @stmt_array = split('\n', $define_stmt);
5485 my $first = 1;
5486 $define_stmt = "";
5487 foreach my $l (@stmt_array) {
5488 $l =~ s/\\$//;
5489 if ($first) {
5490 $define_stmt = $l;
5491 $first = 0;
5492 } elsif ($l =~ /^[\+ ]/) {
5493 $define_stmt .= substr($l, 1);
5494 }
5495 }
5496 $define_stmt =~ s/$;//g;
5497 $define_stmt =~ s/\s+/ /g;
5498 $define_stmt = trim($define_stmt);
5499
5500# check if any macro arguments are reused (ignore '...' and 'type')
5501 foreach my $arg (@def_args) {
5502 next if ($arg =~ /\.\.\./);
5503 next if ($arg =~ /^type$/i);
Martin Roth387dec82017-09-17 19:20:46 -06005504 my $tmp_stmt = $define_stmt;
Elyes Haouas26e0b942022-05-22 11:11:11 +02005505 $tmp_stmt =~ s/\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Martin Roth387dec82017-09-17 19:20:46 -06005506 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5507 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Martin Roth60915b32018-08-10 21:04:05 -06005508 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Martin Rothedd591d2017-03-14 10:16:29 -06005509 if ($use_cnt > 1) {
5510 CHK("MACRO_ARG_REUSE",
5511 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5512 }
5513# check if any macro arguments may have other precedence issues
Martin Roth387dec82017-09-17 19:20:46 -06005514 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Martin Rothedd591d2017-03-14 10:16:29 -06005515 ((defined($1) && $1 ne ',') ||
5516 (defined($2) && $2 ne ','))) {
5517 CHK("MACRO_ARG_PRECEDENCE",
5518 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
5519 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005520 }
5521
5522# check for macros with flow control, but without ## concatenation
5523# ## concatenation is commonly a macro that defines a function so ignore those
5524 if ($has_flow_statement && !$has_arg_concat) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005525 my $cnt = statement_rawlines($ctx);
Martin Roth60915b32018-08-10 21:04:05 -06005526 my $herectx = get_stat_here($linenr, $cnt, $here);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005527
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005528 WARN("MACRO_WITH_FLOW_CONTROL",
5529 "Macros with flow control statements should be avoided\n" . "$herectx");
5530 }
5531
5532# check for line continuations outside of #defines, preprocessor #, and asm
5533
5534 } else {
5535 if ($prevline !~ /^..*\\$/ &&
5536 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5537 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
5538 $line =~ /^\+.*\\$/) {
5539 WARN("LINE_CONTINUATIONS",
5540 "Avoid unnecessary line continuations\n" . $herecurr);
5541 }
5542 }
5543
5544# do {} while (0) macro tests:
5545# single-statement macros do not need to be enclosed in do while (0) loop,
5546# macro should not end with a semicolon
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01005547 if ($perl_version_ok &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005548 $realfile !~ m@/vmlinux.lds.h$@ &&
5549 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5550 my $ln = $linenr;
5551 my $cnt = $realcnt;
5552 my ($off, $dstat, $dcond, $rest);
5553 my $ctx = '';
5554 ($dstat, $dcond, $ln, $cnt, $off) =
5555 ctx_statement_block($linenr, $realcnt, 0);
5556 $ctx = $dstat;
5557
5558 $dstat =~ s/\\\n.//g;
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005559 $dstat =~ s/$;/ /g;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005560
5561 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5562 my $stmts = $2;
5563 my $semis = $3;
5564
5565 $ctx =~ s/\n*$//;
5566 my $cnt = statement_rawlines($ctx);
Martin Roth60915b32018-08-10 21:04:05 -06005567 my $herectx = get_stat_here($linenr, $cnt, $here);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005568
5569 if (($stmts =~ tr/;/;/) == 1 &&
5570 $stmts !~ /^\s*(if|while|for|switch)\b/) {
5571 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5572 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5573 }
5574 if (defined $semis && $semis ne "") {
5575 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5576 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5577 }
5578 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5579 $ctx =~ s/\n*$//;
5580 my $cnt = statement_rawlines($ctx);
Martin Roth60915b32018-08-10 21:04:05 -06005581 my $herectx = get_stat_here($linenr, $cnt, $here);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005582
5583 WARN("TRAILING_SEMICOLON",
5584 "macros should not use a trailing semicolon\n" . "$herectx");
5585 }
5586 }
5587
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005588# check for redundant bracing round if etc
5589 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5590 my ($level, $endln, @chunks) =
5591 ctx_statement_full($linenr, $realcnt, 1);
5592 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5593 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5594 if ($#chunks > 0 && $level == 0) {
5595 my @allowed = ();
5596 my $allow = 0;
5597 my $seen = 0;
5598 my $herectx = $here . "\n";
5599 my $ln = $linenr - 1;
5600 for my $chunk (@chunks) {
5601 my ($cond, $block) = @{$chunk};
5602
5603 # If the condition carries leading newlines, then count those as offsets.
5604 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5605 my $offset = statement_rawlines($whitespace) - 1;
5606
5607 $allowed[$allow] = 0;
5608 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5609
5610 # We have looked at and allowed this specific line.
5611 $suppress_ifbraces{$ln + $offset} = 1;
5612
5613 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5614 $ln += statement_rawlines($block) - 1;
5615
5616 substr($block, 0, length($cond), '');
5617
5618 $seen++ if ($block =~ /^\s*{/);
5619
5620 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5621 if (statement_lines($cond) > 1) {
5622 #print "APW: ALLOWED: cond<$cond>\n";
5623 $allowed[$allow] = 1;
5624 }
5625 if ($block =~/\b(?:if|for|while)\b/) {
5626 #print "APW: ALLOWED: block<$block>\n";
5627 $allowed[$allow] = 1;
5628 }
5629 if (statement_block_size($block) > 1) {
5630 #print "APW: ALLOWED: lines block<$block>\n";
5631 $allowed[$allow] = 1;
5632 }
5633 $allow++;
5634 }
5635 if ($seen) {
5636 my $sum_allowed = 0;
5637 foreach (@allowed) {
5638 $sum_allowed += $_;
5639 }
5640 if ($sum_allowed == 0) {
5641 WARN("BRACES",
5642 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5643 } elsif ($sum_allowed != $allow &&
5644 $seen != $allow) {
5645 CHK("BRACES",
5646 "braces {} should be used on all arms of this statement\n" . $herectx);
5647 }
5648 }
5649 }
5650 }
5651 if (!defined $suppress_ifbraces{$linenr - 1} &&
5652 $line =~ /\b(if|while|for|else)\b/) {
5653 my $allowed = 0;
5654
5655 # Check the pre-context.
5656 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5657 #print "APW: ALLOWED: pre<$1>\n";
5658 $allowed = 1;
5659 }
5660
5661 my ($level, $endln, @chunks) =
5662 ctx_statement_full($linenr, $realcnt, $-[0]);
5663
5664 # Check the condition.
5665 my ($cond, $block) = @{$chunks[0]};
5666 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5667 if (defined $cond) {
5668 substr($block, 0, length($cond), '');
5669 }
5670 if (statement_lines($cond) > 1) {
5671 #print "APW: ALLOWED: cond<$cond>\n";
5672 $allowed = 1;
5673 }
5674 if ($block =~/\b(?:if|for|while)\b/) {
5675 #print "APW: ALLOWED: block<$block>\n";
5676 $allowed = 1;
5677 }
5678 if (statement_block_size($block) > 1) {
5679 #print "APW: ALLOWED: lines block<$block>\n";
5680 $allowed = 1;
5681 }
5682 # Check the post-context.
5683 if (defined $chunks[1]) {
5684 my ($cond, $block) = @{$chunks[1]};
5685 if (defined $cond) {
5686 substr($block, 0, length($cond), '');
5687 }
5688 if ($block =~ /^\s*\{/) {
5689 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5690 $allowed = 1;
5691 }
5692 }
5693 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005694 my $cnt = statement_rawlines($block);
Martin Roth60915b32018-08-10 21:04:05 -06005695 my $herectx = get_stat_here($linenr, $cnt, $here);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005696
5697 WARN("BRACES",
5698 "braces {} are not necessary for single statement blocks\n" . $herectx);
5699 }
5700 }
5701
Martin Rothedd591d2017-03-14 10:16:29 -06005702# check for single line unbalanced braces
5703 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5704 $sline =~ /^.\s*else\s*\{\s*$/) {
5705 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5706 }
5707
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005708# check for unnecessary blank lines around braces
5709 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005710 if (CHK("BRACES",
5711 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5712 $fix && $prevrawline =~ /^\+/) {
5713 fix_delete_line($fixlinenr - 1, $prevrawline);
5714 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005715 }
5716 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005717 if (CHK("BRACES",
5718 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5719 $fix) {
5720 fix_delete_line($fixlinenr, $rawline);
5721 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005722 }
5723
5724# no volatiles please
5725 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5726 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5727 WARN("VOLATILE",
Martin Rothedd591d2017-03-14 10:16:29 -06005728 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005729 }
5730
5731# Check for user-visible strings broken across lines, which breaks the ability
5732# to grep for the string. Make exceptions when the previous string ends in a
5733# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5734# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005735 if ($line =~ /^\+\s*$String/ &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005736 $prevline =~ /"\s*$/ &&
5737 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5738 if (WARN("SPLIT_STRING",
5739 "quoted string split across lines\n" . $hereprev) &&
5740 $fix &&
5741 $prevrawline =~ /^\+.*"\s*$/ &&
5742 $last_coalesced_string_linenr != $linenr - 1) {
5743 my $extracted_string = get_quoted_string($line, $rawline);
5744 my $comma_close = "";
5745 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5746 $comma_close = $1;
5747 }
5748
5749 fix_delete_line($fixlinenr - 1, $prevrawline);
5750 fix_delete_line($fixlinenr, $rawline);
5751 my $fixedline = $prevrawline;
5752 $fixedline =~ s/"\s*$//;
5753 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5754 fix_insert_line($fixlinenr - 1, $fixedline);
5755 $fixedline = $rawline;
5756 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5757 if ($fixedline !~ /\+\s*$/) {
5758 fix_insert_line($fixlinenr, $fixedline);
5759 }
5760 $last_coalesced_string_linenr = $linenr;
5761 }
5762 }
5763
5764# check for missing a space in a string concatenation
5765 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5766 WARN('MISSING_SPACE',
5767 "break quoted strings at a space character\n" . $hereprev);
5768 }
5769
Martin Roth387dec82017-09-17 19:20:46 -06005770# check for an embedded function name in a string when the function is known
5771# This does not work very well for -f --file checking as it depends on patch
5772# context providing the function name or a single line form for in-file
5773# function declarations
Martin Rothedd591d2017-03-14 10:16:29 -06005774 if ($line =~ /^\+.*$String/ &&
5775 defined($context_function) &&
Martin Roth387dec82017-09-17 19:20:46 -06005776 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5777 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Martin Rothedd591d2017-03-14 10:16:29 -06005778 WARN("EMBEDDED_FUNCTION_NAME",
Martin Roth387dec82017-09-17 19:20:46 -06005779 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Martin Rothedd591d2017-03-14 10:16:29 -06005780 }
5781
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005782# check for spaces before a quoted newline
5783 if ($rawline =~ /^.*\".*\s\\n/) {
5784 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5785 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5786 $fix) {
5787 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5788 }
5789
5790 }
5791
5792# concatenated string without spaces between elements
Elyes Haouas71bfcf52022-07-13 16:51:20 +02005793 if ($line =~ /$String[A-Z_]/ ||
5794 ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) {
5795 if (CHK("CONCATENATED_STRING",
5796 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5797 $fix) {
5798 while ($line =~ /($String)/g) {
5799 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5800 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5801 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5802 }
5803 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005804 }
5805
5806# uncoalesced string fragments
Elyes Haouase235a0d2022-07-13 16:52:48 +02005807 if ($line =~ /$String\s*[Lu]?"/) {
5808 if (WARN("STRING_FRAGMENTS",
5809 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5810 $fix) {
5811 while ($line =~ /($String)(?=\s*")/g) {
5812 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5813 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5814 }
5815 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005816 }
5817
Martin Rothedd591d2017-03-14 10:16:29 -06005818# check for non-standard and hex prefixed decimal printf formats
5819 my $show_L = 1; #don't show the same defect twice
5820 my $show_Z = 1;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005821 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Martin Rothedd591d2017-03-14 10:16:29 -06005822 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005823 $string =~ s/%%/__/g;
Martin Rothedd591d2017-03-14 10:16:29 -06005824 # check for %L
5825 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005826 WARN("PRINTF_L",
Martin Rothedd591d2017-03-14 10:16:29 -06005827 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5828 $show_L = 0;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005829 }
Martin Rothedd591d2017-03-14 10:16:29 -06005830 # check for %Z
5831 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5832 WARN("PRINTF_Z",
5833 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5834 $show_Z = 0;
5835 }
5836 # check for 0x<decimal>
5837 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5838 ERROR("PRINTF_0XDECIMAL",
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005839 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5840 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005841 }
5842
5843# check for line continuations in quoted strings with odd counts of "
Martin Roth60915b32018-08-10 21:04:05 -06005844 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005845 WARN("LINE_CONTINUATIONS",
5846 "Avoid line continuations in quoted strings\n" . $herecurr);
5847 }
5848
5849# warn about #if 0
5850 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Elyes Haouas26e0b942022-05-22 11:11:11 +02005851 WARN("IF_0",
5852 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5853 }
5854
5855# warn about #if 1
5856 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5857 WARN("IF_1",
5858 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005859 }
5860
5861# check for needless "if (<foo>) fn(<foo>)" uses
5862 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005863 my $tested = quotemeta($1);
5864 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5865 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5866 my $func = $1;
5867 if (WARN('NEEDLESS_IF',
5868 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5869 $fix) {
5870 my $do_fix = 1;
5871 my $leading_tabs = "";
5872 my $new_leading_tabs = "";
5873 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5874 $leading_tabs = $1;
5875 } else {
5876 $do_fix = 0;
5877 }
5878 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5879 $new_leading_tabs = $1;
5880 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5881 $do_fix = 0;
5882 }
5883 } else {
5884 $do_fix = 0;
5885 }
5886 if ($do_fix) {
5887 fix_delete_line($fixlinenr - 1, $prevrawline);
5888 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5889 }
5890 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005891 }
5892 }
5893
5894# check for unnecessary "Out of Memory" messages
5895 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5896 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5897 (defined $1 || defined $3) &&
5898 $linenr > 3) {
5899 my $testval = $2;
5900 my $testline = $lines[$linenr - 3];
5901
5902 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5903# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5904
Elyes Haouasa7b0d382022-05-23 18:33:15 +02005905 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5906 $s !~ /\b__GFP_NOWARN\b/ ) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005907 WARN("OOM_MESSAGE",
5908 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5909 }
5910 }
5911
5912# check for logging functions with KERN_<LEVEL>
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005913 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005914 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5915 my $level = $1;
5916 if (WARN("UNNECESSARY_KERN_LEVEL",
5917 "Possible unnecessary $level\n" . $herecurr) &&
5918 $fix) {
5919 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5920 }
5921 }
5922
Martin Rothedd591d2017-03-14 10:16:29 -06005923# check for logging continuations
5924 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5925 WARN("LOGGING_CONTINUATION",
5926 "Avoid logging continuation uses where feasible\n" . $herecurr);
5927 }
5928
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005929# check for mask then right shift without a parentheses
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01005930 if ($perl_version_ok &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005931 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5932 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5933 WARN("MASK_THEN_SHIFT",
5934 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5935 }
5936
5937# check for pointer comparisons to NULL
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01005938 if ($perl_version_ok) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01005939 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5940 my $val = $1;
5941 my $equal = "!";
5942 $equal = "" if ($4 eq "!=");
5943 if (CHK("COMPARISON_TO_NULL",
5944 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5945 $fix) {
5946 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5947 }
5948 }
5949 }
5950
5951# check for bad placement of section $InitAttribute (e.g.: __initdata)
5952 if ($line =~ /(\b$InitAttribute\b)/) {
5953 my $attr = $1;
5954 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5955 my $ptr = $1;
5956 my $var = $2;
5957 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5958 ERROR("MISPLACED_INIT",
5959 "$attr should be placed after $var\n" . $herecurr)) ||
5960 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5961 WARN("MISPLACED_INIT",
5962 "$attr should be placed after $var\n" . $herecurr))) &&
5963 $fix) {
5964 $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;
5965 }
5966 }
5967 }
5968
5969# check for $InitAttributeData (ie: __initdata) with const
5970 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5971 my $attr = $1;
5972 $attr =~ /($InitAttributePrefix)(.*)/;
5973 my $attr_prefix = $1;
5974 my $attr_type = $2;
5975 if (ERROR("INIT_ATTRIBUTE",
5976 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5977 $fix) {
5978 $fixed[$fixlinenr] =~
5979 s/$InitAttributeData/${attr_prefix}initconst/;
5980 }
5981 }
5982
5983# check for $InitAttributeConst (ie: __initconst) without const
5984 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5985 my $attr = $1;
5986 if (ERROR("INIT_ATTRIBUTE",
5987 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5988 $fix) {
5989 my $lead = $fixed[$fixlinenr] =~
5990 /(^\+\s*(?:static\s+))/;
5991 $lead = rtrim($1);
5992 $lead = "$lead " if ($lead !~ /^\+$/);
5993 $lead = "${lead}const ";
5994 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5995 }
5996 }
5997
Stefan Reinauerc6080c62016-07-29 16:01:40 -07005998# check for __read_mostly with const non-pointer (should just be const)
5999 if ($line =~ /\b__read_mostly\b/ &&
6000 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6001 if (ERROR("CONST_READ_MOSTLY",
6002 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6003 $fix) {
6004 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6005 }
6006 }
6007
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006008# don't use __constant_<foo> functions outside of include/uapi/
6009 if ($realfile !~ m@^include/uapi/@ &&
6010 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6011 my $constant_func = $1;
6012 my $func = $constant_func;
6013 $func =~ s/^__constant_//;
6014 if (WARN("CONSTANT_CONVERSION",
6015 "$constant_func should be $func\n" . $herecurr) &&
6016 $fix) {
6017 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
6018 }
6019 }
6020
6021# prefer usleep_range over udelay
6022 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
6023 my $delay = $1;
6024 # ignore udelay's < 10, however
6025 if (! ($delay < 10) ) {
6026 CHK("USLEEP_RANGE",
6027 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
6028 }
6029 if ($delay > 2000) {
6030 WARN("LONG_UDELAY",
6031 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
6032 }
6033 }
6034
6035# warn about unexpectedly long msleep's
6036 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6037 if ($1 < 20) {
6038 WARN("MSLEEP",
6039 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
6040 }
6041 }
6042
6043# check for comparisons of jiffies
6044 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6045 WARN("JIFFIES_COMPARISON",
6046 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6047 }
6048
6049# check for comparisons of get_jiffies_64()
6050 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6051 WARN("JIFFIES_COMPARISON",
6052 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6053 }
6054
6055# warn about #ifdefs in C files
6056# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
6057# print "#ifdef in C files should be avoided\n";
6058# print "$herecurr";
6059# $clean = 0;
6060# }
6061
6062# warn about spacing in #ifdefs
6063 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
6064 if (ERROR("SPACING",
6065 "exactly one space required after that #$1\n" . $herecurr) &&
6066 $fix) {
6067 $fixed[$fixlinenr] =~
6068 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6069 }
6070
6071 }
6072
6073# check for spinlock_t definitions without a comment.
6074 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6075 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
6076 my $which = $1;
6077 if (!ctx_has_comment($first_line, $linenr)) {
6078 CHK("UNCOMMENTED_DEFINITION",
6079 "$1 definition without comment\n" . $herecurr);
6080 }
6081 }
6082# check for memory barriers without a comment.
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006083
6084 my $barriers = qr{
6085 mb|
6086 rmb|
Elyes Haouas26e0b942022-05-22 11:11:11 +02006087 wmb
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006088 }x;
6089 my $barrier_stems = qr{
6090 mb__before_atomic|
6091 mb__after_atomic|
6092 store_release|
6093 load_acquire|
6094 store_mb|
6095 (?:$barriers)
6096 }x;
6097 my $all_barriers = qr{
6098 (?:$barriers)|
6099 smp_(?:$barrier_stems)|
6100 virt_(?:$barrier_stems)
6101 }x;
6102
6103 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006104 if (!ctx_has_comment($first_line, $linenr)) {
6105 WARN("MEMORY_BARRIER",
6106 "memory barrier without comment\n" . $herecurr);
6107 }
6108 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006109
6110 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6111
6112 if ($realfile !~ m@^include/asm-generic/@ &&
6113 $realfile !~ m@/barrier\.h$@ &&
6114 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6115 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6116 WARN("MEMORY_BARRIER",
6117 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6118 }
6119
6120# check for waitqueue_active without a comment.
6121 if ($line =~ /\bwaitqueue_active\s*\(/) {
6122 if (!ctx_has_comment($first_line, $linenr)) {
6123 WARN("WAITQUEUE_ACTIVE",
6124 "waitqueue_active without comment\n" . $herecurr);
6125 }
6126 }
6127
Elyes Haouas26e0b942022-05-22 11:11:11 +02006128# check for data_race without a comment.
6129 if ($line =~ /\bdata_race\s*\(/) {
6130 if (!ctx_has_comment($first_line, $linenr)) {
6131 WARN("DATA_RACE",
6132 "data_race without comment\n" . $herecurr);
6133 }
Martin Roth60915b32018-08-10 21:04:05 -06006134 }
6135
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006136# check of hardware specific defines
6137 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6138 CHK("ARCH_DEFINES",
6139 "architecture specific defines should be avoided\n" . $herecurr);
6140 }
6141
Martin Roth387dec82017-09-17 19:20:46 -06006142# check that the storage class is not after a type
6143 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006144 WARN("STORAGE_CLASS",
Martin Roth387dec82017-09-17 19:20:46 -06006145 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6146 }
6147# Check that the storage class is at the beginning of a declaration
6148 if ($line =~ /\b$Storage\b/ &&
6149 $line !~ /^.\s*$Storage/ &&
6150 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6151 $1 !~ /[\,\)]\s*$/) {
6152 WARN("STORAGE_CLASS",
6153 "storage class should be at the beginning of the declaration\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006154 }
6155
6156# check the location of the inline attribute, that it is between
6157# storage class and type.
6158 if ($line =~ /\b$Type\s+$Inline\b/ ||
6159 $line =~ /\b$Inline\s+$Storage\b/) {
6160 ERROR("INLINE_LOCATION",
6161 "inline keyword should sit between storage class and type\n" . $herecurr);
6162 }
6163
6164# Check for __inline__ and __inline, prefer inline
6165 if ($realfile !~ m@\binclude/uapi/@ &&
6166 $line =~ /\b(__inline__|__inline)\b/) {
6167 if (WARN("INLINE",
6168 "plain inline is preferred over $1\n" . $herecurr) &&
6169 $fix) {
6170 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
6171
6172 }
6173 }
6174
Elyes Haouas069dfe32022-05-23 18:38:19 +02006175# Check for compiler attributes
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006176 if ($realfile !~ m@\binclude/uapi/@ &&
Elyes Haouas069dfe32022-05-23 18:38:19 +02006177 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6178 my $attr = $1;
6179 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006180
Elyes Haouas069dfe32022-05-23 18:38:19 +02006181 my %attr_list = (
6182 "alias" => "__alias",
6183 "aligned" => "__aligned",
6184 "always_inline" => "__always_inline",
6185 "assume_aligned" => "__assume_aligned",
6186 "cold" => "__cold",
6187 "const" => "__attribute_const__",
6188 "copy" => "__copy",
6189 "designated_init" => "__designated_init",
6190 "externally_visible" => "__visible",
6191 "format" => "printf|scanf",
6192 "gnu_inline" => "__gnu_inline",
6193 "malloc" => "__malloc",
6194 "mode" => "__mode",
6195 "no_caller_saved_registers" => "__no_caller_saved_registers",
6196 "noclone" => "__noclone",
6197 "noinline" => "noinline",
6198 "nonstring" => "__nonstring",
6199 "noreturn" => "__noreturn",
6200 "packed" => "__packed",
6201 "pure" => "__pure",
6202 "section" => "__section",
6203 "used" => "__used",
6204 "weak" => "__weak"
6205 );
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006206
Elyes Haouas069dfe32022-05-23 18:38:19 +02006207 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
6208 my $orig_attr = $1;
6209 my $params = '';
6210 $params = $2 if defined($2);
6211 my $curr_attr = $orig_attr;
6212 $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
6213 if (exists($attr_list{$curr_attr})) {
6214 my $new = $attr_list{$curr_attr};
6215 if ($curr_attr eq "format" && $params) {
6216 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
6217 $new = "__$1\($2";
6218 } else {
6219 $new = "$new$params";
6220 }
6221 if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6222 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
6223 $fix) {
6224 my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6225 $fixed[$fixlinenr] =~ s/$remove//;
6226 $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6227 $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6228 $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
6229 }
6230 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006231 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006232
Elyes Haouas069dfe32022-05-23 18:38:19 +02006233 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6234 if ($attr =~ /^_*unused/) {
6235 WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6236 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006237 }
6238 }
6239
6240# Check for __attribute__ weak, or __weak declarations (may have link issues)
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006241 if ($perl_version_ok &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006242 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6243 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6244 $line =~ /\b__weak\b/)) {
6245 ERROR("WEAK_DECLARATION",
6246 "Using weak declarations can have unintended link defects\n" . $herecurr);
6247 }
6248
Martin Rothedd591d2017-03-14 10:16:29 -06006249# check for c99 types like uint8_t used outside of uapi/ and tools/
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006250 if ($realfile !~ m@\binclude/uapi/@ &&
Martin Rothedd591d2017-03-14 10:16:29 -06006251 $realfile !~ m@\btools/@ &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006252 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6253 my $type = $1;
6254 if ($type =~ /\b($typeC99Typedefs)\b/) {
6255 $type = $1;
6256 my $kernel_type = 'u';
6257 $kernel_type = 's' if ($type =~ /^_*[si]/);
6258 $type =~ /(\d+)/;
6259 $kernel_type .= $1;
6260 if (CHK("PREFER_KERNEL_TYPES",
6261 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6262 $fix) {
6263 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6264 }
6265 }
6266 }
6267
6268# check for cast of C90 native int or longer types constants
6269 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6270 my $cast = $1;
6271 my $const = $2;
Elyes Haouas00d8ffd2022-04-12 12:50:02 +02006272 my $suffix = "";
6273 my $newconst = $const;
6274 $newconst =~ s/${Int_type}$//;
6275 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6276 if ($cast =~ /\blong\s+long\b/) {
6277 $suffix .= 'LL';
6278 } elsif ($cast =~ /\blong\b/) {
6279 $suffix .= 'L';
6280 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006281 if (WARN("TYPECAST_INT_CONSTANT",
Elyes Haouas00d8ffd2022-04-12 12:50:02 +02006282 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006283 $fix) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006284 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6285 }
6286 }
6287
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006288# check for sizeof(&)
6289 if ($line =~ /\bsizeof\s*\(\s*\&/) {
6290 WARN("SIZEOF_ADDRESS",
6291 "sizeof(& should be avoided\n" . $herecurr);
6292 }
6293
6294# check for sizeof without parenthesis
6295 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6296 if (WARN("SIZEOF_PARENTHESIS",
6297 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6298 $fix) {
6299 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6300 }
6301 }
6302
6303# check for struct spinlock declarations
6304 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6305 WARN("USE_SPINLOCK_T",
6306 "struct spinlock should be spinlock_t\n" . $herecurr);
6307 }
6308
6309# check for seq_printf uses that could be seq_puts
6310 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6311 my $fmt = get_quoted_string($line, $rawline);
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006312 $fmt =~ s/%%//g;
6313 if ($fmt !~ /%/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006314 if (WARN("PREFER_SEQ_PUTS",
6315 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6316 $fix) {
6317 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6318 }
6319 }
6320 }
6321
Martin Roth60915b32018-08-10 21:04:05 -06006322# check for vsprintf extension %p<foo> misuses
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006323 if ($perl_version_ok &&
Martin Roth387dec82017-09-17 19:20:46 -06006324 defined $stat &&
6325 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6326 $1 !~ /^_*volatile_*$/) {
Martin Roth60915b32018-08-10 21:04:05 -06006327 my $stat_real;
6328
Martin Roth387dec82017-09-17 19:20:46 -06006329 my $lc = $stat =~ tr@\n@@;
6330 $lc = $lc + $linenr;
6331 for (my $count = $linenr; $count <= $lc; $count++) {
Martin Roth60915b32018-08-10 21:04:05 -06006332 my $specifier;
6333 my $extension;
6334 my $bad_specifier = "";
Martin Roth387dec82017-09-17 19:20:46 -06006335 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6336 $fmt =~ s/%%//g;
Martin Roth60915b32018-08-10 21:04:05 -06006337
6338 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
6339 $specifier = $1;
6340 $extension = $2;
6341 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
6342 $bad_specifier = $specifier;
6343 last;
6344 }
6345 if ($extension eq "x" && !defined($stat_real)) {
6346 if (!defined($stat_real)) {
6347 $stat_real = get_stat_real($linenr, $lc);
6348 }
6349 WARN("VSPRINTF_SPECIFIER_PX",
6350 "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");
6351 }
Martin Roth387dec82017-09-17 19:20:46 -06006352 }
Martin Roth60915b32018-08-10 21:04:05 -06006353 if ($bad_specifier ne "") {
6354 my $stat_real = get_stat_real($linenr, $lc);
6355 my $ext_type = "Invalid";
6356 my $use = "";
6357 if ($bad_specifier =~ /p[Ff]/) {
6358 $ext_type = "Deprecated";
6359 $use = " - use %pS instead";
6360 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6361 }
6362
6363 WARN("VSPRINTF_POINTER_EXTENSION",
6364 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
Martin Roth387dec82017-09-17 19:20:46 -06006365 }
Martin Roth387dec82017-09-17 19:20:46 -06006366 }
6367 }
6368
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006369# Check for misused memsets
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006370 if ($perl_version_ok &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006371 defined $stat &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006372 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006373
6374 my $ms_addr = $2;
6375 my $ms_val = $7;
6376 my $ms_size = $12;
6377
6378 if ($ms_size =~ /^(0x|)0$/i) {
6379 ERROR("MEMSET",
6380 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6381 } elsif ($ms_size =~ /^(0x|)1$/i) {
6382 WARN("MEMSET",
6383 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6384 }
6385 }
6386
6387# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006388# if ($perl_version_ok &&
Martin Rothedd591d2017-03-14 10:16:29 -06006389# defined $stat &&
6390# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6391# if (WARN("PREFER_ETHER_ADDR_COPY",
6392# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6393# $fix) {
6394# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6395# }
6396# }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006397
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006398# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006399# if ($perl_version_ok &&
Martin Rothedd591d2017-03-14 10:16:29 -06006400# defined $stat &&
6401# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6402# WARN("PREFER_ETHER_ADDR_EQUAL",
6403# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6404# }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006405
6406# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6407# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006408# if ($perl_version_ok &&
Martin Rothedd591d2017-03-14 10:16:29 -06006409# defined $stat &&
6410# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6411#
6412# my $ms_val = $7;
6413#
6414# if ($ms_val =~ /^(?:0x|)0+$/i) {
6415# if (WARN("PREFER_ETH_ZERO_ADDR",
6416# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6417# $fix) {
6418# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6419# }
6420# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6421# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6422# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6423# $fix) {
6424# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6425# }
6426# }
6427# }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006428
Elyes Haouasf0e150c2022-04-12 13:12:19 +02006429# strlcpy uses that should likely be strscpy
6430 if ($line =~ /\bstrlcpy\s*\(/) {
6431 WARN("STRLCPY",
6432 "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr);
6433 }
6434
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006435# typecasts on min/max could be min_t/max_t
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006436 if ($perl_version_ok &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006437 defined $stat &&
6438 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6439 if (defined $2 || defined $7) {
6440 my $call = $1;
6441 my $cast1 = deparenthesize($2);
6442 my $arg1 = $3;
6443 my $cast2 = deparenthesize($7);
6444 my $arg2 = $8;
6445 my $cast;
6446
6447 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6448 $cast = "$cast1 or $cast2";
6449 } elsif ($cast1 ne "") {
6450 $cast = $cast1;
6451 } else {
6452 $cast = $cast2;
6453 }
6454 WARN("MINMAX",
6455 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6456 }
6457 }
6458
6459# check usleep_range arguments
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006460 if ($perl_version_ok &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006461 defined $stat &&
6462 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6463 my $min = $1;
6464 my $max = $7;
6465 if ($min eq $max) {
6466 WARN("USLEEP_RANGE",
6467 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6468 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6469 $min > $max) {
6470 WARN("USLEEP_RANGE",
6471 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6472 }
6473 }
6474
6475# check for naked sscanf
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006476 if ($perl_version_ok &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006477 defined $stat &&
6478 $line =~ /\bsscanf\b/ &&
6479 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6480 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6481 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6482 my $lc = $stat =~ tr@\n@@;
6483 $lc = $lc + $linenr;
Martin Roth60915b32018-08-10 21:04:05 -06006484 my $stat_real = get_stat_real($linenr, $lc);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006485 WARN("NAKED_SSCANF",
6486 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6487 }
6488
6489# check for simple sscanf that should be kstrto<foo>
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006490 if ($perl_version_ok &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006491 defined $stat &&
6492 $line =~ /\bsscanf\b/) {
6493 my $lc = $stat =~ tr@\n@@;
6494 $lc = $lc + $linenr;
Martin Roth60915b32018-08-10 21:04:05 -06006495 my $stat_real = get_stat_real($linenr, $lc);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006496 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6497 my $format = $6;
6498 my $count = $format =~ tr@%@%@;
6499 if ($count == 1 &&
6500 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6501 WARN("SSCANF_TO_KSTRTO",
6502 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6503 }
6504 }
6505 }
6506
6507# check for new externs in .h files.
6508 if ($realfile =~ /\.h$/ &&
6509 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6510 if (CHK("AVOID_EXTERNS",
6511 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
6512 $fix) {
6513 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
6514 }
6515 }
6516
6517# check for new externs in .c files.
6518 if ($realfile =~ /\.c$/ && defined $stat &&
6519 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6520 {
6521 my $function_name = $1;
6522 my $paren_space = $2;
6523
6524 my $s = $stat;
6525 if (defined $cond) {
6526 substr($s, 0, length($cond), '');
6527 }
Elyes Haouas26e0b942022-05-22 11:11:11 +02006528 if ($s =~ /^\s*;/)
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006529 {
6530 WARN("AVOID_EXTERNS",
6531 "externs should be avoided in .c files\n" . $herecurr);
6532 }
6533
6534 if ($paren_space =~ /\n/) {
6535 WARN("FUNCTION_ARGUMENTS",
6536 "arguments for function declarations should follow identifier\n" . $herecurr);
6537 }
6538
6539 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6540 $stat =~ /^.\s*extern\s+/)
6541 {
6542 WARN("AVOID_EXTERNS",
6543 "externs should be avoided in .c files\n" . $herecurr);
6544 }
6545
Martin Roth387dec82017-09-17 19:20:46 -06006546# check for function declarations that have arguments without identifier names
6547 if (defined $stat &&
Martin Roth60915b32018-08-10 21:04:05 -06006548 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Martin Rothedd591d2017-03-14 10:16:29 -06006549 $1 ne "void") {
6550 my $args = trim($1);
6551 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6552 my $arg = trim($1);
6553 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6554 WARN("FUNCTION_ARGUMENTS",
6555 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6556 }
6557 }
6558 }
6559
Martin Roth387dec82017-09-17 19:20:46 -06006560# check for function definitions
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006561 if ($perl_version_ok &&
Martin Roth387dec82017-09-17 19:20:46 -06006562 defined $stat &&
6563 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6564 $context_function = $1;
6565
6566# check for multiline function definition with misplaced open brace
6567 my $ok = 0;
6568 my $cnt = statement_rawlines($stat);
6569 my $herectx = $here . "\n";
6570 for (my $n = 0; $n < $cnt; $n++) {
6571 my $rl = raw_line($linenr, $n);
6572 $herectx .= $rl . "\n";
6573 $ok = 1 if ($rl =~ /^[ \+]\{/);
6574 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6575 last if $rl =~ /^[ \+].*\{/;
6576 }
6577 if (!$ok) {
6578 ERROR("OPEN_BRACE",
6579 "open brace '{' following function definitions go on the next line\n" . $herectx);
6580 }
6581 }
6582
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006583# checks for new __setup's
6584 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6585 my $name = $1;
6586
6587 if (!grep(/$name/, @setup_docs)) {
6588 CHK("UNDOCUMENTED_SETUP",
Martin Rothedd591d2017-03-14 10:16:29 -06006589 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006590 }
6591 }
6592
Elyes Haouasa7b0d382022-05-23 18:33:15 +02006593# check for pointless casting of alloc functions
6594 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006595 WARN("UNNECESSARY_CASTS",
6596 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
6597 }
6598
6599# alloc style
6600# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006601 if ($perl_version_ok &&
Elyes Haouas26e0b942022-05-22 11:11:11 +02006602 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006603 CHK("ALLOC_SIZEOF_STRUCT",
6604 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6605 }
6606
Elyes Haouas709fdb12022-05-28 10:03:10 +02006607# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006608 if ($perl_version_ok &&
Martin Roth387dec82017-09-17 19:20:46 -06006609 defined $stat &&
Elyes Haouas709fdb12022-05-28 10:03:10 +02006610 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006611 my $oldfunc = $3;
6612 my $a1 = $4;
6613 my $a2 = $10;
6614 my $newfunc = "kmalloc_array";
Elyes Haouas709fdb12022-05-28 10:03:10 +02006615 $newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc");
6616 $newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc");
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006617 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
6618 my $r1 = $a1;
6619 my $r2 = $a2;
6620 if ($a1 =~ /^sizeof\s*\S/) {
6621 $r1 = $a2;
6622 $r2 = $a1;
6623 }
6624 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6625 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Martin Roth387dec82017-09-17 19:20:46 -06006626 my $cnt = statement_rawlines($stat);
Martin Roth60915b32018-08-10 21:04:05 -06006627 my $herectx = get_stat_here($linenr, $cnt, $here);
6628
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006629 if (WARN("ALLOC_WITH_MULTIPLY",
Martin Roth387dec82017-09-17 19:20:46 -06006630 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6631 $cnt == 1 &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006632 $fix) {
Elyes Haouas709fdb12022-05-28 10:03:10 +02006633 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006634 }
6635 }
6636 }
6637
6638# check for krealloc arg reuse
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006639 if ($perl_version_ok &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006640 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6641 WARN("KREALLOC_ARG_REUSE",
6642 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6643 }
6644
6645# check for alloc argument mismatch
6646 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6647 WARN("ALLOC_ARRAY_ARGS",
6648 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6649 }
6650
6651# check for multiple semicolons
6652 if ($line =~ /;\s*;\s*$/) {
6653 if (WARN("ONE_SEMICOLON",
6654 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6655 $fix) {
6656 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6657 }
6658 }
6659
Martin Rothedd591d2017-03-14 10:16:29 -06006660# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6661 if ($realfile !~ m@^include/uapi/@ &&
6662 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006663 my $ull = "";
6664 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6665 if (CHK("BIT_MACRO",
6666 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6667 $fix) {
6668 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6669 }
6670 }
6671
Elyes Haouasf62a9892022-04-12 12:17:50 +02006672# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
6673 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
6674 WARN("IS_ENABLED_CONFIG",
6675 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
6676 }
6677
6678# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6679 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6680 my $config = $1;
6681 if (WARN("PREFER_IS_ENABLED",
6682 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
6683 $fix) {
6684 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006685 }
Elyes Haouasf62a9892022-04-12 12:17:50 +02006686 }
6687
6688# check for /* fallthrough */ like comment, prefer fallthrough;
6689 my @fallthroughs = (
6690 'fallthrough',
6691 '@fallthrough@',
6692 'lint -fallthrough[ \t]*',
6693 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6694 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6695 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6696 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6697 );
6698 if ($raw_comment ne '') {
6699 foreach my $ft (@fallthroughs) {
6700 if ($raw_comment =~ /$ft/) {
6701 my $msg_level = \&WARN;
6702 $msg_level = \&CHK if ($file);
6703 &{$msg_level}("PREFER_FALLTHROUGH",
6704 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6705 last;
6706 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006707 }
6708 }
6709
6710# check for switch/default statements without a break;
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006711 if ($perl_version_ok &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006712 defined $stat &&
6713 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006714 my $cnt = statement_rawlines($stat);
Martin Roth60915b32018-08-10 21:04:05 -06006715 my $herectx = get_stat_here($linenr, $cnt, $here);
6716
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006717 WARN("DEFAULT_NO_BREAK",
6718 "switch default: should use break\n" . $herectx);
6719 }
6720
6721# check for gcc specific __FUNCTION__
6722 if ($line =~ /\b__FUNCTION__\b/) {
6723 if (WARN("USE_FUNC",
6724 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6725 $fix) {
6726 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6727 }
6728 }
6729
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006730# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6731 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6732 ERROR("DATE_TIME",
6733 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6734 }
6735
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006736# check for use of yield()
6737 if ($line =~ /\byield\s*\(\s*\)/) {
6738 WARN("YIELD",
6739 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6740 }
6741
6742# check for comparisons against true and false
6743 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6744 my $lead = $1;
6745 my $arg = $2;
6746 my $test = $3;
6747 my $otype = $4;
6748 my $trail = $5;
6749 my $op = "!";
6750
6751 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6752
6753 my $type = lc($otype);
6754 if ($type =~ /^(?:true|false)$/) {
6755 if (("$test" eq "==" && "$type" eq "true") ||
6756 ("$test" eq "!=" && "$type" eq "false")) {
6757 $op = "";
6758 }
6759
6760 CHK("BOOL_COMPARISON",
6761 "Using comparison to $otype is error prone\n" . $herecurr);
6762
6763## maybe suggesting a correct construct would better
6764## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6765
6766 }
6767 }
6768
Martin Roth60915b32018-08-10 21:04:05 -06006769# check for bool bitfields
6770 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6771 WARN("BOOL_BITFIELD",
6772 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6773 }
6774
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006775# check for semaphores initialized locked
6776 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6777 WARN("CONSIDER_COMPLETION",
6778 "consider using a completion\n" . $herecurr);
6779 }
6780
6781# recommend kstrto* over simple_strto* and strict_strto*
6782 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6783 WARN("CONSIDER_KSTRTO",
6784 "$1 is obsolete, use k$3 instead\n" . $herecurr);
6785 }
6786
6787# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6788 if ($line =~ /^.\s*__initcall\s*\(/) {
6789 WARN("USE_DEVICE_INITCALL",
6790 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6791 }
6792
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006793# check for various structs that are normally const (ops, kgdb, device_tree)
Martin Roth387dec82017-09-17 19:20:46 -06006794# and avoid what seem like struct definitions 'struct foo {'
Elyes Haouas43529c82022-04-12 13:19:50 +02006795 if (defined($const_structs) &&
6796 $line !~ /\bconst\b/ &&
Martin Roth387dec82017-09-17 19:20:46 -06006797 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006798 WARN("CONST_STRUCT",
Martin Roth387dec82017-09-17 19:20:46 -06006799 "struct $1 should normally be const\n" . $herecurr);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006800 }
6801
6802# use of NR_CPUS is usually wrong
6803# ignore definitions of NR_CPUS and usage to define arrays as likely right
6804 if ($line =~ /\bNR_CPUS\b/ &&
6805 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6806 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6807 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6808 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6809 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6810 {
6811 WARN("NR_CPUS",
6812 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6813 }
6814
6815# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6816 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6817 ERROR("DEFINE_ARCH_HAS",
6818 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6819 }
6820
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006821# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006822 if ($perl_version_ok &&
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006823 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6824 WARN("LIKELY_MISUSE",
6825 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6826 }
6827
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006828# whine mightly about in_atomic
6829 if ($line =~ /\bin_atomic\s*\(/) {
6830 if ($realfile =~ m@^drivers/@) {
6831 ERROR("IN_ATOMIC",
6832 "do not use in_atomic in drivers\n" . $herecurr);
6833 } elsif ($realfile !~ m@^kernel/@) {
6834 WARN("IN_ATOMIC",
6835 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6836 }
6837 }
6838
Martin Rothedd591d2017-03-14 10:16:29 -06006839# check for mutex_trylock_recursive usage
6840 if ($line =~ /mutex_trylock_recursive/) {
6841 ERROR("LOCKING",
6842 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6843 }
6844
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006845# check for lockdep_set_novalidate_class
6846 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6847 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6848 if ($realfile !~ m@^kernel/lockdep@ &&
6849 $realfile !~ m@^include/linux/lockdep@ &&
6850 $realfile !~ m@^drivers/base/core@) {
6851 ERROR("LOCKDEP",
6852 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
6853 }
6854 }
6855
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006856 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6857 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006858 WARN("EXPORTED_WORLD_WRITABLE",
6859 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
6860 }
6861
Martin Roth60915b32018-08-10 21:04:05 -06006862# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6863# and whether or not function naming is typical and if
6864# DEVICE_ATTR permissions uses are unusual too
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006865 if ($perl_version_ok &&
Martin Roth60915b32018-08-10 21:04:05 -06006866 defined $stat &&
6867 $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*\)/) {
6868 my $var = $1;
6869 my $perms = $2;
6870 my $show = $3;
6871 my $store = $4;
6872 my $octal_perms = perms_to_octal($perms);
6873 if ($show =~ /^${var}_show$/ &&
6874 $store =~ /^${var}_store$/ &&
6875 $octal_perms eq "0644") {
6876 if (WARN("DEVICE_ATTR_RW",
6877 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6878 $fix) {
6879 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6880 }
6881 } elsif ($show =~ /^${var}_show$/ &&
6882 $store =~ /^NULL$/ &&
6883 $octal_perms eq "0444") {
6884 if (WARN("DEVICE_ATTR_RO",
6885 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6886 $fix) {
6887 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6888 }
6889 } elsif ($show =~ /^NULL$/ &&
6890 $store =~ /^${var}_store$/ &&
6891 $octal_perms eq "0200") {
6892 if (WARN("DEVICE_ATTR_WO",
6893 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6894 $fix) {
6895 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6896 }
6897 } elsif ($octal_perms eq "0644" ||
6898 $octal_perms eq "0444" ||
6899 $octal_perms eq "0200") {
6900 my $newshow = "$show";
6901 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6902 my $newstore = $store;
6903 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6904 my $rename = "";
6905 if ($show ne $newshow) {
6906 $rename .= " '$show' to '$newshow'";
6907 }
6908 if ($store ne $newstore) {
6909 $rename .= " '$store' to '$newstore'";
6910 }
6911 WARN("DEVICE_ATTR_FUNCTIONS",
6912 "Consider renaming function(s)$rename\n" . $herecurr);
6913 } else {
6914 WARN("DEVICE_ATTR_PERMS",
6915 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6916 }
6917 }
6918
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006919# Mode permission misuses where it seems decimal should be octal
6920# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Martin Roth60915b32018-08-10 21:04:05 -06006921# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6922# specific definition of not visible in sysfs.
6923# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6924# use the default permissions
Elyes HAOUAS48cb78b2022-01-29 07:42:58 +01006925 if ($perl_version_ok &&
Martin Rothedd591d2017-03-14 10:16:29 -06006926 defined $stat &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006927 $line =~ /$mode_perms_search/) {
6928 foreach my $entry (@mode_permission_funcs) {
6929 my $func = $entry->[0];
6930 my $arg_pos = $entry->[1];
6931
Martin Rothedd591d2017-03-14 10:16:29 -06006932 my $lc = $stat =~ tr@\n@@;
6933 $lc = $lc + $linenr;
Martin Roth60915b32018-08-10 21:04:05 -06006934 my $stat_real = get_stat_real($linenr, $lc);
Martin Rothedd591d2017-03-14 10:16:29 -06006935
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006936 my $skip_args = "";
6937 if ($arg_pos > 1) {
6938 $arg_pos--;
6939 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6940 }
Martin Rothedd591d2017-03-14 10:16:29 -06006941 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6942 if ($stat =~ /$test/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006943 my $val = $1;
6944 $val = $6 if ($skip_args ne "");
Martin Roth60915b32018-08-10 21:04:05 -06006945 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6946 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6947 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006948 ERROR("NON_OCTAL_PERMISSIONS",
Martin Rothedd591d2017-03-14 10:16:29 -06006949 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6950 }
6951 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006952 ERROR("EXPORTED_WORLD_WRITABLE",
Martin Rothedd591d2017-03-14 10:16:29 -06006953 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006954 }
6955 }
6956 }
6957 }
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006958
Martin Rothedd591d2017-03-14 10:16:29 -06006959# check for uses of S_<PERMS> that could be octal for readability
Martin Roth60915b32018-08-10 21:04:05 -06006960 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
6961 my $oval = $1;
6962 my $octal = perms_to_octal($oval);
Martin Rothedd591d2017-03-14 10:16:29 -06006963 if (WARN("SYMBOLIC_PERMS",
6964 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6965 $fix) {
Martin Roth60915b32018-08-10 21:04:05 -06006966 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Martin Rothedd591d2017-03-14 10:16:29 -06006967 }
6968 }
6969
Stefan Reinauerc6080c62016-07-29 16:01:40 -07006970# validate content of MODULE_LICENSE against list from include/linux/module.h
6971 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6972 my $extracted_string = get_quoted_string($line, $rawline);
6973 my $valid_licenses = qr{
6974 GPL|
6975 GPL\ v2|
6976 GPL\ and\ additional\ rights|
6977 Dual\ BSD/GPL|
6978 Dual\ MIT/GPL|
6979 Dual\ MPL/GPL|
6980 Proprietary
6981 }x;
6982 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6983 WARN("MODULE_LICENSE",
6984 "unknown module license " . $extracted_string . "\n" . $herecurr);
6985 }
6986 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01006987 }
6988
6989 # If we have no input at all, then there is nothing to report on
6990 # so just keep quiet.
6991 if ($#rawlines == -1) {
6992 exit(0);
6993 }
6994
6995 # In mailback mode only produce a report in the negative, for
6996 # things that appear to be patches.
6997 if ($mailback && ($clean == 1 || !$is_patch)) {
6998 exit(0);
6999 }
7000
Elyes HAOUAS919b0c72022-01-29 07:48:52 +01007001 # This is not a patch, and we are in 'no-patch' mode so
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007002 # just keep quiet.
7003 if (!$chk_patch && !$is_patch) {
7004 exit(0);
7005 }
7006
Martin Roth60915b32018-08-10 21:04:05 -06007007 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007008 ERROR("NOT_UNIFIED_DIFF",
7009 "Does not appear to be a unified-diff format patch\n");
7010 }
Elyes Haouas6e84c2c2022-07-13 17:35:43 +02007011 if ($is_patch && $has_commit_log && $chk_signoff) {
7012 if ($signoff == 0) {
7013 ERROR("MISSING_SIGN_OFF",
7014 "Missing Signed-off-by: line(s)\n");
7015 } elsif ($authorsignoff != 1) {
7016 # authorsignoff values:
7017 # 0 -> missing sign off
7018 # 1 -> sign off identical
7019 # 2 -> names and addresses match, comments mismatch
7020 # 3 -> addresses match, names different
7021 # 4 -> names match, addresses different
7022 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7023
7024 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7025
7026 if ($authorsignoff == 0) {
7027 ERROR("NO_AUTHOR_SIGN_OFF",
7028 "Missing Signed-off-by: line by nominal patch author '$author'\n");
7029 } elsif ($authorsignoff == 2) {
7030 CHK("FROM_SIGN_OFF_MISMATCH",
7031 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7032 } elsif ($authorsignoff == 3) {
7033 WARN("FROM_SIGN_OFF_MISMATCH",
7034 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7035 } elsif ($authorsignoff == 4) {
7036 WARN("FROM_SIGN_OFF_MISMATCH",
7037 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7038 } elsif ($authorsignoff == 5) {
7039 WARN("FROM_SIGN_OFF_MISMATCH",
7040 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7041 }
7042 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007043 }
7044
7045 print report_dump();
7046 if ($summary && !($clean == 1 && $quiet == 1)) {
7047 print "$filename " if ($summary_file);
7048 print "total: $cnt_error errors, $cnt_warn warnings, " .
7049 (($check)? "$cnt_chk checks, " : "") .
7050 "$cnt_lines lines checked\n";
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007051 }
7052
7053 if ($quiet == 0) {
Stefan Reinauerc6080c62016-07-29 16:01:40 -07007054 # If there were any defects found and not already fixing them
7055 if (!$clean and !$fix) {
7056 print << "EOM"
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007057
Stefan Reinauerc6080c62016-07-29 16:01:40 -07007058NOTE: For some of the reported defects, checkpatch may be able to
7059 mechanically convert to the typical style using --fix or --fix-inplace.
7060EOM
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007061 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007062 # If there were whitespace errors which cleanpatch can fix
7063 # then suggest that.
7064 if ($rpt_cleaners) {
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007065 $rpt_cleaners = 0;
Stefan Reinauerc6080c62016-07-29 16:01:40 -07007066 print << "EOM"
7067
7068NOTE: Whitespace errors detected.
7069 You may wish to use scripts/cleanpatch or scripts/cleanfile
7070EOM
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007071 }
7072 }
7073
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007074 if ($clean == 0 && $fix &&
7075 ("@rawlines" ne "@fixed" ||
7076 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
7077 my $newfile = $filename;
7078 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
7079 my $linecount = 0;
7080 my $f;
7081
7082 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7083
7084 open($f, '>', $newfile)
7085 or die "$P: Can't open $newfile for write\n";
7086 foreach my $fixed_line (@fixed) {
7087 $linecount++;
7088 if ($file) {
7089 if ($linecount > 3) {
7090 $fixed_line =~ s/^\+//;
7091 print $f $fixed_line . "\n";
7092 }
7093 } else {
7094 print $f $fixed_line . "\n";
7095 }
7096 }
7097 close($f);
7098
7099 if (!$quiet) {
7100 print << "EOM";
Stefan Reinauerc6080c62016-07-29 16:01:40 -07007101
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007102Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7103
7104Do _NOT_ trust the results written to this file.
7105Do _NOT_ submit these changes without inspecting them for correctness.
7106
7107This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7108No warranties, expressed or implied...
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007109EOM
7110 }
7111 }
7112
Stefan Reinauerc6080c62016-07-29 16:01:40 -07007113 if ($quiet == 0) {
7114 print "\n";
7115 if ($clean == 1) {
7116 print "$vname has no obvious style problems and is ready for submission.\n";
7117 } else {
7118 print "$vname has style problems, please review.\n";
7119 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007120 }
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01007121 return $clean;
7122}