blob: 54ab6dd320720f708e2d7878e201258ea746b391 [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Joe Perches882ea1d2018-06-07 17:04:33 -07002# SPDX-License-Identifier: GPL-2.0
3#
Dave Jonesdbf004d2010-01-12 16:59:52 -05004# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07005# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08006# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07007# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Joe Perches882ea1d2018-06-07 17:04:33 -07008# (c) 2010-2018 Joe Perches <joe@perches.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07009
10use strict;
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020011use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070012use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080013use File::Basename;
14use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070015use Term::ANSIColor qw(:constants);
Geert Uytterhoevencd261492018-08-21 21:57:40 -070016use Encode qw(decode encode);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080019my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070020
Joe Perches000d1cc12011-07-25 17:13:25 -070021my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070022
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
Dwaipayan Ray52178ce2021-02-26 15:08:26 +053026my $verbose = 0;
27my %verbose_messages = ();
28my %verbose_emitted = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070029my $tree = 1;
30my $chk_signoff = 1;
31my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070032my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080034my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070035my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070037my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070038my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070039my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070040my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080041my $summary = 1;
42my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080043my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070044my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070045my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070046my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080047my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070048my $root;
Joe Perches0f7f6352020-10-24 16:59:04 -070049my $gitroot = $ENV{'GIT_DIR'};
50$gitroot = ".git" if !defined($gitroot);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080051my %debug;
Joe Perches34456862013-07-03 15:05:34 -070052my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070053my %use_type = ();
54my @use = ();
55my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070056my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070057my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070058my $configuration_file = ".checkpatch.conf";
Joe Perchesbdc48fa2020-05-29 16:12:21 -070059my $max_line_length = 100;
Dave Hansend62a2012013-09-11 14:23:56 -070060my $ignore_perl_version = 0;
61my $minimum_perl_version = 5.10.0;
Wanwei Jiang3bdc8022023-02-02 15:49:10 +080062my $min_conf_desc_length = 2;
Kees Cook66b47b42014-10-13 15:51:57 -070063my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070064my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070065my $codespellfile = "/usr/share/codespell/dictionary.txt";
Lee Jonesabea1cb2023-12-15 09:36:19 +000066my $user_codespellfile = "";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070067my $conststructsfile = "$D/const_structs.checkpatch";
Dwaipayan Ray52178ce2021-02-26 15:08:26 +053068my $docsfile = "$D/../Documentation/dev-tools/checkpatch.rst";
Quentin Monnetced69da12020-08-11 18:35:13 -070069my $typedefsfile;
John Brooks737c0762017-07-10 15:52:24 -070070my $color = "auto";
Vadim Bendebury98005e82019-03-07 16:28:38 -080071my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
Joe Perchesdbbf8692019-09-25 16:46:52 -070072# git output parsing needs US English output, so first set backtick child process LANGUAGE
73my $git_command ='export LANGUAGE=en_US.UTF-8; git';
Antonio Borneo713a09d2020-04-06 20:11:07 -070074my $tabsize = 8;
Jerome Forissier3e89ad82020-10-15 20:11:49 -070075my ${CONFIG_} = "CONFIG_";
Hannes Eder77f5b102009-09-21 17:04:37 -070076
Lee Jonesabea1cb2023-12-15 09:36:19 +000077my %maybe_linker_symbol; # for externs in c exceptions, when seen in *vmlinux.lds.h
78
Hannes Eder77f5b102009-09-21 17:04:37 -070079sub help {
80 my ($exitcode) = @_;
81
82 print << "EOM";
83Usage: $P [OPTION]... [FILE]...
84Version: $V
85
86Options:
87 -q, --quiet quiet
Dwaipayan Ray52178ce2021-02-26 15:08:26 +053088 -v, --verbose verbose mode
Hannes Eder77f5b102009-09-21 17:04:37 -070089 --no-tree run without a kernel tree
90 --no-signoff do not check for 'Signed-off-by' line
91 --patch treat FILE as patchfile (default)
92 --emacs emacs compile window format
93 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070094 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070095 -g, --git treat FILE as a single commit or git revision range
96 single git commit with:
97 <rev>
98 <rev>^
99 <rev>~n
100 multiple git commits with:
101 <rev1>..<rev2>
102 <rev1>...<rev2>
103 <rev>-<count>
104 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -0700105 -f, --file treat FILE as regular source file
106 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -0700107 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -0700108 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -0700109 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700110 --show-types show the specific message type in the output
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700111 --max-line-length=n set the maximum line length, (default $max_line_length)
112 if exceeded, warn on patches
113 requires --strict for use with --file
Vadim Bendebury56193272014-10-13 15:51:48 -0700114 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700115 --tab-size=n set the number of spaces for tab (default $tabsize)
Hannes Eder77f5b102009-09-21 17:04:37 -0700116 --root=PATH PATH to the kernel tree root
117 --no-summary suppress the per-file summary
118 --mailback only produce a report in case of warnings/errors
119 --summary-file include the filename in summary
120 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
121 'values', 'possible', 'type', and 'attr' (default
122 is all off)
123 --test-only=WORD report only warnings/errors containing WORD
124 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700125 --fix EXPERIMENTAL - may create horrible results
126 If correctable single-line errors exist, create
127 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
128 with potential errors corrected to the preferred
129 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800130 --fix-inplace EXPERIMENTAL - may create horrible results
131 Is the same as --fix, but overwrites the input
132 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700133 --ignore-perl-version override checking of perl version. expect
134 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700135 --codespell Use the codespell dictionary for spelling/typos
Lee Jonesabea1cb2023-12-15 09:36:19 +0000136 (default:$codespellfile)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700137 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700138 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700139 --color[=WHEN] Use colors 'always', 'never', or only when output
140 is a terminal ('auto'). Default is 'auto'.
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700141 --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default
142 ${CONFIG_})
Hannes Eder77f5b102009-09-21 17:04:37 -0700143 -h, --help, --version display this help and exit
144
145When FILE is - read standard input.
146EOM
147
148 exit($exitcode);
149}
150
Joe Perches3beb42e2016-05-20 17:04:14 -0700151sub uniq {
152 my %seen;
153 return grep { !$seen{$_}++ } @_;
154}
155
156sub list_types {
157 my ($exitcode) = @_;
158
159 my $count = 0;
160
161 local $/ = undef;
162
163 open(my $script, '<', abs_path($P)) or
164 die "$P: Can't read '$P' $!\n";
165
166 my $text = <$script>;
167 close($script);
168
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530169 my %types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700170 # Also catch when type or level is passed through a variable
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530171 while ($text =~ /(?:(\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
172 if (defined($1)) {
173 if (exists($types{$2})) {
174 $types{$2} .= ",$1" if ($types{$2} ne $1);
175 } else {
176 $types{$2} = $1;
177 }
178 } else {
179 $types{$2} = "UNDETERMINED";
180 }
Joe Perches3beb42e2016-05-20 17:04:14 -0700181 }
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530182
Joe Perches3beb42e2016-05-20 17:04:14 -0700183 print("#\tMessage type\n\n");
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530184 if ($color) {
185 print(" ( Color coding: ");
186 print(RED . "ERROR" . RESET);
187 print(" | ");
188 print(YELLOW . "WARNING" . RESET);
189 print(" | ");
190 print(GREEN . "CHECK" . RESET);
191 print(" | ");
192 print("Multiple levels / Undetermined");
193 print(" )\n\n");
194 }
195
196 foreach my $type (sort keys %types) {
197 my $orig_type = $type;
198 if ($color) {
199 my $level = $types{$type};
200 if ($level eq "ERROR") {
201 $type = RED . $type . RESET;
202 } elsif ($level eq "WARN") {
203 $type = YELLOW . $type . RESET;
204 } elsif ($level eq "CHK") {
205 $type = GREEN . $type . RESET;
206 }
207 }
Joe Perches3beb42e2016-05-20 17:04:14 -0700208 print(++$count . "\t" . $type . "\n");
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530209 if ($verbose && exists($verbose_messages{$orig_type})) {
210 my $message = $verbose_messages{$orig_type};
211 $message =~ s/\n/\n\t/g;
212 print("\t" . $message . "\n\n");
213 }
Joe Perches3beb42e2016-05-20 17:04:14 -0700214 }
215
216 exit($exitcode);
217}
218
Joe Perches000d1cc12011-07-25 17:13:25 -0700219my $conf = which_conf($configuration_file);
220if (-f $conf) {
221 my @conf_args;
222 open(my $conffile, '<', "$conf")
223 or warn "$P: Can't find a readable $configuration_file file $!\n";
224
225 while (<$conffile>) {
226 my $line = $_;
227
228 $line =~ s/\s*\n?$//g;
229 $line =~ s/^\s*//g;
230 $line =~ s/\s+/ /g;
231
232 next if ($line =~ m/^\s*#/);
233 next if ($line =~ m/^\s*$/);
234
235 my @words = split(" ", $line);
236 foreach my $word (@words) {
237 last if ($word =~ m/^#/);
238 push (@conf_args, $word);
239 }
240 }
241 close($conffile);
242 unshift(@ARGV, @conf_args) if @conf_args;
243}
244
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530245sub load_docs {
246 open(my $docs, '<', "$docsfile")
247 or warn "$P: Can't read the documentation file $docsfile $!\n";
248
249 my $type = '';
250 my $desc = '';
251 my $in_desc = 0;
252
253 while (<$docs>) {
254 chomp;
255 my $line = $_;
256 $line =~ s/\s+$//;
257
258 if ($line =~ /^\s*\*\*(.+)\*\*$/) {
259 if ($desc ne '') {
260 $verbose_messages{$type} = trim($desc);
261 }
262 $type = $1;
263 $desc = '';
264 $in_desc = 1;
265 } elsif ($in_desc) {
266 if ($line =~ /^(?:\s{4,}|$)/) {
267 $line =~ s/^\s{4}//;
268 $desc .= $line;
269 $desc .= "\n";
270 } else {
271 $verbose_messages{$type} = trim($desc);
272 $type = '';
273 $desc = '';
274 $in_desc = 0;
275 }
276 }
277 }
278
279 if ($desc ne '') {
280 $verbose_messages{$type} = trim($desc);
281 }
282 close($docs);
283}
284
John Brooks737c0762017-07-10 15:52:24 -0700285# Perl's Getopt::Long allows options to take optional arguments after a space.
286# Prevent --color by itself from consuming other arguments
287foreach (@ARGV) {
288 if ($_ eq "--color" || $_ eq "-color") {
289 $_ = "--color=$color";
290 }
291}
292
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700293GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700294 'q|quiet+' => \$quiet,
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530295 'v|verbose!' => \$verbose,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700296 'tree!' => \$tree,
297 'signoff!' => \$chk_signoff,
298 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700299 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800300 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700301 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700302 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700303 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700304 'subjective!' => \$check,
305 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700306 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700307 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700308 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700309 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800310 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700311 'min-conf-desc-length=i' => \$min_conf_desc_length,
Antonio Borneo713a09d2020-04-06 20:11:07 -0700312 'tab-size=i' => \$tabsize,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700313 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800314 'summary!' => \$summary,
315 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800316 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700317 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800318 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700319 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800320 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700321 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700322 'codespell!' => \$codespell,
Lee Jonesabea1cb2023-12-15 09:36:19 +0000323 'codespellfile=s' => \$user_codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700324 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700325 'color=s' => \$color,
326 'no-color' => \$color, #keep old behaviors of -nocolor
327 'nocolor' => \$color, #keep old behaviors of -nocolor
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700328 'kconfig-prefix=s' => \${CONFIG_},
Hannes Eder77f5b102009-09-21 17:04:37 -0700329 'h|help' => \$help,
330 'version' => \$help
Lee Jonesabea1cb2023-12-15 09:36:19 +0000331) or $help = 2;
Hannes Eder77f5b102009-09-21 17:04:37 -0700332
Lee Jonesabea1cb2023-12-15 09:36:19 +0000333if ($user_codespellfile) {
334 # Use the user provided codespell file unconditionally
335 $codespellfile = $user_codespellfile;
336} elsif (!(-f $codespellfile)) {
337 # If /usr/share/codespell/dictionary.txt is not present, try to find it
338 # under codespell's install directory: <codespell_root>/data/dictionary.txt
339 if (($codespell || $help) && which("python3") ne "") {
340 my $python_codespell_dict = << "EOF";
341
342import os.path as op
343import codespell_lib
344codespell_dir = op.dirname(codespell_lib.__file__)
345codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt')
346print(codespell_file, end='')
347EOF
348
349 my $codespell_dict = `python3 -c "$python_codespell_dict" 2> /dev/null`;
350 $codespellfile = $codespell_dict if (-f $codespell_dict);
351 }
352}
353
354# $help is 1 if either -h, --help or --version is passed as option - exitcode: 0
355# $help is 2 if invalid option is passed - exitcode: 1
356help($help - 1) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700357
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530358die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
359die "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse);
360
361if ($color =~ /^[01]$/) {
362 $color = !$color;
363} elsif ($color =~ /^always$/i) {
364 $color = 1;
365} elsif ($color =~ /^never$/i) {
366 $color = 0;
367} elsif ($color =~ /^auto$/i) {
368 $color = (-t STDOUT);
369} else {
370 die "$P: Invalid color mode: $color\n";
371}
372
373load_docs() if ($verbose);
Joe Perches3beb42e2016-05-20 17:04:14 -0700374list_types(0) if ($list_types);
375
Joe Perches9624b8d2014-01-23 15:54:44 -0800376$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700377$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800378
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700379my $exit = 0;
380
Joe Perches5b579802018-08-21 21:57:33 -0700381my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700382if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700383 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700384 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700385 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700386}
387
Allen Hubbe45107ff2016-08-02 14:04:47 -0700388#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700389if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700390 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700391}
392
Antonio Borneo713a09d2020-04-06 20:11:07 -0700393# skip TAB size 1 to avoid additional checks on $tabsize - 1
Joe Perches32f30ca2020-06-04 16:50:40 -0700394die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
Antonio Borneo713a09d2020-04-06 20:11:07 -0700395
Joe Perches91bfe482013-09-11 14:23:59 -0700396sub hash_save_array_words {
397 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700398
Joe Perches91bfe482013-09-11 14:23:59 -0700399 my @array = split(/,/, join(',', @$arrayRef));
400 foreach my $word (@array) {
401 $word =~ s/\s*\n?$//g;
402 $word =~ s/^\s*//g;
403 $word =~ s/\s+/ /g;
404 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700405
Joe Perches91bfe482013-09-11 14:23:59 -0700406 next if ($word =~ m/^\s*#/);
407 next if ($word =~ m/^\s*$/);
408
409 $hashRef->{$word}++;
410 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700411}
412
Joe Perches91bfe482013-09-11 14:23:59 -0700413sub hash_show_words {
414 my ($hashRef, $prefix) = @_;
415
Joe Perches3c816e42015-06-25 15:03:29 -0700416 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700417 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700418 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700419 print " $word";
420 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700421 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700422 }
423}
424
425hash_save_array_words(\%ignore_type, \@ignore);
426hash_save_array_words(\%use_type, \@use);
427
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800428my $dbg_values = 0;
429my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700430my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700431my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800432for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800433 ## no critic
434 eval "\${dbg_$key} = '$debug{$key}';";
435 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800436}
437
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700438my $rpt_cleaners = 0;
439
Andy Whitcroft8905a672007-11-28 16:21:06 -0800440if ($terse) {
441 $emacs = 1;
442 $quiet++;
443}
444
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700445if ($tree) {
446 if (defined $root) {
447 if (!top_of_kernel_tree($root)) {
448 die "$P: $root: --root does not point at a valid tree\n";
449 }
450 } else {
451 if (top_of_kernel_tree('.')) {
452 $root = '.';
453 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
454 top_of_kernel_tree($1)) {
455 $root = $1;
456 }
457 }
458
459 if (!defined $root) {
Wanwei Jiang3bdc8022023-02-02 15:49:10 +0800460 # print "Must be run from the top-level dir. of a kernel tree\n";
461 # exit(2);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700462 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700463}
464
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700465my $emitted_corrupt = 0;
466
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700467our $Ident = qr{
468 [A-Za-z_][A-Za-z\d_]*
469 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
470 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700471our $Storage = qr{extern|static|asmlinkage};
472our $Sparse = qr{
473 __user|
474 __kernel|
475 __force|
476 __iomem|
477 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800478 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700479 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700480 __refconst|
481 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800482 __rcu|
483 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700484 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800485our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
486our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
487our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
488our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
489our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700490
Wolfram Sang52131292010-03-05 13:43:51 -0800491# Notes to $Attribute:
492# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700493our $Attribute = qr{
494 const|
Joe Perchesb5e87362021-02-25 17:21:40 -0800495 volatile|
Joe Perches03f1df72010-10-26 14:23:16 -0700496 __percpu|
497 __nocast|
498 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200499 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700500 __packed__|
501 __packed2__|
502 __naked|
503 __maybe_unused|
504 __always_unused|
505 __noreturn|
506 __used|
507 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800508 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700509 __noclone|
510 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700511 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700512 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700513 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700514 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700515 ____cacheline_aligned|
516 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800517 ____cacheline_internodealigned_in_smp|
Kees Cookff418042021-11-05 13:36:19 -0700518 __weak|
519 __alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\)
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700520 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700521our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700522our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700523our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
524our $Lval = qr{$Ident(?:$Member)*};
525
Joe Perches95e2c602013-07-03 15:05:20 -0700526our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
527our $Binary = qr{(?i)0b[01]+$Int_type?};
528our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
529our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700530our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesd2af5aa2021-09-07 19:59:51 -0700531our $String = qr{(?:\b[Lu])?"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800532our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
533our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
534our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800535our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700536our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800537our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700538our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700539our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700540our $Operators = qr{
541 <=|>=|==|!=|
542 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700543 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700544 }x;
545
Joe Perches91cb5192014-04-03 14:49:32 -0700546our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
547
Joe Perchesab7e23f2015-04-16 12:44:22 -0700548our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800549our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700550our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700551our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800552our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700553our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800554our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700555our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800556
Joe Perches15662b32011-10-31 17:13:12 -0700557our $NON_ASCII_UTF8 = qr{
558 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700559 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
560 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
561 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
562 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
563 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
564 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
565}x;
566
Joe Perches15662b32011-10-31 17:13:12 -0700567our $UTF8 = qr{
568 [\x09\x0A\x0D\x20-\x7E] # ASCII
569 | $NON_ASCII_UTF8
570}x;
571
Joe Perchese6176fa2015-06-25 15:02:49 -0700572our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800573our $typeOtherOSTypedefs = qr{(?x:
574 u_(?:char|short|int|long) | # bsd
575 u(?:nchar|short|int|long) # sysv
576)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700577our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700578 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700579 atomic_t
580)};
Lee Jonesabea1cb2023-12-15 09:36:19 +0000581our $typeStdioTypedefs = qr{(?x:
582 FILE
583)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700584our $typeTypedefs = qr{(?x:
585 $typeC99Typedefs\b|
586 $typeOtherOSTypedefs\b|
Lee Jonesabea1cb2023-12-15 09:36:19 +0000587 $typeKernelTypedefs\b|
588 $typeStdioTypedefs\b
Joe Perchese6176fa2015-06-25 15:02:49 -0700589)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700590
Joe Perches6d32f7a2015-11-06 16:31:37 -0800591our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
592
Joe Perches691e6692010-03-05 13:43:51 -0800593our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800594 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700595 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches87bd4992017-11-17 15:28:48 -0800596 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700597 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700598 panic|
Joe Perches06668722013-11-12 15:10:07 -0800599 MODULE_[A-Z_]+|
600 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800601)};
602
Joe Perchese29a70f2019-03-07 16:28:35 -0800603our $allocFunctions = qr{(?x:
604 (?:(?:devm_)?
Joe Perches58f02262021-02-25 17:22:01 -0800605 (?:kv|k|v)[czm]alloc(?:_array)?(?:_node)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800606 kstrdup(?:_const)? |
607 kmemdup(?:_nul)?) |
Christophe JAILLET461e1562020-04-20 18:13:58 -0700608 (?:\w+)?alloc_skb(?:_ip_align)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800609 # dev_alloc_skb/netdev_alloc_skb, et al
610 dma_alloc_coherent
611)};
612
Joe Perches20112472011-07-25 17:13:23 -0700613our $signature_tags = qr{(?xi:
614 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800615 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700616 Acked-by:|
617 Tested-by:|
618 Reviewed-by:|
619 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700620 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700621 To:|
622 Cc:
623)};
624
Lee Jonesabea1cb2023-12-15 09:36:19 +0000625our @link_tags = qw(Link Closes);
626
627#Create a search and print patterns for all these strings to be used directly below
628our $link_tags_search = "";
629our $link_tags_print = "";
630foreach my $entry (@link_tags) {
631 if ($link_tags_search ne "") {
632 $link_tags_search .= '|';
633 $link_tags_print .= ' or ';
634 }
635 $entry .= ':';
636 $link_tags_search .= $entry;
637 $link_tags_print .= "'$entry'";
638}
639$link_tags_search = "(?:${link_tags_search})";
640
Joe Perchesadb2da82021-02-25 17:21:50 -0800641our $tracing_logging_tags = qr{(?xi:
642 [=-]*> |
643 <[=-]* |
644 \[ |
645 \] |
646 start |
647 called |
648 entered |
649 entry |
650 enter |
651 in |
652 inside |
653 here |
654 begin |
655 exit |
656 end |
657 done |
658 leave |
659 completed |
660 out |
661 return |
662 [\.\!:\s]*
663)};
664
Aditya Srivastava831242a2020-12-15 20:45:12 -0800665sub edit_distance_min {
666 my (@arr) = @_;
667 my $len = scalar @arr;
668 if ((scalar @arr) < 1) {
669 # if underflow, return
670 return;
671 }
672 my $min = $arr[0];
673 for my $i (0 .. ($len-1)) {
674 if ($arr[$i] < $min) {
675 $min = $arr[$i];
676 }
677 }
678 return $min;
679}
680
681sub get_edit_distance {
682 my ($str1, $str2) = @_;
683 $str1 = lc($str1);
684 $str2 = lc($str2);
685 $str1 =~ s/-//g;
686 $str2 =~ s/-//g;
687 my $len1 = length($str1);
688 my $len2 = length($str2);
689 # two dimensional array storing minimum edit distance
690 my @distance;
691 for my $i (0 .. $len1) {
692 for my $j (0 .. $len2) {
693 if ($i == 0) {
694 $distance[$i][$j] = $j;
695 } elsif ($j == 0) {
696 $distance[$i][$j] = $i;
697 } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
698 $distance[$i][$j] = $distance[$i - 1][$j - 1];
699 } else {
700 my $dist1 = $distance[$i][$j - 1]; #insert distance
701 my $dist2 = $distance[$i - 1][$j]; # remove
702 my $dist3 = $distance[$i - 1][$j - 1]; #replace
703 $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
704 }
705 }
706 }
707 return $distance[$len1][$len2];
708}
709
710sub find_standard_signature {
711 my ($sign_off) = @_;
712 my @standard_signature_tags = (
713 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
714 'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
715 );
716 foreach my $signature (@standard_signature_tags) {
717 return $signature if (get_edit_distance($sign_off, $signature) <= 2);
718 }
719
720 return "";
721}
722
Lee Jonesabea1cb2023-12-15 09:36:19 +0000723our $obsolete_archives = qr{(?xi:
724 \Qfreedesktop.org/archives/dri-devel\E |
725 \Qlists.infradead.org\E |
726 \Qlkml.org\E |
727 \Qmail-archive.com\E |
728 \Qmailman.alsa-project.org/pipermail\E |
729 \Qmarc.info\E |
730 \Qozlabs.org/pipermail\E |
731 \Qspinics.net\E
732)};
733
Joe Perches18130872014-08-06 16:11:22 -0700734our @typeListMisordered = (
735 qr{char\s+(?:un)?signed},
736 qr{int\s+(?:(?:un)?signed\s+)?short\s},
737 qr{int\s+short(?:\s+(?:un)?signed)},
738 qr{short\s+int(?:\s+(?:un)?signed)},
739 qr{(?:un)?signed\s+int\s+short},
740 qr{short\s+(?:un)?signed},
741 qr{long\s+int\s+(?:un)?signed},
742 qr{int\s+long\s+(?:un)?signed},
743 qr{long\s+(?:un)?signed\s+int},
744 qr{int\s+(?:un)?signed\s+long},
745 qr{int\s+(?:un)?signed},
746 qr{int\s+long\s+long\s+(?:un)?signed},
747 qr{long\s+long\s+int\s+(?:un)?signed},
748 qr{long\s+long\s+(?:un)?signed\s+int},
749 qr{long\s+long\s+(?:un)?signed},
750 qr{long\s+(?:un)?signed},
751);
752
Andy Whitcroft8905a672007-11-28 16:21:06 -0800753our @typeList = (
754 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700755 qr{(?:(?:un)?signed\s+)?char},
756 qr{(?:(?:un)?signed\s+)?short\s+int},
757 qr{(?:(?:un)?signed\s+)?short},
758 qr{(?:(?:un)?signed\s+)?int},
759 qr{(?:(?:un)?signed\s+)?long\s+int},
760 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
761 qr{(?:(?:un)?signed\s+)?long\s+long},
762 qr{(?:(?:un)?signed\s+)?long},
763 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800764 qr{float},
765 qr{double},
766 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800767 qr{struct\s+$Ident},
768 qr{union\s+$Ident},
769 qr{enum\s+$Ident},
770 qr{${Ident}_t},
771 qr{${Ident}_handler},
772 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700773 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800774);
Joe Perches938224b2016-01-20 14:59:15 -0800775
776our $C90_int_types = qr{(?x:
777 long\s+long\s+int\s+(?:un)?signed|
778 long\s+long\s+(?:un)?signed\s+int|
779 long\s+long\s+(?:un)?signed|
780 (?:(?:un)?signed\s+)?long\s+long\s+int|
781 (?:(?:un)?signed\s+)?long\s+long|
782 int\s+long\s+long\s+(?:un)?signed|
783 int\s+(?:(?:un)?signed\s+)?long\s+long|
784
785 long\s+int\s+(?:un)?signed|
786 long\s+(?:un)?signed\s+int|
787 long\s+(?:un)?signed|
788 (?:(?:un)?signed\s+)?long\s+int|
789 (?:(?:un)?signed\s+)?long|
790 int\s+long\s+(?:un)?signed|
791 int\s+(?:(?:un)?signed\s+)?long|
792
793 int\s+(?:un)?signed|
794 (?:(?:un)?signed\s+)?int
795)};
796
Alex Dowad485ff232015-06-25 15:02:52 -0700797our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700798our @typeListWithAttr = (
799 @typeList,
800 qr{struct\s+$InitAttribute\s+$Ident},
801 qr{union\s+$InitAttribute\s+$Ident},
802);
803
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700804our @modifierList = (
805 qr{fastcall},
806);
Alex Dowad485ff232015-06-25 15:02:52 -0700807our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800808
Joe Perches24358802014-04-03 14:49:13 -0700809our @mode_permission_funcs = (
810 ["module_param", 3],
811 ["module_param_(?:array|named|string)", 4],
812 ["module_param_array_named", 5],
813 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
814 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700815 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
816 ["IIO_DEV_ATTR_[A-Z_]+", 1],
817 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
818 ["SENSOR_TEMPLATE(?:_2|)", 3],
819 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700820);
821
Joe Perches1a3dcf22020-08-11 18:35:16 -0700822my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
823
Joe Perches515a2352014-04-03 14:49:24 -0700824#Create a search pattern for all these functions to speed up a loop below
825our $mode_perms_search = "";
826foreach my $entry (@mode_permission_funcs) {
827 $mode_perms_search .= '|' if ($mode_perms_search ne "");
828 $mode_perms_search .= $entry->[0];
829}
Joe Perches00180462018-02-06 15:38:55 -0800830$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700831
Joe Perches9189c7e2018-09-07 15:26:18 -0700832our %deprecated_apis = (
833 "synchronize_rcu_bh" => "synchronize_rcu",
834 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
835 "call_rcu_bh" => "call_rcu",
836 "rcu_barrier_bh" => "rcu_barrier",
837 "synchronize_sched" => "synchronize_rcu",
838 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
839 "call_rcu_sched" => "call_rcu",
840 "rcu_barrier_sched" => "rcu_barrier",
841 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
842 "cond_synchronize_sched" => "cond_synchronize_rcu",
Lee Jonesabea1cb2023-12-15 09:36:19 +0000843 "kmap" => "kmap_local_page",
844 "kunmap" => "kunmap_local",
845 "kmap_atomic" => "kmap_local_page",
846 "kunmap_atomic" => "kunmap_local",
Joe Perches9189c7e2018-09-07 15:26:18 -0700847);
848
849#Create a search pattern for all these strings to speed up a loop below
850our $deprecated_apis_search = "";
851foreach my $entry (keys %deprecated_apis) {
852 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
853 $deprecated_apis_search .= $entry;
854}
855$deprecated_apis_search = "(?:${deprecated_apis_search})";
856
Joe Perchesb392c642015-04-16 12:44:16 -0700857our $mode_perms_world_writable = qr{
858 S_IWUGO |
859 S_IWOTH |
860 S_IRWXUGO |
861 S_IALLUGO |
862 0[0-7][0-7][2367]
863}x;
864
Joe Perchesf90774e2016-10-11 13:51:47 -0700865our %mode_permission_string_types = (
866 "S_IRWXU" => 0700,
867 "S_IRUSR" => 0400,
868 "S_IWUSR" => 0200,
869 "S_IXUSR" => 0100,
870 "S_IRWXG" => 0070,
871 "S_IRGRP" => 0040,
872 "S_IWGRP" => 0020,
873 "S_IXGRP" => 0010,
874 "S_IRWXO" => 0007,
875 "S_IROTH" => 0004,
876 "S_IWOTH" => 0002,
877 "S_IXOTH" => 0001,
878 "S_IRWXUGO" => 0777,
879 "S_IRUGO" => 0444,
880 "S_IWUGO" => 0222,
881 "S_IXUGO" => 0111,
882);
883
884#Create a search pattern for all these strings to speed up a loop below
885our $mode_perms_string_search = "";
886foreach my $entry (keys %mode_permission_string_types) {
887 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
888 $mode_perms_string_search .= $entry;
889}
Joe Perches00180462018-02-06 15:38:55 -0800890our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
891our $multi_mode_perms_string_search = qr{
892 ${single_mode_perms_string_search}
893 (?:\s*\|\s*${single_mode_perms_string_search})*
894}x;
895
896sub perms_to_octal {
897 my ($string) = @_;
898
899 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
900
901 my $val = "";
902 my $oval = "";
903 my $to = 0;
904 my $curpos = 0;
905 my $lastpos = 0;
906 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
907 $curpos = pos($string);
908 my $match = $2;
909 my $omatch = $1;
910 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
911 $lastpos = $curpos;
912 $to |= $mode_permission_string_types{$match};
913 $val .= '\s*\|\s*' if ($val ne "");
914 $val .= $match;
915 $oval .= $omatch;
916 }
917 $oval =~ s/^\s*\|\s*//;
918 $oval =~ s/\s*\|\s*$//;
919 return sprintf("%04o", $to);
920}
Joe Perchesf90774e2016-10-11 13:51:47 -0700921
Wolfram Sang7840a942010-08-09 17:20:57 -0700922our $allowed_asm_includes = qr{(?x:
923 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700924 memory|
925 time|
926 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700927)};
928# memory.h: ARM has a custom one
929
Kees Cook66b47b42014-10-13 15:51:57 -0700930# Load common spelling mistakes and build regular expression list.
931my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700932my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700933
Joe Perches36061e32014-12-10 15:51:43 -0800934if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800935 while (<$spelling>) {
936 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700937
Joe Perches36061e32014-12-10 15:51:43 -0800938 $line =~ s/\s*\n?$//g;
939 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700940
Joe Perches36061e32014-12-10 15:51:43 -0800941 next if ($line =~ m/^\s*#/);
942 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700943
Joe Perches36061e32014-12-10 15:51:43 -0800944 my ($suspect, $fix) = split(/\|\|/, $line);
945
Joe Perches36061e32014-12-10 15:51:43 -0800946 $spelling_fix{$suspect} = $fix;
947 }
948 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800949} else {
950 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700951}
Kees Cook66b47b42014-10-13 15:51:57 -0700952
Joe Perchesebfd7d62015-04-16 12:44:14 -0700953if ($codespell) {
954 if (open(my $spelling, '<', $codespellfile)) {
955 while (<$spelling>) {
956 my $line = $_;
957
958 $line =~ s/\s*\n?$//g;
959 $line =~ s/^\s*//g;
960
961 next if ($line =~ m/^\s*#/);
962 next if ($line =~ m/^\s*$/);
963 next if ($line =~ m/, disabled/i);
964
965 $line =~ s/,.*$//;
966
967 my ($suspect, $fix) = split(/->/, $line);
968
969 $spelling_fix{$suspect} = $fix;
970 }
971 close($spelling);
972 } else {
973 warn "No codespell typos will be found - file '$codespellfile': $!\n";
974 }
975}
976
977$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
978
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700979sub read_words {
980 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700981
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700982 if (open(my $words, '<', $file)) {
983 while (<$words>) {
984 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700985
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700986 $line =~ s/\s*\n?$//g;
987 $line =~ s/^\s*//g;
988
989 next if ($line =~ m/^\s*#/);
990 next if ($line =~ m/^\s*$/);
991 if ($line =~ /\s/) {
992 print("$file: '$line' invalid - ignored\n");
993 next;
994 }
995
Quentin Monnetced69da12020-08-11 18:35:13 -0700996 $$wordsRef .= '|' if (defined $$wordsRef);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700997 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700998 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700999 close($file);
1000 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -07001001 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -07001002
1003 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -07001004}
1005
Quentin Monnetced69da12020-08-11 18:35:13 -07001006my $const_structs;
1007if (show_type("CONST_STRUCT")) {
1008 read_words(\$const_structs, $conststructsfile)
1009 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
1010}
Jerome Forissier75ad8c52017-05-08 15:56:00 -07001011
Quentin Monnetced69da12020-08-11 18:35:13 -07001012if (defined($typedefsfile)) {
1013 my $typeOtherTypedefs;
Jerome Forissier75ad8c52017-05-08 15:56:00 -07001014 read_words(\$typeOtherTypedefs, $typedefsfile)
1015 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
Quentin Monnetced69da12020-08-11 18:35:13 -07001016 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
Jerome Forissier75ad8c52017-05-08 15:56:00 -07001017}
Jerome Forissier75ad8c52017-05-08 15:56:00 -07001018
Andy Whitcroft8905a672007-11-28 16:21:06 -08001019sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -07001020 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
1021 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -07001022 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -07001023 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001024 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -07001025 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -07001026 (?:$typeTypedefs\b)|
1027 (?:${all}\b)
1028 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001029 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -07001030 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -08001031 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -08001032 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07001033 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001034 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -08001035 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001036 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -08001037 }x;
Joe Perches18130872014-08-06 16:11:22 -07001038 $NonptrTypeMisordered = qr{
1039 (?:$Modifier\s+|const\s+)*
1040 (?:
1041 (?:${Misordered}\b)
1042 )
1043 (?:\s+$Modifier|\s+const)*
1044 }x;
Joe Perches8716de32013-09-11 14:24:05 -07001045 $NonptrTypeWithAttr = qr{
1046 (?:$Modifier\s+|const\s+)*
1047 (?:
1048 (?:typeof|__typeof__)\s*\([^\)]*\)|
1049 (?:$typeTypedefs\b)|
1050 (?:${allWithAttr}\b)
1051 )
1052 (?:\s+$Modifier|\s+const)*
1053 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001054 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001055 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -07001056 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001057 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -08001058 }x;
Joe Perches18130872014-08-06 16:11:22 -07001059 $TypeMisordered = qr{
1060 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -07001061 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -07001062 (?:\s+$Inline|\s+$Modifier)*
1063 }x;
Joe Perches91cb5192014-04-03 14:49:32 -07001064 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -07001065 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -08001066}
1067build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001068
Joe Perches7d2367a2011-07-25 17:13:22 -07001069our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001070
1071# Using $balanced_parens, $LvalOrFunc, or $FuncArg
1072# requires at least perl version v5.10.0
1073# Any use must be runtime checked with $^V
1074
1075our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -07001076our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -08001077our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -07001078
Joe Perchesf8422302014-08-06 16:11:31 -07001079our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -07001080 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -07001081 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Lee Jonesabea1cb2023-12-15 09:36:19 +00001082 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(|
1083 (?:$Storage\s+)?(?:XA_STATE|XA_STATE_ORDER)\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -07001084)};
1085
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08001086our %allow_repeated_words = (
1087 add => '',
1088 added => '',
1089 bad => '',
1090 be => '',
1091);
1092
Joe Perches7d2367a2011-07-25 17:13:22 -07001093sub deparenthesize {
1094 my ($string) = @_;
1095 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -07001096
1097 while ($string =~ /^\s*\(.*\)\s*$/) {
1098 $string =~ s@^\s*\(\s*@@;
1099 $string =~ s@\s*\)\s*$@@;
1100 }
1101
Joe Perches7d2367a2011-07-25 17:13:22 -07001102 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -07001103
Joe Perches7d2367a2011-07-25 17:13:22 -07001104 return $string;
1105}
1106
Joe Perches34456862013-07-03 15:05:34 -07001107sub seed_camelcase_file {
1108 my ($file) = @_;
1109
1110 return if (!(-f $file));
1111
1112 local $/;
1113
1114 open(my $include_file, '<', "$file")
1115 or warn "$P: Can't read '$file' $!\n";
1116 my $text = <$include_file>;
1117 close($include_file);
1118
1119 my @lines = split('\n', $text);
1120
1121 foreach my $line (@lines) {
1122 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
1123 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
1124 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -08001125 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
1126 $camelcase{$1} = 1;
1127 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
Joe Perches34456862013-07-03 15:05:34 -07001128 $camelcase{$1} = 1;
1129 }
1130 }
1131}
1132
Joe Perchescd28b112019-12-04 16:52:09 -08001133our %maintained_status = ();
1134
Joe Perches85b0ee12016-10-11 13:51:44 -07001135sub is_maintained_obsolete {
1136 my ($filename) = @_;
1137
Jerome Forissierf2c19c22016-12-12 16:46:23 -08001138 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -07001139
Joe Perchescd28b112019-12-04 16:52:09 -08001140 if (!exists($maintained_status{$filename})) {
1141 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
1142 }
Joe Perches85b0ee12016-10-11 13:51:44 -07001143
Joe Perchescd28b112019-12-04 16:52:09 -08001144 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -07001145}
1146
Joe Perches3b6e8ac2018-08-21 21:57:47 -07001147sub is_SPDX_License_valid {
1148 my ($license) = @_;
1149
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08001150 return 1 if (!$tree || which("python3") eq "" || !(-x "scripts/spdxcheck.py") || !(-e "$gitroot"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -07001151
Joe Perches56294112018-08-21 21:58:04 -07001152 my $root_path = abs_path($root);
Guenter Roeckf9363b32021-06-30 18:56:19 -07001153 my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -07001154 return 0 if ($status ne "");
1155 return 1;
1156}
1157
Joe Perches34456862013-07-03 15:05:34 -07001158my $camelcase_seeded = 0;
1159sub seed_camelcase_includes {
1160 return if ($camelcase_seeded);
1161
1162 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -07001163 my $camelcase_cache = "";
1164 my @include_files = ();
1165
1166 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -07001167
Joe Perches0f7f6352020-10-24 16:59:04 -07001168 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -07001169 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -07001170 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -07001171 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -07001172 } else {
Joe Perchesc707a812013-07-08 16:00:43 -07001173 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -07001174 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -07001175 @include_files = split('\n', $files);
1176 foreach my $file (@include_files) {
1177 my $date = POSIX::strftime("%Y%m%d%H%M",
1178 localtime((stat $file)[9]));
1179 $last_mod_date = $date if ($last_mod_date < $date);
1180 }
1181 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -07001182 }
Joe Perchesc707a812013-07-08 16:00:43 -07001183
1184 if ($camelcase_cache ne "" && -f $camelcase_cache) {
1185 open(my $camelcase_file, '<', "$camelcase_cache")
1186 or warn "$P: Can't read '$camelcase_cache' $!\n";
1187 while (<$camelcase_file>) {
1188 chomp;
1189 $camelcase{$_} = 1;
1190 }
1191 close($camelcase_file);
1192
1193 return;
1194 }
1195
Joe Perches0f7f6352020-10-24 16:59:04 -07001196 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -07001197 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -07001198 @include_files = split('\n', $files);
1199 }
1200
Joe Perches34456862013-07-03 15:05:34 -07001201 foreach my $file (@include_files) {
1202 seed_camelcase_file($file);
1203 }
Joe Perches351b2a12013-07-03 15:05:36 -07001204
Joe Perchesc707a812013-07-08 16:00:43 -07001205 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -07001206 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -07001207 open(my $camelcase_file, '>', "$camelcase_cache")
1208 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -07001209 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1210 print $camelcase_file ("$_\n");
1211 }
1212 close($camelcase_file);
1213 }
Joe Perches34456862013-07-03 15:05:34 -07001214}
1215
Joe Perchesf5f61322020-10-15 20:12:12 -07001216sub git_is_single_file {
1217 my ($filename) = @_;
1218
1219 return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1220
1221 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1222 my $count = $output =~ tr/\n//;
1223 return $count eq 1 && $output =~ m{^${filename}$};
1224}
1225
Joe Perchesd311cd42014-08-06 16:10:57 -07001226sub git_commit_info {
1227 my ($commit, $id, $desc) = @_;
1228
Joe Perches0f7f6352020-10-24 16:59:04 -07001229 return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
Joe Perchesd311cd42014-08-06 16:10:57 -07001230
Joe Perchesdbbf8692019-09-25 16:46:52 -07001231 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -07001232 $output =~ s/^\s*//gm;
1233 my @lines = split("\n", $output);
1234
Joe Perches0d7835f2015-02-13 14:38:35 -08001235 return ($id, $desc) if ($#lines < 0);
1236
Sean Christopherson5a7f4452019-09-25 16:46:49 -07001237 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -07001238# Maybe one day convert this block of bash into something that returns
1239# all matching commit ids, but it's very slow...
1240#
1241# echo "checking commits $1..."
1242# git rev-list --remotes | grep -i "^$1" |
1243# while read line ; do
1244# git log --format='%H %s' -1 $line |
1245# echo "commit $(cut -c 1-12,41-)"
1246# done
Joe Perches4ce9f972021-09-07 19:59:57 -07001247 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
1248 $lines[0] =~ /^fatal: bad object $commit/) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07001249 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -07001250 } else {
1251 $id = substr($lines[0], 0, 12);
1252 $desc = substr($lines[0], 41);
1253 }
1254
1255 return ($id, $desc);
1256}
1257
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001258$chk_signoff = 0 if ($file);
1259
Andy Whitcroft00df3442007-06-08 13:47:06 -07001260my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001261my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001262my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001263my @fixed_inserted = ();
1264my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001265my $fixlinenr = -1;
1266
Du, Changbin4a593c32016-05-20 17:04:16 -07001267# If input is git commits, extract all commits from the commit expressions.
1268# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
Joe Perches0f7f6352020-10-24 16:59:04 -07001269die "$P: No git repository found\n" if ($git && !-e "$gitroot");
Du, Changbin4a593c32016-05-20 17:04:16 -07001270
1271if ($git) {
1272 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001273 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001274 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001275 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1276 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001277 } elsif ($commit_expr =~ m/\.\./) {
1278 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001279 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001280 $git_range = "-1 $commit_expr";
1281 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001282 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001283 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001284 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1285 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001286 my $sha1 = $1;
1287 my $subject = $2;
1288 unshift(@commits, $sha1);
1289 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001290 }
1291 }
1292 die "$P: no git commits after extraction!\n" if (@commits == 0);
1293 @ARGV = @commits;
1294}
1295
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001296my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001297$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001298for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001299 my $FILE;
Joe Perchesf5f61322020-10-15 20:12:12 -07001300 my $is_git_file = git_is_single_file($filename);
1301 my $oldfile = $file;
1302 $file = 1 if ($is_git_file);
Du, Changbin4a593c32016-05-20 17:04:16 -07001303 if ($git) {
1304 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1305 die "$P: $filename: git format-patch failed - $!\n";
1306 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001307 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001308 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001309 } elsif ($filename eq '-') {
1310 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001311 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001312 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001313 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001314 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001315 if ($filename eq '-') {
1316 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001317 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001318 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001319 } else {
1320 $vname = $filename;
1321 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001322 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001323 chomp;
1324 push(@rawlines, $_);
Geert Uytterhoevenc7f574d2020-06-04 16:50:43 -07001325 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001326 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001327 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001328
1329 if ($#ARGV > 0 && $quiet == 0) {
1330 print '-' x length($vname) . "\n";
1331 print "$vname\n";
1332 print '-' x length($vname) . "\n";
1333 }
1334
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001335 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001336 $exit = 1;
1337 }
1338 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001339 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001340 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001341 @fixed_inserted = ();
1342 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001343 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001344 @modifierListFile = ();
1345 @typeListFile = ();
1346 build_types();
Joe Perchesf5f61322020-10-15 20:12:12 -07001347 $file = $oldfile if ($is_git_file);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001348}
1349
Joe Perchesd8469f12015-06-25 15:03:00 -07001350if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001351 hash_show_words(\%use_type, "Used");
1352 hash_show_words(\%ignore_type, "Ignored");
1353
Joe Perches5b579802018-08-21 21:57:33 -07001354 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001355 print << "EOM"
1356
1357NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001358 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001359EOM
1360 }
1361 if ($exit) {
1362 print << "EOM"
1363
1364NOTE: If any of the errors are false positives, please report
1365 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1366EOM
1367 }
1368}
1369
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001370exit($exit);
1371
1372sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001373 my ($root) = @_;
1374
1375 my @tree_check = (
1376 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1377 "README", "Documentation", "arch", "include", "drivers",
1378 "fs", "init", "ipc", "kernel", "lib", "scripts",
1379 );
1380
1381 foreach my $check (@tree_check) {
1382 if (! -e $root . '/' . $check) {
1383 return 0;
1384 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001385 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001386 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001387}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001388
Joe Perches20112472011-07-25 17:13:23 -07001389sub parse_email {
1390 my ($formatted_email) = @_;
1391
1392 my $name = "";
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001393 my $quoted = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001394 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001395 my $address = "";
1396 my $comment = "";
1397
1398 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1399 $name = $1;
1400 $address = $2;
1401 $comment = $3 if defined $3;
1402 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1403 $address = $1;
1404 $comment = $2 if defined $2;
1405 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1406 $address = $1;
1407 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001408 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001409 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001410 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001411 $name =~ s/^\"|\"$//g;
1412 # If there's a name left after stripping spaces and
1413 # leading quotes, and the address doesn't have both
1414 # leading and trailing angle brackets, the address
1415 # is invalid. ie:
1416 # "joe smith joe@smith.com" bad
1417 # "joe smith <joe@smith.com" bad
1418 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1419 $name = "";
1420 $address = "";
1421 $comment = "";
1422 }
1423 }
1424
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001425 # Extract comments from names excluding quoted parts
1426 # "John D. (Doe)" - Do not extract
1427 if ($name =~ s/\"(.+)\"//) {
1428 $quoted = $1;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001429 }
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001430 while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1431 $name_comment .= trim($1);
1432 }
1433 $name =~ s/^[ \"]+|[ \"]+$//g;
1434 $name = trim("$quoted $name");
1435
Joe Perches3705ce52013-07-03 15:05:31 -07001436 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001437 $address =~ s/^\<|\>$//g;
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001438 $comment = trim($comment);
Joe Perches20112472011-07-25 17:13:23 -07001439
1440 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1441 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1442 $name = "\"$name\"";
1443 }
1444
Joe Perchesdfa05c22020-04-06 20:10:48 -07001445 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001446}
1447
1448sub format_email {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001449 my ($name, $name_comment, $address, $comment) = @_;
Joe Perches20112472011-07-25 17:13:23 -07001450
1451 my $formatted_email;
1452
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001453 $name =~ s/^[ \"]+|[ \"]+$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001454 $address = trim($address);
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001455 $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
Joe Perches20112472011-07-25 17:13:23 -07001456
1457 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1458 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1459 $name = "\"$name\"";
1460 }
1461
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001462 $name_comment = trim($name_comment);
1463 $name_comment = " $name_comment" if ($name_comment ne "");
1464 $comment = trim($comment);
1465 $comment = " $comment" if ($comment ne "");
1466
Joe Perches20112472011-07-25 17:13:23 -07001467 if ("$name" eq "") {
1468 $formatted_email = "$address";
1469 } else {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001470 $formatted_email = "$name$name_comment <$address>";
Joe Perches20112472011-07-25 17:13:23 -07001471 }
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001472 $formatted_email .= "$comment";
Joe Perches20112472011-07-25 17:13:23 -07001473 return $formatted_email;
1474}
1475
Joe Perchesdfa05c22020-04-06 20:10:48 -07001476sub reformat_email {
1477 my ($email) = @_;
1478
1479 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001480 return format_email($email_name, $name_comment, $email_address, $comment);
Joe Perchesdfa05c22020-04-06 20:10:48 -07001481}
1482
1483sub same_email_addresses {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001484 my ($email1, $email2) = @_;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001485
1486 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1487 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1488
1489 return $email1_name eq $email2_name &&
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001490 $email1_address eq $email2_address &&
1491 $name1_comment eq $name2_comment &&
1492 $comment1 eq $comment2;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001493}
1494
Joe Perchesd311cd42014-08-06 16:10:57 -07001495sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001496 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001497
Joe Perchesbd474ca2014-08-06 16:11:10 -07001498 foreach my $path (split(/:/, $ENV{PATH})) {
1499 if (-e "$path/$bin") {
1500 return "$path/$bin";
1501 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001502 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001503
Joe Perchesbd474ca2014-08-06 16:11:10 -07001504 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001505}
1506
Joe Perches000d1cc12011-07-25 17:13:25 -07001507sub which_conf {
1508 my ($conf) = @_;
1509
1510 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1511 if (-e "$path/$conf") {
1512 return "$path/$conf";
1513 }
1514 }
1515
1516 return "";
1517}
1518
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001519sub expand_tabs {
1520 my ($str) = @_;
1521
1522 my $res = '';
1523 my $n = 0;
1524 for my $c (split(//, $str)) {
1525 if ($c eq "\t") {
1526 $res .= ' ';
1527 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001528 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001529 $res .= ' ';
1530 }
1531 next;
1532 }
1533 $res .= $c;
1534 $n++;
1535 }
1536
1537 return $res;
1538}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001539sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001540 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001541 return $res;
1542}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001543
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001544sub line_stats {
1545 my ($line) = @_;
1546
1547 # Drop the diff line leader and expand tabs
1548 $line =~ s/^.//;
1549 $line = expand_tabs($line);
1550
1551 # Pick the indent from the front of the line.
1552 my ($white) = ($line =~ /^(\s*)/);
1553
1554 return (length($line), length($white));
1555}
1556
Andy Whitcroft773647a2008-03-28 14:15:58 -07001557my $sanitise_quote = '';
1558
1559sub sanitise_line_reset {
1560 my ($in_comment) = @_;
1561
1562 if ($in_comment) {
1563 $sanitise_quote = '*/';
1564 } else {
1565 $sanitise_quote = '';
1566 }
1567}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001568sub sanitise_line {
1569 my ($line) = @_;
1570
1571 my $res = '';
1572 my $l = '';
1573
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001574 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001575 my $off = 0;
1576 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001577
Andy Whitcroft773647a2008-03-28 14:15:58 -07001578 # Always copy over the diff marker.
1579 $res = substr($line, 0, 1);
1580
1581 for ($off = 1; $off < length($line); $off++) {
1582 $c = substr($line, $off, 1);
1583
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001584 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001585 # and end, all to $;.
1586 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1587 $sanitise_quote = '*/';
1588
1589 substr($res, $off, 2, "$;$;");
1590 $off++;
1591 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001592 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001593 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001594 $sanitise_quote = '';
1595 substr($res, $off, 2, "$;$;");
1596 $off++;
1597 next;
1598 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001599 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1600 $sanitise_quote = '//';
1601
1602 substr($res, $off, 2, $sanitise_quote);
1603 $off++;
1604 next;
1605 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001606
1607 # A \ in a string means ignore the next character.
1608 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1609 $c eq "\\") {
1610 substr($res, $off, 2, 'XX');
1611 $off++;
1612 next;
1613 }
1614 # Regular quotes.
1615 if ($c eq "'" || $c eq '"') {
1616 if ($sanitise_quote eq '') {
1617 $sanitise_quote = $c;
1618
1619 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001620 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001621 } elsif ($sanitise_quote eq $c) {
1622 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001623 }
1624 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001625
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001626 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001627 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1628 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001629 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1630 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001631 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1632 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001633 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001634 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001635 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001636 }
1637
Daniel Walker113f04a2009-09-21 17:04:35 -07001638 if ($sanitise_quote eq '//') {
1639 $sanitise_quote = '';
1640 }
1641
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001642 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001643 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001644 my $clean = 'X' x length($1);
1645 $res =~ s@\<.*\>@<$clean>@;
1646
1647 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001648 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001649 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001650 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001651 }
1652
Joe Perchesdadf6802016-08-02 14:04:33 -07001653 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1654 my $match = $1;
1655 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1656 }
1657
Andy Whitcroft00df3442007-06-08 13:47:06 -07001658 return $res;
1659}
1660
Joe Perchesa6962d72013-04-29 16:18:13 -07001661sub get_quoted_string {
1662 my ($line, $rawline) = @_;
1663
Joe Perches478b1792018-04-10 16:33:34 -07001664 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001665 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001666 return substr($rawline, $-[0], $+[0] - $-[0]);
1667}
1668
Andy Whitcroft8905a672007-11-28 16:21:06 -08001669sub ctx_statement_block {
1670 my ($linenr, $remain, $off) = @_;
1671 my $line = $linenr - 1;
1672 my $blk = '';
1673 my $soff = $off;
1674 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001675 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001676
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001677 my $loff = 0;
1678
Andy Whitcroft8905a672007-11-28 16:21:06 -08001679 my $type = '';
1680 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001681 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001682 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001683 my $c;
1684 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001685
1686 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001687 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001688 @stack = (['', 0]) if ($#stack == -1);
1689
Andy Whitcroft773647a2008-03-28 14:15:58 -07001690 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001691 # If we are about to drop off the end, pull in more
1692 # context.
1693 if ($off >= $len) {
1694 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001695 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001696 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001697 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001698 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001699 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001700 $len = length($blk);
1701 $line++;
1702 last;
1703 }
1704 # Bail if there is no further context.
1705 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001706 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001707 last;
1708 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001709 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1710 $level++;
1711 $type = '#';
1712 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001713 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001714 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001715 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001716 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001717
Andy Whitcroft773647a2008-03-28 14:15:58 -07001718 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001719
1720 # Handle nested #if/#else.
1721 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1722 push(@stack, [ $type, $level ]);
1723 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1724 ($type, $level) = @{$stack[$#stack - 1]};
1725 } elsif ($remainder =~ /^#\s*endif\b/) {
1726 ($type, $level) = @{pop(@stack)};
1727 }
1728
Andy Whitcroft8905a672007-11-28 16:21:06 -08001729 # Statement ends at the ';' or a close '}' at the
1730 # outermost level.
1731 if ($level == 0 && $c eq ';') {
1732 last;
1733 }
1734
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001735 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001736 if ($level == 0 && $coff_set == 0 &&
1737 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1738 $remainder =~ /^(else)(?:\s|{)/ &&
1739 $remainder !~ /^else\s+if\b/) {
1740 $coff = $off + length($1) - 1;
1741 $coff_set = 1;
1742 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1743 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001744 }
1745
Andy Whitcroft8905a672007-11-28 16:21:06 -08001746 if (($type eq '' || $type eq '(') && $c eq '(') {
1747 $level++;
1748 $type = '(';
1749 }
1750 if ($type eq '(' && $c eq ')') {
1751 $level--;
1752 $type = ($level != 0)? '(' : '';
1753
1754 if ($level == 0 && $coff < $soff) {
1755 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001756 $coff_set = 1;
1757 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001758 }
1759 }
1760 if (($type eq '' || $type eq '{') && $c eq '{') {
1761 $level++;
1762 $type = '{';
1763 }
1764 if ($type eq '{' && $c eq '}') {
1765 $level--;
1766 $type = ($level != 0)? '{' : '';
1767
1768 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001769 if (substr($blk, $off + 1, 1) eq ';') {
1770 $off++;
1771 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001772 last;
1773 }
1774 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001775 # Preprocessor commands end at the newline unless escaped.
1776 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1777 $level--;
1778 $type = '';
1779 $off++;
1780 last;
1781 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001782 $off++;
1783 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001784 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001785 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001786 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001787 $line++;
1788 $remain--;
1789 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001790
1791 my $statement = substr($blk, $soff, $off - $soff + 1);
1792 my $condition = substr($blk, $soff, $coff - $soff + 1);
1793
1794 #warn "STATEMENT<$statement>\n";
1795 #warn "CONDITION<$condition>\n";
1796
Andy Whitcroft773647a2008-03-28 14:15:58 -07001797 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001798
1799 return ($statement, $condition,
1800 $line, $remain + 1, $off - $loff + 1, $level);
1801}
1802
Andy Whitcroftcf655042008-03-04 14:28:20 -08001803sub statement_lines {
1804 my ($stmt) = @_;
1805
1806 # Strip the diff line prefixes and rip blank lines at start and end.
1807 $stmt =~ s/(^|\n)./$1/g;
1808 $stmt =~ s/^\s*//;
1809 $stmt =~ s/\s*$//;
1810
1811 my @stmt_lines = ($stmt =~ /\n/g);
1812
1813 return $#stmt_lines + 2;
1814}
1815
1816sub statement_rawlines {
1817 my ($stmt) = @_;
1818
1819 my @stmt_lines = ($stmt =~ /\n/g);
1820
1821 return $#stmt_lines + 2;
1822}
1823
1824sub statement_block_size {
1825 my ($stmt) = @_;
1826
1827 $stmt =~ s/(^|\n)./$1/g;
1828 $stmt =~ s/^\s*{//;
1829 $stmt =~ s/}\s*$//;
1830 $stmt =~ s/^\s*//;
1831 $stmt =~ s/\s*$//;
1832
1833 my @stmt_lines = ($stmt =~ /\n/g);
1834 my @stmt_statements = ($stmt =~ /;/g);
1835
1836 my $stmt_lines = $#stmt_lines + 2;
1837 my $stmt_statements = $#stmt_statements + 1;
1838
1839 if ($stmt_lines > $stmt_statements) {
1840 return $stmt_lines;
1841 } else {
1842 return $stmt_statements;
1843 }
1844}
1845
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001846sub ctx_statement_full {
1847 my ($linenr, $remain, $off) = @_;
1848 my ($statement, $condition, $level);
1849
1850 my (@chunks);
1851
Andy Whitcroftcf655042008-03-04 14:28:20 -08001852 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001853 ($statement, $condition, $linenr, $remain, $off, $level) =
1854 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001855 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001856 push(@chunks, [ $condition, $statement ]);
1857 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1858 return ($level, $linenr, @chunks);
1859 }
1860
1861 # Pull in the following conditional/block pairs and see if they
1862 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001863 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001864 ($statement, $condition, $linenr, $remain, $off, $level) =
1865 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001866 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001867 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001868 #print "C: push\n";
1869 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001870 }
1871
1872 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001873}
1874
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001875sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001876 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001877 my $line;
1878 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001879 my $blk = '';
1880 my @o;
1881 my @c;
1882 my @res = ();
1883
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001884 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001885 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001886 for ($line = $start; $remain > 0; $line++) {
1887 next if ($rawlines[$line] =~ /^-/);
1888 $remain--;
1889
1890 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001891
1892 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001893 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001894 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001895 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001896 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001897 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001898 $level = pop(@stack);
1899 }
1900
Andy Whitcroft01464f32010-10-26 14:23:19 -07001901 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001902 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1903 if ($off > 0) {
1904 $off--;
1905 next;
1906 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001907
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001908 if ($c eq $close && $level > 0) {
1909 $level--;
1910 last if ($level == 0);
1911 } elsif ($c eq $open) {
1912 $level++;
1913 }
1914 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001915
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001916 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001917 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001918 }
1919
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001920 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001921 }
1922
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001923 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001924}
1925sub ctx_block_outer {
1926 my ($linenr, $remain) = @_;
1927
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001928 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1929 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001930}
1931sub ctx_block {
1932 my ($linenr, $remain) = @_;
1933
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001934 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1935 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001936}
1937sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001938 my ($linenr, $remain, $off) = @_;
1939
1940 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1941 return @r;
1942}
1943sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001944 my ($linenr, $remain) = @_;
1945
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001946 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001947}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001948sub ctx_statement_level {
1949 my ($linenr, $remain, $off) = @_;
1950
1951 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1952}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001953
1954sub ctx_locate_comment {
1955 my ($first_line, $end_line) = @_;
1956
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001957 # If c99 comment on the current line, or the line before or after
1958 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1959 return $current_comment if (defined $current_comment);
1960 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1961 return $current_comment if (defined $current_comment);
1962 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1963 return $current_comment if (defined $current_comment);
1964
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001965 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001966 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001967 return $current_comment if (defined $current_comment);
1968
1969 # Look through the context and try and figure out if there is a
1970 # comment.
1971 my $in_comment = 0;
1972 $current_comment = '';
1973 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001974 my $line = $rawlines[$linenr - 1];
1975 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001976 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1977 $in_comment = 1;
1978 }
1979 if ($line =~ m@/\*@) {
1980 $in_comment = 1;
1981 }
1982 if (!$in_comment && $current_comment ne '') {
1983 $current_comment = '';
1984 }
1985 $current_comment .= $line . "\n" if ($in_comment);
1986 if ($line =~ m@\*/@) {
1987 $in_comment = 0;
1988 }
1989 }
1990
1991 chomp($current_comment);
1992 return($current_comment);
1993}
1994sub ctx_has_comment {
1995 my ($first_line, $end_line) = @_;
1996 my $cmt = ctx_locate_comment($first_line, $end_line);
1997
Andy Whitcroft00df3442007-06-08 13:47:06 -07001998 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001999 ##print "CMMT: $cmt\n";
2000
2001 return ($cmt ne '');
2002}
2003
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002004sub raw_line {
2005 my ($linenr, $cnt) = @_;
2006
2007 my $offset = $linenr - 1;
2008 $cnt++;
2009
2010 my $line;
2011 while ($cnt) {
2012 $line = $rawlines[$offset++];
2013 next if (defined($line) && $line =~ /^-/);
2014 $cnt--;
2015 }
2016
2017 return $line;
2018}
2019
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07002020sub get_stat_real {
2021 my ($linenr, $lc) = @_;
2022
2023 my $stat_real = raw_line($linenr, 0);
2024 for (my $count = $linenr + 1; $count <= $lc; $count++) {
2025 $stat_real = $stat_real . "\n" . raw_line($count, 0);
2026 }
2027
2028 return $stat_real;
2029}
2030
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07002031sub get_stat_here {
2032 my ($linenr, $cnt, $here) = @_;
2033
2034 my $herectx = $here . "\n";
2035 for (my $n = 0; $n < $cnt; $n++) {
2036 $herectx .= raw_line($linenr, $n) . "\n";
2037 }
2038
2039 return $herectx;
2040}
2041
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002042sub cat_vet {
2043 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002044 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002045
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002046 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002047 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
2048 $res .= $1;
2049 if ($2 ne '') {
2050 $coded = sprintf("^%c", unpack('C', $2) + 64);
2051 $res .= $coded;
2052 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002053 }
2054 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002055
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002056 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002057}
2058
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002059my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002060my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002061my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002062my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002063
2064sub annotate_reset {
2065 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002066 $av_pending = '_';
2067 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002068 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002069}
2070
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002071sub annotate_values {
2072 my ($stream, $type) = @_;
2073
2074 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002075 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002076 my $cur = $stream;
2077
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002078 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002079
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002080 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002081 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002082 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002083 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002084 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002085 print "WS($1)\n" if ($dbg_values > 1);
2086 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002087 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002088 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002089 }
2090
Florian Micklerc023e4732011-01-12 16:59:58 -08002091 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07002092 print "CAST($1)\n" if ($dbg_values > 1);
2093 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08002094 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07002095
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07002096 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002097 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002098 $type = 'T';
2099
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07002100 } elsif ($cur =~ /^($Modifier)\s*/) {
2101 print "MODIFIER($1)\n" if ($dbg_values > 1);
2102 $type = 'T';
2103
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002104 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002105 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002106 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002107 push(@av_paren_type, $type);
2108 if ($2 ne '') {
2109 $av_pending = 'N';
2110 }
2111 $type = 'E';
2112
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002113 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002114 print "UNDEF($1)\n" if ($dbg_values > 1);
2115 $av_preprocessor = 1;
2116 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002117
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002118 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002119 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002120 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002121
2122 push(@av_paren_type, $type);
2123 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002124 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08002125
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002126 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002127 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
2128 $av_preprocessor = 1;
2129
2130 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
2131
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002132 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08002133
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002134 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002135 print "PRE_END($1)\n" if ($dbg_values > 1);
2136
2137 $av_preprocessor = 1;
2138
2139 # Assume all arms of the conditional end as this
2140 # one does, and continue as if the #endif was not here.
2141 pop(@av_paren_type);
2142 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002143 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002144
2145 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002146 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002147
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002148 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
2149 print "ATTR($1)\n" if ($dbg_values > 1);
2150 $av_pending = $type;
2151 $type = 'N';
2152
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002153 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002154 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002155 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002156 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002157 }
2158 $type = 'N';
2159
Andy Whitcroft14b111c2008-10-15 22:02:16 -07002160 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002161 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07002162 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002163 $type = 'N';
2164
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002165 } elsif ($cur =~/^(case)/o) {
2166 print "CASE($1)\n" if ($dbg_values > 1);
2167 $av_pend_colon = 'C';
2168 $type = 'N';
2169
Andy Whitcroft14b111c2008-10-15 22:02:16 -07002170 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002171 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002172 $type = 'N';
2173
2174 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002175 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002176 push(@av_paren_type, $av_pending);
2177 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002178 $type = 'N';
2179
2180 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002181 my $new_type = pop(@av_paren_type);
2182 if ($new_type ne '_') {
2183 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002184 print "PAREN('$1') -> $type\n"
2185 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002186 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002187 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002188 }
2189
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07002190 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002191 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07002192 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08002193 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002194
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002195 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
2196 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002197 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002198 } elsif ($type eq 'E') {
2199 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002200 }
2201 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
2202 $type = 'V';
2203
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002204 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002205 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002206 $type = 'V';
2207
2208 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002209 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002210 $type = 'N';
2211
Andy Whitcroftcf655042008-03-04 14:28:20 -08002212 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002213 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002214 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002215 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002216
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002217 } elsif ($cur =~/^(,)/) {
2218 print "COMMA($1)\n" if ($dbg_values > 1);
2219 $type = 'C';
2220
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002221 } elsif ($cur =~ /^(\?)/o) {
2222 print "QUESTION($1)\n" if ($dbg_values > 1);
2223 $type = 'N';
2224
2225 } elsif ($cur =~ /^(:)/o) {
2226 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
2227
2228 substr($var, length($res), 1, $av_pend_colon);
2229 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
2230 $type = 'E';
2231 } else {
2232 $type = 'N';
2233 }
2234 $av_pend_colon = 'O';
2235
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002236 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002237 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002238 $type = 'N';
2239
Andy Whitcroft0d413862008-10-15 22:02:16 -07002240 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002241 my $variant;
2242
2243 print "OPV($1)\n" if ($dbg_values > 1);
2244 if ($type eq 'V') {
2245 $variant = 'B';
2246 } else {
2247 $variant = 'U';
2248 }
2249
2250 substr($var, length($res), 1, $variant);
2251 $type = 'N';
2252
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002253 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002254 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002255 if ($1 ne '++' && $1 ne '--') {
2256 $type = 'N';
2257 }
2258
2259 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002260 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002261 }
2262 if (defined $1) {
2263 $cur = substr($cur, length($1));
2264 $res .= $type x length($1);
2265 }
2266 }
2267
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002268 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002269}
2270
Andy Whitcroft8905a672007-11-28 16:21:06 -08002271sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002272 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002273 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002274 ^(?:
2275 $Modifier|
2276 $Storage|
2277 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002278 DEFINE_\S+
2279 )$|
2280 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002281 goto|
2282 return|
2283 case|
2284 else|
2285 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002286 do|
2287 \#|
2288 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002289 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002290 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002291 )}x;
2292 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2293 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002294 # Check for modifiers.
2295 $possible =~ s/\s*$Storage\s*//g;
2296 $possible =~ s/\s*$Sparse\s*//g;
2297 if ($possible =~ /^\s*$/) {
2298
2299 } elsif ($possible =~ /\s/) {
2300 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002301 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002302 if ($modifier !~ $notPermitted) {
2303 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002304 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002305 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002306 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002307
2308 } else {
2309 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002310 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002311 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002312 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002313 } else {
2314 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002315 }
2316}
2317
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002318my $prefix = '';
2319
Joe Perches000d1cc12011-07-25 17:13:25 -07002320sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002321 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002322
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002323 $type =~ tr/[a-z]/[A-Z]/;
2324
Joe Perchescbec18a2014-04-03 14:49:19 -07002325 return defined $use_type{$type} if (scalar keys %use_type > 0);
2326
2327 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002328}
2329
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002330sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002331 my ($level, $type, $msg) = @_;
2332
2333 if (!show_type($type) ||
2334 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002335 return 0;
2336 }
Joe Perches57230292015-06-25 15:03:03 -07002337 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002338 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002339 if ($level eq 'ERROR') {
2340 $output .= RED;
2341 } elsif ($level eq 'WARNING') {
2342 $output .= YELLOW;
2343 } else {
2344 $output .= GREEN;
2345 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002346 }
Joe Perches57230292015-06-25 15:03:03 -07002347 $output .= $prefix . $level . ':';
2348 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002349 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002350 $output .= "$type:";
2351 }
John Brooks737c0762017-07-10 15:52:24 -07002352 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002353 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002354
2355 if ($showfile) {
2356 my @lines = split("\n", $output, -1);
2357 splice(@lines, 1, 1);
2358 $output = join("\n", @lines);
2359 }
Dwaipayan Ray52178ce2021-02-26 15:08:26 +05302360
2361 if ($terse) {
2362 $output = (split('\n', $output))[0] . "\n";
2363 }
2364
2365 if ($verbose && exists($verbose_messages{$type}) &&
2366 !exists($verbose_emitted{$type})) {
2367 $output .= $verbose_messages{$type} . "\n\n";
2368 $verbose_emitted{$type} = 1;
2369 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002370
Joe Perches57230292015-06-25 15:03:03 -07002371 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002372
2373 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002374}
Joe Perchescbec18a2014-04-03 14:49:19 -07002375
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002376sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002377 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002378}
Joe Perches000d1cc12011-07-25 17:13:25 -07002379
Joe Perchesd752fcc2014-08-06 16:11:05 -07002380sub fixup_current_range {
2381 my ($lineRef, $offset, $length) = @_;
2382
2383 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2384 my $o = $1;
2385 my $l = $2;
2386 my $no = $o + $offset;
2387 my $nl = $l + $length;
2388 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2389 }
2390}
2391
2392sub fix_inserted_deleted_lines {
2393 my ($linesRef, $insertedRef, $deletedRef) = @_;
2394
2395 my $range_last_linenr = 0;
2396 my $delta_offset = 0;
2397
2398 my $old_linenr = 0;
2399 my $new_linenr = 0;
2400
2401 my $next_insert = 0;
2402 my $next_delete = 0;
2403
2404 my @lines = ();
2405
2406 my $inserted = @{$insertedRef}[$next_insert++];
2407 my $deleted = @{$deletedRef}[$next_delete++];
2408
2409 foreach my $old_line (@{$linesRef}) {
2410 my $save_line = 1;
2411 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002412 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002413 $delta_offset = 0;
2414 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2415 $range_last_linenr = $new_linenr;
2416 fixup_current_range(\$line, $delta_offset, 0);
2417 }
2418
2419 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2420 $deleted = @{$deletedRef}[$next_delete++];
2421 $save_line = 0;
2422 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2423 }
2424
2425 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2426 push(@lines, ${$inserted}{'LINE'});
2427 $inserted = @{$insertedRef}[$next_insert++];
2428 $new_linenr++;
2429 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2430 }
2431
2432 if ($save_line) {
2433 push(@lines, $line);
2434 $new_linenr++;
2435 }
2436
2437 $old_linenr++;
2438 }
2439
2440 return @lines;
2441}
2442
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002443sub fix_insert_line {
2444 my ($linenr, $line) = @_;
2445
2446 my $inserted = {
2447 LINENR => $linenr,
2448 LINE => $line,
2449 };
2450 push(@fixed_inserted, $inserted);
2451}
2452
2453sub fix_delete_line {
2454 my ($linenr, $line) = @_;
2455
2456 my $deleted = {
2457 LINENR => $linenr,
2458 LINE => $line,
2459 };
2460
2461 push(@fixed_deleted, $deleted);
2462}
2463
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002464sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002465 my ($type, $msg) = @_;
2466
2467 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002468 our $clean = 0;
2469 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002470 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002471 }
Joe Perches3705ce52013-07-03 15:05:31 -07002472 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002473}
2474sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002475 my ($type, $msg) = @_;
2476
2477 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002478 our $clean = 0;
2479 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002480 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002481 }
Joe Perches3705ce52013-07-03 15:05:31 -07002482 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002483}
2484sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002485 my ($type, $msg) = @_;
2486
2487 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002488 our $clean = 0;
2489 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002490 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002491 }
Joe Perches3705ce52013-07-03 15:05:31 -07002492 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002493}
2494
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002495sub check_absolute_file {
2496 my ($absolute, $herecurr) = @_;
2497 my $file = $absolute;
2498
2499 ##print "absolute<$absolute>\n";
2500
2501 # See if any suffix of this path is a path within the tree.
2502 while ($file =~ s@^[^/]*/@@) {
2503 if (-f "$root/$file") {
2504 ##print "file<$file>\n";
2505 last;
2506 }
2507 }
2508 if (! -f _) {
2509 return 0;
2510 }
2511
2512 # It is, so see if the prefix is acceptable.
2513 my $prefix = $absolute;
2514 substr($prefix, -length($file)) = '';
2515
2516 ##print "prefix<$prefix>\n";
2517 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002518 WARN("USE_RELATIVE_PATH",
2519 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002520 }
2521}
2522
Joe Perches3705ce52013-07-03 15:05:31 -07002523sub trim {
2524 my ($string) = @_;
2525
Joe Perchesb34c6482013-09-11 14:24:01 -07002526 $string =~ s/^\s+|\s+$//g;
2527
2528 return $string;
2529}
2530
2531sub ltrim {
2532 my ($string) = @_;
2533
2534 $string =~ s/^\s+//;
2535
2536 return $string;
2537}
2538
2539sub rtrim {
2540 my ($string) = @_;
2541
2542 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002543
2544 return $string;
2545}
2546
Joe Perches52ea8502013-11-12 15:10:09 -08002547sub string_find_replace {
2548 my ($string, $find, $replace) = @_;
2549
2550 $string =~ s/$find/$replace/g;
2551
2552 return $string;
2553}
2554
Joe Perches3705ce52013-07-03 15:05:31 -07002555sub tabify {
2556 my ($leading) = @_;
2557
Antonio Borneo713a09d2020-04-06 20:11:07 -07002558 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002559 my $max_spaces_before_tab = $source_indent - 1;
2560 my $spaces_to_tab = " " x $source_indent;
2561
2562 #convert leading spaces to tabs
2563 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2564 #Remove spaces before a tab
2565 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2566
2567 return "$leading";
2568}
2569
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002570sub pos_last_openparen {
2571 my ($line) = @_;
2572
2573 my $pos = 0;
2574
2575 my $opens = $line =~ tr/\(/\(/;
2576 my $closes = $line =~ tr/\)/\)/;
2577
2578 my $last_openparen = 0;
2579
2580 if (($opens == 0) || ($closes >= $opens)) {
2581 return -1;
2582 }
2583
2584 my $len = length($line);
2585
2586 for ($pos = 0; $pos < $len; $pos++) {
2587 my $string = substr($line, $pos);
2588 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2589 $pos += length($1) - 1;
2590 } elsif (substr($line, $pos, 1) eq '(') {
2591 $last_openparen = $pos;
2592 } elsif (index($string, '(') == -1) {
2593 last;
2594 }
2595 }
2596
Joe Perches91cb5192014-04-03 14:49:32 -07002597 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002598}
2599
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002600sub get_raw_comment {
2601 my ($line, $rawline) = @_;
2602 my $comment = '';
2603
2604 for my $i (0 .. (length($line) - 1)) {
2605 if (substr($line, $i, 1) eq "$;") {
2606 $comment .= substr($rawline, $i, 1);
2607 }
2608 }
2609
2610 return $comment;
2611}
2612
Song Liu5b8f82e2021-02-25 17:22:08 -08002613sub exclude_global_initialisers {
2614 my ($realfile) = @_;
2615
2616 # Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
2617 return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ ||
2618 $realfile =~ m@^samples/bpf/.*_kern\.c$@ ||
2619 $realfile =~ m@/bpf/.*\.bpf\.c$@;
2620}
2621
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002622sub process {
2623 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002624
2625 my $linenr=0;
2626 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002627 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002628 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002629 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002630
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002631 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002632 my $indent;
2633 my $previndent=0;
2634 my $stashindent=0;
2635
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002636 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002637 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002638 my $author = '';
2639 my $authorsignoff = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002640 my $author_sob = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002641 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002642 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002643 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002644 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002645 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002646 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002647 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002648 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002649 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002650 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002651 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002652 my $non_utf8_charset = 0;
2653
Joe Perches4ce9f972021-09-07 19:59:57 -07002654 my $last_git_commit_id_linenr = -1;
2655
Joe Perches365dd4e2014-08-06 16:10:42 -07002656 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002657 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002658
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002659 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002660 our $cnt_lines = 0;
2661 our $cnt_error = 0;
2662 our $cnt_warn = 0;
2663 our $cnt_chk = 0;
2664
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002665 # Trace the real file/line as we go.
2666 my $realfile = '';
2667 my $realline = 0;
2668 my $realcnt = 0;
2669 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002670 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002671 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002672 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002673 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002674 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002675
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002676 my $prev_values = 'E';
2677
2678 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002679 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002680 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002681 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002682 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002683
Joe Perches7e51f192013-09-11 14:23:57 -07002684 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002685
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002686 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002687 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002688 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002689 my @setup_docs = ();
2690 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002691
Joe Perchesd8b07712013-11-12 15:10:06 -08002692 my $camelcase_file_seeded = 0;
2693
Rob Herring9f3a8992018-04-10 16:33:13 -07002694 my $checklicenseline = 1;
2695
Andy Whitcroft773647a2008-03-28 14:15:58 -07002696 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002697 my $line;
2698 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002699 $linenr++;
2700 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002701
Joe Perches3705ce52013-07-03 15:05:31 -07002702 push(@fixed, $rawline) if ($fix);
2703
Andy Whitcroft773647a2008-03-28 14:15:58 -07002704 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002705 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002706 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002707 $setup_docs = 1;
2708 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002709 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002710 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002711 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002712 $realline=$1-1;
2713 if (defined $2) {
2714 $realcnt=$3+1;
2715 } else {
2716 $realcnt=1+1;
2717 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002718 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002719
2720 # Guestimate if this is a continuing comment. Run
2721 # the context looking for a comment "edge". If this
2722 # edge is a close comment then we must be in a comment
2723 # at context start.
2724 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002725 my $cnt = $realcnt;
2726 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2727 next if (defined $rawlines[$ln - 1] &&
2728 $rawlines[$ln - 1] =~ /^-/);
2729 $cnt--;
2730 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002731 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002732 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2733 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2734 ($edge) = $1;
2735 last;
2736 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002737 }
2738 if (defined $edge && $edge eq '*/') {
2739 $in_comment = 1;
2740 }
2741
2742 # Guestimate if this is a continuing comment. If this
2743 # is the start of a diff block and this line starts
2744 # ' *' then it is very likely a comment.
2745 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002746 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002747 {
2748 $in_comment = 1;
2749 }
2750
2751 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2752 sanitise_line_reset($in_comment);
2753
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002754 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002755 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002756 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002757 $line = sanitise_line($rawline);
2758 }
2759 push(@lines, $line);
2760
2761 if ($realcnt > 1) {
2762 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2763 } else {
2764 $realcnt = 0;
2765 }
2766
2767 #print "==>$rawline\n";
2768 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002769
2770 if ($setup_docs && $line =~ /^\+/) {
2771 push(@setup_docs, $line);
2772 }
2773 }
2774
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002775 $prefix = '';
2776
Andy Whitcroft773647a2008-03-28 14:15:58 -07002777 $realcnt = 0;
2778 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002779 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002780 foreach my $line (@lines) {
2781 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002782 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002783 my $sline = $line; #copy of $line
2784 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002785
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002786 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002787 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002788
Joe Perches12c253a2018-06-07 17:10:58 -07002789# check if it's a mode change, rename or start of a patch
2790 if (!$in_commit_log &&
2791 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2792 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2793 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2794 $is_patch = 1;
2795 }
2796
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002797#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002798 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002799 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2800 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002801 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002802 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002803 $realline=$1-1;
2804 if (defined $2) {
2805 $realcnt=$3+1;
2806 } else {
2807 $realcnt=1+1;
2808 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002809 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002810 $prev_values = 'E';
2811
Andy Whitcroft773647a2008-03-28 14:15:58 -07002812 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002813 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002814 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002815 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002816 if ($context =~ /\b(\w+)\s*\(/) {
2817 $context_function = $1;
2818 } else {
2819 undef $context_function;
2820 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002821 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002822
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002823# track the line number as we move through the hunk, note that
2824# new versions of GNU diff omit the leading space on completely
2825# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002826 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002827 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002828 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002829
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002830 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002831 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002832
2833 # Track the previous line.
2834 ($prevline, $stashline) = ($stashline, $line);
2835 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002836 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2837
Andy Whitcroft773647a2008-03-28 14:15:58 -07002838 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002839
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002840 } elsif ($realcnt == 1) {
2841 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002842 }
2843
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002844 my $hunk_line = ($realcnt != 0);
2845
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002846 $here = "#$linenr: " if (!$file);
2847 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002848
Joe Perches2ac73b42014-06-04 16:12:05 -07002849 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002850 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002851 if ($line =~ /^diff --git.*?(\S+)$/) {
2852 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002853 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002854 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002855 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002856 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002857 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002858 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002859 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002860
2861 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002862 if (!$file && $tree && $p1_prefix ne '' &&
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08002863 -e "$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002864 WARN("PATCH_PREFIX",
2865 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002866 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002867
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002868 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002869 ERROR("MODIFIED_INCLUDE_ASM",
2870 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
Andy Whitcroft773647a2008-03-28 14:15:58 -07002871 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002872 $found_file = 1;
2873 }
2874
Joe Perches34d88152015-06-25 15:03:05 -07002875#make up the handle for any error we report on this line
2876 if ($showfile) {
2877 $prefix = "$realfile:$realline: "
2878 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002879 if ($file) {
2880 $prefix = "$filename:$realline: ";
2881 } else {
2882 $prefix = "$filename:$linenr: ";
2883 }
Joe Perches34d88152015-06-25 15:03:05 -07002884 }
2885
Joe Perches2ac73b42014-06-04 16:12:05 -07002886 if ($found_file) {
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08002887 # if (is_maintained_obsolete($realfile)) {
2888 # WARN("OBSOLETE",
2889 # "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2890 # }
Joe Perches7bd7e482015-09-09 15:37:44 -07002891 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002892 $check = 1;
2893 } else {
2894 $check = $check_orig;
2895 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002896 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002897
2898 if ($realfile !~ /^MAINTAINERS/) {
2899 my $last_binding_patch = $is_binding_patch;
2900
2901 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2902
2903 if (($last_binding_patch != -1) &&
2904 ($last_binding_patch ^ $is_binding_patch)) {
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08002905 # WARN("DT_SPLIT_BINDING_PATCH",
2906 # "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
Rob Herring133712a2018-08-21 21:58:16 -07002907 }
2908 }
2909
Andy Whitcroft773647a2008-03-28 14:15:58 -07002910 next;
2911 }
2912
Randy Dunlap389834b2007-06-08 13:47:03 -07002913 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002914
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002915 my $hereline = "$here\n$rawline\n";
2916 my $herecurr = "$here\n$rawline\n";
2917 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002918
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002919 $cnt_lines++ if ($realcnt != 0);
2920
Joe Perches490b2922018-08-21 21:58:01 -07002921# Verify the existence of a commit log if appropriate
2922# 2 is used because a $signature is counted in $commit_log_lines
2923 if ($in_commit_log) {
2924 if ($line !~ /^\s*$/) {
2925 $commit_log_lines++; #could be a $signature
2926 }
2927 } elsif ($has_commit_log && $commit_log_lines < 2) {
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08002928 # WARN("COMMIT_MESSAGE",
2929 # "Missing commit description - Add an appropriate one\n");
2930 # $commit_log_lines = 2; #warn only once
Joe Perches490b2922018-08-21 21:58:01 -07002931 }
2932
Joe Perchese518e9a2015-06-25 15:03:27 -07002933# Check if the commit log has what seems like a diff which can confuse patch
2934 if ($in_commit_log && !$commit_log_has_diff &&
Mrinal Pandey13e45412020-09-04 16:35:52 -07002935 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2936 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Joe Perchese518e9a2015-06-25 15:03:27 -07002937 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2938 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2939 ERROR("DIFF_IN_COMMIT_MSG",
2940 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2941 $commit_log_has_diff = 1;
2942 }
2943
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002944# Check for incorrect file permissions
2945 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2946 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002947 if ($realfile !~ m@scripts/@ &&
2948 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002949 ERROR("EXECUTE_PERMISSIONS",
2950 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002951 }
2952 }
2953
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002954# Check the patch for a From:
2955 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2956 $author = $1;
Dwaipayan Raye7f929f2020-10-15 20:12:15 -07002957 my $curline = $linenr;
2958 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2959 $author .= $1;
2960 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002961 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2962 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002963 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002964 }
2965
Joe Perches20112472011-07-25 17:13:23 -07002966# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002967 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002968 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002969 $in_commit_log = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002970 if ($author ne '' && $authorsignoff != 1) {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002971 if (same_email_addresses($1, $author)) {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002972 $authorsignoff = 1;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002973 } else {
2974 my $ctx = $1;
2975 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
2976 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
2977
Mimi Zohar046fc742021-09-07 19:59:54 -07002978 if (lc $email_address eq lc $author_address && $email_name eq $author_name) {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002979 $author_sob = $ctx;
2980 $authorsignoff = 2;
Mimi Zohar046fc742021-09-07 19:59:54 -07002981 } elsif (lc $email_address eq lc $author_address) {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002982 $author_sob = $ctx;
2983 $authorsignoff = 3;
2984 } elsif ($email_name eq $author_name) {
2985 $author_sob = $ctx;
2986 $authorsignoff = 4;
2987
2988 my $address1 = $email_address;
2989 my $address2 = $author_address;
2990
2991 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
2992 $address1 = "$1$2";
2993 }
2994 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
2995 $address2 = "$1$2";
2996 }
2997 if ($address1 eq $address2) {
2998 $authorsignoff = 5;
2999 }
3000 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07003001 }
3002 }
Joe Perches20112472011-07-25 17:13:23 -07003003 }
3004
Joe Perches44d303e2020-04-06 20:11:10 -07003005# Check for patch separator
3006 if ($line =~ /^---$/) {
3007 $has_patch_separator = 1;
3008 $in_commit_log = 0;
3009 }
3010
Joe Perchese0d975b2014-12-10 15:51:49 -08003011# Check if MAINTAINERS is being updated. If so, there's probably no need to
3012# emit the "does MAINTAINERS need updating?" message on file add/move/delete
3013 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
3014 $reported_maintainer_file = 1;
3015 }
3016
Joe Perches20112472011-07-25 17:13:23 -07003017# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08003018 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07003019 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07003020 my $space_before = $1;
3021 my $sign_off = $2;
3022 my $space_after = $3;
3023 my $email = $4;
3024 my $ucfirst_sign_off = ucfirst(lc($sign_off));
3025
Joe Perchesce0338df3c2012-07-30 14:41:18 -07003026 if ($sign_off !~ /$signature_tags/) {
Aditya Srivastava831242a2020-12-15 20:45:12 -08003027 my $suggested_signature = find_standard_signature($sign_off);
3028 if ($suggested_signature eq "") {
3029 WARN("BAD_SIGN_OFF",
3030 "Non-standard signature: $sign_off\n" . $herecurr);
3031 } else {
3032 if (WARN("BAD_SIGN_OFF",
3033 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
3034 $fix) {
3035 $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
3036 }
3037 }
Joe Perchesce0338df3c2012-07-30 14:41:18 -07003038 }
Joe Perches20112472011-07-25 17:13:23 -07003039 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07003040 if (WARN("BAD_SIGN_OFF",
3041 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
3042 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003043 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07003044 "$ucfirst_sign_off $email";
3045 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003046 }
Joe Perches20112472011-07-25 17:13:23 -07003047 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07003048 if (WARN("BAD_SIGN_OFF",
3049 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
3050 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003051 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07003052 "$ucfirst_sign_off $email";
3053 }
3054
Joe Perches20112472011-07-25 17:13:23 -07003055 }
3056 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07003057 if (WARN("BAD_SIGN_OFF",
3058 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
3059 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003060 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07003061 "$ucfirst_sign_off $email";
3062 }
Joe Perches20112472011-07-25 17:13:23 -07003063 }
3064
Joe Perchesdfa05c22020-04-06 20:10:48 -07003065 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07003066 my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
Joe Perches20112472011-07-25 17:13:23 -07003067 if ($suggested_email eq "") {
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08003068 # ERROR("BAD_SIGN_OFF",
3069 # "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07003070 } else {
3071 my $dequoted = $suggested_email;
3072 $dequoted =~ s/^"//;
3073 $dequoted =~ s/" </ </;
3074 # Don't force email to have quotes
3075 # Allow just an angle bracketed address
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08003076 if (!same_email_addresses($email, $suggested_email)) {
3077 if (WARN("BAD_SIGN_OFF",
3078 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
3079 $fix) {
3080 $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
3081 }
3082 }
3083
3084 # Address part shouldn't have comments
3085 my $stripped_address = $email_address;
3086 $stripped_address =~ s/\([^\(\)]*\)//g;
3087 if ($email_address ne $stripped_address) {
3088 if (WARN("BAD_SIGN_OFF",
3089 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
3090 $fix) {
3091 $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
3092 }
3093 }
3094
3095 # Only one name comment should be allowed
3096 my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
3097 if ($comment_count > 1) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003098 WARN("BAD_SIGN_OFF",
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08003099 "Use a single name comment in email: '$email'\n" . $herecurr);
3100 }
3101
3102
3103 # stable@vger.kernel.org or stable@kernel.org shouldn't
Dwaipayan Raye73d2712020-12-15 20:44:56 -08003104 # have an email name. In addition comments should strictly
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08003105 # begin with a #
3106 if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
3107 if (($comment ne "" && $comment !~ /^#.+/) ||
3108 ($email_name ne "")) {
3109 my $cur_name = $email_name;
3110 my $new_comment = $comment;
3111 $cur_name =~ s/[a-zA-Z\s\-\"]+//g;
3112
3113 # Remove brackets enclosing comment text
3114 # and # from start of comments to get comment text
3115 $new_comment =~ s/^\((.*)\)$/$1/;
3116 $new_comment =~ s/^\[(.*)\]$/$1/;
3117 $new_comment =~ s/^[\s\#]+|\s+$//g;
3118
3119 $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
3120 $new_comment = " # $new_comment" if ($new_comment ne "");
3121 my $new_email = "$email_address$new_comment";
3122
3123 if (WARN("BAD_STABLE_ADDRESS_STYLE",
3124 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
3125 $fix) {
3126 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3127 }
3128 }
3129 } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
3130 my $new_comment = $comment;
3131
3132 # Extract comment text from within brackets or
3133 # c89 style /*...*/ comments
3134 $new_comment =~ s/^\[(.*)\]$/$1/;
3135 $new_comment =~ s/^\/\*(.*)\*\/$/$1/;
3136
3137 $new_comment = trim($new_comment);
3138 $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
3139 $new_comment = "($new_comment)" if ($new_comment ne "");
3140 my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
3141
3142 if (WARN("BAD_SIGN_OFF",
3143 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
3144 $fix) {
3145 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3146 }
Joe Perches20112472011-07-25 17:13:23 -07003147 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003148 }
Joe Perches7e51f192013-09-11 14:23:57 -07003149
3150# Check for duplicate signatures
3151 my $sig_nospace = $line;
3152 $sig_nospace =~ s/\s//g;
3153 $sig_nospace = lc($sig_nospace);
3154 if (defined $signatures{$sig_nospace}) {
3155 WARN("BAD_SIGN_OFF",
3156 "Duplicate signature\n" . $herecurr);
3157 } else {
3158 $signatures{$sig_nospace} = 1;
3159 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07003160
3161# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
3162 if ($sign_off =~ /^co-developed-by:$/i) {
3163 if ($email eq $author) {
3164 WARN("BAD_SIGN_OFF",
Lee Jonesabea1cb2023-12-15 09:36:19 +00003165 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . $herecurr);
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07003166 }
3167 if (!defined $lines[$linenr]) {
3168 WARN("BAD_SIGN_OFF",
Lee Jonesabea1cb2023-12-15 09:36:19 +00003169 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . $herecurr);
3170 } elsif ($rawlines[$linenr] !~ /^signed-off-by:\s*(.*)/i) {
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07003171 WARN("BAD_SIGN_OFF",
Lee Jonesabea1cb2023-12-15 09:36:19 +00003172 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . $herecurr . $rawlines[$linenr] . "\n");
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07003173 } elsif ($1 ne $email) {
3174 WARN("BAD_SIGN_OFF",
Lee Jonesabea1cb2023-12-15 09:36:19 +00003175 "Co-developed-by and Signed-off-by: name/email do not match\n" . $herecurr . $rawlines[$linenr] . "\n");
3176 }
3177 }
3178
3179# check if Reported-by: is followed by a Closes: tag
3180 if ($sign_off =~ /^reported(?:|-and-tested)-by:$/i) {
3181 if (!defined $lines[$linenr]) {
3182 WARN("BAD_REPORTED_BY_LINK",
3183 "Reported-by: should be immediately followed by Closes: with a URL to the report\n" . $herecurr . "\n");
3184 } elsif ($rawlines[$linenr] !~ /^closes:\s*/i) {
3185 WARN("BAD_REPORTED_BY_LINK",
3186 "Reported-by: should be immediately followed by Closes: with a URL to the report\n" . $herecurr . $rawlines[$linenr] . "\n");
3187 }
3188 }
3189 }
3190
3191
3192# Check Fixes: styles is correct
3193 if (!$in_header_lines &&
3194 $line =~ /^\s*fixes:?\s*(?:commit\s*)?[0-9a-f]{5,}\b/i) {
3195 my $orig_commit = "";
3196 my $id = "0123456789ab";
3197 my $title = "commit title";
3198 my $tag_case = 1;
3199 my $tag_space = 1;
3200 my $id_length = 1;
3201 my $id_case = 1;
3202 my $title_has_quotes = 0;
3203
3204 if ($line =~ /(\s*fixes:?)\s+([0-9a-f]{5,})\s+($balanced_parens)/i) {
3205 my $tag = $1;
3206 $orig_commit = $2;
3207 $title = $3;
3208
3209 $tag_case = 0 if $tag eq "Fixes:";
3210 $tag_space = 0 if ($line =~ /^fixes:? [0-9a-f]{5,} ($balanced_parens)/i);
3211
3212 $id_length = 0 if ($orig_commit =~ /^[0-9a-f]{12}$/i);
3213 $id_case = 0 if ($orig_commit !~ /[A-F]/);
3214
3215 # Always strip leading/trailing parens then double quotes if existing
3216 $title = substr($title, 1, -1);
3217 if ($title =~ /^".*"$/) {
3218 $title = substr($title, 1, -1);
3219 $title_has_quotes = 1;
3220 }
3221 }
3222
3223 my ($cid, $ctitle) = git_commit_info($orig_commit, $id,
3224 $title);
3225
3226 if ($ctitle ne $title || $tag_case || $tag_space ||
3227 $id_length || $id_case || !$title_has_quotes) {
3228 if (WARN("BAD_FIXES_TAG",
3229 "Please use correct Fixes: style 'Fixes: <12 chars of sha1> (\"<title line>\")' - ie: 'Fixes: $cid (\"$ctitle\")'\n" . $herecurr) &&
3230 $fix) {
3231 $fixed[$fixlinenr] = "Fixes: $cid (\"$ctitle\")";
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07003232 }
3233 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003234 }
3235
Joe Perchesa2fe16b2015-02-13 14:39:02 -08003236# Check email subject for common tools that don't need to be mentioned
3237 if ($in_header_lines &&
3238 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
3239 WARN("EMAIL_SUBJECT",
3240 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
3241 }
3242
Joe Perches44d303e2020-04-06 20:11:10 -07003243# Check for Gerrit Change-Ids not in any patch context
3244 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08003245 # if (ERROR("GERRIT_CHANGE_ID",
3246 # "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
3247 # $fix) {
3248 # fix_delete_line($fixlinenr, $rawline);
3249 # }
Christopher Covington7ebd05e2014-04-03 14:49:31 -07003250 }
3251
Joe Perches369c8dd2015-11-06 16:31:34 -08003252# Check if the commit log is in a possible stack dump
3253 if ($in_commit_log && !$commit_log_possible_stack_dump &&
3254 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3255 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3256 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07003257 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3258 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3259 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3260 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08003261 $commit_log_possible_stack_dump = 1;
3262 }
3263
Joe Perches2a076f42015-04-16 12:44:28 -07003264# Check for line lengths > 75 in commit log, warn once
3265 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08003266 length($line) > 75 &&
3267 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3268 # file delta changes
Lee Jonesabea1cb2023-12-15 09:36:19 +00003269 $line =~ /^\s*(?:[\w\.\-\+]*\/)++[\w\.\-\+]+:/ ||
Joe Perches369c8dd2015-11-06 16:31:34 -08003270 # filename then :
Lee Jonesabea1cb2023-12-15 09:36:19 +00003271 $line =~ /^\s*(?:Fixes:|$link_tags_search|$signature_tags)/i ||
3272 # A Fixes:, link or signature tag line
Joe Perches369c8dd2015-11-06 16:31:34 -08003273 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07003274 WARN("COMMIT_LOG_LONG_LINE",
Lee Jonesabea1cb2023-12-15 09:36:19 +00003275 "Prefer a maximum 75 chars per line (possible unwrapped commit description?)\n" . $herecurr);
Joe Perches2a076f42015-04-16 12:44:28 -07003276 $commit_log_long_line = 1;
3277 }
3278
Joe Perchesbf4daf12015-09-09 15:37:50 -07003279# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08003280 if ($in_commit_log && $commit_log_possible_stack_dump &&
3281 $line =~ /^\s*$/) {
3282 $commit_log_possible_stack_dump = 0;
3283 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07003284
Lee Jonesabea1cb2023-12-15 09:36:19 +00003285# Check for odd tags before a URI/URL
3286 if ($in_commit_log &&
3287 $line =~ /^\s*(\w+:)\s*http/ && $1 !~ /^$link_tags_search$/) {
3288 if ($1 =~ /^v(?:ersion)?\d+/i) {
3289 WARN("COMMIT_LOG_VERSIONING",
3290 "Patch version information should be after the --- line\n" . $herecurr);
3291 } else {
3292 WARN("COMMIT_LOG_USE_LINK",
3293 "Unknown link reference '$1', use $link_tags_print instead\n" . $herecurr);
3294 }
3295 }
3296
3297# Check for misuse of the link tags
3298 if ($in_commit_log &&
3299 $line =~ /^\s*(\w+:)\s*(\S+)/) {
3300 my $tag = $1;
3301 my $value = $2;
3302 if ($tag =~ /^$link_tags_search$/ && $value !~ m{^https?://}) {
3303 WARN("COMMIT_LOG_WRONG_LINK",
3304 "'$tag' should be followed by a public http(s) link\n" . $herecurr);
3305 }
3306 }
3307
Dwaipayan Ray084a6172020-12-15 20:45:18 -08003308# Check for lines starting with a #
3309 if ($in_commit_log && $line =~ /^#/) {
3310 if (WARN("COMMIT_COMMENT_SYMBOL",
3311 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3312 $fix) {
3313 $fixed[$fixlinenr] =~ s/^/ /;
3314 }
3315 }
3316
Joe Perches0d7835f2015-02-13 14:38:35 -08003317# Check for git id commit length and improperly formed commit descriptions
Joe Perches4ce9f972021-09-07 19:59:57 -07003318# A correctly formed commit description is:
3319# commit <SHA-1 hash length 12+ chars> ("Complete commit subject")
3320# with the commit subject '("' prefix and '")' suffix
3321# This is a fairly compilicated block as it tests for what appears to be
3322# bare SHA-1 hash with minimum length of 5. It also avoids several types of
3323# possible SHA-1 matches.
3324# A commit match can span multiple lines so this block attempts to find a
3325# complete typical commit on a maximum of 3 lines
3326 if ($perl_version_ok &&
3327 $in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07003328 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07003329 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perches4ce9f972021-09-07 19:59:57 -07003330 (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
3331 ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) ||
Joe Perchesaab38f52016-08-02 14:04:36 -07003332 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08003333 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3334 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07003335 my $init_char = "c";
3336 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08003337 my $short = 1;
3338 my $long = 0;
3339 my $case = 1;
3340 my $space = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08003341 my $id = '0123456789ab';
3342 my $orig_desc = "commit description";
3343 my $description = "";
Joe Perches4ce9f972021-09-07 19:59:57 -07003344 my $herectx = $herecurr;
3345 my $has_parens = 0;
3346 my $has_quotes = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08003347
Joe Perches4ce9f972021-09-07 19:59:57 -07003348 my $input = $line;
3349 if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) {
3350 for (my $n = 0; $n < 2; $n++) {
3351 if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) {
3352 $orig_desc = $1;
3353 $has_parens = 1;
3354 # Always strip leading/trailing parens then double quotes if existing
3355 $orig_desc = substr($orig_desc, 1, -1);
3356 if ($orig_desc =~ /^".*"$/) {
3357 $orig_desc = substr($orig_desc, 1, -1);
3358 $has_quotes = 1;
3359 }
3360 last;
3361 }
3362 last if ($#lines < $linenr + $n);
3363 $input .= " " . trim($rawlines[$linenr + $n]);
3364 $herectx .= "$rawlines[$linenr + $n]\n";
3365 }
3366 $herectx = $herecurr if (!$has_parens);
Joe Perchesfe043ea2015-09-09 15:37:25 -07003367 }
3368
Joe Perches4ce9f972021-09-07 19:59:57 -07003369 if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3370 $init_char = $1;
3371 $orig_commit = lc($2);
3372 $short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i);
3373 $long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i);
3374 $space = 0 if ($input =~ /\bcommit [0-9a-f]/i);
3375 $case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3376 } elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) {
3377 $orig_commit = lc($1);
Joe Perches0d7835f2015-02-13 14:38:35 -08003378 }
3379
3380 ($id, $description) = git_commit_info($orig_commit,
3381 $id, $orig_desc);
3382
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07003383 if (defined($id) &&
Joe Perches4ce9f972021-09-07 19:59:57 -07003384 ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) &&
3385 $last_git_commit_id_linenr != $linenr - 1) {
Joe Perches0d7835f2015-02-13 14:38:35 -08003386 ERROR("GIT_COMMIT_ID",
Joe Perches4ce9f972021-09-07 19:59:57 -07003387 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herectx);
Joe Perches0d7835f2015-02-13 14:38:35 -08003388 }
Joe Perches4ce9f972021-09-07 19:59:57 -07003389 #don't report the next line if this line ends in commit and the sha1 hash is the next line
3390 $last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i);
Joe Perchesd311cd42014-08-06 16:10:57 -07003391 }
3392
Lee Jonesabea1cb2023-12-15 09:36:19 +00003393# Check for mailing list archives other than lore.kernel.org
3394 if ($rawline =~ m{http.*\b$obsolete_archives}) {
3395 WARN("PREFER_LORE_ARCHIVE",
3396 "Use lore.kernel.org archive links when possible - see https://lore.kernel.org/lists.html\n" . $herecurr);
3397 }
3398
Joe Perches13f19372014-08-06 16:10:59 -07003399# Check for added, moved or deleted files
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08003400 # if (!$reported_maintainer_file && !$in_commit_log &&
3401 # ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3402 # $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
3403 # ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
3404 # (defined($1) || defined($2))))) {
3405 # $is_patch = 1;
3406 # $reported_maintainer_file = 1;
3407 # WARN("FILE_PATH_CHANGES",
3408 # "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3409 # }
Joe Perches13f19372014-08-06 16:10:59 -07003410
Rob Herringe400edb2019-09-12 15:18:20 +01003411# Check for adding new DT bindings not in schema format
3412 if (!$in_commit_log &&
3413 ($line =~ /^new file mode\s*\d+\s*$/) &&
3414 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3415 WARN("DT_SCHEMA_BINDING_PATCH",
Mauro Carvalho Chehab56ddc4c2021-04-01 14:17:49 +02003416 "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n");
Rob Herringe400edb2019-09-12 15:18:20 +01003417 }
3418
Andy Whitcroft00df3442007-06-08 13:47:06 -07003419# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08003420 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003421 ERROR("CORRUPTED_PATCH",
3422 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003423 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003424 }
3425
3426# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3427 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003428 $rawline !~ m/^$UTF8*$/) {
3429 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3430
3431 my $blank = copy_spacing($rawline);
3432 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3433 my $hereptr = "$hereline$ptr\n";
3434
Joe Perches34d99212011-07-25 17:13:26 -07003435 CHK("INVALID_UTF8",
3436 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003437 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003438
Joe Perches15662b32011-10-31 17:13:12 -07003439# Check if it's the start of a commit log
3440# (not a header line and we haven't seen the patch filename)
3441 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07003442 !($rawline =~ /^\s+(?:\S|$)/ ||
3443 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07003444 $in_header_lines = 0;
3445 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07003446 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07003447 }
3448
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003449# Check if there is UTF-8 in a commit log when a mail header has explicitly
3450# declined it, i.e defined some charset where it is missing.
3451 if ($in_header_lines &&
3452 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3453 $1 !~ /utf-8/i) {
3454 $non_utf8_charset = 1;
3455 }
3456
3457 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07003458 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003459 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07003460 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3461 }
3462
Joe Perchesd6430f72016-12-12 16:46:28 -08003463# Check for absolute kernel paths in commit message
3464 if ($tree && $in_commit_log) {
3465 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3466 my $file = $1;
3467
3468 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3469 check_absolute_file($1, $herecurr)) {
3470 #
3471 } else {
3472 check_absolute_file($file, $herecurr);
3473 }
3474 }
3475 }
3476
Kees Cook66b47b42014-10-13 15:51:57 -07003477# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07003478 if (defined($misspellings) &&
3479 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003480 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07003481 my $typo = $1;
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003482 my $blank = copy_spacing($rawline);
3483 my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
3484 my $hereptr = "$hereline$ptr\n";
Kees Cook66b47b42014-10-13 15:51:57 -07003485 my $typo_fix = $spelling_fix{lc($typo)};
3486 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3487 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07003488 my $msg_level = \&WARN;
3489 $msg_level = \&CHK if ($file);
3490 if (&{$msg_level}("TYPO_SPELLING",
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003491 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07003492 $fix) {
3493 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3494 }
3495 }
3496 }
3497
Matteo Crocea8dd86b2019-09-25 16:46:38 -07003498# check for invalid commit id
3499 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3500 my $id;
3501 my $description;
3502 ($id, $description) = git_commit_info($2, undef, undef);
3503 if (!defined($id)) {
3504 WARN("UNKNOWN_COMMIT_ID",
3505 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3506 }
3507 }
3508
Joe Perches310cd062020-10-15 20:11:52 -07003509# check for repeated words separated by a single space
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003510# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3511 if (($rawline =~ /^\+/ || $in_commit_log) &&
3512 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003513 pos($rawline) = 1 if (!$in_commit_log);
Joe Perches310cd062020-10-15 20:11:52 -07003514 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3515
3516 my $first = $1;
3517 my $second = $2;
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003518 my $start_pos = $-[1];
3519 my $end_pos = $+[2];
Joe Perches310cd062020-10-15 20:11:52 -07003520 if ($first =~ /(?:struct|union|enum)/) {
3521 pos($rawline) += length($first) + length($second) + 1;
3522 next;
3523 }
3524
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003525 next if (lc($first) ne lc($second));
Joe Perches310cd062020-10-15 20:11:52 -07003526 next if ($first eq 'long');
3527
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003528 # check for character before and after the word matches
3529 my $start_char = '';
3530 my $end_char = '';
3531 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
3532 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3533
3534 next if ($start_char =~ /^\S$/);
3535 next if (index(" \t.,;?!", $end_char) == -1);
3536
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08003537 # avoid repeating hex occurrences like 'ff ff fe 09 ...'
3538 if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3539 next if (!exists($allow_repeated_words{lc($first)}));
3540 }
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003541
Joe Perches310cd062020-10-15 20:11:52 -07003542 if (WARN("REPEATED_WORD",
3543 "Possible repeated word: '$first'\n" . $herecurr) &&
3544 $fix) {
3545 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3546 }
3547 }
3548
3549 # if it's a repeated word on consecutive lines in a comment block
3550 if ($prevline =~ /$;+\s*$/ &&
3551 $prevrawline =~ /($word_pattern)\s*$/) {
3552 my $last_word = $1;
3553 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3554 if (WARN("REPEATED_WORD",
3555 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3556 $fix) {
3557 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3558 }
3559 }
3560 }
3561 }
3562
Andy Whitcroft306708542008-10-15 22:02:28 -07003563# ignore non-hunk lines and lines being removed
3564 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003565
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003566#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003567 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003568 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07003569 if (ERROR("DOS_LINE_ENDINGS",
3570 "DOS line endings\n" . $herevet) &&
3571 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003572 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003573 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003574 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3575 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003576 if (ERROR("TRAILING_WHITESPACE",
3577 "trailing whitespace\n" . $herevet) &&
3578 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003579 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003580 }
3581
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003582 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003583 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07003584
Josh Triplett4783f892013-11-12 15:10:12 -08003585# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003586 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003587 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003588 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3589 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003590 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003591 my $msg_level = \&ERROR;
3592 $msg_level = \&CHK if ($file);
3593 &{$msg_level}("FSF_MAILING_ADDRESS",
3594 "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)
Josh Triplett4783f892013-11-12 15:10:12 -08003595 }
3596
Andi Kleen33549572010-05-24 14:33:29 -07003597# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003598# Only applies when adding the entry originally, after that we do not have
3599# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003600 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003601 # 'choice' is usually the last thing on the line (though
3602 # Kconfig supports named choices), so use a word boundary
3603 # (\b) rather than a whitespace character (\s)
3604 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Lee Jonesabea1cb2023-12-15 09:36:19 +00003605 my $ln = $linenr;
3606 my $needs_help = 0;
3607 my $has_help = 0;
3608 my $help_length = 0;
3609 while (defined $lines[$ln]) {
3610 my $f = $lines[$ln++];
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003611
3612 next if ($f =~ /^-/);
Lee Jonesabea1cb2023-12-15 09:36:19 +00003613 last if ($f !~ /^[\+ ]/); # !patch context
Andy Whitcrofta1385802012-01-10 15:10:03 -08003614
Lee Jonesabea1cb2023-12-15 09:36:19 +00003615 if ($f =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3616 $needs_help = 1;
3617 next;
3618 }
3619 if ($f =~ /^\+\s*help\s*$/) {
3620 $has_help = 1;
3621 next;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003622 }
3623
Lee Jonesabea1cb2023-12-15 09:36:19 +00003624 $f =~ s/^.//; # strip patch context [+ ]
3625 $f =~ s/#.*//; # strip # directives
3626 $f =~ s/^\s+//; # strip leading blanks
3627 next if ($f =~ /^$/); # skip blank lines
Ulf Magnusson678ae162018-02-16 21:22:54 +01003628
Lee Jonesabea1cb2023-12-15 09:36:19 +00003629 # At the end of this Kconfig block:
Ulf Magnusson678ae162018-02-16 21:22:54 +01003630 # This only checks context lines in the patch
3631 # and so hopefully shouldn't trigger false
3632 # positives, even though some of these are
3633 # common words in help texts
Lee Jonesabea1cb2023-12-15 09:36:19 +00003634 if ($f =~ /^(?:config|menuconfig|choice|endchoice|
3635 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003636 last;
3637 }
Lee Jonesabea1cb2023-12-15 09:36:19 +00003638 $help_length++ if ($has_help);
Andi Kleen33549572010-05-24 14:33:29 -07003639 }
Lee Jonesabea1cb2023-12-15 09:36:19 +00003640 if ($needs_help &&
3641 $help_length < $min_conf_desc_length) {
3642 my $stat_real = get_stat_real($linenr, $ln - 1);
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08003643 # WARN("CONFIG_DESCRIPTION",
3644 # "please write a help paragraph that fully describes the config symbol\n" . "$here\n$stat_real\n");
Vadim Bendebury56193272014-10-13 15:51:48 -07003645 }
Andi Kleen33549572010-05-24 14:33:29 -07003646 }
3647
Joe Perches7ccf41a2020-06-04 16:50:33 -07003648# check MAINTAINERS entries
3649 if ($realfile =~ /^MAINTAINERS$/) {
3650# check MAINTAINERS entries for the right form
3651 if ($rawline =~ /^\+[A-Z]:/ &&
3652 $rawline !~ /^\+[A-Z]:\t\S/) {
3653 if (WARN("MAINTAINERS_STYLE",
3654 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3655 $fix) {
3656 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3657 }
3658 }
3659# check MAINTAINERS entries for the right ordering too
3660 my $preferred_order = 'MRLSWQBCPTFXNK';
3661 if ($rawline =~ /^\+[A-Z]:/ &&
3662 $prevrawline =~ /^[\+ ][A-Z]:/) {
3663 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3664 my $cur = $1;
3665 my $curval = $2;
3666 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3667 my $prev = $1;
3668 my $prevval = $2;
3669 my $curindex = index($preferred_order, $cur);
3670 my $previndex = index($preferred_order, $prev);
3671 if ($curindex < 0) {
3672 WARN("MAINTAINERS_STYLE",
3673 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3674 } else {
3675 if ($previndex >= 0 && $curindex < $previndex) {
3676 WARN("MAINTAINERS_STYLE",
3677 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3678 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3679 ($prev eq 'X' && $cur eq 'X')) &&
3680 ($prevval cmp $curval) > 0) {
3681 WARN("MAINTAINERS_STYLE",
3682 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3683 }
3684 }
Joe Perches628f91a2017-07-10 15:52:07 -07003685 }
3686 }
3687
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003688 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3689 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3690 my $flag = $1;
3691 my $replacement = {
3692 'EXTRA_AFLAGS' => 'asflags-y',
3693 'EXTRA_CFLAGS' => 'ccflags-y',
3694 'EXTRA_CPPFLAGS' => 'cppflags-y',
3695 'EXTRA_LDFLAGS' => 'ldflags-y',
3696 };
3697
3698 WARN("DEPRECATED_VARIABLE",
3699 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3700 }
3701
Rob Herringbff5da42014-01-23 15:54:51 -08003702# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003703 if (defined $root &&
3704 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3705 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3706
Rob Herringbff5da42014-01-23 15:54:51 -08003707 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3708
Florian Vaussardcc933192014-04-03 14:49:27 -07003709 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003710 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003711
Rob Herringbff5da42014-01-23 15:54:51 -08003712 foreach my $compat (@compats) {
3713 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003714 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3715 my $compat3 = $compat;
3716 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3717 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003718 if ( $? >> 8 ) {
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08003719 # WARN("UNDOCUMENTED_DT_STRING",
3720 # "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003721 }
3722
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003723 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3724 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003725 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003726 if ( $? >> 8 ) {
3727 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003728 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003729 }
3730 }
3731 }
3732
Rob Herring9f3a8992018-04-10 16:33:13 -07003733# check for using SPDX license tag at beginning of files
3734 if ($realline == $checklicenseline) {
3735 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3736 $checklicenseline = 2;
3737 } elsif ($rawline =~ /^\+/) {
3738 my $comment = "";
3739 if ($realfile =~ /\.(h|s|S)$/) {
3740 $comment = '/*';
Lee Jonesabea1cb2023-12-15 09:36:19 +00003741 } elsif ($realfile =~ /\.(c|rs|dts|dtsi)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003742 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003743 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003744 $comment = '#';
3745 } elsif ($realfile =~ /\.rst$/) {
3746 $comment = '..';
3747 }
3748
Joe Perchesfdf13692019-03-07 16:28:32 -08003749# check SPDX comment style for .[chsS] files
3750 if ($realfile =~ /\.[chsS]$/ &&
3751 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003752 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003753 WARN("SPDX_LICENSE_TAG",
3754 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3755 }
3756
Rob Herring9f3a8992018-04-10 16:33:13 -07003757 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003758 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3759 WARN("SPDX_LICENSE_TAG",
3760 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003761 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003762 my $spdx_license = $1;
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08003763 # if (!is_SPDX_License_valid($spdx_license)) {
3764 # WARN("SPDX_LICENSE_TAG",
3765 # "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3766 # }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003767 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
Lee Jonesabea1cb2023-12-15 09:36:19 +00003768 $spdx_license !~ /GPL-2\.0(?:-only)? OR BSD-2-Clause/) {
Lubomir Rintel50c92902020-04-06 20:11:13 -07003769 my $msg_level = \&WARN;
3770 $msg_level = \&CHK if ($file);
3771 if (&{$msg_level}("SPDX_LICENSE_TAG",
3772
3773 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3774 $fix) {
3775 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3776 }
3777 }
Lee Jonesabea1cb2023-12-15 09:36:19 +00003778 if ($realfile =~ m@^include/dt-bindings/@ &&
3779 $spdx_license !~ /GPL-2\.0(?:-only)? OR \S+/) {
3780 WARN("SPDX_LICENSE_TAG",
3781 "DT binding headers should be licensed (GPL-2.0-only OR .*)\n" . $herecurr);
3782 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003783 }
3784 }
3785 }
3786
Joe Perchesa0154cd2020-10-15 20:12:19 -07003787# check for embedded filenames
Lee Jonesabea1cb2023-12-15 09:36:19 +00003788 if ($rawline =~ /^\+.*\b\Q$realfile\E\b/) {
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08003789 # WARN("EMBEDDED_FILENAME",
3790 # "It's generally not useful to have the filename in the file\n" . $herecurr);
Joe Perchesa0154cd2020-10-15 20:12:19 -07003791 }
3792
Andy Whitcroft5368df202008-10-15 22:02:27 -07003793# check we are in a valid source file if not then ignore this hunk
Lee Jonesabea1cb2023-12-15 09:36:19 +00003794 next if ($realfile !~ /\.(h|c|rs|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003795
Joe Perchesa8da38a2019-03-07 16:28:42 -08003796# check for using SPDX-License-Identifier on the wrong line number
3797 if ($realline != $checklicenseline &&
3798 $rawline =~ /\bSPDX-License-Identifier:/ &&
3799 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3800 WARN("SPDX_LICENSE_TAG",
3801 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3802 }
3803
Joe Perches47e0c882015-06-25 15:02:57 -07003804# line length limit (with some exclusions)
3805#
3806# There are a few types of lines that may extend beyond $max_line_length:
3807# logging functions like pr_info that end in a string
3808# lines with a single string
3809# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003810# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003811#
3812# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003813# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003814# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3815# LONG_LINE all other lines longer than $max_line_length
3816#
3817# if LONG_LINE is ignored, the other 2 types are also ignored
3818#
3819
Joe Perchesb4749e92015-07-17 16:24:01 -07003820 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003821 my $msg_type = "LONG_LINE";
3822
3823 # Check the allowed long line types first
3824
3825 # logging functions that end in a string that starts
3826 # before $max_line_length
3827 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3828 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3829 $msg_type = "";
3830
3831 # lines with only strings (w/ possible termination)
3832 # #defines with only strings
3833 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3834 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3835 $msg_type = "";
3836
Joe Perchescc147502017-11-17 15:28:44 -08003837 # More special cases
3838 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3839 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003840 $msg_type = "";
3841
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003842 # URL ($rawline is used in case the URL is in a comment)
3843 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3844 $msg_type = "";
3845
Joe Perches47e0c882015-06-25 15:02:57 -07003846 # Otherwise set the alternate message types
3847
3848 # a comment starts before $max_line_length
3849 } elsif ($line =~ /($;[\s$;]*)$/ &&
3850 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3851 $msg_type = "LONG_LINE_COMMENT"
3852
3853 # a quoted string starts before $max_line_length
3854 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3855 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3856 $msg_type = "LONG_LINE_STRING"
3857 }
3858
3859 if ($msg_type ne "" &&
3860 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003861 my $msg_level = \&WARN;
3862 $msg_level = \&CHK if ($file);
3863 &{$msg_level}($msg_type,
3864 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003865 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003866 }
3867
Andy Whitcroft8905a672007-11-28 16:21:06 -08003868# check for adding lines without a newline.
3869 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Tom Rix47ca69b2020-12-15 20:44:40 -08003870 if (WARN("MISSING_EOF_NEWLINE",
3871 "adding a line without newline at end of file\n" . $herecurr) &&
3872 $fix) {
3873 fix_delete_line($fixlinenr+1, "No newline at end of file");
3874 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003875 }
3876
Aditya Srivastavade932452021-02-25 17:21:57 -08003877# check for .L prefix local symbols in .S files
3878 if ($realfile =~ /\.S$/ &&
3879 $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) {
3880 WARN("AVOID_L_PREFIX",
Lee Jonesabea1cb2023-12-15 09:36:19 +00003881 "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/core-api/asm-annotations.rst\n" . $herecurr);
Aditya Srivastavade932452021-02-25 17:21:57 -08003882 }
3883
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003884# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003885 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003886
3887# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003888# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003889 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3890 $rawline =~ /^\+\s* \s*/) {
3891 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003892 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003893 if (ERROR("CODE_INDENT",
3894 "code indent should use tabs where possible\n" . $herevet) &&
3895 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003896 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003897 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003898 }
3899
Alberto Panizzo08e44362010-03-05 13:43:54 -08003900# check for space before tabs.
3901 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3902 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003903 if (WARN("SPACE_BEFORE_TAB",
3904 "please, no space before tabs\n" . $herevet) &&
3905 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003906 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003907 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003908 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003909 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003910 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003911 }
3912
Joe Perches6a487212018-04-10 16:34:04 -07003913# check for assignments on the start of a line
3914 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
Aditya Srivastavada7355a2020-12-15 20:45:06 -08003915 my $operator = $1;
3916 if (CHK("ASSIGNMENT_CONTINUATIONS",
3917 "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3918 $fix && $prevrawline =~ /^\+/) {
3919 # add assignment operator to the previous line, remove from current line
3920 $fixed[$fixlinenr - 1] .= " $operator";
3921 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3922 }
Joe Perches6a487212018-04-10 16:34:04 -07003923 }
3924
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003925# check for && or || at the start of a line
3926 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
Aditya Srivastava8e08f072020-12-15 20:45:09 -08003927 my $operator = $1;
3928 if (CHK("LOGICAL_CONTINUATIONS",
3929 "Logical continuations should be on the previous line\n" . $hereprev) &&
3930 $fix && $prevrawline =~ /^\+/) {
3931 # insert logical operator at last non-comment, non-whitepsace char on previous line
3932 $prevline =~ /[\s$;]*$/;
3933 my $line_end = substr($prevrawline, $-[0]);
3934 $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
3935 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3936 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003937 }
3938
Joe Perchesa91e8992016-05-20 17:04:05 -07003939# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003940 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003941 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003942 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003943 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003944 if (WARN("TABSTOP",
3945 "Statements should start on a tabstop\n" . $herecurr) &&
3946 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003947 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003948 }
3949 }
3950 }
3951
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003952# check multi-line statement indentation matches previous line
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08003953 # if ($perl_version_ok &&
3954 # $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3955 # $prevline =~ /^\+(\t*)(.*)$/;
3956 # my $oldindent = $1;
3957 # my $rest = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003958
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08003959 # my $pos = pos_last_openparen($rest);
3960 # if ($pos >= 0) {
3961 # $line =~ /^(\+| )([ \t]*)/;
3962 # my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003963
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08003964 # my $goodtabindent = $oldindent .
3965 # "\t" x ($pos / $tabsize) .
3966 # " " x ($pos % $tabsize);
3967 # my $goodspaceindent = $oldindent . " " x $pos;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003968
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08003969 # if ($newindent ne $goodtabindent &&
3970 # $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003971
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08003972 # if (CHK("PARENTHESIS_ALIGNMENT",
3973 # "Alignment should match open parenthesis\n" . $hereprev) &&
3974 # $fix && $line =~ /^\+/) {
3975 # $fixed[$fixlinenr] =~
3976 # s/^\+[ \t]*/\+$goodtabindent/;
3977 # }
3978 # }
3979 # }
3980 # }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003981
Joe Perches6ab3a972015-04-16 12:44:05 -07003982# check for space after cast like "(int) foo" or "(struct foo) bar"
3983# avoid checking a few false positives:
3984# "sizeof(<type>)" or "__alignof__(<type>)"
3985# function pointer declarations like "(*foo)(int) = bar;"
3986# structure definitions like "(struct foo) { 0 };"
3987# multiline macros that define functions
3988# known attributes or the __attribute__ keyword
3989 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3990 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003991 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003992 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003993 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003994 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003995 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003996 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003997 }
3998
Joe Perches86406b12015-09-09 15:37:41 -07003999# Block comment styles
4000# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07004001 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07004002 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07004003 $rawline =~ /^\+[ \t]*\*/ &&
Łukasz Stelmachc70735c2020-10-15 20:12:25 -07004004 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
Joe Perches05880602012-10-04 17:13:35 -07004005 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
4006 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
4007 }
4008
Joe Perches86406b12015-09-09 15:37:41 -07004009# Block comments use * on subsequent lines
4010 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
4011 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07004012 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07004013 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07004014 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07004015 WARN("BLOCK_COMMENT_STYLE",
4016 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07004017 }
4018
Joe Perches86406b12015-09-09 15:37:41 -07004019# Block comments use */ on trailing lines
4020 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08004021 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
4022 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
4023 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07004024 WARN("BLOCK_COMMENT_STYLE",
4025 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07004026 }
4027
Joe Perches08eb9b82016-10-11 13:51:50 -07004028# Block comment * alignment
4029 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07004030 $line =~ /^\+[ \t]*$;/ && #leading comment
4031 $rawline =~ /^\+[ \t]*\*/ && #leading *
4032 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07004033 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07004034 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
4035 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07004036 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07004037 if (defined($1)) {
4038 $oldindent = expand_tabs($1);
4039 } else {
4040 $prevrawline =~ m@^\+(.*/?)\*@;
4041 $oldindent = expand_tabs($1);
4042 }
Joe Perches08eb9b82016-10-11 13:51:50 -07004043 $rawline =~ m@^\+([ \t]*)\*@;
4044 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07004045 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07004046 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07004047 WARN("BLOCK_COMMENT_STYLE",
4048 "Block comments should align the * on each line\n" . $hereprev);
4049 }
4050 }
4051
Joe Perches7f619192014-08-06 16:10:39 -07004052# check for missing blank lines after struct/union declarations
4053# with exceptions for various attributes and macros
4054 if ($prevline =~ /^[\+ ]};?\s*$/ &&
4055 $line =~ /^\+/ &&
4056 !($line =~ /^\+\s*$/ ||
Lee Jonesabea1cb2023-12-15 09:36:19 +00004057 $line =~ /^\+\s*(?:EXPORT_SYMBOL|early_param)/ ||
Joe Perches7f619192014-08-06 16:10:39 -07004058 $line =~ /^\+\s*MODULE_/i ||
4059 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
4060 $line =~ /^\+[a-z_]*init/ ||
4061 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
4062 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08004063 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07004064 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07004065 if (CHK("LINE_SPACING",
4066 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
4067 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004068 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07004069 }
Joe Perches7f619192014-08-06 16:10:39 -07004070 }
4071
Joe Perches365dd4e2014-08-06 16:10:42 -07004072# check for multiple consecutive blank lines
4073 if ($prevline =~ /^[\+ ]\s*$/ &&
4074 $line =~ /^\+\s*$/ &&
4075 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07004076 if (CHK("LINE_SPACING",
4077 "Please don't use multiple blank lines\n" . $hereprev) &&
4078 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004079 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004080 }
4081
Joe Perches365dd4e2014-08-06 16:10:42 -07004082 $last_blank_line = $linenr;
4083 }
4084
Joe Perches3b617e32014-04-03 14:49:28 -07004085# check for missing blank lines after declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08004086# (declarations must have the same indentation and not be at the start of line)
4087 if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
4088 # use temporaries
4089 my $sl = $sline;
4090 my $pl = $prevline;
4091 # remove $Attribute/$Sparse uses to simplify comparisons
4092 $sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
4093 $pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
4094 if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07004095 # function pointer declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08004096 $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07004097 # foo bar; where foo is some local typedef or #define
Joe Perchesb5e87362021-02-25 17:21:40 -08004098 $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07004099 # known declaration macros
Joe Perchesb5e87362021-02-25 17:21:40 -08004100 $pl =~ /^\+\s+$declaration_macros/) &&
Joe Perches3f7bac02014-06-04 16:12:04 -07004101 # for "else if" which can look like "$Ident $Ident"
Joe Perchesb5e87362021-02-25 17:21:40 -08004102 !($pl =~ /^\+\s+$c90_Keywords\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07004103 # other possible extensions of declaration lines
Joe Perchesb5e87362021-02-25 17:21:40 -08004104 $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07004105 # not starting a section or a macro "\" extended line
Joe Perchesb5e87362021-02-25 17:21:40 -08004106 $pl =~ /(?:\{\s*|\\)$/) &&
Joe Perches3f7bac02014-06-04 16:12:04 -07004107 # looks like a declaration
Joe Perchesb5e87362021-02-25 17:21:40 -08004108 !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07004109 # function pointer declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08004110 $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07004111 # foo bar; where foo is some local typedef or #define
Joe Perchesb5e87362021-02-25 17:21:40 -08004112 $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07004113 # known declaration macros
Joe Perchesb5e87362021-02-25 17:21:40 -08004114 $sl =~ /^\+\s+$declaration_macros/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07004115 # start of struct or union or enum
Joe Perchesb5e87362021-02-25 17:21:40 -08004116 $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07004117 # start or end of block or continuation of declaration
Joe Perchesb5e87362021-02-25 17:21:40 -08004118 $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07004119 # bitfield continuation
Joe Perchesb5e87362021-02-25 17:21:40 -08004120 $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07004121 # other possible extensions of declaration lines
Joe Perchesb5e87362021-02-25 17:21:40 -08004122 $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
4123 if (WARN("LINE_SPACING",
4124 "Missing a blank line after declarations\n" . $hereprev) &&
4125 $fix) {
4126 fix_insert_line($fixlinenr, "\+");
4127 }
Joe Perchesd752fcc2014-08-06 16:11:05 -07004128 }
Joe Perches3b617e32014-04-03 14:49:28 -07004129 }
4130
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07004131# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07004132# Exceptions:
4133# 1) within comments
4134# 2) indented preprocessor commands
4135# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07004136 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07004137 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004138 if (WARN("LEADING_SPACE",
4139 "please, no spaces at the start of a line\n" . $herevet) &&
4140 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004141 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07004142 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07004143 }
4144
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07004145# check we are in a valid C source file if not then ignore this hunk
4146 next if ($realfile !~ /\.(h|c)$/);
4147
Joe Perches5751a242017-11-17 15:28:52 -08004148# check for unusual line ending [ or (
4149 if ($line =~ /^\+.*([\[\(])\s*$/) {
4150 CHK("OPEN_ENDED_LINE",
4151 "Lines should not end with a '$1'\n" . $herecurr);
4152 }
4153
Joe Perches4dbed762017-05-08 15:55:39 -07004154# check if this appears to be the start function declaration, save the name
4155 if ($sline =~ /^\+\{\s*$/ &&
4156 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
4157 $context_function = $1;
4158 }
4159
4160# check if this appears to be the end of function declaration
4161 if ($sline =~ /^\+\}\s*$/) {
4162 undef $context_function;
4163 }
4164
Joe Perches032a4c02014-08-06 16:10:29 -07004165# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07004166# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07004167# if the previous line is a break or return and is indented 1 tab more...
4168 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
4169 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07004170 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
4171 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
4172 defined $lines[$linenr] &&
4173 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07004174 WARN("UNNECESSARY_ELSE",
4175 "else is not generally useful after a break or return\n" . $hereprev);
4176 }
4177 }
4178
Joe Perchesc00df192014-08-06 16:11:01 -07004179# check indentation of a line with a break;
Joe Perchesdc58bc52020-12-15 20:44:33 -08004180# if the previous line is a goto, return or break
4181# and is indented the same # of tabs
Joe Perchesc00df192014-08-06 16:11:01 -07004182 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
4183 my $tabs = $1;
Joe Perchesdc58bc52020-12-15 20:44:33 -08004184 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
4185 if (WARN("UNNECESSARY_BREAK",
4186 "break is not useful after a $1\n" . $hereprev) &&
4187 $fix) {
4188 fix_delete_line($fixlinenr, $rawline);
4189 }
Joe Perchesc00df192014-08-06 16:11:01 -07004190 }
4191 }
4192
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004193# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08004194 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004195 WARN("CVS_KEYWORD",
4196 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004197 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004198
Joe Perches56e77d72013-02-21 16:44:14 -08004199# check for old HOTPLUG __dev<foo> section markings
4200 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
4201 WARN("HOTPLUG_SECTION",
4202 "Using $1 is unnecessary\n" . $herecurr);
4203 }
4204
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004205# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004206 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
4207 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004208#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07004209 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07004210 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004211 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004212 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004213 $stat =~ s/\n./\n /g;
4214 $cond =~ s/\n./\n /g;
4215
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004216#print "linenr<$linenr> <$stat>\n";
4217 # If this statement has no statement boundaries within
4218 # it there is no point in retrying a statement scan
4219 # until we hit end of it.
4220 my $frag = $stat; $frag =~ s/;+\s*$//;
4221 if ($frag !~ /(?:{|;)/) {
4222#print "skip<$line_nr_next>\n";
4223 $suppress_statement = $line_nr_next;
4224 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004225
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004226 # Find the real next line.
4227 $realline_next = $line_nr_next;
4228 if (defined $realline_next &&
4229 (!defined $lines[$realline_next - 1] ||
4230 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
4231 $realline_next++;
4232 }
4233
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004234 my $s = $stat;
4235 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004236
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004237 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004238 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004239
4240 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004241 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004242
Andy Whitcroft463f2862009-09-21 17:04:34 -07004243 } elsif ($s =~ /^.\s*else\b/s) {
4244
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004245 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07004246 } 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) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004247 my $type = $1;
4248 $type =~ s/\s+/ /g;
4249 possible($type, "A:" . $s);
4250
Andy Whitcroft8905a672007-11-28 16:21:06 -08004251 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07004252 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004253 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004254 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004255
4256 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08004257 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004258 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004259 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004260
4261 # Check for any sort of function declaration.
4262 # int foo(something bar, other baz);
4263 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004264 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08004265 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004266
Andy Whitcroftcf655042008-03-04 14:28:20 -08004267 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004268 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004269 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004270
Andy Whitcroft8905a672007-11-28 16:21:06 -08004271 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004272 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08004273
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004274 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004275 }
4276 }
4277 }
4278
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004279 }
4280
Andy Whitcroft653d4872007-06-23 17:16:34 -07004281#
4282# Checks which may be anchored in the context.
4283#
4284
4285# Check for switch () and associated case and default
4286# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07004287 if ($line=~/\bswitch\s*\(.*\)/) {
4288 my $err = '';
4289 my $sep = '';
4290 my @ctx = ctx_block_outer($linenr, $realcnt);
4291 shift(@ctx);
4292 for my $ctx (@ctx) {
4293 my ($clen, $cindent) = line_stats($ctx);
4294 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
4295 $indent != $cindent) {
4296 $err .= "$sep$ctx\n";
4297 $sep = '';
4298 } else {
4299 $sep = "[...]\n";
4300 }
4301 }
4302 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004303 ERROR("SWITCH_CASE_INDENT_LEVEL",
4304 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004305 }
4306 }
4307
4308# if/while/etc brace do not go on next line, unless defining a do while loop,
4309# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07004310 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004311 my $pre_ctx = "$1$2";
4312
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004313 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08004314
4315 if ($line =~ /^\+\t{6,}/) {
4316 WARN("DEEP_INDENTATION",
4317 "Too many leading tabs - consider code refactoring\n" . $herecurr);
4318 }
4319
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004320 my $ctx_cnt = $realcnt - $#ctx - 1;
4321 my $ctx = join("\n", @ctx);
4322
Andy Whitcroft548596d2008-07-23 21:29:01 -07004323 my $ctx_ln = $linenr;
4324 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004325
Andy Whitcroft548596d2008-07-23 21:29:01 -07004326 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4327 defined $lines[$ctx_ln - 1] &&
4328 $lines[$ctx_ln - 1] =~ /^-/)) {
4329 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4330 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004331 $ctx_ln++;
4332 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07004333
Andy Whitcroft53210162008-07-23 21:29:03 -07004334 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
4335 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07004336
Joe Perchesd752fcc2014-08-06 16:11:05 -07004337 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004338 ERROR("OPEN_BRACE",
4339 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07004340 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07004341 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004342 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4343 $ctx =~ /\)\s*\;\s*$/ &&
4344 defined $lines[$ctx_ln - 1])
4345 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004346 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
4347 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004348 WARN("TRAILING_SEMICOLON",
4349 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07004350 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004351 }
4352 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004353 }
4354
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004355# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07004356 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004357 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4358 ctx_statement_block($linenr, $realcnt, 0)
4359 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004360 my ($s, $c) = ($stat, $cond);
4361
4362 substr($s, 0, length($c), '');
4363
Joe Perches9f5af482015-09-09 15:37:30 -07004364 # remove inline comments
4365 $s =~ s/$;/ /g;
4366 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004367
4368 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07004369 my @newlines = ($c =~ /\n/gs);
4370 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004371
Joe Perches9f5af482015-09-09 15:37:30 -07004372 # Make sure we remove the line prefixes as we have
4373 # none on the first line, and are going to readd them
4374 # where necessary.
4375 $s =~ s/\n./\n/gs;
4376 while ($s =~ /\n\s+\\\n/) {
4377 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
4378 }
4379
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004380 # We want to check the first line inside the block
4381 # starting at the end of the conditional, so remove:
4382 # 1) any blank line termination
4383 # 2) any opening brace { on end of the line
4384 # 3) any do (...) {
4385 my $continuation = 0;
4386 my $check = 0;
4387 $s =~ s/^.*\bdo\b//;
4388 $s =~ s/^\s*{//;
4389 if ($s =~ s/^\s*\\//) {
4390 $continuation = 1;
4391 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004392 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004393 $check = 1;
4394 $cond_lines++;
4395 }
4396
4397 # Also ignore a loop construct at the end of a
4398 # preprocessor statement.
4399 if (($prevline =~ /^.\s*#\s*define\s/ ||
4400 $prevline =~ /\\\s*$/) && $continuation == 0) {
4401 $check = 0;
4402 }
4403
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004404 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07004405 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004406 while ($cond_ptr != $cond_lines) {
4407 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004408
Andy Whitcroftf16fa282008-10-15 22:02:32 -07004409 # If we see an #else/#elif then the code
4410 # is not linear.
4411 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4412 $check = 0;
4413 }
4414
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004415 # Ignore:
4416 # 1) blank lines, they should be at 0,
4417 # 2) preprocessor lines, and
4418 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07004419 if ($continuation ||
4420 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004421 $s =~ /^\s*#\s*?/ ||
4422 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07004423 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07004424 if ($s =~ s/^.*?\n//) {
4425 $cond_lines++;
4426 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004427 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004428 }
4429
4430 my (undef, $sindent) = line_stats("+" . $s);
4431 my $stat_real = raw_line($linenr, $cond_lines);
4432
4433 # Check if either of these lines are modified, else
4434 # this is not this patch's fault.
4435 if (!defined($stat_real) ||
4436 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
4437 $check = 0;
4438 }
4439 if (defined($stat_real) && $cond_lines > 1) {
4440 $stat_real = "[...]\n$stat_real";
4441 }
4442
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004443 #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";
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004444
Joe Perches9f5af482015-09-09 15:37:30 -07004445 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07004446 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07004447 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07004448 ($sindent == $indent &&
4449 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07004450 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004451 WARN("SUSPECT_CODE_INDENT",
4452 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004453 }
4454 }
4455
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004456 # Track the 'values' across context and added lines.
4457 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004458 my ($curr_values, $curr_vars) =
4459 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004460 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004461 if ($dbg_values) {
4462 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004463 print "$linenr > .$outline\n";
4464 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004465 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004466 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004467 $prev_values = substr($curr_values, -1);
4468
Andy Whitcroft00df3442007-06-08 13:47:06 -07004469#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07004470 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07004471
Joe Perches99ca38c2020-10-15 20:12:09 -07004472# check for self assignments used to avoid compiler warnings
4473# e.g.: int foo = foo, *bar = NULL;
4474# struct foo bar = *(&(bar));
4475 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
4476 my $var = $1;
4477 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4478 WARN("SELF_ASSIGNMENT",
4479 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4480 }
4481 }
4482
Joe Perches11ca40a2016-12-12 16:46:31 -08004483# check for dereferences that span multiple lines
4484 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4485 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4486 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4487 my $ref = $1;
4488 $line =~ /^.\s*($Lval)/;
4489 $ref .= $1;
4490 $ref =~ s/\s//g;
4491 WARN("MULTILINE_DEREFERENCE",
4492 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4493 }
4494
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004495# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07004496 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004497 my $type = $1;
4498 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07004499 $var = "" if (!defined $var);
4500 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004501 my $sign = $1;
4502 my $pointer = $2;
4503
4504 $pointer = "" if (!defined $pointer);
4505
4506 if (WARN("UNSPECIFIED_INT",
4507 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4508 $fix) {
4509 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07004510 my $comp_pointer = $pointer;
4511 $comp_pointer =~ s/\s//g;
4512 $decl .= $comp_pointer;
4513 $decl = rtrim($decl) if ($var eq "");
4514 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004515 }
4516 }
4517 }
4518
Andy Whitcroft653d4872007-06-23 17:16:34 -07004519# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07004520 if ($dbg_type) {
4521 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004522 ERROR("TEST_TYPE",
4523 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004524 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004525 ERROR("TEST_NOT_TYPE",
4526 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004527 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004528 next;
4529 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004530# TEST: allow direct testing of the attribute matcher.
4531 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004532 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004533 ERROR("TEST_ATTR",
4534 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004535 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004536 ERROR("TEST_NOT_ATTR",
4537 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004538 }
4539 next;
4540 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004541
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004542# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07004543 if ($line =~ /^.\s*{/ &&
4544 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07004545 if (ERROR("OPEN_BRACE",
4546 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004547 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4548 fix_delete_line($fixlinenr - 1, $prevrawline);
4549 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004550 my $fixedline = $prevrawline;
4551 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004552 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004553 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004554 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004555 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004556 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004557 }
4558
Andy Whitcroft653d4872007-06-23 17:16:34 -07004559#
4560# Checks which are anchored on the added line.
4561#
4562
4563# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004564 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07004565 my $path = $1;
4566 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004567 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08004568 "malformed #include filename\n" . $herecurr);
4569 }
4570 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4571 ERROR("UAPI_INCLUDE",
4572 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004573 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004574 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004575
4576# no C99 // comments
4577 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07004578 if (ERROR("C99_COMMENTS",
4579 "do not use C99 // comments\n" . $herecurr) &&
4580 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004581 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07004582 if ($line =~ /\/\/(.*)$/) {
4583 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07004584 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07004585 }
4586 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004587 }
4588 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004589 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004590 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004591
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004592# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4593# the whole statement.
4594#print "APW <$lines[$realline_next - 1]>\n";
4595 if (defined $realline_next &&
4596 exists $lines[$realline_next - 1] &&
4597 !defined $suppress_export{$realline_next} &&
Christoph Hellwig36794822021-02-02 13:13:34 +01004598 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004599 # Handle definitions which produce identifiers with
4600 # a prefix:
4601 # XXX(foo);
4602 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004603 my $name = $1;
Lee Jonesabea1cb2023-12-15 09:36:19 +00004604 $name =~ s/^\s*($Ident).*/$1/;
Andy Whitcroft87a53872012-01-10 15:10:04 -08004605 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004606 $name =~ /^${Ident}_$2/) {
4607#print "FOO C name<$name>\n";
4608 $suppress_export{$realline_next} = 1;
4609
4610 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004611 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07004612 ^.DEFINE_$Ident\(\Q$name\E\)|
4613 ^.DECLARE_$Ident\(\Q$name\E\)|
4614 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004615 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4616 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07004617 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004618#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4619 $suppress_export{$realline_next} = 2;
4620 } else {
4621 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004622 }
4623 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004624 if (!defined $suppress_export{$linenr} &&
4625 $prevline =~ /^.\s*$/ &&
Christoph Hellwig36794822021-02-02 13:13:34 +01004626 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004627#print "FOO B <$lines[$linenr - 1]>\n";
4628 $suppress_export{$linenr} = 2;
4629 }
4630 if (defined $suppress_export{$linenr} &&
4631 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004632 WARN("EXPORT_SYMBOL",
4633 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004634 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004635
Joe Eloff5150bda2010-08-09 17:21:00 -07004636# check for global initialisers.
Song Liu5b8f82e2021-02-25 17:22:08 -08004637 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
4638 !exclude_global_initialisers($realfile)) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004639 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004640 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004641 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004642 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004643 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004644 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004645# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004646 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004647 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004648 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004649 $herecurr) &&
4650 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004651 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004652 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004653 }
4654
Joe Perches18130872014-08-06 16:11:22 -07004655# check for misordered declarations of char/short/int/long with signed/unsigned
4656 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4657 my $tmp = trim($1);
4658 WARN("MISORDERED_TYPE",
4659 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4660 }
4661
Joe Perches809e0822018-08-21 21:58:12 -07004662# check for unnecessary <signed> int declarations of short/long/long long
4663 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4664 my $type = trim($1);
4665 next if ($type !~ /\bint\b/);
4666 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4667 my $new_type = $type;
4668 $new_type =~ s/\b\s*int\s*\b/ /;
4669 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4670 $new_type =~ s/^const\s+//;
4671 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4672 $new_type = "const $new_type" if ($type =~ /^const\b/);
4673 $new_type =~ s/\s+/ /g;
4674 $new_type = trim($new_type);
4675 if (WARN("UNNECESSARY_INT",
4676 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4677 $fix) {
4678 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4679 }
4680 }
4681
Joe Perchescb710ec2010-10-26 14:23:20 -07004682# check for static const char * arrays.
4683 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004684 WARN("STATIC_CONST_CHAR_ARRAY",
4685 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004686 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004687 }
4688
4689# check for initialized const char arrays that should be static const
4690 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4691 if (WARN("STATIC_CONST_CHAR_ARRAY",
4692 "const array should probably be static const\n" . $herecurr) &&
4693 $fix) {
4694 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4695 }
4696 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004697
4698# check for static char foo[] = "bar" declarations.
4699 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004700 WARN("STATIC_CONST_CHAR_ARRAY",
4701 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004702 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004703 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004704
Joe Perchesab7e23f2015-04-16 12:44:22 -07004705# check for const <foo> const where <foo> is not a pointer or array type
4706 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4707 my $found = $1;
4708 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4709 WARN("CONST_CONST",
4710 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4711 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4712 WARN("CONST_CONST",
4713 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4714 }
4715 }
4716
Joe Perches73169762020-12-15 20:44:30 -08004717# check for const static or static <non ptr type> const declarations
4718# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4719 if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4720 $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4721 if (WARN("STATIC_CONST",
4722 "Move const after static - use 'static const $1'\n" . $herecurr) &&
4723 $fix) {
4724 $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4725 $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4726 }
4727 }
4728
Joe Perches9b0fa602014-04-03 14:49:18 -07004729# check for non-global char *foo[] = {"bar", ...} declarations.
4730 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4731 WARN("STATIC_CONST_CHAR_ARRAY",
4732 "char * array declaration might be better as static const\n" .
4733 $herecurr);
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08004734 }
Joe Perches9b0fa602014-04-03 14:49:18 -07004735
Joe Perchesb598b672015-04-16 12:44:36 -07004736# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4737 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4738 my $array = $1;
4739 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4740 my $array_div = $1;
4741 if (WARN("ARRAY_SIZE",
4742 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4743 $fix) {
4744 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4745 }
4746 }
4747 }
4748
Joe Perchesb36190c2014-01-27 17:07:18 -08004749# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004750 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004751 if (ERROR("FUNCTION_WITHOUT_ARGS",
4752 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4753 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004754 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004755 }
4756 }
4757
Andy Whitcroft653d4872007-06-23 17:16:34 -07004758# check for new typedefs, only function parameters and sparse annotations
4759# make sense.
4760 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004761 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004762 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004763 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004764 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004765 WARN("NEW_TYPEDEFS",
4766 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004767 }
4768
4769# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004770 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004771 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4772 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004773 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004774
Andy Whitcroft65863862009-01-06 14:41:21 -08004775 # Should start with a space.
4776 $to =~ s/^(\S)/ $1/;
4777 # Should not end with a space.
4778 $to =~ s/\s+$//;
4779 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004780 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004781 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004782
Joe Perches3705ce52013-07-03 15:05:31 -07004783## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004784 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004785 if (ERROR("POINTER_LOCATION",
4786 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4787 $fix) {
4788 my $sub_from = $ident;
4789 my $sub_to = $ident;
4790 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004791 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004792 s@\Q$sub_from\E@$sub_to@;
4793 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004794 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004795 }
4796 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4797 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004798 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004799
Andy Whitcroft65863862009-01-06 14:41:21 -08004800 # Should start with a space.
4801 $to =~ s/^(\S)/ $1/;
4802 # Should not end with a space.
4803 $to =~ s/\s+$//;
4804 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004805 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004806 }
4807 # Modifiers should have spaces.
4808 $to =~ s/(\b$Modifier$)/$1 /;
4809
Joe Perches3705ce52013-07-03 15:05:31 -07004810## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004811 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004812 if (ERROR("POINTER_LOCATION",
4813 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4814 $fix) {
4815
4816 my $sub_from = $match;
4817 my $sub_to = $match;
4818 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004819 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004820 s@\Q$sub_from\E@$sub_to@;
4821 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004822 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004823 }
4824
Lee Jonesabea1cb2023-12-15 09:36:19 +00004825# do not use BUG() or variants
4826 if ($line =~ /\b(?!AA_|BUILD_|DCCP_|IDA_|KVM_|RWLOCK_|snd_|SPIN_)(?:[a-zA-Z_]*_)?BUG(?:_ON)?(?:_[A-Z_]+)?\s*\(/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004827 my $msg_level = \&WARN;
4828 $msg_level = \&CHK if ($file);
4829 &{$msg_level}("AVOID_BUG",
Lee Jonesabea1cb2023-12-15 09:36:19 +00004830 "Do not crash the kernel unless it is absolutely unavoidable--use WARN_ON_ONCE() plus recovery code (if feasible) instead of BUG() or variants\n" . $herecurr);
Joe Perches9d3e3c72015-09-09 15:37:27 -07004831 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004832
Joe Perches9d3e3c72015-09-09 15:37:27 -07004833# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004834 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004835 WARN("LINUX_VERSION_CODE",
4836 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004837 }
4838
Joe Perches17441222011-06-15 15:08:17 -07004839# check for uses of printk_ratelimit
4840 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004841 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004842 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004843 }
4844
Joe Percheseeef5732017-11-17 15:28:41 -08004845# printk should use KERN_* levels
4846 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4847 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4848 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004849 }
4850
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004851# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4852 if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4853 my $printk = $1;
4854 my $modifier = $2;
4855 my $orig = $3;
4856 $modifier = "" if (!defined($modifier));
Joe Perches243f3802012-05-31 16:26:09 -07004857 my $level = lc($orig);
4858 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004859 my $level2 = $level;
4860 $level2 = "dbg" if ($level eq "debug");
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004861 $level .= $modifier;
4862 $level2 .= $modifier;
Joe Perches243f3802012-05-31 16:26:09 -07004863 WARN("PREFER_PR_LEVEL",
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004864 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to $printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07004865 }
4866
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004867# prefer dev_<level> to dev_printk(KERN_<LEVEL>
Joe Perchesdc139312013-02-21 16:44:13 -08004868 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4869 my $orig = $1;
4870 my $level = lc($orig);
4871 $level = "warn" if ($level eq "warning");
4872 $level = "dbg" if ($level eq "debug");
4873 WARN("PREFER_DEV_LEVEL",
4874 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4875 }
4876
Nicolas Boichat8020b252020-10-15 20:12:02 -07004877# trace_printk should not be used in production code.
4878 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4879 WARN("TRACE_PRINTK",
4880 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4881 }
4882
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004883# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4884# number of false positives, but assembly files are not checked, so at
4885# least the arch entry code will not trigger this warning.
4886 if ($line =~ /\bENOSYS\b/) {
4887 WARN("ENOSYS",
4888 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4889 }
4890
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004891# ENOTSUPP is not a standard error code and should be avoided in new patches.
4892# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4893# Similarly to ENOSYS warning a small number of false positives is expected.
4894 if (!$file && $line =~ /\bENOTSUPP\b/) {
4895 if (WARN("ENOTSUPP",
4896 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4897 $fix) {
4898 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4899 }
4900 }
4901
Andy Whitcroft653d4872007-06-23 17:16:34 -07004902# function brace can't be on same line, except for #defines of do while,
4903# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004904 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004905 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4906 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4907 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004908 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004909 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004910 $fix) {
4911 fix_delete_line($fixlinenr, $rawline);
4912 my $fixed_line = $rawline;
Dwaipayan Ray03f49352020-12-15 20:45:02 -08004913 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
Joe Perches8d182472014-08-06 16:11:12 -07004914 my $line1 = $1;
4915 my $line2 = $2;
4916 fix_insert_line($fixlinenr, ltrim($line1));
4917 fix_insert_line($fixlinenr, "\+{");
4918 if ($line2 !~ /^\s*$/) {
4919 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4920 }
4921 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004922 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004923
Andy Whitcroft8905a672007-11-28 16:21:06 -08004924# open braces for enum, union and struct go on the same line.
4925 if ($line =~ /^.\s*{/ &&
4926 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004927 if (ERROR("OPEN_BRACE",
4928 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4929 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4930 fix_delete_line($fixlinenr - 1, $prevrawline);
4931 fix_delete_line($fixlinenr, $rawline);
4932 my $fixedline = rtrim($prevrawline) . " {";
4933 fix_insert_line($fixlinenr, $fixedline);
4934 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004935 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004936 if ($fixedline !~ /^\+\s*$/) {
4937 fix_insert_line($fixlinenr, $fixedline);
4938 }
4939 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004940 }
4941
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004942# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004943 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4944 if (WARN("SPACING",
4945 "missing space after $1 definition\n" . $herecurr) &&
4946 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004947 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004948 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4949 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004950 }
4951
Joe Perches31070b52014-01-23 15:54:49 -08004952# Function pointer declarations
4953# check spacing between type, funcptr, and args
4954# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004955 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004956 my $declare = $1;
4957 my $pre_pointer_space = $2;
4958 my $post_pointer_space = $3;
4959 my $funcname = $4;
4960 my $post_funcname_space = $5;
4961 my $pre_args_space = $6;
4962
Joe Perches91f72e92014-04-03 14:49:12 -07004963# the $Declare variable will capture all spaces after the type
4964# so check it for a missing trailing missing space but pointer return types
4965# don't need a space so don't warn for those.
4966 my $post_declare_space = "";
4967 if ($declare =~ /(\s+)$/) {
4968 $post_declare_space = $1;
4969 $declare = rtrim($declare);
4970 }
4971 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004972 WARN("SPACING",
4973 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004974 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004975 }
4976
4977# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004978# This test is not currently implemented because these declarations are
4979# equivalent to
4980# int foo(int bar, ...)
4981# and this is form shouldn't/doesn't generate a checkpatch warning.
4982#
4983# elsif ($declare =~ /\s{2,}$/) {
4984# WARN("SPACING",
4985# "Multiple spaces after return type\n" . $herecurr);
4986# }
Joe Perches31070b52014-01-23 15:54:49 -08004987
4988# unnecessary space "type ( *funcptr)(args...)"
4989 if (defined $pre_pointer_space &&
4990 $pre_pointer_space =~ /^\s/) {
4991 WARN("SPACING",
4992 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4993 }
4994
4995# unnecessary space "type (* funcptr)(args...)"
4996 if (defined $post_pointer_space &&
4997 $post_pointer_space =~ /^\s/) {
4998 WARN("SPACING",
4999 "Unnecessary space before function pointer name\n" . $herecurr);
5000 }
5001
5002# unnecessary space "type (*funcptr )(args...)"
5003 if (defined $post_funcname_space &&
5004 $post_funcname_space =~ /^\s/) {
5005 WARN("SPACING",
5006 "Unnecessary space after function pointer name\n" . $herecurr);
5007 }
5008
5009# unnecessary space "type (*funcptr) (args...)"
5010 if (defined $pre_args_space &&
5011 $pre_args_space =~ /^\s/) {
5012 WARN("SPACING",
5013 "Unnecessary space before function pointer arguments\n" . $herecurr);
5014 }
5015
5016 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005017 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07005018 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08005019 }
5020 }
5021
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07005022# check for spacing round square brackets; allowed:
5023# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07005024# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
5025# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07005026 while ($line =~ /(.*?\s)\[/g) {
5027 my ($where, $prefix) = ($-[1], $1);
5028 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07005029 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07005030 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005031 if (ERROR("BRACKET_SPACE",
5032 "space prohibited before open square bracket '['\n" . $herecurr) &&
5033 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005034 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005035 s/^(\+.*?)\s+\[/$1\[/;
5036 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07005037 }
5038 }
5039
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005040# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005041 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005042 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005043 my $ctx_before = substr($line, 0, $-[1]);
5044 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005045
5046 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005047 if ($name =~ /^(?:
5048 if|for|while|switch|return|case|
5049 volatile|__volatile__|
5050 __attribute__|format|__extension__|
Lee Jonesabea1cb2023-12-15 09:36:19 +00005051 asm|__asm__|scoped_guard)$/x)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005052 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005053 # cpp #define statements have non-optional spaces, ie
5054 # if there is a space between the name and the open
5055 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005056 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005057
5058 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005059 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005060
5061 # If this whole things ends with a type its most
5062 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005063 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005064
5065 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07005066 if (WARN("SPACING",
5067 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
5068 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005069 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005070 s/\b$name\s+\(/$name\(/;
5071 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005072 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005073 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07005074
Andy Whitcroft653d4872007-06-23 17:16:34 -07005075# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005076 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005077 my $fixed_line = "";
5078 my $line_fixed = 0;
5079
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005080 my $ops = qr{
5081 <<=|>>=|<=|>=|==|!=|
5082 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
5083 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005084 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08005085 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005086 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08005087 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07005088
5089## print("element count: <" . $#elements . ">\n");
5090## foreach my $el (@elements) {
5091## print("el: <$el>\n");
5092## }
5093
5094 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07005095 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005096
Joe Perches3705ce52013-07-03 15:05:31 -07005097 foreach my $el (@elements) {
5098 push(@fix_elements, substr($rawline, $off, length($el)));
5099 $off += length($el);
5100 }
5101
5102 $off = 0;
5103
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005104 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07005105 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005106
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005107 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07005108
5109 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
5110
5111## print("n: <$n> good: <$good>\n");
5112
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005113 $off += length($elements[$n]);
5114
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005115 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005116 my $ca = substr($opline, 0, $off);
5117 my $cc = '';
5118 if (length($opline) >= ($off + length($elements[$n + 1]))) {
5119 $cc = substr($opline, $off + length($elements[$n + 1]));
5120 }
5121 my $cb = "$ca$;$cc";
5122
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005123 my $a = '';
5124 $a = 'V' if ($elements[$n] ne '');
5125 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005126 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005127 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
5128 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07005129 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005130
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005131 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005132
5133 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005134 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005135 $c = 'V' if ($elements[$n + 2] ne '');
5136 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005137 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005138 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
5139 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08005140 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005141 } else {
5142 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005143 }
5144
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005145 my $ctx = "${a}x${c}";
5146
5147 my $at = "(ctx:$ctx)";
5148
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005149 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005150 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005151
Andy Whitcroft74048ed2008-07-23 21:29:10 -07005152 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005153 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005154
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005155 # Get the full operator variant.
5156 my $opv = $op . substr($curr_vars, $off, 1);
5157
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005158 # Ignore operators passed as parameters.
5159 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07005160 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005161
Andy Whitcroftcf655042008-03-04 14:28:20 -08005162# # Ignore comments
5163# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005164
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005165 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005166 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005167 if ($ctx !~ /.x[WEBC]/ &&
5168 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005169 if (ERROR("SPACING",
5170 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005171 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07005172 $line_fixed = 1;
5173 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005174 }
5175
5176 # // is a comment
5177 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005178
Joe Perchesb00e4812014-04-03 14:49:33 -07005179 # : when part of a bitfield
5180 } elsif ($opv eq ':B') {
5181 # skip the bitfield test for now
5182
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005183 # No spaces for:
5184 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07005185 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005186 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005187 if (ERROR("SPACING",
5188 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005189 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005190 if (defined $fix_elements[$n + 2]) {
5191 $fix_elements[$n + 2] =~ s/^\s+//;
5192 }
Joe Perchesb34c6482013-09-11 14:24:01 -07005193 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07005194 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005195 }
5196
Joe Perches23810972014-12-10 15:51:32 -08005197 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005198 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08005199 my $rtrim_before = 0;
5200 my $space_after = 0;
5201 if ($ctx =~ /Wx./) {
5202 if (ERROR("SPACING",
5203 "space prohibited before that '$op' $at\n" . $hereptr)) {
5204 $line_fixed = 1;
5205 $rtrim_before = 1;
5206 }
5207 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005208 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005209 if (ERROR("SPACING",
5210 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005211 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07005212 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08005213 $space_after = 1;
5214 }
5215 }
5216 if ($rtrim_before || $space_after) {
5217 if ($rtrim_before) {
5218 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
5219 } else {
5220 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
5221 }
5222 if ($space_after) {
5223 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07005224 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005225 }
5226
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005227 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07005228 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005229 #warn "'*' is part of type\n";
5230
5231 # unary operators should have a space before and
5232 # none after. May be left adjacent to another
5233 # unary operator, or a cast
5234 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07005235 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07005236 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005237 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005238 if (ERROR("SPACING",
5239 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005240 if ($n != $last_after + 2) {
5241 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
5242 $line_fixed = 1;
5243 }
Joe Perches3705ce52013-07-03 15:05:31 -07005244 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005245 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08005246 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005247 # A unary '*' may be const
5248
5249 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005250 if (ERROR("SPACING",
5251 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005252 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005253 if (defined $fix_elements[$n + 2]) {
5254 $fix_elements[$n + 2] =~ s/^\s+//;
5255 }
Joe Perchesb34c6482013-09-11 14:24:01 -07005256 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07005257 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005258 }
5259
5260 # unary ++ and unary -- are allowed no space on one side.
5261 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005262 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005263 if (ERROR("SPACING",
5264 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005265 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07005266 $line_fixed = 1;
5267 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005268 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005269 if ($ctx =~ /Wx[BE]/ ||
5270 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005271 if (ERROR("SPACING",
5272 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005273 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005274 $line_fixed = 1;
5275 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005276 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005277 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005278 if (ERROR("SPACING",
5279 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005280 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005281 if (defined $fix_elements[$n + 2]) {
5282 $fix_elements[$n + 2] =~ s/^\s+//;
5283 }
Joe Perchesb34c6482013-09-11 14:24:01 -07005284 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07005285 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005286 }
5287
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005288 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005289 } elsif ($op eq '<<' or $op eq '>>' or
5290 $op eq '&' or $op eq '^' or $op eq '|' or
5291 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005292 $op eq '*' or $op eq '/' or
5293 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005294 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08005295 if ($check) {
5296 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
5297 if (CHK("SPACING",
5298 "spaces preferred around that '$op' $at\n" . $hereptr)) {
5299 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5300 $fix_elements[$n + 2] =~ s/^\s+//;
5301 $line_fixed = 1;
5302 }
5303 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
5304 if (CHK("SPACING",
5305 "space preferred before that '$op' $at\n" . $hereptr)) {
5306 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
5307 $line_fixed = 1;
5308 }
5309 }
5310 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005311 if (ERROR("SPACING",
5312 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005313 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5314 if (defined $fix_elements[$n + 2]) {
5315 $fix_elements[$n + 2] =~ s/^\s+//;
5316 }
Joe Perches3705ce52013-07-03 15:05:31 -07005317 $line_fixed = 1;
5318 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005319 }
5320
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005321 # A colon needs no spaces before when it is
5322 # terminating a case value or a label.
5323 } elsif ($opv eq ':C' || $opv eq ':L') {
Chris Down263afd32021-02-25 17:22:04 -08005324 if ($ctx =~ /Wx./ and $realfile !~ m@.*\.lds\.h$@) {
Joe Perches3705ce52013-07-03 15:05:31 -07005325 if (ERROR("SPACING",
5326 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005327 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005328 $line_fixed = 1;
5329 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005330 }
5331
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005332 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08005333 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005334 my $ok = 0;
5335
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005336 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005337 if (($op eq '<' &&
5338 $cc =~ /^\S+\@\S+>/) ||
5339 ($op eq '>' &&
5340 $ca =~ /<\S+\@\S+$/))
5341 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07005342 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005343 }
5344
Joe Perchese0df7e12015-04-16 12:44:53 -07005345 # for asm volatile statements
5346 # ignore a colon with another
5347 # colon immediately before or after
5348 if (($op eq ':') &&
5349 ($ca =~ /:$/ || $cc =~ /^:/)) {
5350 $ok = 1;
5351 }
5352
Joe Perches84731622013-11-12 15:10:05 -08005353 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005354 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07005355 my $msg_level = \&ERROR;
5356 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08005357
Jean Delvare0675a8f2017-09-08 16:16:07 -07005358 if (&{$msg_level}("SPACING",
5359 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005360 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5361 if (defined $fix_elements[$n + 2]) {
5362 $fix_elements[$n + 2] =~ s/^\s+//;
5363 }
Joe Perches3705ce52013-07-03 15:05:31 -07005364 $line_fixed = 1;
5365 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005366 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005367 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005368 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005369
5370## print("n: <$n> GOOD: <$good>\n");
5371
5372 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005373 }
Joe Perches3705ce52013-07-03 15:05:31 -07005374
5375 if (($#elements % 2) == 0) {
5376 $fixed_line = $fixed_line . $fix_elements[$#elements];
5377 }
5378
Joe Perches194f66f2014-08-06 16:11:03 -07005379 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5380 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07005381 }
5382
5383
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005384 }
5385
Joe Perches786b6322013-07-03 15:05:32 -07005386# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08005387 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07005388 if (WARN("SPACING",
5389 "space prohibited before semicolon\n" . $herecurr) &&
5390 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005391 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07005392 s/^(\+.*\S)\s+;/$1;/;
5393 }
5394 }
5395
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005396# check for multiple assignments
5397 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005398 CHK("MULTIPLE_ASSIGNMENTS",
5399 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005400 }
5401
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005402## # check for multiple declarations, allowing for a function declaration
5403## # continuation.
5404## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
5405## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
5406##
5407## # Remove any bracketed sections to ensure we do not
Dwaipayan Raye73d2712020-12-15 20:44:56 -08005408## # falsely report the parameters of functions.
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005409## my $ln = $line;
5410## while ($ln =~ s/\([^\(\)]*\)//g) {
5411## }
5412## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005413## WARN("MULTIPLE_DECLARATION",
5414## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005415## }
5416## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005417
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005418#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07005419 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005420 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005421 if (ERROR("SPACING",
5422 "space required before the open brace '{'\n" . $herecurr) &&
5423 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005424 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07005425 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005426 }
5427
Joe Perchesc4a62ef2013-07-03 15:05:28 -07005428## # check for blank lines before declarations
5429## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5430## $prevrawline =~ /^.\s*$/) {
5431## WARN("SPACING",
5432## "No blank lines before declarations\n" . $hereprev);
5433## }
5434##
5435
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005436# closing brace should have a space following it when it has anything
5437# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07005438 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005439 if (ERROR("SPACING",
5440 "space required after that close brace '}'\n" . $herecurr) &&
5441 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005442 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07005443 s/}((?!(?:,|;|\)))\S)/} $1/;
5444 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005445 }
5446
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005447# check spacing on square brackets
5448 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005449 if (ERROR("SPACING",
5450 "space prohibited after that open square bracket '['\n" . $herecurr) &&
5451 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005452 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005453 s/\[\s+/\[/;
5454 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005455 }
5456 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005457 if (ERROR("SPACING",
5458 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
5459 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005460 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005461 s/\s+\]/\]/;
5462 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005463 }
5464
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005465# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005466 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
5467 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005468 if (ERROR("SPACING",
5469 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
5470 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005471 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005472 s/\(\s+/\(/;
5473 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005474 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005475 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005476 $line !~ /for\s*\(.*;\s+\)/ &&
5477 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005478 if (ERROR("SPACING",
5479 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
5480 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005481 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005482 s/\s+\)/\)/;
5483 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005484 }
5485
Joe Perchese2826fd2014-08-06 16:10:48 -07005486# check unnecessary parentheses around addressof/dereference single $Lvals
5487# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5488
5489 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08005490 my $var = $1;
5491 if (CHK("UNNECESSARY_PARENTHESES",
5492 "Unnecessary parentheses around $var\n" . $herecurr) &&
5493 $fix) {
5494 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5495 }
5496 }
5497
5498# check for unnecessary parentheses around function pointer uses
5499# ie: (foo->bar)(); should be foo->bar();
5500# but not "if (foo->bar) (" to avoid some false positives
5501 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5502 my $var = $2;
5503 if (CHK("UNNECESSARY_PARENTHESES",
5504 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5505 $fix) {
5506 my $var2 = deparenthesize($var);
5507 $var2 =~ s/\s//g;
5508 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5509 }
5510 }
Joe Perchese2826fd2014-08-06 16:10:48 -07005511
Joe Perches63b7c732017-09-08 16:16:01 -07005512# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08005513# when !drivers/staging or command-line uses --strict
5514 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07005515 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07005516 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5517 my $if_stat = $1;
5518 my $test = substr($2, 1, -1);
5519 my $herectx;
5520 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5521 my $match = $1;
5522 # avoid parentheses around potential macro args
5523 next if ($match =~ /^\s*\w+\s*$/);
5524 if (!defined($herectx)) {
5525 $herectx = $here . "\n";
5526 my $cnt = statement_rawlines($if_stat);
5527 for (my $n = 0; $n < $cnt; $n++) {
5528 my $rl = raw_line($linenr, $n);
5529 $herectx .= $rl . "\n";
5530 last if $rl =~ /^[ \+].*\{/;
5531 }
5532 }
5533 CHK("UNNECESSARY_PARENTHESES",
5534 "Unnecessary parentheses around '$match'\n" . $herectx);
5535 }
5536 }
5537
Joe Perches69078652021-06-30 18:56:22 -07005538# check that goto labels aren't indented (allow a single space indentation)
5539# and ignore bitfield definitions like foo:1
5540# Strictly, labels can have whitespace after the identifier and before the :
5541# but this is not allowed here as many ?: uses would appear to be labels
5542 if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ &&
5543 $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ &&
5544 $sline !~ /^.\s+default:/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005545 if (WARN("INDENTED_LABEL",
5546 "labels should not be indented\n" . $herecurr) &&
5547 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005548 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005549 s/^(.)\s+/$1/;
5550 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005551 }
5552
Joe Perches40873ab2020-10-15 20:11:56 -07005553# check if a statement with a comma should be two statements like:
5554# foo = bar(), /* comma should be semicolon */
5555# bar = baz();
5556 if (defined($stat) &&
5557 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5558 my $cnt = statement_rawlines($stat);
5559 my $herectx = get_stat_here($linenr, $cnt, $here);
5560 WARN("SUSPECT_COMMA_SEMICOLON",
5561 "Possible comma where semicolon could be used\n" . $herectx);
5562 }
5563
Joe Perches5b9553a2014-04-03 14:49:21 -07005564# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08005565 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005566 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07005567 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07005568 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5569 my $value = $1;
5570 $value = deparenthesize($value);
5571 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5572 ERROR("RETURN_PARENTHESES",
5573 "return is not a function, parentheses are not required\n" . $herecurr);
5574 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005575 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005576 ERROR("SPACING",
5577 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005578 }
5579 }
Joe Perches507e5142013-11-12 15:10:13 -08005580
Joe Perchesb43ae212014-06-23 13:22:07 -07005581# unnecessary return in a void function
5582# at end-of-function, with the previous line a single leading tab, then return;
5583# and the line before that not a goto label target like "out:"
5584 if ($sline =~ /^[ \+]}\s*$/ &&
5585 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5586 $linenr >= 3 &&
5587 $lines[$linenr - 3] =~ /^[ +]/ &&
5588 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07005589 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07005590 "void function return statements are not generally useful\n" . $hereprev);
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08005591 }
Joe Perches9819cf22014-06-04 16:12:09 -07005592
Joe Perches189248d2014-01-23 15:54:47 -08005593# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07005594 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08005595 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5596 my $openparens = $1;
5597 my $count = $openparens =~ tr@\(@\(@;
5598 my $msg = "";
5599 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5600 my $comp = $4; #Not $1 because of $LvalOrFunc
5601 $msg = " - maybe == should be = ?" if ($comp eq "==");
5602 WARN("UNNECESSARY_PARENTHESES",
5603 "Unnecessary parentheses$msg\n" . $herecurr);
5604 }
5605 }
5606
Joe Perchesc5595fa2015-09-09 15:37:58 -07005607# comparisons with a constant or upper case identifier on the left
5608# avoid cases like "foo + BAR < baz"
5609# only fix matches surrounded by parentheses to avoid incorrect
5610# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07005611 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005612 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5613 my $lead = $1;
5614 my $const = $2;
5615 my $comp = $3;
5616 my $to = $4;
5617 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07005618 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005619 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5620 WARN("CONSTANT_COMPARISON",
5621 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5622 $fix) {
5623 if ($comp eq "<") {
5624 $newcomp = ">";
5625 } elsif ($comp eq "<=") {
5626 $newcomp = ">=";
5627 } elsif ($comp eq ">") {
5628 $newcomp = "<";
5629 } elsif ($comp eq ">=") {
5630 $newcomp = "<=";
5631 }
5632 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5633 }
5634 }
5635
Joe Perchesf34e4a42015-04-16 12:44:19 -07005636# Return of what appears to be an errno should normally be negative
5637 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005638 my $name = $1;
Guenter Roeck46b85bf2021-06-30 18:56:25 -07005639 if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005640 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07005641 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005642 }
5643 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005644
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005645# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07005646 if ($line =~ /\b(if|while|for|switch)\(/) {
5647 if (ERROR("SPACING",
5648 "space required before the open parenthesis '('\n" . $herecurr) &&
5649 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005650 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005651 s/\b(if|while|for|switch)\(/$1 \(/;
5652 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005653 }
5654
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07005655# Check for illegal assignment in if conditional -- and check for trailing
5656# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005657 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08005658 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5659 ctx_statement_block($linenr, $realcnt, 0)
5660 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005661 my ($stat_next) = ctx_statement_block($line_nr_next,
5662 $remain_next, $off_next);
5663 $stat_next =~ s/\n./\n /g;
5664 ##print "stat<$stat> stat_next<$stat_next>\n";
5665
5666 if ($stat_next =~ /^\s*while\b/) {
5667 # If the statement carries leading newlines,
5668 # then count those as offsets.
5669 my ($whitespace) =
5670 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5671 my $offset =
5672 statement_rawlines($whitespace) - 1;
5673
5674 $suppress_whiletrailers{$line_nr_next +
5675 $offset} = 1;
5676 }
5677 }
5678 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005679 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005680 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005681 my ($s, $c) = ($stat, $cond);
Lee Jonesabea1cb2023-12-15 09:36:19 +00005682 my $fixed_assign_in_if = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08005683
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005684 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches65b64b32020-08-11 18:35:10 -07005685 if (ERROR("ASSIGN_IN_IF",
5686 "do not use assignment in if condition\n" . $herecurr) &&
5687 $fix && $perl_version_ok) {
5688 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5689 my $space = $1;
5690 my $not = $2;
5691 my $statement = $3;
5692 my $assigned = $4;
5693 my $test = $8;
5694 my $against = $9;
5695 my $brace = $15;
5696 fix_delete_line($fixlinenr, $rawline);
5697 fix_insert_line($fixlinenr, "$space$statement;");
5698 my $newline = "${space}if (";
5699 $newline .= '!' if defined($not);
5700 $newline .= '(' if (defined $not && defined($test) && defined($against));
5701 $newline .= "$assigned";
5702 $newline .= " $test $against" if (defined($test) && defined($against));
5703 $newline .= ')' if (defined $not && defined($test) && defined($against));
5704 $newline .= ')';
5705 $newline .= " {" if (defined($brace));
5706 fix_insert_line($fixlinenr + 1, $newline);
Lee Jonesabea1cb2023-12-15 09:36:19 +00005707 $fixed_assign_in_if = 1;
Joe Perches65b64b32020-08-11 18:35:10 -07005708 }
5709 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005710 }
5711
5712 # Find out what is on the end of the line after the
5713 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005714 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005715 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005716 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005717 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5718 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005719 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005720 # Find out how long the conditional actually is.
5721 my @newlines = ($c =~ /\n/gs);
5722 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005723 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005724
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005725 $stat_real = raw_line($linenr, $cond_lines)
5726 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005727 if (defined($stat_real) && $cond_lines > 1) {
5728 $stat_real = "[...]\n$stat_real";
5729 }
5730
Lee Jonesabea1cb2023-12-15 09:36:19 +00005731 if (ERROR("TRAILING_STATEMENTS",
5732 "trailing statements should be on next line\n" . $herecurr . $stat_real) &&
5733 !$fixed_assign_in_if &&
5734 $cond_lines == 0 &&
5735 $fix && $perl_version_ok &&
5736 $fixed[$fixlinenr] =~ /^\+(\s*)((?:if|while|for)\s*$balanced_parens)\s*(.*)$/) {
5737 my $indent = $1;
5738 my $test = $2;
5739 my $rest = rtrim($4);
5740 if ($rest =~ /;$/) {
5741 $fixed[$fixlinenr] = "\+$indent$test";
5742 fix_insert_line($fixlinenr + 1, "$indent\t$rest");
5743 }
5744 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005745 }
5746 }
5747
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005748# Check for bitwise tests written as boolean
5749 if ($line =~ /
5750 (?:
5751 (?:\[|\(|\&\&|\|\|)
5752 \s*0[xX][0-9]+\s*
5753 (?:\&\&|\|\|)
5754 |
5755 (?:\&\&|\|\|)
5756 \s*0[xX][0-9]+\s*
5757 (?:\&\&|\|\||\)|\])
5758 )/x)
5759 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005760 WARN("HEXADECIMAL_BOOLEAN_TEST",
5761 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005762 }
5763
Andy Whitcroft8905a672007-11-28 16:21:06 -08005764# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005765 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5766 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005767 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005768 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005769 ERROR("TRAILING_STATEMENTS",
5770 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005771 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005772 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005773# if should not continue a brace
5774 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005775 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005776 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005777 $herecurr);
5778 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005779# case and default should not have general statements after them
5780 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5781 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005782 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005783 \s*return\s+
5784 )/xg)
5785 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005786 ERROR("TRAILING_STATEMENTS",
5787 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005788 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005789
5790 # Check for }<nl>else {, these must be at the same
5791 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005792 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5793 $previndent == $indent) {
5794 if (ERROR("ELSE_AFTER_BRACE",
5795 "else should follow close brace '}'\n" . $hereprev) &&
5796 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5797 fix_delete_line($fixlinenr - 1, $prevrawline);
5798 fix_delete_line($fixlinenr, $rawline);
5799 my $fixedline = $prevrawline;
5800 $fixedline =~ s/}\s*$//;
5801 if ($fixedline !~ /^\+\s*$/) {
5802 fix_insert_line($fixlinenr, $fixedline);
5803 }
5804 $fixedline = $rawline;
5805 $fixedline =~ s/^(.\s*)else/$1} else/;
5806 fix_insert_line($fixlinenr, $fixedline);
5807 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005808 }
5809
Joe Perches8b8856f2014-08-06 16:11:14 -07005810 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5811 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005812 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5813
5814 # Find out what is on the end of the line after the
5815 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005816 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005817 $s =~ s/\n.*//g;
5818
5819 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005820 if (ERROR("WHILE_AFTER_BRACE",
5821 "while should follow close brace '}'\n" . $hereprev) &&
5822 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5823 fix_delete_line($fixlinenr - 1, $prevrawline);
5824 fix_delete_line($fixlinenr, $rawline);
5825 my $fixedline = $prevrawline;
5826 my $trailing = $rawline;
5827 $trailing =~ s/^\+//;
5828 $trailing = trim($trailing);
5829 $fixedline =~ s/}\s*$/} $trailing/;
5830 fix_insert_line($fixlinenr, $fixedline);
5831 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005832 }
5833 }
5834
Joe Perches95e2c602013-07-03 15:05:20 -07005835#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005836 while ($line =~ m{($Constant|$Lval)}g) {
5837 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005838
Joe Perches95e2c602013-07-03 15:05:20 -07005839#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005840 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005841 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Łukasz Stelmach4104a202020-12-15 20:44:27 -08005842#Ignore some autogenerated defines and enum values
5843 $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005844#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005845 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Lee Jonesabea1cb2023-12-15 09:36:19 +00005846#Ignore ETHTOOL_LINK_MODE_<foo> variants
5847 $var !~ /^ETHTOOL_LINK_MODE_/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005848#Ignore SI style variants like nS, mV and dB
5849#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5850 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005851#Ignore some three character SI units explicitly, like MiB and KHz
5852 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Lee Jonesabea1cb2023-12-15 09:36:19 +00005853 while ($var =~ m{\b($Ident)}g) {
Joe Perches7e781f62013-09-11 14:23:55 -07005854 my $word = $1;
5855 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005856 if ($check) {
5857 seed_camelcase_includes();
5858 if (!$file && !$camelcase_file_seeded) {
5859 seed_camelcase_file($realfile);
5860 $camelcase_file_seeded = 1;
5861 }
5862 }
Joe Perches7e781f62013-09-11 14:23:55 -07005863 if (!defined $camelcase{$word}) {
5864 $camelcase{$word} = 1;
5865 CHK("CAMELCASE",
5866 "Avoid CamelCase: <$word>\n" . $herecurr);
5867 }
Joe Perches34456862013-07-03 15:05:34 -07005868 }
Joe Perches323c1262012-12-17 16:02:07 -08005869 }
5870 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005871
5872#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005873 if ($line =~ /\#\s*define.*\\\s+$/) {
5874 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5875 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5876 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005877 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005878 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005879 }
5880
Fabian Frederick0e212e02015-04-16 12:44:25 -07005881# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5882# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005883 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005884 my $file = "$1.h";
5885 my $checkfile = "include/linux/$file";
5886 if (-f "$root/$checkfile" &&
5887 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005888 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005889 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005890 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5891 if ($asminclude > 0) {
5892 if ($realfile =~ m{^arch/}) {
5893 CHK("ARCH_INCLUDE_LINUX",
5894 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5895 } else {
5896 WARN("INCLUDE_LINUX",
5897 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5898 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005899 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005900 }
5901 }
5902
Andy Whitcroft653d4872007-06-23 17:16:34 -07005903# multi-statement macros should be enclosed in a do while loop, grab the
5904# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005905# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005906 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5907 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005908 my $ln = $linenr;
5909 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005910 my ($off, $dstat, $dcond, $rest);
5911 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005912 my $has_flow_statement = 0;
5913 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005914 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005915 ctx_statement_block($linenr, $realcnt, 0);
5916 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005917 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005918 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005919
Joe Perches08a28432014-10-13 15:51:55 -07005920 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005921 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005922
Joe Perchesf59b64b2016-10-11 13:52:08 -07005923 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5924 my $define_args = $1;
5925 my $define_stmt = $dstat;
5926 my @def_args = ();
5927
5928 if (defined $define_args && $define_args ne "") {
5929 $define_args = substr($define_args, 1, length($define_args) - 2);
5930 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005931 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005932 @def_args = split(",", $define_args);
5933 }
5934
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005935 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005936 $dstat =~ s/\\\n.//g;
5937 $dstat =~ s/^\s*//s;
5938 $dstat =~ s/\s*$//s;
5939
5940 # Flatten any parentheses and braces
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005941 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5942 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5943 $dstat =~ s/.\[[^\[\]]*\]/1u/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005944 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005945 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005946
Antonio Borneo342d3d22020-04-06 20:11:01 -07005947 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005948 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5949 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005950 {
5951 }
5952
Joe Perches42e15292016-03-15 14:58:01 -07005953 # Make asm volatile uses seem like a generic function
5954 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5955
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005956 my $exceptions = qr{
5957 $Declare|
5958 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005959 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005960 DECLARE_PER_CPU|
5961 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005962 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005963 union|
5964 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005965 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005966 ^\"|\"$|
5967 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005968 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005969 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005970
5971 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005972 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005973 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005974
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005975 if ($dstat ne '' &&
5976 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5977 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005978 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005979 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005980 $dstat !~ /$exceptions/ &&
5981 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005982 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Lee Jonesabea1cb2023-12-15 09:36:19 +00005983 $dstat !~ /^case\b/ && # case ...
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005984 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005985 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005986 $dstat !~ /^for\s*$Constant$/ && # for (...)
5987 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5988 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005989 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005990 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005991 {
Joe Perchese7955562017-05-08 15:55:48 -07005992 if ($dstat =~ /^\s*if\b/) {
5993 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5994 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5995 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005996 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5997 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5998 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005999 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07006000 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07006001 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07006002
6003 }
Joe Perches52076492016-10-11 13:52:14 -07006004
6005 # Make $define_stmt single line, comment-free, etc
6006 my @stmt_array = split('\n', $define_stmt);
6007 my $first = 1;
6008 $define_stmt = "";
6009 foreach my $l (@stmt_array) {
6010 $l =~ s/\\$//;
6011 if ($first) {
6012 $define_stmt = $l;
6013 $first = 0;
6014 } elsif ($l =~ /^[\+ ]/) {
6015 $define_stmt .= substr($l, 1);
6016 }
6017 }
6018 $define_stmt =~ s/$;//g;
6019 $define_stmt =~ s/\s+/ /g;
6020 $define_stmt = trim($define_stmt);
6021
Joe Perchesf59b64b2016-10-11 13:52:08 -07006022# check if any macro arguments are reused (ignore '...' and 'type')
6023 foreach my $arg (@def_args) {
6024 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07006025 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07006026 my $tmp_stmt = $define_stmt;
Vincent Mailhol7b844342021-05-06 18:03:58 -07006027 $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;
Joe Perches7fe528a22017-07-10 15:52:27 -07006028 $tmp_stmt =~ s/\#+\s*$arg\b//g;
6029 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07006030 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07006031 if ($use_cnt > 1) {
6032 CHK("MACRO_ARG_REUSE",
6033 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07006034 }
6035# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07006036 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07006037 ((defined($1) && $1 ne ',') ||
6038 (defined($2) && $2 ne ','))) {
6039 CHK("MACRO_ARG_PRECEDENCE",
6040 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07006041 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006042 }
Joe Perches5023d342012-12-17 16:01:47 -08006043
Joe Perches08a28432014-10-13 15:51:55 -07006044# check for macros with flow control, but without ## concatenation
6045# ## concatenation is commonly a macro that defines a function so ignore those
6046 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07006047 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006048 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07006049
Joe Perches08a28432014-10-13 15:51:55 -07006050 WARN("MACRO_WITH_FLOW_CONTROL",
6051 "Macros with flow control statements should be avoided\n" . "$herectx");
6052 }
6053
Joe Perches481eb482012-12-17 16:01:56 -08006054# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08006055
Lee Jonesabea1cb2023-12-15 09:36:19 +00006056 } elsif ($realfile =~ m@/vmlinux.lds.h$@) {
6057 $line =~ s/(\w+)/$maybe_linker_symbol{$1}++/ge;
6058 #print "REAL: $realfile\nln: $line\nkeys:", sort keys %maybe_linker_symbol;
Joe Perches5023d342012-12-17 16:01:47 -08006059 } else {
6060 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08006061 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
6062 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08006063 $line =~ /^\+.*\\$/) {
6064 WARN("LINE_CONTINUATIONS",
6065 "Avoid unnecessary line continuations\n" . $herecurr);
6066 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006067 }
6068
Joe Perchesb13edf72012-07-30 14:41:24 -07006069# do {} while (0) macro tests:
6070# single-statement macros do not need to be enclosed in do while (0) loop,
6071# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07006072 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07006073 $realfile !~ m@/vmlinux.lds.h$@ &&
6074 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
6075 my $ln = $linenr;
6076 my $cnt = $realcnt;
6077 my ($off, $dstat, $dcond, $rest);
6078 my $ctx = '';
6079 ($dstat, $dcond, $ln, $cnt, $off) =
6080 ctx_statement_block($linenr, $realcnt, 0);
6081 $ctx = $dstat;
6082
6083 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08006084 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07006085
6086 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
6087 my $stmts = $2;
6088 my $semis = $3;
6089
6090 $ctx =~ s/\n*$//;
6091 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006092 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07006093
Joe Perchesac8e97f2012-08-21 16:15:53 -07006094 if (($stmts =~ tr/;/;/) == 1 &&
6095 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07006096 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
6097 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
6098 }
6099 if (defined $semis && $semis ne "") {
6100 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
6101 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
6102 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07006103 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
6104 $ctx =~ s/\n*$//;
6105 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006106 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07006107
6108 WARN("TRAILING_SEMICOLON",
6109 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07006110 }
6111 }
6112
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006113# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006114 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
6115 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08006116 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006117 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08006118 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
6119 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07006120 my @allowed = ();
6121 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006122 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07006123 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08006124 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006125 for my $chunk (@chunks) {
6126 my ($cond, $block) = @{$chunk};
6127
Andy Whitcroft773647a2008-03-28 14:15:58 -07006128 # If the condition carries leading newlines, then count those as offsets.
6129 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
6130 my $offset = statement_rawlines($whitespace) - 1;
6131
Joe Perchesaad4f612012-03-23 15:02:19 -07006132 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07006133 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
6134
6135 # We have looked at and allowed this specific line.
6136 $suppress_ifbraces{$ln + $offset} = 1;
6137
6138 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08006139 $ln += statement_rawlines($block) - 1;
6140
Andy Whitcroft773647a2008-03-28 14:15:58 -07006141 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006142
6143 $seen++ if ($block =~ /^\s*{/);
6144
Joe Perchesaad4f612012-03-23 15:02:19 -07006145 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08006146 if (statement_lines($cond) > 1) {
6147 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07006148 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006149 }
6150 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08006151 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07006152 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006153 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08006154 if (statement_block_size($block) > 1) {
6155 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07006156 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006157 }
Joe Perchesaad4f612012-03-23 15:02:19 -07006158 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006159 }
Joe Perchesaad4f612012-03-23 15:02:19 -07006160 if ($seen) {
6161 my $sum_allowed = 0;
6162 foreach (@allowed) {
6163 $sum_allowed += $_;
6164 }
6165 if ($sum_allowed == 0) {
6166 WARN("BRACES",
6167 "braces {} are not necessary for any arm of this statement\n" . $herectx);
6168 } elsif ($sum_allowed != $allow &&
6169 $seen != $allow) {
6170 CHK("BRACES",
6171 "braces {} should be used on all arms of this statement\n" . $herectx);
6172 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006173 }
6174 }
6175 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006176 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006177 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08006178 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006179
Andy Whitcroftcf655042008-03-04 14:28:20 -08006180 # Check the pre-context.
6181 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
6182 #print "APW: ALLOWED: pre<$1>\n";
6183 $allowed = 1;
6184 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006185
6186 my ($level, $endln, @chunks) =
6187 ctx_statement_full($linenr, $realcnt, $-[0]);
6188
Andy Whitcroftcf655042008-03-04 14:28:20 -08006189 # Check the condition.
6190 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07006191 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08006192 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07006193 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08006194 }
6195 if (statement_lines($cond) > 1) {
6196 #print "APW: ALLOWED: cond<$cond>\n";
6197 $allowed = 1;
6198 }
6199 if ($block =~/\b(?:if|for|while)\b/) {
6200 #print "APW: ALLOWED: block<$block>\n";
6201 $allowed = 1;
6202 }
6203 if (statement_block_size($block) > 1) {
6204 #print "APW: ALLOWED: lines block<$block>\n";
6205 $allowed = 1;
6206 }
6207 # Check the post-context.
6208 if (defined $chunks[1]) {
6209 my ($cond, $block) = @{$chunks[1]};
6210 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07006211 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006212 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08006213 if ($block =~ /^\s*\{/) {
6214 #print "APW: ALLOWED: chunk-1 block<$block>\n";
6215 $allowed = 1;
6216 }
6217 }
6218 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07006219 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006220 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08006221
Joe Perches000d1cc12011-07-25 17:13:25 -07006222 WARN("BRACES",
6223 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006224 }
6225 }
6226
Joe Perchese4c5bab2017-02-24 15:01:41 -08006227# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08006228 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
6229 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08006230 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
6231 }
6232
Joe Perches0979ae62012-12-17 16:01:59 -08006233# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07006234 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08006235 if (CHK("BRACES",
6236 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
6237 $fix && $prevrawline =~ /^\+/) {
6238 fix_delete_line($fixlinenr - 1, $prevrawline);
6239 }
Joe Perches0979ae62012-12-17 16:01:59 -08006240 }
Joe Perches77b9a532013-07-03 15:05:29 -07006241 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08006242 if (CHK("BRACES",
6243 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
6244 $fix) {
6245 fix_delete_line($fixlinenr, $rawline);
6246 }
Joe Perches0979ae62012-12-17 16:01:59 -08006247 }
6248
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006249# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07006250 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
6251 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006252 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006253 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006254 }
6255
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006256# Check for user-visible strings broken across lines, which breaks the ability
6257# to grep for the string. Make exceptions when the previous string ends in a
6258# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
6259# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07006260 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006261 $prevline =~ /"\s*$/ &&
6262 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
6263 if (WARN("SPLIT_STRING",
6264 "quoted string split across lines\n" . $hereprev) &&
6265 $fix &&
6266 $prevrawline =~ /^\+.*"\s*$/ &&
6267 $last_coalesced_string_linenr != $linenr - 1) {
6268 my $extracted_string = get_quoted_string($line, $rawline);
6269 my $comma_close = "";
6270 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
6271 $comma_close = $1;
6272 }
6273
6274 fix_delete_line($fixlinenr - 1, $prevrawline);
6275 fix_delete_line($fixlinenr, $rawline);
6276 my $fixedline = $prevrawline;
6277 $fixedline =~ s/"\s*$//;
6278 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
6279 fix_insert_line($fixlinenr - 1, $fixedline);
6280 $fixedline = $rawline;
6281 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
6282 if ($fixedline !~ /\+\s*$/) {
6283 fix_insert_line($fixlinenr, $fixedline);
6284 }
6285 $last_coalesced_string_linenr = $linenr;
6286 }
6287 }
6288
6289# check for missing a space in a string concatenation
6290 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
6291 WARN('MISSING_SPACE',
6292 "break quoted strings at a space character\n" . $hereprev);
6293 }
6294
Joe Perchese4b7d302017-05-08 15:55:51 -07006295# check for an embedded function name in a string when the function is known
6296# This does not work very well for -f --file checking as it depends on patch
6297# context providing the function name or a single line form for in-file
6298# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08006299 if ($line =~ /^\+.*$String/ &&
6300 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07006301 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
6302 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08006303 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07006304 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08006305 }
6306
Joe Perchesadb2da82021-02-25 17:21:50 -08006307# check for unnecessary function tracing like uses
6308# This does not use $logFunctions because there are many instances like
6309# 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions
6310 if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) {
6311 if (WARN("TRACING_LOGGING",
6312 "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) &&
6313 $fix) {
6314 fix_delete_line($fixlinenr, $rawline);
6315 }
6316 }
6317
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006318# check for spaces before a quoted newline
6319 if ($rawline =~ /^.*\".*\s\\n/) {
6320 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
6321 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
6322 $fix) {
6323 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
6324 }
6325
6326 }
6327
Joe Perchesf17dba42014-10-13 15:51:51 -07006328# concatenated string without spaces between elements
Joe Perchesd2af5aa2021-09-07 19:59:51 -07006329 if ($line =~ /$String[A-Z_]/ ||
6330 ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) {
Joe Perches79682c02018-08-21 21:57:29 -07006331 if (CHK("CONCATENATED_STRING",
6332 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
6333 $fix) {
6334 while ($line =~ /($String)/g) {
6335 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6336 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
6337 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
6338 }
6339 }
Joe Perchesf17dba42014-10-13 15:51:51 -07006340 }
6341
Joe Perches90ad30e2014-12-10 15:51:59 -08006342# uncoalesced string fragments
Joe Perchesd2af5aa2021-09-07 19:59:51 -07006343 if ($line =~ /$String\s*[Lu]?"/) {
Joe Perches79682c02018-08-21 21:57:29 -07006344 if (WARN("STRING_FRAGMENTS",
6345 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
6346 $fix) {
6347 while ($line =~ /($String)(?=\s*")/g) {
6348 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6349 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
6350 }
6351 }
Joe Perches90ad30e2014-12-10 15:51:59 -08006352 }
6353
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006354# check for non-standard and hex prefixed decimal printf formats
6355 my $show_L = 1; #don't show the same defect twice
6356 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006357 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006358 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006359 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006360 # check for %L
6361 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006362 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006363 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6364 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006365 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006366 # check for %Z
6367 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6368 WARN("PRINTF_Z",
6369 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6370 $show_Z = 0;
6371 }
6372 # check for 0x<decimal>
6373 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6374 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07006375 "Prefixing 0x with decimal output is defective\n" . $herecurr);
6376 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006377 }
6378
6379# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08006380 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006381 WARN("LINE_CONTINUATIONS",
6382 "Avoid line continuations in quoted strings\n" . $herecurr);
6383 }
6384
Andy Whitcroft00df3442007-06-08 13:47:06 -07006385# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006386 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07006387 WARN("IF_0",
6388 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
6389 }
6390
6391# warn about #if 1
6392 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
6393 WARN("IF_1",
6394 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006395 }
6396
Andy Whitcroft03df4b52012-12-17 16:01:52 -08006397# check for needless "if (<foo>) fn(<foo>)" uses
6398 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07006399 my $tested = quotemeta($1);
6400 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6401 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6402 my $func = $1;
6403 if (WARN('NEEDLESS_IF',
6404 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6405 $fix) {
6406 my $do_fix = 1;
6407 my $leading_tabs = "";
6408 my $new_leading_tabs = "";
6409 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6410 $leading_tabs = $1;
6411 } else {
6412 $do_fix = 0;
6413 }
6414 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6415 $new_leading_tabs = $1;
6416 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6417 $do_fix = 0;
6418 }
6419 } else {
6420 $do_fix = 0;
6421 }
6422 if ($do_fix) {
6423 fix_delete_line($fixlinenr - 1, $prevrawline);
6424 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6425 }
6426 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07006427 }
6428 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006429
Joe Perchesebfdc402014-08-06 16:10:27 -07006430# check for unnecessary "Out of Memory" messages
6431 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6432 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6433 (defined $1 || defined $3) &&
6434 $linenr > 3) {
6435 my $testval = $2;
6436 my $testline = $lines[$linenr - 3];
6437
6438 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6439# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6440
Joe Perchese29a70f2019-03-07 16:28:35 -08006441 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6442 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07006443 WARN("OOM_MESSAGE",
6444 "Possible unnecessary 'out of memory' message\n" . $hereprev);
6445 }
6446 }
6447
Joe Perchesf78d98f2014-10-13 15:52:01 -07006448# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08006449 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07006450 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6451 my $level = $1;
6452 if (WARN("UNNECESSARY_KERN_LEVEL",
6453 "Possible unnecessary $level\n" . $herecurr) &&
6454 $fix) {
6455 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
6456 }
6457 }
6458
Joe Perches45c55e92017-02-24 15:01:31 -08006459# check for logging continuations
6460 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
6461 WARN("LOGGING_CONTINUATION",
6462 "Avoid logging continuation uses where feasible\n" . $herecurr);
6463 }
6464
Dwaipayan Ray70eb2272020-12-15 20:45:15 -08006465# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
6466 if (defined $stat &&
6467 $line =~ /\b$logFunctions\s*\(/ &&
6468 index($stat, '"') >= 0) {
6469 my $lc = $stat =~ tr@\n@@;
6470 $lc = $lc + $linenr;
6471 my $stat_real = get_stat_real($linenr, $lc);
6472 pos($stat_real) = index($stat_real, '"');
6473 while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
6474 my $pspec = $1;
6475 my $h = $2;
6476 my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
6477 if (WARN("UNNECESSARY_MODIFIER",
6478 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
6479 $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
6480 my $nspec = $pspec;
6481 $nspec =~ s/h//g;
6482 $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
6483 }
6484 }
6485 }
6486
Joe Perchesabb08a52014-12-10 15:51:46 -08006487# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07006488 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08006489 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6490 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6491 WARN("MASK_THEN_SHIFT",
6492 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6493 }
6494
Joe Perchesb75ac612014-12-10 15:52:02 -08006495# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07006496 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08006497 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6498 my $val = $1;
6499 my $equal = "!";
6500 $equal = "" if ($4 eq "!=");
6501 if (CHK("COMPARISON_TO_NULL",
6502 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6503 $fix) {
6504 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6505 }
6506 }
6507 }
6508
Joe Perches8716de32013-09-11 14:24:05 -07006509# check for bad placement of section $InitAttribute (e.g.: __initdata)
6510 if ($line =~ /(\b$InitAttribute\b)/) {
6511 my $attr = $1;
6512 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
6513 my $ptr = $1;
6514 my $var = $2;
6515 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
6516 ERROR("MISPLACED_INIT",
6517 "$attr should be placed after $var\n" . $herecurr)) ||
6518 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
6519 WARN("MISPLACED_INIT",
6520 "$attr should be placed after $var\n" . $herecurr))) &&
6521 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006522 $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;
Joe Perches8716de32013-09-11 14:24:05 -07006523 }
6524 }
6525 }
6526
Joe Perchese970b8842013-11-12 15:10:10 -08006527# check for $InitAttributeData (ie: __initdata) with const
6528 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6529 my $attr = $1;
6530 $attr =~ /($InitAttributePrefix)(.*)/;
6531 my $attr_prefix = $1;
6532 my $attr_type = $2;
6533 if (ERROR("INIT_ATTRIBUTE",
6534 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6535 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006536 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006537 s/$InitAttributeData/${attr_prefix}initconst/;
6538 }
6539 }
6540
6541# check for $InitAttributeConst (ie: __initconst) without const
6542 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6543 my $attr = $1;
6544 if (ERROR("INIT_ATTRIBUTE",
6545 "Use of $attr requires a separate use of const\n" . $herecurr) &&
6546 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006547 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006548 /(^\+\s*(?:static\s+))/;
6549 $lead = rtrim($1);
6550 $lead = "$lead " if ($lead !~ /^\+$/);
6551 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07006552 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08006553 }
6554 }
6555
Joe Perchesc17893c2015-04-16 12:44:42 -07006556# check for __read_mostly with const non-pointer (should just be const)
6557 if ($line =~ /\b__read_mostly\b/ &&
6558 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6559 if (ERROR("CONST_READ_MOSTLY",
6560 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6561 $fix) {
6562 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6563 }
6564 }
6565
Joe Perchesfbdb8132014-04-03 14:49:14 -07006566# don't use __constant_<foo> functions outside of include/uapi/
6567 if ($realfile !~ m@^include/uapi/@ &&
6568 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6569 my $constant_func = $1;
6570 my $func = $constant_func;
6571 $func =~ s/^__constant_//;
6572 if (WARN("CONSTANT_CONVERSION",
6573 "$constant_func should be $func\n" . $herecurr) &&
6574 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006575 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07006576 }
6577 }
6578
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006579# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08006580 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07006581 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006582 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07006583 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006584 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006585 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07006586 }
6587 if ($delay > 2000) {
6588 WARN("LONG_UDELAY",
6589 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006590 }
6591 }
6592
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006593# warn about unexpectedly long msleep's
6594 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6595 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006596 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006597 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006598 }
6599 }
6600
Joe Perches36ec1932013-07-03 15:05:25 -07006601# check for comparisons of jiffies
6602 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6603 WARN("JIFFIES_COMPARISON",
6604 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6605 }
6606
Joe Perches9d7a34a2013-07-03 15:05:26 -07006607# check for comparisons of get_jiffies_64()
6608 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6609 WARN("JIFFIES_COMPARISON",
6610 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6611 }
6612
Andy Whitcroft00df3442007-06-08 13:47:06 -07006613# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006614# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07006615# print "#ifdef in C files should be avoided\n";
6616# print "$herecurr";
6617# $clean = 0;
6618# }
6619
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006620# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006621 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07006622 if (ERROR("SPACING",
6623 "exactly one space required after that #$1\n" . $herecurr) &&
6624 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006625 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07006626 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6627 }
6628
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006629 }
6630
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006631# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006632 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6633 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006634 my $which = $1;
6635 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006636 CHK("UNCOMMENTED_DEFINITION",
6637 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006638 }
6639 }
6640# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006641
6642 my $barriers = qr{
6643 mb|
6644 rmb|
Will Deaconad83ec62019-11-07 14:49:00 +00006645 wmb
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006646 }x;
6647 my $barrier_stems = qr{
6648 mb__before_atomic|
6649 mb__after_atomic|
6650 store_release|
6651 load_acquire|
6652 store_mb|
6653 (?:$barriers)
6654 }x;
6655 my $all_barriers = qr{
6656 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02006657 smp_(?:$barrier_stems)|
6658 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006659 }x;
6660
6661 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006662 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08006663 WARN("MEMORY_BARRIER",
6664 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006665 }
6666 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006667
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02006668 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6669
6670 if ($realfile !~ m@^include/asm-generic/@ &&
6671 $realfile !~ m@/barrier\.h$@ &&
6672 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6673 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6674 WARN("MEMORY_BARRIER",
6675 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6676 }
6677
Joe Perchescb426e92015-06-25 15:02:46 -07006678# check for waitqueue_active without a comment.
6679 if ($line =~ /\bwaitqueue_active\s*\(/) {
6680 if (!ctx_has_comment($first_line, $linenr)) {
6681 WARN("WAITQUEUE_ACTIVE",
6682 "waitqueue_active without comment\n" . $herecurr);
6683 }
6684 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006685
Marco Elver5099a722020-04-01 12:17:14 +02006686# check for data_race without a comment.
6687 if ($line =~ /\bdata_race\s*\(/) {
6688 if (!ctx_has_comment($first_line, $linenr)) {
6689 WARN("DATA_RACE",
6690 "data_race without comment\n" . $herecurr);
6691 }
6692 }
6693
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006694# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006695 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006696 CHK("ARCH_DEFINES",
6697 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006698 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006699
Joe Perches596ed452017-07-12 14:37:02 -07006700# check that the storage class is not after a type
6701 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006702 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07006703 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6704 }
6705# Check that the storage class is at the beginning of a declaration
6706 if ($line =~ /\b$Storage\b/ &&
6707 $line !~ /^.\s*$Storage/ &&
6708 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6709 $1 !~ /[\,\)]\s*$/) {
6710 WARN("STORAGE_CLASS",
6711 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07006712 }
6713
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006714# check the location of the inline attribute, that it is between
6715# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006716 if ($line =~ /\b$Type\s+$Inline\b/ ||
6717 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006718 ERROR("INLINE_LOCATION",
6719 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006720 }
6721
Andy Whitcroft8905a672007-11-28 16:21:06 -08006722# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08006723 if ($realfile !~ m@\binclude/uapi/@ &&
6724 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006725 if (WARN("INLINE",
6726 "plain inline is preferred over $1\n" . $herecurr) &&
6727 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006728 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006729
6730 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006731 }
6732
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006733# Check for compiler attributes
Joe Perches2b7ab452013-11-12 15:10:14 -08006734 if ($realfile !~ m@\binclude/uapi/@ &&
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006735 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6736 my $attr = $1;
6737 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
Joe Perches3d130fd2011-01-12 17:00:00 -08006738
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006739 my %attr_list = (
Joe Perches0830aab2020-12-15 20:44:50 -08006740 "alias" => "__alias",
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006741 "aligned" => "__aligned",
6742 "always_inline" => "__always_inline",
6743 "assume_aligned" => "__assume_aligned",
6744 "cold" => "__cold",
6745 "const" => "__attribute_const__",
6746 "copy" => "__copy",
6747 "designated_init" => "__designated_init",
6748 "externally_visible" => "__visible",
6749 "format" => "printf|scanf",
6750 "gnu_inline" => "__gnu_inline",
6751 "malloc" => "__malloc",
6752 "mode" => "__mode",
6753 "no_caller_saved_registers" => "__no_caller_saved_registers",
6754 "noclone" => "__noclone",
6755 "noinline" => "noinline",
6756 "nonstring" => "__nonstring",
6757 "noreturn" => "__noreturn",
6758 "packed" => "__packed",
6759 "pure" => "__pure",
Joe Perches339f29d2020-12-15 20:44:43 -08006760 "section" => "__section",
Joe Perches0830aab2020-12-15 20:44:50 -08006761 "used" => "__used",
6762 "weak" => "__weak"
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006763 );
Joe Perches39b7e282011-07-25 17:13:24 -07006764
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006765 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
Joe Perches339f29d2020-12-15 20:44:43 -08006766 my $orig_attr = $1;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006767 my $params = '';
6768 $params = $2 if defined($2);
Joe Perches339f29d2020-12-15 20:44:43 -08006769 my $curr_attr = $orig_attr;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006770 $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006771 if (exists($attr_list{$curr_attr})) {
Joe Perches339f29d2020-12-15 20:44:43 -08006772 my $new = $attr_list{$curr_attr};
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006773 if ($curr_attr eq "format" && $params) {
6774 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
Joe Perches339f29d2020-12-15 20:44:43 -08006775 $new = "__$1\($2";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006776 } else {
Joe Perches339f29d2020-12-15 20:44:43 -08006777 $new = "$new$params";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006778 }
Joe Perches339f29d2020-12-15 20:44:43 -08006779 if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6780 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
6781 $fix) {
6782 my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6783 $fixed[$fixlinenr] =~ s/$remove//;
6784 $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6785 $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6786 $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
6787 }
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006788 }
6789 }
6790
6791 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6792 if ($attr =~ /^_*unused/) {
6793 WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6794 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
Joe Perchesd5e616f2013-09-11 14:23:54 -07006795 }
Joe Perches6061d942012-03-23 15:02:16 -07006796 }
6797
Joe Perches619a9082014-12-10 15:51:35 -08006798# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006799 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006800 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6801 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6802 $line =~ /\b__weak\b/)) {
6803 ERROR("WEAK_DECLARATION",
6804 "Using weak declarations can have unintended link defects\n" . $herecurr);
6805 }
6806
Tomas Winklerfd39f902016-12-12 16:46:34 -08006807# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006808 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006809 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006810 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6811 my $type = $1;
6812 if ($type =~ /\b($typeC99Typedefs)\b/) {
6813 $type = $1;
6814 my $kernel_type = 'u';
6815 $kernel_type = 's' if ($type =~ /^_*[si]/);
6816 $type =~ /(\d+)/;
6817 $kernel_type .= $1;
6818 if (CHK("PREFER_KERNEL_TYPES",
6819 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6820 $fix) {
6821 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6822 }
6823 }
6824 }
6825
Joe Perches938224b2016-01-20 14:59:15 -08006826# check for cast of C90 native int or longer types constants
6827 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6828 my $cast = $1;
6829 my $const = $2;
Joe Perches0972b8b2021-02-25 17:21:54 -08006830 my $suffix = "";
6831 my $newconst = $const;
6832 $newconst =~ s/${Int_type}$//;
6833 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6834 if ($cast =~ /\blong\s+long\b/) {
6835 $suffix .= 'LL';
6836 } elsif ($cast =~ /\blong\b/) {
6837 $suffix .= 'L';
6838 }
Joe Perches938224b2016-01-20 14:59:15 -08006839 if (WARN("TYPECAST_INT_CONSTANT",
Joe Perches0972b8b2021-02-25 17:21:54 -08006840 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
Joe Perches938224b2016-01-20 14:59:15 -08006841 $fix) {
Joe Perches938224b2016-01-20 14:59:15 -08006842 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6843 }
6844 }
6845
Joe Perches8f53a9b2010-03-05 13:43:48 -08006846# check for sizeof(&)
6847 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006848 WARN("SIZEOF_ADDRESS",
6849 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006850 }
6851
Joe Perches66c80b62012-07-30 14:41:22 -07006852# check for sizeof without parenthesis
6853 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006854 if (WARN("SIZEOF_PARENTHESIS",
6855 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6856 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006857 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006858 }
Joe Perches66c80b62012-07-30 14:41:22 -07006859 }
6860
Joe Perches88982fe2012-12-17 16:02:00 -08006861# check for struct spinlock declarations
6862 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6863 WARN("USE_SPINLOCK_T",
6864 "struct spinlock should be spinlock_t\n" . $herecurr);
6865 }
6866
Joe Perchesa6962d72013-04-29 16:18:13 -07006867# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006868 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006869 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006870 $fmt =~ s/%%//g;
6871 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006872 if (WARN("PREFER_SEQ_PUTS",
6873 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6874 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006875 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006876 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006877 }
6878 }
6879
Joe Perches478b1792018-04-10 16:33:34 -07006880# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006881 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006882 defined $stat &&
6883 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6884 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006885 my $stat_real;
6886
Joe Perches0b523762017-05-08 15:55:36 -07006887 my $lc = $stat =~ tr@\n@@;
6888 $lc = $lc + $linenr;
6889 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006890 my $specifier;
6891 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006892 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006893 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006894 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6895 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006896
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006897 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006898 $specifier = $1;
6899 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006900 $qualifier = $3;
Sakari Ailusaf612e42021-02-16 17:57:20 +02006901 if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006902 ($extension eq "f" &&
Sakari Ailusaf612e42021-02-16 17:57:20 +02006903 defined $qualifier && $qualifier !~ /^w/) ||
6904 ($extension eq "4" &&
6905 defined $qualifier && $qualifier !~ /^cc/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006906 $bad_specifier = $specifier;
6907 last;
6908 }
6909 if ($extension eq "x" && !defined($stat_real)) {
6910 if (!defined($stat_real)) {
6911 $stat_real = get_stat_real($linenr, $lc);
6912 }
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08006913 # WARN("VSPRINTF_SPECIFIER_PX",
6914 # "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");
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006915 }
6916 }
6917 if ($bad_specifier ne "") {
6918 my $stat_real = get_stat_real($linenr, $lc);
Lee Jonesabea1cb2023-12-15 09:36:19 +00006919 my $msg_level = \&WARN;
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006920 my $ext_type = "Invalid";
6921 my $use = "";
6922 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006923 $use = " - use %pS instead";
6924 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
Lee Jonesabea1cb2023-12-15 09:36:19 +00006925 } elsif ($bad_specifier =~ /pA/) {
6926 $use = " - '%pA' is only intended to be used from Rust code";
6927 $msg_level = \&ERROR;
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006928 }
6929
Lee Jonesabea1cb2023-12-15 09:36:19 +00006930 &{$msg_level}("VSPRINTF_POINTER_EXTENSION",
6931 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006932 }
Joe Perches0b523762017-05-08 15:55:36 -07006933 }
6934 }
6935
Andy Whitcroft554e1652012-01-10 15:09:57 -08006936# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006937 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006938 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006939 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006940
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006941 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006942 my $ms_val = $7;
6943 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006944
Andy Whitcroft554e1652012-01-10 15:09:57 -08006945 if ($ms_size =~ /^(0x|)0$/i) {
6946 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006947 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006948 } elsif ($ms_size =~ /^(0x|)1$/i) {
6949 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006950 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6951 }
6952 }
6953
Joe Perches98a9bba2014-01-23 15:54:52 -08006954# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006955# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006956# defined $stat &&
6957# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6958# if (WARN("PREFER_ETHER_ADDR_COPY",
6959# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6960# $fix) {
6961# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6962# }
6963# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006964
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006965# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006966# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006967# defined $stat &&
6968# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6969# WARN("PREFER_ETHER_ADDR_EQUAL",
6970# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6971# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006972
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006973# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6974# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006975# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006976# defined $stat &&
6977# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6978#
6979# my $ms_val = $7;
6980#
6981# if ($ms_val =~ /^(?:0x|)0+$/i) {
6982# if (WARN("PREFER_ETH_ZERO_ADDR",
6983# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6984# $fix) {
6985# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6986# }
6987# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6988# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6989# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6990# $fix) {
6991# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6992# }
6993# }
6994# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006995
Lee Jonesabea1cb2023-12-15 09:36:19 +00006996# strcpy uses that should likely be strscpy
6997 if ($line =~ /\bstrcpy\s*\(/) {
6998 WARN("STRCPY",
6999 "Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88\n" . $herecurr);
7000 }
7001
Joe Perches5dbdb2d2020-12-29 15:14:31 -08007002# strlcpy uses that should likely be strscpy
7003 if ($line =~ /\bstrlcpy\s*\(/) {
7004 WARN("STRLCPY",
Lee Jonesabea1cb2023-12-15 09:36:19 +00007005 "Prefer strscpy over strlcpy - see: https://github.com/KSPP/linux/issues/89\n" . $herecurr);
7006 }
7007
7008# strncpy uses that should likely be strscpy or strscpy_pad
7009 if ($line =~ /\bstrncpy\s*\(/) {
7010 WARN("STRNCPY",
7011 "Prefer strscpy, strscpy_pad, or __nonstring over strncpy - see: https://github.com/KSPP/linux/issues/90\n" . $herecurr);
Joe Perches5dbdb2d2020-12-29 15:14:31 -08007012 }
7013
Joe Perchesd7c76ba2012-01-10 15:09:58 -08007014# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07007015 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07007016 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08007017 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07007018 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08007019 my $call = $1;
7020 my $cast1 = deparenthesize($2);
7021 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07007022 my $cast2 = deparenthesize($7);
7023 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08007024 my $cast;
7025
Joe Perchesd1fe9c02012-03-23 15:02:16 -07007026 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08007027 $cast = "$cast1 or $cast2";
7028 } elsif ($cast1 ne "") {
7029 $cast = $cast1;
7030 } else {
7031 $cast = $cast2;
7032 }
7033 WARN("MINMAX",
7034 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08007035 }
7036 }
7037
Joe Perches4a273192012-07-30 14:41:20 -07007038# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07007039 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07007040 defined $stat &&
7041 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
7042 my $min = $1;
7043 my $max = $7;
7044 if ($min eq $max) {
7045 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03007046 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Joe Perches4a273192012-07-30 14:41:20 -07007047 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
7048 $min > $max) {
7049 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03007050 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Joe Perches4a273192012-07-30 14:41:20 -07007051 }
7052 }
7053
Joe Perches823b7942013-11-12 15:10:15 -08007054# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07007055 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08007056 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07007057 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08007058 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
7059 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
7060 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
7061 my $lc = $stat =~ tr@\n@@;
7062 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07007063 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08007064 WARN("NAKED_SSCANF",
7065 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
7066 }
7067
Joe Perchesafc819a2014-06-04 16:12:08 -07007068# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07007069 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07007070 defined $stat &&
7071 $line =~ /\bsscanf\b/) {
7072 my $lc = $stat =~ tr@\n@@;
7073 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07007074 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07007075 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
7076 my $format = $6;
7077 my $count = $format =~ tr@%@%@;
7078 if ($count == 1 &&
7079 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
7080 WARN("SSCANF_TO_KSTRTO",
7081 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
7082 }
7083 }
7084 }
7085
Joe Perches70dc8a42013-09-11 14:23:58 -07007086# check for new externs in .h files.
7087 if ($realfile =~ /\.h$/ &&
7088 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07007089 if (CHK("AVOID_EXTERNS",
7090 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07007091 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07007092 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07007093 }
7094 }
7095
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07007096# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07007097 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07007098 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07007099 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07007100 my $function_name = $1;
7101 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07007102
7103 my $s = $stat;
7104 if (defined $cond) {
7105 substr($s, 0, length($cond), '');
7106 }
Kees Cookd8b44b52020-06-03 14:18:09 -07007107 if ($s =~ /^\s*;/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07007108 {
Joe Perches000d1cc12011-07-25 17:13:25 -07007109 WARN("AVOID_EXTERNS",
7110 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07007111 }
7112
7113 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007114 WARN("FUNCTION_ARGUMENTS",
7115 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07007116 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07007117
7118 } elsif ($realfile =~ /\.c$/ && defined $stat &&
Lee Jonesabea1cb2023-12-15 09:36:19 +00007119 $stat =~ /^\+extern struct\s+(\w+)\s+(\w+)\[\];/)
7120 {
7121 my ($st_type, $st_name) = ($1, $2);
7122
7123 for my $s (keys %maybe_linker_symbol) {
7124 #print "Linker symbol? $st_name : $s\n";
7125 goto LIKELY_LINKER_SYMBOL
7126 if $st_name =~ /$s/;
7127 }
7128 WARN("AVOID_EXTERNS",
7129 "found a file-scoped extern type:$st_type name:$st_name in .c file\n"
7130 . "is this a linker symbol ?\n" . $herecurr);
7131 LIKELY_LINKER_SYMBOL:
7132
7133 } elsif ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07007134 $stat =~ /^.\s*extern\s+/)
7135 {
Joe Perches000d1cc12011-07-25 17:13:25 -07007136 WARN("AVOID_EXTERNS",
7137 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07007138 }
7139
Joe Perchesa0ad7592017-07-10 15:52:19 -07007140# check for function declarations that have arguments without identifier names
7141 if (defined $stat &&
Kees Cookd8b44b52020-06-03 14:18:09 -07007142 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
7143 $1 ne "void") {
7144 my $args = trim($1);
Joe Perchesca0d8922016-10-11 13:52:16 -07007145 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
7146 my $arg = trim($1);
Kees Cookd8b44b52020-06-03 14:18:09 -07007147 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07007148 WARN("FUNCTION_ARGUMENTS",
7149 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
7150 }
7151 }
7152 }
7153
Joe Perchesa0ad7592017-07-10 15:52:19 -07007154# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07007155 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07007156 defined $stat &&
7157 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
7158 $context_function = $1;
7159
7160# check for multiline function definition with misplaced open brace
7161 my $ok = 0;
7162 my $cnt = statement_rawlines($stat);
7163 my $herectx = $here . "\n";
7164 for (my $n = 0; $n < $cnt; $n++) {
7165 my $rl = raw_line($linenr, $n);
7166 $herectx .= $rl . "\n";
7167 $ok = 1 if ($rl =~ /^[ \+]\{/);
7168 $ok = 1 if ($rl =~ /\{/ && $n == 0);
7169 last if $rl =~ /^[ \+].*\{/;
7170 }
7171 if (!$ok) {
7172 ERROR("OPEN_BRACE",
7173 "open brace '{' following function definitions go on the next line\n" . $herectx);
7174 }
7175 }
7176
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07007177# checks for new __setup's
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08007178 # if ($rawline =~ /\b__setup\("([^"]*)"/) {
7179 # my $name = $1;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07007180
Wanwei Jiang3bdc8022023-02-02 15:49:10 +08007181 # if (!grep(/$name/, @setup_docs)) {
7182 # CHK("UNDOCUMENTED_SETUP",
7183 # "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
7184 # }
7185 # }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07007186
Joe Perchese29a70f2019-03-07 16:28:35 -08007187# check for pointless casting of alloc functions
7188 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007189 WARN("UNNECESSARY_CASTS",
7190 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07007191 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007192
Joe Perchesa640d252013-07-03 15:05:21 -07007193# alloc style
7194# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07007195 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08007196 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
Joe Perchesa640d252013-07-03 15:05:21 -07007197 CHK("ALLOC_SIZEOF_STRUCT",
7198 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
7199 }
7200
Lee Jonesabea1cb2023-12-15 09:36:19 +00007201# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07007202 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07007203 defined $stat &&
Lee Jonesabea1cb2023-12-15 09:36:19 +00007204 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Joe Perches60a55362014-06-04 16:12:07 -07007205 my $oldfunc = $3;
7206 my $a1 = $4;
7207 my $a2 = $10;
7208 my $newfunc = "kmalloc_array";
Lee Jonesabea1cb2023-12-15 09:36:19 +00007209 $newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc");
7210 $newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc");
Joe Perches60a55362014-06-04 16:12:07 -07007211 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07007212 my $r1 = $a1;
7213 my $r2 = $a2;
7214 if ($a1 =~ /^sizeof\s*\S/) {
7215 $r1 = $a2;
7216 $r2 = $a1;
7217 }
7218 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
7219 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07007220 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07007221 my $herectx = get_stat_here($linenr, $cnt, $here);
7222
Joe Perches60a55362014-06-04 16:12:07 -07007223 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07007224 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
7225 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07007226 $fix) {
Lee Jonesabea1cb2023-12-15 09:36:19 +00007227 $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;
Joe Perches60a55362014-06-04 16:12:07 -07007228 }
7229 }
7230 }
7231
Joe Perches972fdea2013-04-29 16:18:12 -07007232# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07007233 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07007234 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
7235 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07007236 WARN("KREALLOC_ARG_REUSE",
7237 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
7238 }
7239
Joe Perches5ce59ae2013-02-21 16:44:18 -08007240# check for alloc argument mismatch
Lee Jonesabea1cb2023-12-15 09:36:19 +00007241 if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
Joe Perches5ce59ae2013-02-21 16:44:18 -08007242 WARN("ALLOC_ARRAY_ARGS",
7243 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
7244 }
7245
Joe Perchescaf2a542011-01-12 16:59:56 -08007246# check for multiple semicolons
7247 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07007248 if (WARN("ONE_SEMICOLON",
7249 "Statements terminations use 1 semicolon\n" . $herecurr) &&
7250 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07007251 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07007252 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007253 }
7254
Tomas Winklercec3aaa2016-08-02 14:04:39 -07007255# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
7256 if ($realfile !~ m@^include/uapi/@ &&
7257 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08007258 my $ull = "";
7259 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
7260 if (CHK("BIT_MACRO",
7261 "Prefer using the BIT$ull macro\n" . $herecurr) &&
7262 $fix) {
7263 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
7264 }
7265 }
7266
Joe Perches50161262020-08-11 18:35:07 -07007267# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
Jerome Forissier3e89ad82020-10-15 20:11:49 -07007268 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
Joe Perches50161262020-08-11 18:35:07 -07007269 WARN("IS_ENABLED_CONFIG",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07007270 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
Joe Perches50161262020-08-11 18:35:07 -07007271 }
7272
Joe Perches2d632742016-05-20 17:04:00 -07007273# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
Jerome Forissier3e89ad82020-10-15 20:11:49 -07007274 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
Joe Perches2d632742016-05-20 17:04:00 -07007275 my $config = $1;
7276 if (WARN("PREFER_IS_ENABLED",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07007277 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
Joe Perches2d632742016-05-20 17:04:00 -07007278 $fix) {
7279 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
7280 }
7281 }
7282
Joe Perchesf36d3eb2020-04-06 20:10:58 -07007283# check for /* fallthrough */ like comment, prefer fallthrough;
7284 my @fallthroughs = (
7285 'fallthrough',
7286 '@fallthrough@',
7287 'lint -fallthrough[ \t]*',
7288 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
7289 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
7290 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7291 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7292 );
7293 if ($raw_comment ne '') {
7294 foreach my $ft (@fallthroughs) {
7295 if ($raw_comment =~ /$ft/) {
7296 my $msg_level = \&WARN;
7297 $msg_level = \&CHK if ($file);
7298 &{$msg_level}("PREFER_FALLTHROUGH",
7299 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
7300 last;
7301 }
7302 }
7303 }
7304
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007305# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07007306 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007307 defined $stat &&
7308 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007309 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07007310 my $herectx = get_stat_here($linenr, $cnt, $here);
7311
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007312 WARN("DEFAULT_NO_BREAK",
7313 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08007314 }
7315
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007316# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07007317 if ($line =~ /\b__FUNCTION__\b/) {
7318 if (WARN("USE_FUNC",
7319 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
7320 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07007321 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07007322 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007323 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07007324
Joe Perches62ec8182015-02-13 14:38:18 -08007325# check for uses of __DATE__, __TIME__, __TIMESTAMP__
7326 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
7327 ERROR("DATE_TIME",
7328 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
7329 }
7330
Joe Perches2c924882012-03-23 15:02:20 -07007331# check for use of yield()
7332 if ($line =~ /\byield\s*\(\s*\)/) {
7333 WARN("YIELD",
7334 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
7335 }
7336
Joe Perches179f8f42013-07-03 15:05:30 -07007337# check for comparisons against true and false
7338 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
7339 my $lead = $1;
7340 my $arg = $2;
7341 my $test = $3;
7342 my $otype = $4;
7343 my $trail = $5;
7344 my $op = "!";
7345
7346 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
7347
7348 my $type = lc($otype);
7349 if ($type =~ /^(?:true|false)$/) {
7350 if (("$test" eq "==" && "$type" eq "true") ||
7351 ("$test" eq "!=" && "$type" eq "false")) {
7352 $op = "";
7353 }
7354
7355 CHK("BOOL_COMPARISON",
7356 "Using comparison to $otype is error prone\n" . $herecurr);
7357
7358## maybe suggesting a correct construct would better
7359## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
7360
7361 }
7362 }
7363
Thomas Gleixner4882720b2010-09-07 14:34:01 +00007364# check for semaphores initialized locked
7365 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007366 WARN("CONSIDER_COMPLETION",
7367 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07007368 }
Joe Perches6712d852012-03-23 15:02:20 -07007369
Joe Perches67d0a072011-10-31 17:13:10 -07007370# recommend kstrto* over simple_strto* and strict_strto*
7371 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007372 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07007373 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07007374 }
Joe Perches6712d852012-03-23 15:02:20 -07007375
Fabian Frederickae3ccc42014-06-04 16:12:10 -07007376# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07007377 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007378 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07007379 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07007380 }
Joe Perches6712d852012-03-23 15:02:20 -07007381
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08007382# check for spin_is_locked(), suggest lockdep instead
7383 if ($line =~ /\bspin_is_locked\(/) {
7384 WARN("USE_LOCKDEP",
7385 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
7386 }
7387
Joe Perches9189c7e2018-09-07 15:26:18 -07007388# check for deprecated apis
7389 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
7390 my $deprecated_api = $1;
7391 my $new_api = $deprecated_apis{$deprecated_api};
7392 WARN("DEPRECATED_API",
7393 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
7394 }
7395
Joe Perches0f3c5aa2015-02-13 14:39:05 -08007396# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07007397# and avoid what seem like struct definitions 'struct foo {'
Quentin Monnetced69da12020-08-11 18:35:13 -07007398 if (defined($const_structs) &&
7399 $line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07007400 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007401 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07007402 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08007403 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07007404
7405# use of NR_CPUS is usually wrong
7406# ignore definitions of NR_CPUS and usage to define arrays as likely right
Peng Wang35cdcbf2021-02-25 17:21:44 -08007407# ignore designated initializers using NR_CPUS
Andy Whitcroft773647a2008-03-28 14:15:58 -07007408 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07007409 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7410 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07007411 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7412 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
Peng Wang35cdcbf2021-02-25 17:21:44 -08007413 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
7414 $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07007415 {
Joe Perches000d1cc12011-07-25 17:13:25 -07007416 WARN("NR_CPUS",
7417 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07007418 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07007419
Joe Perches52ea8502013-11-12 15:10:09 -08007420# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
7421 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
7422 ERROR("DEFINE_ARCH_HAS",
7423 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
7424 }
7425
Joe Perchesacd93622015-02-13 14:38:38 -08007426# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07007427 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08007428 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7429 WARN("LIKELY_MISUSE",
7430 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7431 }
7432
Joe Perchesfbe74542021-05-06 18:03:55 -07007433# return sysfs_emit(foo, fmt, ...) fmt without newline
7434 if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ &&
7435 substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) {
7436 my $offset = $+[6] - 1;
7437 if (WARN("SYSFS_EMIT",
7438 "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) &&
7439 $fix) {
7440 substr($fixed[$fixlinenr], $offset, 0) = '\\n';
7441 }
7442 }
7443
Lee Jonesabea1cb2023-12-15 09:36:19 +00007444# check for array definition/declarations that should use flexible arrays instead
7445 if ($sline =~ /^[\+ ]\s*\}(?:\s*__packed)?\s*;\s*$/ &&
7446 $prevline =~ /^\+\s*(?:\}(?:\s*__packed\s*)?|$Type)\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {
7447 if (ERROR("FLEXIBLE_ARRAY",
7448 "Use C99 flexible arrays - see https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays\n" . $hereprev) &&
7449 $1 == '0' && $fix) {
7450 $fixed[$fixlinenr - 1] =~ s/\[\s*0\s*\]/[]/;
7451 }
7452 }
7453
Denis Efremovde3f1862019-09-25 16:49:25 -07007454# nested likely/unlikely calls
7455 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7456 WARN("LIKELY_MISUSE",
7457 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7458 }
7459
Andy Whitcroft691d77b2009-01-06 14:41:16 -08007460# whine mightly about in_atomic
7461 if ($line =~ /\bin_atomic\s*\(/) {
7462 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007463 ERROR("IN_ATOMIC",
7464 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08007465 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007466 WARN("IN_ATOMIC",
7467 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08007468 }
7469 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01007470
Lee Jonesabea1cb2023-12-15 09:36:19 +00007471# Complain about RCU Tasks Trace used outside of BPF (and of course, RCU).
7472 our $rcu_trace_funcs = qr{(?x:
7473 rcu_read_lock_trace |
7474 rcu_read_lock_trace_held |
7475 rcu_read_unlock_trace |
7476 call_rcu_tasks_trace |
7477 synchronize_rcu_tasks_trace |
7478 rcu_barrier_tasks_trace |
7479 rcu_request_urgent_qs_task
7480 )};
7481 our $rcu_trace_paths = qr{(?x:
7482 kernel/bpf/ |
7483 include/linux/bpf |
7484 net/bpf/ |
7485 kernel/rcu/ |
7486 include/linux/rcu
7487 )};
7488 if ($line =~ /\b($rcu_trace_funcs)\s*\(/) {
7489 if ($realfile !~ m{^$rcu_trace_paths}) {
7490 WARN("RCU_TASKS_TRACE",
7491 "use of RCU tasks trace is incorrect outside BPF or core RCU code\n" . $herecurr);
7492 }
7493 }
7494
Peter Zijlstra1704f472010-03-19 01:37:42 +01007495# check for lockdep_set_novalidate_class
7496 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
7497 $line =~ /__lockdep_no_validate__\s*\)/ ) {
7498 if ($realfile !~ m@^kernel/lockdep@ &&
7499 $realfile !~ m@^include/linux/lockdep@ &&
7500 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007501 ERROR("LOCKDEP",
7502 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01007503 }
7504 }
Dave Jones88f88312011-01-12 16:59:59 -08007505
Joe Perchesb392c642015-04-16 12:44:16 -07007506 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7507 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007508 WARN("EXPORTED_WORLD_WRITABLE",
7509 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08007510 }
Joe Perches24358802014-04-03 14:49:13 -07007511
Joe Perches00180462018-02-06 15:38:55 -08007512# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
7513# and whether or not function naming is typical and if
7514# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07007515 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08007516 defined $stat &&
7517 $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*\)/) {
7518 my $var = $1;
7519 my $perms = $2;
7520 my $show = $3;
7521 my $store = $4;
7522 my $octal_perms = perms_to_octal($perms);
7523 if ($show =~ /^${var}_show$/ &&
7524 $store =~ /^${var}_store$/ &&
7525 $octal_perms eq "0644") {
7526 if (WARN("DEVICE_ATTR_RW",
7527 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
7528 $fix) {
7529 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
7530 }
7531 } elsif ($show =~ /^${var}_show$/ &&
7532 $store =~ /^NULL$/ &&
7533 $octal_perms eq "0444") {
7534 if (WARN("DEVICE_ATTR_RO",
7535 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
7536 $fix) {
7537 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
7538 }
7539 } elsif ($show =~ /^NULL$/ &&
7540 $store =~ /^${var}_store$/ &&
7541 $octal_perms eq "0200") {
7542 if (WARN("DEVICE_ATTR_WO",
7543 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
7544 $fix) {
7545 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
7546 }
7547 } elsif ($octal_perms eq "0644" ||
7548 $octal_perms eq "0444" ||
7549 $octal_perms eq "0200") {
7550 my $newshow = "$show";
7551 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
7552 my $newstore = $store;
7553 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
7554 my $rename = "";
7555 if ($show ne $newshow) {
7556 $rename .= " '$show' to '$newshow'";
7557 }
7558 if ($store ne $newstore) {
7559 $rename .= " '$store' to '$newstore'";
7560 }
7561 WARN("DEVICE_ATTR_FUNCTIONS",
7562 "Consider renaming function(s)$rename\n" . $herecurr);
7563 } else {
7564 WARN("DEVICE_ATTR_PERMS",
7565 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
7566 }
7567 }
7568
Joe Perches515a2352014-04-03 14:49:24 -07007569# Mode permission misuses where it seems decimal should be octal
7570# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08007571# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
7572# specific definition of not visible in sysfs.
7573# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
7574# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07007575 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07007576 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07007577 $line =~ /$mode_perms_search/) {
7578 foreach my $entry (@mode_permission_funcs) {
7579 my $func = $entry->[0];
7580 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07007581
Joe Perches459cf0a2016-10-11 13:52:19 -07007582 my $lc = $stat =~ tr@\n@@;
7583 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07007584 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07007585
Joe Perches515a2352014-04-03 14:49:24 -07007586 my $skip_args = "";
7587 if ($arg_pos > 1) {
7588 $arg_pos--;
7589 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
7590 }
Joe Perchesf90774e2016-10-11 13:51:47 -07007591 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07007592 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07007593 my $val = $1;
7594 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08007595 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
7596 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
7597 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07007598 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07007599 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007600 }
7601 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08007602 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07007603 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007604 }
Joe Perches24358802014-04-03 14:49:13 -07007605 }
7606 }
7607 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007608
Joe Perches459cf0a2016-10-11 13:52:19 -07007609# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07007610 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08007611 my $oval = $1;
7612 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07007613 if (WARN("SYMBOLIC_PERMS",
7614 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7615 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08007616 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07007617 }
7618 }
7619
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007620# validate content of MODULE_LICENSE against list from include/linux/module.h
7621 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
7622 my $extracted_string = get_quoted_string($line, $rawline);
7623 my $valid_licenses = qr{
7624 GPL|
7625 GPL\ v2|
7626 GPL\ and\ additional\ rights|
7627 Dual\ BSD/GPL|
7628 Dual\ MIT/GPL|
7629 Dual\ MPL/GPL|
7630 Proprietary
7631 }x;
7632 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
7633 WARN("MODULE_LICENSE",
7634 "unknown module license " . $extracted_string . "\n" . $herecurr);
7635 }
Lee Jonesabea1cb2023-12-15 09:36:19 +00007636 if (!$file && $extracted_string eq '"GPL v2"') {
7637 if (WARN("MODULE_LICENSE",
7638 "Prefer \"GPL\" over \"GPL v2\" - see commit bf7fbeeae6db (\"module: Cure the MODULE_LICENSE \"GPL\" vs. \"GPL v2\" bogosity\")\n" . $herecurr) &&
7639 $fix) {
7640 $fixed[$fixlinenr] =~ s/\bMODULE_LICENSE\s*\(\s*"GPL v2"\s*\)/MODULE_LICENSE("GPL")/;
7641 }
7642 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007643 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07007644
7645# check for sysctl duplicate constants
7646 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
7647 WARN("DUPLICATED_SYSCTL_CONST",
7648 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
7649 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007650 }
7651
7652 # If we have no input at all, then there is nothing to report on
7653 # so just keep quiet.
7654 if ($#rawlines == -1) {
7655 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007656 }
7657
Andy Whitcroft8905a672007-11-28 16:21:06 -08007658 # In mailback mode only produce a report in the negative, for
7659 # things that appear to be patches.
7660 if ($mailback && ($clean == 1 || !$is_patch)) {
7661 exit(0);
7662 }
7663
Dwaipayan Raye73d2712020-12-15 20:44:56 -08007664 # This is not a patch, and we are in 'no-patch' mode so
Andy Whitcroft8905a672007-11-28 16:21:06 -08007665 # just keep quiet.
7666 if (!$chk_patch && !$is_patch) {
7667 exit(0);
7668 }
7669
Stafford Hornea08ffbe2017-10-03 16:16:51 -07007670 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007671 ERROR("NOT_UNIFIED_DIFF",
7672 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007673 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007674 if ($is_patch && $has_commit_log && $chk_signoff) {
7675 if ($signoff == 0) {
7676 ERROR("MISSING_SIGN_OFF",
7677 "Missing Signed-off-by: line(s)\n");
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07007678 } elsif ($authorsignoff != 1) {
7679 # authorsignoff values:
7680 # 0 -> missing sign off
7681 # 1 -> sign off identical
7682 # 2 -> names and addresses match, comments mismatch
7683 # 3 -> addresses match, names different
7684 # 4 -> names match, addresses different
7685 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7686
7687 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7688
7689 if ($authorsignoff == 0) {
7690 ERROR("NO_AUTHOR_SIGN_OFF",
7691 "Missing Signed-off-by: line by nominal patch author '$author'\n");
7692 } elsif ($authorsignoff == 2) {
7693 CHK("FROM_SIGN_OFF_MISMATCH",
7694 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7695 } elsif ($authorsignoff == 3) {
7696 WARN("FROM_SIGN_OFF_MISMATCH",
7697 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7698 } elsif ($authorsignoff == 4) {
7699 WARN("FROM_SIGN_OFF_MISMATCH",
7700 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7701 } elsif ($authorsignoff == 5) {
7702 WARN("FROM_SIGN_OFF_MISMATCH",
7703 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7704 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007705 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007706 }
7707
Andy Whitcroft8905a672007-11-28 16:21:06 -08007708 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007709 if ($summary && !($clean == 1 && $quiet == 1)) {
7710 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08007711 print "total: $cnt_error errors, $cnt_warn warnings, " .
7712 (($check)? "$cnt_chk checks, " : "") .
7713 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07007714 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08007715
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007716 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07007717 # If there were any defects found and not already fixing them
7718 if (!$clean and !$fix) {
7719 print << "EOM"
7720
7721NOTE: For some of the reported defects, checkpatch may be able to
7722 mechanically convert to the typical style using --fix or --fix-inplace.
7723EOM
7724 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007725 # If there were whitespace errors which cleanpatch can fix
7726 # then suggest that.
7727 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07007728 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07007729 print << "EOM"
7730
7731NOTE: Whitespace errors detected.
7732 You may wish to use scripts/cleanpatch or scripts/cleanfile
7733EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007734 }
7735 }
7736
Joe Perchesd752fcc2014-08-06 16:11:05 -07007737 if ($clean == 0 && $fix &&
7738 ("@rawlines" ne "@fixed" ||
7739 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08007740 my $newfile = $filename;
7741 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07007742 my $linecount = 0;
7743 my $f;
7744
Joe Perchesd752fcc2014-08-06 16:11:05 -07007745 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7746
Joe Perches3705ce52013-07-03 15:05:31 -07007747 open($f, '>', $newfile)
7748 or die "$P: Can't open $newfile for write\n";
7749 foreach my $fixed_line (@fixed) {
7750 $linecount++;
7751 if ($file) {
7752 if ($linecount > 3) {
7753 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07007754 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07007755 }
7756 } else {
7757 print $f $fixed_line . "\n";
7758 }
7759 }
7760 close($f);
7761
7762 if (!$quiet) {
7763 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07007764
Joe Perches3705ce52013-07-03 15:05:31 -07007765Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7766
7767Do _NOT_ trust the results written to this file.
7768Do _NOT_ submit these changes without inspecting them for correctness.
7769
7770This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7771No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07007772EOM
7773 }
7774 }
7775
Joe Perchesd8469f12015-06-25 15:03:00 -07007776 if ($quiet == 0) {
7777 print "\n";
7778 if ($clean == 1) {
7779 print "$vname has no obvious style problems and is ready for submission.\n";
7780 } else {
7781 print "$vname has style problems, please review.\n";
7782 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007783 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007784 return $clean;
7785}